From: Eduard Zingerman <eddyz87@gmail.com>
To: bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org
Cc: daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com,
yonghong.song@linux.dev, eddyz87@gmail.com, memxor@gmail.com,
iii@linux.ibm.com, gimm78064@gmail.com, info@starlabs.sg
Subject: [PATCH bpf-next v4 2/7] bpf: extract is_addr_space_cast32() utility function
Date: Fri, 7 Aug 2026 13:59:31 -0700 [thread overview]
Message-ID: <20260807-static-zext-v4-2-b6c270013c77@gmail.com> (raw)
In-Reply-To: <20260807-static-zext-v4-0-b6c270013c77@gmail.com>
bpf_do_misc_fixups() converts the following address space cast
instructions to 32-bit moves:
- cast from address space 1 (user) to address space 0 (kernel)
- cast from address space 0 (kernel) to address space 1 (user)
iff associated arena map has a BPF_F_NO_USER_CONV flag.
Extract a predicate detecting such instructions for use in the
following patches.
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
---
kernel/bpf/fixups.c | 35 ++++++++++++++++++++++++++---------
1 file changed, 26 insertions(+), 9 deletions(-)
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index a0bddada7964..5f7843648189 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -20,6 +20,26 @@ static bool is_cmpxchg_insn(const struct bpf_insn *insn)
insn->imm == BPF_CMPXCHG;
}
+/* Returns true if 'insn' is an address space cast instruction translated as BPF_ALU op */
+static bool is_addr_space_cast32(struct bpf_prog *prog, const struct bpf_insn *insn)
+{
+ struct bpf_map *arena = (struct bpf_map *)prog->aux->arena;
+
+ if (insn->code != (BPF_ALU64 | BPF_MOV | BPF_X) || insn->off != BPF_ADDR_SPACE_CAST)
+ return false;
+
+ /* cast from as(1) to as(0) */
+ if (insn->imm == 1)
+ return true;
+
+ /* cast from as(0) to as(1) */
+ if (insn->imm == 1 << 16)
+ return arena && arena->map_flags & BPF_F_NO_USER_CONV;
+
+ /* non-BPF_F_NO_USER_CONV cast from as(0) to as(1) should be handled by JIT */
+ return false;
+}
+
/* Return the regno defined by the insn, or -1. */
static int insn_def_regno(const struct bpf_insn *insn)
{
@@ -1513,15 +1533,12 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
}
for (i = 0; i < insn_cnt;) {
- if (insn->code == (BPF_ALU64 | BPF_MOV | BPF_X) && insn->imm) {
- if ((insn->off == BPF_ADDR_SPACE_CAST && insn->imm == 1) ||
- (((struct bpf_map *)env->prog->aux->arena)->map_flags & BPF_F_NO_USER_CONV)) {
- /* convert to 32-bit mov that clears upper 32-bit */
- insn->code = BPF_ALU | BPF_MOV | BPF_X;
- /* clear off and imm, so it's a normal 'wX = wY' from JIT pov */
- insn->off = 0;
- insn->imm = 0;
- } /* cast from as(0) to as(1) should be handled by JIT */
+ if (is_addr_space_cast32(env->prog, insn)) {
+ /* convert to 32-bit mov that clears upper 32-bit */
+ insn->code = BPF_ALU | BPF_MOV | BPF_X;
+ /* clear off and imm, so it's a normal 'wX = wY' from JIT pov */
+ insn->off = 0;
+ insn->imm = 0;
goto next_insn;
}
--
2.55.0
next prev parent reply other threads:[~2026-08-07 21:00 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-07 20:59 [PATCH bpf-next v4 0/7] bpf: infer zext_dst based on static register liveness analysis Eduard Zingerman
2026-08-07 20:59 ` [PATCH bpf-next v4 1/7] bpf: do not print a newline after disassembly in bpf_verbose_insn() Eduard Zingerman
2026-08-07 20:59 ` Eduard Zingerman [this message]
2026-08-07 20:59 ` [PATCH bpf-next v4 3/7] bpf: move bpf_is_reg64() to fixups.c Eduard Zingerman
2026-08-07 21:52 ` bot+bpf-ci
2026-08-07 20:59 ` [PATCH bpf-next v4 4/7] bpf: track upper 32-bit register halves' liveness in compute_live_registers() Eduard Zingerman
2026-08-07 20:59 ` [PATCH bpf-next v4 5/7] bpf: infer zext_dst based on static register liveness analysis Eduard Zingerman
2026-08-07 20:59 ` [PATCH bpf-next v4 6/7] bpf: simplify the bpf_is_reg64() Eduard Zingerman
2026-08-07 20:59 ` [PATCH bpf-next v4 7/7] selftests/bpf: verify zext_dst annotations for various instructions Eduard Zingerman
2026-08-08 10:00 ` [PATCH bpf-next v4 0/7] bpf: infer zext_dst based on static register liveness analysis 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=20260807-static-zext-v4-2-b6c270013c77@gmail.com \
--to=eddyz87@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=gimm78064@gmail.com \
--cc=iii@linux.ibm.com \
--cc=info@starlabs.sg \
--cc=kernel-team@fb.com \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=yonghong.song@linux.dev \
/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