Skip to contentSkip to conversation

Fix timed may_goto with private stacks

PATCH In review Siddharth Chintamaneni 4 participants 21 messages Sep 4 – Sep 21

Subsystems (14)
x86 PowerPC Kselftest BPF +10 more subsystems
lore
Share & tools
lore
#0 lore raw

cover Fix timed may_goto with private stacks

Jeremy reported a bug[1] while executing a BPF program containing timed_may_goto instructions with private stacks.

timed_may_goto[2] is a runtime safety mechanism that allows BPF programs to execute longer loops[3]. The BPF verifier replaces each may_goto instruction with a loop counter initialized to 0xffff and a timestamp check[4] that terminates the loop after 250 ms.

Private stacks[5] allow BPF programs to use per-CPU memory instead of consuming more of the native kernel stack when BPF programs are deeply nested.

To make timed may_goto work, the BPF program reserves 16 bytes of stack space. The first 8 bytes store the loop counter and the next 8 bytes store the timestamp.

After the loop counter is exhausted, arch_bpf_timed_may_goto() is called. On x86, it adds the counter's stack offset to RBP to obtain a pointer to the counter and timestamp[6]. This works when the BPF program uses the normal stack because RBP is also the BPF frame pointer.

When a private stack is used, the x86 JIT uses R9 as the BPF frame pointer. The verifier-generated loads and stores therefore access the counter and timestamp through R9. However, arch_bpf_timed_may_goto() still adds the offset to RBP and accesses an unrelated location in the native JIT stack frame. This is the mismatch Jeremy reported.

Fix the mismatch by resolving the address in the generated BPF instructions:

BPF_REG_AX = BPF_REG_FP
BPF_REG_AX += stack_offset

The JIT can then select the correct BPF frame pointer before calling arch_bpf_timed_may_goto(). The function receives the resolved pointer instead of reconstructing it from RBP.

The LoongArch timed may_goto implementation is currently queued through the loongarch-next tree[7], while its selftests were merged separately through the bpf-next tree[8]. This series is based on bpf-next and therefore does not include the LoongArch trampoline update.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://elixir.bootlin.com/linux/v7.2.2/source/tools/testing/selftests/bpf/libarena/include/bpf_may_goto.h#L8
[4] https://elixir.bootlin.com/linux/v7.2.2/source/kernel/bpf/core.c#L3407
[5] https://lore.kernel.org/bpf/[email protected]/
[6] https://elixir.bootlin.com/linux/v7.2.2/source/arch/x86/net/bpf_timed_may_goto.S#L18
[7] https://lore.kernel.org/loongarch/[email protected]/
[8] https://lore.kernel.org/bpf/[email protected]/

Siddharth Chintamaneni (7):
bpf: Fix timed may_goto stack pointer for private stacks bpf, x86: Use resolved pointer for timed may_goto bpf, arm64: Use resolved pointer for timed may_goto bpf, powerpc64: Use resolved pointer for timed may_goto bpf, riscv: Use resolved pointer for timed may_goto bpf, s390: Use resolved pointer for timed may_goto selftests/bpf: Test timed may_goto with private stacks

 arch/arm64/net/bpf_timed_may_goto.S           | 12 ++------
 arch/powerpc/net/bpf_timed_may_goto.S         |  8 ++---
 arch/riscv/net/bpf_timed_may_goto.S           | 13 ++++----
 arch/s390/net/bpf_jit_comp.c                  |  6 ++--
 arch/s390/net/bpf_timed_may_goto.S            |  8 ++---
 arch/x86/net/bpf_timed_may_goto.S             |  6 ----
 kernel/bpf/fixups.c                           | 19 ++++++------
 .../bpf/progs/verifier_bpf_fastcall.c         | 30 ++++++++++---------
 .../selftests/bpf/progs/verifier_may_goto_1.c | 17 ++++++-----
 .../bpf/progs/verifier_private_stack.c        | 19 ++++++++++++
 10 files changed, 75 insertions(+), 63 deletions(-)

base-commit: d761934c9483ecde93fe99d8705282f716dfee50

Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/7 bpf: Fix timed may_goto stack pointer for private stacks

timed may_goto passes a stack offset to the architecture trampoline, which reconstructs the counter pointer from its BPF frame pointer. This breaks when the JIT uses a private stack with a different frame pointer.

Resolve the counter pointer in the fixup using BPF_REG_FP and pass the pointer through BPF_REG_AX. Account for the extra instruction in the internal branch offsets.

Fixes e723608bf428 ("bpf: Add verifier support for timed may_goto")
---
 kernel/bpf/fixups.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
kernel/bpf/fixups.c +9 -10
index 65b441e4a351..dc59501a32bb 100644
@@ -1797,20 +1797,20 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
 			stack_depth_extra = 16;
 			insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_AX, BPF_REG_10, stack_off_cnt);
 			if (insn->off >= 0)
-				insn_buf[1] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_AX, 0, insn->off + 5);
+				insn_buf[1] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_AX, 0, insn->off + 6);
 			else
 				insn_buf[1] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_AX, 0, insn->off - 1);
 			insn_buf[2] = BPF_ALU64_IMM(BPF_SUB, BPF_REG_AX, 1);
-			insn_buf[3] = BPF_JMP_IMM(BPF_JNE, BPF_REG_AX, 0, 2);
+			insn_buf[3] = BPF_JMP_IMM(BPF_JNE, BPF_REG_AX, 0, 3);
 			/*
-			 * 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.
+			 * AX is used to pass FP + stack_off_cnt as the argument to
+			 * arch_bpf_timed_may_goto(), and also holds its return value.
 			 */
