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

Configure Feed

Select the types of activity you want to include in your feed.

x86/fpu: Keep xfd_state in sync with MSR_IA32_XFD

Commit 672365477ae8 ("x86/fpu: Update XFD state where required") and
commit 8bf26758ca96 ("x86/fpu: Add XFD state to fpstate") introduced a
per CPU variable xfd_state to keep the MSR_IA32_XFD value cached, in
order to avoid unnecessary writes to the MSR.

On CPU hotplug MSR_IA32_XFD is reset to the init_fpstate.xfd, which
wipes out any stale state. But the per CPU cached xfd value is not
reset, which brings them out of sync.

As a consequence a subsequent xfd_update_state() might fail to update
the MSR which in turn can result in XRSTOR raising a #NM in kernel
space, which crashes the kernel.

To fix this, introduce xfd_set_state() to write xfd_state together
with MSR_IA32_XFD, and use it in all places that set MSR_IA32_XFD.

Fixes: 672365477ae8 ("x86/fpu: Update XFD state where required")
Signed-off-by: Adamos Ttofari <attofari@amazon.de>
Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20240322230439.456571-1-chang.seok.bae@intel.com

Closes: https://lore.kernel.org/lkml/20230511152818.13839-1-attofari@amazon.de

authored by

Adamos Ttofari and committed by
Ingo Molnar
10e4b516 a8ed59a3

+13 -6
+3 -2
arch/x86/kernel/fpu/xstate.c
··· 178 178 * Must happen after CR4 setup and before xsetbv() to allow KVM 179 179 * lazy passthrough. Write independent of the dynamic state static 180 180 * key as that does not work on the boot CPU. This also ensures 181 - * that any stale state is wiped out from XFD. 181 + * that any stale state is wiped out from XFD. Reset the per CPU 182 + * xfd cache too. 182 183 */ 183 184 if (cpu_feature_enabled(X86_FEATURE_XFD)) 184 - wrmsrl(MSR_IA32_XFD, init_fpstate.xfd); 185 + xfd_set_state(init_fpstate.xfd); 185 186 186 187 /* 187 188 * XCR_XFEATURE_ENABLED_MASK (aka. XCR0) sets user features
+10 -4
arch/x86/kernel/fpu/xstate.h
··· 148 148 #endif 149 149 150 150 #ifdef CONFIG_X86_64 151 + static inline void xfd_set_state(u64 xfd) 152 + { 153 + wrmsrl(MSR_IA32_XFD, xfd); 154 + __this_cpu_write(xfd_state, xfd); 155 + } 156 + 151 157 static inline void xfd_update_state(struct fpstate *fpstate) 152 158 { 153 159 if (fpu_state_size_dynamic()) { 154 160 u64 xfd = fpstate->xfd; 155 161 156 - if (__this_cpu_read(xfd_state) != xfd) { 157 - wrmsrl(MSR_IA32_XFD, xfd); 158 - __this_cpu_write(xfd_state, xfd); 159 - } 162 + if (__this_cpu_read(xfd_state) != xfd) 163 + xfd_set_state(xfd); 160 164 } 161 165 } 162 166 163 167 extern int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu); 164 168 #else 169 + static inline void xfd_set_state(u64 xfd) { } 170 + 165 171 static inline void xfd_update_state(struct fpstate *fpstate) { } 166 172 167 173 static inline int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu) {