[RFC PATCH v2 07/43] fork: Permit boot-time THREAD_SIZE determination

Eric Curtin ericcurtin17 at gmail.com
Mon Nov 30 17:51:39 PST 2026


From: Ryan Roberts <ryan.roberts at arm.com>

THREAD_SIZE is the size of a kernel thread stack. It has been a
compile-time constant, but with vmap stacks it must be a multiple of
PAGE_SIZE, so with boot-time page size selection it can no longer be
fixed at build time.

Defining THREAD_SIZE for the largest supported page size would waste
memory with smaller pages. For example, arm64 needs 16K stacks, but 64K
pages with a vmap stack require 64K; paying that with 4K or 16K pages
would waste 48K per kernel thread.

So allow THREAD_SIZE to be a runtime value. Add THREAD_SIZE_MAX and
THREAD_ALIGN_MAX, defaulting to THREAD_SIZE and
max(THREAD_ALIGN, THREAD_SIZE_MAX), and use THREAD_SIZE_MAX to size
init_stack, thread_union and the init stack in the linker script.

In fork.c, replace the compile-time split between the page allocator
and the kmem_cache based stack allocator with run-time checks of
THREAD_SIZE >= PAGE_SIZE, and make thread_stack_cache_init() return
early when stacks are page based. The weak stub in init/main.c is now
conditional on CONFIG_VMAP_STACK. KASAN's init_task_stack_addr() uses
THREAD_SIZE rather than sizeof() of the stack array.

No functional change when THREAD_SIZE is a compile-time constant.

Signed-off-by: Ryan Roberts <ryan.roberts at arm.com>
Acked-by: Vlastimil Babka <vbabka at suse.cz>
Signed-off-by: Eric Curtin <eric.curtin at docker.com>
(cherry picked from commit 13e7b56091910f3533cee539b7c1ef857f61fe4d)
---
 include/asm-generic/vmlinux.lds.h |  6 +++-
 include/linux/sched.h             |  4 +--
 include/linux/thread_info.h       | 10 +++++-
 init/main.c                       |  2 +-
 kernel/fork.c                     | 53 ++++++++++---------------------
 mm/kasan/report.c                 |  3 +-
 6 files changed, 35 insertions(+), 43 deletions(-)

diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index 2a86599cb620..4df6c8fd38cb 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -56,6 +56,10 @@
 #define LOAD_OFFSET 0
 #endif
 
+#ifndef THREAD_SIZE_MAX
+#define THREAD_SIZE_MAX		THREAD_SIZE
+#endif
+
 /*
  * Only some architectures want to have the .notes segment visible in
  * a separate PT_NOTE ELF Program Header. When this happens, it needs
@@ -428,7 +432,7 @@
 	init_thread_union = .;						\
 	init_stack = .;							\
 	KEEP(*(.data..init_thread_info))				\
-	. = __start_init_stack + THREAD_SIZE;				\
+	. = __start_init_stack + THREAD_SIZE_MAX;			\
 	__end_init_stack = .;
 
 #define JUMP_TABLE_DATA							\
diff --git a/include/linux/sched.h b/include/linux/sched.h
index e4f6323d7855..2b649bbd668a 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1998,14 +1998,14 @@ union thread_union {
 #ifndef CONFIG_THREAD_INFO_IN_TASK
 	struct thread_info thread_info;
 #endif
-	unsigned long stack[THREAD_SIZE/sizeof(long)];
+	unsigned long stack[THREAD_SIZE_MAX/sizeof(long)];
 };
 
 #ifndef CONFIG_THREAD_INFO_IN_TASK
 extern struct thread_info init_thread_info;
 #endif
 
-extern unsigned long init_stack[THREAD_SIZE / sizeof(unsigned long)];
+extern unsigned long init_stack[THREAD_SIZE_MAX / sizeof(unsigned long)];
 
 #ifdef CONFIG_THREAD_INFO_IN_TASK
 # define task_thread_info(task)	(&(task)->thread_info)
diff --git a/include/linux/thread_info.h b/include/linux/thread_info.h
index 307b8390fc67..73d0a77a3c2c 100644
--- a/include/linux/thread_info.h
+++ b/include/linux/thread_info.h
@@ -89,7 +89,15 @@ static inline long set_restart_fn(struct restart_block *restart,
 }
 
 #ifndef THREAD_ALIGN
-#define THREAD_ALIGN	THREAD_SIZE
+#define THREAD_ALIGN		THREAD_SIZE
+#endif
+
+#ifndef THREAD_SIZE_MAX
+#define THREAD_SIZE_MAX		THREAD_SIZE
+#endif
+
+#ifndef THREAD_ALIGN_MAX
+#define THREAD_ALIGN_MAX	max(THREAD_ALIGN, THREAD_SIZE_MAX)
 #endif
 
 #define THREADINFO_GFP		(GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_SKIP_KASAN)
diff --git a/init/main.c b/init/main.c
index 3d619be281aa..2a44990521eb 100644
--- a/init/main.c
+++ b/init/main.c
@@ -849,7 +849,7 @@ void __init __weak smp_prepare_boot_cpu(void)
 {
 }
 
-# if THREAD_SIZE >= PAGE_SIZE
+#ifdef CONFIG_VMAP_STACK
 void __init __weak thread_stack_cache_init(void)
 {
 }
diff --git a/kernel/fork.c b/kernel/fork.c
index 5a0db5a89615..87c102ea8d62 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -393,45 +393,15 @@ static void free_thread_stack(struct task_struct *tsk)
  * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a
  * kmemcache based allocator.
  */
