From: Hao Sun <sunhao.th@gmail.com>
To: bpf@vger.kernel.org
Cc: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com,
andrii@kernel.org, martin.lau@linux.dev, song@kernel.org,
yhs@fb.com, kpsingh@kernel.org, sdf@google.com,
haoluo@google.com, jolsa@kernel.org, davem@davemloft.net,
linux-kernel@vger.kernel.org, Hao Sun <sunhao.th@gmail.com>
Subject: [PATCH bpf-next] bpf: dup xlated insns with kvmalloc+memcpy
Date: Fri, 16 Dec 2022 13:54:35 +0800 [thread overview]
Message-ID: <20221216055436.4698-1-sunhao.th@gmail.com> (raw)
Currently, kmemdup() is used for allocating and copying xlated insns
in bpf_insn_prepare_dump(). The following warning can be triggered
when dup large amount of insns (roughly BPF_COMPLEXITY_LIMIT_INSNS/2)
because kmemdup() uses kmalloc() which would fail when allocing size
is too big, leading to failure in dump xlated insns:
WARNING: CPU: 2 PID: 7060 at mm/page_alloc.c:5534
Call Trace:
<TASK>
__alloc_pages_node include/linux/gfp.h:237 [inline]
alloc_pages_node include/linux/gfp.h:260 [inline]
__kmalloc_large_node+0x81/0x160 mm/slab_common.c:1096
__do_kmalloc_node mm/slab_common.c:943 [inline]
__kmalloc_node_track_caller.cold+0x5/0x5d mm/slab_common.c:975
kmemdup+0x29/0x60 mm/util.c:129
kmemdup include/linux/fortify-string.h:585 [inline]
bpf_insn_prepare_dump kernel/bpf/syscall.c:3820 [inline]
bpf_prog_get_info_by_fd+0x9a3/0x2cb0 kernel/bpf/syscall.c:3975
bpf_obj_get_info_by_fd kernel/bpf/syscall.c:4297 [inline]
__sys_bpf+0x3928/0x56f0 kernel/bpf/syscall.c:5004
__do_sys_bpf kernel/bpf/syscall.c:5069 [inline]
__se_sys_bpf kernel/bpf/syscall.c:5067 [inline]
...
So use kvmalloc()+memcpy() to fix this, for small size of insns,
this is same as kmemdup(), but this also support dup large amount
of xlated insns.
Signed-off-by: Hao Sun <sunhao.th@gmail.com>
---
kernel/bpf/syscall.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 35972afb6850..06229fddac0d 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -3831,10 +3831,10 @@ static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
u8 code;
int i;
- insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
- GFP_USER);
- if (!insns)
+ insns = kvmalloc(bpf_prog_insn_size(prog), GFP_USER | __GFP_NOWARN);
+ if (unlikely(!insns))
return insns;
+ memcpy(insns, prog->insnsi, bpf_prog_insn_size(prog));
for (i = 0; i < prog->len; i++) {
code = insns[i].code;
@@ -3992,7 +3992,7 @@ static int bpf_prog_get_info_by_fd(struct file *file,
uinsns = u64_to_user_ptr(info.xlated_prog_insns);
ulen = min_t(u32, info.xlated_prog_len, ulen);
fault = copy_to_user(uinsns, insns_sanitized, ulen);
- kfree(insns_sanitized);
+ kvfree(insns_sanitized);
if (fault)
return -EFAULT;
}
base-commit: 0e43662e61f2569500ab83b8188c065603530785
--
2.39.0
next reply other threads:[~2022-12-16 5:54 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-16 5:54 Hao Sun [this message]
2022-12-16 7:03 ` [PATCH bpf-next] bpf: dup xlated insns with kvmalloc+memcpy Yonghong Song
2022-12-16 7:18 ` Hao Sun
2022-12-16 15:24 ` Daniel Borkmann
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=20221216055436.4698-1-sunhao.th@gmail.com \
--to=sunhao.th@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=sdf@google.com \
--cc=song@kernel.org \
--cc=yhs@fb.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