Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796407218; b=Kz5ErYHOOzDOIymlTffshVDKD1bERJ91NnFKDi7BvSXgYAVvv7Sh97Anv2lqR30sr6lD u/7QPAppibHTbGpp8F+7i9Z20ZNrEmyGpbCXRGNl8E2iL+2Tbo+p2jAPaMohyR16K8mOU us/DxmPaxtmJU4TW9MEWgryq/4aw+TEduzTMK/ZF9fcQmA6oyXXHbqJBjxvuqK/xGAB8C pWIVGmuf8dZ0KJqCq1L2P/pPGvLnnjuvXqnYtUGmZutwiDulfmz9+f4zLjFRgcB0qCQKB vlA0ISUoNl7lwtDrolYJJASKHfK9N6Y7+Exx3FJBn43viSPp/Lls0PEuCVo+fdTAi0g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407218; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:Received:From:To:Cc:Subject:Date: Message-ID:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Scanned-By; bh=tQWD9QUKd3iVc4nTp2la0r05TntROGp7jlAetQAs/ow=; b=JP4QfgvMAJkEqE+vdQABjMfpAzog2wWu6vTO8sOhwYkJcCICMpNlDKMfqB+s/7u1peqY MWBSXwMdnB2DbI5wZM76AjcKEp3OiSY01Att1vT5VhYpRhSIvPCo4kaC+QqDvZM0ruTsc 0Ee7oUui7M1pKvvttui2z2XIMpXu01yJP0ergIOxlW/rgsoTKY/jqz3hHXvvHodtdPcWy 6/Cu2Y5PtFDXzUTkgxqhsKgpEnj1h7OX+gJycDnoQswS+no998wlDAWwjRQsxgO1bFWXU lTOfKhN5q0z2nHQHnXYBE77acdd62ATq16uLzQU3EJHShaFmzhLGiP96lzMv4FK5x9A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=gsqwg65T; arc=none smtp.remote-ip=170.10.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DDAA9606A684 for ; Fri, 4 Dec 2026 18:00:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DDAA9606A684 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=gsqwg65T DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407217; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tQWD9QUKd3iVc4nTp2la0r05TntROGp7jlAetQAs/ow=; b=gsqwg65TU0dShBlV+dPYnH9rSna73InafWbOj45bNvBAHqucghEkJqaFkgj8S6Ep/T5a8O YJhHJrpbjLZ6G+Sz0cvk+NP2Jd0WacArBJKvmMEtWGjqYxoltktkL4uLx80ABGcG/4fKvT kNk3NWHo2GGldEUYhpndRZKrb9FbB+w= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS with 8BITMIME (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-92-t2Qnm-TQN7qHCKyn2HsO5Q-1; Fri, 4 Dec 2026 18:00:11 +0000 X-MC-Unique: t2Qnm-TQN7qHCKyn2HsO5Q-1 X-Mimecast-MFC-AGG-ID: t2Qnm-TQN7qHCKyn2HsO5Q_1796407204 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C53ED193A3F4; Fri, 4 Dec 2026 18:00:03 +0000 (UTC) Received: from pbonzini-thinkpadx1carbon6th.rmtit.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 63423BAC; Fri, 4 Dec 2026 18:00:01 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Sean Christopherson , Stefan Teodorescu , Chris Mason , Josef Bacik , David Woodhouse , Jim Mattson , Dave Hansen Subject: [PATCH 15/17] x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state if guest FPU isn't in-use Date: Fri, 4 Dec 2026 18:59:28 +0100 Message-ID: <20261204175930.2154542-16-pbonzini@redhat.com> In-Reply-To: <20261204175930.2154542-1-pbonzini@redhat.com> References: <20261204175930.2154542-1-pbonzini@redhat.com> X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 From: Sean Christopherson 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: 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 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 Suggested-by: Stefan Teodorescu Reported-by: Chris Mason Suggested-by: Chris Mason Cc: Josef Bacik Cc: Paolo Bonzini Cc: David Woodhouse Cc: Jim Mattson Acked-by: Dave Hansen Reviewed-by: Jim Mattson Signed-off-by: Sean Christopherson Link: https://patch.msgid.link/20261119001506.64568-3-seanjc@google.com Signed-off-by: Paolo Bonzini --- 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 2739264b9cf3..2b81d3d8012c 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -339,13 +339,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. * @@ -353,13 +364,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_FOR_KVM(fpu_update_guest_xfd); -- 2.56.1