-			insn_buf[4] = BPF_MOV64_IMM(BPF_REG_AX, stack_off_cnt);
-			insn_buf[5] = BPF_EMIT_CALL(arch_bpf_timed_may_goto);
-			insn_buf[6] = BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_AX, stack_off_cnt);
-			cnt = 7;
+			insn_buf[4] = BPF_MOV64_REG(BPF_REG_AX, BPF_REG_FP);
+			insn_buf[5] = BPF_ALU64_IMM(BPF_ADD, BPF_REG_AX, stack_off_cnt);
+			insn_buf[6] = BPF_EMIT_CALL(arch_bpf_timed_may_goto);
+			insn_buf[7] = BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_AX, stack_off_cnt);
+			cnt = 8;
 
 			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
 			if (!new_prog)
@@ -2661,4 +2661,3 @@ int bpf_remove_fastcall_spills_fills(struct bpf_verifier_env *env)
 
 	return 0;
 }
-
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH bpf-next v1 1/7] bpf: Fix timed may_goto stack pointer for private stacks

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines

timed may_goto passes a stack offset to the architecture trampoline, which reconstructs the counter pointer from its BPF frame pointer. This breaks when the JIT uses a private stack with a different frame pointer.

Quoted 8 lines

Resolve the counter pointer in the fixup using BPF_REG_FP and pass the pointer through BPF_REG_AX. Account for the extra instruction in the internal branch offsets.

Fixes e723608bf428 ("bpf: Add verifier support for timed may_goto")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH 2/7 bpf, x86: Use resolved pointer for timed may_goto

In reply to Siddharth Chintamaneni

The timed may_goto fixup now passes the resolved counter pointer through BPF_REG_AX instead of a stack offset.

Use the pointer directly rather than adding it to RBP. This preserves the private-stack address selected by the JIT through R9.

Fixes 2fb761823ead ("bpf, x86: Add x86 JIT support for timed may_goto")
---
 arch/x86/net/bpf_timed_may_goto.S | 6 ------
 1 file changed, 6 deletions(-)
arch/x86/net/bpf_timed_may_goto.S +0 -6
index 54c690cae190..6e9d9e7c2b1d 100644
@@ -11,12 +11,6 @@
 SYM_FUNC_START(arch_bpf_timed_may_goto)
 	ANNOTATE_NOENDBR
 
-	/*
-	 * r10 passes us stack depth, load the pointer to count and timestamp
-	 * into r10 by adding it to BPF frame pointer.
-	 */
-	leaq (%rbp, %r10, 1), %r10
-
 	/* Setup frame. */
 	pushq %rbp
 	movq %rsp, %rbp
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH bpf-next v1 2/7] bpf, x86: Use resolved pointer for timed may_goto

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

The timed may_goto fixup now passes the resolved counter pointer through BPF_REG_AX instead of a stack offset.

Use the pointer directly rather than adding it to RBP. This preserves the private-stack address selected by the JIT through R9.

Fixes 2fb761823ead ("bpf, x86: Add x86 JIT support for timed may_goto")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw

Re: [PATCH bpf-next v1 2/7] bpf, x86: Use resolved pointer for timed may_goto

In reply to Siddharth Chintamaneni
A subsystem pattern flags this as potentially concerning: This commit changes the x86 trampoline to the new 'pointer in BPF_REG_AX' ABI, but the producer side of that ABI was changed one commit…
Quoted 5 lines
arch/x86/net/bpf_timed_may_goto.S +0 -0
index 54c690cae190..6e9d9e7c2b1d 100644

arch/x86/net/bpf_timed_may_goto.S line 19

Quoted 9 lines
arch/x86/net/bpf_timed_may_goto.S +0 -6
@@ -11,12 +11,6 @@
 SYM_FUNC_START(arch_bpf_timed_may_goto)
 	ANNOTATE_NOENDBR
 
-	/*
-	 * r10 passes us stack depth, load the pointer to count and timestamp
-	 * into r10 by adding it to BPF frame pointer.
-	 */
-	leaq (%rbp, %r10, 1), %r10
-

A subsystem pattern flags this as potentially concerning: This commit changes the x86 trampoline to the new 'pointer in BPF_REG_AX' ABI, but the producer side of that ABI was changed one commit earlier, in d8319a04dafc ("bpf: Fix timed may_goto stack pointer for private stacks"), which rewrote kernel/bpf/fixups.c to emit:

    insn_buf[4] = BPF_MOV64_REG(BPF_REG_AX, BPF_REG_FP);
    insn_buf[5] = BPF_ALU64_IMM(BPF_ADD, BPF_REG_AX, stack_off_cnt);

At commit d8319a04dafc (i.e. HEAD~1), does x86 still execute 'leaq (%rbp, %r10, 1), %r10' on a register that already holds a resolved pointer, producing r10 = rbp + (frame_ptr + stack_off_cnt) -- roughly 2*rbp, a non-canonical address?

bpf_check_timed_may_goto() then reads and writes p->timestamp / p->count through it (kernel/bpf/core.c:3410). Would any BPF program containing may_goto oops on x86_64 at that commit?

The same window exists for arm64, riscv, powerpc64 and s390, whose trampolines are only converted in the four later commits d35642770d98, 7053db7d3c0d, 6e03227c5677 and 20356163d51e. Is the tree bisectable across the series?

The two halves carry different Fixes: tags (this one 2fb761823ead, the fixups.c one e723608bf428), which invites a partial stable backport that reintroduces exactly this wild-pointer write. Would squashing the fixups.c ABI change with the arch trampoline updates, or ordering all arch updates before the generic change, address the bisectability concern?

