AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/09/25 10:05 flow patch-triage
1m Args:
null

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "BPF_JIT",
    "UNWINDER_ORC"
  ],
  "FocusSymbols": [
    "bpf_prepare_cleanup_exceptions",
    "bpf_exc_check_insn",
    "bpf_exc_attach_main_prog",
    "bpf_exc_fill_native_ranges",
    "bpf_unwind_resume",
    "arch_bpf_run_cleanup_pad",
    "bpf_throw"
  ],
  "KMSANReasoning": "The patch series adds support for BPF exception cleanup landing pads across the BPF verifier, x86_64 and arm64 JIT compilers, runtime unwinding helpers, and libbpf.\n\nKASAN vs KMSAN applicability:\n- KMSAN detects reads of uninitialized memory and info-leaks to userspace. In this patch series, no uninitialized kernel memory is read or exposed. All new kernel data structures (`struct bpf_exception_info`, `struct bpf_cleanup_range`, `struct bpf_cleanup_info`, `insn_aux_data`) are either allocated with zeroing (`kzalloc_obj`, `kvcalloc`, `__GFP_ZERO`) or completely initialized before use. The only userspace copy added in `check_cleanup_info()` is `copy_to_bpfptr_offset()`, which writes a scalar `u32 min_size` that is fully initialized.\n- The primary risks in this patch are verifier state tracking/CFG analysis correctness, stack frame offset calculations during JIT compilation, and control flow integrity in landing pad execution. Any resulting bugs (such as stack corruption, out-of-bounds stack/heap access, or instruction pointer corruption) are memory corruption / out-of-bounds issues detectable by KASAN and standard detectors, not uninitialized memory issues.\n\nTherefore, a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false,
  "Reasoning": "The patch series introduces BPF exception cleanup landing pads, extending the BPF verifier (CFG analysis, instruction checks, precision tracking, states, and fixups), x86 JIT compilation, and runtime stack unwinding/landing pad execution (via arch_bpf_run_cleanup_pad). This reachable core subsystem logic exposes new userspace parameters in BPF_PROG_LOAD (cleanup_info) and warrants fuzzing.",
  "WorthFuzzing": true
}

1/1 2026/09/25 10:05 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit 75946f2a321a4c32eb3617347c5e1fdd19fa83fd\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Fri Sep 25 10:05: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..729b1ef3e5850\n--- /dev/null\n+++ b/arch/arm64/net/bpf_cleanup_pad.S\n@@ -0,0 +1,94 @@\n+/* SPDX-License-Identifier: GPL-2.0 */\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \u003clinux/linkage.h\u003e\n+\n+/*\n+ * A frame's prologue pushes the tail call counter pair, and then -- once\n+ * bpf_exc_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, #1\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..c7209c26b235a 100644\n--- a/arch/arm64/net/bpf_jit_comp.c\n+++ b/arch/arm64/net/bpf_jit_comp.c\n@@ -10,6 +10,7 @@\n #include \u003clinux/arm-smccc.h\u003e\n #include \u003clinux/bitfield.h\u003e\n #include \u003clinux/bpf.h\u003e\n+#include \u003clinux/bpf_verifier.h\u003e\n #include \u003clinux/cfi.h\u003e\n #include \u003clinux/filter.h\u003e\n #include \u003clinux/memory.h\u003e\n@@ -75,6 +76,19 @@ static const int bpf2a64[] = {\n \t[ARENA_VM_START] = A64_R(28),\n };\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 \tconst struct bpf_prog *prog;\n \tint idx;\n@@ -87,6 +101,8 @@ struct jit_ctx {\n \t__le32 *ro_image;\n \tu32 stack_size;\n \tu16 stack_arg_size;\n+\t/* Bytes reserved for the throw-site spill; 0 unless this frame throws. */\n+\tu32 throw_spill;\n \tu64 user_vm_start;\n \tu64 arena_vm_start;\n \tbool fp_used;\n@@ -432,7 +448,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_exc_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 +482,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_exc_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 +619,22 @@ 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 * which only a frame that throws has, sits below the callee-saved one\n+\t * rather than in the program stack.\n+\t */\n+\tif (bpf_exc_force_spill(prog))\n+\t\tprog-\u003eaux-\u003eexc-\u003espill_off = -(16 + A64_CLEANUP_SPILL_SZ);\n+\tif (bpf_exc_needs_throw_spill(prog)) {\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 +724,9 @@ 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+\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 +1091,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@@ -1367,6 +1406,8 @@ 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_exc_insn_in_pad(env, ctx-\u003eprog, i);\n+\tconst bool pad_head = bpf_exc_insn_is_pad(env, 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 +1419,13 @@ 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) || pad_head)\n \t\temit_bti(A64_BTI_J, ctx);\n \n+\tif (pad_head)\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 +1788,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_exc_insn_is_throw(env, ctx-\u003eprog, i)) {\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_exc_insn_is_resume(env, ctx-\u003eprog, i)) {\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 +1919,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 +2018,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 +2088,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 +2478,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_exc_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 +3460,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..8bb22644ba10b 100644\n--- a/arch/x86/net/Makefile\n+++ b/arch/x86/net/Makefile\n@@ -7,4 +7,7 @@ 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+        ifdef CONFIG_UNWINDER_ORC\n+                obj-$(CONFIG_BPF_JIT) += bpf_cleanup_pad.o\n+        endif\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..10d612b11f36a\n--- /dev/null\n+++ b/arch/x86/net/bpf_cleanup_pad.S\n@@ -0,0 +1,79 @@\n+/* SPDX-License-Identifier: GPL-2.0 */\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\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_exc_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+ * rbp addresses the pad's frame and rsp this one, so the two are not the\n+ * neighbours a JITed frame expects them to be. That is why\n+ * cleanup_check_pad_insn() refuses a call in a pad that passes an argument on\n+ * the stack: the JIT stages those relative to rbp, and the callee reads them\n+ * relative to rsp.\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 $1, %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..26df52050cd72 100644\n--- a/arch/x86/net/bpf_jit_comp.c\n+++ b/arch/x86/net/bpf_jit_comp.c\n@@ -357,6 +357,12 @@ 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+/*\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 +838,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_exc_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 +905,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_exc_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 +1983,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 +2022,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_exc_needs_throw_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 +2034,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_exc_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 +2049,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_exc_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_exc_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 +2074,21 @@ 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 * Only a frame that throws has a throw-site area.\n+\t */\n+\tif (bpf_exc_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+\tif (bpf_exc_needs_throw_spill(bpf_prog))\n+\t\tbpf_prog-\u003eaux-\u003eexc-\u003ethrow_spill_off =\n+\t\t\t-(round_up(stack_depth, 8) + throw_spill);\n \n \t/*\n \t * Allocate outgoing stack arg area for args 7+ only.\n@@ -2110,7 +2135,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_exc_insn_is_pad(env, bpf_prog, i - 1))\n \t\t\tEMIT_ENDBR();\n \n \t\tip = image + addrs[i - 1] + (prog - temp);\n@@ -2903,9 +2929,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_exc_insn_is_throw(env, 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_exc_insn_is_resume(env, bpf_prog, i - 1)) {\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 +2992,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 +3259,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_exc_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 +4430,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_exc_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 +4553,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 e7c5e203edddb..c11dc224973eb 100644\n--- a/include/linux/bpf.h\n+++ b/include/linux/bpf.h\n@@ -1787,6 +1787,93 @@ 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+\tu32 nr_info;\n+\tu32 nr_ranges;\n+\t/* Set if any instruction of this subprogram calls bpf_throw(). */\n+\tbool has_throw;\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_exc_force_spill(const struct bpf_prog *prog);\n+bool bpf_exc_needs_throw_spill(const struct bpf_prog *prog);\n+bool bpf_exc_insn_is_pad(const struct bpf_verifier_env *env,\n+\t\t\t const struct bpf_prog *prog, u32 idx);\n+bool bpf_exc_insn_in_pad(const struct bpf_verifier_env *env,\n+\t\t\t const struct bpf_prog *prog, u32 idx);\n+bool bpf_exc_insn_is_throw(const struct bpf_verifier_env *env,\n+\t\t\t   const struct bpf_prog *prog, u32 idx);\n+bool bpf_exc_insn_is_resume(const struct bpf_verifier_env *env,\n+\t\t\t    const struct bpf_prog *prog, u32 idx);\n+int bpf_exc_attach_main_prog(struct bpf_verifier_env *env, struct bpf_prog *prog);\n+void bpf_exc_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image);\n+void bpf_exc_free_info(struct bpf_prog_aux *aux);\n+#else\n+static inline bool bpf_exc_force_spill(const struct bpf_prog *prog)\n+{\n+\treturn false;\n+}\n+\n+static inline bool bpf_exc_needs_throw_spill(const struct bpf_prog *prog)\n+{\n+\treturn false;\n+}\n+\n+static inline bool bpf_exc_insn_is_pad(const struct bpf_verifier_env *env,\n+\t\t\t\t       const struct bpf_prog *prog, u32 idx)\n+{\n+\treturn false;\n+}\n+\n+static inline bool bpf_exc_insn_in_pad(const struct bpf_verifier_env *env,\n+\t\t\t\t       const struct bpf_prog *prog, u32 idx)\n+{\n+\treturn false;\n+}\n+\n+static inline bool bpf_exc_insn_is_throw(const struct bpf_verifier_env *env,\n+\t\t\t\t\t const struct bpf_prog *prog, u32 idx)\n+{\n+\treturn false;\n+}\n+\n+static inline bool bpf_exc_insn_is_resume(const struct bpf_verifier_env *env,\n+\t\t\t\t\t  const struct bpf_prog *prog, u32 idx)\n+{\n+\treturn false;\n+}\n+\n+static inline int bpf_exc_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_exc_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image)\n+{\n+}\n+\n+static inline void bpf_exc_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@@ -1867,6 +1954,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_verifier.h b/include/linux/bpf_verifier.h\nindex 92f528c456052..500a1e2535799 100644\n--- a/include/linux/bpf_verifier.h\n+++ b/include/linux/bpf_verifier.h\n@@ -429,7 +429,8 @@ struct bpf_jmp_history_entry {\n \tu32 prev_idx : 20;\n \t/* special INSN_F_xxx flags */\n \tu32 flags : 4;\n-\tu32 : 8;\n+\tu32 unwind_frames : 4;\t/* frames popped to reach this landing pad */\n+\tu32 : 4;\n \t/*\n \t * additional registers that need precision tracking when this\n \t * jump is backtracked, vector of five 11-bit records\n@@ -510,6 +511,9 @@ struct bpf_verifier_state {\n \tbool speculative;\n \tbool in_sleepable;\n \n+\tbool unwinding; /* an exception is in flight */\n+\tu8 unwind_frameno; /* the frame whose landing pad is running */\n+\n \t/* first and last insn idx of this verifier state */\n \tu32 first_insn_idx;\n \tu32 last_insn_idx;\n@@ -675,40 +679,51 @@ struct bpf_insn_aux_data {\n \tu64 map_key_state; /* constant (32 bit) key tracking for maps */\n \tint ctx_field_size; /* the ctx field size for load insn, maybe 0 */\n \tu32 seen; /* this insn was processed by the verifier at env-\u003epass_cnt */\n-\tbool nospec; /* do not execute this instruction speculatively */\n-\tbool nospec_result; /* result is unsafe under speculation, nospec must follow */\n-\tbool zext_dst; /* this insn zero extends dst reg */\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 call_with_percpu_alloc_ptr; /* {this,per}_cpu_ptr() with prog percpu alloc */\n-\tu8 alu_state; /* used in combination with alu_limit */\n+\tu64 nospec:1; /* do not execute this instruction speculatively */\n+\tu64 nospec_result:1; /* result is unsafe under speculation, nospec must follow */\n+\tu64 zext_dst:1; /* this insn zero extends dst reg */\n+\tu64 needs_zext:1; /* alu op needs to clear upper bits */\n+\tu64 non_sleepable:1; /* helper/kfunc may be called from non-sleepable context */\n+\tu64 is_iter_next:1; /* bpf_iter_\u003ctype\u003e_next() kfunc call */\n+\tu64 call_with_percpu_alloc_ptr:1; /* {this,per}_cpu_ptr() with prog percpu alloc */\n+\tu64 alu_state:8; /* used in combination with alu_limit */\n \t/* true if STX or LDX instruction is a part of a spill/fill\n \t * pattern for a bpf_fastcall call.\n \t */\n-\tu8 fastcall_pattern:1;\n+\tu64 fastcall_pattern:1;\n \t/* for CALL instructions, a number of spill/fill pairs in the\n \t * bpf_fastcall pattern.\n \t */\n-\tu8 fastcall_spills_num:3;\n-\tu8 arg_prog:4;\n-\n-\t/* below fields are initialized once */\n-\tunsigned int orig_idx; /* original instruction index */\n-\tu32 jmp_point:1;\n-\tu32 prune_point:1;\n+\tu64 fastcall_spills_num:3;\n+\tu64 arg_prog:4;\n+\tu64 throw_call:1; /* call to bpf_throw() */\n+\tu64 resume_call:1; /* call to bpf_unwind_resume() */\n+\tu64 in_cleanup_pad:1; /* runs with an exception in flight, in the pad's frame */\n+\tu64 outside_cleanup_pad:1; /* ... and the other way round */\n+\tu64 cleanup_pad_head:1; /* first insn of a landing pad */\n+\n+\t/* below flags are initialized once */\n+\tu64 jmp_point:1;\n+\tu64 prune_point:1;\n \t/* ensure we check state equivalence and save state checkpoint and\n \t * this instruction, regardless of any heuristics\n \t */\n-\tu32 force_checkpoint:1;\n+\tu64 force_checkpoint:1;\n \t/* true if instruction is a call to a helper function that\n \t * accepts callback function as a parameter.\n \t */\n-\tu32 calls_callback:1;\n-\tu32 indirect_target:1; /* if it is an indirect jump target */\n-\tu32 non_stack_access:1; /* instruction can access non-stack memory */\n+\tu64 calls_callback:1;\n+\tu64 indirect_target:1; /* if it is an indirect jump target */\n+\tu64 non_stack_access:1; /* instruction can access non-stack memory */\n \t/* true if some jump or call instruction targets this instruction */\n-\tu32 jump_target:1;\n+\tu64 jump_target:1;\n+\n+\tunsigned int orig_idx; /* original instruction index, initialized once */\n+\t/*\n+\t * 1 + the instruction index of the exception cleanup landing pad\n+\t * this call site unwinds to, or 0 for none.\n+\t */\n+\tu32 cleanup_pad;\n \t/*\n \t * CFG strongly connected component this instruction belongs to,\n \t * zero if it is a singleton SCC.\n@@ -983,10 +998,14 @@ struct bpf_verifier_env {\n \t} cfg;\n \tstruct backtrack_state bt;\n \tstruct bpf_jmp_history_entry *cur_hist_ent;\n+\tu8 cur_unwind_frames; /* frames popped to reach this insn, if a pad */\n+\tu8 unwind_frames; /* frames popped, staged for the next insn */\n \t/* Per-callsite copy of parent's converged at_stack_in for cross-frame fills. */\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 +1535,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 422284b4fa96f..b0c495e94f740 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 0aaa54359aebc..646aa8758d48d 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@@ -7599,6 +7602,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/backtrack.c b/kernel/bpf/backtrack.c\nindex 507a366dffa47..88da73345f01b 100644\n--- a/kernel/bpf/backtrack.c\n+++ b/kernel/bpf/backtrack.c\n@@ -4,6 +4,7 @@\n #include \u003clinux/bpf_verifier.h\u003e\n #include \u003clinux/filter.h\u003e\n #include \u003clinux/bitmap.h\u003e\n+#include \"exception.h\"\n \n #define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)\n \n@@ -47,6 +48,7 @@ int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state\n \tp-\u003eflags = insn_flags;\n \tp-\u003espi = spi;\n \tp-\u003eframe = frame;\n+\tp-\u003eunwind_frames = env-\u003ecur_unwind_frames;\n \tp-\u003elinked_regs = linked_regs;\n \tcur-\u003ejmp_history_cnt = cnt;\n \tenv-\u003ecur_hist_ent = p;\n@@ -419,10 +421,12 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,\n \t\t\t\t * extra instructions from subprog; the next\n \t\t\t\t * instruction after call to global subprog\n \t\t\t\t * should be literally next instruction in\n-\t\t\t\t * caller program\n+\t\t\t\t * caller program -- or, if the callee threw,\n+\t\t\t\t * the landing pad of this call site\n \t\t\t\t */\n-\t\t\t\tverifier_bug_if(idx + 1 != subseq_idx, env,\n-\t\t\t\t\t\t\"extra insn from subprog\");\n+\t\t\t\tverifier_bug_if(idx + 1 != subseq_idx \u0026\u0026\n+\t\t\t\t\t\tbpf_exc_pad_of_call(env, idx) != subseq_idx,\n+\t\t\t\t\t\tenv, \"extra insn from subprog\");\n \t\t\t\t/* global subprog always sets R0 */\n \t\t\t\tbt_clear_reg(bt, BPF_REG_0);\n \t\t\t\t/* and if it does not set R2, main pass would catch it */\n@@ -823,6 +827,7 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,\n \tstruct bpf_func_state *func;\n \tbool tmp, skip_first = true;\n \tstruct bpf_reg_state *reg;\n+\tu32 pending_unwind = 0;\n \tint i, fr, err;\n \n \tif (!env-\u003ebpf_capable)\n@@ -888,11 +893,19 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,\n \t\t}\n \n \t\tfor (i = last_idx;;) {\n+\t\t\thist = get_jmp_hist_entry(st, history, i);\n+\t\t\t/*\n+\t\t\t * The insn about to be read sits on the far side of an\n+\t\t\t * unwind edge, so enter the frames that edge crossed\n+\t\t\t * before reading it.\n+\t\t\t */\n+\t\t\tfor (; pending_unwind; pending_unwind--)\n+\t\t\t\tif (bt_subprog_enter(bt))\n+\t\t\t\t\treturn -EFAULT;\n \t\t\tif (skip_first) {\n \t\t\t\terr = 0;\n \t\t\t\tskip_first = false;\n \t\t\t} else {\n-\t\t\t\thist = get_jmp_hist_entry(st, history, i);\n \t\t\t\terr = backtrack_insn(env, i, subseq_idx, hist, bt);\n \t\t\t}\n \t\t\tif (err == -ENOTSUPP) {\n@@ -909,6 +922,21 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,\n \t\t\t\t */\n \t\t\t\treturn 0;\n \t\t\tsubseq_idx = i;\n+\t\t\tif (hist \u0026\u0026 hist-\u003eunwind_frames) {\n+\t\t\t\t/*\n+\t\t\t\t * This insn is a landing pad the unwind reached\n+\t\t\t\t * from a throw or a resume with\n+\t\t\t\t * hist-\u003eunwind_frames frames deeper. The walker\n+\t\t\t\t * writes r0 on the way in, so no insn defines\n+\t\t\t\t * it: left set, the request reaches the call\n+\t\t\t\t * that created this frame, where only r1-r5 may\n+\t\t\t\t * still be.\n+\t\t\t\t */\n+\t\t\t\tbt_clear_reg(bt, BPF_REG_0);\n+\t\t\t\tif (bt_empty(bt))\n+\t\t\t\t\treturn 0;\n+\t\t\t\tpending_unwind = hist-\u003eunwind_frames;\n+\t\t\t}\n \t\t\ti = get_prev_insn_idx(st, i, \u0026history);\n \t\t\tif (i == -ENOENT)\n \t\t\t\tbreak;\n@@ -927,6 +955,11 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,\n \t\tif (!st)\n \t\t\tbreak;\n \n+\t\tif (verifier_bug_if(bt-\u003eframe \u003e st-\u003ecurframe, env,\n+\t\t\t\t    \"backtrack frame %d, state curframe %d\",\n+\t\t\t\t    bt-\u003eframe, st-\u003ecurframe))\n+\t\t\treturn -EFAULT;\n+\n \t\tfor (fr = bt-\u003eframe; fr \u003e= 0; fr--) {\n \t\t\tfunc = st-\u003eframe[fr];\n \t\t\tbitmap_from_u64(mask, bt_frame_reg_mask(bt, fr));\ndiff --git a/kernel/bpf/cfg.c b/kernel/bpf/cfg.c\nindex 842c7d1eabccc..1c3b4165e18c0 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,64 @@ 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_exc_pad_of_call(env, t);\n+\tif (w \u003c 0)\n+\t\treturn DONE_EXPLORING;\n+\n+\t/*\n+\t * @t is a call that may branch here, and @w is the target of that\n+\t * branch, so both are prune points. @w especially: every covered call\n+\t * site in a region unwinds to the same pad, and without a prune point\n+\t * at its head the verifier walks the pad again for each of them.\n+\t */\n+\tmark_prune_point(env, t);\n+\tmark_prune_point(env, w);\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 +228,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 +640,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 +696,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 bpf2bpf call and no bpf_throw() -- is reached by nothing.\n+\t * 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..bc569fa5f97f9 100644\n--- a/kernel/bpf/check_btf.c\n+++ b/kernel/bpf/check_btf.c\n@@ -407,6 +407,136 @@ static int check_core_relo(struct bpf_verifier_env *env,\n \treturn err;\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 env-\u003eprog-\u003elen) {\n+\t\tverbose(env, \"cleanup info has %u records for %u instructions\\n\",\n+\t\t\tnrec, env-\u003eprog-\u003elen);\n+\t\treturn -EINVAL;\n+\t}\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_subprog_info *sb, *se, *sl;\n+\t\tstruct bpf_cleanup_info *rec = \u0026krecord[i];\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 = bpf_find_containing_subprog(env, rec-\u003ebegin_off);\n+\t\tse = bpf_find_containing_subprog(env, rec-\u003eend_off - 1);\n+\t\tsl = bpf_find_containing_subprog(env, rec-\u003elanding_pad_off);\n+\t\tif (!sb || !se || !sl) {\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 * A zero opcode is the second half of a 16-byte insn, not an\n+\t\t * insn. end_off is exclusive, so it may be one past the last.\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/* Reject a landing pad inside any call-site range, its own included. */\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 +571,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 227211166dccf..16e5210a56d59 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_exc_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_exc_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_exc_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@@ -3475,6 +3493,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..28aba67888478\n--- /dev/null\n+++ b/kernel/bpf/exception.c\n@@ -0,0 +1,328 @@\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/bsearch.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+BTF_ID_LIST_SINGLE(bpf_unwind_resume_id, func, bpf_unwind_resume)\n+\n+static void mark_kfunc_sites(struct bpf_verifier_env *env)\n+{\n+\tu32 i;\n+\n+\tfor (i = 0; i \u003c env-\u003eprog-\u003elen; i++) {\n+\t\tstruct bpf_insn *insn = \u0026env-\u003eprog-\u003einsnsi[i];\n+\n+\t\tif (bpf_is_throw_kfunc(insn))\n+\t\t\tenv-\u003einsn_aux_data[i].throw_call = true;\n+\t\telse if (bpf_is_unwind_resume_kfunc(insn))\n+\t\t\tenv-\u003einsn_aux_data[i].resume_call = true;\n+\t}\n+}\n+\n+int bpf_exc_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+static void 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+\tmark_kfunc_sites(env);\n+\tmark_call_sites(env);\n+\treturn 0;\n+}\n+\n+int bpf_exc_check_insn(struct bpf_verifier_env *env, struct bpf_insn *insn)\n+{\n+\tstruct bpf_verifier_state *state = env-\u003ecur_state;\n+\tstruct bpf_insn_aux_data *aux;\n+\tu32 i = env-\u003einsn_idx;\n+\tbool in_pad;\n+\n+\taux = \u0026env-\u003einsn_aux_data[i];\n+\tin_pad = state-\u003eunwinding \u0026\u0026 state-\u003ecurframe == state-\u003eunwind_frameno;\n+\n+\tif (in_pad ? aux-\u003eoutside_cleanup_pad : aux-\u003ein_cleanup_pad) {\n+\t\tverbose(env,\n+\t\t\t\"insn %u runs both inside and outside an exception cleanup landing pad\\n\",\n+\t\t\ti);\n+\t\treturn -EINVAL;\n+\t}\n+\tif (in_pad)\n+\t\taux-\u003ein_cleanup_pad = true;\n+\telse\n+\t\taux-\u003eoutside_cleanup_pad = true;\n+\n+\tif (!state-\u003eunwinding)\n+\t\treturn 0;\n+\n+\tif (bpf_is_throw_kfunc(insn)) {\n+\t\tverbose(env,\n+\t\t\t\"bpf_throw() at insn %u throws while an exception is in flight\\n\",\n+\t\t\ti);\n+\t\treturn -EINVAL;\n+\t}\n+\tif (bpf_pseudo_call(insn)) {\n+\t\tint subprog = bpf_find_subprog(env, i + insn-\u003eimm + 1);\n+\n+\t\tif (subprog \u003e= 0 \u0026\u0026 bpf_subprog_is_global(env, subprog) \u0026\u0026\n+\t\t    env-\u003esubprog_info[subprog].might_throw) {\n+\t\t\tverbose(env,\n+\t\t\t\t\"insn %u calls global subprog %d, which can throw while an exception is in flight\\n\",\n+\t\t\t\ti, subprog);\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\t}\n+\n+\tif (!in_pad)\n+\t\treturn 0;\n+\n+\tif (insn-\u003ecode == (BPF_JMP | BPF_EXIT)) {\n+\t\tverbose(env,\n+\t\t\t\"exit at insn %u ends an exception cleanup landing pad: a catch pad is not supported yet, only cleanup pads that resume\\n\",\n+\t\t\ti);\n+\t\treturn -EOPNOTSUPP;\n+\t}\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+\tif (insn-\u003ecode == (BPF_JMP | BPF_JA | BPF_X) ||\n+\t    insn-\u003ecode == (BPF_JMP32 | BPF_JA | BPF_X)) {\n+\t\tverbose(env,\n+\t\t\t\"indirect jump at insn %u is in an exception cleanup landing pad\\n\",\n+\t\t\ti);\n+\t\treturn -EINVAL;\n+\t}\n+\t/* A BPF_LD_[ABS|IND] can leave the frame through its epilogue. */\n+\tif (BPF_CLASS(insn-\u003ecode) == BPF_LD \u0026\u0026\n+\t    (BPF_MODE(insn-\u003ecode) == BPF_ABS || BPF_MODE(insn-\u003ecode) == BPF_IND)) {\n+\t\tverbose(env,\n+\t\t\t\"BPF_LD_[ABS|IND] at insn %u is in an exception cleanup landing pad\\n\",\n+\t\t\ti);\n+\t\treturn -EINVAL;\n+\t}\n+\tif (is_stack_arg_st(insn) || is_stack_arg_stx(insn)) {\n+\t\tverbose(env,\n+\t\t\t\"insn %u stages an on-stack call argument in an exception cleanup landing pad\\n\",\n+\t\t\ti);\n+\t\treturn -EINVAL;\n+\t}\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 calls a kfunc with an on-stack 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+bool bpf_is_unwind_resume_kfunc(const struct bpf_insn *insn)\n+{\n+\treturn bpf_pseudo_kfunc_call(insn) \u0026\u0026 insn-\u003eoff == 0 \u0026\u0026\n+\t       insn-\u003eimm == bpf_unwind_resume_id[0];\n+}\n+\n+int bpf_exc_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_exc_force_spill(const struct bpf_prog *prog)\n+{\n+\treturn prog-\u003eaux-\u003eexc \u0026\u0026 !prog-\u003eaux-\u003eexception_cb;\n+}\n+\n+bool bpf_exc_needs_throw_spill(const struct bpf_prog *prog)\n+{\n+\treturn bpf_exc_force_spill(prog) \u0026\u0026 prog-\u003eaux-\u003eexc-\u003ehas_throw;\n+}\n+\n+const struct bpf_cleanup_range *bpf_exc_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+int bpf_exc_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_exc_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+\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+\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_exc_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_exc_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+\tkfree(exc);\n+\taux-\u003eexc = NULL;\n+}\n+\n+static const struct bpf_insn_aux_data *subprog_insn_aux(const struct bpf_verifier_env *env,\n+\t\t\t\t\t\t\tconst struct bpf_prog *prog, u32 idx)\n+{\n+\tif (!env || !prog-\u003eaux-\u003eexc)\n+\t\treturn NULL;\n+\treturn \u0026env-\u003einsn_aux_data[idx + prog-\u003eaux-\u003esubprog_start];\n+}\n+\n+bool bpf_exc_insn_is_pad(const struct bpf_verifier_env *env,\n+\t\t\t const struct bpf_prog *prog, u32 idx)\n+{\n+\tconst struct bpf_insn_aux_data *aux = subprog_insn_aux(env, prog, idx);\n+\n+\treturn aux \u0026\u0026 aux-\u003ecleanup_pad_head;\n+}\n+\n+bool bpf_exc_insn_is_throw(const struct bpf_verifier_env *env,\n+\t\t\t   const struct bpf_prog *prog, u32 idx)\n+{\n+\tconst struct bpf_insn_aux_data *aux = subprog_insn_aux(env, prog, idx);\n+\n+\treturn aux \u0026\u0026 aux-\u003ethrow_call;\n+}\n+\n+bool bpf_exc_insn_is_resume(const struct bpf_verifier_env *env,\n+\t\t\t    const struct bpf_prog *prog, u32 idx)\n+{\n+\tconst struct bpf_insn_aux_data *aux = subprog_insn_aux(env, prog, idx);\n+\n+\treturn aux \u0026\u0026 aux-\u003eresume_call;\n+}\n+\n+bool bpf_exc_insn_in_pad(const struct bpf_verifier_env *env,\n+\t\t\t const struct bpf_prog *prog, u32 idx)\n+{\n+\tconst struct bpf_insn_aux_data *aux = subprog_insn_aux(env, prog, idx);\n+\n+\treturn aux \u0026\u0026 aux-\u003ein_cleanup_pad;\n+}\ndiff --git a/kernel/bpf/exception.h b/kernel/bpf/exception.h\nnew file mode 100644\nindex 0000000000000..7a4bfc201e1ce\n--- /dev/null\n+++ b/kernel/bpf/exception.h\n@@ -0,0 +1,23 @@\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_insn;\n+struct bpf_verifier_env;\n+\n+int bpf_prepare_cleanup_exceptions(struct bpf_verifier_env *env);\n+int bpf_exc_check_insn(struct bpf_verifier_env *env, struct bpf_insn *insn);\n+int bpf_exc_check_callback(struct bpf_verifier_env *env, int subprog);\n+int bpf_exc_pad_of_call(struct bpf_verifier_env *env, u32 idx);\n+int bpf_exc_alloc_info(struct bpf_prog_aux *aux);\n+int bpf_exc_attach_info(struct bpf_prog_aux *aux, struct bpf_cleanup_info *recs, u32 cnt);\n+const struct bpf_cleanup_range *bpf_exc_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..02e65c57bf0a5 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,29 @@ 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].throw_call =\n+\t\t\t\tdata[off + cnt - 1].throw_call;\n+\t\t\tdata[off + cnt - 1].throw_call = false;\n+\t\t\tdata[i].resume_call =\n+\t\t\t\tdata[off + cnt - 1].resume_call;\n+\t\t\tdata[off + cnt - 1].resume_call = 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 +565,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 +590,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@@ -648,7 +676,7 @@ int bpf_opt_remove_nops(struct bpf_verifier_env *env)\n \tstruct bpf_insn *insn = env-\u003eprog-\u003einsnsi;\n \tint insn_cnt = env-\u003eprog-\u003elen;\n \tbool is_may_goto_0, is_ja;\n-\tint i, err;\n+\tint i, j, err;\n \n \tfor (i = 0; i \u003c insn_cnt; i++) {\n \t\tis_may_goto_0 = !memcmp(\u0026insn[i], \u0026MAY_GOTO_0, sizeof(MAY_GOTO_0));\n@@ -657,6 +685,11 @@ int bpf_opt_remove_nops(struct bpf_verifier_env *env)\n \t\tif (!is_may_goto_0 \u0026\u0026 !is_ja)\n \t\t\tcontinue;\n \n+\t\tif (env-\u003ecleanup_info_cnt)\n+\t\t\tfor (j = 0; j \u003c insn_cnt; j++)\n+\t\t\t\tif (env-\u003einsn_aux_data[j].cleanup_pad == i + 1)\n+\t\t\t\t\tenv-\u003einsn_aux_data[j].cleanup_pad = i + 2;\n+\n \t\terr = verifier_remove_insns(env, i, 1);\n \t\tif (err)\n \t\t\treturn err;\n@@ -1095,6 +1128,61 @@ 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 exc_info_for_subprog(struct bpf_verifier_env *env, struct bpf_prog *sub,\n+\t\t\t\tu32 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_exc_alloc_info(sub-\u003eaux);\n+\tif (err)\n+\t\treturn err;\n+\n+\tfor (i = start; i \u003c end; i++) {\n+\t\tif (env-\u003einsn_aux_data[i].throw_call)\n+\t\t\tsub-\u003eaux-\u003eexc-\u003ehas_throw = true;\n+\t\tif (env-\u003einsn_aux_data[i].cleanup_pad)\n+\t\t\tcnt++;\n+\t}\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\tenv-\u003einsn_aux_data[pad].cleanup_pad_head = true;\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_exc_attach_info(sub-\u003eaux, recs, cnt);\n+}\n+\n+int bpf_exc_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 exc_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 +1320,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 = exc_info_for_subprog(env, func[i], subprog_start,\n+\t\t\t\t\t   subprog_end);\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 +1428,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 +2010,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 (env-\u003einsn_aux_data[i + delta].resume_call)\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 301b35bd85c8a..03af739b99094 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@@ -3398,8 +3399,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_exc_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@@ -3416,6 +3445,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@@ -3445,6 +3479,19 @@ __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+\t/* Never reached: a JIT emits the way out of a landing pad instead. */\n+\tWARN_ONCE(1, \"A JIT should have replaced the exception cleanup resume\\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@@ -5081,6 +5128,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..00ac8917a7166 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_exc_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,13 @@ 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\tif (!alive \u0026\u0026 unlikely(env-\u003ecleanup_info_cnt)) {\n+\t\t\tint pad = bpf_exc_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..b101baa43171c 100644\n--- a/kernel/bpf/states.c\n+++ b/kernel/bpf/states.c\n@@ -996,6 +996,12 @@ 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 (old-\u003eunwinding \u0026\u0026 old-\u003eunwind_frameno != cur-\u003eunwind_frameno)\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 74496fd716d3b..63c84a60c1b7d 100644\n--- a/kernel/bpf/syscall.c\n+++ b/kernel/bpf/syscall.c\n@@ -2924,7 +2924,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 a7c9e2d8965d5..6462e9f2ffdc3 100644\n--- a/kernel/bpf/verifier.c\n+++ b/kernel/bpf/verifier.c\n@@ -37,6 +37,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@@ -1725,6 +1726,8 @@ 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-\u003eunwind_frameno = src-\u003eunwind_frameno;\n \tdst_state-\u003ecurframe = src-\u003ecurframe;\n \tdst_state-\u003ebranches = src-\u003ebranches;\n \tdst_state-\u003eparent = src-\u003eparent;\n@@ -5599,6 +5602,17 @@ 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 lives in r9, which no spill area holds.\n+\t * Refused on every arch rather than just that one. The subprograms\n+\t * below are then checked against MAX_BPF_STACK together rather than\n+\t * one at a time, so this can turn a program that would have loaded\n+\t * with a private stack into one that is too deep.\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@@ -10546,6 +10560,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_exc_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@@ -10597,8 +10615,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@@ -10688,7 +10706,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@@ -11077,6 +11095,20 @@ static bool retval_range_within(struct bpf_retval_range range, const struct bpf_\n \t\treturn range.minval \u003c= reg_smin(reg) \u0026\u0026 reg_smax(reg) \u003c= range.maxval;\n }\n \n+static u32 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+\tstruct bpf_func_state *caller = state-\u003eframe[state-\u003ecurframe - 1];\n+\tu32 callsite = callee-\u003ecallsite;\n+\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 int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)\n {\n \tstruct bpf_verifier_state *state = env-\u003ecur_state, *prev_st;\n@@ -11156,12 +11188,7 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)\n \t\tverbose(env, \"to caller at %d:\\n\", *insn_idx);\n \t\tprint_verifier_state(env, state, caller-\u003eframeno, true);\n \t}\n-\taccount_processed_insns(env, callee, caller);\n-\t/* clear everything in the callee. In case of exceptional exits using\n-\t * bpf_throw, this will be done by copy_verifier_state for extra frames. */\n-\tfree_func_state(callee);\n-\tstate-\u003eframe[state-\u003ecurframe--] = NULL;\n-\tinvalidate_outgoing_stack_args(env, caller);\n+\tpop_frame(env);\n \n \t/* for callbacks widen imprecise scalars to make programs like below verify:\n \t *\n@@ -14660,7 +14687,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@@ -18585,9 +18612,80 @@ 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 void unwind_enter_pad(struct bpf_verifier_env *env)\n+{\n+\tstruct bpf_verifier_state *state = env-\u003ecur_state;\n+\tstruct bpf_func_state *frame = cur_func(env);\n+\n+\tstate-\u003eunwind_frameno = state-\u003ecurframe;\n+\tclear_caller_saved_regs(env, frame-\u003eregs);\n+\tmark_reg_unknown(env, frame-\u003eregs, BPF_REG_0);\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+\tu8 popped = 0;\n+\n+\tstate-\u003eunwinding = true;\n+\tfor (;;) {\n+\t\tint pad = bpf_exc_pad_of_call(env, callsite);\n+\n+\t\tif (pad \u003e= 0) {\n+\t\t\tunwind_enter_pad(env);\n+\t\t\t/*\n+\t\t\t * The edge from @callsite to the pad crosses @popped\n+\t\t\t * frames, and nothing in the instruction stream says\n+\t\t\t * so. Record it for mark_chain_precision(), which has\n+\t\t\t * to walk back through the same frames.\n+\t\t\t */\n+\t\t\tenv-\u003eunwind_frames = popped;\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 = pop_frame(env);\n+\t\tpopped++;\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+\tint err;\n+\n+\tif (!state-\u003eunwinding) {\n+\t\tverbose(env,\n+\t\t\t\"bpf_unwind_resume() at insn %u reached without an exception in flight\\n\",\n+\t\t\t(u32)*insn_idx);\n+\t\treturn -EINVAL;\n+\t}\n+\tif (state-\u003ecurframe != state-\u003eunwind_frameno) {\n+\t\tverbose(env,\n+\t\t\t\"bpf_unwind_resume() at insn %u is in frame %u, not frame %u whose landing pad the exception entered\\n\",\n+\t\t\t(u32)*insn_idx, state-\u003ecurframe, state-\u003eunwind_frameno);\n+\t\treturn -EINVAL;\n+\t}\n+\tif (!state-\u003ecurframe)\n+\t\treturn unwind_finish(env);\n+\terr = unwind_step(env, pop_frame(env), insn_idx);\n+\t/* unwind_step() counted the frames it popped, not the one popped here. */\n+\tif (err == INSN_IDX_UPDATED)\n+\t\tenv-\u003eunwind_frames++;\n+\treturn err;\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@@ -18597,25 +18695,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@@ -18789,6 +18873,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@@ -18822,7 +18908,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@@ -18863,6 +18949,9 @@ static int do_check(struct bpf_verifier_env *env)\n \n \t\t/* reset current history entry on each new instruction */\n \t\tenv-\u003ecur_hist_ent = NULL;\n+\t\t/* frames the unwind popped to reach this insn, if it is a pad */\n+\t\tenv-\u003ecur_unwind_frames = env-\u003eunwind_frames;\n+\t\tenv-\u003eunwind_frames = 0;\n \n \t\tenv-\u003eprev_insn_idx = prev_insn_idx;\n \t\tif (env-\u003einsn_idx \u003e= insn_cnt) {\n@@ -18926,7 +19015,7 @@ static int do_check(struct bpf_verifier_env *env)\n \t\t\t}\n \t\t}\n \n-\t\tif (bpf_is_jmp_point(env, env-\u003einsn_idx)) {\n+\t\tif (bpf_is_jmp_point(env, env-\u003einsn_idx) || env-\u003ecur_unwind_frames) {\n \t\t\terr = bpf_push_jmp_history(env, state, 0, 0, 0, 0);\n \t\t\tif (err)\n \t\t\t\treturn err;\n@@ -18967,6 +19056,12 @@ static int do_check(struct bpf_verifier_env *env)\n \t\t\t\treturn err;\n \t\t}\n \n+\t\tif (unlikely(env-\u003ecleanup_info_cnt)) {\n+\t\t\terr = bpf_exc_check_insn(env, insn);\n+\t\t\tif (err)\n+\t\t\t\treturn err;\n+\t\t}\n+\n \t\tsanitize_mark_insn_seen(env);\n \t\tprev_insn_idx = env-\u003einsn_idx;\n \n@@ -21792,6 +21887,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@@ -21999,6 +22099,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 0aaa54359aebc..646aa8758d48d 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@@ -7599,6 +7602,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 a9de7f107cf7b..03315b3a3a19f 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_rec_size = OPTS_GET(opts, cleanup_info_rec_size, 0);\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+\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 490e8cb4ba537..ec542fd68f631 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..2345fbdd46f56 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,11 @@ 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, if there is one */\n+\tif (load_attr-\u003ecleanup_info_cnt)\n+\t\temit_rel_store(gen, attr_field(prog_load_attr, cleanup_info),\n+\t\t\t       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 cd1ea1bb53cbf..d0e601b3499c0 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,26 @@ 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+/*\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 +722,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@@ -773,6 +801,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@@ -819,7 +850,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@@ -1561,6 +1595,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@@ -4047,6 +4082,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@@ -4074,6 +4112,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@@ -4854,6 +4893,238 @@ 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/*\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[CLEANUP_REC_FIELDS];\n+\t\tvoid *tmp;\n+\n+\t\tif (raw-\u003esec_idx[CLEANUP_REC_BEGIN] == raw-\u003esec_idx[CLEANUP_REC_END] \u0026\u0026\n+\t\t    raw-\u003einsn_idx[CLEANUP_REC_BEGIN] \u003e= raw-\u003einsn_idx[CLEANUP_REC_END]) {\n+\t\t\tpr_warn(\"%s: record %zu is an empty range [%zu,%zu)\\n\",\n+\t\t\t\tCLEANUP_SEC, i, raw-\u003einsn_idx[CLEANUP_REC_BEGIN],\n+\t\t\t\traw-\u003einsn_idx[CLEANUP_REC_END]);\n+\t\t\treturn -LIBBPF_ERRNO__FORMAT;\n+\t\t}\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/*\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 ends at instruction 0\\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/*\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\tci.begin_off = fields[CLEANUP_REC_BEGIN];\n+\t\tci.end_off = fields[CLEANUP_REC_END];\n+\t\tci.landing_pad_off = fields[CLEANUP_REC_PAD];\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+\tif (!prog-\u003ecleanup_info_cnt)\n+\t\treturn 0;\n+\n+\tif (prog-\u003ecleanup_info_cnt \u003e INT32_MAX / sizeof(struct bpf_cleanup_info)) {\n+\t\tpr_warn(\"prog '%s': too many cleanup records: %u\\n\",\n+\t\t\tprog-\u003ename, prog-\u003ecleanup_info_cnt);\n+\t\treturn -LIBBPF_ERRNO__FORMAT;\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@@ -7589,6 +7860,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@@ -7779,6 +8057,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@@ -8051,6 +8332,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@@ -8379,6 +8665,16 @@ static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_s\n \t}\n }\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@@ -8395,12 +8691,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@@ -8634,6 +8930,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@@ -8876,17 +9173,20 @@ 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 *local_name, *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+\tlocal_name = ext-\u003eessent_name ?: ext-\u003ename;\n+\tkern_name = kern_extern_name(local_name);\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\tkern_name != local_name ? kern_name : ext-\u003ename);\n \t\treturn kfunc_id;\n \t}\n \n@@ -9745,6 +10045,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@@ -10143,6 +10446,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..95f2a5243eda6 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@@ -2258,6 +2259,7 @@ static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *ob\n \t\t\tif (ELF64_ST_TYPE(src_sym-\u003est_info) == STT_SECTION) {\n \t\t\t\tstruct src_sec *sec = \u0026obj-\u003esecs[src_sym-\u003est_shndx];\n \t\t\t\tstruct bpf_insn *insn;\n+\t\t\t\t__u32 *val;\n \n \t\t\t\tif (src_linked_sec-\u003eshdr-\u003esh_flags \u0026 SHF_EXECINSTR) {\n \t\t\t\t\t/* calls to the very first static function inside\n@@ -2274,6 +2276,40 @@ 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/*\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+\n+\t\t\t\t\t/*\n+\t\t\t\t\t * Only an executable section has its\n+\t\t\t\t\t * r_offset checked, and even there only\n+\t\t\t\t\t * for alignment; SHT_NOBITS has no\n+\t\t\t\t\t * raw_data at all. Bound it here,\n+\t\t\t\t\t * subtracting so it cannot wrap.\n+\t\t\t\t\t */\n+\t\t\t\t\tif (!dst_linked_sec-\u003eraw_data ||\n+\t\t\t\t\t    dst_linked_sec-\u003esec_sz \u003c (int)sizeof(*val) ||\n+\t\t\t\t\t    dst_rel-\u003er_offset % sizeof(*val) ||\n+\t\t\t\t\t    dst_rel-\u003er_offset \u003e\n+\t\t\t\t\t    (size_t)dst_linked_sec-\u003esec_sz - sizeof(*val)) {\n+\t\t\t\t\t\tpr_warn(\"ELF relo #%d in section #%zu points outside the data of section '%s' in %s\\n\",\n+\t\t\t\t\t\t\tj, src_sec-\u003esec_idx,\n+\t\t\t\t\t\t\tdst_linked_sec-\u003esec_name,\n+\t\t\t\t\t\t\tobj-\u003efilename);\n+\t\t\t\t\t\treturn -EINVAL;\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.skel b/tools/testing/selftests/bpf/Makefile.skel\nindex 580d1d82c1867..6d7518fa4018b 100644\n--- a/tools/testing/selftests/bpf/Makefile.skel\n+++ b/tools/testing/selftests/bpf/Makefile.skel\n@@ -33,7 +33,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..68f2d61a694b5\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/exceptions_cleanup.h\n@@ -0,0 +1,56 @@\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+#define RAN_NOP_PAD\t\t0x20000\n+#define RAN_VAR_STACK\t\t0x40000\n+#define RAN_GLOBAL_PAD\t\t0x80000\n+#define RAN_PAD_R0\t\t0x100000\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..323ee8d72c913\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c\n@@ -0,0 +1,524 @@\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+\t/* bump() is not a landing pad; it sets its bit on every run. */\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 walks past. */\n+\tif (test__start_subtest(\"shared_callee_no_throw\")) {\n+\t\tskel-\u003ebss-\u003eouter_input = 0;\n+\t\trun_shape(skel, skel-\u003eprogs.entry_shared, 1, 2, 0);\n+\t}\n+\tif (test__start_subtest(\"shared_callee_throw\")) {\n+\t\tskel-\u003ebss-\u003eouter_input = 0;\n+\t\trun_shape(skel, skel-\u003eprogs.entry_shared, 101, THROW_COOKIE, RAN_SHARED);\n+\t}\n+\tif (test__start_subtest(\"shared_callee_uncovered_throw\")) {\n+\t\tskel-\u003ebss-\u003eouter_input = 101;\n+\t\trun_shape(skel, skel-\u003eprogs.entry_shared, 1, THROW_COOKIE, 0);\n+\t\tskel-\u003ebss-\u003eouter_input = 0;\n+\t}\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/*\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/*\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 -- though it\n+\t * is reachable, so a walk past the boundary would find it.\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/*\n+\t * A throwing subprog named by a BPF_PSEUDO_FUNC and handed to a\n+\t * bpf_loop() the verifier never reaches: the callback check has to fire\n+\t * on the helper call, not on 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/*\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/*\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/*\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/*\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/*\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/*\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/*\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/*\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+\t/*\n+\t * A pad whose first instruction opt_remove_nops() deletes: the record\n+\t * has to follow the pad rather than be dropped with the nop.\n+\t */\n+\tif (test__start_subtest(\"nop_at_pad_head\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_nop_pad, 101, THROW_COOKIE,\n+\t\t\t  RAN_NOP_PAD);\n+\n+\t/*\n+\t * A pad that indexes its frame's stack by a register the frame set\n+\t * before the throwing call, marked precise back across the unwind edge.\n+\t */\n+\tif (test__start_subtest(\"pad_var_stack_offset\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_var_stack, 101, THROW_COOKIE,\n+\t\t\t  RAN_VAR_STACK);\n+\n+\t/*\n+\t * The same, where the throw is in a global subprogram, which the\n+\t * verifier walks without a frame of its own.\n+\t */\n+\tif (test__start_subtest(\"pad_over_global_subprog\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_global_pad, 101, THROW_COOKIE,\n+\t\t\t  RAN_GLOBAL_PAD);\n+\n+\t/*\n+\t * A pad that indexes its frame by r0, which no insn in the frame\n+\t * wrote: the precision request for it must not cross the unwind edge.\n+\t */\n+\tif (test__start_subtest(\"pad_r0_precision\"))\n+\t\trun_shape(skel, skel-\u003eprogs.entry_pad_r0, 101, THROW_COOKIE,\n+\t\t\t  RAN_PAD_R0);\n+\n+\texceptions_cleanup_shapes__destroy(skel);\n+}\n+\n+/*\n+ * A table with more records than the program has instructions: no valid one\n+ * can look like that, and it is refused before the kernel allocates for it.\n+ */\n+static void test_cleanup_info_cnt(void)\n+{\n+\tstruct bpf_insn insns[] = {\n+\t\tBPF_MOV64_IMM(BPF_REG_0, 0),\n+\t\tBPF_EXIT_INSN(),\n+\t};\n+\tstruct bpf_cleanup_info rec = {\n+\t\t.begin_off = 0,\n+\t\t.end_off = 1,\n+\t\t.landing_pad_off = 1,\n+\t};\n+\tchar log[512] = {};\n+\n+\tLIBBPF_OPTS(bpf_prog_load_opts, opts,\n+\t\t    .log_buf = log,\n+\t\t    .log_size = sizeof(log),\n+\t\t    .log_level = 1,\n+\t\t    .cleanup_info = \u0026rec,\n+\t\t    .cleanup_info_cnt = 1 \u003c\u003c 20,\n+\t\t    .cleanup_info_rec_size = sizeof(rec));\n+\tint fd;\n+\n+\tfd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, \"GPL\",\n+\t\t\t   insns, ARRAY_SIZE(insns), \u0026opts);\n+\tif (!ASSERT_LT(fd, 0, \"load\")) {\n+\t\tclose(fd);\n+\t\treturn;\n+\t}\n+\t/*\n+\t * Turned away on the count, rather than on whatever the records past\n+\t * the one below happen to hold.\n+\t */\n+\tASSERT_HAS_SUBSTR(log, \"cleanup info has 1048576 records for 2 instructions\",\n+\t\t\t  \"log\");\n+}\n+\n+void test_exceptions_cleanup(void)\n+{\n+\tchar log[8192] = {};\n+\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\tprintf(\"%s:SKIP:JIT cannot dispatch exception cleanup landing pads\\n\",\n+\t\t\t       __func__);\n+\t\t\ttest__skip();\n+\t\t} else if (!ASSERT_OK(err, \"load\")) {\n+\t\t\tfprintf(stderr, \"%s\", log);\n+\t\t}\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/*\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+\n+\tif (test__start_subtest(\"cleanup_info_cnt\"))\n+\t\ttest_cleanup_info_cnt();\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..3b79663579122\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup.c\n@@ -0,0 +1,164 @@\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/*\n+\t\t * r0 at pad entry is whatever the walker leaves\n+\t\t * there, carried across the call in a callee-saved\n+\t\t * register and handed to the resume, the way a\n+\t\t * compiler-emitted pad passes the exception pointer to\n+\t\t * _Unwind_Resume. The kfunc ignores it, and the two pads\n+\t\t * below do without the shuffle.\n+\t\t */\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+/*\n+ * A frame with no cleanup record: an exception leaving it runs no pad. The\n+ * throw never fires -- @never is global -- and the bit marks the return path.\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..a7569f98a98de\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_fail.c\n@@ -0,0 +1,678 @@\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. The\n+ * walk takes both, and the exit is refused the same way a catch pad's is.\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(\"a catch pad is not supported yet\")\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(\"throws 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 global subprogram which can throw. Not case 3:\n+ * the verifier does not walk a global subprogram, so the throw inside it is\n+ * never reached, and what refuses the call is subprog_info.might_throw.\n+ */\n+__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 its\n+ * return value: delivery makes the cookie that return value, and fentry has\n+ * 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 the\n+ * way back out of a pad, which in ordinary code leaves a live frame standing\n+ * with its epilogue skipped; no exception is in flight when the walk gets\n+ * there.\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(\"reached without an exception in flight\")\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. An exception is\n+ * in flight, so what refuses it is the frame it is in: a resume reaches the\n+ * walker only from the frame whose pad the walker called.\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 in frame 2, not frame 1 whose landing pad the exception entered\")\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\n+ * not 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\"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(\"calls a kfunc with an on-stack 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. The instruction it lands on would have to run both\n+ * with an exception in flight and without, which the walk refuses.\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(\"runs both inside and outside an exception cleanup landing pad\")\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. An indirect jump in a landing pad: the pad does not own the stack its\n+ * targets would run on. A jump table entry is an offset from the program's\n+ * section symbol, which has to be spelled in quotes here.\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(\"?syscall\")\n+__failure __msg(\"indirect jump at insn\")\n+__naked void gotox_in_pad(void)\n+{\n+\tasm volatile (\n+\t\".pushsection .jumptables,\\\"\\\",@progbits;\"\n+\"jt0_%=:\"\n+\t\".quad l0_%= - \\\"?syscall\\\";\"\n+\t\".quad l1_%= - \\\"?syscall\\\";\"\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/*\n+\t * gotox r2, as raw bytes: the mnemonic only reached the LLVM\n+\t * assembler in llvm 22, and BPF_RAW_INSN() needs \u003clinux/bpf.h\u003e, which\n+\t * vmlinux.h rules out. dst_reg is the other nibble on a big-endian\n+\t * target.\n+\t */\n+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__\n+\t\".byte 0x0d, 0x20, 0, 0, 0, 0, 0, 0;\"\n+#else\n+\t\".byte 0x0d, 0x02, 0, 0, 0, 0, 0, 0;\"\n+#endif\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+/*\n+ * 17. A BPF_LD_[ABS|IND] in a landing pad. A failed load leaves the\n+ * subprogram through the hidden \"r0 = 0; exit\" gen_ld_abs() patches in, and\n+ * that exit is the epilogue, which unwinds a stack the pad does not own.\n+ */\n+static __used __naked __noinline __u64 ld_abs_pad_frame(void)\n+{\n+\tasm volatile (\n+\t\"r6 = r1;\"\t\t\t/* the skb BPF_LD_ABS reads */\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\"r0 = *(u32 *)skb[0];\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t::: __clobber_all);\n+}\n+\n+SEC(\"?tc\")\n+__failure __msg(\"is in an exception cleanup landing pad\")\n+__naked void ld_abs_in_pad(void)\n+{\n+\tasm volatile (\n+\t\"call ld_abs_pad_frame;\"\n+\t\"exit;\"\n+\t::: __clobber_all);\n+}\n+\n+/*\n+ * 18. A bpf_unwind_resume() in a subprogram a landing pad called, which has a\n+ * pad of its own and reached it by ordinary control flow. Not case 9: an\n+ * exception is in flight, but this is not the frame whose pad the walker ran.\n+ */\n+static __used __naked __noinline __u64 own_pad_callee(void)\n+{\n+\tasm volatile (\n+\t\"r1 = %[input] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\t\"if r1 == 0 goto 3f;\"\t\t/* an ordinary branch into its pad */\n+\"1:\"\t\"call bpf_preempt_disable;\"\t/* cleanup region: nothing that throws */\n+\t\"call bpf_preempt_enable;\"\n+\"2:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* its landing pad */\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+static __used __naked __noinline __u64 pad_calls_own_pad_frame(void)\n+{\n+\tasm volatile (\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, which calls the above */\n+\t\"call own_pad_callee;\"\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 in frame 2, not frame 1 whose landing pad the exception entered\")\n+int resume_in_callee_own_pad(void *ctx)\n+{\n+\treturn pad_calls_own_pad_frame();\n+}\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..f44af8be4fda1\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c\n@@ -0,0 +1,1049 @@\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 outer_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: a throw's continuation, the tail\n+ * after a resume with an ld_imm64 and a branch in it, and the block only that\n+ * continuation reaches.\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\n+ * belongs to the call site, not the callee. Either site can throw; the RCU\n+ * lock is taken between them, so only the covered call unwinds holding it.\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 = %[outer_input] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\t\"call shared_callee;\"\t\t/* uncovered: no pad for its unwind */\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(outer_input),\n+\t  __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: the callee\n+ * overwrites r6-r9 before it throws, so the check passes only if the walker\n+ * found the spill in the discarded callee's prologue.\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, which moves the spill the walker reads. The array is left\n+ * 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/*\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 at entry is\n+ * the only place to get a context 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 walker finds the outer bpf_prog's ksym, so the table has\n+ * to be handed over or the pad is never dispatched.\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, 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/*\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/*\n+\t * Unnarrowed, the way entry_freplace hands fr_callee its argument: a\n+\t * guard here would prune the callee's throw and let the sweep take the\n+\t * pad, leaving no record for a walk past the boundary to match.\n+\t */\n+\t\"r2 = *(u64 *)(r1 + 0);\"\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+\"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.\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+/* 8. A throwing subprogram named by a BPF_PSEUDO_FUNC on a path never taken. */\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 throwing frame is both the\n+ * frame the record covers and the boundary, so the pad runs on the way to\n+ * delivering the cookie.\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 it coming, so what stops a nested exception is the\n+ * walk ending in the extension's frame; pad_runs says the pad ran once.\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 last, where the exception\n+ * callback patchlet -- the one that does not keep the call it replaced in the\n+ * last slot -- has to carry the marks with it; r6-r9 reports a lost one.\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. Well formed, and a\n+ * rule against it would turn it 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 reloads from and stores to its own frame -- the shape every\n+ * compiler-generated pad has. A JIT that addresses the frame through the stack\n+ * pointer, arm64, has to find a pad's frame another way.\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.\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: LLVM emits _Unwind_Resume() and\n+ * libbpf maps it onto bpf_unwind_resume(), which every other pad here calls.\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. A tail call in\n+ * the pad itself is refused, but the callee's prologue really did run, so its\n+ * tail call releases exactly that and the target returns into the pad.\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 at entry is\n+ * the only place to get a context for bpf_tail_call(); the pad reloads it\n+ * from the 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 with a table of its\n+ * own, throwing inside the outer exception. The tail call made it a boundary,\n+ * so the inner walk ends there and the outer pad, cookie and r6-r9 survive.\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+/*\n+ * 19. A landing pad whose first instruction is a nop. opt_remove_nops() runs\n+ * long after the cleanup walk, so the record has to follow the pad to the\n+ * instruction that takes its place rather than be dropped with the nop.\n+ */\n+static __used __naked __noinline __u64 nop_pad_frame(void)\n+{\n+\tasm volatile (\n+\t\"r1 = %[input] ll;\"\n+\t\"r6 = *(u64 *)(r1 + 0);\"\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+\"6:\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\"3:\"\t\t\t\t\t/* landing pad: a nop, then its body */\n+\t\"goto +0;\"\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_NOP_PAD),\n+\t  __imm_addr(input), __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+int entry_nop_pad(void *ctx)\n+{\n+\treturn nop_pad_frame();\n+}\n+\n+/*\n+ * 20. A landing pad that indexes its own frame's stack by a register the frame\n+ * set before the throwing call. The offset is not a constant, so r6 has to be\n+ * marked precise from inside the pad, back across the unwind edge.\n+ */\n+/*\n+ * A thrower that touches none of r6-r9, so nothing in it can answer for the\n+ * pad frame's r6 and the walk has to leave this frame to look.\n+ */\n+static __used __naked __noinline __u64 var_thrower(void)\n+{\n+\tasm volatile (\n+\t\"if r1 \u003c 101 goto 1f;\"\n+\t\"r1 = %[cookie];\"\n+\t\"call bpf_throw;\"\n+\"1:\"\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 var_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/* the slot the pad will read... */\n+\t\"*(u64 *)(r10 - 16) = r1;\"\t/* ...whichever of the two it is */\n+\t\"r1 = %[input] ll;\"\n+\t\"r6 = *(u64 *)(r1 + 0);\"\n+\t\"r6 \u0026= 1;\"\t\t\t/* an unknown slot number... */\n+\t\"r6 \u003c\u003c= 3;\"\t\t\t/* ...as an aligned byte offset */\n+\t\"r1 = %[input] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\"1:\"\t\"call var_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\"r7 = r0;\"\n+\t\"r1 = r10;\"\n+\t\"r1 += r6;\"\t\t\t/* variable offset into the frame */\n+\t\"r2 = *(u64 *)(r1 - 16);\"\n+\t\"r3 = %[magic] ll;\"\n+\t\"r3 = *(u64 *)(r3 + 0);\"\n+\t\"if r2 != r3 goto 9f;\"\n+\tPAD_RAN(\"%[ran]\")\n+\"9:\"\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_VAR_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_var_stack(void *ctx)\n+{\n+\treturn var_stack_frame();\n+}\n+\n+/*\n+ * 21. The same walk, over a call to a global subprogram: the verifier enters\n+ * no frame for one, so the exception arrives at the pad from the call site\n+ * rather than from a throw some frames deeper.\n+ */\n+__noinline __u64 global_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 global_pad_frame(void)\n+{\n+\tasm volatile (\n+\t\"r1 = %[magic] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\t\"*(u64 *)(r10 - 8) = r1;\"\n+\t\"*(u64 *)(r10 - 16) = r1;\"\n+\t\"r1 = %[input] ll;\"\n+\t\"r6 = *(u64 *)(r1 + 0);\"\n+\t\"r6 \u0026= 1;\"\n+\t\"r6 \u003c\u003c= 3;\"\n+\t\"r1 = %[input] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\"1:\"\t\"call global_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\"r7 = r0;\"\n+\t\"r1 = r10;\"\n+\t\"r1 += r6;\"\n+\t\"r2 = *(u64 *)(r1 - 16);\"\n+\t\"r3 = %[magic] ll;\"\n+\t\"r3 = *(u64 *)(r3 + 0);\"\n+\t\"if r2 != r3 goto 9f;\"\n+\tPAD_RAN(\"%[ran]\")\n+\"9:\"\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_GLOBAL_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_global_pad(void *ctx)\n+{\n+\treturn global_pad_frame();\n+}\n+\n+/*\n+ * 22. A landing pad that uses r0 as a variable stack offset. No instruction\n+ * in the frame writes r0 -- the walker leaves it on the way in -- so the\n+ * precision that access asks for has to end at the unwind edge, not cross it.\n+ */\n+static __used __naked __noinline __u64 pad_r0_frame(void)\n+{\n+\tasm volatile (\n+\t\"r1 = %[magic] ll;\"\n+\t\"r1 = *(u64 *)(r1 + 0);\"\n+\t\"*(u64 *)(r10 - 8) = r1;\"\t/* the slot the pad will read... */\n+\t\"*(u64 *)(r10 - 16) = r1;\"\t/* ...whichever of the two it is */\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+\t\"r0 \u0026= 1;\"\t\t\t/* an unknown slot number... */\n+\t\"r0 \u003c\u003c= 3;\"\t\t\t/* ...as an aligned byte offset */\n+\t\"r1 = r10;\"\n+\t\"r1 += r0;\"\t\t\t/* variable offset into the frame */\n+\t\"r2 = *(u64 *)(r1 - 16);\"\n+\t\"r3 = %[magic] ll;\"\n+\t\"r3 = *(u64 *)(r3 + 0);\"\n+\t\"if r2 != r3 goto 9f;\"\n+\tPAD_RAN(\"%[ran]\")\n+\"9:\"\n+\t\"call bpf_unwind_resume;\"\n+\t\"exit;\"\n+\tCLEANUP_REC(\"1b\", \"2b\", \"3b\")\n+\t:\n+\t: [ran]\"i\"(RAN_PAD_R0), __imm_addr(magic), __imm_addr(pads_ran)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+int entry_pad_r0(void *ctx)\n+{\n+\treturn pad_r0_frame();\n+}\n+\n+char _license[] SEC(\"license\") = \"GPL\";\n"
}

