Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Marinas | 1741196 | 2014-06-06 14:38:19 -0700 | [diff] [blame] | 13 | #include <linux/kmemleak.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/mempool.h> |
| 16 | #include <linux/blkdev.h> |
| 17 | #include <linux/writeback.h> |
David Rientjes | e244c9e | 2015-04-15 16:14:14 -0700 | [diff] [blame^] | 18 | #include "slab.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | static void add_element(mempool_t *pool, void *element) |
| 21 | { |
| 22 | BUG_ON(pool->curr_nr >= pool->min_nr); |
| 23 | pool->elements[pool->curr_nr++] = element; |
| 24 | } |
| 25 | |
| 26 | static void *remove_element(mempool_t *pool) |
| 27 | { |
| 28 | BUG_ON(pool->curr_nr <= 0); |
| 29 | return pool->elements[--pool->curr_nr]; |
| 30 | } |
| 31 | |
Tejun Heo | 0565d31 | 2012-01-10 15:08:26 -0800 | [diff] [blame] | 32 | /** |
| 33 | * mempool_destroy - deallocate a memory pool |
| 34 | * @pool: pointer to the memory pool which was allocated via |
| 35 | * mempool_create(). |
| 36 | * |
| 37 | * Free all reserved elements in @pool and @pool itself. This function |
| 38 | * only sleeps if the free_fn() function sleeps. |
| 39 | */ |
| 40 | void mempool_destroy(mempool_t *pool) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | while (pool->curr_nr) { |
| 43 | void *element = remove_element(pool); |
| 44 | pool->free(element, pool->pool_data); |
| 45 | } |
| 46 | kfree(pool->elements); |
| 47 | kfree(pool); |
| 48 | } |
Tejun Heo | 0565d31 | 2012-01-10 15:08:26 -0800 | [diff] [blame] | 49 | EXPORT_SYMBOL(mempool_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * mempool_create - create a memory pool |
| 53 | * @min_nr: the minimum number of elements guaranteed to be |
| 54 | * allocated for this pool. |
| 55 | * @alloc_fn: user-defined element-allocation function. |
| 56 | * @free_fn: user-defined element-freeing function. |
| 57 | * @pool_data: optional private data available to the user-defined functions. |
| 58 | * |
| 59 | * this function creates and allocates a guaranteed size, preallocated |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 60 | * memory pool. The pool can be used from the mempool_alloc() and mempool_free() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | * functions. This function might sleep. Both the alloc_fn() and the free_fn() |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 62 | * functions might sleep - as long as the mempool_alloc() function is not called |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | * from IRQ contexts. |
| 64 | */ |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 65 | mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | mempool_free_t *free_fn, void *pool_data) |
| 67 | { |
Tejun Heo | a91a5ac | 2012-06-04 20:40:53 -0700 | [diff] [blame] | 68 | return mempool_create_node(min_nr,alloc_fn,free_fn, pool_data, |
| 69 | GFP_KERNEL, NUMA_NO_NODE); |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 70 | } |
| 71 | EXPORT_SYMBOL(mempool_create); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 73 | mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn, |
Tejun Heo | a91a5ac | 2012-06-04 20:40:53 -0700 | [diff] [blame] | 74 | mempool_free_t *free_fn, void *pool_data, |
| 75 | gfp_t gfp_mask, int node_id) |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 76 | { |
| 77 | mempool_t *pool; |
Joe Perches | 7b5219d | 2013-09-11 14:23:07 -0700 | [diff] [blame] | 78 | pool = kzalloc_node(sizeof(*pool), gfp_mask, node_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | if (!pool) |
| 80 | return NULL; |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 81 | pool->elements = kmalloc_node(min_nr * sizeof(void *), |
Tejun Heo | a91a5ac | 2012-06-04 20:40:53 -0700 | [diff] [blame] | 82 | gfp_mask, node_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | if (!pool->elements) { |
| 84 | kfree(pool); |
| 85 | return NULL; |
| 86 | } |
| 87 | spin_lock_init(&pool->lock); |
| 88 | pool->min_nr = min_nr; |
| 89 | pool->pool_data = pool_data; |
| 90 | init_waitqueue_head(&pool->wait); |
| 91 | pool->alloc = alloc_fn; |
| 92 | pool->free = free_fn; |
| 93 | |
| 94 | /* |
| 95 | * First pre-allocate the guaranteed number of buffers. |
| 96 | */ |
| 97 | while (pool->curr_nr < pool->min_nr) { |
| 98 | void *element; |
| 99 | |
Tejun Heo | a91a5ac | 2012-06-04 20:40:53 -0700 | [diff] [blame] | 100 | element = pool->alloc(gfp_mask, pool->pool_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | if (unlikely(!element)) { |
Tejun Heo | 0565d31 | 2012-01-10 15:08:26 -0800 | [diff] [blame] | 102 | mempool_destroy(pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | return NULL; |
| 104 | } |
| 105 | add_element(pool, element); |
| 106 | } |
| 107 | return pool; |
| 108 | } |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 109 | EXPORT_SYMBOL(mempool_create_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * mempool_resize - resize an existing memory pool |
| 113 | * @pool: pointer to the memory pool which was allocated via |
| 114 | * mempool_create(). |
| 115 | * @new_min_nr: the new minimum number of elements guaranteed to be |
| 116 | * allocated for this pool. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | * |
| 118 | * This function shrinks/grows the pool. In the case of growing, |
| 119 | * it cannot be guaranteed that the pool will be grown to the new |
| 120 | * size immediately, but new mempool_free() calls will refill it. |
David Rientjes | 11d8336 | 2015-04-14 15:48:21 -0700 | [diff] [blame] | 121 | * This function may sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | * |
| 123 | * Note, the caller must guarantee that no mempool_destroy is called |
| 124 | * while this function is running. mempool_alloc() & mempool_free() |
| 125 | * might be called (eg. from IRQ contexts) while this function executes. |
| 126 | */ |
David Rientjes | 11d8336 | 2015-04-14 15:48:21 -0700 | [diff] [blame] | 127 | int mempool_resize(mempool_t *pool, int new_min_nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
| 129 | void *element; |
| 130 | void **new_elements; |
| 131 | unsigned long flags; |
| 132 | |
| 133 | BUG_ON(new_min_nr <= 0); |
David Rientjes | 11d8336 | 2015-04-14 15:48:21 -0700 | [diff] [blame] | 134 | might_sleep(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
| 136 | spin_lock_irqsave(&pool->lock, flags); |
| 137 | if (new_min_nr <= pool->min_nr) { |
| 138 | while (new_min_nr < pool->curr_nr) { |
| 139 | element = remove_element(pool); |
| 140 | spin_unlock_irqrestore(&pool->lock, flags); |
| 141 | pool->free(element, pool->pool_data); |
| 142 | spin_lock_irqsave(&pool->lock, flags); |
| 143 | } |
| 144 | pool->min_nr = new_min_nr; |
| 145 | goto out_unlock; |
| 146 | } |
| 147 | spin_unlock_irqrestore(&pool->lock, flags); |
| 148 | |
| 149 | /* Grow the pool */ |
David Rientjes | 11d8336 | 2015-04-14 15:48:21 -0700 | [diff] [blame] | 150 | new_elements = kmalloc_array(new_min_nr, sizeof(*new_elements), |
| 151 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | if (!new_elements) |
| 153 | return -ENOMEM; |
| 154 | |
| 155 | spin_lock_irqsave(&pool->lock, flags); |
| 156 | if (unlikely(new_min_nr <= pool->min_nr)) { |
| 157 | /* Raced, other resize will do our work */ |
| 158 | spin_unlock_irqrestore(&pool->lock, flags); |
| 159 | kfree(new_elements); |
| 160 | goto out; |
| 161 | } |
| 162 | memcpy(new_elements, pool->elements, |
| 163 | pool->curr_nr * sizeof(*new_elements)); |
| 164 | kfree(pool->elements); |
| 165 | pool->elements = new_elements; |
| 166 | pool->min_nr = new_min_nr; |
| 167 | |
| 168 | while (pool->curr_nr < pool->min_nr) { |
| 169 | spin_unlock_irqrestore(&pool->lock, flags); |
David Rientjes | 11d8336 | 2015-04-14 15:48:21 -0700 | [diff] [blame] | 170 | element = pool->alloc(GFP_KERNEL, pool->pool_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | if (!element) |
| 172 | goto out; |
| 173 | spin_lock_irqsave(&pool->lock, flags); |
| 174 | if (pool->curr_nr < pool->min_nr) { |
| 175 | add_element(pool, element); |
| 176 | } else { |
| 177 | spin_unlock_irqrestore(&pool->lock, flags); |
| 178 | pool->free(element, pool->pool_data); /* Raced */ |
| 179 | goto out; |
| 180 | } |
| 181 | } |
| 182 | out_unlock: |
| 183 | spin_unlock_irqrestore(&pool->lock, flags); |
| 184 | out: |
| 185 | return 0; |
| 186 | } |
| 187 | EXPORT_SYMBOL(mempool_resize); |
| 188 | |
| 189 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | * mempool_alloc - allocate an element from a specific memory pool |
| 191 | * @pool: pointer to the memory pool which was allocated via |
| 192 | * mempool_create(). |
| 193 | * @gfp_mask: the usual allocation bitmask. |
| 194 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 195 | * this function only sleeps if the alloc_fn() function sleeps or |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | * returns NULL. Note that due to preallocation, this function |
| 197 | * *never* fails when called from process contexts. (it might |
| 198 | * fail if called from an IRQ context.) |
Sebastian Ott | 8bf8fcb | 2014-06-04 16:07:00 -0700 | [diff] [blame] | 199 | * Note: using __GFP_ZERO is not supported. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 201 | void * mempool_alloc(mempool_t *pool, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
| 203 | void *element; |
| 204 | unsigned long flags; |
Benjamin LaHaise | 01890a4 | 2005-06-23 00:10:01 -0700 | [diff] [blame] | 205 | wait_queue_t wait; |
Al Viro | 6daa0e2 | 2005-10-21 03:18:50 -0400 | [diff] [blame] | 206 | gfp_t gfp_temp; |
Nick Piggin | 20a7777 | 2005-05-01 08:58:37 -0700 | [diff] [blame] | 207 | |
Sebastian Ott | 8bf8fcb | 2014-06-04 16:07:00 -0700 | [diff] [blame] | 208 | VM_WARN_ON_ONCE(gfp_mask & __GFP_ZERO); |
Nick Piggin | 20a7777 | 2005-05-01 08:58:37 -0700 | [diff] [blame] | 209 | might_sleep_if(gfp_mask & __GFP_WAIT); |
Nick Piggin | b84a35b | 2005-05-01 08:58:36 -0700 | [diff] [blame] | 210 | |
| 211 | gfp_mask |= __GFP_NOMEMALLOC; /* don't allocate emergency reserves */ |
| 212 | gfp_mask |= __GFP_NORETRY; /* don't loop in __alloc_pages */ |
| 213 | gfp_mask |= __GFP_NOWARN; /* failures are OK */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Nick Piggin | 20a7777 | 2005-05-01 08:58:37 -0700 | [diff] [blame] | 215 | gfp_temp = gfp_mask & ~(__GFP_WAIT|__GFP_IO); |
| 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | repeat_alloc: |
Nick Piggin | 20a7777 | 2005-05-01 08:58:37 -0700 | [diff] [blame] | 218 | |
| 219 | element = pool->alloc(gfp_temp, pool->pool_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | if (likely(element != NULL)) |
| 221 | return element; |
| 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | spin_lock_irqsave(&pool->lock, flags); |
| 224 | if (likely(pool->curr_nr)) { |
| 225 | element = remove_element(pool); |
| 226 | spin_unlock_irqrestore(&pool->lock, flags); |
Tejun Heo | 5b99054 | 2012-01-10 15:08:23 -0800 | [diff] [blame] | 227 | /* paired with rmb in mempool_free(), read comment there */ |
| 228 | smp_wmb(); |
Catalin Marinas | 1741196 | 2014-06-06 14:38:19 -0700 | [diff] [blame] | 229 | /* |
| 230 | * Update the allocation stack trace as this is more useful |
| 231 | * for debugging. |
| 232 | */ |
| 233 | kmemleak_update_trace(element); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | return element; |
| 235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
Tejun Heo | 1ebb704 | 2012-01-10 15:08:28 -0800 | [diff] [blame] | 237 | /* |
| 238 | * We use gfp mask w/o __GFP_WAIT or IO for the first round. If |
| 239 | * alloc failed with that and @pool was empty, retry immediately. |
| 240 | */ |
| 241 | if (gfp_temp != gfp_mask) { |
| 242 | spin_unlock_irqrestore(&pool->lock, flags); |
| 243 | gfp_temp = gfp_mask; |
| 244 | goto repeat_alloc; |
| 245 | } |
| 246 | |
| 247 | /* We must not sleep if !__GFP_WAIT */ |
Tejun Heo | 5b99054 | 2012-01-10 15:08:23 -0800 | [diff] [blame] | 248 | if (!(gfp_mask & __GFP_WAIT)) { |
| 249 | spin_unlock_irqrestore(&pool->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | return NULL; |
Tejun Heo | 5b99054 | 2012-01-10 15:08:23 -0800 | [diff] [blame] | 251 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Tejun Heo | 5b99054 | 2012-01-10 15:08:23 -0800 | [diff] [blame] | 253 | /* Let's wait for someone else to return an element to @pool */ |
Benjamin LaHaise | 01890a4 | 2005-06-23 00:10:01 -0700 | [diff] [blame] | 254 | init_wait(&wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
Tejun Heo | 5b99054 | 2012-01-10 15:08:23 -0800 | [diff] [blame] | 257 | spin_unlock_irqrestore(&pool->lock, flags); |
| 258 | |
| 259 | /* |
| 260 | * FIXME: this should be io_schedule(). The timeout is there as a |
| 261 | * workaround for some DM problems in 2.6.18. |
| 262 | */ |
| 263 | io_schedule_timeout(5*HZ); |
| 264 | |
| 265 | finish_wait(&pool->wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | goto repeat_alloc; |
| 267 | } |
| 268 | EXPORT_SYMBOL(mempool_alloc); |
| 269 | |
| 270 | /** |
| 271 | * mempool_free - return an element to the pool. |
| 272 | * @element: pool element pointer. |
| 273 | * @pool: pointer to the memory pool which was allocated via |
| 274 | * mempool_create(). |
| 275 | * |
| 276 | * this function only sleeps if the free_fn() function sleeps. |
| 277 | */ |
| 278 | void mempool_free(void *element, mempool_t *pool) |
| 279 | { |
| 280 | unsigned long flags; |
| 281 | |
Rusty Russell | c80e7a8 | 2007-07-15 23:42:00 -0700 | [diff] [blame] | 282 | if (unlikely(element == NULL)) |
| 283 | return; |
| 284 | |
Tejun Heo | 5b99054 | 2012-01-10 15:08:23 -0800 | [diff] [blame] | 285 | /* |
| 286 | * Paired with the wmb in mempool_alloc(). The preceding read is |
| 287 | * for @element and the following @pool->curr_nr. This ensures |
| 288 | * that the visible value of @pool->curr_nr is from after the |
| 289 | * allocation of @element. This is necessary for fringe cases |
| 290 | * where @element was passed to this task without going through |
| 291 | * barriers. |
| 292 | * |
| 293 | * For example, assume @p is %NULL at the beginning and one task |
| 294 | * performs "p = mempool_alloc(...);" while another task is doing |
| 295 | * "while (!p) cpu_relax(); mempool_free(p, ...);". This function |
| 296 | * may end up using curr_nr value which is from before allocation |
| 297 | * of @p without the following rmb. |
| 298 | */ |
| 299 | smp_rmb(); |
| 300 | |
| 301 | /* |
| 302 | * For correctness, we need a test which is guaranteed to trigger |
| 303 | * if curr_nr + #allocated == min_nr. Testing curr_nr < min_nr |
| 304 | * without locking achieves that and refilling as soon as possible |
| 305 | * is desirable. |
| 306 | * |
| 307 | * Because curr_nr visible here is always a value after the |
| 308 | * allocation of @element, any task which decremented curr_nr below |
| 309 | * min_nr is guaranteed to see curr_nr < min_nr unless curr_nr gets |
| 310 | * incremented to min_nr afterwards. If curr_nr gets incremented |
| 311 | * to min_nr after the allocation of @element, the elements |
| 312 | * allocated after that are subject to the same guarantee. |
| 313 | * |
| 314 | * Waiters happen iff curr_nr is 0 and the above guarantee also |
| 315 | * ensures that there will be frees which return elements to the |
| 316 | * pool waking up the waiters. |
| 317 | */ |
Mikulas Patocka | eb9a3c6 | 2014-04-07 15:37:35 -0700 | [diff] [blame] | 318 | if (unlikely(pool->curr_nr < pool->min_nr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | spin_lock_irqsave(&pool->lock, flags); |
Mikulas Patocka | eb9a3c6 | 2014-04-07 15:37:35 -0700 | [diff] [blame] | 320 | if (likely(pool->curr_nr < pool->min_nr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | add_element(pool, element); |
| 322 | spin_unlock_irqrestore(&pool->lock, flags); |
| 323 | wake_up(&pool->wait); |
| 324 | return; |
| 325 | } |
| 326 | spin_unlock_irqrestore(&pool->lock, flags); |
| 327 | } |
| 328 | pool->free(element, pool->pool_data); |
| 329 | } |
| 330 | EXPORT_SYMBOL(mempool_free); |
| 331 | |
| 332 | /* |
| 333 | * A commonly used alloc and free fn. |
| 334 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 335 | void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | { |
Pekka Enberg | fcc234f | 2006-03-22 00:08:13 -0800 | [diff] [blame] | 337 | struct kmem_cache *mem = pool_data; |
David Rientjes | e244c9e | 2015-04-15 16:14:14 -0700 | [diff] [blame^] | 338 | VM_BUG_ON(mem->ctor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | return kmem_cache_alloc(mem, gfp_mask); |
| 340 | } |
| 341 | EXPORT_SYMBOL(mempool_alloc_slab); |
| 342 | |
| 343 | void mempool_free_slab(void *element, void *pool_data) |
| 344 | { |
Pekka Enberg | fcc234f | 2006-03-22 00:08:13 -0800 | [diff] [blame] | 345 | struct kmem_cache *mem = pool_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | kmem_cache_free(mem, element); |
| 347 | } |
| 348 | EXPORT_SYMBOL(mempool_free_slab); |
Matthew Dobson | 6e0678f | 2006-03-26 01:37:44 -0800 | [diff] [blame] | 349 | |
| 350 | /* |
Matthew Dobson | 5318408 | 2006-03-26 01:37:46 -0800 | [diff] [blame] | 351 | * A commonly used alloc and free fn that kmalloc/kfrees the amount of memory |
Simon Arlott | 183ff22 | 2007-10-20 01:27:18 +0200 | [diff] [blame] | 352 | * specified by pool_data |
Matthew Dobson | 5318408 | 2006-03-26 01:37:46 -0800 | [diff] [blame] | 353 | */ |
| 354 | void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data) |
| 355 | { |
Figo.zhang | 5e2f89b | 2009-08-08 21:01:22 +0800 | [diff] [blame] | 356 | size_t size = (size_t)pool_data; |
Matthew Dobson | 5318408 | 2006-03-26 01:37:46 -0800 | [diff] [blame] | 357 | return kmalloc(size, gfp_mask); |
| 358 | } |
| 359 | EXPORT_SYMBOL(mempool_kmalloc); |
| 360 | |
| 361 | void mempool_kfree(void *element, void *pool_data) |
| 362 | { |
| 363 | kfree(element); |
| 364 | } |
| 365 | EXPORT_SYMBOL(mempool_kfree); |
| 366 | |
| 367 | /* |
Matthew Dobson | 6e0678f | 2006-03-26 01:37:44 -0800 | [diff] [blame] | 368 | * A simple mempool-backed page allocator that allocates pages |
| 369 | * of the order specified by pool_data. |
| 370 | */ |
| 371 | void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data) |
| 372 | { |
| 373 | int order = (int)(long)pool_data; |
| 374 | return alloc_pages(gfp_mask, order); |
| 375 | } |
| 376 | EXPORT_SYMBOL(mempool_alloc_pages); |
| 377 | |
| 378 | void mempool_free_pages(void *element, void *pool_data) |
| 379 | { |
| 380 | int order = (int)(long)pool_data; |
| 381 | __free_pages(element, order); |
| 382 | } |
| 383 | EXPORT_SYMBOL(mempool_free_pages); |