123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- #include <linux/mm.h>
- #include <linux/slab.h>
- #include <linux/highmem.h>
- #include <linux/kasan.h>
- #include <linux/kmemleak.h>
- #include <linux/export.h>
- #include <linux/mempool.h>
- #include <linux/blkdev.h>
- #include <linux/writeback.h>
- #include "slab.h"
- #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB_DEBUG_ON)
- static void poison_error(mempool_t *pool, void *element, size_t size,
- size_t byte)
- {
- const int nr = pool->curr_nr;
- const int start = max_t(int, byte - (BITS_PER_LONG / 8), 0);
- const int end = min_t(int, byte + (BITS_PER_LONG / 8), size);
- int i;
- pr_err("BUG: mempool element poison mismatch\n");
- pr_err("Mempool %p size %zu\n", pool, size);
- pr_err(" nr=%d @ %p: %s0x", nr, element, start > 0 ? "... " : "");
- for (i = start; i < end; i++)
- pr_cont("%x ", *(u8 *)(element + i));
- pr_cont("%s\n", end < size ? "..." : "");
- dump_stack();
- }
- static void __check_element(mempool_t *pool, void *element, size_t size)
- {
- u8 *obj = element;
- size_t i;
- for (i = 0; i < size; i++) {
- u8 exp = (i < size - 1) ? POISON_FREE : POISON_END;
- if (obj[i] != exp) {
- poison_error(pool, element, size, i);
- return;
- }
- }
- memset(obj, POISON_INUSE, size);
- }
- static void check_element(mempool_t *pool, void *element)
- {
-
- if (pool->free == mempool_free_slab || pool->free == mempool_kfree)
- __check_element(pool, element, ksize(element));
-
- if (pool->free == mempool_free_pages) {
- int order = (int)(long)pool->pool_data;
- void *addr = kmap_atomic((struct page *)element);
- __check_element(pool, addr, 1UL << (PAGE_SHIFT + order));
- kunmap_atomic(addr);
- }
- }
- static void __poison_element(void *element, size_t size)
- {
- u8 *obj = element;
- memset(obj, POISON_FREE, size - 1);
- obj[size - 1] = POISON_END;
- }
- static void poison_element(mempool_t *pool, void *element)
- {
-
- if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
- __poison_element(element, ksize(element));
-
- if (pool->alloc == mempool_alloc_pages) {
- int order = (int)(long)pool->pool_data;
- void *addr = kmap_atomic((struct page *)element);
- __poison_element(addr, 1UL << (PAGE_SHIFT + order));
- kunmap_atomic(addr);
- }
- }
- #else
- static inline void check_element(mempool_t *pool, void *element)
- {
- }
- static inline void poison_element(mempool_t *pool, void *element)
- {
- }
- #endif
- static void kasan_poison_element(mempool_t *pool, void *element)
- {
- if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
- kasan_poison_kfree(element);
- if (pool->alloc == mempool_alloc_pages)
- kasan_free_pages(element, (unsigned long)pool->pool_data);
- }
- static void kasan_unpoison_element(mempool_t *pool, void *element, gfp_t flags)
- {
- if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
- kasan_unpoison_slab(element);
- if (pool->alloc == mempool_alloc_pages)
- kasan_alloc_pages(element, (unsigned long)pool->pool_data);
- }
- static void add_element(mempool_t *pool, void *element)
- {
- BUG_ON(pool->curr_nr >= pool->min_nr);
- poison_element(pool, element);
- kasan_poison_element(pool, element);
- pool->elements[pool->curr_nr++] = element;
- }
- static void *remove_element(mempool_t *pool, gfp_t flags)
- {
- void *element = pool->elements[--pool->curr_nr];
- BUG_ON(pool->curr_nr < 0);
- kasan_unpoison_element(pool, element, flags);
- check_element(pool, element);
- return element;
- }
- void mempool_destroy(mempool_t *pool)
- {
- if (unlikely(!pool))
- return;
- while (pool->curr_nr) {
- void *element = remove_element(pool, GFP_KERNEL);
- pool->free(element, pool->pool_data);
- }
- kfree(pool->elements);
- kfree(pool);
- }
- EXPORT_SYMBOL(mempool_destroy);
- mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
- mempool_free_t *free_fn, void *pool_data)
- {
- return mempool_create_node(min_nr,alloc_fn,free_fn, pool_data,
- GFP_KERNEL, NUMA_NO_NODE);
- }
- EXPORT_SYMBOL(mempool_create);
- mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
- mempool_free_t *free_fn, void *pool_data,
- gfp_t gfp_mask, int node_id)
- {
- mempool_t *pool;
- pool = kzalloc_node(sizeof(*pool), gfp_mask, node_id);
- if (!pool)
- return NULL;
- pool->elements = kmalloc_node(min_nr * sizeof(void *),
- gfp_mask, node_id);
- if (!pool->elements) {
- kfree(pool);
- return NULL;
- }
- spin_lock_init(&pool->lock);
- pool->min_nr = min_nr;
- pool->pool_data = pool_data;
- init_waitqueue_head(&pool->wait);
- pool->alloc = alloc_fn;
- pool->free = free_fn;
-
- while (pool->curr_nr < pool->min_nr) {
- void *element;
- element = pool->alloc(gfp_mask, pool->pool_data);
- if (unlikely(!element)) {
- mempool_destroy(pool);
- return NULL;
- }
- add_element(pool, element);
- }
- return pool;
- }
- EXPORT_SYMBOL(mempool_create_node);
- int mempool_resize(mempool_t *pool, int new_min_nr)
- {
- void *element;
- void **new_elements;
- unsigned long flags;
- BUG_ON(new_min_nr <= 0);
- might_sleep();
- spin_lock_irqsave(&pool->lock, flags);
- if (new_min_nr <= pool->min_nr) {
- while (new_min_nr < pool->curr_nr) {
- element = remove_element(pool, GFP_KERNEL);
- spin_unlock_irqrestore(&pool->lock, flags);
- pool->free(element, pool->pool_data);
- spin_lock_irqsave(&pool->lock, flags);
- }
- pool->min_nr = new_min_nr;
- goto out_unlock;
- }
- spin_unlock_irqrestore(&pool->lock, flags);
-
- new_elements = kmalloc_array(new_min_nr, sizeof(*new_elements),
- GFP_KERNEL);
- if (!new_elements)
- return -ENOMEM;
- spin_lock_irqsave(&pool->lock, flags);
- if (unlikely(new_min_nr <= pool->min_nr)) {
-
- spin_unlock_irqrestore(&pool->lock, flags);
- kfree(new_elements);
- goto out;
- }
- memcpy(new_elements, pool->elements,
- pool->curr_nr * sizeof(*new_elements));
- kfree(pool->elements);
- pool->elements = new_elements;
- pool->min_nr = new_min_nr;
- while (pool->curr_nr < pool->min_nr) {
- spin_unlock_irqrestore(&pool->lock, flags);
- element = pool->alloc(GFP_KERNEL, pool->pool_data);
- if (!element)
- goto out;
- spin_lock_irqsave(&pool->lock, flags);
- if (pool->curr_nr < pool->min_nr) {
- add_element(pool, element);
- } else {
- spin_unlock_irqrestore(&pool->lock, flags);
- pool->free(element, pool->pool_data);
- goto out;
- }
- }
- out_unlock:
- spin_unlock_irqrestore(&pool->lock, flags);
- out:
- return 0;
- }
- EXPORT_SYMBOL(mempool_resize);
- void *mempool_alloc(mempool_t *pool, gfp_t gfp_mask)
- {
- void *element;
- unsigned long flags;
- wait_queue_t wait;
- gfp_t gfp_temp;
- VM_WARN_ON_ONCE(gfp_mask & __GFP_ZERO);
- might_sleep_if(gfp_mask & __GFP_DIRECT_RECLAIM);
- gfp_mask |= __GFP_NOMEMALLOC;
- gfp_mask |= __GFP_NORETRY;
- gfp_mask |= __GFP_NOWARN;
- gfp_temp = gfp_mask & ~(__GFP_DIRECT_RECLAIM|__GFP_IO);
- repeat_alloc:
- element = pool->alloc(gfp_temp, pool->pool_data);
- if (likely(element != NULL))
- return element;
- spin_lock_irqsave(&pool->lock, flags);
- if (likely(pool->curr_nr)) {
- element = remove_element(pool, gfp_temp);
- spin_unlock_irqrestore(&pool->lock, flags);
-
- smp_wmb();
-
- kmemleak_update_trace(element);
- return element;
- }
-
- if (gfp_temp != gfp_mask) {
- spin_unlock_irqrestore(&pool->lock, flags);
- gfp_temp = gfp_mask;
- goto repeat_alloc;
- }
-
- if (!(gfp_mask & __GFP_DIRECT_RECLAIM)) {
- spin_unlock_irqrestore(&pool->lock, flags);
- return NULL;
- }
-
- init_wait(&wait);
- prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
- spin_unlock_irqrestore(&pool->lock, flags);
-
- io_schedule_timeout(5*HZ);
- finish_wait(&pool->wait, &wait);
- goto repeat_alloc;
- }
- EXPORT_SYMBOL(mempool_alloc);
- void mempool_free(void *element, mempool_t *pool)
- {
- unsigned long flags;
- if (unlikely(element == NULL))
- return;
-
- smp_rmb();
-
- if (unlikely(pool->curr_nr < pool->min_nr)) {
- spin_lock_irqsave(&pool->lock, flags);
- if (likely(pool->curr_nr < pool->min_nr)) {
- add_element(pool, element);
- spin_unlock_irqrestore(&pool->lock, flags);
- wake_up(&pool->wait);
- return;
- }
- spin_unlock_irqrestore(&pool->lock, flags);
- }
- pool->free(element, pool->pool_data);
- }
- EXPORT_SYMBOL(mempool_free);
- void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data)
- {
- struct kmem_cache *mem = pool_data;
- VM_BUG_ON(mem->ctor);
- return kmem_cache_alloc(mem, gfp_mask);
- }
- EXPORT_SYMBOL(mempool_alloc_slab);
- void mempool_free_slab(void *element, void *pool_data)
- {
- struct kmem_cache *mem = pool_data;
- kmem_cache_free(mem, element);
- }
- EXPORT_SYMBOL(mempool_free_slab);
- void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data)
- {
- size_t size = (size_t)pool_data;
- return kmalloc(size, gfp_mask);
- }
- EXPORT_SYMBOL(mempool_kmalloc);
- void mempool_kfree(void *element, void *pool_data)
- {
- kfree(element);
- }
- EXPORT_SYMBOL(mempool_kfree);
- void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data)
- {
- int order = (int)(long)pool_data;
- return alloc_pages(gfp_mask, order);
- }
- EXPORT_SYMBOL(mempool_alloc_pages);
- void mempool_free_pages(void *element, void *pool_data)
- {
- int order = (int)(long)pool_data;
- __free_pages(element, order);
- }
- EXPORT_SYMBOL(mempool_free_pages);
|