2/1 2026/09/25 10:05 agent patch-evaluator
0m Model:
core

Args:
null

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "BPF_JIT",
    "UNWINDER_ORC"
  ],
  "FocusSymbols": [
    "bpf_prepare_cleanup_exceptions",
    "bpf_exc_check_insn",
    "bpf_exc_attach_main_prog",
    "bpf_exc_fill_native_ranges",
    "bpf_unwind_resume",
    "arch_bpf_run_cleanup_pad",
    "bpf_throw"
  ],
  "Reasoning": "The patch series introduces BPF exception cleanup landing pads, extending the BPF verifier (CFG analysis, instruction checks, precision tracking, states, and fixups), x86 JIT compilation, and runtime stack unwinding/landing pad execution (via arch_bpf_run_cleanup_pad). This reachable core subsystem logic exposes new userspace parameters in BPF_PROG_LOAD (cleanup_info) and warrants 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 75946f2a321a4c32eb3617347c5e1fdd19fa83fd
Author: syz-cluster <triage@syzkaller.com>
Date:   Fri Sep 25 10:05: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..729b1ef3e5850
--- /dev/null
+++ b/arch/arm64/net/bpf_cleanup_pad.S
@@ -0,0 +1,94 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <linux/linkage.h>
+
+/*
+ * A frame's prologue pushes the tail call counter pair, and then -- once
+ * bpf_exc_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, #1
+
+	/* 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..c7209c26b235a 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -10,6 +10,7 @@
 #include <linux/arm-smccc.h>
 #include <linux/bitfield.h>
 #include <linux/bpf.h>
+#include <linux/bpf_verifier.h>
 #include <linux/cfi.h>
 #include <linux/filter.h>
 #include <linux/memory.h>
@@ -75,6 +76,19 @@ 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 {
 	const struct bpf_prog *prog;
 	int idx;
@@ -87,6 +101,8 @@ struct jit_ctx {
 	__le32 *ro_image;
 	u32 stack_size;
 	u16 stack_arg_size;
+	/* Bytes reserved for the throw-site spill; 0 unless this frame throws. */
+	u32 throw_spill;
 	u64 user_vm_start;
 	u64 arena_vm_start;
 	bool fp_used;
@@ -432,7 +448,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_exc_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 +482,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_exc_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 +619,22 @@ 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,
+	 * which only a frame that throws has, sits below the callee-saved one
+	 * rather than in the program stack.
+	 */
+	if (bpf_exc_force_spill(prog))
+		prog->aux->exc->spill_off = -(16 + A64_CLEANUP_SPILL_SZ);
+	if (bpf_exc_needs_throw_spill(prog)) {
+		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 +724,9 @@ 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);
 
+	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 +1091,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);
@@ -1367,6 +1406,8 @@ 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_exc_insn_in_pad(env, ctx->prog, i);
+	const bool pad_head = bpf_exc_insn_is_pad(env, ctx->prog, i);
 	const bool is64 = BPF_CLASS(code) == BPF_ALU64 ||
 			  BPF_CLASS(code) == BPF_JMP;
 	u8 jmp_cond;
@@ -1378,9 +1419,13 @@ 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) || pad_head)
 		emit_bti(A64_BTI_J, ctx);
 
+	if (pad_head)
+		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 +1788,26 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
 		u64 func_addr;
 		u32 cpu_offset;
 
+		if (bpf_exc_insn_is_throw(env, ctx->prog, i)) {
+			/* 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_exc_insn_is_resume(env, ctx->prog, i)) {
+			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 +1919,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 +2018,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 +2088,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 +2478,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_exc_fill_native_ranges(prog, ctx.offset, ctx.ro_image);
 out_off:
 		if (!ro_header && priv_stack_ptr) {
 			free_percpu(priv_stack_ptr);
@@ -3385,6 +3460,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..8bb22644ba10b 100644
--- a/arch/x86/net/Makefile
+++ b/arch/x86/net/Makefile
@@ -7,4 +7,7 @@ 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
+        ifdef CONFIG_UNWINDER_ORC
+                obj-$(CONFIG_BPF_JIT) += bpf_cleanup_pad.o
+        endif
 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..10d612b11f36a
--- /dev/null
+++ b/arch/x86/net/bpf_cleanup_pad.S
@@ -0,0 +1,79 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <linux/linkage.h>
+#include <asm/nospec-branch.h>
+
+/*
+ * The x86-64 BPF JIT prologue spills, once bpf_exc_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.
+ *
+ * rbp addresses the pad's frame and rsp this one, so the two are not the
+ * neighbours a JITed frame expects them to be. That is why
+ * cleanup_check_pad_insn() refuses a call in a pad that passes an argument on
+ * the stack: the JIT stages those relative to rbp, and the callee reads them
+ * relative to rsp.
+ */
+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 $1, %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..26df52050cd72 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -357,6 +357,12 @@ 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 +838,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_exc_force_spill(bpf_prog)) {
 		pop_callee_regs(&prog, all_callee_regs_used);
 		pop_r12(&prog);
 	} else {
@@ -899,7 +905,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_exc_force_spill(bpf_prog)) {
 		pop_callee_regs(&prog, all_callee_regs_used);
 		pop_r12(&prog);
 	} else {
@@ -1977,6 +1983,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 +2022,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_exc_needs_throw_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 +2034,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_exc_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 +2049,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_exc_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_exc_force_spill(bpf_prog)) {
 		callee_saved_size += 4 * 8; /* rbx, r13, r14, r15 */
 	} else {
 		int j;
@@ -2063,7 +2074,21 @@ 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.
+	 * Only a frame that throws has a throw-site area.
+	 */
+	if (bpf_exc_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);
+	if (bpf_exc_needs_throw_spill(bpf_prog))
+		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 +2135,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_exc_insn_is_pad(env, bpf_prog, i - 1))
 			EMIT_ENDBR();
 
 		ip = image + addrs[i - 1] + (prog - temp);
@@ -2903,9 +2929,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_exc_insn_is_throw(env, 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_exc_insn_is_resume(env, bpf_prog, i - 1)) {
+				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 +2992,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 +3259,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_exc_force_spill(bpf_prog)) {
 				pop_callee_regs(&prog, all_callee_regs_used);
 				pop_r12(&prog);
 			} else {
@@ -4385,6 +4430,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_exc_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 +4553,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 e7c5e203edddb..c11dc224973eb 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1787,6 +1787,93 @@ 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;
+	u32 nr_info;
+	u32 nr_ranges;
+	/* Set if any instruction of this subprogram calls bpf_throw(). */
+	bool has_throw;
+	/* 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_exc_force_spill(const struct bpf_prog *prog);
+bool bpf_exc_needs_throw_spill(const struct bpf_prog *prog);
+bool bpf_exc_insn_is_pad(const struct bpf_verifier_env *env,
+			 const struct bpf_prog *prog, u32 idx);
+bool bpf_exc_insn_in_pad(const struct bpf_verifier_env *env,
+			 const struct bpf_prog *prog, u32 idx);
+bool bpf_exc_insn_is_throw(const struct bpf_verifier_env *env,
+			   const struct bpf_prog *prog, u32 idx);
+bool bpf_exc_insn_is_resume(const struct bpf_verifier_env *env,
+			    const struct bpf_prog *prog, u32 idx);
+int bpf_exc_attach_main_prog(struct bpf_verifier_env *env, struct bpf_prog *prog);
+void bpf_exc_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image);
+void bpf_exc_free_info(struct bpf_prog_aux *aux);
+#else
+static inline bool bpf_exc_force_spill(const struct bpf_prog *prog)
+{
+	return false;
+}
+
+static inline bool bpf_exc_needs_throw_spill(const struct bpf_prog *prog)
+{
+	return false;
+}
+
+static inline bool bpf_exc_insn_is_pad(const struct bpf_verifier_env *env,
+				       const struct bpf_prog *prog, u32 idx)
+{
+	return false;
+}
+
+static inline bool bpf_exc_insn_in_pad(const struct bpf_verifier_env *env,
+				       const struct bpf_prog *prog, u32 idx)
+{
+	return false;
+}
+
+static inline bool bpf_exc_insn_is_throw(const struct bpf_verifier_env *env,
+					 const struct bpf_prog *prog, u32 idx)
+{
+	return false;
+}
+
+static inline bool bpf_exc_insn_is_resume(const struct bpf_verifier_env *env,
+					  const struct bpf_prog *prog, u32 idx)
+{
+	return false;
+}
+
+static inline int bpf_exc_attach_main_prog(struct bpf_verifier_env *env,
+					   struct bpf_prog *prog)
+{
+	return 0;
+}
+
+static inline void bpf_exc_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image)
+{
+}
+
+static inline void bpf_exc_free_info(struct bpf_prog_aux *aux)
+{
+}
+#endif
+
 struct bpf_prog_aux {
 	atomic64_t refcnt;
 	u32 used_map_cnt;
@@ -1867,6 +1954,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_verifier.h b/include/linux/bpf_verifier.h
index 92f528c456052..500a1e2535799 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -429,7 +429,8 @@ struct bpf_jmp_history_entry {
 	u32 prev_idx : 20;
 	/* special INSN_F_xxx flags */
 	u32 flags : 4;
-	u32 : 8;
+	u32 unwind_frames : 4;	/* frames popped to reach this landing pad */
+	u32 : 4;
 	/*
 	 * additional registers that need precision tracking when this
 	 * jump is backtracked, vector of five 11-bit records
@@ -510,6 +511,9 @@ struct bpf_verifier_state {
 	bool speculative;
 	bool in_sleepable;
 
+	bool unwinding; /* an exception is in flight */
+	u8 unwind_frameno; /* the frame whose landing pad is running */
+
 	/* first and last insn idx of this verifier state */
 	u32 first_insn_idx;
 	u32 last_insn_idx;
@@ -675,40 +679,51 @@ struct bpf_insn_aux_data {
 	u64 map_key_state; /* constant (32 bit) key tracking for maps */
 	int ctx_field_size; /* the ctx field size for load insn, maybe 0 */
 	u32 seen; /* this insn was processed by the verifier at env->pass_cnt */
-	bool nospec; /* do not execute this instruction speculatively */
-	bool nospec_result; /* result is unsafe under speculation, nospec must follow */
-	bool zext_dst; /* this insn zero extends dst reg */
-	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 call_with_percpu_alloc_ptr; /* {this,per}_cpu_ptr() with prog percpu alloc */
-	u8 alu_state; /* used in combination with alu_limit */
+	u64 nospec:1; /* do not execute this instruction speculatively */
+	u64 nospec_result:1; /* result is unsafe under speculation, nospec must follow */
+	u64 zext_dst:1; /* this insn zero extends dst reg */
+	u64 needs_zext:1; /* alu op needs to clear upper bits */
+	u64 non_sleepable:1; /* helper/kfunc may be called from non-sleepable context */
+	u64 is_iter_next:1; /* bpf_iter_<type>_next() kfunc call */
+	u64 call_with_percpu_alloc_ptr:1; /* {this,per}_cpu_ptr() with prog percpu alloc */
+	u64 alu_state:8; /* used in combination with alu_limit */
 	/* true if STX or LDX instruction is a part of a spill/fill
 	 * pattern for a bpf_fastcall call.
 	 */
-	u8 fastcall_pattern:1;
+	u64 fastcall_pattern:1;
 	/* for CALL instructions, a number of spill/fill pairs in the
 	 * bpf_fastcall pattern.
 	 */
-	u8 fastcall_spills_num:3;
-	u8 arg_prog:4;
-
-	/* below fields are initialized once */
-	unsigned int orig_idx; /* original instruction index */
-	u32 jmp_point:1;
-	u32 prune_point:1;
+	u64 fastcall_spills_num:3;
+	u64 arg_prog:4;
+	u64 throw_call:1; /* call to bpf_throw() */
+	u64 resume_call:1; /* call to bpf_unwind_resume() */
+	u64 in_cleanup_pad:1; /* runs with an exception in flight, in the pad's frame */
+	u64 outside_cleanup_pad:1; /* ... and the other way round */
+	u64 cleanup_pad_head:1; /* first insn of a landing pad */
+
+	/* below flags are initialized once */
+	u64 jmp_point:1;
+	u64 prune_point:1;
 	/* ensure we check state equivalence and save state checkpoint and
 	 * this instruction, regardless of any heuristics
 	 */
-	u32 force_checkpoint:1;
+	u64 force_checkpoint:1;
 	/* true if instruction is a call to a helper function that
 	 * accepts callback function as a parameter.
 	 */
-	u32 calls_callback:1;
-	u32 indirect_target:1; /* if it is an indirect jump target */
-	u32 non_stack_access:1; /* instruction can access non-stack memory */
+	u64 calls_callback:1;
+	u64 indirect_target:1; /* if it is an indirect jump target */
+	u64 non_stack_access:1; /* instruction can access non-stack memory */
 	/* true if some jump or call instruction targets this instruction */
-	u32 jump_target:1;
+	u64 jump_target:1;
+
+	unsigned int orig_idx; /* original instruction index, initialized once */
+	/*
+	 * 1 + the instruction index of the exception cleanup landing pad
+	 * this call site unwinds to, or 0 for none.
+	 */
+	u32 cleanup_pad;
 	/*
 	 * CFG strongly connected component this instruction belongs to,
 	 * zero if it is a singleton SCC.
@@ -983,10 +998,14 @@ struct bpf_verifier_env {
 	} cfg;
 	struct backtrack_state bt;
 	struct bpf_jmp_history_entry *cur_hist_ent;
+	u8 cur_unwind_frames; /* frames popped to reach this insn, if a pad */
+	u8 unwind_frames; /* frames popped, staged for the next insn */
 	/* Per-callsite copy of parent's converged at_stack_in for cross-frame fills. */
 	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 +1535,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 422284b4fa96f..b0c495e94f740 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 0aaa54359aebc..646aa8758d48d 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 */
@@ -7599,6 +7602,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/backtrack.c b/kernel/bpf/backtrack.c
index 507a366dffa47..88da73345f01b 100644
--- a/kernel/bpf/backtrack.c
+++ b/kernel/bpf/backtrack.c
@@ -4,6 +4,7 @@
 #include <linux/bpf_verifier.h>
 #include <linux/filter.h>
 #include <linux/bitmap.h>
+#include "exception.h"
 
 #define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
 
@@ -47,6 +48,7 @@ int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state
 	p->flags = insn_flags;
 	p->spi = spi;
 	p->frame = frame;
+	p->unwind_frames = env->cur_unwind_frames;
 	p->linked_regs = linked_regs;
 	cur->jmp_history_cnt = cnt;
 	env->cur_hist_ent = p;
@@ -419,10 +421,12 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,
 				 * extra instructions from subprog; the next
 				 * instruction after call to global subprog
 				 * should be literally next instruction in
-				 * caller program
+				 * caller program -- or, if the callee threw,
+				 * the landing pad of this call site
 				 */
-				verifier_bug_if(idx + 1 != subseq_idx, env,
-						"extra insn from subprog");
+				verifier_bug_if(idx + 1 != subseq_idx &&
+						bpf_exc_pad_of_call(env, idx) != subseq_idx,
+						env, "extra insn from subprog");
 				/* global subprog always sets R0 */
 				bt_clear_reg(bt, BPF_REG_0);
 				/* and if it does not set R2, main pass would catch it */
@@ -823,6 +827,7 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
 	struct bpf_func_state *func;
 	bool tmp, skip_first = true;
 	struct bpf_reg_state *reg;
+	u32 pending_unwind = 0;
 	int i, fr, err;
 
 	if (!env->bpf_capable)
@@ -888,11 +893,19 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
 		}
 
 		for (i = last_idx;;) {
+			hist = get_jmp_hist_entry(st, history, i);
+			/*
+			 * The insn about to be read sits on the far side of an
+			 * unwind edge, so enter the frames that edge crossed
+			 * before reading it.
+			 */
+			for (; pending_unwind; pending_unwind--)
+				if (bt_subprog_enter(bt))
+					return -EFAULT;
 			if (skip_first) {
 				err = 0;
 				skip_first = false;
 			} else {
-				hist = get_jmp_hist_entry(st, history, i);
 				err = backtrack_insn(env, i, subseq_idx, hist, bt);
 			}
 			if (err == -ENOTSUPP) {
@@ -909,6 +922,21 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
 				 */
 				return 0;
 			subseq_idx = i;
+			if (hist && hist->unwind_frames) {
+				/*
+				 * This insn is a landing pad the unwind reached
+				 * from a throw or a resume with
+				 * hist->unwind_frames frames deeper. The walker
+				 * writes r0 on the way in, so no insn defines
+				 * it: left set, the request reaches the call
+				 * that created this frame, where only r1-r5 may
+				 * still be.
+				 */
+				bt_clear_reg(bt, BPF_REG_0);
+				if (bt_empty(bt))
+					return 0;
+				pending_unwind = hist->unwind_frames;
+			}
 			i = get_prev_insn_idx(st, i, &history);
 			if (i == -ENOENT)
 				break;
@@ -927,6 +955,11 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
 		if (!st)
 			break;
 
+		if (verifier_bug_if(bt->frame > st->curframe, env,
+				    "backtrack frame %d, state curframe %d",
+				    bt->frame, st->curframe))
+			return -EFAULT;
+
 		for (fr = bt->frame; fr >= 0; fr--) {
 			func = st->frame[fr];
 			bitmap_from_u64(mask, bt_frame_reg_mask(bt, fr));
diff --git a/kernel/bpf/cfg.c b/kernel/bpf/cfg.c
index 842c7d1eabccc..1c3b4165e18c0 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,64 @@ 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_exc_pad_of_call(env, t);
+	if (w < 0)
+		return DONE_EXPLORING;
+
+	/*
+	 * @t is a call that may branch here, and @w is the target of that
+	 * branch, so both are prune points. @w especially: every covered call
+	 * site in a region unwinds to the same pad, and without a prune point
+	 * at its head the verifier walks the pad again for each of them.
+	 */
+	mark_prune_point(env, t);
+	mark_prune_point(env, w);
+	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 +228,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 +640,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 +696,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 bpf2bpf call and no bpf_throw() -- 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..bc569fa5f97f9 100644
--- a/kernel/bpf/check_btf.c
+++ b/kernel/bpf/check_btf.c
@@ -407,6 +407,136 @@ static int check_core_relo(struct bpf_verifier_env *env,
 	return err;
 }
 
+#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 > env->prog->len) {
+		verbose(env, "cleanup info has %u records for %u instructions\n",
+			nrec, env->prog->len);
+		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_subprog_info *sb, *se, *sl;
+		struct bpf_cleanup_info *rec = &krecord[i];
+
+		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 = bpf_find_containing_subprog(env, rec->begin_off);
+		se = bpf_find_containing_subprog(env, rec->end_off - 1);
+		sl = bpf_find_containing_subprog(env, rec->landing_pad_off);
+		if (!sb || !se || !sl) {
+			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;
+		}
+		/*
+		 * A zero opcode is the second half of a 16-byte insn, not an
+		 * insn. end_off is exclusive, so it may be one past the last.
+		 */
+		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 inside any call-site range, its own included. */
+	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 +571,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 227211166dccf..16e5210a56d59 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_exc_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_exc_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_exc_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;
@@ -3475,6 +3493,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..28aba67888478
--- /dev/null
+++ b/kernel/bpf/exception.c
@@ -0,0 +1,328 @@
+// 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/bsearch.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)
+
+BTF_ID_LIST_SINGLE(bpf_unwind_resume_id, func, bpf_unwind_resume)
+
+static void mark_kfunc_sites(struct bpf_verifier_env *env)
+{
+	u32 i;
+
+	for (i = 0; i < env->prog->len; i++) {
+		struct bpf_insn *insn = &env->prog->insnsi[i];
+
+		if (bpf_is_throw_kfunc(insn))
+			env->insn_aux_data[i].throw_call = true;
+		else if (bpf_is_unwind_resume_kfunc(insn))
+			env->insn_aux_data[i].resume_call = true;
+	}
+}
+
+int bpf_exc_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;
+}
+
+static void 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;
+	}
+
+	mark_kfunc_sites(env);
+	mark_call_sites(env);
+	return 0;
+}
+
+int bpf_exc_check_insn(struct bpf_verifier_env *env, struct bpf_insn *insn)
+{
+	struct bpf_verifier_state *state = env->cur_state;
+	struct bpf_insn_aux_data *aux;
+	u32 i = env->insn_idx;
+	bool in_pad;
+
+	aux = &env->insn_aux_data[i];
+	in_pad = state->unwinding && state->curframe == state->unwind_frameno;
+
+	if (in_pad ? aux->outside_cleanup_pad : aux->in_cleanup_pad) {
+		verbose(env,
+			"insn %u runs both inside and outside an exception cleanup landing pad\n",
+			i);
+		return -EINVAL;
+	}
+	if (in_pad)
+		aux->in_cleanup_pad = true;
+	else
+		aux->outside_cleanup_pad = true;
+
+	if (!state->unwinding)
+		return 0;
+
+	if (bpf_is_throw_kfunc(insn)) {
+		verbose(env,
+			"bpf_throw() at insn %u throws while an exception is in flight\n",
+			i);
+		return -EINVAL;
+	}
+	if (bpf_pseudo_call(insn)) {
+		int subprog = bpf_find_subprog(env, i + insn->imm + 1);
+
+		if (subprog >= 0 && bpf_subprog_is_global(env, subprog) &&
+		    env->subprog_info[subprog].might_throw) {
+			verbose(env,
+				"insn %u calls global subprog %d, which can throw while an exception is in flight\n",
+				i, subprog);
+			return -EINVAL;
+		}
+	}
+
+	if (!in_pad)
+		return 0;
+
+	if (insn->code == (BPF_JMP | BPF_EXIT)) {
+		verbose(env,
+			"exit at insn %u ends an exception cleanup landing pad: a catch pad is not supported yet, only cleanup pads that resume\n",
+			i);
+		return -EOPNOTSUPP;
+	}
+	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;
+	}
+	if (insn->code == (BPF_JMP | BPF_JA | BPF_X) ||
+	    insn->code == (BPF_JMP32 | BPF_JA | BPF_X)) {
+		verbose(env,
+			"indirect jump at insn %u is in an exception cleanup landing pad\n",
+			i);
+		return -EINVAL;
+	}
+	/* A BPF_LD_[ABS|IND] can leave the frame through its epilogue. */
+	if (BPF_CLASS(insn->code) == BPF_LD &&
+	    (BPF_MODE(insn->code) == BPF_ABS || BPF_MODE(insn->code) == BPF_IND)) {
+		verbose(env,
+			"BPF_LD_[ABS|IND] at insn %u is in an exception cleanup landing pad\n",
+			i);
+		return -EINVAL;
+	}
+	if (is_stack_arg_st(insn) || is_stack_arg_stx(insn)) {
+		verbose(env,
+			"insn %u stages an on-stack call argument in an exception cleanup landing pad\n",
+			i);
+		return -EINVAL;
+	}
+	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 calls a kfunc with an on-stack argument in an exception cleanup landing pad\n",
+				i);
+			return -EINVAL;
+		}
+	}
+	return 0;
+}
+
+bool bpf_is_unwind_resume_kfunc(const struct bpf_insn *insn)
+{
+	return bpf_pseudo_kfunc_call(insn) && insn->off == 0 &&
+	       insn->imm == bpf_unwind_resume_id[0];
+}
+
+int bpf_exc_pad_of_call(struct bpf_verifier_env *env, u32 idx)
+{
+	u32 pad = env->insn_aux_data[idx].cleanup_pad;
+
+	return pad ? (int)pad - 1 : -1;
+}
+
+/*
+ * 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_exc_force_spill(const struct bpf_prog *prog)
+{
+	return prog->aux->exc && !prog->aux->exception_cb;
+}
+
+bool bpf_exc_needs_throw_spill(const struct bpf_prog *prog)
+{
+	return bpf_exc_force_spill(prog) && prog->aux->exc->has_throw;
+}
+
+const struct bpf_cleanup_range *bpf_exc_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;
+}
+
+int bpf_exc_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_exc_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;
+
+	ranges = kvcalloc(cnt, sizeof(*ranges), GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+	if (!ranges) {
+		kvfree(recs);
+		return -ENOMEM;
+	}
+
+	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_exc_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_exc_free_info(struct bpf_prog_aux *aux)
+{
+	struct bpf_exception_info *exc = aux->exc;
+
+	if (!exc)
+		return;
+	kvfree(exc->ranges);
+	kvfree(exc->info);
+	kfree(exc);
+	aux->exc = NULL;
+}
+
+static const struct bpf_insn_aux_data *subprog_insn_aux(const struct bpf_verifier_env *env,
+							const struct bpf_prog *prog, u32 idx)
+{
+	if (!env || !prog->aux->exc)
+		return NULL;
+	return &env->insn_aux_data[idx + prog->aux->subprog_start];
+}
+
+bool bpf_exc_insn_is_pad(const struct bpf_verifier_env *env,
+			 const struct bpf_prog *prog, u32 idx)
+{
+	const struct bpf_insn_aux_data *aux = subprog_insn_aux(env, prog, idx);
+
+	return aux && aux->cleanup_pad_head;
+}
+
+bool bpf_exc_insn_is_throw(const struct bpf_verifier_env *env,
+			   const struct bpf_prog *prog, u32 idx)
+{
+	const struct bpf_insn_aux_data *aux = subprog_insn_aux(env, prog, idx);
+
+	return aux && aux->throw_call;
+}
+
+bool bpf_exc_insn_is_resume(const struct bpf_verifier_env *env,
+			    const struct bpf_prog *prog, u32 idx)
+{
+	const struct bpf_insn_aux_data *aux = subprog_insn_aux(env, prog, idx);
+
+	return aux && aux->resume_call;
+}
+
+bool bpf_exc_insn_in_pad(const struct bpf_verifier_env *env,
+			 const struct bpf_prog *prog, u32 idx)
+{
+	const struct bpf_insn_aux_data *aux = subprog_insn_aux(env, prog, idx);
+
+	return aux && aux->in_cleanup_pad;
+}
diff --git a/kernel/bpf/exception.h b/kernel/bpf/exception.h
new file mode 100644
index 0000000000000..7a4bfc201e1ce
--- /dev/null
+++ b/kernel/bpf/exception.h
@@ -0,0 +1,23 @@
+/* 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_insn;
+struct bpf_verifier_env;
+
+int bpf_prepare_cleanup_exceptions(struct bpf_verifier_env *env);
+int bpf_exc_check_insn(struct bpf_verifier_env *env, struct bpf_insn *insn);
+int bpf_exc_check_callback(struct bpf_verifier_env *env, int subprog);
+int bpf_exc_pad_of_call(struct bpf_verifier_env *env, u32 idx);
+int bpf_exc_alloc_info(struct bpf_prog_aux *aux);
+int bpf_exc_attach_info(struct bpf_prog_aux *aux, struct bpf_cleanup_info *recs, u32 cnt);
+const struct bpf_cleanup_range *bpf_exc_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..02e65c57bf0a5 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,29 @@ 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].throw_call =
+				data[off + cnt - 1].throw_call;
+			data[off + cnt - 1].throw_call = false;
+			data[i].resume_call =
+				data[off + cnt - 1].resume_call;
+			data[off + cnt - 1].resume_call = 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 +565,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 +590,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;
 }
 
@@ -648,7 +676,7 @@ int bpf_opt_remove_nops(struct bpf_verifier_env *env)
 	struct bpf_insn *insn = env->prog->insnsi;
 	int insn_cnt = env->prog->len;
 	bool is_may_goto_0, is_ja;
-	int i, err;
+	int i, j, err;
 
 	for (i = 0; i < insn_cnt; i++) {
 		is_may_goto_0 = !memcmp(&insn[i], &MAY_GOTO_0, sizeof(MAY_GOTO_0));
@@ -657,6 +685,11 @@ int bpf_opt_remove_nops(struct bpf_verifier_env *env)
 		if (!is_may_goto_0 && !is_ja)
 			continue;
 
+		if (env->cleanup_info_cnt)
+			for (j = 0; j < insn_cnt; j++)
+				if (env->insn_aux_data[j].cleanup_pad == i + 1)
+					env->insn_aux_data[j].cleanup_pad = i + 2;
+
 		err = verifier_remove_insns(env, i, 1);
 		if (err)
 			return err;
@@ -1095,6 +1128,61 @@ 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 exc_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_exc_alloc_info(sub->aux);
+	if (err)
+		return err;
+
+	for (i = start; i < end; i++) {
+		if (env->insn_aux_data[i].throw_call)
+			sub->aux->exc->has_throw = true;
+		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;
+		}
+		env->insn_aux_data[pad].cleanup_pad_head = true;
+		recs[cnt].begin_off = i - start;
+		recs[cnt].end_off = i - start + 1;
+		recs[cnt].landing_pad_off = pad - start;
+		cnt++;
+	}
+	return bpf_exc_attach_info(sub->aux, recs, cnt);
+}
+
+int bpf_exc_attach_main_prog(struct bpf_verifier_env *env, struct bpf_prog *prog)
+{
+	if (!env || env->subprog_cnt > 1)
+		return 0;
+	return exc_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 +1320,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 = exc_info_for_subprog(env, func[i], subprog_start,
+					   subprog_end);
+		if (err)
+			goto out_free;
 		func[i] = bpf_int_jit_compile(env, func[i]);
 		if (!func[i]->jited) {
 			err = -ENOTSUPP;
@@ -1336,6 +1428,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 +2010,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 (env->insn_aux_data[i + delta].resume_call)
+			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 301b35bd85c8a..03af739b99094 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
@@ -3398,8 +3399,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_exc_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;
@@ -3416,6 +3445,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;
@@ -3445,6 +3479,19 @@ __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)
+{
+	/* Never reached: a JIT emits the way out of a landing pad instead. */
+	WARN_ONCE(1, "A JIT should have replaced the exception cleanup resume\n");
+}
+
 __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;
@@ -5081,6 +5128,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..00ac8917a7166 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_exc_pad_of_call(env, idx);
+
+		if (pad >= 0)
+			succ->items[succ->cnt++] = pad;
+	}
+
 	return succ;
 }
 
@@ -397,6 +409,13 @@ 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);
+
+		if (!alive && unlikely(env->cleanup_info_cnt)) {
+			int pad = bpf_exc_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..b101baa43171c 100644
--- a/kernel/bpf/states.c
+++ b/kernel/bpf/states.c
@@ -996,6 +996,12 @@ 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 (old->unwinding && old->unwind_frameno != cur->unwind_frameno)
+		return false;
+
 	if (!refsafe(old, cur, &env->idmap_scratch))
 		return false;
 
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 74496fd716d3b..63c84a60c1b7d 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2924,7 +2924,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 a7c9e2d8965d5..6462e9f2ffdc3 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -37,6 +37,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) \
@@ -1725,6 +1726,8 @@ 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->unwind_frameno = src->unwind_frameno;
 	dst_state->curframe = src->curframe;
 	dst_state->branches = src->branches;
 	dst_state->parent = src->parent;
@@ -5599,6 +5602,17 @@ 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 lives in r9, which no spill area holds.
+	 * Refused on every arch rather than just that one. The subprograms
+	 * below are then checked against MAX_BPF_STACK together rather than
+	 * one at a time, so this can turn a program that would have loaded
+	 * with a private stack into one that is too deep.
+	 */
+	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);
 
@@ -10546,6 +10560,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_exc_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",
@@ -10597,8 +10615,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)
@@ -10688,7 +10706,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 */
@@ -11077,6 +11095,20 @@ static bool retval_range_within(struct bpf_retval_range range, const struct bpf_
 		return range.minval <= reg_smin(reg) && reg_smax(reg) <= range.maxval;
 }
 
+static u32 pop_frame(struct bpf_verifier_env *env)
+{
+	struct bpf_verifier_state *state = env->cur_state;
+	struct bpf_func_state *callee = state->frame[state->curframe];
+	struct bpf_func_state *caller = state->frame[state->curframe - 1];
+	u32 callsite = callee->callsite;
+
+	account_processed_insns(env, callee, caller);
+	free_func_state(callee);
+	state->frame[state->curframe--] = NULL;
+	invalidate_outgoing_stack_args(env, caller);
+	return callsite;
+}
+
 static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
 {
 	struct bpf_verifier_state *state = env->cur_state, *prev_st;
@@ -11156,12 +11188,7 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
 		verbose(env, "to caller at %d:\n", *insn_idx);
 		print_verifier_state(env, state, caller->frameno, true);
 	}
-	account_processed_insns(env, callee, caller);
-	/* clear everything in the callee. In case of exceptional exits using
-	 * bpf_throw, this will be done by copy_verifier_state for extra frames. */
-	free_func_state(callee);
-	state->frame[state->curframe--] = NULL;
-	invalidate_outgoing_stack_args(env, caller);
+	pop_frame(env);
 
 	/* for callbacks widen imprecise scalars to make programs like below verify:
 	 *
@@ -14660,7 +14687,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;
 }
@@ -18585,9 +18612,80 @@ enum {
 	INSN_IDX_UPDATED = 2,
 };
 
-static int process_bpf_exit_full(struct bpf_verifier_env *env,
-				 bool *do_print_state,
-				 bool exception_exit)
+static void unwind_enter_pad(struct bpf_verifier_env *env)
+{
+	struct bpf_verifier_state *state = env->cur_state;
+	struct bpf_func_state *frame = cur_func(env);
+
+	state->unwind_frameno = state->curframe;
+	clear_caller_saved_regs(env, frame->regs);
+	mark_reg_unknown(env, frame->regs, BPF_REG_0);
+}
+
+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;
+	u8 popped = 0;
+
+	state->unwinding = true;
+	for (;;) {
+		int pad = bpf_exc_pad_of_call(env, callsite);
+
+		if (pad >= 0) {
+			unwind_enter_pad(env);
+			/*
+			 * The edge from @callsite to the pad crosses @popped
+			 * frames, and nothing in the instruction stream says
+			 * so. Record it for mark_chain_precision(), which has
+			 * to walk back through the same frames.
+			 */
+			env->unwind_frames = popped;
+			*insn_idx = pad;
+			return INSN_IDX_UPDATED;
+		}
+		if (!state->curframe)
+			return unwind_finish(env);
+		callsite = pop_frame(env);
+		popped++;
+	}
+}
+
+static int process_cleanup_resume(struct bpf_verifier_env *env, int *insn_idx)
+{
+	struct bpf_verifier_state *state = env->cur_state;
+	int err;
+
+	if (!state->unwinding) {
+		verbose(env,
+			"bpf_unwind_resume() at insn %u reached without an exception in flight\n",
+			(u32)*insn_idx);
+		return -EINVAL;
+	}
+	if (state->curframe != state->unwind_frameno) {
+		verbose(env,
+			"bpf_unwind_resume() at insn %u is in frame %u, not frame %u whose landing pad the exception entered\n",
+			(u32)*insn_idx, state->curframe, state->unwind_frameno);
+		return -EINVAL;
+	}
+	if (!state->curframe)
+		return unwind_finish(env);
+	err = unwind_step(env, pop_frame(env), insn_idx);
+	/* unwind_step() counted the frames it popped, not the one popped here. */
+	if (err == INSN_IDX_UPDATED)
+		env->unwind_frames++;
+	return err;
+}
+
+static int process_bpf_exit_full(struct bpf_verifier_env *env, bool *do_print_state)
 {
 	struct bpf_func_state *cur_frame = cur_func(env);
 
@@ -18597,25 +18695,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);
@@ -18789,6 +18873,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 &&
@@ -18822,7 +18908,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);
 	}
@@ -18863,6 +18949,9 @@ static int do_check(struct bpf_verifier_env *env)
 
 		/* reset current history entry on each new instruction */
 		env->cur_hist_ent = NULL;
+		/* frames the unwind popped to reach this insn, if it is a pad */
+		env->cur_unwind_frames = env->unwind_frames;
+		env->unwind_frames = 0;
 
 		env->prev_insn_idx = prev_insn_idx;
 		if (env->insn_idx >= insn_cnt) {
@@ -18926,7 +19015,7 @@ static int do_check(struct bpf_verifier_env *env)
 			}
 		}
 
-		if (bpf_is_jmp_point(env, env->insn_idx)) {
+		if (bpf_is_jmp_point(env, env->insn_idx) || env->cur_unwind_frames) {
 			err = bpf_push_jmp_history(env, state, 0, 0, 0, 0);
 			if (err)
 				return err;
@@ -18967,6 +19056,12 @@ static int do_check(struct bpf_verifier_env *env)
 				return err;
 		}
 
+		if (unlikely(env->cleanup_info_cnt)) {
+			err = bpf_exc_check_insn(env, insn);
+			if (err)
+				return err;
+		}
+
 		sanitize_mark_insn_seen(env);
 		prev_insn_idx = env->insn_idx;
 
@@ -21792,6 +21887,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)
@@ -21999,6 +22099,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 0aaa54359aebc..646aa8758d48d 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 */
@@ -7599,6 +7602,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 a9de7f107cf7b..03315b3a3a19f 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_rec_size = OPTS_GET(opts, cleanup_info_rec_size, 0);
+	attr.cleanup_info = ptr_to_u64(OPTS_GET(opts, cleanup_info, NULL));
+	attr.cleanup_info_cnt = OPTS_GET(opts, cleanup_info_cnt, 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 490e8cb4ba537..ec542fd68f631 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..2345fbdd46f56 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,11 @@ 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, if there is one */
+	if (load_attr->cleanup_info_cnt)
+		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 cd1ea1bb53cbf..d0e601b3499c0 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,26 @@ 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 +722,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;
@@ -773,6 +801,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;
@@ -819,7 +850,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;
@@ -1561,6 +1595,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;
 
@@ -4047,6 +4082,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;
@@ -4074,6 +4112,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,
@@ -4854,6 +4893,238 @@ 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[CLEANUP_REC_FIELDS];
+		void *tmp;
+
+		if (raw->sec_idx[CLEANUP_REC_BEGIN] == raw->sec_idx[CLEANUP_REC_END] &&
+		    raw->insn_idx[CLEANUP_REC_BEGIN] >= raw->insn_idx[CLEANUP_REC_END]) {
+			pr_warn("%s: record %zu is an empty range [%zu,%zu)\n",
+				CLEANUP_SEC, i, raw->insn_idx[CLEANUP_REC_BEGIN],
+				raw->insn_idx[CLEANUP_REC_END]);
+			return -LIBBPF_ERRNO__FORMAT;
+		}
+
+		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 ends at instruction 0\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;
+
+		ci.begin_off = fields[CLEANUP_REC_BEGIN];
+		ci.end_off = fields[CLEANUP_REC_END];
+		ci.landing_pad_off = fields[CLEANUP_REC_PAD];
+
+		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);
+	}
+
+	if (!prog->cleanup_info_cnt)
+		return 0;
+
+	if (prog->cleanup_info_cnt > INT32_MAX / sizeof(struct bpf_cleanup_info)) {
+		pr_warn("prog '%s': too many cleanup records: %u\n",
+			prog->name, prog->cleanup_info_cnt);
+		return -LIBBPF_ERRNO__FORMAT;
+	}
+
+	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)
 {
@@ -7589,6 +7860,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];
@@ -7779,6 +8057,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)
@@ -8051,6 +8332,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;
@@ -8379,6 +8665,16 @@ 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;
@@ -8395,12 +8691,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;
@@ -8634,6 +8930,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;
 
@@ -8876,17 +9173,20 @@ 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 *local_name, *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);
+	local_name = ext->essent_name ?: ext->name;
+	kern_name = kern_extern_name(local_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);
+			kern_name != local_name ? kern_name : ext->name);
 		return kfunc_id;
 	}
 
@@ -9745,6 +10045,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);
@@ -10143,6 +10446,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..95f2a5243eda6 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;
@@ -2258,6 +2259,7 @@ static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *ob
 			if (ELF64_ST_TYPE(src_sym->st_info) == STT_SECTION) {
 				struct src_sec *sec = &obj->secs[src_sym->st_shndx];
 				struct bpf_insn *insn;
+				__u32 *val;
 
 				if (src_linked_sec->shdr->sh_flags & SHF_EXECINSTR) {
 					/* calls to the very first static function inside
@@ -2274,6 +2276,40 @@ 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) {
+					/*
+					 * 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.
+					 */
+
+					/*
+					 * Only an executable section has its
+					 * r_offset checked, and even there only
+					 * for alignment; SHT_NOBITS has no
+					 * raw_data at all. Bound it here,
+					 * subtracting so it cannot wrap.
+					 */
+					if (!dst_linked_sec->raw_data ||
+					    dst_linked_sec->sec_sz < (int)sizeof(*val) ||
+					    dst_rel->r_offset % sizeof(*val) ||
+					    dst_rel->r_offset >
+					    (size_t)dst_linked_sec->sec_sz - sizeof(*val)) {
+						pr_warn("ELF relo #%d in section #%zu points outside the data of section '%s' in %s\n",
+							j, src_sec->sec_idx,
+							dst_linked_sec->sec_name,
+							obj->filename);
+						return -EINVAL;
+					}
+					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.skel b/tools/testing/selftests/bpf/Makefile.skel
index 580d1d82c1867..6d7518fa4018b 100644
--- a/tools/testing/selftests/bpf/Makefile.skel
+++ b/tools/testing/selftests/bpf/Makefile.skel
@@ -33,7 +33,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..68f2d61a694b5
--- /dev/null
+++ b/tools/testing/selftests/bpf/exceptions_cleanup.h
@@ -0,0 +1,56 @@
+/* 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
+#define RAN_NOP_PAD		0x20000
+#define RAN_VAR_STACK		0x40000
+#define RAN_GLOBAL_PAD		0x80000
+#define RAN_PAD_R0		0x100000
+
+/* 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..323ee8d72c913
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
@@ -0,0 +1,524 @@
+// 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");
+	/* bump() is not a landing pad; it sets its bit on every run. */
+	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 walks past. */
+	if (test__start_subtest("shared_callee_no_throw")) {
+		skel->bss->outer_input = 0;
+		run_shape(skel, skel->progs.entry_shared, 1, 2, 0);
+	}
+	if (test__start_subtest("shared_callee_throw")) {
+		skel->bss->outer_input = 0;
+		run_shape(skel, skel->progs.entry_shared, 101, THROW_COOKIE, RAN_SHARED);
+	}
+	if (test__start_subtest("shared_callee_uncovered_throw")) {
+		skel->bss->outer_input = 101;
+		run_shape(skel, skel->progs.entry_shared, 1, THROW_COOKIE, 0);
+		skel->bss->outer_input = 0;
+	}
+
+	/* 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 -- though it
+	 * is reachable, so a walk past the boundary would find it.
+	 */
+	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 and handed to a
+	 * bpf_loop() the verifier never reaches: the callback check has to fire
+	 * on the helper call, not on 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");
+		}
+	}
+
+	/*
+	 * A pad whose first instruction opt_remove_nops() deletes: the record
+	 * has to follow the pad rather than be dropped with the nop.
+	 */
+	if (test__start_subtest("nop_at_pad_head"))
+		run_shape(skel, skel->progs.entry_nop_pad, 101, THROW_COOKIE,
+			  RAN_NOP_PAD);
+
+	/*
+	 * A pad that indexes its frame's stack by a register the frame set
+	 * before the throwing call, marked precise back across the unwind edge.
+	 */
+	if (test__start_subtest("pad_var_stack_offset"))
+		run_shape(skel, skel->progs.entry_var_stack, 101, THROW_COOKIE,
+			  RAN_VAR_STACK);
+
+	/*
+	 * The same, where the throw is in a global subprogram, which the
+	 * verifier walks without a frame of its own.
+	 */
+	if (test__start_subtest("pad_over_global_subprog"))
+		run_shape(skel, skel->progs.entry_global_pad, 101, THROW_COOKIE,
+			  RAN_GLOBAL_PAD);
+
+	/*
+	 * A pad that indexes its frame by r0, which no insn in the frame
+	 * wrote: the precision request for it must not cross the unwind edge.
+	 */
+	if (test__start_subtest("pad_r0_precision"))
+		run_shape(skel, skel->progs.entry_pad_r0, 101, THROW_COOKIE,
+			  RAN_PAD_R0);
+
+	exceptions_cleanup_shapes__destroy(skel);
+}
+
+/*
+ * A table with more records than the program has instructions: no valid one
+ * can look like that, and it is refused before the kernel allocates for it.
+ */
+static void test_cleanup_info_cnt(void)
+{
+	struct bpf_insn insns[] = {
+		BPF_MOV64_IMM(BPF_REG_0, 0),
+		BPF_EXIT_INSN(),
+	};
+	struct bpf_cleanup_info rec = {
+		.begin_off = 0,
+		.end_off = 1,
+		.landing_pad_off = 1,
+	};
+	char log[512] = {};
+
+	LIBBPF_OPTS(bpf_prog_load_opts, opts,
+		    .log_buf = log,
+		    .log_size = sizeof(log),
+		    .log_level = 1,
+		    .cleanup_info = &rec,
+		    .cleanup_info_cnt = 1 << 20,
+		    .cleanup_info_rec_size = sizeof(rec));
+	int fd;
+
+	fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
+			   insns, ARRAY_SIZE(insns), &opts);
+	if (!ASSERT_LT(fd, 0, "load")) {
+		close(fd);
+		return;
+	}
+	/*
+	 * Turned away on the count, rather than on whatever the records past
+	 * the one below happen to hold.
+	 */
+	ASSERT_HAS_SUBSTR(log, "cleanup info has 1048576 records for 2 instructions",
+			  "log");
+}
+
+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")) {
+			printf("%s:SKIP:JIT cannot dispatch exception cleanup landing pads\n",
+			       __func__);
+			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);
+
+	if (test__start_subtest("cleanup_info_cnt"))
+		test_cleanup_info_cnt();
+}
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..3b79663579122
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup.c
@@ -0,0 +1,164 @@
+// 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 */
+		/*
+		 * r0 at pad entry is whatever the walker leaves
+		 * there, carried across the call in a callee-saved
+		 * register and handed to the resume, the way a
+		 * compiler-emitted pad passes the exception pointer to
+		 * _Unwind_Resume. The kfunc ignores it, and the two pads
+		 * below do without the shuffle.
+		 */
+		"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);
+}
+
+/*
+ * A frame with no cleanup record: an exception leaving it runs no pad. The
+ * throw never fires -- @never is global -- and the bit marks the return path.
+ */
+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..a7569f98a98de
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_fail.c
@@ -0,0 +1,678 @@
+// 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. The
+ * walk takes both, and the exit is refused the same way a catch pad's is.
+ */
+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("a catch pad is not supported yet")
+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("throws 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 global subprogram which can throw. Not case 3:
+ * the verifier does not walk a global subprogram, so the throw inside it is
+ * never reached, and what refuses the call is subprog_info.might_throw.
+ */
+__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; no exception is in flight when the walk gets
+ * there.
+ */
+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("reached without an exception in flight")
+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. An exception is
+ * in flight, so what refuses it is the frame it is in: a resume reaches the
+ * walker only from the frame whose pad the walker called.
+ */
+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 in frame 2, not frame 1 whose landing pad the exception entered")
+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;"
+	"call bpf_kfunc_call_test_pair_arg_nofit;"
+	"call bpf_unwind_resume;"
+	"exit;"
+	CLEANUP_REC("1b", "2b", "3b")
+	::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("calls a kfunc with an on-stack 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. The instruction it lands on would have to run both
+ * with an exception in flight and without, which the walk refuses.
+ */
+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("runs both inside and outside an exception cleanup landing pad")
+int jump_into_pad(void *ctx)
+{
+	return jump_into_pad_frame();
+}
+
+#if defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)
+
+/*
+ * 16. An indirect jump in a landing pad: the pad does not own the stack its
+ * targets would run on. A jump table entry is an offset from the program's
+ * section symbol, which has to be spelled in quotes here.
+ */
+static __used __naked __noinline void gotox_thrower(void)
+{
+	asm volatile (
+	"r1 = 15;"
+	"call bpf_throw;"
+	"exit;"
+	::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("indirect jump at insn")
+__naked void gotox_in_pad(void)
+{
+	asm volatile (
+	".pushsection .jumptables,\"\",@progbits;"
+"jt0_%=:"
+	".quad l0_%= - \"?syscall\";"
+	".quad l1_%= - \"?syscall\";"
+	".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, as raw bytes: the mnemonic only reached the LLVM
+	 * assembler in llvm 22, and BPF_RAW_INSN() needs <linux/bpf.h>, which
+	 * vmlinux.h rules out. dst_reg is the other nibble on a big-endian
+	 * target.
+	 */
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+	".byte 0x0d, 0x20, 0, 0, 0, 0, 0, 0;"
+#else
+	".byte 0x0d, 0x02, 0, 0, 0, 0, 0, 0;"
+#endif
+"l0_%=:"
+	"call bpf_unwind_resume;"
+	"exit;"
+"l1_%=:"
+	"call bpf_unwind_resume;"
+	"exit;"
+	CLEANUP_REC("1b", "2b", "3b")
+	::: __clobber_all);
+}
+
+#endif /* x86 || arm64 */
+
+/*
+ * 17. A BPF_LD_[ABS|IND] in a landing pad. A failed load leaves the
+ * subprogram through the hidden "r0 = 0; exit" gen_ld_abs() patches in, and
+ * that exit is the epilogue, which unwinds a stack the pad does not own.
+ */
+static __used __naked __noinline __u64 ld_abs_pad_frame(void)
+{
+	asm volatile (
+	"r6 = r1;"			/* the skb BPF_LD_ABS reads */
+"1:"	"call inner_throw;"		/* cleanup region */
+"2:"
+	"r0 = 0;"
+	"exit;"
+"3:"					/* landing pad */
+	"r0 = *(u32 *)skb[0];"
+	"call bpf_unwind_resume;"
+	"exit;"
+	CLEANUP_REC("1b", "2b", "3b")
+	::: __clobber_all);
+}
+
+SEC("?tc")
+__failure __msg("is in an exception cleanup landing pad")
+__naked void ld_abs_in_pad(void)
+{
+	asm volatile (
+	"call ld_abs_pad_frame;"
+	"exit;"
+	::: __clobber_all);
+}
+
+/*
+ * 18. A bpf_unwind_resume() in a subprogram a landing pad called, which has a
+ * pad of its own and reached it by ordinary control flow. Not case 9: an
+ * exception is in flight, but this is not the frame whose pad the walker ran.
+ */
+static __used __naked __noinline __u64 own_pad_callee(void)
+{
+	asm volatile (
+	"r1 = %[input] ll;"
+	"r1 = *(u64 *)(r1 + 0);"
+	"if r1 == 0 goto 3f;"		/* an ordinary branch into its pad */
+"1:"	"call bpf_preempt_disable;"	/* cleanup region: nothing that throws */
+	"call bpf_preempt_enable;"
+"2:"
+	"r0 = 0;"
+	"exit;"
+"3:"					/* its landing pad */
+	"call bpf_unwind_resume;"
+	"exit;"
+	CLEANUP_REC("1b", "2b", "3b")
+	:
+	: __imm_addr(input)
+	: __clobber_all);
+}
+
+static __used __naked __noinline __u64 pad_calls_own_pad_frame(void)
+{
+	asm volatile (
+"1:"	"call inner_throw;"		/* cleanup region */
+"2:"
+	"r0 = 0;"
+	"exit;"
+"3:"					/* landing pad, which calls the above */
+	"call own_pad_callee;"
+	"call bpf_unwind_resume;"
+	"exit;"
+	CLEANUP_REC("1b", "2b", "3b")
+	::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("is in frame 2, not frame 1 whose landing pad the exception entered")
+int resume_in_callee_own_pad(void *ctx)
+{
+	return pad_calls_own_pad_frame();
+}
+
+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..f44af8be4fda1
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c
@@ -0,0 +1,1049 @@
+// 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 outer_input = 0;
+__u64 magic = 0x5eed;
+__u64 pads_ran = 0;
+__u64 pad_runs = 0;
+
+/*
+ * 1. Everything a cleanup table leaves dead: a throw's continuation, the tail
+ * after a resume with an ld_imm64 and a branch in it, and the block only that
+ * continuation reaches.
+ */
+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
+ * belongs to the call site, not the callee. Either site can throw; the RCU
+ * lock is taken between them, so only the covered call unwinds holding it.
+ */
+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 = %[outer_input] ll;"
+	"r1 = *(u64 *)(r1 + 0);"
+	"call shared_callee;"		/* uncovered: no pad for its unwind */
+	"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(outer_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: the callee
+ * overwrites r6-r9 before it throws, so the check passes only if the walker
+ * found the spill in the discarded callee's prologue.
+ */
+#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, which moves the spill the walker reads. 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 at entry is
+ * the only place to get a context 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 walker finds the outer bpf_prog's ksym, so the table has
+ * to be handed over or the pad is never dispatched.
+ */
+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, 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;"
+	/*
+	 * Unnarrowed, the way entry_freplace hands fr_callee its argument: a
+	 * guard here would prune the callee's throw and let the sweep take the
+	 * pad, leaving no record for a walk past the boundary to match.
+	 */
+	"r2 = *(u64 *)(r1 + 0);"
+	"r1 = *(u64 *)(r10 - 8);"
+"1:"	"call tc_taken_callee;"		/* cleanup region */
+"2:"
+	"exit;"				/* the cookie, delivered at tc_target */
+"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.
+ */
+__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. */
+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 throwing frame is both the
+ * frame the record covers and the boundary, so the pad runs on the way to
+ * delivering the cookie.
+ */
+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 it coming, so what stops a nested exception is the
+ * walk ending in the extension's frame; pad_runs says the pad ran once.
+ */
+__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 last, where the exception
+ * callback patchlet -- the one that does not keep the call it replaced in the
+ * last slot -- has to carry the marks with it; r6-r9 reports a lost one.
+ */
+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. Well formed, and a
+ * rule against it would turn it 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 reloads from and stores to its own frame -- the shape every
+ * compiler-generated pad has. A JIT that addresses the frame through the stack
+ * pointer, arm64, has to find a pad's frame another way.
+ */
+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.
+ */
+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: LLVM emits _Unwind_Resume() and
+ * libbpf maps it onto bpf_unwind_resume(), which every other pad here calls.
+ */
+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. A tail call in
+ * the pad itself is refused, but the callee's prologue really did run, so its
+ * tail call releases exactly that and the target returns into the pad.
+ */
+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 at entry is
+ * the only place to get a context for bpf_tail_call(); the pad reloads it
+ * from the 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 with a table of its
+ * own, throwing inside the outer exception. The tail call made it a boundary,
+ * so the inner walk ends there and the outer pad, cookie and r6-r9 survive.
+ */
+__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);
+}
+
+/*
+ * 19. A landing pad whose first instruction is a nop. opt_remove_nops() runs
+ * long after the cleanup walk, so the record has to follow the pad to the
+ * instruction that takes its place rather than be dropped with the nop.
+ */
+static __used __naked __noinline __u64 nop_pad_frame(void)
+{
+	asm volatile (
+	"r1 = %[input] ll;"
+	"r6 = *(u64 *)(r1 + 0);"
+	"if r6 < 101 goto 6f;"
+	"r1 = %[cookie];"
+"1:"	"call bpf_throw;"		/* cleanup region */
+"2:"
+"6:"
+	"r0 = 0;"
+	"exit;"
+"3:"					/* landing pad: a nop, then its body */
+	"goto +0;"
+	PAD_RAN("%[ran]")
+	"call bpf_unwind_resume;"
+	"exit;"
+	CLEANUP_REC("1b", "2b", "3b")
+	:
+	: [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_NOP_PAD),
+	  __imm_addr(input), __imm_addr(pads_ran)
+	: __clobber_all);
+}
+
+SEC("syscall")
+int entry_nop_pad(void *ctx)
+{
+	return nop_pad_frame();
+}
+
+/*
+ * 20. A landing pad that indexes its own frame's stack by a register the frame
+ * set before the throwing call. The offset is not a constant, so r6 has to be
+ * marked precise from inside the pad, back across the unwind edge.
+ */
+/*
+ * A thrower that touches none of r6-r9, so nothing in it can answer for the
+ * pad frame's r6 and the walk has to leave this frame to look.
+ */
+static __used __naked __noinline __u64 var_thrower(void)
+{
+	asm volatile (
+	"if r1 < 101 goto 1f;"
+	"r1 = %[cookie];"
+	"call bpf_throw;"
+"1:"
+	"r0 = 0;"
+	"exit;"
+	:
+	: [cookie]"i"(THROW_COOKIE)
+	: __clobber_all);
+}
+
+static __used __naked __noinline __u64 var_stack_frame(void)
+{
+	asm volatile (
+	"r1 = %[magic] ll;"
+	"r1 = *(u64 *)(r1 + 0);"
+	"*(u64 *)(r10 - 8) = r1;"	/* the slot the pad will read... */
+	"*(u64 *)(r10 - 16) = r1;"	/* ...whichever of the two it is */
+	"r1 = %[input] ll;"
+	"r6 = *(u64 *)(r1 + 0);"
+	"r6 &= 1;"			/* an unknown slot number... */
+	"r6 <<= 3;"			/* ...as an aligned byte offset */
+	"r1 = %[input] ll;"
+	"r1 = *(u64 *)(r1 + 0);"
+"1:"	"call var_thrower;"		/* cleanup region */
+"2:"
+	"r0 = 0;"
+	"exit;"
+"3:"					/* landing pad */
+	"r7 = r0;"
+	"r1 = r10;"
+	"r1 += r6;"			/* variable offset into the frame */
+	"r2 = *(u64 *)(r1 - 16);"
+	"r3 = %[magic] ll;"
+	"r3 = *(u64 *)(r3 + 0);"
+	"if r2 != r3 goto 9f;"
+	PAD_RAN("%[ran]")
+"9:"
+	"r1 = r7;"
+	"call bpf_unwind_resume;"
+	"exit;"
+	CLEANUP_REC("1b", "2b", "3b")
+	:
+	: [ran]"i"(RAN_VAR_STACK), __imm_addr(input), __imm_addr(magic),
+	  __imm_addr(pads_ran)
+	: __clobber_all);
+}
+
+SEC("syscall")
+int entry_var_stack(void *ctx)
+{
+	return var_stack_frame();
+}
+
+/*
+ * 21. The same walk, over a call to a global subprogram: the verifier enters
+ * no frame for one, so the exception arrives at the pad from the call site
+ * rather than from a throw some frames deeper.
+ */
+__noinline __u64 global_thrower(__u64 x)
+{
+	if (x > 100)
+		bpf_throw(THROW_COOKIE);
+	return x + 1;
+}
+
+static __used __naked __noinline __u64 global_pad_frame(void)
+{
+	asm volatile (
+	"r1 = %[magic] ll;"
+	"r1 = *(u64 *)(r1 + 0);"
+	"*(u64 *)(r10 - 8) = r1;"
+	"*(u64 *)(r10 - 16) = r1;"
+	"r1 = %[input] ll;"
+	"r6 = *(u64 *)(r1 + 0);"
+	"r6 &= 1;"
+	"r6 <<= 3;"
+	"r1 = %[input] ll;"
+	"r1 = *(u64 *)(r1 + 0);"
+"1:"	"call global_thrower;"		/* cleanup region */
+"2:"
+	"r0 = 0;"
+	"exit;"
+"3:"					/* landing pad */
+	"r7 = r0;"
+	"r1 = r10;"
+	"r1 += r6;"
+	"r2 = *(u64 *)(r1 - 16);"
+	"r3 = %[magic] ll;"
+	"r3 = *(u64 *)(r3 + 0);"
+	"if r2 != r3 goto 9f;"
+	PAD_RAN("%[ran]")
+"9:"
+	"r1 = r7;"
+	"call bpf_unwind_resume;"
+	"exit;"
+	CLEANUP_REC("1b", "2b", "3b")
+	:
+	: [ran]"i"(RAN_GLOBAL_PAD), __imm_addr(input), __imm_addr(magic),
+	  __imm_addr(pads_ran)
+	: __clobber_all);
+}
+
+SEC("syscall")
+int entry_global_pad(void *ctx)
+{
+	return global_pad_frame();
+}
+
+/*
+ * 22. A landing pad that uses r0 as a variable stack offset. No instruction
+ * in the frame writes r0 -- the walker leaves it on the way in -- so the
+ * precision that access asks for has to end at the unwind edge, not cross it.
+ */
+static __used __naked __noinline __u64 pad_r0_frame(void)
+{
+	asm volatile (
+	"r1 = %[magic] ll;"
+	"r1 = *(u64 *)(r1 + 0);"
+	"*(u64 *)(r10 - 8) = r1;"	/* the slot the pad will read... */
+	"*(u64 *)(r10 - 16) = r1;"	/* ...whichever of the two it is */
+"1:"	"call regs_thrower;"		/* cleanup region */
+"2:"
+	"r0 = 0;"
+	"exit;"
+"3:"					/* landing pad */
+	"r0 &= 1;"			/* an unknown slot number... */
+	"r0 <<= 3;"			/* ...as an aligned byte offset */
+	"r1 = r10;"
+	"r1 += r0;"			/* variable offset into the frame */
+	"r2 = *(u64 *)(r1 - 16);"
+	"r3 = %[magic] ll;"
+	"r3 = *(u64 *)(r3 + 0);"
+	"if r2 != r3 goto 9f;"
+	PAD_RAN("%[ran]")
+"9:"
+	"call bpf_unwind_resume;"
+	"exit;"
+	CLEANUP_REC("1b", "2b", "3b")
+	:
+	: [ran]"i"(RAN_PAD_R0), __imm_addr(magic), __imm_addr(pads_ran)
+	: __clobber_all);
+}
+
+SEC("syscall")
+int entry_pad_r0(void *ctx)
+{
+	return pad_r0_frame();
+}
+
+char _license[] SEC("license") = "GPL";

3/2 2026/09/25 10:05 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 77449
							output: 21
							thoughts: 955
						

Thoughts:
**Initial Assessment of BPF Exception Cleanup Patch Series**

Okay, so the target architecture is amd64, which is immediately relevant to me. This patch series looks like it's introducing BPF exception cleanup landing pads. A quick look at the modified files confirms this: we're seeing changes in `arch/arm64/net/`, `arch/x86/net/`, `include/linux/bpf.h`, `kernel/bpf/`, and some tools. This is pretty significant. The core subsystem affected is, of course, BPF. The bulk of the logic seems to be concentrated in `kernel/bpf/` (verifier, CFG, exception handling, etc.) and `arch/x86/net/` (JIT compilation, assembly cleanup pads). Given that we're targeting amd64, the `arch/x86/` changes are *directly* applicable.

The question is, how easily can we reach this functionality for testing, especially in virtualized environments? This is *completely* reachable. Anyone with BPF capabilities (which can be unprivileged with appropriate settings, or privileged) can load BPF programs using `bpf(BPF_PROG_LOAD, ...)` passing `cleanup_info`, execute them, throw exceptions, etc. This is perfect for fuzzing.

Given that the changes touch the BPF verifier, JIT, exception unwinding, CFG verification, register tracking, and stack layout, this *absolutely* screams for fuzzing. We have a juicy target here.

Now, let's nail down the configuration requirements. The crucial Kconfig options appear to be `CONFIG_UNWINDER_ORC` (from `arch/x86/net/Makefile` and `kernel/bpf/exception.c`), `CONFIG_BPF_SYSCALL`, and `CONFIG_BPF_JIT`. Specifically, we're seeing that `bpf_jit_supports_cleanup_pads()` requires `CONFIG_UNWINDER_ORC` on x86, which is important for targeting. There might be other dependencies, so I'll need to run a `grepper` search or equivalent to verify. Let's get that done to finalize our strategy.



4/2 2026/09/25 10:06 tool grepper
0m Args:
{
  "Expression": "config UNWINDER_ORC"
}

Results:
{
  "Output": "arch/loongarch/Kconfig.debug=19=config UNWINDER_PROLOGUE\n--\narch/loongarch/Kconfig.debug-28-\narch/loongarch/Kconfig.debug:29:config UNWINDER_ORC\narch/loongarch/Kconfig.debug-30-\tbool \"ORC unwinder\"\n--\narch/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"
}

5/2 2026/09/25 10:06 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 79084
							output: 38
							thoughts: 23
						

Thoughts:
Here's a summary of my thought process, tailored for an expert like yourself:

**Diving into Debugging on x86**

Okay, so I'm thinking, where's the best place to start looking for this specific issue? Let's hone in on the kernel configuration for x86 debugging. I'm going to navigate directly to `arch/x86/Kconfig.debug`. Line 233 is a good starting point – it's often a central area for related options, or a point from which to branch into related options. It seems like the architecture specific debug config might hold the answers I'm searching for. Let's see what's defined there, and if there are any flags that look relevant to the current problem.



6/2 2026/09/25 10:06 tool read-file
0m Args:
{
  "File": "arch/x86/Kconfig.debug",
  "FirstLine": 225,
  "LineCount": 35
}

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 250:\t\tbool \"Frame pointer unwinder\"\n 251:\t\tselect ARCH_WANT_FRAME_POINTERS\n 252:\t\tselect FRAME_POINTER\n 253:\t\thelp\n 254:\t\t  This option enables the frame pointer unwinder for unwinding kernel\n 255:\t\t  stack traces.\n 256:\t\n 257:\t\t  The unwinder itself is fast and it uses less RAM than the ORC\n 258:\t\t  unwinder, but the kernel text size will grow by ~3% and the kernel's\n 259:\t\t  overall performance will degrade by roughly 5-10%.\n"
}

7/2 2026/09/25 10:06 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 79891
							output: 20
							thoughts: 358
						

Thoughts:
**My Analysis of BPF JIT Dependencies**

Okay, so I need to figure out the dependencies for `bpf_jit_supports_cleanup_pads()` on x86_64/amd64. The primary concern is `UNWINDER_ORC`, but what about `BPF_SYSCALL` and `BPF_JIT`? I'll start by checking the Makefiles.

First, I looked into `arch/x86/net/Makefile`. I can see that `bpf_cleanup_pad.o` is included when `CONFIG_UNWINDER_ORC` is defined, *and* that it is also dependent on `CONFIG_BPF_JIT`.

Next, I checked `kernel/bpf/Makefile`, which doesn't seem to have direct relationships to cleanup pads, but it does deal with `CONFIG_BPF_SYSCALL` and related modules. Okay, nothing there directly about cleanup pads.

Now, let's consider `CONFIG_BPF_JIT_ALWAYS_ON` and just plain `BPF_JIT`. I should analyze where `bpf_jit_supports_cleanup_pads()` is actually used.

I found `kernel/bpf/exception.c` that had this check: `if (!bpf_jit_supports_cleanup_pads() || !env->prog->jit_requested)`. This is key. If JIT isn't *requested* and `bpf_jit_supports_cleanup_pads()` isn't supported, we proceed down a different execution path. But that also means if `bpf_jit_enable` is zero (or BPF_JIT is not enabled), the `jit_requested` can be false, unless the JIT is enabled / always on, that's it! `BPF_JIT` *is* definitely required, but I need to make sure I haven't missed other direct dependencies.

Okay, time to search through the Kconfig files for any other mentions of `BPF_JIT`. I need to ensure I've got the full picture.



8/2 2026/09/25 10:06 tool grepper
0m Args:
{
  "Expression": "config BPF_JIT"
}

Results:
{
  "Output": "kernel/bpf/Kconfig=31=config BPF_SYSCALL\n--\nkernel/bpf/Kconfig-45-\nkernel/bpf/Kconfig:46:config BPF_JIT\nkernel/bpf/Kconfig-47-\tbool \"Enable BPF Just In Time compiler\"\n--\nkernel/bpf/Kconfig-61-\nkernel/bpf/Kconfig:62:config BPF_JIT_ALWAYS_ON\nkernel/bpf/Kconfig-63-\tbool \"Permanently enable BPF JIT and remove BPF interpreter\"\n--\nkernel/bpf/Kconfig-72-\nkernel/bpf/Kconfig:73:config BPF_JIT_DEFAULT_ON\nkernel/bpf/Kconfig-74-\tdef_bool ARCH_WANT_DEFAULT_BPF_JIT || BPF_JIT_ALWAYS_ON\n--\nkernel/bpf/Kconfig=96=config BPF_LSM\n--\nkernel/bpf/Kconfig-107-\nkernel/bpf/Kconfig:108:config BPF_JIT_KASAN\nkernel/bpf/Kconfig-109-\tbool\n"
}

9/2 2026/09/25 10:06 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 81020
							output: 28
							thoughts: 36
						

Thoughts:
Okay, here's my assessment of the situation:

**Investigating the bpf_throw Mechanism**

Alright, let's dive right in. My initial thought is to understand the nitty-gritty details of how `bpf_throw` works and, crucially, how it gets activated within the eBPF kernel subsystem. The most direct approach, as always, is to go straight to the source. Therefore, I need to pinpoint the implementation of `bpf_throw`. My go-to method for this type of kernel investigation? A good old `grep` search. I'll launch a `grep` query, focusing on the `kernel/bpf/` directory, to precisely locate every instance where `bpf_throw` is defined, used, or otherwise referenced. This will give me a solid foundation for further analysis.



10/2 2026/09/25 10:06 tool grepper
0m Args:
{
  "Expression": "bpf_throw",
  "PathPrefix": "kernel/bpf"
}

Results:
{
  "Output": "kernel/bpf/cfg.c=638=int bpf_check_cfg(struct bpf_verifier_env *env)\n--\nkernel/bpf/cfg.c-700-\t * A landing pad no call site was marked with -- a record whose range\nkernel/bpf/cfg.c:701:\t * holds no bpf2bpf call and no bpf_throw() -- is reached by nothing.\nkernel/bpf/cfg.c-702-\t * Walk it from here, and let the dead code sweep remove it.\n--\nkernel/bpf/exception.c=87=int bpf_exc_check_insn(struct bpf_verifier_env *env, struct bpf_insn *insn)\n--\nkernel/bpf/exception.c-112-\t\tverbose(env,\nkernel/bpf/exception.c:113:\t\t\t\"bpf_throw() at insn %u throws while an exception is in flight\\n\",\nkernel/bpf/exception.c-114-\t\t\ti);\n--\nkernel/bpf/helpers.c=3392=__bpf_kfunc void bpf_rcu_read_unlock(void)\n--\nkernel/bpf/helpers.c-3396-\nkernel/bpf/helpers.c:3397:struct bpf_throw_ctx {\nkernel/bpf/helpers.c-3398-\tstruct bpf_prog_aux *aux;\n--\nkernel/bpf/helpers.c-3405-\nkernel/bpf/helpers.c:3406:static void bpf_run_cleanup_pad(struct bpf_throw_ctx *ctx, const struct bpf_prog *prog,\nkernel/bpf/helpers.c-3407-\t\t\t\tu64 ip, u64 fp)\n--\nkernel/bpf/helpers.c=3432=static bool bpf_stack_walker(void *cookie, u64 ip, u64 sp, u64 bp)\nkernel/bpf/helpers.c-3433-{\nkernel/bpf/helpers.c:3434:\tstruct bpf_throw_ctx *ctx = cookie;\nkernel/bpf/helpers.c-3435-\tstruct bpf_prog *prog;\n--\nkernel/bpf/helpers.c-3460-\nkernel/bpf/helpers.c:3461:__bpf_kfunc void bpf_throw(u64 cookie)\nkernel/bpf/helpers.c-3462-{\nkernel/bpf/helpers.c:3463:\tstruct bpf_throw_ctx ctx = {};\nkernel/bpf/helpers.c-3464-\n--\nkernel/bpf/helpers.c-3472-\t * Prevent KASAN false positives for CONFIG_KASAN_STACK by unpoisoning\nkernel/bpf/helpers.c:3473:\t * deeper stack depths than ctx.sp as we do not return from bpf_throw,\nkernel/bpf/helpers.c-3474-\t * which skips compiler generated instrumentation to do the same. Some\n--\nkernel/bpf/helpers.c=5129=BTF_ID_FLAGS(func, bpf_task_from_vpid, KF_ACQUIRE | KF_RET_NULL)\nkernel/bpf/helpers.c:5130:BTF_ID_FLAGS(func, bpf_throw)\nkernel/bpf/helpers.c-5131-BTF_ID_FLAGS(func, bpf_unwind_resume)\n--\nkernel/bpf/verifier.c=1704=int bpf_copy_verifier_state(struct bpf_verifier_state *dst_state,\n--\nkernel/bpf/verifier.c-1717-\t/* if dst has more stack frames then src frame, free them, this is also\nkernel/bpf/verifier.c:1718:\t * necessary in case of exceptional exits using bpf_throw.\nkernel/bpf/verifier.c-1719-\t */\n--\nkernel/bpf/verifier.c=3088=static int check_subprogs(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-3132-\t\t\t * the last insn of the subprog should be either exit\nkernel/bpf/verifier.c:3133:\t\t\t * or unconditional jump back or bpf_throw call\nkernel/bpf/verifier.c-3134-\t\t\t */\n--\nkernel/bpf/verifier.c=5384=static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx,\n--\nkernel/bpf/verifier.c-5491-\t\t\tverbose(env,\nkernel/bpf/verifier.c:5492:\t\t\t\t\"bpf_throw kfunc (insn %d) cannot be called from callback subprog %d\\n\",\nkernel/bpf/verifier.c-5493-\t\t\t\ti, idx);\n--\nkernel/bpf/verifier.c=12593=enum special_kfunc_type {\n--\nkernel/bpf/verifier.c-12634-\tKF_bpf_percpu_obj_drop,\nkernel/bpf/verifier.c:12635:\tKF_bpf_throw,\nkernel/bpf/verifier.c-12636-\tKF_bpf_wq_set_callback,\n--\nkernel/bpf/verifier.c=12716=BTF_ID(func, bpf_percpu_obj_drop)\nkernel/bpf/verifier.c:12717:BTF_ID(func, bpf_throw)\nkernel/bpf/verifier.c-12718-BTF_ID(func, bpf_wq_set_callback)\n--\nkernel/bpf/verifier.c=13545=bool bpf_is_throw_kfunc(struct bpf_insn *insn)\n--\nkernel/bpf/verifier.c-13547-\treturn bpf_pseudo_kfunc_call(insn) \u0026\u0026 insn-\u003eoff == 0 \u0026\u0026\nkernel/bpf/verifier.c:13548:\t       insn-\u003eimm == special_kfunc_list[KF_bpf_throw];\nkernel/bpf/verifier.c-13549-}\n--\nkernel/bpf/verifier.c=14210=static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-14448-\nkernel/bpf/verifier.c:14449:\tif (meta.func_id == special_kfunc_list[KF_bpf_throw]) {\nkernel/bpf/verifier.c-14450-\t\tif (!bpf_jit_supports_exceptions()) {\n--\nkernel/bpf/verifier.c-14457-\t\t/* In the case of the default callback, the cookie value passed\nkernel/bpf/verifier.c:14458:\t\t * to bpf_throw becomes the return value of the program.\nkernel/bpf/verifier.c-14459-\t\t */\n--\nkernel/bpf/verifier.c=18625=static int unwind_finish(struct bpf_verifier_env *env)\nkernel/bpf/verifier.c-18626-{\nkernel/bpf/verifier.c:18627:\tint err = check_resource_leak(env, true, true, \"bpf_throw\");\nkernel/bpf/verifier.c-18628-\n"
}

11/2 2026/09/25 10:06 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 83152
							output: 37
							thoughts: 44
						

Thoughts:
Here's my summary, as though I'm thinking through this process:

**Diving into `bpf_throw()`**

Okay, so `bpf_throw()`... right, it's a `kfunc`, registered within `kernel/bpf/helpers.c`. That's the obvious place to start. I need to understand its implementation and exactly how it's being used. Time to crack open that file and see what's happening under the hood. Let's get to the details in `kernel/bpf/helpers.c`.



12/2 2026/09/25 10:06 tool read-file
0m Args:
{
  "File": "kernel/bpf/helpers.c",
  "FirstLine": 3430,
  "LineCount": 55
}

Results:
{
  "Contents": "3430:\t}\n3431:\t\n3432:\tstatic bool bpf_stack_walker(void *cookie, u64 ip, u64 sp, u64 bp)\n3433:\t{\n3434:\t\tstruct bpf_throw_ctx *ctx = cookie;\n3435:\t\tstruct bpf_prog *prog;\n3436:\t\n3437:\t\t/*\n3438:\t\t * The RCU read lock is held to safely traverse the latch tree, but we\n3439:\t\t * don't need its protection when accessing the prog, since it has an\n3440:\t\t * active stack frame on the current stack trace, and won't disappear.\n3441:\t\t */\n3442:\t\trcu_read_lock();\n3443:\t\tprog = bpf_prog_ksym_find(ip);\n3444:\t\trcu_read_unlock();\n3445:\t\tif (!prog)\n3446:\t\t\treturn !ctx-\u003ecnt;\n3447:\t\tctx-\u003ecnt++;\n3448:\t\n3449:\t\tbpf_run_cleanup_pad(ctx, prog, ip, bp);\n3450:\t\tctx-\u003ecallee = prog;\n3451:\t\tctx-\u003ecallee_fp = bp;\n3452:\t\n3453:\t\tif (bpf_is_subprog(prog))\n3454:\t\t\treturn true;\n3455:\t\tctx-\u003eaux = prog-\u003eaux;\n3456:\t\tctx-\u003esp = sp;\n3457:\t\tctx-\u003ebp = bp;\n3458:\t\treturn false;\n3459:\t}\n3460:\t\n3461:\t__bpf_kfunc void bpf_throw(u64 cookie)\n3462:\t{\n3463:\t\tstruct bpf_throw_ctx ctx = {};\n3464:\t\n3465:\t\tarch_bpf_stack_walk(bpf_stack_walker, \u0026ctx);\n3466:\t\tWARN_ON_ONCE(!ctx.aux);\n3467:\t\tif (ctx.aux)\n3468:\t\t\tWARN_ON_ONCE(!ctx.aux-\u003eexception_boundary);\n3469:\t\tWARN_ON_ONCE(!ctx.bp);\n3470:\t\tWARN_ON_ONCE(!ctx.cnt);\n3471:\t\t/*\n3472:\t\t * Prevent KASAN false positives for CONFIG_KASAN_STACK by unpoisoning\n3473:\t\t * deeper stack depths than ctx.sp as we do not return from bpf_throw,\n3474:\t\t * which skips compiler generated instrumentation to do the same. Some\n3475:\t\t * architectures cannot recover sp while unwinding, so fall back to bp.\n3476:\t\t */\n3477:\t\tkasan_unpoison_task_stack_below((void *)(long)(ctx.sp ?: ctx.bp));\n3478:\t\tctx.aux-\u003ebpf_exception_cb(cookie, ctx.sp + ctx.aux-\u003estack_arg_sp_adjust, ctx.bp, 0, 0);\n3479:\t\tWARN(1, \"A call to BPF exception callback should never return\\n\");\n3480:\t}\n3481:\t\n3482:\t/*\n3483:\t * Terminator of a compiler-emitted cleanup landing pad. The compiler names\n3484:\t * this _Unwind_Resume, the base unwind ABI's entry point for carrying an\n"
}

13/2 2026/09/25 10:06 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 84251
							output: 22
							thoughts: 18
						

Thoughts:
Here's my summary, as though I'm thinking through this process:

**Investigating the `arch_bpf_stack_walk` Function**

Okay, so I need to understand where this `arch_bpf_stack_walk` function is defined. It's crucial for understanding the architecture-specific stack walking mechanism being used in this context. Is it a macro, a static inline function, or something else entirely? Knowing its definition will give me critical insights into its implementation details and any platform-specific optimizations or constraints. I need to trace this back to the source code to see how it operates. Let's start by doing a quick search for its declaration.



14/2 2026/09/25 10:06 tool grepper
0m Args:
{
  "Expression": "arch_bpf_stack_walk"
}

Results:
{
  "Output": "arch/arm64/kernel/stacktrace.c=429=arch_bpf_unwind_consume_entry(const struct kunwind_state *state, void *cookie)\n--\narch/arm64/kernel/stacktrace.c-436-\narch/arm64/kernel/stacktrace.c:437:noinline noinstr void arch_bpf_stack_walk(bool (*consume_entry)(void *cookie, u64 ip, u64 sp,\narch/arm64/kernel/stacktrace.c-438-\t\t\t\t\t\t\t\tu64 fp), void *cookie)\n--\narch/loongarch/net/bpf_jit.c=2372=static noinline void walk_bpf_stackframe(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp),\n--\narch/loongarch/net/bpf_jit.c-2392-\narch/loongarch/net/bpf_jit.c:2393:void arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie)\narch/loongarch/net/bpf_jit.c-2394-{\n--\narch/powerpc/net/bpf_jit_comp64.c=401=void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)\n--\narch/powerpc/net/bpf_jit_comp64.c-413-/*\narch/powerpc/net/bpf_jit_comp64.c:414: * arch_bpf_stack_walk() - BPF stack walker for PowerPC\narch/powerpc/net/bpf_jit_comp64.c-415- *\n--\narch/powerpc/net/bpf_jit_comp64.c-425-\narch/powerpc/net/bpf_jit_comp64.c:426:void arch_bpf_stack_walk(bool (*consume_fn)(void *, u64, u64, u64), void *cookie)\narch/powerpc/net/bpf_jit_comp64.c-427-{\n--\narch/s390/net/bpf_jit_comp.c=3089=bool bpf_jit_supports_exceptions(void)\n--\narch/s390/net/bpf_jit_comp.c-3097-\narch/s390/net/bpf_jit_comp.c:3098:void arch_bpf_stack_walk(bool (*consume_fn)(void *, u64, u64, u64),\narch/s390/net/bpf_jit_comp.c-3099-\t\t\t void *cookie)\n--\narch/x86/net/bpf_jit_comp.c=4561=bool bpf_jit_supports_private_stack(void)\n--\narch/x86/net/bpf_jit_comp.c-4565-\narch/x86/net/bpf_jit_comp.c:4566:void arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie)\narch/x86/net/bpf_jit_comp.c-4567-{\n--\ninclude/linux/filter.h=1285=u64 bpf_arch_uaddress_limit(void);\ninclude/linux/filter.h:1286:void arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie);\ninclude/linux/filter.h-1287-u64 arch_bpf_timed_may_goto(void);\n--\nkernel/bpf/core.c=3487=bool __weak bpf_jit_supports_private_stack(void)\n--\nkernel/bpf/core.c-3491-\nkernel/bpf/core.c:3492:void __weak arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie)\nkernel/bpf/core.c-3493-{\n--\nkernel/bpf/core.c=3710=struct bpf_prog *bpf_prog_find_from_stack(void)\n--\nkernel/bpf/core.c-3713-\nkernel/bpf/core.c:3714:\tarch_bpf_stack_walk(find_from_stack_cb, \u0026ctx);\nkernel/bpf/core.c-3715-\treturn ctx.prog;\n--\nkernel/bpf/helpers.c=3461=__bpf_kfunc void bpf_throw(u64 cookie)\n--\nkernel/bpf/helpers.c-3464-\nkernel/bpf/helpers.c:3465:\tarch_bpf_stack_walk(bpf_stack_walker, \u0026ctx);\nkernel/bpf/helpers.c-3466-\tWARN_ON_ONCE(!ctx.aux);\n--\nkernel/bpf/stream.c=400=int bpf_stream_stage_dump_stack(struct bpf_stream_stage *ss)\n--\nkernel/bpf/stream.c-412-\t\treturn ret;\nkernel/bpf/stream.c:413:\tarch_bpf_stack_walk(dump_stack_cb, \u0026ctx);\nkernel/bpf/stream.c-414-\tif (ctx.err)\n"
}

15/2 2026/09/25 10:06 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 85864
							output: 45
							thoughts: 29
						

Thoughts:
Okay, here's my interpretation of that thought process, assuming I'm the expert and considering what I'd likely be thinking:

**Examining BPF JIT Compilation on x86**

Alright, time to dive into the nitty-gritty. My attention is drawn to the `arch/x86/net/bpf_jit_comp.c` file. Specifically, I need to focus on line 4566. I'm probably looking at a potential performance bottleneck, a subtle bug, or perhaps trying to understand a specific implementation detail within the BPF JIT compilation process for the x86 architecture. Line 4566, right, that's likely the key to unlocking something interesting in how the BPF bytecode gets translated to native x86 instructions. I have a hunch it's related to register allocation or instruction scheduling there, or maybe it's the handling of a specific BPF opcode within the JIT. Let's see what the code around that line is doing, and whether it aligns with my expectations given the architectural considerations and performance goals of BPF on x86. I'm guessing that I'll be checking to make sure it's optimized, and that I'm able to use my prior knowledge to quickly parse through the code and grasp what is happening.



16/2 2026/09/25 10:06 tool read-file
0m Args:
{
  "File": "arch/x86/net/bpf_jit_comp.c",
  "FirstLine": 4545,
  "LineCount": 45
}

Results:
{
  "Contents": "4545:\t}\n4546:\t\n4547:\tbool bpf_jit_supports_exceptions(void)\n4548:\t{\n4549:\t\t/* We unwind through both kernel frames (starting from within bpf_throw\n4550:\t\t * call) and BPF frames. Therefore we require ORC unwinder to be enabled\n4551:\t\t * to walk kernel frames and reach BPF frames in the stack trace.\n4552:\t\t */\n4553:\t\treturn IS_ENABLED(CONFIG_UNWINDER_ORC);\n4554:\t}\n4555:\t\n4556:\tbool bpf_jit_supports_cleanup_pads(void)\n4557:\t{\n4558:\t\treturn IS_ENABLED(CONFIG_UNWINDER_ORC);\n4559:\t}\n4560:\t\n4561:\tbool bpf_jit_supports_private_stack(void)\n4562:\t{\n4563:\t\treturn true;\n4564:\t}\n4565:\t\n4566:\tvoid arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie)\n4567:\t{\n4568:\t#if defined(CONFIG_UNWINDER_ORC)\n4569:\t\tstruct unwind_state state;\n4570:\t\tunsigned long addr;\n4571:\t\n4572:\t\tfor (unwind_start(\u0026state, current, NULL, NULL); !unwind_done(\u0026state);\n4573:\t\t     unwind_next_frame(\u0026state)) {\n4574:\t\t\taddr = unwind_get_return_address(\u0026state);\n4575:\t\t\tif (!addr || !consume_fn(cookie, (u64)addr, (u64)state.sp, (u64)state.bp))\n4576:\t\t\t\tbreak;\n4577:\t\t}\n4578:\t\treturn;\n4579:\t#endif\n4580:\t}\n4581:\t\n4582:\tvoid bpf_arch_poke_desc_update(struct bpf_jit_poke_descriptor *poke,\n4583:\t\t\t\t       struct bpf_prog *new, struct bpf_prog *old)\n4584:\t{\n4585:\t\tu8 *old_addr, *new_addr, *old_bypass_addr;\n4586:\t\tenum bpf_text_poke_type t;\n4587:\t\tint ret;\n4588:\t\n4589:\t\told_bypass_addr = old ? NULL : poke-\u003ebypass_addr;\n"
}

17/2 2026/09/25 10:06 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 86917
							output: 225
							thoughts: 2585
						

Thoughts:
**BPF Exception Handling and Cleanup Pad Focus for Fuzzing**

Alright, let's get down to brass tacks. I've been digging into this patch, and here's the mental rundown.  We're dealing with a significant addition: BPF exception handling and cleanup pads. I need to make sure the fuzzing effort is laser-focused and doesn't get diluted by targeting generic, unimportant functions.

First, the dependencies. `arch_bpf_stack_walk` on x86 *demands* `CONFIG_UNWINDER_ORC`. And the `bpf_jit_supports_cleanup_pads()` call confirms its importance, so `UNWINDER_ORC` is definitely a key piece of the puzzle. Now I've broken down the key symbols and logic being modified or added. I went through the code and pulled out the important functions.

*   `bpf_prepare_cleanup_exceptions`
*   `bpf_exc_check_insn`
*   `bpf_exc_attach_main_prog`
*   `bpf_exc_fill_native_ranges`
*   `bpf_unwind_resume`
*   `arch_bpf_run_cleanup_pad`
*   `bpf_jit_supports_cleanup_pads`
*   `bpf_throw`

Next, the guidelines. The goal is to avoid those ubiquitous functions—memory allocators, page fault handlers, core locking primitives—the stuff that's called everywhere and doesn't tell us much about this specific change. Instead, let's target the functions that *specifically* implement this new exception handling logic.

Now, let's see which functions are static. Static inlined functions are a problem, since the fuzzer cannot target these directly. We need to be careful with things like `check_cleanup_info`, because that one is inlined.

After examining the symbols, these functions are non-static and will be good focus targets:

*   `bpf_prepare_cleanup_exceptions`
*   `bpf_exc_check_insn`
*   `bpf_exc_attach_main_prog`
*   `bpf_exc_fill_native_ranges`
*   `bpf_unwind_resume`
*   `arch_bpf_run_cleanup_pad`
*   `bpf_jit_supports_cleanup_pads`
*   `bpf_throw`

Okay, what config flags do we need? Clearly, we need `BPF_SYSCALL` (for the BPF system call itself, obviously), `BPF_JIT` (for the JIT compilation), and `UNWINDER_ORC`.

Is this actually worth fuzzing? Absolutely. This is a big deal: complex new exception cleanup mechanics in the verifier, JIT, and runtime. The verifier now has new validation routines for exception edges, landing pads, stack unwinding, and precision tracking across frames, and also generates JIT code for prologues, epilogues, throws, and resumes.

I've made sure to choose functions that are very specific to the added code:

*   `bpf_prepare_cleanup_exceptions`
*   `bpf_exc_check_insn`
*   `bpf_exc_attach_main_prog`
*   `bpf_exc_fill_native_ranges`
*   `arch_bpf_run_cleanup_pad`
*   `bpf_unwind_resume`
*   `bpf_throw`

These are all *directly* related to the new cleanup pad / exception handling mechanism. Now that I've double-checked all of this, it's time to mark these functions and flags and call this a day.



18/2 2026/09/25 10:06 tool set-results
0m Args:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "BPF_JIT",
    "UNWINDER_ORC"
  ],
  "FocusSymbols": [
    "bpf_prepare_cleanup_exceptions",
    "bpf_exc_check_insn",
    "bpf_exc_attach_main_prog",
    "bpf_exc_fill_native_ranges",
    "bpf_unwind_resume",
    "arch_bpf_run_cleanup_pad",
    "bpf_throw"
  ],
  "Reasoning": "The patch series introduces BPF exception cleanup landing pads, extending the BPF verifier (CFG analysis, instruction checks, precision tracking, states, and fixups), x86 JIT compilation, and runtime stack unwinding/landing pad execution (via arch_bpf_run_cleanup_pad). This reachable core subsystem logic exposes new userspace parameters in BPF_PROG_LOAD (cleanup_info) and warrants fuzzing.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "BPF_JIT",
    "UNWINDER_ORC"
  ],
  "FocusSymbols": [
    "bpf_prepare_cleanup_exceptions",
    "bpf_exc_check_insn",
    "bpf_exc_attach_main_prog",
    "bpf_exc_fill_native_ranges",
    "bpf_unwind_resume",
    "arch_bpf_run_cleanup_pad",
    "bpf_throw"
  ],
  "Reasoning": "The patch series introduces BPF exception cleanup landing pads, extending the BPF verifier (CFG analysis, instruction checks, precision tracking, states, and fixups), x86 JIT compilation, and runtime stack unwinding/landing pad execution (via arch_bpf_run_cleanup_pad). This reachable core subsystem logic exposes new userspace parameters in BPF_PROG_LOAD (cleanup_info) and warrants fuzzing.",
  "WorthFuzzing": true
}

19/1 2026/09/25 10:06 action If
1m Args:
{
  "WorthFuzzing": true
}

Results:
null

20/2 2026/09/25 10:06 agent kmsan-evaluator
1m Model:
core

Args:
null

Results:
{
  "KMSANReasoning": "The patch series adds support for BPF exception cleanup landing pads across the BPF verifier, x86_64 and arm64 JIT compilers, runtime unwinding helpers, and libbpf.\n\nKASAN vs KMSAN applicability:\n- KMSAN detects reads of uninitialized memory and info-leaks to userspace. In this patch series, no uninitialized kernel memory is read or exposed. All new kernel data structures (`struct bpf_exception_info`, `struct bpf_cleanup_range`, `struct bpf_cleanup_info`, `insn_aux_data`) are either allocated with zeroing (`kzalloc_obj`, `kvcalloc`, `__GFP_ZERO`) or completely initialized before use. The only userspace copy added in `check_cleanup_info()` is `copy_to_bpfptr_offset()`, which writes a scalar `u32 min_size` that is fully initialized.\n- The primary risks in this patch are verifier state tracking/CFG analysis correctness, stack frame offset calculations during JIT compilation, and control flow integrity in landing pad execution. Any resulting bugs (such as stack corruption, out-of-bounds stack/heap access, or instruction pointer corruption) are memory corruption / out-of-bounds issues detectable by KASAN and standard detectors, not uninitialized memory issues.\n\nTherefore, 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 75946f2a321a4c32eb3617347c5e1fdd19fa83fd
Author: syz-cluster <triage@syzkaller.com>
Date:   Fri Sep 25 10:05: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..729b1ef3e5850
--- /dev/null
+++ b/arch/arm64/net/bpf_cleanup_pad.S
@@ -0,0 +1,94 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <linux/linkage.h>
+
+/*
+ * A frame's prologue pushes the tail call counter pair, and then -- once
+ * bpf_exc_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, #1
+
+	/* 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..c7209c26b235a 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -10,6 +10,7 @@
 #include <linux/arm-smccc.h>
 #include <linux/bitfield.h>
 #include <linux/bpf.h>
+#include <linux/bpf_verifier.h>
 #include <linux/cfi.h>
 #include <linux/filter.h>
 #include <linux/memory.h>
@@ -75,6 +76,19 @@ 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 {
 	const struct bpf_prog *prog;
 	int idx;
@@ -87,6 +101,8 @@ struct jit_ctx {
 	__le32 *ro_image;
 	u32 stack_size;
 	u16 stack_arg_size;
+	/* Bytes reserved for the throw-site spill; 0 unless this frame throws. */
+	u32 throw_spill;
 	u64 user_vm_start;
 	u64 arena_vm_start;
 	bool fp_used;
@@ -432,7 +448,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_exc_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 +482,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_exc_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 +619,22 @@ 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,
+	 * which only a frame that throws has, sits below the callee-saved one
+	 * rather than in the program stack.
+	 */
+	if (bpf_exc_force_spill(prog))
+		prog->aux->exc->spill_off = -(16 + A64_CLEANUP_SPILL_SZ);
+	if (bpf_exc_needs_throw_spill(prog)) {
+		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 +724,9 @@ 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);
 
+	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 +1091,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);
@@ -1367,6 +1406,8 @@ 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_exc_insn_in_pad(env, ctx->prog, i);
+	const bool pad_head = bpf_exc_insn_is_pad(env, ctx->prog, i);
 	const bool is64 = BPF_CLASS(code) == BPF_ALU64 ||
 			  BPF_CLASS(code) == BPF_JMP;
 	u8 jmp_cond;
@@ -1378,9 +1419,13 @@ 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) || pad_head)
 		emit_bti(A64_BTI_J, ctx);
 
+	if (pad_head)
+		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 +1788,26 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
 		u64 func_addr;
 		u32 cpu_offset;
 
+		if (bpf_exc_insn_is_throw(env, ctx->prog, i)) {
+			/* 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_exc_insn_is_resume(env, ctx->prog, i)) {
+			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 +1919,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 +2018,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 +2088,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 +2478,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_exc_fill_native_ranges(prog, ctx.offset, ctx.ro_image);
 out_off:
 		if (!ro_header && priv_stack_ptr) {
 			free_percpu(priv_stack_ptr);
@@ -3385,6 +3460,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..8bb22644ba10b 100644
--- a/arch/x86/net/Makefile
+++ b/arch/x86/net/Makefile
@@ -7,4 +7,7 @@ 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
+        ifdef CONFIG_UNWINDER_ORC
+                obj-$(CONFIG_BPF_JIT) += bpf_cleanup_pad.o
+        endif
 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..10d612b11f36a
--- /dev/null
+++ b/arch/x86/net/bpf_cleanup_pad.S
@@ -0,0 +1,79 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <linux/linkage.h>
+#include <asm/nospec-branch.h>
+
+/*
+ * The x86-64 BPF JIT prologue spills, once bpf_exc_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.
+ *
+ * rbp addresses the pad's frame and rsp this one, so the two are not the
+ * neighbours a JITed frame expects them to be. That is why
+ * cleanup_check_pad_insn() refuses a call in a pad that passes an argument on
+ * the stack: the JIT stages those relative to rbp, and the callee reads them
+ * relative to rsp.
+ */
+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 $1, %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..26df52050cd72 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -357,6 +357,12 @@ 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 +838,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_exc_force_spill(bpf_prog)) {
 		pop_callee_regs(&prog, all_callee_regs_used);
 		pop_r12(&prog);
 	} else {
@@ -899,7 +905,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_exc_force_spill(bpf_prog)) {
 		pop_callee_regs(&prog, all_callee_regs_used);
 		pop_r12(&prog);
 	} else {
@@ -1977,6 +1983,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 +2022,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_exc_needs_throw_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 +2034,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_exc_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 +2049,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_exc_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_exc_force_spill(bpf_prog)) {
 		callee_saved_size += 4 * 8; /* rbx, r13, r14, r15 */
 	} else {
 		int j;
@@ -2063,7 +2074,21 @@ 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.
+	 * Only a frame that throws has a throw-site area.
+	 */
+	if (bpf_exc_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);
+	if (bpf_exc_needs_throw_spill(bpf_prog))
+		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 +2135,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_exc_insn_is_pad(env, bpf_prog, i - 1))
 			EMIT_ENDBR();
 
 		ip = image + addrs[i - 1] + (prog - temp);
@@ -2903,9 +2929,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_exc_insn_is_throw(env, 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_exc_insn_is_resume(env, bpf_prog, i - 1)) {
+				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 +2992,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 +3259,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_exc_force_spill(bpf_prog)) {
 				pop_callee_regs(&prog, all_callee_regs_used);
 				pop_r12(&prog);
 			} else {
@@ -4385,6 +4430,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_exc_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 +4553,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 e7c5e203edddb..c11dc224973eb 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1787,6 +1787,93 @@ 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;
+	u32 nr_info;
+	u32 nr_ranges;
+	/* Set if any instruction of this subprogram calls bpf_throw(). */
+	bool has_throw;
+	/* 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_exc_force_spill(const struct bpf_prog *prog);
+bool bpf_exc_needs_throw_spill(const struct bpf_prog *prog);
+bool bpf_exc_insn_is_pad(const struct bpf_verifier_env *env,
+			 const struct bpf_prog *prog, u32 idx);
+bool bpf_exc_insn_in_pad(const struct bpf_verifier_env *env,
+			 const struct bpf_prog *prog, u32 idx);
+bool bpf_exc_insn_is_throw(const struct bpf_verifier_env *env,
+			   const struct bpf_prog *prog, u32 idx);
+bool bpf_exc_insn_is_resume(const struct bpf_verifier_env *env,
+			    const struct bpf_prog *prog, u32 idx);
+int bpf_exc_attach_main_prog(struct bpf_verifier_env *env, struct bpf_prog *prog);
+void bpf_exc_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image);
+void bpf_exc_free_info(struct bpf_prog_aux *aux);
+#else
+static inline bool bpf_exc_force_spill(const struct bpf_prog *prog)
+{
+	return false;
+}
+
+static inline bool bpf_exc_needs_throw_spill(const struct bpf_prog *prog)
+{
+	return false;
+}
+
+static inline bool bpf_exc_insn_is_pad(const struct bpf_verifier_env *env,
+				       const struct bpf_prog *prog, u32 idx)
+{
+	return false;
+}
+
+static inline bool bpf_exc_insn_in_pad(const struct bpf_verifier_env *env,
+				       const struct bpf_prog *prog, u32 idx)
+{
+	return false;
+}
+
+static inline bool bpf_exc_insn_is_throw(const struct bpf_verifier_env *env,
+					 const struct bpf_prog *prog, u32 idx)
+{
+	return false;
+}
+
+static inline bool bpf_exc_insn_is_resume(const struct bpf_verifier_env *env,
+					  const struct bpf_prog *prog, u32 idx)
+{
+	return false;
+}
+
+static inline int bpf_exc_attach_main_prog(struct bpf_verifier_env *env,
+					   struct bpf_prog *prog)
+{
+	return 0;
+}
+
+static inline void bpf_exc_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image)
+{
+}
+
+static inline void bpf_exc_free_info(struct bpf_prog_aux *aux)
+{
+}
+#endif
+
 struct bpf_prog_aux {
 	atomic64_t refcnt;
 	u32 used_map_cnt;
@@ -1867,6 +1954,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_verifier.h b/include/linux/bpf_verifier.h
index 92f528c456052..500a1e2535799 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -429,7 +429,8 @@ struct bpf_jmp_history_entry {
 	u32 prev_idx : 20;
 	/* special INSN_F_xxx flags */
 	u32 flags : 4;
-	u32 : 8;
+	u32 unwind_frames : 4;	/* frames popped to reach this landing pad */
+	u32 : 4;
 	/*
 	 * additional registers that need precision tracking when this
 	 * jump is backtracked, vector of five 11-bit records
@@ -510,6 +511,9 @@ struct bpf_verifier_state {
 	bool speculative;
 	bool in_sleepable;
 
+	bool unwinding; /* an exception is in flight */
+	u8 unwind_frameno; /* the frame whose landing pad is running */
+
 	/* first and last insn idx of this verifier state */
 	u32 first_insn_idx;
 	u32 last_insn_idx;
@@ -675,40 +679,51 @@ struct bpf_insn_aux_data {
 	u64 map_key_state; /* constant (32 bit) key tracking for maps */
 	int ctx_field_size; /* the ctx field size for load insn, maybe 0 */
 	u32 seen; /* this insn was processed by the verifier at env->pass_cnt */
-	bool nospec; /* do not execute this instruction speculatively */
-	bool nospec_result; /* result is unsafe under speculation, nospec must follow */
-	bool zext_dst; /* this insn zero extends dst reg */
-	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 call_with_percpu_alloc_ptr; /* {this,per}_cpu_ptr() with prog percpu alloc */
-	u8 alu_state; /* used in combination with alu_limit */
+	u64 nospec:1; /* do not execute this instruction speculatively */
+	u64 nospec_result:1; /* result is unsafe under speculation, nospec must follow */
+	u64 zext_dst:1; /* this insn zero extends dst reg */
+	u64 needs_zext:1; /* alu op needs to clear upper bits */
+	u64 non_sleepable:1; /* helper/kfunc may be called from non-sleepable context */
+	u64 is_iter_next:1; /* bpf_iter_<type>_next() kfunc call */
+	u64 call_with_percpu_alloc_ptr:1; /* {this,per}_cpu_ptr() with prog percpu alloc */
+	u64 alu_state:8; /* used in combination with alu_limit */
 	/* true if STX or LDX instruction is a part of a spill/fill
 	 * pattern for a bpf_fastcall call.
 	 */
-	u8 fastcall_pattern:1;
+	u64 fastcall_pattern:1;
 	/* for CALL instructions, a number of spill/fill pairs in the
 	 * bpf_fastcall pattern.
 	 */
-	u8 fastcall_spills_num:3;
-	u8 arg_prog:4;
-
-	/* below fields are initialized once */
-	unsigned int orig_idx; /* original instruction index */
-	u32 jmp_point:1;
-	u32 prune_point:1;
+	u64 fastcall_spills_num:3;
+	u64 arg_prog:4;
+	u64 throw_call:1; /* call to bpf_throw() */
+	u64 resume_call:1; /* call to bpf_unwind_resume() */
+	u64 in_cleanup_pad:1; /* runs with an exception in flight, in the pad's frame */
+	u64 outside_cleanup_pad:1; /* ... and the other way round */
+	u64 cleanup_pad_head:1; /* first insn of a landing pad */
+
+	/* below flags are initialized once */
+	u64 jmp_point:1;
+	u64 prune_point:1;
 	/* ensure we check state equivalence and save state checkpoint and
 	 * this instruction, regardless of any heuristics
 	 */
-	u32 force_checkpoint:1;
+	u64 force_checkpoint:1;
 	/* true if instruction is a call to a helper function that
 	 * accepts callback function as a parameter.
 	 */
-	u32 calls_callback:1;
-	u32 indirect_target:1; /* if it is an indirect jump target */
-	u32 non_stack_access:1; /* instruction can access non-stack memory */
+	u64 calls_callback:1;
+	u64 indirect_target:1; /* if it is an indirect jump target */
+	u64 non_stack_access:1; /* instruction can access non-stack memory */
 	/* true if some jump or call instruction targets this instruction */
-	u32 jump_target:1;
+	u64 jump_target:1;
+
+	unsigned int orig_idx; /* original instruction index, initialized once */
+	/*
+	 * 1 + the instruction index of the exception cleanup landing pad
+	 * this call site unwinds to, or 0 for none.
+	 */
+	u32 cleanup_pad;
 	/*
 	 * CFG strongly connected component this instruction belongs to,
 	 * zero if it is a singleton SCC.
@@ -983,10 +998,14 @@ struct bpf_verifier_env {
 	} cfg;
 	struct backtrack_state bt;
 	struct bpf_jmp_history_entry *cur_hist_ent;
+	u8 cur_unwind_frames; /* frames popped to reach this insn, if a pad */
+	u8 unwind_frames; /* frames popped, staged for the next insn */
 	/* Per-callsite copy of parent's converged at_stack_in for cross-frame fills. */
 	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 +1535,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 422284b4fa96f..b0c495e94f740 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 0aaa54359aebc..646aa8758d48d 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 */
@@ -7599,6 +7602,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/backtrack.c b/kernel/bpf/backtrack.c
index 507a366dffa47..88da73345f01b 100644
--- a/kernel/bpf/backtrack.c
+++ b/kernel/bpf/backtrack.c
@@ -4,6 +4,7 @@
 #include <linux/bpf_verifier.h>
 #include <linux/filter.h>
 #include <linux/bitmap.h>
+#include "exception.h"
 
 #define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
 
@@ -47,6 +48,7 @@ int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state
 	p->flags = insn_flags;
 	p->spi = spi;
 	p->frame = frame;
+	p->unwind_frames = env->cur_unwind_frames;
 	p->linked_regs = linked_regs;
 	cur->jmp_history_cnt = cnt;
 	env->cur_hist_ent = p;
@@ -419,10 +421,12 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,
 				 * extra instructions from subprog; the next
 				 * instruction after call to global subprog
 				 * should be literally next instruction in
-				 * caller program
+				 * caller program -- or, if the callee threw,
+				 * the landing pad of this call site
 				 */
-				verifier_bug_if(idx + 1 != subseq_idx, env,
-						"extra insn from subprog");
+				verifier_bug_if(idx + 1 != subseq_idx &&
+						bpf_exc_pad_of_call(env, idx) != subseq_idx,
+						env, "extra insn from subprog");
 				/* global subprog always sets R0 */
 				bt_clear_reg(bt, BPF_REG_0);
 				/* and if it does not set R2, main pass would catch it */
@@ -823,6 +827,7 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
 	struct bpf_func_state *func;
 	bool tmp, skip_first = true;
 	struct bpf_reg_state *reg;
+	u32 pending_unwind = 0;
 	int i, fr, err;
 
 	if (!env->bpf_capable)
@@ -888,11 +893,19 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
 		}
 
 		for (i = last_idx;;) {
+			hist = get_jmp_hist_entry(st, history, i);
+			/*
+			 * The insn about to be read sits on the far side of an
+			 * unwind edge, so enter the frames that edge crossed
+			 * before reading it.
+			 */
+			for (; pending_unwind; pending_unwind--)
+				if (bt_subprog_enter(bt))
+					return -EFAULT;
 			if (skip_first) {
 				err = 0;
 				skip_first = false;
 			} else {
-				hist = get_jmp_hist_entry(st, history, i);
 				err = backtrack_insn(env, i, subseq_idx, hist, bt);
 			}
 			if (err == -ENOTSUPP) {
@@ -909,6 +922,21 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
 				 */
 				return 0;
 			subseq_idx = i;
+			if (hist && hist->unwind_frames) {
+				/*
+				 * This insn is a landing pad the unwind reached
+				 * from a throw or a resume with
+				 * hist->unwind_frames frames deeper. The walker
+				 * writes r0 on the way in, so no insn defines
+				 * it: left set, the request reaches the call
+				 * that created this frame, where only r1-r5 may
+				 * still be.
+				 */
+				bt_clear_reg(bt, BPF_REG_0);
+				if (bt_empty(bt))
+					return 0;
+				pending_unwind = hist->unwind_frames;
+			}
 			i = get_prev_insn_idx(st, i, &history);
 			if (i == -ENOENT)
 				break;
@@ -927,6 +955,11 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
 		if (!st)
 			break;
 
+		if (verifier_bug_if(bt->frame > st->curframe, env,
+				    "backtrack frame %d, state curframe %d",
+				    bt->frame, st->curframe))
+			return -EFAULT;
+
 		for (fr = bt->frame; fr >= 0; fr--) {
 			func = st->frame[fr];
 			bitmap_from_u64(mask, bt_frame_reg_mask(bt, fr));
diff --git a/kernel/bpf/cfg.c b/kernel/bpf/cfg.c
index 842c7d1eabccc..1c3b4165e18c0 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,64 @@ 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_exc_pad_of_call(env, t);
+	if (w < 0)
+		return DONE_EXPLORING;
+
+	/*
+	 * @t is a call that may branch here, and @w is the target of that
+	 * branch, so both are prune points. @w especially: every covered call
+	 * site in a region unwinds to the same pad, and without a prune point
+	 * at its head the verifier walks the pad again for each of them.
+	 */
+	mark_prune_point(env, t);
+	mark_prune_point(env, w);
+	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 +228,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 +640,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 +696,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 bpf2bpf call and no bpf_throw() -- 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..bc569fa5f97f9 100644
--- a/kernel/bpf/check_btf.c
+++ b/kernel/bpf/check_btf.c
@@ -407,6 +407,136 @@ static int check_core_relo(struct bpf_verifier_env *env,
 	return err;
 }
 
+#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 > env->prog->len) {
+		verbose(env, "cleanup info has %u records for %u instructions\n",
+			nrec, env->prog->len);
+		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_subprog_info *sb, *se, *sl;
+		struct bpf_cleanup_info *rec = &krecord[i];
+
+		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 = bpf_find_containing_subprog(env, rec->begin_off);
+		se = bpf_find_containing_subprog(env, rec->end_off - 1);
+		sl = bpf_find_containing_subprog(env, rec->landing_pad_off);
+		if (!sb || !se || !sl) {
+			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;
+		}
+		/*
+		 * A zero opcode is the second half of a 16-byte insn, not an
+		 * insn. end_off is exclusive, so it may be one past the last.
+		 */
+		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 inside any call-site range, its own included. */
+	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 +571,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 227211166dccf..16e5210a56d59 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_exc_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_exc_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_exc_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;
@@ -3475,6 +3493,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..28aba67888478
--- /dev/null
+++ b/kernel/bpf/exception.c
@@ -0,0 +1,328 @@
+// 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/bsearch.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)
+
+BTF_ID_LIST_SINGLE(bpf_unwind_resume_id, func, bpf_unwind_resume)
+
+static void mark_kfunc_sites(struct bpf_verifier_env *env)
+{
+	u32 i;
+
+	for (i = 0; i < env->prog->len; i++) {
+		struct bpf_insn *insn = &env->prog->insnsi[i];
+
+		if (bpf_is_throw_kfunc(insn))
+			env->insn_aux_data[i].throw_call = true;
+		else if (bpf_is_unwind_resume_kfunc(insn))
+			env->insn_aux_data[i].resume_call = true;
+	}
+}
+
+int bpf_exc_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;
+}
+
+static void 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;
+	}
+
+	mark_kfunc_sites(env);
+	mark_call_sites(env);
+	return 0;
+}
+
+int bpf_exc_check_insn(struct bpf_verifier_env *env, struct bpf_insn *insn)
+{
+	struct bpf_verifier_state *state = env->cur_state;
+	struct bpf_insn_aux_data *aux;
+	u32 i = env->insn_idx;
+	bool in_pad;
+
+	aux = &env->insn_aux_data[i];
+	in_pad = state->unwinding && state->curframe == state->unwind_frameno;
+
+	if (in_pad ? aux->outside_cleanup_pad : aux->in_cleanup_pad) {
+		verbose(env,
+			"insn %u runs both inside and outside an exception cleanup landing pad\n",
+			i);
+		return -EINVAL;
+	}
+	if (in_pad)
+		aux->in_cleanup_pad = true;
+	else
+		aux->outside_cleanup_pad = true;
+
+	if (!state->unwinding)
+		return 0;
+
+	if (bpf_is_throw_kfunc(insn)) {
+		verbose(env,
+			"bpf_throw() at insn %u throws while an exception is in flight\n",
+			i);
+		return -EINVAL;
+	}
+	if (bpf_pseudo_call(insn)) {
+		int subprog = bpf_find_subprog(env, i + insn->imm + 1);
+
+		if (subprog >= 0 && bpf_subprog_is_global(env, subprog) &&
+		    env->subprog_info[subprog].might_throw) {
+			verbose(env,
+				"insn %u calls global subprog %d, which can throw while an exception is in flight\n",
+				i, subprog);
+			return -EINVAL;
+		}
+	}
+
+	if (!in_pad)
+		return 0;
+
+	if (insn->code == (BPF_JMP | BPF_EXIT)) {
+		verbose(env,
+			"exit at insn %u ends an exception cleanup landing pad: a catch pad is not supported yet, only cleanup pads that resume\n",
+			i);
+		return -EOPNOTSUPP;
+	}
+	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;
+	}
+	if (insn->code == (BPF_JMP | BPF_JA | BPF_X) ||
+	    insn->code == (BPF_JMP32 | BPF_JA | BPF_X)) {
+		verbose(env,
+			"indirect jump at insn %u is in an exception cleanup landing pad\n",
+			i);
+		return -EINVAL;
+	}
+	/* A BPF_LD_[ABS|IND] can leave the frame through its epilogue. */
+	if (BPF_CLASS(insn->code) == BPF_LD &&
+	    (BPF_MODE(insn->code) == BPF_ABS || BPF_MODE(insn->code) == BPF_IND)) {
+		verbose(env,
+			"BPF_LD_[ABS|IND] at insn %u is in an exception cleanup landing pad\n",
+			i);
+		return -EINVAL;
+	}
+	if (is_stack_arg_st(insn) || is_stack_arg_stx(insn)) {
+		verbose(env,
+			"insn %u stages an on-stack call argument in an exception cleanup landing pad\n",
+			i);
+		return -EINVAL;
+	}
+	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 calls a kfunc with an on-stack argument in an exception cleanup landing pad\n",
+				i);
+			return -EINVAL;
+		}
+	}
+	return 0;
+}
+
+bool bpf_is_unwind_resume_kfunc(const struct bpf_insn *insn)
+{
+	return bpf_pseudo_kfunc_call(insn) && insn->off == 0 &&
+	       insn->imm == bpf_unwind_resume_id[0];
+}
+
+int bpf_exc_pad_of_call(struct bpf_verifier_env *env, u32 idx)
+{
+	u32 pad = env->insn_aux_data[idx].cleanup_pad;
+
+	return pad ? (int)pad - 1 : -1;
+}
+
+/*
+ * 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_exc_force_spill(const struct bpf_prog *prog)
+{
+	return prog->aux->exc && !prog->aux->exception_cb;
+}
+
+bool bpf_exc_needs_throw_spill(const struct bpf_prog *prog)
+{
+	return bpf_exc_force_spill(prog) && prog->aux->exc->has_throw;
+}
+
+const struct bpf_cleanup_range *bpf_exc_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;
+}
+
+int bpf_exc_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_exc_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;
+
+	ranges = kvcalloc(cnt, sizeof(*ranges), GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+	if (!ranges) {
+		kvfree(recs);
+		return -ENOMEM;
+	}
+
+	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_exc_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_exc_free_info(struct bpf_prog_aux *aux)
+{
+	struct bpf_exception_info *exc = aux->exc;
+
+	if (!exc)
+		return;
+	kvfree(exc->ranges);
+	kvfree(exc->info);
+	kfree(exc);
+	aux->exc = NULL;
+}
+
+static const struct bpf_insn_aux_data *subprog_insn_aux(const struct bpf_verifier_env *env,
+							const struct bpf_prog *prog, u32 idx)
+{
+	if (!env || !prog->aux->exc)
+		return NULL;
+	return &env->insn_aux_data[idx + prog->aux->subprog_start];
+}
+
+bool bpf_exc_insn_is_pad(const struct bpf_verifier_env *env,
+			 const struct bpf_prog *prog, u32 idx)
+{
+	const struct bpf_insn_aux_data *aux = subprog_insn_aux(env, prog, idx);
+
+	return aux && aux->cleanup_pad_head;
+}
+
+bool bpf_exc_insn_is_throw(const struct bpf_verifier_env *env,
+			   const struct bpf_prog *prog, u32 idx)
+{
+	const struct bpf_insn_aux_data *aux = subprog_insn_aux(env, prog, idx);
+
+	return aux && aux->throw_call;
+}
+
+bool bpf_exc_insn_is_resume(const struct bpf_verifier_env *env,
+			    const struct bpf_prog *prog, u32 idx)
+{
+	const struct bpf_insn_aux_data *aux = subprog_insn_aux(env, prog, idx);
+
+	return aux && aux->resume_call;
+}
+
+bool bpf_exc_insn_in_pad(const struct bpf_verifier_env *env,
+			 const struct bpf_prog *prog, u32 idx)
+{
+	const struct bpf_insn_aux_data *aux = subprog_insn_aux(env, prog, idx);
+
+	return aux && aux->in_cleanup_pad;
+}
diff --git a/kernel/bpf/exception.h b/kernel/bpf/exception.h
new file mode 100644
index 0000000000000..7a4bfc201e1ce
--- /dev/null
+++ b/kernel/bpf/exception.h
@@ -0,0 +1,23 @@
+/* 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_insn;
+struct bpf_verifier_env;
+
+int bpf_prepare_cleanup_exceptions(struct bpf_verifier_env *env);
+int bpf_exc_check_insn(struct bpf_verifier_env *env, struct bpf_insn *insn);
+int bpf_exc_check_callback(struct bpf_verifier_env *env, int subprog);
+int bpf_exc_pad_of_call(struct bpf_verifier_env *env, u32 idx);
+int bpf_exc_alloc_info(struct bpf_prog_aux *aux);
+int bpf_exc_attach_info(struct bpf_prog_aux *aux, struct bpf_cleanup_info *recs, u32 cnt);
+const struct bpf_cleanup_range *bpf_exc_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..02e65c57bf0a5 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,29 @@ 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].throw_call =
+				data[off + cnt - 1].throw_call;
+			data[off + cnt - 1].throw_call = false;
+			data[i].resume_call =
+				data[off + cnt - 1].resume_call;
+			data[off + cnt - 1].resume_call = 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 +565,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 +590,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;
 }
 
@@ -648,7 +676,7 @@ int bpf_opt_remove_nops(struct bpf_verifier_env *env)
 	struct bpf_insn *insn = env->prog->insnsi;
 	int insn_cnt = env->prog->len;
 	bool is_may_goto_0, is_ja;
-	int i, err;
+	int i, j, err;
 
 	for (i = 0; i < insn_cnt; i++) {
 		is_may_goto_0 = !memcmp(&insn[i], &MAY_GOTO_0, sizeof(MAY_GOTO_0));
@@ -657,6 +685,11 @@ int bpf_opt_remove_nops(struct bpf_verifier_env *env)
 		if (!is_may_goto_0 && !is_ja)
 			continue;
 
+		if (env->cleanup_info_cnt)
+			for (j = 0; j < insn_cnt; j++)
+				if (env->insn_aux_data[j].cleanup_pad == i + 1)
+					env->insn_aux_data[j].cleanup_pad = i + 2;
+
 		err = verifier_remove_insns(env, i, 1);
 		if (err)
 			return err;
@@ -1095,6 +1128,61 @@ 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 exc_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_exc_alloc_info(sub->aux);
+	if (err)
+		return err;
+
+	for (i = start; i < end; i++) {
+		if (env->insn_aux_data[i].throw_call)
+			sub->aux->exc->has_throw = true;
+		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;
+		}
+		env->insn_aux_data[pad].cleanup_pad_head = true;
+		recs[cnt].begin_off = i - start;
+		recs[cnt].end_off = i - start + 1;
+		recs[cnt].landing_pad_off = pad - start;
+		cnt++;
+	}
+	return bpf_exc_attach_info(sub->aux, recs, cnt);
+}
+
+int bpf_exc_attach_main_prog(struct bpf_verifier_env *env, struct bpf_prog *prog)
+{
+	if (!env || env->subprog_cnt > 1)
+		return 0;
+	return exc_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 +1320,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 = exc_info_for_subprog(env, func[i], subprog_start,
+					   subprog_end);
+		if (err)
+			goto out_free;
 		func[i] = bpf_int_jit_compile(env, func[i]);
 		if (!func[i]->jited) {
 			err = -ENOTSUPP;
@@ -1336,6 +1428,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 +2010,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 (env->insn_aux_data[i + delta].resume_call)
+			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 301b35bd85c8a..03af739b99094 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
@@ -3398,8 +3399,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_exc_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;
@@ -3416,6 +3445,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;
@@ -3445,6 +3479,19 @@ __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)
+{
+	/* Never reached: a JIT emits the way out of a landing pad instead. */
+	WARN_ONCE(1, "A JIT should have replaced the exception cleanup resume\n");
+}
+
 __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;
@@ -5081,6 +5128,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..00ac8917a7166 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_exc_pad_of_call(env, idx);
+
+		if (pad >= 0)
+			succ->items[succ->cnt++] = pad;
+	}
+
 	return succ;
 }
 
