Re: [PATCH v3 6/6] KVM: VMX: Move VERW closer to VMentry for MDS mitigation

From: Pawan Gupta
Date: Thu Oct 26 2023 - 15:07:51 EST


On Thu, Oct 26, 2023 at 07:14:18PM +0300, Nikolay Borisov wrote:
> > if (static_branch_unlikely(&vmx_l1d_should_flush))
> > vmx_l1d_flush(vcpu);
> > - else if (cpu_feature_enabled(X86_FEATURE_CLEAR_CPU_BUF))
> > - mds_clear_cpu_buffers();
> > else if (static_branch_unlikely(&mmio_stale_data_clear) &&
> > kvm_arch_has_assigned_device(vcpu->kvm))
> > + /* MMIO mitigation is mutually exclusive with MDS mitigation later in asm */
>
> Mutually exclusive implies that you have one or the other but not both,
> whilst I think the right formulation here is redundant? Because if mmio is
> enabled mds_clear_cpu_buffers() will clear the buffers here and later
> they'll be cleared again, no ?

No, because when mmio_stale_data_clear is enabled,
X86_FEATURE_CLEAR_CPU_BUF will not be set because of how mitigation is
selected in mmio_select_mitigation():

mmio_select_mitigation()
{
...
/*
* Enable CPU buffer clear mitigation for host and VMM if also affected
* by MDS or TAA. Otherwise, enable mitigation for VMM only.
*/
if (boot_cpu_has_bug(X86_BUG_MDS) || (boot_cpu_has_bug(X86_BUG_TAA) &&
boot_cpu_has(X86_FEATURE_RTM)))
setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
else
static_branch_enable(&mmio_stale_data_clear);

> Alternatively you might augment this check to only execute iff
> X86_FEATURE_CLEAR_CPU_BUF is not set?

It already is like that due to the logic above. That is what the
comment:

/* MMIO mitigation is mutually exclusive with MDS mitigation later in asm */

... is trying to convey. Suggestions welcome to improve the comment.