BPF List
 help / color / mirror / Atom feed
* [PATCH] bpf: reduce kernel stack usage in bpf_guess_first_trips()
@ 2026-11-24 14:01 Arnd Bergmann
  2026-11-24 14:09 ` sashiko-bot
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Arnd Bergmann @ 2026-11-24 14:01 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi
  Cc: Arnd Bergmann, Martin KaFai Lau, Song Liu, Yonghong Song,
	Jiri Olsa, Emil Tsalapatis, Ihor Solodrai, bpf

From: Arnd Bergmann <arnd@arndb.de>

The large 'bpf_verifier_state' structure causes the newly added
bpf_guess_first_trips() function to have one of the largest stack frames
in the kernel. With CONFIG_KASAN_STACK this exceeds the warning limit
I have set on arm64:

kernel/bpf/equation_join.c: In function 'bpf_guess_first_trips':
kernel/bpf/equation_join.c:1502:1: error: the frame size of 1552 bytes is larger than 1536 bytes [-Werror=frame-larger-than=]

There are already some dynamic allocations in this code path, so
do the same here and use kzalloc_obj() as well here.

Fixes: 97f92b0c1df6 ("bpf: Check the body of a counted loop once")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 kernel/bpf/equation_join.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/kernel/bpf/equation_join.c b/kernel/bpf/equation_join.c
index d7ad7fb52ed6..4123a8448237 100644
--- a/kernel/bpf/equation_join.c
+++ b/kernel/bpf/equation_join.c
@@ -1455,7 +1455,7 @@ int bpf_guess_first_trips(struct bpf_verifier_env *env, struct bpf_verifier_stat
 			  struct bpf_loop *loop)
 {
 	struct loop_stack_entry *e = bpf_top_loop(st->frame[st->curframe]);
-	struct bpf_verifier_state next = {};
+	struct bpf_verifier_state *next;
 	struct bpf_loop_steps *steps;
 	u64 trips = U64_MAX, xreg;
 	struct scalar *s;
@@ -1488,15 +1488,18 @@ int bpf_guess_first_trips(struct bpf_verifier_env *env, struct bpf_verifier_stat
 		return 0;
 	e->exit_threshold = e->trip_index.r64.base + trips;
 	add_iv_threshold(env, loop, e->exit_threshold, 1, THRESHOLD_EXIT_JUMP, U32_MAX);
+	next = kzalloc_obj(*next);
+	if (!next)
+		return -ENOMEM;
 	for (i = 0; i < min(trips, 2) && !err; i++) {
-		err = bpf_copy_verifier_state(env, &next, st);
-		if (err || !move_by_steps(env, &next, steps))
+		err = bpf_copy_verifier_state(env, next, st);
+		if (err || !move_by_steps(env, next, steps))
 			break;
-		err = bpf_states_join(env, st, &next, loop, true);
+		err = bpf_states_join(env, st, next, loop, true);
 		env->extra_values = true;
 		bitmap_or(loop->nonlinear_xregs, loop->nonlinear_xregs, steps->nonlinear_xregs,
 			  MAX_BPF_XREG);
 	}
-	bpf_free_verifier_state(env, &next, false);
+	bpf_free_verifier_state(env, next, true);
 	return err ?: i > 0;
 }
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] bpf: reduce kernel stack usage in bpf_guess_first_trips()
  2026-11-24 14:01 [PATCH] bpf: reduce kernel stack usage in bpf_guess_first_trips() Arnd Bergmann
@ 2026-11-24 14:09 ` sashiko-bot
  2026-11-24 19:51 ` Alexei Starovoitov
  2026-11-24 20:00 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-24 14:09 UTC (permalink / raw)
  To: Arnd Bergmann; +Cc: bpf

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] bpf: missing BPF_VERIFIER_GFP in bpf_guess_first_trips()
  allocation
--

commit 166dd425d776a0ec6b6a3e502aed3e0810e29438
Author: Arnd Bergmann <arnd@arndb.de>

bpf: reduce kernel stack usage in bpf_guess_first_trips()

This commit moves the temporary bpf_verifier_state in
bpf_guess_first_trips() from the stack to the heap to reduce stack frame
size.

> diff --git a/kernel/bpf/equation_join.c b/kernel/bpf/equation_join.c
> index d7ad7fb52ed6c..4123a84482372 100644
> --- a/kernel/bpf/equation_join.c
> +++ b/kernel/bpf/equation_join.c
> @@ -1455,7 +1455,7 @@ int bpf_guess_first_trips(struct bpf_verifier_env *env, struct bpf_verifier_stat
>  			  struct bpf_loop *loop)
>  {
>  	struct loop_stack_entry *e = bpf_top_loop(st->frame[st->curframe]);
> -	struct bpf_verifier_state next = {};
> +	struct bpf_verifier_state *next;
>  	struct bpf_loop_steps *steps;
>  	u64 trips = U64_MAX, xreg;
>  	struct scalar *s;
> @@ -1488,15 +1488,18 @@ int bpf_guess_first_trips(struct bpf_verifier_env *env, struct bpf_verifier_stat
>  		return 0;
>  	e->exit_threshold = e->trip_index.r64.base + trips;
>  	add_iv_threshold(env, loop, e->exit_threshold, 1, THRESHOLD_EXIT_JUMP, U32_MAX);
> +	next = kzalloc_obj(*next);
> +	if (!next)
> +		return -ENOMEM;

[Severity: Low]
Should kzalloc_obj() pass BPF_VERIFIER_GFP here, like the other verifier
allocations in find_steps() and bpf_copy_verifier_state()?

Without BPF_VERIFIER_GFP (GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL |
__GFP_NOWARN), this allocation is not charged to the caller's memcg and can
emit page allocation warnings or invoke the OOM killer under memory
pressure instead of failing the program load cleanly.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124140155.1555535-1-arnd@kernel.org?part=1

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] bpf: reduce kernel stack usage in bpf_guess_first_trips()
  2026-11-24 14:01 [PATCH] bpf: reduce kernel stack usage in bpf_guess_first_trips() Arnd Bergmann
  2026-11-24 14:09 ` sashiko-bot
