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=1796407219; b=5NBT9mpfaaXYkqO2ILYJ9UEuMwdTK4Wzl21Ew8h3pbxKsJK0vYI9hc9Btd3bb9x9DKqz Cfk1ALbycK8X8c0/s6f0WJ9di2ZVe2ROGNktc15yhGzzNYBBMA0QxTryNLADjxk4Hxcat BL1jsvwkOEkB7Wqm00MQRCFe07r13HzNUyAIs3+QvuyVEAXJeJTyjVX0Abm81RyFdR5jT k8jhm4nZNfNZ0ouvhKxDRLyrSdTLv5PeeKaoP8oXV4xnyww1fOzSQUQh63m/gkbbPU68P ud+X6RqS69uqmumegO1LPO7UrZS9sN9h5mLsLQVPbwo1zHxrEhcUvua79zfezGwgGOA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407219; 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=5EYYtSnKnckCoSP0+DOsh0hoEHZO9VXxAXggmcMKi0I=; b=2dw2QEeiX0a4LZyf9NrPJVsF039rvKW8B/a6/1flXZHLm7aWxRfRvm7VWfAc+1mMhCM4 WipnU8kTclQL6ZhGmL0p/WyDFpkhYAfmYo4YW31upKUiBs9t/3Sv7VJ/tvSu6bMvGCWa2 KmQZxxwx32O3aKJKbXBkZ+Ry03zvLyRrpyDtoUi2lKaIU98t8Gjbdbf19tPZzPYw6KGlA 1R7AOkMwd3ba9bfL9oEyP3kjxK6lG1DJefRGzTqxiTzpxq2TG3GyNKUJ1x/Q2shoRSs9Q CVmR/SY/5ZodUT/gtAZP2a9V7ISXm8oy/lUJ7flBIFsIRJhMXxvhaKcF7cvWfmjlfXg== 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=Pm7DqaOm; 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 0FBC860A260D for ; Fri, 4 Dec 2026 18:00:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0FBC860A260D 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=Pm7DqaOm DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407219; 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=5EYYtSnKnckCoSP0+DOsh0hoEHZO9VXxAXggmcMKi0I=; b=Pm7DqaOm4mlXIS/XsBp6jN/piIeVtpNnlX31SKkpqS/YfFD4ZNYNrtOAhLnrw5dQGRQZaU gofunp17s/exJyu2uAw//3u2l/v2XM4LPBctqIhHgWZpwsQe9fD11hgbfpx0FQiGv8MC6y V7iOKBkbPpegq5EM8+fLjFWRPv/39GI= 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-599-i2VXDbMCNSSGTSvxiJPj0g-1; Fri, 4 Dec 2026 18:00:13 +0000 X-MC-Unique: i2VXDbMCNSSGTSvxiJPj0g-1 X-Mimecast-MFC-AGG-ID: i2VXDbMCNSSGTSvxiJPj0g_1796407207 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 06DC218B95F8; Fri, 4 Dec 2026 18:00:07 +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 4149FBB9; Fri, 4 Dec 2026 18:00:04 +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 16/17] x86/fpu: Ensure guest FPU is resident in hardware when updating actual XFD MSR Date: Fri, 4 Dec 2026 18:59:29 +0100 Message-ID: <20261204175930.2154542-17-pbonzini@redhat.com> In-Reply-To: <20261204175930.2154542-1-pbonzini@redhat.com> References: <20261204175930.2154542-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 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 --- 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 2b81d3d8012c..dade99c5fc79 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -357,19 +357,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