Add reference acquire and release events to diagnostic history so Resource Lifetime Safety reports can show the lifetime of a specific reference id along the path. Record acquisitions after the verifier assigns the reference id. Record releases only after release_reference_nomark() succeeds, including the kptr_xchg RCU conversion path that consumes an owning reference. Signed-off-by: Kumar Kartikeya Dwivedi --- kernel/bpf/diagnostics.c | 57 ++++++++++++++++++++++++++++++++++++++-- kernel/bpf/diagnostics.h | 11 ++++++++ kernel/bpf/verifier.c | 12 +++++++-- 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/diagnostics.c b/kernel/bpf/diagnostics.c index 1bdc7e69fbce..56c1236ffe7c 100644 --- a/kernel/bpf/diagnostics.c +++ b/kernel/bpf/diagnostics.c @@ -842,13 +842,38 @@ void bpf_diag_record_stack_arg(struct bpf_verifier_state *state, u32 insn_idx, bpf_diag_append_history(state, &event); } +static void bpf_diag_record_ref(struct bpf_verifier_state *state, u32 insn_idx, + u8 kind, u32 ref_id) +{ + struct bpf_diag_history_event event = { + .insn_idx = insn_idx, + .kind = kind, + .ref.ref_id = ref_id, + }; + + bpf_diag_append_history(state, &event); +} + +void bpf_diag_record_ref_acquire(struct bpf_verifier_state *state, u32 insn_idx, + u32 ref_id) +{ + bpf_diag_record_ref(state, insn_idx, BPF_DIAG_HISTORY_REF_ACQUIRE, + ref_id); +} + +void bpf_diag_record_ref_release(struct bpf_verifier_state *state, u32 insn_idx, + u32 ref_id) +{ + bpf_diag_record_ref(state, insn_idx, BPF_DIAG_HISTORY_REF_RELEASE, + ref_id); +} + static int bpf_diag_history_start_idx(const struct bpf_verifier_state *state, const struct bpf_diag_history_opts *opts) { int i; - if (!opts || (opts->scope != BPF_DIAG_HISTORY_SCOPE_REG && - opts->scope != BPF_DIAG_HISTORY_SCOPE_STACK_ARG)) + if (!opts || opts->scope == BPF_DIAG_HISTORY_SCOPE_ALL) return 0; for (i = state->diag_history_cnt; i > 0; i--) { @@ -864,6 +889,10 @@ static int bpf_diag_history_start_idx(const struct bpf_verifier_state *state, event->stack_arg.slot == opts->stack_arg_slot && event->stack_arg.frameno == opts->frameno) return i - 1; + if (opts->scope == BPF_DIAG_HISTORY_SCOPE_REF && + event->kind == BPF_DIAG_HISTORY_REF_ACQUIRE && + event->ref.ref_id == opts->ref_id) + return i - 1; } return 0; @@ -886,6 +915,10 @@ static bool bpf_diag_history_event_visible(const struct bpf_diag_history_event * return opts->scope == BPF_DIAG_HISTORY_SCOPE_STACK_ARG && event->stack_arg.slot == opts->stack_arg_slot && event->stack_arg.frameno == opts->frameno; + case BPF_DIAG_HISTORY_REF_ACQUIRE: + case BPF_DIAG_HISTORY_REF_RELEASE: + return opts->scope == BPF_DIAG_HISTORY_SCOPE_REF && + event->ref.ref_id == opts->ref_id; default: return false; } @@ -1084,6 +1117,21 @@ static void bpf_diag_print_reg_mod(struct bpf_verifier_env *env, event->reg.dst_reg, old_buf, new_buf); } +static void bpf_diag_print_ref_event(struct bpf_verifier_env *env, + const struct bpf_diag_history_event *event) +{ + if (event->kind == BPF_DIAG_HISTORY_REF_ACQUIRE) { + bpf_diag_report_source(env, event->insn_idx, '+', + "owned resource (id=%u)", + event->ref.ref_id); + return; + } + + bpf_diag_report_source(env, event->insn_idx, '-', + "owned resource (id=%u)", + event->ref.ref_id); +} + void bpf_diag_print_history(struct bpf_verifier_env *env, const struct bpf_diag_history_opts *opts) { @@ -1122,6 +1170,11 @@ void bpf_diag_print_history(struct bpf_verifier_env *env, bpf_diag_print_reg_mod(env, event); printed = true; break; + case BPF_DIAG_HISTORY_REF_ACQUIRE: + case BPF_DIAG_HISTORY_REF_RELEASE: + bpf_diag_print_ref_event(env, event); + printed = true; + break; default: break; } diff --git a/kernel/bpf/diagnostics.h b/kernel/bpf/diagnostics.h index b8dfe8103966..c6ace61547c6 100644 --- a/kernel/bpf/diagnostics.h +++ b/kernel/bpf/diagnostics.h @@ -69,6 +69,9 @@ struct bpf_diag_history_event { u8 reason; struct bpf_diag_reg_snapshot old, new; } stack_arg; + struct { + u32 ref_id; + } ref; }; }; @@ -78,12 +81,15 @@ enum bpf_diag_history_kind { BPF_DIAG_HISTORY_BRANCH, BPF_DIAG_HISTORY_REG_MOD, BPF_DIAG_HISTORY_STACK_ARG, + BPF_DIAG_HISTORY_REF_ACQUIRE, + BPF_DIAG_HISTORY_REF_RELEASE, }; enum bpf_diag_history_scope { BPF_DIAG_HISTORY_SCOPE_ALL, BPF_DIAG_HISTORY_SCOPE_REG, BPF_DIAG_HISTORY_SCOPE_STACK_ARG, + BPF_DIAG_HISTORY_SCOPE_REF, }; enum bpf_diag_stack_arg_reason { @@ -97,6 +103,7 @@ struct bpf_diag_history_opts { u32 frameno; int regno; int stack_arg_slot; + u32 ref_id; }; struct bpf_verifier_env; @@ -134,6 +141,10 @@ void bpf_diag_record_stack_arg(struct bpf_verifier_state *state, u32 insn_idx, enum bpf_diag_stack_arg_reason reason, const struct bpf_reg_state *old_reg, const struct bpf_reg_state *new_reg); +void bpf_diag_record_ref_acquire(struct bpf_verifier_state *state, u32 insn_idx, + u32 ref_id); +void bpf_diag_record_ref_release(struct bpf_verifier_state *state, u32 insn_idx, + u32 ref_id); 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 188201d48108..9b9ffa57b95a 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -1434,6 +1434,7 @@ static int acquire_reference(struct bpf_verifier_env *env, int insn_idx, int par s->type = REF_TYPE_PTR; s->id = ++env->id_gen; s->parent_id = parent_id; + bpf_diag_record_ref_acquire(env->cur_state, insn_idx, s->id); return s->id; } @@ -8998,8 +8999,12 @@ static int release_reference(struct bpf_verifier_env *env, int id) if (err) return err; - if (find_reference_state(vstate, id)) - WARN_ON_ONCE(release_reference_nomark(vstate, id)); + if (find_reference_state(vstate, id)) { + err = release_reference_nomark(vstate, id); + WARN_ON_ONCE(err); + if (!err) + bpf_diag_record_ref_release(vstate, env->insn_idx, id); + } while ((id = idstack_pop(idstack))) { /* @@ -9088,6 +9093,9 @@ static int ref_convert_alloc_rcu_protected(struct bpf_verifier_env *env, u32 id) int err; err = release_reference_nomark(env->cur_state, id); + if (err) + return err; + bpf_diag_record_ref_release(env->cur_state, env->insn_idx, id); bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({ if (reg->id != id) -- 2.53.0