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=1796407304; b=OzcScc4a/6WrZN9Ll8Zcp+byU99czliLTbNiEjofNz5eyi+0/LGkC4PwXyCIJsLsecMJ Zw643zGUc2BULMfWO6lgG22Dxle6mhAU6Gv3tKbRMUXYmEySLs0tayDKmM+Gbz8Tu3scu ZhSKF7HgVbG8PzcVaRXz9mm+cs0cRV0r9MTlycftFJJQmfjbP5cwRtIMSkePxUbJc+/zx 4AwA+2v0PZDKn/Jb+5m8kQ5EWFlkT/LWZw/4k8hC3zNRM6DF2us84FzAr6Jktbl9FiGlZ eF1hQmjWQN8wqPOYGHfpD+wHOiHVsZMaw5Gnf/HQg2jKTTPeQFuFuCsZST6gKk6qgiA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407304; 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=X0VXli39/gry5JeOsPgku/du+DpE+NjCIjRHYW8J09o=; b=U2BGnrqiMyUzgwahF5K1X29yVg8eQFNEUCajWAVBOdp8eD2P7A9k5xmPF5+88AzCmfVZ NBL+8i5fyAUxBo944yrzljCcDMsRPUZnRZHKF1inqfKLmm5gV9zMYeAEepoZ19DWMiPxp 8zjdgt0yQR+7sdKcZzcqryaIr31RV6+a0HT+NkVcldvFZDoMcQsG0Gsmt7Zu6gom1ODon PaiLy1HuNk4pzqfSJqcW/rbdb2dmvm56cTDd07Kjtxpks9rrdBy+FO3OGc5u8Bayc8Ozf oQnfpLiEL6dQONKaM5lhAmieapfDO3uv+tNfwQLp4mLVSJ0VMbu+v8T0rAKlUMB12LQ== 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=KtiAu5Id; 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 B94DE60A6F70 for ; Fri, 4 Dec 2026 18:01:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B94DE60A6F70 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=KtiAu5Id DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407303; 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=X0VXli39/gry5JeOsPgku/du+DpE+NjCIjRHYW8J09o=; b=KtiAu5IdWqdTq0nOefMnISwp5SunNZ0AqnO90H+PH3Pm3kzLE79QuWjB2CVojJddkwxkmO gQMJOGapsUGnhIFvAkYSs3lntXBh5WqmmV0T6jip3Qc3ehQtmey9L+EWlZMzBHwLDEF3Jb nw8hr4gXCnalFEf6BRAjAyni1xYCG1E= Received: from mx-prod-mc-08.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-515-UYMJImZrMWm3wzEywJuDTg-1; Fri, 4 Dec 2026 18:01:36 +0000 X-MC-Unique: UYMJImZrMWm3wzEywJuDTg-1 X-Mimecast-MFC-AGG-ID: UYMJImZrMWm3wzEywJuDTg_1796407294 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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A809C18BC7F6; Fri, 4 Dec 2026 18:01:34 +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 276101955E83; Fri, 4 Dec 2026 18:01:31 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Sean Christopherson , stable@vger.kernel.org, Stefan Teodorescu , Chris Mason , Josef Bacik , David Woodhouse , Jim Mattson , Dave Hansen Subject: [PATCH 6.18.y 16/17] x86/fpu: Ensure guest FPU is resident in hardware when updating actual XFD MSR Date: Fri, 4 Dec 2026 19:00:58 +0100 Message-ID: <20261204180059.2165490-17-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 98d7c3da6a83babc4b209656411adde144ba9ece ] When updating a guest FPU's XFD, ensure the guest FPU state is resident in hardware. Failure to do so means updating XFD may effectively clobber XFD, and in the worst case scenario can lead to an #NM due to failing to update XFD. Specifically, if the guest FPU is in-use (associated with the current task) and the current task has TIF_NEED_FPU_LOAD, then lock and load the task's FPU regs. The underlying issue is that TIF_NEED_FPU_LOAD doesn't actually mean the FPU isn't resident in hardware, it only says the FPU *might* not be resident in hardware and *may* need a reload. E.g. if the current task was tagged with TIF_NEED_FPU_LOAD by switch_fpu(), but never evicted from hardware, then KVM's call to switch_fpu_return() will elide the xfd_update_state() call (and the XRSTOR) due to the FPU being valid, and run the vCPU with a stale XFD. E.g. on a single pCPU 'A', timeshared by two vCPUs: vcpu0 / fpu0 vcpu1 / fpu1 TIF_NEED_FPU_LOAD = 1 dirty TMM0 (XINUSE[18] == 1) wrmsr(MSR_IA32_XFD, 0x40000) fpu_sync_guest_vmexit_xfd_state() TIF_NEED_FPU_LOAD = 1 (switch_fpu()) fpu_fpregs_owner_ctx = fpu0 fpu0->last_cpu = A fpu_update_guest_xfd(fpu1, 0) wrmsrq(MSR_IA32_XFD, 0) fpregs_state_valid() == true | -> switch_fpu_return() skips restore_fpregs_from_fpstate() and xfd_update_state() fpu_swap_kvm_fpstate() (exit to user) | -> fpu0->regs.xsave.header.xfeatures is saved with XSTATE_BV[18] = 1 fpu_swap_kvm_fpstate() (entry from user) | -> restore_fpregs_from_fpstate() => #NM Alternatively, fpu_update_guest_xfd() could skip xfd_update_state() *and* force an invalidation of the current FPU (set last_cpu = -1) to ensure the correct XFD is loaded when doing switch_fpu_return(). However, that gets all kinds of convoluted, and emulating an XFD write with TIF_NEED_FPU_LOAD=1 is very uncommon, and in all likelihood the guest FPU is going to be loaded into hardware in the near future anyways, i.e. avoiding (maybe) loading the guest FPU buys nothing but complexity. Fixes: b45f72177594 ("x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state whenever XFD[i]=1") Cc: stable@vger.kernel.org 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 Signed-off-by: Sean Christopherson Link: https://patch.msgid.link/20261119001506.64568-4-seanjc@google.com Signed-off-by: Paolo Bonzini Signed-off-by: Paolo Bonzini --- arch/x86/kernel/fpu/core.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index c098cb1d9215..3d38d4645716 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -343,19 +343,18 @@ void fpu_update_guest_xfd(struct fpu_guest *guest_fpu, u64 xfd) } /* - * 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. - * - * 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 the guest's FPU state is in-use, then ensure the state is resident + * in hardware and simply update XFD. The guest is responsible for + * clearing disabled components when doing its own XRSTOR operations, + * and the XSAVE in fpu_swap_kvm_fpstate() when putting the guest FPU + * will clear XSTATE_BV[i] whenever XFD[i]=1. + */ + fpregs_lock_and_load(); + /* + * Update XFD *after* loading FPU state, otherwise XRSTOR would try to + * load XSTATE saved with the old XFD, but using the new XFD. */ - fpregs_lock(); fpstate->xfd = xfd; - - if (xfd && test_thread_flag(TIF_NEED_FPU_LOAD)) - fpstate->regs.xsave.header.xfeatures &= ~xfd; - xfd_update_state(fpstate); fpregs_unlock(); } -- 2.56.1