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
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/mempool.h>
16 * mempool_create - create a memory pool
17 * @min_nr: the minimum number of elements guaranteed to be
18 * allocated for this pool.
19 * @alloc_fn: user-defined element-allocation function.
20 * @free_fn: user-defined element-freeing function.
21 * @pool_data: optional private data available to the user-defined functions.
23 * this function creates and allocates a guaranteed size, preallocated
24 * memory pool. The pool can be used from the mempool_alloc and mempool_free
25 * functions. This function might sleep. Both the alloc_fn() and the free_fn()
26 * functions might sleep - as long as the mempool_alloc function is not called
27 * from IRQ contexts. The element allocated by alloc_fn() must be able to
28 * hold a struct list_head. (8 bytes on x86.)
30 mempool_t * mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
31 mempool_free_t *free_fn, void *pool_data)
36 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
39 memset(pool, 0, sizeof(*pool));
41 spin_lock_init(&pool->lock);
42 pool->min_nr = min_nr;
43 pool->pool_data = pool_data;
44 INIT_LIST_HEAD(&pool->elements);
45 init_waitqueue_head(&pool->wait);
46 pool->alloc = alloc_fn;
50 * First pre-allocate the guaranteed number of buffers.
52 for (i = 0; i < min_nr; i++) {
54 struct list_head *tmp;
55 element = pool->alloc(GFP_KERNEL, pool->pool_data);
57 if (unlikely(!element)) {
59 * Not enough memory - free the allocated ones
62 list_for_each(tmp, &pool->elements) {
64 pool->free(element, pool->pool_data);
71 list_add(tmp, &pool->elements);
78 * mempool_resize - resize an existing memory pool
79 * @pool: pointer to the memory pool which was allocated via
81 * @new_min_nr: the new minimum number of elements guaranteed to be
82 * allocated for this pool.
83 * @gfp_mask: the usual allocation bitmask.
85 * This function shrinks/grows the pool. In the case of growing,
86 * it cannot be guaranteed that the pool will be grown to the new
87 * size immediately, but new mempool_free() calls will refill it.
89 * Note, the caller must guarantee that no mempool_destroy is called
90 * while this function is running. mempool_alloc() & mempool_free()
91 * might be called (eg. from IRQ contexts) while this function executes.
93 void mempool_resize(mempool_t *pool, int new_min_nr, int gfp_mask)
98 struct list_head *tmp;
103 spin_lock_irqsave(&pool->lock, flags);
104 if (new_min_nr < pool->min_nr) {
105 pool->min_nr = new_min_nr;
107 * Free possible excess elements.
109 while (pool->curr_nr > pool->min_nr) {
110 tmp = pool->elements.next;
111 if (tmp == &pool->elements)
116 spin_unlock_irqrestore(&pool->lock, flags);
118 pool->free(element, pool->pool_data);
120 spin_lock_irqsave(&pool->lock, flags);
122 spin_unlock_irqrestore(&pool->lock, flags);
125 delta = new_min_nr - pool->min_nr;
126 pool->min_nr = new_min_nr;
127 spin_unlock_irqrestore(&pool->lock, flags);
130 * We refill the pool up to the new treshold - but we dont
131 * (cannot) guarantee that the refill succeeds.
134 element = pool->alloc(gfp_mask, pool->pool_data);
137 mempool_free(element, pool);
143 * mempool_destroy - deallocate a memory pool
144 * @pool: pointer to the memory pool which was allocated via
147 * this function only sleeps if the free_fn() function sleeps. The caller
148 * has to guarantee that no mempool_alloc() nor mempool_free() happens in
149 * this pool when calling this function.
151 void mempool_destroy(mempool_t *pool)
154 struct list_head *head, *tmp;
159 head = &pool->elements;
160 for (tmp = head->next; tmp != head; ) {
163 pool->free(element, pool->pool_data);
172 * mempool_alloc - allocate an element from a specific memory pool
173 * @pool: pointer to the memory pool which was allocated via
175 * @gfp_mask: the usual allocation bitmask.
177 * this function only sleeps if the alloc_fn function sleeps or
178 * returns NULL. Note that due to preallocation, this function
181 void * mempool_alloc(mempool_t *pool, int gfp_mask)
185 struct list_head *tmp;
187 DECLARE_WAITQUEUE(wait, current);
188 int gfp_nowait = gfp_mask & ~__GFP_WAIT;
191 element = pool->alloc(gfp_nowait, pool->pool_data);
192 if (likely(element != NULL))
196 * If the pool is less than 50% full then try harder
197 * to allocate an element:
199 if (gfp_mask != gfp_nowait) {
200 if (pool->curr_nr <= pool->min_nr/2) {
201 element = pool->alloc(gfp_mask, pool->pool_data);
202 if (likely(element != NULL))
206 /* we must not sleep */
210 * Kick the VM at this point.
214 spin_lock_irqsave(&pool->lock, flags);
215 if (likely(pool->curr_nr)) {
216 tmp = pool->elements.next;
220 spin_unlock_irqrestore(&pool->lock, flags);
224 add_wait_queue_exclusive(&pool->wait, &wait);
225 set_task_state(current, TASK_UNINTERRUPTIBLE);
227 curr_nr = pool->curr_nr;
228 spin_unlock_irqrestore(&pool->lock, flags);
231 run_task_queue(&tq_disk);
235 current->state = TASK_RUNNING;
236 remove_wait_queue(&pool->wait, &wait);
242 * mempool_free - return an element to the pool.
243 * @element: pool element pointer.
244 * @pool: pointer to the memory pool which was allocated via
247 * this function only sleeps if the free_fn() function sleeps.
249 void mempool_free(void *element, mempool_t *pool)
253 if (pool->curr_nr < pool->min_nr) {
254 spin_lock_irqsave(&pool->lock, flags);
255 if (pool->curr_nr < pool->min_nr) {
256 list_add(element, &pool->elements);
258 spin_unlock_irqrestore(&pool->lock, flags);
259 wake_up(&pool->wait);
262 spin_unlock_irqrestore(&pool->lock, flags);
264 pool->free(element, pool->pool_data);
267 EXPORT_SYMBOL(mempool_create);
268 EXPORT_SYMBOL(mempool_resize);
269 EXPORT_SYMBOL(mempool_destroy);
270 EXPORT_SYMBOL(mempool_alloc);
271 EXPORT_SYMBOL(mempool_free);