[PATCH v2 2/3] x86: Remove the padding space at top of the init stack
From: Xin Li (Intel)
Date: Tue Mar 18 2025 - 03:08:44 EST
Because the owner of the init stack, init task, doesn't have any user
level context, there will NEVER be an actual pt_regs structure pushed
at top of the init stack.
However a zeroed pt_regs structure is created at build time and kept
at top of the init stack for task_pt_regs() to function properly with
the init task in the same manner as a normal task with user level
context.
Besides, task_pt_regs() no longer converts a fixed offset from top of
a task kernel stack to a pt_regs structure pointer, but rather returns
whatever in the thread_info.user_pt_regs field, which is initialized
at build time to '(struct pt_regs *)TOP_OF_INIT_STACK - 1' for the
init task.
As a result, there is no point to reserve any padding space at top of
the init stack, so remove the padding space.
Signed-off-by: Xin Li (Intel) <xin@xxxxxxxxx>
---
arch/x86/include/asm/processor.h | 16 ++++++++++++++--
arch/x86/kernel/vmlinux.lds.S | 18 ++++++++++++++++--
2 files changed, 30 insertions(+), 4 deletions(-)
diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h
index 79d8df28cd59..cbb5d2158075 100644
--- a/arch/x86/include/asm/processor.h
+++ b/arch/x86/include/asm/processor.h
@@ -641,8 +641,20 @@ static __always_inline void prefetchw(const void *x)
"m" (*(const char *)x));
}
-#define TOP_OF_INIT_STACK ((unsigned long)&init_stack + sizeof(init_stack) - \
- TOP_OF_KERNEL_STACK_PADDING)
+extern unsigned long __end_init_stack[];
+
+/*
+ * No need to reserve extra padding space above the pt_regs structure
+ * at top of the init stack, because its owner init task doesn't have
+ * any user level context, thus there will NEVER be an actual pt_regs
+ * structure pushed at top of the init stack.
+ *
+ * However a zeroed pt_regs structure is created at build time and kept
+ * at top of the init stack for task_pt_regs() to function properly with
+ * the init task in the same manner as a normal task with user level
+ * context.
+ */
+#define TOP_OF_INIT_STACK ((unsigned long)&__end_init_stack)
#define task_top_of_stack(task) ((unsigned long)(task_pt_regs(task) + 1))
diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S
index ccdc45e5b759..553b65e8bb8a 100644
--- a/arch/x86/kernel/vmlinux.lds.S
+++ b/arch/x86/kernel/vmlinux.lds.S
@@ -178,8 +178,22 @@ SECTIONS
/* init_task */
INIT_TASK_DATA(THREAD_SIZE)
- /* equivalent to task_pt_regs(&init_task) */
- __top_init_kernel_stack = __end_init_stack - TOP_OF_KERNEL_STACK_PADDING - PTREGS_SIZE;
+ /*
+ * No need to reserve extra padding space above the pt_regs
+ * structure at top of the init stack, because its owner
+ * init task doesn't have any user level context, thus there
+ * will NEVER be an actual pt_regs structure pushed at top
+ * of the init stack.
+ *
+ * However a zeroed pt_regs structure is created at build
+ * time and kept at top of the init stack for task_pt_regs()
+ * to function properly with the init task in the same manner
+ * as a normal task with user level context.
+ *
+ * task_pt_regs(&init_task) is now:
+ * '(struct pt_regs *)&__end_init_stack - 1'
+ */
+ __top_init_kernel_stack = __end_init_stack - PTREGS_SIZE;
#ifdef CONFIG_X86_32
/* 32 bit has nosave before _edata */
--
2.48.1