From: Eduard Zingerman <eddyz87@gmail.com>
To: bpf@vger.kernel.org, ast@kernel.org
Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev,
kernel-team@fb.com, yonghong.song@linux.dev, iii@linux.ibm.com,
gimm78064@gmail.com, Eduard Zingerman <eddyz87@gmail.com>
Subject: [PATCH bpf-next v3 2/5] bpf: track upper 32-bit register halves' liveness in compute_live_registers()
Date: Sun, 2 Aug 2026 13:24:18 -0700 [thread overview]
Message-ID: <20260802-static-zext-v3-2-3456b2604574@gmail.com> (raw)
In-Reply-To: <20260802-static-zext-v3-0-3456b2604574@gmail.com>
Extend compute_live_registers() to track upper and lower register
halves' liveness separately. This is mostly straightforward:
- use/def masks are extended to track 2 bits per register;
- compute_insn_live_regs() is updated to properly track these
2 bits according to the instruction semantics.
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
---
kernel/bpf/liveness.c | 89 ++++++++++++++++++++++++++++++++++-----------------
kernel/bpf/verifier.c | 1 -
2 files changed, 59 insertions(+), 31 deletions(-)
diff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c
index ff1e68cc4bd1..edfc2480b0f0 100644
--- a/kernel/bpf/liveness.c
+++ b/kernel/bpf/liveness.c
@@ -2047,29 +2047,38 @@ int bpf_compute_subprog_arg_access(struct bpf_verifier_env *env)
/* Each field is a register bitmask */
struct insn_live_regs {
- u16 use; /* registers read by instruction */
- u16 def; /* registers written by instruction */
- u16 in; /* registers that may be alive before instruction */
- u16 out; /* registers that may be alive after instruction */
+ u32 use; /* registers read by instruction */
+ u32 def; /* registers written by instruction */
+ u32 in; /* registers that may be alive before instruction */
+ u32 out; /* registers that may be alive after instruction */
};
/* Bitmask with 1s for all caller saved registers */
#define ALL_CALLER_SAVED_REGS ((1u << CALLER_SAVED_REGS) - 1)
+static inline u32 reg32_mask(u32 n) { return BIT(n); }
+static inline u32 reg64_mask(u32 n) { return BIT(n) | BIT(n + 16); }
+static inline u32 mask_widen(u32 m) { return m | (m << 16); }
+static inline u16 mask_lo(u32 m) { return (u16)m; }
+static inline u16 mask_hi(u32 m) { return (u16)(m >> 16); }
+
/* Compute info->{use,def} fields for the instruction */
static void compute_insn_live_regs(struct bpf_verifier_env *env,
struct bpf_insn *insn,
struct insn_live_regs *info)
{
struct bpf_call_summary cs;
- u8 class = BPF_CLASS(insn->code);
- u8 code = BPF_OP(insn->code);
- u8 mode = BPF_MODE(insn->code);
- u16 src = BIT(insn->src_reg);
- u16 dst = BIT(insn->dst_reg);
- u16 r0 = BIT(0);
- u16 def = 0;
- u16 use = 0xffff;
+ const u8 class = BPF_CLASS(insn->code);
+ const u8 code = BPF_OP(insn->code);
+ const u8 mode = BPF_MODE(insn->code);
+ const u8 size = BPF_SIZE(insn->code);
+ const u32 src = reg64_mask(insn->src_reg);
+ const u32 dst = reg64_mask(insn->dst_reg);
+ const u32 src32 = mask_lo(src);
+ const u32 dst32 = mask_lo(dst);
+ const u32 r0 = reg64_mask(0);
+ u32 def = 0;
+ u32 use = U32_MAX;
switch (class) {
case BPF_LD:
@@ -2080,8 +2089,8 @@ static void compute_insn_live_regs(struct bpf_verifier_env *env,
use = 0;
}
break;
- case BPF_LD | BPF_ABS:
- case BPF_LD | BPF_IND:
+ case BPF_ABS:
+ case BPF_IND:
/* stick with defaults */
break;
}
@@ -2089,7 +2098,15 @@ static void compute_insn_live_regs(struct bpf_verifier_env *env,
case BPF_LDX:
switch (mode) {
case BPF_MEM:
+ /* a narrow load still redefines the whole register */
+ def = dst;
+ use = src;
+ break;
case BPF_MEMSX:
+ /*
+ * sign extension defines the whole register;
+ * src holds a pointer, hence is used as 64-bit.
+ */
def = dst;
use = src;
break;
@@ -2107,12 +2124,19 @@ static void compute_insn_live_regs(struct bpf_verifier_env *env,
switch (mode) {
case BPF_MEM:
def = 0;
- use = dst | src;
+ use = dst | (size == BPF_DW ? src : src32);
break;
- case BPF_ATOMIC:
+ case BPF_ATOMIC: {
+ /*
+ * dst holds a pointer and is always used as 64-bit;
+ * the value operand and r0 are read as 32-bit for BPF_W atomics.
+ */
+ u32 srcv = size == BPF_DW ? src : src32;
+ u32 r0v = size == BPF_DW ? r0 : mask_lo(r0);
+
switch (insn->imm) {
case BPF_CMPXCHG:
- use = r0 | dst | src;
+ use = r0v | dst | srcv;
def = r0;
break;
case BPF_LOAD_ACQ:
@@ -2121,10 +2145,10 @@ static void compute_insn_live_regs(struct bpf_verifier_env *env,
break;
case BPF_STORE_REL:
def = 0;
- use = dst | src;
+ use = dst | srcv;
break;
default:
- use = dst | src;
+ use = dst | srcv;
if (insn->imm & BPF_FETCH)
def = src;
else
@@ -2132,6 +2156,7 @@ static void compute_insn_live_regs(struct bpf_verifier_env *env,
}
break;
}
+ }
break;
case BPF_ALU:
case BPF_ALU64:
@@ -2145,14 +2170,14 @@ static void compute_insn_live_regs(struct bpf_verifier_env *env,
if (BPF_SRC(insn->code) == BPF_K)
use = 0;
else
- use = src;
+ use = class == BPF_ALU64 ? src : src32;
break;
default:
def = dst;
if (BPF_SRC(insn->code) == BPF_K)
- use = dst;
+ use = class == BPF_ALU64 ? dst : dst32;
else
- use = dst | src;
+ use = class == BPF_ALU64 ? (dst | src) : (dst32 | src32);
}
break;
case BPF_JMP:
@@ -2178,13 +2203,14 @@ static void compute_insn_live_regs(struct bpf_verifier_env *env,
use = def & ~BIT(BPF_REG_0);
if (bpf_get_call_summary(env, insn, &cs))
use = GENMASK(min_t(u8, cs.num_params, MAX_BPF_FUNC_REG_ARGS), 1);
+ def = mask_widen(def);
+ use = mask_widen(use);
break;
default:
def = 0;
- if (BPF_SRC(insn->code) == BPF_K)
- use = dst;
- else
- use = dst | src;
+ use = class == BPF_JMP ? dst : dst32;
+ if (BPF_SRC(insn->code) == BPF_X)
+ use |= class == BPF_JMP ? src : src32;
}
break;
}
@@ -2249,8 +2275,8 @@ int bpf_compute_live_registers(struct bpf_verifier_env *env)
int insn_idx = env->cfg.insn_postorder[i];
struct insn_live_regs *live = &state[insn_idx];
struct bpf_iarray *succ;
- u16 new_out = 0;
- u16 new_in = 0;
+ u32 new_out = 0;
+ u32 new_in = 0;
succ = bpf_insn_successors(env, insn_idx);
for (int s = 0; s < succ->cnt; ++s)
@@ -2264,8 +2290,11 @@ int bpf_compute_live_registers(struct bpf_verifier_env *env)
}
}
- for (i = 0; i < insn_cnt; ++i)
- insn_aux[i].live_regs_before = state[i].in;
+ for (i = 0; i < insn_cnt; ++i) {
+ u32 in = state[i].in;
+
+ insn_aux[i].live_regs_before = mask_lo(in) | mask_hi(in);
+ }
if (env->log.level & BPF_LOG_LEVEL2) {
verbose(env, "Live regs before insn:\n");
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 6123e7e4a320..896bc5aaaaaf 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -16730,7 +16730,6 @@ bool bpf_get_call_summary(struct bpf_verifier_env *env, struct bpf_insn *call,
int i;
if (bpf_helper_call(call)) {
-
if (bpf_get_helper_proto(env, call->imm, &fn) < 0)
/* error would be reported later */
return false;
--
2.53.0
next prev parent reply other threads:[~2026-08-02 20:24 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-02 20:24 [PATCH bpf-next v3 0/5] bpf: infer zext_dst based on static register liveness analysis Eduard Zingerman
2026-08-02 20:24 ` [PATCH bpf-next v3 1/5] bpf: do not print a newline after disassembly in bpf_verbose_insn() Eduard Zingerman
2026-08-03 0:22 ` Quentin Monnet
2026-08-02 20:24 ` Eduard Zingerman [this message]
2026-08-02 20:24 ` [PATCH bpf-next v3 3/5] bpf: infer zext_dst based on static register liveness analysis Eduard Zingerman
2026-08-02 21:07 ` sashiko-bot
2026-08-02 21:14 ` Eduard Zingerman
2026-08-02 20:24 ` [PATCH bpf-next v3 4/5] bpf: simplify the bpf_is_reg64() Eduard Zingerman
2026-08-02 21:26 ` sashiko-bot
2026-08-02 21:40 ` Eduard Zingerman
2026-08-02 20:24 ` [PATCH bpf-next v3 5/5] selftests/bpf: verify zext_dst annotations for various instructions Eduard Zingerman
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=20260802-static-zext-v3-2-3456b2604574@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=kernel-team@fb.com \
--cc=martin.lau@linux.dev \
--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