MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / mm / mempool.c
blob8dec8779e28f0c60dfff8fc2afce56308b9c50d5
1 /*
2 * linux/mm/mempool.c
4 * memory buffer pool support. Such pools are mostly used
5 * for guaranteed, deadlock-free memory allocations during
6 * extreme VM load.
8 * started by Ingo Molnar, Copyright (C) 2001
9 */
11 #include <linux/mm.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/mempool.h>
15 #include <linux/blkdev.h>
16 #include <linux/writeback.h>
18 static void add_element(mempool_t *pool, void *element)
20 BUG_ON(pool->curr_nr >= pool->min_nr);
21 pool->elements[pool->curr_nr++] = element;
24 static void *remove_element(mempool_t *pool)
26 BUG_ON(pool->curr_nr <= 0);
27 return pool->elements[--pool->curr_nr];
30 static void free_pool(mempool_t *pool)
32 while (pool->curr_nr) {
33 void *element = remove_element(pool);
34 pool->free(element, pool->pool_data);
36 kfree(pool->elements);
37 kfree(pool);
40 /**
41 * mempool_create - create a memory pool
42 * @min_nr: the minimum number of elements guaranteed to be
43 * allocated for this pool.
44 * @alloc_fn: user-defined element-allocation function.
45 * @free_fn: user-defined element-freeing function.
46 * @pool_data: optional private data available to the user-defined functions.
48 * this function creates and allocates a guaranteed size, preallocated
49 * memory pool. The pool can be used from the mempool_alloc and mempool_free
50 * functions. This function might sleep. Both the alloc_fn() and the free_fn()
51 * functions might sleep - as long as the mempool_alloc function is not called
52 * from IRQ contexts.
54 mempool_t * mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
55 mempool_free_t *free_fn, void *pool_data)
57 mempool_t *pool;
59 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
60 if (!pool)
61 return NULL;
62 memset(pool, 0, sizeof(*pool));
63 pool->elements = kmalloc(min_nr * sizeof(void *), GFP_KERNEL);
64 if (!pool->elements) {
65 kfree(pool);
66 return NULL;
68 spin_lock_init(&pool->lock);
69 pool->min_nr = min_nr;
70 pool->pool_data = pool_data;
71 init_waitqueue_head(&pool->wait);
72 pool->alloc = alloc_fn;
73 pool->free = free_fn;
76 * First pre-allocate the guaranteed number of buffers.
78 while (pool->curr_nr < pool->min_nr) {
79 void *element;
81 element = pool->alloc(GFP_KERNEL, pool->pool_data);
82 if (unlikely(!element)) {
83 free_pool(pool);
84 return NULL;
86 add_element(pool, element);
88 return pool;
90 EXPORT_SYMBOL(mempool_create);
92 /**
93 * mempool_resize - resize an existing memory pool
94 * @pool: pointer to the memory pool which was allocated via
95 * mempool_create().
96 * @new_min_nr: the new minimum number of elements guaranteed to be
97 * allocated for this pool.
98 * @gfp_mask: the usual allocation bitmask.
100 * This function shrinks/grows the pool. In the case of growing,
101 * it cannot be guaranteed that the pool will be grown to the new
102 * size immediately, but new mempool_free() calls will refill it.
104 * Note, the caller must guarantee that no mempool_destroy is called
105 * while this function is running. mempool_alloc() & mempool_free()
106 * might be called (eg. from IRQ contexts) while this function executes.
108 int mempool_resize(mempool_t *pool, int new_min_nr, int gfp_mask)
110 void *element;
111 void **new_elements;
112 unsigned long flags;
114 BUG_ON(new_min_nr <= 0);
116 spin_lock_irqsave(&pool->lock, flags);
117 if (new_min_nr < pool->min_nr) {
118 while (pool->curr_nr > new_min_nr) {
119 element = remove_element(pool);
120 spin_unlock_irqrestore(&pool->lock, flags);
121 pool->free(element, pool->pool_data);
122 spin_lock_irqsave(&pool->lock, flags);
124 pool->min_nr = new_min_nr;
125 goto out_unlock;
127 spin_unlock_irqrestore(&pool->lock, flags);
129 /* Grow the pool */
130 new_elements = kmalloc(new_min_nr * sizeof(*new_elements), gfp_mask);
131 if (!new_elements)
132 return -ENOMEM;
134 spin_lock_irqsave(&pool->lock, flags);
135 memcpy(new_elements, pool->elements,
136 pool->curr_nr * sizeof(*new_elements));
137 kfree(pool->elements);
138 pool->elements = new_elements;
139 pool->min_nr = new_min_nr;
141 while (pool->curr_nr < pool->min_nr) {
142 spin_unlock_irqrestore(&pool->lock, flags);
143 element = pool->alloc(gfp_mask, pool->pool_data);
144 if (!element)
145 goto out;
146 spin_lock_irqsave(&pool->lock, flags);
147 if (pool->curr_nr < pool->min_nr) {
148 add_element(pool, element);
149 } else {
150 spin_unlock_irqrestore(&pool->lock, flags);
151 pool->free(element, pool->pool_data); /* Raced */
152 spin_lock_irqsave(&pool->lock, flags);
155 out_unlock:
156 spin_unlock_irqrestore(&pool->lock, flags);
157 out:
158 return 0;
160 EXPORT_SYMBOL(mempool_resize);
163 * mempool_destroy - deallocate a memory pool
164 * @pool: pointer to the memory pool which was allocated via
165 * mempool_create().
167 * this function only sleeps if the free_fn() function sleeps. The caller
168 * has to guarantee that all elements have been returned to the pool (ie:
169 * freed) prior to calling mempool_destroy().
171 void mempool_destroy(mempool_t *pool)
173 if (pool->curr_nr != pool->min_nr)
174 BUG(); /* There were outstanding elements */
175 free_pool(pool);
177 EXPORT_SYMBOL(mempool_destroy);
180 * mempool_alloc - allocate an element from a specific memory pool
181 * @pool: pointer to the memory pool which was allocated via
182 * mempool_create().
183 * @gfp_mask: the usual allocation bitmask.
185 * this function only sleeps if the alloc_fn function sleeps or
186 * returns NULL. Note that due to preallocation, this function
187 * *never* fails when called from process contexts. (it might
188 * fail if called from an IRQ context.)
190 void * mempool_alloc(mempool_t *pool, int gfp_mask)
192 void *element;
193 unsigned long flags;
194 DEFINE_WAIT(wait);
195 int gfp_nowait = gfp_mask & ~(__GFP_WAIT | __GFP_IO);
197 might_sleep_if(gfp_mask & __GFP_WAIT);
198 repeat_alloc:
199 element = pool->alloc(gfp_nowait|__GFP_NOWARN, pool->pool_data);
200 if (likely(element != NULL))
201 return element;
204 * If the pool is less than 50% full and we can perform effective
205 * page reclaim then try harder to allocate an element.
207 mb();
208 if ((gfp_mask & __GFP_FS) && (gfp_mask != gfp_nowait) &&
209 (pool->curr_nr <= pool->min_nr/2)) {
210 element = pool->alloc(gfp_mask, pool->pool_data);
211 if (likely(element != NULL))
212 return element;
216 * Kick the VM at this point.
218 wakeup_bdflush(0);
220 spin_lock_irqsave(&pool->lock, flags);
221 if (likely(pool->curr_nr)) {
222 element = remove_element(pool);
223 spin_unlock_irqrestore(&pool->lock, flags);
224 return element;
226 spin_unlock_irqrestore(&pool->lock, flags);
228 /* We must not sleep in the GFP_ATOMIC case */
229 if (!(gfp_mask & __GFP_WAIT))
230 return NULL;
232 prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
233 mb();
234 if (!pool->curr_nr)
235 io_schedule();
236 finish_wait(&pool->wait, &wait);
238 goto repeat_alloc;
240 EXPORT_SYMBOL(mempool_alloc);
243 * mempool_free - return an element to the pool.
244 * @element: pool element pointer.
245 * @pool: pointer to the memory pool which was allocated via
246 * mempool_create().
248 * this function only sleeps if the free_fn() function sleeps.
250 void mempool_free(void *element, mempool_t *pool)
252 unsigned long flags;
254 mb();
255 if (pool->curr_nr < pool->min_nr) {
256 spin_lock_irqsave(&pool->lock, flags);
257 if (pool->curr_nr < pool->min_nr) {
258 add_element(pool, element);
259 spin_unlock_irqrestore(&pool->lock, flags);
260 wake_up(&pool->wait);
261 return;
263 spin_unlock_irqrestore(&pool->lock, flags);
265 pool->free(element, pool->pool_data);
267 EXPORT_SYMBOL(mempool_free);
270 * A commonly used alloc and free fn.
272 void *mempool_alloc_slab(int gfp_mask, void *pool_data)
274 kmem_cache_t *mem = (kmem_cache_t *) pool_data;
275 return kmem_cache_alloc(mem, gfp_mask);
277 EXPORT_SYMBOL(mempool_alloc_slab);
279 void mempool_free_slab(void *element, void *pool_data)
281 kmem_cache_t *mem = (kmem_cache_t *) pool_data;
282 kmem_cache_free(mem, element);
284 EXPORT_SYMBOL(mempool_free_slab);