bpf, riscv: Add BPF stack arguments support for RV64 JIT
Subsystems (6)
v2 cover bpf, riscv: Add BPF stack arguments support for RV64 JIT
Port the BPF stack argument feature (BPF_REG_PARAMS / r11) to the RV64 JIT, matching what x86 and arm64 already do.
BPF-to-BPF calls keep all extra arguments on the stack. For kfunc calls, the JIT loads arguments 6-8 into A5-A7 at the call site to satisfy the RISC-V C calling convention.
Tested on riscv64 QEMU with LLVM main [1]; BPF-to-BPF and kfunc selftests pass.
[1] https://github.com/llvm/llvm-project/pull/189060
---
Changes in v2:
- Sign-extend 32-bit kfunc arguments passed on the stack (args 9+),
matching the register path and the RISC-V psABI. (Sashiko)
- Restrict the riscv selftest guard to __riscv_xlen == 64 so the
tests are not enabled on RV32. (Sashiko)
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Feng Jiang (2):
bpf, riscv: Add BPF stack arguments support for RV64 JIT selftests/bpf: Enable stack argument tests for RV64
arch/riscv/net/bpf_jit.h | 1 + arch/riscv/net/bpf_jit_comp64.c | 79 +++++++++++++++++++++- arch/riscv/net/bpf_jit_core.c | 4 ++ .../selftests/bpf/progs/btf__stack_arg_precision.c | 3 +- .../bpf/progs/btf__verifier_stack_arg_order.c | 3 +- tools/testing/selftests/bpf/progs/exceptions.c | 5 +- tools/testing/selftests/bpf/progs/stack_arg.c | 3 +- .../testing/selftests/bpf/progs/stack_arg_kfunc.c | 3 +- .../selftests/bpf/progs/stack_arg_precision.c | 3 +- .../selftests/bpf/progs/verifier_stack_arg.c | 3 +- .../selftests/bpf/progs/verifier_stack_arg_order.c | 3 +- 11 files changed, 98 insertions(+), 12 deletions(-)
---
base-commit: d114bb98936770c501c958bf2bc5fb6b7c0bad7b
change-id: 20260728-bpf-riscv-stack-args-6350d066a1e9
Best regards,
Signature · 1 line
Feng Jiang <[email protected]>
PATCH v2 1/2 bpf, riscv: Add BPF stack arguments support for RV64 JIT
Add bpf_jit_supports_stack_args() for the RV64 JIT so BPF subprograms and kfuncs can receive more than 5 arguments via the stack (BPF_REG_PARAMS / r11).
For BPF-to-BPF calls the caller writes outgoing arguments at the bottom of its frame (SP-relative). The callee reads them with FP-relative loads. Its FP is set to the caller SP in the prologue, so the offsets match.
The RISC-V ABI puts arguments 6-8 in A5-A7 and arguments 9+ at SP+0. Before each kfunc call, load arguments 6-8 from the outgoing area into A5-A7 and copy any remaining arguments down so argument 9 lands at SP+0.
A5 (BPF_REG_0), A6 (TCC) and A7 are safe to clobber here: R0 is not live before a call, TCC is backed up on the stack, and A7 is unused by the JIT.
Limit the existing kfunc sign-extension loop to MAX_BPF_FUNC_REG_ARGS iterations; otherwise idx >= 5 resolves to S1-S5 (BPF_R6-FP) and corrupts callee-saved registers.
--- arch/riscv/net/bpf_jit.h | 1 + arch/riscv/net/bpf_jit_comp64.c | 79 +++++++++++++++++++++++++++++++++++++++-- arch/riscv/net/bpf_jit_core.c | 4 +++ 3 files changed, 81 insertions(+), 3 deletions(-)
arch/riscv/net/bpf_jit.h +1 -0
@@ -82,6 +82,7 @@ struct rv_jit_context {unsigned long flags;int stack_size;int tcc_offset; +u16 stack_arg_size;u64 arena_vm_start;u64 user_vm_start; };
arch/riscv/net/bpf_jit_comp64.c +76 -3
128 lines not on this page · raw message
arch/riscv/net/bpf_jit_core.c +4 -0
@@ -72,6 +72,10 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr ctx->arena_vm_start =bpf_arena_get_kern_vm_start (prog->aux->arena); ctx->user_vm_start =bpf_arena_get_user_vm_start (prog->aux->arena); ctx->prog = prog; + + ctx->stack_arg_size =round_up (bpf_out_stack_arg_cnt (env, prog) * +sizeof (u64),STACK_ALIGN ); + ctx->offset =kvzalloc_objs (int, prog->len);if (!ctx->offset) goto out_offset;
Signature · 1 line
2.53.0
[email protected]
AI reviewThis looks like a bug fix for stack argument support. The commit message describes how the sign-extension loop was limited to prevent corruption of callee-saved registers S1-S5 when idx >= 5. This…
Quoted 20 lines
bpf, riscv: Add BPF stack arguments support for RV64 JIT
Add
bpf_jit_supports_stack_args()for the RV64 JIT so BPF subprograms and kfuncs can receive more than 5 arguments via the stack (BPF_REG_PARAMS/ r11).For BPF-to-BPF calls the caller writes outgoing arguments at the bottom of its frame (SP-relative). The callee reads them with FP-relative loads. Its FP is set to the caller SP in the prologue, so the offsets match.
The RISC-V ABI puts arguments 6-8 in A5-A7 and arguments 9+ at SP+0. Before each kfunc call, load arguments 6-8 from the outgoing area into A5-A7 and copy any remaining arguments down so argument 9 lands at SP+0.
A5 (
BPF_REG_0), A6 (TCC) and A7 are safe to clobber here: R0 is not live before a call, TCC is backed up on the stack, and A7 is unused by the JIT.
Quoted 5 lines
Limit the existing kfunc sign-extension loop to
MAX_BPF_FUNC_REG_ARGSiterations; otherwise idx >= 5 resolves to S1-S5 (BPF_R6-FP) and corrupts callee-saved registers.Signed-off-by Feng Jiang <[email protected]>
This looks like a bug fix for stack argument support. The commit message describes how the sign-extension loop was limited to prevent corruption of callee-saved registers S1-S5 when idx >= 5. This fixes an issue introduced by commit 443574b03387 ("riscv, bpf: Fix kfunc parameters incompatibility between bpf and riscv abi"), which added the sign-extension loop without limiting it to MAX_BPF_FUNC_REG_ARGS.
Should this include:
---
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/31677823910
Quoted 33 lines · On 2026/8/13 16:25, [email protected] wrote:
Quoted 20 lines
bpf, riscv: Add BPF stack arguments support for RV64 JIT
Add
bpf_jit_supports_stack_args()for the RV64 JIT so BPF subprograms and kfuncs can receive more than 5 arguments via the stack (BPF_REG_PARAMS/ r11).For BPF-to-BPF calls the caller writes outgoing arguments at the bottom of its frame (SP-relative). The callee reads them with FP-relative loads. Its FP is set to the caller SP in the prologue, so the offsets match.
The RISC-V ABI puts arguments 6-8 in A5-A7 and arguments 9+ at SP+0. Before each kfunc call, load arguments 6-8 from the outgoing area into A5-A7 and copy any remaining arguments down so argument 9 lands at SP+0.
A5 (
BPF_REG_0), A6 (TCC) and A7 are safe to clobber here: R0 is not live before a call, TCC is backed up on the stack, and A7 is unused by the JIT.Quoted 5 lines
Limit the existing kfunc sign-extension loop to
MAX_BPF_FUNC_REG_ARGSiterations; otherwise idx >= 5 resolves to S1-S5 (BPF_R6-FP) and corrupts callee-saved registers.Signed-off-by Feng Jiang <[email protected]>This looks like a bug fix for stack argument support. The commit message describes how the sign-extension loop was limited to prevent corruption of callee-saved registers S1-S5 when idx >= 5. This fixes an issue introduced by commit
443574b03387("riscv, bpf: Fix kfunc parameters incompatibility between bpf and riscv abi"), which added the sign-extension loop without limiting it toMAX_BPF_FUNC_REG_ARGS.
Quoted 4 lines
Should this include:
Fixes 443574b03387 ("riscv, bpf: Fix kfunc parameters incompatibility between bpf and riscv abi")
No Fixes: tag. When 443574b03387 added the loop, the verifier still unconditionally rejected kfuncs with more than 5 args, so fm->nr_args was always <= 5 and the loop never touched S1-S5. The corruption is only reachable once this series enables stack arguments, so bounding the loop is part of the feature, not a fix for a triggerable bug.
Quoted 6 lines
---
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.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/3167782391
Signature · 2 lines
With Best Regards, Feng Jiang
PATCH v2 2/2 selftests/bpf: Enable stack argument tests for RV64
In reply to Feng JiangEnable the stack argument selftests for RV64. The riscv guard is restricted to __riscv_xlen == 64 since stack arguments are only supported by the RV64 JIT.
Keep the __BPF_FEATURE_STACK_ARGUMENT gate, defined by clang with BPF stack argument support [1].
[1] https://github.com/llvm/llvm-project/pull/189060
--- tools/testing/selftests/bpf/progs/btf__stack_arg_precision.c | 3 ++- tools/testing/selftests/bpf/progs/btf__verifier_stack_arg_order.c | 3 ++- tools/testing/selftests/bpf/progs/exceptions.c | 5 +++-- tools/testing/selftests/bpf/progs/stack_arg.c | 3 ++- tools/testing/selftests/bpf/progs/stack_arg_kfunc.c | 3 ++- tools/testing/selftests/bpf/progs/stack_arg_precision.c | 3 ++- tools/testing/selftests/bpf/progs/verifier_stack_arg.c | 3 ++- tools/testing/selftests/bpf/progs/verifier_stack_arg_order.c | 3 ++- 8 files changed, 17 insertions(+), 9 deletions(-)
tools/testing/selftests/bpf/progs/btf__stack_arg_precision.c +2 -1
@@ -4,7 +4,8 @@#include <bpf/bpf_helpers.h> #include "../test_kmods/bpf_testmod_kfunc.h" -#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)) && \ +#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64) || \ + (defined(__TARGET_ARCH_riscv) && __riscv_xlen ==64 )) && \ defined(__BPF_FEATURE_STACK_ARGUMENT)long subprog_call_mem_kfunc (long a,long b,long c,long d,long e,long size)
tools/testing/selftests/bpf/progs/btf__verifier_stack_arg_order.c +2 -1
@@ -3,7 +3,8 @@#include <vmlinux.h> #include <bpf/bpf_helpers.h> -#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)) && \ +#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64) || \ + (defined(__TARGET_ARCH_riscv) && __riscv_xlen ==64 )) && \ defined(__BPF_FEATURE_STACK_ARGUMENT)int subprog_bad_order_6args (int a,int b,int c,int d,int e,int f)
tools/testing/selftests/bpf/progs/exceptions.c +3 -2
@@ -379,8 +379,9 @@ int exception_bad_assert_range_with(struct __sk_buff *ctx)return 1 ; } -#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)) \ - && defined(__BPF_FEATURE_STACK_ARGUMENT) +#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64) || \ + (defined(__TARGET_ARCH_riscv) && __riscv_xlen ==64 )) && \ + defined(__BPF_FEATURE_STACK_ARGUMENT)const volatile bool has_stack_arg = true;
tools/testing/selftests/bpf/progs/stack_arg.c +2 -1
@@ -21,7 +21,8 @@ struct {int timer_result; -#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)) && \ +#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64) || \ + (defined(__TARGET_ARCH_riscv) && __riscv_xlen ==64 )) && \ defined(__BPF_FEATURE_STACK_ARGUMENT)const volatile bool has_stack_arg = true;
tools/testing/selftests/bpf/progs/stack_arg_kfunc.c +2 -1
@@ -6,7 +6,8 @@#include "bpf_kfuncs.h" #include "../test_kmods/bpf_testmod_kfunc.h" -#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)) && \ +#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64) || \ + (defined(__TARGET_ARCH_riscv) && __riscv_xlen ==64 )) && \ defined(__BPF_FEATURE_STACK_ARGUMENT)const volatile bool has_stack_arg = true;
tools/testing/selftests/bpf/progs/stack_arg_precision.c +2 -1
@@ -6,7 +6,8 @@#include "../test_kmods/bpf_testmod_kfunc.h" #include "bpf_misc.h" -#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)) && \ +#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64) || \ + (defined(__TARGET_ARCH_riscv) && __riscv_xlen ==64 )) && \ defined(__BPF_FEATURE_STACK_ARGUMENT)/* Force kfunc extern BTF generation for inline asm call below.
tools/testing/selftests/bpf/progs/verifier_stack_arg.c +2 -1
@@ -12,7 +12,8 @@ struct { __type(value, long long); } map_hash_8b SEC(".maps"); -#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)) && \ +#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64) || \ + (defined(__TARGET_ARCH_riscv) && __riscv_xlen ==64 )) && \ defined(__BPF_FEATURE_STACK_ARGUMENT) __noinline__used
tools/testing/selftests/bpf/progs/verifier_stack_arg_order.c +2 -1
@@ -5,7 +5,8 @@#include <bpf/bpf_helpers.h> #include "bpf_misc.h" -#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)) && \ +#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64) || \ + (defined(__TARGET_ARCH_riscv) && __riscv_xlen ==64 )) && \ defined(__BPF_FEATURE_STACK_ARGUMENT) __noinline__used __naked
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH bpf-next v2 2/2] selftests/bpf: Enable stack argument tests for RV64
Can the code following this guard actually run on RV64?
Quoted 5 lines
tools/testing/selftests/bpf/progs/exceptions.c +0
-0
tools/testing/selftests/bpf/progs/exceptions.c int exception_bad_assert_range_with(struct __sk_buff *ctx) · line 386
Quoted 10 lines
tools/testing/selftests/bpf/progs/exceptions.c +3
-2@@ -379,8 +379,9 @@ int exception_bad_assert_range_with(struct __sk_buff *ctx)return 1 ; } -#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)) \ - && defined(__BPF_FEATURE_STACK_ARGUMENT) +#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64) || \ + (defined(__TARGET_ARCH_riscv) && __riscv_xlen ==64 )) && \ + defined(__BPF_FEATURE_STACK_ARGUMENT)const volatile bool has_stack_arg = true;
Can the code following this guard actually run on RV64?
The block enabled by this guard contains four bpf_throw()-based programs (exception_throw_stack_arg, exception_throw_after_stack_arg, exception_throw_subprog_stack_arg, exception_throw_subprog_after_stack_arg) plus the has_stack_arg flag. bpf_throw is only accepted by the verifier when the target JIT supports exceptions:
kernel/bpf/verifier.c
if (!bpf_jit_supports_exceptions()) {RISC-V implements neither bpf_jit_supports_exceptions() nor arch_bpf_stack_walk() (only arch/x86, arm64, s390 and powerpc do), so both fall back to the __weak definitions in kernel/bpf/core.c:
bool __weak bpf_jit_supports_exceptions(void) { return false; }
void __weak arch_bpf_stack_walk(...) { }Consequently exceptions.bpf.o cannot be loaded at all on RV64 - the rest of exceptions.c is full of bpf_throw() outside this guard - and the test is already excluded from RV64 runs:
tools/testing/selftests/bpf/DENYLIST.riscv64
# riscv64 deny list for BPF CI and local vmtest
exceptions # JIT does not support exceptionsprog_tests/exceptions.c only consumes has_stack_arg after exceptions__load() succeeds:
ret = exceptions__load(skel);
if (!ASSERT_OK(ret, "exceptions__load"))
goto done;
...
if (skel->rodata->has_stack_arg) {
RUN_SUCCESS(exception_throw_stack_arg, 56);so on RV64 the flag is never read. The hunk therefore changes nothing observable on riscv64 and cannot have been validated there, which conflicts with the commit subject 'Enable the stack argument selftests for RV64'. The other seven hunks are genuinely effective; only this one is not.
Either drop the exceptions.c hunk from this patch (and add it when the RV64 JIT gains arch_bpf_stack_walk()/bpf_jit_supports_exceptions()), or state in the changelog that it is deliberately pre-enabled for a future RV64 exception implementation and remains untested for now.
---
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/31677823910