From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Woojin Ji" <random6.xyz@gmail.com>, <bpf@vger.kernel.org>
Subject: Re: [PATCH bpf] bpf: Zero-extend the index in inlined bpf_get_func_arg()
Date: Sat, 17 Oct 2026 14:35:40 +0000 [thread overview]
Message-ID: <DM76STO8ECCU.36Z2ATFECUFLJ@gmail.com> (raw)
In-Reply-To: <179221432098.1890560.17043109037413683447.get-func-arg-index@gmail.com>
On Sat, Oct 17, 2026 at 02:42 PM Woojin Ji <random6.xyz@gmail.com> wrote:
> + /*
> + * The helper takes a u32 index; zero-extend it before
> + * the bounds check and the 64-bit address math below.
> + */
> + insn_buf[cnt++] = BPF_MOV32_REG(BPF_REG_2, BPF_REG_2);
> insn_buf[cnt++] = BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 6);
> insn_buf[cnt++] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 3);
After JMP32 the low 32 bits of R2 are below nr_args, which is at most
255, so a 32-bit shift cannot overflow and it clears the upper bits.
can we just make the shift 32-bit ?
Then no need for the extra insn and the comment.
The selftest stays as is.
Something like this (compile tested only):
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index d6f8352..ec62df6 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -2256,7 +2256,7 @@ patch_map_ops_generic:
cnt = 2;
}
insn_buf[cnt++] = BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 6);
- insn_buf[cnt++] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 3);
+ insn_buf[cnt++] = BPF_ALU32_IMM(BPF_LSH, BPF_REG_2, 3);
insn_buf[cnt++] = BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1);
insn_buf[cnt++] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 0);
insn_buf[cnt++] = BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0);
pw-bot: cr
prev parent reply other threads:[~2026-10-17 14:35 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-17 5:42 [PATCH bpf] bpf: Zero-extend the index in inlined bpf_get_func_arg() Woojin Ji
2026-10-17 5:58 ` Woojin Ji
2026-10-17 11:58 ` Jiri Olsa
2026-10-17 14:35 ` Alexei Starovoitov [this message]
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=DM76STO8ECCU.36Z2ATFECUFLJ@gmail.com \
--to=alexei.starovoitov@gmail.com \
--cc=bpf@vger.kernel.org \
--cc=random6.xyz@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.