bpf: The register of an implicit kfunc argument is not live
Subsystems (4)
cover bpf: The register of an implicit kfunc argument is not live
From: Alexei Starovoitov <[email protected]>
The verifier fills in the last arguments of a kfunc that has KF_IMPLICIT_ARGS, for example 'aux' of almost all scx_bpf_*() kfuncs. The prog does not pass them. But the analysis of live registers counts their registers as read by the call. Such a register is live from the previous call to this one. Scalar evolution has no formula for it and gives up on the loop, and the verifier compares it when it looks for an equal state.
Patch 1 does not count these registers as used. Patch 2 has the tests.
veristat, progs that load before (A) and after (B):
progs of progs that change insns --------------------- ----- ----------- ------- sched-ext/scx 493 26 -10.52% scx, as BPF CI has it 428 26 -18.87% tools/sched_ext 240 6 -0.08%
selftests 3891 52 -0.32%
36 builds of Cilium 5029 0
No verdict changes. Progs of sched-ext/scx that change by more than 4%, and the progs that need more insns:
Program Insns (A) Insns (B) Insns (DIFF) --------------------- --------- --------- ----------------- lavd_dispatch 281049 5589 -275460 (-98.01%) pandemonium_init 341 95 -246 (-72.14%) arena_selftest_rbtree 140933 95177 -45756 (-32.47%) arena_selftest_atq 171003 125246 -45757 (-26.76%) p2dq_select_cpu 1179 1074 -105 (-8.91%) chaos_select_cpu 1194 1089 -105 (-8.79%) beerland_dispatch 551 515 -36 (-6.53%) layered_running 1693 1590 -103 (-6.08%) p2dq_update_idle 622 590 -32 (-5.14%) mlfq_select_cpu 7227 6929 -298 (-4.12%) mlfq_enqueue 3889 3893 +4 (+0.10%) flow_enqueue 219201 219547 +346 (+0.16%) lavd_enqueue 174292 176453 +2161 (+1.24%)
Alexei Starovoitov (2):
bpf: The register of an implicit kfunc argument is not live selftests/bpf: Test liveness of an implicit kfunc argument
include/linux/bpf_verifier.h | 2 + kernel/bpf/liveness.c | 3 +- kernel/bpf/verifier.c | 9 +++ .../bpf/progs/compute_live_registers.c | 13 +++++ .../selftests/bpf/progs/kfunc_implicit_args.c | 55 +++++++++++++++++++ .../selftests/bpf/progs/verifier_scev.c | 33 +++++++++++ 6 files changed, 114 insertions(+), 1 deletion(-)
base-commit: a30c772627b2ef4bc715332b1b04938a914556cd
Signature · 1 line
2.55.0
PATCH 1/2 bpf: The register of an implicit kfunc argument is not live
From: Alexei Starovoitov <[email protected]>
The verifier fills in the last arguments of a kfunc that has KF_IMPLICIT_ARGS. The prog does not pass them:
bpf_stream_vprintk(stream_id, fmt, args, len) the prog calls
bpf_stream_vprintk(stream_id, fmt, args, len, aux) the kernel has
The prog sets r1-r4. The verifier adds 'r5 = aux' in front of the call.
bpf_compute_live_registers() counts the arguments of the second prototype. Thus r5 is live before the call, and up to the previous insn that writes to r5. In a loop that is the same call in the previous iteration: r5 is live at the loop head, its value is what the call left in it, and it has no formula. Scalar evolution gives up on a loop that has such a register. The verifier also compares r5 when it looks for an equal state.
Let bpf_get_call_summary() tell how many of the last argument slots are implicit, and do not count them as used.
Nothing reads r5 between the last write of the prog and 'r5 = aux':check_func_arg() does not look at the register of such an argument, and a jump to the call goes to the added insn. The call clobbers r5 as before.
Almost all scx_bpf_*() kfuncs have an implicit argument. So have bpf_obj_new(), bpf_obj_drop(), bpf_list_push_*(), bpf_rbtree_add() and bpf_stream_*().
Insns that the verifier processes:
progs progs that change insns ----------------------- ----------------- ------- scx, as BPF CI has it 26 of 466 -18.9 % scx, master 26 of 563 -10.5 % selftests 52 of 6329 -0.3 %
lavd_dispatch of scx_lavd goes from 281049 to 5589. No prog of Cilium changes, no verdict changes. In each set of scx 3 progs get more insns, at most 1.2 %: a path ends earlier, and that moves where later paths end.
--- include/linux/bpf_verifier.h | 2 ++ kernel/bpf/liveness.c | 3 ++- kernel/bpf/verifier.c | 9 +++++++++ 3 files changed, 13 insertions(+), 1 deletion(-)
include/linux/bpf_verifier.h +2 -0
@@ -1235,6 +1235,8 @@ static inline bool bpf_ret_reg_pair(struct bpf_verifier_env *env, int subprog)struct bpf_call_summary {u8 arg_slot_cnt; +/* the last of them are filled in by the verifier, the prog doesn't pass them */ +u8 implicit_slot_cnt;bool is_void;bool fastcall; };
kernel/bpf/liveness.c +2 -1
@@ -2621,7 +2621,8 @@ static void compute_insn_live_regs(struct bpf_verifier_env *env, def =ALL_CALLER_SAVED_REGS ; use = def & ~BIT (BPF_REG_0 );if (bpf_get_call_summary (env, insn, &cs)) { - use =GENMASK (min_t (u8, cs.arg_slot_cnt,MAX_BPF_FUNC_REG_ARGS ),1 ); + use =GENMASK (min_t (u8, cs.arg_slot_cnt - cs.implicit_slot_cnt, +MAX_BPF_FUNC_REG_ARGS ),1 ); }else if (bpf_pseudo_call (insn)) {/* a static callee's reads are added in bpf_compute_live_registers() */ subprog =callee_subprog (env, insn);
kernel/bpf/verifier.c +9 -0
@@ -19687,6 +19687,7 @@ bool bpf_get_call_summary(struct bpf_verifier_env *env, struct bpf_insn *call, bpf_jit_inlines_helper_call(call->imm)); cs->is_void = fn->ret_type ==RET_VOID ; cs->arg_slot_cnt =0 ; + cs->implicit_slot_cnt =0 ;for (i =0 ; i <ARRAY_SIZE (fn->arg_type); ++i) {if (fn->arg_type[i] ==ARG_UNUSED )break ;
@@ -19703,6 +19704,14 @@ bool bpf_get_call_summary(struct bpf_verifier_env *env, struct bpf_insn *call,/* error would be reported later */ return false; cs->arg_slot_cnt =btf_proto_slots (meta.btf, meta.func_proto); + cs->implicit_slot_cnt =0 ; +if (meta.kfunc_flags &KF_IMPLICIT_ARGS ) { +const struct btf_type *func =btf_type_by_id (meta.btf, meta.func_id); + +/* the prototype without implicit args, see is_kfunc_arg_implicit() */ + cs->implicit_slot_cnt = cs->arg_slot_cnt - +btf_proto_slots (meta.btf,btf_type_by_id (meta.btf, func->type)); + } cs->fastcall = meta.kfunc_flags &KF_FASTCALL ; cs->is_void =btf_type_is_void (btf_type_by_id (meta.btf, meta.func_proto->type));return true;
Signature · 1 line
2.55.0
PATCH 2/2 selftests/bpf: Test liveness of an implicit kfunc argument
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
The register of an implicit argument is not live before the call:
kfunc live ------------------------------------ ----- bpf_stream_vprintk() r1-r4 bpf_kfunc_implicit_arg_legacy() r1-r2 bpf_kfunc_implicit_arg_legacy_impl() r1-r3
The last kfunc has no KF_IMPLICIT_ARGS: the prog passes all three arguments.
kfunc_implicit_arg_in_a_loop calls bpf_stream_print_stack() 4 times. r2 is its implicit argument. The test expects that the loop is counted and that each live register has a formula.
Two more progs run. One jumps to the call, one has may_goto in front of the call. The kfunc has to get aux in both.
--- .../bpf/progs/compute_live_registers.c | 13 +++++ .../selftests/bpf/progs/kfunc_implicit_args.c | 55 +++++++++++++++++++ .../selftests/bpf/progs/verifier_scev.c | 33 +++++++++++ 3 files changed, 101 insertions(+)
tools/testing/selftests/bpf/progs/compute_live_registers.c +13 -0
@@ -538,6 +538,19 @@ void gotox(void)#endif /* gotox: x86, arm64, riscv64 */ +extern int bpf_stream_vprintk(int stream_id, const char *fmt__str, const void *args, + __u32 len__sz) __weak __ksym; + +/* The verifier fills in the implicit argument of the kfunc. The prog does not pass r5. */ +SEC("syscall") +__log_level(2) +__msg(": .1234..... (85) call bpf_stream_vprintk") +int kfunc_implicit_arg (void *ctx) +{ +bpf_stream_printk (1 /* BPF_STDOUT */ ,"x" ); +return 0 ; +} +/* to retain debug info for BTF generation */ void kfunc_root (void ) {
tools/testing/selftests/bpf/progs/kfunc_implicit_args.c +55 -0
70 lines not on this page · raw message
tools/testing/selftests/bpf/progs/verifier_scev.c +33 -0
@@ -1411,6 +1411,39 @@ __naked void widened_reg_is_not_linked(void) : __clobber_all); } +extern int bpf_stream_print_stack(int stream_id) __weak __ksym; + +/* +* The verifier fills in r2, the implicit argument of the kfunc. The call clobbers r2 and nothing +* sets it: if r2 is live at the head of the loop it has no formula. +*/ +SEC("syscall") +__success __retval(0) +__log_level(2) +__msg("loop header at 1, header_count is 4 ") +__not_msg("can't widen") +__naked void kfunc_implicit_arg_in_a_loop (void ) +{ + asmvolatile ( +"r6 = 0;" +"1:" +"r1 = 2;" +"call %[bpf_stream_print_stack];" +"r6 += 1;" +"if r6 < 4 goto 1b;" +"r0 = 0;" +"exit;" + : + : __imm(bpf_stream_print_stack) + : __clobber_all); +} + +/* to retain debug info for BTF generation */ +void kfunc_root (void ) +{ +bpf_stream_print_stack (0 ); +} +/* The unknown SCEV of an unbound STACK_MISC slot must not prevent widening. */ SEC ("tracepoint" ) __success
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf-next 2/2] selftests/bpf: Test liveness of an implicit kfunc argument
TL;DR: The new selftest uses the may_goto mnemonic in inline asm without an __BPF_FEATURE_MAY_GOTO guard, so older clang (e.g. 18) may fail to assemble it and break the whole selftest build.
Quoted 1 line
selftests/bpf: Test liveness of an implicit kfunc argument
TL;DR: The new selftest uses the may_goto mnemonic in inline asm without an __BPF_FEATURE_MAY_GOTO guard, so older clang (e.g. 18) may fail to assemble it and break the whole selftest build.
Quoted 3 lines
tools/testing/selftests/bpf/progs/kfunc_implicit_args.c +0
-0
[ ... ]
Quoted 5 lines
tools/testing/selftests/bpf/progs/kfunc_implicit_args.c +5
-0+/* +* r0 and r1 are live at may_goto, r2 is the first register that is not. The +* insns that the verifier adds for may_goto are free to use r2: 'r2 = aux' +* comes behind them. +*/
tools/testing/selftests/bpf/progs/kfunc_implicit_args.c int test_kfunc_implicit_arg_impl_illegal(void *ctx) · line 85
Quoted 10 lines
tools/testing/selftests/bpf/progs/kfunc_implicit_args.c +10
-0+SEC("syscall") +__retval(5) +__naked void implicit_arg_after_may_goto (void ) +{ + asmvolatile ( +"r6 = 1;" +"1:" +"r0 = r6;" +"r1 = r6;" + "may_goto 2f;"
^^^^
Can this build with a clang that predates the BPF assembler support for the may_goto mnemonic?
implicit_arg_after_may_goto() writes may_goto straight into the inline asm with no guard. The LLVM BPF assembler learned the mnemonic after LLVM 18 was released, together with the __BPF_FEATURE_MAY_GOTO macro. Commit 965c6167c93f ("selftests/bpf: Using llvm may_goto inline asm for cond_break macro") says "Using llvm may_goto insn is controlled by macro __BPF_FEATURE_MAY_GOTO".
With an older clang, the asm fails to assemble, so kfunc_implicit_args.bpf.o is not built. kfunc_implicit_args.skel.h is then missing, and test_progs and its no_alu32 and cpuv4 flavors fail to build. That takes down the whole BPF selftest build, not only this test.
This looks like the only unguarded use of the mnemonic under progs/. The other users either guard it:
verifier_large_stack.c and verifier_stack_ptr.c:
#ifdef __BPF_FEATURE_MAY_GOTO
...
may_goto l0_%=;or encode the instruction as raw bytes, as compute_live_registers.c and verifier_may_goto_1.c do:
".8byte %[may_goto];"
...
__imm_insn(may_goto, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, ...))libarena/include/bpf_may_goto.h also falls back to ".byte 0xe5 ..." when __BPF_FEATURE_MAY_GOTO is not defined.
The selftests still support pre-19 compilers on purpose: bpf_misc.h gates features on __clang_major__ >= 18, and commit f7213961e8e2 gates on LLVM 23 and falls back to a dummy test. Ubuntu 24.04 ships clang 18 as its default. The default LLVM version in .github/scripts/matrix.py is 21, so that configuration would not exercise this.
Should implicit_arg_after_may_goto() be wrapped in #ifdef __BPF_FEATURE_MAY_GOTO, as verifier_large_stack.c does? Alternatively, could the instruction be encoded with .8byte and __imm_insn(), as compute_live_registers.c does? For that option the file would need BPF_RAW_INSN, since it includes vmlinux.h and not ../../../include/linux/filter.h.
tools/testing/selftests/bpf/progs/kfunc_implicit_args.c int test_kfunc_implicit_arg_impl_illegal(void *ctx) · line 96
Quoted 11 lines
tools/testing/selftests/bpf/progs/kfunc_implicit_args.c +11
-0+ "call %[bpf_kfunc_implicit_arg];" + "if r0 != r6 goto 2f;" + "r6 += 1;" + "if r6 < 5 goto 1b;" + "r0 = r6;" +"2:" + "exit;" + : + : __imm(bpf_kfunc_implicit_arg) + : __clobber_all); +}
---
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/41334428337
Re: [PATCH bpf-next 2/2] selftests/bpf: Test liveness of an implicit kfunc argument
Quoted 10 lines · On Sun, Nov 01, 2026 at 10:31 AM [email protected] <[email protected]>…
tools/testing/selftests/bpf/progs/kfunc_implicit_args.c int test_kfunc_implicit_arg_impl_illegal(void *ctx) · line 85
Quoted 8 lines
tools/testing/selftests/bpf/progs/kfunc_implicit_args.c +8
-0+__naked void implicit_arg_after_may_goto (void ) +{ + asmvolatile ( +"r6 = 1;" +"1:" +"r0 = r6;" +"r1 = r6;" + "may_goto 2f;"^^^^
Quoted 2 lines
Can this build with a clang that predates the BPF assembler support for the
may_gotomnemonic?
Not a bug.
clang 18 can't build test_progs without this patch either.
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 10 lines · On Sun, 1 Nov 2026 17:44:03 +0000 you wrote:
From: Alexei Starovoitov <[email protected]>
The verifier fills in the last arguments of a kfunc that has
KF_IMPLICIT_ARGS, for example 'aux' of almost all scx_bpf_*() kfuncs. The prog does not pass them. But the analysis of live registers counts their registers as read by the call. Such a register is live from the previous call to this one. Scalar evolution has no formula for it and gives up on the loop, and the verifier compares it when it looks for an equal state.[...]
Here is the summary with links:
- [bpf-next,1/2] bpf: The register of an implicit kfunc argument is not live
https://git.kernel.org/bpf/bpf-next/c/2b1947689c29 - [bpf-next,2/2] selftests/bpf: Test liveness of an implicit kfunc argument https://git.kernel.org/bpf/bpf-next/c/ef41474ed6cb
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html