]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: x86/mmu: Detect if unprotect will do anything based on invalid_list
authorSean Christopherson <seanjc@google.com>
Sat, 31 Aug 2024 00:15:36 +0000 (17:15 -0700)
committerSean Christopherson <seanjc@google.com>
Tue, 10 Sep 2024 03:16:35 +0000 (20:16 -0700)
Explicitly query the list of to-be-zapped shadow pages when checking to
see if unprotecting a gfn for retry has succeeded, i.e. if KVM should
retry the faulting instruction.

Add a comment to explain why the list needs to be checked before zapping,
which is the primary motivation for this change.

No functional change intended.

Reviewed-by: Yuan Yao <yuan.yao@intel.com>
Link: https://lore.kernel.org/r/20240831001538.336683-22-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/mmu/mmu.c

index 29305403f95615858dd5ceded2b849d31a9f8fb5..ebbdc979a069446347843e41249beff6a4619a73 100644 (file)
@@ -2721,12 +2721,15 @@ bool __kvm_mmu_unprotect_gfn_and_retry(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
                        goto out;
        }
 
-       r = false;
        write_lock(&kvm->mmu_lock);
-       for_each_gfn_valid_sp_with_gptes(kvm, sp, gpa_to_gfn(gpa)) {
-               r = true;
+       for_each_gfn_valid_sp_with_gptes(kvm, sp, gpa_to_gfn(gpa))
                kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
-       }
+
+       /*
+        * Snapshot the result before zapping, as zapping will remove all list
+        * entries, i.e. checking the list later would yield a false negative.
+        */
+       r = !list_empty(&invalid_list);
        kvm_mmu_commit_zap_page(kvm, &invalid_list);
        write_unlock(&kvm->mmu_lock);