Skip to contentSkip to conversation

KVM: SVM: Propagate Translation Cache Extensions to the guest

PATCH In review Yosry Ahmed 2 participants 7 messages Mar 6 – Mar 7

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: SVM: Propagate Translation Cache Extensions to the guest

From: Venkatesh Srinivas <[email protected]>

TCE augments the behavior of TLB invalidating instructions (INVLPG, INVLPGB, and INVPCID) to only invalidate translations for relevant intermediate mappings to the address range, rather than ALL intermdiate translations.

The Linux kernel has been setting EFER.TCE if supported by the CPU since commit 440a65b7d25f ("x86/mm: Enable AMD translation cache extensions"), as it may improve performance.

KVM does not need to do anything to virtualize the feature, only advertise it and allow setting EFER.TCE. Passthrough X86_FEATURE_TCE to the guest, and allow the guest to set EFER.TCE if available.

---
 arch/x86/kvm/cpuid.c   | 1 +
 arch/x86/kvm/svm/svm.c | 3 +++
 arch/x86/kvm/x86.c     | 3 +++
 3 files changed, 7 insertions(+)
arch/x86/kvm/cpuid.c +1 -0
index fffbf087937d4..4f810f23b1d9b 100644
@@ -1112,6 +1112,7 @@ void kvm_initialize_cpu_caps(void)
 		F(XOP),
 		/* SKINIT, WDT, LWP */
 		F(FMA4),
+		F(TCE),
 		F(TBM),
 		F(TOPOEXT),
 		VENDOR_F(PERFCTR_CORE),
arch/x86/kvm/svm/svm.c +3 -0
index 3407deac90bd6..fee1c8cd45973 100644
@@ -5580,6 +5580,9 @@ static __init int svm_hardware_setup(void)
 	if (boot_cpu_has(X86_FEATURE_AUTOIBRS))
 		kvm_enable_efer_bits(EFER_AUTOIBRS);
 
+	if (boot_cpu_has(X86_FEATURE_TCE))
+		kvm_enable_efer_bits(EFER_TCE);
+
 	/* Check for pause filtering support */
 	if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
 		pause_filter_count = 0;
arch/x86/kvm/x86.c +3 -0
index 879cdeb6adde2..7336ce1df3f7a 100644
@@ -1743,6 +1743,9 @@ static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
 	if (efer & EFER_NX && !guest_cpu_cap_has(vcpu, X86_FEATURE_NX))
 		return false;
 
+	if (efer & EFER_TCE && !guest_cpu_cap_has(vcpu, X86_FEATURE_TCE))
+		return false;
+
 	return true;
 
 }

base-commit: 5128b972fb2801ad9aca54d990a75611ab5283a9

Signature · 1 line
2.53.0.473.g4a7958ca14-goog
#1 lore raw
Quoted 7 lines · Yosry Ahmed

From: Venkatesh Srinivas <[email protected]>

TCE augments the behavior of TLB invalidating instructions (INVLPG, INVLPGB, and INVPCID) to only invalidate translations for relevant intermediate mappings to the address range, rather than ALL intermdiate translations.

Quoted 5 lines

The Linux kernel has been setting EFER.TCE if supported by the CPU since commit 440a65b7d25f ("x86/mm: Enable AMD translation cache extensions"), as it may improve performance.

KVM does not need to do anything to virtualize the feature,

Please back this up with actual analysis.

Quoted 1 line

only advertise it and allow setting EFER.TCE. Passthrough X86_FEATURE_TCE to

Advertise X86_FEATURE_TCE to userspace, not "passthrough xxx to the guest". Because that's all KVM

Quoted 5 lines

the guest, and allow the guest to set EFER.TCE if available.

Your SoB should come last to capture that the chain of hanlding, i.e. this should be:

Quoted 23 lines
---
 arch/x86/kvm/cpuid.c   | 1 +
 arch/x86/kvm/svm/svm.c | 3 +++
 arch/x86/kvm/x86.c     | 3 +++
 3 files changed, 7 insertions(+)
