blob: f1cc8372df679897cc1f22ad4e876a0f51010cb4 [file] [log] [blame]
Ming Lin9b1d6c82016-04-04 14:48:11 -07001#include <linux/module.h>
2#include <linux/scatterlist.h>
3#include <linux/mempool.h>
4#include <linux/slab.h>
5
6#define SG_MEMPOOL_NR ARRAY_SIZE(sg_pools)
7#define SG_MEMPOOL_SIZE 2
8
9struct sg_pool {
10 size_t size;
11 char *name;
12 struct kmem_cache *slab;
13 mempool_t *pool;
14};
15
16#define SP(x) { .size = x, "sgpool-" __stringify(x) }
17#if (SG_CHUNK_SIZE < 32)
18#error SG_CHUNK_SIZE is too small (must be 32 or greater)
19#endif
20static struct sg_pool sg_pools[] = {
21 SP(8),
22 SP(16),
23#if (SG_CHUNK_SIZE > 32)
24 SP(32),
25#if (SG_CHUNK_SIZE > 64)
26 SP(64),
27#if (SG_CHUNK_SIZE > 128)
28 SP(128),
29#if (SG_CHUNK_SIZE > 256)
30#error SG_CHUNK_SIZE is too large (256 MAX)
31#endif
32#endif
33#endif
34#endif
35 SP(SG_CHUNK_SIZE)
36};
37#undef SP
38
39static inline unsigned int sg_pool_index(unsigned short nents)
40{
41 unsigned int index;
42
43 BUG_ON(nents > SG_CHUNK_SIZE);
44
45 if (nents <= 8)
46 index = 0;
47 else
48 index = get_count_order(nents) - 3;
49
50 return index;
51}
52
53static void sg_pool_free(struct scatterlist *sgl, unsigned int nents)
54{
55 struct sg_pool *sgp;
56
57 sgp = sg_pools + sg_pool_index(nents);
58 mempool_free(sgl, sgp->pool);
59}
60
61static struct scatterlist *sg_pool_alloc(unsigned int nents, gfp_t gfp_mask)
62{
63 struct sg_pool *sgp;
64
65 sgp = sg_pools + sg_pool_index(nents);
66 return mempool_alloc(sgp->pool, gfp_mask);
67}
68
69/**
70 * sg_free_table_chained - Free a previously mapped sg table
71 * @table: The sg table header to use
Ming Lei46358732019-04-28 15:39:30 +080072 * @nents_first_chunk: size of the first_chunk SGL passed to
73 * sg_alloc_table_chained
Ming Lin9b1d6c82016-04-04 14:48:11 -070074 *
75 * Description:
76 * Free an sg table previously allocated and setup with
77 * sg_alloc_table_chained().
78 *
Ming Lei46358732019-04-28 15:39:30 +080079 * @nents_first_chunk has to be same with that same parameter passed
80 * to sg_alloc_table_chained().
81 *
Ming Lin9b1d6c82016-04-04 14:48:11 -070082 **/
Ming Lei46358732019-04-28 15:39:30 +080083void sg_free_table_chained(struct sg_table *table,
84 unsigned nents_first_chunk)
Ming Lin9b1d6c82016-04-04 14:48:11 -070085{
Ming Lei46358732019-04-28 15:39:30 +080086 if (table->orig_nents <= nents_first_chunk)
Ming Lin9b1d6c82016-04-04 14:48:11 -070087 return;
Ming Lei46358732019-04-28 15:39:30 +080088
89 if (nents_first_chunk == 1)
90 nents_first_chunk = 0;
91
92 __sg_free_table(table, SG_CHUNK_SIZE, nents_first_chunk, sg_pool_free);
Ming Lin9b1d6c82016-04-04 14:48:11 -070093}
94EXPORT_SYMBOL_GPL(sg_free_table_chained);
95
96/**
97 * sg_alloc_table_chained - Allocate and chain SGLs in an sg table
98 * @table: The sg table header to use
99 * @nents: Number of entries in sg list
100 * @first_chunk: first SGL
Ming Lei46358732019-04-28 15:39:30 +0800101 * @nents_first_chunk: number of the SGL of @first_chunk
Ming Lin9b1d6c82016-04-04 14:48:11 -0700102 *
103 * Description:
104 * Allocate and chain SGLs in an sg table. If @nents@ is larger than
Ming Leib79d9a02019-06-06 16:34:08 +0800105 * @nents_first_chunk a chained sg table will be setup. @first_chunk is
106 * ignored if nents_first_chunk <= 1 because user expects the SGL points
107 * non-chain SGL.
Ming Lin9b1d6c82016-04-04 14:48:11 -0700108 *
109 **/
110int sg_alloc_table_chained(struct sg_table *table, int nents,
Ming Lei46358732019-04-28 15:39:30 +0800111 struct scatterlist *first_chunk, unsigned nents_first_chunk)
Ming Lin9b1d6c82016-04-04 14:48:11 -0700112{
113 int ret;
114
115 BUG_ON(!nents);
116
Ming Lei46358732019-04-28 15:39:30 +0800117 if (first_chunk && nents_first_chunk) {
118 if (nents <= nents_first_chunk) {
Ming Lin9b1d6c82016-04-04 14:48:11 -0700119 table->nents = table->orig_nents = nents;
120 sg_init_table(table->sgl, nents);
121 return 0;
122 }
123 }
124
Ming Lei46358732019-04-28 15:39:30 +0800125 /* User supposes that the 1st SGL includes real entry */
Ming Leib79d9a02019-06-06 16:34:08 +0800126 if (nents_first_chunk <= 1) {
Ming Lei46358732019-04-28 15:39:30 +0800127 first_chunk = NULL;
128 nents_first_chunk = 0;
129 }
130
Ming Lin9b1d6c82016-04-04 14:48:11 -0700131 ret = __sg_alloc_table(table, nents, SG_CHUNK_SIZE,
Ming Lei46358732019-04-28 15:39:30 +0800132 first_chunk, nents_first_chunk,
133 GFP_ATOMIC, sg_pool_alloc);
Ming Lin9b1d6c82016-04-04 14:48:11 -0700134 if (unlikely(ret))
Ming Lei46358732019-04-28 15:39:30 +0800135 sg_free_table_chained(table, nents_first_chunk);
Ming Lin9b1d6c82016-04-04 14:48:11 -0700136 return ret;
137}
138EXPORT_SYMBOL_GPL(sg_alloc_table_chained);
139
140static __init int sg_pool_init(void)
141{
142 int i;
143
144 for (i = 0; i < SG_MEMPOOL_NR; i++) {
145 struct sg_pool *sgp = sg_pools + i;
146 int size = sgp->size * sizeof(struct scatterlist);
147
148 sgp->slab = kmem_cache_create(sgp->name, size, 0,
149 SLAB_HWCACHE_ALIGN, NULL);
150 if (!sgp->slab) {
151 printk(KERN_ERR "SG_POOL: can't init sg slab %s\n",
152 sgp->name);
153 goto cleanup_sdb;
154 }
155
156 sgp->pool = mempool_create_slab_pool(SG_MEMPOOL_SIZE,
157 sgp->slab);
158 if (!sgp->pool) {
159 printk(KERN_ERR "SG_POOL: can't init sg mempool %s\n",
160 sgp->name);
161 goto cleanup_sdb;
162 }
163 }
164
165 return 0;
166
167cleanup_sdb:
168 for (i = 0; i < SG_MEMPOOL_NR; i++) {
169 struct sg_pool *sgp = sg_pools + i;
zhong jiang7f476712018-10-30 15:05:37 -0700170
171 mempool_destroy(sgp->pool);
172 kmem_cache_destroy(sgp->slab);
Ming Lin9b1d6c82016-04-04 14:48:11 -0700173 }
174
175 return -ENOMEM;
176}
177
178static __exit void sg_pool_exit(void)
179{
180 int i;
181
182 for (i = 0; i < SG_MEMPOOL_NR; i++) {
183 struct sg_pool *sgp = sg_pools + i;
184 mempool_destroy(sgp->pool);
185 kmem_cache_destroy(sgp->slab);
186 }
187}
188
189module_init(sg_pool_init);
190module_exit(sg_pool_exit);