public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Puranjay Mohan <puranjay@kernel.org>
To: Yonghong Song <yonghong.song@linux.dev>, bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	"Jose E . Marchesi" <jose.marchesi@oracle.com>,
	kernel-team@fb.com, Martin KaFai Lau <martin.lau@kernel.org>,
	Puranjay Mohan <puranjay12@gmail.com>
Subject: Re: [PATCH bpf-next v5 00/16] bpf: Support stack arguments for BPF functions and kfuncs
Date: Sat, 18 Apr 2026 17:39:19 +0100	[thread overview]
Message-ID: <m2v7do1avc.fsf@kernel.org> (raw)
In-Reply-To: <20260417034658.2625353-1-yonghong.song@linux.dev>

Yonghong Song <yonghong.song@linux.dev> writes:

> Currently, bpf function calls and kfunc's are limited by 5 reg-level
> parameters. For function calls with more than 5 parameters,
> developers can use always inlining or pass a struct pointer
> after packing more parameters in that struct. But there is
> no workaround for kfunc if more than 5 parameters is needed.
>
> This patch set lifts the 5-argument limit by introducing stack-based
> argument passing for BPF functions and kfunc's, coordinated with
> compiler support in LLVM [1]. The compiler emits loads/stores through
> a new bpf register r11 (BPF_REG_PARAMS) to pass arguments beyond
> the 5th, keeping the stack arg area separate from the r10-based program
> stack. The maximum number of arguments is capped at MAX_BPF_FUNC_ARGS
> (12), which is sufficient for the vast majority of use cases.
>
> In verifier, r11 based stores can survive bpf-to-bpf and kfunc
> calls. For example
>       *(u64 *)(r11 - 8) = r6;
>       *(u64 *)(r11 - 16) = r7;
>       call bar1;                // arg6 = r6, arg7 = r7
>       call bar2;                // reuses same arg6, arg7 without re-storing

Argument registers are caller saved, that means if the x86 JIT will use
R9 for the arg6 and call bar1, it can clobber it (by calling a helper or
another bpf-to-bpf call) and then bar2 will receive clobbered value for
arg6 because it did not reload it.

This only works for caller saved registers like arg5 (R8 on x86-64) (R5
on bpf) because R5 (BPF) is caller saved and compiler will make sure to
save R5 before the call. But for stacked arguments, compiler models them
as stack memory and if the JIT models them as a register to follow
calling convention, they need to be saved/reloaded before calls. 

Let me explain with an example, if you apply this patch to the selftests:

-- >8 --

From 52924eb28056fe8a2321b1cadf9409f6ca90603d Mon Sep 17 00:00:00 2001
From: Puranjay Mohan <puranjay@kernel.org>
Date: Sat, 18 Apr 2026 09:11:06 -0700
Subject: [PATCH] selftests/bpf: Add test for stack arg register clobbering

Add verifier test stack_arg_reuse_across_inner_call which uses inline
asm to store stack args, call a subprog that internally calls another
subprog with different stack arg values (100, 200 instead of 6, 7),
then reuse the original stores for a second call without re-storing.

This exposes JIT bugs where stack arg registers (x5-x7 on arm64, R9
on x86) are clobbered by the inner call but not refreshed before
the second call.

Correct result: 315 + 28 = 343.
Clobbered result: 315 + 315 = 630. (on arm64)

Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
---
 .../selftests/bpf/progs/verifier_stack_arg.c  | 56 +++++++++++++++++++
 1 file changed, 56 insertions(+)

diff --git a/tools/testing/selftests/bpf/progs/verifier_stack_arg.c b/tools/testing/selftests/bpf/progs/verifier_stack_arg.c
index 41ce950ea40e..0c6b433404e8 100644
--- a/tools/testing/selftests/bpf/progs/verifier_stack_arg.c
+++ b/tools/testing/selftests/bpf/progs/verifier_stack_arg.c
@@ -462,6 +462,62 @@ __naked void stack_arg_sequential_calls(void)
        );
 }

