162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci#include <linux/init.h> 362306a36Sopenharmony_ci#include <linux/scatterlist.h> 462306a36Sopenharmony_ci#include <linux/mempool.h> 562306a36Sopenharmony_ci#include <linux/slab.h> 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci#define SG_MEMPOOL_NR ARRAY_SIZE(sg_pools) 862306a36Sopenharmony_ci#define SG_MEMPOOL_SIZE 2 962306a36Sopenharmony_ci 1062306a36Sopenharmony_cistruct sg_pool { 1162306a36Sopenharmony_ci size_t size; 1262306a36Sopenharmony_ci char *name; 1362306a36Sopenharmony_ci struct kmem_cache *slab; 1462306a36Sopenharmony_ci mempool_t *pool; 1562306a36Sopenharmony_ci}; 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci#define SP(x) { .size = x, "sgpool-" __stringify(x) } 1862306a36Sopenharmony_ci#if (SG_CHUNK_SIZE < 32) 1962306a36Sopenharmony_ci#error SG_CHUNK_SIZE is too small (must be 32 or greater) 2062306a36Sopenharmony_ci#endif 2162306a36Sopenharmony_cistatic struct sg_pool sg_pools[] = { 2262306a36Sopenharmony_ci SP(8), 2362306a36Sopenharmony_ci SP(16), 2462306a36Sopenharmony_ci#if (SG_CHUNK_SIZE > 32) 2562306a36Sopenharmony_ci SP(32), 2662306a36Sopenharmony_ci#if (SG_CHUNK_SIZE > 64) 2762306a36Sopenharmony_ci SP(64), 2862306a36Sopenharmony_ci#if (SG_CHUNK_SIZE > 128) 2962306a36Sopenharmony_ci SP(128), 3062306a36Sopenharmony_ci#if (SG_CHUNK_SIZE > 256) 3162306a36Sopenharmony_ci#error SG_CHUNK_SIZE is too large (256 MAX) 3262306a36Sopenharmony_ci#endif 3362306a36Sopenharmony_ci#endif 3462306a36Sopenharmony_ci#endif 3562306a36Sopenharmony_ci#endif 3662306a36Sopenharmony_ci SP(SG_CHUNK_SIZE) 3762306a36Sopenharmony_ci}; 3862306a36Sopenharmony_ci#undef SP 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_cistatic inline unsigned int sg_pool_index(unsigned short nents) 4162306a36Sopenharmony_ci{ 4262306a36Sopenharmony_ci unsigned int index; 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci BUG_ON(nents > SG_CHUNK_SIZE); 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci if (nents <= 8) 4762306a36Sopenharmony_ci index = 0; 4862306a36Sopenharmony_ci else 4962306a36Sopenharmony_ci index = get_count_order(nents) - 3; 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci return index; 5262306a36Sopenharmony_ci} 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_cistatic void sg_pool_free(struct scatterlist *sgl, unsigned int nents) 5562306a36Sopenharmony_ci{ 5662306a36Sopenharmony_ci struct sg_pool *sgp; 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci sgp = sg_pools + sg_pool_index(nents); 5962306a36Sopenharmony_ci mempool_free(sgl, sgp->pool); 6062306a36Sopenharmony_ci} 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_cistatic struct scatterlist *sg_pool_alloc(unsigned int nents, gfp_t gfp_mask) 6362306a36Sopenharmony_ci{ 6462306a36Sopenharmony_ci struct sg_pool *sgp; 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci sgp = sg_pools + sg_pool_index(nents); 6762306a36Sopenharmony_ci return mempool_alloc(sgp->pool, gfp_mask); 6862306a36Sopenharmony_ci} 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci/** 7162306a36Sopenharmony_ci * sg_free_table_chained - Free a previously mapped sg table 7262306a36Sopenharmony_ci * @table: The sg table header to use 7362306a36Sopenharmony_ci * @nents_first_chunk: size of the first_chunk SGL passed to 7462306a36Sopenharmony_ci * sg_alloc_table_chained 7562306a36Sopenharmony_ci * 7662306a36Sopenharmony_ci * Description: 7762306a36Sopenharmony_ci * Free an sg table previously allocated and setup with 7862306a36Sopenharmony_ci * sg_alloc_table_chained(). 7962306a36Sopenharmony_ci * 8062306a36Sopenharmony_ci * @nents_first_chunk has to be same with that same parameter passed 8162306a36Sopenharmony_ci * to sg_alloc_table_chained(). 8262306a36Sopenharmony_ci * 8362306a36Sopenharmony_ci **/ 8462306a36Sopenharmony_civoid sg_free_table_chained(struct sg_table *table, 8562306a36Sopenharmony_ci unsigned nents_first_chunk) 8662306a36Sopenharmony_ci{ 8762306a36Sopenharmony_ci if (table->orig_nents <= nents_first_chunk) 8862306a36Sopenharmony_ci return; 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci if (nents_first_chunk == 1) 9162306a36Sopenharmony_ci nents_first_chunk = 0; 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci __sg_free_table(table, SG_CHUNK_SIZE, nents_first_chunk, sg_pool_free, 9462306a36Sopenharmony_ci table->orig_nents); 9562306a36Sopenharmony_ci} 9662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(sg_free_table_chained); 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci/** 9962306a36Sopenharmony_ci * sg_alloc_table_chained - Allocate and chain SGLs in an sg table 10062306a36Sopenharmony_ci * @table: The sg table header to use 10162306a36Sopenharmony_ci * @nents: Number of entries in sg list 10262306a36Sopenharmony_ci * @first_chunk: first SGL 10362306a36Sopenharmony_ci * @nents_first_chunk: number of the SGL of @first_chunk 10462306a36Sopenharmony_ci * 10562306a36Sopenharmony_ci * Description: 10662306a36Sopenharmony_ci * Allocate and chain SGLs in an sg table. If @nents@ is larger than 10762306a36Sopenharmony_ci * @nents_first_chunk a chained sg table will be setup. @first_chunk is 10862306a36Sopenharmony_ci * ignored if nents_first_chunk <= 1 because user expects the SGL points 10962306a36Sopenharmony_ci * non-chain SGL. 11062306a36Sopenharmony_ci * 11162306a36Sopenharmony_ci **/ 11262306a36Sopenharmony_ciint sg_alloc_table_chained(struct sg_table *table, int nents, 11362306a36Sopenharmony_ci struct scatterlist *first_chunk, unsigned nents_first_chunk) 11462306a36Sopenharmony_ci{ 11562306a36Sopenharmony_ci int ret; 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci BUG_ON(!nents); 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ci if (first_chunk && nents_first_chunk) { 12062306a36Sopenharmony_ci if (nents <= nents_first_chunk) { 12162306a36Sopenharmony_ci table->nents = table->orig_nents = nents; 12262306a36Sopenharmony_ci sg_init_table(table->sgl, nents); 12362306a36Sopenharmony_ci return 0; 12462306a36Sopenharmony_ci } 12562306a36Sopenharmony_ci } 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci /* User supposes that the 1st SGL includes real entry */ 12862306a36Sopenharmony_ci if (nents_first_chunk <= 1) { 12962306a36Sopenharmony_ci first_chunk = NULL; 13062306a36Sopenharmony_ci nents_first_chunk = 0; 13162306a36Sopenharmony_ci } 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ci ret = __sg_alloc_table(table, nents, SG_CHUNK_SIZE, 13462306a36Sopenharmony_ci first_chunk, nents_first_chunk, 13562306a36Sopenharmony_ci GFP_ATOMIC, sg_pool_alloc); 13662306a36Sopenharmony_ci if (unlikely(ret)) 13762306a36Sopenharmony_ci sg_free_table_chained(table, nents_first_chunk); 13862306a36Sopenharmony_ci return ret; 13962306a36Sopenharmony_ci} 14062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(sg_alloc_table_chained); 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_cistatic __init int sg_pool_init(void) 14362306a36Sopenharmony_ci{ 14462306a36Sopenharmony_ci int i; 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci for (i = 0; i < SG_MEMPOOL_NR; i++) { 14762306a36Sopenharmony_ci struct sg_pool *sgp = sg_pools + i; 14862306a36Sopenharmony_ci int size = sgp->size * sizeof(struct scatterlist); 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci sgp->slab = kmem_cache_create(sgp->name, size, 0, 15162306a36Sopenharmony_ci SLAB_HWCACHE_ALIGN, NULL); 15262306a36Sopenharmony_ci if (!sgp->slab) { 15362306a36Sopenharmony_ci printk(KERN_ERR "SG_POOL: can't init sg slab %s\n", 15462306a36Sopenharmony_ci sgp->name); 15562306a36Sopenharmony_ci goto cleanup_sdb; 15662306a36Sopenharmony_ci } 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci sgp->pool = mempool_create_slab_pool(SG_MEMPOOL_SIZE, 15962306a36Sopenharmony_ci sgp->slab); 16062306a36Sopenharmony_ci if (!sgp->pool) { 16162306a36Sopenharmony_ci printk(KERN_ERR "SG_POOL: can't init sg mempool %s\n", 16262306a36Sopenharmony_ci sgp->name); 16362306a36Sopenharmony_ci goto cleanup_sdb; 16462306a36Sopenharmony_ci } 16562306a36Sopenharmony_ci } 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci return 0; 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_cicleanup_sdb: 17062306a36Sopenharmony_ci for (i = 0; i < SG_MEMPOOL_NR; i++) { 17162306a36Sopenharmony_ci struct sg_pool *sgp = sg_pools + i; 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_ci mempool_destroy(sgp->pool); 17462306a36Sopenharmony_ci kmem_cache_destroy(sgp->slab); 17562306a36Sopenharmony_ci } 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci return -ENOMEM; 17862306a36Sopenharmony_ci} 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_cisubsys_initcall(sg_pool_init); 181