Source
x
// SPDX-License-Identifier: GPL-2.0-only
struct sg_pool {
size_t size;
char *name;
struct kmem_cache *slab;
mempool_t *pool;
};
static struct sg_pool sg_pools[] = {
SP(8),
SP(16),
SP(32),
SP(64),
SP(128),
SP(SG_CHUNK_SIZE)
};
static inline unsigned int sg_pool_index(unsigned short nents)
{
unsigned int index;
BUG_ON(nents > SG_CHUNK_SIZE);
if (nents <= 8)
index = 0;
else
index = get_count_order(nents) - 3;
return index;
}
static void sg_pool_free(struct scatterlist *sgl, unsigned int nents)
{
struct sg_pool *sgp;
sgp = sg_pools + sg_pool_index(nents);
mempool_free(sgl, sgp->pool);
}
static struct scatterlist *sg_pool_alloc(unsigned int nents, gfp_t gfp_mask)
{
struct sg_pool *sgp;
sgp = sg_pools + sg_pool_index(nents);
return mempool_alloc(sgp->pool, gfp_mask);
}
/**
* sg_free_table_chained - Free a previously mapped sg table
* @table: The sg table header to use
* @first_chunk: was first_chunk not NULL in sg_alloc_table_chained?
*
* Description:
* Free an sg table previously allocated and setup with
* sg_alloc_table_chained().
*
**/
void sg_free_table_chained(struct sg_table *table, bool first_chunk)
{
if (first_chunk && table->orig_nents <= SG_CHUNK_SIZE)
return;
__sg_free_table(table, SG_CHUNK_SIZE, first_chunk, sg_pool_free);
}
EXPORT_SYMBOL_GPL(sg_free_table_chained);
/**
* sg_alloc_table_chained - Allocate and chain SGLs in an sg table
* @table: The sg table header to use
* @nents: Number of entries in sg list
* @first_chunk: first SGL