blob: 949970db28741532172ad2c7a65fdeb71c3f7404 [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>
18
19static void add_element(mempool_t *pool, void *element)
20{
21 BUG_ON(pool->curr_nr >= pool->min_nr);
22 pool->elements[pool->curr_nr++] = element;
23}
24
25static void *remove_element(mempool_t *pool)
26{
27 BUG_ON(pool->curr_nr <= 0);
28 return pool->elements[--pool->curr_nr];
29}
30
Tejun Heo0565d312012-01-10 15:08:26 -080031/**
32 * mempool_destroy - deallocate a memory pool
33 * @pool: pointer to the memory pool which was allocated via
34 * mempool_create().
35 *
36 * Free all reserved elements in @pool and @pool itself. This function
37 * only sleeps if the free_fn() function sleeps.
38 */
39void mempool_destroy(mempool_t *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 while (pool->curr_nr) {
42 void *element = remove_element(pool);
43 pool->free(element, pool->pool_data);
44 }
45 kfree(pool->elements);
46 kfree(pool);
47}
Tejun Heo0565d312012-01-10 15:08:26 -080048EXPORT_SYMBOL(mempool_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/**
51 * mempool_create - create a memory pool
52 * @min_nr: the minimum number of elements guaranteed to be
53 * allocated for this pool.
54 * @alloc_fn: user-defined element-allocation function.
55 * @free_fn: user-defined element-freeing function.
56 * @pool_data: optional private data available to the user-defined functions.
57 *
58 * this function creates and allocates a guaranteed size, preallocated
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080059 * memory pool. The pool can be used from the mempool_alloc() and mempool_free()
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * functions. This function might sleep. Both the alloc_fn() and the free_fn()
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080061 * functions might sleep - as long as the mempool_alloc() function is not called
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * from IRQ contexts.
63 */
Christoph Lameter19460892005-06-23 00:08:19 -070064mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 mempool_free_t *free_fn, void *pool_data)
66{
Tejun Heoa91a5ac2012-06-04 20:40:53 -070067 return mempool_create_node(min_nr,alloc_fn,free_fn, pool_data,
68 GFP_KERNEL, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -070069}
70EXPORT_SYMBOL(mempool_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Christoph Lameter19460892005-06-23 00:08:19 -070072mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
Tejun Heoa91a5ac2012-06-04 20:40:53 -070073 mempool_free_t *free_fn, void *pool_data,
74 gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -070075{
76 mempool_t *pool;
Joe Perches7b5219d2013-09-11 14:23:07 -070077 pool = kzalloc_node(sizeof(*pool), gfp_mask, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (!pool)
79 return NULL;
Christoph Lameter19460892005-06-23 00:08:19 -070080 pool->elements = kmalloc_node(min_nr * sizeof(void *),
Tejun Heoa91a5ac2012-06-04 20:40:53 -070081 gfp_mask, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (!pool->elements) {
83 kfree(pool);
84 return NULL;
85 }
86 spin_lock_init(&pool->lock);
87 pool->min_nr = min_nr;
88 pool->pool_data = pool_data;
89 init_waitqueue_head(&pool->wait);
90 pool->alloc = alloc_fn;
91 pool->free = free_fn;
92
93 /*
94 * First pre-allocate the guaranteed number of buffers.
95 */
96 while (pool->curr_nr < pool->min_nr) {
97 void *element;
98
Tejun Heoa91a5ac2012-06-04 20:40:53 -070099 element = pool->alloc(gfp_mask, pool->pool_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (unlikely(!element)) {
Tejun Heo0565d312012-01-10 15:08:26 -0800101 mempool_destroy(pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return NULL;
103 }
104 add_element(pool, element);
105 }
106 return pool;
107}
Christoph Lameter19460892005-06-23 00:08:19 -0700108EXPORT_SYMBOL(mempool_create_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110/**
111 * mempool_resize - resize an existing memory pool
112 * @pool: pointer to the memory pool which was allocated via
113 * mempool_create().
114 * @new_min_nr: the new minimum number of elements guaranteed to be
115 * allocated for this pool.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 *
117 * This function shrinks/grows the pool. In the case of growing,
118 * it cannot be guaranteed that the pool will be grown to the new
119 * size immediately, but new mempool_free() calls will refill it.
David Rientjes11d83362015-04-14 15:48:21 -0700120 * This function may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 *
122 * Note, the caller must guarantee that no mempool_destroy is called
123 * while this function is running. mempool_alloc() & mempool_free()
124 * might be called (eg. from IRQ contexts) while this function executes.
125 */
David Rientjes11d83362015-04-14 15:48:21 -0700126int mempool_resize(mempool_t *pool, int new_min_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 void *element;
129 void **new_elements;
130 unsigned long flags;
131
132 BUG_ON(new_min_nr <= 0);
David Rientjes11d83362015-04-14 15:48:21 -0700133 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 spin_lock_irqsave(&pool->lock, flags);
136 if (new_min_nr <= pool->min_nr) {
137 while (new_min_nr < pool->curr_nr) {
138 element = remove_element(pool);
139 spin_unlock_irqrestore(&pool->lock, flags);
140 pool->free(element, pool->pool_data);
141 spin_lock_irqsave(&pool->lock, flags);
142 }
143 pool->min_nr = new_min_nr;
144 goto out_unlock;
145 }
146 spin_unlock_irqrestore(&pool->lock, flags);
147
148 /* Grow the pool */
David Rientjes11d83362015-04-14 15:48:21 -0700149 new_elements = kmalloc_array(new_min_nr, sizeof(*new_elements),
150 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (!new_elements)
152 return -ENOMEM;
153
154 spin_lock_irqsave(&pool->lock, flags);
155 if (unlikely(new_min_nr <= pool->min_nr)) {
156 /* Raced, other resize will do our work */
157 spin_unlock_irqrestore(&pool->lock, flags);
158 kfree(new_elements);
159 goto out;
160 }
161 memcpy(new_elements, pool->elements,
162 pool->curr_nr * sizeof(*new_elements));
163 kfree(pool->elements);
164 pool->elements = new_elements;
165 pool->min_nr = new_min_nr;
166
167 while (pool->curr_nr < pool->min_nr) {
168 spin_unlock_irqrestore(&pool->lock, flags);
David Rientjes11d83362015-04-14 15:48:21 -0700169 element = pool->alloc(GFP_KERNEL, pool->pool_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (!element)
171 goto out;
172 spin_lock_irqsave(&pool->lock, flags);
173 if (pool->curr_nr < pool->min_nr) {
174 add_element(pool, element);
175 } else {
176 spin_unlock_irqrestore(&pool->lock, flags);
177 pool->free(element, pool->pool_data); /* Raced */
178 goto out;
179 }
180 }
181out_unlock:
182 spin_unlock_irqrestore(&pool->lock, flags);
183out:
184 return 0;
185}
186EXPORT_SYMBOL(mempool_resize);
187
188/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * mempool_alloc - allocate an element from a specific memory pool
190 * @pool: pointer to the memory pool which was allocated via
191 * mempool_create().
192 * @gfp_mask: the usual allocation bitmask.
193 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800194 * this function only sleeps if the alloc_fn() function sleeps or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * returns NULL. Note that due to preallocation, this function
196 * *never* fails when called from process contexts. (it might
197 * fail if called from an IRQ context.)
Sebastian Ott8bf8fcb2014-06-04 16:07:00 -0700198 * Note: using __GFP_ZERO is not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
Al Virodd0fc662005-10-07 07:46:04 +0100200void * mempool_alloc(mempool_t *pool, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 void *element;
203 unsigned long flags;
Benjamin LaHaise01890a42005-06-23 00:10:01 -0700204 wait_queue_t wait;
Al Viro6daa0e22005-10-21 03:18:50 -0400205 gfp_t gfp_temp;
Nick Piggin20a77772005-05-01 08:58:37 -0700206
Sebastian Ott8bf8fcb2014-06-04 16:07:00 -0700207 VM_WARN_ON_ONCE(gfp_mask & __GFP_ZERO);
Nick Piggin20a77772005-05-01 08:58:37 -0700208 might_sleep_if(gfp_mask & __GFP_WAIT);
Nick Pigginb84a35b2005-05-01 08:58:36 -0700209
210 gfp_mask |= __GFP_NOMEMALLOC; /* don't allocate emergency reserves */
211 gfp_mask |= __GFP_NORETRY; /* don't loop in __alloc_pages */
212 gfp_mask |= __GFP_NOWARN; /* failures are OK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Nick Piggin20a77772005-05-01 08:58:37 -0700214 gfp_temp = gfp_mask & ~(__GFP_WAIT|__GFP_IO);
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216repeat_alloc:
Nick Piggin20a77772005-05-01 08:58:37 -0700217
218 element = pool->alloc(gfp_temp, pool->pool_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (likely(element != NULL))
220 return element;
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 spin_lock_irqsave(&pool->lock, flags);
223 if (likely(pool->curr_nr)) {
224 element = remove_element(pool);
225 spin_unlock_irqrestore(&pool->lock, flags);
Tejun Heo5b990542012-01-10 15:08:23 -0800226 /* paired with rmb in mempool_free(), read comment there */
227 smp_wmb();
Catalin Marinas17411962014-06-06 14:38:19 -0700228 /*
229 * Update the allocation stack trace as this is more useful
230 * for debugging.
231 */
232 kmemleak_update_trace(element);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return element;
234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Tejun Heo1ebb7042012-01-10 15:08:28 -0800236 /*
237 * We use gfp mask w/o __GFP_WAIT or IO for the first round. If
238 * alloc failed with that and @pool was empty, retry immediately.
239 */
240 if (gfp_temp != gfp_mask) {
241 spin_unlock_irqrestore(&pool->lock, flags);
242 gfp_temp = gfp_mask;
243 goto repeat_alloc;
244 }
245
246 /* We must not sleep if !__GFP_WAIT */
Tejun Heo5b990542012-01-10 15:08:23 -0800247 if (!(gfp_mask & __GFP_WAIT)) {
248 spin_unlock_irqrestore(&pool->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return NULL;
Tejun Heo5b990542012-01-10 15:08:23 -0800250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Tejun Heo5b990542012-01-10 15:08:23 -0800252 /* Let's wait for someone else to return an element to @pool */
Benjamin LaHaise01890a42005-06-23 00:10:01 -0700253 init_wait(&wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Tejun Heo5b990542012-01-10 15:08:23 -0800256 spin_unlock_irqrestore(&pool->lock, flags);
257
258 /*
259 * FIXME: this should be io_schedule(). The timeout is there as a
260 * workaround for some DM problems in 2.6.18.
261 */
262 io_schedule_timeout(5*HZ);
263
264 finish_wait(&pool->wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 goto repeat_alloc;
266}
267EXPORT_SYMBOL(mempool_alloc);
268
269/**
270 * mempool_free - return an element to the pool.
271 * @element: pool element pointer.
272 * @pool: pointer to the memory pool which was allocated via
273 * mempool_create().
274 *
275 * this function only sleeps if the free_fn() function sleeps.
276 */
277void mempool_free(void *element, mempool_t *pool)
278{
279 unsigned long flags;
280
Rusty Russellc80e7a82007-07-15 23:42:00 -0700281 if (unlikely(element == NULL))
282 return;
283
Tejun Heo5b990542012-01-10 15:08:23 -0800284 /*
285 * Paired with the wmb in mempool_alloc(). The preceding read is
286 * for @element and the following @pool->curr_nr. This ensures
287 * that the visible value of @pool->curr_nr is from after the
288 * allocation of @element. This is necessary for fringe cases
289 * where @element was passed to this task without going through
290 * barriers.
291 *
292 * For example, assume @p is %NULL at the beginning and one task
293 * performs "p = mempool_alloc(...);" while another task is doing
294 * "while (!p) cpu_relax(); mempool_free(p, ...);". This function
295 * may end up using curr_nr value which is from before allocation
296 * of @p without the following rmb.
297 */
298 smp_rmb();
299
300 /*
301 * For correctness, we need a test which is guaranteed to trigger
302 * if curr_nr + #allocated == min_nr. Testing curr_nr < min_nr
303 * without locking achieves that and refilling as soon as possible
304 * is desirable.
305 *
306 * Because curr_nr visible here is always a value after the
307 * allocation of @element, any task which decremented curr_nr below
308 * min_nr is guaranteed to see curr_nr < min_nr unless curr_nr gets
309 * incremented to min_nr afterwards. If curr_nr gets incremented
310 * to min_nr after the allocation of @element, the elements
311 * allocated after that are subject to the same guarantee.
312 *
313 * Waiters happen iff curr_nr is 0 and the above guarantee also
314 * ensures that there will be frees which return elements to the
315 * pool waking up the waiters.
316 */
Mikulas Patockaeb9a3c62014-04-07 15:37:35 -0700317 if (unlikely(pool->curr_nr < pool->min_nr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 spin_lock_irqsave(&pool->lock, flags);
Mikulas Patockaeb9a3c62014-04-07 15:37:35 -0700319 if (likely(pool->curr_nr < pool->min_nr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 add_element(pool, element);
321 spin_unlock_irqrestore(&pool->lock, flags);
322 wake_up(&pool->wait);
323 return;
324 }
325 spin_unlock_irqrestore(&pool->lock, flags);
326 }
327 pool->free(element, pool->pool_data);
328}
329EXPORT_SYMBOL(mempool_free);
330
331/*
332 * A commonly used alloc and free fn.
333 */
Al Virodd0fc662005-10-07 07:46:04 +0100334void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Pekka Enbergfcc234f2006-03-22 00:08:13 -0800336 struct kmem_cache *mem = pool_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return kmem_cache_alloc(mem, gfp_mask);
338}
339EXPORT_SYMBOL(mempool_alloc_slab);
340
341void mempool_free_slab(void *element, void *pool_data)
342{
Pekka Enbergfcc234f2006-03-22 00:08:13 -0800343 struct kmem_cache *mem = pool_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 kmem_cache_free(mem, element);
345}
346EXPORT_SYMBOL(mempool_free_slab);
Matthew Dobson6e0678f2006-03-26 01:37:44 -0800347
348/*
Matthew Dobson53184082006-03-26 01:37:46 -0800349 * A commonly used alloc and free fn that kmalloc/kfrees the amount of memory
Simon Arlott183ff222007-10-20 01:27:18 +0200350 * specified by pool_data
Matthew Dobson53184082006-03-26 01:37:46 -0800351 */
352void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data)
353{
Figo.zhang5e2f89b2009-08-08 21:01:22 +0800354 size_t size = (size_t)pool_data;
Matthew Dobson53184082006-03-26 01:37:46 -0800355 return kmalloc(size, gfp_mask);
356}
357EXPORT_SYMBOL(mempool_kmalloc);
358
359void mempool_kfree(void *element, void *pool_data)
360{
361 kfree(element);
362}
363EXPORT_SYMBOL(mempool_kfree);
364
365/*
Matthew Dobson6e0678f2006-03-26 01:37:44 -0800366 * A simple mempool-backed page allocator that allocates pages
367 * of the order specified by pool_data.
368 */
369void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data)
370{
371 int order = (int)(long)pool_data;
372 return alloc_pages(gfp_mask, order);
373}
374EXPORT_SYMBOL(mempool_alloc_pages);
375
376void mempool_free_pages(void *element, void *pool_data)
377{
378 int order = (int)(long)pool_data;
379 __free_pages(element, order);
380}
381EXPORT_SYMBOL(mempool_free_pages);