@ 2026-11-24 19:51 ` Alexei Starovoitov
  2026-11-24 20:00 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Alexei Starovoitov @ 2026-11-24 19:51 UTC (permalink / raw)
  To: Arnd Bergmann, Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman,
	Kumar Kartikeya Dwivedi
  Cc: Arnd Bergmann, Martin KaFai Lau, Song Liu, Yonghong Song,
	Jiri Olsa, Emil Tsalapatis, Ihor Solodrai, bpf

On Tue, Nov 24, 2026 at 06:01 AM Arnd Bergmann <arnd@kernel.org> wrote:
> +	next = kzalloc_obj(*next);
> +	if (!next)
> +		return -ENOMEM;

Every allocation in the verifier uses BPF_VERIFIER_GFP.
I changed it to
	next = kzalloc_obj(*next, BPF_VERIFIER_GFP);
while applying.

Applied to bpf-next. Thanks for the fix!

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] bpf: reduce kernel stack usage in bpf_guess_first_trips()
  2026-11-24 14:01 [PATCH] bpf: reduce kernel stack usage in bpf_guess_first_trips() Arnd Bergmann
  2026-11-24 14:09 ` sashiko-bot
  2026-11-24 19:51 ` Alexei Starovoitov
@ 2026-11-24 20:00 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-11-24 20:00 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: ast, daniel, andrii, eddyz87, memxor, arnd, martin.lau, song,
	yonghong.song, jolsa, emil, ihor.solodrai, bpf

Hello:

This patch was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:

On Tue, 24 Nov 2026 15:01:31 +0100 you wrote:
> From: Arnd Bergmann <arnd@arndb.de>
> 
> The large 'bpf_verifier_state' structure causes the newly added
> bpf_guess_first_trips() function to have one of the largest stack frames
> in the kernel. With CONFIG_KASAN_STACK this exceeds the warning limit
> I have set on arm64:
> 
> [...]

Here is the summary with links:
  - bpf: reduce kernel stack usage in bpf_guess_first_trips()
    https://git.kernel.org/bpf/bpf-next/c/a58375b858fa

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-24 20:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 14:01 [PATCH] bpf: reduce kernel stack usage in bpf_guess_first_trips() Arnd Bergmann
2026-11-24 14:09 ` sashiko-bot
2026-11-24 19:51 ` Alexei Starovoitov
2026-11-24 20:00 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox