1 #include <linux/module.h>
2 #include <linux/scatterlist.h>
3 #include <linux/mempool.h>
4 #include <linux/slab.h>
6 #define SG_MEMPOOL_NR ARRAY_SIZE(sg_pools)
7 #define SG_MEMPOOL_SIZE 2
12 struct kmem_cache *slab;
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)
20 static struct sg_pool sg_pools[] = {
23 #if (SG_CHUNK_SIZE > 32)
25 #if (SG_CHUNK_SIZE > 64)
27 #if (SG_CHUNK_SIZE > 128)
29 #if (SG_CHUNK_SIZE > 256)
30 #error SG_CHUNK_SIZE is too large (256 MAX)
39 static inline unsigned int sg_pool_index(unsigned short nents)
43 BUG_ON(nents > SG_CHUNK_SIZE);
48 index = get_count_order(nents) - 3;
53 static void sg_pool_free(struct scatterlist *sgl, unsigned int nents)
57 sgp = sg_pools + sg_pool_index(nents);
58 mempool_free(sgl, sgp->pool);
61 static struct scatterlist *sg_pool_alloc(unsigned int nents, gfp_t gfp_mask)
65 sgp = sg_pools + sg_pool_index(nents);
66 return mempool_alloc(sgp->pool, gfp_mask);
70 * sg_free_table_chained - Free a previously mapped sg table
71 * @table: The sg table header to use
72 * @nents_first_chunk: size of the first_chunk SGL passed to
73 * sg_alloc_table_chained
76 * Free an sg table previously allocated and setup with
77 * sg_alloc_table_chained().
79 * @nents_first_chunk has to be same with that same parameter passed
80 * to sg_alloc_table_chained().
83 void sg_free_table_chained(struct sg_table *table,
84 unsigned nents_first_chunk)
86 if (table->orig_nents <= nents_first_chunk)
89 if (nents_first_chunk == 1)
90 nents_first_chunk = 0;
92 __sg_free_table(table, SG_CHUNK_SIZE, nents_first_chunk, sg_pool_free);
94 EXPORT_SYMBOL_GPL(sg_free_table_chained);
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
101 * @nents_first_chunk: number of the SGL of @first_chunk
104 * Allocate and chain SGLs in an sg table. If @nents@ is larger than
105 * @nents_first_chunk a chained sg table will be setup.
108 int sg_alloc_table_chained(struct sg_table *table, int nents,
109 struct scatterlist *first_chunk, unsigned nents_first_chunk)
115 if (first_chunk && nents_first_chunk) {
116 if (nents <= nents_first_chunk) {
117 table->nents = table->orig_nents = nents;
118 sg_init_table(table->sgl, nents);
123 /* User supposes that the 1st SGL includes real entry */
124 if (nents_first_chunk == 1) {
126 nents_first_chunk = 0;
129 ret = __sg_alloc_table(table, nents, SG_CHUNK_SIZE,
130 first_chunk, nents_first_chunk,
131 GFP_ATOMIC, sg_pool_alloc);
133 sg_free_table_chained(table, nents_first_chunk);
136 EXPORT_SYMBOL_GPL(sg_alloc_table_chained);
138 static __init int sg_pool_init(void)
142 for (i = 0; i < SG_MEMPOOL_NR; i++) {
143 struct sg_pool *sgp = sg_pools + i;
144 int size = sgp->size * sizeof(struct scatterlist);
146 sgp->slab = kmem_cache_create(sgp->name, size, 0,
147 SLAB_HWCACHE_ALIGN, NULL);
149 printk(KERN_ERR "SG_POOL: can't init sg slab %s\n",
154 sgp->pool = mempool_create_slab_pool(SG_MEMPOOL_SIZE,
157 printk(KERN_ERR "SG_POOL: can't init sg mempool %s\n",
166 for (i = 0; i < SG_MEMPOOL_NR; i++) {
167 struct sg_pool *sgp = sg_pools + i;
169 mempool_destroy(sgp->pool);
170 kmem_cache_destroy(sgp->slab);
176 static __exit void sg_pool_exit(void)
180 for (i = 0; i < SG_MEMPOOL_NR; i++) {
181 struct sg_pool *sgp = sg_pools + i;
182 mempool_destroy(sgp->pool);
183 kmem_cache_destroy(sgp->slab);
187 module_init(sg_pool_init);
188 module_exit(sg_pool_exit);