| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/11/01 12:37 | flow | patch-triage |
4mArgs:null Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_check_timed_may_goto",
"bpf_timed_may_goto_save",
"bpf_timed_may_goto_restore",
"arch_bpf_timed_may_goto"
],
"KMSANReasoning": "The patch series refactors the BPF timed `may_goto` mechanism. Instead of allocating loop counters and timestamps on the BPF program's stack frame per invocation/subprogram, it uses per-CPU memory (`struct bpf_timed_may_goto __percpu *timed_may_goto`) allocated per program via `alloc_percpu_gfp()`. It updates the verifier fixups (`fixups.c`), the architecture JIT trampolines (arm64, loongarch, powerpc, riscv, s390, x86), and adds save/restore logic around asynchronous callbacks (`bpf_timer_cb`, `bpf_wq_work`, `bpf_task_work_callback`, `bpf_rcu_run_callback`).\n\nFrom a memory initialization perspective:\n1. The per-CPU `struct bpf_timed_may_goto` is zero-allocated by `alloc_percpu_gfp()` and explicitly initialized in the program prologue (`count`, `timestamp`, `owner`).\n2. The local stack variable `struct bpf_timed_may_goto saved` used in async callback helpers is guarded by `if (!prog-\u003eaux-\u003etimed_may_goto) return;`. When active, `*saved = *cur;` completely copies the 24-byte struct before any subsequent read in `bpf_timed_may_goto_restore()`. If `timed_may_goto` is NULL, neither function accesses `saved`.\n3. No kernel memory or structure padding is copied to user space or exposed via infoleaks.\n4. Any potential bugs in this patch series (such as register clobbering, incorrect JIT assembly generation, invalid pointer offsets, or use-after-free upon program teardown) are detectable by KASAN, assertions, or standard runtime checks.\n\nTherefore, this change does not introduce uninitialized memory risks that would require a dedicated KMSAN fuzzing session.",
"NeedsKMSAN": false,
"Reasoning": "The patch modifies the implementation of BPF timed may_goto loops. It switches state management from per-stack-frame storage to a dedicated percpu structure (struct bpf_timed_may_goto), refactors instruction expansion in the verifier fixup pass (may_goto_expand_timed), alters the register calling convention used when calling arch_bpf_timed_may_goto on x86_64, and adds save/restore logic for async callbacks (timers, workqueues, task_work, RCU). This code is reachable from user space on amd64 via standard bpf() syscall operations (BPF_PROG_LOAD and program execution with may_goto instructions), making it suitable and important for fuzzing.",
"WorthFuzzing": true
} |
| 1/1 | 2026/11/01 12:37 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 51ba9bdea0ef67afe96b0bfb46fdbcdf514999af\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Sun Nov 1 12:37:55 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/arch/arm64/net/bpf_timed_may_goto.S b/arch/arm64/net/bpf_timed_may_goto.S\nindex 1b4a4f91ffc90c..f8732b13bb1e58 100644\n--- a/arch/arm64/net/bpf_timed_may_goto.S\n+++ b/arch/arm64/net/bpf_timed_may_goto.S\n@@ -17,16 +17,10 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)\n \tstr\tx7, [sp, #80]\n \n \t/*\n-\t * Stack depth was passed in BPF_REG_AX (x9), add it to the BPF_FP\n-\t * (x25) to get the pointer to count and timestamp and pass it as the\n-\t * first argument in x0.\n-\t *\n-\t * Before generating the call to arch_bpf_timed_may_goto, the verifier\n-\t * generates a load instruction using FP, i.e. REG_AX = *(u64 *)(FP -\n-\t * stack_off_cnt), so BPF_REG_FP (x25) is always set up by the arm64\n-\t * jit in this case.\n+\t * The pointer to count and timestamp was passed in BPF_REG_AX (x9),\n+\t * pass it as the first argument in x0.\n \t */\n-\tadd\tx0, x9, x25\n+\tmov\tx0, x9\n \tbl\tbpf_check_timed_may_goto\n \t/* BPF_REG_AX(x9) will be stored into count, so move return value to it. */\n \tmov\tx9, x0\ndiff --git a/arch/loongarch/net/bpf_timed_may_goto.S b/arch/loongarch/net/bpf_timed_may_goto.S\nindex fa128acdb108a4..4aaca029404bcf 100644\n--- a/arch/loongarch/net/bpf_timed_may_goto.S\n+++ b/arch/loongarch/net/bpf_timed_may_goto.S\n@@ -22,14 +22,10 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)\n \tst.d\t\ta4, sp, 48\n \n \t/*\n-\t * BPF_REG_AX (t0) holds the offset passed in by the verifier;\n-\t * add it to BPF_REG_FP (s4) to get the pointer to the count and\n-\t * timestamp, then pass it as the first argument in a0.\n-\t *\n-\t * The verifier emits a load using FP right before this call,\n-\t * so BPF_REG_FP (s4) is always set up by the JIT in this case.\n+\t * BPF_REG_AX (t0) holds the pointer to the count and timestamp;\n+\t * pass it as the first argument in a0.\n \t */\n-\tadd.d\t\ta0, t0, s4\n+\tmove\t\ta0, t0\n \tbl\t\tbpf_check_timed_may_goto\n \t/* BPF_REG_AX (t0) will be stored into count, so move the return value to it. */\n \tmove\t\tt0, a0\ndiff --git a/arch/powerpc/net/bpf_timed_may_goto.S b/arch/powerpc/net/bpf_timed_may_goto.S\nindex 84ecf6fa7f5dcf..b8ae9885eb31cb 100644\n--- a/arch/powerpc/net/bpf_timed_may_goto.S\n+++ b/arch/powerpc/net/bpf_timed_may_goto.S\n@@ -8,7 +8,7 @@\n * arch_bpf_timed_may_goto() trampoline for powerpc64\n *\n * Custom BPF convention (verifier/JIT):\n- *\t- input: stack offset in BPF_REG_AX (r12)\n+ *\t- input: pointer to count and timestamp in BPF_REG_AX (r12)\n *\t- output: updated count in BPF_REG_AX (r12)\n *\n * Call bpf_check_timed_may_goto(ptr) with normal powerpc64 ABI:\n@@ -31,11 +31,8 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)\n \tstd r7, 64(r1)\n \tstd r8, 72(r1)\n \n-\t/*\n-\t * r3 = BPF_REG_FP + BPF_REG_AX\n-\t * BPF_REG_FP is r31; BPF_REG_AX is r12 (stack offset in bytes).\n-\t */\n-\tadd r3, r31, r12\n+\t/* r3 = BPF_REG_AX (r12) */\n+\tmr r3, r12\n \tbl CFUNC(bpf_check_timed_may_goto)\n \n \t/* Put return value back into AX */\ndiff --git a/arch/riscv/net/bpf_timed_may_goto.S b/arch/riscv/net/bpf_timed_may_goto.S\nindex 6ec37f634aff63..2ce1c89c913e55 100644\n--- a/arch/riscv/net/bpf_timed_may_goto.S\n+++ b/arch/riscv/net/bpf_timed_may_goto.S\n@@ -6,13 +6,11 @@\n \n /*\n * Trampoline for the BPF timed may_goto loop bound. Custom calling convention:\n- *\t- input: stack offset in BPF_REG_AX (t0)\n+ *\t- input: pointer to count and timestamp in BPF_REG_AX (t0)\n *\t- output: updated count in BPF_REG_AX (t0)\n *\n- * Calls bpf_check_timed_may_goto(ptr) with the standard RISC-V ABI, where\n- * ptr = BPF_REG_FP (s5) + BPF_REG_AX (t0). BPF R0-R5 (t6, a0-a4) and stack\n- * arguments 6-8 (a5-a7) are saved across the call; BPF_REG_FP (s5) is\n- * callee-saved and needs no saving.\n+ * Calls bpf_check_timed_may_goto(ptr) with the standard RISC-V ABI. BPF R0-R5\n+ * (t6, a0-a4) and stack arguments 6-8 (a5-a7) are saved across the call.\n */\n \n SYM_FUNC_START(arch_bpf_timed_may_goto)\n@@ -33,7 +31,7 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)\n \tREG_S\ta6, 7*SZREG(sp)\n \tREG_S\ta7, 8*SZREG(sp)\n \n-\tadd\ta0, t0, s5\n+\tmv\ta0, t0\n \tcall\tbpf_check_timed_may_goto\n \tmv\tt0, a0\n \ndiff --git a/arch/s390/net/bpf_jit_comp.c b/arch/s390/net/bpf_jit_comp.c\nindex 56ec49894dda38..15029cedc50baf 100644\n--- a/arch/s390/net/bpf_jit_comp.c\n+++ b/arch/s390/net/bpf_jit_comp.c\n@@ -1005,6 +1005,17 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,\n \t\t\t\t\t(jit-\u003eprg - patch_brc) \u003e\u003e 1;\n \t\t\tbreak;\n \t\t}\n+\t\tif (insn_is_mov_percpu_addr(insn)) {\n+\t\t\tconst u64 *percpu_offset = \u0026get_lowcore()-\u003epercpu_offset;\n+\n+\t\t\tif (dst_reg != src_reg)\n+\t\t\t\t/* lgr %dst,%src */\n+\t\t\t\tEMIT4(0xb9040000, dst_reg, src_reg);\n+\t\t\t/* ag %dst,percpu_offset */\n+\t\t\tEMIT6_DISP_LH(0xe3000000, 0x0008, dst_reg, REG_0, REG_0,\n+\t\t\t\t (unsigned long)percpu_offset);\n+\t\t\tbreak;\n+\t\t}\n \t\tswitch (insn-\u003eoff) {\n \t\tcase 0: /* DST = SRC */\n \t\t\t/* lgr %dst,%src */\n@@ -1866,9 +1877,9 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,\n \t\tif ((void *)func == arch_bpf_timed_may_goto) {\n \t\t\t/*\n \t\t\t * arch_bpf_timed_may_goto() has a special ABI: the\n-\t\t\t * parameters are in BPF_REG_AX and BPF_REG_10; the\n-\t\t\t * return value is in BPF_REG_AX; and all GPRs except\n-\t\t\t * REG_W0, REG_W1, and BPF_REG_AX are callee-saved.\n+\t\t\t * parameter is in BPF_REG_AX; the return value is in\n+\t\t\t * BPF_REG_AX; and all GPRs except REG_W0, REG_W1, and\n+\t\t\t * BPF_REG_AX are callee-saved.\n \t\t\t */\n \n \t\t\t/* brasl %r0,func */\n@@ -3116,6 +3127,11 @@ bool bpf_jit_supports_timed_may_goto(void)\n \treturn true;\n }\n \n+bool bpf_jit_supports_percpu_insn(void)\n+{\n+\treturn true;\n+}\n+\n bool bpf_jit_inlines_helper_call(s32 imm)\n {\n \tswitch (imm) {\ndiff --git a/arch/s390/net/bpf_timed_may_goto.S b/arch/s390/net/bpf_timed_may_goto.S\nindex 06f567a460d7bd..05f532f8c63903 100644\n--- a/arch/s390/net/bpf_timed_may_goto.S\n+++ b/arch/s390/net/bpf_timed_may_goto.S\n@@ -21,9 +21,9 @@\n \n SYM_FUNC_START(arch_bpf_timed_may_goto)\n \t/*\n-\t * This function has a special ABI: the parameters are in %r12 and\n-\t * %r13; the return value is in %r12; all GPRs except %r0, %r1, and\n-\t * %r12 are callee-saved; and the return address is in %r0.\n+\t * This function has a special ABI: the parameter is in %r12; the\n+\t * return value is in %r12; all GPRs except %r0, %r1, and %r12 are\n+\t * callee-saved; and the return address is in %r0.\n \t */\n \tstmg %r2,%r5,FRAME_OFF+R2_OFF(%r15)\n \tstg %r14,FRAME_OFF+R14_OFF(%r15)\n@@ -33,7 +33,7 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)\n \tlay %r15,-FRAME_SIZE(%r15)\n \tstg %r1,__SF_BACKCHAIN(%r15)\n \n-\tlay %r2,0(%r12,%r13)\n+\tlgr %r2,%r12\n \tbrasl %r14,bpf_check_timed_may_goto\n \tlgr %r12,%r2\n \ndiff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c\nindex d90b6d238b3a2e..38871963e67671 100644\n--- a/arch/x86/net/bpf_jit_comp.c\n+++ b/arch/x86/net/bpf_jit_comp.c\n@@ -3436,18 +3436,6 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *\n \t\t\t\tip += emit_kfunc_arg_moves(fm, outgoing_arg_base -\n \t\t\t\t\t\t\t outgoing_rsp, \u0026prog);\n \t\t\t}\n-\t\t\tif (func == (u8 *)arch_bpf_timed_may_goto) {\n-\t\t\t\tu32 fp = priv_frame_ptr ? X86_REG_R9 : BPF_REG_FP;\n-\n-\t\t\t\t/*\n-\t\t\t\t * AX has the offset of count and timestamp\n-\t\t\t\t * in the stack. Turn it into a pointer.\n-\t\t\t\t * add r10, rbp or add r10, r9\n-\t\t\t\t */\n-\t\t\t\tmaybe_emit_mod(\u0026prog, BPF_REG_AX, fp, true);\n-\t\t\t\tEMIT2(0x01, add_2reg(0xC0, BPF_REG_AX, fp));\n-\t\t\t\tip += 3;\n-\t\t\t}\n \t\t\tif (priv_frame_ptr) {\n \t\t\t\tpush_r9(\u0026prog);\n \t\t\t\tip += 2;\ndiff --git a/arch/x86/net/bpf_timed_may_goto.S b/arch/x86/net/bpf_timed_may_goto.S\nindex dd2dde89836b8f..da42de7cca9882 100644\n--- a/arch/x86/net/bpf_timed_may_goto.S\n+++ b/arch/x86/net/bpf_timed_may_goto.S\n@@ -25,10 +25,7 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)\n \t/* Save stack argument 6. */\n \tpushq %r9\n \n-\t/*\n-\t * r10 has the pointer to count and timestamp. JIT added the frame\n-\t * pointer to the offset. Pass it as first argument.\n-\t */\n+\t/* r10 has the pointer to count and timestamp. Pass it as first argument. */\n \tmovq %r10, %rdi\n \n \t/* Emit call depth accounting for call below. */\ndiff --git a/include/linux/bpf.h b/include/linux/bpf.h\nindex ff043678129358..01d5c79b487e2e 100644\n--- a/include/linux/bpf.h\n+++ b/include/linux/bpf.h\n@@ -1916,6 +1916,8 @@ struct bpf_prog_aux {\n \tconst char *attach_func_name;\n \tstruct bpf_prog **func;\n \tstruct bpf_prog_aux *main_prog_aux;\n+\t/* main prog: there when the prog has timed may_goto */\n+\tstruct bpf_timed_may_goto __percpu *timed_may_goto;\n \tvoid *jit_data; /* JIT specific data. arch dependent */\n \tstruct bpf_jit_poke_descriptor *poke_tab;\n \tstruct bpf_kfunc_desc_tab *kfunc_tab;\ndiff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h\nindex 7baeb831281f38..12ec5b14007fbf 100644\n--- a/include/linux/bpf_verifier.h\n+++ b/include/linux/bpf_verifier.h\n@@ -964,6 +964,10 @@ struct bpf_subprog_info {\n \tbool is_cb: 1;\n \tbool is_async_cb: 1;\n \tbool is_exception_cb: 1;\n+\t/* bpf_loop() or bpf_for_each_map_elem() call it in a loop */\n+\tbool is_loop_cb: 1;\n+\t/* BPF code, another helper or a kfunc call it */\n+\tbool is_called: 1;\n \tbool args_cached: 1;\n \t/* true if the return value is passed in the R0:R2 register pair */\n \tbool ret_reg_pair: 1;\ndiff --git a/include/linux/filter.h b/include/linux/filter.h\nindex 98254742ae37a2..4f0cd67caccb80 100644\n--- a/include/linux/filter.h\n+++ b/include/linux/filter.h\n@@ -758,9 +758,15 @@ struct bpf_prog_stats {\n \tstruct u64_stats_sync syncp;\n } __aligned(2 * sizeof(u64));\n \n+/*\n+ * Per-cpu memory of a prog. All may_goto of one invocation of the prog use it. An invocation is\n+ * one call from the kernel into the main prog, with the subprogs and sync callbacks it reaches.\n+ */\n struct bpf_timed_may_goto {\n \tu64 count;\n \tu64 timestamp;\n+\t/* the async callback that has set count and timestamp, see bpf_timed_may_goto_save() */\n+\tvoid *owner;\n };\n \n struct sk_filter {\n@@ -1328,6 +1334,8 @@ u64 bpf_arch_uaddress_limit(void);\n void arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie);\n u64 arch_bpf_timed_may_goto(void);\n u64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *);\n+void bpf_timed_may_goto_save(const struct bpf_prog *prog, struct bpf_timed_may_goto *saved);\n+void bpf_timed_may_goto_restore(const struct bpf_prog *prog, struct bpf_timed_may_goto *saved);\n bool bpf_helper_changes_pkt_data(enum bpf_func_id func_id);\n \n static inline bool bpf_dump_raw_ok(const struct cred *cred)\ndiff --git a/kernel/bpf/core.c b/kernel/bpf/core.c\nindex 7cf37690d41fa4..3b02aafba4c549 100644\n--- a/kernel/bpf/core.c\n+++ b/kernel/bpf/core.c\n@@ -292,6 +292,7 @@ void __bpf_prog_free(struct bpf_prog *fp)\n \t\tmutex_destroy(\u0026fp-\u003eaux-\u003eused_maps_mutex);\n \t\tmutex_destroy(\u0026fp-\u003eaux-\u003edst_mutex);\n \t\tkfree(fp-\u003eaux-\u003epoke_tab);\n+\t\tfree_percpu(fp-\u003eaux-\u003etimed_may_goto);\n \t\tkfree(fp-\u003eaux);\n \t}\n \tfree_percpu(fp-\u003estats);\n@@ -3643,6 +3644,38 @@ u64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *p)\n \treturn BPF_MAX_TIMED_LOOPS;\n }\n \n+/*\n+ * The main prog sets count and timestamp in its first insns. A call of an async callback is an\n+ * invocation of its own too, but it can interrupt an invocation of the prog on this cpu, or go on\n+ * next to one that is preempted. So its caller sets count and timestamp, and puts the old ones\n+ * back at the end. Not when another invocation has set them in the meantime: they are for that\n+ * one now.\n+ * The caller has disabled migration.\n+ */\n+void bpf_timed_may_goto_save(const struct bpf_prog *prog, struct bpf_timed_may_goto *saved)\n+{\n+\tstruct bpf_timed_may_goto *cur;\n+\n+\tif (!prog-\u003eaux-\u003etimed_may_goto)\n+\t\treturn;\n+\tcur = this_cpu_ptr(prog-\u003eaux-\u003etimed_may_goto);\n+\t*saved = *cur;\n+\tcur-\u003ecount = BPF_MAX_TIMED_LOOPS;\n+\tcur-\u003etimestamp = 0;\n+\tcur-\u003eowner = saved;\n+}\n+\n+void bpf_timed_may_goto_restore(const struct bpf_prog *prog, struct bpf_timed_may_goto *saved)\n+{\n+\tstruct bpf_timed_may_goto *cur;\n+\n+\tif (!prog-\u003eaux-\u003etimed_may_goto)\n+\t\treturn;\n+\tcur = this_cpu_ptr(prog-\u003eaux-\u003etimed_may_goto);\n+\tif (cur-\u003eowner == saved)\n+\t\t*cur = *saved;\n+}\n+\n /* for configs without MMU or 32-bit */\n __weak const struct bpf_map_ops arena_map_ops;\n __weak u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena)\ndiff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c\nindex 41bc37e205e0f2..a656634fa9e468 100644\n--- a/kernel/bpf/fixups.c\n+++ b/kernel/bpf/fixups.c\n@@ -1803,6 +1803,50 @@ static int may_goto_expand(struct bpf_insn *insn_buf, int off, int stack_off,\n \treturn cnt + tail_cnt;\n }\n \n+/*\n+ * Timed may_goto. All may_goto of a prog use one count and one timestamp: per-cpu memory of the\n+ * prog, set in the first insns of the main prog, and by the callers of async callbacks.\n+ * @reg holds the address. It is a register that is dead at the may_goto, or R0 that is saved at\n+ * @save_off in the stack.\n+ */\n+static int may_goto_expand_timed(struct bpf_insn *insn_buf, int off,\n+\t\t\t\t struct bpf_timed_may_goto __percpu *tmg, int reg, int save_off)\n+{\n+\tstruct bpf_insn ld_tmg[] = { BPF_LD_IMM64(reg, (__force unsigned long)tmg) };\n+\tint cnt = 0, jmp, tail_cnt = save_off ? 6 : 5;\n+\n+\tif (save_off)\n+\t\tinsn_buf[cnt++] = BPF_STX_MEM(BPF_DW, BPF_REG_10, reg, save_off);\n+\tinsn_buf[cnt++] = ld_tmg[0];\n+\tinsn_buf[cnt++] = ld_tmg[1];\n+\tinsn_buf[cnt++] = BPF_MOV64_PERCPU_REG(reg, reg);\n+\tinsn_buf[cnt++] = BPF_LDX_MEM(BPF_DW, BPF_REG_AX, reg, 0);\n+\tif (save_off) {\n+\t\tinsn_buf[cnt++] = BPF_JMP_IMM(BPF_JNE, BPF_REG_AX, 0, 2);\n+\t\tinsn_buf[cnt++] = BPF_LDX_MEM(BPF_DW, reg, BPF_REG_10, save_off);\n+\t\tjmp = cnt++;\n+\t\tinsn_buf[jmp] = BPF_JMP32_A(off \u003e= 0 ? off + tail_cnt : off - jmp);\n+\t} else {\n+\t\tjmp = cnt;\n+\t\toff = off \u003e= 0 ? off + tail_cnt : off - jmp;\n+\t\tif (off == (s16)off) {\n+\t\t\tinsn_buf[cnt++] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_AX, 0, off);\n+\t\t} else {\n+\t\t\tinsn_buf[cnt++] = BPF_JMP_IMM(BPF_JNE, BPF_REG_AX, 0, 1);\n+\t\t\tinsn_buf[cnt++] = BPF_JMP32_A(off \u003e= 0 ? off : off - 1);\n+\t\t}\n+\t}\n+\tinsn_buf[cnt++] = BPF_ALU64_IMM(BPF_SUB, BPF_REG_AX, 1);\n+\tinsn_buf[cnt++] = BPF_JMP_IMM(BPF_JNE, BPF_REG_AX, 0, 2);\n+\t/* AX is the argument and the return value */\n+\tinsn_buf[cnt++] = BPF_MOV64_REG(BPF_REG_AX, reg);\n+\tinsn_buf[cnt++] = BPF_EMIT_CALL(arch_bpf_timed_may_goto);\n+\tinsn_buf[cnt++] = BPF_STX_MEM(BPF_DW, reg, BPF_REG_AX, 0);\n+\tif (save_off)\n+\t\tinsn_buf[cnt++] = BPF_LDX_MEM(BPF_DW, reg, BPF_REG_10, save_off);\n+\treturn cnt;\n+}\n+\n /* Do various post-verification rewrites in a single program pass.\n * These rewrites simplify JIT and interpreter implementations.\n */\n@@ -1823,6 +1867,9 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)\n \tstruct bpf_subprog_info *subprogs = env-\u003esubprog_info;\n \tu16 stack_depth = subprogs[cur_subprog].stack_depth;\n \tu16 stack_depth_extra = 0;\n+\t/* arch_bpf_timed_may_goto() takes and returns AX in a cpu register: for JITs only */\n+\tbool timed_may_goto = prog-\u003ejit_requested \u0026\u0026 bpf_jit_supports_timed_may_goto();\n+\tstruct bpf_timed_may_goto __percpu *tmg = NULL;\n \n \tif (env-\u003eseen_exception \u0026\u0026 !env-\u003eexception_callback_subprog) {\n \t\tstruct bpf_insn *patch = insn_buf;\n@@ -2078,38 +2125,28 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)\n \t\t\tgoto next_insn;\n \t\t}\n \n-\t\tif (bpf_is_may_goto_insn(insn) \u0026\u0026 bpf_jit_supports_timed_may_goto()) {\n-\t\t\tint stack_off_cnt = -stack_depth - 16;\n-\t\t\t/*\n-\t\t\t * AX is used as an argument to pass in stack_off_cnt\n-\t\t\t * (to add to r10/fp), and also as the return value of\n-\t\t\t * the call to arch_bpf_timed_may_goto.\n-\t\t\t */\n-\t\t\tstruct bpf_insn tail[] = {\n-\t\t\t\tBPF_ALU64_IMM(BPF_SUB, BPF_REG_AX, 1),\n-\t\t\t\tBPF_JMP_IMM(BPF_JNE, BPF_REG_AX, 0, 2),\n-\t\t\t\tBPF_MOV64_IMM(BPF_REG_AX, stack_off_cnt),\n-\t\t\t\tBPF_EMIT_CALL(arch_bpf_timed_may_goto),\n-\t\t\t\tBPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_AX, stack_off_cnt),\n-\t\t\t};\n+\t\tif (bpf_is_may_goto_insn(insn) \u0026\u0026 timed_may_goto) {\n+\t\t\tu16 live = env-\u003einsn_aux_data[i + delta].live_regs_before;\n+\t\t\tint reg, save_off = 0;\n \n-\t\t\t/*\n-\t\t\t * Two 8 byte slots, depth-16 stores the count, and\n-\t\t\t * depth-8 stores the start timestamp of the loop.\n-\t\t\t *\n-\t\t\t * The starting value of count is BPF_MAX_TIMED_LOOPS\n-\t\t\t * (0xffff). Every iteration loads it and subs it by 1,\n-\t\t\t * until the value becomes 0 in AX (thus, 1 in stack),\n-\t\t\t * after which we call arch_bpf_timed_may_goto, which\n-\t\t\t * either sets AX to 0xffff to keep looping, or to 0\n-\t\t\t * upon timeout. AX is then stored into the stack. In\n-\t\t\t * the next iteration, we either see 0 and break out, or\n-\t\t\t * continue iterating until the next time value is 0\n-\t\t\t * after subtraction, rinse and repeat.\n-\t\t\t */\n-\t\t\tstack_depth_extra = 16;\n-\t\t\tcnt = may_goto_expand(insn_buf, insn-\u003eoff, stack_off_cnt,\n-\t\t\t\t\t tail, ARRAY_SIZE(tail));\n+\t\t\t/* a register that is dead here holds the address of the count */\n+\t\t\tfor (reg = BPF_REG_0; reg \u003c= BPF_REG_9; reg++)\n+\t\t\t\tif (!(live \u0026 BIT(reg)))\n+\t\t\t\t\tbreak;\n+\t\t\tif (reg \u003e BPF_REG_9) {\n+\t\t\t\treg = BPF_REG_0;\n+\t\t\t\tsave_off = -stack_depth - 8;\n+\t\t\t\tstack_depth_extra = 8;\n+\t\t\t}\n+\t\t\tif (!tmg) {\n+\t\t\t\ttmg = alloc_percpu_gfp(struct bpf_timed_may_goto,\n+\t\t\t\t\t\t GFP_KERNEL_ACCOUNT);\n+\t\t\t\tif (!tmg)\n+\t\t\t\t\treturn -ENOMEM;\n+\t\t\t\tprog-\u003eaux-\u003etimed_may_goto = tmg;\n+\t\t\t}\n+\t\t\tprog-\u003ejit_required = 1;\n+\t\t\tcnt = may_goto_expand_timed(insn_buf, insn-\u003eoff, tmg, reg, save_off);\n \n \t\t\tnew_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);\n \t\t\tif (!new_prog)\n@@ -2730,24 +2767,52 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)\n \n \tenv-\u003eprog-\u003eaux-\u003estack_depth = subprogs[0].stack_depth;\n \tfor (i = 0; i \u003c env-\u003esubprog_cnt; i++) {\n-\t\tint delta = bpf_jit_supports_timed_may_goto() ? 2 : 1;\n \t\tint subprog_start = subprogs[i].start;\n \t\tint stack_slots = subprogs[i].stack_extra / 8;\n-\t\tint slots = delta, cnt = 0;\n-\n-\t\tif (!stack_slots)\n+\t\t/* an invocation begins, see bpf_timed_may_goto_save() for async callbacks */\n+\t\tbool main_prog = tmg \u0026\u0026 !i;\n+\t\t/* only a helper calls it, in a loop that ends when it returns 1 */\n+\t\tbool loop_cb = tmg \u0026\u0026 i \u0026\u0026 subprogs[i].is_loop_cb \u0026\u0026 !subprogs[i].is_called \u0026\u0026\n+\t\t\t !subprogs[i].is_async_cb;\n+\t\tint cnt = 0;\n+\n+\t\tif (timed_may_goto ? !main_prog \u0026\u0026 !loop_cb : !stack_slots)\n \t\t\tcontinue;\n-\t\t/* We need two slots in case timed may_goto is supported. */\n-\t\tif (stack_slots \u003e slots) {\n+\t\tif (stack_slots \u003e 1) {\n \t\t\tverifier_bug(env, \"stack_slots supports may_goto only\");\n \t\t\treturn -EFAULT;\n \t\t}\n \n \t\tstack_depth = subprogs[i].stack_depth;\n-\t\tif (bpf_jit_supports_timed_may_goto()) {\n-\t\t\tinsn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, -stack_depth,\n-\t\t\t\t\t\t BPF_MAX_TIMED_LOOPS);\n-\t\t\tinsn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, -stack_depth + 8, 0);\n+\t\tif (timed_may_goto) {\n+\t\t\tstruct bpf_insn ld_tmg[] = {\n+\t\t\t\tBPF_LD_IMM64(BPF_REG_0, (__force unsigned long)tmg)\n+\t\t\t};\n+\t\t\tint count_off = offsetof(struct bpf_timed_may_goto, count);\n+\t\t\tint timestamp_off = offsetof(struct bpf_timed_may_goto, timestamp);\n+\t\t\tint owner_off = offsetof(struct bpf_timed_may_goto, owner);\n+\n+\t\t\t/* R0 has nothing at the entry */\n+\t\t\tinsn_buf[cnt++] = ld_tmg[0];\n+\t\t\tinsn_buf[cnt++] = ld_tmg[1];\n+\t\t\tinsn_buf[cnt++] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);\n+\t\t\tif (main_prog) {\n+\t\t\t\tinsn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_0, count_off,\n+\t\t\t\t\t\t\t BPF_MAX_TIMED_LOOPS);\n+\t\t\t\tinsn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_0, timestamp_off, 0);\n+\t\t\t\tinsn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_0, owner_off, 0);\n+\t\t\t} else {\n+\t\t\t\t/*\n+\t\t\t\t * The time is up, but the helper would call the function again and\n+\t\t\t\t * again. End its loop. The verifier has seen that: the loop can end\n+\t\t\t\t * after each call.\n+\t\t\t\t */\n+\t\t\t\tinsn_buf[cnt++] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0,\n+\t\t\t\t\t\t\t count_off);\n+\t\t\t\tinsn_buf[cnt++] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2);\n+\t\t\t\tinsn_buf[cnt++] = BPF_MOV64_IMM(BPF_REG_0, 1);\n+\t\t\t\tinsn_buf[cnt++] = BPF_EXIT_INSN();\n+\t\t\t}\n \t\t} else {\n \t\t\t/* Add ST insn to subprog prologue to init extra stack */\n \t\t\tinsn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, -stack_depth,\n@@ -2774,7 +2839,7 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)\n \t\t * to insn after BPF_ST that inits may_goto count.\n \t\t * Adjustment will succeed because bpf_patch_insn_data() didn't fail.\n \t\t */\n-\t\tWARN_ON(adjust_jmp_off(env-\u003eprog, subprog_start, delta));\n+\t\tWARN_ON(adjust_jmp_off(env-\u003eprog, subprog_start, cnt - 1));\n \t}\n \n \t/* Since poke tab is now finalized, publish aux to tracker. */\ndiff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c\nindex 58e357302850e3..2b75f7ba07a566 100644\n--- a/kernel/bpf/helpers.c\n+++ b/kernel/bpf/helpers.c\n@@ -1222,6 +1222,8 @@ static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)\n {\n \tstruct bpf_hrtimer *t = container_of(hrtimer, struct bpf_hrtimer, timer);\n \tstruct bpf_map *map = READ_ONCE(t-\u003ecb.map);\n+\tstruct bpf_prog *prog = READ_ONCE(t-\u003ecb.prog);\n+\tstruct bpf_timed_may_goto saved;\n \tvoid *value = t-\u003ecb.value;\n \tbpf_callback_t callback_fn;\n \tvoid *key;\n@@ -1229,7 +1231,7 @@ static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)\n \n \tBTF_TYPE_EMIT(struct bpf_timer);\n \tcallback_fn = rcu_dereference_check(t-\u003ecb.callback_fn, rcu_read_lock_bh_held());\n-\tif (!callback_fn || !map)\n+\tif (!callback_fn || !map || !prog)\n \t\tgoto out;\n \n \t/* bpf_timer_cb() runs in hrtimer_run_softirq. It doesn't migrate and\n@@ -1242,8 +1244,10 @@ static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)\n \n \tkey = map_key_from_value(map, value, \u0026idx);\n \n+\tbpf_timed_may_goto_save(prog, \u0026saved);\n \tcallback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);\n \t/* The verifier checked that return value is zero. */\n+\tbpf_timed_may_goto_restore(prog, \u0026saved);\n \n \tthis_cpu_write(hrtimer_running, NULL);\n out:\n@@ -1254,6 +1258,8 @@ static void bpf_wq_work(struct work_struct *work)\n {\n \tstruct bpf_work *w = container_of(work, struct bpf_work, work);\n \tstruct bpf_async_cb *cb = \u0026w-\u003ecb;\n+\tstruct bpf_timed_may_goto saved;\n+\tstruct bpf_prog *prog;\n \tstruct bpf_map *map;\n \tbpf_callback_t callback_fn;\n \tvoid *value = cb-\u003evalue;\n@@ -1267,14 +1273,17 @@ static void bpf_wq_work(struct work_struct *work)\n \n \tmap = READ_ONCE(cb-\u003emap);\n \tcallback_fn = READ_ONCE(cb-\u003ecallback_fn);\n-\tif (!callback_fn || !map)\n+\tprog = READ_ONCE(cb-\u003eprog);\n+\tif (!callback_fn || !map || !prog)\n \t\tgoto out;\n \n \tkey = map_key_from_value(map, value, \u0026idx);\n \n \tmigrate_disable();\n \n+\tbpf_timed_may_goto_save(prog, \u0026saved);\n \tcallback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);\n+\tbpf_timed_may_goto_restore(prog, \u0026saved);\n \n \tmigrate_enable();\n out:\n@@ -4593,6 +4602,7 @@ static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx,\n static void bpf_task_work_callback(struct callback_head *cb)\n {\n \tstruct bpf_task_work_ctx *ctx = container_of(cb, struct bpf_task_work_ctx, work);\n+\tstruct bpf_timed_may_goto saved;\n \tenum bpf_task_work_state state;\n \tu32 idx;\n \tvoid *key;\n@@ -4614,8 +4624,10 @@ static void bpf_task_work_callback(struct callback_head *cb)\n \tkey = (void *)map_key_from_value(ctx-\u003emap, ctx-\u003emap_val, \u0026idx);\n \n \tmigrate_disable();\n+\tbpf_timed_may_goto_save(ctx-\u003eprog, \u0026saved);\n \tctx-\u003ecallback_fn((u64)(long)ctx-\u003emap, (u64)(long)key,\n \t\t\t (u64)(long)ctx-\u003emap_val, 0, 0);\n+\tbpf_timed_may_goto_restore(ctx-\u003eprog, \u0026saved);\n \tmigrate_enable();\n \n \tbpf_task_work_ctx_reset(ctx);\n@@ -4855,6 +4867,7 @@ static void bpf_rcu_run_callback(struct rcu_head *rcu)\n \tstruct bpf_rcu_head_kern *rh = container_of(rcu, struct bpf_rcu_head_kern, rcu);\n \tbpf_callback_t callback_fn = rh-\u003ecallback_fn;\n \tstruct bpf_prog *prog = rh-\u003eprog;\n+\tstruct bpf_timed_may_goto saved;\n \tstruct bpf_map *map = rh-\u003emap;\n \tvoid *value, *key;\n \tu32 idx;\n@@ -4866,7 +4879,9 @@ static void bpf_rcu_run_callback(struct rcu_head *rcu)\n \tsmp_store_release(\u0026rh-\u003earmed, 0);\n \n \trcu_read_lock_dont_migrate();\n+\tbpf_timed_may_goto_save(prog, \u0026saved);\n \tcallback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);\n+\tbpf_timed_may_goto_restore(prog, \u0026saved);\n \trcu_read_unlock_migrate();\n \n \tbpf_prog_put(prog);\ndiff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c\nindex 26a5ee0708a3d3..383cdc11b5b45f 100644\n--- a/kernel/bpf/verifier.c\n+++ b/kernel/bpf/verifier.c\n@@ -11469,6 +11469,12 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins\n \tif (err)\n \t\treturn err;\n \n+\tif (!bpf_pseudo_kfunc_call(insn) \u0026\u0026\n+\t (insn-\u003eimm == BPF_FUNC_loop || insn-\u003eimm == BPF_FUNC_for_each_map_elem))\n+\t\tenv-\u003esubprog_info[subprog].is_loop_cb = true;\n+\telse\n+\t\tenv-\u003esubprog_info[subprog].is_called = true;\n+\n \t/* for callback functions enqueue entry to callback and\n \t * proceed with next instruction within current frame.\n \t */\n@@ -11505,6 +11511,7 @@ static int check_static_func_call(struct bpf_verifier_env *env, int subprog,\n \tint err;\n \n \tcaller = state-\u003eframe[state-\u003ecurframe];\n+\tenv-\u003esubprog_info[subprog].is_called = true;\n \n \t/*\n \t * Track caller's total stack arg count (incoming + max outgoing).\ndiff --git a/tools/testing/selftests/bpf/libarena/selftests/test_parallel_bitmap.bpf.c b/tools/testing/selftests/bpf/libarena/selftests/test_parallel_bitmap.bpf.c\nindex ea1fac95b461e5..017add229befe7 100644\n--- a/tools/testing/selftests/bpf/libarena/selftests/test_parallel_bitmap.bpf.c\n+++ b/tools/testing/selftests/bpf/libarena/selftests/test_parallel_bitmap.bpf.c\n@@ -11,6 +11,7 @@\n #define TEST_BITMAP_BITS\t(2 * BITS_PER_LONG_LONG)\n #define TEST_BITMAP_SYNC_SPINS\tBPF_MAX_LOOPS\n #define TEST_BITMAP_ITERS\t10 * 1000 * 1000\n+#define TEST_BITMAP_NSEC\t(50 * 1000 * 1000)\n \n static struct arena_bitmap __arena *bitmap;\n static volatile u64 started;\n@@ -129,6 +130,7 @@ static int bitmap_test_common_single(int parity)\n \n static int bitmap_test_common(int parity)\n {\n+\tu64 start;\n \tint ret;\n \tu32 i;\n \n@@ -138,6 +140,7 @@ static int bitmap_test_common(int parity)\n \tif (ret)\n \t\treturn ret;\n \n+\tstart = bpf_ktime_get_ns();\n \tfor (i = zero; i \u003c TEST_BITMAP_ITERS \u0026\u0026 can_loop; i++) {\n \t\tret = bitmap_test_common_single(parity);\n \t\tif (ret)\n@@ -147,6 +150,13 @@ static int bitmap_test_common(int parity)\n \t\t\tbreak;\n \n \t\tbitmap_test_reset_single(parity);\n+\n+\t\t/*\n+\t\t * Stop before the time slice of may_goto is used up. After that the\n+\t\t * bmp_*() functions can't retry a cmpxchg that fails.\n+\t\t */\n+\t\tif (bpf_ktime_get_ns() - start \u003e TEST_BITMAP_NSEC)\n+\t\t\tbreak;\n \t}\n \n \treturn 0;\ndiff --git a/tools/testing/selftests/bpf/prog_tests/may_goto_far.c b/tools/testing/selftests/bpf/prog_tests/may_goto_far.c\nindex 8d0ff30bd068cb..b6d262911a3ea3 100644\n--- a/tools/testing/selftests/bpf/prog_tests/may_goto_far.c\n+++ b/tools/testing/selftests/bpf/prog_tests/may_goto_far.c\n@@ -34,8 +34,10 @@ static struct bpf_insn *fill(struct bpf_insn *insn, int cnt, int reg)\n * The prog returns 0 when may_goto jumps to its target and 2 when\n * it lands in one of 'r0 = r8' areas. There are no constants between\n * may_goto and its target to keep the distance when constants are blinded.\n+ *\n+ * With 'subprog' all of that is a subprog.\n */\n-static void test_far(int off)\n+static void test_far(int off, bool subprog)\n {\n \tLIBBPF_OPTS(bpf_prog_load_opts, opts, .prog_flags = BPF_F_SLEEPABLE);\n \tLIBBPF_OPTS(bpf_test_run_opts, topts);\n@@ -50,6 +52,10 @@ static void test_far(int off)\n \t\treturn;\n \n \tinsn = insns;\n+\tif (subprog) {\n+\t\t*insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0, 1);\n+\t\t*insn++ = BPF_EXIT_INSN();\n+\t}\n \t*insn++ = BPF_EMIT_CALL(BPF_FUNC_ktime_get_ns);\n \t*insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_0);\n \t*insn++ = BPF_ALU64_IMM(BPF_RSH, BPF_REG_6, 63);\n@@ -82,11 +88,19 @@ static void test_far(int off)\n void test_may_goto_far(void)\n {\n \tif (test__start_subtest(\"32762\"))\n-\t\ttest_far(32762);\n+\t\ttest_far(32762, false);\n \tif (test__start_subtest(\"32767\"))\n-\t\ttest_far(32767);\n+\t\ttest_far(32767, false);\n \tif (test__start_subtest(\"-32767\"))\n-\t\ttest_far(-32767);\n+\t\ttest_far(-32767, false);\n \tif (test__start_subtest(\"-32768\"))\n-\t\ttest_far(-32768);\n+\t\ttest_far(-32768, false);\n+\tif (test__start_subtest(\"subprog 1\"))\n+\t\ttest_far(1, true);\n+\tif (test__start_subtest(\"subprog 32767\"))\n+\t\ttest_far(32767, true);\n+\tif (test__start_subtest(\"subprog -6\"))\n+\t\ttest_far(-6, true);\n+\tif (test__start_subtest(\"subprog -32768\"))\n+\t\ttest_far(-32768, true);\n }\ndiff --git a/tools/testing/selftests/bpf/prog_tests/timed_may_goto.c b/tools/testing/selftests/bpf/prog_tests/timed_may_goto.c\nnew file mode 100644\nindex 00000000000000..3da3f33a2898b4\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/prog_tests/timed_may_goto.c\n@@ -0,0 +1,195 @@\n+// SPDX-License-Identifier: GPL-2.0\n+#define _GNU_SOURCE\n+#include \u003csched.h\u003e\n+#include \u003ctest_progs.h\u003e\n+#include \u003cnetwork_helpers.h\u003e\n+#include \"timed_may_goto.skel.h\"\n+#include \"timed_may_goto_user.skel.h\"\n+#include \"timed_may_goto_ext.skel.h\"\n+\n+#define TRIES 4\n+\n+static int run_tc(struct bpf_program *prog)\n+{\n+\tLIBBPF_OPTS(bpf_test_run_opts, opts,\n+\t\t.data_in = \u0026pkt_v4,\n+\t\t.data_size_in = sizeof(pkt_v4),\n+\t\t.repeat = 1,\n+\t);\n+\n+\tif (!ASSERT_OK(bpf_prog_test_run_opts(bpf_program__fd(prog), \u0026opts), \"test_run\"))\n+\t\treturn -1;\n+\treturn opts.retval;\n+}\n+\n+static int run_syscall(struct bpf_program *prog)\n+{\n+\tLIBBPF_OPTS(bpf_test_run_opts, opts);\n+\n+\tif (!ASSERT_OK(bpf_prog_test_run_opts(bpf_program__fd(prog), \u0026opts), \"test_run\"))\n+\t\treturn -1;\n+\treturn opts.retval;\n+}\n+\n+static void wait_for(int *val, int expected, const char *name)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c 500 \u0026\u0026 *(volatile int *)val != expected; i++)\n+\t\tusleep(10000);\n+\tASSERT_EQ(*val, expected, name);\n+}\n+\n+static void test_async(struct timed_may_goto_user *skel)\n+{\n+\tASSERT_EQ(run_syscall(skel-\u003eprogs.async_after_timeout), 0, \"async_after_timeout\");\n+\twait_for(\u0026skel-\u003ebss-\u003etimer_cnt, 1000, \"timer_cnt\");\n+\twait_for(\u0026skel-\u003ebss-\u003ewq_cnt, 1000, \"wq_cnt\");\n+\twait_for(\u0026skel-\u003ebss-\u003etw_cnt, 1000, \"tw_cnt\");\n+\twait_for(\u0026skel-\u003ebss-\u003ercu_cnt, 1000, \"rcu_cnt\");\n+\twait_for(\u0026skel-\u003ebss-\u003ercu_trace_cnt, 1000, \"rcu_trace_cnt\");\n+}\n+\n+static void test_timer_on_top(struct timed_may_goto_user *skel)\n+{\n+\tint ms = run_syscall(skel-\u003eprogs.spin_under_timer);\n+\tint ticks = skel-\u003ebss-\u003eticks;\n+\n+\t/* stop the timer */\n+\tskel-\u003ebss-\u003eticks = 100;\n+\tASSERT_GE(ms, 200, \"ms\");\n+\tASSERT_LE(ms, 500, \"ms\");\n+\t/* control: the callback interrupted the prog, and its loop went on to the end */\n+\tASSERT_GE(ticks, 5, \"ticks\");\n+\tASSERT_LT(ticks, 100, \"ticks\");\n+}\n+\n+static void test_freplace(struct timed_may_goto_user *skel)\n+{\n+\tstruct timed_may_goto_ext *ext;\n+\n+\tASSERT_EQ(run_tc(skel-\u003eprogs.freplace_target), 1, \"not replaced\");\n+\n+\text = timed_may_goto_ext__open();\n+\tif (!ASSERT_OK_PTR(ext, \"ext_open\"))\n+\t\treturn;\n+\tbpf_program__set_attach_target(ext-\u003eprogs.replacement,\n+\t\t\t\t bpf_program__fd(skel-\u003eprogs.freplace_target),\n+\t\t\t\t \"replaced_global\");\n+\tif (!ASSERT_OK(timed_may_goto_ext__load(ext), \"ext_load\") ||\n+\t !ASSERT_OK(timed_may_goto_ext__attach(ext), \"ext_attach\"))\n+\t\tgoto out;\n+\tASSERT_EQ(run_tc(skel-\u003eprogs.freplace_target), TRIES, \"replaced\");\n+out:\n+\ttimed_may_goto_ext__destroy(ext);\n+\tASSERT_EQ(run_tc(skel-\u003eprogs.freplace_target), 1, \"not replaced\");\n+}\n+\n+/* What a prog leaves behind when its time is up doesn't matter for the next one on the cpu. */\n+static void test_tail_call_and_throw(struct timed_may_goto_user *skel)\n+{\n+\tint key = 0, fd = bpf_program__fd(skel-\u003eprogs.tail_callee);\n+\tint map_fd = bpf_map__fd(skel-\u003emaps.jmp_table);\n+\n+\tif (!ASSERT_OK(bpf_map_update_elem(map_fd, \u0026key, \u0026fd, 0), \"update\"))\n+\t\treturn;\n+\n+\tASSERT_EQ(run_tc(skel-\u003eprogs.normal_prog), 1, \"normal_prog\");\n+\tASSERT_EQ(run_tc(skel-\u003eprogs.normal_prog), 1, \"normal_prog\");\n+\n+\tASSERT_EQ(run_tc(skel-\u003eprogs.tail_call_in_subprog), 1, \"tail_call_in_subprog\");\n+\tASSERT_EQ(skel-\u003ebss-\u003etail_callee_spun, 1, \"tail_callee_spun\");\n+\n+\tASSERT_EQ(run_tc(skel-\u003eprogs.tail_call_after_timeout), 0, \"tail_call_after_timeout\");\n+\tASSERT_EQ(skel-\u003ebss-\u003etail_callee_spun, 1, \"tail_callee_spun\");\n+\n+\tASSERT_EQ(run_tc(skel-\u003eprogs.throw_after_timeout), 7, \"throw_after_timeout\");\n+\tASSERT_EQ(run_tc(skel-\u003eprogs.throw_after_timeout), 7, \"throw_after_timeout\");\n+}\n+\n+static void test_user_ringbuf(struct timed_may_goto_user *skel)\n+{\n+\tstruct user_ring_buffer *rb;\n+\tvoid *sample;\n+\tint i;\n+\n+\trb = user_ring_buffer__new(bpf_map__fd(skel-\u003emaps.user_ringbuf), NULL);\n+\tif (!ASSERT_OK_PTR(rb, \"user_ring_buffer__new\"))\n+\t\treturn;\n+\tfor (i = 0; i \u003c TRIES; i++) {\n+\t\tsample = user_ring_buffer__reserve(rb, 8);\n+\t\tif (!ASSERT_OK_PTR(sample, \"reserve\"))\n+\t\t\tgoto out;\n+\t\tuser_ring_buffer__submit(rb, sample);\n+\t}\n+\t/* The helper takes a sample out of the buffer for each call: all calls have to run. */\n+\tASSERT_EQ(run_syscall(skel-\u003eprogs.drain), TRIES * 10 + 1, \"drain\");\n+out:\n+\tuser_ring_buffer__free(rb);\n+}\n+\n+static void test_priv_stack(struct timed_may_goto_user *skel)\n+{\n+\tLIBBPF_OPTS(bpf_test_run_opts, opts);\n+\tint fd = bpf_program__fd(skel-\u003eprogs.priv_stack);\n+\tstruct bpf_link *link;\n+\n+\tlink = bpf_program__attach(skel-\u003eprogs.priv_stack);\n+\tif (!ASSERT_OK_PTR(link, \"attach\"))\n+\t\treturn;\n+\tASSERT_OK(bpf_prog_test_run_opts(fd, \u0026opts), \"test_run\");\n+\tASSERT_EQ(skel-\u003ebss-\u003epriv_stack_spun, 1, \"priv_stack_spun\");\n+\tASSERT_EQ(skel-\u003ebss-\u003epriv_stack_cb_spun, 0, \"priv_stack_cb_spun\");\n+\tbpf_link__destroy(link);\n+}\n+\n+/*\n+ * Serial: most progs spin for 250 ms in an RCU read-side critical section, one after the other.\n+ * That delays what other tests wait for. In parallel mode \"mmap\" gives up its wait for the end of\n+ * a map.\n+ */\n+void serial_test_timed_may_goto(void)\n+{\n+\tstruct timed_may_goto_user *skel;\n+\tcpu_set_t old, new;\n+\n+\t/* The interpreter counts in each frame. */\n+\tif (!is_jit_enabled()) {\n+\t\ttest__skip();\n+\t\treturn;\n+\t}\n+\n+\tRUN_TESTS(timed_may_goto);\n+\n+#if !defined(__x86_64__) \u0026\u0026 !defined(__aarch64__)\n+\treturn;\n+#endif\n+\tskel = timed_may_goto_user__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\treturn;\n+\n+\t/* Count and timestamp are per-cpu: all progs of a subtest run on one cpu. */\n+\tif (!ASSERT_OK(sched_getaffinity(0, sizeof(old), \u0026old), \"getaffinity\"))\n+\t\tgoto out;\n+\tCPU_ZERO(\u0026new);\n+\tCPU_SET(sched_getcpu(), \u0026new);\n+\tif (!ASSERT_OK(sched_setaffinity(0, sizeof(new), \u0026new), \"setaffinity\"))\n+\t\tgoto out;\n+\n+\tif (test__start_subtest(\"async\"))\n+\t\ttest_async(skel);\n+\tif (test__start_subtest(\"timer_on_top\"))\n+\t\ttest_timer_on_top(skel);\n+\tif (test__start_subtest(\"freplace\"))\n+\t\ttest_freplace(skel);\n+\tif (test__start_subtest(\"tail_call_and_throw\"))\n+\t\ttest_tail_call_and_throw(skel);\n+\tif (test__start_subtest(\"user_ringbuf\"))\n+\t\ttest_user_ringbuf(skel);\n+\tif (test__start_subtest(\"priv_stack\"))\n+\t\ttest_priv_stack(skel);\n+\n+\tsched_setaffinity(0, sizeof(old), \u0026old);\n+out:\n+\ttimed_may_goto_user__destroy(skel);\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/arena_htab.c b/tools/testing/selftests/bpf/progs/arena_htab.c\nindex 81eaa94afeb01c..80cc14bb1c0f94 100644\n--- a/tools/testing/selftests/bpf/progs/arena_htab.c\n+++ b/tools/testing/selftests/bpf/progs/arena_htab.c\n@@ -34,8 +34,6 @@ int arena_htab_llvm(void *ctx)\n \tcast_kern(htab);\n \thtab_init(htab);\n \n-\tcast_kern(arr);\n-\n \t/* first run. No old elems in the table */\n \tfor (i = zero; i \u003c 100000 \u0026\u0026 can_loop; i++) {\n \t\thtab_update_elem(htab, i, i);\ndiff --git a/tools/testing/selftests/bpf/progs/timed_may_goto.c b/tools/testing/selftests/bpf/progs/timed_may_goto.c\nnew file mode 100644\nindex 00000000000000..c1c48af808a99c\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/timed_may_goto.c\n@@ -0,0 +1,898 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/*\n+ * There is one time slice of may_goto for an invocation of a prog: one call from the kernel into\n+ * the main prog, with the subprogs and sync callbacks that it reaches.\n+ *\n+ * Most tests call a function that spins until may_goto stops it, four times, each in a different\n+ * way. Only the first call can spin. With a time slice for each call all four do.\n+ */\n+#include \u003cvmlinux.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \u003cbpf/bpf_tracing.h\u003e\n+#include \"bpf_misc.h\"\n+#include \"bpf_experimental.h\"\n+\n+char _license[] SEC(\"license\") = \"GPL\";\n+\n+#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)) \u0026\u0026 __clang_major__ \u003e= 18\n+\n+#define TRIES 4\n+\n+int zero;\n+\n+/* Return 1 if the loop went on until the time was up. */\n+static __always_inline int spin(void)\n+{\n+\t__u64 t0 = bpf_ktime_get_ns();\n+\n+\twhile (can_loop)\n+\t\t;\n+\treturn bpf_ktime_get_ns() - t0 \u003e 100 * 1000 * 1000;\n+}\n+\n+static __noinline int spins(void)\n+{\n+\treturn spin();\n+}\n+\n+static __noinline int counts_to_1000(void)\n+{\n+\tint i, cnt = zero;\n+\n+\tfor (i = zero; i \u003c 1000 \u0026\u0026 can_loop; i++)\n+\t\tcnt++;\n+\treturn cnt;\n+}\n+\n+SEC(\"tc\")\n+__description(\"timed may_goto: call\")\n+__success __retval(1)\n+int call_subprog(void *ctx)\n+{\n+\tint i, n = 0;\n+\n+\tfor (i = 0; i \u003c TRIES; i++)\n+\t\tn += spins();\n+\treturn n;\n+}\n+\n+SEC(\"tc\")\n+__description(\"timed may_goto: a call after the time is up makes no iteration\")\n+__success __retval(0)\n+int short_loop_after_timeout(void *ctx)\n+{\n+\tspins();\n+\treturn counts_to_1000();\n+}\n+\n+SEC(\"tc\")\n+__description(\"timed may_goto: the loop of the main prog stops when the time is up in a subprog\")\n+__success __retval(1)\n+int main_loop_after_timeout(void *ctx)\n+{\n+\tint n = 0;\n+\n+\twhile (can_loop \u0026\u0026 n \u003c 2 * TRIES)\n+\t\tn += spins();\n+\treturn n;\n+}\n+\n+static __noinline int passes(int i)\n+{\n+\treturn spins() + i;\n+}\n+\n+SEC(\"tc\")\n+__description(\"timed may_goto: call through a function without a loop\")\n+__success __retval(1)\n+int call_through(void *ctx)\n+{\n+\tint i, n = 0;\n+\n+\tfor (i = 0; i \u003c TRIES; i++)\n+\t\tn += passes(zero);\n+\treturn n;\n+}\n+\n+__noinline int global_spins(void)\n+{\n+\treturn spin();\n+}\n+\n+SEC(\"tc\")\n+__description(\"timed may_goto: call of a global function\")\n+__success __retval(1)\n+int call_global(void *ctx)\n+{\n+\tint i, n = 0;\n+\n+\tfor (i = 0; i \u003c TRIES; i++)\n+\t\tn += global_spins();\n+\treturn n;\n+}\n+\n+SEC(\"tc\")\n+__description(\"timed may_goto: callx\")\n+__success __retval(1)\n+__naked void callx_subprog(void)\n+{\n+\tasm volatile (\n+\t\t\"r6 = 0;\"\n+\t\t\"r7 = 0;\"\n+\t\"1:\"\n+\t\t\"r3 = %[spins] ll;\"\n+\t\t\"callx r3;\"\n+\t\t\"r6 += r0;\"\n+\t\t\"r7 += 1;\"\n+\t\t\"if r7 \u003c 4 goto 1b;\"\n+\t\t\"r0 = r6;\"\n+\t\t\"exit;\"\n+\t\t:\n+\t\t: __imm_addr(spins)\n+\t\t: __clobber_all);\n+}\n+\n+/* With a time slice for each call it gives up, so that the test fails and doesn't lock up. */\n+static int loop_cb(__u64 i, int *spun)\n+{\n+\t*spun += spin();\n+\treturn *spun \u003e 2 * TRIES;\n+}\n+\n+SEC(\"tc\")\n+__description(\"timed may_goto: bpf_loop() inlined\")\n+__success __retval(1)\n+int loop_inlined(void *ctx)\n+{\n+\tint spun = 0;\n+\n+\tbpf_loop(1 \u003c\u003c 20, loop_cb, \u0026spun, 0);\n+\treturn spun;\n+}\n+\n+SEC(\"tc\")\n+__description(\"timed may_goto: bpf_loop() not inlined\")\n+__success __retval(1)\n+int loop_not_inlined(void *ctx)\n+{\n+\tint spun = 0;\n+\n+\tbpf_loop(1 \u003c\u003c 20, loop_cb, \u0026spun, zero);\n+\treturn spun;\n+}\n+\n+static int count_cb(__u64 i, int *calls)\n+{\n+\t(*calls)++;\n+\treturn 0;\n+}\n+\n+/* The time is up: the helper calls the callback one more time. It returns 1 in its first insns. */\n+#define LOOP_ENDS(name, flags)\t\t\t\t\t\t\t\\\n+SEC(\"tc\")\t\t\t\t\t\t\t\t\t\\\n+__description(\"timed may_goto: bpf_loop() \" #name \" ends when the time is up\")\t\\\n+__success __retval(10)\t\t\t\t\t\t\t\t\\\n+int loop_ends_##name(void *ctx)\t\t\t\t\t\t\t\\\n+{\t\t\t\t\t\t\t\t\t\t\\\n+\tint calls = 0;\t\t\t\t\t\t\t\t\\\n+\t\t\t\t\t\t\t\t\t\t\\\n+\tspins();\t\t\t\t\t\t\t\t\\\n+\treturn bpf_loop(1000, count_cb, \u0026calls, flags) * 10 + calls;\t\t\\\n+}\n+\n+LOOP_ENDS(inlined, 0)\n+LOOP_ENDS(not_inlined, zero)\n+\n+struct nested {\n+\t__u64 t0;\n+\tint spun;\n+};\n+\n+/* Without an end of bpf_loop(): give up, so that the test fails and doesn't lock up. */\n+static int inner_cb(__u64 i, struct nested *n)\n+{\n+\tif (!n-\u003espun)\n+\t\tn-\u003espun = spin();\n+\treturn (i \u0026 1023) == 0 \u0026\u0026 bpf_ktime_get_ns() - n-\u003et0 \u003e 1000 * 1000 * 1000;\n+}\n+\n+static int outer_cb(__u64 i, struct nested *n)\n+{\n+\tbpf_loop(1 \u003c\u003c 23, inner_cb, n, 0);\n+\treturn bpf_ktime_get_ns() - n-\u003et0 \u003e 1000 * 1000 * 1000;\n+}\n+\n+SEC(\"tc\")\n+__description(\"timed may_goto: bpf_loop() in bpf_loop() ends when the time is up\")\n+__success __retval(1)\n+int loop_in_loop(void *ctx)\n+{\n+\tstruct nested n = { .t0 = bpf_ktime_get_ns() };\n+\n+\tbpf_loop(1 \u003c\u003c 23, outer_cb, \u0026n, 0);\n+\treturn n.spun \u0026\u0026 bpf_ktime_get_ns() - n.t0 \u003c 500 * 1000 * 1000;\n+}\n+\n+static __noinline int called_cb(__u64 i, int *calls)\n+{\n+\t(*calls)++;\n+\treturn 0;\n+}\n+\n+/* BPF code calls the callback too, so its return value has to be its own. bpf_loop() goes on. */\n+SEC(\"tc\")\n+__description(\"timed may_goto: bpf_loop() with a callback that is called as well\")\n+__success __retval(45)\n+int loop_and_call(void *ctx)\n+{\n+\tint calls = 0;\n+\n+\tspins();\n+\tif (called_cb(zero, \u0026calls))\n+\t\treturn 100;\n+\treturn bpf_loop(TRIES, called_cb, \u0026calls, 0) * 10 + calls;\n+}\n+\n+static int short_cb(__u64 i, __u64 *t0)\n+{\n+\tint j;\n+\n+\tfor (j = zero; j \u003c 60000 \u0026\u0026 can_loop; j++)\n+\t\t;\n+\treturn (i \u0026 1023) == 0 \u0026\u0026 bpf_ktime_get_ns() - *t0 \u003e 1000 * 1000 * 1000;\n+}\n+\n+/*\n+ * The main prog has no may_goto. Each call of the callback ends before it reads the clock. The\n+ * iterations of all calls add up.\n+ */\n+SEC(\"tc\")\n+__description(\"timed may_goto: bpf_loop() the callback makes 60000 iterations\")\n+__success __retval(1)\n+int loop_short_cb(void *ctx)\n+{\n+\t__u64 t0 = bpf_ktime_get_ns();\n+\n+\tbpf_loop(1 \u003c\u003c 20, short_cb, \u0026t0, 0);\n+\treturn bpf_ktime_get_ns() - t0 \u003c 1000 * 1000 * 1000;\n+}\n+\n+/* spins() uses up the time. The loop that calls it ends at its next may_goto. */\n+static __noinline int loops_around_spins(void)\n+{\n+\tint n = zero;\n+\n+\twhile (can_loop \u0026\u0026 n \u003c 20000) {\n+\t\tif (n \u003c 2 * TRIES)\n+\t\t\tspins();\n+\t\tn++;\n+\t}\n+\treturn n;\n+}\n+\n+SEC(\"tc\")\n+__description(\"timed may_goto: a loop that is on the stack ends when the time is up\")\n+__success __retval(1)\n+int rest_of_batch(void *ctx)\n+{\n+\treturn loops_around_spins();\n+}\n+\n+static __noinline int one_iteration(void)\n+{\n+\tint i, n = zero;\n+\n+\tfor (i = zero; i \u003c 1 \u0026\u0026 can_loop; i++)\n+\t\tn++;\n+\treturn n;\n+}\n+\n+long calls_ok;\n+\n+static int call_one(__u64 i, __u64 *t0)\n+{\n+\tif (!one_iteration())\n+\t\treturn 1;\n+\tcalls_ok++;\n+\t/* With a time slice for each call: give up. */\n+\treturn (i \u0026 1023) == 0 \u0026\u0026 bpf_ktime_get_ns() - *t0 \u003e 1000 * 1000 * 1000;\n+}\n+\n+static int call_many(__u64 i, __u64 *t0)\n+{\n+\tlong before = calls_ok;\n+\n+\tbpf_loop(1 \u003c\u003c 23, call_one, t0, 0);\n+\treturn calls_ok - before != 1 \u003c\u003c 23;\n+}\n+\n+/*\n+ * The iterations of all calls count: the prog reads the clock in each 65535th call. The call that\n+ * finds out that the time is up makes its iteration.\n+ */\n+SEC(\"tc\")\n+__description(\"timed may_goto: 65535 calls that make one iteration read the clock\")\n+__success __retval(1)\n+int clock_after_65535_calls(void *ctx)\n+{\n+\t__u64 t0 = bpf_ktime_get_ns();\n+\n+\tcalls_ok = 0;\n+\tbpf_loop(1000, call_many, \u0026t0, 0);\n+\treturn calls_ok \u003e 0 \u0026\u0026 calls_ok % 65535 == 0;\n+}\n+\n+#define MAP(name, map_type, flags)\t\t\t\\\n+struct {\t\t\t\t\t\t\\\n+\t__uint(type, map_type);\t\t\t\t\\\n+\t__uint(max_entries, TRIES);\t\t\t\\\n+\t__uint(map_flags, flags);\t\t\t\\\n+\t__type(key, int);\t\t\t\t\\\n+\t__type(value, int);\t\t\t\t\\\n+} name SEC(\".maps\")\n+\n+MAP(array, BPF_MAP_TYPE_ARRAY, 0);\n+MAP(hash, BPF_MAP_TYPE_HASH, 0);\n+\n+/* The first call uses up the time. The second call ends the loop of the helper. */\n+static int elem_cb(void *map, int *key, int *val, int *spun)\n+{\n+\t*spun += spin();\n+\treturn 0;\n+}\n+\n+#define FOR_EACH(name)\t\t\t\t\t\t\t\t\\\n+SEC(\"tc\")\t\t\t\t\t\t\t\t\t\\\n+__description(\"timed may_goto: bpf_for_each_map_elem() \" #name)\t\t\t\\\n+__success __retval(21)\t\t\t\t\t\t\t\t\\\n+int for_each_##name(void *ctx)\t\t\t\t\t\t\t\\\n+{\t\t\t\t\t\t\t\t\t\t\\\n+\tint i, key, spun = 0;\t\t\t\t\t\t\t\\\n+\t\t\t\t\t\t\t\t\t\t\\\n+\tfor (i = 0; i \u003c TRIES; i++) {\t\t\t\t\t\t\\\n+\t\tkey = i;\t\t\t\t\t\t\t\\\n+\t\tbpf_map_update_elem(\u0026name, \u0026key, \u0026key, 0);\t\t\t\\\n+\t}\t\t\t\t\t\t\t\t\t\\\n+\treturn bpf_for_each_map_elem(\u0026name, elem_cb, \u0026spun, 0) * 10 + spun;\t\\\n+}\n+\n+FOR_EACH(array)\n+FOR_EACH(hash)\n+\n+static long vma_cb(struct task_struct *task, struct vm_area_struct *vma, int *spun)\n+{\n+\t*spun += spin();\n+\treturn 0;\n+}\n+\n+SEC(\"raw_tp\")\n+__description(\"timed may_goto: bpf_find_vma()\")\n+__success __retval(1)\n+int find_vma(void *ctx)\n+{\n+\tstruct task_struct *task = bpf_get_current_task_btf();\n+\t__u64 addr = task-\u003emm-\u003earg_start;\n+\tint i, spun = 0;\n+\n+\tfor (i = 0; i \u003c TRIES; i++)\n+\t\tif (bpf_find_vma(task, addr, vma_cb, \u0026spun, 0))\n+\t\t\treturn 100;\n+\treturn spun;\n+}\n+\n+struct tree_node {\n+\tstruct bpf_rb_node rb;\n+\tint key;\n+};\n+\n+#define private(name) SEC(\".data.\" #name) __hidden __attribute__((aligned(8)))\n+private(A) struct bpf_spin_lock lock;\n+private(A) struct bpf_rb_root root __contains(tree_node, rb);\n+\n+int less_spun, less_calls;\n+\n+static bool less(struct bpf_rb_node *a, const struct bpf_rb_node *b)\n+{\n+\tlong n = zero;\n+\n+\t/* no helpers under the lock */\n+\twhile (can_loop)\n+\t\tn++;\n+\tless_calls++;\n+\tless_spun += n \u003e 1000 * 1000;\n+\treturn container_of(a, struct tree_node, rb)-\u003ekey \u003c\n+\t container_of(b, struct tree_node, rb)-\u003ekey;\n+}\n+\n+SEC(\"tc\")\n+__description(\"timed may_goto: bpf_rbtree_add()\")\n+__success __retval(1)\n+int rbtree_add(void *ctx)\n+{\n+\tstruct bpf_rb_node *rb;\n+\tstruct tree_node *n;\n+\tint i;\n+\n+\tless_spun = less_calls = 0;\n+\tfor (i = 0; i \u003c TRIES; i++) {\n+\t\tn = bpf_obj_new(typeof(*n));\n+\t\tif (!n)\n+\t\t\treturn 100;\n+\t\tn-\u003ekey = TRIES - i;\n+\t\tbpf_spin_lock(\u0026lock);\n+\t\tbpf_rbtree_add(\u0026root, \u0026n-\u003erb, less);\n+\t\tbpf_spin_unlock(\u0026lock);\n+\t}\n+\tfor (i = 0; i \u003c TRIES; i++) {\n+\t\tbpf_spin_lock(\u0026lock);\n+\t\trb = bpf_rbtree_first(\u0026root);\n+\t\tif (rb)\n+\t\t\trb = bpf_rbtree_remove(\u0026root, rb);\n+\t\tbpf_spin_unlock(\u0026lock);\n+\t\tif (!rb)\n+\t\t\treturn 101;\n+\t\tbpf_obj_drop(container_of(rb, struct tree_node, rb));\n+\t}\n+\tif (less_calls \u003c TRIES - 1)\n+\t\treturn 102;\n+\treturn less_spun;\n+}\n+\n+/*\n+ * All registers are live at the may_goto, so it saves one of them in the stack. They are the same\n+ * after 70000 iterations: the loop reads the clock.\n+ */\n+__naked __noinline __used\n+static long regs_across_may_goto(void)\n+{\n+\tasm volatile (\n+\t\t\"r0 = 10;\"\n+\t\t\"r6 = 6;\"\n+\t\t\"r7 = 7;\"\n+\t\t\"r8 = 8;\"\n+\t\t\"r9 = 0;\"\n+\t\"1:\"\n+\t\t\"may_goto 2f;\"\n+\t\t\"r9 += 1;\"\n+\t\t\"if r9 \u003c 70000 goto 1b;\"\n+\t\"2:\"\n+\t\t\"r0 += r1;\"\n+\t\t\"r0 += r2;\"\n+\t\t\"r0 += r3;\"\n+\t\t\"r0 += r4;\"\n+\t\t\"r0 += r5;\"\n+\t\t\"r0 += r6;\"\n+\t\t\"r0 += r7;\"\n+\t\t\"r0 += r8;\"\n+\t\t\"r0 += r9;\"\n+\t\t\"exit;\"\n+\t\t::: __clobber_all);\n+}\n+\n+SEC(\"tc\")\n+__description(\"timed may_goto: registers across may_goto\")\n+__success __retval(70046)\n+__naked void regs_across_may_goto_main(void)\n+{\n+\tasm volatile (\n+\t\t\"r1 = 1;\"\n+\t\t\"r2 = 2;\"\n+\t\t\"r3 = 3;\"\n+\t\t\"r4 = 4;\"\n+\t\t\"r5 = 5;\"\n+\t\t\"call regs_across_may_goto;\"\n+\t\t\"exit;\"\n+\t\t::: __clobber_all);\n+}\n+\n+/*\n+ * may_goto uses a register that is dead. In live_r0_rN() R0 - RN are live at the may_goto and the\n+ * next one is not. The values come from memory, so the verifier doesn't know them. Return their sum\n+ * and the number of iterations.\n+ */\n+__u64 vals[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };\n+__u64 zero64;\n+\n+#define LD0(b)\t\t\"r0 = *(u64 *)(r\" #b \" + 0);\"\n+#define LD1(b) LD0(b)\t\"r1 = *(u64 *)(r\" #b \" + 8);\"\n+#define LD2(b) LD1(b)\t\"r2 = *(u64 *)(r\" #b \" + 16);\"\n+#define LD3(b) LD2(b)\t\"r3 = *(u64 *)(r\" #b \" + 24);\"\n+#define LD4(b) LD3(b)\t\"r4 = *(u64 *)(r\" #b \" + 32);\"\n+#define LD5(b) LD4(b)\t\"r5 = *(u64 *)(r\" #b \" + 40);\"\n+#define LD6(b) LD5(b)\t\"r6 = *(u64 *)(r\" #b \" + 48);\"\n+#define LD7(b) LD6(b)\t\"r7 = *(u64 *)(r\" #b \" + 56);\"\n+#define LD8(b) LD7(b)\t\"r8 = *(u64 *)(r\" #b \" + 64);\"\n+\n+#define SUM0\n+#define SUM1 SUM0\t\"r0 += r1;\"\n+#define SUM2 SUM1\t\"r0 += r2;\"\n+#define SUM3 SUM2\t\"r0 += r3;\"\n+#define SUM4 SUM3\t\"r0 += r4;\"\n+#define SUM5 SUM4\t\"r0 += r5;\"\n+#define SUM6 SUM5\t\"r0 += r6;\"\n+#define SUM7 SUM6\t\"r0 += r7;\"\n+#define SUM8 SUM7\t\"r0 += r8;\"\n+\n+/* The count of iterations is at r10 - 8. It begins with a number from memory too. */\n+#define COUNT_INIT(t)\t\t\t\t\t\t\\\n+\t\t\"r\" #t \" = %[zero64] ll;\"\t\t\t\\\n+\t\t\"r\" #t \" = *(u64 *)(r\" #t \" + 0);\"\t\t\\\n+\t\t\"*(u64 *)(r10 - 8) = r\" #t \";\"\n+#define COUNT_AND_LOOP(t)\t\t\t\t\t\\\n+\t\t\"r\" #t \" = *(u64 *)(r10 - 8);\"\t\t\t\\\n+\t\t\"r\" #t \" += 1;\"\t\t\t\t\t\\\n+\t\t\"*(u64 *)(r10 - 8) = r\" #t \";\"\t\t\t\\\n+\t\t\"if r\" #t \" \u003c 70000 goto 1b;\"\n+#define ADD_COUNT_AND_EXIT\t\t\t\t\t\\\n+\t\t\"r1 = *(u64 *)(r10 - 8);\"\t\t\t\\\n+\t\t\"r0 += r1;\"\t\t\t\t\t\\\n+\t\t\"exit;\"\t\t\t\t\t\t\\\n+\t\t:\t\t\t\t\t\t\\\n+\t\t: __imm_addr(vals), __imm_addr(zero64)\t\t\\\n+\t\t: __clobber_all\n+\n+#define LIVE_REGS(n, next)\t\t\t\t\t\\\n+__naked __noinline __used\t\t\t\t\t\\\n+static long live_r0_r##n(void)\t\t\t\t\t\\\n+{\t\t\t\t\t\t\t\t\\\n+\tasm volatile (\t\t\t\t\t\t\\\n+\t\tCOUNT_INIT(next)\t\t\t\t\\\n+\t\t\"r\" #n \" = %[vals] ll;\"\t\t\t\t\\\n+\t\tLD##n(n)\t\t\t\t\t\\\n+\t\"1:\"\t\t\t\t\t\t\t\\\n+\t\t\"may_goto 2f;\"\t\t\t\t\t\\\n+\t\tCOUNT_AND_LOOP(next)\t\t\t\t\\\n+\t\"2:\"\t\t\t\t\t\t\t\\\n+\t\tSUM##n\t\t\t\t\t\t\\\n+\t\tADD_COUNT_AND_EXIT);\t\t\t\t\\\n+}\n+\n+LIVE_REGS(0, 1)\n+LIVE_REGS(1, 2)\n+LIVE_REGS(2, 3)\n+LIVE_REGS(3, 4)\n+LIVE_REGS(4, 5)\n+LIVE_REGS(5, 6)\n+LIVE_REGS(6, 7)\n+LIVE_REGS(7, 8)\n+LIVE_REGS(8, 9)\n+\n+/* R2 is read at the top of the loop only: it is live at the may_goto through the back edge. */\n+__naked __noinline __used\n+static long live_back_edge(void)\n+{\n+\tasm volatile (\n+\t\tCOUNT_INIT(3)\n+\t\t\"r2 = %[vals] ll;\"\n+\t\tLD2(2)\n+\t\"1:\"\n+\t\t\"*(u64 *)(r10 - 16) = r2;\"\n+\t\t\"may_goto 2f;\"\n+\t\tCOUNT_AND_LOOP(3)\n+\t\"2:\"\n+\t\t\"r0 += r1;\"\n+\t\t\"r2 = *(u64 *)(r10 - 16);\"\n+\t\t\"r0 += r2;\"\n+\t\tADD_COUNT_AND_EXIT);\n+}\n+\n+/* R2 is read only where the may_goto jumps to. */\n+__naked __noinline __used\n+static long live_target(void)\n+{\n+\tasm volatile (\n+\t\tCOUNT_INIT(3)\n+\t\t\"r2 = %[vals] ll;\"\n+\t\tLD2(2)\n+\t\"1:\"\n+\t\t\"may_goto 2f;\"\n+\t\tCOUNT_AND_LOOP(3)\n+\t\t\"goto 3f;\"\n+\t\"2:\"\n+\t\t\"r0 += r2;\"\n+\t\"3:\"\n+\t\t\"r0 += r1;\"\n+\t\tADD_COUNT_AND_EXIT);\n+}\n+\n+/* R2 is set in front of the may_goto in each iteration, and read only right behind it. */\n+__naked __noinline __used\n+static long live_fall_through(void)\n+{\n+\tasm volatile (\n+\t\tCOUNT_INIT(3)\n+\t\t\"r1 = %[vals] ll;\"\n+\t\tLD1(1)\n+\t\t\"*(u64 *)(r10 - 16) = r0;\"\n+\t\"1:\"\n+\t\t\"r2 = %[vals] ll;\"\n+\t\t\"r2 = *(u64 *)(r2 + 16);\"\n+\t\t\"may_goto 2f;\"\n+\t\t\"*(u64 *)(r10 - 16) = r2;\"\n+\t\tCOUNT_AND_LOOP(3)\n+\t\"2:\"\n+\t\t\"r0 += r1;\"\n+\t\t\"r2 = *(u64 *)(r10 - 16);\"\n+\t\t\"r0 += r2;\"\n+\t\tADD_COUNT_AND_EXIT);\n+}\n+\n+#define CALL_AND_ADD(f)\t\"call \" #f \";\" \"r6 += r0;\"\n+#define CALL_ALL_LIVE\t\t\t\t\t\t\\\n+\t\t\"r6 = 0;\"\t\t\t\t\t\\\n+\t\tCALL_AND_ADD(live_r0_r0)\t\t\t\\\n+\t\tCALL_AND_ADD(live_r0_r1)\t\t\t\\\n+\t\tCALL_AND_ADD(live_r0_r2)\t\t\t\\\n+\t\tCALL_AND_ADD(live_r0_r3)\t\t\t\\\n+\t\tCALL_AND_ADD(live_r0_r4)\t\t\t\\\n+\t\tCALL_AND_ADD(live_r0_r5)\t\t\t\\\n+\t\tCALL_AND_ADD(live_r0_r6)\t\t\t\\\n+\t\tCALL_AND_ADD(live_r0_r7)\t\t\t\\\n+\t\tCALL_AND_ADD(live_r0_r8)\t\t\t\\\n+\t\tCALL_AND_ADD(live_back_edge)\t\t\t\\\n+\t\tCALL_AND_ADD(live_target)\t\t\t\\\n+\t\tCALL_AND_ADD(live_fall_through)\t\t\t\\\n+\t\t\"r0 = r6;\"\t\t\t\t\t\\\n+\t\t\"exit;\"\n+\n+/*\n+ * The sums: 1 + 3 + 6 + 10 + 15 + 21 + 28 + 36 + 45 = 165. Then 6, 3 (the may_goto doesn't jump)\n+ * and 6. 12 * 70000 iterations: each function reads the clock.\n+ */\n+SEC(\"tc\")\n+__description(\"timed may_goto: may_goto takes a register that is dead\")\n+__success __retval(840180)\n+__naked void dead_reg(void)\n+{\n+\tasm volatile (\n+\t\tCALL_ALL_LIVE\n+\t\t::: __clobber_all);\n+}\n+\n+/* No iteration: 165, then 6, 6 (the may_goto jumps) and 4 (R2 isn't stored). */\n+SEC(\"tc\")\n+__description(\"timed may_goto: may_goto takes a register that is dead, the time is up\")\n+__success __retval(181)\n+__naked void dead_reg_time_is_up(void)\n+{\n+\tasm volatile (\n+\t\t\"call spins_on_may_goto;\"\n+\t\tCALL_ALL_LIVE\n+\t\t::: __clobber_all);\n+}\n+\n+/*\n+ * The subprog uses up the time of the prog and returns. With a time slice for each call the caller\n+ * calls it 65535 times before it reads the clock.\n+ */\n+__naked __noinline __used\n+static unsigned long spins_on_may_goto(void)\n+{\n+\tasm volatile (\n+\t\"1:\"\n+\t\t\"may_goto 2f;\"\n+\t\t\"goto 1b;\"\n+\t\"2:\"\n+\t\t\"r0 = 0;\"\n+\t\t\"exit;\"\n+\t\t::: __clobber_all);\n+}\n+\n+/* The first may_goto of the function finds that the time is up */\n+SEC(\"tc\")\n+__description(\"timed may_goto: registers across may_goto when the time is up\")\n+__success __retval(46)\n+__naked void regs_when_time_is_up(void)\n+{\n+\tasm volatile (\n+\t\t\"call spins_on_may_goto;\"\n+\t\t\"r1 = 1;\"\n+\t\t\"r2 = 2;\"\n+\t\t\"r3 = 3;\"\n+\t\t\"r4 = 4;\"\n+\t\t\"r5 = 5;\"\n+\t\t\"call regs_across_may_goto;\"\n+\t\t\"exit;\"\n+\t\t::: __clobber_all);\n+}\n+\n+/* The first insn of the main prog is the target of a jump. It must not set the count again. */\n+SEC(\"socket\")\n+__description(\"timed may_goto: 9 insns\")\n+__success __retval(1)\n+__naked void loop_calls_subprog_with_may_goto(void)\n+{\n+\tasm volatile (\n+\t\"1:\"\n+\t\t\"call spins_on_may_goto;\"\n+\t\t\"may_goto 2f;\"\n+\t\t\"goto 1b;\"\n+\t\"2:\"\n+\t\t\"r0 = 1;\"\n+\t\t\"exit;\"\n+\t\t::: __clobber_all);\n+}\n+\n+/*\n+ * These subprogs make 60000 iterations in each call. With a read of the clock after 65535\n+ * iterations of a call none of them reads it, and their caller does it after\n+ * 65535 * 60000 * 60000 iterations.\n+ */\n+__naked __noinline __used\n+static unsigned long loops_60000(void)\n+{\n+\tasm volatile (\n+\t\t\"r1 = 60000;\"\n+\t\"1:\"\n+\t\t\"may_goto 2f;\"\n+\t\t\"r1 += -1;\"\n+\t\t\"if r1 != 0 goto 1b;\"\n+\t\"2:\"\n+\t\t\"r0 = 0;\"\n+\t\t\"exit;\"\n+\t\t::: __clobber_all);\n+}\n+\n+__naked __noinline __used\n+static unsigned long calls_60000_times(void)\n+{\n+\tasm volatile (\n+\t\t\"r6 = 60000;\"\n+\t\"1:\"\n+\t\t\"call loops_60000;\"\n+\t\t\"may_goto 2f;\"\n+\t\t\"r6 += -1;\"\n+\t\t\"if r6 != 0 goto 1b;\"\n+\t\"2:\"\n+\t\t\"r0 = 0;\"\n+\t\t\"exit;\"\n+\t\t::: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"timed may_goto: three levels of loops that end before they read the clock\")\n+__success __retval(1)\n+__naked void three_levels_of_short_loops(void)\n+{\n+\tasm volatile (\n+\t\"1:\"\n+\t\t\"call calls_60000_times;\"\n+\t\t\"may_goto 2f;\"\n+\t\t\"goto 1b;\"\n+\t\"2:\"\n+\t\t\"r0 = 1;\"\n+\t\t\"exit;\"\n+\t\t::: __clobber_all);\n+}\n+\n+/* The callee ends one iteration before it reads the clock. */\n+__naked __noinline __used\n+static unsigned long loops_65534(void)\n+{\n+\tasm volatile (\n+\t\t\"r1 = 65534;\"\n+\t\"1:\"\n+\t\t\"may_goto 2f;\"\n+\t\t\"r1 += -1;\"\n+\t\t\"if r1 != 0 goto 1b;\"\n+\t\"2:\"\n+\t\t\"r0 = 0;\"\n+\t\t\"exit;\"\n+\t\t::: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"timed may_goto: the subprog makes 65534 iterations\")\n+__success __retval(1)\n+__naked void loop_calls_subprog_with_65534_iterations(void)\n+{\n+\tasm volatile (\n+\t\t\"call %[bpf_ktime_get_ns];\"\n+\t\t\"r6 = r0;\"\n+\t\"1:\"\n+\t\t\"call loops_65534;\"\n+\t\t\"may_goto 2f;\"\n+\t\t\"goto 1b;\"\n+\t\"2:\"\n+\t\t\"call %[bpf_ktime_get_ns];\"\n+\t\t\"r0 -= r6;\"\n+\t\t\"r1 = r0;\"\n+\t\t\"r0 = 1;\"\n+\t\t\"if r1 \u003c 1000000000 goto +1;\"\n+\t\t\"r0 = 0;\"\n+\t\t\"exit;\"\n+\t\t:\n+\t\t: __imm(bpf_ktime_get_ns)\n+\t\t: __clobber_all);\n+}\n+\n+/* The subprog returns 16 bytes in R0 and R2. may_goto must not change them. */\n+__naked __noinline __used\n+static unsigned __int128 returns_pair_after_may_goto(void)\n+{\n+\tasm volatile (\n+\t\t\"r0 = 1;\"\n+\t\t\"r2 = 2;\"\n+\t\"1:\"\n+\t\t\"may_goto 2f;\"\n+\t\t\"goto 1b;\"\n+\t\"2:\"\n+\t\t\"exit;\"\n+\t\t::: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"timed may_goto: the subprog returns 16 bytes\")\n+__success __retval(3)\n+__naked void subprog_with_may_goto_returns_pair(void)\n+{\n+\tasm volatile (\n+\t\t\"call returns_pair_after_may_goto;\"\n+\t\t\"r0 += r2;\"\n+\t\t\"exit;\"\n+\t\t::: __clobber_all);\n+}\n+\n+/* Dead code doesn't count: a prog that has no may_goto left gets no insns. */\n+__naked __noinline __used\n+static unsigned long may_goto_is_dead_code(void)\n+{\n+\tasm volatile (\n+\t\t\"r0 = 0;\"\n+\t\t\"if r0 == 0 goto 2f;\"\n+\t\"1:\"\n+\t\t\"may_goto 2f;\"\n+\t\t\"goto 1b;\"\n+\t\"2:\"\n+\t\t\"exit;\"\n+\t\t::: __clobber_all);\n+}\n+\n+SEC(\"socket\")\n+__description(\"timed may_goto: may_goto in dead code\")\n+__success __retval(0)\n+__xlated(\"0: call pc+1\")\n+__xlated(\"1: exit\")\n+__xlated(\"2: r0 = 0\")\n+__xlated(\"3: exit\")\n+__naked void dead_may_goto(void)\n+{\n+\tasm volatile (\n+\t\t\"call may_goto_is_dead_code;\"\n+\t\t\"exit;\"\n+\t\t::: __clobber_all);\n+}\n+\n+static __noinline int throws_after_spin(void)\n+{\n+\tspin();\n+\tbpf_throw(0);\n+\treturn 0;\n+}\n+\n+__noinline int exception_cb_loops(u64 cookie)\n+{\n+\treturn counts_to_1000();\n+}\n+\n+/* The exception callback belongs to the invocation that throws. */\n+SEC(\"tc\")\n+__description(\"timed may_goto: bpf_throw() the exception callback has a loop\")\n+__exception_cb(exception_cb_loops)\n+__success __retval(0)\n+int throw_after_timeout(void *ctx)\n+{\n+\treturn throws_after_spin();\n+}\n+\n+#else\n+\n+SEC(\"socket\")\n+__description(\"timed may_goto: not supported dummy test\")\n+__success\n+int dummy_test(void)\n+{\n+\treturn 0;\n+}\n+\n+#endif\ndiff --git a/tools/testing/selftests/bpf/progs/timed_may_goto_ext.c b/tools/testing/selftests/bpf/progs/timed_may_goto_ext.c\nnew file mode 100644\nindex 00000000000000..969ca58b6eedcc\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/timed_may_goto_ext.c\n@@ -0,0 +1,23 @@\n+// SPDX-License-Identifier: GPL-2.0\n+#include \u003cvmlinux.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \"bpf_misc.h\"\n+#include \"bpf_experimental.h\"\n+\n+char _license[] SEC(\"license\") = \"GPL\";\n+\n+static __noinline int spins(void)\n+{\n+\t__u64 t0 = bpf_ktime_get_ns();\n+\n+\twhile (can_loop)\n+\t\t;\n+\treturn bpf_ktime_get_ns() - t0 \u003e 100 * 1000 * 1000;\n+}\n+\n+/* Each call of an extension prog is an invocation: one of its two calls spins. */\n+SEC(\"freplace\")\n+int replacement(int i)\n+{\n+\treturn spins() + spins() + i;\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/timed_may_goto_user.c b/tools/testing/selftests/bpf/progs/timed_may_goto_user.c\nnew file mode 100644\nindex 00000000000000..8b85815d45d25e\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/timed_may_goto_user.c\n@@ -0,0 +1,311 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* The tests of timed_may_goto.c that need help from user space. */\n+#include \u003cvmlinux.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \u003cbpf/bpf_tracing.h\u003e\n+#include \"bpf_misc.h\"\n+#include \"bpf_experimental.h\"\n+\n+char _license[] SEC(\"license\") = \"GPL\";\n+\n+#define TRIES 4\n+#define CLOCK_MONOTONIC 1\n+\n+int zero;\n+\n+/* Return 1 if the loop went on until the time was up. */\n+static __always_inline int spin(void)\n+{\n+\t__u64 t0 = bpf_ktime_get_ns();\n+\n+\twhile (can_loop)\n+\t\t;\n+\treturn bpf_ktime_get_ns() - t0 \u003e 100 * 1000 * 1000;\n+}\n+\n+static __noinline int spins(void)\n+{\n+\treturn spin();\n+}\n+\n+static __noinline int counts_to_1000(void)\n+{\n+\tint i, cnt = zero;\n+\n+\tfor (i = zero; i \u003c 1000 \u0026\u0026 can_loop; i++)\n+\t\tcnt++;\n+\treturn cnt;\n+}\n+\n+/* Nothing special: the loops go on, then the time is up one time. */\n+SEC(\"tc\")\n+int normal_prog(void *ctx)\n+{\n+\tint i, n = 0;\n+\n+\tif (counts_to_1000() != 1000)\n+\t\treturn 100;\n+\tfor (i = 0; i \u003c TRIES; i++)\n+\t\tn += spins();\n+\treturn n;\n+}\n+\n+/*\n+ * Async callbacks\n+ */\n+struct elem {\n+\tstruct bpf_timer timer;\n+\tstruct bpf_wq wq;\n+\tstruct bpf_task_work tw;\n+\tstruct bpf_rcu_head rcu;\n+};\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARRAY);\n+\t__uint(max_entries, 2);\n+\t__type(key, int);\n+\t__type(value, struct elem);\n+} async SEC(\".maps\");\n+\n+int timer_cnt, wq_cnt, tw_cnt, rcu_cnt, rcu_trace_cnt;\n+\n+static int timer_cb(void *map, int *key, struct elem *e)\n+{\n+\ttimer_cnt = counts_to_1000();\n+\treturn 0;\n+}\n+\n+static int wq_cb(void *map, int *key, void *val)\n+{\n+\twq_cnt = counts_to_1000();\n+\treturn 0;\n+}\n+\n+static int tw_cb(struct bpf_map *map, void *key, void *val)\n+{\n+\ttw_cnt = counts_to_1000();\n+\treturn 0;\n+}\n+\n+static int rcu_cb(struct bpf_map *map, void *key, void *val)\n+{\n+\trcu_cnt = counts_to_1000();\n+\treturn 0;\n+}\n+\n+static int rcu_trace_cb(struct bpf_map *map, void *key, void *val)\n+{\n+\trcu_trace_cnt = counts_to_1000();\n+\treturn 0;\n+}\n+\n+/*\n+ * The time of this prog is up when it starts the callbacks. User space keeps the task on the cpu,\n+ * so the callbacks find what it has left. Each call of a callback is an invocation of its own.\n+ */\n+SEC(\"syscall\")\n+int async_after_timeout(void *ctx)\n+{\n+\tstruct task_struct *task = bpf_get_current_task_btf();\n+\tstruct elem *e, *e1;\n+\tint key = 0, key1 = 1;\n+\n+\te = bpf_map_lookup_elem(\u0026async, \u0026key);\n+\te1 = bpf_map_lookup_elem(\u0026async, \u0026key1);\n+\tif (!e || !e1)\n+\t\treturn 100;\n+\tif (bpf_timer_init(\u0026e-\u003etimer, \u0026async, CLOCK_MONOTONIC) ||\n+\t bpf_timer_set_callback(\u0026e-\u003etimer, timer_cb))\n+\t\treturn 101;\n+\tif (bpf_wq_init(\u0026e-\u003ewq, \u0026async, 0) || bpf_wq_set_callback(\u0026e-\u003ewq, wq_cb, 0))\n+\t\treturn 102;\n+\tif (!spins())\n+\t\treturn 103;\n+\t/* control: the time is up */\n+\tif (counts_to_1000())\n+\t\treturn 109;\n+\tif (bpf_timer_start(\u0026e-\u003etimer, 0, BPF_F_TIMER_CPU_PIN))\n+\t\treturn 104;\n+\tif (bpf_wq_start(\u0026e-\u003ewq, 0))\n+\t\treturn 105;\n+\tif (bpf_task_work_schedule_resume(task, \u0026e-\u003etw, \u0026async, tw_cb))\n+\t\treturn 106;\n+\tif (bpf_call_rcu(\u0026e-\u003ercu, \u0026async, rcu_cb))\n+\t\treturn 107;\n+\tif (bpf_call_rcu_tasks_trace(\u0026e1-\u003ercu, \u0026async, rcu_trace_cb))\n+\t\treturn 108;\n+\treturn 0;\n+}\n+\n+int ticks;\n+\n+static int tick_cb(void *map, int *key, struct elem *e)\n+{\n+\tif (counts_to_1000() != 1000)\n+\t\tticks = 1000;\n+\tif (++ticks \u003c 100)\n+\t\tbpf_timer_start(\u0026e-\u003etimer, 20 * 1000 * 1000, BPF_F_TIMER_CPU_PIN);\n+\treturn 0;\n+}\n+\n+/*\n+ * A callback of the prog interrupts it, on the same cpu, each 20 ms. It has its own time. That\n+ * doesn't give more time to the prog. Return how long the loop went on, in ms.\n+ */\n+SEC(\"syscall\")\n+int spin_under_timer(void *ctx)\n+{\n+\tstruct elem *e;\n+\tint key = 1;\n+\t__u64 t0;\n+\n+\te = bpf_map_lookup_elem(\u0026async, \u0026key);\n+\tif (!e)\n+\t\treturn 0;\n+\tif (bpf_timer_init(\u0026e-\u003etimer, \u0026async, CLOCK_MONOTONIC) ||\n+\t bpf_timer_set_callback(\u0026e-\u003etimer, tick_cb) ||\n+\t bpf_timer_start(\u0026e-\u003etimer, 20 * 1000 * 1000, BPF_F_TIMER_CPU_PIN))\n+\t\treturn 0;\n+\tt0 = bpf_ktime_get_ns();\n+\twhile (can_loop)\n+\t\t;\n+\treturn (bpf_ktime_get_ns() - t0) / 1000000;\n+}\n+\n+/*\n+ * Extension prog\n+ */\n+__noinline int replaced_global(int i)\n+{\n+\treturn spin() + i;\n+}\n+\n+SEC(\"tc\")\n+int freplace_target(void *ctx)\n+{\n+\tint i, n = 0;\n+\n+\tfor (i = 0; i \u003c TRIES; i++)\n+\t\tn += replaced_global(zero);\n+\treturn n;\n+}\n+\n+/*\n+ * Tail calls\n+ */\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_PROG_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__uint(key_size, sizeof(int));\n+\t__uint(value_size, sizeof(int));\n+} jmp_table SEC(\".maps\");\n+\n+int tail_callee_spun;\n+\n+SEC(\"tc\")\n+int tail_callee(struct __sk_buff *skb)\n+{\n+\tint i;\n+\n+\ttail_callee_spun = 0;\n+\tfor (i = 0; i \u003c TRIES; i++)\n+\t\ttail_callee_spun += spins();\n+\treturn 0;\n+}\n+\n+static __noinline int tail_calls(struct __sk_buff *skb)\n+{\n+\tbpf_tail_call(skb, \u0026jmp_table, 0);\n+\treturn zero + 100;\n+}\n+\n+/* The target of the tail call is another prog. It returns to this function. */\n+SEC(\"tc\")\n+int tail_call_in_subprog(struct __sk_buff *skb)\n+{\n+\tint i, n;\n+\n+\tn = tail_calls(skb);\n+\tif (counts_to_1000() != 1000)\n+\t\treturn 101;\n+\tfor (i = 0; i \u003c TRIES; i++)\n+\t\tn += spins();\n+\treturn n;\n+}\n+\n+SEC(\"tc\")\n+int tail_call_after_timeout(struct __sk_buff *skb)\n+{\n+\tspins();\n+\tbpf_tail_call(skb, \u0026jmp_table, 0);\n+\treturn 100;\n+}\n+\n+static __noinline int throws(void)\n+{\n+\tspin();\n+\tbpf_throw(7);\n+\treturn 0;\n+}\n+\n+SEC(\"tc\")\n+int throw_after_timeout(void *ctx)\n+{\n+\treturn throws();\n+}\n+\n+/*\n+ * bpf_user_ringbuf_drain()\n+ */\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_USER_RINGBUF);\n+\t__uint(max_entries, 4096);\n+} user_ringbuf SEC(\".maps\");\n+\n+static long drain_cb(struct bpf_dynptr *dynptr, int *spun)\n+{\n+\t*spun += spin();\n+\treturn 0;\n+}\n+\n+SEC(\"syscall\")\n+int drain(void *ctx)\n+{\n+\tint spun = 0;\n+\n+\treturn bpf_user_ringbuf_drain(\u0026user_ringbuf, drain_cb, \u0026spun, 0) * 10 + spun;\n+}\n+\n+/*\n+ * A tracing prog with large frames: they are in a private stack.\n+ */\n+int priv_stack_spun, priv_stack_cb_spun;\n+\n+static __noinline int spins_with_stack(void)\n+{\n+\tvolatile char buf[128] = {};\n+\n+\treturn spin() + buf[zero \u0026 127];\n+}\n+\n+static int priv_stack_cb(__u64 i, int *spun)\n+{\n+\tvolatile char buf[128] = {};\n+\n+\t*spun += spin() + buf[zero \u0026 127];\n+\treturn 0;\n+}\n+\n+SEC(\"fentry/bpf_fentry_test1\")\n+int BPF_PROG(priv_stack)\n+{\n+\tvolatile char buf[128] = {};\n+\tint i, n = buf[zero \u0026 127], spun = 0;\n+\n+\tfor (i = 0; i \u003c TRIES; i++)\n+\t\tn += spins_with_stack();\n+\tbpf_loop(TRIES, priv_stack_cb, \u0026spun, zero);\n+\tpriv_stack_spun = n;\n+\tpriv_stack_cb_spun = spun;\n+\treturn 0;\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c b/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c\nindex e0c389102db9f8..d908298b9fc796 100644\n--- a/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c\n+++ b/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c\n@@ -903,26 +903,28 @@ __naked void unconditional_jump_to_fill(void)\n SEC(\"raw_tp\")\n __arch_x86_64\n __log_level(4)\n-__msg(\"subprog 0 (may_goto_interaction_x86_64) main {{.*}} stack 24\")\n-/* may_goto counter at -24 */\n-__xlated(\"0: *(u64 *)(r10 -24) =\")\n-/* may_goto timestamp at -16 */\n-__xlated(\"1: *(u64 *)(r10 -16) =\")\n-__xlated(\"2: r1 = 1\")\n+__msg(\"subprog 0 (may_goto_interaction_x86_64) main {{.*}} stack 8\")\n+/* may_goto counter and timestamp */\n+__xlated(\"0: r0 = \")\n+__xlated(\"2: r0 = \u0026(void __percpu *)(r0)\")\n __xlated(\"...\")\n-__xlated(\"4: r0 = \u0026(void __percpu *)(r0)\")\n+__xlated(\"6: r1 = 1\")\n+__xlated(\"...\")\n+__xlated(\"8: r0 = \u0026(void __percpu *)(r0)\")\n __xlated(\"...\")\n-/* may_goto expansion starts */\n-__xlated(\"6: r12 = *(u64 *)(r10 -24)\")\n-__xlated(\"7: if r12 == 0x0 goto pc+6\")\n-__xlated(\"8: r12 -= 1\")\n-__xlated(\"9: if r12 != 0x0 goto pc+2\")\n-__xlated(\"10: r12 = -24\")\n-__xlated(\"11: call unknown\")\n-__xlated(\"12: *(u64 *)(r10 -24) = r12\")\n+/* may_goto expansion starts, r0 and r1 are live */\n+__xlated(\"10: r2 = \")\n+__xlated(\"12: r2 = \u0026(void __percpu *)(r2)\")\n+__xlated(\"13: r12 = *(u64 *)(r2 +0)\")\n+__xlated(\"14: if r12 == 0x0 goto pc+6\")\n+__xlated(\"15: r12 -= 1\")\n+__xlated(\"16: if r12 != 0x0 goto pc+2\")\n+__xlated(\"17: r12 = r2\")\n+__xlated(\"18: call unknown\")\n+__xlated(\"19: *(u64 *)(r2 +0) = r12\")\n /* may_goto expansion ends */\n-__xlated(\"13: *(u64 *)(r10 -8) = r1\")\n-__xlated(\"14: exit\")\n+__xlated(\"20: *(u64 *)(r10 -8) = r1\")\n+__xlated(\"21: exit\")\n __success\n __naked void may_goto_interaction_x86_64(void)\n {\n@@ -946,24 +948,26 @@ __arch_arm64\n __arch_riscv64\n __arch_loongarch\n __log_level(4)\n-__msg(\"subprog 0 (may_goto_interaction) main {{.*}} stack 24\")\n-/* may_goto counter at -24 */\n-__xlated(\"0: *(u64 *)(r10 -24) =\")\n-/* may_goto timestamp at -16 */\n-__xlated(\"1: *(u64 *)(r10 -16) =\")\n-__xlated(\"2: r1 = 1\")\n-__xlated(\"3: call bpf_get_smp_processor_id\")\n-/* may_goto expansion starts */\n-__xlated(\"4: r12 = *(u64 *)(r10 -24)\")\n-__xlated(\"5: if r12 == 0x0 goto pc+6\")\n-__xlated(\"6: r12 -= 1\")\n-__xlated(\"7: if r12 != 0x0 goto pc+2\")\n-__xlated(\"8: r12 = -24\")\n-__xlated(\"9: call unknown\")\n-__xlated(\"10: *(u64 *)(r10 -24) = r12\")\n+__msg(\"subprog 0 (may_goto_interaction) main {{.*}} stack 8\")\n+/* may_goto counter and timestamp */\n+__xlated(\"0: r0 = \")\n+__xlated(\"2: r0 = \u0026(void __percpu *)(r0)\")\n+__xlated(\"...\")\n+__xlated(\"6: r1 = 1\")\n+__xlated(\"7: call bpf_get_smp_processor_id\")\n+/* may_goto expansion starts, r0 and r1 are live */\n+__xlated(\"8: r2 = \")\n+__xlated(\"10: r2 = \u0026(void __percpu *)(r2)\")\n+__xlated(\"11: r12 = *(u64 *)(r2 +0)\")\n+__xlated(\"12: if r12 == 0x0 goto pc+6\")\n+__xlated(\"13: r12 -= 1\")\n+__xlated(\"14: if r12 != 0x0 goto pc+2\")\n+__xlated(\"15: r12 = r2\")\n+__xlated(\"16: call unknown\")\n+__xlated(\"17: *(u64 *)(r2 +0) = r12\")\n /* may_goto expansion ends */\n-__xlated(\"11: *(u64 *)(r10 -8) = r1\")\n-__xlated(\"12: exit\")\n+__xlated(\"18: *(u64 *)(r10 -8) = r1\")\n+__xlated(\"19: exit\")\n __success\n __naked void may_goto_interaction(void)\n {\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c b/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c\nindex 1fbcc522830614..4dd98acc6436d4 100644\n--- a/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c\n+++ b/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c\n@@ -553,7 +553,11 @@ int cond_break5(const void *ctx)\n {\n \tint cnt1 = zero, cnt2;\n \n-\tfor (;;) {\n+\t/*\n+\t * There is one time slice for all may_goto of the prog. When it's used up a function that\n+\t * is called after that makes no iteration. So this loop has an end.\n+\t */\n+\tfor (; cnt1 \u003c 100;) {\n \t\tcond_break;\n \t\tcnt1++;\n \t}\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c b/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c\nindex faf9eec0a63ebc..20152a15ca360b 100644\n--- a/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c\n+++ b/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c\n@@ -82,23 +82,31 @@ __naked void may_goto_batch_1(void)\n \n SEC(\"raw_tp\")\n __description(\"may_goto batch with offsets 2/0\")\n+__load_if_JITed()\n __arch_x86_64\n __arch_s390x\n __arch_arm64\n __arch_riscv64\n __arch_loongarch\n-__xlated(\"0: *(u64 *)(r10 -16) = 65535\")\n-__xlated(\"1: *(u64 *)(r10 -8) = 0\")\n-__xlated(\"2: r12 = *(u64 *)(r10 -16)\")\n-__xlated(\"3: if r12 == 0x0 goto pc+6\")\n-__xlated(\"4: r12 -= 1\")\n-__xlated(\"5: if r12 != 0x0 goto pc+2\")\n-__xlated(\"6: r12 = -16\")\n-__xlated(\"7: call unknown\")\n-__xlated(\"8: *(u64 *)(r10 -16) = r12\")\n-__xlated(\"9: r0 = 1\")\n-__xlated(\"10: r0 = 2\")\n-__xlated(\"11: exit\")\n+/* the main prog sets count, timestamp, owner */\n+__xlated(\"0: r0 = \")\n+__xlated(\"2: r0 = \u0026(void __percpu *)(r0)\")\n+__xlated(\"3: *(u64 *)(r0 +0) = 65535\")\n+__xlated(\"4: *(u64 *)(r0 +8) = 0\")\n+__xlated(\"5: *(u64 *)(r0 +16) = 0\")\n+/* may_goto, r0 is dead */\n+__xlated(\"6: r0 = \")\n+__xlated(\"8: r0 = \u0026(void __percpu *)(r0)\")\n+__xlated(\"9: r12 = *(u64 *)(r0 +0)\")\n+__xlated(\"10: if r12 == 0x0 goto pc+6\")\n+__xlated(\"11: r12 -= 1\")\n+__xlated(\"12: if r12 != 0x0 goto pc+2\")\n+__xlated(\"13: r12 = r0\")\n+__xlated(\"14: call unknown\")\n+__xlated(\"15: *(u64 *)(r0 +0) = r12\")\n+__xlated(\"16: r0 = 1\")\n+__xlated(\"17: r0 = 2\")\n+__xlated(\"18: exit\")\n __success\n __naked void may_goto_batch_2(void)\n {\n"
} |
| 2/1 | 2026/11/01 12:37 | agent | patch-evaluator |
3mModel:core Args: null Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_check_timed_may_goto",
"bpf_timed_may_goto_save",
"bpf_timed_may_goto_restore",
"arch_bpf_timed_may_goto"
],
"Reasoning": "The patch modifies the implementation of BPF timed may_goto loops. It switches state management from per-stack-frame storage to a dedicated percpu structure (struct bpf_timed_may_goto), refactors instruction expansion in the verifier fixup pass (may_goto_expand_timed), alters the register calling convention used when calling arch_bpf_timed_may_goto on x86_64, and adds save/restore logic for async callbacks (timers, workqueues, task_work, RCU). This code is reachable from user space on amd64 via standard bpf() syscall operations (BPF_PROG_LOAD and program execution with may_goto instructions), making it suitable and important for 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 51ba9bdea0ef67afe96b0bfb46fdbcdf514999af
Author: syz-cluster <triage@syzkaller.com>
Date: Sun Nov 1 12:37:55 2026 +0000
syz-cluster: applied patch under review
diff --git a/arch/arm64/net/bpf_timed_may_goto.S b/arch/arm64/net/bpf_timed_may_goto.S
index 1b4a4f91ffc90c..f8732b13bb1e58 100644
--- a/arch/arm64/net/bpf_timed_may_goto.S
+++ b/arch/arm64/net/bpf_timed_may_goto.S
@@ -17,16 +17,10 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)
str x7, [sp, #80]
/*
- * Stack depth was passed in BPF_REG_AX (x9), add it to the BPF_FP
- * (x25) to get the pointer to count and timestamp and pass it as the
- * first argument in x0.
- *
- * Before generating the call to arch_bpf_timed_may_goto, the verifier
- * generates a load instruction using FP, i.e. REG_AX = *(u64 *)(FP -
- * stack_off_cnt), so BPF_REG_FP (x25) is always set up by the arm64
- * jit in this case.
+ * The pointer to count and timestamp was passed in BPF_REG_AX (x9),
+ * pass it as the first argument in x0.
*/
- add x0, x9, x25
+ mov x0, x9
bl bpf_check_timed_may_goto
/* BPF_REG_AX(x9) will be stored into count, so move return value to it. */
mov x9, x0
diff --git a/arch/loongarch/net/bpf_timed_may_goto.S b/arch/loongarch/net/bpf_timed_may_goto.S
index fa128acdb108a4..4aaca029404bcf 100644
--- a/arch/loongarch/net/bpf_timed_may_goto.S
+++ b/arch/loongarch/net/bpf_timed_may_goto.S
@@ -22,14 +22,10 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)
st.d a4, sp, 48
/*
- * BPF_REG_AX (t0) holds the offset passed in by the verifier;
- * add it to BPF_REG_FP (s4) to get the pointer to the count and
- * timestamp, then pass it as the first argument in a0.
- *
- * The verifier emits a load using FP right before this call,
- * so BPF_REG_FP (s4) is always set up by the JIT in this case.
+ * BPF_REG_AX (t0) holds the pointer to the count and timestamp;
+ * pass it as the first argument in a0.
*/
- add.d a0, t0, s4
+ move a0, t0
bl bpf_check_timed_may_goto
/* BPF_REG_AX (t0) will be stored into count, so move the return value to it. */
move t0, a0
diff --git a/arch/powerpc/net/bpf_timed_may_goto.S b/arch/powerpc/net/bpf_timed_may_goto.S
index 84ecf6fa7f5dcf..b8ae9885eb31cb 100644
--- a/arch/powerpc/net/bpf_timed_may_goto.S
+++ b/arch/powerpc/net/bpf_timed_may_goto.S
@@ -8,7 +8,7 @@
* arch_bpf_timed_may_goto() trampoline for powerpc64
*
* Custom BPF convention (verifier/JIT):
- * - input: stack offset in BPF_REG_AX (r12)
+ * - input: pointer to count and timestamp in BPF_REG_AX (r12)
* - output: updated count in BPF_REG_AX (r12)
*
* Call bpf_check_timed_may_goto(ptr) with normal powerpc64 ABI:
@@ -31,11 +31,8 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)
std r7, 64(r1)
std r8, 72(r1)
- /*
- * r3 = BPF_REG_FP + BPF_REG_AX
- * BPF_REG_FP is r31; BPF_REG_AX is r12 (stack offset in bytes).
- */
- add r3, r31, r12
+ /* r3 = BPF_REG_AX (r12) */
+ mr r3, r12
bl CFUNC(bpf_check_timed_may_goto)
/* Put return value back into AX */
diff --git a/arch/riscv/net/bpf_timed_may_goto.S b/arch/riscv/net/bpf_timed_may_goto.S
index 6ec37f634aff63..2ce1c89c913e55 100644
--- a/arch/riscv/net/bpf_timed_may_goto.S
+++ b/arch/riscv/net/bpf_timed_may_goto.S
@@ -6,13 +6,11 @@
/*
* Trampoline for the BPF timed may_goto loop bound. Custom calling convention:
- * - input: stack offset in BPF_REG_AX (t0)
+ * - input: pointer to count and timestamp in BPF_REG_AX (t0)
* - output: updated count in BPF_REG_AX (t0)
*
- * Calls bpf_check_timed_may_goto(ptr) with the standard RISC-V ABI, where
- * ptr = BPF_REG_FP (s5) + BPF_REG_AX (t0). BPF R0-R5 (t6, a0-a4) and stack
- * arguments 6-8 (a5-a7) are saved across the call; BPF_REG_FP (s5) is
- * callee-saved and needs no saving.
+ * Calls bpf_check_timed_may_goto(ptr) with the standard RISC-V ABI. BPF R0-R5
+ * (t6, a0-a4) and stack arguments 6-8 (a5-a7) are saved across the call.
*/
SYM_FUNC_START(arch_bpf_timed_may_goto)
@@ -33,7 +31,7 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)
REG_S a6, 7*SZREG(sp)
REG_S a7, 8*SZREG(sp)
- add a0, t0, s5
+ mv a0, t0
call bpf_check_timed_may_goto
mv t0, a0
diff --git a/arch/s390/net/bpf_jit_comp.c b/arch/s390/net/bpf_jit_comp.c
index 56ec49894dda38..15029cedc50baf 100644
--- a/arch/s390/net/bpf_jit_comp.c
+++ b/arch/s390/net/bpf_jit_comp.c
@@ -1005,6 +1005,17 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
(jit->prg - patch_brc) >> 1;
break;
}
+ if (insn_is_mov_percpu_addr(insn)) {
+ const u64 *percpu_offset = &get_lowcore()->percpu_offset;
+
+ if (dst_reg != src_reg)
+ /* lgr %dst,%src */
+ EMIT4(0xb9040000, dst_reg, src_reg);
+ /* ag %dst,percpu_offset */
+ EMIT6_DISP_LH(0xe3000000, 0x0008, dst_reg, REG_0, REG_0,
+ (unsigned long)percpu_offset);
+ break;
+ }
switch (insn->off) {
case 0: /* DST = SRC */
/* lgr %dst,%src */
@@ -1866,9 +1877,9 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
if ((void *)func == arch_bpf_timed_may_goto) {
/*
* arch_bpf_timed_may_goto() has a special ABI: the
- * parameters are in BPF_REG_AX and BPF_REG_10; the
- * return value is in BPF_REG_AX; and all GPRs except
- * REG_W0, REG_W1, and BPF_REG_AX are callee-saved.
+ * parameter is in BPF_REG_AX; the return value is in
+ * BPF_REG_AX; and all GPRs except REG_W0, REG_W1, and
+ * BPF_REG_AX are callee-saved.
*/
/* brasl %r0,func */
@@ -3116,6 +3127,11 @@ bool bpf_jit_supports_timed_may_goto(void)
return true;
}
+bool bpf_jit_supports_percpu_insn(void)
+{
+ return true;
+}
+
bool bpf_jit_inlines_helper_call(s32 imm)
{
switch (imm) {
diff --git a/arch/s390/net/bpf_timed_may_goto.S b/arch/s390/net/bpf_timed_may_goto.S
index 06f567a460d7bd..05f532f8c63903 100644
--- a/arch/s390/net/bpf_timed_may_goto.S
+++ b/arch/s390/net/bpf_timed_may_goto.S
@@ -21,9 +21,9 @@
SYM_FUNC_START(arch_bpf_timed_may_goto)
/*
- * This function has a special ABI: the parameters are in %r12 and
- * %r13; the return value is in %r12; all GPRs except %r0, %r1, and
- * %r12 are callee-saved; and the return address is in %r0.
+ * This function has a special ABI: the parameter is in %r12; the
+ * return value is in %r12; all GPRs except %r0, %r1, and %r12 are
+ * callee-saved; and the return address is in %r0.
*/
stmg %r2,%r5,FRAME_OFF+R2_OFF(%r15)
stg %r14,FRAME_OFF+R14_OFF(%r15)
@@ -33,7 +33,7 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)
lay %r15,-FRAME_SIZE(%r15)
stg %r1,__SF_BACKCHAIN(%r15)
- lay %r2,0(%r12,%r13)
+ lgr %r2,%r12
brasl %r14,bpf_check_timed_may_goto
lgr %r12,%r2
diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index d90b6d238b3a2e..38871963e67671 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -3436,18 +3436,6 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
ip += emit_kfunc_arg_moves(fm, outgoing_arg_base -
outgoing_rsp, &prog);
}
- if (func == (u8 *)arch_bpf_timed_may_goto) {
- u32 fp = priv_frame_ptr ? X86_REG_R9 : BPF_REG_FP;
-
- /*
- * AX has the offset of count and timestamp
- * in the stack. Turn it into a pointer.
- * add r10, rbp or add r10, r9
- */
- maybe_emit_mod(&prog, BPF_REG_AX, fp, true);
- EMIT2(0x01, add_2reg(0xC0, BPF_REG_AX, fp));
- ip += 3;
- }
if (priv_frame_ptr) {
push_r9(&prog);
ip += 2;
diff --git a/arch/x86/net/bpf_timed_may_goto.S b/arch/x86/net/bpf_timed_may_goto.S
index dd2dde89836b8f..da42de7cca9882 100644
--- a/arch/x86/net/bpf_timed_may_goto.S
+++ b/arch/x86/net/bpf_timed_may_goto.S
@@ -25,10 +25,7 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)
/* Save stack argument 6. */
pushq %r9
- /*
- * r10 has the pointer to count and timestamp. JIT added the frame
- * pointer to the offset. Pass it as first argument.
- */
+ /* r10 has the pointer to count and timestamp. Pass it as first argument. */
movq %r10, %rdi
/* Emit call depth accounting for call below. */
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index ff043678129358..01d5c79b487e2e 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1916,6 +1916,8 @@ struct bpf_prog_aux {
const char *attach_func_name;
struct bpf_prog **func;
struct bpf_prog_aux *main_prog_aux;
+ /* main prog: there when the prog has timed may_goto */
+ struct bpf_timed_may_goto __percpu *timed_may_goto;
void *jit_data; /* JIT specific data. arch dependent */
struct bpf_jit_poke_descriptor *poke_tab;
struct bpf_kfunc_desc_tab *kfunc_tab;
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 7baeb831281f38..12ec5b14007fbf 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -964,6 +964,10 @@ struct bpf_subprog_info {
bool is_cb: 1;
bool is_async_cb: 1;
bool is_exception_cb: 1;
+ /* bpf_loop() or bpf_for_each_map_elem() call it in a loop */
+ bool is_loop_cb: 1;
+ /* BPF code, another helper or a kfunc call it */
+ bool is_called: 1;
bool args_cached: 1;
/* true if the return value is passed in the R0:R2 register pair */
bool ret_reg_pair: 1;
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 98254742ae37a2..4f0cd67caccb80 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -758,9 +758,15 @@ struct bpf_prog_stats {
struct u64_stats_sync syncp;
} __aligned(2 * sizeof(u64));
+/*
+ * Per-cpu memory of a prog. All may_goto of one invocation of the prog use it. An invocation is
+ * one call from the kernel into the main prog, with the subprogs and sync callbacks it reaches.
+ */
struct bpf_timed_may_goto {
u64 count;
u64 timestamp;
+ /* the async callback that has set count and timestamp, see bpf_timed_may_goto_save() */
+ void *owner;
};
struct sk_filter {
@@ -1328,6 +1334,8 @@ u64 bpf_arch_uaddress_limit(void);
void arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie);
u64 arch_bpf_timed_may_goto(void);
u64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *);
+void bpf_timed_may_goto_save(const struct bpf_prog *prog, struct bpf_timed_may_goto *saved);
+void bpf_timed_may_goto_restore(const struct bpf_prog *prog, struct bpf_timed_may_goto *saved);
bool bpf_helper_changes_pkt_data(enum bpf_func_id func_id);
static inline bool bpf_dump_raw_ok(const struct cred *cred)
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 7cf37690d41fa4..3b02aafba4c549 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -292,6 +292,7 @@ void __bpf_prog_free(struct bpf_prog *fp)
mutex_destroy(&fp->aux->used_maps_mutex);
mutex_destroy(&fp->aux->dst_mutex);
kfree(fp->aux->poke_tab);
+ free_percpu(fp->aux->timed_may_goto);
kfree(fp->aux);
}
free_percpu(fp->stats);
@@ -3643,6 +3644,38 @@ u64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *p)
return BPF_MAX_TIMED_LOOPS;
}
+/*
+ * The main prog sets count and timestamp in its first insns. A call of an async callback is an
+ * invocation of its own too, but it can interrupt an invocation of the prog on this cpu, or go on
+ * next to one that is preempted. So its caller sets count and timestamp, and puts the old ones
+ * back at the end. Not when another invocation has set them in the meantime: they are for that
+ * one now.
+ * The caller has disabled migration.
+ */
+void bpf_timed_may_goto_save(const struct bpf_prog *prog, struct bpf_timed_may_goto *saved)
+{
+ struct bpf_timed_may_goto *cur;
+
+ if (!prog->aux->timed_may_goto)
+ return;
+ cur = this_cpu_ptr(prog->aux->timed_may_goto);
+ *saved = *cur;
+ cur->count = BPF_MAX_TIMED_LOOPS;
+ cur->timestamp = 0;
+ cur->owner = saved;
+}
+
+void bpf_timed_may_goto_restore(const struct bpf_prog *prog, struct bpf_timed_may_goto *saved)
+{
+ struct bpf_timed_may_goto *cur;
+
+ if (!prog->aux->timed_may_goto)
+ return;
+ cur = this_cpu_ptr(prog->aux->timed_may_goto);
+ if (cur->owner == saved)
+ *cur = *saved;
+}
+
/* for configs without MMU or 32-bit */
__weak const struct bpf_map_ops arena_map_ops;
__weak u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena)
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index 41bc37e205e0f2..a656634fa9e468 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -1803,6 +1803,50 @@ static int may_goto_expand(struct bpf_insn *insn_buf, int off, int stack_off,
return cnt + tail_cnt;
}
+/*
+ * Timed may_goto. All may_goto of a prog use one count and one timestamp: per-cpu memory of the
+ * prog, set in the first insns of the main prog, and by the callers of async callbacks.
+ * @reg holds the address. It is a register that is dead at the may_goto, or R0 that is saved at
+ * @save_off in the stack.
+ */
+static int may_goto_expand_timed(struct bpf_insn *insn_buf, int off,
+ struct bpf_timed_may_goto __percpu *tmg, int reg, int save_off)
+{
+ struct bpf_insn ld_tmg[] = { BPF_LD_IMM64(reg, (__force unsigned long)tmg) };
+ int cnt = 0, jmp, tail_cnt = save_off ? 6 : 5;
+
+ if (save_off)
+ insn_buf[cnt++] = BPF_STX_MEM(BPF_DW, BPF_REG_10, reg, save_off);
+ insn_buf[cnt++] = ld_tmg[0];
+ insn_buf[cnt++] = ld_tmg[1];
+ insn_buf[cnt++] = BPF_MOV64_PERCPU_REG(reg, reg);
+ insn_buf[cnt++] = BPF_LDX_MEM(BPF_DW, BPF_REG_AX, reg, 0);
+ if (save_off) {
+ insn_buf[cnt++] = BPF_JMP_IMM(BPF_JNE, BPF_REG_AX, 0, 2);
+ insn_buf[cnt++] = BPF_LDX_MEM(BPF_DW, reg, BPF_REG_10, save_off);
+ jmp = cnt++;
+ insn_buf[jmp] = BPF_JMP32_A(off >= 0 ? off + tail_cnt : off - jmp);
+ } else {
+ jmp = cnt;
+ off = off >= 0 ? off + tail_cnt : off - jmp;
+ if (off == (s16)off) {
+ insn_buf[cnt++] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_AX, 0, off);
+ } else {
+ insn_buf[cnt++] = BPF_JMP_IMM(BPF_JNE, BPF_REG_AX, 0, 1);
+ insn_buf[cnt++] = BPF_JMP32_A(off >= 0 ? off : off - 1);
+ }
+ }
+ insn_buf[cnt++] = BPF_ALU64_IMM(BPF_SUB, BPF_REG_AX, 1);
+ insn_buf[cnt++] = BPF_JMP_IMM(BPF_JNE, BPF_REG_AX, 0, 2);
+ /* AX is the argument and the return value */
+ insn_buf[cnt++] = BPF_MOV64_REG(BPF_REG_AX, reg);
+ insn_buf[cnt++] = BPF_EMIT_CALL(arch_bpf_timed_may_goto);
+ insn_buf[cnt++] = BPF_STX_MEM(BPF_DW, reg, BPF_REG_AX, 0);
+ if (save_off)
+ insn_buf[cnt++] = BPF_LDX_MEM(BPF_DW, reg, BPF_REG_10, save_off);
+ return cnt;
+}
+
/* Do various post-verification rewrites in a single program pass.
* These rewrites simplify JIT and interpreter implementations.
*/
@@ -1823,6 +1867,9 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
struct bpf_subprog_info *subprogs = env->subprog_info;
u16 stack_depth = subprogs[cur_subprog].stack_depth;
u16 stack_depth_extra = 0;
+ /* arch_bpf_timed_may_goto() takes and returns AX in a cpu register: for JITs only */
+ bool timed_may_goto = prog->jit_requested && bpf_jit_supports_timed_may_goto();
+ struct bpf_timed_may_goto __percpu *tmg = NULL;
if (env->seen_exception && !env->exception_callback_subprog) {
struct bpf_insn *patch = insn_buf;
@@ -2078,38 +2125,28 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
goto next_insn;
}
- if (bpf_is_may_goto_insn(insn) && bpf_jit_supports_timed_may_goto()) {
- int stack_off_cnt = -stack_depth - 16;
- /*
- * AX is used as an argument to pass in stack_off_cnt
- * (to add to r10/fp), and also as the return value of
- * the call to arch_bpf_timed_may_goto.
- */
- struct bpf_insn tail[] = {
- BPF_ALU64_IMM(BPF_SUB, BPF_REG_AX, 1),
- BPF_JMP_IMM(BPF_JNE, BPF_REG_AX, 0, 2),
- BPF_MOV64_IMM(BPF_REG_AX, stack_off_cnt),
- BPF_EMIT_CALL(arch_bpf_timed_may_goto),
- BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_AX, stack_off_cnt),
- };
+ if (bpf_is_may_goto_insn(insn) && timed_may_goto) {
+ u16 live = env->insn_aux_data[i + delta].live_regs_before;
+ int reg, save_off = 0;
- /*
- * Two 8 byte slots, depth-16 stores the count, and
- * depth-8 stores the start timestamp of the loop.
- *
- * The starting value of count is BPF_MAX_TIMED_LOOPS
- * (0xffff). Every iteration loads it and subs it by 1,
- * until the value becomes 0 in AX (thus, 1 in stack),
- * after which we call arch_bpf_timed_may_goto, which
- * either sets AX to 0xffff to keep looping, or to 0
- * upon timeout. AX is then stored into the stack. In
- * the next iteration, we either see 0 and break out, or
- * continue iterating until the next time value is 0
- * after subtraction, rinse and repeat.
- */
- stack_depth_extra = 16;
- cnt = may_goto_expand(insn_buf, insn->off, stack_off_cnt,
- tail, ARRAY_SIZE(tail));
+ /* a register that is dead here holds the address of the count */
+ for (reg = BPF_REG_0; reg <= BPF_REG_9; reg++)
+ if (!(live & BIT(reg)))
+ break;
+ if (reg > BPF_REG_9) {
+ reg = BPF_REG_0;
+ save_off = -stack_depth - 8;
+ stack_depth_extra = 8;
+ }
+ if (!tmg) {
+ tmg = alloc_percpu_gfp(struct bpf_timed_may_goto,
+ GFP_KERNEL_ACCOUNT);
+ if (!tmg)
+ return -ENOMEM;
+ prog->aux->timed_may_goto = tmg;
+ }
+ prog->jit_required = 1;
+ cnt = may_goto_expand_timed(insn_buf, insn->off, tmg, reg, save_off);
new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
if (!new_prog)
@@ -2730,24 +2767,52 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
env->prog->aux->stack_depth = subprogs[0].stack_depth;
for (i = 0; i < env->subprog_cnt; i++) {
- int delta = bpf_jit_supports_timed_may_goto() ? 2 : 1;
int subprog_start = subprogs[i].start;
int stack_slots = subprogs[i].stack_extra / 8;
- int slots = delta, cnt = 0;
-
- if (!stack_slots)
+ /* an invocation begins, see bpf_timed_may_goto_save() for async callbacks */
+ bool main_prog = tmg && !i;
+ /* only a helper calls it, in a loop that ends when it returns 1 */
+ bool loop_cb = tmg && i && subprogs[i].is_loop_cb && !subprogs[i].is_called &&
+ !subprogs[i].is_async_cb;
+ int cnt = 0;
+
+ if (timed_may_goto ? !main_prog && !loop_cb : !stack_slots)
continue;
- /* We need two slots in case timed may_goto is supported. */
- if (stack_slots > slots) {
+ if (stack_slots > 1) {
verifier_bug(env, "stack_slots supports may_goto only");
return -EFAULT;
}
stack_depth = subprogs[i].stack_depth;
- if (bpf_jit_supports_timed_may_goto()) {
- insn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, -stack_depth,
- BPF_MAX_TIMED_LOOPS);
- insn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, -stack_depth + 8, 0);
+ if (timed_may_goto) {
+ struct bpf_insn ld_tmg[] = {
+ BPF_LD_IMM64(BPF_REG_0, (__force unsigned long)tmg)
+ };
+ int count_off = offsetof(struct bpf_timed_may_goto, count);
+ int timestamp_off = offsetof(struct bpf_timed_may_goto, timestamp);
+ int owner_off = offsetof(struct bpf_timed_may_goto, owner);
+
+ /* R0 has nothing at the entry */
+ insn_buf[cnt++] = ld_tmg[0];
+ insn_buf[cnt++] = ld_tmg[1];
+ insn_buf[cnt++] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);
+ if (main_prog) {
+ insn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_0, count_off,
+ BPF_MAX_TIMED_LOOPS);
+ insn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_0, timestamp_off, 0);
+ insn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_0, owner_off, 0);
+ } else {
+ /*
+ * The time is up, but the helper would call the function again and
+ * again. End its loop. The verifier has seen that: the loop can end
+ * after each call.
+ */
+ insn_buf[cnt++] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0,
+ count_off);
+ insn_buf[cnt++] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2);
+ insn_buf[cnt++] = BPF_MOV64_IMM(BPF_REG_0, 1);
+ insn_buf[cnt++] = BPF_EXIT_INSN();
+ }
} else {
/* Add ST insn to subprog prologue to init extra stack */
insn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, -stack_depth,
@@ -2774,7 +2839,7 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
* to insn after BPF_ST that inits may_goto count.
* Adjustment will succeed because bpf_patch_insn_data() didn't fail.
*/
- WARN_ON(adjust_jmp_off(env->prog, subprog_start, delta));
+ WARN_ON(adjust_jmp_off(env->prog, subprog_start, cnt - 1));
}
/* Since poke tab is now finalized, publish aux to tracker. */
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 58e357302850e3..2b75f7ba07a566 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -1222,6 +1222,8 @@ static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)
{
struct bpf_hrtimer *t = container_of(hrtimer, struct bpf_hrtimer, timer);
struct bpf_map *map = READ_ONCE(t->cb.map);
+ struct bpf_prog *prog = READ_ONCE(t->cb.prog);
+ struct bpf_timed_may_goto saved;
void *value = t->cb.value;
bpf_callback_t callback_fn;
void *key;
@@ -1229,7 +1231,7 @@ static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)
BTF_TYPE_EMIT(struct bpf_timer);
callback_fn = rcu_dereference_check(t->cb.callback_fn, rcu_read_lock_bh_held());
- if (!callback_fn || !map)
+ if (!callback_fn || !map || !prog)
goto out;
/* bpf_timer_cb() runs in hrtimer_run_softirq. It doesn't migrate and
@@ -1242,8 +1244,10 @@ static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)
key = map_key_from_value(map, value, &idx);
+ bpf_timed_may_goto_save(prog, &saved);
callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);
/* The verifier checked that return value is zero. */
+ bpf_timed_may_goto_restore(prog, &saved);
this_cpu_write(hrtimer_running, NULL);
out:
@@ -1254,6 +1258,8 @@ static void bpf_wq_work(struct work_struct *work)
{
struct bpf_work *w = container_of(work, struct bpf_work, work);
struct bpf_async_cb *cb = &w->cb;
+ struct bpf_timed_may_goto saved;
+ struct bpf_prog *prog;
struct bpf_map *map;
bpf_callback_t callback_fn;
void *value = cb->value;
@@ -1267,14 +1273,17 @@ static void bpf_wq_work(struct work_struct *work)
map = READ_ONCE(cb->map);
callback_fn = READ_ONCE(cb->callback_fn);
- if (!callback_fn || !map)
+ prog = READ_ONCE(cb->prog);
+ if (!callback_fn || !map || !prog)
goto out;
key = map_key_from_value(map, value, &idx);
migrate_disable();
+ bpf_timed_may_goto_save(prog, &saved);
callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);
+ bpf_timed_may_goto_restore(prog, &saved);
migrate_enable();
out:
@@ -4593,6 +4602,7 @@ static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx,
static void bpf_task_work_callback(struct callback_head *cb)
{
struct bpf_task_work_ctx *ctx = container_of(cb, struct bpf_task_work_ctx, work);
+ struct bpf_timed_may_goto saved;
enum bpf_task_work_state state;
u32 idx;
void *key;
@@ -4614,8 +4624,10 @@ static void bpf_task_work_callback(struct callback_head *cb)
key = (void *)map_key_from_value(ctx->map, ctx->map_val, &idx);
migrate_disable();
+ bpf_timed_may_goto_save(ctx->prog, &saved);
ctx->callback_fn((u64)(long)ctx->map, (u64)(long)key,
(u64)(long)ctx->map_val, 0, 0);
+ bpf_timed_may_goto_restore(ctx->prog, &saved);
migrate_enable();
bpf_task_work_ctx_reset(ctx);
@@ -4855,6 +4867,7 @@ static void bpf_rcu_run_callback(struct rcu_head *rcu)
struct bpf_rcu_head_kern *rh = container_of(rcu, struct bpf_rcu_head_kern, rcu);
bpf_callback_t callback_fn = rh->callback_fn;
struct bpf_prog *prog = rh->prog;
+ struct bpf_timed_may_goto saved;
struct bpf_map *map = rh->map;
void *value, *key;
u32 idx;
@@ -4866,7 +4879,9 @@ static void bpf_rcu_run_callback(struct rcu_head *rcu)
smp_store_release(&rh->armed, 0);
rcu_read_lock_dont_migrate();
+ bpf_timed_may_goto_save(prog, &saved);
callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);
+ bpf_timed_may_goto_restore(prog, &saved);
rcu_read_unlock_migrate();
bpf_prog_put(prog);
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 26a5ee0708a3d3..383cdc11b5b45f 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -11469,6 +11469,12 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins
if (err)
return err;
+ if (!bpf_pseudo_kfunc_call(insn) &&
+ (insn->imm == BPF_FUNC_loop || insn->imm == BPF_FUNC_for_each_map_elem))
+ env->subprog_info[subprog].is_loop_cb = true;
+ else
+ env->subprog_info[subprog].is_called = true;
+
/* for callback functions enqueue entry to callback and
* proceed with next instruction within current frame.
*/
@@ -11505,6 +11511,7 @@ static int check_static_func_call(struct bpf_verifier_env *env, int subprog,
int err;
caller = state->frame[state->curframe];
+ env->subprog_info[subprog].is_called = true;
/*
* Track caller's total stack arg count (incoming + max outgoing).
diff --git a/tools/testing/selftests/bpf/libarena/selftests/test_parallel_bitmap.bpf.c b/tools/testing/selftests/bpf/libarena/selftests/test_parallel_bitmap.bpf.c
index ea1fac95b461e5..017add229befe7 100644
--- a/tools/testing/selftests/bpf/libarena/selftests/test_parallel_bitmap.bpf.c
+++ b/tools/testing/selftests/bpf/libarena/selftests/test_parallel_bitmap.bpf.c
@@ -11,6 +11,7 @@
#define TEST_BITMAP_BITS (2 * BITS_PER_LONG_LONG)
#define TEST_BITMAP_SYNC_SPINS BPF_MAX_LOOPS
#define TEST_BITMAP_ITERS 10 * 1000 * 1000
+#define TEST_BITMAP_NSEC (50 * 1000 * 1000)
static struct arena_bitmap __arena *bitmap;
static volatile u64 started;
@@ -129,6 +130,7 @@ static int bitmap_test_common_single(int parity)
static int bitmap_test_common(int parity)
{
+ u64 start;
int ret;
u32 i;
@@ -138,6 +140,7 @@ static int bitmap_test_common(int parity)
if (ret)
return ret;
+ start = bpf_ktime_get_ns();
for (i = zero; i < TEST_BITMAP_ITERS && can_loop; i++) {
ret = bitmap_test_common_single(parity);
if (ret)
@@ -147,6 +150,13 @@ static int bitmap_test_common(int parity)
break;
bitmap_test_reset_single(parity);
+
+ /*
+ * Stop before the time slice of may_goto is used up. After that the
+ * bmp_*() functions can't retry a cmpxchg that fails.
+ */
+ if (bpf_ktime_get_ns() - start > TEST_BITMAP_NSEC)
+ break;
}
return 0;
diff --git a/tools/testing/selftests/bpf/prog_tests/may_goto_far.c b/tools/testing/selftests/bpf/prog_tests/may_goto_far.c
index 8d0ff30bd068cb..b6d262911a3ea3 100644
--- a/tools/testing/selftests/bpf/prog_tests/may_goto_far.c
+++ b/tools/testing/selftests/bpf/prog_tests/may_goto_far.c
@@ -34,8 +34,10 @@ static struct bpf_insn *fill(struct bpf_insn *insn, int cnt, int reg)
* The prog returns 0 when may_goto jumps to its target and 2 when
* it lands in one of 'r0 = r8' areas. There are no constants between
* may_goto and its target to keep the distance when constants are blinded.
+ *
+ * With 'subprog' all of that is a subprog.
*/
-static void test_far(int off)
+static void test_far(int off, bool subprog)
{
LIBBPF_OPTS(bpf_prog_load_opts, opts, .prog_flags = BPF_F_SLEEPABLE);
LIBBPF_OPTS(bpf_test_run_opts, topts);
@@ -50,6 +52,10 @@ static void test_far(int off)
return;
insn = insns;
+ if (subprog) {
+ *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0, 1);
+ *insn++ = BPF_EXIT_INSN();
+ }
*insn++ = BPF_EMIT_CALL(BPF_FUNC_ktime_get_ns);
*insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_0);
*insn++ = BPF_ALU64_IMM(BPF_RSH, BPF_REG_6, 63);
@@ -82,11 +88,19 @@ static void test_far(int off)
void test_may_goto_far(void)
{
if (test__start_subtest("32762"))
- test_far(32762);
+ test_far(32762, false);
if (test__start_subtest("32767"))
- test_far(32767);
+ test_far(32767, false);
if (test__start_subtest("-32767"))
- test_far(-32767);
+ test_far(-32767, false);
if (test__start_subtest("-32768"))
- test_far(-32768);
+ test_far(-32768, false);
+ if (test__start_subtest("subprog 1"))
+ test_far(1, true);
+ if (test__start_subtest("subprog 32767"))
+ test_far(32767, true);
+ if (test__start_subtest("subprog -6"))
+ test_far(-6, true);
+ if (test__start_subtest("subprog -32768"))
+ test_far(-32768, true);
}
diff --git a/tools/testing/selftests/bpf/prog_tests/timed_may_goto.c b/tools/testing/selftests/bpf/prog_tests/timed_may_goto.c
new file mode 100644
index 00000000000000..3da3f33a2898b4
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/timed_may_goto.c
@@ -0,0 +1,195 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <sched.h>
+#include <test_progs.h>
+#include <network_helpers.h>
+#include "timed_may_goto.skel.h"
+#include "timed_may_goto_user.skel.h"
+#include "timed_may_goto_ext.skel.h"
+
+#define TRIES 4
+
+static int run_tc(struct bpf_program *prog)
+{
+ LIBBPF_OPTS(bpf_test_run_opts, opts,
+ .data_in = &pkt_v4,
+ .data_size_in = sizeof(pkt_v4),
+ .repeat = 1,
+ );
+
+ if (!ASSERT_OK(bpf_prog_test_run_opts(bpf_program__fd(prog), &opts), "test_run"))
+ return -1;
+ return opts.retval;
+}
+
+static int run_syscall(struct bpf_program *prog)
+{
+ LIBBPF_OPTS(bpf_test_run_opts, opts);
+
+ if (!ASSERT_OK(bpf_prog_test_run_opts(bpf_program__fd(prog), &opts), "test_run"))
+ return -1;
+ return opts.retval;
+}
+
+static void wait_for(int *val, int expected, const char *name)
+{
+ int i;
+
+ for (i = 0; i < 500 && *(volatile int *)val != expected; i++)
+ usleep(10000);
+ ASSERT_EQ(*val, expected, name);
+}
+
+static void test_async(struct timed_may_goto_user *skel)
+{
+ ASSERT_EQ(run_syscall(skel->progs.async_after_timeout), 0, "async_after_timeout");
+ wait_for(&skel->bss->timer_cnt, 1000, "timer_cnt");
+ wait_for(&skel->bss->wq_cnt, 1000, "wq_cnt");
+ wait_for(&skel->bss->tw_cnt, 1000, "tw_cnt");
+ wait_for(&skel->bss->rcu_cnt, 1000, "rcu_cnt");
+ wait_for(&skel->bss->rcu_trace_cnt, 1000, "rcu_trace_cnt");
+}
+
+static void test_timer_on_top(struct timed_may_goto_user *skel)
+{
+ int ms = run_syscall(skel->progs.spin_under_timer);
+ int ticks = skel->bss->ticks;
+
+ /* stop the timer */
+ skel->bss->ticks = 100;
+ ASSERT_GE(ms, 200, "ms");
+ ASSERT_LE(ms, 500, "ms");
+ /* control: the callback interrupted the prog, and its loop went on to the end */
+ ASSERT_GE(ticks, 5, "ticks");
+ ASSERT_LT(ticks, 100, "ticks");
+}
+
+static void test_freplace(struct timed_may_goto_user *skel)
+{
+ struct timed_may_goto_ext *ext;
+
+ ASSERT_EQ(run_tc(skel->progs.freplace_target), 1, "not replaced");
+
+ ext = timed_may_goto_ext__open();
+ if (!ASSERT_OK_PTR(ext, "ext_open"))
+ return;
+ bpf_program__set_attach_target(ext->progs.replacement,
+ bpf_program__fd(skel->progs.freplace_target),
+ "replaced_global");
+ if (!ASSERT_OK(timed_may_goto_ext__load(ext), "ext_load") ||
+ !ASSERT_OK(timed_may_goto_ext__attach(ext), "ext_attach"))
+ goto out;
+ ASSERT_EQ(run_tc(skel->progs.freplace_target), TRIES, "replaced");
+out:
+ timed_may_goto_ext__destroy(ext);
+ ASSERT_EQ(run_tc(skel->progs.freplace_target), 1, "not replaced");
+}
+
+/* What a prog leaves behind when its time is up doesn't matter for the next one on the cpu. */
+static void test_tail_call_and_throw(struct timed_may_goto_user *skel)
+{
+ int key = 0, fd = bpf_program__fd(skel->progs.tail_callee);
+ int map_fd = bpf_map__fd(skel->maps.jmp_table);
+
+ if (!ASSERT_OK(bpf_map_update_elem(map_fd, &key, &fd, 0), "update"))
+ return;
+
+ ASSERT_EQ(run_tc(skel->progs.normal_prog), 1, "normal_prog");
+ ASSERT_EQ(run_tc(skel->progs.normal_prog), 1, "normal_prog");
+
+ ASSERT_EQ(run_tc(skel->progs.tail_call_in_subprog), 1, "tail_call_in_subprog");
+ ASSERT_EQ(skel->bss->tail_callee_spun, 1, "tail_callee_spun");
+
+ ASSERT_EQ(run_tc(skel->progs.tail_call_after_timeout), 0, "tail_call_after_timeout");
+ ASSERT_EQ(skel->bss->tail_callee_spun, 1, "tail_callee_spun");
+
+ ASSERT_EQ(run_tc(skel->progs.throw_after_timeout), 7, "throw_after_timeout");
+ ASSERT_EQ(run_tc(skel->progs.throw_after_timeout), 7, "throw_after_timeout");
+}
+
+static void test_user_ringbuf(struct timed_may_goto_user *skel)
+{
+ struct user_ring_buffer *rb;
+ void *sample;
+ int i;
+
+ rb = user_ring_buffer__new(bpf_map__fd(skel->maps.user_ringbuf), NULL);
+ if (!ASSERT_OK_PTR(rb, "user_ring_buffer__new"))
+ return;
+ for (i = 0; i < TRIES; i++) {
+ sample = user_ring_buffer__reserve(rb, 8);
+ if (!ASSERT_OK_PTR(sample, "reserve"))
+ goto out;
+ user_ring_buffer__submit(rb, sample);
+ }
+ /* The helper takes a sample out of the buffer for each call: all calls have to run. */
+ ASSERT_EQ(run_syscall(skel->progs.drain), TRIES * 10 + 1, "drain");
+out:
+ user_ring_buffer__free(rb);
+}
+
+static void test_priv_stack(struct timed_may_goto_user *skel)
+{
+ LIBBPF_OPTS(bpf_test_run_opts, opts);
+ int fd = bpf_program__fd(skel->progs.priv_stack);
+ struct bpf_link *link;
+
+ link = bpf_program__attach(skel->progs.priv_stack);
+ if (!ASSERT_OK_PTR(link, "attach"))
+ return;
+ ASSERT_OK(bpf_prog_test_run_opts(fd, &opts), "test_run");
+ ASSERT_EQ(skel->bss->priv_stack_spun, 1, "priv_stack_spun");
+ ASSERT_EQ(skel->bss->priv_stack_cb_spun, 0, "priv_stack_cb_spun");
+ bpf_link__destroy(link);
+}
+
+/*
+ * Serial: most progs spin for 250 ms in an RCU read-side critical section, one after the other.
+ * That delays what other tests wait for. In parallel mode "mmap" gives up its wait for the end of
+ * a map.
+ */
+void serial_test_timed_may_goto(void)
+{
+ struct timed_may_goto_user *skel;
+ cpu_set_t old, new;
+
+ /* The interpreter counts in each frame. */
+ if (!is_jit_enabled()) {
+ test__skip();
+ return;
+ }
+
+ RUN_TESTS(timed_may_goto);
+
+#if !defined(__x86_64__) && !defined(__aarch64__)
+ return;
+#endif
+ skel = timed_may_goto_user__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ return;
+
+ /* Count and timestamp are per-cpu: all progs of a subtest run on one cpu. */
+ if (!ASSERT_OK(sched_getaffinity(0, sizeof(old), &old), "getaffinity"))
+ goto out;
+ CPU_ZERO(&new);
+ CPU_SET(sched_getcpu(), &new);
+ if (!ASSERT_OK(sched_setaffinity(0, sizeof(new), &new), "setaffinity"))
+ goto out;
+
+ if (test__start_subtest("async"))
+ test_async(skel);
+ if (test__start_subtest("timer_on_top"))
+ test_timer_on_top(skel);
+ if (test__start_subtest("freplace"))
+ test_freplace(skel);
+ if (test__start_subtest("tail_call_and_throw"))
+ test_tail_call_and_throw(skel);
+ if (test__start_subtest("user_ringbuf"))
+ test_user_ringbuf(skel);
+ if (test__start_subtest("priv_stack"))
+ test_priv_stack(skel);
+
+ sched_setaffinity(0, sizeof(old), &old);
+out:
+ timed_may_goto_user__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/arena_htab.c b/tools/testing/selftests/bpf/progs/arena_htab.c
index 81eaa94afeb01c..80cc14bb1c0f94 100644
--- a/tools/testing/selftests/bpf/progs/arena_htab.c
+++ b/tools/testing/selftests/bpf/progs/arena_htab.c
@@ -34,8 +34,6 @@ int arena_htab_llvm(void *ctx)
cast_kern(htab);
htab_init(htab);
- cast_kern(arr);
-
/* first run. No old elems in the table */
for (i = zero; i < 100000 && can_loop; i++) {
htab_update_elem(htab, i, i);
diff --git a/tools/testing/selftests/bpf/progs/timed_may_goto.c b/tools/testing/selftests/bpf/progs/timed_may_goto.c
new file mode 100644
index 00000000000000..c1c48af808a99c
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/timed_may_goto.c
@@ -0,0 +1,898 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * There is one time slice of may_goto for an invocation of a prog: one call from the kernel into
+ * the main prog, with the subprogs and sync callbacks that it reaches.
+ *
+ * Most tests call a function that spins until may_goto stops it, four times, each in a different
+ * way. Only the first call can spin. With a time slice for each call all four do.
+ */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+#include "bpf_experimental.h"
+
+char _license[] SEC("license") = "GPL";
+
+#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)) && __clang_major__ >= 18
+
+#define TRIES 4
+
+int zero;
+
+/* Return 1 if the loop went on until the time was up. */
+static __always_inline int spin(void)
+{
+ __u64 t0 = bpf_ktime_get_ns();
+
+ while (can_loop)
+ ;
+ return bpf_ktime_get_ns() - t0 > 100 * 1000 * 1000;
+}
+
+static __noinline int spins(void)
+{
+ return spin();
+}
+
+static __noinline int counts_to_1000(void)
+{
+ int i, cnt = zero;
+
+ for (i = zero; i < 1000 && can_loop; i++)
+ cnt++;
+ return cnt;
+}
+
+SEC("tc")
+__description("timed may_goto: call")
+__success __retval(1)
+int call_subprog(void *ctx)
+{
+ int i, n = 0;
+
+ for (i = 0; i < TRIES; i++)
+ n += spins();
+ return n;
+}
+
+SEC("tc")
+__description("timed may_goto: a call after the time is up makes no iteration")
+__success __retval(0)
+int short_loop_after_timeout(void *ctx)
+{
+ spins();
+ return counts_to_1000();
+}
+
+SEC("tc")
+__description("timed may_goto: the loop of the main prog stops when the time is up in a subprog")
+__success __retval(1)
+int main_loop_after_timeout(void *ctx)
+{
+ int n = 0;
+
+ while (can_loop && n < 2 * TRIES)
+ n += spins();
+ return n;
+}
+
+static __noinline int passes(int i)
+{
+ return spins() + i;
+}
+
+SEC("tc")
+__description("timed may_goto: call through a function without a loop")
+__success __retval(1)
+int call_through(void *ctx)
+{
+ int i, n = 0;
+
+ for (i = 0; i < TRIES; i++)
+ n += passes(zero);
+ return n;
+}
+
+__noinline int global_spins(void)
+{
+ return spin();
+}
+
+SEC("tc")
+__description("timed may_goto: call of a global function")
+__success __retval(1)
+int call_global(void *ctx)
+{
+ int i, n = 0;
+
+ for (i = 0; i < TRIES; i++)
+ n += global_spins();
+ return n;
+}
+
+SEC("tc")
+__description("timed may_goto: callx")
+__success __retval(1)
+__naked void callx_subprog(void)
+{
+ asm volatile (
+ "r6 = 0;"
+ "r7 = 0;"
+ "1:"
+ "r3 = %[spins] ll;"
+ "callx r3;"
+ "r6 += r0;"
+ "r7 += 1;"
+ "if r7 < 4 goto 1b;"
+ "r0 = r6;"
+ "exit;"
+ :
+ : __imm_addr(spins)
+ : __clobber_all);
+}
+
+/* With a time slice for each call it gives up, so that the test fails and doesn't lock up. */
+static int loop_cb(__u64 i, int *spun)
+{
+ *spun += spin();
+ return *spun > 2 * TRIES;
+}
+
+SEC("tc")
+__description("timed may_goto: bpf_loop() inlined")
+__success __retval(1)
+int loop_inlined(void *ctx)
+{
+ int spun = 0;
+
+ bpf_loop(1 << 20, loop_cb, &spun, 0);
+ return spun;
+}
+
+SEC("tc")
+__description("timed may_goto: bpf_loop() not inlined")
+__success __retval(1)
+int loop_not_inlined(void *ctx)
+{
+ int spun = 0;
+
+ bpf_loop(1 << 20, loop_cb, &spun, zero);
+ return spun;
+}
+
+static int count_cb(__u64 i, int *calls)
+{
+ (*calls)++;
+ return 0;
+}
+
+/* The time is up: the helper calls the callback one more time. It returns 1 in its first insns. */
+#define LOOP_ENDS(name, flags) \
+SEC("tc") \
+__description("timed may_goto: bpf_loop() " #name " ends when the time is up") \
+__success __retval(10) \
+int loop_ends_##name(void *ctx) \
+{ \
+ int calls = 0; \
+ \
+ spins(); \
+ return bpf_loop(1000, count_cb, &calls, flags) * 10 + calls; \
+}
+
+LOOP_ENDS(inlined, 0)
+LOOP_ENDS(not_inlined, zero)
+
+struct nested {
+ __u64 t0;
+ int spun;
+};
+
+/* Without an end of bpf_loop(): give up, so that the test fails and doesn't lock up. */
+static int inner_cb(__u64 i, struct nested *n)
+{
+ if (!n->spun)
+ n->spun = spin();
+ return (i & 1023) == 0 && bpf_ktime_get_ns() - n->t0 > 1000 * 1000 * 1000;
+}
+
+static int outer_cb(__u64 i, struct nested *n)
+{
+ bpf_loop(1 << 23, inner_cb, n, 0);
+ return bpf_ktime_get_ns() - n->t0 > 1000 * 1000 * 1000;
+}
+
+SEC("tc")
+__description("timed may_goto: bpf_loop() in bpf_loop() ends when the time is up")
+__success __retval(1)
+int loop_in_loop(void *ctx)
+{
+ struct nested n = { .t0 = bpf_ktime_get_ns() };
+
+ bpf_loop(1 << 23, outer_cb, &n, 0);
+ return n.spun && bpf_ktime_get_ns() - n.t0 < 500 * 1000 * 1000;
+}
+
+static __noinline int called_cb(__u64 i, int *calls)
+{
+ (*calls)++;
+ return 0;
+}
+
+/* BPF code calls the callback too, so its return value has to be its own. bpf_loop() goes on. */
+SEC("tc")
+__description("timed may_goto: bpf_loop() with a callback that is called as well")
+__success __retval(45)
+int loop_and_call(void *ctx)
+{
+ int calls = 0;
+
+ spins();
+ if (called_cb(zero, &calls))
+ return 100;
+ return bpf_loop(TRIES, called_cb, &calls, 0) * 10 + calls;
+}
+
+static int short_cb(__u64 i, __u64 *t0)
+{
+ int j;
+
+ for (j = zero; j < 60000 && can_loop; j++)
+ ;
+ return (i & 1023) == 0 && bpf_ktime_get_ns() - *t0 > 1000 * 1000 * 1000;
+}
+
+/*
+ * The main prog has no may_goto. Each call of the callback ends before it reads the clock. The
+ * iterations of all calls add up.
+ */
+SEC("tc")
+__description("timed may_goto: bpf_loop() the callback makes 60000 iterations")
+__success __retval(1)
+int loop_short_cb(void *ctx)
+{
+ __u64 t0 = bpf_ktime_get_ns();
+
+ bpf_loop(1 << 20, short_cb, &t0, 0);
+ return bpf_ktime_get_ns() - t0 < 1000 * 1000 * 1000;
+}
+
+/* spins() uses up the time. The loop that calls it ends at its next may_goto. */
+static __noinline int loops_around_spins(void)
+{
+ int n = zero;
+
+ while (can_loop && n < 20000) {
+ if (n < 2 * TRIES)
+ spins();
+ n++;
+ }
+ return n;
+}
+
+SEC("tc")
+__description("timed may_goto: a loop that is on the stack ends when the time is up")
+__success __retval(1)
+int rest_of_batch(void *ctx)
+{
+ return loops_around_spins();
+}
+
+static __noinline int one_iteration(void)
+{
+ int i, n = zero;
+
+ for (i = zero; i < 1 && can_loop; i++)
+ n++;
+ return n;
+}
+
+long calls_ok;
+
+static int call_one(__u64 i, __u64 *t0)
+{
+ if (!one_iteration())
+ return 1;
+ calls_ok++;
+ /* With a time slice for each call: give up. */
+ return (i & 1023) == 0 && bpf_ktime_get_ns() - *t0 > 1000 * 1000 * 1000;
+}
+
+static int call_many(__u64 i, __u64 *t0)
+{
+ long before = calls_ok;
+
+ bpf_loop(1 << 23, call_one, t0, 0);
+ return calls_ok - before != 1 << 23;
+}
+
+/*
+ * The iterations of all calls count: the prog reads the clock in each 65535th call. The call that
+ * finds out that the time is up makes its iteration.
+ */
+SEC("tc")
+__description("timed may_goto: 65535 calls that make one iteration read the clock")
+__success __retval(1)
+int clock_after_65535_calls(void *ctx)
+{
+ __u64 t0 = bpf_ktime_get_ns();
+
+ calls_ok = 0;
+ bpf_loop(1000, call_many, &t0, 0);
+ return calls_ok > 0 && calls_ok % 65535 == 0;
+}
+
+#define MAP(name, map_type, flags) \
+struct { \
+ __uint(type, map_type); \
+ __uint(max_entries, TRIES); \
+ __uint(map_flags, flags); \
+ __type(key, int); \
+ __type(value, int); \
+} name SEC(".maps")
+
+MAP(array, BPF_MAP_TYPE_ARRAY, 0);
+MAP(hash, BPF_MAP_TYPE_HASH, 0);
+
+/* The first call uses up the time. The second call ends the loop of the helper. */
+static int elem_cb(void *map, int *key, int *val, int *spun)
+{
+ *spun += spin();
+ return 0;
+}
+
+#define FOR_EACH(name) \
+SEC("tc") \
+__description("timed may_goto: bpf_for_each_map_elem() " #name) \
+__success __retval(21) \
+int for_each_##name(void *ctx) \
+{ \
+ int i, key, spun = 0; \
+ \
+ for (i = 0; i < TRIES; i++) { \
+ key = i; \
+ bpf_map_update_elem(&name, &key, &key, 0); \
+ } \
+ return bpf_for_each_map_elem(&name, elem_cb, &spun, 0) * 10 + spun; \
+}
+
+FOR_EACH(array)
+FOR_EACH(hash)
+
+static long vma_cb(struct task_struct *task, struct vm_area_struct *vma, int *spun)
+{
+ *spun += spin();
+ return 0;
+}
+
+SEC("raw_tp")
+__description("timed may_goto: bpf_find_vma()")
+__success __retval(1)
+int find_vma(void *ctx)
+{
+ struct task_struct *task = bpf_get_current_task_btf();
+ __u64 addr = task->mm->arg_start;
+ int i, spun = 0;
+
+ for (i = 0; i < TRIES; i++)
+ if (bpf_find_vma(task, addr, vma_cb, &spun, 0))
+ return 100;
+ return spun;
+}
+
+struct tree_node {
+ struct bpf_rb_node rb;
+ int key;
+};
+
+#define private(name) SEC(".data." #name) __hidden __attribute__((aligned(8)))
+private(A) struct bpf_spin_lock lock;
+private(A) struct bpf_rb_root root __contains(tree_node, rb);
+
+int less_spun, less_calls;
+
+static bool less(struct bpf_rb_node *a, const struct bpf_rb_node *b)
+{
+ long n = zero;
+
+ /* no helpers under the lock */
+ while (can_loop)
+ n++;
+ less_calls++;
+ less_spun += n > 1000 * 1000;
+ return container_of(a, struct tree_node, rb)->key <
+ container_of(b, struct tree_node, rb)->key;
+}
+
+SEC("tc")
+__description("timed may_goto: bpf_rbtree_add()")
+__success __retval(1)
+int rbtree_add(void *ctx)
+{
+ struct bpf_rb_node *rb;
+ struct tree_node *n;
+ int i;
+
+ less_spun = less_calls = 0;
+ for (i = 0; i < TRIES; i++) {
+ n = bpf_obj_new(typeof(*n));
+ if (!n)
+ return 100;
+ n->key = TRIES - i;
+ bpf_spin_lock(&lock);
+ bpf_rbtree_add(&root, &n->rb, less);
+ bpf_spin_unlock(&lock);
+ }
+ for (i = 0; i < TRIES; i++) {
+ bpf_spin_lock(&lock);
+ rb = bpf_rbtree_first(&root);
+ if (rb)
+ rb = bpf_rbtree_remove(&root, rb);
+ bpf_spin_unlock(&lock);
+ if (!rb)
+ return 101;
+ bpf_obj_drop(container_of(rb, struct tree_node, rb));
+ }
+ if (less_calls < TRIES - 1)
+ return 102;
+ return less_spun;
+}
+
+/*
+ * All registers are live at the may_goto, so it saves one of them in the stack. They are the same
+ * after 70000 iterations: the loop reads the clock.
+ */
+__naked __noinline __used
+static long regs_across_may_goto(void)
+{
+ asm volatile (
+ "r0 = 10;"
+ "r6 = 6;"
+ "r7 = 7;"
+ "r8 = 8;"
+ "r9 = 0;"
+ "1:"
+ "may_goto 2f;"
+ "r9 += 1;"
+ "if r9 < 70000 goto 1b;"
+ "2:"
+ "r0 += r1;"
+ "r0 += r2;"
+ "r0 += r3;"
+ "r0 += r4;"
+ "r0 += r5;"
+ "r0 += r6;"
+ "r0 += r7;"
+ "r0 += r8;"
+ "r0 += r9;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+SEC("tc")
+__description("timed may_goto: registers across may_goto")
+__success __retval(70046)
+__naked void regs_across_may_goto_main(void)
+{
+ asm volatile (
+ "r1 = 1;"
+ "r2 = 2;"
+ "r3 = 3;"
+ "r4 = 4;"
+ "r5 = 5;"
+ "call regs_across_may_goto;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+/*
+ * may_goto uses a register that is dead. In live_r0_rN() R0 - RN are live at the may_goto and the
+ * next one is not. The values come from memory, so the verifier doesn't know them. Return their sum
+ * and the number of iterations.
+ */
+__u64 vals[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+__u64 zero64;
+
+#define LD0(b) "r0 = *(u64 *)(r" #b " + 0);"
+#define LD1(b) LD0(b) "r1 = *(u64 *)(r" #b " + 8);"
+#define LD2(b) LD1(b) "r2 = *(u64 *)(r" #b " + 16);"
+#define LD3(b) LD2(b) "r3 = *(u64 *)(r" #b " + 24);"
+#define LD4(b) LD3(b) "r4 = *(u64 *)(r" #b " + 32);"
+#define LD5(b) LD4(b) "r5 = *(u64 *)(r" #b " + 40);"
+#define LD6(b) LD5(b) "r6 = *(u64 *)(r" #b " + 48);"
+#define LD7(b) LD6(b) "r7 = *(u64 *)(r" #b " + 56);"
+#define LD8(b) LD7(b) "r8 = *(u64 *)(r" #b " + 64);"
+
+#define SUM0
+#define SUM1 SUM0 "r0 += r1;"
+#define SUM2 SUM1 "r0 += r2;"
+#define SUM3 SUM2 "r0 += r3;"
+#define SUM4 SUM3 "r0 += r4;"
+#define SUM5 SUM4 "r0 += r5;"
+#define SUM6 SUM5 "r0 += r6;"
+#define SUM7 SUM6 "r0 += r7;"
+#define SUM8 SUM7 "r0 += r8;"
+
+/* The count of iterations is at r10 - 8. It begins with a number from memory too. */
+#define COUNT_INIT(t) \
+ "r" #t " = %[zero64] ll;" \
+ "r" #t " = *(u64 *)(r" #t " + 0);" \
+ "*(u64 *)(r10 - 8) = r" #t ";"
+#define COUNT_AND_LOOP(t) \
+ "r" #t " = *(u64 *)(r10 - 8);" \
+ "r" #t " += 1;" \
+ "*(u64 *)(r10 - 8) = r" #t ";" \
+ "if r" #t " < 70000 goto 1b;"
+#define ADD_COUNT_AND_EXIT \
+ "r1 = *(u64 *)(r10 - 8);" \
+ "r0 += r1;" \
+ "exit;" \
+ : \
+ : __imm_addr(vals), __imm_addr(zero64) \
+ : __clobber_all
+
+#define LIVE_REGS(n, next) \
+__naked __noinline __used \
+static long live_r0_r##n(void) \
+{ \
+ asm volatile ( \
+ COUNT_INIT(next) \
+ "r" #n " = %[vals] ll;" \
+ LD##n(n) \
+ "1:" \
+ "may_goto 2f;" \
+ COUNT_AND_LOOP(next) \
+ "2:" \
+ SUM##n \
+ ADD_COUNT_AND_EXIT); \
+}
+
+LIVE_REGS(0, 1)
+LIVE_REGS(1, 2)
+LIVE_REGS(2, 3)
+LIVE_REGS(3, 4)
+LIVE_REGS(4, 5)
+LIVE_REGS(5, 6)
+LIVE_REGS(6, 7)
+LIVE_REGS(7, 8)
+LIVE_REGS(8, 9)
+
+/* R2 is read at the top of the loop only: it is live at the may_goto through the back edge. */
+__naked __noinline __used
+static long live_back_edge(void)
+{
+ asm volatile (
+ COUNT_INIT(3)
+ "r2 = %[vals] ll;"
+ LD2(2)
+ "1:"
+ "*(u64 *)(r10 - 16) = r2;"
+ "may_goto 2f;"
+ COUNT_AND_LOOP(3)
+ "2:"
+ "r0 += r1;"
+ "r2 = *(u64 *)(r10 - 16);"
+ "r0 += r2;"
+ ADD_COUNT_AND_EXIT);
+}
+
+/* R2 is read only where the may_goto jumps to. */
+__naked __noinline __used
+static long live_target(void)
+{
+ asm volatile (
+ COUNT_INIT(3)
+ "r2 = %[vals] ll;"
+ LD2(2)
+ "1:"
+ "may_goto 2f;"
+ COUNT_AND_LOOP(3)
+ "goto 3f;"
+ "2:"
+ "r0 += r2;"
+ "3:"
+ "r0 += r1;"
+ ADD_COUNT_AND_EXIT);
+}
+
+/* R2 is set in front of the may_goto in each iteration, and read only right behind it. */
+__naked __noinline __used
+static long live_fall_through(void)
+{
+ asm volatile (
+ COUNT_INIT(3)
+ "r1 = %[vals] ll;"
+ LD1(1)
+ "*(u64 *)(r10 - 16) = r0;"
+ "1:"
+ "r2 = %[vals] ll;"
+ "r2 = *(u64 *)(r2 + 16);"
+ "may_goto 2f;"
+ "*(u64 *)(r10 - 16) = r2;"
+ COUNT_AND_LOOP(3)
+ "2:"
+ "r0 += r1;"
+ "r2 = *(u64 *)(r10 - 16);"
+ "r0 += r2;"
+ ADD_COUNT_AND_EXIT);
+}
+
+#define CALL_AND_ADD(f) "call " #f ";" "r6 += r0;"
+#define CALL_ALL_LIVE \
+ "r6 = 0;" \
+ CALL_AND_ADD(live_r0_r0) \
+ CALL_AND_ADD(live_r0_r1) \
+ CALL_AND_ADD(live_r0_r2) \
+ CALL_AND_ADD(live_r0_r3) \
+ CALL_AND_ADD(live_r0_r4) \
+ CALL_AND_ADD(live_r0_r5) \
+ CALL_AND_ADD(live_r0_r6) \
+ CALL_AND_ADD(live_r0_r7) \
+ CALL_AND_ADD(live_r0_r8) \
+ CALL_AND_ADD(live_back_edge) \
+ CALL_AND_ADD(live_target) \
+ CALL_AND_ADD(live_fall_through) \
+ "r0 = r6;" \
+ "exit;"
+
+/*
+ * The sums: 1 + 3 + 6 + 10 + 15 + 21 + 28 + 36 + 45 = 165. Then 6, 3 (the may_goto doesn't jump)
+ * and 6. 12 * 70000 iterations: each function reads the clock.
+ */
+SEC("tc")
+__description("timed may_goto: may_goto takes a register that is dead")
+__success __retval(840180)
+__naked void dead_reg(void)
+{
+ asm volatile (
+ CALL_ALL_LIVE
+ ::: __clobber_all);
+}
+
+/* No iteration: 165, then 6, 6 (the may_goto jumps) and 4 (R2 isn't stored). */
+SEC("tc")
+__description("timed may_goto: may_goto takes a register that is dead, the time is up")
+__success __retval(181)
+__naked void dead_reg_time_is_up(void)
+{
+ asm volatile (
+ "call spins_on_may_goto;"
+ CALL_ALL_LIVE
+ ::: __clobber_all);
+}
+
+/*
+ * The subprog uses up the time of the prog and returns. With a time slice for each call the caller
+ * calls it 65535 times before it reads the clock.
+ */
+__naked __noinline __used
+static unsigned long spins_on_may_goto(void)
+{
+ asm volatile (
+ "1:"
+ "may_goto 2f;"
+ "goto 1b;"
+ "2:"
+ "r0 = 0;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+/* The first may_goto of the function finds that the time is up */
+SEC("tc")
+__description("timed may_goto: registers across may_goto when the time is up")
+__success __retval(46)
+__naked void regs_when_time_is_up(void)
+{
+ asm volatile (
+ "call spins_on_may_goto;"
+ "r1 = 1;"
+ "r2 = 2;"
+ "r3 = 3;"
+ "r4 = 4;"
+ "r5 = 5;"
+ "call regs_across_may_goto;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+/* The first insn of the main prog is the target of a jump. It must not set the count again. */
+SEC("socket")
+__description("timed may_goto: 9 insns")
+__success __retval(1)
+__naked void loop_calls_subprog_with_may_goto(void)
+{
+ asm volatile (
+ "1:"
+ "call spins_on_may_goto;"
+ "may_goto 2f;"
+ "goto 1b;"
+ "2:"
+ "r0 = 1;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+/*
+ * These subprogs make 60000 iterations in each call. With a read of the clock after 65535
+ * iterations of a call none of them reads it, and their caller does it after
+ * 65535 * 60000 * 60000 iterations.
+ */
+__naked __noinline __used
+static unsigned long loops_60000(void)
+{
+ asm volatile (
+ "r1 = 60000;"
+ "1:"
+ "may_goto 2f;"
+ "r1 += -1;"
+ "if r1 != 0 goto 1b;"
+ "2:"
+ "r0 = 0;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+__naked __noinline __used
+static unsigned long calls_60000_times(void)
+{
+ asm volatile (
+ "r6 = 60000;"
+ "1:"
+ "call loops_60000;"
+ "may_goto 2f;"
+ "r6 += -1;"
+ "if r6 != 0 goto 1b;"
+ "2:"
+ "r0 = 0;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+SEC("socket")
+__description("timed may_goto: three levels of loops that end before they read the clock")
+__success __retval(1)
+__naked void three_levels_of_short_loops(void)
+{
+ asm volatile (
+ "1:"
+ "call calls_60000_times;"
+ "may_goto 2f;"
+ "goto 1b;"
+ "2:"
+ "r0 = 1;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+/* The callee ends one iteration before it reads the clock. */
+__naked __noinline __used
+static unsigned long loops_65534(void)
+{
+ asm volatile (
+ "r1 = 65534;"
+ "1:"
+ "may_goto 2f;"
+ "r1 += -1;"
+ "if r1 != 0 goto 1b;"
+ "2:"
+ "r0 = 0;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+SEC("socket")
+__description("timed may_goto: the subprog makes 65534 iterations")
+__success __retval(1)
+__naked void loop_calls_subprog_with_65534_iterations(void)
+{
+ asm volatile (
+ "call %[bpf_ktime_get_ns];"
+ "r6 = r0;"
+ "1:"
+ "call loops_65534;"
+ "may_goto 2f;"
+ "goto 1b;"
+ "2:"
+ "call %[bpf_ktime_get_ns];"
+ "r0 -= r6;"
+ "r1 = r0;"
+ "r0 = 1;"
+ "if r1 < 1000000000 goto +1;"
+ "r0 = 0;"
+ "exit;"
+ :
+ : __imm(bpf_ktime_get_ns)
+ : __clobber_all);
+}
+
+/* The subprog returns 16 bytes in R0 and R2. may_goto must not change them. */
+__naked __noinline __used
+static unsigned __int128 returns_pair_after_may_goto(void)
+{
+ asm volatile (
+ "r0 = 1;"
+ "r2 = 2;"
+ "1:"
+ "may_goto 2f;"
+ "goto 1b;"
+ "2:"
+ "exit;"
+ ::: __clobber_all);
+}
+
+SEC("socket")
+__description("timed may_goto: the subprog returns 16 bytes")
+__success __retval(3)
+__naked void subprog_with_may_goto_returns_pair(void)
+{
+ asm volatile (
+ "call returns_pair_after_may_goto;"
+ "r0 += r2;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+/* Dead code doesn't count: a prog that has no may_goto left gets no insns. */
+__naked __noinline __used
+static unsigned long may_goto_is_dead_code(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "if r0 == 0 goto 2f;"
+ "1:"
+ "may_goto 2f;"
+ "goto 1b;"
+ "2:"
+ "exit;"
+ ::: __clobber_all);
+}
+
+SEC("socket")
+__description("timed may_goto: may_goto in dead code")
+__success __retval(0)
+__xlated("0: call pc+1")
+__xlated("1: exit")
+__xlated("2: r0 = 0")
+__xlated("3: exit")
+__naked void dead_may_goto(void)
+{
+ asm volatile (
+ "call may_goto_is_dead_code;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+static __noinline int throws_after_spin(void)
+{
+ spin();
+ bpf_throw(0);
+ return 0;
+}
+
+__noinline int exception_cb_loops(u64 cookie)
+{
+ return counts_to_1000();
+}
+
+/* The exception callback belongs to the invocation that throws. */
+SEC("tc")
+__description("timed may_goto: bpf_throw() the exception callback has a loop")
+__exception_cb(exception_cb_loops)
+__success __retval(0)
+int throw_after_timeout(void *ctx)
+{
+ return throws_after_spin();
+}
+
+#else
+
+SEC("socket")
+__description("timed may_goto: not supported dummy test")
+__success
+int dummy_test(void)
+{
+ return 0;
+}
+
+#endif
diff --git a/tools/testing/selftests/bpf/progs/timed_may_goto_ext.c b/tools/testing/selftests/bpf/progs/timed_may_goto_ext.c
new file mode 100644
index 00000000000000..969ca58b6eedcc
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/timed_may_goto_ext.c
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include "bpf_experimental.h"
+
+char _license[] SEC("license") = "GPL";
+
+static __noinline int spins(void)
+{
+ __u64 t0 = bpf_ktime_get_ns();
+
+ while (can_loop)
+ ;
+ return bpf_ktime_get_ns() - t0 > 100 * 1000 * 1000;
+}
+
+/* Each call of an extension prog is an invocation: one of its two calls spins. */
+SEC("freplace")
+int replacement(int i)
+{
+ return spins() + spins() + i;
+}
diff --git a/tools/testing/selftests/bpf/progs/timed_may_goto_user.c b/tools/testing/selftests/bpf/progs/timed_may_goto_user.c
new file mode 100644
index 00000000000000..8b85815d45d25e
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/timed_may_goto_user.c
@@ -0,0 +1,311 @@
+// SPDX-License-Identifier: GPL-2.0
+/* The tests of timed_may_goto.c that need help from user space. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+#include "bpf_experimental.h"
+
+char _license[] SEC("license") = "GPL";
+
+#define TRIES 4
+#define CLOCK_MONOTONIC 1
+
+int zero;
+
+/* Return 1 if the loop went on until the time was up. */
+static __always_inline int spin(void)
+{
+ __u64 t0 = bpf_ktime_get_ns();
+
+ while (can_loop)
+ ;
+ return bpf_ktime_get_ns() - t0 > 100 * 1000 * 1000;
+}
+
+static __noinline int spins(void)
+{
+ return spin();
+}
+
+static __noinline int counts_to_1000(void)
+{
+ int i, cnt = zero;
+
+ for (i = zero; i < 1000 && can_loop; i++)
+ cnt++;
+ return cnt;
+}
+
+/* Nothing special: the loops go on, then the time is up one time. */
+SEC("tc")
+int normal_prog(void *ctx)
+{
+ int i, n = 0;
+
+ if (counts_to_1000() != 1000)
+ return 100;
+ for (i = 0; i < TRIES; i++)
+ n += spins();
+ return n;
+}
+
+/*
+ * Async callbacks
+ */
+struct elem {
+ struct bpf_timer timer;
+ struct bpf_wq wq;
+ struct bpf_task_work tw;
+ struct bpf_rcu_head rcu;
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, 2);
+ __type(key, int);
+ __type(value, struct elem);
+} async SEC(".maps");
+
+int timer_cnt, wq_cnt, tw_cnt, rcu_cnt, rcu_trace_cnt;
+
+static int timer_cb(void *map, int *key, struct elem *e)
+{
+ timer_cnt = counts_to_1000();
+ return 0;
+}
+
+static int wq_cb(void *map, int *key, void *val)
+{
+ wq_cnt = counts_to_1000();
+ return 0;
+}
+
+static int tw_cb(struct bpf_map *map, void *key, void *val)
+{
+ tw_cnt = counts_to_1000();
+ return 0;
+}
+
+static int rcu_cb(struct bpf_map *map, void *key, void *val)
+{
+ rcu_cnt = counts_to_1000();
+ return 0;
+}
+
+static int rcu_trace_cb(struct bpf_map *map, void *key, void *val)
+{
+ rcu_trace_cnt = counts_to_1000();
+ return 0;
+}
+
+/*
+ * The time of this prog is up when it starts the callbacks. User space keeps the task on the cpu,
+ * so the callbacks find what it has left. Each call of a callback is an invocation of its own.
+ */
+SEC("syscall")
+int async_after_timeout(void *ctx)
+{
+ struct task_struct *task = bpf_get_current_task_btf();
+ struct elem *e, *e1;
+ int key = 0, key1 = 1;
+
+ e = bpf_map_lookup_elem(&async, &key);
+ e1 = bpf_map_lookup_elem(&async, &key1);
+ if (!e || !e1)
+ return 100;
+ if (bpf_timer_init(&e->timer, &async, CLOCK_MONOTONIC) ||
+ bpf_timer_set_callback(&e->timer, timer_cb))
+ return 101;
+ if (bpf_wq_init(&e->wq, &async, 0) || bpf_wq_set_callback(&e->wq, wq_cb, 0))
+ return 102;
+ if (!spins())
+ return 103;
+ /* control: the time is up */
+ if (counts_to_1000())
+ return 109;
+ if (bpf_timer_start(&e->timer, 0, BPF_F_TIMER_CPU_PIN))
+ return 104;
+ if (bpf_wq_start(&e->wq, 0))
+ return 105;
+ if (bpf_task_work_schedule_resume(task, &e->tw, &async, tw_cb))
+ return 106;
+ if (bpf_call_rcu(&e->rcu, &async, rcu_cb))
+ return 107;
+ if (bpf_call_rcu_tasks_trace(&e1->rcu, &async, rcu_trace_cb))
+ return 108;
+ return 0;
+}
+
+int ticks;
+
+static int tick_cb(void *map, int *key, struct elem *e)
+{
+ if (counts_to_1000() != 1000)
+ ticks = 1000;
+ if (++ticks < 100)
+ bpf_timer_start(&e->timer, 20 * 1000 * 1000, BPF_F_TIMER_CPU_PIN);
+ return 0;
+}
+
+/*
+ * A callback of the prog interrupts it, on the same cpu, each 20 ms. It has its own time. That
+ * doesn't give more time to the prog. Return how long the loop went on, in ms.
+ */
+SEC("syscall")
+int spin_under_timer(void *ctx)
+{
+ struct elem *e;
+ int key = 1;
+ __u64 t0;
+
+ e = bpf_map_lookup_elem(&async, &key);
+ if (!e)
+ return 0;
+ if (bpf_timer_init(&e->timer, &async, CLOCK_MONOTONIC) ||
+ bpf_timer_set_callback(&e->timer, tick_cb) ||
+ bpf_timer_start(&e->timer, 20 * 1000 * 1000, BPF_F_TIMER_CPU_PIN))
+ return 0;
+ t0 = bpf_ktime_get_ns();
+ while (can_loop)
+ ;
+ return (bpf_ktime_get_ns() - t0) / 1000000;
+}
+
+/*
+ * Extension prog
+ */
+__noinline int replaced_global(int i)
+{
+ return spin() + i;
+}
+
+SEC("tc")
+int freplace_target(void *ctx)
+{
+ int i, n = 0;
+
+ for (i = 0; i < TRIES; i++)
+ n += replaced_global(zero);
+ return n;
+}
+
+/*
+ * Tail calls
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(int));
+ __uint(value_size, sizeof(int));
+} jmp_table SEC(".maps");
+
+int tail_callee_spun;
+
+SEC("tc")
+int tail_callee(struct __sk_buff *skb)
+{
+ int i;
+
+ tail_callee_spun = 0;
+ for (i = 0; i < TRIES; i++)
+ tail_callee_spun += spins();
+ return 0;
+}
+
+static __noinline int tail_calls(struct __sk_buff *skb)
+{
+ bpf_tail_call(skb, &jmp_table, 0);
+ return zero + 100;
+}
+
+/* The target of the tail call is another prog. It returns to this function. */
+SEC("tc")
+int tail_call_in_subprog(struct __sk_buff *skb)
+{
+ int i, n;
+
+ n = tail_calls(skb);
+ if (counts_to_1000() != 1000)
+ return 101;
+ for (i = 0; i < TRIES; i++)
+ n += spins();
+ return n;
+}
+
+SEC("tc")
+int tail_call_after_timeout(struct __sk_buff *skb)
+{
+ spins();
+ bpf_tail_call(skb, &jmp_table, 0);
+ return 100;
+}
+
+static __noinline int throws(void)
+{
+ spin();
+ bpf_throw(7);
+ return 0;
+}
+
+SEC("tc")
+int throw_after_timeout(void *ctx)
+{
+ return throws();
+}
+
+/*
+ * bpf_user_ringbuf_drain()
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_USER_RINGBUF);
+ __uint(max_entries, 4096);
+} user_ringbuf SEC(".maps");
+
+static long drain_cb(struct bpf_dynptr *dynptr, int *spun)
+{
+ *spun += spin();
+ return 0;
+}
+
+SEC("syscall")
+int drain(void *ctx)
+{
+ int spun = 0;
+
+ return bpf_user_ringbuf_drain(&user_ringbuf, drain_cb, &spun, 0) * 10 + spun;
+}
+
+/*
+ * A tracing prog with large frames: they are in a private stack.
+ */
+int priv_stack_spun, priv_stack_cb_spun;
+
+static __noinline int spins_with_stack(void)
+{
+ volatile char buf[128] = {};
+
+ return spin() + buf[zero & 127];
+}
+
+static int priv_stack_cb(__u64 i, int *spun)
+{
+ volatile char buf[128] = {};
+
+ *spun += spin() + buf[zero & 127];
+ return 0;
+}
+
+SEC("fentry/bpf_fentry_test1")
+int BPF_PROG(priv_stack)
+{
+ volatile char buf[128] = {};
+ int i, n = buf[zero & 127], spun = 0;
+
+ for (i = 0; i < TRIES; i++)
+ n += spins_with_stack();
+ bpf_loop(TRIES, priv_stack_cb, &spun, zero);
+ priv_stack_spun = n;
+ priv_stack_cb_spun = spun;
+ return 0;
+}
diff --git a/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c b/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c
index e0c389102db9f8..d908298b9fc796 100644
--- a/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c
+++ b/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c
@@ -903,26 +903,28 @@ __naked void unconditional_jump_to_fill(void)
SEC("raw_tp")
__arch_x86_64
__log_level(4)
-__msg("subprog 0 (may_goto_interaction_x86_64) main {{.*}} stack 24")
-/* may_goto counter at -24 */
-__xlated("0: *(u64 *)(r10 -24) =")
-/* may_goto timestamp at -16 */
-__xlated("1: *(u64 *)(r10 -16) =")
-__xlated("2: r1 = 1")
+__msg("subprog 0 (may_goto_interaction_x86_64) main {{.*}} stack 8")
+/* may_goto counter and timestamp */
+__xlated("0: r0 = ")
+__xlated("2: r0 = &(void __percpu *)(r0)")
__xlated("...")
-__xlated("4: r0 = &(void __percpu *)(r0)")
+__xlated("6: r1 = 1")
+__xlated("...")
+__xlated("8: r0 = &(void __percpu *)(r0)")
__xlated("...")
-/* may_goto expansion starts */
-__xlated("6: r12 = *(u64 *)(r10 -24)")
-__xlated("7: if r12 == 0x0 goto pc+6")
-__xlated("8: r12 -= 1")
-__xlated("9: if r12 != 0x0 goto pc+2")
-__xlated("10: r12 = -24")
-__xlated("11: call unknown")
-__xlated("12: *(u64 *)(r10 -24) = r12")
+/* may_goto expansion starts, r0 and r1 are live */
+__xlated("10: r2 = ")
+__xlated("12: r2 = &(void __percpu *)(r2)")
+__xlated("13: r12 = *(u64 *)(r2 +0)")
+__xlated("14: if r12 == 0x0 goto pc+6")
+__xlated("15: r12 -= 1")
+__xlated("16: if r12 != 0x0 goto pc+2")
+__xlated("17: r12 = r2")
+__xlated("18: call unknown")
+__xlated("19: *(u64 *)(r2 +0) = r12")
/* may_goto expansion ends */
-__xlated("13: *(u64 *)(r10 -8) = r1")
-__xlated("14: exit")
+__xlated("20: *(u64 *)(r10 -8) = r1")
+__xlated("21: exit")
__success
__naked void may_goto_interaction_x86_64(void)
{
@@ -946,24 +948,26 @@ __arch_arm64
__arch_riscv64
__arch_loongarch
__log_level(4)
-__msg("subprog 0 (may_goto_interaction) main {{.*}} stack 24")
-/* may_goto counter at -24 */
-__xlated("0: *(u64 *)(r10 -24) =")
-/* may_goto timestamp at -16 */
-__xlated("1: *(u64 *)(r10 -16) =")
-__xlated("2: r1 = 1")
-__xlated("3: call bpf_get_smp_processor_id")
-/* may_goto expansion starts */
-__xlated("4: r12 = *(u64 *)(r10 -24)")
-__xlated("5: if r12 == 0x0 goto pc+6")
-__xlated("6: r12 -= 1")
-__xlated("7: if r12 != 0x0 goto pc+2")
-__xlated("8: r12 = -24")
-__xlated("9: call unknown")
-__xlated("10: *(u64 *)(r10 -24) = r12")
+__msg("subprog 0 (may_goto_interaction) main {{.*}} stack 8")
+/* may_goto counter and timestamp */
+__xlated("0: r0 = ")
+__xlated("2: r0 = &(void __percpu *)(r0)")
+__xlated("...")
+__xlated("6: r1 = 1")
+__xlated("7: call bpf_get_smp_processor_id")
+/* may_goto expansion starts, r0 and r1 are live */
+__xlated("8: r2 = ")
+__xlated("10: r2 = &(void __percpu *)(r2)")
+__xlated("11: r12 = *(u64 *)(r2 +0)")
+__xlated("12: if r12 == 0x0 goto pc+6")
+__xlated("13: r12 -= 1")
+__xlated("14: if r12 != 0x0 goto pc+2")
+__xlated("15: r12 = r2")
+__xlated("16: call unknown")
+__xlated("17: *(u64 *)(r2 +0) = r12")
/* may_goto expansion ends */
-__xlated("11: *(u64 *)(r10 -8) = r1")
-__xlated("12: exit")
+__xlated("18: *(u64 *)(r10 -8) = r1")
+__xlated("19: exit")
__success
__naked void may_goto_interaction(void)
{
diff --git a/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c b/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c
index 1fbcc522830614..4dd98acc6436d4 100644
--- a/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c
+++ b/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c
@@ -553,7 +553,11 @@ int cond_break5(const void *ctx)
{
int cnt1 = zero, cnt2;
- for (;;) {
+ /*
+ * There is one time slice for all may_goto of the prog. When it's used up a function that
+ * is called after that makes no iteration. So this loop has an end.
+ */
+ for (; cnt1 < 100;) {
cond_break;
cnt1++;
}
diff --git a/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c b/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c
index faf9eec0a63ebc..20152a15ca360b 100644
--- a/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c
+++ b/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c
@@ -82,23 +82,31 @@ __naked void may_goto_batch_1(void)
SEC("raw_tp")
__description("may_goto batch with offsets 2/0")
+__load_if_JITed()
__arch_x86_64
__arch_s390x
__arch_arm64
__arch_riscv64
__arch_loongarch
-__xlated("0: *(u64 *)(r10 -16) = 65535")
-__xlated("1: *(u64 *)(r10 -8) = 0")
-__xlated("2: r12 = *(u64 *)(r10 -16)")
-__xlated("3: if r12 == 0x0 goto pc+6")
-__xlated("4: r12 -= 1")
-__xlated("5: if r12 != 0x0 goto pc+2")
-__xlated("6: r12 = -16")
-__xlated("7: call unknown")
-__xlated("8: *(u64 *)(r10 -16) = r12")
-__xlated("9: r0 = 1")
-__xlated("10: r0 = 2")
-__xlated("11: exit")
+/* the main prog sets count, timestamp, owner */
+__xlated("0: r0 = ")
+__xlated("2: r0 = &(void __percpu *)(r0)")
+__xlated("3: *(u64 *)(r0 +0) = 65535")
+__xlated("4: *(u64 *)(r0 +8) = 0")
+__xlated("5: *(u64 *)(r0 +16) = 0")
+/* may_goto, r0 is dead */
+__xlated("6: r0 = ")
+__xlated("8: r0 = &(void __percpu *)(r0)")
+__xlated("9: r12 = *(u64 *)(r0 +0)")
+__xlated("10: if r12 == 0x0 goto pc+6")
+__xlated("11: r12 -= 1")
+__xlated("12: if r12 != 0x0 goto pc+2")
+__xlated("13: r12 = r0")
+__xlated("14: call unknown")
+__xlated("15: *(u64 *)(r0 +0) = r12")
+__xlated("16: r0 = 1")
+__xlated("17: r0 = 2")
+__xlated("18: exit")
__success
__naked void may_goto_batch_2(void)
{
|
| 3/2 | 2026/11/01 12:37 | llm | patch-evaluator |
1mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 33821 output: 45 thoughts: 1302 Thoughts: **BPF Timed May Goto Rework: Initial Assessment** Okay, so they want me to evaluate this patch series. My initial thought is that this looks like a significant refactor of the BPF `timed_may_goto` mechanism. It's a fairly invasive change, and given its scope, it definitely warrants a thorough look. First, I'll need to understand the architectural implications. The target architecture is amd64, which is good, as I'm very familiar with the x86_64 architecture. I see that the changes touch various architectures, specifically arm64, loongarch, powerpc, riscv and s390, which means there may be implications for cross-architecture behavior or maintainability. I'm especially interested in the x86_64 JIT and verifier changes, as this is where I'll need to focus most of my attention. This includes files in `arch/x86/net/` and core BPF components. From what I can tell, they're changing the calling conventions for `arch_bpf_timed_may_goto`, specifically around how the count/timestamp pointer is passed (instead of offsets). This likely necessitates changes to the JIT compiler, and the verifier will also need to be updated to handle this new flow, especially regarding the percpu state management. The addition of `struct bpf_timed_may_goto __percpu *timed_may_goto;` to `struct bpf_prog_aux` is a key indicator of this percpu state management change. It is necessary in async callbacks, to avoid locking and guarantee that the callback is executed with the expected timing constraints. I need to confirm if this change is for performance reasons, code size or security. If the latter, it will warrant immediate, thorough scrutiny. I notice that the patch also introduces save/restore mechanisms around async callback executions (`bpf_timer_cb`, `bpf_wq_work`, etc.). This is likely to preserve the `timed_may_goto` state across these asynchronous boundaries. This is interesting as it indicates deeper integration of `timed_may_goto` into the broader BPF framework, especially in `kernel/bpf/helpers.c`. The verifier changes in `kernel/bpf/verifier.c` (setting `is_loop_cb` and `is_called` flags) are also important. These new flags likely affect how the verifier analyzes and optimizes the BPF programs using `timed_may_goto`, especially in loops. From my experience, this means that the `kernel/bpf/fixups.c` code is likely the most complex and interesting, since the verifier can influence the JIT which is very specific to x86_64 architecture. Given the scope and the core nature of BPF, this clearly needs extensive testing, fuzzing, and code review. Let's see... - **Reachable?**: Yes, BPF is fully reachable via standard syscalls (`bpf`) on amd64 in QEMU. - **WorthFuzzing?**: Absolutely. This has fundamental logic changes to BPF instruction patching, JIT calling conventions, verifier fixups, and percpu state management for timed may\_goto. - **FocusSymbols?**: Requires specific, non-ubiquitous kernel functions to focus fuzzing on. - **EnableConfigs?**: We'll need to determine the necessary kernel configurations to compile and reach the modified code. Next step: I'll dive in deeper using grepper and read-file to understand the code changes in detail and confirm my initial assumptions. I'll need to create a test plan to verify the correctness of the new implementation and its impact on performance and security. |
| 4/2 | 2026/11/01 12:39 | tool | grepper |
0mArgs:{
"Expression": "arch_bpf_timed_may_goto",
"PathPrefixes": [
"arch/x86/",
"kernel/bpf/"
]
}Results: {
"Output": "arch/x86/net/bpf_timed_may_goto.S-10-\narch/x86/net/bpf_timed_may_goto.S:11:SYM_FUNC_START(arch_bpf_timed_may_goto)\narch/x86/net/bpf_timed_may_goto.S-12-\tANNOTATE_NOENDBR\n--\narch/x86/net/bpf_timed_may_goto.S-48-\tRET\narch/x86/net/bpf_timed_may_goto.S:49:SYM_FUNC_END(arch_bpf_timed_may_goto)\n--\nkernel/bpf/core.c=3603=bool __weak bpf_jit_supports_timed_may_goto(void)\n--\nkernel/bpf/core.c-3607-\nkernel/bpf/core.c:3608:u64 __weak arch_bpf_timed_may_goto(void)\nkernel/bpf/core.c-3609-{\n--\nkernel/bpf/fixups.c=1812=static int may_goto_expand_timed(struct bpf_insn *insn_buf, int off,\n--\nkernel/bpf/fixups.c-1842-\tinsn_buf[cnt++] = BPF_MOV64_REG(BPF_REG_AX, reg);\nkernel/bpf/fixups.c:1843:\tinsn_buf[cnt++] = BPF_EMIT_CALL(arch_bpf_timed_may_goto);\nkernel/bpf/fixups.c-1844-\tinsn_buf[cnt++] = BPF_STX_MEM(BPF_DW, reg, BPF_REG_AX, 0);\n--\nkernel/bpf/fixups.c=1853=int bpf_do_misc_fixups(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-1869-\tu16 stack_depth_extra = 0;\nkernel/bpf/fixups.c:1870:\t/* arch_bpf_timed_may_goto() takes and returns AX in a cpu register: for JITs only */\nkernel/bpf/fixups.c-1871-\tbool timed_may_goto = prog-\u003ejit_requested \u0026\u0026 bpf_jit_supports_timed_may_goto();\n"
} |
| 5/2 | 2026/11/01 12:39 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 36582 output: 37 thoughts: 63 Thoughts: Here's my take on the thought process, as requested: **Diving into the BPF Timed Goto Implementation** Okay, so I need to understand the nuances of this timed goto functionality within the BPF framework. My first instinct is to zero in on the core implementation. I'll need to examine `kernel/bpf/core.c`, specifically around the functions involved in the timing and control flow for conditional jumps. This means taking a close look at `arch_bpf_timed_may_goto`, `bpf_check_timed_may_goto`, `bpf_timed_may_goto_save`, and `bpf_timed_may_goto_restore`. I'm expecting to find the architectural-specific checks, the general validation, and the save/restore mechanics of the timer related goto state within those functions. My goal is to understand how the timing mechanism is integrated with the BPF VM's control flow and state management. I need to see the atomic operations, the memory barriers, and the logic that governs the allowed jumps based on the timing constraints. From there, I can determine the level of complexity, and how it handles various concurrency and exception conditions. |
| 6/2 | 2026/11/01 12:39 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/core.c",
"FirstLine": 3600,
"LineCount": 80
}Results: {
"Contents": "3600:\t{\n3601:\t}\n3602:\t\n3603:\tbool __weak bpf_jit_supports_timed_may_goto(void)\n3604:\t{\n3605:\t\treturn false;\n3606:\t}\n3607:\t\n3608:\tu64 __weak arch_bpf_timed_may_goto(void)\n3609:\t{\n3610:\t\treturn 0;\n3611:\t}\n3612:\t\n3613:\tstatic noinline void bpf_prog_report_may_goto_violation(void)\n3614:\t{\n3615:\t#ifdef CONFIG_BPF_SYSCALL\n3616:\t\tstruct bpf_stream_stage ss;\n3617:\t\tstruct bpf_prog *prog;\n3618:\t\n3619:\t\tprog = bpf_prog_find_from_stack();\n3620:\t\tif (!prog)\n3621:\t\t\treturn;\n3622:\t\tbpf_stream_stage(ss, prog, BPF_STDERR, ({\n3623:\t\t\tbpf_stream_printk(ss, \"ERROR: Timeout detected for may_goto instruction\\n\");\n3624:\t\t\tbpf_stream_dump_stack(ss);\n3625:\t\t}));\n3626:\t#endif\n3627:\t}\n3628:\t\n3629:\tu64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *p)\n3630:\t{\n3631:\t\tu64 time = ktime_get_mono_fast_ns();\n3632:\t\n3633:\t\t/* Populate the timestamp for this stack frame, and refresh count. */\n3634:\t\tif (!p-\u003etimestamp) {\n3635:\t\t\tp-\u003etimestamp = time;\n3636:\t\t\treturn BPF_MAX_TIMED_LOOPS;\n3637:\t\t}\n3638:\t\t/* Check if we've exhausted our time slice, and zero count. */\n3639:\t\tif (unlikely(time - p-\u003etimestamp \u003e= (NSEC_PER_SEC / 4))) {\n3640:\t\t\tbpf_prog_report_may_goto_violation();\n3641:\t\t\treturn 0;\n3642:\t\t}\n3643:\t\t/* Refresh the count for the stack frame. */\n3644:\t\treturn BPF_MAX_TIMED_LOOPS;\n3645:\t}\n3646:\t\n3647:\t/*\n3648:\t * The main prog sets count and timestamp in its first insns. A call of an async callback is an\n3649:\t * invocation of its own too, but it can interrupt an invocation of the prog on this cpu, or go on\n3650:\t * next to one that is preempted. So its caller sets count and timestamp, and puts the old ones\n3651:\t * back at the end. Not when another invocation has set them in the meantime: they are for that\n3652:\t * one now.\n3653:\t * The caller has disabled migration.\n3654:\t */\n3655:\tvoid bpf_timed_may_goto_save(const struct bpf_prog *prog, struct bpf_timed_may_goto *saved)\n3656:\t{\n3657:\t\tstruct bpf_timed_may_goto *cur;\n3658:\t\n3659:\t\tif (!prog-\u003eaux-\u003etimed_may_goto)\n3660:\t\t\treturn;\n3661:\t\tcur = this_cpu_ptr(prog-\u003eaux-\u003etimed_may_goto);\n3662:\t\t*saved = *cur;\n3663:\t\tcur-\u003ecount = BPF_MAX_TIMED_LOOPS;\n3664:\t\tcur-\u003etimestamp = 0;\n3665:\t\tcur-\u003eowner = saved;\n3666:\t}\n3667:\t\n3668:\tvoid bpf_timed_may_goto_restore(const struct bpf_prog *prog, struct bpf_timed_may_goto *saved)\n3669:\t{\n3670:\t\tstruct bpf_timed_may_goto *cur;\n3671:\t\n3672:\t\tif (!prog-\u003eaux-\u003etimed_may_goto)\n3673:\t\t\treturn;\n3674:\t\tcur = this_cpu_ptr(prog-\u003eaux-\u003etimed_may_goto);\n3675:\t\tif (cur-\u003eowner == saved)\n3676:\t\t\t*cur = *saved;\n3677:\t}\n3678:\t\n3679:\t/* for configs without MMU or 32-bit */\n"
} |
| 7/2 | 2026/11/01 12:39 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 38160 output: 44 thoughts: 54 Thoughts: Okay, let's dive into this. Here's how I'd approach understanding the `arch_bpf_timed_may_goto` function on x86_64: **Exploring the x86_64 BPF Timed May Goto Implementation** First, I need to understand the specifics of how `arch_bpf_timed_may_goto` is actually *implemented* on this architecture. My initial instinct is to dig into the assembly code. The crucial piece I'm after is the definition itself. I'm going to head straight to `arch/x86/net/bpf_timed_may_goto.S`. That's where the low-level details, the actual instructions, will be laid bare. I'm not just looking for a C declaration; I need to see the compiled assembly. This is where the rubber meets the road. I need to see how the conditional branch is handled, any potential optimizations, and how it interacts with the BPF interpreter's context. I want to build a clear mental model of how this function executes at the machine level on x86_64. From there, I can analyze how the x86 architecture is interacting with this critical piece of BPF infrastructure. |
| 8/2 | 2026/11/01 12:39 | tool | read-file |
0mArgs:{
"File": "arch/x86/net/bpf_timed_may_goto.S",
"FirstLine": 1,
"LineCount": 60
}Results: {
"Contents": " 1:\t// SPDX-License-Identifier: GPL-2.0\n 2:\t/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */\n 3:\t\n 4:\t#include \u003clinux/export.h\u003e\n 5:\t#include \u003clinux/linkage.h\u003e\n 6:\t#include \u003casm/nospec-branch.h\u003e\n 7:\t\n 8:\t\t.code64\n 9:\t\t.section .text, \"ax\"\n 10:\t\n 11:\tSYM_FUNC_START(arch_bpf_timed_may_goto)\n 12:\t\tANNOTATE_NOENDBR\n 13:\t\n 14:\t\t/* Setup frame. */\n 15:\t\tpushq %rbp\n 16:\t\tmovq %rsp, %rbp\n 17:\t\n 18:\t\t/* Save r0-r5. */\n 19:\t\tpushq %rax\n 20:\t\tpushq %rdi\n 21:\t\tpushq %rsi\n 22:\t\tpushq %rdx\n 23:\t\tpushq %rcx\n 24:\t\tpushq %r8\n 25:\t\t/* Save stack argument 6. */\n 26:\t\tpushq %r9\n 27:\t\n 28:\t\t/* r10 has the pointer to count and timestamp. Pass it as first argument. */\n 29:\t\tmovq %r10, %rdi\n 30:\t\n 31:\t\t/* Emit call depth accounting for call below. */\n 32:\t\tCALL_DEPTH_ACCOUNT\n 33:\t\tcall bpf_check_timed_may_goto\n 34:\t\n 35:\t\t/* BPF_REG_AX=r10 will be stored into count, so move return value to it. */\n 36:\t\tmovq %rax, %r10\n 37:\t\n 38:\t\tpopq %r9\n 39:\t\t/* Restore r5-r0. */\n 40:\t\tpopq %r8\n 41:\t\tpopq %rcx\n 42:\t\tpopq %rdx\n 43:\t\tpopq %rsi\n 44:\t\tpopq %rdi\n 45:\t\tpopq %rax\n 46:\t\n 47:\t\tleave\n 48:\t\tRET\n 49:\tSYM_FUNC_END(arch_bpf_timed_may_goto)\n"
} |
| 9/2 | 2026/11/01 12:39 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 39137 output: 39 thoughts: 29 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Investigating `bpf_jit_supports_timed_may_goto()` in the x86 Architecture** Right, let's dive into this. I need to figure out the definition of `bpf_jit_supports_timed_may_goto()`. My initial hunch is that it'll be in the `arch/x86` directory. Given the nature of BPF and JIT support, that's the most logical place to start digging within the kernel source tree. I'm expecting some architecture-specific implementation details there, potentially tied to instruction set extensions, the CPU's timing capabilities, or the general JIT infrastructure on x86. Hopefully, it's reasonably well-documented within the x86 specific code. Now, let me get to it and find where it is defined. |
| 10/2 | 2026/11/01 12:39 | tool | grepper |
0mArgs:{
"Expression": "bpf_jit_supports_timed_may_goto",
"PathPrefixes": [
"arch/x86/"
]
}Results: {
"Output": "arch/x86/net/bpf_jit_comp.c=5212=u64 bpf_arch_uaddress_limit(void)\n--\narch/x86/net/bpf_jit_comp.c-5216-\narch/x86/net/bpf_jit_comp.c:5217:bool bpf_jit_supports_timed_may_goto(void)\narch/x86/net/bpf_jit_comp.c-5218-{\n"
} |
| 11/2 | 2026/11/01 12:39 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 39533 output: 45 thoughts: 29 Thoughts: Okay, here's my attempt at summarizing the thought process, as requested, geared toward an expert audience, and from a first-person perspective: **Diving into the BPF JIT Compiler** Alright, let's get down to brass tacks. I need to understand what's happening with the BPF JIT compilation for x86. I'm going to start by zeroing in on `arch/x86/net/bpf_jit_comp.c`. Specifically, I'm going to look around line 5217. I'm expecting to see some crucial code there that is likely dealing with an interesting function - perhaps related to register allocation, instruction emission, or some aspect of the code generation process. Hopefully this location is a good point to start the analysis of how the instruction stream is generated. I'm curious to see the nitty-gritty of the code generation, and how specific instructions are chosen to translate the BPF opcodes. |
| 12/2 | 2026/11/01 12:40 | tool | read-file |
0mArgs:{
"File": "arch/x86/net/bpf_jit_comp.c",
"FirstLine": 5215,
"LineCount": 20
}Results: {
"Contents": "5215:\t}\n5216:\t\n5217:\tbool bpf_jit_supports_timed_may_goto(void)\n5218:\t{\n5219:\t\treturn true;\n5220:\t}\n5221:\t\n5222:\tbool bpf_jit_supports_fsession(void)\n5223:\t{\n5224:\t\treturn true;\n5225:\t}\n"
} |
| 13/2 | 2026/11/01 12:40 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 39945 output: 35 thoughts: 137 Thoughts: Here's my thought process, as it stands: **BPF JIT Timed `may_goto` Support on x86_64** Okay, so `bpf_jit_supports_timed_may_goto` is returning true on x86_64. That's good to know. Now, I need to understand *how* that's being handled in the kernel. Let's dig into `kernel/bpf/fixups.c`. That file's likely where the core logic for these fixups resides. Ah, here we are: `may_goto_expand_timed`. That looks like a critical static helper function; it seems to be involved with the "timed may goto" expansion. And it's called from `bpf_do_misc_fixups`, right. That makes sense, miscellaneous fixups. Now I know that `bpf_do_misc_fixups` gets invoked during the BPF program verification or loading process. Alright, so the next step is to examine the call sites of `bpf_do_misc_fixups`. I need to understand what triggers its execution and where it's situated in the verification and loading sequence. More importantly, I need to see *how* `bpf_is_may_goto_insn` is being used, to see how that fits into this expansion. That should give me a solid grasp of how timed `may_goto` is supported on x86_64. |
| 14/2 | 2026/11/01 12:40 | tool | grepper |
0mArgs:{
"Expression": "bpf_do_misc_fixups",
"PathPrefixes": [
"kernel/bpf/"
]
}Results: {
"Output": "kernel/bpf/fixups.c=67=static bool bpf_is_reg64(struct bpf_prog *prog, struct bpf_insn *insn)\n--\nkernel/bpf/fixups.c-82-\nkernel/bpf/fixups.c:83:\t/* address space casts converted to BPF_ALU, see bpf_do_misc_fixups() */\nkernel/bpf/fixups.c-84-\tif (is_addr_space_cast32(prog, insn))\n--\nkernel/bpf/fixups.c=1812=static int may_goto_expand_timed(struct bpf_insn *insn_buf, int off,\n--\nkernel/bpf/fixups.c-1852- */\nkernel/bpf/fixups.c:1853:int bpf_do_misc_fixups(struct bpf_verifier_env *env)\nkernel/bpf/fixups.c-1854-{\n--\nkernel/bpf/verifier.c=23810=int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,\n--\nkernel/bpf/verifier.c-24085-\tif (ret == 0)\nkernel/bpf/verifier.c:24086:\t\tret = bpf_do_misc_fixups(env);\nkernel/bpf/verifier.c-24087-\n"
} |
| 15/2 | 2026/11/01 12:40 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 40815 output: 51 thoughts: 47 Thoughts: Here's my thought process, summarized as if I were considering this issue: **Delving into `BPF_MOV64_PERCPU_REG` within `may_goto_expand_timed`** Okay, so I need to check how `BPF_MOV64_PERCPU_REG` is handled within the context of `may_goto_expand_timed`. It's crucial for understanding potential timing behavior and optimization opportunities. My initial focus has to be on finding the actual definition and implementation of `BPF_MOV64_PERCPU_REG`. I need to track down where this constant is established, and critically, how the BPF interpreter treats it. I'm going to start with a quick search to locate that definition; knowing its type and scope is foundational to what I'm looking into. |
| 16/2 | 2026/11/01 12:40 | tool | grepper |
0mArgs:{
"Expression": "BPF_MOV64_PERCPU_REG",
"PathPrefixes": [
"include/",
"kernel/bpf/",
"arch/x86/"
]
}Results: {
"Output": "include/linux/filter.h=40=struct ctl_table_header;\n--\ninclude/linux/filter.h-198-\ninclude/linux/filter.h:199:#define BPF_MOV64_PERCPU_REG(DST, SRC)\t\t\t\t\\\ninclude/linux/filter.h-200-\t((struct bpf_insn) {\t\t\t\t\t\\\n--\nkernel/bpf/arraymap.c=294=static int percpu_array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)\n--\nkernel/bpf/arraymap.c-318-\t*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0);\nkernel/bpf/arraymap.c:319:\t*insn++ = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);\nkernel/bpf/arraymap.c-320-\t*insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);\n--\nkernel/bpf/fixups.c=1812=static int may_goto_expand_timed(struct bpf_insn *insn_buf, int off,\n--\nkernel/bpf/fixups.c-1821-\tinsn_buf[cnt++] = ld_tmg[1];\nkernel/bpf/fixups.c:1822:\tinsn_buf[cnt++] = BPF_MOV64_PERCPU_REG(reg, reg);\nkernel/bpf/fixups.c-1823-\tinsn_buf[cnt++] = BPF_LDX_MEM(BPF_DW, BPF_REG_AX, reg, 0);\n--\nkernel/bpf/fixups.c=1853=int bpf_do_misc_fixups(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-2202-\t\t\tinsn_buf[0] = insn[1];\nkernel/bpf/fixups.c:2203:\t\t\tinsn_buf[1] = BPF_MOV64_PERCPU_REG(insn-\u003edst_reg, insn-\u003edst_reg);\nkernel/bpf/fixups.c-2204-\t\t\tcnt = 2;\n--\nkernel/bpf/fixups.c-2520-\t\t\tinsn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, (u32)(unsigned long)\u0026cpu_number);\nkernel/bpf/fixups.c:2521:\t\t\tinsn_buf[1] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);\nkernel/bpf/fixups.c-2522-\t\t\tinsn_buf[2] = BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0, 0);\n--\nkernel/bpf/fixups.c-2542-\t\t\tinsn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, (u32)(unsigned long)\u0026current_task);\nkernel/bpf/fixups.c:2543:\t\t\tinsn_buf[1] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);\nkernel/bpf/fixups.c-2544-\t\t\tinsn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0);\n--\nkernel/bpf/fixups.c-2797-\t\t\tinsn_buf[cnt++] = ld_tmg[1];\nkernel/bpf/fixups.c:2798:\t\t\tinsn_buf[cnt++] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);\nkernel/bpf/fixups.c-2799-\t\t\tif (main_prog) {\n--\nkernel/bpf/hashtab.c=2650=static int htab_percpu_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)\n--\nkernel/bpf/hashtab.c-2663-\t*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0);\nkernel/bpf/hashtab.c:2664:\t*insn++ = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);\nkernel/bpf/hashtab.c-2665-\n"
} |
| 17/2 | 2026/11/01 12:40 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 42298 output: 38 thoughts: 23 Thoughts: Here's a summary of my thought process: **Diving into x86 JIT and BPF_MOV64_PERCPU_REG** Alright, let's take a closer look at how the x86 Just-In-Time (JIT) compiler specifically deals with the `BPF_MOV64_PERCPU_REG` instruction. I need to understand the translation and optimization strategies employed here. I'm already familiar with the general BPF-to-x86 JIT process, but this particular instruction, given its per-CPU context, likely introduces some interesting complexities. I expect it involves managing per-CPU data structures, potentially using thread-local storage or some other mechanism to maintain data integrity across different CPU cores. I want to see how the JIT translates this BPF instruction into x86 assembly, what registers are used, and whether there are any optimizations that are taking place to minimize overhead. I'm particularly interested in how they're handling atomic operations if this instruction involves updating shared per-CPU data. It could be a simple load/store, but it's more likely some form of atomic operation is involved to maintain consistency. Let's see how they approach it in this specific context. |
| 18/2 | 2026/11/01 12:40 | tool | grepper |
0mArgs:{
"Expression": "insn_is_mov_percpu_addr",
"PathPrefixes": [
"arch/x86/"
]
}Results: {
"Output": "arch/x86/net/bpf_jit_comp.c=2516=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-2711-\t\t\t\tbreak;\narch/x86/net/bpf_jit_comp.c:2712:\t\t\t} else if (insn_is_mov_percpu_addr(insn)) {\narch/x86/net/bpf_jit_comp.c-2713-\t\t\t\t/* mov \u003cdst\u003e, \u003csrc\u003e (if necessary) */\n"
} |
| 19/2 | 2026/11/01 12:40 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 42800 output: 45 thoughts: 1934 Thoughts: **Analysis of `bpf_timed_may_goto` and its Impact** Okay, let's dive into this `bpf_timed_may_goto` business. First, I need to understand where this is used. It looks like it's called from a few key places in `kernel/bpf/helpers.c`: timer callbacks (`bpf_timer_cb`), workqueues (`bpf_wq_work`), task work (`bpf_task_work_callback`), and RCU callbacks (`bpf_rcu_run_callback`). We also see it in `kernel/bpf/fixups.c`, specifically during the expansion of `may_goto` instructions (`may_goto_expand_timed`), where it emits a call to `arch_bpf_timed_may_goto`. `bpf_do_misc_fixups` seems to be responsible for allocating and initializing the percpu structure (`struct bpf_timed_may_goto`). In `kernel/bpf/core.c`, `__bpf_prog_free` is responsible for cleaning up the `timed_may_goto` data. `bpf_check_timed_may_goto` is the function that actually performs the timestamp check and handles the loop logic. The save/restore mechanisms, `bpf_timed_may_goto_save` and `bpf_timed_may_goto_restore`, are also called. Finally, the verifier, in `kernel/bpf/verifier.c`, is marking callback functions as loop callbacks. Looking deeper into the verifier's behavior, `push_callback_call` and `check_static_func_call` identify loop callbacks. In `bpf_do_misc_fixups`, the prologue logic specifically addresses `main_prog` and `loop_cb` scenarios and inserts code to initialize or terminate the loop based on the `struct bpf_timed_may_goto`'s state. There are even error conditions checked by the verifier which would certainly merit investigation. I've also noticed a few important details. `may_goto` seems to be the instruction that enables bounded loops, introduced in Linux 6.9, making it a relatively new feature. The implementation has moved from per-stack-frame accounting to a percpu struct, which has implications for concurrency and memory access patterns. Now, the crucial question: is this reachable? Absolutely! BPF is directly accessible via the `bpf()` syscall on x86_64 and amd64 architectures. Syzkaller is a solid testing tool for BPF. Any program loaded with `BPF_PROG_LOAD` that uses `may_goto` instructions will exercise this code path during verification, fixup, JIT compilation, and runtime execution. The runtime execution calls `arch_bpf_timed_may_goto`, `bpf_check_timed_may_goto`, `bpf_timed_may_goto_save`, and `bpf_timed_may_goto_restore`. And of course the JIT compiler is going to exercise that path. Therefore, this area is *definitely* worth fuzzing! Now to figure out the necessary kernel configurations to enable this. I need to search the Kconfig files related to BPF, the verifier, and the JIT compiler to determine the relevant dependencies. |
| 20/2 | 2026/11/01 12:40 | tool | grepper |
0mArgs:{
"Expression": "BPF_JIT",
"PathPrefixes": [
"init/",
"kernel/bpf/",
"arch/x86/"
]
}Results: {
"Output": "\nFull output is too long, showing 200 out of 266 lines.\nUse more precise expression or PathPrefixes if possible.\n\nNumber of matching lines per file (16 files in total):\narch/x86/Kconfig:3\narch/x86/include/asm/extable.h:1\narch/x86/include/asm/nospec-branch.h:1\narch/x86/kernel/callthunks.c:1\narch/x86/kernel/cpu/bugs.c:2\narch/x86/kernel/unwind_orc.c:1\narch/x86/net/Makefile:2\narch/x86/net/bpf_jit_comp.c:12\narch/x86/net/bpf_jit_comp32.c:1\nkernel/bpf/Kconfig:16\nkernel/bpf/Makefile:4\nkernel/bpf/btf.c:1\nkernel/bpf/core.c:13\nkernel/bpf/fixups.c:3\nkernel/bpf/syscall.c:1\nkernel/bpf/verifier.c:1\n\narch/x86/Kconfig=57=config X86\n--\narch/x86/Kconfig-142-\tselect ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH\narch/x86/Kconfig:143:\tselect ARCH_WANT_DEFAULT_BPF_JIT\tif X86_64\narch/x86/Kconfig-144-\tselect ARCH_WANTS_CLOCKSOURCE_READ_INLINE\tif X86_64\n--\narch/x86/Kconfig-235-\tselect HAVE_SAMPLE_FTRACE_DIRECT_MULTI\tif X86_64\narch/x86/Kconfig:236:\tselect HAVE_EBPF_JIT\narch/x86/Kconfig:237:\tselect HAVE_EBPF_JIT_KASAN\t\tif X86_64\narch/x86/Kconfig-238-\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=2237=static void __init spectre_v2_update_mitigation(void)\n--\narch/x86/kernel/cpu/bugs.c-2254-\narch/x86/kernel/cpu/bugs.c:2255:#ifdef CONFIG_BPF_JIT\narch/x86/kernel/cpu/bugs.c-2256-static void __bpf_arch_ibpb(void *unused)\n--\narch/x86/kernel/cpu/bugs.c=2276=static void __init spectre_v2_apply_mitigation(void)\n--\narch/x86/kernel/cpu/bugs.c-2352-\narch/x86/kernel/cpu/bugs.c:2353:#ifdef CONFIG_BPF_JIT\narch/x86/kernel/cpu/bugs.c-2354-\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-26-\narch/x86/net/bpf_jit_comp.c:27:#if IS_ENABLED(CONFIG_BPF_JIT_KASAN)\narch/x86/net/bpf_jit_comp.c-28-void __asan_load1(void *p);\n--\narch/x86/net/bpf_jit_comp.c=117=static int bpf_size_to_x86_bytes(int bpf_size)\n--\narch/x86/net/bpf_jit_comp.c-146-/* Pick a register outside of BPF range for JIT internal work */\narch/x86/net/bpf_jit_comp.c:147:#define AUX_REG (MAX_BPF_JIT_REG + 1)\narch/x86/net/bpf_jit_comp.c:148:#define X86_REG_R9 (MAX_BPF_JIT_REG + 2)\narch/x86/net/bpf_jit_comp.c:149:#define X86_REG_R12 (MAX_BPF_JIT_REG + 3)\narch/x86/net/bpf_jit_comp.c-150-\n--\narch/x86/net/bpf_jit_comp.c=1251=static int emit_kasan_check(struct bpf_verifier_env *env, u8 **pprog,\n--\narch/x86/net/bpf_jit_comp.c-1254-{\narch/x86/net/bpf_jit_comp.c:1255:#ifdef CONFIG_BPF_JIT_KASAN\narch/x86/net/bpf_jit_comp.c-1256-\tu8 *prog = *pprog;\n--\narch/x86/net/bpf_jit_comp.c-1326-\t*pprog = prog;\narch/x86/net/bpf_jit_comp.c:1327:#endif /* CONFIG_BPF_JIT_KASAN */\narch/x86/net/bpf_jit_comp.c-1328-\treturn 0;\n--\narch/x86/net/bpf_jit_comp.c=2319=static int emit_kfunc_arg_moves(const struct btf_func_model *fm, s32 stack_base, u8 **pprog)\narch/x86/net/bpf_jit_comp.c-2320-{\narch/x86/net/bpf_jit_comp.c:2321:\tstruct bpf_jit_arg_move moves[BPF_JIT_MAX_ARG_MOVES];\narch/x86/net/bpf_jit_comp.c-2322-\tconst u8 nreg = x86_arg_abi.nr_arg_regs;\n--\narch/x86/net/bpf_jit_comp.c-2329-\t\tu8 dst = moves[i].dst, src = moves[i].src, reg;\narch/x86/net/bpf_jit_comp.c:2330:\t\tbool dst_mem = dst != BPF_JIT_ARG_TMP \u0026\u0026 dst \u003e= nreg;\narch/x86/net/bpf_jit_comp.c:2331:\t\tbool src_mem = src != BPF_JIT_ARG_TMP \u0026\u0026 src \u003e= nreg;\narch/x86/net/bpf_jit_comp.c-2332-\n--\narch/x86/net/bpf_jit_comp.c-2337-\t\t */\narch/x86/net/bpf_jit_comp.c:2338:\t\tif (src == BPF_JIT_ARG_TMP) {\narch/x86/net/bpf_jit_comp.c-2339-\t\t\treg = AUX_REG;\narch/x86/net/bpf_jit_comp.c-2340-\t\t} else if (src_mem) {\narch/x86/net/bpf_jit_comp.c:2341:\t\t\treg = dst == BPF_JIT_ARG_TMP ? AUX_REG :\narch/x86/net/bpf_jit_comp.c-2342-\t\t\t dst_mem ? BPF_REG_AX : x86_arg_reg[dst];\n--\narch/x86/net/bpf_jit_comp.c-2349-\t\t/* And leave it where the argument belongs. */\narch/x86/net/bpf_jit_comp.c:2350:\t\tif (dst == BPF_JIT_ARG_TMP)\narch/x86/net/bpf_jit_comp.c-2351-\t\t\temit_mov_reg(\u0026prog, true, AUX_REG, reg);\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--\nkernel/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-20-# KASAN support for JIT compiler\nkernel/bpf/Kconfig:21:config HAVE_EBPF_JIT_KASAN\nkernel/bpf/Kconfig-22-\tbool\n--\nkernel/bpf/Kconfig-25-# default for kernels that were compiled with BPF JIT support.\nkernel/bpf/Kconfig:26:config ARCH_WANT_DEFAULT_BPF_JIT\nkernel/bpf/Kconfig-27-\tbool\n--\nkernel/bpf/Kconfig=31=config BPF_SYSCALL\n--\nkernel/bpf/Kconfig-45-\nkernel/bpf/Kconfig:46:config BPF_JIT\nkernel/bpf/Kconfig-47-\tbool \"Enable BPF Just In Time compiler\"\nkernel/bpf/Kconfig-48-\tdepends on BPF\nkernel/bpf/Kconfig:49:\tdepends on HAVE_CBPF_JIT || HAVE_EBPF_JIT\nkernel/bpf/Kconfig-50-\tselect EXECMEM\n--\nkernel/bpf/Kconfig-61-\nkernel/bpf/Kconfig:62:config BPF_JIT_ALWAYS_ON\nkernel/bpf/Kconfig-63-\tbool \"Permanently enable BPF JIT and remove BPF interpreter\"\nkernel/bpf/Kconfig:64:\tdepends on BPF_SYSCALL \u0026\u0026 HAVE_EBPF_JIT \u0026\u0026 BPF_JIT\nkernel/bpf/Kconfig-65-\thelp\n--\nkernel/bpf/Kconfig-68-\nkernel/bpf/Kconfig:69:\t When CONFIG_BPF_JIT_ALWAYS_ON is enabled, /proc/sys/net/core/bpf_jit_enable\nkernel/bpf/Kconfig-70-\t is permanently set to 1 and setting any other value than that will\n--\nkernel/bpf/Kconfig-72-\nkernel/bpf/Kconfig:73:config BPF_JIT_DEFAULT_ON\nkernel/bpf/Kconfig:74:\tdef_bool ARCH_WANT_DEFAULT_BPF_JIT || BPF_JIT_ALWAYS_ON\nkernel/bpf/Kconfig:75:\tdepends on HAVE_EBPF_JIT \u0026\u0026 BPF_JIT\nkernel/bpf/Kconfig-76-\n--\nkernel/bpf/Kconfig=105=config BPF_LSM\n--\nkernel/bpf/Kconfig-109-\tdepends on SECURITY\nkernel/bpf/Kconfig:110:\tdepends on BPF_JIT\nkernel/bpf/Kconfig-111-\thelp\n--\nkernel/bpf/Kconfig-116-\nkernel/bpf/Kconfig:117:config BPF_JIT_KASAN\nkernel/bpf/Kconfig-118-\tbool\nkernel/bpf/Kconfig:119:\tdepends on HAVE_EBPF_JIT_KASAN\nkernel/bpf/Kconfig-120-\tdepends on KASAN_GENERIC\nkernel/bpf/Kconfig-121-\tdepends on KASAN_VMALLOC\nkernel/bpf/Kconfig:122:\tdepends on BPF_JIT\nkernel/bpf/Kconfig-123-\tdefault y\n--\nkernel/bpf/Makefile=2=obj-y := core.o\nkernel/bpf/Makefile:3:ifneq ($(CONFIG_BPF_JIT_ALWAYS_ON),y)\nkernel/bpf/Makefile-4-# ___bpf_prog_run() needs GCSE disabled on x86; see 3193c0836f203 for details\n--\nkernel/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/Makefile=21=endif\nkernel/bpf/Makefile:22:obj-$(CONFIG_BPF_JIT) += dispatcher.o\nkernel/bpf/Makefile-23-ifeq ($(CONFIG_NET),y)\n--\nkernel/bpf/Makefile=46=endif\nkernel/bpf/Makefile:47:ifeq ($(CONFIG_BPF_JIT),y)\nkernel/bpf/Makefile-48-obj-$(CONFIG_BPF_SYSCALL) += bpf_struct_ops.o\n--\nkernel/bpf/btf.c=10784=bool btf_type_ids_nocast_alias(struct bpf_verifier_log *log,\n--\nkernel/bpf/btf.c-10838-\nkernel/bpf/btf.c:10839:#ifdef CONFIG_BPF_JIT\nkernel/bpf/btf.c-10840-static int\n--\nkernel/bpf/core.c=102=struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags)\n--\nkernel/bpf/core.c-130-\tfp-\u003ejit_requested = ebpf_jit_enabled();\nkernel/bpf/core.c:131:\tfp-\u003ejit_required = IS_ENABLED(CONFIG_BPF_JIT_ALWAYS_ON);\nkernel/bpf/core.c-132-\tfp-\u003eblinding_requested = bpf_jit_blinding_enabled(fp);\n--\nkernel/bpf/core.c=542=void bpf_prog_kallsyms_del_all(struct bpf_prog *fp)\n--\nkernel/bpf/core.c-547-\nkernel/bpf/core.c:548:#ifdef CONFIG_BPF_JIT\nkernel/bpf/core.c-549-/* All BPF JIT sysctl knobs here. */\nkernel/bpf/core.c:550:int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON);\nkernel/bpf/core.c:551:int bpf_jit_kallsyms __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON);\nkernel/bpf/core.c-552-int bpf_jit_harden __read_mostly;\n--\nkernel/bpf/core.c=1362=static int bpf_jit_blind_insn(const struct bpf_insn *from,\n--\nkernel/bpf/core.c-1370-\nkernel/bpf/core.c:1371:\tBUILD_BUG_ON(BPF_REG_PARAMS + 2 != MAX_BPF_JIT_REG);\nkernel/bpf/core.c:1372:\tBUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);\n\n"
} |
| 21/2 | 2026/11/01 12:40 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 50200 output: 32 thoughts: 188 Thoughts: **My Assessment of the eBPF JIT Compilation Flow** Okay, let's break this down. My initial observation is that `bpf_timed_may_goto.o` for x86 is constructed through `arch/x86/net/Makefile`. Specifically, the rule `obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o` determines its inclusion based on `CONFIG_BPF_JIT`. That’s my starting point. Now, I'm digging deeper into how this `timed_may_goto` feature is utilized. It looks like `bpf_do_misc_fixups` has a crucial check: `bool timed_may_goto = prog->jit_requested && bpf_jit_supports_timed_may_goto();`. This boolean, `timed_may_goto`, hinges on the `prog->jit_requested` flag being set and the architecture supporting this specific feature, as determined by `bpf_jit_supports_timed_may_goto()`. Importantly, `prog->jit_requested` is set to the result of `ebpf_jit_enabled()`, which essentially tells me that if the JIT is enabled, we'll try to use timed may goto. Obviously, JIT requires `BPF_JIT` which make sense. And, of course, the `bpf` syscall necessitates `BPF_SYSCALL` for loading BPF programs and for the verifier itself, specifically in `verifier.c` and `fixups.c`. That’s a fundamental requirement. Now to delve into the `kernel/bpf/Makefile`... that's where I need to head next. |
| 22/2 | 2026/11/01 12:40 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/Makefile",
"FirstLine": 1,
"LineCount": 40
}Results: {
"Contents": " 1:\t# SPDX-License-Identifier: GPL-2.0\n 2:\tobj-y := core.o\n 3:\tifneq ($(CONFIG_BPF_JIT_ALWAYS_ON),y)\n 4:\t# ___bpf_prog_run() needs GCSE disabled on x86; see 3193c0836f203 for details\n 5:\tcflags-nogcse-$(CONFIG_X86)$(CONFIG_CC_IS_GCC) := -fno-gcse\n 6:\tendif\n 7:\tCFLAGS_core.o += -Wno-override-init $(cflags-nogcse-yy)\n 8:\t\n 9:\tobj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o cnum.o log.o token.o liveness.o const_fold.o diagnostics.o loops.o scev.o\n 10:\tobj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_iter.o\n 11:\tobj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o\n 12:\tobj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o bpf_insn_array.o\n 13:\tobj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o\n 14:\tobj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o backtrack.o check_btf.o\n 15:\tobj-${CONFIG_BPF_LSM}\t += bpf_inode_storage.o\n 16:\tobj-$(CONFIG_BPF_SYSCALL) += disasm.o mprog.o\n 17:\tobj-$(CONFIG_BPF_JIT) += trampoline.o\n 18:\tobj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o rqspinlock.o stream.o\n 19:\tifeq ($(CONFIG_MMU)$(CONFIG_64BIT),yy)\n 20:\tobj-$(CONFIG_BPF_SYSCALL) += arena.o range_tree.o\n 21:\tendif\n 22:\tobj-$(CONFIG_BPF_JIT) += dispatcher.o\n 23:\tifeq ($(CONFIG_NET),y)\n 24:\tobj-$(CONFIG_BPF_SYSCALL) += devmap.o\n 25:\tobj-$(CONFIG_BPF_SYSCALL) += cpumap.o\n 26:\tobj-$(CONFIG_BPF_SYSCALL) += offload.o\n 27:\tobj-$(CONFIG_BPF_SYSCALL) += net_namespace.o\n 28:\tobj-$(CONFIG_BPF_SYSCALL) += tcx.o\n 29:\tendif\n 30:\tifeq ($(CONFIG_KEYS),y)\n 31:\tobj-$(CONFIG_BPF_SYSCALL) += keys.o\n 32:\tendif\n 33:\tifeq ($(CONFIG_PERF_EVENTS),y)\n 34:\tobj-$(CONFIG_BPF_SYSCALL) += stackmap.o\n 35:\tendif\n 36:\tifeq ($(CONFIG_CGROUPS),y)\n 37:\tobj-$(CONFIG_BPF_SYSCALL) += cgroup_iter.o bpf_cgrp_storage.o\n 38:\tendif\n 39:\tobj-$(CONFIG_CGROUP_BPF) += cgroup.o\n 40:\tifeq ($(CONFIG_INET),y)\n"
} |
| 23/2 | 2026/11/01 12:40 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 51773 output: 253 thoughts: 4881 Thoughts: **Focus Symbols and Configuration for Timed `may_goto` in BPF** Okay, so I've been diving into this patch related to the BPF `timed_may_goto` feature, and I think I've got a handle on what's critical for fuzzing. We've got dependencies to deal with, and we need to be precise about which functions are the *actual* entrypoints or specific logic for the new timed may_goto implementation, to avoid diluting the fuzzing effort by including too much generic code. First, the dependencies are clear: `fixups.o`, `helpers.o`, and `verifier.o` need `CONFIG_BPF_SYSCALL`, and both `arch/x86/net/bpf_jit_comp.o` and `bpf_timed_may_goto.o` need `CONFIG_BPF_JIT`, which itself requires `CONFIG_BPF`. So, `CONFIG_BPF_SYSCALL` and `CONFIG_BPF_JIT` it is. Now, the important part: identifying the *right* `FocusSymbols`. I need to avoid ubiquitous functions that are called by almost every BPF program, as that will dilute the fuzzing results. The goal is to target specific entrypoints or logic for the timed may_goto feature. Here's the breakdown of what is relevant: - `arch_bpf_timed_may_goto`: This is the assembly symbol executed when a BPF program uses the may_goto instruction and hits a time check; this is a global function in `arch/x86/net/bpf_timed_may_goto.S`, so it should have coverage instrumentation. - `bpf_check_timed_may_goto`: A non-static C function in `kernel/bpf/core.c` that gets called by `arch_bpf_timed_may_goto`. This one *definitely* has coverage instrumentation. - `bpf_timed_may_goto_save` and `bpf_timed_may_goto_restore`: These are non-static global C functions in `kernel/bpf/core.c` that are crucial for context saving and restoring in asynchronous callbacks (timer, wq, task_work, and rcu). These also have coverage instrumentation. - I need to exclude `bpf_do_misc_fixups` even though it's modified in the patch, because it is called in *every* BPF program load, which means we can't focus just on tests related to the timed feature. - Functions like `bpf_timer_cb`, `bpf_wq_work`, `bpf_task_work_callback`, and `bpf_rcu_run_callback` are static but are crucial to include. So, the focus is on the four key functions and the enable configs: - `bpf_check_timed_may_goto` - `bpf_timed_may_goto_save` - `bpf_timed_may_goto_restore` - `arch_bpf_timed_may_goto` - `BPF_SYSCALL` - `BPF_JIT` These choices should help target the fuzzing efforts precisely. |
| 24/2 | 2026/11/01 12:41 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_check_timed_may_goto",
"bpf_timed_may_goto_save",
"bpf_timed_may_goto_restore",
"arch_bpf_timed_may_goto"
],
"Reasoning": "The patch modifies the implementation of BPF timed may_goto loops. It switches state management from per-stack-frame storage to a dedicated percpu structure (struct bpf_timed_may_goto), refactors instruction expansion in the verifier fixup pass (may_goto_expand_timed), alters the register calling convention used when calling arch_bpf_timed_may_goto on x86_64, and adds save/restore logic for async callbacks (timers, workqueues, task_work, RCU). This code is reachable from user space on amd64 via standard bpf() syscall operations (BPF_PROG_LOAD and program execution with may_goto instructions), making it suitable and important for fuzzing.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"BPF_SYSCALL",
"BPF_JIT"
],
"FocusSymbols": [
"bpf_check_timed_may_goto",
"bpf_timed_may_goto_save",
"bpf_timed_may_goto_restore",
"arch_bpf_timed_may_goto"
],
"Reasoning": "The patch modifies the implementation of BPF timed may_goto loops. It switches state management from per-stack-frame storage to a dedicated percpu structure (struct bpf_timed_may_goto), refactors instruction expansion in the verifier fixup pass (may_goto_expand_timed), alters the register calling convention used when calling arch_bpf_timed_may_goto on x86_64, and adds save/restore logic for async callbacks (timers, workqueues, task_work, RCU). This code is reachable from user space on amd64 via standard bpf() syscall operations (BPF_PROG_LOAD and program execution with may_goto instructions), making it suitable and important for fuzzing.",
"WorthFuzzing": true
} |
| 25/1 | 2026/11/01 12:41 | action | If |
1mArgs:{
"WorthFuzzing": true
}Results: null |
| 26/2 | 2026/11/01 12:41 | agent | kmsan-evaluator |
1mModel:core Args: null Results: {
"KMSANReasoning": "The patch series refactors the BPF timed `may_goto` mechanism. Instead of allocating loop counters and timestamps on the BPF program's stack frame per invocation/subprogram, it uses per-CPU memory (`struct bpf_timed_may_goto __percpu *timed_may_goto`) allocated per program via `alloc_percpu_gfp()`. It updates the verifier fixups (`fixups.c`), the architecture JIT trampolines (arm64, loongarch, powerpc, riscv, s390, x86), and adds save/restore logic around asynchronous callbacks (`bpf_timer_cb`, `bpf_wq_work`, `bpf_task_work_callback`, `bpf_rcu_run_callback`).\n\nFrom a memory initialization perspective:\n1. The per-CPU `struct bpf_timed_may_goto` is zero-allocated by `alloc_percpu_gfp()` and explicitly initialized in the program prologue (`count`, `timestamp`, `owner`).\n2. The local stack variable `struct bpf_timed_may_goto saved` used in async callback helpers is guarded by `if (!prog-\u003eaux-\u003etimed_may_goto) return;`. When active, `*saved = *cur;` completely copies the 24-byte struct before any subsequent read in `bpf_timed_may_goto_restore()`. If `timed_may_goto` is NULL, neither function accesses `saved`.\n3. No kernel memory or structure padding is copied to user space or exposed via infoleaks.\n4. Any potential bugs in this patch series (such as register clobbering, incorrect JIT assembly generation, invalid pointer offsets, or use-after-free upon program teardown) are detectable by KASAN, assertions, or standard runtime checks.\n\nTherefore, this change does not introduce uninitialized memory risks that would require a dedicated KMSAN fuzzing session.",
"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 51ba9bdea0ef67afe96b0bfb46fdbcdf514999af
Author: syz-cluster <triage@syzkaller.com>
Date: Sun Nov 1 12:37:55 2026 +0000
syz-cluster: applied patch under review
diff --git a/arch/arm64/net/bpf_timed_may_goto.S b/arch/arm64/net/bpf_timed_may_goto.S
index 1b4a4f91ffc90c..f8732b13bb1e58 100644
--- a/arch/arm64/net/bpf_timed_may_goto.S
+++ b/arch/arm64/net/bpf_timed_may_goto.S
@@ -17,16 +17,10 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)
str x7, [sp, #80]
/*
- * Stack depth was passed in BPF_REG_AX (x9), add it to the BPF_FP
- * (x25) to get the pointer to count and timestamp and pass it as the
- * first argument in x0.
- *
- * Before generating the call to arch_bpf_timed_may_goto, the verifier
- * generates a load instruction using FP, i.e. REG_AX = *(u64 *)(FP -
- * stack_off_cnt), so BPF_REG_FP (x25) is always set up by the arm64
- * jit in this case.
+ * The pointer to count and timestamp was passed in BPF_REG_AX (x9),
+ * pass it as the first argument in x0.
*/
- add x0, x9, x25
+ mov x0, x9
bl bpf_check_timed_may_goto
/* BPF_REG_AX(x9) will be stored into count, so move return value to it. */
mov x9, x0
diff --git a/arch/loongarch/net/bpf_timed_may_goto.S b/arch/loongarch/net/bpf_timed_may_goto.S
index fa128acdb108a4..4aaca029404bcf 100644
--- a/arch/loongarch/net/bpf_timed_may_goto.S
+++ b/arch/loongarch/net/bpf_timed_may_goto.S
@@ -22,14 +22,10 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)
st.d a4, sp, 48
/*
- * BPF_REG_AX (t0) holds the offset passed in by the verifier;
- * add it to BPF_REG_FP (s4) to get the pointer to the count and
- * timestamp, then pass it as the first argument in a0.
- *
- * The verifier emits a load using FP right before this call,
- * so BPF_REG_FP (s4) is always set up by the JIT in this case.
+ * BPF_REG_AX (t0) holds the pointer to the count and timestamp;
+ * pass it as the first argument in a0.
*/
- add.d a0, t0, s4
+ move a0, t0
bl bpf_check_timed_may_goto
/* BPF_REG_AX (t0) will be stored into count, so move the return value to it. */
move t0, a0
diff --git a/arch/powerpc/net/bpf_timed_may_goto.S b/arch/powerpc/net/bpf_timed_may_goto.S
index 84ecf6fa7f5dcf..b8ae9885eb31cb 100644
--- a/arch/powerpc/net/bpf_timed_may_goto.S
+++ b/arch/powerpc/net/bpf_timed_may_goto.S
@@ -8,7 +8,7 @@
* arch_bpf_timed_may_goto() trampoline for powerpc64
*
* Custom BPF convention (verifier/JIT):
- * - input: stack offset in BPF_REG_AX (r12)
+ * - input: pointer to count and timestamp in BPF_REG_AX (r12)
* - output: updated count in BPF_REG_AX (r12)
*
* Call bpf_check_timed_may_goto(ptr) with normal powerpc64 ABI:
@@ -31,11 +31,8 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)
std r7, 64(r1)
std r8, 72(r1)
- /*
- * r3 = BPF_REG_FP + BPF_REG_AX
- * BPF_REG_FP is r31; BPF_REG_AX is r12 (stack offset in bytes).
- */
- add r3, r31, r12
+ /* r3 = BPF_REG_AX (r12) */
+ mr r3, r12
bl CFUNC(bpf_check_timed_may_goto)
/* Put return value back into AX */
diff --git a/arch/riscv/net/bpf_timed_may_goto.S b/arch/riscv/net/bpf_timed_may_goto.S
index 6ec37f634aff63..2ce1c89c913e55 100644
--- a/arch/riscv/net/bpf_timed_may_goto.S
+++ b/arch/riscv/net/bpf_timed_may_goto.S
@@ -6,13 +6,11 @@
/*
* Trampoline for the BPF timed may_goto loop bound. Custom calling convention:
- * - input: stack offset in BPF_REG_AX (t0)
+ * - input: pointer to count and timestamp in BPF_REG_AX (t0)
* - output: updated count in BPF_REG_AX (t0)
*
- * Calls bpf_check_timed_may_goto(ptr) with the standard RISC-V ABI, where
- * ptr = BPF_REG_FP (s5) + BPF_REG_AX (t0). BPF R0-R5 (t6, a0-a4) and stack
- * arguments 6-8 (a5-a7) are saved across the call; BPF_REG_FP (s5) is
- * callee-saved and needs no saving.
+ * Calls bpf_check_timed_may_goto(ptr) with the standard RISC-V ABI. BPF R0-R5
+ * (t6, a0-a4) and stack arguments 6-8 (a5-a7) are saved across the call.
*/
SYM_FUNC_START(arch_bpf_timed_may_goto)
@@ -33,7 +31,7 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)
REG_S a6, 7*SZREG(sp)
REG_S a7, 8*SZREG(sp)
- add a0, t0, s5
+ mv a0, t0
call bpf_check_timed_may_goto
mv t0, a0
diff --git a/arch/s390/net/bpf_jit_comp.c b/arch/s390/net/bpf_jit_comp.c
index 56ec49894dda38..15029cedc50baf 100644
--- a/arch/s390/net/bpf_jit_comp.c
+++ b/arch/s390/net/bpf_jit_comp.c
@@ -1005,6 +1005,17 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
(jit->prg - patch_brc) >> 1;
break;
}
+ if (insn_is_mov_percpu_addr(insn)) {
+ const u64 *percpu_offset = &get_lowcore()->percpu_offset;
+
+ if (dst_reg != src_reg)
+ /* lgr %dst,%src */
+ EMIT4(0xb9040000, dst_reg, src_reg);
+ /* ag %dst,percpu_offset */
+ EMIT6_DISP_LH(0xe3000000, 0x0008, dst_reg, REG_0, REG_0,
+ (unsigned long)percpu_offset);
+ break;
+ }
switch (insn->off) {
case 0: /* DST = SRC */
/* lgr %dst,%src */
@@ -1866,9 +1877,9 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
if ((void *)func == arch_bpf_timed_may_goto) {
/*
* arch_bpf_timed_may_goto() has a special ABI: the
- * parameters are in BPF_REG_AX and BPF_REG_10; the
- * return value is in BPF_REG_AX; and all GPRs except
- * REG_W0, REG_W1, and BPF_REG_AX are callee-saved.
+ * parameter is in BPF_REG_AX; the return value is in
+ * BPF_REG_AX; and all GPRs except REG_W0, REG_W1, and
+ * BPF_REG_AX are callee-saved.
*/
/* brasl %r0,func */
@@ -3116,6 +3127,11 @@ bool bpf_jit_supports_timed_may_goto(void)
return true;
}
+bool bpf_jit_supports_percpu_insn(void)
+{
+ return true;
+}
+
bool bpf_jit_inlines_helper_call(s32 imm)
{
switch (imm) {
diff --git a/arch/s390/net/bpf_timed_may_goto.S b/arch/s390/net/bpf_timed_may_goto.S
index 06f567a460d7bd..05f532f8c63903 100644
--- a/arch/s390/net/bpf_timed_may_goto.S
+++ b/arch/s390/net/bpf_timed_may_goto.S
@@ -21,9 +21,9 @@
SYM_FUNC_START(arch_bpf_timed_may_goto)
/*
- * This function has a special ABI: the parameters are in %r12 and
- * %r13; the return value is in %r12; all GPRs except %r0, %r1, and
- * %r12 are callee-saved; and the return address is in %r0.
+ * This function has a special ABI: the parameter is in %r12; the
+ * return value is in %r12; all GPRs except %r0, %r1, and %r12 are
+ * callee-saved; and the return address is in %r0.
*/
stmg %r2,%r5,FRAME_OFF+R2_OFF(%r15)
stg %r14,FRAME_OFF+R14_OFF(%r15)
@@ -33,7 +33,7 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)
lay %r15,-FRAME_SIZE(%r15)
stg %r1,__SF_BACKCHAIN(%r15)
- lay %r2,0(%r12,%r13)
+ lgr %r2,%r12
brasl %r14,bpf_check_timed_may_goto
lgr %r12,%r2
diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index d90b6d238b3a2e..38871963e67671 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -3436,18 +3436,6 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
ip += emit_kfunc_arg_moves(fm, outgoing_arg_base -
outgoing_rsp, &prog);
}
- if (func == (u8 *)arch_bpf_timed_may_goto) {
- u32 fp = priv_frame_ptr ? X86_REG_R9 : BPF_REG_FP;
-
- /*
- * AX has the offset of count and timestamp
- * in the stack. Turn it into a pointer.
- * add r10, rbp or add r10, r9
- */
- maybe_emit_mod(&prog, BPF_REG_AX, fp, true);
- EMIT2(0x01, add_2reg(0xC0, BPF_REG_AX, fp));
- ip += 3;
- }
if (priv_frame_ptr) {
push_r9(&prog);
ip += 2;
diff --git a/arch/x86/net/bpf_timed_may_goto.S b/arch/x86/net/bpf_timed_may_goto.S
index dd2dde89836b8f..da42de7cca9882 100644
--- a/arch/x86/net/bpf_timed_may_goto.S
+++ b/arch/x86/net/bpf_timed_may_goto.S
@@ -25,10 +25,7 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)
/* Save stack argument 6. */
pushq %r9
- /*
- * r10 has the pointer to count and timestamp. JIT added the frame
- * pointer to the offset. Pass it as first argument.
- */
+ /* r10 has the pointer to count and timestamp. Pass it as first argument. */
movq %r10, %rdi
/* Emit call depth accounting for call below. */
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index ff043678129358..01d5c79b487e2e 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1916,6 +1916,8 @@ struct bpf_prog_aux {
const char *attach_func_name;
struct bpf_prog **func;
struct bpf_prog_aux *main_prog_aux;
+ /* main prog: there when the prog has timed may_goto */
+ struct bpf_timed_may_goto __percpu *timed_may_goto;
void *jit_data; /* JIT specific data. arch dependent */
struct bpf_jit_poke_descriptor *poke_tab;
struct bpf_kfunc_desc_tab *kfunc_tab;
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 7baeb831281f38..12ec5b14007fbf 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -964,6 +964,10 @@ struct bpf_subprog_info {
bool is_cb: 1;
bool is_async_cb: 1;
bool is_exception_cb: 1;
+ /* bpf_loop() or bpf_for_each_map_elem() call it in a loop */
+ bool is_loop_cb: 1;
+ /* BPF code, another helper or a kfunc call it */
+ bool is_called: 1;
bool args_cached: 1;
/* true if the return value is passed in the R0:R2 register pair */
bool ret_reg_pair: 1;
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 98254742ae37a2..4f0cd67caccb80 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -758,9 +758,15 @@ struct bpf_prog_stats {
struct u64_stats_sync syncp;
} __aligned(2 * sizeof(u64));
+/*
+ * Per-cpu memory of a prog. All may_goto of one invocation of the prog use it. An invocation is
+ * one call from the kernel into the main prog, with the subprogs and sync callbacks it reaches.
+ */
struct bpf_timed_may_goto {
u64 count;
u64 timestamp;
+ /* the async callback that has set count and timestamp, see bpf_timed_may_goto_save() */
+ void *owner;
};
struct sk_filter {
@@ -1328,6 +1334,8 @@ u64 bpf_arch_uaddress_limit(void);
void arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie);
u64 arch_bpf_timed_may_goto(void);
u64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *);
+void bpf_timed_may_goto_save(const struct bpf_prog *prog, struct bpf_timed_may_goto *saved);
+void bpf_timed_may_goto_restore(const struct bpf_prog *prog, struct bpf_timed_may_goto *saved);
bool bpf_helper_changes_pkt_data(enum bpf_func_id func_id);
static inline bool bpf_dump_raw_ok(const struct cred *cred)
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 7cf37690d41fa4..3b02aafba4c549 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -292,6 +292,7 @@ void __bpf_prog_free(struct bpf_prog *fp)
mutex_destroy(&fp->aux->used_maps_mutex);
mutex_destroy(&fp->aux->dst_mutex);
kfree(fp->aux->poke_tab);
+ free_percpu(fp->aux->timed_may_goto);
kfree(fp->aux);
}
free_percpu(fp->stats);
@@ -3643,6 +3644,38 @@ u64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *p)
return BPF_MAX_TIMED_LOOPS;
}
+/*
+ * The main prog sets count and timestamp in its first insns. A call of an async callback is an
+ * invocation of its own too, but it can interrupt an invocation of the prog on this cpu, or go on
+ * next to one that is preempted. So its caller sets count and timestamp, and puts the old ones
+ * back at the end. Not when another invocation has set them in the meantime: they are for that
+ * one now.
+ * The caller has disabled migration.
+ */
+void bpf_timed_may_goto_save(const struct bpf_prog *prog, struct bpf_timed_may_goto *saved)
+{
+ struct bpf_timed_may_goto *cur;
+
+ if (!prog->aux->timed_may_goto)
+ return;
+ cur = this_cpu_ptr(prog->aux->timed_may_goto);
+ *saved = *cur;
+ cur->count = BPF_MAX_TIMED_LOOPS;
+ cur->timestamp = 0;
+ cur->owner = saved;
+}
+
+void bpf_timed_may_goto_restore(const struct bpf_prog *prog, struct bpf_timed_may_goto *saved)
+{
+ struct bpf_timed_may_goto *cur;
+
+ if (!prog->aux->timed_may_goto)
+ return;
+ cur = this_cpu_ptr(prog->aux->timed_may_goto);
+ if (cur->owner == saved)
+ *cur = *saved;
+}
+
/* for configs without MMU or 32-bit */
__weak const struct bpf_map_ops arena_map_ops;
__weak u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena)
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index 41bc37e205e0f2..a656634fa9e468 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -1803,6 +1803,50 @@ static int may_goto_expand(struct bpf_insn *insn_buf, int off, int stack_off,
return cnt + tail_cnt;
}
+/*
+ * Timed may_goto. All may_goto of a prog use one count and one timestamp: per-cpu memory of the
+ * prog, set in the first insns of the main prog, and by the callers of async callbacks.
+ * @reg holds the address. It is a register that is dead at the may_goto, or R0 that is saved at
+ * @save_off in the stack.
+ */
+static int may_goto_expand_timed(struct bpf_insn *insn_buf, int off,
+ struct bpf_timed_may_goto __percpu *tmg, int reg, int save_off)
+{
+ struct bpf_insn ld_tmg[] = { BPF_LD_IMM64(reg, (__force unsigned long)tmg) };
+ int cnt = 0, jmp, tail_cnt = save_off ? 6 : 5;
+
+ if (save_off)
+ insn_buf[cnt++] = BPF_STX_MEM(BPF_DW, BPF_REG_10, reg, save_off);
+ insn_buf[cnt++] = ld_tmg[0];
+ insn_buf[cnt++] = ld_tmg[1];
+ insn_buf[cnt++] = BPF_MOV64_PERCPU_REG(reg, reg);
+ insn_buf[cnt++] = BPF_LDX_MEM(BPF_DW, BPF_REG_AX, reg, 0);
+ if (save_off) {
+ insn_buf[cnt++] = BPF_JMP_IMM(BPF_JNE, BPF_REG_AX, 0, 2);
+ insn_buf[cnt++] = BPF_LDX_MEM(BPF_DW, reg, BPF_REG_10, save_off);
+ jmp = cnt++;
+ insn_buf[jmp] = BPF_JMP32_A(off >= 0 ? off + tail_cnt : off - jmp);
+ } else {
+ jmp = cnt;
+ off = off >= 0 ? off + tail_cnt : off - jmp;
+ if (off == (s16)off) {
+ insn_buf[cnt++] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_AX, 0, off);
+ } else {
+ insn_buf[cnt++] = BPF_JMP_IMM(BPF_JNE, BPF_REG_AX, 0, 1);
+ insn_buf[cnt++] = BPF_JMP32_A(off >= 0 ? off : off - 1);
+ }
+ }
+ insn_buf[cnt++] = BPF_ALU64_IMM(BPF_SUB, BPF_REG_AX, 1);
+ insn_buf[cnt++] = BPF_JMP_IMM(BPF_JNE, BPF_REG_AX, 0, 2);
+ /* AX is the argument and the return value */
+ insn_buf[cnt++] = BPF_MOV64_REG(BPF_REG_AX, reg);
+ insn_buf[cnt++] = BPF_EMIT_CALL(arch_bpf_timed_may_goto);
+ insn_buf[cnt++] = BPF_STX_MEM(BPF_DW, reg, BPF_REG_AX, 0);
+ if (save_off)
+ insn_buf[cnt++] = BPF_LDX_MEM(BPF_DW, reg, BPF_REG_10, save_off);
+ return cnt;
+}
+
/* Do various post-verification rewrites in a single program pass.
* These rewrites simplify JIT and interpreter implementations.
*/
@@ -1823,6 +1867,9 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
struct bpf_subprog_info *subprogs = env->subprog_info;
u16 stack_depth = subprogs[cur_subprog].stack_depth;
u16 stack_depth_extra = 0;
+ /* arch_bpf_timed_may_goto() takes and returns AX in a cpu register: for JITs only */
+ bool timed_may_goto = prog->jit_requested && bpf_jit_supports_timed_may_goto();
+ struct bpf_timed_may_goto __percpu *tmg = NULL;
if (env->seen_exception && !env->exception_callback_subprog) {
struct bpf_insn *patch = insn_buf;
@@ -2078,38 +2125,28 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
goto next_insn;
}
- if (bpf_is_may_goto_insn(insn) && bpf_jit_supports_timed_may_goto()) {
- int stack_off_cnt = -stack_depth - 16;
- /*
- * AX is used as an argument to pass in stack_off_cnt
- * (to add to r10/fp), and also as the return value of
- * the call to arch_bpf_timed_may_goto.
- */
- struct bpf_insn tail[] = {
- BPF_ALU64_IMM(BPF_SUB, BPF_REG_AX, 1),
- BPF_JMP_IMM(BPF_JNE, BPF_REG_AX, 0, 2),
- BPF_MOV64_IMM(BPF_REG_AX, stack_off_cnt),
- BPF_EMIT_CALL(arch_bpf_timed_may_goto),
- BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_AX, stack_off_cnt),
- };
+ if (bpf_is_may_goto_insn(insn) && timed_may_goto) {
+ u16 live = env->insn_aux_data[i + delta].live_regs_before;
+ int reg, save_off = 0;
- /*
- * Two 8 byte slots, depth-16 stores the count, and
- * depth-8 stores the start timestamp of the loop.
- *
- * The starting value of count is BPF_MAX_TIMED_LOOPS
- * (0xffff). Every iteration loads it and subs it by 1,
- * until the value becomes 0 in AX (thus, 1 in stack),
- * after which we call arch_bpf_timed_may_goto, which
- * either sets AX to 0xffff to keep looping, or to 0
- * upon timeout. AX is then stored into the stack. In
- * the next iteration, we either see 0 and break out, or
- * continue iterating until the next time value is 0
- * after subtraction, rinse and repeat.
- */
- stack_depth_extra = 16;
- cnt = may_goto_expand(insn_buf, insn->off, stack_off_cnt,
- tail, ARRAY_SIZE(tail));
+ /* a register that is dead here holds the address of the count */
+ for (reg = BPF_REG_0; reg <= BPF_REG_9; reg++)
+ if (!(live & BIT(reg)))
+ break;
+ if (reg > BPF_REG_9) {
+ reg = BPF_REG_0;
+ save_off = -stack_depth - 8;
+ stack_depth_extra = 8;
+ }
+ if (!tmg) {
+ tmg = alloc_percpu_gfp(struct bpf_timed_may_goto,
+ GFP_KERNEL_ACCOUNT);
+ if (!tmg)
+ return -ENOMEM;
+ prog->aux->timed_may_goto = tmg;
+ }
+ prog->jit_required = 1;
+ cnt = may_goto_expand_timed(insn_buf, insn->off, tmg, reg, save_off);
new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
if (!new_prog)
@@ -2730,24 +2767,52 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
env->prog->aux->stack_depth = subprogs[0].stack_depth;
for (i = 0; i < env->subprog_cnt; i++) {
- int delta = bpf_jit_supports_timed_may_goto() ? 2 : 1;
int subprog_start = subprogs[i].start;
int stack_slots = subprogs[i].stack_extra / 8;
- int slots = delta, cnt = 0;
-
- if (!stack_slots)
+ /* an invocation begins, see bpf_timed_may_goto_save() for async callbacks */
+ bool main_prog = tmg && !i;
+ /* only a helper calls it, in a loop that ends when it returns 1 */
+ bool loop_cb = tmg && i && subprogs[i].is_loop_cb && !subprogs[i].is_called &&
+ !subprogs[i].is_async_cb;
+ int cnt = 0;
+
+ if (timed_may_goto ? !main_prog && !loop_cb : !stack_slots)
continue;
- /* We need two slots in case timed may_goto is supported. */
- if (stack_slots > slots) {
+ if (stack_slots > 1) {
verifier_bug(env, "stack_slots supports may_goto only");
return -EFAULT;
}
stack_depth = subprogs[i].stack_depth;
- if (bpf_jit_supports_timed_may_goto()) {
- insn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, -stack_depth,
- BPF_MAX_TIMED_LOOPS);
- insn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, -stack_depth + 8, 0);
+ if (timed_may_goto) {
+ struct bpf_insn ld_tmg[] = {
+ BPF_LD_IMM64(BPF_REG_0, (__force unsigned long)tmg)
+ };
+ int count_off = offsetof(struct bpf_timed_may_goto, count);
+ int timestamp_off = offsetof(struct bpf_timed_may_goto, timestamp);
+ int owner_off = offsetof(struct bpf_timed_may_goto, owner);
+
+ /* R0 has nothing at the entry */
+ insn_buf[cnt++] = ld_tmg[0];
+ insn_buf[cnt++] = ld_tmg[1];
+ insn_buf[cnt++] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);
+ if (main_prog) {
+ insn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_0, count_off,
+ BPF_MAX_TIMED_LOOPS);
+ insn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_0, timestamp_off, 0);
+ insn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_0, owner_off, 0);
+ } else {
+ /*
+ * The time is up, but the helper would call the function again and
+ * again. End its loop. The verifier has seen that: the loop can end
+ * after each call.
+ */
+ insn_buf[cnt++] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0,
+ count_off);
+ insn_buf[cnt++] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2);
+ insn_buf[cnt++] = BPF_MOV64_IMM(BPF_REG_0, 1);
+ insn_buf[cnt++] = BPF_EXIT_INSN();
+ }
} else {
/* Add ST insn to subprog prologue to init extra stack */
insn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, -stack_depth,
@@ -2774,7 +2839,7 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
* to insn after BPF_ST that inits may_goto count.
* Adjustment will succeed because bpf_patch_insn_data() didn't fail.
*/
- WARN_ON(adjust_jmp_off(env->prog, subprog_start, delta));
+ WARN_ON(adjust_jmp_off(env->prog, subprog_start, cnt - 1));
}
/* Since poke tab is now finalized, publish aux to tracker. */
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 58e357302850e3..2b75f7ba07a566 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -1222,6 +1222,8 @@ static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)
{
struct bpf_hrtimer *t = container_of(hrtimer, struct bpf_hrtimer, timer);
struct bpf_map *map = READ_ONCE(t->cb.map);
+ struct bpf_prog *prog = READ_ONCE(t->cb.prog);
+ struct bpf_timed_may_goto saved;
void *value = t->cb.value;
bpf_callback_t callback_fn;
void *key;
@@ -1229,7 +1231,7 @@ static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)
BTF_TYPE_EMIT(struct bpf_timer);
callback_fn = rcu_dereference_check(t->cb.callback_fn, rcu_read_lock_bh_held());
- if (!callback_fn || !map)
+ if (!callback_fn || !map || !prog)
goto out;
/* bpf_timer_cb() runs in hrtimer_run_softirq. It doesn't migrate and
@@ -1242,8 +1244,10 @@ static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)
key = map_key_from_value(map, value, &idx);
+ bpf_timed_may_goto_save(prog, &saved);
callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);
/* The verifier checked that return value is zero. */
+ bpf_timed_may_goto_restore(prog, &saved);
this_cpu_write(hrtimer_running, NULL);
out:
@@ -1254,6 +1258,8 @@ static void bpf_wq_work(struct work_struct *work)
{
struct bpf_work *w = container_of(work, struct bpf_work, work);
struct bpf_async_cb *cb = &w->cb;
+ struct bpf_timed_may_goto saved;
+ struct bpf_prog *prog;
struct bpf_map *map;
bpf_callback_t callback_fn;
void *value = cb->value;
@@ -1267,14 +1273,17 @@ static void bpf_wq_work(struct work_struct *work)
map = READ_ONCE(cb->map);
callback_fn = READ_ONCE(cb->callback_fn);
- if (!callback_fn || !map)
+ prog = READ_ONCE(cb->prog);
+ if (!callback_fn || !map || !prog)
goto out;
key = map_key_from_value(map, value, &idx);
migrate_disable();
+ bpf_timed_may_goto_save(prog, &saved);
callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);
+ bpf_timed_may_goto_restore(prog, &saved);
migrate_enable();
out:
@@ -4593,6 +4602,7 @@ static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx,
static void bpf_task_work_callback(struct callback_head *cb)
{
struct bpf_task_work_ctx *ctx = container_of(cb, struct bpf_task_work_ctx, work);
+ struct bpf_timed_may_goto saved;
enum bpf_task_work_state state;
u32 idx;
void *key;
@@ -4614,8 +4624,10 @@ static void bpf_task_work_callback(struct callback_head *cb)
key = (void *)map_key_from_value(ctx->map, ctx->map_val, &idx);
migrate_disable();
+ bpf_timed_may_goto_save(ctx->prog, &saved);
ctx->callback_fn((u64)(long)ctx->map, (u64)(long)key,
(u64)(long)ctx->map_val, 0, 0);
+ bpf_timed_may_goto_restore(ctx->prog, &saved);
migrate_enable();
bpf_task_work_ctx_reset(ctx);
@@ -4855,6 +4867,7 @@ static void bpf_rcu_run_callback(struct rcu_head *rcu)
struct bpf_rcu_head_kern *rh = container_of(rcu, struct bpf_rcu_head_kern, rcu);
bpf_callback_t callback_fn = rh->callback_fn;
struct bpf_prog *prog = rh->prog;
+ struct bpf_timed_may_goto saved;
struct bpf_map *map = rh->map;
void *value, *key;
u32 idx;
@@ -4866,7 +4879,9 @@ static void bpf_rcu_run_callback(struct rcu_head *rcu)
smp_store_release(&rh->armed, 0);
rcu_read_lock_dont_migrate();
+ bpf_timed_may_goto_save(prog, &saved);
callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);
+ bpf_timed_may_goto_restore(prog, &saved);
rcu_read_unlock_migrate();
bpf_prog_put(prog);
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 26a5ee0708a3d3..383cdc11b5b45f 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -11469,6 +11469,12 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins
if (err)
return err;
+ if (!bpf_pseudo_kfunc_call(insn) &&
+ (insn->imm == BPF_FUNC_loop || insn->imm == BPF_FUNC_for_each_map_elem))
+ env->subprog_info[subprog].is_loop_cb = true;
+ else
+ env->subprog_info[subprog].is_called = true;
+
/* for callback functions enqueue entry to callback and
* proceed with next instruction within current frame.
*/
@@ -11505,6 +11511,7 @@ static int check_static_func_call(struct bpf_verifier_env *env, int subprog,
int err;
caller = state->frame[state->curframe];
+ env->subprog_info[subprog].is_called = true;
/*
* Track caller's total stack arg count (incoming + max outgoing).
diff --git a/tools/testing/selftests/bpf/libarena/selftests/test_parallel_bitmap.bpf.c b/tools/testing/selftests/bpf/libarena/selftests/test_parallel_bitmap.bpf.c
index ea1fac95b461e5..017add229befe7 100644
--- a/tools/testing/selftests/bpf/libarena/selftests/test_parallel_bitmap.bpf.c
+++ b/tools/testing/selftests/bpf/libarena/selftests/test_parallel_bitmap.bpf.c
@@ -11,6 +11,7 @@
#define TEST_BITMAP_BITS (2 * BITS_PER_LONG_LONG)
#define TEST_BITMAP_SYNC_SPINS BPF_MAX_LOOPS
#define TEST_BITMAP_ITERS 10 * 1000 * 1000
+#define TEST_BITMAP_NSEC (50 * 1000 * 1000)
static struct arena_bitmap __arena *bitmap;
static volatile u64 started;
@@ -129,6 +130,7 @@ static int bitmap_test_common_single(int parity)
static int bitmap_test_common(int parity)
{
+ u64 start;
int ret;
u32 i;
@@ -138,6 +140,7 @@ static int bitmap_test_common(int parity)
if (ret)
return ret;
+ start = bpf_ktime_get_ns();
for (i = zero; i < TEST_BITMAP_ITERS && can_loop; i++) {
ret = bitmap_test_common_single(parity);
if (ret)
@@ -147,6 +150,13 @@ static int bitmap_test_common(int parity)
break;
bitmap_test_reset_single(parity);
+
+ /*
+ * Stop before the time slice of may_goto is used up. After that the
+ * bmp_*() functions can't retry a cmpxchg that fails.
+ */
+ if (bpf_ktime_get_ns() - start > TEST_BITMAP_NSEC)
+ break;
}
return 0;
diff --git a/tools/testing/selftests/bpf/prog_tests/may_goto_far.c b/tools/testing/selftests/bpf/prog_tests/may_goto_far.c
index 8d0ff30bd068cb..b6d262911a3ea3 100644
--- a/tools/testing/selftests/bpf/prog_tests/may_goto_far.c
+++ b/tools/testing/selftests/bpf/prog_tests/may_goto_far.c
@@ -34,8 +34,10 @@ static struct bpf_insn *fill(struct bpf_insn *insn, int cnt, int reg)
* The prog returns 0 when may_goto jumps to its target and 2 when
* it lands in one of 'r0 = r8' areas. There are no constants between
* may_goto and its target to keep the distance when constants are blinded.
+ *
+ * With 'subprog' all of that is a subprog.
*/
-static void test_far(int off)
+static void test_far(int off, bool subprog)
{
LIBBPF_OPTS(bpf_prog_load_opts, opts, .prog_flags = BPF_F_SLEEPABLE);
LIBBPF_OPTS(bpf_test_run_opts, topts);
@@ -50,6 +52,10 @@ static void test_far(int off)
return;
insn = insns;
+ if (subprog) {
+ *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 0, 1);
+ *insn++ = BPF_EXIT_INSN();
+ }
*insn++ = BPF_EMIT_CALL(BPF_FUNC_ktime_get_ns);
*insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_0);
*insn++ = BPF_ALU64_IMM(BPF_RSH, BPF_REG_6, 63);
@@ -82,11 +88,19 @@ static void test_far(int off)
void test_may_goto_far(void)
{
if (test__start_subtest("32762"))
- test_far(32762);
+ test_far(32762, false);
if (test__start_subtest("32767"))
- test_far(32767);
+ test_far(32767, false);
if (test__start_subtest("-32767"))
- test_far(-32767);
+ test_far(-32767, false);
if (test__start_subtest("-32768"))
- test_far(-32768);
+ test_far(-32768, false);
+ if (test__start_subtest("subprog 1"))
+ test_far(1, true);
+ if (test__start_subtest("subprog 32767"))
+ test_far(32767, true);
+ if (test__start_subtest("subprog -6"))
+ test_far(-6, true);
+ if (test__start_subtest("subprog -32768"))
+ test_far(-32768, true);
}
diff --git a/tools/testing/selftests/bpf/prog_tests/timed_may_goto.c b/tools/testing/selftests/bpf/prog_tests/timed_may_goto.c
new file mode 100644
index 00000000000000..3da3f33a2898b4
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/timed_may_goto.c
@@ -0,0 +1,195 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <sched.h>
+#include <test_progs.h>
+#include <network_helpers.h>
+#include "timed_may_goto.skel.h"
+#include "timed_may_goto_user.skel.h"
+#include "timed_may_goto_ext.skel.h"
+
+#define TRIES 4
+
+static int run_tc(struct bpf_program *prog)
+{
+ LIBBPF_OPTS(bpf_test_run_opts, opts,
+ .data_in = &pkt_v4,
+ .data_size_in = sizeof(pkt_v4),
+ .repeat = 1,
+ );
+
+ if (!ASSERT_OK(bpf_prog_test_run_opts(bpf_program__fd(prog), &opts), "test_run"))
+ return -1;
+ return opts.retval;
+}
+
+static int run_syscall(struct bpf_program *prog)
+{
+ LIBBPF_OPTS(bpf_test_run_opts, opts);
+
+ if (!ASSERT_OK(bpf_prog_test_run_opts(bpf_program__fd(prog), &opts), "test_run"))
+ return -1;
+ return opts.retval;
+}
+
+static void wait_for(int *val, int expected, const char *name)
+{
+ int i;
+
+ for (i = 0; i < 500 && *(volatile int *)val != expected; i++)
+ usleep(10000);
+ ASSERT_EQ(*val, expected, name);
+}
+
+static void test_async(struct timed_may_goto_user *skel)
+{
+ ASSERT_EQ(run_syscall(skel->progs.async_after_timeout), 0, "async_after_timeout");
+ wait_for(&skel->bss->timer_cnt, 1000, "timer_cnt");
+ wait_for(&skel->bss->wq_cnt, 1000, "wq_cnt");
+ wait_for(&skel->bss->tw_cnt, 1000, "tw_cnt");
+ wait_for(&skel->bss->rcu_cnt, 1000, "rcu_cnt");
+ wait_for(&skel->bss->rcu_trace_cnt, 1000, "rcu_trace_cnt");
+}
+
+static void test_timer_on_top(struct timed_may_goto_user *skel)
+{
+ int ms = run_syscall(skel->progs.spin_under_timer);
+ int ticks = skel->bss->ticks;
+
+ /* stop the timer */
+ skel->bss->ticks = 100;
+ ASSERT_GE(ms, 200, "ms");
+ ASSERT_LE(ms, 500, "ms");
+ /* control: the callback interrupted the prog, and its loop went on to the end */
+ ASSERT_GE(ticks, 5, "ticks");
+ ASSERT_LT(ticks, 100, "ticks");
+}
+
+static void test_freplace(struct timed_may_goto_user *skel)
+{
+ struct timed_may_goto_ext *ext;
+
+ ASSERT_EQ(run_tc(skel->progs.freplace_target), 1, "not replaced");
+
+ ext = timed_may_goto_ext__open();
+ if (!ASSERT_OK_PTR(ext, "ext_open"))
+ return;
+ bpf_program__set_attach_target(ext->progs.replacement,
+ bpf_program__fd(skel->progs.freplace_target),
+ "replaced_global");
+ if (!ASSERT_OK(timed_may_goto_ext__load(ext), "ext_load") ||
+ !ASSERT_OK(timed_may_goto_ext__attach(ext), "ext_attach"))
+ goto out;
+ ASSERT_EQ(run_tc(skel->progs.freplace_target), TRIES, "replaced");
+out:
+ timed_may_goto_ext__destroy(ext);
+ ASSERT_EQ(run_tc(skel->progs.freplace_target), 1, "not replaced");
+}
+
+/* What a prog leaves behind when its time is up doesn't matter for the next one on the cpu. */
+static void test_tail_call_and_throw(struct timed_may_goto_user *skel)
+{
+ int key = 0, fd = bpf_program__fd(skel->progs.tail_callee);
+ int map_fd = bpf_map__fd(skel->maps.jmp_table);
+
+ if (!ASSERT_OK(bpf_map_update_elem(map_fd, &key, &fd, 0), "update"))
+ return;
+
+ ASSERT_EQ(run_tc(skel->progs.normal_prog), 1, "normal_prog");
+ ASSERT_EQ(run_tc(skel->progs.normal_prog), 1, "normal_prog");
+
+ ASSERT_EQ(run_tc(skel->progs.tail_call_in_subprog), 1, "tail_call_in_subprog");
+ ASSERT_EQ(skel->bss->tail_callee_spun, 1, "tail_callee_spun");
+
+ ASSERT_EQ(run_tc(skel->progs.tail_call_after_timeout), 0, "tail_call_after_timeout");
+ ASSERT_EQ(skel->bss->tail_callee_spun, 1, "tail_callee_spun");
+
+ ASSERT_EQ(run_tc(skel->progs.throw_after_timeout), 7, "throw_after_timeout");
+ ASSERT_EQ(run_tc(skel->progs.throw_after_timeout), 7, "throw_after_timeout");
+}
+
+static void test_user_ringbuf(struct timed_may_goto_user *skel)
+{
+ struct user_ring_buffer *rb;
+ void *sample;
+ int i;
+
+ rb = user_ring_buffer__new(bpf_map__fd(skel->maps.user_ringbuf), NULL);
+ if (!ASSERT_OK_PTR(rb, "user_ring_buffer__new"))
+ return;
+ for (i = 0; i < TRIES; i++) {
+ sample = user_ring_buffer__reserve(rb, 8);
+ if (!ASSERT_OK_PTR(sample, "reserve"))
+ goto out;
+ user_ring_buffer__submit(rb, sample);
+ }
+ /* The helper takes a sample out of the buffer for each call: all calls have to run. */
+ ASSERT_EQ(run_syscall(skel->progs.drain), TRIES * 10 + 1, "drain");
+out:
+ user_ring_buffer__free(rb);
+}
+
+static void test_priv_stack(struct timed_may_goto_user *skel)
+{
+ LIBBPF_OPTS(bpf_test_run_opts, opts);
+ int fd = bpf_program__fd(skel->progs.priv_stack);
+ struct bpf_link *link;
+
+ link = bpf_program__attach(skel->progs.priv_stack);
+ if (!ASSERT_OK_PTR(link, "attach"))
+ return;
+ ASSERT_OK(bpf_prog_test_run_opts(fd, &opts), "test_run");
+ ASSERT_EQ(skel->bss->priv_stack_spun, 1, "priv_stack_spun");
+ ASSERT_EQ(skel->bss->priv_stack_cb_spun, 0, "priv_stack_cb_spun");
+ bpf_link__destroy(link);
+}
+
+/*
+ * Serial: most progs spin for 250 ms in an RCU read-side critical section, one after the other.
+ * That delays what other tests wait for. In parallel mode "mmap" gives up its wait for the end of
+ * a map.
+ */
+void serial_test_timed_may_goto(void)
+{
+ struct timed_may_goto_user *skel;
+ cpu_set_t old, new;
+
+ /* The interpreter counts in each frame. */
+ if (!is_jit_enabled()) {
+ test__skip();
+ return;
+ }
+
+ RUN_TESTS(timed_may_goto);
+
+#if !defined(__x86_64__) && !defined(__aarch64__)
+ return;
+#endif
+ skel = timed_may_goto_user__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ return;
+
+ /* Count and timestamp are per-cpu: all progs of a subtest run on one cpu. */
+ if (!ASSERT_OK(sched_getaffinity(0, sizeof(old), &old), "getaffinity"))
+ goto out;
+ CPU_ZERO(&new);
+ CPU_SET(sched_getcpu(), &new);
+ if (!ASSERT_OK(sched_setaffinity(0, sizeof(new), &new), "setaffinity"))
+ goto out;
+
+ if (test__start_subtest("async"))
+ test_async(skel);
+ if (test__start_subtest("timer_on_top"))
+ test_timer_on_top(skel);
+ if (test__start_subtest("freplace"))
+ test_freplace(skel);
+ if (test__start_subtest("tail_call_and_throw"))
+ test_tail_call_and_throw(skel);
+ if (test__start_subtest("user_ringbuf"))
+ test_user_ringbuf(skel);
+ if (test__start_subtest("priv_stack"))
+ test_priv_stack(skel);
+
+ sched_setaffinity(0, sizeof(old), &old);
+out:
+ timed_may_goto_user__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/arena_htab.c b/tools/testing/selftests/bpf/progs/arena_htab.c
index 81eaa94afeb01c..80cc14bb1c0f94 100644
--- a/tools/testing/selftests/bpf/progs/arena_htab.c
+++ b/tools/testing/selftests/bpf/progs/arena_htab.c
@@ -34,8 +34,6 @@ int arena_htab_llvm(void *ctx)
cast_kern(htab);
htab_init(htab);
- cast_kern(arr);
-
/* first run. No old elems in the table */
for (i = zero; i < 100000 && can_loop; i++) {
htab_update_elem(htab, i, i);
diff --git a/tools/testing/selftests/bpf/progs/timed_may_goto.c b/tools/testing/selftests/bpf/progs/timed_may_goto.c
new file mode 100644
index 00000000000000..c1c48af808a99c
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/timed_may_goto.c
@@ -0,0 +1,898 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * There is one time slice of may_goto for an invocation of a prog: one call from the kernel into
+ * the main prog, with the subprogs and sync callbacks that it reaches.
+ *
+ * Most tests call a function that spins until may_goto stops it, four times, each in a different
+ * way. Only the first call can spin. With a time slice for each call all four do.
+ */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+#include "bpf_experimental.h"
+
+char _license[] SEC("license") = "GPL";
+
+#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)) && __clang_major__ >= 18
+
+#define TRIES 4
+
+int zero;
+
+/* Return 1 if the loop went on until the time was up. */
+static __always_inline int spin(void)
+{
+ __u64 t0 = bpf_ktime_get_ns();
+
+ while (can_loop)
+ ;
+ return bpf_ktime_get_ns() - t0 > 100 * 1000 * 1000;
+}
+
+static __noinline int spins(void)
+{
+ return spin();
+}
+
+static __noinline int counts_to_1000(void)
+{
+ int i, cnt = zero;
+
+ for (i = zero; i < 1000 && can_loop; i++)
+ cnt++;
+ return cnt;
+}
+
+SEC("tc")
+__description("timed may_goto: call")
+__success __retval(1)
+int call_subprog(void *ctx)
+{
+ int i, n = 0;
+
+ for (i = 0; i < TRIES; i++)
+ n += spins();
+ return n;
+}
+
+SEC("tc")
+__description("timed may_goto: a call after the time is up makes no iteration")
+__success __retval(0)
+int short_loop_after_timeout(void *ctx)
+{
+ spins();
+ return counts_to_1000();
+}
+
+SEC("tc")
+__description("timed may_goto: the loop of the main prog stops when the time is up in a subprog")
+__success __retval(1)
+int main_loop_after_timeout(void *ctx)
+{
+ int n = 0;
+
+ while (can_loop && n < 2 * TRIES)
+ n += spins();
+ return n;
+}
+
+static __noinline int passes(int i)
+{
+ return spins() + i;
+}
+
+SEC("tc")
+__description("timed may_goto: call through a function without a loop")
+__success __retval(1)
+int call_through(void *ctx)
+{
+ int i, n = 0;
+
+ for (i = 0; i < TRIES; i++)
+ n += passes(zero);
+ return n;
+}
+
+__noinline int global_spins(void)
+{
+ return spin();
+}
+
+SEC("tc")
+__description("timed may_goto: call of a global function")
+__success __retval(1)
+int call_global(void *ctx)
+{
+ int i, n = 0;
+
+ for (i = 0; i < TRIES; i++)
+ n += global_spins();
+ return n;
+}
+
+SEC("tc")
+__description("timed may_goto: callx")
+__success __retval(1)
+__naked void callx_subprog(void)
+{
+ asm volatile (
+ "r6 = 0;"
+ "r7 = 0;"
+ "1:"
+ "r3 = %[spins] ll;"
+ "callx r3;"
+ "r6 += r0;"
+ "r7 += 1;"
+ "if r7 < 4 goto 1b;"
+ "r0 = r6;"
+ "exit;"
+ :
+ : __imm_addr(spins)
+ : __clobber_all);
+}
+
+/* With a time slice for each call it gives up, so that the test fails and doesn't lock up. */
+static int loop_cb(__u64 i, int *spun)
+{
+ *spun += spin();
+ return *spun > 2 * TRIES;
+}
+
+SEC("tc")
+__description("timed may_goto: bpf_loop() inlined")
+__success __retval(1)
+int loop_inlined(void *ctx)
+{
+ int spun = 0;
+
+ bpf_loop(1 << 20, loop_cb, &spun, 0);
+ return spun;
+}
+
+SEC("tc")
+__description("timed may_goto: bpf_loop() not inlined")
+__success __retval(1)
+int loop_not_inlined(void *ctx)
+{
+ int spun = 0;
+
+ bpf_loop(1 << 20, loop_cb, &spun, zero);
+ return spun;
+}
+
+static int count_cb(__u64 i, int *calls)
+{
+ (*calls)++;
+ return 0;
+}
+
+/* The time is up: the helper calls the callback one more time. It returns 1 in its first insns. */
+#define LOOP_ENDS(name, flags) \
+SEC("tc") \
+__description("timed may_goto: bpf_loop() " #name " ends when the time is up") \
+__success __retval(10) \
+int loop_ends_##name(void *ctx) \
+{ \
+ int calls = 0; \
+ \
+ spins(); \
+ return bpf_loop(1000, count_cb, &calls, flags) * 10 + calls; \
+}
+
+LOOP_ENDS(inlined, 0)
+LOOP_ENDS(not_inlined, zero)
+
+struct nested {
+ __u64 t0;
+ int spun;
+};
+
+/* Without an end of bpf_loop(): give up, so that the test fails and doesn't lock up. */
+static int inner_cb(__u64 i, struct nested *n)
+{
+ if (!n->spun)
+ n->spun = spin();
+ return (i & 1023) == 0 && bpf_ktime_get_ns() - n->t0 > 1000 * 1000 * 1000;
+}
+
+static int outer_cb(__u64 i, struct nested *n)
+{
+ bpf_loop(1 << 23, inner_cb, n, 0);
+ return bpf_ktime_get_ns() - n->t0 > 1000 * 1000 * 1000;
+}
+
+SEC("tc")
+__description("timed may_goto: bpf_loop() in bpf_loop() ends when the time is up")
+__success __retval(1)
+int loop_in_loop(void *ctx)
+{
+ struct nested n = { .t0 = bpf_ktime_get_ns() };
+
+ bpf_loop(1 << 23, outer_cb, &n, 0);
+ return n.spun && bpf_ktime_get_ns() - n.t0 < 500 * 1000 * 1000;
+}
+
+static __noinline int called_cb(__u64 i, int *calls)
+{
+ (*calls)++;
+ return 0;
+}
+
+/* BPF code calls the callback too, so its return value has to be its own. bpf_loop() goes on. */
+SEC("tc")
+__description("timed may_goto: bpf_loop() with a callback that is called as well")
+__success __retval(45)
+int loop_and_call(void *ctx)
+{
+ int calls = 0;
+
+ spins();
+ if (called_cb(zero, &calls))
+ return 100;
+ return bpf_loop(TRIES, called_cb, &calls, 0) * 10 + calls;
+}
+
+static int short_cb(__u64 i, __u64 *t0)
+{
+ int j;
+
+ for (j = zero; j < 60000 && can_loop; j++)
+ ;
+ return (i & 1023) == 0 && bpf_ktime_get_ns() - *t0 > 1000 * 1000 * 1000;
+}
+
+/*
+ * The main prog has no may_goto. Each call of the callback ends before it reads the clock. The
+ * iterations of all calls add up.
+ */
+SEC("tc")
+__description("timed may_goto: bpf_loop() the callback makes 60000 iterations")
+__success __retval(1)
+int loop_short_cb(void *ctx)
+{
+ __u64 t0 = bpf_ktime_get_ns();
+
+ bpf_loop(1 << 20, short_cb, &t0, 0);
+ return bpf_ktime_get_ns() - t0 < 1000 * 1000 * 1000;
+}
+
+/* spins() uses up the time. The loop that calls it ends at its next may_goto. */
+static __noinline int loops_around_spins(void)
+{
+ int n = zero;
+
+ while (can_loop && n < 20000) {
+ if (n < 2 * TRIES)
+ spins();
+ n++;
+ }
+ return n;
+}
+
+SEC("tc")
+__description("timed may_goto: a loop that is on the stack ends when the time is up")
+__success __retval(1)
+int rest_of_batch(void *ctx)
+{
+ return loops_around_spins();
+}
+
+static __noinline int one_iteration(void)
+{
+ int i, n = zero;
+
+ for (i = zero; i < 1 && can_loop; i++)
+ n++;
+ return n;
+}
+
+long calls_ok;
+
+static int call_one(__u64 i, __u64 *t0)
+{
+ if (!one_iteration())
+ return 1;
+ calls_ok++;
+ /* With a time slice for each call: give up. */
+ return (i & 1023) == 0 && bpf_ktime_get_ns() - *t0 > 1000 * 1000 * 1000;
+}
+
+static int call_many(__u64 i, __u64 *t0)
+{
+ long before = calls_ok;
+
+ bpf_loop(1 << 23, call_one, t0, 0);
+ return calls_ok - before != 1 << 23;
+}
+
+/*
+ * The iterations of all calls count: the prog reads the clock in each 65535th call. The call that
+ * finds out that the time is up makes its iteration.
+ */
+SEC("tc")
+__description("timed may_goto: 65535 calls that make one iteration read the clock")
+__success __retval(1)
+int clock_after_65535_calls(void *ctx)
+{
+ __u64 t0 = bpf_ktime_get_ns();
+
+ calls_ok = 0;
+ bpf_loop(1000, call_many, &t0, 0);
+ return calls_ok > 0 && calls_ok % 65535 == 0;
+}
+
+#define MAP(name, map_type, flags) \
+struct { \
+ __uint(type, map_type); \
+ __uint(max_entries, TRIES); \
+ __uint(map_flags, flags); \
+ __type(key, int); \
+ __type(value, int); \
+} name SEC(".maps")
+
+MAP(array, BPF_MAP_TYPE_ARRAY, 0);
+MAP(hash, BPF_MAP_TYPE_HASH, 0);
+
+/* The first call uses up the time. The second call ends the loop of the helper. */
+static int elem_cb(void *map, int *key, int *val, int *spun)
+{
+ *spun += spin();
+ return 0;
+}
+
+#define FOR_EACH(name) \
+SEC("tc") \
+__description("timed may_goto: bpf_for_each_map_elem() " #name) \
+__success __retval(21) \
+int for_each_##name(void *ctx) \
+{ \
+ int i, key, spun = 0; \
+ \
+ for (i = 0; i < TRIES; i++) { \
+ key = i; \
+ bpf_map_update_elem(&name, &key, &key, 0); \
+ } \
+ return bpf_for_each_map_elem(&name, elem_cb, &spun, 0) * 10 + spun; \
+}
+
+FOR_EACH(array)
+FOR_EACH(hash)
+
+static long vma_cb(struct task_struct *task, struct vm_area_struct *vma, int *spun)
+{
+ *spun += spin();
+ return 0;
+}
+
+SEC("raw_tp")
+__description("timed may_goto: bpf_find_vma()")
+__success __retval(1)
+int find_vma(void *ctx)
+{
+ struct task_struct *task = bpf_get_current_task_btf();
+ __u64 addr = task->mm->arg_start;
+ int i, spun = 0;
+
+ for (i = 0; i < TRIES; i++)
+ if (bpf_find_vma(task, addr, vma_cb, &spun, 0))
+ return 100;
+ return spun;
+}
+
+struct tree_node {
+ struct bpf_rb_node rb;
+ int key;
+};
+
+#define private(name) SEC(".data." #name) __hidden __attribute__((aligned(8)))
+private(A) struct bpf_spin_lock lock;
+private(A) struct bpf_rb_root root __contains(tree_node, rb);
+
+int less_spun, less_calls;
+
+static bool less(struct bpf_rb_node *a, const struct bpf_rb_node *b)
+{
+ long n = zero;
+
+ /* no helpers under the lock */
+ while (can_loop)
+ n++;
+ less_calls++;
+ less_spun += n > 1000 * 1000;
+ return container_of(a, struct tree_node, rb)->key <
+ container_of(b, struct tree_node, rb)->key;
+}
+
+SEC("tc")
+__description("timed may_goto: bpf_rbtree_add()")
+__success __retval(1)
+int rbtree_add(void *ctx)
+{
+ struct bpf_rb_node *rb;
+ struct tree_node *n;
+ int i;
+
+ less_spun = less_calls = 0;
+ for (i = 0; i < TRIES; i++) {
+ n = bpf_obj_new(typeof(*n));
+ if (!n)
+ return 100;
+ n->key = TRIES - i;
+ bpf_spin_lock(&lock);
+ bpf_rbtree_add(&root, &n->rb, less);
+ bpf_spin_unlock(&lock);
+ }
+ for (i = 0; i < TRIES; i++) {
+ bpf_spin_lock(&lock);
+ rb = bpf_rbtree_first(&root);
+ if (rb)
+ rb = bpf_rbtree_remove(&root, rb);
+ bpf_spin_unlock(&lock);
+ if (!rb)
+ return 101;
+ bpf_obj_drop(container_of(rb, struct tree_node, rb));
+ }
+ if (less_calls < TRIES - 1)
+ return 102;
+ return less_spun;
+}
+
+/*
+ * All registers are live at the may_goto, so it saves one of them in the stack. They are the same
+ * after 70000 iterations: the loop reads the clock.
+ */
+__naked __noinline __used
+static long regs_across_may_goto(void)
+{
+ asm volatile (
+ "r0 = 10;"
+ "r6 = 6;"
+ "r7 = 7;"
+ "r8 = 8;"
+ "r9 = 0;"
+ "1:"
+ "may_goto 2f;"
+ "r9 += 1;"
+ "if r9 < 70000 goto 1b;"
+ "2:"
+ "r0 += r1;"
+ "r0 += r2;"
+ "r0 += r3;"
+ "r0 += r4;"
+ "r0 += r5;"
+ "r0 += r6;"
+ "r0 += r7;"
+ "r0 += r8;"
+ "r0 += r9;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+SEC("tc")
+__description("timed may_goto: registers across may_goto")
+__success __retval(70046)
+__naked void regs_across_may_goto_main(void)
+{
+ asm volatile (
+ "r1 = 1;"
+ "r2 = 2;"
+ "r3 = 3;"
+ "r4 = 4;"
+ "r5 = 5;"
+ "call regs_across_may_goto;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+/*
+ * may_goto uses a register that is dead. In live_r0_rN() R0 - RN are live at the may_goto and the
+ * next one is not. The values come from memory, so the verifier doesn't know them. Return their sum
+ * and the number of iterations.
+ */
+__u64 vals[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+__u64 zero64;
+
+#define LD0(b) "r0 = *(u64 *)(r" #b " + 0);"
+#define LD1(b) LD0(b) "r1 = *(u64 *)(r" #b " + 8);"
+#define LD2(b) LD1(b) "r2 = *(u64 *)(r" #b " + 16);"
+#define LD3(b) LD2(b) "r3 = *(u64 *)(r" #b " + 24);"
+#define LD4(b) LD3(b) "r4 = *(u64 *)(r" #b " + 32);"
+#define LD5(b) LD4(b) "r5 = *(u64 *)(r" #b " + 40);"
+#define LD6(b) LD5(b) "r6 = *(u64 *)(r" #b " + 48);"
+#define LD7(b) LD6(b) "r7 = *(u64 *)(r" #b " + 56);"
+#define LD8(b) LD7(b) "r8 = *(u64 *)(r" #b " + 64);"
+
+#define SUM0
+#define SUM1 SUM0 "r0 += r1;"
+#define SUM2 SUM1 "r0 += r2;"
+#define SUM3 SUM2 "r0 += r3;"
+#define SUM4 SUM3 "r0 += r4;"
+#define SUM5 SUM4 "r0 += r5;"
+#define SUM6 SUM5 "r0 += r6;"
+#define SUM7 SUM6 "r0 += r7;"
+#define SUM8 SUM7 "r0 += r8;"
+
+/* The count of iterations is at r10 - 8. It begins with a number from memory too. */
+#define COUNT_INIT(t) \
+ "r" #t " = %[zero64] ll;" \
+ "r" #t " = *(u64 *)(r" #t " + 0);" \
+ "*(u64 *)(r10 - 8) = r" #t ";"
+#define COUNT_AND_LOOP(t) \
+ "r" #t " = *(u64 *)(r10 - 8);" \
+ "r" #t " += 1;" \
+ "*(u64 *)(r10 - 8) = r" #t ";" \
+ "if r" #t " < 70000 goto 1b;"
+#define ADD_COUNT_AND_EXIT \
+ "r1 = *(u64 *)(r10 - 8);" \
+ "r0 += r1;" \
+ "exit;" \
+ : \
+ : __imm_addr(vals), __imm_addr(zero64) \
+ : __clobber_all
+
+#define LIVE_REGS(n, next) \
+__naked __noinline __used \
+static long live_r0_r##n(void) \
+{ \
+ asm volatile ( \
+ COUNT_INIT(next) \
+ "r" #n " = %[vals] ll;" \
+ LD##n(n) \
+ "1:" \
+ "may_goto 2f;" \
+ COUNT_AND_LOOP(next) \
+ "2:" \
+ SUM##n \
+ ADD_COUNT_AND_EXIT); \
+}
+
+LIVE_REGS(0, 1)
+LIVE_REGS(1, 2)
+LIVE_REGS(2, 3)
+LIVE_REGS(3, 4)
+LIVE_REGS(4, 5)
+LIVE_REGS(5, 6)
+LIVE_REGS(6, 7)
+LIVE_REGS(7, 8)
+LIVE_REGS(8, 9)
+
+/* R2 is read at the top of the loop only: it is live at the may_goto through the back edge. */
+__naked __noinline __used
+static long live_back_edge(void)
+{
+ asm volatile (
+ COUNT_INIT(3)
+ "r2 = %[vals] ll;"
+ LD2(2)
+ "1:"
+ "*(u64 *)(r10 - 16) = r2;"
+ "may_goto 2f;"
+ COUNT_AND_LOOP(3)
+ "2:"
+ "r0 += r1;"
+ "r2 = *(u64 *)(r10 - 16);"
+ "r0 += r2;"
+ ADD_COUNT_AND_EXIT);
+}
+
+/* R2 is read only where the may_goto jumps to. */
+__naked __noinline __used
+static long live_target(void)
+{
+ asm volatile (
+ COUNT_INIT(3)
+ "r2 = %[vals] ll;"
+ LD2(2)
+ "1:"
+ "may_goto 2f;"
+ COUNT_AND_LOOP(3)
+ "goto 3f;"
+ "2:"
+ "r0 += r2;"
+ "3:"
+ "r0 += r1;"
+ ADD_COUNT_AND_EXIT);
+}
+
+/* R2 is set in front of the may_goto in each iteration, and read only right behind it. */
+__naked __noinline __used
+static long live_fall_through(void)
+{
+ asm volatile (
+ COUNT_INIT(3)
+ "r1 = %[vals] ll;"
+ LD1(1)
+ "*(u64 *)(r10 - 16) = r0;"
+ "1:"
+ "r2 = %[vals] ll;"
+ "r2 = *(u64 *)(r2 + 16);"
+ "may_goto 2f;"
+ "*(u64 *)(r10 - 16) = r2;"
+ COUNT_AND_LOOP(3)
+ "2:"
+ "r0 += r1;"
+ "r2 = *(u64 *)(r10 - 16);"
+ "r0 += r2;"
+ ADD_COUNT_AND_EXIT);
+}
+
+#define CALL_AND_ADD(f) "call " #f ";" "r6 += r0;"
+#define CALL_ALL_LIVE \
+ "r6 = 0;" \
+ CALL_AND_ADD(live_r0_r0) \
+ CALL_AND_ADD(live_r0_r1) \
+ CALL_AND_ADD(live_r0_r2) \
+ CALL_AND_ADD(live_r0_r3) \
+ CALL_AND_ADD(live_r0_r4) \
+ CALL_AND_ADD(live_r0_r5) \
+ CALL_AND_ADD(live_r0_r6) \
+ CALL_AND_ADD(live_r0_r7) \
+ CALL_AND_ADD(live_r0_r8) \
+ CALL_AND_ADD(live_back_edge) \
+ CALL_AND_ADD(live_target) \
+ CALL_AND_ADD(live_fall_through) \
+ "r0 = r6;" \
+ "exit;"
+
+/*
+ * The sums: 1 + 3 + 6 + 10 + 15 + 21 + 28 + 36 + 45 = 165. Then 6, 3 (the may_goto doesn't jump)
+ * and 6. 12 * 70000 iterations: each function reads the clock.
+ */
+SEC("tc")
+__description("timed may_goto: may_goto takes a register that is dead")
+__success __retval(840180)
+__naked void dead_reg(void)
+{
+ asm volatile (
+ CALL_ALL_LIVE
+ ::: __clobber_all);
+}
+
+/* No iteration: 165, then 6, 6 (the may_goto jumps) and 4 (R2 isn't stored). */
+SEC("tc")
+__description("timed may_goto: may_goto takes a register that is dead, the time is up")
+__success __retval(181)
+__naked void dead_reg_time_is_up(void)
+{
+ asm volatile (
+ "call spins_on_may_goto;"
+ CALL_ALL_LIVE
+ ::: __clobber_all);
+}
+
+/*
+ * The subprog uses up the time of the prog and returns. With a time slice for each call the caller
+ * calls it 65535 times before it reads the clock.
+ */
+__naked __noinline __used
+static unsigned long spins_on_may_goto(void)
+{
+ asm volatile (
+ "1:"
+ "may_goto 2f;"
+ "goto 1b;"
+ "2:"
+ "r0 = 0;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+/* The first may_goto of the function finds that the time is up */
+SEC("tc")
+__description("timed may_goto: registers across may_goto when the time is up")
+__success __retval(46)
+__naked void regs_when_time_is_up(void)
+{
+ asm volatile (
+ "call spins_on_may_goto;"
+ "r1 = 1;"
+ "r2 = 2;"
+ "r3 = 3;"
+ "r4 = 4;"
+ "r5 = 5;"
+ "call regs_across_may_goto;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+/* The first insn of the main prog is the target of a jump. It must not set the count again. */
+SEC("socket")
+__description("timed may_goto: 9 insns")
+__success __retval(1)
+__naked void loop_calls_subprog_with_may_goto(void)
+{
+ asm volatile (
+ "1:"
+ "call spins_on_may_goto;"
+ "may_goto 2f;"
+ "goto 1b;"
+ "2:"
+ "r0 = 1;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+/*
+ * These subprogs make 60000 iterations in each call. With a read of the clock after 65535
+ * iterations of a call none of them reads it, and their caller does it after
+ * 65535 * 60000 * 60000 iterations.
+ */
+__naked __noinline __used
+static unsigned long loops_60000(void)
+{
+ asm volatile (
+ "r1 = 60000;"
+ "1:"
+ "may_goto 2f;"
+ "r1 += -1;"
+ "if r1 != 0 goto 1b;"
+ "2:"
+ "r0 = 0;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+__naked __noinline __used
+static unsigned long calls_60000_times(void)
+{
+ asm volatile (
+ "r6 = 60000;"
+ "1:"
+ "call loops_60000;"
+ "may_goto 2f;"
+ "r6 += -1;"
+ "if r6 != 0 goto 1b;"
+ "2:"
+ "r0 = 0;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+SEC("socket")
+__description("timed may_goto: three levels of loops that end before they read the clock")
+__success __retval(1)
+__naked void three_levels_of_short_loops(void)
+{
+ asm volatile (
+ "1:"
+ "call calls_60000_times;"
+ "may_goto 2f;"
+ "goto 1b;"
+ "2:"
+ "r0 = 1;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+/* The callee ends one iteration before it reads the clock. */
+__naked __noinline __used
+static unsigned long loops_65534(void)
+{
+ asm volatile (
+ "r1 = 65534;"
+ "1:"
+ "may_goto 2f;"
+ "r1 += -1;"
+ "if r1 != 0 goto 1b;"
+ "2:"
+ "r0 = 0;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+SEC("socket")
+__description("timed may_goto: the subprog makes 65534 iterations")
+__success __retval(1)
+__naked void loop_calls_subprog_with_65534_iterations(void)
+{
+ asm volatile (
+ "call %[bpf_ktime_get_ns];"
+ "r6 = r0;"
+ "1:"
+ "call loops_65534;"
+ "may_goto 2f;"
+ "goto 1b;"
+ "2:"
+ "call %[bpf_ktime_get_ns];"
+ "r0 -= r6;"
+ "r1 = r0;"
+ "r0 = 1;"
+ "if r1 < 1000000000 goto +1;"
+ "r0 = 0;"
+ "exit;"
+ :
+ : __imm(bpf_ktime_get_ns)
+ : __clobber_all);
+}
+
+/* The subprog returns 16 bytes in R0 and R2. may_goto must not change them. */
+__naked __noinline __used
+static unsigned __int128 returns_pair_after_may_goto(void)
+{
+ asm volatile (
+ "r0 = 1;"
+ "r2 = 2;"
+ "1:"
+ "may_goto 2f;"
+ "goto 1b;"
+ "2:"
+ "exit;"
+ ::: __clobber_all);
+}
+
+SEC("socket")
+__description("timed may_goto: the subprog returns 16 bytes")
+__success __retval(3)
+__naked void subprog_with_may_goto_returns_pair(void)
+{
+ asm volatile (
+ "call returns_pair_after_may_goto;"
+ "r0 += r2;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+/* Dead code doesn't count: a prog that has no may_goto left gets no insns. */
+__naked __noinline __used
+static unsigned long may_goto_is_dead_code(void)
+{
+ asm volatile (
+ "r0 = 0;"
+ "if r0 == 0 goto 2f;"
+ "1:"
+ "may_goto 2f;"
+ "goto 1b;"
+ "2:"
+ "exit;"
+ ::: __clobber_all);
+}
+
+SEC("socket")
+__description("timed may_goto: may_goto in dead code")
+__success __retval(0)
+__xlated("0: call pc+1")
+__xlated("1: exit")
+__xlated("2: r0 = 0")
+__xlated("3: exit")
+__naked void dead_may_goto(void)
+{
+ asm volatile (
+ "call may_goto_is_dead_code;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+static __noinline int throws_after_spin(void)
+{
+ spin();
+ bpf_throw(0);
+ return 0;
+}
+
+__noinline int exception_cb_loops(u64 cookie)
+{
+ return counts_to_1000();
+}
+
+/* The exception callback belongs to the invocation that throws. */
+SEC("tc")
+__description("timed may_goto: bpf_throw() the exception callback has a loop")
+__exception_cb(exception_cb_loops)
+__success __retval(0)
+int throw_after_timeout(void *ctx)
+{
+ return throws_after_spin();
+}
+
+#else
+
+SEC("socket")
+__description("timed may_goto: not supported dummy test")
+__success
+int dummy_test(void)
+{
+ return 0;
+}
+
+#endif
diff --git a/tools/testing/selftests/bpf/progs/timed_may_goto_ext.c b/tools/testing/selftests/bpf/progs/timed_may_goto_ext.c
new file mode 100644
index 00000000000000..969ca58b6eedcc
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/timed_may_goto_ext.c
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include "bpf_experimental.h"
+
+char _license[] SEC("license") = "GPL";
+
+static __noinline int spins(void)
+{
+ __u64 t0 = bpf_ktime_get_ns();
+
+ while (can_loop)
+ ;
+ return bpf_ktime_get_ns() - t0 > 100 * 1000 * 1000;
+}
+
+/* Each call of an extension prog is an invocation: one of its two calls spins. */
+SEC("freplace")
+int replacement(int i)
+{
+ return spins() + spins() + i;
+}
diff --git a/tools/testing/selftests/bpf/progs/timed_may_goto_user.c b/tools/testing/selftests/bpf/progs/timed_may_goto_user.c
new file mode 100644
index 00000000000000..8b85815d45d25e
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/timed_may_goto_user.c
@@ -0,0 +1,311 @@
+// SPDX-License-Identifier: GPL-2.0
+/* The tests of timed_may_goto.c that need help from user space. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+#include "bpf_experimental.h"
+
+char _license[] SEC("license") = "GPL";
+
+#define TRIES 4
+#define CLOCK_MONOTONIC 1
+
+int zero;
+
+/* Return 1 if the loop went on until the time was up. */
+static __always_inline int spin(void)
+{
+ __u64 t0 = bpf_ktime_get_ns();
+
+ while (can_loop)
+ ;
+ return bpf_ktime_get_ns() - t0 > 100 * 1000 * 1000;
+}
+
+static __noinline int spins(void)
+{
+ return spin();
+}
+
+static __noinline int counts_to_1000(void)
+{
+ int i, cnt = zero;
+
+ for (i = zero; i < 1000 && can_loop; i++)
+ cnt++;
+ return cnt;
+}
+
+/* Nothing special: the loops go on, then the time is up one time. */
+SEC("tc")
+int normal_prog(void *ctx)
+{
+ int i, n = 0;
+
+ if (counts_to_1000() != 1000)
+ return 100;
+ for (i = 0; i < TRIES; i++)
+ n += spins();
+ return n;
+}
+
+/*
+ * Async callbacks
+ */
+struct elem {
+ struct bpf_timer timer;
+ struct bpf_wq wq;
+ struct bpf_task_work tw;
+ struct bpf_rcu_head rcu;
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, 2);
+ __type(key, int);
+ __type(value, struct elem);
+} async SEC(".maps");
+
+int timer_cnt, wq_cnt, tw_cnt, rcu_cnt, rcu_trace_cnt;
+
+static int timer_cb(void *map, int *key, struct elem *e)
+{
+ timer_cnt = counts_to_1000();
+ return 0;
+}
+
+static int wq_cb(void *map, int *key, void *val)
+{
+ wq_cnt = counts_to_1000();
+ return 0;
+}
+
+static int tw_cb(struct bpf_map *map, void *key, void *val)
+{
+ tw_cnt = counts_to_1000();
+ return 0;
+}
+
+static int rcu_cb(struct bpf_map *map, void *key, void *val)
+{
+ rcu_cnt = counts_to_1000();
+ return 0;
+}
+
+static int rcu_trace_cb(struct bpf_map *map, void *key, void *val)
+{
+ rcu_trace_cnt = counts_to_1000();
+ return 0;
+}
+
+/*
+ * The time of this prog is up when it starts the callbacks. User space keeps the task on the cpu,
+ * so the callbacks find what it has left. Each call of a callback is an invocation of its own.
+ */
+SEC("syscall")
+int async_after_timeout(void *ctx)
+{
+ struct task_struct *task = bpf_get_current_task_btf();
+ struct elem *e, *e1;
+ int key = 0, key1 = 1;
+
+ e = bpf_map_lookup_elem(&async, &key);
+ e1 = bpf_map_lookup_elem(&async, &key1);
+ if (!e || !e1)
+ return 100;
+ if (bpf_timer_init(&e->timer, &async, CLOCK_MONOTONIC) ||
+ bpf_timer_set_callback(&e->timer, timer_cb))
+ return 101;
+ if (bpf_wq_init(&e->wq, &async, 0) || bpf_wq_set_callback(&e->wq, wq_cb, 0))
+ return 102;
+ if (!spins())
+ return 103;
+ /* control: the time is up */
+ if (counts_to_1000())
+ return 109;
+ if (bpf_timer_start(&e->timer, 0, BPF_F_TIMER_CPU_PIN))
+ return 104;
+ if (bpf_wq_start(&e->wq, 0))
+ return 105;
+ if (bpf_task_work_schedule_resume(task, &e->tw, &async, tw_cb))
+ return 106;
+ if (bpf_call_rcu(&e->rcu, &async, rcu_cb))
+ return 107;
+ if (bpf_call_rcu_tasks_trace(&e1->rcu, &async, rcu_trace_cb))
+ return 108;
+ return 0;
+}
+
+int ticks;
+
+static int tick_cb(void *map, int *key, struct elem *e)
+{
+ if (counts_to_1000() != 1000)
+ ticks = 1000;
+ if (++ticks < 100)
+ bpf_timer_start(&e->timer, 20 * 1000 * 1000, BPF_F_TIMER_CPU_PIN);
+ return 0;
+}
+
+/*
+ * A callback of the prog interrupts it, on the same cpu, each 20 ms. It has its own time. That
+ * doesn't give more time to the prog. Return how long the loop went on, in ms.
+ */
+SEC("syscall")
+int spin_under_timer(void *ctx)
+{
+ struct elem *e;
+ int key = 1;
+ __u64 t0;
+
+ e = bpf_map_lookup_elem(&async, &key);
+ if (!e)
+ return 0;
+ if (bpf_timer_init(&e->timer, &async, CLOCK_MONOTONIC) ||
+ bpf_timer_set_callback(&e->timer, tick_cb) ||
+ bpf_timer_start(&e->timer, 20 * 1000 * 1000, BPF_F_TIMER_CPU_PIN))
+ return 0;
+ t0 = bpf_ktime_get_ns();
+ while (can_loop)
+ ;
+ return (bpf_ktime_get_ns() - t0) / 1000000;
+}
+
+/*
+ * Extension prog
+ */
+__noinline int replaced_global(int i)
+{
+ return spin() + i;
+}
+
+SEC("tc")
+int freplace_target(void *ctx)
+{
+ int i, n = 0;
+
+ for (i = 0; i < TRIES; i++)
+ n += replaced_global(zero);
+ return n;
+}
+
+/*
+ * Tail calls
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(int));
+ __uint(value_size, sizeof(int));
+} jmp_table SEC(".maps");
+
+int tail_callee_spun;
+
+SEC("tc")
+int tail_callee(struct __sk_buff *skb)
+{
+ int i;
+
+ tail_callee_spun = 0;
+ for (i = 0; i < TRIES; i++)
+ tail_callee_spun += spins();
+ return 0;
+}
+
+static __noinline int tail_calls(struct __sk_buff *skb)
+{
+ bpf_tail_call(skb, &jmp_table, 0);
+ return zero + 100;
+}
+
+/* The target of the tail call is another prog. It returns to this function. */
+SEC("tc")
+int tail_call_in_subprog(struct __sk_buff *skb)
+{
+ int i, n;
+
+ n = tail_calls(skb);
+ if (counts_to_1000() != 1000)
+ return 101;
+ for (i = 0; i < TRIES; i++)
+ n += spins();
+ return n;
+}
+
+SEC("tc")
+int tail_call_after_timeout(struct __sk_buff *skb)
+{
+ spins();
+ bpf_tail_call(skb, &jmp_table, 0);
+ return 100;
+}
+
+static __noinline int throws(void)
+{
+ spin();
+ bpf_throw(7);
+ return 0;
+}
+
+SEC("tc")
+int throw_after_timeout(void *ctx)
+{
+ return throws();
+}
+
+/*
+ * bpf_user_ringbuf_drain()
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_USER_RINGBUF);
+ __uint(max_entries, 4096);
+} user_ringbuf SEC(".maps");
+
+static long drain_cb(struct bpf_dynptr *dynptr, int *spun)
+{
+ *spun += spin();
+ return 0;
+}
+
+SEC("syscall")
+int drain(void *ctx)
+{
+ int spun = 0;
+
+ return bpf_user_ringbuf_drain(&user_ringbuf, drain_cb, &spun, 0) * 10 + spun;
+}
+
+/*
+ * A tracing prog with large frames: they are in a private stack.
+ */
+int priv_stack_spun, priv_stack_cb_spun;
+
+static __noinline int spins_with_stack(void)
+{
+ volatile char buf[128] = {};
+
+ return spin() + buf[zero & 127];
+}
+
+static int priv_stack_cb(__u64 i, int *spun)
+{
+ volatile char buf[128] = {};
+
+ *spun += spin() + buf[zero & 127];
+ return 0;
+}
+
+SEC("fentry/bpf_fentry_test1")
+int BPF_PROG(priv_stack)
+{
+ volatile char buf[128] = {};
+ int i, n = buf[zero & 127], spun = 0;
+
+ for (i = 0; i < TRIES; i++)
+ n += spins_with_stack();
+ bpf_loop(TRIES, priv_stack_cb, &spun, zero);
+ priv_stack_spun = n;
+ priv_stack_cb_spun = spun;
+ return 0;
+}
diff --git a/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c b/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c
index e0c389102db9f8..d908298b9fc796 100644
--- a/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c
+++ b/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c
@@ -903,26 +903,28 @@ __naked void unconditional_jump_to_fill(void)
SEC("raw_tp")
__arch_x86_64
__log_level(4)
-__msg("subprog 0 (may_goto_interaction_x86_64) main {{.*}} stack 24")
-/* may_goto counter at -24 */
-__xlated("0: *(u64 *)(r10 -24) =")
-/* may_goto timestamp at -16 */
-__xlated("1: *(u64 *)(r10 -16) =")
-__xlated("2: r1 = 1")
+__msg("subprog 0 (may_goto_interaction_x86_64) main {{.*}} stack 8")
+/* may_goto counter and timestamp */
+__xlated("0: r0 = ")
+__xlated("2: r0 = &(void __percpu *)(r0)")
__xlated("...")
-__xlated("4: r0 = &(void __percpu *)(r0)")
+__xlated("6: r1 = 1")
+__xlated("...")
+__xlated("8: r0 = &(void __percpu *)(r0)")
__xlated("...")
-/* may_goto expansion starts */
-__xlated("6: r12 = *(u64 *)(r10 -24)")
-__xlated("7: if r12 == 0x0 goto pc+6")
-__xlated("8: r12 -= 1")
-__xlated("9: if r12 != 0x0 goto pc+2")
-__xlated("10: r12 = -24")
-__xlated("11: call unknown")
-__xlated("12: *(u64 *)(r10 -24) = r12")
+/* may_goto expansion starts, r0 and r1 are live */
+__xlated("10: r2 = ")
+__xlated("12: r2 = &(void __percpu *)(r2)")
+__xlated("13: r12 = *(u64 *)(r2 +0)")
+__xlated("14: if r12 == 0x0 goto pc+6")
+__xlated("15: r12 -= 1")
+__xlated("16: if r12 != 0x0 goto pc+2")
+__xlated("17: r12 = r2")
+__xlated("18: call unknown")
+__xlated("19: *(u64 *)(r2 +0) = r12")
/* may_goto expansion ends */
-__xlated("13: *(u64 *)(r10 -8) = r1")
-__xlated("14: exit")
+__xlated("20: *(u64 *)(r10 -8) = r1")
+__xlated("21: exit")
__success
__naked void may_goto_interaction_x86_64(void)
{
@@ -946,24 +948,26 @@ __arch_arm64
__arch_riscv64
__arch_loongarch
__log_level(4)
-__msg("subprog 0 (may_goto_interaction) main {{.*}} stack 24")
-/* may_goto counter at -24 */
-__xlated("0: *(u64 *)(r10 -24) =")
-/* may_goto timestamp at -16 */
-__xlated("1: *(u64 *)(r10 -16) =")
-__xlated("2: r1 = 1")
-__xlated("3: call bpf_get_smp_processor_id")
-/* may_goto expansion starts */
-__xlated("4: r12 = *(u64 *)(r10 -24)")
-__xlated("5: if r12 == 0x0 goto pc+6")
-__xlated("6: r12 -= 1")
-__xlated("7: if r12 != 0x0 goto pc+2")
-__xlated("8: r12 = -24")
-__xlated("9: call unknown")
-__xlated("10: *(u64 *)(r10 -24) = r12")
+__msg("subprog 0 (may_goto_interaction) main {{.*}} stack 8")
+/* may_goto counter and timestamp */
+__xlated("0: r0 = ")
+__xlated("2: r0 = &(void __percpu *)(r0)")
+__xlated("...")
+__xlated("6: r1 = 1")
+__xlated("7: call bpf_get_smp_processor_id")
+/* may_goto expansion starts, r0 and r1 are live */
+__xlated("8: r2 = ")
+__xlated("10: r2 = &(void __percpu *)(r2)")
+__xlated("11: r12 = *(u64 *)(r2 +0)")
+__xlated("12: if r12 == 0x0 goto pc+6")
+__xlated("13: r12 -= 1")
+__xlated("14: if r12 != 0x0 goto pc+2")
+__xlated("15: r12 = r2")
+__xlated("16: call unknown")
+__xlated("17: *(u64 *)(r2 +0) = r12")
/* may_goto expansion ends */
-__xlated("11: *(u64 *)(r10 -8) = r1")
-__xlated("12: exit")
+__xlated("18: *(u64 *)(r10 -8) = r1")
+__xlated("19: exit")
__success
__naked void may_goto_interaction(void)
{
diff --git a/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c b/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c
index 1fbcc522830614..4dd98acc6436d4 100644
--- a/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c
+++ b/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c
@@ -553,7 +553,11 @@ int cond_break5(const void *ctx)
{
int cnt1 = zero, cnt2;
- for (;;) {
+ /*
+ * There is one time slice for all may_goto of the prog. When it's used up a function that
+ * is called after that makes no iteration. So this loop has an end.
+ */
+ for (; cnt1 < 100;) {
cond_break;
cnt1++;
}
diff --git a/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c b/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c
index faf9eec0a63ebc..20152a15ca360b 100644
--- a/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c
+++ b/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c
@@ -82,23 +82,31 @@ __naked void may_goto_batch_1(void)
SEC("raw_tp")
__description("may_goto batch with offsets 2/0")
+__load_if_JITed()
__arch_x86_64
__arch_s390x
__arch_arm64
__arch_riscv64
__arch_loongarch
-__xlated("0: *(u64 *)(r10 -16) = 65535")
-__xlated("1: *(u64 *)(r10 -8) = 0")
-__xlated("2: r12 = *(u64 *)(r10 -16)")
-__xlated("3: if r12 == 0x0 goto pc+6")
-__xlated("4: r12 -= 1")
-__xlated("5: if r12 != 0x0 goto pc+2")
-__xlated("6: r12 = -16")
-__xlated("7: call unknown")
-__xlated("8: *(u64 *)(r10 -16) = r12")
-__xlated("9: r0 = 1")
-__xlated("10: r0 = 2")
-__xlated("11: exit")
+/* the main prog sets count, timestamp, owner */
+__xlated("0: r0 = ")
+__xlated("2: r0 = &(void __percpu *)(r0)")
+__xlated("3: *(u64 *)(r0 +0) = 65535")
+__xlated("4: *(u64 *)(r0 +8) = 0")
+__xlated("5: *(u64 *)(r0 +16) = 0")
+/* may_goto, r0 is dead */
+__xlated("6: r0 = ")
+__xlated("8: r0 = &(void __percpu *)(r0)")
+__xlated("9: r12 = *(u64 *)(r0 +0)")
+__xlated("10: if r12 == 0x0 goto pc+6")
+__xlated("11: r12 -= 1")
+__xlated("12: if r12 != 0x0 goto pc+2")
+__xlated("13: r12 = r0")
+__xlated("14: call unknown")
+__xlated("15: *(u64 *)(r0 +0) = r12")
+__xlated("16: r0 = 1")
+__xlated("17: r0 = 2")
+__xlated("18: exit")
__success
__naked void may_goto_batch_2(void)
{
|
| 27/3 | 2026/11/01 12:41 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 33033 output: 23 thoughts: 1396 Thoughts: **KMSAN Fuzzing Evaluation for BPF Timed May_Goto Changes** Alright, let's break this down. My goal here is to determine if this patch series, targeting amd64, warrants a dedicated KMSAN fuzzing session. The core change seems to be around optimizing the implementation of `bpf_timed_may_goto`. Previously, stack space was allocated for a count and timestamp per BPF stack frame, but now, the code's transitioned to using per-CPU memory with `struct bpf_timed_may_goto`. This struct is allocated using `alloc_percpu_gfp`. The patch series updates `fixups.c`, modifies several architecture-specific files (including x86 which is notable), and includes selftests. The code generates BPF instructions to handle this memory, including loading the per-cpu pointer, decrementing a counter, and calling `arch_bpf_timed_may_goto`. The prologue for the main program initializes the counter, timestamp, and owner. Save and restore functions, `bpf_timed_may_goto_save` and `bpf_timed_may_goto_restore`, are introduced for async callbacks. These functions use `this_cpu_ptr()` to access per-CPU memory. Now, things get interesting. I need to scrutinize these save/restore functions. Specifically, I see that within the `bpf_timer_cb` function, a local `saved` variable of type `struct bpf_timed_may_goto` is used. The key is what happens when `prog->aux->timed_may_goto` is NULL. If it is NULL, `bpf_timed_may_goto_save` returns without writing to `saved` – which makes `saved` an uninitialized stack variable. But, the restore function, when `timed_may_goto` is NULL, *also* returns early, leaving `saved` unread. This seems safe. But, if `timed_may_goto` IS non-NULL, then `*saved = *cur;` copies data from per-CPU memory into the `saved` variable. The question is: is this safe in terms of memory initialization? I'm going to look more closely at the implementation of `alloc_percpu_gfp` to ensure the memory allocated is zeroed, because if it *is* zeroed, then this is safe, because initialization of the saved struct is guaranteed in *that* code path. If so, then perhaps this doesn't warrant a dedicated KMSAN session. |
| 28/3 | 2026/11/01 12:41 | tool | grepper |
0mArgs:{
"Expression": "#define alloc_percpu_gfp"
}Results: {
"Output": "include/linux/percpu.h=137=extern void __percpu *pcpu_alloc_noprof(size_t size, size_t align, bool reserved,\n--\ninclude/linux/percpu.h-146-\ninclude/linux/percpu.h:147:#define alloc_percpu_gfp(type, gfp)\t\t\t\t\t\\\ninclude/linux/percpu.h-148-\t(typeof(type) __percpu *)__alloc_percpu_gfp(sizeof(type),\t\\\n"
} |
| 29/3 | 2026/11/01 12:41 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 35135 output: 35 thoughts: 1510 Thoughts: **Analyzing `bpf_timed_may_goto` and its Usage** Okay, so I'm diving into this `bpf_timed_may_goto` structure and how it's used within the BPF framework. First, let's establish the basics. Per-cpu memory allocations (`__alloc_percpu_gfp`) are initialized to zero by `pcpu_alloc`, which is a good starting point. Then, I see the `main_prog` prologue explicitly writing zero values for the `count`, `timestamp`, and `owner` fields of this structure, all 64-bit, to pre-defined offsets. This tells me that they're being explicitly initialized. Let's quickly refresh what `struct bpf_timed_may_goto` actually *is*. Ah, it's pretty straightforward: `u64 count`, `u64 timestamp`, and a `void *owner`. On a 64-bit architecture, this is 24 bytes in total - no padding to worry about. Good. Now, I'm thinking about the save and restore functions, specifically if the `prog` (program) pointer is consistent across those operations. I'm seeing that the `prog` pointer is the same in `bpf_timed_may_goto_save` and `bpf_timed_may_goto_restore` across the various callback functions: `bpf_timer_cb`, `bpf_wq_work`, `bpf_task_work_callback`, and `bpf_rcu_run_callback`. The code clearly uses the same `prog` pointer for both save and restore. But, a thought emerges - what if the `bpf_timed_may_goto_save` *didn't* initialize `saved` because `prog->aux->timed_may_goto` was initially NULL, then, during the `callback_fn`, `prog->aux->timed_may_goto` *becomes* non-NULL? I have to keep in mind, however, that BPF programs are verified, JITed, and loaded. That `prog->aux->timed_may_goto` is allocated during `bpf_do_misc_fixups` *at program load time*, and critically, it *never* changes after the program is loaded! Therefore, once loaded, the `prog->aux->timed_may_goto` is constant for the entire lifetime of the `prog`. Thus, if it was NULL at the save, it is guaranteed to still be NULL at the restore. Now, I'm analyzing the `bpf_timed_may_goto_restore` function itself. The function returns early if `timed_may_goto` is NULL, bypassing touching the `saved` memory. Also, the check `cur->owner == saved` is interesting. Crucially, the code is only comparing the *pointer* value (the address of `saved`) not the contents. So, if that condition is true, then `cur->owner` *must* have been set in `bpf_timed_may_goto_save`, which means the previous line `*cur = *saved` was executed. This is good because it implies that if it gets to this point, then `*saved` will have been initialized. Now, I'm curious about how `subprogs` and `is_loop_cb` interact with all this. I'll need to check how these flags are initialized and used in the verifier. |
| 30/3 | 2026/11/01 12:41 | tool | grepper |
0mArgs:{
"Expression": "is_loop_cb|is_called",
"PathPrefixes": [
"kernel/bpf/"
]
}Results: {
"Output": "kernel/bpf/fixups.c=1853=int bpf_do_misc_fixups(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-2774-\t\t/* only a helper calls it, in a loop that ends when it returns 1 */\nkernel/bpf/fixups.c:2775:\t\tbool loop_cb = tmg \u0026\u0026 i \u0026\u0026 subprogs[i].is_loop_cb \u0026\u0026 !subprogs[i].is_called \u0026\u0026\nkernel/bpf/fixups.c-2776-\t\t\t !subprogs[i].is_async_cb;\n--\nkernel/bpf/verifier.c=11406=static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-11473-\t (insn-\u003eimm == BPF_FUNC_loop || insn-\u003eimm == BPF_FUNC_for_each_map_elem))\nkernel/bpf/verifier.c:11474:\t\tenv-\u003esubprog_info[subprog].is_loop_cb = true;\nkernel/bpf/verifier.c-11475-\telse\nkernel/bpf/verifier.c:11476:\t\tenv-\u003esubprog_info[subprog].is_called = true;\nkernel/bpf/verifier.c-11477-\n--\nkernel/bpf/verifier.c=11504=static int check_static_func_call(struct bpf_verifier_env *env, int subprog,\n--\nkernel/bpf/verifier.c-11513-\tcaller = state-\u003eframe[state-\u003ecurframe];\nkernel/bpf/verifier.c:11514:\tenv-\u003esubprog_info[subprog].is_called = true;\nkernel/bpf/verifier.c-11515-\n"
} |
| 31/3 | 2026/11/01 12:41 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 37917 output: 33 thoughts: 19 Thoughts: Here's my take on the thought process: **Diving into `subprog_info` Initialization** Alright, let's get down to brass tacks. I need to understand how the `subprog_info` structure is initially set up within `verifier.c`. This is crucial for tracing its lifecycle and understanding how the verifier's assumptions about subprograms are established. I'll need to go directly to the source code to confirm how this is done. Understanding its initial state is the cornerstone of understanding its later usage. I will be looking for the initialization method, the variables used in its instantiation, and the values assigned to each member of the structure. Knowing the genesis of `subprog_info` will help me understand its subsequent evolution during verification. |
| 32/3 | 2026/11/01 12:42 | tool | grepper |
0mArgs:{
"Expression": "subprog_info",
"PathPrefixes": [
"kernel/bpf/verifier.c"
]
}Results: {
"Output": "\nFull output is too long, showing 200 out of 306 lines.\nUse more precise expression or PathPrefixes if possible.\n\nNumber of matching lines per file (1 files in total):\nkernel/bpf/verifier.c:65\n\nkernel/bpf/verifier.c=443=static int bpf_compute_subprog_ret_regs(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-466-\t\tif (ret_regs_cnt(size) \u003e 1) {\nkernel/bpf/verifier.c:467:\t\t\tsubprog_info(env, subprog)-\u003eret_reg_pair = true;\nkernel/bpf/verifier.c-468-\t\t\tenv-\u003eprog-\u003ejit_required = 1;\n--\nkernel/bpf/verifier.c=486=void bpf_mark_subprog_exc_cb(struct bpf_verifier_env *env, int subprog)\nkernel/bpf/verifier.c-487-{\nkernel/bpf/verifier.c:488:\tstruct bpf_subprog_info *info = subprog_info(env, subprog);\nkernel/bpf/verifier.c-489-\n--\nkernel/bpf/verifier.c=495=static bool subprog_is_exc_cb(struct bpf_verifier_env *env, int subprog)\nkernel/bpf/verifier.c-496-{\nkernel/bpf/verifier.c:497:\treturn subprog_info(env, subprog)-\u003eis_exception_cb;\nkernel/bpf/verifier.c-498-}\n--\nkernel/bpf/verifier.c=1472=static int resize_reference_state(struct bpf_verifier_state *state, size_t n)\n--\nkernel/bpf/verifier.c-1483-/* Possibly update state-\u003eallocated_stack to be at least size bytes. Also\nkernel/bpf/verifier.c:1484: * possibly update the function's high-water mark in its bpf_subprog_info.\nkernel/bpf/verifier.c-1485- */\nkernel/bpf/verifier.c=1486=int bpf_grow_stack_state(struct bpf_verifier_env *env, struct bpf_func_state *state, int size)\n--\nkernel/bpf/verifier.c-1503-\t/* update known max for given subprogram */\nkernel/bpf/verifier.c:1504:\tif (env-\u003esubprog_info[state-\u003esubprogno].stack_depth \u003c size)\nkernel/bpf/verifier.c:1505:\t\tenv-\u003esubprog_info[state-\u003esubprogno].stack_depth = size;\nkernel/bpf/verifier.c-1506-\n--\nkernel/bpf/verifier.c=2578=static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-2620-/* Find subprogram that contains instruction at 'off' */\nkernel/bpf/verifier.c:2621:struct bpf_subprog_info *bpf_find_containing_subprog(struct bpf_verifier_env *env, int off)\nkernel/bpf/verifier.c-2622-{\nkernel/bpf/verifier.c:2623:\tstruct bpf_subprog_info *vals = env-\u003esubprog_info;\nkernel/bpf/verifier.c-2624-\tint l, r, m;\n--\nkernel/bpf/verifier.c=2642=int bpf_find_subprog(struct bpf_verifier_env *env, int off)\nkernel/bpf/verifier.c-2643-{\nkernel/bpf/verifier.c:2644:\tstruct bpf_subprog_info *p;\nkernel/bpf/verifier.c-2645-\n--\nkernel/bpf/verifier.c-2648-\t\treturn -ENOENT;\nkernel/bpf/verifier.c:2649:\treturn p - env-\u003esubprog_info;\nkernel/bpf/verifier.c-2650-}\n--\nkernel/bpf/verifier.c=2652=static int add_subprog(struct bpf_verifier_env *env, int off)\nkernel/bpf/verifier.c-2653-{\nkernel/bpf/verifier.c:2654:\tstruct bpf_subprog_info *subprog = env-\u003esubprog_info;\nkernel/bpf/verifier.c-2655-\tint insn_cnt = env-\u003eprog-\u003elen;\n--\nkernel/bpf/verifier.c-2674-\t\tsubprog[i] = subprog[i - 1];\nkernel/bpf/verifier.c:2675:\tsubprog[i] = (struct bpf_subprog_info){ .start = off, .exit_idx = U32_MAX };\nkernel/bpf/verifier.c-2676-\treturn i;\n--\nkernel/bpf/verifier.c=3207=static int add_subprogs(struct bpf_verifier_env *env)\nkernel/bpf/verifier.c-3208-{\nkernel/bpf/verifier.c:3209:\tstruct bpf_subprog_info *subprog = env-\u003esubprog_info;\nkernel/bpf/verifier.c-3210-\tint i, ret, insn_cnt = env-\u003eprog-\u003elen, ex_cb_insn;\n--\nkernel/bpf/verifier.c-3272-\t\tfor (i = 1; i \u003c env-\u003esubprog_cnt; i++) {\nkernel/bpf/verifier.c:3273:\t\t\tif (env-\u003esubprog_info[i].start != ex_cb_insn)\nkernel/bpf/verifier.c-3274-\t\t\t\tcontinue;\n--\nkernel/bpf/verifier.c=3319=static void find_subprog_properties(struct bpf_verifier_env *env)\nkernel/bpf/verifier.c-3320-{\nkernel/bpf/verifier.c:3321:\tstruct bpf_subprog_info *subprog = env-\u003esubprog_info;\nkernel/bpf/verifier.c-3322-\tstruct bpf_insn *insn = env-\u003eprog-\u003einsnsi;\n--\nkernel/bpf/verifier.c=3347=static int check_subprogs(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-3349-\tint i, subprog_start, subprog_end, off, cur_subprog = 0;\nkernel/bpf/verifier.c:3350:\tstruct bpf_subprog_info *subprog = env-\u003esubprog_info;\nkernel/bpf/verifier.c-3351-\tstruct bpf_insn *insn = env-\u003eprog-\u003einsnsi;\n--\nkernel/bpf/verifier.c=3448=static int check_unwind_insns(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-3478-\tenv-\u003eprog-\u003ejit_required = 1;\nkernel/bpf/verifier.c:3479:\tenv-\u003eprog-\u003eaux-\u003ecan_unwind = env-\u003esubprog_info[0].can_unwind;\nkernel/bpf/verifier.c-3480-\treturn 0;\n--\nkernel/bpf/verifier.c=3494=static int sort_subprogs_topo(struct bpf_verifier_env *env)\nkernel/bpf/verifier.c-3495-{\nkernel/bpf/verifier.c:3496:\tstruct bpf_subprog_info *si = env-\u003esubprog_info;\nkernel/bpf/verifier.c-3497-\tint *insn_postorder = env-\u003ecfg.insn_postorder;\n--\nkernel/bpf/verifier.c=3877=static void check_fastcall_stack_contract(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-3879-{\nkernel/bpf/verifier.c:3880:\tstruct bpf_subprog_info *subprog = \u0026env-\u003esubprog_info[state-\u003esubprogno];\nkernel/bpf/verifier.c-3881-\tstruct bpf_insn_aux_data *aux = env-\u003einsn_aux_data;\n--\nkernel/bpf/verifier.c=4596=static int check_stack_arg_write(struct bpf_verifier_env *env, struct bpf_func_state *state,\n--\nkernel/bpf/verifier.c-4599-\tint max_stack_arg_regs = MAX_BPF_FUNC_ARGS - MAX_BPF_FUNC_REG_ARGS;\nkernel/bpf/verifier.c:4600:\tstruct bpf_subprog_info *subprog = \u0026env-\u003esubprog_info[state-\u003esubprogno];\nkernel/bpf/verifier.c-4601-\tint spi = -off / BPF_REG_SIZE - 1;\n--\nkernel/bpf/verifier.c=4639=static int check_stack_arg_read(struct bpf_verifier_env *env, struct bpf_func_state *state,\n--\nkernel/bpf/verifier.c-4641-{\nkernel/bpf/verifier.c:4642:\tstruct bpf_subprog_info *subprog = \u0026env-\u003esubprog_info[state-\u003esubprogno];\nkernel/bpf/verifier.c-4643-\tstruct bpf_verifier_state *vstate = env-\u003ecur_state;\n--\nkernel/bpf/verifier.c=5751=static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx,\n--\nkernel/bpf/verifier.c-5754-{\nkernel/bpf/verifier.c:5755:\tstruct bpf_subprog_info *subprog = env-\u003esubprog_info;\nkernel/bpf/verifier.c-5756-\tstruct bpf_insn *insn = env-\u003eprog-\u003einsnsi;\n--\nkernel/bpf/verifier.c=6001=static int check_max_stack_depth(struct bpf_verifier_env *env)\n--\nkernel/bpf/verifier.c-6004-\tstruct bpf_subprog_call_depth_info *dinfo;\nkernel/bpf/verifier.c:6005:\tstruct bpf_subprog_info *si = env-\u003esubprog_info;\nkernel/bpf/verifier.c-6006-\tbool priv_stack_supported;\n--\nkernel/bpf/verifier.c=6442=void bpf_adjust_func_ptrs_after_remove(struct bpf_verifier_env *env, u32 off, u32 len)\n--\nkernel/bpf/verifier.c-6456-\t\tsubprog = bpf_find_subprog(env, p-\u003exlated_off);\nkernel/bpf/verifier.c:6457:\t\tif (subprog \u003e 0 \u0026\u0026 env-\u003esubprog_info[subprog + 1].start \u003e off + len)\nkernel/bpf/verifier.c-6458-\t\t\tp-\u003exlated_off = off;\n--\nkernel/bpf/verifier.c=11244=static int setup_func_entry(struct bpf_verifier_env *env, int subprog, int callsite,\n--\nkernel/bpf/verifier.c-11291-\nkernel/bpf/verifier.c:11292:static void gen_subprog_arg_proto(const struct bpf_subprog_info *sub, const struct btf *btf,\nkernel/bpf/verifier.c-11293-\t\t\t\t const struct btf_type *func_proto, struct bpf_func_proto *proto)\n--\nkernel/bpf/verifier.c=11324=static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog, struct btf *btf)\nkernel/bpf/verifier.c-11325-{\nkernel/bpf/verifier.c:11326:\tstruct bpf_subprog_info *sub = subprog_info(env, subprog);\nkernel/bpf/verifier.c-11327-\tstruct bpf_func_state *caller = cur_func(env);\n--\nkernel/bpf/verifier.c=11406=static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-11417-\t\treturn err;\nkernel/bpf/verifier.c:11418:\tif (bpf_in_stack_arg_cnt(\u0026env-\u003esubprog_info[subprog])) {\nkernel/bpf/verifier.c-11419-\t\tverbose(env, \"callback subprog cannot have stack args\\n\");\n--\nkernel/bpf/verifier.c-11426-\t */\nkernel/bpf/verifier.c:11427:\tenv-\u003esubprog_info[subprog].is_cb = true;\nkernel/bpf/verifier.c-11428-\tif (bpf_pseudo_kfunc_call(insn) \u0026\u0026\n--\nkernel/bpf/verifier.c-11443-\t\t/* there is no real recursion here. timer and workqueue callbacks are async */\nkernel/bpf/verifier.c:11444:\t\tenv-\u003esubprog_info[subprog].is_async_cb = true;\nkernel/bpf/verifier.c:11445:\t\tasync_cb = push_async_cb(env, env-\u003esubprog_info[subprog].start,\nkernel/bpf/verifier.c-11446-\t\t\t\t\t insn_idx, subprog,\n--\nkernel/bpf/verifier.c-11473-\t (insn-\u003eimm == BPF_FUNC_loop || insn-\u003eimm == BPF_FUNC_for_each_map_elem))\nkernel/bpf/verifier.c:11474:\t\tenv-\u003esubprog_info[subprog].is_loop_cb = true;\nkernel/bpf/verifier.c-11475-\telse\nkernel/bpf/verifier.c:11476:\t\tenv-\u003esubprog_info[subprog].is_called = true;\nkernel/bpf/verifier.c-11477-\n--\nkernel/bpf/verifier.c-11480-\t */\nkernel/bpf/verifier.c:11481:\tcallback_state = push_stack(env, env-\u003esubprog_info[subprog].start, insn_idx, false);\nkernel/bpf/verifier.c-11482-\tif (IS_ERR(callback_state))\n--\nkernel/bpf/verifier.c=11504=static int check_static_func_call(struct bpf_verifier_env *env, int subprog,\n--\nkernel/bpf/verifier.c-11507-\tstruct bpf_verifier_state *state = env-\u003ecur_state;\nkernel/bpf/verifier.c:11508:\tstruct bpf_subprog_info *caller_info;\nkernel/bpf/verifier.c-11509-\tu16 callee_incoming, stack_arg_cnt;\n--\nkernel/bpf/verifier.c-11513-\tcaller = state-\u003eframe[state-\u003ecurframe];\nkernel/bpf/verifier.c:11514:\tenv-\u003esubprog_info[subprog].is_called = true;\nkernel/bpf/verifier.c-11515-\n--\nkernel/bpf/verifier.c-11519-\t */\nkernel/bpf/verifier.c:11520:\tcaller_info = \u0026env-\u003esubprog_info[caller-\u003esubprogno];\nkernel/bpf/verifier.c:11521:\tcallee_incoming = bpf_in_stack_arg_cnt(\u0026env-\u003esubprog_info[subprog]);\nkernel/bpf/verifier.c-11522-\tstack_arg_cnt = bpf_in_stack_arg_cnt(caller_info) + callee_incoming;\n--\nkernel/bpf/verifier.c-11537-\t/* and go analyze first insn of the callee */\nkernel/bpf/verifier.c:11538:\t*insn_idx = env-\u003esubprog_info[subprog].start - 1;\nkernel/bpf/verifier.c-11539-\n--\nkernel/bpf/verifier.c=11550=static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n--\nkernel/bpf/verifier.c-11582-\nkernel/bpf/verifier.c:11583:\t\tif (env-\u003esubprog_info[subprog].might_sleep \u0026\u0026 !in_sleepable_context(env)) {\nkernel/bpf/verifier.c-11584-\t\t\tconst char *suggestion;\n--\nkernel/bpf/verifier.c-11612-\t\treturns_void = subprog_returns_void(env, subprog);\nkernel/bpf/verifier.c:11613:\t\tif (env-\u003esubprog_info[subprog].changes_pkt_data)\nkernel/bpf/verifier.c-11614-\t\t\tclear_all_pkt_pointers(env);\n--\nkernel/bpf/verifier.c-11624-\t\t */\nkernel/bpf/verifier.c:11625:\t\tif (!env-\u003esubprog_info[subprog].changes_pkt_data \u0026\u0026\nkernel/bpf/verifier.c-11626-\t\t (state-\u003eframe[0]-\u003esubprogno || holds_pkt_pointers(env)))\n--\nkernel/bpf/verifier.c-11644-\nkernel/bpf/verifier.c:11645:\t\tif (env-\u003esubprog_info[subprog].can_unwind) {\nkernel/bpf/verifier.c-11646-\t\t\tstruct bpf_verifier_state *branch;\n--\nkernel/bpf/verifier.c-11657-\nkernel/bpf/verifier.c:11658:\t\tif (env-\u003esubprog_info[subprog].might_throw) {\nkernel/bpf/verifier.c-11659-\t\t\tstruct bpf_verifier_state *branch;\n--\nkernel/bpf/verifier.c=12035=static void account_processed_insn(struct bpf_verifier_env *env)\n--\n\n"
} |
| 33/3 | 2026/11/01 12:42 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 42828 output: 36 thoughts: 251 Thoughts: **My Analysis of Subprogram Information and Timed `may_goto` in BPF** Okay, so I'm looking at how subprogram information is managed within the BPF verifier. The `subprog_info` array is a key component, residing within the `bpf_verifier_env` struct. This `bpf_verifier_env` is allocated with `kzalloc`, so it's initialized, which is good. When new subprograms are introduced via `add_subprog`, I see how the `subprog` array is populated. Each new subprogram entry, `subprog[i]`, gets its `start` offset assigned. The `exit_idx` is initially set to `U32_MAX`, which effectively signifies an uninitialized exit point – and importantly, this also zeroes out things like `is_loop_cb` and `is_called`. Now, I need to understand the implications of `bpf_timed_may_goto`. This is clearly related to bounded looping in BPF. The "timed" aspect is intriguing, and I can see how it works: each iteration of a timed `may_goto` loop will decrement a counter, which can go up to 65535 iterations before triggering an explicit check. After a specific number of iterations, the check happens in `arch_bpf_timed_may_goto`. This then means, at that point, the time budget is checked. They are using `ktime_get_mono_fast_ns()` to get the current time. My next step is to carefully examine `bpf_check_timed_may_goto` in `kernel/bpf/core.c` to fully grasp the specifics of how the time budget is enforced and what the implications are for loop unrolling and resource constraints. I want to see how these checks are implemented and how they interact with the overall verification process. I need to be sure that the time checks do not introduce any new vulnerabilities in the code. |
| 34/3 | 2026/11/01 12:42 | tool | grepper |
0mArgs:{
"Expression": "bpf_check_timed_may_goto",
"PathPrefixes": [
"kernel/bpf/"
]
}Results: {
"Output": "kernel/bpf/core.c=3613=static noinline void bpf_prog_report_may_goto_violation(void)\n--\nkernel/bpf/core.c-3628-\nkernel/bpf/core.c:3629:u64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *p)\nkernel/bpf/core.c-3630-{\n"
} |
| 35/3 | 2026/11/01 12:42 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 43656 output: 37 thoughts: 27 Thoughts: Okay, let's dive into this. Here's my take on those thoughts: **Dissecting `bpf_check_timed_may_goto`** Alright, I'm thinking we need to refresh our memory on that `bpf_check_timed_may_goto` function. Let's head over to `kernel/bpf/core.c` and take a closer look. I'm especially interested in how this function handles those conditional jumps in BPF programs, and how it deals with potential timing issues or constraints. I'm mentally parsing the name - the "timed" part is likely crucial, so I need to pay extra attention to what that entails within the context of BPF verification. It's probably related to ensuring programs don't run for too long, or at least that their execution can be bounded somehow. Knowing how it interacts with the broader verification system is key. Let's see what's actually going on in there. I'll be looking for the crucial logic that handles these "goto" statements within timing constraints. |
| 36/3 | 2026/11/01 12:42 | tool | read-file |
0mArgs:{
"File": "kernel/bpf/core.c",
"FirstLine": 3625,
"LineCount": 25
}Results: {
"Contents": "3625:\t\t}));\n3626:\t#endif\n3627:\t}\n3628:\t\n3629:\tu64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *p)\n3630:\t{\n3631:\t\tu64 time = ktime_get_mono_fast_ns();\n3632:\t\n3633:\t\t/* Populate the timestamp for this stack frame, and refresh count. */\n3634:\t\tif (!p-\u003etimestamp) {\n3635:\t\t\tp-\u003etimestamp = time;\n3636:\t\t\treturn BPF_MAX_TIMED_LOOPS;\n3637:\t\t}\n3638:\t\t/* Check if we've exhausted our time slice, and zero count. */\n3639:\t\tif (unlikely(time - p-\u003etimestamp \u003e= (NSEC_PER_SEC / 4))) {\n3640:\t\t\tbpf_prog_report_may_goto_violation();\n3641:\t\t\treturn 0;\n3642:\t\t}\n3643:\t\t/* Refresh the count for the stack frame. */\n3644:\t\treturn BPF_MAX_TIMED_LOOPS;\n3645:\t}\n3646:\t\n3647:\t/*\n3648:\t * The main prog sets count and timestamp in its first insns. A call of an async callback is an\n3649:\t * invocation of its own too, but it can interrupt an invocation of the prog on this cpu, or go on\n"
} |
| 37/3 | 2026/11/01 12:42 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 44347 output: 444 thoughts: 2201 Thoughts: **KMSAN Assessment: No Need Here** Okay, let's break this down. My job here is to determine if this patch necessitates KMSAN. From what I see, `bpf_check_timed_may_goto` is accessing and writing to `p->timestamp`. Noted. Now, let's go down the checklist. First, does this patch expose any kernel structures to user space with potential for uninitialized data? No. The `bpf_timed_may_goto` structure is entirely internal, percpu, and not shared with user space at all. Clear. Second, does this involve any conditional logic that might depend on uninitialized variables or struct fields? We need to look at stack variables here. The `struct bpf_timed_may_goto saved` is a local stack variable used in a few async callback functions. Before anything happens, there is a call to `bpf_timed_may_goto_save`. If the percpu data `prog->aux->timed_may_goto` is NULL, the `bpf_timed_may_goto_save` function returns early without writing to `saved`. Then the callback runs, then a call to `bpf_timed_may_goto_restore`. Within `bpf_timed_may_goto_restore`, if `prog->aux->timed_may_goto` is NULL, the function does an early return and so `saved` is never read uninitialized. If not NULL, `bpf_timed_may_goto_save` executes and fully initializes `saved`. In `bpf_timed_may_goto_restore`, `saved` is initialized. So, no read of uninitialized variables here. Third, are we allocating data structures where uninitialized fields could be read? Well, the patch uses `alloc_percpu_gfp()` for `struct bpf_timed_may_goto`. This means the percpu memory is zero-initialized by the kernel allocator. Furthermore, the BPF prologue explicitly sets `count`, `timestamp`, and `owner` to known values. No. Fourth, any bounds checks or logic that could lead to reading uninitialized bytes? No. Ah, but wait, what about the JIT changes? The changes are architectural adaptations related to how the per-CPU address of the structure is passed to `bpf_check_timed_may_goto`. All of these trampolines are adaptations of the calling convention from BPF_REG_AX holding a stack offset to it holding an absolute per-CPU address. Then, could a BPF program potentially access registers or stack that are uninitialized? The patch modifies `may_goto_expand_timed` which uses register allocation. The logic is that if there is a dead register, it's used as a scratch. If not, R0 is spilled to stack. And all registers used are either dead or saved/restored. Finally, the instructions that are emitted are: `BPF_LD_IMM64`, `BPF_MOV64_PERCPU_REG`, `BPF_LDX_MEM`. Therefore, nothing touches uninitialized memory. So, let's re-evaluate the criteria based on the information gathered. We are looking for issues that are _only_ detected by KMSAN. Since `alloc_percpu_gfp()` zeros the per-CPU memory and the structures allocated on the stack in async callbacks are either not read or fully initialized before use, there is no uninitialized memory risk exposed in the patch. The patch does not introduce any risks of uninitialized memory usage or information leaks. So, any bugs this patch *could* introduce like register corruption, JIT translation errors, memory leaks or lifetime issues of `prog->aux->timed_may_goto` will be in the realm of KASAN or standard assertions, not uninitialized memory issues that only KMSAN detects. Therefore, **NeedsKMSAN = false**. |
| 38/3 | 2026/11/01 12:42 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series refactors the BPF timed `may_goto` mechanism. Instead of allocating loop counters and timestamps on the BPF program's stack frame per invocation/subprogram, it uses per-CPU memory (`struct bpf_timed_may_goto __percpu *timed_may_goto`) allocated per program via `alloc_percpu_gfp()`. It updates the verifier fixups (`fixups.c`), the architecture JIT trampolines (arm64, loongarch, powerpc, riscv, s390, x86), and adds save/restore logic around asynchronous callbacks (`bpf_timer_cb`, `bpf_wq_work`, `bpf_task_work_callback`, `bpf_rcu_run_callback`).\n\nFrom a memory initialization perspective:\n1. The per-CPU `struct bpf_timed_may_goto` is zero-allocated by `alloc_percpu_gfp()` and explicitly initialized in the program prologue (`count`, `timestamp`, `owner`).\n2. The local stack variable `struct bpf_timed_may_goto saved` used in async callback helpers is guarded by `if (!prog-\u003eaux-\u003etimed_may_goto) return;`. When active, `*saved = *cur;` completely copies the 24-byte struct before any subsequent read in `bpf_timed_may_goto_restore()`. If `timed_may_goto` is NULL, neither function accesses `saved`.\n3. No kernel memory or structure padding is copied to user space or exposed via infoleaks.\n4. Any potential bugs in this patch series (such as register clobbering, incorrect JIT assembly generation, invalid pointer offsets, or use-after-free upon program teardown) are detectable by KASAN, assertions, or standard runtime checks.\n\nTherefore, this change does not introduce uninitialized memory risks that would require a dedicated KMSAN fuzzing session.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series refactors the BPF timed `may_goto` mechanism. Instead of allocating loop counters and timestamps on the BPF program's stack frame per invocation/subprogram, it uses per-CPU memory (`struct bpf_timed_may_goto __percpu *timed_may_goto`) allocated per program via `alloc_percpu_gfp()`. It updates the verifier fixups (`fixups.c`), the architecture JIT trampolines (arm64, loongarch, powerpc, riscv, s390, x86), and adds save/restore logic around asynchronous callbacks (`bpf_timer_cb`, `bpf_wq_work`, `bpf_task_work_callback`, `bpf_rcu_run_callback`).\n\nFrom a memory initialization perspective:\n1. The per-CPU `struct bpf_timed_may_goto` is zero-allocated by `alloc_percpu_gfp()` and explicitly initialized in the program prologue (`count`, `timestamp`, `owner`).\n2. The local stack variable `struct bpf_timed_may_goto saved` used in async callback helpers is guarded by `if (!prog-\u003eaux-\u003etimed_may_goto) return;`. When active, `*saved = *cur;` completely copies the 24-byte struct before any subsequent read in `bpf_timed_may_goto_restore()`. If `timed_may_goto` is NULL, neither function accesses `saved`.\n3. No kernel memory or structure padding is copied to user space or exposed via infoleaks.\n4. Any potential bugs in this patch series (such as register clobbering, incorrect JIT assembly generation, invalid pointer offsets, or use-after-free upon program teardown) are detectable by KASAN, assertions, or standard runtime checks.\n\nTherefore, this change does not introduce uninitialized memory risks that would require a dedicated KMSAN fuzzing session.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|