+/*
+ * subprog_7args_inner_call takes 7 args like subprog_7args, but
+ * internally calls subprog_7args with different stack arg values
+ * (100, 200).  This clobbers whatever the JIT placed in the stack
+ * arg registers (x5-x7 on arm64, R9 on x86).
+ */
+__noinline __used
+static int subprog_7args_with_inner_call(int a, int b, int c, int d,
+                                        int e, int f, int g)
+{
+       return subprog_7args(a, b, c, d, e, 100, 200);
+}
+
+SEC("tc")
+__description("stack_arg: store reuse across inner call")
+__success __retval(343)
+__arch_x86_64
+__arch_arm64
+__naked void stack_arg_reuse_across_inner_call(void)
+{
+       asm volatile (
+               /* call1: subprog_7args_inner_call(1,2,3,4,5,6,7)
+                * internally calls subprog_7args(1,2,3,4,5,100,200)=315
+                * which clobbers stack arg registers with 100, 200.
+                * Returns 315.
+                */
+               "r1 = 1;"
+               "r2 = 2;"
+               "r3 = 3;"
+               "r4 = 4;"
+               "r5 = 5;"
+               "*(u64 *)(r11 - 8) = 6;"
+               "*(u64 *)(r11 - 16) = 7;"
+               "call subprog_7args_with_inner_call;"
+               "r6 = r0;" /* 315 */
+               /* call2: reuse r11 stores from call1 (no re-store).
+                * subprog_7args(1,2,3,4,5,6,7) should return 28.
+                * If clobbered: subprog_7args(1,2,3,4,5,100,200)=315.
+                */
+               "r1 = 1;"
+               "r2 = 2;"
+               "r3 = 3;"
+               "r4 = 4;"
+               "r5 = 5;"
+               /*
+                * the 6th and 7th arg should be 6, 7 as set above.
+                * But the inner call in subprog_7args_inner_call clobbered
+                * them and reusing again without a re-store will be wrong.
+                */
+               "call subprog_7args;" /* should return 28, but will be 315 due to clobber */
+               "r0 += r6;" /* should have 315 + 28 = 343 but will be 315 + 315 = 630 */
+               "exit;"
+               ::: __clobber_all
+       );
+}
+
 #else

 SEC("socket")
--
2.52.0

-- 8< --

If I run this arm64 (I have a patch to add support)

I get:
run_subtest:PASS:obj_open_mem 0 nsec
run_subtest:PASS:unexpected_load_failure 0 nsec
do_prog_test_run:PASS:bpf_prog_test_run 0 nsec
run_subtest:FAIL:1313 Unexpected retval: 630 != 343
#631/14  verifier_stack_arg/stack_arg: store reuse across inner
call:FAIL

