Skip to contentSkip to conversation

bpf, arm64: Stack argument fixes

PATCH Applied Puranjay Mohan 4 participants 6 messages May 28 – Jun 1

Subsystems (6)
Kselftest BPF arm64 Arm STM32 +2 more subsystems
lore
Share & tools
lore

Puranjay Mohan

author maintainer
#0 lore raw

cover bpf, arm64: Stack argument fixes

Patch 1 fixes a redundant MOV in the arm64 JIT's emit_stack_arg_store_imm() and clarifies the stack layout comments. This is not a bug fix but an improvement.

Patch 2 bumps the stack argument tests from 6-8 args to at least 10 so they actually exercise the native stack on arm64, where x0-x7 cover the first 8 arguments.

Puranjay Mohan (2):
bpf, arm64: Fix redundant MOV and clarify stack arg comments selftests/bpf: Use at least 10 args in stack argument tests

 arch/arm64/net/bpf_jit_comp.c                 | 12 +--
 .../selftests/bpf/prog_tests/stack_arg.c      | 30 +++----
 tools/testing/selftests/bpf/progs/stack_arg.c | 90 +++++++++++--------
 .../selftests/bpf/progs/stack_arg_kfunc.c     | 24 ++---
 .../selftests/bpf/test_kmods/bpf_testmod.c    | 25 ++++--
 .../bpf/test_kmods/bpf_testmod_kfunc.h        | 11 ++-
 6 files changed, 115 insertions(+), 77 deletions(-)

base-commit: e42e53ae23b7d41df22ccd7788192bf578f24da2

Signature · 1 line
2.53.0-Meta
author maintainer
#1 lore raw

PATCH 1/2 bpf, arm64: Fix redundant MOV and clarify stack arg comments

emit_stack_arg_store_imm() materializes the immediate into tmp and then moves tmp to the target register (x5-x7). Emit the immediate directly into the target register to avoid the redundant MOV.

While here, qualify the bare "FP" in the stack-layout ASCII art as "A64_FP" so it is not confused with BPF_FP, and note that incoming stack arguments sit above the FP/LR pair pushed by the callee prologue.

---
 arch/arm64/net/bpf_jit_comp.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
arch/arm64/net/bpf_jit_comp.c +7 -5
index e3bbeaa94590..b4abc3138f37 100644
@@ -546,7 +546,8 @@ static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
 	 *                          low
 	 *
 	 * Stack args 6-8 are passed in x5-x7, args 9+ at [SP].
-	 * Incoming args 9+ are at [FP + 16], [FP + 24], ...
+	 * Incoming args 9+ are at [A64_FP + 16], [A64_FP + 24], ...
+	 * (above the saved FP/LR pair pushed in the callee prologue).
 	 */
 
 	emit_kcfi(is_main_prog ? cfi_bpf_hash : cfi_bpf_subprog_hash, ctx);
@@ -1235,11 +1236,12 @@ static void emit_stack_arg_store_imm(s32 imm, s16 bpf_off, const u8 tmp, struct
 {
 	int idx = -bpf_off / sizeof(u64) - 1;
 
-	emit_a64_mov_i(1, tmp, imm, ctx);
-	if (idx < NR_STACK_ARG_REGS)
-		emit(A64_MOV(1, stack_arg_reg[idx], tmp), ctx);
-	else
+	if (idx < NR_STACK_ARG_REGS) {
+		emit_a64_mov_i(1, stack_arg_reg[idx], imm, ctx);
+	} else {
+		emit_a64_mov_i(1, tmp, imm, ctx);
 		emit(A64_STR64I(tmp, A64_SP, (idx - NR_STACK_ARG_REGS) * sizeof(u64)), ctx);
+	}
 }
 
 /* JITs an eBPF instruction.
Signature · 1 line
2.53.0-Meta
author maintainer
#2 lore raw

PATCH 2/2 selftests/bpf: Use at least 10 args in stack argument tests

In reply to Puranjay Mohan

On arm64, the first 8 arguments are passed in registers (x0-x7), so tests with 8 or fewer arguments never exercise the native stack argument path in the JIT. Increase argument counts to at least 10 across all BPF-to-BPF subprog and kfunc stack argument tests so that at least 2 arguments land on the arm64 stack.

For the two-callees test, bump foo1 from 8 to 10 and foo2 from 10 to 12 args to preserve the different-stack-depth flavor of the test.

The bpf_kfunc_call_stack_arg_mem kfunc is left unchanged at 7 args to avoid breaking the precision backtracking test which relies on hardcoded verifier log instruction indices.

---
 .../selftests/bpf/prog_tests/stack_arg.c      | 30 +++----
 tools/testing/selftests/bpf/progs/stack_arg.c | 90 +++++++++++--------
 .../selftests/bpf/progs/stack_arg_kfunc.c     | 24 ++---
 .../selftests/bpf/test_kmods/bpf_testmod.c    | 25 ++++--
 .../bpf/test_kmods/bpf_testmod_kfunc.h        | 11 ++-
 5 files changed, 108 insertions(+), 72 deletions(-)
tools/testing/selftests/bpf/prog_tests/stack_arg.c +15 -15
index d61bac33f809..57193543f260 100644
@@ -37,7 +37,7 @@ static void test_global_many(void)
 	if (!ASSERT_OK(stack_arg__load(skel), "load"))
 		goto out;
 
-	run_subtest(skel->progs.test_global_many_args, 36);
+	run_subtest(skel->progs.test_global_many_args, 55);
 
 out:
 	stack_arg__destroy(skel);
@@ -62,10 +62,10 @@ static void test_async_cb_many(void)
 	run_subtest(skel->progs.test_async_cb_many_args, 0);
 
 	/* Wait for the timer callback to fire and verify the result.
-	 * 10+20+30+40+50+60+70+80 = 360
+	 * 10+20+30+40+50+60+70+80+90+100 = 550
 	 */
 	usleep(50);
-	ASSERT_EQ(skel->bss->timer_result, 360, "timer_result");
+	ASSERT_EQ(skel->bss->timer_result, 550, "timer_result");
 
 out:
 	stack_arg__destroy(skel);
@@ -87,11 +87,11 @@ static void test_bpf2bpf(void)
 	if (!ASSERT_OK(stack_arg__load(skel), "load"))
 		goto out;
 
-	run_subtest(skel->progs.test_bpf2bpf_ptr_stack_arg, 45);
-	run_subtest(skel->progs.test_bpf2bpf_mix_stack_args, 51);
-	run_subtest(skel->progs.test_bpf2bpf_nesting_stack_arg, 50);
-	run_subtest(skel->progs.test_bpf2bpf_dynptr_stack_arg, 69);
-	run_subtest(skel->progs.test_two_callees, 91);
+	run_subtest(skel->progs.test_bpf2bpf_ptr_stack_arg, 75);
+	run_subtest(skel->progs.test_bpf2bpf_mix_stack_args, 66);
+	run_subtest(skel->progs.test_bpf2bpf_nesting_stack_arg, 84);
+	run_subtest(skel->progs.test_bpf2bpf_dynptr_stack_arg, 99);
+	run_subtest(skel->progs.test_two_callees, 133);
 
 out:
 	stack_arg__destroy(skel);
@@ -113,14 +113,14 @@ static void test_kfunc(void)
 	if (!ASSERT_OK(stack_arg_kfunc__load(skel), "load"))
 		goto out;
 
-	run_subtest(skel->progs.test_stack_arg_scalar, 36);
-	run_subtest(skel->progs.test_stack_arg_ptr, 45);
-	run_subtest(skel->progs.test_stack_arg_mix, 51);
-	run_subtest(skel->progs.test_stack_arg_dynptr, 69);
+	run_subtest(skel->progs.test_stack_arg_scalar, 55);
+	run_subtest(skel->progs.test_stack_arg_ptr, 75);
+	run_subtest(skel->progs.test_stack_arg_mix, 66);
+	run_subtest(skel->progs.test_stack_arg_dynptr, 99);
 	run_subtest(skel->progs.test_stack_arg_mem, 151);
-	run_subtest(skel->progs.test_stack_arg_iter, 115);
-	run_subtest(skel->progs.test_stack_arg_const_str, 15);
-	run_subtest(skel->progs.test_stack_arg_timer, 15);
+	run_subtest(skel->progs.test_stack_arg_iter, 145);
+	run_subtest(skel->progs.test_stack_arg_const_str, 45);
+	run_subtest(skel->progs.test_stack_arg_timer, 45);
 
 out:
 	stack_arg_kfunc__destroy(skel);
tools/testing/selftests/bpf/progs/stack_arg.c +55 -35

191 lines not on this page · raw message

tools/testing/selftests/bpf/progs/stack_arg_kfunc.c +13 -11
index da0d4f91d273..345f2da2e361 100644
@@ -33,7 +33,7 @@ struct {
 SEC("tc")
 int test_stack_arg_scalar(struct __sk_buff *skb)
 {
-	return bpf_kfunc_call_stack_arg(1, 2, 3, 4, 5, 6, 7, 8);
+	return bpf_kfunc_call_stack_arg(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
 }
 
 SEC("tc")
@@ -41,7 +41,7 @@ int test_stack_arg_ptr(struct __sk_buff *skb)
 {
 	struct prog_test_pass1 p = { .x0 = 10, .x1 = 20 };
 
-	return bpf_kfunc_call_stack_arg_ptr(1, 2, 3, 4, 5, &p);
+	return bpf_kfunc_call_stack_arg_ptr(1, 2, 3, 4, 5, 6, 7, 8, 9, &p);
 }
 
 SEC("tc")
@@ -50,17 +50,17 @@ int test_stack_arg_mix(struct __sk_buff *skb)
 	struct prog_test_pass1 p = { .x0 = 10 };
 	struct prog_test_pass1 q = { .x1 = 20 };
 
-	return bpf_kfunc_call_stack_arg_mix(1, 2, 3, 4, 5, &p, 6, &q);
+	return bpf_kfunc_call_stack_arg_mix(1, 2, 3, 4, 5, 6, 7, &p, 8, &q);
 }
 
-/* 1 + 2 + 3 + 4 + 5 + sizeof(pkt_v4) = 15 + 54 = 69 */
+/* 1+2+3+4+5+6+7+8+9+sizeof(pkt_v4) = 45+54 = 99 */
 SEC("tc")
 int test_stack_arg_dynptr(struct __sk_buff *skb)
 {
 	struct bpf_dynptr ptr;
 
 	bpf_dynptr_from_skb(skb, 0, &ptr);
-	return bpf_kfunc_call_stack_arg_dynptr(1, 2, 3, 4, 5, &ptr);
+	return bpf_kfunc_call_stack_arg_dynptr(1, 2, 3, 4, 5, 6, 7, 8, 9, &ptr);
 }
 
 /* 1 + 2 + 3 + 4 + 5 + (1 + 2 + ... + 16) = 15 + 136 = 151 */
@@ -72,7 +72,7 @@ int test_stack_arg_mem(struct __sk_buff *skb)
 	return bpf_kfunc_call_stack_arg_mem(1, 2, 3, 4, 5, buf, sizeof(buf));
 }
 
-/* 1 + 2 + 3 + 4 + 5 + 100 = 115 */
+/* 1+2+3+4+5+6+7+8+9+100 = 145 */
 SEC("tc")
 int test_stack_arg_iter(struct __sk_buff *skb)
 {
@@ -80,21 +80,22 @@ int test_stack_arg_iter(struct __sk_buff *skb)
 	u64 ret;
 
 	bpf_iter_testmod_seq_new(&it, 100, 10);
-	ret = bpf_kfunc_call_stack_arg_iter(1, 2, 3, 4, 5, &it);
+	ret = bpf_kfunc_call_stack_arg_iter(1, 2, 3, 4, 5, 6, 7, 8, 9, &it);
 	bpf_iter_testmod_seq_destroy(&it);
 	return ret;
 }
 
 const char cstr[] = "hello";
 
-/* 1 + 2 + 3 + 4 + 5 = 15 */
+/* 1+2+3+4+5+6+7+8+9 = 45 */
 SEC("tc")
 int test_stack_arg_const_str(struct __sk_buff *skb)
 {
-	return bpf_kfunc_call_stack_arg_const_str(1, 2, 3, 4, 5, cstr);
+	return bpf_kfunc_call_stack_arg_const_str(1, 2, 3, 4, 5, 6, 7, 8, 9,
+						  cstr);
 }
 
-/* 1 + 2 + 3 + 4 + 5 = 15 */
+/* 1+2+3+4+5+6+7+8+9 = 45 */
 SEC("tc")
 int test_stack_arg_timer(struct __sk_buff *skb)
 {
@@ -104,7 +105,8 @@ int test_stack_arg_timer(struct __sk_buff *skb)
 	val = bpf_map_lookup_elem(&kfunc_timer_map, &key);
 	if (!val)
 		return 0;
-	return bpf_kfunc_call_stack_arg_timer(1, 2, 3, 4, 5, &val->timer);
+	return bpf_kfunc_call_stack_arg_timer(1, 2, 3, 4, 5, 6, 7, 8, 9,
+					      &val->timer);
 }
 
 #else
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +16 -9
index 0be918fe3021..30f1cd23093c 100644
@@ -826,30 +826,34 @@ __bpf_kfunc int bpf_kfunc_call_test5(u8 a, u16 b, u32 c)
 }
 
 __bpf_kfunc u64 bpf_kfunc_call_stack_arg(u64 a, u64 b, u64 c, u64 d,
-					 u64 e, u64 f, u64 g, u64 h)
+					 u64 e, u64 f, u64 g, u64 h,
+					 u64 i, u64 j)
 {
-	return a + b + c + d + e + f + g + h;
+	return a + b + c + d + e + f + g + h + i + j;
 }
 
 __bpf_kfunc u64 bpf_kfunc_call_stack_arg_ptr(u64 a, u64 b, u64 c, u64 d, u64 e,
+					     u64 f, u64 g, u64 h, u64 i,
 					     struct prog_test_pass1 *p)
 {
-	return a + b + c + d + e + p->x0 + p->x1;
+	return a + b + c + d + e + f + g + h + i + p->x0 + p->x1;
 }
 
 __bpf_kfunc u64 bpf_kfunc_call_stack_arg_mix(u64 a, u64 b, u64 c, u64 d, u64 e,
-					     struct prog_test_pass1 *p, u64 f,
+					     u64 f, u64 g,
+					     struct prog_test_pass1 *p, u64 h,
 					     struct prog_test_pass1 *q)
 {
-	return a + b + c + d + e + p->x0 + f + q->x1;
+	return a + b + c + d + e + f + g + p->x0 + h + q->x1;
 }
 
 __bpf_kfunc u64 bpf_kfunc_call_stack_arg_dynptr(u64 a, u64 b, u64 c, u64 d, u64 e,
+					       u64 f, u64 g, u64 h, u64 i,
 					       struct bpf_dynptr *ptr)
 {
 	const struct bpf_dynptr_kern *kern_ptr = (void *)ptr;
 
-	return a + b + c + d + e + (kern_ptr->size & 0xFFFFFF);
+	return a + b + c + d + e + f + g + h + i + (kern_ptr->size & 0xFFFFFF);
 }
 
 __bpf_kfunc u64 bpf_kfunc_call_stack_arg_mem(u64 a, u64 b, u64 c, u64 d, u64 e,
@@ -865,21 +869,24 @@ __bpf_kfunc u64 bpf_kfunc_call_stack_arg_mem(u64 a, u64 b, u64 c, u64 d, u64 e,
 }
 
 __bpf_kfunc u64 bpf_kfunc_call_stack_arg_iter(u64 a, u64 b, u64 c, u64 d, u64 e,
+					      u64 f, u64 g, u64 h, u64 i,
 					      struct bpf_iter_testmod_seq *it__iter)
 {
-	return a + b + c + d + e + it__iter->value;
+	return a + b + c + d + e + f + g + h + i + it__iter->value;
 }
 
 __bpf_kfunc u64 bpf_kfunc_call_stack_arg_const_str(u64 a, u64 b, u64 c, u64 d, u64 e,
+						   u64 f, u64 g, u64 h, u64 i,
 						   const char *str__str)
 {
-	return a + b + c + d + e;
+	return a + b + c + d + e + f + g + h + i;
 }
 
 __bpf_kfunc u64 bpf_kfunc_call_stack_arg_timer(u64 a, u64 b, u64 c, u64 d, u64 e,
+					       u64 f, u64 g, u64 h, u64 i,
 					       struct bpf_timer *timer)
 {
-	return a + b + c + d + e;
+	return a + b + c + d + e + f + g + h + i;
 }
 
 __bpf_kfunc u64 bpf_kfunc_call_stack_arg_big(u64 a, u64 b, u64 c, u64 d, u64 e,
tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h +9 -2
index 2edc36b66de9..c36bb911defa 100644
@@ -119,21 +119,28 @@ struct sock *bpf_kfunc_call_test3(struct sock *sk) __ksym;
 long bpf_kfunc_call_test4(signed char a, short b, int c, long d) __ksym;
 int bpf_kfunc_call_test5(__u8 a, __u16 b, __u32 c) __ksym;
 __u64 bpf_kfunc_call_stack_arg(__u64 a, __u64 b, __u64 c, __u64 d,
-			       __u64 e, __u64 f, __u64 g, __u64 h) __ksym;
+			       __u64 e, __u64 f, __u64 g, __u64 h,
+			       __u64 i, __u64 j) __ksym;
 __u64 bpf_kfunc_call_stack_arg_ptr(__u64 a, __u64 b, __u64 c, __u64 d, __u64 e,
+				   __u64 f, __u64 g, __u64 h, __u64 i,
 				   struct prog_test_pass1 *p) __ksym;
 __u64 bpf_kfunc_call_stack_arg_mix(__u64 a, __u64 b, __u64 c, __u64 d, __u64 e,
-				   struct prog_test_pass1 *p, __u64 f,
+				   __u64 f, __u64 g,
+				   struct prog_test_pass1 *p, __u64 h,
 				   struct prog_test_pass1 *q) __ksym;
 __u64 bpf_kfunc_call_stack_arg_dynptr(__u64 a, __u64 b, __u64 c, __u64 d, __u64 e,
+				      __u64 f, __u64 g, __u64 h, __u64 i,
 				      struct bpf_dynptr *ptr) __ksym;
 __u64 bpf_kfunc_call_stack_arg_mem(__u64 a, __u64 b, __u64 c, __u64 d, __u64 e,
 				   void *mem, int mem__sz) __ksym;
 __u64 bpf_kfunc_call_stack_arg_iter(__u64 a, __u64 b, __u64 c, __u64 d, __u64 e,
+				    __u64 f, __u64 g, __u64 h, __u64 i,
 				    struct bpf_iter_testmod_seq *it__iter) __ksym;
 __u64 bpf_kfunc_call_stack_arg_const_str(__u64 a, __u64 b, __u64 c, __u64 d, __u64 e,
+					 __u64 f, __u64 g, __u64 h, __u64 i,
 					 const char *str__str) __ksym;
 __u64 bpf_kfunc_call_stack_arg_timer(__u64 a, __u64 b, __u64 c, __u64 d, __u64 e,
+				     __u64 f, __u64 g, __u64 h, __u64 i,
 				     struct bpf_timer *timer) __ksym;
 __u64 bpf_kfunc_call_stack_arg_big(__u64 a, __u64 b, __u64 c, __u64 d, __u64 e,
 				   struct prog_test_big_arg s) __ksym;
Signature · 1 line
2.53.0-Meta
reviewer Acked-by on v1 2/2
#3 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: Use at least 10 args in stack argument tests

Quoted 9 lines · Puranjay Mohan

On arm64, the first 8 arguments are passed in registers (x0-x7), so tests with 8 or fewer arguments never exercise the native stack argument path in the JIT. Increase argument counts to at least 10 across all BPF-to-BPF subprog and kfunc stack argument tests so that at least 2 arguments land on the arm64 stack.

For the two-callees test, bump foo1 from 8 to 10 and foo2 from 10 to 12 args to preserve the different-stack-depth flavor of the test.

Quoted 6 lines

The bpf_kfunc_call_stack_arg_mem kfunc is left unchanged at 7 args to avoid breaking the precision backtracking test which relies on hardcoded verifier log instruction indices.

#4 lore raw
In reply to Puranjay Mohan
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

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

Quoted 9 lines · On Thu, 28 May 2026 09:17:46 -0700 you wrote:

Patch 1 fixes a redundant MOV in the arm64 JIT's emit_stack_arg_store_imm() and clarifies the stack layout comments. This is not a bug fix but an improvement.

Patch 2 bumps the stack argument tests from 6-8 args to at least 10 so they actually exercise the native stack on arm64, where x0-x7 cover the first 8 arguments.

[...]

Here is the summary with links:
- [bpf-next,1/2] bpf, arm64: Fix redundant MOV and clarify stack arg comments

    https://git.kernel.org/bpf/bpf-next/c/12a585e607fa
  - [bpf-next,2/2] selftests/bpf: Use at least 10 args in stack argument tests
    https://git.kernel.org/bpf/bpf-next/c/157317ba662a

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
maintainer
#5 lore raw
Quoted 14 lines · On Mon, Jun 01, 2026 at 12:50:19AM +0000, [email protected]…

Hello:

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

Quoted 8 lines · On Thu, 28 May 2026 09:17:46 -0700 you wrote:

Patch 1 fixes a redundant MOV in the arm64 JIT's emit_stack_arg_store_imm() and clarifies the stack layout comments. This is not a bug fix but an improvement.

Patch 2 bumps the stack argument tests from 6-8 args to at least 10 so they actually exercise the native stack on arm64, where x0-x7 cover the first 8 arguments.

Quoted 7 lines
Quoted 1 line

[...]

Here is the summary with links:
- [bpf-next,1/2] bpf, arm64: Fix redundant MOV and clarify stack arg comments

    https://git.kernel.org/bpf/bpf-next/c/12a585e607fa
  - [bpf-next,2/2] selftests/bpf: Use at least 10 args in stack argument tests
    https://git.kernel.org/bpf/bpf-next/c/157317ba662a

Thanks, Puranjay!

Will