Skip to content

Commit d19af0e

Browse files
isilenceaxboe
authored andcommitted
io_uring: add alloc_cache.c
Avoid inlining all and everything from alloc_cache.h and move cold bits into a new file. Signed-off-by: Pavel Begunkov <[email protected]> Reviewed-by: Gabriel Krisman Bertazi <[email protected]> Link: https://lore.kernel.org/r/06984c6cd58e703f7cfae5ab3067912f9f635a06.1738087204.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <[email protected]>
1 parent 16ac51a commit d19af0e

File tree

3 files changed

+54
-36
lines changed

3 files changed

+54
-36
lines changed

io_uring/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ obj-$(CONFIG_IO_URING) += io_uring.o opdef.o kbuf.o rsrc.o notif.o \
1313
sync.o msg_ring.o advise.o openclose.o \
1414
epoll.o statx.o timeout.o fdinfo.o \
1515
cancel.o waitid.o register.o \
16-
truncate.o memmap.o
16+
truncate.o memmap.o alloc_cache.o
1717
obj-$(CONFIG_IO_WQ) += io-wq.o
1818
obj-$(CONFIG_FUTEX) += futex.o
1919
obj-$(CONFIG_NET_RX_BUSY_POLL) += napi.o

io_uring/alloc_cache.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include "alloc_cache.h"
4+
5+
void io_alloc_cache_free(struct io_alloc_cache *cache,
6+
void (*free)(const void *))
7+
{
8+
void *entry;
9+
10+
if (!cache->entries)
11+
return;
12+
13+
while ((entry = io_alloc_cache_get(cache)) != NULL)
14+
free(entry);
15+
16+
kvfree(cache->entries);
17+
cache->entries = NULL;
18+
}
19+
20+
/* returns false if the cache was initialized properly */
21+
bool io_alloc_cache_init(struct io_alloc_cache *cache,
22+
unsigned max_nr, unsigned int size,
23+
unsigned int init_bytes)
24+
{
25+
cache->entries = kvmalloc_array(max_nr, sizeof(void *), GFP_KERNEL);
26+
if (!cache->entries)
27+
return true;
28+
29+
cache->nr_cached = 0;
30+
cache->max_cached = max_nr;
31+
cache->elem_size = size;
32+
cache->init_clear = init_bytes;
33+
return false;
34+
}
35+
36+
void *io_cache_alloc_new(struct io_alloc_cache *cache, gfp_t gfp)
37+
{
38+
void *obj;
39+
40+
obj = kmalloc(cache->elem_size, gfp);
41+
if (obj && cache->init_clear)
42+
memset(obj, 0, cache->init_clear);
43+
return obj;
44+
}

io_uring/alloc_cache.h

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
*/
99
#define IO_ALLOC_CACHE_MAX 128
1010

11+
void io_alloc_cache_free(struct io_alloc_cache *cache,
12+
void (*free)(const void *));
13+
bool io_alloc_cache_init(struct io_alloc_cache *cache,
14+
unsigned max_nr, unsigned int size,
15+
unsigned int init_bytes);
16+
17+
void *io_cache_alloc_new(struct io_alloc_cache *cache, gfp_t gfp);
18+
1119
static inline void io_alloc_cache_kasan(struct iovec **iov, int *nr)
1220
{
1321
if (IS_ENABLED(CONFIG_KASAN)) {
@@ -57,41 +65,7 @@ static inline void *io_cache_alloc(struct io_alloc_cache *cache, gfp_t gfp)
5765
obj = io_alloc_cache_get(cache);
5866
if (obj)
5967
return obj;
60-
61-
obj = kmalloc(cache->elem_size, gfp);
62-
if (obj && cache->init_clear)
63-
memset(obj, 0, cache->init_clear);
64-
return obj;
65-
}
66-
67-
/* returns false if the cache was initialized properly */
68-
static inline bool io_alloc_cache_init(struct io_alloc_cache *cache,
69-
unsigned max_nr, unsigned int size,
70-
unsigned int init_bytes)
71-
{
72-
cache->entries = kvmalloc_array(max_nr, sizeof(void *), GFP_KERNEL);
73-
if (cache->entries) {
74-
cache->nr_cached = 0;
75-
cache->max_cached = max_nr;
76-
cache->elem_size = size;
77-
cache->init_clear = init_bytes;
78-
return false;
79-
}
80-
return true;
68+
return io_cache_alloc_new(cache, gfp);
8169
}
8270

83-
static inline void io_alloc_cache_free(struct io_alloc_cache *cache,
84-
void (*free)(const void *))
85-
{
86-
void *entry;
87-
88-
if (!cache->entries)
89-
return;
90-
91-
while ((entry = io_alloc_cache_get(cache)) != NULL)
92-
free(entry);
93-
94-
kvfree(cache->entries);
95-
cache->entries = NULL;
96-
}
9771
#endif

0 commit comments

Comments
 (0)