Linux-RISC-V Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>
To: "Siddharth Chintamaneni" <sidchintamaneni@gmail.com>,
	<bpf@vger.kernel.org>
Cc: "Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"John Fastabend" <john.fastabend@gmail.com>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Eduard Zingerman" <eddyz87@gmail.com>,
	"Martin KaFai Lau" <martin.lau@linux.dev>,
	"Song Liu" <song@kernel.org>,
	"Yonghong Song" <yonghong.song@linux.dev>,
	"Jiri Olsa" <jolsa@kernel.org>,
	"Emil Tsalapatis" <emil@etsalapatis.com>,
	"Ihor Solodrai" <ihor.solodrai@linux.dev>,
	"Anton Protopopov" <a.s.protopopov@gmail.com>,
	"Puranjay Mohan" <puranjay@kernel.org>,
	<linuxppc-dev@lists.ozlabs.org>, <linux-s390@vger.kernel.org>,
	<linux-riscv@lists.infradead.org>, <rlmenge@gmail.com>,
	<hargar@linux.microsoft.com>, <apais@microsoft.com>
Subject: Re: [PATCH bpf-next v1 0/7] Fix timed may_goto with private stacks
Date: Mon, 21 Sep 2026 05:48:52 +0200	[thread overview]
Message-ID: <DLKORFKGW8D8.XVSVVIH3Q6Q@gmail.com> (raw)
In-Reply-To: <20260904195132.141068-1-sidchintamaneni@gmail.com>

On Fri Sep 4, 2026 at 9:51 PM CEST, Siddharth Chintamaneni wrote:
> 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/20260824213158.3755932-2-Jeremy.Jean@oss.cyber.gouv.fr/
> [2] https://lore.kernel.org/all/20250304003239.2390751-1-memxor@gmail.com/
> [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/20260417034658.2625353-1-yonghong.song@linux.dev/
> [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/20260804153938.16129-3-dongtai.guo@linux.dev/
> [8] https://lore.kernel.org/bpf/20260813070906.5164-1-yangtiezhu@loongson.cn/
>

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

> 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


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

  parent reply	other threads:[~2026-09-21  3:49 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-04 19:51 [PATCH bpf-next v1 0/7] Fix timed may_goto with private stacks Siddharth Chintamaneni
2026-09-04 19:51 ` [PATCH bpf-next v1 1/7] bpf: Fix timed may_goto stack pointer for " Siddharth Chintamaneni
2026-09-04 19:51 ` [PATCH bpf-next v1 2/7] bpf, x86: Use resolved pointer for timed may_goto Siddharth Chintamaneni
2026-09-04 20:33   ` bot+bpf-ci
2026-09-09 16:34     ` Siddharth Chintamaneni
2026-09-04 19:51 ` [PATCH bpf-next v1 3/7] bpf, arm64: " Siddharth Chintamaneni
2026-09-04 19:51 ` [PATCH bpf-next v1 4/7] bpf, powerpc64: " Siddharth Chintamaneni
2026-09-04 19:51 ` [PATCH bpf-next v1 5/7] bpf, riscv: " Siddharth Chintamaneni
2026-09-04 20:33   ` bot+bpf-ci
2026-09-04 19:51 ` [PATCH bpf-next v1 6/7] bpf, s390: " Siddharth Chintamaneni
2026-09-04 20:33   ` bot+bpf-ci
2026-09-04 19:51 ` [PATCH bpf-next v1 7/7] selftests/bpf: Test timed may_goto with private stacks Siddharth Chintamaneni
2026-09-21  3:48 ` Kumar Kartikeya Dwivedi [this message]
2026-09-21 16:24   ` [PATCH bpf-next v1 0/7] Fix " Siddharth Chintamaneni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DLKORFKGW8D8.XVSVVIH3Q6Q@gmail.com \
    --to=memxor@gmail.com \
    --cc=a.s.protopopov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=apais@microsoft.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=hargar@linux.microsoft.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=martin.lau@linux.dev \
    --cc=puranjay@kernel.org \
    --cc=rlmenge@gmail.com \
    --cc=sidchintamaneni@gmail.com \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox