| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/09/18 21:42 | flow | patch-triage |
1mArgs:null Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT",
"UNWINDER_ORC"
],
"FocusSymbols": [
"arch_bpf_run_cleanup_pad",
"bpf_prepare_cleanup_exceptions",
"bpf_check_cleanup_exceptions",
"bpf_cleanup_attach_main_prog",
"bpf_cleanup_fill_native_ranges",
"bpf_cleanup_pad_for_ip",
"bpf_unwind_resume",
"bpf_cleanup_pad_of_call"
],
"KMSANReasoning": "The patch series adds support for BPF exception cleanup landing pads in the BPF verifier and JIT compilers (x86-64 and arm64).\n\nEvaluation against KMSAN criteria:\n1. Kernel data copied to user space: In `check_cleanup_info()`, only `min_size` (a fully initialized 32-bit integer scalar) is conditionally written back to userspace `cleanup_info_rec_size` on `-E2BIG`. No structs with padding or uninitialized memory are copied to user space.\n2. Structure allocations: All newly allocated kernel data structures (`struct bpf_exception_info`, `bpf_cleanup_range`, `bpf_cleanup_info`, scratch DFS buffers, and bitmaps) are allocated using zero-initializing functions (`kzalloc_obj`, `kvcalloc`, `kvzalloc`, `bitmap_zalloc`) or fully populated before use.\n3. JIT and Assembly execution: The landing pad execution routines in `arch/x86/net/bpf_cleanup_pad.S` and JITed BPF bytecode are raw machine code, which is not instrumented by KMSAN.\n4. Verification \u0026 control flow: Any potential bugs introduced by stack frame offset calculations, JIT prologue/epilogue spills, or invalid branch targets will manifest as out-of-bounds accesses or crashes, which are already detected by KASAN.\n\nTherefore, the patch introduces no uninitialized memory usage or info-leak risks detectable only by KMSAN, and a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false,
"Reasoning": "The patch introduces support for BPF exception cleanup landing pads. This adds new fields (cleanup_info, cleanup_info_cnt, cleanup_info_rec_size) to the BPF_PROG_LOAD command of the bpf() syscall, along with extensive verifier validation (BTF/attribute checking, CFG reachability analysis, liveness analysis, verifier state tracking for unwinding) and x86 JIT runtime handling (arch_bpf_run_cleanup_pad, throw-site register spills, unwinding execution with bpf_throw and bpf_unwind_resume). This modifies core BPF verifier and runtime execution logic reachable from userspace, warranting fuzzing.",
"WorthFuzzing": true
} |
| 1/1 | 2026/09/18 21:42 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit aa17089d32a7e14651815f32eec679e963dce2a9\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Fri Sep 18 21:42:55 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/arch/arm64/net/Makefile b/arch/arm64/net/Makefile\nindex 3ae382bfca879..ebec2a44a52b6 100644\n--- a/arch/arm64/net/Makefile\n+++ b/arch/arm64/net/Makefile\n@@ -2,4 +2,4 @@\n #\n # ARM64 networking code\n #\n-obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o\n+obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o bpf_cleanup_pad.o\ndiff --git a/arch/arm64/net/bpf_cleanup_pad.S b/arch/arm64/net/bpf_cleanup_pad.S\nnew file mode 100644\nindex 0000000000000..ef441241949ec\n--- /dev/null\n+++ b/arch/arm64/net/bpf_cleanup_pad.S\n@@ -0,0 +1,95 @@\n+/* SPDX-License-Identifier: GPL-2.0 */\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \u003clinux/bpf_cleanup_abi.h\u003e\n+#include \u003clinux/linkage.h\u003e\n+\n+/*\n+ * A frame's prologue pushes the tail call counter pair, and then -- once\n+ * bpf_cleanup_force_spill() says so -- x19/x20, x21/x22, x23/x24, x25/x26 and\n+ * x27/x28. Each A64_PUSH pre-decrements, so the lowest address of the spill\n+ * area holds x27 and the highest x20:\n+ *\n+ *\tspill_base + 0\t\tx27 (private stack pointer)\n+ *\tspill_base + 8\t\tx28 (arena base)\n+ *\tspill_base + 16\t\tx25 (BPF r10, the frame pointer)\n+ *\tspill_base + 24\t\tx26 (tail call counter pointer)\n+ *\tspill_base + 32\t\tx23 -- the pad's own, see below\n+ *\tspill_base + 40\t\tx24 -- likewise\n+ *\tspill_base + 48\t\tx21 (BPF r8)\n+ *\tspill_base + 56\t\tx22 (BPF r9)\n+ *\tspill_base + 64\t\tx19 (BPF r6)\n+ *\tspill_base + 72\t\tx20 (BPF r7)\n+ *\n+ * Neither x23 nor x24 is restored from that spill: the pad has its own use for\n+ * both. bpf2a64[] maps nothing to either -- they are pushed only to keep the\n+ * frame shape the exception callback expects -- so nothing else in generated\n+ * code touches them, and being callee-saved they survive every call the pad\n+ * makes.\n+ *\n+ * x23 is the pad's return address. Unlike x86-64 a pad cannot simply return:\n+ * every call it makes clobbers x30, so nothing is left to return through by\n+ * the time it reaches its resume. The JIT emits \"br x23\" for the pad's\n+ * bpf_unwind_resume() and this routine puts .Lcleanup_pad_done there.\n+ *\n+ * x24 is where the pad's frame is anchored. Generated code addresses the BPF\n+ * frame through the stack pointer, which here is this routine's rather than\n+ * the unwinding frame's, so the JIT has the pad recompute the equivalent from\n+ * BPF r10 on entry and address its frame off x24 for as long as it runs.\n+ *\n+ * Both of those branches are indirect, so both targets carry a BTI landing\n+ * marker: the JIT emits one at each pad, and .Lcleanup_pad_done below has one\n+ * of its own.\n+ */\n+\n+\t.text\n+\n+/*\n+ * void arch_bpf_run_cleanup_pad(u64 pad, u64 frame_fp, u64 spill_base)\n+ *\n+ * x0 = native address of the landing pad\n+ * x1 = frame pointer of the frame the pad belongs to (unused here: BPF r10 is\n+ * x25, which the spill area already holds)\n+ * x2 = spill area holding that frame's BPF callee-saved registers\n+ *\n+ * Give the pad the register state of its own frame and call it. It runs on\n+ * this stack, far below the frame it is cleaning up after, so nothing it\n+ * calls can reach into that frame.\n+ */\n+SYM_FUNC_START(arch_bpf_run_cleanup_pad)\n+\t/* Save the kernel's callee-saved registers; the pad owns them next. */\n+\tstp\tx29, x30, [sp, #-96]!\n+\tmov\tx29, sp\n+\tstp\tx19, x20, [sp, #16]\n+\tstp\tx21, x22, [sp, #32]\n+\tstp\tx23, x24, [sp, #48]\n+\tstp\tx25, x26, [sp, #64]\n+\tstp\tx27, x28, [sp, #80]\n+\n+\t/* x9 is BPF_REG_AX, so the pad's address does not stay in BPF r1. */\n+\tmov\tx9, x0\n+\n+\tldp\tx27, x28, [x2, #0]\n+\tldp\tx25, x26, [x2, #16]\n+\tldp\tx21, x22, [x2, #48]\n+\tldp\tx19, x20, [x2, #64]\n+\n+\t/* BPF r0 (x8) on the way into a pad, not whatever the kernel left. */\n+\tmov\tx8, #BPF_PAD_ENTRY_R0\n+\n+\t/* Where the pad's resume branches back to. */\n+\tadr\tx23, .Lcleanup_pad_done\n+\n+\tbr\tx9\n+\n+.Lcleanup_pad_done:\n+\t/* Reached by the pad's \"br x23\", so it is an indirect branch target. */\n+\tbti\tj\n+\tldp\tx19, x20, [sp, #16]\n+\tldp\tx21, x22, [sp, #32]\n+\tldp\tx23, x24, [sp, #48]\n+\tldp\tx25, x26, [sp, #64]\n+\tldp\tx27, x28, [sp, #80]\n+\tldp\tx29, x30, [sp], #96\n+\tret\n+SYM_FUNC_END(arch_bpf_run_cleanup_pad)\ndiff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c\nindex 6c04fee468766..560eba305bca2 100644\n--- a/arch/arm64/net/bpf_jit_comp.c\n+++ b/arch/arm64/net/bpf_jit_comp.c\n@@ -11,6 +11,7 @@\n #include \u003clinux/bitfield.h\u003e\n #include \u003clinux/bpf.h\u003e\n #include \u003clinux/cfi.h\u003e\n+#include \u003clinux/bpf_verifier.h\u003e\n #include \u003clinux/filter.h\u003e\n #include \u003clinux/memory.h\u003e\n #include \u003clinux/printk.h\u003e\n@@ -75,7 +76,21 @@ static const int bpf2a64[] = {\n \t[ARENA_VM_START] = A64_R(28),\n };\n \n+/* Throw-site spill: the five pairs push_callee_regs() forces on, same size and\n+ * slot order, so arch_bpf_run_cleanup_pad() reads both alike.\n+ */\n+#define A64_CLEANUP_SPILL_SZ\t(5 * 16)\n+\n+/*\n+ * Where a landing pad's frame is anchored, since the stack pointer generated\n+ * code normally addresses it through is the walker's inside a pad. bpf2a64[]\n+ * maps nothing to x24, so nothing else in generated code touches it.\n+ */\n+#define A64_CLEANUP_FP\t\tA64_R(24)\n+\n struct jit_ctx {\n+\t/* Bytes reserved for the throw-site spill; see bpf_cleanup_force_spill(). */\n+\tu32 throw_spill;\n \tconst struct bpf_prog *prog;\n \tint idx;\n \tint epilogue_offset;\n@@ -432,7 +447,7 @@ static void push_callee_regs(struct jit_ctx *ctx)\n \t * Callee-saved registers as the exception callback needs to recover\n \t * all ARM64 Callee-saved registers in its epilogue.\n \t */\n-\tif (ctx-\u003eprog-\u003eaux-\u003eexception_boundary) {\n+\tif (ctx-\u003eprog-\u003eaux-\u003eexception_boundary || bpf_cleanup_force_spill(ctx-\u003eprog)) {\n \t\temit(A64_PUSH(A64_R(19), A64_R(20), A64_SP), ctx);\n \t\temit(A64_PUSH(A64_R(21), A64_R(22), A64_SP), ctx);\n \t\temit(A64_PUSH(A64_R(23), A64_R(24), A64_SP), ctx);\n@@ -466,7 +481,8 @@ static void pop_callee_regs(struct jit_ctx *ctx)\n \t * program's stack frame, so recover these extra registers in the above\n \t * two cases.\n \t */\n-\tif (aux-\u003eexception_boundary || aux-\u003eexception_cb) {\n+\tif (aux-\u003eexception_boundary || aux-\u003eexception_cb ||\n+\t bpf_cleanup_force_spill(ctx-\u003eprog)) {\n \t\temit(A64_POP(A64_R(27), A64_R(28), A64_SP), ctx);\n \t\temit(A64_POP(A64_R(25), A64_R(26), A64_SP), ctx);\n \t\temit(A64_POP(A64_R(23), A64_R(24), A64_SP), ctx);\n@@ -602,6 +618,20 @@ static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)\n \t\temit(A64_SUB_I(1, A64_SP, A64_FP, 96), ctx);\n \t}\n \n+\t/*\n+\t * Lowest address of each spill area, as an offset from A64_FP; see\n+\t * bpf_cleanup_pad.S for the layout. The 16 is the tail call counter\n+\t * pair pushed just below the frame record, and the throw-site area\n+\t * sits below the callee-saved one rather than in the program stack.\n+\t */\n+\tif (bpf_cleanup_force_spill(prog)) {\n+\t\tprog-\u003eaux-\u003eexc-\u003espill_off = -(16 + A64_CLEANUP_SPILL_SZ);\n+\t\tctx-\u003ethrow_spill = A64_CLEANUP_SPILL_SZ;\n+\t\temit(A64_SUB_I(1, A64_SP, A64_SP, ctx-\u003ethrow_spill), ctx);\n+\t\tprog-\u003eaux-\u003eexc-\u003ethrow_spill_off =\n+\t\t\t-(16 + A64_CLEANUP_SPILL_SZ) - ctx-\u003ethrow_spill;\n+\t}\n+\n \t/* Stack must be multiples of 16B */\n \tctx-\u003estack_size = round_up(prog-\u003eaux-\u003estack_depth, 16);\n \n@@ -691,6 +721,10 @@ static int emit_bpf_tail_call(struct jit_ctx *ctx)\n \tif (ctx-\u003estack_size \u0026\u0026 !ctx-\u003epriv_sp_used)\n \t\temit(A64_ADD_I(1, A64_SP, A64_SP, ctx-\u003estack_size), ctx);\n \n+\t/* Release it for the same reason build_epilogue() does. */\n+\tif (ctx-\u003ethrow_spill)\n+\t\temit(A64_ADD_I(1, A64_SP, A64_SP, ctx-\u003ethrow_spill), ctx);\n+\n \tpop_callee_regs(ctx);\n \n \t/* goto *(prog-\u003ebpf_func + prologue_offset); */\n@@ -1055,6 +1089,9 @@ static void build_epilogue(struct jit_ctx *ctx, bool was_classic)\n \tif (ctx-\u003estack_size \u0026\u0026 !ctx-\u003epriv_sp_used)\n \t\temit(A64_ADD_I(1, A64_SP, A64_SP, ctx-\u003estack_size), ctx);\n \n+\tif (ctx-\u003ethrow_spill)\n+\t\temit(A64_ADD_I(1, A64_SP, A64_SP, ctx-\u003ethrow_spill), ctx);\n+\n \tpop_callee_regs(ctx);\n \n \temit(A64_POP(A64_ZR, ptr, A64_SP), ctx);\n@@ -1230,6 +1267,13 @@ static const u8 stack_arg_reg[] = { A64_R(5), A64_R(6), A64_R(7) };\n \n #define NR_STACK_ARG_REGS\tARRAY_SIZE(stack_arg_reg)\n \n+/*\n+ * This reads the incoming argument area off A64_FP, which in an exception\n+ * cleanup landing pad would be arch_bpf_run_cleanup_pad()'s frame record\n+ * rather than the unwinding frame's -- but a pad cannot contain one of these:\n+ * check_stack_arg_read() requires every r11 load to come before the frame's\n+ * first call, and a pad only ever runs after one.\n+ */\n static void emit_stack_arg_load(u8 dst, s16 bpf_off, struct jit_ctx *ctx)\n {\n \tint idx = bpf_off / sizeof(u64) - 1;\n@@ -1367,6 +1411,7 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn\n \tconst s16 off = insn-\u003eoff;\n \tconst s32 imm = insn-\u003eimm;\n \tconst int i = insn - ctx-\u003eprog-\u003einsnsi;\n+\tconst bool in_pad = bpf_cleanup_insn_in_pad(ctx-\u003eprog, i);\n \tconst bool is64 = BPF_CLASS(code) == BPF_ALU64 ||\n \t\t\t BPF_CLASS(code) == BPF_JMP;\n \tu8 jmp_cond;\n@@ -1378,9 +1423,14 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn\n \tint ret;\n \tbool sign_extend;\n \n-\tif (bpf_insn_is_indirect_target(env, ctx-\u003eprog, i))\n+\tif (bpf_insn_is_indirect_target(env, ctx-\u003eprog, i) ||\n+\t bpf_cleanup_insn_is_pad(ctx-\u003eprog, i))\n \t\temit_bti(A64_BTI_J, ctx);\n \n+\tif (bpf_cleanup_insn_is_pad(ctx-\u003eprog, i))\n+\t\temit(A64_SUB_I(1, A64_CLEANUP_FP, fp,\n+\t\t\t ctx-\u003estack_size + ctx-\u003estack_arg_size), ctx);\n+\n \tswitch (code) {\n \t/* dst = src */\n \tcase BPF_ALU | BPF_MOV | BPF_X:\n@@ -1743,6 +1793,26 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn\n \t\tu64 func_addr;\n \t\tu32 cpu_offset;\n \n+\t\tif (bpf_cleanup_insn_is_throw(ctx-\u003eprog, insn - ctx-\u003eprog-\u003einsnsi)) {\n+\t\t\t/* Spill where the bpf_throw() walker looks. */\n+\t\t\tconst s32 off = ctx-\u003eprog-\u003eaux-\u003eexc-\u003ethrow_spill_off;\n+\n+\t\t\temit(A64_SUB_I(1, tmp, A64_FP, -off), ctx);\n+\t\t\temit(A64_STR64I(bpf2a64[PRIVATE_SP], tmp, 0), ctx);\n+\t\t\temit(A64_STR64I(bpf2a64[ARENA_VM_START], tmp, 8), ctx);\n+\t\t\temit(A64_STR64I(bpf2a64[BPF_REG_FP], tmp, 16), ctx);\n+\t\t\temit(A64_STR64I(bpf2a64[TCCNT_PTR], tmp, 24), ctx);\n+\t\t\temit(A64_STR64I(bpf2a64[BPF_REG_8], tmp, 48), ctx);\n+\t\t\temit(A64_STR64I(bpf2a64[BPF_REG_9], tmp, 56), ctx);\n+\t\t\temit(A64_STR64I(bpf2a64[BPF_REG_6], tmp, 64), ctx);\n+\t\t\temit(A64_STR64I(bpf2a64[BPF_REG_7], tmp, 72), ctx);\n+\t\t}\n+\n+\t\tif (bpf_is_unwind_resume_kfunc(insn)) {\n+\t\t\temit(A64_BR(A64_R(23)), ctx);\n+\t\t\tbreak;\n+\t\t}\n+\n \t\t/* Implement helper call to bpf_get_smp_processor_id() inline */\n \t\tif (insn-\u003esrc_reg == 0 \u0026\u0026 insn-\u003eimm == BPF_FUNC_get_smp_processor_id) {\n \t\t\tcpu_offset = offsetof(struct thread_info, cpu);\n@@ -1854,7 +1924,8 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn\n \t\t\tsrc = tmp2;\n \t\t}\n \t\tif (src == fp) {\n-\t\t\tsrc_adj = ctx-\u003epriv_sp_used ? priv_sp : A64_SP;\n+\t\t\tsrc_adj = ctx-\u003epriv_sp_used ? priv_sp :\n+\t\t\t\t in_pad ? A64_CLEANUP_FP : A64_SP;\n \t\t\toff_adj = off + ctx-\u003estack_size;\n \t\t\tif (!ctx-\u003epriv_sp_used)\n \t\t\t\toff_adj += ctx-\u003estack_arg_size;\n@@ -1952,7 +2023,8 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn\n \t\t\tdst = tmp3;\n \t\t}\n \t\tif (dst == fp) {\n-\t\t\tdst_adj = ctx-\u003epriv_sp_used ? priv_sp : A64_SP;\n+\t\t\tdst_adj = ctx-\u003epriv_sp_used ? priv_sp :\n+\t\t\t\t in_pad ? A64_CLEANUP_FP : A64_SP;\n \t\t\toff_adj = off + ctx-\u003estack_size;\n \t\t\tif (!ctx-\u003epriv_sp_used)\n \t\t\t\toff_adj += ctx-\u003estack_arg_size;\n@@ -2021,7 +2093,8 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn\n \t\t\tdst = tmp2;\n \t\t}\n \t\tif (dst == fp) {\n-\t\t\tdst_adj = ctx-\u003epriv_sp_used ? priv_sp : A64_SP;\n+\t\t\tdst_adj = ctx-\u003epriv_sp_used ? priv_sp :\n+\t\t\t\t in_pad ? A64_CLEANUP_FP : A64_SP;\n \t\t\toff_adj = off + ctx-\u003estack_size;\n \t\t\tif (!ctx-\u003epriv_sp_used)\n \t\t\t\toff_adj += ctx-\u003estack_arg_size;\n@@ -2410,6 +2483,13 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr\n \t\t * reasons, expects to point to the next instruction)\n \t\t */\n \t\tbpf_prog_update_insn_ptrs(prog, ctx.offset, ctx.ro_image);\n+\n+\t\t/*\n+\t\t * Same byte offsets, consumed by the bpf_throw() frame walker:\n+\t\t * turn the cleanup records into native address ranges now that\n+\t\t * the image is final.\n+\t\t */\n+\t\tbpf_cleanup_fill_native_ranges(prog, ctx.offset, ctx.ro_image);\n out_off:\n \t\tif (!ro_header \u0026\u0026 priv_stack_ptr) {\n \t\t\tfree_percpu(priv_stack_ptr);\n@@ -3385,6 +3465,11 @@ bool bpf_jit_supports_exceptions(void)\n \treturn true;\n }\n \n+bool bpf_jit_supports_cleanup_pads(void)\n+{\n+\treturn true;\n+}\n+\n bool bpf_jit_supports_arena(void)\n {\n \treturn true;\ndiff --git a/arch/x86/net/Makefile b/arch/x86/net/Makefile\nindex dddbefc0f4398..9d574d972df37 100644\n--- a/arch/x86/net/Makefile\n+++ b/arch/x86/net/Makefile\n@@ -6,5 +6,5 @@\n ifeq ($(CONFIG_X86_32),y)\n obj-$(CONFIG_BPF_JIT) += bpf_jit_comp32.o\n else\n- obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o\n+ obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o bpf_cleanup_pad.o\n endif\ndiff --git a/arch/x86/net/bpf_cleanup_pad.S b/arch/x86/net/bpf_cleanup_pad.S\nnew file mode 100644\nindex 0000000000000..da4b448ecf095\n--- /dev/null\n+++ b/arch/x86/net/bpf_cleanup_pad.S\n@@ -0,0 +1,74 @@\n+/* SPDX-License-Identifier: GPL-2.0 */\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \u003clinux/bpf_cleanup_abi.h\u003e\n+#include \u003clinux/linkage.h\u003e\n+#include \u003casm/nospec-branch.h\u003e\n+\n+/*\n+ * The x86-64 BPF JIT prologue spills, once bpf_cleanup_force_spill() makes it\n+ * unconditional, r12, rbx, r13, r14 and r15 in that order -- so within the\n+ * spill area the lowest address holds r15 and the highest r12. The throw-site\n+ * spill the JIT emits uses the same layout, so the routine below reads both\n+ * the same way:\n+ *\n+ *\tspill_base + 0\t\tBPF r9 (r15)\n+ *\tspill_base + 8\t\tBPF r8 (r14)\n+ *\tspill_base + 16\t\tBPF r7 (r13)\n+ *\tspill_base + 24\t\tBPF r6 (rbx)\n+ *\tspill_base + 32\t\tr12 (arena base, not a BPF register)\n+ */\n+\n+\t.code64\n+\t.section .text, \"ax\"\n+\n+/*\n+ * void arch_bpf_run_cleanup_pad(u64 pad, u64 frame_fp, u64 spill_base)\n+ *\n+ * rdi = native address of the landing pad\n+ * rsi = frame pointer of the frame the pad belongs to\n+ * rdx = spill area holding that frame's BPF callee-saved registers\n+ *\n+ * Give the pad the register state of its own frame and call it. The pad ends\n+ * in the bare return the JIT emits for its bpf_unwind_resume(), so it comes\n+ * back here rather than returning to its frame's caller. It runs on this\n+ * stack, far below the frame it is cleaning up after, so nothing it calls can\n+ * reach into that frame.\n+ */\n+SYM_FUNC_START(arch_bpf_run_cleanup_pad)\n+\tANNOTATE_NOENDBR\n+\n+\tpushq %rbp\n+\tpushq %rbx\n+\tpushq %r12\n+\tpushq %r13\n+\tpushq %r14\n+\tpushq %r15\n+\t/* Keep the pad's entry rsp congruent to a normal call's. */\n+\tsubq $8, %rsp\n+\n+\tmovq 0(%rdx), %r15\n+\tmovq 8(%rdx), %r14\n+\tmovq 16(%rdx), %r13\n+\tmovq 24(%rdx), %rbx\n+\tmovq 32(%rdx), %r12\n+\t/* rbp is BPF r10, so this is the whole of the pad's frame setup. */\n+\tmovq %rsi, %rbp\n+\n+\t/* CALL_NOSPEC needs the target in a register; rcx is BPF r4, dead. */\n+\tmovq %rdi, %rcx\n+\n+\t/* BPF r0 on the way into a pad, not whatever the kernel left in rax. */\n+\tmovl $BPF_PAD_ENTRY_R0, %eax\n+\n+\tCALL_NOSPEC rcx\n+\n+\taddq $8, %rsp\n+\tpopq %r15\n+\tpopq %r14\n+\tpopq %r13\n+\tpopq %r12\n+\tpopq %rbx\n+\tpopq %rbp\n+\tRET\n+SYM_FUNC_END(arch_bpf_run_cleanup_pad)\ndiff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c\nindex d4a980140b48d..9d0dd54773e83 100644\n--- a/arch/x86/net/bpf_jit_comp.c\n+++ b/arch/x86/net/bpf_jit_comp.c\n@@ -357,6 +357,11 @@ struct jit_context {\n /* Number of bytes that will be skipped on tailcall */\n #define X86_TAIL_CALL_OFFSET\t(12 + ENDBR_INSN_SIZE)\n \n+/* Throw-site spill: r15, r14, r13, rbx, r12 low to high, the layout the\n+ * prologue's pushes leave, so arch_bpf_run_cleanup_pad() reads both alike.\n+ */\n+#define X86_CLEANUP_SPILL_SZ\t(5 * 8)\n+\n static void push_r9(u8 **pprog)\n {\n \tu8 *prog = *pprog;\n@@ -832,7 +837,7 @@ static void emit_bpf_tail_call_indirect(struct bpf_prog *bpf_prog,\n \t/* Inc tail_call_cnt if the slot is populated. */\n \tEMIT4(0x48, 0x83, 0x00, 0x01); /* add qword ptr [rax], 1 */\n \n-\tif (bpf_prog-\u003eaux-\u003eexception_boundary) {\n+\tif (bpf_prog-\u003eaux-\u003eexception_boundary || bpf_cleanup_force_spill(bpf_prog)) {\n \t\tpop_callee_regs(\u0026prog, all_callee_regs_used);\n \t\tpop_r12(\u0026prog);\n \t} else {\n@@ -899,7 +904,7 @@ static void emit_bpf_tail_call_direct(struct bpf_prog *bpf_prog,\n \t/* Inc tail_call_cnt if the slot is populated. */\n \tEMIT4(0x48, 0x83, 0x00, 0x01); /* add qword ptr [rax], 1 */\n \n-\tif (bpf_prog-\u003eaux-\u003eexception_boundary) {\n+\tif (bpf_prog-\u003eaux-\u003eexception_boundary || bpf_cleanup_force_spill(bpf_prog)) {\n \t\tpop_callee_regs(\u0026prog, all_callee_regs_used);\n \t\tpop_r12(\u0026prog);\n \t} else {\n@@ -1977,6 +1982,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *\n \tu8 *ip, *prog = temp;\n \tu32 stack_depth;\n \tint callee_saved_size;\n+\tu32 throw_spill, prologue_depth;\n \ts32 outgoing_arg_base;\n \tint err;\n \n@@ -2015,7 +2021,10 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *\n \n \tdetect_reg_usage(insn, insn_cnt, callee_regs_used);\n \n-\temit_prologue(\u0026prog, image, stack_depth,\n+\tthrow_spill = bpf_cleanup_force_spill(bpf_prog) ? X86_CLEANUP_SPILL_SZ : 0;\n+\tprologue_depth = stack_depth + throw_spill;\n+\n+\temit_prologue(\u0026prog, image, prologue_depth,\n \t\t bpf_prog_was_classic(bpf_prog), tail_call_reachable,\n \t\t bpf_is_subprog(bpf_prog), bpf_prog-\u003eaux-\u003eexception_cb);\n \n@@ -2024,7 +2033,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *\n \t/* Exception callback will clobber callee regs for its own use, and\n \t * restore the original callee regs from main prog's stack frame.\n \t */\n-\tif (bpf_prog-\u003eaux-\u003eexception_boundary) {\n+\tif (bpf_prog-\u003eaux-\u003eexception_boundary || bpf_cleanup_force_spill(bpf_prog)) {\n \t\t/* We also need to save r12, which is not mapped to any BPF\n \t\t * register, as we throw after entry into the kernel, which may\n \t\t * overwrite r12.\n@@ -2039,9 +2048,10 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *\n \n \t/* Compute callee-saved register area size. */\n \tcallee_saved_size = 0;\n-\tif (bpf_prog-\u003eaux-\u003eexception_boundary || arena_vm_start)\n+\tif (bpf_prog-\u003eaux-\u003eexception_boundary || bpf_cleanup_force_spill(bpf_prog) ||\n+\t arena_vm_start)\n \t\tcallee_saved_size += 8; /* r12 */\n-\tif (bpf_prog-\u003eaux-\u003eexception_boundary) {\n+\tif (bpf_prog-\u003eaux-\u003eexception_boundary || bpf_cleanup_force_spill(bpf_prog)) {\n \t\tcallee_saved_size += 4 * 8; /* rbx, r13, r14, r15 */\n \t} else {\n \t\tint j;\n@@ -2063,7 +2073,19 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *\n \t * Note that tail_call_reachable is guaranteed to be false when\n \t * stack args exist, so tcc pushes need not be accounted for.\n \t */\n-\toutgoing_arg_base = -(round_up(stack_depth, 8) + callee_saved_size);\n+\toutgoing_arg_base = -(round_up(stack_depth, 8) + throw_spill + callee_saved_size);\n+\n+\t/*\n+\t * Lowest address of each spill area, as an offset from rbp; see\n+\t * bpf_cleanup_pad.S for the layout. The 16 is the tail call counter\n+\t * pair emit_prologue_tail_call() pushes above the callee-saved one.\n+\t */\n+\tif (bpf_cleanup_force_spill(bpf_prog)) {\n+\t\tbpf_prog-\u003eaux-\u003eexc-\u003espill_off = -(round_up(stack_depth, 8) + throw_spill +\n+\t\t\t\t\t\t (tail_call_reachable ? 16 : 0) +\n+\t\t\t\t\t\t callee_saved_size);\n+\t\tbpf_prog-\u003eaux-\u003eexc-\u003ethrow_spill_off = -(round_up(stack_depth, 8) + throw_spill);\n+\t}\n \n \t/*\n \t * Allocate outgoing stack arg area for args 7+ only.\n@@ -2110,7 +2132,8 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *\n \t\t\t\tdst_reg = X86_REG_R9;\n \t\t}\n \n-\t\tif (bpf_insn_is_indirect_target(env, bpf_prog, i - 1))\n+\t\tif (bpf_insn_is_indirect_target(env, bpf_prog, i - 1) ||\n+\t\t bpf_cleanup_insn_is_pad(bpf_prog, i - 1))\n \t\t\tEMIT_ENDBR();\n \n \t\tip = image + addrs[i - 1] + (prog - temp);\n@@ -2903,9 +2926,27 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *\n \t\tcase BPF_JMP | BPF_CALL: {\n \t\t\tconst struct btf_func_model *fm = NULL;\n \n+\t\t\tif (bpf_cleanup_insn_is_throw(bpf_prog, i - 1)) {\n+\t\t\t\t/* Spill r6-r9 and r12 where the bpf_throw() walker looks. */\n+\t\t\t\ts32 off = bpf_prog-\u003eaux-\u003eexc-\u003ethrow_spill_off;\n+\t\t\t\tu8 *spill = prog;\n+\n+\t\t\t\temit_stx(\u0026prog, BPF_DW, BPF_REG_FP, BPF_REG_9, off + 0);\n+\t\t\t\temit_stx(\u0026prog, BPF_DW, BPF_REG_FP, BPF_REG_8, off + 8);\n+\t\t\t\temit_stx(\u0026prog, BPF_DW, BPF_REG_FP, BPF_REG_7, off + 16);\n+\t\t\t\temit_stx(\u0026prog, BPF_DW, BPF_REG_FP, BPF_REG_6, off + 24);\n+\t\t\t\temit_stx(\u0026prog, BPF_DW, BPF_REG_FP, X86_REG_R12, off + 32);\n+\t\t\t\tip += prog - spill;\n+\t\t\t}\n+\n+\t\t\tif (bpf_is_unwind_resume_kfunc(insn)) {\n+\t\t\t\temit_return(\u0026prog, image + addrs[i - 1] + (prog - temp));\n+\t\t\t\tbreak;\n+\t\t\t}\n+\n \t\t\tfunc = (u8 *) __bpf_call_base + imm32;\n \t\t\tif (src_reg == BPF_PSEUDO_CALL \u0026\u0026 tail_call_reachable) {\n-\t\t\t\tLOAD_TAIL_CALL_CNT_PTR(stack_depth);\n+\t\t\t\tLOAD_TAIL_CALL_CNT_PTR(prologue_depth);\n \t\t\t\tip += 7;\n \t\t\t}\n \t\t\tif (!imm32)\n@@ -2948,13 +2989,13 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *\n \t\t\t\t\t\t\t \u0026prog,\n \t\t\t\t\t\t\t ip,\n \t\t\t\t\t\t\t callee_regs_used,\n-\t\t\t\t\t\t\t stack_depth,\n+\t\t\t\t\t\t\t prologue_depth,\n \t\t\t\t\t\t\t ctx);\n \t\t\telse\n \t\t\t\temit_bpf_tail_call_indirect(bpf_prog,\n \t\t\t\t\t\t\t \u0026prog,\n \t\t\t\t\t\t\t callee_regs_used,\n-\t\t\t\t\t\t\t stack_depth,\n+\t\t\t\t\t\t\t prologue_depth,\n \t\t\t\t\t\t\t ip,\n \t\t\t\t\t\t\t ctx);\n \t\t\tbreak;\n@@ -3215,7 +3256,8 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *\n \t\t\t}\n \t\t\t/* Deallocate outgoing args 7+ area. */\n \t\t\temit_add_rsp(\u0026prog, outgoing_rsp);\n-\t\t\tif (bpf_prog-\u003eaux-\u003eexception_boundary) {\n+\t\t\tif (bpf_prog-\u003eaux-\u003eexception_boundary ||\n+\t\t\t bpf_cleanup_force_spill(bpf_prog)) {\n \t\t\t\tpop_callee_regs(\u0026prog, all_callee_regs_used);\n \t\t\t\tpop_r12(\u0026prog);\n \t\t\t} else {\n@@ -4385,6 +4427,13 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr\n \t\t */\n \t\tbpf_prog_update_insn_ptrs(prog, addrs, image);\n \n+\t\t/*\n+\t\t * Same mapping, consumed by the bpf_throw() frame walker:\n+\t\t * turn the cleanup records into native address ranges now\n+\t\t * that the image is final.\n+\t\t */\n+\t\tbpf_cleanup_fill_native_ranges(prog, addrs, image);\n+\n \t\t/*\n \t\t * ctx.prog_offset is used when CFI preambles put code *before*\n \t\t * the function. See emit_cfi(). For FineIBT specifically this code\n@@ -4501,6 +4550,11 @@ bool bpf_jit_supports_exceptions(void)\n \treturn IS_ENABLED(CONFIG_UNWINDER_ORC);\n }\n \n+bool bpf_jit_supports_cleanup_pads(void)\n+{\n+\treturn IS_ENABLED(CONFIG_UNWINDER_ORC);\n+}\n+\n bool bpf_jit_supports_private_stack(void)\n {\n \treturn true;\ndiff --git a/include/linux/bpf.h b/include/linux/bpf.h\nindex 2a5fa346aadaa..83f2b0d7e5961 100644\n--- a/include/linux/bpf.h\n+++ b/include/linux/bpf.h\n@@ -1770,6 +1770,80 @@ enum bpf_sig_keyring {\n \tBPF_SIG_KEYRING_BPF,\n };\n \n+/*\n+ * One cleanup region of a JITed (sub)program: @pad is the landing pad to run\n+ * for a return address in (begin, end], the native code of its call sites.\n+ */\n+struct bpf_cleanup_range {\n+\tu64 begin;\n+\tu64 end;\n+\tu64 pad;\n+};\n+\n+struct bpf_exception_info {\n+\tstruct bpf_cleanup_info *info;\n+\tstruct bpf_cleanup_range *ranges;\n+\t/* Landing pad instruction indices, sorted and deduplicated. */\n+\tu32 *pad_at;\n+\t/* bpf_throw() call instruction indices, sorted. */\n+\tu32 *throw_at;\n+\t/* One bit per instruction that only runs while unwinding. */\n+\tunsigned long *pad_body;\n+\tu32 nr_info;\n+\tu32 nr_ranges;\n+\tu32 nr_pad_at;\n+\tu32 nr_throw_at;\n+\tu32 nr_pad_body;\n+\t/* Offset from a frame's FP to the caller's spilled r6-r9. */\n+\ts32 spill_off;\n+\t/* Likewise, to the registers a frame spills before calling bpf_throw(). */\n+\ts32 throw_spill_off;\n+};\n+\n+#ifdef CONFIG_BPF_SYSCALL\n+bool bpf_cleanup_force_spill(const struct bpf_prog *prog);\n+bool bpf_cleanup_insn_is_pad(const struct bpf_prog *prog, u32 idx);\n+bool bpf_cleanup_insn_in_pad(const struct bpf_prog *prog, u32 idx);\n+bool bpf_cleanup_insn_is_throw(const struct bpf_prog *prog, u32 idx);\n+int bpf_cleanup_attach_main_prog(struct bpf_verifier_env *env, struct bpf_prog *prog);\n+void bpf_cleanup_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image);\n+void bpf_cleanup_free_info(struct bpf_prog_aux *aux);\n+#else\n+static inline bool bpf_cleanup_force_spill(const struct bpf_prog *prog)\n+{\n+\treturn false;\n+}\n+\n+static inline bool bpf_cleanup_insn_is_pad(const struct bpf_prog *prog, u32 idx)\n+{\n+\treturn false;\n+}\n+\n+static inline bool bpf_cleanup_insn_in_pad(const struct bpf_prog *prog, u32 idx)\n+{\n+\treturn false;\n+}\n+\n+static inline bool bpf_cleanup_insn_is_throw(const struct bpf_prog *prog, u32 idx)\n+{\n+\treturn false;\n+}\n+\n+static inline int bpf_cleanup_attach_main_prog(struct bpf_verifier_env *env,\n+\t\t\t\t\t struct bpf_prog *prog)\n+{\n+\treturn 0;\n+}\n+\n+static inline void bpf_cleanup_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image)\n+{\n+}\n+\n+static inline void bpf_cleanup_free_info(struct bpf_prog_aux *aux)\n+{\n+}\n+#endif\n+\n struct bpf_prog_aux {\n \tatomic64_t refcnt;\n \tu32 used_map_cnt;\n@@ -1850,6 +1924,7 @@ struct bpf_prog_aux {\n \tchar name[BPF_OBJ_NAME_LEN];\n \tu64 (*bpf_exception_cb)(u64 cookie, u64 sp, u64 bp, u64, u64);\n \tu16 stack_arg_sp_adjust;\n+\tstruct bpf_exception_info *exc;\n #ifdef CONFIG_SECURITY\n \tvoid *security;\n #endif\ndiff --git a/include/linux/bpf_cleanup_abi.h b/include/linux/bpf_cleanup_abi.h\nnew file mode 100644\nindex 0000000000000..b6c1d589abda7\n--- /dev/null\n+++ b/include/linux/bpf_cleanup_abi.h\n@@ -0,0 +1,16 @@\n+/* SPDX-License-Identifier: GPL-2.0-only */\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+#ifndef _LINUX_BPF_CLEANUP_ABI_H\n+#define _LINUX_BPF_CLEANUP_ABI_H\n+\n+/*\n+ * Value arch_bpf_run_cleanup_pad() leaves in r0 on the way into a landing pad.\n+ * It has to be a constant the verifier knows: LLVM names r0 as both the\n+ * exception pointer and the exception selector register, so every pad reads it\n+ * before anything else and is free to store what it read. Kept on its own\n+ * because the verifier and the arch dispatchers, which are assembly, have to\n+ * agree on it.\n+ */\n+#define BPF_PAD_ENTRY_R0\t1\n+\n+#endif /* _LINUX_BPF_CLEANUP_ABI_H */\ndiff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h\nindex cf85141ea1674..09fb89fda40af 100644\n--- a/include/linux/bpf_verifier.h\n+++ b/include/linux/bpf_verifier.h\n@@ -509,6 +509,7 @@ struct bpf_verifier_state {\n \n \tbool speculative;\n \tbool in_sleepable;\n+\tbool unwinding;\n \n \t/* first and last insn idx of this verifier state */\n \tu32 first_insn_idx;\n@@ -681,6 +682,13 @@ struct bpf_insn_aux_data {\n \tbool needs_zext; /* alu op needs to clear upper bits */\n \tbool non_sleepable; /* helper/kfunc may be called from non-sleepable context */\n \tbool is_iter_next; /* bpf_iter_\u003ctype\u003e_next() kfunc call */\n+\tbool cleanup_throw_site; /* call to bpf_throw() */\n+\tbool in_cleanup_pad; /* only runs with an exception in flight */\n+\t/*\n+\t * 1 + the instruction index of the exception cleanup landing pad this\n+\t * call site unwinds to, or 0 for none.\n+\t */\n+\tu32 cleanup_pad;\n \tbool call_with_percpu_alloc_ptr; /* {this,per}_cpu_ptr() with prog percpu alloc */\n \tu8 alu_state; /* used in combination with alu_limit */\n \t/* true if STX or LDX instruction is a part of a spill/fill\n@@ -987,6 +995,8 @@ struct bpf_verifier_env {\n \tstruct arg_track **callsite_at_stack;\n \tu32 pass_cnt; /* number of times do_check() was called */\n \tu32 subprog_cnt;\n+\tstruct bpf_cleanup_info *cleanup_info;\n+\tu32 cleanup_info_cnt;\n \t/* number of instructions analyzed by the verifier */\n \tu32 prev_insn_processed, insn_processed;\n \t/* number of jmps, calls, exits analyzed so far */\n@@ -1516,6 +1526,7 @@ u32 btf_func_arg_align(const struct btf *btf, const struct btf_type *t);\n \n int bpf_find_subprog(struct bpf_verifier_env *env, int off);\n bool bpf_is_throw_kfunc(struct bpf_insn *insn);\n+bool bpf_is_unwind_resume_kfunc(const struct bpf_insn *insn);\n int bpf_compute_const_regs(struct bpf_verifier_env *env);\n int bpf_prune_dead_branches(struct bpf_verifier_env *env);\n int bpf_check_cfg(struct bpf_verifier_env *env);\ndiff --git a/include/linux/filter.h b/include/linux/filter.h\nindex b17222db2efc3..287cd9b59aa9e 100644\n--- a/include/linux/filter.h\n+++ b/include/linux/filter.h\n@@ -1242,6 +1242,8 @@ bool bpf_jit_supports_stack_args(void);\n bool bpf_jit_supports_arena_args(void);\n bool bpf_jit_supports_far_kfunc_call(void);\n bool bpf_jit_supports_exceptions(void);\n+bool bpf_jit_supports_cleanup_pads(void);\n+void arch_bpf_run_cleanup_pad(u64 pad, u64 frame_fp, u64 spill_base);\n bool bpf_jit_supports_ptr_xchg(void);\n bool bpf_jit_supports_arena(void);\n bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena);\ndiff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h\nindex 732b35cc08d1c..f7dc121be0947 100644\n--- a/include/uapi/linux/bpf.h\n+++ b/include/uapi/linux/bpf.h\n@@ -1669,6 +1669,9 @@ union bpf_attr {\n \t\t * verification.\n \t\t */\n \t\t__s32\t\tkeyring_id;\n+\t\t__aligned_u64\tcleanup_info;\t/* exception cleanup table */\n+\t\t__u32\t\tcleanup_info_rec_size; /* userspace bpf_cleanup_info size */\n+\t\t__u32\t\tcleanup_info_cnt; /* number of bpf_cleanup_info records */\n \t};\n \n \tstruct { /* anonymous struct used by BPF_OBJ_* commands */\n@@ -7588,6 +7591,12 @@ struct bpf_line_info {\n \t__u32\tline_col;\n };\n \n+struct bpf_cleanup_info {\n+\t__u32\tbegin_off;\n+\t__u32\tend_off;\n+\t__u32\tlanding_pad_off;\n+};\n+\n struct bpf_spin_lock {\n \t__u32\tval;\n };\ndiff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile\nindex 9a92c348bbda6..af9bc60428ad1 100644\n--- a/kernel/bpf/Makefile\n+++ b/kernel/bpf/Makefile\n@@ -11,7 +11,7 @@ obj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_\n obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o\n obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o bpf_insn_array.o\n obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o\n-obj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o backtrack.o check_btf.o\n+obj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o backtrack.o check_btf.o exception.o\n obj-${CONFIG_BPF_LSM}\t += bpf_inode_storage.o\n obj-$(CONFIG_BPF_SYSCALL) += disasm.o mprog.o\n obj-$(CONFIG_BPF_JIT) += trampoline.o\ndiff --git a/kernel/bpf/cfg.c b/kernel/bpf/cfg.c\nindex 842c7d1eabccc..5a2b48b6a9e1d 100644\n--- a/kernel/bpf/cfg.c\n+++ b/kernel/bpf/cfg.c\n@@ -6,6 +6,7 @@\n #include \u003clinux/sort.h\u003e\n \n #include \"diagnostics.h\"\n+#include \"exception.h\"\n \n #define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)\n \n@@ -158,17 +159,57 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)\n \treturn DONE_EXPLORING;\n }\n \n+static int visit_cleanup_pad_edge(int t, struct bpf_verifier_env *env)\n+{\n+\tint *insn_stack = env-\u003ecfg.insn_stack;\n+\tint *insn_state = env-\u003ecfg.insn_state;\n+\tint w;\n+\n+\tif (!env-\u003ecleanup_info_cnt)\n+\t\treturn DONE_EXPLORING;\n+\tw = bpf_cleanup_pad_of_call(env, t);\n+\tif (w \u003c 0)\n+\t\treturn DONE_EXPLORING;\n+\n+\tmark_prune_point(env, t);\n+\tmark_jmp_point(env, w);\n+\tmark_jump_target(env, w);\n+\n+\tif (insn_state[w])\n+\t\treturn DONE_EXPLORING;\n+\tif (env-\u003ecfg.cur_stack \u003e= env-\u003eprog-\u003elen)\n+\t\treturn -E2BIG;\n+\tinsn_stack[env-\u003ecfg.cur_stack++] = w;\n+\tinsn_state[w] |= DISCOVERED;\n+\treturn KEEP_EXPLORING;\n+}\n+\n+static int merge_visit_ret(int a, int b)\n+{\n+\tif (a \u003c 0)\n+\t\treturn a;\n+\tif (b \u003c 0)\n+\t\treturn b;\n+\tif (a == KEEP_EXPLORING || b == KEEP_EXPLORING)\n+\t\treturn KEEP_EXPLORING;\n+\treturn DONE_EXPLORING;\n+}\n+\n static int visit_func_call_insn(int t, struct bpf_insn *insns,\n \t\t\t\tstruct bpf_verifier_env *env,\n \t\t\t\tbool visit_callee)\n {\n-\tint ret, insn_sz;\n+\tint ret, insn_sz, pad_ret;\n \tint w;\n \n+\tpad_ret = visit_cleanup_pad_edge(t, env);\n+\tif (pad_ret \u003c 0)\n+\t\treturn pad_ret;\n+\n \tinsn_sz = bpf_is_ldimm64(\u0026insns[t]) ? 2 : 1;\n \tret = push_insn(t, t + insn_sz, FALLTHROUGH, env);\n \tif (ret)\n-\t\treturn ret;\n+\t\treturn merge_visit_ret(pad_ret, ret);\n \n \tmark_prune_point(env, t + insn_sz);\n \t/* when we exit from subprog, we need to record non-linear history */\n@@ -180,7 +221,7 @@ static int visit_func_call_insn(int t, struct bpf_insn *insns,\n \t\tmerge_callee_effects(env, t, w);\n \t\tret = push_insn(t, w, BRANCH, env);\n \t}\n-\treturn ret;\n+\treturn merge_visit_ret(pad_ret, ret);\n }\n \n struct bpf_iarray *bpf_iarray_realloc(struct bpf_iarray *old, size_t n_elem)\n@@ -592,6 +633,7 @@ int bpf_check_cfg(struct bpf_verifier_env *env)\n \tint insn_cnt = env-\u003eprog-\u003elen;\n \tint *insn_stack, *insn_state;\n \tint ex_insn_beg, i, ret = 0;\n+\tu32 pad_idx = 0;\n \n \tinsn_state = env-\u003ecfg.insn_state = kvzalloc_objs(int, insn_cnt,\n \t\t\t\t\t\t\t GFP_KERNEL_ACCOUNT);\n@@ -647,6 +689,22 @@ int bpf_check_cfg(struct bpf_verifier_env *env)\n \t\tgoto walk_cfg;\n \t}\n \n+\t/*\n+\t * A landing pad no call site was marked with -- a record whose range\n+\t * holds no call an exception can unwind out of -- is reached by\n+\t * nothing. Walk it from here, and let the dead code sweep remove it.\n+\t */\n+\twhile (pad_idx \u003c env-\u003ecleanup_info_cnt) {\n+\t\tu32 pad = env-\u003ecleanup_info[pad_idx++].landing_pad_off;\n+\n+\t\tif (insn_state[pad] != EXPLORED) {\n+\t\t\tinsn_state[pad] = DISCOVERED;\n+\t\t\tinsn_stack[0] = pad;\n+\t\t\tenv-\u003ecfg.cur_stack = 1;\n+\t\t\tgoto walk_cfg;\n+\t\t}\n+\t}\n+\n \tfor (i = 0; i \u003c insn_cnt; i++) {\n \t\tstruct bpf_insn *insn = \u0026env-\u003eprog-\u003einsnsi[i];\n \ndiff --git a/kernel/bpf/check_btf.c b/kernel/bpf/check_btf.c\nindex 0e8b3ccc7a5b9..d03dc791042a2 100644\n--- a/kernel/bpf/check_btf.c\n+++ b/kernel/bpf/check_btf.c\n@@ -407,6 +407,149 @@ static int check_core_relo(struct bpf_verifier_env *env,\n \treturn err;\n }\n \n+static int cleanup_insn_subprog(struct bpf_verifier_env *env, u32 off)\n+{\n+\tstruct bpf_subprog_info *info;\n+\n+\tif (off \u003e= env-\u003eprog-\u003elen)\n+\t\treturn -1;\n+\tinfo = bpf_find_containing_subprog(env, off);\n+\treturn info ? info - env-\u003esubprog_info : -1;\n+}\n+\n+#define MIN_BPF_CLEANUP_INFO_SIZE\t12\n+#define MAX_CLEANUP_INFO_REC_SIZE\tMAX_FUNCINFO_REC_SIZE\n+\n+static int check_cleanup_info(struct bpf_verifier_env *env,\n+\t\t\t const union bpf_attr *attr,\n+\t\t\t bpfptr_t uattr)\n+{\n+\tu32 krec_size = sizeof(struct bpf_cleanup_info);\n+\tu32 i, nrec, urec_size, min_size, prev_end = 0;\n+\tstruct bpf_cleanup_info *krecord;\n+\tbpfptr_t urecord;\n+\tint ret = -EINVAL;\n+\n+\tnrec = attr-\u003ecleanup_info_cnt;\n+\tif (!nrec)\n+\t\treturn 0;\n+\tif (nrec \u003e INT_MAX / krec_size)\n+\t\treturn -EINVAL;\n+\n+\turec_size = attr-\u003ecleanup_info_rec_size;\n+\tif (urec_size \u003c MIN_BPF_CLEANUP_INFO_SIZE ||\n+\t urec_size \u003e MAX_CLEANUP_INFO_REC_SIZE ||\n+\t urec_size % sizeof(u32)) {\n+\t\tverbose(env, \"invalid cleanup info rec size %u\\n\", urec_size);\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tkrecord = kvcalloc(nrec, krec_size, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);\n+\tif (!krecord)\n+\t\treturn -ENOMEM;\n+\n+\tmin_size = min_t(u32, krec_size, urec_size);\n+\turecord = make_bpfptr(attr-\u003ecleanup_info, uattr.is_kernel);\n+\tfor (i = 0; i \u003c nrec; i++) {\n+\t\tstruct bpf_cleanup_info *rec = \u0026krecord[i];\n+\t\tint sb, se, sl;\n+\n+\t\tret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size);\n+\t\tif (ret) {\n+\t\t\tif (ret == -E2BIG) {\n+\t\t\t\tverbose(env, \"nonzero tailing record in cleanup info\\n\");\n+\t\t\t\tif (copy_to_bpfptr_offset(uattr,\n+\t\t\t\t\t\t\t offsetof(union bpf_attr,\n+\t\t\t\t\t\t\t\t cleanup_info_rec_size),\n+\t\t\t\t\t\t\t \u0026min_size, sizeof(min_size)))\n+\t\t\t\t\tret = -EFAULT;\n+\t\t\t}\n+\t\t\tgoto err_free;\n+\t\t}\n+\n+\t\tif (copy_from_bpfptr(rec, urecord, min_size)) {\n+\t\t\tret = -EFAULT;\n+\t\t\tgoto err_free;\n+\t\t}\n+\t\tbpfptr_add(\u0026urecord, urec_size);\n+\n+\t\tret = -EINVAL;\n+\t\tif (rec-\u003ebegin_off \u003e= rec-\u003eend_off) {\n+\t\t\tverbose(env, \"cleanup_info[%u]: begin %u \u003e= end %u\\n\",\n+\t\t\t\ti, rec-\u003ebegin_off, rec-\u003eend_off);\n+\t\t\tgoto err_free;\n+\t\t}\n+\t\tif (i \u0026\u0026 rec-\u003ebegin_off \u003c prev_end) {\n+\t\t\tverbose(env,\n+\t\t\t\t\"cleanup_info[%u]: range [%u,%u) is unsorted or overlaps the previous record\\n\",\n+\t\t\t\ti, rec-\u003ebegin_off, rec-\u003eend_off);\n+\t\t\tgoto err_free;\n+\t\t}\n+\t\tprev_end = rec-\u003eend_off;\n+\n+\t\tsb = cleanup_insn_subprog(env, rec-\u003ebegin_off);\n+\t\tse = cleanup_insn_subprog(env, rec-\u003eend_off - 1);\n+\t\tsl = cleanup_insn_subprog(env, rec-\u003elanding_pad_off);\n+\t\tif (sb \u003c 0 || se \u003c 0 || sl \u003c 0) {\n+\t\t\tverbose(env, \"cleanup_info[%u]: offset out of range\\n\", i);\n+\t\t\tgoto err_free;\n+\t\t}\n+\t\tif (sb != se || sb != sl) {\n+\t\t\tverbose(env,\n+\t\t\t\t\"cleanup_info[%u]: range/landing pad span multiple subprogs\\n\",\n+\t\t\t\ti);\n+\t\t\tgoto err_free;\n+\t\t}\n+\t\t/*\n+\t\t * The second half of a 16-byte instruction carries a zero\n+\t\t * opcode and is not an instruction of its own, so no offset\n+\t\t * may name one. end_off is exclusive, so it may also be one\n+\t\t * past the last instruction of the program.\n+\t\t */\n+\t\tif (!env-\u003eprog-\u003einsnsi[rec-\u003ebegin_off].code ||\n+\t\t !env-\u003eprog-\u003einsnsi[rec-\u003elanding_pad_off].code ||\n+\t\t (rec-\u003eend_off \u003c env-\u003eprog-\u003elen \u0026\u0026\n+\t\t !env-\u003eprog-\u003einsnsi[rec-\u003eend_off].code)) {\n+\t\t\tverbose(env, \"cleanup_info[%u]: points at invalid insn\\n\", i);\n+\t\t\tgoto err_free;\n+\t\t}\n+\t}\n+\n+\t/*\n+\t * Reject a landing pad that lies inside a call-site range, its own\n+\t * included: it would be both a pad and a call that unwinds to one, and\n+\t * an exception out of it would have nowhere to go.\n+\t */\n+\tret = -EINVAL;\n+\tfor (i = 0; i \u003c nrec; i++) {\n+\t\tu32 pad = krecord[i].landing_pad_off;\n+\t\tu32 l = 0, r = nrec;\n+\n+\t\twhile (l \u003c r) {\n+\t\t\tu32 m = l + (r - l) / 2;\n+\n+\t\t\tif (pad \u003c krecord[m].begin_off) {\n+\t\t\t\tr = m;\n+\t\t\t} else if (pad \u003e= krecord[m].end_off) {\n+\t\t\t\tl = m + 1;\n+\t\t\t} else {\n+\t\t\t\tverbose(env,\n+\t\t\t\t\t\"cleanup_info[%u]: landing pad %u is inside the call-site range of cleanup_info[%u]\\n\",\n+\t\t\t\t\ti, pad, m);\n+\t\t\t\tgoto err_free;\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\tenv-\u003ecleanup_info = krecord;\n+\tenv-\u003ecleanup_info_cnt = nrec;\n+\treturn 0;\n+\n+err_free:\n+\tkvfree(krecord);\n+\treturn ret;\n+}\n+\n int bpf_prepare_btf_info(struct bpf_verifier_env *env,\n \t\t\t const union bpf_attr *attr,\n \t\t\t bpfptr_t uattr)\n@@ -441,6 +584,10 @@ int bpf_check_btf_info(struct bpf_verifier_env *env,\n {\n \tint err;\n \n+\terr = check_cleanup_info(env, attr, uattr);\n+\tif (err)\n+\t\treturn err;\n+\n \tif (!attr-\u003efunc_info_cnt \u0026\u0026 !attr-\u003eline_info_cnt) {\n \t\tif (check_abnormal_return(env))\n \t\t\treturn -EINVAL;\ndiff --git a/kernel/bpf/core.c b/kernel/bpf/core.c\nindex 4e208cc94752f..bd2919063cecd 100644\n--- a/kernel/bpf/core.c\n+++ b/kernel/bpf/core.c\n@@ -292,6 +292,7 @@ void __bpf_prog_free(struct bpf_prog *fp)\n \t\tmutex_destroy(\u0026fp-\u003eaux-\u003edst_mutex);\n \t\tmutex_destroy(\u0026fp-\u003eaux-\u003est_ops_assoc_mutex);\n \t\tkfree(fp-\u003eaux-\u003epoke_tab);\n+\t\tbpf_cleanup_free_info(fp-\u003eaux);\n \t\tkfree(fp-\u003eaux);\n \t}\n \tfree_percpu(fp-\u003estats);\n@@ -2625,13 +2626,21 @@ static bool bpf_prog_select_interpreter(struct bpf_prog *fp)\n \treturn select_interpreter;\n }\n \n-static struct bpf_prog *bpf_prog_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)\n+static struct bpf_prog *bpf_prog_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog,\n+\t\t\t\t\t int *err)\n {\n #ifdef CONFIG_BPF_JIT\n \tstruct bpf_prog *orig_prog;\n+\tint ret;\n \n-\tif (!bpf_prog_need_blind(prog))\n+\tif (!bpf_prog_need_blind(prog)) {\n+\t\tret = bpf_cleanup_attach_main_prog(env, prog);\n+\t\tif (ret) {\n+\t\t\t*err = ret;\n+\t\t\treturn prog;\n+\t\t}\n \t\treturn bpf_int_jit_compile(env, prog);\n+\t}\n \n \torig_prog = prog;\n \tprog = bpf_jit_blind_constants(env, prog);\n@@ -2642,6 +2651,13 @@ static struct bpf_prog *bpf_prog_jit_compile(struct bpf_verifier_env *env, struc\n \tif (IS_ERR(prog))\n \t\tgoto out_restore;\n \n+\tret = bpf_cleanup_attach_main_prog(env, prog);\n+\tif (ret) {\n+\t\t*err = ret;\n+\t\tbpf_jit_prog_release_other(orig_prog, prog);\n+\t\tgoto out_restore;\n+\t}\n+\n \tprog = bpf_int_jit_compile(env, prog);\n \tif (prog-\u003ejited) {\n \t\tbpf_jit_prog_release_other(prog, orig_prog);\n@@ -2681,8 +2697,10 @@ struct bpf_prog *__bpf_prog_select_runtime(struct bpf_verifier_env *env, struct\n \t\tif (*err)\n \t\t\treturn fp;\n \n-\t\tfp = bpf_prog_jit_compile(env, fp);\n+\t\tfp = bpf_prog_jit_compile(env, fp, err);\n \t\tbpf_prog_jit_attempt_done(fp);\n+\t\tif (*err)\n+\t\t\treturn fp;\n \t\tif (!fp-\u003ejited \u0026\u0026 jit_needed) {\n \t\t\t*err = -ENOTSUPP;\n \t\t\treturn fp;\n@@ -3470,6 +3488,17 @@ void __weak arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp,\n {\n }\n \n+bool __weak bpf_jit_supports_cleanup_pads(void)\n+{\n+\treturn false;\n+}\n+\n+/* Call @pad with the frame pointer @frame_fp and r6-r9 spilled at @spill_base. */\n+void __weak arch_bpf_run_cleanup_pad(u64 pad, u64 frame_fp, u64 spill_base)\n+{\n+\tWARN_ON_ONCE(1);\n+}\n+\n bool __weak bpf_jit_supports_timed_may_goto(void)\n {\n \treturn false;\ndiff --git a/kernel/bpf/exception.c b/kernel/bpf/exception.c\nnew file mode 100644\nindex 0000000000000..521086d084a35\n--- /dev/null\n+++ b/kernel/bpf/exception.c\n@@ -0,0 +1,654 @@\n+// SPDX-License-Identifier: GPL-2.0-only\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+#include \u003clinux/bitmap.h\u003e\n+#include \u003clinux/bpf.h\u003e\n+#include \u003clinux/bpf_verifier.h\u003e\n+#include \u003clinux/btf.h\u003e\n+#include \u003clinux/btf_ids.h\u003e\n+#include \u003clinux/filter.h\u003e\n+#include \u003clinux/slab.h\u003e\n+#include \u003clinux/sort.h\u003e\n+#include \"exception.h\"\n+\n+#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)\n+\n+enum exc_kfunc {\n+\tEXC_KF_bpf_unwind_resume,\n+};\n+\n+BTF_ID_LIST(exc_kfunc_list)\n+BTF_ID(func, bpf_unwind_resume)\n+\n+static bool insn_is_exc_kfunc(const struct bpf_insn *insn, int kf)\n+{\n+\treturn bpf_pseudo_kfunc_call(insn) \u0026\u0026 insn-\u003eoff == 0 \u0026\u0026\n+\t insn-\u003eimm == exc_kfunc_list[kf];\n+}\n+\n+/* What an instruction does to intra-subprog control flow. */\n+enum cleanup_insn_kind {\n+\tCLEANUP_INSN_PLAIN,\t/* the next insn runs */\n+\tCLEANUP_INSN_JUMP,\t/* unconditional jump */\n+\tCLEANUP_INSN_COND,\t/* the next insn runs, or the branch target */\n+\tCLEANUP_INSN_EXIT,\n+\tCLEANUP_INSN_THROW,\t/* call bpf_throw: nothing after it runs */\n+\tCLEANUP_INSN_RESUME,\t/* call bpf_unwind_resume: likewise */\n+\tCLEANUP_INSN_CALL,\t/* call to another subprog */\n+\tCLEANUP_INSN_GOTOX,\t/* indirect jump: successors not known here */\n+};\n+\n+/* What each instruction can reach, computed once by cleanup_reachability(). */\n+#define CLEANUP_REACH_RESUME\tBIT(0)\t/* a bpf_unwind_resume() call */\n+#define CLEANUP_REACH_EXIT\tBIT(1)\t/* a plain BPF_EXIT */\n+#define CLEANUP_REACH_UNKNOWN\tBIT(2)\t/* an indirect jump */\n+#define CLEANUP_REACH_THROW\tBIT(3)\t/* a bpf_throw() call */\n+\n+/* Scratch shared by the analyses, sized once so no walker has to allocate. */\n+struct cleanup_ctx {\n+\tstruct bpf_verifier_env *env;\n+\tu8 *reach;\t\t/* per insn: CLEANUP_REACH_* mask */\n+\tu32 *stack;\t\t/* per insn: DFS stack */\n+\tvoid *scratch;\t\t/* the one allocation all of the above live in */\n+};\n+\n+static bool in_pad(struct bpf_verifier_env *env, u32 i)\n+{\n+\treturn env-\u003einsn_aux_data[i].in_cleanup_pad;\n+}\n+\n+/* One scratch array for cleanup_alloc() to hand out. */\n+struct cleanup_alloc_req {\n+\tvoid **dst;\n+\tsize_t n, sz;\n+};\n+\n+static void *cleanup_alloc(const struct cleanup_alloc_req *tab, u32 cnt)\n+{\n+\tsize_t total = 0;\n+\tchar *block, *p;\n+\tu32 i;\n+\n+\tfor (i = 0; i \u003c cnt; i++)\n+\t\ttotal += round_up(tab[i].n * tab[i].sz, 8);\n+\n+\tblock = kvzalloc(total, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);\n+\tif (!block)\n+\t\treturn NULL;\n+\n+\tfor (i = 0, p = block; i \u003c cnt; i++) {\n+\t\t*tab[i].dst = p;\n+\t\tp += round_up(tab[i].n * tab[i].sz, 8);\n+\t}\n+\treturn block;\n+}\n+\n+static int cleanup_subprog_of(struct bpf_verifier_env *env, u32 off)\n+{\n+\tstruct bpf_subprog_info *info = bpf_find_containing_subprog(env, off);\n+\n+\treturn info ? info - env-\u003esubprog_info : -1;\n+}\n+\n+/* The subprogram a linear pass is currently in. */\n+struct cleanup_cursor {\n+\tu32 start, end;\t\t/* [start, end) of the current subprogram */\n+\tint sub;\t\t/* its index */\n+};\n+\n+#define CLEANUP_CURSOR_INIT { .sub = -1 }\n+\n+static void cleanup_cursor_to(struct bpf_verifier_env *env, struct cleanup_cursor *c, u32 i)\n+{\n+\twhile (i \u003e= c-\u003eend) {\n+\t\tc-\u003esub++;\n+\t\tc-\u003estart = env-\u003esubprog_info[c-\u003esub].start;\n+\t\tc-\u003eend = env-\u003esubprog_info[c-\u003esub + 1].start;\n+\t}\n+}\n+\n+static enum cleanup_insn_kind cleanup_classify(struct bpf_verifier_env *env, u32 i,\n+\t\t\t\t\t int *next, int *target)\n+{\n+\tstruct bpf_insn *insn = \u0026env-\u003eprog-\u003einsnsi[i];\n+\tu8 class = BPF_CLASS(insn-\u003ecode);\n+\n+\t*next = i + 1;\n+\t*target = -1;\n+\n+\tif (insn-\u003ecode == (BPF_LD | BPF_IMM | BPF_DW)) {\n+\t\t*next = i + 2;\n+\t\treturn CLEANUP_INSN_PLAIN;\n+\t}\n+\tif (class != BPF_JMP \u0026\u0026 class != BPF_JMP32)\n+\t\treturn CLEANUP_INSN_PLAIN;\n+\n+\tswitch (BPF_OP(insn-\u003ecode)) {\n+\tcase BPF_EXIT:\n+\t\t*next = -1;\n+\t\treturn CLEANUP_INSN_EXIT;\n+\tcase BPF_JA:\n+\t\t*next = -1;\n+\t\tif (BPF_SRC(insn-\u003ecode) == BPF_X)\n+\t\t\treturn CLEANUP_INSN_GOTOX;\n+\t\t*target = class == BPF_JMP32 ? i + insn-\u003eimm + 1 : i + insn-\u003eoff + 1;\n+\t\treturn CLEANUP_INSN_JUMP;\n+\tcase BPF_CALL:\n+\t\tif (bpf_is_throw_kfunc(insn)) {\n+\t\t\t*next = -1;\n+\t\t\treturn CLEANUP_INSN_THROW;\n+\t\t}\n+\t\tif (insn_is_exc_kfunc(insn, EXC_KF_bpf_unwind_resume)) {\n+\t\t\t*next = -1;\n+\t\t\treturn CLEANUP_INSN_RESUME;\n+\t\t}\n+\t\treturn bpf_pseudo_call(insn) ? CLEANUP_INSN_CALL : CLEANUP_INSN_PLAIN;\n+\tdefault:\n+\t\t/* Conditional jump, including BPF_JCOND. */\n+\t\t*target = i + insn-\u003eoff + 1;\n+\t\treturn CLEANUP_INSN_COND;\n+\t}\n+}\n+\n+static void cleanup_mark_throw_sites(struct bpf_verifier_env *env)\n+{\n+\tu32 i;\n+\n+\tfor (i = 0; i \u003c env-\u003eprog-\u003elen; i++)\n+\t\tif (bpf_is_throw_kfunc(\u0026env-\u003eprog-\u003einsnsi[i]))\n+\t\t\tenv-\u003einsn_aux_data[i].cleanup_throw_site = true;\n+}\n+\n+int bpf_cleanup_check_callback(struct bpf_verifier_env *env, int subprog)\n+{\n+\tif (!env-\u003ecleanup_info_cnt || !env-\u003esubprog_info[subprog].might_throw)\n+\t\treturn 0;\n+\n+\tverbose(env, \"subprog %d may unwind and is used as a callback\\n\", subprog);\n+\treturn -EINVAL;\n+}\n+\n+/* Intra-subprog successors of @i, or -1 each when absent. */\n+static enum cleanup_insn_kind cleanup_succ(struct bpf_verifier_env *env, u32 i,\n+\t\t\t\t\t u32 start, u32 end, int *next, int *target)\n+{\n+\tenum cleanup_insn_kind kind = cleanup_classify(env, i, next, target);\n+\n+\tif (*next \u003c (int)start || *next \u003e= (int)end)\n+\t\t*next = -1;\n+\tif (*target \u003c (int)start || *target \u003e= (int)end)\n+\t\t*target = -1;\n+\treturn kind;\n+}\n+\n+static void cleanup_add_pred(u32 *head, u32 *link, u32 to, u32 e)\n+{\n+\tlink[e] = head[to];\n+\thead[to] = e + 1;\n+}\n+\n+/* What every instruction can reach along intra-subprog edges, for\n+ * cleanup_pad_is_catch(). One backward walk over a predecessor index, rather\n+ * than a forward walk from each landing pad, which would be quadratic.\n+ */\n+static int cleanup_reachability(struct cleanup_ctx *ctx)\n+{\n+\tstruct bpf_verifier_env *env = ctx-\u003eenv;\n+\tu32 len = env-\u003eprog-\u003elen;\n+\tstruct cleanup_cursor c = CLEANUP_CURSOR_INIT;\n+\tu32 *head = NULL, *link = NULL;\n+\tbool *queued = NULL;\n+\tu32 i, sp = 0;\n+\tvoid *scratch;\n+\tconst struct cleanup_alloc_req tab[] = {\n+\t\t{ (void **)\u0026head, len, sizeof(*head) },\n+\t\t{ (void **)\u0026link, 2 * (size_t)len, sizeof(*link) },\n+\t\t{ (void **)\u0026queued, len, sizeof(*queued) },\n+\t};\n+\n+\tscratch = cleanup_alloc(tab, ARRAY_SIZE(tab));\n+\tif (!scratch)\n+\t\treturn -ENOMEM;\n+\n+\t/* Index the predecessors, and seed the walk at the terminators. */\n+\tfor (i = 0; i \u003c len; i++) {\n+\t\tenum cleanup_insn_kind kind;\n+\t\tint next, target;\n+\n+\t\tcleanup_cursor_to(env, \u0026c, i);\n+\t\tkind = cleanup_succ(env, i, c.start, c.end, \u0026next, \u0026target);\n+\n+\t\tif (kind == CLEANUP_INSN_RESUME)\n+\t\t\tctx-\u003ereach[i] |= CLEANUP_REACH_RESUME;\n+\t\telse if (kind == CLEANUP_INSN_EXIT)\n+\t\t\tctx-\u003ereach[i] |= CLEANUP_REACH_EXIT;\n+\t\telse if (kind == CLEANUP_INSN_GOTOX)\n+\t\t\tctx-\u003ereach[i] |= CLEANUP_REACH_UNKNOWN;\n+\t\telse if (kind == CLEANUP_INSN_THROW)\n+\t\t\tctx-\u003ereach[i] |= CLEANUP_REACH_THROW;\n+\n+\t\tif (next \u003e= 0)\n+\t\t\tcleanup_add_pred(head, link, next, 2 * i);\n+\t\tif (target \u003e= 0)\n+\t\t\tcleanup_add_pred(head, link, target, 2 * i + 1);\n+\n+\t\tif (ctx-\u003ereach[i]) {\n+\t\t\tqueued[i] = true;\n+\t\t\tctx-\u003estack[sp++] = i;\n+\t\t}\n+\t}\n+\n+\t/* Each instruction re-enters the worklist at most once per bit it\n+\t * gains, so this is linear in the number of edges.\n+\t */\n+\twhile (sp) {\n+\t\tu32 j = ctx-\u003estack[--sp];\n+\t\tu8 flags = ctx-\u003ereach[j];\n+\t\tu32 e;\n+\n+\t\tqueued[j] = false;\n+\t\tfor (e = head[j]; e; e = link[e - 1]) {\n+\t\t\tu32 p = (e - 1) / 2;\n+\n+\t\t\tif ((ctx-\u003ereach[p] | flags) == ctx-\u003ereach[p])\n+\t\t\t\tcontinue;\n+\t\t\tctx-\u003ereach[p] |= flags;\n+\t\t\tif (!queued[p]) {\n+\t\t\t\tqueued[p] = true;\n+\t\t\t\tctx-\u003estack[sp++] = p;\n+\t\t\t}\n+\t\t}\n+\t}\n+\tkvfree(scratch);\n+\treturn 0;\n+}\n+\n+static int cleanup_pad_is_catch(struct cleanup_ctx *ctx, u32 pad)\n+{\n+\tu8 reach = ctx-\u003ereach[pad];\n+\n+\tif (reach \u0026 CLEANUP_REACH_UNKNOWN) {\n+\t\tverbose(ctx-\u003eenv, \"cleanup landing pad %u reaches an indirect jump\\n\", pad);\n+\t\treturn -EINVAL;\n+\t}\n+\tif (reach \u0026 CLEANUP_REACH_THROW) {\n+\t\tverbose(ctx-\u003eenv,\n+\t\t\t\"cleanup landing pad %u can throw while an exception is in flight\\n\",\n+\t\t\tpad);\n+\t\treturn -EINVAL;\n+\t}\n+\tif (!(reach \u0026 CLEANUP_REACH_RESUME) == !(reach \u0026 CLEANUP_REACH_EXIT)) {\n+\t\tverbose(ctx-\u003eenv, \"cleanup landing pad %u %s\\n\", pad,\n+\t\t\t(reach \u0026 CLEANUP_REACH_RESUME) ?\n+\t\t\t\"reaches both bpf_unwind_resume() and a plain exit\" :\n+\t\t\t\"reaches neither bpf_unwind_resume() nor an exit\");\n+\t\treturn -EINVAL;\n+\t}\n+\treturn !!(reach \u0026 CLEANUP_REACH_EXIT);\n+}\n+\n+static int cleanup_check_pad_insn(struct bpf_verifier_env *env, u32 i)\n+{\n+\tstruct bpf_insn *insn = \u0026env-\u003eprog-\u003einsnsi[i];\n+\n+\tif (bpf_helper_call(insn) \u0026\u0026 insn-\u003eimm == BPF_FUNC_tail_call) {\n+\t\tverbose(env,\n+\t\t\t\"bpf_tail_call() at insn %u is in an exception cleanup landing pad\\n\",\n+\t\t\ti);\n+\t\treturn -EINVAL;\n+\t}\n+\t/* Stack arguments are not supported. */\n+\tif (is_stack_arg_st(insn) || is_stack_arg_stx(insn)) {\n+\t\tverbose(env,\n+\t\t\t\"insn %u passes an on-stack call argument in an exception cleanup landing pad\\n\",\n+\t\t\ti);\n+\t\treturn -EINVAL;\n+\t}\n+\t/* Likewise, stack arguments are not supported. */\n+\tif (bpf_pseudo_kfunc_call(insn)) {\n+\t\tstruct bpf_call_summary cs;\n+\n+\t\tif (bpf_get_call_summary(env, insn, \u0026cs) \u0026\u0026\n+\t\t cs.arg_slot_cnt \u003e MAX_BPF_FUNC_REG_ARGS) {\n+\t\t\tverbose(env,\n+\t\t\t\t\"insn %u passes an on-stack call argument in an exception cleanup landing pad\\n\",\n+\t\t\t\ti);\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\t}\n+\treturn 0;\n+}\n+\n+static int cleanup_mark_pad_bodies(struct cleanup_ctx *ctx)\n+{\n+\tstruct bpf_verifier_env *env = ctx-\u003eenv;\n+\tu32 i, sp = 0;\n+\tint ret;\n+\n+\tfor (i = 0; i \u003c env-\u003ecleanup_info_cnt; i++) {\n+\t\tu32 pad = env-\u003ecleanup_info[i].landing_pad_off;\n+\n+\t\tif (in_pad(env, pad))\n+\t\t\tcontinue;\n+\n+\t\tret = cleanup_pad_is_catch(ctx, pad);\n+\t\tif (ret \u003c 0)\n+\t\t\treturn ret;\n+\t\tif (ret) {\n+\t\t\tverbose(env,\n+\t\t\t\t\"catch landing pad %u is not supported yet, only cleanup pads that resume\\n\",\n+\t\t\t\tpad);\n+\t\t\treturn -EOPNOTSUPP;\n+\t\t}\n+\t\tenv-\u003einsn_aux_data[pad].in_cleanup_pad = true;\n+\t\tctx-\u003estack[sp++] = pad;\n+\t}\n+\n+\twhile (sp) {\n+\t\tu32 j = ctx-\u003estack[--sp];\n+\t\tenum cleanup_insn_kind kind;\n+\t\tint next, target, sub;\n+\t\tu32 start, end;\n+\n+\t\tret = cleanup_check_pad_insn(env, j);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\n+\t\tsub = cleanup_subprog_of(env, j);\n+\t\tstart = env-\u003esubprog_info[sub].start;\n+\t\tend = env-\u003esubprog_info[sub + 1].start;\n+\t\tkind = cleanup_succ(env, j, start, end, \u0026next, \u0026target);\n+\n+\t\tif (kind == CLEANUP_INSN_CALL) {\n+\t\t\tint callee = cleanup_subprog_of(env, j + env-\u003eprog-\u003einsnsi[j].imm + 1);\n+\n+\t\t\tif (env-\u003esubprog_info[callee].might_throw) {\n+\t\t\t\tverbose(env,\n+\t\t\t\t\t\"cleanup landing pad calls subprog %d at insn %u, which can throw while an exception is in flight\\n\",\n+\t\t\t\t\tcallee, j);\n+\t\t\t\treturn -EINVAL;\n+\t\t\t}\n+\t\t}\n+\n+\t\tif (next \u003e= 0 \u0026\u0026 !in_pad(env, next)) {\n+\t\t\tenv-\u003einsn_aux_data[next].in_cleanup_pad = true;\n+\t\t\tctx-\u003estack[sp++] = next;\n+\t\t}\n+\t\tif (target \u003e= 0 \u0026\u0026 !in_pad(env, target)) {\n+\t\t\tenv-\u003einsn_aux_data[target].in_cleanup_pad = true;\n+\t\t\tctx-\u003estack[sp++] = target;\n+\t\t}\n+\t}\n+\treturn 0;\n+}\n+\n+static int cleanup_check_resumes(struct cleanup_ctx *ctx)\n+{\n+\tstruct bpf_verifier_env *env = ctx-\u003eenv;\n+\tu32 i;\n+\n+\tfor (i = 0; i \u003c env-\u003eprog-\u003elen; i++) {\n+\t\tif (!insn_is_exc_kfunc(\u0026env-\u003eprog-\u003einsnsi[i], EXC_KF_bpf_unwind_resume))\n+\t\t\tcontinue;\n+\t\tif (in_pad(env, i))\n+\t\t\tcontinue;\n+\t\tverbose(env,\n+\t\t\t\"bpf_unwind_resume() at insn %u is not in an exception cleanup landing pad\\n\",\n+\t\t\ti);\n+\t\treturn -EINVAL;\n+\t}\n+\treturn 0;\n+}\n+\n+static void cleanup_mark_call_sites(struct bpf_verifier_env *env)\n+{\n+\tu32 i, j;\n+\n+\tfor (i = 0; i \u003c env-\u003ecleanup_info_cnt; i++) {\n+\t\tstruct bpf_cleanup_info *rec = \u0026env-\u003ecleanup_info[i];\n+\n+\t\tfor (j = rec-\u003ebegin_off; j \u003c rec-\u003eend_off; j++) {\n+\t\t\tstruct bpf_insn *insn = \u0026env-\u003eprog-\u003einsnsi[j];\n+\n+\t\t\tif (!bpf_pseudo_call(insn) \u0026\u0026 !bpf_is_throw_kfunc(insn))\n+\t\t\t\tcontinue;\n+\t\t\tenv-\u003einsn_aux_data[j].cleanup_pad = rec-\u003elanding_pad_off + 1;\n+\t\t}\n+\t}\n+}\n+\n+int bpf_prepare_cleanup_exceptions(struct bpf_verifier_env *env)\n+{\n+\tif (!env-\u003ecleanup_info_cnt)\n+\t\treturn 0;\n+\n+\tif (bpf_prog_is_offloaded(env-\u003eprog-\u003eaux)) {\n+\t\tverbose(env,\n+\t\t\t\"exception cleanup is not supported for offloaded programs\\n\");\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tif (!bpf_jit_supports_cleanup_pads() || !env-\u003eprog-\u003ejit_requested) {\n+\t\tverbose(env,\n+\t\t\t\"exception cleanup needs a JIT that can dispatch landing pads\\n\");\n+\t\treturn -EOPNOTSUPP;\n+\t}\n+\tenv-\u003eprog-\u003ejit_required = 1;\n+\n+\tif (env-\u003eexception_callback_subprog) {\n+\t\tverbose(env,\n+\t\t\t\"exception cleanup table cannot be combined with an exception callback\\n\");\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tcleanup_mark_throw_sites(env);\n+\tcleanup_mark_call_sites(env);\n+\treturn 0;\n+}\n+\n+int bpf_check_cleanup_exceptions(struct bpf_verifier_env *env)\n+{\n+\tu32 len = env-\u003eprog-\u003elen;\n+\tstruct cleanup_ctx ctx = { .env = env };\n+\tconst struct cleanup_alloc_req tab[] = {\n+\t\t{ (void **)\u0026ctx.reach, len, sizeof(*ctx.reach) },\n+\t\t{ (void **)\u0026ctx.stack, len, sizeof(*ctx.stack) },\n+\t};\n+\tint ret;\n+\n+\tif (!env-\u003ecleanup_info_cnt)\n+\t\treturn 0;\n+\n+\tctx.scratch = cleanup_alloc(tab, ARRAY_SIZE(tab));\n+\tif (!ctx.scratch)\n+\t\treturn -ENOMEM;\n+\n+\tret = cleanup_reachability(\u0026ctx);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\tret = cleanup_mark_pad_bodies(\u0026ctx);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\tret = cleanup_check_resumes(\u0026ctx);\n+out:\n+\tkvfree(ctx.scratch);\n+\treturn ret;\n+}\n+\n+bool bpf_is_unwind_resume_kfunc(const struct bpf_insn *insn)\n+{\n+\treturn insn_is_exc_kfunc(insn, EXC_KF_bpf_unwind_resume);\n+}\n+\n+int bpf_cleanup_pad_of_call(struct bpf_verifier_env *env, u32 idx)\n+{\n+\tu32 pad = env-\u003einsn_aux_data[idx].cleanup_pad;\n+\n+\treturn pad ? (int)pad - 1 : -1;\n+}\n+\n+/*\n+ * Every subprogram of a cleanup-carrying program spills the BPF callee-saved\n+ * registers, even one that never throws: a frame's spill holds its caller's\n+ * registers, and that is what the walker restores before running the caller's\n+ * pad. The exception callback does not, because it reuses the boundary frame\n+ * rather than building one of its own.\n+ */\n+bool bpf_cleanup_force_spill(const struct bpf_prog *prog)\n+{\n+\treturn prog-\u003eaux-\u003eexc \u0026\u0026 !prog-\u003eaux-\u003eexception_cb;\n+}\n+\n+const struct bpf_cleanup_range *bpf_cleanup_pad_for_ip(const struct bpf_prog *prog, u64 ip)\n+{\n+\tconst struct bpf_exception_info *exc = prog-\u003eaux-\u003eexc;\n+\tu32 l = 0, r = exc ? exc-\u003enr_ranges : 0;\n+\n+\twhile (l \u003c r) {\n+\t\tu32 m = l + (r - l) / 2;\n+\t\tconst struct bpf_cleanup_range *rec = \u0026exc-\u003eranges[m];\n+\n+\t\tif (ip \u003c= rec-\u003ebegin)\n+\t\t\tr = m;\n+\t\telse if (ip \u003e rec-\u003eend)\n+\t\t\tl = m + 1;\n+\t\telse\n+\t\t\treturn rec;\n+\t}\n+\treturn NULL;\n+}\n+\n+static int cmp_u32(const void *a, const void *b)\n+{\n+\tu32 x = *(const u32 *)a, y = *(const u32 *)b;\n+\n+\treturn x \u003c y ? -1 : x \u003e y;\n+}\n+\n+int bpf_cleanup_alloc_info(struct bpf_prog_aux *aux)\n+{\n+\tif (aux-\u003eexc)\n+\t\treturn 0;\n+\taux-\u003eexc = kzalloc_obj(struct bpf_exception_info, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);\n+\treturn aux-\u003eexc ? 0 : -ENOMEM;\n+}\n+\n+int bpf_cleanup_attach_info(struct bpf_prog_aux *aux, struct bpf_cleanup_info *recs, u32 cnt)\n+{\n+\tstruct bpf_exception_info *exc = aux-\u003eexc;\n+\tstruct bpf_cleanup_range *ranges;\n+\tu32 i, n_at, *at;\n+\n+\tif (!cnt) {\n+\t\tkvfree(recs);\n+\t\treturn 0;\n+\t}\n+\n+\tranges = kvcalloc(cnt, sizeof(*ranges), GFP_KERNEL_ACCOUNT | __GFP_NOWARN);\n+\tif (!ranges) {\n+\t\tkvfree(recs);\n+\t\treturn -ENOMEM;\n+\t}\n+\n+\t/* The pads on their own, sorted and deduplicated. */\n+\tat = kvmalloc_array(cnt, sizeof(*at), GFP_KERNEL_ACCOUNT | __GFP_NOWARN);\n+\tif (!at) {\n+\t\tkvfree(ranges);\n+\t\tkvfree(recs);\n+\t\treturn -ENOMEM;\n+\t}\n+\tfor (i = 0; i \u003c cnt; i++)\n+\t\tat[i] = recs[i].landing_pad_off;\n+\tsort(at, cnt, sizeof(*at), cmp_u32, NULL);\n+\tfor (i = 0, n_at = 0; i \u003c cnt; i++)\n+\t\tif (!n_at || at[n_at - 1] != at[i])\n+\t\t\tat[n_at++] = at[i];\n+\n+\texc-\u003epad_at = at;\n+\texc-\u003enr_pad_at = n_at;\n+\texc-\u003einfo = recs;\n+\texc-\u003enr_info = cnt;\n+\texc-\u003eranges = ranges;\n+\t/* Withheld until the JIT has filled the table in. */\n+\texc-\u003enr_ranges = 0;\n+\treturn 0;\n+}\n+\n+void bpf_cleanup_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image)\n+{\n+\tstruct bpf_exception_info *exc = prog-\u003eaux-\u003eexc;\n+\tu32 i, n;\n+\n+\tif (!exc || !exc-\u003enr_info || !exc-\u003eranges)\n+\t\treturn;\n+\n+\tn = exc-\u003enr_info;\n+\tfor (i = 0; i \u003c n; i++) {\n+\t\tconst struct bpf_cleanup_info *rec = \u0026exc-\u003einfo[i];\n+\n+\t\tif (WARN_ON_ONCE(rec-\u003ebegin_off \u003e= prog-\u003elen ||\n+\t\t\t\t rec-\u003eend_off \u003e prog-\u003elen ||\n+\t\t\t\t rec-\u003elanding_pad_off \u003e= prog-\u003elen))\n+\t\t\treturn;\n+\t\texc-\u003eranges[i].begin = (u64)(long)image + addrs[rec-\u003ebegin_off];\n+\t\texc-\u003eranges[i].end = (u64)(long)image + addrs[rec-\u003eend_off];\n+\t\texc-\u003eranges[i].pad = (u64)(long)image + addrs[rec-\u003elanding_pad_off];\n+\t}\n+\texc-\u003enr_ranges = n;\n+}\n+\n+void bpf_cleanup_free_info(struct bpf_prog_aux *aux)\n+{\n+\tstruct bpf_exception_info *exc = aux-\u003eexc;\n+\n+\tif (!exc)\n+\t\treturn;\n+\tkvfree(exc-\u003eranges);\n+\tkvfree(exc-\u003einfo);\n+\tkvfree(exc-\u003epad_at);\n+\tkvfree(exc-\u003ethrow_at);\n+\tbitmap_free(exc-\u003epad_body);\n+\tkfree(exc);\n+\taux-\u003eexc = NULL;\n+}\n+\n+/* Is @idx in the sorted array @at of @n instruction indices? */\n+static bool insn_idx_in(const u32 *at, u32 n, u32 idx)\n+{\n+\tu32 l = 0, r = n;\n+\n+\twhile (l \u003c r) {\n+\t\tu32 m = l + (r - l) / 2;\n+\n+\t\tif (idx \u003c at[m])\n+\t\t\tr = m;\n+\t\telse if (idx \u003e at[m])\n+\t\t\tl = m + 1;\n+\t\telse\n+\t\t\treturn true;\n+\t}\n+\treturn false;\n+}\n+\n+bool bpf_cleanup_insn_is_pad(const struct bpf_prog *prog, u32 idx)\n+{\n+\tconst struct bpf_exception_info *exc = prog-\u003eaux-\u003eexc;\n+\n+\treturn exc \u0026\u0026 insn_idx_in(exc-\u003epad_at, exc-\u003enr_pad_at, idx);\n+}\n+\n+bool bpf_cleanup_insn_is_throw(const struct bpf_prog *prog, u32 idx)\n+{\n+\tconst struct bpf_exception_info *exc = prog-\u003eaux-\u003eexc;\n+\n+\treturn exc \u0026\u0026 insn_idx_in(exc-\u003ethrow_at, exc-\u003enr_throw_at, idx);\n+}\n+\n+bool bpf_cleanup_insn_in_pad(const struct bpf_prog *prog, u32 idx)\n+{\n+\tconst struct bpf_exception_info *exc = prog-\u003eaux-\u003eexc;\n+\n+\treturn exc \u0026\u0026 exc-\u003epad_body \u0026\u0026 idx \u003c exc-\u003enr_pad_body \u0026\u0026\n+\t test_bit(idx, exc-\u003epad_body);\n+}\ndiff --git a/kernel/bpf/exception.h b/kernel/bpf/exception.h\nnew file mode 100644\nindex 0000000000000..c0e68ce227c83\n--- /dev/null\n+++ b/kernel/bpf/exception.h\n@@ -0,0 +1,22 @@\n+/* SPDX-License-Identifier: GPL-2.0-only */\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+#ifndef _LINUX_BPF_EXCEPTION_H\n+#define _LINUX_BPF_EXCEPTION_H\n+\n+#include \u003clinux/types.h\u003e\n+\n+struct bpf_cleanup_info;\n+struct bpf_cleanup_range;\n+struct bpf_prog;\n+struct bpf_prog_aux;\n+struct bpf_verifier_env;\n+\n+int bpf_prepare_cleanup_exceptions(struct bpf_verifier_env *env);\n+int bpf_check_cleanup_exceptions(struct bpf_verifier_env *env);\n+int bpf_cleanup_check_callback(struct bpf_verifier_env *env, int subprog);\n+int bpf_cleanup_pad_of_call(struct bpf_verifier_env *env, u32 idx);\n+int bpf_cleanup_alloc_info(struct bpf_prog_aux *aux);\n+int bpf_cleanup_attach_info(struct bpf_prog_aux *aux, struct bpf_cleanup_info *recs, u32 cnt);\n+const struct bpf_cleanup_range *bpf_cleanup_pad_for_ip(const struct bpf_prog *prog, u64 ip);\n+\n+#endif /* _LINUX_BPF_EXCEPTION_H */\ndiff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c\nindex 2add8001c3ec3..134aafa6a6c9b 100644\n--- a/kernel/bpf/fixups.c\n+++ b/kernel/bpf/fixups.c\n@@ -1,5 +1,6 @@\n // SPDX-License-Identifier: GPL-2.0-only\n /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+#include \u003clinux/bitmap.h\u003e\n #include \u003clinux/bpf.h\u003e\n #include \u003clinux/btf.h\u003e\n #include \u003clinux/bpf_verifier.h\u003e\n@@ -10,6 +11,7 @@\n #include \u003clinux/perf_event.h\u003e\n #include \u003cnet/xdp.h\u003e\n #include \"disasm.h\"\n+#include \"exception.h\"\n \n #define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)\n \n@@ -252,15 +254,26 @@ static void adjust_insn_aux_data(struct bpf_verifier_env *env,\n \t\t/* Expand insni[off]'s seen count to the patched range. */\n \t\tdata[i].seen = old_seen;\n \t\tdata[i].zext_dst = bpf_insn_def32(new_prog, insn + i) \u003e= 0;\n+\t\tdata[i].in_cleanup_pad = data[off + cnt - 1].in_cleanup_pad;\n \t\tif (!memcmp(insn + i, original_insn, sizeof(struct bpf_insn))) {\n \t\t\tdata[i].non_stack_access =\n \t\t\t\tdata[off + cnt - 1].non_stack_access;\n \t\t\tdata[off + cnt - 1].non_stack_access = false;\n+\t\t\tdata[i].cleanup_throw_site =\n+\t\t\t\tdata[off + cnt - 1].cleanup_throw_site;\n+\t\t\tdata[off + cnt - 1].cleanup_throw_site = false;\n+\t\t\tdata[i].cleanup_pad = data[off + cnt - 1].cleanup_pad;\n+\t\t\tdata[off + cnt - 1].cleanup_pad = 0;\n \t\t} else if (bpf_is_mem_insn(insn + i)) {\n \t\t\tdata[i].non_stack_access = true;\n \t\t}\n \t}\n \n+\tif (env-\u003ecleanup_info_cnt)\n+\t\tfor (i = 0; i \u003c prog_len; i++)\n+\t\t\tif (data[i].cleanup_pad \u003e off + 1)\n+\t\t\t\tdata[i].cleanup_pad += cnt - 1;\n+\n \t/*\n \t * Last slot instruction could be a newly generated\n \t * BPF_ST/BPF_LDX/BPF_STX, systematically mark it for non-stack access\n@@ -549,6 +562,7 @@ static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)\n \tstruct bpf_insn_aux_data *aux_data = env-\u003einsn_aux_data;\n \tunsigned int orig_prog_len = env-\u003eprog-\u003elen;\n \tint err;\n+\tu32 i;\n \n \tif (bpf_prog_is_offloaded(env-\u003eprog-\u003eaux))\n \t\tbpf_prog_offload_remove_insns(env, off, cnt);\n@@ -573,6 +587,17 @@ static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)\n \t\tsizeof(*aux_data) * (orig_prog_len - off - cnt));\n \tenv-\u003einsn_aux_data_len -= cnt;\n \n+\tif (env-\u003ecleanup_info_cnt) {\n+\t\tfor (i = 0; i \u003c env-\u003einsn_aux_data_len; i++) {\n+\t\t\tu32 pad = aux_data[i].cleanup_pad;\n+\n+\t\t\tif (pad \u003e off + cnt)\n+\t\t\t\taux_data[i].cleanup_pad = pad - cnt;\n+\t\t\telse if (pad \u003e off)\n+\t\t\t\taux_data[i].cleanup_pad = 0;\n+\t\t}\n+\t}\n+\n \treturn 0;\n }\n \n@@ -1095,6 +1120,116 @@ static void bpf_restore_subprog_starts(struct bpf_verifier_env *env, u32 *orig_s\n \tenv-\u003esubprog_info[env-\u003esubprog_cnt].start = env-\u003eprog-\u003elen;\n }\n \n+static int cleanup_throw_sites_for_subprog(struct bpf_verifier_env *env, struct bpf_prog *sub,\n+\t\t\t\t\t u32 start, u32 end)\n+{\n+\tu32 i, cnt = 0, *at;\n+\n+\tfor (i = start; i \u003c end; i++)\n+\t\tif (env-\u003einsn_aux_data[i].cleanup_throw_site)\n+\t\t\tcnt++;\n+\tif (!cnt)\n+\t\treturn 0;\n+\n+\tat = kvmalloc_array(cnt, sizeof(*at), GFP_KERNEL_ACCOUNT | __GFP_NOWARN);\n+\tif (!at)\n+\t\treturn -ENOMEM;\n+\n+\tfor (i = start, cnt = 0; i \u003c end; i++) {\n+\t\tif (!env-\u003einsn_aux_data[i].cleanup_throw_site)\n+\t\t\tcontinue;\n+\t\tat[cnt++] = i - start;\n+\t}\n+\n+\tsub-\u003eaux-\u003eexc-\u003ethrow_at = at;\n+\tsub-\u003eaux-\u003eexc-\u003enr_throw_at = cnt;\n+\treturn 0;\n+}\n+\n+static int cleanup_pad_body_for_subprog(struct bpf_verifier_env *env, struct bpf_prog *sub,\n+\t\t\t\t\tu32 start, u32 end)\n+{\n+\tunsigned long *bits;\n+\tu32 i, cnt = 0;\n+\n+\tfor (i = start; i \u003c end; i++)\n+\t\tif (env-\u003einsn_aux_data[i].in_cleanup_pad)\n+\t\t\tcnt++;\n+\tif (!cnt)\n+\t\treturn 0;\n+\n+\tbits = bitmap_zalloc(end - start, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);\n+\tif (!bits)\n+\t\treturn -ENOMEM;\n+\n+\tfor (i = start; i \u003c end; i++)\n+\t\tif (env-\u003einsn_aux_data[i].in_cleanup_pad)\n+\t\t\t__set_bit(i - start, bits);\n+\n+\tsub-\u003eaux-\u003eexc-\u003epad_body = bits;\n+\tsub-\u003eaux-\u003eexc-\u003enr_pad_body = end - start;\n+\treturn 0;\n+}\n+\n+static int cleanup_info_for_subprog(struct bpf_verifier_env *env, struct bpf_prog *sub,\n+\t\t\t\t u32 start, u32 end)\n+{\n+\tstruct bpf_cleanup_info *recs;\n+\tu32 i, cnt = 0;\n+\tint err;\n+\n+\tif (!env-\u003ecleanup_info_cnt)\n+\t\treturn 0;\n+\n+\terr = bpf_cleanup_alloc_info(sub-\u003eaux);\n+\tif (err)\n+\t\treturn err;\n+\n+\terr = cleanup_throw_sites_for_subprog(env, sub, start, end);\n+\tif (err)\n+\t\treturn err;\n+\n+\terr = cleanup_pad_body_for_subprog(env, sub, start, end);\n+\tif (err)\n+\t\treturn err;\n+\n+\tfor (i = start; i \u003c end; i++)\n+\t\tif (env-\u003einsn_aux_data[i].cleanup_pad)\n+\t\t\tcnt++;\n+\tif (!cnt)\n+\t\treturn 0;\n+\n+\trecs = kvmalloc_array(cnt, sizeof(*recs), GFP_KERNEL_ACCOUNT | __GFP_NOWARN);\n+\tif (!recs)\n+\t\treturn -ENOMEM;\n+\n+\tfor (i = start, cnt = 0; i \u003c end; i++) {\n+\t\tu32 pad = env-\u003einsn_aux_data[i].cleanup_pad;\n+\n+\t\tif (!pad)\n+\t\t\tcontinue;\n+\t\tpad--;\n+\t\tif (verifier_bug_if(pad \u003c start || pad \u003e= end, env,\n+\t\t\t\t \"insn %u is covered by a landing pad at %u outside its subprog [%u, %u)\",\n+\t\t\t\t i, pad, start, end)) {\n+\t\t\tkvfree(recs);\n+\t\t\treturn -EFAULT;\n+\t\t}\n+\t\trecs[cnt].begin_off = i - start;\n+\t\trecs[cnt].end_off = i - start + 1;\n+\t\trecs[cnt].landing_pad_off = pad - start;\n+\t\tcnt++;\n+\t}\n+\treturn bpf_cleanup_attach_info(sub-\u003eaux, recs, cnt);\n+}\n+\n+int bpf_cleanup_attach_main_prog(struct bpf_verifier_env *env, struct bpf_prog *prog)\n+{\n+\tif (!env || env-\u003esubprog_cnt \u003e 1)\n+\t\treturn 0;\n+\treturn cleanup_info_for_subprog(env, prog, 0, prog-\u003elen);\n+}\n+\n static int jit_subprogs(struct bpf_verifier_env *env)\n {\n \tstruct bpf_prog *prog = env-\u003eprog, **func, *tmp;\n@@ -1232,6 +1367,10 @@ static int jit_subprogs(struct bpf_verifier_env *env)\n \t\tfunc[i]-\u003eaux-\u003etoken = prog-\u003eaux-\u003etoken;\n \t\tif (!i)\n \t\t\tfunc[i]-\u003eaux-\u003eexception_boundary = env-\u003eseen_exception;\n+\t\terr = cleanup_info_for_subprog(env, func[i], subprog_start,\n+\t\t\t\t\t env-\u003esubprog_info[i + 1].start);\n+\t\tif (err)\n+\t\t\tgoto out_free;\n \t\tfunc[i] = bpf_int_jit_compile(env, func[i]);\n \t\tif (!func[i]-\u003ejited) {\n \t\t\terr = -ENOTSUPP;\n@@ -1336,6 +1475,8 @@ static int jit_subprogs(struct bpf_verifier_env *env)\n \tprog-\u003eaux-\u003ebpf_exception_cb = (void *)func[env-\u003eexception_callback_subprog]-\u003ebpf_func;\n \tprog-\u003eaux-\u003eexception_boundary = func[0]-\u003eaux-\u003eexception_boundary;\n \tprog-\u003eaux-\u003estack_arg_sp_adjust = func[0]-\u003eaux-\u003estack_arg_sp_adjust;\n+\tprog-\u003eaux-\u003eexc = func[0]-\u003eaux-\u003eexc;\n+\tfunc[0]-\u003eaux-\u003eexc = NULL;\n \tbpf_prog_jit_attempt_done(prog);\n \treturn 0;\n out_free:\n@@ -1916,6 +2057,8 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)\n \t\t\tgoto next_insn;\n \t\tif (insn-\u003esrc_reg == BPF_PSEUDO_CALL)\n \t\t\tgoto next_insn;\n+\t\tif (bpf_is_unwind_resume_kfunc(insn))\n+\t\t\tgoto next_insn;\n \t\tif (insn-\u003esrc_reg == BPF_PSEUDO_KFUNC_CALL) {\n \t\t\tret = bpf_fixup_kfunc_call(env, insn, insn_buf, i + delta, \u0026cnt);\n \t\t\tif (ret)\ndiff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c\nindex 051b6654e57c6..ffef72804fc9a 100644\n--- a/kernel/bpf/helpers.c\n+++ b/kernel/bpf/helpers.c\n@@ -31,6 +31,7 @@\n #include \u003clinux/buildid.h\u003e\n \n #include \"../../lib/kstrtox.h\"\n+#include \"exception.h\"\n \n /* If kernel subsystem is allowing eBPF programs to call this function,\n * inside its own verifier_ops-\u003eget_func_proto() callback it should return\n@@ -3360,8 +3361,36 @@ struct bpf_throw_ctx {\n \tu64 sp;\n \tu64 bp;\n \tint cnt;\n+\tconst struct bpf_prog *callee;\n+\tu64 callee_fp;\n };\n \n+static void bpf_run_cleanup_pad(struct bpf_throw_ctx *ctx, const struct bpf_prog *prog,\n+\t\t\t\tu64 ip, u64 fp)\n+{\n+\tconst struct bpf_exception_info *exc = prog-\u003eaux-\u003eexc;\n+\tconst struct bpf_cleanup_range *rec;\n+\tu64 spill_base;\n+\n+\tif (!exc || !exc-\u003enr_ranges)\n+\t\treturn;\n+\trec = bpf_cleanup_pad_for_ip(prog, ip);\n+\tif (!rec)\n+\t\treturn;\n+\n+\t/*\n+\t * The callee is always another subprogram of this program -- the walk\n+\t * ends at any frame that is not one -- so its prologue spilled these\n+\t * registers and its exc is there to say where.\n+\t */\n+\tif (ctx-\u003ecallee)\n+\t\tspill_base = ctx-\u003ecallee_fp + ctx-\u003ecallee-\u003eaux-\u003eexc-\u003espill_off;\n+\telse\n+\t\tspill_base = fp + exc-\u003ethrow_spill_off;\n+\n+\tarch_bpf_run_cleanup_pad(rec-\u003epad, fp, spill_base);\n+}\n+\n static bool bpf_stack_walker(void *cookie, u64 ip, u64 sp, u64 bp)\n {\n \tstruct bpf_throw_ctx *ctx = cookie;\n@@ -3378,6 +3407,11 @@ static bool bpf_stack_walker(void *cookie, u64 ip, u64 sp, u64 bp)\n \tif (!prog)\n \t\treturn !ctx-\u003ecnt;\n \tctx-\u003ecnt++;\n+\n+\tbpf_run_cleanup_pad(ctx, prog, ip, bp);\n+\tctx-\u003ecallee = prog;\n+\tctx-\u003ecallee_fp = bp;\n+\n \tif (bpf_is_subprog(prog))\n \t\treturn true;\n \tctx-\u003eaux = prog-\u003eaux;\n@@ -3407,6 +3441,17 @@ __bpf_kfunc void bpf_throw(u64 cookie)\n \tWARN(1, \"A call to BPF exception callback should never return\\n\");\n }\n \n+/*\n+ * Terminator of a compiler-emitted cleanup landing pad. The compiler names\n+ * this _Unwind_Resume, the base unwind ABI's entry point for carrying an\n+ * unwind on once a frame's cleanups have run. To match kernel kfunc\n+ * convention, the kernel calls it bpf_unwind_resume and libbpf maps the\n+ * compiler's name onto it.\n+ */\n+__bpf_kfunc void bpf_unwind_resume(void)\n+{\n+}\n+\n __bpf_kfunc int bpf_wq_init(struct bpf_wq *wq, void *p__const_map, unsigned int flags)\n {\n \tstruct bpf_async_kern *async = (struct bpf_async_kern *)wq;\n@@ -4853,6 +4898,7 @@ BTF_ID_FLAGS(func, bpf_task_get_cgroup1, KF_ACQUIRE | KF_RCU | KF_RET_NULL)\n BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL)\n BTF_ID_FLAGS(func, bpf_task_from_vpid, KF_ACQUIRE | KF_RET_NULL)\n BTF_ID_FLAGS(func, bpf_throw)\n+BTF_ID_FLAGS(func, bpf_unwind_resume)\n #ifdef CONFIG_BPF_EVENTS\n BTF_ID_FLAGS(func, bpf_send_signal_task)\n #endif\ndiff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c\nindex 44ecdc5b4ec2d..9cfd05f970bc9 100644\n--- a/kernel/bpf/liveness.c\n+++ b/kernel/bpf/liveness.c\n@@ -8,6 +8,8 @@\n #include \u003clinux/slab.h\u003e\n #include \u003clinux/sort.h\u003e\n \n+#include \"exception.h\"\n+\n #define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)\n \n struct per_frame_masks {\n@@ -256,6 +258,9 @@ bpf_insn_successors(struct bpf_verifier_env *env, u32 idx)\n \tsucc = env-\u003esucc;\n \tsucc-\u003ecnt = 0;\n \n+\tif (unlikely(bpf_is_unwind_resume_kfunc(insn)))\n+\t\treturn succ;\n+\n \topcode_info = \u0026opcode_info_tbl[BPF_CLASS(insn-\u003ecode) | BPF_OP(insn-\u003ecode)];\n \tinsn_sz = bpf_is_ldimm64(insn) ? 2 : 1;\n \tif (opcode_info-\u003ecan_fallthrough)\n@@ -264,6 +269,13 @@ bpf_insn_successors(struct bpf_verifier_env *env, u32 idx)\n \tif (opcode_info-\u003ecan_jump)\n \t\tsucc-\u003eitems[succ-\u003ecnt++] = idx + bpf_jmp_offset(insn) + 1;\n \n+\tif (unlikely(env-\u003ecleanup_info_cnt)) {\n+\t\tint pad = bpf_cleanup_pad_of_call(env, idx);\n+\n+\t\tif (pad \u003e= 0)\n+\t\t\tsucc-\u003eitems[succ-\u003ecnt++] = pad;\n+\t}\n+\n \treturn succ;\n }\n \n@@ -397,6 +409,14 @@ bool bpf_stack_slot_alive(struct bpf_verifier_env *env, u32 frameno, u32 half_sp\n \t\talive = bpf_calls_callback(env, callsite)\n \t\t\t? is_live_before(instance, callsite, rel, half_spi)\n \t\t\t: is_live_before(instance, callsite + 1, rel, half_spi);\n+\n+\t\t/* Control may also go to the landing pad. */\n+\t\tif (!alive \u0026\u0026 unlikely(env-\u003ecleanup_info_cnt)) {\n+\t\t\tint pad = bpf_cleanup_pad_of_call(env, callsite);\n+\n+\t\t\tif (pad \u003e= 0)\n+\t\t\t\talive = is_live_before(instance, pad, rel, half_spi);\n+\t\t}\n \t\tif (alive)\n \t\t\treturn true;\n \t}\ndiff --git a/kernel/bpf/states.c b/kernel/bpf/states.c\nindex 66fb11b6c6a76..be0f529f7eccb 100644\n--- a/kernel/bpf/states.c\n+++ b/kernel/bpf/states.c\n@@ -996,6 +996,9 @@ static bool states_equal(struct bpf_verifier_env *env,\n \tif (old-\u003ein_sleepable != cur-\u003ein_sleepable)\n \t\treturn false;\n \n+\tif (old-\u003eunwinding != cur-\u003eunwinding)\n+\t\treturn false;\n+\n \tif (!refsafe(old, cur, \u0026env-\u003eidmap_scratch))\n \t\treturn false;\n \ndiff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c\nindex def57bddb0924..ac70914697662 100644\n--- a/kernel/bpf/syscall.c\n+++ b/kernel/bpf/syscall.c\n@@ -2912,7 +2912,7 @@ int __init __used bpf_multi_func(void) { return 0; }\n BTF_ID_LIST_GLOBAL_SINGLE(bpf_multi_func_btf_id, func, bpf_multi_func)\n \n /* last field in 'union bpf_attr' used by this command */\n-#define BPF_PROG_LOAD_LAST_FIELD keyring_id\n+#define BPF_PROG_LOAD_LAST_FIELD cleanup_info_cnt\n \n static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_attr *attr_log)\n {\ndiff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c\nindex 6c6b8d8520cdf..43ecf79baa4a9 100644\n--- a/kernel/bpf/verifier.c\n+++ b/kernel/bpf/verifier.c\n@@ -10,6 +10,7 @@\n #include \u003clinux/slab.h\u003e\n #include \u003clinux/bpf.h\u003e\n #include \u003clinux/btf.h\u003e\n+#include \u003clinux/bpf_cleanup_abi.h\u003e\n #include \u003clinux/bpf_verifier.h\u003e\n #include \u003clinux/filter.h\u003e\n #include \u003cnet/netlink.h\u003e\n@@ -37,6 +38,7 @@\n \n #include \"diagnostics.h\"\n #include \"disasm.h\"\n+#include \"exception.h\"\n \n static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {\n #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \\\n@@ -1714,6 +1716,7 @@ int bpf_copy_verifier_state(struct bpf_verifier_state *dst_state,\n \t\treturn err;\n \tdst_state-\u003especulative = src-\u003especulative;\n \tdst_state-\u003ein_sleepable = src-\u003ein_sleepable;\n+\tdst_state-\u003eunwinding = src-\u003eunwinding;\n \tdst_state-\u003ecurframe = src-\u003ecurframe;\n \tdst_state-\u003ebranches = src-\u003ebranches;\n \tdst_state-\u003eparent = src-\u003eparent;\n@@ -5588,6 +5591,14 @@ static int check_max_stack_depth(struct bpf_verifier_env *env)\n \t\t}\n \t}\n \n+\t/*\n+\t * A pad rebuilds its frame from a spill area, and on x86-64 a private\n+\t * stack's frame pointer is in no spill area. Refused on every arch\n+\t * rather than just that one.\n+\t */\n+\tif (env-\u003ecleanup_info_cnt)\n+\t\tpriv_stack_mode = NO_PRIV_STACK;\n+\n \tif (priv_stack_mode == PRIV_STACK_UNKNOWN)\n \t\tpriv_stack_mode = bpf_enable_priv_stack(env-\u003eprog);\n \n@@ -10484,6 +10495,10 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins\n \t * callbacks\n \t */\n \tenv-\u003esubprog_info[subprog].is_cb = true;\n+\terr = bpf_cleanup_check_callback(env, subprog);\n+\tif (err)\n+\t\treturn err;\n+\n \tif (bpf_pseudo_kfunc_call(insn) \u0026\u0026\n \t !is_callback_calling_kfunc(insn-\u003eimm)) {\n \t\tverifier_bug(env, \"kfunc %s#%d not marked as callback-calling\",\n@@ -10535,8 +10550,8 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins\n \treturn 0;\n }\n \n-static int process_bpf_exit_full(struct bpf_verifier_env *env,\n-\t\t\t\t bool *do_print_state, bool exception_exit);\n+static int process_bpf_exit_full(struct bpf_verifier_env *env, bool *do_print_state);\n+static int unwind_step(struct bpf_verifier_env *env, u32 callsite, int *insn_idx);\n \n static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t\t\t int *insn_idx)\n@@ -10626,7 +10641,7 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t\t\t\tverbose(env, \"failed to push state for global subprog exception path\\n\");\n \t\t\t\treturn PTR_ERR(branch);\n \t\t\t}\n-\t\t\treturn process_bpf_exit_full(env, NULL, true);\n+\t\t\treturn unwind_step(env, *insn_idx, insn_idx);\n \t\t}\n \n \t\t/* continue with next insn after call */\n@@ -14506,7 +14521,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t\tenv-\u003eprog-\u003ecall_session_cookie = true;\n \n \tif (bpf_is_throw_kfunc(insn))\n-\t\treturn process_bpf_exit_full(env, NULL, true);\n+\t\treturn unwind_step(env, insn_idx, \u0026env-\u003einsn_idx);\n \n \treturn 0;\n }\n@@ -18431,9 +18446,75 @@ enum {\n \tINSN_IDX_UPDATED = 2,\n };\n \n-static int process_bpf_exit_full(struct bpf_verifier_env *env,\n-\t\t\t\t bool *do_print_state,\n-\t\t\t\t bool exception_exit)\n+static u32 unwind_pop_frame(struct bpf_verifier_env *env)\n+{\n+\tstruct bpf_verifier_state *state = env-\u003ecur_state;\n+\tstruct bpf_func_state *callee = state-\u003eframe[state-\u003ecurframe];\n+\tu32 callsite = callee-\u003ecallsite;\n+\tstruct bpf_func_state *caller;\n+\n+\tcaller = state-\u003eframe[state-\u003ecurframe - 1];\n+\taccount_processed_insns(env, callee, caller);\n+\tfree_func_state(callee);\n+\tstate-\u003eframe[state-\u003ecurframe--] = NULL;\n+\tinvalidate_outgoing_stack_args(env, caller);\n+\treturn callsite;\n+}\n+\n+static void unwind_enter_pad(struct bpf_verifier_env *env)\n+{\n+\tstruct bpf_func_state *frame = cur_func(env);\n+\n+\tclear_caller_saved_regs(env, frame-\u003eregs);\n+\tmark_reg_unknown(env, frame-\u003eregs, BPF_REG_0);\n+\t__mark_reg_known(\u0026frame-\u003eregs[BPF_REG_0], BPF_PAD_ENTRY_R0);\n+}\n+\n+static int unwind_finish(struct bpf_verifier_env *env)\n+{\n+\tint err = check_resource_leak(env, true, true, \"bpf_throw\");\n+\n+\tif (err)\n+\t\treturn err;\n+\treturn PROCESS_BPF_EXIT;\n+}\n+\n+static int unwind_step(struct bpf_verifier_env *env, u32 callsite, int *insn_idx)\n+{\n+\tstruct bpf_verifier_state *state = env-\u003ecur_state;\n+\n+\tstate-\u003eunwinding = true;\n+\tfor (;;) {\n+\t\tint pad = bpf_cleanup_pad_of_call(env, callsite);\n+\n+\t\tif (pad \u003e= 0) {\n+\t\t\tunwind_enter_pad(env);\n+\t\t\t*insn_idx = pad;\n+\t\t\treturn INSN_IDX_UPDATED;\n+\t\t}\n+\t\tif (!state-\u003ecurframe)\n+\t\t\treturn unwind_finish(env);\n+\t\tcallsite = unwind_pop_frame(env);\n+\t}\n+}\n+\n+static int process_cleanup_resume(struct bpf_verifier_env *env, int *insn_idx)\n+{\n+\tstruct bpf_verifier_state *state = env-\u003ecur_state;\n+\n+\t/* A pad entered by ordinary control flow. */\n+\tif (!state-\u003eunwinding) {\n+\t\tverbose(env,\n+\t\t\t\"bpf_unwind_resume() at insn %d reached without an exception in flight\\n\",\n+\t\t\t*insn_idx);\n+\t\treturn -EINVAL;\n+\t}\n+\tif (!state-\u003ecurframe)\n+\t\treturn unwind_finish(env);\n+\treturn unwind_step(env, unwind_pop_frame(env), insn_idx);\n+}\n+\n+static int process_bpf_exit_full(struct bpf_verifier_env *env, bool *do_print_state)\n {\n \tstruct bpf_func_state *cur_frame = cur_func(env);\n \n@@ -18443,25 +18524,11 @@ static int process_bpf_exit_full(struct bpf_verifier_env *env,\n \t * for which reference_state must match caller reference\n \t * state when it exits.\n \t */\n-\tint err = check_resource_leak(env, exception_exit,\n-\t\t\t\t exception_exit || !env-\u003ecur_state-\u003ecurframe,\n-\t\t\t\t exception_exit ? \"bpf_throw\" :\n+\tint err = check_resource_leak(env, false, !env-\u003ecur_state-\u003ecurframe,\n \t\t\t\t \"BPF_EXIT instruction in main prog\");\n \tif (err)\n \t\treturn err;\n \n-\t/* The side effect of the prepare_func_exit which is\n-\t * being skipped is that it frees bpf_func_state.\n-\t * Typically, process_bpf_exit will only be hit with\n-\t * outermost exit. copy_verifier_state in pop_stack will\n-\t * handle freeing of any extra bpf_func_state left over\n-\t * from not processing all nested function exits. We\n-\t * also skip return code checks as they are not needed\n-\t * for exceptional exits.\n-\t */\n-\tif (exception_exit)\n-\t\treturn PROCESS_BPF_EXIT;\n-\n \tif (env-\u003ecur_state-\u003ecurframe) {\n \t\t/* exit from nested function */\n \t\terr = prepare_func_exit(env, \u0026env-\u003einsn_idx);\n@@ -18635,6 +18702,8 @@ static int do_check_insn(struct bpf_verifier_env *env, bool *do_print_state)\n \n \t\tenv-\u003ejmps_processed++;\n \t\tif (opcode == BPF_CALL) {\n+\t\t\tif (bpf_is_unwind_resume_kfunc(insn))\n+\t\t\t\treturn process_cleanup_resume(env, \u0026env-\u003einsn_idx);\n \t\t\tif (env-\u003ecur_state-\u003eactive_locks) {\n \t\t\t\tif ((insn-\u003esrc_reg == BPF_REG_0 \u0026\u0026\n \t\t\t\t insn-\u003eimm != BPF_FUNC_spin_unlock \u0026\u0026\n@@ -18668,7 +18737,7 @@ static int do_check_insn(struct bpf_verifier_env *env, bool *do_print_state)\n \t\t\t\tenv-\u003einsn_idx += insn-\u003eimm + 1;\n \t\t\treturn INSN_IDX_UPDATED;\n \t\t} else if (opcode == BPF_EXIT) {\n-\t\t\treturn process_bpf_exit_full(env, do_print_state, false);\n+\t\t\treturn process_bpf_exit_full(env, do_print_state);\n \t\t}\n \t\treturn check_cond_jmp_op(env, insn, \u0026env-\u003einsn_idx);\n \t}\n@@ -21638,6 +21707,11 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n \tif (ret \u003c 0)\n \t\tgoto skip_full_check;\n \n+\t/* The CFG needs an edge from a call in a cleanup range to its pad. */\n+\tret = bpf_prepare_cleanup_exceptions(env);\n+\tif (ret \u003c 0)\n+\t\tgoto skip_full_check;\n+\n \t/* Validate instructions and resolve the program's referenced resources. */\n \tret = check_and_resolve_insns(env);\n \tif (ret \u003c 0)\n@@ -21658,6 +21732,10 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n \tif (ret \u003c 0)\n \t\tgoto skip_full_check;\n \n+\tret = bpf_check_cleanup_exceptions(env);\n+\tif (ret \u003c 0)\n+\t\tgoto skip_full_check;\n+\n \tret = bpf_compute_postorder(env);\n \tif (ret \u003c 0)\n \t\tgoto skip_full_check;\n@@ -21845,6 +21923,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n \tkvfree(env-\u003esucc);\n \tkvfree(env-\u003egotox_tmp_buf);\n \tbpf_diag_free(env);\n+\tkvfree(env-\u003ecleanup_info);\n \tkvfree(env);\n \treturn ret;\n }\ndiff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h\nindex 732b35cc08d1c..f7dc121be0947 100644\n--- a/tools/include/uapi/linux/bpf.h\n+++ b/tools/include/uapi/linux/bpf.h\n@@ -1669,6 +1669,9 @@ union bpf_attr {\n \t\t * verification.\n \t\t */\n \t\t__s32\t\tkeyring_id;\n+\t\t__aligned_u64\tcleanup_info;\t/* exception cleanup table */\n+\t\t__u32\t\tcleanup_info_rec_size; /* userspace bpf_cleanup_info size */\n+\t\t__u32\t\tcleanup_info_cnt; /* number of bpf_cleanup_info records */\n \t};\n \n \tstruct { /* anonymous struct used by BPF_OBJ_* commands */\n@@ -7588,6 +7591,12 @@ struct bpf_line_info {\n \t__u32\tline_col;\n };\n \n+struct bpf_cleanup_info {\n+\t__u32\tbegin_off;\n+\t__u32\tend_off;\n+\t__u32\tlanding_pad_off;\n+};\n+\n struct bpf_spin_lock {\n \t__u32\tval;\n };\ndiff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c\nindex 96819c082c772..bcf490570961c 100644\n--- a/tools/lib/bpf/bpf.c\n+++ b/tools/lib/bpf/bpf.c\n@@ -295,7 +295,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type,\n \t\t const struct bpf_insn *insns, size_t insn_cnt,\n \t\t struct bpf_prog_load_opts *opts)\n {\n-\tconst size_t attr_sz = offsetofend(union bpf_attr, keyring_id);\n+\tconst size_t attr_sz = offsetofend(union bpf_attr, cleanup_info_cnt);\n \tvoid *finfo = NULL, *linfo = NULL;\n \tconst char *func_info, *line_info;\n \t__u32 log_size, log_level, attach_prog_fd, attach_btf_obj_fd;\n@@ -370,6 +370,10 @@ int bpf_prog_load(enum bpf_prog_type prog_type,\n \tattr.fd_array = ptr_to_u64(OPTS_GET(opts, fd_array, NULL));\n \tattr.fd_array_cnt = OPTS_GET(opts, fd_array_cnt, 0);\n \n+\tattr.cleanup_info = ptr_to_u64(OPTS_GET(opts, cleanup_info, NULL));\n+\tattr.cleanup_info_cnt = OPTS_GET(opts, cleanup_info_cnt, 0);\n+\tattr.cleanup_info_rec_size = OPTS_GET(opts, cleanup_info_rec_size, 0);\n+\n \tif (log_level) {\n \t\tattr.log_buf = ptr_to_u64(log_buf);\n \t\tattr.log_size = log_size;\ndiff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h\nindex 7534a593edae2..6f62a99e1e3e5 100644\n--- a/tools/lib/bpf/bpf.h\n+++ b/tools/lib/bpf/bpf.h\n@@ -128,9 +128,14 @@ struct bpf_prog_load_opts {\n \n \t/* if set, provides the length of fd_array */\n \t__u32 fd_array_cnt;\n+\n+\t/* exception cleanup table, from the .bpf_cleanup section */\n+\tconst void *cleanup_info;\n+\t__u32 cleanup_info_cnt;\n+\t__u32 cleanup_info_rec_size;\n \tsize_t :0;\n };\n-#define bpf_prog_load_opts__last_field fd_array_cnt\n+#define bpf_prog_load_opts__last_field cleanup_info_rec_size\n \n LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type,\n \t\t\t const char *prog_name, const char *license,\ndiff --git a/tools/lib/bpf/gen_loader.c b/tools/lib/bpf/gen_loader.c\nindex af3a04f161ac1..e9bb282a557a3 100644\n--- a/tools/lib/bpf/gen_loader.c\n+++ b/tools/lib/bpf/gen_loader.c\n@@ -981,13 +981,15 @@ static void cleanup_relos(struct bpf_gen *gen, int insns)\n \tcleanup_core_relo(gen);\n }\n \n-/* Convert func, line, and core relo info blobs to target endianness */\n+/* Convert func, line, core relo and cleanup info blobs to target endianness */\n static void info_blob_bswap(struct bpf_gen *gen, int func_info, int line_info,\n-\t\t\t int core_relos, struct bpf_prog_load_opts *load_attr)\n+\t\t\t int core_relos, int cleanup_info,\n+\t\t\t struct bpf_prog_load_opts *load_attr)\n {\n \tstruct bpf_func_info *fi = gen-\u003edata_start + func_info;\n \tstruct bpf_line_info *li = gen-\u003edata_start + line_info;\n \tstruct bpf_core_relo *cr = gen-\u003edata_start + core_relos;\n+\tstruct bpf_cleanup_info *ci = gen-\u003edata_start + cleanup_info;\n \tint i;\n \n \tfor (i = 0; i \u003c load_attr-\u003efunc_info_cnt; i++)\n@@ -998,6 +1000,9 @@ static void info_blob_bswap(struct bpf_gen *gen, int func_info, int line_info,\n \n \tfor (i = 0; i \u003c gen-\u003ecore_relo_cnt; i++)\n \t\tbpf_core_relo_bswap(cr++);\n+\n+\tfor (i = 0; i \u003c load_attr-\u003ecleanup_info_cnt; i++)\n+\t\tbpf_cleanup_info_bswap(ci++);\n }\n \n void bpf_gen__prog_load(struct bpf_gen *gen,\n@@ -1011,8 +1016,11 @@ void bpf_gen__prog_load(struct bpf_gen *gen,\n \t\t\t load_attr-\u003eline_info_rec_size;\n \tint core_relo_tot_sz = gen-\u003ecore_relo_cnt *\n \t\t\t sizeof(struct bpf_core_relo);\n+\tint cleanup_info_tot_sz = load_attr-\u003ecleanup_info_cnt *\n+\t\t\t\t load_attr-\u003ecleanup_info_rec_size;\n \tint prog_load_attr, license_off, insns_off, func_info, line_info, core_relos;\n-\tint attr_size = offsetofend(union bpf_attr, core_relo_rec_size);\n+\tint attr_size = offsetofend(union bpf_attr, cleanup_info_cnt);\n+\tint cleanup_info;\n \tunion bpf_attr attr;\n \n \tmemset(\u0026attr, 0, attr_size);\n@@ -1061,9 +1069,17 @@ void bpf_gen__prog_load(struct bpf_gen *gen,\n \t\t core_relos, gen-\u003ecore_relo_cnt,\n \t\t sizeof(struct bpf_core_relo));\n \n+\tattr.cleanup_info_rec_size = tgt_endian(load_attr-\u003ecleanup_info_rec_size);\n+\tattr.cleanup_info_cnt = tgt_endian(load_attr-\u003ecleanup_info_cnt);\n+\tcleanup_info = add_data(gen, load_attr-\u003ecleanup_info, cleanup_info_tot_sz);\n+\tpr_debug(\"gen: prog_load: cleanup_info: off %d cnt %u rec size %u\\n\",\n+\t\t cleanup_info, load_attr-\u003ecleanup_info_cnt,\n+\t\t load_attr-\u003ecleanup_info_rec_size);\n+\n \t/* convert all info blobs to target endianness */\n \tif (gen-\u003eswapped_endian \u0026\u0026 !gen-\u003eerror)\n-\t\tinfo_blob_bswap(gen, func_info, line_info, core_relos, load_attr);\n+\t\tinfo_blob_bswap(gen, func_info, line_info, core_relos, cleanup_info,\n+\t\t\t\tload_attr);\n \n \tlibbpf_strlcpy(attr.prog_name, prog_name, sizeof(attr.prog_name));\n \tprog_load_attr = add_data(gen, \u0026attr, attr_size);\n@@ -1085,6 +1101,9 @@ void bpf_gen__prog_load(struct bpf_gen *gen,\n \t/* populate union bpf_attr with a pointer to core_relos */\n \temit_rel_store(gen, attr_field(prog_load_attr, core_relos), core_relos);\n \n+\t/* populate union bpf_attr with a pointer to cleanup_info */\n+\temit_rel_store(gen, attr_field(prog_load_attr, cleanup_info), cleanup_info);\n+\n \t/* populate union bpf_attr fd_array with a pointer to data where map_fds are saved */\n \temit_rel_store(gen, attr_field(prog_load_attr, fd_array), gen-\u003efd_array);\n \ndiff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c\nindex 613afae265199..ebb21c9cdfe08 100644\n--- a/tools/lib/bpf/libbpf.c\n+++ b/tools/lib/bpf/libbpf.c\n@@ -514,6 +514,11 @@ struct bpf_program {\n \tvoid *line_info;\n \t__u32 line_info_rec_size;\n \t__u32 line_info_cnt;\n+\n+\tstruct bpf_cleanup_info *cleanup_info;\n+\t__u32 cleanup_info_rec_size;\n+\t__u32 cleanup_info_cnt;\n+\n \t__u32 prog_flags;\n \t__u8 hash[SHA256_DIGEST_LENGTH];\n \n@@ -549,6 +554,7 @@ struct bpf_struct_ops {\n #define STRUCT_OPS_SEC \".struct_ops\"\n #define STRUCT_OPS_LINK_SEC \".struct_ops.link\"\n #define ARENA_SEC \".addr_space.1\"\n+#define CLEANUP_SEC \".bpf_cleanup\"\n \n enum libbpf_map_type {\n \tLIBBPF_MAP_UNSPEC,\n@@ -677,6 +683,25 @@ struct elf_sec_desc {\n \tElf_Data *data;\n };\n \n+#define CLEANUP_REC_FIELDS\t(sizeof(struct bpf_cleanup_info) / sizeof(__u32))\n+\n+/* Index of each field of struct bpf_cleanup_info, read as an array of __u32. */\n+enum {\n+\tCLEANUP_REC_BEGIN,\n+\tCLEANUP_REC_END,\n+\tCLEANUP_REC_PAD,\n+};\n+\n+/* One (begin, end, landing_pad) triple from .bpf_cleanup, with each field\n+ * resolved from its relocation to an ELF section plus a section-relative\n+ * instruction index. The mapping to final program instruction indices can only\n+ * happen after subprogram placement, which differs per main program.\n+ */\n+struct cleanup_raw_rec {\n+\tint sec_idx[CLEANUP_REC_FIELDS];\n+\tsize_t insn_idx[CLEANUP_REC_FIELDS];\n+};\n+\n struct elf_state {\n \tint fd;\n \tconst void *obj_buf;\n@@ -696,6 +721,8 @@ struct elf_state {\n \tbool has_st_ops;\n \tint arena_data_shndx;\n \tint jumptables_data_shndx;\n+\tElf_Data *cleanup_data;\n+\tint cleanup_shndx;\n };\n \n struct usdt_manager;\n@@ -771,6 +798,9 @@ struct bpf_object {\n \tvoid *jumptables_data;\n \tsize_t jumptables_data_sz;\n \n+\tstruct cleanup_raw_rec *cleanup_recs;\n+\tsize_t cleanup_rec_cnt;\n+\n \tstruct {\n \t\tstruct bpf_program *prog;\n \t\tunsigned int sym_off;\n@@ -817,7 +847,10 @@ static void bpf_program__exit(struct bpf_program *prog)\n \tzfree(\u0026prog-\u003esec_name);\n \tzfree(\u0026prog-\u003einsns);\n \tzfree(\u0026prog-\u003ereloc_desc);\n+\tzfree(\u0026prog-\u003ecleanup_info);\n \n+\tprog-\u003ecleanup_info_rec_size = 0;\n+\tprog-\u003ecleanup_info_cnt = 0;\n \tprog-\u003enr_reloc = 0;\n \tprog-\u003einsns_cnt = 0;\n \tprog-\u003esec_idx = -1;\n@@ -1554,6 +1587,7 @@ static struct bpf_object *bpf_object__new(const char *path,\n \tobj-\u003eefile.obj_buf = obj_buf;\n \tobj-\u003eefile.obj_buf_sz = obj_buf_sz;\n \tobj-\u003eefile.btf_maps_shndx = -1;\n+\tobj-\u003eefile.cleanup_shndx = -1;\n \tobj-\u003ekconfig_map_idx = -1;\n \tobj-\u003earena_map_idx = -1;\n \n@@ -4040,6 +4074,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj)\n \t\t\t\tsec_desc-\u003eshdr = sh;\n \t\t\t\tsec_desc-\u003edata = data;\n \t\t\t\tobj-\u003eefile.has_st_ops = true;\n+\t\t\t} else if (strcmp(name, CLEANUP_SEC) == 0) {\n+\t\t\t\tobj-\u003eefile.cleanup_data = data;\n+\t\t\t\tobj-\u003eefile.cleanup_shndx = idx;\n \t\t\t} else if (strcmp(name, ARENA_SEC) == 0) {\n \t\t\t\tobj-\u003eefile.arena_data = data;\n \t\t\t\tobj-\u003eefile.arena_data_shndx = idx;\n@@ -4067,6 +4104,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)\n \t\t\t strcmp(name, \".rel\" STRUCT_OPS_LINK_SEC) \u0026\u0026\n \t\t\t strcmp(name, \".rel?\" STRUCT_OPS_SEC) \u0026\u0026\n \t\t\t strcmp(name, \".rel?\" STRUCT_OPS_LINK_SEC) \u0026\u0026\n+\t\t\t strcmp(name, \".rel\" CLEANUP_SEC) \u0026\u0026\n \t\t\t strcmp(name, \".rel\" MAPS_ELF_SEC)) {\n \t\t\t\tpr_info(\"elf: skipping relo section(%d) %s for section(%d) %s\\n\",\n \t\t\t\t\tidx, name, targ_sec_idx,\n@@ -4847,6 +4885,214 @@ static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj,\n \treturn NULL;\n }\n \n+static int bpf_object__init_cleanup_info(struct bpf_object *obj)\n+{\n+\tElf_Data *data = obj-\u003eefile.cleanup_data;\n+\tElf_Data *relo = NULL;\n+\tsize_t i, nrels, nslots, nrecs;\n+\tstruct cleanup_raw_rec *recs;\n+\tint *slot_sec, ret = 0;\n+\tsize_t *slot_val;\n+\tconst __u32 *vals;\n+\tbool native;\n+\n+\tif (!data || obj-\u003eefile.cleanup_shndx \u003c 0)\n+\t\treturn 0;\n+\n+\tnative = is_native_endianness(obj);\n+\n+\tfor (i = 0; i \u003c obj-\u003eefile.sec_cnt; i++) {\n+\t\tstruct elf_sec_desc *sd = \u0026obj-\u003eefile.secs[i];\n+\n+\t\tif (sd-\u003esec_type == SEC_RELO \u0026\u0026 sd-\u003eshdr \u0026\u0026\n+\t\t sd-\u003eshdr-\u003esh_info == (Elf64_Word)obj-\u003eefile.cleanup_shndx) {\n+\t\t\trelo = sd-\u003edata;\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\tif (!relo) {\n+\t\tpr_warn(\"%s present without relocations\\n\", CLEANUP_SEC);\n+\t\treturn -LIBBPF_ERRNO__FORMAT;\n+\t}\n+\tif (data-\u003ed_size % sizeof(struct bpf_cleanup_info)) {\n+\t\tpr_warn(\"%s size %zu is not a multiple of the record size %zu\\n\",\n+\t\t\tCLEANUP_SEC, data-\u003ed_size, sizeof(struct bpf_cleanup_info));\n+\t\treturn -LIBBPF_ERRNO__FORMAT;\n+\t}\n+\n+\tvals = data-\u003ed_buf;\n+\tnslots = data-\u003ed_size / sizeof(__u32);\n+\tnrecs = data-\u003ed_size / sizeof(struct bpf_cleanup_info);\n+\n+\tslot_sec = calloc(nslots, sizeof(*slot_sec));\n+\tslot_val = calloc(nslots, sizeof(*slot_val));\n+\trecs = calloc(nrecs ?: 1, sizeof(*recs));\n+\tif (!slot_sec || !slot_val || !recs) {\n+\t\tret = -ENOMEM;\n+\t\tgoto out;\n+\t}\n+\tfor (i = 0; i \u003c nslots; i++)\n+\t\tslot_sec[i] = -1;\n+\n+\t/* One relocation per 4-byte field, naming the section it points into. */\n+\tnrels = relo-\u003ed_size / sizeof(Elf64_Rel);\n+\tfor (i = 0; i \u003c nrels; i++) {\n+\t\tElf64_Rel *rel = elf_rel_by_idx(relo, i);\n+\t\tElf64_Sym *sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel-\u003er_info));\n+\t\tsize_t type = ELF64_R_TYPE(rel-\u003er_info);\n+\t\tsize_t slot = rel-\u003er_offset / sizeof(__u32);\n+\n+\t\tif (type != R_BPF_64_NODYLD32 \u0026\u0026 type != R_BPF_64_ABS32) {\n+\t\t\tpr_warn(\"%s: relocation %zu has unexpected type %zu\\n\",\n+\t\t\t\tCLEANUP_SEC, i, type);\n+\t\t\tret = -LIBBPF_ERRNO__FORMAT;\n+\t\t\tgoto out;\n+\t\t}\n+\t\tif (!sym || slot \u003e= nslots || rel-\u003er_offset % sizeof(__u32)) {\n+\t\t\tpr_warn(\"%s: bad relocation %zu\\n\", CLEANUP_SEC, i);\n+\t\t\tret = -LIBBPF_ERRNO__FORMAT;\n+\t\t\tgoto out;\n+\t\t}\n+\t\tslot_sec[slot] = sym-\u003est_shndx;\n+\t\t/* The addend lives in the section data, which libelf leaves in\n+\t\t * the object's byte order; a non-section symbol additionally\n+\t\t * contributes its own value.\n+\t\t */\n+\t\tslot_val[slot] = (native ? vals[slot] : bswap_32(vals[slot])) +\n+\t\t\t\t sym-\u003est_value;\n+\t}\n+\n+\tfor (i = 0; i \u003c nslots; i++) {\n+\t\tstruct cleanup_raw_rec *rec = \u0026recs[i / CLEANUP_REC_FIELDS];\n+\t\tsize_t field = i % CLEANUP_REC_FIELDS;\n+\n+\t\tif (slot_sec[i] \u003c 0) {\n+\t\t\tpr_warn(\"%s: field %zu has no relocation\\n\", CLEANUP_SEC, i);\n+\t\t\tret = -LIBBPF_ERRNO__FORMAT;\n+\t\t\tgoto out;\n+\t\t}\n+\t\tif (slot_val[i] % BPF_INSN_SZ) {\n+\t\t\tpr_warn(\"%s: field %zu offset %zu is not instruction aligned\\n\",\n+\t\t\t\tCLEANUP_SEC, i, slot_val[i]);\n+\t\t\tret = -LIBBPF_ERRNO__FORMAT;\n+\t\t\tgoto out;\n+\t\t}\n+\t\trec-\u003esec_idx[field] = slot_sec[i];\n+\t\trec-\u003einsn_idx[field] = slot_val[i] / BPF_INSN_SZ;\n+\t}\n+\n+\tobj-\u003ecleanup_recs = recs;\n+\tobj-\u003ecleanup_rec_cnt = nrecs;\n+\trecs = NULL;\n+out:\n+\tfree(recs);\n+\tfree(slot_val);\n+\tfree(slot_sec);\n+\treturn ret;\n+}\n+\n+static int cmp_cleanup_info(const void *a, const void *b)\n+{\n+\tconst struct bpf_cleanup_info *x = a, *y = b;\n+\n+\tif (x-\u003ebegin_off == y-\u003ebegin_off)\n+\t\treturn 0;\n+\treturn x-\u003ebegin_off \u003c y-\u003ebegin_off ? -1 : 1;\n+}\n+\n+static int bpf_prog_collect_cleanup_info(struct bpf_object *obj,\n+\t\t\t\t\t struct bpf_program *prog)\n+{\n+\tsize_t i;\n+\tint j;\n+\n+\tfor (i = 0; i \u003c obj-\u003ecleanup_rec_cnt; i++) {\n+\t\tstruct cleanup_raw_rec *raw = \u0026obj-\u003ecleanup_recs[i];\n+\t\tstruct bpf_program *owner = NULL;\n+\t\tstruct bpf_cleanup_info ci = {};\n+\t\t__u32 *fields = (__u32 *)\u0026ci;\n+\t\tvoid *tmp;\n+\n+\t\tfor (j = 0; j \u003c CLEANUP_REC_FIELDS; j++) {\n+\t\t\tsize_t idx = raw-\u003einsn_idx[j], final;\n+\t\t\tstruct bpf_program *p;\n+\n+\t\t\t/* The end of a range is exclusive, so it may name the\n+\t\t\t * instruction just past the last one of a function,\n+\t\t\t * which belongs to the next function or to nothing at\n+\t\t\t * all. Ask about the last instruction the range covers,\n+\t\t\t * the way the kernel does.\n+\t\t\t */\n+\t\t\tif (j == CLEANUP_REC_END) {\n+\t\t\t\tif (!idx) {\n+\t\t\t\t\tpr_warn(\"%s: record %zu is an empty range\\n\",\n+\t\t\t\t\t\tCLEANUP_SEC, i);\n+\t\t\t\t\treturn -LIBBPF_ERRNO__FORMAT;\n+\t\t\t\t}\n+\t\t\t\tidx--;\n+\t\t\t}\n+\n+\t\t\tp = find_prog_by_sec_insn(obj, raw-\u003esec_idx[j], idx);\n+\t\t\tif (!p) {\n+\t\t\t\tpr_warn(\"%s: record %zu field %d is not inside a function\\n\",\n+\t\t\t\t\tCLEANUP_SEC, i, j);\n+\t\t\t\treturn -LIBBPF_ERRNO__FORMAT;\n+\t\t\t}\n+\t\t\tif (!owner) {\n+\t\t\t\towner = p;\n+\t\t\t} else if (owner != p) {\n+\t\t\t\tpr_warn(\"%s: record %zu spans functions '%s' and '%s'\\n\",\n+\t\t\t\t\tCLEANUP_SEC, i, owner-\u003ename, p-\u003ename);\n+\t\t\t\treturn -LIBBPF_ERRNO__FORMAT;\n+\t\t\t}\n+\n+\t\t\tif (owner == prog) {\n+\t\t\t\tfinal = raw-\u003einsn_idx[j] - prog-\u003esec_insn_off;\n+\t\t\t} else if (prog_is_subprog(obj, owner) \u0026\u0026 owner-\u003esub_insn_off) {\n+\t\t\t\t/* sub_insn_off is where this subprogram was\n+\t\t\t\t * appended to the main program being relocated;\n+\t\t\t\t * zero means it is not part of it.\n+\t\t\t\t */\n+\t\t\t\tfinal = owner-\u003esub_insn_off +\n+\t\t\t\t\traw-\u003einsn_idx[j] - owner-\u003esec_insn_off;\n+\t\t\t} else {\n+\t\t\t\towner = NULL;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t\tfields[j] = final;\n+\t\t}\n+\t\tif (!owner)\n+\t\t\tcontinue;\n+\n+\t\ttmp = libbpf_reallocarray(prog-\u003ecleanup_info, prog-\u003ecleanup_info_cnt + 1,\n+\t\t\t\t\t sizeof(*prog-\u003ecleanup_info));\n+\t\tif (!tmp)\n+\t\t\treturn -ENOMEM;\n+\t\tprog-\u003ecleanup_info = tmp;\n+\t\tprog-\u003ecleanup_info_rec_size = sizeof(struct bpf_cleanup_info);\n+\t\tprog-\u003ecleanup_info[prog-\u003ecleanup_info_cnt++] = ci;\n+\n+\t\tpr_debug(\"prog '%s': cleanup region [%u,%u) -\u003e landing pad %u\\n\",\n+\t\t\t prog-\u003ename, ci.begin_off, ci.end_off, ci.landing_pad_off);\n+\t}\n+\n+\tqsort(prog-\u003ecleanup_info, prog-\u003ecleanup_info_cnt,\n+\t sizeof(*prog-\u003ecleanup_info), cmp_cleanup_info);\n+\tfor (i = 1; i \u003c prog-\u003ecleanup_info_cnt; i++) {\n+\t\tstruct bpf_cleanup_info *prev = \u0026prog-\u003ecleanup_info[i - 1];\n+\t\tstruct bpf_cleanup_info *cur = \u0026prog-\u003ecleanup_info[i];\n+\n+\t\tif (cur-\u003ebegin_off \u003c prev-\u003eend_off) {\n+\t\t\tpr_warn(\"prog '%s': overlapping cleanup regions [%u,%u) and [%u,%u)\\n\",\n+\t\t\t\tprog-\u003ename, prev-\u003ebegin_off, prev-\u003eend_off,\n+\t\t\t\tcur-\u003ebegin_off, cur-\u003eend_off);\n+\t\t\treturn -LIBBPF_ERRNO__FORMAT;\n+\t\t}\n+\t}\n+\n+\treturn 0;\n+}\n+\n static int\n bpf_object__collect_prog_relos(struct bpf_object *obj, Elf64_Shdr *shdr, Elf_Data *data)\n {\n@@ -7556,6 +7802,13 @@ static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_pat\n \t\t\t\t\treturn err;\n \t\t\t}\n \t\t}\n+\n+\t\terr = bpf_prog_collect_cleanup_info(obj, prog);\n+\t\tif (err) {\n+\t\t\tpr_warn(\"prog '%s': failed to collect cleanup info: %s\\n\",\n+\t\t\t\tprog-\u003ename, errstr(err));\n+\t\t\treturn err;\n+\t\t}\n \t}\n \tfor (i = 0; i \u003c obj-\u003enr_programs; i++) {\n \t\tprog = \u0026obj-\u003eprograms[i];\n@@ -7746,6 +7999,9 @@ static int bpf_object__collect_relos(struct bpf_object *obj)\n \t\t\treturn -LIBBPF_ERRNO__INTERNAL;\n \t\t}\n \n+\t\tif (idx == obj-\u003eefile.cleanup_shndx)\n+\t\t\tcontinue;\n+\n \t\tif (obj-\u003eefile.secs[idx].sec_type == SEC_ST_OPS)\n \t\t\terr = bpf_object__collect_st_ops_relos(obj, shdr, data);\n \t\telse if (idx == obj-\u003eefile.btf_maps_shndx)\n@@ -8018,6 +8274,11 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog\n \t\tload_attr.line_info_rec_size = prog-\u003eline_info_rec_size;\n \t\tload_attr.line_info_cnt = prog-\u003eline_info_cnt;\n \t}\n+\tif (prog-\u003ecleanup_info_cnt) {\n+\t\tload_attr.cleanup_info = prog-\u003ecleanup_info;\n+\t\tload_attr.cleanup_info_cnt = prog-\u003ecleanup_info_cnt;\n+\t\tload_attr.cleanup_info_rec_size = prog-\u003ecleanup_info_rec_size;\n+\t}\n \tload_attr.log_level = log_level;\n \tload_attr.prog_flags = prog-\u003eprog_flags;\n \tload_attr.fd_array = obj-\u003efd_array;\n@@ -8346,6 +8607,15 @@ static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_s\n \t}\n }\n \n+/* LLVM terminates a cleanup landing pad with a call to _Unwind_Resume, the\n+ * base unwind ABI's entry point for carrying an unwind on once a frame's\n+ * cleanups have run. The kernel knows it as bpf_unwind_resume.\n+ */\n+static const char *kern_extern_name(const char *name)\n+{\n+\treturn strcmp(name, \"_Unwind_Resume\") ? name : \"bpf_unwind_resume\";\n+}\n+\n static int bpf_program_record_relos(struct bpf_program *prog)\n {\n \tstruct bpf_object *obj = prog-\u003eobj;\n@@ -8362,12 +8632,12 @@ static int bpf_program_record_relos(struct bpf_program *prog)\n \t\t\t\tcontinue;\n \t\t\tkind = btf_is_var(btf__type_by_id(obj-\u003ebtf, ext-\u003ebtf_id)) ?\n \t\t\t\tBTF_KIND_VAR : BTF_KIND_FUNC;\n-\t\t\tbpf_gen__record_extern(obj-\u003egen_loader, ext-\u003ename,\n+\t\t\tbpf_gen__record_extern(obj-\u003egen_loader, kern_extern_name(ext-\u003ename),\n \t\t\t\t\t ext-\u003eis_weak, !ext-\u003eksym.type_id,\n \t\t\t\t\t true, kind, relo-\u003einsn_idx);\n \t\t\tbreak;\n \t\tcase RELO_EXTERN_CALL:\n-\t\t\tbpf_gen__record_extern(obj-\u003egen_loader, ext-\u003ename,\n+\t\t\tbpf_gen__record_extern(obj-\u003egen_loader, kern_extern_name(ext-\u003ename),\n \t\t\t\t\t ext-\u003eis_weak, false, false, BTF_KIND_FUNC,\n \t\t\t\t\t relo-\u003einsn_idx);\n \t\t\tbreak;\n@@ -8565,6 +8835,7 @@ static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf,\n \terr = err ? : bpf_object__init_maps(obj, opts);\n \terr = err ? : bpf_object_init_progs(obj, opts);\n \terr = err ? : bpf_object__collect_relos(obj);\n+\terr = err ? : bpf_object__init_cleanup_info(obj);\n \tif (err)\n \t\tgoto out;\n \n@@ -8807,17 +9078,19 @@ static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,\n \tstruct module_btf *mod_btf = NULL;\n \tconst struct btf_type *kern_func;\n \tstruct btf *kern_btf = NULL;\n+\tconst char *kern_name;\n \tint ret;\n \n \tlocal_func_proto_id = ext-\u003eksym.type_id;\n \n-\tkfunc_id = find_ksym_btf_id(obj, ext-\u003eessent_name ?: ext-\u003ename, BTF_KIND_FUNC, \u0026kern_btf,\n-\t\t\t\t \u0026mod_btf);\n+\tkern_name = kern_extern_name(ext-\u003eessent_name ?: ext-\u003ename);\n+\n+\tkfunc_id = find_ksym_btf_id(obj, kern_name, BTF_KIND_FUNC, \u0026kern_btf, \u0026mod_btf);\n \tif (kfunc_id \u003c 0) {\n \t\tif (kfunc_id == -ESRCH \u0026\u0026 ext-\u003eis_weak)\n \t\t\treturn 0;\n \t\tpr_warn(\"extern (func ksym) '%s': not found in kernel or module BTFs\\n\",\n-\t\t\text-\u003ename);\n+\t\t\tstrcmp(kern_name, \"bpf_unwind_resume\") ? ext-\u003ename : kern_name);\n \t\treturn kfunc_id;\n \t}\n \n@@ -9676,6 +9949,9 @@ void bpf_object__close(struct bpf_object *obj)\n \tzfree(\u0026obj-\u003ejumptables_data);\n \tobj-\u003ejumptables_data_sz = 0;\n \n+\tzfree(\u0026obj-\u003ecleanup_recs);\n+\tobj-\u003ecleanup_rec_cnt = 0;\n+\n \tfor (i = 0; i \u003c obj-\u003ejumptable_map_cnt; i++)\n \t\tclose(obj-\u003ejumptable_maps[i].fd);\n \tzfree(\u0026obj-\u003ejumptable_maps);\n@@ -10068,6 +10344,25 @@ int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_opts\n \t\tattr.line_info_rec_size = info ? info_rec_size : prog-\u003eline_info_rec_size;\n \t}\n \n+\t/* exception cleanup table */\n+\tinfo = OPTS_GET(opts, cleanup_info, NULL);\n+\tinfo_cnt = OPTS_GET(opts, cleanup_info_cnt, 0);\n+\tinfo_rec_size = OPTS_GET(opts, cleanup_info_rec_size, 0);\n+\tif (!!info != !!info_cnt || !!info != !!info_rec_size) {\n+\t\tpr_warn(\"prog '%s': cleanup_info, cleanup_info_cnt, and cleanup_info_rec_size must all be specified or all omitted\\n\",\n+\t\t\tprog-\u003ename);\n+\t\treturn libbpf_err(-EINVAL);\n+\t}\n+\tif (info) {\n+\t\tattr.cleanup_info = info;\n+\t\tattr.cleanup_info_cnt = info_cnt;\n+\t\tattr.cleanup_info_rec_size = info_rec_size;\n+\t} else if (prog-\u003ecleanup_info_cnt) {\n+\t\tattr.cleanup_info = prog-\u003ecleanup_info;\n+\t\tattr.cleanup_info_cnt = prog-\u003ecleanup_info_cnt;\n+\t\tattr.cleanup_info_rec_size = prog-\u003ecleanup_info_rec_size;\n+\t}\n+\n \t/* Logging is caller-controlled; no fallback to prog/obj log settings */\n \tattr.log_buf = OPTS_GET(opts, log_buf, NULL);\n \tattr.log_size = OPTS_GET(opts, log_size, 0);\ndiff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h\nindex cb4d962338441..78519f24fb404 100644\n--- a/tools/lib/bpf/libbpf_internal.h\n+++ b/tools/lib/bpf/libbpf_internal.h\n@@ -56,6 +56,9 @@\n #ifndef R_BPF_64_ABS32\n #define R_BPF_64_ABS32 3\n #endif\n+#ifndef R_BPF_64_NODYLD32\n+#define R_BPF_64_NODYLD32 4\n+#endif\n #ifndef R_BPF_64_32\n #define R_BPF_64_32 10\n #endif\n@@ -569,6 +572,13 @@ static inline void bpf_core_relo_bswap(struct bpf_core_relo *i)\n \ti-\u003ekind = bswap_32(i-\u003ekind);\n }\n \n+static inline void bpf_cleanup_info_bswap(struct bpf_cleanup_info *i)\n+{\n+\ti-\u003ebegin_off = bswap_32(i-\u003ebegin_off);\n+\ti-\u003eend_off = bswap_32(i-\u003eend_off);\n+\ti-\u003elanding_pad_off = bswap_32(i-\u003elanding_pad_off);\n+}\n+\n enum btf_field_iter_kind {\n \tBTF_FIELD_ITER_IDS,\n \tBTF_FIELD_ITER_STRS,\ndiff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c\nindex 78f92c39290af..e5c06023cb5b1 100644\n--- a/tools/lib/bpf/linker.c\n+++ b/tools/lib/bpf/linker.c\n@@ -1036,7 +1036,8 @@ static int linker_sanity_check_elf_relos(struct src_obj *obj, struct src_sec *se\n \t\tsize_t sym_type = ELF64_R_TYPE(relo-\u003er_info);\n \n \t\tif (sym_type != R_BPF_64_64 \u0026\u0026 sym_type != R_BPF_64_32 \u0026\u0026\n-\t\t sym_type != R_BPF_64_ABS64 \u0026\u0026 sym_type != R_BPF_64_ABS32) {\n+\t\t sym_type != R_BPF_64_ABS64 \u0026\u0026 sym_type != R_BPF_64_ABS32 \u0026\u0026\n+\t\t sym_type != R_BPF_64_NODYLD32) {\n \t\t\tpr_warn(\"ELF relo #%d in section #%zu has unexpected type %zu in %s\\n\",\n \t\t\t\ti, sec-\u003esec_idx, sym_type, obj-\u003efilename);\n \t\t\treturn -EINVAL;\n@@ -2274,6 +2275,22 @@ static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *ob\n \t\t\t\t\t\tinsn-\u003eimm += sec-\u003edst_off / sizeof(struct bpf_insn);\n \t\t\t\t\telse\n \t\t\t\t\t\tinsn-\u003eimm += sec-\u003edst_off;\n+\t\t\t\t} else if (sym_type == R_BPF_64_NODYLD32 ||\n+\t\t\t\t\t sym_type == R_BPF_64_ABS32) {\n+\t\t\t\t\t__u32 *val;\n+\n+\t\t\t\t\t/* Two spellings of the one thing: LLVM\n+\t\t\t\t\t * emits NODYLD32 for a .long against a\n+\t\t\t\t\t * section symbol, GNU as emits ABS32\n+\t\t\t\t\t * (bpf_reloc_type_lookup() maps\n+\t\t\t\t\t * BFD_RELOC_32 to it), and the value\n+\t\t\t\t\t * they describe is the same.\n+\t\t\t\t\t */\n+\t\t\t\t\tval = dst_linked_sec-\u003eraw_data + dst_rel-\u003er_offset;\n+\t\t\t\t\tif (linker-\u003eswapped_endian)\n+\t\t\t\t\t\t*val = bswap_32(bswap_32(*val) + sec-\u003edst_off);\n+\t\t\t\t\telse\n+\t\t\t\t\t\t*val += sec-\u003edst_off;\n \t\t\t\t} else {\n \t\t\t\t\tpr_warn(\"relocation against STT_SECTION in non-exec section is not supported!\\n\");\n \t\t\t\t\treturn -EINVAL;\ndiff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile\nindex 7ea5ba1df29e3..981cc26430495 100644\n--- a/tools/testing/selftests/bpf/Makefile\n+++ b/tools/testing/selftests/bpf/Makefile\n@@ -523,7 +523,7 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h\t\t\\\n LSKELS := fexit_sleep.c trace_printk.c trace_vprintk.c map_ptr_kern.c \t\\\n \tcore_kern.c core_kern_overflow.c test_ringbuf.c\t\t\t\\\n \ttest_ringbuf_n.c test_ringbuf_map_key.c test_ringbuf_write.c \\\n-\ttest_ringbuf_overwrite.c\n+\ttest_ringbuf_overwrite.c exceptions_cleanup_light.c\n \n LSKELS_SIGNED := fentry_test.c fexit_test.c atomics.c\n \ndiff --git a/tools/testing/selftests/bpf/exceptions_cleanup.h b/tools/testing/selftests/bpf/exceptions_cleanup.h\nnew file mode 100644\nindex 0000000000000..0c088d96ca013\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/exceptions_cleanup.h\n@@ -0,0 +1,52 @@\n+/* SPDX-License-Identifier: GPL-2.0 */\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+#ifndef __EXCEPTIONS_CLEANUP_H__\n+#define __EXCEPTIONS_CLEANUP_H__\n+\n+#define THROW_COOKIE\t\t0x100\n+#define INNER_COOKIE\t\t0x200\n+\n+/* progs/exceptions_cleanup.c: one bit per frame that reports it ran. */\n+#define RAN_FOO3_PREEMPT\t0x1\n+#define RAN_FOO2_RCU\t\t0x2\n+#define RAN_FOO1V_PREEMPT\t0x4\n+#define RAN_FOO2_DROP\t\t0x8\n+#define RAN_BUMP\t\t0x10\n+\n+/* progs/exceptions_cleanup_shapes.c: one bit per shape, numbered its own way. */\n+#define RAN_SWEEP\t\t0x1\n+#define RAN_SHARED\t\t0x2\n+#define RAN_REGS\t\t0x4\n+#define RAN_TAIL_CALL\t\t0x8\n+#define RAN_MAIN_PAD\t\t0x10\n+#define RAN_TC_TAKEN\t\t0x20\n+#define RAN_FREPLACE\t\t0x40\n+#define RAN_ADDR_TAKEN\t\t0x80\n+#define RAN_NO_SUBPROG\t\t0x100\n+#define RAN_PAD_CALLS\t\t0x200\n+#define RAN_PAD_FIRST\t\t0x400\n+#define RAN_WIDE_REC\t\t0x800\n+#define RAN_PAD_STACK\t\t0x1000\n+#define RAN_DEEP_PAD\t\t0x2000\n+#define RAN_NOUNWIND_REC\t0x4000\n+#define RAN_RESUME_ALIAS\t0x8000\n+#define RAN_PAD_TAIL_CALL\t0x10000\n+\n+/* progs/exceptions_cleanup_light.c: the one pad it has. */\n+#define RAN_LIGHT\t\t0x1\n+\n+#define CLEANUP_REC(begin, end, landing_pad)\t\t\t\\\n+\t\".pushsection .bpf_cleanup,\\\"a\\\",@progbits;\"\t\t\\\n+\t\".long \" begin \";\"\t\t\t\t\t\\\n+\t\".long \" end \";\"\t\t\t\t\t\\\n+\t\".long \" landing_pad \";\"\t\t\t\t\\\n+\t\".popsection;\"\n+\n+/* Set a bit in @pads_ran. */\n+#define PAD_RAN(bit)\t\t\t\t\t\t\\\n+\t\"r1 = %[pads_ran] ll;\"\t\t\t\t\t\\\n+\t\"r2 = *(u64 *)(r1 + 0);\"\t\t\t\t\\\n+\t\"r2 |= \" bit \";\"\t\t\t\t\t\\\n+\t\"*(u64 *)(r1 + 0) = r2;\"\n+\n+#endif /* __EXCEPTIONS_CLEANUP_H__ */\ndiff --git a/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c\nnew file mode 100644\nindex 0000000000000..ad0ff949f10df\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c\n@@ -0,0 +1,421 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+#include \u003ctest_progs.h\u003e\n+#include \"exceptions_cleanup.h\"\n+#include \"exceptions_cleanup.skel.h\"\n+#include \"exceptions_cleanup_fail.skel.h\"\n+#include \"exceptions_cleanup_shapes.skel.h\"\n+#include \"exceptions_cleanup_freplace.skel.h\"\n+#include \"exceptions_cleanup_pad_freplace.skel.h\"\n+#include \"exceptions_cleanup_ext_table.skel.h\"\n+#include \"exceptions_cleanup_light.lskel.h\"\n+\n+/* foo3 threw: every frame that has a pad ran it. */\n+#define PADS_FOO3_THREW \\\n+\t(RAN_FOO3_PREEMPT | RAN_FOO2_RCU | RAN_FOO1V_PREEMPT | RAN_FOO2_DROP)\n+\n+/* foo2 threw after foo3 returned normally: foo3's pad must not run. */\n+#define PADS_FOO2_THREW \\\n+\t(RAN_FOO2_RCU | RAN_FOO1V_PREEMPT | RAN_FOO2_DROP)\n+\n+static void run(struct exceptions_cleanup *skel, __u64 input, __u32 retval,\n+\t\t__u64 pads)\n+{\n+\t__u64 ctx = 0;\n+\tint err;\n+\n+\tLIBBPF_OPTS(bpf_test_run_opts, topts,\n+\t\t .ctx_in = \u0026ctx,\n+\t\t .ctx_size_in = sizeof(ctx),\n+\t);\n+\n+\tskel-\u003ebss-\u003einput = input;\n+\tskel-\u003ebss-\u003epads_ran = 0;\n+\tskel-\u003ebss-\u003eresult = 0;\n+\n+\terr = bpf_prog_test_run_opts(bpf_program__fd(skel-\u003eprogs.entry), \u0026topts);\n+\tif (!ASSERT_OK(err, \"run\"))\n+\t\treturn;\n+\tASSERT_EQ(topts.retval, retval, \"retval\");\n+\tASSERT_EQ(skel-\u003ebss-\u003epads_ran, pads | RAN_BUMP, \"pads_ran\");\n+}\n+\n+static void run_shape(struct exceptions_cleanup_shapes *skel, struct bpf_program *prog,\n+\t\t __u64 input, __u32 retval, __u64 pads)\n+{\n+\t__u64 ctx = 0;\n+\tint err;\n+\n+\tLIBBPF_OPTS(bpf_test_run_opts, topts,\n+\t\t .ctx_in = \u0026ctx,\n+\t\t .ctx_size_in = sizeof(ctx),\n+\t);\n+\n+\tskel-\u003ebss-\u003einput = input;\n+\tskel-\u003ebss-\u003epads_ran = 0;\n+\n+\terr = bpf_prog_test_run_opts(bpf_program__fd(prog), \u0026topts);\n+\tif (!ASSERT_OK(err, \"run\"))\n+\t\treturn;\n+\tASSERT_EQ(topts.retval, retval, \"retval\");\n+\tASSERT_EQ(skel-\u003ebss-\u003epads_ran, pads, \"pads_ran\");\n+}\n+\n+static void test_freplace(struct exceptions_cleanup_shapes *skel)\n+{\n+\tstruct exceptions_cleanup_freplace *fr;\n+\tstruct bpf_link *link;\n+\tint tgt_fd;\n+\n+\ttgt_fd = bpf_program__fd(skel-\u003eprogs.entry_freplace);\n+\n+\tfr = exceptions_cleanup_freplace__open();\n+\tif (!ASSERT_OK_PTR(fr, \"freplace open\"))\n+\t\treturn;\n+\n+\tif (!ASSERT_OK(bpf_program__set_attach_target(fr-\u003eprogs.new_fr_callee,\n+\t\t\t\t\t\t tgt_fd, \"fr_callee\"),\n+\t\t \"set_attach_target\"))\n+\t\tgoto out;\n+\tif (!ASSERT_OK(exceptions_cleanup_freplace__load(fr), \"freplace load\"))\n+\t\tgoto out;\n+\n+\tlink = bpf_program__attach_freplace(fr-\u003eprogs.new_fr_callee, tgt_fd,\n+\t\t\t\t\t \"fr_callee\");\n+\tif (!ASSERT_OK_PTR(link, \"attach_freplace\"))\n+\t\tgoto out;\n+\n+\trun_shape(skel, skel-\u003eprogs.entry_freplace, 101, THROW_COOKIE, 0);\n+\tbpf_link__destroy(link);\n+out:\n+\texceptions_cleanup_freplace__destroy(fr);\n+}\n+\n+static void test_pad_calls_freplace(struct exceptions_cleanup_shapes *skel)\n+{\n+\tstruct exceptions_cleanup_pad_freplace *fr;\n+\tstruct bpf_link *link;\n+\t__u64 ctx = 0;\n+\tint tgt_fd, err;\n+\n+\tLIBBPF_OPTS(bpf_test_run_opts, topts,\n+\t\t .ctx_in = \u0026ctx,\n+\t\t .ctx_size_in = sizeof(ctx),\n+\t);\n+\n+\ttgt_fd = bpf_program__fd(skel-\u003eprogs.entry_pad_calls);\n+\n+\tfr = exceptions_cleanup_pad_freplace__open();\n+\tif (!ASSERT_OK_PTR(fr, \"pad freplace open\"))\n+\t\treturn;\n+\n+\tif (!ASSERT_OK(bpf_program__set_attach_target(fr-\u003eprogs.new_pad_callee,\n+\t\t\t\t\t\t tgt_fd, \"pad_callee\"),\n+\t\t \"set_attach_target\"))\n+\t\tgoto out;\n+\tif (!ASSERT_OK(exceptions_cleanup_pad_freplace__load(fr), \"pad freplace load\"))\n+\t\tgoto out;\n+\n+\tlink = bpf_program__attach_freplace(fr-\u003eprogs.new_pad_callee, tgt_fd,\n+\t\t\t\t\t \"pad_callee\");\n+\tif (!ASSERT_OK_PTR(link, \"attach_freplace\"))\n+\t\tgoto out;\n+\n+\tskel-\u003ebss-\u003einput = 101;\n+\tskel-\u003ebss-\u003epads_ran = 0;\n+\tskel-\u003ebss-\u003epad_runs = 0;\n+\n+\terr = bpf_prog_test_run_opts(tgt_fd, \u0026topts);\n+\tif (!ASSERT_OK(err, \"run\"))\n+\t\tgoto out_link;\n+\n+\tASSERT_EQ(skel-\u003ebss-\u003epad_runs, 1, \"pad_runs\");\n+\tASSERT_EQ(skel-\u003ebss-\u003epads_ran, RAN_PAD_CALLS, \"pads_ran\");\n+\tASSERT_EQ(topts.retval, THROW_COOKIE, \"retval\");\n+out_link:\n+\tbpf_link__destroy(link);\n+out:\n+\texceptions_cleanup_pad_freplace__destroy(fr);\n+}\n+\n+static void test_ext_table(struct exceptions_cleanup_shapes *skel)\n+{\n+\tstruct exceptions_cleanup_ext_table *fr;\n+\tstruct bpf_link *link;\n+\tint tgt_fd;\n+\n+\ttgt_fd = bpf_program__fd(skel-\u003eprogs.entry_freplace);\n+\n+\tfr = exceptions_cleanup_ext_table__open();\n+\tif (!ASSERT_OK_PTR(fr, \"ext table open\"))\n+\t\treturn;\n+\n+\tif (!ASSERT_OK(bpf_program__set_attach_target(fr-\u003eprogs.new_fr_callee,\n+\t\t\t\t\t\t tgt_fd, \"fr_callee\"),\n+\t\t \"set_attach_target\"))\n+\t\tgoto out;\n+\tif (!ASSERT_OK(exceptions_cleanup_ext_table__load(fr), \"ext table load\"))\n+\t\tgoto out;\n+\n+\tlink = bpf_program__attach_freplace(fr-\u003eprogs.new_fr_callee, tgt_fd,\n+\t\t\t\t\t \"fr_callee\");\n+\tif (!ASSERT_OK_PTR(link, \"attach_freplace\"))\n+\t\tgoto out;\n+\n+\tfr-\u003ebss-\u003eext_pad_ran = 0;\n+\trun_shape(skel, skel-\u003eprogs.entry_freplace, 101, THROW_COOKIE, 0);\n+\tASSERT_EQ(fr-\u003ebss-\u003eext_pad_ran, 1, \"ext_pad_ran\");\n+\n+\tbpf_link__destroy(link);\n+out:\n+\texceptions_cleanup_ext_table__destroy(fr);\n+}\n+\n+static void test_light_skeleton(void)\n+{\n+\tstruct exceptions_cleanup_light_lskel *skel;\n+\t__u64 ctx = 0;\n+\tint err;\n+\n+\tLIBBPF_OPTS(bpf_test_run_opts, topts,\n+\t\t .ctx_in = \u0026ctx,\n+\t\t .ctx_size_in = sizeof(ctx),\n+\t);\n+\n+\tskel = exceptions_cleanup_light_lskel__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"light open_and_load\"))\n+\t\treturn;\n+\n+\terr = bpf_prog_test_run_opts(skel-\u003eprogs.entry_light.prog_fd, \u0026topts);\n+\tif (!ASSERT_OK(err, \"run\"))\n+\t\tgoto out;\n+\tASSERT_EQ(topts.retval, THROW_COOKIE, \"retval\");\n+\tASSERT_EQ(skel-\u003ebss-\u003epads_ran, RAN_LIGHT, \"pads_ran\");\n+out:\n+\texceptions_cleanup_light_lskel__destroy(skel);\n+}\n+\n+static void test_shapes(void)\n+{\n+\tstruct exceptions_cleanup_shapes *skel;\n+\n+\tskel = exceptions_cleanup_shapes__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"shapes open_and_load\"))\n+\t\treturn;\n+\n+\t/* The frame loads at all only if everything unreachable in it went. */\n+\tif (test__start_subtest(\"sweep_no_throw\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_sweep, 1, 0, 0);\n+\tif (test__start_subtest(\"sweep_throw\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_sweep, 101, THROW_COOKIE, RAN_SWEEP);\n+\n+\t/* The covered call unwinds to the pad; the uncovered one never does. */\n+\tif (test__start_subtest(\"shared_callee_no_throw\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_shared, 1, 2, 0);\n+\tif (test__start_subtest(\"shared_callee_throw\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_shared, 101, THROW_COOKIE, RAN_SHARED);\n+\n+\t/* The pad only sets its bit if it got the frame's own r6-r9 back. */\n+\tif (test__start_subtest(\"pad_sees_callee_saved\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_regs, 101, THROW_COOKIE, RAN_REGS);\n+\n+\t/* Same check, with a tail-call-reachable callee: its spill moves. */\n+\tif (test__start_subtest(\"tail_call_no_throw\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_tail_call, 1, 0, 0);\n+\tif (test__start_subtest(\"tail_call_throw\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_tail_call, 101, THROW_COOKIE,\n+\t\t\t RAN_TAIL_CALL);\n+\n+\t/* A region around a nounwind call: no pad dispatched, still loads. */\n+\tif (test__start_subtest(\"nounwind_region\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_nounwind_rec, 1, 0, 0);\n+\n+\t/* A pad in the main program's own frame, not in a subprogram. */\n+\tif (test__start_subtest(\"main_program_pad\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_main_pad, 101, THROW_COOKIE,\n+\t\t\t RAN_MAIN_PAD);\n+\n+\t/* The same call site either way: the subprogram's throw unwinds into\n+\t * this frame and runs its pad, an extension's stops at its own boundary.\n+\t */\n+\tif (test__start_subtest(\"freplace_subprog_throws\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_freplace, 7, THROW_COOKIE,\n+\t\t\t RAN_FREPLACE);\n+\tif (test__start_subtest(\"freplace_extension_throws\"))\n+\t\ttest_freplace(skel);\n+\n+\t/* A tail call that is taken: the walk ends at the target, so the cookie\n+\t * comes back from there and this frame's pad does not run.\n+\t */\n+\tif (test__start_subtest(\"tail_call_taken\")) {\n+\t\tint key = 0, prog_fd = bpf_program__fd(skel-\u003eprogs.tc_target);\n+\n+\t\tif (ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel-\u003emaps.taken_table),\n+\t\t\t\t\t\t \u0026key, \u0026prog_fd, BPF_ANY),\n+\t\t\t \"populate taken_table\"))\n+\t\t\trun_shape(skel, skel-\u003eprogs.entry_tail_taken, 101,\n+\t\t\t\t THROW_COOKIE, 0);\n+\t}\n+\n+\t/* A throwing subprog named by a BPF_PSEUDO_FUNC no helper is handed: the\n+\t * callback check has to look at the bpf_loop(), not at the ld_imm64.\n+\t */\n+\tif (test__start_subtest(\"addr_taken_no_throw\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_addr_taken, 1, 2, 0);\n+\tif (test__start_subtest(\"addr_taken_throw\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_addr_taken, 101, THROW_COOKIE,\n+\t\t\t RAN_ADDR_TAKEN);\n+\n+\t/* A record covering bpf_throw() itself rather than a call to a frame\n+\t * that throws: raised, caught up with and delivered in one frame.\n+\t */\n+\tif (test__start_subtest(\"no_subprog_no_throw\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_no_subprog, 1, 0, 0);\n+\tif (test__start_subtest(\"no_subprog_throw\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_no_subprog, 101, THROW_COOKIE,\n+\t\t\t RAN_NO_SUBPROG);\n+\n+\t/* A pad that calls a subprogram; with a throwing extension in its place,\n+\t * the nested exception has to stop there, not restart this pad.\n+\t */\n+\tif (test__start_subtest(\"pad_calls_subprog\")) {\n+\t\tskel-\u003ebss-\u003epad_runs = 0;\n+\t\trun_shape(skel, skel-\u003eprogs.entry_pad_calls, 101, THROW_COOKIE,\n+\t\t\t RAN_PAD_CALLS);\n+\t\tASSERT_EQ(skel-\u003ebss-\u003epad_runs, 1, \"pad_runs\");\n+\t}\n+\tif (test__start_subtest(\"pad_calls_throwing_extension\"))\n+\t\ttest_pad_calls_freplace(skel);\n+\n+\t/* A covered throw the sweep leaves last, where the default exception\n+\t * callback is patched in; the pad's bit needs r6-r9 still spilled.\n+\t */\n+\tif (test__start_subtest(\"pad_before_throw\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_pad_first, 101, THROW_COOKIE,\n+\t\t\t RAN_PAD_FIRST);\n+\n+\t/* A region whose last instruction is a 16-byte one, so that end - 1\n+\t * names the half of it that is not an instruction.\n+\t */\n+\tif (test__start_subtest(\"region_ends_on_ldimm64\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_wide_rec, 101, THROW_COOKIE,\n+\t\t\t RAN_WIDE_REC);\n+\n+\t/* A pad that reloads from and writes to its own frame's stack, which a\n+\t * JIT addressing the frame through the stack pointer gets wrong.\n+\t */\n+\tif (test__start_subtest(\"pad_uses_own_frame\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_pad_stack, 101, THROW_COOKIE,\n+\t\t\t RAN_PAD_STACK);\n+\n+\t/* The same, with an uncovered frame between the throw and the pad. */\n+\tif (test__start_subtest(\"pad_two_frames_up\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_deep_pad, 101, THROW_COOKIE,\n+\t\t\t RAN_DEEP_PAD);\n+\n+\t/* An extension program with a cleanup table of its own. */\n+\tif (test__start_subtest(\"extension_carries_table\"))\n+\t\ttest_ext_table(skel);\n+\n+\t/* A pad terminated by _Unwind_Resume, which libbpf maps onto the kfunc;\n+\t * every other program here calls bpf_unwind_resume directly.\n+\t */\n+\tif (test__start_subtest(\"resume_alias\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_resume_alias, 101,\n+\t\t\t THROW_COOKIE, RAN_RESUME_ALIAS);\n+\n+\t/* A pad that calls a subprogram which tail calls, array empty and then\n+\t * populated: the tail call releases only the callee's own prologue.\n+\t */\n+\tif (test__start_subtest(\"pad_callee_tail_call\")) {\n+\t\tint key = 0, prog_fd = bpf_program__fd(skel-\u003eprogs.pad_tc_target);\n+\n+\t\tskel-\u003ebss-\u003epad_tc_target_ran = 0;\n+\t\tskel-\u003ebss-\u003epad_runs = 0;\n+\t\trun_shape(skel, skel-\u003eprogs.entry_pad_tail_call, 101,\n+\t\t\t THROW_COOKIE, RAN_PAD_TAIL_CALL);\n+\t\tASSERT_EQ(skel-\u003ebss-\u003epad_tc_target_ran, 0, \"target not run\");\n+\t\tASSERT_EQ(skel-\u003ebss-\u003epad_runs, 1, \"pad_runs\");\n+\n+\t\tif (ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel-\u003emaps.pad_tc_table),\n+\t\t\t\t\t\t \u0026key, \u0026prog_fd, BPF_ANY),\n+\t\t\t \"populate pad_tc_table\")) {\n+\t\t\tskel-\u003ebss-\u003epad_runs = 0;\n+\t\t\trun_shape(skel, skel-\u003eprogs.entry_pad_tail_call, 101,\n+\t\t\t\t THROW_COOKIE, RAN_PAD_TAIL_CALL);\n+\t\t\tASSERT_EQ(skel-\u003ebss-\u003epad_tc_target_ran, 1, \"target ran\");\n+\t\t\tASSERT_EQ(skel-\u003ebss-\u003epad_runs, 1, \"pad_runs\");\n+\t\t}\n+\t}\n+\n+\t/* The same, into a target that carries a table and throws: that target\n+\t * is a boundary, so the outer pad runs once, not twice.\n+\t */\n+\tif (test__start_subtest(\"pad_callee_tail_call_throws\")) {\n+\t\tint key = 0, prog_fd = bpf_program__fd(skel-\u003eprogs.pad_tc_throw_target);\n+\n+\t\tif (ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel-\u003emaps.pad_tc_table),\n+\t\t\t\t\t\t \u0026key, \u0026prog_fd, BPF_ANY),\n+\t\t\t \"populate pad_tc_table\")) {\n+\t\t\tskel-\u003ebss-\u003epad_runs = 0;\n+\t\t\tskel-\u003ebss-\u003etc_target_pad_runs = 0;\n+\t\t\trun_shape(skel, skel-\u003eprogs.entry_pad_tail_call, 101,\n+\t\t\t\t THROW_COOKIE, RAN_PAD_TAIL_CALL);\n+\t\t\t/* The target cleaned up after itself, once. */\n+\t\t\tASSERT_EQ(skel-\u003ebss-\u003etc_target_pad_runs, 1,\n+\t\t\t\t \"tc_target_pad_runs\");\n+\t\t\t/* And the outer pad was not started over. */\n+\t\t\tASSERT_EQ(skel-\u003ebss-\u003epad_runs, 1, \"pad_runs\");\n+\t\t}\n+\t}\n+\n+\texceptions_cleanup_shapes__destroy(skel);\n+}\n+\n+void test_exceptions_cleanup(void)\n+{\n+\tchar log[8192] = {};\n+\tLIBBPF_OPTS(bpf_object_open_opts, opts,\n+\t\t .kernel_log_buf = log,\n+\t\t .kernel_log_size = sizeof(log));\n+\tstruct exceptions_cleanup *skel;\n+\tint err;\n+\n+\tskel = exceptions_cleanup__open_opts(\u0026opts);\n+\tif (!ASSERT_OK_PTR(skel, \"open\"))\n+\t\treturn;\n+\n+\terr = exceptions_cleanup__load(skel);\n+\tif (err) {\n+\t\tif (err == -EOPNOTSUPP \u0026\u0026\n+\t\t strstr(log, \"exception cleanup needs a JIT that can dispatch landing pads\"))\n+\t\t\ttest__skip();\n+\t\telse if (!ASSERT_OK(err, \"load\"))\n+\t\t\tfprintf(stderr, \"%s\", log);\n+\t\texceptions_cleanup__destroy(skel);\n+\t\treturn;\n+\t}\n+\n+\t/* No throw: foo3 returns 1 ^ 1 == 0, foo2 adds one, no pad runs. */\n+\tif (test__start_subtest(\"no_throw\"))\n+\t\trun(skel, 1, 1, 0);\n+\n+\t/* foo3 throws; every pad runs and the cookie is delivered at entry. */\n+\tif (test__start_subtest(\"throw_from_foo3\"))\n+\t\trun(skel, 101, THROW_COOKIE, PADS_FOO3_THREW);\n+\n+\t/* foo3 returns 2 ^ 1 == 3, so foo2 throws from its own second region;\n+\t * foo3's frame is long gone, so its pad must not run.\n+\t */\n+\tif (test__start_subtest(\"throw_from_foo2\"))\n+\t\trun(skel, 2, THROW_COOKIE, PADS_FOO2_THREW);\n+\n+\texceptions_cleanup__destroy(skel);\n+\n+\tif (test__start_subtest(\"light_skeleton\"))\n+\t\ttest_light_skeleton();\n+\n+\ttest_shapes();\n+\n+\tRUN_TESTS(exceptions_cleanup_fail);\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup.c\nnew file mode 100644\nindex 0000000000000..95199a2828faa\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup.c\n@@ -0,0 +1,152 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+#include \u003cvmlinux.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \"bpf_misc.h\"\n+#include \"exceptions_cleanup.h\"\n+\n+static __used __noinline void __kfunc_btf_anchor(void)\n+{\n+\tbpf_throw(0);\n+\tbpf_rcu_read_lock();\n+\tbpf_rcu_read_unlock();\n+\tbpf_preempt_disable();\n+\tbpf_preempt_enable();\n+\tbpf_unwind_resume();\n+}\n+\n+__u64 input = 0;\n+__u64 pads_ran = 0;\n+__u64 result = 0;\n+\n+static __used __noinline __u64 foo3(__u64 x)\n+{\n+\tbpf_preempt_disable();\n+\tif (x \u003e 100)\n+\t\tasm volatile (\n+\t\t\"r1 = %[cookie];\"\n+\t\"1:\"\t\"call bpf_throw;\"\t\t/* cleanup region */\n+\t\"2:\"\n+\t\t\"goto 3f;\"\n+\t\"4:\"\t\t\t\t\t/* landing pad */\n+\t\t\"r7 = r0;\"\n+\t\t\"call bpf_preempt_enable;\"\n+\t\tPAD_RAN(\"%[ran]\")\n+\t\t\"r1 = r7;\"\n+\t\t\"call bpf_unwind_resume;\"\n+\t\"3:\"\n+\t\tCLEANUP_REC(\"1b\", \"2b\", \"4b\")\n+\t\t:\n+\t\t: [cookie]\"i\"(THROW_COOKIE), [ran]\"i\"(RAN_FOO3_PREEMPT),\n+\t\t __imm_addr(pads_ran)\n+\t\t: __clobber_all);\n+\tbpf_preempt_enable();\n+\treturn x ^ 1;\n+}\n+\n+__u64 never = 0;\n+\n+static __used __naked __noinline void drop_glue(void)\n+{\n+\tasm volatile (\n+\tPAD_RAN(\"%[ran]\")\n+\t\"exit;\"\n+\t:\n+\t: [ran]\"i\"(RAN_FOO2_DROP), __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+static __used __naked __noinline __u64 foo2(void)\n+{\n+\tasm volatile (\n+\t\"r6 = r1;\"\n+\t\"call bpf_rcu_read_lock;\"\n+\t\"r1 = r6;\"\n+\"1:\"\t\"call foo3;\"\t\t\t/* cleanup region #1 */\n+\"2:\"\n+\t\"r6 = r0;\"\n+\t\"if r6 == 0 goto 5f;\"\n+\t\"r1 = %[cookie];\"\n+\"3:\"\t\"call bpf_throw;\"\t\t/* cleanup region #2 */\n+\"4:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"5:\"\n+\t\"call bpf_rcu_read_unlock;\"\n+\t\"r0 = r6;\"\n+\t\"r0 += 1;\"\n+\t\"exit;\"\n+\"6:\"\t\t\t\t\t/* landing pad, shared by both regions */\n+\t\"call drop_glue;\"\n+\t\"call bpf_rcu_read_unlock;\"\n+\tPAD_RAN(\"%[ran_rcu]\")\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"6b\")\n+\tCLEANUP_REC(\"3b\", \"4b\", \"6b\")\n+\t:\n+\t: [cookie]\"i\"(THROW_COOKIE), [ran_rcu]\"i\"(RAN_FOO2_RCU),\n+\t __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+static __used __naked __noinline void foo1v(void)\n+{\n+\tasm volatile (\n+\t\"call bpf_preempt_disable;\"\n+\t\"r1 = %[input] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\"1:\"\t\"call foo2;\"\t\t\t/* cleanup region */\n+\"2:\"\n+\t\"r6 = r0;\"\n+\t\"call bpf_preempt_enable;\"\n+\t\"r1 = %[result] ll;\"\n+\t\"*(u64 *)(r1 + 0) = r6;\"\n+\t\"goto 7f;\"\n+\"8:\"\t\t\t\t\t/* landing pad */\n+\t\"call bpf_preempt_enable;\"\n+\tPAD_RAN(\"%[ran]\")\n+\t\"goto 9f;\"\n+\"7:\"\t\t\t\t\t/* the frame's own exit block */\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"9:\"\t\t\t\t\t/* shared resume block */\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"8b\")\n+\t:\n+\t: [ran]\"i\"(RAN_FOO1V_PREEMPT), __imm_addr(input),\n+\t __imm_addr(result), __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+static __used __naked __noinline void bump(void)\n+{\n+\tasm volatile (\n+\tPAD_RAN(\"%[ran]\")\n+\t\"r1 = %[never] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\t\"if r1 == 0 goto 1f;\"\n+\t\"r1 = 0;\"\n+\t\"call bpf_throw;\"\n+\"1:\"\n+\t\"exit;\"\t\t\t\t/* r0 deliberately left alone */\n+\t:\n+\t: [ran]\"i\"(RAN_BUMP), __imm_addr(never), __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+__noinline __u64 foo1(void)\n+{\n+\tbump();\n+\tfoo1v();\n+\treturn result;\n+}\n+\n+SEC(\"syscall\")\n+int entry(void *ctx)\n+{\n+\treturn foo1();\n+}\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_ext_table.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_ext_table.c\nnew file mode 100644\nindex 0000000000000..d14db48d6b29b\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_ext_table.c\n@@ -0,0 +1,48 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+#include \u003cvmlinux.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \"bpf_misc.h\"\n+#include \"exceptions_cleanup.h\"\n+\n+__u64 ext_pad_ran = 0;\n+\n+/* Without a 32-bit int in BTF, libbpf's dummy_ksym var gets type id 0. */\n+int btf_int_anchor;\n+\n+static __used __noinline void __kfunc_btf_anchor(void)\n+{\n+\tbpf_throw(0);\n+\tbpf_preempt_disable();\n+\tbpf_preempt_enable();\n+\tbpf_unwind_resume();\n+}\n+\n+static __used __naked __noinline __u64 ext_frame(void)\n+{\n+\tasm volatile (\n+\t\"call bpf_preempt_disable;\"\n+\t\"r1 = %[cookie];\"\n+\"1:\"\t\"call bpf_throw;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"call bpf_preempt_enable;\"\n+\t\"r1 = %[ext_pad_ran] ll;\"\n+\t\"r2 = 1;\"\n+\t\"*(u64 *)(r1 + 0) = r2;\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [cookie]\"i\"(THROW_COOKIE), __imm_addr(ext_pad_ran)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"freplace/fr_callee\")\n+__u64 new_fr_callee(__u64 x)\n+{\n+\treturn ext_frame();\n+}\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_fail.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_fail.c\nnew file mode 100644\nindex 0000000000000..ce2dac306a84c\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_fail.c\n@@ -0,0 +1,600 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+#include \u003cvmlinux.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \"bpf_experimental.h\"\n+#include \"bpf_misc.h\"\n+#include \"../test_kmods/bpf_testmod_kfunc.h\"\n+#include \"exceptions_cleanup.h\"\n+\n+__u64 input = 0;\n+\n+static __used __noinline void __kfunc_btf_anchor(void)\n+{\n+\tbpf_throw(0);\n+\tbpf_preempt_disable();\n+\tbpf_preempt_enable();\n+\tbpf_unwind_resume();\n+}\n+\n+/*\n+ * 1. A subprogram that may unwind, also used as a helper callback:\n+ * bpf_loop()'s own kernel frame would end the walk before it found a\n+ * boundary.\n+ */\n+static int throwing_cb(__u32 idx, void *ctx)\n+{\n+\tbpf_throw(0xbad);\n+\treturn 0;\n+}\n+\n+static __used __naked __noinline __u64 cb_frame(void)\n+{\n+\tasm volatile (\n+\t\"call bpf_preempt_disable;\"\n+\"1:\"\t\"call throwing_cb;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"call bpf_preempt_enable;\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"call bpf_preempt_enable;\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t::: __clobber_all);\n+}\n+\n+SEC(\"?syscall\")\n+__failure __msg(\"may unwind and is used as a callback\")\n+int callback_may_unwind(void *ctx)\n+{\n+\tbpf_loop(1, throwing_cb, NULL, 0);\n+\treturn cb_frame();\n+}\n+\n+/*\n+ * 2. A landing pad that reaches both an unwind resume and a plain exit, so\n+ * nothing says whether it is a cleanup pad or a catch pad.\n+ */\n+static __used __naked __noinline __u64 inner_throw(void)\n+{\n+\tasm volatile (\n+\t\"r1 = 1;\"\n+\t\"call bpf_throw;\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\t::: __clobber_all);\n+}\n+\n+static __used __naked __noinline __u64 ambiguous_pad_frame(void)\n+{\n+\tasm volatile (\n+\t\"r6 = r1;\"\n+\t\"call bpf_preempt_disable;\"\n+\"1:\"\t\"call inner_throw;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"call bpf_preempt_enable;\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad: two ways out */\n+\t\"call bpf_preempt_enable;\"\n+\t\"if r6 \u003e 10 goto 4f;\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\"4:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t::: __clobber_all);\n+}\n+\n+SEC(\"?syscall\")\n+__failure __msg(\"reaches both bpf_unwind_resume() and a plain exit\")\n+int ambiguous_landing_pad(void *ctx)\n+{\n+\treturn ambiguous_pad_frame();\n+}\n+\n+/*\n+ * 3. A throw from inside a landing pad: a second walk over the frames the\n+ * first one is in the middle of discarding.\n+ */\n+static __used __naked __noinline __u64 throw_in_pad_frame(void)\n+{\n+\tasm volatile (\n+\t\"call bpf_preempt_disable;\"\n+\"1:\"\t\"call inner_throw;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"call bpf_preempt_enable;\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad that throws again */\n+\t\"call bpf_preempt_enable;\"\n+\t\"r1 = 2;\"\n+\t\"call bpf_throw;\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t::: __clobber_all);\n+}\n+\n+SEC(\"?syscall\")\n+__failure __msg(\"can throw while an exception is in flight\")\n+int throw_from_landing_pad(void *ctx)\n+{\n+\treturn throw_in_pad_frame();\n+}\n+\n+/*\n+ * 4. A cleanup table in a program that also installs an exception callback,\n+ * two different answers to what runs on the way out.\n+ */\n+__noinline int unused_exc_cb(u64 cookie)\n+{\n+\treturn 0;\n+}\n+\n+static __used __naked __noinline __u64 cb_and_table_frame(void)\n+{\n+\tasm volatile (\n+\t\"call bpf_preempt_disable;\"\n+\t\"r1 = 9;\"\n+\"1:\"\t\"call bpf_throw;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"call bpf_preempt_enable;\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t::: __clobber_all);\n+}\n+\n+SEC(\"?syscall\")\n+__exception_cb(unused_exc_cb)\n+__failure __msg(\"cannot be combined with an exception callback\")\n+int table_with_exception_cb(void *ctx)\n+{\n+\treturn cb_and_table_frame();\n+}\n+\n+__u64 never;\n+\n+/*\n+ * 5. A landing pad that calls a subprogram which can throw. Not case 3: the\n+ * throw is in another subprogram, so what catches it is the walk of the pad's\n+ * body, off subprog_info.might_throw.\n+ */\n+static __used __noinline void pad_callee_that_throws(void)\n+{\n+\tif (never)\n+\t\tbpf_throw(0);\n+}\n+\n+static __used __naked __noinline __u64 pad_calls_thrower_frame(void)\n+{\n+\tasm volatile (\n+\t\"call bpf_preempt_disable;\"\n+\t\"r1 = 11;\"\n+\"1:\"\t\"call bpf_throw;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"call pad_callee_that_throws;\"\t/* ...which can throw: refused */\n+\t\"call bpf_preempt_enable;\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t::: __clobber_all);\n+}\n+\n+SEC(\"?syscall\")\n+__failure __msg(\"which can throw while an exception is in flight\")\n+int pad_calls_thrower(void *ctx)\n+{\n+\treturn pad_calls_thrower_frame();\n+}\n+\n+/*\n+ * 6. A catch pad: it ends in a plain exit rather than a resume, and a walker\n+ * that calls pads as subroutines cannot hand a frame back its own execution.\n+ */\n+static __used __naked __noinline __u64 catch_pad_frame(void)\n+{\n+\tasm volatile (\n+\t\"call bpf_preempt_disable;\"\n+\t\"r1 = 12;\"\n+\"1:\"\t\"call bpf_throw;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* catch pad: no resume, it stops here */\n+\t\"call bpf_preempt_enable;\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t::: __clobber_all);\n+}\n+\n+SEC(\"?syscall\")\n+__failure __msg(\"is not supported yet, only cleanup pads that resume\")\n+int catch_landing_pad(void *ctx)\n+{\n+\treturn catch_pad_frame();\n+}\n+\n+/*\n+ * 7. An exception reaching the boundary of a program type that constrains\n+ * its return value: delivery makes the cookie that return value, and fentry\n+ * has to return 0.\n+ */\n+static __used __naked __noinline __u64 boundary_throw_frame(void)\n+{\n+\tasm volatile (\n+\t\"call bpf_preempt_disable;\"\n+\t\"r1 = 7;\"\n+\"1:\"\t\"call bpf_throw;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"call bpf_preempt_enable;\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t::: __clobber_all);\n+}\n+\n+SEC(\"?fentry/bpf_fentry_test1\")\n+__failure __msg(\"the register R1 has smin=7 smax=7 should have been in [0, 0]\")\n+int boundary_delivers(void *ctx)\n+{\n+\treturn boundary_throw_frame();\n+}\n+\n+/*\n+ * 8. A bpf_unwind_resume() outside any landing pad. Both JITs lower it as\n+ * the way back out of a pad, which in ordinary code leaves a live frame\n+ * standing with its epilogue skipped.\n+ */\n+static __used __naked __noinline __u64 stray_resume_frame(void)\n+{\n+\tasm volatile (\n+\t\"call bpf_preempt_disable;\"\n+\t\"r1 = 13;\"\n+\"1:\"\t\"call bpf_throw;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"call bpf_preempt_enable;\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t::: __clobber_all);\n+}\n+\n+SEC(\"?syscall\")\n+__failure __msg(\"is not in an exception cleanup landing pad\")\n+int resume_outside_pad(void *ctx)\n+{\n+\t/* Never taken, but reachable, which is all the verifier needs. */\n+\tif (never)\n+\t\tbpf_unwind_resume();\n+\treturn stray_resume_frame();\n+}\n+\n+/*\n+ * 9. A bpf_unwind_resume() in a subprogram a landing pad calls. The\n+ * verifier's walk cannot tell it from a resume in the pad itself -- an\n+ * exception is in flight either way -- so the rule is static: a resume sits in\n+ * a pad body.\n+ */\n+static __used __naked __noinline void resume_in_callee(void)\n+{\n+\tasm volatile (\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\t::: __clobber_all);\n+}\n+\n+static __used __naked __noinline __u64 pad_calls_resumer_frame(void)\n+{\n+\tasm volatile (\n+\t\"call bpf_preempt_disable;\"\n+\t\"r1 = 14;\"\n+\"1:\"\t\"call bpf_throw;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"call bpf_preempt_enable;\"\n+\t\"call resume_in_callee;\"\t/* ...which resumes: refused */\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t::: __clobber_all);\n+}\n+\n+SEC(\"?syscall\")\n+__failure __msg(\"is not in an exception cleanup landing pad\")\n+int resume_in_pad_callee(void *ctx)\n+{\n+\treturn pad_calls_resumer_frame();\n+}\n+\n+/*\n+ * 10. A bpf_unwind_resume() in a program carrying no cleanup table, where\n+ * that static rule does not run at all. do_check() refuses it on the state not\n+ * unwinding, and has to: the JITs lower every one of these the same way.\n+ */\n+static __used __naked __noinline __u64 no_table_resume_frame(void)\n+{\n+\tasm volatile (\n+\t\"call bpf_unwind_resume;\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\t::: __clobber_all);\n+}\n+\n+SEC(\"?syscall\")\n+__failure __msg(\"reached without an exception in flight\")\n+int resume_without_table(void *ctx)\n+{\n+\treturn no_table_resume_frame();\n+}\n+\n+/*\n+ * 11. A landing pad that is itself a covered call site, so an exception out\n+ * of it would have nowhere to go. Hand-written only: LLVM sinks a function's\n+ * pads past every range it emits.\n+ */\n+static __used __naked __noinline __u64 nested_pad_frame(void)\n+{\n+\tasm volatile (\n+\t\"call bpf_preempt_disable;\"\n+\"1:\"\t\"call inner_throw;\"\t\t/* first cleanup region */\n+\"2:\"\n+\t\"call bpf_preempt_enable;\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* first pad, second region's call */\n+\t\"call bpf_preempt_enable;\"\n+\"4:\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\"5:\"\t\t\t\t\t/* second pad */\n+\t\"call bpf_preempt_enable;\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\tCLEANUP_REC(\"3b\", \"4b\", \"5b\")\n+\t::: __clobber_all);\n+}\n+\n+SEC(\"?syscall\")\n+__failure __msg(\"is inside the call-site range of\")\n+int nested_landing_pad(void *ctx)\n+{\n+\treturn nested_pad_frame();\n+}\n+\n+/*\n+ * 12. A tail call in a landing pad: it unwinds the prologue off the stack\n+ * pointer, which in a pad is the walker's.\n+ */\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_PROG_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__uint(key_size, sizeof(__u32));\n+\t__uint(value_size, sizeof(__u32));\n+} tc_map SEC(\".maps\");\n+\n+static __used __naked __noinline __u64 tail_call_pad_frame(void)\n+{\n+\tasm volatile (\n+\t\"r6 = r1;\"\n+\"1:\"\t\"call inner_throw;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"r1 = r6;\"\n+\t\"r2 = %[tc_map] ll;\"\n+\t\"r3 = 0;\"\n+\t\"call %[bpf_tail_call];\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: __imm(bpf_tail_call), __imm_addr(tc_map)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"?syscall\")\n+__failure __msg(\"is in an exception cleanup landing pad\")\n+int tail_call_in_pad(void *ctx)\n+{\n+\treturn tail_call_pad_frame();\n+}\n+\n+#if defined(__BPF_FEATURE_STACK_ARGUMENT)\n+\n+/*\n+ * 13. A call that passes an argument on the stack, in a landing pad: the\n+ * outgoing area the callee reads is not the one the caller wrote, the frame\n+ * being the unwinding one and the stack pointer the walker's.\n+ */\n+static __used __noinline __u64 six_args(__u64 a, __u64 b, __u64 c, __u64 d,\n+\t\t\t\t\t__u64 e, __u64 f)\n+{\n+\treturn a + b + c + d + e + f;\n+}\n+\n+static __used __naked __noinline __u64 stack_arg_pad_frame(void)\n+{\n+\tasm volatile (\n+\t\"call bpf_preempt_disable;\"\n+\"1:\"\t\"call inner_throw;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"call bpf_preempt_enable;\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"call bpf_preempt_enable;\"\n+\t\"r1 = 1;\"\n+\t\"r2 = 2;\"\n+\t\"r3 = 3;\"\n+\t\"r4 = 4;\"\n+\t\"r5 = 5;\"\n+\t\"*(u64 *)(r11 - 8) = 6;\"\t/* the sixth argument */\n+\t\"call six_args;\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t::: __clobber_all);\n+}\n+\n+SEC(\"?syscall\")\n+__failure __msg(\"on-stack call argument in an exception cleanup landing pad\")\n+int stack_arg_in_pad(void *ctx)\n+{\n+\treturn stack_arg_pad_frame();\n+}\n+\n+/*\n+ * 14. The same, reached the other way: a kfunc whose by-value argument runs\n+ * past the five argument registers, where the JIT fills the outgoing area and\n+ * the rule above has no store to catch. The C call gives the extern its BTF.\n+ */\n+static __used __noinline void __nofit_btf_anchor(void)\n+{\n+\tstruct prog_test_pair_arg s = {};\n+\n+\tbpf_kfunc_call_test_pair_arg_nofit(1, 2, 3, 4, s);\n+}\n+\n+static __used __naked __noinline __u64 kfunc_arg_pad_frame(void)\n+{\n+\tasm volatile (\n+\t\"call bpf_preempt_disable;\"\n+\"1:\"\t\"call inner_throw;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"call bpf_preempt_enable;\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"call bpf_preempt_enable;\"\n+\t\"r1 = 1;\"\n+\t\"r2 = 2;\"\n+\t\"r3 = 3;\"\n+\t\"r4 = 4;\"\n+\t\"r5 = 5;\"\n+\t\"call bpf_kfunc_call_test_pair_arg_nofit;\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t::: __clobber_all);\n+}\n+\n+SEC(\"?syscall\")\n+__failure __msg(\"on-stack call argument in an exception cleanup landing pad\")\n+int kfunc_stack_arg_in_pad(void *ctx)\n+{\n+\treturn kfunc_arg_pad_frame();\n+}\n+\n+#endif /* __BPF_FEATURE_STACK_ARGUMENT */\n+\n+/*\n+ * 15. A landing pad entered by ordinary control flow, arriving with none of\n+ * what the walker sets up. Nothing static sees it -- the resume really is in a\n+ * pad body -- so do_check() refuses it on the state not unwinding.\n+ */\n+static __used __naked __noinline __u64 jump_into_pad_frame(void)\n+{\n+\tasm volatile (\n+\t\"r1 = %[input] ll;\"\n+\t\"r6 = *(u64 *)(r1 + 0);\"\n+\t\"if r6 \u003e 7 goto 4f;\"\t\t/* an ordinary branch into the pad */\n+\"1:\"\t\"call inner_throw;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"r7 = r0;\"\n+\"4:\"\t\t\t\t\t/* ... and its second instruction */\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: __imm_addr(input)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"?syscall\")\n+__failure __msg(\"reached without an exception in flight\")\n+int jump_into_pad(void *ctx)\n+{\n+\treturn jump_into_pad_frame();\n+}\n+\n+#if defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)\n+\n+/*\n+ * 16. A landing pad that reaches an indirect jump, which cannot be told from\n+ * a catch pad. SEC(\"socket\") because a jump table entry is an offset from the\n+ * program's section symbol, and \"?syscall\" is not a name assembly can use.\n+ */\n+static __used __naked __noinline void gotox_thrower(void)\n+{\n+\tasm volatile (\n+\t\"r1 = 15;\"\n+\t\"call bpf_throw;\"\n+\t\"exit;\"\n+\t::: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__failure __msg(\"reaches an indirect jump\")\n+__naked void gotox_in_pad(void)\n+{\n+\tasm volatile (\n+\t\".pushsection .jumptables,\\\"\\\",@progbits;\"\n+\"jt0_%=:\"\n+\t\".quad l0_%= - socket;\"\n+\t\".quad l1_%= - socket;\"\n+\t\".size jt0_%=, 16;\"\n+\t\".global jt0_%=;\"\n+\t\".popsection;\"\n+\n+\"1:\"\t\"call gotox_thrower;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"r1 = jt0_%= ll;\"\n+\t\"r1 += 8;\"\n+\t\"r2 = *(u64 *)(r1 + 0);\"\n+\t/* gotox r2. Spelled as a raw insn on purpose: the \"gotox\" mnemonic\n+\t * only reached the LLVM assembler in llvm 22, and BPF_RAW_INSN()\n+\t * needs \u003clinux/bpf.h\u003e, which this file cannot have -- vmlinux.h\n+\t * already defines the uapi enums.\n+\t */\n+\t\".8byte 0x20d;\"\n+\"l0_%=:\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\"l1_%=:\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t::: __clobber_all);\n+}\n+\n+#endif /* x86 || arm64 */\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_freplace.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_freplace.c\nnew file mode 100644\nindex 0000000000000..afb358fd3d405\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_freplace.c\n@@ -0,0 +1,17 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+#include \u003cvmlinux.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \"exceptions_cleanup.h\"\n+\n+/* Without a 32-bit int in BTF, libbpf's dummy_ksym var gets type id 0. */\n+int btf_int_anchor;\n+\n+SEC(\"freplace/fr_callee\")\n+__u64 new_fr_callee(__u64 x)\n+{\n+\tbpf_throw(THROW_COOKIE);\n+\treturn 0;\n+}\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_light.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_light.c\nnew file mode 100644\nindex 0000000000000..de7ae478d67f3\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_light.c\n@@ -0,0 +1,39 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+#include \u003cvmlinux.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \"bpf_misc.h\"\n+#include \"exceptions_cleanup.h\"\n+\n+static __used __noinline void __kfunc_btf_anchor(void)\n+{\n+\tbpf_throw(0);\n+\tbpf_preempt_disable();\n+\tbpf_preempt_enable();\n+\tbpf_unwind_resume();\n+}\n+\n+__u64 pads_ran = 0;\n+\n+SEC(\"syscall\")\n+__naked int entry_light(void)\n+{\n+\tasm volatile (\n+\t\"call bpf_preempt_disable;\"\n+\t\"r1 = %[cookie];\"\n+\"1:\"\t\"call bpf_throw;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"call bpf_preempt_enable;\"\n+\tPAD_RAN(\"%[ran]\")\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [cookie]\"i\"(THROW_COOKIE), [ran]\"i\"(RAN_LIGHT),\n+\t __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_pad_freplace.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_pad_freplace.c\nnew file mode 100644\nindex 0000000000000..eabac6baabb70\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_pad_freplace.c\n@@ -0,0 +1,17 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+#include \u003cvmlinux.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \"exceptions_cleanup.h\"\n+\n+/* Without a 32-bit int in BTF, libbpf's dummy_ksym var gets type id 0. */\n+int btf_int_anchor;\n+\n+SEC(\"freplace/pad_callee\")\n+__u64 new_pad_callee(__u64 x)\n+{\n+\tbpf_throw(INNER_COOKIE);\n+\treturn 0;\n+}\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c\nnew file mode 100644\nindex 0000000000000..f5eb2ff15c896\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c\n@@ -0,0 +1,863 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+#include \u003cvmlinux.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \"bpf_misc.h\"\n+#include \"exceptions_cleanup.h\"\n+\n+#define PAD_COUNT\t\t\t\t\t\t\\\n+\t\"r1 = %[pad_runs] ll;\"\t\t\t\t\t\\\n+\t\"r2 = *(u64 *)(r1 + 0);\"\t\t\t\t\\\n+\t\"r2 += 1;\"\t\t\t\t\t\t\\\n+\t\"*(u64 *)(r1 + 0) = r2;\"\n+\n+static __used __noinline void __kfunc_btf_anchor(void)\n+{\n+\tbpf_throw(0);\n+\tbpf_rcu_read_lock();\n+\tbpf_rcu_read_unlock();\n+\tbpf_preempt_disable();\n+\tbpf_preempt_enable();\n+\tbpf_unwind_resume();\n+}\n+\n+__u64 input = 0;\n+__u64 magic = 0x5eed;\n+__u64 pads_ran = 0;\n+__u64 pad_runs = 0;\n+\n+/*\n+ * 1. Everything a cleanup table leaves dead: the continuation after a throw,\n+ * the tail after a pad's resume, an ld_imm64 and a conditional branch inside\n+ * that tail, and a block reached only by the dead continuation.\n+ */\n+static __used __naked __noinline __u64 sweep_frame(void)\n+{\n+\tasm volatile (\n+\t\"r1 = %[input] ll;\"\n+\t\"r6 = *(u64 *)(r1 + 0);\"\n+\t\"call bpf_preempt_disable;\"\n+\t\"if r6 \u003c 101 goto 6f;\"\n+\t\"r1 = %[cookie];\"\n+\"1:\"\t\"call bpf_throw;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"goto 3f;\"\n+\"4:\"\t\t\t\t\t/* landing pad */\n+\t\"r7 = r0;\"\n+\t\"call bpf_preempt_enable;\"\n+\tPAD_RAN(\"%[ran]\")\n+\t\"r1 = r7;\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"r1 = %[pads_ran] ll;\"\n+\t\"r2 = *(u64 *)(r1 + 0);\"\n+\t\"if r2 == 0 goto 5f;\"\n+\t\"call bpf_preempt_enable;\"\n+\t\"r0 = 7;\"\n+\t\"exit;\"\n+\"5:\"\n+\t\"r0 = 8;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* dead: only the dead goto reaches it */\n+\t\"r0 = 9;\"\n+\t\"exit;\"\n+\"6:\"\t\t\t\t\t/* live: the ordinary return */\n+\t\"call bpf_preempt_enable;\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"4b\")\n+\t:\n+\t: [cookie]\"i\"(THROW_COOKIE), [ran]\"i\"(RAN_SWEEP),\n+\t __imm_addr(input), __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+int entry_sweep(void *ctx)\n+{\n+\treturn sweep_frame();\n+}\n+\n+/*\n+ * 2. A callee called from both a covered and an uncovered site: the pad is\n+ * recorded on the call site, not on the callee. The lock sits between the two\n+ * calls because the frame really would leak it if the uncovered call unwound.\n+ */\n+static __used __noinline __u64 shared_callee(__u64 x)\n+{\n+\tif (x \u003e 100)\n+\t\tbpf_throw(THROW_COOKIE);\n+\treturn x + 1;\n+}\n+\n+static __used __naked __noinline __u64 shared_frame(void)\n+{\n+\tasm volatile (\n+\t\"r1 = %[input] ll;\"\n+\t\"r6 = *(u64 *)(r1 + 0);\"\n+\t\"r1 = 0;\"\n+\t\"call shared_callee;\"\n+\t\"call bpf_rcu_read_lock;\"\n+\t\"r1 = r6;\"\n+\"1:\"\t\"call shared_callee;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"r6 = r0;\"\n+\t\"call bpf_rcu_read_unlock;\"\n+\t\"r0 = r6;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"r7 = r0;\"\n+\t\"call bpf_rcu_read_unlock;\"\n+\tPAD_RAN(\"%[ran]\")\n+\t\"r1 = r7;\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [ran]\"i\"(RAN_SHARED), __imm_addr(input), __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+int entry_shared(void *ctx)\n+{\n+\treturn shared_frame();\n+}\n+\n+/*\n+ * 3. A landing pad that reads its frame's callee-saved registers, which only\n+ * the spill in the discarded callee's prologue still holds. The callee fills\n+ * r6-r9 with something else before it throws, so the pad's check passes only\n+ * if the walker found that spill.\n+ */\n+#define LOAD_MAGIC_REGS\t\t\t\t\t\t\\\n+\t\"r1 = %[magic] ll;\"\t\t\t\t\t\\\n+\t\"r6 = *(u64 *)(r1 + 0);\"\t\t\t\t\\\n+\t\"r7 = r6;\"\t\t\t\t\t\t\\\n+\t\"r7 += 1;\"\t\t\t\t\t\t\\\n+\t\"r8 = r6;\"\t\t\t\t\t\t\\\n+\t\"r8 += 2;\"\t\t\t\t\t\t\\\n+\t\"r9 = r6;\"\t\t\t\t\t\t\\\n+\t\"r9 += 3;\"\n+\n+/* Set @bit only if r6-r9 still hold what LOAD_MAGIC_REGS put there. */\n+#define CHECK_MAGIC_REGS(bit)\t\t\t\t\t\\\n+\t\"r1 = %[magic] ll;\"\t\t\t\t\t\\\n+\t\"r2 = *(u64 *)(r1 + 0);\"\t\t\t\t\\\n+\t\"if r6 != r2 goto 9f;\"\t\t\t\t\t\\\n+\t\"r2 += 1;\"\t\t\t\t\t\t\\\n+\t\"if r7 != r2 goto 9f;\"\t\t\t\t\t\\\n+\t\"r2 += 1;\"\t\t\t\t\t\t\\\n+\t\"if r8 != r2 goto 9f;\"\t\t\t\t\t\\\n+\t\"r2 += 1;\"\t\t\t\t\t\t\\\n+\t\"if r9 != r2 goto 9f;\"\t\t\t\t\t\\\n+\tPAD_RAN(bit)\t\t\t\t\t\t\\\n+\t\"9:\"\n+\n+static __used __naked __noinline __u64 regs_thrower(void)\n+{\n+\tasm volatile (\n+\t/* Not this frame's to keep, and that is the point. */\n+\t\"r6 = 0xdead;\"\n+\t\"r7 = 0xbeef;\"\n+\t\"r8 = 0xcafe;\"\n+\t\"r9 = 0xf00d;\"\n+\t\"r1 = %[cookie];\"\n+\t\"call bpf_throw;\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\t:\n+\t: [cookie]\"i\"(THROW_COOKIE)\n+\t: __clobber_all);\n+}\n+\n+static __used __naked __noinline __u64 regs_frame(void)\n+{\n+\tasm volatile (\n+\tLOAD_MAGIC_REGS\n+\t\"call bpf_preempt_disable;\"\n+\"1:\"\t\"call regs_thrower;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"call bpf_preempt_enable;\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"call bpf_preempt_enable;\"\n+\tCHECK_MAGIC_REGS(\"%[ran]\")\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [cookie]\"i\"(THROW_COOKIE), [ran]\"i\"(RAN_REGS),\n+\t __imm_addr(magic), __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+int entry_regs(void *ctx)\n+{\n+\treturn regs_frame();\n+}\n+\n+/*\n+ * 4. The same, with a tail-call-reachable callee: its prologue pushes the tail\n+ * call counter between the program stack and the spill area, so the spill the\n+ * walker reads moves. The array is left empty; being reachable is the point.\n+ */\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_PROG_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__uint(key_size, sizeof(__u32));\n+\t__uint(value_size, sizeof(__u32));\n+} jmp_table SEC(\".maps\");\n+\n+static __used __noinline __u64 tc_thrower(void *ctx)\n+{\n+\t/* Never taken; its presence is what makes this frame, whose spill the\n+\t * walker reads, tail-call-reachable.\n+\t */\n+\tbpf_tail_call_static(ctx, \u0026jmp_table, 0);\n+\tasm volatile (\n+\t\"r6 = 0xdead;\"\n+\t\"r7 = 0xbeef;\"\n+\t\"r8 = 0xcafe;\"\n+\t\"r9 = 0xf00d;\"\n+\t\"r1 = %[cookie];\"\n+\t\"call bpf_throw;\"\n+\t:\n+\t: [cookie]\"i\"(THROW_COOKIE)\n+\t: __clobber_all);\n+\treturn 0;\n+}\n+\n+/*\n+ * The frame with the pad is the program itself, and __naked: r1 holds the\n+ * context at entry, which is the only place to get one for bpf_tail_call().\n+ */\n+SEC(\"syscall\")\n+__naked int entry_tail_call(void)\n+{\n+\tasm volatile (\n+\t\"*(u64 *)(r10 - 8) = r1;\"\t/* the context, straight from entry */\n+\tLOAD_MAGIC_REGS\n+\t\"r1 = %[input] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\t\"if r1 \u003c 101 goto 8f;\"\n+\t\"r1 = *(u64 *)(r10 - 8);\"\n+\"1:\"\t\"call tc_thrower;\"\t\t/* cleanup region */\n+\"2:\"\n+\"8:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\tCHECK_MAGIC_REGS(\"%[ran]\")\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [ran]\"i\"(RAN_TAIL_CALL), __imm_addr(input),\n+\t __imm_addr(magic), __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+/*\n+ * 5. A landing pad in the main program's own frame. jit_subprogs() compiles it\n+ * as func[0], but the ksym the walker finds is the outer bpf_prog's, so the\n+ * table has to be handed over or the pad is never dispatched -- silently.\n+ */\n+SEC(\"syscall\")\n+__naked int entry_main_pad(void)\n+{\n+\tasm volatile (\n+\tLOAD_MAGIC_REGS\n+\t\"r1 = %[input] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\t\"if r1 \u003c 101 goto 8f;\"\n+\"1:\"\t\"call regs_thrower;\"\t\t/* cleanup region */\n+\"2:\"\n+\"8:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\tCHECK_MAGIC_REGS(\"%[ran]\")\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [ran]\"i\"(RAN_MAIN_PAD), __imm_addr(input),\n+\t __imm_addr(magic), __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+/*\n+ * 6. A tail call that is really taken: the target is a program in its own\n+ * right, so the walk ends there and this frame's pad does not run. The callee\n+ * can also throw on a path never taken, which keeps the pad out of the sweep.\n+ */\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_PROG_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__uint(key_size, sizeof(__u32));\n+\t__uint(value_size, sizeof(__u32));\n+} taken_table SEC(\".maps\");\n+\n+SEC(\"syscall\")\n+int tc_target(void *ctx)\n+{\n+\tbpf_throw(THROW_COOKIE);\n+\treturn 0;\n+}\n+\n+static __used __noinline __u64 tc_taken_callee(void *ctx, __u64 x)\n+{\n+\t/* Never true at run time; the verifier cannot know that, and its\n+\t * unwind out of here is what keeps the caller's pad alive.\n+\t */\n+\tif (x == 7)\n+\t\tbpf_throw(THROW_COOKIE);\n+\tbpf_tail_call_static(ctx, \u0026taken_table, 0);\n+\treturn 0;\n+}\n+\n+SEC(\"syscall\")\n+__naked int entry_tail_taken(void)\n+{\n+\tasm volatile (\n+\t\"*(u64 *)(r10 - 8) = r1;\"\t/* the context, straight from entry */\n+\t\"r1 = %[input] ll;\"\n+\t\"r2 = *(u64 *)(r1 + 0);\"\n+\t\"if r2 \u003c 101 goto 8f;\"\n+\t\"r1 = *(u64 *)(r10 - 8);\"\n+\"1:\"\t\"call tc_taken_callee;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"exit;\"\t\t\t\t/* the cookie, delivered at tc_target */\n+\"8:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad: must not run */\n+\tPAD_RAN(\"%[ran]\")\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [ran]\"i\"(RAN_TC_TAKEN), __imm_addr(input), __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+/*\n+ * 7. An extension program over the callee of a covered call. The walk ends in\n+ * the extension's frame, as it does for a tail call target, so the pad does\n+ * not run; fr_callee() can also throw by itself, giving the same call site\n+ * both answers.\n+ */\n+__noinline __u64 fr_callee(__u64 x)\n+{\n+\tif (x == 7)\n+\t\tbpf_throw(THROW_COOKIE);\n+\treturn x + 1;\n+}\n+\n+SEC(\"syscall\")\n+__naked int entry_freplace(void)\n+{\n+\tasm volatile (\n+\t\"r1 = %[input] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\"1:\"\t\"call fr_callee;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\tPAD_RAN(\"%[ran]\")\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [ran]\"i\"(RAN_FREPLACE), __imm_addr(input), __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+/*\n+ * 8. A throwing subprogram named by a BPF_PSEUDO_FUNC on a path never taken.\n+ * Handing one to a helper is what is refused, not naming it, so anything going\n+ * by the ld_imm64 alone turns this program away.\n+ */\n+static __used __noinline int cb_thrower(__u32 idx, void *ctx)\n+{\n+\tbpf_throw(THROW_COOKIE);\n+\treturn 0;\n+}\n+\n+static __used __noinline __u64 addr_taken_callee(__u64 x)\n+{\n+\tif (x \u003c= 100)\n+\t\treturn x + 1;\n+\tbpf_throw(THROW_COOKIE);\n+\treturn bpf_loop(1, cb_thrower, NULL, 0);\n+}\n+\n+SEC(\"syscall\")\n+__naked int entry_addr_taken(void)\n+{\n+\tasm volatile (\n+\t\"r1 = %[input] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\"1:\"\t\"call addr_taken_callee;\"\t/* cleanup region */\n+\"2:\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\tPAD_RAN(\"%[ran]\")\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [ran]\"i\"(RAN_ADDR_TAKEN), __imm_addr(input), __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+/*\n+ * 9. A record that covers bpf_throw() itself: the frame that raises the\n+ * exception is the frame the record covers and the boundary both, so the pad\n+ * runs on the way to delivering the cookie out of the program it came from.\n+ */\n+SEC(\"syscall\")\n+__naked int entry_no_subprog(void)\n+{\n+\tasm volatile (\n+\t\"r1 = %[input] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\t\"if r1 \u003c 101 goto 8f;\"\n+\t\"call bpf_preempt_disable;\"\n+\t\"r1 = %[cookie];\"\n+\"1:\"\t\"call bpf_throw;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"exit;\"\n+\"8:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"call bpf_preempt_enable;\"\n+\tPAD_RAN(\"%[ran]\")\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [cookie]\"i\"(THROW_COOKIE), [ran]\"i\"(RAN_NO_SUBPROG),\n+\t __imm_addr(input), __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+/*\n+ * 10. A landing pad that calls a subprogram an extension can replace. The\n+ * load-time rule cannot see the extension coming, so what stops a nested\n+ * exception is the walk, which ends in the extension's own frame. pad_runs\n+ * says the pad ran once rather than twice.\n+ */\n+__noinline __u64 pad_callee(__u64 x)\n+{\n+\treturn x + 1;\n+}\n+\n+static __used __noinline __u64 pc_thrower(__u64 x)\n+{\n+\tif (x \u003e 100)\n+\t\tbpf_throw(THROW_COOKIE);\n+\treturn x + 1;\n+}\n+\n+static __used __naked __noinline __u64 pad_calls_frame(void)\n+{\n+\tasm volatile (\n+\t\"r1 = %[input] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\"1:\"\t\"call pc_thrower;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"r6 = r0;\"\n+\t\"r1 = 1;\"\n+\t\"call pad_callee;\"\t\t/* an extension can stand in here */\n+\tPAD_COUNT\n+\tPAD_RAN(\"%[ran]\")\n+\t\"r1 = r6;\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [ran]\"i\"(RAN_PAD_CALLS), __imm_addr(input), __imm_addr(pads_ran),\n+\t __imm_addr(pad_runs)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+int entry_pad_calls(void *ctx)\n+{\n+\treturn pad_calls_frame();\n+}\n+\n+/*\n+ * 11. A covered bpf_throw() the sweep leaves as the last instruction, where\n+ * the default exception callback is then patched in -- the one patchlet that\n+ * does not keep the call it replaced in the last slot, so the marks have to\n+ * follow it. The r6-r9 check is what reports a lost throw site mark.\n+ */\n+SEC(\"syscall\")\n+__naked int entry_pad_first(void)\n+{\n+\tasm volatile (\n+\tLOAD_MAGIC_REGS\n+\t\"r1 = %[input] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\t\"if r1 \u003c 101 goto 7f;\"\n+\t\"goto 4f;\"\n+\"3:\"\t\t\t\t\t/* landing pad, ahead of the call */\n+\tCHECK_MAGIC_REGS(\"%[ran]\")\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\"7:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"4:\"\n+\t\"r1 = %[cookie];\"\n+\"1:\"\t\"call bpf_throw;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"exit;\"\t\t\t\t/* dead: swept, leaving the call last */\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [cookie]\"i\"(THROW_COOKIE), [ran]\"i\"(RAN_PAD_FIRST),\n+\t __imm_addr(input), __imm_addr(magic), __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+/*\n+ * 12. A cleanup region whose last instruction is a 16-byte one, so end - 1\n+ * names the half that is not an instruction of its own. A well formed region\n+ * that a rule against it would turn away.\n+ */\n+static __used __naked __noinline __u64 wide_rec_frame(void)\n+{\n+\tasm volatile (\n+\t\"r1 = %[input] ll;\"\n+\t\"r6 = *(u64 *)(r1 + 0);\"\n+\t\"call bpf_rcu_read_lock;\"\n+\t\"r1 = r6;\"\n+\"1:\"\t\"call shared_callee;\"\t\t/* cleanup region begins */\n+\t\"r1 = %[magic] ll;\"\t\t/* ... and ends on this pair */\n+\"2:\"\n+\t\"r6 = r0;\"\n+\t\"call bpf_rcu_read_unlock;\"\n+\t\"r0 = r6;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"r7 = r0;\"\n+\t\"call bpf_rcu_read_unlock;\"\n+\tPAD_RAN(\"%[ran]\")\n+\t\"r1 = r7;\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [ran]\"i\"(RAN_WIDE_REC), __imm_addr(input), __imm_addr(magic),\n+\t __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+int entry_wide_rec(void *ctx)\n+{\n+\treturn wide_rec_frame();\n+}\n+\n+/*\n+ * 13. A pad that works out of its own frame's stack, the shape every\n+ * compiler-generated pad has. A JIT that addresses the frame through the\n+ * stack pointer -- arm64 -- has to address a pad's frame some other way. Both\n+ * directions are here: the reload sees the frame, and the store lands in it.\n+ */\n+static __used __naked __noinline __u64 pad_stack_frame(void)\n+{\n+\tasm volatile (\n+\t\"r1 = %[magic] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\t\"*(u64 *)(r10 - 8) = r1;\"\t/* what the pad will want */\n+\t\"r1 = %[input] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\"1:\"\t\"call pc_thrower;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"r6 = r0;\"\n+\t\"r7 = *(u64 *)(r10 - 8);\"\t/* reload it out of the frame */\n+\t\"*(u64 *)(r10 - 16) = r7;\"\t/* and write the frame while here */\n+\t\"r1 = %[magic] ll;\"\n+\t\"r2 = *(u64 *)(r1 + 0);\"\n+\t\"if r7 != r2 goto 9f;\"\n+\t\"r3 = *(u64 *)(r10 - 16);\"\n+\t\"if r3 != r2 goto 9f;\"\n+\tPAD_RAN(\"%[ran]\")\n+\"9:\"\n+\t\"r1 = r6;\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [ran]\"i\"(RAN_PAD_STACK), __imm_addr(input), __imm_addr(magic),\n+\t __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+int entry_pad_stack(void *ctx)\n+{\n+\treturn pad_stack_frame();\n+}\n+\n+/*\n+ * 14. The same, with a frame in between that has no pad of its own, so the\n+ * liveness query for an outer frame has more than one frame to walk and the\n+ * pad has to be counted at every step.\n+ */\n+static __used __noinline __u64 deep_mid(__u64 x)\n+{\n+\treturn pc_thrower(x) + 1;\n+}\n+\n+static __used __naked __noinline __u64 deep_frame(void)\n+{\n+\tasm volatile (\n+\t\"r1 = %[magic] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\t\"*(u64 *)(r10 - 8) = r1;\"\t/* nothing but the pad reads this */\n+\t\"r1 = %[input] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\"1:\"\t\"call deep_mid;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t\"r6 = r0;\"\n+\t\"r7 = *(u64 *)(r10 - 8);\"\n+\t\"r1 = %[magic] ll;\"\n+\t\"r2 = *(u64 *)(r1 + 0);\"\n+\t\"if r7 != r2 goto 9f;\"\n+\tPAD_RAN(\"%[ran]\")\n+\"9:\"\n+\t\"r1 = r6;\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [ran]\"i\"(RAN_DEEP_PAD), __imm_addr(input), __imm_addr(magic),\n+\t __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+int entry_deep_pad(void *ctx)\n+{\n+\treturn deep_frame();\n+}\n+\n+/*\n+ * 15. A region around a call the kernel knows cannot unwind: no call site is\n+ * marked, nothing reaches the pad, and the sweep removes it. The program is\n+ * otherwise ordinary and has to load.\n+ */\n+static __used __naked __noinline __u64 nounwind_rec_frame(void)\n+{\n+\tasm volatile (\n+\t\"call bpf_preempt_disable;\"\n+\"1:\"\t\"call bpf_preempt_enable;\"\t/* cleanup region: nounwind */\n+\"2:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad, never dispatched */\n+\tPAD_RAN(\"%[ran]\")\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [ran]\"i\"(RAN_NOUNWIND_REC), __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+int entry_nounwind_rec(void *ctx)\n+{\n+\treturn nounwind_rec_frame();\n+}\n+\n+/*\n+ * 16. The name a frontend gives the resume. Every pad above calls\n+ * bpf_unwind_resume(); LLVM emits _Unwind_Resume() and libbpf maps one onto\n+ * the other, so this program is what keeps that mapping tested.\n+ */\n+extern void _Unwind_Resume(void) __ksym;\n+\n+static __used __noinline void __resume_alias_btf_anchor(void)\n+{\n+\t_Unwind_Resume();\n+}\n+\n+static __used __naked __noinline __u64 resume_alias_frame(void)\n+{\n+\tasm volatile (\n+\"1:\"\t\"call regs_thrower;\"\t\t/* cleanup region */\n+\"2:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\tPAD_RAN(\"%[ran]\")\n+\t\"call _Unwind_Resume;\"\t\t/* the frontend's name for it */\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [ran]\"i\"(RAN_RESUME_ALIAS), __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+int entry_resume_alias(void *ctx)\n+{\n+\treturn resume_alias_frame();\n+}\n+\n+/*\n+ * 17. A landing pad that calls a subprogram which tail calls. What a pad may\n+ * not contain is a tail call of its own, which would unwind a prologue the\n+ * walker's stack never held; a callee's prologue really did run there, so its\n+ * tail call releases exactly that and the target returns into the pad. The\n+ * tail call counter comes out of the unwinding frame, which is one of the\n+ * pad's own subprogram and so really holds one.\n+ */\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_PROG_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__uint(key_size, sizeof(__u32));\n+\t__uint(value_size, sizeof(__u32));\n+} pad_tc_table SEC(\".maps\");\n+\n+__u64 pad_tc_target_ran = 0;\n+\n+SEC(\"syscall\")\n+int pad_tc_target(void *ctx)\n+{\n+\tpad_tc_target_ran += 1;\n+\treturn 0;\n+}\n+\n+static __used __noinline __u64 pad_tc_callee(void *ctx)\n+{\n+\t/* Taken only once the test has populated the array. */\n+\tbpf_tail_call_static(ctx, \u0026pad_tc_table, 0);\n+\treturn 0;\n+}\n+\n+/*\n+ * The frame with the pad is the program itself, and __naked: r1 holds the\n+ * context at entry, which is the only place to get one for bpf_tail_call().\n+ * The pad reloads it from its own frame's stack.\n+ */\n+SEC(\"syscall\")\n+__naked int entry_pad_tail_call(void)\n+{\n+\tasm volatile (\n+\t\"*(u64 *)(r10 - 8) = r1;\"\t/* the context, straight from entry */\n+\tLOAD_MAGIC_REGS\n+\t\"r1 = %[input] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\t\"if r1 \u003c 101 goto 8f;\"\n+\"1:\"\t\"call regs_thrower;\"\t\t/* cleanup region */\n+\"2:\"\n+\"8:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\tPAD_COUNT\n+\t\"r1 = *(u64 *)(r10 - 8);\"\n+\t\"call pad_tc_callee;\"\n+\t/* Only if the frame survived the callee's tail call. */\n+\tCHECK_MAGIC_REGS(\"%[ran]\")\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [ran]\"i\"(RAN_PAD_TAIL_CALL), __imm_addr(input),\n+\t __imm_addr(magic), __imm_addr(pads_ran), __imm_addr(pad_runs)\n+\t: __clobber_all);\n+}\n+\n+/*\n+ * 18. The other target for that same tail call: a program carrying a cleanup\n+ * table of its own, which throws while the outer exception is still in flight.\n+ * The tail call made it a boundary, so the inner walk runs its pad and ends in\n+ * its own frame, never reaching the walker's frames above it: the outer pad is\n+ * not restarted and the outer cookie is still the one delivered. The outer\n+ * pad's r6-r9, which this target overwrites, come back with its frame.\n+ */\n+__u64 tc_target_pad_runs = 0;\n+__u64 inner_magic = 0xd00d;\n+\n+static __used __naked __noinline __u64 inner_thrower(void)\n+{\n+\tasm volatile (\n+\t/* Not this frame's to keep, the same as regs_thrower. */\n+\t\"r6 = 0xf00d;\"\n+\t\"r7 = 0xcafe;\"\n+\t\"r8 = 0xbeef;\"\n+\t\"r9 = 0xdead;\"\n+\t\"r1 = %[cookie];\"\n+\t\"call bpf_throw;\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\t:\n+\t: [cookie]\"i\"(INNER_COOKIE)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__naked int pad_tc_throw_target(void)\n+{\n+\tasm volatile (\n+\t/* Distinct from the outer pad's, so neither can stand in for it. */\n+\t\"r1 = %[inner_magic] ll;\"\n+\t\"r6 = *(u64 *)(r1 + 0);\"\n+\t\"r7 = r6;\"\n+\t\"r7 += 1;\"\n+\t\"r8 = r6;\"\n+\t\"r8 += 2;\"\n+\t\"r9 = r6;\"\n+\t\"r9 += 3;\"\n+\t\"r1 = %[input] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\t\"if r1 \u003c 101 goto 8f;\"\n+\"1:\"\t\"call inner_thrower;\"\t\t/* cleanup region */\n+\"2:\"\n+\"8:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad */\n+\t/* This frame's own r6-r9, not the outer pad's. */\n+\t\"r1 = %[inner_magic] ll;\"\n+\t\"r2 = *(u64 *)(r1 + 0);\"\n+\t\"if r6 != r2 goto 9f;\"\n+\t\"r2 += 1;\"\n+\t\"if r7 != r2 goto 9f;\"\n+\t\"r2 += 1;\"\n+\t\"if r8 != r2 goto 9f;\"\n+\t\"r2 += 1;\"\n+\t\"if r9 != r2 goto 9f;\"\n+\t\"r1 = %[tc_target_pad_runs] ll;\"\n+\t\"r2 = *(u64 *)(r1 + 0);\"\n+\t\"r2 += 1;\"\n+\t\"*(u64 *)(r1 + 0) = r2;\"\n+\"9:\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: __imm_addr(input), __imm_addr(inner_magic),\n+\t __imm_addr(tc_target_pad_runs)\n+\t: __clobber_all);\n+}\n+\n+char _license[] SEC(\"license\") = \"GPL\";\n"
} |
| 2/1 | 2026/09/18 21:42 | agent | patch-evaluator |
0mModel:core Args: null Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT",
"UNWINDER_ORC"
],
"FocusSymbols": [
"arch_bpf_run_cleanup_pad",
"bpf_prepare_cleanup_exceptions",
"bpf_check_cleanup_exceptions",
"bpf_cleanup_attach_main_prog",
"bpf_cleanup_fill_native_ranges",
"bpf_cleanup_pad_for_ip",
"bpf_unwind_resume",
"bpf_cleanup_pad_of_call"
],
"Reasoning": "The patch introduces support for BPF exception cleanup landing pads. This adds new fields (cleanup_info, cleanup_info_cnt, cleanup_info_rec_size) to the BPF_PROG_LOAD command of the bpf() syscall, along with extensive verifier validation (BTF/attribute checking, CFG reachability analysis, liveness analysis, verifier state tracking for unwinding) and x86 JIT runtime handling (arch_bpf_run_cleanup_pad, throw-site register spills, unwinding execution with bpf_throw and bpf_unwind_resume). This modifies core BPF verifier and runtime execution logic reachable from userspace, warranting fuzzing.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.
IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.
================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.
- REACHABILITY IS THE PRIMARY GATE:
Fuzzing can only discover bugs in code that can actually execute in standard virtualized
environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
regardless of whether it adds assertions or complex logic.
- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
Patch authors routinely label changes as "cleanups", "refactorings", or state
"No functional change intended". Do NOT take these claims at face value.
Code refactorings that rearrange logic, introduce helper functions, or alter state management
in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
If reachable executable code is modified or refactored, it MUST be fuzzed.
- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
Even if the author believes the invariant always holds, fuzzing is essential to verify whether
an unusual sequence of operations can violate it.
================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:
- Non-kernel and non-executable changes:
* Modifications to Documentation/, comments, or spelling fixes.
* User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
that do not affect the compiled kernel image (vmlinux) or kernel modules.
* Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
that do not alter control flow or data structures.
* Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
* Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
* Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
physical PCI hot-unplug or manual sysfs driver unbinding.
* Code paths exclusive to architectures other than the target architecture.
================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
* Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
* Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
* Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
* Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).
================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:
- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
Do NOT list generic, ubiquitous functions called by almost every program in the corpus
(including, but not limited to: general memory allocators and deallocators, page fault
and trap handlers, or core synchronization primitives; this is not an exhaustive list).
Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
which severely dilutes fuzzing effort away from the actual changes.
- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
List functions that specifically implement the logic being added or altered, or direct API entrypoints
for the subsystem feature under review.
- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).
================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit aa17089d32a7e14651815f32eec679e963dce2a9
Author: syz-cluster <triage@syzkaller.com>
Date: Fri Sep 18 21:42:55 2026 +0000
syz-cluster: applied patch under review
diff --git a/arch/arm64/net/Makefile b/arch/arm64/net/Makefile
index 3ae382bfca879..ebec2a44a52b6 100644
--- a/arch/arm64/net/Makefile
+++ b/arch/arm64/net/Makefile
@@ -2,4 +2,4 @@
#
# ARM64 networking code
#
-obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o
+obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o bpf_cleanup_pad.o
diff --git a/arch/arm64/net/bpf_cleanup_pad.S b/arch/arm64/net/bpf_cleanup_pad.S
new file mode 100644
index 0000000000000..ef441241949ec
--- /dev/null
+++ b/arch/arm64/net/bpf_cleanup_pad.S
@@ -0,0 +1,95 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <linux/bpf_cleanup_abi.h>
+#include <linux/linkage.h>
+
+/*
+ * A frame's prologue pushes the tail call counter pair, and then -- once
+ * bpf_cleanup_force_spill() says so -- x19/x20, x21/x22, x23/x24, x25/x26 and
+ * x27/x28. Each A64_PUSH pre-decrements, so the lowest address of the spill
+ * area holds x27 and the highest x20:
+ *
+ * spill_base + 0 x27 (private stack pointer)
+ * spill_base + 8 x28 (arena base)
+ * spill_base + 16 x25 (BPF r10, the frame pointer)
+ * spill_base + 24 x26 (tail call counter pointer)
+ * spill_base + 32 x23 -- the pad's own, see below
+ * spill_base + 40 x24 -- likewise
+ * spill_base + 48 x21 (BPF r8)
+ * spill_base + 56 x22 (BPF r9)
+ * spill_base + 64 x19 (BPF r6)
+ * spill_base + 72 x20 (BPF r7)
+ *
+ * Neither x23 nor x24 is restored from that spill: the pad has its own use for
+ * both. bpf2a64[] maps nothing to either -- they are pushed only to keep the
+ * frame shape the exception callback expects -- so nothing else in generated
+ * code touches them, and being callee-saved they survive every call the pad
+ * makes.
+ *
+ * x23 is the pad's return address. Unlike x86-64 a pad cannot simply return:
+ * every call it makes clobbers x30, so nothing is left to return through by
+ * the time it reaches its resume. The JIT emits "br x23" for the pad's
+ * bpf_unwind_resume() and this routine puts .Lcleanup_pad_done there.
+ *
+ * x24 is where the pad's frame is anchored. Generated code addresses the BPF
+ * frame through the stack pointer, which here is this routine's rather than
+ * the unwinding frame's, so the JIT has the pad recompute the equivalent from
+ * BPF r10 on entry and address its frame off x24 for as long as it runs.
+ *
+ * Both of those branches are indirect, so both targets carry a BTI landing
+ * marker: the JIT emits one at each pad, and .Lcleanup_pad_done below has one
+ * of its own.
+ */
+
+ .text
+
+/*
+ * void arch_bpf_run_cleanup_pad(u64 pad, u64 frame_fp, u64 spill_base)
+ *
+ * x0 = native address of the landing pad
+ * x1 = frame pointer of the frame the pad belongs to (unused here: BPF r10 is
+ * x25, which the spill area already holds)
+ * x2 = spill area holding that frame's BPF callee-saved registers
+ *
+ * Give the pad the register state of its own frame and call it. It runs on
+ * this stack, far below the frame it is cleaning up after, so nothing it
+ * calls can reach into that frame.
+ */
+SYM_FUNC_START(arch_bpf_run_cleanup_pad)
+ /* Save the kernel's callee-saved registers; the pad owns them next. */
+ stp x29, x30, [sp, #-96]!
+ mov x29, sp
+ stp x19, x20, [sp, #16]
+ stp x21, x22, [sp, #32]
+ stp x23, x24, [sp, #48]
+ stp x25, x26, [sp, #64]
+ stp x27, x28, [sp, #80]
+
+ /* x9 is BPF_REG_AX, so the pad's address does not stay in BPF r1. */
+ mov x9, x0
+
+ ldp x27, x28, [x2, #0]
+ ldp x25, x26, [x2, #16]
+ ldp x21, x22, [x2, #48]
+ ldp x19, x20, [x2, #64]
+
+ /* BPF r0 (x8) on the way into a pad, not whatever the kernel left. */
+ mov x8, #BPF_PAD_ENTRY_R0
+
+ /* Where the pad's resume branches back to. */
+ adr x23, .Lcleanup_pad_done
+
+ br x9
+
+.Lcleanup_pad_done:
+ /* Reached by the pad's "br x23", so it is an indirect branch target. */
+ bti j
+ ldp x19, x20, [sp, #16]
+ ldp x21, x22, [sp, #32]
+ ldp x23, x24, [sp, #48]
+ ldp x25, x26, [sp, #64]
+ ldp x27, x28, [sp, #80]
+ ldp x29, x30, [sp], #96
+ ret
+SYM_FUNC_END(arch_bpf_run_cleanup_pad)
diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index 6c04fee468766..560eba305bca2 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -11,6 +11,7 @@
#include <linux/bitfield.h>
#include <linux/bpf.h>
#include <linux/cfi.h>
+#include <linux/bpf_verifier.h>
#include <linux/filter.h>
#include <linux/memory.h>
#include <linux/printk.h>
@@ -75,7 +76,21 @@ static const int bpf2a64[] = {
[ARENA_VM_START] = A64_R(28),
};
+/* Throw-site spill: the five pairs push_callee_regs() forces on, same size and
+ * slot order, so arch_bpf_run_cleanup_pad() reads both alike.
+ */
+#define A64_CLEANUP_SPILL_SZ (5 * 16)
+
+/*
+ * Where a landing pad's frame is anchored, since the stack pointer generated
+ * code normally addresses it through is the walker's inside a pad. bpf2a64[]
+ * maps nothing to x24, so nothing else in generated code touches it.
+ */
+#define A64_CLEANUP_FP A64_R(24)
+
struct jit_ctx {
+ /* Bytes reserved for the throw-site spill; see bpf_cleanup_force_spill(). */
+ u32 throw_spill;
const struct bpf_prog *prog;
int idx;
int epilogue_offset;
@@ -432,7 +447,7 @@ static void push_callee_regs(struct jit_ctx *ctx)
* Callee-saved registers as the exception callback needs to recover
* all ARM64 Callee-saved registers in its epilogue.
*/
- if (ctx->prog->aux->exception_boundary) {
+ if (ctx->prog->aux->exception_boundary || bpf_cleanup_force_spill(ctx->prog)) {
emit(A64_PUSH(A64_R(19), A64_R(20), A64_SP), ctx);
emit(A64_PUSH(A64_R(21), A64_R(22), A64_SP), ctx);
emit(A64_PUSH(A64_R(23), A64_R(24), A64_SP), ctx);
@@ -466,7 +481,8 @@ static void pop_callee_regs(struct jit_ctx *ctx)
* program's stack frame, so recover these extra registers in the above
* two cases.
*/
- if (aux->exception_boundary || aux->exception_cb) {
+ if (aux->exception_boundary || aux->exception_cb ||
+ bpf_cleanup_force_spill(ctx->prog)) {
emit(A64_POP(A64_R(27), A64_R(28), A64_SP), ctx);
emit(A64_POP(A64_R(25), A64_R(26), A64_SP), ctx);
emit(A64_POP(A64_R(23), A64_R(24), A64_SP), ctx);
@@ -602,6 +618,20 @@ static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
emit(A64_SUB_I(1, A64_SP, A64_FP, 96), ctx);
}
+ /*
+ * Lowest address of each spill area, as an offset from A64_FP; see
+ * bpf_cleanup_pad.S for the layout. The 16 is the tail call counter
+ * pair pushed just below the frame record, and the throw-site area
+ * sits below the callee-saved one rather than in the program stack.
+ */
+ if (bpf_cleanup_force_spill(prog)) {
+ prog->aux->exc->spill_off = -(16 + A64_CLEANUP_SPILL_SZ);
+ ctx->throw_spill = A64_CLEANUP_SPILL_SZ;
+ emit(A64_SUB_I(1, A64_SP, A64_SP, ctx->throw_spill), ctx);
+ prog->aux->exc->throw_spill_off =
+ -(16 + A64_CLEANUP_SPILL_SZ) - ctx->throw_spill;
+ }
+
/* Stack must be multiples of 16B */
ctx->stack_size = round_up(prog->aux->stack_depth, 16);
@@ -691,6 +721,10 @@ static int emit_bpf_tail_call(struct jit_ctx *ctx)
if (ctx->stack_size && !ctx->priv_sp_used)
emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
+ /* Release it for the same reason build_epilogue() does. */
+ if (ctx->throw_spill)
+ emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->throw_spill), ctx);
+
pop_callee_regs(ctx);
/* goto *(prog->bpf_func + prologue_offset); */
@@ -1055,6 +1089,9 @@ static void build_epilogue(struct jit_ctx *ctx, bool was_classic)
if (ctx->stack_size && !ctx->priv_sp_used)
emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
+ if (ctx->throw_spill)
+ emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->throw_spill), ctx);
+
pop_callee_regs(ctx);
emit(A64_POP(A64_ZR, ptr, A64_SP), ctx);
@@ -1230,6 +1267,13 @@ static const u8 stack_arg_reg[] = { A64_R(5), A64_R(6), A64_R(7) };
#define NR_STACK_ARG_REGS ARRAY_SIZE(stack_arg_reg)
+/*
+ * This reads the incoming argument area off A64_FP, which in an exception
+ * cleanup landing pad would be arch_bpf_run_cleanup_pad()'s frame record
+ * rather than the unwinding frame's -- but a pad cannot contain one of these:
+ * check_stack_arg_read() requires every r11 load to come before the frame's
+ * first call, and a pad only ever runs after one.
+ */
static void emit_stack_arg_load(u8 dst, s16 bpf_off, struct jit_ctx *ctx)
{
int idx = bpf_off / sizeof(u64) - 1;
@@ -1367,6 +1411,7 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
const s16 off = insn->off;
const s32 imm = insn->imm;
const int i = insn - ctx->prog->insnsi;
+ const bool in_pad = bpf_cleanup_insn_in_pad(ctx->prog, i);
const bool is64 = BPF_CLASS(code) == BPF_ALU64 ||
BPF_CLASS(code) == BPF_JMP;
u8 jmp_cond;
@@ -1378,9 +1423,14 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
int ret;
bool sign_extend;
- if (bpf_insn_is_indirect_target(env, ctx->prog, i))
+ if (bpf_insn_is_indirect_target(env, ctx->prog, i) ||
+ bpf_cleanup_insn_is_pad(ctx->prog, i))
emit_bti(A64_BTI_J, ctx);
+ if (bpf_cleanup_insn_is_pad(ctx->prog, i))
+ emit(A64_SUB_I(1, A64_CLEANUP_FP, fp,
+ ctx->stack_size + ctx->stack_arg_size), ctx);
+
switch (code) {
/* dst = src */
case BPF_ALU | BPF_MOV | BPF_X:
@@ -1743,6 +1793,26 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
u64 func_addr;
u32 cpu_offset;
+ if (bpf_cleanup_insn_is_throw(ctx->prog, insn - ctx->prog->insnsi)) {
+ /* Spill where the bpf_throw() walker looks. */
+ const s32 off = ctx->prog->aux->exc->throw_spill_off;
+
+ emit(A64_SUB_I(1, tmp, A64_FP, -off), ctx);
+ emit(A64_STR64I(bpf2a64[PRIVATE_SP], tmp, 0), ctx);
+ emit(A64_STR64I(bpf2a64[ARENA_VM_START], tmp, 8), ctx);
+ emit(A64_STR64I(bpf2a64[BPF_REG_FP], tmp, 16), ctx);
+ emit(A64_STR64I(bpf2a64[TCCNT_PTR], tmp, 24), ctx);
+ emit(A64_STR64I(bpf2a64[BPF_REG_8], tmp, 48), ctx);
+ emit(A64_STR64I(bpf2a64[BPF_REG_9], tmp, 56), ctx);
+ emit(A64_STR64I(bpf2a64[BPF_REG_6], tmp, 64), ctx);
+ emit(A64_STR64I(bpf2a64[BPF_REG_7], tmp, 72), ctx);
+ }
+
+ if (bpf_is_unwind_resume_kfunc(insn)) {
+ emit(A64_BR(A64_R(23)), ctx);
+ break;
+ }
+
/* Implement helper call to bpf_get_smp_processor_id() inline */
if (insn->src_reg == 0 && insn->imm == BPF_FUNC_get_smp_processor_id) {
cpu_offset = offsetof(struct thread_info, cpu);
@@ -1854,7 +1924,8 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
src = tmp2;
}
if (src == fp) {
- src_adj = ctx->priv_sp_used ? priv_sp : A64_SP;
+ src_adj = ctx->priv_sp_used ? priv_sp :
+ in_pad ? A64_CLEANUP_FP : A64_SP;
off_adj = off + ctx->stack_size;
if (!ctx->priv_sp_used)
off_adj += ctx->stack_arg_size;
@@ -1952,7 +2023,8 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
dst = tmp3;
}
if (dst == fp) {
- dst_adj = ctx->priv_sp_used ? priv_sp : A64_SP;
+ dst_adj = ctx->priv_sp_used ? priv_sp :
+ in_pad ? A64_CLEANUP_FP : A64_SP;
off_adj = off + ctx->stack_size;
if (!ctx->priv_sp_used)
off_adj += ctx->stack_arg_size;
@@ -2021,7 +2093,8 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
dst = tmp2;
}
if (dst == fp) {
- dst_adj = ctx->priv_sp_used ? priv_sp : A64_SP;
+ dst_adj = ctx->priv_sp_used ? priv_sp :
+ in_pad ? A64_CLEANUP_FP : A64_SP;
off_adj = off + ctx->stack_size;
if (!ctx->priv_sp_used)
off_adj += ctx->stack_arg_size;
@@ -2410,6 +2483,13 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
* reasons, expects to point to the next instruction)
*/
bpf_prog_update_insn_ptrs(prog, ctx.offset, ctx.ro_image);
+
+ /*
+ * Same byte offsets, consumed by the bpf_throw() frame walker:
+ * turn the cleanup records into native address ranges now that
+ * the image is final.
+ */
+ bpf_cleanup_fill_native_ranges(prog, ctx.offset, ctx.ro_image);
out_off:
if (!ro_header && priv_stack_ptr) {
free_percpu(priv_stack_ptr);
@@ -3385,6 +3465,11 @@ bool bpf_jit_supports_exceptions(void)
return true;
}
+bool bpf_jit_supports_cleanup_pads(void)
+{
+ return true;
+}
+
bool bpf_jit_supports_arena(void)
{
return true;
diff --git a/arch/x86/net/Makefile b/arch/x86/net/Makefile
index dddbefc0f4398..9d574d972df37 100644
--- a/arch/x86/net/Makefile
+++ b/arch/x86/net/Makefile
@@ -6,5 +6,5 @@
ifeq ($(CONFIG_X86_32),y)
obj-$(CONFIG_BPF_JIT) += bpf_jit_comp32.o
else
- obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o
+ obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o bpf_cleanup_pad.o
endif
diff --git a/arch/x86/net/bpf_cleanup_pad.S b/arch/x86/net/bpf_cleanup_pad.S
new file mode 100644
index 0000000000000..da4b448ecf095
--- /dev/null
+++ b/arch/x86/net/bpf_cleanup_pad.S
@@ -0,0 +1,74 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <linux/bpf_cleanup_abi.h>
+#include <linux/linkage.h>
+#include <asm/nospec-branch.h>
+
+/*
+ * The x86-64 BPF JIT prologue spills, once bpf_cleanup_force_spill() makes it
+ * unconditional, r12, rbx, r13, r14 and r15 in that order -- so within the
+ * spill area the lowest address holds r15 and the highest r12. The throw-site
+ * spill the JIT emits uses the same layout, so the routine below reads both
+ * the same way:
+ *
+ * spill_base + 0 BPF r9 (r15)
+ * spill_base + 8 BPF r8 (r14)
+ * spill_base + 16 BPF r7 (r13)
+ * spill_base + 24 BPF r6 (rbx)
+ * spill_base + 32 r12 (arena base, not a BPF register)
+ */
+
+ .code64
+ .section .text, "ax"
+
+/*
+ * void arch_bpf_run_cleanup_pad(u64 pad, u64 frame_fp, u64 spill_base)
+ *
+ * rdi = native address of the landing pad
+ * rsi = frame pointer of the frame the pad belongs to
+ * rdx = spill area holding that frame's BPF callee-saved registers
+ *
+ * Give the pad the register state of its own frame and call it. The pad ends
+ * in the bare return the JIT emits for its bpf_unwind_resume(), so it comes
+ * back here rather than returning to its frame's caller. It runs on this
+ * stack, far below the frame it is cleaning up after, so nothing it calls can
+ * reach into that frame.
+ */
+SYM_FUNC_START(arch_bpf_run_cleanup_pad)
+ ANNOTATE_NOENDBR
+
+ pushq %rbp
+ pushq %rbx
+ pushq %r12
+ pushq %r13
+ pushq %r14
+ pushq %r15
+ /* Keep the pad's entry rsp congruent to a normal call's. */
+ subq $8, %rsp
+
+ movq 0(%rdx), %r15
+ movq 8(%rdx), %r14
+ movq 16(%rdx), %r13
+ movq 24(%rdx), %rbx
+ movq 32(%rdx), %r12
+ /* rbp is BPF r10, so this is the whole of the pad's frame setup. */
+ movq %rsi, %rbp
+
+ /* CALL_NOSPEC needs the target in a register; rcx is BPF r4, dead. */
+ movq %rdi, %rcx
+
+ /* BPF r0 on the way into a pad, not whatever the kernel left in rax. */
+ movl $BPF_PAD_ENTRY_R0, %eax
+
+ CALL_NOSPEC rcx
+
+ addq $8, %rsp
+ popq %r15
+ popq %r14
+ popq %r13
+ popq %r12
+ popq %rbx
+ popq %rbp
+ RET
+SYM_FUNC_END(arch_bpf_run_cleanup_pad)
diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index d4a980140b48d..9d0dd54773e83 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -357,6 +357,11 @@ struct jit_context {
/* Number of bytes that will be skipped on tailcall */
#define X86_TAIL_CALL_OFFSET (12 + ENDBR_INSN_SIZE)
+/* Throw-site spill: r15, r14, r13, rbx, r12 low to high, the layout the
+ * prologue's pushes leave, so arch_bpf_run_cleanup_pad() reads both alike.
+ */
+#define X86_CLEANUP_SPILL_SZ (5 * 8)
+
static void push_r9(u8 **pprog)
{
u8 *prog = *pprog;
@@ -832,7 +837,7 @@ static void emit_bpf_tail_call_indirect(struct bpf_prog *bpf_prog,
/* Inc tail_call_cnt if the slot is populated. */
EMIT4(0x48, 0x83, 0x00, 0x01); /* add qword ptr [rax], 1 */
- if (bpf_prog->aux->exception_boundary) {
+ if (bpf_prog->aux->exception_boundary || bpf_cleanup_force_spill(bpf_prog)) {
pop_callee_regs(&prog, all_callee_regs_used);
pop_r12(&prog);
} else {
@@ -899,7 +904,7 @@ static void emit_bpf_tail_call_direct(struct bpf_prog *bpf_prog,
/* Inc tail_call_cnt if the slot is populated. */
EMIT4(0x48, 0x83, 0x00, 0x01); /* add qword ptr [rax], 1 */
- if (bpf_prog->aux->exception_boundary) {
+ if (bpf_prog->aux->exception_boundary || bpf_cleanup_force_spill(bpf_prog)) {
pop_callee_regs(&prog, all_callee_regs_used);
pop_r12(&prog);
} else {
@@ -1977,6 +1982,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
u8 *ip, *prog = temp;
u32 stack_depth;
int callee_saved_size;
+ u32 throw_spill, prologue_depth;
s32 outgoing_arg_base;
int err;
@@ -2015,7 +2021,10 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
detect_reg_usage(insn, insn_cnt, callee_regs_used);
- emit_prologue(&prog, image, stack_depth,
+ throw_spill = bpf_cleanup_force_spill(bpf_prog) ? X86_CLEANUP_SPILL_SZ : 0;
+ prologue_depth = stack_depth + throw_spill;
+
+ emit_prologue(&prog, image, prologue_depth,
bpf_prog_was_classic(bpf_prog), tail_call_reachable,
bpf_is_subprog(bpf_prog), bpf_prog->aux->exception_cb);
@@ -2024,7 +2033,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
/* Exception callback will clobber callee regs for its own use, and
* restore the original callee regs from main prog's stack frame.
*/
- if (bpf_prog->aux->exception_boundary) {
+ if (bpf_prog->aux->exception_boundary || bpf_cleanup_force_spill(bpf_prog)) {
/* We also need to save r12, which is not mapped to any BPF
* register, as we throw after entry into the kernel, which may
* overwrite r12.
@@ -2039,9 +2048,10 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
/* Compute callee-saved register area size. */
callee_saved_size = 0;
- if (bpf_prog->aux->exception_boundary || arena_vm_start)
+ if (bpf_prog->aux->exception_boundary || bpf_cleanup_force_spill(bpf_prog) ||
+ arena_vm_start)
callee_saved_size += 8; /* r12 */
- if (bpf_prog->aux->exception_boundary) {
+ if (bpf_prog->aux->exception_boundary || bpf_cleanup_force_spill(bpf_prog)) {
callee_saved_size += 4 * 8; /* rbx, r13, r14, r15 */
} else {
int j;
@@ -2063,7 +2073,19 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
* Note that tail_call_reachable is guaranteed to be false when
* stack args exist, so tcc pushes need not be accounted for.
*/
- outgoing_arg_base = -(round_up(stack_depth, 8) + callee_saved_size);
+ outgoing_arg_base = -(round_up(stack_depth, 8) + throw_spill + callee_saved_size);
+
+ /*
+ * Lowest address of each spill area, as an offset from rbp; see
+ * bpf_cleanup_pad.S for the layout. The 16 is the tail call counter
+ * pair emit_prologue_tail_call() pushes above the callee-saved one.
+ */
+ if (bpf_cleanup_force_spill(bpf_prog)) {
+ bpf_prog->aux->exc->spill_off = -(round_up(stack_depth, 8) + throw_spill +
+ (tail_call_reachable ? 16 : 0) +
+ callee_saved_size);
+ bpf_prog->aux->exc->throw_spill_off = -(round_up(stack_depth, 8) + throw_spill);
+ }
/*
* Allocate outgoing stack arg area for args 7+ only.
@@ -2110,7 +2132,8 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
dst_reg = X86_REG_R9;
}
- if (bpf_insn_is_indirect_target(env, bpf_prog, i - 1))
+ if (bpf_insn_is_indirect_target(env, bpf_prog, i - 1) ||
+ bpf_cleanup_insn_is_pad(bpf_prog, i - 1))
EMIT_ENDBR();
ip = image + addrs[i - 1] + (prog - temp);
@@ -2903,9 +2926,27 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
case BPF_JMP | BPF_CALL: {
const struct btf_func_model *fm = NULL;
+ if (bpf_cleanup_insn_is_throw(bpf_prog, i - 1)) {
+ /* Spill r6-r9 and r12 where the bpf_throw() walker looks. */
+ s32 off = bpf_prog->aux->exc->throw_spill_off;
+ u8 *spill = prog;
+
+ emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_9, off + 0);
+ emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_8, off + 8);
+ emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_7, off + 16);
+ emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_6, off + 24);
+ emit_stx(&prog, BPF_DW, BPF_REG_FP, X86_REG_R12, off + 32);
+ ip += prog - spill;
+ }
+
+ if (bpf_is_unwind_resume_kfunc(insn)) {
+ emit_return(&prog, image + addrs[i - 1] + (prog - temp));
+ break;
+ }
+
func = (u8 *) __bpf_call_base + imm32;
if (src_reg == BPF_PSEUDO_CALL && tail_call_reachable) {
- LOAD_TAIL_CALL_CNT_PTR(stack_depth);
+ LOAD_TAIL_CALL_CNT_PTR(prologue_depth);
ip += 7;
}
if (!imm32)
@@ -2948,13 +2989,13 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
&prog,
ip,
callee_regs_used,
- stack_depth,
+ prologue_depth,
ctx);
else
emit_bpf_tail_call_indirect(bpf_prog,
&prog,
callee_regs_used,
- stack_depth,
+ prologue_depth,
ip,
ctx);
break;
@@ -3215,7 +3256,8 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
}
/* Deallocate outgoing args 7+ area. */
emit_add_rsp(&prog, outgoing_rsp);
- if (bpf_prog->aux->exception_boundary) {
+ if (bpf_prog->aux->exception_boundary ||
+ bpf_cleanup_force_spill(bpf_prog)) {
pop_callee_regs(&prog, all_callee_regs_used);
pop_r12(&prog);
} else {
@@ -4385,6 +4427,13 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
*/
bpf_prog_update_insn_ptrs(prog, addrs, image);
+ /*
+ * Same mapping, consumed by the bpf_throw() frame walker:
+ * turn the cleanup records into native address ranges now
+ * that the image is final.
+ */
+ bpf_cleanup_fill_native_ranges(prog, addrs, image);
+
/*
* ctx.prog_offset is used when CFI preambles put code *before*
* the function. See emit_cfi(). For FineIBT specifically this code
@@ -4501,6 +4550,11 @@ bool bpf_jit_supports_exceptions(void)
return IS_ENABLED(CONFIG_UNWINDER_ORC);
}
+bool bpf_jit_supports_cleanup_pads(void)
+{
+ return IS_ENABLED(CONFIG_UNWINDER_ORC);
+}
+
bool bpf_jit_supports_private_stack(void)
{
return true;
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 2a5fa346aadaa..83f2b0d7e5961 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1770,6 +1770,80 @@ enum bpf_sig_keyring {
BPF_SIG_KEYRING_BPF,
};
+/*
+ * One cleanup region of a JITed (sub)program: @pad is the landing pad to run
+ * for a return address in (begin, end], the native code of its call sites.
+ */
+struct bpf_cleanup_range {
+ u64 begin;
+ u64 end;
+ u64 pad;
+};
+
+struct bpf_exception_info {
+ struct bpf_cleanup_info *info;
+ struct bpf_cleanup_range *ranges;
+ /* Landing pad instruction indices, sorted and deduplicated. */
+ u32 *pad_at;
+ /* bpf_throw() call instruction indices, sorted. */
+ u32 *throw_at;
+ /* One bit per instruction that only runs while unwinding. */
+ unsigned long *pad_body;
+ u32 nr_info;
+ u32 nr_ranges;
+ u32 nr_pad_at;
+ u32 nr_throw_at;
+ u32 nr_pad_body;
+ /* Offset from a frame's FP to the caller's spilled r6-r9. */
+ s32 spill_off;
+ /* Likewise, to the registers a frame spills before calling bpf_throw(). */
+ s32 throw_spill_off;
+};
+
+#ifdef CONFIG_BPF_SYSCALL
+bool bpf_cleanup_force_spill(const struct bpf_prog *prog);
+bool bpf_cleanup_insn_is_pad(const struct bpf_prog *prog, u32 idx);
+bool bpf_cleanup_insn_in_pad(const struct bpf_prog *prog, u32 idx);
+bool bpf_cleanup_insn_is_throw(const struct bpf_prog *prog, u32 idx);
+int bpf_cleanup_attach_main_prog(struct bpf_verifier_env *env, struct bpf_prog *prog);
+void bpf_cleanup_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image);
+void bpf_cleanup_free_info(struct bpf_prog_aux *aux);
+#else
+static inline bool bpf_cleanup_force_spill(const struct bpf_prog *prog)
+{
+ return false;
+}
+
+static inline bool bpf_cleanup_insn_is_pad(const struct bpf_prog *prog, u32 idx)
+{
+ return false;
+}
+
+static inline bool bpf_cleanup_insn_in_pad(const struct bpf_prog *prog, u32 idx)
+{
+ return false;
+}
+
+static inline bool bpf_cleanup_insn_is_throw(const struct bpf_prog *prog, u32 idx)
+{
+ return false;
+}
+
+static inline int bpf_cleanup_attach_main_prog(struct bpf_verifier_env *env,
+ struct bpf_prog *prog)
+{
+ return 0;
+}
+
+static inline void bpf_cleanup_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image)
+{
+}
+
+static inline void bpf_cleanup_free_info(struct bpf_prog_aux *aux)
+{
+}
+#endif
+
struct bpf_prog_aux {
atomic64_t refcnt;
u32 used_map_cnt;
@@ -1850,6 +1924,7 @@ struct bpf_prog_aux {
char name[BPF_OBJ_NAME_LEN];
u64 (*bpf_exception_cb)(u64 cookie, u64 sp, u64 bp, u64, u64);
u16 stack_arg_sp_adjust;
+ struct bpf_exception_info *exc;
#ifdef CONFIG_SECURITY
void *security;
#endif
diff --git a/include/linux/bpf_cleanup_abi.h b/include/linux/bpf_cleanup_abi.h
new file mode 100644
index 0000000000000..b6c1d589abda7
--- /dev/null
+++ b/include/linux/bpf_cleanup_abi.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#ifndef _LINUX_BPF_CLEANUP_ABI_H
+#define _LINUX_BPF_CLEANUP_ABI_H
+
+/*
+ * Value arch_bpf_run_cleanup_pad() leaves in r0 on the way into a landing pad.
+ * It has to be a constant the verifier knows: LLVM names r0 as both the
+ * exception pointer and the exception selector register, so every pad reads it
+ * before anything else and is free to store what it read. Kept on its own
+ * because the verifier and the arch dispatchers, which are assembly, have to
+ * agree on it.
+ */
+#define BPF_PAD_ENTRY_R0 1
+
+#endif /* _LINUX_BPF_CLEANUP_ABI_H */
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index cf85141ea1674..09fb89fda40af 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -509,6 +509,7 @@ struct bpf_verifier_state {
bool speculative;
bool in_sleepable;
+ bool unwinding;
/* first and last insn idx of this verifier state */
u32 first_insn_idx;
@@ -681,6 +682,13 @@ struct bpf_insn_aux_data {
bool needs_zext; /* alu op needs to clear upper bits */
bool non_sleepable; /* helper/kfunc may be called from non-sleepable context */
bool is_iter_next; /* bpf_iter_<type>_next() kfunc call */
+ bool cleanup_throw_site; /* call to bpf_throw() */
+ bool in_cleanup_pad; /* only runs with an exception in flight */
+ /*
+ * 1 + the instruction index of the exception cleanup landing pad this
+ * call site unwinds to, or 0 for none.
+ */
+ u32 cleanup_pad;
bool call_with_percpu_alloc_ptr; /* {this,per}_cpu_ptr() with prog percpu alloc */
u8 alu_state; /* used in combination with alu_limit */
/* true if STX or LDX instruction is a part of a spill/fill
@@ -987,6 +995,8 @@ struct bpf_verifier_env {
struct arg_track **callsite_at_stack;
u32 pass_cnt; /* number of times do_check() was called */
u32 subprog_cnt;
+ struct bpf_cleanup_info *cleanup_info;
+ u32 cleanup_info_cnt;
/* number of instructions analyzed by the verifier */
u32 prev_insn_processed, insn_processed;
/* number of jmps, calls, exits analyzed so far */
@@ -1516,6 +1526,7 @@ u32 btf_func_arg_align(const struct btf *btf, const struct btf_type *t);
int bpf_find_subprog(struct bpf_verifier_env *env, int off);
bool bpf_is_throw_kfunc(struct bpf_insn *insn);
+bool bpf_is_unwind_resume_kfunc(const struct bpf_insn *insn);
int bpf_compute_const_regs(struct bpf_verifier_env *env);
int bpf_prune_dead_branches(struct bpf_verifier_env *env);
int bpf_check_cfg(struct bpf_verifier_env *env);
diff --git a/include/linux/filter.h b/include/linux/filter.h
index b17222db2efc3..287cd9b59aa9e 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -1242,6 +1242,8 @@ bool bpf_jit_supports_stack_args(void);
bool bpf_jit_supports_arena_args(void);
bool bpf_jit_supports_far_kfunc_call(void);
bool bpf_jit_supports_exceptions(void);
+bool bpf_jit_supports_cleanup_pads(void);
+void arch_bpf_run_cleanup_pad(u64 pad, u64 frame_fp, u64 spill_base);
bool bpf_jit_supports_ptr_xchg(void);
bool bpf_jit_supports_arena(void);
bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena);
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 732b35cc08d1c..f7dc121be0947 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -1669,6 +1669,9 @@ union bpf_attr {
* verification.
*/
__s32 keyring_id;
+ __aligned_u64 cleanup_info; /* exception cleanup table */
+ __u32 cleanup_info_rec_size; /* userspace bpf_cleanup_info size */
+ __u32 cleanup_info_cnt; /* number of bpf_cleanup_info records */
};
struct { /* anonymous struct used by BPF_OBJ_* commands */
@@ -7588,6 +7591,12 @@ struct bpf_line_info {
__u32 line_col;
};
+struct bpf_cleanup_info {
+ __u32 begin_off;
+ __u32 end_off;
+ __u32 landing_pad_off;
+};
+
struct bpf_spin_lock {
__u32 val;
};
diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile
index 9a92c348bbda6..af9bc60428ad1 100644
--- a/kernel/bpf/Makefile
+++ b/kernel/bpf/Makefile
@@ -11,7 +11,7 @@ obj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_
obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o
obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o bpf_insn_array.o
obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o
-obj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o backtrack.o check_btf.o
+obj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o backtrack.o check_btf.o exception.o
obj-${CONFIG_BPF_LSM} += bpf_inode_storage.o
obj-$(CONFIG_BPF_SYSCALL) += disasm.o mprog.o
obj-$(CONFIG_BPF_JIT) += trampoline.o
diff --git a/kernel/bpf/cfg.c b/kernel/bpf/cfg.c
index 842c7d1eabccc..5a2b48b6a9e1d 100644
--- a/kernel/bpf/cfg.c
+++ b/kernel/bpf/cfg.c
@@ -6,6 +6,7 @@
#include <linux/sort.h>
#include "diagnostics.h"
+#include "exception.h"
#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
@@ -158,17 +159,57 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
return DONE_EXPLORING;
}
+static int visit_cleanup_pad_edge(int t, struct bpf_verifier_env *env)
+{
+ int *insn_stack = env->cfg.insn_stack;
+ int *insn_state = env->cfg.insn_state;
+ int w;
+
+ if (!env->cleanup_info_cnt)
+ return DONE_EXPLORING;
+ w = bpf_cleanup_pad_of_call(env, t);
+ if (w < 0)
+ return DONE_EXPLORING;
+
+ mark_prune_point(env, t);
+ mark_jmp_point(env, w);
+ mark_jump_target(env, w);
+
+ if (insn_state[w])
+ return DONE_EXPLORING;
+ if (env->cfg.cur_stack >= env->prog->len)
+ return -E2BIG;
+ insn_stack[env->cfg.cur_stack++] = w;
+ insn_state[w] |= DISCOVERED;
+ return KEEP_EXPLORING;
+}
+
+static int merge_visit_ret(int a, int b)
+{
+ if (a < 0)
+ return a;
+ if (b < 0)
+ return b;
+ if (a == KEEP_EXPLORING || b == KEEP_EXPLORING)
+ return KEEP_EXPLORING;
+ return DONE_EXPLORING;
+}
+
static int visit_func_call_insn(int t, struct bpf_insn *insns,
struct bpf_verifier_env *env,
bool visit_callee)
{
- int ret, insn_sz;
+ int ret, insn_sz, pad_ret;
int w;
+ pad_ret = visit_cleanup_pad_edge(t, env);
+ if (pad_ret < 0)
+ return pad_ret;
+
insn_sz = bpf_is_ldimm64(&insns[t]) ? 2 : 1;
ret = push_insn(t, t + insn_sz, FALLTHROUGH, env);
if (ret)
- return ret;
+ return merge_visit_ret(pad_ret, ret);
mark_prune_point(env, t + insn_sz);
/* when we exit from subprog, we need to record non-linear history */
@@ -180,7 +221,7 @@ static int visit_func_call_insn(int t, struct bpf_insn *insns,
merge_callee_effects(env, t, w);
ret = push_insn(t, w, BRANCH, env);
}
- return ret;
+ return merge_visit_ret(pad_ret, ret);
}
struct bpf_iarray *bpf_iarray_realloc(struct bpf_iarray *old, size_t n_elem)
@@ -592,6 +633,7 @@ int bpf_check_cfg(struct bpf_verifier_env *env)
int insn_cnt = env->prog->len;
int *insn_stack, *insn_state;
int ex_insn_beg, i, ret = 0;
+ u32 pad_idx = 0;
insn_state = env->cfg.insn_state = kvzalloc_objs(int, insn_cnt,
GFP_KERNEL_ACCOUNT);
@@ -647,6 +689,22 @@ int bpf_check_cfg(struct bpf_verifier_env *env)
goto walk_cfg;
}
+ /*
+ * A landing pad no call site was marked with -- a record whose range
+ * holds no call an exception can unwind out of -- is reached by
+ * nothing. Walk it from here, and let the dead code sweep remove it.
+ */
+ while (pad_idx < env->cleanup_info_cnt) {
+ u32 pad = env->cleanup_info[pad_idx++].landing_pad_off;
+
+ if (insn_state[pad] != EXPLORED) {
+ insn_state[pad] = DISCOVERED;
+ insn_stack[0] = pad;
+ env->cfg.cur_stack = 1;
+ goto walk_cfg;
+ }
+ }
+
for (i = 0; i < insn_cnt; i++) {
struct bpf_insn *insn = &env->prog->insnsi[i];
diff --git a/kernel/bpf/check_btf.c b/kernel/bpf/check_btf.c
index 0e8b3ccc7a5b9..d03dc791042a2 100644
--- a/kernel/bpf/check_btf.c
+++ b/kernel/bpf/check_btf.c
@@ -407,6 +407,149 @@ static int check_core_relo(struct bpf_verifier_env *env,
return err;
}
+static int cleanup_insn_subprog(struct bpf_verifier_env *env, u32 off)
+{
+ struct bpf_subprog_info *info;
+
+ if (off >= env->prog->len)
+ return -1;
+ info = bpf_find_containing_subprog(env, off);
+ return info ? info - env->subprog_info : -1;
+}
+
+#define MIN_BPF_CLEANUP_INFO_SIZE 12
+#define MAX_CLEANUP_INFO_REC_SIZE MAX_FUNCINFO_REC_SIZE
+
+static int check_cleanup_info(struct bpf_verifier_env *env,
+ const union bpf_attr *attr,
+ bpfptr_t uattr)
+{
+ u32 krec_size = sizeof(struct bpf_cleanup_info);
+ u32 i, nrec, urec_size, min_size, prev_end = 0;
+ struct bpf_cleanup_info *krecord;
+ bpfptr_t urecord;
+ int ret = -EINVAL;
+
+ nrec = attr->cleanup_info_cnt;
+ if (!nrec)
+ return 0;
+ if (nrec > INT_MAX / krec_size)
+ return -EINVAL;
+
+ urec_size = attr->cleanup_info_rec_size;
+ if (urec_size < MIN_BPF_CLEANUP_INFO_SIZE ||
+ urec_size > MAX_CLEANUP_INFO_REC_SIZE ||
+ urec_size % sizeof(u32)) {
+ verbose(env, "invalid cleanup info rec size %u\n", urec_size);
+ return -EINVAL;
+ }
+
+ krecord = kvcalloc(nrec, krec_size, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!krecord)
+ return -ENOMEM;
+
+ min_size = min_t(u32, krec_size, urec_size);
+ urecord = make_bpfptr(attr->cleanup_info, uattr.is_kernel);
+ for (i = 0; i < nrec; i++) {
+ struct bpf_cleanup_info *rec = &krecord[i];
+ int sb, se, sl;
+
+ ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size);
+ if (ret) {
+ if (ret == -E2BIG) {
+ verbose(env, "nonzero tailing record in cleanup info\n");
+ if (copy_to_bpfptr_offset(uattr,
+ offsetof(union bpf_attr,
+ cleanup_info_rec_size),
+ &min_size, sizeof(min_size)))
+ ret = -EFAULT;
+ }
+ goto err_free;
+ }
+
+ if (copy_from_bpfptr(rec, urecord, min_size)) {
+ ret = -EFAULT;
+ goto err_free;
+ }
+ bpfptr_add(&urecord, urec_size);
+
+ ret = -EINVAL;
+ if (rec->begin_off >= rec->end_off) {
+ verbose(env, "cleanup_info[%u]: begin %u >= end %u\n",
+ i, rec->begin_off, rec->end_off);
+ goto err_free;
+ }
+ if (i && rec->begin_off < prev_end) {
+ verbose(env,
+ "cleanup_info[%u]: range [%u,%u) is unsorted or overlaps the previous record\n",
+ i, rec->begin_off, rec->end_off);
+ goto err_free;
+ }
+ prev_end = rec->end_off;
+
+ sb = cleanup_insn_subprog(env, rec->begin_off);
+ se = cleanup_insn_subprog(env, rec->end_off - 1);
+ sl = cleanup_insn_subprog(env, rec->landing_pad_off);
+ if (sb < 0 || se < 0 || sl < 0) {
+ verbose(env, "cleanup_info[%u]: offset out of range\n", i);
+ goto err_free;
+ }
+ if (sb != se || sb != sl) {
+ verbose(env,
+ "cleanup_info[%u]: range/landing pad span multiple subprogs\n",
+ i);
+ goto err_free;
+ }
+ /*
+ * The second half of a 16-byte instruction carries a zero
+ * opcode and is not an instruction of its own, so no offset
+ * may name one. end_off is exclusive, so it may also be one
+ * past the last instruction of the program.
+ */
+ if (!env->prog->insnsi[rec->begin_off].code ||
+ !env->prog->insnsi[rec->landing_pad_off].code ||
+ (rec->end_off < env->prog->len &&
+ !env->prog->insnsi[rec->end_off].code)) {
+ verbose(env, "cleanup_info[%u]: points at invalid insn\n", i);
+ goto err_free;
+ }
+ }
+
+ /*
+ * Reject a landing pad that lies inside a call-site range, its own
+ * included: it would be both a pad and a call that unwinds to one, and
+ * an exception out of it would have nowhere to go.
+ */
+ ret = -EINVAL;
+ for (i = 0; i < nrec; i++) {
+ u32 pad = krecord[i].landing_pad_off;
+ u32 l = 0, r = nrec;
+
+ while (l < r) {
+ u32 m = l + (r - l) / 2;
+
+ if (pad < krecord[m].begin_off) {
+ r = m;
+ } else if (pad >= krecord[m].end_off) {
+ l = m + 1;
+ } else {
+ verbose(env,
+ "cleanup_info[%u]: landing pad %u is inside the call-site range of cleanup_info[%u]\n",
+ i, pad, m);
+ goto err_free;
+ }
+ }
+ }
+
+ env->cleanup_info = krecord;
+ env->cleanup_info_cnt = nrec;
+ return 0;
+
+err_free:
+ kvfree(krecord);
+ return ret;
+}
+
int bpf_prepare_btf_info(struct bpf_verifier_env *env,
const union bpf_attr *attr,
bpfptr_t uattr)
@@ -441,6 +584,10 @@ int bpf_check_btf_info(struct bpf_verifier_env *env,
{
int err;
+ err = check_cleanup_info(env, attr, uattr);
+ if (err)
+ return err;
+
if (!attr->func_info_cnt && !attr->line_info_cnt) {
if (check_abnormal_return(env))
return -EINVAL;
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 4e208cc94752f..bd2919063cecd 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -292,6 +292,7 @@ void __bpf_prog_free(struct bpf_prog *fp)
mutex_destroy(&fp->aux->dst_mutex);
mutex_destroy(&fp->aux->st_ops_assoc_mutex);
kfree(fp->aux->poke_tab);
+ bpf_cleanup_free_info(fp->aux);
kfree(fp->aux);
}
free_percpu(fp->stats);
@@ -2625,13 +2626,21 @@ static bool bpf_prog_select_interpreter(struct bpf_prog *fp)
return select_interpreter;
}
-static struct bpf_prog *bpf_prog_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)
+static struct bpf_prog *bpf_prog_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog,
+ int *err)
{
#ifdef CONFIG_BPF_JIT
struct bpf_prog *orig_prog;
+ int ret;
- if (!bpf_prog_need_blind(prog))
+ if (!bpf_prog_need_blind(prog)) {
+ ret = bpf_cleanup_attach_main_prog(env, prog);
+ if (ret) {
+ *err = ret;
+ return prog;
+ }
return bpf_int_jit_compile(env, prog);
+ }
orig_prog = prog;
prog = bpf_jit_blind_constants(env, prog);
@@ -2642,6 +2651,13 @@ static struct bpf_prog *bpf_prog_jit_compile(struct bpf_verifier_env *env, struc
if (IS_ERR(prog))
goto out_restore;
+ ret = bpf_cleanup_attach_main_prog(env, prog);
+ if (ret) {
+ *err = ret;
+ bpf_jit_prog_release_other(orig_prog, prog);
+ goto out_restore;
+ }
+
prog = bpf_int_jit_compile(env, prog);
if (prog->jited) {
bpf_jit_prog_release_other(prog, orig_prog);
@@ -2681,8 +2697,10 @@ struct bpf_prog *__bpf_prog_select_runtime(struct bpf_verifier_env *env, struct
if (*err)
return fp;
- fp = bpf_prog_jit_compile(env, fp);
+ fp = bpf_prog_jit_compile(env, fp, err);
bpf_prog_jit_attempt_done(fp);
+ if (*err)
+ return fp;
if (!fp->jited && jit_needed) {
*err = -ENOTSUPP;
return fp;
@@ -3470,6 +3488,17 @@ void __weak arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp,
{
}
+bool __weak bpf_jit_supports_cleanup_pads(void)
+{
+ return false;
+}
+
+/* Call @pad with the frame pointer @frame_fp and r6-r9 spilled at @spill_base. */
+void __weak arch_bpf_run_cleanup_pad(u64 pad, u64 frame_fp, u64 spill_base)
+{
+ WARN_ON_ONCE(1);
+}
+
bool __weak bpf_jit_supports_timed_may_goto(void)
{
return false;
diff --git a/kernel/bpf/exception.c b/kernel/bpf/exception.c
new file mode 100644
index 0000000000000..521086d084a35
--- /dev/null
+++ b/kernel/bpf/exception.c
@@ -0,0 +1,654 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <linux/bitmap.h>
+#include <linux/bpf.h>
+#include <linux/bpf_verifier.h>
+#include <linux/btf.h>
+#include <linux/btf_ids.h>
+#include <linux/filter.h>
+#include <linux/slab.h>
+#include <linux/sort.h>
+#include "exception.h"
+
+#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
+
+enum exc_kfunc {
+ EXC_KF_bpf_unwind_resume,
+};
+
+BTF_ID_LIST(exc_kfunc_list)
+BTF_ID(func, bpf_unwind_resume)
+
+static bool insn_is_exc_kfunc(const struct bpf_insn *insn, int kf)
+{
+ return bpf_pseudo_kfunc_call(insn) && insn->off == 0 &&
+ insn->imm == exc_kfunc_list[kf];
+}
+
+/* What an instruction does to intra-subprog control flow. */
+enum cleanup_insn_kind {
+ CLEANUP_INSN_PLAIN, /* the next insn runs */
+ CLEANUP_INSN_JUMP, /* unconditional jump */
+ CLEANUP_INSN_COND, /* the next insn runs, or the branch target */
+ CLEANUP_INSN_EXIT,
+ CLEANUP_INSN_THROW, /* call bpf_throw: nothing after it runs */
+ CLEANUP_INSN_RESUME, /* call bpf_unwind_resume: likewise */
+ CLEANUP_INSN_CALL, /* call to another subprog */
+ CLEANUP_INSN_GOTOX, /* indirect jump: successors not known here */
+};
+
+/* What each instruction can reach, computed once by cleanup_reachability(). */
+#define CLEANUP_REACH_RESUME BIT(0) /* a bpf_unwind_resume() call */
+#define CLEANUP_REACH_EXIT BIT(1) /* a plain BPF_EXIT */
+#define CLEANUP_REACH_UNKNOWN BIT(2) /* an indirect jump */
+#define CLEANUP_REACH_THROW BIT(3) /* a bpf_throw() call */
+
+/* Scratch shared by the analyses, sized once so no walker has to allocate. */
+struct cleanup_ctx {
+ struct bpf_verifier_env *env;
+ u8 *reach; /* per insn: CLEANUP_REACH_* mask */
+ u32 *stack; /* per insn: DFS stack */
+ void *scratch; /* the one allocation all of the above live in */
+};
+
+static bool in_pad(struct bpf_verifier_env *env, u32 i)
+{
+ return env->insn_aux_data[i].in_cleanup_pad;
+}
+
+/* One scratch array for cleanup_alloc() to hand out. */
+struct cleanup_alloc_req {
+ void **dst;
+ size_t n, sz;
+};
+
+static void *cleanup_alloc(const struct cleanup_alloc_req *tab, u32 cnt)
+{
+ size_t total = 0;
+ char *block, *p;
+ u32 i;
+
+ for (i = 0; i < cnt; i++)
+ total += round_up(tab[i].n * tab[i].sz, 8);
+
+ block = kvzalloc(total, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!block)
+ return NULL;
+
+ for (i = 0, p = block; i < cnt; i++) {
+ *tab[i].dst = p;
+ p += round_up(tab[i].n * tab[i].sz, 8);
+ }
+ return block;
+}
+
+static int cleanup_subprog_of(struct bpf_verifier_env *env, u32 off)
+{
+ struct bpf_subprog_info *info = bpf_find_containing_subprog(env, off);
+
+ return info ? info - env->subprog_info : -1;
+}
+
+/* The subprogram a linear pass is currently in. */
+struct cleanup_cursor {
+ u32 start, end; /* [start, end) of the current subprogram */
+ int sub; /* its index */
+};
+
+#define CLEANUP_CURSOR_INIT { .sub = -1 }
+
+static void cleanup_cursor_to(struct bpf_verifier_env *env, struct cleanup_cursor *c, u32 i)
+{
+ while (i >= c->end) {
+ c->sub++;
+ c->start = env->subprog_info[c->sub].start;
+ c->end = env->subprog_info[c->sub + 1].start;
+ }
+}
+
+static enum cleanup_insn_kind cleanup_classify(struct bpf_verifier_env *env, u32 i,
+ int *next, int *target)
+{
+ struct bpf_insn *insn = &env->prog->insnsi[i];
+ u8 class = BPF_CLASS(insn->code);
+
+ *next = i + 1;
+ *target = -1;
+
+ if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) {
+ *next = i + 2;
+ return CLEANUP_INSN_PLAIN;
+ }
+ if (class != BPF_JMP && class != BPF_JMP32)
+ return CLEANUP_INSN_PLAIN;
+
+ switch (BPF_OP(insn->code)) {
+ case BPF_EXIT:
+ *next = -1;
+ return CLEANUP_INSN_EXIT;
+ case BPF_JA:
+ *next = -1;
+ if (BPF_SRC(insn->code) == BPF_X)
+ return CLEANUP_INSN_GOTOX;
+ *target = class == BPF_JMP32 ? i + insn->imm + 1 : i + insn->off + 1;
+ return CLEANUP_INSN_JUMP;
+ case BPF_CALL:
+ if (bpf_is_throw_kfunc(insn)) {
+ *next = -1;
+ return CLEANUP_INSN_THROW;
+ }
+ if (insn_is_exc_kfunc(insn, EXC_KF_bpf_unwind_resume)) {
+ *next = -1;
+ return CLEANUP_INSN_RESUME;
+ }
+ return bpf_pseudo_call(insn) ? CLEANUP_INSN_CALL : CLEANUP_INSN_PLAIN;
+ default:
+ /* Conditional jump, including BPF_JCOND. */
+ *target = i + insn->off + 1;
+ return CLEANUP_INSN_COND;
+ }
+}
+
+static void cleanup_mark_throw_sites(struct bpf_verifier_env *env)
+{
+ u32 i;
+
+ for (i = 0; i < env->prog->len; i++)
+ if (bpf_is_throw_kfunc(&env->prog->insnsi[i]))
+ env->insn_aux_data[i].cleanup_throw_site = true;
+}
+
+int bpf_cleanup_check_callback(struct bpf_verifier_env *env, int subprog)
+{
+ if (!env->cleanup_info_cnt || !env->subprog_info[subprog].might_throw)
+ return 0;
+
+ verbose(env, "subprog %d may unwind and is used as a callback\n", subprog);
+ return -EINVAL;
+}
+
+/* Intra-subprog successors of @i, or -1 each when absent. */
+static enum cleanup_insn_kind cleanup_succ(struct bpf_verifier_env *env, u32 i,
+ u32 start, u32 end, int *next, int *target)
+{
+ enum cleanup_insn_kind kind = cleanup_classify(env, i, next, target);
+
+ if (*next < (int)start || *next >= (int)end)
+ *next = -1;
+ if (*target < (int)start || *target >= (int)end)
+ *target = -1;
+ return kind;
+}
+
+static void cleanup_add_pred(u32 *head, u32 *link, u32 to, u32 e)
+{
+ link[e] = head[to];
+ head[to] = e + 1;
+}
+
+/* What every instruction can reach along intra-subprog edges, for
+ * cleanup_pad_is_catch(). One backward walk over a predecessor index, rather
+ * than a forward walk from each landing pad, which would be quadratic.
+ */
+static int cleanup_reachability(struct cleanup_ctx *ctx)
+{
+ struct bpf_verifier_env *env = ctx->env;
+ u32 len = env->prog->len;
+ struct cleanup_cursor c = CLEANUP_CURSOR_INIT;
+ u32 *head = NULL, *link = NULL;
+ bool *queued = NULL;
+ u32 i, sp = 0;
+ void *scratch;
+ const struct cleanup_alloc_req tab[] = {
+ { (void **)&head, len, sizeof(*head) },
+ { (void **)&link, 2 * (size_t)len, sizeof(*link) },
+ { (void **)&queued, len, sizeof(*queued) },
+ };
+
+ scratch = cleanup_alloc(tab, ARRAY_SIZE(tab));
+ if (!scratch)
+ return -ENOMEM;
+
+ /* Index the predecessors, and seed the walk at the terminators. */
+ for (i = 0; i < len; i++) {
+ enum cleanup_insn_kind kind;
+ int next, target;
+
+ cleanup_cursor_to(env, &c, i);
+ kind = cleanup_succ(env, i, c.start, c.end, &next, &target);
+
+ if (kind == CLEANUP_INSN_RESUME)
+ ctx->reach[i] |= CLEANUP_REACH_RESUME;
+ else if (kind == CLEANUP_INSN_EXIT)
+ ctx->reach[i] |= CLEANUP_REACH_EXIT;
+ else if (kind == CLEANUP_INSN_GOTOX)
+ ctx->reach[i] |= CLEANUP_REACH_UNKNOWN;
+ else if (kind == CLEANUP_INSN_THROW)
+ ctx->reach[i] |= CLEANUP_REACH_THROW;
+
+ if (next >= 0)
+ cleanup_add_pred(head, link, next, 2 * i);
+ if (target >= 0)
+ cleanup_add_pred(head, link, target, 2 * i + 1);
+
+ if (ctx->reach[i]) {
+ queued[i] = true;
+ ctx->stack[sp++] = i;
+ }
+ }
+
+ /* Each instruction re-enters the worklist at most once per bit it
+ * gains, so this is linear in the number of edges.
+ */
+ while (sp) {
+ u32 j = ctx->stack[--sp];
+ u8 flags = ctx->reach[j];
+ u32 e;
+
+ queued[j] = false;
+ for (e = head[j]; e; e = link[e - 1]) {
+ u32 p = (e - 1) / 2;
+
+ if ((ctx->reach[p] | flags) == ctx->reach[p])
+ continue;
+ ctx->reach[p] |= flags;
+ if (!queued[p]) {
+ queued[p] = true;
+ ctx->stack[sp++] = p;
+ }
+ }
+ }
+ kvfree(scratch);
+ return 0;
+}
+
+static int cleanup_pad_is_catch(struct cleanup_ctx *ctx, u32 pad)
+{
+ u8 reach = ctx->reach[pad];
+
+ if (reach & CLEANUP_REACH_UNKNOWN) {
+ verbose(ctx->env, "cleanup landing pad %u reaches an indirect jump\n", pad);
+ return -EINVAL;
+ }
+ if (reach & CLEANUP_REACH_THROW) {
+ verbose(ctx->env,
+ "cleanup landing pad %u can throw while an exception is in flight\n",
+ pad);
+ return -EINVAL;
+ }
+ if (!(reach & CLEANUP_REACH_RESUME) == !(reach & CLEANUP_REACH_EXIT)) {
+ verbose(ctx->env, "cleanup landing pad %u %s\n", pad,
+ (reach & CLEANUP_REACH_RESUME) ?
+ "reaches both bpf_unwind_resume() and a plain exit" :
+ "reaches neither bpf_unwind_resume() nor an exit");
+ return -EINVAL;
+ }
+ return !!(reach & CLEANUP_REACH_EXIT);
+}
+
+static int cleanup_check_pad_insn(struct bpf_verifier_env *env, u32 i)
+{
+ struct bpf_insn *insn = &env->prog->insnsi[i];
+
+ if (bpf_helper_call(insn) && insn->imm == BPF_FUNC_tail_call) {
+ verbose(env,
+ "bpf_tail_call() at insn %u is in an exception cleanup landing pad\n",
+ i);
+ return -EINVAL;
+ }
+ /* Stack arguments are not supported. */
+ if (is_stack_arg_st(insn) || is_stack_arg_stx(insn)) {
+ verbose(env,
+ "insn %u passes an on-stack call argument in an exception cleanup landing pad\n",
+ i);
+ return -EINVAL;
+ }
+ /* Likewise, stack arguments are not supported. */
+ if (bpf_pseudo_kfunc_call(insn)) {
+ struct bpf_call_summary cs;
+
+ if (bpf_get_call_summary(env, insn, &cs) &&
+ cs.arg_slot_cnt > MAX_BPF_FUNC_REG_ARGS) {
+ verbose(env,
+ "insn %u passes an on-stack call argument in an exception cleanup landing pad\n",
+ i);
+ return -EINVAL;
+ }
+ }
+ return 0;
+}
+
+static int cleanup_mark_pad_bodies(struct cleanup_ctx *ctx)
+{
+ struct bpf_verifier_env *env = ctx->env;
+ u32 i, sp = 0;
+ int ret;
+
+ for (i = 0; i < env->cleanup_info_cnt; i++) {
+ u32 pad = env->cleanup_info[i].landing_pad_off;
+
+ if (in_pad(env, pad))
+ continue;
+
+ ret = cleanup_pad_is_catch(ctx, pad);
+ if (ret < 0)
+ return ret;
+ if (ret) {
+ verbose(env,
+ "catch landing pad %u is not supported yet, only cleanup pads that resume\n",
+ pad);
+ return -EOPNOTSUPP;
+ }
+ env->insn_aux_data[pad].in_cleanup_pad = true;
+ ctx->stack[sp++] = pad;
+ }
+
+ while (sp) {
+ u32 j = ctx->stack[--sp];
+ enum cleanup_insn_kind kind;
+ int next, target, sub;
+ u32 start, end;
+
+ ret = cleanup_check_pad_insn(env, j);
+ if (ret)
+ return ret;
+
+ sub = cleanup_subprog_of(env, j);
+ start = env->subprog_info[sub].start;
+ end = env->subprog_info[sub + 1].start;
+ kind = cleanup_succ(env, j, start, end, &next, &target);
+
+ if (kind == CLEANUP_INSN_CALL) {
+ int callee = cleanup_subprog_of(env, j + env->prog->insnsi[j].imm + 1);
+
+ if (env->subprog_info[callee].might_throw) {
+ verbose(env,
+ "cleanup landing pad calls subprog %d at insn %u, which can throw while an exception is in flight\n",
+ callee, j);
+ return -EINVAL;
+ }
+ }
+
+ if (next >= 0 && !in_pad(env, next)) {
+ env->insn_aux_data[next].in_cleanup_pad = true;
+ ctx->stack[sp++] = next;
+ }
+ if (target >= 0 && !in_pad(env, target)) {
+ env->insn_aux_data[target].in_cleanup_pad = true;
+ ctx->stack[sp++] = target;
+ }
+ }
+ return 0;
+}
+
+static int cleanup_check_resumes(struct cleanup_ctx *ctx)
+{
+ struct bpf_verifier_env *env = ctx->env;
+ u32 i;
+
+ for (i = 0; i < env->prog->len; i++) {
+ if (!insn_is_exc_kfunc(&env->prog->insnsi[i], EXC_KF_bpf_unwind_resume))
+ continue;
+ if (in_pad(env, i))
+ continue;
+ verbose(env,
+ "bpf_unwind_resume() at insn %u is not in an exception cleanup landing pad\n",
+ i);
+ return -EINVAL;
+ }
+ return 0;
+}
+
+static void cleanup_mark_call_sites(struct bpf_verifier_env *env)
+{
+ u32 i, j;
+
+ for (i = 0; i < env->cleanup_info_cnt; i++) {
+ struct bpf_cleanup_info *rec = &env->cleanup_info[i];
+
+ for (j = rec->begin_off; j < rec->end_off; j++) {
+ struct bpf_insn *insn = &env->prog->insnsi[j];
+
+ if (!bpf_pseudo_call(insn) && !bpf_is_throw_kfunc(insn))
+ continue;
+ env->insn_aux_data[j].cleanup_pad = rec->landing_pad_off + 1;
+ }
+ }
+}
+
+int bpf_prepare_cleanup_exceptions(struct bpf_verifier_env *env)
+{
+ if (!env->cleanup_info_cnt)
+ return 0;
+
+ if (bpf_prog_is_offloaded(env->prog->aux)) {
+ verbose(env,
+ "exception cleanup is not supported for offloaded programs\n");
+ return -EINVAL;
+ }
+
+ if (!bpf_jit_supports_cleanup_pads() || !env->prog->jit_requested) {
+ verbose(env,
+ "exception cleanup needs a JIT that can dispatch landing pads\n");
+ return -EOPNOTSUPP;
+ }
+ env->prog->jit_required = 1;
+
+ if (env->exception_callback_subprog) {
+ verbose(env,
+ "exception cleanup table cannot be combined with an exception callback\n");
+ return -EINVAL;
+ }
+
+ cleanup_mark_throw_sites(env);
+ cleanup_mark_call_sites(env);
+ return 0;
+}
+
+int bpf_check_cleanup_exceptions(struct bpf_verifier_env *env)
+{
+ u32 len = env->prog->len;
+ struct cleanup_ctx ctx = { .env = env };
+ const struct cleanup_alloc_req tab[] = {
+ { (void **)&ctx.reach, len, sizeof(*ctx.reach) },
+ { (void **)&ctx.stack, len, sizeof(*ctx.stack) },
+ };
+ int ret;
+
+ if (!env->cleanup_info_cnt)
+ return 0;
+
+ ctx.scratch = cleanup_alloc(tab, ARRAY_SIZE(tab));
+ if (!ctx.scratch)
+ return -ENOMEM;
+
+ ret = cleanup_reachability(&ctx);
+ if (ret)
+ goto out;
+
+ ret = cleanup_mark_pad_bodies(&ctx);
+ if (ret)
+ goto out;
+
+ ret = cleanup_check_resumes(&ctx);
+out:
+ kvfree(ctx.scratch);
+ return ret;
+}
+
+bool bpf_is_unwind_resume_kfunc(const struct bpf_insn *insn)
+{
+ return insn_is_exc_kfunc(insn, EXC_KF_bpf_unwind_resume);
+}
+
+int bpf_cleanup_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;
+}
+
+/*
+ * Every subprogram of a cleanup-carrying program spills the BPF callee-saved
+ * registers, even one that never throws: a frame's spill holds its caller's
+ * registers, and that is what the walker restores before running the caller's
+ * pad. The exception callback does not, because it reuses the boundary frame
+ * rather than building one of its own.
+ */
+bool bpf_cleanup_force_spill(const struct bpf_prog *prog)
+{
+ return prog->aux->exc && !prog->aux->exception_cb;
+}
+
+const struct bpf_cleanup_range *bpf_cleanup_pad_for_ip(const struct bpf_prog *prog, u64 ip)
+{
+ const struct bpf_exception_info *exc = prog->aux->exc;
+ u32 l = 0, r = exc ? exc->nr_ranges : 0;
+
+ while (l < r) {
+ u32 m = l + (r - l) / 2;
+ const struct bpf_cleanup_range *rec = &exc->ranges[m];
+
+ if (ip <= rec->begin)
+ r = m;
+ else if (ip > rec->end)
+ l = m + 1;
+ else
+ return rec;
+ }
+ return NULL;
+}
+
+static int cmp_u32(const void *a, const void *b)
+{
+ u32 x = *(const u32 *)a, y = *(const u32 *)b;
+
+ return x < y ? -1 : x > y;
+}
+
+int bpf_cleanup_alloc_info(struct bpf_prog_aux *aux)
+{
+ if (aux->exc)
+ return 0;
+ aux->exc = kzalloc_obj(struct bpf_exception_info, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ return aux->exc ? 0 : -ENOMEM;
+}
+
+int bpf_cleanup_attach_info(struct bpf_prog_aux *aux, struct bpf_cleanup_info *recs, u32 cnt)
+{
+ struct bpf_exception_info *exc = aux->exc;
+ struct bpf_cleanup_range *ranges;
+ u32 i, n_at, *at;
+
+ if (!cnt) {
+ kvfree(recs);
+ return 0;
+ }
+
+ ranges = kvcalloc(cnt, sizeof(*ranges), GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!ranges) {
+ kvfree(recs);
+ return -ENOMEM;
+ }
+
+ /* The pads on their own, sorted and deduplicated. */
+ at = kvmalloc_array(cnt, sizeof(*at), GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!at) {
+ kvfree(ranges);
+ kvfree(recs);
+ return -ENOMEM;
+ }
+ for (i = 0; i < cnt; i++)
+ at[i] = recs[i].landing_pad_off;
+ sort(at, cnt, sizeof(*at), cmp_u32, NULL);
+ for (i = 0, n_at = 0; i < cnt; i++)
+ if (!n_at || at[n_at - 1] != at[i])
+ at[n_at++] = at[i];
+
+ exc->pad_at = at;
+ exc->nr_pad_at = n_at;
+ exc->info = recs;
+ exc->nr_info = cnt;
+ exc->ranges = ranges;
+ /* Withheld until the JIT has filled the table in. */
+ exc->nr_ranges = 0;
+ return 0;
+}
+
+void bpf_cleanup_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image)
+{
+ struct bpf_exception_info *exc = prog->aux->exc;
+ u32 i, n;
+
+ if (!exc || !exc->nr_info || !exc->ranges)
+ return;
+
+ n = exc->nr_info;
+ for (i = 0; i < n; i++) {
+ const struct bpf_cleanup_info *rec = &exc->info[i];
+
+ if (WARN_ON_ONCE(rec->begin_off >= prog->len ||
+ rec->end_off > prog->len ||
+ rec->landing_pad_off >= prog->len))
+ return;
+ exc->ranges[i].begin = (u64)(long)image + addrs[rec->begin_off];
+ exc->ranges[i].end = (u64)(long)image + addrs[rec->end_off];
+ exc->ranges[i].pad = (u64)(long)image + addrs[rec->landing_pad_off];
+ }
+ exc->nr_ranges = n;
+}
+
+void bpf_cleanup_free_info(struct bpf_prog_aux *aux)
+{
+ struct bpf_exception_info *exc = aux->exc;
+
+ if (!exc)
+ return;
+ kvfree(exc->ranges);
+ kvfree(exc->info);
+ kvfree(exc->pad_at);
+ kvfree(exc->throw_at);
+ bitmap_free(exc->pad_body);
+ kfree(exc);
+ aux->exc = NULL;
+}
+
+/* Is @idx in the sorted array @at of @n instruction indices? */
+static bool insn_idx_in(const u32 *at, u32 n, u32 idx)
+{
+ u32 l = 0, r = n;
+
+ while (l < r) {
+ u32 m = l + (r - l) / 2;
+
+ if (idx < at[m])
+ r = m;
+ else if (idx > at[m])
+ l = m + 1;
+ else
+ return true;
+ }
+ return false;
+}
+
+bool bpf_cleanup_insn_is_pad(const struct bpf_prog *prog, u32 idx)
+{
+ const struct bpf_exception_info *exc = prog->aux->exc;
+
+ return exc && insn_idx_in(exc->pad_at, exc->nr_pad_at, idx);
+}
+
+bool bpf_cleanup_insn_is_throw(const struct bpf_prog *prog, u32 idx)
+{
+ const struct bpf_exception_info *exc = prog->aux->exc;
+
+ return exc && insn_idx_in(exc->throw_at, exc->nr_throw_at, idx);
+}
+
+bool bpf_cleanup_insn_in_pad(const struct bpf_prog *prog, u32 idx)
+{
+ const struct bpf_exception_info *exc = prog->aux->exc;
+
+ return exc && exc->pad_body && idx < exc->nr_pad_body &&
+ test_bit(idx, exc->pad_body);
+}
diff --git a/kernel/bpf/exception.h b/kernel/bpf/exception.h
new file mode 100644
index 0000000000000..c0e68ce227c83
--- /dev/null
+++ b/kernel/bpf/exception.h
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#ifndef _LINUX_BPF_EXCEPTION_H
+#define _LINUX_BPF_EXCEPTION_H
+
+#include <linux/types.h>
+
+struct bpf_cleanup_info;
+struct bpf_cleanup_range;
+struct bpf_prog;
+struct bpf_prog_aux;
+struct bpf_verifier_env;
+
+int bpf_prepare_cleanup_exceptions(struct bpf_verifier_env *env);
+int bpf_check_cleanup_exceptions(struct bpf_verifier_env *env);
+int bpf_cleanup_check_callback(struct bpf_verifier_env *env, int subprog);
+int bpf_cleanup_pad_of_call(struct bpf_verifier_env *env, u32 idx);
+int bpf_cleanup_alloc_info(struct bpf_prog_aux *aux);
+int bpf_cleanup_attach_info(struct bpf_prog_aux *aux, struct bpf_cleanup_info *recs, u32 cnt);
+const struct bpf_cleanup_range *bpf_cleanup_pad_for_ip(const struct bpf_prog *prog, u64 ip);
+
+#endif /* _LINUX_BPF_EXCEPTION_H */
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index 2add8001c3ec3..134aafa6a6c9b 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <linux/bitmap.h>
#include <linux/bpf.h>
#include <linux/btf.h>
#include <linux/bpf_verifier.h>
@@ -10,6 +11,7 @@
#include <linux/perf_event.h>
#include <net/xdp.h>
#include "disasm.h"
+#include "exception.h"
#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
@@ -252,15 +254,26 @@ static void adjust_insn_aux_data(struct bpf_verifier_env *env,
/* Expand insni[off]'s seen count to the patched range. */
data[i].seen = old_seen;
data[i].zext_dst = bpf_insn_def32(new_prog, insn + i) >= 0;
+ data[i].in_cleanup_pad = data[off + cnt - 1].in_cleanup_pad;
if (!memcmp(insn + i, original_insn, sizeof(struct bpf_insn))) {
data[i].non_stack_access =
data[off + cnt - 1].non_stack_access;
data[off + cnt - 1].non_stack_access = false;
+ data[i].cleanup_throw_site =
+ data[off + cnt - 1].cleanup_throw_site;
+ data[off + cnt - 1].cleanup_throw_site = 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
@@ -549,6 +562,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_prog_is_offloaded(env->prog->aux))
bpf_prog_offload_remove_insns(env, off, cnt);
@@ -573,6 +587,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;
}
@@ -1095,6 +1120,116 @@ static void bpf_restore_subprog_starts(struct bpf_verifier_env *env, u32 *orig_s
env->subprog_info[env->subprog_cnt].start = env->prog->len;
}
+static int cleanup_throw_sites_for_subprog(struct bpf_verifier_env *env, struct bpf_prog *sub,
+ u32 start, u32 end)
+{
+ u32 i, cnt = 0, *at;
+
+ for (i = start; i < end; i++)
+ if (env->insn_aux_data[i].cleanup_throw_site)
+ cnt++;
+ if (!cnt)
+ return 0;
+
+ at = kvmalloc_array(cnt, sizeof(*at), GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!at)
+ return -ENOMEM;
+
+ for (i = start, cnt = 0; i < end; i++) {
+ if (!env->insn_aux_data[i].cleanup_throw_site)
+ continue;
+ at[cnt++] = i - start;
+ }
+
+ sub->aux->exc->throw_at = at;
+ sub->aux->exc->nr_throw_at = cnt;
+ return 0;
+}
+
+static int cleanup_pad_body_for_subprog(struct bpf_verifier_env *env, struct bpf_prog *sub,
+ u32 start, u32 end)
+{
+ unsigned long *bits;
+ u32 i, cnt = 0;
+
+ for (i = start; i < end; i++)
+ if (env->insn_aux_data[i].in_cleanup_pad)
+ cnt++;
+ if (!cnt)
+ return 0;
+
+ bits = bitmap_zalloc(end - start, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!bits)
+ return -ENOMEM;
+
+ for (i = start; i < end; i++)
+ if (env->insn_aux_data[i].in_cleanup_pad)
+ __set_bit(i - start, bits);
+
+ sub->aux->exc->pad_body = bits;
+ sub->aux->exc->nr_pad_body = end - start;
+ return 0;
+}
+
+static int cleanup_info_for_subprog(struct bpf_verifier_env *env, struct bpf_prog *sub,
+ u32 start, u32 end)
+{
+ struct bpf_cleanup_info *recs;
+ u32 i, cnt = 0;
+ int err;
+
+ if (!env->cleanup_info_cnt)
+ return 0;
+
+ err = bpf_cleanup_alloc_info(sub->aux);
+ if (err)
+ return err;
+
+ err = cleanup_throw_sites_for_subprog(env, sub, start, end);
+ if (err)
+ return err;
+
+ err = cleanup_pad_body_for_subprog(env, sub, start, end);
+ if (err)
+ return err;
+
+ for (i = start; i < end; i++)
+ if (env->insn_aux_data[i].cleanup_pad)
+ cnt++;
+ if (!cnt)
+ return 0;
+
+ recs = kvmalloc_array(cnt, sizeof(*recs), GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!recs)
+ return -ENOMEM;
+
+ for (i = start, cnt = 0; i < end; i++) {
+ u32 pad = env->insn_aux_data[i].cleanup_pad;
+
+ if (!pad)
+ continue;
+ pad--;
+ if (verifier_bug_if(pad < start || pad >= end, env,
+ "insn %u is covered by a landing pad at %u outside its subprog [%u, %u)",
+ i, pad, start, end)) {
+ kvfree(recs);
+ return -EFAULT;
+ }
+ recs[cnt].begin_off = i - start;
+ recs[cnt].end_off = i - start + 1;
+ recs[cnt].landing_pad_off = pad - start;
+ cnt++;
+ }
+ return bpf_cleanup_attach_info(sub->aux, recs, cnt);
+}
+
+int bpf_cleanup_attach_main_prog(struct bpf_verifier_env *env, struct bpf_prog *prog)
+{
+ if (!env || env->subprog_cnt > 1)
+ return 0;
+ return cleanup_info_for_subprog(env, prog, 0, prog->len);
+}
+
static int jit_subprogs(struct bpf_verifier_env *env)
{
struct bpf_prog *prog = env->prog, **func, *tmp;
@@ -1232,6 +1367,10 @@ static int jit_subprogs(struct bpf_verifier_env *env)
func[i]->aux->token = prog->aux->token;
if (!i)
func[i]->aux->exception_boundary = env->seen_exception;
+ err = cleanup_info_for_subprog(env, func[i], subprog_start,
+ env->subprog_info[i + 1].start);
+ if (err)
+ goto out_free;
func[i] = bpf_int_jit_compile(env, func[i]);
if (!func[i]->jited) {
err = -ENOTSUPP;
@@ -1336,6 +1475,8 @@ static int jit_subprogs(struct bpf_verifier_env *env)
prog->aux->bpf_exception_cb = (void *)func[env->exception_callback_subprog]->bpf_func;
prog->aux->exception_boundary = func[0]->aux->exception_boundary;
prog->aux->stack_arg_sp_adjust = func[0]->aux->stack_arg_sp_adjust;
+ prog->aux->exc = func[0]->aux->exc;
+ func[0]->aux->exc = NULL;
bpf_prog_jit_attempt_done(prog);
return 0;
out_free:
@@ -1916,6 +2057,8 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
goto next_insn;
if (insn->src_reg == BPF_PSEUDO_CALL)
goto next_insn;
+ if (bpf_is_unwind_resume_kfunc(insn))
+ goto next_insn;
if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) {
ret = bpf_fixup_kfunc_call(env, insn, insn_buf, i + delta, &cnt);
if (ret)
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 051b6654e57c6..ffef72804fc9a 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -31,6 +31,7 @@
#include <linux/buildid.h>
#include "../../lib/kstrtox.h"
+#include "exception.h"
/* If kernel subsystem is allowing eBPF programs to call this function,
* inside its own verifier_ops->get_func_proto() callback it should return
@@ -3360,8 +3361,36 @@ struct bpf_throw_ctx {
u64 sp;
u64 bp;
int cnt;
+ const struct bpf_prog *callee;
+ u64 callee_fp;
};
+static void bpf_run_cleanup_pad(struct bpf_throw_ctx *ctx, const struct bpf_prog *prog,
+ u64 ip, u64 fp)
+{
+ const struct bpf_exception_info *exc = prog->aux->exc;
+ const struct bpf_cleanup_range *rec;
+ u64 spill_base;
+
+ if (!exc || !exc->nr_ranges)
+ return;
+ rec = bpf_cleanup_pad_for_ip(prog, ip);
+ if (!rec)
+ return;
+
+ /*
+ * The callee is always another subprogram of this program -- the walk
+ * ends at any frame that is not one -- so its prologue spilled these
+ * registers and its exc is there to say where.
+ */
+ if (ctx->callee)
+ spill_base = ctx->callee_fp + ctx->callee->aux->exc->spill_off;
+ else
+ spill_base = fp + exc->throw_spill_off;
+
+ arch_bpf_run_cleanup_pad(rec->pad, fp, spill_base);
+}
+
static bool bpf_stack_walker(void *cookie, u64 ip, u64 sp, u64 bp)
{
struct bpf_throw_ctx *ctx = cookie;
@@ -3378,6 +3407,11 @@ static bool bpf_stack_walker(void *cookie, u64 ip, u64 sp, u64 bp)
if (!prog)
return !ctx->cnt;
ctx->cnt++;
+
+ bpf_run_cleanup_pad(ctx, prog, ip, bp);
+ ctx->callee = prog;
+ ctx->callee_fp = bp;
+
if (bpf_is_subprog(prog))
return true;
ctx->aux = prog->aux;
@@ -3407,6 +3441,17 @@ __bpf_kfunc void bpf_throw(u64 cookie)
WARN(1, "A call to BPF exception callback should never return\n");
}
+/*
+ * Terminator of a compiler-emitted cleanup landing pad. The compiler names
+ * this _Unwind_Resume, the base unwind ABI's entry point for carrying an
+ * unwind on once a frame's cleanups have run. To match kernel kfunc
+ * convention, the kernel calls it bpf_unwind_resume and libbpf maps the
+ * compiler's name onto it.
+ */
+__bpf_kfunc void bpf_unwind_resume(void)
+{
+}
+
__bpf_kfunc int bpf_wq_init(struct bpf_wq *wq, void *p__const_map, unsigned int flags)
{
struct bpf_async_kern *async = (struct bpf_async_kern *)wq;
@@ -4853,6 +4898,7 @@ BTF_ID_FLAGS(func, bpf_task_get_cgroup1, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_task_from_vpid, KF_ACQUIRE | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_throw)
+BTF_ID_FLAGS(func, bpf_unwind_resume)
#ifdef CONFIG_BPF_EVENTS
BTF_ID_FLAGS(func, bpf_send_signal_task)
#endif
diff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c
index 44ecdc5b4ec2d..9cfd05f970bc9 100644
--- a/kernel/bpf/liveness.c
+++ b/kernel/bpf/liveness.c
@@ -8,6 +8,8 @@
#include <linux/slab.h>
#include <linux/sort.h>
+#include "exception.h"
+
#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
struct per_frame_masks {
@@ -256,6 +258,9 @@ bpf_insn_successors(struct bpf_verifier_env *env, u32 idx)
succ = env->succ;
succ->cnt = 0;
+ if (unlikely(bpf_is_unwind_resume_kfunc(insn)))
+ return succ;
+
opcode_info = &opcode_info_tbl[BPF_CLASS(insn->code) | BPF_OP(insn->code)];
insn_sz = bpf_is_ldimm64(insn) ? 2 : 1;
if (opcode_info->can_fallthrough)
@@ -264,6 +269,13 @@ bpf_insn_successors(struct bpf_verifier_env *env, u32 idx)
if (opcode_info->can_jump)
succ->items[succ->cnt++] = idx + bpf_jmp_offset(insn) + 1;
+ if (unlikely(env->cleanup_info_cnt)) {
+ int pad = bpf_cleanup_pad_of_call(env, idx);
+
+ if (pad >= 0)
+ succ->items[succ->cnt++] = pad;
+ }
+
return succ;
}
@@ -397,6 +409,14 @@ bool bpf_stack_slot_alive(struct bpf_verifier_env *env, u32 frameno, u32 half_sp
alive = bpf_calls_callback(env, callsite)
? is_live_before(instance, callsite, rel, half_spi)
: is_live_before(instance, callsite + 1, rel, half_spi);
+
+ /* Control may also go to the landing pad. */
+ if (!alive && unlikely(env->cleanup_info_cnt)) {
+ int pad = bpf_cleanup_pad_of_call(env, callsite);
+
+ if (pad >= 0)
+ alive = is_live_before(instance, pad, rel, half_spi);
+ }
if (alive)
return true;
}
diff --git a/kernel/bpf/states.c b/kernel/bpf/states.c
index 66fb11b6c6a76..be0f529f7eccb 100644
--- a/kernel/bpf/states.c
+++ b/kernel/bpf/states.c
@@ -996,6 +996,9 @@ static bool states_equal(struct bpf_verifier_env *env,
if (old->in_sleepable != cur->in_sleepable)
return false;
+ if (old->unwinding != cur->unwinding)
+ return false;
+
if (!refsafe(old, cur, &env->idmap_scratch))
return false;
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index def57bddb0924..ac70914697662 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2912,7 +2912,7 @@ int __init __used bpf_multi_func(void) { return 0; }
BTF_ID_LIST_GLOBAL_SINGLE(bpf_multi_func_btf_id, func, bpf_multi_func)
/* last field in 'union bpf_attr' used by this command */
-#define BPF_PROG_LOAD_LAST_FIELD keyring_id
+#define BPF_PROG_LOAD_LAST_FIELD cleanup_info_cnt
static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_attr *attr_log)
{
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 6c6b8d8520cdf..43ecf79baa4a9 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -10,6 +10,7 @@
#include <linux/slab.h>
#include <linux/bpf.h>
#include <linux/btf.h>
+#include <linux/bpf_cleanup_abi.h>
#include <linux/bpf_verifier.h>
#include <linux/filter.h>
#include <net/netlink.h>
@@ -37,6 +38,7 @@
#include "diagnostics.h"
#include "disasm.h"
+#include "exception.h"
static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
@@ -1714,6 +1716,7 @@ int bpf_copy_verifier_state(struct bpf_verifier_state *dst_state,
return err;
dst_state->speculative = src->speculative;
dst_state->in_sleepable = src->in_sleepable;
+ dst_state->unwinding = src->unwinding;
dst_state->curframe = src->curframe;
dst_state->branches = src->branches;
dst_state->parent = src->parent;
@@ -5588,6 +5591,14 @@ static int check_max_stack_depth(struct bpf_verifier_env *env)
}
}
+ /*
+ * A pad rebuilds its frame from a spill area, and on x86-64 a private
+ * stack's frame pointer is in no spill area. Refused on every arch
+ * rather than just that one.
+ */
+ if (env->cleanup_info_cnt)
+ priv_stack_mode = NO_PRIV_STACK;
+
if (priv_stack_mode == PRIV_STACK_UNKNOWN)
priv_stack_mode = bpf_enable_priv_stack(env->prog);
@@ -10484,6 +10495,10 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins
* callbacks
*/
env->subprog_info[subprog].is_cb = true;
+ err = bpf_cleanup_check_callback(env, subprog);
+ if (err)
+ return err;
+
if (bpf_pseudo_kfunc_call(insn) &&
!is_callback_calling_kfunc(insn->imm)) {
verifier_bug(env, "kfunc %s#%d not marked as callback-calling",
@@ -10535,8 +10550,8 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins
return 0;
}
-static int process_bpf_exit_full(struct bpf_verifier_env *env,
- bool *do_print_state, bool exception_exit);
+static int process_bpf_exit_full(struct bpf_verifier_env *env, bool *do_print_state);
+static int unwind_step(struct bpf_verifier_env *env, u32 callsite, int *insn_idx);
static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
int *insn_idx)
@@ -10626,7 +10641,7 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
verbose(env, "failed to push state for global subprog exception path\n");
return PTR_ERR(branch);
}
- return process_bpf_exit_full(env, NULL, true);
+ return unwind_step(env, *insn_idx, insn_idx);
}
/* continue with next insn after call */
@@ -14506,7 +14521,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
env->prog->call_session_cookie = true;
if (bpf_is_throw_kfunc(insn))
- return process_bpf_exit_full(env, NULL, true);
+ return unwind_step(env, insn_idx, &env->insn_idx);
return 0;
}
@@ -18431,9 +18446,75 @@ enum {
INSN_IDX_UPDATED = 2,
};
-static int process_bpf_exit_full(struct bpf_verifier_env *env,
- bool *do_print_state,
- bool exception_exit)
+static u32 unwind_pop_frame(struct bpf_verifier_env *env)
+{
+ struct bpf_verifier_state *state = env->cur_state;
+ struct bpf_func_state *callee = state->frame[state->curframe];
+ u32 callsite = callee->callsite;
+ struct bpf_func_state *caller;
+
+ caller = state->frame[state->curframe - 1];
+ account_processed_insns(env, callee, caller);
+ free_func_state(callee);
+ state->frame[state->curframe--] = NULL;
+ invalidate_outgoing_stack_args(env, caller);
+ return callsite;
+}
+
+static void unwind_enter_pad(struct bpf_verifier_env *env)
+{
+ struct bpf_func_state *frame = cur_func(env);
+
+ clear_caller_saved_regs(env, frame->regs);
+ mark_reg_unknown(env, frame->regs, BPF_REG_0);
+ __mark_reg_known(&frame->regs[BPF_REG_0], BPF_PAD_ENTRY_R0);
+}
+
+static int unwind_finish(struct bpf_verifier_env *env)
+{
+ int err = check_resource_leak(env, true, true, "bpf_throw");
+
+ if (err)
+ return err;
+ return PROCESS_BPF_EXIT;
+}
+
+static int unwind_step(struct bpf_verifier_env *env, u32 callsite, int *insn_idx)
+{
+ struct bpf_verifier_state *state = env->cur_state;
+
+ state->unwinding = true;
+ for (;;) {
+ int pad = bpf_cleanup_pad_of_call(env, callsite);
+
+ if (pad >= 0) {
+ unwind_enter_pad(env);
+ *insn_idx = pad;
+ return INSN_IDX_UPDATED;
+ }
+ if (!state->curframe)
+ return unwind_finish(env);
+ callsite = unwind_pop_frame(env);
+ }
+}
+
+static int process_cleanup_resume(struct bpf_verifier_env *env, int *insn_idx)
+{
+ struct bpf_verifier_state *state = env->cur_state;
+
+ /* A pad entered by ordinary control flow. */
+ if (!state->unwinding) {
+ verbose(env,
+ "bpf_unwind_resume() at insn %d reached without an exception in flight\n",
+ *insn_idx);
+ return -EINVAL;
+ }
+ if (!state->curframe)
+ return unwind_finish(env);
+ return unwind_step(env, unwind_pop_frame(env), insn_idx);
+}
+
+static int process_bpf_exit_full(struct bpf_verifier_env *env, bool *do_print_state)
{
struct bpf_func_state *cur_frame = cur_func(env);
@@ -18443,25 +18524,11 @@ static int process_bpf_exit_full(struct bpf_verifier_env *env,
* for which reference_state must match caller reference
* state when it exits.
*/
- int err = check_resource_leak(env, exception_exit,
- exception_exit || !env->cur_state->curframe,
- exception_exit ? "bpf_throw" :
+ int err = check_resource_leak(env, false, !env->cur_state->curframe,
"BPF_EXIT instruction in main prog");
if (err)
return err;
- /* The side effect of the prepare_func_exit which is
- * being skipped is that it frees bpf_func_state.
- * Typically, process_bpf_exit will only be hit with
- * outermost exit. copy_verifier_state in pop_stack will
- * handle freeing of any extra bpf_func_state left over
- * from not processing all nested function exits. We
- * also skip return code checks as they are not needed
- * for exceptional exits.
- */
- if (exception_exit)
- return PROCESS_BPF_EXIT;
-
if (env->cur_state->curframe) {
/* exit from nested function */
err = prepare_func_exit(env, &env->insn_idx);
@@ -18635,6 +18702,8 @@ static int do_check_insn(struct bpf_verifier_env *env, bool *do_print_state)
env->jmps_processed++;
if (opcode == BPF_CALL) {
+ if (bpf_is_unwind_resume_kfunc(insn))
+ return process_cleanup_resume(env, &env->insn_idx);
if (env->cur_state->active_locks) {
if ((insn->src_reg == BPF_REG_0 &&
insn->imm != BPF_FUNC_spin_unlock &&
@@ -18668,7 +18737,7 @@ static int do_check_insn(struct bpf_verifier_env *env, bool *do_print_state)
env->insn_idx += insn->imm + 1;
return INSN_IDX_UPDATED;
} else if (opcode == BPF_EXIT) {
- return process_bpf_exit_full(env, do_print_state, false);
+ return process_bpf_exit_full(env, do_print_state);
}
return check_cond_jmp_op(env, insn, &env->insn_idx);
}
@@ -21638,6 +21707,11 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
if (ret < 0)
goto skip_full_check;
+ /* The CFG needs an edge from a call in a cleanup range to its pad. */
+ ret = bpf_prepare_cleanup_exceptions(env);
+ if (ret < 0)
+ goto skip_full_check;
+
/* Validate instructions and resolve the program's referenced resources. */
ret = check_and_resolve_insns(env);
if (ret < 0)
@@ -21658,6 +21732,10 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
if (ret < 0)
goto skip_full_check;
+ ret = bpf_check_cleanup_exceptions(env);
+ if (ret < 0)
+ goto skip_full_check;
+
ret = bpf_compute_postorder(env);
if (ret < 0)
goto skip_full_check;
@@ -21845,6 +21923,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
kvfree(env->succ);
kvfree(env->gotox_tmp_buf);
bpf_diag_free(env);
+ kvfree(env->cleanup_info);
kvfree(env);
return ret;
}
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 732b35cc08d1c..f7dc121be0947 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -1669,6 +1669,9 @@ union bpf_attr {
* verification.
*/
__s32 keyring_id;
+ __aligned_u64 cleanup_info; /* exception cleanup table */
+ __u32 cleanup_info_rec_size; /* userspace bpf_cleanup_info size */
+ __u32 cleanup_info_cnt; /* number of bpf_cleanup_info records */
};
struct { /* anonymous struct used by BPF_OBJ_* commands */
@@ -7588,6 +7591,12 @@ struct bpf_line_info {
__u32 line_col;
};
+struct bpf_cleanup_info {
+ __u32 begin_off;
+ __u32 end_off;
+ __u32 landing_pad_off;
+};
+
struct bpf_spin_lock {
__u32 val;
};
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 96819c082c772..bcf490570961c 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -295,7 +295,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
const struct bpf_insn *insns, size_t insn_cnt,
struct bpf_prog_load_opts *opts)
{
- const size_t attr_sz = offsetofend(union bpf_attr, keyring_id);
+ const size_t attr_sz = offsetofend(union bpf_attr, cleanup_info_cnt);
void *finfo = NULL, *linfo = NULL;
const char *func_info, *line_info;
__u32 log_size, log_level, attach_prog_fd, attach_btf_obj_fd;
@@ -370,6 +370,10 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
attr.fd_array = ptr_to_u64(OPTS_GET(opts, fd_array, NULL));
attr.fd_array_cnt = OPTS_GET(opts, fd_array_cnt, 0);
+ attr.cleanup_info = ptr_to_u64(OPTS_GET(opts, cleanup_info, NULL));
+ attr.cleanup_info_cnt = OPTS_GET(opts, cleanup_info_cnt, 0);
+ attr.cleanup_info_rec_size = OPTS_GET(opts, cleanup_info_rec_size, 0);
+
if (log_level) {
attr.log_buf = ptr_to_u64(log_buf);
attr.log_size = log_size;
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 7534a593edae2..6f62a99e1e3e5 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -128,9 +128,14 @@ struct bpf_prog_load_opts {
/* if set, provides the length of fd_array */
__u32 fd_array_cnt;
+
+ /* exception cleanup table, from the .bpf_cleanup section */
+ const void *cleanup_info;
+ __u32 cleanup_info_cnt;
+ __u32 cleanup_info_rec_size;
size_t :0;
};
-#define bpf_prog_load_opts__last_field fd_array_cnt
+#define bpf_prog_load_opts__last_field cleanup_info_rec_size
LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type,
const char *prog_name, const char *license,
diff --git a/tools/lib/bpf/gen_loader.c b/tools/lib/bpf/gen_loader.c
index af3a04f161ac1..e9bb282a557a3 100644
--- a/tools/lib/bpf/gen_loader.c
+++ b/tools/lib/bpf/gen_loader.c
@@ -981,13 +981,15 @@ static void cleanup_relos(struct bpf_gen *gen, int insns)
cleanup_core_relo(gen);
}
-/* Convert func, line, and core relo info blobs to target endianness */
+/* Convert func, line, core relo and cleanup info blobs to target endianness */
static void info_blob_bswap(struct bpf_gen *gen, int func_info, int line_info,
- int core_relos, struct bpf_prog_load_opts *load_attr)
+ int core_relos, int cleanup_info,
+ struct bpf_prog_load_opts *load_attr)
{
struct bpf_func_info *fi = gen->data_start + func_info;
struct bpf_line_info *li = gen->data_start + line_info;
struct bpf_core_relo *cr = gen->data_start + core_relos;
+ struct bpf_cleanup_info *ci = gen->data_start + cleanup_info;
int i;
for (i = 0; i < load_attr->func_info_cnt; i++)
@@ -998,6 +1000,9 @@ static void info_blob_bswap(struct bpf_gen *gen, int func_info, int line_info,
for (i = 0; i < gen->core_relo_cnt; i++)
bpf_core_relo_bswap(cr++);
+
+ for (i = 0; i < load_attr->cleanup_info_cnt; i++)
+ bpf_cleanup_info_bswap(ci++);
}
void bpf_gen__prog_load(struct bpf_gen *gen,
@@ -1011,8 +1016,11 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
load_attr->line_info_rec_size;
int core_relo_tot_sz = gen->core_relo_cnt *
sizeof(struct bpf_core_relo);
+ int cleanup_info_tot_sz = load_attr->cleanup_info_cnt *
+ load_attr->cleanup_info_rec_size;
int prog_load_attr, license_off, insns_off, func_info, line_info, core_relos;
- int attr_size = offsetofend(union bpf_attr, core_relo_rec_size);
+ int attr_size = offsetofend(union bpf_attr, cleanup_info_cnt);
+ int cleanup_info;
union bpf_attr attr;
memset(&attr, 0, attr_size);
@@ -1061,9 +1069,17 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
core_relos, gen->core_relo_cnt,
sizeof(struct bpf_core_relo));
+ attr.cleanup_info_rec_size = tgt_endian(load_attr->cleanup_info_rec_size);
+ attr.cleanup_info_cnt = tgt_endian(load_attr->cleanup_info_cnt);
+ cleanup_info = add_data(gen, load_attr->cleanup_info, cleanup_info_tot_sz);
+ pr_debug("gen: prog_load: cleanup_info: off %d cnt %u rec size %u\n",
+ cleanup_info, load_attr->cleanup_info_cnt,
+ load_attr->cleanup_info_rec_size);
+
/* convert all info blobs to target endianness */
if (gen->swapped_endian && !gen->error)
- info_blob_bswap(gen, func_info, line_info, core_relos, load_attr);
+ info_blob_bswap(gen, func_info, line_info, core_relos, cleanup_info,
+ load_attr);
libbpf_strlcpy(attr.prog_name, prog_name, sizeof(attr.prog_name));
prog_load_attr = add_data(gen, &attr, attr_size);
@@ -1085,6 +1101,9 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
/* populate union bpf_attr with a pointer to core_relos */
emit_rel_store(gen, attr_field(prog_load_attr, core_relos), core_relos);
+ /* populate union bpf_attr with a pointer to cleanup_info */
+ emit_rel_store(gen, attr_field(prog_load_attr, cleanup_info), cleanup_info);
+
/* populate union bpf_attr fd_array with a pointer to data where map_fds are saved */
emit_rel_store(gen, attr_field(prog_load_attr, fd_array), gen->fd_array);
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 613afae265199..ebb21c9cdfe08 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -514,6 +514,11 @@ struct bpf_program {
void *line_info;
__u32 line_info_rec_size;
__u32 line_info_cnt;
+
+ struct bpf_cleanup_info *cleanup_info;
+ __u32 cleanup_info_rec_size;
+ __u32 cleanup_info_cnt;
+
__u32 prog_flags;
__u8 hash[SHA256_DIGEST_LENGTH];
@@ -549,6 +554,7 @@ struct bpf_struct_ops {
#define STRUCT_OPS_SEC ".struct_ops"
#define STRUCT_OPS_LINK_SEC ".struct_ops.link"
#define ARENA_SEC ".addr_space.1"
+#define CLEANUP_SEC ".bpf_cleanup"
enum libbpf_map_type {
LIBBPF_MAP_UNSPEC,
@@ -677,6 +683,25 @@ struct elf_sec_desc {
Elf_Data *data;
};
+#define CLEANUP_REC_FIELDS (sizeof(struct bpf_cleanup_info) / sizeof(__u32))
+
+/* Index of each field of struct bpf_cleanup_info, read as an array of __u32. */
+enum {
+ CLEANUP_REC_BEGIN,
+ CLEANUP_REC_END,
+ CLEANUP_REC_PAD,
+};
+
+/* One (begin, end, landing_pad) triple from .bpf_cleanup, with each field
+ * resolved from its relocation to an ELF section plus a section-relative
+ * instruction index. The mapping to final program instruction indices can only
+ * happen after subprogram placement, which differs per main program.
+ */
+struct cleanup_raw_rec {
+ int sec_idx[CLEANUP_REC_FIELDS];
+ size_t insn_idx[CLEANUP_REC_FIELDS];
+};
+
struct elf_state {
int fd;
const void *obj_buf;
@@ -696,6 +721,8 @@ struct elf_state {
bool has_st_ops;
int arena_data_shndx;
int jumptables_data_shndx;
+ Elf_Data *cleanup_data;
+ int cleanup_shndx;
};
struct usdt_manager;
@@ -771,6 +798,9 @@ struct bpf_object {
void *jumptables_data;
size_t jumptables_data_sz;
+ struct cleanup_raw_rec *cleanup_recs;
+ size_t cleanup_rec_cnt;
+
struct {
struct bpf_program *prog;
unsigned int sym_off;
@@ -817,7 +847,10 @@ static void bpf_program__exit(struct bpf_program *prog)
zfree(&prog->sec_name);
zfree(&prog->insns);
zfree(&prog->reloc_desc);
+ zfree(&prog->cleanup_info);
+ prog->cleanup_info_rec_size = 0;
+ prog->cleanup_info_cnt = 0;
prog->nr_reloc = 0;
prog->insns_cnt = 0;
prog->sec_idx = -1;
@@ -1554,6 +1587,7 @@ static struct bpf_object *bpf_object__new(const char *path,
obj->efile.obj_buf = obj_buf;
obj->efile.obj_buf_sz = obj_buf_sz;
obj->efile.btf_maps_shndx = -1;
+ obj->efile.cleanup_shndx = -1;
obj->kconfig_map_idx = -1;
obj->arena_map_idx = -1;
@@ -4040,6 +4074,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
sec_desc->shdr = sh;
sec_desc->data = data;
obj->efile.has_st_ops = true;
+ } else if (strcmp(name, CLEANUP_SEC) == 0) {
+ obj->efile.cleanup_data = data;
+ obj->efile.cleanup_shndx = idx;
} else if (strcmp(name, ARENA_SEC) == 0) {
obj->efile.arena_data = data;
obj->efile.arena_data_shndx = idx;
@@ -4067,6 +4104,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
strcmp(name, ".rel" STRUCT_OPS_LINK_SEC) &&
strcmp(name, ".rel?" STRUCT_OPS_SEC) &&
strcmp(name, ".rel?" STRUCT_OPS_LINK_SEC) &&
+ strcmp(name, ".rel" CLEANUP_SEC) &&
strcmp(name, ".rel" MAPS_ELF_SEC)) {
pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n",
idx, name, targ_sec_idx,
@@ -4847,6 +4885,214 @@ static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj,
return NULL;
}
+static int bpf_object__init_cleanup_info(struct bpf_object *obj)
+{
+ Elf_Data *data = obj->efile.cleanup_data;
+ Elf_Data *relo = NULL;
+ size_t i, nrels, nslots, nrecs;
+ struct cleanup_raw_rec *recs;
+ int *slot_sec, ret = 0;
+ size_t *slot_val;
+ const __u32 *vals;
+ bool native;
+
+ if (!data || obj->efile.cleanup_shndx < 0)
+ return 0;
+
+ native = is_native_endianness(obj);
+
+ for (i = 0; i < obj->efile.sec_cnt; i++) {
+ struct elf_sec_desc *sd = &obj->efile.secs[i];
+
+ if (sd->sec_type == SEC_RELO && sd->shdr &&
+ sd->shdr->sh_info == (Elf64_Word)obj->efile.cleanup_shndx) {
+ relo = sd->data;
+ break;
+ }
+ }
+ if (!relo) {
+ pr_warn("%s present without relocations\n", CLEANUP_SEC);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+ if (data->d_size % sizeof(struct bpf_cleanup_info)) {
+ pr_warn("%s size %zu is not a multiple of the record size %zu\n",
+ CLEANUP_SEC, data->d_size, sizeof(struct bpf_cleanup_info));
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+
+ vals = data->d_buf;
+ nslots = data->d_size / sizeof(__u32);
+ nrecs = data->d_size / sizeof(struct bpf_cleanup_info);
+
+ slot_sec = calloc(nslots, sizeof(*slot_sec));
+ slot_val = calloc(nslots, sizeof(*slot_val));
+ recs = calloc(nrecs ?: 1, sizeof(*recs));
+ if (!slot_sec || !slot_val || !recs) {
+ ret = -ENOMEM;
+ goto out;
+ }
+ for (i = 0; i < nslots; i++)
+ slot_sec[i] = -1;
+
+ /* One relocation per 4-byte field, naming the section it points into. */
+ nrels = relo->d_size / sizeof(Elf64_Rel);
+ for (i = 0; i < nrels; i++) {
+ Elf64_Rel *rel = elf_rel_by_idx(relo, i);
+ Elf64_Sym *sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info));
+ size_t type = ELF64_R_TYPE(rel->r_info);
+ size_t slot = rel->r_offset / sizeof(__u32);
+
+ if (type != R_BPF_64_NODYLD32 && type != R_BPF_64_ABS32) {
+ pr_warn("%s: relocation %zu has unexpected type %zu\n",
+ CLEANUP_SEC, i, type);
+ ret = -LIBBPF_ERRNO__FORMAT;
+ goto out;
+ }
+ if (!sym || slot >= nslots || rel->r_offset % sizeof(__u32)) {
+ pr_warn("%s: bad relocation %zu\n", CLEANUP_SEC, i);
+ ret = -LIBBPF_ERRNO__FORMAT;
+ goto out;
+ }
+ slot_sec[slot] = sym->st_shndx;
+ /* The addend lives in the section data, which libelf leaves in
+ * the object's byte order; a non-section symbol additionally
+ * contributes its own value.
+ */
+ slot_val[slot] = (native ? vals[slot] : bswap_32(vals[slot])) +
+ sym->st_value;
+ }
+
+ for (i = 0; i < nslots; i++) {
+ struct cleanup_raw_rec *rec = &recs[i / CLEANUP_REC_FIELDS];
+ size_t field = i % CLEANUP_REC_FIELDS;
+
+ if (slot_sec[i] < 0) {
+ pr_warn("%s: field %zu has no relocation\n", CLEANUP_SEC, i);
+ ret = -LIBBPF_ERRNO__FORMAT;
+ goto out;
+ }
+ if (slot_val[i] % BPF_INSN_SZ) {
+ pr_warn("%s: field %zu offset %zu is not instruction aligned\n",
+ CLEANUP_SEC, i, slot_val[i]);
+ ret = -LIBBPF_ERRNO__FORMAT;
+ goto out;
+ }
+ rec->sec_idx[field] = slot_sec[i];
+ rec->insn_idx[field] = slot_val[i] / BPF_INSN_SZ;
+ }
+
+ obj->cleanup_recs = recs;
+ obj->cleanup_rec_cnt = nrecs;
+ recs = NULL;
+out:
+ free(recs);
+ free(slot_val);
+ free(slot_sec);
+ return ret;
+}
+
+static int cmp_cleanup_info(const void *a, const void *b)
+{
+ const struct bpf_cleanup_info *x = a, *y = b;
+
+ if (x->begin_off == y->begin_off)
+ return 0;
+ return x->begin_off < y->begin_off ? -1 : 1;
+}
+
+static int bpf_prog_collect_cleanup_info(struct bpf_object *obj,
+ struct bpf_program *prog)
+{
+ size_t i;
+ int j;
+
+ for (i = 0; i < obj->cleanup_rec_cnt; i++) {
+ struct cleanup_raw_rec *raw = &obj->cleanup_recs[i];
+ struct bpf_program *owner = NULL;
+ struct bpf_cleanup_info ci = {};
+ __u32 *fields = (__u32 *)&ci;
+ void *tmp;
+
+ for (j = 0; j < CLEANUP_REC_FIELDS; j++) {
+ size_t idx = raw->insn_idx[j], final;
+ struct bpf_program *p;
+
+ /* The end of a range is exclusive, so it may name the
+ * instruction just past the last one of a function,
+ * which belongs to the next function or to nothing at
+ * all. Ask about the last instruction the range covers,
+ * the way the kernel does.
+ */
+ if (j == CLEANUP_REC_END) {
+ if (!idx) {
+ pr_warn("%s: record %zu is an empty range\n",
+ CLEANUP_SEC, i);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+ idx--;
+ }
+
+ p = find_prog_by_sec_insn(obj, raw->sec_idx[j], idx);
+ if (!p) {
+ pr_warn("%s: record %zu field %d is not inside a function\n",
+ CLEANUP_SEC, i, j);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+ if (!owner) {
+ owner = p;
+ } else if (owner != p) {
+ pr_warn("%s: record %zu spans functions '%s' and '%s'\n",
+ CLEANUP_SEC, i, owner->name, p->name);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+
+ if (owner == prog) {
+ final = raw->insn_idx[j] - prog->sec_insn_off;
+ } else if (prog_is_subprog(obj, owner) && owner->sub_insn_off) {
+ /* sub_insn_off is where this subprogram was
+ * appended to the main program being relocated;
+ * zero means it is not part of it.
+ */
+ final = owner->sub_insn_off +
+ raw->insn_idx[j] - owner->sec_insn_off;
+ } else {
+ owner = NULL;
+ break;
+ }
+ fields[j] = final;
+ }
+ if (!owner)
+ continue;
+
+ tmp = libbpf_reallocarray(prog->cleanup_info, prog->cleanup_info_cnt + 1,
+ sizeof(*prog->cleanup_info));
+ if (!tmp)
+ return -ENOMEM;
+ prog->cleanup_info = tmp;
+ prog->cleanup_info_rec_size = sizeof(struct bpf_cleanup_info);
+ prog->cleanup_info[prog->cleanup_info_cnt++] = ci;
+
+ pr_debug("prog '%s': cleanup region [%u,%u) -> landing pad %u\n",
+ prog->name, ci.begin_off, ci.end_off, ci.landing_pad_off);
+ }
+
+ qsort(prog->cleanup_info, prog->cleanup_info_cnt,
+ sizeof(*prog->cleanup_info), cmp_cleanup_info);
+ for (i = 1; i < prog->cleanup_info_cnt; i++) {
+ struct bpf_cleanup_info *prev = &prog->cleanup_info[i - 1];
+ struct bpf_cleanup_info *cur = &prog->cleanup_info[i];
+
+ if (cur->begin_off < prev->end_off) {
+ pr_warn("prog '%s': overlapping cleanup regions [%u,%u) and [%u,%u)\n",
+ prog->name, prev->begin_off, prev->end_off,
+ cur->begin_off, cur->end_off);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+ }
+
+ return 0;
+}
+
static int
bpf_object__collect_prog_relos(struct bpf_object *obj, Elf64_Shdr *shdr, Elf_Data *data)
{
@@ -7556,6 +7802,13 @@ static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_pat
return err;
}
}
+
+ err = bpf_prog_collect_cleanup_info(obj, prog);
+ if (err) {
+ pr_warn("prog '%s': failed to collect cleanup info: %s\n",
+ prog->name, errstr(err));
+ return err;
+ }
}
for (i = 0; i < obj->nr_programs; i++) {
prog = &obj->programs[i];
@@ -7746,6 +7999,9 @@ static int bpf_object__collect_relos(struct bpf_object *obj)
return -LIBBPF_ERRNO__INTERNAL;
}
+ if (idx == obj->efile.cleanup_shndx)
+ continue;
+
if (obj->efile.secs[idx].sec_type == SEC_ST_OPS)
err = bpf_object__collect_st_ops_relos(obj, shdr, data);
else if (idx == obj->efile.btf_maps_shndx)
@@ -8018,6 +8274,11 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
load_attr.line_info_rec_size = prog->line_info_rec_size;
load_attr.line_info_cnt = prog->line_info_cnt;
}
+ if (prog->cleanup_info_cnt) {
+ load_attr.cleanup_info = prog->cleanup_info;
+ load_attr.cleanup_info_cnt = prog->cleanup_info_cnt;
+ load_attr.cleanup_info_rec_size = prog->cleanup_info_rec_size;
+ }
load_attr.log_level = log_level;
load_attr.prog_flags = prog->prog_flags;
load_attr.fd_array = obj->fd_array;
@@ -8346,6 +8607,15 @@ static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_s
}
}
+/* LLVM terminates a cleanup landing pad with a call to _Unwind_Resume, the
+ * base unwind ABI's entry point for carrying an unwind on once a frame's
+ * cleanups have run. The kernel knows it as bpf_unwind_resume.
+ */
+static const char *kern_extern_name(const char *name)
+{
+ return strcmp(name, "_Unwind_Resume") ? name : "bpf_unwind_resume";
+}
+
static int bpf_program_record_relos(struct bpf_program *prog)
{
struct bpf_object *obj = prog->obj;
@@ -8362,12 +8632,12 @@ static int bpf_program_record_relos(struct bpf_program *prog)
continue;
kind = btf_is_var(btf__type_by_id(obj->btf, ext->btf_id)) ?
BTF_KIND_VAR : BTF_KIND_FUNC;
- bpf_gen__record_extern(obj->gen_loader, ext->name,
+ bpf_gen__record_extern(obj->gen_loader, kern_extern_name(ext->name),
ext->is_weak, !ext->ksym.type_id,
true, kind, relo->insn_idx);
break;
case RELO_EXTERN_CALL:
- bpf_gen__record_extern(obj->gen_loader, ext->name,
+ bpf_gen__record_extern(obj->gen_loader, kern_extern_name(ext->name),
ext->is_weak, false, false, BTF_KIND_FUNC,
relo->insn_idx);
break;
@@ -8565,6 +8835,7 @@ static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf,
err = err ? : bpf_object__init_maps(obj, opts);
err = err ? : bpf_object_init_progs(obj, opts);
err = err ? : bpf_object__collect_relos(obj);
+ err = err ? : bpf_object__init_cleanup_info(obj);
if (err)
goto out;
@@ -8807,17 +9078,19 @@ static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
struct module_btf *mod_btf = NULL;
const struct btf_type *kern_func;
struct btf *kern_btf = NULL;
+ const char *kern_name;
int ret;
local_func_proto_id = ext->ksym.type_id;
- kfunc_id = find_ksym_btf_id(obj, ext->essent_name ?: ext->name, BTF_KIND_FUNC, &kern_btf,
- &mod_btf);
+ kern_name = kern_extern_name(ext->essent_name ?: ext->name);
+
+ kfunc_id = find_ksym_btf_id(obj, kern_name, BTF_KIND_FUNC, &kern_btf, &mod_btf);
if (kfunc_id < 0) {
if (kfunc_id == -ESRCH && ext->is_weak)
return 0;
pr_warn("extern (func ksym) '%s': not found in kernel or module BTFs\n",
- ext->name);
+ strcmp(kern_name, "bpf_unwind_resume") ? ext->name : kern_name);
return kfunc_id;
}
@@ -9676,6 +9949,9 @@ void bpf_object__close(struct bpf_object *obj)
zfree(&obj->jumptables_data);
obj->jumptables_data_sz = 0;
+ zfree(&obj->cleanup_recs);
+ obj->cleanup_rec_cnt = 0;
+
for (i = 0; i < obj->jumptable_map_cnt; i++)
close(obj->jumptable_maps[i].fd);
zfree(&obj->jumptable_maps);
@@ -10068,6 +10344,25 @@ int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_opts
attr.line_info_rec_size = info ? info_rec_size : prog->line_info_rec_size;
}
+ /* exception cleanup table */
+ info = OPTS_GET(opts, cleanup_info, NULL);
+ info_cnt = OPTS_GET(opts, cleanup_info_cnt, 0);
+ info_rec_size = OPTS_GET(opts, cleanup_info_rec_size, 0);
+ if (!!info != !!info_cnt || !!info != !!info_rec_size) {
+ pr_warn("prog '%s': cleanup_info, cleanup_info_cnt, and cleanup_info_rec_size must all be specified or all omitted\n",
+ prog->name);
+ return libbpf_err(-EINVAL);
+ }
+ if (info) {
+ attr.cleanup_info = info;
+ attr.cleanup_info_cnt = info_cnt;
+ attr.cleanup_info_rec_size = info_rec_size;
+ } else if (prog->cleanup_info_cnt) {
+ attr.cleanup_info = prog->cleanup_info;
+ attr.cleanup_info_cnt = prog->cleanup_info_cnt;
+ attr.cleanup_info_rec_size = prog->cleanup_info_rec_size;
+ }
+
/* Logging is caller-controlled; no fallback to prog/obj log settings */
attr.log_buf = OPTS_GET(opts, log_buf, NULL);
attr.log_size = OPTS_GET(opts, log_size, 0);
diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
index cb4d962338441..78519f24fb404 100644
--- a/tools/lib/bpf/libbpf_internal.h
+++ b/tools/lib/bpf/libbpf_internal.h
@@ -56,6 +56,9 @@
#ifndef R_BPF_64_ABS32
#define R_BPF_64_ABS32 3
#endif
+#ifndef R_BPF_64_NODYLD32
+#define R_BPF_64_NODYLD32 4
+#endif
#ifndef R_BPF_64_32
#define R_BPF_64_32 10
#endif
@@ -569,6 +572,13 @@ static inline void bpf_core_relo_bswap(struct bpf_core_relo *i)
i->kind = bswap_32(i->kind);
}
+static inline void bpf_cleanup_info_bswap(struct bpf_cleanup_info *i)
+{
+ i->begin_off = bswap_32(i->begin_off);
+ i->end_off = bswap_32(i->end_off);
+ i->landing_pad_off = bswap_32(i->landing_pad_off);
+}
+
enum btf_field_iter_kind {
BTF_FIELD_ITER_IDS,
BTF_FIELD_ITER_STRS,
diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c
index 78f92c39290af..e5c06023cb5b1 100644
--- a/tools/lib/bpf/linker.c
+++ b/tools/lib/bpf/linker.c
@@ -1036,7 +1036,8 @@ static int linker_sanity_check_elf_relos(struct src_obj *obj, struct src_sec *se
size_t sym_type = ELF64_R_TYPE(relo->r_info);
if (sym_type != R_BPF_64_64 && sym_type != R_BPF_64_32 &&
- sym_type != R_BPF_64_ABS64 && sym_type != R_BPF_64_ABS32) {
+ sym_type != R_BPF_64_ABS64 && sym_type != R_BPF_64_ABS32 &&
+ sym_type != R_BPF_64_NODYLD32) {
pr_warn("ELF relo #%d in section #%zu has unexpected type %zu in %s\n",
i, sec->sec_idx, sym_type, obj->filename);
return -EINVAL;
@@ -2274,6 +2275,22 @@ static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *ob
insn->imm += sec->dst_off / sizeof(struct bpf_insn);
else
insn->imm += sec->dst_off;
+ } else if (sym_type == R_BPF_64_NODYLD32 ||
+ sym_type == R_BPF_64_ABS32) {
+ __u32 *val;
+
+ /* Two spellings of the one thing: LLVM
+ * emits NODYLD32 for a .long against a
+ * section symbol, GNU as emits ABS32
+ * (bpf_reloc_type_lookup() maps
+ * BFD_RELOC_32 to it), and the value
+ * they describe is the same.
+ */
+ val = dst_linked_sec->raw_data + dst_rel->r_offset;
+ if (linker->swapped_endian)
+ *val = bswap_32(bswap_32(*val) + sec->dst_off);
+ else
+ *val += sec->dst_off;
} else {
pr_warn("relocation against STT_SECTION in non-exec section is not supported!\n");
return -EINVAL;
diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 7ea5ba1df29e3..981cc26430495 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -523,7 +523,7 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \
LSKELS := fexit_sleep.c trace_printk.c trace_vprintk.c map_ptr_kern.c \
core_kern.c core_kern_overflow.c test_ringbuf.c \
test_ringbuf_n.c test_ringbuf_map_key.c test_ringbuf_write.c \
- test_ringbuf_overwrite.c
+ test_ringbuf_overwrite.c exceptions_cleanup_light.c
LSKELS_SIGNED := fentry_test.c fexit_test.c atomics.c
diff --git a/tools/testing/selftests/bpf/exceptions_cleanup.h b/tools/testing/selftests/bpf/exceptions_cleanup.h
new file mode 100644
index 0000000000000..0c088d96ca013
--- /dev/null
+++ b/tools/testing/selftests/bpf/exceptions_cleanup.h
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#ifndef __EXCEPTIONS_CLEANUP_H__
+#define __EXCEPTIONS_CLEANUP_H__
+
+#define THROW_COOKIE 0x100
+#define INNER_COOKIE 0x200
+
+/* progs/exceptions_cleanup.c: one bit per frame that reports it ran. */
+#define RAN_FOO3_PREEMPT 0x1
+#define RAN_FOO2_RCU 0x2
+#define RAN_FOO1V_PREEMPT 0x4
+#define RAN_FOO2_DROP 0x8
+#define RAN_BUMP 0x10
+
+/* progs/exceptions_cleanup_shapes.c: one bit per shape, numbered its own way. */
+#define RAN_SWEEP 0x1
+#define RAN_SHARED 0x2
+#define RAN_REGS 0x4
+#define RAN_TAIL_CALL 0x8
+#define RAN_MAIN_PAD 0x10
+#define RAN_TC_TAKEN 0x20
+#define RAN_FREPLACE 0x40
+#define RAN_ADDR_TAKEN 0x80
+#define RAN_NO_SUBPROG 0x100
+#define RAN_PAD_CALLS 0x200
+#define RAN_PAD_FIRST 0x400
+#define RAN_WIDE_REC 0x800
+#define RAN_PAD_STACK 0x1000
+#define RAN_DEEP_PAD 0x2000
+#define RAN_NOUNWIND_REC 0x4000
+#define RAN_RESUME_ALIAS 0x8000
+#define RAN_PAD_TAIL_CALL 0x10000
+
+/* progs/exceptions_cleanup_light.c: the one pad it has. */
+#define RAN_LIGHT 0x1
+
+#define CLEANUP_REC(begin, end, landing_pad) \
+ ".pushsection .bpf_cleanup,\"a\",@progbits;" \
+ ".long " begin ";" \
+ ".long " end ";" \
+ ".long " landing_pad ";" \
+ ".popsection;"
+
+/* Set a bit in @pads_ran. */
+#define PAD_RAN(bit) \
+ "r1 = %[pads_ran] ll;" \
+ "r2 = *(u64 *)(r1 + 0);" \
+ "r2 |= " bit ";" \
+ "*(u64 *)(r1 + 0) = r2;"
+
+#endif /* __EXCEPTIONS_CLEANUP_H__ */
diff --git a/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
new file mode 100644
index 0000000000000..ad0ff949f10df
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
@@ -0,0 +1,421 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <test_progs.h>
+#include "exceptions_cleanup.h"
+#include "exceptions_cleanup.skel.h"
+#include "exceptions_cleanup_fail.skel.h"
+#include "exceptions_cleanup_shapes.skel.h"
+#include "exceptions_cleanup_freplace.skel.h"
+#include "exceptions_cleanup_pad_freplace.skel.h"
+#include "exceptions_cleanup_ext_table.skel.h"
+#include "exceptions_cleanup_light.lskel.h"
+
+/* foo3 threw: every frame that has a pad ran it. */
+#define PADS_FOO3_THREW \
+ (RAN_FOO3_PREEMPT | RAN_FOO2_RCU | RAN_FOO1V_PREEMPT | RAN_FOO2_DROP)
+
+/* foo2 threw after foo3 returned normally: foo3's pad must not run. */
+#define PADS_FOO2_THREW \
+ (RAN_FOO2_RCU | RAN_FOO1V_PREEMPT | RAN_FOO2_DROP)
+
+static void run(struct exceptions_cleanup *skel, __u64 input, __u32 retval,
+ __u64 pads)
+{
+ __u64 ctx = 0;
+ int err;
+
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .ctx_in = &ctx,
+ .ctx_size_in = sizeof(ctx),
+ );
+
+ skel->bss->input = input;
+ skel->bss->pads_ran = 0;
+ skel->bss->result = 0;
+
+ err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.entry), &topts);
+ if (!ASSERT_OK(err, "run"))
+ return;
+ ASSERT_EQ(topts.retval, retval, "retval");
+ ASSERT_EQ(skel->bss->pads_ran, pads | RAN_BUMP, "pads_ran");
+}
+
+static void run_shape(struct exceptions_cleanup_shapes *skel, struct bpf_program *prog,
+ __u64 input, __u32 retval, __u64 pads)
+{
+ __u64 ctx = 0;
+ int err;
+
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .ctx_in = &ctx,
+ .ctx_size_in = sizeof(ctx),
+ );
+
+ skel->bss->input = input;
+ skel->bss->pads_ran = 0;
+
+ err = bpf_prog_test_run_opts(bpf_program__fd(prog), &topts);
+ if (!ASSERT_OK(err, "run"))
+ return;
+ ASSERT_EQ(topts.retval, retval, "retval");
+ ASSERT_EQ(skel->bss->pads_ran, pads, "pads_ran");
+}
+
+static void test_freplace(struct exceptions_cleanup_shapes *skel)
+{
+ struct exceptions_cleanup_freplace *fr;
+ struct bpf_link *link;
+ int tgt_fd;
+
+ tgt_fd = bpf_program__fd(skel->progs.entry_freplace);
+
+ fr = exceptions_cleanup_freplace__open();
+ if (!ASSERT_OK_PTR(fr, "freplace open"))
+ return;
+
+ if (!ASSERT_OK(bpf_program__set_attach_target(fr->progs.new_fr_callee,
+ tgt_fd, "fr_callee"),
+ "set_attach_target"))
+ goto out;
+ if (!ASSERT_OK(exceptions_cleanup_freplace__load(fr), "freplace load"))
+ goto out;
+
+ link = bpf_program__attach_freplace(fr->progs.new_fr_callee, tgt_fd,
+ "fr_callee");
+ if (!ASSERT_OK_PTR(link, "attach_freplace"))
+ goto out;
+
+ run_shape(skel, skel->progs.entry_freplace, 101, THROW_COOKIE, 0);
+ bpf_link__destroy(link);
+out:
+ exceptions_cleanup_freplace__destroy(fr);
+}
+
+static void test_pad_calls_freplace(struct exceptions_cleanup_shapes *skel)
+{
+ struct exceptions_cleanup_pad_freplace *fr;
+ struct bpf_link *link;
+ __u64 ctx = 0;
+ int tgt_fd, err;
+
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .ctx_in = &ctx,
+ .ctx_size_in = sizeof(ctx),
+ );
+
+ tgt_fd = bpf_program__fd(skel->progs.entry_pad_calls);
+
+ fr = exceptions_cleanup_pad_freplace__open();
+ if (!ASSERT_OK_PTR(fr, "pad freplace open"))
+ return;
+
+ if (!ASSERT_OK(bpf_program__set_attach_target(fr->progs.new_pad_callee,
+ tgt_fd, "pad_callee"),
+ "set_attach_target"))
+ goto out;
+ if (!ASSERT_OK(exceptions_cleanup_pad_freplace__load(fr), "pad freplace load"))
+ goto out;
+
+ link = bpf_program__attach_freplace(fr->progs.new_pad_callee, tgt_fd,
+ "pad_callee");
+ if (!ASSERT_OK_PTR(link, "attach_freplace"))
+ goto out;
+
+ skel->bss->input = 101;
+ skel->bss->pads_ran = 0;
+ skel->bss->pad_runs = 0;
+
+ err = bpf_prog_test_run_opts(tgt_fd, &topts);
+ if (!ASSERT_OK(err, "run"))
+ goto out_link;
+
+ ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
+ ASSERT_EQ(skel->bss->pads_ran, RAN_PAD_CALLS, "pads_ran");
+ ASSERT_EQ(topts.retval, THROW_COOKIE, "retval");
+out_link:
+ bpf_link__destroy(link);
+out:
+ exceptions_cleanup_pad_freplace__destroy(fr);
+}
+
+static void test_ext_table(struct exceptions_cleanup_shapes *skel)
+{
+ struct exceptions_cleanup_ext_table *fr;
+ struct bpf_link *link;
+ int tgt_fd;
+
+ tgt_fd = bpf_program__fd(skel->progs.entry_freplace);
+
+ fr = exceptions_cleanup_ext_table__open();
+ if (!ASSERT_OK_PTR(fr, "ext table open"))
+ return;
+
+ if (!ASSERT_OK(bpf_program__set_attach_target(fr->progs.new_fr_callee,
+ tgt_fd, "fr_callee"),
+ "set_attach_target"))
+ goto out;
+ if (!ASSERT_OK(exceptions_cleanup_ext_table__load(fr), "ext table load"))
+ goto out;
+
+ link = bpf_program__attach_freplace(fr->progs.new_fr_callee, tgt_fd,
+ "fr_callee");
+ if (!ASSERT_OK_PTR(link, "attach_freplace"))
+ goto out;
+
+ fr->bss->ext_pad_ran = 0;
+ run_shape(skel, skel->progs.entry_freplace, 101, THROW_COOKIE, 0);
+ ASSERT_EQ(fr->bss->ext_pad_ran, 1, "ext_pad_ran");
+
+ bpf_link__destroy(link);
+out:
+ exceptions_cleanup_ext_table__destroy(fr);
+}
+
+static void test_light_skeleton(void)
+{
+ struct exceptions_cleanup_light_lskel *skel;
+ __u64 ctx = 0;
+ int err;
+
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .ctx_in = &ctx,
+ .ctx_size_in = sizeof(ctx),
+ );
+
+ skel = exceptions_cleanup_light_lskel__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "light open_and_load"))
+ return;
+
+ err = bpf_prog_test_run_opts(skel->progs.entry_light.prog_fd, &topts);
+ if (!ASSERT_OK(err, "run"))
+ goto out;
+ ASSERT_EQ(topts.retval, THROW_COOKIE, "retval");
+ ASSERT_EQ(skel->bss->pads_ran, RAN_LIGHT, "pads_ran");
+out:
+ exceptions_cleanup_light_lskel__destroy(skel);
+}
+
+static void test_shapes(void)
+{
+ struct exceptions_cleanup_shapes *skel;
+
+ skel = exceptions_cleanup_shapes__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "shapes open_and_load"))
+ return;
+
+ /* The frame loads at all only if everything unreachable in it went. */
+ if (test__start_subtest("sweep_no_throw"))
+ run_shape(skel, skel->progs.entry_sweep, 1, 0, 0);
+ if (test__start_subtest("sweep_throw"))
+ run_shape(skel, skel->progs.entry_sweep, 101, THROW_COOKIE, RAN_SWEEP);
+
+ /* The covered call unwinds to the pad; the uncovered one never does. */
+ if (test__start_subtest("shared_callee_no_throw"))
+ run_shape(skel, skel->progs.entry_shared, 1, 2, 0);
+ if (test__start_subtest("shared_callee_throw"))
+ run_shape(skel, skel->progs.entry_shared, 101, THROW_COOKIE, RAN_SHARED);
+
+ /* The pad only sets its bit if it got the frame's own r6-r9 back. */
+ if (test__start_subtest("pad_sees_callee_saved"))
+ run_shape(skel, skel->progs.entry_regs, 101, THROW_COOKIE, RAN_REGS);
+
+ /* Same check, with a tail-call-reachable callee: its spill moves. */
+ if (test__start_subtest("tail_call_no_throw"))
+ run_shape(skel, skel->progs.entry_tail_call, 1, 0, 0);
+ if (test__start_subtest("tail_call_throw"))
+ run_shape(skel, skel->progs.entry_tail_call, 101, THROW_COOKIE,
+ RAN_TAIL_CALL);
+
+ /* A region around a nounwind call: no pad dispatched, still loads. */
+ if (test__start_subtest("nounwind_region"))
+ run_shape(skel, skel->progs.entry_nounwind_rec, 1, 0, 0);
+
+ /* A pad in the main program's own frame, not in a subprogram. */
+ if (test__start_subtest("main_program_pad"))
+ run_shape(skel, skel->progs.entry_main_pad, 101, THROW_COOKIE,
+ RAN_MAIN_PAD);
+
+ /* The same call site either way: the subprogram's throw unwinds into
+ * this frame and runs its pad, an extension's stops at its own boundary.
+ */
+ if (test__start_subtest("freplace_subprog_throws"))
+ run_shape(skel, skel->progs.entry_freplace, 7, THROW_COOKIE,
+ RAN_FREPLACE);
+ if (test__start_subtest("freplace_extension_throws"))
+ test_freplace(skel);
+
+ /* A tail call that is taken: the walk ends at the target, so the cookie
+ * comes back from there and this frame's pad does not run.
+ */
+ if (test__start_subtest("tail_call_taken")) {
+ int key = 0, prog_fd = bpf_program__fd(skel->progs.tc_target);
+
+ if (ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel->maps.taken_table),
+ &key, &prog_fd, BPF_ANY),
+ "populate taken_table"))
+ run_shape(skel, skel->progs.entry_tail_taken, 101,
+ THROW_COOKIE, 0);
+ }
+
+ /* A throwing subprog named by a BPF_PSEUDO_FUNC no helper is handed: the
+ * callback check has to look at the bpf_loop(), not at the ld_imm64.
+ */
+ if (test__start_subtest("addr_taken_no_throw"))
+ run_shape(skel, skel->progs.entry_addr_taken, 1, 2, 0);
+ if (test__start_subtest("addr_taken_throw"))
+ run_shape(skel, skel->progs.entry_addr_taken, 101, THROW_COOKIE,
+ RAN_ADDR_TAKEN);
+
+ /* A record covering bpf_throw() itself rather than a call to a frame
+ * that throws: raised, caught up with and delivered in one frame.
+ */
+ if (test__start_subtest("no_subprog_no_throw"))
+ run_shape(skel, skel->progs.entry_no_subprog, 1, 0, 0);
+ if (test__start_subtest("no_subprog_throw"))
+ run_shape(skel, skel->progs.entry_no_subprog, 101, THROW_COOKIE,
+ RAN_NO_SUBPROG);
+
+ /* A pad that calls a subprogram; with a throwing extension in its place,
+ * the nested exception has to stop there, not restart this pad.
+ */
+ if (test__start_subtest("pad_calls_subprog")) {
+ skel->bss->pad_runs = 0;
+ run_shape(skel, skel->progs.entry_pad_calls, 101, THROW_COOKIE,
+ RAN_PAD_CALLS);
+ ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
+ }
+ if (test__start_subtest("pad_calls_throwing_extension"))
+ test_pad_calls_freplace(skel);
+
+ /* A covered throw the sweep leaves last, where the default exception
+ * callback is patched in; the pad's bit needs r6-r9 still spilled.
+ */
+ if (test__start_subtest("pad_before_throw"))
+ run_shape(skel, skel->progs.entry_pad_first, 101, THROW_COOKIE,
+ RAN_PAD_FIRST);
+
+ /* A region whose last instruction is a 16-byte one, so that end - 1
+ * names the half of it that is not an instruction.
+ */
+ if (test__start_subtest("region_ends_on_ldimm64"))
+ run_shape(skel, skel->progs.entry_wide_rec, 101, THROW_COOKIE,
+ RAN_WIDE_REC);
+
+ /* A pad that reloads from and writes to its own frame's stack, which a
+ * JIT addressing the frame through the stack pointer gets wrong.
+ */
+ if (test__start_subtest("pad_uses_own_frame"))
+ run_shape(skel, skel->progs.entry_pad_stack, 101, THROW_COOKIE,
+ RAN_PAD_STACK);
+
+ /* The same, with an uncovered frame between the throw and the pad. */
+ if (test__start_subtest("pad_two_frames_up"))
+ run_shape(skel, skel->progs.entry_deep_pad, 101, THROW_COOKIE,
+ RAN_DEEP_PAD);
+
+ /* An extension program with a cleanup table of its own. */
+ if (test__start_subtest("extension_carries_table"))
+ test_ext_table(skel);
+
+ /* A pad terminated by _Unwind_Resume, which libbpf maps onto the kfunc;
+ * every other program here calls bpf_unwind_resume directly.
+ */
+ if (test__start_subtest("resume_alias"))
+ run_shape(skel, skel->progs.entry_resume_alias, 101,
+ THROW_COOKIE, RAN_RESUME_ALIAS);
+
+ /* A pad that calls a subprogram which tail calls, array empty and then
+ * populated: the tail call releases only the callee's own prologue.
+ */
+ if (test__start_subtest("pad_callee_tail_call")) {
+ int key = 0, prog_fd = bpf_program__fd(skel->progs.pad_tc_target);
+
+ skel->bss->pad_tc_target_ran = 0;
+ skel->bss->pad_runs = 0;
+ run_shape(skel, skel->progs.entry_pad_tail_call, 101,
+ THROW_COOKIE, RAN_PAD_TAIL_CALL);
+ ASSERT_EQ(skel->bss->pad_tc_target_ran, 0, "target not run");
+ ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
+
+ if (ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel->maps.pad_tc_table),
+ &key, &prog_fd, BPF_ANY),
+ "populate pad_tc_table")) {
+ skel->bss->pad_runs = 0;
+ run_shape(skel, skel->progs.entry_pad_tail_call, 101,
+ THROW_COOKIE, RAN_PAD_TAIL_CALL);
+ ASSERT_EQ(skel->bss->pad_tc_target_ran, 1, "target ran");
+ ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
+ }
+ }
+
+ /* The same, into a target that carries a table and throws: that target
+ * is a boundary, so the outer pad runs once, not twice.
+ */
+ if (test__start_subtest("pad_callee_tail_call_throws")) {
+ int key = 0, prog_fd = bpf_program__fd(skel->progs.pad_tc_throw_target);
+
+ if (ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel->maps.pad_tc_table),
+ &key, &prog_fd, BPF_ANY),
+ "populate pad_tc_table")) {
+ skel->bss->pad_runs = 0;
+ skel->bss->tc_target_pad_runs = 0;
+ run_shape(skel, skel->progs.entry_pad_tail_call, 101,
+ THROW_COOKIE, RAN_PAD_TAIL_CALL);
+ /* The target cleaned up after itself, once. */
+ ASSERT_EQ(skel->bss->tc_target_pad_runs, 1,
+ "tc_target_pad_runs");
+ /* And the outer pad was not started over. */
+ ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
+ }
+ }
+
+ exceptions_cleanup_shapes__destroy(skel);
+}
+
+void test_exceptions_cleanup(void)
+{
+ char log[8192] = {};
+ LIBBPF_OPTS(bpf_object_open_opts, opts,
+ .kernel_log_buf = log,
+ .kernel_log_size = sizeof(log));
+ struct exceptions_cleanup *skel;
+ int err;
+
+ skel = exceptions_cleanup__open_opts(&opts);
+ if (!ASSERT_OK_PTR(skel, "open"))
+ return;
+
+ err = exceptions_cleanup__load(skel);
+ if (err) {
+ if (err == -EOPNOTSUPP &&
+ strstr(log, "exception cleanup needs a JIT that can dispatch landing pads"))
+ test__skip();
+ else if (!ASSERT_OK(err, "load"))
+ fprintf(stderr, "%s", log);
+ exceptions_cleanup__destroy(skel);
+ return;
+ }
+
+ /* No throw: foo3 returns 1 ^ 1 == 0, foo2 adds one, no pad runs. */
+ if (test__start_subtest("no_throw"))
+ run(skel, 1, 1, 0);
+
+ /* foo3 throws; every pad runs and the cookie is delivered at entry. */
+ if (test__start_subtest("throw_from_foo3"))
+ run(skel, 101, THROW_COOKIE, PADS_FOO3_THREW);
+
+ /* foo3 returns 2 ^ 1 == 3, so foo2 throws from its own second region;
+ * foo3's frame is long gone, so its pad must not run.
+ */
+ if (test__start_subtest("throw_from_foo2"))
+ run(skel, 2, THROW_COOKIE, PADS_FOO2_THREW);
+
+ exceptions_cleanup__destroy(skel);
+
+ if (test__start_subtest("light_skeleton"))
+ test_light_skeleton();
+
+ test_shapes();
+
+ RUN_TESTS(exceptions_cleanup_fail);
+}
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup.c
new file mode 100644
index 0000000000000..95199a2828faa
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup.c
@@ -0,0 +1,152 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include "exceptions_cleanup.h"
+
+static __used __noinline void __kfunc_btf_anchor(void)
+{
+ bpf_throw(0);
+ bpf_rcu_read_lock();
+ bpf_rcu_read_unlock();
+ bpf_preempt_disable();
+ bpf_preempt_enable();
+ bpf_unwind_resume();
+}
+
+__u64 input = 0;
+__u64 pads_ran = 0;
+__u64 result = 0;
+
+static __used __noinline __u64 foo3(__u64 x)
+{
+ bpf_preempt_disable();
+ if (x > 100)
+ asm volatile (
+ "r1 = %[cookie];"
+ "1:" "call bpf_throw;" /* cleanup region */
+ "2:"
+ "goto 3f;"
+ "4:" /* landing pad */
+ "r7 = r0;"
+ "call bpf_preempt_enable;"
+ PAD_RAN("%[ran]")
+ "r1 = r7;"
+ "call bpf_unwind_resume;"
+ "3:"
+ CLEANUP_REC("1b", "2b", "4b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_FOO3_PREEMPT),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+ bpf_preempt_enable();
+ return x ^ 1;
+}
+
+__u64 never = 0;
+
+static __used __naked __noinline void drop_glue(void)
+{
+ asm volatile (
+ PAD_RAN("%[ran]")
+ "exit;"
+ :
+ : [ran]"i"(RAN_FOO2_DROP), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+static __used __naked __noinline __u64 foo2(void)
+{
+ asm volatile (
+ "r6 = r1;"
+ "call bpf_rcu_read_lock;"
+ "r1 = r6;"
+"1:" "call foo3;" /* cleanup region #1 */
+"2:"
+ "r6 = r0;"
+ "if r6 == 0 goto 5f;"
+ "r1 = %[cookie];"
+"3:" "call bpf_throw;" /* cleanup region #2 */
+"4:"
+ "r0 = 0;"
+ "exit;"
+"5:"
+ "call bpf_rcu_read_unlock;"
+ "r0 = r6;"
+ "r0 += 1;"
+ "exit;"
+"6:" /* landing pad, shared by both regions */
+ "call drop_glue;"
+ "call bpf_rcu_read_unlock;"
+ PAD_RAN("%[ran_rcu]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "6b")
+ CLEANUP_REC("3b", "4b", "6b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran_rcu]"i"(RAN_FOO2_RCU),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+static __used __naked __noinline void foo1v(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call foo2;" /* cleanup region */
+"2:"
+ "r6 = r0;"
+ "call bpf_preempt_enable;"
+ "r1 = %[result] ll;"
+ "*(u64 *)(r1 + 0) = r6;"
+ "goto 7f;"
+"8:" /* landing pad */
+ "call bpf_preempt_enable;"
+ PAD_RAN("%[ran]")
+ "goto 9f;"
+"7:" /* the frame's own exit block */
+ "r0 = 0;"
+ "exit;"
+"9:" /* shared resume block */
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "8b")
+ :
+ : [ran]"i"(RAN_FOO1V_PREEMPT), __imm_addr(input),
+ __imm_addr(result), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+static __used __naked __noinline void bump(void)
+{
+ asm volatile (
+ PAD_RAN("%[ran]")
+ "r1 = %[never] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 == 0 goto 1f;"
+ "r1 = 0;"
+ "call bpf_throw;"
+"1:"
+ "exit;" /* r0 deliberately left alone */
+ :
+ : [ran]"i"(RAN_BUMP), __imm_addr(never), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+__noinline __u64 foo1(void)
+{
+ bump();
+ foo1v();
+ return result;
+}
+
+SEC("syscall")
+int entry(void *ctx)
+{
+ return foo1();
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_ext_table.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_ext_table.c
new file mode 100644
index 0000000000000..d14db48d6b29b
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_ext_table.c
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include "exceptions_cleanup.h"
+
+__u64 ext_pad_ran = 0;
+
+/* Without a 32-bit int in BTF, libbpf's dummy_ksym var gets type id 0. */
+int btf_int_anchor;
+
+static __used __noinline void __kfunc_btf_anchor(void)
+{
+ bpf_throw(0);
+ bpf_preempt_disable();
+ bpf_preempt_enable();
+ bpf_unwind_resume();
+}
+
+static __used __naked __noinline __u64 ext_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = %[cookie];"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "r1 = %[ext_pad_ran] ll;"
+ "r2 = 1;"
+ "*(u64 *)(r1 + 0) = r2;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [cookie]"i"(THROW_COOKIE), __imm_addr(ext_pad_ran)
+ : __clobber_all);
+}
+
+SEC("freplace/fr_callee")
+__u64 new_fr_callee(__u64 x)
+{
+ return ext_frame();
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_fail.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_fail.c
new file mode 100644
index 0000000000000..ce2dac306a84c
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_fail.c
@@ -0,0 +1,600 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_experimental.h"
+#include "bpf_misc.h"
+#include "../test_kmods/bpf_testmod_kfunc.h"
+#include "exceptions_cleanup.h"
+
+__u64 input = 0;
+
+static __used __noinline void __kfunc_btf_anchor(void)
+{
+ bpf_throw(0);
+ bpf_preempt_disable();
+ bpf_preempt_enable();
+ bpf_unwind_resume();
+}
+
+/*
+ * 1. A subprogram that may unwind, also used as a helper callback:
+ * bpf_loop()'s own kernel frame would end the walk before it found a
+ * boundary.
+ */
+static int throwing_cb(__u32 idx, void *ctx)
+{
+ bpf_throw(0xbad);
+ return 0;
+}
+
+static __used __naked __noinline __u64 cb_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+"1:" "call throwing_cb;" /* cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("may unwind and is used as a callback")
+int callback_may_unwind(void *ctx)
+{
+ bpf_loop(1, throwing_cb, NULL, 0);
+ return cb_frame();
+}
+
+/*
+ * 2. A landing pad that reaches both an unwind resume and a plain exit, so
+ * nothing says whether it is a cleanup pad or a catch pad.
+ */
+static __used __naked __noinline __u64 inner_throw(void)
+{
+ asm volatile (
+ "r1 = 1;"
+ "call bpf_throw;"
+ "r0 = 0;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+static __used __naked __noinline __u64 ambiguous_pad_frame(void)
+{
+ asm volatile (
+ "r6 = r1;"
+ "call bpf_preempt_disable;"
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad: two ways out */
+ "call bpf_preempt_enable;"
+ "if r6 > 10 goto 4f;"
+ "call bpf_unwind_resume;"
+ "exit;"
+"4:"
+ "r0 = 0;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("reaches both bpf_unwind_resume() and a plain exit")
+int ambiguous_landing_pad(void *ctx)
+{
+ return ambiguous_pad_frame();
+}
+
+/*
+ * 3. A throw from inside a landing pad: a second walk over the frames the
+ * first one is in the middle of discarding.
+ */
+static __used __naked __noinline __u64 throw_in_pad_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad that throws again */
+ "call bpf_preempt_enable;"
+ "r1 = 2;"
+ "call bpf_throw;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("can throw while an exception is in flight")
+int throw_from_landing_pad(void *ctx)
+{
+ return throw_in_pad_frame();
+}
+
+/*
+ * 4. A cleanup table in a program that also installs an exception callback,
+ * two different answers to what runs on the way out.
+ */
+__noinline int unused_exc_cb(u64 cookie)
+{
+ return 0;
+}
+
+static __used __naked __noinline __u64 cb_and_table_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = 9;"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__exception_cb(unused_exc_cb)
+__failure __msg("cannot be combined with an exception callback")
+int table_with_exception_cb(void *ctx)
+{
+ return cb_and_table_frame();
+}
+
+__u64 never;
+
+/*
+ * 5. A landing pad that calls a subprogram which can throw. Not case 3: the
+ * throw is in another subprogram, so what catches it is the walk of the pad's
+ * body, off subprog_info.might_throw.
+ */
+static __used __noinline void pad_callee_that_throws(void)
+{
+ if (never)
+ bpf_throw(0);
+}
+
+static __used __naked __noinline __u64 pad_calls_thrower_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = 11;"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call pad_callee_that_throws;" /* ...which can throw: refused */
+ "call bpf_preempt_enable;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("which can throw while an exception is in flight")
+int pad_calls_thrower(void *ctx)
+{
+ return pad_calls_thrower_frame();
+}
+
+/*
+ * 6. A catch pad: it ends in a plain exit rather than a resume, and a walker
+ * that calls pads as subroutines cannot hand a frame back its own execution.
+ */
+static __used __naked __noinline __u64 catch_pad_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = 12;"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* catch pad: no resume, it stops here */
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("is not supported yet, only cleanup pads that resume")
+int catch_landing_pad(void *ctx)
+{
+ return catch_pad_frame();
+}
+
+/*
+ * 7. An exception reaching the boundary of a program type that constrains
+ * its return value: delivery makes the cookie that return value, and fentry
+ * has to return 0.
+ */
+static __used __naked __noinline __u64 boundary_throw_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = 7;"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?fentry/bpf_fentry_test1")
+__failure __msg("the register R1 has smin=7 smax=7 should have been in [0, 0]")
+int boundary_delivers(void *ctx)
+{
+ return boundary_throw_frame();
+}
+
+/*
+ * 8. A bpf_unwind_resume() outside any landing pad. Both JITs lower it as
+ * the way back out of a pad, which in ordinary code leaves a live frame
+ * standing with its epilogue skipped.
+ */
+static __used __naked __noinline __u64 stray_resume_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = 13;"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("is not in an exception cleanup landing pad")
+int resume_outside_pad(void *ctx)
+{
+ /* Never taken, but reachable, which is all the verifier needs. */
+ if (never)
+ bpf_unwind_resume();
+ return stray_resume_frame();
+}
+
+/*
+ * 9. A bpf_unwind_resume() in a subprogram a landing pad calls. The
+ * verifier's walk cannot tell it from a resume in the pad itself -- an
+ * exception is in flight either way -- so the rule is static: a resume sits in
+ * a pad body.
+ */
+static __used __naked __noinline void resume_in_callee(void)
+{
+ asm volatile (
+ "call bpf_unwind_resume;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+static __used __naked __noinline __u64 pad_calls_resumer_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = 14;"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "call resume_in_callee;" /* ...which resumes: refused */
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("is not in an exception cleanup landing pad")
+int resume_in_pad_callee(void *ctx)
+{
+ return pad_calls_resumer_frame();
+}
+
+/*
+ * 10. A bpf_unwind_resume() in a program carrying no cleanup table, where
+ * that static rule does not run at all. do_check() refuses it on the state not
+ * unwinding, and has to: the JITs lower every one of these the same way.
+ */
+static __used __naked __noinline __u64 no_table_resume_frame(void)
+{
+ asm volatile (
+ "call bpf_unwind_resume;"
+ "r0 = 0;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("reached without an exception in flight")
+int resume_without_table(void *ctx)
+{
+ return no_table_resume_frame();
+}
+
+/*
+ * 11. A landing pad that is itself a covered call site, so an exception out
+ * of it would have nowhere to go. Hand-written only: LLVM sinks a function's
+ * pads past every range it emits.
+ */
+static __used __naked __noinline __u64 nested_pad_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+"1:" "call inner_throw;" /* first cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* first pad, second region's call */
+ "call bpf_preempt_enable;"
+"4:"
+ "call bpf_unwind_resume;"
+ "exit;"
+"5:" /* second pad */
+ "call bpf_preempt_enable;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ CLEANUP_REC("3b", "4b", "5b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("is inside the call-site range of")
+int nested_landing_pad(void *ctx)
+{
+ return nested_pad_frame();
+}
+
+/*
+ * 12. A tail call in a landing pad: it unwinds the prologue off the stack
+ * pointer, which in a pad is the walker's.
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} tc_map SEC(".maps");
+
+static __used __naked __noinline __u64 tail_call_pad_frame(void)
+{
+ asm volatile (
+ "r6 = r1;"
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r1 = r6;"
+ "r2 = %[tc_map] ll;"
+ "r3 = 0;"
+ "call %[bpf_tail_call];"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : __imm(bpf_tail_call), __imm_addr(tc_map)
+ : __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("is in an exception cleanup landing pad")
+int tail_call_in_pad(void *ctx)
+{
+ return tail_call_pad_frame();
+}
+
+#if defined(__BPF_FEATURE_STACK_ARGUMENT)
+
+/*
+ * 13. A call that passes an argument on the stack, in a landing pad: the
+ * outgoing area the callee reads is not the one the caller wrote, the frame
+ * being the unwinding one and the stack pointer the walker's.
+ */
+static __used __noinline __u64 six_args(__u64 a, __u64 b, __u64 c, __u64 d,
+ __u64 e, __u64 f)
+{
+ return a + b + c + d + e + f;
+}
+
+static __used __naked __noinline __u64 stack_arg_pad_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "r1 = 1;"
+ "r2 = 2;"
+ "r3 = 3;"
+ "r4 = 4;"
+ "r5 = 5;"
+ "*(u64 *)(r11 - 8) = 6;" /* the sixth argument */
+ "call six_args;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("on-stack call argument in an exception cleanup landing pad")
+int stack_arg_in_pad(void *ctx)
+{
+ return stack_arg_pad_frame();
+}
+
+/*
+ * 14. The same, reached the other way: a kfunc whose by-value argument runs
+ * past the five argument registers, where the JIT fills the outgoing area and
+ * the rule above has no store to catch. The C call gives the extern its BTF.
+ */
+static __used __noinline void __nofit_btf_anchor(void)
+{
+ struct prog_test_pair_arg s = {};
+
+ bpf_kfunc_call_test_pair_arg_nofit(1, 2, 3, 4, s);
+}
+
+static __used __naked __noinline __u64 kfunc_arg_pad_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "r1 = 1;"
+ "r2 = 2;"
+ "r3 = 3;"
+ "r4 = 4;"
+ "r5 = 5;"
+ "call bpf_kfunc_call_test_pair_arg_nofit;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("on-stack call argument in an exception cleanup landing pad")
+int kfunc_stack_arg_in_pad(void *ctx)
+{
+ return kfunc_arg_pad_frame();
+}
+
+#endif /* __BPF_FEATURE_STACK_ARGUMENT */
+
+/*
+ * 15. A landing pad entered by ordinary control flow, arriving with none of
+ * what the walker sets up. Nothing static sees it -- the resume really is in a
+ * pad body -- so do_check() refuses it on the state not unwinding.
+ */
+static __used __naked __noinline __u64 jump_into_pad_frame(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r6 = *(u64 *)(r1 + 0);"
+ "if r6 > 7 goto 4f;" /* an ordinary branch into the pad */
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r7 = r0;"
+"4:" /* ... and its second instruction */
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : __imm_addr(input)
+ : __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("reached without an exception in flight")
+int jump_into_pad(void *ctx)
+{
+ return jump_into_pad_frame();
+}
+
+#if defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)
+
+/*
+ * 16. A landing pad that reaches an indirect jump, which cannot be told from
+ * a catch pad. SEC("socket") because a jump table entry is an offset from the
+ * program's section symbol, and "?syscall" is not a name assembly can use.
+ */
+static __used __naked __noinline void gotox_thrower(void)
+{
+ asm volatile (
+ "r1 = 15;"
+ "call bpf_throw;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+SEC("socket")
+__failure __msg("reaches an indirect jump")
+__naked void gotox_in_pad(void)
+{
+ asm volatile (
+ ".pushsection .jumptables,\"\",@progbits;"
+"jt0_%=:"
+ ".quad l0_%= - socket;"
+ ".quad l1_%= - socket;"
+ ".size jt0_%=, 16;"
+ ".global jt0_%=;"
+ ".popsection;"
+
+"1:" "call gotox_thrower;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r1 = jt0_%= ll;"
+ "r1 += 8;"
+ "r2 = *(u64 *)(r1 + 0);"
+ /* gotox r2. Spelled as a raw insn on purpose: the "gotox" mnemonic
+ * only reached the LLVM assembler in llvm 22, and BPF_RAW_INSN()
+ * needs <linux/bpf.h>, which this file cannot have -- vmlinux.h
+ * already defines the uapi enums.
+ */
+ ".8byte 0x20d;"
+"l0_%=:"
+ "call bpf_unwind_resume;"
+ "exit;"
+"l1_%=:"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+#endif /* x86 || arm64 */
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_freplace.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_freplace.c
new file mode 100644
index 0000000000000..afb358fd3d405
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_freplace.c
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "exceptions_cleanup.h"
+
+/* Without a 32-bit int in BTF, libbpf's dummy_ksym var gets type id 0. */
+int btf_int_anchor;
+
+SEC("freplace/fr_callee")
+__u64 new_fr_callee(__u64 x)
+{
+ bpf_throw(THROW_COOKIE);
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_light.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_light.c
new file mode 100644
index 0000000000000..de7ae478d67f3
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_light.c
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include "exceptions_cleanup.h"
+
+static __used __noinline void __kfunc_btf_anchor(void)
+{
+ bpf_throw(0);
+ bpf_preempt_disable();
+ bpf_preempt_enable();
+ bpf_unwind_resume();
+}
+
+__u64 pads_ran = 0;
+
+SEC("syscall")
+__naked int entry_light(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = %[cookie];"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_LIGHT),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_pad_freplace.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_pad_freplace.c
new file mode 100644
index 0000000000000..eabac6baabb70
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_pad_freplace.c
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "exceptions_cleanup.h"
+
+/* Without a 32-bit int in BTF, libbpf's dummy_ksym var gets type id 0. */
+int btf_int_anchor;
+
+SEC("freplace/pad_callee")
+__u64 new_pad_callee(__u64 x)
+{
+ bpf_throw(INNER_COOKIE);
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c
new file mode 100644
index 0000000000000..f5eb2ff15c896
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c
@@ -0,0 +1,863 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include "exceptions_cleanup.h"
+
+#define PAD_COUNT \
+ "r1 = %[pad_runs] ll;" \
+ "r2 = *(u64 *)(r1 + 0);" \
+ "r2 += 1;" \
+ "*(u64 *)(r1 + 0) = r2;"
+
+static __used __noinline void __kfunc_btf_anchor(void)
+{
+ bpf_throw(0);
+ bpf_rcu_read_lock();
+ bpf_rcu_read_unlock();
+ bpf_preempt_disable();
+ bpf_preempt_enable();
+ bpf_unwind_resume();
+}
+
+__u64 input = 0;
+__u64 magic = 0x5eed;
+__u64 pads_ran = 0;
+__u64 pad_runs = 0;
+
+/*
+ * 1. Everything a cleanup table leaves dead: the continuation after a throw,
+ * the tail after a pad's resume, an ld_imm64 and a conditional branch inside
+ * that tail, and a block reached only by the dead continuation.
+ */
+static __used __naked __noinline __u64 sweep_frame(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r6 = *(u64 *)(r1 + 0);"
+ "call bpf_preempt_disable;"
+ "if r6 < 101 goto 6f;"
+ "r1 = %[cookie];"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "goto 3f;"
+"4:" /* landing pad */
+ "r7 = r0;"
+ "call bpf_preempt_enable;"
+ PAD_RAN("%[ran]")
+ "r1 = r7;"
+ "call bpf_unwind_resume;"
+ "r1 = %[pads_ran] ll;"
+ "r2 = *(u64 *)(r1 + 0);"
+ "if r2 == 0 goto 5f;"
+ "call bpf_preempt_enable;"
+ "r0 = 7;"
+ "exit;"
+"5:"
+ "r0 = 8;"
+ "exit;"
+"3:" /* dead: only the dead goto reaches it */
+ "r0 = 9;"
+ "exit;"
+"6:" /* live: the ordinary return */
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "4b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_SWEEP),
+ __imm_addr(input), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_sweep(void *ctx)
+{
+ return sweep_frame();
+}
+
+/*
+ * 2. A callee called from both a covered and an uncovered site: the pad is
+ * recorded on the call site, not on the callee. The lock sits between the two
+ * calls because the frame really would leak it if the uncovered call unwound.
+ */
+static __used __noinline __u64 shared_callee(__u64 x)
+{
+ if (x > 100)
+ bpf_throw(THROW_COOKIE);
+ return x + 1;
+}
+
+static __used __naked __noinline __u64 shared_frame(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r6 = *(u64 *)(r1 + 0);"
+ "r1 = 0;"
+ "call shared_callee;"
+ "call bpf_rcu_read_lock;"
+ "r1 = r6;"
+"1:" "call shared_callee;" /* cleanup region */
+"2:"
+ "r6 = r0;"
+ "call bpf_rcu_read_unlock;"
+ "r0 = r6;"
+ "exit;"
+"3:" /* landing pad */
+ "r7 = r0;"
+ "call bpf_rcu_read_unlock;"
+ PAD_RAN("%[ran]")
+ "r1 = r7;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_SHARED), __imm_addr(input), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_shared(void *ctx)
+{
+ return shared_frame();
+}
+
+/*
+ * 3. A landing pad that reads its frame's callee-saved registers, which only
+ * the spill in the discarded callee's prologue still holds. The callee fills
+ * r6-r9 with something else before it throws, so the pad's check passes only
+ * if the walker found that spill.
+ */
+#define LOAD_MAGIC_REGS \
+ "r1 = %[magic] ll;" \
+ "r6 = *(u64 *)(r1 + 0);" \
+ "r7 = r6;" \
+ "r7 += 1;" \
+ "r8 = r6;" \
+ "r8 += 2;" \
+ "r9 = r6;" \
+ "r9 += 3;"
+
+/* Set @bit only if r6-r9 still hold what LOAD_MAGIC_REGS put there. */
+#define CHECK_MAGIC_REGS(bit) \
+ "r1 = %[magic] ll;" \
+ "r2 = *(u64 *)(r1 + 0);" \
+ "if r6 != r2 goto 9f;" \
+ "r2 += 1;" \
+ "if r7 != r2 goto 9f;" \
+ "r2 += 1;" \
+ "if r8 != r2 goto 9f;" \
+ "r2 += 1;" \
+ "if r9 != r2 goto 9f;" \
+ PAD_RAN(bit) \
+ "9:"
+
+static __used __naked __noinline __u64 regs_thrower(void)
+{
+ asm volatile (
+ /* Not this frame's to keep, and that is the point. */
+ "r6 = 0xdead;"
+ "r7 = 0xbeef;"
+ "r8 = 0xcafe;"
+ "r9 = 0xf00d;"
+ "r1 = %[cookie];"
+ "call bpf_throw;"
+ "r0 = 0;"
+ "exit;"
+ :
+ : [cookie]"i"(THROW_COOKIE)
+ : __clobber_all);
+}
+
+static __used __naked __noinline __u64 regs_frame(void)
+{
+ asm volatile (
+ LOAD_MAGIC_REGS
+ "call bpf_preempt_disable;"
+"1:" "call regs_thrower;" /* cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ CHECK_MAGIC_REGS("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_REGS),
+ __imm_addr(magic), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_regs(void *ctx)
+{
+ return regs_frame();
+}
+
+/*
+ * 4. The same, with a tail-call-reachable callee: its prologue pushes the tail
+ * call counter between the program stack and the spill area, so the spill the
+ * walker reads moves. The array is left empty; being reachable is the point.
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} jmp_table SEC(".maps");
+
+static __used __noinline __u64 tc_thrower(void *ctx)
+{
+ /* Never taken; its presence is what makes this frame, whose spill the
+ * walker reads, tail-call-reachable.
+ */
+ bpf_tail_call_static(ctx, &jmp_table, 0);
+ asm volatile (
+ "r6 = 0xdead;"
+ "r7 = 0xbeef;"
+ "r8 = 0xcafe;"
+ "r9 = 0xf00d;"
+ "r1 = %[cookie];"
+ "call bpf_throw;"
+ :
+ : [cookie]"i"(THROW_COOKIE)
+ : __clobber_all);
+ return 0;
+}
+
+/*
+ * The frame with the pad is the program itself, and __naked: r1 holds the
+ * context at entry, which is the only place to get one for bpf_tail_call().
+ */
+SEC("syscall")
+__naked int entry_tail_call(void)
+{
+ asm volatile (
+ "*(u64 *)(r10 - 8) = r1;" /* the context, straight from entry */
+ LOAD_MAGIC_REGS
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 < 101 goto 8f;"
+ "r1 = *(u64 *)(r10 - 8);"
+"1:" "call tc_thrower;" /* cleanup region */
+"2:"
+"8:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ CHECK_MAGIC_REGS("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_TAIL_CALL), __imm_addr(input),
+ __imm_addr(magic), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 5. A landing pad in the main program's own frame. jit_subprogs() compiles it
+ * as func[0], but the ksym the walker finds is the outer bpf_prog's, so the
+ * table has to be handed over or the pad is never dispatched -- silently.
+ */
+SEC("syscall")
+__naked int entry_main_pad(void)
+{
+ asm volatile (
+ LOAD_MAGIC_REGS
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 < 101 goto 8f;"
+"1:" "call regs_thrower;" /* cleanup region */
+"2:"
+"8:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ CHECK_MAGIC_REGS("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_MAIN_PAD), __imm_addr(input),
+ __imm_addr(magic), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 6. A tail call that is really taken: the target is a program in its own
+ * right, so the walk ends there and this frame's pad does not run. The callee
+ * can also throw on a path never taken, which keeps the pad out of the sweep.
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} taken_table SEC(".maps");
+
+SEC("syscall")
+int tc_target(void *ctx)
+{
+ bpf_throw(THROW_COOKIE);
+ return 0;
+}
+
+static __used __noinline __u64 tc_taken_callee(void *ctx, __u64 x)
+{
+ /* Never true at run time; the verifier cannot know that, and its
+ * unwind out of here is what keeps the caller's pad alive.
+ */
+ if (x == 7)
+ bpf_throw(THROW_COOKIE);
+ bpf_tail_call_static(ctx, &taken_table, 0);
+ return 0;
+}
+
+SEC("syscall")
+__naked int entry_tail_taken(void)
+{
+ asm volatile (
+ "*(u64 *)(r10 - 8) = r1;" /* the context, straight from entry */
+ "r1 = %[input] ll;"
+ "r2 = *(u64 *)(r1 + 0);"
+ "if r2 < 101 goto 8f;"
+ "r1 = *(u64 *)(r10 - 8);"
+"1:" "call tc_taken_callee;" /* cleanup region */
+"2:"
+ "exit;" /* the cookie, delivered at tc_target */
+"8:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad: must not run */
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_TC_TAKEN), __imm_addr(input), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 7. An extension program over the callee of a covered call. The walk ends in
+ * the extension's frame, as it does for a tail call target, so the pad does
+ * not run; fr_callee() can also throw by itself, giving the same call site
+ * both answers.
+ */
+__noinline __u64 fr_callee(__u64 x)
+{
+ if (x == 7)
+ bpf_throw(THROW_COOKIE);
+ return x + 1;
+}
+
+SEC("syscall")
+__naked int entry_freplace(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call fr_callee;" /* cleanup region */
+"2:"
+ "exit;"
+"3:" /* landing pad */
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_FREPLACE), __imm_addr(input), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 8. A throwing subprogram named by a BPF_PSEUDO_FUNC on a path never taken.
+ * Handing one to a helper is what is refused, not naming it, so anything going
+ * by the ld_imm64 alone turns this program away.
+ */
+static __used __noinline int cb_thrower(__u32 idx, void *ctx)
+{
+ bpf_throw(THROW_COOKIE);
+ return 0;
+}
+
+static __used __noinline __u64 addr_taken_callee(__u64 x)
+{
+ if (x <= 100)
+ return x + 1;
+ bpf_throw(THROW_COOKIE);
+ return bpf_loop(1, cb_thrower, NULL, 0);
+}
+
+SEC("syscall")
+__naked int entry_addr_taken(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call addr_taken_callee;" /* cleanup region */
+"2:"
+ "exit;"
+"3:" /* landing pad */
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_ADDR_TAKEN), __imm_addr(input), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 9. A record that covers bpf_throw() itself: the frame that raises the
+ * exception is the frame the record covers and the boundary both, so the pad
+ * runs on the way to delivering the cookie out of the program it came from.
+ */
+SEC("syscall")
+__naked int entry_no_subprog(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 < 101 goto 8f;"
+ "call bpf_preempt_disable;"
+ "r1 = %[cookie];"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "exit;"
+"8:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_NO_SUBPROG),
+ __imm_addr(input), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 10. A landing pad that calls a subprogram an extension can replace. The
+ * load-time rule cannot see the extension coming, so what stops a nested
+ * exception is the walk, which ends in the extension's own frame. pad_runs
+ * says the pad ran once rather than twice.
+ */
+__noinline __u64 pad_callee(__u64 x)
+{
+ return x + 1;
+}
+
+static __used __noinline __u64 pc_thrower(__u64 x)
+{
+ if (x > 100)
+ bpf_throw(THROW_COOKIE);
+ return x + 1;
+}
+
+static __used __naked __noinline __u64 pad_calls_frame(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call pc_thrower;" /* cleanup region */
+"2:"
+ "exit;"
+"3:" /* landing pad */
+ "r6 = r0;"
+ "r1 = 1;"
+ "call pad_callee;" /* an extension can stand in here */
+ PAD_COUNT
+ PAD_RAN("%[ran]")
+ "r1 = r6;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_PAD_CALLS), __imm_addr(input), __imm_addr(pads_ran),
+ __imm_addr(pad_runs)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_pad_calls(void *ctx)
+{
+ return pad_calls_frame();
+}
+
+/*
+ * 11. A covered bpf_throw() the sweep leaves as the last instruction, where
+ * the default exception callback is then patched in -- the one patchlet that
+ * does not keep the call it replaced in the last slot, so the marks have to
+ * follow it. The r6-r9 check is what reports a lost throw site mark.
+ */
+SEC("syscall")
+__naked int entry_pad_first(void)
+{
+ asm volatile (
+ LOAD_MAGIC_REGS
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 < 101 goto 7f;"
+ "goto 4f;"
+"3:" /* landing pad, ahead of the call */
+ CHECK_MAGIC_REGS("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+"7:"
+ "r0 = 0;"
+ "exit;"
+"4:"
+ "r1 = %[cookie];"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "exit;" /* dead: swept, leaving the call last */
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_PAD_FIRST),
+ __imm_addr(input), __imm_addr(magic), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 12. A cleanup region whose last instruction is a 16-byte one, so end - 1
+ * names the half that is not an instruction of its own. A well formed region
+ * that a rule against it would turn away.
+ */
+static __used __naked __noinline __u64 wide_rec_frame(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r6 = *(u64 *)(r1 + 0);"
+ "call bpf_rcu_read_lock;"
+ "r1 = r6;"
+"1:" "call shared_callee;" /* cleanup region begins */
+ "r1 = %[magic] ll;" /* ... and ends on this pair */
+"2:"
+ "r6 = r0;"
+ "call bpf_rcu_read_unlock;"
+ "r0 = r6;"
+ "exit;"
+"3:" /* landing pad */
+ "r7 = r0;"
+ "call bpf_rcu_read_unlock;"
+ PAD_RAN("%[ran]")
+ "r1 = r7;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_WIDE_REC), __imm_addr(input), __imm_addr(magic),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_wide_rec(void *ctx)
+{
+ return wide_rec_frame();
+}
+
+/*
+ * 13. A pad that works out of its own frame's stack, the shape every
+ * compiler-generated pad has. A JIT that addresses the frame through the
+ * stack pointer -- arm64 -- has to address a pad's frame some other way. Both
+ * directions are here: the reload sees the frame, and the store lands in it.
+ */
+static __used __naked __noinline __u64 pad_stack_frame(void)
+{
+ asm volatile (
+ "r1 = %[magic] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "*(u64 *)(r10 - 8) = r1;" /* what the pad will want */
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call pc_thrower;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r6 = r0;"
+ "r7 = *(u64 *)(r10 - 8);" /* reload it out of the frame */
+ "*(u64 *)(r10 - 16) = r7;" /* and write the frame while here */
+ "r1 = %[magic] ll;"
+ "r2 = *(u64 *)(r1 + 0);"
+ "if r7 != r2 goto 9f;"
+ "r3 = *(u64 *)(r10 - 16);"
+ "if r3 != r2 goto 9f;"
+ PAD_RAN("%[ran]")
+"9:"
+ "r1 = r6;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_PAD_STACK), __imm_addr(input), __imm_addr(magic),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_pad_stack(void *ctx)
+{
+ return pad_stack_frame();
+}
+
+/*
+ * 14. The same, with a frame in between that has no pad of its own, so the
+ * liveness query for an outer frame has more than one frame to walk and the
+ * pad has to be counted at every step.
+ */
+static __used __noinline __u64 deep_mid(__u64 x)
+{
+ return pc_thrower(x) + 1;
+}
+
+static __used __naked __noinline __u64 deep_frame(void)
+{
+ asm volatile (
+ "r1 = %[magic] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "*(u64 *)(r10 - 8) = r1;" /* nothing but the pad reads this */
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call deep_mid;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r6 = r0;"
+ "r7 = *(u64 *)(r10 - 8);"
+ "r1 = %[magic] ll;"
+ "r2 = *(u64 *)(r1 + 0);"
+ "if r7 != r2 goto 9f;"
+ PAD_RAN("%[ran]")
+"9:"
+ "r1 = r6;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_DEEP_PAD), __imm_addr(input), __imm_addr(magic),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_deep_pad(void *ctx)
+{
+ return deep_frame();
+}
+
+/*
+ * 15. A region around a call the kernel knows cannot unwind: no call site is
+ * marked, nothing reaches the pad, and the sweep removes it. The program is
+ * otherwise ordinary and has to load.
+ */
+static __used __naked __noinline __u64 nounwind_rec_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+"1:" "call bpf_preempt_enable;" /* cleanup region: nounwind */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad, never dispatched */
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_NOUNWIND_REC), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_nounwind_rec(void *ctx)
+{
+ return nounwind_rec_frame();
+}
+
+/*
+ * 16. The name a frontend gives the resume. Every pad above calls
+ * bpf_unwind_resume(); LLVM emits _Unwind_Resume() and libbpf maps one onto
+ * the other, so this program is what keeps that mapping tested.
+ */
+extern void _Unwind_Resume(void) __ksym;
+
+static __used __noinline void __resume_alias_btf_anchor(void)
+{
+ _Unwind_Resume();
+}
+
+static __used __naked __noinline __u64 resume_alias_frame(void)
+{
+ asm volatile (
+"1:" "call regs_thrower;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ PAD_RAN("%[ran]")
+ "call _Unwind_Resume;" /* the frontend's name for it */
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_RESUME_ALIAS), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_resume_alias(void *ctx)
+{
+ return resume_alias_frame();
+}
+
+/*
+ * 17. A landing pad that calls a subprogram which tail calls. What a pad may
+ * not contain is a tail call of its own, which would unwind a prologue the
+ * walker's stack never held; a callee's prologue really did run there, so its
+ * tail call releases exactly that and the target returns into the pad. The
+ * tail call counter comes out of the unwinding frame, which is one of the
+ * pad's own subprogram and so really holds one.
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} pad_tc_table SEC(".maps");
+
+__u64 pad_tc_target_ran = 0;
+
+SEC("syscall")
+int pad_tc_target(void *ctx)
+{
+ pad_tc_target_ran += 1;
+ return 0;
+}
+
+static __used __noinline __u64 pad_tc_callee(void *ctx)
+{
+ /* Taken only once the test has populated the array. */
+ bpf_tail_call_static(ctx, &pad_tc_table, 0);
+ return 0;
+}
+
+/*
+ * The frame with the pad is the program itself, and __naked: r1 holds the
+ * context at entry, which is the only place to get one for bpf_tail_call().
+ * The pad reloads it from its own frame's stack.
+ */
+SEC("syscall")
+__naked int entry_pad_tail_call(void)
+{
+ asm volatile (
+ "*(u64 *)(r10 - 8) = r1;" /* the context, straight from entry */
+ LOAD_MAGIC_REGS
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 < 101 goto 8f;"
+"1:" "call regs_thrower;" /* cleanup region */
+"2:"
+"8:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ PAD_COUNT
+ "r1 = *(u64 *)(r10 - 8);"
+ "call pad_tc_callee;"
+ /* Only if the frame survived the callee's tail call. */
+ CHECK_MAGIC_REGS("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_PAD_TAIL_CALL), __imm_addr(input),
+ __imm_addr(magic), __imm_addr(pads_ran), __imm_addr(pad_runs)
+ : __clobber_all);
+}
+
+/*
+ * 18. The other target for that same tail call: a program carrying a cleanup
+ * table of its own, which throws while the outer exception is still in flight.
+ * The tail call made it a boundary, so the inner walk runs its pad and ends in
+ * its own frame, never reaching the walker's frames above it: the outer pad is
+ * not restarted and the outer cookie is still the one delivered. The outer
+ * pad's r6-r9, which this target overwrites, come back with its frame.
+ */
+__u64 tc_target_pad_runs = 0;
+__u64 inner_magic = 0xd00d;
+
+static __used __naked __noinline __u64 inner_thrower(void)
+{
+ asm volatile (
+ /* Not this frame's to keep, the same as regs_thrower. */
+ "r6 = 0xf00d;"
+ "r7 = 0xcafe;"
+ "r8 = 0xbeef;"
+ "r9 = 0xdead;"
+ "r1 = %[cookie];"
+ "call bpf_throw;"
+ "r0 = 0;"
+ "exit;"
+ :
+ : [cookie]"i"(INNER_COOKIE)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__naked int pad_tc_throw_target(void)
+{
+ asm volatile (
+ /* Distinct from the outer pad's, so neither can stand in for it. */
+ "r1 = %[inner_magic] ll;"
+ "r6 = *(u64 *)(r1 + 0);"
+ "r7 = r6;"
+ "r7 += 1;"
+ "r8 = r6;"
+ "r8 += 2;"
+ "r9 = r6;"
+ "r9 += 3;"
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 < 101 goto 8f;"
+"1:" "call inner_thrower;" /* cleanup region */
+"2:"
+"8:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ /* This frame's own r6-r9, not the outer pad's. */
+ "r1 = %[inner_magic] ll;"
+ "r2 = *(u64 *)(r1 + 0);"
+ "if r6 != r2 goto 9f;"
+ "r2 += 1;"
+ "if r7 != r2 goto 9f;"
+ "r2 += 1;"
+ "if r8 != r2 goto 9f;"
+ "r2 += 1;"
+ "if r9 != r2 goto 9f;"
+ "r1 = %[tc_target_pad_runs] ll;"
+ "r2 = *(u64 *)(r1 + 0);"
+ "r2 += 1;"
+ "*(u64 *)(r1 + 0) = r2;"
+"9:"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : __imm_addr(input), __imm_addr(inner_magic),
+ __imm_addr(tc_target_pad_runs)
+ : __clobber_all);
+}
+
+char _license[] SEC("license") = "GPL";
|
| 3/2 | 2026/09/18 21:42 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 72900 output: 35 thoughts: 1239 Thoughts: **BPF Exception Cleanup Landing Pads: Syzkaller Worthy?** Okay, so I've got this patch for BPF exception cleanup landing pads. My initial thought is, "This looks *interesting*." Let's break it down and decide if this is syzkaller material. First, I see it's focused on amd64, though there are arm64 changes too. I see the usual suspects: BPF infrastructure files, verifier modifications, JIT changes, and a whole new set of exception handling code. Specifically, this patch introduces a whole new feature set: The new attribute `cleanup_info`, `cleanup_info_cnt`, `cleanup_info_rec_size` in `bpf_attr` for `BPF_PROG_LOAD`, verifier work with new functions, JIT support for amd64, and the introduction of exception handling and unwinding within the kernel. The files touched are extensive and spread across the entire BPF infrastructure. Now, based on my understanding of the "CORE TRIAGE PHILOSOPHY," this screams "WorthFuzzing=true." BPF is *absolutely* a core subsystem. The patch modifies code in `kernel/bpf/*`, `arch/x86/net/*`, and messes with verifier, JIT, the `bpf()` syscall (`BPF_PROG_LOAD`), and exception handling. Syzkaller is a natural fit here. This code is definitely reachable through the `bpf()` syscall which syzkaller tests heavily. And of course, QEMU/GCE amd64 environments are the target. This patch adds a new interface to `bpf()` and adds substantial verification and runtime logic. So, I'm leaning heavily towards "yes." Next, I need to check the `EnableConfigs`. Let's look for conditional compilation. I can see in `kernel/bpf/Makefile` and `arch/x86/net/Makefile` the code is gated on `CONFIG_BPF_SYSCALL` and `CONFIG_BPF_JIT`. Importantly, the new functionality relies on `CONFIG_UNWINDER_ORC` to be enabled, specifically through `bpf_jit_supports_cleanup_pads()`. This is key. We absolutely need that enabled. I'll need to confirm that via `grepper` or by reading the Kconfig files. In conclusion, my assessment is: this patch is highly promising for syzkaller, provided we set the correct kernel configs. |
| 4/2 | 2026/09/18 21:43 | tool | grepper |
0mArgs:{
"Expression": "bpf_jit_supports_cleanup_pads",
"PathPrefix": "arch/x86"
}Results: {
"Output": "arch/x86/net/bpf_jit_comp.c=4544=bool bpf_jit_supports_exceptions(void)\n--\narch/x86/net/bpf_jit_comp.c-4552-\narch/x86/net/bpf_jit_comp.c:4553:bool bpf_jit_supports_cleanup_pads(void)\narch/x86/net/bpf_jit_comp.c-4554-{\n"
} |
| 5/2 | 2026/09/18 21:43 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 74839 output: 45 thoughts: 38 Thoughts: Here's a summary of my thought process, as requested: **Diving into the BPF JIT Compiler** Okay, time to dig into the x86 BPF JIT compiler. I need to understand how this particular optimization is working, so I'll start by checking out the relevant source code. Let's see... I'm going to pull up `arch/x86/net/bpf_jit_comp.c` and focus on lines 4550 through 4560. My plan is to use a read-file approach. This should provide the low-level details I need to understand the function of this code segment in the compilation process. It's a quick and efficient method for getting familiar with this specific section of the code. Let's see what we can find. |
| 6/2 | 2026/09/18 21:43 | tool | read-file |
0mArgs:{
"File": "arch/x86/net/bpf_jit_comp.c",
"FirstLine": 4545,
"LineCount": 20
}Results: {
"Contents": "4545:\t{\n4546:\t\t/* We unwind through both kernel frames (starting from within bpf_throw\n4547:\t\t * call) and BPF frames. Therefore we require ORC unwinder to be enabled\n4548:\t\t * to walk kernel frames and reach BPF frames in the stack trace.\n4549:\t\t */\n4550:\t\treturn IS_ENABLED(CONFIG_UNWINDER_ORC);\n4551:\t}\n4552:\t\n4553:\tbool bpf_jit_supports_cleanup_pads(void)\n4554:\t{\n4555:\t\treturn IS_ENABLED(CONFIG_UNWINDER_ORC);\n4556:\t}\n4557:\t\n4558:\tbool bpf_jit_supports_private_stack(void)\n4559:\t{\n4560:\t\treturn true;\n4561:\t}\n4562:\t\n4563:\tvoid arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie)\n4564:\t{\n"
} |
| 7/2 | 2026/09/18 21:43 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 75404 output: 30 thoughts: 70 Thoughts: Here's my thought process, as it stands: **Kernel Configuration Dependencies for eBPF** Okay, so I'm looking at these kernel configs: `CONFIG_BPF_SYSCALL`, `CONFIG_BPF_JIT`, and `CONFIG_UNWINDER_ORC`. My initial thought is to make sure I haven't missed anything crucial. These are the big three, obviously, for eBPF functionality. But, I know from experience that kernel configurations can be deceptively interconnected. Time to dig into the `Kconfig` files and see if there are any other direct dependencies or indirect relationships I need to be aware of. I'll search specifically for the definitions of `BPF_JIT`, `BPF_SYSCALL`, and `UNWINDER_ORC` and make a list of anything related to these that might be relevant. This ensures I don't miss some other subtly important config that's required. I'll check all of the dependencies for this functionality. |
| 8/2 | 2026/09/18 21:43 | tool | grepper |
0mError:no matches Args: {
"Expression": "config BPF_JIT",
"PathPrefix": "init/Kconfig"
}Results: {
"error": "no matches"
} |
| 9/2 | 2026/09/18 21:43 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 75732 output: 34 thoughts: 18 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Hunting Down BPF Configuration Options** Okay, so I need to track down the configuration options for BPF JIT and the system call filter. My immediate thought is to locate `BPF_JIT` and `BPF_SYSCALL` in the kernel config. Given my experience, I know these are likely defined in the kernel's build configuration. I'll probably start by grepping the `Kconfig` files, or maybe `defconfig` and its associated files, to find where these symbols are declared and how they influence the build. Knowing the kernel's internal structure, I'd then drill down from there to understand how these flags affect various BPF subsystems. It's also important to understand the relationships between them. For instance, is `BPF_SYSCALL` dependent on `BPF_JIT`? Or does `BPF_JIT` have a dependency on a different option? I'll also be mindful of architecture-specific variations, so I'll probably keep in mind that the configuration might differ between x86_64, ARM, and other architectures. Ultimately, I'm trying to understand the dependencies and implications of these settings. |
| 10/2 | 2026/09/18 21:43 | tool | grepper |
0mArgs:{
"Expression": "config BPF_SYSCALL",
"PathPrefix": "kernel/bpf/Kconfig"
}Results: {
"Output": "kernel/bpf/Kconfig=29=menu \"BPF subsystem\"\nkernel/bpf/Kconfig-30-\nkernel/bpf/Kconfig:31:config BPF_SYSCALL\nkernel/bpf/Kconfig-32-\tbool \"Enable bpf() system call\"\n"
} |
| 11/2 | 2026/09/18 21:43 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 76135 output: 34 thoughts: 19 Thoughts: Here's my take on the thought process: **Diving into the BPF Kernel Configuration** Okay, so I need to understand something about the BPF configuration within the kernel. My instinct tells me to start digging into the `kernel/bpf/Kconfig` file. I'm aiming for around line 31. That's usually where the meat of the initial setup, or maybe some key options, are defined. Time to see what's what and get a handle on the landscape there. Let's see what goodies await. |
| 12/2 | 2026/09/18 21:43 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/Kconfig",
"FirstLine": 30,
"LineCount": 40
}Results: {
"Contents": " 30:\t\n 31:\tconfig BPF_SYSCALL\n 32:\t\tbool \"Enable bpf() system call\"\n 33:\t\tselect BPF\n 34:\t\tselect IRQ_WORK\n 35:\t\tselect NEED_TASKS_RCU\n 36:\t\tselect TASKS_TRACE_RCU\n 37:\t\tselect BINARY_PRINTF\n 38:\t\tselect NET_SOCK_MSG if NET\n 39:\t\tselect NET_XGRESS if NET\n 40:\t\tselect PAGE_POOL if NET\n 41:\t\tdefault n\n 42:\t\thelp\n 43:\t\t Enable the bpf() system call that allows to manipulate BPF programs\n 44:\t\t and maps via file descriptors.\n 45:\t\n 46:\tconfig BPF_JIT\n 47:\t\tbool \"Enable BPF Just In Time compiler\"\n 48:\t\tdepends on BPF\n 49:\t\tdepends on HAVE_CBPF_JIT || HAVE_EBPF_JIT\n 50:\t\tselect EXECMEM\n 51:\t\thelp\n 52:\t\t BPF programs are normally handled by a BPF interpreter. This option\n 53:\t\t allows the kernel to generate native code when a program is loaded\n 54:\t\t into the kernel. This will significantly speed-up processing of BPF\n 55:\t\t programs.\n 56:\t\n 57:\t\t Note, an admin should enable this feature changing:\n 58:\t\t /proc/sys/net/core/bpf_jit_enable\n 59:\t\t /proc/sys/net/core/bpf_jit_harden (optional)\n 60:\t\t /proc/sys/net/core/bpf_jit_kallsyms (optional)\n 61:\t\n 62:\tconfig BPF_JIT_ALWAYS_ON\n 63:\t\tbool \"Permanently enable BPF JIT and remove BPF interpreter\"\n 64:\t\tdepends on BPF_SYSCALL \u0026\u0026 HAVE_EBPF_JIT \u0026\u0026 BPF_JIT\n 65:\t\thelp\n 66:\t\t Enables BPF JIT and removes BPF interpreter to avoid speculative\n 67:\t\t execution of BPF instructions by the interpreter.\n 68:\t\n 69:\t\t When CONFIG_BPF_JIT_ALWAYS_ON is enabled, /proc/sys/net/core/bpf_jit_enable\n"
} |
| 13/2 | 2026/09/18 21:43 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 76913 output: 32 thoughts: 67 Thoughts: Here's my summary, as if I were thinking these things: **Kernel Debugging Deep Dive: BPF, JIT, and Unwinders** Alright, let's take a look at these kernel components. I'm thinking about BPF, specifically around system calls (that's `BPF_SYSCALL`), the Just-In-Time compiler (`BPF_JIT`), and how it's configured, whether it's always on or not (`BPF_JIT_ALWAYS_ON` or just `BPF_JIT`). The question of when, how and why we enable JIT is clearly important for performance and security implications. But the bigger thought now, is around the unwinder, `UNWINDER_ORC`. This is crucial for debugging and understanding what's going on in the kernel when things go south. I need to refresh my memory on what that ORC unwinder configuration provides. I'll quickly check its definition to see what it can tell me in the x86 architecture, specifically in the debugging configuration (`arch/x86/Kconfig.debug`). Time to dive into the config! |
| 14/2 | 2026/09/18 21:43 | tool | grepper |
0mArgs:{
"Expression": "config UNWINDER_ORC",
"PathPrefix": "arch/x86"
}Results: {
"Output": "arch/x86/Kconfig.debug=224=choice\n--\narch/x86/Kconfig.debug-232-\narch/x86/Kconfig.debug:233:config UNWINDER_ORC\narch/x86/Kconfig.debug-234-\tbool \"ORC unwinder\"\n"
} |
| 15/2 | 2026/09/18 21:43 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 77349 output: 38 thoughts: 23 Thoughts: Here's a summary of my thought process, as requested: **Debugging the x86 Architecture Configuration** Okay, so I'm looking into a potential issue, and my intuition is leading me to the x86 architecture's configuration file. Specifically, I need to take a look at `arch/x86/Kconfig.debug`. It's a key file for debugging options. Let me pinpoint the area around line 233. Chances are, there's a setting or a conditional definition lurking there that might be relevant to the problem I'm trying to solve. I have a hunch it might be related to some debug feature, so this seems like a logical first step to investigate. Let's dive in and see what's there. |
| 16/2 | 2026/09/18 21:43 | tool | read-file |
0mArgs:{
"File": "arch/x86/Kconfig.debug",
"FirstLine": 225,
"LineCount": 25
}Results: {
"Contents": " 225:\t\tprompt \"Choose kernel unwinder\"\n 226:\t\tdefault UNWINDER_ORC if X86_64\n 227:\t\tdefault UNWINDER_FRAME_POINTER if X86_32\n 228:\t\thelp\n 229:\t\t This determines which method will be used for unwinding kernel stack\n 230:\t\t traces for panics, oopses, bugs, warnings, perf, /proc/\u003cpid\u003e/stack,\n 231:\t\t livepatch, lockdep, and more.\n 232:\t\n 233:\tconfig UNWINDER_ORC\n 234:\t\tbool \"ORC unwinder\"\n 235:\t\tdepends on X86_64\n 236:\t\tselect OBJTOOL\n 237:\t\thelp\n 238:\t\t This option enables the ORC (Oops Rewind Capability) unwinder for\n 239:\t\t unwinding kernel stack traces. It uses a custom data format which is\n 240:\t\t a simplified version of the DWARF Call Frame Information standard.\n 241:\t\n 242:\t\t This unwinder is more accurate across interrupt entry frames than the\n 243:\t\t frame pointer unwinder. It also enables a 5-10% performance\n 244:\t\t improvement across the entire kernel compared to frame pointers.\n 245:\t\n 246:\t\t Enabling this option will increase the kernel's runtime memory usage\n 247:\t\t by roughly 2-4MB, depending on your kernel config.\n 248:\t\n 249:\tconfig UNWINDER_FRAME_POINTER\n"
} |
| 17/2 | 2026/09/18 21:43 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 77993 output: 299 thoughts: 2003 Thoughts: **Focusing on BPF Exception Cleanup for Fuzzing** Alright, let's break down these kernel changes and figure out the best approach for fuzzing them. My primary concern here is identifying the critical functions related to BPF exception cleanup, specifically those that are exposed and directly relevant to user-space interaction. I need to make sure I don't get sidetracked by ubiquitous functions that aren't the focus of this work. I've got a comprehensive list of newly introduced or modified symbols across several kernel areas. Looking at `kernel/bpf/exception.c`, I see a cluster of functions prefixed with `bpf_cleanup_`, along with `bpf_prepare_cleanup_exceptions` and `bpf_check_cleanup_exceptions`. The `kernel/bpf/check_btf.c` file provides `check_cleanup_info`. `kernel/bpf/cfg.c` offers `visit_cleanup_pad_edge`, while `kernel/bpf/fixups.c` contains several functions like `cleanup_throw_sites_for_subprog` and `bpf_cleanup_attach_main_prog`. Then we have the important `bpf_run_cleanup_pad` and `bpf_throw` from `kernel/bpf/helpers.c`, alongside `bpf_unwind_resume`, as well as several functions in `kernel/bpf/verifier.c` and a key function, `arch_bpf_run_cleanup_pad`, in `arch/x86/net/bpf_cleanup_pad.S`. Finally, `bpf_jit_supports_cleanup_pads` and modifications to `do_jit` in `arch/x86/net/bpf_jit_comp.c`. Now, according to the focus symbol rules, I need to pinpoint the *specific feature logic* and *entrypoints* that aren't overly generic. I need to avoid including things like memory allocators, core synchronization primitives, and instead focus on the new features being added. My next step is to examine the list, checking which are non-static and visible in vmlinux or kallsyms. I confirm that `arch_bpf_run_cleanup_pad` has a symbol, and `bpf_unwind_resume` is a kfunc, while some of the `bpf_cleanup_*` are non-static and at the heart of the new cleanup mechanism. I can also add `bpf_prepare_cleanup_exceptions`, `bpf_check_cleanup_exceptions`, `bpf_cleanup_attach_main_prog`, `bpf_cleanup_fill_native_ranges`, and `bpf_cleanup_pad_for_ip`,. I've assembled a list of potential focus symbols, keeping in mind the need to identify the feature's core. Finally, let's clarify the configurations: I see `BPF_SYSCALL`, `BPF_JIT`, and `UNWINDER_ORC` as important configs. `BPF_SYSCALL` automatically includes `BPF`, and `UNWINDER_ORC` depends on `X86_64`. These configurations determine which features are enabled. In summary, this patch introduces BPF exception cleanup mechanisms and is perfect for fuzzing. It includes: 1. UAPI changes in `bpf_attr` to support `cleanup_info`, etc., via `BPF_PROG_LOAD`. 2. Verifier/CFG analysis in `exception.c`, `cfg.c`, etc. 3. x86 JIT support and landing pad execution via `arch_bpf_run_cleanup_pad()` and `bpf_unwind_resume()`. 4. Runtime unwinding via `bpf_throw()` and `bpf_run_cleanup_pad()`. This code directly handles user-space interactions through the `bpf()` syscall. Let's move to `set-results`. |
| 18/2 | 2026/09/18 21:43 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT",
"UNWINDER_ORC"
],
"FocusSymbols": [
"arch_bpf_run_cleanup_pad",
"bpf_prepare_cleanup_exceptions",
"bpf_check_cleanup_exceptions",
"bpf_cleanup_attach_main_prog",
"bpf_cleanup_fill_native_ranges",
"bpf_cleanup_pad_for_ip",
"bpf_unwind_resume",
"bpf_cleanup_pad_of_call"
],
"Reasoning": "The patch introduces support for BPF exception cleanup landing pads. This adds new fields (cleanup_info, cleanup_info_cnt, cleanup_info_rec_size) to the BPF_PROG_LOAD command of the bpf() syscall, along with extensive verifier validation (BTF/attribute checking, CFG reachability analysis, liveness analysis, verifier state tracking for unwinding) and x86 JIT runtime handling (arch_bpf_run_cleanup_pad, throw-site register spills, unwinding execution with bpf_throw and bpf_unwind_resume). This modifies core BPF verifier and runtime execution logic reachable from userspace, warranting fuzzing.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT",
"UNWINDER_ORC"
],
"FocusSymbols": [
"arch_bpf_run_cleanup_pad",
"bpf_prepare_cleanup_exceptions",
"bpf_check_cleanup_exceptions",
"bpf_cleanup_attach_main_prog",
"bpf_cleanup_fill_native_ranges",
"bpf_cleanup_pad_for_ip",
"bpf_unwind_resume",
"bpf_cleanup_pad_of_call"
],
"Reasoning": "The patch introduces support for BPF exception cleanup landing pads. This adds new fields (cleanup_info, cleanup_info_cnt, cleanup_info_rec_size) to the BPF_PROG_LOAD command of the bpf() syscall, along with extensive verifier validation (BTF/attribute checking, CFG reachability analysis, liveness analysis, verifier state tracking for unwinding) and x86 JIT runtime handling (arch_bpf_run_cleanup_pad, throw-site register spills, unwinding execution with bpf_throw and bpf_unwind_resume). This modifies core BPF verifier and runtime execution logic reachable from userspace, warranting fuzzing.",
"WorthFuzzing": true
} |
| 19/1 | 2026/09/18 21:43 | action | If |
0mArgs:{
"WorthFuzzing": true
}Results: null |
| 20/2 | 2026/09/18 21:43 | agent | kmsan-evaluator |
0mModel:core Args: null Results: {
"KMSANReasoning": "The patch series adds support for BPF exception cleanup landing pads in the BPF verifier and JIT compilers (x86-64 and arm64).\n\nEvaluation against KMSAN criteria:\n1. Kernel data copied to user space: In `check_cleanup_info()`, only `min_size` (a fully initialized 32-bit integer scalar) is conditionally written back to userspace `cleanup_info_rec_size` on `-E2BIG`. No structs with padding or uninitialized memory are copied to user space.\n2. Structure allocations: All newly allocated kernel data structures (`struct bpf_exception_info`, `bpf_cleanup_range`, `bpf_cleanup_info`, scratch DFS buffers, and bitmaps) are allocated using zero-initializing functions (`kzalloc_obj`, `kvcalloc`, `kvzalloc`, `bitmap_zalloc`) or fully populated before use.\n3. JIT and Assembly execution: The landing pad execution routines in `arch/x86/net/bpf_cleanup_pad.S` and JITed BPF bytecode are raw machine code, which is not instrumented by KMSAN.\n4. Verification \u0026 control flow: Any potential bugs introduced by stack frame offset calculations, JIT prologue/epilogue spills, or invalid branch targets will manifest as out-of-bounds accesses or crashes, which are already detected by KASAN.\n\nTherefore, the patch introduces no uninitialized memory usage or info-leak risks detectable only by KMSAN, and a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit aa17089d32a7e14651815f32eec679e963dce2a9
Author: syz-cluster <triage@syzkaller.com>
Date: Fri Sep 18 21:42:55 2026 +0000
syz-cluster: applied patch under review
diff --git a/arch/arm64/net/Makefile b/arch/arm64/net/Makefile
index 3ae382bfca879..ebec2a44a52b6 100644
--- a/arch/arm64/net/Makefile
+++ b/arch/arm64/net/Makefile
@@ -2,4 +2,4 @@
#
# ARM64 networking code
#
-obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o
+obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o bpf_cleanup_pad.o
diff --git a/arch/arm64/net/bpf_cleanup_pad.S b/arch/arm64/net/bpf_cleanup_pad.S
new file mode 100644
index 0000000000000..ef441241949ec
--- /dev/null
+++ b/arch/arm64/net/bpf_cleanup_pad.S
@@ -0,0 +1,95 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <linux/bpf_cleanup_abi.h>
+#include <linux/linkage.h>
+
+/*
+ * A frame's prologue pushes the tail call counter pair, and then -- once
+ * bpf_cleanup_force_spill() says so -- x19/x20, x21/x22, x23/x24, x25/x26 and
+ * x27/x28. Each A64_PUSH pre-decrements, so the lowest address of the spill
+ * area holds x27 and the highest x20:
+ *
+ * spill_base + 0 x27 (private stack pointer)
+ * spill_base + 8 x28 (arena base)
+ * spill_base + 16 x25 (BPF r10, the frame pointer)
+ * spill_base + 24 x26 (tail call counter pointer)
+ * spill_base + 32 x23 -- the pad's own, see below
+ * spill_base + 40 x24 -- likewise
+ * spill_base + 48 x21 (BPF r8)
+ * spill_base + 56 x22 (BPF r9)
+ * spill_base + 64 x19 (BPF r6)
+ * spill_base + 72 x20 (BPF r7)
+ *
+ * Neither x23 nor x24 is restored from that spill: the pad has its own use for
+ * both. bpf2a64[] maps nothing to either -- they are pushed only to keep the
+ * frame shape the exception callback expects -- so nothing else in generated
+ * code touches them, and being callee-saved they survive every call the pad
+ * makes.
+ *
+ * x23 is the pad's return address. Unlike x86-64 a pad cannot simply return:
+ * every call it makes clobbers x30, so nothing is left to return through by
+ * the time it reaches its resume. The JIT emits "br x23" for the pad's
+ * bpf_unwind_resume() and this routine puts .Lcleanup_pad_done there.
+ *
+ * x24 is where the pad's frame is anchored. Generated code addresses the BPF
+ * frame through the stack pointer, which here is this routine's rather than
+ * the unwinding frame's, so the JIT has the pad recompute the equivalent from
+ * BPF r10 on entry and address its frame off x24 for as long as it runs.
+ *
+ * Both of those branches are indirect, so both targets carry a BTI landing
+ * marker: the JIT emits one at each pad, and .Lcleanup_pad_done below has one
+ * of its own.
+ */
+
+ .text
+
+/*
+ * void arch_bpf_run_cleanup_pad(u64 pad, u64 frame_fp, u64 spill_base)
+ *
+ * x0 = native address of the landing pad
+ * x1 = frame pointer of the frame the pad belongs to (unused here: BPF r10 is
+ * x25, which the spill area already holds)
+ * x2 = spill area holding that frame's BPF callee-saved registers
+ *
+ * Give the pad the register state of its own frame and call it. It runs on
+ * this stack, far below the frame it is cleaning up after, so nothing it
+ * calls can reach into that frame.
+ */
+SYM_FUNC_START(arch_bpf_run_cleanup_pad)
+ /* Save the kernel's callee-saved registers; the pad owns them next. */
+ stp x29, x30, [sp, #-96]!
+ mov x29, sp
+ stp x19, x20, [sp, #16]
+ stp x21, x22, [sp, #32]
+ stp x23, x24, [sp, #48]
+ stp x25, x26, [sp, #64]
+ stp x27, x28, [sp, #80]
+
+ /* x9 is BPF_REG_AX, so the pad's address does not stay in BPF r1. */
+ mov x9, x0
+
+ ldp x27, x28, [x2, #0]
+ ldp x25, x26, [x2, #16]
+ ldp x21, x22, [x2, #48]
+ ldp x19, x20, [x2, #64]
+
+ /* BPF r0 (x8) on the way into a pad, not whatever the kernel left. */
+ mov x8, #BPF_PAD_ENTRY_R0
+
+ /* Where the pad's resume branches back to. */
+ adr x23, .Lcleanup_pad_done
+
+ br x9
+
+.Lcleanup_pad_done:
+ /* Reached by the pad's "br x23", so it is an indirect branch target. */
+ bti j
+ ldp x19, x20, [sp, #16]
+ ldp x21, x22, [sp, #32]
+ ldp x23, x24, [sp, #48]
+ ldp x25, x26, [sp, #64]
+ ldp x27, x28, [sp, #80]
+ ldp x29, x30, [sp], #96
+ ret
+SYM_FUNC_END(arch_bpf_run_cleanup_pad)
diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index 6c04fee468766..560eba305bca2 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -11,6 +11,7 @@
#include <linux/bitfield.h>
#include <linux/bpf.h>
#include <linux/cfi.h>
+#include <linux/bpf_verifier.h>
#include <linux/filter.h>
#include <linux/memory.h>
#include <linux/printk.h>
@@ -75,7 +76,21 @@ static const int bpf2a64[] = {
[ARENA_VM_START] = A64_R(28),
};
+/* Throw-site spill: the five pairs push_callee_regs() forces on, same size and
+ * slot order, so arch_bpf_run_cleanup_pad() reads both alike.
+ */
+#define A64_CLEANUP_SPILL_SZ (5 * 16)
+
+/*
+ * Where a landing pad's frame is anchored, since the stack pointer generated
+ * code normally addresses it through is the walker's inside a pad. bpf2a64[]
+ * maps nothing to x24, so nothing else in generated code touches it.
+ */
+#define A64_CLEANUP_FP A64_R(24)
+
struct jit_ctx {
+ /* Bytes reserved for the throw-site spill; see bpf_cleanup_force_spill(). */
+ u32 throw_spill;
const struct bpf_prog *prog;
int idx;
int epilogue_offset;
@@ -432,7 +447,7 @@ static void push_callee_regs(struct jit_ctx *ctx)
* Callee-saved registers as the exception callback needs to recover
* all ARM64 Callee-saved registers in its epilogue.
*/
- if (ctx->prog->aux->exception_boundary) {
+ if (ctx->prog->aux->exception_boundary || bpf_cleanup_force_spill(ctx->prog)) {
emit(A64_PUSH(A64_R(19), A64_R(20), A64_SP), ctx);
emit(A64_PUSH(A64_R(21), A64_R(22), A64_SP), ctx);
emit(A64_PUSH(A64_R(23), A64_R(24), A64_SP), ctx);
@@ -466,7 +481,8 @@ static void pop_callee_regs(struct jit_ctx *ctx)
* program's stack frame, so recover these extra registers in the above
* two cases.
*/
- if (aux->exception_boundary || aux->exception_cb) {
+ if (aux->exception_boundary || aux->exception_cb ||
+ bpf_cleanup_force_spill(ctx->prog)) {
emit(A64_POP(A64_R(27), A64_R(28), A64_SP), ctx);
emit(A64_POP(A64_R(25), A64_R(26), A64_SP), ctx);
emit(A64_POP(A64_R(23), A64_R(24), A64_SP), ctx);
@@ -602,6 +618,20 @@ static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
emit(A64_SUB_I(1, A64_SP, A64_FP, 96), ctx);
}
+ /*
+ * Lowest address of each spill area, as an offset from A64_FP; see
+ * bpf_cleanup_pad.S for the layout. The 16 is the tail call counter
+ * pair pushed just below the frame record, and the throw-site area
+ * sits below the callee-saved one rather than in the program stack.
+ */
+ if (bpf_cleanup_force_spill(prog)) {
+ prog->aux->exc->spill_off = -(16 + A64_CLEANUP_SPILL_SZ);
+ ctx->throw_spill = A64_CLEANUP_SPILL_SZ;
+ emit(A64_SUB_I(1, A64_SP, A64_SP, ctx->throw_spill), ctx);
+ prog->aux->exc->throw_spill_off =
+ -(16 + A64_CLEANUP_SPILL_SZ) - ctx->throw_spill;
+ }
+
/* Stack must be multiples of 16B */
ctx->stack_size = round_up(prog->aux->stack_depth, 16);
@@ -691,6 +721,10 @@ static int emit_bpf_tail_call(struct jit_ctx *ctx)
if (ctx->stack_size && !ctx->priv_sp_used)
emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
+ /* Release it for the same reason build_epilogue() does. */
+ if (ctx->throw_spill)
+ emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->throw_spill), ctx);
+
pop_callee_regs(ctx);
/* goto *(prog->bpf_func + prologue_offset); */
@@ -1055,6 +1089,9 @@ static void build_epilogue(struct jit_ctx *ctx, bool was_classic)
if (ctx->stack_size && !ctx->priv_sp_used)
emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
+ if (ctx->throw_spill)
+ emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->throw_spill), ctx);
+
pop_callee_regs(ctx);
emit(A64_POP(A64_ZR, ptr, A64_SP), ctx);
@@ -1230,6 +1267,13 @@ static const u8 stack_arg_reg[] = { A64_R(5), A64_R(6), A64_R(7) };
#define NR_STACK_ARG_REGS ARRAY_SIZE(stack_arg_reg)
+/*
+ * This reads the incoming argument area off A64_FP, which in an exception
+ * cleanup landing pad would be arch_bpf_run_cleanup_pad()'s frame record
+ * rather than the unwinding frame's -- but a pad cannot contain one of these:
+ * check_stack_arg_read() requires every r11 load to come before the frame's
+ * first call, and a pad only ever runs after one.
+ */
static void emit_stack_arg_load(u8 dst, s16 bpf_off, struct jit_ctx *ctx)
{
int idx = bpf_off / sizeof(u64) - 1;
@@ -1367,6 +1411,7 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
const s16 off = insn->off;
const s32 imm = insn->imm;
const int i = insn - ctx->prog->insnsi;
+ const bool in_pad = bpf_cleanup_insn_in_pad(ctx->prog, i);
const bool is64 = BPF_CLASS(code) == BPF_ALU64 ||
BPF_CLASS(code) == BPF_JMP;
u8 jmp_cond;
@@ -1378,9 +1423,14 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
int ret;
bool sign_extend;
- if (bpf_insn_is_indirect_target(env, ctx->prog, i))
+ if (bpf_insn_is_indirect_target(env, ctx->prog, i) ||
+ bpf_cleanup_insn_is_pad(ctx->prog, i))
emit_bti(A64_BTI_J, ctx);
+ if (bpf_cleanup_insn_is_pad(ctx->prog, i))
+ emit(A64_SUB_I(1, A64_CLEANUP_FP, fp,
+ ctx->stack_size + ctx->stack_arg_size), ctx);
+
switch (code) {
/* dst = src */
case BPF_ALU | BPF_MOV | BPF_X:
@@ -1743,6 +1793,26 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
u64 func_addr;
u32 cpu_offset;
+ if (bpf_cleanup_insn_is_throw(ctx->prog, insn - ctx->prog->insnsi)) {
+ /* Spill where the bpf_throw() walker looks. */
+ const s32 off = ctx->prog->aux->exc->throw_spill_off;
+
+ emit(A64_SUB_I(1, tmp, A64_FP, -off), ctx);
+ emit(A64_STR64I(bpf2a64[PRIVATE_SP], tmp, 0), ctx);
+ emit(A64_STR64I(bpf2a64[ARENA_VM_START], tmp, 8), ctx);
+ emit(A64_STR64I(bpf2a64[BPF_REG_FP], tmp, 16), ctx);
+ emit(A64_STR64I(bpf2a64[TCCNT_PTR], tmp, 24), ctx);
+ emit(A64_STR64I(bpf2a64[BPF_REG_8], tmp, 48), ctx);
+ emit(A64_STR64I(bpf2a64[BPF_REG_9], tmp, 56), ctx);
+ emit(A64_STR64I(bpf2a64[BPF_REG_6], tmp, 64), ctx);
+ emit(A64_STR64I(bpf2a64[BPF_REG_7], tmp, 72), ctx);
+ }
+
+ if (bpf_is_unwind_resume_kfunc(insn)) {
+ emit(A64_BR(A64_R(23)), ctx);
+ break;
+ }
+
/* Implement helper call to bpf_get_smp_processor_id() inline */
if (insn->src_reg == 0 && insn->imm == BPF_FUNC_get_smp_processor_id) {
cpu_offset = offsetof(struct thread_info, cpu);
@@ -1854,7 +1924,8 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
src = tmp2;
}
if (src == fp) {
- src_adj = ctx->priv_sp_used ? priv_sp : A64_SP;
+ src_adj = ctx->priv_sp_used ? priv_sp :
+ in_pad ? A64_CLEANUP_FP : A64_SP;
off_adj = off + ctx->stack_size;
if (!ctx->priv_sp_used)
off_adj += ctx->stack_arg_size;
@@ -1952,7 +2023,8 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
dst = tmp3;
}
if (dst == fp) {
- dst_adj = ctx->priv_sp_used ? priv_sp : A64_SP;
+ dst_adj = ctx->priv_sp_used ? priv_sp :
+ in_pad ? A64_CLEANUP_FP : A64_SP;
off_adj = off + ctx->stack_size;
if (!ctx->priv_sp_used)
off_adj += ctx->stack_arg_size;
@@ -2021,7 +2093,8 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
dst = tmp2;
}
if (dst == fp) {
- dst_adj = ctx->priv_sp_used ? priv_sp : A64_SP;
+ dst_adj = ctx->priv_sp_used ? priv_sp :
+ in_pad ? A64_CLEANUP_FP : A64_SP;
off_adj = off + ctx->stack_size;
if (!ctx->priv_sp_used)
off_adj += ctx->stack_arg_size;
@@ -2410,6 +2483,13 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
* reasons, expects to point to the next instruction)
*/
bpf_prog_update_insn_ptrs(prog, ctx.offset, ctx.ro_image);
+
+ /*
+ * Same byte offsets, consumed by the bpf_throw() frame walker:
+ * turn the cleanup records into native address ranges now that
+ * the image is final.
+ */
+ bpf_cleanup_fill_native_ranges(prog, ctx.offset, ctx.ro_image);
out_off:
if (!ro_header && priv_stack_ptr) {
free_percpu(priv_stack_ptr);
@@ -3385,6 +3465,11 @@ bool bpf_jit_supports_exceptions(void)
return true;
}
+bool bpf_jit_supports_cleanup_pads(void)
+{
+ return true;
+}
+
bool bpf_jit_supports_arena(void)
{
return true;
diff --git a/arch/x86/net/Makefile b/arch/x86/net/Makefile
index dddbefc0f4398..9d574d972df37 100644
--- a/arch/x86/net/Makefile
+++ b/arch/x86/net/Makefile
@@ -6,5 +6,5 @@
ifeq ($(CONFIG_X86_32),y)
obj-$(CONFIG_BPF_JIT) += bpf_jit_comp32.o
else
- obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o
+ obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o bpf_cleanup_pad.o
endif
diff --git a/arch/x86/net/bpf_cleanup_pad.S b/arch/x86/net/bpf_cleanup_pad.S
new file mode 100644
index 0000000000000..da4b448ecf095
--- /dev/null
+++ b/arch/x86/net/bpf_cleanup_pad.S
@@ -0,0 +1,74 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <linux/bpf_cleanup_abi.h>
+#include <linux/linkage.h>
+#include <asm/nospec-branch.h>
+
+/*
+ * The x86-64 BPF JIT prologue spills, once bpf_cleanup_force_spill() makes it
+ * unconditional, r12, rbx, r13, r14 and r15 in that order -- so within the
+ * spill area the lowest address holds r15 and the highest r12. The throw-site
+ * spill the JIT emits uses the same layout, so the routine below reads both
+ * the same way:
+ *
+ * spill_base + 0 BPF r9 (r15)
+ * spill_base + 8 BPF r8 (r14)
+ * spill_base + 16 BPF r7 (r13)
+ * spill_base + 24 BPF r6 (rbx)
+ * spill_base + 32 r12 (arena base, not a BPF register)
+ */
+
+ .code64
+ .section .text, "ax"
+
+/*
+ * void arch_bpf_run_cleanup_pad(u64 pad, u64 frame_fp, u64 spill_base)
+ *
+ * rdi = native address of the landing pad
+ * rsi = frame pointer of the frame the pad belongs to
+ * rdx = spill area holding that frame's BPF callee-saved registers
+ *
+ * Give the pad the register state of its own frame and call it. The pad ends
+ * in the bare return the JIT emits for its bpf_unwind_resume(), so it comes
+ * back here rather than returning to its frame's caller. It runs on this
+ * stack, far below the frame it is cleaning up after, so nothing it calls can
+ * reach into that frame.
+ */
+SYM_FUNC_START(arch_bpf_run_cleanup_pad)
+ ANNOTATE_NOENDBR
+
+ pushq %rbp
+ pushq %rbx
+ pushq %r12
+ pushq %r13
+ pushq %r14
+ pushq %r15
+ /* Keep the pad's entry rsp congruent to a normal call's. */
+ subq $8, %rsp
+
+ movq 0(%rdx), %r15
+ movq 8(%rdx), %r14
+ movq 16(%rdx), %r13
+ movq 24(%rdx), %rbx
+ movq 32(%rdx), %r12
+ /* rbp is BPF r10, so this is the whole of the pad's frame setup. */
+ movq %rsi, %rbp
+
+ /* CALL_NOSPEC needs the target in a register; rcx is BPF r4, dead. */
+ movq %rdi, %rcx
+
+ /* BPF r0 on the way into a pad, not whatever the kernel left in rax. */
+ movl $BPF_PAD_ENTRY_R0, %eax
+
+ CALL_NOSPEC rcx
+
+ addq $8, %rsp
+ popq %r15
+ popq %r14
+ popq %r13
+ popq %r12
+ popq %rbx
+ popq %rbp
+ RET
+SYM_FUNC_END(arch_bpf_run_cleanup_pad)
diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index d4a980140b48d..9d0dd54773e83 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -357,6 +357,11 @@ struct jit_context {
/* Number of bytes that will be skipped on tailcall */
#define X86_TAIL_CALL_OFFSET (12 + ENDBR_INSN_SIZE)
+/* Throw-site spill: r15, r14, r13, rbx, r12 low to high, the layout the
+ * prologue's pushes leave, so arch_bpf_run_cleanup_pad() reads both alike.
+ */
+#define X86_CLEANUP_SPILL_SZ (5 * 8)
+
static void push_r9(u8 **pprog)
{
u8 *prog = *pprog;
@@ -832,7 +837,7 @@ static void emit_bpf_tail_call_indirect(struct bpf_prog *bpf_prog,
/* Inc tail_call_cnt if the slot is populated. */
EMIT4(0x48, 0x83, 0x00, 0x01); /* add qword ptr [rax], 1 */
- if (bpf_prog->aux->exception_boundary) {
+ if (bpf_prog->aux->exception_boundary || bpf_cleanup_force_spill(bpf_prog)) {
pop_callee_regs(&prog, all_callee_regs_used);
pop_r12(&prog);
} else {
@@ -899,7 +904,7 @@ static void emit_bpf_tail_call_direct(struct bpf_prog *bpf_prog,
/* Inc tail_call_cnt if the slot is populated. */
EMIT4(0x48, 0x83, 0x00, 0x01); /* add qword ptr [rax], 1 */
- if (bpf_prog->aux->exception_boundary) {
+ if (bpf_prog->aux->exception_boundary || bpf_cleanup_force_spill(bpf_prog)) {
pop_callee_regs(&prog, all_callee_regs_used);
pop_r12(&prog);
} else {
@@ -1977,6 +1982,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
u8 *ip, *prog = temp;
u32 stack_depth;
int callee_saved_size;
+ u32 throw_spill, prologue_depth;
s32 outgoing_arg_base;
int err;
@@ -2015,7 +2021,10 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
detect_reg_usage(insn, insn_cnt, callee_regs_used);
- emit_prologue(&prog, image, stack_depth,
+ throw_spill = bpf_cleanup_force_spill(bpf_prog) ? X86_CLEANUP_SPILL_SZ : 0;
+ prologue_depth = stack_depth + throw_spill;
+
+ emit_prologue(&prog, image, prologue_depth,
bpf_prog_was_classic(bpf_prog), tail_call_reachable,
bpf_is_subprog(bpf_prog), bpf_prog->aux->exception_cb);
@@ -2024,7 +2033,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
/* Exception callback will clobber callee regs for its own use, and
* restore the original callee regs from main prog's stack frame.
*/
- if (bpf_prog->aux->exception_boundary) {
+ if (bpf_prog->aux->exception_boundary || bpf_cleanup_force_spill(bpf_prog)) {
/* We also need to save r12, which is not mapped to any BPF
* register, as we throw after entry into the kernel, which may
* overwrite r12.
@@ -2039,9 +2048,10 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
/* Compute callee-saved register area size. */
callee_saved_size = 0;
- if (bpf_prog->aux->exception_boundary || arena_vm_start)
+ if (bpf_prog->aux->exception_boundary || bpf_cleanup_force_spill(bpf_prog) ||
+ arena_vm_start)
callee_saved_size += 8; /* r12 */
- if (bpf_prog->aux->exception_boundary) {
+ if (bpf_prog->aux->exception_boundary || bpf_cleanup_force_spill(bpf_prog)) {
callee_saved_size += 4 * 8; /* rbx, r13, r14, r15 */
} else {
int j;
@@ -2063,7 +2073,19 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
* Note that tail_call_reachable is guaranteed to be false when
* stack args exist, so tcc pushes need not be accounted for.
*/
- outgoing_arg_base = -(round_up(stack_depth, 8) + callee_saved_size);
+ outgoing_arg_base = -(round_up(stack_depth, 8) + throw_spill + callee_saved_size);
+
+ /*
+ * Lowest address of each spill area, as an offset from rbp; see
+ * bpf_cleanup_pad.S for the layout. The 16 is the tail call counter
+ * pair emit_prologue_tail_call() pushes above the callee-saved one.
+ */
+ if (bpf_cleanup_force_spill(bpf_prog)) {
+ bpf_prog->aux->exc->spill_off = -(round_up(stack_depth, 8) + throw_spill +
+ (tail_call_reachable ? 16 : 0) +
+ callee_saved_size);
+ bpf_prog->aux->exc->throw_spill_off = -(round_up(stack_depth, 8) + throw_spill);
+ }
/*
* Allocate outgoing stack arg area for args 7+ only.
@@ -2110,7 +2132,8 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
dst_reg = X86_REG_R9;
}
- if (bpf_insn_is_indirect_target(env, bpf_prog, i - 1))
+ if (bpf_insn_is_indirect_target(env, bpf_prog, i - 1) ||
+ bpf_cleanup_insn_is_pad(bpf_prog, i - 1))
EMIT_ENDBR();
ip = image + addrs[i - 1] + (prog - temp);
@@ -2903,9 +2926,27 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
case BPF_JMP | BPF_CALL: {
const struct btf_func_model *fm = NULL;
+ if (bpf_cleanup_insn_is_throw(bpf_prog, i - 1)) {
+ /* Spill r6-r9 and r12 where the bpf_throw() walker looks. */
+ s32 off = bpf_prog->aux->exc->throw_spill_off;
+ u8 *spill = prog;
+
+ emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_9, off + 0);
+ emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_8, off + 8);
+ emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_7, off + 16);
+ emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_6, off + 24);
+ emit_stx(&prog, BPF_DW, BPF_REG_FP, X86_REG_R12, off + 32);
+ ip += prog - spill;
+ }
+
+ if (bpf_is_unwind_resume_kfunc(insn)) {
+ emit_return(&prog, image + addrs[i - 1] + (prog - temp));
+ break;
+ }
+
func = (u8 *) __bpf_call_base + imm32;
if (src_reg == BPF_PSEUDO_CALL && tail_call_reachable) {
- LOAD_TAIL_CALL_CNT_PTR(stack_depth);
+ LOAD_TAIL_CALL_CNT_PTR(prologue_depth);
ip += 7;
}
if (!imm32)
@@ -2948,13 +2989,13 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
&prog,
ip,
callee_regs_used,
- stack_depth,
+ prologue_depth,
ctx);
else
emit_bpf_tail_call_indirect(bpf_prog,
&prog,
callee_regs_used,
- stack_depth,
+ prologue_depth,
ip,
ctx);
break;
@@ -3215,7 +3256,8 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
}
/* Deallocate outgoing args 7+ area. */
emit_add_rsp(&prog, outgoing_rsp);
- if (bpf_prog->aux->exception_boundary) {
+ if (bpf_prog->aux->exception_boundary ||
+ bpf_cleanup_force_spill(bpf_prog)) {
pop_callee_regs(&prog, all_callee_regs_used);
pop_r12(&prog);
} else {
@@ -4385,6 +4427,13 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
*/
bpf_prog_update_insn_ptrs(prog, addrs, image);
+ /*
+ * Same mapping, consumed by the bpf_throw() frame walker:
+ * turn the cleanup records into native address ranges now
+ * that the image is final.
+ */
+ bpf_cleanup_fill_native_ranges(prog, addrs, image);
+
/*
* ctx.prog_offset is used when CFI preambles put code *before*
* the function. See emit_cfi(). For FineIBT specifically this code
@@ -4501,6 +4550,11 @@ bool bpf_jit_supports_exceptions(void)
return IS_ENABLED(CONFIG_UNWINDER_ORC);
}
+bool bpf_jit_supports_cleanup_pads(void)
+{
+ return IS_ENABLED(CONFIG_UNWINDER_ORC);
+}
+
bool bpf_jit_supports_private_stack(void)
{
return true;
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 2a5fa346aadaa..83f2b0d7e5961 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1770,6 +1770,80 @@ enum bpf_sig_keyring {
BPF_SIG_KEYRING_BPF,
};
+/*
+ * One cleanup region of a JITed (sub)program: @pad is the landing pad to run
+ * for a return address in (begin, end], the native code of its call sites.
+ */
+struct bpf_cleanup_range {
+ u64 begin;
+ u64 end;
+ u64 pad;
+};
+
+struct bpf_exception_info {
+ struct bpf_cleanup_info *info;
+ struct bpf_cleanup_range *ranges;
+ /* Landing pad instruction indices, sorted and deduplicated. */
+ u32 *pad_at;
+ /* bpf_throw() call instruction indices, sorted. */
+ u32 *throw_at;
+ /* One bit per instruction that only runs while unwinding. */
+ unsigned long *pad_body;
+ u32 nr_info;
+ u32 nr_ranges;
+ u32 nr_pad_at;
+ u32 nr_throw_at;
+ u32 nr_pad_body;
+ /* Offset from a frame's FP to the caller's spilled r6-r9. */
+ s32 spill_off;
+ /* Likewise, to the registers a frame spills before calling bpf_throw(). */
+ s32 throw_spill_off;
+};
+
+#ifdef CONFIG_BPF_SYSCALL
+bool bpf_cleanup_force_spill(const struct bpf_prog *prog);
+bool bpf_cleanup_insn_is_pad(const struct bpf_prog *prog, u32 idx);
+bool bpf_cleanup_insn_in_pad(const struct bpf_prog *prog, u32 idx);
+bool bpf_cleanup_insn_is_throw(const struct bpf_prog *prog, u32 idx);
+int bpf_cleanup_attach_main_prog(struct bpf_verifier_env *env, struct bpf_prog *prog);
+void bpf_cleanup_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image);
+void bpf_cleanup_free_info(struct bpf_prog_aux *aux);
+#else
+static inline bool bpf_cleanup_force_spill(const struct bpf_prog *prog)
+{
+ return false;
+}
+
+static inline bool bpf_cleanup_insn_is_pad(const struct bpf_prog *prog, u32 idx)
+{
+ return false;
+}
+
+static inline bool bpf_cleanup_insn_in_pad(const struct bpf_prog *prog, u32 idx)
+{
+ return false;
+}
+
+static inline bool bpf_cleanup_insn_is_throw(const struct bpf_prog *prog, u32 idx)
+{
+ return false;
+}
+
+static inline int bpf_cleanup_attach_main_prog(struct bpf_verifier_env *env,
+ struct bpf_prog *prog)
+{
+ return 0;
+}
+
+static inline void bpf_cleanup_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image)
+{
+}
+
+static inline void bpf_cleanup_free_info(struct bpf_prog_aux *aux)
+{
+}
+#endif
+
struct bpf_prog_aux {
atomic64_t refcnt;
u32 used_map_cnt;
@@ -1850,6 +1924,7 @@ struct bpf_prog_aux {
char name[BPF_OBJ_NAME_LEN];
u64 (*bpf_exception_cb)(u64 cookie, u64 sp, u64 bp, u64, u64);
u16 stack_arg_sp_adjust;
+ struct bpf_exception_info *exc;
#ifdef CONFIG_SECURITY
void *security;
#endif
diff --git a/include/linux/bpf_cleanup_abi.h b/include/linux/bpf_cleanup_abi.h
new file mode 100644
index 0000000000000..b6c1d589abda7
--- /dev/null
+++ b/include/linux/bpf_cleanup_abi.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#ifndef _LINUX_BPF_CLEANUP_ABI_H
+#define _LINUX_BPF_CLEANUP_ABI_H
+
+/*
+ * Value arch_bpf_run_cleanup_pad() leaves in r0 on the way into a landing pad.
+ * It has to be a constant the verifier knows: LLVM names r0 as both the
+ * exception pointer and the exception selector register, so every pad reads it
+ * before anything else and is free to store what it read. Kept on its own
+ * because the verifier and the arch dispatchers, which are assembly, have to
+ * agree on it.
+ */
+#define BPF_PAD_ENTRY_R0 1
+
+#endif /* _LINUX_BPF_CLEANUP_ABI_H */
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index cf85141ea1674..09fb89fda40af 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -509,6 +509,7 @@ struct bpf_verifier_state {
bool speculative;
bool in_sleepable;
+ bool unwinding;
/* first and last insn idx of this verifier state */
u32 first_insn_idx;
@@ -681,6 +682,13 @@ struct bpf_insn_aux_data {
bool needs_zext; /* alu op needs to clear upper bits */
bool non_sleepable; /* helper/kfunc may be called from non-sleepable context */
bool is_iter_next; /* bpf_iter_<type>_next() kfunc call */
+ bool cleanup_throw_site; /* call to bpf_throw() */
+ bool in_cleanup_pad; /* only runs with an exception in flight */
+ /*
+ * 1 + the instruction index of the exception cleanup landing pad this
+ * call site unwinds to, or 0 for none.
+ */
+ u32 cleanup_pad;
bool call_with_percpu_alloc_ptr; /* {this,per}_cpu_ptr() with prog percpu alloc */
u8 alu_state; /* used in combination with alu_limit */
/* true if STX or LDX instruction is a part of a spill/fill
@@ -987,6 +995,8 @@ struct bpf_verifier_env {
struct arg_track **callsite_at_stack;
u32 pass_cnt; /* number of times do_check() was called */
u32 subprog_cnt;
+ struct bpf_cleanup_info *cleanup_info;
+ u32 cleanup_info_cnt;
/* number of instructions analyzed by the verifier */
u32 prev_insn_processed, insn_processed;
/* number of jmps, calls, exits analyzed so far */
@@ -1516,6 +1526,7 @@ u32 btf_func_arg_align(const struct btf *btf, const struct btf_type *t);
int bpf_find_subprog(struct bpf_verifier_env *env, int off);
bool bpf_is_throw_kfunc(struct bpf_insn *insn);
+bool bpf_is_unwind_resume_kfunc(const struct bpf_insn *insn);
int bpf_compute_const_regs(struct bpf_verifier_env *env);
int bpf_prune_dead_branches(struct bpf_verifier_env *env);
int bpf_check_cfg(struct bpf_verifier_env *env);
diff --git a/include/linux/filter.h b/include/linux/filter.h
index b17222db2efc3..287cd9b59aa9e 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -1242,6 +1242,8 @@ bool bpf_jit_supports_stack_args(void);
bool bpf_jit_supports_arena_args(void);
bool bpf_jit_supports_far_kfunc_call(void);
bool bpf_jit_supports_exceptions(void);
+bool bpf_jit_supports_cleanup_pads(void);
+void arch_bpf_run_cleanup_pad(u64 pad, u64 frame_fp, u64 spill_base);
bool bpf_jit_supports_ptr_xchg(void);
bool bpf_jit_supports_arena(void);
bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena);
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 732b35cc08d1c..f7dc121be0947 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -1669,6 +1669,9 @@ union bpf_attr {
* verification.
*/
__s32 keyring_id;
+ __aligned_u64 cleanup_info; /* exception cleanup table */
+ __u32 cleanup_info_rec_size; /* userspace bpf_cleanup_info size */
+ __u32 cleanup_info_cnt; /* number of bpf_cleanup_info records */
};
struct { /* anonymous struct used by BPF_OBJ_* commands */
@@ -7588,6 +7591,12 @@ struct bpf_line_info {
__u32 line_col;
};
+struct bpf_cleanup_info {
+ __u32 begin_off;
+ __u32 end_off;
+ __u32 landing_pad_off;
+};
+
struct bpf_spin_lock {
__u32 val;
};
diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile
index 9a92c348bbda6..af9bc60428ad1 100644
--- a/kernel/bpf/Makefile
+++ b/kernel/bpf/Makefile
@@ -11,7 +11,7 @@ obj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_
obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o
obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o bpf_insn_array.o
obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o
-obj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o backtrack.o check_btf.o
+obj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o backtrack.o check_btf.o exception.o
obj-${CONFIG_BPF_LSM} += bpf_inode_storage.o
obj-$(CONFIG_BPF_SYSCALL) += disasm.o mprog.o
obj-$(CONFIG_BPF_JIT) += trampoline.o
diff --git a/kernel/bpf/cfg.c b/kernel/bpf/cfg.c
index 842c7d1eabccc..5a2b48b6a9e1d 100644
--- a/kernel/bpf/cfg.c
+++ b/kernel/bpf/cfg.c
@@ -6,6 +6,7 @@
#include <linux/sort.h>
#include "diagnostics.h"
+#include "exception.h"
#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
@@ -158,17 +159,57 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
return DONE_EXPLORING;
}
+static int visit_cleanup_pad_edge(int t, struct bpf_verifier_env *env)
+{
+ int *insn_stack = env->cfg.insn_stack;
+ int *insn_state = env->cfg.insn_state;
+ int w;
+
+ if (!env->cleanup_info_cnt)
+ return DONE_EXPLORING;
+ w = bpf_cleanup_pad_of_call(env, t);
+ if (w < 0)
+ return DONE_EXPLORING;
+
+ mark_prune_point(env, t);
+ mark_jmp_point(env, w);
+ mark_jump_target(env, w);
+
+ if (insn_state[w])
+ return DONE_EXPLORING;
+ if (env->cfg.cur_stack >= env->prog->len)
+ return -E2BIG;
+ insn_stack[env->cfg.cur_stack++] = w;
+ insn_state[w] |= DISCOVERED;
+ return KEEP_EXPLORING;
+}
+
+static int merge_visit_ret(int a, int b)
+{
+ if (a < 0)
+ return a;
+ if (b < 0)
+ return b;
+ if (a == KEEP_EXPLORING || b == KEEP_EXPLORING)
+ return KEEP_EXPLORING;
+ return DONE_EXPLORING;
+}
+
static int visit_func_call_insn(int t, struct bpf_insn *insns,
struct bpf_verifier_env *env,
bool visit_callee)
{
- int ret, insn_sz;
+ int ret, insn_sz, pad_ret;
int w;
+ pad_ret = visit_cleanup_pad_edge(t, env);
+ if (pad_ret < 0)
+ return pad_ret;
+
insn_sz = bpf_is_ldimm64(&insns[t]) ? 2 : 1;
ret = push_insn(t, t + insn_sz, FALLTHROUGH, env);
if (ret)
- return ret;
+ return merge_visit_ret(pad_ret, ret);
mark_prune_point(env, t + insn_sz);
/* when we exit from subprog, we need to record non-linear history */
@@ -180,7 +221,7 @@ static int visit_func_call_insn(int t, struct bpf_insn *insns,
merge_callee_effects(env, t, w);
ret = push_insn(t, w, BRANCH, env);
}
- return ret;
+ return merge_visit_ret(pad_ret, ret);
}
struct bpf_iarray *bpf_iarray_realloc(struct bpf_iarray *old, size_t n_elem)
@@ -592,6 +633,7 @@ int bpf_check_cfg(struct bpf_verifier_env *env)
int insn_cnt = env->prog->len;
int *insn_stack, *insn_state;
int ex_insn_beg, i, ret = 0;
+ u32 pad_idx = 0;
insn_state = env->cfg.insn_state = kvzalloc_objs(int, insn_cnt,
GFP_KERNEL_ACCOUNT);
@@ -647,6 +689,22 @@ int bpf_check_cfg(struct bpf_verifier_env *env)
goto walk_cfg;
}
+ /*
+ * A landing pad no call site was marked with -- a record whose range
+ * holds no call an exception can unwind out of -- is reached by
+ * nothing. Walk it from here, and let the dead code sweep remove it.
+ */
+ while (pad_idx < env->cleanup_info_cnt) {
+ u32 pad = env->cleanup_info[pad_idx++].landing_pad_off;
+
+ if (insn_state[pad] != EXPLORED) {
+ insn_state[pad] = DISCOVERED;
+ insn_stack[0] = pad;
+ env->cfg.cur_stack = 1;
+ goto walk_cfg;
+ }
+ }
+
for (i = 0; i < insn_cnt; i++) {
struct bpf_insn *insn = &env->prog->insnsi[i];
diff --git a/kernel/bpf/check_btf.c b/kernel/bpf/check_btf.c
index 0e8b3ccc7a5b9..d03dc791042a2 100644
--- a/kernel/bpf/check_btf.c
+++ b/kernel/bpf/check_btf.c
@@ -407,6 +407,149 @@ static int check_core_relo(struct bpf_verifier_env *env,
return err;
}
+static int cleanup_insn_subprog(struct bpf_verifier_env *env, u32 off)
+{
+ struct bpf_subprog_info *info;
+
+ if (off >= env->prog->len)
+ return -1;
+ info = bpf_find_containing_subprog(env, off);
+ return info ? info - env->subprog_info : -1;
+}
+
+#define MIN_BPF_CLEANUP_INFO_SIZE 12
+#define MAX_CLEANUP_INFO_REC_SIZE MAX_FUNCINFO_REC_SIZE
+
+static int check_cleanup_info(struct bpf_verifier_env *env,
+ const union bpf_attr *attr,
+ bpfptr_t uattr)
+{
+ u32 krec_size = sizeof(struct bpf_cleanup_info);
+ u32 i, nrec, urec_size, min_size, prev_end = 0;
+ struct bpf_cleanup_info *krecord;
+ bpfptr_t urecord;
+ int ret = -EINVAL;
+
+ nrec = attr->cleanup_info_cnt;
+ if (!nrec)
+ return 0;
+ if (nrec > INT_MAX / krec_size)
+ return -EINVAL;
+
+ urec_size = attr->cleanup_info_rec_size;
+ if (urec_size < MIN_BPF_CLEANUP_INFO_SIZE ||
+ urec_size > MAX_CLEANUP_INFO_REC_SIZE ||
+ urec_size % sizeof(u32)) {
+ verbose(env, "invalid cleanup info rec size %u\n", urec_size);
+ return -EINVAL;
+ }
+
+ krecord = kvcalloc(nrec, krec_size, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!krecord)
+ return -ENOMEM;
+
+ min_size = min_t(u32, krec_size, urec_size);
+ urecord = make_bpfptr(attr->cleanup_info, uattr.is_kernel);
+ for (i = 0; i < nrec; i++) {
+ struct bpf_cleanup_info *rec = &krecord[i];
+ int sb, se, sl;
+
+ ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size);
+ if (ret) {
+ if (ret == -E2BIG) {
+ verbose(env, "nonzero tailing record in cleanup info\n");
+ if (copy_to_bpfptr_offset(uattr,
+ offsetof(union bpf_attr,
+ cleanup_info_rec_size),
+ &min_size, sizeof(min_size)))
+ ret = -EFAULT;
+ }
+ goto err_free;
+ }
+
+ if (copy_from_bpfptr(rec, urecord, min_size)) {
+ ret = -EFAULT;
+ goto err_free;
+ }
+ bpfptr_add(&urecord, urec_size);
+
+ ret = -EINVAL;
+ if (rec->begin_off >= rec->end_off) {
+ verbose(env, "cleanup_info[%u]: begin %u >= end %u\n",
+ i, rec->begin_off, rec->end_off);
+ goto err_free;
+ }
+ if (i && rec->begin_off < prev_end) {
+ verbose(env,
+ "cleanup_info[%u]: range [%u,%u) is unsorted or overlaps the previous record\n",
+ i, rec->begin_off, rec->end_off);
+ goto err_free;
+ }
+ prev_end = rec->end_off;
+
+ sb = cleanup_insn_subprog(env, rec->begin_off);
+ se = cleanup_insn_subprog(env, rec->end_off - 1);
+ sl = cleanup_insn_subprog(env, rec->landing_pad_off);
+ if (sb < 0 || se < 0 || sl < 0) {
+ verbose(env, "cleanup_info[%u]: offset out of range\n", i);
+ goto err_free;
+ }
+ if (sb != se || sb != sl) {
+ verbose(env,
+ "cleanup_info[%u]: range/landing pad span multiple subprogs\n",
+ i);
+ goto err_free;
+ }
+ /*
+ * The second half of a 16-byte instruction carries a zero
+ * opcode and is not an instruction of its own, so no offset
+ * may name one. end_off is exclusive, so it may also be one
+ * past the last instruction of the program.
+ */
+ if (!env->prog->insnsi[rec->begin_off].code ||
+ !env->prog->insnsi[rec->landing_pad_off].code ||
+ (rec->end_off < env->prog->len &&
+ !env->prog->insnsi[rec->end_off].code)) {
+ verbose(env, "cleanup_info[%u]: points at invalid insn\n", i);
+ goto err_free;
+ }
+ }
+
+ /*
+ * Reject a landing pad that lies inside a call-site range, its own
+ * included: it would be both a pad and a call that unwinds to one, and
+ * an exception out of it would have nowhere to go.
+ */
+ ret = -EINVAL;
+ for (i = 0; i < nrec; i++) {
+ u32 pad = krecord[i].landing_pad_off;
+ u32 l = 0, r = nrec;
+
+ while (l < r) {
+ u32 m = l + (r - l) / 2;
+
+ if (pad < krecord[m].begin_off) {
+ r = m;
+ } else if (pad >= krecord[m].end_off) {
+ l = m + 1;
+ } else {
+ verbose(env,
+ "cleanup_info[%u]: landing pad %u is inside the call-site range of cleanup_info[%u]\n",
+ i, pad, m);
+ goto err_free;
+ }
+ }
+ }
+
+ env->cleanup_info = krecord;
+ env->cleanup_info_cnt = nrec;
+ return 0;
+
+err_free:
+ kvfree(krecord);
+ return ret;
+}
+
int bpf_prepare_btf_info(struct bpf_verifier_env *env,
const union bpf_attr *attr,
bpfptr_t uattr)
@@ -441,6 +584,10 @@ int bpf_check_btf_info(struct bpf_verifier_env *env,
{
int err;
+ err = check_cleanup_info(env, attr, uattr);
+ if (err)
+ return err;
+
if (!attr->func_info_cnt && !attr->line_info_cnt) {
if (check_abnormal_return(env))
return -EINVAL;
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 4e208cc94752f..bd2919063cecd 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -292,6 +292,7 @@ void __bpf_prog_free(struct bpf_prog *fp)
mutex_destroy(&fp->aux->dst_mutex);
mutex_destroy(&fp->aux->st_ops_assoc_mutex);
kfree(fp->aux->poke_tab);
+ bpf_cleanup_free_info(fp->aux);
kfree(fp->aux);
}
free_percpu(fp->stats);
@@ -2625,13 +2626,21 @@ static bool bpf_prog_select_interpreter(struct bpf_prog *fp)
return select_interpreter;
}
-static struct bpf_prog *bpf_prog_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)
+static struct bpf_prog *bpf_prog_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog,
+ int *err)
{
#ifdef CONFIG_BPF_JIT
struct bpf_prog *orig_prog;
+ int ret;
- if (!bpf_prog_need_blind(prog))
+ if (!bpf_prog_need_blind(prog)) {
+ ret = bpf_cleanup_attach_main_prog(env, prog);
+ if (ret) {
+ *err = ret;
+ return prog;
+ }
return bpf_int_jit_compile(env, prog);
+ }
orig_prog = prog;
prog = bpf_jit_blind_constants(env, prog);
@@ -2642,6 +2651,13 @@ static struct bpf_prog *bpf_prog_jit_compile(struct bpf_verifier_env *env, struc
if (IS_ERR(prog))
goto out_restore;
+ ret = bpf_cleanup_attach_main_prog(env, prog);
+ if (ret) {
+ *err = ret;
+ bpf_jit_prog_release_other(orig_prog, prog);
+ goto out_restore;
+ }
+
prog = bpf_int_jit_compile(env, prog);
if (prog->jited) {
bpf_jit_prog_release_other(prog, orig_prog);
@@ -2681,8 +2697,10 @@ struct bpf_prog *__bpf_prog_select_runtime(struct bpf_verifier_env *env, struct
if (*err)
return fp;
- fp = bpf_prog_jit_compile(env, fp);
+ fp = bpf_prog_jit_compile(env, fp, err);
bpf_prog_jit_attempt_done(fp);
+ if (*err)
+ return fp;
if (!fp->jited && jit_needed) {
*err = -ENOTSUPP;
return fp;
@@ -3470,6 +3488,17 @@ void __weak arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp,
{
}
+bool __weak bpf_jit_supports_cleanup_pads(void)
+{
+ return false;
+}
+
+/* Call @pad with the frame pointer @frame_fp and r6-r9 spilled at @spill_base. */
+void __weak arch_bpf_run_cleanup_pad(u64 pad, u64 frame_fp, u64 spill_base)
+{
+ WARN_ON_ONCE(1);
+}
+
bool __weak bpf_jit_supports_timed_may_goto(void)
{
return false;
diff --git a/kernel/bpf/exception.c b/kernel/bpf/exception.c
new file mode 100644
index 0000000000000..521086d084a35
--- /dev/null
+++ b/kernel/bpf/exception.c
@@ -0,0 +1,654 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <linux/bitmap.h>
+#include <linux/bpf.h>
+#include <linux/bpf_verifier.h>
+#include <linux/btf.h>
+#include <linux/btf_ids.h>
+#include <linux/filter.h>
+#include <linux/slab.h>
+#include <linux/sort.h>
+#include "exception.h"
+
+#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
+
+enum exc_kfunc {
+ EXC_KF_bpf_unwind_resume,
+};
+
+BTF_ID_LIST(exc_kfunc_list)
+BTF_ID(func, bpf_unwind_resume)
+
+static bool insn_is_exc_kfunc(const struct bpf_insn *insn, int kf)
+{
+ return bpf_pseudo_kfunc_call(insn) && insn->off == 0 &&
+ insn->imm == exc_kfunc_list[kf];
+}
+
+/* What an instruction does to intra-subprog control flow. */
+enum cleanup_insn_kind {
+ CLEANUP_INSN_PLAIN, /* the next insn runs */
+ CLEANUP_INSN_JUMP, /* unconditional jump */
+ CLEANUP_INSN_COND, /* the next insn runs, or the branch target */
+ CLEANUP_INSN_EXIT,
+ CLEANUP_INSN_THROW, /* call bpf_throw: nothing after it runs */
+ CLEANUP_INSN_RESUME, /* call bpf_unwind_resume: likewise */
+ CLEANUP_INSN_CALL, /* call to another subprog */
+ CLEANUP_INSN_GOTOX, /* indirect jump: successors not known here */
+};
+
+/* What each instruction can reach, computed once by cleanup_reachability(). */
+#define CLEANUP_REACH_RESUME BIT(0) /* a bpf_unwind_resume() call */
+#define CLEANUP_REACH_EXIT BIT(1) /* a plain BPF_EXIT */
+#define CLEANUP_REACH_UNKNOWN BIT(2) /* an indirect jump */
+#define CLEANUP_REACH_THROW BIT(3) /* a bpf_throw() call */
+
+/* Scratch shared by the analyses, sized once so no walker has to allocate. */
+struct cleanup_ctx {
+ struct bpf_verifier_env *env;
+ u8 *reach; /* per insn: CLEANUP_REACH_* mask */
+ u32 *stack; /* per insn: DFS stack */
+ void *scratch; /* the one allocation all of the above live in */
+};
+
+static bool in_pad(struct bpf_verifier_env *env, u32 i)
+{
+ return env->insn_aux_data[i].in_cleanup_pad;
+}
+
+/* One scratch array for cleanup_alloc() to hand out. */
+struct cleanup_alloc_req {
+ void **dst;
+ size_t n, sz;
+};
+
+static void *cleanup_alloc(const struct cleanup_alloc_req *tab, u32 cnt)
+{
+ size_t total = 0;
+ char *block, *p;
+ u32 i;
+
+ for (i = 0; i < cnt; i++)
+ total += round_up(tab[i].n * tab[i].sz, 8);
+
+ block = kvzalloc(total, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!block)
+ return NULL;
+
+ for (i = 0, p = block; i < cnt; i++) {
+ *tab[i].dst = p;
+ p += round_up(tab[i].n * tab[i].sz, 8);
+ }
+ return block;
+}
+
+static int cleanup_subprog_of(struct bpf_verifier_env *env, u32 off)
+{
+ struct bpf_subprog_info *info = bpf_find_containing_subprog(env, off);
+
+ return info ? info - env->subprog_info : -1;
+}
+
+/* The subprogram a linear pass is currently in. */
+struct cleanup_cursor {
+ u32 start, end; /* [start, end) of the current subprogram */
+ int sub; /* its index */
+};
+
+#define CLEANUP_CURSOR_INIT { .sub = -1 }
+
+static void cleanup_cursor_to(struct bpf_verifier_env *env, struct cleanup_cursor *c, u32 i)
+{
+ while (i >= c->end) {
+ c->sub++;
+ c->start = env->subprog_info[c->sub].start;
+ c->end = env->subprog_info[c->sub + 1].start;
+ }
+}
+
+static enum cleanup_insn_kind cleanup_classify(struct bpf_verifier_env *env, u32 i,
+ int *next, int *target)
+{
+ struct bpf_insn *insn = &env->prog->insnsi[i];
+ u8 class = BPF_CLASS(insn->code);
+
+ *next = i + 1;
+ *target = -1;
+
+ if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) {
+ *next = i + 2;
+ return CLEANUP_INSN_PLAIN;
+ }
+ if (class != BPF_JMP && class != BPF_JMP32)
+ return CLEANUP_INSN_PLAIN;
+
+ switch (BPF_OP(insn->code)) {
+ case BPF_EXIT:
+ *next = -1;
+ return CLEANUP_INSN_EXIT;
+ case BPF_JA:
+ *next = -1;
+ if (BPF_SRC(insn->code) == BPF_X)
+ return CLEANUP_INSN_GOTOX;
+ *target = class == BPF_JMP32 ? i + insn->imm + 1 : i + insn->off + 1;
+ return CLEANUP_INSN_JUMP;
+ case BPF_CALL:
+ if (bpf_is_throw_kfunc(insn)) {
+ *next = -1;
+ return CLEANUP_INSN_THROW;
+ }
+ if (insn_is_exc_kfunc(insn, EXC_KF_bpf_unwind_resume)) {
+ *next = -1;
+ return CLEANUP_INSN_RESUME;
+ }
+ return bpf_pseudo_call(insn) ? CLEANUP_INSN_CALL : CLEANUP_INSN_PLAIN;
+ default:
+ /* Conditional jump, including BPF_JCOND. */
+ *target = i + insn->off + 1;
+ return CLEANUP_INSN_COND;
+ }
+}
+
+static void cleanup_mark_throw_sites(struct bpf_verifier_env *env)
+{
+ u32 i;
+
+ for (i = 0; i < env->prog->len; i++)
+ if (bpf_is_throw_kfunc(&env->prog->insnsi[i]))
+ env->insn_aux_data[i].cleanup_throw_site = true;
+}
+
+int bpf_cleanup_check_callback(struct bpf_verifier_env *env, int subprog)
+{
+ if (!env->cleanup_info_cnt || !env->subprog_info[subprog].might_throw)
+ return 0;
+
+ verbose(env, "subprog %d may unwind and is used as a callback\n", subprog);
+ return -EINVAL;
+}
+
+/* Intra-subprog successors of @i, or -1 each when absent. */
+static enum cleanup_insn_kind cleanup_succ(struct bpf_verifier_env *env, u32 i,
+ u32 start, u32 end, int *next, int *target)
+{
+ enum cleanup_insn_kind kind = cleanup_classify(env, i, next, target);
+
+ if (*next < (int)start || *next >= (int)end)
+ *next = -1;
+ if (*target < (int)start || *target >= (int)end)
+ *target = -1;
+ return kind;
+}
+
+static void cleanup_add_pred(u32 *head, u32 *link, u32 to, u32 e)
+{
+ link[e] = head[to];
+ head[to] = e + 1;
+}
+
+/* What every instruction can reach along intra-subprog edges, for
+ * cleanup_pad_is_catch(). One backward walk over a predecessor index, rather
+ * than a forward walk from each landing pad, which would be quadratic.
+ */
+static int cleanup_reachability(struct cleanup_ctx *ctx)
+{
+ struct bpf_verifier_env *env = ctx->env;
+ u32 len = env->prog->len;
+ struct cleanup_cursor c = CLEANUP_CURSOR_INIT;
+ u32 *head = NULL, *link = NULL;
+ bool *queued = NULL;
+ u32 i, sp = 0;
+ void *scratch;
+ const struct cleanup_alloc_req tab[] = {
+ { (void **)&head, len, sizeof(*head) },
+ { (void **)&link, 2 * (size_t)len, sizeof(*link) },
+ { (void **)&queued, len, sizeof(*queued) },
+ };
+
+ scratch = cleanup_alloc(tab, ARRAY_SIZE(tab));
+ if (!scratch)
+ return -ENOMEM;
+
+ /* Index the predecessors, and seed the walk at the terminators. */
+ for (i = 0; i < len; i++) {
+ enum cleanup_insn_kind kind;
+ int next, target;
+
+ cleanup_cursor_to(env, &c, i);
+ kind = cleanup_succ(env, i, c.start, c.end, &next, &target);
+
+ if (kind == CLEANUP_INSN_RESUME)
+ ctx->reach[i] |= CLEANUP_REACH_RESUME;
+ else if (kind == CLEANUP_INSN_EXIT)
+ ctx->reach[i] |= CLEANUP_REACH_EXIT;
+ else if (kind == CLEANUP_INSN_GOTOX)
+ ctx->reach[i] |= CLEANUP_REACH_UNKNOWN;
+ else if (kind == CLEANUP_INSN_THROW)
+ ctx->reach[i] |= CLEANUP_REACH_THROW;
+
+ if (next >= 0)
+ cleanup_add_pred(head, link, next, 2 * i);
+ if (target >= 0)
+ cleanup_add_pred(head, link, target, 2 * i + 1);
+
+ if (ctx->reach[i]) {
+ queued[i] = true;
+ ctx->stack[sp++] = i;
+ }
+ }
+
+ /* Each instruction re-enters the worklist at most once per bit it
+ * gains, so this is linear in the number of edges.
+ */
+ while (sp) {
+ u32 j = ctx->stack[--sp];
+ u8 flags = ctx->reach[j];
+ u32 e;
+
+ queued[j] = false;
+ for (e = head[j]; e; e = link[e - 1]) {
+ u32 p = (e - 1) / 2;
+
+ if ((ctx->reach[p] | flags) == ctx->reach[p])
+ continue;
+ ctx->reach[p] |= flags;
+ if (!queued[p]) {
+ queued[p] = true;
+ ctx->stack[sp++] = p;
+ }
+ }
+ }
+ kvfree(scratch);
+ return 0;
+}
+
+static int cleanup_pad_is_catch(struct cleanup_ctx *ctx, u32 pad)
+{
+ u8 reach = ctx->reach[pad];
+
+ if (reach & CLEANUP_REACH_UNKNOWN) {
+ verbose(ctx->env, "cleanup landing pad %u reaches an indirect jump\n", pad);
+ return -EINVAL;
+ }
+ if (reach & CLEANUP_REACH_THROW) {
+ verbose(ctx->env,
+ "cleanup landing pad %u can throw while an exception is in flight\n",
+ pad);
+ return -EINVAL;
+ }
+ if (!(reach & CLEANUP_REACH_RESUME) == !(reach & CLEANUP_REACH_EXIT)) {
+ verbose(ctx->env, "cleanup landing pad %u %s\n", pad,
+ (reach & CLEANUP_REACH_RESUME) ?
+ "reaches both bpf_unwind_resume() and a plain exit" :
+ "reaches neither bpf_unwind_resume() nor an exit");
+ return -EINVAL;
+ }
+ return !!(reach & CLEANUP_REACH_EXIT);
+}
+
+static int cleanup_check_pad_insn(struct bpf_verifier_env *env, u32 i)
+{
+ struct bpf_insn *insn = &env->prog->insnsi[i];
+
+ if (bpf_helper_call(insn) && insn->imm == BPF_FUNC_tail_call) {
+ verbose(env,
+ "bpf_tail_call() at insn %u is in an exception cleanup landing pad\n",
+ i);
+ return -EINVAL;
+ }
+ /* Stack arguments are not supported. */
+ if (is_stack_arg_st(insn) || is_stack_arg_stx(insn)) {
+ verbose(env,
+ "insn %u passes an on-stack call argument in an exception cleanup landing pad\n",
+ i);
+ return -EINVAL;
+ }
+ /* Likewise, stack arguments are not supported. */
+ if (bpf_pseudo_kfunc_call(insn)) {
+ struct bpf_call_summary cs;
+
+ if (bpf_get_call_summary(env, insn, &cs) &&
+ cs.arg_slot_cnt > MAX_BPF_FUNC_REG_ARGS) {
+ verbose(env,
+ "insn %u passes an on-stack call argument in an exception cleanup landing pad\n",
+ i);
+ return -EINVAL;
+ }
+ }
+ return 0;
+}
+
+static int cleanup_mark_pad_bodies(struct cleanup_ctx *ctx)
+{
+ struct bpf_verifier_env *env = ctx->env;
+ u32 i, sp = 0;
+ int ret;
+
+ for (i = 0; i < env->cleanup_info_cnt; i++) {
+ u32 pad = env->cleanup_info[i].landing_pad_off;
+
+ if (in_pad(env, pad))
+ continue;
+
+ ret = cleanup_pad_is_catch(ctx, pad);
+ if (ret < 0)
+ return ret;
+ if (ret) {
+ verbose(env,
+ "catch landing pad %u is not supported yet, only cleanup pads that resume\n",
+ pad);
+ return -EOPNOTSUPP;
+ }
+ env->insn_aux_data[pad].in_cleanup_pad = true;
+ ctx->stack[sp++] = pad;
+ }
+
+ while (sp) {
+ u32 j = ctx->stack[--sp];
+ enum cleanup_insn_kind kind;
+ int next, target, sub;
+ u32 start, end;
+
+ ret = cleanup_check_pad_insn(env, j);
+ if (ret)
+ return ret;
+
+ sub = cleanup_subprog_of(env, j);
+ start = env->subprog_info[sub].start;
+ end = env->subprog_info[sub + 1].start;
+ kind = cleanup_succ(env, j, start, end, &next, &target);
+
+ if (kind == CLEANUP_INSN_CALL) {
+ int callee = cleanup_subprog_of(env, j + env->prog->insnsi[j].imm + 1);
+
+ if (env->subprog_info[callee].might_throw) {
+ verbose(env,
+ "cleanup landing pad calls subprog %d at insn %u, which can throw while an exception is in flight\n",
+ callee, j);
+ return -EINVAL;
+ }
+ }
+
+ if (next >= 0 && !in_pad(env, next)) {
+ env->insn_aux_data[next].in_cleanup_pad = true;
+ ctx->stack[sp++] = next;
+ }
+ if (target >= 0 && !in_pad(env, target)) {
+ env->insn_aux_data[target].in_cleanup_pad = true;
+ ctx->stack[sp++] = target;
+ }
+ }
+ return 0;
+}
+
+static int cleanup_check_resumes(struct cleanup_ctx *ctx)
+{
+ struct bpf_verifier_env *env = ctx->env;
+ u32 i;
+
+ for (i = 0; i < env->prog->len; i++) {
+ if (!insn_is_exc_kfunc(&env->prog->insnsi[i], EXC_KF_bpf_unwind_resume))
+ continue;
+ if (in_pad(env, i))
+ continue;
+ verbose(env,
+ "bpf_unwind_resume() at insn %u is not in an exception cleanup landing pad\n",
+ i);
+ return -EINVAL;
+ }
+ return 0;
+}
+
+static void cleanup_mark_call_sites(struct bpf_verifier_env *env)
+{
+ u32 i, j;
+
+ for (i = 0; i < env->cleanup_info_cnt; i++) {
+ struct bpf_cleanup_info *rec = &env->cleanup_info[i];
+
+ for (j = rec->begin_off; j < rec->end_off; j++) {
+ struct bpf_insn *insn = &env->prog->insnsi[j];
+
+ if (!bpf_pseudo_call(insn) && !bpf_is_throw_kfunc(insn))
+ continue;
+ env->insn_aux_data[j].cleanup_pad = rec->landing_pad_off + 1;
+ }
+ }
+}
+
+int bpf_prepare_cleanup_exceptions(struct bpf_verifier_env *env)
+{
+ if (!env->cleanup_info_cnt)
+ return 0;
+
+ if (bpf_prog_is_offloaded(env->prog->aux)) {
+ verbose(env,
+ "exception cleanup is not supported for offloaded programs\n");
+ return -EINVAL;
+ }
+
+ if (!bpf_jit_supports_cleanup_pads() || !env->prog->jit_requested) {
+ verbose(env,
+ "exception cleanup needs a JIT that can dispatch landing pads\n");
+ return -EOPNOTSUPP;
+ }
+ env->prog->jit_required = 1;
+
+ if (env->exception_callback_subprog) {
+ verbose(env,
+ "exception cleanup table cannot be combined with an exception callback\n");
+ return -EINVAL;
+ }
+
+ cleanup_mark_throw_sites(env);
+ cleanup_mark_call_sites(env);
+ return 0;
+}
+
+int bpf_check_cleanup_exceptions(struct bpf_verifier_env *env)
+{
+ u32 len = env->prog->len;
+ struct cleanup_ctx ctx = { .env = env };
+ const struct cleanup_alloc_req tab[] = {
+ { (void **)&ctx.reach, len, sizeof(*ctx.reach) },
+ { (void **)&ctx.stack, len, sizeof(*ctx.stack) },
+ };
+ int ret;
+
+ if (!env->cleanup_info_cnt)
+ return 0;
+
+ ctx.scratch = cleanup_alloc(tab, ARRAY_SIZE(tab));
+ if (!ctx.scratch)
+ return -ENOMEM;
+
+ ret = cleanup_reachability(&ctx);
+ if (ret)
+ goto out;
+
+ ret = cleanup_mark_pad_bodies(&ctx);
+ if (ret)
+ goto out;
+
+ ret = cleanup_check_resumes(&ctx);
+out:
+ kvfree(ctx.scratch);
+ return ret;
+}
+
+bool bpf_is_unwind_resume_kfunc(const struct bpf_insn *insn)
+{
+ return insn_is_exc_kfunc(insn, EXC_KF_bpf_unwind_resume);
+}
+
+int bpf_cleanup_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;
+}
+
+/*
+ * Every subprogram of a cleanup-carrying program spills the BPF callee-saved
+ * registers, even one that never throws: a frame's spill holds its caller's
+ * registers, and that is what the walker restores before running the caller's
+ * pad. The exception callback does not, because it reuses the boundary frame
+ * rather than building one of its own.
+ */
+bool bpf_cleanup_force_spill(const struct bpf_prog *prog)
+{
+ return prog->aux->exc && !prog->aux->exception_cb;
+}
+
+const struct bpf_cleanup_range *bpf_cleanup_pad_for_ip(const struct bpf_prog *prog, u64 ip)
+{
+ const struct bpf_exception_info *exc = prog->aux->exc;
+ u32 l = 0, r = exc ? exc->nr_ranges : 0;
+
+ while (l < r) {
+ u32 m = l + (r - l) / 2;
+ const struct bpf_cleanup_range *rec = &exc->ranges[m];
+
+ if (ip <= rec->begin)
+ r = m;
+ else if (ip > rec->end)
+ l = m + 1;
+ else
+ return rec;
+ }
+ return NULL;
+}
+
+static int cmp_u32(const void *a, const void *b)
+{
+ u32 x = *(const u32 *)a, y = *(const u32 *)b;
+
+ return x < y ? -1 : x > y;
+}
+
+int bpf_cleanup_alloc_info(struct bpf_prog_aux *aux)
+{
+ if (aux->exc)
+ return 0;
+ aux->exc = kzalloc_obj(struct bpf_exception_info, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ return aux->exc ? 0 : -ENOMEM;
+}
+
+int bpf_cleanup_attach_info(struct bpf_prog_aux *aux, struct bpf_cleanup_info *recs, u32 cnt)
+{
+ struct bpf_exception_info *exc = aux->exc;
+ struct bpf_cleanup_range *ranges;
+ u32 i, n_at, *at;
+
+ if (!cnt) {
+ kvfree(recs);
+ return 0;
+ }
+
+ ranges = kvcalloc(cnt, sizeof(*ranges), GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!ranges) {
+ kvfree(recs);
+ return -ENOMEM;
+ }
+
+ /* The pads on their own, sorted and deduplicated. */
+ at = kvmalloc_array(cnt, sizeof(*at), GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!at) {
+ kvfree(ranges);
+ kvfree(recs);
+ return -ENOMEM;
+ }
+ for (i = 0; i < cnt; i++)
+ at[i] = recs[i].landing_pad_off;
+ sort(at, cnt, sizeof(*at), cmp_u32, NULL);
+ for (i = 0, n_at = 0; i < cnt; i++)
+ if (!n_at || at[n_at - 1] != at[i])
+ at[n_at++] = at[i];
+
+ exc->pad_at = at;
+ exc->nr_pad_at = n_at;
+ exc->info = recs;
+ exc->nr_info = cnt;
+ exc->ranges = ranges;
+ /* Withheld until the JIT has filled the table in. */
+ exc->nr_ranges = 0;
+ return 0;
+}
+
+void bpf_cleanup_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image)
+{
+ struct bpf_exception_info *exc = prog->aux->exc;
+ u32 i, n;
+
+ if (!exc || !exc->nr_info || !exc->ranges)
+ return;
+
+ n = exc->nr_info;
+ for (i = 0; i < n; i++) {
+ const struct bpf_cleanup_info *rec = &exc->info[i];
+
+ if (WARN_ON_ONCE(rec->begin_off >= prog->len ||
+ rec->end_off > prog->len ||
+ rec->landing_pad_off >= prog->len))
+ return;
+ exc->ranges[i].begin = (u64)(long)image + addrs[rec->begin_off];
+ exc->ranges[i].end = (u64)(long)image + addrs[rec->end_off];
+ exc->ranges[i].pad = (u64)(long)image + addrs[rec->landing_pad_off];
+ }
+ exc->nr_ranges = n;
+}
+
+void bpf_cleanup_free_info(struct bpf_prog_aux *aux)
+{
+ struct bpf_exception_info *exc = aux->exc;
+
+ if (!exc)
+ return;
+ kvfree(exc->ranges);
+ kvfree(exc->info);
+ kvfree(exc->pad_at);
+ kvfree(exc->throw_at);
+ bitmap_free(exc->pad_body);
+ kfree(exc);
+ aux->exc = NULL;
+}
+
+/* Is @idx in the sorted array @at of @n instruction indices? */
+static bool insn_idx_in(const u32 *at, u32 n, u32 idx)
+{
+ u32 l = 0, r = n;
+
+ while (l < r) {
+ u32 m = l + (r - l) / 2;
+
+ if (idx < at[m])
+ r = m;
+ else if (idx > at[m])
+ l = m + 1;
+ else
+ return true;
+ }
+ return false;
+}
+
+bool bpf_cleanup_insn_is_pad(const struct bpf_prog *prog, u32 idx)
+{
+ const struct bpf_exception_info *exc = prog->aux->exc;
+
+ return exc && insn_idx_in(exc->pad_at, exc->nr_pad_at, idx);
+}
+
+bool bpf_cleanup_insn_is_throw(const struct bpf_prog *prog, u32 idx)
+{
+ const struct bpf_exception_info *exc = prog->aux->exc;
+
+ return exc && insn_idx_in(exc->throw_at, exc->nr_throw_at, idx);
+}
+
+bool bpf_cleanup_insn_in_pad(const struct bpf_prog *prog, u32 idx)
+{
+ const struct bpf_exception_info *exc = prog->aux->exc;
+
+ return exc && exc->pad_body && idx < exc->nr_pad_body &&
+ test_bit(idx, exc->pad_body);
+}
diff --git a/kernel/bpf/exception.h b/kernel/bpf/exception.h
new file mode 100644
index 0000000000000..c0e68ce227c83
--- /dev/null
+++ b/kernel/bpf/exception.h
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#ifndef _LINUX_BPF_EXCEPTION_H
+#define _LINUX_BPF_EXCEPTION_H
+
+#include <linux/types.h>
+
+struct bpf_cleanup_info;
+struct bpf_cleanup_range;
+struct bpf_prog;
+struct bpf_prog_aux;
+struct bpf_verifier_env;
+
+int bpf_prepare_cleanup_exceptions(struct bpf_verifier_env *env);
+int bpf_check_cleanup_exceptions(struct bpf_verifier_env *env);
+int bpf_cleanup_check_callback(struct bpf_verifier_env *env, int subprog);
+int bpf_cleanup_pad_of_call(struct bpf_verifier_env *env, u32 idx);
+int bpf_cleanup_alloc_info(struct bpf_prog_aux *aux);
+int bpf_cleanup_attach_info(struct bpf_prog_aux *aux, struct bpf_cleanup_info *recs, u32 cnt);
+const struct bpf_cleanup_range *bpf_cleanup_pad_for_ip(const struct bpf_prog *prog, u64 ip);
+
+#endif /* _LINUX_BPF_EXCEPTION_H */
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index 2add8001c3ec3..134aafa6a6c9b 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <linux/bitmap.h>
#include <linux/bpf.h>
#include <linux/btf.h>
#include <linux/bpf_verifier.h>
@@ -10,6 +11,7 @@
#include <linux/perf_event.h>
#include <net/xdp.h>
#include "disasm.h"
+#include "exception.h"
#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
@@ -252,15 +254,26 @@ static void adjust_insn_aux_data(struct bpf_verifier_env *env,
/* Expand insni[off]'s seen count to the patched range. */
data[i].seen = old_seen;
data[i].zext_dst = bpf_insn_def32(new_prog, insn + i) >= 0;
+ data[i].in_cleanup_pad = data[off + cnt - 1].in_cleanup_pad;
if (!memcmp(insn + i, original_insn, sizeof(struct bpf_insn))) {
data[i].non_stack_access =
data[off + cnt - 1].non_stack_access;
data[off + cnt - 1].non_stack_access = false;
+ data[i].cleanup_throw_site =
+ data[off + cnt - 1].cleanup_throw_site;
+ data[off + cnt - 1].cleanup_throw_site = 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
@@ -549,6 +562,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_prog_is_offloaded(env->prog->aux))
bpf_prog_offload_remove_insns(env, off, cnt);
@@ -573,6 +587,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;
}
@@ -1095,6 +1120,116 @@ static void bpf_restore_subprog_starts(struct bpf_verifier_env *env, u32 *orig_s
env->subprog_info[env->subprog_cnt].start = env->prog->len;
}
+static int cleanup_throw_sites_for_subprog(struct bpf_verifier_env *env, struct bpf_prog *sub,
+ u32 start, u32 end)
+{
+ u32 i, cnt = 0, *at;
+
+ for (i = start; i < end; i++)
+ if (env->insn_aux_data[i].cleanup_throw_site)
+ cnt++;
+ if (!cnt)
+ return 0;
+
+ at = kvmalloc_array(cnt, sizeof(*at), GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!at)
+ return -ENOMEM;
+
+ for (i = start, cnt = 0; i < end; i++) {
+ if (!env->insn_aux_data[i].cleanup_throw_site)
+ continue;
+ at[cnt++] = i - start;
+ }
+
+ sub->aux->exc->throw_at = at;
+ sub->aux->exc->nr_throw_at = cnt;
+ return 0;
+}
+
+static int cleanup_pad_body_for_subprog(struct bpf_verifier_env *env, struct bpf_prog *sub,
+ u32 start, u32 end)
+{
+ unsigned long *bits;
+ u32 i, cnt = 0;
+
+ for (i = start; i < end; i++)
+ if (env->insn_aux_data[i].in_cleanup_pad)
+ cnt++;
+ if (!cnt)
+ return 0;
+
+ bits = bitmap_zalloc(end - start, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!bits)
+ return -ENOMEM;
+
+ for (i = start; i < end; i++)
+ if (env->insn_aux_data[i].in_cleanup_pad)
+ __set_bit(i - start, bits);
+
+ sub->aux->exc->pad_body = bits;
+ sub->aux->exc->nr_pad_body = end - start;
+ return 0;
+}
+
+static int cleanup_info_for_subprog(struct bpf_verifier_env *env, struct bpf_prog *sub,
+ u32 start, u32 end)
+{
+ struct bpf_cleanup_info *recs;
+ u32 i, cnt = 0;
+ int err;
+
+ if (!env->cleanup_info_cnt)
+ return 0;
+
+ err = bpf_cleanup_alloc_info(sub->aux);
+ if (err)
+ return err;
+
+ err = cleanup_throw_sites_for_subprog(env, sub, start, end);
+ if (err)
+ return err;
+
+ err = cleanup_pad_body_for_subprog(env, sub, start, end);
+ if (err)
+ return err;
+
+ for (i = start; i < end; i++)
+ if (env->insn_aux_data[i].cleanup_pad)
+ cnt++;
+ if (!cnt)
+ return 0;
+
+ recs = kvmalloc_array(cnt, sizeof(*recs), GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!recs)
+ return -ENOMEM;
+
+ for (i = start, cnt = 0; i < end; i++) {
+ u32 pad = env->insn_aux_data[i].cleanup_pad;
+
+ if (!pad)
+ continue;
+ pad--;
+ if (verifier_bug_if(pad < start || pad >= end, env,
+ "insn %u is covered by a landing pad at %u outside its subprog [%u, %u)",
+ i, pad, start, end)) {
+ kvfree(recs);
+ return -EFAULT;
+ }
+ recs[cnt].begin_off = i - start;
+ recs[cnt].end_off = i - start + 1;
+ recs[cnt].landing_pad_off = pad - start;
+ cnt++;
+ }
+ return bpf_cleanup_attach_info(sub->aux, recs, cnt);
+}
+
+int bpf_cleanup_attach_main_prog(struct bpf_verifier_env *env, struct bpf_prog *prog)
+{
+ if (!env || env->subprog_cnt > 1)
+ return 0;
+ return cleanup_info_for_subprog(env, prog, 0, prog->len);
+}
+
static int jit_subprogs(struct bpf_verifier_env *env)
{
struct bpf_prog *prog = env->prog, **func, *tmp;
@@ -1232,6 +1367,10 @@ static int jit_subprogs(struct bpf_verifier_env *env)
func[i]->aux->token = prog->aux->token;
if (!i)
func[i]->aux->exception_boundary = env->seen_exception;
+ err = cleanup_info_for_subprog(env, func[i], subprog_start,
+ env->subprog_info[i + 1].start);
+ if (err)
+ goto out_free;
func[i] = bpf_int_jit_compile(env, func[i]);
if (!func[i]->jited) {
err = -ENOTSUPP;
@@ -1336,6 +1475,8 @@ static int jit_subprogs(struct bpf_verifier_env *env)
prog->aux->bpf_exception_cb = (void *)func[env->exception_callback_subprog]->bpf_func;
prog->aux->exception_boundary = func[0]->aux->exception_boundary;
prog->aux->stack_arg_sp_adjust = func[0]->aux->stack_arg_sp_adjust;
+ prog->aux->exc = func[0]->aux->exc;
+ func[0]->aux->exc = NULL;
bpf_prog_jit_attempt_done(prog);
return 0;
out_free:
@@ -1916,6 +2057,8 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
goto next_insn;
if (insn->src_reg == BPF_PSEUDO_CALL)
goto next_insn;
+ if (bpf_is_unwind_resume_kfunc(insn))
+ goto next_insn;
if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) {
ret = bpf_fixup_kfunc_call(env, insn, insn_buf, i + delta, &cnt);
if (ret)
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 051b6654e57c6..ffef72804fc9a 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -31,6 +31,7 @@
#include <linux/buildid.h>
#include "../../lib/kstrtox.h"
+#include "exception.h"
/* If kernel subsystem is allowing eBPF programs to call this function,
* inside its own verifier_ops->get_func_proto() callback it should return
@@ -3360,8 +3361,36 @@ struct bpf_throw_ctx {
u64 sp;
u64 bp;
int cnt;
+ const struct bpf_prog *callee;
+ u64 callee_fp;
};
+static void bpf_run_cleanup_pad(struct bpf_throw_ctx *ctx, const struct bpf_prog *prog,
+ u64 ip, u64 fp)
+{
+ const struct bpf_exception_info *exc = prog->aux->exc;
+ const struct bpf_cleanup_range *rec;
+ u64 spill_base;
+
+ if (!exc || !exc->nr_ranges)
+ return;
+ rec = bpf_cleanup_pad_for_ip(prog, ip);
+ if (!rec)
+ return;
+
+ /*
+ * The callee is always another subprogram of this program -- the walk
+ * ends at any frame that is not one -- so its prologue spilled these
+ * registers and its exc is there to say where.
+ */
+ if (ctx->callee)
+ spill_base = ctx->callee_fp + ctx->callee->aux->exc->spill_off;
+ else
+ spill_base = fp + exc->throw_spill_off;
+
+ arch_bpf_run_cleanup_pad(rec->pad, fp, spill_base);
+}
+
static bool bpf_stack_walker(void *cookie, u64 ip, u64 sp, u64 bp)
{
struct bpf_throw_ctx *ctx = cookie;
@@ -3378,6 +3407,11 @@ static bool bpf_stack_walker(void *cookie, u64 ip, u64 sp, u64 bp)
if (!prog)
return !ctx->cnt;
ctx->cnt++;
+
+ bpf_run_cleanup_pad(ctx, prog, ip, bp);
+ ctx->callee = prog;
+ ctx->callee_fp = bp;
+
if (bpf_is_subprog(prog))
return true;
ctx->aux = prog->aux;
@@ -3407,6 +3441,17 @@ __bpf_kfunc void bpf_throw(u64 cookie)
WARN(1, "A call to BPF exception callback should never return\n");
}
+/*
+ * Terminator of a compiler-emitted cleanup landing pad. The compiler names
+ * this _Unwind_Resume, the base unwind ABI's entry point for carrying an
+ * unwind on once a frame's cleanups have run. To match kernel kfunc
+ * convention, the kernel calls it bpf_unwind_resume and libbpf maps the
+ * compiler's name onto it.
+ */
+__bpf_kfunc void bpf_unwind_resume(void)
+{
+}
+
__bpf_kfunc int bpf_wq_init(struct bpf_wq *wq, void *p__const_map, unsigned int flags)
{
struct bpf_async_kern *async = (struct bpf_async_kern *)wq;
@@ -4853,6 +4898,7 @@ BTF_ID_FLAGS(func, bpf_task_get_cgroup1, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_task_from_vpid, KF_ACQUIRE | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_throw)
+BTF_ID_FLAGS(func, bpf_unwind_resume)
#ifdef CONFIG_BPF_EVENTS
BTF_ID_FLAGS(func, bpf_send_signal_task)
#endif
diff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c
index 44ecdc5b4ec2d..9cfd05f970bc9 100644
--- a/kernel/bpf/liveness.c
+++ b/kernel/bpf/liveness.c
@@ -8,6 +8,8 @@
#include <linux/slab.h>
#include <linux/sort.h>
+#include "exception.h"
+
#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
struct per_frame_masks {
@@ -256,6 +258,9 @@ bpf_insn_successors(struct bpf_verifier_env *env, u32 idx)
succ = env->succ;
succ->cnt = 0;
+ if (unlikely(bpf_is_unwind_resume_kfunc(insn)))
+ return succ;
+
opcode_info = &opcode_info_tbl[BPF_CLASS(insn->code) | BPF_OP(insn->code)];
insn_sz = bpf_is_ldimm64(insn) ? 2 : 1;
if (opcode_info->can_fallthrough)
@@ -264,6 +269,13 @@ bpf_insn_successors(struct bpf_verifier_env *env, u32 idx)
if (opcode_info->can_jump)
succ->items[succ->cnt++] = idx + bpf_jmp_offset(insn) + 1;
+ if (unlikely(env->cleanup_info_cnt)) {
+ int pad = bpf_cleanup_pad_of_call(env, idx);
+
+ if (pad >= 0)
+ succ->items[succ->cnt++] = pad;
+ }
+
return succ;
}
@@ -397,6 +409,14 @@ bool bpf_stack_slot_alive(struct bpf_verifier_env *env, u32 frameno, u32 half_sp
alive = bpf_calls_callback(env, callsite)
? is_live_before(instance, callsite, rel, half_spi)
: is_live_before(instance, callsite + 1, rel, half_spi);
+
+ /* Control may also go to the landing pad. */
+ if (!alive && unlikely(env->cleanup_info_cnt)) {
+ int pad = bpf_cleanup_pad_of_call(env, callsite);
+
+ if (pad >= 0)
+ alive = is_live_before(instance, pad, rel, half_spi);
+ }
if (alive)
return true;
}
diff --git a/kernel/bpf/states.c b/kernel/bpf/states.c
index 66fb11b6c6a76..be0f529f7eccb 100644
--- a/kernel/bpf/states.c
+++ b/kernel/bpf/states.c
@@ -996,6 +996,9 @@ static bool states_equal(struct bpf_verifier_env *env,
if (old->in_sleepable != cur->in_sleepable)
return false;
+ if (old->unwinding != cur->unwinding)
+ return false;
+
if (!refsafe(old, cur, &env->idmap_scratch))
return false;
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index def57bddb0924..ac70914697662 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2912,7 +2912,7 @@ int __init __used bpf_multi_func(void) { return 0; }
BTF_ID_LIST_GLOBAL_SINGLE(bpf_multi_func_btf_id, func, bpf_multi_func)
/* last field in 'union bpf_attr' used by this command */
-#define BPF_PROG_LOAD_LAST_FIELD keyring_id
+#define BPF_PROG_LOAD_LAST_FIELD cleanup_info_cnt
static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_attr *attr_log)
{
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 6c6b8d8520cdf..43ecf79baa4a9 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -10,6 +10,7 @@
#include <linux/slab.h>
#include <linux/bpf.h>
#include <linux/btf.h>
+#include <linux/bpf_cleanup_abi.h>
#include <linux/bpf_verifier.h>
#include <linux/filter.h>
#include <net/netlink.h>
@@ -37,6 +38,7 @@
#include "diagnostics.h"
#include "disasm.h"
+#include "exception.h"
static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
@@ -1714,6 +1716,7 @@ int bpf_copy_verifier_state(struct bpf_verifier_state *dst_state,
return err;
dst_state->speculative = src->speculative;
dst_state->in_sleepable = src->in_sleepable;
+ dst_state->unwinding = src->unwinding;
dst_state->curframe = src->curframe;
dst_state->branches = src->branches;
dst_state->parent = src->parent;
@@ -5588,6 +5591,14 @@ static int check_max_stack_depth(struct bpf_verifier_env *env)
}
}
+ /*
+ * A pad rebuilds its frame from a spill area, and on x86-64 a private
+ * stack's frame pointer is in no spill area. Refused on every arch
+ * rather than just that one.
+ */
+ if (env->cleanup_info_cnt)
+ priv_stack_mode = NO_PRIV_STACK;
+
if (priv_stack_mode == PRIV_STACK_UNKNOWN)
priv_stack_mode = bpf_enable_priv_stack(env->prog);
@@ -10484,6 +10495,10 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins
* callbacks
*/
env->subprog_info[subprog].is_cb = true;
+ err = bpf_cleanup_check_callback(env, subprog);
+ if (err)
+ return err;
+
if (bpf_pseudo_kfunc_call(insn) &&
!is_callback_calling_kfunc(insn->imm)) {
verifier_bug(env, "kfunc %s#%d not marked as callback-calling",
@@ -10535,8 +10550,8 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins
return 0;
}
-static int process_bpf_exit_full(struct bpf_verifier_env *env,
- bool *do_print_state, bool exception_exit);
+static int process_bpf_exit_full(struct bpf_verifier_env *env, bool *do_print_state);
+static int unwind_step(struct bpf_verifier_env *env, u32 callsite, int *insn_idx);
static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
int *insn_idx)
@@ -10626,7 +10641,7 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
verbose(env, "failed to push state for global subprog exception path\n");
return PTR_ERR(branch);
}
- return process_bpf_exit_full(env, NULL, true);
+ return unwind_step(env, *insn_idx, insn_idx);
}
/* continue with next insn after call */
@@ -14506,7 +14521,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
env->prog->call_session_cookie = true;
if (bpf_is_throw_kfunc(insn))
- return process_bpf_exit_full(env, NULL, true);
+ return unwind_step(env, insn_idx, &env->insn_idx);
return 0;
}
@@ -18431,9 +18446,75 @@ enum {
INSN_IDX_UPDATED = 2,
};
-static int process_bpf_exit_full(struct bpf_verifier_env *env,
- bool *do_print_state,
- bool exception_exit)
+static u32 unwind_pop_frame(struct bpf_verifier_env *env)
+{
+ struct bpf_verifier_state *state = env->cur_state;
+ struct bpf_func_state *callee = state->frame[state->curframe];
+ u32 callsite = callee->callsite;
+ struct bpf_func_state *caller;
+
+ caller = state->frame[state->curframe - 1];
+ account_processed_insns(env, callee, caller);
+ free_func_state(callee);
+ state->frame[state->curframe--] = NULL;
+ invalidate_outgoing_stack_args(env, caller);
+ return callsite;
+}
+
+static void unwind_enter_pad(struct bpf_verifier_env *env)
+{
+ struct bpf_func_state *frame = cur_func(env);
+
+ clear_caller_saved_regs(env, frame->regs);
+ mark_reg_unknown(env, frame->regs, BPF_REG_0);
+ __mark_reg_known(&frame->regs[BPF_REG_0], BPF_PAD_ENTRY_R0);
+}
+
+static int unwind_finish(struct bpf_verifier_env *env)
+{
+ int err = check_resource_leak(env, true, true, "bpf_throw");
+
+ if (err)
+ return err;
+ return PROCESS_BPF_EXIT;
+}
+
+static int unwind_step(struct bpf_verifier_env *env, u32 callsite, int *insn_idx)
+{
+ struct bpf_verifier_state *state = env->cur_state;
+
+ state->unwinding = true;
+ for (;;) {
+ int pad = bpf_cleanup_pad_of_call(env, callsite);
+
+ if (pad >= 0) {
+ unwind_enter_pad(env);
+ *insn_idx = pad;
+ return INSN_IDX_UPDATED;
+ }
+ if (!state->curframe)
+ return unwind_finish(env);
+ callsite = unwind_pop_frame(env);
+ }
+}
+
+static int process_cleanup_resume(struct bpf_verifier_env *env, int *insn_idx)
+{
+ struct bpf_verifier_state *state = env->cur_state;
+
+ /* A pad entered by ordinary control flow. */
+ if (!state->unwinding) {
+ verbose(env,
+ "bpf_unwind_resume() at insn %d reached without an exception in flight\n",
+ *insn_idx);
+ return -EINVAL;
+ }
+ if (!state->curframe)
+ return unwind_finish(env);
+ return unwind_step(env, unwind_pop_frame(env), insn_idx);
+}
+
+static int process_bpf_exit_full(struct bpf_verifier_env *env, bool *do_print_state)
{
struct bpf_func_state *cur_frame = cur_func(env);
@@ -18443,25 +18524,11 @@ static int process_bpf_exit_full(struct bpf_verifier_env *env,
* for which reference_state must match caller reference
* state when it exits.
*/
- int err = check_resource_leak(env, exception_exit,
- exception_exit || !env->cur_state->curframe,
- exception_exit ? "bpf_throw" :
+ int err = check_resource_leak(env, false, !env->cur_state->curframe,
"BPF_EXIT instruction in main prog");
if (err)
return err;
- /* The side effect of the prepare_func_exit which is
- * being skipped is that it frees bpf_func_state.
- * Typically, process_bpf_exit will only be hit with
- * outermost exit. copy_verifier_state in pop_stack will
- * handle freeing of any extra bpf_func_state left over
- * from not processing all nested function exits. We
- * also skip return code checks as they are not needed
- * for exceptional exits.
- */
- if (exception_exit)
- return PROCESS_BPF_EXIT;
-
if (env->cur_state->curframe) {
/* exit from nested function */
err = prepare_func_exit(env, &env->insn_idx);
@@ -18635,6 +18702,8 @@ static int do_check_insn(struct bpf_verifier_env *env, bool *do_print_state)
env->jmps_processed++;
if (opcode == BPF_CALL) {
+ if (bpf_is_unwind_resume_kfunc(insn))
+ return process_cleanup_resume(env, &env->insn_idx);
if (env->cur_state->active_locks) {
if ((insn->src_reg == BPF_REG_0 &&
insn->imm != BPF_FUNC_spin_unlock &&
@@ -18668,7 +18737,7 @@ static int do_check_insn(struct bpf_verifier_env *env, bool *do_print_state)
env->insn_idx += insn->imm + 1;
return INSN_IDX_UPDATED;
} else if (opcode == BPF_EXIT) {
- return process_bpf_exit_full(env, do_print_state, false);
+ return process_bpf_exit_full(env, do_print_state);
}
return check_cond_jmp_op(env, insn, &env->insn_idx);
}
@@ -21638,6 +21707,11 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
if (ret < 0)
goto skip_full_check;
+ /* The CFG needs an edge from a call in a cleanup range to its pad. */
+ ret = bpf_prepare_cleanup_exceptions(env);
+ if (ret < 0)
+ goto skip_full_check;
+
/* Validate instructions and resolve the program's referenced resources. */
ret = check_and_resolve_insns(env);
if (ret < 0)
@@ -21658,6 +21732,10 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
if (ret < 0)
goto skip_full_check;
+ ret = bpf_check_cleanup_exceptions(env);
+ if (ret < 0)
+ goto skip_full_check;
+
ret = bpf_compute_postorder(env);
if (ret < 0)
goto skip_full_check;
@@ -21845,6 +21923,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
kvfree(env->succ);
kvfree(env->gotox_tmp_buf);
bpf_diag_free(env);
+ kvfree(env->cleanup_info);
kvfree(env);
return ret;
}
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 732b35cc08d1c..f7dc121be0947 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -1669,6 +1669,9 @@ union bpf_attr {
* verification.
*/
__s32 keyring_id;
+ __aligned_u64 cleanup_info; /* exception cleanup table */
+ __u32 cleanup_info_rec_size; /* userspace bpf_cleanup_info size */
+ __u32 cleanup_info_cnt; /* number of bpf_cleanup_info records */
};
struct { /* anonymous struct used by BPF_OBJ_* commands */
@@ -7588,6 +7591,12 @@ struct bpf_line_info {
__u32 line_col;
};
+struct bpf_cleanup_info {
+ __u32 begin_off;
+ __u32 end_off;
+ __u32 landing_pad_off;
+};
+
struct bpf_spin_lock {
__u32 val;
};
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 96819c082c772..bcf490570961c 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -295,7 +295,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
const struct bpf_insn *insns, size_t insn_cnt,
struct bpf_prog_load_opts *opts)
{
- const size_t attr_sz = offsetofend(union bpf_attr, keyring_id);
+ const size_t attr_sz = offsetofend(union bpf_attr, cleanup_info_cnt);
void *finfo = NULL, *linfo = NULL;
const char *func_info, *line_info;
__u32 log_size, log_level, attach_prog_fd, attach_btf_obj_fd;
@@ -370,6 +370,10 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
attr.fd_array = ptr_to_u64(OPTS_GET(opts, fd_array, NULL));
attr.fd_array_cnt = OPTS_GET(opts, fd_array_cnt, 0);
+ attr.cleanup_info = ptr_to_u64(OPTS_GET(opts, cleanup_info, NULL));
+ attr.cleanup_info_cnt = OPTS_GET(opts, cleanup_info_cnt, 0);
+ attr.cleanup_info_rec_size = OPTS_GET(opts, cleanup_info_rec_size, 0);
+
if (log_level) {
attr.log_buf = ptr_to_u64(log_buf);
attr.log_size = log_size;
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 7534a593edae2..6f62a99e1e3e5 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -128,9 +128,14 @@ struct bpf_prog_load_opts {
/* if set, provides the length of fd_array */
__u32 fd_array_cnt;
+
+ /* exception cleanup table, from the .bpf_cleanup section */
+ const void *cleanup_info;
+ __u32 cleanup_info_cnt;
+ __u32 cleanup_info_rec_size;
size_t :0;
};
-#define bpf_prog_load_opts__last_field fd_array_cnt
+#define bpf_prog_load_opts__last_field cleanup_info_rec_size
LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type,
const char *prog_name, const char *license,
diff --git a/tools/lib/bpf/gen_loader.c b/tools/lib/bpf/gen_loader.c
index af3a04f161ac1..e9bb282a557a3 100644
--- a/tools/lib/bpf/gen_loader.c
+++ b/tools/lib/bpf/gen_loader.c
@@ -981,13 +981,15 @@ static void cleanup_relos(struct bpf_gen *gen, int insns)
cleanup_core_relo(gen);
}
-/* Convert func, line, and core relo info blobs to target endianness */
+/* Convert func, line, core relo and cleanup info blobs to target endianness */
static void info_blob_bswap(struct bpf_gen *gen, int func_info, int line_info,
- int core_relos, struct bpf_prog_load_opts *load_attr)
+ int core_relos, int cleanup_info,
+ struct bpf_prog_load_opts *load_attr)
{
struct bpf_func_info *fi = gen->data_start + func_info;
struct bpf_line_info *li = gen->data_start + line_info;
struct bpf_core_relo *cr = gen->data_start + core_relos;
+ struct bpf_cleanup_info *ci = gen->data_start + cleanup_info;
int i;
for (i = 0; i < load_attr->func_info_cnt; i++)
@@ -998,6 +1000,9 @@ static void info_blob_bswap(struct bpf_gen *gen, int func_info, int line_info,
for (i = 0; i < gen->core_relo_cnt; i++)
bpf_core_relo_bswap(cr++);
+
+ for (i = 0; i < load_attr->cleanup_info_cnt; i++)
+ bpf_cleanup_info_bswap(ci++);
}
void bpf_gen__prog_load(struct bpf_gen *gen,
@@ -1011,8 +1016,11 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
load_attr->line_info_rec_size;
int core_relo_tot_sz = gen->core_relo_cnt *
sizeof(struct bpf_core_relo);
+ int cleanup_info_tot_sz = load_attr->cleanup_info_cnt *
+ load_attr->cleanup_info_rec_size;
int prog_load_attr, license_off, insns_off, func_info, line_info, core_relos;
- int attr_size = offsetofend(union bpf_attr, core_relo_rec_size);
+ int attr_size = offsetofend(union bpf_attr, cleanup_info_cnt);
+ int cleanup_info;
union bpf_attr attr;
memset(&attr, 0, attr_size);
@@ -1061,9 +1069,17 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
core_relos, gen->core_relo_cnt,
sizeof(struct bpf_core_relo));
+ attr.cleanup_info_rec_size = tgt_endian(load_attr->cleanup_info_rec_size);
+ attr.cleanup_info_cnt = tgt_endian(load_attr->cleanup_info_cnt);
+ cleanup_info = add_data(gen, load_attr->cleanup_info, cleanup_info_tot_sz);
+ pr_debug("gen: prog_load: cleanup_info: off %d cnt %u rec size %u\n",
+ cleanup_info, load_attr->cleanup_info_cnt,
+ load_attr->cleanup_info_rec_size);
+
/* convert all info blobs to target endianness */
if (gen->swapped_endian && !gen->error)
- info_blob_bswap(gen, func_info, line_info, core_relos, load_attr);
+ info_blob_bswap(gen, func_info, line_info, core_relos, cleanup_info,
+ load_attr);
libbpf_strlcpy(attr.prog_name, prog_name, sizeof(attr.prog_name));
prog_load_attr = add_data(gen, &attr, attr_size);
@@ -1085,6 +1101,9 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
/* populate union bpf_attr with a pointer to core_relos */
emit_rel_store(gen, attr_field(prog_load_attr, core_relos), core_relos);
+ /* populate union bpf_attr with a pointer to cleanup_info */
+ emit_rel_store(gen, attr_field(prog_load_attr, cleanup_info), cleanup_info);
+
/* populate union bpf_attr fd_array with a pointer to data where map_fds are saved */
emit_rel_store(gen, attr_field(prog_load_attr, fd_array), gen->fd_array);
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 613afae265199..ebb21c9cdfe08 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -514,6 +514,11 @@ struct bpf_program {
void *line_info;
__u32 line_info_rec_size;
__u32 line_info_cnt;
+
+ struct bpf_cleanup_info *cleanup_info;
+ __u32 cleanup_info_rec_size;
+ __u32 cleanup_info_cnt;
+
__u32 prog_flags;
__u8 hash[SHA256_DIGEST_LENGTH];
@@ -549,6 +554,7 @@ struct bpf_struct_ops {
#define STRUCT_OPS_SEC ".struct_ops"
#define STRUCT_OPS_LINK_SEC ".struct_ops.link"
#define ARENA_SEC ".addr_space.1"
+#define CLEANUP_SEC ".bpf_cleanup"
enum libbpf_map_type {
LIBBPF_MAP_UNSPEC,
@@ -677,6 +683,25 @@ struct elf_sec_desc {
Elf_Data *data;
};
+#define CLEANUP_REC_FIELDS (sizeof(struct bpf_cleanup_info) / sizeof(__u32))
+
+/* Index of each field of struct bpf_cleanup_info, read as an array of __u32. */
+enum {
+ CLEANUP_REC_BEGIN,
+ CLEANUP_REC_END,
+ CLEANUP_REC_PAD,
+};
+
+/* One (begin, end, landing_pad) triple from .bpf_cleanup, with each field
+ * resolved from its relocation to an ELF section plus a section-relative
+ * instruction index. The mapping to final program instruction indices can only
+ * happen after subprogram placement, which differs per main program.
+ */
+struct cleanup_raw_rec {
+ int sec_idx[CLEANUP_REC_FIELDS];
+ size_t insn_idx[CLEANUP_REC_FIELDS];
+};
+
struct elf_state {
int fd;
const void *obj_buf;
@@ -696,6 +721,8 @@ struct elf_state {
bool has_st_ops;
int arena_data_shndx;
int jumptables_data_shndx;
+ Elf_Data *cleanup_data;
+ int cleanup_shndx;
};
struct usdt_manager;
@@ -771,6 +798,9 @@ struct bpf_object {
void *jumptables_data;
size_t jumptables_data_sz;
+ struct cleanup_raw_rec *cleanup_recs;
+ size_t cleanup_rec_cnt;
+
struct {
struct bpf_program *prog;
unsigned int sym_off;
@@ -817,7 +847,10 @@ static void bpf_program__exit(struct bpf_program *prog)
zfree(&prog->sec_name);
zfree(&prog->insns);
zfree(&prog->reloc_desc);
+ zfree(&prog->cleanup_info);
+ prog->cleanup_info_rec_size = 0;
+ prog->cleanup_info_cnt = 0;
prog->nr_reloc = 0;
prog->insns_cnt = 0;
prog->sec_idx = -1;
@@ -1554,6 +1587,7 @@ static struct bpf_object *bpf_object__new(const char *path,
obj->efile.obj_buf = obj_buf;
obj->efile.obj_buf_sz = obj_buf_sz;
obj->efile.btf_maps_shndx = -1;
+ obj->efile.cleanup_shndx = -1;
obj->kconfig_map_idx = -1;
obj->arena_map_idx = -1;
@@ -4040,6 +4074,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
sec_desc->shdr = sh;
sec_desc->data = data;
obj->efile.has_st_ops = true;
+ } else if (strcmp(name, CLEANUP_SEC) == 0) {
+ obj->efile.cleanup_data = data;
+ obj->efile.cleanup_shndx = idx;
} else if (strcmp(name, ARENA_SEC) == 0) {
obj->efile.arena_data = data;
obj->efile.arena_data_shndx = idx;
@@ -4067,6 +4104,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
strcmp(name, ".rel" STRUCT_OPS_LINK_SEC) &&
strcmp(name, ".rel?" STRUCT_OPS_SEC) &&
strcmp(name, ".rel?" STRUCT_OPS_LINK_SEC) &&
+ strcmp(name, ".rel" CLEANUP_SEC) &&
strcmp(name, ".rel" MAPS_ELF_SEC)) {
pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n",
idx, name, targ_sec_idx,
@@ -4847,6 +4885,214 @@ static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj,
return NULL;
}
+static int bpf_object__init_cleanup_info(struct bpf_object *obj)
+{
+ Elf_Data *data = obj->efile.cleanup_data;
+ Elf_Data *relo = NULL;
+ size_t i, nrels, nslots, nrecs;
+ struct cleanup_raw_rec *recs;
+ int *slot_sec, ret = 0;
+ size_t *slot_val;
+ const __u32 *vals;
+ bool native;
+
+ if (!data || obj->efile.cleanup_shndx < 0)
+ return 0;
+
+ native = is_native_endianness(obj);
+
+ for (i = 0; i < obj->efile.sec_cnt; i++) {
+ struct elf_sec_desc *sd = &obj->efile.secs[i];
+
+ if (sd->sec_type == SEC_RELO && sd->shdr &&
+ sd->shdr->sh_info == (Elf64_Word)obj->efile.cleanup_shndx) {
+ relo = sd->data;
+ break;
+ }
+ }
+ if (!relo) {
+ pr_warn("%s present without relocations\n", CLEANUP_SEC);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+ if (data->d_size % sizeof(struct bpf_cleanup_info)) {
+ pr_warn("%s size %zu is not a multiple of the record size %zu\n",
+ CLEANUP_SEC, data->d_size, sizeof(struct bpf_cleanup_info));
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+
+ vals = data->d_buf;
+ nslots = data->d_size / sizeof(__u32);
+ nrecs = data->d_size / sizeof(struct bpf_cleanup_info);
+
+ slot_sec = calloc(nslots, sizeof(*slot_sec));
+ slot_val = calloc(nslots, sizeof(*slot_val));
+ recs = calloc(nrecs ?: 1, sizeof(*recs));
+ if (!slot_sec || !slot_val || !recs) {
+ ret = -ENOMEM;
+ goto out;
+ }
+ for (i = 0; i < nslots; i++)
+ slot_sec[i] = -1;
+
+ /* One relocation per 4-byte field, naming the section it points into. */
+ nrels = relo->d_size / sizeof(Elf64_Rel);
+ for (i = 0; i < nrels; i++) {
+ Elf64_Rel *rel = elf_rel_by_idx(relo, i);
+ Elf64_Sym *sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info));
+ size_t type = ELF64_R_TYPE(rel->r_info);
+ size_t slot = rel->r_offset / sizeof(__u32);
+
+ if (type != R_BPF_64_NODYLD32 && type != R_BPF_64_ABS32) {
+ pr_warn("%s: relocation %zu has unexpected type %zu\n",
+ CLEANUP_SEC, i, type);
+ ret = -LIBBPF_ERRNO__FORMAT;
+ goto out;
+ }
+ if (!sym || slot >= nslots || rel->r_offset % sizeof(__u32)) {
+ pr_warn("%s: bad relocation %zu\n", CLEANUP_SEC, i);
+ ret = -LIBBPF_ERRNO__FORMAT;
+ goto out;
+ }
+ slot_sec[slot] = sym->st_shndx;
+ /* The addend lives in the section data, which libelf leaves in
+ * the object's byte order; a non-section symbol additionally
+ * contributes its own value.
+ */
+ slot_val[slot] = (native ? vals[slot] : bswap_32(vals[slot])) +
+ sym->st_value;
+ }
+
+ for (i = 0; i < nslots; i++) {
+ struct cleanup_raw_rec *rec = &recs[i / CLEANUP_REC_FIELDS];
+ size_t field = i % CLEANUP_REC_FIELDS;
+
+ if (slot_sec[i] < 0) {
+ pr_warn("%s: field %zu has no relocation\n", CLEANUP_SEC, i);
+ ret = -LIBBPF_ERRNO__FORMAT;
+ goto out;
+ }
+ if (slot_val[i] % BPF_INSN_SZ) {
+ pr_warn("%s: field %zu offset %zu is not instruction aligned\n",
+ CLEANUP_SEC, i, slot_val[i]);
+ ret = -LIBBPF_ERRNO__FORMAT;
+ goto out;
+ }
+ rec->sec_idx[field] = slot_sec[i];
+ rec->insn_idx[field] = slot_val[i] / BPF_INSN_SZ;
+ }
+
+ obj->cleanup_recs = recs;
+ obj->cleanup_rec_cnt = nrecs;
+ recs = NULL;
+out:
+ free(recs);
+ free(slot_val);
+ free(slot_sec);
+ return ret;
+}
+
+static int cmp_cleanup_info(const void *a, const void *b)
+{
+ const struct bpf_cleanup_info *x = a, *y = b;
+
+ if (x->begin_off == y->begin_off)
+ return 0;
+ return x->begin_off < y->begin_off ? -1 : 1;
+}
+
+static int bpf_prog_collect_cleanup_info(struct bpf_object *obj,
+ struct bpf_program *prog)
+{
+ size_t i;
+ int j;
+
+ for (i = 0; i < obj->cleanup_rec_cnt; i++) {
+ struct cleanup_raw_rec *raw = &obj->cleanup_recs[i];
+ struct bpf_program *owner = NULL;
+ struct bpf_cleanup_info ci = {};
+ __u32 *fields = (__u32 *)&ci;
+ void *tmp;
+
+ for (j = 0; j < CLEANUP_REC_FIELDS; j++) {
+ size_t idx = raw->insn_idx[j], final;
+ struct bpf_program *p;
+
+ /* The end of a range is exclusive, so it may name the
+ * instruction just past the last one of a function,
+ * which belongs to the next function or to nothing at
+ * all. Ask about the last instruction the range covers,
+ * the way the kernel does.
+ */
+ if (j == CLEANUP_REC_END) {
+ if (!idx) {
+ pr_warn("%s: record %zu is an empty range\n",
+ CLEANUP_SEC, i);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+ idx--;
+ }
+
+ p = find_prog_by_sec_insn(obj, raw->sec_idx[j], idx);
+ if (!p) {
+ pr_warn("%s: record %zu field %d is not inside a function\n",
+ CLEANUP_SEC, i, j);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+ if (!owner) {
+ owner = p;
+ } else if (owner != p) {
+ pr_warn("%s: record %zu spans functions '%s' and '%s'\n",
+ CLEANUP_SEC, i, owner->name, p->name);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+
+ if (owner == prog) {
+ final = raw->insn_idx[j] - prog->sec_insn_off;
+ } else if (prog_is_subprog(obj, owner) && owner->sub_insn_off) {
+ /* sub_insn_off is where this subprogram was
+ * appended to the main program being relocated;
+ * zero means it is not part of it.
+ */
+ final = owner->sub_insn_off +
+ raw->insn_idx[j] - owner->sec_insn_off;
+ } else {
+ owner = NULL;
+ break;
+ }
+ fields[j] = final;
+ }
+ if (!owner)
+ continue;
+
+ tmp = libbpf_reallocarray(prog->cleanup_info, prog->cleanup_info_cnt + 1,
+ sizeof(*prog->cleanup_info));
+ if (!tmp)
+ return -ENOMEM;
+ prog->cleanup_info = tmp;
+ prog->cleanup_info_rec_size = sizeof(struct bpf_cleanup_info);
+ prog->cleanup_info[prog->cleanup_info_cnt++] = ci;
+
+ pr_debug("prog '%s': cleanup region [%u,%u) -> landing pad %u\n",
+ prog->name, ci.begin_off, ci.end_off, ci.landing_pad_off);
+ }
+
+ qsort(prog->cleanup_info, prog->cleanup_info_cnt,
+ sizeof(*prog->cleanup_info), cmp_cleanup_info);
+ for (i = 1; i < prog->cleanup_info_cnt; i++) {
+ struct bpf_cleanup_info *prev = &prog->cleanup_info[i - 1];
+ struct bpf_cleanup_info *cur = &prog->cleanup_info[i];
+
+ if (cur->begin_off < prev->end_off) {
+ pr_warn("prog '%s': overlapping cleanup regions [%u,%u) and [%u,%u)\n",
+ prog->name, prev->begin_off, prev->end_off,
+ cur->begin_off, cur->end_off);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+ }
+
+ return 0;
+}
+
static int
bpf_object__collect_prog_relos(struct bpf_object *obj, Elf64_Shdr *shdr, Elf_Data *data)
{
@@ -7556,6 +7802,13 @@ static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_pat
return err;
}
}
+
+ err = bpf_prog_collect_cleanup_info(obj, prog);
+ if (err) {
+ pr_warn("prog '%s': failed to collect cleanup info: %s\n",
+ prog->name, errstr(err));
+ return err;
+ }
}
for (i = 0; i < obj->nr_programs; i++) {
prog = &obj->programs[i];
@@ -7746,6 +7999,9 @@ static int bpf_object__collect_relos(struct bpf_object *obj)
return -LIBBPF_ERRNO__INTERNAL;
}
+ if (idx == obj->efile.cleanup_shndx)
+ continue;
+
if (obj->efile.secs[idx].sec_type == SEC_ST_OPS)
err = bpf_object__collect_st_ops_relos(obj, shdr, data);
else if (idx == obj->efile.btf_maps_shndx)
@@ -8018,6 +8274,11 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
load_attr.line_info_rec_size = prog->line_info_rec_size;
load_attr.line_info_cnt = prog->line_info_cnt;
}
+ if (prog->cleanup_info_cnt) {
+ load_attr.cleanup_info = prog->cleanup_info;
+ load_attr.cleanup_info_cnt = prog->cleanup_info_cnt;
+ load_attr.cleanup_info_rec_size = prog->cleanup_info_rec_size;
+ }
load_attr.log_level = log_level;
load_attr.prog_flags = prog->prog_flags;
load_attr.fd_array = obj->fd_array;
@@ -8346,6 +8607,15 @@ static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_s
}
}
+/* LLVM terminates a cleanup landing pad with a call to _Unwind_Resume, the
+ * base unwind ABI's entry point for carrying an unwind on once a frame's
+ * cleanups have run. The kernel knows it as bpf_unwind_resume.
+ */
+static const char *kern_extern_name(const char *name)
+{
+ return strcmp(name, "_Unwind_Resume") ? name : "bpf_unwind_resume";
+}
+
static int bpf_program_record_relos(struct bpf_program *prog)
{
struct bpf_object *obj = prog->obj;
@@ -8362,12 +8632,12 @@ static int bpf_program_record_relos(struct bpf_program *prog)
continue;
kind = btf_is_var(btf__type_by_id(obj->btf, ext->btf_id)) ?
BTF_KIND_VAR : BTF_KIND_FUNC;
- bpf_gen__record_extern(obj->gen_loader, ext->name,
+ bpf_gen__record_extern(obj->gen_loader, kern_extern_name(ext->name),
ext->is_weak, !ext->ksym.type_id,
true, kind, relo->insn_idx);
break;
case RELO_EXTERN_CALL:
- bpf_gen__record_extern(obj->gen_loader, ext->name,
+ bpf_gen__record_extern(obj->gen_loader, kern_extern_name(ext->name),
ext->is_weak, false, false, BTF_KIND_FUNC,
relo->insn_idx);
break;
@@ -8565,6 +8835,7 @@ static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf,
err = err ? : bpf_object__init_maps(obj, opts);
err = err ? : bpf_object_init_progs(obj, opts);
err = err ? : bpf_object__collect_relos(obj);
+ err = err ? : bpf_object__init_cleanup_info(obj);
if (err)
goto out;
@@ -8807,17 +9078,19 @@ static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
struct module_btf *mod_btf = NULL;
const struct btf_type *kern_func;
struct btf *kern_btf = NULL;
+ const char *kern_name;
int ret;
local_func_proto_id = ext->ksym.type_id;
- kfunc_id = find_ksym_btf_id(obj, ext->essent_name ?: ext->name, BTF_KIND_FUNC, &kern_btf,
- &mod_btf);
+ kern_name = kern_extern_name(ext->essent_name ?: ext->name);
+
+ kfunc_id = find_ksym_btf_id(obj, kern_name, BTF_KIND_FUNC, &kern_btf, &mod_btf);
if (kfunc_id < 0) {
if (kfunc_id == -ESRCH && ext->is_weak)
return 0;
pr_warn("extern (func ksym) '%s': not found in kernel or module BTFs\n",
- ext->name);
+ strcmp(kern_name, "bpf_unwind_resume") ? ext->name : kern_name);
return kfunc_id;
}
@@ -9676,6 +9949,9 @@ void bpf_object__close(struct bpf_object *obj)
zfree(&obj->jumptables_data);
obj->jumptables_data_sz = 0;
+ zfree(&obj->cleanup_recs);
+ obj->cleanup_rec_cnt = 0;
+
for (i = 0; i < obj->jumptable_map_cnt; i++)
close(obj->jumptable_maps[i].fd);
zfree(&obj->jumptable_maps);
@@ -10068,6 +10344,25 @@ int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_opts
attr.line_info_rec_size = info ? info_rec_size : prog->line_info_rec_size;
}
+ /* exception cleanup table */
+ info = OPTS_GET(opts, cleanup_info, NULL);
+ info_cnt = OPTS_GET(opts, cleanup_info_cnt, 0);
+ info_rec_size = OPTS_GET(opts, cleanup_info_rec_size, 0);
+ if (!!info != !!info_cnt || !!info != !!info_rec_size) {
+ pr_warn("prog '%s': cleanup_info, cleanup_info_cnt, and cleanup_info_rec_size must all be specified or all omitted\n",
+ prog->name);
+ return libbpf_err(-EINVAL);
+ }
+ if (info) {
+ attr.cleanup_info = info;
+ attr.cleanup_info_cnt = info_cnt;
+ attr.cleanup_info_rec_size = info_rec_size;
+ } else if (prog->cleanup_info_cnt) {
+ attr.cleanup_info = prog->cleanup_info;
+ attr.cleanup_info_cnt = prog->cleanup_info_cnt;
+ attr.cleanup_info_rec_size = prog->cleanup_info_rec_size;
+ }
+
/* Logging is caller-controlled; no fallback to prog/obj log settings */
attr.log_buf = OPTS_GET(opts, log_buf, NULL);
attr.log_size = OPTS_GET(opts, log_size, 0);
diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
index cb4d962338441..78519f24fb404 100644
--- a/tools/lib/bpf/libbpf_internal.h
+++ b/tools/lib/bpf/libbpf_internal.h
@@ -56,6 +56,9 @@
#ifndef R_BPF_64_ABS32
#define R_BPF_64_ABS32 3
#endif
+#ifndef R_BPF_64_NODYLD32
+#define R_BPF_64_NODYLD32 4
+#endif
#ifndef R_BPF_64_32
#define R_BPF_64_32 10
#endif
@@ -569,6 +572,13 @@ static inline void bpf_core_relo_bswap(struct bpf_core_relo *i)
i->kind = bswap_32(i->kind);
}
+static inline void bpf_cleanup_info_bswap(struct bpf_cleanup_info *i)
+{
+ i->begin_off = bswap_32(i->begin_off);
+ i->end_off = bswap_32(i->end_off);
+ i->landing_pad_off = bswap_32(i->landing_pad_off);
+}
+
enum btf_field_iter_kind {
BTF_FIELD_ITER_IDS,
BTF_FIELD_ITER_STRS,
diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c
index 78f92c39290af..e5c06023cb5b1 100644
--- a/tools/lib/bpf/linker.c
+++ b/tools/lib/bpf/linker.c
@@ -1036,7 +1036,8 @@ static int linker_sanity_check_elf_relos(struct src_obj *obj, struct src_sec *se
size_t sym_type = ELF64_R_TYPE(relo->r_info);
if (sym_type != R_BPF_64_64 && sym_type != R_BPF_64_32 &&
- sym_type != R_BPF_64_ABS64 && sym_type != R_BPF_64_ABS32) {
+ sym_type != R_BPF_64_ABS64 && sym_type != R_BPF_64_ABS32 &&
+ sym_type != R_BPF_64_NODYLD32) {
pr_warn("ELF relo #%d in section #%zu has unexpected type %zu in %s\n",
i, sec->sec_idx, sym_type, obj->filename);
return -EINVAL;
@@ -2274,6 +2275,22 @@ static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *ob
insn->imm += sec->dst_off / sizeof(struct bpf_insn);
else
insn->imm += sec->dst_off;
+ } else if (sym_type == R_BPF_64_NODYLD32 ||
+ sym_type == R_BPF_64_ABS32) {
+ __u32 *val;
+
+ /* Two spellings of the one thing: LLVM
+ * emits NODYLD32 for a .long against a
+ * section symbol, GNU as emits ABS32
+ * (bpf_reloc_type_lookup() maps
+ * BFD_RELOC_32 to it), and the value
+ * they describe is the same.
+ */
+ val = dst_linked_sec->raw_data + dst_rel->r_offset;
+ if (linker->swapped_endian)
+ *val = bswap_32(bswap_32(*val) + sec->dst_off);
+ else
+ *val += sec->dst_off;
} else {
pr_warn("relocation against STT_SECTION in non-exec section is not supported!\n");
return -EINVAL;
diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 7ea5ba1df29e3..981cc26430495 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -523,7 +523,7 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \
LSKELS := fexit_sleep.c trace_printk.c trace_vprintk.c map_ptr_kern.c \
core_kern.c core_kern_overflow.c test_ringbuf.c \
test_ringbuf_n.c test_ringbuf_map_key.c test_ringbuf_write.c \
- test_ringbuf_overwrite.c
+ test_ringbuf_overwrite.c exceptions_cleanup_light.c
LSKELS_SIGNED := fentry_test.c fexit_test.c atomics.c
diff --git a/tools/testing/selftests/bpf/exceptions_cleanup.h b/tools/testing/selftests/bpf/exceptions_cleanup.h
new file mode 100644
index 0000000000000..0c088d96ca013
--- /dev/null
+++ b/tools/testing/selftests/bpf/exceptions_cleanup.h
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#ifndef __EXCEPTIONS_CLEANUP_H__
+#define __EXCEPTIONS_CLEANUP_H__
+
+#define THROW_COOKIE 0x100
+#define INNER_COOKIE 0x200
+
+/* progs/exceptions_cleanup.c: one bit per frame that reports it ran. */
+#define RAN_FOO3_PREEMPT 0x1
+#define RAN_FOO2_RCU 0x2
+#define RAN_FOO1V_PREEMPT 0x4
+#define RAN_FOO2_DROP 0x8
+#define RAN_BUMP 0x10
+
+/* progs/exceptions_cleanup_shapes.c: one bit per shape, numbered its own way. */
+#define RAN_SWEEP 0x1
+#define RAN_SHARED 0x2
+#define RAN_REGS 0x4
+#define RAN_TAIL_CALL 0x8
+#define RAN_MAIN_PAD 0x10
+#define RAN_TC_TAKEN 0x20
+#define RAN_FREPLACE 0x40
+#define RAN_ADDR_TAKEN 0x80
+#define RAN_NO_SUBPROG 0x100
+#define RAN_PAD_CALLS 0x200
+#define RAN_PAD_FIRST 0x400
+#define RAN_WIDE_REC 0x800
+#define RAN_PAD_STACK 0x1000
+#define RAN_DEEP_PAD 0x2000
+#define RAN_NOUNWIND_REC 0x4000
+#define RAN_RESUME_ALIAS 0x8000
+#define RAN_PAD_TAIL_CALL 0x10000
+
+/* progs/exceptions_cleanup_light.c: the one pad it has. */
+#define RAN_LIGHT 0x1
+
+#define CLEANUP_REC(begin, end, landing_pad) \
+ ".pushsection .bpf_cleanup,\"a\",@progbits;" \
+ ".long " begin ";" \
+ ".long " end ";" \
+ ".long " landing_pad ";" \
+ ".popsection;"
+
+/* Set a bit in @pads_ran. */
+#define PAD_RAN(bit) \
+ "r1 = %[pads_ran] ll;" \
+ "r2 = *(u64 *)(r1 + 0);" \
+ "r2 |= " bit ";" \
+ "*(u64 *)(r1 + 0) = r2;"
+
+#endif /* __EXCEPTIONS_CLEANUP_H__ */
diff --git a/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
new file mode 100644
index 0000000000000..ad0ff949f10df
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
@@ -0,0 +1,421 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <test_progs.h>
+#include "exceptions_cleanup.h"
+#include "exceptions_cleanup.skel.h"
+#include "exceptions_cleanup_fail.skel.h"
+#include "exceptions_cleanup_shapes.skel.h"
+#include "exceptions_cleanup_freplace.skel.h"
+#include "exceptions_cleanup_pad_freplace.skel.h"
+#include "exceptions_cleanup_ext_table.skel.h"
+#include "exceptions_cleanup_light.lskel.h"
+
+/* foo3 threw: every frame that has a pad ran it. */
+#define PADS_FOO3_THREW \
+ (RAN_FOO3_PREEMPT | RAN_FOO2_RCU | RAN_FOO1V_PREEMPT | RAN_FOO2_DROP)
+
+/* foo2 threw after foo3 returned normally: foo3's pad must not run. */
+#define PADS_FOO2_THREW \
+ (RAN_FOO2_RCU | RAN_FOO1V_PREEMPT | RAN_FOO2_DROP)
+
+static void run(struct exceptions_cleanup *skel, __u64 input, __u32 retval,
+ __u64 pads)
+{
+ __u64 ctx = 0;
+ int err;
+
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .ctx_in = &ctx,
+ .ctx_size_in = sizeof(ctx),
+ );
+
+ skel->bss->input = input;
+ skel->bss->pads_ran = 0;
+ skel->bss->result = 0;
+
+ err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.entry), &topts);
+ if (!ASSERT_OK(err, "run"))
+ return;
+ ASSERT_EQ(topts.retval, retval, "retval");
+ ASSERT_EQ(skel->bss->pads_ran, pads | RAN_BUMP, "pads_ran");
+}
+
+static void run_shape(struct exceptions_cleanup_shapes *skel, struct bpf_program *prog,
+ __u64 input, __u32 retval, __u64 pads)
+{
+ __u64 ctx = 0;
+ int err;
+
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .ctx_in = &ctx,
+ .ctx_size_in = sizeof(ctx),
+ );
+
+ skel->bss->input = input;
+ skel->bss->pads_ran = 0;
+
+ err = bpf_prog_test_run_opts(bpf_program__fd(prog), &topts);
+ if (!ASSERT_OK(err, "run"))
+ return;
+ ASSERT_EQ(topts.retval, retval, "retval");
+ ASSERT_EQ(skel->bss->pads_ran, pads, "pads_ran");
+}
+
+static void test_freplace(struct exceptions_cleanup_shapes *skel)
+{
+ struct exceptions_cleanup_freplace *fr;
+ struct bpf_link *link;
+ int tgt_fd;
+
+ tgt_fd = bpf_program__fd(skel->progs.entry_freplace);
+
+ fr = exceptions_cleanup_freplace__open();
+ if (!ASSERT_OK_PTR(fr, "freplace open"))
+ return;
+
+ if (!ASSERT_OK(bpf_program__set_attach_target(fr->progs.new_fr_callee,
+ tgt_fd, "fr_callee"),
+ "set_attach_target"))
+ goto out;
+ if (!ASSERT_OK(exceptions_cleanup_freplace__load(fr), "freplace load"))
+ goto out;
+
+ link = bpf_program__attach_freplace(fr->progs.new_fr_callee, tgt_fd,
+ "fr_callee");
+ if (!ASSERT_OK_PTR(link, "attach_freplace"))
+ goto out;
+
+ run_shape(skel, skel->progs.entry_freplace, 101, THROW_COOKIE, 0);
+ bpf_link__destroy(link);
+out:
+ exceptions_cleanup_freplace__destroy(fr);
+}
+
+static void test_pad_calls_freplace(struct exceptions_cleanup_shapes *skel)
+{
+ struct exceptions_cleanup_pad_freplace *fr;
+ struct bpf_link *link;
+ __u64 ctx = 0;
+ int tgt_fd, err;
+
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .ctx_in = &ctx,
+ .ctx_size_in = sizeof(ctx),
+ );
+
+ tgt_fd = bpf_program__fd(skel->progs.entry_pad_calls);
+
+ fr = exceptions_cleanup_pad_freplace__open();
+ if (!ASSERT_OK_PTR(fr, "pad freplace open"))
+ return;
+
+ if (!ASSERT_OK(bpf_program__set_attach_target(fr->progs.new_pad_callee,
+ tgt_fd, "pad_callee"),
+ "set_attach_target"))
+ goto out;
+ if (!ASSERT_OK(exceptions_cleanup_pad_freplace__load(fr), "pad freplace load"))
+ goto out;
+
+ link = bpf_program__attach_freplace(fr->progs.new_pad_callee, tgt_fd,
+ "pad_callee");
+ if (!ASSERT_OK_PTR(link, "attach_freplace"))
+ goto out;
+
+ skel->bss->input = 101;
+ skel->bss->pads_ran = 0;
+ skel->bss->pad_runs = 0;
+
+ err = bpf_prog_test_run_opts(tgt_fd, &topts);
+ if (!ASSERT_OK(err, "run"))
+ goto out_link;
+
+ ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
+ ASSERT_EQ(skel->bss->pads_ran, RAN_PAD_CALLS, "pads_ran");
+ ASSERT_EQ(topts.retval, THROW_COOKIE, "retval");
+out_link:
+ bpf_link__destroy(link);
+out:
+ exceptions_cleanup_pad_freplace__destroy(fr);
+}
+
+static void test_ext_table(struct exceptions_cleanup_shapes *skel)
+{
+ struct exceptions_cleanup_ext_table *fr;
+ struct bpf_link *link;
+ int tgt_fd;
+
+ tgt_fd = bpf_program__fd(skel->progs.entry_freplace);
+
+ fr = exceptions_cleanup_ext_table__open();
+ if (!ASSERT_OK_PTR(fr, "ext table open"))
+ return;
+
+ if (!ASSERT_OK(bpf_program__set_attach_target(fr->progs.new_fr_callee,
+ tgt_fd, "fr_callee"),
+ "set_attach_target"))
+ goto out;
+ if (!ASSERT_OK(exceptions_cleanup_ext_table__load(fr), "ext table load"))
+ goto out;
+
+ link = bpf_program__attach_freplace(fr->progs.new_fr_callee, tgt_fd,
+ "fr_callee");
+ if (!ASSERT_OK_PTR(link, "attach_freplace"))
+ goto out;
+
+ fr->bss->ext_pad_ran = 0;
+ run_shape(skel, skel->progs.entry_freplace, 101, THROW_COOKIE, 0);
+ ASSERT_EQ(fr->bss->ext_pad_ran, 1, "ext_pad_ran");
+
+ bpf_link__destroy(link);
+out:
+ exceptions_cleanup_ext_table__destroy(fr);
+}
+
+static void test_light_skeleton(void)
+{
+ struct exceptions_cleanup_light_lskel *skel;
+ __u64 ctx = 0;
+ int err;
+
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .ctx_in = &ctx,
+ .ctx_size_in = sizeof(ctx),
+ );
+
+ skel = exceptions_cleanup_light_lskel__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "light open_and_load"))
+ return;
+
+ err = bpf_prog_test_run_opts(skel->progs.entry_light.prog_fd, &topts);
+ if (!ASSERT_OK(err, "run"))
+ goto out;
+ ASSERT_EQ(topts.retval, THROW_COOKIE, "retval");
+ ASSERT_EQ(skel->bss->pads_ran, RAN_LIGHT, "pads_ran");
+out:
+ exceptions_cleanup_light_lskel__destroy(skel);
+}
+
+static void test_shapes(void)
+{
+ struct exceptions_cleanup_shapes *skel;
+
+ skel = exceptions_cleanup_shapes__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "shapes open_and_load"))
+ return;
+
+ /* The frame loads at all only if everything unreachable in it went. */
+ if (test__start_subtest("sweep_no_throw"))
+ run_shape(skel, skel->progs.entry_sweep, 1, 0, 0);
+ if (test__start_subtest("sweep_throw"))
+ run_shape(skel, skel->progs.entry_sweep, 101, THROW_COOKIE, RAN_SWEEP);
+
+ /* The covered call unwinds to the pad; the uncovered one never does. */
+ if (test__start_subtest("shared_callee_no_throw"))
+ run_shape(skel, skel->progs.entry_shared, 1, 2, 0);
+ if (test__start_subtest("shared_callee_throw"))
+ run_shape(skel, skel->progs.entry_shared, 101, THROW_COOKIE, RAN_SHARED);
+
+ /* The pad only sets its bit if it got the frame's own r6-r9 back. */
+ if (test__start_subtest("pad_sees_callee_saved"))
+ run_shape(skel, skel->progs.entry_regs, 101, THROW_COOKIE, RAN_REGS);
+
+ /* Same check, with a tail-call-reachable callee: its spill moves. */
+ if (test__start_subtest("tail_call_no_throw"))
+ run_shape(skel, skel->progs.entry_tail_call, 1, 0, 0);
+ if (test__start_subtest("tail_call_throw"))
+ run_shape(skel, skel->progs.entry_tail_call, 101, THROW_COOKIE,
+ RAN_TAIL_CALL);
+
+ /* A region around a nounwind call: no pad dispatched, still loads. */
+ if (test__start_subtest("nounwind_region"))
+ run_shape(skel, skel->progs.entry_nounwind_rec, 1, 0, 0);
+
+ /* A pad in the main program's own frame, not in a subprogram. */
+ if (test__start_subtest("main_program_pad"))
+ run_shape(skel, skel->progs.entry_main_pad, 101, THROW_COOKIE,
+ RAN_MAIN_PAD);
+
+ /* The same call site either way: the subprogram's throw unwinds into
+ * this frame and runs its pad, an extension's stops at its own boundary.
+ */
+ if (test__start_subtest("freplace_subprog_throws"))
+ run_shape(skel, skel->progs.entry_freplace, 7, THROW_COOKIE,
+ RAN_FREPLACE);
+ if (test__start_subtest("freplace_extension_throws"))
+ test_freplace(skel);
+
+ /* A tail call that is taken: the walk ends at the target, so the cookie
+ * comes back from there and this frame's pad does not run.
+ */
+ if (test__start_subtest("tail_call_taken")) {
+ int key = 0, prog_fd = bpf_program__fd(skel->progs.tc_target);
+
+ if (ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel->maps.taken_table),
+ &key, &prog_fd, BPF_ANY),
+ "populate taken_table"))
+ run_shape(skel, skel->progs.entry_tail_taken, 101,
+ THROW_COOKIE, 0);
+ }
+
+ /* A throwing subprog named by a BPF_PSEUDO_FUNC no helper is handed: the
+ * callback check has to look at the bpf_loop(), not at the ld_imm64.
+ */
+ if (test__start_subtest("addr_taken_no_throw"))
+ run_shape(skel, skel->progs.entry_addr_taken, 1, 2, 0);
+ if (test__start_subtest("addr_taken_throw"))
+ run_shape(skel, skel->progs.entry_addr_taken, 101, THROW_COOKIE,
+ RAN_ADDR_TAKEN);
+
+ /* A record covering bpf_throw() itself rather than a call to a frame
+ * that throws: raised, caught up with and delivered in one frame.
+ */
+ if (test__start_subtest("no_subprog_no_throw"))
+ run_shape(skel, skel->progs.entry_no_subprog, 1, 0, 0);
+ if (test__start_subtest("no_subprog_throw"))
+ run_shape(skel, skel->progs.entry_no_subprog, 101, THROW_COOKIE,
+ RAN_NO_SUBPROG);
+
+ /* A pad that calls a subprogram; with a throwing extension in its place,
+ * the nested exception has to stop there, not restart this pad.
+ */
+ if (test__start_subtest("pad_calls_subprog")) {
+ skel->bss->pad_runs = 0;
+ run_shape(skel, skel->progs.entry_pad_calls, 101, THROW_COOKIE,
+ RAN_PAD_CALLS);
+ ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
+ }
+ if (test__start_subtest("pad_calls_throwing_extension"))
+ test_pad_calls_freplace(skel);
+
+ /* A covered throw the sweep leaves last, where the default exception
+ * callback is patched in; the pad's bit needs r6-r9 still spilled.
+ */
+ if (test__start_subtest("pad_before_throw"))
+ run_shape(skel, skel->progs.entry_pad_first, 101, THROW_COOKIE,
+ RAN_PAD_FIRST);
+
+ /* A region whose last instruction is a 16-byte one, so that end - 1
+ * names the half of it that is not an instruction.
+ */
+ if (test__start_subtest("region_ends_on_ldimm64"))
+ run_shape(skel, skel->progs.entry_wide_rec, 101, THROW_COOKIE,
+ RAN_WIDE_REC);
+
+ /* A pad that reloads from and writes to its own frame's stack, which a
+ * JIT addressing the frame through the stack pointer gets wrong.
+ */
+ if (test__start_subtest("pad_uses_own_frame"))
+ run_shape(skel, skel->progs.entry_pad_stack, 101, THROW_COOKIE,
+ RAN_PAD_STACK);
+
+ /* The same, with an uncovered frame between the throw and the pad. */
+ if (test__start_subtest("pad_two_frames_up"))
+ run_shape(skel, skel->progs.entry_deep_pad, 101, THROW_COOKIE,
+ RAN_DEEP_PAD);
+
+ /* An extension program with a cleanup table of its own. */
+ if (test__start_subtest("extension_carries_table"))
+ test_ext_table(skel);
+
+ /* A pad terminated by _Unwind_Resume, which libbpf maps onto the kfunc;
+ * every other program here calls bpf_unwind_resume directly.
+ */
+ if (test__start_subtest("resume_alias"))
+ run_shape(skel, skel->progs.entry_resume_alias, 101,
+ THROW_COOKIE, RAN_RESUME_ALIAS);
+
+ /* A pad that calls a subprogram which tail calls, array empty and then
+ * populated: the tail call releases only the callee's own prologue.
+ */
+ if (test__start_subtest("pad_callee_tail_call")) {
+ int key = 0, prog_fd = bpf_program__fd(skel->progs.pad_tc_target);
+
+ skel->bss->pad_tc_target_ran = 0;
+ skel->bss->pad_runs = 0;
+ run_shape(skel, skel->progs.entry_pad_tail_call, 101,
+ THROW_COOKIE, RAN_PAD_TAIL_CALL);
+ ASSERT_EQ(skel->bss->pad_tc_target_ran, 0, "target not run");
+ ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
+
+ if (ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel->maps.pad_tc_table),
+ &key, &prog_fd, BPF_ANY),
+ "populate pad_tc_table")) {
+ skel->bss->pad_runs = 0;
+ run_shape(skel, skel->progs.entry_pad_tail_call, 101,
+ THROW_COOKIE, RAN_PAD_TAIL_CALL);
+ ASSERT_EQ(skel->bss->pad_tc_target_ran, 1, "target ran");
+ ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
+ }
+ }
+
+ /* The same, into a target that carries a table and throws: that target
+ * is a boundary, so the outer pad runs once, not twice.
+ */
+ if (test__start_subtest("pad_callee_tail_call_throws")) {
+ int key = 0, prog_fd = bpf_program__fd(skel->progs.pad_tc_throw_target);
+
+ if (ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel->maps.pad_tc_table),
+ &key, &prog_fd, BPF_ANY),
+ "populate pad_tc_table")) {
+ skel->bss->pad_runs = 0;
+ skel->bss->tc_target_pad_runs = 0;
+ run_shape(skel, skel->progs.entry_pad_tail_call, 101,
+ THROW_COOKIE, RAN_PAD_TAIL_CALL);
+ /* The target cleaned up after itself, once. */
+ ASSERT_EQ(skel->bss->tc_target_pad_runs, 1,
+ "tc_target_pad_runs");
+ /* And the outer pad was not started over. */
+ ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
+ }
+ }
+
+ exceptions_cleanup_shapes__destroy(skel);
+}
+
+void test_exceptions_cleanup(void)
+{
+ char log[8192] = {};
+ LIBBPF_OPTS(bpf_object_open_opts, opts,
+ .kernel_log_buf = log,
+ .kernel_log_size = sizeof(log));
+ struct exceptions_cleanup *skel;
+ int err;
+
+ skel = exceptions_cleanup__open_opts(&opts);
+ if (!ASSERT_OK_PTR(skel, "open"))
+ return;
+
+ err = exceptions_cleanup__load(skel);
+ if (err) {
+ if (err == -EOPNOTSUPP &&
+ strstr(log, "exception cleanup needs a JIT that can dispatch landing pads"))
+ test__skip();
+ else if (!ASSERT_OK(err, "load"))
+ fprintf(stderr, "%s", log);
+ exceptions_cleanup__destroy(skel);
+ return;
+ }
+
+ /* No throw: foo3 returns 1 ^ 1 == 0, foo2 adds one, no pad runs. */
+ if (test__start_subtest("no_throw"))
+ run(skel, 1, 1, 0);
+
+ /* foo3 throws; every pad runs and the cookie is delivered at entry. */
+ if (test__start_subtest("throw_from_foo3"))
+ run(skel, 101, THROW_COOKIE, PADS_FOO3_THREW);
+
+ /* foo3 returns 2 ^ 1 == 3, so foo2 throws from its own second region;
+ * foo3's frame is long gone, so its pad must not run.
+ */
+ if (test__start_subtest("throw_from_foo2"))
+ run(skel, 2, THROW_COOKIE, PADS_FOO2_THREW);
+
+ exceptions_cleanup__destroy(skel);
+
+ if (test__start_subtest("light_skeleton"))
+ test_light_skeleton();
+
+ test_shapes();
+
+ RUN_TESTS(exceptions_cleanup_fail);
+}
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup.c
new file mode 100644
index 0000000000000..95199a2828faa
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup.c
@@ -0,0 +1,152 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include "exceptions_cleanup.h"
+
+static __used __noinline void __kfunc_btf_anchor(void)
+{
+ bpf_throw(0);
+ bpf_rcu_read_lock();
+ bpf_rcu_read_unlock();
+ bpf_preempt_disable();
+ bpf_preempt_enable();
+ bpf_unwind_resume();
+}
+
+__u64 input = 0;
+__u64 pads_ran = 0;
+__u64 result = 0;
+
+static __used __noinline __u64 foo3(__u64 x)
+{
+ bpf_preempt_disable();
+ if (x > 100)
+ asm volatile (
+ "r1 = %[cookie];"
+ "1:" "call bpf_throw;" /* cleanup region */
+ "2:"
+ "goto 3f;"
+ "4:" /* landing pad */
+ "r7 = r0;"
+ "call bpf_preempt_enable;"
+ PAD_RAN("%[ran]")
+ "r1 = r7;"
+ "call bpf_unwind_resume;"
+ "3:"
+ CLEANUP_REC("1b", "2b", "4b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_FOO3_PREEMPT),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+ bpf_preempt_enable();
+ return x ^ 1;
+}
+
+__u64 never = 0;
+
+static __used __naked __noinline void drop_glue(void)
+{
+ asm volatile (
+ PAD_RAN("%[ran]")
+ "exit;"
+ :
+ : [ran]"i"(RAN_FOO2_DROP), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+static __used __naked __noinline __u64 foo2(void)
+{
+ asm volatile (
+ "r6 = r1;"
+ "call bpf_rcu_read_lock;"
+ "r1 = r6;"
+"1:" "call foo3;" /* cleanup region #1 */
+"2:"
+ "r6 = r0;"
+ "if r6 == 0 goto 5f;"
+ "r1 = %[cookie];"
+"3:" "call bpf_throw;" /* cleanup region #2 */
+"4:"
+ "r0 = 0;"
+ "exit;"
+"5:"
+ "call bpf_rcu_read_unlock;"
+ "r0 = r6;"
+ "r0 += 1;"
+ "exit;"
+"6:" /* landing pad, shared by both regions */
+ "call drop_glue;"
+ "call bpf_rcu_read_unlock;"
+ PAD_RAN("%[ran_rcu]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "6b")
+ CLEANUP_REC("3b", "4b", "6b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran_rcu]"i"(RAN_FOO2_RCU),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+static __used __naked __noinline void foo1v(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call foo2;" /* cleanup region */
+"2:"
+ "r6 = r0;"
+ "call bpf_preempt_enable;"
+ "r1 = %[result] ll;"
+ "*(u64 *)(r1 + 0) = r6;"
+ "goto 7f;"
+"8:" /* landing pad */
+ "call bpf_preempt_enable;"
+ PAD_RAN("%[ran]")
+ "goto 9f;"
+"7:" /* the frame's own exit block */
+ "r0 = 0;"
+ "exit;"
+"9:" /* shared resume block */
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "8b")
+ :
+ : [ran]"i"(RAN_FOO1V_PREEMPT), __imm_addr(input),
+ __imm_addr(result), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+static __used __naked __noinline void bump(void)
+{
+ asm volatile (
+ PAD_RAN("%[ran]")
+ "r1 = %[never] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 == 0 goto 1f;"
+ "r1 = 0;"
+ "call bpf_throw;"
+"1:"
+ "exit;" /* r0 deliberately left alone */
+ :
+ : [ran]"i"(RAN_BUMP), __imm_addr(never), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+__noinline __u64 foo1(void)
+{
+ bump();
+ foo1v();
+ return result;
+}
+
+SEC("syscall")
+int entry(void *ctx)
+{
+ return foo1();
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_ext_table.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_ext_table.c
new file mode 100644
index 0000000000000..d14db48d6b29b
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_ext_table.c
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include "exceptions_cleanup.h"
+
+__u64 ext_pad_ran = 0;
+
+/* Without a 32-bit int in BTF, libbpf's dummy_ksym var gets type id 0. */
+int btf_int_anchor;
+
+static __used __noinline void __kfunc_btf_anchor(void)
+{
+ bpf_throw(0);
+ bpf_preempt_disable();
+ bpf_preempt_enable();
+ bpf_unwind_resume();
+}
+
+static __used __naked __noinline __u64 ext_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = %[cookie];"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "r1 = %[ext_pad_ran] ll;"
+ "r2 = 1;"
+ "*(u64 *)(r1 + 0) = r2;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [cookie]"i"(THROW_COOKIE), __imm_addr(ext_pad_ran)
+ : __clobber_all);
+}
+
+SEC("freplace/fr_callee")
+__u64 new_fr_callee(__u64 x)
+{
+ return ext_frame();
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_fail.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_fail.c
new file mode 100644
index 0000000000000..ce2dac306a84c
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_fail.c
@@ -0,0 +1,600 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_experimental.h"
+#include "bpf_misc.h"
+#include "../test_kmods/bpf_testmod_kfunc.h"
+#include "exceptions_cleanup.h"
+
+__u64 input = 0;
+
+static __used __noinline void __kfunc_btf_anchor(void)
+{
+ bpf_throw(0);
+ bpf_preempt_disable();
+ bpf_preempt_enable();
+ bpf_unwind_resume();
+}
+
+/*
+ * 1. A subprogram that may unwind, also used as a helper callback:
+ * bpf_loop()'s own kernel frame would end the walk before it found a
+ * boundary.
+ */
+static int throwing_cb(__u32 idx, void *ctx)
+{
+ bpf_throw(0xbad);
+ return 0;
+}
+
+static __used __naked __noinline __u64 cb_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+"1:" "call throwing_cb;" /* cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("may unwind and is used as a callback")
+int callback_may_unwind(void *ctx)
+{
+ bpf_loop(1, throwing_cb, NULL, 0);
+ return cb_frame();
+}
+
+/*
+ * 2. A landing pad that reaches both an unwind resume and a plain exit, so
+ * nothing says whether it is a cleanup pad or a catch pad.
+ */
+static __used __naked __noinline __u64 inner_throw(void)
+{
+ asm volatile (
+ "r1 = 1;"
+ "call bpf_throw;"
+ "r0 = 0;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+static __used __naked __noinline __u64 ambiguous_pad_frame(void)
+{
+ asm volatile (
+ "r6 = r1;"
+ "call bpf_preempt_disable;"
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad: two ways out */
+ "call bpf_preempt_enable;"
+ "if r6 > 10 goto 4f;"
+ "call bpf_unwind_resume;"
+ "exit;"
+"4:"
+ "r0 = 0;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("reaches both bpf_unwind_resume() and a plain exit")
+int ambiguous_landing_pad(void *ctx)
+{
+ return ambiguous_pad_frame();
+}
+
+/*
+ * 3. A throw from inside a landing pad: a second walk over the frames the
+ * first one is in the middle of discarding.
+ */
+static __used __naked __noinline __u64 throw_in_pad_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad that throws again */
+ "call bpf_preempt_enable;"
+ "r1 = 2;"
+ "call bpf_throw;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("can throw while an exception is in flight")
+int throw_from_landing_pad(void *ctx)
+{
+ return throw_in_pad_frame();
+}
+
+/*
+ * 4. A cleanup table in a program that also installs an exception callback,
+ * two different answers to what runs on the way out.
+ */
+__noinline int unused_exc_cb(u64 cookie)
+{
+ return 0;
+}
+
+static __used __naked __noinline __u64 cb_and_table_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = 9;"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__exception_cb(unused_exc_cb)
+__failure __msg("cannot be combined with an exception callback")
+int table_with_exception_cb(void *ctx)
+{
+ return cb_and_table_frame();
+}
+
+__u64 never;
+
+/*
+ * 5. A landing pad that calls a subprogram which can throw. Not case 3: the
+ * throw is in another subprogram, so what catches it is the walk of the pad's
+ * body, off subprog_info.might_throw.
+ */
+static __used __noinline void pad_callee_that_throws(void)
+{
+ if (never)
+ bpf_throw(0);
+}
+
+static __used __naked __noinline __u64 pad_calls_thrower_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = 11;"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call pad_callee_that_throws;" /* ...which can throw: refused */
+ "call bpf_preempt_enable;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("which can throw while an exception is in flight")
+int pad_calls_thrower(void *ctx)
+{
+ return pad_calls_thrower_frame();
+}
+
+/*
+ * 6. A catch pad: it ends in a plain exit rather than a resume, and a walker
+ * that calls pads as subroutines cannot hand a frame back its own execution.
+ */
+static __used __naked __noinline __u64 catch_pad_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = 12;"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* catch pad: no resume, it stops here */
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("is not supported yet, only cleanup pads that resume")
+int catch_landing_pad(void *ctx)
+{
+ return catch_pad_frame();
+}
+
+/*
+ * 7. An exception reaching the boundary of a program type that constrains
+ * its return value: delivery makes the cookie that return value, and fentry
+ * has to return 0.
+ */
+static __used __naked __noinline __u64 boundary_throw_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = 7;"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?fentry/bpf_fentry_test1")
+__failure __msg("the register R1 has smin=7 smax=7 should have been in [0, 0]")
+int boundary_delivers(void *ctx)
+{
+ return boundary_throw_frame();
+}
+
+/*
+ * 8. A bpf_unwind_resume() outside any landing pad. Both JITs lower it as
+ * the way back out of a pad, which in ordinary code leaves a live frame
+ * standing with its epilogue skipped.
+ */
+static __used __naked __noinline __u64 stray_resume_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = 13;"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("is not in an exception cleanup landing pad")
+int resume_outside_pad(void *ctx)
+{
+ /* Never taken, but reachable, which is all the verifier needs. */
+ if (never)
+ bpf_unwind_resume();
+ return stray_resume_frame();
+}
+
+/*
+ * 9. A bpf_unwind_resume() in a subprogram a landing pad calls. The
+ * verifier's walk cannot tell it from a resume in the pad itself -- an
+ * exception is in flight either way -- so the rule is static: a resume sits in
+ * a pad body.
+ */
+static __used __naked __noinline void resume_in_callee(void)
+{
+ asm volatile (
+ "call bpf_unwind_resume;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+static __used __naked __noinline __u64 pad_calls_resumer_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = 14;"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "call resume_in_callee;" /* ...which resumes: refused */
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("is not in an exception cleanup landing pad")
+int resume_in_pad_callee(void *ctx)
+{
+ return pad_calls_resumer_frame();
+}
+
+/*
+ * 10. A bpf_unwind_resume() in a program carrying no cleanup table, where
+ * that static rule does not run at all. do_check() refuses it on the state not
+ * unwinding, and has to: the JITs lower every one of these the same way.
+ */
+static __used __naked __noinline __u64 no_table_resume_frame(void)
+{
+ asm volatile (
+ "call bpf_unwind_resume;"
+ "r0 = 0;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("reached without an exception in flight")
+int resume_without_table(void *ctx)
+{
+ return no_table_resume_frame();
+}
+
+/*
+ * 11. A landing pad that is itself a covered call site, so an exception out
+ * of it would have nowhere to go. Hand-written only: LLVM sinks a function's
+ * pads past every range it emits.
+ */
+static __used __naked __noinline __u64 nested_pad_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+"1:" "call inner_throw;" /* first cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* first pad, second region's call */
+ "call bpf_preempt_enable;"
+"4:"
+ "call bpf_unwind_resume;"
+ "exit;"
+"5:" /* second pad */
+ "call bpf_preempt_enable;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ CLEANUP_REC("3b", "4b", "5b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("is inside the call-site range of")
+int nested_landing_pad(void *ctx)
+{
+ return nested_pad_frame();
+}
+
+/*
+ * 12. A tail call in a landing pad: it unwinds the prologue off the stack
+ * pointer, which in a pad is the walker's.
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} tc_map SEC(".maps");
+
+static __used __naked __noinline __u64 tail_call_pad_frame(void)
+{
+ asm volatile (
+ "r6 = r1;"
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r1 = r6;"
+ "r2 = %[tc_map] ll;"
+ "r3 = 0;"
+ "call %[bpf_tail_call];"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : __imm(bpf_tail_call), __imm_addr(tc_map)
+ : __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("is in an exception cleanup landing pad")
+int tail_call_in_pad(void *ctx)
+{
+ return tail_call_pad_frame();
+}
+
+#if defined(__BPF_FEATURE_STACK_ARGUMENT)
+
+/*
+ * 13. A call that passes an argument on the stack, in a landing pad: the
+ * outgoing area the callee reads is not the one the caller wrote, the frame
+ * being the unwinding one and the stack pointer the walker's.
+ */
+static __used __noinline __u64 six_args(__u64 a, __u64 b, __u64 c, __u64 d,
+ __u64 e, __u64 f)
+{
+ return a + b + c + d + e + f;
+}
+
+static __used __naked __noinline __u64 stack_arg_pad_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "r1 = 1;"
+ "r2 = 2;"
+ "r3 = 3;"
+ "r4 = 4;"
+ "r5 = 5;"
+ "*(u64 *)(r11 - 8) = 6;" /* the sixth argument */
+ "call six_args;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("on-stack call argument in an exception cleanup landing pad")
+int stack_arg_in_pad(void *ctx)
+{
+ return stack_arg_pad_frame();
+}
+
+/*
+ * 14. The same, reached the other way: a kfunc whose by-value argument runs
+ * past the five argument registers, where the JIT fills the outgoing area and
+ * the rule above has no store to catch. The C call gives the extern its BTF.
+ */
+static __used __noinline void __nofit_btf_anchor(void)
+{
+ struct prog_test_pair_arg s = {};
+
+ bpf_kfunc_call_test_pair_arg_nofit(1, 2, 3, 4, s);
+}
+
+static __used __naked __noinline __u64 kfunc_arg_pad_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "r1 = 1;"
+ "r2 = 2;"
+ "r3 = 3;"
+ "r4 = 4;"
+ "r5 = 5;"
+ "call bpf_kfunc_call_test_pair_arg_nofit;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("on-stack call argument in an exception cleanup landing pad")
+int kfunc_stack_arg_in_pad(void *ctx)
+{
+ return kfunc_arg_pad_frame();
+}
+
+#endif /* __BPF_FEATURE_STACK_ARGUMENT */
+
+/*
+ * 15. A landing pad entered by ordinary control flow, arriving with none of
+ * what the walker sets up. Nothing static sees it -- the resume really is in a
+ * pad body -- so do_check() refuses it on the state not unwinding.
+ */
+static __used __naked __noinline __u64 jump_into_pad_frame(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r6 = *(u64 *)(r1 + 0);"
+ "if r6 > 7 goto 4f;" /* an ordinary branch into the pad */
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r7 = r0;"
+"4:" /* ... and its second instruction */
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : __imm_addr(input)
+ : __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("reached without an exception in flight")
+int jump_into_pad(void *ctx)
+{
+ return jump_into_pad_frame();
+}
+
+#if defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)
+
+/*
+ * 16. A landing pad that reaches an indirect jump, which cannot be told from
+ * a catch pad. SEC("socket") because a jump table entry is an offset from the
+ * program's section symbol, and "?syscall" is not a name assembly can use.
+ */
+static __used __naked __noinline void gotox_thrower(void)
+{
+ asm volatile (
+ "r1 = 15;"
+ "call bpf_throw;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+SEC("socket")
+__failure __msg("reaches an indirect jump")
+__naked void gotox_in_pad(void)
+{
+ asm volatile (
+ ".pushsection .jumptables,\"\",@progbits;"
+"jt0_%=:"
+ ".quad l0_%= - socket;"
+ ".quad l1_%= - socket;"
+ ".size jt0_%=, 16;"
+ ".global jt0_%=;"
+ ".popsection;"
+
+"1:" "call gotox_thrower;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r1 = jt0_%= ll;"
+ "r1 += 8;"
+ "r2 = *(u64 *)(r1 + 0);"
+ /* gotox r2. Spelled as a raw insn on purpose: the "gotox" mnemonic
+ * only reached the LLVM assembler in llvm 22, and BPF_RAW_INSN()
+ * needs <linux/bpf.h>, which this file cannot have -- vmlinux.h
+ * already defines the uapi enums.
+ */
+ ".8byte 0x20d;"
+"l0_%=:"
+ "call bpf_unwind_resume;"
+ "exit;"
+"l1_%=:"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+#endif /* x86 || arm64 */
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_freplace.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_freplace.c
new file mode 100644
index 0000000000000..afb358fd3d405
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_freplace.c
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "exceptions_cleanup.h"
+
+/* Without a 32-bit int in BTF, libbpf's dummy_ksym var gets type id 0. */
+int btf_int_anchor;
+
+SEC("freplace/fr_callee")
+__u64 new_fr_callee(__u64 x)
+{
+ bpf_throw(THROW_COOKIE);
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_light.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_light.c
new file mode 100644
index 0000000000000..de7ae478d67f3
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_light.c
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include "exceptions_cleanup.h"
+
+static __used __noinline void __kfunc_btf_anchor(void)
+{
+ bpf_throw(0);
+ bpf_preempt_disable();
+ bpf_preempt_enable();
+ bpf_unwind_resume();
+}
+
+__u64 pads_ran = 0;
+
+SEC("syscall")
+__naked int entry_light(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = %[cookie];"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_LIGHT),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_pad_freplace.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_pad_freplace.c
new file mode 100644
index 0000000000000..eabac6baabb70
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_pad_freplace.c
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "exceptions_cleanup.h"
+
+/* Without a 32-bit int in BTF, libbpf's dummy_ksym var gets type id 0. */
+int btf_int_anchor;
+
+SEC("freplace/pad_callee")
+__u64 new_pad_callee(__u64 x)
+{
+ bpf_throw(INNER_COOKIE);
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c
new file mode 100644
index 0000000000000..f5eb2ff15c896
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c
@@ -0,0 +1,863 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include "exceptions_cleanup.h"
+
+#define PAD_COUNT \
+ "r1 = %[pad_runs] ll;" \
+ "r2 = *(u64 *)(r1 + 0);" \
+ "r2 += 1;" \
+ "*(u64 *)(r1 + 0) = r2;"
+
+static __used __noinline void __kfunc_btf_anchor(void)
+{
+ bpf_throw(0);
+ bpf_rcu_read_lock();
+ bpf_rcu_read_unlock();
+ bpf_preempt_disable();
+ bpf_preempt_enable();
+ bpf_unwind_resume();
+}
+
+__u64 input = 0;
+__u64 magic = 0x5eed;
+__u64 pads_ran = 0;
+__u64 pad_runs = 0;
+
+/*
+ * 1. Everything a cleanup table leaves dead: the continuation after a throw,
+ * the tail after a pad's resume, an ld_imm64 and a conditional branch inside
+ * that tail, and a block reached only by the dead continuation.
+ */
+static __used __naked __noinline __u64 sweep_frame(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r6 = *(u64 *)(r1 + 0);"
+ "call bpf_preempt_disable;"
+ "if r6 < 101 goto 6f;"
+ "r1 = %[cookie];"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "goto 3f;"
+"4:" /* landing pad */
+ "r7 = r0;"
+ "call bpf_preempt_enable;"
+ PAD_RAN("%[ran]")
+ "r1 = r7;"
+ "call bpf_unwind_resume;"
+ "r1 = %[pads_ran] ll;"
+ "r2 = *(u64 *)(r1 + 0);"
+ "if r2 == 0 goto 5f;"
+ "call bpf_preempt_enable;"
+ "r0 = 7;"
+ "exit;"
+"5:"
+ "r0 = 8;"
+ "exit;"
+"3:" /* dead: only the dead goto reaches it */
+ "r0 = 9;"
+ "exit;"
+"6:" /* live: the ordinary return */
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "4b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_SWEEP),
+ __imm_addr(input), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_sweep(void *ctx)
+{
+ return sweep_frame();
+}
+
+/*
+ * 2. A callee called from both a covered and an uncovered site: the pad is
+ * recorded on the call site, not on the callee. The lock sits between the two
+ * calls because the frame really would leak it if the uncovered call unwound.
+ */
+static __used __noinline __u64 shared_callee(__u64 x)
+{
+ if (x > 100)
+ bpf_throw(THROW_COOKIE);
+ return x + 1;
+}
+
+static __used __naked __noinline __u64 shared_frame(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r6 = *(u64 *)(r1 + 0);"
+ "r1 = 0;"
+ "call shared_callee;"
+ "call bpf_rcu_read_lock;"
+ "r1 = r6;"
+"1:" "call shared_callee;" /* cleanup region */
+"2:"
+ "r6 = r0;"
+ "call bpf_rcu_read_unlock;"
+ "r0 = r6;"
+ "exit;"
+"3:" /* landing pad */
+ "r7 = r0;"
+ "call bpf_rcu_read_unlock;"
+ PAD_RAN("%[ran]")
+ "r1 = r7;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_SHARED), __imm_addr(input), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_shared(void *ctx)
+{
+ return shared_frame();
+}
+
+/*
+ * 3. A landing pad that reads its frame's callee-saved registers, which only
+ * the spill in the discarded callee's prologue still holds. The callee fills
+ * r6-r9 with something else before it throws, so the pad's check passes only
+ * if the walker found that spill.
+ */
+#define LOAD_MAGIC_REGS \
+ "r1 = %[magic] ll;" \
+ "r6 = *(u64 *)(r1 + 0);" \
+ "r7 = r6;" \
+ "r7 += 1;" \
+ "r8 = r6;" \
+ "r8 += 2;" \
+ "r9 = r6;" \
+ "r9 += 3;"
+
+/* Set @bit only if r6-r9 still hold what LOAD_MAGIC_REGS put there. */
+#define CHECK_MAGIC_REGS(bit) \
+ "r1 = %[magic] ll;" \
+ "r2 = *(u64 *)(r1 + 0);" \
+ "if r6 != r2 goto 9f;" \
+ "r2 += 1;" \
+ "if r7 != r2 goto 9f;" \
+ "r2 += 1;" \
+ "if r8 != r2 goto 9f;" \
+ "r2 += 1;" \
+ "if r9 != r2 goto 9f;" \
+ PAD_RAN(bit) \
+ "9:"
+
+static __used __naked __noinline __u64 regs_thrower(void)
+{
+ asm volatile (
+ /* Not this frame's to keep, and that is the point. */
+ "r6 = 0xdead;"
+ "r7 = 0xbeef;"
+ "r8 = 0xcafe;"
+ "r9 = 0xf00d;"
+ "r1 = %[cookie];"
+ "call bpf_throw;"
+ "r0 = 0;"
+ "exit;"
+ :
+ : [cookie]"i"(THROW_COOKIE)
+ : __clobber_all);
+}
+
+static __used __naked __noinline __u64 regs_frame(void)
+{
+ asm volatile (
+ LOAD_MAGIC_REGS
+ "call bpf_preempt_disable;"
+"1:" "call regs_thrower;" /* cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ CHECK_MAGIC_REGS("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_REGS),
+ __imm_addr(magic), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_regs(void *ctx)
+{
+ return regs_frame();
+}
+
+/*
+ * 4. The same, with a tail-call-reachable callee: its prologue pushes the tail
+ * call counter between the program stack and the spill area, so the spill the
+ * walker reads moves. The array is left empty; being reachable is the point.
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} jmp_table SEC(".maps");
+
+static __used __noinline __u64 tc_thrower(void *ctx)
+{
+ /* Never taken; its presence is what makes this frame, whose spill the
+ * walker reads, tail-call-reachable.
+ */
+ bpf_tail_call_static(ctx, &jmp_table, 0);
+ asm volatile (
+ "r6 = 0xdead;"
+ "r7 = 0xbeef;"
+ "r8 = 0xcafe;"
+ "r9 = 0xf00d;"
+ "r1 = %[cookie];"
+ "call bpf_throw;"
+ :
+ : [cookie]"i"(THROW_COOKIE)
+ : __clobber_all);
+ return 0;
+}
+
+/*
+ * The frame with the pad is the program itself, and __naked: r1 holds the
+ * context at entry, which is the only place to get one for bpf_tail_call().
+ */
+SEC("syscall")
+__naked int entry_tail_call(void)
+{
+ asm volatile (
+ "*(u64 *)(r10 - 8) = r1;" /* the context, straight from entry */
+ LOAD_MAGIC_REGS
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 < 101 goto 8f;"
+ "r1 = *(u64 *)(r10 - 8);"
+"1:" "call tc_thrower;" /* cleanup region */
+"2:"
+"8:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ CHECK_MAGIC_REGS("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_TAIL_CALL), __imm_addr(input),
+ __imm_addr(magic), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 5. A landing pad in the main program's own frame. jit_subprogs() compiles it
+ * as func[0], but the ksym the walker finds is the outer bpf_prog's, so the
+ * table has to be handed over or the pad is never dispatched -- silently.
+ */
+SEC("syscall")
+__naked int entry_main_pad(void)
+{
+ asm volatile (
+ LOAD_MAGIC_REGS
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 < 101 goto 8f;"
+"1:" "call regs_thrower;" /* cleanup region */
+"2:"
+"8:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ CHECK_MAGIC_REGS("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_MAIN_PAD), __imm_addr(input),
+ __imm_addr(magic), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 6. A tail call that is really taken: the target is a program in its own
+ * right, so the walk ends there and this frame's pad does not run. The callee
+ * can also throw on a path never taken, which keeps the pad out of the sweep.
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} taken_table SEC(".maps");
+
+SEC("syscall")
+int tc_target(void *ctx)
+{
+ bpf_throw(THROW_COOKIE);
+ return 0;
+}
+
+static __used __noinline __u64 tc_taken_callee(void *ctx, __u64 x)
+{
+ /* Never true at run time; the verifier cannot know that, and its
+ * unwind out of here is what keeps the caller's pad alive.
+ */
+ if (x == 7)
+ bpf_throw(THROW_COOKIE);
+ bpf_tail_call_static(ctx, &taken_table, 0);
+ return 0;
+}
+
+SEC("syscall")
+__naked int entry_tail_taken(void)
+{
+ asm volatile (
+ "*(u64 *)(r10 - 8) = r1;" /* the context, straight from entry */
+ "r1 = %[input] ll;"
+ "r2 = *(u64 *)(r1 + 0);"
+ "if r2 < 101 goto 8f;"
+ "r1 = *(u64 *)(r10 - 8);"
+"1:" "call tc_taken_callee;" /* cleanup region */
+"2:"
+ "exit;" /* the cookie, delivered at tc_target */
+"8:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad: must not run */
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_TC_TAKEN), __imm_addr(input), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 7. An extension program over the callee of a covered call. The walk ends in
+ * the extension's frame, as it does for a tail call target, so the pad does
+ * not run; fr_callee() can also throw by itself, giving the same call site
+ * both answers.
+ */
+__noinline __u64 fr_callee(__u64 x)
+{
+ if (x == 7)
+ bpf_throw(THROW_COOKIE);
+ return x + 1;
+}
+
+SEC("syscall")
+__naked int entry_freplace(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call fr_callee;" /* cleanup region */
+"2:"
+ "exit;"
+"3:" /* landing pad */
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_FREPLACE), __imm_addr(input), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 8. A throwing subprogram named by a BPF_PSEUDO_FUNC on a path never taken.
+ * Handing one to a helper is what is refused, not naming it, so anything going
+ * by the ld_imm64 alone turns this program away.
+ */
+static __used __noinline int cb_thrower(__u32 idx, void *ctx)
+{
+ bpf_throw(THROW_COOKIE);
+ return 0;
+}
+
+static __used __noinline __u64 addr_taken_callee(__u64 x)
+{
+ if (x <= 100)
+ return x + 1;
+ bpf_throw(THROW_COOKIE);
+ return bpf_loop(1, cb_thrower, NULL, 0);
+}
+
+SEC("syscall")
+__naked int entry_addr_taken(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call addr_taken_callee;" /* cleanup region */
+"2:"
+ "exit;"
+"3:" /* landing pad */
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_ADDR_TAKEN), __imm_addr(input), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 9. A record that covers bpf_throw() itself: the frame that raises the
+ * exception is the frame the record covers and the boundary both, so the pad
+ * runs on the way to delivering the cookie out of the program it came from.
+ */
+SEC("syscall")
+__naked int entry_no_subprog(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 < 101 goto 8f;"
+ "call bpf_preempt_disable;"
+ "r1 = %[cookie];"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "exit;"
+"8:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_NO_SUBPROG),
+ __imm_addr(input), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 10. A landing pad that calls a subprogram an extension can replace. The
+ * load-time rule cannot see the extension coming, so what stops a nested
+ * exception is the walk, which ends in the extension's own frame. pad_runs
+ * says the pad ran once rather than twice.
+ */
+__noinline __u64 pad_callee(__u64 x)
+{
+ return x + 1;
+}
+
+static __used __noinline __u64 pc_thrower(__u64 x)
+{
+ if (x > 100)
+ bpf_throw(THROW_COOKIE);
+ return x + 1;
+}
+
+static __used __naked __noinline __u64 pad_calls_frame(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call pc_thrower;" /* cleanup region */
+"2:"
+ "exit;"
+"3:" /* landing pad */
+ "r6 = r0;"
+ "r1 = 1;"
+ "call pad_callee;" /* an extension can stand in here */
+ PAD_COUNT
+ PAD_RAN("%[ran]")
+ "r1 = r6;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_PAD_CALLS), __imm_addr(input), __imm_addr(pads_ran),
+ __imm_addr(pad_runs)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_pad_calls(void *ctx)
+{
+ return pad_calls_frame();
+}
+
+/*
+ * 11. A covered bpf_throw() the sweep leaves as the last instruction, where
+ * the default exception callback is then patched in -- the one patchlet that
+ * does not keep the call it replaced in the last slot, so the marks have to
+ * follow it. The r6-r9 check is what reports a lost throw site mark.
+ */
+SEC("syscall")
+__naked int entry_pad_first(void)
+{
+ asm volatile (
+ LOAD_MAGIC_REGS
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 < 101 goto 7f;"
+ "goto 4f;"
+"3:" /* landing pad, ahead of the call */
+ CHECK_MAGIC_REGS("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+"7:"
+ "r0 = 0;"
+ "exit;"
+"4:"
+ "r1 = %[cookie];"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "exit;" /* dead: swept, leaving the call last */
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_PAD_FIRST),
+ __imm_addr(input), __imm_addr(magic), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 12. A cleanup region whose last instruction is a 16-byte one, so end - 1
+ * names the half that is not an instruction of its own. A well formed region
+ * that a rule against it would turn away.
+ */
+static __used __naked __noinline __u64 wide_rec_frame(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r6 = *(u64 *)(r1 + 0);"
+ "call bpf_rcu_read_lock;"
+ "r1 = r6;"
+"1:" "call shared_callee;" /* cleanup region begins */
+ "r1 = %[magic] ll;" /* ... and ends on this pair */
+"2:"
+ "r6 = r0;"
+ "call bpf_rcu_read_unlock;"
+ "r0 = r6;"
+ "exit;"
+"3:" /* landing pad */
+ "r7 = r0;"
+ "call bpf_rcu_read_unlock;"
+ PAD_RAN("%[ran]")
+ "r1 = r7;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_WIDE_REC), __imm_addr(input), __imm_addr(magic),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_wide_rec(void *ctx)
+{
+ return wide_rec_frame();
+}
+
+/*
+ * 13. A pad that works out of its own frame's stack, the shape every
+ * compiler-generated pad has. A JIT that addresses the frame through the
+ * stack pointer -- arm64 -- has to address a pad's frame some other way. Both
+ * directions are here: the reload sees the frame, and the store lands in it.
+ */
+static __used __naked __noinline __u64 pad_stack_frame(void)
+{
+ asm volatile (
+ "r1 = %[magic] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "*(u64 *)(r10 - 8) = r1;" /* what the pad will want */
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call pc_thrower;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r6 = r0;"
+ "r7 = *(u64 *)(r10 - 8);" /* reload it out of the frame */
+ "*(u64 *)(r10 - 16) = r7;" /* and write the frame while here */
+ "r1 = %[magic] ll;"
+ "r2 = *(u64 *)(r1 + 0);"
+ "if r7 != r2 goto 9f;"
+ "r3 = *(u64 *)(r10 - 16);"
+ "if r3 != r2 goto 9f;"
+ PAD_RAN("%[ran]")
+"9:"
+ "r1 = r6;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_PAD_STACK), __imm_addr(input), __imm_addr(magic),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_pad_stack(void *ctx)
+{
+ return pad_stack_frame();
+}
+
+/*
+ * 14. The same, with a frame in between that has no pad of its own, so the
+ * liveness query for an outer frame has more than one frame to walk and the
+ * pad has to be counted at every step.
+ */
+static __used __noinline __u64 deep_mid(__u64 x)
+{
+ return pc_thrower(x) + 1;
+}
+
+static __used __naked __noinline __u64 deep_frame(void)
+{
+ asm volatile (
+ "r1 = %[magic] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "*(u64 *)(r10 - 8) = r1;" /* nothing but the pad reads this */
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call deep_mid;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r6 = r0;"
+ "r7 = *(u64 *)(r10 - 8);"
+ "r1 = %[magic] ll;"
+ "r2 = *(u64 *)(r1 + 0);"
+ "if r7 != r2 goto 9f;"
+ PAD_RAN("%[ran]")
+"9:"
+ "r1 = r6;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_DEEP_PAD), __imm_addr(input), __imm_addr(magic),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_deep_pad(void *ctx)
+{
+ return deep_frame();
+}
+
+/*
+ * 15. A region around a call the kernel knows cannot unwind: no call site is
+ * marked, nothing reaches the pad, and the sweep removes it. The program is
+ * otherwise ordinary and has to load.
+ */
+static __used __naked __noinline __u64 nounwind_rec_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+"1:" "call bpf_preempt_enable;" /* cleanup region: nounwind */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad, never dispatched */
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_NOUNWIND_REC), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_nounwind_rec(void *ctx)
+{
+ return nounwind_rec_frame();
+}
+
+/*
+ * 16. The name a frontend gives the resume. Every pad above calls
+ * bpf_unwind_resume(); LLVM emits _Unwind_Resume() and libbpf maps one onto
+ * the other, so this program is what keeps that mapping tested.
+ */
+extern void _Unwind_Resume(void) __ksym;
+
+static __used __noinline void __resume_alias_btf_anchor(void)
+{
+ _Unwind_Resume();
+}
+
+static __used __naked __noinline __u64 resume_alias_frame(void)
+{
+ asm volatile (
+"1:" "call regs_thrower;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ PAD_RAN("%[ran]")
+ "call _Unwind_Resume;" /* the frontend's name for it */
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_RESUME_ALIAS), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_resume_alias(void *ctx)
+{
+ return resume_alias_frame();
+}
+
+/*
+ * 17. A landing pad that calls a subprogram which tail calls. What a pad may
+ * not contain is a tail call of its own, which would unwind a prologue the
+ * walker's stack never held; a callee's prologue really did run there, so its
+ * tail call releases exactly that and the target returns into the pad. The
+ * tail call counter comes out of the unwinding frame, which is one of the
+ * pad's own subprogram and so really holds one.
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} pad_tc_table SEC(".maps");
+
+__u64 pad_tc_target_ran = 0;
+
+SEC("syscall")
+int pad_tc_target(void *ctx)
+{
+ pad_tc_target_ran += 1;
+ return 0;
+}
+
+static __used __noinline __u64 pad_tc_callee(void *ctx)
+{
+ /* Taken only once the test has populated the array. */
+ bpf_tail_call_static(ctx, &pad_tc_table, 0);
+ return 0;
+}
+
+/*
+ * The frame with the pad is the program itself, and __naked: r1 holds the
+ * context at entry, which is the only place to get one for bpf_tail_call().
+ * The pad reloads it from its own frame's stack.
+ */
+SEC("syscall")
+__naked int entry_pad_tail_call(void)
+{
+ asm volatile (
+ "*(u64 *)(r10 - 8) = r1;" /* the context, straight from entry */
+ LOAD_MAGIC_REGS
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 < 101 goto 8f;"
+"1:" "call regs_thrower;" /* cleanup region */
+"2:"
+"8:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ PAD_COUNT
+ "r1 = *(u64 *)(r10 - 8);"
+ "call pad_tc_callee;"
+ /* Only if the frame survived the callee's tail call. */
+ CHECK_MAGIC_REGS("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_PAD_TAIL_CALL), __imm_addr(input),
+ __imm_addr(magic), __imm_addr(pads_ran), __imm_addr(pad_runs)
+ : __clobber_all);
+}
+
+/*
+ * 18. The other target for that same tail call: a program carrying a cleanup
+ * table of its own, which throws while the outer exception is still in flight.
+ * The tail call made it a boundary, so the inner walk runs its pad and ends in
+ * its own frame, never reaching the walker's frames above it: the outer pad is
+ * not restarted and the outer cookie is still the one delivered. The outer
+ * pad's r6-r9, which this target overwrites, come back with its frame.
+ */
+__u64 tc_target_pad_runs = 0;
+__u64 inner_magic = 0xd00d;
+
+static __used __naked __noinline __u64 inner_thrower(void)
+{
+ asm volatile (
+ /* Not this frame's to keep, the same as regs_thrower. */
+ "r6 = 0xf00d;"
+ "r7 = 0xcafe;"
+ "r8 = 0xbeef;"
+ "r9 = 0xdead;"
+ "r1 = %[cookie];"
+ "call bpf_throw;"
+ "r0 = 0;"
+ "exit;"
+ :
+ : [cookie]"i"(INNER_COOKIE)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__naked int pad_tc_throw_target(void)
+{
+ asm volatile (
+ /* Distinct from the outer pad's, so neither can stand in for it. */
+ "r1 = %[inner_magic] ll;"
+ "r6 = *(u64 *)(r1 + 0);"
+ "r7 = r6;"
+ "r7 += 1;"
+ "r8 = r6;"
+ "r8 += 2;"
+ "r9 = r6;"
+ "r9 += 3;"
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 < 101 goto 8f;"
+"1:" "call inner_thrower;" /* cleanup region */
+"2:"
+"8:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ /* This frame's own r6-r9, not the outer pad's. */
+ "r1 = %[inner_magic] ll;"
+ "r2 = *(u64 *)(r1 + 0);"
+ "if r6 != r2 goto 9f;"
+ "r2 += 1;"
+ "if r7 != r2 goto 9f;"
+ "r2 += 1;"
+ "if r8 != r2 goto 9f;"
+ "r2 += 1;"
+ "if r9 != r2 goto 9f;"
+ "r1 = %[tc_target_pad_runs] ll;"
+ "r2 = *(u64 *)(r1 + 0);"
+ "r2 += 1;"
+ "*(u64 *)(r1 + 0) = r2;"
+"9:"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : __imm_addr(input), __imm_addr(inner_magic),
+ __imm_addr(tc_target_pad_runs)
+ : __clobber_all);
+}
+
+char _license[] SEC("license") = "GPL";
|
| 21/3 | 2026/09/18 21:43 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 72112 output: 17 thoughts: 1726 Thoughts: **KMSAN Evaluation for BPF Cleanup Patch Series** Okay, let's assess whether this BPF cleanup patch series warrants a dedicated KMSAN fuzzing session, keeping in mind my expertise and the need to conserve resources. The target architecture is x86_64, which is relevant. First, I need to understand the scope of this patch. The title points to BPF cleanup pads and exception handling support, specifically landing pads for BPF exceptions. The modified files cover architecture-specific assembly and C code, relevant headers, and core BPF functionality within the kernel. The tools directory is also touched, which suggests some modifications in related user-space utilities. Now, let's apply the KMSAN rules rigorously: KMSAN is for detecting uninitialized memory reads, which KASAN, LOCKDEP, and other standard debuggers might miss. Therefore, I need to focus on situations where the patch introduces or modifies kernel structures sent to user space where fields might be uninitialized, potentially conditional logic or branching depending on potentially uninitialized variables or struct fields, the allocation or initialization of complex data structures where uninitialized fields could be read later in reachable code paths, and where bounds checks, lengths, or logic in a way that allows surrounding code to access uninitialized bytes of existing buffers. The absence of these factors makes KMSAN unnecessary. The patch includes changes to `kernel/bpf/check_btf.c`. Looking at the `check_cleanup_info` function, I need to evaluate where memory is allocated and/or copied. There's a `kvcalloc` for `krecord`, which *should* zero the memory, eliminating concerns. `min_size` is copied into `uattr` via `copy_to_bpfptr_offset`. `min_size` is a `u32` initialized to `min_t(u32, krec_size, urec_size)`. Sizeof(min_size) is 4 bytes. Is it initialized? Yes. My immediate focus is whether `krecord` is ever copied back to user space. I'll need to do a thorough code search across the tree for uses of `cleanup_info` or a related data structure in any function that might copy data to user space (e.g., in `bpf_prog_get_info_by_fd`), to confirm whether there could be an information leak if there were uninitialized memory. A grep is required to find any uses of `cleanup_info` and determine if data is copied to userspace. |
| 22/3 | 2026/09/18 21:43 | tool | grepper |
0mArgs:{
"Expression": "cleanup_info"
}Results: {
"Output": "drivers/net/ethernet/broadcom/cnic_defs.h=2411=struct fcoe_cached_sge_ctx {\n--\ndrivers/net/ethernet/broadcom/cnic_defs.h-2421- */\ndrivers/net/ethernet/broadcom/cnic_defs.h:2422:struct fcoe_cleanup_info {\ndrivers/net/ethernet/broadcom/cnic_defs.h-2423-\t__le16 cleaned_task_id;\n--\ndrivers/net/ethernet/broadcom/cnic_defs.h=2511=struct fcoe_ext_abts_info {\n--\ndrivers/net/ethernet/broadcom/cnic_defs.h-2519- */\ndrivers/net/ethernet/broadcom/cnic_defs.h:2520:struct fcoe_ext_cleanup_info {\ndrivers/net/ethernet/broadcom/cnic_defs.h-2521-\t__le32 rsrv0[6];\ndrivers/net/ethernet/broadcom/cnic_defs.h:2522:\tstruct fcoe_cleanup_info ctx;\ndrivers/net/ethernet/broadcom/cnic_defs.h-2523-};\n--\ndrivers/net/ethernet/broadcom/cnic_defs.h=2962=union fcoe_tx_wr_rx_rd_union_ctx {\n--\ndrivers/net/ethernet/broadcom/cnic_defs.h-2964-\tstruct fcoe_fcp_cmd_payload fcp_cmd;\ndrivers/net/ethernet/broadcom/cnic_defs.h:2965:\tstruct fcoe_ext_cleanup_info cleanup;\ndrivers/net/ethernet/broadcom/cnic_defs.h-2966-\tstruct fcoe_ext_abts_info abts;\n--\ndrivers/net/ethernet/broadcom/cnic_defs.h=3287=union fcoe_u_tce_tx_wr_rx_rd_union {\ndrivers/net/ethernet/broadcom/cnic_defs.h-3288-\tstruct fcoe_abts_info abts;\ndrivers/net/ethernet/broadcom/cnic_defs.h:3289:\tstruct fcoe_cleanup_info cleanup;\ndrivers/net/ethernet/broadcom/cnic_defs.h-3290-\tstruct fcoe_fw_tx_seq_ctx tx_seq_ctx;\n--\ndrivers/scsi/bnx2fc/57xx_hsi_bnx2fc.h=109=struct fcoe_cached_sge_ctx {\n--\ndrivers/scsi/bnx2fc/57xx_hsi_bnx2fc.h-119- */\ndrivers/scsi/bnx2fc/57xx_hsi_bnx2fc.h:120:struct fcoe_cleanup_info {\ndrivers/scsi/bnx2fc/57xx_hsi_bnx2fc.h-121-\t__le16 cleaned_task_id;\n--\ndrivers/scsi/bnx2fc/57xx_hsi_bnx2fc.h=209=struct fcoe_ext_abts_info {\n--\ndrivers/scsi/bnx2fc/57xx_hsi_bnx2fc.h-217- */\ndrivers/scsi/bnx2fc/57xx_hsi_bnx2fc.h:218:struct fcoe_ext_cleanup_info {\ndrivers/scsi/bnx2fc/57xx_hsi_bnx2fc.h-219-\t__le32 rsrv0[6];\ndrivers/scsi/bnx2fc/57xx_hsi_bnx2fc.h:220:\tstruct fcoe_cleanup_info ctx;\ndrivers/scsi/bnx2fc/57xx_hsi_bnx2fc.h-221-};\n--\ndrivers/scsi/bnx2fc/57xx_hsi_bnx2fc.h=660=union fcoe_tx_wr_rx_rd_union_ctx {\n--\ndrivers/scsi/bnx2fc/57xx_hsi_bnx2fc.h-662-\tstruct fcoe_fcp_cmd_payload fcp_cmd;\ndrivers/scsi/bnx2fc/57xx_hsi_bnx2fc.h:663:\tstruct fcoe_ext_cleanup_info cleanup;\ndrivers/scsi/bnx2fc/57xx_hsi_bnx2fc.h-664-\tstruct fcoe_ext_abts_info abts;\n--\ndrivers/usb/host/xhci-mem.c=609=struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,\n--\ndrivers/usb/host/xhci-mem.c-641-\tif (!stream_info-\u003estream_rings)\ndrivers/usb/host/xhci-mem.c:642:\t\tgoto cleanup_info;\ndrivers/usb/host/xhci-mem.c-643-\n--\ndrivers/usb/host/xhci-mem.c-715-\tkfree(stream_info-\u003estream_rings);\ndrivers/usb/host/xhci-mem.c:716:cleanup_info:\ndrivers/usb/host/xhci-mem.c-717-\tkfree(stream_info);\n--\ninclude/linux/bpf.h=1783=struct bpf_exception_info {\ninclude/linux/bpf.h:1784:\tstruct bpf_cleanup_info *info;\ninclude/linux/bpf.h-1785-\tstruct bpf_cleanup_range *ranges;\n--\ninclude/linux/bpf_verifier.h=929=struct bpf_verifier_env {\n--\ninclude/linux/bpf_verifier.h-997-\tu32 subprog_cnt;\ninclude/linux/bpf_verifier.h:998:\tstruct bpf_cleanup_info *cleanup_info;\ninclude/linux/bpf_verifier.h:999:\tu32 cleanup_info_cnt;\ninclude/linux/bpf_verifier.h-1000-\t/* number of instructions analyzed by the verifier */\n--\ninclude/uapi/linux/bpf.h=1527=union bpf_attr {\n--\ninclude/uapi/linux/bpf.h-1671-\t\t__s32\t\tkeyring_id;\ninclude/uapi/linux/bpf.h:1672:\t\t__aligned_u64\tcleanup_info;\t/* exception cleanup table */\ninclude/uapi/linux/bpf.h:1673:\t\t__u32\t\tcleanup_info_rec_size; /* userspace bpf_cleanup_info size */\ninclude/uapi/linux/bpf.h:1674:\t\t__u32\t\tcleanup_info_cnt; /* number of bpf_cleanup_info records */\ninclude/uapi/linux/bpf.h-1675-\t};\n--\ninclude/uapi/linux/bpf.h=7587=struct bpf_line_info {\n--\ninclude/uapi/linux/bpf.h-7593-\ninclude/uapi/linux/bpf.h:7594:struct bpf_cleanup_info {\ninclude/uapi/linux/bpf.h-7595-\t__u32\tbegin_off;\n--\nkernel/bpf/cfg.c=162=static int visit_cleanup_pad_edge(int t, struct bpf_verifier_env *env)\n--\nkernel/bpf/cfg.c-167-\nkernel/bpf/cfg.c:168:\tif (!env-\u003ecleanup_info_cnt)\nkernel/bpf/cfg.c-169-\t\treturn DONE_EXPLORING;\n--\nkernel/bpf/cfg.c=631=int bpf_check_cfg(struct bpf_verifier_env *env)\n--\nkernel/bpf/cfg.c-696-\t */\nkernel/bpf/cfg.c:697:\twhile (pad_idx \u003c env-\u003ecleanup_info_cnt) {\nkernel/bpf/cfg.c:698:\t\tu32 pad = env-\u003ecleanup_info[pad_idx++].landing_pad_off;\nkernel/bpf/cfg.c-699-\n--\nkernel/bpf/check_btf.c=410=static int cleanup_insn_subprog(struct bpf_verifier_env *env, u32 off)\n--\nkernel/bpf/check_btf.c-422-\nkernel/bpf/check_btf.c:423:static int check_cleanup_info(struct bpf_verifier_env *env,\nkernel/bpf/check_btf.c-424-\t\t\t const union bpf_attr *attr,\n--\nkernel/bpf/check_btf.c-426-{\nkernel/bpf/check_btf.c:427:\tu32 krec_size = sizeof(struct bpf_cleanup_info);\nkernel/bpf/check_btf.c-428-\tu32 i, nrec, urec_size, min_size, prev_end = 0;\nkernel/bpf/check_btf.c:429:\tstruct bpf_cleanup_info *krecord;\nkernel/bpf/check_btf.c-430-\tbpfptr_t urecord;\n--\nkernel/bpf/check_btf.c-432-\nkernel/bpf/check_btf.c:433:\tnrec = attr-\u003ecleanup_info_cnt;\nkernel/bpf/check_btf.c-434-\tif (!nrec)\n--\nkernel/bpf/check_btf.c-438-\nkernel/bpf/check_btf.c:439:\turec_size = attr-\u003ecleanup_info_rec_size;\nkernel/bpf/check_btf.c-440-\tif (urec_size \u003c MIN_BPF_CLEANUP_INFO_SIZE ||\n--\nkernel/bpf/check_btf.c-451-\tmin_size = min_t(u32, krec_size, urec_size);\nkernel/bpf/check_btf.c:452:\turecord = make_bpfptr(attr-\u003ecleanup_info, uattr.is_kernel);\nkernel/bpf/check_btf.c-453-\tfor (i = 0; i \u003c nrec; i++) {\nkernel/bpf/check_btf.c:454:\t\tstruct bpf_cleanup_info *rec = \u0026krecord[i];\nkernel/bpf/check_btf.c-455-\t\tint sb, se, sl;\n--\nkernel/bpf/check_btf.c-462-\t\t\t\t\t\t\t offsetof(union bpf_attr,\nkernel/bpf/check_btf.c:463:\t\t\t\t\t\t\t\t cleanup_info_rec_size),\nkernel/bpf/check_btf.c-464-\t\t\t\t\t\t\t \u0026min_size, sizeof(min_size)))\n--\nkernel/bpf/check_btf.c-477-\t\tif (rec-\u003ebegin_off \u003e= rec-\u003eend_off) {\nkernel/bpf/check_btf.c:478:\t\t\tverbose(env, \"cleanup_info[%u]: begin %u \u003e= end %u\\n\",\nkernel/bpf/check_btf.c-479-\t\t\t\ti, rec-\u003ebegin_off, rec-\u003eend_off);\n--\nkernel/bpf/check_btf.c-483-\t\t\tverbose(env,\nkernel/bpf/check_btf.c:484:\t\t\t\t\"cleanup_info[%u]: range [%u,%u) is unsorted or overlaps the previous record\\n\",\nkernel/bpf/check_btf.c-485-\t\t\t\ti, rec-\u003ebegin_off, rec-\u003eend_off);\n--\nkernel/bpf/check_btf.c-493-\t\tif (sb \u003c 0 || se \u003c 0 || sl \u003c 0) {\nkernel/bpf/check_btf.c:494:\t\t\tverbose(env, \"cleanup_info[%u]: offset out of range\\n\", i);\nkernel/bpf/check_btf.c-495-\t\t\tgoto err_free;\n--\nkernel/bpf/check_btf.c-498-\t\t\tverbose(env,\nkernel/bpf/check_btf.c:499:\t\t\t\t\"cleanup_info[%u]: range/landing pad span multiple subprogs\\n\",\nkernel/bpf/check_btf.c-500-\t\t\t\ti);\n--\nkernel/bpf/check_btf.c-512-\t\t !env-\u003eprog-\u003einsnsi[rec-\u003eend_off].code)) {\nkernel/bpf/check_btf.c:513:\t\t\tverbose(env, \"cleanup_info[%u]: points at invalid insn\\n\", i);\nkernel/bpf/check_btf.c-514-\t\t\tgoto err_free;\n--\nkernel/bpf/check_btf.c-536-\t\t\t\tverbose(env,\nkernel/bpf/check_btf.c:537:\t\t\t\t\t\"cleanup_info[%u]: landing pad %u is inside the call-site range of cleanup_info[%u]\\n\",\nkernel/bpf/check_btf.c-538-\t\t\t\t\ti, pad, m);\n--\nkernel/bpf/check_btf.c-543-\nkernel/bpf/check_btf.c:544:\tenv-\u003ecleanup_info = krecord;\nkernel/bpf/check_btf.c:545:\tenv-\u003ecleanup_info_cnt = nrec;\nkernel/bpf/check_btf.c-546-\treturn 0;\n--\nkernel/bpf/check_btf.c=581=int bpf_check_btf_info(struct bpf_verifier_env *env,\n--\nkernel/bpf/check_btf.c-586-\nkernel/bpf/check_btf.c:587:\terr = check_cleanup_info(env, attr, uattr);\nkernel/bpf/check_btf.c-588-\tif (err)\n--\nkernel/bpf/exception.c=161=int bpf_cleanup_check_callback(struct bpf_verifier_env *env, int subprog)\nkernel/bpf/exception.c-162-{\nkernel/bpf/exception.c:163:\tif (!env-\u003ecleanup_info_cnt || !env-\u003esubprog_info[subprog].might_throw)\nkernel/bpf/exception.c-164-\t\treturn 0;\n--\nkernel/bpf/exception.c=321=static int cleanup_mark_pad_bodies(struct cleanup_ctx *ctx)\n--\nkernel/bpf/exception.c-326-\nkernel/bpf/exception.c:327:\tfor (i = 0; i \u003c env-\u003ecleanup_info_cnt; i++) {\nkernel/bpf/exception.c:328:\t\tu32 pad = env-\u003ecleanup_info[i].landing_pad_off;\nkernel/bpf/exception.c-329-\n--\nkernel/bpf/exception.c=402=static void cleanup_mark_call_sites(struct bpf_verifier_env *env)\n--\nkernel/bpf/exception.c-405-\nkernel/bpf/exception.c:406:\tfor (i = 0; i \u003c env-\u003ecleanup_info_cnt; i++) {\nkernel/bpf/exception.c:407:\t\tstruct bpf_cleanup_info *rec = \u0026env-\u003ecleanup_info[i];\nkernel/bpf/exception.c-408-\n--\nkernel/bpf/exception.c=419=int bpf_prepare_cleanup_exceptions(struct bpf_verifier_env *env)\nkernel/bpf/exception.c-420-{\nkernel/bpf/exception.c:421:\tif (!env-\u003ecleanup_info_cnt)\nkernel/bpf/exception.c-422-\t\treturn 0;\n--\nkernel/bpf/exception.c=448=int bpf_check_cleanup_exceptions(struct bpf_verifier_env *env)\n--\nkernel/bpf/exception.c-457-\nkernel/bpf/exception.c:458:\tif (!env-\u003ecleanup_info_cnt)\nkernel/bpf/exception.c-459-\t\treturn 0;\n--\nkernel/bpf/exception.c=529=int bpf_cleanup_alloc_info(struct bpf_prog_aux *aux)\n--\nkernel/bpf/exception.c-536-\nkernel/bpf/exception.c:537:int bpf_cleanup_attach_info(struct bpf_prog_aux *aux, struct bpf_cleanup_info *recs, u32 cnt)\nkernel/bpf/exception.c-538-{\n--\nkernel/bpf/exception.c=578=void bpf_cleanup_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image)\n--\nkernel/bpf/exception.c-587-\tfor (i = 0; i \u003c n; i++) {\nkernel/bpf/exception.c:588:\t\tconst struct bpf_cleanup_info *rec = \u0026exc-\u003einfo[i];\nkernel/bpf/exception.c-589-\n--\nkernel/bpf/exception.h-7-\nkernel/bpf/exception.h:8:struct bpf_cleanup_info;\nkernel/bpf/exception.h-9-struct bpf_cleanup_range;\n--\nkernel/bpf/exception.h=18=int bpf_cleanup_alloc_info(struct bpf_prog_aux *aux);\nkernel/bpf/exception.h:19:int bpf_cleanup_attach_info(struct bpf_prog_aux *aux, struct bpf_cleanup_info *recs, u32 cnt);\nkernel/bpf/exception.h-20-const struct bpf_cleanup_range *bpf_cleanup_pad_for_ip(const struct bpf_prog *prog, u64 ip);\n--\nkernel/bpf/fixups.c=222=static void adjust_insn_aux_data(struct bpf_verifier_env *env,\n--\nkernel/bpf/fixups.c-271-\nkernel/bpf/fixups.c:272:\tif (env-\u003ecleanup_info_cnt)\nkernel/bpf/fixups.c-273-\t\tfor (i = 0; i \u003c prog_len; i++)\n--\nkernel/bpf/fixups.c=560=static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)\n--\nkernel/bpf/fixups.c-589-\nkernel/bpf/fixups.c:590:\tif (env-\u003ecleanup_info_cnt) {\nkernel/bpf/fixups.c-591-\t\tfor (i = 0; i \u003c env-\u003einsn_aux_data_len; i++) {\n--\nkernel/bpf/fixups.c=1149=static int cleanup_pad_body_for_subprog(struct bpf_verifier_env *env, struct bpf_prog *sub,\n--\nkernel/bpf/fixups.c-1173-\nkernel/bpf/fixups.c:1174:static int cleanup_info_for_subprog(struct bpf_verifier_env *env, struct bpf_prog *sub,\nkernel/bpf/fixups.c-1175-\t\t\t\t u32 start, u32 end)\nkernel/bpf/fixups.c-1176-{\nkernel/bpf/fixups.c:1177:\tstruct bpf_cleanup_info *recs;\nkernel/bpf/fixups.c-1178-\tu32 i, cnt = 0;\n--\nkernel/bpf/fixups.c-1180-\nkernel/bpf/fixups.c:1181:\tif (!env-\u003ecleanup_info_cnt)\nkernel/bpf/fixups.c-1182-\t\treturn 0;\n--\nkernel/bpf/fixups.c=1226=int bpf_cleanup_attach_main_prog(struct bpf_verifier_env *env, struct bpf_prog *prog)\n--\nkernel/bpf/fixups.c-1229-\t\treturn 0;\nkernel/bpf/fixups.c:1230:\treturn cleanup_info_for_subprog(env, prog, 0, prog-\u003elen);\nkernel/bpf/fixups.c-1231-}\n--\nkernel/bpf/fixups.c=1233=static int jit_subprogs(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-1369-\t\t\tfunc[i]-\u003eaux-\u003eexception_boundary = env-\u003eseen_exception;\nkernel/bpf/fixups.c:1370:\t\terr = cleanup_info_for_subprog(env, func[i], subprog_start,\nkernel/bpf/fixups.c-1371-\t\t\t\t\t env-\u003esubprog_info[i + 1].start);\n--\nkernel/bpf/liveness.c=220=bpf_insn_successors(struct bpf_verifier_env *env, u32 idx)\n--\nkernel/bpf/liveness.c-271-\nkernel/bpf/liveness.c:272:\tif (unlikely(env-\u003ecleanup_info_cnt)) {\nkernel/bpf/liveness.c-273-\t\tint pad = bpf_cleanup_pad_of_call(env, idx);\n--\nkernel/bpf/liveness.c=371=bool bpf_stack_slot_alive(struct bpf_verifier_env *env, u32 frameno, u32 half_spi)\n--\nkernel/bpf/liveness.c-413-\t\t/* Control may also go to the landing pad. */\nkernel/bpf/liveness.c:414:\t\tif (!alive \u0026\u0026 unlikely(env-\u003ecleanup_info_cnt)) {\nkernel/bpf/liveness.c-415-\t\t\tint pad = bpf_cleanup_pad_of_call(env, callsite);\n--\nkernel/bpf/syscall.c=2912=BTF_ID_LIST_GLOBAL_SINGLE(bpf_multi_func_btf_id, func, bpf_multi_func)\n--\nkernel/bpf/syscall.c-2914-/* last field in 'union bpf_attr' used by this command */\nkernel/bpf/syscall.c:2915:#define BPF_PROG_LOAD_LAST_FIELD cleanup_info_cnt\nkernel/bpf/syscall.c-2916-\n--\nkernel/bpf/verifier.c=5575=static int check_max_stack_depth(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-5598-\t */\nkernel/bpf/verifier.c:5599:\tif (env-\u003ecleanup_info_cnt)\nkernel/bpf/verifier.c-5600-\t\tpriv_stack_mode = NO_PRIV_STACK;\n--\nkernel/bpf/verifier.c=21580=int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/verifier.c-21925-\tbpf_diag_free(env);\nkernel/bpf/verifier.c:21926:\tkvfree(env-\u003ecleanup_info);\nkernel/bpf/verifier.c-21927-\tkvfree(env);\n--\ntools/include/uapi/linux/bpf.h=1527=union bpf_attr {\n--\ntools/include/uapi/linux/bpf.h-1671-\t\t__s32\t\tkeyring_id;\ntools/include/uapi/linux/bpf.h:1672:\t\t__aligned_u64\tcleanup_info;\t/* exception cleanup table */\ntools/include/uapi/linux/bpf.h:1673:\t\t__u32\t\tcleanup_info_rec_size; /* userspace bpf_cleanup_info size */\ntools/include/uapi/linux/bpf.h:1674:\t\t__u32\t\tcleanup_info_cnt; /* number of bpf_cleanup_info records */\ntools/include/uapi/linux/bpf.h-1675-\t};\n--\ntools/include/uapi/linux/bpf.h=7587=struct bpf_line_info {\n--\ntools/include/uapi/linux/bpf.h-7593-\ntools/include/uapi/linux/bpf.h:7594:struct bpf_cleanup_info {\ntools/include/uapi/linux/bpf.h-7595-\t__u32\tbegin_off;\n--\ntools/lib/bpf/bpf.c=293=int bpf_prog_load(enum bpf_prog_type prog_type,\n--\ntools/lib/bpf/bpf.c-297-{\ntools/lib/bpf/bpf.c:298:\tconst size_t attr_sz = offsetofend(union bpf_attr, cleanup_info_cnt);\ntools/lib/bpf/bpf.c-299-\tvoid *finfo = NULL, *linfo = NULL;\n--\ntools/lib/bpf/bpf.c-372-\ntools/lib/bpf/bpf.c:373:\tattr.cleanup_info = ptr_to_u64(OPTS_GET(opts, cleanup_info, NULL));\ntools/lib/bpf/bpf.c:374:\tattr.cleanup_info_cnt = OPTS_GET(opts, cleanup_info_cnt, 0);\ntools/lib/bpf/bpf.c:375:\tattr.cleanup_info_rec_size = OPTS_GET(opts, cleanup_info_rec_size, 0);\ntools/lib/bpf/bpf.c-376-\n--\ntools/lib/bpf/bpf.h=86=struct bpf_prog_load_opts {\n--\ntools/lib/bpf/bpf.h-132-\t/* exception cleanup table, from the .bpf_cleanup section */\ntools/lib/bpf/bpf.h:133:\tconst void *cleanup_info;\ntools/lib/bpf/bpf.h:134:\t__u32 cleanup_info_cnt;\ntools/lib/bpf/bpf.h:135:\t__u32 cleanup_info_rec_size;\ntools/lib/bpf/bpf.h-136-\tsize_t :0;\ntools/lib/bpf/bpf.h-137-};\ntools/lib/bpf/bpf.h:138:#define bpf_prog_load_opts__last_field cleanup_info_rec_size\ntools/lib/bpf/bpf.h-139-\n--\ntools/lib/bpf/gen_loader.c=985=static void info_blob_bswap(struct bpf_gen *gen, int func_info, int line_info,\ntools/lib/bpf/gen_loader.c:986:\t\t\t int core_relos, int cleanup_info,\ntools/lib/bpf/gen_loader.c-987-\t\t\t struct bpf_prog_load_opts *load_attr)\n--\ntools/lib/bpf/gen_loader.c-991-\tstruct bpf_core_relo *cr = gen-\u003edata_start + core_relos;\ntools/lib/bpf/gen_loader.c:992:\tstruct bpf_cleanup_info *ci = gen-\u003edata_start + cleanup_info;\ntools/lib/bpf/gen_loader.c-993-\tint i;\n--\ntools/lib/bpf/gen_loader.c-1003-\ntools/lib/bpf/gen_loader.c:1004:\tfor (i = 0; i \u003c load_attr-\u003ecleanup_info_cnt; i++)\ntools/lib/bpf/gen_loader.c:1005:\t\tbpf_cleanup_info_bswap(ci++);\ntools/lib/bpf/gen_loader.c-1006-}\n--\ntools/lib/bpf/gen_loader.c=1008=void bpf_gen__prog_load(struct bpf_gen *gen,\n--\ntools/lib/bpf/gen_loader.c-1018-\t\t\t sizeof(struct bpf_core_relo);\ntools/lib/bpf/gen_loader.c:1019:\tint cleanup_info_tot_sz = load_attr-\u003ecleanup_info_cnt *\ntools/lib/bpf/gen_loader.c:1020:\t\t\t\t load_attr-\u003ecleanup_info_rec_size;\ntools/lib/bpf/gen_loader.c-1021-\tint prog_load_attr, license_off, insns_off, func_info, line_info, core_relos;\ntools/lib/bpf/gen_loader.c:1022:\tint attr_size = offsetofend(union bpf_attr, cleanup_info_cnt);\ntools/lib/bpf/gen_loader.c:1023:\tint cleanup_info;\ntools/lib/bpf/gen_loader.c-1024-\tunion bpf_attr attr;\n--\ntools/lib/bpf/gen_loader.c-1071-\ntools/lib/bpf/gen_loader.c:1072:\tattr.cleanup_info_rec_size = tgt_endian(load_attr-\u003ecleanup_info_rec_size);\ntools/lib/bpf/gen_loader.c:1073:\tattr.cleanup_info_cnt = tgt_endian(load_attr-\u003ecleanup_info_cnt);\ntools/lib/bpf/gen_loader.c:1074:\tcleanup_info = add_data(gen, load_attr-\u003ecleanup_info, cleanup_info_tot_sz);\ntools/lib/bpf/gen_loader.c:1075:\tpr_debug(\"gen: prog_load: cleanup_info: off %d cnt %u rec size %u\\n\",\ntools/lib/bpf/gen_loader.c:1076:\t\t cleanup_info, load_attr-\u003ecleanup_info_cnt,\ntools/lib/bpf/gen_loader.c:1077:\t\t load_attr-\u003ecleanup_info_rec_size);\ntools/lib/bpf/gen_loader.c-1078-\n--\ntools/lib/bpf/gen_loader.c-1080-\tif (gen-\u003eswapped_endian \u0026\u0026 !gen-\u003eerror)\ntools/lib/bpf/gen_loader.c:1081:\t\tinfo_blob_bswap(gen, func_info, line_info, core_relos, cleanup_info,\ntools/lib/bpf/gen_loader.c-1082-\t\t\t\tload_attr);\n--\ntools/lib/bpf/gen_loader.c-1103-\ntools/lib/bpf/gen_loader.c:1104:\t/* populate union bpf_attr with a pointer to cleanup_info */\ntools/lib/bpf/gen_loader.c:1105:\temit_rel_store(gen, attr_field(prog_load_attr, cleanup_info), cleanup_info);\ntools/lib/bpf/gen_loader.c-1106-\n--\ntools/lib/bpf/libbpf.c=450=struct bpf_program {\n--\ntools/lib/bpf/libbpf.c-517-\ntools/lib/bpf/libbpf.c:518:\tstruct bpf_cleanup_info *cleanup_info;\ntools/lib/bpf/libbpf.c:519:\t__u32 cleanup_info_rec_size;\ntools/lib/bpf/libbpf.c:520:\t__u32 cleanup_info_cnt;\ntools/lib/bpf/libbpf.c-521-\n--\ntools/lib/bpf/libbpf.c=680=struct elf_sec_desc {\n--\ntools/lib/bpf/libbpf.c-685-\ntools/lib/bpf/libbpf.c:686:#define CLEANUP_REC_FIELDS\t(sizeof(struct bpf_cleanup_info) / sizeof(__u32))\ntools/lib/bpf/libbpf.c-687-\ntools/lib/bpf/libbpf.c:688:/* Index of each field of struct bpf_cleanup_info, read as an array of __u32. */\ntools/lib/bpf/libbpf.c-689-enum {\n--\ntools/lib/bpf/libbpf.c=840=static void bpf_program__exit(struct bpf_program *prog)\n--\ntools/lib/bpf/libbpf.c-849-\tzfree(\u0026prog-\u003ereloc_desc);\ntools/lib/bpf/libbpf.c:850:\tzfree(\u0026prog-\u003ecleanup_info);\ntools/lib/bpf/libbpf.c-851-\ntools/lib/bpf/libbpf.c:852:\tprog-\u003ecleanup_info_rec_size = 0;\ntools/lib/bpf/libbpf.c:853:\tprog-\u003ecleanup_info_cnt = 0;\ntools/lib/bpf/libbpf.c-854-\tprog-\u003enr_reloc = 0;\n--\ntools/lib/bpf/libbpf.c=4860=static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj,\n--\ntools/lib/bpf/libbpf.c-4887-\ntools/lib/bpf/libbpf.c:4888:static int bpf_object__init_cleanup_info(struct bpf_object *obj)\ntools/lib/bpf/libbpf.c-4889-{\n--\ntools/lib/bpf/libbpf.c-4916-\t}\ntools/lib/bpf/libbpf.c:4917:\tif (data-\u003ed_size % sizeof(struct bpf_cleanup_info)) {\ntools/lib/bpf/libbpf.c-4918-\t\tpr_warn(\"%s size %zu is not a multiple of the record size %zu\\n\",\ntools/lib/bpf/libbpf.c:4919:\t\t\tCLEANUP_SEC, data-\u003ed_size, sizeof(struct bpf_cleanup_info));\ntools/lib/bpf/libbpf.c-4920-\t\treturn -LIBBPF_ERRNO__FORMAT;\n--\ntools/lib/bpf/libbpf.c-4924-\tnslots = data-\u003ed_size / sizeof(__u32);\ntools/lib/bpf/libbpf.c:4925:\tnrecs = data-\u003ed_size / sizeof(struct bpf_cleanup_info);\ntools/lib/bpf/libbpf.c-4926-\n--\ntools/lib/bpf/libbpf.c-4993-\ntools/lib/bpf/libbpf.c:4994:static int cmp_cleanup_info(const void *a, const void *b)\ntools/lib/bpf/libbpf.c-4995-{\ntools/lib/bpf/libbpf.c:4996:\tconst struct bpf_cleanup_info *x = a, *y = b;\ntools/lib/bpf/libbpf.c-4997-\n--\ntools/lib/bpf/libbpf.c-5002-\ntools/lib/bpf/libbpf.c:5003:static int bpf_prog_collect_cleanup_info(struct bpf_object *obj,\ntools/lib/bpf/libbpf.c-5004-\t\t\t\t\t struct bpf_program *prog)\n--\ntools/lib/bpf/libbpf.c-5011-\t\tstruct bpf_program *owner = NULL;\ntools/lib/bpf/libbpf.c:5012:\t\tstruct bpf_cleanup_info ci = {};\ntools/lib/bpf/libbpf.c-5013-\t\t__u32 *fields = (__u32 *)\u0026ci;\n--\ntools/lib/bpf/libbpf.c-5066-\ntools/lib/bpf/libbpf.c:5067:\t\ttmp = libbpf_reallocarray(prog-\u003ecleanup_info, prog-\u003ecleanup_info_cnt + 1,\ntools/lib/bpf/libbpf.c:5068:\t\t\t\t\t sizeof(*prog-\u003ecleanup_info));\ntools/lib/bpf/libbpf.c-5069-\t\tif (!tmp)\ntools/lib/bpf/libbpf.c-5070-\t\t\treturn -ENOMEM;\ntools/lib/bpf/libbpf.c:5071:\t\tprog-\u003ecleanup_info = tmp;\ntools/lib/bpf/libbpf.c:5072:\t\tprog-\u003ecleanup_info_rec_size = sizeof(struct bpf_cleanup_info);\ntools/lib/bpf/libbpf.c:5073:\t\tprog-\u003ecleanup_info[prog-\u003ecleanup_info_cnt++] = ci;\ntools/lib/bpf/libbpf.c-5074-\n--\ntools/lib/bpf/libbpf.c-5078-\ntools/lib/bpf/libbpf.c:5079:\tqsort(prog-\u003ecleanup_info, prog-\u003ecleanup_info_cnt,\ntools/lib/bpf/libbpf.c:5080:\t sizeof(*prog-\u003ecleanup_info), cmp_cleanup_info);\ntools/lib/bpf/libbpf.c:5081:\tfor (i = 1; i \u003c prog-\u003ecleanup_info_cnt; i++) {\ntools/lib/bpf/libbpf.c:5082:\t\tstruct bpf_cleanup_info *prev = \u0026prog-\u003ecleanup_info[i - 1];\ntools/lib/bpf/libbpf.c:5083:\t\tstruct bpf_cleanup_info *cur = \u0026prog-\u003ecleanup_info[i];\ntools/lib/bpf/libbpf.c-5084-\n--\ntools/lib/bpf/libbpf.c=7709=static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)\n--\ntools/lib/bpf/libbpf.c-7805-\ntools/lib/bpf/libbpf.c:7806:\t\terr = bpf_prog_collect_cleanup_info(obj, prog);\ntools/lib/bpf/libbpf.c-7807-\t\tif (err) {\n--\ntools/lib/bpf/libbpf.c=8220=static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog,\n--\ntools/lib/bpf/libbpf.c-8276-\t}\ntools/lib/bpf/libbpf.c:8277:\tif (prog-\u003ecleanup_info_cnt) {\ntools/lib/bpf/libbpf.c:8278:\t\tload_attr.cleanup_info = prog-\u003ecleanup_info;\ntools/lib/bpf/libbpf.c:8279:\t\tload_attr.cleanup_info_cnt = prog-\u003ecleanup_info_cnt;\ntools/lib/bpf/libbpf.c:8280:\t\tload_attr.cleanup_info_rec_size = prog-\u003ecleanup_info_rec_size;\ntools/lib/bpf/libbpf.c-8281-\t}\n--\ntools/lib/bpf/libbpf.c=8744=static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf, size_t obj_buf_sz,\n--\ntools/lib/bpf/libbpf.c-8837-\terr = err ? : bpf_object__collect_relos(obj);\ntools/lib/bpf/libbpf.c:8838:\terr = err ? : bpf_object__init_cleanup_info(obj);\ntools/lib/bpf/libbpf.c-8839-\tif (err)\n--\ntools/lib/bpf/libbpf.c=10281=int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_opts *opts)\n--\ntools/lib/bpf/libbpf.c-10347-\t/* exception cleanup table */\ntools/lib/bpf/libbpf.c:10348:\tinfo = OPTS_GET(opts, cleanup_info, NULL);\ntools/lib/bpf/libbpf.c:10349:\tinfo_cnt = OPTS_GET(opts, cleanup_info_cnt, 0);\ntools/lib/bpf/libbpf.c:10350:\tinfo_rec_size = OPTS_GET(opts, cleanup_info_rec_size, 0);\ntools/lib/bpf/libbpf.c-10351-\tif (!!info != !!info_cnt || !!info != !!info_rec_size) {\ntools/lib/bpf/libbpf.c:10352:\t\tpr_warn(\"prog '%s': cleanup_info, cleanup_info_cnt, and cleanup_info_rec_size must all be specified or all omitted\\n\",\ntools/lib/bpf/libbpf.c-10353-\t\t\tprog-\u003ename);\n--\ntools/lib/bpf/libbpf.c-10356-\tif (info) {\ntools/lib/bpf/libbpf.c:10357:\t\tattr.cleanup_info = info;\ntools/lib/bpf/libbpf.c:10358:\t\tattr.cleanup_info_cnt = info_cnt;\ntools/lib/bpf/libbpf.c:10359:\t\tattr.cleanup_info_rec_size = info_rec_size;\ntools/lib/bpf/libbpf.c:10360:\t} else if (prog-\u003ecleanup_info_cnt) {\ntools/lib/bpf/libbpf.c:10361:\t\tattr.cleanup_info = prog-\u003ecleanup_info;\ntools/lib/bpf/libbpf.c:10362:\t\tattr.cleanup_info_cnt = prog-\u003ecleanup_info_cnt;\ntools/lib/bpf/libbpf.c:10363:\t\tattr.cleanup_info_rec_size = prog-\u003ecleanup_info_rec_size;\ntools/lib/bpf/libbpf.c-10364-\t}\n--\ntools/lib/bpf/libbpf_internal.h=567=static inline void bpf_core_relo_bswap(struct bpf_core_relo *i)\n--\ntools/lib/bpf/libbpf_internal.h-574-\ntools/lib/bpf/libbpf_internal.h:575:static inline void bpf_cleanup_info_bswap(struct bpf_cleanup_info *i)\ntools/lib/bpf/libbpf_internal.h-576-{\n"
} |
| 23/3 | 2026/09/18 21:43 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 85440 output: 37 thoughts: 61 Thoughts: Here's my take on those thoughts: **Analyzing `bpf_prog_get_info_by_fd` and Its Interaction with `cleanup_info`** Okay, I'm thinking about this `bpf_prog_get_info_by_fd` function and how it relates to `cleanup_info`. My initial hunch is that it doesn't actually interact with `cleanup_info` directly. But, given the importance of thoroughly understanding this area, I need to confirm that. I'm going to jump into `kernel/bpf/syscall.c` and take a look at the implementation of `bpf_prog_get_info_by_fd`. I need to ensure there are no subtle interactions or indirect calls that might affect `cleanup_info` that I'm missing at first glance. It's best to be absolutely certain on this point. I'm especially keen to look for any potential side effects. |
| 24/3 | 2026/09/18 21:43 | tool | grepper |
0mArgs:{
"Expression": "bpf_prog_get_info_by_fd",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/syscall.c=5068=static int set_info_rec_size(struct bpf_prog_info *info)\n--\nkernel/bpf/syscall.c-5098-\nkernel/bpf/syscall.c:5099:static int bpf_prog_get_info_by_fd(struct file *file,\nkernel/bpf/syscall.c-5100-\t\t\t\t struct bpf_prog *prog,\n--\nkernel/bpf/syscall.c=5535=static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,\n--\nkernel/bpf/syscall.c-5545-\tif (fd_file(f)-\u003ef_op == \u0026bpf_prog_fops)\nkernel/bpf/syscall.c:5546:\t\treturn bpf_prog_get_info_by_fd(fd_file(f), fd_file(f)-\u003eprivate_data, attr,\nkernel/bpf/syscall.c-5547-\t\t\t\t\t uattr);\n"
} |
| 25/3 | 2026/09/18 21:43 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 86010 output: 27 thoughts: 4586 Thoughts: **KMSAN Check for Uninitialized Data in BPF Exception Cleanup** Alright, after meticulously reviewing the proposed patch and the surrounding code, I've confirmed my initial assessment. The core concern revolves around the potential for uninitialized data within the context of BPF exception handling, specifically in the cleanup pad mechanism. The goal is to determine if we need to set `NeedsKMSAN=true`. My first step was to scrutinize the code for any instances of kernel data being copied to user space, the primary culprit for uninitialized data exposure. I found one relevant call to `copy_to_bpfptr_offset` which copies `min_size`, a `u32`, to user space. `min_size` is fully initialized, so no issues here. Next, I looked for conditional logic or complex data structure allocation that could lead to reading uninitialized memory. I went through the entire patch, meticulously tracing data flow, and examining related structures, specifically `bpf_exception_info`, `bpf_cleanup_info`, `bpf_verifier_state` and their allocations and initializations, in files like `exception.c`, `fixups.c`, and `verifier.c`. All allocations use `kzalloc` or `kvcalloc`, ensuring zero initialization, or allocate arrays where values are written, preventing the use of uninitialized data. The key area of interest was the register spilling and restoration performed by `arch_bpf_run_cleanup_pad`. I dug deeply into how registers are saved, restored, and what code is responsible. However, I confirmed that this occurs within the generated JIT code, *not* within the kernel C code that KMSAN instruments. The BPF JIT code is *not* checked by KMSAN. The BPF interpreter is not used in the exception cleanup path, which relies on JIT. I verified that KMSAN would not catch uninitialized variables in JIT generated code. I scrutinized all files modified and introduced by the patch to rule out any subtle vulnerabilities. After this rigorous investigation, I concluded that this patch does *not* introduce or modify the code in a way that necessitates setting `NeedsKMSAN=true`. The patch is safe with respect to uninitialized memory access. |
| 26/3 | 2026/09/18 21:43 | tool | grepper |
0mArgs:{
"Expression": "unwinding",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/helpers.c=3423=__bpf_kfunc void bpf_throw(u64 cookie)\n--\nkernel/bpf/helpers.c-3436-\t * which skips compiler generated instrumentation to do the same. Some\nkernel/bpf/helpers.c:3437:\t * architectures cannot recover sp while unwinding, so fall back to bp.\nkernel/bpf/helpers.c-3438-\t */\n--\nkernel/bpf/states.c=977=static bool states_equal(struct bpf_verifier_env *env,\n--\nkernel/bpf/states.c-998-\nkernel/bpf/states.c:999:\tif (old-\u003eunwinding != cur-\u003eunwinding)\nkernel/bpf/states.c-1000-\t\treturn false;\n--\nkernel/bpf/verifier.c=1694=int bpf_copy_verifier_state(struct bpf_verifier_state *dst_state,\n--\nkernel/bpf/verifier.c-1718-\tdst_state-\u003ein_sleepable = src-\u003ein_sleepable;\nkernel/bpf/verifier.c:1719:\tdst_state-\u003eunwinding = src-\u003eunwinding;\nkernel/bpf/verifier.c-1720-\tdst_state-\u003ecurframe = src-\u003ecurframe;\n--\nkernel/bpf/verifier.c=18482=static int unwind_step(struct bpf_verifier_env *env, u32 callsite, int *insn_idx)\n--\nkernel/bpf/verifier.c-18485-\nkernel/bpf/verifier.c:18486:\tstate-\u003eunwinding = true;\nkernel/bpf/verifier.c-18487-\tfor (;;) {\n--\nkernel/bpf/verifier.c=18501=static int process_cleanup_resume(struct bpf_verifier_env *env, int *insn_idx)\n--\nkernel/bpf/verifier.c-18505-\t/* A pad entered by ordinary control flow. */\nkernel/bpf/verifier.c:18506:\tif (!state-\u003eunwinding) {\nkernel/bpf/verifier.c-18507-\t\tverbose(env,\n"
} |
| 27/3 | 2026/09/18 21:43 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 91666 output: 34 thoughts: 21 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Tracing the Allocation of `bpf_verifier_state`** Alright, let's dive into the verifier code, specifically `verifier.c`. My immediate goal is to understand the lifecycle of the `bpf_verifier_state` struct. I need to pinpoint exactly where this critical structure is allocated within the verifier's execution flow. I'm after the `kmalloc()` or equivalent calls, ideally those tied to the instantiation of the verification process. I'll need to follow the control flow from the entry point, likely the function that's initially called to kick off the verification, and trace how the state is constructed and initialized. I'm also curious about any related metadata or tracking associated with the struct allocation; understanding how the memory is handled is crucial. |
| 28/3 | 2026/09/18 21:43 | tool | grepper |
0mArgs:{
"Expression": "kzalloc.*bpf_verifier_state",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/states.c=1239=int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)\n--\nkernel/bpf/states.c-1559-\t */\nkernel/bpf/states.c:1560:\tnew_sl = kzalloc_obj(struct bpf_verifier_state_list, GFP_KERNEL_ACCOUNT);\nkernel/bpf/states.c-1561-\tif (!new_sl)\n--\nkernel/bpf/verifier.c=19852=static int do_check_common(struct bpf_verifier_env *env, int subprog)\n--\nkernel/bpf/verifier.c-19864-\nkernel/bpf/verifier.c:19865:\tstate = kzalloc_obj(struct bpf_verifier_state, GFP_KERNEL_ACCOUNT);\nkernel/bpf/verifier.c-19866-\tif (!state)\n"
} |
| 29/3 | 2026/09/18 21:43 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 92153 output: 33 thoughts: 207 Thoughts: **Examining the BPF Verifier State and Auxiliary Data** Okay, so I'm diving into this `kzalloc_obj` call for the `bpf_verifier_state` structure. I know that starts `unwinding` at `false`, and that `bpf_copy_verifier_state` should handle the copy correctly. Good. Now, let's turn our attention to `bpf_insn_aux_data`. This is key for managing exception handling within the BPF verifier. I remember seeing the definition in `include/linux/bpf_verifier.h`. Specifically, I'm interested in those flags: `cleanup_throw_site`, `in_cleanup_pad`, and the `cleanup_pad` index. These flags are critical, as they signal calls to `bpf_throw()`, and manage unwinding/exception handling. But how is `env->insn_aux_data` actually *allocated*? I need to track down where this memory is coming from. Time to check the implementation in `kernel/bpf/verifier.c`. Let's see how that auxiliary data is initialized and managed. |
| 30/3 | 2026/09/18 21:43 | tool | grepper |
0mArgs:{
"Expression": "insn_aux_data = ",
"PathPrefix": "kernel/bpf/"
}Results: {
"Output": "kernel/bpf/fixups.c=347=struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,\n--\nkernel/bpf/fixups.c-361-\nkernel/bpf/fixups.c:362:\t\tenv-\u003einsn_aux_data = new_data;\nkernel/bpf/fixups.c-363-\t}\n"
} |
| 31/3 | 2026/09/18 21:43 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 92760 output: 34 thoughts: 27 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience and written in the first person: **Initial Investigation into `bpf_patch_insn_data` and Allocation** Okay, let's dive into this. My immediate thought is to trace the execution path of `bpf_patch_insn_data`. I want to understand how it interfaces with the initial allocation of `insn_aux_data`. Specifically, I'm trying to get a good sense of the relationship between instruction patching and the auxiliary data structure – are we talking about modifications in place, a fresh allocation on the side, or something more complex? I need to clarify the ownership of `insn_aux_data` through the patching process. Does the patch routine handle the allocation, or does it assume its presence? How do potential race conditions come into play here? I’m looking for any signs of memory corruption or incorrect state. I want to pay attention to synchronization primitives too if there are any. What's the impact if the allocation or data is stale? I also want to verify whether we are handling different architectures correctly. |
| 32/3 | 2026/09/18 21:43 | tool | grepper |
0mArgs:{
"Expression": "insn_aux_data",
"PathPrefix": "kernel/bpf/verifier.c"
}Results: {
"Output": "kernel/bpf/verifier.c=224=static void scalar_min_max_add(struct bpf_reg_state *dst_reg, struct bpf_reg_state *src_reg);\nkernel/bpf/verifier.c-225-\nkernel/bpf/verifier.c:226:static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux,\nkernel/bpf/verifier.c-227-\t\t\t struct bpf_map *map,\n--\nkernel/bpf/verifier.c-235-\nkernel/bpf/verifier.c:236:static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state)\nkernel/bpf/verifier.c-237-{\n--\nkernel/bpf/verifier.c=3287=static void mark_indirect_target(struct bpf_verifier_env *env, int idx)\nkernel/bpf/verifier.c-3288-{\nkernel/bpf/verifier.c:3289:\tenv-\u003einsn_aux_data[idx].indirect_target = true;\nkernel/bpf/verifier.c-3290-}\n--\nkernel/bpf/verifier.c=3292=static void mark_non_stack_access(struct bpf_verifier_env *env, int idx)\nkernel/bpf/verifier.c-3293-{\nkernel/bpf/verifier.c:3294:\tenv-\u003einsn_aux_data[idx].non_stack_access = true;\nkernel/bpf/verifier.c-3295-}\n--\nkernel/bpf/verifier.c=3544=static void check_fastcall_stack_contract(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-3547-\tstruct bpf_subprog_info *subprog = \u0026env-\u003esubprog_info[state-\u003esubprogno];\nkernel/bpf/verifier.c:3548:\tstruct bpf_insn_aux_data *aux = env-\u003einsn_aux_data;\nkernel/bpf/verifier.c-3549-\tint i;\n--\nkernel/bpf/verifier.c=3583=static int check_stack_write_fixed_off(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-3632-\t\tif (sanitize)\nkernel/bpf/verifier.c:3633:\t\t\tenv-\u003einsn_aux_data[insn_idx].nospec_result = true;\nkernel/bpf/verifier.c-3634-\t}\n--\nkernel/bpf/verifier.c=4962=static int __check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,\n--\nkernel/bpf/verifier.c-4981-\t\t} else {\nkernel/bpf/verifier.c:4982:\t\t\tenv-\u003einsn_aux_data[insn_idx].ctx_field_size = info-\u003ectx_field_size;\nkernel/bpf/verifier.c-4983-\t\t}\n--\nkernel/bpf/verifier.c=5044=static int check_sock_access(struct bpf_verifier_env *env, int insn_idx,\n--\nkernel/bpf/verifier.c-5074-\tif (valid) {\nkernel/bpf/verifier.c:5075:\t\tenv-\u003einsn_aux_data[insn_idx].ctx_field_size =\nkernel/bpf/verifier.c-5076-\t\t\tinfo.ctx_field_size;\n--\nkernel/bpf/verifier.c=8270=static int get_bpf_res_spin_lock_kfunc_flags(const struct bpf_call_arg_meta *meta);\nkernel/bpf/verifier.c:8271:static struct bpf_insn_aux_data *cur_aux(const struct bpf_verifier_env *env);\nkernel/bpf/verifier.c-8272-static int process_irq_flag(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c=10727=static int set_map_elem_callback_state(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-10731-{\nkernel/bpf/verifier.c:10732:\tstruct bpf_insn_aux_data *insn_aux = \u0026env-\u003einsn_aux_data[insn_idx];\nkernel/bpf/verifier.c-10733-\tstruct bpf_map *map;\n--\nkernel/bpf/verifier.c=11161=record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,\n--\nkernel/bpf/verifier.c-11163-{\nkernel/bpf/verifier.c:11164:\tstruct bpf_insn_aux_data *aux = \u0026env-\u003einsn_aux_data[insn_idx];\nkernel/bpf/verifier.c-11165-\tstruct bpf_map *map = meta-\u003emap.ptr;\n--\nkernel/bpf/verifier.c=11207=record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,\n--\nkernel/bpf/verifier.c-11209-{\nkernel/bpf/verifier.c:11210:\tstruct bpf_insn_aux_data *aux = \u0026env-\u003einsn_aux_data[insn_idx];\nkernel/bpf/verifier.c-11211-\tstruct bpf_reg_state *reg;\n--\nkernel/bpf/verifier.c=11351=static int check_get_func_ip(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-11371-\nkernel/bpf/verifier.c:11372:static struct bpf_insn_aux_data *cur_aux(const struct bpf_verifier_env *env)\nkernel/bpf/verifier.c-11373-{\nkernel/bpf/verifier.c:11374:\treturn \u0026env-\u003einsn_aux_data[env-\u003einsn_idx];\nkernel/bpf/verifier.c-11375-}\n--\nkernel/bpf/verifier.c=11490=static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-11570-\tif (!in_sleepable_context(env))\nkernel/bpf/verifier.c:11571:\t\tenv-\u003einsn_aux_data[insn_idx].non_sleepable = true;\nkernel/bpf/verifier.c-11572-\n--\nkernel/bpf/verifier.c-11741-\t\t\treturns_cpu_specific_alloc_ptr = true;\nkernel/bpf/verifier.c:11742:\t\t\tenv-\u003einsn_aux_data[insn_idx].call_with_percpu_alloc_ptr = true;\nkernel/bpf/verifier.c-11743-\t\t}\n--\nkernel/bpf/verifier.c=13694=s64 bpf_helper_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-13696-{\nkernel/bpf/verifier.c:13697:\tstruct bpf_insn_aux_data *aux = \u0026env-\u003einsn_aux_data[insn_idx];\nkernel/bpf/verifier.c-13698-\tconst struct bpf_func_proto *fn;\n--\nkernel/bpf/verifier.c=13812=s64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-13814-{\nkernel/bpf/verifier.c:13815:\tstruct bpf_insn_aux_data *aux = \u0026env-\u003einsn_aux_data[insn_idx];\nkernel/bpf/verifier.c-13816-\tstruct bpf_call_arg_meta meta;\n--\nkernel/bpf/verifier.c=13887=static int check_special_kfunc(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,\nkernel/bpf/verifier.c:13888:\t\t\t struct bpf_reg_state *regs, struct bpf_insn_aux_data *insn_aux,\nkernel/bpf/verifier.c-13889-\t\t\t const struct btf_type *ptr_type, struct btf *desc_btf)\n--\nkernel/bpf/verifier.c=14058=static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-14066-\tstruct bpf_call_arg_meta meta;\nkernel/bpf/verifier.c:14067:\tstruct bpf_insn_aux_data *insn_aux;\nkernel/bpf/verifier.c-14068-\tconst char *operation;\n--\nkernel/bpf/verifier.c-14090-\tfunc_name = meta.func_name;\nkernel/bpf/verifier.c:14091:\tinsn_aux = \u0026env-\u003einsn_aux_data[insn_idx];\nkernel/bpf/verifier.c-14092-\n--\nkernel/bpf/verifier.c=14618=static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-14625-\nkernel/bpf/verifier.c:14626:static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux,\nkernel/bpf/verifier.c-14627-\t\t\t\t u32 alu_state, u32 alu_limit)\n--\nkernel/bpf/verifier.c=14643=static int sanitize_val_alu(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-14645-{\nkernel/bpf/verifier.c:14646:\tstruct bpf_insn_aux_data *aux = cur_aux(env);\nkernel/bpf/verifier.c-14647-\n--\nkernel/bpf/verifier.c=14659=struct bpf_sanitize_info {\nkernel/bpf/verifier.c:14660:\tstruct bpf_insn_aux_data aux;\nkernel/bpf/verifier.c-14661-\tbool mask_to_left;\n--\nkernel/bpf/verifier.c=14684=static int sanitize_ptr_alu(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-14691-{\nkernel/bpf/verifier.c:14692:\tstruct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : \u0026info-\u003eaux;\nkernel/bpf/verifier.c-14693-\tstruct bpf_verifier_state *vstate = env-\u003ecur_state;\n--\nkernel/bpf/verifier.c=14780=static void sanitize_mark_insn_seen(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-14789-\tif (!vstate-\u003especulative)\nkernel/bpf/verifier.c:14790:\t\tenv-\u003einsn_aux_data[env-\u003einsn_idx].seen = env-\u003epass_cnt;\nkernel/bpf/verifier.c-14791-}\n--\nkernel/bpf/verifier.c=16056=static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-16074-\tif (dst_reg-\u003etype == PTR_TO_ARENA || (src_reg \u0026\u0026 src_reg-\u003etype == PTR_TO_ARENA)) {\nkernel/bpf/verifier.c:16075:\t\tstruct bpf_insn_aux_data *aux = cur_aux(env);\nkernel/bpf/verifier.c-16076-\n--\nkernel/bpf/verifier.c=17165=static void collect_linked_regs(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-17169-{\nkernel/bpf/verifier.c:17170:\tstruct bpf_insn_aux_data *aux = env-\u003einsn_aux_data;\nkernel/bpf/verifier.c-17171-\tstruct bpf_func_state *func;\n--\nkernel/bpf/verifier.c=17508=static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)\nkernel/bpf/verifier.c-17509-{\nkernel/bpf/verifier.c:17510:\tstruct bpf_insn_aux_data *aux = cur_aux(env);\nkernel/bpf/verifier.c-17511-\tstruct bpf_reg_state *regs = cur_regs(env);\n--\nkernel/bpf/verifier.c=18036=bool bpf_get_call_summary(struct bpf_verifier_env *env, struct bpf_insn *call,\n--\nkernel/bpf/verifier.c-18123- * - look for such patterns;\nkernel/bpf/verifier.c:18124: * - mark spill and fill instructions in env-\u003einsn_aux_data[*].fastcall_pattern;\nkernel/bpf/verifier.c:18125: * - mark set env-\u003einsn_aux_data[*].fastcall_spills_num for call instruction;\nkernel/bpf/verifier.c-18126- * - update env-\u003esubprog_info[*]-\u003efastcall_stack_off to find an offset\n--\nkernel/bpf/verifier.c=18154=static void mark_fastcall_pattern_for_call(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-18215-\t\texpected_regs_mask \u0026= ~BIT(stx-\u003esrc_reg);\nkernel/bpf/verifier.c:18216:\t\tenv-\u003einsn_aux_data[insn_idx - i].fastcall_pattern = 1;\nkernel/bpf/verifier.c:18217:\t\tenv-\u003einsn_aux_data[insn_idx + i].fastcall_pattern = 1;\nkernel/bpf/verifier.c-18218-\t}\n--\nkernel/bpf/verifier.c-18238-\tif (cs.fastcall)\nkernel/bpf/verifier.c:18239:\t\tenv-\u003einsn_aux_data[insn_idx].fastcall_spills_num = i - 1;\nkernel/bpf/verifier.c-18240-\telse\n--\nkernel/bpf/verifier.c=18405=static int save_aux_ptr_type(struct bpf_verifier_env *env, enum bpf_reg_type type,\n--\nkernel/bpf/verifier.c-18407-{\nkernel/bpf/verifier.c:18408:\tenum bpf_reg_type *prev_type = \u0026env-\u003einsn_aux_data[env-\u003einsn_idx].ptr_type;\nkernel/bpf/verifier.c-18409-\n--\nkernel/bpf/verifier.c=18765=static int do_check(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-18775-\t\tstruct bpf_insn *insn;\nkernel/bpf/verifier.c:18776:\t\tstruct bpf_insn_aux_data *insn_aux;\nkernel/bpf/verifier.c-18777-\t\tint err;\n--\nkernel/bpf/verifier.c-18789-\t\tinsn = \u0026insns[env-\u003einsn_idx];\nkernel/bpf/verifier.c:18790:\t\tinsn_aux = \u0026env-\u003einsn_aux_data[env-\u003einsn_idx];\nkernel/bpf/verifier.c-18791-\n--\nkernel/bpf/verifier.c=19054=static int __check_pseudo_btf_id(struct bpf_verifier_env *env,\nkernel/bpf/verifier.c-19055-\t\t\t\t struct bpf_insn *insn,\nkernel/bpf/verifier.c:19056:\t\t\t\t struct bpf_insn_aux_data *aux,\nkernel/bpf/verifier.c-19057-\t\t\t\t struct btf *btf)\n--\nkernel/bpf/verifier.c=19137=static int check_pseudo_btf_id(struct bpf_verifier_env *env,\nkernel/bpf/verifier.c-19138-\t\t\t struct bpf_insn *insn,\nkernel/bpf/verifier.c:19139:\t\t\t struct bpf_insn_aux_data *aux)\nkernel/bpf/verifier.c-19140-{\n--\nkernel/bpf/verifier.c=19602=static int check_and_resolve_insns(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-19622-\t\tif (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {\nkernel/bpf/verifier.c:19623:\t\t\tstruct bpf_insn_aux_data *aux;\nkernel/bpf/verifier.c-19624-\t\t\tstruct bpf_map *map;\n--\nkernel/bpf/verifier.c-19644-\t\t\tif (insn[0].src_reg == BPF_PSEUDO_BTF_ID) {\nkernel/bpf/verifier.c:19645:\t\t\t\taux = \u0026env-\u003einsn_aux_data[i];\nkernel/bpf/verifier.c-19646-\t\t\t\terr = check_pseudo_btf_id(env, insn, aux);\n--\nkernel/bpf/verifier.c-19652-\t\t\tif (insn[0].src_reg == BPF_PSEUDO_FUNC) {\nkernel/bpf/verifier.c:19653:\t\t\t\taux = \u0026env-\u003einsn_aux_data[i];\nkernel/bpf/verifier.c-19654-\t\t\t\taux-\u003eptr_type = PTR_TO_FUNC;\n--\nkernel/bpf/verifier.c-19692-\nkernel/bpf/verifier.c:19693:\t\t\taux = \u0026env-\u003einsn_aux_data[i];\nkernel/bpf/verifier.c-19694-\t\t\taux-\u003emap_index = map_idx;\n--\nkernel/bpf/verifier.c=19793=static void sanitize_dead_code(struct bpf_verifier_env *env)\nkernel/bpf/verifier.c-19794-{\nkernel/bpf/verifier.c:19795:\tstruct bpf_insn_aux_data *aux_data = env-\u003einsn_aux_data;\nkernel/bpf/verifier.c-19796-\tstruct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1);\n--\nkernel/bpf/verifier.c=21168=static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc *desc, int insn_idx)\n--\nkernel/bpf/verifier.c-21203-\t} else if (func_id == special_kfunc_list[KF_bpf_dynptr_from_file]) {\nkernel/bpf/verifier.c:21204:\t\tif (!env-\u003einsn_aux_data[insn_idx].non_sleepable)\nkernel/bpf/verifier.c-21205-\t\t\taddr = (unsigned long)bpf_dynptr_from_file_sleepable;\nkernel/bpf/verifier.c-21206-\t} else if (func_id == special_kfunc_list[KF_bpf_arena_alloc_pages]) {\nkernel/bpf/verifier.c:21207:\t\tif (env-\u003einsn_aux_data[insn_idx].non_sleepable)\nkernel/bpf/verifier.c-21208-\t\t\taddr = (unsigned long)bpf_arena_alloc_pages_non_sleepable;\nkernel/bpf/verifier.c-21209-\t} else if (func_id == special_kfunc_list[KF_bpf_arena_free_pages]) {\nkernel/bpf/verifier.c:21210:\t\tif (env-\u003einsn_aux_data[insn_idx].non_sleepable)\nkernel/bpf/verifier.c-21211-\t\t\taddr = (unsigned long)bpf_arena_free_pages_non_sleepable;\n--\nkernel/bpf/verifier.c-21216-\nkernel/bpf/verifier.c:21217:static void __fixup_collection_insert_kfunc(struct bpf_insn_aux_data *insn_aux,\nkernel/bpf/verifier.c-21218-\t\t\t\t\t u16 struct_meta_reg,\n--\nkernel/bpf/verifier.c=21234=int bpf_fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-21265-\tif (is_bpf_obj_new_kfunc(desc-\u003efunc_id) || is_bpf_percpu_obj_new_kfunc(desc-\u003efunc_id)) {\nkernel/bpf/verifier.c:21266:\t\tstruct btf_struct_meta *kptr_struct_meta = env-\u003einsn_aux_data[insn_idx].kptr_struct_meta;\nkernel/bpf/verifier.c-21267-\t\tstruct bpf_insn addr[2] = { BPF_LD_IMM64(BPF_REG_2, (long)kptr_struct_meta) };\nkernel/bpf/verifier.c:21268:\t\tu64 obj_new_size = env-\u003einsn_aux_data[insn_idx].obj_new_size;\nkernel/bpf/verifier.c-21269-\n--\nkernel/bpf/verifier.c-21283-\t\t is_bpf_refcount_acquire_kfunc(desc-\u003efunc_id)) {\nkernel/bpf/verifier.c:21284:\t\tstruct btf_struct_meta *kptr_struct_meta = env-\u003einsn_aux_data[insn_idx].kptr_struct_meta;\nkernel/bpf/verifier.c-21285-\t\tstruct bpf_insn addr[2] = { BPF_LD_IMM64(BPF_REG_2, (long)kptr_struct_meta) };\n--\nkernel/bpf/verifier.c-21304-\t\t is_bpf_rbtree_add_kfunc(desc-\u003efunc_id)) {\nkernel/bpf/verifier.c:21305:\t\tstruct btf_struct_meta *kptr_struct_meta = env-\u003einsn_aux_data[insn_idx].kptr_struct_meta;\nkernel/bpf/verifier.c-21306-\t\tint struct_meta_reg = BPF_REG_3;\n--\nkernel/bpf/verifier.c-21323-\nkernel/bpf/verifier.c:21324:\t\t__fixup_collection_insert_kfunc(\u0026env-\u003einsn_aux_data[insn_idx], struct_meta_reg,\nkernel/bpf/verifier.c-21325-\t\t\t\t\t\tnode_offset_reg, insn, insn_buf, cnt);\n--\nkernel/bpf/verifier.c-21409-\nkernel/bpf/verifier.c:21410:\tif (env-\u003einsn_aux_data[insn_idx].arg_prog) {\nkernel/bpf/verifier.c:21411:\t\tu32 regno = env-\u003einsn_aux_data[insn_idx].arg_prog;\nkernel/bpf/verifier.c-21412-\t\tstruct bpf_insn ld_addrs[2] = { BPF_LD_IMM64(regno, (long)env-\u003eprog-\u003eaux) };\n--\nkernel/bpf/verifier.c=21580=int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/verifier.c-21647-\nkernel/bpf/verifier.c:21648:\tlen = env-\u003einsn_aux_data_len = env-\u003eprog-\u003elen;\nkernel/bpf/verifier.c:21649:\tenv-\u003einsn_aux_data =\nkernel/bpf/verifier.c:21650:\t\t__vmalloc(array_size(sizeof(struct bpf_insn_aux_data), len),\nkernel/bpf/verifier.c-21651-\t\t\t GFP_KERNEL_ACCOUNT | __GFP_ZERO);\nkernel/bpf/verifier.c-21652-\tret = -ENOMEM;\nkernel/bpf/verifier.c:21653:\tif (!env-\u003einsn_aux_data)\nkernel/bpf/verifier.c-21654-\t\tgoto skip_full_check;\nkernel/bpf/verifier.c-21655-\tfor (i = 0; i \u003c len; i++)\nkernel/bpf/verifier.c:21656:\t\tenv-\u003einsn_aux_data[i].orig_idx = i;\nkernel/bpf/verifier.c-21657-\tenv-\u003esucc = bpf_iarray_realloc(NULL, 2);\n--\nkernel/bpf/verifier.c-21915-err_free_env:\nkernel/bpf/verifier.c:21916:\tif (env-\u003einsn_aux_data)\nkernel/bpf/verifier.c:21917:\t\tbpf_clear_insn_aux_data(env, 0, env-\u003einsn_aux_data_len);\nkernel/bpf/verifier.c:21918:\tvfree(env-\u003einsn_aux_data);\nkernel/bpf/verifier.c-21919-\tkvfree(env-\u003efd_array);\n"
} |
| 33/3 | 2026/09/18 21:43 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 99919 output: 37 thoughts: 82 Thoughts: **Examining the BPF Instruction Data Handling** Okay, so I'm diving into how we manage the `env->insn_aux_data`. I see it's initialized with `__GFP_ZERO`, meaning the memory is zeroed out right from the start. That's good, minimizes potential security vulnerabilities. Now, I need to understand what happens to this data during the patching process, specifically within `adjust_insn_aux_data` and `bpf_patch_insn_data`. My concern is whether there are any potential issues or unexpected behaviors with newly allocated, zeroed memory in these functions. To get a handle on this, I'm going to focus my attention on `kernel/bpf/fixups.c`, around lines 250 through 370. That seems to be the critical area where the allocation and modification of instruction auxiliary data happens. I'll need to carefully analyze the logic here to ensure there are no off-by-one errors, uninitialized variable reads, or other memory safety issues stemming from how this data is handled after initial allocation. I need to be thorough. Let's see what we've got. |
| 34/3 | 2026/09/18 21:44 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/fixups.c",
"FirstLine": 240,
"LineCount": 50
}Results: {
"Contents": " 240:\t\t\t * A non-memory accessing insn could have been replaced by a\n 241:\t\t\t * memory accessing insn, systematically mark it for non-stack\n 242:\t\t\t * access\n 243:\t\t\t */\n 244:\t\t\tdata[off].non_stack_access = bpf_is_mem_insn(insn + off);\n 245:\t\t\treturn;\n 246:\t\t}\n 247:\t\tprog_len = new_prog-\u003elen;\n 248:\t\tenv-\u003einsn_aux_data_len = prog_len;\n 249:\t\n 250:\t\tmemmove(data + off + cnt - 1, data + off,\n 251:\t\t\tsizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));\n 252:\t\tmemset(data + off, 0, sizeof(struct bpf_insn_aux_data) * (cnt - 1));\n 253:\t\tfor (i = off; i \u003c off + cnt - 1; i++) {\n 254:\t\t\t/* Expand insni[off]'s seen count to the patched range. */\n 255:\t\t\tdata[i].seen = old_seen;\n 256:\t\t\tdata[i].zext_dst = bpf_insn_def32(new_prog, insn + i) \u003e= 0;\n 257:\t\t\tdata[i].in_cleanup_pad = data[off + cnt - 1].in_cleanup_pad;\n 258:\t\t\tif (!memcmp(insn + i, original_insn, sizeof(struct bpf_insn))) {\n 259:\t\t\t\tdata[i].non_stack_access =\n 260:\t\t\t\t\tdata[off + cnt - 1].non_stack_access;\n 261:\t\t\t\tdata[off + cnt - 1].non_stack_access = false;\n 262:\t\t\t\tdata[i].cleanup_throw_site =\n 263:\t\t\t\t\tdata[off + cnt - 1].cleanup_throw_site;\n 264:\t\t\t\tdata[off + cnt - 1].cleanup_throw_site = false;\n 265:\t\t\t\tdata[i].cleanup_pad = data[off + cnt - 1].cleanup_pad;\n 266:\t\t\t\tdata[off + cnt - 1].cleanup_pad = 0;\n 267:\t\t\t} else if (bpf_is_mem_insn(insn + i)) {\n 268:\t\t\t\tdata[i].non_stack_access = true;\n 269:\t\t\t}\n 270:\t\t}\n 271:\t\n 272:\t\tif (env-\u003ecleanup_info_cnt)\n 273:\t\t\tfor (i = 0; i \u003c prog_len; i++)\n 274:\t\t\t\tif (data[i].cleanup_pad \u003e off + 1)\n 275:\t\t\t\t\tdata[i].cleanup_pad += cnt - 1;\n 276:\t\n 277:\t\t/*\n 278:\t\t * Last slot instruction could be a newly generated\n 279:\t\t * BPF_ST/BPF_LDX/BPF_STX, systematically mark it for non-stack access\n 280:\t\t * if it is not the original instruction, otherwise keep the\n 281:\t\t * original marking\n 282:\t\t */\n 283:\t\tif (bpf_is_mem_insn(insn + off + cnt - 1) \u0026\u0026\n 284:\t\t memcmp(insn + off + cnt - 1, original_insn, sizeof(struct bpf_insn)))\n 285:\t\t\tdata[off + cnt - 1].non_stack_access = true;\n 286:\t\n 287:\t\t/*\n 288:\t\t * The indirect_target flag of the original instruction was moved to the last of the\n 289:\t\t * new instructions by the above memmove and memset, but the indirect jump target is\n"
} |
| 35/3 | 2026/09/18 21:44 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 101293 output: 37 thoughts: 1080 Thoughts: **Analyzing BPF Cleanup Mechanism** Okay, so I'm diving into this BPF code, specifically the cleanup mechanism. First, I noticed that on line 252, `memset` is used to zero out the memory allocated for `struct bpf_insn_aux_data`. This is important for understanding how the system initializes these auxiliary data structures. Now, I'm checking `verifier_remove_insns` in `kernel/bpf/fixups.c`. It adjusts `cleanup_pad` values in `aux_data` when instructions are removed. If `pad` is greater than `off + cnt`, it reduces `pad` by `cnt`; otherwise, if `pad` is only greater than `off`, it zeros the `cleanup_pad`. I can see how instruction removal potentially affects cleanup pad locations, so this is about adapting the cleanup process to instruction modification. Next, I'm looking at `kernel/bpf/cfg.c`. The `visit_cleanup_pad_edge` function seems to be at the heart of traversing the control flow graph related to cleanup pads. It checks `env->cleanup_info_cnt`, calls `bpf_cleanup_pad_of_call`, marks jump points and targets, and uses a stack (`insn_stack`) and state array (`insn_state`) to explore the CFG. `insn_state` is allocated using `kvzalloc_objs`, ensuring all entries are initially zero. Then, in `kernel/bpf/core.c`, I examined the `__bpf_prog_free` function and the cleanup of `aux` data. `bpf_cleanup_free_info` frees the allocated arrays and the `exc` structure. So this is where the cleanup data gets deallocated when a program is freed. Finally, I'm inspecting `kernel/bpf/helpers.c`, specifically `bpf_run_cleanup_pad` and `bpf_stack_walker`. `bpf_run_cleanup_pad` seems to be the function that actually executes the cleanup operations, calling `arch_bpf_run_cleanup_pad` based on information from `struct bpf_exception_info` (pointed to by `exc`) and the relevant `bpf_cleanup_range` record. It calculates a `spill_base` which is dependent on caller/callee structure and performs the cleanup in the stack space, indicated by `spill_base`. The `bpf_stack_walker` function uses a `struct bpf_throw_ctx` to trace the stack. This looks like a stack unwinding function, walking through stack frames and applying cleanup pads for each BPF subprogram. It sets `ctx->callee` and `ctx->callee_fp` to track the current callee program and its frame pointer. I need to understand more about `struct bpf_throw_ctx`, so I'll check its definition and allocation details in `kernel/bpf/helpers.c` around line 3360. |
| 36/3 | 2026/09/18 21:44 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/helpers.c",
"FirstLine": 3355,
"LineCount": 45
}Results: {
"Contents": "3355:\t{\n3356:\t\trcu_read_unlock();\n3357:\t}\n3358:\t\n3359:\tstruct bpf_throw_ctx {\n3360:\t\tstruct bpf_prog_aux *aux;\n3361:\t\tu64 sp;\n3362:\t\tu64 bp;\n3363:\t\tint cnt;\n3364:\t\tconst struct bpf_prog *callee;\n3365:\t\tu64 callee_fp;\n3366:\t};\n3367:\t\n3368:\tstatic void bpf_run_cleanup_pad(struct bpf_throw_ctx *ctx, const struct bpf_prog *prog,\n3369:\t\t\t\t\tu64 ip, u64 fp)\n3370:\t{\n3371:\t\tconst struct bpf_exception_info *exc = prog-\u003eaux-\u003eexc;\n3372:\t\tconst struct bpf_cleanup_range *rec;\n3373:\t\tu64 spill_base;\n3374:\t\n3375:\t\tif (!exc || !exc-\u003enr_ranges)\n3376:\t\t\treturn;\n3377:\t\trec = bpf_cleanup_pad_for_ip(prog, ip);\n3378:\t\tif (!rec)\n3379:\t\t\treturn;\n3380:\t\n3381:\t\t/*\n3382:\t\t * The callee is always another subprogram of this program -- the walk\n3383:\t\t * ends at any frame that is not one -- so its prologue spilled these\n3384:\t\t * registers and its exc is there to say where.\n3385:\t\t */\n3386:\t\tif (ctx-\u003ecallee)\n3387:\t\t\tspill_base = ctx-\u003ecallee_fp + ctx-\u003ecallee-\u003eaux-\u003eexc-\u003espill_off;\n3388:\t\telse\n3389:\t\t\tspill_base = fp + exc-\u003ethrow_spill_off;\n3390:\t\n3391:\t\tarch_bpf_run_cleanup_pad(rec-\u003epad, fp, spill_base);\n3392:\t}\n3393:\t\n3394:\tstatic bool bpf_stack_walker(void *cookie, u64 ip, u64 sp, u64 bp)\n3395:\t{\n3396:\t\tstruct bpf_throw_ctx *ctx = cookie;\n3397:\t\tstruct bpf_prog *prog;\n3398:\t\n3399:\t\t/*\n"
} |
| 37/3 | 2026/09/18 21:44 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 103724 output: 37 thoughts: 21 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Tracing the Initialization of `bpf_throw_ctx`** Okay, so I need to understand how the `bpf_throw_ctx` structure is initialized. Specifically, I want to find the precise point where the values within it are set when `bpf_throw` is called. This is critical for understanding the data flow and how the BPF infrastructure handles exceptions. I'm assuming there's a struct instantiation somewhere or some setup that defines its contents, and I want to identify that section of code. I'll need to trace the execution path from the `bpf_throw` function call. Let's start with a look at the code. I'm going to look for the function declaration, then trace any function calls and assignments from there. |
| 38/3 | 2026/09/18 21:44 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/helpers.c",
"FirstLine": 3415,
"LineCount": 35
}Results: {
"Contents": "3415:\t\tif (bpf_is_subprog(prog))\n3416:\t\t\treturn true;\n3417:\t\tctx-\u003eaux = prog-\u003eaux;\n3418:\t\tctx-\u003esp = sp;\n3419:\t\tctx-\u003ebp = bp;\n3420:\t\treturn false;\n3421:\t}\n3422:\t\n3423:\t__bpf_kfunc void bpf_throw(u64 cookie)\n3424:\t{\n3425:\t\tstruct bpf_throw_ctx ctx = {};\n3426:\t\n3427:\t\tarch_bpf_stack_walk(bpf_stack_walker, \u0026ctx);\n3428:\t\tWARN_ON_ONCE(!ctx.aux);\n3429:\t\tif (ctx.aux)\n3430:\t\t\tWARN_ON_ONCE(!ctx.aux-\u003eexception_boundary);\n3431:\t\tWARN_ON_ONCE(!ctx.bp);\n3432:\t\tWARN_ON_ONCE(!ctx.cnt);\n3433:\t\t/*\n3434:\t\t * Prevent KASAN false positives for CONFIG_KASAN_STACK by unpoisoning\n3435:\t\t * deeper stack depths than ctx.sp as we do not return from bpf_throw,\n3436:\t\t * which skips compiler generated instrumentation to do the same. Some\n3437:\t\t * architectures cannot recover sp while unwinding, so fall back to bp.\n3438:\t\t */\n3439:\t\tkasan_unpoison_task_stack_below((void *)(long)(ctx.sp ?: ctx.bp));\n3440:\t\tctx.aux-\u003ebpf_exception_cb(cookie, ctx.sp + ctx.aux-\u003estack_arg_sp_adjust, ctx.bp, 0, 0);\n3441:\t\tWARN(1, \"A call to BPF exception callback should never return\\n\");\n3442:\t}\n3443:\t\n3444:\t/*\n3445:\t * Terminator of a compiler-emitted cleanup landing pad. The compiler names\n3446:\t * this _Unwind_Resume, the base unwind ABI's entry point for carrying an\n3447:\t * unwind on once a frame's cleanups have run. To match kernel kfunc\n3448:\t * convention, the kernel calls it bpf_unwind_resume and libbpf maps the\n3449:\t * compiler's name onto it.\n"
} |
| 39/3 | 2026/09/18 21:44 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 104589 output: 358 thoughts: 1238 Thoughts: **KMSAN Assessment: No Need for Dedicated Session** Okay, let's break this down. First, I initialized the `bpf_throw_ctx ctx` struct to zero, good practice. Now, my main concern is whether this patch introduces any new risks around uninitialized memory or information leaks. I carefully went through the checklist. I looked for kernel structures sent to userspace, conditional logic that relies on potentially uninitialized variables, and the allocation of complex data structures where uninitialized fields could be read later. I saw nothing that triggered any of those alarms. The only data copied back to userspace is `min_size`, a 4-byte scalar, which is *fully* initialized. Every variable and struct field in the C code is explicitly initialized. All of the key data structures, like `bpf_cleanup_info`, `bpf_exception_info`, and the various ranges, are zeroed using safe allocation functions like `kzalloc`, `kvzalloc`, etc., or they are systematically populated. Checks for things like bounds and lengths are rigorously performed. I then considered what KASAN and standard checkers would catch. Out-of-bounds offsets, JIT stack spills, frame offsets... if there are any errors like those, KASAN will detect them. And there is an explicit `kasan_unpoison_task_stack_below` call, which already interfaces with KASAN for stack unwinding. Now, KMSAN. The goal here is to catch *reads* of uninitialized memory, and info leaks to user space. I inspected the assembly code in `bpf_cleanup_pad.S`. The assembly code pushes callee-saved registers, restores registers from the spill area, calls the landing pad and returns. Neither the assembly code nor JITed BPF bytecode is instrumented by KMSAN. Also, and crucially, all the kernel data structures that I'm allocating are zeroed out via safe allocation functions like `kzalloc` or `kvcalloc`, and no uninitialized fields are exposed to user space. So, in conclusion, this patch does *not* introduce uninitialized memory risks or kernel-to-user information leaks that would require KMSAN. I then went through my personal checklist: 1. All allocated structures are zeroed. 2. The only data copied to userspace is initialized. 3. The patch handles CFG analysis, verifier checks, and JIT code generation. KASAN is the right test for any potential issues there. 4. Stack offsets, register spills, or JIT jumps would be caught by KASAN. Therefore, `NeedsKMSAN=false` is the right conclusion. KASAN and standard bug detectors are sufficient for this patch. I feel very confident. Time to call `set-results`. |
| 40/3 | 2026/09/18 21:44 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series adds support for BPF exception cleanup landing pads in the BPF verifier and JIT compilers (x86-64 and arm64).\n\nEvaluation against KMSAN criteria:\n1. Kernel data copied to user space: In `check_cleanup_info()`, only `min_size` (a fully initialized 32-bit integer scalar) is conditionally written back to userspace `cleanup_info_rec_size` on `-E2BIG`. No structs with padding or uninitialized memory are copied to user space.\n2. Structure allocations: All newly allocated kernel data structures (`struct bpf_exception_info`, `bpf_cleanup_range`, `bpf_cleanup_info`, scratch DFS buffers, and bitmaps) are allocated using zero-initializing functions (`kzalloc_obj`, `kvcalloc`, `kvzalloc`, `bitmap_zalloc`) or fully populated before use.\n3. JIT and Assembly execution: The landing pad execution routines in `arch/x86/net/bpf_cleanup_pad.S` and JITed BPF bytecode are raw machine code, which is not instrumented by KMSAN.\n4. Verification \u0026 control flow: Any potential bugs introduced by stack frame offset calculations, JIT prologue/epilogue spills, or invalid branch targets will manifest as out-of-bounds accesses or crashes, which are already detected by KASAN.\n\nTherefore, the patch introduces no uninitialized memory usage or info-leak risks detectable only by KMSAN, and a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series adds support for BPF exception cleanup landing pads in the BPF verifier and JIT compilers (x86-64 and arm64).\n\nEvaluation against KMSAN criteria:\n1. Kernel data copied to user space: In `check_cleanup_info()`, only `min_size` (a fully initialized 32-bit integer scalar) is conditionally written back to userspace `cleanup_info_rec_size` on `-E2BIG`. No structs with padding or uninitialized memory are copied to user space.\n2. Structure allocations: All newly allocated kernel data structures (`struct bpf_exception_info`, `bpf_cleanup_range`, `bpf_cleanup_info`, scratch DFS buffers, and bitmaps) are allocated using zero-initializing functions (`kzalloc_obj`, `kvcalloc`, `kvzalloc`, `bitmap_zalloc`) or fully populated before use.\n3. JIT and Assembly execution: The landing pad execution routines in `arch/x86/net/bpf_cleanup_pad.S` and JITed BPF bytecode are raw machine code, which is not instrumented by KMSAN.\n4. Verification \u0026 control flow: Any potential bugs introduced by stack frame offset calculations, JIT prologue/epilogue spills, or invalid branch targets will manifest as out-of-bounds accesses or crashes, which are already detected by KASAN.\n\nTherefore, the patch introduces no uninitialized memory usage or info-leak risks detectable only by KMSAN, and a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|