arch/x86/net/bpf_timed_may_goto.S line 16

Quoted 3 lines
arch/x86/net/bpf_timed_may_goto.S +0 -0
 	/* Setup frame. */
 	pushq %rbp
 	movq %rsp, %rbp

The x86-only __xlated expectations for the timed may_goto expansion in tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c were never updated for the new 7-insn -> 8-insn sequence. The test expects:

    __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")

Does this test fail on x86_64 for the whole series? Every line from index 7 onward is now wrong: the first branch becomes 'goto pc+7' (insn->off + 6 instead of insn->off + 5), the second becomes 'goto pc+3' instead of 'pc+2', 'r12 = -24' is replaced by the pair 'r12 = r10' / 'r12 += -24', and the trailing indices shift by one ('call unknown' at 12, the store at 13, then 14 and 15).

git log 28bd77eb835b..8dc5c7d5340d shows the only selftest commit in the series is 8dc5c7d5340d ("selftests/bpf: Test timed may_goto with private stacks"), which adds new coverage but does not touch verifier_bpf_fastcall.c. Note the original x86 JIT enablement commit 2fb761823ead -- the commit this patch lists in its Fixes: tag -- adjusted this very test for the same reason, so the update belongs with the ABI change.

Quoted 5 lines

commit 28bd77eb835bc1a95190315e93afbc6bb186b9ff
Author: Siddharth Chintamaneni <[email protected]>

bpf, x86: Use resolved pointer for timed may_goto

Quoted 7 lines

The timed may_goto fixup now passes the resolved counter pointer through BPF_REG_AX instead of a stack offset.

Use the pointer directly rather than adding it to RBP. This preserves the private-stack address selected by the JIT through R9.

Fixes 2fb761823ead ("bpf, x86: Add x86 JIT support for timed may_goto")
                 ^^^^^^^^^^^

The existing Fixes: tag points to commit 2fb761823ead, which is not an ancestor of this fix and is on a different branch (bpf-next/res_spin_lock).

The bug was actually introduced by commit 47ab38f3f4d9 in this branch's history, which added the incorrect code 'leaq (%rbp, %r10, 1), %r10' that overwrites r10 instead of preserving it.

Should this use:

Fixes 47ab38f3f4d9 ("adding ci files")

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/33914557238

#6 lore raw

Re: [PATCH bpf-next v1 2/7] bpf, x86: Use resolved pointer for timed may_goto

Quoted 39 lines · On Fri, 4 Sept 2026 at 13:33, <[email protected]> wrote:
Quoted 6 lines
arch/x86/net/bpf_timed_may_goto.S +0 -0
index 54c690cae190..6e9d9e7c2b1d 100644

arch/x86/net/bpf_timed_may_goto.S line 19

Quoted 9 lines
arch/x86/net/bpf_timed_may_goto.S +0 -6
@@ -11,12 +11,6 @@
 SYM_FUNC_START(arch_bpf_timed_may_goto)
      ANNOTATE_NOENDBR
 
-     /*
-      * r10 passes us stack depth, load the pointer to count and timestamp
-      * into r10 by adding it to BPF frame pointer.
-      */
-     leaq (%rbp, %r10, 1), %r10
-

A subsystem pattern flags this as potentially concerning: This commit changes the x86 trampoline to the new 'pointer in BPF_REG_AX' ABI, but the producer side of that ABI was changed one commit earlier, in d8319a04dafc ("bpf: Fix timed may_goto stack pointer for private stacks"), which rewrote kernel/bpf/fixups.c to emit:

    insn_buf[4] = BPF_MOV64_REG(BPF_REG_AX, BPF_REG_FP);
    insn_buf[5] = BPF_ALU64_IMM(BPF_ADD, BPF_REG_AX, stack_off_cnt);

At commit d8319a04dafc (i.e. HEAD~1), does x86 still execute 'leaq (%rbp, %r10, 1), %r10' on a register that already holds a resolved pointer, producing r10 = rbp + (frame_ptr + stack_off_cnt) -- roughly 2*rbp, a non-canonical address?

bpf_check_timed_may_goto() then reads and writes p->timestamp / p->count through it (kernel/bpf/core.c:3410). Would any BPF program containing may_goto oops on x86_64 at that commit?

The same window exists for arm64, riscv, powerpc64 and s390, whose trampolines are only converted in the four later commits d35642770d98, 7053db7d3c0d, 6e03227c5677 and 20356163d51e. Is the tree bisectable across the series?

Quoted 10 lines

The two halves carry different Fixes: tags (this one 2fb761823ead, the fixups.c one e723608bf428), which invites a partial stable backport that reintroduces exactly this wild-pointer write. Would squashing the fixups.c ABI change with the arch trampoline updates, or ordering all arch updates before the generic change, address the bisectability concern?

Quoted 3 lines
arch/x86/net/bpf_timed_may_goto.S +0 -0
      /* Setup frame. */
      pushq %rbp
      movq %rsp, %rbp

I tried to follow the same pattern used when the patches were introduced, but this is a valid concern.
Should I just squash all the patches to one then?

Quoted 11 lines

The x86-only __xlated expectations for the timed may_goto expansion in tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c were never updated for the new 7-insn -> 8-insn sequence. The test expects:

    __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")

I've fixed this in the selftests patch.

Quoted 23 lines

Does this test fail on x86_64 for the whole series? Every line from index 7 onward is now wrong: the first branch becomes 'goto pc+7' (insn->off + 6 instead of insn->off + 5), the second becomes 'goto pc+3' instead of 'pc+2', 'r12 = -24' is replaced by the pair 'r12 = r10' / 'r12 += -24', and the trailing indices shift by one ('call unknown' at 12, the store at 13, then 14 and 15).

