AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/09/27 09:12 flow patch-triage
2m Args:
null

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "BPF_JIT",
    "DYNAMIC_FTRACE_WITH_DIRECT_CALLS",
    "TASKS_RCU"
  ],
  "FocusSymbols": [
    "bpf_trampoline_link_prog",
    "bpf_trampoline_unlink_prog",
    "bpf_trampoline_update",
    "bpf_trampoline_skip_prog",
    "bpf_trampoline_remove_prog",
    "bpf_tramp_image_put",
    "arch_prepare_bpf_trampoline",
    "bpf_trampoline_put"
  ],
  "KMSANReasoning": "The patch series modifies BPF trampoline generation and lifecycle handling across multiple architectures (x86, arm64, loongarch, powerpc, riscv, s390) and core BPF trampoline code to prevent a use-after-free (UAF) race condition when BPF programs are detached and freed while another task is preempted or sleeping inside a trampoline image. It patches NOP instructions to jump over detached programs and refines RCU synchronization (RCU tasks trace / tasks RCU).\n\nKMSAN vs. KASAN applicability:\n- The bug being addressed is a lifetime / use-after-free race condition, which is directly tracked and detected by KASAN, not KMSAN.\n- All newly introduced or modified kernel data structures (such as `im-\u003eskips` allocated via `kzalloc_objs` and stack instances `im` initialized with `= {}`) are fully zero-initialized before access.\n- There are no kernel-to-user memory transfers (no `copy_to_user`, `put_user`, socket options, or netlink attributes) introduced or altered that could result in information disclosure of uninitialized padding or stack/heap bytes.\n- Any potential memory safety defects introduced by this patch (e.g., out-of-bounds access in the `skips` array, list corruption, or use-after-free during image teardown) are detectable by KASAN and standard kernel debuggers.\n\nTherefore, this patch does not introduce or expose uninitialized memory risks, and a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false,
  "Reasoning": "The patch modifies BPF trampoline management and x86 JIT generation to prevent use-after-free/race conditions when detaching BPF programs while tasks are executing inside older trampoline images. Instead of patching only at the epilogue, it reserves NOPs before each program call and dynamically text-pokes them into skip jumps when programs detach, tracking images on a per-trampoline list and adjusting RCU grace periods (call_rcu_tasks). This code is fully reachable on amd64 via standard BPF syscalls (attaching and detaching fentry/fexit/fmod_ret/fsession programs) and warrants fuzzing due to the concurrency, live kernel text patching (bpf_arch_text_poke), and reference counting involved.",
  "WorthFuzzing": true
}

1/1 2026/09/27 09:12 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit 95aa626b973aa62514c088b7f1ab61c19b68b7ba\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Sun Sep 27 09:12:39 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c\nindex c5f55d6161fee..bd73179c00eca 100644\n--- a/arch/arm64/net/bpf_jit_comp.c\n+++ b/arch/arm64/net/bpf_jit_comp.c\n@@ -2418,10 +2418,11 @@ bool bpf_jit_supports_subprog_tailcalls(void)\n \treturn true;\n }\n \n-static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *node,\n-\t\t\t    int bargs_off, int retval_off, int run_ctx_off,\n-\t\t\t    bool save_ret)\n+static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_image *im,\n+\t\t\t    struct bpf_tramp_node *node, int bargs_off,\n+\t\t\t    int retval_off, int run_ctx_off, bool save_ret)\n {\n+\tvoid *skip;\n \t__le32 *branch;\n \tu64 enter_prog;\n \tu64 exit_prog;\n@@ -2431,6 +2432,10 @@ static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *node,\n \tenter_prog = (u64)bpf_trampoline_enter(p);\n \texit_prog = (u64)bpf_trampoline_exit(p);\n \n+\t/* nop, patched to skip this prog when it is detached */\n+\tskip = ctx-\u003ero_image + ctx-\u003eidx;\n+\temit(A64_NOP, ctx);\n+\n \tif (node-\u003ecookie == 0) {\n \t\t/* if cookie is zero, one instruction is enough to store it */\n \t\temit(A64_STR64I(A64_ZR, A64_SP, run_ctx_off + cookie_off), ctx);\n@@ -2483,11 +2488,13 @@ static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *node,\n \temit(A64_ADD_I(1, A64_R(2), A64_SP, run_ctx_off), ctx);\n \n \temit_call(exit_prog, ctx);\n+\n+\tbpf_tramp_image_add_skip(im, p, skip, ctx-\u003ero_image + ctx-\u003eidx);\n }\n \n-static void invoke_bpf_mod_ret(struct jit_ctx *ctx, struct bpf_tramp_nodes *tn,\n-\t\t\t       int bargs_off, int retval_off, int run_ctx_off,\n-\t\t\t       __le32 **branches)\n+static void invoke_bpf_mod_ret(struct jit_ctx *ctx, struct bpf_tramp_image *im,\n+\t\t\t       struct bpf_tramp_nodes *tn, int bargs_off,\n+\t\t\t       int retval_off, int run_ctx_off, __le32 **branches)\n {\n \tint i;\n \n@@ -2496,7 +2503,7 @@ static void invoke_bpf_mod_ret(struct jit_ctx *ctx, struct bpf_tramp_nodes *tn,\n \t */\n \temit(A64_STR64I(A64_ZR, A64_SP, retval_off), ctx);\n \tfor (i = 0; i \u003c tn-\u003enr_nodes; i++) {\n-\t\tinvoke_bpf_prog(ctx, tn-\u003enodes[i], bargs_off, retval_off,\n+\t\tinvoke_bpf_prog(ctx, im, tn-\u003enodes[i], bargs_off, retval_off,\n \t\t\t\trun_ctx_off, true);\n \t\t/* if (*(u64 *)(sp + retval_off) !=  0)\n \t\t *\tgoto do_fexit;\n@@ -2882,7 +2889,7 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,\n \t\t\tstore_func_meta(ctx, meta, func_meta_off);\n \t\t\tcookie_bargs_off--;\n \t\t}\n-\t\tinvoke_bpf_prog(ctx, fentry-\u003enodes[i], bargs_off,\n+\t\tinvoke_bpf_prog(ctx, im, fentry-\u003enodes[i], bargs_off,\n \t\t\t\tretval_off, run_ctx_off,\n \t\t\t\tflags \u0026 BPF_TRAMP_F_RET_FENTRY_RET);\n \t}\n@@ -2893,7 +2900,7 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,\n \t\tif (!branches)\n \t\t\treturn -ENOMEM;\n \n-\t\tinvoke_bpf_mod_ret(ctx, fmod_ret, bargs_off, retval_off,\n+\t\tinvoke_bpf_mod_ret(ctx, im, fmod_ret, bargs_off, retval_off,\n \t\t\t\t   run_ctx_off, branches);\n \t}\n \n@@ -2906,9 +2913,6 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,\n \t\temit(A64_RET(A64_R(10)), ctx);\n \t\t/* store return value */\n \t\temit(A64_STR64I(A64_R(0), A64_SP, retval_off), ctx);\n-\t\t/* reserve a nop for bpf_tramp_image_put */\n-\t\tim-\u003eip_after_call = ctx-\u003ero_image + ctx-\u003eidx;\n-\t\temit(A64_NOP, ctx);\n \t}\n \n \t/* update the branches saved in invoke_bpf_mod_ret with cbnz */\n@@ -2930,12 +2934,11 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,\n \t\t\tstore_func_meta(ctx, meta, func_meta_off);\n \t\t\tcookie_bargs_off--;\n \t\t}\n-\t\tinvoke_bpf_prog(ctx, fexit-\u003enodes[i], bargs_off, retval_off,\n+\t\tinvoke_bpf_prog(ctx, im, fexit-\u003enodes[i], bargs_off, retval_off,\n \t\t\t\trun_ctx_off, false);\n \t}\n \n \tif (flags \u0026 BPF_TRAMP_F_CALL_ORIG) {\n-\t\tim-\u003eip_epilogue = ctx-\u003ero_image + ctx-\u003eidx;\n \t\t/* for the first pass, assume the worst case */\n \t\tif (!ctx-\u003eimage)\n \t\t\tctx-\u003eidx += 4;\n@@ -2994,7 +2997,7 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n \t\t.image = NULL,\n \t\t.idx = 0,\n \t};\n-\tstruct bpf_tramp_image im;\n+\tstruct bpf_tramp_image im = {};\n \tstruct arg_aux aaux;\n \tint ret;\n \ndiff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c\nindex 4da2789009383..df8abfa055d1c 100644\n--- a/arch/loongarch/net/bpf_jit.c\n+++ b/arch/loongarch/net/bpf_jit.c\n@@ -1696,13 +1696,19 @@ static void restore_stk_args(struct jit_ctx *ctx, int nr_stk_args, int args_off,\n \t}\n }\n \n-static int invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *n,\n-\t\t\t   int args_off, int retval_off, int run_ctx_off, bool save_ret)\n+static int invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_image *im,\n+\t\t\t   struct bpf_tramp_node *n, int args_off, int retval_off,\n+\t\t\t   int run_ctx_off, bool save_ret)\n {\n-\tint ret;\n+\tint i, ret;\n \tu32 *branch;\n \tstruct bpf_prog *p = n-\u003elink-\u003eprog;\n \tint cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);\n+\tvoid *skip = ctx-\u003ero_image + ctx-\u003eidx;\n+\n+\t/* nops for move_imm+jirl, patched to skip this prog when it is detached */\n+\tfor (i = 0; i \u003c LOONGARCH_LONG_JUMP_NINSNS; i++)\n+\t\temit_insn(ctx, nop);\n \n \tif (n-\u003ecookie)\n \t\temit_store_stack_imm64(ctx, LOONGARCH_GPR_T1,\n@@ -1755,13 +1761,17 @@ static int invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *n,\n \t/* arg3: \u0026run_ctx */\n \temit_insn(ctx, addid, LOONGARCH_GPR_A2, LOONGARCH_GPR_FP, -run_ctx_off);\n \tret = emit_call(ctx, (const u64)bpf_trampoline_exit(p));\n+\tif (ret)\n+\t\treturn ret;\n \n-\treturn ret;\n+\tbpf_tramp_image_add_skip(im, p, skip, ctx-\u003ero_image + ctx-\u003eidx);\n+\treturn 0;\n }\n \n-static int invoke_bpf(struct jit_ctx *ctx, struct bpf_tramp_nodes *tn,\n-\t\t      int args_off, int retval_off, int run_ctx_off,\n-\t\t      int func_meta_off, bool save_ret, u64 func_meta, int cookie_off)\n+static int invoke_bpf(struct jit_ctx *ctx, struct bpf_tramp_image *im,\n+\t\t      struct bpf_tramp_nodes *tn, int args_off, int retval_off,\n+\t\t      int run_ctx_off, int func_meta_off, bool save_ret,\n+\t\t      u64 func_meta, int cookie_off)\n {\n \tint i, cur_cookie = (cookie_off - args_off) / 8;\n \n@@ -1774,7 +1784,8 @@ static int invoke_bpf(struct jit_ctx *ctx, struct bpf_tramp_nodes *tn,\n \t\t\temit_store_stack_imm64(ctx, LOONGARCH_GPR_T1, -func_meta_off, meta);\n \t\t\tcur_cookie--;\n \t\t}\n-\t\terr = invoke_bpf_prog(ctx, tn-\u003enodes[i], args_off, retval_off, run_ctx_off, save_ret);\n+\t\terr = invoke_bpf_prog(ctx, im, tn-\u003enodes[i], args_off, retval_off,\n+\t\t\t\t      run_ctx_off, save_ret);\n \t\tif (err)\n \t\t\treturn err;\n \t}\n@@ -2017,7 +2028,7 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i\n \t}\n \n \tif (fentry-\u003enr_nodes) {\n-\t\tret = invoke_bpf(ctx, fentry, args_off, retval_off, run_ctx_off, func_meta_off,\n+\t\tret = invoke_bpf(ctx, im, fentry, args_off, retval_off, run_ctx_off, func_meta_off,\n \t\t\t\t flags \u0026 BPF_TRAMP_F_RET_FENTRY_RET, func_meta, cookie_off);\n \t\tif (ret)\n \t\t\treturn ret;\n@@ -2029,7 +2040,7 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i\n \n \t\temit_insn(ctx, std, LOONGARCH_GPR_ZERO, LOONGARCH_GPR_FP, -retval_off);\n \t\tfor (i = 0; i \u003c fmod_ret-\u003enr_nodes; i++) {\n-\t\t\tret = invoke_bpf_prog(ctx, fmod_ret-\u003enodes[i],\n+\t\t\tret = invoke_bpf_prog(ctx, im, fmod_ret-\u003enodes[i],\n \t\t\t\t\t      args_off, retval_off, run_ctx_off, true);\n \t\t\tif (ret)\n \t\t\t\tgoto out;\n@@ -2051,10 +2062,6 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i\n \t\t\tgoto out;\n \t\temit_insn(ctx, std, LOONGARCH_GPR_A0, LOONGARCH_GPR_FP, -retval_off);\n \t\temit_insn(ctx, std, regmap[BPF_REG_0], LOONGARCH_GPR_FP, -(retval_off - 8));\n-\t\tim-\u003eip_after_call = ctx-\u003ero_image + ctx-\u003eidx;\n-\t\t/* Reserve space for the move_imm + jirl instruction */\n-\t\tfor (i = 0; i \u003c LOONGARCH_LONG_JUMP_NINSNS; i++)\n-\t\t\temit_insn(ctx, nop);\n \t}\n \n \tfor (i = 0; ctx-\u003eimage \u0026\u0026 i \u003c fmod_ret-\u003enr_nodes; i++) {\n@@ -2068,14 +2075,13 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i\n \t\temit_store_stack_imm64(ctx, LOONGARCH_GPR_T1, -func_meta_off, func_meta);\n \n \tif (fexit-\u003enr_nodes) {\n-\t\tret = invoke_bpf(ctx, fexit, args_off, retval_off, run_ctx_off,\n+\t\tret = invoke_bpf(ctx, im, fexit, args_off, retval_off, run_ctx_off,\n \t\t\t\t func_meta_off, false, func_meta, cookie_off);\n \t\tif (ret)\n \t\t\tgoto out;\n \t}\n \n \tif (flags \u0026 BPF_TRAMP_F_CALL_ORIG) {\n-\t\tim-\u003eip_epilogue = ctx-\u003ero_image + ctx-\u003eidx;\n \t\tmove_addr(ctx, LOONGARCH_GPR_A0, (const u64)im);\n \t\tret = emit_call(ctx, (const u64)__bpf_tramp_exit);\n \t\tif (ret)\n@@ -2177,11 +2183,8 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n \t\t\t     struct bpf_tramp_nodes *tnodes, void *func_addr)\n {\n \tint ret;\n-\tstruct jit_ctx ctx;\n-\tstruct bpf_tramp_image im;\n-\n-\tctx.image = NULL;\n-\tctx.idx = 0;\n+\tstruct jit_ctx ctx = {};\n+\tstruct bpf_tramp_image im = {};\n \n \tret = __arch_prepare_bpf_trampoline(\u0026ctx, \u0026im, m, tnodes, func_addr, flags);\n \ndiff --git a/arch/powerpc/net/bpf_jit_comp.c b/arch/powerpc/net/bpf_jit_comp.c\nindex 7b07b43575f11..7a612688e7a20 100644\n--- a/arch/powerpc/net/bpf_jit_comp.c\n+++ b/arch/powerpc/net/bpf_jit_comp.c\n@@ -602,14 +602,18 @@ int arch_protect_bpf_trampoline(void *image, unsigned int size)\n }\n \n static int invoke_bpf_prog(u32 *image, u32 *ro_image, struct codegen_context *ctx,\n-\t\t\t   struct bpf_tramp_node *n, int regs_off, int retval_off,\n-\t\t\t   int run_ctx_off, bool save_ret)\n+\t\t\t   struct bpf_tramp_image *im, struct bpf_tramp_node *n,\n+\t\t\t   int regs_off, int retval_off, int run_ctx_off, bool save_ret)\n {\n \tstruct bpf_prog *p = n-\u003elink-\u003eprog;\n \tppc_inst_t branch_insn;\n-\tu32 jmp_idx;\n+\tu32 jmp_idx, skip_idx;\n \tint ret = 0;\n \n+\t/* nop, patched to skip this prog when it is detached */\n+\tskip_idx = ctx-\u003eidx;\n+\tEMIT(PPC_RAW_NOP());\n+\n \t/* Save cookie */\n \tif (IS_ENABLED(CONFIG_PPC64)) {\n \t\tPPC_LI64(_R3, n-\u003ecookie);\n@@ -679,13 +683,17 @@ static int invoke_bpf_prog(u32 *image, u32 *ro_image, struct codegen_context *ct\n \tEMIT(PPC_RAW_ADDI(_R5, _R1, run_ctx_off));\n \tret = bpf_jit_emit_func_call_rel(image, ro_image, ctx,\n \t\t\t\t\t (unsigned long)bpf_trampoline_exit(p));\n+\tif (ret)\n+\t\treturn ret;\n \n-\treturn ret;\n+\tif (ro_image) /* image is NULL for dummy pass */\n+\t\tbpf_tramp_image_add_skip(im, p, \u0026ro_image[skip_idx], \u0026ro_image[ctx-\u003eidx]);\n+\treturn 0;\n }\n \n static int invoke_bpf_mod_ret(u32 *image, u32 *ro_image, struct codegen_context *ctx,\n-\t\t\t      struct bpf_tramp_nodes *tn, int regs_off, int retval_off,\n-\t\t\t      int run_ctx_off, u32 *branches)\n+\t\t\t      struct bpf_tramp_image *im, struct bpf_tramp_nodes *tn,\n+\t\t\t      int regs_off, int retval_off, int run_ctx_off, u32 *branches)\n {\n \tint i;\n \n@@ -696,8 +704,8 @@ static int invoke_bpf_mod_ret(u32 *image, u32 *ro_image, struct codegen_context\n \tEMIT(PPC_RAW_LI(_R3, 0));\n \tEMIT(PPC_RAW_STL(_R3, _R1, retval_off));\n \tfor (i = 0; i \u003c tn-\u003enr_nodes; i++) {\n-\t\tif (invoke_bpf_prog(image, ro_image, ctx, tn-\u003enodes[i], regs_off, retval_off,\n-\t\t\t\t    run_ctx_off, true))\n+\t\tif (invoke_bpf_prog(image, ro_image, ctx, im, tn-\u003enodes[i], regs_off,\n+\t\t\t\t    retval_off, run_ctx_off, true))\n \t\t\treturn -EINVAL;\n \n \t\t/*\n@@ -1043,8 +1051,8 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im\n \t\t\tcookie_ctx_off--;\n \t\t}\n \n-\t\tif (invoke_bpf_prog(image, ro_image, ctx, fentry-\u003enodes[i], regs_off, retval_off,\n-\t\t\t\t    run_ctx_off, flags \u0026 BPF_TRAMP_F_RET_FENTRY_RET))\n+\t\tif (invoke_bpf_prog(image, ro_image, ctx, im, fentry-\u003enodes[i], regs_off,\n+\t\t\t\t    retval_off, run_ctx_off, flags \u0026 BPF_TRAMP_F_RET_FENTRY_RET))\n \t\t\treturn -EINVAL;\n \t}\n \n@@ -1053,7 +1061,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im\n \t\tif (!branches)\n \t\t\treturn -ENOMEM;\n \n-\t\tif (invoke_bpf_mod_ret(image, ro_image, ctx, fmod_ret, regs_off, retval_off,\n+\t\tif (invoke_bpf_mod_ret(image, ro_image, ctx, im, fmod_ret, regs_off, retval_off,\n \t\t\t\t       run_ctx_off, branches)) {\n \t\t\tret = -EINVAL;\n \t\t\tgoto cleanup;\n@@ -1090,11 +1098,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im\n \t\t/* Restore updated tail_call_cnt */\n \t\tif (flags \u0026 BPF_TRAMP_F_TAIL_CALL_CTX)\n \t\t\tbpf_trampoline_restore_tail_call_cnt(image, ctx, bpf_frame_size, r4_off);\n-\n-\t\t/* Reserve space to patch branch instruction to skip fexit progs */\n-\t\tif (ro_image) /* image is NULL for dummy pass */\n-\t\t\tim-\u003eip_after_call = \u0026((u32 *)ro_image)[ctx-\u003eidx];\n-\t\tEMIT(PPC_RAW_NOP());\n \t}\n \n \t/* Update branches saved in invoke_bpf_mod_ret with address of do_fexit */\n@@ -1123,16 +1126,14 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im\n \t\t\tcookie_ctx_off--;\n \t\t}\n \n-\t\tif (invoke_bpf_prog(image, ro_image, ctx, fexit-\u003enodes[i], regs_off, retval_off,\n-\t\t\t\t    run_ctx_off, false)) {\n+\t\tif (invoke_bpf_prog(image, ro_image, ctx, im, fexit-\u003enodes[i], regs_off,\n+\t\t\t\t    retval_off, run_ctx_off, false)) {\n \t\t\tret = -EINVAL;\n \t\t\tgoto cleanup;\n \t\t}\n \t}\n \n \tif (flags \u0026 BPF_TRAMP_F_CALL_ORIG) {\n-\t\tif (ro_image) /* image is NULL for dummy pass */\n-\t\t\tim-\u003eip_epilogue = \u0026((u32 *)ro_image)[ctx-\u003eidx];\n \t\tPPC_LI_ADDR(_R3, im);\n \t\tret = bpf_jit_emit_func_call_rel(image, ro_image, ctx,\n \t\t\t\t\t\t (unsigned long)__bpf_tramp_exit);\n@@ -1192,7 +1193,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im\n int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n \t\t\t     struct bpf_tramp_nodes *tnodes, void *func_addr)\n {\n-\tstruct bpf_tramp_image im;\n+\tstruct bpf_tramp_image im = {};\n \tint ret;\n \n \tret = __arch_prepare_bpf_trampoline(\u0026im, NULL, NULL, NULL, m, flags, tnodes, func_addr);\n@@ -1320,7 +1321,7 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,\n \n \t/*\n \t * If we are not poking at bpf prog entry, then we are simply patching in/out\n-\t * an unconditional branch instruction at im-\u003eip_after_call\n+\t * an unconditional branch instruction in a trampoline image\n \t */\n \tif (offset) {\n \t\tif (old_t == BPF_MOD_CALL || new_t == BPF_MOD_CALL) {\ndiff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c\nindex 151031e97a243..558e02dc2fe39 100644\n--- a/arch/riscv/net/bpf_jit_comp64.c\n+++ b/arch/riscv/net/bpf_jit_comp64.c\n@@ -904,12 +904,18 @@ static void emit_store_stack_imm64(u8 reg, int stack_off, u64 imm64,\n \temit_sd(RV_REG_FP, stack_off, reg, ctx);\n }\n \n-static int invoke_bpf_prog(struct bpf_tramp_node *node, int args_off, int retval_off,\n-\t\t\t   int run_ctx_off, bool save_ret, struct rv_jit_context *ctx)\n+static int invoke_bpf_prog(struct bpf_tramp_image *im, struct bpf_tramp_node *node,\n+\t\t\t   int args_off, int retval_off, int run_ctx_off, bool save_ret,\n+\t\t\t   struct rv_jit_context *ctx)\n {\n \tint ret, branch_off;\n \tstruct bpf_prog *p = node-\u003elink-\u003eprog;\n \tint cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);\n+\tvoid *skip = ctx-\u003ero_insns + ctx-\u003eninsns;\n+\n+\t/* 2 nops for auipc+jalr, patched to skip this prog when it is detached */\n+\temit(rv_nop(), ctx);\n+\temit(rv_nop(), ctx);\n \n \tif (node-\u003ecookie)\n \t\temit_store_stack_imm64(RV_REG_T1, -run_ctx_off + cookie_off, node-\u003ecookie, ctx);\n@@ -962,13 +968,17 @@ static int invoke_bpf_prog(struct bpf_tramp_node *node, int args_off, int retval\n \t/* arg3: \u0026run_ctx */\n \temit_addi(RV_REG_A2, RV_REG_FP, -run_ctx_off, ctx);\n \tret = emit_call((const u64)bpf_trampoline_exit(p), true, ctx);\n+\tif (ret)\n+\t\treturn ret;\n \n-\treturn ret;\n+\tbpf_tramp_image_add_skip(im, p, skip, ctx-\u003ero_insns + ctx-\u003eninsns);\n+\treturn 0;\n }\n \n-static int invoke_bpf(struct bpf_tramp_nodes *tn, int args_off, int retval_off,\n-\t\t      int run_ctx_off, int func_meta_off, bool save_ret, u64 func_meta,\n-\t\t      int cookie_off, struct rv_jit_context *ctx)\n+static int invoke_bpf(struct bpf_tramp_image *im, struct bpf_tramp_nodes *tn,\n+\t\t      int args_off, int retval_off, int run_ctx_off, int func_meta_off,\n+\t\t      bool save_ret, u64 func_meta, int cookie_off,\n+\t\t      struct rv_jit_context *ctx)\n {\n \tint i, cur_cookie = (cookie_off - args_off) / 8;\n \n@@ -981,8 +991,8 @@ static int invoke_bpf(struct bpf_tramp_nodes *tn, int args_off, int retval_off,\n \t\t\temit_store_stack_imm64(RV_REG_T1, -func_meta_off, meta, ctx);\n \t\t\tcur_cookie--;\n \t\t}\n-\t\terr = invoke_bpf_prog(tn-\u003enodes[i], args_off, retval_off, run_ctx_off,\n-\t\t\t\t      save_ret, ctx);\n+\t\terr = invoke_bpf_prog(im, tn-\u003enodes[i], args_off, retval_off,\n+\t\t\t\t      run_ctx_off, save_ret, ctx);\n \t\tif (err)\n \t\t\treturn err;\n \t}\n@@ -1170,7 +1180,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,\n \t}\n \n \tif (fentry-\u003enr_nodes) {\n-\t\tret = invoke_bpf(fentry, args_off, retval_off, run_ctx_off, func_meta_off,\n+\t\tret = invoke_bpf(im, fentry, args_off, retval_off, run_ctx_off, func_meta_off,\n \t\t\t\t flags \u0026 BPF_TRAMP_F_RET_FENTRY_RET, func_meta, cookie_off, ctx);\n \t\tif (ret)\n \t\t\treturn ret;\n@@ -1184,7 +1194,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,\n \t\t/* cleanup to avoid garbage return value confusion */\n \t\temit_sd(RV_REG_FP, -retval_off, RV_REG_ZERO, ctx);\n \t\tfor (i = 0; i \u003c fmod_ret-\u003enr_nodes; i++) {\n-\t\t\tret = invoke_bpf_prog(fmod_ret-\u003enodes[i], args_off, retval_off,\n+\t\t\tret = invoke_bpf_prog(im, fmod_ret-\u003enodes[i], args_off, retval_off,\n \t\t\t\t\t      run_ctx_off, true, ctx);\n \t\t\tif (ret)\n \t\t\t\tgoto out;\n@@ -1211,10 +1221,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,\n \t\t\temit_sd(RV_REG_FP, -tcc_off, RV_REG_TCC, ctx);\n \t\temit_sd(RV_REG_FP, -retval_off, RV_REG_A0, ctx);\n \t\temit_sd(RV_REG_FP, -(retval_off - 8), regmap[BPF_REG_0], ctx);\n-\t\tim-\u003eip_after_call = ctx-\u003ero_insns + ctx-\u003eninsns;\n-\t\t/* 2 nops reserved for auipc+jalr pair */\n-\t\temit(rv_nop(), ctx);\n-\t\temit(rv_nop(), ctx);\n \t}\n \n \t/* update branches saved in invoke_bpf_mod_ret with bnez */\n@@ -1230,14 +1236,13 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,\n \t\temit_store_stack_imm64(RV_REG_T1, -func_meta_off, func_meta, ctx);\n \n \tif (fexit-\u003enr_nodes) {\n-\t\tret = invoke_bpf(fexit, args_off, retval_off, run_ctx_off, func_meta_off,\n+\t\tret = invoke_bpf(im, fexit, args_off, retval_off, run_ctx_off, func_meta_off,\n \t\t\t\t false, func_meta, cookie_off, ctx);\n \t\tif (ret)\n \t\t\tgoto out;\n \t}\n \n \tif (flags \u0026 BPF_TRAMP_F_CALL_ORIG) {\n-\t\tim-\u003eip_epilogue = ctx-\u003ero_insns + ctx-\u003eninsns;\n \t\temit_imm(RV_REG_A0, ctx-\u003einsns ? (const s64)im : RV_MAX_COUNT_IMM, ctx);\n \t\tret = emit_call((const u64)__bpf_tramp_exit, true, ctx);\n \t\tif (ret)\n@@ -1299,7 +1304,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,\n int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n \t\t\t     struct bpf_tramp_nodes *tnodes, void *func_addr)\n {\n-\tstruct bpf_tramp_image im;\n+\tstruct bpf_tramp_image im = {};\n \tstruct rv_jit_context ctx;\n \tint ret;\n \ndiff --git a/arch/s390/net/bpf_jit_comp.c b/arch/s390/net/bpf_jit_comp.c\nindex c4b47070bb590..1b2566012b63a 100644\n--- a/arch/s390/net/bpf_jit_comp.c\n+++ b/arch/s390/net/bpf_jit_comp.c\n@@ -2566,6 +2566,8 @@ struct bpf_tramp_jit {\n \tint r14_off;\t\t/* Offset of saved %r14, has to be at the\n \t\t\t\t * bottom */\n \tint do_fexit;\t\t/* do_fexit: label */\n+\tint skip[BPF_MAX_TRAMP_LINKS];\t/* skip: labels after each prog */\n+\tint nr_progs;\n };\n \n static void load_imm64(struct bpf_jit *jit, int dst_reg, u64 val)\n@@ -2584,6 +2586,7 @@ static void emit_store_stack_imm64(struct bpf_jit *jit, int tmp_reg, int stack_o\n }\n \n static int invoke_bpf_prog(struct bpf_tramp_jit *tjit,\n+\t\t\t   struct bpf_tramp_image *im,\n \t\t\t   const struct btf_func_model *m,\n \t\t\t   struct bpf_tramp_node *node, bool save_ret)\n {\n@@ -2591,8 +2594,20 @@ static int invoke_bpf_prog(struct bpf_tramp_jit *tjit,\n \tint cookie_off = tjit-\u003erun_ctx_off +\n \t\t\t offsetof(struct bpf_tramp_run_ctx, bpf_cookie);\n \tstruct bpf_prog *p = node-\u003elink-\u003eprog;\n+\tvoid *skip = jit-\u003eprg_buf + jit-\u003eprg;\n+\tint idx = tjit-\u003enr_progs++;\n \tint patch;\n \n+\tif (idx \u003e= ARRAY_SIZE(tjit-\u003eskip))\n+\t\treturn -E2BIG;\n+\n+\t/*\n+\t * nop, patched to skip this prog when it is detached\n+\t */\n+\n+\t/* brcl 0,skip */\n+\tEMIT6_PCREL_RILC(0xc0040000, 0, tjit-\u003eskip[idx]);\n+\n \t/*\n \t * run_ctx.cookie = node-\u003ecookie;\n \t */\n@@ -2652,10 +2667,15 @@ static int invoke_bpf_prog(struct bpf_tramp_jit *tjit,\n \t/* brasl %r14,__bpf_prog_exit */\n \tEMIT6_PCREL_RILB_PTR(0xc0050000, REG_14, bpf_trampoline_exit(p));\n \n+\t/* skip: */\n+\ttjit-\u003eskip[idx] = jit-\u003eprg;\n+\tbpf_tramp_image_add_skip(im, p, skip, jit-\u003eprg_buf + jit-\u003eprg);\n+\n \treturn 0;\n }\n \n static int invoke_bpf(struct bpf_tramp_jit *tjit,\n+\t\t      struct bpf_tramp_image *im,\n \t\t      const struct btf_func_model *m,\n \t\t      struct bpf_tramp_nodes *tn, bool save_ret,\n \t\t      u64 func_meta, int cookie_off)\n@@ -2670,7 +2690,7 @@ static int invoke_bpf(struct bpf_tramp_jit *tjit,\n \t\t\temit_store_stack_imm64(jit, REG_0, tjit-\u003efunc_meta_off, meta);\n \t\t\tcur_cookie--;\n \t\t}\n-\t\tif (invoke_bpf_prog(tjit, m, tn-\u003enodes[i], save_ret))\n+\t\tif (invoke_bpf_prog(tjit, im, m, tn-\u003enodes[i], save_ret))\n \t\t\treturn -EINVAL;\n \t}\n \n@@ -2712,6 +2732,11 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,\n \tu64 func_meta;\n \tint i, j;\n \n+\t/* The skip labels are taken from the previous pass. */\n+\ttjit-\u003enr_progs = 0;\n+\tif (im)\n+\t\tim-\u003enr_skips = 0;\n+\n \t/* Support as many stack arguments as \"mvc\" instruction can handle. */\n \tnr_reg_args = min_t(int, m-\u003enr_args, MAX_NR_REG_ARGS);\n \tnr_stack_args = m-\u003enr_args - nr_reg_args;\n@@ -2875,7 +2900,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,\n \t\temit_store_stack_imm64(jit, REG_0, tjit-\u003eretval_off, 0);\n \t}\n \n-\tif (invoke_bpf(tjit, m, fentry, flags \u0026 BPF_TRAMP_F_RET_FENTRY_RET,\n+\tif (invoke_bpf(tjit, im, m, fentry, flags \u0026 BPF_TRAMP_F_RET_FENTRY_RET,\n \t\t       func_meta, cookie_off))\n \t\treturn -EINVAL;\n \n@@ -2889,7 +2914,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,\n \t\t       0xf000 | tjit-\u003eretval_off);\n \n \t\tfor (i = 0; i \u003c fmod_ret-\u003enr_nodes; i++) {\n-\t\t\tif (invoke_bpf_prog(tjit, m, fmod_ret-\u003enodes[i], true))\n+\t\t\tif (invoke_bpf_prog(tjit, im, m, fmod_ret-\u003enodes[i], true))\n \t\t\t\treturn -EINVAL;\n \n \t\t\t/*\n@@ -2943,15 +2968,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,\n \t\t/* mvc tccnt_off(%r15),tail_call_cnt(4,%r15) */\n \t\t_EMIT6(0xd203f000 | tjit-\u003etccnt_off,\n \t\t       0xf000 | offsetof(struct prog_frame, tail_call_cnt));\n-\n-\t\tim-\u003eip_after_call = jit-\u003eprg_buf + jit-\u003eprg;\n-\n-\t\t/*\n-\t\t * The following nop will be patched by bpf_tramp_image_put().\n-\t\t */\n-\n-\t\t/* brcl 0,im-\u003eip_epilogue */\n-\t\tEMIT6_PCREL_RILC(0xc0040000, 0, (u64)im-\u003eip_epilogue);\n \t}\n \n \t/* Set the \"is_return\" flag for fsession. */\n@@ -2962,12 +2978,10 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,\n \n \t/* do_fexit: */\n \ttjit-\u003edo_fexit = jit-\u003eprg;\n-\tif (invoke_bpf(tjit, m, fexit, false, func_meta, cookie_off))\n+\tif (invoke_bpf(tjit, im, m, fexit, false, func_meta, cookie_off))\n \t\treturn -EINVAL;\n \n \tif (flags \u0026 BPF_TRAMP_F_CALL_ORIG) {\n-\t\tim-\u003eip_epilogue = jit-\u003eprg_buf + jit-\u003eprg;\n-\n \t\t/*\n \t\t * __bpf_tramp_exit(im);\n \t\t */\n@@ -3016,7 +3030,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,\n int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n \t\t\t     struct bpf_tramp_nodes *tnodes, void *orig_call)\n {\n-\tstruct bpf_tramp_image im;\n+\tstruct bpf_tramp_image im = {};\n \tstruct bpf_tramp_jit tjit;\n \tint ret;\n \ndiff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c\nindex 2853e87797a78..6bca87457e872 100644\n--- a/arch/x86/net/bpf_jit_comp.c\n+++ b/arch/x86/net/bpf_jit_comp.c\n@@ -3217,16 +3217,21 @@ static void restore_regs(const struct btf_func_model *m, u8 **prog,\n }\n \n static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,\n+\t\t\t   struct bpf_tramp_image *im,\n \t\t\t   struct bpf_tramp_node *node, int stack_size,\n \t\t\t   int run_ctx_off, bool save_ret,\n \t\t\t   void *image, void *rw_image)\n {\n \tu8 *prog = *pprog;\n-\tu8 *jmp_insn;\n+\tu8 *jmp_insn, *skip;\n \tint ctx_cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);\n \tstruct bpf_prog *p = node-\u003elink-\u003eprog;\n \tu64 cookie = node-\u003ecookie;\n \n+\t/* nop, patched to skip this prog when it is detached */\n+\tskip = image + (prog - (u8 *)rw_image);\n+\temit_nops(\u0026prog, X86_PATCH_SIZE);\n+\n \t/* mov rdi, cookie */\n \temit_mov_imm64(\u0026prog, BPF_REG_1, (long) cookie \u003e\u003e 32, (u32) (long) cookie);\n \n@@ -3301,6 +3306,8 @@ static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,\n \tif (emit_rsb_call(\u0026prog, bpf_trampoline_exit(p), image + (prog - (u8 *)rw_image)))\n \t\treturn -EINVAL;\n \n+\tbpf_tramp_image_add_skip(im, p, skip, image + (prog - (u8 *)rw_image));\n+\n \t*pprog = prog;\n \treturn 0;\n }\n@@ -3332,6 +3339,7 @@ static int emit_cond_near_jump(u8 **pprog, void *func, void *ip, u8 jmp_cond)\n }\n \n static int invoke_bpf(const struct btf_func_model *m, u8 **pprog,\n+\t\t      struct bpf_tramp_image *im,\n \t\t      struct bpf_tramp_nodes *tl, int stack_size,\n \t\t      int run_ctx_off, int func_meta_off, bool save_ret,\n \t\t      void *image, void *rw_image, u64 func_meta,\n@@ -3346,7 +3354,7 @@ static int invoke_bpf(const struct btf_func_model *m, u8 **pprog,\n \t\t\t\tfunc_meta | (cur_cookie \u003c\u003c BPF_TRAMP_COOKIE_INDEX_SHIFT));\n \t\t\tcur_cookie--;\n \t\t}\n-\t\tif (invoke_bpf_prog(m, \u0026prog, tl-\u003enodes[i], stack_size,\n+\t\tif (invoke_bpf_prog(m, \u0026prog, im, tl-\u003enodes[i], stack_size,\n \t\t\t\t    run_ctx_off, save_ret, image, rw_image))\n \t\t\treturn -EINVAL;\n \t}\n@@ -3355,6 +3363,7 @@ static int invoke_bpf(const struct btf_func_model *m, u8 **pprog,\n }\n \n static int invoke_bpf_mod_ret(const struct btf_func_model *m, u8 **pprog,\n+\t\t\t      struct bpf_tramp_image *im,\n \t\t\t      struct bpf_tramp_nodes *tl, int stack_size,\n \t\t\t      int run_ctx_off, u8 **branches,\n \t\t\t      void *image, void *rw_image)\n@@ -3368,7 +3377,7 @@ static int invoke_bpf_mod_ret(const struct btf_func_model *m, u8 **pprog,\n \temit_mov_imm32(\u0026prog, false, BPF_REG_0, 0);\n \temit_stx(\u0026prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8);\n \tfor (i = 0; i \u003c tl-\u003enr_nodes; i++) {\n-\t\tif (invoke_bpf_prog(m, \u0026prog, tl-\u003enodes[i], stack_size, run_ctx_off, true,\n+\t\tif (invoke_bpf_prog(m, \u0026prog, im, tl-\u003enodes[i], stack_size, run_ctx_off, true,\n \t\t\t\t    image, rw_image))\n \t\t\treturn -EINVAL;\n \n@@ -3640,7 +3649,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im\n \t}\n \n \tif (fentry-\u003enr_nodes) {\n-\t\tif (invoke_bpf(m, \u0026prog, fentry, regs_off, run_ctx_off, func_meta_off,\n+\t\tif (invoke_bpf(m, \u0026prog, im, fentry, regs_off, run_ctx_off, func_meta_off,\n \t\t\t       flags \u0026 BPF_TRAMP_F_RET_FENTRY_RET, image, rw_image,\n \t\t\t       func_meta, cookie_off))\n \t\t\treturn -EINVAL;\n@@ -3652,7 +3661,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im\n \t\tif (!branches)\n \t\t\treturn -ENOMEM;\n \n-\t\tif (invoke_bpf_mod_ret(m, \u0026prog, fmod_ret, regs_off,\n+\t\tif (invoke_bpf_mod_ret(m, \u0026prog, im, fmod_ret, regs_off,\n \t\t\t\t       run_ctx_off, branches, image, rw_image)) {\n \t\t\tret = -EINVAL;\n \t\t\tgoto cleanup;\n@@ -3682,8 +3691,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im\n \t\t}\n \t\t/* remember return value in a stack for bpf prog to access */\n \t\temit_stx(\u0026prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8);\n-\t\tim-\u003eip_after_call = image + (prog - (u8 *)rw_image);\n-\t\temit_nops(\u0026prog, X86_PATCH_SIZE);\n \t}\n \n \tif (fmod_ret-\u003enr_nodes) {\n@@ -3708,7 +3715,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im\n \t\temit_store_stack_imm64(\u0026prog, BPF_REG_0, -func_meta_off, func_meta);\n \n \tif (fexit-\u003enr_nodes) {\n-\t\tif (invoke_bpf(m, \u0026prog, fexit, regs_off, run_ctx_off, func_meta_off,\n+\t\tif (invoke_bpf(m, \u0026prog, im, fexit, regs_off, run_ctx_off, func_meta_off,\n \t\t\t       false, image, rw_image, func_meta, cookie_off)) {\n \t\t\tret = -EINVAL;\n \t\t\tgoto cleanup;\n@@ -3723,7 +3730,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im\n \t * restored to R0.\n \t */\n \tif (flags \u0026 BPF_TRAMP_F_CALL_ORIG) {\n-\t\tim-\u003eip_epilogue = image + (prog - (u8 *)rw_image);\n \t\t/* arg1: mov rdi, im */\n \t\temit_mov_imm64(\u0026prog, BPF_REG_1, (long) im \u003e\u003e 32, (u32) (long) im);\n \t\tif (emit_rsb_call(\u0026prog, __bpf_tramp_exit, image + (prog - (u8 *)rw_image))) {\n@@ -3811,7 +3817,7 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i\n int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n \t\t\t     struct bpf_tramp_nodes *tnodes, void *func_addr)\n {\n-\tstruct bpf_tramp_image im;\n+\tstruct bpf_tramp_image im = {};\n \tvoid *image;\n \tint ret;\n \ndiff --git a/include/linux/bpf.h b/include/linux/bpf.h\nindex 1d2676782d707..d5b507f090ebe 100644\n--- a/include/linux/bpf.h\n+++ b/include/linux/bpf.h\n@@ -1258,11 +1258,17 @@ struct btf_func_model {\n #define BPF_TRAMP_F_INDIRECT\t\tBIT(8)\n \n /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50\n- * bytes on x86.\n+ * bytes on x86. The trampoline image has to fit in PAGE_SIZE.\n  */\n enum {\n-#if defined(__s390x__)\n+#if defined(__s390x__) || defined(__powerpc64__)\n \tBPF_MAX_TRAMP_LINKS = 27,\n+#elif defined(__loongarch__)\n+\tBPF_MAX_TRAMP_LINKS = 33,\n+#elif defined(__x86_64__)\n+\tBPF_MAX_TRAMP_LINKS = 36,\n+#elif defined(__aarch64__)\n+\tBPF_MAX_TRAMP_LINKS = 37,\n #else\n \tBPF_MAX_TRAMP_LINKS = 38,\n #endif\n@@ -1363,19 +1369,48 @@ enum bpf_tramp_prog_type {\n \tBPF_TRAMP_FSESSION,\n };\n \n+/*\n+ * Each prog call in a trampoline image is preceded by a nop. When the prog is\n+ * detached, the nop is patched to a jump to target, right after the call, so\n+ * that tasks still running in the image skip the prog.\n+ */\n+struct bpf_tramp_skip {\n+\tstruct bpf_prog *prog;\n+\tvoid *nop;\n+\tvoid *target;\n+};\n+\n struct bpf_tramp_image {\n \tvoid *image;\n \tint size;\n \tstruct bpf_ksym ksym;\n \tstruct percpu_ref pcref;\n-\tvoid *ip_after_call;\n-\tvoid *ip_epilogue;\n+\tbool call_orig;\n+\t/* entry in tr-\u003eimages, the image holds a reference on tr */\n+\tstruct bpf_trampoline *tr;\n+\tstruct list_head list;\n+\tint nr_skips;\n+\tstruct bpf_tramp_skip *skips;\n \tunion {\n \t\tstruct rcu_head rcu;\n \t\tstruct work_struct work;\n \t};\n };\n \n+static inline void bpf_tramp_image_add_skip(struct bpf_tramp_image *im, struct bpf_prog *prog,\n+\t\t\t\t\t    void *nop, void *target)\n+{\n+\tstruct bpf_tramp_skip *skip;\n+\n+\t/* struct_ops trampolines and arch_bpf_trampoline_size() have no image */\n+\tif (!im || !im-\u003eskips)\n+\t\treturn;\n+\tskip = \u0026im-\u003eskips[im-\u003enr_skips++];\n+\tskip-\u003eprog = prog;\n+\tskip-\u003enop = nop;\n+\tskip-\u003etarget = target;\n+}\n+\n struct bpf_trampoline {\n \t/* hlist for trampoline_key_table */\n \tstruct hlist_node hlist_key;\n@@ -1402,6 +1437,8 @@ struct bpf_trampoline {\n \tint progs_cnt[BPF_TRAMP_MAX];\n \t/* Executable image of trampoline */\n \tstruct bpf_tramp_image *cur_image;\n+\t/* Images not freed yet, cur_image and older ones still in use */\n+\tstruct list_head images;\n \t/* Used as temporary old image storage for multi_attach */\n \tstruct {\n \t\tstruct bpf_tramp_image *old_image;\n@@ -1770,6 +1807,7 @@ struct bpf_prog_aux {\n \tbool offload_requested; /* Program is bound and offloaded to the netdev. */\n \tbool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */\n \tbool attach_tracing_prog; /* true if tracing another tracing program */\n+\tbool tramp_linked; /* true if it was ever called from a trampoline */\n \tbool func_proto_unreliable;\n \tbool tail_call_reachable;\n \tbool xdp_has_frags;\ndiff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c\nindex 244a939b9d2da..96217b99399d5 100644\n--- a/kernel/bpf/syscall.c\n+++ b/kernel/bpf/syscall.c\n@@ -2448,6 +2448,21 @@ static void __bpf_prog_put_rcu(struct rcu_head *rcu)\n \tbpf_prog_free(aux-\u003eprog);\n }\n \n+/*\n+ * Progs called from a trampoline can also be reached by a task that was\n+ * preempted in the trampoline before the prog's enter helper took its RCU\n+ * read lock, wait for those first.\n+ */\n+static void __bpf_prog_put_rcu_tasks(struct rcu_head *rcu)\n+{\n+\tstruct bpf_prog *prog = container_of(rcu, struct bpf_prog_aux, rcu)-\u003eprog;\n+\n+\tif (prog-\u003esleepable)\n+\t\tcall_rcu_tasks_trace(rcu, __bpf_prog_put_rcu);\n+\telse\n+\t\tcall_rcu(rcu, __bpf_prog_put_rcu);\n+}\n+\n static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)\n {\n \tbpf_prog_kallsyms_del_all(prog);\n@@ -2461,7 +2476,9 @@ static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)\n \t\tbtf_put(prog-\u003eaux-\u003eattach_btf);\n \n \tif (deferred) {\n-\t\tif (prog-\u003esleepable)\n+\t\tif (IS_ENABLED(CONFIG_TASKS_RCU) \u0026\u0026 prog-\u003eaux-\u003etramp_linked)\n+\t\t\tcall_rcu_tasks(\u0026prog-\u003eaux-\u003ercu, __bpf_prog_put_rcu_tasks);\n+\t\telse if (prog-\u003esleepable)\n \t\t\tcall_rcu_tasks_trace(\u0026prog-\u003eaux-\u003ercu, __bpf_prog_put_rcu);\n \t\telse\n \t\t\tcall_rcu(\u0026prog-\u003eaux-\u003ercu, __bpf_prog_put_rcu);\ndiff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c\nindex 90b70ea0d3709..ea8e67a389194 100644\n--- a/kernel/bpf/trampoline.c\n+++ b/kernel/bpf/trampoline.c\n@@ -401,6 +401,7 @@ static struct bpf_trampoline *bpf_trampoline_lookup(u64 key, unsigned long ip)\n \thead = \u0026trampoline_ip_table[hash_64(tr-\u003eip, TRAMPOLINE_HASH_BITS)];\n \thlist_add_head(\u0026tr-\u003ehlist_ip, head);\n \trefcount_set(\u0026tr-\u003erefcnt, 1);\n+\tINIT_LIST_HEAD(\u0026tr-\u003eimages);\n \tfor (i = 0; i \u003c BPF_TRAMP_MAX; i++)\n \t\tINIT_HLIST_HEAD(\u0026tr-\u003eprogs_hlist[i]);\n out:\n@@ -565,15 +566,22 @@ static void bpf_tramp_image_free(struct bpf_tramp_image *im)\n \tarch_free_bpf_trampoline(im-\u003eimage, im-\u003esize);\n \tbpf_jit_uncharge_modmem(im-\u003esize);\n \tpercpu_ref_exit(\u0026im-\u003epcref);\n+\tkfree(im-\u003eskips);\n \tkfree_rcu(im, rcu);\n }\n \n static void __bpf_tramp_image_put_deferred(struct work_struct *work)\n {\n \tstruct bpf_tramp_image *im;\n+\tstruct bpf_trampoline *tr;\n \n \tim = container_of(work, struct bpf_tramp_image, work);\n+\ttr = im-\u003etr;\n+\ttrampoline_lock(tr);\n+\tlist_del(\u0026im-\u003elist);\n+\ttrampoline_unlock(tr);\n \tbpf_tramp_image_free(im);\n+\tbpf_trampoline_put(tr);\n }\n \n /* callback, fexit step 3 or fentry step 2 */\n@@ -601,7 +609,7 @@ static void __bpf_tramp_image_put_rcu_tasks(struct rcu_head *rcu)\n \tstruct bpf_tramp_image *im;\n \n \tim = container_of(rcu, struct bpf_tramp_image, rcu);\n-\tif (im-\u003eip_after_call)\n+\tif (im-\u003ecall_orig)\n \t\t/* the case of fmod_ret/fexit trampoline and CONFIG_PREEMPTION=y */\n \t\tpercpu_ref_kill(\u0026im-\u003epcref);\n \telse\n@@ -621,9 +629,9 @@ static void bpf_tramp_image_put(struct bpf_tramp_image *im)\n \t *\n \t * The trampoline is unreachable before bpf_tramp_image_put().\n \t *\n-\t * First, patch the trampoline to avoid calling into fexit progs.\n-\t * The progs will be freed even if the original function is still\n-\t * executing or sleeping.\n+\t * Progs are patched out of the image when they are detached, see\n+\t * bpf_trampoline_skip_prog(), so they can be freed even if a task is\n+\t * still in the image.\n \t * In case of CONFIG_PREEMPT=y use call_rcu_tasks() to wait on\n \t * first few asm instructions to execute and call into\n \t * __bpf_tramp_enter-\u003epercpu_ref_get.\n@@ -637,11 +645,7 @@ static void bpf_tramp_image_put(struct bpf_tramp_image *im)\n \t * percpu_ref_kill will be waiting for. Hence the first\n \t * call_rcu_tasks() is not necessary.\n \t */\n-\tif (im-\u003eip_after_call) {\n-\t\tint err = bpf_arch_text_poke(im-\u003eip_after_call, BPF_MOD_NOP,\n-\t\t\t\t\t     BPF_MOD_JUMP, NULL,\n-\t\t\t\t\t     im-\u003eip_epilogue);\n-\t\tWARN_ON(err);\n+\tif (im-\u003ecall_orig) {\n \t\tif (IS_ENABLED(CONFIG_TASKS_RCU))\n \t\t\tcall_rcu_tasks(\u0026im-\u003ercu, __bpf_tramp_image_put_rcu_tasks);\n \t\telse\n@@ -658,7 +662,7 @@ static void bpf_tramp_image_put(struct bpf_tramp_image *im)\n \tcall_rcu_tasks_trace(\u0026im-\u003ercu, __bpf_tramp_image_put_rcu_tasks);\n }\n \n-static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size)\n+static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size, int nr_progs)\n {\n \tstruct bpf_tramp_image *im;\n \tstruct bpf_ksym *ksym;\n@@ -669,6 +673,10 @@ static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size)\n \tif (!im)\n \t\tgoto out;\n \n+\tim-\u003eskips = kzalloc_objs(*im-\u003eskips, nr_progs);\n+\tif (!im-\u003eskips)\n+\t\tgoto out_free_im;\n+\n \terr = bpf_jit_charge_modmem(size);\n \tif (err)\n \t\tgoto out_free_im;\n@@ -695,6 +703,7 @@ static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size)\n out_uncharge:\n \tbpf_jit_uncharge_modmem(size);\n out_free_im:\n+\tkfree(im-\u003eskips);\n \tkfree(im);\n out:\n \treturn ERR_PTR(err);\n@@ -771,11 +780,12 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut\n \t\tgoto out;\n \t}\n \n-\tim = bpf_tramp_image_alloc(tr-\u003ekey, size);\n+\tim = bpf_tramp_image_alloc(tr-\u003ekey, size, total);\n \tif (IS_ERR(im)) {\n \t\terr = PTR_ERR(im);\n \t\tgoto out;\n \t}\n+\tim-\u003ecall_orig = tr-\u003eflags \u0026 BPF_TRAMP_F_CALL_ORIG;\n \n \terr = arch_prepare_bpf_trampoline(im, im-\u003eimage, im-\u003eimage + size,\n \t\t\t\t\t  \u0026tr-\u003efunc.model, tr-\u003eflags, tnodes,\n@@ -806,8 +816,14 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut\n #endif\n \n out_free:\n-\tif (err)\n+\tif (err) {\n \t\tbpf_tramp_image_free(im);\n+\t} else {\n+\t\t/* track the image until it is freed, for bpf_trampoline_skip_prog() */\n+\t\trefcount_inc(\u0026tr-\u003erefcnt);\n+\t\tim-\u003etr = tr;\n+\t\tlist_add(\u0026im-\u003elist, \u0026tr-\u003eimages);\n+\t}\n out:\n \t/* If any error happens, restore previous flags */\n \tif (err)\n@@ -907,6 +923,7 @@ static int bpf_trampoline_add_prog(struct bpf_trampoline *tr,\n \t}\n \n \thlist_add_head(\u0026node-\u003etramp_hlist, prog_list);\n+\tnode-\u003elink-\u003eprog-\u003eaux-\u003etramp_linked = true;\n \tif (kind == BPF_TRAMP_FSESSION) {\n \t\ttr-\u003eprogs_cnt[BPF_TRAMP_FENTRY]++;\n \t\tfexit = fsession_exit(node);\n@@ -920,6 +937,32 @@ static int bpf_trampoline_add_prog(struct bpf_trampoline *tr,\n \treturn 0;\n }\n \n+/*\n+ * prog was detached and can be freed, but tasks may still be running in images\n+ * that call it, sleeping in an earlier prog for example. They can be in any\n+ * image that is not freed yet, not only in cur_image, so patch all of them to\n+ * jump over prog.\n+ */\n+static void bpf_trampoline_skip_prog(struct bpf_trampoline *tr, struct bpf_prog *prog)\n+{\n+\tstruct bpf_tramp_image *im;\n+\tint i, err;\n+\n+\tlist_for_each_entry(im, \u0026tr-\u003eimages, list) {\n+\t\tfor (i = 0; i \u003c im-\u003enr_skips; i++) {\n+\t\t\tstruct bpf_tramp_skip *skip = \u0026im-\u003eskips[i];\n+\n+\t\t\tif (skip-\u003eprog != prog)\n+\t\t\t\tcontinue;\n+\t\t\terr = bpf_arch_text_poke(skip-\u003enop, BPF_MOD_NOP, BPF_MOD_JUMP,\n+\t\t\t\t\t\t NULL, skip-\u003etarget);\n+\t\t\tWARN_ON_ONCE(err);\n+\t\t\t/* not a nop anymore, and prog's address can be reused */\n+\t\t\tskip-\u003eprog = NULL;\n+\t\t}\n+\t}\n+}\n+\n static void bpf_trampoline_remove_prog(struct bpf_trampoline *tr,\n \t\t\t\t       struct bpf_tramp_node *node)\n {\n@@ -937,6 +980,7 @@ static void bpf_trampoline_remove_prog(struct bpf_trampoline *tr,\n \t}\n \thlist_del_init(\u0026node-\u003etramp_hlist);\n \ttr-\u003eprogs_cnt[kind]--;\n+\tbpf_trampoline_skip_prog(tr, node-\u003elink-\u003eprog);\n }\n \n static int __bpf_trampoline_link_prog(struct bpf_tramp_node *node,\n@@ -1245,11 +1289,8 @@ void bpf_trampoline_put(struct bpf_trampoline *tr)\n \t\tif (WARN_ON_ONCE(!hlist_empty(\u0026tr-\u003eprogs_hlist[i])))\n \t\t\tgoto out;\n \n-\t/* This code will be executed even when the last bpf_tramp_image\n-\t * is alive. All progs are detached from the trampoline and the\n-\t * trampoline image is patched with jmp into epilogue to skip\n-\t * fexit progs. The fentry-only trampoline will be freed via\n-\t * multiple rcu callbacks.\n+\t/* All progs are detached and the last image has been freed, images\n+\t * hold a reference on the trampoline until then.\n \t */\n \thlist_del(\u0026tr-\u003ehlist_key);\n \thlist_del(\u0026tr-\u003ehlist_ip);\ndiff --git a/tools/testing/selftests/bpf/prog_tests/bpf_mod_race.c b/tools/testing/selftests/bpf/prog_tests/bpf_mod_race.c\nindex ecc3d47919ad6..f8497e764beb5 100644\n--- a/tools/testing/selftests/bpf/prog_tests/bpf_mod_race.c\n+++ b/tools/testing/selftests/bpf/prog_tests/bpf_mod_race.c\n@@ -55,38 +55,6 @@ static void *load_module_thread(void *p)\n \treturn p;\n }\n \n-static int sys_userfaultfd(int flags)\n-{\n-\treturn syscall(__NR_userfaultfd, flags);\n-}\n-\n-static int test_setup_uffd(void *fault_addr)\n-{\n-\tstruct uffdio_register uffd_register = {};\n-\tstruct uffdio_api uffd_api = {};\n-\tint uffd;\n-\n-\tuffd = sys_userfaultfd(O_CLOEXEC);\n-\tif (uffd \u003c 0)\n-\t\treturn -errno;\n-\n-\tuffd_api.api = UFFD_API;\n-\tuffd_api.features = 0;\n-\tif (ioctl(uffd, UFFDIO_API, \u0026uffd_api)) {\n-\t\tclose(uffd);\n-\t\treturn -1;\n-\t}\n-\n-\tuffd_register.range.start = (unsigned long)fault_addr;\n-\tuffd_register.range.len = getpagesize();\n-\tuffd_register.mode = UFFDIO_REGISTER_MODE_MISSING;\n-\tif (ioctl(uffd, UFFDIO_REGISTER, \u0026uffd_register)) {\n-\t\tclose(uffd);\n-\t\treturn -1;\n-\t}\n-\treturn uffd;\n-}\n-\n static void test_bpf_mod_race_config(const struct test_config *config)\n {\n \tvoid *fault_addr, *skel_fail;\n@@ -117,7 +85,7 @@ static void test_bpf_mod_race_config(const struct test_config *config)\n \tif (!ASSERT_OK(bpf_mod_race__attach(skel), \"bpf_mod_kfunc_race__attach\"))\n \t\tgoto end_destroy;\n \n-\tuffd = test_setup_uffd(fault_addr);\n+\tuffd = uffd_block_page(fault_addr);\n \tif (!ASSERT_GE(uffd, 0, \"userfaultfd open + register address\"))\n \t\tgoto end_destroy;\n \ndiff --git a/tools/testing/selftests/bpf/prog_tests/tramp_prog_detach.c b/tools/testing/selftests/bpf/prog_tests/tramp_prog_detach.c\nnew file mode 100644\nindex 0000000000000..1eb0d72376055\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/prog_tests/tramp_prog_detach.c\n@@ -0,0 +1,188 @@\n+// SPDX-License-Identifier: GPL-2.0\n+#include \u003ctest_progs.h\u003e\n+#include \u003cpthread.h\u003e\n+#include \u003cpoll.h\u003e\n+#include \u003csys/mman.h\u003e\n+#include \u003clinux/userfaultfd.h\u003e\n+#include \"tramp_prog_detach.skel.h\"\n+#include \"testing_helpers.h\"\n+\n+/*\n+ * Detach and free progs while a task sleeps in the prog that runs before them\n+ * in the same trampoline image, then let that task continue through the\n+ * image. It must not call into the freed progs.\n+ *\n+ * The task is held in a sleepable prog with userfaultfd, like bpf_mod_race\n+ * does.\n+ */\n+\n+static struct bpf_program *pick_prog(struct tramp_prog_detach *skel,\n+\t\t\t\t     bool fexit, bool sleepable)\n+{\n+\tif (fexit)\n+\t\treturn sleepable ? skel-\u003eprogs.fexit_sleepable :\n+\t\t\t\t   skel-\u003eprogs.fexit_victim;\n+\treturn sleepable ? skel-\u003eprogs.fentry_sleepable :\n+\t\t\t   skel-\u003eprogs.fentry_victim;\n+}\n+\n+static struct bpf_program *sleepable_prog;\n+\n+static void *run_sleepable(void *arg)\n+{\n+\tLIBBPF_OPTS(bpf_test_run_opts, topts);\n+\n+\t/* calls bpf_fentry_test1() */\n+\treturn (void *)(long)bpf_prog_test_run_opts(bpf_program__fd(sleepable_prog),\n+\t\t\t\t\t\t    \u0026topts);\n+}\n+\n+static struct tramp_prog_detach *load_one(bool fexit, bool sleepable)\n+{\n+\tstruct tramp_prog_detach *skel;\n+\tint err;\n+\n+\tskel = tramp_prog_detach__open();\n+\tif (!ASSERT_OK_PTR(skel, \"open\"))\n+\t\treturn NULL;\n+\n+\tbpf_program__set_autoload(pick_prog(skel, fexit, sleepable), true);\n+\terr = tramp_prog_detach__load(skel);\n+\tif (!ASSERT_OK(err, \"load\"))\n+\t\tgoto err;\n+\tskel-\u003ebss-\u003epid = getpid();\n+\terr = tramp_prog_detach__attach(skel);\n+\tif (!ASSERT_OK(err, \"attach\"))\n+\t\tgoto err;\n+\treturn skel;\n+err:\n+\ttramp_prog_detach__destroy(skel);\n+\treturn NULL;\n+}\n+\n+/* The .bss map of a destroyed skeleton goes away when its prog is freed */\n+static bool wait_for_map_free(__u32 map_id)\n+{\n+\tint i, fd;\n+\n+\tfor (i = 0; i \u003c 100; i++) {\n+\t\tfd = bpf_map_get_fd_by_id(map_id);\n+\t\tif (fd \u003c 0)\n+\t\t\treturn true;\n+\t\tclose(fd);\n+\t\tusleep(100 * 1000);\n+\t}\n+\treturn false;\n+}\n+\n+static void test_detach(bool sleepable_fexit, bool victim_fexit)\n+{\n+\tstruct tramp_prog_detach *sleepable = NULL, *victims[2] = {};\n+\tstruct pollfd pfd = { .events = POLLIN };\n+\tstruct uffdio_copy uffd_copy = {};\n+\tstruct bpf_map_info map_info = {};\n+\t__u32 map_info_len = sizeof(map_info);\n+\tstruct uffd_msg uffd_msg;\n+\tvoid *fault_page, *src_page = MAP_FAILED;\n+\tlong page_size = getpagesize();\n+\tbool started = false;\n+\tvoid *thread_ret;\n+\tpthread_t thread;\n+\tint i, uffd = -1;\n+\n+\tfault_page = mmap(NULL, page_size, PROT_READ | PROT_WRITE,\n+\t\t\t  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);\n+\tif (!ASSERT_NEQ(fault_page, MAP_FAILED, \"mmap fault_page\"))\n+\t\treturn;\n+\tsrc_page = mmap(NULL, page_size, PROT_READ | PROT_WRITE,\n+\t\t\tMAP_PRIVATE | MAP_ANONYMOUS, -1, 0);\n+\tif (!ASSERT_NEQ(src_page, MAP_FAILED, \"mmap src_page\"))\n+\t\tgoto out;\n+\n+\t/* The most recently attached prog runs first */\n+\tfor (i = 0; i \u003c ARRAY_SIZE(victims); i++) {\n+\t\tvictims[i] = load_one(victim_fexit, false);\n+\t\tif (!victims[i])\n+\t\t\tgoto out;\n+\t}\n+\tsleepable = load_one(sleepable_fexit, true);\n+\tif (!sleepable)\n+\t\tgoto out;\n+\tsleepable_prog = pick_prog(sleepable, sleepable_fexit, true);\n+\n+\t/* Not armed yet so this doesn't block, make sure sleepable runs first */\n+\tif (!ASSERT_OK((long)run_sleepable(NULL), \"dry run\"))\n+\t\tgoto out;\n+\tfor (i = 0; i \u003c ARRAY_SIZE(victims); i++)\n+\t\tif (!ASSERT_LT(sleepable-\u003ebss-\u003ets, victims[i]-\u003ebss-\u003ets, \"prog order\"))\n+\t\t\tgoto out;\n+\n+\tuffd = uffd_block_page(fault_page);\n+\tif (!ASSERT_GE(uffd, 0, \"userfaultfd open + register address\"))\n+\t\tgoto out;\n+\tsleepable-\u003ebss-\u003efault_addr = fault_page;\n+\n+\tif (!ASSERT_OK(pthread_create(\u0026thread, NULL, run_sleepable, NULL),\n+\t\t       \"pthread_create\"))\n+\t\tgoto out;\n+\tstarted = true;\n+\n+\t/* Wait for the thread to sleep in bpf_copy_from_user() */\n+\tpfd.fd = uffd;\n+\tif (!ASSERT_EQ(poll(\u0026pfd, 1, 10000), 1, \"poll uffd\"))\n+\t\tgoto out;\n+\tif (!ASSERT_EQ(read(uffd, \u0026uffd_msg, sizeof(uffd_msg)), sizeof(uffd_msg),\n+\t\t       \"read uffd\"))\n+\t\tgoto out;\n+\tif (!ASSERT_EQ(uffd_msg.event, UFFD_EVENT_PAGEFAULT, \"uffd pagefault\"))\n+\t\tgoto out;\n+\n+\t/*\n+\t * Detach and unload the victim progs and wait for them to be freed.\n+\t * After the first one, the task is in an image that isn't the\n+\t * trampoline's current one anymore.\n+\t */\n+\tfor (i = 0; i \u003c ARRAY_SIZE(victims); i++) {\n+\t\tif (!ASSERT_OK(bpf_map_get_info_by_fd(bpf_map__fd(victims[i]-\u003emaps.bss),\n+\t\t\t\t\t\t      \u0026map_info, \u0026map_info_len),\n+\t\t\t       \"victim bss info\"))\n+\t\t\tgoto out;\n+\t\ttramp_prog_detach__destroy(victims[i]);\n+\t\tvictims[i] = NULL;\n+\t\tif (!ASSERT_TRUE(wait_for_map_free(map_info.id), \"victim freed\"))\n+\t\t\tgoto out;\n+\t}\n+\n+out:\n+\t/* Let the thread proceed with the rest of the trampoline */\n+\tif (uffd \u003e= 0) {\n+\t\tuffd_copy.dst = (unsigned long)fault_page;\n+\t\tuffd_copy.src = (unsigned long)src_page;\n+\t\tuffd_copy.len = page_size;\n+\t\tASSERT_OK(ioctl(uffd, UFFDIO_COPY, \u0026uffd_copy), \"uffd copy\");\n+\t\tclose(uffd);\n+\t}\n+\tif (started \u0026\u0026\n+\t    ASSERT_OK(pthread_join(thread, \u0026thread_ret), \"pthread_join\"))\n+\t\tASSERT_NULL(thread_ret, \"blocking run\");\n+\n+\tfor (i = 0; i \u003c ARRAY_SIZE(victims); i++)\n+\t\ttramp_prog_detach__destroy(victims[i]);\n+\ttramp_prog_detach__destroy(sleepable);\n+\tif (src_page != MAP_FAILED)\n+\t\tmunmap(src_page, page_size);\n+\tmunmap(fault_page, page_size);\n+}\n+\n+void serial_test_tramp_prog_detach(void)\n+{\n+\t/* a task sleeping before the original function is called */\n+\tif (test__start_subtest(\"fentry\"))\n+\t\ttest_detach(false, false);\n+\t/* a task sleeping after the original function returned */\n+\tif (test__start_subtest(\"fexit\"))\n+\t\ttest_detach(true, true);\n+\t/* the original function runs in between and must still be called */\n+\tif (test__start_subtest(\"fentry_fexit\"))\n+\t\ttest_detach(false, true);\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/tramp_prog_detach.c b/tools/testing/selftests/bpf/progs/tramp_prog_detach.c\nnew file mode 100644\nindex 0000000000000..507d372167ecb\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/tramp_prog_detach.c\n@@ -0,0 +1,56 @@\n+// SPDX-License-Identifier: GPL-2.0\n+#include \"vmlinux.h\"\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \u003cbpf/bpf_tracing.h\u003e\n+\n+char _license[] SEC(\"license\") = \"GPL\";\n+\n+int pid;\n+void *fault_addr;\n+__u64 ts;\n+\n+static int do_sleepable(void)\n+{\n+\tchar dst;\n+\n+\tif (bpf_get_current_pid_tgid() \u003e\u003e 32 != pid)\n+\t\treturn 0;\n+\n+\tts = bpf_ktime_get_ns();\n+\t/* blocks for as long as user space wants when fault_addr is armed */\n+\tbpf_copy_from_user(\u0026dst, sizeof(dst), fault_addr);\n+\treturn 0;\n+}\n+\n+static int do_victim(void)\n+{\n+\tif (bpf_get_current_pid_tgid() \u003e\u003e 32 != pid)\n+\t\treturn 0;\n+\n+\tts = bpf_ktime_get_ns();\n+\treturn 0;\n+}\n+\n+SEC(\"?fentry.s/bpf_fentry_test1\")\n+int BPF_PROG(fentry_sleepable, int a)\n+{\n+\treturn do_sleepable();\n+}\n+\n+SEC(\"?fentry/bpf_fentry_test1\")\n+int BPF_PROG(fentry_victim, int a)\n+{\n+\treturn do_victim();\n+}\n+\n+SEC(\"?fexit.s/bpf_fentry_test1\")\n+int BPF_PROG(fexit_sleepable, int a, int ret)\n+{\n+\treturn do_sleepable();\n+}\n+\n+SEC(\"?fexit/bpf_fentry_test1\")\n+int BPF_PROG(fexit_victim, int a, int ret)\n+{\n+\treturn do_victim();\n+}\ndiff --git a/tools/testing/selftests/bpf/testing_helpers.c b/tools/testing/selftests/bpf/testing_helpers.c\nindex c970e7793dfcb..9f672d0c9dda2 100644\n--- a/tools/testing/selftests/bpf/testing_helpers.c\n+++ b/tools/testing/selftests/bpf/testing_helpers.c\n@@ -13,6 +13,7 @@\n #include \"test_progs.h\"\n #include \"testing_helpers.h\"\n #include \u003clinux/membarrier.h\u003e\n+#include \u003clinux/userfaultfd.h\u003e\n \n int parse_num_list(const char *s, bool **num_set, int *num_set_len)\n {\n@@ -459,6 +460,33 @@ int kern_sync_rcu(void)\n \treturn syscall(__NR_membarrier, MEMBARRIER_CMD_SHARED, 0, 0);\n }\n \n+int uffd_block_page(void *fault_addr)\n+{\n+\tstruct uffdio_register uffd_register = {};\n+\tstruct uffdio_api uffd_api = {};\n+\tint uffd;\n+\n+\tuffd = syscall(__NR_userfaultfd, O_CLOEXEC);\n+\tif (uffd \u003c 0)\n+\t\treturn -errno;\n+\n+\tuffd_api.api = UFFD_API;\n+\tuffd_api.features = 0;\n+\tif (ioctl(uffd, UFFDIO_API, \u0026uffd_api)) {\n+\t\tclose(uffd);\n+\t\treturn -1;\n+\t}\n+\n+\tuffd_register.range.start = (unsigned long)fault_addr;\n+\tuffd_register.range.len = getpagesize();\n+\tuffd_register.mode = UFFDIO_REGISTER_MODE_MISSING;\n+\tif (ioctl(uffd, UFFDIO_REGISTER, \u0026uffd_register)) {\n+\t\tclose(uffd);\n+\t\treturn -1;\n+\t}\n+\treturn uffd;\n+}\n+\n int get_xlated_program(int fd_prog, struct bpf_insn **buf, __u32 *cnt)\n {\n \t__u32 buf_element_size = sizeof(struct bpf_insn);\ndiff --git a/tools/testing/selftests/bpf/testing_helpers.h b/tools/testing/selftests/bpf/testing_helpers.h\nindex 2edc6fb7fc521..1f83b6080716f 100644\n--- a/tools/testing/selftests/bpf/testing_helpers.h\n+++ b/tools/testing/selftests/bpf/testing_helpers.h\n@@ -36,6 +36,8 @@ __u64 read_perf_max_sample_freq(void);\n int load_bpf_testmod(bool verbose);\n int unload_bpf_testmod(bool verbose);\n int kern_sync_rcu(void);\n+/* returns a userfaultfd that makes accesses to fault_addr's page block */\n+int uffd_block_page(void *fault_addr);\n int finit_module(int fd, const char *param_values, int flags);\n int delete_module(const char *name, int flags);\n int load_module(const char *path, bool verbose);\n"
}

2/1 2026/09/27 09:12 agent patch-evaluator
1m Model:
core

Args:
null

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "BPF_JIT",
    "DYNAMIC_FTRACE_WITH_DIRECT_CALLS",
    "TASKS_RCU"
  ],
  "FocusSymbols": [
    "bpf_trampoline_link_prog",
    "bpf_trampoline_unlink_prog",
    "bpf_trampoline_update",
    "bpf_trampoline_skip_prog",
    "bpf_trampoline_remove_prog",
    "bpf_tramp_image_put",
    "arch_prepare_bpf_trampoline",
    "bpf_trampoline_put"
  ],
  "Reasoning": "The patch modifies BPF trampoline management and x86 JIT generation to prevent use-after-free/race conditions when detaching BPF programs while tasks are executing inside older trampoline images. Instead of patching only at the epilogue, it reserves NOPs before each program call and dynamically text-pokes them into skip jumps when programs detach, tracking images on a per-trampoline list and adjusting RCU grace periods (call_rcu_tasks). This code is fully reachable on amd64 via standard BPF syscalls (attaching and detaching fentry/fexit/fmod_ret/fsession programs) and warrants fuzzing due to the concurrency, live kernel text patching (bpf_arch_text_poke), and reference counting involved.",
  "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 95aa626b973aa62514c088b7f1ab61c19b68b7ba
Author: syz-cluster <triage@syzkaller.com>
Date:   Sun Sep 27 09:12:39 2026 +0000

    syz-cluster: applied patch under review

diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index c5f55d6161fee..bd73179c00eca 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -2418,10 +2418,11 @@ bool bpf_jit_supports_subprog_tailcalls(void)
 	return true;
 }
 
-static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *node,
-			    int bargs_off, int retval_off, int run_ctx_off,
-			    bool save_ret)
+static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_image *im,
+			    struct bpf_tramp_node *node, int bargs_off,
+			    int retval_off, int run_ctx_off, bool save_ret)
 {
+	void *skip;
 	__le32 *branch;
 	u64 enter_prog;
 	u64 exit_prog;
@@ -2431,6 +2432,10 @@ static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *node,
 	enter_prog = (u64)bpf_trampoline_enter(p);
 	exit_prog = (u64)bpf_trampoline_exit(p);
 
+	/* nop, patched to skip this prog when it is detached */
+	skip = ctx->ro_image + ctx->idx;
+	emit(A64_NOP, ctx);
+
 	if (node->cookie == 0) {
 		/* if cookie is zero, one instruction is enough to store it */
 		emit(A64_STR64I(A64_ZR, A64_SP, run_ctx_off + cookie_off), ctx);
@@ -2483,11 +2488,13 @@ static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *node,
 	emit(A64_ADD_I(1, A64_R(2), A64_SP, run_ctx_off), ctx);
 
 	emit_call(exit_prog, ctx);
+
+	bpf_tramp_image_add_skip(im, p, skip, ctx->ro_image + ctx->idx);
 }
 
-static void invoke_bpf_mod_ret(struct jit_ctx *ctx, struct bpf_tramp_nodes *tn,
-			       int bargs_off, int retval_off, int run_ctx_off,
-			       __le32 **branches)
+static void invoke_bpf_mod_ret(struct jit_ctx *ctx, struct bpf_tramp_image *im,
+			       struct bpf_tramp_nodes *tn, int bargs_off,
+			       int retval_off, int run_ctx_off, __le32 **branches)
 {
 	int i;
 
@@ -2496,7 +2503,7 @@ static void invoke_bpf_mod_ret(struct jit_ctx *ctx, struct bpf_tramp_nodes *tn,
 	 */
 	emit(A64_STR64I(A64_ZR, A64_SP, retval_off), ctx);
 	for (i = 0; i < tn->nr_nodes; i++) {
-		invoke_bpf_prog(ctx, tn->nodes[i], bargs_off, retval_off,
+		invoke_bpf_prog(ctx, im, tn->nodes[i], bargs_off, retval_off,
 				run_ctx_off, true);
 		/* if (*(u64 *)(sp + retval_off) !=  0)
 		 *	goto do_fexit;
@@ -2882,7 +2889,7 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
 			store_func_meta(ctx, meta, func_meta_off);
 			cookie_bargs_off--;
 		}
-		invoke_bpf_prog(ctx, fentry->nodes[i], bargs_off,
+		invoke_bpf_prog(ctx, im, fentry->nodes[i], bargs_off,
 				retval_off, run_ctx_off,
 				flags & BPF_TRAMP_F_RET_FENTRY_RET);
 	}
@@ -2893,7 +2900,7 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
 		if (!branches)
 			return -ENOMEM;
 
-		invoke_bpf_mod_ret(ctx, fmod_ret, bargs_off, retval_off,
+		invoke_bpf_mod_ret(ctx, im, fmod_ret, bargs_off, retval_off,
 				   run_ctx_off, branches);
 	}
 
@@ -2906,9 +2913,6 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
 		emit(A64_RET(A64_R(10)), ctx);
 		/* store return value */
 		emit(A64_STR64I(A64_R(0), A64_SP, retval_off), ctx);
-		/* reserve a nop for bpf_tramp_image_put */
-		im->ip_after_call = ctx->ro_image + ctx->idx;
-		emit(A64_NOP, ctx);
 	}
 
 	/* update the branches saved in invoke_bpf_mod_ret with cbnz */
@@ -2930,12 +2934,11 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
 			store_func_meta(ctx, meta, func_meta_off);
 			cookie_bargs_off--;
 		}
-		invoke_bpf_prog(ctx, fexit->nodes[i], bargs_off, retval_off,
+		invoke_bpf_prog(ctx, im, fexit->nodes[i], bargs_off, retval_off,
 				run_ctx_off, false);
 	}
 
 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
-		im->ip_epilogue = ctx->ro_image + ctx->idx;
 		/* for the first pass, assume the worst case */
 		if (!ctx->image)
 			ctx->idx += 4;
@@ -2994,7 +2997,7 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
 		.image = NULL,
 		.idx = 0,
 	};
-	struct bpf_tramp_image im;
+	struct bpf_tramp_image im = {};
 	struct arg_aux aaux;
 	int ret;
 
diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index 4da2789009383..df8abfa055d1c 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -1696,13 +1696,19 @@ static void restore_stk_args(struct jit_ctx *ctx, int nr_stk_args, int args_off,
 	}
 }
 
-static int invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *n,
-			   int args_off, int retval_off, int run_ctx_off, bool save_ret)
+static int invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_image *im,
+			   struct bpf_tramp_node *n, int args_off, int retval_off,
+			   int run_ctx_off, bool save_ret)
 {
-	int ret;
+	int i, ret;
 	u32 *branch;
 	struct bpf_prog *p = n->link->prog;
 	int cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
+	void *skip = ctx->ro_image + ctx->idx;
+
+	/* nops for move_imm+jirl, patched to skip this prog when it is detached */
+	for (i = 0; i < LOONGARCH_LONG_JUMP_NINSNS; i++)
+		emit_insn(ctx, nop);
 
 	if (n->cookie)
 		emit_store_stack_imm64(ctx, LOONGARCH_GPR_T1,
@@ -1755,13 +1761,17 @@ static int invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *n,
 	/* arg3: &run_ctx */
 	emit_insn(ctx, addid, LOONGARCH_GPR_A2, LOONGARCH_GPR_FP, -run_ctx_off);
 	ret = emit_call(ctx, (const u64)bpf_trampoline_exit(p));
+	if (ret)
+		return ret;
 
-	return ret;
+	bpf_tramp_image_add_skip(im, p, skip, ctx->ro_image + ctx->idx);
+	return 0;
 }
 
-static int invoke_bpf(struct jit_ctx *ctx, struct bpf_tramp_nodes *tn,
-		      int args_off, int retval_off, int run_ctx_off,
-		      int func_meta_off, bool save_ret, u64 func_meta, int cookie_off)
+static int invoke_bpf(struct jit_ctx *ctx, struct bpf_tramp_image *im,
+		      struct bpf_tramp_nodes *tn, int args_off, int retval_off,
+		      int run_ctx_off, int func_meta_off, bool save_ret,
+		      u64 func_meta, int cookie_off)
 {
 	int i, cur_cookie = (cookie_off - args_off) / 8;
 
@@ -1774,7 +1784,8 @@ static int invoke_bpf(struct jit_ctx *ctx, struct bpf_tramp_nodes *tn,
 			emit_store_stack_imm64(ctx, LOONGARCH_GPR_T1, -func_meta_off, meta);
 			cur_cookie--;
 		}
-		err = invoke_bpf_prog(ctx, tn->nodes[i], args_off, retval_off, run_ctx_off, save_ret);
+		err = invoke_bpf_prog(ctx, im, tn->nodes[i], args_off, retval_off,
+				      run_ctx_off, save_ret);
 		if (err)
 			return err;
 	}
@@ -2017,7 +2028,7 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i
 	}
 
 	if (fentry->nr_nodes) {
-		ret = invoke_bpf(ctx, fentry, args_off, retval_off, run_ctx_off, func_meta_off,
+		ret = invoke_bpf(ctx, im, fentry, args_off, retval_off, run_ctx_off, func_meta_off,
 				 flags & BPF_TRAMP_F_RET_FENTRY_RET, func_meta, cookie_off);
 		if (ret)
 			return ret;
@@ -2029,7 +2040,7 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i
 
 		emit_insn(ctx, std, LOONGARCH_GPR_ZERO, LOONGARCH_GPR_FP, -retval_off);
 		for (i = 0; i < fmod_ret->nr_nodes; i++) {
-			ret = invoke_bpf_prog(ctx, fmod_ret->nodes[i],
+			ret = invoke_bpf_prog(ctx, im, fmod_ret->nodes[i],
 					      args_off, retval_off, run_ctx_off, true);
 			if (ret)
 				goto out;
@@ -2051,10 +2062,6 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i
 			goto out;
 		emit_insn(ctx, std, LOONGARCH_GPR_A0, LOONGARCH_GPR_FP, -retval_off);
 		emit_insn(ctx, std, regmap[BPF_REG_0], LOONGARCH_GPR_FP, -(retval_off - 8));
-		im->ip_after_call = ctx->ro_image + ctx->idx;
-		/* Reserve space for the move_imm + jirl instruction */
-		for (i = 0; i < LOONGARCH_LONG_JUMP_NINSNS; i++)
-			emit_insn(ctx, nop);
 	}
 
 	for (i = 0; ctx->image && i < fmod_ret->nr_nodes; i++) {
@@ -2068,14 +2075,13 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i
 		emit_store_stack_imm64(ctx, LOONGARCH_GPR_T1, -func_meta_off, func_meta);
 
 	if (fexit->nr_nodes) {
-		ret = invoke_bpf(ctx, fexit, args_off, retval_off, run_ctx_off,
+		ret = invoke_bpf(ctx, im, fexit, args_off, retval_off, run_ctx_off,
 				 func_meta_off, false, func_meta, cookie_off);
 		if (ret)
 			goto out;
 	}
 
 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
-		im->ip_epilogue = ctx->ro_image + ctx->idx;
 		move_addr(ctx, LOONGARCH_GPR_A0, (const u64)im);
 		ret = emit_call(ctx, (const u64)__bpf_tramp_exit);
 		if (ret)
@@ -2177,11 +2183,8 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
 			     struct bpf_tramp_nodes *tnodes, void *func_addr)
 {
 	int ret;
-	struct jit_ctx ctx;
-	struct bpf_tramp_image im;
-
-	ctx.image = NULL;
-	ctx.idx = 0;
+	struct jit_ctx ctx = {};
+	struct bpf_tramp_image im = {};
 
 	ret = __arch_prepare_bpf_trampoline(&ctx, &im, m, tnodes, func_addr, flags);
 
diff --git a/arch/powerpc/net/bpf_jit_comp.c b/arch/powerpc/net/bpf_jit_comp.c
index 7b07b43575f11..7a612688e7a20 100644
--- a/arch/powerpc/net/bpf_jit_comp.c
+++ b/arch/powerpc/net/bpf_jit_comp.c
@@ -602,14 +602,18 @@ int arch_protect_bpf_trampoline(void *image, unsigned int size)
 }
 
 static int invoke_bpf_prog(u32 *image, u32 *ro_image, struct codegen_context *ctx,
-			   struct bpf_tramp_node *n, int regs_off, int retval_off,
-			   int run_ctx_off, bool save_ret)
+			   struct bpf_tramp_image *im, struct bpf_tramp_node *n,
+			   int regs_off, int retval_off, int run_ctx_off, bool save_ret)
 {
 	struct bpf_prog *p = n->link->prog;
 	ppc_inst_t branch_insn;
-	u32 jmp_idx;
+	u32 jmp_idx, skip_idx;
 	int ret = 0;
 
+	/* nop, patched to skip this prog when it is detached */
+	skip_idx = ctx->idx;
+	EMIT(PPC_RAW_NOP());
+
 	/* Save cookie */
 	if (IS_ENABLED(CONFIG_PPC64)) {
 		PPC_LI64(_R3, n->cookie);
@@ -679,13 +683,17 @@ static int invoke_bpf_prog(u32 *image, u32 *ro_image, struct codegen_context *ct
 	EMIT(PPC_RAW_ADDI(_R5, _R1, run_ctx_off));
 	ret = bpf_jit_emit_func_call_rel(image, ro_image, ctx,
 					 (unsigned long)bpf_trampoline_exit(p));
+	if (ret)
+		return ret;
 
-	return ret;
+	if (ro_image) /* image is NULL for dummy pass */
+		bpf_tramp_image_add_skip(im, p, &ro_image[skip_idx], &ro_image[ctx->idx]);
+	return 0;
 }
 
 static int invoke_bpf_mod_ret(u32 *image, u32 *ro_image, struct codegen_context *ctx,
-			      struct bpf_tramp_nodes *tn, int regs_off, int retval_off,
-			      int run_ctx_off, u32 *branches)
+			      struct bpf_tramp_image *im, struct bpf_tramp_nodes *tn,
+			      int regs_off, int retval_off, int run_ctx_off, u32 *branches)
 {
 	int i;
 
@@ -696,8 +704,8 @@ static int invoke_bpf_mod_ret(u32 *image, u32 *ro_image, struct codegen_context
 	EMIT(PPC_RAW_LI(_R3, 0));
 	EMIT(PPC_RAW_STL(_R3, _R1, retval_off));
 	for (i = 0; i < tn->nr_nodes; i++) {
-		if (invoke_bpf_prog(image, ro_image, ctx, tn->nodes[i], regs_off, retval_off,
-				    run_ctx_off, true))
+		if (invoke_bpf_prog(image, ro_image, ctx, im, tn->nodes[i], regs_off,
+				    retval_off, run_ctx_off, true))
 			return -EINVAL;
 
 		/*
@@ -1043,8 +1051,8 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 			cookie_ctx_off--;
 		}
 
-		if (invoke_bpf_prog(image, ro_image, ctx, fentry->nodes[i], regs_off, retval_off,
-				    run_ctx_off, flags & BPF_TRAMP_F_RET_FENTRY_RET))
+		if (invoke_bpf_prog(image, ro_image, ctx, im, fentry->nodes[i], regs_off,
+				    retval_off, run_ctx_off, flags & BPF_TRAMP_F_RET_FENTRY_RET))
 			return -EINVAL;
 	}
 
@@ -1053,7 +1061,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 		if (!branches)
 			return -ENOMEM;
 
-		if (invoke_bpf_mod_ret(image, ro_image, ctx, fmod_ret, regs_off, retval_off,
+		if (invoke_bpf_mod_ret(image, ro_image, ctx, im, fmod_ret, regs_off, retval_off,
 				       run_ctx_off, branches)) {
 			ret = -EINVAL;
 			goto cleanup;
@@ -1090,11 +1098,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 		/* Restore updated tail_call_cnt */
 		if (flags & BPF_TRAMP_F_TAIL_CALL_CTX)
 			bpf_trampoline_restore_tail_call_cnt(image, ctx, bpf_frame_size, r4_off);
-
-		/* Reserve space to patch branch instruction to skip fexit progs */
-		if (ro_image) /* image is NULL for dummy pass */
-			im->ip_after_call = &((u32 *)ro_image)[ctx->idx];
-		EMIT(PPC_RAW_NOP());
 	}
 
 	/* Update branches saved in invoke_bpf_mod_ret with address of do_fexit */
@@ -1123,16 +1126,14 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 			cookie_ctx_off--;
 		}
 
-		if (invoke_bpf_prog(image, ro_image, ctx, fexit->nodes[i], regs_off, retval_off,
-				    run_ctx_off, false)) {
+		if (invoke_bpf_prog(image, ro_image, ctx, im, fexit->nodes[i], regs_off,
+				    retval_off, run_ctx_off, false)) {
 			ret = -EINVAL;
 			goto cleanup;
 		}
 	}
 
 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
-		if (ro_image) /* image is NULL for dummy pass */
-			im->ip_epilogue = &((u32 *)ro_image)[ctx->idx];
 		PPC_LI_ADDR(_R3, im);
 		ret = bpf_jit_emit_func_call_rel(image, ro_image, ctx,
 						 (unsigned long)__bpf_tramp_exit);
@@ -1192,7 +1193,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
 			     struct bpf_tramp_nodes *tnodes, void *func_addr)
 {
-	struct bpf_tramp_image im;
+	struct bpf_tramp_image im = {};
 	int ret;
 
 	ret = __arch_prepare_bpf_trampoline(&im, NULL, NULL, NULL, m, flags, tnodes, func_addr);
@@ -1320,7 +1321,7 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
 
 	/*
 	 * If we are not poking at bpf prog entry, then we are simply patching in/out
-	 * an unconditional branch instruction at im->ip_after_call
+	 * an unconditional branch instruction in a trampoline image
 	 */
 	if (offset) {
 		if (old_t == BPF_MOD_CALL || new_t == BPF_MOD_CALL) {
diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c
index 151031e97a243..558e02dc2fe39 100644
--- a/arch/riscv/net/bpf_jit_comp64.c
+++ b/arch/riscv/net/bpf_jit_comp64.c
@@ -904,12 +904,18 @@ static void emit_store_stack_imm64(u8 reg, int stack_off, u64 imm64,
 	emit_sd(RV_REG_FP, stack_off, reg, ctx);
 }
 
-static int invoke_bpf_prog(struct bpf_tramp_node *node, int args_off, int retval_off,
-			   int run_ctx_off, bool save_ret, struct rv_jit_context *ctx)
+static int invoke_bpf_prog(struct bpf_tramp_image *im, struct bpf_tramp_node *node,
+			   int args_off, int retval_off, int run_ctx_off, bool save_ret,
+			   struct rv_jit_context *ctx)
 {
 	int ret, branch_off;
 	struct bpf_prog *p = node->link->prog;
 	int cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
+	void *skip = ctx->ro_insns + ctx->ninsns;
+
+	/* 2 nops for auipc+jalr, patched to skip this prog when it is detached */
+	emit(rv_nop(), ctx);
+	emit(rv_nop(), ctx);
 
 	if (node->cookie)
 		emit_store_stack_imm64(RV_REG_T1, -run_ctx_off + cookie_off, node->cookie, ctx);
@@ -962,13 +968,17 @@ static int invoke_bpf_prog(struct bpf_tramp_node *node, int args_off, int retval
 	/* arg3: &run_ctx */
 	emit_addi(RV_REG_A2, RV_REG_FP, -run_ctx_off, ctx);
 	ret = emit_call((const u64)bpf_trampoline_exit(p), true, ctx);
+	if (ret)
+		return ret;
 
-	return ret;
+	bpf_tramp_image_add_skip(im, p, skip, ctx->ro_insns + ctx->ninsns);
+	return 0;
 }
 
-static int invoke_bpf(struct bpf_tramp_nodes *tn, int args_off, int retval_off,
-		      int run_ctx_off, int func_meta_off, bool save_ret, u64 func_meta,
-		      int cookie_off, struct rv_jit_context *ctx)
+static int invoke_bpf(struct bpf_tramp_image *im, struct bpf_tramp_nodes *tn,
+		      int args_off, int retval_off, int run_ctx_off, int func_meta_off,
+		      bool save_ret, u64 func_meta, int cookie_off,
+		      struct rv_jit_context *ctx)
 {
 	int i, cur_cookie = (cookie_off - args_off) / 8;
 
@@ -981,8 +991,8 @@ static int invoke_bpf(struct bpf_tramp_nodes *tn, int args_off, int retval_off,
 			emit_store_stack_imm64(RV_REG_T1, -func_meta_off, meta, ctx);
 			cur_cookie--;
 		}
-		err = invoke_bpf_prog(tn->nodes[i], args_off, retval_off, run_ctx_off,
-				      save_ret, ctx);
+		err = invoke_bpf_prog(im, tn->nodes[i], args_off, retval_off,
+				      run_ctx_off, save_ret, ctx);
 		if (err)
 			return err;
 	}
@@ -1170,7 +1180,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 	}
 
 	if (fentry->nr_nodes) {
-		ret = invoke_bpf(fentry, args_off, retval_off, run_ctx_off, func_meta_off,
+		ret = invoke_bpf(im, fentry, args_off, retval_off, run_ctx_off, func_meta_off,
 				 flags & BPF_TRAMP_F_RET_FENTRY_RET, func_meta, cookie_off, ctx);
 		if (ret)
 			return ret;
@@ -1184,7 +1194,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 		/* cleanup to avoid garbage return value confusion */
 		emit_sd(RV_REG_FP, -retval_off, RV_REG_ZERO, ctx);
 		for (i = 0; i < fmod_ret->nr_nodes; i++) {
-			ret = invoke_bpf_prog(fmod_ret->nodes[i], args_off, retval_off,
+			ret = invoke_bpf_prog(im, fmod_ret->nodes[i], args_off, retval_off,
 					      run_ctx_off, true, ctx);
 			if (ret)
 				goto out;
@@ -1211,10 +1221,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 			emit_sd(RV_REG_FP, -tcc_off, RV_REG_TCC, ctx);
 		emit_sd(RV_REG_FP, -retval_off, RV_REG_A0, ctx);
 		emit_sd(RV_REG_FP, -(retval_off - 8), regmap[BPF_REG_0], ctx);
-		im->ip_after_call = ctx->ro_insns + ctx->ninsns;
-		/* 2 nops reserved for auipc+jalr pair */
-		emit(rv_nop(), ctx);
-		emit(rv_nop(), ctx);
 	}
 
 	/* update branches saved in invoke_bpf_mod_ret with bnez */
@@ -1230,14 +1236,13 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 		emit_store_stack_imm64(RV_REG_T1, -func_meta_off, func_meta, ctx);
 
 	if (fexit->nr_nodes) {
-		ret = invoke_bpf(fexit, args_off, retval_off, run_ctx_off, func_meta_off,
+		ret = invoke_bpf(im, fexit, args_off, retval_off, run_ctx_off, func_meta_off,
 				 false, func_meta, cookie_off, ctx);
 		if (ret)
 			goto out;
 	}
 
 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
-		im->ip_epilogue = ctx->ro_insns + ctx->ninsns;
 		emit_imm(RV_REG_A0, ctx->insns ? (const s64)im : RV_MAX_COUNT_IMM, ctx);
 		ret = emit_call((const u64)__bpf_tramp_exit, true, ctx);
 		if (ret)
@@ -1299,7 +1304,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
 			     struct bpf_tramp_nodes *tnodes, void *func_addr)
 {
-	struct bpf_tramp_image im;
+	struct bpf_tramp_image im = {};
 	struct rv_jit_context ctx;
 	int ret;
 
diff --git a/arch/s390/net/bpf_jit_comp.c b/arch/s390/net/bpf_jit_comp.c
index c4b47070bb590..1b2566012b63a 100644
--- a/arch/s390/net/bpf_jit_comp.c
+++ b/arch/s390/net/bpf_jit_comp.c
@@ -2566,6 +2566,8 @@ struct bpf_tramp_jit {
 	int r14_off;		/* Offset of saved %r14, has to be at the
 				 * bottom */
 	int do_fexit;		/* do_fexit: label */
+	int skip[BPF_MAX_TRAMP_LINKS];	/* skip: labels after each prog */
+	int nr_progs;
 };
 
 static void load_imm64(struct bpf_jit *jit, int dst_reg, u64 val)
@@ -2584,6 +2586,7 @@ static void emit_store_stack_imm64(struct bpf_jit *jit, int tmp_reg, int stack_o
 }
 
 static int invoke_bpf_prog(struct bpf_tramp_jit *tjit,
+			   struct bpf_tramp_image *im,
 			   const struct btf_func_model *m,
 			   struct bpf_tramp_node *node, bool save_ret)
 {
@@ -2591,8 +2594,20 @@ static int invoke_bpf_prog(struct bpf_tramp_jit *tjit,
 	int cookie_off = tjit->run_ctx_off +
 			 offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
 	struct bpf_prog *p = node->link->prog;
+	void *skip = jit->prg_buf + jit->prg;
+	int idx = tjit->nr_progs++;
 	int patch;
 
+	if (idx >= ARRAY_SIZE(tjit->skip))
+		return -E2BIG;
+
+	/*
+	 * nop, patched to skip this prog when it is detached
+	 */
+
+	/* brcl 0,skip */
+	EMIT6_PCREL_RILC(0xc0040000, 0, tjit->skip[idx]);
+
 	/*
 	 * run_ctx.cookie = node->cookie;
 	 */
@@ -2652,10 +2667,15 @@ static int invoke_bpf_prog(struct bpf_tramp_jit *tjit,
 	/* brasl %r14,__bpf_prog_exit */
 	EMIT6_PCREL_RILB_PTR(0xc0050000, REG_14, bpf_trampoline_exit(p));
 
+	/* skip: */
+	tjit->skip[idx] = jit->prg;
+	bpf_tramp_image_add_skip(im, p, skip, jit->prg_buf + jit->prg);
+
 	return 0;
 }
 
 static int invoke_bpf(struct bpf_tramp_jit *tjit,
+		      struct bpf_tramp_image *im,
 		      const struct btf_func_model *m,
 		      struct bpf_tramp_nodes *tn, bool save_ret,
 		      u64 func_meta, int cookie_off)
@@ -2670,7 +2690,7 @@ static int invoke_bpf(struct bpf_tramp_jit *tjit,
 			emit_store_stack_imm64(jit, REG_0, tjit->func_meta_off, meta);
 			cur_cookie--;
 		}
-		if (invoke_bpf_prog(tjit, m, tn->nodes[i], save_ret))
+		if (invoke_bpf_prog(tjit, im, m, tn->nodes[i], save_ret))
 			return -EINVAL;
 	}
 
@@ -2712,6 +2732,11 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 	u64 func_meta;
 	int i, j;
 
+	/* The skip labels are taken from the previous pass. */
+	tjit->nr_progs = 0;
+	if (im)
+		im->nr_skips = 0;
+
 	/* Support as many stack arguments as "mvc" instruction can handle. */
 	nr_reg_args = min_t(int, m->nr_args, MAX_NR_REG_ARGS);
 	nr_stack_args = m->nr_args - nr_reg_args;
@@ -2875,7 +2900,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 		emit_store_stack_imm64(jit, REG_0, tjit->retval_off, 0);
 	}
 
-	if (invoke_bpf(tjit, m, fentry, flags & BPF_TRAMP_F_RET_FENTRY_RET,
+	if (invoke_bpf(tjit, im, m, fentry, flags & BPF_TRAMP_F_RET_FENTRY_RET,
 		       func_meta, cookie_off))
 		return -EINVAL;
 
@@ -2889,7 +2914,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 		       0xf000 | tjit->retval_off);
 
 		for (i = 0; i < fmod_ret->nr_nodes; i++) {
-			if (invoke_bpf_prog(tjit, m, fmod_ret->nodes[i], true))
+			if (invoke_bpf_prog(tjit, im, m, fmod_ret->nodes[i], true))
 				return -EINVAL;
 
 			/*
@@ -2943,15 +2968,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 		/* mvc tccnt_off(%r15),tail_call_cnt(4,%r15) */
 		_EMIT6(0xd203f000 | tjit->tccnt_off,
 		       0xf000 | offsetof(struct prog_frame, tail_call_cnt));
-
-		im->ip_after_call = jit->prg_buf + jit->prg;
-
-		/*
-		 * The following nop will be patched by bpf_tramp_image_put().
-		 */
-
-		/* brcl 0,im->ip_epilogue */
-		EMIT6_PCREL_RILC(0xc0040000, 0, (u64)im->ip_epilogue);
 	}
 
 	/* Set the "is_return" flag for fsession. */
@@ -2962,12 +2978,10 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 
 	/* do_fexit: */
 	tjit->do_fexit = jit->prg;
-	if (invoke_bpf(tjit, m, fexit, false, func_meta, cookie_off))
+	if (invoke_bpf(tjit, im, m, fexit, false, func_meta, cookie_off))
 		return -EINVAL;
 
 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
-		im->ip_epilogue = jit->prg_buf + jit->prg;
-
 		/*
 		 * __bpf_tramp_exit(im);
 		 */
@@ -3016,7 +3030,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
 			     struct bpf_tramp_nodes *tnodes, void *orig_call)
 {
-	struct bpf_tramp_image im;
+	struct bpf_tramp_image im = {};
 	struct bpf_tramp_jit tjit;
 	int ret;
 
diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index 2853e87797a78..6bca87457e872 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -3217,16 +3217,21 @@ static void restore_regs(const struct btf_func_model *m, u8 **prog,
 }
 
 static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,
+			   struct bpf_tramp_image *im,
 			   struct bpf_tramp_node *node, int stack_size,
 			   int run_ctx_off, bool save_ret,
 			   void *image, void *rw_image)
 {
 	u8 *prog = *pprog;
-	u8 *jmp_insn;
+	u8 *jmp_insn, *skip;
 	int ctx_cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
 	struct bpf_prog *p = node->link->prog;
 	u64 cookie = node->cookie;
 
+	/* nop, patched to skip this prog when it is detached */
+	skip = image + (prog - (u8 *)rw_image);
+	emit_nops(&prog, X86_PATCH_SIZE);
+
 	/* mov rdi, cookie */
 	emit_mov_imm64(&prog, BPF_REG_1, (long) cookie >> 32, (u32) (long) cookie);
 
@@ -3301,6 +3306,8 @@ static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,
 	if (emit_rsb_call(&prog, bpf_trampoline_exit(p), image + (prog - (u8 *)rw_image)))
 		return -EINVAL;
 
+	bpf_tramp_image_add_skip(im, p, skip, image + (prog - (u8 *)rw_image));
+
 	*pprog = prog;
 	return 0;
 }
@@ -3332,6 +3339,7 @@ static int emit_cond_near_jump(u8 **pprog, void *func, void *ip, u8 jmp_cond)
 }
 
 static int invoke_bpf(const struct btf_func_model *m, u8 **pprog,
+		      struct bpf_tramp_image *im,
 		      struct bpf_tramp_nodes *tl, int stack_size,
 		      int run_ctx_off, int func_meta_off, bool save_ret,
 		      void *image, void *rw_image, u64 func_meta,
@@ -3346,7 +3354,7 @@ static int invoke_bpf(const struct btf_func_model *m, u8 **pprog,
 				func_meta | (cur_cookie << BPF_TRAMP_COOKIE_INDEX_SHIFT));
 			cur_cookie--;
 		}
-		if (invoke_bpf_prog(m, &prog, tl->nodes[i], stack_size,
+		if (invoke_bpf_prog(m, &prog, im, tl->nodes[i], stack_size,
 				    run_ctx_off, save_ret, image, rw_image))
 			return -EINVAL;
 	}
@@ -3355,6 +3363,7 @@ static int invoke_bpf(const struct btf_func_model *m, u8 **pprog,
 }
 
 static int invoke_bpf_mod_ret(const struct btf_func_model *m, u8 **pprog,
+			      struct bpf_tramp_image *im,
 			      struct bpf_tramp_nodes *tl, int stack_size,
 			      int run_ctx_off, u8 **branches,
 			      void *image, void *rw_image)
@@ -3368,7 +3377,7 @@ static int invoke_bpf_mod_ret(const struct btf_func_model *m, u8 **pprog,
 	emit_mov_imm32(&prog, false, BPF_REG_0, 0);
 	emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8);
 	for (i = 0; i < tl->nr_nodes; i++) {
-		if (invoke_bpf_prog(m, &prog, tl->nodes[i], stack_size, run_ctx_off, true,
+		if (invoke_bpf_prog(m, &prog, im, tl->nodes[i], stack_size, run_ctx_off, true,
 				    image, rw_image))
 			return -EINVAL;
 
@@ -3640,7 +3649,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 	}
 
 	if (fentry->nr_nodes) {
-		if (invoke_bpf(m, &prog, fentry, regs_off, run_ctx_off, func_meta_off,
+		if (invoke_bpf(m, &prog, im, fentry, regs_off, run_ctx_off, func_meta_off,
 			       flags & BPF_TRAMP_F_RET_FENTRY_RET, image, rw_image,
 			       func_meta, cookie_off))
 			return -EINVAL;
@@ -3652,7 +3661,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 		if (!branches)
 			return -ENOMEM;
 
-		if (invoke_bpf_mod_ret(m, &prog, fmod_ret, regs_off,
+		if (invoke_bpf_mod_ret(m, &prog, im, fmod_ret, regs_off,
 				       run_ctx_off, branches, image, rw_image)) {
 			ret = -EINVAL;
 			goto cleanup;
@@ -3682,8 +3691,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 		}
 		/* remember return value in a stack for bpf prog to access */
 		emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8);
-		im->ip_after_call = image + (prog - (u8 *)rw_image);
-		emit_nops(&prog, X86_PATCH_SIZE);
 	}
 
 	if (fmod_ret->nr_nodes) {
@@ -3708,7 +3715,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 		emit_store_stack_imm64(&prog, BPF_REG_0, -func_meta_off, func_meta);
 
 	if (fexit->nr_nodes) {
-		if (invoke_bpf(m, &prog, fexit, regs_off, run_ctx_off, func_meta_off,
+		if (invoke_bpf(m, &prog, im, fexit, regs_off, run_ctx_off, func_meta_off,
 			       false, image, rw_image, func_meta, cookie_off)) {
 			ret = -EINVAL;
 			goto cleanup;
@@ -3723,7 +3730,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 	 * restored to R0.
 	 */
 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
-		im->ip_epilogue = image + (prog - (u8 *)rw_image);
 		/* arg1: mov rdi, im */
 		emit_mov_imm64(&prog, BPF_REG_1, (long) im >> 32, (u32) (long) im);
 		if (emit_rsb_call(&prog, __bpf_tramp_exit, image + (prog - (u8 *)rw_image))) {
@@ -3811,7 +3817,7 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i
 int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
 			     struct bpf_tramp_nodes *tnodes, void *func_addr)
 {
-	struct bpf_tramp_image im;
+	struct bpf_tramp_image im = {};
 	void *image;
 	int ret;
 
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 1d2676782d707..d5b507f090ebe 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1258,11 +1258,17 @@ struct btf_func_model {
 #define BPF_TRAMP_F_INDIRECT		BIT(8)
 
 /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
- * bytes on x86.
+ * bytes on x86. The trampoline image has to fit in PAGE_SIZE.
  */
 enum {
-#if defined(__s390x__)
+#if defined(__s390x__) || defined(__powerpc64__)
 	BPF_MAX_TRAMP_LINKS = 27,
+#elif defined(__loongarch__)
+	BPF_MAX_TRAMP_LINKS = 33,
+#elif defined(__x86_64__)
+	BPF_MAX_TRAMP_LINKS = 36,
+#elif defined(__aarch64__)
+	BPF_MAX_TRAMP_LINKS = 37,
 #else
 	BPF_MAX_TRAMP_LINKS = 38,
 #endif
@@ -1363,19 +1369,48 @@ enum bpf_tramp_prog_type {
 	BPF_TRAMP_FSESSION,
 };
 
+/*
+ * Each prog call in a trampoline image is preceded by a nop. When the prog is
+ * detached, the nop is patched to a jump to target, right after the call, so
+ * that tasks still running in the image skip the prog.
+ */
+struct bpf_tramp_skip {
+	struct bpf_prog *prog;
+	void *nop;
+	void *target;
+};
+
 struct bpf_tramp_image {
 	void *image;
 	int size;
 	struct bpf_ksym ksym;
 	struct percpu_ref pcref;
-	void *ip_after_call;
-	void *ip_epilogue;
+	bool call_orig;
+	/* entry in tr->images, the image holds a reference on tr */
+	struct bpf_trampoline *tr;
+	struct list_head list;
+	int nr_skips;
+	struct bpf_tramp_skip *skips;
 	union {
 		struct rcu_head rcu;
 		struct work_struct work;
 	};
 };
 
+static inline void bpf_tramp_image_add_skip(struct bpf_tramp_image *im, struct bpf_prog *prog,
+					    void *nop, void *target)
+{
+	struct bpf_tramp_skip *skip;
+
+	/* struct_ops trampolines and arch_bpf_trampoline_size() have no image */
+	if (!im || !im->skips)
+		return;
+	skip = &im->skips[im->nr_skips++];
+	skip->prog = prog;
+	skip->nop = nop;
+	skip->target = target;
+}
+
 struct bpf_trampoline {
 	/* hlist for trampoline_key_table */
 	struct hlist_node hlist_key;
@@ -1402,6 +1437,8 @@ struct bpf_trampoline {
 	int progs_cnt[BPF_TRAMP_MAX];
 	/* Executable image of trampoline */
 	struct bpf_tramp_image *cur_image;
+	/* Images not freed yet, cur_image and older ones still in use */
+	struct list_head images;
 	/* Used as temporary old image storage for multi_attach */
 	struct {
 		struct bpf_tramp_image *old_image;
@@ -1770,6 +1807,7 @@ struct bpf_prog_aux {
 	bool offload_requested; /* Program is bound and offloaded to the netdev. */
 	bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
 	bool attach_tracing_prog; /* true if tracing another tracing program */
+	bool tramp_linked; /* true if it was ever called from a trampoline */
 	bool func_proto_unreliable;
 	bool tail_call_reachable;
 	bool xdp_has_frags;
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 244a939b9d2da..96217b99399d5 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2448,6 +2448,21 @@ static void __bpf_prog_put_rcu(struct rcu_head *rcu)
 	bpf_prog_free(aux->prog);
 }
 
+/*
+ * Progs called from a trampoline can also be reached by a task that was
+ * preempted in the trampoline before the prog's enter helper took its RCU
+ * read lock, wait for those first.
+ */
+static void __bpf_prog_put_rcu_tasks(struct rcu_head *rcu)
+{
+	struct bpf_prog *prog = container_of(rcu, struct bpf_prog_aux, rcu)->prog;
+
+	if (prog->sleepable)
+		call_rcu_tasks_trace(rcu, __bpf_prog_put_rcu);
+	else
+		call_rcu(rcu, __bpf_prog_put_rcu);
+}
+
 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
 {
 	bpf_prog_kallsyms_del_all(prog);
@@ -2461,7 +2476,9 @@ static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
 		btf_put(prog->aux->attach_btf);
 
 	if (deferred) {
-		if (prog->sleepable)
+		if (IS_ENABLED(CONFIG_TASKS_RCU) && prog->aux->tramp_linked)
+			call_rcu_tasks(&prog->aux->rcu, __bpf_prog_put_rcu_tasks);
+		else if (prog->sleepable)
 			call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
 		else
 			call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
index 90b70ea0d3709..ea8e67a389194 100644
--- a/kernel/bpf/trampoline.c
+++ b/kernel/bpf/trampoline.c
@@ -401,6 +401,7 @@ static struct bpf_trampoline *bpf_trampoline_lookup(u64 key, unsigned long ip)
 	head = &trampoline_ip_table[hash_64(tr->ip, TRAMPOLINE_HASH_BITS)];
 	hlist_add_head(&tr->hlist_ip, head);
 	refcount_set(&tr->refcnt, 1);
+	INIT_LIST_HEAD(&tr->images);
 	for (i = 0; i < BPF_TRAMP_MAX; i++)
 		INIT_HLIST_HEAD(&tr->progs_hlist[i]);
 out:
@@ -565,15 +566,22 @@ static void bpf_tramp_image_free(struct bpf_tramp_image *im)
 	arch_free_bpf_trampoline(im->image, im->size);
 	bpf_jit_uncharge_modmem(im->size);
 	percpu_ref_exit(&im->pcref);
+	kfree(im->skips);
 	kfree_rcu(im, rcu);
 }
 
 static void __bpf_tramp_image_put_deferred(struct work_struct *work)
 {
 	struct bpf_tramp_image *im;
+	struct bpf_trampoline *tr;
 
 	im = container_of(work, struct bpf_tramp_image, work);
+	tr = im->tr;
+	trampoline_lock(tr);
+	list_del(&im->list);
+	trampoline_unlock(tr);
 	bpf_tramp_image_free(im);
+	bpf_trampoline_put(tr);
 }
 
 /* callback, fexit step 3 or fentry step 2 */
@@ -601,7 +609,7 @@ static void __bpf_tramp_image_put_rcu_tasks(struct rcu_head *rcu)
 	struct bpf_tramp_image *im;
 
 	im = container_of(rcu, struct bpf_tramp_image, rcu);
-	if (im->ip_after_call)
+	if (im->call_orig)
 		/* the case of fmod_ret/fexit trampoline and CONFIG_PREEMPTION=y */
 		percpu_ref_kill(&im->pcref);
 	else
@@ -621,9 +629,9 @@ static void bpf_tramp_image_put(struct bpf_tramp_image *im)
 	 *
 	 * The trampoline is unreachable before bpf_tramp_image_put().
 	 *
-	 * First, patch the trampoline to avoid calling into fexit progs.
-	 * The progs will be freed even if the original function is still
-	 * executing or sleeping.
+	 * Progs are patched out of the image when they are detached, see
+	 * bpf_trampoline_skip_prog(), so they can be freed even if a task is
+	 * still in the image.
 	 * In case of CONFIG_PREEMPT=y use call_rcu_tasks() to wait on
 	 * first few asm instructions to execute and call into
 	 * __bpf_tramp_enter->percpu_ref_get.
@@ -637,11 +645,7 @@ static void bpf_tramp_image_put(struct bpf_tramp_image *im)
 	 * percpu_ref_kill will be waiting for. Hence the first
 	 * call_rcu_tasks() is not necessary.
 	 */
-	if (im->ip_after_call) {
-		int err = bpf_arch_text_poke(im->ip_after_call, BPF_MOD_NOP,
-					     BPF_MOD_JUMP, NULL,
-					     im->ip_epilogue);
-		WARN_ON(err);
+	if (im->call_orig) {
 		if (IS_ENABLED(CONFIG_TASKS_RCU))
 			call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu_tasks);
 		else
@@ -658,7 +662,7 @@ static void bpf_tramp_image_put(struct bpf_tramp_image *im)
 	call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks);
 }
 
-static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size)
+static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size, int nr_progs)
 {
 	struct bpf_tramp_image *im;
 	struct bpf_ksym *ksym;
@@ -669,6 +673,10 @@ static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size)
 	if (!im)
 		goto out;
 
+	im->skips = kzalloc_objs(*im->skips, nr_progs);
+	if (!im->skips)
+		goto out_free_im;
+
 	err = bpf_jit_charge_modmem(size);
 	if (err)
 		goto out_free_im;
@@ -695,6 +703,7 @@ static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size)
 out_uncharge:
 	bpf_jit_uncharge_modmem(size);
 out_free_im:
+	kfree(im->skips);
 	kfree(im);
 out:
 	return ERR_PTR(err);
@@ -771,11 +780,12 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut
 		goto out;
 	}
 
-	im = bpf_tramp_image_alloc(tr->key, size);
+	im = bpf_tramp_image_alloc(tr->key, size, total);
 	if (IS_ERR(im)) {
 		err = PTR_ERR(im);
 		goto out;
 	}
+	im->call_orig = tr->flags & BPF_TRAMP_F_CALL_ORIG;
 
 	err = arch_prepare_bpf_trampoline(im, im->image, im->image + size,
 					  &tr->func.model, tr->flags, tnodes,
@@ -806,8 +816,14 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut
 #endif
 
 out_free:
-	if (err)
+	if (err) {
 		bpf_tramp_image_free(im);
+	} else {
+		/* track the image until it is freed, for bpf_trampoline_skip_prog() */
+		refcount_inc(&tr->refcnt);
+		im->tr = tr;
+		list_add(&im->list, &tr->images);
+	}
 out:
 	/* If any error happens, restore previous flags */
 	if (err)
@@ -907,6 +923,7 @@ static int bpf_trampoline_add_prog(struct bpf_trampoline *tr,
 	}
 
 	hlist_add_head(&node->tramp_hlist, prog_list);
+	node->link->prog->aux->tramp_linked = true;
 	if (kind == BPF_TRAMP_FSESSION) {
 		tr->progs_cnt[BPF_TRAMP_FENTRY]++;
 		fexit = fsession_exit(node);
@@ -920,6 +937,32 @@ static int bpf_trampoline_add_prog(struct bpf_trampoline *tr,
 	return 0;
 }
 
+/*
+ * prog was detached and can be freed, but tasks may still be running in images
+ * that call it, sleeping in an earlier prog for example. They can be in any
+ * image that is not freed yet, not only in cur_image, so patch all of them to
+ * jump over prog.
+ */
+static void bpf_trampoline_skip_prog(struct bpf_trampoline *tr, struct bpf_prog *prog)
+{
+	struct bpf_tramp_image *im;
+	int i, err;
+
+	list_for_each_entry(im, &tr->images, list) {
+		for (i = 0; i < im->nr_skips; i++) {
+			struct bpf_tramp_skip *skip = &im->skips[i];
+
+			if (skip->prog != prog)
+				continue;
+			err = bpf_arch_text_poke(skip->nop, BPF_MOD_NOP, BPF_MOD_JUMP,
+						 NULL, skip->target);
+			WARN_ON_ONCE(err);
+			/* not a nop anymore, and prog's address can be reused */
+			skip->prog = NULL;
+		}
+	}
+}
+
 static void bpf_trampoline_remove_prog(struct bpf_trampoline *tr,
 				       struct bpf_tramp_node *node)
 {
@@ -937,6 +980,7 @@ static void bpf_trampoline_remove_prog(struct bpf_trampoline *tr,
 	}
 	hlist_del_init(&node->tramp_hlist);
 	tr->progs_cnt[kind]--;
+	bpf_trampoline_skip_prog(tr, node->link->prog);
 }
 
 static int __bpf_trampoline_link_prog(struct bpf_tramp_node *node,
@@ -1245,11 +1289,8 @@ void bpf_trampoline_put(struct bpf_trampoline *tr)
 		if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[i])))
 			goto out;
 
-	/* This code will be executed even when the last bpf_tramp_image
-	 * is alive. All progs are detached from the trampoline and the
-	 * trampoline image is patched with jmp into epilogue to skip
-	 * fexit progs. The fentry-only trampoline will be freed via
-	 * multiple rcu callbacks.
+	/* All progs are detached and the last image has been freed, images
+	 * hold a reference on the trampoline until then.
 	 */
 	hlist_del(&tr->hlist_key);
 	hlist_del(&tr->hlist_ip);
diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_mod_race.c b/tools/testing/selftests/bpf/prog_tests/bpf_mod_race.c
index ecc3d47919ad6..f8497e764beb5 100644
--- a/tools/testing/selftests/bpf/prog_tests/bpf_mod_race.c
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_mod_race.c
@@ -55,38 +55,6 @@ static void *load_module_thread(void *p)
 	return p;
 }
 
-static int sys_userfaultfd(int flags)
-{
-	return syscall(__NR_userfaultfd, flags);
-}
-
-static int test_setup_uffd(void *fault_addr)
-{
-	struct uffdio_register uffd_register = {};
-	struct uffdio_api uffd_api = {};
-	int uffd;
-
-	uffd = sys_userfaultfd(O_CLOEXEC);
-	if (uffd < 0)
-		return -errno;
-
-	uffd_api.api = UFFD_API;
-	uffd_api.features = 0;
-	if (ioctl(uffd, UFFDIO_API, &uffd_api)) {
-		close(uffd);
-		return -1;
-	}
-
-	uffd_register.range.start = (unsigned long)fault_addr;
-	uffd_register.range.len = getpagesize();
-	uffd_register.mode = UFFDIO_REGISTER_MODE_MISSING;
-	if (ioctl(uffd, UFFDIO_REGISTER, &uffd_register)) {
-		close(uffd);
-		return -1;
-	}
-	return uffd;
-}
-
 static void test_bpf_mod_race_config(const struct test_config *config)
 {
 	void *fault_addr, *skel_fail;
@@ -117,7 +85,7 @@ static void test_bpf_mod_race_config(const struct test_config *config)
 	if (!ASSERT_OK(bpf_mod_race__attach(skel), "bpf_mod_kfunc_race__attach"))
 		goto end_destroy;
 
-	uffd = test_setup_uffd(fault_addr);
+	uffd = uffd_block_page(fault_addr);
 	if (!ASSERT_GE(uffd, 0, "userfaultfd open + register address"))
 		goto end_destroy;
 
diff --git a/tools/testing/selftests/bpf/prog_tests/tramp_prog_detach.c b/tools/testing/selftests/bpf/prog_tests/tramp_prog_detach.c
new file mode 100644
index 0000000000000..1eb0d72376055
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/tramp_prog_detach.c
@@ -0,0 +1,188 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <pthread.h>
+#include <poll.h>
+#include <sys/mman.h>
+#include <linux/userfaultfd.h>
+#include "tramp_prog_detach.skel.h"
+#include "testing_helpers.h"
+
+/*
+ * Detach and free progs while a task sleeps in the prog that runs before them
+ * in the same trampoline image, then let that task continue through the
+ * image. It must not call into the freed progs.
+ *
+ * The task is held in a sleepable prog with userfaultfd, like bpf_mod_race
+ * does.
+ */
+
+static struct bpf_program *pick_prog(struct tramp_prog_detach *skel,
+				     bool fexit, bool sleepable)
+{
+	if (fexit)
+		return sleepable ? skel->progs.fexit_sleepable :
+				   skel->progs.fexit_victim;
+	return sleepable ? skel->progs.fentry_sleepable :
+			   skel->progs.fentry_victim;
+}
+
+static struct bpf_program *sleepable_prog;
+
+static void *run_sleepable(void *arg)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+
+	/* calls bpf_fentry_test1() */
+	return (void *)(long)bpf_prog_test_run_opts(bpf_program__fd(sleepable_prog),
+						    &topts);
+}
+
+static struct tramp_prog_detach *load_one(bool fexit, bool sleepable)
+{
+	struct tramp_prog_detach *skel;
+	int err;
+
+	skel = tramp_prog_detach__open();
+	if (!ASSERT_OK_PTR(skel, "open"))
+		return NULL;
+
+	bpf_program__set_autoload(pick_prog(skel, fexit, sleepable), true);
+	err = tramp_prog_detach__load(skel);
+	if (!ASSERT_OK(err, "load"))
+		goto err;
+	skel->bss->pid = getpid();
+	err = tramp_prog_detach__attach(skel);
+	if (!ASSERT_OK(err, "attach"))
+		goto err;
+	return skel;
+err:
+	tramp_prog_detach__destroy(skel);
+	return NULL;
+}
+
+/* The .bss map of a destroyed skeleton goes away when its prog is freed */
+static bool wait_for_map_free(__u32 map_id)
+{
+	int i, fd;
+
+	for (i = 0; i < 100; i++) {
+		fd = bpf_map_get_fd_by_id(map_id);
+		if (fd < 0)
+			return true;
+		close(fd);
+		usleep(100 * 1000);
+	}
+	return false;
+}
+
+static void test_detach(bool sleepable_fexit, bool victim_fexit)
+{
+	struct tramp_prog_detach *sleepable = NULL, *victims[2] = {};
+	struct pollfd pfd = { .events = POLLIN };
+	struct uffdio_copy uffd_copy = {};
+	struct bpf_map_info map_info = {};
+	__u32 map_info_len = sizeof(map_info);
+	struct uffd_msg uffd_msg;
+	void *fault_page, *src_page = MAP_FAILED;
+	long page_size = getpagesize();
+	bool started = false;
+	void *thread_ret;
+	pthread_t thread;
+	int i, uffd = -1;
+
+	fault_page = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
+			  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	if (!ASSERT_NEQ(fault_page, MAP_FAILED, "mmap fault_page"))
+		return;
+	src_page = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
+			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	if (!ASSERT_NEQ(src_page, MAP_FAILED, "mmap src_page"))
+		goto out;
+
+	/* The most recently attached prog runs first */
+	for (i = 0; i < ARRAY_SIZE(victims); i++) {
+		victims[i] = load_one(victim_fexit, false);
+		if (!victims[i])
+			goto out;
+	}
+	sleepable = load_one(sleepable_fexit, true);
+	if (!sleepable)
+		goto out;
+	sleepable_prog = pick_prog(sleepable, sleepable_fexit, true);
+
+	/* Not armed yet so this doesn't block, make sure sleepable runs first */
+	if (!ASSERT_OK((long)run_sleepable(NULL), "dry run"))
+		goto out;
+	for (i = 0; i < ARRAY_SIZE(victims); i++)
+		if (!ASSERT_LT(sleepable->bss->ts, victims[i]->bss->ts, "prog order"))
+			goto out;
+
+	uffd = uffd_block_page(fault_page);
+	if (!ASSERT_GE(uffd, 0, "userfaultfd open + register address"))
+		goto out;
+	sleepable->bss->fault_addr = fault_page;
+
+	if (!ASSERT_OK(pthread_create(&thread, NULL, run_sleepable, NULL),
+		       "pthread_create"))
+		goto out;
+	started = true;
+
+	/* Wait for the thread to sleep in bpf_copy_from_user() */
+	pfd.fd = uffd;
+	if (!ASSERT_EQ(poll(&pfd, 1, 10000), 1, "poll uffd"))
+		goto out;
+	if (!ASSERT_EQ(read(uffd, &uffd_msg, sizeof(uffd_msg)), sizeof(uffd_msg),
+		       "read uffd"))
+		goto out;
+	if (!ASSERT_EQ(uffd_msg.event, UFFD_EVENT_PAGEFAULT, "uffd pagefault"))
+		goto out;
+
+	/*
+	 * Detach and unload the victim progs and wait for them to be freed.
+	 * After the first one, the task is in an image that isn't the
+	 * trampoline's current one anymore.
+	 */
+	for (i = 0; i < ARRAY_SIZE(victims); i++) {
+		if (!ASSERT_OK(bpf_map_get_info_by_fd(bpf_map__fd(victims[i]->maps.bss),
+						      &map_info, &map_info_len),
+			       "victim bss info"))
+			goto out;
+		tramp_prog_detach__destroy(victims[i]);
+		victims[i] = NULL;
+		if (!ASSERT_TRUE(wait_for_map_free(map_info.id), "victim freed"))
+			goto out;
+	}
+
+out:
+	/* Let the thread proceed with the rest of the trampoline */
+	if (uffd >= 0) {
+		uffd_copy.dst = (unsigned long)fault_page;
+		uffd_copy.src = (unsigned long)src_page;
+		uffd_copy.len = page_size;
+		ASSERT_OK(ioctl(uffd, UFFDIO_COPY, &uffd_copy), "uffd copy");
+		close(uffd);
+	}
+	if (started &&
+	    ASSERT_OK(pthread_join(thread, &thread_ret), "pthread_join"))
+		ASSERT_NULL(thread_ret, "blocking run");
+
+	for (i = 0; i < ARRAY_SIZE(victims); i++)
+		tramp_prog_detach__destroy(victims[i]);
+	tramp_prog_detach__destroy(sleepable);
+	if (src_page != MAP_FAILED)
+		munmap(src_page, page_size);
+	munmap(fault_page, page_size);
+}
+
+void serial_test_tramp_prog_detach(void)
+{
+	/* a task sleeping before the original function is called */
+	if (test__start_subtest("fentry"))
+		test_detach(false, false);
+	/* a task sleeping after the original function returned */
+	if (test__start_subtest("fexit"))
+		test_detach(true, true);
+	/* the original function runs in between and must still be called */
+	if (test__start_subtest("fentry_fexit"))
+		test_detach(false, true);
+}
diff --git a/tools/testing/selftests/bpf/progs/tramp_prog_detach.c b/tools/testing/selftests/bpf/progs/tramp_prog_detach.c
new file mode 100644
index 0000000000000..507d372167ecb
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/tramp_prog_detach.c
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+int pid;
+void *fault_addr;
+__u64 ts;
+
+static int do_sleepable(void)
+{
+	char dst;
+
+	if (bpf_get_current_pid_tgid() >> 32 != pid)
+		return 0;
+
+	ts = bpf_ktime_get_ns();
+	/* blocks for as long as user space wants when fault_addr is armed */
+	bpf_copy_from_user(&dst, sizeof(dst), fault_addr);
+	return 0;
+}
+
+static int do_victim(void)
+{
+	if (bpf_get_current_pid_tgid() >> 32 != pid)
+		return 0;
+
+	ts = bpf_ktime_get_ns();
+	return 0;
+}
+
+SEC("?fentry.s/bpf_fentry_test1")
+int BPF_PROG(fentry_sleepable, int a)
+{
+	return do_sleepable();
+}
+
+SEC("?fentry/bpf_fentry_test1")
+int BPF_PROG(fentry_victim, int a)
+{
+	return do_victim();
+}
+
+SEC("?fexit.s/bpf_fentry_test1")
+int BPF_PROG(fexit_sleepable, int a, int ret)
+{
+	return do_sleepable();
+}
+
+SEC("?fexit/bpf_fentry_test1")
+int BPF_PROG(fexit_victim, int a, int ret)
+{
+	return do_victim();
+}
diff --git a/tools/testing/selftests/bpf/testing_helpers.c b/tools/testing/selftests/bpf/testing_helpers.c
index c970e7793dfcb..9f672d0c9dda2 100644
--- a/tools/testing/selftests/bpf/testing_helpers.c
+++ b/tools/testing/selftests/bpf/testing_helpers.c
@@ -13,6 +13,7 @@
 #include "test_progs.h"
 #include "testing_helpers.h"
 #include <linux/membarrier.h>
+#include <linux/userfaultfd.h>
 
 int parse_num_list(const char *s, bool **num_set, int *num_set_len)
 {
@@ -459,6 +460,33 @@ int kern_sync_rcu(void)
 	return syscall(__NR_membarrier, MEMBARRIER_CMD_SHARED, 0, 0);
 }
 
+int uffd_block_page(void *fault_addr)
+{
+	struct uffdio_register uffd_register = {};
+	struct uffdio_api uffd_api = {};
+	int uffd;
+
+	uffd = syscall(__NR_userfaultfd, O_CLOEXEC);
+	if (uffd < 0)
+		return -errno;
+
+	uffd_api.api = UFFD_API;
+	uffd_api.features = 0;
+	if (ioctl(uffd, UFFDIO_API, &uffd_api)) {
+		close(uffd);
+		return -1;
+	}
+
+	uffd_register.range.start = (unsigned long)fault_addr;
+	uffd_register.range.len = getpagesize();
+	uffd_register.mode = UFFDIO_REGISTER_MODE_MISSING;
+	if (ioctl(uffd, UFFDIO_REGISTER, &uffd_register)) {
+		close(uffd);
+		return -1;
+	}
+	return uffd;
+}
+
 int get_xlated_program(int fd_prog, struct bpf_insn **buf, __u32 *cnt)
 {
 	__u32 buf_element_size = sizeof(struct bpf_insn);
diff --git a/tools/testing/selftests/bpf/testing_helpers.h b/tools/testing/selftests/bpf/testing_helpers.h
index 2edc6fb7fc521..1f83b6080716f 100644
--- a/tools/testing/selftests/bpf/testing_helpers.h
+++ b/tools/testing/selftests/bpf/testing_helpers.h
@@ -36,6 +36,8 @@ __u64 read_perf_max_sample_freq(void);
 int load_bpf_testmod(bool verbose);
 int unload_bpf_testmod(bool verbose);
 int kern_sync_rcu(void);
+/* returns a userfaultfd that makes accesses to fault_addr's page block */
+int uffd_block_page(void *fault_addr);
 int finit_module(int fd, const char *param_values, int flags);
 int delete_module(const char *name, int flags);
 int load_module(const char *path, bool verbose);

3/2 2026/09/27 09:12 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 24428
							output: 32
							thoughts: 1118
						

Thoughts:
**Initial Assessment of the BPF Trampoline Patch**

Okay, let's dive into this patch. The target architecture is amd64, so I need to pay close attention to the x86 code, but also note that the changes are much broader. My initial impression is that this touches core BPF functionality, particularly the trampoline mechanism. I see changes across multiple architectures (arm64, loongarch, powerpc, riscv, s390, and x86), the BPF headers, and, critically, both the BPF syscall implementation and the trampoline management code. This means a good understanding of the entire subsystem is needed.

The patch modifies how BPF programs are attached to and detached from trampolines. It touches image allocation and refcounting, skip patching, and RCU deferred freeing for trampolines. That means this is going to be high risk. These are things that can easily lead to race conditions, memory corruption, use-after-free conditions, locking issues, or even bugs related to "text poking". The implications are significant, as this affects the core BPF subsystem. This kind of change is heavily reachable through standard virtualized environments like QEMU or GCE via the BPF syscalls, particularly BPF_PROG_LOAD and BPF_LINK_CREATE, and other uses like tracing and fentry/fexit. My expert assessment is that `WorthFuzzing = true`.

I now want to verify configuration, concentrate on the core symbols and get more details.

I need to focus my attention on the specific functions altered in the x86 and `kernel/bpf` code. In the x86 code (`arch/x86/net/bpf_jit_comp.c`), I see changes to several key functions including `invoke_bpf_prog`, `invoke_bpf`, `invoke_bpf_mod_ret`, `__arch_prepare_bpf_trampoline`, `arch_bpf_trampoline_size`, and `arch_prepare_bpf_trampoline`. In `kernel/bpf/syscall.c`, I see changes to `__bpf_prog_put_rcu_tasks`, `__bpf_prog_put_noref`, `bpf_prog_put` and `bpf_prog_put_deferred`. The trampoline code (`kernel/bpf/trampoline.c`) has seen the most changes. I see `bpf_trampoline_lookup`, `bpf_tramp_image_free`, `__bpf_tramp_image_put_deferred`, `__bpf_tramp_image_put_rcu_tasks`, `bpf_tramp_image_put`, `bpf_tramp_image_alloc`, `bpf_trampoline_update`, `bpf_trampoline_add_prog`, `bpf_trampoline_skip_prog`, `bpf_trampoline_remove_prog`, `bpf_trampoline_put`, and `bpf_tramp_image_add_skip` (defined inline in `include/linux/bpf.h`).

I'll now need to check whether the functions in `kernel/bpf/trampoline.c` and `arch/x86/net/bpf_jit_comp.c` are static, global, or exported. My next step is to run `grepper` or check the kernel source to get a good handle on which of these symbols are visible outside their respective modules. This will tell me more about the areas of the kernel that can directly use this code, and therefore, potentially influence the attack surface.



4/2 2026/09/27 09:12 tool grepper
0m Args:
{
  "Expression": "bpf_trampoline_update",
  "PathPrefix": "kernel/bpf/"
}

Results:
{
  "Output": "kernel/bpf/trampoline.c=62=struct bpf_trampoline_ops {\n--\nkernel/bpf/trampoline.c-69-#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/bpf/trampoline.c:70:static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex,\nkernel/bpf/trampoline.c-71-\t\t\t\t const struct bpf_trampoline_ops *ops, void *data);\n--\nkernel/bpf/trampoline.c=98=static int bpf_tramp_ftrace_ops_func(struct ftrace_ops *ops, unsigned long ip,\n--\nkernel/bpf/trampoline.c-156-\t\t    !(tr-\u003eflags \u0026 BPF_TRAMP_F_ORIG_STACK))\nkernel/bpf/trampoline.c:157:\t\t\tret = bpf_trampoline_update(tr, false /* lock_direct_mutex */,\nkernel/bpf/trampoline.c-158-\t\t\t\t\t\t    \u0026trampoline_ops, NULL);\n--\nkernel/bpf/trampoline.c-163-\t\tif (tr-\u003eflags \u0026 BPF_TRAMP_F_ORIG_STACK)\nkernel/bpf/trampoline.c:164:\t\t\tret = bpf_trampoline_update(tr, false /* lock_direct_mutex */,\nkernel/bpf/trampoline.c-165-\t\t\t\t\t\t    \u0026trampoline_ops, NULL);\n--\nkernel/bpf/trampoline.c=373=static struct bpf_trampoline *bpf_trampoline_lookup(u64 key, unsigned long ip)\n--\nkernel/bpf/trampoline.c-411-\nkernel/bpf/trampoline.c:412:static int bpf_trampoline_update_fentry(struct bpf_trampoline *tr, u32 orig_flags,\nkernel/bpf/trampoline.c-413-\t\t\t\t\tvoid *old_addr, void *new_addr)\n--\nkernel/bpf/trampoline.c=433=static int unregister_fentry(struct bpf_trampoline *tr, u32 orig_flags, void *data __maybe_unused)\n--\nkernel/bpf/trampoline.c-440-\telse\nkernel/bpf/trampoline.c:441:\t\tret = bpf_trampoline_update_fentry(tr, orig_flags, old_addr, NULL);\nkernel/bpf/trampoline.c-442-\n--\nkernel/bpf/trampoline.c=451=static int modify_fentry(struct bpf_trampoline *tr, u32 orig_flags, struct bpf_tramp_image *im,\n--\nkernel/bpf/trampoline.c-460-\t} else {\nkernel/bpf/trampoline.c:461:\t\tret = bpf_trampoline_update_fentry(tr, orig_flags, old_addr,\nkernel/bpf/trampoline.c-462-\t\t\t\t\t\t   new_addr);\n--\nkernel/bpf/trampoline.c=474=static int register_fentry(struct bpf_trampoline *tr, struct bpf_tramp_image *im,\n--\nkernel/bpf/trampoline.c-491-\t} else {\nkernel/bpf/trampoline.c:492:\t\tret = bpf_trampoline_update_fentry(tr, 0, NULL, new_addr);\nkernel/bpf/trampoline.c-493-\t}\n--\nkernel/bpf/trampoline.c=712=void bpf_trampoline_set_flags(struct bpf_trampoline *tr, u32 flags)\n--\nkernel/bpf/trampoline.c-718-\nkernel/bpf/trampoline.c:719:static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex,\nkernel/bpf/trampoline.c-720-\t\t\t\t const struct bpf_trampoline_ops *ops, void *data)\n--\nkernel/bpf/trampoline.c=986=static int __bpf_trampoline_link_prog(struct bpf_tramp_node *node,\n--\nkernel/bpf/trampoline.c-1027-\t\treturn err;\nkernel/bpf/trampoline.c:1028:\terr = bpf_trampoline_update(tr, true /* lock_direct_mutex */, ops, data);\nkernel/bpf/trampoline.c-1029-\tif (err)\n--\nkernel/bpf/trampoline.c=1046=static int __bpf_trampoline_unlink_prog(struct bpf_tramp_node *node,\n--\nkernel/bpf/trampoline.c-1066-\tbpf_trampoline_remove_prog(tr, node);\nkernel/bpf/trampoline.c:1067:\treturn bpf_trampoline_update(tr, true /* lock_direct_mutex */, ops, data);\nkernel/bpf/trampoline.c-1068-}\n--\nkernel/bpf/trampoline.c=1674=static void bpf_trampoline_multi_attach_free(struct bpf_trampoline *tr)\n--\nkernel/bpf/trampoline.c-1677-\t * Only free old_image if it is no longer the active image.\nkernel/bpf/trampoline.c:1678:\t * When bpf_trampoline_update() fails before modify_fentry_multi()/\nkernel/bpf/trampoline.c-1679-\t * unregister_fentry_multi() is called, cur_image is unchanged\n"
}

5/2 2026/09/27 09:12 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 27774
							output: 52
							thoughts: 70
						

Thoughts:
Here's my summary, as the expert:

**Investigating BPF Trampolines and JIT Compilation**

Alright, let's dive in. My immediate focus is on understanding the interaction between the BPF trampoline mechanism and the JIT compilation for x86, specifically around `kernel/bpf/trampoline.c` and `arch/x86/net/bpf_jit_comp.c`. I need to verify how these two components come together. My first instinct is to zero in on the exposed symbols and entry points in `kernel/bpf/trampoline.c`. I need to understand what's visible outside that module. A quick `grep` for `EXPORT_SYMBOL` or any other non-static function declarations in that file should give me a good overview of its public API and its potential involvement with the x86 JIT. This will give me the information I need to understand how the trampoline hooks into the JIT infrastructure.



6/2 2026/09/27 09:12 tool grepper
0m Args:
{
  "Expression": "^(int|void|struct [a-z0-9_]+ \\*) ?bpf_tramp",
  "PathPrefix": "kernel/bpf/trampoline.c"
}

Results:
{
  "Output": "kernel/bpf/trampoline.c=665=static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size, int nr_progs)\n--\nkernel/bpf/trampoline.c-711-\nkernel/bpf/trampoline.c:712:void bpf_trampoline_set_flags(struct bpf_trampoline *tr, u32 flags)\nkernel/bpf/trampoline.c-713-{\n--\nkernel/bpf/trampoline.c=986=static int __bpf_trampoline_link_prog(struct bpf_tramp_node *node,\n--\nkernel/bpf/trampoline.c-1033-\nkernel/bpf/trampoline.c:1034:int bpf_trampoline_link_prog(struct bpf_tramp_node *node,\nkernel/bpf/trampoline.c-1035-\t\t\t     struct bpf_trampoline *tr,\n--\nkernel/bpf/trampoline.c=1046=static int __bpf_trampoline_unlink_prog(struct bpf_tramp_node *node,\n--\nkernel/bpf/trampoline.c-1070-/* bpf_trampoline_unlink_prog() should never fail. */\nkernel/bpf/trampoline.c:1071:int bpf_trampoline_unlink_prog(struct bpf_tramp_node *node,\nkernel/bpf/trampoline.c-1072-\t\t\t       struct bpf_trampoline *tr,\n--\nkernel/bpf/trampoline.c=1149=static struct bpf_shim_tramp_link *cgroup_shim_find(struct bpf_trampoline *tr,\n--\nkernel/bpf/trampoline.c-1166-\nkernel/bpf/trampoline.c:1167:int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,\nkernel/bpf/trampoline.c-1168-\t\t\t\t    int cgroup_atype,\n--\nkernel/bpf/trampoline.c-1231-\nkernel/bpf/trampoline.c:1232:void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog)\nkernel/bpf/trampoline.c-1233-{\n--\nkernel/bpf/trampoline.c-1257-\nkernel/bpf/trampoline.c:1258:struct bpf_trampoline *bpf_trampoline_get(u64 key,\nkernel/bpf/trampoline.c-1259-\t\t\t\t\t  struct bpf_attach_target_info *tgt_info)\n--\nkernel/bpf/trampoline.c-1277-\nkernel/bpf/trampoline.c:1278:void bpf_trampoline_put(struct bpf_trampoline *tr)\nkernel/bpf/trampoline.c-1279-{\n--\nkernel/bpf/trampoline.c=1693=static void bpf_trampoline_multi_attach_rollback(struct bpf_trampoline *tr)\n--\nkernel/bpf/trampoline.c-1709-\nkernel/bpf/trampoline.c:1710:int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids,\nkernel/bpf/trampoline.c-1711-\t\t\t\tstruct bpf_tracing_multi_link *link)\n--\nkernel/bpf/trampoline.c-1809-\nkernel/bpf/trampoline.c:1810:void bpf_trampoline_multi_detach(struct bpf_prog *prog,\nkernel/bpf/trampoline.c-1811-\t\t\t\t struct bpf_tracing_multi_link *link)\n"
}

7/2 2026/09/27 09:12 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 29060
							output: 41
							thoughts: 27
						

Thoughts:
Here's my summary, as though I'm thinking it:

**Digging into the x86 BPF JIT Compiler**

Alright, let's dive in. I need to understand this BPF JIT compiler for x86. The best way to get a solid handle on it is to look at the source code itself. Time to zero in on `arch/x86/net/bpf_jit_comp.c`. That should give me a clear picture of the x86-specific optimizations and the code generation process. I'm especially interested in seeing which functions are defined in this file to provide insight into how the JIT compiler works. I'm expecting this file to have functions for code generation based on the BPF instructions.



8/2 2026/09/27 09:12 tool grepper
0m Args:
{
  "Expression": "arch_.*trampoline",
  "PathPrefix": "arch/x86/net/bpf_jit_comp.c"
}

Results:
{
  "Output": "arch/x86/net/bpf_jit_comp.c=3219=static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,\n--\narch/x86/net/bpf_jit_comp.c-3240-\t * bpf_tramp_run_ctx is already preserved by\narch/x86/net/bpf_jit_comp.c:3241:\t * arch_prepare_bpf_trampoline().\narch/x86/net/bpf_jit_comp.c-3242-\t *\n--\narch/x86/net/bpf_jit_comp.c=3365=static int invoke_bpf_mod_ret(const struct btf_func_model *m, u8 **pprog,\n--\narch/x86/net/bpf_jit_comp.c-3467- */\narch/x86/net/bpf_jit_comp.c:3468:static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_image,\narch/x86/net/bpf_jit_comp.c-3469-\t\t\t\t\t void *rw_image_end, void *image,\n--\narch/x86/net/bpf_jit_comp.c-3772-\narch/x86/net/bpf_jit_comp.c:3773:void *arch_alloc_bpf_trampoline(unsigned int size)\narch/x86/net/bpf_jit_comp.c-3774-{\n--\narch/x86/net/bpf_jit_comp.c-3777-\narch/x86/net/bpf_jit_comp.c:3778:void arch_free_bpf_trampoline(void *image, unsigned int size)\narch/x86/net/bpf_jit_comp.c-3779-{\n--\narch/x86/net/bpf_jit_comp.c-3782-\narch/x86/net/bpf_jit_comp.c:3783:int arch_protect_bpf_trampoline(void *image, unsigned int size)\narch/x86/net/bpf_jit_comp.c-3784-{\n--\narch/x86/net/bpf_jit_comp.c-3787-\narch/x86/net/bpf_jit_comp.c:3788:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,\narch/x86/net/bpf_jit_comp.c-3789-\t\t\t\tconst struct btf_func_model *m, u32 flags,\n--\narch/x86/net/bpf_jit_comp.c-3803-\narch/x86/net/bpf_jit_comp.c:3804:\tret = __arch_prepare_bpf_trampoline(im, rw_image, rw_image + size, image, m,\narch/x86/net/bpf_jit_comp.c-3805-\t\t\t\t\t    flags, tnodes, func_addr);\n--\narch/x86/net/bpf_jit_comp.c-3816-\narch/x86/net/bpf_jit_comp.c:3817:int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\narch/x86/net/bpf_jit_comp.c-3818-\t\t\t     struct bpf_tramp_nodes *tnodes, void *func_addr)\n--\narch/x86/net/bpf_jit_comp.c-3823-\narch/x86/net/bpf_jit_comp.c:3824:\t/* Allocate a temporary buffer for __arch_prepare_bpf_trampoline().\narch/x86/net/bpf_jit_comp.c-3825-\t *\n--\narch/x86/net/bpf_jit_comp.c-3834-\narch/x86/net/bpf_jit_comp.c:3835:\tret = __arch_prepare_bpf_trampoline(\u0026im, image, image + PAGE_SIZE, image,\narch/x86/net/bpf_jit_comp.c-3836-\t\t\t\t\t    m, flags, tnodes, func_addr);\n"
}

9/2 2026/09/27 09:12 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 30436
							output: 28
							thoughts: 39
						

Thoughts:
Okay, let's see, I need to understand how these BPF trampolines are configured and built within the kernel. My immediate thought is, "What configurations specifically govern these?" I need to pinpoint the relevant settings.

So, I'll start with the code itself. Where is `bpf_trampoline.c` built within the kernel source tree? The most logical place to look for that build information is the `Makefile` within the `kernel/bpf/` directory. That's usually where these things are defined. That should point me in the right direction.



10/2 2026/09/27 09:12 tool grepper
0m Args:
{
  "Expression": "trampoline",
  "PathPrefix": "kernel/bpf/Makefile"
}

Results:
{
  "Output": "kernel/bpf/Makefile=16=obj-$(CONFIG_BPF_SYSCALL) += disasm.o mprog.o\nkernel/bpf/Makefile:17:obj-$(CONFIG_BPF_JIT) += trampoline.o\nkernel/bpf/Makefile-18-obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o rqspinlock.o stream.o\n"
}

11/2 2026/09/27 09:12 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 30733
							output: 29
							thoughts: 39
						

Thoughts:
Here's my take on the thought process, as requested:

**Kconfig Shenanigans: BPF and JIT**

Alright, let's dive in. First things first: `CONFIG_BPF_JIT` and `CONFIG_BPF_SYSCALL` – those are the key players right now. I need to get a handle on how these Kconfig options interact, especially in the context of the BPF JIT and trampoline mechanisms. My immediate focus is going to be on the `BPF_JIT` configuration options and anything related to trampolines. It's time to dig into the Kconfig files and see how everything is wired together. I expect there might be some dependencies or conditional logic at play here that need clarification. Understanding the build-time configuration is critical for grasping how BPF programs are compiled and executed, so I want to be certain I have all the pieces in place.



12/2 2026/09/27 09:13 tool grepper
0m Error:
no matches

Args:
{
  "Expression": "BPF_JIT",
  "PathPrefix": "init/Kconfig"
}

Results:
{
  "error": "no matches"
}

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

Args:
null

Results:
null

Tokens:
							input: 31006
							output: 20
							thoughts: 16
						

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

**Hunting Down BPF_JIT Configuration**

Okay, so I need to track down the configuration option related to the BPF JIT, specifically `CONFIG_BPF_JIT`. My mental map immediately goes to the kernel source tree, of course. I need to figure out where that flag is defined, how it's used, and probably where the associated logic lives. So the first step is a search; I need to quickly scan the entire codebase to locate where `CONFIG_BPF_JIT` is referenced. Time to fire up my search tools! I'll probably start with a simple `grep` or similar to get a high-level view and then refine my search. This is going to give me a broad understanding before I dive into details.



14/2 2026/09/27 09:13 tool grepper
0m Args:
{
  "Expression": "config BPF_JIT"
}

Results:
{
  "Output": "kernel/bpf/Kconfig=27=config BPF_SYSCALL\n--\nkernel/bpf/Kconfig-41-\nkernel/bpf/Kconfig:42:config BPF_JIT\nkernel/bpf/Kconfig-43-\tbool \"Enable BPF Just In Time compiler\"\n--\nkernel/bpf/Kconfig-57-\nkernel/bpf/Kconfig:58:config BPF_JIT_ALWAYS_ON\nkernel/bpf/Kconfig-59-\tbool \"Permanently enable BPF JIT and remove BPF interpreter\"\n--\nkernel/bpf/Kconfig-68-\nkernel/bpf/Kconfig:69:config BPF_JIT_DEFAULT_ON\nkernel/bpf/Kconfig-70-\tdef_bool ARCH_WANT_DEFAULT_BPF_JIT || BPF_JIT_ALWAYS_ON\n"
}

15/2 2026/09/27 09:13 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 31437
							output: 34
							thoughts: 22
						

Thoughts:
Here's my summary, as if I were thinking these thoughts:

**Examining the BPF Kconfig Configuration**

Okay, let's dive into this. I need to refresh my understanding of the BPF configuration options. I'm going to jump into `kernel/bpf/Kconfig`, specifically focusing on lines 25 to 60. I know this file is where the core configuration options related to BPF are defined, and I'm guessing that area will deal with the fundamental enabling/disabling of the BPF subsystem, perhaps initial feature flags, or maybe some core infrastructure selections. I'm expecting to see dependencies and default values declared – crucial for understanding how these options interact within the broader kernel build configuration. Let's see what we find.



16/2 2026/09/27 09:13 tool read-file
0m Args:
{
  "File": "kernel/bpf/Kconfig",
  "FirstLine": 25,
  "LineCount": 40
}

Results:
{
  "Contents": "  25:\tmenu \"BPF subsystem\"\n  26:\t\n  27:\tconfig BPF_SYSCALL\n  28:\t\tbool \"Enable bpf() system call\"\n  29:\t\tselect BPF\n  30:\t\tselect IRQ_WORK\n  31:\t\tselect NEED_TASKS_RCU\n  32:\t\tselect TASKS_TRACE_RCU\n  33:\t\tselect BINARY_PRINTF\n  34:\t\tselect NET_SOCK_MSG if NET\n  35:\t\tselect NET_XGRESS if NET\n  36:\t\tselect PAGE_POOL if NET\n  37:\t\tdefault n\n  38:\t\thelp\n  39:\t\t  Enable the bpf() system call that allows to manipulate BPF programs\n  40:\t\t  and maps via file descriptors.\n  41:\t\n  42:\tconfig BPF_JIT\n  43:\t\tbool \"Enable BPF Just In Time compiler\"\n  44:\t\tdepends on BPF\n  45:\t\tdepends on HAVE_CBPF_JIT || HAVE_EBPF_JIT\n  46:\t\tselect EXECMEM\n  47:\t\thelp\n  48:\t\t  BPF programs are normally handled by a BPF interpreter. This option\n  49:\t\t  allows the kernel to generate native code when a program is loaded\n  50:\t\t  into the kernel. This will significantly speed-up processing of BPF\n  51:\t\t  programs.\n  52:\t\n  53:\t\t  Note, an admin should enable this feature changing:\n  54:\t\t  /proc/sys/net/core/bpf_jit_enable\n  55:\t\t  /proc/sys/net/core/bpf_jit_harden   (optional)\n  56:\t\t  /proc/sys/net/core/bpf_jit_kallsyms (optional)\n  57:\t\n  58:\tconfig BPF_JIT_ALWAYS_ON\n  59:\t\tbool \"Permanently enable BPF JIT and remove BPF interpreter\"\n  60:\t\tdepends on BPF_SYSCALL \u0026\u0026 HAVE_EBPF_JIT \u0026\u0026 BPF_JIT\n  61:\t\thelp\n  62:\t\t  Enables BPF JIT and removes BPF interpreter to avoid speculative\n  63:\t\t  execution of BPF instructions by the interpreter.\n  64:\t\n"
}

17/2 2026/09/27 09:13 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 32239
							output: 28
							thoughts: 50
						

Thoughts:
Okay, let's dive into this BPF trampoline question.

**Investigating BPF Trampoline Configuration Options**

Alright, I need to get a clear picture of how BPF trampolines are enabled and configured within this kernel. My initial thought is to check for specific configuration options, specifically if there's anything directly named `BPF_TRAMPOLINE` in the kernel configuration files. Since I'm working with BPF, the logical place to start looking is within the `kernel/bpf/Kconfig` file. So, let's fire up a `grep` and see what that Kconfig file reveals about anything related to "TRAMPOLINE." Hopefully this will give me some insight into how this functionality is controlled within the system.



18/2 2026/09/27 09:13 tool grepper
0m Args:
{
  "Expression": "TRAMP",
  "PathPrefix": "kernel/bpf/"
}

Results:
{
  "Output": "kernel/bpf/bpf_struct_ops.c=17=struct bpf_struct_ops_value {\n--\nkernel/bpf/bpf_struct_ops.c-21-\nkernel/bpf/bpf_struct_ops.c:22:#define MAX_TRAMP_IMAGE_PAGES 8\nkernel/bpf/bpf_struct_ops.c-23-\nkernel/bpf/bpf_struct_ops.c=24=struct bpf_struct_ops_map {\n--\nkernel/bpf/bpf_struct_ops.c-40-\t */\nkernel/bpf/bpf_struct_ops.c:41:\tvoid *image_pages[MAX_TRAMP_IMAGE_PAGES];\nkernel/bpf/bpf_struct_ops.c-42-\t/* The owner moduler's btf. */\n--\nkernel/bpf/bpf_struct_ops.c=637=int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_nodes *tnodes,\n--\nkernel/bpf/bpf_struct_ops.c-643-{\nkernel/bpf/bpf_struct_ops.c:644:\tu32 image_off = *_image_off, flags = BPF_TRAMP_F_INDIRECT;\nkernel/bpf/bpf_struct_ops.c-645-\tvoid *image = *_image;\n--\nkernel/bpf/bpf_struct_ops.c-647-\nkernel/bpf/bpf_struct_ops.c:648:\ttnodes[BPF_TRAMP_FENTRY].nodes[0] = node;\nkernel/bpf/bpf_struct_ops.c:649:\ttnodes[BPF_TRAMP_FENTRY].nr_nodes = 1;\nkernel/bpf/bpf_struct_ops.c-650-\nkernel/bpf/bpf_struct_ops.c-651-\tif (model-\u003eret_size \u003e 0)\nkernel/bpf/bpf_struct_ops.c:652:\t\tflags |= BPF_TRAMP_F_RET_FENTRY_RET;\nkernel/bpf/bpf_struct_ops.c-653-\n--\nkernel/bpf/bpf_struct_ops.c=726=static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,\n--\nkernel/bpf/bpf_struct_ops.c-762-\nkernel/bpf/bpf_struct_ops.c:763:\ttnodes = kzalloc_objs(*tnodes, BPF_TRAMP_MAX);\nkernel/bpf/bpf_struct_ops.c-764-\tif (!tnodes)\n--\nkernel/bpf/bpf_struct_ops.c-879-\t\t\t\t\t\t\u0026image, \u0026image_off,\nkernel/bpf/bpf_struct_ops.c:880:\t\t\t\t\t\tst_map-\u003eimage_pages_cnt \u003c MAX_TRAMP_IMAGE_PAGES);\nkernel/bpf/bpf_struct_ops.c-881-\t\tif (err)\n--\nkernel/bpf/trampoline.c=20=const struct bpf_prog_ops bpf_extension_prog_ops = {\n--\nkernel/bpf/trampoline.c-23-/* btf_vmlinux has ~22k attachable functions. 1k htab is enough. */\nkernel/bpf/trampoline.c:24:#define TRAMPOLINE_HASH_BITS 10\nkernel/bpf/trampoline.c:25:#define TRAMPOLINE_TABLE_SIZE (1 \u003c\u003c TRAMPOLINE_HASH_BITS)\nkernel/bpf/trampoline.c-26-\nkernel/bpf/trampoline.c:27:static struct hlist_head trampoline_key_table[TRAMPOLINE_TABLE_SIZE];\nkernel/bpf/trampoline.c:28:static struct hlist_head trampoline_ip_table[TRAMPOLINE_TABLE_SIZE];\nkernel/bpf/trampoline.c-29-\n--\nkernel/bpf/trampoline.c=31=static DEFINE_MUTEX(trampoline_mutex);\n--\nkernel/bpf/trampoline.c-38- */\nkernel/bpf/trampoline.c:39:#define TRAMPOLINE_LOCKS_BITS 5\nkernel/bpf/trampoline.c:40:#define TRAMPOLINE_LOCKS_TABLE_SIZE (1 \u003c\u003c TRAMPOLINE_LOCKS_BITS)\nkernel/bpf/trampoline.c-41-\nkernel/bpf/trampoline.c=42=static struct {\n--\nkernel/bpf/trampoline.c-44-\tstruct lock_class_key key;\nkernel/bpf/trampoline.c:45:} trampoline_locks[TRAMPOLINE_LOCKS_TABLE_SIZE];\nkernel/bpf/trampoline.c-46-\nkernel/bpf/trampoline.c=47=static struct mutex *select_trampoline_lock(struct bpf_trampoline *tr)\nkernel/bpf/trampoline.c-48-{\nkernel/bpf/trampoline.c:49:\treturn \u0026trampoline_locks[hash_ptr(tr, TRAMPOLINE_LOCKS_BITS)].mutex;\nkernel/bpf/trampoline.c-50-}\n--\nkernel/bpf/trampoline.c=75=static struct bpf_trampoline *direct_ops_ip_lookup(struct ftrace_ops *ops, unsigned long ip)\n--\nkernel/bpf/trampoline.c-80-\tmutex_lock(\u0026trampoline_mutex);\nkernel/bpf/trampoline.c:81:\thead_ip = \u0026trampoline_ip_table[hash_64(ip, TRAMPOLINE_HASH_BITS)];\nkernel/bpf/trampoline.c-82-\thlist_for_each_entry(tr, head_ip, hlist_ip) {\n--\nkernel/bpf/trampoline.c=98=static int bpf_tramp_ftrace_ops_func(struct ftrace_ops *ops, unsigned long ip,\n--\nkernel/bpf/trampoline.c-118-\t\t */\nkernel/bpf/trampoline.c:119:\t\tif ((tr-\u003eflags \u0026 BPF_TRAMP_F_CALL_ORIG) \u0026\u0026\nkernel/bpf/trampoline.c:120:\t\t    !(tr-\u003eflags \u0026 BPF_TRAMP_F_ORIG_STACK)) {\nkernel/bpf/trampoline.c:121:\t\t\tif (WARN_ON_ONCE(tr-\u003eflags \u0026 BPF_TRAMP_F_SHARE_IPMODIFY))\nkernel/bpf/trampoline.c-122-\t\t\t\treturn -EBUSY;\nkernel/bpf/trampoline.c-123-\nkernel/bpf/trampoline.c:124:\t\t\ttr-\u003eflags |= BPF_TRAMP_F_SHARE_IPMODIFY;\nkernel/bpf/trampoline.c-125-\t\t\treturn -EAGAIN;\n--\nkernel/bpf/trampoline.c-152-\tcase FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER:\nkernel/bpf/trampoline.c:153:\t\ttr-\u003eflags |= BPF_TRAMP_F_SHARE_IPMODIFY;\nkernel/bpf/trampoline.c-154-\nkernel/bpf/trampoline.c:155:\t\tif ((tr-\u003eflags \u0026 BPF_TRAMP_F_CALL_ORIG) \u0026\u0026\nkernel/bpf/trampoline.c:156:\t\t    !(tr-\u003eflags \u0026 BPF_TRAMP_F_ORIG_STACK))\nkernel/bpf/trampoline.c-157-\t\t\tret = bpf_trampoline_update(tr, false /* lock_direct_mutex */,\n--\nkernel/bpf/trampoline.c-160-\tcase FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER:\nkernel/bpf/trampoline.c:161:\t\ttr-\u003eflags \u0026= ~BPF_TRAMP_F_SHARE_IPMODIFY;\nkernel/bpf/trampoline.c-162-\nkernel/bpf/trampoline.c:163:\t\tif (tr-\u003eflags \u0026 BPF_TRAMP_F_ORIG_STACK)\nkernel/bpf/trampoline.c-164-\t\t\tret = bpf_trampoline_update(tr, false /* lock_direct_mutex */,\n--\nkernel/bpf/trampoline.c=373=static struct bpf_trampoline *bpf_trampoline_lookup(u64 key, unsigned long ip)\n--\nkernel/bpf/trampoline.c-379-\tmutex_lock(\u0026trampoline_mutex);\nkernel/bpf/trampoline.c:380:\thead = \u0026trampoline_key_table[hash_64(key, TRAMPOLINE_HASH_BITS)];\nkernel/bpf/trampoline.c-381-\thlist_for_each_entry(tr, head, hlist_key) {\n--\nkernel/bpf/trampoline.c-400-\thlist_add_head(\u0026tr-\u003ehlist_key, head);\nkernel/bpf/trampoline.c:401:\thead = \u0026trampoline_ip_table[hash_64(tr-\u003eip, TRAMPOLINE_HASH_BITS)];\nkernel/bpf/trampoline.c-402-\thlist_add_head(\u0026tr-\u003ehlist_ip, head);\n--\nkernel/bpf/trampoline.c-404-\tINIT_LIST_HEAD(\u0026tr-\u003eimages);\nkernel/bpf/trampoline.c:405:\tfor (i = 0; i \u003c BPF_TRAMP_MAX; i++)\nkernel/bpf/trampoline.c-406-\t\tINIT_HLIST_HEAD(\u0026tr-\u003eprogs_hlist[i]);\n--\nkernel/bpf/trampoline.c=509=bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total, bool *ip_arg)\n--\nkernel/bpf/trampoline.c-515-\t*total = 0;\nkernel/bpf/trampoline.c:516:\ttnodes = kzalloc_objs(*tnodes, BPF_TRAMP_MAX);\nkernel/bpf/trampoline.c-517-\tif (!tnodes)\n--\nkernel/bpf/trampoline.c-519-\nkernel/bpf/trampoline.c:520:\tfor (kind = 0; kind \u003c BPF_TRAMP_MAX; kind++) {\nkernel/bpf/trampoline.c-521-\t\ttnodes[kind].nr_nodes = tr-\u003eprogs_cnt[kind];\n--\nkernel/bpf/trampoline.c=539=u64 bpf_tramp_arena_base(const struct btf_func_model *m,\n--\nkernel/bpf/trampoline.c-544-\nkernel/bpf/trampoline.c:545:\tif (!(flags \u0026 BPF_TRAMP_F_INDIRECT) ||\nkernel/bpf/trampoline.c:546:\t    tnodes[BPF_TRAMP_FENTRY].nr_nodes != 1)\nkernel/bpf/trampoline.c-547-\t\treturn 0;\n--\nkernel/bpf/trampoline.c-555-\t/* Verification rejects an arena argument without an arena. */\nkernel/bpf/trampoline.c:556:\tprog = tnodes[BPF_TRAMP_FENTRY].nodes[0]-\u003elink-\u003eprog;\nkernel/bpf/trampoline.c-557-\tif (WARN_ON_ONCE(!prog-\u003eaux-\u003earena))\n--\nkernel/bpf/trampoline.c=719=static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex,\n--\nkernel/bpf/trampoline.c-737-\t/* clear all bits except SHARE_IPMODIFY and TAIL_CALL_CTX */\nkernel/bpf/trampoline.c:738:\ttr-\u003eflags \u0026= (BPF_TRAMP_F_SHARE_IPMODIFY | BPF_TRAMP_F_TAIL_CALL_CTX);\nkernel/bpf/trampoline.c-739-\nkernel/bpf/trampoline.c:740:\tif (tnodes[BPF_TRAMP_FEXIT].nr_nodes ||\nkernel/bpf/trampoline.c:741:\t    tnodes[BPF_TRAMP_MODIFY_RETURN].nr_nodes) {\nkernel/bpf/trampoline.c:742:\t\t/* NOTE: BPF_TRAMP_F_RESTORE_REGS and BPF_TRAMP_F_SKIP_FRAME\nkernel/bpf/trampoline.c-743-\t\t * should not be set together.\nkernel/bpf/trampoline.c-744-\t\t */\nkernel/bpf/trampoline.c:745:\t\ttr-\u003eflags |= BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME;\nkernel/bpf/trampoline.c-746-\t} else {\nkernel/bpf/trampoline.c:747:\t\ttr-\u003eflags |= BPF_TRAMP_F_RESTORE_REGS;\nkernel/bpf/trampoline.c-748-\t}\n--\nkernel/bpf/trampoline.c-750-\tif (ip_arg)\nkernel/bpf/trampoline.c:751:\t\ttr-\u003eflags |= BPF_TRAMP_F_IP_ARG;\nkernel/bpf/trampoline.c-752-\n--\nkernel/bpf/trampoline.c-754-again:\nkernel/bpf/trampoline.c:755:\tif (tr-\u003eflags \u0026 BPF_TRAMP_F_CALL_ORIG) {\nkernel/bpf/trampoline.c:756:\t\tif (tr-\u003eflags \u0026 BPF_TRAMP_F_SHARE_IPMODIFY) {\nkernel/bpf/trampoline.c:757:\t\t\t/* The BPF_TRAMP_F_SKIP_FRAME can be cleared in the\nkernel/bpf/trampoline.c-758-\t\t\t * first try, reset it in the second try.\nkernel/bpf/trampoline.c-759-\t\t\t */\nkernel/bpf/trampoline.c:760:\t\t\ttr-\u003eflags |= BPF_TRAMP_F_ORIG_STACK | BPF_TRAMP_F_SKIP_FRAME;\nkernel/bpf/trampoline.c-761-\t\t} else if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_JMP)) {\n--\nkernel/bpf/trampoline.c-765-\t\t\t */\nkernel/bpf/trampoline.c:766:\t\t\ttr-\u003eflags \u0026= ~BPF_TRAMP_F_SKIP_FRAME;\nkernel/bpf/trampoline.c-767-\t\t}\n--\nkernel/bpf/trampoline.c-787-\t}\nkernel/bpf/trampoline.c:788:\tim-\u003ecall_orig = tr-\u003eflags \u0026 BPF_TRAMP_F_CALL_ORIG;\nkernel/bpf/trampoline.c-789-\n--\nkernel/bpf/trampoline.c-809-\t\t/* -EAGAIN from bpf_tramp_ftrace_ops_func. Now\nkernel/bpf/trampoline.c:810:\t\t * BPF_TRAMP_F_SHARE_IPMODIFY is set, we can generate the\nkernel/bpf/trampoline.c-811-\t\t * trampoline again, and retry register.\n--\nkernel/bpf/trampoline.c=835=static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog)\n--\nkernel/bpf/trampoline.c-839-\tcase BPF_TRACE_FENTRY_MULTI:\nkernel/bpf/trampoline.c:840:\t\treturn BPF_TRAMP_FENTRY;\nkernel/bpf/trampoline.c-841-\tcase BPF_MODIFY_RETURN:\nkernel/bpf/trampoline.c:842:\t\treturn BPF_TRAMP_MODIFY_RETURN;\nkernel/bpf/trampoline.c-843-\tcase BPF_TRACE_FEXIT:\nkernel/bpf/trampoline.c-844-\tcase BPF_TRACE_FEXIT_MULTI:\nkernel/bpf/trampoline.c:845:\t\treturn BPF_TRAMP_FEXIT;\nkernel/bpf/trampoline.c-846-\tcase BPF_TRACE_FSESSION:\nkernel/bpf/trampoline.c-847-\tcase BPF_TRACE_FSESSION_MULTI:\nkernel/bpf/trampoline.c:848:\t\treturn BPF_TRAMP_FSESSION;\nkernel/bpf/trampoline.c-849-\tcase BPF_LSM_MAC:\n--\nkernel/bpf/trampoline.c-853-\t\t\t */\nkernel/bpf/trampoline.c:854:\t\t\treturn BPF_TRAMP_FEXIT;\nkernel/bpf/trampoline.c-855-\t\telse\nkernel/bpf/trampoline.c:856:\t\t\treturn BPF_TRAMP_MODIFY_RETURN;\nkernel/bpf/trampoline.c-857-\tdefault:\nkernel/bpf/trampoline.c:858:\t\treturn BPF_TRAMP_REPLACE;\nkernel/bpf/trampoline.c-859-\t}\n--\nkernel/bpf/trampoline.c=898=static int bpf_trampoline_add_prog(struct bpf_trampoline *tr,\n--\nkernel/bpf/trampoline.c-906-\tkind = bpf_attach_type_to_tramp(node-\u003elink-\u003eprog);\nkernel/bpf/trampoline.c:907:\tif (kind == BPF_TRAMP_FSESSION) {\nkernel/bpf/trampoline.c:908:\t\tprog_list = \u0026tr-\u003eprogs_hlist[BPF_TRAMP_FENTRY];\nkernel/bpf/trampoline.c-909-\t\tcnt++;\n--\nkernel/bpf/trampoline.c-912-\t}\nkernel/bpf/trampoline.c:913:\tif (cnt \u003e= BPF_MAX_TRAMP_LINKS)\nkernel/bpf/trampoline.c-914-\t\treturn -E2BIG;\n--\nkernel/bpf/trampoline.c-926-\tnode-\u003elink-\u003eprog-\u003eaux-\u003etramp_linked = true;\nkernel/bpf/trampoline.c:927:\tif (kind == BPF_TRAMP_FSESSION) {\nkernel/bpf/trampoline.c:928:\t\ttr-\u003eprogs_cnt[BPF_TRAMP_FENTRY]++;\nkernel/bpf/trampoline.c-929-\t\tfexit = fsession_exit(node);\n--\nkernel/bpf/trampoline.c-931-\t\t\treturn -EINVAL;\nkernel/bpf/trampoline.c:932:\t\thlist_add_head(\u0026fexit-\u003etramp_hlist, \u0026tr-\u003eprogs_hlist[BPF_TRAMP_FEXIT]);\nkernel/bpf/trampoline.c:933:\t\ttr-\u003eprogs_cnt[BPF_TRAMP_FEXIT]++;\nkernel/bpf/trampoline.c-934-\t} else {\n--\nkernel/bpf/trampoline.c=966=static void bpf_trampoline_remove_prog(struct bpf_trampoline *tr,\n--\nkernel/bpf/trampoline.c-972-\tkind = bpf_attach_type_to_tramp(node-\u003elink-\u003eprog);\nkernel/bpf/trampoline.c:973:\tif (kind == BPF_TRAMP_FSESSION) {\nkernel/bpf/trampoline.c-974-\t\tfexit = fsession_exit(node);\n--\nkernel/bpf/trampoline.c-977-\t\thlist_del_init(\u0026fexit-\u003etramp_hlist);\nkernel/bpf/trampoline.c:978:\t\ttr-\u003eprogs_cnt[BPF_TRAMP_FEXIT]--;\nkernel/bpf/trampoline.c:979:\t\tkind = BPF_TRAMP_FENTRY;\nkernel/bpf/trampoline.c-980-\t}\n--\nkernel/bpf/trampoline.c=986=static int __bpf_trampoline_link_prog(struct bpf_tramp_node *node,\n--\nkernel/bpf/trampoline.c-1009-\nkernel/bpf/trampoline.c:1010:\tfor (i = 0; i \u003c BPF_TRAMP_MAX; i++)\nkernel/bpf/trampoline.c-1011-\t\tcnt += tr-\u003eprogs_cnt[i];\nkernel/bpf/trampoline.c-1012-\nkernel/bpf/trampoline.c:1013:\tif (kind == BPF_TRAMP_REPLACE) {\nkernel/bpf/trampoline.c-1014-\t\t/* Cannot attach extension if fentry/fexit are in use. */\n--\nkernel/bpf/trampoline.c=1046=static int __bpf_trampoline_unlink_prog(struct bpf_tramp_node *node,\n--\nkernel/bpf/trampoline.c-1055-\tkind = bpf_attach_type_to_tramp(node-\u003elink-\u003eprog);\nkernel/bpf/trampoline.c:1056:\tif (kind == BPF_TRAMP_REPLACE) {\nkernel/bpf/trampoline.c-1057-\t\tWARN_ON_ONCE(!tr-\u003eextension_prog);\n--\nkernel/bpf/trampoline.c=1149=static struct bpf_shim_tramp_link *cgroup_shim_find(struct bpf_trampoline *tr,\n--\nkernel/bpf/trampoline.c-1154-\nkernel/bpf/trampoline.c:1155:\tfor (kind = 0; kind \u003c BPF_TRAMP_MAX; kind++) {\nkernel/bpf/trampoline.c-1156-\t\thlist_for_each_entry(node, \u0026tr-\u003eprogs_hlist[kind], tramp_hlist) {\n--\nkernel/bpf/trampoline.c=1278=void bpf_trampoline_put(struct bpf_trampoline *tr)\n--\nkernel/bpf/trampoline.c-1287-\nkernel/bpf/trampoline.c:1288:\tfor (i = 0; i \u003c BPF_TRAMP_MAX; i++)\nkernel/bpf/trampoline.c-1289-\t\tif (WARN_ON_ONCE(!hlist_empty(\u0026tr-\u003eprogs_hlist[i])))\n--\nkernel/bpf/trampoline.c=1565=static void trampoline_lock_all(void)\n--\nkernel/bpf/trampoline.c-1568-\nkernel/bpf/trampoline.c:1569:\tfor (i = 0; i \u003c TRAMPOLINE_LOCKS_TABLE_SIZE; i++)\nkernel/bpf/trampoline.c-1570-\t\tmutex_lock(\u0026trampoline_locks[i].mutex);\n--\nkernel/bpf/trampoline.c=1573=static void trampoline_unlock_all(void)\n--\nkernel/bpf/trampoline.c-1576-\nkernel/bpf/trampoline.c:1577:\tfor (i = 0; i \u003c TRAMPOLINE_LOCKS_TABLE_SIZE; i++)\nkernel/bpf/trampoline.c-1578-\t\tmutex_unlock(\u0026trampoline_locks[i].mutex);\n--\nkernel/bpf/trampoline.c=1850=static int __init init_trampolines(void)\n--\nkernel/bpf/trampoline.c-1853-\nkernel/bpf/trampoline.c:1854:\tfor (i = 0; i \u003c TRAMPOLINE_TABLE_SIZE; i++)\nkernel/bpf/trampoline.c-1855-\t\tINIT_HLIST_HEAD(\u0026trampoline_key_table[i]);\nkernel/bpf/trampoline.c:1856:\tfor (i = 0; i \u003c TRAMPOLINE_TABLE_SIZE; i++)\nkernel/bpf/trampoline.c-1857-\t\tINIT_HLIST_HEAD(\u0026trampoline_ip_table[i]);\nkernel/bpf/trampoline.c:1858:\tfor (i = 0; i \u003c TRAMPOLINE_LOCKS_TABLE_SIZE; i++)\nkernel/bpf/trampoline.c-1859-\t\t__mutex_init(\u0026trampoline_locks[i].mutex, \"trampoline_lock\", \u0026trampoline_locks[i].key);\n--\nkernel/bpf/verifier.c=20526=static int check_attach_btf_id(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-20619-\tif (tgt_prog \u0026\u0026 tgt_prog-\u003eaux-\u003etail_call_reachable)\nkernel/bpf/verifier.c:20620:\t\tbpf_trampoline_set_flags(tr, BPF_TRAMP_F_TAIL_CALL_CTX);\nkernel/bpf/verifier.c-20621-\n--\nkernel/bpf/verifier.c=20869=int bpf_fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-20972-\t\t *   {\nkernel/bpf/verifier.c:20973:\t\t *       return (((u64 *)ctx)[-1] \u003e\u003e BPF_TRAMP_IS_RETURN_SHIFT) \u0026 1;\nkernel/bpf/verifier.c-20974-\t\t *   }\n--\nkernel/bpf/verifier.c-20976-\t\tinsn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8);\nkernel/bpf/verifier.c:20977:\t\tinsn_buf[1] = BPF_ALU64_IMM(BPF_RSH, BPF_REG_0, BPF_TRAMP_IS_RETURN_SHIFT);\nkernel/bpf/verifier.c-20978-\t\tinsn_buf[2] = BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 1);\n--\nkernel/bpf/verifier.c-20986-\t\t *   {\nkernel/bpf/verifier.c:20987:\t\t *       u64 off = (((u64 *)ctx)[-1] \u003e\u003e BPF_TRAMP_COOKIE_INDEX_SHIFT) \u0026 0xFF;\nkernel/bpf/verifier.c-20988-\t\t *       return \u0026((u64 *)ctx)[-off];\n--\nkernel/bpf/verifier.c-20991-\t\tinsn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8);\nkernel/bpf/verifier.c:20992:\t\tinsn_buf[1] = BPF_ALU64_IMM(BPF_RSH, BPF_REG_0, BPF_TRAMP_COOKIE_INDEX_SHIFT);\nkernel/bpf/verifier.c-20993-\t\tinsn_buf[2] = BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 0xFF);\n"
}

19/2 2026/09/27 09:13 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 39473
							output: 35
							thoughts: 64
						

Thoughts:
Here's my thought process, breaking down the kernel configuration related to BPF JIT on x86:

**Delving into BPF JIT on x86**

Okay, so I'm digging into how BPF JIT works on x86, and I immediately remember that `BPF_JIT` needs both `BPF_SYSCALL` and `HAVE_EBPF_JIT`. That's the baseline, the dependencies I have to verify. My next step? I have to go check the architecture-specific Kconfig file. Right, that would be `arch/x86/Kconfig`. That's where I should find the `HAVE_EBPF_JIT` option defined, or at least a clue about it. Gotta confirm that's enabled.



20/2 2026/09/27 09:13 tool grepper
0m Args:
{
  "Expression": "HAVE_EBPF_JIT",
  "PathPrefix": "arch/x86/"
}

Results:
{
  "Output": "arch/x86/Kconfig=58=config X86\n--\narch/x86/Kconfig-237-\tselect HAVE_SAMPLE_FTRACE_DIRECT_MULTI\tif X86_64\narch/x86/Kconfig:238:\tselect HAVE_EBPF_JIT\narch/x86/Kconfig-239-\tselect HAVE_EFFICIENT_UNALIGNED_ACCESS\n"
}

21/2 2026/09/27 09:13 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 39862
							output: 31
							thoughts: 265
						

Thoughts:
**Analyzing BPF Program Release in Kernel Space**

Okay, let's dive into these recent `kernel/bpf/syscall.c` changes. My initial focus is on memory management, specifically how BPF programs are released. I see a new function, `__bpf_prog_put_rcu_tasks`, and a `__bpf_prog_put_noref` function, both involved in releasing BPF programs. The critical piece here is the conditional logic: `if (IS_ENABLED(CONFIG_TASKS_RCU))`. This is intriguing because it ties the BPF program release to the `CONFIG_TASKS_RCU` kernel configuration option. This suggests that the release mechanism is potentially tied to tasks RCU (Read-Copy Update) mechanism, which affects how programs are de-allocated based on the state of the tasks running at that time. Given my expertise, I need to understand this connection thoroughly. I need to understand when and why `CONFIG_TASKS_RCU` is enabled and what the implications are for BPF program lifetime and memory management. It's a key detail to fully appreciate the new code.



22/2 2026/09/27 09:13 tool grepper
0m Args:
{
  "Expression": "config TASKS_RCU",
  "PathPrefix": "kernel/rcu/"
}

Results:
{
  "Output": "kernel/rcu/Kconfig=81=config NEED_SRCU_NMI_SAFE\n--\nkernel/rcu/Kconfig-83-\nkernel/rcu/Kconfig:84:config TASKS_RCU_GENERIC\nkernel/rcu/Kconfig-85-\tdef_bool TASKS_RCU || TASKS_RUDE_RCU\n--\nkernel/rcu/Kconfig=101=config NEED_TASKS_RCU\n--\nkernel/rcu/Kconfig-104-\nkernel/rcu/Kconfig:105:config TASKS_RCU\nkernel/rcu/Kconfig-106-\tbool\n"
}

23/2 2026/09/27 09:13 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 40571
							output: 35
							thoughts: 66
						

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

**Diving into BPF and RCU Dependencies**

Okay, so I'm digging into this BPF code, and the `BPF_SYSCALL` selection is immediately interesting. It's pulling in both `NEED_TASKS_RCU` and `TASKS_TRACE_RCU`. That suggests a strong connection to RCU (Read-Copy Update) mechanisms, which is expected given the context. Now I need to understand *why*. My first instinct is to explore the RCU configuration options directly. I need to understand what those two options represent at a deeper level.

Let's quickly check `kernel/rcu/Kconfig` to see the dependencies and what they control. Specifically, I'll focus on lines 100-120. That should give me the core context. I'm expecting to find the key descriptions for `NEED_TASKS_RCU` and `TASKS_TRACE_RCU` there, which I can then correlate with the relevant BPF code and figure out how they interrelate. Hopefully, this section will shed light on why `BPF_SYSCALL` is mandating this RCU interaction, especially given the kernel's architecture and potential concurrent data manipulation, which would necessitate RCU in the first place.



24/2 2026/09/27 09:13 tool read-file
0m Args:
{
  "File": "kernel/rcu/Kconfig",
  "FirstLine": 100,
  "LineCount": 25
}

Results:
{
  "Contents": " 100:\t\n 101:\tconfig NEED_TASKS_RCU\n 102:\t\tbool\n 103:\t\tdefault n\n 104:\t\n 105:\tconfig TASKS_RCU\n 106:\t\tbool\n 107:\t\tdefault NEED_TASKS_RCU \u0026\u0026 PREEMPTION\n 108:\t\tselect IRQ_WORK\n 109:\t\n 110:\tconfig FORCE_TASKS_RUDE_RCU\n 111:\t\tbool \"Force selection of Tasks Rude RCU\"\n 112:\t\tdepends on RCU_EXPERT\n 113:\t\tselect TASKS_RUDE_RCU\n 114:\t\tdefault n\n 115:\t\thelp\n 116:\t\t  This option force-enables a task-based RCU implementation\n 117:\t\t  that uses only context switch (including preemption) and\n 118:\t\t  user-mode execution as quiescent states.  It forces IPIs and\n 119:\t\t  context switches on all online CPUs, including idle ones,\n 120:\t\t  so use with caution.\tNot for manual selection in most cases.\n 121:\t\n 122:\tconfig TASKS_RUDE_RCU\n 123:\t\tbool\n 124:\t\tdefault n\n"
}

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

Args:
null

Results:
null

Tokens:
							input: 41305
							output: 31
							thoughts: 58
						

Thoughts:
Here's my take on the thought process, summarized as you requested:

**Assessing Kernel Configuration Requirements for BPF Trampolines**

Okay, so `TASKS_RCU` has a default configuration based on `NEED_TASKS_RCU` and `PREEMPTION`. Noted. Now, I'm trying to recall the exact dependencies of BPF trampolines. I seem to recall they require `DYNAMIC_FTRACE_WITH_DIRECT_CALLS` or something similar, don't they? I should probably double-check that. Let's dig into the kernel config to pinpoint exactly when BPF trampoline functionality is supported and enabled. It's important to have this information clear in my mind when I'm assessing system capabilities or debugging potential issues.



26/2 2026/09/27 09:13 tool grepper
0m Args:
{
  "Expression": "bpf_trampoline",
  "PathPrefix": "arch/x86/"
}

Results:
{
  "Output": "arch/x86/net/bpf_jit_comp.c=3097=static void save_args(const struct btf_func_model *m, u8 **prog,\n--\narch/x86/net/bpf_jit_comp.c-3101-\tint arg_regs, first_off = 0, nr_regs = 0, nr_stack_slots = 0;\narch/x86/net/bpf_jit_comp.c:3102:\tbool use_jmp = bpf_trampoline_use_jmp(flags);\narch/x86/net/bpf_jit_comp.c-3103-\tint stack_args_off = (use_jmp || (flags \u0026 BPF_TRAMP_F_INDIRECT)) ? 16 : 24;\n--\narch/x86/net/bpf_jit_comp.c=3219=static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,\n--\narch/x86/net/bpf_jit_comp.c-3240-\t * bpf_tramp_run_ctx is already preserved by\narch/x86/net/bpf_jit_comp.c:3241:\t * arch_prepare_bpf_trampoline().\narch/x86/net/bpf_jit_comp.c-3242-\t *\n--\narch/x86/net/bpf_jit_comp.c-3254-\narch/x86/net/bpf_jit_comp.c:3255:\tif (emit_rsb_call(\u0026prog, bpf_trampoline_enter(p), image + (prog - (u8 *)rw_image)))\narch/x86/net/bpf_jit_comp.c-3256-\t\treturn -EINVAL;\n--\narch/x86/net/bpf_jit_comp.c-3305-\t\tEMIT4(0x48, 0x8D, 0x55, -run_ctx_off);\narch/x86/net/bpf_jit_comp.c:3306:\tif (emit_rsb_call(\u0026prog, bpf_trampoline_exit(p), image + (prog - (u8 *)rw_image)))\narch/x86/net/bpf_jit_comp.c-3307-\t\treturn -EINVAL;\n--\narch/x86/net/bpf_jit_comp.c=3365=static int invoke_bpf_mod_ret(const struct btf_func_model *m, u8 **pprog,\n--\narch/x86/net/bpf_jit_comp.c-3432- * eth_type_trans has 5 byte nop at the beginning. These 5 bytes will be\narch/x86/net/bpf_jit_comp.c:3433: * replaced with 'call generated_bpf_trampoline'. When it returns\narch/x86/net/bpf_jit_comp.c-3434- * eth_type_trans will continue executing with original skb and dev pointers.\n--\narch/x86/net/bpf_jit_comp.c-3467- */\narch/x86/net/bpf_jit_comp.c:3468:static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_image,\narch/x86/net/bpf_jit_comp.c-3469-\t\t\t\t\t void *rw_image_end, void *image,\n--\narch/x86/net/bpf_jit_comp.c-3569-\t\t */\narch/x86/net/bpf_jit_comp.c:3570:\t\tif (bpf_trampoline_use_jmp(flags)) {\narch/x86/net/bpf_jit_comp.c-3571-\t\t\t/* no rip in the \"jmp\" case */\n--\narch/x86/net/bpf_jit_comp.c-3772-\narch/x86/net/bpf_jit_comp.c:3773:void *arch_alloc_bpf_trampoline(unsigned int size)\narch/x86/net/bpf_jit_comp.c-3774-{\n--\narch/x86/net/bpf_jit_comp.c-3777-\narch/x86/net/bpf_jit_comp.c:3778:void arch_free_bpf_trampoline(void *image, unsigned int size)\narch/x86/net/bpf_jit_comp.c-3779-{\n--\narch/x86/net/bpf_jit_comp.c-3782-\narch/x86/net/bpf_jit_comp.c:3783:int arch_protect_bpf_trampoline(void *image, unsigned int size)\narch/x86/net/bpf_jit_comp.c-3784-{\n--\narch/x86/net/bpf_jit_comp.c-3787-\narch/x86/net/bpf_jit_comp.c:3788:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,\narch/x86/net/bpf_jit_comp.c-3789-\t\t\t\tconst struct btf_func_model *m, u32 flags,\n--\narch/x86/net/bpf_jit_comp.c-3803-\narch/x86/net/bpf_jit_comp.c:3804:\tret = __arch_prepare_bpf_trampoline(im, rw_image, rw_image + size, image, m,\narch/x86/net/bpf_jit_comp.c-3805-\t\t\t\t\t    flags, tnodes, func_addr);\n--\narch/x86/net/bpf_jit_comp.c-3816-\narch/x86/net/bpf_jit_comp.c:3817:int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\narch/x86/net/bpf_jit_comp.c-3818-\t\t\t     struct bpf_tramp_nodes *tnodes, void *func_addr)\n--\narch/x86/net/bpf_jit_comp.c-3823-\narch/x86/net/bpf_jit_comp.c:3824:\t/* Allocate a temporary buffer for __arch_prepare_bpf_trampoline().\narch/x86/net/bpf_jit_comp.c-3825-\t *\n--\narch/x86/net/bpf_jit_comp.c-3834-\narch/x86/net/bpf_jit_comp.c:3835:\tret = __arch_prepare_bpf_trampoline(\u0026im, image, image + PAGE_SIZE, image,\narch/x86/net/bpf_jit_comp.c-3836-\t\t\t\t\t    m, flags, tnodes, func_addr);\n"
}

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

Args:
null

Results:
null

Tokens:
							input: 43384
							output: 34
							thoughts: 20
						

Thoughts:
Okay, let's dive into this. Here's how I'd approach this, as though these are *my* thoughts:

**Analyzing `bpf_trampoline_link_prog` Usage**

Alright, the task at hand is to understand the context of `bpf_trampoline_link_prog`. My immediate instinct is to map its call locations. I want to see *where* this function is being invoked within the codebase. I need to understand its role within the larger BPF ecosystem. Is it a central point, or something more specialized? This knowledge of its call graph will immediately provide the most useful information.



28/2 2026/09/27 09:13 tool grepper
0m Args:
{
  "Expression": "bpf_trampoline_link_prog",
  "PathPrefix": "kernel/bpf/"
}

Results:
{
  "Output": "kernel/bpf/syscall.c=3651=static int bpf_tracing_prog_attach(struct bpf_prog *prog,\n--\nkernel/bpf/syscall.c-3757-\t\t * Allow re-attach for TRACING and LSM programs. If it's\nkernel/bpf/syscall.c:3758:\t\t * currently linked, bpf_trampoline_link_prog will fail.\nkernel/bpf/syscall.c-3759-\t\t * EXT programs need to specify tgt_prog_fd, so they\n--\nkernel/bpf/syscall.c-3831-\nkernel/bpf/syscall.c:3832:\terr = bpf_trampoline_link_prog(\u0026link-\u003elink.node, tr, tgt_prog);\nkernel/bpf/syscall.c-3833-\tif (err) {\n--\nkernel/bpf/trampoline.c=966=static void bpf_trampoline_remove_prog(struct bpf_trampoline *tr,\n--\nkernel/bpf/trampoline.c-985-\nkernel/bpf/trampoline.c:986:static int __bpf_trampoline_link_prog(struct bpf_tramp_node *node,\nkernel/bpf/trampoline.c-987-\t\t\t\t      struct bpf_trampoline *tr,\n--\nkernel/bpf/trampoline.c-1033-\nkernel/bpf/trampoline.c:1034:int bpf_trampoline_link_prog(struct bpf_tramp_node *node,\nkernel/bpf/trampoline.c-1035-\t\t\t     struct bpf_trampoline *tr,\n--\nkernel/bpf/trampoline.c-1040-\ttrampoline_lock(tr);\nkernel/bpf/trampoline.c:1041:\terr = __bpf_trampoline_link_prog(node, tr, tgt_prog, \u0026trampoline_ops, NULL);\nkernel/bpf/trampoline.c-1042-\ttrampoline_unlock(tr);\n--\nkernel/bpf/trampoline.c=1167=int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,\n--\nkernel/bpf/trampoline.c-1209-\nkernel/bpf/trampoline.c:1210:\terr = __bpf_trampoline_link_prog(\u0026shim_link-\u003elink.node, tr, NULL, \u0026trampoline_ops, NULL);\nkernel/bpf/trampoline.c-1211-\tif (err)\n--\nkernel/bpf/trampoline.c=1710=int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids,\n--\nkernel/bpf/trampoline.c-1759-\t\tdata-\u003eentry = \u0026mnode-\u003eentry;\nkernel/bpf/trampoline.c:1760:\t\terr = __bpf_trampoline_link_prog(\u0026mnode-\u003enode, mnode-\u003etrampoline, NULL,\nkernel/bpf/trampoline.c-1761-\t\t\t\t\t\t \u0026trampoline_multi_ops, data);\n"
}

29/2 2026/09/27 09:13 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 44361
							output: 34
							thoughts: 19
						

Thoughts:
Okay, here's my take on those thoughts, tailored for an expert audience:

**Investigating `bpf_trampoline_unlink_prog` and Its Callers**

Alright, so we're looking at `bpf_trampoline_unlink_prog`, and my initial instinct is to dig into its usage. It's a critical point, and understanding where it's called is paramount. I'm going to start by tracing the callers of this function. I need to get a clear picture of the contexts in which it's invoked. Knowing *why* this function gets called will give me insight into the control flow, possible dependencies, and any potential areas of concern that might arise during program unlinking. I expect that understanding its usage is critical to evaluating its security or stability.



30/2 2026/09/27 09:13 tool grepper
0m Args:
{
  "Expression": "bpf_trampoline_unlink_prog",
  "PathPrefix": "kernel/bpf/"
}

Results:
{
  "Output": "kernel/bpf/syscall.c=3585=static void bpf_tracing_link_release(struct bpf_link *link)\n--\nkernel/bpf/syscall.c-3590-\nkernel/bpf/syscall.c:3591:\terr = bpf_trampoline_unlink_prog(\u0026tr_link-\u003elink.node,\nkernel/bpf/syscall.c-3592-\t\t\t\t\ttr_link-\u003etrampoline,\nkernel/bpf/syscall.c-3593-\t\t\t\t\ttr_link-\u003etgt_prog);\nkernel/bpf/syscall.c:3594:\tWARN_ONCE(err, \"bpf_trampoline_unlink_prog failed: %d\\n\", err);\nkernel/bpf/syscall.c-3595-\n--\nkernel/bpf/trampoline.c=1034=int bpf_trampoline_link_prog(struct bpf_tramp_node *node,\n--\nkernel/bpf/trampoline.c-1045-\nkernel/bpf/trampoline.c:1046:static int __bpf_trampoline_unlink_prog(struct bpf_tramp_node *node,\nkernel/bpf/trampoline.c-1047-\t\t\t\t\tstruct bpf_trampoline *tr,\n--\nkernel/bpf/trampoline.c-1069-\nkernel/bpf/trampoline.c:1070:/* bpf_trampoline_unlink_prog() should never fail. */\nkernel/bpf/trampoline.c:1071:int bpf_trampoline_unlink_prog(struct bpf_tramp_node *node,\nkernel/bpf/trampoline.c-1072-\t\t\t       struct bpf_trampoline *tr,\n--\nkernel/bpf/trampoline.c-1077-\ttrampoline_lock(tr);\nkernel/bpf/trampoline.c:1078:\terr = __bpf_trampoline_unlink_prog(node, tr, tgt_prog, \u0026trampoline_ops, NULL);\nkernel/bpf/trampoline.c-1079-\ttrampoline_unlock(tr);\n--\nkernel/bpf/trampoline.c=1084=static void bpf_shim_tramp_link_release(struct bpf_link *link)\n--\nkernel/bpf/trampoline.c-1093-\nkernel/bpf/trampoline.c:1094:\terr = bpf_trampoline_unlink_prog(\u0026shim_link-\u003elink.node, shim_link-\u003etrampoline, NULL);\nkernel/bpf/trampoline.c:1095:\tWARN_ONCE(err, \"bpf_trampoline_unlink_prog failed: %d\\n\", err);\nkernel/bpf/trampoline.c-1096-\n--\nkernel/bpf/trampoline.c=1810=void bpf_trampoline_multi_detach(struct bpf_prog *prog,\n--\nkernel/bpf/trampoline.c-1821-\t\tbpf_trampoline_multi_attach_init(mnode-\u003etrampoline);\nkernel/bpf/trampoline.c:1822:\t\terr = __bpf_trampoline_unlink_prog(\u0026mnode-\u003enode, mnode-\u003etrampoline, NULL,\nkernel/bpf/trampoline.c-1823-\t\t\t\t\t\u0026trampoline_multi_ops, data);\nkernel/bpf/trampoline.c:1824:\t\tWARN_ONCE(err, \"__bpf_trampoline_unlink_prog failed: %d\\n\", err);\nkernel/bpf/trampoline.c-1825-\t}\n"
}

31/2 2026/09/27 09:13 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 45486
							output: 32
							thoughts: 1585
						

Thoughts:
**Analyzing BPF Trampoline Program Unlinking and Destruction**

Okay, let's break this down. My focus is on what happens during `bpf_trampoline_unlink_prog` and the subsequent program destruction, because I'm looking for a potential vulnerability here, specifically a use-after-free or race condition. I'm going through the core BPF and x86 code.

First, I see `bpf_trampoline_remove_prog` in `kernel/bpf/trampoline.c`. This function handles the removal of a BPF program from a trampoline. It seems to handle different program types (like `FSESSION` and `FEXIT`) and does some housekeeping with lists and counters within the `bpf_trampoline` structure. The key function here is `bpf_trampoline_skip_prog`, which is called after removing the program from lists and counters.

`bpf_trampoline_skip_prog` is where it gets interesting. This function iterates through `bpf_tramp_image` structures associated with the trampoline. For each image, it checks if the program to be removed is present in the "skips" array. If it is, `bpf_arch_text_poke` is called to modify the instruction at the specified offset within the code, changing it from a NOP to a jump instruction, skipping the BPF program that is being removed. This prevents the execution of the soon to be freed code. The `skip->prog = NULL` assignment is also important, as this effectively invalidates the entry in the `skips` array.

Now, I'm examining the program deallocation path, starting with `__bpf_prog_put_noref` in `kernel/bpf/syscall.c`. This function is responsible for cleaning up the BPF program. It handles BTF cleanup, LINFO cleanup, RCU-related actions and potentially calls `call_rcu_tasks` based on the program's sleepable state and trampoline linkage. The `rcu` operations are critical for ensuring that any tasks still referencing the program are completed before the memory is freed, but there is a call to `__bpf_prog_put_rcu` which I will need to understand.

`__bpf_tramp_image_put_deferred` in `kernel/bpf/trampoline.c` shows how trampoline images themselves are freed. This function, triggered by a work queue, removes the image from the trampoline's list and calls `bpf_tramp_image_free`, which handles the actual memory deallocation.

This patch is designed to prevent a use-after-free vulnerability that might occur when a BPF program is detached but tasks are still executing in an *older* trampoline image. Prior to this patch, the "skip" mechanism was applied to the current image, but it didn't solve the issue if tasks were still in *earlier* programs. The new approach is to have a NOP before each program call, which is then patched to skip the invocation.

Okay, this looks like a promising fuzz target. I need to think about how to trigger this, its impact, and the configurations needed.

*   **Reachability:** This is excellent. It is reachable on AMD64 in QEMU/GCE, meaning I can easily test it. I can trigger this through standard BPF syscall operations: loading, attaching (with `BPF_LINK_CREATE` or `BPF_RAW_TRACEPOINT_OPEN`), triggering (running code), detaching (closing file descriptors or using `LINK_DETACH`), and unloading programs.

*   **Impact:** This is significant. It modifies code patching (text poking), concurrency, RCU lifetime, list operations, memory allocation, and freeing for trampolines on AMD64 and core BPF. Subtle bugs in the text poking mechanism (incorrect offsets, incorrect values written), concurrency issues (race conditions related to image list iteration, reference counting), RCU grace periods, and reference counting (`tr->refcnt`, `tr->images`), or trampoline allocation/deallocation could lead to kernel panics, page faults, use-after-free, or deadlocks, which are exactly the types of bugs I like to find.

*   **Required configurations**: I need to ensure the necessary configurations are enabled, `BPF_SYSCALL`, `BPF_JIT` for sure. And `DYNAMIC_FTRACE_WITH_DIRECT_CALLS` or something like that is needed. I'll double-check `kernel/bpf/trampoline.c` to be sure. This all looks good. Time to start the fuzzing!



32/2 2026/09/27 09:13 tool grepper
0m Args:
{
  "Expression": "CONFIG_.*",
  "PathPrefix": "kernel/bpf/trampoline.c"
}

Results:
{
  "Output": "kernel/bpf/trampoline.c=62=struct bpf_trampoline_ops {\n--\nkernel/bpf/trampoline.c-68-\nkernel/bpf/trampoline.c:69:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/bpf/trampoline.c-70-static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex,\n--\nkernel/bpf/trampoline.c=72=static const struct bpf_trampoline_ops trampoline_ops;\nkernel/bpf/trampoline.c-73-\nkernel/bpf/trampoline.c:74:#ifdef CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS\nkernel/bpf/trampoline.c-75-static struct bpf_trampoline *direct_ops_ip_lookup(struct ftrace_ops *ops, unsigned long ip)\n--\nkernel/bpf/trampoline.c=92=static struct bpf_trampoline *direct_ops_ip_lookup(struct ftrace_ops *ops, unsigned long ip)\n--\nkernel/bpf/trampoline.c-95-}\nkernel/bpf/trampoline.c:96:#endif /* CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS */\nkernel/bpf/trampoline.c-97-\n--\nkernel/bpf/trampoline.c=210=void bpf_image_ksym_del(struct bpf_ksym *ksym)\n--\nkernel/bpf/trampoline.c-216-\nkernel/bpf/trampoline.c:217:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/bpf/trampoline.c:218:#ifdef CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS\nkernel/bpf/trampoline.c-219-/*\n--\nkernel/bpf/trampoline.c=337=static int direct_ops_mod(struct bpf_trampoline *tr, void *ptr, bool lock_direct_mutex)\n--\nkernel/bpf/trampoline.c-347-}\nkernel/bpf/trampoline.c:348:#endif /* CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS */\nkernel/bpf/trampoline.c-349-#else\n--\nkernel/bpf/trampoline.c=367=static int direct_ops_mod(struct bpf_trampoline *tr, void *ptr, bool lock_direct_mutex)\n--\nkernel/bpf/trampoline.c-370-}\nkernel/bpf/trampoline.c:371:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */\nkernel/bpf/trampoline.c-372-\n--\nkernel/bpf/trampoline.c=607=static void __bpf_tramp_image_put_rcu_tasks(struct rcu_head *rcu)\n--\nkernel/bpf/trampoline.c-612-\tif (im-\u003ecall_orig)\nkernel/bpf/trampoline.c:613:\t\t/* the case of fmod_ret/fexit trampoline and CONFIG_PREEMPTION=y */\nkernel/bpf/trampoline.c-614-\t\tpercpu_ref_kill(\u0026im-\u003epcref);\n--\nkernel/bpf/trampoline.c=620=static void bpf_tramp_image_put(struct bpf_tramp_image *im)\n--\nkernel/bpf/trampoline.c-634-\t * still in the image.\nkernel/bpf/trampoline.c:635:\t * In case of CONFIG_PREEMPT=y use call_rcu_tasks() to wait on\nkernel/bpf/trampoline.c-636-\t * first few asm instructions to execute and call into\n--\nkernel/bpf/trampoline.c-648-\tif (im-\u003ecall_orig) {\nkernel/bpf/trampoline.c:649:\t\tif (IS_ENABLED(CONFIG_TASKS_RCU))\nkernel/bpf/trampoline.c-650-\t\t\tcall_rcu_tasks(\u0026im-\u003ercu, __bpf_tramp_image_put_rcu_tasks);\n--\nkernel/bpf/trampoline.c=719=static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex,\n--\nkernel/bpf/trampoline.c-752-\nkernel/bpf/trampoline.c:753:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/bpf/trampoline.c-754-again:\n--\nkernel/bpf/trampoline.c-760-\t\t\ttr-\u003eflags |= BPF_TRAMP_F_ORIG_STACK | BPF_TRAMP_F_SKIP_FRAME;\nkernel/bpf/trampoline.c:761:\t\t} else if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_JMP)) {\nkernel/bpf/trampoline.c-762-\t\t\t/* Use \"jmp\" instead of \"call\" for the trampoline\n--\nkernel/bpf/trampoline.c-806-\nkernel/bpf/trampoline.c:807:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/bpf/trampoline.c-808-\tif (err == -EAGAIN) {\n--\nkernel/bpf/trampoline.c=1071=int bpf_trampoline_unlink_prog(struct bpf_tramp_node *node,\n--\nkernel/bpf/trampoline.c-1082-\nkernel/bpf/trampoline.c:1083:#if defined(CONFIG_CGROUP_BPF) \u0026\u0026 defined(CONFIG_BPF_LSM)\nkernel/bpf/trampoline.c-1084-static void bpf_shim_tramp_link_release(struct bpf_link *link)\n--\nkernel/bpf/trampoline.c=1555=int __weak arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\nkernel/bpf/trampoline.c-1560-\nkernel/bpf/trampoline.c:1561:#if defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) \u0026\u0026 \\\nkernel/bpf/trampoline.c:1562:    defined(CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS) \u0026\u0026 \\\nkernel/bpf/trampoline.c:1563:    defined(CONFIG_BPF_SYSCALL)\nkernel/bpf/trampoline.c-1564-\n--\nkernel/bpf/trampoline.c=1810=void bpf_trampoline_multi_detach(struct bpf_prog *prog,\n--\nkernel/bpf/trampoline.c-1845-\nkernel/bpf/trampoline.c:1846:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS \u0026\u0026\nkernel/bpf/trampoline.c:1847:\t  CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS \u0026\u0026\nkernel/bpf/trampoline.c:1848:\t  CONFIG_BPF_SYSCALL */\nkernel/bpf/trampoline.c-1849-\n"
}

33/2 2026/09/27 09:13 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 49906
							output: 25
							thoughts: 24
						

Thoughts:
Okay, here's my summary, tailored for an expert audience, presented as if it were my own thought process:

**Diving into `DYNAMIC_FTRACE_WITH_DIRECT_CALLS` Configuration**

Right, let's get down to brass tacks. I need to understand how the `DYNAMIC_FTRACE_WITH_DIRECT_CALLS` option is currently set up within the Kconfig system. Specifically, I'm thinking about the implications for direct function calls and how the ftrace infrastructure interacts with that, or doesn't. My focus will be on the impact of any changes to this config on the direct call usage and how the direct call usage impacts the ftrace usage. I'm already anticipating some performance nuances here, and perhaps some interesting interactions with various CPU features or instruction set architectures. It's time to dive into the config files and trace the dependencies, looking for any gotchas or unexpected behavior. This configuration option is critical for understanding the behavior of the system.



34/2 2026/09/27 09:13 tool grepper
0m Args:
{
  "Expression": "DYNAMIC_FTRACE_WITH_DIRECT_CALLS"
}

Results:
{
  "Output": "arch/arm64/Kconfig=2=config ARM64\n--\narch/arm64/Kconfig-189-\t\t    CLANG_SUPPORTS_DYNAMIC_FTRACE_WITH_ARGS)\narch/arm64/Kconfig:190:\tselect HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS \\\narch/arm64/Kconfig-191-\t\tif DYNAMIC_FTRACE_WITH_ARGS\n--\narch/arm64/include/asm/ftrace.h=71=struct __arch_ftrace_regs {\n--\narch/arm64/include/asm/ftrace.h-74-\narch/arm64/include/asm/ftrace.h:75:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/arm64/include/asm/ftrace.h-76-\tunsigned long direct_tramp;\n--\narch/arm64/include/asm/ftrace.h=172=void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,\n--\narch/arm64/include/asm/ftrace.h-175-\narch/arm64/include/asm/ftrace.h:176:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/arm64/include/asm/ftrace.h-177-static inline void arch_ftrace_set_direct_caller(struct ftrace_regs *fregs,\n--\narch/arm64/include/asm/ftrace.h-185-}\narch/arm64/include/asm/ftrace.h:186:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */\narch/arm64/include/asm/ftrace.h-187-\n--\narch/arm64/kernel/asm-offsets.c=27=int main(void)\n--\narch/arm64/kernel/asm-offsets.c-92-  DEFINE(FREGS_PC,\t\toffsetof(struct __arch_ftrace_regs, pc));\narch/arm64/kernel/asm-offsets.c:93:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/arm64/kernel/asm-offsets.c-94-  DEFINE(FREGS_DIRECT_TRAMP,\toffsetof(struct __arch_ftrace_regs, direct_tramp));\n--\narch/arm64/kernel/asm-offsets.c-181-  BLANK();\narch/arm64/kernel/asm-offsets.c:182:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/arm64/kernel/asm-offsets.c-183-  DEFINE(FTRACE_OPS_DIRECT_CALL,\toffsetof(struct ftrace_ops, direct_call));\n--\narch/arm64/kernel/entry-ftrace.S=36=SYM_CODE_START(ftrace_caller)\n--\narch/arm64/kernel/entry-ftrace.S-53-\narch/arm64/kernel/entry-ftrace.S:54:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/arm64/kernel/entry-ftrace.S-55-\t/*\n--\narch/arm64/kernel/entry-ftrace.S-76-\narch/arm64/kernel/entry-ftrace.S:77:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/arm64/kernel/entry-ftrace.S-78-\tstr\txzr, [sp, #FREGS_DIRECT_TRAMP]\n--\narch/arm64/kernel/entry-ftrace.S=110=SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)\n--\narch/arm64/kernel/entry-ftrace.S-128-\narch/arm64/kernel/entry-ftrace.S:129:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/arm64/kernel/entry-ftrace.S-130-\tldr\tx17, [sp, #FREGS_DIRECT_TRAMP]\n--\narch/arm64/kernel/entry-ftrace.S-142-\narch/arm64/kernel/entry-ftrace.S:143:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/arm64/kernel/entry-ftrace.S-144-SYM_INNER_LABEL(ftrace_caller_direct_late, SYM_L_LOCAL)\n--\narch/arm64/kernel/entry-ftrace.S=161=SYM_INNER_LABEL(ftrace_caller_direct, SYM_L_LOCAL)\n--\narch/arm64/kernel/entry-ftrace.S-168-\tbr\tx17\narch/arm64/kernel/entry-ftrace.S:169:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */\narch/arm64/kernel/entry-ftrace.S-170-SYM_CODE_END(ftrace_caller)\narch/arm64/kernel/entry-ftrace.S-171-\narch/arm64/kernel/entry-ftrace.S:172:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/arm64/kernel/entry-ftrace.S-173-SYM_CODE_START(ftrace_stub_direct_tramp)\n--\narch/arm64/kernel/entry-ftrace.S=178=SYM_CODE_END(ftrace_stub_direct_tramp)\narch/arm64/kernel/entry-ftrace.S:179:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */\narch/arm64/kernel/entry-ftrace.S-180-\n--\narch/arm64/kernel/ftrace.c=393=int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)\n--\narch/arm64/kernel/ftrace.c-412-#if defined(CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS) || \\\narch/arm64/kernel/ftrace.c:413:\tdefined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS)\narch/arm64/kernel/ftrace.c-414-int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,\n--\narch/loongarch/Kconfig=2=config LOONGARCH\n--\narch/loongarch/Kconfig-142-\tselect HAVE_FTRACE_REGS_HAVING_PT_REGS\narch/loongarch/Kconfig:143:\tselect HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/loongarch/Kconfig-144-\tselect HAVE_DYNAMIC_FTRACE_WITH_REGS\n--\narch/loongarch/include/asm/ftrace.h=71=void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,\n--\narch/loongarch/include/asm/ftrace.h-73-\narch/loongarch/include/asm/ftrace.h:74:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/loongarch/include/asm/ftrace.h-75-static inline void\narch/loongarch/include/asm/ftrace.h=76=__arch_ftrace_set_direct_caller(struct pt_regs *regs, unsigned long addr)\n--\narch/loongarch/include/asm/ftrace.h-82-\t__arch_ftrace_set_direct_caller(\u0026arch_ftrace_regs(fregs)-\u003eregs, addr)\narch/loongarch/include/asm/ftrace.h:83:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */\narch/loongarch/include/asm/ftrace.h-84-\n--\narch/loongarch/kernel/mcount_dyn.S=162=SYM_CODE_END(return_to_handler)\n--\narch/loongarch/kernel/mcount_dyn.S-164-\narch/loongarch/kernel/mcount_dyn.S:165:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/loongarch/kernel/mcount_dyn.S-166-SYM_CODE_START(ftrace_stub_direct_tramp)\n--\narch/loongarch/kernel/mcount_dyn.S=171=SYM_CODE_END(ftrace_stub_direct_tramp)\narch/loongarch/kernel/mcount_dyn.S:172:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */\n--\narch/powerpc/Kconfig=118=config PPC\n--\narch/powerpc/Kconfig-244-\tselect HAVE_DYNAMIC_FTRACE_WITH_CALL_OPS if PPC_FTRACE_OUT_OF_LINE || (PPC32 \u0026\u0026 ARCH_USING_PATCHABLE_FUNCTION_ENTRY)\narch/powerpc/Kconfig:245:\tselect HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS if HAVE_DYNAMIC_FTRACE_WITH_CALL_OPS\narch/powerpc/Kconfig-246-\tselect HAVE_DYNAMIC_FTRACE_WITH_REGS\tif ARCH_USING_PATCHABLE_FUNCTION_ENTRY || MPROFILE_KERNEL || PPC32\n--\narch/powerpc/Kconfig-289-\tselect HAVE_RUST\t\t\tif PPC64 \u0026\u0026 CPU_LITTLE_ENDIAN\narch/powerpc/Kconfig:290:\tselect HAVE_SAMPLE_FTRACE_DIRECT\tif HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/powerpc/Kconfig:291:\tselect HAVE_SAMPLE_FTRACE_DIRECT_MULTI\tif HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/powerpc/Kconfig-292-\tselect HAVE_SETUP_PER_CPU_AREA\t\tif PPC64\n--\narch/powerpc/include/asm/ftrace.h=157=unsigned long ftrace_call_adjust(unsigned long addr);\narch/powerpc/include/asm/ftrace.h-158-\narch/powerpc/include/asm/ftrace.h:159:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/powerpc/include/asm/ftrace.h-160-/*\n--\narch/powerpc/include/asm/ftrace.h=167=static inline void arch_ftrace_set_direct_caller(struct ftrace_regs *fregs, unsigned long addr)\n--\narch/powerpc/include/asm/ftrace.h-172-}\narch/powerpc/include/asm/ftrace.h:173:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */\narch/powerpc/include/asm/ftrace.h-174-#else\n--\narch/powerpc/kernel/asm-offsets.c=78=int main(void)\n--\narch/powerpc/kernel/asm-offsets.c-681-\tOFFSET(FTRACE_OPS_FUNC, ftrace_ops, func);\narch/powerpc/kernel/asm-offsets.c:682:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/powerpc/kernel/asm-offsets.c-683-\tOFFSET(FTRACE_OPS_DIRECT_CALL, ftrace_ops, direct_call);\n--\narch/powerpc/kernel/trace/ftrace.c=159=static int ftrace_get_call_inst(struct dyn_ftrace *rec, unsigned long addr, ppc_inst_t *call_inst)\n--\narch/powerpc/kernel/trace/ftrace.c-171-\t\t/* This can only happen with ftrace direct */\narch/powerpc/kernel/trace/ftrace.c:172:\t\tif (!IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS)) {\narch/powerpc/kernel/trace/ftrace.c-173-\t\t\tpr_err(\"0x%lx (0x%lx): Unexpected target address 0x%lx\\n\",\n--\narch/powerpc/kernel/trace/ftrace_entry.S-48-\narch/powerpc/kernel/trace/ftrace_entry.S:49:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/powerpc/kernel/trace/ftrace_entry.S-50-\t/* Load the ftrace_op */\n--\narch/powerpc/kernel/trace/ftrace_entry.S-131-\narch/powerpc/kernel/trace/ftrace_entry.S:132:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/powerpc/kernel/trace/ftrace_entry.S-133-\t/* Clear orig_gpr3 to later detect ftrace_direct call */\n--\narch/powerpc/kernel/trace/ftrace_entry.S-181-.macro\tftrace_regs_exit allregs\narch/powerpc/kernel/trace/ftrace_entry.S:182:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/powerpc/kernel/trace/ftrace_entry.S-183-\t/* Check orig_gpr3 to detect ftrace_direct call */\n--\narch/powerpc/kernel/trace/ftrace_entry.S-198-\narch/powerpc/kernel/trace/ftrace_entry.S:199:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/powerpc/kernel/trace/ftrace_entry.S-200-\tbeq\tcr1,2f\n--\narch/powerpc/kernel/trace/ftrace_entry.S-245-#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE\narch/powerpc/kernel/trace/ftrace_entry.S:246:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/powerpc/kernel/trace/ftrace_entry.S-247-\tbnectr\tcr1\n--\narch/powerpc/kernel/trace/ftrace_entry.S=289=ftrace_no_trace:\n--\narch/powerpc/kernel/trace/ftrace_entry.S-303-\narch/powerpc/kernel/trace/ftrace_entry.S:304:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/powerpc/kernel/trace/ftrace_entry.S-305-.Lftrace_direct_call_regs:\n--\narch/riscv/Kconfig=13=config RISCV\n--\narch/riscv/Kconfig-163-\tselect FUNCTION_ALIGNMENT_4B if DYNAMIC_FTRACE \u0026\u0026 RISCV_ISA_C\narch/riscv/Kconfig:164:\tselect HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS if HAVE_DYNAMIC_FTRACE_WITH_CALL_OPS\narch/riscv/Kconfig-165-\tselect HAVE_DYNAMIC_FTRACE_WITH_CALL_OPS if (DYNAMIC_FTRACE_WITH_ARGS \u0026\u0026 !CFI)\n--\narch/riscv/include/asm/ftrace.h=127=struct __arch_ftrace_regs {\n--\narch/riscv/include/asm/ftrace.h-132-\tunsigned long t1;\narch/riscv/include/asm/ftrace.h:133:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/riscv/include/asm/ftrace.h-134-\tunsigned long direct_tramp;\n--\narch/riscv/include/asm/ftrace.h=225=void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,\n--\narch/riscv/include/asm/ftrace.h-228-\narch/riscv/include/asm/ftrace.h:229:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/riscv/include/asm/ftrace.h-230-static inline void arch_ftrace_set_direct_caller(struct ftrace_regs *fregs, unsigned long addr)\n--\narch/riscv/include/asm/ftrace.h-233-}\narch/riscv/include/asm/ftrace.h:234:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */\narch/riscv/include/asm/ftrace.h-235-\n--\narch/riscv/kernel/asm-offsets.c=22=void asm_offsets(void)\n--\narch/riscv/kernel/asm-offsets.c-507-\tDEFINE(FTRACE_OPS_FUNC,\t\toffsetof(struct ftrace_ops, func));\narch/riscv/kernel/asm-offsets.c:508:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/riscv/kernel/asm-offsets.c-509-\tDEFINE(FTRACE_OPS_DIRECT_CALL,\toffsetof(struct ftrace_ops, direct_call));\narch/riscv/kernel/asm-offsets.c:510:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */\narch/riscv/kernel/asm-offsets.c-511-#endif\n--\narch/riscv/kernel/ftrace.c=208=unsigned long ftrace_call_adjust(unsigned long addr)\n--\narch/riscv/kernel/ftrace.c-213-\narch/riscv/kernel/ftrace.c:214:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/riscv/kernel/ftrace.c-215-int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,\n--\narch/riscv/kernel/mcount-dyn.S=154=SYM_FUNC_START(ftrace_caller)\n--\narch/riscv/kernel/mcount-dyn.S-175-\narch/riscv/kernel/mcount-dyn.S:176:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/riscv/kernel/mcount-dyn.S-177-\t/*\n--\narch/riscv/kernel/mcount-dyn.S=191=SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)\n--\narch/riscv/kernel/mcount-dyn.S-195-\tRESTORE_ABI_REGS\narch/riscv/kernel/mcount-dyn.S:196:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/riscv/kernel/mcount-dyn.S-197-\tbnez\tt1, ftrace_caller_direct\n--\narch/riscv/kernel/mcount-dyn.S-199-\tjr\tt0\narch/riscv/kernel/mcount-dyn.S:200:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/riscv/kernel/mcount-dyn.S-201-SYM_INNER_LABEL(ftrace_caller_direct, SYM_L_LOCAL)\n--\narch/riscv/kernel/mcount-dyn.S=204=SYM_FUNC_END(ftrace_caller)\narch/riscv/kernel/mcount-dyn.S-205-\narch/riscv/kernel/mcount-dyn.S:206:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/riscv/kernel/mcount-dyn.S-207-SYM_CODE_START(ftrace_stub_direct_tramp)\n--\narch/riscv/kernel/mcount-dyn.S=209=SYM_CODE_END(ftrace_stub_direct_tramp)\narch/riscv/kernel/mcount-dyn.S:210:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */\n--\narch/s390/Kconfig=78=config S390\n--\narch/s390/Kconfig-211-\tselect HAVE_FTRACE_REGS_HAVING_PT_REGS\narch/s390/Kconfig:212:\tselect HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/s390/Kconfig-213-\tselect HAVE_DYNAMIC_FTRACE_WITH_REGS\n--\narch/s390/include/asm/ftrace.h=82=ftrace_regs_get_return_address(const struct ftrace_regs *fregs)\n--\narch/s390/include/asm/ftrace.h-92-\narch/s390/include/asm/ftrace.h:93:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/s390/include/asm/ftrace.h-94-/*\n--\narch/s390/include/asm/ftrace.h=101=static inline void arch_ftrace_set_direct_caller(struct ftrace_regs *fregs, unsigned long addr)\n--\narch/s390/include/asm/ftrace.h-105-}\narch/s390/include/asm/ftrace.h:106:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */\narch/s390/include/asm/ftrace.h-107-\n--\narch/x86/Kconfig=58=config X86\n--\narch/x86/Kconfig-233-\tselect HAVE_FTRACE_REGS_HAVING_PT_REGS\tif X86_64\narch/x86/Kconfig:234:\tselect HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/x86/Kconfig-235-\tselect HAVE_DYNAMIC_FTRACE_WITH_JMP\tif X86_64\n--\narch/x86/Kconfig-341-\tselect ARCH_SUPPORTS_SYSCALL_USER_DISPATCH\narch/x86/Kconfig:342:\tselect HAVE_SINGLE_FTRACE_DIRECT_OPS\tif X86_64 \u0026\u0026 DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/x86/Kconfig-343-\n--\narch/x86/include/asm/ftrace.h=83=void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,\n--\narch/x86/include/asm/ftrace.h-88-\narch/x86/include/asm/ftrace.h:89:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\narch/x86/include/asm/ftrace.h-90-/*\n--\narch/x86/include/asm/ftrace.h=98=__arch_ftrace_set_direct_caller(struct pt_regs *regs, unsigned long addr)\n--\narch/x86/include/asm/ftrace.h-104-\t__arch_ftrace_set_direct_caller(\u0026arch_ftrace_regs(fregs)-\u003eregs, addr)\narch/x86/include/asm/ftrace.h:105:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */\narch/x86/include/asm/ftrace.h-106-\n--\ndrivers/hid/bpf/Kconfig=4=config HID_BPF\n--\ndrivers/hid/bpf/Kconfig-7-\tdepends on BPF_SYSCALL\ndrivers/hid/bpf/Kconfig:8:\tdepends on DYNAMIC_FTRACE_WITH_DIRECT_CALLS\ndrivers/hid/bpf/Kconfig-9-\thelp\n--\ninclude/linux/ftrace.h=449=struct ftrace_ops {\n--\ninclude/linux/ftrace.h-464-\tstruct ftrace_ops\t\t*managed;\ninclude/linux/ftrace.h:465:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\ninclude/linux/ftrace.h-466-\tunsigned long\t\t\tdirect_call;\n--\ninclude/linux/ftrace.h=536=struct ftrace_func_entry {\n--\ninclude/linux/ftrace.h-541-\ninclude/linux/ftrace.h:542:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\ninclude/linux/ftrace.h-543-unsigned long ftrace_find_rec_direct(unsigned long ip);\n--\ninclude/linux/ftrace.h=615=static inline void arch_ftrace_set_direct_caller(struct ftrace_regs *fregs,\ninclude/linux/ftrace.h-616-\t\t\t\t\t\t unsigned long addr) { }\ninclude/linux/ftrace.h:617:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */\ninclude/linux/ftrace.h-618-\n--\ninclude/linux/ftrace.h=1018=extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);\n--\ninclude/linux/ftrace.h-1021-\tdefined(CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS) || \\\ninclude/linux/ftrace.h:1022:\tdefined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS)\ninclude/linux/ftrace.h-1023-/**\n--\nkernel/bpf/trampoline.c=62=struct bpf_trampoline_ops {\n--\nkernel/bpf/trampoline.c-68-\nkernel/bpf/trampoline.c:69:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/bpf/trampoline.c-70-static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex,\n--\nkernel/bpf/trampoline.c=210=void bpf_image_ksym_del(struct bpf_ksym *ksym)\n--\nkernel/bpf/trampoline.c-216-\nkernel/bpf/trampoline.c:217:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/bpf/trampoline.c-218-#ifdef CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS\n--\nkernel/bpf/trampoline.c=367=static int direct_ops_mod(struct bpf_trampoline *tr, void *ptr, bool lock_direct_mutex)\n--\nkernel/bpf/trampoline.c-370-}\nkernel/bpf/trampoline.c:371:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */\nkernel/bpf/trampoline.c-372-\n--\nkernel/bpf/trampoline.c=719=static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex,\n--\nkernel/bpf/trampoline.c-752-\nkernel/bpf/trampoline.c:753:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/bpf/trampoline.c-754-again:\n--\nkernel/bpf/trampoline.c-806-\nkernel/bpf/trampoline.c:807:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/bpf/trampoline.c-808-\tif (err == -EAGAIN) {\n--\nkernel/bpf/trampoline.c=1555=int __weak arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\nkernel/bpf/trampoline.c-1560-\nkernel/bpf/trampoline.c:1561:#if defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) \u0026\u0026 \\\nkernel/bpf/trampoline.c-1562-    defined(CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS) \u0026\u0026 \\\n--\nkernel/bpf/trampoline.c=1810=void bpf_trampoline_multi_detach(struct bpf_prog *prog,\n--\nkernel/bpf/trampoline.c-1845-\nkernel/bpf/trampoline.c:1846:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS \u0026\u0026\nkernel/bpf/trampoline.c-1847-\t  CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS \u0026\u0026\n--\nkernel/trace/Kconfig=47=config HAVE_DYNAMIC_FTRACE_WITH_REGS\n--\nkernel/trace/Kconfig-49-\nkernel/trace/Kconfig:50:config HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/trace/Kconfig-51-\tbool\n--\nkernel/trace/Kconfig=324=config DYNAMIC_FTRACE_WITH_REGS\n--\nkernel/trace/Kconfig-328-\nkernel/trace/Kconfig:329:config DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/trace/Kconfig-330-\tdef_bool y\nkernel/trace/Kconfig-331-\tdepends on DYNAMIC_FTRACE_WITH_REGS || DYNAMIC_FTRACE_WITH_ARGS\nkernel/trace/Kconfig:332:\tdepends on HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/trace/Kconfig-333-\n--\nkernel/trace/Kconfig=343=config DYNAMIC_FTRACE_WITH_JMP\n--\nkernel/trace/Kconfig-345-\tdepends on DYNAMIC_FTRACE\nkernel/trace/Kconfig:346:\tdepends on DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/trace/Kconfig-347-\tdepends on HAVE_DYNAMIC_FTRACE_WITH_JMP\n--\nkernel/trace/bpf_trace.c=3680=__bpf_kfunc_end_defs();\nkernel/trace/bpf_trace.c-3681-\nkernel/trace/bpf_trace.c:3682:#if defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) \u0026\u0026 \\\nkernel/trace/bpf_trace.c-3683-    defined(CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS)\n--\nkernel/trace/bpf_trace.c=3930=int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr)\n--\nkernel/trace/bpf_trace.c-3934-\nkernel/trace/bpf_trace.c:3935:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS \u0026\u0026 CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS */\n--\nkernel/trace/fgraph.c=560=ftrace_push_return_trace(unsigned long ret, unsigned long func,\n--\nkernel/trace/fgraph.c-641-/* Make sure this only works without direct calls */\nkernel/trace/fgraph.c:642:# ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/trace/fgraph.c-643-#  error MCOUNT_INSN_SIZE not defined with direct calls enabled\n--\nkernel/trace/ftrace.c=2626=ftrace_find_unique_ops(struct dyn_ftrace *rec)\n--\nkernel/trace/ftrace.c-2643-\nkernel/trace/ftrace.c:2644:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/trace/ftrace.c-2645-/* Protected by rcu_tasks for reading, and direct_mutex for writing */\n--\nkernel/trace/ftrace.c=2665=static void call_direct_funcs(unsigned long ip, unsigned long pip,\n--\nkernel/trace/ftrace.c-2679-}\nkernel/trace/ftrace.c:2680:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */\nkernel/trace/ftrace.c-2681-\n--\nkernel/trace/ftrace.c=6012=ftrace_set_addr(struct ftrace_ops *ops, unsigned long *ips, unsigned int cnt,\n--\nkernel/trace/ftrace.c-6017-\nkernel/trace/ftrace.c:6018:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/trace/ftrace.c-6019-\n--\nkernel/trace/ftrace.c=6640=int update_ftrace_direct_mod(struct ftrace_ops *ops, struct ftrace_hash *hash, bool do_direct_lock)\n--\nkernel/trace/ftrace.c-6720-\nkernel/trace/ftrace.c:6721:#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */\nkernel/trace/ftrace.c-6722-\n--\nkernel/trace/ftrace.c=7800=static int ftrace_get_trampoline_kallsym(unsigned int symnum,\n--\nkernel/trace/ftrace.c-7820-\nkernel/trace/ftrace.c:7821:#if defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) || defined(CONFIG_MODULES)\nkernel/trace/ftrace.c-7822-/*\n--\nkernel/trace/ftrace.c=9106=int ftrace_is_dead(void)\n--\nkernel/trace/ftrace.c-9110-\nkernel/trace/ftrace.c:9111:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/trace/ftrace.c-9112-/*\n--\nkernel/trace/ftrace.c=9173=static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops *ops)\n--\nkernel/trace/ftrace.c-9213-\nkernel/trace/ftrace.c:9214:#else  /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */\nkernel/trace/ftrace.c-9215-\n--\nkernel/trace/ftrace.c=9221=static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops *ops)\n--\nkernel/trace/ftrace.c-9227-\nkernel/trace/ftrace.c:9228:#endif  /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */\nkernel/trace/ftrace.c-9229-\n--\nkernel/trace/trace_selftest.c=1048=static struct fgraph_ops fgraph_ops __initdata  = {\n--\nkernel/trace/trace_selftest.c-1052-\nkernel/trace/trace_selftest.c:1053:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/trace/trace_selftest.c-1054-static struct ftrace_ops direct;\n--\nkernel/trace/trace_selftest.c=1062=trace_selftest_startup_function_graph(struct tracer *trace,\n--\nkernel/trace/trace_selftest.c-1115-\nkernel/trace/trace_selftest.c:1116:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/trace/trace_selftest.c-1117-\t/*\n--\nsamples/Kconfig=32=config SAMPLE_FTRACE_DIRECT\nsamples/Kconfig-33-\ttristate \"Build register_ftrace_direct() example\"\nsamples/Kconfig:34:\tdepends on DYNAMIC_FTRACE_WITH_DIRECT_CALLS \u0026\u0026 m\nsamples/Kconfig-35-\tdepends on HAVE_SAMPLE_FTRACE_DIRECT\n--\nsamples/Kconfig=40=config SAMPLE_FTRACE_DIRECT_MULTI\nsamples/Kconfig-41-\ttristate \"Build register_ftrace_direct() on multiple ips example\"\nsamples/Kconfig:42:\tdepends on DYNAMIC_FTRACE_WITH_DIRECT_CALLS \u0026\u0026 m\nsamples/Kconfig-43-\tdepends on HAVE_SAMPLE_FTRACE_DIRECT_MULTI\n--\ntools/testing/selftests/hid/config=12=CONFIG_DEBUG_INFO_BTF=y\ntools/testing/selftests/hid/config:13:CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS=y\ntools/testing/selftests/hid/config-14-CONFIG_FPROBE=y\n"
}

35/2 2026/09/27 09:13 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 59159
							output: 33
							thoughts: 101
						

Thoughts:
Okay, let's break this down.

**Dependency Deep Dive**

Alright, I need to figure out the dependencies for `DYNAMIC_FTRACE_WITH_DIRECT_CALLS`. Let's see... it hinges on a couple of things: `DYNAMIC_FTRACE_WITH_REGS` OR `DYNAMIC_FTRACE_WITH_ARGS`, AND also, critically, `HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS`. So, that second one is likely a hardware or architecture feature check, something at the platform level that enables the functionality.

Now, `DYNAMIC_FTRACE_WITH_REGS`... that depends on `DYNAMIC_FTRACE` itself. Hmm, so we're building a dependency chain here. `DYNAMIC_FTRACE` is the foundation. Given it seems central, I better check out `DYNAMIC_FTRACE` in `kernel/trace/Kconfig` to see what _it_ depends on and what this whole tracing infrastructure is about. Need to understand the base before I can fully grasp the implications of direct calls.



36/2 2026/09/27 09:13 tool grepper
0m Args:
{
  "Expression": "config DYNAMIC_FTRACE$",
  "PathPrefix": "kernel/trace/Kconfig"
}

Results:
{
  "Output": "kernel/trace/Kconfig=290=config FUNCTION_TRACE_ARGS\n--\nkernel/trace/Kconfig-300-\nkernel/trace/Kconfig:301:config DYNAMIC_FTRACE\nkernel/trace/Kconfig-302-\tbool\n"
}

37/2 2026/09/27 09:13 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 59594
							output: 34
							thoughts: 23
						

Thoughts:
Okay, let's dive into this. Here's my take on those lines of thought:

**Diving into the Kernel Trace Configuration**

Alright, let's get down to brass tacks. I need to get a handle on how the kernel tracing features are configured, specifically around the area of `kernel/trace/Kconfig`, roughly lines 300 to 340. I know this area holds key configuration options, and I want a deeper understanding of what's being enabled or disabled there. I need to get a handle on what modules or settings are related to the core tracing functionality, and potentially identify areas for customization or optimization that I might need later. Time to grab my preferred editor and start dissecting that code block. Hopefully, I'll find a few things of interest to build a better overall picture.



38/2 2026/09/27 09:13 tool read-file
0m Args:
{
  "File": "kernel/trace/Kconfig",
  "FirstLine": 300,
  "LineCount": 40
}

Results:
{
  "Contents": " 300:\t\n 301:\tconfig DYNAMIC_FTRACE\n 302:\t\tbool\n 303:\t\tdepends on FUNCTION_TRACER\n 304:\t\tdepends on HAVE_DYNAMIC_FTRACE\n 305:\t\tdefault y\n 306:\t\thelp\n 307:\t\t  This option will modify all the calls to function tracing\n 308:\t\t  dynamically (will patch them out of the binary image and\n 309:\t\t  replace them with a No-Op instruction) on boot up. During\n 310:\t\t  compile time, a table is made of all the locations that ftrace\n 311:\t\t  can function trace, and this table is linked into the kernel\n 312:\t\t  image. When this is enabled, functions can be individually\n 313:\t\t  enabled, and the functions not enabled will not affect\n 314:\t\t  performance of the system.\n 315:\t\n 316:\t\t  See the files in /sys/kernel/tracing:\n 317:\t\t    available_filter_functions\n 318:\t\t    set_ftrace_filter\n 319:\t\t    set_ftrace_notrace\n 320:\t\n 321:\t\t  This way a CONFIG_FUNCTION_TRACER kernel is slightly larger, but\n 322:\t\t  otherwise has native performance as long as no tracing is active.\n 323:\t\n 324:\tconfig DYNAMIC_FTRACE_WITH_REGS\n 325:\t\tdef_bool y\n 326:\t\tdepends on DYNAMIC_FTRACE\n 327:\t\tdepends on HAVE_DYNAMIC_FTRACE_WITH_REGS\n 328:\t\n 329:\tconfig DYNAMIC_FTRACE_WITH_DIRECT_CALLS\n 330:\t\tdef_bool y\n 331:\t\tdepends on DYNAMIC_FTRACE_WITH_REGS || DYNAMIC_FTRACE_WITH_ARGS\n 332:\t\tdepends on HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\n 333:\t\n 334:\tconfig DYNAMIC_FTRACE_WITH_CALL_OPS\n 335:\t\tdef_bool y\n 336:\t\tdepends on HAVE_DYNAMIC_FTRACE_WITH_CALL_OPS\n 337:\t\n 338:\tconfig DYNAMIC_FTRACE_WITH_ARGS\n 339:\t\tdef_bool y\n"
}

39/2 2026/09/27 09:13 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 60435
							output: 28
							thoughts: 269
						

Thoughts:
**My Configuration Quandary**

Okay, so I'm thinking about the kernel configurations, specifically how everything relates to eBPF tracing. Right off the bat, I know that `DYNAMIC_FTRACE` is inherently tied to `FUNCTION_TRACER`. That's a given. But what about `BPF_JIT`? This is where it gets interesting, because the BPF JIT trampolines are crucial for all those tracepoint functions like `fentry`, `fexit`, `fmod_ret`, and `fsession`.

So, which configs actually matter here? I need to keep track of:

*   `BPF_SYSCALL`
*   `BPF_JIT` (obviously!)
*   `FUNCTION_TRACER` (to support DYNAMIC_FTRACE, indirectly)
*   `DYNAMIC_FTRACE` (for dynamic trace points)
*   `DYNAMIC_FTRACE_WITH_DIRECT_CALLS`

Now, the `CONFIG_TASKS_RCU` situation is where I need to dig a little deeper. I'm seeing that `NEED_TASKS_RCU` is selected if `PREEMPTION` is enabled. `BPF_SYSCALL` also selects `NEED_TASKS_RCU`, so if `PREEMPTION` is enabled, then `TASKS_RCU` is, too. I remember seeing something about this in `kernel/bpf/syscall.c` : if `IS_ENABLED(CONFIG_TASKS_RCU) && prog->aux->tramp_linked` then we have to use `call_rcu_tasks`. This is a point to bear in mind.

Finally, I need to check the configurations related to tracing with BPF, specifically `BPF_EVENTS`. The best place to start is `kernel/trace/Kconfig` to see the dependencies and relationships around that.



40/2 2026/09/27 09:13 tool grepper
0m Args:
{
  "Expression": "BPF_EVENTS",
  "PathPrefix": "kernel/trace/"
}

Results:
{
  "Output": "kernel/trace/Kconfig=851=config EPROBE_EVENTS\n--\nkernel/trace/Kconfig-864-\nkernel/trace/Kconfig:865:config BPF_EVENTS\nkernel/trace/Kconfig-866-\tdepends on BPF_SYSCALL\n--\nkernel/trace/Kconfig=880=config BPF_KPROBE_OVERRIDE\nkernel/trace/Kconfig-881-\tbool \"Enable BPF programs to override a kprobed function\"\nkernel/trace/Kconfig:882:\tdepends on BPF_EVENTS\nkernel/trace/Kconfig-883-\tdepends on FUNCTION_ERROR_INJECTION\n--\nkernel/trace/Makefile=109=obj-$(CONFIG_USER_EVENTS) += trace_events_user.o\nkernel/trace/Makefile:110:obj-$(CONFIG_BPF_EVENTS) += bpf_trace.o\nkernel/trace/Makefile-111-obj-$(CONFIG_KPROBE_EVENTS) += trace_kprobe.o\n--\nkernel/trace/trace_events.c=2219=event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)\n--\nkernel/trace/trace_events.c-2233-\nkernel/trace/trace_events.c:2234:#ifdef CONFIG_BPF_EVENTS\nkernel/trace/trace_events.c-2235-static ssize_t\n--\nkernel/trace/trace_events.c=2783=static const struct file_operations ftrace_event_id_fops = {\n--\nkernel/trace/trace_events.c-2788-\nkernel/trace/trace_events.c:2789:#ifdef CONFIG_BPF_EVENTS\nkernel/trace/trace_events.c-2790-static const struct file_operations ftrace_event_btf_ids_fops = {\n--\nkernel/trace/trace_events.c=3159=static int event_callback(const char *name, umode_t *mode, void **data,\n--\nkernel/trace/trace_events.c-3208-\nkernel/trace/trace_events.c:3209:#ifdef CONFIG_BPF_EVENTS\nkernel/trace/trace_events.c-3210-\tif (call-\u003eclass-\u003ebtf_ids \u0026\u0026 strcmp(name, \"btf_ids\") == 0) {\n--\nkernel/trace/trace_events.c=3251=event_create_dir(struct eventfs_inode *parent, struct trace_event_file *file)\n--\nkernel/trace/trace_events.c-3270-#endif\nkernel/trace/trace_events.c:3271:#ifdef CONFIG_BPF_EVENTS\nkernel/trace/trace_events.c-3272-\t\t{\n--\nkernel/trace/trace_events.c-3277-#define NR_RO_EVENT_ENTRIES\t(1 + IS_ENABLED(CONFIG_PERF_EVENTS) + \\\nkernel/trace/trace_events.c:3278:\t\t\t\t IS_ENABLED(CONFIG_BPF_EVENTS))\nkernel/trace/trace_events.c-3279-/* Readonly files must be above this line and counted by NR_RO_EVENT_ENTRIES. */\n--\nkernel/trace/trace_syscalls.c=1303=struct trace_event_functions exit_syscall_print_funcs = {\n--\nkernel/trace/trace_syscalls.c-1306-\nkernel/trace/trace_syscalls.c:1307:#if defined(CONFIG_BPF_EVENTS) \u0026\u0026 defined(CONFIG_DEBUG_INFO_BTF)\nkernel/trace/trace_syscalls.c-1308-/* BTF id lists for the shared sys_enter/sys_exit dispatcher tracepoints. */\n--\nkernel/trace/trace_syscalls.c=1318=struct trace_event_class __refdata event_class_syscall_enter = {\n--\nkernel/trace/trace_syscalls.c-1323-\t.raw_init\t= init_syscall_trace,\nkernel/trace/trace_syscalls.c:1324:#if defined(CONFIG_BPF_EVENTS) \u0026\u0026 defined(CONFIG_DEBUG_INFO_BTF)\nkernel/trace/trace_syscalls.c-1325-\t.btf_ids\t= syscall_enter_btf_ids,\n--\nkernel/trace/trace_syscalls.c=1329=struct trace_event_class __refdata event_class_syscall_exit = {\n--\nkernel/trace/trace_syscalls.c-1338-\t.raw_init\t= init_syscall_trace,\nkernel/trace/trace_syscalls.c:1339:#if defined(CONFIG_BPF_EVENTS) \u0026\u0026 defined(CONFIG_DEBUG_INFO_BTF)\nkernel/trace/trace_syscalls.c-1340-\t.btf_ids\t= syscall_exit_btf_ids,\n--\nkernel/trace/trace_uprobe.c=1390=static void __uprobe_perf_func(struct trace_uprobe *tu,\n--\nkernel/trace/trace_uprobe.c-1401-\nkernel/trace/trace_uprobe.c:1402:#ifdef CONFIG_BPF_EVENTS\nkernel/trace/trace_uprobe.c-1403-\tif (bpf_prog_array_valid(call)) {\n--\nkernel/trace/trace_uprobe.c-1413-\t}\nkernel/trace/trace_uprobe.c:1414:#endif /* CONFIG_BPF_EVENTS */\nkernel/trace/trace_uprobe.c-1415-\n"
}

41/2 2026/09/27 09:13 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 62547
							output: 34
							thoughts: 22
						

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

**Diving into the Tracepoint Configuration**

Alright, time to dig in. I need to understand something related to tracing, so my first instinct is to check the configuration file. Specifically, I'm going to examine `kernel/trace/Kconfig`, and I'm focusing on lines 860 through 875. I'm expecting this section to deal with a specific feature or option related to the tracing infrastructure, perhaps a compile-time setting, a dependency, or maybe even a grouping of related options. I'll be looking for clues about the feature's name, purpose, and any dependencies it might have. This should give me a solid starting point.



42/2 2026/09/27 09:13 tool read-file
0m Args:
{
  "File": "kernel/trace/Kconfig",
  "FirstLine": 860,
  "LineCount": 20
}

Results:
{
  "Contents": " 860:\t\t  events. It can be used to limit what fields are recorded in\n 861:\t\t  an event or even dereference a field of an event. It can\n 862:\t\t  convert the type of an event field. For example, turn an\n 863:\t\t  address into a string.\n 864:\t\n 865:\tconfig BPF_EVENTS\n 866:\t\tdepends on BPF_SYSCALL\n 867:\t\tdepends on (KPROBE_EVENTS || UPROBE_EVENTS) \u0026\u0026 PERF_EVENTS\n 868:\t\tbool\n 869:\t\tdefault y\n 870:\t\thelp\n 871:\t\t  This allows the user to attach BPF programs to kprobe, uprobe, and\n 872:\t\t  tracepoint events.\n 873:\t\n 874:\tconfig DYNAMIC_EVENTS\n 875:\t\tdef_bool n\n 876:\t\n 877:\tconfig PROBE_EVENTS\n 878:\t\tdef_bool n\n 879:\t\n"
}

43/2 2026/09/27 09:13 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 63036
							output: 318
							thoughts: 2034
						

Thoughts:
**Focus Symbol Identification for BPF Trampoline Fuzzing**

Alright, let's break this down. My goal here is to identify the most effective `FocusSymbols` for fuzzing BPF trampoline logic, ensuring we target the right functions.

First, I examined the diff and extracted potential candidates.  The key functions are: `bpf_trampoline_update`, `bpf_trampoline_skip_prog`, `bpf_trampoline_unlink_prog`, `bpf_trampoline_link_prog`, `arch_prepare_bpf_trampoline`, `bpf_tramp_image_put`, `__bpf_tramp_image_put_deferred`, `__bpf_prog_put_rcu_tasks`, and `bpf_tracing_link_release`. Also relevant are the trampoline multi-attach and detach functions.

Now, I'm verifying which of these are actually exposed as symbols. The key here is to distinguish between static and global functions, because the fuzzer can target globals, and potentially statics but not necessarily static inlines.  I've confirmed that `bpf_trampoline_link_prog`, `bpf_trampoline_unlink_prog`, `arch_prepare_bpf_trampoline`, `bpf_trampoline_put`, `bpf_trampoline_multi_detach` are all global.  `bpf_trampoline_update`, `bpf_trampoline_skip_prog`, `bpf_tramp_image_put`, and `__bpf_tramp_image_put_deferred` are static, but since they're in .c files, they should be in kallsyms.

The crucial point is to avoid overly broad functions that are ubiquitous, like memory allocators or generic synchronization primitives. These candidates seem specific to the BPF trampoline subsystem, making them ideal targets. They are the core of the logic or represent API entry points.  We should definitely focus on the entry points and the functions that perform the core logic, which is the modification of skip function pointers.

Let's carefully select our `FocusSymbols`. Based on this analysis, the best candidates are:

*   `bpf_trampoline_link_prog`
*   `bpf_trampoline_unlink_prog`
*   `bpf_trampoline_update`
*   `bpf_trampoline_skip_prog`
*   `bpf_tramp_image_put`
*   `arch_prepare_bpf_trampoline`
*   `bpf_trampoline_put`
*   `bpf_trampoline_multi_detach`

Also, some further investigation:

Further investigation into the relevant files reveals other useful functions like `bpf_trampoline_remove_prog`, `bpf_tramp_image_free`, `bpf_tramp_image_alloc`, and `__bpf_prog_put_rcu_tasks`.

Regarding configurations, `BPF_SYSCALL`, `BPF_JIT`, `DYNAMIC_FTRACE_WITH_DIRECT_CALLS`, and `TASKS_RCU` appear relevant. Also, since `TASKS_RCU` requires `PREEMPTION` we should be fine, as this is a standard configuration.

Finally, regarding the changes: this patch addresses a race/UAF during BPF trampoline program detachment. It involves text patching, RCU tasks, reference counting, and JIT generation which makes it highly relevant for fuzzing. These functions are reachable via standard BPF syscalls on amd64 when using tracing BPF programs.

The reasoning is sound, and the selected symbols effectively target the critical logic and entry points. I'm ready to proceed with these focus symbols.



44/2 2026/09/27 09:14 tool set-results
0m Args:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "BPF_JIT",
    "DYNAMIC_FTRACE_WITH_DIRECT_CALLS",
    "TASKS_RCU"
  ],
  "FocusSymbols": [
    "bpf_trampoline_link_prog",
    "bpf_trampoline_unlink_prog",
    "bpf_trampoline_update",
    "bpf_trampoline_skip_prog",
    "bpf_trampoline_remove_prog",
    "bpf_tramp_image_put",
    "arch_prepare_bpf_trampoline",
    "bpf_trampoline_put"
  ],
  "Reasoning": "The patch modifies BPF trampoline management and x86 JIT generation to prevent use-after-free/race conditions when detaching BPF programs while tasks are executing inside older trampoline images. Instead of patching only at the epilogue, it reserves NOPs before each program call and dynamically text-pokes them into skip jumps when programs detach, tracking images on a per-trampoline list and adjusting RCU grace periods (call_rcu_tasks). This code is fully reachable on amd64 via standard BPF syscalls (attaching and detaching fentry/fexit/fmod_ret/fsession programs) and warrants fuzzing due to the concurrency, live kernel text patching (bpf_arch_text_poke), and reference counting involved.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "BPF_JIT",
    "DYNAMIC_FTRACE_WITH_DIRECT_CALLS",
    "TASKS_RCU"
  ],
  "FocusSymbols": [
    "bpf_trampoline_link_prog",
    "bpf_trampoline_unlink_prog",
    "bpf_trampoline_update",
    "bpf_trampoline_skip_prog",
    "bpf_trampoline_remove_prog",
    "bpf_tramp_image_put",
    "arch_prepare_bpf_trampoline",
    "bpf_trampoline_put"
  ],
  "Reasoning": "The patch modifies BPF trampoline management and x86 JIT generation to prevent use-after-free/race conditions when detaching BPF programs while tasks are executing inside older trampoline images. Instead of patching only at the epilogue, it reserves NOPs before each program call and dynamically text-pokes them into skip jumps when programs detach, tracking images on a per-trampoline list and adjusting RCU grace periods (call_rcu_tasks). This code is fully reachable on amd64 via standard BPF syscalls (attaching and detaching fentry/fexit/fmod_ret/fsession programs) and warrants fuzzing due to the concurrency, live kernel text patching (bpf_arch_text_poke), and reference counting involved.",
  "WorthFuzzing": true
}

45/1 2026/09/27 09:14 action If
0m Args:
{
  "WorthFuzzing": true
}

Results:
null

46/2 2026/09/27 09:14 agent kmsan-evaluator
0m Model:
core

Args:
null

Results:
{
  "KMSANReasoning": "The patch series modifies BPF trampoline generation and lifecycle handling across multiple architectures (x86, arm64, loongarch, powerpc, riscv, s390) and core BPF trampoline code to prevent a use-after-free (UAF) race condition when BPF programs are detached and freed while another task is preempted or sleeping inside a trampoline image. It patches NOP instructions to jump over detached programs and refines RCU synchronization (RCU tasks trace / tasks RCU).\n\nKMSAN vs. KASAN applicability:\n- The bug being addressed is a lifetime / use-after-free race condition, which is directly tracked and detected by KASAN, not KMSAN.\n- All newly introduced or modified kernel data structures (such as `im-\u003eskips` allocated via `kzalloc_objs` and stack instances `im` initialized with `= {}`) are fully zero-initialized before access.\n- There are no kernel-to-user memory transfers (no `copy_to_user`, `put_user`, socket options, or netlink attributes) introduced or altered that could result in information disclosure of uninitialized padding or stack/heap bytes.\n- Any potential memory safety defects introduced by this patch (e.g., out-of-bounds access in the `skips` array, list corruption, or use-after-free during image teardown) are detectable by KASAN and standard kernel debuggers.\n\nTherefore, this patch does not introduce or expose uninitialized memory risks, and a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false
}

Instruction:
You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.

CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
  a comprehensive suite of debugging tools and sanitizers, including KASAN
  (out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
  (locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
  or page allocations) and kernel-to-user memory info-leaks.

Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.

Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.

Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
   attributes, ioctl output arguments, socket options, or BPF buffers) where fields
   or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
   or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
   could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
   uninitialized bytes of existing buffers.

Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
  dereferences, locking deadlocks, or use-after-free bugs (these are already caught
  by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
  or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.

Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
Target architecture: amd64

For your convenience, here is the diff of the changes:
commit 95aa626b973aa62514c088b7f1ab61c19b68b7ba
Author: syz-cluster <triage@syzkaller.com>
Date:   Sun Sep 27 09:12:39 2026 +0000

    syz-cluster: applied patch under review

diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index c5f55d6161fee..bd73179c00eca 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -2418,10 +2418,11 @@ bool bpf_jit_supports_subprog_tailcalls(void)
 	return true;
 }
 
-static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *node,
-			    int bargs_off, int retval_off, int run_ctx_off,
-			    bool save_ret)
+static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_image *im,
+			    struct bpf_tramp_node *node, int bargs_off,
+			    int retval_off, int run_ctx_off, bool save_ret)
 {
+	void *skip;
 	__le32 *branch;
 	u64 enter_prog;
 	u64 exit_prog;
@@ -2431,6 +2432,10 @@ static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *node,
 	enter_prog = (u64)bpf_trampoline_enter(p);
 	exit_prog = (u64)bpf_trampoline_exit(p);
 
+	/* nop, patched to skip this prog when it is detached */
+	skip = ctx->ro_image + ctx->idx;
+	emit(A64_NOP, ctx);
+
 	if (node->cookie == 0) {
 		/* if cookie is zero, one instruction is enough to store it */
 		emit(A64_STR64I(A64_ZR, A64_SP, run_ctx_off + cookie_off), ctx);
@@ -2483,11 +2488,13 @@ static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *node,
 	emit(A64_ADD_I(1, A64_R(2), A64_SP, run_ctx_off), ctx);
 
 	emit_call(exit_prog, ctx);
+
+	bpf_tramp_image_add_skip(im, p, skip, ctx->ro_image + ctx->idx);
 }
 
-static void invoke_bpf_mod_ret(struct jit_ctx *ctx, struct bpf_tramp_nodes *tn,
-			       int bargs_off, int retval_off, int run_ctx_off,
-			       __le32 **branches)
+static void invoke_bpf_mod_ret(struct jit_ctx *ctx, struct bpf_tramp_image *im,
+			       struct bpf_tramp_nodes *tn, int bargs_off,
+			       int retval_off, int run_ctx_off, __le32 **branches)
 {
 	int i;
 
@@ -2496,7 +2503,7 @@ static void invoke_bpf_mod_ret(struct jit_ctx *ctx, struct bpf_tramp_nodes *tn,
 	 */
 	emit(A64_STR64I(A64_ZR, A64_SP, retval_off), ctx);
 	for (i = 0; i < tn->nr_nodes; i++) {
-		invoke_bpf_prog(ctx, tn->nodes[i], bargs_off, retval_off,
+		invoke_bpf_prog(ctx, im, tn->nodes[i], bargs_off, retval_off,
 				run_ctx_off, true);
 		/* if (*(u64 *)(sp + retval_off) !=  0)
 		 *	goto do_fexit;
@@ -2882,7 +2889,7 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
 			store_func_meta(ctx, meta, func_meta_off);
 			cookie_bargs_off--;
 		}
-		invoke_bpf_prog(ctx, fentry->nodes[i], bargs_off,
+		invoke_bpf_prog(ctx, im, fentry->nodes[i], bargs_off,
 				retval_off, run_ctx_off,
 				flags & BPF_TRAMP_F_RET_FENTRY_RET);
 	}
@@ -2893,7 +2900,7 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
 		if (!branches)
 			return -ENOMEM;
 
-		invoke_bpf_mod_ret(ctx, fmod_ret, bargs_off, retval_off,
+		invoke_bpf_mod_ret(ctx, im, fmod_ret, bargs_off, retval_off,
 				   run_ctx_off, branches);
 	}
 
@@ -2906,9 +2913,6 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
 		emit(A64_RET(A64_R(10)), ctx);
 		/* store return value */
 		emit(A64_STR64I(A64_R(0), A64_SP, retval_off), ctx);
-		/* reserve a nop for bpf_tramp_image_put */
-		im->ip_after_call = ctx->ro_image + ctx->idx;
-		emit(A64_NOP, ctx);
 	}
 
 	/* update the branches saved in invoke_bpf_mod_ret with cbnz */
@@ -2930,12 +2934,11 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
 			store_func_meta(ctx, meta, func_meta_off);
 			cookie_bargs_off--;
 		}
-		invoke_bpf_prog(ctx, fexit->nodes[i], bargs_off, retval_off,
+		invoke_bpf_prog(ctx, im, fexit->nodes[i], bargs_off, retval_off,
 				run_ctx_off, false);
 	}
 
 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
-		im->ip_epilogue = ctx->ro_image + ctx->idx;
 		/* for the first pass, assume the worst case */
 		if (!ctx->image)
 			ctx->idx += 4;
@@ -2994,7 +2997,7 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
 		.image = NULL,
 		.idx = 0,
 	};
-	struct bpf_tramp_image im;
+	struct bpf_tramp_image im = {};
 	struct arg_aux aaux;
 	int ret;
 
diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index 4da2789009383..df8abfa055d1c 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -1696,13 +1696,19 @@ static void restore_stk_args(struct jit_ctx *ctx, int nr_stk_args, int args_off,
 	}
 }
 
-static int invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *n,
-			   int args_off, int retval_off, int run_ctx_off, bool save_ret)
+static int invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_image *im,
+			   struct bpf_tramp_node *n, int args_off, int retval_off,
+			   int run_ctx_off, bool save_ret)
 {
-	int ret;
+	int i, ret;
 	u32 *branch;
 	struct bpf_prog *p = n->link->prog;
 	int cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
+	void *skip = ctx->ro_image + ctx->idx;
+
+	/* nops for move_imm+jirl, patched to skip this prog when it is detached */
+	for (i = 0; i < LOONGARCH_LONG_JUMP_NINSNS; i++)
+		emit_insn(ctx, nop);
 
 	if (n->cookie)
 		emit_store_stack_imm64(ctx, LOONGARCH_GPR_T1,
@@ -1755,13 +1761,17 @@ static int invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *n,
 	/* arg3: &run_ctx */
 	emit_insn(ctx, addid, LOONGARCH_GPR_A2, LOONGARCH_GPR_FP, -run_ctx_off);
 	ret = emit_call(ctx, (const u64)bpf_trampoline_exit(p));
+	if (ret)
+		return ret;
 
-	return ret;
+	bpf_tramp_image_add_skip(im, p, skip, ctx->ro_image + ctx->idx);
+	return 0;
 }
 
-static int invoke_bpf(struct jit_ctx *ctx, struct bpf_tramp_nodes *tn,
-		      int args_off, int retval_off, int run_ctx_off,
-		      int func_meta_off, bool save_ret, u64 func_meta, int cookie_off)
+static int invoke_bpf(struct jit_ctx *ctx, struct bpf_tramp_image *im,
+		      struct bpf_tramp_nodes *tn, int args_off, int retval_off,
+		      int run_ctx_off, int func_meta_off, bool save_ret,
+		      u64 func_meta, int cookie_off)
 {
 	int i, cur_cookie = (cookie_off - args_off) / 8;
 
@@ -1774,7 +1784,8 @@ static int invoke_bpf(struct jit_ctx *ctx, struct bpf_tramp_nodes *tn,
 			emit_store_stack_imm64(ctx, LOONGARCH_GPR_T1, -func_meta_off, meta);
 			cur_cookie--;
 		}
-		err = invoke_bpf_prog(ctx, tn->nodes[i], args_off, retval_off, run_ctx_off, save_ret);
+		err = invoke_bpf_prog(ctx, im, tn->nodes[i], args_off, retval_off,
+				      run_ctx_off, save_ret);
 		if (err)
 			return err;
 	}
@@ -2017,7 +2028,7 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i
 	}
 
 	if (fentry->nr_nodes) {
-		ret = invoke_bpf(ctx, fentry, args_off, retval_off, run_ctx_off, func_meta_off,
+		ret = invoke_bpf(ctx, im, fentry, args_off, retval_off, run_ctx_off, func_meta_off,
 				 flags & BPF_TRAMP_F_RET_FENTRY_RET, func_meta, cookie_off);
 		if (ret)
 			return ret;
@@ -2029,7 +2040,7 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i
 
 		emit_insn(ctx, std, LOONGARCH_GPR_ZERO, LOONGARCH_GPR_FP, -retval_off);
 		for (i = 0; i < fmod_ret->nr_nodes; i++) {
-			ret = invoke_bpf_prog(ctx, fmod_ret->nodes[i],
+			ret = invoke_bpf_prog(ctx, im, fmod_ret->nodes[i],
 					      args_off, retval_off, run_ctx_off, true);
 			if (ret)
 				goto out;
@@ -2051,10 +2062,6 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i
 			goto out;
 		emit_insn(ctx, std, LOONGARCH_GPR_A0, LOONGARCH_GPR_FP, -retval_off);
 		emit_insn(ctx, std, regmap[BPF_REG_0], LOONGARCH_GPR_FP, -(retval_off - 8));
-		im->ip_after_call = ctx->ro_image + ctx->idx;
-		/* Reserve space for the move_imm + jirl instruction */
-		for (i = 0; i < LOONGARCH_LONG_JUMP_NINSNS; i++)
-			emit_insn(ctx, nop);
 	}
 
 	for (i = 0; ctx->image && i < fmod_ret->nr_nodes; i++) {
@@ -2068,14 +2075,13 @@ static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_i
 		emit_store_stack_imm64(ctx, LOONGARCH_GPR_T1, -func_meta_off, func_meta);
 
 	if (fexit->nr_nodes) {
-		ret = invoke_bpf(ctx, fexit, args_off, retval_off, run_ctx_off,
+		ret = invoke_bpf(ctx, im, fexit, args_off, retval_off, run_ctx_off,
 				 func_meta_off, false, func_meta, cookie_off);
 		if (ret)
 			goto out;
 	}
 
 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
-		im->ip_epilogue = ctx->ro_image + ctx->idx;
 		move_addr(ctx, LOONGARCH_GPR_A0, (const u64)im);
 		ret = emit_call(ctx, (const u64)__bpf_tramp_exit);
 		if (ret)
@@ -2177,11 +2183,8 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
 			     struct bpf_tramp_nodes *tnodes, void *func_addr)
 {
 	int ret;
-	struct jit_ctx ctx;
-	struct bpf_tramp_image im;
-
-	ctx.image = NULL;
-	ctx.idx = 0;
+	struct jit_ctx ctx = {};
+	struct bpf_tramp_image im = {};
 
 	ret = __arch_prepare_bpf_trampoline(&ctx, &im, m, tnodes, func_addr, flags);
 
diff --git a/arch/powerpc/net/bpf_jit_comp.c b/arch/powerpc/net/bpf_jit_comp.c
index 7b07b43575f11..7a612688e7a20 100644
--- a/arch/powerpc/net/bpf_jit_comp.c
+++ b/arch/powerpc/net/bpf_jit_comp.c
@@ -602,14 +602,18 @@ int arch_protect_bpf_trampoline(void *image, unsigned int size)
 }
 
 static int invoke_bpf_prog(u32 *image, u32 *ro_image, struct codegen_context *ctx,
-			   struct bpf_tramp_node *n, int regs_off, int retval_off,
-			   int run_ctx_off, bool save_ret)
+			   struct bpf_tramp_image *im, struct bpf_tramp_node *n,
+			   int regs_off, int retval_off, int run_ctx_off, bool save_ret)
 {
 	struct bpf_prog *p = n->link->prog;
 	ppc_inst_t branch_insn;
-	u32 jmp_idx;
+	u32 jmp_idx, skip_idx;
 	int ret = 0;
 
+	/* nop, patched to skip this prog when it is detached */
+	skip_idx = ctx->idx;
+	EMIT(PPC_RAW_NOP());
+
 	/* Save cookie */
 	if (IS_ENABLED(CONFIG_PPC64)) {
 		PPC_LI64(_R3, n->cookie);
@@ -679,13 +683,17 @@ static int invoke_bpf_prog(u32 *image, u32 *ro_image, struct codegen_context *ct
 	EMIT(PPC_RAW_ADDI(_R5, _R1, run_ctx_off));
 	ret = bpf_jit_emit_func_call_rel(image, ro_image, ctx,
 					 (unsigned long)bpf_trampoline_exit(p));
+	if (ret)
+		return ret;
 
-	return ret;
+	if (ro_image) /* image is NULL for dummy pass */
+		bpf_tramp_image_add_skip(im, p, &ro_image[skip_idx], &ro_image[ctx->idx]);
+	return 0;
 }
 
 static int invoke_bpf_mod_ret(u32 *image, u32 *ro_image, struct codegen_context *ctx,
-			      struct bpf_tramp_nodes *tn, int regs_off, int retval_off,
-			      int run_ctx_off, u32 *branches)
+			      struct bpf_tramp_image *im, struct bpf_tramp_nodes *tn,
+			      int regs_off, int retval_off, int run_ctx_off, u32 *branches)
 {
 	int i;
 
@@ -696,8 +704,8 @@ static int invoke_bpf_mod_ret(u32 *image, u32 *ro_image, struct codegen_context
 	EMIT(PPC_RAW_LI(_R3, 0));
 	EMIT(PPC_RAW_STL(_R3, _R1, retval_off));
 	for (i = 0; i < tn->nr_nodes; i++) {
-		if (invoke_bpf_prog(image, ro_image, ctx, tn->nodes[i], regs_off, retval_off,
-				    run_ctx_off, true))
+		if (invoke_bpf_prog(image, ro_image, ctx, im, tn->nodes[i], regs_off,
+				    retval_off, run_ctx_off, true))
 			return -EINVAL;
 
 		/*
@@ -1043,8 +1051,8 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 			cookie_ctx_off--;
 		}
 
-		if (invoke_bpf_prog(image, ro_image, ctx, fentry->nodes[i], regs_off, retval_off,
-				    run_ctx_off, flags & BPF_TRAMP_F_RET_FENTRY_RET))
+		if (invoke_bpf_prog(image, ro_image, ctx, im, fentry->nodes[i], regs_off,
+				    retval_off, run_ctx_off, flags & BPF_TRAMP_F_RET_FENTRY_RET))
 			return -EINVAL;
 	}
 
@@ -1053,7 +1061,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 		if (!branches)
 			return -ENOMEM;
 
-		if (invoke_bpf_mod_ret(image, ro_image, ctx, fmod_ret, regs_off, retval_off,
+		if (invoke_bpf_mod_ret(image, ro_image, ctx, im, fmod_ret, regs_off, retval_off,
 				       run_ctx_off, branches)) {
 			ret = -EINVAL;
 			goto cleanup;
@@ -1090,11 +1098,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 		/* Restore updated tail_call_cnt */
 		if (flags & BPF_TRAMP_F_TAIL_CALL_CTX)
 			bpf_trampoline_restore_tail_call_cnt(image, ctx, bpf_frame_size, r4_off);
-
-		/* Reserve space to patch branch instruction to skip fexit progs */
-		if (ro_image) /* image is NULL for dummy pass */
-			im->ip_after_call = &((u32 *)ro_image)[ctx->idx];
-		EMIT(PPC_RAW_NOP());
 	}
 
 	/* Update branches saved in invoke_bpf_mod_ret with address of do_fexit */
@@ -1123,16 +1126,14 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 			cookie_ctx_off--;
 		}
 
-		if (invoke_bpf_prog(image, ro_image, ctx, fexit->nodes[i], regs_off, retval_off,
-				    run_ctx_off, false)) {
+		if (invoke_bpf_prog(image, ro_image, ctx, im, fexit->nodes[i], regs_off,
+				    retval_off, run_ctx_off, false)) {
 			ret = -EINVAL;
 			goto cleanup;
 		}
 	}
 
 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
-		if (ro_image) /* image is NULL for dummy pass */
-			im->ip_epilogue = &((u32 *)ro_image)[ctx->idx];
 		PPC_LI_ADDR(_R3, im);
 		ret = bpf_jit_emit_func_call_rel(image, ro_image, ctx,
 						 (unsigned long)__bpf_tramp_exit);
@@ -1192,7 +1193,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
 			     struct bpf_tramp_nodes *tnodes, void *func_addr)
 {
-	struct bpf_tramp_image im;
+	struct bpf_tramp_image im = {};
 	int ret;
 
 	ret = __arch_prepare_bpf_trampoline(&im, NULL, NULL, NULL, m, flags, tnodes, func_addr);
@@ -1320,7 +1321,7 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
 
 	/*
 	 * If we are not poking at bpf prog entry, then we are simply patching in/out
-	 * an unconditional branch instruction at im->ip_after_call
+	 * an unconditional branch instruction in a trampoline image
 	 */
 	if (offset) {
 		if (old_t == BPF_MOD_CALL || new_t == BPF_MOD_CALL) {
diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c
index 151031e97a243..558e02dc2fe39 100644
--- a/arch/riscv/net/bpf_jit_comp64.c
+++ b/arch/riscv/net/bpf_jit_comp64.c
@@ -904,12 +904,18 @@ static void emit_store_stack_imm64(u8 reg, int stack_off, u64 imm64,
 	emit_sd(RV_REG_FP, stack_off, reg, ctx);
 }
 
-static int invoke_bpf_prog(struct bpf_tramp_node *node, int args_off, int retval_off,
-			   int run_ctx_off, bool save_ret, struct rv_jit_context *ctx)
+static int invoke_bpf_prog(struct bpf_tramp_image *im, struct bpf_tramp_node *node,
+			   int args_off, int retval_off, int run_ctx_off, bool save_ret,
+			   struct rv_jit_context *ctx)
 {
 	int ret, branch_off;
 	struct bpf_prog *p = node->link->prog;
 	int cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
+	void *skip = ctx->ro_insns + ctx->ninsns;
+
+	/* 2 nops for auipc+jalr, patched to skip this prog when it is detached */
+	emit(rv_nop(), ctx);
+	emit(rv_nop(), ctx);
 
 	if (node->cookie)
 		emit_store_stack_imm64(RV_REG_T1, -run_ctx_off + cookie_off, node->cookie, ctx);
@@ -962,13 +968,17 @@ static int invoke_bpf_prog(struct bpf_tramp_node *node, int args_off, int retval
 	/* arg3: &run_ctx */
 	emit_addi(RV_REG_A2, RV_REG_FP, -run_ctx_off, ctx);
 	ret = emit_call((const u64)bpf_trampoline_exit(p), true, ctx);
+	if (ret)
+		return ret;
 
-	return ret;
+	bpf_tramp_image_add_skip(im, p, skip, ctx->ro_insns + ctx->ninsns);
+	return 0;
 }
 
-static int invoke_bpf(struct bpf_tramp_nodes *tn, int args_off, int retval_off,
-		      int run_ctx_off, int func_meta_off, bool save_ret, u64 func_meta,
-		      int cookie_off, struct rv_jit_context *ctx)
+static int invoke_bpf(struct bpf_tramp_image *im, struct bpf_tramp_nodes *tn,
+		      int args_off, int retval_off, int run_ctx_off, int func_meta_off,
+		      bool save_ret, u64 func_meta, int cookie_off,
+		      struct rv_jit_context *ctx)
 {
 	int i, cur_cookie = (cookie_off - args_off) / 8;
 
@@ -981,8 +991,8 @@ static int invoke_bpf(struct bpf_tramp_nodes *tn, int args_off, int retval_off,
 			emit_store_stack_imm64(RV_REG_T1, -func_meta_off, meta, ctx);
 			cur_cookie--;
 		}
-		err = invoke_bpf_prog(tn->nodes[i], args_off, retval_off, run_ctx_off,
-				      save_ret, ctx);
+		err = invoke_bpf_prog(im, tn->nodes[i], args_off, retval_off,
+				      run_ctx_off, save_ret, ctx);
 		if (err)
 			return err;
 	}
@@ -1170,7 +1180,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 	}
 
 	if (fentry->nr_nodes) {
-		ret = invoke_bpf(fentry, args_off, retval_off, run_ctx_off, func_meta_off,
+		ret = invoke_bpf(im, fentry, args_off, retval_off, run_ctx_off, func_meta_off,
 				 flags & BPF_TRAMP_F_RET_FENTRY_RET, func_meta, cookie_off, ctx);
 		if (ret)
 			return ret;
@@ -1184,7 +1194,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 		/* cleanup to avoid garbage return value confusion */
 		emit_sd(RV_REG_FP, -retval_off, RV_REG_ZERO, ctx);
 		for (i = 0; i < fmod_ret->nr_nodes; i++) {
-			ret = invoke_bpf_prog(fmod_ret->nodes[i], args_off, retval_off,
+			ret = invoke_bpf_prog(im, fmod_ret->nodes[i], args_off, retval_off,
 					      run_ctx_off, true, ctx);
 			if (ret)
 				goto out;
@@ -1211,10 +1221,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 			emit_sd(RV_REG_FP, -tcc_off, RV_REG_TCC, ctx);
 		emit_sd(RV_REG_FP, -retval_off, RV_REG_A0, ctx);
 		emit_sd(RV_REG_FP, -(retval_off - 8), regmap[BPF_REG_0], ctx);
-		im->ip_after_call = ctx->ro_insns + ctx->ninsns;
-		/* 2 nops reserved for auipc+jalr pair */
-		emit(rv_nop(), ctx);
-		emit(rv_nop(), ctx);
 	}
 
 	/* update branches saved in invoke_bpf_mod_ret with bnez */
@@ -1230,14 +1236,13 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 		emit_store_stack_imm64(RV_REG_T1, -func_meta_off, func_meta, ctx);
 
 	if (fexit->nr_nodes) {
-		ret = invoke_bpf(fexit, args_off, retval_off, run_ctx_off, func_meta_off,
+		ret = invoke_bpf(im, fexit, args_off, retval_off, run_ctx_off, func_meta_off,
 				 false, func_meta, cookie_off, ctx);
 		if (ret)
 			goto out;
 	}
 
 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
-		im->ip_epilogue = ctx->ro_insns + ctx->ninsns;
 		emit_imm(RV_REG_A0, ctx->insns ? (const s64)im : RV_MAX_COUNT_IMM, ctx);
 		ret = emit_call((const u64)__bpf_tramp_exit, true, ctx);
 		if (ret)
@@ -1299,7 +1304,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
 			     struct bpf_tramp_nodes *tnodes, void *func_addr)
 {
-	struct bpf_tramp_image im;
+	struct bpf_tramp_image im = {};
 	struct rv_jit_context ctx;
 	int ret;
 
diff --git a/arch/s390/net/bpf_jit_comp.c b/arch/s390/net/bpf_jit_comp.c
index c4b47070bb590..1b2566012b63a 100644
--- a/arch/s390/net/bpf_jit_comp.c
+++ b/arch/s390/net/bpf_jit_comp.c
@@ -2566,6 +2566,8 @@ struct bpf_tramp_jit {
 	int r14_off;		/* Offset of saved %r14, has to be at the
 				 * bottom */
 	int do_fexit;		/* do_fexit: label */
+	int skip[BPF_MAX_TRAMP_LINKS];	/* skip: labels after each prog */
+	int nr_progs;
 };
 
 static void load_imm64(struct bpf_jit *jit, int dst_reg, u64 val)
@@ -2584,6 +2586,7 @@ static void emit_store_stack_imm64(struct bpf_jit *jit, int tmp_reg, int stack_o
 }
 
 static int invoke_bpf_prog(struct bpf_tramp_jit *tjit,
+			   struct bpf_tramp_image *im,
 			   const struct btf_func_model *m,
 			   struct bpf_tramp_node *node, bool save_ret)
 {
@@ -2591,8 +2594,20 @@ static int invoke_bpf_prog(struct bpf_tramp_jit *tjit,
 	int cookie_off = tjit->run_ctx_off +
 			 offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
 	struct bpf_prog *p = node->link->prog;
+	void *skip = jit->prg_buf + jit->prg;
+	int idx = tjit->nr_progs++;
 	int patch;
 
+	if (idx >= ARRAY_SIZE(tjit->skip))
+		return -E2BIG;
+
+	/*
+	 * nop, patched to skip this prog when it is detached
+	 */
+
+	/* brcl 0,skip */
+	EMIT6_PCREL_RILC(0xc0040000, 0, tjit->skip[idx]);
+
 	/*
 	 * run_ctx.cookie = node->cookie;
 	 */
@@ -2652,10 +2667,15 @@ static int invoke_bpf_prog(struct bpf_tramp_jit *tjit,
 	/* brasl %r14,__bpf_prog_exit */
 	EMIT6_PCREL_RILB_PTR(0xc0050000, REG_14, bpf_trampoline_exit(p));
 
+	/* skip: */
+	tjit->skip[idx] = jit->prg;
+	bpf_tramp_image_add_skip(im, p, skip, jit->prg_buf + jit->prg);
+
 	return 0;
 }
 
 static int invoke_bpf(struct bpf_tramp_jit *tjit,
+		      struct bpf_tramp_image *im,
 		      const struct btf_func_model *m,
 		      struct bpf_tramp_nodes *tn, bool save_ret,
 		      u64 func_meta, int cookie_off)
@@ -2670,7 +2690,7 @@ static int invoke_bpf(struct bpf_tramp_jit *tjit,
 			emit_store_stack_imm64(jit, REG_0, tjit->func_meta_off, meta);
 			cur_cookie--;
 		}
-		if (invoke_bpf_prog(tjit, m, tn->nodes[i], save_ret))
+		if (invoke_bpf_prog(tjit, im, m, tn->nodes[i], save_ret))
 			return -EINVAL;
 	}
 
@@ -2712,6 +2732,11 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 	u64 func_meta;
 	int i, j;
 
+	/* The skip labels are taken from the previous pass. */
+	tjit->nr_progs = 0;
+	if (im)
+		im->nr_skips = 0;
+
 	/* Support as many stack arguments as "mvc" instruction can handle. */
 	nr_reg_args = min_t(int, m->nr_args, MAX_NR_REG_ARGS);
 	nr_stack_args = m->nr_args - nr_reg_args;
@@ -2875,7 +2900,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 		emit_store_stack_imm64(jit, REG_0, tjit->retval_off, 0);
 	}
 
-	if (invoke_bpf(tjit, m, fentry, flags & BPF_TRAMP_F_RET_FENTRY_RET,
+	if (invoke_bpf(tjit, im, m, fentry, flags & BPF_TRAMP_F_RET_FENTRY_RET,
 		       func_meta, cookie_off))
 		return -EINVAL;
 
@@ -2889,7 +2914,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 		       0xf000 | tjit->retval_off);
 
 		for (i = 0; i < fmod_ret->nr_nodes; i++) {
-			if (invoke_bpf_prog(tjit, m, fmod_ret->nodes[i], true))
+			if (invoke_bpf_prog(tjit, im, m, fmod_ret->nodes[i], true))
 				return -EINVAL;
 
 			/*
@@ -2943,15 +2968,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 		/* mvc tccnt_off(%r15),tail_call_cnt(4,%r15) */
 		_EMIT6(0xd203f000 | tjit->tccnt_off,
 		       0xf000 | offsetof(struct prog_frame, tail_call_cnt));
-
-		im->ip_after_call = jit->prg_buf + jit->prg;
-
-		/*
-		 * The following nop will be patched by bpf_tramp_image_put().
-		 */
-
-		/* brcl 0,im->ip_epilogue */
-		EMIT6_PCREL_RILC(0xc0040000, 0, (u64)im->ip_epilogue);
 	}
 
 	/* Set the "is_return" flag for fsession. */
@@ -2962,12 +2978,10 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 
 	/* do_fexit: */
 	tjit->do_fexit = jit->prg;
-	if (invoke_bpf(tjit, m, fexit, false, func_meta, cookie_off))
+	if (invoke_bpf(tjit, im, m, fexit, false, func_meta, cookie_off))
 		return -EINVAL;
 
 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
-		im->ip_epilogue = jit->prg_buf + jit->prg;
-
 		/*
 		 * __bpf_tramp_exit(im);
 		 */
@@ -3016,7 +3030,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
 			     struct bpf_tramp_nodes *tnodes, void *orig_call)
 {
-	struct bpf_tramp_image im;
+	struct bpf_tramp_image im = {};
 	struct bpf_tramp_jit tjit;
 	int ret;
 
diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index 2853e87797a78..6bca87457e872 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -3217,16 +3217,21 @@ static void restore_regs(const struct btf_func_model *m, u8 **prog,
 }
 
 static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,
+			   struct bpf_tramp_image *im,
 			   struct bpf_tramp_node *node, int stack_size,
 			   int run_ctx_off, bool save_ret,
 			   void *image, void *rw_image)
 {
 	u8 *prog = *pprog;
-	u8 *jmp_insn;
+	u8 *jmp_insn, *skip;
 	int ctx_cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
 	struct bpf_prog *p = node->link->prog;
 	u64 cookie = node->cookie;
 
+	/* nop, patched to skip this prog when it is detached */
+	skip = image + (prog - (u8 *)rw_image);
+	emit_nops(&prog, X86_PATCH_SIZE);
+
 	/* mov rdi, cookie */
 	emit_mov_imm64(&prog, BPF_REG_1, (long) cookie >> 32, (u32) (long) cookie);
 
@@ -3301,6 +3306,8 @@ static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,
 	if (emit_rsb_call(&prog, bpf_trampoline_exit(p), image + (prog - (u8 *)rw_image)))
 		return -EINVAL;
 
+	bpf_tramp_image_add_skip(im, p, skip, image + (prog - (u8 *)rw_image));
+
 	*pprog = prog;
 	return 0;
 }
@@ -3332,6 +3339,7 @@ static int emit_cond_near_jump(u8 **pprog, void *func, void *ip, u8 jmp_cond)
 }
 
 static int invoke_bpf(const struct btf_func_model *m, u8 **pprog,
+		      struct bpf_tramp_image *im,
 		      struct bpf_tramp_nodes *tl, int stack_size,
 		      int run_ctx_off, int func_meta_off, bool save_ret,
 		      void *image, void *rw_image, u64 func_meta,
@@ -3346,7 +3354,7 @@ static int invoke_bpf(const struct btf_func_model *m, u8 **pprog,
 				func_meta | (cur_cookie << BPF_TRAMP_COOKIE_INDEX_SHIFT));
 			cur_cookie--;
 		}
-		if (invoke_bpf_prog(m, &prog, tl->nodes[i], stack_size,
+		if (invoke_bpf_prog(m, &prog, im, tl->nodes[i], stack_size,
 				    run_ctx_off, save_ret, image, rw_image))
 			return -EINVAL;
 	}
@@ -3355,6 +3363,7 @@ static int invoke_bpf(const struct btf_func_model *m, u8 **pprog,
 }
 
 static int invoke_bpf_mod_ret(const struct btf_func_model *m, u8 **pprog,
+			      struct bpf_tramp_image *im,
 			      struct bpf_tramp_nodes *tl, int stack_size,
 			      int run_ctx_off, u8 **branches,
 			      void *image, void *rw_image)
@@ -3368,7 +3377,7 @@ static int invoke_bpf_mod_ret(const struct btf_func_model *m, u8 **pprog,
 	emit_mov_imm32(&prog, false, BPF_REG_0, 0);
 	emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8);
 	for (i = 0; i < tl->nr_nodes; i++) {
-		if (invoke_bpf_prog(m, &prog, tl->nodes[i], stack_size, run_ctx_off, true,
+		if (invoke_bpf_prog(m, &prog, im, tl->nodes[i], stack_size, run_ctx_off, true,
 				    image, rw_image))
 			return -EINVAL;
 
@@ -3640,7 +3649,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 	}
 
 	if (fentry->nr_nodes) {
-		if (invoke_bpf(m, &prog, fentry, regs_off, run_ctx_off, func_meta_off,
+		if (invoke_bpf(m, &prog, im, fentry, regs_off, run_ctx_off, func_meta_off,
 			       flags & BPF_TRAMP_F_RET_FENTRY_RET, image, rw_image,
 			       func_meta, cookie_off))
 			return -EINVAL;
@@ -3652,7 +3661,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 		if (!branches)
 			return -ENOMEM;
 
-		if (invoke_bpf_mod_ret(m, &prog, fmod_ret, regs_off,
+		if (invoke_bpf_mod_ret(m, &prog, im, fmod_ret, regs_off,
 				       run_ctx_off, branches, image, rw_image)) {
 			ret = -EINVAL;
 			goto cleanup;
@@ -3682,8 +3691,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 		}
 		/* remember return value in a stack for bpf prog to access */
 		emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8);
-		im->ip_after_call = image + (prog - (u8 *)rw_image);
-		emit_nops(&prog, X86_PATCH_SIZE);
 	}
 
 	if (fmod_ret->nr_nodes) {
@@ -3708,7 +3715,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 		emit_store_stack_imm64(&prog, BPF_REG_0, -func_meta_off, func_meta);
 
 	if (fexit->nr_nodes) {
-		if (invoke_bpf(m, &prog, fexit, regs_off, run_ctx_off, func_meta_off,
+		if (invoke_bpf(m, &prog, im, fexit, regs_off, run_ctx_off, func_meta_off,
 			       false, image, rw_image, func_meta, cookie_off)) {
 			ret = -EINVAL;
 			goto cleanup;
@@ -3723,7 +3730,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
 	 * restored to R0.
 	 */
 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
-		im->ip_epilogue = image + (prog - (u8 *)rw_image);
 		/* arg1: mov rdi, im */
 		emit_mov_imm64(&prog, BPF_REG_1, (long) im >> 32, (u32) (long) im);
 		if (emit_rsb_call(&prog, __bpf_tramp_exit, image + (prog - (u8 *)rw_image))) {
@@ -3811,7 +3817,7 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i
 int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
 			     struct bpf_tramp_nodes *tnodes, void *func_addr)
 {
-	struct bpf_tramp_image im;
+	struct bpf_tramp_image im = {};
 	void *image;
 	int ret;
 
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 1d2676782d707..d5b507f090ebe 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1258,11 +1258,17 @@ struct btf_func_model {
 #define BPF_TRAMP_F_INDIRECT		BIT(8)
 
 /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
- * bytes on x86.
+ * bytes on x86. The trampoline image has to fit in PAGE_SIZE.
  */
 enum {
-#if defined(__s390x__)
+#if defined(__s390x__) || defined(__powerpc64__)
 	BPF_MAX_TRAMP_LINKS = 27,
+#elif defined(__loongarch__)
+	BPF_MAX_TRAMP_LINKS = 33,
+#elif defined(__x86_64__)
+	BPF_MAX_TRAMP_LINKS = 36,
+#elif defined(__aarch64__)
+	BPF_MAX_TRAMP_LINKS = 37,
 #else
 	BPF_MAX_TRAMP_LINKS = 38,
 #endif
@@ -1363,19 +1369,48 @@ enum bpf_tramp_prog_type {
 	BPF_TRAMP_FSESSION,
 };
 
+/*
+ * Each prog call in a trampoline image is preceded by a nop. When the prog is
+ * detached, the nop is patched to a jump to target, right after the call, so
+ * that tasks still running in the image skip the prog.
+ */
+struct bpf_tramp_skip {
+	struct bpf_prog *prog;
+	void *nop;
+	void *target;
+};
+
 struct bpf_tramp_image {
 	void *image;
 	int size;
 	struct bpf_ksym ksym;
 	struct percpu_ref pcref;
-	void *ip_after_call;
-	void *ip_epilogue;
+	bool call_orig;
+	/* entry in tr->images, the image holds a reference on tr */
+	struct bpf_trampoline *tr;
+	struct list_head list;
+	int nr_skips;
+	struct bpf_tramp_skip *skips;
 	union {
 		struct rcu_head rcu;
 		struct work_struct work;
 	};
 };
 
+static inline void bpf_tramp_image_add_skip(struct bpf_tramp_image *im, struct bpf_prog *prog,
+					    void *nop, void *target)
+{
+	struct bpf_tramp_skip *skip;
+
+	/* struct_ops trampolines and arch_bpf_trampoline_size() have no image */
+	if (!im || !im->skips)
+		return;
+	skip = &im->skips[im->nr_skips++];
+	skip->prog = prog;
+	skip->nop = nop;
+	skip->target = target;
+}
+
 struct bpf_trampoline {
 	/* hlist for trampoline_key_table */
 	struct hlist_node hlist_key;
@@ -1402,6 +1437,8 @@ struct bpf_trampoline {
 	int progs_cnt[BPF_TRAMP_MAX];
 	/* Executable image of trampoline */
 	struct bpf_tramp_image *cur_image;
+	/* Images not freed yet, cur_image and older ones still in use */
+	struct list_head images;
 	/* Used as temporary old image storage for multi_attach */
 	struct {
 		struct bpf_tramp_image *old_image;
@@ -1770,6 +1807,7 @@ struct bpf_prog_aux {
 	bool offload_requested; /* Program is bound and offloaded to the netdev. */
 	bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
 	bool attach_tracing_prog; /* true if tracing another tracing program */
+	bool tramp_linked; /* true if it was ever called from a trampoline */
 	bool func_proto_unreliable;
 	bool tail_call_reachable;
 	bool xdp_has_frags;
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 244a939b9d2da..96217b99399d5 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2448,6 +2448,21 @@ static void __bpf_prog_put_rcu(struct rcu_head *rcu)
 	bpf_prog_free(aux->prog);
 }
 
+/*
+ * Progs called from a trampoline can also be reached by a task that was
+ * preempted in the trampoline before the prog's enter helper took its RCU
+ * read lock, wait for those first.
+ */
+static void __bpf_prog_put_rcu_tasks(struct rcu_head *rcu)
+{
+	struct bpf_prog *prog = container_of(rcu, struct bpf_prog_aux, rcu)->prog;
+
+	if (prog->sleepable)
+		call_rcu_tasks_trace(rcu, __bpf_prog_put_rcu);
+	else
+		call_rcu(rcu, __bpf_prog_put_rcu);
+}
+
 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
 {
 	bpf_prog_kallsyms_del_all(prog);
@@ -2461,7 +2476,9 @@ static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
 		btf_put(prog->aux->attach_btf);
 
 	if (deferred) {
-		if (prog->sleepable)
+		if (IS_ENABLED(CONFIG_TASKS_RCU) && prog->aux->tramp_linked)
+			call_rcu_tasks(&prog->aux->rcu, __bpf_prog_put_rcu_tasks);
+		else if (prog->sleepable)
 			call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
 		else
 			call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
index 90b70ea0d3709..ea8e67a389194 100644
--- a/kernel/bpf/trampoline.c
+++ b/kernel/bpf/trampoline.c
@@ -401,6 +401,7 @@ static struct bpf_trampoline *bpf_trampoline_lookup(u64 key, unsigned long ip)
 	head = &trampoline_ip_table[hash_64(tr->ip, TRAMPOLINE_HASH_BITS)];
 	hlist_add_head(&tr->hlist_ip, head);
 	refcount_set(&tr->refcnt, 1);
+	INIT_LIST_HEAD(&tr->images);
 	for (i = 0; i < BPF_TRAMP_MAX; i++)
 		INIT_HLIST_HEAD(&tr->progs_hlist[i]);
 out:
@@ -565,15 +566,22 @@ static void bpf_tramp_image_free(struct bpf_tramp_image *im)
 	arch_free_bpf_trampoline(im->image, im->size);
 	bpf_jit_uncharge_modmem(im->size);
 	percpu_ref_exit(&im->pcref);
+	kfree(im->skips);
 	kfree_rcu(im, rcu);
 }
 
 static void __bpf_tramp_image_put_deferred(struct work_struct *work)
 {
 	struct bpf_tramp_image *im;
+	struct bpf_trampoline *tr;
 
 	im = container_of(work, struct bpf_tramp_image, work);
+	tr = im->tr;
+	trampoline_lock(tr);
+	list_del(&im->list);
+	trampoline_unlock(tr);
 	bpf_tramp_image_free(im);
+	bpf_trampoline_put(tr);
 }
 
 /* callback, fexit step 3 or fentry step 2 */
@@ -601,7 +609,7 @@ static void __bpf_tramp_image_put_rcu_tasks(struct rcu_head *rcu)
 	struct bpf_tramp_image *im;
 
 	im = container_of(rcu, struct bpf_tramp_image, rcu);
-	if (im->ip_after_call)
+	if (im->call_orig)
 		/* the case of fmod_ret/fexit trampoline and CONFIG_PREEMPTION=y */
 		percpu_ref_kill(&im->pcref);
 	else
@@ -621,9 +629,9 @@ static void bpf_tramp_image_put(struct bpf_tramp_image *im)
 	 *
 	 * The trampoline is unreachable before bpf_tramp_image_put().
 	 *
-	 * First, patch the trampoline to avoid calling into fexit progs.
-	 * The progs will be freed even if the original function is still
-	 * executing or sleeping.
+	 * Progs are patched out of the image when they are detached, see
+	 * bpf_trampoline_skip_prog(), so they can be freed even if a task is
+	 * still in the image.
 	 * In case of CONFIG_PREEMPT=y use call_rcu_tasks() to wait on
 	 * first few asm instructions to execute and call into
 	 * __bpf_tramp_enter->percpu_ref_get.
@@ -637,11 +645,7 @@ static void bpf_tramp_image_put(struct bpf_tramp_image *im)
 	 * percpu_ref_kill will be waiting for. Hence the first
 	 * call_rcu_tasks() is not necessary.
 	 */
-	if (im->ip_after_call) {
-		int err = bpf_arch_text_poke(im->ip_after_call, BPF_MOD_NOP,
-					     BPF_MOD_JUMP, NULL,
-					     im->ip_epilogue);
-		WARN_ON(err);
+	if (im->call_orig) {
 		if (IS_ENABLED(CONFIG_TASKS_RCU))
 			call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu_tasks);
 		else
@@ -658,7 +662,7 @@ static void bpf_tramp_image_put(struct bpf_tramp_image *im)
 	call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks);
 }
 
-static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size)
+static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size, int nr_progs)
 {
 	struct bpf_tramp_image *im;
 	struct bpf_ksym *ksym;
@@ -669,6 +673,10 @@ static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size)
 	if (!im)
 		goto out;
 
+	im->skips = kzalloc_objs(*im->skips, nr_progs);
+	if (!im->skips)
+		goto out_free_im;
+
 	err = bpf_jit_charge_modmem(size);
 	if (err)
 		goto out_free_im;
@@ -695,6 +703,7 @@ static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size)
 out_uncharge:
 	bpf_jit_uncharge_modmem(size);
 out_free_im:
+	kfree(im->skips);
 	kfree(im);
 out:
 	return ERR_PTR(err);
@@ -771,11 +780,12 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut
 		goto out;
 	}
 
-	im = bpf_tramp_image_alloc(tr->key, size);
+	im = bpf_tramp_image_alloc(tr->key, size, total);
 	if (IS_ERR(im)) {
 		err = PTR_ERR(im);
 		goto out;
 	}
+	im->call_orig = tr->flags & BPF_TRAMP_F_CALL_ORIG;
 
 	err = arch_prepare_bpf_trampoline(im, im->image, im->image + size,
 					  &tr->func.model, tr->flags, tnodes,
@@ -806,8 +816,14 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut
 #endif
 
 out_free:
-	if (err)
+	if (err) {
 		bpf_tramp_image_free(im);
+	} else {
+		/* track the image until it is freed, for bpf_trampoline_skip_prog() */
+		refcount_inc(&tr->refcnt);
+		im->tr = tr;
+		list_add(&im->list, &tr->images);
+	}
 out:
 	/* If any error happens, restore previous flags */
 	if (err)
@@ -907,6 +923,7 @@ static int bpf_trampoline_add_prog(struct bpf_trampoline *tr,
 	}
 
 	hlist_add_head(&node->tramp_hlist, prog_list);
+	node->link->prog->aux->tramp_linked = true;
 	if (kind == BPF_TRAMP_FSESSION) {
 		tr->progs_cnt[BPF_TRAMP_FENTRY]++;
 		fexit = fsession_exit(node);
@@ -920,6 +937,32 @@ static int bpf_trampoline_add_prog(struct bpf_trampoline *tr,
 	return 0;
 }
 
+/*
+ * prog was detached and can be freed, but tasks may still be running in images
+ * that call it, sleeping in an earlier prog for example. They can be in any
+ * image that is not freed yet, not only in cur_image, so patch all of them to
+ * jump over prog.
+ */
+static void bpf_trampoline_skip_prog(struct bpf_trampoline *tr, struct bpf_prog *prog)
+{
+	struct bpf_tramp_image *im;
+	int i, err;
+
+	list_for_each_entry(im, &tr->images, list) {
+		for (i = 0; i < im->nr_skips; i++) {
+			struct bpf_tramp_skip *skip = &im->skips[i];
+
+			if (skip->prog != prog)
+				continue;
+			err = bpf_arch_text_poke(skip->nop, BPF_MOD_NOP, BPF_MOD_JUMP,
+						 NULL, skip->target);
+			WARN_ON_ONCE(err);
+			/* not a nop anymore, and prog's address can be reused */
+			skip->prog = NULL;
+		}
+	}
+}
+
 static void bpf_trampoline_remove_prog(struct bpf_trampoline *tr,
 				       struct bpf_tramp_node *node)
 {
@@ -937,6 +980,7 @@ static void bpf_trampoline_remove_prog(struct bpf_trampoline *tr,
 	}
 	hlist_del_init(&node->tramp_hlist);
 	tr->progs_cnt[kind]--;
+	bpf_trampoline_skip_prog(tr, node->link->prog);
 }
 
 static int __bpf_trampoline_link_prog(struct bpf_tramp_node *node,
@@ -1245,11 +1289,8 @@ void bpf_trampoline_put(struct bpf_trampoline *tr)
 		if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[i])))
 			goto out;
 
-	/* This code will be executed even when the last bpf_tramp_image
-	 * is alive. All progs are detached from the trampoline and the
-	 * trampoline image is patched with jmp into epilogue to skip
-	 * fexit progs. The fentry-only trampoline will be freed via
-	 * multiple rcu callbacks.
+	/* All progs are detached and the last image has been freed, images
+	 * hold a reference on the trampoline until then.
 	 */
 	hlist_del(&tr->hlist_key);
 	hlist_del(&tr->hlist_ip);
diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_mod_race.c b/tools/testing/selftests/bpf/prog_tests/bpf_mod_race.c
index ecc3d47919ad6..f8497e764beb5 100644
--- a/tools/testing/selftests/bpf/prog_tests/bpf_mod_race.c
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_mod_race.c
@@ -55,38 +55,6 @@ static void *load_module_thread(void *p)
 	return p;
 }
 
-static int sys_userfaultfd(int flags)
-{
-	return syscall(__NR_userfaultfd, flags);
-}
-
-static int test_setup_uffd(void *fault_addr)
-{
-	struct uffdio_register uffd_register = {};
-	struct uffdio_api uffd_api = {};
-	int uffd;
-
-	uffd = sys_userfaultfd(O_CLOEXEC);
-	if (uffd < 0)
-		return -errno;
-
-	uffd_api.api = UFFD_API;
-	uffd_api.features = 0;
-	if (ioctl(uffd, UFFDIO_API, &uffd_api)) {
-		close(uffd);
-		return -1;
-	}
-
-	uffd_register.range.start = (unsigned long)fault_addr;
-	uffd_register.range.len = getpagesize();
-	uffd_register.mode = UFFDIO_REGISTER_MODE_MISSING;
-	if (ioctl(uffd, UFFDIO_REGISTER, &uffd_register)) {
-		close(uffd);
-		return -1;
-	}
-	return uffd;
-}
-
 static void test_bpf_mod_race_config(const struct test_config *config)
 {
 	void *fault_addr, *skel_fail;
@@ -117,7 +85,7 @@ static void test_bpf_mod_race_config(const struct test_config *config)
 	if (!ASSERT_OK(bpf_mod_race__attach(skel), "bpf_mod_kfunc_race__attach"))
 		goto end_destroy;
 
-	uffd = test_setup_uffd(fault_addr);
+	uffd = uffd_block_page(fault_addr);
 	if (!ASSERT_GE(uffd, 0, "userfaultfd open + register address"))
 		goto end_destroy;
 
diff --git a/tools/testing/selftests/bpf/prog_tests/tramp_prog_detach.c b/tools/testing/selftests/bpf/prog_tests/tramp_prog_detach.c
new file mode 100644
index 0000000000000..1eb0d72376055
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/tramp_prog_detach.c
@@ -0,0 +1,188 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <pthread.h>
+#include <poll.h>
+#include <sys/mman.h>
+#include <linux/userfaultfd.h>
+#include "tramp_prog_detach.skel.h"
+#include "testing_helpers.h"
+
+/*
+ * Detach and free progs while a task sleeps in the prog that runs before them
+ * in the same trampoline image, then let that task continue through the
+ * image. It must not call into the freed progs.
+ *
+ * The task is held in a sleepable prog with userfaultfd, like bpf_mod_race
+ * does.
+ */
+
+static struct bpf_program *pick_prog(struct tramp_prog_detach *skel,
+				     bool fexit, bool sleepable)
+{
+	if (fexit)
+		return sleepable ? skel->progs.fexit_sleepable :
+				   skel->progs.fexit_victim;
+	return sleepable ? skel->progs.fentry_sleepable :
+			   skel->progs.fentry_victim;
+}
+
+static struct bpf_program *sleepable_prog;
+
+static void *run_sleepable(void *arg)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+
+	/* calls bpf_fentry_test1() */
+	return (void *)(long)bpf_prog_test_run_opts(bpf_program__fd(sleepable_prog),
+						    &topts);
+}
+
+static struct tramp_prog_detach *load_one(bool fexit, bool sleepable)
+{
+	struct tramp_prog_detach *skel;
+	int err;
+
+	skel = tramp_prog_detach__open();
+	if (!ASSERT_OK_PTR(skel, "open"))
+		return NULL;
+
+	bpf_program__set_autoload(pick_prog(skel, fexit, sleepable), true);
+	err = tramp_prog_detach__load(skel);
+	if (!ASSERT_OK(err, "load"))
+		goto err;
+	skel->bss->pid = getpid();
+	err = tramp_prog_detach__attach(skel);
+	if (!ASSERT_OK(err, "attach"))
+		goto err;
+	return skel;
+err:
+	tramp_prog_detach__destroy(skel);
+	return NULL;
+}
+
+/* The .bss map of a destroyed skeleton goes away when its prog is freed */
+static bool wait_for_map_free(__u32 map_id)
+{
+	int i, fd;
+
+	for (i = 0; i < 100; i++) {
+		fd = bpf_map_get_fd_by_id(map_id);
+		if (fd < 0)
+			return true;
+		close(fd);
+		usleep(100 * 1000);
+	}
+	return false;
+}
+
+static void test_detach(bool sleepable_fexit, bool victim_fexit)
+{
+	struct tramp_prog_detach *sleepable = NULL, *victims[2] = {};
+	struct pollfd pfd = { .events = POLLIN };
+	struct uffdio_copy uffd_copy = {};
+	struct bpf_map_info map_info = {};
+	__u32 map_info_len = sizeof(map_info);
+	struct uffd_msg uffd_msg;
+	void *fault_page, *src_page = MAP_FAILED;
+	long page_size = getpagesize();
+	bool started = false;
+	void *thread_ret;
+	pthread_t thread;
+	int i, uffd = -1;
+
+	fault_page = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
+			  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	if (!ASSERT_NEQ(fault_page, MAP_FAILED, "mmap fault_page"))
+		return;
+	src_page = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
+			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	if (!ASSERT_NEQ(src_page, MAP_FAILED, "mmap src_page"))
+		goto out;
+
+	/* The most recently attached prog runs first */
+	for (i = 0; i < ARRAY_SIZE(victims); i++) {
+		victims[i] = load_one(victim_fexit, false);
+		if (!victims[i])
+			goto out;
+	}
+	sleepable = load_one(sleepable_fexit, true);
+	if (!sleepable)
+		goto out;
+	sleepable_prog = pick_prog(sleepable, sleepable_fexit, true);
+
+	/* Not armed yet so this doesn't block, make sure sleepable runs first */
+	if (!ASSERT_OK((long)run_sleepable(NULL), "dry run"))
+		goto out;
+	for (i = 0; i < ARRAY_SIZE(victims); i++)
+		if (!ASSERT_LT(sleepable->bss->ts, victims[i]->bss->ts, "prog order"))
+			goto out;
+
+	uffd = uffd_block_page(fault_page);
+	if (!ASSERT_GE(uffd, 0, "userfaultfd open + register address"))
+		goto out;
+	sleepable->bss->fault_addr = fault_page;
+
+	if (!ASSERT_OK(pthread_create(&thread, NULL, run_sleepable, NULL),
+		       "pthread_create"))
+		goto out;
+	started = true;
+
+	/* Wait for the thread to sleep in bpf_copy_from_user() */
+	pfd.fd = uffd;
+	if (!ASSERT_EQ(poll(&pfd, 1, 10000), 1, "poll uffd"))
+		goto out;
+	if (!ASSERT_EQ(read(uffd, &uffd_msg, sizeof(uffd_msg)), sizeof(uffd_msg),
+		       "read uffd"))
+		goto out;
+	if (!ASSERT_EQ(uffd_msg.event, UFFD_EVENT_PAGEFAULT, "uffd pagefault"))
+		goto out;
+
+	/*
+	 * Detach and unload the victim progs and wait for them to be freed.
+	 * After the first one, the task is in an image that isn't the
+	 * trampoline's current one anymore.
+	 */
+	for (i = 0; i < ARRAY_SIZE(victims); i++) {
+		if (!ASSERT_OK(bpf_map_get_info_by_fd(bpf_map__fd(victims[i]->maps.bss),
+						      &map_info, &map_info_len),
+			       "victim bss info"))
+			goto out;
+		tramp_prog_detach__destroy(victims[i]);
+		victims[i] = NULL;
+		if (!ASSERT_TRUE(wait_for_map_free(map_info.id), "victim freed"))
+			goto out;
+	}
+
+out:
+	/* Let the thread proceed with the rest of the trampoline */
+	if (uffd >= 0) {
+		uffd_copy.dst = (unsigned long)fault_page;
+		uffd_copy.src = (unsigned long)src_page;
+		uffd_copy.len = page_size;
+		ASSERT_OK(ioctl(uffd, UFFDIO_COPY, &uffd_copy), "uffd copy");
+		close(uffd);
+	}
+	if (started &&
+	    ASSERT_OK(pthread_join(thread, &thread_ret), "pthread_join"))
+		ASSERT_NULL(thread_ret, "blocking run");
+
+	for (i = 0; i < ARRAY_SIZE(victims); i++)
+		tramp_prog_detach__destroy(victims[i]);
+	tramp_prog_detach__destroy(sleepable);
+	if (src_page != MAP_FAILED)
+		munmap(src_page, page_size);
+	munmap(fault_page, page_size);
+}
+
+void serial_test_tramp_prog_detach(void)
+{
+	/* a task sleeping before the original function is called */
+	if (test__start_subtest("fentry"))
+		test_detach(false, false);
+	/* a task sleeping after the original function returned */
+	if (test__start_subtest("fexit"))
+		test_detach(true, true);
+	/* the original function runs in between and must still be called */
+	if (test__start_subtest("fentry_fexit"))
+		test_detach(false, true);
+}
diff --git a/tools/testing/selftests/bpf/progs/tramp_prog_detach.c b/tools/testing/selftests/bpf/progs/tramp_prog_detach.c
new file mode 100644
index 0000000000000..507d372167ecb
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/tramp_prog_detach.c
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+int pid;
+void *fault_addr;
+__u64 ts;
+
+static int do_sleepable(void)
+{
+	char dst;
+
+	if (bpf_get_current_pid_tgid() >> 32 != pid)
+		return 0;
+
+	ts = bpf_ktime_get_ns();
+	/* blocks for as long as user space wants when fault_addr is armed */
+	bpf_copy_from_user(&dst, sizeof(dst), fault_addr);
+	return 0;
+}
+
+static int do_victim(void)
+{
+	if (bpf_get_current_pid_tgid() >> 32 != pid)
+		return 0;
+
+	ts = bpf_ktime_get_ns();
+	return 0;
+}
+
+SEC("?fentry.s/bpf_fentry_test1")
+int BPF_PROG(fentry_sleepable, int a)
+{
+	return do_sleepable();
+}
+
+SEC("?fentry/bpf_fentry_test1")
+int BPF_PROG(fentry_victim, int a)
+{
+	return do_victim();
+}
+
+SEC("?fexit.s/bpf_fentry_test1")
+int BPF_PROG(fexit_sleepable, int a, int ret)
+{
+	return do_sleepable();
+}
+
+SEC("?fexit/bpf_fentry_test1")
+int BPF_PROG(fexit_victim, int a, int ret)
+{
+	return do_victim();
+}
diff --git a/tools/testing/selftests/bpf/testing_helpers.c b/tools/testing/selftests/bpf/testing_helpers.c
index c970e7793dfcb..9f672d0c9dda2 100644
--- a/tools/testing/selftests/bpf/testing_helpers.c
+++ b/tools/testing/selftests/bpf/testing_helpers.c
@@ -13,6 +13,7 @@
 #include "test_progs.h"
 #include "testing_helpers.h"
 #include <linux/membarrier.h>
+#include <linux/userfaultfd.h>
 
 int parse_num_list(const char *s, bool **num_set, int *num_set_len)
 {
@@ -459,6 +460,33 @@ int kern_sync_rcu(void)
 	return syscall(__NR_membarrier, MEMBARRIER_CMD_SHARED, 0, 0);
 }
 
+int uffd_block_page(void *fault_addr)
+{
+	struct uffdio_register uffd_register = {};
+	struct uffdio_api uffd_api = {};
+	int uffd;
+
+	uffd = syscall(__NR_userfaultfd, O_CLOEXEC);
+	if (uffd < 0)
+		return -errno;
+
+	uffd_api.api = UFFD_API;
+	uffd_api.features = 0;
+	if (ioctl(uffd, UFFDIO_API, &uffd_api)) {
+		close(uffd);
+		return -1;
+	}
+
+	uffd_register.range.start = (unsigned long)fault_addr;
+	uffd_register.range.len = getpagesize();
+	uffd_register.mode = UFFDIO_REGISTER_MODE_MISSING;
+	if (ioctl(uffd, UFFDIO_REGISTER, &uffd_register)) {
+		close(uffd);
+		return -1;
+	}
+	return uffd;
+}
+
 int get_xlated_program(int fd_prog, struct bpf_insn **buf, __u32 *cnt)
 {
 	__u32 buf_element_size = sizeof(struct bpf_insn);
diff --git a/tools/testing/selftests/bpf/testing_helpers.h b/tools/testing/selftests/bpf/testing_helpers.h
index 2edc6fb7fc521..1f83b6080716f 100644
--- a/tools/testing/selftests/bpf/testing_helpers.h
+++ b/tools/testing/selftests/bpf/testing_helpers.h
@@ -36,6 +36,8 @@ __u64 read_perf_max_sample_freq(void);
 int load_bpf_testmod(bool verbose);
 int unload_bpf_testmod(bool verbose);
 int kern_sync_rcu(void);
+/* returns a userfaultfd that makes accesses to fault_addr's page block */
+int uffd_block_page(void *fault_addr);
 int finit_module(int fd, const char *param_values, int flags);
 int delete_module(const char *name, int flags);
 int load_module(const char *path, bool verbose);

47/3 2026/09/27 09:14 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 23640
							output: 39
							thoughts: 1141
						

Thoughts:
**BPF Trampoline Detachment: Memory Safety Analysis**

Okay, let's break down this BPF trampoline patch. My initial focus is on the memory aspects, particularly around the introduction of the new skip mechanism for detached programs. The target architecture, for this analysis, is amd64.

The core idea is to handle detached BPF programs within trampoline images gracefully. Previously, there were `ip_after_call` and `ip_epilogue` in the image when a BPF trampoline detached progs or when the image was freed. The patch introduces a `nop` before each program call within the trampoline image. When a program is detached, this `nop` gets patched to a jump that bypasses the detached program for any still-running tasks in the trampoline image.

Key data structure: `struct bpf_tramp_skip`, which holds pointers to the program (`prog`), the `nop` instruction (`nop`), and the jump target (`target`). These `skip` entries are managed in an array `im->skips` within the `bpf_tramp_image`.

The code seems to allocate the `im->skips` array with `kzalloc_objs`, ensuring zero-initialization which is a good thing for safety.  `bpf_tramp_image_add_skip` adds skip entries, which is called in the architecture-specific trampoline emission code (arm64, loongarch, ppc, riscv, s390, x86). It looks like the architectures emit `nops` before the program calls and then record the information about which instruction to skip in each skip struct.

Now, the critical part: memory safety. The big question is whether this patch introduces any uninitialized memory reads or writes. My analysis focuses on:

1.  **Kernel structures sent to user space:**  The new structures (`struct bpf_tramp_skip`, fields within `struct bpf_tramp_image`, etc.) are *purely* kernel-internal. So, no risk there of leaking uninitialized data to userspace via the various methods I know, such as `copy_to_user`. Phew.

2.  **Conditional logic depending on uninitialized variables:** This is the more interesting part.  I've been looking very carefully at how `bpf_tramp_image` is initialized, particularly in `arch_bpf_trampoline_size()`. Previously, on architectures like arm64, ppc, riscv, s390, and x86, `im` was declared as `struct bpf_tramp_image im;` which is uninitialized on the stack!  And `bpf_tramp_image_add_skip` was calling `im->skips` which would be undefined, and potentially lead to badness. *However* the patch changes this to `struct bpf_tramp_image im = {};` meaning it is initialized as zero. This ensures that `im->skips` is `NULL` when the function begins, so the conditional `!im || !im->skips` in `bpf_tramp_image_add_skip` is safe, and importantly prevents a read of uninitialized memory. That's a clever and subtle fix. It prevents an exploitable flaw!

Also, checking `bpf_tramp_image_alloc`, I confirmed that the `im->skips` array is allocated via `kzalloc_objs`, which further guarantees zero-initialization. The structure itself, `im`, is allocated with `kzalloc`. So, on the whole, the memory handling within this patch seems sound. The changes made to zero-initialize the `bpf_tramp_image` seem to have removed any potential uninitialized memory risks.  I would still expect this to be tested on the target architectures, and for more advanced static analysis tools to be brought to bear on the code, but I'm cautiously optimistic, from a memory safety perspective.



48/3 2026/09/27 09:14 tool grepper
0m Error:
no matches

Args:
{
  "Expression": "kzalloc.*bpf_tramp_image",
  "PathPrefix": "kernel/bpf/trampoline.c"
}

Results:
{
  "error": "no matches"
}

49/3 2026/09/27 09:14 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 25677
							output: 37
							thoughts: 22
						

Thoughts:
Okay, let's dive into this. Here's my thought process:

**Diving into kernel/bpf/trampoline.c, around Line 665**

Right, so I'm thinking, let's zero in on `kernel/bpf/trampoline.c` and specifically, that area around line 665. I'm assuming that's where we're looking to understand something specific about the BPF trampoline mechanism. Given my expertise, I already have a mental model of the general flow, the key data structures, and the purpose of the trampoline code. Now, I'm going to pinpoint a specific code region to analyse, based on the question at hand. I'll read the code with the relevant concepts in mind. Probably, there's some interesting logic there, perhaps related to error handling, function linking, or trampoline execution itself. I'm prepared to interpret what I find within the context of the larger BPF ecosystem and identify the likely role of the code I examine.



50/3 2026/09/27 09:14 tool read-file
0m Args:
{
  "File": "kernel/bpf/trampoline.c",
  "FirstLine": 660,
  "LineCount": 45
}

Results:
{
  "Contents": " 660:\t\t * and normal progs.\n 661:\t\t */\n 662:\t\tcall_rcu_tasks_trace(\u0026im-\u003ercu, __bpf_tramp_image_put_rcu_tasks);\n 663:\t}\n 664:\t\n 665:\tstatic struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size, int nr_progs)\n 666:\t{\n 667:\t\tstruct bpf_tramp_image *im;\n 668:\t\tstruct bpf_ksym *ksym;\n 669:\t\tvoid *image;\n 670:\t\tint err = -ENOMEM;\n 671:\t\n 672:\t\tim = kzalloc_obj(*im);\n 673:\t\tif (!im)\n 674:\t\t\tgoto out;\n 675:\t\n 676:\t\tim-\u003eskips = kzalloc_objs(*im-\u003eskips, nr_progs);\n 677:\t\tif (!im-\u003eskips)\n 678:\t\t\tgoto out_free_im;\n 679:\t\n 680:\t\terr = bpf_jit_charge_modmem(size);\n 681:\t\tif (err)\n 682:\t\t\tgoto out_free_im;\n 683:\t\tim-\u003esize = size;\n 684:\t\n 685:\t\terr = -ENOMEM;\n 686:\t\tim-\u003eimage = image = arch_alloc_bpf_trampoline(size);\n 687:\t\tif (!image)\n 688:\t\t\tgoto out_uncharge;\n 689:\t\n 690:\t\terr = percpu_ref_init(\u0026im-\u003epcref, __bpf_tramp_image_release, 0, GFP_KERNEL);\n 691:\t\tif (err)\n 692:\t\t\tgoto out_free_image;\n 693:\t\n 694:\t\tksym = \u0026im-\u003eksym;\n 695:\t\tINIT_LIST_HEAD_RCU(\u0026ksym-\u003elnode);\n 696:\t\tsnprintf(ksym-\u003ename, KSYM_NAME_LEN, \"bpf_trampoline_%llu\", key);\n 697:\t\tbpf_image_ksym_init(image, size, ksym);\n 698:\t\tbpf_image_ksym_add(ksym);\n 699:\t\treturn im;\n 700:\t\n 701:\tout_free_image:\n 702:\t\tarch_free_bpf_trampoline(im-\u003eimage, im-\u003esize);\n 703:\tout_uncharge:\n 704:\t\tbpf_jit_uncharge_modmem(size);\n"
}

51/3 2026/09/27 09:14 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 26672
							output: 37
							thoughts: 254
						

Thoughts:
**My Analysis of BPF Trampoline Initialization**

Okay, so I'm looking at this BPF trampoline initialization, specifically the memory allocation and skip list management. First, I see `im = kzalloc_obj(*im);` and `im->skips = kzalloc_objs(*im->skips, nr_progs);`. Good, both are fully zero-allocated using `kzalloc`. That's standard and prevents any potential garbage data issues, assuming the structures are properly defined.

Now, I'm immediately thinking about potential architecture-specific concerns. Does `arch_bpf_trampoline_size` play any role here? I need to keep that in mind. Then I see that, across all architectures, `struct bpf_tramp_image im = {};` is zero-initialized. Excellent, consistency is key, and this confirms the initial zero-state.

Moving onto `bpf_tramp_image_add_skip`, there's a quick check for null pointers: `if (!im || !im->skips) return;`. Standard null check. If it passes:

```c
skip = &im->skips[im->nr_skips++];
skip->prog = prog;
skip->nop = nop;
skip->target = target;
```

Alright, this increments `nr_skips` and assigns the skip information. *This* is where I need to be careful. Is there a risk of `nr_skips` overflowing `nr_progs`? That's a classic off-by-one or integer overflow scenario, and a potential security vulnerability. I need to nail down the bounds on those values.

Let's see how `nr_progs` is actually computed. The code says: In `bpf_trampoline_update`:

```c
total = 0;
// total is computed from tnodes / progs
```

Okay, so `total` is computed from something related to `tnodes` or `progs`. I'm going to have to dive into that calculation to understand the limits. I know where that code is, it's `kernel/bpf/trampoline.c` lines 750-785. I'll read it closely to determine the relationship between `nr_progs` and the data structure sizes. It's imperative I verify there's no way `nr_skips` can exceed the allocated size of `im->skips`. That would lead to a classic heap buffer overflow, and it's something I need to prevent.



52/3 2026/09/27 09:14 tool read-file
0m Args:
{
  "File": "kernel/bpf/trampoline.c",
  "FirstLine": 750,
  "LineCount": 45
}

Results:
{
  "Contents": " 750:\t\tif (ip_arg)\n 751:\t\t\ttr-\u003eflags |= BPF_TRAMP_F_IP_ARG;\n 752:\t\n 753:\t#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\n 754:\tagain:\n 755:\t\tif (tr-\u003eflags \u0026 BPF_TRAMP_F_CALL_ORIG) {\n 756:\t\t\tif (tr-\u003eflags \u0026 BPF_TRAMP_F_SHARE_IPMODIFY) {\n 757:\t\t\t\t/* The BPF_TRAMP_F_SKIP_FRAME can be cleared in the\n 758:\t\t\t\t * first try, reset it in the second try.\n 759:\t\t\t\t */\n 760:\t\t\t\ttr-\u003eflags |= BPF_TRAMP_F_ORIG_STACK | BPF_TRAMP_F_SKIP_FRAME;\n 761:\t\t\t} else if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_JMP)) {\n 762:\t\t\t\t/* Use \"jmp\" instead of \"call\" for the trampoline\n 763:\t\t\t\t * in the origin call case, and we don't need to\n 764:\t\t\t\t * skip the frame.\n 765:\t\t\t\t */\n 766:\t\t\t\ttr-\u003eflags \u0026= ~BPF_TRAMP_F_SKIP_FRAME;\n 767:\t\t\t}\n 768:\t\t}\n 769:\t#endif\n 770:\t\n 771:\t\tsize = arch_bpf_trampoline_size(\u0026tr-\u003efunc.model, tr-\u003eflags,\n 772:\t\t\t\t\t\ttnodes, tr-\u003efunc.addr);\n 773:\t\tif (size \u003c 0) {\n 774:\t\t\terr = size;\n 775:\t\t\tgoto out;\n 776:\t\t}\n 777:\t\n 778:\t\tif (size \u003e PAGE_SIZE) {\n 779:\t\t\terr = -E2BIG;\n 780:\t\t\tgoto out;\n 781:\t\t}\n 782:\t\n 783:\t\tim = bpf_tramp_image_alloc(tr-\u003ekey, size, total);\n 784:\t\tif (IS_ERR(im)) {\n 785:\t\t\terr = PTR_ERR(im);\n 786:\t\t\tgoto out;\n 787:\t\t}\n 788:\t\tim-\u003ecall_orig = tr-\u003eflags \u0026 BPF_TRAMP_F_CALL_ORIG;\n 789:\t\n 790:\t\terr = arch_prepare_bpf_trampoline(im, im-\u003eimage, im-\u003eimage + size,\n 791:\t\t\t\t\t\t  \u0026tr-\u003efunc.model, tr-\u003eflags, tnodes,\n 792:\t\t\t\t\t\t  tr-\u003efunc.addr);\n 793:\t\tif (err \u003c 0)\n 794:\t\t\tgoto out_free;\n"
}

53/3 2026/09/27 09:14 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 28247
							output: 23
							thoughts: 66
						

Thoughts:
Okay, let's see, my mind is racing here, running through potential failure scenarios.

**Analyzing Potential KASAN Issues and Trampoline Function Calls**

My first thought is, if `nr_skips`, for whatever reason, ends up holding a value that's outside of expected bounds, that's a classic KASAN situation – a certain recipe for a nasty, memory-access-out-of-bounds error. That's a critical point to consider. We need to be absolutely sure that value is always within the expected range.

Then I'm thinking about the implications for `struct_ops` and those trampolines. Specifically, I need to check if calls to `struct_ops` might inadvertently trigger the `arch_prepare_bpf_trampoline` function. That's another potential entry point for this error that I need to be diligent about testing. We need to make sure that these calls don't introduce any vulnerabilities.



54/3 2026/09/27 09:14 tool grepper
0m Args:
{
  "Expression": "arch_prepare_bpf_trampoline"
}

Results:
{
  "Output": "arch/arm64/net/bpf_jit_comp.c=2688=static void store_func_meta(struct jit_ctx *ctx, u64 func_meta, int func_meta_off)\n--\narch/arm64/net/bpf_jit_comp.c-2693-\narch/arm64/net/bpf_jit_comp.c:2694:/* Based on the x86's implementation of arch_prepare_bpf_trampoline().\narch/arm64/net/bpf_jit_comp.c-2695- *\n--\narch/arm64/net/bpf_jit_comp.c=3025=int arch_protect_bpf_trampoline(void *image, unsigned int size)\n--\narch/arm64/net/bpf_jit_comp.c-3029-\narch/arm64/net/bpf_jit_comp.c:3030:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image,\narch/arm64/net/bpf_jit_comp.c-3031-\t\t\t\tvoid *ro_image_end, const struct btf_func_model *m,\n--\narch/loongarch/net/bpf_jit.c=1814=static void sign_extend(struct jit_ctx *ctx, int rd, int rj, u8 size, bool sign)\n--\narch/loongarch/net/bpf_jit.c-1841-\narch/loongarch/net/bpf_jit.c:1842:static int __arch_prepare_bpf_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,\narch/loongarch/net/bpf_jit.c-1843-\t\t\t\t\t const struct btf_func_model *m, struct bpf_tramp_nodes *tnodes,\n--\narch/loongarch/net/bpf_jit.c-2143-\narch/loongarch/net/bpf_jit.c:2144:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image,\narch/loongarch/net/bpf_jit.c-2145-\t\t\t\tvoid *ro_image_end, const struct btf_func_model *m,\n--\narch/loongarch/net/bpf_jit.c-2161-\tjit_fill_hole(image, (unsigned int)(ro_image_end - ro_image));\narch/loongarch/net/bpf_jit.c:2162:\tret = __arch_prepare_bpf_trampoline(\u0026ctx, im, m, tnodes, func_addr, flags);\narch/loongarch/net/bpf_jit.c-2163-\tif (ret \u003c 0)\n--\narch/loongarch/net/bpf_jit.c=2182=int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\narch/loongarch/net/bpf_jit.c-2188-\narch/loongarch/net/bpf_jit.c:2189:\tret = __arch_prepare_bpf_trampoline(\u0026ctx, \u0026im, m, tnodes, func_addr, flags);\narch/loongarch/net/bpf_jit.c-2190-\n--\narch/parisc/net/bpf_jit_comp64.c-10- * - check if bpf_jit_needs_zext() is needed (currently enabled)\narch/parisc/net/bpf_jit_comp64.c:11: * - implement arch_prepare_bpf_trampoline(), poke(), ...\narch/parisc/net/bpf_jit_comp64.c-12- */\n--\narch/powerpc/net/bpf_jit_comp.c=694=static int invoke_bpf_mod_ret(u32 *image, u32 *ro_image, struct codegen_context *ctx,\n--\narch/powerpc/net/bpf_jit_comp.c-731-/*\narch/powerpc/net/bpf_jit_comp.c:732: * Refer __arch_prepare_bpf_trampoline() for stack component details.\narch/powerpc/net/bpf_jit_comp.c-733- *\n--\narch/powerpc/net/bpf_jit_comp.c=805=static void bpf_trampoline_restore_args_stack(u32 *image, struct codegen_context *ctx,\n--\narch/powerpc/net/bpf_jit_comp.c-819-\narch/powerpc/net/bpf_jit_comp.c:820:static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_image,\narch/powerpc/net/bpf_jit_comp.c-821-\t\t\t\t\t void *rw_image_end, void *ro_image,\n--\narch/powerpc/net/bpf_jit_comp.c=1193=int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\narch/powerpc/net/bpf_jit_comp.c-1198-\narch/powerpc/net/bpf_jit_comp.c:1199:\tret = __arch_prepare_bpf_trampoline(\u0026im, NULL, NULL, NULL, m, flags, tnodes, func_addr);\narch/powerpc/net/bpf_jit_comp.c-1200-\treturn ret;\n--\narch/powerpc/net/bpf_jit_comp.c-1202-\narch/powerpc/net/bpf_jit_comp.c:1203:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,\narch/powerpc/net/bpf_jit_comp.c-1204-\t\t\t\tconst struct btf_func_model *m, u32 flags,\n--\narch/powerpc/net/bpf_jit_comp.c-1219-\narch/powerpc/net/bpf_jit_comp.c:1220:\tret = __arch_prepare_bpf_trampoline(im, rw_image, rw_image + size, image, m,\narch/powerpc/net/bpf_jit_comp.c-1221-\t\t\t\t\t    flags, tnodes, func_addr);\n--\narch/riscv/net/bpf_jit_comp64.c=978=static int invoke_bpf(struct bpf_tramp_image *im, struct bpf_tramp_nodes *tn,\n--\narch/riscv/net/bpf_jit_comp64.c-1001-\narch/riscv/net/bpf_jit_comp64.c:1002:static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,\narch/riscv/net/bpf_jit_comp64.c-1003-\t\t\t\t\t const struct btf_func_model *m,\n--\narch/riscv/net/bpf_jit_comp64.c=1304=int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\narch/riscv/net/bpf_jit_comp64.c-1313-\tctx.ro_insns = NULL;\narch/riscv/net/bpf_jit_comp64.c:1314:\tret = __arch_prepare_bpf_trampoline(\u0026im, m, tnodes, func_addr, flags, \u0026ctx);\narch/riscv/net/bpf_jit_comp64.c-1315-\n--\narch/riscv/net/bpf_jit_comp64.c=1324=void arch_free_bpf_trampoline(void *image, unsigned int size)\n--\narch/riscv/net/bpf_jit_comp64.c-1328-\narch/riscv/net/bpf_jit_comp64.c:1329:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image,\narch/riscv/net/bpf_jit_comp64.c-1330-\t\t\t\tvoid *ro_image_end, const struct btf_func_model *m,\n--\narch/riscv/net/bpf_jit_comp64.c-1345-\tctx.ro_insns = ro_image;\narch/riscv/net/bpf_jit_comp64.c:1346:\tret = __arch_prepare_bpf_trampoline(im, m, tnodes, func_addr, flags, \u0026ctx);\narch/riscv/net/bpf_jit_comp64.c-1347-\tif (ret \u003c 0)\n--\narch/s390/net/bpf_jit_comp.c=2700=static int alloc_stack(struct bpf_tramp_jit *tjit, size_t size)\n--\narch/s390/net/bpf_jit_comp.c-2717-\narch/s390/net/bpf_jit_comp.c:2718:static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,\narch/s390/net/bpf_jit_comp.c-2719-\t\t\t\t\t struct bpf_tramp_jit *tjit,\n--\narch/s390/net/bpf_jit_comp.c=3030=int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\narch/s390/net/bpf_jit_comp.c-3038-\narch/s390/net/bpf_jit_comp.c:3039:\tret = __arch_prepare_bpf_trampoline(\u0026im, \u0026tjit, m, flags,\narch/s390/net/bpf_jit_comp.c-3040-\t\t\t\t\t    tnodes, orig_call);\n--\narch/s390/net/bpf_jit_comp.c-3044-\narch/s390/net/bpf_jit_comp.c:3045:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image,\narch/s390/net/bpf_jit_comp.c-3046-\t\t\t\tvoid *image_end, const struct btf_func_model *m,\n--\narch/s390/net/bpf_jit_comp.c-3054-\tmemset(\u0026tjit, 0, sizeof(tjit));\narch/s390/net/bpf_jit_comp.c:3055:\tret = __arch_prepare_bpf_trampoline(im, \u0026tjit, m, flags,\narch/s390/net/bpf_jit_comp.c-3056-\t\t\t\t\t    tnodes, func_addr);\n--\narch/s390/net/bpf_jit_comp.c-3068-\ttjit.common.prg_buf = image;\narch/s390/net/bpf_jit_comp.c:3069:\tret = __arch_prepare_bpf_trampoline(im, \u0026tjit, m, flags,\narch/s390/net/bpf_jit_comp.c-3070-\t\t\t\t\t    tnodes, func_addr);\n--\narch/x86/net/bpf_jit_comp.c=3219=static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,\n--\narch/x86/net/bpf_jit_comp.c-3240-\t * bpf_tramp_run_ctx is already preserved by\narch/x86/net/bpf_jit_comp.c:3241:\t * arch_prepare_bpf_trampoline().\narch/x86/net/bpf_jit_comp.c-3242-\t *\n--\narch/x86/net/bpf_jit_comp.c=3365=static int invoke_bpf_mod_ret(const struct btf_func_model *m, u8 **pprog,\n--\narch/x86/net/bpf_jit_comp.c-3467- */\narch/x86/net/bpf_jit_comp.c:3468:static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_image,\narch/x86/net/bpf_jit_comp.c-3469-\t\t\t\t\t void *rw_image_end, void *image,\n--\narch/x86/net/bpf_jit_comp.c=3783=int arch_protect_bpf_trampoline(void *image, unsigned int size)\n--\narch/x86/net/bpf_jit_comp.c-3787-\narch/x86/net/bpf_jit_comp.c:3788:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,\narch/x86/net/bpf_jit_comp.c-3789-\t\t\t\tconst struct btf_func_model *m, u32 flags,\n--\narch/x86/net/bpf_jit_comp.c-3803-\narch/x86/net/bpf_jit_comp.c:3804:\tret = __arch_prepare_bpf_trampoline(im, rw_image, rw_image + size, image, m,\narch/x86/net/bpf_jit_comp.c-3805-\t\t\t\t\t    flags, tnodes, func_addr);\n--\narch/x86/net/bpf_jit_comp.c=3817=int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\narch/x86/net/bpf_jit_comp.c-3823-\narch/x86/net/bpf_jit_comp.c:3824:\t/* Allocate a temporary buffer for __arch_prepare_bpf_trampoline().\narch/x86/net/bpf_jit_comp.c-3825-\t *\n--\narch/x86/net/bpf_jit_comp.c-3834-\narch/x86/net/bpf_jit_comp.c:3835:\tret = __arch_prepare_bpf_trampoline(\u0026im, image, image + PAGE_SIZE, image,\narch/x86/net/bpf_jit_comp.c-3836-\t\t\t\t\t    m, flags, tnodes, func_addr);\n--\ninclude/linux/bpf.h=1316=struct bpf_tramp_image;\ninclude/linux/bpf.h:1317:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,\ninclude/linux/bpf.h-1318-\t\t\t\tconst struct btf_func_model *m, u32 flags,\n--\nkernel/bpf/bpf_struct_ops.c=637=int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_nodes *tnodes,\n--\nkernel/bpf/bpf_struct_ops.c-668-\nkernel/bpf/bpf_struct_ops.c:669:\tsize = arch_prepare_bpf_trampoline(NULL, image + image_off,\nkernel/bpf/bpf_struct_ops.c-670-\t\t\t\t\t   image + image_off + size,\n--\nkernel/bpf/trampoline.c=719=static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex,\n--\nkernel/bpf/trampoline.c-789-\nkernel/bpf/trampoline.c:790:\terr = arch_prepare_bpf_trampoline(im, im-\u003eimage, im-\u003eimage + size,\nkernel/bpf/trampoline.c-791-\t\t\t\t\t  \u0026tr-\u003efunc.model, tr-\u003eflags, tnodes,\n--\nkernel/bpf/trampoline.c=1519=int __weak\nkernel/bpf/trampoline.c:1520:arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,\nkernel/bpf/trampoline.c-1521-\t\t\t    const struct btf_func_model *m, u32 flags,\n"
}

55/3 2026/09/27 09:14 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 32822
							output: 357
							thoughts: 1950
						

Thoughts:
**KMSAN Needs Analysis: Patch Review for BPF Trampoline Race Condition**

Okay, so looking at this patch, my initial thought is that the primary issue being addressed is a classic use-after-free scenario within the BPF trampoline mechanism. Specifically, it seems a program can be detached and freed while another task is executing code within that same trampoline image. This creates a race, where a preempted or sleeping task resumes execution and potentially jumps to freed program code. This screams *memory corruption* and, thus, *KASAN* territory.

Digging deeper, I see `bpf_struct_ops_prepare_trampoline` is a key function, and `arch_prepare_bpf_trampoline` is where the trampoline image structure (`im`) gets involved. The code immediately returns early when `im` or `im->skips` is NULL. This structure, `bpf_tramp_image`, seems crucial because it holds information related to the trampoline's skip table. Looking at `BPF_MAX_TRAMP_LINKS`, it looks like this value gets adjusted to account for the image size to make sure it fits within `PAGE_SIZE`. The value is platform-dependent, which makes sense considering the instruction size variations across architectures.

I'm now checking the patch's changes. The patch is addressing a race condition when a program is detached from a trampoline. The commit message highlights that the critical issue is that "It must not call into the freed progs." which makes my gut say, 'UAF bug.'

Now let's clearly assess the KMSAN situation. The rule of thumb, and my established procedure, is that if KASAN, LOCKDEP, or standard bug detectors would catch the issue, KMSAN isn't needed. KMSAN is for detecting uninitialized memory usage, especially if the data is going to user space or influencing conditional logic.

Let's do a complete assessment:

1.  `struct bpf_tramp_image im = {};` in `arch_bpf_trampoline_size()`: Zero-initialized on the stack. Good.
2.  `im = kzalloc_obj(*im);` in `bpf_tramp_image_alloc()`: Zero-allocated heap structure. Excellent.
3.  `im->skips = kzalloc_objs(*im->skips, nr_progs);` in `bpf_tramp_image_alloc()`: Zero-allocated. All good here.
4.  In `arch/s390/net/bpf_jit_comp.c`, `struct bpf_tramp_jit tjit; memset(&tjit, 0, sizeof(tjit));` and `tjit->nr_progs = 0;`: Zero-initialized again. Perfect.
5.  In `arch/loongarch/net/bpf_jit.c`: `struct jit_ctx ctx = {}; struct bpf_tramp_image im = {};`: All zero-initialized.
6. `bpf_trampoline_skip_prog(tr, prog)` iterates over `tr->images` and checks `im->skips[i]`.
7. `struct bpf_tramp_skip` is internal to the trampoline, holding kernel pointers. Never copied to user space.

There's no copying data to user space via things like `copy_to_user`, no potential for info leaks. The potential bugs are use-after-free, which KASAN nails. Any out-of-bounds indices here KASAN will catch. Synchronization, like the `call_rcu_tasks` and `refcount_inc`, are concurrency issues. KASAN/LOCKDEP handle those issues.

The bug is a clear use-after-free, which KASAN, not KMSAN, is designed to catch!

**Therefore, NeedsKMSAN = false. Absolutely.** This patch addresses a memory corruption bug (UAF) that's already caught by KASAN. We are looking at memory access violations and race conditions, not information leaks or use of uninitialized data.

Time to call `set-results`.



56/3 2026/09/27 09:14 tool set-results
0m Args:
{
  "KMSANReasoning": "The patch series modifies BPF trampoline generation and lifecycle handling across multiple architectures (x86, arm64, loongarch, powerpc, riscv, s390) and core BPF trampoline code to prevent a use-after-free (UAF) race condition when BPF programs are detached and freed while another task is preempted or sleeping inside a trampoline image. It patches NOP instructions to jump over detached programs and refines RCU synchronization (RCU tasks trace / tasks RCU).\n\nKMSAN vs. KASAN applicability:\n- The bug being addressed is a lifetime / use-after-free race condition, which is directly tracked and detected by KASAN, not KMSAN.\n- All newly introduced or modified kernel data structures (such as `im-\u003eskips` allocated via `kzalloc_objs` and stack instances `im` initialized with `= {}`) are fully zero-initialized before access.\n- There are no kernel-to-user memory transfers (no `copy_to_user`, `put_user`, socket options, or netlink attributes) introduced or altered that could result in information disclosure of uninitialized padding or stack/heap bytes.\n- Any potential memory safety defects introduced by this patch (e.g., out-of-bounds access in the `skips` array, list corruption, or use-after-free during image teardown) are detectable by KASAN and standard kernel debuggers.\n\nTherefore, this patch does not introduce or expose uninitialized memory risks, and a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false
}

Results:
{
  "KMSANReasoning": "The patch series modifies BPF trampoline generation and lifecycle handling across multiple architectures (x86, arm64, loongarch, powerpc, riscv, s390) and core BPF trampoline code to prevent a use-after-free (UAF) race condition when BPF programs are detached and freed while another task is preempted or sleeping inside a trampoline image. It patches NOP instructions to jump over detached programs and refines RCU synchronization (RCU tasks trace / tasks RCU).\n\nKMSAN vs. KASAN applicability:\n- The bug being addressed is a lifetime / use-after-free race condition, which is directly tracked and detected by KASAN, not KMSAN.\n- All newly introduced or modified kernel data structures (such as `im-\u003eskips` allocated via `kzalloc_objs` and stack instances `im` initialized with `= {}`) are fully zero-initialized before access.\n- There are no kernel-to-user memory transfers (no `copy_to_user`, `put_user`, socket options, or netlink attributes) introduced or altered that could result in information disclosure of uninitialized padding or stack/heap bytes.\n- Any potential memory safety defects introduced by this patch (e.g., out-of-bounds access in the `skips` array, list corruption, or use-after-free during image teardown) are detectable by KASAN and standard kernel debuggers.\n\nTherefore, this patch does not introduce or expose uninitialized memory risks, and 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)