blob: 2884d5bad77ee388a4f7913a0dd0f93d03cd1635 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/mempool.c
3 *
4 * memory buffer pool support. Such pools are mostly used
5 * for guaranteed, deadlock-free memory allocations during
6 * extreme VM load.
7 *
8 * started by Ingo Molnar, Copyright (C) 2001
David Rientjesbdfedb72015-04-15 16:14:17 -07009 * debugging by David Rientjes, Copyright (C) 2015
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/mm.h>
13#include <linux/slab.h>
David Rientjesbdfedb72015-04-15 16:14:17 -070014#include <linux/highmem.h>
Catalin Marinas17411962014-06-06 14:38:19 -070015#include <linux/kmemleak.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040016#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mempool.h>
18#include <linux/blkdev.h>
19#include <linux/writeback.h>
David Rientjese244c9e2015-04-15 16:14:14 -070020#include "slab.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
David Rientjesbdfedb72015-04-15 16:14:17 -070022#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB_DEBUG_ON)
23static void poison_error(mempool_t *pool, void *element, size_t size,
24 size_t byte)
25{
26 const int nr = pool->curr_nr;
27 const int start = max_t(int, byte - (BITS_PER_LONG / 8), 0);
28 const int end = min_t(int, byte + (BITS_PER_LONG / 8), size);
29 int i;
30
31 pr_err("BUG: mempool element poison mismatch\n");
32 pr_err("Mempool %p size %zu\n", pool, size);
33 pr_err(" nr=%d @ %p: %s0x", nr, element, start > 0 ? "... " : "");
34 for (i = start; i < end; i++)
35 pr_cont("%x ", *(u8 *)(element + i));
36 pr_cont("%s\n", end < size ? "..." : "");
37 dump_stack();
38}
39
40static void __check_element(mempool_t *pool, void *element, size_t size)
41{
42 u8 *obj = element;
43 size_t i;
44
45 for (i = 0; i < size; i++) {
46 u8 exp = (i < size - 1) ? POISON_FREE : POISON_END;
47
48 if (obj[i] != exp) {
49 poison_error(pool, element, size, i);
50 return;
51 }
52 }
53 memset(obj, POISON_INUSE, size);
54}
55
56static void check_element(mempool_t *pool, void *element)
57{
58 /* Mempools backed by slab allocator */
59 if (pool->free == mempool_free_slab || pool->free == mempool_kfree)
60 __check_element(pool, element, ksize(element));
61
62 /* Mempools backed by page allocator */
63 if (pool->free == mempool_free_pages) {
64 int order = (int)(long)pool->pool_data;
65 void *addr = kmap_atomic((struct page *)element);
66
67 __check_element(pool, addr, 1UL << (PAGE_SHIFT + order));
68 kunmap_atomic(addr);
69 }
70}
71
72static void __poison_element(void *element, size_t size)
73{
74 u8 *obj = element;
75
76 memset(obj, POISON_FREE, size - 1);
77 obj[size - 1] = POISON_END;
78}
79
80static void poison_element(mempool_t *pool, void *element)
81{
82 /* Mempools backed by slab allocator */
83 if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
84 __poison_element(element, ksize(element));
85
86 /* Mempools backed by page allocator */
87 if (pool->alloc == mempool_alloc_pages) {
88 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static void add_element(mempool_t *pool, void *element)
105{
106 BUG_ON(pool->curr_nr >= pool->min_nr);
David Rientjesbdfedb72015-04-15 16:14:17 -0700107 poison_element(pool, element);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 pool->elements[pool->curr_nr++] = element;
109}
110
111static void *remove_element(mempool_t *pool)
112{
David Rientjesbdfedb72015-04-15 16:14:17 -0700113 void *element = pool->elements[--pool->curr_nr];
114
115 BUG_ON(pool->curr_nr < 0);
116 check_element(pool, element);
117 return element;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Tejun Heo0565d312012-01-10 15:08:26 -0800120/**
121 * mempool_destroy - deallocate a memory pool
122 * @pool: pointer to the memory pool which was allocated via
123 * mempool_create().
124 *
125 * Free all reserved elements in @pool and @pool itself. This function
126 * only sleeps if the free_fn() function sleeps.
127 */
128void mempool_destroy(mempool_t *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 while (pool->curr_nr) {
131 void *element = remove_element(pool);
132 pool->free(element, pool->pool_data);
133 }
134 kfree(pool->elements);
135 kfree(pool);
136}
Tejun Heo0565d312012-01-10 15:08:26 -0800137EXPORT_SYMBOL(mempool_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139/**
140 * mempool_create - create a memory pool
141 * @min_nr: the minimum number of elements guaranteed to be
142 * allocated for this pool.
143 * @alloc_fn: user-defined element-allocation function.
144 * @free_fn: user-defined element-freeing function.
145 * @pool_data: optional private data available to the user-defined functions.
146 *
147 * this function creates and allocates a guaranteed size, preallocated
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800148 * memory pool. The pool can be used from the mempool_alloc() and mempool_free()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 * functions. This function might sleep. Both the alloc_fn() and the free_fn()
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800150 * functions might sleep - as long as the mempool_alloc() function is not called
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * from IRQ contexts.
152 */
Christoph Lameter19460892005-06-23 00:08:19 -0700153mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 mempool_free_t *free_fn, void *pool_data)
155{
Tejun Heoa91a5ac2012-06-04 20:40:53 -0700156 return mempool_create_node(min_nr,alloc_fn,free_fn, pool_data,
157 GFP_KERNEL, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -0700158}
159EXPORT_SYMBOL(mempool_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Christoph Lameter19460892005-06-23 00:08:19 -0700161mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
Tejun Heoa91a5ac2012-06-04 20:40:53 -0700162 mempool_free_t *free_fn, void *pool_data,
163 gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700164{
165 mempool_t *pool;
Joe Perches7b5219d2013-09-11 14:23:07 -0700166 pool = kzalloc_node(sizeof(*pool), gfp_mask, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (!pool)
168 return NULL;
Christoph Lameter19460892005-06-23 00:08:19 -0700169 pool->elements = kmalloc_node(min_nr * sizeof(void *),
Tejun Heoa91a5ac2012-06-04 20:40:53 -0700170 gfp_mask, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (!pool->elements) {
172 kfree(pool);
173 return NULL;
174 }
175 spin_lock_init(&pool->lock);
176 pool->min_nr = min_nr;
177 pool->pool_data = pool_data;
178 init_waitqueue_head(&pool->wait);
179 pool->alloc = alloc_fn;
180 pool->free = free_fn;
181
182 /*
183 * First pre-allocate the guaranteed number of buffers.
184 */
185 while (pool->curr_nr < pool->min_nr) {
186 void *element;
187
Tejun Heoa91a5ac2012-06-04 20:40:53 -0700188 element = pool->alloc(gfp_mask, pool->pool_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (unlikely(!element)) {
Tejun Heo0565d312012-01-10 15:08:26 -0800190 mempool_destroy(pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return NULL;
192 }
193 add_element(pool, element);
194 }
195 return pool;
196}
Christoph Lameter19460892005-06-23 00:08:19 -0700197EXPORT_SYMBOL(mempool_create_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199/**
200 * mempool_resize - resize an existing memory pool
201 * @pool: pointer to the memory pool which was allocated via
202 * mempool_create().
203 * @new_min_nr: the new minimum number of elements guaranteed to be
204 * allocated for this pool.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 *
206 * This function shrinks/grows the pool. In the case of growing,
207 * it cannot be guaranteed that the pool will be grown to the new
208 * size immediately, but new mempool_free() calls will refill it.
David Rientjes11d83362015-04-14 15:48:21 -0700209 * This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 *
211 * Note, the caller must guarantee that no mempool_destroy is called
212 * while this function is running. mempool_alloc() & mempool_free()
213 * might be called (eg. from IRQ contexts) while this function executes.
214 */
David Rientjes11d83362015-04-14 15:48:21 -0700215int mempool_resize(mempool_t *pool, int new_min_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 void *element;
218 void **new_elements;
219 unsigned long flags;
220
221 BUG_ON(new_min_nr <= 0);
David Rientjes11d83362015-04-14 15:48:21 -0700222 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 spin_lock_irqsave(&pool->lock, flags);
225 if (new_min_nr <= pool->min_nr) {
226 while (new_min_nr < pool->curr_nr) {
227 element = remove_element(pool);
228 spin_unlock_irqrestore(&pool->lock, flags);
229 pool->free(element, pool->pool_data);
230 spin_lock_irqsave(&pool->lock, flags);
231 }
232 pool->min_nr = new_min_nr;
233 goto out_unlock;
234 }
235 spin_unlock_irqrestore(&pool->lock, flags);
236
237 /* Grow the pool */
David Rientjes11d83362015-04-14 15:48:21 -0700238 new_elements = kmalloc_array(new_min_nr, sizeof(*new_elements),
239 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (!new_elements)
241 return -ENOMEM;
242
243 spin_lock_irqsave(&pool->lock, flags);
244 if (unlikely(new_min_nr <= pool->min_nr)) {
245 /* Raced, other resize will do our work */
246 spin_unlock_irqrestore(&pool->lock, flags);
247 kfree(new_elements);
248 goto out;
249 }
250 memcpy(new_elements, pool->elements,
251 pool->curr_nr * sizeof(*new_elements));
252 kfree(pool->elements);
253 pool->elements = new_elements;
254 pool->min_nr = new_min_nr;
255
256 while (pool->curr_nr < pool->min_nr) {
257 spin_unlock_irqrestore(&pool->lock, flags);
David Rientjes11d83362015-04-14 15:48:21 -0700258 element = pool->alloc(GFP_KERNEL, pool->pool_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (!element)
260 goto out;
261 spin_lock_irqsave(&pool->lock, flags);
262 if (pool->curr_nr < pool->min_nr) {
263 add_element(pool, element);
264 } else {
265 spin_unlock_irqrestore(&pool->lock, flags);
266 pool->free(element, pool->pool_data); /* Raced */
267 goto out;
268 }
269 }
270out_unlock:
271 spin_unlock_irqrestore(&pool->lock, flags);
272out:
273 return 0;
274}
275EXPORT_SYMBOL(mempool_resize);
276
277/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * mempool_alloc - allocate an element from a specific memory pool
279 * @pool: pointer to the memory pool which was allocated via
280 * mempool_create().
281 * @gfp_mask: the usual allocation bitmask.
282 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800283 * this function only sleeps if the alloc_fn() function sleeps or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * returns NULL. Note that due to preallocation, this function
285 * *never* fails when called from process contexts. (it might
286 * fail if called from an IRQ context.)
Sebastian Ott8bf8fcb2014-06-04 16:07:00 -0700287 * Note: using __GFP_ZERO is not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
Al Virodd0fc662005-10-07 07:46:04 +0100289void * mempool_alloc(mempool_t *pool, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 void *element;
292 unsigned long flags;
Benjamin LaHaise01890a42005-06-23 00:10:01 -0700293 wait_queue_t wait;
Al Viro6daa0e22005-10-21 03:18:50 -0400294 gfp_t gfp_temp;
Nick Piggin20a77772005-05-01 08:58:37 -0700295
Sebastian Ott8bf8fcb2014-06-04 16:07:00 -0700296 VM_WARN_ON_ONCE(gfp_mask & __GFP_ZERO);
Nick Piggin20a77772005-05-01 08:58:37 -0700297 might_sleep_if(gfp_mask & __GFP_WAIT);
Nick Pigginb84a35b2005-05-01 08:58:36 -0700298
299 gfp_mask |= __GFP_NOMEMALLOC; /* don't allocate emergency reserves */
300 gfp_mask |= __GFP_NORETRY; /* don't loop in __alloc_pages */
301 gfp_mask |= __GFP_NOWARN; /* failures are OK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Nick Piggin20a77772005-05-01 08:58:37 -0700303 gfp_temp = gfp_mask & ~(__GFP_WAIT|__GFP_IO);
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305repeat_alloc:
Nick Piggin20a77772005-05-01 08:58:37 -0700306
307 element = pool->alloc(gfp_temp, pool->pool_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (likely(element != NULL))
309 return element;
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 spin_lock_irqsave(&pool->lock, flags);
312 if (likely(pool->curr_nr)) {
313 element = remove_element(pool);
314 spin_unlock_irqrestore(&pool->lock, flags);
Tejun Heo5b990542012-01-10 15:08:23 -0800315 /* paired with rmb in mempool_free(), read comment there */
316 smp_wmb();
Catalin Marinas17411962014-06-06 14:38:19 -0700317 /*
318 * Update the allocation stack trace as this is more useful
319 * for debugging.
320 */
321 kmemleak_update_trace(element);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return element;
323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Tejun Heo1ebb7042012-01-10 15:08:28 -0800325 /*
326 * We use gfp mask w/o __GFP_WAIT or IO for the first round. If
327 * alloc failed with that and @pool was empty, retry immediately.
328 */
329 if (gfp_temp != gfp_mask) {
330 spin_unlock_irqrestore(&pool->lock, flags);
331 gfp_temp = gfp_mask;
332 goto repeat_alloc;
333 }
334
335 /* We must not sleep if !__GFP_WAIT */
Tejun Heo5b990542012-01-10 15:08:23 -0800336 if (!(gfp_mask & __GFP_WAIT)) {
337 spin_unlock_irqrestore(&pool->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return NULL;
Tejun Heo5b990542012-01-10 15:08:23 -0800339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Tejun Heo5b990542012-01-10 15:08:23 -0800341 /* Let's wait for someone else to return an element to @pool */
Benjamin LaHaise01890a42005-06-23 00:10:01 -0700342 init_wait(&wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Tejun Heo5b990542012-01-10 15:08:23 -0800345 spin_unlock_irqrestore(&pool->lock, flags);
346
347 /*
348 * FIXME: this should be io_schedule(). The timeout is there as a
349 * workaround for some DM problems in 2.6.18.
350 */
351 io_schedule_timeout(5*HZ);
352
353 finish_wait(&pool->wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 goto repeat_alloc;
355}
356EXPORT_SYMBOL(mempool_alloc);
357
358/**
359 * mempool_free - return an element to the pool.
360 * @element: pool element pointer.
361 * @pool: pointer to the memory pool which was allocated via
362 * mempool_create().
363 *
364 * this function only sleeps if the free_fn() function sleeps.
365 */
366void mempool_free(void *element, mempool_t *pool)
367{
368 unsigned long flags;
369
Rusty Russellc80e7a82007-07-15 23:42:00 -0700370 if (unlikely(element == NULL))
371 return;
372
Tejun Heo5b990542012-01-10 15:08:23 -0800373 /*
374 * Paired with the wmb in mempool_alloc(). The preceding read is
375 * for @element and the following @pool->curr_nr. This ensures
376 * that the visible value of @pool->curr_nr is from after the
377 * allocation of @element. This is necessary for fringe cases
378 * where @element was passed to this task without going through
379 * barriers.
380 *
381 * For example, assume @p is %NULL at the beginning and one task
382 * performs "p = mempool_alloc(...);" while another task is doing
383 * "while (!p) cpu_relax(); mempool_free(p, ...);". This function
384 * may end up using curr_nr value which is from before allocation
385 * of @p without the following rmb.
386 */
387 smp_rmb();
388
389 /*
390 * For correctness, we need a test which is guaranteed to trigger
391 * if curr_nr + #allocated == min_nr. Testing curr_nr < min_nr
392 * without locking achieves that and refilling as soon as possible
393 * is desirable.
394 *
395 * Because curr_nr visible here is always a value after the
396 * allocation of @element, any task which decremented curr_nr below
397 * min_nr is guaranteed to see curr_nr < min_nr unless curr_nr gets
398 * incremented to min_nr afterwards. If curr_nr gets incremented
399 * to min_nr after the allocation of @element, the elements
400 * allocated after that are subject to the same guarantee.
401 *
402 * Waiters happen iff curr_nr is 0 and the above guarantee also
403 * ensures that there will be frees which return elements to the
404 * pool waking up the waiters.
405 */
Mikulas Patockaeb9a3c62014-04-07 15:37:35 -0700406 if (unlikely(pool->curr_nr < pool->min_nr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 spin_lock_irqsave(&pool->lock, flags);
Mikulas Patockaeb9a3c62014-04-07 15:37:35 -0700408 if (likely(pool->curr_nr < pool->min_nr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 add_element(pool, element);
410 spin_unlock_irqrestore(&pool->lock, flags);
411 wake_up(&pool->wait);
412 return;
413 }
414 spin_unlock_irqrestore(&pool->lock, flags);
415 }
416 pool->free(element, pool->pool_data);
417}
418EXPORT_SYMBOL(mempool_free);
419
420/*
421 * A commonly used alloc and free fn.
422 */
Al Virodd0fc662005-10-07 07:46:04 +0100423void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Pekka Enbergfcc234f2006-03-22 00:08:13 -0800425 struct kmem_cache *mem = pool_data;
David Rientjese244c9e2015-04-15 16:14:14 -0700426 VM_BUG_ON(mem->ctor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return kmem_cache_alloc(mem, gfp_mask);
428}
429EXPORT_SYMBOL(mempool_alloc_slab);
430
431void mempool_free_slab(void *element, void *pool_data)
432{
Pekka Enbergfcc234f2006-03-22 00:08:13 -0800433 struct kmem_cache *mem = pool_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 kmem_cache_free(mem, element);
435}
436EXPORT_SYMBOL(mempool_free_slab);
Matthew Dobson6e0678f2006-03-26 01:37:44 -0800437
438/*
Matthew Dobson53184082006-03-26 01:37:46 -0800439 * A commonly used alloc and free fn that kmalloc/kfrees the amount of memory
Simon Arlott183ff222007-10-20 01:27:18 +0200440 * specified by pool_data
Matthew Dobson53184082006-03-26 01:37:46 -0800441 */
442void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data)
443{
Figo.zhang5e2f89b2009-08-08 21:01:22 +0800444 size_t size = (size_t)pool_data;
Matthew Dobson53184082006-03-26 01:37:46 -0800445 return kmalloc(size, gfp_mask);
446}
447EXPORT_SYMBOL(mempool_kmalloc);
448
449void mempool_kfree(void *element, void *pool_data)
450{
451 kfree(element);
452}
453EXPORT_SYMBOL(mempool_kfree);
454
455/*
Matthew Dobson6e0678f2006-03-26 01:37:44 -0800456 * A simple mempool-backed page allocator that allocates pages
457 * of the order specified by pool_data.
458 */
459void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data)
460{
461 int order = (int)(long)pool_data;
462 return alloc_pages(gfp_mask, order);
463}
464EXPORT_SYMBOL(mempool_alloc_pages);
465
466void mempool_free_pages(void *element, void *pool_data)
467{
468 int order = (int)(long)pool_data;
469 __free_pages(element, order);
470}
471EXPORT_SYMBOL(mempool_free_pages);