[PATCH 6.12.y 15/17] x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state if guest FPU isn't in-use
From: Paolo Bonzini
Date: Fri Dec 04 2026 - 13:05:41 EST
From: Sean Christopherson <seanjc@xxxxxxxxxx>
[ Upstream commit 28743b52a883547367ca1a8de86ecbc30fd9a660 ]
When updating a guest FPU's XFD, manually clear affected XSTATE_BV[i] state
if the FPU isn't in-use, otherwise a subsequent load of the guest's FPU
may trigger an #NM due to XSTATE_BV[i] being stale with respect to the XFD
value loaded in hardware.
Oops: unexpected #NM exception: 0000 [#1] SMP
CPU: 63 UID: 1000 PID: 1139 Comm: guest_xfd_nm Tainted: G W
Tainted: [W]=WARN
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015
RIP: 0010:restore_fpregs_from_fpstate+0x36/0xa0
Call Trace:
<TASK>
fpu_swap_kvm_fpstate+0x64/0x130
kvm_arch_vcpu_ioctl_run+0x334/0x21e0 [kvm]
kvm_vcpu_ioctl+0x2c7/0x960 [kvm]
__x64_sys_ioctl+0x90/0xd0
do_syscall_64+0x9c/0x4d0
entry_SYSCALL_64_after_hwframe+0x4b/0x53
</TASK>
Arguably, failure to ensure the FPU is loaded is a KVM bug, but addressing
that issue is non-trivial due to quirks in KVM's uABI. KVM saves/restores
INIT and SIPI events via MP_STATE (where valid states are RUNNING, HALTED,
SHUTDOWN, WFS, etc.) instead of saving/restoring the raw INIT/SIPI events.
To avoid losing pending INIT/SIPI events, KVM processes pending INIT/SIPIs
when userspace is getting MP_STATE, i.e. outside of the KVM_RUN flow and
thus outside of kvm_arch_vcpu_ioctl_run()'s kvm_{load,put}_guest_fpu() pair.
Combined with KVM's emulation of VMX's VM-Exit MSR load/store lists (when
the vCPU is running nested), KVM will inadvertently trigger an emulated XFD
write when emulating an INIT VM-Exit outside of KVM_RUN:
kvm_arch_vcpu_ioctl_get_mpstate()
-> kvm_apic_accept_events()
-> vmx_check_nested_events()
-> nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0)
-> load_vmcs12_host_state()
-> nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, vmcs12->vm_exit_msr_load_count)
-> kvm_set_msr(vcpu, MSR_IA32_XFD, 0x40000)
-> kvm_set_msr_common()
-> fpu_update_guest_xfd(&vcpu->arch.guest_fpu, 0x40000)
Unconditionally doing kvm_{load,put}_guest_fpu() around INIT emulation when
handling KVM_GET_MP_STATE would fix this specific variant, but the nVMX MSR
load/store lists are problematic in general and can be reached by even more
subtle methods outside of KVM_RUN. E.g. if userspace disables VMX entirely
by stuffing MSR_IA32_FEAT_CTL via KVM_SET_MSRS or KVM_SET_ONE_REG while L2
is active, then KVM will force the vCPU out of L2 via a (non-architectural)
nested VM-Exit. For now, fix the immediate, urgent issue, and temporarily
defer a more complete fix for the KVM mess.
Deliberately handle the !in_use case separately in anticipation of fixing
another, similar bug for the in_use=true case. If the guest FPU isn't in
use, i.e. isn't associated with the current task (or *any* task), then
locking the FPU regs and checking TIF_NEED_FPU_LOAD is meaningless and
unnecessary, and trying to juggle TIF_NEED_FPU_LOAD vs. in_use in the same
flow makes the entire mess inscrutable.
Similarly, duplicate the fpstate->xfd update as well (although this one is
unfortunately more likely to be permanent). If the FPU is in-use, then XFD
needs to be updated *after* locking the FPU, otherwise trying to load FPU
state saved with the old XFD using new XFD will #NM if the old state
contains valid data for any newly-disabled features.
Fixes: b45f72177594 ("x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state whenever XFD[i]=1")
Reported-by: Stefan Teodorescu <fane@xxxxxxxxxx>
Suggested-by: Stefan Teodorescu <fane@xxxxxxxxxx>
Reported-by: Chris Mason <mason@xxxxxxxxxx>
Suggested-by: Chris Mason <mason@xxxxxxxxxx>
Cc: Josef Bacik <josef@xxxxxxxxxxxxxx>
Cc: Paolo Bonzini <pbonzini@xxxxxxxxxx>
Cc: David Woodhouse <dwmw2@xxxxxxxxxxxxx>
Cc: Jim Mattson <jmattson@xxxxxxxxxx>
Acked-by: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
Reviewed-by: Jim Mattson <jmattson@xxxxxxxxxx>
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
Link: https://patch.msgid.link/20261119001506.64568-3-seanjc@xxxxxxxxxx
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
---
arch/x86/kernel/fpu/core.c | 23 +++++++++++++++++------
1 file changed, 17 insertions(+), 6 deletions(-)
diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
index b8828bde39e9..6b1f7be24ea0 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -301,13 +301,24 @@ void fpu_update_guest_xfd(struct fpu_guest *guest_fpu, u64 xfd)
return;
}
- fpregs_lock();
-
/*
* KVM's guest ABI is that setting XFD[i]=1 *can* immediately revert the
* save state to its initial configuration. Likewise, KVM_GET_XSAVE does
* the same as XSAVE and returns XSTATE_BV[i]=0 whenever XFD[i]=1.
*
+ * If the guest's FPU state isn't in-use, i.e. if the host FPU state is
+ * active, then the guest FPU isn't associated with the current task and
+ * *can't* be resident in hardware. Update the in-memory guest fpstate
+ * and clear all disabled components to prevent an #NM on XRSTOR if/when
+ * the guest FPU is next loaded into hardware.
+ */
+ if (!fpstate->in_use) {
+ fpstate->xfd = xfd;
+ fpstate->regs.xsave.header.xfeatures &= ~xfd;
+ return;
+ }
+
+ /*
* If the guest's FPU state is in hardware, just update XFD: the XSAVE
* in fpu_swap_kvm_fpstate will clear XSTATE_BV[i] whenever XFD[i]=1.
*
@@ -315,13 +326,13 @@ void fpu_update_guest_xfd(struct fpu_guest *guest_fpu, u64 xfd)
* disabled components in XSTATE_BV now, or a subsequent XRSTOR will
* attempt to load disabled components and generate #NM _in the host_.
*/
+ fpregs_lock();
+ fpstate->xfd = xfd;
+
if (xfd && test_thread_flag(TIF_NEED_FPU_LOAD))
fpstate->regs.xsave.header.xfeatures &= ~xfd;
- fpstate->xfd = xfd;
- if (fpstate->in_use)
- xfd_update_state(fpstate);
-
+ xfd_update_state(fpstate);
fpregs_unlock();
}
EXPORT_SYMBOL_GPL(fpu_update_guest_xfd);
--
2.56.1