Skip to content

Commit 9beda16

Browse files
Yonghong SongAlexei Starovoitov
authored andcommitted
bpf: Avoid unnecessary extra percpu memory allocation
Currently, for percpu memory allocation, say if the user requests allocation size to be 32 bytes, the actually calculated size will be 40 bytes and it further rounds to 64 bytes, and eventually 64 bytes are allocated, wasting 32-byte memory. Change bpf_mem_alloc() to calculate the cache index based on the user-provided allocation size so unnecessary extra memory can be avoided. Suggested-by: Hou Tao <[email protected]> Acked-by: Hou Tao <[email protected]> Signed-off-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 417fa6d commit 9beda16

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

kernel/bpf/memalloc.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,9 @@ void notrace *bpf_mem_alloc(struct bpf_mem_alloc *ma, size_t size)
833833
if (!size)
834834
return NULL;
835835

836-
idx = bpf_mem_cache_idx(size + LLIST_NODE_SZ);
836+
if (!ma->percpu)
837+
size += LLIST_NODE_SZ;
838+
idx = bpf_mem_cache_idx(size);
837839
if (idx < 0)
838840
return NULL;
839841

0 commit comments

Comments
 (0)