From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796407297; b=jD1c4Y7KWqNyh3mubSIDJk8+BZGozIVMGAsPuFrvgUQo495EXDqpuusnTqdVsTwZ82W7 eAqFYxuNKda7+TlhI7zHL4nEZLoiG7DgedcLpQaa+Z21TyDq3KU7zQGgR7FKTDt4ZUzth oIDVluXl8nKMbByk+NAVH4X+VboiZZLmm1WnYKYXD8Wx4rW3/M2/RC1/Zthu7wPlbYoT8 VuB/xMJ7VmmHdgCFngqpGXO+RYIUEBCtGlmFNelNOp73QMge0BdBOlgihX19BS7jlbjys STk3flhKkliS3DyZ8xv9R2u08HCcs+uAUKlFYbxXd8xyxnX/xXhHC1m8Wg2t4w9HLCQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407297; 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=SM0/41j5uqTDxNmMfB1pb//Lnt9+32U8d803W8EkSiw=; b=jnhNcxQcWxvJVDEsdXkd9+nU+CIbUDsoxu0mlQWIKLZr9/BXzvuWEXgEGEzaKErwxFAf Zw+loeKL0m+Hegc+DdKNsNRFOmardx3UQTaz0acIzWB3cAJ/kOYCqJFVnKb0I6CU67AZv Iv+RnfJrxGZmvQHL5jUaaLgYX49vkmfBYKZhI1tZ5vvSokaOrceQQedSeB8NC01pXElFB O6MM+hp6GGoh2Iw2ro6QIUeBhqMHhLq8+gGGxFiwBr1UReP4VyZpTUnbbLJKVLLFJptSG YXS9U8+LStIMrm4WP8yNXheEAQKtYjgfqxSa0XfnH38g3uukfYa2lZodV0Esn55wMOg== 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=H71F7P1B; arc=none smtp.remote-ip=170.10.129.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 E063660A2622 for ; Fri, 4 Dec 2026 18:01:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E063660A2622 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=H71F7P1B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407297; 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=SM0/41j5uqTDxNmMfB1pb//Lnt9+32U8d803W8EkSiw=; b=H71F7P1BVaeVUF8Vyri5G6U9zjYN91kJvTiSyU4UR7GilbWJRTV1XAtRQ8Ci/23yn3B9kM rjOjSDwURtkMfIfz41kNP29n+PuLEEZ6aLVyB3t6dwu0HvZrAtOcM1na++lrc5UMDtzWPl pDjOt94RGF2MCSIlmw3cV9FBOIMa37g= 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-127-so3cC56YOoC3xbQZT84GGQ-1; Fri, 4 Dec 2026 18:01:33 +0000 X-MC-Unique: so3cC56YOoC3xbQZT84GGQ-1 X-Mimecast-MFC-AGG-ID: so3cC56YOoC3xbQZT84GGQ_1796407291 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 BC12B18C0DA9; Fri, 4 Dec 2026 18:01:31 +0000 (UTC) Received: from pbonzini-thinkpadx1carbon6th.rmtit.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3915B1955E88; Fri, 4 Dec 2026 18:01:29 +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.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 Message-ID: <20261204180059.2165490-16-pbonzini@redhat.com> In-Reply-To: <20261204180059.2165490-1-pbonzini@redhat.com> References: <20261204180059.2165490-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.0 on 10.30.177.12 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 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