From mboxrd@z Thu Jan 1 00:00:00 1970 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=1796407407; b=FjtpXTwxHkRSVbcOBvWw7lILjALETl5enKO9ZhfDDl0HCDFVCldFF/EY/zC9HpNrGj+i qTOPosxGteeHqyklQS5b4ZQDk62gX+fueKlyWCVouF+07w2sgx1Cnw7Wp1NFtjWRDCwHi 8pqYG7iBtuLrB9/TrUPTmi3ompLJaAuEZV7CGs9KWSWyGJOJvtE6hhQbBmjKIQiRhRiHs 3DeeWFxvgUkm/GFX3FGScAfsYa7HbVWcpDDNOYWc2/LX6b7yA+D3skhAYknKfz++MD+Uu EB5eQVSAvIS0UpfW6BxLBCyYbk1AUXoRXyh7T22U1+CnfHNCksDv7laIktK8q7CMvxQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407407; 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=D9bYsqMpX84B6YQ6nOhcENkLK0I0KkzVnxJH2MzRU+E=; b=E3p5jenLaQQtyL1v22Adh6bn2J4uiSOfIqS0I/iiyYgFbT27loSmFK7sOWQO6RBZpw34 5SWsXpClhLca8/NuOmweGRRTvEHHvf7S3KCIjDayhWqdwHzjZbDosoxL0X2h7lC+QzYgd +ckTiPNVgIMJjf1OGtSIaNlbOPTh68wA80RIWbHtjozjPcI9+c/tHEM6DCaSdKToGBYKC KvQvjhSQRLUgb0SOCMZwsaZxxyvRVIQeMQfLkXl7uMX1c/sJq2UrqOVutuKjNHHwQp78/ TtiQiG2xhfsmvI2qBcHBBn4NSo1hwvhOWUBE0ZlG5ikzwgSZ5BDZP9cH6VbiAEiP3nA== 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=duSCsXo7; 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 B5B12606A69A for ; Fri, 4 Dec 2026 18:03:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B5B12606A69A 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=duSCsXo7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407406; 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=D9bYsqMpX84B6YQ6nOhcENkLK0I0KkzVnxJH2MzRU+E=; b=duSCsXo7m5w+jZNIPyh7C6b/1Jvur02/uTcGKaDfMydFPIObJEcp2mFG7WduUpnNU9zGCF lNHBeywIYyqs38CaSyAfvIIttiJVnOF8J8cY3NGGMZOsVY7WQ40N7+0IFqrdrNbC0XEBkf zkAwzlrxXcoiLNCdguIuFbn3afEeW0c= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS with 8BITMIME (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-594-OCu3hoHwOvOlwdDa5NA_BQ-1; Fri, 4 Dec 2026 18:03:19 +0000 X-MC-Unique: OCu3hoHwOvOlwdDa5NA_BQ-1 X-Mimecast-MFC-AGG-ID: OCu3hoHwOvOlwdDa5NA_BQ_1796407397 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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 567A518B95F2; Fri, 4 Dec 2026 18:03:17 +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 ECB7ABAC; Fri, 4 Dec 2026 18:03:14 +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 6.12.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:02:43 +0100 Message-ID: <20261204180245.2166967-16-pbonzini@redhat.com> In-Reply-To: <20261204180245.2166967-1-pbonzini@redhat.com> References: <20261204180245.2166967-1-pbonzini@redhat.com> X-Mailing-List: linux-kernel@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 [ 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: 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 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 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