Skip to contentSkip to conversation

Add bpf stack arguments support for RV64

PATCH Applied Pu Lehui 5 participants 10 messages Sep 5 – Sep 6

Subsystems (6)
Kselftest BPF RISC-V BPF selftests +2 more subsystems
lore
Share & tools
lore

Pu Lehui

author
#0 lore raw

v5 cover Add bpf stack arguments support for RV64

Implement bpf_jit_supports_stack_args() on RV64 JIT to allow bpf subprogs and kfuncs to pass and receive more than 5 arguments.

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 v5:
- Add RV_EXTRA_STK_ARGS macro to represent the exceeding stack args passed in riscv arg regs. (BPF CI)
- Optimize emit_stack_arg_st() by materializing immediates directly into destination registers. (BPF CI)
- Update stale comments. (BPF CI and sashiko)

Changes in v4:
- Remap BPF_REG_0 and RV_REG_TCC to align the calling convention of bpf2bpf and kfunc calls with the riscv abi.
- Adapt the bpf stack arguments implementation based on change 1.

Changes in v3:
- Drop the exceptions.c guard change: the guarded programs use
bpf_throw(), unsupported by the RV64 JIT, so the test is denylisted
on riscv64 and the guard has no effect. (BPF CI)
- Link to v2: https://lore.kernel.org/r/[email protected]

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):
riscv, bpf: Add BPF stack arguments support for RV64 JIT selftests/bpf: Enable stack argument tests for RV64

Pu Lehui (1):
riscv, bpf: Remap BPF_REG_0 and RV_REG_TCC

 arch/riscv/net/bpf_jit.h                      |  4 +
 arch/riscv/net/bpf_jit_comp64.c               | 90 +++++++++++++++----
 arch/riscv/net/bpf_jit_core.c                 |  7 ++
 arch/riscv/net/bpf_timed_may_goto.S           |  8 +-
 .../bpf/progs/btf__stack_arg_precision.c      |  3 +-
 .../bpf/progs/btf__verifier_stack_arg_order.c |  3 +-
 tools/testing/selftests/bpf/progs/stack_arg.c |  3 +-
 .../selftests/bpf/progs/stack_arg_kfunc.c     |  3 +-
 .../selftests/bpf/progs/stack_arg_precision.c |  3 +-
 .../selftests/bpf/progs/verifier_jit_inline.c |  2 +-
 .../selftests/bpf/progs/verifier_ldsx.c       |  6 +-
 .../selftests/bpf/progs/verifier_stack_arg.c  |  3 +-
 .../bpf/progs/verifier_stack_arg_order.c      |  3 +-
 13 files changed, 108 insertions(+), 30 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v5 1/3 riscv, bpf: Remap BPF_REG_0 and RV_REG_TCC

From: Pu Lehui <[email protected]>

To prepare for supporting bpf stack arguments and unifying the calling convention between bpf2bpf calls and kfunc calls, remap BPF_REG_0 to t6 and RV_REG_TCC to t5.

In the riscv abi, a0-a7 are designated for function argument passing. Freeing a5 and a6 allows bpf2bpf calls to pass extra arguments (6th to 8th) via a5-a7 consistently with kfuncs.

---
 arch/riscv/net/bpf_jit_comp64.c                        | 10 +++++-----
 arch/riscv/net/bpf_timed_may_goto.S                    |  8 ++++----
 .../testing/selftests/bpf/progs/verifier_jit_inline.c  |  2 +-
 tools/testing/selftests/bpf/progs/verifier_ldsx.c      |  6 +++---
 4 files changed, 13 insertions(+), 13 deletions(-)
arch/riscv/net/bpf_jit_comp64.c +5 -5
index e7378be171a9..fc1c8c5480b6 100644
@@ -23,11 +23,11 @@
 /* fentry and TCC init insns will be skipped on tailcall */
 #define RV_TAILCALL_OFFSET ((RV_FENTRY_NINSNS + 1) * 4)
 
