]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
fork: check charging success before zeroing stack
authorPasha Tatashin <pasha.tatashin@soleen.com>
Fri, 9 May 2025 06:29:28 +0000 (08:29 +0200)
committerAndrew Morton <akpm@linux-foundation.org>
Wed, 21 May 2025 17:48:23 +0000 (10:48 -0700)
No need to do zero cached stack if memcg charge fails, so move the
charging attempt before the memset operation.

[linus.walleij@linaro.org: rebased]
Link: https://lkml.kernel.org/r/20250509-fork-fixes-v3-3-e6c69dd356f2@linaro.org
Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Link: https://lore.kernel.org/20240311164638.2015063-6-pasha.tatashin@soleen.com
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Acked-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
kernel/fork.c

index 8b8457562740c114c640a8cc230876f6a286b246..d6907c49ee87a96a10e231496d27bd29d50be881 100644 (file)
@@ -280,6 +280,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);
 
@@ -288,11 +293,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;