| 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * memory buffer pool support |
| 4 | */ |
| 5 | #ifndef _LINUX_MEMPOOL_H |
| 6 | #define _LINUX_MEMPOOL_H |
| 7 | |
| 8 | #include <linux/sched.h> |
| 9 | #include <linux/alloc_tag.h> |
| 10 | #include <linux/wait.h> |
| 11 | #include <linux/compiler.h> |
| 12 | |
| 13 | struct kmem_cache; |
| 14 | |
| 15 | typedef void * (mempool_alloc_t)(gfp_t gfp_mask, void *pool_data); |
| 16 | typedef void (mempool_free_t)(void *element, void *pool_data); |
| 17 | |
| 18 | typedef struct mempool { |
| 19 | spinlock_t lock; |
| 20 | int min_nr; /* nr of elements at *elements */ |
| 21 | int curr_nr; /* Current nr of elements at *elements */ |
| 22 | void **elements; |
| 23 | |
| 24 | void *pool_data; |
| 25 | mempool_alloc_t *alloc; |
| 26 | mempool_free_t *free; |
| 27 | wait_queue_head_t wait; |
| 28 | } mempool_t; |
| 29 | |
| 30 | static inline bool mempool_initialized(struct mempool *pool) |
| 31 | { |
| 32 | return pool->elements != NULL; |
| 33 | } |
| 34 | |
| 35 | static inline bool mempool_is_saturated(struct mempool *pool) |
| 36 | { |
| 37 | return READ_ONCE(pool->curr_nr) >= pool->min_nr; |
| 38 | } |
| 39 | |
| 40 | void mempool_exit(struct mempool *pool); |
| 41 | int mempool_init_node(struct mempool *pool, int min_nr, |
| 42 | mempool_alloc_t *alloc_fn, mempool_free_t *free_fn, |
| 43 | void *pool_data, gfp_t gfp_mask, int node_id); |
| 44 | int mempool_init_noprof(struct mempool *pool, int min_nr, |
| 45 | mempool_alloc_t *alloc_fn, mempool_free_t *free_fn, |
| 46 | void *pool_data); |
| 47 | #define mempool_init(...) \ |
| 48 | alloc_hooks(mempool_init_noprof(__VA_ARGS__)) |
| 49 | |
| 50 | struct mempool *mempool_create(int min_nr, mempool_alloc_t *alloc_fn, |
| 51 | mempool_free_t *free_fn, void *pool_data); |
| 52 | struct mempool *mempool_create_node_noprof(int min_nr, |
| 53 | mempool_alloc_t *alloc_fn, mempool_free_t *free_fn, |
| 54 | void *pool_data, gfp_t gfp_mask, int nid); |
| 55 | #define mempool_create_node(...) \ |
| 56 | alloc_hooks(mempool_create_node_noprof(__VA_ARGS__)) |
| 57 | |
| 58 | #define mempool_create(_min_nr, _alloc_fn, _free_fn, _pool_data) \ |
| 59 | mempool_create_node(_min_nr, _alloc_fn, _free_fn, _pool_data, \ |
| 60 | GFP_KERNEL, NUMA_NO_NODE) |
| 61 | |
| 62 | int mempool_resize(struct mempool *pool, int new_min_nr); |
| 63 | void mempool_destroy(struct mempool *pool); |
| 64 | |
| 65 | void *mempool_alloc_noprof(struct mempool *pool, gfp_t gfp_mask) __malloc; |
| 66 | #define mempool_alloc(...) \ |
| 67 | alloc_hooks(mempool_alloc_noprof(__VA_ARGS__)) |
| 68 | int mempool_alloc_bulk_noprof(struct mempool *pool, void **elem, |
| 69 | unsigned int count, unsigned int allocated); |
| 70 | #define mempool_alloc_bulk(...) \ |
| 71 | alloc_hooks(mempool_alloc_bulk_noprof(__VA_ARGS__)) |
| 72 | |
| 73 | void *mempool_alloc_preallocated(struct mempool *pool) __malloc; |
| 74 | void mempool_free(void *element, struct mempool *pool); |
| 75 | unsigned int mempool_free_bulk(struct mempool *pool, void **elem, |
| 76 | unsigned int count); |
| 77 | |
| 78 | /* |
| 79 | * A mempool_alloc_t and mempool_free_t that get the memory from |
| 80 | * a slab cache that is passed in through pool_data. |
| 81 | * Note: the slab cache may not have a ctor function. |
| 82 | */ |
| 83 | void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data); |
| 84 | void mempool_free_slab(void *element, void *pool_data); |
| 85 | |
| 86 | #define mempool_init_slab_pool(_pool, _min_nr, _kc) \ |
| 87 | mempool_init(_pool, (_min_nr), mempool_alloc_slab, mempool_free_slab, (void *)(_kc)) |
| 88 | #define mempool_create_slab_pool(_min_nr, _kc) \ |
| 89 | mempool_create((_min_nr), mempool_alloc_slab, mempool_free_slab, (void *)(_kc)) |
| 90 | |
| 91 | /* |
| 92 | * a mempool_alloc_t and a mempool_free_t to kmalloc and kfree the |
| 93 | * amount of memory specified by pool_data |
| 94 | */ |
| 95 | void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data); |
| 96 | void mempool_kfree(void *element, void *pool_data); |
| 97 | |
| 98 | #define mempool_init_kmalloc_pool(_pool, _min_nr, _size) \ |
| 99 | mempool_init(_pool, (_min_nr), mempool_kmalloc, mempool_kfree, \ |
| 100 | (void *)(unsigned long)(_size)) |
| 101 | #define mempool_create_kmalloc_pool(_min_nr, _size) \ |
| 102 | mempool_create((_min_nr), mempool_kmalloc, mempool_kfree, \ |
| 103 | (void *)(unsigned long)(_size)) |
| 104 | |
| 105 | /* |
| 106 | * A mempool_alloc_t and mempool_free_t for a simple page allocator that |
| 107 | * allocates pages of the order specified by pool_data |
| 108 | */ |
| 109 | void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data); |
| 110 | void mempool_free_pages(void *element, void *pool_data); |
| 111 | |
| 112 | #define mempool_init_page_pool(_pool, _min_nr, _order) \ |
| 113 | mempool_init(_pool, (_min_nr), mempool_alloc_pages, \ |
| 114 | mempool_free_pages, (void *)(long)(_order)) |
| 115 | #define mempool_create_page_pool(_min_nr, _order) \ |
| 116 | mempool_create((_min_nr), mempool_alloc_pages, \ |
| 117 | mempool_free_pages, (void *)(long)(_order)) |
| 118 | |
| 119 | #endif /* _LINUX_MEMPOOL_H */ |
| 120 | |