Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/cpu: Load microcode during restore_processor_state()

When resuming from system sleep state, restore_processor_state()
restores the boot CPU MSRs. These MSRs could be emulated by microcode.
If microcode is not loaded yet, writing to emulated MSRs leads to
unchecked MSR access error:

...
PM: Calling lapic_suspend+0x0/0x210
unchecked MSR access error: WRMSR to 0x10f (tried to write 0x0...0) at rIP: ... (native_write_msr)
Call Trace:
<TASK>
? restore_processor_state
x86_acpi_suspend_lowlevel
acpi_suspend_enter
suspend_devices_and_enter
pm_suspend.cold
state_store
kobj_attr_store
sysfs_kf_write
kernfs_fop_write_iter
new_sync_write
vfs_write
ksys_write
__x64_sys_write
do_syscall_64
entry_SYSCALL_64_after_hwframe
RIP: 0033:0x7fda13c260a7

To ensure microcode emulated MSRs are available for restoration, load
the microcode on the boot CPU before restoring these MSRs.

[ Pawan: write commit message and productize it. ]

Fixes: e2a1256b17b1 ("x86/speculation: Restore speculation related MSRs during S3 resume")
Reported-by: Kyle D. Pelton <kyle.d.pelton@intel.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Signed-off-by: Pawan Gupta <pawan.kumar.gupta@linux.intel.com>
Tested-by: Kyle D. Pelton <kyle.d.pelton@intel.com>
Cc: stable@vger.kernel.org
Link: https://bugzilla.kernel.org/show_bug.cgi?id=215841
Link: https://lore.kernel.org/r/4350dfbf785cd482d3fafa72b2b49c83102df3ce.1650386317.git.pawan.kumar.gupta@linux.intel.com

+14 -4
+2
arch/x86/include/asm/microcode.h
··· 131 131 extern void load_ucode_ap(void); 132 132 void reload_early_microcode(void); 133 133 extern bool initrd_gone; 134 + void microcode_bsp_resume(void); 134 135 #else 135 136 static inline void __init load_ucode_bsp(void) { } 136 137 static inline void load_ucode_ap(void) { } 137 138 static inline void reload_early_microcode(void) { } 139 + static inline void microcode_bsp_resume(void) { } 138 140 #endif 139 141 140 142 #endif /* _ASM_X86_MICROCODE_H */
+3 -3
arch/x86/kernel/cpu/microcode/core.c
··· 758 758 }; 759 759 760 760 /** 761 - * mc_bp_resume - Update boot CPU microcode during resume. 761 + * microcode_bsp_resume - Update boot CPU microcode during resume. 762 762 */ 763 - static void mc_bp_resume(void) 763 + void microcode_bsp_resume(void) 764 764 { 765 765 int cpu = smp_processor_id(); 766 766 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; ··· 772 772 } 773 773 774 774 static struct syscore_ops mc_syscore_ops = { 775 - .resume = mc_bp_resume, 775 + .resume = microcode_bsp_resume, 776 776 }; 777 777 778 778 static int mc_cpu_starting(unsigned int cpu)
+9 -1
arch/x86/power/cpu.c
··· 25 25 #include <asm/cpu.h> 26 26 #include <asm/mmu_context.h> 27 27 #include <asm/cpu_device_id.h> 28 + #include <asm/microcode.h> 28 29 29 30 #ifdef CONFIG_X86_32 30 31 __visible unsigned long saved_context_ebx; ··· 263 262 x86_platform.restore_sched_clock_state(); 264 263 mtrr_bp_restore(); 265 264 perf_restore_debug_store(); 266 - msr_restore_context(ctxt); 267 265 268 266 c = &cpu_data(smp_processor_id()); 269 267 if (cpu_has(c, X86_FEATURE_MSR_IA32_FEAT_CTL)) 270 268 init_ia32_feat_ctl(c); 269 + 270 + microcode_bsp_resume(); 271 + 272 + /* 273 + * This needs to happen after the microcode has been updated upon resume 274 + * because some of the MSRs are "emulated" in microcode. 275 + */ 276 + msr_restore_context(ctxt); 271 277 } 272 278 273 279 /* Needed by apm.c */