xref: /openbmc/linux/mm/mempool.c (revision b2b23ba0)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/mm/mempool.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  memory buffer pool support. Such pools are mostly used
61da177e4SLinus Torvalds  *  for guaranteed, deadlock-free memory allocations during
71da177e4SLinus Torvalds  *  extreme VM load.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *  started by Ingo Molnar, Copyright (C) 2001
10bdfedb76SDavid Rientjes  *  debugging by David Rientjes, Copyright (C) 2015
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/mm.h>
141da177e4SLinus Torvalds #include <linux/slab.h>
15bdfedb76SDavid Rientjes #include <linux/highmem.h>
1692393615SAndrey Ryabinin #include <linux/kasan.h>
1717411962SCatalin Marinas #include <linux/kmemleak.h>
18b95f1b31SPaul Gortmaker #include <linux/export.h>
191da177e4SLinus Torvalds #include <linux/mempool.h>
201da177e4SLinus Torvalds #include <linux/writeback.h>
21e244c9e6SDavid Rientjes #include "slab.h"
221da177e4SLinus Torvalds 
23bdfedb76SDavid Rientjes #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB_DEBUG_ON)
poison_error(mempool_t * pool,void * element,size_t size,size_t byte)24bdfedb76SDavid Rientjes static void poison_error(mempool_t *pool, void *element, size_t size,
25bdfedb76SDavid Rientjes 			 size_t byte)
26bdfedb76SDavid Rientjes {
27bdfedb76SDavid Rientjes 	const int nr = pool->curr_nr;
28bdfedb76SDavid Rientjes 	const int start = max_t(int, byte - (BITS_PER_LONG / 8), 0);
29bdfedb76SDavid Rientjes 	const int end = min_t(int, byte + (BITS_PER_LONG / 8), size);
30bdfedb76SDavid Rientjes 	int i;
31bdfedb76SDavid Rientjes 
32bdfedb76SDavid Rientjes 	pr_err("BUG: mempool element poison mismatch\n");
33bdfedb76SDavid Rientjes 	pr_err("Mempool %p size %zu\n", pool, size);
34bdfedb76SDavid Rientjes 	pr_err(" nr=%d @ %p: %s0x", nr, element, start > 0 ? "... " : "");
35bdfedb76SDavid Rientjes 	for (i = start; i < end; i++)
36bdfedb76SDavid Rientjes 		pr_cont("%x ", *(u8 *)(element + i));
37bdfedb76SDavid Rientjes 	pr_cont("%s\n", end < size ? "..." : "");
38bdfedb76SDavid Rientjes 	dump_stack();
39bdfedb76SDavid Rientjes }
40bdfedb76SDavid Rientjes 
__check_element(mempool_t * pool,void * element,size_t size)41bdfedb76SDavid Rientjes static void __check_element(mempool_t *pool, void *element, size_t size)
42bdfedb76SDavid Rientjes {
43bdfedb76SDavid Rientjes 	u8 *obj = element;
44bdfedb76SDavid Rientjes 	size_t i;
45bdfedb76SDavid Rientjes 
46bdfedb76SDavid Rientjes 	for (i = 0; i < size; i++) {
47bdfedb76SDavid Rientjes 		u8 exp = (i < size - 1) ? POISON_FREE : POISON_END;
48bdfedb76SDavid Rientjes 
49bdfedb76SDavid Rientjes 		if (obj[i] != exp) {
50bdfedb76SDavid Rientjes 			poison_error(pool, element, size, i);
51bdfedb76SDavid Rientjes 			return;
52bdfedb76SDavid Rientjes 		}
53bdfedb76SDavid Rientjes 	}
54bdfedb76SDavid Rientjes 	memset(obj, POISON_INUSE, size);
55bdfedb76SDavid Rientjes }
56bdfedb76SDavid Rientjes 
check_element(mempool_t * pool,void * element)57bdfedb76SDavid Rientjes static void check_element(mempool_t *pool, void *element)
58bdfedb76SDavid Rientjes {
59bdfedb76SDavid Rientjes 	/* Mempools backed by slab allocator */
60*b2b23ba0SKees Cook 	if (pool->free == mempool_kfree) {
61*b2b23ba0SKees Cook 		__check_element(pool, element, (size_t)pool->pool_data);
62*b2b23ba0SKees Cook 	} else if (pool->free == mempool_free_slab) {
63*b2b23ba0SKees Cook 		__check_element(pool, element, kmem_cache_size(pool->pool_data));
64544941d7SMiaohe Lin 	} else if (pool->free == mempool_free_pages) {
65bdfedb76SDavid Rientjes 		/* Mempools backed by page allocator */
66bdfedb76SDavid Rientjes 		int order = (int)(long)pool->pool_data;
67bdfedb76SDavid Rientjes 		void *addr = kmap_atomic((struct page *)element);
68bdfedb76SDavid Rientjes 
69bdfedb76SDavid Rientjes 		__check_element(pool, addr, 1UL << (PAGE_SHIFT + order));
70bdfedb76SDavid Rientjes 		kunmap_atomic(addr);
71bdfedb76SDavid Rientjes 	}
72bdfedb76SDavid Rientjes }
73bdfedb76SDavid Rientjes 
__poison_element(void * element,size_t size)74bdfedb76SDavid Rientjes static void __poison_element(void *element, size_t size)
75bdfedb76SDavid Rientjes {
76bdfedb76SDavid Rientjes 	u8 *obj = element;
77bdfedb76SDavid Rientjes 
78bdfedb76SDavid Rientjes 	memset(obj, POISON_FREE, size - 1);
79bdfedb76SDavid Rientjes 	obj[size - 1] = POISON_END;
80bdfedb76SDavid Rientjes }
81bdfedb76SDavid Rientjes 
poison_element(mempool_t * pool,void * element)82bdfedb76SDavid Rientjes static void poison_element(mempool_t *pool, void *element)
83bdfedb76SDavid Rientjes {
84bdfedb76SDavid Rientjes 	/* Mempools backed by slab allocator */
85*b2b23ba0SKees Cook 	if (pool->alloc == mempool_kmalloc) {
86*b2b23ba0SKees Cook 		__poison_element(element, (size_t)pool->pool_data);
87*b2b23ba0SKees Cook 	} else if (pool->alloc == mempool_alloc_slab) {
88*b2b23ba0SKees Cook 		__poison_element(element, kmem_cache_size(pool->pool_data));
89544941d7SMiaohe Lin 	} else if (pool->alloc == mempool_alloc_pages) {
90bdfedb76SDavid Rientjes 		/* Mempools backed by page allocator */
91bdfedb76SDavid Rientjes 		int order = (int)(long)pool->pool_data;
92bdfedb76SDavid Rientjes 		void *addr = kmap_atomic((struct page *)element);
93bdfedb76SDavid Rientjes 
94bdfedb76SDavid Rientjes 		__poison_element(addr, 1UL << (PAGE_SHIFT + order));
95bdfedb76SDavid Rientjes 		kunmap_atomic(addr);
96bdfedb76SDavid Rientjes 	}
97bdfedb76SDavid Rientjes }
98bdfedb76SDavid Rientjes #else /* CONFIG_DEBUG_SLAB || CONFIG_SLUB_DEBUG_ON */
check_element(mempool_t * pool,void * element)99bdfedb76SDavid Rientjes static inline void check_element(mempool_t *pool, void *element)
100bdfedb76SDavid Rientjes {
101bdfedb76SDavid Rientjes }
poison_element(mempool_t * pool,void * element)102bdfedb76SDavid Rientjes static inline void poison_element(mempool_t *pool, void *element)
103bdfedb76SDavid Rientjes {
104bdfedb76SDavid Rientjes }
105bdfedb76SDavid Rientjes #endif /* CONFIG_DEBUG_SLAB || CONFIG_SLUB_DEBUG_ON */
106bdfedb76SDavid Rientjes 
kasan_poison_element(mempool_t * pool,void * element)1076860f634SDmitry Vyukov static __always_inline void kasan_poison_element(mempool_t *pool, void *element)
10892393615SAndrey Ryabinin {
1099b75a867SAndrey Ryabinin 	if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
110027b37b5SAndrey Konovalov 		kasan_slab_free_mempool(element);
111544941d7SMiaohe Lin 	else if (pool->alloc == mempool_alloc_pages)
1127a3b8353SPeter Collingbourne 		kasan_poison_pages(element, (unsigned long)pool->pool_data,
1137a3b8353SPeter Collingbourne 				   false);
11492393615SAndrey Ryabinin }
11592393615SAndrey Ryabinin 
kasan_unpoison_element(mempool_t * pool,void * element)1168cded866SJia-Ju Bai static void kasan_unpoison_element(mempool_t *pool, void *element)
11792393615SAndrey Ryabinin {
118*b2b23ba0SKees Cook 	if (pool->alloc == mempool_kmalloc)
119*b2b23ba0SKees Cook 		kasan_unpoison_range(element, (size_t)pool->pool_data);
120*b2b23ba0SKees Cook 	else if (pool->alloc == mempool_alloc_slab)
121*b2b23ba0SKees Cook 		kasan_unpoison_range(element, kmem_cache_size(pool->pool_data));
122544941d7SMiaohe Lin 	else if (pool->alloc == mempool_alloc_pages)
1237a3b8353SPeter Collingbourne 		kasan_unpoison_pages(element, (unsigned long)pool->pool_data,
1247a3b8353SPeter Collingbourne 				     false);
12592393615SAndrey Ryabinin }
12692393615SAndrey Ryabinin 
add_element(mempool_t * pool,void * element)1276860f634SDmitry Vyukov static __always_inline void add_element(mempool_t *pool, void *element)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	BUG_ON(pool->curr_nr >= pool->min_nr);
130bdfedb76SDavid Rientjes 	poison_element(pool, element);
13192393615SAndrey Ryabinin 	kasan_poison_element(pool, element);
1321da177e4SLinus Torvalds 	pool->elements[pool->curr_nr++] = element;
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
remove_element(mempool_t * pool)1358cded866SJia-Ju Bai static void *remove_element(mempool_t *pool)
1361da177e4SLinus Torvalds {
137bdfedb76SDavid Rientjes 	void *element = pool->elements[--pool->curr_nr];
138bdfedb76SDavid Rientjes 
139bdfedb76SDavid Rientjes 	BUG_ON(pool->curr_nr < 0);
1408cded866SJia-Ju Bai 	kasan_unpoison_element(pool, element);
14176401310SMatthew Dawson 	check_element(pool, element);
142bdfedb76SDavid Rientjes 	return element;
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
1450565d317STejun Heo /**
146c1a67fefSKent Overstreet  * mempool_exit - exit a mempool initialized with mempool_init()
147c1a67fefSKent Overstreet  * @pool:      pointer to the memory pool which was initialized with
148c1a67fefSKent Overstreet  *             mempool_init().
149c1a67fefSKent Overstreet  *
150c1a67fefSKent Overstreet  * Free all reserved elements in @pool and @pool itself.  This function
151c1a67fefSKent Overstreet  * only sleeps if the free_fn() function sleeps.
152c1a67fefSKent Overstreet  *
153c1a67fefSKent Overstreet  * May be called on a zeroed but uninitialized mempool (i.e. allocated with
154c1a67fefSKent Overstreet  * kzalloc()).
155c1a67fefSKent Overstreet  */
mempool_exit(mempool_t * pool)156c1a67fefSKent Overstreet void mempool_exit(mempool_t *pool)
157c1a67fefSKent Overstreet {
158c1a67fefSKent Overstreet 	while (pool->curr_nr) {
1598cded866SJia-Ju Bai 		void *element = remove_element(pool);
160c1a67fefSKent Overstreet 		pool->free(element, pool->pool_data);
161c1a67fefSKent Overstreet 	}
162c1a67fefSKent Overstreet 	kfree(pool->elements);
163c1a67fefSKent Overstreet 	pool->elements = NULL;
164c1a67fefSKent Overstreet }
165c1a67fefSKent Overstreet EXPORT_SYMBOL(mempool_exit);
166c1a67fefSKent Overstreet 
167c1a67fefSKent Overstreet /**
1680565d317STejun Heo  * mempool_destroy - deallocate a memory pool
1690565d317STejun Heo  * @pool:      pointer to the memory pool which was allocated via
1700565d317STejun Heo  *             mempool_create().
1710565d317STejun Heo  *
1720565d317STejun Heo  * Free all reserved elements in @pool and @pool itself.  This function
1730565d317STejun Heo  * only sleeps if the free_fn() function sleeps.
1740565d317STejun Heo  */
mempool_destroy(mempool_t * pool)1750565d317STejun Heo void mempool_destroy(mempool_t *pool)
1761da177e4SLinus Torvalds {
1774e3ca3e0SSergey Senozhatsky 	if (unlikely(!pool))
1784e3ca3e0SSergey Senozhatsky 		return;
1794e3ca3e0SSergey Senozhatsky 
180c1a67fefSKent Overstreet 	mempool_exit(pool);
1811da177e4SLinus Torvalds 	kfree(pool);
1821da177e4SLinus Torvalds }
1830565d317STejun Heo EXPORT_SYMBOL(mempool_destroy);
1841da177e4SLinus Torvalds 
mempool_init_node(mempool_t * pool,int min_nr,mempool_alloc_t * alloc_fn,mempool_free_t * free_fn,void * pool_data,gfp_t gfp_mask,int node_id)185c1a67fefSKent Overstreet int mempool_init_node(mempool_t *pool, int min_nr, mempool_alloc_t *alloc_fn,
186c1a67fefSKent Overstreet 		      mempool_free_t *free_fn, void *pool_data,
187c1a67fefSKent Overstreet 		      gfp_t gfp_mask, int node_id)
188c1a67fefSKent Overstreet {
189c1a67fefSKent Overstreet 	spin_lock_init(&pool->lock);
190c1a67fefSKent Overstreet 	pool->min_nr	= min_nr;
191c1a67fefSKent Overstreet 	pool->pool_data = pool_data;
192c1a67fefSKent Overstreet 	pool->alloc	= alloc_fn;
193c1a67fefSKent Overstreet 	pool->free	= free_fn;
194c1a67fefSKent Overstreet 	init_waitqueue_head(&pool->wait);
195c1a67fefSKent Overstreet 
196c1a67fefSKent Overstreet 	pool->elements = kmalloc_array_node(min_nr, sizeof(void *),
197c1a67fefSKent Overstreet 					    gfp_mask, node_id);
198c1a67fefSKent Overstreet 	if (!pool->elements)
199c1a67fefSKent Overstreet 		return -ENOMEM;
200c1a67fefSKent Overstreet 
201c1a67fefSKent Overstreet 	/*
202c1a67fefSKent Overstreet 	 * First pre-allocate the guaranteed number of buffers.
203c1a67fefSKent Overstreet 	 */
204c1a67fefSKent Overstreet 	while (pool->curr_nr < pool->min_nr) {
205c1a67fefSKent Overstreet 		void *element;
206c1a67fefSKent Overstreet 
207c1a67fefSKent Overstreet 		element = pool->alloc(gfp_mask, pool->pool_data);
208c1a67fefSKent Overstreet 		if (unlikely(!element)) {
209c1a67fefSKent Overstreet 			mempool_exit(pool);
210c1a67fefSKent Overstreet 			return -ENOMEM;
211c1a67fefSKent Overstreet 		}
212c1a67fefSKent Overstreet 		add_element(pool, element);
213c1a67fefSKent Overstreet 	}
214c1a67fefSKent Overstreet 
215c1a67fefSKent Overstreet 	return 0;
216c1a67fefSKent Overstreet }
217c1a67fefSKent Overstreet EXPORT_SYMBOL(mempool_init_node);
218c1a67fefSKent Overstreet 
219c1a67fefSKent Overstreet /**
220c1a67fefSKent Overstreet  * mempool_init - initialize a memory pool
221a3bf6ce3SMike Rapoport  * @pool:      pointer to the memory pool that should be initialized
222c1a67fefSKent Overstreet  * @min_nr:    the minimum number of elements guaranteed to be
223c1a67fefSKent Overstreet  *             allocated for this pool.
224c1a67fefSKent Overstreet  * @alloc_fn:  user-defined element-allocation function.
225c1a67fefSKent Overstreet  * @free_fn:   user-defined element-freeing function.
226c1a67fefSKent Overstreet  * @pool_data: optional private data available to the user-defined functions.
227c1a67fefSKent Overstreet  *
228c1a67fefSKent Overstreet  * Like mempool_create(), but initializes the pool in (i.e. embedded in another
229c1a67fefSKent Overstreet  * structure).
230a862f68aSMike Rapoport  *
231a862f68aSMike Rapoport  * Return: %0 on success, negative error code otherwise.
232c1a67fefSKent Overstreet  */
mempool_init(mempool_t * pool,int min_nr,mempool_alloc_t * alloc_fn,mempool_free_t * free_fn,void * pool_data)233c1a67fefSKent Overstreet int mempool_init(mempool_t *pool, int min_nr, mempool_alloc_t *alloc_fn,
234c1a67fefSKent Overstreet 		 mempool_free_t *free_fn, void *pool_data)
235c1a67fefSKent Overstreet {
236c1a67fefSKent Overstreet 	return mempool_init_node(pool, min_nr, alloc_fn, free_fn,
237c1a67fefSKent Overstreet 				 pool_data, GFP_KERNEL, NUMA_NO_NODE);
238c1a67fefSKent Overstreet 
239c1a67fefSKent Overstreet }
240c1a67fefSKent Overstreet EXPORT_SYMBOL(mempool_init);
241c1a67fefSKent Overstreet 
2421da177e4SLinus Torvalds /**
2431da177e4SLinus Torvalds  * mempool_create - create a memory pool
2441da177e4SLinus Torvalds  * @min_nr:    the minimum number of elements guaranteed to be
2451da177e4SLinus Torvalds  *             allocated for this pool.
2461da177e4SLinus Torvalds  * @alloc_fn:  user-defined element-allocation function.
2471da177e4SLinus Torvalds  * @free_fn:   user-defined element-freeing function.
2481da177e4SLinus Torvalds  * @pool_data: optional private data available to the user-defined functions.
2491da177e4SLinus Torvalds  *
2501da177e4SLinus Torvalds  * this function creates and allocates a guaranteed size, preallocated
25172fd4a35SRobert P. J. Day  * memory pool. The pool can be used from the mempool_alloc() and mempool_free()
2521da177e4SLinus Torvalds  * functions. This function might sleep. Both the alloc_fn() and the free_fn()
25372fd4a35SRobert P. J. Day  * functions might sleep - as long as the mempool_alloc() function is not called
2541da177e4SLinus Torvalds  * from IRQ contexts.
255a862f68aSMike Rapoport  *
256a862f68aSMike Rapoport  * Return: pointer to the created memory pool object or %NULL on error.
2571da177e4SLinus Torvalds  */
mempool_create(int min_nr,mempool_alloc_t * alloc_fn,mempool_free_t * free_fn,void * pool_data)2581da177e4SLinus Torvalds mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
2591da177e4SLinus Torvalds 				mempool_free_t *free_fn, void *pool_data)
2601da177e4SLinus Torvalds {
261a91a5ac6STejun Heo 	return mempool_create_node(min_nr, alloc_fn, free_fn, pool_data,
262a91a5ac6STejun Heo 				   GFP_KERNEL, NUMA_NO_NODE);
2631946089aSChristoph Lameter }
2641946089aSChristoph Lameter EXPORT_SYMBOL(mempool_create);
2651da177e4SLinus Torvalds 
mempool_create_node(int min_nr,mempool_alloc_t * alloc_fn,mempool_free_t * free_fn,void * pool_data,gfp_t gfp_mask,int node_id)2661946089aSChristoph Lameter mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
267a91a5ac6STejun Heo 			       mempool_free_t *free_fn, void *pool_data,
268a91a5ac6STejun Heo 			       gfp_t gfp_mask, int node_id)
2691946089aSChristoph Lameter {
2701946089aSChristoph Lameter 	mempool_t *pool;
271c1a67fefSKent Overstreet 
2727b5219dbSJoe Perches 	pool = kzalloc_node(sizeof(*pool), gfp_mask, node_id);
2731da177e4SLinus Torvalds 	if (!pool)
2741da177e4SLinus Torvalds 		return NULL;
275c1a67fefSKent Overstreet 
276c1a67fefSKent Overstreet 	if (mempool_init_node(pool, min_nr, alloc_fn, free_fn, pool_data,
277c1a67fefSKent Overstreet 			      gfp_mask, node_id)) {
2781da177e4SLinus Torvalds 		kfree(pool);
2791da177e4SLinus Torvalds 		return NULL;
2801da177e4SLinus Torvalds 	}
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	return pool;
2831da177e4SLinus Torvalds }
2841946089aSChristoph Lameter EXPORT_SYMBOL(mempool_create_node);
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds /**
2871da177e4SLinus Torvalds  * mempool_resize - resize an existing memory pool
2881da177e4SLinus Torvalds  * @pool:       pointer to the memory pool which was allocated via
2891da177e4SLinus Torvalds  *              mempool_create().
2901da177e4SLinus Torvalds  * @new_min_nr: the new minimum number of elements guaranteed to be
2911da177e4SLinus Torvalds  *              allocated for this pool.
2921da177e4SLinus Torvalds  *
2931da177e4SLinus Torvalds  * This function shrinks/grows the pool. In the case of growing,
2941da177e4SLinus Torvalds  * it cannot be guaranteed that the pool will be grown to the new
2951da177e4SLinus Torvalds  * size immediately, but new mempool_free() calls will refill it.
29611d83360SDavid Rientjes  * This function may sleep.
2971da177e4SLinus Torvalds  *
2981da177e4SLinus Torvalds  * Note, the caller must guarantee that no mempool_destroy is called
2991da177e4SLinus Torvalds  * while this function is running. mempool_alloc() & mempool_free()
3001da177e4SLinus Torvalds  * might be called (eg. from IRQ contexts) while this function executes.
301a862f68aSMike Rapoport  *
302a862f68aSMike Rapoport  * Return: %0 on success, negative error code otherwise.
3031da177e4SLinus Torvalds  */
mempool_resize(mempool_t * pool,int new_min_nr)30411d83360SDavid Rientjes int mempool_resize(mempool_t *pool, int new_min_nr)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds 	void *element;
3071da177e4SLinus Torvalds 	void **new_elements;
3081da177e4SLinus Torvalds 	unsigned long flags;
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	BUG_ON(new_min_nr <= 0);
31111d83360SDavid Rientjes 	might_sleep();
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	spin_lock_irqsave(&pool->lock, flags);
3141da177e4SLinus Torvalds 	if (new_min_nr <= pool->min_nr) {
3151da177e4SLinus Torvalds 		while (new_min_nr < pool->curr_nr) {
3168cded866SJia-Ju Bai 			element = remove_element(pool);
3171da177e4SLinus Torvalds 			spin_unlock_irqrestore(&pool->lock, flags);
3181da177e4SLinus Torvalds 			pool->free(element, pool->pool_data);
3191da177e4SLinus Torvalds 			spin_lock_irqsave(&pool->lock, flags);
3201da177e4SLinus Torvalds 		}
3211da177e4SLinus Torvalds 		pool->min_nr = new_min_nr;
3221da177e4SLinus Torvalds 		goto out_unlock;
3231da177e4SLinus Torvalds 	}
3241da177e4SLinus Torvalds 	spin_unlock_irqrestore(&pool->lock, flags);
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	/* Grow the pool */
32711d83360SDavid Rientjes 	new_elements = kmalloc_array(new_min_nr, sizeof(*new_elements),
32811d83360SDavid Rientjes 				     GFP_KERNEL);
3291da177e4SLinus Torvalds 	if (!new_elements)
3301da177e4SLinus Torvalds 		return -ENOMEM;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	spin_lock_irqsave(&pool->lock, flags);
3331da177e4SLinus Torvalds 	if (unlikely(new_min_nr <= pool->min_nr)) {
3341da177e4SLinus Torvalds 		/* Raced, other resize will do our work */
3351da177e4SLinus Torvalds 		spin_unlock_irqrestore(&pool->lock, flags);
3361da177e4SLinus Torvalds 		kfree(new_elements);
3371da177e4SLinus Torvalds 		goto out;
3381da177e4SLinus Torvalds 	}
3391da177e4SLinus Torvalds 	memcpy(new_elements, pool->elements,
3401da177e4SLinus Torvalds 			pool->curr_nr * sizeof(*new_elements));
3411da177e4SLinus Torvalds 	kfree(pool->elements);
3421da177e4SLinus Torvalds 	pool->elements = new_elements;
3431da177e4SLinus Torvalds 	pool->min_nr = new_min_nr;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	while (pool->curr_nr < pool->min_nr) {
3461da177e4SLinus Torvalds 		spin_unlock_irqrestore(&pool->lock, flags);
34711d83360SDavid Rientjes 		element = pool->alloc(GFP_KERNEL, pool->pool_data);
3481da177e4SLinus Torvalds 		if (!element)
3491da177e4SLinus Torvalds 			goto out;
3501da177e4SLinus Torvalds 		spin_lock_irqsave(&pool->lock, flags);
3511da177e4SLinus Torvalds 		if (pool->curr_nr < pool->min_nr) {
3521da177e4SLinus Torvalds 			add_element(pool, element);
3531da177e4SLinus Torvalds 		} else {
3541da177e4SLinus Torvalds 			spin_unlock_irqrestore(&pool->lock, flags);
3551da177e4SLinus Torvalds 			pool->free(element, pool->pool_data);	/* Raced */
3561da177e4SLinus Torvalds 			goto out;
3571da177e4SLinus Torvalds 		}
3581da177e4SLinus Torvalds 	}
3591da177e4SLinus Torvalds out_unlock:
3601da177e4SLinus Torvalds 	spin_unlock_irqrestore(&pool->lock, flags);
3611da177e4SLinus Torvalds out:
3621da177e4SLinus Torvalds 	return 0;
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds EXPORT_SYMBOL(mempool_resize);
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds /**
3671da177e4SLinus Torvalds  * mempool_alloc - allocate an element from a specific memory pool
3681da177e4SLinus Torvalds  * @pool:      pointer to the memory pool which was allocated via
3691da177e4SLinus Torvalds  *             mempool_create().
3701da177e4SLinus Torvalds  * @gfp_mask:  the usual allocation bitmask.
3711da177e4SLinus Torvalds  *
37272fd4a35SRobert P. J. Day  * this function only sleeps if the alloc_fn() function sleeps or
3731da177e4SLinus Torvalds  * returns NULL. Note that due to preallocation, this function
3741da177e4SLinus Torvalds  * *never* fails when called from process contexts. (it might
3751da177e4SLinus Torvalds  * fail if called from an IRQ context.)
3764e390b2bSMichal Hocko  * Note: using __GFP_ZERO is not supported.
377a862f68aSMike Rapoport  *
378a862f68aSMike Rapoport  * Return: pointer to the allocated element or %NULL on error.
3791da177e4SLinus Torvalds  */
mempool_alloc(mempool_t * pool,gfp_t gfp_mask)380dd0fc66fSAl Viro void *mempool_alloc(mempool_t *pool, gfp_t gfp_mask)
3811da177e4SLinus Torvalds {
3821da177e4SLinus Torvalds 	void *element;
3831da177e4SLinus Torvalds 	unsigned long flags;
384ac6424b9SIngo Molnar 	wait_queue_entry_t wait;
3856daa0e28SAl Viro 	gfp_t gfp_temp;
38620a77776SNick Piggin 
3878bf8fcb0SSebastian Ott 	VM_WARN_ON_ONCE(gfp_mask & __GFP_ZERO);
38821bfe8dbSDaniel Vetter 	might_alloc(gfp_mask);
389b84a35beSNick Piggin 
3904e390b2bSMichal Hocko 	gfp_mask |= __GFP_NOMEMALLOC;	/* don't allocate emergency reserves */
391b84a35beSNick Piggin 	gfp_mask |= __GFP_NORETRY;	/* don't loop in __alloc_pages */
392b84a35beSNick Piggin 	gfp_mask |= __GFP_NOWARN;	/* failures are OK */
3931da177e4SLinus Torvalds 
394d0164adcSMel Gorman 	gfp_temp = gfp_mask & ~(__GFP_DIRECT_RECLAIM|__GFP_IO);
39520a77776SNick Piggin 
3961da177e4SLinus Torvalds repeat_alloc:
39720a77776SNick Piggin 
39820a77776SNick Piggin 	element = pool->alloc(gfp_temp, pool->pool_data);
3991da177e4SLinus Torvalds 	if (likely(element != NULL))
4001da177e4SLinus Torvalds 		return element;
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	spin_lock_irqsave(&pool->lock, flags);
4031da177e4SLinus Torvalds 	if (likely(pool->curr_nr)) {
4048cded866SJia-Ju Bai 		element = remove_element(pool);
4051da177e4SLinus Torvalds 		spin_unlock_irqrestore(&pool->lock, flags);
4065b990546STejun Heo 		/* paired with rmb in mempool_free(), read comment there */
4075b990546STejun Heo 		smp_wmb();
40817411962SCatalin Marinas 		/*
40917411962SCatalin Marinas 		 * Update the allocation stack trace as this is more useful
41017411962SCatalin Marinas 		 * for debugging.
41117411962SCatalin Marinas 		 */
41217411962SCatalin Marinas 		kmemleak_update_trace(element);
4131da177e4SLinus Torvalds 		return element;
4141da177e4SLinus Torvalds 	}
4151da177e4SLinus Torvalds 
4161ebb7044STejun Heo 	/*
417d0164adcSMel Gorman 	 * We use gfp mask w/o direct reclaim or IO for the first round.  If
4181ebb7044STejun Heo 	 * alloc failed with that and @pool was empty, retry immediately.
4191ebb7044STejun Heo 	 */
4204e390b2bSMichal Hocko 	if (gfp_temp != gfp_mask) {
4211ebb7044STejun Heo 		spin_unlock_irqrestore(&pool->lock, flags);
4221ebb7044STejun Heo 		gfp_temp = gfp_mask;
4231ebb7044STejun Heo 		goto repeat_alloc;
4241ebb7044STejun Heo 	}
4251ebb7044STejun Heo 
426d0164adcSMel Gorman 	/* We must not sleep if !__GFP_DIRECT_RECLAIM */
427d0164adcSMel Gorman 	if (!(gfp_mask & __GFP_DIRECT_RECLAIM)) {
4285b990546STejun Heo 		spin_unlock_irqrestore(&pool->lock, flags);
4291da177e4SLinus Torvalds 		return NULL;
4305b990546STejun Heo 	}
4311da177e4SLinus Torvalds 
4325b990546STejun Heo 	/* Let's wait for someone else to return an element to @pool */
43301890a4cSBenjamin LaHaise 	init_wait(&wait);
4341da177e4SLinus Torvalds 	prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
4355b990546STejun Heo 
4365b990546STejun Heo 	spin_unlock_irqrestore(&pool->lock, flags);
4375b990546STejun Heo 
4380b1d647aSPavel Mironchik 	/*
4395b990546STejun Heo 	 * FIXME: this should be io_schedule().  The timeout is there as a
4405b990546STejun Heo 	 * workaround for some DM problems in 2.6.18.
4410b1d647aSPavel Mironchik 	 */
4420b1d647aSPavel Mironchik 	io_schedule_timeout(5*HZ);
4431da177e4SLinus Torvalds 
4445b990546STejun Heo 	finish_wait(&pool->wait, &wait);
4451da177e4SLinus Torvalds 	goto repeat_alloc;
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds EXPORT_SYMBOL(mempool_alloc);
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds /**
4501da177e4SLinus Torvalds  * mempool_free - return an element to the pool.
4511da177e4SLinus Torvalds  * @element:   pool element pointer.
4521da177e4SLinus Torvalds  * @pool:      pointer to the memory pool which was allocated via
4531da177e4SLinus Torvalds  *             mempool_create().
4541da177e4SLinus Torvalds  *
4551da177e4SLinus Torvalds  * this function only sleeps if the free_fn() function sleeps.
4561da177e4SLinus Torvalds  */
mempool_free(void * element,mempool_t * pool)4571da177e4SLinus Torvalds void mempool_free(void *element, mempool_t *pool)
4581da177e4SLinus Torvalds {
4591da177e4SLinus Torvalds 	unsigned long flags;
4601da177e4SLinus Torvalds 
461c80e7a82SRusty Russell 	if (unlikely(element == NULL))
462c80e7a82SRusty Russell 		return;
463c80e7a82SRusty Russell 
4645b990546STejun Heo 	/*
4655b990546STejun Heo 	 * Paired with the wmb in mempool_alloc().  The preceding read is
4665b990546STejun Heo 	 * for @element and the following @pool->curr_nr.  This ensures
4675b990546STejun Heo 	 * that the visible value of @pool->curr_nr is from after the
4685b990546STejun Heo 	 * allocation of @element.  This is necessary for fringe cases
4695b990546STejun Heo 	 * where @element was passed to this task without going through
4705b990546STejun Heo 	 * barriers.
4715b990546STejun Heo 	 *
4725b990546STejun Heo 	 * For example, assume @p is %NULL at the beginning and one task
4735b990546STejun Heo 	 * performs "p = mempool_alloc(...);" while another task is doing
4745b990546STejun Heo 	 * "while (!p) cpu_relax(); mempool_free(p, ...);".  This function
4755b990546STejun Heo 	 * may end up using curr_nr value which is from before allocation
4765b990546STejun Heo 	 * of @p without the following rmb.
4775b990546STejun Heo 	 */
4785b990546STejun Heo 	smp_rmb();
4795b990546STejun Heo 
4805b990546STejun Heo 	/*
4815b990546STejun Heo 	 * For correctness, we need a test which is guaranteed to trigger
4825b990546STejun Heo 	 * if curr_nr + #allocated == min_nr.  Testing curr_nr < min_nr
4835b990546STejun Heo 	 * without locking achieves that and refilling as soon as possible
4845b990546STejun Heo 	 * is desirable.
4855b990546STejun Heo 	 *
4865b990546STejun Heo 	 * Because curr_nr visible here is always a value after the
4875b990546STejun Heo 	 * allocation of @element, any task which decremented curr_nr below
4885b990546STejun Heo 	 * min_nr is guaranteed to see curr_nr < min_nr unless curr_nr gets
4895b990546STejun Heo 	 * incremented to min_nr afterwards.  If curr_nr gets incremented
4905b990546STejun Heo 	 * to min_nr after the allocation of @element, the elements
4915b990546STejun Heo 	 * allocated after that are subject to the same guarantee.
4925b990546STejun Heo 	 *
4935b990546STejun Heo 	 * Waiters happen iff curr_nr is 0 and the above guarantee also
4945b990546STejun Heo 	 * ensures that there will be frees which return elements to the
4955b990546STejun Heo 	 * pool waking up the waiters.
4965b990546STejun Heo 	 */
497abe1de42SQian Cai 	if (unlikely(READ_ONCE(pool->curr_nr) < pool->min_nr)) {
4981da177e4SLinus Torvalds 		spin_lock_irqsave(&pool->lock, flags);
499eb9a3c62SMikulas Patocka 		if (likely(pool->curr_nr < pool->min_nr)) {
5001da177e4SLinus Torvalds 			add_element(pool, element);
5011da177e4SLinus Torvalds 			spin_unlock_irqrestore(&pool->lock, flags);
5021da177e4SLinus Torvalds 			wake_up(&pool->wait);
5031da177e4SLinus Torvalds 			return;
5041da177e4SLinus Torvalds 		}
5051da177e4SLinus Torvalds 		spin_unlock_irqrestore(&pool->lock, flags);
5061da177e4SLinus Torvalds 	}
5071da177e4SLinus Torvalds 	pool->free(element, pool->pool_data);
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds EXPORT_SYMBOL(mempool_free);
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds /*
5121da177e4SLinus Torvalds  * A commonly used alloc and free fn.
5131da177e4SLinus Torvalds  */
mempool_alloc_slab(gfp_t gfp_mask,void * pool_data)514dd0fc66fSAl Viro void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data)
5151da177e4SLinus Torvalds {
516fcc234f8SPekka Enberg 	struct kmem_cache *mem = pool_data;
517e244c9e6SDavid Rientjes 	VM_BUG_ON(mem->ctor);
5181da177e4SLinus Torvalds 	return kmem_cache_alloc(mem, gfp_mask);
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds EXPORT_SYMBOL(mempool_alloc_slab);
5211da177e4SLinus Torvalds 
mempool_free_slab(void * element,void * pool_data)5221da177e4SLinus Torvalds void mempool_free_slab(void *element, void *pool_data)
5231da177e4SLinus Torvalds {
524fcc234f8SPekka Enberg 	struct kmem_cache *mem = pool_data;
5251da177e4SLinus Torvalds 	kmem_cache_free(mem, element);
5261da177e4SLinus Torvalds }
5271da177e4SLinus Torvalds EXPORT_SYMBOL(mempool_free_slab);
5286e0678f3SMatthew Dobson 
5296e0678f3SMatthew Dobson /*
53053184082SMatthew Dobson  * A commonly used alloc and free fn that kmalloc/kfrees the amount of memory
531183ff22bSSimon Arlott  * specified by pool_data
53253184082SMatthew Dobson  */
mempool_kmalloc(gfp_t gfp_mask,void * pool_data)53353184082SMatthew Dobson void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data)
53453184082SMatthew Dobson {
5355e2f89b5SFigo.zhang 	size_t size = (size_t)pool_data;
53653184082SMatthew Dobson 	return kmalloc(size, gfp_mask);
53753184082SMatthew Dobson }
53853184082SMatthew Dobson EXPORT_SYMBOL(mempool_kmalloc);
53953184082SMatthew Dobson 
mempool_kfree(void * element,void * pool_data)54053184082SMatthew Dobson void mempool_kfree(void *element, void *pool_data)
54153184082SMatthew Dobson {
54253184082SMatthew Dobson 	kfree(element);
54353184082SMatthew Dobson }
54453184082SMatthew Dobson EXPORT_SYMBOL(mempool_kfree);
54553184082SMatthew Dobson 
54653184082SMatthew Dobson /*
5476e0678f3SMatthew Dobson  * A simple mempool-backed page allocator that allocates pages
5486e0678f3SMatthew Dobson  * of the order specified by pool_data.
5496e0678f3SMatthew Dobson  */
mempool_alloc_pages(gfp_t gfp_mask,void * pool_data)5506e0678f3SMatthew Dobson void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data)
5516e0678f3SMatthew Dobson {
5526e0678f3SMatthew Dobson 	int order = (int)(long)pool_data;
5536e0678f3SMatthew Dobson 	return alloc_pages(gfp_mask, order);
5546e0678f3SMatthew Dobson }
5556e0678f3SMatthew Dobson EXPORT_SYMBOL(mempool_alloc_pages);
5566e0678f3SMatthew Dobson 
mempool_free_pages(void * element,void * pool_data)5576e0678f3SMatthew Dobson void mempool_free_pages(void *element, void *pool_data)
5586e0678f3SMatthew Dobson {
5596e0678f3SMatthew Dobson 	int order = (int)(long)pool_data;
5606e0678f3SMatthew Dobson 	__free_pages(element, order);
5616e0678f3SMatthew Dobson }
5626e0678f3SMatthew Dobson EXPORT_SYMBOL(mempool_free_pages);
563