Re: [PATCH 1/2] fork: check charging success before zeroing stack
From: Liam R. Howlett
Date: Fri Aug 29 2025 - 11:43:35 EST
* Linus Walleij <linus.walleij@xxxxxxxxxx> [250829 07:44]:
> From: Pasha Tatashin <pasha.tatashin@xxxxxxxxxx>
>
> No need to do zero cached stack if memcg charge fails, so move the
> charging attempt before the memset operation.
>
> Signed-off-by: Pasha Tatashin <pasha.tatashin@xxxxxxxxxx>
> Link: https://lore.kernel.org/20240311164638.2015063-6-pasha.tatashin@xxxxxxxxxx
> Signed-off-by: Linus Walleij <linus.walleij@xxxxxxxxxx>
Reviewed-by: Liam R. Howlett <Liam.Howlett@xxxxxxxxxx>
> ---
> kernel/fork.c | 10 +++++-----
> 1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/kernel/fork.c b/kernel/fork.c
> index af673856499dcaa35e135a9e8042ef28d5c5370d..2a5b7a5fa09b1f3a42473cf44a1316ec8b3b31d0 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -290,6 +290,11 @@ static int alloc_thread_stack_node(struct task_struct *tsk, int node)
> if (!vm_area)
> continue;
>
> + if (memcg_charge_kernel_stack(vm_area)) {
> + vfree(vm_area->addr);
> + return -ENOMEM;
> + }
> +
> /* Reset stack metadata. */
> kasan_unpoison_range(vm_area->addr, THREAD_SIZE);
>
> @@ -298,11 +303,6 @@ static int alloc_thread_stack_node(struct task_struct *tsk, int node)
> /* Clear stale pointers from reused stack. */
> memset(stack, 0, THREAD_SIZE);
>
> - if (memcg_charge_kernel_stack(vm_area)) {
> - vfree(vm_area->addr);
> - return -ENOMEM;
> - }
> -
> tsk->stack_vm_area = vm_area;
> tsk->stack = stack;
> return 0;
>
> --
> 2.50.1
>