git log 28bd77eb835b..8dc5c7d5340d shows the only selftest commit in the series is 8dc5c7d5340d ("selftests/bpf: Test timed may_goto with private stacks"), which adds new coverage but does not touch verifier_bpf_fastcall.c. Note the original x86 JIT enablement commit 2fb761823ead -- the commit this patch lists in its Fixes: tag -- adjusted this very test for the same reason, so the update belongs with the ABI change.

Quoted 8 lines

commit 28bd77eb835bc1a95190315e93afbc6bb186b9ff
Author: Siddharth Chintamaneni <[email protected]>

bpf, x86: Use resolved pointer for timed may_goto

The timed may_goto fixup now passes the resolved counter pointer through BPF_REG_AX instead of a stack offset.

Quoted 9 lines
Quoted 4 lines

Use the pointer directly rather than adding it to RBP. This preserves the private-stack address selected by the JIT through R9.

Fixes 2fb761823ead ("bpf, x86: Add x86 JIT support for timed may_goto")
                 ^^^^^^^^^^^

The existing Fixes: tag points to commit 2fb761823ead, which is not an ancestor of this fix and is on a different branch (bpf-next/res_spin_lock).

I'll fix this in the next iteration

Quoted 14 lines

The bug was actually introduced by commit 47ab38f3f4d9 in this branch's history, which added the incorrect code 'leaq (%rbp, %r10, 1), %r10' that overwrites r10 instead of preserving it.

Should this use:

Fixes 47ab38f3f4d9 ("adding ci files")

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/33914557238

#7 lore raw

PATCH 3/7 bpf, arm64: Use resolved pointer for timed may_goto

In reply to Siddharth Chintamaneni

The timed may_goto fixup now passes the resolved counter pointer through BPF_REG_AX instead of a stack offset.

Copy the pointer directly into the first argument register rather than adding it to the BPF frame pointer again.

Fixes 16175375da36 ("bpf, arm64: Add JIT support for timed may_goto")
Assisted-by Copilot:gpt-5.6-sol
---
 arch/arm64/net/bpf_timed_may_goto.S | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
arch/arm64/net/bpf_timed_may_goto.S +3 -9
index a9a802711a7f..53148c9a1c58 100644
@@ -14,16 +14,10 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)
 	stp	x3, x4, [sp, #48]
 
 	/*
-	 * 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.
+	 * BPF_REG_AX (x9) contains the count and timestamp pointer; 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
Signature · 1 line
2.43.0
#8 lore raw

Re: [PATCH bpf-next v1 3/7] bpf, arm64: Use resolved pointer for timed may_goto

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

The timed may_goto fixup now passes the resolved counter pointer through BPF_REG_AX instead of a stack offset.

Copy the pointer directly into the first argument register rather than adding it to the BPF frame pointer again.

Fixes 16175375da36 ("bpf, arm64: Add JIT support for timed may_goto")
Assisted-by Copilot:gpt-5.6-sol

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#9 lore raw

PATCH 4/7 bpf, powerpc64: Use resolved pointer for timed may_goto

In reply to Siddharth Chintamaneni

The timed may_goto fixup now passes the resolved counter pointer through BPF_REG_AX instead of a stack offset.

Copy the pointer directly into the first argument register rather than adding it to the BPF frame pointer again.

Fixes b55b6b9ad76c ("powerpc64/bpf: Add powerpc64 JIT support for timed may_goto")
Assisted-by Copilot:gpt-5.6-sol
---
 arch/powerpc/net/bpf_timed_may_goto.S | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/powerpc/net/bpf_timed_may_goto.S +4 -4
index 84ecf6fa7f5d..d88825dd34fe 100644
@@ -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:  count and timestamp pointer in BPF_REG_AX (r12)
  *	- output: updated count in BPF_REG_AX (r12)
  *
  * Call bpf_check_timed_may_goto(ptr) with normal powerpc64 ABI:
@@ -32,10 +32,10 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)
 	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).
+	 * BPF_REG_AX (r12) contains the count and timestamp pointer; pass it as
+	 * the first argument in r3.
 	 */
-	add     r3, r31, r12
+	mr      r3, r12
 	bl      CFUNC(bpf_check_timed_may_goto)
 
 	/* Put return value back into AX */
Signature · 1 line
2.43.0
#10 lore raw

Re: [PATCH bpf-next v1 4/7] bpf, powerpc64: Use resolved pointer for timed may_goto

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

The timed may_goto fixup now passes the resolved counter pointer through BPF_REG_AX instead of a stack offset.

Copy the pointer directly into the first argument register rather than adding it to the BPF frame pointer again.

Fixes b55b6b9ad76c ("powerpc64/bpf: Add powerpc64 JIT support for timed may_goto")
Assisted-by Copilot:gpt-5.6-sol

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#11 lore raw

PATCH 5/7 bpf, riscv: Use resolved pointer for timed may_goto

In reply to Siddharth Chintamaneni

The timed may_goto fixup now passes the resolved counter pointer through BPF_REG_AX instead of a stack offset.

Copy the pointer directly into the first argument register rather than adding it to the BPF frame pointer again.

Fixes 6ef8ff20c30b ("bpf, riscv: Add support for timed may_goto")
Assisted-by Copilot:gpt-5.6-sol
---
 arch/riscv/net/bpf_timed_may_goto.S | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
arch/riscv/net/bpf_timed_may_goto.S +8 -5
index 02c637d87420..8c03e272e0bf 100644
@@ -6,12 +6,11 @@
 
 /*
  * Trampoline for the BPF timed may_goto loop bound. Custom calling convention:
- *	- input:  stack offset in BPF_REG_AX (t0)
+ *	- input:  count and timestamp pointer 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 (a5, a0-a4) 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 (a5, a0-a4) are saved across the call.
  */
 
 SYM_FUNC_START(arch_bpf_timed_may_goto)
@@ -28,7 +27,11 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)
 	REG_S	a3, 1*SZREG(sp)
 	REG_S	a4, 0*SZREG(sp)
 
