bpf: Improve linked register tracking
Subsystems (4)
v2 cover bpf: Improve linked register tracking
This series extends the BPF verifier's linked register tracking to handle
negative offsets, BPF_SUB operations, and alu32 operations, enabling better bounds propagation for common arithmetic patterns.
The verifier previously only tracked positive constant deltas between linked registers using BPF_ADD. This meant patterns using negative offsets or subtraction couldn't benefit from bounds propagation:
r1 = r0
r1 += -4
if r1 s>= 0 goto ... // r1 >= 0 implies r0 >= 4
// verifier couldn't propagate bounds back to r0
Verifier also couldn't detect such patterns with ALU32 operations.
I ran veristat before and after this change on cpuv4 selftests, sched_ext programs, and meta internal BPF programs, here is the difference:
selftests:
----------
./veristat -C -e file,prog,states,insns -f "insns_pct>5" self_before.csv self_after.csv
File Program States (A) States (B) States (DIFF) Insns (A) Insns (B) Insns (DIFF) ---------------------------------- ---------------------- ---------- ---------- ------------- --------- --------- -------------- bpf_cubic.bpf.o bpf_cubic_acked 21 38 +17 (+80.95%) 237 420 +183 (+77.22%) iters.bpf.o iter_obfuscate_counter 8 14 +6 (+75.00%) 83 164 +81 (+97.59%) linked_list_peek.bpf.o list_peek 16 9 -7 (-43.75%) 152 88 -64 (-42.11%) verifier_iterating_callbacks.bpf.o cond_break2 13 10 -3 (-23.08%) 110 88 -22 (-20.00%) verifier_iterating_callbacks.bpf.o test4 2 4 +2 (+100.00%) 20 32 +12 (+60.00%)
sched_ext:
----------
./veristat/src/veristat -C -e file,prog,states,insns -f "insns_pct>5" scx_before.csv scx_after.csv
File Program States (A) States (B) States (DIFF) Insns (A) Insns (B) Insns (DIFF) ----------------- ---------------- ---------- ---------- ------------- --------- --------- ------------- scx_layered.bpf.o layered_runnable 359 381 +22 (+6.13%) 5674 6077 +403 (+7.10%) scx_p2dq.bpf.o p2dq_dispatch 197 208 +11 (+5.58%) 1948 2065 +117 (+6.01%)
FB programs saw the most diff (used insns_pct>20 here to output fewer lines :
-----------------------------------------------------------------------------
./veristat/src/veristat -C -e file,prog,states,insns -f "insns_pct>20" fb_before.csv fb_after.csv
File Program States (A) States (B) States (DIFF) Insns (A) Insns (B) Insns (DIFF) ------------------------------------------------- ---------------- ---------- ---------- --------------- --------- --------- ---------------- bpfjailer-lib-tests-bpf-dbus_test-dbus_test.bpf.o do_parse 6019 7631 +1612 (+26.78%) 124809 151707 +26898 (+21.55%) bpfjailer-lib-tests-bpf-dbus_test-dbus_test.bpf.o do_sendmsg 6020 7632 +1612 (+26.78%) 124823 151721 +26898 (+21.55%) third-party-scx-6.9-scheds-rust-scx_layered-bpf_ layered_runnable 125 148 +23 (+18.40%) 1940 2381 +441 (+22.73%) third-party-scx-backports-6.9-1.0.11.1-scheds-r layered_runnable 133 155 +22 (+16.54%) 1700 2152 +452 (+26.59%) third-party-scx-backports-6.9-1.0.8-scheds-rust layered_runnable 114 136 +22 (+19.30%) 1499 1951 +452 (+30.15%)
v1: https://lore.kernel.org/bpf/[email protected]/
Changes in v1->v2:
- Add support for alu32 operations in linked register tracking (Alexei)
- Squash the selftest fix with the first patch (Eduard)
- Add more selftests to detect edge cases
Puranjay Mohan (2):
bpf: Support negative offsets, BPF_SUB, and alu32 for linked register
tracking
selftests/bpf: Add tests for improved linked register tracking
include/linux/bpf_verifier.h | 8 +- kernel/bpf/verifier.c | 39 ++- .../selftests/bpf/progs/verifier_bounds.c | 2 +- .../bpf/progs/verifier_linked_scalars.c | 232 +++++++++++++++++- 4 files changed, 265 insertions(+), 16 deletions(-)
base-commit: 9c1a3525fd64839d71e0c1a21170003a4621e6e8
Signature · 1 line
2.47.3
PATCH v2 1/2 bpf: Support negative offsets, BPF_SUB, and alu32 for linked register tracking
Previously, the verifier only tracked positive constant deltas between linked registers using BPF_ADD. This limitation meant patterns like:
r1 = r0
r1 += -4
if r1 s>= 0 goto ... // r1 >= 0 implies r0 >= 4
// verifier couldn't propagate bounds back to r0
Similar limitation existed for 32 bit registers.
With this change, the verifier can now track negative deltas in reg->off enabling bound propagation for the above pattern.
32-bit registers are supported by splitting BPF_ADD_CONST into BPF_ADD_CONST32 and BPF_ADD_CONST64, adjust_reg_min_max_vals() now encodes precise information in dst_reg->id about if the value was created using a 32 or 64 bit ALU operation. sync_linked_regs() uses this information to zext the register if 32-bit operation was used.
--- include/linux/bpf_verifier.h | 8 +++- kernel/bpf/verifier.c | 39 +++++++++++++------ .../selftests/bpf/progs/verifier_bounds.c | 2 +- 3 files changed, 35 insertions(+), 14 deletions(-)
include/linux/bpf_verifier.h +6 -2
@@ -146,9 +146,13 @@ struct bpf_reg_state {* Upper bit of ID is used to remember relationship between "linked" * registers. Example: * r1 = r2; both will have r1->id == r2->id == N -* r1 += 10; r1->id == N | BPF_ADD_CONST and r1->off == 10 +* r1 += 10; r1->id == N | BPF_ADD_CONST64 and r1->off == 10 +* r3 = r2; both will have r3->id == r2->id == N +* w3 += 10; r3->id == N | BPF_ADD_CONST32 and r3->off == 10 */ -#define BPF_ADD_CONST (1U << 31) +#define BPF_ADD_CONST64 (1U << 31) +#define BPF_ADD_CONST32 (1U << 30) +#define BPF_ADD_CONST (BPF_ADD_CONST64 | BPF_ADD_CONST32)u32 id;/* PTR_TO_SOCKET and PTR_TO_TCP_SOCK could be a ptr returned * from a pointer-cast helper, bpf_sk_fullsock() and
kernel/bpf/verifier.c +28 -11
@@ -15706,26 +15706,41 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,* r1 += 0x1 * if r2 < 1000 goto ... * use r1 in memory access -* So for 64-bit alu remember constant delta between r2 and r1 and -* update r1 after 'if' condition. +* So remember constant delta between r2 and r1 and update r1 after +* 'if' condition. */ if (env->bpf_capable && -BPF_OP (insn->code) ==BPF_ADD && !alu32 && - dst_reg->id &&is_reg_const (src_reg, false)) { -u64 val =reg_const_value (src_reg, false); + (BPF_OP (insn->code) ==BPF_ADD ||BPF_OP (insn->code) ==BPF_SUB ) && + dst_reg->id &&is_reg_const (src_reg, alu32)) { +u64 val =reg_const_value (src_reg, alu32); +s32 off; -if ((dst_reg->id &BPF_ADD_CONST ) || -/* prevent overflow in sync_linked_regs() later */ - val > (u32 )S32_MAX ) { +if (!alu32 && ((s64 )val <S32_MIN || (s64 )val >S32_MAX )) + goto clear_id; + + off = (s32 )val; + +if (BPF_OP (insn->code) ==BPF_SUB ) { +/* Negating S32_MIN would overflow */ +if (off ==S32_MIN ) + goto clear_id; + off = -off; + } + +if (dst_reg->id &BPF_ADD_CONST ) {/* * If the register already went through rX += val * we cannot accumulate another val into rx->off. */ +clear_id: dst_reg->off =0 ; dst_reg->id =0 ; }else { - dst_reg->id |=BPF_ADD_CONST ; - dst_reg->off = val; +if (alu32) + dst_reg->id |=BPF_ADD_CONST32 ; +else + dst_reg->id |=BPF_ADD_CONST64 ; + dst_reg->off = off; } }else {/*
@@ -16821,7 +16836,7 @@ static void sync_linked_regs(struct bpf_verifier_state *vstate, struct bpf_reg_ss32 saved_off = reg->off; fake_reg.type =SCALAR_VALUE ; -__mark_reg_known (&fake_reg, (s32 )reg->off - (s32 )known_reg->off); +__mark_reg_known (&fake_reg, (s64 )reg->off - (s64 )known_reg->off);/* reg = known_reg; reg += delta */ copy_register_state (reg, known_reg);
@@ -16835,6 +16850,8 @@ static void sync_linked_regs(struct bpf_verifier_state *vstate, struct bpf_reg_sscalar32_min_max_add (reg, &fake_reg);scalar_min_max_add (reg, &fake_reg); reg->var_off =tnum_add (reg->var_off, fake_reg.var_off); +if (reg->id &BPF_ADD_CONST32 ) +zext_32_to_64 (reg); } } }
tools/testing/selftests/bpf/progs/verifier_bounds.c +1 -1
@@ -1477,7 +1477,7 @@ __naked void sub64_full_overflow(void) SEC("socket") __description("64-bit subtraction, partial overflow, result in unbounded reg") __success __log_level(2) -__msg("3: (1f) r3 -= r2 {{.*}} R3=scalar()") +__msg("3: (1f) r3 -= r2 {{.*}} R3=scalar(id=1-1)") __retval(0)__naked void sub64_partial_overflow (void ) {
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH bpf-next v2 1/2] bpf: Support negative offsets, BPF_SUB, and alu32 for linked register tracking
The comment above copy_register_state() says "Must preserve off, id and add_const flag" but only off and subreg_def are actually restored after the copy. At this point reg->id has been overwritten…
Quoted 2 lines
kernel/bpf/verifier.c +0
-0
[ ... ]
Quoted 10 lines
kernel/bpf/verifier.c +1
-1@@ -16821,7 +16836,7 @@ static void sync_linked_regs(struct bpf_verifier_state *vstate, struct bpf_reg_ss32 saved_off = reg->off; fake_reg.type =SCALAR_VALUE ; -__mark_reg_known (&fake_reg, (s32 )reg->off - (s32 )known_reg->off); +__mark_reg_known (&fake_reg, (s64 )reg->off - (s64 )known_reg->off);/* reg = known_reg; reg += delta */ copy_register_state (reg, known_reg);
kernel/bpf/verifier.c static void sync_linked_regs(struct bpf_verifier_state *vstate, struct bpf_reg_s · line 16854
Quoted 5 lines
kernel/bpf/verifier.c +2
-0@@ -16835,6 +16850,8 @@ static void sync_linked_regs(struct bpf_verifier_state *vstate, struct bpf_reg_sscalar32_min_max_add (reg, &fake_reg);scalar_min_max_add (reg, &fake_reg); reg->var_off =tnum_add (reg->var_off, fake_reg.var_off); +if (reg->id &BPF_ADD_CONST32 ) +zext_32_to_64 (reg);
The comment above copy_register_state() says "Must preserve off, id and add_const flag" but only off and subreg_def are actually restored after the copy. At this point reg->id has been overwritten with known_reg->id.
If reg was created with a 32-bit operation (w1 += 5) it would have BPF_ADD_CONST32 in its id. But if known_reg was created with a 64-bit operation or has no ADD_CONST flag at all, then after copy_register_state() the BPF_ADD_CONST32 flag is lost.
Should the original reg->id be saved before copy_register_state() and used for the BPF_ADD_CONST32 check, similar to how saved_off is used? For example:
s32 saved_subreg_def = reg->subreg_def;
s32 saved_off = reg->off;
u32 saved_id = reg->id; /* save original id */
...
copy_register_state(reg, known_reg);
reg->off = saved_off;
reg->subreg_def = saved_subreg_def;
...
if (saved_id & BPF_ADD_CONST32) /* use saved_id */
zext_32_to_64(reg);kernel/bpf/verifier.c static void sync_linked_regs(struct bpf_verifier_state *vstate, struct bpf_reg_s · line 16857
Quoted 3 lines
kernel/bpf/verifier.c +0
-0} } }
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/20962647765
Eduard Zingerman
maintainerRe: [PATCH bpf-next v2 1/2] bpf: Support negative offsets, BPF_SUB, and alu32 for linked register tracking
In reply to Puranjay MohanOn Tue, 2026-01-13 at 07:25 -0800, Puranjay Mohan wrote:
[...]
Quoted 28 lines
kernel/bpf/verifier.c +9
-8@@ -15706,26 +15706,41 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,* r1 += 0x1 * if r2 < 1000 goto ... * use r1 in memory access -* So for 64-bit alu remember constant delta between r2 and r1 and -* update r1 after 'if' condition. +* So remember constant delta between r2 and r1 and update r1 after +* 'if' condition. */ if (env->bpf_capable && -BPF_OP (insn->code) ==BPF_ADD && !alu32 && - dst_reg->id &&is_reg_const (src_reg, false)) { -u64 val =reg_const_value (src_reg, false); + (BPF_OP (insn->code) ==BPF_ADD ||BPF_OP (insn->code) ==BPF_SUB ) && + dst_reg->id &&is_reg_const (src_reg, alu32)) { +u64 val =reg_const_value (src_reg, alu32); +s32 off; -if ((dst_reg->id &BPF_ADD_CONST ) || -/* prevent overflow in sync_linked_regs() later */ - val > (u32 )S32_MAX ) { +if (!alu32 && ((s64 )val <S32_MIN || (s64 )val >S32_MAX )) + goto clear_id; +
kernel/bpf/verifier.c static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, · line 15730
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ off = (s32 )val; + +if (BPF_OP (insn->code) ==BPF_SUB ) { +/* Negating S32_MIN would overflow */ +if (off ==S32_MIN ) + goto clear_id; + off = -off; + } + +if (dst_reg->id &BPF_ADD_CONST ) {
This logic is not correct, consider the following example:
SEC("socket")
__success
__naked void bug1(void)
{
asm volatile (" \
call %[bpf_get_prandom_u32]; \
r1 = r0; \
w1 += 1; \
if r1 > 10 goto 1f; \
r0 >>= 32; \
if r0 == 0 goto 1f; \
r0 /= 0; \
1: \
r0 = 0; \
exit; \
" :
: __imm(bpf_get_prandom_u32)
: __clobber_all);
}It is verified as follows:
Global function bug1() doesn't return scalar. Only those are supported.
0: R1=ctx() R10=fp0
; asm volatile (" \ @ verifier_linked_scalars.c:266
0: (85) call bpf_get_prandom_u32#7 ; R0=scalar()
1: (bf) r1 = r0 ; R0=scalar(id=1) R1=scalar(id=1)
2: (04) w1 += 1 ; R1=scalar(id=1+1,smin=0,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff))
3: (25) if r1 > 0xa goto pc+3 ; R1=scalar(id=1+1,smin=smin32=0,smax=umax=smax32=umax32=10,var_off=(0x0; 0xf))
4: (77) r0 >>= 32 ; R0=0
5: (15) if r0 == 0x0 goto pc+1
mark_precise: frame0: last_idx 5 first_idx 0 subseq_idx -1
mark_precise: frame0: regs=r0 stack= before 4: (77) r0 >>= 32
mark_precise: frame0: regs=r0 stack= before 3: (25) if r1 > 0xa goto pc+3
mark_precise: frame0: regs=r0,r1 stack= before 2: (04) w1 += 1
mark_precise: frame0: regs=r0,r1 stack= before 1: (bf) r1 = r0
mark_precise: frame0: regs=r0 stack= before 0: (85) call bpf_get_prandom_u32#7
5: R0=0
7: (b7) r0 = 0 ; R0=0
8: (95) exit
from 3 to 7: R0=scalar(id=1+0,smin=0,smax=0xffffffff,umin=umin32=10,umax=umax32=0xfffffffe,var_off=(0x0; 0xffffffff)) R1=scalar(id=1+1,smin=umin=umin32=11,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff)) R10=fp0
7: R0=scalar(id=1+0,smin=0,smax=0xffffffff,umin=umin32=10,umax=umax32=0xfffffffe,var_off=(0x0; 0xffffffff)) R1=scalar(id=1+1,smin=umin=umin32=11,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff)) R10=fp0
7: (b7) r0 = 0 ; R0=0
8: (95) exit
processed 10 insns (limit 1000000) max_states_per_insn 0 total_states 1 peak_states 1 mark_read 0The problem here is that at instruction 2: w1 += 1 id of the register is preserved, while this instruction destructs higher bits of the register.
After each instruction you must maintain that for a set of linked registers R ∀ r ∈ R: value(r) - off(r) == C, where C is some constant representing the id. This is not the case for operation w1 += 1 if you don't make sure prior to the operation that upper bits of r1 are zero.
Quoted 18 lines
kernel/bpf/verifier.c +6
-2/* * If the register already went through rX += val * we cannot accumulate another val into rx->off. */ +clear_id: dst_reg->off =0 ; dst_reg->id =0 ; }else { - dst_reg->id |=BPF_ADD_CONST ; - dst_reg->off = val; +if (alu32) + dst_reg->id |=BPF_ADD_CONST32 ; +else + dst_reg->id |=BPF_ADD_CONST64 ; + dst_reg->off = off; } }else {/*
[...]
Re: [PATCH bpf-next v2 1/2] bpf: Support negative offsets, BPF_SUB, and alu32 for linked register tracking
Quoted 93 lines · Eduard Zingerman
On Tue, 2026-01-13 at 07:25 -0800, Puranjay Mohan wrote:
[...]
Quoted 28 lines
kernel/bpf/verifier.c +9
-8@@ -15706,26 +15706,41 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,* r1 += 0x1 * if r2 < 1000 goto ... * use r1 in memory access -* So for 64-bit alu remember constant delta between r2 and r1 and -* update r1 after 'if' condition. +* So remember constant delta between r2 and r1 and update r1 after +* 'if' condition. */ if (env->bpf_capable && -BPF_OP (insn->code) ==BPF_ADD && !alu32 && - dst_reg->id &&is_reg_const (src_reg, false)) { -u64 val =reg_const_value (src_reg, false); + (BPF_OP (insn->code) ==BPF_ADD ||BPF_OP (insn->code) ==BPF_SUB ) && + dst_reg->id &&is_reg_const (src_reg, alu32)) { +u64 val =reg_const_value (src_reg, alu32); +s32 off; -if ((dst_reg->id &BPF_ADD_CONST ) || -/* prevent overflow in sync_linked_regs() later */ - val > (u32 )S32_MAX ) { +if (!alu32 && ((s64 )val <S32_MIN || (s64 )val >S32_MAX )) + goto clear_id; +kernel/bpf/verifier.c static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, · line 15730
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ off = (s32 )val; + +if (BPF_OP (insn->code) ==BPF_SUB ) { +/* Negating S32_MIN would overflow */ +if (off ==S32_MIN ) + goto clear_id; + off = -off; + } + +if (dst_reg->id &BPF_ADD_CONST ) {This logic is not correct, consider the following example:
SEC("socket") __success __naked void bug1(void) { asm volatile (" \ call %[bpf_get_prandom_u32]; \ r1 = r0; \ w1 += 1; \ if r1 > 10 goto 1f; \ r0 >>= 32; \ if r0 == 0 goto 1f; \ r0 /= 0; \ 1: \ r0 = 0; \ exit; \ " : : __imm(bpf_get_prandom_u32) : __clobber_all); }It is verified as follows:
Global function bug1() doesn't return scalar. Only those are supported. 0: R1=ctx() R10=fp0 ; asm volatile (" \ @ verifier_linked_scalars.c:266 0: (85) call bpf_get_prandom_u32#7 ; R0=scalar() 1: (bf) r1 = r0 ; R0=scalar(id=1) R1=scalar(id=1) 2: (04) w1 += 1 ; R1=scalar(id=1+1,smin=0,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff)) 3: (25) if r1 > 0xa goto pc+3 ; R1=scalar(id=1+1,smin=smin32=0,smax=umax=smax32=umax32=10,var_off=(0x0; 0xf)) 4: (77) r0 >>= 32 ; R0=0 5: (15) if r0 == 0x0 goto pc+1 mark_precise: frame0: last_idx 5 first_idx 0 subseq_idx -1 mark_precise: frame0: regs=r0 stack= before 4: (77) r0 >>= 32 mark_precise: frame0: regs=r0 stack= before 3: (25) if r1 > 0xa goto pc+3 mark_precise: frame0: regs=r0,r1 stack= before 2: (04) w1 += 1 mark_precise: frame0: regs=r0,r1 stack= before 1: (bf) r1 = r0 mark_precise: frame0: regs=r0 stack= before 0: (85) call bpf_get_prandom_u32#7 5: R0=0 7: (b7) r0 = 0 ; R0=0 8: (95) exit from 3 to 7: R0=scalar(id=1+0,smin=0,smax=0xffffffff,umin=umin32=10,umax=umax32=0xfffffffe,var_off=(0x0; 0xffffffff)) R1=scalar(id=1+1,smin=umin=umin32=11,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff)) R10=fp0 7: R0=scalar(id=1+0,smin=0,smax=0xffffffff,umin=umin32=10,umax=umax32=0xfffffffe,var_off=(0x0; 0xffffffff)) R1=scalar(id=1+1,smin=umin=umin32=11,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff)) R10=fp0 7: (b7) r0 = 0 ; R0=0 8: (95) exit processed 10 insns (limit 1000000) max_states_per_insn 0 total_states 1 peak_states 1 mark_read 0
Quoted 8 lines
The problem here is that at instruction
2: w1 += 1id of the register is preserved, while this instruction destructs higher bits of the register.After each instruction you must maintain that for a set of linked registers R ∀ r ∈ R: value(r) - off(r) == C, where C is some constant representing the id. This is not the case for operation
w1 += 1if you don't make sure prior to the operation that upper bits ofr1are zero.
Thanks for the detailed explanation. My fix of BPF_ADD_CONST32 and BPF_ADD_CONST64 was trying to fix the symptom but not the bug. As you explained, the link should be broken if we do an alu32 operation and the upper 32 bits are non-zero. I will fix it properly and send the next version.
PATCH v2 2/2 selftests/bpf: Add tests for improved linked register tracking
In reply to Puranjay MohanAdd tests for linked register tracking with negative offsets, BPF_SUB, and alu32. These test for all edge cases like over-flows, etc.
--- .../bpf/progs/verifier_linked_scalars.c | 232 +++++++++++++++++- 1 file changed, 230 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/progs/verifier_linked_scalars.c +230 -2
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/bpf.h> +#include <limits.h> #include <bpf/bpf_helpers.h> #include "bpf_misc.h"
@@ -18,9 +19,9 @@ __naked void scalars(void) r4 = r1; \ w2 +=0x7FFFFFFF ; \ w4 +=0 ; \ - if r2 == 0 goto l1; \ + if r2 == 0 goto l0_%=; \ exit; \ -l1: \ +l0_%=: \ r4 >>=63 ; \ r3 =1 ; \ r3 -= r4; \
@@ -31,4 +32,231 @@ l1: \ " ::: __clobber_all); } +SEC("socket") +__success +__naked void scalars_neg (void ) +{ + asmvolatile (" \ +call %[bpf_get_prandom_u32]; \ +r0 &= 0xff; \ +r1 = r0; \ +r1 += -4; \ +if r1 s< 0 goto l0_%=; \ +if r0 != 0 goto l0_%=; \ +r0 /= 0; \ +l0_%=: \ +r0 = 0; \ +exit; \ +" : + : __imm(bpf_get_prandom_u32) + : __clobber_all); +} + +/* Same test but using BPF_SUB instead of BPF_ADD with negative immediate */ +SEC("socket") +__success +__naked void scalars_neg_sub (void ) +{ + asmvolatile (" \ +call %[bpf_get_prandom_u32]; \ +r0 &= 0xff; \ +r1 = r0; \ +r1 -= 4; \ +if r1 s< 0 goto l0_%=; \ +if r0 != 0 goto l0_%=; \ +r0 /= 0; \ +l0_%=: \ +r0 = 0; \ +exit; \ +" : + : __imm(bpf_get_prandom_u32) + : __clobber_all); +} + +/* alu32 with negative offset */ +SEC("socket") +__success +__naked void scalars_neg_alu32_add (void ) +{ + asmvolatile (" \ +call %[bpf_get_prandom_u32]; \ +w0 &= 0xff; \ +w1 = w0; \ +w1 += -4; \ +if w1 s< 0 goto l0_%=; \ +if w0 != 0 goto l0_%=; \ +r0 /= 0; \ +l0_%=: \ +r0 = 0; \ +exit; \ +" : + : __imm(bpf_get_prandom_u32) + : __clobber_all); +} + +/* alu32 with negative offset using SUB */ +SEC("socket") +__success +__naked void scalars_neg_alu32_sub (void ) +{ + asmvolatile (" \ +call %[bpf_get_prandom_u32]; \ +w0 &= 0xff; \ +w1 = w0; \ +w1 -= 4; \ +if w1 s< 0 goto l0_%=; \ +if w0 != 0 goto l0_%=; \ +r0 /= 0; \ +l0_%=: \ +r0 = 0; \ +exit; \ +" : + : __imm(bpf_get_prandom_u32) + : __clobber_all); +} + +/* Positive offset: r1 = r0 + 4, then if r1 >= 6, r0 >= 2, so r0 != 0 */ +SEC("socket") +__success +__naked void scalars_pos (void ) +{ + asmvolatile (" \ +call %[bpf_get_prandom_u32]; \ +r0 &= 0xff; \ +r1 = r0; \ +r1 += 4; \ +if r1 < 6 goto l0_%=; \ +if r0 != 0 goto l0_%=; \ +r0 /= 0; \ +l0_%=: \ +r0 = 0; \ +exit; \ +" : + : __imm(bpf_get_prandom_u32) + : __clobber_all); +} + +/* SUB with negative immediate: r1 -= -4 is equivalent to r1 += 4 */ +SEC("socket") +__success +__naked void scalars_sub_neg_imm (void ) +{ + asmvolatile (" \ +call %[bpf_get_prandom_u32]; \ +r0 &= 0xff; \ +r1 = r0; \ +r1 -= -4; \ +if r1 < 6 goto l0_%=; \ +if r0 != 0 goto l0_%=; \ +r0 /= 0; \ +l0_%=: \ +r0 = 0; \ +exit; \ +" : + : __imm(bpf_get_prandom_u32) + : __clobber_all); +} + +/* Double ADD clears the ID (can't accumulate offsets) */ +SEC("socket") +__failure +__msg("div by zero") +__naked void scalars_double_add(void) +{ + asmvolatile (" \ +call %[bpf_get_prandom_u32]; \ +r0 &= 0xff; \ +r1 = r0; \ +r1 += 2; \ +r1 += 2; \ +if r1 < 6 goto l0_%=; \ +if r0 != 0 goto l0_%=; \ +r0 /= 0; \ +l0_%=: \ +r0 = 0; \ +exit; \ +" : + : __imm(bpf_get_prandom_u32) + : __clobber_all); +} + +/* +* Test that sync_linked_regs() correctly handles large offset differences. +* r1.off = S32_MIN, r2.off = 1, delta = S32_MIN - 1 requires 64-bit math. +*/ +SEC("socket") +__success +__naked void scalars_sync_delta_overflow (void ) +{ + asmvolatile (" \ +call %[bpf_get_prandom_u32]; \ +r0 &= 0xff; \ +r1 = r0; \ +r2 = r0; \ +r1 += %[s32_min]; \ +r2 += 1; \ +if r2 s< 100 goto l0_%=; \ +if r1 s< 0 goto l0_%=; \ +r0 /= 0; \ +l0_%=: \ +r0 = 0; \ +exit; \ +" : + : __imm(bpf_get_prandom_u32), + [s32_min]"i"(INT_MIN) + : __clobber_all); +} + +/* +* Another large delta case: r1.off = S32_MAX, r2.off = -1. +* delta = S32_MAX - (-1) = S32_MAX + 1 requires 64-bit math. +*/ +SEC("socket") +__success +__naked void scalars_sync_delta_overflow_large_range (void ) +{ + asmvolatile (" \ +call %[bpf_get_prandom_u32]; \ +r0 &= 0xff; \ +r1 = r0; \ +r2 = r0; \ +r1 += %[s32_max]; \ +r2 += -1; \ +if r2 s< 0 goto l0_%=; \ +if r1 s>= 0 goto l0_%=; \ +r0 /= 0; \ +l0_%=: \ +r0 = 0; \ +exit; \ +" : + : __imm(bpf_get_prandom_u32), + [s32_max]"i"(INT_MAX) + : __clobber_all); +} + +/* +* Test linked scalar tracking with alu32 and large positive offset (0x7FFFFFFF). +* After w1 += 0x7FFFFFFF, w1 wraps to negative for any r0 >= 1. +* If w1 is signed-negative, then r0 >= 1, so r0 != 0. +*/ +SEC("socket") +__success +__naked void scalars_alu32_big_offset (void ) +{ + asmvolatile (" \ +call %[bpf_get_prandom_u32]; \ +w0 &= 0xff; \ +w1 = w0; \ +w1 += 0x7FFFFFFF; \ +if w1 s>= 0 goto l0_%=; \ +if w0 != 0 goto l0_%=; \ +r0 /= 0; \ +l0_%=: \ +r0 = 0; \ +exit; \ +" : + : __imm(bpf_get_prandom_u32) + : __clobber_all); +} + char _license[] SEC("license") = "GPL";
Signature · 1 line
2.47.3
Quoted 1 line · Puranjay Mohan
-----------------------------------------------------------------------------
Quoted 8 lines
./veristat/src/veristat -C -e file,prog,states,insns -f "insns_pct>20" fb_before.csv fb_after.csv
File Program States (A) States (B) States (DIFF) Insns (A) Insns (B) Insns (DIFF) ------------------------------------------------- ---------------- ---------- ---------- --------------- --------- --------- ---------------- bpfjailer-lib-tests-bpf-dbus_test-dbus_test.bpf.o do_parse 6019 7631 +1612 (+26.78%) 124809 151707 +26898 (+21.55%) bpfjailer-lib-tests-bpf-dbus_test-dbus_test.bpf.o do_sendmsg 6020 7632 +1612 (+26.78%) 124823 151721 +26898 (+21.55%) third-party-scx-6.9-scheds-rust-scx_layered-bpf_ layered_runnable 125 148 +23 (+18.40%) 1940 2381 +441 (+22.73%) third-party-scx-backports-6.9-1.0.11.1-scheds-r layered_runnable 133 155 +22 (+16.54%) 1700 2152 +452 (+26.59%) third-party-scx-backports-6.9-1.0.8-scheds-rust layered_runnable 114 136 +22 (+19.30%) 1499 1951 +452 (+30.15%)
This regression needs to be investigated. Figure out which part of the diff is causing it. Is this cpu=v4 only? What cpu=v3 numbers look like?
pw-bot: cr