[tip: x86/urgent] x86/fred: Fix system hang during S4 resume with FRED enabled

From: tip-bot2 for Xin Li (Intel)
Date: Mon Mar 31 2025 - 08:36:58 EST


The following commit has been merged into the x86/urgent branch of tip:

Commit-ID: b578bfe07ae943937a2945a9f6ac4b497f4d4e09
Gitweb: https://git.kernel.org/tip/b578bfe07ae943937a2945a9f6ac4b497f4d4e09
Author: Xin Li (Intel) <xin@xxxxxxxxx>
AuthorDate: Tue, 25 Mar 2025 23:25:40 -07:00
Committer: Ingo Molnar <mingo@xxxxxxxxxx>
CommitterDate: Mon, 31 Mar 2025 14:19:35 +02:00

x86/fred: Fix system hang during S4 resume with FRED enabled

During an S4 resume, the system first performs a cold power-on. The
kernel image is initially loaded to a random linear address, and the
FRED MSRs are initialized. Subsequently, the S4 image is loaded,
and the kernel image is relocated to its original address from before
the S4 suspend. Due to changes in the kernel text and data mappings,
the FRED MSRs must be reinitialized with new values.

[ mingo: Rephrased & clarified the comments. ]

Reported-by: Xi Pardee <xi.pardee@xxxxxxxxx>
Reported-by: Todd Brandt <todd.e.brandt@xxxxxxxxx>
Suggested-by: H. Peter Anvin (Intel) <hpa@xxxxxxxxx>
Signed-off-by: Xin Li (Intel) <xin@xxxxxxxxx>
Signed-off-by: Ingo Molnar <mingo@xxxxxxxxxx>
Tested-by: Todd Brandt <todd.e.brandt@xxxxxxxxx>
Cc: Andy Lutomirski <luto@xxxxxxxxxx>
Cc: Brian Gerst <brgerst@xxxxxxxxx>
Cc: Juergen Gross <jgross@xxxxxxxx>
Cc: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Link: https://lore.kernel.org/r/20250326062540.820556-1-xin@xxxxxxxxx
---
arch/x86/power/cpu.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)

diff --git a/arch/x86/power/cpu.c b/arch/x86/power/cpu.c
index 63230ff..70c8906 100644
--- a/arch/x86/power/cpu.c
+++ b/arch/x86/power/cpu.c
@@ -27,6 +27,7 @@
#include <asm/mmu_context.h>
#include <asm/cpu_device_id.h>
#include <asm/microcode.h>
+#include <asm/fred.h>

#ifdef CONFIG_X86_32
__visible unsigned long saved_context_ebx;
@@ -231,6 +232,25 @@ static void notrace __restore_processor_state(struct saved_context *ctxt)
*/
#ifdef CONFIG_X86_64
wrmsrl(MSR_GS_BASE, ctxt->kernelmode_gs_base);
+
+ /*
+ * Restore FRED settings.
+ *
+ * FRED settings can be completely derived from
+ * current kernel text and data data mappings, thus
+ * nothing needs to be saved and restored.
+ *
+ * As such, simply re-initialize FRED to restore the FRED
+ * settings. Note that any changes to text and data mappings
+ * after S4 resume will generate different FRED configuration
+ * values.
+ *
+ * Also, FRED RSPs setup needs to access per-CPU data structures.
+ */
+ if (ctxt->cr4 & X86_CR4_FRED) {
+ cpu_init_fred_exceptions();
+ cpu_init_fred_rsps();
+ }
#else
loadsegment(fs, __KERNEL_PERCPU);
#endif