11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/mm/mempool.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * memory buffer pool support. Such pools are mostly used 51da177e4SLinus Torvalds * for guaranteed, deadlock-free memory allocations during 61da177e4SLinus Torvalds * extreme VM load. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * started by Ingo Molnar, Copyright (C) 2001 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds #include <linux/mm.h> 121da177e4SLinus Torvalds #include <linux/slab.h> 131da177e4SLinus Torvalds #include <linux/module.h> 141da177e4SLinus Torvalds #include <linux/mempool.h> 151da177e4SLinus Torvalds #include <linux/blkdev.h> 161da177e4SLinus Torvalds #include <linux/writeback.h> 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds static void add_element(mempool_t *pool, void *element) 191da177e4SLinus Torvalds { 201da177e4SLinus Torvalds BUG_ON(pool->curr_nr >= pool->min_nr); 211da177e4SLinus Torvalds pool->elements[pool->curr_nr++] = element; 221da177e4SLinus Torvalds } 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds static void *remove_element(mempool_t *pool) 251da177e4SLinus Torvalds { 261da177e4SLinus Torvalds BUG_ON(pool->curr_nr <= 0); 271da177e4SLinus Torvalds return pool->elements[--pool->curr_nr]; 281da177e4SLinus Torvalds } 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds static void free_pool(mempool_t *pool) 311da177e4SLinus Torvalds { 321da177e4SLinus Torvalds while (pool->curr_nr) { 331da177e4SLinus Torvalds void *element = remove_element(pool); 341da177e4SLinus Torvalds pool->free(element, pool->pool_data); 351da177e4SLinus Torvalds } 361da177e4SLinus Torvalds kfree(pool->elements); 371da177e4SLinus Torvalds kfree(pool); 381da177e4SLinus Torvalds } 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds /** 411da177e4SLinus Torvalds * mempool_create - create a memory pool 421da177e4SLinus Torvalds * @min_nr: the minimum number of elements guaranteed to be 431da177e4SLinus Torvalds * allocated for this pool. 441da177e4SLinus Torvalds * @alloc_fn: user-defined element-allocation function. 451da177e4SLinus Torvalds * @free_fn: user-defined element-freeing function. 461da177e4SLinus Torvalds * @pool_data: optional private data available to the user-defined functions. 471da177e4SLinus Torvalds * 481da177e4SLinus Torvalds * this function creates and allocates a guaranteed size, preallocated 491da177e4SLinus Torvalds * memory pool. The pool can be used from the mempool_alloc and mempool_free 501da177e4SLinus Torvalds * functions. This function might sleep. Both the alloc_fn() and the free_fn() 511da177e4SLinus Torvalds * functions might sleep - as long as the mempool_alloc function is not called 521da177e4SLinus Torvalds * from IRQ contexts. 531da177e4SLinus Torvalds */ 541da177e4SLinus Torvalds mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn, 551da177e4SLinus Torvalds mempool_free_t *free_fn, void *pool_data) 561da177e4SLinus Torvalds { 571946089aSChristoph Lameter return mempool_create_node(min_nr,alloc_fn,free_fn, pool_data,-1); 581946089aSChristoph Lameter } 591946089aSChristoph Lameter EXPORT_SYMBOL(mempool_create); 601da177e4SLinus Torvalds 611946089aSChristoph Lameter mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn, 621946089aSChristoph Lameter mempool_free_t *free_fn, void *pool_data, int node_id) 631946089aSChristoph Lameter { 641946089aSChristoph Lameter mempool_t *pool; 651946089aSChristoph Lameter pool = kmalloc_node(sizeof(*pool), GFP_KERNEL, node_id); 661da177e4SLinus Torvalds if (!pool) 671da177e4SLinus Torvalds return NULL; 681da177e4SLinus Torvalds memset(pool, 0, sizeof(*pool)); 691946089aSChristoph Lameter pool->elements = kmalloc_node(min_nr * sizeof(void *), 701946089aSChristoph Lameter GFP_KERNEL, node_id); 711da177e4SLinus Torvalds if (!pool->elements) { 721da177e4SLinus Torvalds kfree(pool); 731da177e4SLinus Torvalds return NULL; 741da177e4SLinus Torvalds } 751da177e4SLinus Torvalds spin_lock_init(&pool->lock); 761da177e4SLinus Torvalds pool->min_nr = min_nr; 771da177e4SLinus Torvalds pool->pool_data = pool_data; 781da177e4SLinus Torvalds init_waitqueue_head(&pool->wait); 791da177e4SLinus Torvalds pool->alloc = alloc_fn; 801da177e4SLinus Torvalds pool->free = free_fn; 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds /* 831da177e4SLinus Torvalds * First pre-allocate the guaranteed number of buffers. 841da177e4SLinus Torvalds */ 851da177e4SLinus Torvalds while (pool->curr_nr < pool->min_nr) { 861da177e4SLinus Torvalds void *element; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds element = pool->alloc(GFP_KERNEL, pool->pool_data); 891da177e4SLinus Torvalds if (unlikely(!element)) { 901da177e4SLinus Torvalds free_pool(pool); 911da177e4SLinus Torvalds return NULL; 921da177e4SLinus Torvalds } 931da177e4SLinus Torvalds add_element(pool, element); 941da177e4SLinus Torvalds } 951da177e4SLinus Torvalds return pool; 961da177e4SLinus Torvalds } 971946089aSChristoph Lameter EXPORT_SYMBOL(mempool_create_node); 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds /** 1001da177e4SLinus Torvalds * mempool_resize - resize an existing memory pool 1011da177e4SLinus Torvalds * @pool: pointer to the memory pool which was allocated via 1021da177e4SLinus Torvalds * mempool_create(). 1031da177e4SLinus Torvalds * @new_min_nr: the new minimum number of elements guaranteed to be 1041da177e4SLinus Torvalds * allocated for this pool. 1051da177e4SLinus Torvalds * @gfp_mask: the usual allocation bitmask. 1061da177e4SLinus Torvalds * 1071da177e4SLinus Torvalds * This function shrinks/grows the pool. In the case of growing, 1081da177e4SLinus Torvalds * it cannot be guaranteed that the pool will be grown to the new 1091da177e4SLinus Torvalds * size immediately, but new mempool_free() calls will refill it. 1101da177e4SLinus Torvalds * 1111da177e4SLinus Torvalds * Note, the caller must guarantee that no mempool_destroy is called 1121da177e4SLinus Torvalds * while this function is running. mempool_alloc() & mempool_free() 1131da177e4SLinus Torvalds * might be called (eg. from IRQ contexts) while this function executes. 1141da177e4SLinus Torvalds */ 115dd0fc66fSAl Viro int mempool_resize(mempool_t *pool, int new_min_nr, gfp_t gfp_mask) 1161da177e4SLinus Torvalds { 1171da177e4SLinus Torvalds void *element; 1181da177e4SLinus Torvalds void **new_elements; 1191da177e4SLinus Torvalds unsigned long flags; 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds BUG_ON(new_min_nr <= 0); 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds spin_lock_irqsave(&pool->lock, flags); 1241da177e4SLinus Torvalds if (new_min_nr <= pool->min_nr) { 1251da177e4SLinus Torvalds while (new_min_nr < pool->curr_nr) { 1261da177e4SLinus Torvalds element = remove_element(pool); 1271da177e4SLinus Torvalds spin_unlock_irqrestore(&pool->lock, flags); 1281da177e4SLinus Torvalds pool->free(element, pool->pool_data); 1291da177e4SLinus Torvalds spin_lock_irqsave(&pool->lock, flags); 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds pool->min_nr = new_min_nr; 1321da177e4SLinus Torvalds goto out_unlock; 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds spin_unlock_irqrestore(&pool->lock, flags); 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds /* Grow the pool */ 1371da177e4SLinus Torvalds new_elements = kmalloc(new_min_nr * sizeof(*new_elements), gfp_mask); 1381da177e4SLinus Torvalds if (!new_elements) 1391da177e4SLinus Torvalds return -ENOMEM; 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds spin_lock_irqsave(&pool->lock, flags); 1421da177e4SLinus Torvalds if (unlikely(new_min_nr <= pool->min_nr)) { 1431da177e4SLinus Torvalds /* Raced, other resize will do our work */ 1441da177e4SLinus Torvalds spin_unlock_irqrestore(&pool->lock, flags); 1451da177e4SLinus Torvalds kfree(new_elements); 1461da177e4SLinus Torvalds goto out; 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds memcpy(new_elements, pool->elements, 1491da177e4SLinus Torvalds pool->curr_nr * sizeof(*new_elements)); 1501da177e4SLinus Torvalds kfree(pool->elements); 1511da177e4SLinus Torvalds pool->elements = new_elements; 1521da177e4SLinus Torvalds pool->min_nr = new_min_nr; 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds while (pool->curr_nr < pool->min_nr) { 1551da177e4SLinus Torvalds spin_unlock_irqrestore(&pool->lock, flags); 1561da177e4SLinus Torvalds element = pool->alloc(gfp_mask, pool->pool_data); 1571da177e4SLinus Torvalds if (!element) 1581da177e4SLinus Torvalds goto out; 1591da177e4SLinus Torvalds spin_lock_irqsave(&pool->lock, flags); 1601da177e4SLinus Torvalds if (pool->curr_nr < pool->min_nr) { 1611da177e4SLinus Torvalds add_element(pool, element); 1621da177e4SLinus Torvalds } else { 1631da177e4SLinus Torvalds spin_unlock_irqrestore(&pool->lock, flags); 1641da177e4SLinus Torvalds pool->free(element, pool->pool_data); /* Raced */ 1651da177e4SLinus Torvalds goto out; 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds out_unlock: 1691da177e4SLinus Torvalds spin_unlock_irqrestore(&pool->lock, flags); 1701da177e4SLinus Torvalds out: 1711da177e4SLinus Torvalds return 0; 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds EXPORT_SYMBOL(mempool_resize); 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds /** 1761da177e4SLinus Torvalds * mempool_destroy - deallocate a memory pool 1771da177e4SLinus Torvalds * @pool: pointer to the memory pool which was allocated via 1781da177e4SLinus Torvalds * mempool_create(). 1791da177e4SLinus Torvalds * 1801da177e4SLinus Torvalds * this function only sleeps if the free_fn() function sleeps. The caller 1811da177e4SLinus Torvalds * has to guarantee that all elements have been returned to the pool (ie: 1821da177e4SLinus Torvalds * freed) prior to calling mempool_destroy(). 1831da177e4SLinus Torvalds */ 1841da177e4SLinus Torvalds void mempool_destroy(mempool_t *pool) 1851da177e4SLinus Torvalds { 1861da177e4SLinus Torvalds if (pool->curr_nr != pool->min_nr) 1871da177e4SLinus Torvalds BUG(); /* There were outstanding elements */ 1881da177e4SLinus Torvalds free_pool(pool); 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds EXPORT_SYMBOL(mempool_destroy); 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds /** 1931da177e4SLinus Torvalds * mempool_alloc - allocate an element from a specific memory pool 1941da177e4SLinus Torvalds * @pool: pointer to the memory pool which was allocated via 1951da177e4SLinus Torvalds * mempool_create(). 1961da177e4SLinus Torvalds * @gfp_mask: the usual allocation bitmask. 1971da177e4SLinus Torvalds * 1981da177e4SLinus Torvalds * this function only sleeps if the alloc_fn function sleeps or 1991da177e4SLinus Torvalds * returns NULL. Note that due to preallocation, this function 2001da177e4SLinus Torvalds * *never* fails when called from process contexts. (it might 2011da177e4SLinus Torvalds * fail if called from an IRQ context.) 2021da177e4SLinus Torvalds */ 203dd0fc66fSAl Viro void * mempool_alloc(mempool_t *pool, gfp_t gfp_mask) 2041da177e4SLinus Torvalds { 2051da177e4SLinus Torvalds void *element; 2061da177e4SLinus Torvalds unsigned long flags; 20701890a4cSBenjamin LaHaise wait_queue_t wait; 2086daa0e28SAl Viro gfp_t gfp_temp; 20920a77776SNick Piggin 21020a77776SNick Piggin might_sleep_if(gfp_mask & __GFP_WAIT); 211b84a35beSNick Piggin 212b84a35beSNick Piggin gfp_mask |= __GFP_NOMEMALLOC; /* don't allocate emergency reserves */ 213b84a35beSNick Piggin gfp_mask |= __GFP_NORETRY; /* don't loop in __alloc_pages */ 214b84a35beSNick Piggin gfp_mask |= __GFP_NOWARN; /* failures are OK */ 2151da177e4SLinus Torvalds 21620a77776SNick Piggin gfp_temp = gfp_mask & ~(__GFP_WAIT|__GFP_IO); 21720a77776SNick Piggin 2181da177e4SLinus Torvalds repeat_alloc: 21920a77776SNick Piggin 22020a77776SNick Piggin element = pool->alloc(gfp_temp, pool->pool_data); 2211da177e4SLinus Torvalds if (likely(element != NULL)) 2221da177e4SLinus Torvalds return element; 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds spin_lock_irqsave(&pool->lock, flags); 2251da177e4SLinus Torvalds if (likely(pool->curr_nr)) { 2261da177e4SLinus Torvalds element = remove_element(pool); 2271da177e4SLinus Torvalds spin_unlock_irqrestore(&pool->lock, flags); 2281da177e4SLinus Torvalds return element; 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds spin_unlock_irqrestore(&pool->lock, flags); 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds /* We must not sleep in the GFP_ATOMIC case */ 2331da177e4SLinus Torvalds if (!(gfp_mask & __GFP_WAIT)) 2341da177e4SLinus Torvalds return NULL; 2351da177e4SLinus Torvalds 23620a77776SNick Piggin /* Now start performing page reclaim */ 23720a77776SNick Piggin gfp_temp = gfp_mask; 23801890a4cSBenjamin LaHaise init_wait(&wait); 2391da177e4SLinus Torvalds prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE); 240d59dd462Sakpm@osdl.org smp_mb(); 2411da177e4SLinus Torvalds if (!pool->curr_nr) 2421da177e4SLinus Torvalds io_schedule(); 2431da177e4SLinus Torvalds finish_wait(&pool->wait, &wait); 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds goto repeat_alloc; 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds EXPORT_SYMBOL(mempool_alloc); 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds /** 2501da177e4SLinus Torvalds * mempool_free - return an element to the pool. 2511da177e4SLinus Torvalds * @element: pool element pointer. 2521da177e4SLinus Torvalds * @pool: pointer to the memory pool which was allocated via 2531da177e4SLinus Torvalds * mempool_create(). 2541da177e4SLinus Torvalds * 2551da177e4SLinus Torvalds * this function only sleeps if the free_fn() function sleeps. 2561da177e4SLinus Torvalds */ 2571da177e4SLinus Torvalds void mempool_free(void *element, mempool_t *pool) 2581da177e4SLinus Torvalds { 2591da177e4SLinus Torvalds unsigned long flags; 2601da177e4SLinus Torvalds 261d59dd462Sakpm@osdl.org smp_mb(); 2621da177e4SLinus Torvalds if (pool->curr_nr < pool->min_nr) { 2631da177e4SLinus Torvalds spin_lock_irqsave(&pool->lock, flags); 2641da177e4SLinus Torvalds if (pool->curr_nr < pool->min_nr) { 2651da177e4SLinus Torvalds add_element(pool, element); 2661da177e4SLinus Torvalds spin_unlock_irqrestore(&pool->lock, flags); 2671da177e4SLinus Torvalds wake_up(&pool->wait); 2681da177e4SLinus Torvalds return; 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds spin_unlock_irqrestore(&pool->lock, flags); 2711da177e4SLinus Torvalds } 2721da177e4SLinus Torvalds pool->free(element, pool->pool_data); 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds EXPORT_SYMBOL(mempool_free); 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds /* 2771da177e4SLinus Torvalds * A commonly used alloc and free fn. 2781da177e4SLinus Torvalds */ 279dd0fc66fSAl Viro void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data) 2801da177e4SLinus Torvalds { 281fcc234f8SPekka Enberg struct kmem_cache *mem = pool_data; 2821da177e4SLinus Torvalds return kmem_cache_alloc(mem, gfp_mask); 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds EXPORT_SYMBOL(mempool_alloc_slab); 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds void mempool_free_slab(void *element, void *pool_data) 2871da177e4SLinus Torvalds { 288fcc234f8SPekka Enberg struct kmem_cache *mem = pool_data; 2891da177e4SLinus Torvalds kmem_cache_free(mem, element); 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds EXPORT_SYMBOL(mempool_free_slab); 2926e0678f3SMatthew Dobson 2936e0678f3SMatthew Dobson /* 294*53184082SMatthew Dobson * A commonly used alloc and free fn that kmalloc/kfrees the amount of memory 295*53184082SMatthew Dobson * specfied by pool_data 296*53184082SMatthew Dobson */ 297*53184082SMatthew Dobson void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data) 298*53184082SMatthew Dobson { 299*53184082SMatthew Dobson size_t size = (size_t) pool_data; 300*53184082SMatthew Dobson return kmalloc(size, gfp_mask); 301*53184082SMatthew Dobson } 302*53184082SMatthew Dobson EXPORT_SYMBOL(mempool_kmalloc); 303*53184082SMatthew Dobson 304*53184082SMatthew Dobson void mempool_kfree(void *element, void *pool_data) 305*53184082SMatthew Dobson { 306*53184082SMatthew Dobson kfree(element); 307*53184082SMatthew Dobson } 308*53184082SMatthew Dobson EXPORT_SYMBOL(mempool_kfree); 309*53184082SMatthew Dobson 310*53184082SMatthew Dobson /* 3116e0678f3SMatthew Dobson * A simple mempool-backed page allocator that allocates pages 3126e0678f3SMatthew Dobson * of the order specified by pool_data. 3136e0678f3SMatthew Dobson */ 3146e0678f3SMatthew Dobson void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data) 3156e0678f3SMatthew Dobson { 3166e0678f3SMatthew Dobson int order = (int)(long)pool_data; 3176e0678f3SMatthew Dobson return alloc_pages(gfp_mask, order); 3186e0678f3SMatthew Dobson } 3196e0678f3SMatthew Dobson EXPORT_SYMBOL(mempool_alloc_pages); 3206e0678f3SMatthew Dobson 3216e0678f3SMatthew Dobson void mempool_free_pages(void *element, void *pool_data) 3226e0678f3SMatthew Dobson { 3236e0678f3SMatthew Dobson int order = (int)(long)pool_data; 3246e0678f3SMatthew Dobson __free_pages(element, order); 3256e0678f3SMatthew Dobson } 3266e0678f3SMatthew Dobson EXPORT_SYMBOL(mempool_free_pages); 327