From nobody Tue Feb 10 03:45:11 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 200DB2772D for ; Wed, 24 Dec 2025 00:12:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766535181; cv=none; b=cPPGxcOi5boDZBxrU5JiMuigA1x0qG/FA/7Fb6vgeE++WdBSjiKw7afHjDvTvVXPP+f9GYasGJwiKaWXWesDSZ2/vv3/Q3iIySCT4euMwUsIZ3H9SYLBwNAe707E0pwiA7Nx2ctBKGHr3WtSzK45oQ1lzMSo+C4Zy28w8JmVZ7Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766535181; c=relaxed/simple; bh=hWlCqxxO4rNeS3u3z5E066XItSLKm9DA4Bx5w9CplKo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FgPD6sPFWj08tlb2ZgwZ9w/noYn3eBqy8tGYKSxFosjxe/iNZIPlayz3LYZqNJ6luRv++PJKPSiOiDKnWwuS06xbWFpB+bNibuQzlEZxiYAsMKdii7r8KAl2Anw4X8U3HTp5WrvtscjsF0MrNposyCAdqFE1o9jhDPYLoZsPtPM= 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=DjXFD2Xi; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=mT79PEif; arc=none smtp.client-ip=170.10.129.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="DjXFD2Xi"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="mT79PEif" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1766535177; 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=Wpa7TThaJI94/M4NKCUGmC4bcd1iUcwxLhMpe/Pmu+I=; b=DjXFD2XiDCi8PdwpIJykKnJjaav3BdgcAPU8mYtDTkE5mEXdX8hE1dkL7fZzp+ejz9SfW+ GeruasQIPXa+SA3bdNoCCfxb8b8PsCDDl50xD/Z8MoeNc9ebFwwHmApjkEPbeQ/eiGyZhZ IyNvGLMgyLe7Va4bWKcgAxmhzb2tr38= 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-607-2KEi11pvOQWwbYd0BOXrWQ-1; Tue, 23 Dec 2025 19:12:56 -0500 X-MC-Unique: 2KEi11pvOQWwbYd0BOXrWQ-1 X-Mimecast-MFC-AGG-ID: 2KEi11pvOQWwbYd0BOXrWQ_1766535175 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-477cf25ceccso62876495e9.0 for ; Tue, 23 Dec 2025 16:12:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1766535174; x=1767139974; 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=Wpa7TThaJI94/M4NKCUGmC4bcd1iUcwxLhMpe/Pmu+I=; b=mT79PEifZOE7VN1nrXdpzsqaA4NEd1eDjFI5JrOBCzE8YsHk/JesPwSGBq1i07jdXq KQTx7dlYz7k9JElo+T8lgTqJT6z/KRJnCeQaAP9jfn/Pv44xM/sAnkJAVAcPiFbemt9q PgwYrwL5Oo5IZp7jM4oGsLUL3genNE85QtV7N6r4WFoVXaVUvbWzJcl1v5yIwYgS0ZaH Ob4HqMISAqGDkNx//JSNH6ogNo85gLlQSGruzbSc/nJO6IUXv7lkCCtp5MNtm/vqUd9K Tjxjy5Qho6QeyvKx9tQT8Jcg8fwTkvuHKdTRBAPjXSltSPj7HKy49RfhuBnw5AfEkv19 UHYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766535174; x=1767139974; 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=Wpa7TThaJI94/M4NKCUGmC4bcd1iUcwxLhMpe/Pmu+I=; b=QcHcgJg3sic38ds0wCqlLRvDmTKe0M/8yXXsmMhfTcbwTD8BmEVDQXE+XWQedjrcjZ y5EiL7Zznl95WR4T6i1NeIuHK+NGgF/JKEOAwdgYmXZgZ+dFRXz3olNrNW+CP9a7V8xx /pjuuXza7fN7kWSyzez5QMesCeFCpW0O4Zp6eGSoiBxS6mtDBW9asXx4RxfDiIg5rlj1 F9BwvEYHB5jgde5G6LIrBms2nkdNYHmLyeA2+slxtZ58tb8QN4CimChPNJS82fsoNR2w qFWpHcrVKqU1W+ZbQDGVDe13Hret/Wal/ZJNrPra+LOglq5ap98t1qxNFDInSpakwmpQ argg== X-Gm-Message-State: AOJu0YzhEORI+iByaVHBl36xPtDqUq6SVH7A3CLm2sEmffToNfLcayjF cpDUgGzQ9Mk1N+RN3/u/ObSk4Ks4oRO/vB7XWnS/6sGnyvwd3lVluirgodHpQhXOaQr0E8EDta0 I6HOLlGkiMfvvgzcx7dfdQlKyG2FhoEoeoZhau9vpCaf1dyCUbCRGkn6fySHp0C/RRUUhBdyaf1 dMp2xByWS3BIcSfvK7dcu4+iblFXGYiSGUA1z4WOjteHrKksKD7w== X-Gm-Gg: AY/fxX6JVKo0DdSXcOKhrEREaujn9vI6Nx/v1PgNVJJYHmUo3NnsffVzZuaEqu7QWeb 6aBjCTdKCqrduWgwq42aUUYBHChd+N4Ze1s6bWhwxlebOacuwsviyQdZMnQn4QTPMIJZ0Wz5ZM7 wVNGiLrlQqJIarUcuoNdQdd8kieHa1wd5cVv8cWnM7EdvBBRWVo9hldDJRpPB1OC1n2gkq2Y5AX INiN7uYxqI1UHK5nb9g1lqvKBnOGpPuMTq0/h/gKiywoCHSQtTI/Gdr4uNJ76ImOSmRrN8qL52t PO4jzUwfESQKgmvurkHg9rmFGb/odBAy9JVoErMs+Q4KoB4hNxYbavNWdFcJ8SfBlX4plc2+7yW R6q1jUZQHLME4HkWJU5isYmOGz+Mjd6ZFwymZpFMgr1mUINDRpIFdUorhAw10QXZeL88kNWlTR+ wtJhb5ReAKrpgn91I= X-Received: by 2002:a05:600c:4709:b0:477:9aeb:6a8f with SMTP id 5b1f17b1804b1-47d220b7f4dmr127797295e9.9.1766535174474; Tue, 23 Dec 2025 16:12:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IF4shwSxKh09RXUVkp89RPVC5AHTfJRsb/m35JDsGHtZiPzxZLORNyA9500LRq6dFLONLymXg== X-Received: by 2002:a05:600c:4709:b0:477:9aeb:6a8f with SMTP id 5b1f17b1804b1-47d220b7f4dmr127797055e9.9.1766535174069; Tue, 23 Dec 2025 16:12:54 -0800 (PST) Received: from [192.168.10.48] ([151.95.145.106]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47be279c5f8sm302090195e9.9.2025.12.23.16.12.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Dec 2025 16:12:52 -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/5] x86, fpu: introduce fpu_load_guest_fpstate() Date: Wed, 24 Dec 2025 01:12:45 +0100 Message-ID: <20251224001249.1041934-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20251224001249.1041934-1-pbonzini@redhat.com> References: <20251224001249.1041934-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" Create a variant of fpregs_lock_and_load() that KVM can use in its vCPU entry code after preemption has been disabled. While basing it on the existing logic in vcpu_enter_guest(), ensure that fpregs_assert_state_consistent() always runs and sprinkle a few more assertions. Cc: stable@vger.kernel.org Fixes: 820a6ee944e7 ("kvm: x86: Add emulation for IA32_XFD", 2022-01-14) Signed-off-by: Paolo Bonzini --- arch/x86/include/asm/fpu/api.h | 1 + arch/x86/kernel/fpu/core.c | 17 +++++++++++++++++ arch/x86/kvm/x86.c | 8 +------- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/fpu/api.h b/arch/x86/include/asm/fpu/api.h index cd6f194a912b..0820b2621416 100644 --- a/arch/x86/include/asm/fpu/api.h +++ b/arch/x86/include/asm/fpu/api.h @@ -147,6 +147,7 @@ extern void *get_xsave_addr(struct xregs_state *xsave, = int xfeature_nr); /* KVM specific functions */ extern bool fpu_alloc_guest_fpstate(struct fpu_guest *gfpu); extern void fpu_free_guest_fpstate(struct fpu_guest *gfpu); +extern void fpu_load_guest_fpstate(struct fpu_guest *gfpu); extern int fpu_swap_kvm_fpstate(struct fpu_guest *gfpu, bool enter_guest); extern int fpu_enable_guest_xfd_features(struct fpu_guest *guest_fpu, u64 = xfeatures); =20 diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index 3ab27fb86618..a480fa8c65d5 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -878,6 +878,23 @@ void fpregs_lock_and_load(void) fpregs_assert_state_consistent(); } =20 +void fpu_load_guest_fpstate(struct fpu_guest *gfpu) +{ +#ifdef CONFIG_X86_DEBUG_FPU + struct fpu *fpu =3D x86_task_fpu(current); + WARN_ON_ONCE(gfpu->fpstate !=3D fpu->fpstate); +#endif + + lockdep_assert_preemption_disabled(); + if (test_thread_flag(TIF_NEED_FPU_LOAD)) + fpregs_restore_userregs(); + + fpregs_assert_state_consistent(); + if (gfpu->xfd_err) + wrmsrq(MSR_IA32_XFD_ERR, gfpu->xfd_err); +} +EXPORT_SYMBOL_FOR_KVM(fpu_load_guest_fpstate); + #ifdef CONFIG_X86_DEBUG_FPU /* * If current FPU state according to its tracking (loaded FPU context on t= his diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ff8812f3a129..01d95192dfc5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11300,13 +11300,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) kvm_make_request(KVM_REQ_EVENT, vcpu); } =20 - fpregs_assert_state_consistent(); - if (test_thread_flag(TIF_NEED_FPU_LOAD)) - switch_fpu_return(); - - if (vcpu->arch.guest_fpu.xfd_err) - wrmsrq(MSR_IA32_XFD_ERR, vcpu->arch.guest_fpu.xfd_err); - + fpu_load_guest_fpstate(&vcpu->arch.guest_fpu); kvm_load_xfeatures(vcpu, true); =20 if (unlikely(vcpu->arch.switch_db_regs && --=20 2.52.0