-	add	a0, t0, s5
+	/*
+	 * BPF_REG_AX (t0) contains the count and timestamp pointer; pass it as
+	 * the first argument in a0.
+	 */
+	mv	a0, t0
 	call	bpf_check_timed_may_goto
 	mv	t0, a0
 
Signature · 1 line
2.43.0
#12 lore raw

Re: [PATCH bpf-next v1 5/7] bpf, riscv: Use resolved pointer for timed may_goto

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

The timed may_goto fixup now passes the resolved counter pointer through BPF_REG_AX instead of a stack offset.

Copy the pointer directly into the first argument register rather than adding it to the BPF frame pointer again.

Fixes 6ef8ff20c30b ("bpf, riscv: Add support for timed may_goto")
Assisted-by Copilot:gpt-5.6-sol

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#13 lore raw

Re: [PATCH bpf-next v1 5/7] bpf, riscv: Use resolved pointer for timed may_goto

In reply to Siddharth Chintamaneni
Should the Fixes: tag point to d8319a04dafc ("bpf: Fix timed may_goto stack pointer for private stacks") instead? That commit changed the calling convention from passing a stack offset to passing a…
Quoted 9 lines

bpf, riscv: Use resolved pointer for timed may_goto

The timed may_goto fixup now passes the resolved counter pointer through BPF_REG_AX instead of a stack offset.

Copy the pointer directly into the first argument register rather than adding it to the BPF frame pointer again.

Fixes 6ef8ff20c30b ("bpf, riscv: Add support for timed may_goto")

Should the Fixes: tag point to d8319a04dafc ("bpf: Fix timed may_goto stack pointer for private stacks") instead? That commit changed the calling convention from passing a stack offset to passing a resolved pointer through BPF_REG_AX, which is what broke the riscv trampoline.

The original riscv implementation (6ef8ff20c30b) worked correctly with the original calling convention. riscv64 cannot hit the private-stack bug that d8319a04dafc targeted: it does not implement bpf_jit_supports_private_stack(), so the __weak default in kernel/bpf/core.c returns false, and private stacks are gated on it in kernel/bpf/verifier.c. On riscv, regmap[BPF_REG_FP] = RV_REG_S5 is the one and only BPF frame pointer, and the pre-patch 'add a0, t0, s5' computed exactly the right address.

What this patch actually does on riscv is adapt to the new BPF_REG_AX calling convention introduced by d8319a04dafc, which is patch 1 of the same series. If stable/AUTOSEL tooling picks this commit up on the strength of its Fixes: tag without also taking d8319a04dafc, BPF_REG_AX still holds the raw immediate from the old fixup (BPF_MOV64_IMM(BPF_REG_AX, stack_off_cnt), i.e. -stack_depth-16, up to -528):

    mv a0, t0       /* a0 = -528, not a pointer */
    call bpf_check_timed_may_goto

bpf_check_timed_may_goto() then dereferences p->timestamp (kernel/bpf/core.c) at 0xfffffffffffffdf0, oopsing on every timed may_goto loop.

Should the changelog also declare that d8319a04dafc is a prerequisite for this patch?

Because d8319a04dafc lands first and each architecture is converted in a later commit, riscv64 (and arm64, powerpc64, s390) BPF timed may_goto is broken at every intermediate commit of the series. Does the series need to be structured differently to remain bisectable for those architectures?

Quoted 23 lines
arch/riscv/net/bpf_timed_may_goto.S +3 -4
index 02c637d87420..8c03e272e0bf 100644
@@ -6,12 +6,11 @@
 
 /*
  * Trampoline for the BPF timed may_goto loop bound. Custom calling convention:
- *	- input:  stack offset in BPF_REG_AX (t0)
+ *	- input:  count and timestamp pointer 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 (a5, a0-a4) 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 (a5, a0-a4) are saved across the call.
  */
 
 SYM_FUNC_START(arch_bpf_timed_may_goto)
@@ -28,7 +27,11 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)
 	REG_S	a3, 1*SZREG(sp)
 	REG_S	a4, 0*SZREG(sp)

arch/riscv/net/bpf_timed_may_goto.S SYM_FUNC_START(arch_bpf_timed_may_goto) · line 37

Quoted 10 lines
arch/riscv/net/bpf_timed_may_goto.S +5 -1
 
-	add	a0, t0, s5
+	/*
+	 * BPF_REG_AX (t0) contains the count and timestamp pointer; pass it as
+	 * the first argument in a0.
+	 */
+	mv	a0, t0
 	call	bpf_check_timed_may_goto
 	mv	t0, a0
 

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/33914557238

#14 lore raw

PATCH 6/7 bpf, s390: Use resolved pointer for timed may_goto

In reply to Siddharth Chintamaneni

The timed may_goto fixup now passes the resolved counter pointer through BPF_REG_AX instead of a stack offset and BPF frame pointer pair.

Copy the pointer directly into the first argument register and update the special calling convention documentation.

Fixes b8efa810c1db ("s390/bpf: Add s390 JIT support for timed may_goto")
Assisted-by Copilot:gpt-5.6-sol
---
 arch/s390/net/bpf_jit_comp.c       | 6 +++---
 arch/s390/net/bpf_timed_may_goto.S | 8 ++++----
 2 files changed, 7 insertions(+), 7 deletions(-)
