Cleanup syscall code to look more like it's mips64 equivalent.
[linux-2.6/linux-mips.git] / mm / mempool.c
blob027e93c450a22589ff39c537e0b35f40746fe6ae
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);
93 * mempool_resize is disabled for now, because it has no callers. Feel free
94 * to turn it back on if needed.
96 #if 0
97 /**
98 * mempool_resize - resize an existing memory pool
99 * @pool: pointer to the memory pool which was allocated via
100 * mempool_create().
101 * @new_min_nr: the new minimum number of elements guaranteed to be
102 * allocated for this pool.
103 * @gfp_mask: the usual allocation bitmask.
105 * This function shrinks/grows the pool. In the case of growing,
106 * it cannot be guaranteed that the pool will be grown to the new
107 * size immediately, but new mempool_free() calls will refill it.
109 * Note, the caller must guarantee that no mempool_destroy is called
110 * while this function is running. mempool_alloc() & mempool_free()
111 * might be called (eg. from IRQ contexts) while this function executes.
113 int mempool_resize(mempool_t *pool, int new_min_nr, int gfp_mask)
115 void *element;
116 void **new_elements;
117 unsigned long flags;
119 BUG_ON(new_min_nr <= 0);
121 spin_lock_irqsave(&pool->lock, flags);
122 if (new_min_nr < pool->min_nr) {
123 while (pool->curr_nr > new_min_nr) {
124 element = remove_element(pool);
125 spin_unlock_irqrestore(&pool->lock, flags);
126 pool->free(element, pool->pool_data);
127 spin_lock_irqsave(&pool->lock, flags);
129 pool->min_nr = new_min_nr;
130 goto out_unlock;
132 spin_unlock_irqrestore(&pool->lock, flags);
134 /* Grow the pool */
135 new_elements = kmalloc(new_min_nr * sizeof(*new_elements), gfp_mask);
136 if (!new_elements)
137 return -ENOMEM;
139 spin_lock_irqsave(&pool->lock, flags);
140 memcpy(new_elements, pool->elements,
141 pool->curr_nr * sizeof(*new_elements));
142 kfree(pool->elements);
143 pool->elements = new_elements;
144 pool->min_nr = new_min_nr;
146 while (pool->curr_nr < pool->min_nr) {
147 spin_unlock_irqrestore(&pool->lock, flags);
148 element = pool->alloc(gfp_mask, pool->pool_data);
149 if (!element)
150 goto out;
151 spin_lock_irqsave(&pool->lock, flags);
152 if (pool->curr_nr < pool->min_nr) {
153 add_element(pool, element);
154 } else {
155 spin_unlock_irqrestore(&pool->lock, flags);
156 pool->free(element, pool->pool_data); /* Raced */
157 spin_lock_irqsave(&pool->lock, flags);
160 out_unlock:
161 spin_unlock_irqrestore(&pool->lock, flags);
162 out:
163 return 0;
165 EXPORT_SYMBOL(mempool_resize);
166 #endif
169 * mempool_destroy - deallocate a memory pool
170 * @pool: pointer to the memory pool which was allocated via
171 * mempool_create().
173 * this function only sleeps if the free_fn() function sleeps. The caller
174 * has to guarantee that all elements have been returned to the pool (ie:
175 * freed) prior to calling mempool_destroy().
177 void mempool_destroy(mempool_t *pool)
179 if (pool->curr_nr != pool->min_nr)
180 BUG(); /* There were outstanding elements */
181 free_pool(pool);
183 EXPORT_SYMBOL(mempool_destroy);
186 * mempool_alloc - allocate an element from a specific memory pool
187 * @pool: pointer to the memory pool which was allocated via
188 * mempool_create().
189 * @gfp_mask: the usual allocation bitmask.
191 * this function only sleeps if the alloc_fn function sleeps or
192 * returns NULL. Note that due to preallocation, this function
193 * *never* fails when called from process contexts. (it might
194 * fail if called from an IRQ context.)
196 void * mempool_alloc(mempool_t *pool, int gfp_mask)
198 void *element;
199 unsigned long flags;
200 DEFINE_WAIT(wait);
201 int gfp_nowait = gfp_mask & ~(__GFP_WAIT | __GFP_IO);
203 repeat_alloc:
204 element = pool->alloc(gfp_nowait|__GFP_NOWARN, pool->pool_data);
205 if (likely(element != NULL))
206 return element;
209 * If the pool is less than 50% full and we can perform effective
210 * page reclaim then try harder to allocate an element.
212 if ((gfp_mask & __GFP_FS) && (gfp_mask != gfp_nowait) &&
213 (pool->curr_nr <= pool->min_nr/2)) {
214 element = pool->alloc(gfp_mask, pool->pool_data);
215 if (likely(element != NULL))
216 return element;
220 * Kick the VM at this point.
222 wakeup_bdflush(0);
224 spin_lock_irqsave(&pool->lock, flags);
225 if (likely(pool->curr_nr)) {
226 element = remove_element(pool);
227 spin_unlock_irqrestore(&pool->lock, flags);
228 return element;
230 spin_unlock_irqrestore(&pool->lock, flags);
232 /* We must not sleep in the GFP_ATOMIC case */
233 if (!(gfp_mask & __GFP_WAIT))
234 return NULL;
236 blk_run_queues();
238 prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
239 if (!pool->curr_nr)
240 io_schedule();
241 finish_wait(&pool->wait, &wait);
243 goto repeat_alloc;
245 EXPORT_SYMBOL(mempool_alloc);
248 * mempool_free - return an element to the pool.
249 * @element: pool element pointer.
250 * @pool: pointer to the memory pool which was allocated via
251 * mempool_create().
253 * this function only sleeps if the free_fn() function sleeps.
255 void mempool_free(void *element, mempool_t *pool)
257 unsigned long flags;
259 if (pool->curr_nr < pool->min_nr) {
260 spin_lock_irqsave(&pool->lock, flags);
261 if (pool->curr_nr < pool->min_nr) {
262 add_element(pool, element);
263 spin_unlock_irqrestore(&pool->lock, flags);
264 wake_up(&pool->wait);
265 return;
267 spin_unlock_irqrestore(&pool->lock, flags);
269 pool->free(element, pool->pool_data);
271 EXPORT_SYMBOL(mempool_free);
274 * A commonly used alloc and free fn.
276 void *mempool_alloc_slab(int gfp_mask, void *pool_data)
278 kmem_cache_t *mem = (kmem_cache_t *) pool_data;
279 return kmem_cache_alloc(mem, gfp_mask);
281 EXPORT_SYMBOL(mempool_alloc_slab);
283 void mempool_free_slab(void *element, void *pool_data)
285 kmem_cache_t *mem = (kmem_cache_t *) pool_data;
286 kmem_cache_free(mem, element);
288 EXPORT_SYMBOL(mempool_free_slab);