BPF List
 help / color / mirror / Atom feed
* [PATCH] bpf: use kvzmalloc to allocate BPF  verifier environment
@ 2024-10-08 21:07 Rik van Riel
  2024-10-08 21:39 ` Shakeel Butt
  2024-10-10  1:20 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 3+ messages in thread
From: Rik van Riel @ 2024-10-08 21:07 UTC (permalink / raw)
  To: bpf
  Cc: linux-kernel, kernel-team, Alexei Starovoitov, Daniel Borkmann,
	John Fastabend, Andrii Nakryiko

The kzmalloc call in bpf_check can fail when memory is very fragmented,
which in turn can lead to an OOM kill.

Use kvzmalloc to fall back to vmalloc when memory is too fragmented to
allocate an order 3 sized bpf verifier environment.

Admittedly this is not a very common case, and only happens on systems
where memory has already been squeezed close to the limit, but this does
not seem like much of a hot path, and it's a simple enough fix.

Signed-off-by: Rik van Riel <riel@surriel.com>
---
 kernel/bpf/verifier.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index dd86282ccaa4..0c41646a8793 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -22298,7 +22298,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3
 	/* 'struct bpf_verifier_env' can be global, but since it's not small,
 	 * allocate/free it every time bpf_check() is called
 	 */
-	env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
+	env = kvzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
 	if (!env)
 		return -ENOMEM;
 
@@ -22534,6 +22534,6 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3
 		mutex_unlock(&bpf_verifier_lock);
 	vfree(env->insn_aux_data);
 err_free_env:
-	kfree(env);
+	kvfree(env);
 	return ret;
 }
-- 
2.45.2


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

* Re: [PATCH] bpf: use kvzmalloc to allocate BPF  verifier environment
  2024-10-08 21:07 [PATCH] bpf: use kvzmalloc to allocate BPF verifier environment Rik van Riel
@ 2024-10-08 21:39 ` Shakeel Butt
  2024-10-10  1:20 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: Shakeel Butt @ 2024-10-08 21:39 UTC (permalink / raw)
  To: Rik van Riel
  Cc: bpf, linux-kernel, kernel-team, Alexei Starovoitov,
	Daniel Borkmann, John Fastabend, Andrii Nakryiko

On Tue, Oct 08, 2024 at 05:07:35PM GMT, Rik van Riel wrote:
> The kzmalloc call in bpf_check can fail when memory is very fragmented,
> which in turn can lead to an OOM kill.
> 
> Use kvzmalloc to fall back to vmalloc when memory is too fragmented to
> allocate an order 3 sized bpf verifier environment.
> 
> Admittedly this is not a very common case, and only happens on systems
> where memory has already been squeezed close to the limit, but this does
> not seem like much of a hot path, and it's a simple enough fix.
> 
> Signed-off-by: Rik van Riel <riel@surriel.com>

It seems like a temporary allocation, so using kvmalloc* seems
reasonable.

Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev>

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

* Re: [PATCH] bpf: use kvzmalloc to allocate BPF  verifier environment
  2024-10-08 21:07 [PATCH] bpf: use kvzmalloc to allocate BPF verifier environment Rik van Riel
  2024-10-08 21:39 ` Shakeel Butt
@ 2024-10-10  1:20 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-10-10  1:20 UTC (permalink / raw)
  To: Rik van Riel
  Cc: bpf, linux-kernel, kernel-team, ast, daniel, john.fastabend,
	andrii

Hello:

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

On Tue, 8 Oct 2024 17:07:35 -0400 you wrote:
> The kzmalloc call in bpf_check can fail when memory is very fragmented,
> which in turn can lead to an OOM kill.
> 
> Use kvzmalloc to fall back to vmalloc when memory is too fragmented to
> allocate an order 3 sized bpf verifier environment.
> 
> Admittedly this is not a very common case, and only happens on systems
> where memory has already been squeezed close to the limit, but this does
> not seem like much of a hot path, and it's a simple enough fix.
> 
> [...]

Here is the summary with links:
  - bpf: use kvzmalloc to allocate BPF verifier environment
    https://git.kernel.org/bpf/bpf/c/434247637c66

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] 3+ messages in thread

end of thread, other threads:[~2024-10-10  1:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-08 21:07 [PATCH] bpf: use kvzmalloc to allocate BPF verifier environment Rik van Riel
2024-10-08 21:39 ` Shakeel Butt
2024-10-10  1:20 ` 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