| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/09/12 10:07 | flow | patch-triage |
2mArgs:null Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT",
"DYNAMIC_FTRACE_WITH_DIRECT_CALLS"
],
"FocusSymbols": [
"bpf_trampoline_link_prog",
"bpf_trampoline_unlink_prog",
"bpf_trampoline_update",
"bpf_trampoline_skip_prog",
"arch_prepare_bpf_trampoline",
"arch_bpf_trampoline_size",
"bpf_tramp_image_alloc",
"bpf_tramp_image_free",
"__bpf_tramp_image_put_deferred"
],
"KMSANReasoning": "The patch series addresses a use-after-free (UAF) race condition in BPF trampolines where a task executing in a multi-prog trampoline could sleep (e.g. via a sleepable BPF program) while a subsequent attached program is detached and freed. When the sleeping task resumes, it would invoke the now-freed program. The fix emits NOPs before each invoked program call in the JIT trampoline images and patches them to jump instructions skipping the detached program.\n\nFrom a memory safety perspective:\n1. All newly introduced heap allocations (struct bpf_tramp_skip array via kzalloc_objs, struct bpf_tramp_image via kzalloc_obj) and stack allocations (struct bpf_tramp_image im = {} in arch_bpf_trampoline_size) are explicitly zero-initialized.\n2. No uninitialized memory is read, branched on, or exposed across user/kernel boundaries.\n3. The bug class targeted and any potential regressions introduced by this patch (use-after-free, refcounting issues on trampoline images, list corruption, or out-of-bounds accesses during text patching) fall squarely under the domain of KASAN, LOCKDEP, and standard kernel sanitizers, not KMSAN.\n\nTherefore, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false,
"Reasoning": "The patch addresses a use-after-free race condition in BPF trampolines where a task sleeping in an earlier BPF prog (e.g., sleepable fentry/fexit) could wake up and execute into an adjacent program that was detached and freed. It introduces per-program NOPs in generated trampolines (including x86 JIT), dynamically patches them to branch over detached programs via text poking in bpf_trampoline_skip_prog(), and manages active trampoline images in a linked list with updated refcounting and deferred cleanup. The modified code is in core BPF and x86 JIT, reachable on amd64 via the bpf() syscall (e.g., BPF_LINK_CREATE / detach of tracing links), making it worth fuzzing.",
"WorthFuzzing": true
} |
| 1/1 | 2026/09/12 10:07 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 486b508ac7e65d71c58ea5e0110860c000698245\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Sat Sep 12 10:07:25 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 c18e005a41dbe..9c166bdfbc6a6 100644\n--- a/arch/arm64/net/bpf_jit_comp.c\n+++ b/arch/arm64/net/bpf_jit_comp.c\n@@ -2416,10 +2416,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@@ -2429,6 +2430,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@@ -2481,11 +2486,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@@ -2494,7 +2501,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@@ -2880,7 +2887,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@@ -2891,7 +2898,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@@ -2928,7 +2935,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, 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@@ -2992,7 +2999,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..0e6f1ad36c2d5 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@@ -2068,7 +2079,7 @@ 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@@ -2178,7 +2189,7 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n {\n \tint ret;\n \tstruct jit_ctx ctx;\n-\tstruct bpf_tramp_image im;\n+\tstruct bpf_tramp_image im = {};\n \n \tctx.image = NULL;\n \tctx.idx = 0;\ndiff --git a/arch/powerpc/net/bpf_jit_comp.c b/arch/powerpc/net/bpf_jit_comp.c\nindex 7b07b43575f11..95ea27148c918 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@@ -1123,8 +1131,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, 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@@ -1192,7 +1200,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 +1328,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..21f8ce2ec89ae 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@@ -1230,7 +1240,7 @@ 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@@ -1299,7 +1309,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..286a7c9215cb9 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@@ -2962,7 +2987,7 @@ 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@@ -3016,7 +3041,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..0d974da4f5093 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@@ -3708,7 +3717,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@@ -3811,7 +3820,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 e57af902560c3..9dcd3bc395eb0 100644\n--- a/include/linux/bpf.h\n+++ b/include/linux/bpf.h\n@@ -1258,11 +1258,15 @@ 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(__aarch64__)\n+\tBPF_MAX_TRAMP_LINKS = 37,\n #else\n \tBPF_MAX_TRAMP_LINKS = 38,\n #endif\n@@ -1363,6 +1367,17 @@ 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@@ -1374,8 +1389,27 @@ struct bpf_tramp_image {\n \t\tstruct rcu_head rcu;\n \t\tstruct work_struct work;\n \t};\n+\t/* entry in tr-\u003eimages, the image holds a reference on tr */\n+\tstruct bpf_trampoline *tr;\n+\tstruct list_head list;\n+\tstruct bpf_tramp_skip *skips;\n+\tint nr_skips;\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 +1436,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;\ndiff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c\nindex 90b70ea0d3709..b82d8592319a4 100644\n--- a/kernel/bpf/trampoline.c\n+++ b/kernel/bpf/trampoline.c\n@@ -403,6 +403,7 @@ static struct bpf_trampoline *bpf_trampoline_lookup(u64 key, unsigned long ip)\n \trefcount_set(\u0026tr-\u003erefcnt, 1);\n \tfor (i = 0; i \u003c BPF_TRAMP_MAX; i++)\n \t\tINIT_HLIST_HEAD(\u0026tr-\u003eprogs_hlist[i]);\n+\tINIT_LIST_HEAD(\u0026tr-\u003eimages);\n out:\n \tmutex_unlock(\u0026trampoline_mutex);\n \treturn tr;\n@@ -565,14 +566,21 @@ 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_trampoline *tr;\n \tstruct bpf_tramp_image *im;\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_trampoline_put(tr);\n \tbpf_tramp_image_free(im);\n }\n \n@@ -658,7 +666,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 +677,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,11 +707,37 @@ 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 }\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. Patch these images 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 void bpf_trampoline_set_flags(struct bpf_trampoline *tr, u32 flags)\n {\n \ttrampoline_lock(tr);\n@@ -771,7 +809,7 @@ 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@@ -806,8 +844,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@@ -937,6 +981,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,\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..9a0e1fc44c74c\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/prog_tests/tramp_prog_detach.c\n@@ -0,0 +1,191 @@\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 \u003csys/syscall.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 a prog while a task sleeps in the prog that runs before it\n+ * in the same trampoline image, then let that task continue through the\n+ * image. It must not call into the freed prog.\n+ *\n+ * The task is held in a sleepable fentry prog with userfaultfd, like\n+ * bpf_mod_race does.\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 = 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+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+static void test_detach(bool fexit)\n+{\n+\tstruct tramp_prog_detach *sleepable = NULL, *victim = NULL;\n+\tstruct pollfd pfd = { .events = POLLIN };\n+\tstruct uffdio_copy uffd_copy = {};\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 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+\tvictim = load_one(fexit, false);\n+\tif (!victim)\n+\t\tgoto out;\n+\tsleepable = load_one(fexit, true);\n+\tif (!sleepable)\n+\t\tgoto out;\n+\tsleepable_prog = pick_prog(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+\tif (!ASSERT_LT(sleepable-\u003ebss-\u003ets, victim-\u003ebss-\u003ets, \"prog order\"))\n+\t\tgoto out;\n+\n+\tuffd = test_setup_uffd(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/* Detach and unload the victim prog, and make sure it is gone */\n+\ttramp_prog_detach__destroy(victim);\n+\tvictim = NULL;\n+\tkern_sync_rcu();\n+\tusleep(100 * 1000);\n+\tkern_sync_rcu();\n+\n+\t/*\n+\t * That was enough to test the use-after-free but do it once more, so\n+\t * that an older image with an already patched nop gets patched too.\n+\t */\n+\tvictim = load_one(fexit, false);\n+\ttramp_prog_detach__destroy(victim);\n+\tvictim = NULL;\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+\ttramp_prog_detach__destroy(victim);\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);\n+\t/* a task sleeping after it returned, past the jmp that detach installs */\n+\tif (test__start_subtest(\"fexit\"))\n+\t\ttest_detach(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+}\n"
} |
| 2/1 | 2026/09/12 10:07 | agent | patch-evaluator |
1mModel:core Args: null Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT",
"DYNAMIC_FTRACE_WITH_DIRECT_CALLS"
],
"FocusSymbols": [
"bpf_trampoline_link_prog",
"bpf_trampoline_unlink_prog",
"bpf_trampoline_update",
"bpf_trampoline_skip_prog",
"arch_prepare_bpf_trampoline",
"arch_bpf_trampoline_size",
"bpf_tramp_image_alloc",
"bpf_tramp_image_free",
"__bpf_tramp_image_put_deferred"
],
"Reasoning": "The patch addresses a use-after-free race condition in BPF trampolines where a task sleeping in an earlier BPF prog (e.g., sleepable fentry/fexit) could wake up and execute into an adjacent program that was detached and freed. It introduces per-program NOPs in generated trampolines (including x86 JIT), dynamically patches them to branch over detached programs via text poking in bpf_trampoline_skip_prog(), and manages active trampoline images in a linked list with updated refcounting and deferred cleanup. The modified code is in core BPF and x86 JIT, reachable on amd64 via the bpf() syscall (e.g., BPF_LINK_CREATE / detach of tracing links), making it worth fuzzing.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.
IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.
================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.
- REACHABILITY IS THE PRIMARY GATE:
Fuzzing can only discover bugs in code that can actually execute in standard virtualized
environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
regardless of whether it adds assertions or complex logic.
- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
Patch authors routinely label changes as "cleanups", "refactorings", or state
"No functional change intended". Do NOT take these claims at face value.
Code refactorings that rearrange logic, introduce helper functions, or alter state management
in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
If reachable executable code is modified or refactored, it MUST be fuzzed.
- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
Even if the author believes the invariant always holds, fuzzing is essential to verify whether
an unusual sequence of operations can violate it.
================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:
- Non-kernel and non-executable changes:
* Modifications to Documentation/, comments, or spelling fixes.
* User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
that do not affect the compiled kernel image (vmlinux) or kernel modules.
* Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
that do not alter control flow or data structures.
* Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
* Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
* Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
physical PCI hot-unplug or manual sysfs driver unbinding.
* Code paths exclusive to architectures other than the target architecture.
================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
* Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
* Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
* Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
* Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).
================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:
- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
Do NOT list generic, ubiquitous functions called by almost every program in the corpus
(including, but not limited to: general memory allocators and deallocators, page fault
and trap handlers, or core synchronization primitives; this is not an exhaustive list).
Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
which severely dilutes fuzzing effort away from the actual changes.
- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
List functions that specifically implement the logic being added or altered, or direct API entrypoints
for the subsystem feature under review.
- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).
================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 486b508ac7e65d71c58ea5e0110860c000698245
Author: syz-cluster <triage@syzkaller.com>
Date: Sat Sep 12 10:07:25 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 c18e005a41dbe..9c166bdfbc6a6 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -2416,10 +2416,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;
@@ -2429,6 +2430,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);
@@ -2481,11 +2486,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;
@@ -2494,7 +2501,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;
@@ -2880,7 +2887,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);
}
@@ -2891,7 +2898,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);
}
@@ -2928,7 +2935,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, fexit->nodes[i], bargs_off, retval_off,
+ invoke_bpf_prog(ctx, im, fexit->nodes[i], bargs_off, retval_off,
run_ctx_off, false);
}
@@ -2992,7 +2999,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..0e6f1ad36c2d5 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;
@@ -2068,7 +2079,7 @@ 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;
@@ -2178,7 +2189,7 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
{
int ret;
struct jit_ctx ctx;
- struct bpf_tramp_image im;
+ struct bpf_tramp_image im = {};
ctx.image = NULL;
ctx.idx = 0;
diff --git a/arch/powerpc/net/bpf_jit_comp.c b/arch/powerpc/net/bpf_jit_comp.c
index 7b07b43575f11..95ea27148c918 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;
@@ -1123,8 +1131,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, 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;
}
@@ -1192,7 +1200,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 +1328,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..21f8ce2ec89ae 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;
@@ -1230,7 +1240,7 @@ 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;
@@ -1299,7 +1309,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..286a7c9215cb9 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;
/*
@@ -2962,7 +2987,7 @@ 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) {
@@ -3016,7 +3041,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..0d974da4f5093 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;
@@ -3708,7 +3717,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;
@@ -3811,7 +3820,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 e57af902560c3..9dcd3bc395eb0 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1258,11 +1258,15 @@ 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(__aarch64__)
+ BPF_MAX_TRAMP_LINKS = 37,
#else
BPF_MAX_TRAMP_LINKS = 38,
#endif
@@ -1363,6 +1367,17 @@ 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;
@@ -1374,8 +1389,27 @@ struct bpf_tramp_image {
struct rcu_head rcu;
struct work_struct work;
};
+ /* entry in tr->images, the image holds a reference on tr */
+ struct bpf_trampoline *tr;
+ struct list_head list;
+ struct bpf_tramp_skip *skips;
+ int nr_skips;
};
+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 +1436,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;
diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
index 90b70ea0d3709..b82d8592319a4 100644
--- a/kernel/bpf/trampoline.c
+++ b/kernel/bpf/trampoline.c
@@ -403,6 +403,7 @@ static struct bpf_trampoline *bpf_trampoline_lookup(u64 key, unsigned long ip)
refcount_set(&tr->refcnt, 1);
for (i = 0; i < BPF_TRAMP_MAX; i++)
INIT_HLIST_HEAD(&tr->progs_hlist[i]);
+ INIT_LIST_HEAD(&tr->images);
out:
mutex_unlock(&trampoline_mutex);
return tr;
@@ -565,14 +566,21 @@ 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_trampoline *tr;
struct bpf_tramp_image *im;
im = container_of(work, struct bpf_tramp_image, work);
+ tr = im->tr;
+ trampoline_lock(tr);
+ list_del(&im->list);
+ trampoline_unlock(tr);
+ bpf_trampoline_put(tr);
bpf_tramp_image_free(im);
}
@@ -658,7 +666,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 +677,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,11 +707,37 @@ 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);
}
+/*
+ * 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. Patch these images 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;
+ }
+ }
+}
+
void bpf_trampoline_set_flags(struct bpf_trampoline *tr, u32 flags)
{
trampoline_lock(tr);
@@ -771,7 +809,7 @@ 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;
@@ -806,8 +844,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)
@@ -937,6 +981,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,
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..9a0e1fc44c74c
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/tramp_prog_detach.c
@@ -0,0 +1,191 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <pthread.h>
+#include <poll.h>
+#include <sys/mman.h>
+#include <sys/syscall.h>
+#include <linux/userfaultfd.h>
+#include "tramp_prog_detach.skel.h"
+#include "testing_helpers.h"
+
+/*
+ * Detach and free a prog while a task sleeps in the prog that runs before it
+ * in the same trampoline image, then let that task continue through the
+ * image. It must not call into the freed prog.
+ *
+ * The task is held in a sleepable fentry prog with userfaultfd, like
+ * bpf_mod_race does.
+ */
+
+static int test_setup_uffd(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;
+}
+
+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;
+}
+
+static void test_detach(bool fexit)
+{
+ struct tramp_prog_detach *sleepable = NULL, *victim = NULL;
+ struct pollfd pfd = { .events = POLLIN };
+ struct uffdio_copy uffd_copy = {};
+ 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 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 */
+ victim = load_one(fexit, false);
+ if (!victim)
+ goto out;
+ sleepable = load_one(fexit, true);
+ if (!sleepable)
+ goto out;
+ sleepable_prog = pick_prog(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;
+ if (!ASSERT_LT(sleepable->bss->ts, victim->bss->ts, "prog order"))
+ goto out;
+
+ uffd = test_setup_uffd(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 prog, and make sure it is gone */
+ tramp_prog_detach__destroy(victim);
+ victim = NULL;
+ kern_sync_rcu();
+ usleep(100 * 1000);
+ kern_sync_rcu();
+
+ /*
+ * That was enough to test the use-after-free but do it once more, so
+ * that an older image with an already patched nop gets patched too.
+ */
+ victim = load_one(fexit, false);
+ tramp_prog_detach__destroy(victim);
+ victim = NULL;
+
+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");
+
+ tramp_prog_detach__destroy(victim);
+ 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);
+ /* a task sleeping after it returned, past the jmp that detach installs */
+ if (test__start_subtest("fexit"))
+ test_detach(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();
+}
|
| 3/2 | 2026/09/12 10:07 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 18883 output: 31 thoughts: 970 Thoughts: **BPF Trampoline Modification Analysis: Worthy of Fuzzing** Okay, let's dive into this patch. The target architecture is amd64, which immediately grabs my attention. This patch touches several architecture-specific BPF JIT compilation files – specifically arm64, loongarch, powerpc, riscv, s390, and x86 – along with core BPF trampoline handling and related testing. The modifications in x86 are what I'm focused on, since we're analyzing for amd64 fuzzing relevance: - `arch/x86/net/bpf_jit_comp.c` has these changes: `invoke_bpf_prog` now emits NOPs and registers skip info. - `kernel/bpf/trampoline.c` shows changes to struct initialization, list management of trampoline images, image allocation/deallocation, skipping program iterations (that now pokes the nop to jump to target), update, and removal. There's a `WARN_ON_ONCE` here. - `include/linux/bpf.h` is also modified, which I will want to verify later. From a core systems perspective, this is definitely relevant. BPF is a core subsystem and these changes directly impact x86 trampoline generation, text patching (essentially replacing NOPs with jump instructions), and the management of trampoline images. This includes the lifetime of `bpf_trampoline` and `bpf_tramp_image`, with refcounting and locking primitives. The `WARN_ON_ONCE` in `bpf_trampoline_skip_prog` is also a red flag to investigate. Now, the crucial question: is this reachable on amd64 in a standard virtualized environment like QEMU? Absolutely! BPF trampolines (particularly for fentry, fexit, and fmod\_ret) are heavily used and are easily fuzzable via syzkaller through BPF syscalls like `BPF_PROG_LOAD`, `BPF_LINK_CREATE`, and `BPF_PROG_ATTACH`. The core changes to text poking (modifying the NOP with jump instructions) seem like good targets for fuzzing. Therefore, this patch warrants being flagged as `WorthFuzzing = true`. I'll next turn to grepper to examine Kconfig options and relevant symbols. |
| 4/2 | 2026/09/12 10:07 | tool | grepper |
0mError:no matches Args: {
"Expression": "BPF_TRAMPOLINE",
"PathPrefix": "kernel/bpf"
}Results: {
"error": "no matches"
} |
| 5/2 | 2026/09/12 10:07 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 20406 output: 28 thoughts: 28 Thoughts: Here's my take on the thought process, as if it were my own: **Investigating BPF Implementation Details** Okay, so I need to dig into the BPF implementation a bit more. Given what I'm looking at, my initial hunch is that the relevant code will reside in either the `kernel/bpf/Makefile` or the `kernel/bpf/trampoline.c` file. Both seem like good candidates for core BPF functionality, the Makefile obviously for build details and dependencies, and trampoline.c is a likely place for the actual code execution and hooking. Let's fire up `grepper` and see what we can find. I'll start with those two files and see if that narrows down where I need to focus. I'm hoping I can find what I need quickly, so I can start working on my goal. |
| 6/2 | 2026/09/12 10:07 | tool | grepper |
0mArgs:{
"Expression": "trampoline.o",
"PathPrefix": "kernel/bpf"
}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--\nkernel/bpf/trampoline.c=57=static void trampoline_unlock(struct bpf_trampoline *tr)\n--\nkernel/bpf/trampoline.c-61-\nkernel/bpf/trampoline.c:62:struct bpf_trampoline_ops {\nkernel/bpf/trampoline.c-63-\tint (*register_fentry)(struct bpf_trampoline *tr, struct bpf_tramp_image *im, void *data);\n--\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);\nkernel/bpf/trampoline.c:72:static const struct bpf_trampoline_ops trampoline_ops;\nkernel/bpf/trampoline.c-73-\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-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);\nkernel/bpf/trampoline.c-159-\t\tbreak;\n--\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);\nkernel/bpf/trampoline.c-166-\t\tbreak;\n--\nkernel/bpf/trampoline.c=474=static int register_fentry(struct bpf_trampoline *tr, struct bpf_tramp_image *im,\n--\nkernel/bpf/trampoline.c-501-\nkernel/bpf/trampoline.c:502:static const struct bpf_trampoline_ops trampoline_ops = {\nkernel/bpf/trampoline.c-503-\t.register_fentry = register_fentry,\n--\nkernel/bpf/trampoline.c=748=static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex,\nkernel/bpf/trampoline.c:749:\t\t\t\t const struct bpf_trampoline_ops *ops, void *data)\nkernel/bpf/trampoline.c-750-{\n--\nkernel/bpf/trampoline.c=987=static int __bpf_trampoline_link_prog(struct bpf_tramp_node *node,\n--\nkernel/bpf/trampoline.c-989-\t\t\t\t struct bpf_prog *tgt_prog,\nkernel/bpf/trampoline.c:990:\t\t\t\t const struct bpf_trampoline_ops *ops,\nkernel/bpf/trampoline.c-991-\t\t\t\t void *data)\n--\nkernel/bpf/trampoline.c=1035=int bpf_trampoline_link_prog(struct bpf_tramp_node *node,\n--\nkernel/bpf/trampoline.c-1041-\ttrampoline_lock(tr);\nkernel/bpf/trampoline.c:1042:\terr = __bpf_trampoline_link_prog(node, tr, tgt_prog, \u0026trampoline_ops, NULL);\nkernel/bpf/trampoline.c-1043-\ttrampoline_unlock(tr);\n--\nkernel/bpf/trampoline.c=1047=static int __bpf_trampoline_unlink_prog(struct bpf_tramp_node *node,\n--\nkernel/bpf/trampoline.c-1049-\t\t\t\t\tstruct bpf_prog *tgt_prog,\nkernel/bpf/trampoline.c:1050:\t\t\t\t\tconst struct bpf_trampoline_ops *ops,\nkernel/bpf/trampoline.c-1051-\t\t\t\t\tvoid *data)\n--\nkernel/bpf/trampoline.c=1072=int bpf_trampoline_unlink_prog(struct bpf_tramp_node *node,\n--\nkernel/bpf/trampoline.c-1078-\ttrampoline_lock(tr);\nkernel/bpf/trampoline.c:1079:\terr = __bpf_trampoline_unlink_prog(node, tr, tgt_prog, \u0026trampoline_ops, NULL);\nkernel/bpf/trampoline.c-1080-\ttrampoline_unlock(tr);\n--\nkernel/bpf/trampoline.c=1168=int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,\n--\nkernel/bpf/trampoline.c-1210-\nkernel/bpf/trampoline.c:1211:\terr = __bpf_trampoline_link_prog(\u0026shim_link-\u003elink.node, tr, NULL, \u0026trampoline_ops, NULL);\nkernel/bpf/trampoline.c-1212-\tif (err)\n--\nkernel/bpf/trampoline.c=1651=static int modify_fentry_multi(struct bpf_trampoline *tr, u32 orig_flags, struct bpf_tramp_image *im,\n--\nkernel/bpf/trampoline.c-1665-\nkernel/bpf/trampoline.c:1666:static const struct bpf_trampoline_ops trampoline_multi_ops = {\nkernel/bpf/trampoline.c-1667-\t.register_fentry = register_fentry_multi,\n"
} |
| 7/2 | 2026/09/12 10:07 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 22177 output: 32 thoughts: 52 Thoughts: **Examining the BPF Trampoline Requirements** Okay, so `trampoline.o` is compiled with `CONFIG_BPF_JIT` enabled. That means it's likely relying on the Just-In-Time compiler for BPF. I need to understand the dependencies and specifics of this trampoline mechanism, especially with the JIT involved. My next step is clear: I need to dig into the kernel's configuration files, specifically `kernel/bpf/Kconfig`. I'll be searching for definitions related to "trampoline" and, importantly, "BPF_JIT" to understand the full context and any associated requirements or features that are toggled on when the JIT is in play. Hopefully, this will give me some clarity on how the trampoline is structured and what other components it touches. |
| 8/2 | 2026/09/12 10:07 | tool | grepper |
0mArgs:{
"Expression": "BPF_JIT",
"PathPrefix": "kernel/bpf/Kconfig"
}Results: {
"Output": "kernel/bpf/Kconfig=4=config BPF\n--\nkernel/bpf/Kconfig-12-# Classic BPF JIT (cBPF)\nkernel/bpf/Kconfig:13:config HAVE_CBPF_JIT\nkernel/bpf/Kconfig-14-\tbool\n--\nkernel/bpf/Kconfig-16-# Extended BPF JIT (eBPF)\nkernel/bpf/Kconfig:17:config HAVE_EBPF_JIT\nkernel/bpf/Kconfig-18-\tbool\n--\nkernel/bpf/Kconfig-21-# default for kernels that were compiled with BPF JIT support.\nkernel/bpf/Kconfig:22:config ARCH_WANT_DEFAULT_BPF_JIT\nkernel/bpf/Kconfig-23-\tbool\n--\nkernel/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\"\nkernel/bpf/Kconfig-44-\tdepends on BPF\nkernel/bpf/Kconfig:45:\tdepends on HAVE_CBPF_JIT || HAVE_EBPF_JIT\nkernel/bpf/Kconfig-46-\tselect EXECMEM\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\"\nkernel/bpf/Kconfig:60:\tdepends on BPF_SYSCALL \u0026\u0026 HAVE_EBPF_JIT \u0026\u0026 BPF_JIT\nkernel/bpf/Kconfig-61-\thelp\n--\nkernel/bpf/Kconfig-64-\nkernel/bpf/Kconfig:65:\t When CONFIG_BPF_JIT_ALWAYS_ON is enabled, /proc/sys/net/core/bpf_jit_enable\nkernel/bpf/Kconfig-66-\t is permanently set to 1 and setting any other value than that will\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\nkernel/bpf/Kconfig:71:\tdepends on HAVE_EBPF_JIT \u0026\u0026 BPF_JIT\nkernel/bpf/Kconfig-72-\n--\nkernel/bpf/Kconfig=92=config BPF_LSM\n--\nkernel/bpf/Kconfig-96-\tdepends on SECURITY\nkernel/bpf/Kconfig:97:\tdepends on BPF_JIT\nkernel/bpf/Kconfig-98-\thelp\n"
} |
| 9/2 | 2026/09/12 10:07 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 23170 output: 38 thoughts: 44 Thoughts: **Investigating BPF and Trampoline Dependencies** Okay, so I need to get a handle on the current state of things. My initial thought is to zero in on `BPF_SYSCALL` and `BPF_JIT`. These are critical for the kernel-level BPF work I'm focusing on, so understanding their current status is paramount. Beyond that, I'm specifically curious about the dependencies of the "trampoline" mechanism. Trampolines are often a critical part of dynamic code injection and instrumentation, and figuring out what other kernel features it relies upon is going to be important in the broader view of system optimization and security research I'm conducting. I'll start by checking the `arch/x86/Kconfig` file. I'll need to know what architecture-specific configurations might influence the trampoline's functionality. This is a good starting point to build a deeper understanding of the relevant modules. |
| 10/2 | 2026/09/12 10:07 | tool | grepper |
0mError:no matches Args: {
"Expression": "HAVE_BPF_TRAMPOLINE",
"PathPrefix": "arch/x86/Kconfig"
}Results: {
"error": "no matches"
} |
| 11/2 | 2026/09/12 10:07 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 23462 output: 24 thoughts: 20 Thoughts: Here's my take on that thought process: **Diving Deep into BPF Trampoline Support** Okay, so I need to get a handle on how widely `HAVE_BPF_TRAMPOLINE` is actually being used and how it's configured throughout the kernel's various subsystems. This isn't just a surface-level scan; I want a comprehensive view. I need to understand where we're relying on this feature, where it's potentially causing issues, and if there are any areas we can optimize. This is the starting point for a deeper dive. |
| 12/2 | 2026/09/12 10:07 | tool | grepper |
0mError:no matches Args: {
"Expression": "HAVE_BPF_TRAMPOLINE"
}Results: {
"error": "no matches"
} |
| 13/2 | 2026/09/12 10:07 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 23644 output: 23 thoughts: 26 Thoughts: Here's my take on those thoughts, tailored for an expert audience: **Tracing the BPF Trampoline** Okay, so I need to understand the execution path of this `trampoline` concept, specifically focusing on the `arch_prepare_bpf_trampoline` function. My immediate goal is to understand its definition and how it's being *used* within the larger BPF ecosystem. I want to see this as a control flow graph and how it is employed. This isn't just about a static definition, I want to see where it *lives* and how it *breathes*. I'm thinking about the low-level interactions and security implications of this code. Where is this trampoline used? Is it in the kernel directly or is there some intermediate layer? Are we talking about a standard trampoline, or something with a specific arch-level twist? This is likely architectural, so I want to understand what the architecture dependencies are and how this specific function handles them. Knowing where to look for potential vulnerabilities is critical. Right, let's start digging through the codebase. |
| 14/2 | 2026/09/12 10:07 | tool | grepper |
0mArgs:{
"Expression": "arch_prepare_bpf_trampoline"
}Results: {
"Output": "arch/arm64/net/bpf_jit_comp.c=2686=static void store_func_meta(struct jit_ctx *ctx, u64 func_meta, int func_meta_off)\n--\narch/arm64/net/bpf_jit_comp.c-2691-\narch/arm64/net/bpf_jit_comp.c:2692:/* Based on the x86's implementation of arch_prepare_bpf_trampoline().\narch/arm64/net/bpf_jit_comp.c-2693- *\n--\narch/arm64/net/bpf_jit_comp.c=3027=int arch_protect_bpf_trampoline(void *image, unsigned int size)\n--\narch/arm64/net/bpf_jit_comp.c-3031-\narch/arm64/net/bpf_jit_comp.c:3032:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image,\narch/arm64/net/bpf_jit_comp.c-3033-\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-2148-\narch/loongarch/net/bpf_jit.c:2149:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image,\narch/loongarch/net/bpf_jit.c-2150-\t\t\t\tvoid *ro_image_end, const struct btf_func_model *m,\n--\narch/loongarch/net/bpf_jit.c-2166-\tjit_fill_hole(image, (unsigned int)(ro_image_end - ro_image));\narch/loongarch/net/bpf_jit.c:2167:\tret = __arch_prepare_bpf_trampoline(\u0026ctx, im, m, tnodes, func_addr, flags);\narch/loongarch/net/bpf_jit.c-2168-\tif (ret \u003c 0)\n--\narch/loongarch/net/bpf_jit.c=2187=int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\narch/loongarch/net/bpf_jit.c-2196-\narch/loongarch/net/bpf_jit.c:2197:\tret = __arch_prepare_bpf_trampoline(\u0026ctx, \u0026im, m, tnodes, func_addr, flags);\narch/loongarch/net/bpf_jit.c-2198-\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=1200=int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\narch/powerpc/net/bpf_jit_comp.c-1205-\narch/powerpc/net/bpf_jit_comp.c:1206:\tret = __arch_prepare_bpf_trampoline(\u0026im, NULL, NULL, NULL, m, flags, tnodes, func_addr);\narch/powerpc/net/bpf_jit_comp.c-1207-\treturn ret;\n--\narch/powerpc/net/bpf_jit_comp.c-1209-\narch/powerpc/net/bpf_jit_comp.c:1210:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,\narch/powerpc/net/bpf_jit_comp.c-1211-\t\t\t\tconst struct btf_func_model *m, u32 flags,\n--\narch/powerpc/net/bpf_jit_comp.c-1226-\narch/powerpc/net/bpf_jit_comp.c:1227:\tret = __arch_prepare_bpf_trampoline(im, rw_image, rw_image + size, image, m,\narch/powerpc/net/bpf_jit_comp.c-1228-\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=1309=int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\narch/riscv/net/bpf_jit_comp64.c-1318-\tctx.ro_insns = NULL;\narch/riscv/net/bpf_jit_comp64.c:1319:\tret = __arch_prepare_bpf_trampoline(\u0026im, m, tnodes, func_addr, flags, \u0026ctx);\narch/riscv/net/bpf_jit_comp64.c-1320-\n--\narch/riscv/net/bpf_jit_comp64.c=1329=void arch_free_bpf_trampoline(void *image, unsigned int size)\n--\narch/riscv/net/bpf_jit_comp64.c-1333-\narch/riscv/net/bpf_jit_comp64.c:1334:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image,\narch/riscv/net/bpf_jit_comp64.c-1335-\t\t\t\tvoid *ro_image_end, const struct btf_func_model *m,\n--\narch/riscv/net/bpf_jit_comp64.c-1350-\tctx.ro_insns = ro_image;\narch/riscv/net/bpf_jit_comp64.c:1351:\tret = __arch_prepare_bpf_trampoline(im, m, tnodes, func_addr, flags, \u0026ctx);\narch/riscv/net/bpf_jit_comp64.c-1352-\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=3041=int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\narch/s390/net/bpf_jit_comp.c-3049-\narch/s390/net/bpf_jit_comp.c:3050:\tret = __arch_prepare_bpf_trampoline(\u0026im, \u0026tjit, m, flags,\narch/s390/net/bpf_jit_comp.c-3051-\t\t\t\t\t tnodes, orig_call);\n--\narch/s390/net/bpf_jit_comp.c-3055-\narch/s390/net/bpf_jit_comp.c:3056:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image,\narch/s390/net/bpf_jit_comp.c-3057-\t\t\t\tvoid *image_end, const struct btf_func_model *m,\n--\narch/s390/net/bpf_jit_comp.c-3065-\tmemset(\u0026tjit, 0, sizeof(tjit));\narch/s390/net/bpf_jit_comp.c:3066:\tret = __arch_prepare_bpf_trampoline(im, \u0026tjit, m, flags,\narch/s390/net/bpf_jit_comp.c-3067-\t\t\t\t\t tnodes, func_addr);\n--\narch/s390/net/bpf_jit_comp.c-3079-\ttjit.common.prg_buf = image;\narch/s390/net/bpf_jit_comp.c:3080:\tret = __arch_prepare_bpf_trampoline(im, \u0026tjit, m, flags,\narch/s390/net/bpf_jit_comp.c-3081-\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=3786=int arch_protect_bpf_trampoline(void *image, unsigned int size)\n--\narch/x86/net/bpf_jit_comp.c-3790-\narch/x86/net/bpf_jit_comp.c:3791:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,\narch/x86/net/bpf_jit_comp.c-3792-\t\t\t\tconst struct btf_func_model *m, u32 flags,\n--\narch/x86/net/bpf_jit_comp.c-3806-\narch/x86/net/bpf_jit_comp.c:3807:\tret = __arch_prepare_bpf_trampoline(im, rw_image, rw_image + size, image, m,\narch/x86/net/bpf_jit_comp.c-3808-\t\t\t\t\t flags, tnodes, func_addr);\n--\narch/x86/net/bpf_jit_comp.c=3820=int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\narch/x86/net/bpf_jit_comp.c-3826-\narch/x86/net/bpf_jit_comp.c:3827:\t/* Allocate a temporary buffer for __arch_prepare_bpf_trampoline().\narch/x86/net/bpf_jit_comp.c-3828-\t *\n--\narch/x86/net/bpf_jit_comp.c-3837-\narch/x86/net/bpf_jit_comp.c:3838:\tret = __arch_prepare_bpf_trampoline(\u0026im, image, image + PAGE_SIZE, image,\narch/x86/net/bpf_jit_comp.c-3839-\t\t\t\t\t m, flags, tnodes, func_addr);\n--\ninclude/linux/bpf.h=1314=struct bpf_tramp_image;\ninclude/linux/bpf.h:1315:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,\ninclude/linux/bpf.h-1316-\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=748=static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex,\n--\nkernel/bpf/trampoline.c-817-\nkernel/bpf/trampoline.c:818:\terr = arch_prepare_bpf_trampoline(im, im-\u003eimage, im-\u003eimage + size,\nkernel/bpf/trampoline.c-819-\t\t\t\t\t \u0026tr-\u003efunc.model, tr-\u003eflags, tnodes,\n--\nkernel/bpf/trampoline.c=1523=int __weak\nkernel/bpf/trampoline.c:1524:arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,\nkernel/bpf/trampoline.c-1525-\t\t\t const struct btf_func_model *m, u32 flags,\n"
} |
| 15/2 | 2026/09/12 10:07 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 28204 output: 22 thoughts: 63 Thoughts: **My BPF Configuration Checklist** Okay, so I'm diving into this BPF configuration, and the first things that jump out are `BPF_JIT` and `BPF_SYSCALL`. Those are the usual suspects, so I'll make a mental note to confirm they're enabled. But I'm also thinking ahead – are there any *other* configs that might be needed, especially if we're using trampolines or planning to do some BPF text poking? Best to be thorough, especially when dealing with such low-level stuff. So, let's fire up `grep` and see what else might be lurking in the kernel config related to `bpf_arch_text_poke`. That should flush out any hidden dependencies I might be missing. Gotta get this right. |
| 16/2 | 2026/09/12 10:07 | tool | grepper |
0mArgs:{
"Expression": "bpf_arch_text_poke"
}Results: {
"Output": "arch/arm64/net/bpf_jit_comp.c=3094=static int gen_branch_or_nop(enum aarch64_insn_branch_type type, void *ip,\n--\narch/arm64/net/bpf_jit_comp.c-3175- */\narch/arm64/net/bpf_jit_comp.c:3176:int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,\narch/arm64/net/bpf_jit_comp.c-3177-\t\t enum bpf_text_poke_type new_t, void *old_addr,\n--\narch/loongarch/net/bpf_jit.c=1568=void *bpf_arch_text_copy(void *dst, void *src, size_t len)\n--\narch/loongarch/net/bpf_jit.c-1580-\narch/loongarch/net/bpf_jit.c:1581:int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,\narch/loongarch/net/bpf_jit.c-1582-\t\t enum bpf_text_poke_type new_t, void *old_addr,\n--\narch/powerpc/net/bpf_jit_comp.c=820=static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_image,\n--\narch/powerpc/net/bpf_jit_comp.c-993-\t * after the 'bl' instruction in the OOL stub. Refer to ftrace_init_ool_stub() and\narch/powerpc/net/bpf_jit_comp.c:994:\t * bpf_arch_text_poke() for OOL stub of kernel functions and bpf programs respectively.\narch/powerpc/net/bpf_jit_comp.c-995-\t * Relevant stub sequence:\n--\narch/powerpc/net/bpf_jit_comp.c=1265=static void do_isync(void *info __maybe_unused)\n--\narch/powerpc/net/bpf_jit_comp.c-1308- */\narch/powerpc/net/bpf_jit_comp.c:1309:int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,\narch/powerpc/net/bpf_jit_comp.c-1310-\t\t enum bpf_text_poke_type new_t, void *old_addr,\n--\narch/riscv/net/bpf_jit_comp64.c=807=static int gen_jump_or_nops(void *target, void *ip, u32 *insns, bool is_call)\n--\narch/riscv/net/bpf_jit_comp64.c-824-\narch/riscv/net/bpf_jit_comp64.c:825:int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,\narch/riscv/net/bpf_jit_comp64.c-826-\t\t enum bpf_text_poke_type new_t, void *old_addr,\n--\narch/s390/net/bpf_jit_comp.c=2480=bool bpf_jit_supports_far_kfunc_call(void)\n--\narch/s390/net/bpf_jit_comp.c-2484-\narch/s390/net/bpf_jit_comp.c:2485:int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,\narch/s390/net/bpf_jit_comp.c-2486-\t\t enum bpf_text_poke_type new_t, void *old_addr,\n--\narch/x86/net/bpf_jit_comp.c=616=static int emit_jump(u8 **pprog, void *func, void *ip)\n--\narch/x86/net/bpf_jit_comp.c-620-\narch/x86/net/bpf_jit_comp.c:621:static int __bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,\narch/x86/net/bpf_jit_comp.c-622-\t\t\t\tenum bpf_text_poke_type new_t,\n--\narch/x86/net/bpf_jit_comp.c-664-\narch/x86/net/bpf_jit_comp.c:665:int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,\narch/x86/net/bpf_jit_comp.c-666-\t\t enum bpf_text_poke_type new_t, void *old_addr,\n--\narch/x86/net/bpf_jit_comp.c-680-\narch/x86/net/bpf_jit_comp.c:681:\treturn __bpf_arch_text_poke(ip, old_t, new_t, old_addr, new_addr);\narch/x86/net/bpf_jit_comp.c-682-}\n--\narch/x86/net/bpf_jit_comp.c=901=static void bpf_tail_call_direct_fixup(struct bpf_prog *prog)\n--\narch/x86/net/bpf_jit_comp.c-921-\t\tif (target) {\narch/x86/net/bpf_jit_comp.c:922:\t\t\tret = __bpf_arch_text_poke(poke-\u003etailcall_target,\narch/x86/net/bpf_jit_comp.c-923-\t\t\t\t\t\t BPF_MOD_NOP, BPF_MOD_JUMP,\n--\narch/x86/net/bpf_jit_comp.c-927-\t\t\tBUG_ON(ret \u003c 0);\narch/x86/net/bpf_jit_comp.c:928:\t\t\tret = __bpf_arch_text_poke(poke-\u003etailcall_bypass,\narch/x86/net/bpf_jit_comp.c-929-\t\t\t\t\t\t BPF_MOD_JUMP, BPF_MOD_NOP,\n--\narch/x86/net/bpf_jit_comp.c=4263=void bpf_arch_poke_desc_update(struct bpf_jit_poke_descriptor *poke,\n--\narch/x86/net/bpf_jit_comp.c-4275-\t * On program loading or teardown, the program's kallsym entry\narch/x86/net/bpf_jit_comp.c:4276:\t * might not be in place, so we use __bpf_arch_text_poke to skip\narch/x86/net/bpf_jit_comp.c-4277-\t * the kallsyms check.\n--\narch/x86/net/bpf_jit_comp.c-4280-\t\tt = old_addr ? BPF_MOD_JUMP : BPF_MOD_NOP;\narch/x86/net/bpf_jit_comp.c:4281:\t\tret = __bpf_arch_text_poke(poke-\u003etailcall_target,\narch/x86/net/bpf_jit_comp.c-4282-\t\t\t\t\t t, BPF_MOD_JUMP,\n--\narch/x86/net/bpf_jit_comp.c-4285-\t\tif (!old) {\narch/x86/net/bpf_jit_comp.c:4286:\t\t\tret = __bpf_arch_text_poke(poke-\u003etailcall_bypass,\narch/x86/net/bpf_jit_comp.c-4287-\t\t\t\t\t\t BPF_MOD_JUMP, BPF_MOD_NOP,\n--\narch/x86/net/bpf_jit_comp.c-4293-\t\tt = old_bypass_addr ? BPF_MOD_JUMP : BPF_MOD_NOP;\narch/x86/net/bpf_jit_comp.c:4294:\t\tret = __bpf_arch_text_poke(poke-\u003etailcall_bypass,\narch/x86/net/bpf_jit_comp.c-4295-\t\t\t\t\t t, BPF_MOD_JUMP, old_bypass_addr,\n--\narch/x86/net/bpf_jit_comp.c-4304-\t\tt = old_addr ? BPF_MOD_JUMP : BPF_MOD_NOP;\narch/x86/net/bpf_jit_comp.c:4305:\t\tret = __bpf_arch_text_poke(poke-\u003etailcall_target,\narch/x86/net/bpf_jit_comp.c-4306-\t\t\t\t\t t, BPF_MOD_NOP, old_addr, NULL);\n--\ninclude/linux/bpf.h=4094=enum bpf_text_poke_type {\n--\ninclude/linux/bpf.h-4099-\ninclude/linux/bpf.h:4100:int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,\ninclude/linux/bpf.h-4101-\t\t enum bpf_text_poke_type new_t, void *old_addr,\n--\nkernel/bpf/core.c=3337=int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,\n--\nkernel/bpf/core.c-3342-\nkernel/bpf/core.c:3343:int __weak bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,\nkernel/bpf/core.c-3344-\t\t\t enum bpf_text_poke_type new_t, void *old_addr,\n--\nkernel/bpf/trampoline.c=412=static int bpf_trampoline_update_fentry(struct bpf_trampoline *tr, u32 orig_flags,\n--\nkernel/bpf/trampoline.c-427-\nkernel/bpf/trampoline.c:428:\treturn bpf_arch_text_poke(ip, old_t, new_t, old_addr, new_addr);\nkernel/bpf/trampoline.c-429-}\n--\nkernel/bpf/trampoline.c=620=static void bpf_tramp_image_put(struct bpf_tramp_image *im)\n--\nkernel/bpf/trampoline.c-648-\tif (im-\u003eip_after_call) {\nkernel/bpf/trampoline.c:649:\t\tint err = bpf_arch_text_poke(im-\u003eip_after_call, BPF_MOD_NOP,\nkernel/bpf/trampoline.c-650-\t\t\t\t\t BPF_MOD_JUMP, NULL,\n--\nkernel/bpf/trampoline.c=721=static void bpf_trampoline_skip_prog(struct bpf_trampoline *tr, struct bpf_prog *prog)\n--\nkernel/bpf/trampoline.c-731-\t\t\t\tcontinue;\nkernel/bpf/trampoline.c:732:\t\t\terr = bpf_arch_text_poke(skip-\u003enop, BPF_MOD_NOP, BPF_MOD_JUMP,\nkernel/bpf/trampoline.c-733-\t\t\t\t\t\t NULL, skip-\u003etarget);\n--\nkernel/bpf/trampoline.c=987=static int __bpf_trampoline_link_prog(struct bpf_tramp_node *node,\n--\nkernel/bpf/trampoline.c-1021-\t\ttr-\u003eextension_prog = node-\u003elink-\u003eprog;\nkernel/bpf/trampoline.c:1022:\t\treturn bpf_arch_text_poke(tr-\u003efunc.addr, BPF_MOD_NOP,\nkernel/bpf/trampoline.c-1023-\t\t\t\t\t BPF_MOD_JUMP, NULL,\n--\nkernel/bpf/trampoline.c=1047=static int __bpf_trampoline_unlink_prog(struct bpf_tramp_node *node,\n--\nkernel/bpf/trampoline.c-1058-\t\tWARN_ON_ONCE(!tr-\u003eextension_prog);\nkernel/bpf/trampoline.c:1059:\t\terr = bpf_arch_text_poke(tr-\u003efunc.addr, BPF_MOD_JUMP,\nkernel/bpf/trampoline.c-1060-\t\t\t\t\t BPF_MOD_NOP,\n"
} |
| 17/2 | 2026/09/12 10:07 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 31720 output: 33 thoughts: 1073 Thoughts: **Analyzing BPF Trampoline Vulnerability for Fuzzing**
Okay, let's break this down. My initial thought is that `bpf_trampoline_skip_prog` is the key here. It gets called when a BPF program is being removed or unlinked from a trampoline, specifically via `bpf_trampoline_remove_prog`. The commit I'm looking at addresses a classic use-after-free issue. If a program is detached while another task is still *in* that program (e.g., sleeping within a fentry/fexit), freeing it prematurely would cause issues. To prevent this, `bpf_trampoline_skip_prog` cleverly patches the preceding NOP instruction to jump over the detached program in *all* active trampoline images. This is done on x86, which is what I'm interested in.
Now, let's determine if this is fuzzable.
1. **WorthFuzzing?** Absolutely. This is reachable on amd64 (x86\_64), which utilizes `arch/x86/net/bpf_jit_comp.c`, `kernel/bpf/trampoline.c`, and `include/linux/bpf.h`. BPF trampolines are crucial for fentry, fexit, mod\_ret, BPF LSM, struct\_ops, and more. Userspace interaction via the `bpf()` syscall (`BPF_PROG_LOAD`, `BPF_LINK_CREATE`, `close()` or `BPF_LINK_DETACH`) allows loading, attaching, and detaching tracing programs. This introduces dynamic patching, memory allocation, list operations, reference counting, and potential assertions. So, yes, there are a lot of moving parts here that could benefit from fuzzing.
2. **FocusSymbols?** Now, for finding the specific functions of interest. I need the less ubiquitous ones involved. The critical functions modified or directly involved in the logic I'm interested in on x86, are:
* `kernel/bpf/trampoline.c`: `bpf_trampoline_skip_prog`, `bpf_trampoline_remove_prog`, `__bpf_trampoline_unlink_prog`, `bpf_trampoline_unlink_prog`, `bpf_trampoline_update`, `bpf_tramp_image_alloc`, `bpf_tramp_image_free`, `__bpf_tramp_image_put_deferred`, `bpf_trampoline_link_prog`.
* `arch/x86/net/bpf_jit_comp.c`: `arch_prepare_bpf_trampoline`, `arch_bpf_trampoline_size`, `invoke_bpf_prog`, `invoke_bpf`, `invoke_bpf_mod_ret`.
* `include/linux/bpf.h`: `bpf_tramp_image_add_skip`.
I will now check if `bpf_trampoline_unlink_prog`, `bpf_trampoline_link_prog`, and `arch_prepare_bpf_trampoline` are good symbols to focus on. I will also investigate who calls `bpf_trampoline_unlink_prog` and `bpf_trampoline_link_prog` within `kernel/bpf/`. This should give me a good starting point for fuzzing.
|
| 18/2 | 2026/09/12 10:08 | tool | grepper |
0mArgs:{
"Expression": "bpf_trampoline_unlink_prog",
"PathPrefix": "kernel/bpf"
}Results: {
"Output": "kernel/bpf/syscall.c=3568=static void bpf_tracing_link_release(struct bpf_link *link)\n--\nkernel/bpf/syscall.c-3573-\nkernel/bpf/syscall.c:3574:\terr = bpf_trampoline_unlink_prog(\u0026tr_link-\u003elink.node,\nkernel/bpf/syscall.c-3575-\t\t\t\t\ttr_link-\u003etrampoline,\nkernel/bpf/syscall.c-3576-\t\t\t\t\ttr_link-\u003etgt_prog);\nkernel/bpf/syscall.c:3577:\tWARN_ONCE(err, \"bpf_trampoline_unlink_prog failed: %d\\n\", err);\nkernel/bpf/syscall.c-3578-\n--\nkernel/bpf/trampoline.c=1035=int bpf_trampoline_link_prog(struct bpf_tramp_node *node,\n--\nkernel/bpf/trampoline.c-1046-\nkernel/bpf/trampoline.c:1047:static int __bpf_trampoline_unlink_prog(struct bpf_tramp_node *node,\nkernel/bpf/trampoline.c-1048-\t\t\t\t\tstruct bpf_trampoline *tr,\n--\nkernel/bpf/trampoline.c-1070-\nkernel/bpf/trampoline.c:1071:/* bpf_trampoline_unlink_prog() should never fail. */\nkernel/bpf/trampoline.c:1072:int bpf_trampoline_unlink_prog(struct bpf_tramp_node *node,\nkernel/bpf/trampoline.c-1073-\t\t\t struct bpf_trampoline *tr,\n--\nkernel/bpf/trampoline.c-1078-\ttrampoline_lock(tr);\nkernel/bpf/trampoline.c:1079:\terr = __bpf_trampoline_unlink_prog(node, tr, tgt_prog, \u0026trampoline_ops, NULL);\nkernel/bpf/trampoline.c-1080-\ttrampoline_unlock(tr);\n--\nkernel/bpf/trampoline.c=1085=static void bpf_shim_tramp_link_release(struct bpf_link *link)\n--\nkernel/bpf/trampoline.c-1094-\nkernel/bpf/trampoline.c:1095:\terr = bpf_trampoline_unlink_prog(\u0026shim_link-\u003elink.node, shim_link-\u003etrampoline, NULL);\nkernel/bpf/trampoline.c:1096:\tWARN_ONCE(err, \"bpf_trampoline_unlink_prog failed: %d\\n\", err);\nkernel/bpf/trampoline.c-1097-\n--\nkernel/bpf/trampoline.c=1814=void bpf_trampoline_multi_detach(struct bpf_prog *prog,\n--\nkernel/bpf/trampoline.c-1825-\t\tbpf_trampoline_multi_attach_init(mnode-\u003etrampoline);\nkernel/bpf/trampoline.c:1826:\t\terr = __bpf_trampoline_unlink_prog(\u0026mnode-\u003enode, mnode-\u003etrampoline, NULL,\nkernel/bpf/trampoline.c-1827-\t\t\t\t\t\u0026trampoline_multi_ops, data);\nkernel/bpf/trampoline.c:1828:\t\tWARN_ONCE(err, \"__bpf_trampoline_unlink_prog failed: %d\\n\", err);\nkernel/bpf/trampoline.c-1829-\t}\n"
} |
| 19/2 | 2026/09/12 10:08 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 34470 output: 28 thoughts: 455 Thoughts: **BPF Trampoline Investigation** Okay, so I'm digging into how BPF trampolines are handled, specifically focusing on the linking and unlinking process. It's clear that `bpf_trampoline_unlink_prog` is directly called when a link is released, and conversely, `bpf_trampoline_link_prog` is triggered during link attachment. The architectural function responsible for JIT compiling the trampoline is `arch_prepare_bpf_trampoline`, which is good to know. Now, I want to see what's going on with `bpf_trampoline_update`. Beyond that, I'm taking a look at other modified functions within `kernel/bpf/trampoline.c` to get a complete picture, so I'm checking out: `bpf_trampoline_skip_prog`, `bpf_tramp_image_alloc`, `bpf_tramp_image_free`, `__bpf_tramp_image_put_deferred`, `bpf_trampoline_remove_prog`, `arch_bpf_trampoline_size`. Aha, `bpf_trampoline_skip_prog` is static! That's good to know, and the KCOV tracker will include both the static functions and their callers, that's what I want. This means the list of non-static functions of interest includes: - `bpf_trampoline_unlink_prog` - `bpf_trampoline_link_prog` - `arch_prepare_bpf_trampoline` - `arch_bpf_trampoline_size` - `bpf_trampoline_skip_prog` - `bpf_trampoline_update` - `bpf_tramp_image_alloc` - `bpf_tramp_image_free` - `__bpf_tramp_image_put_deferred` Next, I'll need to look at the x86 configuration required for BPF trampolines. I should be able to get this from Kconfig, by checking where `bpf_trampoline` is used and configured. |
| 20/2 | 2026/09/12 10:08 | tool | grepper |
0mError:no matches Args: {
"Expression": "BPF_TRAMPOLINE",
"PathPrefix": "kernel"
}Results: {
"error": "no matches"
} |
| 21/2 | 2026/09/12 10:08 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 35431 output: 30 thoughts: 72 Thoughts: **Investigating BPF JIT Configuration for x86** Okay, let's break this down. I'm looking at the `kernel/bpf/Makefile` and specifically, the line `obj-$(CONFIG_BPF_JIT) += trampoline.o`. That tells me that the `trampoline.o` object file gets included when `CONFIG_BPF_JIT` is enabled. My question is: does the x86 architecture have any specific selections or dependencies related to BPF JIT? It's time to figure out how it's enabled for x86. The best way to do that is to search within the `arch/x86/` directory for any references to `CONFIG_BPF_JIT`. That should reveal whether the architecture has any relevant configurations or mechanisms in place to handle the BPF JIT compilation. Let's get to it. |
| 22/2 | 2026/09/12 10:08 | tool | grepper |
0mArgs:{
"Expression": "BPF_JIT",
"PathPrefix": "arch/x86"
}Results: {
"Output": "arch/x86/Kconfig=58=config X86\n--\narch/x86/Kconfig-143-\tselect ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH\narch/x86/Kconfig:144:\tselect ARCH_WANT_DEFAULT_BPF_JIT\tif X86_64\narch/x86/Kconfig-145-\tselect ARCH_WANTS_CLOCKSOURCE_READ_INLINE\tif X86_64\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--\narch/x86/include/asm/extable.h=46=static inline void __noreturn ex_handler_msr_mce(struct pt_regs *regs, bool wrmsr)\n--\narch/x86/include/asm/extable.h-52-\narch/x86/include/asm/extable.h:53:#if defined(CONFIG_BPF_JIT) \u0026\u0026 defined(CONFIG_X86_64)\narch/x86/include/asm/extable.h-54-bool ex_handler_bpf(const struct exception_table_entry *x, struct pt_regs *regs);\n--\narch/x86/include/asm/nospec-branch.h=443=extern void write_ibpb(void);\narch/x86/include/asm/nospec-branch.h-444-\narch/x86/include/asm/nospec-branch.h:445:#ifdef CONFIG_BPF_JIT\narch/x86/include/asm/nospec-branch.h-446-extern void bpf_arch_ibpb(void);\n--\narch/x86/kernel/callthunks.c=265=void *callthunks_translate_call_dest(void *dest)\n--\narch/x86/kernel/callthunks.c-280-\narch/x86/kernel/callthunks.c:281:#ifdef CONFIG_BPF_JIT\narch/x86/kernel/callthunks.c-282-static bool is_callthunk(void *addr)\n--\narch/x86/kernel/cpu/bugs.c=2236=static void __init spectre_v2_update_mitigation(void)\n--\narch/x86/kernel/cpu/bugs.c-2253-\narch/x86/kernel/cpu/bugs.c:2254:#ifdef CONFIG_BPF_JIT\narch/x86/kernel/cpu/bugs.c-2255-static void __bpf_arch_ibpb(void *unused)\n--\narch/x86/kernel/cpu/bugs.c=2275=static void __init spectre_v2_apply_mitigation(void)\n--\narch/x86/kernel/cpu/bugs.c-2351-\narch/x86/kernel/cpu/bugs.c:2352:#ifdef CONFIG_BPF_JIT\narch/x86/kernel/cpu/bugs.c-2353-\tif (cpu_wants_ibpb_bpf()) {\n--\narch/x86/kernel/unwind_orc.c=185=static struct orc_entry *orc_bpf_find(unsigned long ip)\narch/x86/kernel/unwind_orc.c-186-{\narch/x86/kernel/unwind_orc.c:187:#ifdef CONFIG_BPF_JIT\narch/x86/kernel/unwind_orc.c-188-\tif (bpf_has_frame_pointer(ip))\n--\narch/x86/net/Makefile=6=ifeq ($(CONFIG_X86_32),y)\narch/x86/net/Makefile:7: obj-$(CONFIG_BPF_JIT) += bpf_jit_comp32.o\narch/x86/net/Makefile-8-else\narch/x86/net/Makefile:9: obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o\narch/x86/net/Makefile-10-endif\n--\narch/x86/net/bpf_jit_comp.c=139=static int bpf_size_to_x86_bytes(int bpf_size)\n--\narch/x86/net/bpf_jit_comp.c-168-/* Pick a register outside of BPF range for JIT internal work */\narch/x86/net/bpf_jit_comp.c:169:#define AUX_REG (MAX_BPF_JIT_REG + 1)\narch/x86/net/bpf_jit_comp.c:170:#define X86_REG_R9 (MAX_BPF_JIT_REG + 2)\narch/x86/net/bpf_jit_comp.c:171:#define X86_REG_R12 (MAX_BPF_JIT_REG + 3)\narch/x86/net/bpf_jit_comp.c-172-\n--\narch/x86/net/bpf_jit_comp32.c=89=static bool is_simm32(s64 value)\n--\narch/x86/net/bpf_jit_comp32.c-94-#define STACK_OFFSET(k)\t(k)\narch/x86/net/bpf_jit_comp32.c:95:#define TCALL_CNT\t(MAX_BPF_JIT_REG + 0)\t/* Tail Call Count */\narch/x86/net/bpf_jit_comp32.c-96-\n"
} |
| 23/2 | 2026/09/12 10:08 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 37227 output: 45 thoughts: 268 Thoughts: **My BPF JIT Configuration Analysis** Okay, let's break this down. First, looking at `arch/x86/net/Makefile`, I see a conditional compilation for `bpf_jit_comp32.o` depending on `CONFIG_X86_32`. Otherwise, it's `bpf_jit_comp.o` and `bpf_timed_may_goto.o`. Interesting, but that's just the object file selection. Then, `kernel/bpf/Makefile` shows `trampoline.o` also relies on `CONFIG_BPF_JIT`. So, the key is definitely `CONFIG_BPF_JIT`. What triggers *that*? I know it hinges on `BPF` being enabled, and also either `HAVE_CBPF_JIT` or `HAVE_EBPF_JIT`. Of course, we need `BPF_SYSCALL` enabled to *use* BPF effectively - to load and attach programs. The dependency chain is clear now. Now, let me just quickly scan for any other configuration options that might influence the JIT compilation behavior, such as a "always on" or dynamic ftrace related flag. Perhaps something like `BPF_JIT_ALWAYS_ON` or `DYNAMIC_FTRACE_WITH_REGS`? I'll quickly double-check that. Finally, I should examine `arch_prepare_bpf_trampoline` in `arch/x86/net/bpf_jit_comp.c`. That function will likely hold some platform-specific details regarding how the trampoline is set up. That's the logical next step. |
| 24/2 | 2026/09/12 10:08 | tool | grepper |
0mArgs:{
"Expression": "arch_prepare_bpf_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=3786=int arch_protect_bpf_trampoline(void *image, unsigned int size)\n--\narch/x86/net/bpf_jit_comp.c-3790-\narch/x86/net/bpf_jit_comp.c:3791:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,\narch/x86/net/bpf_jit_comp.c-3792-\t\t\t\tconst struct btf_func_model *m, u32 flags,\n--\narch/x86/net/bpf_jit_comp.c-3806-\narch/x86/net/bpf_jit_comp.c:3807:\tret = __arch_prepare_bpf_trampoline(im, rw_image, rw_image + size, image, m,\narch/x86/net/bpf_jit_comp.c-3808-\t\t\t\t\t flags, tnodes, func_addr);\n--\narch/x86/net/bpf_jit_comp.c=3820=int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\narch/x86/net/bpf_jit_comp.c-3826-\narch/x86/net/bpf_jit_comp.c:3827:\t/* Allocate a temporary buffer for __arch_prepare_bpf_trampoline().\narch/x86/net/bpf_jit_comp.c-3828-\t *\n--\narch/x86/net/bpf_jit_comp.c-3837-\narch/x86/net/bpf_jit_comp.c:3838:\tret = __arch_prepare_bpf_trampoline(\u0026im, image, image + PAGE_SIZE, image,\narch/x86/net/bpf_jit_comp.c-3839-\t\t\t\t\t m, flags, tnodes, func_addr);\n"
} |
| 25/2 | 2026/09/12 10:08 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 38761 output: 38 thoughts: 78 Thoughts: **Checking the BPF JIT Compiler for x86** Okay, let's dive into this. I need to quickly ascertain whether there are any conditional compilation blocks, specifically `#ifdef` directives, within the `arch/x86/net/bpf_jit_comp.c` file that might influence the `arch_prepare_bpf_trampoline` function. This is a crucial step when working with architecture-specific code and JIT compilation. I'll need to focus my attention on the sections of the file that encompass the function in question. So, I'll start by checking lines 3460 through 3840. That should give me a good overview of the relevant code. Time to dig in and see what I find. |
| 26/2 | 2026/09/12 10:08 | tool | grepper |
0mArgs:{
"Expression": "#if",
"PathPrefix": "arch/x86/net/bpf_jit_comp.c"
}Results: {
"Output": "arch/x86/net/bpf_jit_comp.c=27=static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)\n--\narch/x86/net/bpf_jit_comp.c-58-\narch/x86/net/bpf_jit_comp.c:59:#ifdef CONFIG_X86_KERNEL_IBT\narch/x86/net/bpf_jit_comp.c-60-#define EMIT_ENDBR()\t\tEMIT(gen_endbr(), 4)\n--\narch/x86/net/bpf_jit_comp.c=465=static void emit_kcfi(u8 **pprog, u32 hash)\n--\narch/x86/net/bpf_jit_comp.c-469-\tEMIT1_off32(0xb8, hash);\t\t\t/* movl $hash, %eax\t*/\narch/x86/net/bpf_jit_comp.c:470:#ifdef CONFIG_CALL_PADDING\narch/x86/net/bpf_jit_comp.c-471-\tfor (int i = 0; i \u003c CONFIG_FUNCTION_PADDING_CFI; i++)\n--\narch/x86/net/bpf_jit_comp.c=1614=static void emit_priv_frame_ptr(u8 **pprog, void __percpu *priv_frame_ptr)\n--\narch/x86/net/bpf_jit_comp.c-1621-\narch/x86/net/bpf_jit_comp.c:1622:#ifdef CONFIG_SMP\narch/x86/net/bpf_jit_comp.c-1623-\t/* add \u003cr9\u003e, gs:[\u003coff\u003e] */\n--\narch/x86/net/bpf_jit_comp.c=1730=static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *addrs, u8 *image,\n--\narch/x86/net/bpf_jit_comp.c-1936-\t\t\t\tEMIT_mov(dst_reg, src_reg);\narch/x86/net/bpf_jit_comp.c:1937:#ifdef CONFIG_SMP\narch/x86/net/bpf_jit_comp.c-1938-\t\t\t\t/* add \u003cdst\u003e, gs:[\u003coff\u003e] */\n--\narch/x86/net/bpf_jit_comp.c=4247=void arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie)\narch/x86/net/bpf_jit_comp.c-4248-{\narch/x86/net/bpf_jit_comp.c:4249:#if defined(CONFIG_UNWINDER_ORC)\narch/x86/net/bpf_jit_comp.c-4250-\tstruct unwind_state state;\n"
} |
| 27/2 | 2026/09/12 10:08 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 39811 output: 31 thoughts: 53 Thoughts: **Examining BPF JIT Trampoline Compilation** Okay, so I'm looking into the BPF JIT compilation process, specifically where trampolines come into play on x86. I was expecting to find conditional compilation directives, you know, `#ifdef`s, sprinkled around the trampoline code in `arch/x86/net/bpf_jit_comp.c`. But it seems that's not the case. Hmm, that's interesting. Perhaps the configuration for trampolines is handled differently or perhaps it is compiled regardless. Let's dig deeper and see if the control flow is elsewhere. I'll need to check the core trampoline implementation itself in `kernel/bpf/trampoline.c` now to understand what's going on. Maybe there are some flags there that control this feature. |
| 28/2 | 2026/09/12 10:08 | tool | grepper |
0mArgs:{
"Expression": "#if",
"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=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=748=static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex,\n--\nkernel/bpf/trampoline.c-781-\nkernel/bpf/trampoline.c:782:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/bpf/trampoline.c-783-again:\n--\nkernel/bpf/trampoline.c-834-\nkernel/bpf/trampoline.c:835:#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\nkernel/bpf/trampoline.c-836-\tif (err == -EAGAIN) {\n--\nkernel/bpf/trampoline.c=1072=int bpf_trampoline_unlink_prog(struct bpf_tramp_node *node,\n--\nkernel/bpf/trampoline.c-1083-\nkernel/bpf/trampoline.c:1084:#if defined(CONFIG_CGROUP_BPF) \u0026\u0026 defined(CONFIG_BPF_LSM)\nkernel/bpf/trampoline.c-1085-static void bpf_shim_tramp_link_release(struct bpf_link *link)\n--\nkernel/bpf/trampoline.c=1559=int __weak arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\nkernel/bpf/trampoline.c-1564-\nkernel/bpf/trampoline.c:1565:#if defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) \u0026\u0026 \\\nkernel/bpf/trampoline.c-1566- defined(CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS) \u0026\u0026 \\\n"
} |
| 29/2 | 2026/09/12 10:08 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 40877 output: 36 thoughts: 84 Thoughts: **Examining the BPF Trampoline and Direct Calls** Alright, let's take a look at this. I see that the `bpf_trampoline_update` function has an `#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS` guard around its direct call registration. So, naturally, my next step is to get a deeper understanding of how this direct call registration is handled. I need to refresh my memory on the implementation details and how they interact with the dynamic ftrace features, so let's check out lines 60-80 of `kernel/bpf/trampoline.c` with a `read-file` command. I'm going to look for the key parts of that code region that help me understand this interplay better. |
| 30/2 | 2026/09/12 10:08 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/trampoline.c",
"FirstLine": 60,
"LineCount": 40
}Results: {
"Contents": " 60:\t}\n 61:\t\n 62:\tstruct bpf_trampoline_ops {\n 63:\t\tint (*register_fentry)(struct bpf_trampoline *tr, struct bpf_tramp_image *im, void *data);\n 64:\t\tint (*unregister_fentry)(struct bpf_trampoline *tr, u32 orig_flags, void *data);\n 65:\t\tint (*modify_fentry)(struct bpf_trampoline *tr, u32 orig_flags, struct bpf_tramp_image *im,\n 66:\t\t\t\t bool lock_direct_mutex, void *data);\n 67:\t};\n 68:\t\n 69:\t#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\n 70:\tstatic int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex,\n 71:\t\t\t\t\t const struct bpf_trampoline_ops *ops, void *data);\n 72:\tstatic const struct bpf_trampoline_ops trampoline_ops;\n 73:\t\n 74:\t#ifdef CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS\n 75:\tstatic struct bpf_trampoline *direct_ops_ip_lookup(struct ftrace_ops *ops, unsigned long ip)\n 76:\t{\n 77:\t\tstruct hlist_head *head_ip;\n 78:\t\tstruct bpf_trampoline *tr;\n 79:\t\n 80:\t\tmutex_lock(\u0026trampoline_mutex);\n 81:\t\thead_ip = \u0026trampoline_ip_table[hash_64(ip, TRAMPOLINE_HASH_BITS)];\n 82:\t\thlist_for_each_entry(tr, head_ip, hlist_ip) {\n 83:\t\t\tif (tr-\u003eip == ip)\n 84:\t\t\t\tgoto out;\n 85:\t\t}\n 86:\t\ttr = NULL;\n 87:\tout:\n 88:\t\tmutex_unlock(\u0026trampoline_mutex);\n 89:\t\treturn tr;\n 90:\t}\n 91:\t#else\n 92:\tstatic struct bpf_trampoline *direct_ops_ip_lookup(struct ftrace_ops *ops, unsigned long ip)\n 93:\t{\n 94:\t\treturn ops-\u003eprivate;\n 95:\t}\n 96:\t#endif /* CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS */\n 97:\t\n 98:\tstatic int bpf_tramp_ftrace_ops_func(struct ftrace_ops *ops, unsigned long ip,\n 99:\t\t\t\t\t enum ftrace_ops_cmd cmd)\n"
} |
| 31/2 | 2026/09/12 10:08 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 41842 output: 36 thoughts: 66 Thoughts: Here's my take: **Investigating x86_64 and Dynamic FTrace** Okay, so I'm looking at the build configuration, and it seems that `DYNAMIC_FTRACE_WITH_DIRECT_CALLS` is automatically selected on x86_64 when `DYNAMIC_FTRACE` is enabled. That's interesting, but I need to understand *why*. My next step is obvious; I need to dive into the Kconfig files. Specifically, I should check the definition of `DYNAMIC_FTRACE_WITH_DIRECT_CALLS` within `arch/x86/Kconfig` to figure out the conditions and rationale behind this automatic selection. This will give me the deeper context I need. |
| 32/2 | 2026/09/12 10:08 | tool | grepper |
0mArgs:{
"Expression": "DYNAMIC_FTRACE_WITH_DIRECT_CALLS",
"PathPrefix": "arch/x86"
}Results: {
"Output": "arch/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"
} |
| 33/2 | 2026/09/12 10:08 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 42587 output: 36 thoughts: 22 Thoughts: Here's my summary, as though I'm thinking through this myself: **Investigating `DYNAMIC_FTRACE_WITH_DIRECT_CALLS` Configuration Dependencies** Okay, so I need to figure out the dependencies for this `DYNAMIC_FTRACE_WITH_DIRECT_CALLS` config option. Where do I even start? I know the kernel build system pretty well, so I'll probably just dive into the Kconfig files. I'm guessing it’s going to be nested somewhere, with a parent config or two that needs to be enabled first. Let's trace it and see what falls into place. I'm expecting some standard tracing-related configs to be involved, and possibly something to do with the CPU architecture, given the "direct calls" aspect. Time to dig in! |
| 34/2 | 2026/09/12 10:08 | tool | grepper |
0mArgs:{
"Expression": "config DYNAMIC_FTRACE_WITH_DIRECT_CALLS",
"PathPrefix": "kernel/trace"
}Results: {
"Output": "kernel/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\n"
} |
| 35/2 | 2026/09/12 10:08 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 42905 output: 34 thoughts: 22 Thoughts: Here's my take on those lines from `kernel/trace/Kconfig`: **Analyzing `kernel/trace/Kconfig` Section** Okay, let's dive into lines 325-340 of `kernel/trace/Kconfig`. I need to get a clear picture of what's being configured, probably some tracing options. Right away, I see some `config` options being declared - good, they're individual configurations. Let me see... I'm looking for the dependencies here. The `depends on` clause is key; this tells me what needs to be enabled for this to even *be* an option. I'll need to figure out what those base dependencies are and whether they're core or more specialized. The `tristate` values are interesting; it looks like this particular option is not just a binary choice; it can be built-in or a module. Hmm, is this a key trace functionality or something more niche? The descriptions give some hints - I'm looking for keywords, functionality, maybe performance impact. I bet that's what that help text is designed to convey. Finally, I'll need to see how these options might interact with each other; can multiple of them be enabled at the same time, or is there some overlap or conflict? This whole section will ultimately influence the final kernel build and the tracing capabilities it ends up providing. I have to understand the nuances here. |
| 36/2 | 2026/09/12 10:08 | tool | read-file |
0mArgs:{
"File": "kernel/trace/Kconfig",
"FirstLine": 325,
"LineCount": 20
}Results: {
"Contents": " 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 340:\t\tdepends on DYNAMIC_FTRACE\n 341:\t\tdepends on HAVE_DYNAMIC_FTRACE_WITH_ARGS\n 342:\t\n 343:\tconfig DYNAMIC_FTRACE_WITH_JMP\n 344:\t\tdef_bool y\n"
} |
| 37/2 | 2026/09/12 10:08 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 43571 output: 37 thoughts: 219 Thoughts: **My Analysis of Kernel BPF and Dynamic Ftrace Dependencies** Okay, so I'm looking at how these kernel features interact. It looks like setting `DYNAMIC_FTRACE` automatically triggers `DYNAMIC_FTRACE_WITH_DIRECT_CALLS`. And then, if I enable `BPF_JIT` or `BPF_SYSCALL`, that means I'm getting BPF trampolines. Let me quickly verify if `BPF_JIT` has any hidden dependencies. I'll check `kernel/bpf/Kconfig`. Right, it depends on `BPF`, and either `HAVE_CBPF_JIT` or `HAVE_EBPF_JIT`, and importantly it *selects* `EXECMEM`. Good to know. Now, let's look at `BPF_SYSCALL` in the Kconfig. Yep, it also depends on `BPF`. And it just *selects* `BPF` by default. Makes sense. Back to `DYNAMIC_FTRACE`, I should double check what it depends on. Ah, right, that one is tied to `FUNCTION_TRACER`. That's important context. Finally, I'm going to have to dive into `kernel/bpf/trampoline.c` and check lines 770-820 to get a better understanding of how all of this is actually working under the hood. I suspect that section has some key details regarding the trampoline implementation and how it's integrated with everything we've just discussed. |
| 38/2 | 2026/09/12 10:08 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/trampoline.c",
"FirstLine": 770,
"LineCount": 50
}Results: {
"Contents": " 770:\t\t tnodes[BPF_TRAMP_MODIFY_RETURN].nr_nodes) {\n 771:\t\t\t/* NOTE: BPF_TRAMP_F_RESTORE_REGS and BPF_TRAMP_F_SKIP_FRAME\n 772:\t\t\t * should not be set together.\n 773:\t\t\t */\n 774:\t\t\ttr-\u003eflags |= BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME;\n 775:\t\t} else {\n 776:\t\t\ttr-\u003eflags |= BPF_TRAMP_F_RESTORE_REGS;\n 777:\t\t}\n 778:\t\n 779:\t\tif (ip_arg)\n 780:\t\t\ttr-\u003eflags |= BPF_TRAMP_F_IP_ARG;\n 781:\t\n 782:\t#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\n 783:\tagain:\n 784:\t\tif (tr-\u003eflags \u0026 BPF_TRAMP_F_CALL_ORIG) {\n 785:\t\t\tif (tr-\u003eflags \u0026 BPF_TRAMP_F_SHARE_IPMODIFY) {\n 786:\t\t\t\t/* The BPF_TRAMP_F_SKIP_FRAME can be cleared in the\n 787:\t\t\t\t * first try, reset it in the second try.\n 788:\t\t\t\t */\n 789:\t\t\t\ttr-\u003eflags |= BPF_TRAMP_F_ORIG_STACK | BPF_TRAMP_F_SKIP_FRAME;\n 790:\t\t\t} else if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_JMP)) {\n 791:\t\t\t\t/* Use \"jmp\" instead of \"call\" for the trampoline\n 792:\t\t\t\t * in the origin call case, and we don't need to\n 793:\t\t\t\t * skip the frame.\n 794:\t\t\t\t */\n 795:\t\t\t\ttr-\u003eflags \u0026= ~BPF_TRAMP_F_SKIP_FRAME;\n 796:\t\t\t}\n 797:\t\t}\n 798:\t#endif\n 799:\t\n 800:\t\tsize = arch_bpf_trampoline_size(\u0026tr-\u003efunc.model, tr-\u003eflags,\n 801:\t\t\t\t\t\ttnodes, tr-\u003efunc.addr);\n 802:\t\tif (size \u003c 0) {\n 803:\t\t\terr = size;\n 804:\t\t\tgoto out;\n 805:\t\t}\n 806:\t\n 807:\t\tif (size \u003e PAGE_SIZE) {\n 808:\t\t\terr = -E2BIG;\n 809:\t\t\tgoto out;\n 810:\t\t}\n 811:\t\n 812:\t\tim = bpf_tramp_image_alloc(tr-\u003ekey, size, total);\n 813:\t\tif (IS_ERR(im)) {\n 814:\t\t\terr = PTR_ERR(im);\n 815:\t\t\tgoto out;\n 816:\t\t}\n 817:\t\n 818:\t\terr = arch_prepare_bpf_trampoline(im, im-\u003eimage, im-\u003eimage + size,\n 819:\t\t\t\t\t\t \u0026tr-\u003efunc.model, tr-\u003eflags, tnodes,\n"
} |
| 39/2 | 2026/09/12 10:08 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 44997 output: 331 thoughts: 733 Thoughts: **My Assessment of the BPF Trampoline Vulnerability Fix** Okay, let's break this down. My initial thought process is focusing on the function calls involved in the BPF trampoline mechanism. I see `bpf_trampoline_update` calling `bpf_tramp_image_alloc`, then `arch_prepare_bpf_trampoline`, and finally adding to the `tr->images` structure. When unlinking, it's `bpf_trampoline_unlink_prog` which leads to `__bpf_trampoline_unlink_prog`, then `bpf_trampoline_remove_prog`, and finally, the crucial `bpf_trampoline_skip_prog`. Then, when the image is released, we see `bpf_tramp_image_put` leading to `__bpf_tramp_image_put_deferred` and eventually `bpf_tramp_image_free`. Now, let me analyze the key symbols involved. I've got a list here: `bpf_trampoline_link_prog`, `bpf_trampoline_unlink_prog`, `bpf_trampoline_update`, `bpf_trampoline_skip_prog`, `arch_prepare_bpf_trampoline`, `arch_bpf_trampoline_size`, `bpf_tramp_image_alloc`, `bpf_tramp_image_free`, and `__bpf_tramp_image_put_deferred`. Are any of these used ubiquitously across the kernel? No. They're all specific to BPF trampolines. That confirms this is a localized concern. Let's check the enabling configurations. I'm seeing `BPF_SYSCALL`, `BPF_JIT`, and `DYNAMIC_FTRACE_WITH_DIRECT_CALLS`. That points me toward the relevant parts of the kernel. Now, let's solidify the reasoning. First, the target architecture is amd64. The patch specifically deals with architecture-specific BPF JIT trampoline code on x86 (`arch/x86/net/bpf_jit_comp.c`) and the core BPF trampoline management (`kernel/bpf/trampoline.c`, `include/linux/bpf.h`). It's addressing a race condition or use-after-free issue where a detached BPF program *could still be* executed by threads in older trampoline images, particularly within sleepable fentry/fexit points. This fix introduces NOPs before program invocations, tracks active trampoline images, and dynamically patches the NOP to bypass detached programs using `bpf_trampoline_skip_prog()`. This involves dynamic patching (`bpf_arch_text_poke`), assertion checks (`WARN_ON_ONCE`), memory management (`im->skips`), and image list tracking, which are all worth reviewing. This code is fully reachable within standard QEMU environments on amd64 when using BPF syscalls to create and release tracing links (fentry/fexit/fmod_ret). Given all of that, it's clear this is definitely a fuzzing target. The logic is sound, the reasoning is solid, and it adheres to the guidelines. I'm ready to proceed. |
| 40/2 | 2026/09/12 10:08 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT",
"DYNAMIC_FTRACE_WITH_DIRECT_CALLS"
],
"FocusSymbols": [
"bpf_trampoline_link_prog",
"bpf_trampoline_unlink_prog",
"bpf_trampoline_update",
"bpf_trampoline_skip_prog",
"arch_prepare_bpf_trampoline",
"arch_bpf_trampoline_size",
"bpf_tramp_image_alloc",
"bpf_tramp_image_free",
"__bpf_tramp_image_put_deferred"
],
"Reasoning": "The patch addresses a use-after-free race condition in BPF trampolines where a task sleeping in an earlier BPF prog (e.g., sleepable fentry/fexit) could wake up and execute into an adjacent program that was detached and freed. It introduces per-program NOPs in generated trampolines (including x86 JIT), dynamically patches them to branch over detached programs via text poking in bpf_trampoline_skip_prog(), and manages active trampoline images in a linked list with updated refcounting and deferred cleanup. The modified code is in core BPF and x86 JIT, reachable on amd64 via the bpf() syscall (e.g., BPF_LINK_CREATE / detach of tracing links), making it worth fuzzing.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT",
"DYNAMIC_FTRACE_WITH_DIRECT_CALLS"
],
"FocusSymbols": [
"bpf_trampoline_link_prog",
"bpf_trampoline_unlink_prog",
"bpf_trampoline_update",
"bpf_trampoline_skip_prog",
"arch_prepare_bpf_trampoline",
"arch_bpf_trampoline_size",
"bpf_tramp_image_alloc",
"bpf_tramp_image_free",
"__bpf_tramp_image_put_deferred"
],
"Reasoning": "The patch addresses a use-after-free race condition in BPF trampolines where a task sleeping in an earlier BPF prog (e.g., sleepable fentry/fexit) could wake up and execute into an adjacent program that was detached and freed. It introduces per-program NOPs in generated trampolines (including x86 JIT), dynamically patches them to branch over detached programs via text poking in bpf_trampoline_skip_prog(), and manages active trampoline images in a linked list with updated refcounting and deferred cleanup. The modified code is in core BPF and x86 JIT, reachable on amd64 via the bpf() syscall (e.g., BPF_LINK_CREATE / detach of tracing links), making it worth fuzzing.",
"WorthFuzzing": true
} |
| 41/1 | 2026/09/12 10:08 | action | If |
0mArgs:{
"WorthFuzzing": true
}Results: null |
| 42/2 | 2026/09/12 10:08 | agent | kmsan-evaluator |
0mModel:core Args: null Results: {
"KMSANReasoning": "The patch series addresses a use-after-free (UAF) race condition in BPF trampolines where a task executing in a multi-prog trampoline could sleep (e.g. via a sleepable BPF program) while a subsequent attached program is detached and freed. When the sleeping task resumes, it would invoke the now-freed program. The fix emits NOPs before each invoked program call in the JIT trampoline images and patches them to jump instructions skipping the detached program.\n\nFrom a memory safety perspective:\n1. All newly introduced heap allocations (struct bpf_tramp_skip array via kzalloc_objs, struct bpf_tramp_image via kzalloc_obj) and stack allocations (struct bpf_tramp_image im = {} in arch_bpf_trampoline_size) are explicitly zero-initialized.\n2. No uninitialized memory is read, branched on, or exposed across user/kernel boundaries.\n3. The bug class targeted and any potential regressions introduced by this patch (use-after-free, refcounting issues on trampoline images, list corruption, or out-of-bounds accesses during text patching) fall squarely under the domain of KASAN, LOCKDEP, and standard kernel sanitizers, not KMSAN.\n\nTherefore, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit 486b508ac7e65d71c58ea5e0110860c000698245
Author: syz-cluster <triage@syzkaller.com>
Date: Sat Sep 12 10:07:25 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 c18e005a41dbe..9c166bdfbc6a6 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -2416,10 +2416,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;
@@ -2429,6 +2430,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);
@@ -2481,11 +2486,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;
@@ -2494,7 +2501,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;
@@ -2880,7 +2887,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);
}
@@ -2891,7 +2898,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);
}
@@ -2928,7 +2935,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, fexit->nodes[i], bargs_off, retval_off,
+ invoke_bpf_prog(ctx, im, fexit->nodes[i], bargs_off, retval_off,
run_ctx_off, false);
}
@@ -2992,7 +2999,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..0e6f1ad36c2d5 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;
@@ -2068,7 +2079,7 @@ 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;
@@ -2178,7 +2189,7 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
{
int ret;
struct jit_ctx ctx;
- struct bpf_tramp_image im;
+ struct bpf_tramp_image im = {};
ctx.image = NULL;
ctx.idx = 0;
diff --git a/arch/powerpc/net/bpf_jit_comp.c b/arch/powerpc/net/bpf_jit_comp.c
index 7b07b43575f11..95ea27148c918 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;
@@ -1123,8 +1131,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, 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;
}
@@ -1192,7 +1200,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 +1328,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..21f8ce2ec89ae 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;
@@ -1230,7 +1240,7 @@ 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;
@@ -1299,7 +1309,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..286a7c9215cb9 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;
/*
@@ -2962,7 +2987,7 @@ 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) {
@@ -3016,7 +3041,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..0d974da4f5093 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;
@@ -3708,7 +3717,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;
@@ -3811,7 +3820,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 e57af902560c3..9dcd3bc395eb0 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1258,11 +1258,15 @@ 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(__aarch64__)
+ BPF_MAX_TRAMP_LINKS = 37,
#else
BPF_MAX_TRAMP_LINKS = 38,
#endif
@@ -1363,6 +1367,17 @@ 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;
@@ -1374,8 +1389,27 @@ struct bpf_tramp_image {
struct rcu_head rcu;
struct work_struct work;
};
+ /* entry in tr->images, the image holds a reference on tr */
+ struct bpf_trampoline *tr;
+ struct list_head list;
+ struct bpf_tramp_skip *skips;
+ int nr_skips;
};
+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 +1436,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;
diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
index 90b70ea0d3709..b82d8592319a4 100644
--- a/kernel/bpf/trampoline.c
+++ b/kernel/bpf/trampoline.c
@@ -403,6 +403,7 @@ static struct bpf_trampoline *bpf_trampoline_lookup(u64 key, unsigned long ip)
refcount_set(&tr->refcnt, 1);
for (i = 0; i < BPF_TRAMP_MAX; i++)
INIT_HLIST_HEAD(&tr->progs_hlist[i]);
+ INIT_LIST_HEAD(&tr->images);
out:
mutex_unlock(&trampoline_mutex);
return tr;
@@ -565,14 +566,21 @@ 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_trampoline *tr;
struct bpf_tramp_image *im;
im = container_of(work, struct bpf_tramp_image, work);
+ tr = im->tr;
+ trampoline_lock(tr);
+ list_del(&im->list);
+ trampoline_unlock(tr);
+ bpf_trampoline_put(tr);
bpf_tramp_image_free(im);
}
@@ -658,7 +666,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 +677,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,11 +707,37 @@ 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);
}
+/*
+ * 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. Patch these images 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;
+ }
+ }
+}
+
void bpf_trampoline_set_flags(struct bpf_trampoline *tr, u32 flags)
{
trampoline_lock(tr);
@@ -771,7 +809,7 @@ 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;
@@ -806,8 +844,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)
@@ -937,6 +981,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,
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..9a0e1fc44c74c
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/tramp_prog_detach.c
@@ -0,0 +1,191 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <pthread.h>
+#include <poll.h>
+#include <sys/mman.h>
+#include <sys/syscall.h>
+#include <linux/userfaultfd.h>
+#include "tramp_prog_detach.skel.h"
+#include "testing_helpers.h"
+
+/*
+ * Detach and free a prog while a task sleeps in the prog that runs before it
+ * in the same trampoline image, then let that task continue through the
+ * image. It must not call into the freed prog.
+ *
+ * The task is held in a sleepable fentry prog with userfaultfd, like
+ * bpf_mod_race does.
+ */
+
+static int test_setup_uffd(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;
+}
+
+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;
+}
+
+static void test_detach(bool fexit)
+{
+ struct tramp_prog_detach *sleepable = NULL, *victim = NULL;
+ struct pollfd pfd = { .events = POLLIN };
+ struct uffdio_copy uffd_copy = {};
+ 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 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 */
+ victim = load_one(fexit, false);
+ if (!victim)
+ goto out;
+ sleepable = load_one(fexit, true);
+ if (!sleepable)
+ goto out;
+ sleepable_prog = pick_prog(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;
+ if (!ASSERT_LT(sleepable->bss->ts, victim->bss->ts, "prog order"))
+ goto out;
+
+ uffd = test_setup_uffd(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 prog, and make sure it is gone */
+ tramp_prog_detach__destroy(victim);
+ victim = NULL;
+ kern_sync_rcu();
+ usleep(100 * 1000);
+ kern_sync_rcu();
+
+ /*
+ * That was enough to test the use-after-free but do it once more, so
+ * that an older image with an already patched nop gets patched too.
+ */
+ victim = load_one(fexit, false);
+ tramp_prog_detach__destroy(victim);
+ victim = NULL;
+
+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");
+
+ tramp_prog_detach__destroy(victim);
+ 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);
+ /* a task sleeping after it returned, past the jmp that detach installs */
+ if (test__start_subtest("fexit"))
+ test_detach(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();
+}
|
| 43/3 | 2026/09/12 10:08 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 18095 output: 20 thoughts: 1699 Thoughts: **Analyzing the BPF Trampoline Patch Series for Fuzzing Implications**
Okay, so I've got this patch series related to BPF trampolines, and I need to figure out if it warrants a dedicated KMSAN fuzzing session, beyond what we're already doing with KASAN. My target is amd64, but I'll need to keep an eye on the broader architectural impact.
First, the commit message says this is a "syz-cluster: applied patch under review", so it's likely fixing an issue found by syzkaller. Let's dig into the changes.
The core of the patch involves adding "skip NOPs" to BPF trampoline code across multiple architectures (arm64, loongarch, powerpc, riscv, s390, and x86). The general pattern is:
* Adding a `skip` variable (a `void*` or a similar construct, architecture-specific) in `invoke_bpf_prog`.
* Inserting an `emit(NOP)` instruction before the call to a BPF program. For x86 it's using `emit_nops` function.
* Calling `bpf_tramp_image_add_skip()` to register the location of the NOP.
* Zero-initializing the `struct bpf_tramp_image im = {};` struct in the trampoline size calculation functions.
Looking deeper in `include/linux/bpf.h`, the changes introduce:
* Adjustments to `BPF_MAX_TRAMP_LINKS`.
* A new `struct bpf_tramp_skip` to store information about the skipped program, the NOP, and the target of the jump.
* A modified `struct bpf_tramp_image`, adding a list of skips and a count.
* The `bpf_tramp_image_add_skip()` function, which adds skip entries.
In `kernel/bpf/trampoline.c`, I see a lot of the core logic. Specifically, I see:
* Initialization of `tr->images` using `INIT_LIST_HEAD`.
* Memory allocation and deallocation for the skip structures (`bpf_tramp_image_alloc`, `bpf_tramp_image_free`, which now include `kzalloc_objs`, which looks important, and `kfree`).
* `bpf_trampoline_skip_prog()`, which iterates through the skips and patches the NOP to a jump.
* `bpf_trampoline_update()`, which links the new image into the trampoline.
* `bpf_trampoline_remove_prog()`, which calls `bpf_trampoline_skip_prog()` to remove the program.
And there are also tests. The tools directory has selftests dealing with a trampoline detach race!
The patch's motivation is clearly about addressing a use-after-free race condition in BPF trampolines. A BPF program could be freed while another task is still executing within the trampoline, leading to a UAF. The NOP insertion and patching fix this by creating a jump that skips the freed program.
The initialization of `struct bpf_tramp_image im = {};` is crucial. It ensures that the `im.skips` pointer is valid before the `bpf_tramp_image_add_skip()` is called. Without it, the code could potentially read from uninitialized memory, even though there's an earlier check within `bpf_tramp_image_add_skip()`.
Now, the critical question: Does this patch introduce any new uninitialized memory reads or information leaks? The `kzalloc_objs` use is something I need to carefully scrutinize. This is a common pattern, but let's see where it is used. I'll need to investigate where `kzalloc_objs` is defined and how it's used. Based on the name it looks like it's zero-initializing memory, and the allocation and deallocation code paths look correct (they `kfree` on error). However, I need to verify that all the pointers are initialized correctly. I need to make sure this patch *doesn't* introduce any new ways to leak kernel memory or read uninitialized values. I will now grep for `kzalloc_objs` and find its definition to verify my assumption. If it allocates, but does not initialize, or somehow fails, this could be a KMSAN candidate.
|
| 44/3 | 2026/09/12 10:08 | tool | grepper |
0mArgs:{
"Expression": "#define kzalloc_objs"
}Results: {
"Output": "include/linux/slab.h=1073=void *kmalloc_nolock(size_t size, gfp_t gfp_flags, int node);\n--\ninclude/linux/slab.h-1156-\t__alloc_objs(kzalloc, default_gfp(__VA_ARGS__), typeof(P), 1)\ninclude/linux/slab.h:1157:#define kzalloc_objs(P, COUNT, ...) \\\ninclude/linux/slab.h-1158-\t__alloc_objs(kzalloc, default_gfp(__VA_ARGS__), typeof(P), COUNT)\n"
} |
| 45/3 | 2026/09/12 10:08 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 20942 output: 37 thoughts: 95 Thoughts: **My Mental Debugging Process: Allocating BPF Trampoline Images** Okay, so I'm digging into how these BPF trampoline images are allocated. The code starts with `kzalloc_objs`, which, as I recall, is important because it guarantees zeroed memory. That's a critical safety feature when working with kernel memory; initialization is paramount to prevent information leakage or unexpected behavior. Now, I'm looking at the core allocation: `im = bpf_tramp_image_alloc(tr->key, size, total);`. Makes sense – we're calling a specific function, `bpf_tramp_image_alloc`, probably responsible for setting up the structure needed for the trampoline image. The arguments make sense: `tr->key` presumably identifies the trampoline, `size` relates to the size of the program, and `total` probably refers to the total amount of memory we need. I see that within `bpf_tramp_image_alloc`, there's another `kzalloc` (or something functionally equivalent) call: `im = kzalloc(sizeof(*im), GFP_KERNEL);`. This is where the actual memory allocation for the `im` struct occurs. So it's double-checked. First `kzalloc_objs`, then `bpf_tramp_image_alloc`. Alright, I'm going to jump into the `kernel/bpf/trampoline.c` file and dive into the implementation of `bpf_tramp_image_alloc`. That's where the magic really happens, and where I can confirm my assumptions about what's going on and debug if I've misunderstood anything. Need to check what's being initialized and how this allocation is interacting with the underlying memory management. |
| 46/3 | 2026/09/12 10:09 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/trampoline.c",
"FirstLine": 666,
"LineCount": 45
}Results: {
"Contents": " 666:\t\tcall_rcu_tasks_trace(\u0026im-\u003ercu, __bpf_tramp_image_put_rcu_tasks);\n 667:\t}\n 668:\t\n 669:\tstatic struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size, int nr_progs)\n 670:\t{\n 671:\t\tstruct bpf_tramp_image *im;\n 672:\t\tstruct bpf_ksym *ksym;\n 673:\t\tvoid *image;\n 674:\t\tint err = -ENOMEM;\n 675:\t\n 676:\t\tim = kzalloc_obj(*im);\n 677:\t\tif (!im)\n 678:\t\t\tgoto out;\n 679:\t\n 680:\t\tim-\u003eskips = kzalloc_objs(*im-\u003eskips, nr_progs);\n 681:\t\tif (!im-\u003eskips)\n 682:\t\t\tgoto out_free_im;\n 683:\t\n 684:\t\terr = bpf_jit_charge_modmem(size);\n 685:\t\tif (err)\n 686:\t\t\tgoto out_free_im;\n 687:\t\tim-\u003esize = size;\n 688:\t\n 689:\t\terr = -ENOMEM;\n 690:\t\tim-\u003eimage = image = arch_alloc_bpf_trampoline(size);\n 691:\t\tif (!image)\n 692:\t\t\tgoto out_uncharge;\n 693:\t\n 694:\t\terr = percpu_ref_init(\u0026im-\u003epcref, __bpf_tramp_image_release, 0, GFP_KERNEL);\n 695:\t\tif (err)\n 696:\t\t\tgoto out_free_image;\n 697:\t\n 698:\t\tksym = \u0026im-\u003eksym;\n 699:\t\tINIT_LIST_HEAD_RCU(\u0026ksym-\u003elnode);\n 700:\t\tsnprintf(ksym-\u003ename, KSYM_NAME_LEN, \"bpf_trampoline_%llu\", key);\n 701:\t\tbpf_image_ksym_init(image, size, ksym);\n 702:\t\tbpf_image_ksym_add(ksym);\n 703:\t\treturn im;\n 704:\t\n 705:\tout_free_image:\n 706:\t\tarch_free_bpf_trampoline(im-\u003eimage, im-\u003esize);\n 707:\tout_uncharge:\n 708:\t\tbpf_jit_uncharge_modmem(size);\n 709:\tout_free_im:\n 710:\t\tkfree(im-\u003eskips);\n"
} |
| 47/3 | 2026/09/12 10:09 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 22174 output: 37 thoughts: 312 Thoughts: **Analyzing BPF Trampoline Allocation and Skipping**
Okay, so I'm looking at how BPF trampolines are handled, specifically focusing on the `im` struct and the `im->skips` field. It's clear that both are initially zero-allocated using `kzalloc_obj` or `kzalloc_objs`. That's standard practice, initializing everything to zero before we even begin.
Now, I need to understand how the architecture-specific trampoline size factors in. We see a `struct bpf_tramp_image im = {};` being declared, and since it's an uninitialized struct, it's zeroed out. This means `im.skips` starts as NULL, which is important for the `bpf_tramp_image_add_skip` function.
This is where the check comes in:
```c
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;
}
```
The crucial part is the `if (!im || !im->skips)` check. If `im->skips` is NULL, which is the case when it's just initialized, the function safely returns. This prevents any dereferencing of a NULL pointer, which is exactly what we want. It's a robust guard.
The next question in my head is, how is the size for `im` determined when it's allocated by `bpf_tramp_image_alloc(tr->key, size, total)`? I'll need to dig into `bpf_trampoline_update` to find out where the `total` size comes from. That's the key to understanding how we size these skips correctly.
|
| 48/3 | 2026/09/12 10:09 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/trampoline.c",
"FirstLine": 750,
"LineCount": 45
}Results: {
"Contents": " 750:\t{\n 751:\t\tstruct bpf_tramp_image *im;\n 752:\t\tstruct bpf_tramp_nodes *tnodes;\n 753:\t\tu32 orig_flags = tr-\u003eflags;\n 754:\t\tbool ip_arg = false;\n 755:\t\tint err, total, size;\n 756:\t\n 757:\t\ttnodes = bpf_trampoline_get_progs(tr, \u0026total, \u0026ip_arg);\n 758:\t\tif (IS_ERR(tnodes))\n 759:\t\t\treturn PTR_ERR(tnodes);\n 760:\t\n 761:\t\tif (total == 0) {\n 762:\t\t\terr = ops-\u003eunregister_fentry(tr, orig_flags, data);\n 763:\t\t\tgoto out;\n 764:\t\t}\n 765:\t\n 766:\t\t/* clear all bits except SHARE_IPMODIFY and TAIL_CALL_CTX */\n 767:\t\ttr-\u003eflags \u0026= (BPF_TRAMP_F_SHARE_IPMODIFY | BPF_TRAMP_F_TAIL_CALL_CTX);\n 768:\t\n 769:\t\tif (tnodes[BPF_TRAMP_FEXIT].nr_nodes ||\n 770:\t\t tnodes[BPF_TRAMP_MODIFY_RETURN].nr_nodes) {\n 771:\t\t\t/* NOTE: BPF_TRAMP_F_RESTORE_REGS and BPF_TRAMP_F_SKIP_FRAME\n 772:\t\t\t * should not be set together.\n 773:\t\t\t */\n 774:\t\t\ttr-\u003eflags |= BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME;\n 775:\t\t} else {\n 776:\t\t\ttr-\u003eflags |= BPF_TRAMP_F_RESTORE_REGS;\n 777:\t\t}\n 778:\t\n 779:\t\tif (ip_arg)\n 780:\t\t\ttr-\u003eflags |= BPF_TRAMP_F_IP_ARG;\n 781:\t\n 782:\t#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\n 783:\tagain:\n 784:\t\tif (tr-\u003eflags \u0026 BPF_TRAMP_F_CALL_ORIG) {\n 785:\t\t\tif (tr-\u003eflags \u0026 BPF_TRAMP_F_SHARE_IPMODIFY) {\n 786:\t\t\t\t/* The BPF_TRAMP_F_SKIP_FRAME can be cleared in the\n 787:\t\t\t\t * first try, reset it in the second try.\n 788:\t\t\t\t */\n 789:\t\t\t\ttr-\u003eflags |= BPF_TRAMP_F_ORIG_STACK | BPF_TRAMP_F_SKIP_FRAME;\n 790:\t\t\t} else if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_JMP)) {\n 791:\t\t\t\t/* Use \"jmp\" instead of \"call\" for the trampoline\n 792:\t\t\t\t * in the origin call case, and we don't need to\n 793:\t\t\t\t * skip the frame.\n 794:\t\t\t\t */\n"
} |
| 49/3 | 2026/09/12 10:09 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 23832 output: 37 thoughts: 16 Thoughts: Okay, here's my attempt at summarizing the thought process, as if I were considering the provided text, and assuming a high level of expertise: **Diving Deeper into `bpf_trampoline_update`** Alright, so we're looking at `bpf_trampoline_update`. My immediate reaction is, "Hmm, this function's important. It's the core of how the trampoline is updated, presumably for security or performance reasons, or maybe both." I need to understand what's happening *within* it. I'm going to start by quickly scanning the function's structure. I'm thinking about the control flow – what inputs does it take? What does it do with those inputs? Are there any obvious security considerations or potential pitfalls? Any areas where a performance regression might creep in, such as loops, conditional jumps or data copies? I'm anticipating that I will want to check error handling specifically, and look for how it handles different scenarios, particularly edge cases that may be sources of instability. I'm already formulating questions in my mind: Are there race conditions? Are there memory leaks possible? Does this function play well with other BPF components? Okay, let's see what's actually there. |
| 50/3 | 2026/09/12 10:09 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/trampoline.c",
"FirstLine": 795,
"LineCount": 60
}Results: {
"Contents": " 795:\t\t\t\ttr-\u003eflags \u0026= ~BPF_TRAMP_F_SKIP_FRAME;\n 796:\t\t\t}\n 797:\t\t}\n 798:\t#endif\n 799:\t\n 800:\t\tsize = arch_bpf_trampoline_size(\u0026tr-\u003efunc.model, tr-\u003eflags,\n 801:\t\t\t\t\t\ttnodes, tr-\u003efunc.addr);\n 802:\t\tif (size \u003c 0) {\n 803:\t\t\terr = size;\n 804:\t\t\tgoto out;\n 805:\t\t}\n 806:\t\n 807:\t\tif (size \u003e PAGE_SIZE) {\n 808:\t\t\terr = -E2BIG;\n 809:\t\t\tgoto out;\n 810:\t\t}\n 811:\t\n 812:\t\tim = bpf_tramp_image_alloc(tr-\u003ekey, size, total);\n 813:\t\tif (IS_ERR(im)) {\n 814:\t\t\terr = PTR_ERR(im);\n 815:\t\t\tgoto out;\n 816:\t\t}\n 817:\t\n 818:\t\terr = arch_prepare_bpf_trampoline(im, im-\u003eimage, im-\u003eimage + size,\n 819:\t\t\t\t\t\t \u0026tr-\u003efunc.model, tr-\u003eflags, tnodes,\n 820:\t\t\t\t\t\t tr-\u003efunc.addr);\n 821:\t\tif (err \u003c 0)\n 822:\t\t\tgoto out_free;\n 823:\t\n 824:\t\terr = arch_protect_bpf_trampoline(im-\u003eimage, im-\u003esize);\n 825:\t\tif (err)\n 826:\t\t\tgoto out_free;\n 827:\t\n 828:\t\tif (tr-\u003ecur_image)\n 829:\t\t\t/* progs already running at this address */\n 830:\t\t\terr = ops-\u003emodify_fentry(tr, orig_flags, im, lock_direct_mutex, data);\n 831:\t\telse\n 832:\t\t\t/* first time registering */\n 833:\t\t\terr = ops-\u003eregister_fentry(tr, im, data);\n 834:\t\n 835:\t#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS\n 836:\t\tif (err == -EAGAIN) {\n 837:\t\t\t/* -EAGAIN from bpf_tramp_ftrace_ops_func. Now\n 838:\t\t\t * BPF_TRAMP_F_SHARE_IPMODIFY is set, we can generate the\n 839:\t\t\t * trampoline again, and retry register.\n 840:\t\t\t */\n 841:\t\t\tbpf_tramp_image_free(im);\n 842:\t\t\tgoto again;\n 843:\t\t}\n 844:\t#endif\n 845:\t\n 846:\tout_free:\n 847:\t\tif (err) {\n 848:\t\t\tbpf_tramp_image_free(im);\n 849:\t\t} else {\n 850:\t\t\t/* track the image until it is freed, for bpf_trampoline_skip_prog() */\n 851:\t\t\trefcount_inc(\u0026tr-\u003erefcnt);\n 852:\t\t\tim-\u003etr = tr;\n 853:\t\t\tlist_add(\u0026im-\u003elist, \u0026tr-\u003eimages);\n 854:\t\t}\n"
} |
| 51/3 | 2026/09/12 10:09 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 25027 output: 23 thoughts: 50 Thoughts: Okay, let's dive into this. Here's how I'd approach this analysis: **Investigating `bpf_tramp_image_alloc` Usage in Struct Operations** Right, so I need to understand how `bpf_tramp_image_alloc` is being used, specifically within the context of `struct_ops` trampolines. The logical next step is to see where it's *actually* being called. I'm going to start with a quick and dirty approach – I'll use `grep` to search the entire codebase for instances of `bpf_tramp_image_alloc`. That should quickly reveal the call sites. From there, I can trace back to understand the surrounding context and how `struct_ops` are involved, which I'll, of course, follow up on with a more focused investigation. |
| 52/3 | 2026/09/12 10:09 | tool | grepper |
0mArgs:{
"Expression": "bpf_tramp_image_alloc"
}Results: {
"Output": "kernel/bpf/trampoline.c=620=static void bpf_tramp_image_put(struct bpf_tramp_image *im)\n--\nkernel/bpf/trampoline.c-668-\nkernel/bpf/trampoline.c:669:static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size, int nr_progs)\nkernel/bpf/trampoline.c-670-{\n--\nkernel/bpf/trampoline.c=748=static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex,\n--\nkernel/bpf/trampoline.c-811-\nkernel/bpf/trampoline.c:812:\tim = bpf_tramp_image_alloc(tr-\u003ekey, size, total);\nkernel/bpf/trampoline.c-813-\tif (IS_ERR(im)) {\n"
} |
| 53/3 | 2026/09/12 10:09 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 25535 output: 23 thoughts: 66 Thoughts: **My Initial Thoughts on BPF Trampoline Allocation** Okay, so `bpf_tramp_image_alloc` is defined locally within `kernel/bpf/trampoline.c` and, as far as I can tell, it's exclusively called by `bpf_trampoline_update`. That's the starting point. But, to get a fuller picture of the trampoline preparation, I need to understand the architecture-specific aspects. That means digging into `struct_ops` and, more specifically, finding all the places where `arch_prepare_bpf_trampoline` is invoked. Understanding those call sites will give me a better understanding of the wider context of this function. |
| 54/3 | 2026/09/12 10:09 | tool | grepper |
0mArgs:{
"Expression": "arch_prepare_bpf_trampoline"
}Results: {
"Output": "arch/arm64/net/bpf_jit_comp.c=2686=static void store_func_meta(struct jit_ctx *ctx, u64 func_meta, int func_meta_off)\n--\narch/arm64/net/bpf_jit_comp.c-2691-\narch/arm64/net/bpf_jit_comp.c:2692:/* Based on the x86's implementation of arch_prepare_bpf_trampoline().\narch/arm64/net/bpf_jit_comp.c-2693- *\n--\narch/arm64/net/bpf_jit_comp.c=3027=int arch_protect_bpf_trampoline(void *image, unsigned int size)\n--\narch/arm64/net/bpf_jit_comp.c-3031-\narch/arm64/net/bpf_jit_comp.c:3032:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image,\narch/arm64/net/bpf_jit_comp.c-3033-\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-2148-\narch/loongarch/net/bpf_jit.c:2149:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image,\narch/loongarch/net/bpf_jit.c-2150-\t\t\t\tvoid *ro_image_end, const struct btf_func_model *m,\n--\narch/loongarch/net/bpf_jit.c-2166-\tjit_fill_hole(image, (unsigned int)(ro_image_end - ro_image));\narch/loongarch/net/bpf_jit.c:2167:\tret = __arch_prepare_bpf_trampoline(\u0026ctx, im, m, tnodes, func_addr, flags);\narch/loongarch/net/bpf_jit.c-2168-\tif (ret \u003c 0)\n--\narch/loongarch/net/bpf_jit.c=2187=int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\narch/loongarch/net/bpf_jit.c-2196-\narch/loongarch/net/bpf_jit.c:2197:\tret = __arch_prepare_bpf_trampoline(\u0026ctx, \u0026im, m, tnodes, func_addr, flags);\narch/loongarch/net/bpf_jit.c-2198-\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=1200=int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\narch/powerpc/net/bpf_jit_comp.c-1205-\narch/powerpc/net/bpf_jit_comp.c:1206:\tret = __arch_prepare_bpf_trampoline(\u0026im, NULL, NULL, NULL, m, flags, tnodes, func_addr);\narch/powerpc/net/bpf_jit_comp.c-1207-\treturn ret;\n--\narch/powerpc/net/bpf_jit_comp.c-1209-\narch/powerpc/net/bpf_jit_comp.c:1210:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,\narch/powerpc/net/bpf_jit_comp.c-1211-\t\t\t\tconst struct btf_func_model *m, u32 flags,\n--\narch/powerpc/net/bpf_jit_comp.c-1226-\narch/powerpc/net/bpf_jit_comp.c:1227:\tret = __arch_prepare_bpf_trampoline(im, rw_image, rw_image + size, image, m,\narch/powerpc/net/bpf_jit_comp.c-1228-\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=1309=int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\narch/riscv/net/bpf_jit_comp64.c-1318-\tctx.ro_insns = NULL;\narch/riscv/net/bpf_jit_comp64.c:1319:\tret = __arch_prepare_bpf_trampoline(\u0026im, m, tnodes, func_addr, flags, \u0026ctx);\narch/riscv/net/bpf_jit_comp64.c-1320-\n--\narch/riscv/net/bpf_jit_comp64.c=1329=void arch_free_bpf_trampoline(void *image, unsigned int size)\n--\narch/riscv/net/bpf_jit_comp64.c-1333-\narch/riscv/net/bpf_jit_comp64.c:1334:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image,\narch/riscv/net/bpf_jit_comp64.c-1335-\t\t\t\tvoid *ro_image_end, const struct btf_func_model *m,\n--\narch/riscv/net/bpf_jit_comp64.c-1350-\tctx.ro_insns = ro_image;\narch/riscv/net/bpf_jit_comp64.c:1351:\tret = __arch_prepare_bpf_trampoline(im, m, tnodes, func_addr, flags, \u0026ctx);\narch/riscv/net/bpf_jit_comp64.c-1352-\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=3041=int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\narch/s390/net/bpf_jit_comp.c-3049-\narch/s390/net/bpf_jit_comp.c:3050:\tret = __arch_prepare_bpf_trampoline(\u0026im, \u0026tjit, m, flags,\narch/s390/net/bpf_jit_comp.c-3051-\t\t\t\t\t tnodes, orig_call);\n--\narch/s390/net/bpf_jit_comp.c-3055-\narch/s390/net/bpf_jit_comp.c:3056:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image,\narch/s390/net/bpf_jit_comp.c-3057-\t\t\t\tvoid *image_end, const struct btf_func_model *m,\n--\narch/s390/net/bpf_jit_comp.c-3065-\tmemset(\u0026tjit, 0, sizeof(tjit));\narch/s390/net/bpf_jit_comp.c:3066:\tret = __arch_prepare_bpf_trampoline(im, \u0026tjit, m, flags,\narch/s390/net/bpf_jit_comp.c-3067-\t\t\t\t\t tnodes, func_addr);\n--\narch/s390/net/bpf_jit_comp.c-3079-\ttjit.common.prg_buf = image;\narch/s390/net/bpf_jit_comp.c:3080:\tret = __arch_prepare_bpf_trampoline(im, \u0026tjit, m, flags,\narch/s390/net/bpf_jit_comp.c-3081-\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=3786=int arch_protect_bpf_trampoline(void *image, unsigned int size)\n--\narch/x86/net/bpf_jit_comp.c-3790-\narch/x86/net/bpf_jit_comp.c:3791:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,\narch/x86/net/bpf_jit_comp.c-3792-\t\t\t\tconst struct btf_func_model *m, u32 flags,\n--\narch/x86/net/bpf_jit_comp.c-3806-\narch/x86/net/bpf_jit_comp.c:3807:\tret = __arch_prepare_bpf_trampoline(im, rw_image, rw_image + size, image, m,\narch/x86/net/bpf_jit_comp.c-3808-\t\t\t\t\t flags, tnodes, func_addr);\n--\narch/x86/net/bpf_jit_comp.c=3820=int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,\n--\narch/x86/net/bpf_jit_comp.c-3826-\narch/x86/net/bpf_jit_comp.c:3827:\t/* Allocate a temporary buffer for __arch_prepare_bpf_trampoline().\narch/x86/net/bpf_jit_comp.c-3828-\t *\n--\narch/x86/net/bpf_jit_comp.c-3837-\narch/x86/net/bpf_jit_comp.c:3838:\tret = __arch_prepare_bpf_trampoline(\u0026im, image, image + PAGE_SIZE, image,\narch/x86/net/bpf_jit_comp.c-3839-\t\t\t\t\t m, flags, tnodes, func_addr);\n--\ninclude/linux/bpf.h=1314=struct bpf_tramp_image;\ninclude/linux/bpf.h:1315:int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,\ninclude/linux/bpf.h-1316-\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=748=static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex,\n--\nkernel/bpf/trampoline.c-817-\nkernel/bpf/trampoline.c:818:\terr = arch_prepare_bpf_trampoline(im, im-\u003eimage, im-\u003eimage + size,\nkernel/bpf/trampoline.c-819-\t\t\t\t\t \u0026tr-\u003efunc.model, tr-\u003eflags, tnodes,\n--\nkernel/bpf/trampoline.c=1523=int __weak\nkernel/bpf/trampoline.c:1524:arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,\nkernel/bpf/trampoline.c-1525-\t\t\t const struct btf_func_model *m, u32 flags,\n"
} |
| 55/3 | 2026/09/12 10:09 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 30055 output: 33 thoughts: 426 Thoughts: **Analyzing BPF Trampoline Image Handling** Okay, so I'm looking at this `bpf_struct_ops_prepare_trampoline` function, and I see that the `im` variable, which appears to be a pointer to a `bpf_tramp_image` struct, can be `NULL` initially. Then, I cross-reference this with the `bpf_tramp_image_add_skip` function, and the check there makes perfect sense: when `im` is `NULL`, the function just returns. The comment clarifies that it's safe because struct operation trampolines and `arch_bpf_trampoline_size()` have no image. It also seems safe when `im` is the address of an empty image, since the `im->skips` field will then be `NULL`, and the code path similarly just returns. Now, I'm shifting focus to `bpf_trampoline_skip_prog`. This is where the real work of skipping a program within a trampoline seems to happen. It iterates through a list of images associated with the trampoline (`tr`). Inside, it iterates over the skips within each image. If a skip's `prog` pointer matches the target program, it then calls `bpf_arch_text_poke`. This function modifies the text of the BPF program, changing something to a NOP. Crucially, I have to examine the x86 implementation for `bpf_arch_text_poke` to understand the low-level change. Also, it sets the skip's program pointer to `NULL`. This way, the code prevents it from being skipped again. Essentially, the goal is to safely remove a program from the trampoline's execution path by patching it with a NOP, and the `bpf_trampoline_skip_prog` function ensures the safe handling of image structures and skip information during this process. |
| 56/3 | 2026/09/12 10:09 | tool | grepper |
0mArgs:{
"Expression": "bpf_arch_text_poke",
"PathPrefix": "arch/x86"
}Results: {
"Output": "arch/x86/net/bpf_jit_comp.c=616=static int emit_jump(u8 **pprog, void *func, void *ip)\n--\narch/x86/net/bpf_jit_comp.c-620-\narch/x86/net/bpf_jit_comp.c:621:static int __bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,\narch/x86/net/bpf_jit_comp.c-622-\t\t\t\tenum bpf_text_poke_type new_t,\n--\narch/x86/net/bpf_jit_comp.c-664-\narch/x86/net/bpf_jit_comp.c:665:int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,\narch/x86/net/bpf_jit_comp.c-666-\t\t enum bpf_text_poke_type new_t, void *old_addr,\n--\narch/x86/net/bpf_jit_comp.c-680-\narch/x86/net/bpf_jit_comp.c:681:\treturn __bpf_arch_text_poke(ip, old_t, new_t, old_addr, new_addr);\narch/x86/net/bpf_jit_comp.c-682-}\n--\narch/x86/net/bpf_jit_comp.c=901=static void bpf_tail_call_direct_fixup(struct bpf_prog *prog)\n--\narch/x86/net/bpf_jit_comp.c-921-\t\tif (target) {\narch/x86/net/bpf_jit_comp.c:922:\t\t\tret = __bpf_arch_text_poke(poke-\u003etailcall_target,\narch/x86/net/bpf_jit_comp.c-923-\t\t\t\t\t\t BPF_MOD_NOP, BPF_MOD_JUMP,\n--\narch/x86/net/bpf_jit_comp.c-927-\t\t\tBUG_ON(ret \u003c 0);\narch/x86/net/bpf_jit_comp.c:928:\t\t\tret = __bpf_arch_text_poke(poke-\u003etailcall_bypass,\narch/x86/net/bpf_jit_comp.c-929-\t\t\t\t\t\t BPF_MOD_JUMP, BPF_MOD_NOP,\n--\narch/x86/net/bpf_jit_comp.c=4263=void bpf_arch_poke_desc_update(struct bpf_jit_poke_descriptor *poke,\n--\narch/x86/net/bpf_jit_comp.c-4275-\t * On program loading or teardown, the program's kallsym entry\narch/x86/net/bpf_jit_comp.c:4276:\t * might not be in place, so we use __bpf_arch_text_poke to skip\narch/x86/net/bpf_jit_comp.c-4277-\t * the kallsyms check.\n--\narch/x86/net/bpf_jit_comp.c-4280-\t\tt = old_addr ? BPF_MOD_JUMP : BPF_MOD_NOP;\narch/x86/net/bpf_jit_comp.c:4281:\t\tret = __bpf_arch_text_poke(poke-\u003etailcall_target,\narch/x86/net/bpf_jit_comp.c-4282-\t\t\t\t\t t, BPF_MOD_JUMP,\n--\narch/x86/net/bpf_jit_comp.c-4285-\t\tif (!old) {\narch/x86/net/bpf_jit_comp.c:4286:\t\t\tret = __bpf_arch_text_poke(poke-\u003etailcall_bypass,\narch/x86/net/bpf_jit_comp.c-4287-\t\t\t\t\t\t BPF_MOD_JUMP, BPF_MOD_NOP,\n--\narch/x86/net/bpf_jit_comp.c-4293-\t\tt = old_bypass_addr ? BPF_MOD_JUMP : BPF_MOD_NOP;\narch/x86/net/bpf_jit_comp.c:4294:\t\tret = __bpf_arch_text_poke(poke-\u003etailcall_bypass,\narch/x86/net/bpf_jit_comp.c-4295-\t\t\t\t\t t, BPF_MOD_JUMP, old_bypass_addr,\n--\narch/x86/net/bpf_jit_comp.c-4304-\t\tt = old_addr ? BPF_MOD_JUMP : BPF_MOD_NOP;\narch/x86/net/bpf_jit_comp.c:4305:\t\tret = __bpf_arch_text_poke(poke-\u003etailcall_target,\narch/x86/net/bpf_jit_comp.c-4306-\t\t\t\t\t t, BPF_MOD_NOP, old_addr, NULL);\n"
} |
| 57/3 | 2026/09/12 10:09 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 32237 output: 44 thoughts: 129 Thoughts: **Analyzing the BPF Instruction Modification** Okay, so I'm looking at this `bpf_arch_text_poke` function call, specifically how it's being used with the `skip` structure. It's calling `bpf_arch_text_poke(skip->nop, BPF_MOD_NOP, BPF_MOD_JUMP, NULL, skip->target)`. Essentially, I need to understand what's happening when we're patching the instruction at `skip->nop` to change its behavior. It's currently a `BPF_MOD_NOP` and we're converting it to a `BPF_MOD_JUMP`. No previous address is involved, and we want to jump to `skip->target`. I need to dig into the implementation to understand the context and how the architecture handles this modification, so I'll go straight to `arch/x86/net/bpf_jit_comp.c`, and focus on lines 620-660 of `__bpf_arch_text_poke`. This is where the core logic of `__bpf_arch_text_poke` is implemented. I want to see how the x86 architecture specifically handles instruction patching. What's the code doing to safely and correctly alter the instruction stream? Are there any specific x86 instructions or techniques being used to perform this "poke"? Are there any considerations related to cache coherency or instruction alignment that I need to keep in mind? I need to understand the low-level details of how this jump is being implemented and if there are any specific aspects of this `BPF_MOD_JUMP` change that need further analysis. |
| 58/3 | 2026/09/12 10:09 | tool | read-file |
0mArgs:{
"File": "arch/x86/net/bpf_jit_comp.c",
"FirstLine": 620,
"LineCount": 45
}Results: {
"Contents": " 620:\t\n 621:\tstatic int __bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,\n 622:\t\t\t\t\tenum bpf_text_poke_type new_t,\n 623:\t\t\t\t\tvoid *old_addr, void *new_addr)\n 624:\t{\n 625:\t\tconst u8 *nop_insn = x86_nops[5];\n 626:\t\tu8 old_insn[X86_PATCH_SIZE];\n 627:\t\tu8 new_insn[X86_PATCH_SIZE];\n 628:\t\tu8 *prog;\n 629:\t\tint ret;\n 630:\t\n 631:\t\tmemcpy(old_insn, nop_insn, X86_PATCH_SIZE);\n 632:\t\tif (old_t != BPF_MOD_NOP \u0026\u0026 old_addr) {\n 633:\t\t\tprog = old_insn;\n 634:\t\t\tret = old_t == BPF_MOD_CALL ?\n 635:\t\t\t emit_call(\u0026prog, old_addr, ip) :\n 636:\t\t\t emit_jump(\u0026prog, old_addr, ip);\n 637:\t\t\tif (ret)\n 638:\t\t\t\treturn ret;\n 639:\t\t}\n 640:\t\n 641:\t\tmemcpy(new_insn, nop_insn, X86_PATCH_SIZE);\n 642:\t\tif (new_t != BPF_MOD_NOP \u0026\u0026 new_addr) {\n 643:\t\t\tprog = new_insn;\n 644:\t\t\tret = new_t == BPF_MOD_CALL ?\n 645:\t\t\t emit_call(\u0026prog, new_addr, ip) :\n 646:\t\t\t emit_jump(\u0026prog, new_addr, ip);\n 647:\t\t\tif (ret)\n 648:\t\t\t\treturn ret;\n 649:\t\t}\n 650:\t\n 651:\t\tret = -EBUSY;\n 652:\t\tmutex_lock(\u0026text_mutex);\n 653:\t\tif (memcmp(ip, old_insn, X86_PATCH_SIZE))\n 654:\t\t\tgoto out;\n 655:\t\tret = 1;\n 656:\t\tif (memcmp(ip, new_insn, X86_PATCH_SIZE)) {\n 657:\t\t\tsmp_text_poke_single(ip, new_insn, X86_PATCH_SIZE, NULL);\n 658:\t\t\tret = 0;\n 659:\t\t}\n 660:\tout:\n 661:\t\tmutex_unlock(\u0026text_mutex);\n 662:\t\treturn ret;\n 663:\t}\n 664:\t\n"
} |
| 59/3 | 2026/09/12 10:09 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 33504 output: 304 thoughts: 1131 Thoughts: **KMSAN vs. KASAN: A Detailed Assessment** Okay, let's break this down. Given my expertise, I need to be precise. The guidelines are clear: KMSAN is for catching *uninitialized* memory reads and kernel-to-user leaks. Standard KASAN, with its suite of debugging tools, already covers a lot: out-of-bounds accesses, use-after-free, double frees, invalid frees, locking issues with LOCKDEP, and various other memory corruption checks. We only bring in KMSAN when the code *specifically* introduces or exposes uninitialized memory risks that KASAN won't catch. That's the key. Now, analyzing the changes I'm looking at, the first question is whether we're sending kernel structures to user space. No. These changes are isolated to BPF trampoline and JIT compilation infrastructure. Then, I need to evaluate all of the core criteria. No uninitialized variables influence control flow. All memory allocations are zero-initialized. Nothing. Check. Next, I want to confirm that there is no risk of allocation of complex data structures with later uninitialized reads. Also, no. Finally, no existing buffers are being accessed in a way that could expose uninitialized data. The focus of *this* patch is about addressing a use-after-free race condition. The underlying bug lies in a detached BPF program potentially being freed while still in use from another program in the trampoline. The solution involves patching the NOP instruction before the freed program. Any problems would be with JIT, UAF, refcounting, locking, or out-of-bounds issues with the `im->skips` structure. *All* of these are already within the scope of KASAN, LOCKDEP, and standard kernel debugging. Therefore, I'm confident that these changes *do not* introduce any new uninitialized memory risks, and thus don't justify the overhead of a dedicated KMSAN fuzzing session. The reasoning is solid, and there's no justification to set `NeedsKMSAN` to true. Therefore, `NeedsKMSAN` should be `false`. Now, let's call `set-results`. |
| 60/3 | 2026/09/12 10:09 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series addresses a use-after-free (UAF) race condition in BPF trampolines where a task executing in a multi-prog trampoline could sleep (e.g. via a sleepable BPF program) while a subsequent attached program is detached and freed. When the sleeping task resumes, it would invoke the now-freed program. The fix emits NOPs before each invoked program call in the JIT trampoline images and patches them to jump instructions skipping the detached program.\n\nFrom a memory safety perspective:\n1. All newly introduced heap allocations (struct bpf_tramp_skip array via kzalloc_objs, struct bpf_tramp_image via kzalloc_obj) and stack allocations (struct bpf_tramp_image im = {} in arch_bpf_trampoline_size) are explicitly zero-initialized.\n2. No uninitialized memory is read, branched on, or exposed across user/kernel boundaries.\n3. The bug class targeted and any potential regressions introduced by this patch (use-after-free, refcounting issues on trampoline images, list corruption, or out-of-bounds accesses during text patching) fall squarely under the domain of KASAN, LOCKDEP, and standard kernel sanitizers, not KMSAN.\n\nTherefore, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series addresses a use-after-free (UAF) race condition in BPF trampolines where a task executing in a multi-prog trampoline could sleep (e.g. via a sleepable BPF program) while a subsequent attached program is detached and freed. When the sleeping task resumes, it would invoke the now-freed program. The fix emits NOPs before each invoked program call in the JIT trampoline images and patches them to jump instructions skipping the detached program.\n\nFrom a memory safety perspective:\n1. All newly introduced heap allocations (struct bpf_tramp_skip array via kzalloc_objs, struct bpf_tramp_image via kzalloc_obj) and stack allocations (struct bpf_tramp_image im = {} in arch_bpf_trampoline_size) are explicitly zero-initialized.\n2. No uninitialized memory is read, branched on, or exposed across user/kernel boundaries.\n3. The bug class targeted and any potential regressions introduced by this patch (use-after-free, refcounting issues on trampoline images, list corruption, or out-of-bounds accesses during text patching) fall squarely under the domain of KASAN, LOCKDEP, and standard kernel sanitizers, not KMSAN.\n\nTherefore, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|