xref: /openbmc/linux/mm/mempool.c (revision ac6424b981bce1c4bc55675c6ce11bfe1bbfa64f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/mm/mempool.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  memory buffer pool support. Such pools are mostly used
51da177e4SLinus Torvalds  *  for guaranteed, deadlock-free memory allocations during
61da177e4SLinus Torvalds  *  extreme VM load.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  started by Ingo Molnar, Copyright (C) 2001
9bdfedb76SDavid Rientjes  *  debugging by David Rientjes, Copyright (C) 2015
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/mm.h>
131da177e4SLinus Torvalds #include <linux/slab.h>
14bdfedb76SDavid Rientjes #include <linux/highmem.h>
1592393615SAndrey Ryabinin #include <linux/kasan.h>
1617411962SCatalin Marinas #include <linux/kmemleak.h>
17b95f1b31SPaul Gortmaker #include <linux/export.h>
181da177e4SLinus Torvalds #include <linux/mempool.h>
191da177e4SLinus Torvalds #include <linux/blkdev.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)
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 
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 
57bdfedb76SDavid Rientjes static void check_element(mempool_t *pool, void *element)
58bdfedb76SDavid Rientjes {
59bdfedb76SDavid Rientjes 	/* Mempools backed by slab allocator */
60bdfedb76SDavid Rientjes 	if (pool->free == mempool_free_slab || pool->free == mempool_kfree)
61bdfedb76SDavid Rientjes 		__check_element(pool, element, ksize(element));
62bdfedb76SDavid Rientjes 
63bdfedb76SDavid Rientjes 	/* Mempools backed by page allocator */
64bdfedb76SDavid Rientjes 	if (pool->free == mempool_free_pages) {
65bdfedb76SDavid Rientjes 		int order = (int)(long)pool->pool_data;
66bdfedb76SDavid Rientjes 		void *addr = kmap_atomic((struct page *)element);
67bdfedb76SDavid Rientjes 
68bdfedb76SDavid Rientjes 		__check_element(pool, addr, 1UL << (PAGE_SHIFT + order));
69bdfedb76SDavid Rientjes 		kunmap_atomic(addr);
70bdfedb76SDavid Rientjes 	}
71bdfedb76SDavid Rientjes }
72bdfedb76SDavid Rientjes 
73bdfedb76SDavid Rientjes static void __poison_element(void *element, size_t size)
74bdfedb76SDavid Rientjes {
75bdfedb76SDavid Rientjes 	u8 *obj = element;
76bdfedb76SDavid Rientjes 
77bdfedb76SDavid Rientjes 	memset(obj, POISON_FREE, size - 1);
78bdfedb76SDavid Rientjes 	obj[size - 1] = POISON_END;
79bdfedb76SDavid Rientjes }
80bdfedb76SDavid Rientjes 
81bdfedb76SDavid Rientjes static void poison_element(mempool_t *pool, void *element)
82bdfedb76SDavid Rientjes {
83bdfedb76SDavid Rientjes 	/* Mempools backed by slab allocator */
84bdfedb76SDavid Rientjes 	if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
85bdfedb76SDavid Rientjes 		__poison_element(element, ksize(element));
86bdfedb76SDavid Rientjes 
87bdfedb76SDavid Rientjes 	/* Mempools backed by page allocator */
88bdfedb76SDavid Rientjes 	if (pool->alloc == mempool_alloc_pages) {
89bdfedb76SDavid Rientjes 		int order = (int)(long)pool->pool_data;
90bdfedb76SDavid Rientjes 		void *addr = kmap_atomic((struct page *)element);
91bdfedb76SDavid Rientjes 
92bdfedb76SDavid Rientjes 		__poison_element(addr, 1UL << (PAGE_SHIFT + order));
93bdfedb76SDavid Rientjes 		kunmap_atomic(addr);
94bdfedb76SDavid Rientjes 	}
95bdfedb76SDavid Rientjes }
96bdfedb76SDavid Rientjes #else /* CONFIG_DEBUG_SLAB || CONFIG_SLUB_DEBUG_ON */
97bdfedb76SDavid Rientjes static inline void check_element(mempool_t *pool, void *element)
98bdfedb76SDavid Rientjes {
99bdfedb76SDavid Rientjes }
100bdfedb76SDavid Rientjes static inline void poison_element(mempool_t *pool, void *element)
101bdfedb76SDavid Rientjes {
102bdfedb76SDavid Rientjes }
103bdfedb76SDavid Rientjes #endif /* CONFIG_DEBUG_SLAB || CONFIG_SLUB_DEBUG_ON */
104bdfedb76SDavid Rientjes 
10592393615SAndrey Ryabinin static void kasan_poison_element(mempool_t *pool, void *element)
10692393615SAndrey Ryabinin {
1079b75a867SAndrey Ryabinin 	if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
1089b75a867SAndrey Ryabinin 		kasan_poison_kfree(element);
10992393615SAndrey Ryabinin 	if (pool->alloc == mempool_alloc_pages)
11092393615SAndrey Ryabinin 		kasan_free_pages(element, (unsigned long)pool->pool_data);
11192393615SAndrey Ryabinin }
11292393615SAndrey Ryabinin 
113505f5dcbSAlexander Potapenko static void kasan_unpoison_element(mempool_t *pool, void *element, gfp_t flags)
11492393615SAndrey Ryabinin {
1159b75a867SAndrey Ryabinin 	if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
1169b75a867SAndrey Ryabinin 		kasan_unpoison_slab(element);
11792393615SAndrey Ryabinin 	if (pool->alloc == mempool_alloc_pages)
11892393615SAndrey Ryabinin 		kasan_alloc_pages(element, (unsigned long)pool->pool_data);
11992393615SAndrey Ryabinin }
12092393615SAndrey Ryabinin 
1211da177e4SLinus Torvalds static void add_element(mempool_t *pool, void *element)
1221da177e4SLinus Torvalds {
1231da177e4SLinus Torvalds 	BUG_ON(pool->curr_nr >= pool->min_nr);
124bdfedb76SDavid Rientjes 	poison_element(pool, element);
12592393615SAndrey Ryabinin 	kasan_poison_element(pool, element);
1261da177e4SLinus Torvalds 	pool->elements[pool->curr_nr++] = element;
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
129505f5dcbSAlexander Potapenko static void *remove_element(mempool_t *pool, gfp_t flags)
1301da177e4SLinus Torvalds {
131bdfedb76SDavid Rientjes 	void *element = pool->elements[--pool->curr_nr];
132bdfedb76SDavid Rientjes 
133bdfedb76SDavid Rientjes 	BUG_ON(pool->curr_nr < 0);
134505f5dcbSAlexander Potapenko 	kasan_unpoison_element(pool, element, flags);
13576401310SMatthew Dawson 	check_element(pool, element);
136bdfedb76SDavid Rientjes 	return element;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1390565d317STejun Heo /**
1400565d317STejun Heo  * mempool_destroy - deallocate a memory pool
1410565d317STejun Heo  * @pool:      pointer to the memory pool which was allocated via
1420565d317STejun Heo  *             mempool_create().
1430565d317STejun Heo  *
1440565d317STejun Heo  * Free all reserved elements in @pool and @pool itself.  This function
1450565d317STejun Heo  * only sleeps if the free_fn() function sleeps.
1460565d317STejun Heo  */
1470565d317STejun Heo void mempool_destroy(mempool_t *pool)
1481da177e4SLinus Torvalds {
1494e3ca3e0SSergey Senozhatsky 	if (unlikely(!pool))
1504e3ca3e0SSergey Senozhatsky 		return;
1514e3ca3e0SSergey Senozhatsky 
1521da177e4SLinus Torvalds 	while (pool->curr_nr) {
153505f5dcbSAlexander Potapenko 		void *element = remove_element(pool, GFP_KERNEL);
1541da177e4SLinus Torvalds 		pool->free(element, pool->pool_data);
1551da177e4SLinus Torvalds 	}
1561da177e4SLinus Torvalds 	kfree(pool->elements);
1571da177e4SLinus Torvalds 	kfree(pool);
1581da177e4SLinus Torvalds }
1590565d317STejun Heo EXPORT_SYMBOL(mempool_destroy);
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds /**
1621da177e4SLinus Torvalds  * mempool_create - create a memory pool
1631da177e4SLinus Torvalds  * @min_nr:    the minimum number of elements guaranteed to be
1641da177e4SLinus Torvalds  *             allocated for this pool.
1651da177e4SLinus Torvalds  * @alloc_fn:  user-defined element-allocation function.
1661da177e4SLinus Torvalds  * @free_fn:   user-defined element-freeing function.
1671da177e4SLinus Torvalds  * @pool_data: optional private data available to the user-defined functions.
1681da177e4SLinus Torvalds  *
1691da177e4SLinus Torvalds  * this function creates and allocates a guaranteed size, preallocated
17072fd4a35SRobert P. J. Day  * memory pool. The pool can be used from the mempool_alloc() and mempool_free()
1711da177e4SLinus Torvalds  * functions. This function might sleep. Both the alloc_fn() and the free_fn()
17272fd4a35SRobert P. J. Day  * functions might sleep - as long as the mempool_alloc() function is not called
1731da177e4SLinus Torvalds  * from IRQ contexts.
1741da177e4SLinus Torvalds  */
1751da177e4SLinus Torvalds mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
1761da177e4SLinus Torvalds 				mempool_free_t *free_fn, void *pool_data)
1771da177e4SLinus Torvalds {
178a91a5ac6STejun Heo 	return mempool_create_node(min_nr,alloc_fn,free_fn, pool_data,
179a91a5ac6STejun Heo 				   GFP_KERNEL, NUMA_NO_NODE);
1801946089aSChristoph Lameter }
1811946089aSChristoph Lameter EXPORT_SYMBOL(mempool_create);
1821da177e4SLinus Torvalds 
1831946089aSChristoph Lameter mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
184a91a5ac6STejun Heo 			       mempool_free_t *free_fn, void *pool_data,
185a91a5ac6STejun Heo 			       gfp_t gfp_mask, int node_id)
1861946089aSChristoph Lameter {
1871946089aSChristoph Lameter 	mempool_t *pool;
1887b5219dbSJoe Perches 	pool = kzalloc_node(sizeof(*pool), gfp_mask, node_id);
1891da177e4SLinus Torvalds 	if (!pool)
1901da177e4SLinus Torvalds 		return NULL;
1911946089aSChristoph Lameter 	pool->elements = kmalloc_node(min_nr * sizeof(void *),
192a91a5ac6STejun Heo 				      gfp_mask, node_id);
1931da177e4SLinus Torvalds 	if (!pool->elements) {
1941da177e4SLinus Torvalds 		kfree(pool);
1951da177e4SLinus Torvalds 		return NULL;
1961da177e4SLinus Torvalds 	}
1971da177e4SLinus Torvalds 	spin_lock_init(&pool->lock);
1981da177e4SLinus Torvalds 	pool->min_nr = min_nr;
1991da177e4SLinus Torvalds 	pool->pool_data = pool_data;
2001da177e4SLinus Torvalds 	init_waitqueue_head(&pool->wait);
2011da177e4SLinus Torvalds 	pool->alloc = alloc_fn;
2021da177e4SLinus Torvalds 	pool->free = free_fn;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	/*
2051da177e4SLinus Torvalds 	 * First pre-allocate the guaranteed number of buffers.
2061da177e4SLinus Torvalds 	 */
2071da177e4SLinus Torvalds 	while (pool->curr_nr < pool->min_nr) {
2081da177e4SLinus Torvalds 		void *element;
2091da177e4SLinus Torvalds 
210a91a5ac6STejun Heo 		element = pool->alloc(gfp_mask, pool->pool_data);
2111da177e4SLinus Torvalds 		if (unlikely(!element)) {
2120565d317STejun Heo 			mempool_destroy(pool);
2131da177e4SLinus Torvalds 			return NULL;
2141da177e4SLinus Torvalds 		}
2151da177e4SLinus Torvalds 		add_element(pool, element);
2161da177e4SLinus Torvalds 	}
2171da177e4SLinus Torvalds 	return pool;
2181da177e4SLinus Torvalds }
2191946089aSChristoph Lameter EXPORT_SYMBOL(mempool_create_node);
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds /**
2221da177e4SLinus Torvalds  * mempool_resize - resize an existing memory pool
2231da177e4SLinus Torvalds  * @pool:       pointer to the memory pool which was allocated via
2241da177e4SLinus Torvalds  *              mempool_create().
2251da177e4SLinus Torvalds  * @new_min_nr: the new minimum number of elements guaranteed to be
2261da177e4SLinus Torvalds  *              allocated for this pool.
2271da177e4SLinus Torvalds  *
2281da177e4SLinus Torvalds  * This function shrinks/grows the pool. In the case of growing,
2291da177e4SLinus Torvalds  * it cannot be guaranteed that the pool will be grown to the new
2301da177e4SLinus Torvalds  * size immediately, but new mempool_free() calls will refill it.
23111d83360SDavid Rientjes  * This function may sleep.
2321da177e4SLinus Torvalds  *
2331da177e4SLinus Torvalds  * Note, the caller must guarantee that no mempool_destroy is called
2341da177e4SLinus Torvalds  * while this function is running. mempool_alloc() & mempool_free()
2351da177e4SLinus Torvalds  * might be called (eg. from IRQ contexts) while this function executes.
2361da177e4SLinus Torvalds  */
23711d83360SDavid Rientjes int mempool_resize(mempool_t *pool, int new_min_nr)
2381da177e4SLinus Torvalds {
2391da177e4SLinus Torvalds 	void *element;
2401da177e4SLinus Torvalds 	void **new_elements;
2411da177e4SLinus Torvalds 	unsigned long flags;
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	BUG_ON(new_min_nr <= 0);
24411d83360SDavid Rientjes 	might_sleep();
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	spin_lock_irqsave(&pool->lock, flags);
2471da177e4SLinus Torvalds 	if (new_min_nr <= pool->min_nr) {
2481da177e4SLinus Torvalds 		while (new_min_nr < pool->curr_nr) {
249505f5dcbSAlexander Potapenko 			element = remove_element(pool, GFP_KERNEL);
2501da177e4SLinus Torvalds 			spin_unlock_irqrestore(&pool->lock, flags);
2511da177e4SLinus Torvalds 			pool->free(element, pool->pool_data);
2521da177e4SLinus Torvalds 			spin_lock_irqsave(&pool->lock, flags);
2531da177e4SLinus Torvalds 		}
2541da177e4SLinus Torvalds 		pool->min_nr = new_min_nr;
2551da177e4SLinus Torvalds 		goto out_unlock;
2561da177e4SLinus Torvalds 	}
2571da177e4SLinus Torvalds 	spin_unlock_irqrestore(&pool->lock, flags);
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	/* Grow the pool */
26011d83360SDavid Rientjes 	new_elements = kmalloc_array(new_min_nr, sizeof(*new_elements),
26111d83360SDavid Rientjes 				     GFP_KERNEL);
2621da177e4SLinus Torvalds 	if (!new_elements)
2631da177e4SLinus Torvalds 		return -ENOMEM;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	spin_lock_irqsave(&pool->lock, flags);
2661da177e4SLinus Torvalds 	if (unlikely(new_min_nr <= pool->min_nr)) {
2671da177e4SLinus Torvalds 		/* Raced, other resize will do our work */
2681da177e4SLinus Torvalds 		spin_unlock_irqrestore(&pool->lock, flags);
2691da177e4SLinus Torvalds 		kfree(new_elements);
2701da177e4SLinus Torvalds 		goto out;
2711da177e4SLinus Torvalds 	}
2721da177e4SLinus Torvalds 	memcpy(new_elements, pool->elements,
2731da177e4SLinus Torvalds 			pool->curr_nr * sizeof(*new_elements));
2741da177e4SLinus Torvalds 	kfree(pool->elements);
2751da177e4SLinus Torvalds 	pool->elements = new_elements;
2761da177e4SLinus Torvalds 	pool->min_nr = new_min_nr;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	while (pool->curr_nr < pool->min_nr) {
2791da177e4SLinus Torvalds 		spin_unlock_irqrestore(&pool->lock, flags);
28011d83360SDavid Rientjes 		element = pool->alloc(GFP_KERNEL, pool->pool_data);
2811da177e4SLinus Torvalds 		if (!element)
2821da177e4SLinus Torvalds 			goto out;
2831da177e4SLinus Torvalds 		spin_lock_irqsave(&pool->lock, flags);
2841da177e4SLinus Torvalds 		if (pool->curr_nr < pool->min_nr) {
2851da177e4SLinus Torvalds 			add_element(pool, element);
2861da177e4SLinus Torvalds 		} else {
2871da177e4SLinus Torvalds 			spin_unlock_irqrestore(&pool->lock, flags);
2881da177e4SLinus Torvalds 			pool->free(element, pool->pool_data);	/* Raced */
2891da177e4SLinus Torvalds 			goto out;
2901da177e4SLinus Torvalds 		}
2911da177e4SLinus Torvalds 	}
2921da177e4SLinus Torvalds out_unlock:
2931da177e4SLinus Torvalds 	spin_unlock_irqrestore(&pool->lock, flags);
2941da177e4SLinus Torvalds out:
2951da177e4SLinus Torvalds 	return 0;
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds EXPORT_SYMBOL(mempool_resize);
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds /**
3001da177e4SLinus Torvalds  * mempool_alloc - allocate an element from a specific memory pool
3011da177e4SLinus Torvalds  * @pool:      pointer to the memory pool which was allocated via
3021da177e4SLinus Torvalds  *             mempool_create().
3031da177e4SLinus Torvalds  * @gfp_mask:  the usual allocation bitmask.
3041da177e4SLinus Torvalds  *
30572fd4a35SRobert P. J. Day  * this function only sleeps if the alloc_fn() function sleeps or
3061da177e4SLinus Torvalds  * returns NULL. Note that due to preallocation, this function
3071da177e4SLinus Torvalds  * *never* fails when called from process contexts. (it might
3081da177e4SLinus Torvalds  * fail if called from an IRQ context.)
3094e390b2bSMichal Hocko  * Note: using __GFP_ZERO is not supported.
3101da177e4SLinus Torvalds  */
311dd0fc66fSAl Viro void *mempool_alloc(mempool_t *pool, gfp_t gfp_mask)
3121da177e4SLinus Torvalds {
3131da177e4SLinus Torvalds 	void *element;
3141da177e4SLinus Torvalds 	unsigned long flags;
315*ac6424b9SIngo Molnar 	wait_queue_entry_t wait;
3166daa0e28SAl Viro 	gfp_t gfp_temp;
31720a77776SNick Piggin 
3188bf8fcb0SSebastian Ott 	VM_WARN_ON_ONCE(gfp_mask & __GFP_ZERO);
319d0164adcSMel Gorman 	might_sleep_if(gfp_mask & __GFP_DIRECT_RECLAIM);
320b84a35beSNick Piggin 
3214e390b2bSMichal Hocko 	gfp_mask |= __GFP_NOMEMALLOC;	/* don't allocate emergency reserves */
322b84a35beSNick Piggin 	gfp_mask |= __GFP_NORETRY;	/* don't loop in __alloc_pages */
323b84a35beSNick Piggin 	gfp_mask |= __GFP_NOWARN;	/* failures are OK */
3241da177e4SLinus Torvalds 
325d0164adcSMel Gorman 	gfp_temp = gfp_mask & ~(__GFP_DIRECT_RECLAIM|__GFP_IO);
32620a77776SNick Piggin 
3271da177e4SLinus Torvalds repeat_alloc:
32820a77776SNick Piggin 
32920a77776SNick Piggin 	element = pool->alloc(gfp_temp, pool->pool_data);
3301da177e4SLinus Torvalds 	if (likely(element != NULL))
3311da177e4SLinus Torvalds 		return element;
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	spin_lock_irqsave(&pool->lock, flags);
3341da177e4SLinus Torvalds 	if (likely(pool->curr_nr)) {
335505f5dcbSAlexander Potapenko 		element = remove_element(pool, gfp_temp);
3361da177e4SLinus Torvalds 		spin_unlock_irqrestore(&pool->lock, flags);
3375b990546STejun Heo 		/* paired with rmb in mempool_free(), read comment there */
3385b990546STejun Heo 		smp_wmb();
33917411962SCatalin Marinas 		/*
34017411962SCatalin Marinas 		 * Update the allocation stack trace as this is more useful
34117411962SCatalin Marinas 		 * for debugging.
34217411962SCatalin Marinas 		 */
34317411962SCatalin Marinas 		kmemleak_update_trace(element);
3441da177e4SLinus Torvalds 		return element;
3451da177e4SLinus Torvalds 	}
3461da177e4SLinus Torvalds 
3471ebb7044STejun Heo 	/*
348d0164adcSMel Gorman 	 * We use gfp mask w/o direct reclaim or IO for the first round.  If
3491ebb7044STejun Heo 	 * alloc failed with that and @pool was empty, retry immediately.
3501ebb7044STejun Heo 	 */
3514e390b2bSMichal Hocko 	if (gfp_temp != gfp_mask) {
3521ebb7044STejun Heo 		spin_unlock_irqrestore(&pool->lock, flags);
3531ebb7044STejun Heo 		gfp_temp = gfp_mask;
3541ebb7044STejun Heo 		goto repeat_alloc;
3551ebb7044STejun Heo 	}
3561ebb7044STejun Heo 
357d0164adcSMel Gorman 	/* We must not sleep if !__GFP_DIRECT_RECLAIM */
358d0164adcSMel Gorman 	if (!(gfp_mask & __GFP_DIRECT_RECLAIM)) {
3595b990546STejun Heo 		spin_unlock_irqrestore(&pool->lock, flags);
3601da177e4SLinus Torvalds 		return NULL;
3615b990546STejun Heo 	}
3621da177e4SLinus Torvalds 
3635b990546STejun Heo 	/* Let's wait for someone else to return an element to @pool */
36401890a4cSBenjamin LaHaise 	init_wait(&wait);
3651da177e4SLinus Torvalds 	prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
3665b990546STejun Heo 
3675b990546STejun Heo 	spin_unlock_irqrestore(&pool->lock, flags);
3685b990546STejun Heo 
3690b1d647aSPavel Mironchik 	/*
3705b990546STejun Heo 	 * FIXME: this should be io_schedule().  The timeout is there as a
3715b990546STejun Heo 	 * workaround for some DM problems in 2.6.18.
3720b1d647aSPavel Mironchik 	 */
3730b1d647aSPavel Mironchik 	io_schedule_timeout(5*HZ);
3741da177e4SLinus Torvalds 
3755b990546STejun Heo 	finish_wait(&pool->wait, &wait);
3761da177e4SLinus Torvalds 	goto repeat_alloc;
3771da177e4SLinus Torvalds }
3781da177e4SLinus Torvalds EXPORT_SYMBOL(mempool_alloc);
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds /**
3811da177e4SLinus Torvalds  * mempool_free - return an element to the pool.
3821da177e4SLinus Torvalds  * @element:   pool element pointer.
3831da177e4SLinus Torvalds  * @pool:      pointer to the memory pool which was allocated via
3841da177e4SLinus Torvalds  *             mempool_create().
3851da177e4SLinus Torvalds  *
3861da177e4SLinus Torvalds  * this function only sleeps if the free_fn() function sleeps.
3871da177e4SLinus Torvalds  */
3881da177e4SLinus Torvalds void mempool_free(void *element, mempool_t *pool)
3891da177e4SLinus Torvalds {
3901da177e4SLinus Torvalds 	unsigned long flags;
3911da177e4SLinus Torvalds 
392c80e7a82SRusty Russell 	if (unlikely(element == NULL))
393c80e7a82SRusty Russell 		return;
394c80e7a82SRusty Russell 
3955b990546STejun Heo 	/*
3965b990546STejun Heo 	 * Paired with the wmb in mempool_alloc().  The preceding read is
3975b990546STejun Heo 	 * for @element and the following @pool->curr_nr.  This ensures
3985b990546STejun Heo 	 * that the visible value of @pool->curr_nr is from after the
3995b990546STejun Heo 	 * allocation of @element.  This is necessary for fringe cases
4005b990546STejun Heo 	 * where @element was passed to this task without going through
4015b990546STejun Heo 	 * barriers.
4025b990546STejun Heo 	 *
4035b990546STejun Heo 	 * For example, assume @p is %NULL at the beginning and one task
4045b990546STejun Heo 	 * performs "p = mempool_alloc(...);" while another task is doing
4055b990546STejun Heo 	 * "while (!p) cpu_relax(); mempool_free(p, ...);".  This function
4065b990546STejun Heo 	 * may end up using curr_nr value which is from before allocation
4075b990546STejun Heo 	 * of @p without the following rmb.
4085b990546STejun Heo 	 */
4095b990546STejun Heo 	smp_rmb();
4105b990546STejun Heo 
4115b990546STejun Heo 	/*
4125b990546STejun Heo 	 * For correctness, we need a test which is guaranteed to trigger
4135b990546STejun Heo 	 * if curr_nr + #allocated == min_nr.  Testing curr_nr < min_nr
4145b990546STejun Heo 	 * without locking achieves that and refilling as soon as possible
4155b990546STejun Heo 	 * is desirable.
4165b990546STejun Heo 	 *
4175b990546STejun Heo 	 * Because curr_nr visible here is always a value after the
4185b990546STejun Heo 	 * allocation of @element, any task which decremented curr_nr below
4195b990546STejun Heo 	 * min_nr is guaranteed to see curr_nr < min_nr unless curr_nr gets
4205b990546STejun Heo 	 * incremented to min_nr afterwards.  If curr_nr gets incremented
4215b990546STejun Heo 	 * to min_nr after the allocation of @element, the elements
4225b990546STejun Heo 	 * allocated after that are subject to the same guarantee.
4235b990546STejun Heo 	 *
4245b990546STejun Heo 	 * Waiters happen iff curr_nr is 0 and the above guarantee also
4255b990546STejun Heo 	 * ensures that there will be frees which return elements to the
4265b990546STejun Heo 	 * pool waking up the waiters.
4275b990546STejun Heo 	 */
428eb9a3c62SMikulas Patocka 	if (unlikely(pool->curr_nr < pool->min_nr)) {
4291da177e4SLinus Torvalds 		spin_lock_irqsave(&pool->lock, flags);
430eb9a3c62SMikulas Patocka 		if (likely(pool->curr_nr < pool->min_nr)) {
4311da177e4SLinus Torvalds 			add_element(pool, element);
4321da177e4SLinus Torvalds 			spin_unlock_irqrestore(&pool->lock, flags);
4331da177e4SLinus Torvalds 			wake_up(&pool->wait);
4341da177e4SLinus Torvalds 			return;
4351da177e4SLinus Torvalds 		}
4361da177e4SLinus Torvalds 		spin_unlock_irqrestore(&pool->lock, flags);
4371da177e4SLinus Torvalds 	}
4381da177e4SLinus Torvalds 	pool->free(element, pool->pool_data);
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds EXPORT_SYMBOL(mempool_free);
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds /*
4431da177e4SLinus Torvalds  * A commonly used alloc and free fn.
4441da177e4SLinus Torvalds  */
445dd0fc66fSAl Viro void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data)
4461da177e4SLinus Torvalds {
447fcc234f8SPekka Enberg 	struct kmem_cache *mem = pool_data;
448e244c9e6SDavid Rientjes 	VM_BUG_ON(mem->ctor);
4491da177e4SLinus Torvalds 	return kmem_cache_alloc(mem, gfp_mask);
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds EXPORT_SYMBOL(mempool_alloc_slab);
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds void mempool_free_slab(void *element, void *pool_data)
4541da177e4SLinus Torvalds {
455fcc234f8SPekka Enberg 	struct kmem_cache *mem = pool_data;
4561da177e4SLinus Torvalds 	kmem_cache_free(mem, element);
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds EXPORT_SYMBOL(mempool_free_slab);
4596e0678f3SMatthew Dobson 
4606e0678f3SMatthew Dobson /*
46153184082SMatthew Dobson  * A commonly used alloc and free fn that kmalloc/kfrees the amount of memory
462183ff22bSSimon Arlott  * specified by pool_data
46353184082SMatthew Dobson  */
46453184082SMatthew Dobson void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data)
46553184082SMatthew Dobson {
4665e2f89b5SFigo.zhang 	size_t size = (size_t)pool_data;
46753184082SMatthew Dobson 	return kmalloc(size, gfp_mask);
46853184082SMatthew Dobson }
46953184082SMatthew Dobson EXPORT_SYMBOL(mempool_kmalloc);
47053184082SMatthew Dobson 
47153184082SMatthew Dobson void mempool_kfree(void *element, void *pool_data)
47253184082SMatthew Dobson {
47353184082SMatthew Dobson 	kfree(element);
47453184082SMatthew Dobson }
47553184082SMatthew Dobson EXPORT_SYMBOL(mempool_kfree);
47653184082SMatthew Dobson 
47753184082SMatthew Dobson /*
4786e0678f3SMatthew Dobson  * A simple mempool-backed page allocator that allocates pages
4796e0678f3SMatthew Dobson  * of the order specified by pool_data.
4806e0678f3SMatthew Dobson  */
4816e0678f3SMatthew Dobson void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data)
4826e0678f3SMatthew Dobson {
4836e0678f3SMatthew Dobson 	int order = (int)(long)pool_data;
4846e0678f3SMatthew Dobson 	return alloc_pages(gfp_mask, order);
4856e0678f3SMatthew Dobson }
4866e0678f3SMatthew Dobson EXPORT_SYMBOL(mempool_alloc_pages);
4876e0678f3SMatthew Dobson 
4886e0678f3SMatthew Dobson void mempool_free_pages(void *element, void *pool_data)
4896e0678f3SMatthew Dobson {
4906e0678f3SMatthew Dobson 	int order = (int)(long)pool_data;
4916e0678f3SMatthew Dobson 	__free_pages(element, order);
4926e0678f3SMatthew Dobson }
4936e0678f3SMatthew Dobson EXPORT_SYMBOL(mempool_free_pages);
494