Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux-2.6-microblaze.git] / arch / x86 / kvm / mmu / tdp_mmu.c
index e7e7876..d71d177 100644 (file)
@@ -14,21 +14,24 @@ static bool __read_mostly tdp_mmu_enabled = true;
 module_param_named(tdp_mmu, tdp_mmu_enabled, bool, 0644);
 
 /* Initializes the TDP MMU for the VM, if enabled. */
-bool kvm_mmu_init_tdp_mmu(struct kvm *kvm)
+int kvm_mmu_init_tdp_mmu(struct kvm *kvm)
 {
+       struct workqueue_struct *wq;
+
        if (!tdp_enabled || !READ_ONCE(tdp_mmu_enabled))
-               return false;
+               return 0;
+
+       wq = alloc_workqueue("kvm", WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 0);
+       if (!wq)
+               return -ENOMEM;
 
        /* This should not be changed for the lifetime of the VM. */
        kvm->arch.tdp_mmu_enabled = true;
-
        INIT_LIST_HEAD(&kvm->arch.tdp_mmu_roots);
        spin_lock_init(&kvm->arch.tdp_mmu_pages_lock);
        INIT_LIST_HEAD(&kvm->arch.tdp_mmu_pages);
-       kvm->arch.tdp_mmu_zap_wq =
-               alloc_workqueue("kvm", WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 0);
-
-       return true;
+       kvm->arch.tdp_mmu_zap_wq = wq;
+       return 1;
 }
 
 /* Arbitrarily returns true so that this may be used in if statements. */
@@ -906,10 +909,8 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
 }
 
 /*
- * Tears down the mappings for the range of gfns, [start, end), and frees the
- * non-root pages mapping GFNs strictly within that range. Returns true if
- * SPTEs have been cleared and a TLB flush is needed before releasing the
- * MMU lock.
+ * Zap leafs SPTEs for the range of gfns, [start, end). Returns true if SPTEs
+ * have been cleared and a TLB flush is needed before releasing the MMU lock.
  *
  * If can_yield is true, will release the MMU lock and reschedule if the
  * scheduler needs the CPU or there is contention on the MMU lock. If this
@@ -917,42 +918,25 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
  * the caller must ensure it does not supply too large a GFN range, or the
  * operation can cause a soft lockup.
  */
-static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,
-                         gfn_t start, gfn_t end, bool can_yield, bool flush)
+static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root,
+                             gfn_t start, gfn_t end, bool can_yield, bool flush)
 {
-       bool zap_all = (start == 0 && end >= tdp_mmu_max_gfn_host());
        struct tdp_iter iter;
 
-       /*
-        * No need to try to step down in the iterator when zapping all SPTEs,
-        * zapping the top-level non-leaf SPTEs will recurse on their children.
-        */
-       int min_level = zap_all ? root->role.level : PG_LEVEL_4K;
-
        end = min(end, tdp_mmu_max_gfn_host());
 
        lockdep_assert_held_write(&kvm->mmu_lock);
 
        rcu_read_lock();
 
-       for_each_tdp_pte_min_level(iter, root, min_level, start, end) {
+       for_each_tdp_pte_min_level(iter, root, PG_LEVEL_4K, start, end) {
                if (can_yield &&
                    tdp_mmu_iter_cond_resched(kvm, &iter, flush, false)) {
                        flush = false;
                        continue;
                }
 
-               if (!is_shadow_present_pte(iter.old_spte))
-                       continue;
-
-               /*
-                * If this is a non-last-level SPTE that covers a larger range
-                * than should be zapped, continue, and zap the mappings at a
-                * lower level, except when zapping all SPTEs.
-                */
-               if (!zap_all &&
-                   (iter.gfn < start ||
-                    iter.gfn + KVM_PAGES_PER_HPAGE(iter.level) > end) &&
+               if (!is_shadow_present_pte(iter.old_spte) ||
                    !is_last_spte(iter.old_spte, iter.level))
                        continue;
 
@@ -960,17 +944,13 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,
                flush = true;
        }
 
-       /*
-        * Need to flush before releasing RCU.  TODO: do it only if intermediate
-        * page tables were zapped; there is no need to flush under RCU protection
-        * if no 'struct kvm_mmu_page' is freed.
-        */
-       if (flush)
-               kvm_flush_remote_tlbs_with_address(kvm, start, end - start);
-
        rcu_read_unlock();
 
-       return false;
+       /*
+        * Because this flow zaps _only_ leaf SPTEs, the caller doesn't need
+        * to provide RCU protection as no 'struct kvm_mmu_page' will be freed.
+        */
+       return flush;
 }
 
 /*
@@ -979,13 +959,13 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,
  * SPTEs have been cleared and a TLB flush is needed before releasing the
  * MMU lock.
  */
-bool __kvm_tdp_mmu_zap_gfn_range(struct kvm *kvm, int as_id, gfn_t start,
-                                gfn_t end, bool can_yield, bool flush)
+bool kvm_tdp_mmu_zap_leafs(struct kvm *kvm, int as_id, gfn_t start, gfn_t end,
+                          bool can_yield, bool flush)
 {
        struct kvm_mmu_page *root;
 
        for_each_tdp_mmu_root_yield_safe(kvm, root, as_id)
-               flush = zap_gfn_range(kvm, root, start, end, can_yield, flush);
+               flush = tdp_mmu_zap_leafs(kvm, root, start, end, can_yield, flush);
 
        return flush;
 }
@@ -1233,8 +1213,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
 bool kvm_tdp_mmu_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range,
                                 bool flush)
 {
-       return __kvm_tdp_mmu_zap_gfn_range(kvm, range->slot->as_id, range->start,
-                                          range->end, range->may_block, flush);
+       return kvm_tdp_mmu_zap_leafs(kvm, range->slot->as_id, range->start,
+                                    range->end, range->may_block, flush);
 }
 
 typedef bool (*tdp_handler_t)(struct kvm *kvm, struct tdp_iter *iter,