xref: /linux/kernel/bpf/diagnostics.h (revision 956a66e5c33fb53003ca2bc043a90ff0b671b3a5)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
3 
4 #ifndef __BPF_DIAGNOSTICS_H
5 #define __BPF_DIAGNOSTICS_H
6 
7 #include <linux/compiler_attributes.h>
8 #include <linux/stdarg.h>
9 #include <linux/types.h>
10 
11 struct bpf_func_state;
12 struct bpf_reg_state;
13 struct bpf_verifier_env;
14 struct bpf_verifier_state;
15 struct btf;
16 
17 enum bpf_diag_mod_reason {
18 	BPF_DIAG_MOD_WRITE,
19 	BPF_DIAG_MOD_SPILL,
20 	BPF_DIAG_MOD_VAR_WRITE,
21 	BPF_DIAG_MOD_REF_RELEASE,
22 	BPF_DIAG_MOD_PKT_DATA_CHANGE,
23 	BPF_DIAG_MOD_NON_OWN_REF,
24 	BPF_DIAG_MOD_CALLER_SAVED,
25 };
26 
27 enum bpf_diag_context_kind {
28 	BPF_DIAG_CONTEXT_NONE,
29 	BPF_DIAG_CONTEXT_RCU,
30 	BPF_DIAG_CONTEXT_PREEMPT,
31 	BPF_DIAG_CONTEXT_IRQ,
32 	BPF_DIAG_CONTEXT_LOCK,
33 };
34 
35 bool bpf_diag_enabled(const struct bpf_verifier_env *env);
36 int bpf_diag_init(struct bpf_verifier_env *env);
37 void bpf_diag_init_frame(struct bpf_verifier_env *env, struct bpf_func_state *state);
38 char *bpf_diag_fmt_buf(struct bpf_verifier_env *env, size_t size);
39 const char *bpf_diag_vfmt(struct bpf_verifier_env *env, const char *fmt, va_list args)
40 	__printf(2, 0);
41 const char *bpf_diag_fmt(struct bpf_verifier_env *env, const char *fmt, ...) __printf(2, 3);
42 const char *bpf_diag_fmt_btf_type(struct bpf_verifier_env *env, const struct btf *btf, u32 type_id);
43 u64 bpf_diag_event_log_save(struct bpf_verifier_env *env);
44 void bpf_diag_event_log_restore(struct bpf_verifier_env *env, u64 log_pos);
45 u32 bpf_diag_irq_depth(const struct bpf_verifier_state *state);
46 void bpf_diag_free(struct bpf_verifier_env *env);
47 void bpf_diag_record_branch(struct bpf_verifier_env *env, u32 insn_idx, bool cond_true);
48 void bpf_diag_mod_begin(struct bpf_verifier_env *env, const struct bpf_reg_state *reg,
49 			const struct bpf_reg_state *origin, enum bpf_diag_mod_reason reason);
50 void bpf_diag_mod_end(struct bpf_verifier_env *env);
51 void bpf_diag_record_scrub(struct bpf_verifier_env *env, const struct bpf_reg_state *reg,
52 			   enum bpf_diag_mod_reason reason);
53 void bpf_diag_record_scrub_stack(struct bpf_verifier_env *env,
54 				 const struct bpf_func_state *state, s16 min_off, s16 max_off,
55 				 enum bpf_diag_mod_reason reason);
56 void bpf_diag_record_ref_acquire(struct bpf_verifier_env *env, u32 insn_idx, u32 ref_id);
57 void bpf_diag_record_ref_release(struct bpf_verifier_env *env, u32 insn_idx, u32 ref_id);
58 void bpf_diag_record_context(struct bpf_verifier_env *env, u32 insn_idx,
59 			     enum bpf_diag_context_kind ctx_kind, bool enter, u32 depth);
60 
61 #endif /* __BPF_DIAGNOSTICS_H */
62