Skip to contentSkip to conversation

KVM: VMX: Don't disable FB_CLEAR when doing the MMIO Stale Data VERW

PATCH In review Amirmohammad Eftekhar 2 participants 2 messages Nov 24

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

PATCH 1/1 KVM: VMX: Don't disable FB_CLEAR when doing the MMIO Stale Data VERW

Don't set FB_CLEAR_DIS for a VM-Enter that does the MMIO Stale Data VERW. VERW doesn't overwrite the fill buffers while FB_CLEAR_DIS is set, so the buffer clear that is meant to protect the host from a guest with access to host MMIO currently does nothing. Keep using FB_CLEAR_DIS for vCPUs that can't access host MMIO, which are the guests it was added for.

Commit e6ff1d61de51 ("KVM: VMX: Handle MMIO Stale Data in VM-Enter assembly via ALTERNATIVES_2") moved the MMIO Stale Data VERW from vmx_vcpu_enter_exit(), where it ran before vmx_disable_fb_clear(), into __vmx_vcpu_run(), which is called after it. When commit 43fb862de8f6 ("KVM/VMX: Move VERW closer to VMentry for MDS mitigation") moved the unconditional VERW into the asm, vmx_update_fb_clear_dis() was taught to leave FB_CLEAR alone if X86_FEATURE_CLEAR_CPU_BUF is set, but there is no such check for X86_FEATURE_CLEAR_CPU_BUF_VM_MMIO.

Both end up enabled on the same hosts. A CPU that is affected by MMIO Stale Data but not by MDS or TAA mitigates it with X86_FEATURE_CLEAR_CPU_BUF_VM_MMIO (unless another VERW based mitigation, such as the one for RFDS, is enabled), and if the CPU enumerates FB_CLEAR_CTRL, KVM disables FB_CLEAR for vCPUs whose ARCH_CAPABILITIES enumerate neither FB_CLEAR nor immunity, e.g. QEMU CPU models that expose arch-capabilities without fb-clear. On those hosts, every VM-Enter of such a vCPU with host MMIO mapped does the VERW with FB_CLEAR_DIS=1, while sysfs still reports "Mitigation: Clear CPU buffers".

Fixes e6ff1d61de51 ("KVM: VMX: Handle MMIO Stale Data in VM-Enter assembly via ALTERNATIVES_2")

---
I don't have access to a CPU that enumerates FB_CLEAR_CTRL, so this isn't tested on affected hardware. Instead I ran it as L1 on a vCPU that looks like an MMIO Stale Data affected Ice Lake-SP (MDS_NO, TAA_NO, FB_CLEAR and FB_CLEAR_CTRL), with an L2 guest that has a PCI BAR in a memslot and ARCH_CAPABILITIES without FB_CLEAR. L1's MCU_OPT_CTRL writes #GP, but L0 still sees the values L1 tries to write, and a watchpoint on L1's x86_verw_sel shows where the VERW happens. Without the patch, all 2576 MMIO VERWs came right after the write that sets FB_CLEAR_DIS; with the patch, none of the 4478 did, and a guest without host MMIO still gets FB_CLEAR_DIS on every entry. Same result with QEMU running the L2 guest with -cpu Icelake-Server-v5.

KVM selftests and kvm-unit-tests (including the vmx tests) gave the same results with and without the patch in that setup (a few fail or time out there either way), and it builds cleanly with KVM_INTEL=m, KVM_AMD=m and KVM_WERROR=y. I can share the test scripts if that helps.

6.19.y to 7.1.y still call the flag VMX_RUN_CLEAR_CPU_BUFFERS_FOR_MMIO, so the backport needs that rename.

 arch/x86/kvm/vmx/vmx.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)
arch/x86/kvm/vmx/vmx.c +20 -6
index 68fa251dda60..490cad070c13 100644
@@ -482,11 +482,24 @@ static const struct kernel_param_ops vmentry_l1d_flush_ops = {
 };
 module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
 