-#define RV_REG_TCC RV_REG_A6
+#define RV_REG_TCC RV_REG_T5
 #define RV_REG_ARENA RV_REG_S7 /* For storing arena_vm_start */
 
 static const int regmap[] = {
-	[BPF_REG_0] =	RV_REG_A5,
+	[BPF_REG_0] =	RV_REG_T6,
 	[BPF_REG_1] =	RV_REG_A0,
 	[BPF_REG_2] =	RV_REG_A1,
 	[BPF_REG_3] =	RV_REG_A2,
@@ -47,13 +47,13 @@ static const int pt_regmap[] = {
 	[RV_REG_A2] = offsetof(struct pt_regs, a2),
 	[RV_REG_A3] = offsetof(struct pt_regs, a3),
 	[RV_REG_A4] = offsetof(struct pt_regs, a4),
-	[RV_REG_A5] = offsetof(struct pt_regs, a5),
 	[RV_REG_S1] = offsetof(struct pt_regs, s1),
 	[RV_REG_S2] = offsetof(struct pt_regs, s2),
 	[RV_REG_S3] = offsetof(struct pt_regs, s3),
 	[RV_REG_S4] = offsetof(struct pt_regs, s4),
 	[RV_REG_S5] = offsetof(struct pt_regs, s5),
 	[RV_REG_T0] = offsetof(struct pt_regs, t0),
+	[RV_REG_T6] = offsetof(struct pt_regs, t6),
 };
 
 enum {
@@ -239,7 +239,7 @@ static void __build_epilogue(bool is_tail_call, struct rv_jit_context *ctx)
 	emit_addi(RV_REG_SP, RV_REG_SP, stack_adjust, ctx);
 	/* Set return value. */
 	if (!is_tail_call)
-		emit_addiw(RV_REG_A0, RV_REG_A5, 0, ctx);
+		emit_addiw(RV_REG_A0, regmap[BPF_REG_0], 0, ctx);
 	emit_jalr(RV_REG_ZERO, is_tail_call ? RV_REG_T3 : RV_REG_RA,
 		  is_tail_call ? RV_TAILCALL_OFFSET : 0, ctx);
 }
@@ -1075,7 +1075,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 
 	save_ret = flags & (BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_RET_FENTRY_RET);
 	if (save_ret)
-		stack_size += 16; /* Save both A5 (BPF R0) and A0 */
+		stack_size += 16; /* Save both RV_REG_T6 (BPF R0) and RV_REG_A0 */
 	retval_off = stack_size;
 
 	stack_size += nr_arg_slots * 8;
arch/riscv/net/bpf_timed_may_goto.S +4 -4
index 02c637d87420..fcf850f244c9 100644
@@ -10,7 +10,7 @@
  *	- 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
+ * ptr = BPF_REG_FP (s5) + BPF_REG_AX (t0). BPF R0-R5 (t6, a0-a4) are saved
  * across the call; BPF_REG_FP (s5) is callee-saved and needs no saving.
  */
 
@@ -20,8 +20,8 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)
 	REG_S	s0, 6*SZREG(sp)
 	addi	s0, sp, 8*SZREG
 
-	/* Save BPF registers R0-R5 (a5, a0-a4) */
-	REG_S	a5, 5*SZREG(sp)
+	/* Save BPF registers R0-R5 (t6, a0-a4) */
+	REG_S	t6, 5*SZREG(sp)
 	REG_S	a0, 4*SZREG(sp)
 	REG_S	a1, 3*SZREG(sp)
 	REG_S	a2, 2*SZREG(sp)
@@ -38,7 +38,7 @@ SYM_FUNC_START(arch_bpf_timed_may_goto)
 	REG_L	a2, 2*SZREG(sp)
 	REG_L	a1, 3*SZREG(sp)
 	REG_L	a0, 4*SZREG(sp)
-	REG_L	a5, 5*SZREG(sp)
+	REG_L	t6, 5*SZREG(sp)
 
 	REG_L	s0, 6*SZREG(sp)
 	REG_L	ra, 7*SZREG(sp)
tools/testing/selftests/bpf/progs/verifier_jit_inline.c +1 -1
index 02e562f56f9d..5b92da2ab75c 100644
@@ -11,7 +11,7 @@ __jited("	addq	%gs:{{.*}}, %rax")
 __arch_arm64
 __jited("	mrs	x8, SP_EL0")
 __arch_riscv64
-__jited("	mv	a5, tp")
+__jited("	mv	t6, tp")
 __arch_loongarch
 __jited("	move	$a5, $tp")
 int inline_bpf_get_current_task(void)
