* Re: [PATCH v3 2/5] LoongArch: BPF: Add support for exceptions [not found] <sashiko-outbox-234712@kernel.org> @ 2026-12-01 8:08 ` George Guo 0 siblings, 0 replies; 3+ messages in thread From: George Guo @ 2026-12-01 8:08 UTC (permalink / raw) To: yangtiezhu Cc: sashiko-reviews, chenhuacai, hengqi.chen, loongarch, bpf, linux-kernel On 2026/11/26 03:59, sashiko-bot@kernel.org wrote: > Do build_prologue() and __build_epilogue() also need to save and restore > LOONGARCH_GPR_S7 and LOONGARCH_GPR_S8 when is_exception_prog is true? > > In the LoongArch C ABI, s0 through s8 are callee-saved registers. JITed BPF > programs normally leave s7 and s8 untouched, and any called C function saves > and restores them in its own frame. When bpf_throw() unwinds the stack, > however, bpf_throw() and its callees never run their epilogues; the exception > callback restores only the boundary frame and returns directly to the > boundary program's caller, leaving s7 and s8 clobbered if any unwound C > function used them. Yes, this is a real issue. The JIT does not save $s7 and $s8 because it never uses them, but the C functions between the boundary program and bpf_throw() may use them, and their epilogues never run. So the caller of the boundary program can get $s7 and $s8 back clobbered. Will fix in v4: reserve two more slots below the $s6 slot for the boundary and callback programs, save $s7 and $s8 there in the prologue and restore them in the epilogue. The callback reuses the frame of the boundary program, so its epilogue restores the values saved by the boundary program. Thanks, George ^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v3 0/5] LoongArch: BPF: Add private stack, exceptions, large stack and arena support
@ 2026-11-26 3:40 George Guo
2026-11-26 3:40 ` [PATCH v3 2/5] LoongArch: BPF: Add support for exceptions George Guo
0 siblings, 1 reply; 3+ messages in thread
From: George Guo @ 2026-11-26 3:40 UTC (permalink / raw)
To: chenhuacai, yangtiezhu
Cc: hengqi.chen, guodongtai, bpf, loongarch, linux-kernel
From: George Guo <guodongtai@kylinos.cn>
This series adds the following features to the LoongArch BPF JIT:
- private stack
- exceptions (bpf_throw)
- 2 KiB of stack for programs, the same as on x86-64 and arm64
- sign-extending loads from arena (BPF_PROBE_MEM32SX)
- atomics on arena pointers (BPF_PROBE_ATOMIC)
The series is based on 7.4-rc4 with the following patches applied:
1. My 1 fix patch:
LoongArch: BPF: Fix the offset of stptr.w and stptr.d stores
https://lore.kernel.org/loongarch/20261118071444.10258-1-dongtai.guo@linux.dev/
2. Tiezhu's 1 optimization patch:
LoongArch: BPF: Optimize doubleword loads with ld.d and ldptr.d
https://lore.kernel.org/loongarch/20261118081201.10498-1-yangtiezhu@loongson.cn/
3. Tiezhu's 4 feature patches:
LoongArch: BPF: Support instructions array, indirect jumps and calls
https://lore.kernel.org/loongarch/20261123043907.20225-1-yangtiezhu@loongson.cn/
"LoongArch: BPF: Allow programs 2 KiB of stack" needs the stptr fix:
stack stores beyond the signed 12-bit offset range use stptr.{w/d}.
With this series, the following tests passed on a LoongArch VM (Fedora
43, clang 22.1.8):
$ sudo ./test_progs -t exceptions
Summary: 1/119 PASSED, 0 SKIPPED, 0/0 FAILED
$ sudo ./test_progs -a verifier_large_stack
Summary: 1/17 PASSED, 1 SKIPPED, 0/0 FAILED
$ sudo ./test_progs -t tailcalls
Summary: 1/43 PASSED, 0 SKIPPED, 0/0 FAILED
$ sudo ./test_progs -t struct_ops_private_stack
Summary: 1/4 PASSED, 0 SKIPPED, 0/0 FAILED
The skipped verifier_large_stack test only runs without large stack
support.
$ sudo ./test_progs -t verifier_ldsx | tail -1
Summary: 1/24 PASSED, 0 SKIPPED, 0/0 FAILED
$ sudo ./test_progs -t arena_atomics | tail -1
Summary: 1/11 PASSED, 0 SKIPPED, 0/0 FAILED
$ sudo ./test_progs -t libarena | tail -1
Summary: 2/43 PASSED, 0 SKIPPED, 0/0 FAILED
libarena also needs commit 27f2cf406a35 ("selftests/bpf: libarena: Raise
arena spinlock CPU limits") from bpf-next, since CONFIG_NR_CPUS is 2048
by default on LoongArch.
The struct_ops_private_stack, arena_atomics load-acquire/store-release
and libarena parallel tests, the arena tests in verifier_ldsx and the
"private stack: two frames of 2048 bytes" test in verifier_large_stack
are enabled for LoongArch by a separate selftests series for bpf-next,
which was applied for these runs.
Changes since v2:
- Drop v2 patches 1-3: "Support internal-only MOV to resolve per-CPU
addrs" and "Add timed may_goto support" are in mainline, and "Fix
tail call count pointer offset for arena programs" is superseded by
commit cd7e356b07a2 ("LoongArch: BPF: Move arena register slot below
TCC context").
- Patch 2: arch_bpf_stack_walk() went into mainline on its own as
commit e7103e5e3bde ("LoongArch: BPF: Add arch_bpf_stack_walk()
implementation"), so this patch only changes bpf_jit.c now.
- New patch 3: allow programs 2 KiB of stack.
- Patch 4: add bpf_jit_supports_insn() without the atomics rejection
that patch 5 used to remove (Tiezhu).
- Patch 5: on a fault, clear the register that the instruction loads
into instead of leaving it stale (Sashiko).
- Patch 5: emit cmpxchg on arena as a single amcas_db.{w/d}, only when
the CPU has LAMCAS, since the sc of an ll/sc loop can fault too
(Sashiko). Pass the new value in a temporary register, since rd of
amcas_db must differ from rk.
- Patch 5: drop the __add_exception_handler() split, which only the
ll/sc entry needed.
- Drop "selftests/bpf: Add LoongArch deny list" (Tiezhu).
- Move the selftests patches to a separate series for bpf-next,
"selftests/bpf: Enable private stack, large stack and arena tests
for LoongArch".
- Reword the subjects and commit messages following Tiezhu's style.
- Rebase onto 7.4-rc4 and the patches listed above.
v2: https://lore.kernel.org/loongarch/20260702022322.51033-1-dongtai.guo@linux.dev/
George Guo (5):
LoongArch: BPF: Add support for private stack
LoongArch: BPF: Add support for exceptions
LoongArch: BPF: Allow programs 2 KiB of stack
LoongArch: BPF: Add support for signed arena loads
LoongArch: BPF: Add support for arena atomics
Documentation/bpf/bpf_design_QA.rst | 13 +-
arch/loongarch/include/asm/inst.h | 4 +
arch/loongarch/net/bpf_jit.c | 391 ++++++++++++++++++++++++----
arch/loongarch/net/bpf_jit.h | 1 +
4 files changed, 355 insertions(+), 54 deletions(-)
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
prerequisite-patch-id: ee451ad20d651112f42e66d7ba6fa4242b124fb4
prerequisite-patch-id: 403cb992a7f660379bedce79e3068f1bc6facf05
prerequisite-patch-id: 221fa3fc56a55b9a7462ae7b21330a660240c8d2
prerequisite-patch-id: 08e033b51cefbf1d41f2e5325d591ff900ab81ef
prerequisite-patch-id: ffd6050832069f626c1f55492f0a1898137f6aa4
prerequisite-patch-id: 25f0b3ea5219dfd0676bdf22466453f2bf5dd5ea
--
2.53.0
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH v3 2/5] LoongArch: BPF: Add support for exceptions 2026-11-26 3:40 [PATCH v3 0/5] LoongArch: BPF: Add private stack, exceptions, large stack and arena support George Guo @ 2026-11-26 3:40 ` George Guo 2026-11-30 11:27 ` Tiezhu Yang 0 siblings, 1 reply; 3+ messages in thread From: George Guo @ 2026-11-26 3:40 UTC (permalink / raw) To: chenhuacai, yangtiezhu Cc: hengqi.chen, guodongtai, bpf, loongarch, linux-kernel From: George Guo <guodongtai@kylinos.cn> In order to implement JIT support for BPF exceptions, bpf_throw() needs to find the frame of the exception boundary program by walking the stack, and then call the exception callback with the stack pointer and frame pointer of that frame. The stack walking is done by arch_bpf_stack_walk(), which was added by commit e7103e5e3bde ("LoongArch: BPF: Add arch_bpf_stack_walk() implementation") and only works with the ORC unwinder, so make bpf_jit_supports_exceptions() return true only if CONFIG_UNWINDER_ORC is enabled. The exception callback does not set up a frame of its own: it takes the frame pointer of the boundary program from its third argument (a2) and reuses that frame, so its epilogue restores the callee-saved registers of the boundary program and returns to the caller of the boundary program. In order to keep the same stack layout for the boundary program and the callback, always reserve the $s6 slot for them, just like for arena programs. This is similar to commit 22fc0e80aeb5 ("bpf, arm64: support exceptions"). Signed-off-by: George Guo <guodongtai@kylinos.cn> --- arch/loongarch/net/bpf_jit.c | 45 ++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c index 3ff15b64925f..19907612a51c 100644 --- a/arch/loongarch/net/bpf_jit.c +++ b/arch/loongarch/net/bpf_jit.c @@ -120,8 +120,8 @@ static void prepare_bpf_tail_call_cnt(struct jit_ctx *ctx, int *store_offset) * +-------------------------+ * | tcc_ptr | * +-------------------------+ - * | arena | - * | (optional) | + * | $s6 (arena/exception) | + * | (optional) | * +-------------------------+ <--BPF_REG_FP * | prog->aux->stack_depth | * | (optional) | @@ -133,6 +133,13 @@ static void build_prologue(struct jit_ctx *ctx) int i, stack_adjust = 0, store_offset, bpf_stack_adjust; const struct bpf_prog *prog = ctx->prog; const bool is_main_prog = !bpf_is_subprog(prog); + /* + * Exception boundary and callback programs must agree on the frame + * layout: the callback reuses the boundary's frame to restore its + * callee-saved registers, so the s6 slot is always reserved for them. + */ + const bool is_exception_prog = prog->aux->exception_boundary || + prog->aux->exception_cb; bpf_stack_adjust = round_up(ctx->prog->aux->stack_depth, 16); @@ -142,7 +149,7 @@ static void build_prologue(struct jit_ctx *ctx) /* To store tcc and tcc_ptr */ stack_adjust += sizeof(long) * 2; - if (ctx->arena_vm_start) + if (ctx->arena_vm_start || is_exception_prog) stack_adjust += sizeof(long); stack_adjust = round_up(stack_adjust, 16); @@ -173,6 +180,19 @@ static void build_prologue(struct jit_ctx *ctx) if (is_main_prog) emit_insn(ctx, addid, REG_TCC, LOONGARCH_GPR_ZERO, 0); + if (prog->aux->exception_cb) { + /* + * The exception callback receives the boundary program's frame + * pointer as its third argument (a2). Reuse that frame so the + * (FP-anchored) epilogue restores the boundary's callee-saved + * registers and returns to the boundary's caller. The boundary + * already saved them, so nothing is pushed here. + */ + move_reg(ctx, LOONGARCH_GPR_FP, LOONGARCH_GPR_A2); + emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_FP, -stack_adjust); + goto setup_bpf_fp; + } + emit_insn(ctx, addid, LOONGARCH_GPR_SP, LOONGARCH_GPR_SP, -stack_adjust); store_offset = stack_adjust - sizeof(long); @@ -201,13 +221,14 @@ static void build_prologue(struct jit_ctx *ctx) prepare_bpf_tail_call_cnt(ctx, &store_offset); - if (ctx->arena_vm_start) { + if (ctx->arena_vm_start || is_exception_prog) { store_offset -= sizeof(long); emit_insn(ctx, std, REG_ARENA, LOONGARCH_GPR_SP, store_offset); } emit_insn(ctx, addid, LOONGARCH_GPR_FP, LOONGARCH_GPR_SP, stack_adjust); +setup_bpf_fp: if (ctx->priv_sp_used) { /* Point the BPF frame pointer at the top of this CPU's private stack */ void __percpu *priv_stack_ptr; @@ -229,6 +250,9 @@ static void __build_epilogue(struct jit_ctx *ctx, bool is_tail_call) { int stack_adjust = ctx->stack_size; int load_offset; + const struct bpf_prog *prog = ctx->prog; + const bool is_exception_prog = prog->aux->exception_boundary || + prog->aux->exception_cb; load_offset = stack_adjust - sizeof(long); emit_insn(ctx, ldd, LOONGARCH_GPR_RA, LOONGARCH_GPR_SP, load_offset); @@ -261,7 +285,7 @@ static void __build_epilogue(struct jit_ctx *ctx, bool is_tail_call) /* Skip the unused local 'tcc_ptr' slot to align with arena */ load_offset -= sizeof(long); - if (ctx->arena_vm_start) { + if (ctx->arena_vm_start || is_exception_prog) { load_offset -= sizeof(long); emit_insn(ctx, ldd, REG_ARENA, LOONGARCH_GPR_SP, load_offset); } @@ -2649,6 +2673,17 @@ bool bpf_jit_supports_private_stack(void) return true; } +bool bpf_jit_supports_exceptions(void) +{ + /* + * Walking kernel and BPF frames from within bpf_throw() relies on + * arch_bpf_stack_walk(), which is only implemented for the ORC + * unwinder. ORC reports each frame's stack and frame pointer and + * walks JITed BPF frames via its frame-pointer fallback. + */ + return IS_ENABLED(CONFIG_UNWINDER_ORC); +} + /* Indicate the JIT backend supports mixing bpf2bpf and tailcalls. */ bool bpf_jit_supports_subprog_tailcalls(void) { -- 2.53.0 ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v3 2/5] LoongArch: BPF: Add support for exceptions 2026-11-26 3:40 ` [PATCH v3 2/5] LoongArch: BPF: Add support for exceptions George Guo @ 2026-11-30 11:27 ` Tiezhu Yang 0 siblings, 0 replies; 3+ messages in thread From: Tiezhu Yang @ 2026-11-30 11:27 UTC (permalink / raw) To: George Guo, chenhuacai Cc: hengqi.chen, guodongtai, bpf, loongarch, linux-kernel On 11/26/26 11:40 AM, George Guo wrote: > From: George Guo <guodongtai@kylinos.cn> > > In order to implement JIT support for BPF exceptions, bpf_throw() needs > to find the frame of the exception boundary program by walking the > stack, and then call the exception callback with the stack pointer and > frame pointer of that frame. Please reply to the Sashiko AI review comments: https://lore.kernel.org/bpf/sashiko-outbox-234712@kernel.org/ Thanks, Tiezhu ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-01 8:08 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <sashiko-outbox-234712@kernel.org>
2026-12-01 8:08 ` [PATCH v3 2/5] LoongArch: BPF: Add support for exceptions George Guo
2026-11-26 3:40 [PATCH v3 0/5] LoongArch: BPF: Add private stack, exceptions, large stack and arena support George Guo
2026-11-26 3:40 ` [PATCH v3 2/5] LoongArch: BPF: Add support for exceptions George Guo
2026-11-30 11:27 ` Tiezhu Yang
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®