]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: x86/mmu: Only check gfn age in shadow MMU if indirect_shadow_pages > 0
authorJames Houghton <jthoughton@google.com>
Tue, 4 Feb 2025 00:40:34 +0000 (00:40 +0000)
committerSean Christopherson <seanjc@google.com>
Fri, 14 Feb 2025 15:17:28 +0000 (07:17 -0800)
When aging SPTEs and the TDP MMU is enabled, process the shadow MMU if and
only if the VM has at least one shadow page, as opposed to checking if the
VM has rmaps.  Checking for rmaps will effectively yield a false positive
if the VM ran nested TDP VMs in the past, but is not currently doing so.

Signed-off-by: James Houghton <jthoughton@google.com>
Acked-by: Yu Zhao <yuzhao@google.com>
Link: https://lore.kernel.org/r/20250204004038.1680123-8-jthoughton@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/mmu/mmu.c

index 3fc461ebaf05548332b571bf1b29cddfbea427fb..6af7eaa9feff0d2d8f09806a2b4d0ff65c5159ea 100644 (file)
@@ -1588,6 +1588,11 @@ static bool kvm_rmap_age_gfn_range(struct kvm *kvm,
        return young;
 }
 
+static bool kvm_may_have_shadow_mmu_sptes(struct kvm *kvm)
+{
+       return !tdp_mmu_enabled || READ_ONCE(kvm->arch.indirect_shadow_pages);
+}
+
 bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
 {
        bool young = false;
@@ -1595,7 +1600,7 @@ bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
        if (tdp_mmu_enabled)
                young = kvm_tdp_mmu_age_gfn_range(kvm, range);
 
-       if (kvm_memslots_have_rmaps(kvm)) {
+       if (kvm_may_have_shadow_mmu_sptes(kvm)) {
                write_lock(&kvm->mmu_lock);
                young |= kvm_rmap_age_gfn_range(kvm, range, false);
                write_unlock(&kvm->mmu_lock);
@@ -1611,7 +1616,7 @@ bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
        if (tdp_mmu_enabled)
                young = kvm_tdp_mmu_test_age_gfn(kvm, range);
 
-       if (!young && kvm_memslots_have_rmaps(kvm)) {
+       if (!young && kvm_may_have_shadow_mmu_sptes(kvm)) {
                write_lock(&kvm->mmu_lock);
                young |= kvm_rmap_age_gfn_range(kvm, range, true);
                write_unlock(&kvm->mmu_lock);