kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jing Liu <jing2.liu@intel.com>
To: x86@kernel.org, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, tglx@linutronix.de,
	mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com,
	pbonzini@redhat.com
Cc: seanjc@google.com, jun.nakajima@intel.com, kevin.tian@intel.com,
	jing2.liu@linux.intel.com, jing2.liu@intel.com,
	guang.zeng@intel.com, wei.w.wang@intel.com, yang.zhong@intel.com
Subject: [PATCH v2 22/23] kvm: x86: Disable interception for IA32_XFD on demand
Date: Fri, 17 Dec 2021 07:30:02 -0800	[thread overview]
Message-ID: <20211217153003.1719189-23-jing2.liu@intel.com> (raw)
In-Reply-To: <20211217153003.1719189-1-jing2.liu@intel.com>

Always intercepting IA32_XFD causes non-negligible overhead when this
register is updated frequently in the guest.

Disable r/w emulation after intercepting the first WRMSR(IA32_XFD)
with a non-zero value.

Disable WRMSR emulation implies that IA32_XFD becomes out-of-sync
with the software states in fpstate and the per-cpu xfd cache. Call
fpu_sync_guest_vmexit_xfd_state() to bring them back in-sync, before
preemption is enabled.

p.s. We have confirmed that SDM is being revised to say that
when setting IA32_XFD[18] the AMX register state is not guaranteed
to be preserved. This clarification avoids adding mess for a creative
guest which sets IA32_XFD[18]=1 before saving active AMX state to
its own storage.

Signed-off-by: Kevin Tian <kevin.tian@intel.com>
Signed-off-by: Yang Zhong <yang.zhong@intel.com>
Signed-off-by: Jing Liu <jing2.liu@intel.com>
---
 arch/x86/include/asm/kvm-x86-ops.h |  1 +
 arch/x86/include/asm/kvm_host.h    |  2 ++
 arch/x86/kvm/vmx/vmx.c             | 10 ++++++++++
 arch/x86/kvm/vmx/vmx.h             |  2 +-
 arch/x86/kvm/x86.c                 |  6 ++++++
 5 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h
index cefe1d81e2e8..60c27f9990e9 100644
--- a/arch/x86/include/asm/kvm-x86-ops.h
+++ b/arch/x86/include/asm/kvm-x86-ops.h
@@ -30,6 +30,7 @@ KVM_X86_OP(update_exception_bitmap)
 KVM_X86_OP(get_msr)
 KVM_X86_OP(set_msr)
 KVM_X86_OP(get_segment_base)
