]> git.baikalelectronics.ru Git - kernel.git/commitdiff
KVM: x86/mmu: Add helpers to read/write TDP MMU SPTEs and document RCU
authorSean Christopherson <seanjc@google.com>
Sat, 26 Feb 2022 00:15:28 +0000 (00:15 +0000)
committerPaolo Bonzini <pbonzini@redhat.com>
Tue, 8 Mar 2022 14:31:52 +0000 (09:31 -0500)
Add helpers to read and write TDP MMU SPTEs instead of open coding
rcu_dereference() all over the place, and to provide a convenient
location to document why KVM doesn't exempt holding mmu_lock for write
from having to hold RCU (and any future changes to the rules).

No functional change intended.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Reviewed-by: Ben Gardon <bgardon@google.com>
Message-Id: <20220226001546.360188-11-seanjc@google.com>
Reviewed-by: Mingwei Zhang <mizhang@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/mmu/tdp_iter.c
arch/x86/kvm/mmu/tdp_iter.h
arch/x86/kvm/mmu/tdp_mmu.c

index be3f096db2eb517f938294cc1975bcc8c5857140..6d3b3e5a5533b174ab30a5cc4c7ef12f3d26fb72 100644 (file)
@@ -12,7 +12,7 @@ static void tdp_iter_refresh_sptep(struct tdp_iter *iter)
 {
        iter->sptep = iter->pt_path[iter->level - 1] +
                SHADOW_PT_INDEX(iter->gfn << PAGE_SHIFT, iter->level);
-       iter->old_spte = READ_ONCE(*rcu_dereference(iter->sptep));
+       iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep);
 }
 
 static gfn_t round_gfn_for_level(gfn_t gfn, int level)
@@ -89,7 +89,7 @@ static bool try_step_down(struct tdp_iter *iter)
         * Reread the SPTE before stepping down to avoid traversing into page
         * tables that are no longer linked from this entry.
         */
-       iter->old_spte = READ_ONCE(*rcu_dereference(iter->sptep));
+       iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep);
 
        child_pt = spte_to_child_pt(iter->old_spte, iter->level);
        if (!child_pt)
@@ -123,7 +123,7 @@ static bool try_step_side(struct tdp_iter *iter)
        iter->gfn += KVM_PAGES_PER_HPAGE(iter->level);
        iter->next_last_level_gfn = iter->gfn;
        iter->sptep++;
-       iter->old_spte = READ_ONCE(*rcu_dereference(iter->sptep));
+       iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep);
 
        return true;
 }
index 216ebbe76dddfd6ae40ef8e3fc527b21ff72dd80..bb9b581f1ee4e6b44500e40f11bfe9e56553ecef 100644 (file)
@@ -9,6 +9,22 @@
 
 typedef u64 __rcu *tdp_ptep_t;
 
+/*
+ * TDP MMU SPTEs are RCU protected to allow paging structures (non-leaf SPTEs)
+ * to be zapped while holding mmu_lock for read.  Holding RCU isn't required for
+ * correctness if mmu_lock is held for write, but plumbing "struct kvm" down to
+ * the lower depths of the TDP MMU just to make lockdep happy is a nightmare, so
+ * all accesses to SPTEs are done under RCU protection.
+ */
+static inline u64 kvm_tdp_mmu_read_spte(tdp_ptep_t sptep)
+{
+       return READ_ONCE(*rcu_dereference(sptep));
+}
+static inline void kvm_tdp_mmu_write_spte(tdp_ptep_t sptep, u64 val)
+{
+       WRITE_ONCE(*rcu_dereference(sptep), val);
+}
+
 /*
  * A TDP iterator performs a pre-order walk over a TDP paging structure.
  */
index 22b0c03b673bd6b3af49add5ec750bfb9c9237cf..371b6a1087368648057786130432df1cc76c904c 100644 (file)
@@ -595,7 +595,7 @@ static inline int tdp_mmu_zap_spte_atomic(struct kvm *kvm,
         * here since the SPTE is going from non-present
         * to non-present.
         */
-       WRITE_ONCE(*rcu_dereference(iter->sptep), 0);
+       kvm_tdp_mmu_write_spte(iter->sptep, 0);
 
        return 0;
 }
@@ -634,7 +634,7 @@ static inline void __tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter,
         */
        WARN_ON(is_removed_spte(iter->old_spte));
 
-       WRITE_ONCE(*rcu_dereference(iter->sptep), new_spte);
+       kvm_tdp_mmu_write_spte(iter->sptep, new_spte);
 
        __handle_changed_spte(kvm, iter->as_id, iter->gfn, iter->old_spte,
                              new_spte, iter->level, false);
@@ -1069,7 +1069,7 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
                         * because the new value informs the !present
                         * path below.
                         */
-                       iter.old_spte = READ_ONCE(*rcu_dereference(iter.sptep));
+                       iter.old_spte = kvm_tdp_mmu_read_spte(iter.sptep);
                }
 
                if (!is_shadow_present_pte(iter.old_spte)) {