diff mbox series

[bpf] bpf, arm64: allocate program buffer using kvcalloc instead of kcalloc

Message ID 20220804025442.22524-1-aijun.sun@unisoc.com (mailing list archive)
State Accepted
Commit 19f68ed6dc90c93daf7e54d3350ea67fead7cbbf
Delegated to: BPF
Headers show
Series [bpf] bpf, arm64: allocate program buffer using kvcalloc instead of kcalloc | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf
netdev/fixes_present fail Series targets non-next tree, but doesn't contain any Fixes tags
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 9 maintainers not CCed: john.fastabend@gmail.com song@kernel.org sdf@google.com martin.lau@linux.dev kpsingh@kernel.org jolsa@kernel.org linux-arm-kernel@lists.infradead.org haoluo@google.com yhs@fb.com
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning WARNING: From:/Signed-off-by: email address mismatch: 'From: Aijun Sun <aijun.sprd@gmail.com>' != 'Signed-off-by: Aijun Sun <aijun.sun@unisoc.com>'
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-PR success PR summary
bpf/vmtest-bpf-VM_Test-1 success Logs for Kernel LATEST on ubuntu-latest with gcc
bpf/vmtest-bpf-VM_Test-2 success Logs for Kernel LATEST on ubuntu-latest with llvm-16
bpf/vmtest-bpf-VM_Test-3 success Logs for Kernel LATEST on z15 with gcc

Commit Message

Aijun Sun Aug. 4, 2022, 2:54 a.m. UTC
It is not necessary to allocate contiguous physical memory for BPF
program buffer using kcalloc. When the BPF program is large more than
memory page size, kcalloc allocates multiple memory pages from buddy
system. If the device can not provide sufficient memory, for example
in low-end android devices[1], memory allocation for BPF program is likely
failed.

Test cases in lib/test_bpf.c all pass on ARM64 QEMU.

[1]
AndroidTestSuit: page allocation failure: order:4,
mode:0x40dc0(GFP_KERNEL|__GFP_COMP|__GFP_ZERO), nodemask=(null),cpuset=foreground,mems_allowed=0
Call trace:
 dump_stack+0xa4/0x114
 warn_alloc+0xf8/0x14c
 __alloc_pages_slowpath+0xac8/0xb14
 __alloc_pages_nodemask+0x194/0x3d0
 kmalloc_order_trace+0x44/0x1e8
 __kmalloc+0x29c/0x66c
 bpf_int_jit_compile+0x17c/0x568
 bpf_prog_select_runtime+0x4c/0x1b0
 bpf_prepare_filter+0x5fc/0x6bc
 bpf_prog_create_from_user+0x118/0x1c0
 seccomp_set_mode_filter+0x1c4/0x7cc
 __do_sys_prctl+0x380/0x1424
 __arm64_sys_prctl+0x20/0x2c
 el0_svc_common+0xc8/0x22c
 el0_svc_handler+0x1c/0x28
 el0_svc+0x8/0x100

Signed-off-by: Aijun Sun <aijun.sun@unisoc.com>
---
 arch/arm64/net/bpf_jit_comp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

patchwork-bot+netdevbpf@kernel.org Aug. 8, 2022, 2:50 p.m. UTC | #1
Hello:

This patch was applied to bpf/bpf.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:

On Thu,  4 Aug 2022 10:54:42 +0800 you wrote:
> It is not necessary to allocate contiguous physical memory for BPF
> program buffer using kcalloc. When the BPF program is large more than
> memory page size, kcalloc allocates multiple memory pages from buddy
> system. If the device can not provide sufficient memory, for example
> in low-end android devices[1], memory allocation for BPF program is likely
> failed.
> 
> [...]

Here is the summary with links:
  - [bpf] bpf, arm64: allocate program buffer using kvcalloc instead of kcalloc
    https://git.kernel.org/bpf/bpf/c/19f68ed6dc90

You are awesome, thank you!
diff mbox series

Patch

diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index 42f2e9a8616c..80918e60387b 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -1399,7 +1399,7 @@  struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
 	memset(&ctx, 0, sizeof(ctx));
 	ctx.prog = prog;
 
-	ctx.offset = kcalloc(prog->len + 1, sizeof(int), GFP_KERNEL);
+	ctx.offset = kvcalloc(prog->len + 1, sizeof(int), GFP_KERNEL);
 	if (ctx.offset == NULL) {
 		prog = orig_prog;
 		goto out_off;
@@ -1499,7 +1499,7 @@  struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
 			ctx.offset[i] *= AARCH64_INSN_SIZE;
 		bpf_prog_fill_jited_linfo(prog, ctx.offset + 1);
 out_off:
-		kfree(ctx.offset);
+		kvfree(ctx.offset);
 		kfree(jit_data);
 		prog->aux->jit_data = NULL;
 	}