+KVM_X86_OP_NULL(set_xfd_passthrough)
 KVM_X86_OP(get_segment)
 KVM_X86_OP(get_cpl)
 KVM_X86_OP(set_segment)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 860ed500580c..b513c12fa39e 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -640,6 +640,7 @@ struct kvm_vcpu_arch {
 	u64 smi_count;
 	bool tpr_access_reporting;
 	bool xsaves_enabled;
+	bool xfd_out_of_sync;
 	u64 ia32_xss;
 	u64 microcode_version;
 	u64 arch_capabilities;
@@ -1329,6 +1330,7 @@ struct kvm_x86_ops {
 	void (*update_exception_bitmap)(struct kvm_vcpu *vcpu);
 	int (*get_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr);
 	int (*set_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr);
+	void (*set_xfd_passthrough)(struct kvm_vcpu *vcpu);
 	u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
 	void (*get_segment)(struct kvm_vcpu *vcpu,
 			    struct kvm_segment *var, int seg);
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 483075045253..97a823a3f23f 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -162,6 +162,7 @@ static u32 vmx_possible_passthrough_msrs[MAX_POSSIBLE_PASSTHROUGH_MSRS] = {
 	MSR_FS_BASE,
 	MSR_GS_BASE,
 	MSR_KERNEL_GS_BASE,
+	MSR_IA32_XFD,
 #endif
 	MSR_IA32_SYSENTER_CS,
 	MSR_IA32_SYSENTER_ESP,
@@ -1929,6 +1930,14 @@ static u64 vcpu_supported_debugctl(struct kvm_vcpu *vcpu)
 	return debugctl;
 }
 
+#ifdef CONFIG_X86_64
+static void vmx_set_xfd_passthrough(struct kvm_vcpu *vcpu)
+{
+	vmx_disable_intercept_for_msr(vcpu, MSR_IA32_XFD, MSR_TYPE_RW);
+	vcpu->arch.xfd_out_of_sync = true;
+}
+#endif
+
 /*
  * Writes msr value into the appropriate "register".
  * Returns 0 on success, non-0 otherwise.
@@ -7664,6 +7673,7 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = {
 #ifdef CONFIG_X86_64
 	.set_hv_timer = vmx_set_hv_timer,
 	.cancel_hv_timer = vmx_cancel_hv_timer,
+	.set_xfd_passthrough = vmx_set_xfd_passthrough,
 #endif
 
 	.setup_mce = vmx_setup_mce,
diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h
index 4df2ac24ffc1..bf9d3051cd6c 100644
--- a/arch/x86/kvm/vmx/vmx.h
+++ b/arch/x86/kvm/vmx/vmx.h
@@ -340,7 +340,7 @@ struct vcpu_vmx {
 	struct lbr_desc lbr_desc;
 
 	/* Save desired MSR intercept (read: pass-through) state */
-#define MAX_POSSIBLE_PASSTHROUGH_MSRS	13
+#define MAX_POSSIBLE_PASSTHROUGH_MSRS	14
 	struct {
 		DECLARE_BITMAP(read, MAX_POSSIBLE_PASSTHROUGH_MSRS);
 		DECLARE_BITMAP(write, MAX_POSSIBLE_PASSTHROUGH_MSRS);
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 796a9f2d1f23..13ba1e066b55 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -3686,6 +3686,9 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 			return 1;
 
 		fpu_update_guest_xfd(&vcpu->arch.guest_fpu, data);
+
+		if (data && kvm_x86_ops.set_xfd_passthrough)
+			static_call(kvm_x86_set_xfd_passthrough)(vcpu);
 		break;
 	case MSR_IA32_XFD_ERR:
 		if (!msr_info->host_initiated &&
@@ -10023,6 +10026,9 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	if (vcpu->arch.guest_fpu.xfd_err)
 		wrmsrl(MSR_IA32_XFD_ERR, 0);
 
+	if (vcpu->arch.xfd_out_of_sync)
+		fpu_sync_guest_vmexit_xfd_state();
+
 	/*
 	 * Consume any pending interrupts, including the possible source of
 	 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
-- 
2.27.0


  parent reply	other threads:[~2021-12-17 15:30 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-17 15:29 [PATCH v2 00/23] AMX Support in KVM Jing Liu
2021-12-17 15:29 ` [PATCH v2 01/23] x86/fpu: Extend fpu_xstate_prctl() with guest permissions Jing Liu
2021-12-17 18:45   ` Thomas Gleixner
2021-12-17 18:47     ` Thomas Gleixner
2021-12-17 15:29 ` [PATCH v2 02/23] x86/fpu: Prepare guest FPU for dynamically enabled FPU features Jing Liu
2021-12-17 15:29 ` [PATCH v2 03/23] kvm: x86: Fix xstate_required_size() to follow XSTATE alignment rule Jing Liu
2021-12-17 15:29 ` [PATCH v2 04/23] kvm: x86: Exclude unpermitted xfeatures at KVM_GET_SUPPORTED_CPUID Jing Liu
2021-12-20 13:20   ` Paolo Bonzini
2021-12-17 15:29 ` [PATCH v2 05/23] kvm: x86: Check permitted dynamic xfeatures at KVM_SET_CPUID2 Jing Liu
2021-12-17 15:29 ` [PATCH v2 06/23] x86/fpu: Make XFD initialization in __fpstate_reset() a function argument Jing Liu
2021-12-17 15:29 ` [PATCH v2 07/23] x86/fpu: Add guest support to xfd_enable_feature() Jing Liu
2021-12-17 15:29 ` [PATCH v2 08/23] x86/fpu: Provide fpu_update_guest_perm_features() for guest Jing Liu
2021-12-17 18:50   ` Thomas Gleixner
2021-12-17 15:29 ` [PATCH v2 09/23] kvm: x86: Enable dynamic XSAVE features at KVM_SET_CPUID2 Jing Liu
2021-12-17 15:29 ` [PATCH v2 10/23] x86/fpu: Provide fpu_update_guest_xfd() for IA32_XFD emulation Jing Liu
2021-12-17 15:29 ` [PATCH v2 11/23] kvm: x86: Add emulation for IA32_XFD Jing Liu
2021-12-17 15:29 ` [PATCH v2 12/23] x86/fpu: Prepare xfd_err in struct fpu_guest Jing Liu
2021-12-17 15:29 ` [PATCH v2 13/23] kvm: x86: Intercept #NM for saving IA32_XFD_ERR Jing Liu
2021-12-17 15:29 ` [PATCH v2 14/23] kvm: x86: Emulate IA32_XFD_ERR for guest Jing Liu
2021-12-17 15:29 ` [PATCH v2 15/23] kvm: x86: Add XCR0 support for Intel AMX Jing Liu
2021-12-17 15:29 ` [PATCH v2 16/23] kvm: x86: Add CPUID " Jing Liu
2021-12-17 15:29 ` [PATCH v2 17/23] x86/fpu: add uabi_size to guest_fpu Jing Liu
2021-12-17 15:29 ` [PATCH v2 18/23] kvm: x86: Get/set expanded xstate buffer Jing Liu
2021-12-20  9:04   ` Paolo Bonzini
2021-12-21  2:45     ` Wang, Wei W
2021-12-21  8:45       ` Paolo Bonzini
2021-12-21  9:06         ` Wang, Wei W
2021-12-21  9:30           ` Paolo Bonzini
2021-12-17 15:29 ` [PATCH v2 19/23] kvm: selftests: Add support for KVM_CAP_XSAVE2 Jing Liu
2021-12-20 13:19   ` Paolo Bonzini
2021-12-17 15:30 ` [PATCH v2 20/23] docs: kvm: Add KVM_GET_XSAVE2 Jing Liu
2021-12-20  9:04   ` Paolo Bonzini
2021-12-17 15:30 ` [PATCH v2 21/23] x86/fpu: Provide fpu_sync_guest_vmexit_xfd_state() Jing Liu
2021-12-17 15:30 ` Jing Liu [this message]
2021-12-20  9:07   ` [PATCH v2 22/23] kvm: x86: Disable interception for IA32_XFD on demand Paolo Bonzini
2021-12-21  6:42     ` Liu, Jing2
2021-12-21  8:46       ` Paolo Bonzini
2021-12-17 15:30 ` [PATCH v2 23/23] kvm: x86: Disable RDMSR interception of IA32_XFD_ERR Jing Liu
2021-12-20  9:08   ` Paolo Bonzini
2021-12-21  6:29     ` Liu, Jing2
2021-12-21  8:48       ` Paolo Bonzini
2021-12-21  9:00         ` Liu, Jing2
2021-12-21  9:05           ` Paolo Bonzini
2021-12-17 18:52 ` [PATCH v2 00/23] AMX Support in KVM Thomas Gleixner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211217153003.1719189-23-jing2.liu@intel.com \
    --to=jing2.liu@intel.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=guang.zeng@intel.com \
    --cc=jing2.liu@linux.intel.com \
    --cc=jun.nakajima@intel.com \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=tglx@linutronix.de \
    --cc=wei.w.wang@intel.com \
    --cc=x86@kernel.org \
    --cc=yang.zhong@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).