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_s {
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
mempool_initialized(mempool_t * pool)30 static inline bool mempool_initialized(mempool_t *pool)
31 {
32 return pool->elements != NULL;
33 }
34
mempool_is_saturated(mempool_t * pool)35 static inline bool mempool_is_saturated(mempool_t *pool)
36 {
37 return READ_ONCE(pool->curr_nr) >= pool->min_nr;
38 }
39
40 void mempool_exit(mempool_t *pool);
41 int mempool_init_node(mempool_t *pool, int min_nr, mempool_alloc_t *alloc_fn,
42 mempool_free_t *free_fn, void *pool_data,
43 gfp_t gfp_mask, int node_id);
44
45 int mempool_init_noprof(mempool_t *pool, int min_nr, mempool_alloc_t *alloc_fn,
46 mempool_free_t *free_fn, void *pool_data);
47 #define mempool_init(...) \
48 alloc_hooks(mempool_init_noprof(__VA_ARGS__))
49
50 extern mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
51 mempool_free_t *free_fn, void *pool_data);
52
53 extern mempool_t *mempool_create_node_noprof(int min_nr, mempool_alloc_t *alloc_fn,
54 mempool_free_t *free_fn, void *pool_data,
55 gfp_t gfp_mask, int nid);
56 #define mempool_create_node(...) \
57 alloc_hooks(mempool_create_node_noprof(__VA_ARGS__))
58
59 #define mempool_create(_min_nr, _alloc_fn, _free_fn, _pool_data) \
60 mempool_create_node(_min_nr, _alloc_fn, _free_fn, _pool_data, \
61 GFP_KERNEL, NUMA_NO_NODE)
62
63 extern int mempool_resize(mempool_t *pool, int new_min_nr);
64 extern void mempool_destroy(mempool_t *pool);
65
66 extern void *mempool_alloc_noprof(mempool_t *pool, gfp_t gfp_mask) __malloc;
67 #define mempool_alloc(...) \
68 alloc_hooks(mempool_alloc_noprof(__VA_ARGS__))
69
70 extern void *mempool_alloc_preallocated(mempool_t *pool) __malloc;
71 extern void mempool_free(void *element, mempool_t *pool);
72
73 /*
74 * A mempool_alloc_t and mempool_free_t that get the memory from
75 * a slab cache that is passed in through pool_data.
76 * Note: the slab cache may not have a ctor function.
77 */
78 void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data);
79 void mempool_free_slab(void *element, void *pool_data);
80
81 #define mempool_init_slab_pool(_pool, _min_nr, _kc) \
82 mempool_init(_pool, (_min_nr), mempool_alloc_slab, mempool_free_slab, (void *)(_kc))
83 #define mempool_create_slab_pool(_min_nr, _kc) \
84 mempool_create((_min_nr), mempool_alloc_slab, mempool_free_slab, (void *)(_kc))
85
86 /*
87 * a mempool_alloc_t and a mempool_free_t to kmalloc and kfree the
88 * amount of memory specified by pool_data
89 */
90 void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data);
91 void mempool_kfree(void *element, void *pool_data);
92
93 #define mempool_init_kmalloc_pool(_pool, _min_nr, _size) \
94 mempool_init(_pool, (_min_nr), mempool_kmalloc, mempool_kfree, \
95 (void *)(unsigned long)(_size))
96 #define mempool_create_kmalloc_pool(_min_nr, _size) \
97 mempool_create((_min_nr), mempool_kmalloc, mempool_kfree, \
98 (void *)(unsigned long)(_size))
99
100 void *mempool_kvmalloc(gfp_t gfp_mask, void *pool_data);
101 void mempool_kvfree(void *element, void *pool_data);
102
mempool_init_kvmalloc_pool(mempool_t * pool,int min_nr,size_t size)103 static inline int mempool_init_kvmalloc_pool(mempool_t *pool, int min_nr, size_t size)
104 {
105 return mempool_init(pool, min_nr, mempool_kvmalloc, mempool_kvfree, (void *) size);
106 }
107
mempool_create_kvmalloc_pool(int min_nr,size_t size)108 static inline mempool_t *mempool_create_kvmalloc_pool(int min_nr, size_t size)
109 {
110 return mempool_create(min_nr, mempool_kvmalloc, mempool_kvfree, (void *) size);
111 }
112
113 /*
114 * A mempool_alloc_t and mempool_free_t for a simple page allocator that
115 * allocates pages of the order specified by pool_data
116 */
117 void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data);
118 void mempool_free_pages(void *element, void *pool_data);
119
120 #define mempool_init_page_pool(_pool, _min_nr, _order) \
121 mempool_init(_pool, (_min_nr), mempool_alloc_pages, \
122 mempool_free_pages, (void *)(long)(_order))
123 #define mempool_create_page_pool(_min_nr, _order) \
124 mempool_create((_min_nr), mempool_alloc_pages, \
125 mempool_free_pages, (void *)(long)(_order))
126
127 #endif /* _LINUX_MEMPOOL_H */
128