blob: fe19d290a30152d60a37882790711e1747cc4678 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/mm/mempool.c
4 *
5 * memory buffer pool support. Such pools are mostly used
6 * for guaranteed, deadlock-free memory allocations during
7 * extreme VM load.
8 *
9 * started by Ingo Molnar, Copyright (C) 2001
David Rientjesbdfedb72015-04-15 16:14:17 -070010 * debugging by David Rientjes, Copyright (C) 2015
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13#include <linux/mm.h>
14#include <linux/slab.h>
David Rientjesbdfedb72015-04-15 16:14:17 -070015#include <linux/highmem.h>
Andrey Ryabinin92393612015-04-15 16:15:05 -070016#include <linux/kasan.h>
Catalin Marinas17411962014-06-06 14:38:19 -070017#include <linux/kmemleak.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040018#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mempool.h>
20#include <linux/blkdev.h>
21#include <linux/writeback.h>
David Rientjese244c9e2015-04-15 16:14:14 -070022#include "slab.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
David Rientjesbdfedb72015-04-15 16:14:17 -070024#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB_DEBUG_ON)
25static void poison_error(mempool_t *pool, void *element, size_t size,
26 size_t byte)
27{
28 const int nr = pool->curr_nr;
29 const int start = max_t(int, byte - (BITS_PER_LONG / 8), 0);
30 const int end = min_t(int, byte + (BITS_PER_LONG / 8), size);
31 int i;
32
33 pr_err("BUG: mempool element poison mismatch\n");
34 pr_err("Mempool %p size %zu\n", pool, size);
35 pr_err(" nr=%d @ %p: %s0x", nr, element, start > 0 ? "... " : "");
36 for (i = start; i < end; i++)
37 pr_cont("%x ", *(u8 *)(element + i));
38 pr_cont("%s\n", end < size ? "..." : "");
39 dump_stack();
40}
41
42static void __check_element(mempool_t *pool, void *element, size_t size)
43{
44 u8 *obj = element;
45 size_t i;
46
47 for (i = 0; i < size; i++) {
48 u8 exp = (i < size - 1) ? POISON_FREE : POISON_END;
49
50 if (obj[i] != exp) {
51 poison_error(pool, element, size, i);
52 return;
53 }
54 }
55 memset(obj, POISON_INUSE, size);
56}
57
58static void check_element(mempool_t *pool, void *element)
59{
60 /* Mempools backed by slab allocator */
Miaohe Lin544941d2020-10-13 16:57:14 -070061 if (pool->free == mempool_free_slab || pool->free == mempool_kfree) {
David Rientjesbdfedb72015-04-15 16:14:17 -070062 __check_element(pool, element, ksize(element));
Miaohe Lin544941d2020-10-13 16:57:14 -070063 } else if (pool->free == mempool_free_pages) {
64 /* Mempools backed by page allocator */
David Rientjesbdfedb72015-04-15 16:14:17 -070065 int order = (int)(long)pool->pool_data;
66 void *addr = kmap_atomic((struct page *)element);
67
68 __check_element(pool, addr, 1UL << (PAGE_SHIFT + order));
69 kunmap_atomic(addr);
70 }
71}
72
73static void __poison_element(void *element, size_t size)
74{
75 u8 *obj = element;
76
77 memset(obj, POISON_FREE, size - 1);
78 obj[size - 1] = POISON_END;
79}
80
81static void poison_element(mempool_t *pool, void *element)
82{
83 /* Mempools backed by slab allocator */
Miaohe Lin544941d2020-10-13 16:57:14 -070084 if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc) {
David Rientjesbdfedb72015-04-15 16:14:17 -070085 __poison_element(element, ksize(element));
Miaohe Lin544941d2020-10-13 16:57:14 -070086 } else if (pool->alloc == mempool_alloc_pages) {
87 /* Mempools backed by page allocator */
David Rientjesbdfedb72015-04-15 16:14:17 -070088 int order = (int)(long)pool->pool_data;
89 void *addr = kmap_atomic((struct page *)element);
90
91 __poison_element(addr, 1UL << (PAGE_SHIFT + order));
92 kunmap_atomic(addr);
93 }
94}
95#else /* CONFIG_DEBUG_SLAB || CONFIG_SLUB_DEBUG_ON */
96static inline void check_element(mempool_t *pool, void *element)
97{
98}
99static inline void poison_element(mempool_t *pool, void *element)
100{
101}
102#endif /* CONFIG_DEBUG_SLAB || CONFIG_SLUB_DEBUG_ON */
103
Dmitry Vyukov6860f632018-02-06 15:36:30 -0800104static __always_inline void kasan_poison_element(mempool_t *pool, void *element)
Andrey Ryabinin92393612015-04-15 16:15:05 -0700105{
Andrey Ryabinin9b75a862016-06-24 14:49:34 -0700106 if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
Andrey Konovalov3cd65f52021-02-03 15:35:05 +1100107 kasan_slab_free_mempool(element);
Miaohe Lin544941d2020-10-13 16:57:14 -0700108 else if (pool->alloc == mempool_alloc_pages)
Andrey Konovalova1598942021-03-18 17:01:40 +1100109 kasan_free_pages(element, (unsigned long)pool->pool_data, false);
Andrey Ryabinin92393612015-04-15 16:15:05 -0700110}
111
Jia-Ju Bai8cded862018-08-17 15:45:22 -0700112static void kasan_unpoison_element(mempool_t *pool, void *element)
Andrey Ryabinin92393612015-04-15 16:15:05 -0700113{
Andrey Ryabinin9b75a862016-06-24 14:49:34 -0700114 if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
Andrey Konovalova0603052020-12-22 12:02:59 -0800115 kasan_unpoison_range(element, __ksize(element));
Miaohe Lin544941d2020-10-13 16:57:14 -0700116 else if (pool->alloc == mempool_alloc_pages)
Andrey Konovalova1598942021-03-18 17:01:40 +1100117 kasan_alloc_pages(element, (unsigned long)pool->pool_data, false);
Andrey Ryabinin92393612015-04-15 16:15:05 -0700118}
119
Dmitry Vyukov6860f632018-02-06 15:36:30 -0800120static __always_inline void add_element(mempool_t *pool, void *element)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 BUG_ON(pool->curr_nr >= pool->min_nr);
David Rientjesbdfedb72015-04-15 16:14:17 -0700123 poison_element(pool, element);
Andrey Ryabinin92393612015-04-15 16:15:05 -0700124 kasan_poison_element(pool, element);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 pool->elements[pool->curr_nr++] = element;
126}
127
Jia-Ju Bai8cded862018-08-17 15:45:22 -0700128static void *remove_element(mempool_t *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
David Rientjesbdfedb72015-04-15 16:14:17 -0700130 void *element = pool->elements[--pool->curr_nr];
131
132 BUG_ON(pool->curr_nr < 0);
Jia-Ju Bai8cded862018-08-17 15:45:22 -0700133 kasan_unpoison_element(pool, element);
Matthew Dawson76401312016-03-11 13:08:07 -0800134 check_element(pool, element);
David Rientjesbdfedb72015-04-15 16:14:17 -0700135 return element;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Tejun Heo0565d312012-01-10 15:08:26 -0800138/**
Kent Overstreetc1a67fe2015-05-04 16:52:20 -0700139 * mempool_exit - exit a mempool initialized with mempool_init()
140 * @pool: pointer to the memory pool which was initialized with
141 * mempool_init().
142 *
143 * Free all reserved elements in @pool and @pool itself. This function
144 * only sleeps if the free_fn() function sleeps.
145 *
146 * May be called on a zeroed but uninitialized mempool (i.e. allocated with
147 * kzalloc()).
148 */
149void mempool_exit(mempool_t *pool)
150{
151 while (pool->curr_nr) {
Jia-Ju Bai8cded862018-08-17 15:45:22 -0700152 void *element = remove_element(pool);
Kent Overstreetc1a67fe2015-05-04 16:52:20 -0700153 pool->free(element, pool->pool_data);
154 }
155 kfree(pool->elements);
156 pool->elements = NULL;
157}
158EXPORT_SYMBOL(mempool_exit);
159
160/**
Tejun Heo0565d312012-01-10 15:08:26 -0800161 * mempool_destroy - deallocate a memory pool
162 * @pool: pointer to the memory pool which was allocated via
163 * mempool_create().
164 *
165 * Free all reserved elements in @pool and @pool itself. This function
166 * only sleeps if the free_fn() function sleeps.
167 */
168void mempool_destroy(mempool_t *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Sergey Senozhatsky4e3ca3e2015-09-08 15:00:53 -0700170 if (unlikely(!pool))
171 return;
172
Kent Overstreetc1a67fe2015-05-04 16:52:20 -0700173 mempool_exit(pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 kfree(pool);
175}
Tejun Heo0565d312012-01-10 15:08:26 -0800176EXPORT_SYMBOL(mempool_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Kent Overstreetc1a67fe2015-05-04 16:52:20 -0700178int mempool_init_node(mempool_t *pool, int min_nr, mempool_alloc_t *alloc_fn,
179 mempool_free_t *free_fn, void *pool_data,
180 gfp_t gfp_mask, int node_id)
181{
182 spin_lock_init(&pool->lock);
183 pool->min_nr = min_nr;
184 pool->pool_data = pool_data;
185 pool->alloc = alloc_fn;
186 pool->free = free_fn;
187 init_waitqueue_head(&pool->wait);
188
189 pool->elements = kmalloc_array_node(min_nr, sizeof(void *),
190 gfp_mask, node_id);
191 if (!pool->elements)
192 return -ENOMEM;
193
194 /*
195 * First pre-allocate the guaranteed number of buffers.
196 */
197 while (pool->curr_nr < pool->min_nr) {
198 void *element;
199
200 element = pool->alloc(gfp_mask, pool->pool_data);
201 if (unlikely(!element)) {
202 mempool_exit(pool);
203 return -ENOMEM;
204 }
205 add_element(pool, element);
206 }
207
208 return 0;
209}
210EXPORT_SYMBOL(mempool_init_node);
211
212/**
213 * mempool_init - initialize a memory pool
Mike Rapoporta3bf6ce2018-08-21 21:53:03 -0700214 * @pool: pointer to the memory pool that should be initialized
Kent Overstreetc1a67fe2015-05-04 16:52:20 -0700215 * @min_nr: the minimum number of elements guaranteed to be
216 * allocated for this pool.
217 * @alloc_fn: user-defined element-allocation function.
218 * @free_fn: user-defined element-freeing function.
219 * @pool_data: optional private data available to the user-defined functions.
220 *
221 * Like mempool_create(), but initializes the pool in (i.e. embedded in another
222 * structure).
Mike Rapoporta862f682019-03-05 15:48:42 -0800223 *
224 * Return: %0 on success, negative error code otherwise.
Kent Overstreetc1a67fe2015-05-04 16:52:20 -0700225 */
226int mempool_init(mempool_t *pool, int min_nr, mempool_alloc_t *alloc_fn,
227 mempool_free_t *free_fn, void *pool_data)
228{
229 return mempool_init_node(pool, min_nr, alloc_fn, free_fn,
230 pool_data, GFP_KERNEL, NUMA_NO_NODE);
231
232}
233EXPORT_SYMBOL(mempool_init);
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235/**
236 * mempool_create - create a memory pool
237 * @min_nr: the minimum number of elements guaranteed to be
238 * allocated for this pool.
239 * @alloc_fn: user-defined element-allocation function.
240 * @free_fn: user-defined element-freeing function.
241 * @pool_data: optional private data available to the user-defined functions.
242 *
243 * this function creates and allocates a guaranteed size, preallocated
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800244 * memory pool. The pool can be used from the mempool_alloc() and mempool_free()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 * functions. This function might sleep. Both the alloc_fn() and the free_fn()
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800246 * functions might sleep - as long as the mempool_alloc() function is not called
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * from IRQ contexts.
Mike Rapoporta862f682019-03-05 15:48:42 -0800248 *
249 * Return: pointer to the created memory pool object or %NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 */
Christoph Lameter19460892005-06-23 00:08:19 -0700251mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 mempool_free_t *free_fn, void *pool_data)
253{
Tejun Heoa91a5ac2012-06-04 20:40:53 -0700254 return mempool_create_node(min_nr,alloc_fn,free_fn, pool_data,
255 GFP_KERNEL, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -0700256}
257EXPORT_SYMBOL(mempool_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Christoph Lameter19460892005-06-23 00:08:19 -0700259mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
Tejun Heoa91a5ac2012-06-04 20:40:53 -0700260 mempool_free_t *free_fn, void *pool_data,
261 gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700262{
263 mempool_t *pool;
Kent Overstreetc1a67fe2015-05-04 16:52:20 -0700264
Joe Perches7b5219d2013-09-11 14:23:07 -0700265 pool = kzalloc_node(sizeof(*pool), gfp_mask, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!pool)
267 return NULL;
Kent Overstreetc1a67fe2015-05-04 16:52:20 -0700268
269 if (mempool_init_node(pool, min_nr, alloc_fn, free_fn, pool_data,
270 gfp_mask, node_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 kfree(pool);
272 return NULL;
273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return pool;
276}
Christoph Lameter19460892005-06-23 00:08:19 -0700277EXPORT_SYMBOL(mempool_create_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279/**
280 * mempool_resize - resize an existing memory pool
281 * @pool: pointer to the memory pool which was allocated via
282 * mempool_create().
283 * @new_min_nr: the new minimum number of elements guaranteed to be
284 * allocated for this pool.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 *
286 * This function shrinks/grows the pool. In the case of growing,
287 * it cannot be guaranteed that the pool will be grown to the new
288 * size immediately, but new mempool_free() calls will refill it.
David Rientjes11d83362015-04-14 15:48:21 -0700289 * This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 *
291 * Note, the caller must guarantee that no mempool_destroy is called
292 * while this function is running. mempool_alloc() & mempool_free()
293 * might be called (eg. from IRQ contexts) while this function executes.
Mike Rapoporta862f682019-03-05 15:48:42 -0800294 *
295 * Return: %0 on success, negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 */
David Rientjes11d83362015-04-14 15:48:21 -0700297int mempool_resize(mempool_t *pool, int new_min_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 void *element;
300 void **new_elements;
301 unsigned long flags;
302
303 BUG_ON(new_min_nr <= 0);
David Rientjes11d83362015-04-14 15:48:21 -0700304 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 spin_lock_irqsave(&pool->lock, flags);
307 if (new_min_nr <= pool->min_nr) {
308 while (new_min_nr < pool->curr_nr) {
Jia-Ju Bai8cded862018-08-17 15:45:22 -0700309 element = remove_element(pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 spin_unlock_irqrestore(&pool->lock, flags);
311 pool->free(element, pool->pool_data);
312 spin_lock_irqsave(&pool->lock, flags);
313 }
314 pool->min_nr = new_min_nr;
315 goto out_unlock;
316 }
317 spin_unlock_irqrestore(&pool->lock, flags);
318
319 /* Grow the pool */
David Rientjes11d83362015-04-14 15:48:21 -0700320 new_elements = kmalloc_array(new_min_nr, sizeof(*new_elements),
321 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (!new_elements)
323 return -ENOMEM;
324
325 spin_lock_irqsave(&pool->lock, flags);
326 if (unlikely(new_min_nr <= pool->min_nr)) {
327 /* Raced, other resize will do our work */
328 spin_unlock_irqrestore(&pool->lock, flags);
329 kfree(new_elements);
330 goto out;
331 }
332 memcpy(new_elements, pool->elements,
333 pool->curr_nr * sizeof(*new_elements));
334 kfree(pool->elements);
335 pool->elements = new_elements;
336 pool->min_nr = new_min_nr;
337
338 while (pool->curr_nr < pool->min_nr) {
339 spin_unlock_irqrestore(&pool->lock, flags);
David Rientjes11d83362015-04-14 15:48:21 -0700340 element = pool->alloc(GFP_KERNEL, pool->pool_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (!element)
342 goto out;
343 spin_lock_irqsave(&pool->lock, flags);
344 if (pool->curr_nr < pool->min_nr) {
345 add_element(pool, element);
346 } else {
347 spin_unlock_irqrestore(&pool->lock, flags);
348 pool->free(element, pool->pool_data); /* Raced */
349 goto out;
350 }
351 }
352out_unlock:
353 spin_unlock_irqrestore(&pool->lock, flags);
354out:
355 return 0;
356}
357EXPORT_SYMBOL(mempool_resize);
358
359/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * mempool_alloc - allocate an element from a specific memory pool
361 * @pool: pointer to the memory pool which was allocated via
362 * mempool_create().
363 * @gfp_mask: the usual allocation bitmask.
364 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800365 * this function only sleeps if the alloc_fn() function sleeps or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * returns NULL. Note that due to preallocation, this function
367 * *never* fails when called from process contexts. (it might
368 * fail if called from an IRQ context.)
Michal Hocko4e390b22016-07-28 15:48:44 -0700369 * Note: using __GFP_ZERO is not supported.
Mike Rapoporta862f682019-03-05 15:48:42 -0800370 *
371 * Return: pointer to the allocated element or %NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 */
David Rientjesf9054c72016-03-17 14:19:19 -0700373void *mempool_alloc(mempool_t *pool, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 void *element;
376 unsigned long flags;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200377 wait_queue_entry_t wait;
Al Viro6daa0e22005-10-21 03:18:50 -0400378 gfp_t gfp_temp;
Nick Piggin20a77772005-05-01 08:58:37 -0700379
Sebastian Ott8bf8fcb2014-06-04 16:07:00 -0700380 VM_WARN_ON_ONCE(gfp_mask & __GFP_ZERO);
Mel Gormand0164ad2015-11-06 16:28:21 -0800381 might_sleep_if(gfp_mask & __GFP_DIRECT_RECLAIM);
Nick Pigginb84a35b2005-05-01 08:58:36 -0700382
Michal Hocko4e390b22016-07-28 15:48:44 -0700383 gfp_mask |= __GFP_NOMEMALLOC; /* don't allocate emergency reserves */
Nick Pigginb84a35b2005-05-01 08:58:36 -0700384 gfp_mask |= __GFP_NORETRY; /* don't loop in __alloc_pages */
385 gfp_mask |= __GFP_NOWARN; /* failures are OK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Mel Gormand0164ad2015-11-06 16:28:21 -0800387 gfp_temp = gfp_mask & ~(__GFP_DIRECT_RECLAIM|__GFP_IO);
Nick Piggin20a77772005-05-01 08:58:37 -0700388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389repeat_alloc:
Nick Piggin20a77772005-05-01 08:58:37 -0700390
391 element = pool->alloc(gfp_temp, pool->pool_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (likely(element != NULL))
393 return element;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 spin_lock_irqsave(&pool->lock, flags);
396 if (likely(pool->curr_nr)) {
Jia-Ju Bai8cded862018-08-17 15:45:22 -0700397 element = remove_element(pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 spin_unlock_irqrestore(&pool->lock, flags);
Tejun Heo5b990542012-01-10 15:08:23 -0800399 /* paired with rmb in mempool_free(), read comment there */
400 smp_wmb();
Catalin Marinas17411962014-06-06 14:38:19 -0700401 /*
402 * Update the allocation stack trace as this is more useful
403 * for debugging.
404 */
405 kmemleak_update_trace(element);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return element;
407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Tejun Heo1ebb7042012-01-10 15:08:28 -0800409 /*
Mel Gormand0164ad2015-11-06 16:28:21 -0800410 * We use gfp mask w/o direct reclaim or IO for the first round. If
Tejun Heo1ebb7042012-01-10 15:08:28 -0800411 * alloc failed with that and @pool was empty, retry immediately.
412 */
Michal Hocko4e390b22016-07-28 15:48:44 -0700413 if (gfp_temp != gfp_mask) {
Tejun Heo1ebb7042012-01-10 15:08:28 -0800414 spin_unlock_irqrestore(&pool->lock, flags);
415 gfp_temp = gfp_mask;
416 goto repeat_alloc;
417 }
418
Mel Gormand0164ad2015-11-06 16:28:21 -0800419 /* We must not sleep if !__GFP_DIRECT_RECLAIM */
420 if (!(gfp_mask & __GFP_DIRECT_RECLAIM)) {
Tejun Heo5b990542012-01-10 15:08:23 -0800421 spin_unlock_irqrestore(&pool->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return NULL;
Tejun Heo5b990542012-01-10 15:08:23 -0800423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Tejun Heo5b990542012-01-10 15:08:23 -0800425 /* Let's wait for someone else to return an element to @pool */
Benjamin LaHaise01890a42005-06-23 00:10:01 -0700426 init_wait(&wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Tejun Heo5b990542012-01-10 15:08:23 -0800429 spin_unlock_irqrestore(&pool->lock, flags);
430
431 /*
432 * FIXME: this should be io_schedule(). The timeout is there as a
433 * workaround for some DM problems in 2.6.18.
434 */
435 io_schedule_timeout(5*HZ);
436
437 finish_wait(&pool->wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 goto repeat_alloc;
439}
440EXPORT_SYMBOL(mempool_alloc);
441
442/**
443 * mempool_free - return an element to the pool.
444 * @element: pool element pointer.
445 * @pool: pointer to the memory pool which was allocated via
446 * mempool_create().
447 *
448 * this function only sleeps if the free_fn() function sleeps.
449 */
450void mempool_free(void *element, mempool_t *pool)
451{
452 unsigned long flags;
453
Rusty Russellc80e7a82007-07-15 23:42:00 -0700454 if (unlikely(element == NULL))
455 return;
456
Tejun Heo5b990542012-01-10 15:08:23 -0800457 /*
458 * Paired with the wmb in mempool_alloc(). The preceding read is
459 * for @element and the following @pool->curr_nr. This ensures
460 * that the visible value of @pool->curr_nr is from after the
461 * allocation of @element. This is necessary for fringe cases
462 * where @element was passed to this task without going through
463 * barriers.
464 *
465 * For example, assume @p is %NULL at the beginning and one task
466 * performs "p = mempool_alloc(...);" while another task is doing
467 * "while (!p) cpu_relax(); mempool_free(p, ...);". This function
468 * may end up using curr_nr value which is from before allocation
469 * of @p without the following rmb.
470 */
471 smp_rmb();
472
473 /*
474 * For correctness, we need a test which is guaranteed to trigger
475 * if curr_nr + #allocated == min_nr. Testing curr_nr < min_nr
476 * without locking achieves that and refilling as soon as possible
477 * is desirable.
478 *
479 * Because curr_nr visible here is always a value after the
480 * allocation of @element, any task which decremented curr_nr below
481 * min_nr is guaranteed to see curr_nr < min_nr unless curr_nr gets
482 * incremented to min_nr afterwards. If curr_nr gets incremented
483 * to min_nr after the allocation of @element, the elements
484 * allocated after that are subject to the same guarantee.
485 *
486 * Waiters happen iff curr_nr is 0 and the above guarantee also
487 * ensures that there will be frees which return elements to the
488 * pool waking up the waiters.
489 */
Qian Caiabe1de42020-08-14 17:31:44 -0700490 if (unlikely(READ_ONCE(pool->curr_nr) < pool->min_nr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 spin_lock_irqsave(&pool->lock, flags);
Mikulas Patockaeb9a3c62014-04-07 15:37:35 -0700492 if (likely(pool->curr_nr < pool->min_nr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 add_element(pool, element);
494 spin_unlock_irqrestore(&pool->lock, flags);
495 wake_up(&pool->wait);
496 return;
497 }
498 spin_unlock_irqrestore(&pool->lock, flags);
499 }
500 pool->free(element, pool->pool_data);
501}
502EXPORT_SYMBOL(mempool_free);
503
504/*
505 * A commonly used alloc and free fn.
506 */
Al Virodd0fc662005-10-07 07:46:04 +0100507void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Pekka Enbergfcc234f2006-03-22 00:08:13 -0800509 struct kmem_cache *mem = pool_data;
David Rientjese244c9e2015-04-15 16:14:14 -0700510 VM_BUG_ON(mem->ctor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return kmem_cache_alloc(mem, gfp_mask);
512}
513EXPORT_SYMBOL(mempool_alloc_slab);
514
515void mempool_free_slab(void *element, void *pool_data)
516{
Pekka Enbergfcc234f2006-03-22 00:08:13 -0800517 struct kmem_cache *mem = pool_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 kmem_cache_free(mem, element);
519}
520EXPORT_SYMBOL(mempool_free_slab);
Matthew Dobson6e0678f2006-03-26 01:37:44 -0800521
522/*
Matthew Dobson53184082006-03-26 01:37:46 -0800523 * A commonly used alloc and free fn that kmalloc/kfrees the amount of memory
Simon Arlott183ff222007-10-20 01:27:18 +0200524 * specified by pool_data
Matthew Dobson53184082006-03-26 01:37:46 -0800525 */
526void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data)
527{
Figo.zhang5e2f89b2009-08-08 21:01:22 +0800528 size_t size = (size_t)pool_data;
Matthew Dobson53184082006-03-26 01:37:46 -0800529 return kmalloc(size, gfp_mask);
530}
531EXPORT_SYMBOL(mempool_kmalloc);
532
533void mempool_kfree(void *element, void *pool_data)
534{
535 kfree(element);
536}
537EXPORT_SYMBOL(mempool_kfree);
538
539/*
Matthew Dobson6e0678f2006-03-26 01:37:44 -0800540 * A simple mempool-backed page allocator that allocates pages
541 * of the order specified by pool_data.
542 */
543void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data)
544{
545 int order = (int)(long)pool_data;
546 return alloc_pages(gfp_mask, order);
547}
548EXPORT_SYMBOL(mempool_alloc_pages);
549
550void mempool_free_pages(void *element, void *pool_data)
551{
552 int order = (int)(long)pool_data;
553 __free_pages(element, order);
554}
555EXPORT_SYMBOL(mempool_free_pages);