Re: [PATCH] KVM: nVMX: Treat vpid01 as current if L2 is active, but with VPID disabled

From: Chao Gao
Date: Sun Nov 03 2024 - 21:47:43 EST


On Thu, Oct 31, 2024 at 01:20:11PM -0700, Sean Christopherson wrote:
>When getting the current VPID, e.g. to emulate a guest TLB flush, return
>vpid01 if L2 is running but with VPID disabled, i.e. if VPID is disabled
>in vmcs12. Architecturally, if VPID is disabled, then the guest and host
>effectively share VPID=0. KVM emulates this behavior by using vpid01 when
>running an L2 with VPID disabled (see prepare_vmcs02_early_rare()), and so
>KVM must also treat vpid01 as the current VPID while L2 is active.
>
>Unconditionally treating vpid02 as the current VPID when L2 is active
>causes KVM to flush TLB entries for vpid02 instead of vpid01, which
>results in TLB entries from L1 being incorrectly preserved across nested
>VM-Enter to L2 (L2=>L1 isn't problematic, because the TLB flush after
>nested VM-Exit flushes vpid01).
>
>The bug manifests as failures in the vmx_apicv_test KVM-Unit-Test, as KVM
>incorrectly retains TLB entries for the APIC-access page across a nested
>VM-Enter.
>
>Opportunisticaly add comments at various touchpoints to explain the
>architectural requirements, and also why KVM uses vpid01 instead of vpid02.
>
>All credit goes to Chao, who root caused the issue and identified the fix.
>
>Link: https://lore.kernel.org/all/ZwzczkIlYGX+QXJz@xxxxxxxxx
>Fixes: 2b4a5a5d5688 ("KVM: nVMX: Flush current VPID (L1 vs. L2) for KVM_REQ_TLB_FLUSH_GUEST")
>Cc: stable@xxxxxxxxxxxxxxx
>Cc: Like Xu <like.xu.linux@xxxxxxxxx>
>Debugged-by: Chao Gao <chao.gao@xxxxxxxxx>
>Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>

Reviewed-by: Chao Gao <chao.gao@xxxxxxxxx>

I also ran the vmx_apicv_test KVM-Unit-Test. All failures are gone with this
patch applied. So,

Tested-by: Chao Gao <chao.gao@xxxxxxxxx>