arch/x86/kvm/cpuid.c +1 -0
index fffbf087937d4..4f810f23b1d9b 100644
@@ -1112,6 +1112,7 @@ void kvm_initialize_cpu_caps(void)
 		F(XOP),
 		/* SKINIT, WDT, LWP */
 		F(FMA4),
+		F(TCE),
 		F(TBM),
 		F(TOPOEXT),
 		VENDOR_F(PERFCTR_CORE),
arch/x86/kvm/svm/svm.c +0 -0
index 3407deac90bd6..fee1c8cd45973 100644

arch/x86/kvm/svm/svm.c static __init int svm_hardware_setup(void) · line 5584

Quoted 5 lines
arch/x86/kvm/svm/svm.c +2 -0
@@ -5580,6 +5580,9 @@ static __init int svm_hardware_setup(void)
 	if (boot_cpu_has(X86_FEATURE_AUTOIBRS))
 		kvm_enable_efer_bits(EFER_AUTOIBRS);
 
+	if (boot_cpu_has(X86_FEATURE_TCE))
+		kvm_enable_efer_bits(EFER_TCE);

Hrm, I think we should handle all of the kvm_enable_efer_bits() calls that are conditioned only on CPU support in common code. While it's highly unlikely Intel CPUs will ever support more EFER-based features, if they do, then KVM will over-report support since kvm_initialize_cpu_caps() will effectively enable the feature, but VMX won't enable the corresponding EFER bit.

I can't think anything that will go sideways if we rely purely on KVM caps, so get to something like this as prep work, and then land TCE in common x86?

---
 arch/x86/kvm/svm/svm.c |  7 +------
 arch/x86/kvm/vmx/vmx.c |  4 ----
 arch/x86/kvm/x86.c     | 14 ++++++++++++++
 3 files changed, 15 insertions(+), 10 deletions(-)
arch/x86/kvm/svm/svm.c +1 -6
index 3407deac90bd..c23ee45f2ba8 100644
@@ -5556,14 +5556,10 @@ static __init int svm_hardware_setup(void)
 		pr_err_ratelimited("NX (Execute Disable) not supported\n");
 		return -EOPNOTSUPP;
 	}
-	kvm_enable_efer_bits(EFER_NX);
 
 	kvm_caps.supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS |
 				     XFEATURE_MASK_BNDCSR);
 
-	if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
-		kvm_enable_efer_bits(EFER_FFXSR);
-
 	if (tsc_scaling) {
 		if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
 			tsc_scaling = false;
@@ -5577,8 +5573,7 @@ static __init int svm_hardware_setup(void)
 
 	tsc_aux_uret_slot = kvm_add_user_return_msr(MSR_TSC_AUX);
 
-	if (boot_cpu_has(X86_FEATURE_AUTOIBRS))
-		kvm_enable_efer_bits(EFER_AUTOIBRS);
+
 
 	/* Check for pause filtering support */
 	if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
arch/x86/kvm/vmx/vmx.c +0 -4
index 9302c16571cd..2b8a7456039c 100644
@@ -8583,10 +8583,6 @@ __init int vmx_hardware_setup(void)
 
 	vmx_setup_user_return_msrs();
 
-
-	if (boot_cpu_has(X86_FEATURE_NX))
-		kvm_enable_efer_bits(EFER_NX);
-
 	if (boot_cpu_has(X86_FEATURE_MPX)) {
 		rdmsrq(MSR_IA32_BNDCFGS, host_bndcfgs);
 		WARN_ONCE(host_bndcfgs, "BNDCFGS in host will be lost");
arch/x86/kvm/x86.c +14 -0
index 879cdeb6adde..0b5d48e75b65 100644
@@ -10025,6 +10025,18 @@ void kvm_setup_xss_caps(void)
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_setup_xss_caps);
 
+static void kvm_setup_efer_caps(void)
+{
+	if (kvm_cpu_cap_has(X86_FEATURE_NX))
+		kvm_enable_efer_bits(EFER_NX);
+
+	if (kvm_cpu_cap_has(X86_FEATURE_FXSR_OPT))
+		kvm_enable_efer_bits(EFER_FFXSR);
+
+	if (kvm_cpu_cap_has(X86_FEATURE_AUTOIBRS))
+		kvm_enable_efer_bits(EFER_AUTOIBRS);
+}
+
 static inline void kvm_ops_update(struct kvm_x86_init_ops *ops)
 {
 	memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops));