-#if THREAD_SIZE >= PAGE_SIZE
-
-static void thread_stack_free_rcu(struct rcu_head *rh)
-{
-	__free_pages(virt_to_page(rh), THREAD_SIZE_ORDER);
-}
-
-static void thread_stack_delayed_free(struct task_struct *tsk)
-{
-	struct rcu_head *rh = tsk->stack;
-
-	call_rcu(rh, thread_stack_free_rcu);
-}
-
-static int alloc_thread_stack_node(struct task_struct *tsk, int node)
-{
-	struct page *page = alloc_pages_node(node, THREADINFO_GFP,
-					     THREAD_SIZE_ORDER);
-
-	if (likely(page)) {
-		tsk->stack = kasan_reset_tag(page_address(page));
-		return 0;
-	}
-	return -ENOMEM;
-}
-
-static void free_thread_stack(struct task_struct *tsk)
-{
-	thread_stack_delayed_free(tsk);
-	tsk->stack = NULL;
-}
-
-#else /* !(THREAD_SIZE >= PAGE_SIZE) */
 
 static struct kmem_cache *thread_stack_cache;
 
 static void thread_stack_free_rcu(struct rcu_head *rh)
 {
-	kmem_cache_free(thread_stack_cache, rh);
+	if (THREAD_SIZE >= PAGE_SIZE)
+		__free_pages(virt_to_page(rh), THREAD_SIZE_ORDER);
+	else
+		kmem_cache_free(thread_stack_cache, rh);
 }
 
 static void thread_stack_delayed_free(struct task_struct *tsk)
@@ -444,7 +414,16 @@ static void thread_stack_delayed_free(struct task_struct *tsk)
 static int alloc_thread_stack_node(struct task_struct *tsk, int node)
 {
 	unsigned long *stack;
-	stack = kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node);
+	struct page *page;
+
+	if (THREAD_SIZE >= PAGE_SIZE) {
+		page = alloc_pages_node(node, THREADINFO_GFP, THREAD_SIZE_ORDER);
+		stack = likely(page) ? page_address(page) : NULL;
+	} else {
+		stack = kmem_cache_alloc_node(thread_stack_cache,
+					      THREADINFO_GFP, node);
+	}
+
 	stack = kasan_reset_tag(stack);
 	tsk->stack = stack;
 	return stack ? 0 : -ENOMEM;
@@ -458,13 +437,15 @@ static void free_thread_stack(struct task_struct *tsk)
 
 void thread_stack_cache_init(void)
 {
+	if (THREAD_SIZE >= PAGE_SIZE)
+		return;
+
 	thread_stack_cache = kmem_cache_create_usercopy("thread_stack",
 					THREAD_SIZE, THREAD_SIZE, 0, 0,
 					THREAD_SIZE, NULL);
 	BUG_ON(thread_stack_cache == NULL);
 }
 
-#endif /* THREAD_SIZE >= PAGE_SIZE */
 #endif /* CONFIG_VMAP_STACK */
 
 /* SLAB cache for signal_struct structures (tsk->signal) */
diff --git a/mm/kasan/report.c b/mm/kasan/report.c
index e804b1e1f886..7b170efd224b 100644
--- a/mm/kasan/report.c
+++ b/mm/kasan/report.c
@@ -366,8 +366,7 @@ static inline bool kernel_or_module_addr(const void *addr)
 static inline bool init_task_stack_addr(const void *addr)
 {
 	return addr >= (void *)&init_thread_union.stack &&
-		(addr <= (void *)&init_thread_union.stack +
-			sizeof(init_thread_union.stack));
+		(addr <= (void *)&init_thread_union.stack + THREAD_SIZE);
 }
 
 static void print_address_description(void *addr, u8 tag,



More information about the linux-arm-kernel mailing list