tools/testing/selftests/bpf/progs/verifier_ldsx.c +3 -3
index 7e25e1b8cd0b..49e8cd4bb4f9 100644
@@ -289,11 +289,11 @@ __jited("ldrsh	x22, [x11, #0x18]")
 __jited("add	x11, x0, x28")
 __jited("ldrsb	x22, [x11, #0x20]")
 __arch_riscv64
-__jited("add	t2, a5, s7")
+__jited("add	t2, t6, s7")
 __jited("lw	s3, 0x10(t2)")
-__jited("add	t2, a5, s7")
+__jited("add	t2, t6, s7")
 __jited("lh	s3, 0x18(t2)")
-__jited("add	t2, a5, s7")
+__jited("add	t2, t6, s7")
 __jited("lb	s3, 0x20(t2)")
 __jited("add	t2, a0, s7")
 __jited("lw	s4, 0x10(t2)")
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH bpf-next v5 1/3] riscv, bpf: Remap BPF_REG_0 and RV_REG_TCC

This isn't a bug, but does moving BPF_REG_0 to t6 affect code density? arch/riscv/net/bpf_jit.h:is_creg() only accepts fp, s1, and a0-a5 for RVC-compressible forms. With CONFIG_RISCV_ISA_C, every…
Quoted 8 lines
arch/riscv/net/bpf_jit_comp64.c +0 -0
index e7378be171a9..fc1c8c5480b6 100644
@@ -23,11 +23,11 @@
 /* fentry and TCC init insns will be skipped on tailcall */
 #define RV_TAILCALL_OFFSET ((RV_FENTRY_NINSNS + 1) * 4)
 

arch/riscv/net/bpf_jit_comp64.c line 33

