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=1796407518; b=eCWlhV2U4Vq69Aj94Cz+zTlzB5Kv3mftIKDuwVk11d/TK7d4aMUH5nWPYVlAXCqhlxEB 6nnab1defv+eeizDzJRxWa396o5KIukLIj6aJs3qpT/k/MikbTn55OX5xGWuyqtYkDEpA tX9Zo4mwfln+h65TV6k5VPE80Lxg8Gx6Yd6Wc2HJi/ommFPlP78O0KdnQvziAbU0Z6Fnr pybpzufCSVsYMnBtpDYzbE9saXSYOTPhMTdhiQHHcinGAJwzFLRvE8u0tYruC1yoRd+Eg vNrl01JFPkqS3bmSvSjoop48syQloZpQZUfFGQES2UZxLuLR1pRONeXKTf45uDpP2OQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407518; 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=ghxCOVJje862/nkJrJ0SP1lWCIE4hR8jBdhgmzixIB4=; b=q2e8qlU+OG+cwX2Z5d/HmSIJ5+6eJbUrPQ00rKOGHMFg8nUtmQKOuOrKA/143gS2qI2R vkeI/eJLj5esYRUbfxA1tecEQ37GSYC4AolF+tIjpUSDr2ZhKsVingnvTZYrW8VLYl/NU +nVi5YfDMpo8WCwN1uQ4F+UOhCppKw7MKDii5qIQh/IIVKH1AVh3dv9s5wmfGudOKHSCM XEBhURxhBB77MoHhq9FopyA3iJ/PNbFUShXQ4FXxrIyo6MKoZzXXuIttO8Ec3w+RYPMUh AHoGGF/eVdQqJnP2UYFIJ0AoQn76YiIr7aPCq+WqbbX8g/w/jhksB6nIDM5c8zKsV0Q== 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=QZOIVQuM; 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 EECD5606A68E for ; Fri, 4 Dec 2026 18:05:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EECD5606A68E 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=QZOIVQuM DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407518; 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=ghxCOVJje862/nkJrJ0SP1lWCIE4hR8jBdhgmzixIB4=; b=QZOIVQuMjqYHLuCcEfvO0zCA4UL57cszBvibo2KK0Ccn6byq43ApdDH9tMYWL/sog54LQY Ekaie5M9YhOosmH//wI52MzuO33YYyANlGZ63zAcfAau4KGNAI05l8kBMnqRmFt5f6Wc0Q UoOYPXafD/IUAJXPh2FtAEro4OuHDOk= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS with 8BITMIME (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-635-_7u3VcedPP2LFncJ3ZgX1g-1; Fri, 4 Dec 2026 18:05:13 +0000 X-MC-Unique: _7u3VcedPP2LFncJ3ZgX1g-1 X-Mimecast-MFC-AGG-ID: _7u3VcedPP2LFncJ3ZgX1g_1796407511 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3C38F19670C3; Fri, 4 Dec 2026 18:05:11 +0000 (UTC) Received: from pbonzini-thinkpadx1carbon6th.rmtit.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B64043003EA7; Fri, 4 Dec 2026 18:05:08 +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.1.y 23/24] x86/fpu: Ensure guest FPU is resident in hardware when updating actual XFD MSR Date: Fri, 4 Dec 2026 19:04:22 +0100 Message-ID: <20261204180423.2168641-24-pbonzini@redhat.com> In-Reply-To: <20261204180423.2168641-1-pbonzini@redhat.com> References: <20261204180423.2168641-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.4 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 99e910ebedc4..f21e42de9c25 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