bpf: Speed up loads with entries
Subsystems (5)
cover bpf: Speed up loads with entries
From: Alexei Starovoitov <[email protected]>
Follow-up to "bpf: Share subprogs between programs".
The time of a load with entries grows with the number of main programs times the size of all code. Patch 1 cuts the work that each main program does over code that it doesn't get to. Patch 2 checks global functions once for struct_ops, kprobes and tracepoints too.
64 tc programs, 5K insns each, that call one common function:
copies 0.42 s, shared 3.26 s -> 0.52 s
32 kprobe programs that call the same 65 global functions:
copies 1.12 s, shared 0.71 s -> 0.10 s, verified insns 922751 -> 28990
Verified insns of scx_lavd 54574 -> 46541, scx_rusty 12697 -> 8703.
Alexei Starovoitov (3):
bpf: Limit the passes of an entry to the functions that it calls
bpf: Check a global function once for all entries selftests/bpf: Test global functions that are checked once
Documentation/bpf/verifier.rst | 20 ++- include/linux/bpf_verifier.h | 9 ++ kernel/bpf/fixups.c | 5 +- kernel/bpf/flow.c | 25 ++-- kernel/bpf/pads.c | 34 +++-- kernel/bpf/precision.c | 16 +-- kernel/bpf/verifier.c | 119 ++++++++++++++---- .../selftests/bpf/prog_tests/share_subprogs.c | 32 ++++- .../selftests/bpf/progs/share_subprogs.c | 68 ++++++++++ 9 files changed, 258 insertions(+), 70 deletions(-)
base-commit: 147acc591553f6b9b64d7d895e32332782e2a798
Signature · 1 line
2.55.0
PATCH 1/3 bpf: Limit the passes of an entry to the functions that it calls
From: Alexei Starovoitov <[email protected]>
Each main program has passes of its own, and each pass walks all insns of the load several times: snapshots, landing pads, loops. With many main programs that have a lot of code of their own the shared load is much slower than copies.
64 tc programs, 5K insns each, that call one common function:
copies 0.42 s, shared 3.26 s -> 0.52 s
A main program needs only the functions that it gets to. Keep the call graph of sort_subprogs_topo(), mark these functions and walk only them. The graph has no edges of callx and of dead code, so mark the functions that have their address taken or are recursive for all main programs.
The last main program still walks all functions, so what is emitted is settled over the whole load as before. A conflict in a caller that only an earlier program gets to shows there, and the load is rejected. A load with one main program is not affected.
--- include/linux/bpf_verifier.h | 9 ++++++++ kernel/bpf/fixups.c | 5 +++-- kernel/bpf/flow.c | 25 +++++++++++---------- kernel/bpf/pads.c | 34 ++++++++++++++++++---------- kernel/bpf/precision.c | 16 +++++++------- kernel/bpf/verifier.c | 43 +++++++++++++++++++++++++++--------- 6 files changed, 88 insertions(+), 44 deletions(-)
include/linux/bpf_verifier.h +9 -0
@@ -2013,6 +2013,8 @@ struct bpf_verifier_env {struct bpf_subprog_info *subprog_info;/* subprog indices sorted in topological order: leaves first, callers last */ int *subprog_topo_order; +struct bpf_call_graph calls;/* marks: see mark_pass_subprogs() */ +u32 pass_len;/* insns of the marked subprogs */ /* * Pointers to static subprogs found in frozen read-only maps of the * program, see resolve_func_ptrs(). Sorted by map and map_off.
@@ -2204,6 +2206,13 @@ static inline void bpf_note_bug(struct bpf_verifier_env *env) bpf_log(&env->log, "verifier bug: " fmt "\n", ##args); \ }) +/* Two loops, break does not leave it */ +#define bpf_for_each_pass_insn (env, i) \ + for (u32 ___s = 0, ___end; ___s < (env)->subprog_cnt; ___s++) \ + for (i = (env)->subprog_info[___s].start, \ + ___end = (env)->calls.marks[___s] ? (env)->subprog_info[___s + 1].start : 0; \ + i < ___end; i++) +static inline void mark_calls_callback (struct bpf_verifier_env *env,int idx) { env->insn_aux_data[idx].calls_callback = true;
kernel/bpf/fixups.c +3 -2
@@ -708,7 +708,7 @@ int bpf_mark_subprogs_that_unwind(struct bpf_verifier_env *env, bool in_pass) do { changed = false; /* callees come first: only callx and a cycle of calls take more than one round */ - err = in_pass ? bpf_add_cost_and_check(env, env->prog->len, BPF_COST_SCAN) : + err = in_pass ? bpf_add_cost_and_check(env, env->pass_len, BPF_COST_SCAN) : bpf_must_abort() ? -EINTR : 0; if (err) return err;
@@ -716,7 +716,8 @@ int bpf_mark_subprogs_that_unwind(struct bpf_verifier_env *env, bool in_pass) s = env->subprog_topo_order[k]; sub = &env->subprog_info[s];if ((sub->can_unwind && sub->can_cancel) || - (!sub->main && !sub->bail_unwinds)) + (!sub->main && !sub->bail_unwinds) || + (in_pass && !env->calls.marks[s]))continue ; unwinds = sub->can_unwind; cancels = sub->can_cancel;
kernel/bpf/flow.c +13 -12
@@ -344,7 +344,7 @@ bool bpf_flow_loops_lost_bound(struct bpf_verifier_env *env)struct bpf_loop *loop;int h; -for (h =0 ; h < env->prog->len; h++) { +bpf_for_each_pass_insn (env, h) { loop = env->insn_aux_data[h].loop;if (loop && loop->rt_bounded && !flow_loop_is_bounded (env, h))return true;
@@ -388,8 +388,8 @@ static void flow_forget_loop(struct bpf_verifier_env *env, struct bpf_loop *loop/* A pass starts. It verifies the main prog and the global functions. */ void bpf_flow_new_pass (struct bpf_verifier_env *env) { -struct bpf_insn_aux_data *aux = env->insn_aux_data;struct bpf_flow *flow = env->flow; +struct bpf_insn_aux_data *aux;int i;memset (flow->cost,0 , env->subprog_cnt *sizeof (*flow->cost));
@@ -397,7 +397,8 @@ void bpf_flow_new_pass(struct bpf_verifier_env *env) flow->budget_func_cnt =0 ; flow->apart_cost =0 ; flow->start_cnt++; -for (i =0 ; i < env->prog->len; i++, aux++) { +bpf_for_each_pass_insn (env, i) { + aux = &env->insn_aux_data[i];if (aux->callback_loop) aux->callback_loop->this_pass = (struct bpf_loop_per_pass ){};if (aux->recursion_loop)
@@ -422,9 +423,9 @@ void bpf_flow_note_guards(struct bpf_verifier_env *env)struct bpf_insn_aux_data *aux = env->insn_aux_data;int i; -for (i =0 ; i < env->prog->len; i++, aux++) { - aux->needs_guard |= aux->loop && aux->loop->this_pass.needs_guard; - aux->no_guard_edges |= aux->loop && aux->loop->this_pass.no_guard_edges; +bpf_for_each_pass_insn (env, i) { + aux[i].needs_guard |= aux[i].loop && aux[i].loop->this_pass.needs_guard; + aux[i].no_guard_edges |= aux[i].loop && aux[i].loop->this_pass.no_guard_edges; } }
@@ -434,7 +435,7 @@ void bpf_flow_drop_guards(struct bpf_verifier_env *env)struct bpf_insn_aux_data *aux = env->insn_aux_data;int i, h; -for (i =0 ; i < env->prog->len; i++) { +bpf_for_each_pass_insn (env, i) { h =bpf_loop_at_index (env, i);if (h >=0 ) aux[i].loop_guard &= aux[h].needs_guard |
@@ -534,11 +535,11 @@ void bpf_flow_new_main(struct bpf_verifier_env *env) env->flow->start_cnt =0 ;memset (&env->per_pass,0 , offsetof(struct bpf_env_per_pass , seen_direct_write)); -for (i =0 ; i < env->prog->len; i++, aux++) { - aux->pessimized = false; -flow_reset_loop (env, aux->loop); -flow_reset_loop (env, aux->callback_loop); -flow_reset_loop (env, aux->recursion_loop); +bpf_for_each_pass_insn (env, i) { + aux[i].pessimized = false; +flow_reset_loop (env, aux[i].loop); +flow_reset_loop (env, aux[i].callback_loop); +flow_reset_loop (env, aux[i].recursion_loop); } }
kernel/bpf/pads.c +22 -12
@@ -535,23 +535,26 @@ void bpf_pads_keep(struct bpf_verifier_env *env, struct bpf_pads_moved *kept)struct bpf_insn_aux_data *aux = env->insn_aux_data;int i; -for (i =0 ; i < env->prog->len && !env->no_mem; i++, aux++, kept++) - env->no_mem =copy_plans (env, &kept->plans, aux->pad_plans) || -copy_notes (env, &kept->note, aux->pad_note); +bpf_for_each_pass_insn (env, i) { + env->no_mem = env->no_mem ||copy_plans (env, &kept->plans, aux[i].pad_plans) || +copy_notes (env, &kept->note, aux[i].pad_note); + kept++; + } }/* * The records and the notes are results of a pass, so they move with the snapshot of the pass, see -* copy_snapshot(): to @moved, which has an element per insn, if @save, and back otherwise. What was -* at the destination is freed. +* copy_snapshot(): to @moved, which has an element per insn of the pass, if @save, and back +* otherwise. What was at the destination is freed. */ void bpf_pads_move (struct bpf_verifier_env *env,struct bpf_pads_moved *moved,bool save) { -struct bpf_insn_aux_data *aux = env->insn_aux_data; +struct bpf_insn_aux_data *aux;int i; env->pad_skip_gen++; -for (i =0 ; i < env->prog->len; i++, aux++, moved++) { +bpf_for_each_pass_insn (env, i) { + aux = &env->insn_aux_data[i];if (save) { env->mem -=free_plans (moved->plans); env->mem -=bpf_pad_note_free (moved->note);
@@ -567,6 +570,7 @@ void bpf_pads_move(struct bpf_verifier_env *env, struct bpf_pads_moved *moved, b moved->plans =NULL ; moved->note =NULL ; } + moved++; } }
@@ -2542,10 +2546,12 @@ int bpf_pads_must_unwind(struct bpf_verifier_env *env) do { changed = false; - err = bpf_add_cost_and_check(env, env->prog->len, BPF_COST_SCAN); + err = bpf_add_cost_and_check(env, env->pass_len, BPF_COST_SCAN); if (err) return err; for (k = 0; k < env->subprog_cnt; k++) { + if (!env->calls.marks[env->subprog_topo_order[k]]) + continue; sub = &env->subprog_info[env->subprog_topo_order[k]]; must = sub->must_unwind; must_cancel = sub->must_cancel;
@@ -2612,10 +2618,12 @@ static int sites_of_subprogs(struct bpf_verifier_env *env) } do { changed = false; - err = bpf_add_cost_and_check(env, env->prog->len, BPF_COST_SCAN); + err = bpf_add_cost_and_check(env, env->pass_len, BPF_COST_SCAN); if (err) return err; for (k = 0; k < env->subprog_cnt; k++) { + if (!env->calls.marks[env->subprog_topo_order[k]]) + continue; sub = &env->subprog_info[env->subprog_topo_order[k]]; kinds = sub->left_at; for (i = sub->start; i < sub[1].start; i++) {
@@ -2982,6 +2990,8 @@ static int settle_slots(struct bpf_verifier_env *env) env->subprog_info[s].pend_off =0 ; }for (s =0 ; s < env->subprog_cnt; s++) { +if (!env->calls.marks[s]) +continue ; sub = &env->subprog_info[s]; all_ptrs = !acquires_stack_obj (env, sub); do {
@@ -3471,7 +3481,7 @@ void bpf_pads_mark_used(struct bpf_verifier_env *env)struct bpf_pad_plan *plan;int i; -for (i =0 ; i < env->prog->len; i++) +bpf_for_each_pass_insn (env, i)for (plan = env->insn_aux_data[i].pad_plans; plan; plan = plan->next) plan->used =bpf_site_is_used (env, i, plan); }
@@ -3493,7 +3503,7 @@ int bpf_pads_settle(struct bpf_verifier_env *env)if (err)return err;/* sort the releases first: hidden slots are inserted into that order */ -for (i =0 ; i < env->prog->len; i++) { +bpf_for_each_pass_insn (env, i) {for (plan = env->insn_aux_data[i].pad_plans; plan; plan = plan->next) { err =order_ents (env, plan);if (err)
@@ -3503,7 +3513,7 @@ int bpf_pads_settle(struct bpf_verifier_env *env) err =settle_slots (env);if (err)return err; -for (i =0 ; i < env->prog->len; i++) { +bpf_for_each_pass_insn (env, i) {if (env->insn_aux_data[i].pad_plans &&bpf_must_abort ())return -EINTR ;for (plan = env->insn_aux_data[i].pad_plans; plan; plan = plan->next) {
kernel/bpf/precision.c +8 -8
@@ -720,11 +720,11 @@ void bpf_find_unproven_after_pass(struct bpf_verifier_env *env)for (i =0 ; i < env->subprog_cnt; i++)note_unproven_after_pass (env, env->subprog_info[i].deepest_insn); -for (i =0 ; i < env->prog->len; i++, insn++, aux++) { -if (aux->pessimized ||bpf_pads_conflict (env, aux)) { +bpf_for_each_pass_insn (env, i) { +if (aux[i].pessimized ||bpf_pads_conflict (env, &aux[i])) {note_unproven_after_pass (env, i); - }else if (aux->seen && (bpf_calls_function (env, i) ||is_stack_arg_st (insn) || -is_stack_arg_stx (insn))) { + }else if (aux[i].seen && (bpf_calls_function (env, i) ||is_stack_arg_st (&insn[i]) || +is_stack_arg_stx (&insn[i]))) {__set_bit (i, env->precision->this_pass.call_insns);suspect_loops_of_insn (env, i); }
@@ -1694,7 +1694,7 @@ int bpf_partition_for_pessimized(struct bpf_verifier_env *env)/* bpf_partition_more() can fail before partition_next() makes this copy */ if (prec->data_deps)memcpy (prec->undo, prec->data_deps,5 * prec->words *sizeof (long )); -for (i =0 ; i < env->prog->len; i++) +bpf_for_each_pass_insn (env, i)if (env->insn_aux_data[i].pessimized)note_unproven_after_pass (env, i); ret =bpf_partition_more (env);
@@ -1814,9 +1814,9 @@ int bpf_precision_init(struct bpf_verifier_env *env) prec->join_live_types =bpf_prog_is_offloaded (env->prog->aux); prec->budget_left = prec->budget =bpf_precision_first_budget (env); prec->first_unproven_insn = prec->loop_carried_at =U32_MAX ; -for (i =0 ; i < env->prog->len && !err; i++, aux++) - err =add_loop (prec, aux->loop) ?:add_loop (prec, aux->callback_loop) ?: -add_loop (prec, aux->recursion_loop); +bpf_for_each_pass_insn (env, i) + err = err ?:add_loop (prec, aux[i].loop) ?:add_loop (prec, aux[i].callback_loop) ?: +add_loop (prec, aux[i].recursion_loop);return err; }
kernel/bpf/verifier.c +33 -10
114 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH 2/3 bpf: Check a global function once for all entries
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
A global function is checked again for each main program that calls it, unless the type converts ctx access. So struct_ops, kprobe and tracepoint programs share the code, but not the time to verify it.
32 kprobe programs that call the same 65 global functions:
copies 1.12 s, shared 0.71 s -> 0.10 s, verified insns 922751 -> 28990
Verified insns of scx_lavd 54574 -> 46541, scx_rusty 12697 -> 8703.
Check it once. What depends on the attach point in a function without ctx is which helpers, kfuncs and maps it may use, and the value of bpf_throw(). So before the passes of a main program ask for each such insn that was seen in the functions that it gets to. When the answer is no, check the function and its callers again, as before. The same for a function with ctx.
--- Documentation/bpf/verifier.rst | 20 +++------ kernel/bpf/verifier.c | 78 ++++++++++++++++++++++++++++------ 2 files changed, 72 insertions(+), 26 deletions(-)
Documentation/bpf/verifier.rst +7 -13
@@ -1153,19 +1153,13 @@ any member, sleepable or not. Each main program has passes of its own and the full limit on cost, see ``do_check_mains()``. The passes start with no precision, and with the fixups of the programs before it. ``enter_main()`` makes ``env->prog`` answer for the -attach point of the main program. What it calls is checked like this: - -============================================= =================================== -function checked -============================================= =================================== -static for each main program that calls it -global, the type does not convert ctx access the same -global, the type converts ctx access once -============================================= =================================== - -Main programs of a type that converts ctx access, like tc and XDP, are the same -to a function. For other types, like struct_ops and tracepoints, the ctx, the -kfuncs and the valid results depend on the attach point. +attach point of the main program. + +A static function is checked for each main program that calls it. A global +function is checked once. It is checked again for a main program when it gets +the ctx, or when it gets to an insn that depends on the attach point, like the +call of a kfunc, see ``mark_check_again()``. Main programs of a type that +converts ctx access, like tc and XDP, are the same to a function. An insn has one fixup, a loop has one ``may_goto`` and a site has one landing pad for the states of all main programs. A main program that is proven is not
kernel/bpf/verifier.c +65 -13
107 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH 3/3 selftests/bpf: Test global functions that are checked once
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
Check that a global function is verified once, and that it's verified again when a kfunc below it is for one member only, when unwind_exit gives the result of the member, when it reads the ctx, and when it uses a map that is not for sleepable programs.
--- .../selftests/bpf/prog_tests/share_subprogs.c | 32 ++++++++- .../selftests/bpf/progs/share_subprogs.c | 68 +++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/share_subprogs.c +31 -1
@@ -113,6 +113,29 @@ static int run_tp(int fd)return bpf_prog_test_run_opts (fd, &opts) ?: opts.retval; } +static void test_once (void ) +{ +static char log[1 <<20 ]; + LIBBPF_OPTS(bpf_object_open_opts, opts, .kernel_log_buf = log, .kernel_log_level = 2, + .kernel_log_size = sizeof(log)); +struct share_subprogs *skel =share_subprogs__open_opts (&opts); +struct bpf_program *prog; +char *safe; + +if (!ASSERT_OK_PTR (skel,"open" )) +return ; +bpf_object__for_each_program (prog, skel->obj) +bpf_program__set_autoload (prog, false); +bpf_map__set_autocreate (skel->maps.once, true); +if (ASSERT_OK (share_subprogs__load (skel),"load" )) { +ASSERT_NEQ (owner_of (bpf_program__fd (skel->progs.once_2)),0 ,"shared" ); + safe =strstr (log,"('set_opt') is safe" ); +if (ASSERT_OK_PTR (safe,"checked" )) +ASSERT_NULL (strstr (safe +1 ,"('set_opt') is safe" ),"once" ); + } +share_subprogs__destroy (skel); +} +static void test_signed (void ) {struct share_subprogs_signed_lskel *skel =share_subprogs_signed_lskel__open ();
@@ -146,7 +169,8 @@ static void test_pair(const char *name)void serial_test_share_subprogs (void ) {static const char *const pairs[] = { -"helper" ,"kfunc" ,"context" ,"cookie" ,"sleep" ,"rcu" , +"helper" ,"kfunc" ,"nested" ,"callback" ,"unwind" ,"context" , +"cookie" ,"sleep" ,"rcu" ,"rcu_global" , };struct share_subprogs *skel;int i, fd;
@@ -169,6 +193,12 @@ void serial_test_share_subprogs(void)ASSERT_EQ (owner_of (bpf_program__fd (skel->progs.global_1)),0 ,"copies" );if (test__start_subtest ("stuck" ))ASSERT_EQ (owner_of (bpf_program__fd (skel->progs.stuck_1)),0 ,"copies" ); +if (test__start_subtest ("tp_ctx" )) { +ASSERT_NEQ (owner_of (bpf_program__fd (skel->progs.tp_2)),0 ,"shared" ); +ASSERT_EQ (run_tp (bpf_program__fd (skel->progs.tp_2)), -EINVAL ,"no ctx" ); + } +if (test__start_subtest ("once" )) +test_once ();if (test__start_subtest ("arena" ))test_arena ();if (test__start_subtest ("value" ))
tools/testing/selftests/bpf/progs/share_subprogs.c +68 -0
87 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf-next 3/3] selftests/bpf: Test global functions that are checked once
TL;DR: Nits only, in the selftests: the hand-encoded unwind_exit asm could use the existing __imm_insn() idiom, and the PAIR(once) line needs a comment or a move.
Quoted 1 line
selftests/bpf: Test global functions that are checked once
TL;DR: Nits only, in the selftests: the hand-encoded unwind_exit asm could use the existing __imm_insn() idiom, and the PAIR(once) line needs a comment or a move.
Quoted 3 lines
tools/testing/selftests/bpf/progs/share_subprogs.c +0
-0
[ ... ]
tools/testing/selftests/bpf/progs/share_subprogs.c __noinline int watchdog(struct Qdisc *sch __arg_trusted) · line 232
Quoted 8 lines
tools/testing/selftests/bpf/progs/share_subprogs.c +8
-0+__noinline int leaves(int x) +{ +/* unwind_exit */ +if (x) + asmvolatile ("r0 = 5; .byte 0x95, %0, 0, 0, 0, 0, 0, 0;" :: +"i" (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ ?0x10 :0x01 ) :"r0" ); +return 0 ; +}
This isn't a bug, but would the existing __imm_insn() idiom work here, with a struct bpf_insn that sets .src_reg = BPF_UNWIND_EXIT, as in stream.c or unwind_attach.c?
That way the src_reg nibble would not have to be chosen by hand per endianness.
[ ... ]
Quoted 3 lines
tools/testing/selftests/bpf/progs/share_subprogs.c +2
-0+PAIR (unwind,"" , Qdisc_ops, reset, dequeue, throws_or_leaves, zero); +PAIR (once,"" , tcp_congestion_ops, init, cwnd_event, set_opt, (void *)ctx[0 ]);
PAIR(context, "", bpf_testmod_ops, test_maybe_null, test_2, pid, ctx);
This isn't a bug, but PAIR(once) is meant to load, unlike the pairs around it, which are expected to fail.
Could it move next to the value/thrown pairs, or get a short comment, so the comment above the PAIR() macro ("The shared function is valid for one member, and not for the other one") doesn't seem to cover it?
---
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/47013580055
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 Thu, 3 Dec 2026 01:27:17 +0000 you wrote:
From: Alexei Starovoitov <[email protected]>
Follow-up to "bpf: Share subprogs between programs".
The time of a load with entries grows with the number of main programs times the size of all code. Patch 1 cuts the work that each main program does over code that it doesn't get to. Patch 2 checks global functions once for
struct_ops, kprobes and tracepoints too.[...]
Here is the summary with links:
- [bpf-next,1/3] bpf: Limit the passes of an entry to the functions that it calls
https://git.kernel.org/bpf/bpf-next/c/335a63d76580 - [bpf-next,2/3] bpf: Check a global function once for all entries https://git.kernel.org/bpf/bpf-next/c/88c4279999ca - [bpf-next,3/3] selftests/bpf: Test global functions that are checked once https://git.kernel.org/bpf/bpf-next/c/f9cf93ec0217
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html