[RFC PATCH 20/27] x86/shstk: Disable kenrel shadow stack when CPU is offlined
From: K Prateek Nayak
Date: Sun Nov 29 2026 - 17:36:15 EST
Once the CPU is marked dead, disable kernel shadow stacks just before
the play_dead() noreturn function.
A subsequent online will setup the shadow stack again from
start_secondary. This prepares the hotplug path for subsequent suspend
resume / hibernation handling.
disable_kernel_shstk() is proactively made to push a restore token on
top of the shadow stack and return the pointer just below to allow for
error handling on the hibernation restore path if the switching to saved
image fails.
Signed-off-by: K Prateek Nayak <kprateek.nayak@xxxxxxx>
---
arch/x86/include/asm/cpu.h | 40 ++++++++++++++++++++++++++++++++++++++
arch/x86/kernel/smpboot.c | 1 +
2 files changed, 41 insertions(+)
diff --git a/arch/x86/include/asm/cpu.h b/arch/x86/include/asm/cpu.h
index 5f2e5b150b61..e9b0907b1a2b 100644
--- a/arch/x86/include/asm/cpu.h
+++ b/arch/x86/include/asm/cpu.h
@@ -79,6 +79,9 @@ extern void prep_kernel_shadow_stack(struct task_struct *tsk,
*/
static __always_inline void enable_kernel_shstk(unsigned long shstk_top)
{
+ if (!cpu_feature_enabled(X86_FEATURE_KERN_SHSTK))
+ return;
+
asm volatile ("rdmsr\n"
"orl %1, %%eax\n"
"wrmsr\n"
@@ -92,6 +95,38 @@ static __always_inline void enable_kernel_shstk(unsigned long shstk_top)
: "eax", "edx", "cc", "memory");
}
+/*
+ * This function should always be inlined. disable_kernel_shstk()
+ * returns the token to shstk_top that can be supplied later to
+ * enable_kernel_shstk() for error handling such that shadow stack
+ * can be re-enabled in the same function. Used for hotplug and
+ * and suspend-resume / hibernation.
+ */
+static __always_inline unsigned long disable_kernel_shstk(void)
+{
+ unsigned long ssp = 0, tok;
+
+ if (!cpu_feature_enabled(X86_FEATURE_KERN_SHSTK))
+ return ssp;
+
+ asm volatile ("rdsspq %0\n"
+ "test %0, %0\n"
+ "jz 1f\n"
+ "lea 1(%0), %1\n"
+ "wrssq %1, -8(%0)\n"
+ "rdmsr\n"
+ "andl %3, %%eax\n"
+ "wrmsr\n"
+ "1:"
+ : "+r" (ssp),
+ "=&r" (tok)
+ : "c" (MSR_IA32_S_CET),
+ "i" (~(u32)(CET_SHSTK_EN | CET_WRSS_EN))
+ : "eax", "edx", "cc", "memory");
+
+ return ssp;
+}
+
/*
* Setup the shadow stack for this CPU.
*
@@ -121,6 +156,11 @@ static __always_inline void cpu_setup_kernel_shstk(bool boot_cpu)
#else /* !CONFIG_X86_KERN_SHADOW_STACK */
+static inline void enable_kernel_shstk(unsigned long shstk_top) { }
+static inline unsigned long disable_kernel_shstk(void)
+{
+ return 0;
+}
static inline void cpu_setup_kernel_shstk(bool boot_cpu) { }
#endif /* CONFIG_X86_KERN_SHADOW_STACK */
diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c
index 6ff2730a39c0..afd7c6740ed8 100644
--- a/arch/x86/kernel/smpboot.c
+++ b/arch/x86/kernel/smpboot.c
@@ -1510,6 +1510,7 @@ void __noreturn native_play_dead(void)
play_dead_common();
tboot_shutdown(TB_SHUTDOWN_WFS);
+ disable_kernel_shstk();
/* Below returns only on error. */
cpuidle_play_dead();
--
2.43.0