@@ -397,6 +409,13 @@ 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);
+
+		if (!alive && unlikely(env->cleanup_info_cnt)) {
+			int pad = bpf_exc_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..b101baa43171c 100644
--- a/kernel/bpf/states.c
+++ b/kernel/bpf/states.c
@@ -996,6 +996,12 @@ 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 (old->unwinding && old->unwind_frameno != cur->unwind_frameno)
+		return false;
+
 	if (!refsafe(old, cur, &env->idmap_scratch))
 		return false;
 
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 74496fd716d3b..63c84a60c1b7d 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2924,7 +2924,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 a7c9e2d8965d5..6462e9f2ffdc3 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -37,6 +37,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) \
@@ -1725,6 +1726,8 @@ 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->unwind_frameno = src->unwind_frameno;
 	dst_state->curframe = src->curframe;
 	dst_state->branches = src->branches;
 	dst_state->parent = src->parent;
@@ -5599,6 +5602,17 @@ 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 lives in r9, which no spill area holds.
+	 * Refused on every arch rather than just that one. The subprograms
+	 * below are then checked against MAX_BPF_STACK together rather than
+	 * one at a time, so this can turn a program that would have loaded
+	 * with a private stack into one that is too deep.
+	 */
+	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);
 
@@ -10546,6 +10560,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_exc_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",
@@ -10597,8 +10615,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)
@@ -10688,7 +10706,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 */
@@ -11077,6 +11095,20 @@ static bool retval_range_within(struct bpf_retval_range range, const struct bpf_
 		return range.minval <= reg_smin(reg) && reg_smax(reg) <= range.maxval;
 }
 
+static u32 pop_frame(struct bpf_verifier_env *env)
+{
+	struct bpf_verifier_state *state = env->cur_state;
+	struct bpf_func_state *callee = state->frame[state->curframe];
+	struct bpf_func_state *caller = state->frame[state->curframe - 1];
+	u32 callsite = callee->callsite;
+
+	account_processed_insns(env, callee, caller);
+	free_func_state(callee);
+	state->frame[state->curframe--] = NULL;
+	invalidate_outgoing_stack_args(env, caller);
+	return callsite;
+}
+
 static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
 {
 	struct bpf_verifier_state *state = env->cur_state, *prev_st;
@@ -11156,12 +11188,7 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
 		verbose(env, "to caller at %d:\n", *insn_idx);
 		print_verifier_state(env, state, caller->frameno, true);
 	}
-	account_processed_insns(env, callee, caller);
-	/* clear everything in the callee. In case of exceptional exits using
-	 * bpf_throw, this will be done by copy_verifier_state for extra frames. */
-	free_func_state(callee);
-	state->frame[state->curframe--] = NULL;
-	invalidate_outgoing_stack_args(env, caller);
+	pop_frame(env);
 
 	/* for callbacks widen imprecise scalars to make programs like below verify:
 	 *
@@ -14660,7 +14687,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;
 }
@@ -18585,9 +18612,80 @@ enum {
 	INSN_IDX_UPDATED = 2,
 };
 
-static int process_bpf_exit_full(struct bpf_verifier_env *env,
-				 bool *do_print_state,
-				 bool exception_exit)
+static void unwind_enter_pad(struct bpf_verifier_env *env)
+{
+	struct bpf_verifier_state *state = env->cur_state;
+	struct bpf_func_state *frame = cur_func(env);
+
+	state->unwind_frameno = state->curframe;
+	clear_caller_saved_regs(env, frame->regs);
+	mark_reg_unknown(env, frame->regs, BPF_REG_0);
+}
+
+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;
+	u8 popped = 0;
+
+	state->unwinding = true;
+	for (;;) {
+		int pad = bpf_exc_pad_of_call(env, callsite);
+
+		if (pad >= 0) {
+			unwind_enter_pad(env);
+			/*
+			 * The edge from @callsite to the pad crosses @popped
+			 * frames, and nothing in the instruction stream says
+			 * so. Record it for mark_chain_precision(), which has
+			 * to walk back through the same frames.
+			 */
+			env->unwind_frames = popped;
+			*insn_idx = pad;
+			return INSN_IDX_UPDATED;
+		}
+		if (!state->curframe)
+			return unwind_finish(env);
+		callsite = pop_frame(env);
+		popped++;
+	}
+}
+
+static int process_cleanup_resume(struct bpf_verifier_env *env, int *insn_idx)
+{
+	struct bpf_verifier_state *state = env->cur_state;
+	int err;
+
+	if (!state->unwinding) {
+		verbose(env,
+			"bpf_unwind_resume() at insn %u reached without an exception in flight\n",
+			(u32)*insn_idx);
+		return -EINVAL;
+	}
+	if (state->curframe != state->unwind_frameno) {
+		verbose(env,
+			"bpf_unwind_resume() at insn %u is in frame %u, not frame %u whose landing pad the exception entered\n",
+			(u32)*insn_idx, state->curframe, state->unwind_frameno);
+		return -EINVAL;
+	}
+	if (!state->curframe)
+		return unwind_finish(env);
+	err = unwind_step(env, pop_frame(env), insn_idx);
+	/* unwind_step() counted the frames it popped, not the one popped here. */
+	if (err == INSN_IDX_UPDATED)
+		env->unwind_frames++;
+	return err;
+}
+
+static int process_bpf_exit_full(struct bpf_verifier_env *env, bool *do_print_state)
 {
 	struct bpf_func_state *cur_frame = cur_func(env);
 
@@ -18597,25 +18695,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);
@@ -18789,6 +18873,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 &&
@@ -18822,7 +18908,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);
 	}
@@ -18863,6 +18949,9 @@ static int do_check(struct bpf_verifier_env *env)
 
 		/* reset current history entry on each new instruction */
 		env->cur_hist_ent = NULL;
+		/* frames the unwind popped to reach this insn, if it is a pad */
+		env->cur_unwind_frames = env->unwind_frames;
+		env->unwind_frames = 0;
 
 		env->prev_insn_idx = prev_insn_idx;
 		if (env->insn_idx >= insn_cnt) {
@@ -18926,7 +19015,7 @@ static int do_check(struct bpf_verifier_env *env)
 			}
 		}
 
-		if (bpf_is_jmp_point(env, env->insn_idx)) {
+		if (bpf_is_jmp_point(env, env->insn_idx) || env->cur_unwind_frames) {
 			err = bpf_push_jmp_history(env, state, 0, 0, 0, 0);
 			if (err)
 				return err;
@@ -18967,6 +19056,12 @@ static int do_check(struct bpf_verifier_env *env)
 				return err;
 		}
 
+		if (unlikely(env->cleanup_info_cnt)) {
+			err = bpf_exc_check_insn(env, insn);
+			if (err)
+				return err;
+		}
+
 		sanitize_mark_insn_seen(env);
 		prev_insn_idx = env->insn_idx;
 
@@ -21792,6 +21887,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)
@@ -21999,6 +22099,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 0aaa54359aebc..646aa8758d48d 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 */
@@ -7599,6 +7602,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 a9de7f107cf7b..03315b3a3a19f 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_rec_size = OPTS_GET(opts, cleanup_info_rec_size, 0);
+	attr.cleanup_info = ptr_to_u64(OPTS_GET(opts, cleanup_info, NULL));
+	attr.cleanup_info_cnt = OPTS_GET(opts, cleanup_info_cnt, 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 490e8cb4ba537..ec542fd68f631 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..2345fbdd46f56 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,11 @@ 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, if there is one */
+	if (load_attr->cleanup_info_cnt)
+		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 cd1ea1bb53cbf..d0e601b3499c0 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,26 @@ 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 +722,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;
@@ -773,6 +801,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;
@@ -819,7 +850,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;
@@ -1561,6 +1595,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;
 
@@ -4047,6 +4082,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;
@@ -4074,6 +4112,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,
@@ -4854,6 +4893,238 @@ 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[CLEANUP_REC_FIELDS];
+		void *tmp;
+
+		if (raw->sec_idx[CLEANUP_REC_BEGIN] == raw->sec_idx[CLEANUP_REC_END] &&
+		    raw->insn_idx[CLEANUP_REC_BEGIN] >= raw->insn_idx[CLEANUP_REC_END]) {
+			pr_warn("%s: record %zu is an empty range [%zu,%zu)\n",
+				CLEANUP_SEC, i, raw->insn_idx[CLEANUP_REC_BEGIN],
+				raw->insn_idx[CLEANUP_REC_END]);
+			return -LIBBPF_ERRNO__FORMAT;
+		}
+
+		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 ends at instruction 0\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;
+
+		ci.begin_off = fields[CLEANUP_REC_BEGIN];
+		ci.end_off = fields[CLEANUP_REC_END];
+		ci.landing_pad_off = fields[CLEANUP_REC_PAD];
+
+		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);
+	}
+
+	if (!prog->cleanup_info_cnt)
+		return 0;
+
+	if (prog->cleanup_info_cnt > INT32_MAX / sizeof(struct bpf_cleanup_info)) {
+		pr_warn("prog '%s': too many cleanup records: %u\n",
+			prog->name, prog->cleanup_info_cnt);
+		return -LIBBPF_ERRNO__FORMAT;
+	}
+
+	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)
 {
@@ -7589,6 +7860,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];
@@ -7779,6 +8057,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)
@@ -8051,6 +8332,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;
@@ -8379,6 +8665,16 @@ 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;
@@ -8395,12 +8691,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;
@@ -8634,6 +8930,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;
 
@@ -8876,17 +9173,20 @@ 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 *local_name, *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);
+	local_name = ext->essent_name ?: ext->name;
+	kern_name = kern_extern_name(local_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);
+			kern_name != local_name ? kern_name : ext->name);
 		return kfunc_id;
 	}
 
