From: Linus Walleij <linus.walleij@linaro.org>
To: Andrew Morton <akpm@linux-foundation.org>,
linux-mm@kvack.org, Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Linus Walleij <linus.walleij@linaro.org>
Subject: [PATCH v2 5/5] fork: zero vmap stack using clear_page() instead of memset()
Date: Wed, 07 May 2025 14:46:31 +0200 [thread overview]
Message-ID: <20250507-fork-fixes-v2-5-82ab1e42cde3@linaro.org> (raw)
In-Reply-To: <20250507-fork-fixes-v2-0-82ab1e42cde3@linaro.org>
From: Pasha Tatashin <pasha.tatashin@soleen.com>
Do not zero the whole span of the stack, but instead only the
pages that are part of the vm_area.
As several architectures have optimized implementations of
clear_page(), this will give the architecture a clear idea
of what is going on and will speed up the clearing operation.
Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Link: https://lore.kernel.org/20240311164638.2015063-7-pasha.tatashin@soleen.com
[linus.walleij@linaro.org: Rebased]
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
kernel/fork.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/kernel/fork.c b/kernel/fork.c
index 6ac4674fdf04081fbcbf1eb99167a4c990a58506..d3f000b846c634221c7dafe6e64185a276c5a08b 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -271,13 +271,15 @@ static int memcg_charge_kernel_stack(struct vm_struct *vm_area)
static int alloc_thread_stack_node(struct task_struct *tsk, int node)
{
struct vm_struct *vm_area;
+ int nr_pages;
void *stack;
- int i;
+ int i, j;
for (i = 0; i < NR_CACHED_STACKS; i++) {
vm_area = this_cpu_xchg(cached_stacks[i], NULL);
if (!vm_area)
continue;
+ nr_pages = vm_area->nr_pages;
if (memcg_charge_kernel_stack(vm_area)) {
vfree(vm_area->addr);
@@ -290,7 +292,8 @@ static int alloc_thread_stack_node(struct task_struct *tsk, int node)
stack = kasan_reset_tag(vm_area->addr);
/* Clear stale pointers from reused stack. */
- memset(stack, 0, THREAD_SIZE);
+ for (j = 0; j < nr_pages; j++)
+ clear_page(page_address(vm_area->pages[j]));
tsk->stack_vm_area = vm_area;
tsk->stack = stack;
--
2.49.0
next prev parent reply other threads:[~2025-05-07 12:46 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-07 12:46 [PATCH v2 0/5] fork: Page operation cleanups in the fork code Linus Walleij
2025-05-07 12:46 ` [PATCH v2 1/5] fork: Clean-up ifdef logic around stack allocation Linus Walleij
2025-05-13 6:29 ` Mike Rapoport
2025-05-07 12:46 ` [PATCH v2 2/5] fork: Clean-up naming of vm_stack/vm_struct variables in vmap stacks code Linus Walleij
2025-05-13 6:33 ` Mike Rapoport
2025-05-07 12:46 ` [PATCH v2 3/5] fork: Remove assumption that vm_area->nr_pages equals to THREAD_SIZE Linus Walleij
2025-05-07 16:56 ` Mateusz Guzik
2025-05-09 5:44 ` Linus Walleij
2025-05-07 12:46 ` [PATCH v2 4/5] fork: check charging success before zeroing stack Linus Walleij
2025-05-13 7:38 ` Mike Rapoport
2025-05-07 12:46 ` Linus Walleij [this message]
2025-05-07 16:51 ` [PATCH v2 5/5] fork: zero vmap stack using clear_page() instead of memset() Mateusz Guzik
2025-05-09 5:49 ` Linus Walleij
2025-05-07 17:03 ` [PATCH v2 0/5] fork: Page operation cleanups in the fork code Mateusz Guzik
2025-05-09 6:57 ` Linus Walleij
2025-05-09 11:16 ` Mateusz Guzik
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=20250507-fork-fixes-v2-5-82ab1e42cde3@linaro.org \
--to=linus.walleij@linaro.org \
--cc=akpm@linux-foundation.org \
--cc=linux-mm@kvack.org \
--cc=pasha.tatashin@soleen.com \
/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