summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorEduard Zingerman <eddyz87@gmail.com>2026-08-12 17:45:07 -0700
committerEduard Zingerman <eddyz87@gmail.com>2026-08-12 17:45:07 -0700
commitdf2175350ec272f0250d30bc7ee762b4d23030cf (patch)
tree532f41ea11e0219d9e57e22bcaf188f0e67ec654 /kernel
parent3a59f11e0f989bdd637c87151992605a6559a7cb (diff)
parent5cb481e1391a886b13f9a6cdf2853a521f660d3f (diff)
Merge branch 'improve-stack-depth-verification-stats-output'
Kumar Kartikeya Dwivedi says: ==================== Improve stack depth verification stats output Some improvements for more clarity in the stack depth verification statistics output. See commit logs for details. For example, ./test_progs -t subprogs/subprogs_alone loads prog4, which has a main program, two static subprograms, and two independently verified global subprograms. A sample run produces: verification time 1765 usec stack depth max 48 subprog 0 (prog4) main insns_self 29 insns_total 51 stack 8 subprog 1 (get_task_tgid) global insns_self 9 insns_total 9 stack 8 subprog 2 (sub4) static insns_self 15 insns_total 22 stack 8 subprog 3 (sub3) static insns_self 7 insns_total 7 stack 0 subprog 4 (sub1) global insns_self 10 insns_total 10 stack 8 processed 70 insns (limit 1000000) max_states_per_insn 0 total_states 7 peak_states 7 mark_read 0 The insns_self counts account for every processed instruction exactly once: 29 + 9 + 15 + 7 + 10 = 70 The main program and global subprograms are independent exploration roots, so their insns_total counts also account for the full processed budget: 51 + 9 + 10 = 70 Static subprogram totals provide a nested, top-down breakdown inside their root. In this example: sub4: 22 = 15 self + 7 in sub3 prog4: 51 = 29 self + 22 in sub4 The global subprogram bodies are accounted in their own root totals rather than being included in prog4 or the static callees which call them. Asynchronous callbacks start from fresh frame-zero verifier states, but the work remains part of the do_check_common() invocation for the main or global verification root under which it was scheduled. Running: ./test_progs -t verifier_subprog_insn_stats/stats_async_nested -v produces the following stats: stack depth max 0 subprog 0 (stats_async_nested) main insns_self 9 insns_total 25 stack 0 subprog 1 (stats_async_nested_schedule) static insns_self 7 insns_total 7 stack 0 subprog 2 (stats_async_outer) static insns_self 7 insns_total 7 stack 0 subprog 3 (stats_async_nested_leaf) static insns_self 2 insns_total 2 stack 0 processed 25 insns Here, 9 + 2 + 7 + 7 = 25. The main root total is the complete verifier budget for its do_check_common() invocation, including both directly and transitively scheduled asynchronous callbacks. Static subprogram and callback totals remain local to their synchronous paths. Changelog: ---------- v7 -> v8 v7: https://lore.kernel.org/bpf/20260808062601.1070988-1-memxor@gmail.com * Move the insns_total snapshot and delta for main and global roots into do_check_common() and explain why the override is needed for async subprograms. (Eduard) * Avoid splitting __msg string literals in the stack-depth stats tests. (Eduard) * Add a comment explaining why both the new per-subprogram records and the legacy one-line format are matched in veristat's parse_verif_log(). (Eduard) v6 -> v7 v6: https://lore.kernel.org/bpf/20260805011517.1717238-1-memxor@gmail.com * Rename insns_own to insns_self throughout. (Andrii) * Drop the async accounting call stack and attribute callback work to the scheduling main or global verification root using its processed-insn delta. (Eduard, Andrii) * Skip missing frames when folding instruction totals after a partial verifier state copy. (BPF CI Bot) * Use explicit callback argument operands in deterministic instruction-count tests and update tests and examples for root attribution. (BPF CI Bot) v5 -> v6 v5: https://lore.kernel.org/bpf/20260804081114.3871564-1-memxor@gmail.com * Track self and inclusive instruction counts for main, global, and static subprograms. (Andrii, Eduard) * Keep instruction subtotals path-local across verifier state copies. * Propagate async callback budget through nested scheduling chains. (Andrii) * Split per-subprogram instruction accounting into a preparatory patch. * Add deterministic selftests with exact self, total, and processed counts. v4 -> v5 v4: https://lore.kernel.org/bpf/20260803072733.191502-1-memxor@gmail.com * Change the format to combine instruction counts and stack depths into per-program records. (Andrii) * Adjust veristat for the new format while retaining support for the legacy format. * Explain why the legacy stack parsing buffer is zero-initialized. (BPF CI Bot) v3 -> v4 v3: https://lore.kernel.org/bpf/20260803031457.3115812-1-memxor@gmail.com * Read names from subprog_info directly to avoid an out-of-bounds access when func_info validation fails. (BPF CI Bot) v2 -> v3 v2: https://lore.kernel.org/bpf/20260802225209.2511758-1-memxor@gmail.com * Reuse subprog_name() to fetch subprogram names. (BPF CI Bot) v1 -> v2 v1: https://lore.kernel.org/bpf/20260801230400.850271-1-memxor@gmail.com * Use multi-line format. (Eduard) * Adjust veristat to work with old and new format. * Adjust selftest log_level without new option. (Eduard) ==================== Link: https://patch.msgid.link/20260812221925.3358041-1-memxor@gmail.com Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Diffstat (limited to 'kernel')
-rw-r--r--kernel/bpf/verifier.c84
1 files changed, 68 insertions, 16 deletions
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 61ef43325c6f..73d6cd563cdf 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -1593,6 +1593,8 @@ static int copy_func_state(struct bpf_func_state *dst,
const struct bpf_func_state *src)
{
memcpy(dst, src, offsetof(struct bpf_func_state, stack));
+ /* Instruction accounting is path-local, not part of verifier state. */
+ dst->insns_subtotal = 0;
return copy_stack_state(dst, src);
}
@@ -9708,6 +9710,42 @@ static int set_task_work_schedule_callback_state(struct bpf_verifier_env *env,
static bool is_rbtree_lock_required_kfunc(u32 btf_id);
+static void account_processed_insn(struct bpf_verifier_env *env)
+{
+ struct bpf_func_state *frame = cur_func(env);
+
+ env->insn_processed++;
+ frame->insns_subtotal++;
+ env->subprog_info[frame->subprogno].insns_self++;
+}
+
+static void account_processed_insns(struct bpf_verifier_env *env,
+ struct bpf_func_state *callee,
+ struct bpf_func_state *caller)
+{
+ u32 insns;
+
+ if (!callee)
+ return;
+
+ insns = callee->insns_subtotal;
+
+ env->subprog_info[callee->subprogno].insns_total += insns;
+ if (caller)
+ caller->insns_subtotal += insns;
+ callee->insns_subtotal = 0;
+}
+
+static void account_current_path(struct bpf_verifier_env *env)
+{
+ struct bpf_verifier_state *state = env->cur_state;
+ int frame;
+
+ for (frame = state->curframe; frame >= 0; frame--)
+ account_processed_insns(env, state->frame[frame],
+ frame ? state->frame[frame - 1] : NULL);
+}
+
/* Are we currently verifying the callback for a rbtree helper that must
* be called with lock held? If so, no need to complain about unreleased
* lock
@@ -9804,6 +9842,7 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
verbose(env, "to caller at %d:\n", *insn_idx);
print_verifier_state(env, state, caller->frameno, true);
}
+ account_processed_insns(env, callee, caller);
/* clear everything in the callee. In case of exceptional exits using
* bpf_throw, this will be done by copy_verifier_state for extra frames. */
free_func_state(callee);
@@ -17359,7 +17398,9 @@ static int do_check(struct bpf_verifier_env *env)
insn = &insns[env->insn_idx];
insn_aux = &env->insn_aux_data[env->insn_idx];
- if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
+ account_processed_insn(env);
+
+ if (env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
verbose(env,
"BPF program is too large. Processed %d insn\n",
env->insn_processed);
@@ -17500,6 +17541,7 @@ static int do_check(struct bpf_verifier_env *env)
"speculation barrier after jump instruction may not have the desired effect"))
return -EFAULT;
process_bpf_exit:
+ account_current_path(env);
mark_verifier_state_scratched(env);
err = bpf_update_branch_counts(env, env->cur_state);
if (err)
@@ -18406,6 +18448,7 @@ static int do_check_common(struct bpf_verifier_env *env, int subprog)
struct bpf_prog_aux *aux = env->prog->aux;
struct bpf_verifier_state *state;
struct bpf_reg_state *regs;
+ u32 insn_processed = env->insn_processed;
int ret, i;
env->prev_linfo = NULL;
@@ -18544,9 +18587,19 @@ static int do_check_common(struct bpf_verifier_env *env, int subprog)
ret = do_check(env);
out:
+ account_current_path(env);
if (!ret && pop_log)
bpf_vlog_reset(&env->log, 0);
free_states(env);
+
+ /*
+ * The override is needed to account for async subprograms, which
+ * are verified with their own set of stack frames and thus are
+ * not accounted as callees by account_current_path().
+ * Accumulate their total counts as total counts of the main or
+ * global subprog hosting the async call.
+ */
+ env->subprog_info[subprog].insns_total = env->insn_processed - insn_processed;
return ret;
}
@@ -18575,7 +18628,6 @@ static int do_check_subprogs(struct bpf_verifier_env *env)
struct bpf_prog_aux *aux = env->prog->aux;
struct bpf_func_info_aux *sub_aux;
int i, ret, new_cnt;
- u32 insn_processed;
if (!aux->func_info)
return 0;
@@ -18590,8 +18642,6 @@ again:
if (!bpf_subprog_is_global(env, i))
continue;
- insn_processed = env->insn_processed;
-
sub_aux = subprog_aux(env, i);
if (!sub_aux->called || sub_aux->verified)
continue;
@@ -18599,7 +18649,6 @@ again:
env->insn_idx = env->subprog_info[i].start;
WARN_ON_ONCE(env->insn_idx == 0);
ret = do_check_common(env, i);
- env->subprog_info[i].insn_processed = env->insn_processed - insn_processed;
if (ret) {
return ret;
} else if (env->log.level & BPF_LOG_LEVEL) {
@@ -18626,12 +18675,10 @@ again:
static int do_check_main(struct bpf_verifier_env *env)
{
- u32 insn_processed = env->insn_processed;
int ret;
env->insn_idx = 0;
ret = do_check_common(env, 0);
- env->subprog_info[0].insn_processed = env->insn_processed - insn_processed;
if (!ret)
env->prog->aux->stack_depth = env->subprog_info[0].stack_depth;
return ret;
@@ -18646,15 +18693,20 @@ static void print_verification_stats(struct bpf_verifier_env *env)
if (env->log.level & BPF_LOG_STATS) {
verbose(env, "verification time %lld usec\n",
div_u64(env->verification_time, 1000));
- verbose(env, "stack depth %d", env->subprog_info[0].stack_depth);
- for (i = 1; i < subprog_cnt; i++)
- verbose(env, "+%d", env->subprog_info[i].stack_depth);
- verbose(env, " max %d\n", env->max_stack_depth);
- verbose(env, "insns processed %d", env->subprog_info[0].insn_processed);
- for (i = 1; i < subprog_cnt; i++)
- if (bpf_subprog_is_global(env, i))
- verbose(env, "+%d", env->subprog_info[i].insn_processed);
- verbose(env, "\n");
+ verbose(env, "stack depth max %d\n", env->max_stack_depth);
+ for (i = 0; i < subprog_cnt; i++) {
+ const char *name = env->subprog_info[i].name;
+ const char *kind;
+
+ if (!name || !name[0])
+ name = "<unknown>";
+ kind = i == 0 ? "main" :
+ bpf_subprog_is_global(env, i) ? "global" : "static";
+ verbose(env, "subprog %d (%s) %s insns_self %d insns_total %d stack %d\n",
+ i, name, kind, env->subprog_info[i].insns_self,
+ env->subprog_info[i].insns_total,
+ env->subprog_info[i].stack_depth);
+ }
}
verbose(env, "processed %d insns (limit %d) max_states_per_insn %d "
"total_states %d peak_states %d mark_read %d\n",