162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *  linux/mm/mempool.c
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  memory buffer pool support. Such pools are mostly used
662306a36Sopenharmony_ci *  for guaranteed, deadlock-free memory allocations during
762306a36Sopenharmony_ci *  extreme VM load.
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci *  started by Ingo Molnar, Copyright (C) 2001
1062306a36Sopenharmony_ci *  debugging by David Rientjes, Copyright (C) 2015
1162306a36Sopenharmony_ci */
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <linux/mm.h>
1462306a36Sopenharmony_ci#include <linux/slab.h>
1562306a36Sopenharmony_ci#include <linux/highmem.h>
1662306a36Sopenharmony_ci#include <linux/kasan.h>
1762306a36Sopenharmony_ci#include <linux/kmemleak.h>
1862306a36Sopenharmony_ci#include <linux/export.h>
1962306a36Sopenharmony_ci#include <linux/mempool.h>
2062306a36Sopenharmony_ci#include <linux/writeback.h>
2162306a36Sopenharmony_ci#include "slab.h"
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB_DEBUG_ON)
2462306a36Sopenharmony_cistatic void poison_error(mempool_t *pool, void *element, size_t size,
2562306a36Sopenharmony_ci			 size_t byte)
2662306a36Sopenharmony_ci{
2762306a36Sopenharmony_ci	const int nr = pool->curr_nr;
2862306a36Sopenharmony_ci	const int start = max_t(int, byte - (BITS_PER_LONG / 8), 0);
2962306a36Sopenharmony_ci	const int end = min_t(int, byte + (BITS_PER_LONG / 8), size);
3062306a36Sopenharmony_ci	int i;
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci	pr_err("BUG: mempool element poison mismatch\n");
3362306a36Sopenharmony_ci	pr_err("Mempool %p size %zu\n", pool, size);
3462306a36Sopenharmony_ci	pr_err(" nr=%d @ %p: %s0x", nr, element, start > 0 ? "... " : "");
3562306a36Sopenharmony_ci	for (i = start; i < end; i++)
3662306a36Sopenharmony_ci		pr_cont("%x ", *(u8 *)(element + i));
3762306a36Sopenharmony_ci	pr_cont("%s\n", end < size ? "..." : "");
3862306a36Sopenharmony_ci	dump_stack();
3962306a36Sopenharmony_ci}
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cistatic void __check_element(mempool_t *pool, void *element, size_t size)
4262306a36Sopenharmony_ci{
4362306a36Sopenharmony_ci	u8 *obj = element;
4462306a36Sopenharmony_ci	size_t i;
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci	for (i = 0; i < size; i++) {
4762306a36Sopenharmony_ci		u8 exp = (i < size - 1) ? POISON_FREE : POISON_END;
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci		if (obj[i] != exp) {
5062306a36Sopenharmony_ci			poison_error(pool, element, size, i);
5162306a36Sopenharmony_ci			return;
5262306a36Sopenharmony_ci		}
5362306a36Sopenharmony_ci	}
5462306a36Sopenharmony_ci	memset(obj, POISON_INUSE, size);
5562306a36Sopenharmony_ci}
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_cistatic void check_element(mempool_t *pool, void *element)
5862306a36Sopenharmony_ci{
5962306a36Sopenharmony_ci	/* Mempools backed by slab allocator */
6062306a36Sopenharmony_ci	if (pool->free == mempool_kfree) {
6162306a36Sopenharmony_ci		__check_element(pool, element, (size_t)pool->pool_data);
6262306a36Sopenharmony_ci	} else if (pool->free == mempool_free_slab) {
6362306a36Sopenharmony_ci		__check_element(pool, element, kmem_cache_size(pool->pool_data));
6462306a36Sopenharmony_ci	} else if (pool->free == mempool_free_pages) {
6562306a36Sopenharmony_ci		/* Mempools backed by page allocator */
6662306a36Sopenharmony_ci		int order = (int)(long)pool->pool_data;
6762306a36Sopenharmony_ci		void *addr = kmap_atomic((struct page *)element);
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci		__check_element(pool, addr, 1UL << (PAGE_SHIFT + order));
7062306a36Sopenharmony_ci		kunmap_atomic(addr);
7162306a36Sopenharmony_ci	}
7262306a36Sopenharmony_ci}
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_cistatic void __poison_element(void *element, size_t size)
7562306a36Sopenharmony_ci{
7662306a36Sopenharmony_ci	u8 *obj = element;
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	memset(obj, POISON_FREE, size - 1);
7962306a36Sopenharmony_ci	obj[size - 1] = POISON_END;
8062306a36Sopenharmony_ci}
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cistatic void poison_element(mempool_t *pool, void *element)
8362306a36Sopenharmony_ci{
8462306a36Sopenharmony_ci	/* Mempools backed by slab allocator */
8562306a36Sopenharmony_ci	if (pool->alloc == mempool_kmalloc) {
8662306a36Sopenharmony_ci		__poison_element(element, (size_t)pool->pool_data);
8762306a36Sopenharmony_ci	} else if (pool->alloc == mempool_alloc_slab) {
8862306a36Sopenharmony_ci		__poison_element(element, kmem_cache_size(pool->pool_data));
8962306a36Sopenharmony_ci	} else if (pool->alloc == mempool_alloc_pages) {
9062306a36Sopenharmony_ci		/* Mempools backed by page allocator */
9162306a36Sopenharmony_ci		int order = (int)(long)pool->pool_data;
9262306a36Sopenharmony_ci		void *addr = kmap_atomic((struct page *)element);
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci		__poison_element(addr, 1UL << (PAGE_SHIFT + order));
9562306a36Sopenharmony_ci		kunmap_atomic(addr);
9662306a36Sopenharmony_ci	}
9762306a36Sopenharmony_ci}
9862306a36Sopenharmony_ci#else /* CONFIG_DEBUG_SLAB || CONFIG_SLUB_DEBUG_ON */
9962306a36Sopenharmony_cistatic inline void check_element(mempool_t *pool, void *element)
10062306a36Sopenharmony_ci{
10162306a36Sopenharmony_ci}
10262306a36Sopenharmony_cistatic inline void poison_element(mempool_t *pool, void *element)
10362306a36Sopenharmony_ci{
10462306a36Sopenharmony_ci}
10562306a36Sopenharmony_ci#endif /* CONFIG_DEBUG_SLAB || CONFIG_SLUB_DEBUG_ON */
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_cistatic __always_inline void kasan_poison_element(mempool_t *pool, void *element)
10862306a36Sopenharmony_ci{
10962306a36Sopenharmony_ci	if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
11062306a36Sopenharmony_ci		kasan_slab_free_mempool(element);
11162306a36Sopenharmony_ci	else if (pool->alloc == mempool_alloc_pages)
11262306a36Sopenharmony_ci		kasan_poison_pages(element, (unsigned long)pool->pool_data,
11362306a36Sopenharmony_ci				   false);
11462306a36Sopenharmony_ci}
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_cistatic void kasan_unpoison_element(mempool_t *pool, void *element)
11762306a36Sopenharmony_ci{
11862306a36Sopenharmony_ci	if (pool->alloc == mempool_kmalloc)
11962306a36Sopenharmony_ci		kasan_unpoison_range(element, (size_t)pool->pool_data);
12062306a36Sopenharmony_ci	else if (pool->alloc == mempool_alloc_slab)
12162306a36Sopenharmony_ci		kasan_unpoison_range(element, kmem_cache_size(pool->pool_data));
12262306a36Sopenharmony_ci	else if (pool->alloc == mempool_alloc_pages)
12362306a36Sopenharmony_ci		kasan_unpoison_pages(element, (unsigned long)pool->pool_data,
12462306a36Sopenharmony_ci				     false);
12562306a36Sopenharmony_ci}
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_cistatic __always_inline void add_element(mempool_t *pool, void *element)
12862306a36Sopenharmony_ci{
12962306a36Sopenharmony_ci	BUG_ON(pool->curr_nr >= pool->min_nr);
13062306a36Sopenharmony_ci	poison_element(pool, element);
13162306a36Sopenharmony_ci	kasan_poison_element(pool, element);
13262306a36Sopenharmony_ci	pool->elements[pool->curr_nr++] = element;
13362306a36Sopenharmony_ci}
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_cistatic void *remove_element(mempool_t *pool)
13662306a36Sopenharmony_ci{
13762306a36Sopenharmony_ci	void *element = pool->elements[--pool->curr_nr];
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	BUG_ON(pool->curr_nr < 0);
14062306a36Sopenharmony_ci	kasan_unpoison_element(pool, element);
14162306a36Sopenharmony_ci	check_element(pool, element);
14262306a36Sopenharmony_ci	return element;
14362306a36Sopenharmony_ci}
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci/**
14662306a36Sopenharmony_ci * mempool_exit - exit a mempool initialized with mempool_init()
14762306a36Sopenharmony_ci * @pool:      pointer to the memory pool which was initialized with
14862306a36Sopenharmony_ci *             mempool_init().
14962306a36Sopenharmony_ci *
15062306a36Sopenharmony_ci * Free all reserved elements in @pool and @pool itself.  This function
15162306a36Sopenharmony_ci * only sleeps if the free_fn() function sleeps.
15262306a36Sopenharmony_ci *
15362306a36Sopenharmony_ci * May be called on a zeroed but uninitialized mempool (i.e. allocated with
15462306a36Sopenharmony_ci * kzalloc()).
15562306a36Sopenharmony_ci */
15662306a36Sopenharmony_civoid mempool_exit(mempool_t *pool)
15762306a36Sopenharmony_ci{
15862306a36Sopenharmony_ci	while (pool->curr_nr) {
15962306a36Sopenharmony_ci		void *element = remove_element(pool);
16062306a36Sopenharmony_ci		pool->free(element, pool->pool_data);
16162306a36Sopenharmony_ci	}
16262306a36Sopenharmony_ci	kfree(pool->elements);
16362306a36Sopenharmony_ci	pool->elements = NULL;
16462306a36Sopenharmony_ci}
16562306a36Sopenharmony_ciEXPORT_SYMBOL(mempool_exit);
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci/**
16862306a36Sopenharmony_ci * mempool_destroy - deallocate a memory pool
16962306a36Sopenharmony_ci * @pool:      pointer to the memory pool which was allocated via
17062306a36Sopenharmony_ci *             mempool_create().
17162306a36Sopenharmony_ci *
17262306a36Sopenharmony_ci * Free all reserved elements in @pool and @pool itself.  This function
17362306a36Sopenharmony_ci * only sleeps if the free_fn() function sleeps.
17462306a36Sopenharmony_ci */
17562306a36Sopenharmony_civoid mempool_destroy(mempool_t *pool)
17662306a36Sopenharmony_ci{
17762306a36Sopenharmony_ci	if (unlikely(!pool))
17862306a36Sopenharmony_ci		return;
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	mempool_exit(pool);
18162306a36Sopenharmony_ci	kfree(pool);
18262306a36Sopenharmony_ci}
18362306a36Sopenharmony_ciEXPORT_SYMBOL(mempool_destroy);
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ciint mempool_init_node(mempool_t *pool, int min_nr, mempool_alloc_t *alloc_fn,
18662306a36Sopenharmony_ci		      mempool_free_t *free_fn, void *pool_data,
18762306a36Sopenharmony_ci		      gfp_t gfp_mask, int node_id)
18862306a36Sopenharmony_ci{
18962306a36Sopenharmony_ci	spin_lock_init(&pool->lock);
19062306a36Sopenharmony_ci	pool->min_nr	= min_nr;
19162306a36Sopenharmony_ci	pool->pool_data = pool_data;
19262306a36Sopenharmony_ci	pool->alloc	= alloc_fn;
19362306a36Sopenharmony_ci	pool->free	= free_fn;
19462306a36Sopenharmony_ci	init_waitqueue_head(&pool->wait);
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci	pool->elements = kmalloc_array_node(min_nr, sizeof(void *),
19762306a36Sopenharmony_ci					    gfp_mask, node_id);
19862306a36Sopenharmony_ci	if (!pool->elements)
19962306a36Sopenharmony_ci		return -ENOMEM;
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci	/*
20262306a36Sopenharmony_ci	 * First pre-allocate the guaranteed number of buffers.
20362306a36Sopenharmony_ci	 */
20462306a36Sopenharmony_ci	while (pool->curr_nr < pool->min_nr) {
20562306a36Sopenharmony_ci		void *element;
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci		element = pool->alloc(gfp_mask, pool->pool_data);
20862306a36Sopenharmony_ci		if (unlikely(!element)) {
20962306a36Sopenharmony_ci			mempool_exit(pool);
21062306a36Sopenharmony_ci			return -ENOMEM;
21162306a36Sopenharmony_ci		}
21262306a36Sopenharmony_ci		add_element(pool, element);
21362306a36Sopenharmony_ci	}
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	return 0;
21662306a36Sopenharmony_ci}
21762306a36Sopenharmony_ciEXPORT_SYMBOL(mempool_init_node);
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci/**
22062306a36Sopenharmony_ci * mempool_init - initialize a memory pool
22162306a36Sopenharmony_ci * @pool:      pointer to the memory pool that should be initialized
22262306a36Sopenharmony_ci * @min_nr:    the minimum number of elements guaranteed to be
22362306a36Sopenharmony_ci *             allocated for this pool.
22462306a36Sopenharmony_ci * @alloc_fn:  user-defined element-allocation function.
22562306a36Sopenharmony_ci * @free_fn:   user-defined element-freeing function.
22662306a36Sopenharmony_ci * @pool_data: optional private data available to the user-defined functions.
22762306a36Sopenharmony_ci *
22862306a36Sopenharmony_ci * Like mempool_create(), but initializes the pool in (i.e. embedded in another
22962306a36Sopenharmony_ci * structure).
23062306a36Sopenharmony_ci *
23162306a36Sopenharmony_ci * Return: %0 on success, negative error code otherwise.
23262306a36Sopenharmony_ci */
23362306a36Sopenharmony_ciint mempool_init(mempool_t *pool, int min_nr, mempool_alloc_t *alloc_fn,
23462306a36Sopenharmony_ci		 mempool_free_t *free_fn, void *pool_data)
23562306a36Sopenharmony_ci{
23662306a36Sopenharmony_ci	return mempool_init_node(pool, min_nr, alloc_fn, free_fn,
23762306a36Sopenharmony_ci				 pool_data, GFP_KERNEL, NUMA_NO_NODE);
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci}
24062306a36Sopenharmony_ciEXPORT_SYMBOL(mempool_init);
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci/**
24362306a36Sopenharmony_ci * mempool_create - create a memory pool
24462306a36Sopenharmony_ci * @min_nr:    the minimum number of elements guaranteed to be
24562306a36Sopenharmony_ci *             allocated for this pool.
24662306a36Sopenharmony_ci * @alloc_fn:  user-defined element-allocation function.
24762306a36Sopenharmony_ci * @free_fn:   user-defined element-freeing function.
24862306a36Sopenharmony_ci * @pool_data: optional private data available to the user-defined functions.
24962306a36Sopenharmony_ci *
25062306a36Sopenharmony_ci * this function creates and allocates a guaranteed size, preallocated
25162306a36Sopenharmony_ci * memory pool. The pool can be used from the mempool_alloc() and mempool_free()
25262306a36Sopenharmony_ci * functions. This function might sleep. Both the alloc_fn() and the free_fn()
25362306a36Sopenharmony_ci * functions might sleep - as long as the mempool_alloc() function is not called
25462306a36Sopenharmony_ci * from IRQ contexts.
25562306a36Sopenharmony_ci *
25662306a36Sopenharmony_ci * Return: pointer to the created memory pool object or %NULL on error.
25762306a36Sopenharmony_ci */
25862306a36Sopenharmony_cimempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
25962306a36Sopenharmony_ci				mempool_free_t *free_fn, void *pool_data)
26062306a36Sopenharmony_ci{
26162306a36Sopenharmony_ci	return mempool_create_node(min_nr, alloc_fn, free_fn, pool_data,
26262306a36Sopenharmony_ci				   GFP_KERNEL, NUMA_NO_NODE);
26362306a36Sopenharmony_ci}
26462306a36Sopenharmony_ciEXPORT_SYMBOL(mempool_create);
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_cimempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
26762306a36Sopenharmony_ci			       mempool_free_t *free_fn, void *pool_data,
26862306a36Sopenharmony_ci			       gfp_t gfp_mask, int node_id)
26962306a36Sopenharmony_ci{
27062306a36Sopenharmony_ci	mempool_t *pool;
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci	pool = kzalloc_node(sizeof(*pool), gfp_mask, node_id);
27362306a36Sopenharmony_ci	if (!pool)
27462306a36Sopenharmony_ci		return NULL;
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci	if (mempool_init_node(pool, min_nr, alloc_fn, free_fn, pool_data,
27762306a36Sopenharmony_ci			      gfp_mask, node_id)) {
27862306a36Sopenharmony_ci		kfree(pool);
27962306a36Sopenharmony_ci		return NULL;
28062306a36Sopenharmony_ci	}
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci	return pool;
28362306a36Sopenharmony_ci}
28462306a36Sopenharmony_ciEXPORT_SYMBOL(mempool_create_node);
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci/**
28762306a36Sopenharmony_ci * mempool_resize - resize an existing memory pool
28862306a36Sopenharmony_ci * @pool:       pointer to the memory pool which was allocated via
28962306a36Sopenharmony_ci *              mempool_create().
29062306a36Sopenharmony_ci * @new_min_nr: the new minimum number of elements guaranteed to be
29162306a36Sopenharmony_ci *              allocated for this pool.
29262306a36Sopenharmony_ci *
29362306a36Sopenharmony_ci * This function shrinks/grows the pool. In the case of growing,
29462306a36Sopenharmony_ci * it cannot be guaranteed that the pool will be grown to the new
29562306a36Sopenharmony_ci * size immediately, but new mempool_free() calls will refill it.
29662306a36Sopenharmony_ci * This function may sleep.
29762306a36Sopenharmony_ci *
29862306a36Sopenharmony_ci * Note, the caller must guarantee that no mempool_destroy is called
29962306a36Sopenharmony_ci * while this function is running. mempool_alloc() & mempool_free()
30062306a36Sopenharmony_ci * might be called (eg. from IRQ contexts) while this function executes.
30162306a36Sopenharmony_ci *
30262306a36Sopenharmony_ci * Return: %0 on success, negative error code otherwise.
30362306a36Sopenharmony_ci */
30462306a36Sopenharmony_ciint mempool_resize(mempool_t *pool, int new_min_nr)
30562306a36Sopenharmony_ci{
30662306a36Sopenharmony_ci	void *element;
30762306a36Sopenharmony_ci	void **new_elements;
30862306a36Sopenharmony_ci	unsigned long flags;
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_ci	BUG_ON(new_min_nr <= 0);
31162306a36Sopenharmony_ci	might_sleep();
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ci	spin_lock_irqsave(&pool->lock, flags);
31462306a36Sopenharmony_ci	if (new_min_nr <= pool->min_nr) {
31562306a36Sopenharmony_ci		while (new_min_nr < pool->curr_nr) {
31662306a36Sopenharmony_ci			element = remove_element(pool);
31762306a36Sopenharmony_ci			spin_unlock_irqrestore(&pool->lock, flags);
31862306a36Sopenharmony_ci			pool->free(element, pool->pool_data);
31962306a36Sopenharmony_ci			spin_lock_irqsave(&pool->lock, flags);
32062306a36Sopenharmony_ci		}
32162306a36Sopenharmony_ci		pool->min_nr = new_min_nr;
32262306a36Sopenharmony_ci		goto out_unlock;
32362306a36Sopenharmony_ci	}
32462306a36Sopenharmony_ci	spin_unlock_irqrestore(&pool->lock, flags);
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci	/* Grow the pool */
32762306a36Sopenharmony_ci	new_elements = kmalloc_array(new_min_nr, sizeof(*new_elements),
32862306a36Sopenharmony_ci				     GFP_KERNEL);
32962306a36Sopenharmony_ci	if (!new_elements)
33062306a36Sopenharmony_ci		return -ENOMEM;
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci	spin_lock_irqsave(&pool->lock, flags);
33362306a36Sopenharmony_ci	if (unlikely(new_min_nr <= pool->min_nr)) {
33462306a36Sopenharmony_ci		/* Raced, other resize will do our work */
33562306a36Sopenharmony_ci		spin_unlock_irqrestore(&pool->lock, flags);
33662306a36Sopenharmony_ci		kfree(new_elements);
33762306a36Sopenharmony_ci		goto out;
33862306a36Sopenharmony_ci	}
33962306a36Sopenharmony_ci	memcpy(new_elements, pool->elements,
34062306a36Sopenharmony_ci			pool->curr_nr * sizeof(*new_elements));
34162306a36Sopenharmony_ci	kfree(pool->elements);
34262306a36Sopenharmony_ci	pool->elements = new_elements;
34362306a36Sopenharmony_ci	pool->min_nr = new_min_nr;
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ci	while (pool->curr_nr < pool->min_nr) {
34662306a36Sopenharmony_ci		spin_unlock_irqrestore(&pool->lock, flags);
34762306a36Sopenharmony_ci		element = pool->alloc(GFP_KERNEL, pool->pool_data);
34862306a36Sopenharmony_ci		if (!element)
34962306a36Sopenharmony_ci			goto out;
35062306a36Sopenharmony_ci		spin_lock_irqsave(&pool->lock, flags);
35162306a36Sopenharmony_ci		if (pool->curr_nr < pool->min_nr) {
35262306a36Sopenharmony_ci			add_element(pool, element);
35362306a36Sopenharmony_ci		} else {
35462306a36Sopenharmony_ci			spin_unlock_irqrestore(&pool->lock, flags);
35562306a36Sopenharmony_ci			pool->free(element, pool->pool_data);	/* Raced */
35662306a36Sopenharmony_ci			goto out;
35762306a36Sopenharmony_ci		}
35862306a36Sopenharmony_ci	}
35962306a36Sopenharmony_ciout_unlock:
36062306a36Sopenharmony_ci	spin_unlock_irqrestore(&pool->lock, flags);
36162306a36Sopenharmony_ciout:
36262306a36Sopenharmony_ci	return 0;
36362306a36Sopenharmony_ci}
36462306a36Sopenharmony_ciEXPORT_SYMBOL(mempool_resize);
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci/**
36762306a36Sopenharmony_ci * mempool_alloc - allocate an element from a specific memory pool
36862306a36Sopenharmony_ci * @pool:      pointer to the memory pool which was allocated via
36962306a36Sopenharmony_ci *             mempool_create().
37062306a36Sopenharmony_ci * @gfp_mask:  the usual allocation bitmask.
37162306a36Sopenharmony_ci *
37262306a36Sopenharmony_ci * this function only sleeps if the alloc_fn() function sleeps or
37362306a36Sopenharmony_ci * returns NULL. Note that due to preallocation, this function
37462306a36Sopenharmony_ci * *never* fails when called from process contexts. (it might
37562306a36Sopenharmony_ci * fail if called from an IRQ context.)
37662306a36Sopenharmony_ci * Note: using __GFP_ZERO is not supported.
37762306a36Sopenharmony_ci *
37862306a36Sopenharmony_ci * Return: pointer to the allocated element or %NULL on error.
37962306a36Sopenharmony_ci */
38062306a36Sopenharmony_civoid *mempool_alloc(mempool_t *pool, gfp_t gfp_mask)
38162306a36Sopenharmony_ci{
38262306a36Sopenharmony_ci	void *element;
38362306a36Sopenharmony_ci	unsigned long flags;
38462306a36Sopenharmony_ci	wait_queue_entry_t wait;
38562306a36Sopenharmony_ci	gfp_t gfp_temp;
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci	VM_WARN_ON_ONCE(gfp_mask & __GFP_ZERO);
38862306a36Sopenharmony_ci	might_alloc(gfp_mask);
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci	gfp_mask |= __GFP_NOMEMALLOC;	/* don't allocate emergency reserves */
39162306a36Sopenharmony_ci	gfp_mask |= __GFP_NORETRY;	/* don't loop in __alloc_pages */
39262306a36Sopenharmony_ci	gfp_mask |= __GFP_NOWARN;	/* failures are OK */
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci	gfp_temp = gfp_mask & ~(__GFP_DIRECT_RECLAIM|__GFP_IO);
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_cirepeat_alloc:
39762306a36Sopenharmony_ci
39862306a36Sopenharmony_ci	element = pool->alloc(gfp_temp, pool->pool_data);
39962306a36Sopenharmony_ci	if (likely(element != NULL))
40062306a36Sopenharmony_ci		return element;
40162306a36Sopenharmony_ci
40262306a36Sopenharmony_ci	spin_lock_irqsave(&pool->lock, flags);
40362306a36Sopenharmony_ci	if (likely(pool->curr_nr)) {
40462306a36Sopenharmony_ci		element = remove_element(pool);
40562306a36Sopenharmony_ci		spin_unlock_irqrestore(&pool->lock, flags);
40662306a36Sopenharmony_ci		/* paired with rmb in mempool_free(), read comment there */
40762306a36Sopenharmony_ci		smp_wmb();
40862306a36Sopenharmony_ci		/*
40962306a36Sopenharmony_ci		 * Update the allocation stack trace as this is more useful
41062306a36Sopenharmony_ci		 * for debugging.
41162306a36Sopenharmony_ci		 */
41262306a36Sopenharmony_ci		kmemleak_update_trace(element);
41362306a36Sopenharmony_ci		return element;
41462306a36Sopenharmony_ci	}
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ci	/*
41762306a36Sopenharmony_ci	 * We use gfp mask w/o direct reclaim or IO for the first round.  If
41862306a36Sopenharmony_ci	 * alloc failed with that and @pool was empty, retry immediately.
41962306a36Sopenharmony_ci	 */
42062306a36Sopenharmony_ci	if (gfp_temp != gfp_mask) {
42162306a36Sopenharmony_ci		spin_unlock_irqrestore(&pool->lock, flags);
42262306a36Sopenharmony_ci		gfp_temp = gfp_mask;
42362306a36Sopenharmony_ci		goto repeat_alloc;
42462306a36Sopenharmony_ci	}
42562306a36Sopenharmony_ci
42662306a36Sopenharmony_ci	/* We must not sleep if !__GFP_DIRECT_RECLAIM */
42762306a36Sopenharmony_ci	if (!(gfp_mask & __GFP_DIRECT_RECLAIM)) {
42862306a36Sopenharmony_ci		spin_unlock_irqrestore(&pool->lock, flags);
42962306a36Sopenharmony_ci		return NULL;
43062306a36Sopenharmony_ci	}
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_ci	/* Let's wait for someone else to return an element to @pool */
43362306a36Sopenharmony_ci	init_wait(&wait);
43462306a36Sopenharmony_ci	prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
43562306a36Sopenharmony_ci
43662306a36Sopenharmony_ci	spin_unlock_irqrestore(&pool->lock, flags);
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci	/*
43962306a36Sopenharmony_ci	 * FIXME: this should be io_schedule().  The timeout is there as a
44062306a36Sopenharmony_ci	 * workaround for some DM problems in 2.6.18.
44162306a36Sopenharmony_ci	 */
44262306a36Sopenharmony_ci	io_schedule_timeout(5*HZ);
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_ci	finish_wait(&pool->wait, &wait);
44562306a36Sopenharmony_ci	goto repeat_alloc;
44662306a36Sopenharmony_ci}
44762306a36Sopenharmony_ciEXPORT_SYMBOL(mempool_alloc);
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ci/**
45062306a36Sopenharmony_ci * mempool_free - return an element to the pool.
45162306a36Sopenharmony_ci * @element:   pool element pointer.
45262306a36Sopenharmony_ci * @pool:      pointer to the memory pool which was allocated via
45362306a36Sopenharmony_ci *             mempool_create().
45462306a36Sopenharmony_ci *
45562306a36Sopenharmony_ci * this function only sleeps if the free_fn() function sleeps.
45662306a36Sopenharmony_ci */
45762306a36Sopenharmony_civoid mempool_free(void *element, mempool_t *pool)
45862306a36Sopenharmony_ci{
45962306a36Sopenharmony_ci	unsigned long flags;
46062306a36Sopenharmony_ci
46162306a36Sopenharmony_ci	if (unlikely(element == NULL))
46262306a36Sopenharmony_ci		return;
46362306a36Sopenharmony_ci
46462306a36Sopenharmony_ci	/*
46562306a36Sopenharmony_ci	 * Paired with the wmb in mempool_alloc().  The preceding read is
46662306a36Sopenharmony_ci	 * for @element and the following @pool->curr_nr.  This ensures
46762306a36Sopenharmony_ci	 * that the visible value of @pool->curr_nr is from after the
46862306a36Sopenharmony_ci	 * allocation of @element.  This is necessary for fringe cases
46962306a36Sopenharmony_ci	 * where @element was passed to this task without going through
47062306a36Sopenharmony_ci	 * barriers.
47162306a36Sopenharmony_ci	 *
47262306a36Sopenharmony_ci	 * For example, assume @p is %NULL at the beginning and one task
47362306a36Sopenharmony_ci	 * performs "p = mempool_alloc(...);" while another task is doing
47462306a36Sopenharmony_ci	 * "while (!p) cpu_relax(); mempool_free(p, ...);".  This function
47562306a36Sopenharmony_ci	 * may end up using curr_nr value which is from before allocation
47662306a36Sopenharmony_ci	 * of @p without the following rmb.
47762306a36Sopenharmony_ci	 */
47862306a36Sopenharmony_ci	smp_rmb();
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ci	/*
48162306a36Sopenharmony_ci	 * For correctness, we need a test which is guaranteed to trigger
48262306a36Sopenharmony_ci	 * if curr_nr + #allocated == min_nr.  Testing curr_nr < min_nr
48362306a36Sopenharmony_ci	 * without locking achieves that and refilling as soon as possible
48462306a36Sopenharmony_ci	 * is desirable.
48562306a36Sopenharmony_ci	 *
48662306a36Sopenharmony_ci	 * Because curr_nr visible here is always a value after the
48762306a36Sopenharmony_ci	 * allocation of @element, any task which decremented curr_nr below
48862306a36Sopenharmony_ci	 * min_nr is guaranteed to see curr_nr < min_nr unless curr_nr gets
48962306a36Sopenharmony_ci	 * incremented to min_nr afterwards.  If curr_nr gets incremented
49062306a36Sopenharmony_ci	 * to min_nr after the allocation of @element, the elements
49162306a36Sopenharmony_ci	 * allocated after that are subject to the same guarantee.
49262306a36Sopenharmony_ci	 *
49362306a36Sopenharmony_ci	 * Waiters happen iff curr_nr is 0 and the above guarantee also
49462306a36Sopenharmony_ci	 * ensures that there will be frees which return elements to the
49562306a36Sopenharmony_ci	 * pool waking up the waiters.
49662306a36Sopenharmony_ci	 */
49762306a36Sopenharmony_ci	if (unlikely(READ_ONCE(pool->curr_nr) < pool->min_nr)) {
49862306a36Sopenharmony_ci		spin_lock_irqsave(&pool->lock, flags);
49962306a36Sopenharmony_ci		if (likely(pool->curr_nr < pool->min_nr)) {
50062306a36Sopenharmony_ci			add_element(pool, element);
50162306a36Sopenharmony_ci			spin_unlock_irqrestore(&pool->lock, flags);
50262306a36Sopenharmony_ci			wake_up(&pool->wait);
50362306a36Sopenharmony_ci			return;
50462306a36Sopenharmony_ci		}
50562306a36Sopenharmony_ci		spin_unlock_irqrestore(&pool->lock, flags);
50662306a36Sopenharmony_ci	}
50762306a36Sopenharmony_ci	pool->free(element, pool->pool_data);
50862306a36Sopenharmony_ci}
50962306a36Sopenharmony_ciEXPORT_SYMBOL(mempool_free);
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_ci/*
51262306a36Sopenharmony_ci * A commonly used alloc and free fn.
51362306a36Sopenharmony_ci */
51462306a36Sopenharmony_civoid *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data)
51562306a36Sopenharmony_ci{
51662306a36Sopenharmony_ci	struct kmem_cache *mem = pool_data;
51762306a36Sopenharmony_ci	VM_BUG_ON(mem->ctor);
51862306a36Sopenharmony_ci	return kmem_cache_alloc(mem, gfp_mask);
51962306a36Sopenharmony_ci}
52062306a36Sopenharmony_ciEXPORT_SYMBOL(mempool_alloc_slab);
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_civoid mempool_free_slab(void *element, void *pool_data)
52362306a36Sopenharmony_ci{
52462306a36Sopenharmony_ci	struct kmem_cache *mem = pool_data;
52562306a36Sopenharmony_ci	kmem_cache_free(mem, element);
52662306a36Sopenharmony_ci}
52762306a36Sopenharmony_ciEXPORT_SYMBOL(mempool_free_slab);
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ci/*
53062306a36Sopenharmony_ci * A commonly used alloc and free fn that kmalloc/kfrees the amount of memory
53162306a36Sopenharmony_ci * specified by pool_data
53262306a36Sopenharmony_ci */
53362306a36Sopenharmony_civoid *mempool_kmalloc(gfp_t gfp_mask, void *pool_data)
53462306a36Sopenharmony_ci{
53562306a36Sopenharmony_ci	size_t size = (size_t)pool_data;
53662306a36Sopenharmony_ci	return kmalloc(size, gfp_mask);
53762306a36Sopenharmony_ci}
53862306a36Sopenharmony_ciEXPORT_SYMBOL(mempool_kmalloc);
53962306a36Sopenharmony_ci
54062306a36Sopenharmony_civoid mempool_kfree(void *element, void *pool_data)
54162306a36Sopenharmony_ci{
54262306a36Sopenharmony_ci	kfree(element);
54362306a36Sopenharmony_ci}
54462306a36Sopenharmony_ciEXPORT_SYMBOL(mempool_kfree);
54562306a36Sopenharmony_ci
54662306a36Sopenharmony_ci/*
54762306a36Sopenharmony_ci * A simple mempool-backed page allocator that allocates pages
54862306a36Sopenharmony_ci * of the order specified by pool_data.
54962306a36Sopenharmony_ci */
55062306a36Sopenharmony_civoid *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data)
55162306a36Sopenharmony_ci{
55262306a36Sopenharmony_ci	int order = (int)(long)pool_data;
55362306a36Sopenharmony_ci	return alloc_pages(gfp_mask, order);
55462306a36Sopenharmony_ci}
55562306a36Sopenharmony_ciEXPORT_SYMBOL(mempool_alloc_pages);
55662306a36Sopenharmony_ci
55762306a36Sopenharmony_civoid mempool_free_pages(void *element, void *pool_data)
55862306a36Sopenharmony_ci{
55962306a36Sopenharmony_ci	int order = (int)(long)pool_data;
56062306a36Sopenharmony_ci	__free_pages(element, order);
56162306a36Sopenharmony_ci}
56262306a36Sopenharmony_ciEXPORT_SYMBOL(mempool_free_pages);
563