18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * linux/mm/mempool.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * memory buffer pool support. Such pools are mostly used 68c2ecf20Sopenharmony_ci * for guaranteed, deadlock-free memory allocations during 78c2ecf20Sopenharmony_ci * extreme VM load. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * started by Ingo Molnar, Copyright (C) 2001 108c2ecf20Sopenharmony_ci * debugging by David Rientjes, Copyright (C) 2015 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <linux/mm.h> 148c2ecf20Sopenharmony_ci#include <linux/slab.h> 158c2ecf20Sopenharmony_ci#include <linux/highmem.h> 168c2ecf20Sopenharmony_ci#include <linux/kasan.h> 178c2ecf20Sopenharmony_ci#include <linux/kmemleak.h> 188c2ecf20Sopenharmony_ci#include <linux/export.h> 198c2ecf20Sopenharmony_ci#include <linux/mempool.h> 208c2ecf20Sopenharmony_ci#include <linux/blkdev.h> 218c2ecf20Sopenharmony_ci#include <linux/writeback.h> 228c2ecf20Sopenharmony_ci#include "slab.h" 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB_DEBUG_ON) 258c2ecf20Sopenharmony_cistatic void poison_error(mempool_t *pool, void *element, size_t size, 268c2ecf20Sopenharmony_ci size_t byte) 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci const int nr = pool->curr_nr; 298c2ecf20Sopenharmony_ci const int start = max_t(int, byte - (BITS_PER_LONG / 8), 0); 308c2ecf20Sopenharmony_ci const int end = min_t(int, byte + (BITS_PER_LONG / 8), size); 318c2ecf20Sopenharmony_ci int i; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci pr_err("BUG: mempool element poison mismatch\n"); 348c2ecf20Sopenharmony_ci pr_err("Mempool %p size %zu\n", pool, size); 358c2ecf20Sopenharmony_ci pr_err(" nr=%d @ %p: %s0x", nr, element, start > 0 ? "... " : ""); 368c2ecf20Sopenharmony_ci for (i = start; i < end; i++) 378c2ecf20Sopenharmony_ci pr_cont("%x ", *(u8 *)(element + i)); 388c2ecf20Sopenharmony_ci pr_cont("%s\n", end < size ? "..." : ""); 398c2ecf20Sopenharmony_ci dump_stack(); 408c2ecf20Sopenharmony_ci} 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cistatic void __check_element(mempool_t *pool, void *element, size_t size) 438c2ecf20Sopenharmony_ci{ 448c2ecf20Sopenharmony_ci u8 *obj = element; 458c2ecf20Sopenharmony_ci size_t i; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci for (i = 0; i < size; i++) { 488c2ecf20Sopenharmony_ci u8 exp = (i < size - 1) ? POISON_FREE : POISON_END; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci if (obj[i] != exp) { 518c2ecf20Sopenharmony_ci poison_error(pool, element, size, i); 528c2ecf20Sopenharmony_ci return; 538c2ecf20Sopenharmony_ci } 548c2ecf20Sopenharmony_ci } 558c2ecf20Sopenharmony_ci memset(obj, POISON_INUSE, size); 568c2ecf20Sopenharmony_ci} 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistatic void check_element(mempool_t *pool, void *element) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci /* Mempools backed by slab allocator */ 618c2ecf20Sopenharmony_ci if (pool->free == mempool_free_slab || pool->free == mempool_kfree) { 628c2ecf20Sopenharmony_ci __check_element(pool, element, ksize(element)); 638c2ecf20Sopenharmony_ci } else if (pool->free == mempool_free_pages) { 648c2ecf20Sopenharmony_ci /* Mempools backed by page allocator */ 658c2ecf20Sopenharmony_ci int order = (int)(long)pool->pool_data; 668c2ecf20Sopenharmony_ci void *addr = kmap_atomic((struct page *)element); 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci __check_element(pool, addr, 1UL << (PAGE_SHIFT + order)); 698c2ecf20Sopenharmony_ci kunmap_atomic(addr); 708c2ecf20Sopenharmony_ci } 718c2ecf20Sopenharmony_ci} 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_cistatic void __poison_element(void *element, size_t size) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci u8 *obj = element; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci memset(obj, POISON_FREE, size - 1); 788c2ecf20Sopenharmony_ci obj[size - 1] = POISON_END; 798c2ecf20Sopenharmony_ci} 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cistatic void poison_element(mempool_t *pool, void *element) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci /* Mempools backed by slab allocator */ 848c2ecf20Sopenharmony_ci if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc) { 858c2ecf20Sopenharmony_ci __poison_element(element, ksize(element)); 868c2ecf20Sopenharmony_ci } else if (pool->alloc == mempool_alloc_pages) { 878c2ecf20Sopenharmony_ci /* Mempools backed by page allocator */ 888c2ecf20Sopenharmony_ci int order = (int)(long)pool->pool_data; 898c2ecf20Sopenharmony_ci void *addr = kmap_atomic((struct page *)element); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci __poison_element(addr, 1UL << (PAGE_SHIFT + order)); 928c2ecf20Sopenharmony_ci kunmap_atomic(addr); 938c2ecf20Sopenharmony_ci } 948c2ecf20Sopenharmony_ci} 958c2ecf20Sopenharmony_ci#else /* CONFIG_DEBUG_SLAB || CONFIG_SLUB_DEBUG_ON */ 968c2ecf20Sopenharmony_cistatic inline void check_element(mempool_t *pool, void *element) 978c2ecf20Sopenharmony_ci{ 988c2ecf20Sopenharmony_ci} 998c2ecf20Sopenharmony_cistatic inline void poison_element(mempool_t *pool, void *element) 1008c2ecf20Sopenharmony_ci{ 1018c2ecf20Sopenharmony_ci} 1028c2ecf20Sopenharmony_ci#endif /* CONFIG_DEBUG_SLAB || CONFIG_SLUB_DEBUG_ON */ 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistatic __always_inline void kasan_poison_element(mempool_t *pool, void *element) 1058c2ecf20Sopenharmony_ci{ 1068c2ecf20Sopenharmony_ci if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc) 1078c2ecf20Sopenharmony_ci kasan_poison_kfree(element, _RET_IP_); 1088c2ecf20Sopenharmony_ci else if (pool->alloc == mempool_alloc_pages) 1098c2ecf20Sopenharmony_ci kasan_free_pages(element, (unsigned long)pool->pool_data); 1108c2ecf20Sopenharmony_ci} 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_cistatic void kasan_unpoison_element(mempool_t *pool, void *element) 1138c2ecf20Sopenharmony_ci{ 1148c2ecf20Sopenharmony_ci if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc) 1158c2ecf20Sopenharmony_ci kasan_unpoison_slab(element); 1168c2ecf20Sopenharmony_ci else if (pool->alloc == mempool_alloc_pages) 1178c2ecf20Sopenharmony_ci kasan_alloc_pages(element, (unsigned long)pool->pool_data); 1188c2ecf20Sopenharmony_ci} 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_cistatic __always_inline void add_element(mempool_t *pool, void *element) 1218c2ecf20Sopenharmony_ci{ 1228c2ecf20Sopenharmony_ci BUG_ON(pool->curr_nr >= pool->min_nr); 1238c2ecf20Sopenharmony_ci poison_element(pool, element); 1248c2ecf20Sopenharmony_ci kasan_poison_element(pool, element); 1258c2ecf20Sopenharmony_ci pool->elements[pool->curr_nr++] = element; 1268c2ecf20Sopenharmony_ci} 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_cistatic void *remove_element(mempool_t *pool) 1298c2ecf20Sopenharmony_ci{ 1308c2ecf20Sopenharmony_ci void *element = pool->elements[--pool->curr_nr]; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci BUG_ON(pool->curr_nr < 0); 1338c2ecf20Sopenharmony_ci kasan_unpoison_element(pool, element); 1348c2ecf20Sopenharmony_ci check_element(pool, element); 1358c2ecf20Sopenharmony_ci return element; 1368c2ecf20Sopenharmony_ci} 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci/** 1398c2ecf20Sopenharmony_ci * mempool_exit - exit a mempool initialized with mempool_init() 1408c2ecf20Sopenharmony_ci * @pool: pointer to the memory pool which was initialized with 1418c2ecf20Sopenharmony_ci * mempool_init(). 1428c2ecf20Sopenharmony_ci * 1438c2ecf20Sopenharmony_ci * Free all reserved elements in @pool and @pool itself. This function 1448c2ecf20Sopenharmony_ci * only sleeps if the free_fn() function sleeps. 1458c2ecf20Sopenharmony_ci * 1468c2ecf20Sopenharmony_ci * May be called on a zeroed but uninitialized mempool (i.e. allocated with 1478c2ecf20Sopenharmony_ci * kzalloc()). 1488c2ecf20Sopenharmony_ci */ 1498c2ecf20Sopenharmony_civoid mempool_exit(mempool_t *pool) 1508c2ecf20Sopenharmony_ci{ 1518c2ecf20Sopenharmony_ci while (pool->curr_nr) { 1528c2ecf20Sopenharmony_ci void *element = remove_element(pool); 1538c2ecf20Sopenharmony_ci pool->free(element, pool->pool_data); 1548c2ecf20Sopenharmony_ci } 1558c2ecf20Sopenharmony_ci kfree(pool->elements); 1568c2ecf20Sopenharmony_ci pool->elements = NULL; 1578c2ecf20Sopenharmony_ci} 1588c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mempool_exit); 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci/** 1618c2ecf20Sopenharmony_ci * mempool_destroy - deallocate a memory pool 1628c2ecf20Sopenharmony_ci * @pool: pointer to the memory pool which was allocated via 1638c2ecf20Sopenharmony_ci * mempool_create(). 1648c2ecf20Sopenharmony_ci * 1658c2ecf20Sopenharmony_ci * Free all reserved elements in @pool and @pool itself. This function 1668c2ecf20Sopenharmony_ci * only sleeps if the free_fn() function sleeps. 1678c2ecf20Sopenharmony_ci */ 1688c2ecf20Sopenharmony_civoid mempool_destroy(mempool_t *pool) 1698c2ecf20Sopenharmony_ci{ 1708c2ecf20Sopenharmony_ci if (unlikely(!pool)) 1718c2ecf20Sopenharmony_ci return; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci mempool_exit(pool); 1748c2ecf20Sopenharmony_ci kfree(pool); 1758c2ecf20Sopenharmony_ci} 1768c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mempool_destroy); 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ciint mempool_init_node(mempool_t *pool, int min_nr, mempool_alloc_t *alloc_fn, 1798c2ecf20Sopenharmony_ci mempool_free_t *free_fn, void *pool_data, 1808c2ecf20Sopenharmony_ci gfp_t gfp_mask, int node_id) 1818c2ecf20Sopenharmony_ci{ 1828c2ecf20Sopenharmony_ci spin_lock_init(&pool->lock); 1838c2ecf20Sopenharmony_ci pool->min_nr = min_nr; 1848c2ecf20Sopenharmony_ci pool->pool_data = pool_data; 1858c2ecf20Sopenharmony_ci pool->alloc = alloc_fn; 1868c2ecf20Sopenharmony_ci pool->free = free_fn; 1878c2ecf20Sopenharmony_ci init_waitqueue_head(&pool->wait); 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci pool->elements = kmalloc_array_node(min_nr, sizeof(void *), 1908c2ecf20Sopenharmony_ci gfp_mask, node_id); 1918c2ecf20Sopenharmony_ci if (!pool->elements) 1928c2ecf20Sopenharmony_ci return -ENOMEM; 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci /* 1958c2ecf20Sopenharmony_ci * First pre-allocate the guaranteed number of buffers. 1968c2ecf20Sopenharmony_ci */ 1978c2ecf20Sopenharmony_ci while (pool->curr_nr < pool->min_nr) { 1988c2ecf20Sopenharmony_ci void *element; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci element = pool->alloc(gfp_mask, pool->pool_data); 2018c2ecf20Sopenharmony_ci if (unlikely(!element)) { 2028c2ecf20Sopenharmony_ci mempool_exit(pool); 2038c2ecf20Sopenharmony_ci return -ENOMEM; 2048c2ecf20Sopenharmony_ci } 2058c2ecf20Sopenharmony_ci add_element(pool, element); 2068c2ecf20Sopenharmony_ci } 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci return 0; 2098c2ecf20Sopenharmony_ci} 2108c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mempool_init_node); 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci/** 2138c2ecf20Sopenharmony_ci * mempool_init - initialize a memory pool 2148c2ecf20Sopenharmony_ci * @pool: pointer to the memory pool that should be initialized 2158c2ecf20Sopenharmony_ci * @min_nr: the minimum number of elements guaranteed to be 2168c2ecf20Sopenharmony_ci * allocated for this pool. 2178c2ecf20Sopenharmony_ci * @alloc_fn: user-defined element-allocation function. 2188c2ecf20Sopenharmony_ci * @free_fn: user-defined element-freeing function. 2198c2ecf20Sopenharmony_ci * @pool_data: optional private data available to the user-defined functions. 2208c2ecf20Sopenharmony_ci * 2218c2ecf20Sopenharmony_ci * Like mempool_create(), but initializes the pool in (i.e. embedded in another 2228c2ecf20Sopenharmony_ci * structure). 2238c2ecf20Sopenharmony_ci * 2248c2ecf20Sopenharmony_ci * Return: %0 on success, negative error code otherwise. 2258c2ecf20Sopenharmony_ci */ 2268c2ecf20Sopenharmony_ciint mempool_init(mempool_t *pool, int min_nr, mempool_alloc_t *alloc_fn, 2278c2ecf20Sopenharmony_ci mempool_free_t *free_fn, void *pool_data) 2288c2ecf20Sopenharmony_ci{ 2298c2ecf20Sopenharmony_ci return mempool_init_node(pool, min_nr, alloc_fn, free_fn, 2308c2ecf20Sopenharmony_ci pool_data, GFP_KERNEL, NUMA_NO_NODE); 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci} 2338c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mempool_init); 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci/** 2368c2ecf20Sopenharmony_ci * mempool_create - create a memory pool 2378c2ecf20Sopenharmony_ci * @min_nr: the minimum number of elements guaranteed to be 2388c2ecf20Sopenharmony_ci * allocated for this pool. 2398c2ecf20Sopenharmony_ci * @alloc_fn: user-defined element-allocation function. 2408c2ecf20Sopenharmony_ci * @free_fn: user-defined element-freeing function. 2418c2ecf20Sopenharmony_ci * @pool_data: optional private data available to the user-defined functions. 2428c2ecf20Sopenharmony_ci * 2438c2ecf20Sopenharmony_ci * this function creates and allocates a guaranteed size, preallocated 2448c2ecf20Sopenharmony_ci * memory pool. The pool can be used from the mempool_alloc() and mempool_free() 2458c2ecf20Sopenharmony_ci * functions. This function might sleep. Both the alloc_fn() and the free_fn() 2468c2ecf20Sopenharmony_ci * functions might sleep - as long as the mempool_alloc() function is not called 2478c2ecf20Sopenharmony_ci * from IRQ contexts. 2488c2ecf20Sopenharmony_ci * 2498c2ecf20Sopenharmony_ci * Return: pointer to the created memory pool object or %NULL on error. 2508c2ecf20Sopenharmony_ci */ 2518c2ecf20Sopenharmony_cimempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn, 2528c2ecf20Sopenharmony_ci mempool_free_t *free_fn, void *pool_data) 2538c2ecf20Sopenharmony_ci{ 2548c2ecf20Sopenharmony_ci return mempool_create_node(min_nr,alloc_fn,free_fn, pool_data, 2558c2ecf20Sopenharmony_ci GFP_KERNEL, NUMA_NO_NODE); 2568c2ecf20Sopenharmony_ci} 2578c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mempool_create); 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_cimempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn, 2608c2ecf20Sopenharmony_ci mempool_free_t *free_fn, void *pool_data, 2618c2ecf20Sopenharmony_ci gfp_t gfp_mask, int node_id) 2628c2ecf20Sopenharmony_ci{ 2638c2ecf20Sopenharmony_ci mempool_t *pool; 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci pool = kzalloc_node(sizeof(*pool), gfp_mask, node_id); 2668c2ecf20Sopenharmony_ci if (!pool) 2678c2ecf20Sopenharmony_ci return NULL; 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci if (mempool_init_node(pool, min_nr, alloc_fn, free_fn, pool_data, 2708c2ecf20Sopenharmony_ci gfp_mask, node_id)) { 2718c2ecf20Sopenharmony_ci kfree(pool); 2728c2ecf20Sopenharmony_ci return NULL; 2738c2ecf20Sopenharmony_ci } 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci return pool; 2768c2ecf20Sopenharmony_ci} 2778c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mempool_create_node); 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci/** 2808c2ecf20Sopenharmony_ci * mempool_resize - resize an existing memory pool 2818c2ecf20Sopenharmony_ci * @pool: pointer to the memory pool which was allocated via 2828c2ecf20Sopenharmony_ci * mempool_create(). 2838c2ecf20Sopenharmony_ci * @new_min_nr: the new minimum number of elements guaranteed to be 2848c2ecf20Sopenharmony_ci * allocated for this pool. 2858c2ecf20Sopenharmony_ci * 2868c2ecf20Sopenharmony_ci * This function shrinks/grows the pool. In the case of growing, 2878c2ecf20Sopenharmony_ci * it cannot be guaranteed that the pool will be grown to the new 2888c2ecf20Sopenharmony_ci * size immediately, but new mempool_free() calls will refill it. 2898c2ecf20Sopenharmony_ci * This function may sleep. 2908c2ecf20Sopenharmony_ci * 2918c2ecf20Sopenharmony_ci * Note, the caller must guarantee that no mempool_destroy is called 2928c2ecf20Sopenharmony_ci * while this function is running. mempool_alloc() & mempool_free() 2938c2ecf20Sopenharmony_ci * might be called (eg. from IRQ contexts) while this function executes. 2948c2ecf20Sopenharmony_ci * 2958c2ecf20Sopenharmony_ci * Return: %0 on success, negative error code otherwise. 2968c2ecf20Sopenharmony_ci */ 2978c2ecf20Sopenharmony_ciint mempool_resize(mempool_t *pool, int new_min_nr) 2988c2ecf20Sopenharmony_ci{ 2998c2ecf20Sopenharmony_ci void *element; 3008c2ecf20Sopenharmony_ci void **new_elements; 3018c2ecf20Sopenharmony_ci unsigned long flags; 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci BUG_ON(new_min_nr <= 0); 3048c2ecf20Sopenharmony_ci might_sleep(); 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci spin_lock_irqsave(&pool->lock, flags); 3078c2ecf20Sopenharmony_ci if (new_min_nr <= pool->min_nr) { 3088c2ecf20Sopenharmony_ci while (new_min_nr < pool->curr_nr) { 3098c2ecf20Sopenharmony_ci element = remove_element(pool); 3108c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&pool->lock, flags); 3118c2ecf20Sopenharmony_ci pool->free(element, pool->pool_data); 3128c2ecf20Sopenharmony_ci spin_lock_irqsave(&pool->lock, flags); 3138c2ecf20Sopenharmony_ci } 3148c2ecf20Sopenharmony_ci pool->min_nr = new_min_nr; 3158c2ecf20Sopenharmony_ci goto out_unlock; 3168c2ecf20Sopenharmony_ci } 3178c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&pool->lock, flags); 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci /* Grow the pool */ 3208c2ecf20Sopenharmony_ci new_elements = kmalloc_array(new_min_nr, sizeof(*new_elements), 3218c2ecf20Sopenharmony_ci GFP_KERNEL); 3228c2ecf20Sopenharmony_ci if (!new_elements) 3238c2ecf20Sopenharmony_ci return -ENOMEM; 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci spin_lock_irqsave(&pool->lock, flags); 3268c2ecf20Sopenharmony_ci if (unlikely(new_min_nr <= pool->min_nr)) { 3278c2ecf20Sopenharmony_ci /* Raced, other resize will do our work */ 3288c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&pool->lock, flags); 3298c2ecf20Sopenharmony_ci kfree(new_elements); 3308c2ecf20Sopenharmony_ci goto out; 3318c2ecf20Sopenharmony_ci } 3328c2ecf20Sopenharmony_ci memcpy(new_elements, pool->elements, 3338c2ecf20Sopenharmony_ci pool->curr_nr * sizeof(*new_elements)); 3348c2ecf20Sopenharmony_ci kfree(pool->elements); 3358c2ecf20Sopenharmony_ci pool->elements = new_elements; 3368c2ecf20Sopenharmony_ci pool->min_nr = new_min_nr; 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci while (pool->curr_nr < pool->min_nr) { 3398c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&pool->lock, flags); 3408c2ecf20Sopenharmony_ci element = pool->alloc(GFP_KERNEL, pool->pool_data); 3418c2ecf20Sopenharmony_ci if (!element) 3428c2ecf20Sopenharmony_ci goto out; 3438c2ecf20Sopenharmony_ci spin_lock_irqsave(&pool->lock, flags); 3448c2ecf20Sopenharmony_ci if (pool->curr_nr < pool->min_nr) { 3458c2ecf20Sopenharmony_ci add_element(pool, element); 3468c2ecf20Sopenharmony_ci } else { 3478c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&pool->lock, flags); 3488c2ecf20Sopenharmony_ci pool->free(element, pool->pool_data); /* Raced */ 3498c2ecf20Sopenharmony_ci goto out; 3508c2ecf20Sopenharmony_ci } 3518c2ecf20Sopenharmony_ci } 3528c2ecf20Sopenharmony_ciout_unlock: 3538c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&pool->lock, flags); 3548c2ecf20Sopenharmony_ciout: 3558c2ecf20Sopenharmony_ci return 0; 3568c2ecf20Sopenharmony_ci} 3578c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mempool_resize); 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci/** 3608c2ecf20Sopenharmony_ci * mempool_alloc - allocate an element from a specific memory pool 3618c2ecf20Sopenharmony_ci * @pool: pointer to the memory pool which was allocated via 3628c2ecf20Sopenharmony_ci * mempool_create(). 3638c2ecf20Sopenharmony_ci * @gfp_mask: the usual allocation bitmask. 3648c2ecf20Sopenharmony_ci * 3658c2ecf20Sopenharmony_ci * this function only sleeps if the alloc_fn() function sleeps or 3668c2ecf20Sopenharmony_ci * returns NULL. Note that due to preallocation, this function 3678c2ecf20Sopenharmony_ci * *never* fails when called from process contexts. (it might 3688c2ecf20Sopenharmony_ci * fail if called from an IRQ context.) 3698c2ecf20Sopenharmony_ci * Note: using __GFP_ZERO is not supported. 3708c2ecf20Sopenharmony_ci * 3718c2ecf20Sopenharmony_ci * Return: pointer to the allocated element or %NULL on error. 3728c2ecf20Sopenharmony_ci */ 3738c2ecf20Sopenharmony_civoid *mempool_alloc(mempool_t *pool, gfp_t gfp_mask) 3748c2ecf20Sopenharmony_ci{ 3758c2ecf20Sopenharmony_ci void *element; 3768c2ecf20Sopenharmony_ci unsigned long flags; 3778c2ecf20Sopenharmony_ci wait_queue_entry_t wait; 3788c2ecf20Sopenharmony_ci gfp_t gfp_temp; 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci VM_WARN_ON_ONCE(gfp_mask & __GFP_ZERO); 3818c2ecf20Sopenharmony_ci might_sleep_if(gfp_mask & __GFP_DIRECT_RECLAIM); 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci gfp_mask |= __GFP_NOMEMALLOC; /* don't allocate emergency reserves */ 3848c2ecf20Sopenharmony_ci gfp_mask |= __GFP_NORETRY; /* don't loop in __alloc_pages */ 3858c2ecf20Sopenharmony_ci gfp_mask |= __GFP_NOWARN; /* failures are OK */ 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci gfp_temp = gfp_mask & ~(__GFP_DIRECT_RECLAIM|__GFP_IO); 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_cirepeat_alloc: 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci element = pool->alloc(gfp_temp, pool->pool_data); 3928c2ecf20Sopenharmony_ci if (likely(element != NULL)) 3938c2ecf20Sopenharmony_ci return element; 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci spin_lock_irqsave(&pool->lock, flags); 3968c2ecf20Sopenharmony_ci if (likely(pool->curr_nr)) { 3978c2ecf20Sopenharmony_ci element = remove_element(pool); 3988c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&pool->lock, flags); 3998c2ecf20Sopenharmony_ci /* paired with rmb in mempool_free(), read comment there */ 4008c2ecf20Sopenharmony_ci smp_wmb(); 4018c2ecf20Sopenharmony_ci /* 4028c2ecf20Sopenharmony_ci * Update the allocation stack trace as this is more useful 4038c2ecf20Sopenharmony_ci * for debugging. 4048c2ecf20Sopenharmony_ci */ 4058c2ecf20Sopenharmony_ci kmemleak_update_trace(element); 4068c2ecf20Sopenharmony_ci return element; 4078c2ecf20Sopenharmony_ci } 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci /* 4108c2ecf20Sopenharmony_ci * We use gfp mask w/o direct reclaim or IO for the first round. If 4118c2ecf20Sopenharmony_ci * alloc failed with that and @pool was empty, retry immediately. 4128c2ecf20Sopenharmony_ci */ 4138c2ecf20Sopenharmony_ci if (gfp_temp != gfp_mask) { 4148c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&pool->lock, flags); 4158c2ecf20Sopenharmony_ci gfp_temp = gfp_mask; 4168c2ecf20Sopenharmony_ci goto repeat_alloc; 4178c2ecf20Sopenharmony_ci } 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ci /* We must not sleep if !__GFP_DIRECT_RECLAIM */ 4208c2ecf20Sopenharmony_ci if (!(gfp_mask & __GFP_DIRECT_RECLAIM)) { 4218c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&pool->lock, flags); 4228c2ecf20Sopenharmony_ci return NULL; 4238c2ecf20Sopenharmony_ci } 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci /* Let's wait for someone else to return an element to @pool */ 4268c2ecf20Sopenharmony_ci init_wait(&wait); 4278c2ecf20Sopenharmony_ci prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE); 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&pool->lock, flags); 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ci /* 4328c2ecf20Sopenharmony_ci * FIXME: this should be io_schedule(). The timeout is there as a 4338c2ecf20Sopenharmony_ci * workaround for some DM problems in 2.6.18. 4348c2ecf20Sopenharmony_ci */ 4358c2ecf20Sopenharmony_ci io_schedule_timeout(5*HZ); 4368c2ecf20Sopenharmony_ci 4378c2ecf20Sopenharmony_ci finish_wait(&pool->wait, &wait); 4388c2ecf20Sopenharmony_ci goto repeat_alloc; 4398c2ecf20Sopenharmony_ci} 4408c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mempool_alloc); 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci/** 4438c2ecf20Sopenharmony_ci * mempool_free - return an element to the pool. 4448c2ecf20Sopenharmony_ci * @element: pool element pointer. 4458c2ecf20Sopenharmony_ci * @pool: pointer to the memory pool which was allocated via 4468c2ecf20Sopenharmony_ci * mempool_create(). 4478c2ecf20Sopenharmony_ci * 4488c2ecf20Sopenharmony_ci * this function only sleeps if the free_fn() function sleeps. 4498c2ecf20Sopenharmony_ci */ 4508c2ecf20Sopenharmony_civoid mempool_free(void *element, mempool_t *pool) 4518c2ecf20Sopenharmony_ci{ 4528c2ecf20Sopenharmony_ci unsigned long flags; 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_ci if (unlikely(element == NULL)) 4558c2ecf20Sopenharmony_ci return; 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_ci /* 4588c2ecf20Sopenharmony_ci * Paired with the wmb in mempool_alloc(). The preceding read is 4598c2ecf20Sopenharmony_ci * for @element and the following @pool->curr_nr. This ensures 4608c2ecf20Sopenharmony_ci * that the visible value of @pool->curr_nr is from after the 4618c2ecf20Sopenharmony_ci * allocation of @element. This is necessary for fringe cases 4628c2ecf20Sopenharmony_ci * where @element was passed to this task without going through 4638c2ecf20Sopenharmony_ci * barriers. 4648c2ecf20Sopenharmony_ci * 4658c2ecf20Sopenharmony_ci * For example, assume @p is %NULL at the beginning and one task 4668c2ecf20Sopenharmony_ci * performs "p = mempool_alloc(...);" while another task is doing 4678c2ecf20Sopenharmony_ci * "while (!p) cpu_relax(); mempool_free(p, ...);". This function 4688c2ecf20Sopenharmony_ci * may end up using curr_nr value which is from before allocation 4698c2ecf20Sopenharmony_ci * of @p without the following rmb. 4708c2ecf20Sopenharmony_ci */ 4718c2ecf20Sopenharmony_ci smp_rmb(); 4728c2ecf20Sopenharmony_ci 4738c2ecf20Sopenharmony_ci /* 4748c2ecf20Sopenharmony_ci * For correctness, we need a test which is guaranteed to trigger 4758c2ecf20Sopenharmony_ci * if curr_nr + #allocated == min_nr. Testing curr_nr < min_nr 4768c2ecf20Sopenharmony_ci * without locking achieves that and refilling as soon as possible 4778c2ecf20Sopenharmony_ci * is desirable. 4788c2ecf20Sopenharmony_ci * 4798c2ecf20Sopenharmony_ci * Because curr_nr visible here is always a value after the 4808c2ecf20Sopenharmony_ci * allocation of @element, any task which decremented curr_nr below 4818c2ecf20Sopenharmony_ci * min_nr is guaranteed to see curr_nr < min_nr unless curr_nr gets 4828c2ecf20Sopenharmony_ci * incremented to min_nr afterwards. If curr_nr gets incremented 4838c2ecf20Sopenharmony_ci * to min_nr after the allocation of @element, the elements 4848c2ecf20Sopenharmony_ci * allocated after that are subject to the same guarantee. 4858c2ecf20Sopenharmony_ci * 4868c2ecf20Sopenharmony_ci * Waiters happen iff curr_nr is 0 and the above guarantee also 4878c2ecf20Sopenharmony_ci * ensures that there will be frees which return elements to the 4888c2ecf20Sopenharmony_ci * pool waking up the waiters. 4898c2ecf20Sopenharmony_ci */ 4908c2ecf20Sopenharmony_ci if (unlikely(READ_ONCE(pool->curr_nr) < pool->min_nr)) { 4918c2ecf20Sopenharmony_ci spin_lock_irqsave(&pool->lock, flags); 4928c2ecf20Sopenharmony_ci if (likely(pool->curr_nr < pool->min_nr)) { 4938c2ecf20Sopenharmony_ci add_element(pool, element); 4948c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&pool->lock, flags); 4958c2ecf20Sopenharmony_ci wake_up(&pool->wait); 4968c2ecf20Sopenharmony_ci return; 4978c2ecf20Sopenharmony_ci } 4988c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&pool->lock, flags); 4998c2ecf20Sopenharmony_ci } 5008c2ecf20Sopenharmony_ci pool->free(element, pool->pool_data); 5018c2ecf20Sopenharmony_ci} 5028c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mempool_free); 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci/* 5058c2ecf20Sopenharmony_ci * A commonly used alloc and free fn. 5068c2ecf20Sopenharmony_ci */ 5078c2ecf20Sopenharmony_civoid *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data) 5088c2ecf20Sopenharmony_ci{ 5098c2ecf20Sopenharmony_ci struct kmem_cache *mem = pool_data; 5108c2ecf20Sopenharmony_ci VM_BUG_ON(mem->ctor); 5118c2ecf20Sopenharmony_ci return kmem_cache_alloc(mem, gfp_mask); 5128c2ecf20Sopenharmony_ci} 5138c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mempool_alloc_slab); 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_civoid mempool_free_slab(void *element, void *pool_data) 5168c2ecf20Sopenharmony_ci{ 5178c2ecf20Sopenharmony_ci struct kmem_cache *mem = pool_data; 5188c2ecf20Sopenharmony_ci kmem_cache_free(mem, element); 5198c2ecf20Sopenharmony_ci} 5208c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mempool_free_slab); 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci/* 5238c2ecf20Sopenharmony_ci * A commonly used alloc and free fn that kmalloc/kfrees the amount of memory 5248c2ecf20Sopenharmony_ci * specified by pool_data 5258c2ecf20Sopenharmony_ci */ 5268c2ecf20Sopenharmony_civoid *mempool_kmalloc(gfp_t gfp_mask, void *pool_data) 5278c2ecf20Sopenharmony_ci{ 5288c2ecf20Sopenharmony_ci size_t size = (size_t)pool_data; 5298c2ecf20Sopenharmony_ci return kmalloc(size, gfp_mask); 5308c2ecf20Sopenharmony_ci} 5318c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mempool_kmalloc); 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_civoid mempool_kfree(void *element, void *pool_data) 5348c2ecf20Sopenharmony_ci{ 5358c2ecf20Sopenharmony_ci kfree(element); 5368c2ecf20Sopenharmony_ci} 5378c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mempool_kfree); 5388c2ecf20Sopenharmony_ci 5398c2ecf20Sopenharmony_ci/* 5408c2ecf20Sopenharmony_ci * A simple mempool-backed page allocator that allocates pages 5418c2ecf20Sopenharmony_ci * of the order specified by pool_data. 5428c2ecf20Sopenharmony_ci */ 5438c2ecf20Sopenharmony_civoid *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data) 5448c2ecf20Sopenharmony_ci{ 5458c2ecf20Sopenharmony_ci int order = (int)(long)pool_data; 5468c2ecf20Sopenharmony_ci return alloc_pages(gfp_mask, order); 5478c2ecf20Sopenharmony_ci} 5488c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mempool_alloc_pages); 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_civoid mempool_free_pages(void *element, void *pool_data) 5518c2ecf20Sopenharmony_ci{ 5528c2ecf20Sopenharmony_ci int order = (int)(long)pool_data; 5538c2ecf20Sopenharmony_ci __free_pages(element, order); 5548c2ecf20Sopenharmony_ci} 5558c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mempool_free_pages); 556