Re: [RFC PATCH v2 22/25] KVM: x86/mmu: Refactor kvm_mmu_invlpg() to allow skipping the gva flush

From: Sean Christopherson

Date: Wed Jul 22 2026 - 20:53:25 EST


On Tue, Jun 16, 2026, Yosry Ahmed wrote:
> Refactor helpers out of kvm_mmu_invalidate_addr() and kvm_mmu_invlpg()
> that take in an extra argument to skip the GVA flush.
>
> This will be used when invalidating GVAs in a different context than the
> correct one (i.e. invalidating an L2 GVA from L1), so flushing the
> current context would flush the wrong TLB entries.
>
> No functional change intended.
>
> Signed-off-by: Yosry Ahmed <yosry@xxxxxxxxxx>
> ---
> arch/x86/kvm/mmu/mmu.c | 23 +++++++++++++++++------
> 1 file changed, 17 insertions(+), 6 deletions(-)
>
> diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
> index 65c35ed8f4a01..3feb75732f7b4 100644
> --- a/arch/x86/kvm/mmu/mmu.c
> +++ b/arch/x86/kvm/mmu/mmu.c
> @@ -6615,15 +6615,15 @@ static void kvm_mmu_invalidate_addr_in_root(struct kvm_vcpu *vcpu,
> write_unlock(&vcpu->kvm->mmu_lock);
> }
>
> -void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
> - u64 addr, unsigned long roots)
> +static void __kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
> + u64 addr, unsigned long roots, bool flush_gva)
> {
> int i;
>
> WARN_ON_ONCE(roots & ~KVM_MMU_ROOTS_ALL);
>
> /* It's actually a GPA for vcpu->arch.guest_mmu. */
> - if (mmu != &vcpu->arch.guest_mmu) {
> + if (flush_gva && mmu != &vcpu->arch.guest_mmu) {
> /* INVLPG on a non-canonical address is a NOP according to the SDM. */
> if (is_noncanonical_invlpg_address(addr, vcpu))
> return;
> @@ -6642,9 +6642,15 @@ void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
> kvm_mmu_invalidate_addr_in_root(vcpu, mmu, addr, mmu->prev_roots[i].hpa);
> }
> }
> +
> +void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
> + u64 addr, unsigned long roots)

Rather than kvm_mmu_invalidate_addr() for the wrapper, what if we call this
kvm_mmu_invalidate_gva()? And then kvm_mmu_invlpg_gva(). Then we don't need
to have the "in_root" version to a quad-underscores helper, and IMO it's more
obvious what's different between the one-line wrappers and the inner helpers.

> +{
> + __kvm_mmu_invalidate_addr(vcpu, mmu, addr, roots, true);
> +}

Make these one-liners static inlines to avoid the export, and because they're
trivial wrappers that should be inlined.

> EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_mmu_invalidate_addr);
>
> -void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
> +static void __kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva, bool flush_gva)
> {
> /*
> * INVLPG is required to invalidate any global mappings for the VA,
> @@ -6656,11 +6662,16 @@ void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
> * be synced when switching to that new cr3, so nothing needs to be
> * done here for them.
> */
> - kvm_mmu_invalidate_addr(vcpu, vcpu->arch.walk_mmu, gva, KVM_MMU_ROOTS_ALL);
> + __kvm_mmu_invalidate_addr(vcpu, vcpu->arch.walk_mmu, gva,
> + KVM_MMU_ROOTS_ALL, flush_gva);
> ++vcpu->stat.invlpg;
> }
> -EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_mmu_invlpg);
>
> +void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
> +{
> + __kvm_mmu_invlpg(vcpu, gva, true);
> +}
> +EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_mmu_invlpg);
>
> void kvm_mmu_invpcid_gva(struct kvm_vcpu *vcpu, gva_t gva, unsigned long pcid)
> {
> --
> 2.54.0.1136.gdb2ca164c4-goog
>