From nobody Tue Feb 10 04:12:29 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ADCD62BEC34 for ; Thu, 1 Jan 2026 09:05:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767258333; cv=none; b=CHhwlGtIUFfSo5VTmh4rayUFZvDg95cAu8dsFCMe63P6Y9Euismb4QZcgH9gCRdvun4Pm+HBjAjmNXPS5plyIkAxokKlfB1Lwpd2pO0pOKGgskXylMIzDL+PzB02DQ7xcEfougk5Mtv8mGmY85M4Eu0PY8Ei7CzXpJaqaZKK70s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767258333; c=relaxed/simple; bh=3/a9OCUVyU9ScqNZQ0b1YmABoW/17pDAl/TUK/D/XVw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Rw/Y33VRqvBsRuSUaLqxb8ubyQcAReGRM90hHrvom+fPuiz14J1cOH5iShrftn5/kmAePSshxrtJmnBvTC+J2N4qOCF91yQ25epnbnQDTfKP8oM3djfyniRBvh2r/zBjuaxbqN0h/ZeBdpunhB2/d82xgRevOso6SbKSfD6rw08= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=S0J84aLR; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=LUG/s/6O; arc=none smtp.client-ip=170.10.133.124 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.b="S0J84aLR"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="LUG/s/6O" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767258330; 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=oVrzWg1QLfb+e/xteMN/2PG1AfjCSUiR2/tQXeGIqc4=; b=S0J84aLRpd1g5dqryxHgP774KoRhIMzbCQPOP0lVRB8FZjZTZ1NPWeu2C0voUo0BtHazMo CtzXK6hoKCl5P4bS9b28j4tWoi2iUavYZJw0cL9bpfGKSeO0FHY1FagwumChWdS8JuOVRV Z6Cls5JUfDFqg2wmSSxQhRgNfbuCZIY= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-274-dH7WcyvjOkeeypI5laTomQ-1; Thu, 01 Jan 2026 04:05:29 -0500 X-MC-Unique: dH7WcyvjOkeeypI5laTomQ-1 X-Mimecast-MFC-AGG-ID: dH7WcyvjOkeeypI5laTomQ_1767258328 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-477c49f273fso140877575e9.3 for ; Thu, 01 Jan 2026 01:05:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1767258328; x=1767863128; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=oVrzWg1QLfb+e/xteMN/2PG1AfjCSUiR2/tQXeGIqc4=; b=LUG/s/6OV0vkHswBFpixutpi5sQfptn9ptOrmRKRdvmeLD1vRo1awbtMkeSOkzo/Vc rNw6RJw1v44ykp63ur0Fx9W5Bb2rbxrK3gp9u63WQt+UXAlJ4bQzXlfli5FYjPASxT8R LiCkIOXyAJ7p7ziyiplaIdtwtnq/Y2vJpkqRDwodlYFopTlWtN02o/jGkQe4iSFWl9e4 Phc9HGOM7Z299ti1iDIlMtrLUC9RJ+uGeBZyUBtOUaX3q2YdpRk/JsEbxox7Hm26Iu5r 5uqu3VBBCBfS5aW4GMhMVBsQOYNT1dDFtM7fWRaaAczr1N8ukXS9j6UCtASGSOBgxMkB gKeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767258328; x=1767863128; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=oVrzWg1QLfb+e/xteMN/2PG1AfjCSUiR2/tQXeGIqc4=; b=hKio7WsA7TSsqAi/l9/y5NsZxtg0L571PF1Ib9JY8fDg59PuIokDI+HuubWcJni7fs hpTbafNApBwnVH+PnKvSO2qFRdA1zFUqc84mZstMI8I8gIHXp3d9YVH2QfVKalvmeOby oOJ8GwRpIKZ7sJo9UKyLXLFITfmCHHAIeCc5pPOh1lgwwAEJ0HTpomLlJ+Y0aq7F0qK1 t31+uP52EnYjg458aMQ4tQVyBJFLQwbX3xlCP3joSOiRghgLy9vj10b+WQ9T9aoKdS2C tZyetsiQMjVT+rd6/jh0kXTd2q2V9e1cg00fCm1wlY2ibqtmiNq3k5g3Zx2Lp/EBG2iU AHmA== X-Gm-Message-State: AOJu0YwWG/nrIIVR06+2I01JCZFAK3svu2CpNBboomC7lakXhe5539J7 x9gmI7lcFcfspUrz7JWJle5jFrGj5n6X6oSX8gpjRGWG1QQ8GHjgmFQtEefuuJU2KQuM9ngvnJ/ IcCGho2WhrFbOtmeXS3ZTH/DJ+9vllwrFzLwYhE9u2ml1Ax7QnIraByRi1qqL5s8urBsDOSQug/ oGDzXZ16Un1eo0qCOy/wvnUGGJbjRx/+sgk/hIOUtZ/CACdXQnfA== X-Gm-Gg: AY/fxX4q5kyR3Am5DJuiMK/ihmJqmSRkd2nzxnvEsG9kRkPdLMEbxpqbFQsoIqO4f0S HxD5e/hFVkwfp04OMbnRY+p8cRls1nwskdCykjWo4PmyU5o4pmkrjbLWmEV7gasLYd8urq0NhkU d2s+MLZ2z/TrP+l9xPoRaxTlA4xiTq/GBimOTTxVfVrFxA8UgzCVb1xdcMj4aXOAVZc6iiX13vK +JseOjaFciUY2QOXf1FU4hAfZc/Okus32DmxFBeNo0sNr5053+UaCvGx5vtUidaDNbij1cWl5x9 mZkTzDuCRr0iDeWPzHB2UKmMyJP5yANHX7vm+ptgJJEsFZkuQTt15fEJJCSXMrpbKVm+kmMkhb8 azbaNK2HY6Cia8vER3ipp2TVw/PmHw61GT3V2bmaNscku6A9At1yYIc+yhcy+GWevQnzDFB6V49 ufUgBYF6bFWH6x0g== X-Received: by 2002:a05:600c:1384:b0:46e:3550:9390 with SMTP id 5b1f17b1804b1-47d39ddee7amr338592245e9.20.1767258322885; Thu, 01 Jan 2026 01:05:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IGIJiRlDf1E7O6XdT4RGGyZlYOteJ7r4qmoeA9C3oj2jnU10v7zPnwjpPk4IhSjXasbvCrsfg== X-Received: by 2002:a05:600c:1384:b0:46e:3550:9390 with SMTP id 5b1f17b1804b1-47d39ddee7amr338590295e9.20.1767258320516; Thu, 01 Jan 2026 01:05:20 -0800 (PST) Received: from [192.168.10.48] ([151.61.26.160]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4324ea22674sm79916544f8f.10.2026.01.01.01.05.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Jan 2026 01:05:19 -0800 (PST) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, x86@kernel.org, stable@vger.kernel.org Subject: [PATCH 1/4] x86/fpu: Clear XSTATE_BV[i] in save state whenever XFD[i]=1 Date: Thu, 1 Jan 2026 10:05:13 +0100 Message-ID: <20260101090516.316883-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260101090516.316883-1-pbonzini@redhat.com> References: <20260101090516.316883-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Sean Christopherson When loading guest XSAVE state via KVM_SET_XSAVE, and when updating XFD in response to a guest WRMSR, clear XFD-disabled features in the saved (or to be restored) XSTATE_BV to ensure KVM doesn't attempt to load state for features that are disabled via the guest's XFD. Because the kernel executes XRSTOR with the guest's XFD, saving XSTATE_BV[i]=3D1 with XFD[i]= =3D1 will cause XRSTOR to #NM and panic the kernel. E.g. if fpu_update_guest_xfd() sets XFD without clearing XSTATE_BV: ------------[ cut here ]------------ WARNING: arch/x86/kernel/traps.c:1524 at exc_device_not_available+0x101/0= x110, CPU#29: amx_test/848 Modules linked in: kvm_intel kvm irqbypass CPU: 29 UID: 1000 PID: 848 Comm: amx_test Not tainted 6.19.0-rc2-ffa07f7f= d437-x86_amx_nm_xfd_non_init-vm #171 NONE Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 RIP: 0010:exc_device_not_available+0x101/0x110 Call Trace: asm_exc_device_not_available+0x1a/0x20 RIP: 0010:restore_fpregs_from_fpstate+0x36/0x90 switch_fpu_return+0x4a/0xb0 kvm_arch_vcpu_ioctl_run+0x1245/0x1e40 [kvm] kvm_vcpu_ioctl+0x2c3/0x8f0 [kvm] __x64_sys_ioctl+0x8f/0xd0 do_syscall_64+0x62/0x940 entry_SYSCALL_64_after_hwframe+0x4b/0x53 ---[ end trace 0000000000000000 ]--- This can happen if the guest executes WRMSR(MSR_IA32_XFD) to set XFD[18] = =3D 1, and a host IRQ triggers kernel_fpu_begin() prior to the vmexit handler's call to fpu_update_guest_xfd(). and if userspace stuffs XSTATE_BV[i]=3D1 via KVM_SET_XSAVE: ------------[ cut here ]------------ WARNING: arch/x86/kernel/traps.c:1524 at exc_device_not_available+0x101/0= x110, CPU#14: amx_test/867 Modules linked in: kvm_intel kvm irqbypass CPU: 14 UID: 1000 PID: 867 Comm: amx_test Not tainted 6.19.0-rc2-2dace9fa= ccd6-x86_amx_nm_xfd_non_init-vm #168 NONE Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 RIP: 0010:exc_device_not_available+0x101/0x110 Call Trace: asm_exc_device_not_available+0x1a/0x20 RIP: 0010:restore_fpregs_from_fpstate+0x36/0x90 fpu_swap_kvm_fpstate+0x6b/0x120 kvm_load_guest_fpu+0x30/0x80 [kvm] kvm_arch_vcpu_ioctl_run+0x85/0x1e40 [kvm] kvm_vcpu_ioctl+0x2c3/0x8f0 [kvm] __x64_sys_ioctl+0x8f/0xd0 do_syscall_64+0x62/0x940 entry_SYSCALL_64_after_hwframe+0x4b/0x53 ---[ end trace 0000000000000000 ]--- The new behavior is consistent with the AMX architecture. Per Intel's SDM, XSAVE saves XSTATE_BV as '0' for components that are disabled via XFD (and non-compacted XSAVE saves the initial configuration of the state component): If XSAVE, XSAVEC, XSAVEOPT, or XSAVES is saving the state component i, the instruction does not generate #NM when XCR0[i] =3D IA32_XFD[i] =3D 1; instead, it operates as if XINUSE[i] =3D 0 (and the state component was in its initial state): it saves bit i of XSTATE_BV field of the XSAVE header as 0; in addition, XSAVE saves the initial configuration of the state component (the other instructions do not save state component i). Alternatively, KVM could always do XRSTOR with XFD=3D0, e.g. by using a constant XFD based on the set of enabled features when XSAVEing for a struct fpu_guest. However, having XSTATE_BV[i]=3D1 for XFD-disabled features can only happen in the above interrupt case, or in similar scenarios involving preemption on preemptible kernels, because fpu_swap_kvm_fpstate()'s call to save_fpregs_to_fpstate() saves the outgoing FPU state with the current XFD; and that is (on all but the first WRMSR to XFD) the guest XFD. Therefore, XFD can only go out of sync with XSTATE_BV in the above interrupt case, or in similar scenarios involving preemption on preemptible kernels, and it we can consider it (de facto) part of KVM ABI that KVM_GET_XSAVE returns XSTATE_BV[i]=3D0 for XFD-disabled features. Reported-by: Paolo Bonzini Cc: stable@vger.kernel.org Fixes: 820a6ee944e7 ("kvm: x86: Add emulation for IA32_XFD", 2022-01-14) Signed-off-by: Sean Christopherson [Move clearing of XSTATE_BV from fpu_copy_uabi_to_guest_fpstate to kvm_vcpu_ioctl_x86_set_xsave. - Paolo] Signed-off-by: Paolo Bonzini Reviewed-by: Binbin Wu Reviewed-by: Yuan Yao --- arch/x86/kernel/fpu/core.c | 32 +++++++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 9 +++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index da233f20ae6f..166c380b0161 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -319,10 +319,29 @@ EXPORT_SYMBOL_FOR_KVM(fpu_enable_guest_xfd_features); #ifdef CONFIG_X86_64 void fpu_update_guest_xfd(struct fpu_guest *guest_fpu, u64 xfd) { + struct fpstate *fpstate =3D guest_fpu->fpstate; + fpregs_lock(); - guest_fpu->fpstate->xfd =3D xfd; - if (guest_fpu->fpstate->in_use) - xfd_update_state(guest_fpu->fpstate); + + /* + * KVM's guest ABI is that setting XFD[i]=3D1 *can* immediately revert + * the save state to initialized. Likewise, KVM_GET_XSAVE does the + * same as XSAVE and returns XSTATE_BV[i]=3D0 whenever XFD[i]=3D1. + * + * 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]=3D1. + * + * 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 (xfd && test_thread_flag(TIF_NEED_FPU_LOAD)) + fpstate->regs.xsave.header.xfeatures &=3D ~xfd; + + fpstate->xfd =3D xfd; + if (fpstate->in_use) + xfd_update_state(fpstate); + fpregs_unlock(); } EXPORT_SYMBOL_FOR_KVM(fpu_update_guest_xfd); @@ -430,6 +449,13 @@ int fpu_copy_uabi_to_guest_fpstate(struct fpu_guest *g= fpu, const void *buf, if (ustate->xsave.header.xfeatures & ~xcr0) return -EINVAL; =20 + /* + * Disabled features must be in their initial state, otherwise XRSTOR + * causes an exception. + */ + if (WARN_ON_ONCE(ustate->xsave.header.xfeatures & kstate->xfd)) + return -EINVAL; + /* * Nullify @vpkru to preserve its current value if PKRU's bit isn't set * in the header. KVM's odd ABI is to leave PKRU untouched in this diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ff8812f3a129..c0416f53b5f5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -5807,9 +5807,18 @@ static int kvm_vcpu_ioctl_x86_get_xsave(struct kvm_v= cpu *vcpu, static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu, struct kvm_xsave *guest_xsave) { + union fpregs_state *xstate =3D (union fpregs_state *)guest_xsave->region; + if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; =20 + /* + * Do not reject non-initialized disabled features for backwards + * compatibility, but clear XSTATE_BV[i] whenever XFD[i]=3D1. + * Otherwise, XRSTOR would cause a #NM. + */ + xstate->xsave.header.xfeatures &=3D ~vcpu->arch.guest_fpu.fpstate->xfd; + return fpu_copy_uabi_to_guest_fpstate(&vcpu->arch.guest_fpu, guest_xsave->region, kvm_caps.supported_xcr0, --=20 2.52.0