Quoted 10 lines
arch/riscv/net/bpf_jit_comp64.c +2 -2
-#define RV_REG_TCC RV_REG_A6
+#define RV_REG_TCC RV_REG_T5
 #define RV_REG_ARENA RV_REG_S7 /* For storing arena_vm_start */
 
 static const int regmap[] = {
-	[BPF_REG_0] =	RV_REG_A5,
+	[BPF_REG_0] =	RV_REG_T6,
 	[BPF_REG_1] =	RV_REG_A0,
 	[BPF_REG_2] =	RV_REG_A1,
 	[BPF_REG_3] =	RV_REG_A2,

This isn't a bug, but does moving BPF_REG_0 to t6 affect code density? arch/riscv/net/bpf_jit.h:is_creg() only accepts fp, s1, and a0-a5 for RVC-compressible forms. With CONFIG_RISCV_ISA_C, every emitter that gates on is_creg() now falls back to 4-byte instructions whenever BPF R0 is the destination or base register. Since R0 holds return values and helper results, could this measurably grow JIT image size for R0-heavy code?

The changelog explains why a5 and a6 needed to be freed, but it doesn't mention why t6 and t5 were chosen. Could a sentence be added about the register selection rationale, particularly since t4 appears unused by the JIT?

arch/riscv/net/bpf_jit_comp64.c static const int pt_regmap[] = { · line 57

Quoted 13 lines
arch/riscv/net/bpf_jit_comp64.c +1 -1
@@ -47,13 +47,13 @@ static const int pt_regmap[] = {
 	[RV_REG_A2] = offsetof(struct pt_regs, a2),
 	[RV_REG_A3] = offsetof(struct pt_regs, a3),
 	[RV_REG_A4] = offsetof(struct pt_regs, a4),
-	[RV_REG_A5] = offsetof(struct pt_regs, a5),
 	[RV_REG_S1] = offsetof(struct pt_regs, s1),
 	[RV_REG_S2] = offsetof(struct pt_regs, s2),
 	[RV_REG_S3] = offsetof(struct pt_regs, s3),
 	[RV_REG_S4] = offsetof(struct pt_regs, s4),
 	[RV_REG_S5] = offsetof(struct pt_regs, s5),
 	[RV_REG_T0] = offsetof(struct pt_regs, t0),
+	[RV_REG_T6] = offsetof(struct pt_regs, t6),
 };

[ ... ]

---
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/33956847598

author
#3 lore raw

Re: [PATCH bpf-next v5 1/3] riscv, bpf: Remap BPF_REG_0 and RV_REG_TCC

Quoted 12 lines · On 2026/9/5 17:42, [email protected] wrote:
Quoted 12 lines
arch/riscv/net/bpf_jit_comp64.c +1 -1
index e7378be171a9..fc1c8c5480b6 100644
@@ -23,11 +23,11 @@
  /* fentry and TCC init insns will be skipped on tailcall */
  #define RV_TAILCALL_OFFSET ((RV_FENTRY_NINSNS + 1) * 4)
 
-#define RV_REG_TCC RV_REG_A6
+#define RV_REG_TCC RV_REG_T5
  #define RV_REG_ARENA RV_REG_S7 /* For storing arena_vm_start */
 
Quoted 13 lines

arch/riscv/net/bpf_jit_comp64.c line 33

Quoted 6 lines
arch/riscv/net/bpf_jit_comp64.c +1 -1
  static const int regmap[] = {
-	[BPF_REG_0] =	RV_REG_A5,
+	[BPF_REG_0] =	RV_REG_T6,
  	[BPF_REG_1] =	RV_REG_A0,
  	[BPF_REG_2] =	RV_REG_A1,
  	[BPF_REG_3] =	RV_REG_A2,

This isn't a bug, but does moving BPF_REG_0 to t6 affect code density? arch/riscv/net/bpf_jit.h:is_creg() only accepts fp, s1, and a0-a5 for RVC-compressible forms. With CONFIG_RISCV_ISA_C, every emitter that gates on is_creg() now falls back to 4-byte instructions whenever BPF R0 is the destination or base register. Since R0 holds return values and helper results, could this measurably grow JIT image size for R0-heavy code?

This is expected. The bpf prog image size growth is expected to be negligible, and it is a necessary trade-off to free up registers for the calling convention.

Quoted 5 lines

The changelog explains why a5 and a6 needed to be freed, but it doesn't mention why t6 and t5 were chosen. Could a sentence be added about the register selection rationale, particularly since t4 appears unused by the JIT?

It's fine, let's mention here. We prefer caller-saved registers that are unused both now and going forward.

Quoted 23 lines
Quoted 4 lines
arch/riscv/net/bpf_jit_comp64.c +0 -0
@@ -47,13 +47,13 @@ static const int pt_regmap[] = {
  	[RV_REG_A2] = offsetof(struct pt_regs, a2),
  	[RV_REG_A3] = offsetof(struct pt_regs, a3),

arch/riscv/net/bpf_jit_comp64.c static const int pt_regmap[] = { · line 57

Quoted 10 lines
arch/riscv/net/bpf_jit_comp64.c +1 -1
  	[RV_REG_A4] = offsetof(struct pt_regs, a4),
-	[RV_REG_A5] = offsetof(struct pt_regs, a5),
  	[RV_REG_S1] = offsetof(struct pt_regs, s1),
  	[RV_REG_S2] = offsetof(struct pt_regs, s2),
  	[RV_REG_S3] = offsetof(struct pt_regs, s3),
  	[RV_REG_S4] = offsetof(struct pt_regs, s4),
  	[RV_REG_S5] = offsetof(struct pt_regs, s5),
  	[RV_REG_T0] = offsetof(struct pt_regs, t0),
+	[RV_REG_T6] = offsetof(struct pt_regs, t6),
  };

[ ... ]

---
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/33956847598

author
#4 lore raw

PATCH v5 2/3 riscv, bpf: Add BPF stack arguments support for RV64 JIT

In reply to Pu Lehui

From: Feng Jiang <[email protected]>

Implement bpf_jit_supports_stack_args() on RV64 JIT to allow bpf subprogs and kfuncs to pass and receive more than 5 arguments.

In the riscv abi, the first 8 arguments are passed in registers a0 to a7, and arguments 9+ reside on the stack. To align bpf stack arguments with this calling convention and unify bpf2bpf calls with kfuncs, map the first 3 bpf stack arguments (6th to 8th) directly to a5 to a7, and store or load the remaining arguments (9th+) at SP or FP. Reserve outgoing stack space in the prologue accordingly when stack_arg_cnt exceeds 3.

In addition, update kfunc argument sign extension to handle all 8 register arguments as well as any arguments passed on the stack.

---
 arch/riscv/net/bpf_jit.h        |  4 ++
 arch/riscv/net/bpf_jit_comp64.c | 80 ++++++++++++++++++++++++++++-----
 arch/riscv/net/bpf_jit_core.c   |  7 +++
 3 files changed, 81 insertions(+), 10 deletions(-)
arch/riscv/net/bpf_jit.h +4 -0
index 419b9d795f2a..5c4f53060c91 100644
@@ -12,6 +12,9 @@
 #include <linux/bpf.h>
 #include <linux/filter.h>
 
+#define RV_MAX_REG_ARGS		8
+#define RV_EXTRA_STK_ARGS	(RV_MAX_REG_ARGS - MAX_BPF_FUNC_REG_ARGS)
+
 /* verify runtime detection extension status */
 #define rv_ext_enabled(ext) \
 	(IS_ENABLED(CONFIG_RISCV_ISA_##ext) && riscv_has_extension_likely(RISCV_ISA_EXT_##ext))
@@ -82,6 +85,7 @@ struct rv_jit_context {
 	unsigned long flags;
 	int stack_size;
 	int tcc_offset;
+	int stack_arg_sz;
 	u64 arena_vm_start;
 	u64 user_vm_start;
 };
arch/riscv/net/bpf_jit_comp64.c +70 -10

139 lines not on this page · raw message

arch/riscv/net/bpf_jit_core.c +7 -0
index cbfcd287ea16..2fb0b4e198b9 100644
@@ -48,6 +48,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
 	int pass = 0, prev_ninsns = 0, i;
 	struct rv_jit_data *jit_data;
 	struct rv_jit_context *ctx;
+	u16 stack_arg_cnt;
 
 	if (!prog->jit_requested)
 		return prog;
@@ -71,6 +72,12 @@ 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);
+
+	stack_arg_cnt = bpf_out_stack_arg_cnt(env, prog);
+	/* First 3 stack args in regs, rest on stack */
+	ctx->stack_arg_sz = stack_arg_cnt > RV_EXTRA_STK_ARGS ?
+			    round_up((stack_arg_cnt - RV_EXTRA_STK_ARGS) * 8, STACK_ALIGN) : 0;
+
 	ctx->prog = prog;
 	ctx->offset = kvzalloc_objs(int, prog->len);
 	if (!ctx->offset)
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH v5 3/3 selftests/bpf: Enable stack argument tests for RV64

In reply to Pu Lehui

From: Feng Jiang <[email protected]>

Enable 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 ++-
 .../selftests/bpf/progs/btf__verifier_stack_arg_order.c        | 3 ++-
 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 ++-
 7 files changed, 14 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/progs/btf__stack_arg_precision.c +2 -1
index 8d38aafe66a2..ab12397a9b52 100644
@@ -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
index 99bc115f8380..a980a3a399d3 100644
@@ -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/stack_arg.c +2 -1
index 944e3bb603e7..3e9ed37c57bb 100644
@@ -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
index 345f2da2e361..b6b1a8aa7f44 100644
@@ -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
index ce0301a41fa9..7baa9c1c8e31 100644
@@ -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
index 51d22faf4559..f6a4a6aa0e61 100644
@@ -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
index 8e4325273b3d..57f22691744a 100644
@@ -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.34.1
#6 lore raw

Re: [PATCH bpf-next v5 3/3] selftests/bpf: Enable stack argument tests for RV64

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Missing architecture guard update for stack arguments in…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Missing architecture guard update for stack arguments in tools/testing/selftests/bpf/progs/exceptions.c. --

commit 7ea892ecb0a3b4c34ad8033c7d6de54aaa01a98d
Author: Feng Jiang <[email protected]>

selftests/bpf: Enable stack argument tests for RV64

Enable 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

Quoted 5 lines
tools/testing/selftests/bpf/progs/stack_arg.c +0 -0
index 944e3bb603e7e..3e9ed37c57bb9 100644

tools/testing/selftests/bpf/progs/stack_arg.c struct { · line 28

Quoted 9 lines
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;

[Severity: Low]
Does tools/testing/selftests/bpf/progs/exceptions.c also need this architecture guard update?

The has_stack_arg macro in exceptions.c enables stack-argument-specific exception tests. The RV64 architecture check seems to be omitted from that file during this refactoring:

tools/testing/selftests/bpf/progs/exceptions.c:

#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)) \
	&& defined(__BPF_FEATURE_STACK_ARGUMENT)

const volatile bool has_stack_arg = true;

When exception support is eventually added for the RV64 JIT, the stack argument portions of the exceptions selftest will silently fail to enable, reducing test coverage.

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

Re: [PATCH bpf-next v5 3/3] selftests/bpf: Enable stack argument tests for RV64

Quoted 40 lines · On 2026/9/5 17:03, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Missing architecture guard update for stack arguments in tools/testing/selftests/bpf/progs/exceptions.c. --

commit 7ea892ecb0a3b4c34ad8033c7d6de54aaa01a98d
Author: Feng Jiang <[email protected]>

selftests/bpf: Enable stack argument tests for RV64

Enable 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

Quoted 5 lines
tools/testing/selftests/bpf/progs/stack_arg.c +0 -0
index 944e3bb603e7e..3e9ed37c57bb9 100644

tools/testing/selftests/bpf/progs/stack_arg.c struct { · line 28

Quoted 9 lines
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;

[Severity: Low]
Does tools/testing/selftests/bpf/progs/exceptions.c also need this architecture guard update?

The has_stack_arg macro in exceptions.c enables stack-argument-specific exception tests. The RV64 architecture check seems to be omitted from that file during this refactoring:

tools/testing/selftests/bpf/progs/exceptions.c:

Quoted 9 lines
#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)) \
	&& defined(__BPF_FEATURE_STACK_ARGUMENT)

const volatile bool has_stack_arg = true;

When exception support is eventually added for the RV64 JIT, the stack argument portions of the exceptions selftest will silently fail to enable, reducing test coverage.

bpf exception not support for rv64 yet.

maintainer Acked-by on the v5 series
#8 lore raw
In reply to Pu Lehui
Quoted 34 lines · Pu Lehui

Implement bpf_jit_supports_stack_args() on RV64 JIT to allow bpf subprogs and kfuncs to pass and receive more than 5 arguments.

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 v5:
- Add RV_EXTRA_STK_ARGS macro to represent the exceeding stack args passed in riscv arg regs. (BPF CI)
- Optimize emit_stack_arg_st() by materializing immediates directly into destination registers. (BPF CI)
- Update stale comments. (BPF CI and sashiko)

Changes in v4:
- Remap BPF_REG_0 and RV_REG_TCC to align the calling convention of bpf2bpf and kfunc calls with the riscv abi.
- Adapt the bpf stack arguments implementation based on change 1.

Changes in v3:
- Drop the exceptions.c guard change: the guarded programs use
bpf_throw(), unsupported by the RV64 JIT, so the test is denylisted
on riscv64 and the guard has no effect. (BPF CI)
- Link to v2: https://lore.kernel.org/r/[email protected]

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]

Quoted 6 lines

Feng Jiang (2):
riscv, bpf: Add BPF stack arguments support for RV64 JIT selftests/bpf: Enable stack argument tests for RV64

Pu Lehui (1):
riscv, bpf: Remap BPF_REG_0 and RV_REG_TCC

Thanks!

For the series:

#9 lore raw
In reply to Pu Lehui
This series was applied to bpf/bpf-next.git (master) by Kumar Kartikeya Dwivedi <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Kumar Kartikeya Dwivedi <[email protected]>:

Quoted 9 lines · On Sat, 5 Sep 2026 08:58:54 +0000 you wrote:

Implement bpf_jit_supports_stack_args() on RV64 JIT to allow bpf subprogs and kfuncs to pass and receive more than 5 arguments.

Tested on riscv64 QEMU with LLVM main [1]; BPF-to-BPF and kfunc selftests pass.

[1] https://github.com/llvm/llvm-project/pull/189060

[...]

Here is the summary with links:
- [bpf-next,v5,1/3] riscv, bpf: Remap BPF_REG_0 and RV_REG_TCC

    https://git.kernel.org/bpf/bpf-next/c/f80a09c486e7
  - [bpf-next,v5,2/3] riscv, bpf: Add BPF stack arguments support for RV64 JIT
    https://git.kernel.org/bpf/bpf-next/c/ba754185f902
  - [bpf-next,v5,3/3] selftests/bpf: Enable stack argument tests for RV64
    https://git.kernel.org/bpf/bpf-next/c/7687f1cbbaf8

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html