4 * memory buffer pool support. Such pools are mostly used
5 * for guaranteed, deadlock-free memory allocations during
8 * started by Ingo Molnar, Copyright (C) 2001
12 #include <linux/slab.h>
13 #include <linux/kmemleak.h>
14 #include <linux/export.h>
15 #include <linux/mempool.h>
16 #include <linux/blkdev.h>
17 #include <linux/writeback.h>
19 static void add_element(mempool_t *pool, void *element)
21 BUG_ON(pool->curr_nr >= pool->min_nr);
22 pool->elements[pool->curr_nr++] = element;
25 static void *remove_element(mempool_t *pool)
27 BUG_ON(pool->curr_nr <= 0);
28 return pool->elements[--pool->curr_nr];
32 * mempool_destroy - deallocate a memory pool
33 * @pool: pointer to the memory pool which was allocated via
36 * Free all reserved elements in @pool and @pool itself. This function
37 * only sleeps if the free_fn() function sleeps.
39 void mempool_destroy(mempool_t *pool)
41 while (pool->curr_nr) {
42 void *element = remove_element(pool);
43 pool->free(element, pool->pool_data);
45 kfree(pool->elements);
48 EXPORT_SYMBOL(mempool_destroy);
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.
58 * this function creates and allocates a guaranteed size, preallocated
59 * memory pool. The pool can be used from the mempool_alloc() and mempool_free()
60 * functions. This function might sleep. Both the alloc_fn() and the free_fn()
61 * functions might sleep - as long as the mempool_alloc() function is not called
64 mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
65 mempool_free_t *free_fn, void *pool_data)
67 return mempool_create_node(min_nr,alloc_fn,free_fn, pool_data,
68 GFP_KERNEL, NUMA_NO_NODE);
70 EXPORT_SYMBOL(mempool_create);
72 mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
73 mempool_free_t *free_fn, void *pool_data,
74 gfp_t gfp_mask, int node_id)
77 pool = kzalloc_node(sizeof(*pool), gfp_mask, node_id);
80 pool->elements = kmalloc_node(min_nr * sizeof(void *),
82 if (!pool->elements) {
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;
94 * First pre-allocate the guaranteed number of buffers.
96 while (pool->curr_nr < pool->min_nr) {
99 element = pool->alloc(gfp_mask, pool->pool_data);
100 if (unlikely(!element)) {
101 mempool_destroy(pool);
104 add_element(pool, element);
108 EXPORT_SYMBOL(mempool_create_node);
111 * mempool_resize - resize an existing memory pool
112 * @pool: pointer to the memory pool which was allocated via
114 * @new_min_nr: the new minimum number of elements guaranteed to be
115 * allocated for this pool.
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.
120 * This function may sleep.
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.
126 int mempool_resize(mempool_t *pool, int new_min_nr)
132 BUG_ON(new_min_nr <= 0);
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);
143 pool->min_nr = new_min_nr;
146 spin_unlock_irqrestore(&pool->lock, flags);
149 new_elements = kmalloc_array(new_min_nr, sizeof(*new_elements),
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);
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;
167 while (pool->curr_nr < pool->min_nr) {
168 spin_unlock_irqrestore(&pool->lock, flags);
169 element = pool->alloc(GFP_KERNEL, pool->pool_data);
172 spin_lock_irqsave(&pool->lock, flags);
173 if (pool->curr_nr < pool->min_nr) {
174 add_element(pool, element);
176 spin_unlock_irqrestore(&pool->lock, flags);
177 pool->free(element, pool->pool_data); /* Raced */
182 spin_unlock_irqrestore(&pool->lock, flags);
186 EXPORT_SYMBOL(mempool_resize);
189 * mempool_alloc - allocate an element from a specific memory pool
190 * @pool: pointer to the memory pool which was allocated via
192 * @gfp_mask: the usual allocation bitmask.
194 * this function only sleeps if the alloc_fn() function sleeps or
195 * 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.)
198 * Note: using __GFP_ZERO is not supported.
200 void * mempool_alloc(mempool_t *pool, gfp_t gfp_mask)
207 VM_WARN_ON_ONCE(gfp_mask & __GFP_ZERO);
208 might_sleep_if(gfp_mask & __GFP_WAIT);
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 */
214 gfp_temp = gfp_mask & ~(__GFP_WAIT|__GFP_IO);
218 element = pool->alloc(gfp_temp, pool->pool_data);
219 if (likely(element != NULL))
222 spin_lock_irqsave(&pool->lock, flags);
223 if (likely(pool->curr_nr)) {
224 element = remove_element(pool);
225 spin_unlock_irqrestore(&pool->lock, flags);
226 /* paired with rmb in mempool_free(), read comment there */
229 * Update the allocation stack trace as this is more useful
232 kmemleak_update_trace(element);
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.
240 if (gfp_temp != gfp_mask) {
241 spin_unlock_irqrestore(&pool->lock, flags);
246 /* We must not sleep if !__GFP_WAIT */
247 if (!(gfp_mask & __GFP_WAIT)) {
248 spin_unlock_irqrestore(&pool->lock, flags);
252 /* Let's wait for someone else to return an element to @pool */
254 prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
256 spin_unlock_irqrestore(&pool->lock, flags);
259 * FIXME: this should be io_schedule(). The timeout is there as a
260 * workaround for some DM problems in 2.6.18.
262 io_schedule_timeout(5*HZ);
264 finish_wait(&pool->wait, &wait);
267 EXPORT_SYMBOL(mempool_alloc);
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
275 * this function only sleeps if the free_fn() function sleeps.
277 void mempool_free(void *element, mempool_t *pool)
281 if (unlikely(element == NULL))
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
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.
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
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.
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.
317 if (unlikely(pool->curr_nr < pool->min_nr)) {
318 spin_lock_irqsave(&pool->lock, flags);
319 if (likely(pool->curr_nr < pool->min_nr)) {
320 add_element(pool, element);
321 spin_unlock_irqrestore(&pool->lock, flags);
322 wake_up(&pool->wait);
325 spin_unlock_irqrestore(&pool->lock, flags);
327 pool->free(element, pool->pool_data);
329 EXPORT_SYMBOL(mempool_free);
332 * A commonly used alloc and free fn.
334 void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data)
336 struct kmem_cache *mem = pool_data;
337 return kmem_cache_alloc(mem, gfp_mask);
339 EXPORT_SYMBOL(mempool_alloc_slab);
341 void mempool_free_slab(void *element, void *pool_data)
343 struct kmem_cache *mem = pool_data;
344 kmem_cache_free(mem, element);
346 EXPORT_SYMBOL(mempool_free_slab);
349 * A commonly used alloc and free fn that kmalloc/kfrees the amount of memory
350 * specified by pool_data
352 void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data)
354 size_t size = (size_t)pool_data;
355 return kmalloc(size, gfp_mask);
357 EXPORT_SYMBOL(mempool_kmalloc);
359 void mempool_kfree(void *element, void *pool_data)
363 EXPORT_SYMBOL(mempool_kfree);
366 * A simple mempool-backed page allocator that allocates pages
367 * of the order specified by pool_data.
369 void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data)
371 int order = (int)(long)pool_data;
372 return alloc_pages(gfp_mask, order);
374 EXPORT_SYMBOL(mempool_alloc_pages);
376 void mempool_free_pages(void *element, void *pool_data)
378 int order = (int)(long)pool_data;
379 __free_pages(element, order);
381 EXPORT_SYMBOL(mempool_free_pages);