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