Skip to content

Commit 479b2f4

Browse files
krismanaxboe
authored andcommitted
io_uring: Fold allocation into alloc_cache helper
The allocation paths that use alloc_cache duplicate the same code pattern, sometimes in a quite convoluted way. Fold the allocation into the cache code itself, making it just an allocator function, and keeping the cache policy invisible to callers. Another justification for doing this, beyond code simplicity, is that it makes it trivial to test the impact of disabling the cache and using slab directly, which I've used for slab improvement experiments. One relevant detail is that we provide a callback to optionally initialize memory only when we actually reach slab. This allows us to avoid blindly executing the allocation with GFP_ZERO and only clean fields when they matter. Signed-off-by: Gabriel Krisman Bertazi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 29b95ac commit 479b2f4

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

io_uring/alloc_cache.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ static inline void *io_alloc_cache_get(struct io_alloc_cache *cache)
3030
return NULL;
3131
}
3232

33+
static inline void *io_cache_alloc(struct io_alloc_cache *cache, gfp_t gfp,
34+
void (*init_once)(void *obj))
35+
{
36+
if (unlikely(!cache->nr_cached)) {
37+
void *obj = kmalloc(cache->elem_size, gfp);
38+
39+
if (obj && init_once)
40+
init_once(obj);
41+
return obj;
42+
}
43+
return io_alloc_cache_get(cache);
44+
}
45+
3346
/* returns false if the cache was initialized properly */
3447
static inline bool io_alloc_cache_init(struct io_alloc_cache *cache,
3548
unsigned max_nr, size_t size)

0 commit comments

Comments
 (0)