@@ -10161,6 +10173,8 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)
 	if (r != 0)
 		goto out_mmu_exit;
 
+	kvm_setup_efer_caps();
+
 	enable_device_posted_irqs &= enable_apicv &&
 				     irq_remapping_cap(IRQ_POSTING_CAP);
 

base-commit: 5128b972fb2801ad9aca54d990a75611ab5283a9

#2 lore raw
Quoted 9 lines · Sean Christopherson
Quoted 7 lines · Yosry Ahmed

From: Venkatesh Srinivas <[email protected]>

TCE augments the behavior of TLB invalidating instructions (INVLPG, INVLPGB, and INVPCID) to only invalidate translations for relevant intermediate mappings to the address range, rather than ALL intermdiate translations.

Quoted 7 lines
Quoted 5 lines

The Linux kernel has been setting EFER.TCE if supported by the CPU since commit 440a65b7d25f ("x86/mm: Enable AMD translation cache extensions"), as it may improve performance.

KVM does not need to do anything to virtualize the feature,

Please back this up with actual analysis.

Something like this?

If a TLB invalidating instruction is not intercepted, it will behave according to the guest's setting of EFER.TCE as the value will be loaded on VM-Enter. Otherwise, KVM's emulation may invalidate more TLB entries, which is perfectly fine as the CPU is allowed to invalidate more TLB entries that it strictly needs to.

Quoted 6 lines
Quoted 2 lines

only advertise it and allow setting EFER.TCE. Passthrough X86_FEATURE_TCE to

Advertise X86_FEATURE_TCE to userspace, not "passthrough xxx to the guest". Because that's all KVM

Quoted 8 lines
Quoted 5 lines

the guest, and allow the guest to set EFER.TCE if available.

Your SoB should come last to capture that the chain of hanlding, i.e. this should be:

Ack.

Quoted 5 lines

[..]

Quoted 17 lines

arch/x86/kvm/svm/svm.c static __init int svm_hardware_setup(void) · line 5584

Quoted 10 lines
arch/x86/kvm/svm/svm.c +2 -0
index 3407deac90bd6..fee1c8cd45973 100644
@@ -5580,6 +5580,9 @@ static __init int svm_hardware_setup(void)
      if (boot_cpu_has(X86_FEATURE_AUTOIBRS))
              kvm_enable_efer_bits(EFER_AUTOIBRS);
 
+     if (boot_cpu_has(X86_FEATURE_TCE))
+             kvm_enable_efer_bits(EFER_TCE);

Hrm, I think we should handle all of the kvm_enable_efer_bits() calls that are conditioned only on CPU support in common code. While it's highly unlikely Intel CPUs will ever support more EFER-based features, if they do, then KVM will over-report support since kvm_initialize_cpu_caps() will effectively enable the feature, but VMX won't enable the corresponding EFER bit.

Quoted 2 lines

I can't think anything that will go sideways if we rely purely on KVM caps, so get to something like this as prep work, and then land TCE in common x86?

Will do.

#3 lore raw
Quoted 18 lines · Yosry Ahmed
Quoted 9 lines · Sean Christopherson
Quoted 7 lines · Yosry Ahmed

From: Venkatesh Srinivas <[email protected]>

TCE augments the behavior of TLB invalidating instructions (INVLPG, INVLPGB, and INVPCID) to only invalidate translations for relevant intermediate mappings to the address range, rather than ALL intermdiate translations.

Quoted 7 lines
Quoted 5 lines

