blob: b60fb85526ed62937e4545262b2cbb0b9894315f [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
9 */
10
11#include <linux/mm.h>
12#include <linux/slab.h>
Catalin Marinas17411962014-06-06 14:38:19 -070013#include <linux/kmemleak.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/mempool.h>
16#include <linux/blkdev.h>
17#include <linux/writeback.h>
David Rientjese244c9e2015-04-15 16:14:14 -070018#include "slab.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20static void add_element(mempool_t *pool, void *element)
21{
22 BUG_ON(pool->curr_nr >= pool->min_nr);
23 pool->elements[pool->curr_nr++] = element;
24}
25
26static void *remove_element(mempool_t *pool)
27{
28 BUG_ON(pool->curr_nr <= 0);
29 return pool->elements[--pool->curr_nr];
30}
31
Tejun Heo0565d312012-01-10 15:08:26 -080032/**
33 * mempool_destroy - deallocate a memory pool
34 * @pool: pointer to the memory pool which was allocated via
35 * mempool_create().
36 *
37 * Free all reserved elements in @pool and @pool itself. This function
38 * only sleeps if the free_fn() function sleeps.
39 */
40void mempool_destroy(mempool_t *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 while (pool->curr_nr) {
43 void *element = remove_element(pool);
44 pool->free(element, pool->pool_data);
45 }
46 kfree(pool->elements);
47 kfree(pool);
48}
Tejun Heo0565d312012-01-10 15:08:26 -080049EXPORT_SYMBOL(mempool_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/**
52 * mempool_create - create a memory pool
53 * @min_nr: the minimum number of elements guaranteed to be
54 * allocated for this pool.
55 * @alloc_fn: user-defined element-allocation function.
56 * @free_fn: user-defined element-freeing function.
57 * @pool_data: optional private data available to the user-defined functions.
58 *
59 * this function creates and allocates a guaranteed size, preallocated
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080060 * memory pool. The pool can be used from the mempool_alloc() and mempool_free()
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * functions. This function might sleep. Both the alloc_fn() and the free_fn()
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080062 * functions might sleep - as long as the mempool_alloc() function is not called
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * from IRQ contexts.
64 */
Christoph Lameter19460892005-06-23 00:08:19 -070065mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 mempool_free_t *free_fn, void *pool_data)
67{
Tejun Heoa91a5ac2012-06-04 20:40:53 -070068 return mempool_create_node(min_nr,alloc_fn,free_fn, pool_data,
69 GFP_KERNEL, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -070070}
71EXPORT_SYMBOL(mempool_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Christoph Lameter19460892005-06-23 00:08:19 -070073mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
Tejun Heoa91a5ac2012-06-04 20:40:53 -070074 mempool_free_t *free_fn, void *pool_data,
75 gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -070076{
77 mempool_t *pool;
Joe Perches7b5219d2013-09-11 14:23:07 -070078 pool = kzalloc_node(sizeof(*pool), gfp_mask, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (!pool)
80 return NULL;
Christoph Lameter19460892005-06-23 00:08:19 -070081 pool->elements = kmalloc_node(min_nr * sizeof(void *),
Tejun Heoa91a5ac2012-06-04 20:40:53 -070082 gfp_mask, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (!pool->elements) {
84 kfree(pool);
85 return NULL;
86 }
87 spin_lock_init(&pool->lock);
88 pool->min_nr = min_nr;
89 pool->pool_data = pool_data;
90 init_waitqueue_head(&pool->wait);
91 pool->alloc = alloc_fn;
92 pool->free = free_fn;
93
94 /*
95 * First pre-allocate the guaranteed number of buffers.
96 */
97 while (pool->curr_nr < pool->min_nr) {
98 void *element;
99
Tejun Heoa91a5ac2012-06-04 20:40:53 -0700100 element = pool->alloc(gfp_mask, pool->pool_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (unlikely(!element)) {
Tejun Heo0565d312012-01-10 15:08:26 -0800102 mempool_destroy(pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return NULL;
104 }
105 add_element(pool, element);
106 }
107 return pool;
108}
Christoph Lameter19460892005-06-23 00:08:19 -0700109EXPORT_SYMBOL(mempool_create_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/**
112 * mempool_resize - resize an existing memory pool
113 * @pool: pointer to the memory pool which was allocated via
114 * mempool_create().
115 * @new_min_nr: the new minimum number of elements guaranteed to be
116 * allocated for this pool.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 *
118 * This function shrinks/grows the pool. In the case of growing,
119 * it cannot be guaranteed that the pool will be grown to the new
120 * size immediately, but new mempool_free() calls will refill it.
David Rientjes11d83362015-04-14 15:48:21 -0700121 * This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 *
123 * Note, the caller must guarantee that no mempool_destroy is called
124 * while this function is running. mempool_alloc() & mempool_free()
125 * might be called (eg. from IRQ contexts) while this function executes.
126 */
David Rientjes11d83362015-04-14 15:48:21 -0700127int mempool_resize(mempool_t *pool, int new_min_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 void *element;
130 void **new_elements;
131 unsigned long flags;
132
133 BUG_ON(new_min_nr <= 0);
David Rientjes11d83362015-04-14 15:48:21 -0700134 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 spin_lock_irqsave(&pool->lock, flags);
137 if (new_min_nr <= pool->min_nr) {
138 while (new_min_nr < pool->curr_nr) {
139 element = remove_element(pool);
140 spin_unlock_irqrestore(&pool->lock, flags);
141 pool->free(element, pool->pool_data);
142 spin_lock_irqsave(&pool->lock, flags);
143 }
144 pool->min_nr = new_min_nr;
145 goto out_unlock;
146 }
147 spin_unlock_irqrestore(&pool->lock, flags);
148
149 /* Grow the pool */
David Rientjes11d83362015-04-14 15:48:21 -0700150 new_elements = kmalloc_array(new_min_nr, sizeof(*new_elements),
151 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (!new_elements)
153 return -ENOMEM;
154
155 spin_lock_irqsave(&pool->lock, flags);
156 if (unlikely(new_min_nr <= pool->min_nr)) {
157 /* Raced, other resize will do our work */
158 spin_unlock_irqrestore(&pool->lock, flags);
159 kfree(new_elements);
160 goto out;
161 }
162 memcpy(new_elements, pool->elements,
163 pool->curr_nr * sizeof(*new_elements));
164 kfree(pool->elements);
165 pool->elements = new_elements;
166 pool->min_nr = new_min_nr;
167
168 while (pool->curr_nr < pool->min_nr) {
169 spin_unlock_irqrestore(&pool->lock, flags);
David Rientjes11d83362015-04-14 15:48:21 -0700170 element = pool->alloc(GFP_KERNEL, pool->pool_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (!element)
172 goto out;
173 spin_lock_irqsave(&pool->lock, flags);
174 if (pool->curr_nr < pool->min_nr) {
175 add_element(pool, element);
176 } else {
177 spin_unlock_irqrestore(&pool->lock, flags);
178 pool->free(element, pool->pool_data); /* Raced */
179 goto out;
180 }
181 }
182out_unlock:
183 spin_unlock_irqrestore(&pool->lock, flags);
184out:
185 return 0;
186}
187EXPORT_SYMBOL(mempool_resize);
188
189/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * mempool_alloc - allocate an element from a specific memory pool
191 * @pool: pointer to the memory pool which was allocated via
192 * mempool_create().
193 * @gfp_mask: the usual allocation bitmask.
194 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800195 * this function only sleeps if the alloc_fn() function sleeps or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * returns NULL. Note that due to preallocation, this function
197 * *never* fails when called from process contexts. (it might
198 * fail if called from an IRQ context.)
Sebastian Ott8bf8fcb2014-06-04 16:07:00 -0700199 * Note: using __GFP_ZERO is not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 */
Al Virodd0fc662005-10-07 07:46:04 +0100201void * mempool_alloc(mempool_t *pool, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 void *element;
204 unsigned long flags;
Benjamin LaHaise01890a42005-06-23 00:10:01 -0700205 wait_queue_t wait;
Al Viro6daa0e22005-10-21 03:18:50 -0400206 gfp_t gfp_temp;
Nick Piggin20a77772005-05-01 08:58:37 -0700207
Sebastian Ott8bf8fcb2014-06-04 16:07:00 -0700208 VM_WARN_ON_ONCE(gfp_mask & __GFP_ZERO);
Nick Piggin20a77772005-05-01 08:58:37 -0700209 might_sleep_if(gfp_mask & __GFP_WAIT);
Nick Pigginb84a35b2005-05-01 08:58:36 -0700210
211 gfp_mask |= __GFP_NOMEMALLOC; /* don't allocate emergency reserves */
212 gfp_mask |= __GFP_NORETRY; /* don't loop in __alloc_pages */
213 gfp_mask |= __GFP_NOWARN; /* failures are OK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Nick Piggin20a77772005-05-01 08:58:37 -0700215 gfp_temp = gfp_mask & ~(__GFP_WAIT|__GFP_IO);
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217repeat_alloc:
Nick Piggin20a77772005-05-01 08:58:37 -0700218
219 element = pool->alloc(gfp_temp, pool->pool_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (likely(element != NULL))
221 return element;
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 spin_lock_irqsave(&pool->lock, flags);
224 if (likely(pool->curr_nr)) {
225 element = remove_element(pool);
226 spin_unlock_irqrestore(&pool->lock, flags);
Tejun Heo5b990542012-01-10 15:08:23 -0800227 /* paired with rmb in mempool_free(), read comment there */
228 smp_wmb();
Catalin Marinas17411962014-06-06 14:38:19 -0700229 /*
230 * Update the allocation stack trace as this is more useful
231 * for debugging.
232 */
233 kmemleak_update_trace(element);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return element;
235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Tejun Heo1ebb7042012-01-10 15:08:28 -0800237 /*
238 * We use gfp mask w/o __GFP_WAIT or IO for the first round. If
239 * alloc failed with that and @pool was empty, retry immediately.
240 */
241 if (gfp_temp != gfp_mask) {
242 spin_unlock_irqrestore(&pool->lock, flags);
243 gfp_temp = gfp_mask;
244 goto repeat_alloc;
245 }
246
247 /* We must not sleep if !__GFP_WAIT */
Tejun Heo5b990542012-01-10 15:08:23 -0800248 if (!(gfp_mask & __GFP_WAIT)) {
249 spin_unlock_irqrestore(&pool->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return NULL;
Tejun Heo5b990542012-01-10 15:08:23 -0800251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Tejun Heo5b990542012-01-10 15:08:23 -0800253 /* Let's wait for someone else to return an element to @pool */
Benjamin LaHaise01890a42005-06-23 00:10:01 -0700254 init_wait(&wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Tejun Heo5b990542012-01-10 15:08:23 -0800257 spin_unlock_irqrestore(&pool->lock, flags);
258
259 /*
260 * FIXME: this should be io_schedule(). The timeout is there as a
261 * workaround for some DM problems in 2.6.18.
262 */
263 io_schedule_timeout(5*HZ);
264
265 finish_wait(&pool->wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 goto repeat_alloc;
267}
268EXPORT_SYMBOL(mempool_alloc);
269
270/**
271 * mempool_free - return an element to the pool.
272 * @element: pool element pointer.
273 * @pool: pointer to the memory pool which was allocated via
274 * mempool_create().
275 *
276 * this function only sleeps if the free_fn() function sleeps.
277 */
278void mempool_free(void *element, mempool_t *pool)
279{
280 unsigned long flags;
281
Rusty Russellc80e7a82007-07-15 23:42:00 -0700282 if (unlikely(element == NULL))
283 return;
284
Tejun Heo5b990542012-01-10 15:08:23 -0800285 /*
286 * Paired with the wmb in mempool_alloc(). The preceding read is
287 * for @element and the following @pool->curr_nr. This ensures
288 * that the visible value of @pool->curr_nr is from after the
289 * allocation of @element. This is necessary for fringe cases
290 * where @element was passed to this task without going through
291 * barriers.
292 *
293 * For example, assume @p is %NULL at the beginning and one task
294 * performs "p = mempool_alloc(...);" while another task is doing
295 * "while (!p) cpu_relax(); mempool_free(p, ...);". This function
296 * may end up using curr_nr value which is from before allocation
297 * of @p without the following rmb.
298 */
299 smp_rmb();
300
301 /*
302 * For correctness, we need a test which is guaranteed to trigger
303 * if curr_nr + #allocated == min_nr. Testing curr_nr < min_nr
304 * without locking achieves that and refilling as soon as possible
305 * is desirable.
306 *
307 * Because curr_nr visible here is always a value after the
308 * allocation of @element, any task which decremented curr_nr below
309 * min_nr is guaranteed to see curr_nr < min_nr unless curr_nr gets
310 * incremented to min_nr afterwards. If curr_nr gets incremented
311 * to min_nr after the allocation of @element, the elements
312 * allocated after that are subject to the same guarantee.
313 *
314 * Waiters happen iff curr_nr is 0 and the above guarantee also
315 * ensures that there will be frees which return elements to the
316 * pool waking up the waiters.
317 */
Mikulas Patockaeb9a3c62014-04-07 15:37:35 -0700318 if (unlikely(pool->curr_nr < pool->min_nr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 spin_lock_irqsave(&pool->lock, flags);
Mikulas Patockaeb9a3c62014-04-07 15:37:35 -0700320 if (likely(pool->curr_nr < pool->min_nr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 add_element(pool, element);
322 spin_unlock_irqrestore(&pool->lock, flags);
323 wake_up(&pool->wait);
324 return;
325 }
326 spin_unlock_irqrestore(&pool->lock, flags);
327 }
328 pool->free(element, pool->pool_data);
329}
330EXPORT_SYMBOL(mempool_free);
331
332/*
333 * A commonly used alloc and free fn.
334 */
Al Virodd0fc662005-10-07 07:46:04 +0100335void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Pekka Enbergfcc234f2006-03-22 00:08:13 -0800337 struct kmem_cache *mem = pool_data;
David Rientjese244c9e2015-04-15 16:14:14 -0700338 VM_BUG_ON(mem->ctor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return kmem_cache_alloc(mem, gfp_mask);
340}
341EXPORT_SYMBOL(mempool_alloc_slab);
342
343void mempool_free_slab(void *element, void *pool_data)
344{
Pekka Enbergfcc234f2006-03-22 00:08:13 -0800345 struct kmem_cache *mem = pool_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 kmem_cache_free(mem, element);
347}
348EXPORT_SYMBOL(mempool_free_slab);
Matthew Dobson6e0678f2006-03-26 01:37:44 -0800349
350/*
Matthew Dobson53184082006-03-26 01:37:46 -0800351 * A commonly used alloc and free fn that kmalloc/kfrees the amount of memory
Simon Arlott183ff222007-10-20 01:27:18 +0200352 * specified by pool_data
Matthew Dobson53184082006-03-26 01:37:46 -0800353 */
354void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data)
355{
Figo.zhang5e2f89b2009-08-08 21:01:22 +0800356 size_t size = (size_t)pool_data;
Matthew Dobson53184082006-03-26 01:37:46 -0800357 return kmalloc(size, gfp_mask);
358}
359EXPORT_SYMBOL(mempool_kmalloc);
360
361void mempool_kfree(void *element, void *pool_data)
362{
363 kfree(element);
364}
365EXPORT_SYMBOL(mempool_kfree);
366
367/*
Matthew Dobson6e0678f2006-03-26 01:37:44 -0800368 * A simple mempool-backed page allocator that allocates pages
369 * of the order specified by pool_data.
370 */
371void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data)
372{
373 int order = (int)(long)pool_data;
374 return alloc_pages(gfp_mask, order);
375}
376EXPORT_SYMBOL(mempool_alloc_pages);
377
378void mempool_free_pages(void *element, void *pool_data)
379{
380 int order = (int)(long)pool_data;
381 __free_pages(element, order);
382}
383EXPORT_SYMBOL(mempool_free_pages);