arch/s390/net/bpf_jit_comp.c +3 -3
index c46872b071ce..4098c3377c21 100644
@@ -1872,9 +1872,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 and return value are in BPF_REG_AX; all
+			 * GPRs except REG_W0, REG_W1, and BPF_REG_AX are
+			 * callee-saved.
 			 */
 
 			/* brasl %r0,func */
arch/s390/net/bpf_timed_may_goto.S +4 -4
index 06f567a460d7..e34c99982c4a 100644
@@ -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 and return value are
+	 * 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
 
Signature · 1 line
2.43.0
#15 lore raw

Re: [PATCH bpf-next v1 6/7] bpf, s390: Use resolved pointer for timed may_goto

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

The timed may_goto fixup now passes the resolved counter pointer through BPF_REG_AX instead of a stack offset and BPF frame pointer pair.

Copy the pointer directly into the first argument register and update the special calling convention documentation.

Fixes b8efa810c1db ("s390/bpf: Add s390 JIT support for timed may_goto")
Assisted-by Copilot:gpt-5.6-sol

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#16 lore raw

Re: [PATCH bpf-next v1 6/7] bpf, s390: Use resolved pointer for timed may_goto

In reply to Siddharth Chintamaneni
Should the Fixes: tag point at b8efa810c1db?
Quoted 11 lines

commit 20356163d51e7b8551f96780e7e969a26767c5c4

Author:     Siddharth Chintamaneni <[email protected]>
AuthorDate: Fri Sep 4 19:51:31 2026 +0000
Commit:     Kernel Patches Daemon <[email protected]>
CommitDate: Fri Sep 4 13:03:02 2026 -0700

bpf, s390: Use resolved pointer for timed may_goto

The timed may_goto fixup now passes the resolved counter pointer through BPF_REG_AX instead of a stack offset and BPF frame pointer pair.

Quoted 8 lines

Copy the pointer directly into the first argument register and update the special calling convention documentation.

Fixes b8efa810c1db ("s390/bpf: Add s390 JIT support for timed may_goto")
Assisted-by Copilot:gpt-5.6-sol

Should the Fixes: tag point at b8efa810c1db?

At that commit the s390 trampoline was correct for the ABI in force at the time. kernel/bpf/fixups.c emitted insn_buf[4] = BPF_MOV64_IMM(BPF_REG_AX, stack_off_cnt), so %r12 held a signed byte offset and lay %r2,0(%r12,%r13) correctly resolved it against the s390 BPF frame pointer %r13. Nothing was wrong with b8efa810c1db.

What makes the old code wrong is the parent commit in this series, d8319a04dafc ("bpf: Fix timed may_goto stack pointer for private stacks"), which replaced that with:

  insn_buf[4] = BPF_MOV64_REG(BPF_REG_AX, BPF_REG_FP);
  insn_buf[5] = BPF_ALU64_IMM(BPF_ADD, BPF_REG_AX, stack_off_cnt)

so that %r12 now carries an already-resolved pointer. Only after that does lay %r2,0(%r12,%r13) double-add the frame pointer.

If a stable maintainer or AUTOSEL picks 20356163d51e on the strength of its Fixes: tag without also taking d8319a04dafc, s390 is left with lgr %r2,%r12 while the fixup still puts a raw offset (stack_off_cnt = -stack_depth - 16, e.g. -528) in %r12:

bpf_check_timed_may_goto(struct bpf_timed_may_goto *p)

then does p->count / p->timestamp loads and stores through a small negative integer treated as a kernel pointer, which would trigger an oops or memory corruption on every timed may_goto timeout.

The mirror case is equally bad: d8319a04dafc carries `Fixes:
e723608bf428` and will be selected for stable on its own, leaving s390 (and arm64/ppc64/riscv) double-adding the frame pointer.

The generic ABI change and its five arch consumers are not independently correct, so either they belong in one commit, or the arch patches should carry the same Fixes: e723608bf428 ("bpf: Add verifier support for timed may_goto") and a Cc: <[email protected]> # <sha>: <generic commit> prerequisite note. No Cc: stable and no prerequisite note is present today.

Note: the same pattern is used by all four sibling commits (28bd77eb835b x86, d35642770d98 arm64, 6e03227c5677 ppc64, 7053db7d3c0d riscv), so this is a series-wide tagging decision rather than an s390-only slip.

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/33914557238

#17 lore raw

PATCH 7/7 selftests/bpf: Test timed may_goto with private stacks

In reply to Siddharth Chintamaneni

Add private_stack_timed_may_goto() with enough stack usage to select a private stack. Check that the JIT uses the private-stack frame pointer when constructing the pointer passed to the architecture trampoline.

Update the translated instruction expectations in may_goto_batch_2(), may_goto_interaction_x86_64(), and may_goto_interaction() for the additional pointer construction instruction and adjusted branch offsets.

---
 .../bpf/progs/verifier_bpf_fastcall.c         | 30 ++++++++++---------
 .../selftests/bpf/progs/verifier_may_goto_1.c | 17 ++++++-----
 .../bpf/progs/verifier_private_stack.c        | 19 ++++++++++++
 3 files changed, 44 insertions(+), 22 deletions(-)
tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c +16 -14
index 328cf630210a..c5996d9dc8cc 100644
@@ -635,15 +635,16 @@ __xlated("4: r0 = &(void __percpu *)(r0)")
 __xlated("...")
 /* may_goto expansion starts */
 __xlated("6: r12 = *(u64 *)(r10 -24)")
