x86/fred: Fix init_task thread stack pointer initialization
Subsystems (2)
PATCH v2 1/1 x86/fred: Fix init_task thread stack pointer initialization
As TOP_OF_KERNEL_STACK_PADDING is defined as 0 on x86_64, no one noticed it's missing in the calculation of the .sp field in INIT_THREAD until it is defined to 16 with CONFIG_X86_FRED=y.
Subtract TOP_OF_KERNEL_STACK_PADDING from the .sp field of INIT_THREAD.
---
Change Since v1:
* Apply offset TOP_OF_KERNEL_STACK_PADDING to all uses of __end_init_task (Brian Gerst).
--- arch/x86/include/asm/processor.h | 6 ++++-- arch/x86/kernel/head_64.S | 3 ++- arch/x86/xen/xen-head.S | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-)
arch/x86/include/asm/processor.h +4 -2
@@ -664,8 +664,10 @@ static __always_inline void prefetchw(const void *x)#else extern unsigned long __end_init_task[]; -#define INIT_THREAD { \ - .sp = (unsigned long)&__end_init_task - sizeof(struct pt_regs), \ +#define INIT_THREAD { \ + .sp = (unsigned long)&__end_init_task - \ + TOP_OF_KERNEL_STACK_PADDING - \ + sizeof(struct pt_regs), \ }extern unsigned long KSTK_ESP (struct task_struct *task);
arch/x86/kernel/head_64.S +2 -1
@@ -26,6 +26,7 @@ #include <asm/apicdef.h> #include <asm/fixmap.h> #include <asm/smp.h> +#include <asm/thread_info.h> /* * We are not able to switch in one step to the final KERNEL ADDRESS SPACE
@@ -66,7 +67,7 @@ SYM_CODE_START_NOALIGN(startup_64) mov %rsi, %r15 /* Set up the stack for verify_cpu() */ - leaq (__end_init_task - PTREGS_SIZE)(%rip), %rsp + leaq (__end_init_task - TOP_OF_KERNEL_STACK_PADDING - PTREGS_SIZE)(%rip), %rsp leaq _text(%rip), %rdi
arch/x86/xen/xen-head.S +1 -1
@@ -49,7 +49,7 @@ SYM_CODE_START(startup_xen) ANNOTATE_NOENDBR cld - leaq (__end_init_task - PTREGS_SIZE)(%rip), %rsp + leaq (__end_init_task - TOP_OF_KERNEL_STACK_PADDING - PTREGS_SIZE)(%rip), %rsp /* Set up %gs. *
base-commit: e13841907b8fda0ae0ce1ec03684665f578416a8
Signature · 1 line
2.44.0
Quoted 4 lines · Xin Li (Intel)
As
TOP_OF_KERNEL_STACK_PADDINGis defined as 0 onx86_64, no one noticed it's missing in the calculation of the .sp field inINIT_THREADuntil it is defined to 16 withCONFIG_X86_FRED=y.
Quoted 8 lines
Subtract
TOP_OF_KERNEL_STACK_PADDINGfrom the .sp field ofINIT_THREAD.Fixes 65c9cc9e2c14 ("x86/fred: Reserve space for the FRED stack frame")Fixes 3adee777ad0d ("x86/smpboot: Remove initial_stack on 64-bit")Reported-by kernel test robot <[email protected]>Signed-off-by Xin Li (Intel) <[email protected]>---
Should this fix, if it looks good, be included for the coming merge window?
Thanks!
Xin
Quoted 63 lines
Change Since v1:
* Apply offsetTOP_OF_KERNEL_STACK_PADDINGto all uses of__end_init_task(Brian Gerst).--- arch/x86/include/asm/processor.h | 6 ++++-- arch/x86/kernel/head_64.S | 3 ++- arch/x86/xen/xen-head.S | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-)arch/x86/include/asm/processor.h +4
-2@@ -664,8 +664,10 @@ static __always_inline void prefetchw(const void *x)#else extern unsigned long __end_init_task[]; -#define INIT_THREAD { \ - .sp = (unsigned long)&__end_init_task - sizeof(struct pt_regs), \ +#define INIT_THREAD { \ + .sp = (unsigned long)&__end_init_task - \ + TOP_OF_KERNEL_STACK_PADDING - \ + sizeof(struct pt_regs), \ }extern unsigned long KSTK_ESP (struct task_struct *task);arch/x86/kernel/head_64.S +2
-1@@ -26,6 +26,7 @@ #include <asm/apicdef.h> #include <asm/fixmap.h> #include <asm/smp.h> +#include <asm/thread_info.h> /* * We are not able to switch in one step to the final KERNEL ADDRESS SPACE@@ -66,7 +67,7 @@ SYM_CODE_START_NOALIGN(startup_64) mov %rsi, %r15 /* Set up the stack for verify_cpu() */ - leaq (__end_init_task - PTREGS_SIZE)(%rip), %rsp + leaq (__end_init_task - TOP_OF_KERNEL_STACK_PADDING - PTREGS_SIZE)(%rip), %rsp leaq _text(%rip), %rdiarch/x86/xen/xen-head.S +1
-1@@ -49,7 +49,7 @@ SYM_CODE_START(startup_xen) ANNOTATE_NOENDBR cld - leaq (__end_init_task - PTREGS_SIZE)(%rip), %rsp + leaq (__end_init_task - TOP_OF_KERNEL_STACK_PADDING - PTREGS_SIZE)(%rip), %rsp /* Set up %gs. *base-commit:
e13841907b8fda0ae0ce1ec03684665f578416a8
H. Peter Anvin
reviewerQuoted 74 lines · Xin Li
Quoted 4 lines · Xin Li (Intel)
As
TOP_OF_KERNEL_STACK_PADDINGis defined as 0 onx86_64, no one noticed it's missing in the calculation of the .sp field inINIT_THREADuntil it is defined to 16 withCONFIG_X86_FRED=y.Quoted 8 lines
Subtract
TOP_OF_KERNEL_STACK_PADDINGfrom the .sp field ofINIT_THREAD.Fixes 65c9cc9e2c14 ("x86/fred: Reserve space for the FRED stack frame")Fixes 3adee777ad0d ("x86/smpboot: Remove initial_stack on 64-bit")Reported-by kernel test robot <[email protected]>Signed-off-by Xin Li (Intel) <[email protected]>---
Should this fix, if it looks good, be included for the coming merge window?
Thanks!
XinQuoted 54 lines
Change Since v1:
* Apply offsetTOP_OF_KERNEL_STACK_PADDINGto all uses of__end_init_task(Brian Gerst).--- arch/x86/include/asm/processor.h | 6 ++++-- arch/x86/kernel/head_64.S | 3 ++- arch/x86/xen/xen-head.S | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-)arch/x86/include/asm/processor.h +4
-1@@ -664,8 +664,10 @@ static __always_inline void prefetchw(const void *x)#else extern unsigned long __end_init_task[]; -#define INIT_THREAD { \ - .sp = (unsigned long)&__end_init_task - sizeof(struct pt_regs), \ +#define INIT_THREAD { \ + .sp = (unsigned long)&__end_init_task - \ + TOP_OF_KERNEL_STACK_PADDING - \ + sizeof(struct pt_regs), \ }extern unsigned long KSTK_ESP (struct task_struct *task);arch/x86/kernel/head_64.S +2
-1@@ -26,6 +26,7 @@ #include <asm/apicdef.h> #include <asm/fixmap.h> #include <asm/smp.h> +#include <asm/thread_info.h> /* * We are not able to switch in one step to the final KERNEL ADDRESS SPACE@@ -66,7 +67,7 @@ SYM_CODE_START_NOALIGN(startup_64) mov %rsi, %r15 /* Set up the stack for verify_cpu() */ - leaq (__end_init_task - PTREGS_SIZE)(%rip), %rsp + leaq (__end_init_task - TOP_OF_KERNEL_STACK_PADDING - PTREGS_SIZE)(%rip), %rsp leaq _text(%rip), %rdi diff --git a/arch/x86/xen/xen-head.S b/arch/x86/xen/xen-head.Sindex
a0ea285878db..04101b984f24100644arch/x86/xen/xen-head.S +1
-0@@ -49,7 +49,7 @@ SYM_CODE_START(startup_xen) ANNOTATE_NOENDBR cld - leaq (__end_init_task - PTREGS_SIZE)(%rip), %rsp + leaq (__end_init_task - TOP_OF_KERNEL_STACK_PADDING - PTREGS_SIZE)(%rip), %rsp /* Set up %gs. *
Quoted 2 lines
Quoted 1 line
base-commit:
e13841907b8fda0ae0ce1ec03684665f578416a8
Absolutely.
The following commit has been merged into the x86/fred branch of tip:
The following commit has been merged into the x86/fred branch of tip:
Commit-ID: c416b5bac6ad6ffe21e36225553b82ff2ec1558c Gitweb: https://git.kernel.org/tip/c416b5bac6ad6ffe21e36225553b82ff2ec1558c Author: Xin Li (Intel) <[email protected]> AuthorDate: Mon, 04 Mar 2024 00:33:33 -08:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Thu, 07 Mar 2024 11:55:36 +01:00
x86/fred: Fix init_task thread stack pointer initialization
As TOP_OF_KERNEL_STACK_PADDING was defined as 0 on x86_64, it went unnoticed that the initialization of the .sp field in INIT_THREAD and some calculations in the low level startup code do not take the padding into account.
FRED enabled kernels require a 16 byte padding, which means that the init task initialization and the low level startup code use the wrong stack offset.
Subtract TOP_OF_KERNEL_STACK_PADDING in all affected places to adjust for this.
--- arch/x86/include/asm/processor.h | 6 ++++-- arch/x86/kernel/head_64.S | 3 ++- arch/x86/xen/xen-head.S | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-)
arch/x86/include/asm/processor.h +4 -2
@@ -664,8 +664,10 @@ static __always_inline void prefetchw(const void *x)#else extern unsigned long __end_init_task[]; -#define INIT_THREAD { \ - .sp = (unsigned long)&__end_init_task - sizeof(struct pt_regs), \ +#define INIT_THREAD { \ + .sp = (unsigned long)&__end_init_task - \ + TOP_OF_KERNEL_STACK_PADDING - \ + sizeof(struct pt_regs), \ }extern unsigned long KSTK_ESP (struct task_struct *task);
arch/x86/kernel/head_64.S +2 -1
@@ -26,6 +26,7 @@ #include <asm/apicdef.h> #include <asm/fixmap.h> #include <asm/smp.h> +#include <asm/thread_info.h> /* * We are not able to switch in one step to the final KERNEL ADDRESS SPACE
@@ -66,7 +67,7 @@ SYM_CODE_START_NOALIGN(startup_64) mov %rsi, %r15 /* Set up the stack for verify_cpu() */ - leaq (__end_init_task - PTREGS_SIZE)(%rip), %rsp + leaq (__end_init_task - TOP_OF_KERNEL_STACK_PADDING - PTREGS_SIZE)(%rip), %rsp leaq _text(%rip), %rdi
arch/x86/xen/xen-head.S +1 -1
@@ -49,7 +49,7 @@ SYM_CODE_START(startup_xen) ANNOTATE_NOENDBR cld - leaq (__end_init_task - PTREGS_SIZE)(%rip), %rsp + leaq (__end_init_task - TOP_OF_KERNEL_STACK_PADDING - PTREGS_SIZE)(%rip), %rsp /* Set up %gs. *