And on x86 it will be:
  - Second call gets R9 = 100 (clobbered), [rsp+0] = 7 (survives, because subprog get's own stack slot)
  - subprog_7args(1,2,3,4,5, 100, 7) = 122 instead of 28
  - Total: 315 + 122 = 437 instead of 343

^^ this is not tested just a theoretical looking at x86 JIT code added
in this patchset that used R9 for sixth argument.

So, I think the fix it to make the JITs keep the 6th arg (x86) and
(6,7,8)th arg (arm64) on the stack and re-load them before every call.
Or the compiler should treat all r11 stack slots like caller saved stack
and reload them.

Please let me know if my analysis is correct or maybe my arm64 jit
implementation is broken.

Thanks,
Puranjay

  parent reply	other threads:[~2026-04-18 16:39 UTC|newest]

Thread overview: 73+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-17  3:46 [PATCH bpf-next v5 00/16] bpf: Support stack arguments for BPF functions and kfuncs Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 01/16] bpf: Remove unused parameter from check_map_kptr_access() Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 02/16] bpf: Refactor to avoid redundant calculation of bpf_reg_state Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 03/16] bpf: Refactor to handle memory and size together Yonghong Song
2026-04-17  4:49   ` sashiko-bot
2026-04-18 16:40     ` Yonghong Song
2026-04-18  0:52   ` bot+bpf-ci
2026-04-18 16:47     ` Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 04/16] bpf: Prepare verifier logs for upcoming kfunc stack arguments Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 05/16] bpf: Introduce bpf register BPF_REG_PARAMS Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 06/16] bpf: Limit the scope of BPF_REG_PARAMS usage Yonghong Song
2026-04-17  4:30   ` bot+bpf-ci
2026-04-18 16:48     ` Yonghong Song
2026-04-17  4:50   ` sashiko-bot
2026-04-18 16:50     ` Yonghong Song
2026-04-18  1:04   ` bot+bpf-ci
2026-04-18 16:54     ` Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 07/16] bpf: Reuse MAX_BPF_FUNC_ARGS for maximum number of arguments Yonghong Song
2026-04-17  4:30   ` bot+bpf-ci
2026-04-18 17:00     ` Yonghong Song
2026-04-18  0:52   ` bot+bpf-ci
2026-04-18 17:03     ` Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 08/16] bpf: Support stack arguments for bpf functions Yonghong Song
2026-04-17  4:35   ` sashiko-bot
2026-04-18 17:10     ` Yonghong Song
2026-04-17  4:43   ` bot+bpf-ci
2026-04-18 17:11     ` Yonghong Song
2026-04-18  1:04   ` bot+bpf-ci
2026-04-17  3:47 ` [PATCH bpf-next v5 09/16] bpf: Reject stack arguments in non-JITed programs Yonghong Song
2026-04-17  4:30   ` bot+bpf-ci
2026-04-18 17:17     ` Yonghong Song
2026-04-18  0:52   ` bot+bpf-ci
2026-04-17  3:47 ` [PATCH bpf-next v5 10/16] bpf: Reject stack arguments if tail call reachable Yonghong Song
2026-04-17  4:08   ` sashiko-bot
2026-04-18 17:18     ` Yonghong Song
2026-04-18 17:37     ` Yonghong Song
2026-04-17  4:30   ` bot+bpf-ci
2026-04-18  1:04   ` bot+bpf-ci
2026-04-18 17:24     ` Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 11/16] bpf: Support stack arguments for kfunc calls Yonghong Song
2026-04-17  4:40   ` sashiko-bot
2026-04-18 17:46     ` Yonghong Song
2026-04-17  4:43   ` bot+bpf-ci
2026-04-18 17:57     ` Yonghong Song
2026-04-18  1:04   ` bot+bpf-ci
2026-04-18 18:04     ` Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 12/16] bpf: Enable stack argument support for x86_64 Yonghong Song
2026-04-17  4:30   ` bot+bpf-ci
2026-04-17  5:03   ` sashiko-bot
2026-04-18 18:07     ` Yonghong Song
2026-04-18  1:04   ` bot+bpf-ci
2026-04-17  3:48 ` [PATCH bpf-next v5 13/16] bpf,x86: Implement JIT support for stack arguments Yonghong Song
2026-04-17  4:44   ` sashiko-bot
2026-04-18 16:43     ` Puranjay Mohan
2026-04-18 18:15     ` Yonghong Song
2026-04-18  1:20   ` bot+bpf-ci
2026-04-18 18:23     ` Yonghong Song
2026-04-17  3:48 ` [PATCH bpf-next v5 14/16] selftests/bpf: Add tests for BPF function " Yonghong Song
2026-04-17  4:20   ` sashiko-bot
2026-04-18  0:52   ` bot+bpf-ci
2026-04-18 18:26     ` Yonghong Song
2026-04-17  3:48 ` [PATCH bpf-next v5 15/16] selftests/bpf: Add negative test for greater-than-8-byte kfunc stack argument Yonghong Song
2026-04-17  4:28   ` sashiko-bot
2026-04-18 18:29     ` Yonghong Song
2026-04-18  0:52   ` bot+bpf-ci
2026-04-17  3:48 ` [PATCH bpf-next v5 16/16] selftests/bpf: Add verifier tests for stack argument validation Yonghong Song
2026-04-17  4:38   ` sashiko-bot
2026-04-18 18:36     ` Yonghong Song
2026-04-18  0:52   ` bot+bpf-ci
2026-04-18 16:39 ` Puranjay Mohan [this message]
2026-04-18 18:47   ` [PATCH bpf-next v5 00/16] bpf: Support stack arguments for BPF functions and kfuncs Alexei Starovoitov
2026-04-18 18:54     ` Yonghong Song
2026-04-18 17:06 ` Puranjay Mohan

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=m2v7do1avc.fsf@kernel.org \
    --to=puranjay@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=jose.marchesi@oracle.com \
    --cc=kernel-team@fb.com \
    --cc=martin.lau@kernel.org \
    --cc=puranjay12@gmail.com \
    --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