-__xlated("7: if r12 == 0x0 goto pc+6")
+__xlated("7: if r12 == 0x0 goto pc+7")
 __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")
+__xlated("9: if r12 != 0x0 goto pc+3")
+__xlated("10: r12 = r10")
+__xlated("11: r12 += -24")
+__xlated("12: call unknown")
+__xlated("13: *(u64 *)(r10 -24) = r12")
 /* may_goto expansion ends */
-__xlated("13: *(u64 *)(r10 -8) = r1")
-__xlated("14: exit")
+__xlated("14: *(u64 *)(r10 -8) = r1")
+__xlated("15: exit")
 __success
 __naked void may_goto_interaction_x86_64(void)
 {
@@ -676,15 +677,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("5: if r12 == 0x0 goto pc+7")
 __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")
+__xlated("7: if r12 != 0x0 goto pc+3")
+__xlated("8: r12 = r10")
+__xlated("9: r12 += -24")
+__xlated("10: call unknown")
+__xlated("11: *(u64 *)(r10 -24) = r12")
 /* may_goto expansion ends */
-__xlated("11: *(u64 *)(r10 -8) = r1")
-__xlated("12: exit")
+__xlated("12: *(u64 *)(r10 -8) = r1")
+__xlated("13: exit")
 __success
 __naked void may_goto_interaction(void)
 {
tools/testing/selftests/bpf/progs/verifier_may_goto_1.c +9 -8
index db7e30da234f..1721897160dc 100644
@@ -90,15 +90,16 @@ __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("3: if r12 == 0x0 goto pc+7")
 __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")
+__xlated("5: if r12 != 0x0 goto pc+3")
+__xlated("6: r12 = r10")
+__xlated("7: r12 += -16")
+__xlated("8: call unknown")
+__xlated("9: *(u64 *)(r10 -16) = r12")
+__xlated("10: r0 = 1")
+__xlated("11: r0 = 2")
+__xlated("12: exit")
 __success
 __naked void may_goto_batch_2(void)
 {
tools/testing/selftests/bpf/progs/verifier_private_stack.c +19 -0
index 3d9994a52464..639b53363c9a 100644
@@ -70,6 +70,25 @@ __naked void no_private_stack_nested(void)
 "	::: __clobber_all);
 }
 
+SEC("fentry/bpf_fentry_test9")
+__description("Private stack, timed may_goto")
+__success __retval(0)
+__arch_x86_64
+__jited("	movabsq	$0x{{.*}}, %r9")
+__jited("...")
+__jited("	movq	%r9, %r10")
+__arch_arm64
+__jited("	mov	x9, x25")
+int private_stack_timed_may_goto(void)
+{
+	volatile __u64 stack_space[16] = {};
+
+	while (can_loop)
+		;
+
+	return stack_space[0];
+}
+
 __used
 __naked static void cumulative_stack_depth_subprog(void)
 {
Signature · 1 line
2.43.0
#18 lore raw

Re: [PATCH bpf-next v1 7/7] selftests/bpf: Test timed may_goto with private stacks

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

Add private_stack_timed_may_goto() with enough stack usage to select a private stack. Check that the JIT uses the private-stack frame pointer when constructing the pointer passed to the architecture trampoline.

Update the translated instruction expectations in may_goto_batch_2(), may_goto_interaction_x86_64(), and may_goto_interaction() for the additional pointer construction instruction and adjusted branch offsets.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#19 lore raw
In reply to Siddharth Chintamaneni
Quoted 42 lines · Siddharth Chintamaneni

Jeremy reported a bug[1] while executing a BPF program containing timed_may_goto instructions with private stacks.

timed_may_goto[2] is a runtime safety mechanism that allows BPF programs to execute longer loops[3]. The BPF verifier replaces each may_goto instruction with a loop counter initialized to 0xffff and a timestamp check[4] that terminates the loop after 250 ms.

Private stacks[5] allow BPF programs to use per-CPU memory instead of consuming more of the native kernel stack when BPF programs are deeply nested.

To make timed may_goto work, the BPF program reserves 16 bytes of stack space. The first 8 bytes store the loop counter and the next 8 bytes store the timestamp.

After the loop counter is exhausted, arch_bpf_timed_may_goto() is called. On x86, it adds the counter's stack offset to RBP to obtain a pointer to the counter and timestamp[6]. This works when the BPF program uses the normal stack because RBP is also the BPF frame pointer.

When a private stack is used, the x86 JIT uses R9 as the BPF frame pointer. The verifier-generated loads and stores therefore access the counter and timestamp through R9. However, arch_bpf_timed_may_goto() still adds the offset to RBP and accesses an unrelated location in the native JIT stack frame. This is the mismatch Jeremy reported.

Fix the mismatch by resolving the address in the generated BPF instructions:

BPF_REG_AX = BPF_REG_FP
BPF_REG_AX += stack_offset

The JIT can then select the correct BPF frame pointer before calling arch_bpf_timed_may_goto(). The function receives the resolved pointer instead of reconstructing it from RBP.

The LoongArch timed may_goto implementation is currently queued through the loongarch-next tree[7], while its selftests were merged separately through the bpf-next tree[8]. This series is based on bpf-next and therefore does not include the LoongArch trampoline update.

Quoted 9 lines

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://elixir.bootlin.com/linux/v7.2.2/source/tools/testing/selftests/bpf/libarena/include/bpf_may_goto.h#L8
[4] https://elixir.bootlin.com/linux/v7.2.2/source/kernel/bpf/core.c#L3407
[5] https://lore.kernel.org/bpf/[email protected]/
[6] https://elixir.bootlin.com/linux/v7.2.2/source/arch/x86/net/bpf_timed_may_goto.S#L18
[7] https://lore.kernel.org/loongarch/[email protected]/
[8] https://lore.kernel.org/bpf/[email protected]/

I think we can keep this fix local to x86. Would be less churn in the end.

The problem is that x86 switches the BPF frame pointer from RBP to R9 for private stacks, but the trampoline still uses RBP. arm64 and powerpc64 keep using X25 and R31 respectively for both stack modes, so their trampolines already use the right frame pointer.

Instead of changing the generic fixup, can we resolve the pointer in the x86 JIT when emitting the call to arch_bpf_timed_may_goto()? We already know whether priv_frame_ptr is set there. R10 contains the offset passed through BPF_REG_AX, so we can emit:

/* Private stack */
leaq (%r9, %r10), %r10

/* Normal stack */
leaq (%rbp, %r10), %r10

Then remove the LEA from the x86 trampoline, as patch 2 already does.

This gives bpf_check_timed_may_goto() the same address used by the generated loads and stores: the BPF frame pointer plus the counter’s stack offset. For normal stacks, we just move the existing calculation into the JIT. For private stacks, we use R9 instead of RBP, which fixes the mismatch.

The existing save/restore of R9 around the call should stay. We also leave RBP alone, since it is still needed for the native frame chain and unwinding.

I haven’t tested this approach, so it still needs checking with both normal and private stacks.

pw-bot: cr

Quoted 23 lines

Siddharth Chintamaneni (7):
bpf: Fix timed may_goto stack pointer for private stacks bpf, x86: Use resolved pointer for timed may_goto bpf, arm64: Use resolved pointer for timed may_goto bpf, powerpc64: Use resolved pointer for timed may_goto bpf, riscv: Use resolved pointer for timed may_goto bpf, s390: Use resolved pointer for timed may_goto selftests/bpf: Test timed may_goto with private stacks

 arch/arm64/net/bpf_timed_may_goto.S           | 12 ++------
 arch/powerpc/net/bpf_timed_may_goto.S         |  8 ++---
 arch/riscv/net/bpf_timed_may_goto.S           | 13 ++++----
 arch/s390/net/bpf_jit_comp.c                  |  6 ++--
 arch/s390/net/bpf_timed_may_goto.S            |  8 ++---
 arch/x86/net/bpf_timed_may_goto.S             |  6 ----
 kernel/bpf/fixups.c                           | 19 ++++++------
 .../bpf/progs/verifier_bpf_fastcall.c         | 30 ++++++++++---------
 .../selftests/bpf/progs/verifier_may_goto_1.c | 17 ++++++-----
 .../bpf/progs/verifier_private_stack.c        | 19 ++++++++++++
 10 files changed, 75 insertions(+), 63 deletions(-)

base-commit: d761934c9483ecde93fe99d8705282f716dfee50

#20 lore raw

<SNIP>

Quoted 12 lines

I think we can keep this fix local to x86. Would be less churn in the end.

The problem is that x86 switches the BPF frame pointer from RBP to R9 for private stacks, but the trampoline still uses RBP. arm64 and powerpc64 keep using X25 and R31 respectively for both stack modes, so their trampolines already use the right frame pointer.

Instead of changing the generic fixup, can we resolve the pointer in the x86 JIT when emitting the call to arch_bpf_timed_may_goto()? We already know whether priv_frame_ptr is set there. R10 contains the offset passed through BPF_REG_AX, so we can emit:

Quoted 8 lines

/* Private stack */
leaq (%r9, %r10), %r10

/* Normal stack */
leaq (%rbp, %r10), %r10

Then remove the LEA from the x86 trampoline, as patch 2 already does.

make sense! I thought that the remaining arch's have a similar private stacks approach to x86. I'll give it a try and re-spin the patch.

Quoted 37 lines

This gives bpf_check_timed_may_goto() the same address used by the generated loads and stores: the BPF frame pointer plus the counter’s stack offset. For normal stacks, we just move the existing calculation into the JIT. For private stacks, we use R9 instead of RBP, which fixes the mismatch.

The existing save/restore of R9 around the call should stay. We also leave RBP alone, since it is still needed for the native frame chain and unwinding.

I haven’t tested this approach, so it still needs checking with both normal and private stacks.

pw-bot: cr

Quoted 23 lines

Siddharth Chintamaneni (7):
bpf: Fix timed may_goto stack pointer for private stacks bpf, x86: Use resolved pointer for timed may_goto bpf, arm64: Use resolved pointer for timed may_goto bpf, powerpc64: Use resolved pointer for timed may_goto bpf, riscv: Use resolved pointer for timed may_goto bpf, s390: Use resolved pointer for timed may_goto selftests/bpf: Test timed may_goto with private stacks

 arch/arm64/net/bpf_timed_may_goto.S           | 12 ++------
 arch/powerpc/net/bpf_timed_may_goto.S         |  8 ++---
 arch/riscv/net/bpf_timed_may_goto.S           | 13 ++++----
 arch/s390/net/bpf_jit_comp.c                  |  6 ++--
 arch/s390/net/bpf_timed_may_goto.S            |  8 ++---
 arch/x86/net/bpf_timed_may_goto.S             |  6 ----
 kernel/bpf/fixups.c                           | 19 ++++++------
 .../bpf/progs/verifier_bpf_fastcall.c         | 30 ++++++++++---------
 .../selftests/bpf/progs/verifier_may_goto_1.c | 17 ++++++-----
 .../bpf/progs/verifier_private_stack.c        | 19 ++++++++++++
 10 files changed, 75 insertions(+), 63 deletions(-)

base-commit: d761934c9483ecde93fe99d8705282f716dfee50