From: K Prateek Nayak <kprateek.nayak@amd.com>
To: Dave Hansen <dave.hansen@linux.intel.com>,
Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
Borislav Petkov <bp@alien8.de>,
the arch/x86 maintainers <x86@kernel.org>,
<linux-kernel@vger.kernel.org>
Cc: Andy Lutomirski <luto@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Rick Edgecombe <rick.p.edgecombe@intel.com>,
Andrew Cooper <andrew.cooper3@citrix.com>,
Kees Cook <kees@kernel.org>,
Pawan Gupta <pawan.kumar.gupta@linux.intel.com>,
K Prateek Nayak <kprateek.nayak@amd.com>
Subject: [RFC PATCH 21/27] x86/shstsk: Allow suspend-resume and hibernation with kernel shadow stack
Date: Sun, 29 Nov 2026 22:29:46 +0000 [thread overview]
Message-ID: <20261129222953.9124-22-kprateek.nayak@amd.com> (raw)
In-Reply-To: <20261129222953.9124-1-kprateek.nayak@amd.com>
Save and restore the shadow stack pointers from the CPUs driving the
suspend-resume / hibernation while disabling kernel shadow stack just
before the system is suspended.
Since APs are taken down, they would have disabled shadow stack before
hitting the idle routine and only the BSP (CPU doing the final bits of
suspend-resume) needs to cache its SSP with restore token, disable
kernel shadow stack and execute the suspend routine.
The caches SSP is restored on resume such that it finds the matching ret
on the way out.
For hibernation, the CPU restoring the image disables shadow stacks, and
the restored kernel then enables and restores the SSP it had cached
before the hibernation.
Tested with:
rtcwake -m mem -s 20
rtcwake -m freeze -s 30
rtcwake -m disk -s 100
Signed-off-by: K Prateek Nayak <kprateek.nayak@amd.com>
---
arch/x86/Kconfig | 7 ----
arch/x86/include/asm/nospec-branch.h | 55 ++++++++++++++++++++++++++++
arch/x86/include/asm/suspend.h | 2 +
arch/x86/kernel/acpi/wakeup_64.S | 6 +++
arch/x86/power/hibernate.c | 2 +
arch/x86/power/hibernate_32.c | 16 +++++---
arch/x86/power/hibernate_asm_64.S | 7 ++++
7 files changed, 83 insertions(+), 12 deletions(-)
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 21f2b41043f4..4f5097fdd365 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -1911,13 +1911,6 @@ config X86_KERN_SHADOW_STACK
# and uses "pushq $0 ... ret" as the handoff into purgatory. It
# needs S_CET.SH_STK_EN cleared first, which nothing does yet.
depends on !KEXEC_CORE
- # restore_registers() RETs on a stack reloaded from the image
- # with S_CET, the PLx/FRED_SSPx MSRs and SSP itself never saved or
- # restored. Same problem for do_suspend_lowlevel()/wakeup_long64(),
- # which resume with "jmp *%rax" into the middle of the function
- # and then RET.
- depends on !HIBERNATION
- depends on !SUSPEND
# __efi64_thunk() drops into 32-bit firmware with LRETQ and comes
# back with LRET. Far returns are shadow stack checked and expect
# a far CALL frame that was never pushed.
diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
index 0872dceb3819..ff9449921106 100644
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -417,6 +417,61 @@
#define CLEAR_BRANCH_HISTORY_VMEXIT
#endif
+#ifdef CONFIG_X86_KERN_SHADOW_STACK
+
+.macro SUSPEND_KERNEL_SHSTK ssp_offset cet_offset
+ xorl %eax, %eax
+ rdsspq %rax
+ movq %rax, \ssp_offset(%rip)
+ testq %rax, %rax
+ jz .Lsusp_no_shstk\@
+
+ /* Write the restore token = saved_ssp | MODE64 */
+ leaq 1(%rax), %rcx
+ wrssq %rcx, -8(%rax)
+
+ /* Disable Shadow Stack */
+ movl $MSR_IA32_S_CET, %ecx
+ rdmsr
+ movl %eax, \cet_offset(%rip)
+ movl %edx, \cet_offset+4(%rip)
+ andl $~(CET_SHSTK_EN | CET_WRSS_EN), %eax
+ wrmsr
+.Lsusp_no_shstk\@:
+.endm
+
+/*
+ * Call at .Lresume_point after CR4/CR3/CR0 and the GPRs are restored,
+ * before jmp restore_processor_state. Clobbers rax, rcx, rdx and flags.
+ */
+.macro RESUME_KERNEL_SHSTK ssp_offset cet_offset
+ cmpq $0, \ssp_offset(%rip)
+ je .Lres_no_shstk\@
+
+ movl $MSR_IA32_S_CET, %ecx
+ movl \cet_offset(%rip), %eax
+ movl \cet_offset+4(%rip), %edx
+ wrmsr
+
+ movq \ssp_offset(%rip), %rax
+
+ /* Consume and drop the token. */
+ rstorssp -8(%rax)
+ movl $1, %ecx
+ incsspq %rcx
+.Lres_no_shstk\@:
+.endm
+
+#else /* !CONFIG_X86_KERN_SHADOW_STACK */
+
+.macro SUSPEND_KERNEL_SHSTK ssp_offset cet_offset
+.endm
+.macro RESUME_KERNEL_SHSTK ssp_offset cet_offset
+.endm
+
+#endif /* CONFIG_X86_KERN_SHADOW_STACK */
+
+
#else /* __ASSEMBLER__ */
#define ITS_THUNK_SIZE 64
diff --git a/arch/x86/include/asm/suspend.h b/arch/x86/include/asm/suspend.h
index a892494ca5e4..7ea62a6624a0 100644
--- a/arch/x86/include/asm/suspend.h
+++ b/arch/x86/include/asm/suspend.h
@@ -9,6 +9,8 @@ extern unsigned long jump_address_phys;
extern unsigned long restore_cr3 __visible;
extern unsigned long temp_pgt __visible;
extern unsigned long relocated_restore_code __visible;
+extern unsigned long restore_ssp __visible;
+extern unsigned long restore_s_cet __visible;
extern int relocate_restore_code(void);
/* Defined in hibernate_asm_32/64.S */
extern asmlinkage __visible int restore_image(void);
diff --git a/arch/x86/kernel/acpi/wakeup_64.S b/arch/x86/kernel/acpi/wakeup_64.S
index 04f561f75e99..dc4ba38c4e40 100644
--- a/arch/x86/kernel/acpi/wakeup_64.S
+++ b/arch/x86/kernel/acpi/wakeup_64.S
@@ -79,6 +79,8 @@ SYM_FUNC_START(do_suspend_lowlevel)
movq %rdi, saved_rdi(%rip)
movq %rsi, saved_rsi(%rip)
+ SUSPEND_KERNEL_SHSTK saved_ssp, saved_s_cet
+
addq $8, %rsp
movl $3, %edi
xorl %eax, %eax
@@ -126,6 +128,8 @@ SYM_FUNC_START(do_suspend_lowlevel)
call kasan_unpoison_task_stack_below
#endif
+ RESUME_KERNEL_SHSTK saved_ssp, saved_s_cet
+
xorl %eax, %eax
addq $8, %rsp
FRAME_END
@@ -141,5 +145,7 @@ saved_rbx: .quad 0
saved_rip: .quad 0
saved_rsp: .quad 0
+saved_ssp: .quad 0
+saved_s_cet: .quad 0
SYM_DATA(saved_magic, .quad 0)
diff --git a/arch/x86/power/hibernate.c b/arch/x86/power/hibernate.c
index a2294c1649f6..940f8c6bfebc 100644
--- a/arch/x86/power/hibernate.c
+++ b/arch/x86/power/hibernate.c
@@ -39,6 +39,8 @@ unsigned long jump_address_phys;
unsigned long restore_cr3 __visible;
unsigned long temp_pgt __visible;
unsigned long relocated_restore_code __visible;
+unsigned long restore_ssp __visible;
+unsigned long restore_s_cet __visible;
/**
* pfn_is_nosave - check if given pfn is in the 'nosave' section
diff --git a/arch/x86/power/hibernate_32.c b/arch/x86/power/hibernate_32.c
index 223d5bca29b8..0b5498585c25 100644
--- a/arch/x86/power/hibernate_32.c
+++ b/arch/x86/power/hibernate_32.c
@@ -170,29 +170,35 @@ static int set_up_temporary_text_mapping(pgd_t *pgd_base)
asmlinkage int swsusp_arch_resume(void)
{
+ unsigned long ssp = disable_kernel_shstk();
int error;
resume_pg_dir = (pgd_t *)get_safe_page(GFP_ATOMIC);
- if (!resume_pg_dir)
- return -ENOMEM;
+ if (!resume_pg_dir) {
+ error = -ENOMEM;
+ goto out_error;
+ }
resume_init_first_level_page_table(resume_pg_dir);
error = set_up_temporary_text_mapping(resume_pg_dir);
if (error)
- return error;
+ goto out_error;
error = resume_physical_mapping_init(resume_pg_dir);
if (error)
- return error;
+ goto out_error;
temp_pgt = __pa(resume_pg_dir);
error = relocate_restore_code();
if (error)
- return error;
+ goto out_error;
/* We have got enough memory and from now on we cannot recover */
restore_image();
return 0;
+out_error:
+ disable_kernel_shstk(ssp);
+ return err;
}
diff --git a/arch/x86/power/hibernate_asm_64.S b/arch/x86/power/hibernate_asm_64.S
index c73be0a02a6c..e41dadac5045 100644
--- a/arch/x86/power/hibernate_asm_64.S
+++ b/arch/x86/power/hibernate_asm_64.S
@@ -62,6 +62,8 @@ SYM_FUNC_START(restore_registers)
/* Saved in save_processor_state. */
lgdt saved_context_gdt_desc(%rax)
+ RESUME_KERNEL_SHSTK restore_ssp, restore_s_cet
+
xorl %eax, %eax
/* tell the hibernation core that we've just restored the memory */
@@ -94,9 +96,14 @@ SYM_FUNC_START(swsusp_arch_suspend)
movq %cr3, %rax
movq %rax, restore_cr3(%rip)
+ SUSPEND_KERNEL_SHSTK restore_ssp, restore_s_cet
+
FRAME_BEGIN
call swsusp_save
FRAME_END
+
+ RESUME_KERNEL_SHSTK restore_ssp, restore_s_cet
+
RET
SYM_FUNC_END(swsusp_arch_suspend)
--
2.43.0
next prev parent reply other threads:[~2026-11-29 22:38 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-29 22:29 [RFC PATCH 00/27] x86/shstk: More shadow stack enablement and path to IDT support K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 01/27] x86/entry: Parameterize kernel shadow stack handling at FRED entry K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 02/27] x86/perf_regs: Rename pt_regs_offset array to prevent build failure K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 03/27] x86/cet: Fix build for KERN_SHADOW_STACK and !USER_SHADOW_STACK K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 04/27] x86/cet: Make kernel shadow stack #CP fatal instead of livelocking K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 05/27] x86/sync_core: Don't use IRET-to-self with kernel shadow stacks K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 06/27] x86/process: Initialize and enable kernel shadow stack at boot K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 07/27] x86/entry: Handle int3 text patching with kernel shadow stack K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 08/27] x86/retpoline: Handle retpolies and thunks " K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 09/27] x86/ftrace: Prepare ftrace trampolines to handle " K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 10/27] x86/kprobes: Prepare optprobe trampoline to handle shadow stacks K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 11/27] x86/rethook: Prepare the rethook trampoline to handle shadow stack K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 12/27] x86/cet: Make regs_set_return_ip() helpers context aware K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 13/27] x86/cet: Introduce regs_set_return_trampoline() for rethook trampoline K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 14/27] x86/ftrace,livepatch: Use CET helpers for ftrace regs->ip and trampoline redirection K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 15/27] x86/rethook: Make the trampoline insert and return shadow stack aware K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 16/27] x86/error_inject: Use CET helpers to insert just_return_func trampoline K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 17/27] x86/kprobes: Use CET helpers in kprobe core to modify regs->ip K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 18/27] x86/bpf: Make BPF JIT aware of CET handling K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 19/27] x86/shstk: Remove profiling related dependencies for KERN_SHADOW_STACK K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 20/27] x86/shstk: Disable kenrel shadow stack when CPU is offlined K Prateek Nayak
2026-11-29 22:29 ` K Prateek Nayak [this message]
2026-11-29 22:29 ` [RFC PATCH 22/27] x86/shstk: Save and restore shadow stack pointer during kexec K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 23/27] x86/idt: Map MCE shadow stack and populate the INT_SPP table K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 24/27] x86/process: Initialize shadow stack for IDT systems K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 25/27] x86/entry: Prepare IDT entry for shadow stacks K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 26/27] x86/syscall: Toggle shadow stack busy on entry and exit from syscall K Prateek Nayak
2026-11-29 22:29 ` [RFC PATCH 27/27] x86/entry: Handle shadow stack on nested NMI K Prateek Nayak
2026-11-29 23:00 ` [RFC PATCH 00/27] x86/shstk: More shadow stack enablement and path to IDT support Andrew Cooper
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=20261129222953.9124-22-kprateek.nayak@amd.com \
--to=kprateek.nayak@amd.com \
--cc=andrew.cooper3@citrix.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=mingo@redhat.com \
--cc=pawan.kumar.gupta@linux.intel.com \
--cc=peterz@infradead.org \
--cc=rick.p.edgecombe@intel.com \
--cc=tglx@kernel.org \
--cc=x86@kernel.org \
/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
all inboxes | Powered by JetHome®