]>
exis.tech > repos - linux.git/blob - io_uring/alloc_cache.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef IOU_ALLOC_CACHE_H
3 #define IOU_ALLOC_CACHE_H
5 #include <linux/io_uring_types.h>
6 #include <linux/kasan.h>
9 * Don't allow the cache to grow beyond this size.
11 #define IO_ALLOC_CACHE_MAX 128
13 void io_alloc_cache_free(struct io_alloc_cache
*cache
,
14 void (*free
)(const void *));
15 bool io_alloc_cache_init(struct io_alloc_cache
*cache
,
16 unsigned max_nr
, unsigned int size
,
17 unsigned int init_bytes
);
19 void *io_cache_alloc_new(struct io_alloc_cache
*cache
, gfp_t gfp
);
21 static inline bool io_alloc_cache_put(struct io_alloc_cache
*cache
,
24 if (cache
->nr_cached
< cache
->max_cached
) {
25 if (!kasan_mempool_poison_object(entry
))
27 cache
->entries
[cache
->nr_cached
++] = entry
;
33 static inline void *io_alloc_cache_get(struct io_alloc_cache
*cache
)
35 if (cache
->nr_cached
) {
36 void *entry
= cache
->entries
[--cache
->nr_cached
];
39 * If KASAN is enabled, always clear the initial bytes that
40 * must be zeroed post alloc, in case any of them overlap
43 #if defined(CONFIG_KASAN)
44 kasan_mempool_unpoison_object(entry
, cache
->elem_size
);
45 if (cache
->init_clear
)
46 memset(entry
, 0, cache
->init_clear
);
54 static inline void *io_cache_alloc(struct io_alloc_cache
*cache
, gfp_t gfp
)
58 obj
= io_alloc_cache_get(cache
);
61 return io_cache_alloc_new(cache
, gfp
);
64 static inline void io_cache_free(struct io_alloc_cache
*cache
, void *obj
)
66 if (!io_alloc_cache_put(cache
, obj
))