From: Paolo Bonzini <pbonzini@redhat.com>
To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Cc: Sean Christopherson <seanjc@google.com>,
Stefan Teodorescu <fane@google.com>,
Chris Mason <mason@kernel.org>,
Josef Bacik <josef@toxicpanda.com>,
David Woodhouse <dwmw2@infradead.org>,
Jim Mattson <jmattson@google.com>,
Dave Hansen <dave.hansen@linux.intel.com>
Subject: [PATCH 6.18.y 15/17] x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state if guest FPU isn't in-use
Date: Fri, 4 Dec 2026 19:00:57 +0100 [thread overview]
Message-ID: <20261204180059.2165490-16-pbonzini@redhat.com> (raw)
In-Reply-To: <20261204180059.2165490-1-pbonzini@redhat.com>
From: Sean Christopherson <seanjc@google.com>
[ 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@google.com>
Suggested-by: Stefan Teodorescu <fane@google.com>
Reported-by: Chris Mason <mason@kernel.org>
Suggested-by: Chris Mason <mason@kernel.org>
Cc: Josef Bacik <josef@toxicpanda.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Jim Mattson <jmattson@google.com>
Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Reviewed-by: Jim Mattson <jmattson@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261119001506.64568-3-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
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 023f0f8da527..c098cb1d9215 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -325,13 +325,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.
*
@@ -339,13 +350,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
next prev parent reply other threads:[~2026-12-04 18:01 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 18:00 [PATCH 6.18.y 00/17] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 01/17] KVM: x86: Grab lapic_timer in a local variable to cleanup periodic code Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 02/17] KVM: x86/mmu: Flush TLBs if child SPTE is zapped when splitting a huge SPTE Paolo Bonzini
2026-12-05 12:48 ` Sasha Levin
2026-12-04 18:00 ` [PATCH 6.18.y 03/17] KVM: x86/mmu: Propagate the return from kvm_zap_all_rmap_sptes() out of __rmap_add() Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 04/17] KVM: x86: Disable hypervisor (VMX Preemption) timer if TSC deadline wraps Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 05/17] KVM: x86: Reset local APIC timer metadata on RESET, INIT, and KVM_SET_LAPIC Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 06/17] LoongArch: KVM: Flush TLBs *before* dropping mmu_lock Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 07/17] KVM: x86/mmu: Flush all TLB entries, not a precise range, when yielding Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 08/17] KVM: x86/mmu: Flush TLBs in kvm_unmap_gfn_range() if the unmap may block/yield Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 09/17] KVM: x86/xen: Pin kvm->mm prior to borrowing mm when setting event channel Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 10/17] KVM: Fold kvm_flush_shadow_all() into kvm_mmu_notifier_release() Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 11/17] KVM: WARN if there are in-flight memslot updates when a VM's process exits Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 12/17] KVM: Set mn_active_invalidate_count when the VM's mm is released Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 13/17] KVM: Invalidate all GFN=>PFN caches when VM and/or its process is dying Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 14/17] x86/fpu: Bail early from guest XFD update if XFD isn't supported Paolo Bonzini
2026-12-04 18:00 ` Paolo Bonzini [this message]
2026-12-04 18:00 ` [PATCH 6.18.y 16/17] x86/fpu: Ensure guest FPU is resident in hardware when updating actual XFD MSR Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 17/17] KVM: VMX: Ensure pCPU is valid before sending IPI to deliver posted IRQ Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261204180059.2165490-16-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=dave.hansen@linux.intel.com \
--cc=dwmw2@infradead.org \
--cc=fane@google.com \
--cc=jmattson@google.com \
--cc=josef@toxicpanda.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mason@kernel.org \
--cc=seanjc@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®