A call site's landing pad, if any, is kept in insn_aux_data as cleanup_pad. adjust_insn_aux_data(), which every instruction patch goes through, and verifier_remove_insns() keep it in step, and bpf_opt_remove_nops() moves a pad off a nop to the next instruction before removing the nop. Add lookups for it and for calls to bpf_unwind() and bpf_unwind_resume(); their users come in later patches. Signed-off-by: Yonghong Song --- include/linux/bpf_verifier.h | 5 +++++ kernel/bpf/exception.c | 24 ++++++++++++++++++++++++ kernel/bpf/exception.h | 4 ++++ kernel/bpf/fixups.c | 26 +++++++++++++++++++++++++- 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 11cd90d85bad..89a714057820 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -806,6 +806,11 @@ struct bpf_insn_aux_data { u64 jump_target:1; unsigned int orig_idx; /* original instruction index, initialized once */ + /* + * 1 + the instruction index of the exception cleanup landing pad + * this call site unwinds to, or 0 for none. + */ + u32 cleanup_pad; /* * CFG strongly connected component this instruction belongs to, * zero if it is a singleton SCC. diff --git a/kernel/bpf/exception.c b/kernel/bpf/exception.c index ac3b575f935e..9ab29c73c44b 100644 --- a/kernel/bpf/exception.c +++ b/kernel/bpf/exception.c @@ -2,6 +2,8 @@ /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ #include #include +#include +#include #include #include "exception.h" @@ -135,3 +137,25 @@ int bpf_exc_check_info(struct bpf_verifier_env *env, const union bpf_attr *attr, kvfree(krecord); return ret; } + +BTF_ID_LIST_SINGLE(bpf_unwind_id, func, bpf_unwind) +BTF_ID_LIST_SINGLE(bpf_unwind_resume_id, func, bpf_unwind_resume) + +bool bpf_is_unwind_kfunc(const struct bpf_insn *insn) +{ + return bpf_pseudo_kfunc_call(insn) && insn->off == 0 && + insn->imm == bpf_unwind_id[0]; +} + +bool bpf_is_unwind_resume_kfunc(const struct bpf_insn *insn) +{ + return bpf_pseudo_kfunc_call(insn) && insn->off == 0 && + insn->imm == bpf_unwind_resume_id[0]; +} + +int bpf_exc_pad_of_call(struct bpf_verifier_env *env, u32 idx) +{ + u32 pad = env->insn_aux_data[idx].cleanup_pad; + + return pad ? (int)pad - 1 : -1; +} diff --git a/kernel/bpf/exception.h b/kernel/bpf/exception.h index cf099dcc5b74..d5c6ac459870 100644 --- a/kernel/bpf/exception.h +++ b/kernel/bpf/exception.h @@ -8,8 +8,12 @@ union bpf_attr; struct bpf_verifier_env; +struct bpf_insn; int bpf_exc_check_info(struct bpf_verifier_env *env, const union bpf_attr *attr, bpfptr_t uattr); +int bpf_exc_pad_of_call(struct bpf_verifier_env *env, u32 idx); +bool bpf_is_unwind_kfunc(const struct bpf_insn *insn); +bool bpf_is_unwind_resume_kfunc(const struct bpf_insn *insn); #endif /* __BPF_EXCEPTION_H */ diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c index b2fa7e8bedf3..18c563abd988 100644 --- a/kernel/bpf/fixups.c +++ b/kernel/bpf/fixups.c @@ -244,11 +244,18 @@ static void adjust_insn_aux_data(struct bpf_verifier_env *env, data[i].non_stack_access = data[off + cnt - 1].non_stack_access; data[off + cnt - 1].non_stack_access = false; + data[i].cleanup_pad = data[off + cnt - 1].cleanup_pad; + data[off + cnt - 1].cleanup_pad = 0; } else if (bpf_is_mem_insn(insn + i)) { data[i].non_stack_access = true; } } + if (env->cleanup_info_cnt) + for (i = 0; i < prog_len; i++) + if (data[i].cleanup_pad > off + 1) + data[i].cleanup_pad += cnt - 1; + /* * Last slot instruction could be a newly generated * BPF_ST/BPF_LDX/BPF_STX, systematically mark it for non-stack access @@ -611,6 +618,7 @@ static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) struct bpf_insn_aux_data *aux_data = env->insn_aux_data; unsigned int orig_prog_len = env->prog->len; int err; + u32 i; if (bpf_rewrite_must_abort()) return -EINTR; @@ -641,6 +649,17 @@ static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) sizeof(*aux_data) * (orig_prog_len - off - cnt)); env->insn_aux_data_len -= cnt; + if (env->cleanup_info_cnt) { + for (i = 0; i < env->insn_aux_data_len; i++) { + u32 pad = aux_data[i].cleanup_pad; + + if (pad > off + cnt) + aux_data[i].cleanup_pad = pad - cnt; + else if (pad > off) + aux_data[i].cleanup_pad = 0; + } + } + return 0; } @@ -746,7 +765,7 @@ int bpf_opt_remove_nops(struct bpf_verifier_env *env) struct bpf_insn *insn = env->prog->insnsi; int insn_cnt = env->prog->len; bool is_may_goto_0, is_ja; - int i, err; + int i, j, err; for (i = 0; i < insn_cnt; i++) { is_may_goto_0 = !memcmp(&insn[i], &MAY_GOTO_0, sizeof(MAY_GOTO_0)); @@ -757,6 +776,11 @@ int bpf_opt_remove_nops(struct bpf_verifier_env *env) if (aux[i].indirect_target) continue; + if (env->cleanup_info_cnt) + for (j = 0; j < insn_cnt; j++) + if (aux[j].cleanup_pad == i + 1) + aux[j].cleanup_pad = i + 2; + err = verifier_remove_insns(env, i, 1); if (err) return err; -- 2.53.0-Meta