bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Aijun Sun <aijun.sprd@gmail.com>
To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
	zlim.lnx@gmail.com
Cc: bpf@vger.kernel.org, catalin.marinas@arm.com, will@kernel.org,
	Aijun Sun <aijun.sun@unisoc.com>
Subject: [PATCH bpf] bpf, arm64: allocate program buffer using kvcalloc instead of kcalloc
Date: Thu,  4 Aug 2022 10:54:42 +0800	[thread overview]
Message-ID: <20220804025442.22524-1-aijun.sun@unisoc.com> (raw)

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(-)

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;
 	}
-- 
2.29.0


             reply	other threads:[~2022-08-04  2:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-04  2:54 Aijun Sun [this message]
2022-08-08 14:50 ` [PATCH bpf] bpf, arm64: allocate program buffer using kvcalloc instead of kcalloc patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220804025442.22524-1-aijun.sun@unisoc.com \
    --to=aijun.sprd@gmail.com \
    --cc=aijun.sun@unisoc.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=daniel@iogearbox.net \
    --cc=will@kernel.org \
    --cc=zlim.lnx@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).