The Linux kernel has been setting EFER.TCE if supported by the CPU since commit 440a65b7d25f ("x86/mm: Enable AMD translation cache extensions"), as it may improve performance.

KVM does not need to do anything to virtualize the feature,

Please back this up with actual analysis.

Quoted 7 lines

Something like this?

If a TLB invalidating instruction is not intercepted, it will behave according to the guest's setting of EFER.TCE as the value will be loaded on VM-Enter. Otherwise, KVM's emulation may invalidate more TLB entries, which is perfectly fine as the CPU is allowed to invalidate more TLB entries that it strictly needs to.

Ya, LGTM.

#4 lore raw
In reply to Sean Christopherson
Quoted 8 lines

Hrm, I think we should handle all of the kvm_enable_efer_bits() calls that are conditioned only on CPU support in common code. While it's highly unlikely Intel CPUs will ever support more EFER-based features, if they do, then KVM will over-report support since kvm_initialize_cpu_caps() will effectively enable the feature, but VMX won't enable the corresponding EFER bit.

I can't think anything that will go sideways if we rely purely on KVM caps, so get to something like this as prep work, and then land TCE in common x86?

Taking a second look here, doesn't this break the changes introduced by commit 11988499e62b ("KVM: x86: Skip EFER vs. guest CPUID checks for host-initiated writes")? Userspace writes may fail if the corresponding CPUID feature is not enabled.

We can still pull the enablement to common code, but kvm_setup_efer_caps() still needs to query the host CPU features (i.e. boot_cpu_has()) AFAICT.

#5 lore raw
Quoted 6 lines · Yosry Ahmed
Quoted 6 lines

Hrm, I think we should handle all of the kvm_enable_efer_bits() calls that are conditioned only on CPU support in common code. While it's highly unlikely Intel CPUs will ever support more EFER-based features, if they do, then KVM will over-report support since kvm_initialize_cpu_caps() will effectively enable the feature, but VMX won't enable the corresponding EFER bit.

Quoted 7 lines
Quoted 2 lines

I can't think anything that will go sideways if we rely purely on KVM caps, so get to something like this as prep work, and then land TCE in common x86?

Taking a second look here, doesn't this break the changes introduced by commit 11988499e62b ("KVM: x86: Skip EFER vs. guest CPUID checks for host-initiated writes")? Userspace writes may fail if the corresponding CPUID feature is not enabled.

No, because kvm_cpu_cap_has() == boot_cpu_has() filtered by what KVM supports. All of these EFER updates subtly rely on KVM enabling the associated CPUID feature in kvm_set_cpu_caps().

If we used guest_cpu_cap_has(), then yes, that would be a problem.

#6 lore raw
Quoted 16 lines · Sean Christopherson
Quoted 6 lines · Yosry Ahmed
Quoted 6 lines

Hrm, I think we should handle all of the kvm_enable_efer_bits() calls that are conditioned only on CPU support in common code. While it's highly unlikely Intel CPUs will ever support more EFER-based features, if they do, then KVM will over-report support since kvm_initialize_cpu_caps() will effectively enable the feature, but VMX won't enable the corresponding EFER bit.

Quoted 7 lines
Quoted 2 lines

I can't think anything that will go sideways if we rely purely on KVM caps, so get to something like this as prep work, and then land TCE in common x86?

Taking a second look here, doesn't this break the changes introduced by commit 11988499e62b ("KVM: x86: Skip EFER vs. guest CPUID checks for host-initiated writes")? Userspace writes may fail if the corresponding CPUID feature is not enabled.

Quoted 5 lines

No, because kvm_cpu_cap_has() == boot_cpu_has() filtered by what KVM supports. All of these EFER updates subtly rely on KVM enabling the associated CPUID feature in kvm_set_cpu_caps().

If we used guest_cpu_cap_has(), then yes, that would be a problem.

Gaah I mixed up guest_cpu_cap_has() and kvm_cpu_cap_has(), sorry for the noise.