[PATCH 16/17] x86/fpu: Ensure guest FPU is resident in hardware when updating actual XFD MSR
From: Paolo Bonzini
Date: Fri Dec 04 2026 - 13:01:03 EST
From: Sean Christopherson <seanjc@xxxxxxxxxx>
When updating a guest FPU's XFD, ensure the guest FPU state is resident in
hardware. Failure to do so means updating XFD may effectively clobber XFD,
and in the worst case scenario can lead to an #NM due to failing to update
XFD. Specifically, if the guest FPU is in-use (associated with the current
task) and the current task has TIF_NEED_FPU_LOAD, then lock and load the
task's FPU regs.
The underlying issue is that TIF_NEED_FPU_LOAD doesn't actually mean the
FPU isn't resident in hardware, it only says the FPU *might* not be resident
in hardware and *may* need a reload. E.g. if the current task was tagged
with TIF_NEED_FPU_LOAD by switch_fpu(), but never evicted from hardware,
then KVM's call to switch_fpu_return() will elide the xfd_update_state()
call (and the XRSTOR) due to the FPU being valid, and run the vCPU with a
stale XFD.
E.g. on a single pCPU 'A', timeshared by two vCPUs:
vcpu0 / fpu0 vcpu1 / fpu1
TIF_NEED_FPU_LOAD = 1
<schedules out>
dirty TMM0 (XINUSE[18] == 1)
wrmsr(MSR_IA32_XFD, 0x40000)
fpu_sync_guest_vmexit_xfd_state()
<schedules out>
TIF_NEED_FPU_LOAD = 1 (switch_fpu())
fpu_fpregs_owner_ctx = fpu0
fpu0->last_cpu = A
<schedules in>
fpu_update_guest_xfd(fpu1, 0)
wrmsrq(MSR_IA32_XFD, 0)
fpregs_state_valid() == true
|
-> switch_fpu_return() skips
restore_fpregs_from_fpstate() and
xfd_update_state()
fpu_swap_kvm_fpstate() (exit to user)
|
-> fpu0->regs.xsave.header.xfeatures
is saved with XSTATE_BV[18] = 1
fpu_swap_kvm_fpstate() (entry from user)
|
-> restore_fpregs_from_fpstate() => #NM
Alternatively, fpu_update_guest_xfd() could skip xfd_update_state() *and*
force an invalidation of the current FPU (set last_cpu = -1) to ensure the
correct XFD is loaded when doing switch_fpu_return(). However, that gets
all kinds of convoluted, and emulating an XFD write with TIF_NEED_FPU_LOAD=1
is very uncommon, and in all likelihood the guest FPU is going to be loaded
into hardware in the near future anyways, i.e. avoiding (maybe) loading the
guest FPU buys nothing but complexity.
Fixes: b45f72177594 ("x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state whenever XFD[i]=1")
Cc: stable@xxxxxxxxxxxxxxx
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>
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
Link: https://patch.msgid.link/20261119001506.64568-4-seanjc@xxxxxxxxxx
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
---
arch/x86/kernel/fpu/core.c | 21 ++++++++++-----------
1 file changed, 10 insertions(+), 11 deletions(-)
diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
index 2b81d3d8012c..dade99c5fc79 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -357,19 +357,18 @@ void fpu_update_guest_xfd(struct fpu_guest *guest_fpu, u64 xfd)
}
/*
- * 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.
- *
- * If however the guest's FPU state is NOT resident in hardware, clear
- * disabled components in XSTATE_BV now, or a subsequent XRSTOR will
- * attempt to load disabled components and generate #NM _in the host_.
+ * If the guest's FPU state is in-use, then ensure the state is resident
+ * in hardware and simply update XFD. The guest is responsible for
+ * clearing disabled components when doing its own XRSTOR operations,
+ * and the XSAVE in fpu_swap_kvm_fpstate() when putting the guest FPU
+ * will clear XSTATE_BV[i] whenever XFD[i]=1.
+ */
+ fpregs_lock_and_load();
+ /*
+ * Update XFD *after* loading FPU state, otherwise XRSTOR would try to
+ * load XSTATE saved with the old XFD, but using the new XFD.
*/
- fpregs_lock();
fpstate->xfd = xfd;
-
- if (xfd && test_thread_flag(TIF_NEED_FPU_LOAD))
- fpstate->regs.xsave.header.xfeatures &= ~xfd;
-
xfd_update_state(fpstate);
fpregs_unlock();
}
--
2.56.1