* [PATCH bpf-next 0/3] bpf: Speed up loads with entries
@ 2026-12-03 1:27 Alexei Starovoitov
2026-12-03 1:27 ` [PATCH bpf-next 1/3] bpf: Limit the passes of an entry to the functions that it calls Alexei Starovoitov
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: Alexei Starovoitov @ 2026-12-03 1:27 UTC (permalink / raw)
To: bpf; +Cc: daniel, andrii, eddyz87, memxor
From: Alexei Starovoitov <ast@kernel.org>
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.
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
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
--
2.55.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH bpf-next 1/3] bpf: Limit the passes of an entry to the functions that it calls
2026-12-03 1:27 [PATCH bpf-next 0/3] bpf: Speed up loads with entries Alexei Starovoitov
@ 2026-12-03 1:27 ` Alexei Starovoitov
2026-12-03 1:27 ` [PATCH bpf-next 2/3] bpf: Check a global function once for all entries Alexei Starovoitov
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Alexei Starovoitov @ 2026-12-03 1:27 UTC (permalink / raw)
To: bpf; +Cc: daniel, andrii, eddyz87, memxor
From: Alexei Starovoitov <ast@kernel.org>
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.
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
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(-)
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 619df34f0ced..acef0bd410b1 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -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;
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index 4d76b9a83b39..f8c70dd90bfb 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -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;
diff --git a/kernel/bpf/flow.c b/kernel/bpf/flow.c
index 30758913c0a0..4ccd5386805c 100644
--- a/kernel/bpf/flow.c
+++ b/kernel/bpf/flow.c
@@ -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);
}
}
diff --git a/kernel/bpf/pads.c b/kernel/bpf/pads.c
index befe9a729115..25455fd508d8 100644
--- a/kernel/bpf/pads.c
+++ b/kernel/bpf/pads.c
@@ -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) {
diff --git a/kernel/bpf/precision.c b/kernel/bpf/precision.c
index 725de358f5ca..a5cc813817c4 100644
--- a/kernel/bpf/precision.c
+++ b/kernel/bpf/precision.c
@@ -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;
}
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index cc5a39abe9a7..461a0db06b87 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -3779,7 +3779,7 @@ static int sort_subprogs_topo(struct bpf_verifier_env *env)
verbose(env, "topo_order[%d] = %s\n",
i, bpf_subprog_name(env, env->subprog_topo_order[i]));
out:
- bpf_call_graph_free(&cg);
+ env->calls = cg;
return ret;
}
@@ -24836,7 +24836,7 @@ struct bpf_snapshot {
struct bpf_env_per_pass env;
struct bpf_prog_aux aux;
struct bpf_insn_fixups *insns;
- struct bpf_pads_moved *pads; /* per insn, see bpf_pads_move() */
+ struct bpf_pads_moved *pads; /* both: per insn of the pass */
struct bpf_subprog_info *subprogs;
struct bpf_func_info_aux *funcs;
size_t bytes;
@@ -24847,6 +24847,7 @@ static void copy_snapshot(struct bpf_verifier_env *env, struct bpf_snapshot *sna
{
bool has_callchain_buf = env->prog->has_callchain_buf;
struct bpf_prog_aux *aux = env->prog->aux;
+ struct bpf_insn_fixups *insns = snapshot->insns;
struct bpf_subprog_info *sub;
int i;
@@ -24867,8 +24868,8 @@ static void copy_snapshot(struct bpf_verifier_env *env, struct bpf_snapshot *sna
COPY_AUX(tail_call_reachable);
COPY_AUX(jits_use_priv_stack);
COPY_AUX(kprobe_write_ctx);
- for (i = 0; i < env->prog->len; i++)
- COPY(snapshot->insns[i], env->insn_aux_data[i].fixups);
+ bpf_for_each_pass_insn(env, i)
+ COPY(*insns++, env->insn_aux_data[i].fixups);
bpf_pads_move(env, snapshot->pads, save);
for (i = 0, sub = env->subprog_info; i < env->subprog_cnt; i++, sub++) {
/* the sum over all passes */
@@ -24880,14 +24881,14 @@ static void copy_snapshot(struct bpf_verifier_env *env, struct bpf_snapshot *sna
COPY(snapshot->funcs[i], aux->func_info_aux[i]);
#undef COPY_AUX
#undef COPY
- bpf_add_cost(env, env->prog->len, BPF_COST_XREG);
+ bpf_add_cost(env, max(env->pass_len, env->subprog_cnt), BPF_COST_XREG);
}
static struct bpf_snapshot *save_snapshot(struct bpf_verifier_env *env,
struct bpf_snapshot *snapshot)
{
- size_t insns = array_size(env->prog->len, sizeof(*snapshot->insns));
- size_t pads = array_size(env->prog->len, sizeof(*snapshot->pads));
+ size_t insns = array_size(env->pass_len, sizeof(*snapshot->insns));
+ size_t pads = array_size(env->pass_len, sizeof(*snapshot->pads));
size_t subprogs = env->subprog_cnt * sizeof(*snapshot->subprogs);
if (!snapshot) {
@@ -24912,7 +24913,7 @@ static void free_snapshot(struct bpf_verifier_env *env, struct bpf_snapshot *sna
{
if (!snapshot)
return;
- env->mem -= snapshot->bytes + bpf_pads_free_moved(snapshot->pads, env->prog->len);
+ env->mem -= snapshot->bytes + bpf_pads_free_moved(snapshot->pads, env->pass_len);
kvfree(snapshot);
}
@@ -24998,7 +24999,7 @@ static int settle_late_unwinds(struct bpf_verifier_env *env, const struct bpf_sn
sub->can_unwind = initial->subprogs[s].can_unwind;
sub->can_cancel = initial->subprogs[s].can_cancel;
/* The walk did not finish: keep what the states of the pass found. */
- if (err)
+ if (err || !env->calls.marks[s])
continue;
late |= bpf_subprog_is_global(env, s) &&
((unwinds && !initial->subprogs[s].did_unwind) ||
@@ -25528,7 +25529,7 @@ static int check_proven_mains(struct bpf_verifier_env *env)
verbose(env, "programs with bpf_throw return different values when cancelled\n");
return -EINVAL;
}
- for (i = 0; i < env->prog->len; i++) {
+ bpf_for_each_pass_insn(env, i) {
if (aux[i].needs_guard && aux[i].no_guard_edges) {
verbose(env, "loop at insn %d needs may_goto for some programs only\n", i);
return -EINVAL;
@@ -25551,6 +25552,26 @@ static int check_proven_mains(struct bpf_verifier_env *env)
return 0;
}
+/*
+ * The passes of a main prog need only the functions that it gets to. The graph has no edges of
+ * callx and of dead code, so add what they can get to. The last main prog looks at all functions:
+ * what is settled after it is for all.
+ */
+static void mark_pass_subprogs(struct bpf_verifier_env *env)
+{
+ struct bpf_subprog_info *sub = env->subprog_info;
+ bool last = env->pass_main + 1 == env->main_cnt;
+ u16 *marks = env->calls.marks;
+ int s;
+
+ for (s = 0; s < env->subprog_cnt; s++)
+ marks[s] = last || s == env->mains[env->pass_main].subprog ||
+ sub[s].addr_taken || sub[s].recursive;
+ bpf_call_graph_mark_callees(&env->calls, BIT(0));
+ for (s = 0, env->pass_len = 0; s < env->subprog_cnt; s++, sub++)
+ env->pass_len += marks[s] ? sub[1].start - sub->start : 0;
+}
+
/* Each main prog has passes of its own. They start from what the progs before it wrote. */
static int do_check_mains(struct bpf_verifier_env *env)
{
@@ -25561,6 +25582,7 @@ static int do_check_mains(struct bpf_verifier_env *env)
env->cost = cost;
env->max_cost = max_cost;
env->max_mem = max_mem;
+ mark_pass_subprogs(env);
bpf_flow_new_main(env);
env->handed_back = false;
err = do_check_all(env);
@@ -27975,6 +27997,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
kvfree(env->idoms);
kvfree(env->subprog_info);
kvfree(env->subprog_topo_order);
+ bpf_call_graph_free(&env->calls);
kvfree(env->mains);
kvfree(env);
return ret;
--
2.55.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH bpf-next 2/3] bpf: Check a global function once for all entries
2026-12-03 1:27 [PATCH bpf-next 0/3] bpf: Speed up loads with entries Alexei Starovoitov
2026-12-03 1:27 ` [PATCH bpf-next 1/3] bpf: Limit the passes of an entry to the functions that it calls Alexei Starovoitov
@ 2026-12-03 1:27 ` Alexei Starovoitov
2026-12-03 1:27 ` [PATCH bpf-next 3/3] selftests/bpf: Test global functions that are checked once Alexei Starovoitov
2026-12-03 5:30 ` [PATCH bpf-next 0/3] bpf: Speed up loads with entries patchwork-bot+netdevbpf
3 siblings, 0 replies; 6+ messages in thread
From: Alexei Starovoitov @ 2026-12-03 1:27 UTC (permalink / raw)
To: bpf; +Cc: daniel, andrii, eddyz87, memxor
From: Alexei Starovoitov <ast@kernel.org>
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.
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
Documentation/bpf/verifier.rst | 20 +++------
kernel/bpf/verifier.c | 78 ++++++++++++++++++++++++++++------
2 files changed, 72 insertions(+), 26 deletions(-)
diff --git a/Documentation/bpf/verifier.rst b/Documentation/bpf/verifier.rst
index aba25fefb4f5..4351fe4b7010 100644
--- a/Documentation/bpf/verifier.rst
+++ b/Documentation/bpf/verifier.rst
@@ -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
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 461a0db06b87..d4598b6cd440 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -12572,13 +12572,16 @@ static int check_static_func_call(struct bpf_verifier_env *env, int subprog,
return 0;
}
-/*
- * Where ctx accesses are converted all main progs are the same to a global subprog.
- * Otherwise the ctx, helpers and kfuncs depend on the attach point, so check it for each.
- */
-static u32 subprog_caller(struct bpf_verifier_env *env)
+enum { SUBPROG_IN_PASS = BIT(0), SUBPROG_CHECK_AGAIN = BIT(1) };
+
+/* A global subprog is checked when this is not the main prog that it's verified for */
+static u32 subprog_caller(struct bpf_verifier_env *env, int subprog, int context)
{
- return 1 + (env->ops->convert_ctx_access ? 0 : env->cur_main);
+ u32 verified = subprog_aux(env, subprog)->verified[context];
+
+ if (verified && !(env->calls.marks[subprog] & SUBPROG_CHECK_AGAIN))
+ return verified;
+ return 1 + env->cur_main;
}
static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
@@ -12640,12 +12643,9 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
if (env->log.level & BPF_LOG_LEVEL)
verbose(env, "Func#%d ('%s') is global and assumed valid.\n",
subprog, sub_name);
- if (in_sleepable_context(env))
- sub_aux->called[BPF_FUNC_CTX_SLEEPABLE] = subprog_caller(env);
- else if (has_rcu_read_lock(env))
- sub_aux->called[BPF_FUNC_CTX_NON_SLEEPABLE] = subprog_caller(env);
- else
- sub_aux->called[BPF_FUNC_CTX_NO_RCU_LOCK] = subprog_caller(env);
+ i = in_sleepable_context(env) ? BPF_FUNC_CTX_SLEEPABLE :
+ has_rcu_read_lock(env) ? BPF_FUNC_CTX_NON_SLEEPABLE : BPF_FUNC_CTX_NO_RCU_LOCK;
+ sub_aux->called[i] = subprog_caller(env, subprog, i);
returns_void = subprog_returns_void(env, subprog);
if (env->subprog_info[subprog].changes_pkt_data)
clear_all_pkt_pointers(env);
@@ -25567,11 +25567,62 @@ static void mark_pass_subprogs(struct bpf_verifier_env *env)
for (s = 0; s < env->subprog_cnt; s++)
marks[s] = last || s == env->mains[env->pass_main].subprog ||
sub[s].addr_taken || sub[s].recursive;
- bpf_call_graph_mark_callees(&env->calls, BIT(0));
+ bpf_call_graph_mark_callees(&env->calls, SUBPROG_IN_PASS);
for (s = 0, env->pass_len = 0; s < env->subprog_cnt; s++, sub++)
env->pass_len += marks[s] ? sub[1].start - sub->start : 0;
}
+/* Is the insn valid for this main prog, if it is valid for another one? */
+static bool valid_for_main(struct bpf_verifier_env *env, int idx)
+{
+ struct bpf_insn *insn = &env->prog->insnsi[idx];
+ const struct bpf_func_proto *fn;
+
+ /* the value is the result of the main prog */
+ if (env->has_throw && (bpf_is_throw_kfunc(insn) || bpf_is_unwind_exit(insn)))
+ return false;
+ if (bpf_pseudo_kfunc_call(insn))
+ return btf_kfunc_is_allowed(find_kfunc_desc_btf_cached(env, insn->off), insn->imm,
+ env->prog);
+ if (bpf_helper_call(insn))
+ return !bpf_get_helper_proto(env, insn->imm, &fn) &&
+ fn == env->ops->get_func_proto(insn->imm, env->prog) &&
+ (!fn->allowed || fn->allowed(env->prog));
+ if (env->prog->sleepable && bpf_is_ldimm64(insn) &&
+ (insn->src_reg == BPF_PSEUDO_MAP_FD || insn->src_reg == BPF_PSEUDO_MAP_IDX))
+ return map_needs_no_rcu(env->used_maps[env->insn_aux_data[idx].map_index]);
+ return true;
+}
+
+/*
+ * A global subprog is checked once. Check it again for this main prog when it has the ctx, or gets
+ * to an insn that depends on the attach point. Where ctx accesses are converted it's one for all.
+ */
+static void mark_check_again(struct bpf_verifier_env *env)
+{
+ struct bpf_subprog_info *sub = env->subprog_info;
+ u16 *marks = env->calls.marks;
+ int s, i;
+
+ if (!env->pass_main || env->ops->convert_ctx_access)
+ return;
+ bpf_add_cost(env, env->pass_len, BPF_COST_SCAN);
+ enter_main(env, env->pass_main);
+ for (s = 0; s < env->subprog_cnt; s++)
+ for (i = sub[s].start; i < sub[s + 1].start && marks[s] == SUBPROG_IN_PASS; i++)
+ if (env->insn_aux_data[i].seen && !valid_for_main(env, i))
+ marks[s] |= SUBPROG_CHECK_AGAIN;
+ enter_main(env, 0);
+ bpf_call_graph_mark_callers(&env->calls, SUBPROG_CHECK_AGAIN, 0);
+ for (s = 0; s < env->subprog_cnt; s++) {
+ for (i = 0; i < sub[s].arg_slot_cnt; i++)
+ if (sub[s].args[i].arg_type == ARG_PTR_TO_CTX)
+ marks[s] |= SUBPROG_CHECK_AGAIN;
+ if (!(marks[s] & SUBPROG_IN_PASS))
+ marks[s] = 0;
+ }
+}
+
/* Each main prog has passes of its own. They start from what the progs before it wrote. */
static int do_check_mains(struct bpf_verifier_env *env)
{
@@ -25583,6 +25634,7 @@ static int do_check_mains(struct bpf_verifier_env *env)
env->max_cost = max_cost;
env->max_mem = max_mem;
mark_pass_subprogs(env);
+ mark_check_again(env);
bpf_flow_new_main(env);
env->handed_back = false;
err = do_check_all(env);
--
2.55.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH bpf-next 3/3] selftests/bpf: Test global functions that are checked once
2026-12-03 1:27 [PATCH bpf-next 0/3] bpf: Speed up loads with entries Alexei Starovoitov
2026-12-03 1:27 ` [PATCH bpf-next 1/3] bpf: Limit the passes of an entry to the functions that it calls Alexei Starovoitov
2026-12-03 1:27 ` [PATCH bpf-next 2/3] bpf: Check a global function once for all entries Alexei Starovoitov
@ 2026-12-03 1:27 ` Alexei Starovoitov
2026-12-03 2:06 ` bot+bpf-ci
2026-12-03 5:30 ` [PATCH bpf-next 0/3] bpf: Speed up loads with entries patchwork-bot+netdevbpf
3 siblings, 1 reply; 6+ messages in thread
From: Alexei Starovoitov @ 2026-12-03 1:27 UTC (permalink / raw)
To: bpf; +Cc: daniel, andrii, eddyz87, memxor
From: Alexei Starovoitov <ast@kernel.org>
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.
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
.../selftests/bpf/prog_tests/share_subprogs.c | 32 ++++++++-
.../selftests/bpf/progs/share_subprogs.c | 68 +++++++++++++++++++
2 files changed, 99 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/share_subprogs.c b/tools/testing/selftests/bpf/prog_tests/share_subprogs.c
index d3382d40a118..607af342aede 100644
--- a/tools/testing/selftests/bpf/prog_tests/share_subprogs.c
+++ b/tools/testing/selftests/bpf/prog_tests/share_subprogs.c
@@ -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"))
diff --git a/tools/testing/selftests/bpf/progs/share_subprogs.c b/tools/testing/selftests/bpf/progs/share_subprogs.c
index 7382b9499370..6c903de39e66 100644
--- a/tools/testing/selftests/bpf/progs/share_subprogs.c
+++ b/tools/testing/selftests/bpf/progs/share_subprogs.c
@@ -206,6 +206,46 @@ __noinline int watchdog(struct Qdisc *sch __arg_trusted)
return 0;
}
+__noinline int nested_fn(struct Qdisc *sch __arg_trusted)
+{
+ return watchdog(sch);
+}
+
+static int arm(__u32 i, struct Qdisc **sch)
+{
+ bpf_qdisc_watchdog_schedule(*sch, 0, 0);
+ return 0;
+}
+
+__noinline int loops(struct Qdisc *sch __arg_trusted)
+{
+ return bpf_loop(1, arm, &sch, 0);
+}
+
+__noinline int leaves(int x)
+{
+ /* unwind_exit */
+ if (x)
+ asm volatile ("r0 = 5; .byte 0x95, %0, 0, 0, 0, 0, 0, 0;" ::
+ "i"(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ ? 0x10 : 0x01) : "r0");
+ return 0;
+}
+
+static __noinline int throws_or_leaves(int x)
+{
+ if (x == 7)
+ bpf_throw(0);
+ return leaves(x);
+}
+
+__noinline int arg1(__u64 *ctx __arg_ctx)
+{
+ return ctx[1];
+}
+
+SEC("raw_tp") int tp_1(__u64 *ctx) { return arg1(ctx); }
+SEC("raw_tp") int tp_2(__u64 *ctx) { return arg1(ctx); }
+
__noinline int pid(__u64 *ctx __arg_ctx)
{
struct task_struct *task = (void *)ctx[1];
@@ -233,13 +273,41 @@ static __noinline int lookup(int *key)
return 0;
}
+__noinline int find(void)
+{
+ return traced = !bpf_map_lookup_elem(&devmap, &zero);
+}
+
PAIR(sleep, ".s", bpf_testmod_ops3, test_2, test_1, sleeps, &traced);
PAIR(rcu, ".s", bpf_testmod_ops3, test_2, test_1, lookup, &zero);
PAIR(helper, "", tcp_congestion_ops, init, release, set_opt, (void *)ctx[0]);
PAIR(kfunc, "", Qdisc_ops, dequeue, reset, watchdog, (void *)ctx[0]);
+PAIR(nested, "", Qdisc_ops, dequeue, reset, nested_fn, (void *)ctx[0]);
+PAIR(callback, "", Qdisc_ops, dequeue, reset, loops, (void *)ctx[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);
PAIR(cookie, "", Qdisc_ops, reset, dequeue, throws, zero);
+/* The sleepable one comes second */
+SEC("?struct_ops") long rcu_global_1(__u64 *ctx)
+{
+ return find();
+}
+
+SEC("?struct_ops.s/second") long rcu_global_2(__u64 *ctx)
+{
+ bpf_rcu_read_lock();
+ find();
+ bpf_rcu_read_unlock();
+ return 0;
+}
+
+SEC("?.struct_ops") struct Qdisc_ops rcu_global = {
+ .reset = (void *)rcu_global_1,
+ .destroy = (void *)rcu_global_2,
+};
+
/* They return 2 and 1 when they are cancelled. What throws() leaves with is passed on. */
PAIR(value, "", tcp_congestion_ops, ssthresh, undo_cwnd, down1, big);
PAIR(thrown, "", tcp_congestion_ops, ssthresh, undo_cwnd, throws, zero);
--
2.55.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH bpf-next 3/3] selftests/bpf: Test global functions that are checked once
2026-12-03 1:27 ` [PATCH bpf-next 3/3] selftests/bpf: Test global functions that are checked once Alexei Starovoitov
@ 2026-12-03 2:06 ` bot+bpf-ci
0 siblings, 0 replies; 6+ messages in thread
From: bot+bpf-ci @ 2026-12-03 2:06 UTC (permalink / raw)
To: alexei.starovoitov, bpf
Cc: daniel, andrii, eddyz87, memxor, ast, andrii, daniel, martin.lau,
eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 1755 bytes --]
> 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.
> diff --git a/tools/testing/selftests/bpf/progs/share_subprogs.c b/tools/testing/selftests/bpf/progs/share_subprogs.c
> --- a/tools/testing/selftests/bpf/progs/share_subprogs.c
> +++ b/tools/testing/selftests/bpf/progs/share_subprogs.c
[ ... ]
> +__noinline int leaves(int x)
> +{
> + /* unwind_exit */
> + if (x)
> + asm volatile ("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.
[ ... ]
> +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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH bpf-next 0/3] bpf: Speed up loads with entries
2026-12-03 1:27 [PATCH bpf-next 0/3] bpf: Speed up loads with entries Alexei Starovoitov
` (2 preceding siblings ...)
2026-12-03 1:27 ` [PATCH bpf-next 3/3] selftests/bpf: Test global functions that are checked once Alexei Starovoitov
@ 2026-12-03 5:30 ` patchwork-bot+netdevbpf
3 siblings, 0 replies; 6+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-12-03 5:30 UTC (permalink / raw)
To: Alexei Starovoitov; +Cc: bpf, daniel, andrii, eddyz87, memxor
Hello:
This series was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:
On Thu, 3 Dec 2026 01:27:17 +0000 you wrote:
> From: Alexei Starovoitov <ast@kernel.org>
>
> 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!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-03 5:30 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 1:27 [PATCH bpf-next 0/3] bpf: Speed up loads with entries Alexei Starovoitov
2026-12-03 1:27 ` [PATCH bpf-next 1/3] bpf: Limit the passes of an entry to the functions that it calls Alexei Starovoitov
2026-12-03 1:27 ` [PATCH bpf-next 2/3] bpf: Check a global function once for all entries Alexei Starovoitov
2026-12-03 1:27 ` [PATCH bpf-next 3/3] selftests/bpf: Test global functions that are checked once Alexei Starovoitov
2026-12-03 2:06 ` bot+bpf-ci
2026-12-03 5:30 ` [PATCH bpf-next 0/3] bpf: Speed up loads with entries patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox