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=1796407460; b=AiXJsPXmCO/rjg8EVFY7zYemE+J+VoIiYSut4tl4rIPAvMSZ02xkyZcCm7+dSTuf//sJ WTsAl8Lcj6jtsmp+rYpBUHSDVVDXP07tlK6gYvLWJUr2+j3hSvcY8UCb5lLc00QehHHYB m4qpH2x6jG7kGE5/Di0JbqtDGJXGFtJot3yjoI0PFE3UJM4InTt/PKmu632DjRu0IZd/4 lBYPKzpSvaPX9VCFk/EUuJ2Cdeq3gp9+DBbAHAnH3bEForEPnMHnC2NQYsP3eDyuX99VO 7OvMCPEJLcEhEcXTs7L8Y/ZyJq4sQbIC9anWU+AxFZuMuhPJmdHUjoUFWWPzalL9imA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407460; 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=aDbQXtzeXc3DhX+8EUAEGUSqItfwjq9gQt76oKaUXGY=; b=yX/HU8UHBpi9KSdtb14VqKwwBixWnNOt6q7UO052IWz+k+RJ8UGyObMkRMVpt60w1Qbm zVosLLWOs5s+S6eFNI8n0TGfr2usLoTZcBsaeNaJTzmrORo4iEaBwBcAZvkRes1xnOAu7 Lx5dv1IeYegAjZe72A4wMMfBkTz2Rj7HR5iFv1xqwSaIKjwaah7+6bfMxCdqTrIR6KbNT 7k3ZEdIywVq5Sz4aTf09nz+T4VmhbxqYd8QGI6E0R6vgp9tJW2Q2sVmso7xypujSWnqyM IMW9ToHiXINhCGicsGHUjxG5HiUmwDsnQ6NNE95dCYs1ENnFhLv8xmXmOXq/W5bMDTw== 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=KAcjRtf2; 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 0C77760A6F56 for ; Fri, 4 Dec 2026 18:04:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0C77760A6F56 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=KAcjRtf2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407460; 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=aDbQXtzeXc3DhX+8EUAEGUSqItfwjq9gQt76oKaUXGY=; b=KAcjRtf2C8VT2pKjEsXNN0zplsbD3xutMOA78dFiuUNjxtXG/Gt/UWOUSryEzXUSJjboMt SDWSLiIQJizTW1wFlxtOoFMEfG1tN6+43PAlToZctWclDQgTr/7UipDYAOAoKol+81Utmk yA3nhRes/hM7aSPlHz6CmGMPjg3eadw= 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-612-URsRbeBdPJylECGdSbeFuQ-1; Fri, 4 Dec 2026 18:04:14 +0000 X-MC-Unique: URsRbeBdPJylECGdSbeFuQ-1 X-Mimecast-MFC-AGG-ID: URsRbeBdPJylECGdSbeFuQ_1796407452 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (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 584B1189C2D0; Fri, 4 Dec 2026 18:04:12 +0000 (UTC) Received: from pbonzini-thinkpadx1carbon6th.rmtit.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D0B1318019CC; Fri, 4 Dec 2026 18:04:09 +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.6.y 16/17] x86/fpu: Ensure guest FPU is resident in hardware when updating actual XFD MSR Date: Fri, 4 Dec 2026 19:03:36 +0100 Message-ID: <20261204180337.2168031-17-pbonzini@redhat.com> In-Reply-To: <20261204180337.2168031-1-pbonzini@redhat.com> References: <20261204180337.2168031-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.4.1 on 10.30.177.93 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 84822f9e2dc4..f87d279be54b 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -319,19 +319,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