Commit | Line | Data |
---|---|---|
9b797a37 JA |
1 | #ifndef IOU_ALLOC_CACHE_H |
2 | #define IOU_ALLOC_CACHE_H | |
3 | ||
9731bc98 JA |
4 | /* |
5 | * Don't allow the cache to grow beyond this size. | |
6 | */ | |
7 | #define IO_ALLOC_CACHE_MAX 512 | |
8 | ||
9b797a37 | 9 | struct io_cache_entry { |
efba1a9e | 10 | struct io_wq_work_node node; |
9b797a37 JA |
11 | }; |
12 | ||
9731bc98 | 13 | static inline bool io_alloc_cache_put(struct io_alloc_cache *cache, |
9b797a37 JA |
14 | struct io_cache_entry *entry) |
15 | { | |
69bbc6ad | 16 | if (cache->nr_cached < cache->max_cached) { |
9731bc98 | 17 | cache->nr_cached++; |
efba1a9e | 18 | wq_stack_add_head(&entry->node, &cache->list); |
e1fe7ee8 BL |
19 | /* KASAN poisons object */ |
20 | kasan_slab_free_mempool(entry); | |
9731bc98 JA |
21 | return true; |
22 | } | |
23 | return false; | |
9b797a37 JA |
24 | } |
25 | ||
528407b1 PB |
26 | static inline bool io_alloc_cache_empty(struct io_alloc_cache *cache) |
27 | { | |
28 | return !cache->list.next; | |
29 | } | |
30 | ||
9b797a37 JA |
31 | static inline struct io_cache_entry *io_alloc_cache_get(struct io_alloc_cache *cache) |
32 | { | |
efba1a9e BL |
33 | if (cache->list.next) { |
34 | struct io_cache_entry *entry; | |
9b797a37 | 35 | |
efba1a9e | 36 | entry = container_of(cache->list.next, struct io_cache_entry, node); |
e1fe7ee8 | 37 | kasan_unpoison_range(entry, cache->elem_size); |
efba1a9e | 38 | cache->list.next = cache->list.next->next; |
fd30d1cd | 39 | cache->nr_cached--; |
efba1a9e | 40 | return entry; |
9b797a37 JA |
41 | } |
42 | ||
43 | return NULL; | |
44 | } | |
45 | ||
69bbc6ad PB |
46 | static inline void io_alloc_cache_init(struct io_alloc_cache *cache, |
47 | unsigned max_nr, size_t size) | |
9b797a37 | 48 | { |
efba1a9e | 49 | cache->list.next = NULL; |
9731bc98 | 50 | cache->nr_cached = 0; |
69bbc6ad | 51 | cache->max_cached = max_nr; |
e1fe7ee8 | 52 | cache->elem_size = size; |
9b797a37 JA |
53 | } |
54 | ||
55 | static inline void io_alloc_cache_free(struct io_alloc_cache *cache, | |
56 | void (*free)(struct io_cache_entry *)) | |
57 | { | |
efba1a9e BL |
58 | while (1) { |
59 | struct io_cache_entry *entry = io_alloc_cache_get(cache); | |
9b797a37 | 60 | |
efba1a9e BL |
61 | if (!entry) |
62 | break; | |
63 | free(entry); | |
9b797a37 | 64 | } |
9731bc98 | 65 | cache->nr_cached = 0; |
9b797a37 JA |
66 | } |
67 | #endif |