-static __always_inline void vmx_disable_fb_clear(struct vcpu_vmx *vmx)
+/*
+ * VERW doesn't overwrite the fill buffers if FB_CLEAR_DIS is set, so leave
+ * FB_CLEAR enabled for a VM-Enter that does the MMIO Stale Data VERW.  The
+ * unconditional VERW is already handled by vmx_update_fb_clear_dis().
+ */
+static __always_inline bool vmx_should_disable_fb_clear(struct vcpu_vmx *vmx,
+							unsigned int flags)
+{
+	return vmx->disable_fb_clear &&
+	       !(flags & KVM_ENTER_CLEAR_CPU_BUFFERS_FOR_MMIO);
+}
+
+static __always_inline void vmx_disable_fb_clear(struct vcpu_vmx *vmx,
+						 unsigned int flags)
 {
 	u64 msr;
 
-	if (!vmx->disable_fb_clear)
+	if (!vmx_should_disable_fb_clear(vmx, flags))
 		return;
 
 	msr = native_rdmsrq(MSR_IA32_MCU_OPT_CTRL);
@@ -496,9 +509,10 @@ static __always_inline void vmx_disable_fb_clear(struct vcpu_vmx *vmx)
 	vmx->msr_ia32_mcu_opt_ctrl = msr;
 }
 
-static __always_inline void vmx_enable_fb_clear(struct vcpu_vmx *vmx)
+static __always_inline void vmx_enable_fb_clear(struct vcpu_vmx *vmx,
+						unsigned int flags)
 {
-	if (!vmx->disable_fb_clear)
+	if (!vmx_should_disable_fb_clear(vmx, flags))
 		return;
 
 	vmx->msr_ia32_mcu_opt_ctrl &= ~FB_CLEAR_DIS;
@@ -7323,7 +7337,7 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
 
 	vmx_l1d_flush(vcpu);
 
-	vmx_disable_fb_clear(vmx);
+	vmx_disable_fb_clear(vmx, flags);
 
 	if (vcpu->arch.cr2 != native_read_cr2())
 		native_write_cr2(vcpu->arch.cr2);
@@ -7335,7 +7349,7 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
 
 	vmx->idt_vectoring_info = 0;
 
-	vmx_enable_fb_clear(vmx);
+	vmx_enable_fb_clear(vmx, flags);
 
 	if (unlikely(vmx->fail)) {
 		vmx->vt.exit_reason.full = EXIT_REASON_UNDEFINED;

base-commit: cbe54fc3462b3949ab27b9ae81fb9ddb509040c7

Signature · 1 line
2.34.1
#1 lore raw
Quoted 5 lines · Amirmohammad Eftekhar

Don't set FB_CLEAR_DIS for a VM-Enter that does the MMIO Stale Data VERW. VERW doesn't overwrite the fill buffers while FB_CLEAR_DIS is set, so the buffer clear that is meant to protect the host from a guest with access to host MMIO currently does nothing. Keep using FB_CLEAR_DIS for vCPUs that can't access host MMIO, which are the guests it was added for.

Thanks for the patch, the problem is real. I believe the affected use case is a guest with MMIO device on ICX, and none of MDS_NO/TAA_NO/FB_CLEAR exported to guest.

...

Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +0 -1
index 68fa251dda60..490cad070c13 100644
@@ -482,11 +482,24 @@ static const struct kernel_param_ops vmentry_l1d_flush_ops = {
 };
 module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
 
-static __always_inline void vmx_disable_fb_clear(struct vcpu_vmx *vmx)

arch/x86/kvm/vmx/vmx.c static const struct kernel_param_ops vmentry_l1d_flush_ops = { · line 494

Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10 -0
+/*
+ * VERW doesn't overwrite the fill buffers if FB_CLEAR_DIS is set, so leave
+ * FB_CLEAR enabled for a VM-Enter that does the MMIO Stale Data VERW.  The
+ * unconditional VERW is already handled by vmx_update_fb_clear_dis().
+ */
+static __always_inline bool vmx_should_disable_fb_clear(struct vcpu_vmx *vmx,
+							unsigned int flags)
+{
+	return vmx->disable_fb_clear &&
+	       !(flags & KVM_ENTER_CLEAR_CPU_BUFFERS_FOR_MMIO);

This doesn't seem right, FB_CLEAR_DIS's main use case is FOR_MMIO.

The ideal fix would be to set FB_CLEAR_DIS after VERW in KVM-entry. But, thats quite late in the entry path, moving the MSR-write past that is non-trivial.

I believe it is best to restore the earlier order just for the MMIO case, i.e. do VERW for CLEAR_CPU_BUFFERS_FOR_MMIO before setting FB_CLEAR_DIS like:

vmx_disable_fb_clear()

{
	if (!vmx->disable_fb_clear)
		return;

	if (flags & KVM_ENTER_CLEAR_CPU_BUFFERS_FOR_MMIO)
		x86_clear_cpu_buffers();                    <-------- here

	msr = native_rdmsrq(MSR_IA32_MCU_OPT_CTRL);
	...

Sean, would you agree?