Record verifier context transitions in the diagnostic history so later reports can anchor causal paths to the critical section that made an operation invalid. This covers lock, IRQ, RCU, and preempt regions without adding any new verifier error reports. Category-specific commits decide where those recorded events should be rendered. Use context depth when selecting scoped history so nested regions anchor at the outer active region, and fall back to the earliest retained event when the matching entry was pruned. Signed-off-by: Kumar Kartikeya Dwivedi --- kernel/bpf/diagnostics.c | 85 +++++++++++++++++++++++++++++++++++++++- kernel/bpf/diagnostics.h | 20 ++++++++++ kernel/bpf/verifier.c | 35 ++++++++++++++--- 3 files changed, 133 insertions(+), 7 deletions(-) diff --git a/kernel/bpf/diagnostics.c b/kernel/bpf/diagnostics.c index 58cc7c18cf98..d6a9a2315f54 100644 --- a/kernel/bpf/diagnostics.c +++ b/kernel/bpf/diagnostics.c @@ -1041,6 +1041,53 @@ void bpf_diag_record_ref_release(struct bpf_verifier_env *env, u32 insn_idx, ref_id); } +void bpf_diag_record_context(struct bpf_verifier_env *env, u32 insn_idx, + enum bpf_diag_context_kind ctx_kind, bool enter, + u32 depth) +{ + /* Keep leave events so context rendering can stop at a depth-zero exit + * and show nested-region depth accurately for the active path. + */ + struct bpf_diag_history_event event = { + .insn_idx = insn_idx, + .kind = BPF_DIAG_HISTORY_CONTEXT, + .ctx.kind = ctx_kind, + .ctx.enter = enter, + .ctx.depth = depth, + }; + + if (ctx_kind == BPF_DIAG_CONTEXT_NONE) + return; + + bpf_diag_append_history(env, &event); +} + +static int bpf_diag_history_context_start_idx(const struct bpf_diag_log *log, + const struct bpf_diag_history_opts *opts) +{ + int i; + + if (!opts->ctx_depth) + return 0; + + for (i = log->cnt; i > 0; i--) { + const struct bpf_diag_history_event *event; + + event = bpf_diag_history_event(log, i - 1); + + if (event->kind != BPF_DIAG_HISTORY_CONTEXT || + event->ctx.kind != opts->ctx_kind) + continue; + + if (event->ctx.enter && event->ctx.depth == 1) + return i - 1; + if (!event->ctx.enter && event->ctx.depth == 0) + return 0; + } + + return 0; +} + struct bpf_diag_history_filter { const struct bpf_diag_history_opts *opts; bool stack_slot_valid; @@ -1147,6 +1194,8 @@ static int bpf_diag_history_start_idx(const struct bpf_diag_log *log, if (!opts || opts->scope == BPF_DIAG_HISTORY_SCOPE_ALL) return 0; + if (opts->scope == BPF_DIAG_HISTORY_SCOPE_CONTEXT) + return bpf_diag_history_context_start_idx(log, opts); if (filter->stack_slot_valid) return bpf_diag_history_stack_start_idx(log, filter); @@ -1179,7 +1228,7 @@ bpf_diag_history_event_visible(const struct bpf_diag_history_event *event, const struct bpf_diag_history_opts *opts = filter->opts; if (!opts || opts->scope == BPF_DIAG_HISTORY_SCOPE_ALL) - return true; + return event->kind != BPF_DIAG_HISTORY_CONTEXT; switch (event->kind) { case BPF_DIAG_HISTORY_BRANCH: @@ -1200,6 +1249,9 @@ bpf_diag_history_event_visible(const struct bpf_diag_history_event *event, case BPF_DIAG_HISTORY_REF_RELEASE: return opts->scope == BPF_DIAG_HISTORY_SCOPE_REF && event->ref.ref_id == opts->ref_id; + case BPF_DIAG_HISTORY_CONTEXT: + return opts->scope == BPF_DIAG_HISTORY_SCOPE_CONTEXT && + event->ctx.kind == opts->ctx_kind; default: return false; } @@ -1584,6 +1636,33 @@ static void bpf_diag_print_ref_event(struct bpf_verifier_env *env, "owned resource (id=%u)", event->ref.ref_id); } +static const char *bpf_diag_context_name(enum bpf_diag_context_kind kind) +{ + switch (kind) { + case BPF_DIAG_CONTEXT_RCU: + return "RCU read lock region"; + case BPF_DIAG_CONTEXT_PREEMPT: + return "non-preemptible region"; + case BPF_DIAG_CONTEXT_IRQ: + return "IRQ-disabled region"; + case BPF_DIAG_CONTEXT_LOCK: + return "lock region"; + case BPF_DIAG_CONTEXT_NONE: + default: + return "context"; + } +} + +static void bpf_diag_print_context_event(struct bpf_verifier_env *env, + const struct bpf_diag_history_event *event) +{ + bpf_diag_report_source(env, event->insn_idx, "context", + "%s %s; depth is now %u", + event->ctx.enter ? "entered" : "left", + bpf_diag_context_name(event->ctx.kind), + event->ctx.depth); +} + void bpf_diag_print_history(struct bpf_verifier_env *env, const struct bpf_diag_history_opts *opts) { @@ -1638,6 +1717,10 @@ void bpf_diag_print_history(struct bpf_verifier_env *env, bpf_diag_print_ref_event(env, event); printed = true; break; + case BPF_DIAG_HISTORY_CONTEXT: + bpf_diag_print_context_event(env, event); + printed = true; + break; default: break; } diff --git a/kernel/bpf/diagnostics.h b/kernel/bpf/diagnostics.h index af8b738f7087..684574388343 100644 --- a/kernel/bpf/diagnostics.h +++ b/kernel/bpf/diagnostics.h @@ -83,6 +83,11 @@ struct bpf_diag_history_event { struct { u32 ref_id; } ref; + struct { + u8 kind; + bool enter; + u32 depth; + } ctx; }; }; @@ -93,6 +98,7 @@ enum bpf_diag_history_kind { BPF_DIAG_HISTORY_STACK_SLOT, BPF_DIAG_HISTORY_REF_ACQUIRE, BPF_DIAG_HISTORY_REF_RELEASE, + BPF_DIAG_HISTORY_CONTEXT, }; enum bpf_diag_history_scope { @@ -100,6 +106,15 @@ enum bpf_diag_history_scope { BPF_DIAG_HISTORY_SCOPE_REG, BPF_DIAG_HISTORY_SCOPE_STACK_ARG, BPF_DIAG_HISTORY_SCOPE_REF, + BPF_DIAG_HISTORY_SCOPE_CONTEXT, +}; + +enum bpf_diag_context_kind { + BPF_DIAG_CONTEXT_NONE, + BPF_DIAG_CONTEXT_RCU, + BPF_DIAG_CONTEXT_PREEMPT, + BPF_DIAG_CONTEXT_IRQ, + BPF_DIAG_CONTEXT_LOCK, }; struct bpf_diag_history_opts { @@ -108,6 +123,8 @@ struct bpf_diag_history_opts { int regno; int stack_arg_slot; u32 ref_id; + enum bpf_diag_context_kind ctx_kind; + u32 ctx_depth; }; bool bpf_diag_enabled(const struct bpf_verifier_env *env); @@ -164,6 +181,9 @@ void bpf_diag_record_ref_acquire(struct bpf_verifier_env *env, u32 insn_idx, u32 ref_id); void bpf_diag_record_ref_release(struct bpf_verifier_env *env, u32 insn_idx, u32 ref_id); +void bpf_diag_record_context(struct bpf_verifier_env *env, u32 insn_idx, + enum bpf_diag_context_kind ctx_kind, bool enter, + u32 depth); void bpf_diag_print_history(struct bpf_verifier_env *env, const struct bpf_diag_history_opts *opts); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 93941deb2cd8..e584dec04b34 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -1064,7 +1064,7 @@ static int is_iter_reg_valid_init(struct bpf_verifier_env *env, struct bpf_reg_s } static int acquire_irq_state(struct bpf_verifier_env *env, int insn_idx); -static int release_irq_state(struct bpf_verifier_state *state, int id); +static int release_irq_state(struct bpf_verifier_env *env, int id); static int mark_stack_slot_irq_flag(struct bpf_verifier_env *env, struct bpf_kfunc_call_arg_meta *meta, @@ -1123,7 +1123,7 @@ static int unmark_stack_slot_irq_flag(struct bpf_verifier_env *env, struct bpf_r return -EINVAL; } - err = release_irq_state(env->cur_state, st->id); + err = release_irq_state(env, st->id); WARN_ON_ONCE(err && err != -EACCES); if (err) { int insn_idx = 0; @@ -1458,6 +1458,8 @@ static int acquire_lock_state(struct bpf_verifier_env *env, int insn_idx, enum r state->active_locks++; state->active_lock_id = id; state->active_lock_ptr = ptr; + bpf_diag_record_context(env, insn_idx, BPF_DIAG_CONTEXT_LOCK, true, + state->active_locks); return 0; } @@ -1473,6 +1475,7 @@ static int acquire_irq_state(struct bpf_verifier_env *env, int insn_idx) s->id = ++env->id_gen; state->active_irq_id = s->id; + bpf_diag_record_context(env, insn_idx, BPF_DIAG_CONTEXT_IRQ, true, 1); return s->id; } @@ -1514,8 +1517,10 @@ static bool reg_is_referenced(struct bpf_verifier_env *env, const struct bpf_reg return find_reference_state(env->cur_state, reg->id); } -static int release_lock_state(struct bpf_verifier_state *state, int type, int id, void *ptr) +static int release_lock_state(struct bpf_verifier_env *env, int type, int id, + void *ptr) { + struct bpf_verifier_state *state = env->cur_state; void *prev_ptr = NULL; u32 prev_id = 0; int i; @@ -1528,6 +1533,9 @@ static int release_lock_state(struct bpf_verifier_state *state, int type, int id /* Reassign active lock (id, ptr). */ state->active_lock_id = prev_id; state->active_lock_ptr = prev_ptr; + bpf_diag_record_context(env, env->insn_idx, + BPF_DIAG_CONTEXT_LOCK, false, + state->active_locks); return 0; } if (state->refs[i].type & REF_TYPE_LOCK_MASK) { @@ -1538,8 +1546,9 @@ static int release_lock_state(struct bpf_verifier_state *state, int type, int id return -EINVAL; } -static int release_irq_state(struct bpf_verifier_state *state, int id) +static int release_irq_state(struct bpf_verifier_env *env, int id) { + struct bpf_verifier_state *state = env->cur_state; u32 prev_id = 0; int i; @@ -1552,6 +1561,9 @@ static int release_irq_state(struct bpf_verifier_state *state, int id) if (state->refs[i].id == id) { release_reference_state(state, i); state->active_irq_id = prev_id; + bpf_diag_record_context(env, env->insn_idx, + BPF_DIAG_CONTEXT_IRQ, false, + state->active_irq_id ? 1 : 0); return 0; } else { prev_id = state->refs[i].id; @@ -7209,7 +7221,7 @@ static int process_spin_lock(struct bpf_verifier_env *env, struct bpf_reg_state verbose(env, "%s_unlock cannot be out of order\n", lock_str); return -EINVAL; } - if (release_lock_state(cur, type, reg->id, ptr)) { + if (release_lock_state(env, type, reg->id, ptr)) { verbose(env, "%s_unlock of different lock\n", lock_str); return -EINVAL; } @@ -13290,21 +13302,32 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, if (rcu_lock) { env->cur_state->active_rcu_locks++; + bpf_diag_record_context(env, insn_idx, BPF_DIAG_CONTEXT_RCU, + true, env->cur_state->active_rcu_locks); } else if (rcu_unlock) { if (env->cur_state->active_rcu_locks == 0) { verbose(env, "unmatched rcu read unlock (kernel function %s)\n", func_name); return -EINVAL; } - if (--env->cur_state->active_rcu_locks == 0) + env->cur_state->active_rcu_locks--; + bpf_diag_record_context(env, insn_idx, BPF_DIAG_CONTEXT_RCU, + false, env->cur_state->active_rcu_locks); + if (env->cur_state->active_rcu_locks == 0) invalidate_rcu_protected_refs(env); } else if (preempt_disable) { env->cur_state->active_preempt_locks++; + bpf_diag_record_context(env, insn_idx, + BPF_DIAG_CONTEXT_PREEMPT, true, + env->cur_state->active_preempt_locks); } else if (preempt_enable) { if (env->cur_state->active_preempt_locks == 0) { verbose(env, "unmatched attempt to enable preemption (kernel function %s)\n", func_name); return -EINVAL; } env->cur_state->active_preempt_locks--; + bpf_diag_record_context(env, insn_idx, + BPF_DIAG_CONTEXT_PREEMPT, false, + env->cur_state->active_preempt_locks); } if (sleepable && !in_sleepable_context(env)) { -- 2.53.0