@@ -9745,6 +10045,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);
@@ -10143,6 +10446,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..95f2a5243eda6 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;
@@ -2258,6 +2259,7 @@ static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *ob
 			if (ELF64_ST_TYPE(src_sym->st_info) == STT_SECTION) {
 				struct src_sec *sec = &obj->secs[src_sym->st_shndx];
 				struct bpf_insn *insn;
+				__u32 *val;
 
 				if (src_linked_sec->shdr->sh_flags & SHF_EXECINSTR) {
 					/* calls to the very first static function inside
@@ -2274,6 +2276,40 @@ 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) {
+					/*
+					 * 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.
+					 */
+
+					/*
+					 * Only an executable section has its
+					 * r_offset checked, and even there only
+					 * for alignment; SHT_NOBITS has no
+					 * raw_data at all. Bound it here,
+					 * subtracting so it cannot wrap.
+					 */
+					if (!dst_linked_sec->raw_data ||
+					    dst_linked_sec->sec_sz < (int)sizeof(*val) ||
+					    dst_rel->r_offset % sizeof(*val) ||
+					    dst_rel->r_offset >
+					    (size_t)dst_linked_sec->sec_sz - sizeof(*val)) {
+						pr_warn("ELF relo #%d in section #%zu points outside the data of section '%s' in %s\n",
+							j, src_sec->sec_idx,
+							dst_linked_sec->sec_name,
+							obj->filename);
+						return -EINVAL;
+					}
+					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.skel b/tools/testing/selftests/bpf/Makefile.skel
index 580d1d82c1867..6d7518fa4018b 100644
--- a/tools/testing/selftests/bpf/Makefile.skel
+++ b/tools/testing/selftests/bpf/Makefile.skel
@@ -33,7 +33,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..68f2d61a694b5
--- /dev/null
+++ b/tools/testing/selftests/bpf/exceptions_cleanup.h
@@ -0,0 +1,56 @@
+/* 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
+#define RAN_NOP_PAD		0x20000
+#define RAN_VAR_STACK		0x40000
+#define RAN_GLOBAL_PAD		0x80000
+#define RAN_PAD_R0		0x100000
+
+/* 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..323ee8d72c913
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
@@ -0,0 +1,524 @@
+// 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");
+	/* bump() is not a landing pad; it sets its bit on every run. */
+	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 walks past. */
+	if (test__start_subtest("shared_callee_no_throw")) {
+		skel->bss->outer_input = 0;
+		run_shape(skel, skel->progs.entry_shared, 1, 2, 0);
+	}
+	if (test__start_subtest("shared_callee_throw")) {
+		skel->bss->outer_input = 0;
+		run_shape(skel, skel->progs.entry_shared, 101, THROW_COOKIE, RAN_SHARED);
+	}
+	if (test__start_subtest("shared_callee_uncovered_throw")) {
+		skel->bss->outer_input = 101;
+		run_shape(skel, skel->progs.entry_shared, 1, THROW_COOKIE, 0);
+		skel->bss->outer_input = 0;
+	}
+
+	/* 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 -- though it
+	 * is reachable, so a walk past the boundary would find it.
+	 */
+	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 and handed to a
+	 * bpf_loop() the verifier never reaches: the callback check has to fire
+	 * on the helper call, not on 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");
+		}
+	}
+
+	/*
+	 * A pad whose first instruction opt_remove_nops() deletes: the record
+	 * has to follow the pad rather than be dropped with the nop.
+	 */
+	if (test__start_subtest("nop_at_pad_head"))
+		run_shape(skel, skel->progs.entry_nop_pad, 101, THROW_COOKIE,
+			  RAN_NOP_PAD);
+
+	/*
+	 * A pad that indexes its frame's stack by a register the frame set
+	 * before the throwing call, marked precise back across the unwind edge.
+	 */
+	if (test__start_subtest("pad_var_stack_offset"))
+		run_shape(skel, skel->progs.entry_var_stack, 101, THROW_COOKIE,
+			  RAN_VAR_STACK);
+
+	/*
+	 * The same, where the throw is in a global subprogram, which the
+	 * verifier walks without a frame of its own.
+	 */
+	if (test__start_subtest("pad_over_global_subprog"))
+		run_shape(skel, skel->progs.entry_global_pad, 101, THROW_COOKIE,
+			  RAN_GLOBAL_PAD);
+
+	/*
+	 * A pad that indexes its frame by r0, which no insn in the frame
+	 * wrote: the precision request for it must not cross the unwind edge.
+	 */
+	if (test__start_subtest("pad_r0_precision"))
+		run_shape(skel, skel->progs.entry_pad_r0, 101, THROW_COOKIE,
+			  RAN_PAD_R0);
+
+	exceptions_cleanup_shapes__destroy(skel);
+}
+
+/*
+ * A table with more records than the program has instructions: no valid one
+ * can look like that, and it is refused before the kernel allocates for it.
+ */
+static void test_cleanup_info_cnt(void)
+{
+	struct bpf_insn insns[] = {
+		BPF_MOV64_IMM(BPF_REG_0, 0),
+		BPF_EXIT_INSN(),
+	};
+	struct bpf_cleanup_info rec = {
+		.begin_off = 0,
+		.end_off = 1,
+		.landing_pad_off = 1,
+	};
+	char log[512] = {};
+
+	LIBBPF_OPTS(bpf_prog_load_opts, opts,
+		    .log_buf = log,
+		    .log_size = sizeof(log),
+		    .log_level = 1,
+		    .cleanup_info = &rec,
+		    .cleanup_info_cnt = 1 << 20,
+		    .cleanup_info_rec_size = sizeof(rec));
+	int fd;
+
+	fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
+			   insns, ARRAY_SIZE(insns), &opts);
+	if (!ASSERT_LT(fd, 0, "load")) {
+		close(fd);
+		return;
+	}
+	/*
+	 * Turned away on the count, rather than on whatever the records past
+	 * the one below happen to hold.
+	 */
+	ASSERT_HAS_SUBSTR(log, "cleanup info has 1048576 records for 2 instructions",
+			  "log");
+}
+
+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")) {
+			printf("%s:SKIP:JIT cannot dispatch exception cleanup landing pads\n",
+			       __func__);
+			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);
+
+	if (test__start_subtest("cleanup_info_cnt"))
+		test_cleanup_info_cnt();
+}
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..3b79663579122
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup.c
@@ -0,0 +1,164 @@
+// 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 */
+		/*
+		 * r0 at pad entry is whatever the walker leaves
+		 * there, carried across the call in a callee-saved
+		 * register and handed to the resume, the way a
+		 * compiler-emitted pad passes the exception pointer to
+		 * _Unwind_Resume. The kfunc ignores it, and the two pads
+		 * below do without the shuffle.
+		 */
+		"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);
+}
+
+/*
+ * A frame with no cleanup record: an exception leaving it runs no pad. The
+ * throw never fires -- @never is global -- and the bit marks the return path.
+ */
+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..a7569f98a98de
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_fail.c
@@ -0,0 +1,678 @@
+// 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. The
+ * walk takes both, and the exit is refused the same way a catch pad's is.
+ */
+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("a catch pad is not supported yet")
+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("throws 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 global subprogram which can throw. Not case 3:
+ * the verifier does not walk a global subprogram, so the throw inside it is
+ * never reached, and what refuses the call is subprog_info.might_throw.
+ */
+__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; no exception is in flight when the walk gets
+ * there.
+ */
+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("reached without an exception in flight")
+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. An exception is
+ * in flight, so what refuses it is the frame it is in: a resume reaches the
+ * walker only from the frame whose pad the walker called.
+ */
+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 in frame 2, not frame 1 whose landing pad the exception entered")
+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;"
+	"call bpf_kfunc_call_test_pair_arg_nofit;"
+	"call bpf_unwind_resume;"
+	"exit;"
+	CLEANUP_REC("1b", "2b", "3b")
+	::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("calls a kfunc with an on-stack 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. The instruction it lands on would have to run both
+ * with an exception in flight and without, which the walk refuses.
+ */
+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("runs both inside and outside an exception cleanup landing pad")
+int jump_into_pad(void *ctx)
+{
+	return jump_into_pad_frame();
+}
+
+#if defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)
+
+/*
+ * 16. An indirect jump in a landing pad: the pad does not own the stack its
+ * targets would run on. A jump table entry is an offset from the program's
+ * section symbol, which has to be spelled in quotes here.
+ */
+static __used __naked __noinline void gotox_thrower(void)
+{
+	asm volatile (
+	"r1 = 15;"
+	"call bpf_throw;"
+	"exit;"
+	::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("indirect jump at insn")
+__naked void gotox_in_pad(void)
+{
+	asm volatile (
+	".pushsection .jumptables,\"\",@progbits;"
+"jt0_%=:"
+	".quad l0_%= - \"?syscall\";"
+	".quad l1_%= - \"?syscall\";"
+	".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, as raw bytes: the mnemonic only reached the LLVM
+	 * assembler in llvm 22, and BPF_RAW_INSN() needs <linux/bpf.h>, which
+	 * vmlinux.h rules out. dst_reg is the other nibble on a big-endian
+	 * target.
+	 */
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+	".byte 0x0d, 0x20, 0, 0, 0, 0, 0, 0;"
+#else
+	".byte 0x0d, 0x02, 0, 0, 0, 0, 0, 0;"
+#endif
+"l0_%=:"
+	"call bpf_unwind_resume;"
+	"exit;"
+"l1_%=:"
+	"call bpf_unwind_resume;"
+	"exit;"
+	CLEANUP_REC("1b", "2b", "3b")
+	::: __clobber_all);
+}
+
+#endif /* x86 || arm64 */
+
+/*
+ * 17. A BPF_LD_[ABS|IND] in a landing pad. A failed load leaves the
+ * subprogram through the hidden "r0 = 0; exit" gen_ld_abs() patches in, and
+ * that exit is the epilogue, which unwinds a stack the pad does not own.
+ */
+static __used __naked __noinline __u64 ld_abs_pad_frame(void)
+{
+	asm volatile (
+	"r6 = r1;"			/* the skb BPF_LD_ABS reads */
+"1:"	"call inner_throw;"		/* cleanup region */
+"2:"
+	"r0 = 0;"
+	"exit;"
+"3:"					/* landing pad */
+	"r0 = *(u32 *)skb[0];"
+	"call bpf_unwind_resume;"
+	"exit;"
+	CLEANUP_REC("1b", "2b", "3b")
+	::: __clobber_all);
+}
+
+SEC("?tc")
+__failure __msg("is in an exception cleanup landing pad")
+__naked void ld_abs_in_pad(void)
+{
+	asm volatile (
+	"call ld_abs_pad_frame;"
+	"exit;"
+	::: __clobber_all);
+}
+
+/*
+ * 18. A bpf_unwind_resume() in a subprogram a landing pad called, which has a
+ * pad of its own and reached it by ordinary control flow. Not case 9: an
+ * exception is in flight, but this is not the frame whose pad the walker ran.
+ */
+static __used __naked __noinline __u64 own_pad_callee(void)
+{
+	asm volatile (
+	"r1 = %[input] ll;"
+	"r1 = *(u64 *)(r1 + 0);"
+	"if r1 == 0 goto 3f;"		/* an ordinary branch into its pad */
+"1:"	"call bpf_preempt_disable;"	/* cleanup region: nothing that throws */
+	"call bpf_preempt_enable;"
+"2:"
+	"r0 = 0;"
+	"exit;"
+"3:"					/* its landing pad */
+	"call bpf_unwind_resume;"
+	"exit;"
+	CLEANUP_REC("1b", "2b", "3b")
+	:
+	: __imm_addr(input)
+	: __clobber_all);
+}
+
+static __used __naked __noinline __u64 pad_calls_own_pad_frame(void)
+{
+	asm volatile (
+"1:"	"call inner_throw;"		/* cleanup region */
+"2:"
+	"r0 = 0;"
+	"exit;"
+"3:"					/* landing pad, which calls the above */
+	"call own_pad_callee;"
+	"call bpf_unwind_resume;"
+	"exit;"
+	CLEANUP_REC("1b", "2b", "3b")
+	::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("is in frame 2, not frame 1 whose landing pad the exception entered")
+int resume_in_callee_own_pad(void *ctx)
+{
+	return pad_calls_own_pad_frame();
+}
+
+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..f44af8be4fda1
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c
@@ -0,0 +1,1049 @@
+// 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 outer_input = 0;
+__u64 magic = 0x5eed;
+__u64 pads_ran = 0;
+__u64 pad_runs = 0;
+
+/*
+ * 1. Everything a cleanup table leaves dead: a throw's continuation, the tail
+ * after a resume with an ld_imm64 and a branch in it, and the block only that
+ * continuation reaches.
+ */
+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
+ * belongs to the call site, not the callee. Either site can throw; the RCU
+ * lock is taken between them, so only the covered call unwinds holding it.
+ */
+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 = %[outer_input] ll;"
+	"r1 = *(u64 *)(r1 + 0);"
+	"call shared_callee;"		/* uncovered: no pad for its unwind */
+	"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(outer_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: the callee
+ * overwrites r6-r9 before it throws, so the check passes only if the walker
+ * found the spill in the discarded callee's prologue.
+ */
+#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, which moves the spill the walker reads. 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 at entry is
+ * the only place to get a context 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 walker finds the outer bpf_prog's ksym, so the table has
+ * to be handed over or the pad is never dispatched.
+ */
+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, 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;"
+	/*
+	 * Unnarrowed, the way entry_freplace hands fr_callee its argument: a
+	 * guard here would prune the callee's throw and let the sweep take the
+	 * pad, leaving no record for a walk past the boundary to match.
+	 */
+	"r2 = *(u64 *)(r1 + 0);"
+	"r1 = *(u64 *)(r10 - 8);"
+"1:"	"call tc_taken_callee;"		/* cleanup region */
+"2:"
+	"exit;"				/* the cookie, delivered at tc_target */
+"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.
+ */
+__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. */
+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 throwing frame is both the
+ * frame the record covers and the boundary, so the pad runs on the way to
+ * delivering the cookie.
+ */
+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 it coming, so what stops a nested exception is the
+ * walk ending in the extension's frame; pad_runs says the pad ran once.
+ */
+__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 last, where the exception
+ * callback patchlet -- the one that does not keep the call it replaced in the
+ * last slot -- has to carry the marks with it; r6-r9 reports a lost one.
+ */
+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. Well formed, and a
+ * rule against it would turn it 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 reloads from and stores to its own frame -- the shape every
+ * compiler-generated pad has. A JIT that addresses the frame through the stack
+ * pointer, arm64, has to find a pad's frame another way.
+ */
+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.
+ */
+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: LLVM emits _Unwind_Resume() and
+ * libbpf maps it onto bpf_unwind_resume(), which every other pad here calls.
+ */
+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. A tail call in
+ * the pad itself is refused, but the callee's prologue really did run, so its
+ * tail call releases exactly that and the target returns into the pad.
+ */
+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 at entry is
+ * the only place to get a context for bpf_tail_call(); the pad reloads it
+ * from the 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 with a table of its
+ * own, throwing inside the outer exception. The tail call made it a boundary,
+ * so the inner walk ends there and the outer pad, cookie and r6-r9 survive.
+ */
+__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);
+}
+
+/*
+ * 19. A landing pad whose first instruction is a nop. opt_remove_nops() runs
+ * long after the cleanup walk, so the record has to follow the pad to the
+ * instruction that takes its place rather than be dropped with the nop.
+ */
+static __used __naked __noinline __u64 nop_pad_frame(void)
+{
+	asm volatile (
+	"r1 = %[input] ll;"
+	"r6 = *(u64 *)(r1 + 0);"
+	"if r6 < 101 goto 6f;"
+	"r1 = %[cookie];"
+"1:"	"call bpf_throw;"		/* cleanup region */
+"2:"
+"6:"
+	"r0 = 0;"
+	"exit;"
+"3:"					/* landing pad: a nop, then its body */
+	"goto +0;"
+	PAD_RAN("%[ran]")
+	"call bpf_unwind_resume;"
+	"exit;"
+	CLEANUP_REC("1b", "2b", "3b")
+	:
+	: [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_NOP_PAD),
+	  __imm_addr(input), __imm_addr(pads_ran)
+	: __clobber_all);
+}
+
+SEC("syscall")
+int entry_nop_pad(void *ctx)
+{
+	return nop_pad_frame();
+}
+
+/*
+ * 20. A landing pad that indexes its own frame's stack by a register the frame
+ * set before the throwing call. The offset is not a constant, so r6 has to be
+ * marked precise from inside the pad, back across the unwind edge.
+ */
+/*
+ * A thrower that touches none of r6-r9, so nothing in it can answer for the
+ * pad frame's r6 and the walk has to leave this frame to look.
+ */
+static __used __naked __noinline __u64 var_thrower(void)
+{
+	asm volatile (
+	"if r1 < 101 goto 1f;"
+	"r1 = %[cookie];"
+	"call bpf_throw;"
+"1:"
+	"r0 = 0;"
+	"exit;"
+	:
+	: [cookie]"i"(THROW_COOKIE)
+	: __clobber_all);
+}
+
+static __used __naked __noinline __u64 var_stack_frame(void)
+{
+	asm volatile (
+	"r1 = %[magic] ll;"
+	"r1 = *(u64 *)(r1 + 0);"
+	"*(u64 *)(r10 - 8) = r1;"	/* the slot the pad will read... */
+	"*(u64 *)(r10 - 16) = r1;"	/* ...whichever of the two it is */
+	"r1 = %[input] ll;"
+	"r6 = *(u64 *)(r1 + 0);"
+	"r6 &= 1;"			/* an unknown slot number... */
+	"r6 <<= 3;"			/* ...as an aligned byte offset */
+	"r1 = %[input] ll;"
+	"r1 = *(u64 *)(r1 + 0);"
+"1:"	"call var_thrower;"		/* cleanup region */
+"2:"
+	"r0 = 0;"
+	"exit;"
+"3:"					/* landing pad */
+	"r7 = r0;"
+	"r1 = r10;"
+	"r1 += r6;"			/* variable offset into the frame */
+	"r2 = *(u64 *)(r1 - 16);"
+	"r3 = %[magic] ll;"
+	"r3 = *(u64 *)(r3 + 0);"
+	"if r2 != r3 goto 9f;"
+	PAD_RAN("%[ran]")
+"9:"
+	"r1 = r7;"
+	"call bpf_unwind_resume;"
+	"exit;"
+	CLEANUP_REC("1b", "2b", "3b")
+	:
+	: [ran]"i"(RAN_VAR_STACK), __imm_addr(input), __imm_addr(magic),
+	  __imm_addr(pads_ran)
+	: __clobber_all);
+}
+
+SEC("syscall")
+int entry_var_stack(void *ctx)
+{
+	return var_stack_frame();
+}
+
+/*
+ * 21. The same walk, over a call to a global subprogram: the verifier enters
+ * no frame for one, so the exception arrives at the pad from the call site
+ * rather than from a throw some frames deeper.
+ */
+__noinline __u64 global_thrower(__u64 x)
+{
+	if (x > 100)
+		bpf_throw(THROW_COOKIE);
+	return x + 1;
+}
+
+static __used __naked __noinline __u64 global_pad_frame(void)
+{
+	asm volatile (
+	"r1 = %[magic] ll;"
+	"r1 = *(u64 *)(r1 + 0);"
+	"*(u64 *)(r10 - 8) = r1;"
+	"*(u64 *)(r10 - 16) = r1;"
+	"r1 = %[input] ll;"
+	"r6 = *(u64 *)(r1 + 0);"
+	"r6 &= 1;"
+	"r6 <<= 3;"
+	"r1 = %[input] ll;"
+	"r1 = *(u64 *)(r1 + 0);"
+"1:"	"call global_thrower;"		/* cleanup region */
+"2:"
+	"r0 = 0;"
+	"exit;"
+"3:"					/* landing pad */
+	"r7 = r0;"
+	"r1 = r10;"
+	"r1 += r6;"
+	"r2 = *(u64 *)(r1 - 16);"
+	"r3 = %[magic] ll;"
+	"r3 = *(u64 *)(r3 + 0);"
+	"if r2 != r3 goto 9f;"
+	PAD_RAN("%[ran]")
+"9:"
+	"r1 = r7;"
+	"call bpf_unwind_resume;"
+	"exit;"
+	CLEANUP_REC("1b", "2b", "3b")
+	:
+	: [ran]"i"(RAN_GLOBAL_PAD), __imm_addr(input), __imm_addr(magic),
+	  __imm_addr(pads_ran)
+	: __clobber_all);
+}
+
+SEC("syscall")
+int entry_global_pad(void *ctx)
+{
+	return global_pad_frame();
+}
+
+/*
+ * 22. A landing pad that uses r0 as a variable stack offset. No instruction
+ * in the frame writes r0 -- the walker leaves it on the way in -- so the
+ * precision that access asks for has to end at the unwind edge, not cross it.
+ */
+static __used __naked __noinline __u64 pad_r0_frame(void)
+{
+	asm volatile (
+	"r1 = %[magic] ll;"
+	"r1 = *(u64 *)(r1 + 0);"
+	"*(u64 *)(r10 - 8) = r1;"	/* the slot the pad will read... */
+	"*(u64 *)(r10 - 16) = r1;"	/* ...whichever of the two it is */
+"1:"	"call regs_thrower;"		/* cleanup region */
+"2:"
+	"r0 = 0;"
+	"exit;"
+"3:"					/* landing pad */
+	"r0 &= 1;"			/* an unknown slot number... */
+	"r0 <<= 3;"			/* ...as an aligned byte offset */
+	"r1 = r10;"
+	"r1 += r0;"			/* variable offset into the frame */
+	"r2 = *(u64 *)(r1 - 16);"
+	"r3 = %[magic] ll;"
+	"r3 = *(u64 *)(r3 + 0);"
+	"if r2 != r3 goto 9f;"
+	PAD_RAN("%[ran]")
+"9:"
+	"call bpf_unwind_resume;"
+	"exit;"
+	CLEANUP_REC("1b", "2b", "3b")
+	:
+	: [ran]"i"(RAN_PAD_R0), __imm_addr(magic), __imm_addr(pads_ran)
+	: __clobber_all);
+}
+
+SEC("syscall")
+int entry_pad_r0(void *ctx)
+{
+	return pad_r0_frame();
+}
+
+char _license[] SEC("license") = "GPL";

21/3 2026/09/25 10:06 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 76661
							output: 26
							thoughts: 2275
						

Thoughts:
**KMSAN Analysis of BPF Exception Cleanup Patch**

Okay, let's dive into this BPF exception cleanup patch. My goal is to determine if this introduces any risks that KMSAN (Kernel Memory Sanitizer) might detect, particularly uninitialized memory reads or information leaks to user space. Given my expertise, I need to be thorough.

First, I need to remind myself of the KMSAN triggers. We're looking for kernel data structures sent to user space where fields might be uninitialized, conditional logic depending on uninitialized variables, complex data structures with potentially uninitialized fields, or anything involving bounds checks that might allow reading uninitialized bytes. Crucially, I have to ensure that any potential issues aren't already caught by KASAN, LOCKDEP, or other standard bug detectors.

Let's start by methodically examining any copies to user space, memory allocations, and struct field initializations introduced by this patch, with a focus on any data structures involved, and any exported information. The target architecture is amd64, so I will analyze the relevant functions.

I began by looking at `kernel/bpf/check_btf.c` and specifically the `check_cleanup_info` function, which appears to deal with cleanup information. It contains a critical section that copies data to user space via `copy_to_bpfptr_offset`. Upon review, the `min_size` variable being copied is fully initialized. No uninitialized memory is exposed to user space in this section.

Next, I did a full text search of the relevant code for every call of `copy_to` and any user space copy operations to ensure no other functions export data that is uninitialized. The result: `min_size` is the only instance, and it is initialized.

Now, let's address memory allocations. `kvcalloc` and `kzalloc_obj` are the primary allocation functions here, which zero-initializes the memory.

`bpf_exc_attach_info` allocates `ranges` with `kvcalloc` (zeroed). Inside the loop of `exc_info_for_subprog`, I see `kvmalloc_array` being used to allocate `recs`. This is where I need to be careful. The patch calculates the size of `recs` based on a loop that seems to count the cleanup records. I need to be very precise here: All elements of `recs` are fully initialized. Each element's `begin_off`, `end_off`, and `landing_pad_off` fields, totaling 12 bytes (`struct bpf_cleanup_info` has 3 u32 fields with no padding) are completely filled before being passed. Subsequently, `bpf_exc_fill_native_ranges` uses these fields to populate ranges.

However, I have to be absolutely certain that this information is not exported to user space. *Does* `BPF_OBJ_GET_INFO_BY_FD` export cleanup info? I need to search the kernel code for every instance of `BPF_OBJ_GET_INFO_BY_FD` to verify. I need to be sure that *nothing* from the cleanup info is sent to user space via BPF object info retrieval. I will perform that search to definitively conclude.



22/3 2026/09/25 10:07 tool grepper
0m Args:
{
  "Expression": "bpf_prog_get_info_by_fd"
}

Results:
{
  "Output": "\nFull output is too long, showing 500 out of 517 lines.\nUse more precise expression if possible.\n\nkernel/bpf/syscall.c=5081=static int set_info_rec_size(struct bpf_prog_info *info)\n--\nkernel/bpf/syscall.c-5111-\nkernel/bpf/syscall.c:5112:static int bpf_prog_get_info_by_fd(struct file *file,\nkernel/bpf/syscall.c-5113-\t\t\t\t   struct bpf_prog *prog,\n--\nkernel/bpf/syscall.c=5548=static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,\n--\nkernel/bpf/syscall.c-5558-\tif (fd_file(f)-\u003ef_op == \u0026bpf_prog_fops)\nkernel/bpf/syscall.c:5559:\t\treturn bpf_prog_get_info_by_fd(fd_file(f), fd_file(f)-\u003eprivate_data, attr,\nkernel/bpf/syscall.c-5560-\t\t\t\t\t      uattr);\n--\nsamples/bpf/xdp_adjust_tail_user.c=82=int main(int argc, char **argv)\n--\nsamples/bpf/xdp_adjust_tail_user.c-186-\nsamples/bpf/xdp_adjust_tail_user.c:187:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len);\nsamples/bpf/xdp_adjust_tail_user.c-188-\tif (err) {\n--\nsamples/bpf/xdp_fwd_user.c=50=static int do_detach(int ifindex, const char *ifname, const char *app_name)\n--\nsamples/bpf/xdp_fwd_user.c-78-\nsamples/bpf/xdp_fwd_user.c:79:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026prog_info, \u0026info_len);\nsamples/bpf/xdp_fwd_user.c-80-\tif (err) {\nsamples/bpf/xdp_fwd_user.c:81:\t\tprintf(\"ERROR: bpf_prog_get_info_by_fd failed (%s)\\n\",\nsamples/bpf/xdp_fwd_user.c-82-\t\t       strerror(errno));\n--\nsamples/bpf/xdp_tx_iptunnel_user.c=152=int main(int argc, char **argv)\n--\nsamples/bpf/xdp_tx_iptunnel_user.c-297-\nsamples/bpf/xdp_tx_iptunnel_user.c:298:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len);\nsamples/bpf/xdp_tx_iptunnel_user.c-299-\tif (err) {\n--\ntools/bpf/bpftool/btf.c=919=static int do_dump(int argc, char **argv)\n--\ntools/bpf/bpftool/btf.c-980-\ntools/bpf/bpftool/btf.c:981:\t\terr = bpf_prog_get_info_by_fd(fd, \u0026info, \u0026len);\ntools/bpf/bpftool/btf.c-982-\t\tif (err) {\n--\ntools/bpf/bpftool/btf.c=1208=build_btf_type_table(struct hashmap *tab, enum bpf_obj_type type,\n--\ntools/bpf/bpftool/btf.c-1269-\t\tif (type == BPF_OBJ_PROG)\ntools/bpf/bpftool/btf.c:1270:\t\t\terr = bpf_prog_get_info_by_fd(fd, info, len);\ntools/bpf/bpftool/btf.c-1271-\t\telse\n--\ntools/bpf/bpftool/btf_dumper.c=31=static int dump_prog_id_as_func_ptr(const struct btf_dumper *d,\n--\ntools/bpf/bpftool/btf_dumper.c-59-\ntools/bpf/bpftool/btf_dumper.c:60:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len);\ntools/bpf/bpftool/btf_dumper.c-61-\tif (err)\n--\ntools/bpf/bpftool/btf_dumper.c-72-\ntools/bpf/bpftool/btf_dumper.c:73:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len);\ntools/bpf/bpftool/btf_dumper.c-74-\tif (err)\n--\ntools/bpf/bpftool/cgroup.c=141=static int show_bpf_prog(int id, enum bpf_attach_type attach_type,\n--\ntools/bpf/bpftool/cgroup.c-155-\ntools/bpf/bpftool/cgroup.c:156:\tif (bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len)) {\ntools/bpf/bpftool/cgroup.c-157-\t\tclose(prog_fd);\n--\ntools/bpf/bpftool/common.c=413=void get_prog_full_name(const struct bpf_prog_info *prog_info, int prog_fd,\n--\ntools/bpf/bpftool/common.c-435-\ntools/bpf/bpftool/common.c:436:\tif (bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len))\ntools/bpf/bpftool/common.c-437-\t\tgoto copy_name;\n--\ntools/bpf/bpftool/common.c=551=static int do_build_table_cb(const char *fpath, const struct stat *sb,\n--\ntools/bpf/bpftool/common.c-571-\tmemset(\u0026pinned_info, 0, sizeof(pinned_info));\ntools/bpf/bpftool/common.c:572:\tif (bpf_prog_get_info_by_fd(fd, \u0026pinned_info, \u0026len))\ntools/bpf/bpftool/common.c-573-\t\tgoto out_close;\n--\ntools/bpf/bpftool/common.c=812=static int prog_fd_by_nametag(void *nametag, int **fds, bool tag)\n--\ntools/bpf/bpftool/common.c-841-\ntools/bpf/bpftool/common.c:842:\t\terr = bpf_prog_get_info_by_fd(fd, \u0026info, \u0026len);\ntools/bpf/bpftool/common.c-843-\t\tif (err) {\n--\ntools/bpf/bpftool/link.c=235=static int get_prog_info(int prog_id, struct bpf_prog_info *info)\n--\ntools/bpf/bpftool/link.c-244-\tmemset(info, 0, sizeof(*info));\ntools/bpf/bpftool/link.c:245:\terr = bpf_prog_get_info_by_fd(prog_fd, info, \u0026len);\ntools/bpf/bpftool/link.c-246-\tif (err)\n--\ntools/bpf/bpftool/prog.c=191=static void show_prog_maps(int fd, __u32 num_maps)\n--\ntools/bpf/bpftool/prog.c-201-\ntools/bpf/bpftool/prog.c:202:\terr = bpf_prog_get_info_by_fd(fd, \u0026info, \u0026len);\ntools/bpf/bpftool/prog.c-203-\tif (err || !info.nr_map_ids)\n--\ntools/bpf/bpftool/prog.c=220=static void *find_metadata(int prog_fd, struct bpf_map_info *map_info)\n--\ntools/bpf/bpftool/prog.c-234-\tprog_info_len = sizeof(prog_info);\ntools/bpf/bpftool/prog.c:235:\tret = bpf_prog_get_info_by_fd(prog_fd, \u0026prog_info, \u0026prog_info_len);\ntools/bpf/bpftool/prog.c-236-\tif (ret)\n--\ntools/bpf/bpftool/prog.c-251-\ntools/bpf/bpftool/prog.c:252:\tret = bpf_prog_get_info_by_fd(prog_fd, \u0026prog_info, \u0026prog_info_len);\ntools/bpf/bpftool/prog.c-253-\tif (ret)\n--\ntools/bpf/bpftool/prog.c=582=static int show_prog(int fd)\n--\ntools/bpf/bpftool/prog.c-587-\ntools/bpf/bpftool/prog.c:588:\terr = bpf_prog_get_info_by_fd(fd, \u0026info, \u0026len);\ntools/bpf/bpftool/prog.c-589-\tif (err \u0026\u0026 err != -ENODEV) {\n--\ntools/bpf/bpftool/prog.c=881=static int do_dump(int argc, char **argv)\n--\ntools/bpf/bpftool/prog.c-967-\ntools/bpf/bpftool/prog.c:968:\t\terr = bpf_prog_get_info_by_fd(fds[i], \u0026info, \u0026info_len);\ntools/bpf/bpftool/prog.c-969-\t\tif (err) {\n--\ntools/bpf/bpftool/prog.c-979-\ntools/bpf/bpftool/prog.c:980:\t\terr = bpf_prog_get_info_by_fd(fds[i], \u0026info, \u0026info_len);\ntools/bpf/bpftool/prog.c-981-\t\tif (err) {\n--\ntools/bpf/bpftool/prog.c=2326=static char *profile_target_name(int tgt_fd)\n--\ntools/bpf/bpftool/prog.c-2336-\ntools/bpf/bpftool/prog.c:2337:\terr = bpf_prog_get_info_by_fd(tgt_fd, \u0026info, \u0026info_len);\ntools/bpf/bpftool/prog.c-2338-\tif (err) {\n--\ntools/bpf/bpftool/prog.c-2358-\ntools/bpf/bpftool/prog.c:2359:\terr = bpf_prog_get_info_by_fd(tgt_fd, \u0026info, \u0026info_len);\ntools/bpf/bpftool/prog.c-2360-\tif (err) {\n--\ntools/lib/bpf/bpf.c=1262=int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len)\n--\ntools/lib/bpf/bpf.c-1278-\ntools/lib/bpf/bpf.c:1279:int bpf_prog_get_info_by_fd(int prog_fd, struct bpf_prog_info *info, __u32 *info_len)\ntools/lib/bpf/bpf.c-1280-{\n--\ntools/lib/bpf/bpf.h=549=LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len);\n--\ntools/lib/bpf/bpf.h-551-/**\ntools/lib/bpf/bpf.h:552: * @brief **bpf_prog_get_info_by_fd()** obtains information about the BPF\ntools/lib/bpf/bpf.h-553- * program corresponding to *prog_fd*.\n--\ntools/lib/bpf/bpf.h-568- */\ntools/lib/bpf/bpf.h:569:LIBBPF_API int bpf_prog_get_info_by_fd(int prog_fd, struct bpf_prog_info *info, __u32 *info_len);\ntools/lib/bpf/bpf.h-570-\n--\ntools/lib/bpf/libbpf.c=11091=static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd, int token_fd)\n--\ntools/lib/bpf/libbpf.c-11098-\tmemset(\u0026info, 0, info_len);\ntools/lib/bpf/libbpf.c:11099:\terr = bpf_prog_get_info_by_fd(attach_prog_fd, \u0026info, \u0026info_len);\ntools/lib/bpf/libbpf.c-11100-\tif (err) {\ntools/lib/bpf/libbpf.c:11101:\t\tpr_warn(\"failed bpf_prog_get_info_by_fd for FD %u: %s\\n\",\ntools/lib/bpf/libbpf.c-11102-\t\t\tattach_prog_fd, errstr(err));\n--\ntools/lib/bpf/libbpf.map=384=LIBBPF_1.2.0 {\n--\ntools/lib/bpf/libbpf.map-389-\t\tbpf_map_get_info_by_fd;\ntools/lib/bpf/libbpf.map:390:\t\tbpf_prog_get_info_by_fd;\ntools/lib/bpf/libbpf.map-391-} LIBBPF_1.1.0;\n--\ntools/lib/bpf/netlink.c=711=static int tc_add_fd_and_name(struct libbpf_nla_req *req, int fd)\n--\ntools/lib/bpf/netlink.c-718-\tmemset(\u0026info, 0, info_len);\ntools/lib/bpf/netlink.c:719:\tret = bpf_prog_get_info_by_fd(fd, \u0026info, \u0026info_len);\ntools/lib/bpf/netlink.c-720-\tif (ret \u003c 0)\n--\ntools/testing/selftests/bpf/jit_disasm_helpers.c=177=int get_jited_program_text(int fd, char *text, size_t text_sz)\n--\ntools/testing/selftests/bpf/jit_disasm_helpers.c-200-\t/* first call is to find out jited program len */\ntools/testing/selftests/bpf/jit_disasm_helpers.c:201:\terr = bpf_prog_get_info_by_fd(fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/jit_disasm_helpers.c:202:\tif (!ASSERT_OK(err, \"bpf_prog_get_info_by_fd #1\"))\ntools/testing/selftests/bpf/jit_disasm_helpers.c-203-\t\tgoto out;\n--\ntools/testing/selftests/bpf/jit_disasm_helpers.c-223-\tinfo.nr_jited_func_lens = jited_funcs;\ntools/testing/selftests/bpf/jit_disasm_helpers.c:224:\terr = bpf_prog_get_info_by_fd(fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/jit_disasm_helpers.c:225:\tif (!ASSERT_OK(err, \"bpf_prog_get_info_by_fd #2\"))\ntools/testing/selftests/bpf/jit_disasm_helpers.c-226-\t\tgoto out;\n--\ntools/testing/selftests/bpf/prog_tests/bpf_obj_id.c=6=void serial_test_bpf_obj_id(void)\n--\ntools/testing/selftests/bpf/prog_tests/bpf_obj_id.c-114-\ntools/testing/selftests/bpf/prog_tests/bpf_obj_id.c:115:\t\terr = bpf_prog_get_info_by_fd(prog_fds[i], \u0026prog_infos[i],\ntools/testing/selftests/bpf/prog_tests/bpf_obj_id.c-116-\t\t\t\t\t      \u0026info_len);\n--\ntools/testing/selftests/bpf/prog_tests/bpf_obj_id.c-119-\ntools/testing/selftests/bpf/prog_tests/bpf_obj_id.c:120:\t\tif (!ASSERT_OK(err, \"bpf_prog_get_info_by_fd\") ||\ntools/testing/selftests/bpf/prog_tests/bpf_obj_id.c-121-\t\t\t\t!ASSERT_EQ(prog_infos[i].type, BPF_PROG_TYPE_RAW_TRACEPOINT, \"prog_type\") ||\n--\ntools/testing/selftests/bpf/prog_tests/bpf_obj_id.c-181-\t\tprog_info.nr_map_ids = 1;\ntools/testing/selftests/bpf/prog_tests/bpf_obj_id.c:182:\t\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026prog_info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/bpf_obj_id.c:183:\t\tif (!ASSERT_ERR(err, \"bpf_prog_get_info_by_fd\") ||\ntools/testing/selftests/bpf/prog_tests/bpf_obj_id.c:184:\t\t\t\t!ASSERT_EQ(errno, EFAULT, \"bpf_prog_get_info_by_fd\"))\ntools/testing/selftests/bpf/prog_tests/bpf_obj_id.c-185-\t\t\tbreak;\n--\ntools/testing/selftests/bpf/prog_tests/bpf_obj_id.c-191-\t\tprog_info.nr_map_ids = 2;\ntools/testing/selftests/bpf/prog_tests/bpf_obj_id.c:192:\t\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026prog_info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/bpf_obj_id.c-193-\t\tprog_infos[i].jited_prog_insns = 0;\n--\ntools/testing/selftests/bpf/prog_tests/bpf_obj_id.c-196-\ntools/testing/selftests/bpf/prog_tests/bpf_obj_id.c:197:\t\tASSERT_OK(err, \"bpf_prog_get_info_by_fd\");\ntools/testing/selftests/bpf/prog_tests/bpf_obj_id.c-198-\t\tASSERT_EQ(info_len, sizeof(struct bpf_prog_info), \"prog_info_len\");\n--\ntools/testing/selftests/bpf/prog_tests/btf.c=4903=static void do_test_file(unsigned int test_num)\n--\ntools/testing/selftests/bpf/prog_tests/btf.c-4975-\tinfo_len = sizeof(struct bpf_prog_info);\ntools/testing/selftests/bpf/prog_tests/btf.c:4976:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/btf.c-4977-\n--\ntools/testing/selftests/bpf/prog_tests/btf.c-5007-\ntools/testing/selftests/bpf/prog_tests/btf.c:5008:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/btf.c-5009-\n--\ntools/testing/selftests/bpf/prog_tests/btf.c=6615=static int test_get_finfo(const struct prog_info_raw_test *test,\n--\ntools/testing/selftests/bpf/prog_tests/btf.c-6626-\tinfo_len = sizeof(struct bpf_prog_info);\ntools/testing/selftests/bpf/prog_tests/btf.c:6627:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/btf.c-6628-\tif (CHECK(err \u003c 0, \"invalid get info (1st) errno:%d\", errno)) {\n--\ntools/testing/selftests/bpf/prog_tests/btf.c-6656-\tinfo.func_info = ptr_to_u64(func_info);\ntools/testing/selftests/bpf/prog_tests/btf.c:6657:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/btf.c-6658-\tif (CHECK(err \u003c 0, \"invalid get info (2nd) errno:%d\", errno)) {\n--\ntools/testing/selftests/bpf/prog_tests/btf.c=6696=static int test_get_linfo(const struct prog_info_raw_test *test,\n--\ntools/testing/selftests/bpf/prog_tests/btf.c-6720-\tinfo_len = sizeof(struct bpf_prog_info);\ntools/testing/selftests/bpf/prog_tests/btf.c:6721:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/btf.c-6722-\tif (CHECK(err \u003c 0, \"err:%d errno:%d\", err, errno)) {\n--\ntools/testing/selftests/bpf/prog_tests/btf.c-6794-\ntools/testing/selftests/bpf/prog_tests/btf.c:6795:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/btf.c-6796-\n--\ntools/testing/selftests/bpf/prog_tests/call_rcu.c=99=static void test_call_rcu_teardown(void)\n--\ntools/testing/selftests/bpf/prog_tests/call_rcu.c-114-\tlen = sizeof(pinfo);\ntools/testing/selftests/bpf/prog_tests/call_rcu.c:115:\tif (!ASSERT_OK(bpf_prog_get_info_by_fd(bpf_program__fd(skel-\u003eprogs.arm), \u0026pinfo, \u0026len),\ntools/testing/selftests/bpf/prog_tests/call_rcu.c-116-\t\t       \"prog_info\"))\n--\ntools/testing/selftests/bpf/prog_tests/clone_attach_btf_id.c-13- * bpf_fentry_test2. Verify each loaded program's attach_btf_id via\ntools/testing/selftests/bpf/prog_tests/clone_attach_btf_id.c:14: * bpf_prog_get_info_by_fd().\ntools/testing/selftests/bpf/prog_tests/clone_attach_btf_id.c-15- */\n--\ntools/testing/selftests/bpf/prog_tests/clone_attach_btf_id.c=17=static int get_prog_attach_btf_id(int prog_fd)\n--\ntools/testing/selftests/bpf/prog_tests/clone_attach_btf_id.c-22-\ntools/testing/selftests/bpf/prog_tests/clone_attach_btf_id.c:23:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/clone_attach_btf_id.c-24-\tif (err)\n--\ntools/testing/selftests/bpf/prog_tests/enable_stats.c=5=void test_enable_stats(void)\n--\ntools/testing/selftests/bpf/prog_tests/enable_stats.c-30-\tmemset(\u0026info, 0, info_len);\ntools/testing/selftests/bpf/prog_tests/enable_stats.c:31:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/enable_stats.c-32-\tif (CHECK(err, \"get_prog_info\",\n--\ntools/testing/selftests/bpf/prog_tests/fd_array.c=79=static inline int bpf_prog_get_map_ids(int prog_fd, __u32 *nr_map_ids, __u32 *map_ids)\n--\ntools/testing/selftests/bpf/prog_tests/fd_array.c-88-\ntools/testing/selftests/bpf/prog_tests/fd_array.c:89:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026len);\ntools/testing/selftests/bpf/prog_tests/fd_array.c:90:\tif (!ASSERT_OK(err, \"bpf_prog_get_info_by_fd\"))\ntools/testing/selftests/bpf/prog_tests/fd_array.c-91-\t\treturn -1;\n--\ntools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c=57=static void test_fexit_bpf2bpf_common(const char *obj_file,\n--\ntools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c-82-\tinfo_len = sizeof(prog_info);\ntools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c:83:\terr = bpf_prog_get_info_by_fd(tgt_fd, \u0026prog_info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c-84-\tif (!ASSERT_OK(err, \"tgt_fd_get_info\"))\n--\ntools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c=460=static int find_prog_btf_id(const char *name, __u32 attach_prog_fd)\n--\ntools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c-466-\ntools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c:467:\tret = bpf_prog_get_info_by_fd(attach_prog_fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c-468-\tif (ret)\n--\ntools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c=504=static void test_fentry_to_cgroup_bpf(void)\n--\ntools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c-532-\ntools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c:533:\t/* Make sure bpf_prog_get_info_by_fd works correctly when attaching\ntools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c-534-\t * to another BPF program.\n--\ntools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c-536-\ntools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c:537:\tASSERT_OK(bpf_prog_get_info_by_fd(fentry_fd, \u0026info, \u0026info_len),\ntools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c:538:\t\t  \"bpf_prog_get_info_by_fd\");\ntools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c-539-\n--\ntools/testing/selftests/bpf/prog_tests/fill_link_info.c=424=static int setup_tmulti_targets(const struct bpf_program *prog,\n--\ntools/testing/selftests/bpf/prog_tests/fill_link_info.c-449-\tmemset(\u0026prog_info, 0, len);\ntools/testing/selftests/bpf/prog_tests/fill_link_info.c:450:\terr = bpf_prog_get_info_by_fd(bpf_program__fd(prog), \u0026prog_info, \u0026len);\ntools/testing/selftests/bpf/prog_tests/fill_link_info.c:451:\tif (!ASSERT_OK(err, \"bpf_prog_get_info_by_fd\"))\ntools/testing/selftests/bpf/prog_tests/fill_link_info.c-452-\t\tgoto error;\n--\ntools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c=57=static __u32 query_prog_id(int prog)\n--\ntools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c-62-\ntools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c:63:\terr = bpf_prog_get_info_by_fd(prog, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c-64-\tif (CHECK_FAIL(err || info_len != sizeof(info))) {\ntools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c:65:\t\tperror(\"bpf_prog_get_info_by_fd\");\ntools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c-66-\t\treturn 0;\n--\ntools/testing/selftests/bpf/prog_tests/kasan.c=165=static void exec_subtest(struct test_ctx *ctx, struct test_spec *test,\n--\ntools/testing/selftests/bpf/prog_tests/kasan.c-183-\t\treturn;\ntools/testing/selftests/bpf/prog_tests/kasan.c:184:\tret = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/kasan.c-185-\tif (!ASSERT_OK(ret, \"fetch loaded program info\"))\n--\ntools/testing/selftests/bpf/prog_tests/lirc_mode2.c=123=void test_lirc_mode2(void)\n--\ntools/testing/selftests/bpf/prog_tests/lirc_mode2.c-154-\tinfo_len = sizeof(info);\ntools/testing/selftests/bpf/prog_tests/lirc_mode2.c:155:\tret = bpf_prog_get_info_by_fd(progfd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/lirc_mode2.c-156-\tif (!ASSERT_OK(ret, \"get first program's info\"))\n--\ntools/testing/selftests/bpf/prog_tests/lirc_mode2.c-273-\tinfo_len = sizeof(info);\ntools/testing/selftests/bpf/prog_tests/lirc_mode2.c:274:\tret = bpf_prog_get_info_by_fd(progfd2, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/lirc_mode2.c-275-\tif (!ASSERT_OK(ret, \"get second program's info\"))\n--\ntools/testing/selftests/bpf/prog_tests/lsm_cgroup.c=16=static __u32 query_prog_cnt(int cgroup_fd, const char *attach_func)\n--\ntools/testing/selftests/bpf/prog_tests/lsm_cgroup.c-46-\t\tASSERT_GE(fd, 0, \"prog_get_fd_by_id\");\ntools/testing/selftests/bpf/prog_tests/lsm_cgroup.c:47:\t\tASSERT_OK(bpf_prog_get_info_by_fd(fd, \u0026info, \u0026info_len),\ntools/testing/selftests/bpf/prog_tests/lsm_cgroup.c-48-\t\t\t  \"prog_info_by_fd\");\n--\ntools/testing/selftests/bpf/prog_tests/metadata.c=16=static int prog_holds_map(int prog_fd, int map_fd)\n--\ntools/testing/selftests/bpf/prog_tests/metadata.c-32-\tprog_info_len = sizeof(prog_info);\ntools/testing/selftests/bpf/prog_tests/metadata.c:33:\tret = bpf_prog_get_info_by_fd(prog_fd, \u0026prog_info, \u0026prog_info_len);\ntools/testing/selftests/bpf/prog_tests/metadata.c-34-\tif (ret)\n--\ntools/testing/selftests/bpf/prog_tests/metadata.c-46-\ntools/testing/selftests/bpf/prog_tests/metadata.c:47:\tret = bpf_prog_get_info_by_fd(prog_fd, \u0026prog_info, \u0026prog_info_len);\ntools/testing/selftests/bpf/prog_tests/metadata.c-48-\tif (ret) {\n--\ntools/testing/selftests/bpf/prog_tests/missed.c=45=static __u64 get_missed_count(int fd)\n--\ntools/testing/selftests/bpf/prog_tests/missed.c-50-\ntools/testing/selftests/bpf/prog_tests/missed.c:51:\terr = bpf_prog_get_info_by_fd(fd, \u0026info, \u0026len);\ntools/testing/selftests/bpf/prog_tests/missed.c:52:\tif (!ASSERT_OK(err, \"bpf_prog_get_info_by_fd\"))\ntools/testing/selftests/bpf/prog_tests/missed.c-53-\t\treturn (__u64) -1;\n--\ntools/testing/selftests/bpf/prog_tests/prog_run_opts.c=9=static void check_run_cnt(int prog_fd, __u64 run_cnt)\n--\ntools/testing/selftests/bpf/prog_tests/prog_run_opts.c-14-\ntools/testing/selftests/bpf/prog_tests/prog_run_opts.c:15:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/prog_run_opts.c-16-\tif (CHECK(err, \"get_prog_info\", \"failed to get bpf_prog_info for fd %d\\n\", prog_fd))\n--\ntools/testing/selftests/bpf/prog_tests/recursion.c=6=void test_recursion(void)\n--\ntools/testing/selftests/bpf/prog_tests/recursion.c-33-\ntools/testing/selftests/bpf/prog_tests/recursion.c:34:\terr = bpf_prog_get_info_by_fd(bpf_program__fd(skel-\u003eprogs.on_delete),\ntools/testing/selftests/bpf/prog_tests/recursion.c-35-\t\t\t\t      \u0026prog_info, \u0026prog_info_len);\n--\ntools/testing/selftests/bpf/prog_tests/sockmap_basic.c=391=static __u32 query_prog_id(int prog_fd)\n--\ntools/testing/selftests/bpf/prog_tests/sockmap_basic.c-396-\ntools/testing/selftests/bpf/prog_tests/sockmap_basic.c:397:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/sockmap_basic.c:398:\tif (!ASSERT_OK(err, \"bpf_prog_get_info_by_fd\") ||\ntools/testing/selftests/bpf/prog_tests/sockmap_basic.c:399:\t    !ASSERT_EQ(info_len, sizeof(info), \"bpf_prog_get_info_by_fd\"))\ntools/testing/selftests/bpf/prog_tests/sockmap_basic.c-400-\t\treturn 0;\n--\ntools/testing/selftests/bpf/prog_tests/stacktrace_ips.c=96=static void test_stacktrace_ips_raw_tp(void)\n--\ntools/testing/selftests/bpf/prog_tests/stacktrace_ips.c-122-\tinfo.nr_jited_ksyms = 1;\ntools/testing/selftests/bpf/prog_tests/stacktrace_ips.c:123:\terr = bpf_prog_get_info_by_fd(bpf_program__fd(skel-\u003eprogs.rawtp_test),\ntools/testing/selftests/bpf/prog_tests/stacktrace_ips.c-124-\t\t\t\t      \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/stacktrace_ips.c:125:\tif (!ASSERT_OK(err, \"bpf_prog_get_info_by_fd\"))\ntools/testing/selftests/bpf/prog_tests/stacktrace_ips.c-126-\t\tgoto cleanup;\n--\ntools/testing/selftests/bpf/prog_tests/task_local_storage.c=95=static void test_recursion(void)\n--\ntools/testing/selftests/bpf/prog_tests/task_local_storage.c-136-\tmemset(\u0026info, 0, sizeof(info));\ntools/testing/selftests/bpf/prog_tests/task_local_storage.c:137:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/task_local_storage.c-138-\tASSERT_OK(err, \"get prog info\");\n--\ntools/testing/selftests/bpf/prog_tests/task_local_storage.c-142-\tmemset(\u0026info, 0, sizeof(info));\ntools/testing/selftests/bpf/prog_tests/task_local_storage.c:143:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/task_local_storage.c-144-\tASSERT_OK(err, \"get prog info\");\n--\ntools/testing/selftests/bpf/prog_tests/task_local_storage.c=178=static void test_nodeadlock(void)\n--\ntools/testing/selftests/bpf/prog_tests/task_local_storage.c-232-\tprog_fd = bpf_program__fd(skel-\u003eprogs.socket_post_create);\ntools/testing/selftests/bpf/prog_tests/task_local_storage.c:233:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/task_local_storage.c-234-\tASSERT_OK(err, \"get prog info\");\n--\ntools/testing/selftests/bpf/prog_tests/tc_bpf.c=26=static int test_tc_bpf_basic(const struct bpf_tc_hook *hook, int fd)\n--\ntools/testing/selftests/bpf/prog_tests/tc_bpf.c-32-\ntools/testing/selftests/bpf/prog_tests/tc_bpf.c:33:\tret = bpf_prog_get_info_by_fd(fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/tc_bpf.c:34:\tif (!ASSERT_OK(ret, \"bpf_prog_get_info_by_fd\"))\ntools/testing/selftests/bpf/prog_tests/tc_bpf.c-35-\t\treturn ret;\n--\ntools/testing/selftests/bpf/prog_tests/test_btf_ext.c=7=static void subtest_line_func_info(void)\n--\ntools/testing/selftests/bpf/prog_tests/test_btf_ext.c-25-\tinfo.line_info_rec_size = sizeof(*line_info);\ntools/testing/selftests/bpf/prog_tests/test_btf_ext.c:26:\terr = bpf_prog_get_info_by_fd(fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/test_btf_ext.c-27-\tif (!ASSERT_OK(err, \"prog_line_info\"))\n--\ntools/testing/selftests/bpf/prog_tests/test_btf_ext.c-36-\tinfo.func_info_rec_size = sizeof(*func_info);\ntools/testing/selftests/bpf/prog_tests/test_btf_ext.c:37:\terr = bpf_prog_get_info_by_fd(fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/test_btf_ext.c-38-\tif (!ASSERT_OK(err, \"prog_func_info\"))\n--\ntools/testing/selftests/bpf/prog_tests/test_global_funcs.c=42=static void subtest_ctx_arg_rewrite(void)\n--\ntools/testing/selftests/bpf/prog_tests/test_global_funcs.c-80-\tfd = bpf_program__fd(skel-\u003eprogs.arg_tag_ctx_perf);\ntools/testing/selftests/bpf/prog_tests/test_global_funcs.c:81:\terr = bpf_prog_get_info_by_fd(fd, \u0026info, \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/test_global_funcs.c-82-\tif (!ASSERT_OK(err, \"prog_info\"))\n--\ntools/testing/selftests/bpf/prog_tests/tp_attach_query.c=4=void serial_test_tp_attach_query(void)\n--\ntools/testing/selftests/bpf/prog_tests/tp_attach_query.c-52-\t\tinfo_len = sizeof(prog_info);\ntools/testing/selftests/bpf/prog_tests/tp_attach_query.c:53:\t\terr = bpf_prog_get_info_by_fd(prog_fd[i], \u0026prog_info,\ntools/testing/selftests/bpf/prog_tests/tp_attach_query.c-54-\t\t\t\t\t      \u0026info_len);\ntools/testing/selftests/bpf/prog_tests/tp_attach_query.c:55:\t\tif (CHECK(err, \"bpf_prog_get_info_by_fd\", \"err %d errno %d\\n\",\ntools/testing/selftests/bpf/prog_tests/tp_attach_query.c-56-\t\t\t  err, errno))\n--\ntools/testing/selftests/bpf/prog_tests/unpriv_bpf_disabled.c=202=void test_unpriv_bpf_disabled(void)\n--\ntools/testing/selftests/bpf/prog_tests/unpriv_bpf_disabled.c-257-\tprog_fd = bpf_program__fd(skel-\u003eprogs.sys_nanosleep_enter);\ntools/testing/selftests/bpf/prog_tests/unpriv_bpf_disabled.c:258:\tASSERT_OK(bpf_prog_get_info_by_fd(prog_fd, \u0026prog_info, \u0026prog_info_len),\ntools/testing/selftests/bpf/prog_tests/unpriv_bpf_disabled.c-259-\t\t  \"obj_get_info_by_fd\");\n--\ntools/testing/selftests/bpf/prog_tests/verif_stats.c=8=void test_verif_stats(void)\n--\ntools/testing/selftests/bpf/prog_tests/verif_stats.c-18-\ntools/testing/selftests/bpf/prog_tests/verif_stats.c:19:\terr = bpf_prog_get_info_by_fd(skel-\u003eprogs.sys_enter.prog_fd,\ntools/testing/selftests/bpf/prog_tests/verif_stats.c-20-\t\t\t\t      \u0026info, \u0026len);\ntools/testing/selftests/bpf/prog_tests/verif_stats.c:21:\tif (!ASSERT_OK(err, \"bpf_prog_get_info_by_fd\"))\ntools/testing/selftests/bpf/prog_tests/verif_stats.c-22-\t\tgoto cleanup;\n--\ntools/testing/selftests/bpf/prog_tests/xdp_attach.c=8=static void test_xdp_attach(const char *file)\n--\ntools/testing/selftests/bpf/prog_tests/xdp_attach.c-20-\t\treturn;\ntools/testing/selftests/bpf/prog_tests/xdp_attach.c:21:\terr = bpf_prog_get_info_by_fd(fd1, \u0026info, \u0026len);\ntools/testing/selftests/bpf/prog_tests/xdp_attach.c-22-\tif (CHECK_FAIL(err))\n--\ntools/testing/selftests/bpf/prog_tests/xdp_attach.c-30-\tmemset(\u0026info, 0, sizeof(info));\ntools/testing/selftests/bpf/prog_tests/xdp_attach.c:31:\terr = bpf_prog_get_info_by_fd(fd2, \u0026info, \u0026len);\ntools/testing/selftests/bpf/prog_tests/xdp_attach.c-32-\tif (CHECK_FAIL(err))\n--\ntools/testing/selftests/bpf/prog_tests/xdp_cpumap_attach.c=13=static void test_xdp_with_cpumap_helpers(void)\n--\ntools/testing/selftests/bpf/prog_tests/xdp_cpumap_attach.c-41-\tmap_fd = bpf_map__fd(skel-\u003emaps.cpu_map);\ntools/testing/selftests/bpf/prog_tests/xdp_cpumap_attach.c:42:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026len);\ntools/testing/selftests/bpf/prog_tests/xdp_cpumap_attach.c:43:\tif (!ASSERT_OK(err, \"bpf_prog_get_info_by_fd\"))\ntools/testing/selftests/bpf/prog_tests/xdp_cpumap_attach.c-44-\t\tgoto out_close;\n--\ntools/testing/selftests/bpf/prog_tests/xdp_cpumap_attach.c=114=static void test_xdp_with_cpumap_frags_helpers(void)\n--\ntools/testing/selftests/bpf/prog_tests/xdp_cpumap_attach.c-130-\tmap_fd = bpf_map__fd(skel-\u003emaps.cpu_map);\ntools/testing/selftests/bpf/prog_tests/xdp_cpumap_attach.c:131:\terr = bpf_prog_get_info_by_fd(frags_prog_fd, \u0026info, \u0026len);\ntools/testing/selftests/bpf/prog_tests/xdp_cpumap_attach.c:132:\tif (!ASSERT_OK(err, \"bpf_prog_get_info_by_fd\"))\ntools/testing/selftests/bpf/prog_tests/xdp_cpumap_attach.c-133-\t\tgoto out_close;\n--\ntools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c=17=static void test_xdp_with_devmap_helpers(void)\n--\ntools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c-46-\tmap_fd = bpf_map__fd(skel-\u003emaps.dm_ports);\ntools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c:47:\terr = bpf_prog_get_info_by_fd(dm_fd, \u0026info, \u0026len);\ntools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c:48:\tif (!ASSERT_OK(err, \"bpf_prog_get_info_by_fd\"))\ntools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c-49-\t\tgoto out_close;\n--\ntools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c=134=static void test_xdp_with_devmap_frags_helpers(void)\n--\ntools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c-155-\tmap_fd = bpf_map__fd(skel-\u003emaps.dm_ports);\ntools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c:156:\terr = bpf_prog_get_info_by_fd(dm_fd_frags, \u0026info, \u0026len);\ntools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c:157:\tif (!ASSERT_OK(err, \"bpf_prog_get_info_by_fd\"))\ntools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c-158-\t\tgoto out_close;\n--\ntools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c=182=static void test_xdp_with_devmap_helpers_veth(void)\n--\ntools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c-218-\tmap_fd = bpf_map__fd(skel-\u003emaps.dm_ports);\ntools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c:219:\terr = bpf_prog_get_info_by_fd(dm_fd, \u0026info, \u0026len);\ntools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c:220:\tif (!ASSERT_OK(err, \"bpf_prog_get_info_by_fd\"))\ntools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c-221-\t\tgoto out_close;\n--\ntools/testing/selftests/bpf/prog_tests/xdp_info.c=7=void serial_test_xdp_info(void)\n--\ntools/testing/selftests/bpf/prog_tests/xdp_info.c-36-\ntools/testing/selftests/bpf/prog_tests/xdp_info.c:37:\terr = bpf_prog_get_info_by_fd(prog_fd, \u0026info, \u0026len);\ntools/testing/selftests/bpf/prog_tests/xdp_info.c-38-\tif (CHECK(err, \"get_prog_info\", \"errno=%d\\n\", errno))\n--\ntools/testing/selftests/bpf/prog_tests/xdp_link.c=9=void serial_test_xdp_link(void)\n--\ntools/testing/selftests/bpf/prog_tests/xdp_link.c-31-\tmemset(\u0026prog_info, 0, sizeof(prog_info));\ntools/testing/selftests/bpf/prog_tests/xdp_link.c:32:\terr = bpf_prog_get_info_by_fd(prog_fd1, \u0026prog_info, \u0026prog_info_len);\ntools/testing/selftests/bpf/prog_tests/xdp_link.c-33-\tif (!ASSERT_OK(err, \"fd_info1\"))\n--\ntools/testing/selftests/bpf/prog_tests/xdp_link.c-37-\tmemset(\u0026prog_info, 0, sizeof(prog_info));\ntools/testing/selftests/bpf/prog_tests/xdp_link.c:38:\terr = bpf_prog_get_info_by_fd(prog_fd2, \u0026prog_info, \u0026prog_info_len);\ntools/testing/selftests/bpf/prog_tests/xdp_link.c-39-\tif (!ASSERT_OK(err, \"fd_info2\"))\n--\ntools/testing/selftests/bpf/testing_helpers.c=460=int get_xlated_program(int fd_prog, struct bpf_insn **buf, __u32 *cnt)\n--\ntools/testing/selftests/bpf/testing_helpers.c-466-\ntools/testing/selftests/bpf/testing_helpers.c:467:\tif (bpf_prog_get_info_by_fd(fd_prog, \u0026info, \u0026info_len)) {\ntools/testing/selftests/bpf/testing_helpers.c:468:\t\tperror(\"bpf_prog_get_info_by_fd failed\");\ntools/testing/selftests/bpf/testing_helpers.c-469-\t\treturn -1;\n--\ntools/testing/selftests/bpf/testing_helpers.c-488-\tinfo.xlated_prog_insns = (__u64)(unsigned long)*buf;\ntools/testing/selftests/bpf/testing_helpers.c:489:\tif (bpf_prog_get_info_by_fd(fd_prog, \u0026info, \u0026info_len)) {\ntools/testing/selftests/bpf/testing_helpers.c:490:\t\tperror(\"second bpf_prog_get_info_by_fd failed\");\ntools/testing/selftests/bpf/testing_helpers.c-491-\t\tgoto out_free_buf;\n--\ntools/testing/selftests/bpf/veristat.c=1676=static int process_prog(const char *filename, struct bpf_object *obj, struct bpf_program *prog)\n--\ntools/testing/selftests/bpf/veristat.c-1755-\tmemset(\u0026info, 0, info_len);\ntools/testing/selftests/bpf/veristat.c:1756:\tif (fd \u003e 0 \u0026\u0026 bpf_prog_get_info_by_fd(fd, \u0026info, \u0026info_len) == 0) {\n\n"
}

23/3 2026/09/25 10:07 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 94231
							output: 37
							thoughts: 39
						

Thoughts:
Here's a summary of my thought process:

**Investigating BPF Program Info Cleanup**

Okay, so I need to dig into how BPF program information is handled, specifically concerning cleanup. My initial thought is to go straight to the source, so I'm focusing on the `kernel/bpf/syscall.c` file. I'm guessing that line 5081 might be a good starting point, as it's likely involved with the system call handling related to BPF programs. I'm particularly interested in seeing what's recorded, and if there's any mention of a structure called `cleanup_info` within `bpf_prog_info`. I'm hoping to understand if and where cleanup-related data is stored or accessed in this context. It's a key piece of information when dealing with resource management and potential leaks after BPF program execution. This information can help me, for example, determine if there is a possibility of a memory leak if cleanup of BPF programs isn't handled correctly.



24/3 2026/09/25 10:07 tool read-file
0m Args:
{
  "File": "kernel/bpf/syscall.c",
  "FirstLine": 5080,
  "LineCount": 40
}

Results:
{
  "Contents": "5080:\t\n5081:\tstatic int set_info_rec_size(struct bpf_prog_info *info)\n5082:\t{\n5083:\t\t/*\n5084:\t\t * Ensure info.*_rec_size is the same as kernel expected size\n5085:\t\t *\n5086:\t\t * or\n5087:\t\t *\n5088:\t\t * Only allow zero *_rec_size if both _rec_size and _cnt are\n5089:\t\t * zero.  In this case, the kernel will set the expected\n5090:\t\t * _rec_size back to the info.\n5091:\t\t */\n5092:\t\n5093:\t\tif ((info-\u003enr_func_info || info-\u003efunc_info_rec_size) \u0026\u0026\n5094:\t\t    info-\u003efunc_info_rec_size != sizeof(struct bpf_func_info))\n5095:\t\t\treturn -EINVAL;\n5096:\t\n5097:\t\tif ((info-\u003enr_line_info || info-\u003eline_info_rec_size) \u0026\u0026\n5098:\t\t    info-\u003eline_info_rec_size != sizeof(struct bpf_line_info))\n5099:\t\t\treturn -EINVAL;\n5100:\t\n5101:\t\tif ((info-\u003enr_jited_line_info || info-\u003ejited_line_info_rec_size) \u0026\u0026\n5102:\t\t    info-\u003ejited_line_info_rec_size != sizeof(__u64))\n5103:\t\t\treturn -EINVAL;\n5104:\t\n5105:\t\tinfo-\u003efunc_info_rec_size = sizeof(struct bpf_func_info);\n5106:\t\tinfo-\u003eline_info_rec_size = sizeof(struct bpf_line_info);\n5107:\t\tinfo-\u003ejited_line_info_rec_size = sizeof(__u64);\n5108:\t\n5109:\t\treturn 0;\n5110:\t}\n5111:\t\n5112:\tstatic int bpf_prog_get_info_by_fd(struct file *file,\n5113:\t\t\t\t\t   struct bpf_prog *prog,\n5114:\t\t\t\t\t   const union bpf_attr *attr,\n5115:\t\t\t\t\t   union bpf_attr __user *uattr)\n5116:\t{\n5117:\t\tstruct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr-\u003einfo.info);\n5118:\t\tstruct btf *attach_btf = bpf_prog_get_target_btf(prog);\n5119:\t\tstruct bpf_prog_info info;\n"
}

25/3 2026/09/25 10:07 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 95225
							output: 22
							thoughts: 1325
						

Thoughts:
**Analyzing the BPF Cleanup Info Implementation**

Okay, so this patch *doesn't* introduce `cleanup_info` directly into the `struct bpf_prog_info`. It's only within the `union bpf_attr` structure for `BPF_PROG_LOAD`. I need to confirm this is the case, and I'll do this by double-checking the definition of `BPF_PROG_LOAD_LAST_FIELD`. Ah, yes, in `kernel/bpf/syscall.c`, it confirms the change: `BPF_PROG_LOAD_LAST_FIELD` is now `cleanup_info_cnt`, replacing the previous `keyring_id`. Looking at the `union bpf_attr` definition, I see `cleanup_info`, `cleanup_info_rec_size`, and `cleanup_info_cnt`, all present now. So, `bpf_prog_load` uses `bpf_check_uarg_tail_zero` to ensure that fields beyond `cleanup_info_cnt` in the attribute structure are zeroed, which makes sense for safety.

Now, let's dive into what `check_cleanup_info` itself does. The code allocates `krecord` via `kvcalloc`, which means it's zeroed by default, which is good. Then, it copies data from a user-provided pointer (`urecord`) into `rec` using `copy_from_bpfptr`. If the user provides a record size (`urec_size`) larger than the kernel's internal struct size (`krec_size`), and there's non-zero data in the tail, then a potentially malicious situation arises. Therefore, the minimum size is written back to user space via `copy_to_bpfptr_offset`. This overwrites user space with the expected minimal size.

Now I need to consider potential security implications, especially about how the verifier will handle uninitialized data. That's a critical area in BPF, since the verifier's primary role is to track register and stack initialization. This patch adds information about cleanup, and changes `BPF_PROG_LOAD_LAST_FIELD`. Does this *change* how the verifier checks or marks uninitialized registers or stack slots?

I need to focus on what happens during function entry, let's start with `unwind_enter_pad(struct bpf_verifier_env *env)`. What is going on in this function that may or may not change or introduce behaviour? I want to see what is changed in this function to assess any risks. I need to see what `clear_caller_saved_regs` does. I will check the kernel sources...



26/3 2026/09/25 10:07 tool grepper
0m Args:
{
  "Expression": "void clear_caller_saved_regs"
}

Results:
{
  "Output": "kernel/bpf/verifier.c=10269=static int ref_convert_alloc_rcu_protected(struct bpf_verifier_env *env, u32 id)\n--\nkernel/bpf/verifier.c-10294-\nkernel/bpf/verifier.c:10295:static void clear_caller_saved_regs(struct bpf_verifier_env *env,\nkernel/bpf/verifier.c-10296-\t\t\t\t    struct bpf_reg_state *regs)\n"
}

27/3 2026/09/25 10:07 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 97285
							output: 38
							thoughts: 13
						

Thoughts:
Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person:

**Analyzing the Function: `clear_caller_saved_regs`**

Alright, let's dive into this `clear_caller_saved_regs` routine. My immediate thought is to understand its purpose within the broader context of the system. Given its name, it's clear it's involved with managing caller-saved registers, but the *why* is the key question. What architecture are we dealing with? Is this part of a context switch, an interrupt handler, a system call implementation, or something else entirely? The specific register set being cleared will provide critical clues. I'm already anticipating questions about potential side effects and performance implications. Are we optimizing for speed here, or security by zeroing out the registers? Are we ensuring register state isolation, perhaps in a multi-threaded or privileged execution environment? I'll need to look closely at the surrounding code to grasp its true role and the trade-offs it embodies.



28/3 2026/09/25 10:07 tool read-file
0m Args:
{
  "File": "kernel/bpf/verifier.c",
  "FirstLine": 10294,
  "LineCount": 20
}

Results:
{
  "Contents": "10294:\t\n10295:\tstatic void clear_caller_saved_regs(struct bpf_verifier_env *env,\n10296:\t\t\t\t\t    struct bpf_reg_state *regs)\n10297:\t{\n10298:\t\tint i;\n10299:\t\n10300:\t\tbpf_diag_record_caller_saved(env, regs);\n10301:\t\n10302:\t\t/* after the call registers r0 - r5 were scratched */\n10303:\t\tfor (i = 0; i \u003c CALLER_SAVED_REGS; i++) {\n10304:\t\t\tbpf_mark_reg_not_init(env, \u0026regs[caller_saved[i]]);\n10305:\t\t\t__check_reg_arg(env, regs, caller_saved[i], DST_OP_NO_MARK);\n10306:\t\t}\n10307:\t}\n10308:\t\n10309:\tstatic void invalidate_outgoing_stack_args(struct bpf_verifier_env *env,\n10310:\t\t\t\t\t\t   struct bpf_func_state *state)\n10311:\t{\n10312:\t\tint i, nslots = state-\u003eout_stack_arg_cnt;\n10313:\t\n"
}

29/3 2026/09/25 10:07 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 97909
							output: 33
							thoughts: 2908
						

Thoughts:
**Analysis of BPF Cleanup Path for KMSAN Vulnerabilities**

Okay, so I've been dissecting this BPF cleanup path, specifically focusing on potential uninitialized memory exposures that KMSAN might miss. My goal is to determine if this code requires KMSAN annotations or instrumentation.

The core of the issue revolves around how the kernel handles exceptions and the restoration of callee-saved registers during a cleanup operation. The initial `clear_caller_saved_regs` clears registers, and then r0 is explicitly marked as unknown. The `arch_bpf_run_cleanup_pad` function is critical. The JIT code restores callee-saved registers (r6-r9) from a `spill_base` address on the stack. My concern is whether that `spill_base` might point to uninitialized memory.

After stepping through the code and inspecting the JIT assembly, it seems the prologue *does* explicitly spill those registers to a dedicated stack location (`throw_spill_off`) before a `bpf_throw()` call, as indicated in `bpf_exc_force_spill`. So that is promising. The `bpf_run_cleanup_pad` reads from `spill_base` which, if the program is written to throw, is filled. In the normal case, r0 is initialized. R1-R5 are scratch/dead.

Now, let's go back and check for potential KMSAN vulnerabilities, systematically going through the four criteria that apply here:

1.  **Kernel structures sent to user space:** I carefully examined all code paths involving `copy_to_user`, `put_user`, and similar functions in the diff. The only copy to user space seems to be related to checking BTF data, specifically `bpf_check_uarg_tail_zero`. The data copied to userspace is `min_size` which is initialized. **No KMSAN here.**

2.  **Conditional logic based on uninitialized variables:** I scrutinized all the newly introduced or modified structures, such as `bpf_exception_info`, and `bpf_verifier_state`. `bpf_exception_info` is zero-allocated, which means its members are initialized. `bpf_verifier_state` and related structures also appear to be properly initialized, either via `kzalloc` or explicit copy. All seems safe.

3.  **Allocation of complex data structures with potential uninitialized fields:** I checked how `bpf_exception_info` and `bpf_verifier_state` are allocated. `kzalloc` is used. This prevents uninitialized memory.

4.  **Bounds checks exposing uninitialized bytes:** No evidence found.

I'm confident the cleanup path, while complex, doesn't inherently introduce vulnerabilities that KMSAN would miss. The explicit spilling of registers before a throw, the zero-initialization of key structures, and the careful handling of memory copies all mitigate the risk of exposing uninitialized memory. **NeedsKMSAN=false**.



30/3 2026/09/25 10:07 tool grepper
0m Args:
{
  "Expression": "insn_aux_data = ",
  "PathPrefix": "kernel/bpf/"
}

Results:
{
  "Output": "kernel/bpf/fixups.c=350=struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,\n--\nkernel/bpf/fixups.c-364-\nkernel/bpf/fixups.c:365:\t\tenv-\u003einsn_aux_data = new_data;\nkernel/bpf/fixups.c-366-\t}\n"
}

31/3 2026/09/25 10:07 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 101609
							output: 34
							thoughts: 22
						

Thoughts:
Here's a summary of my thought process, focusing on the initial allocation of `insn_aux_data` within `verifier.c`:

**Initial Examination of `insn_aux_data` Allocation**

Okay, so I need to track down the genesis of `insn_aux_data`. Specifically, where does this thing get its start in `verifier.c`? That's the key to understanding its lifecycle and how it interacts with the rest of the verification process. I'm going to start by tracing the allocation. Let's dig into that file and see how this data structure is first brought into existence. This is a critical first step.



32/3 2026/09/25 10:07 tool grepper
0m Args:
{
  "Expression": "insn_aux_data",
  "PathPrefix": "kernel/bpf/verifier.c"
}

Results:
{
  "Output": "kernel/bpf/verifier.c=223=static void scalar_min_max_add(struct bpf_reg_state *dst_reg, struct bpf_reg_state *src_reg);\nkernel/bpf/verifier.c-224-\nkernel/bpf/verifier.c:225:static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux,\nkernel/bpf/verifier.c-226-\t\t\t      struct bpf_map *map,\n--\nkernel/bpf/verifier.c-234-\nkernel/bpf/verifier.c:235:static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state)\nkernel/bpf/verifier.c-236-{\n--\nkernel/bpf/verifier.c=3298=static void mark_indirect_target(struct bpf_verifier_env *env, int idx)\nkernel/bpf/verifier.c-3299-{\nkernel/bpf/verifier.c:3300:\tenv-\u003einsn_aux_data[idx].indirect_target = true;\nkernel/bpf/verifier.c-3301-}\n--\nkernel/bpf/verifier.c=3303=static void mark_non_stack_access(struct bpf_verifier_env *env, int idx)\nkernel/bpf/verifier.c-3304-{\nkernel/bpf/verifier.c:3305:\tenv-\u003einsn_aux_data[idx].non_stack_access = true;\nkernel/bpf/verifier.c-3306-}\n--\nkernel/bpf/verifier.c=3555=static void check_fastcall_stack_contract(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-3558-\tstruct bpf_subprog_info *subprog = \u0026env-\u003esubprog_info[state-\u003esubprogno];\nkernel/bpf/verifier.c:3559:\tstruct bpf_insn_aux_data *aux = env-\u003einsn_aux_data;\nkernel/bpf/verifier.c-3560-\tint i;\n--\nkernel/bpf/verifier.c=3594=static int check_stack_write_fixed_off(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-3643-\t\tif (sanitize)\nkernel/bpf/verifier.c:3644:\t\t\tenv-\u003einsn_aux_data[insn_idx].nospec_result = true;\nkernel/bpf/verifier.c-3645-\t}\n--\nkernel/bpf/verifier.c=4973=static int __check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,\n--\nkernel/bpf/verifier.c-4992-\t\t} else {\nkernel/bpf/verifier.c:4993:\t\t\tenv-\u003einsn_aux_data[insn_idx].ctx_field_size = info-\u003ectx_field_size;\nkernel/bpf/verifier.c-4994-\t\t}\n--\nkernel/bpf/verifier.c=5055=static int check_sock_access(struct bpf_verifier_env *env, int insn_idx,\n--\nkernel/bpf/verifier.c-5085-\tif (valid) {\nkernel/bpf/verifier.c:5086:\t\tenv-\u003einsn_aux_data[insn_idx].ctx_field_size =\nkernel/bpf/verifier.c-5087-\t\t\tinfo.ctx_field_size;\n--\nkernel/bpf/verifier.c=8299=static int get_bpf_res_spin_lock_kfunc_flags(const struct bpf_call_arg_meta *meta);\nkernel/bpf/verifier.c:8300:static struct bpf_insn_aux_data *cur_aux(const struct bpf_verifier_env *env);\nkernel/bpf/verifier.c-8301-static int process_irq_flag(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c=10792=static int set_map_elem_callback_state(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-10796-{\nkernel/bpf/verifier.c:10797:\tstruct bpf_insn_aux_data *insn_aux = \u0026env-\u003einsn_aux_data[insn_idx];\nkernel/bpf/verifier.c-10798-\tstruct bpf_map *map;\n--\nkernel/bpf/verifier.c=11269=record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,\n--\nkernel/bpf/verifier.c-11271-{\nkernel/bpf/verifier.c:11272:\tstruct bpf_insn_aux_data *aux = \u0026env-\u003einsn_aux_data[insn_idx];\nkernel/bpf/verifier.c-11273-\tstruct bpf_map *map = meta-\u003emap.ptr;\n--\nkernel/bpf/verifier.c=11315=record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,\n--\nkernel/bpf/verifier.c-11317-{\nkernel/bpf/verifier.c:11318:\tstruct bpf_insn_aux_data *aux = \u0026env-\u003einsn_aux_data[insn_idx];\nkernel/bpf/verifier.c-11319-\tstruct bpf_reg_state *reg;\n--\nkernel/bpf/verifier.c=11459=static int check_get_func_ip(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-11479-\nkernel/bpf/verifier.c:11480:static struct bpf_insn_aux_data *cur_aux(const struct bpf_verifier_env *env)\nkernel/bpf/verifier.c-11481-{\nkernel/bpf/verifier.c:11482:\treturn \u0026env-\u003einsn_aux_data[env-\u003einsn_idx];\nkernel/bpf/verifier.c-11483-}\n--\nkernel/bpf/verifier.c=11598=static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-11678-\tif (!in_sleepable_context(env))\nkernel/bpf/verifier.c:11679:\t\tenv-\u003einsn_aux_data[insn_idx].non_sleepable = true;\nkernel/bpf/verifier.c-11680-\n--\nkernel/bpf/verifier.c-11842-\t\t\treturns_cpu_specific_alloc_ptr = true;\nkernel/bpf/verifier.c:11843:\t\t\tenv-\u003einsn_aux_data[insn_idx].call_with_percpu_alloc_ptr = true;\nkernel/bpf/verifier.c-11844-\t\t}\n--\nkernel/bpf/verifier.c=13837=s64 bpf_helper_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-13839-{\nkernel/bpf/verifier.c:13840:\tstruct bpf_insn_aux_data *aux = \u0026env-\u003einsn_aux_data[insn_idx];\nkernel/bpf/verifier.c-13841-\tconst struct bpf_func_proto *fn;\n--\nkernel/bpf/verifier.c=13963=s64 bpf_kfunc_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-13965-{\nkernel/bpf/verifier.c:13966:\tstruct bpf_insn_aux_data *aux = \u0026env-\u003einsn_aux_data[insn_idx];\nkernel/bpf/verifier.c-13967-\tstruct bpf_call_arg_meta meta;\n--\nkernel/bpf/verifier.c=14039=static int check_special_kfunc(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,\nkernel/bpf/verifier.c:14040:\t\t\t       struct bpf_reg_state *regs, struct bpf_insn_aux_data *insn_aux,\nkernel/bpf/verifier.c-14041-\t\t\t       const struct btf_type *ptr_type, struct btf *desc_btf)\n--\nkernel/bpf/verifier.c=14210=static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-14218-\tstruct bpf_call_arg_meta meta;\nkernel/bpf/verifier.c:14219:\tstruct bpf_insn_aux_data *insn_aux;\nkernel/bpf/verifier.c-14220-\tconst char *operation;\n--\nkernel/bpf/verifier.c-14242-\tfunc_name = meta.func_name;\nkernel/bpf/verifier.c:14243:\tinsn_aux = \u0026env-\u003einsn_aux_data[insn_idx];\nkernel/bpf/verifier.c-14244-\n--\nkernel/bpf/verifier.c=14784=static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-14791-\nkernel/bpf/verifier.c:14792:static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux,\nkernel/bpf/verifier.c-14793-\t\t\t\t       u32 alu_state, u32 alu_limit)\n--\nkernel/bpf/verifier.c=14809=static int sanitize_val_alu(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-14811-{\nkernel/bpf/verifier.c:14812:\tstruct bpf_insn_aux_data *aux = cur_aux(env);\nkernel/bpf/verifier.c-14813-\n--\nkernel/bpf/verifier.c=14825=struct bpf_sanitize_info {\nkernel/bpf/verifier.c:14826:\tstruct bpf_insn_aux_data aux;\nkernel/bpf/verifier.c-14827-\tbool mask_to_left;\n--\nkernel/bpf/verifier.c=14850=static int sanitize_ptr_alu(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-14857-{\nkernel/bpf/verifier.c:14858:\tstruct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : \u0026info-\u003eaux;\nkernel/bpf/verifier.c-14859-\tstruct bpf_verifier_state *vstate = env-\u003ecur_state;\n--\nkernel/bpf/verifier.c=14946=static void sanitize_mark_insn_seen(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-14955-\tif (!vstate-\u003especulative)\nkernel/bpf/verifier.c:14956:\t\tenv-\u003einsn_aux_data[env-\u003einsn_idx].seen = env-\u003epass_cnt;\nkernel/bpf/verifier.c-14957-}\n--\nkernel/bpf/verifier.c=16222=static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-16240-\tif (dst_reg-\u003etype == PTR_TO_ARENA || (src_reg \u0026\u0026 src_reg-\u003etype == PTR_TO_ARENA)) {\nkernel/bpf/verifier.c:16241:\t\tstruct bpf_insn_aux_data *aux = cur_aux(env);\nkernel/bpf/verifier.c-16242-\n--\nkernel/bpf/verifier.c=17331=static void collect_linked_regs(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-17335-{\nkernel/bpf/verifier.c:17336:\tstruct bpf_insn_aux_data *aux = env-\u003einsn_aux_data;\nkernel/bpf/verifier.c-17337-\tstruct bpf_func_state *func;\n--\nkernel/bpf/verifier.c=17674=static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)\nkernel/bpf/verifier.c-17675-{\nkernel/bpf/verifier.c:17676:\tstruct bpf_insn_aux_data *aux = cur_aux(env);\nkernel/bpf/verifier.c-17677-\tstruct bpf_reg_state *regs = cur_regs(env);\n--\nkernel/bpf/verifier.c=18202=bool bpf_get_call_summary(struct bpf_verifier_env *env, struct bpf_insn *call,\n--\nkernel/bpf/verifier.c-18289- * - look for such patterns;\nkernel/bpf/verifier.c:18290: * - mark spill and fill instructions in env-\u003einsn_aux_data[*].fastcall_pattern;\nkernel/bpf/verifier.c:18291: * - mark set env-\u003einsn_aux_data[*].fastcall_spills_num for call instruction;\nkernel/bpf/verifier.c-18292- * - update env-\u003esubprog_info[*]-\u003efastcall_stack_off to find an offset\n--\nkernel/bpf/verifier.c=18320=static void mark_fastcall_pattern_for_call(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-18381-\t\texpected_regs_mask \u0026= ~BIT(stx-\u003esrc_reg);\nkernel/bpf/verifier.c:18382:\t\tenv-\u003einsn_aux_data[insn_idx - i].fastcall_pattern = 1;\nkernel/bpf/verifier.c:18383:\t\tenv-\u003einsn_aux_data[insn_idx + i].fastcall_pattern = 1;\nkernel/bpf/verifier.c-18384-\t}\n--\nkernel/bpf/verifier.c-18404-\tif (cs.fastcall)\nkernel/bpf/verifier.c:18405:\t\tenv-\u003einsn_aux_data[insn_idx].fastcall_spills_num = i - 1;\nkernel/bpf/verifier.c-18406-\telse\n--\nkernel/bpf/verifier.c=18571=static int save_aux_ptr_type(struct bpf_verifier_env *env, enum bpf_reg_type type,\n--\nkernel/bpf/verifier.c-18573-{\nkernel/bpf/verifier.c:18574:\tenum bpf_reg_type *prev_type = \u0026env-\u003einsn_aux_data[env-\u003einsn_idx].ptr_type;\nkernel/bpf/verifier.c-18575-\n--\nkernel/bpf/verifier.c=18936=static int do_check(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-18946-\t\tstruct bpf_insn *insn;\nkernel/bpf/verifier.c:18947:\t\tstruct bpf_insn_aux_data *insn_aux;\nkernel/bpf/verifier.c-18948-\t\tint err;\n--\nkernel/bpf/verifier.c-18963-\t\tinsn = \u0026insns[env-\u003einsn_idx];\nkernel/bpf/verifier.c:18964:\t\tinsn_aux = \u0026env-\u003einsn_aux_data[env-\u003einsn_idx];\nkernel/bpf/verifier.c-18965-\n--\nkernel/bpf/verifier.c=19234=static int __check_pseudo_btf_id(struct bpf_verifier_env *env,\nkernel/bpf/verifier.c-19235-\t\t\t\t struct bpf_insn *insn,\nkernel/bpf/verifier.c:19236:\t\t\t\t struct bpf_insn_aux_data *aux,\nkernel/bpf/verifier.c-19237-\t\t\t\t struct btf *btf)\n--\nkernel/bpf/verifier.c=19317=static int check_pseudo_btf_id(struct bpf_verifier_env *env,\nkernel/bpf/verifier.c-19318-\t\t\t       struct bpf_insn *insn,\nkernel/bpf/verifier.c:19319:\t\t\t       struct bpf_insn_aux_data *aux)\nkernel/bpf/verifier.c-19320-{\n--\nkernel/bpf/verifier.c=19782=static int check_and_resolve_insns(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-19802-\t\tif (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {\nkernel/bpf/verifier.c:19803:\t\t\tstruct bpf_insn_aux_data *aux;\nkernel/bpf/verifier.c-19804-\t\t\tstruct bpf_map *map;\n--\nkernel/bpf/verifier.c-19824-\t\t\tif (insn[0].src_reg == BPF_PSEUDO_BTF_ID) {\nkernel/bpf/verifier.c:19825:\t\t\t\taux = \u0026env-\u003einsn_aux_data[i];\nkernel/bpf/verifier.c-19826-\t\t\t\terr = check_pseudo_btf_id(env, insn, aux);\n--\nkernel/bpf/verifier.c-19832-\t\t\tif (insn[0].src_reg == BPF_PSEUDO_FUNC) {\nkernel/bpf/verifier.c:19833:\t\t\t\taux = \u0026env-\u003einsn_aux_data[i];\nkernel/bpf/verifier.c-19834-\t\t\t\taux-\u003eptr_type = PTR_TO_FUNC;\n--\nkernel/bpf/verifier.c-19872-\nkernel/bpf/verifier.c:19873:\t\t\taux = \u0026env-\u003einsn_aux_data[i];\nkernel/bpf/verifier.c-19874-\t\t\taux-\u003emap_index = map_idx;\n--\nkernel/bpf/verifier.c=19973=static void sanitize_dead_code(struct bpf_verifier_env *env)\nkernel/bpf/verifier.c-19974-{\nkernel/bpf/verifier.c:19975:\tstruct bpf_insn_aux_data *aux_data = env-\u003einsn_aux_data;\nkernel/bpf/verifier.c-19976-\tstruct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1);\n--\nkernel/bpf/verifier.c=21348=static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc *desc, int insn_idx)\n--\nkernel/bpf/verifier.c-21383-\t} else if (func_id == special_kfunc_list[KF_bpf_dynptr_from_file]) {\nkernel/bpf/verifier.c:21384:\t\tif (!env-\u003einsn_aux_data[insn_idx].non_sleepable)\nkernel/bpf/verifier.c-21385-\t\t\taddr = (unsigned long)bpf_dynptr_from_file_sleepable;\nkernel/bpf/verifier.c-21386-\t} else if (func_id == special_kfunc_list[KF_bpf_arena_alloc_pages]) {\nkernel/bpf/verifier.c:21387:\t\tif (env-\u003einsn_aux_data[insn_idx].non_sleepable)\nkernel/bpf/verifier.c-21388-\t\t\taddr = (unsigned long)bpf_arena_alloc_pages_non_sleepable;\nkernel/bpf/verifier.c-21389-\t} else if (func_id == special_kfunc_list[KF_bpf_arena_free_pages]) {\nkernel/bpf/verifier.c:21390:\t\tif (env-\u003einsn_aux_data[insn_idx].non_sleepable)\nkernel/bpf/verifier.c-21391-\t\t\taddr = (unsigned long)bpf_arena_free_pages_non_sleepable;\n--\nkernel/bpf/verifier.c-21396-\nkernel/bpf/verifier.c:21397:static void __fixup_collection_insert_kfunc(struct bpf_insn_aux_data *insn_aux,\nkernel/bpf/verifier.c-21398-\t\t\t\t\t    u16 struct_meta_reg,\n--\nkernel/bpf/verifier.c=21414=int bpf_fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-21445-\tif (is_bpf_obj_new_kfunc(desc-\u003efunc_id) || is_bpf_percpu_obj_new_kfunc(desc-\u003efunc_id)) {\nkernel/bpf/verifier.c:21446:\t\tstruct btf_struct_meta *kptr_struct_meta = env-\u003einsn_aux_data[insn_idx].kptr_struct_meta;\nkernel/bpf/verifier.c-21447-\t\tstruct bpf_insn addr[2] = { BPF_LD_IMM64(BPF_REG_2, (long)kptr_struct_meta) };\nkernel/bpf/verifier.c:21448:\t\tu64 obj_new_size = env-\u003einsn_aux_data[insn_idx].obj_new_size;\nkernel/bpf/verifier.c-21449-\n--\nkernel/bpf/verifier.c-21463-\t\t   is_bpf_refcount_acquire_kfunc(desc-\u003efunc_id)) {\nkernel/bpf/verifier.c:21464:\t\tstruct btf_struct_meta *kptr_struct_meta = env-\u003einsn_aux_data[insn_idx].kptr_struct_meta;\nkernel/bpf/verifier.c-21465-\t\tstruct bpf_insn addr[2] = { BPF_LD_IMM64(BPF_REG_2, (long)kptr_struct_meta) };\n--\nkernel/bpf/verifier.c-21484-\t\t   is_bpf_rbtree_add_kfunc(desc-\u003efunc_id)) {\nkernel/bpf/verifier.c:21485:\t\tstruct btf_struct_meta *kptr_struct_meta = env-\u003einsn_aux_data[insn_idx].kptr_struct_meta;\nkernel/bpf/verifier.c-21486-\t\tint struct_meta_reg = BPF_REG_3;\n--\nkernel/bpf/verifier.c-21503-\nkernel/bpf/verifier.c:21504:\t\t__fixup_collection_insert_kfunc(\u0026env-\u003einsn_aux_data[insn_idx], struct_meta_reg,\nkernel/bpf/verifier.c-21505-\t\t\t\t\t\tnode_offset_reg, insn, insn_buf, cnt);\n--\nkernel/bpf/verifier.c-21589-\nkernel/bpf/verifier.c:21590:\tif (env-\u003einsn_aux_data[insn_idx].arg_prog) {\nkernel/bpf/verifier.c:21591:\t\tu32 regno = env-\u003einsn_aux_data[insn_idx].arg_prog;\nkernel/bpf/verifier.c-21592-\t\tstruct bpf_insn ld_addrs[2] = { BPF_LD_IMM64(regno, (long)env-\u003eprog-\u003eaux) };\n--\nkernel/bpf/verifier.c=21760=int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/verifier.c-21827-\nkernel/bpf/verifier.c:21828:\tlen = env-\u003einsn_aux_data_len = env-\u003eprog-\u003elen;\nkernel/bpf/verifier.c:21829:\tenv-\u003einsn_aux_data =\nkernel/bpf/verifier.c:21830:\t\t__vmalloc(array_size(sizeof(struct bpf_insn_aux_data), len),\nkernel/bpf/verifier.c-21831-\t\t\t  GFP_KERNEL_ACCOUNT | __GFP_ZERO);\nkernel/bpf/verifier.c-21832-\tret = -ENOMEM;\nkernel/bpf/verifier.c:21833:\tif (!env-\u003einsn_aux_data)\nkernel/bpf/verifier.c-21834-\t\tgoto skip_full_check;\nkernel/bpf/verifier.c-21835-\tfor (i = 0; i \u003c len; i++)\nkernel/bpf/verifier.c:21836:\t\tenv-\u003einsn_aux_data[i].orig_idx = i;\nkernel/bpf/verifier.c-21837-\tenv-\u003esucc = bpf_iarray_realloc(NULL, 2);\n--\nkernel/bpf/verifier.c-22091-err_free_env:\nkernel/bpf/verifier.c:22092:\tif (env-\u003einsn_aux_data)\nkernel/bpf/verifier.c:22093:\t\tbpf_clear_insn_aux_data(env, 0, env-\u003einsn_aux_data_len);\nkernel/bpf/verifier.c:22094:\tvfree(env-\u003einsn_aux_data);\nkernel/bpf/verifier.c-22095-\tkvfree(env-\u003efd_array);\n"
}

33/3 2026/09/25 10:07 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 108667
							output: 325
							thoughts: 2229
						

Thoughts:
**KMSAN Assessment: BPF Exception Cleanup Landing Pads**

Okay, let's break this down. My initial thought is to make sure all memory allocations are properly initialized. `env->insn_aux_data` is `__vmalloc`'d with `GFP_KERNEL_ACCOUNT | __GFP_ZERO`, so it's all zeroed from the start. Perfect. Now, I need to check against the established criteria, specifically focusing on uninitialized data usage.

First, let's examine complex data structure initialization. `check_cleanup_info` uses `kvcalloc` for `krecord` (zeroed). Similarly, `exc` gets `kzalloc`'d for the `bpf_exception_info` struct (also zeroed), and `ranges` via `kvcalloc` is also zeroed. The `exc_info_for_subprog` function allocates `recs` with `kvmalloc_array`, and importantly, the fields `begin_off`, `end_off`, and `landing_pad_off` are explicitly populated in a loop, ensuring they aren't left uninitialized.

Now, on to the second criterion: potential for bounds issues and accessing uninitialized data in existing buffers. The `check_cleanup_info` function validates `nrec`, `urec_size`, checks offsets, and ensures no overlaps or out-of-bounds access. The `bpf_exc_fill_native_ranges` function includes bound checks as well, using `WARN_ON_ONCE` to catch potential problems. Furthermore, the verifier's CFG and instruction checks cover landing pad formation and resume calls. It's clear that the code is deliberately designed to prevent uninitialized bytes from being accessed, or exposed to userspace.

Wait, what about `bpf_jmp_history_entry`? Let's check how it's allocated. Okay, in `bpf_push_jmp_history`, the fields `p->flags`, `p->spi`, `p->frame`, and `p->unwind_frames` are populated from a pre-existing environment. `p->unwind_frames` is assigned from `env->cur_unwind_frames`, which in turn is assigned from `env->unwind_frames`. Now, `env->unwind_frames` is initialized to zero when the environment is created (with `kzalloc`), and is subsequently modified in `unwind_step` and `process_cleanup_resume`. This means `bpf_jmp_history_entry` is fully initialized before use, given it is part of a larger, initialized structure.

What about `bpf_throw` and the assembly in `arch_bpf_run_cleanup_pad`? Can KMSAN detect anything here? No, the assembly is AMD64, which is not instrumented by KMSAN. The BPF code running in that assembly is JIT compiled, meaning that all the verifier checks and KASAN will detect if there is any bad memory access. This is a crucial point; the compiled BPF code *is* verified, so bad accesses should get caught. The registers are carefully restored.

Okay, let's take a step back and identify potential risks.

1.  Verifier bugs: Could be CFG bugs, infinite loops, precision issues, or illegal instruction paths. KASAN/UBSAN and assertions like `verifier_bug_if`, `WARN_ON` should catch memory errors, crashes and out-of-bounds access.
2.  JIT bugs: Incorrect offsets, stack alignment, or register spills. KASAN, stack canaries, and crashes are likely detectors.
3.  Memory leaks: `bpf_exc_free_info` should free allocated memory; if it fails, kmemleak/KASAN would catch it.
4.  Concurrency: Verification is single-threaded, and programs are executed in standard BPF contexts, so there are no concurrency or lock issues.

So, are there any uninitialized memory risks? No. There are no kernel stack structures that are copied to user space with uninitialized padding. Similarly, there are no kernel heap structures copied to user space. There are no `copy_to_user` calls with uninitialized buffers. All allocated kernel structures are zeroed (via `kzalloc`, `kvcalloc`, `GFP_ZERO`) or fully initialized before use. There are no conditional branches in the C code that depend on uninitialized variables.

Now, let's double-check against the KMSAN guidelines. It states, and I agree, that KMSAN should only be used if the code introduces or exposes *uninitialized* memory risks that are detected *only* by KMSAN. A dedicated KMSAN session is costly. It shouldn't be used if bugs can be caught by KASAN


34/3 2026/09/25 10:07 tool set-results
0m Args:
{
  "KMSANReasoning": "The patch series adds support for BPF exception cleanup landing pads across the BPF verifier, x86_64 and arm64 JIT compilers, runtime unwinding helpers, and libbpf.\n\nKASAN vs KMSAN applicability:\n- KMSAN detects reads of uninitialized memory and info-leaks to userspace. In this patch series, no uninitialized kernel memory is read or exposed. All new kernel data structures (`struct bpf_exception_info`, `struct bpf_cleanup_range`, `struct bpf_cleanup_info`, `insn_aux_data`) are either allocated with zeroing (`kzalloc_obj`, `kvcalloc`, `__GFP_ZERO`) or completely initialized before use. The only userspace copy added in `check_cleanup_info()` is `copy_to_bpfptr_offset()`, which writes a scalar `u32 min_size` that is fully initialized.\n- The primary risks in this patch are verifier state tracking/CFG analysis correctness, stack frame offset calculations during JIT compilation, and control flow integrity in landing pad execution. Any resulting bugs (such as stack corruption, out-of-bounds stack/heap access, or instruction pointer corruption) are memory corruption / out-of-bounds issues detectable by KASAN and standard detectors, not uninitialized memory issues.\n\nTherefore, a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false
}

Results:
{
  "KMSANReasoning": "The patch series adds support for BPF exception cleanup landing pads across the BPF verifier, x86_64 and arm64 JIT compilers, runtime unwinding helpers, and libbpf.\n\nKASAN vs KMSAN applicability:\n- KMSAN detects reads of uninitialized memory and info-leaks to userspace. In this patch series, no uninitialized kernel memory is read or exposed. All new kernel data structures (`struct bpf_exception_info`, `struct bpf_cleanup_range`, `struct bpf_cleanup_info`, `insn_aux_data`) are either allocated with zeroing (`kzalloc_obj`, `kvcalloc`, `__GFP_ZERO`) or completely initialized before use. The only userspace copy added in `check_cleanup_info()` is `copy_to_bpfptr_offset()`, which writes a scalar `u32 min_size` that is fully initialized.\n- The primary risks in this patch are verifier state tracking/CFG analysis correctness, stack frame offset calculations during JIT compilation, and control flow integrity in landing pad execution. Any resulting bugs (such as stack corruption, out-of-bounds stack/heap access, or instruction pointer corruption) are memory corruption / out-of-bounds issues detectable by KASAN and standard detectors, not uninitialized memory issues.\n\nTherefore, a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false
}

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)