Skip to content

Commit 43fb862

Browse files
pa1guptahansendc
authored andcommitted
KVM/VMX: Move VERW closer to VMentry for MDS mitigation
During VMentry VERW is executed to mitigate MDS. After VERW, any memory access like register push onto stack may put host data in MDS affected CPU buffers. A guest can then use MDS to sample host data. Although likelihood of secrets surviving in registers at current VERW callsite is less, but it can't be ruled out. Harden the MDS mitigation by moving the VERW mitigation late in VMentry path. Note that VERW for MMIO Stale Data mitigation is unchanged because of the complexity of per-guest conditional VERW which is not easy to handle that late in asm with no GPRs available. If the CPU is also affected by MDS, VERW is unconditionally executed late in asm regardless of guest having MMIO access. Signed-off-by: Pawan Gupta <pawan.kumar.gupta@linux.intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Acked-by: Sean Christopherson <seanjc@google.com> Link: https://lore.kernel.org/all/20240213-delay-verw-v8-6-a6216d83edb7%40linux.intel.com
1 parent 706a189 commit 43fb862

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

arch/x86/kvm/vmx/vmenter.S

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ SYM_FUNC_START(__vmx_vcpu_run)
161161
/* Load guest RAX. This kills the @regs pointer! */
162162
mov VCPU_RAX(%_ASM_AX), %_ASM_AX
163163

164+
/* Clobbers EFLAGS.ZF */
165+
CLEAR_CPU_BUFFERS
166+
164167
/* Check EFLAGS.CF from the VMX_RUN_VMRESUME bit test above. */
165168
jnc .Lvmlaunch
166169

arch/x86/kvm/vmx/vmx.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,16 @@ static __always_inline void vmx_enable_fb_clear(struct vcpu_vmx *vmx)
388388

389389
static void vmx_update_fb_clear_dis(struct kvm_vcpu *vcpu, struct vcpu_vmx *vmx)
390390
{
391-
vmx->disable_fb_clear = (host_arch_capabilities & ARCH_CAP_FB_CLEAR_CTRL) &&
391+
/*
392+
* Disable VERW's behavior of clearing CPU buffers for the guest if the
393+
* CPU isn't affected by MDS/TAA, and the host hasn't forcefully enabled
394+
* the mitigation. Disabling the clearing behavior provides a
395+
* performance boost for guests that aren't aware that manually clearing
396+
* CPU buffers is unnecessary, at the cost of MSR accesses on VM-Entry
397+
* and VM-Exit.
398+
*/
399+
vmx->disable_fb_clear = !cpu_feature_enabled(X86_FEATURE_CLEAR_CPU_BUF) &&
400+
(host_arch_capabilities & ARCH_CAP_FB_CLEAR_CTRL) &&
392401
!boot_cpu_has_bug(X86_BUG_MDS) &&
393402
!boot_cpu_has_bug(X86_BUG_TAA);
394403

@@ -7224,11 +7233,14 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
72247233

72257234
guest_state_enter_irqoff();
72267235

7227-
/* L1D Flush includes CPU buffer clear to mitigate MDS */
7236+
/*
7237+
* L1D Flush includes CPU buffer clear to mitigate MDS, but VERW
7238+
* mitigation for MDS is done late in VMentry and is still
7239+
* executed in spite of L1D Flush. This is because an extra VERW
7240+
* should not matter much after the big hammer L1D Flush.
7241+
*/
72287242
if (static_branch_unlikely(&vmx_l1d_should_flush))
72297243
vmx_l1d_flush(vcpu);
7230-
else if (cpu_feature_enabled(X86_FEATURE_CLEAR_CPU_BUF))
7231-
mds_clear_cpu_buffers();
72327244
else if (static_branch_unlikely(&mmio_stale_data_clear) &&
72337245
kvm_arch_has_assigned_device(vcpu->kvm))
72347246
mds_clear_cpu_buffers();

0 commit comments

Comments
 (0)