Hi, On 11/18/2024 9:29 PM, Thomas Weißschuh wrote: > On Mon, Nov 18, 2024 at 09:08:04AM +0800, Hou Tao wrote: >> From: Hou Tao <houtao1@xxxxxxxxxx> >> >> Add bpf_mem_cache_is_mergeable() to check whether two bpf mem allocator >> for fixed-size objects are mergeable or not. The merging could reduce >> the memory overhead of bpf mem allocator. >> >> Signed-off-by: Hou Tao <houtao1@xxxxxxxxxx> >> --- >> include/linux/bpf_mem_alloc.h | 1 + >> kernel/bpf/memalloc.c | 12 ++++++++++++ >> 2 files changed, 13 insertions(+) >> >> diff --git a/include/linux/bpf_mem_alloc.h b/include/linux/bpf_mem_alloc.h >> index e45162ef59bb..faa54b9c7a04 100644 >> --- a/include/linux/bpf_mem_alloc.h >> +++ b/include/linux/bpf_mem_alloc.h >> @@ -47,5 +47,6 @@ void bpf_mem_cache_free(struct bpf_mem_alloc *ma, void *ptr); >> void bpf_mem_cache_free_rcu(struct bpf_mem_alloc *ma, void *ptr); >> void bpf_mem_cache_raw_free(void *ptr); >> void *bpf_mem_cache_alloc_flags(struct bpf_mem_alloc *ma, gfp_t flags); >> +bool bpf_mem_cache_is_mergeable(size_t size, size_t new_size, bool percpu); >> >> #endif /* _BPF_MEM_ALLOC_H */ >> diff --git a/kernel/bpf/memalloc.c b/kernel/bpf/memalloc.c >> index 889374722d0a..49dd08ad1d4f 100644 >> --- a/kernel/bpf/memalloc.c >> +++ b/kernel/bpf/memalloc.c >> @@ -1014,3 +1014,15 @@ int bpf_mem_alloc_check_size(bool percpu, size_t size) >> >> return 0; >> } >> + >> +bool bpf_mem_cache_is_mergeable(size_t size, size_t new_size, bool percpu) >> +{ >> + /* Only for fixed-size object allocator */ >> + if (!size || !new_size) >> + return false; >> + >> + return (percpu && ALIGN(size, PCPU_MIN_ALLOC_SIZE) == >> + ALIGN(new_size, PCPU_MIN_ALLOC_SIZE)) || >> + (!percpu && kmalloc_size_roundup(size + LLIST_NODE_SZ) == >> + kmalloc_size_roundup(new_size + LLIST_NODE_SZ)); > This would be easier to read: > > if (percpu) > return ALIGN() == ALIGN(); > else > return kmalloc_size_roundup() == kmalloc_size_roundup(); Indeed. Thanks for the suggestion. Will do in v2. >> +} >> -- >> 2.29.2 >>