fault->write_fault_to_shadow_pgtable = true;
        }
 
+       /*
+        * Adjust the hugepage size _after_ resolving indirect shadow pages.
+        * KVM doesn't support mapping hugepages into the guest for gfns that
+        * are being shadowed by KVM, i.e. allocating a new shadow page may
+        * affect the allowed hugepage size.
+        */
        kvm_mmu_hugepage_adjust(vcpu, fault);
 
        trace_kvm_mmu_spte_requested(fault);
        return RET_PF_RETRY;
 }
 
- /*
- * To see whether the mapped gfn can write its page table in the current
- * mapping.
- *
- * It is the helper function of FNAME(page_fault). When guest uses large page
- * size to map the writable gfn which is used as current page table, we should
- * force kvm to use small page size to map it because new shadow page will be
- * created when kvm establishes shadow page table that stop kvm using large
- * page size. Do it early can avoid unnecessary #PF and emulation.
- *
- * Note: the PDPT page table is not checked for PAE-32 bit guest. It is ok
- * since the PDPT is always shadowed, that means, we can not use large page
- * size to map the gfn which is used as PDPT.
- */
-static bool
-FNAME(is_self_change_mapping)(struct kvm_vcpu *vcpu,
-                             struct guest_walker *walker, bool user_fault)
-{
-       int level;
-       gfn_t mask = ~(KVM_PAGES_PER_HPAGE(walker->level) - 1);
-       bool self_changed = false;
-
-       if (!(walker->pte_access & ACC_WRITE_MASK ||
-           (!is_cr0_wp(vcpu->arch.mmu) && !user_fault)))
-               return false;
-
-       for (level = walker->level; level <= walker->max_level; level++) {
-               gfn_t gfn = walker->gfn ^ walker->table_gfn[level - 1];
-
-               self_changed |= !(gfn & mask);
-       }
-
-       return self_changed;
-}
-
 /*
  * Page fault handler.  There are several causes for a page fault:
  *   - there is no shadow pte for the guest pte
 {
        struct guest_walker walker;
        int r;
-       bool is_self_change_mapping;
 
        pgprintk("%s: addr %lx err %x\n", __func__, fault->addr, fault->error_code);
        WARN_ON_ONCE(fault->is_tdp);
        }
 
        fault->gfn = walker.gfn;
+       fault->max_level = walker.level;
        fault->slot = kvm_vcpu_gfn_to_memslot(vcpu, fault->gfn);
 
        if (page_fault_handle_page_track(vcpu, fault)) {
        if (r)
                return r;
 
-       is_self_change_mapping = FNAME(is_self_change_mapping)(vcpu,
-             &walker, fault->user);
-
-       if (is_self_change_mapping)
-               fault->max_level = PG_LEVEL_4K;
-       else
-               fault->max_level = walker.level;
-
        r = kvm_faultin_pfn(vcpu, fault, walker.pte_access);
        if (r != RET_PF_CONTINUE)
                return r;