Merge tag 'regulator-v5.12' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[linux-2.6-microblaze.git] / mm / memory.c
index feff48e..c32318d 100644 (file)
@@ -134,6 +134,18 @@ static inline bool arch_faults_on_old_pte(void)
 }
 #endif
 
+#ifndef arch_wants_old_prefaulted_pte
+static inline bool arch_wants_old_prefaulted_pte(void)
+{
+       /*
+        * Transitioning a PTE from 'old' to 'young' can be expensive on
+        * some architectures, even if it's performed in hardware. By
+        * default, "false" means prefaulted entries will be 'young'.
+        */
+       return false;
+}
+#endif
+
 static int __init disable_randmaps(char *s)
 {
        randomize_va_space = 0;
@@ -1534,13 +1546,13 @@ void zap_page_range(struct vm_area_struct *vma, unsigned long start,
        lru_add_drain();
        mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
                                start, start + size);
-       tlb_gather_mmu(&tlb, vma->vm_mm, start, range.end);
+       tlb_gather_mmu(&tlb, vma->vm_mm);
        update_hiwater_rss(vma->vm_mm);
        mmu_notifier_invalidate_range_start(&range);
        for ( ; vma && vma->vm_start < range.end; vma = vma->vm_next)
                unmap_single_vma(&tlb, vma, start, range.end, NULL);
        mmu_notifier_invalidate_range_end(&range);
-       tlb_finish_mmu(&tlb, start, range.end);
+       tlb_finish_mmu(&tlb);
 }
 
 /**
@@ -1561,12 +1573,12 @@ static void zap_page_range_single(struct vm_area_struct *vma, unsigned long addr
        lru_add_drain();
        mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
                                address, address + size);
-       tlb_gather_mmu(&tlb, vma->vm_mm, address, range.end);
+       tlb_gather_mmu(&tlb, vma->vm_mm);
        update_hiwater_rss(vma->vm_mm);
        mmu_notifier_invalidate_range_start(&range);
        unmap_single_vma(&tlb, vma, address, range.end, details);
        mmu_notifier_invalidate_range_end(&range);
-       tlb_finish_mmu(&tlb, address, range.end);
+       tlb_finish_mmu(&tlb);
 }
 
 /**
@@ -3503,7 +3515,7 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
        if (pte_alloc(vma->vm_mm, vmf->pmd))
                return VM_FAULT_OOM;
 
-       /* See the comment in pte_alloc_one_map() */
+       /* See comment in handle_pte_fault() */
        if (unlikely(pmd_trans_unstable(vmf->pmd)))
                return 0;
 
@@ -3643,66 +3655,6 @@ static vm_fault_t __do_fault(struct vm_fault *vmf)
        return ret;
 }
 
-/*
- * The ordering of these checks is important for pmds with _PAGE_DEVMAP set.
- * If we check pmd_trans_unstable() first we will trip the bad_pmd() check
- * inside of pmd_none_or_trans_huge_or_clear_bad(). This will end up correctly
- * returning 1 but not before it spams dmesg with the pmd_clear_bad() output.
- */
-static int pmd_devmap_trans_unstable(pmd_t *pmd)
-{
-       return pmd_devmap(*pmd) || pmd_trans_unstable(pmd);
-}
-
-static vm_fault_t pte_alloc_one_map(struct vm_fault *vmf)
-{
-       struct vm_area_struct *vma = vmf->vma;
-
-       if (!pmd_none(*vmf->pmd))
-               goto map_pte;
-       if (vmf->prealloc_pte) {
-               vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
-               if (unlikely(!pmd_none(*vmf->pmd))) {
-                       spin_unlock(vmf->ptl);
-                       goto map_pte;
-               }
-
-               mm_inc_nr_ptes(vma->vm_mm);
-               pmd_populate(vma->vm_mm, vmf->pmd, vmf->prealloc_pte);
-               spin_unlock(vmf->ptl);
-               vmf->prealloc_pte = NULL;
-       } else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd))) {
-               return VM_FAULT_OOM;
-       }
-map_pte:
-       /*
-        * If a huge pmd materialized under us just retry later.  Use
-        * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead of
-        * pmd_trans_huge() to ensure the pmd didn't become pmd_trans_huge
-        * under us and then back to pmd_none, as a result of MADV_DONTNEED
-        * running immediately after a huge pmd fault in a different thread of
-        * this mm, in turn leading to a misleading pmd_trans_huge() retval.
-        * All we have to ensure is that it is a regular pmd that we can walk
-        * with pte_offset_map() and we can do that through an atomic read in
-        * C, which is what pmd_trans_unstable() provides.
-        */
-       if (pmd_devmap_trans_unstable(vmf->pmd))
-               return VM_FAULT_NOPAGE;
-
-       /*
-        * At this point we know that our vmf->pmd points to a page of ptes
-        * and it cannot become pmd_none(), pmd_devmap() or pmd_trans_huge()
-        * for the duration of the fault.  If a racing MADV_DONTNEED runs and
-        * we zap the ptes pointed to by our vmf->pmd, the vmf->ptl will still
-        * be valid and we will re-check to make sure the vmf->pte isn't
-        * pte_none() under vmf->ptl protection when we return to
-        * alloc_set_pte().
-        */
-       vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
-                       &vmf->ptl);
-       return 0;
-}
-
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 static void deposit_prealloc_pte(struct vm_fault *vmf)
 {
@@ -3717,7 +3669,7 @@ static void deposit_prealloc_pte(struct vm_fault *vmf)
        vmf->prealloc_pte = NULL;
 }
 
-static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
+vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
 {
        struct vm_area_struct *vma = vmf->vma;
        bool write = vmf->flags & FAULT_FLAG_WRITE;
@@ -3775,76 +3727,41 @@ out:
        return ret;
 }
 #else
-static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
+vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
 {
-       BUILD_BUG();
-       return 0;
+       return VM_FAULT_FALLBACK;
 }
 #endif
 
-/**
- * alloc_set_pte - setup new PTE entry for given page and add reverse page
- * mapping. If needed, the function allocates page table or use pre-allocated.
- *
- * @vmf: fault environment
- * @page: page to map
- *
- * Caller must take care of unlocking vmf->ptl, if vmf->pte is non-NULL on
- * return.
- *
- * Target users are page handler itself and implementations of
- * vm_ops->map_pages.
- *
- * Return: %0 on success, %VM_FAULT_ code in case of error.
- */
-vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct page *page)
+void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr)
 {
        struct vm_area_struct *vma = vmf->vma;
        bool write = vmf->flags & FAULT_FLAG_WRITE;
+       bool prefault = vmf->address != addr;
        pte_t entry;
-       vm_fault_t ret;
-
-       if (pmd_none(*vmf->pmd) && PageTransCompound(page)) {
-               ret = do_set_pmd(vmf, page);
-               if (ret != VM_FAULT_FALLBACK)
-                       return ret;
-       }
-
-       if (!vmf->pte) {
-               ret = pte_alloc_one_map(vmf);
-               if (ret)
-                       return ret;
-       }
-
-       /* Re-check under ptl */
-       if (unlikely(!pte_none(*vmf->pte))) {
-               update_mmu_tlb(vma, vmf->address, vmf->pte);
-               return VM_FAULT_NOPAGE;
-       }
 
        flush_icache_page(vma, page);
        entry = mk_pte(page, vma->vm_page_prot);
-       entry = pte_sw_mkyoung(entry);
+
+       if (prefault && arch_wants_old_prefaulted_pte())
+               entry = pte_mkold(entry);
+       else
+               entry = pte_sw_mkyoung(entry);
+
        if (write)
                entry = maybe_mkwrite(pte_mkdirty(entry), vma);
        /* copy-on-write page */
        if (write && !(vma->vm_flags & VM_SHARED)) {
                inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
-               page_add_new_anon_rmap(page, vma, vmf->address, false);
+               page_add_new_anon_rmap(page, vma, addr, false);
                lru_cache_add_inactive_or_unevictable(page, vma);
        } else {
                inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
                page_add_file_rmap(page, false);
        }
-       set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);
-
-       /* no need to invalidate: a not-present page won't be cached */
-       update_mmu_cache(vma, vmf->address, vmf->pte);
-
-       return 0;
+       set_pte_at(vma->vm_mm, addr, vmf->pte, entry);
 }
 
-
 /**
  * finish_fault - finish page fault once we have prepared the page to fault
  *
@@ -3862,12 +3779,12 @@ vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct page *page)
  */
 vm_fault_t finish_fault(struct vm_fault *vmf)
 {
+       struct vm_area_struct *vma = vmf->vma;
        struct page *page;
-       vm_fault_t ret = 0;
+       vm_fault_t ret;
 
        /* Did we COW the page? */
-       if ((vmf->flags & FAULT_FLAG_WRITE) &&
-           !(vmf->vma->vm_flags & VM_SHARED))
+       if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED))
                page = vmf->cow_page;
        else
                page = vmf->page;
@@ -3876,12 +3793,38 @@ vm_fault_t finish_fault(struct vm_fault *vmf)
         * check even for read faults because we might have lost our CoWed
         * page
         */
-       if (!(vmf->vma->vm_flags & VM_SHARED))
-               ret = check_stable_address_space(vmf->vma->vm_mm);
-       if (!ret)
-               ret = alloc_set_pte(vmf, page);
-       if (vmf->pte)
-               pte_unmap_unlock(vmf->pte, vmf->ptl);
+       if (!(vma->vm_flags & VM_SHARED)) {
+               ret = check_stable_address_space(vma->vm_mm);
+               if (ret)
+                       return ret;
+       }
+
+       if (pmd_none(*vmf->pmd)) {
+               if (PageTransCompound(page)) {
+                       ret = do_set_pmd(vmf, page);
+                       if (ret != VM_FAULT_FALLBACK)
+                               return ret;
+               }
+
+               if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd)))
+                       return VM_FAULT_OOM;
+       }
+
+       /* See comment in handle_pte_fault() */
+       if (pmd_devmap_trans_unstable(vmf->pmd))
+               return 0;
+
+       vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
+                                     vmf->address, &vmf->ptl);
+       ret = 0;
+       /* Re-check under ptl */
+       if (likely(pte_none(*vmf->pte)))
+               do_set_pte(vmf, page, vmf->address);
+       else
+               ret = VM_FAULT_NOPAGE;
+
+       update_mmu_tlb(vma, vmf->address, vmf->pte);
+       pte_unmap_unlock(vmf->pte, vmf->ptl);
        return ret;
 }
 
@@ -3951,13 +3894,12 @@ static vm_fault_t do_fault_around(struct vm_fault *vmf)
        pgoff_t start_pgoff = vmf->pgoff;
        pgoff_t end_pgoff;
        int off;
-       vm_fault_t ret = 0;
 
        nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT;
        mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
 
-       vmf->address = max(address & mask, vmf->vma->vm_start);
-       off = ((address - vmf->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
+       address = max(address & mask, vmf->vma->vm_start);
+       off = ((vmf->address - address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
        start_pgoff -= off;
 
        /*
@@ -3965,7 +3907,7 @@ static vm_fault_t do_fault_around(struct vm_fault *vmf)
         *  the vma or nr_pages from start_pgoff, depending what is nearest.
         */
        end_pgoff = start_pgoff -
-               ((vmf->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
+               ((address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
                PTRS_PER_PTE - 1;
        end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1,
                        start_pgoff + nr_pages - 1);
@@ -3973,31 +3915,11 @@ static vm_fault_t do_fault_around(struct vm_fault *vmf)
        if (pmd_none(*vmf->pmd)) {
                vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm);
                if (!vmf->prealloc_pte)
-                       goto out;
+                       return VM_FAULT_OOM;
                smp_wmb(); /* See comment in __pte_alloc() */
        }
 
-       vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff);
-
-       /* Huge page is mapped? Page fault is solved */
-       if (pmd_trans_huge(*vmf->pmd)) {
-               ret = VM_FAULT_NOPAGE;
-               goto out;
-       }
-
-       /* ->map_pages() haven't done anything useful. Cold page cache? */
-       if (!vmf->pte)
-               goto out;
-
-       /* check if the page fault is solved */
-       vmf->pte -= (vmf->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT);
-       if (!pte_none(*vmf->pte))
-               ret = VM_FAULT_NOPAGE;
-       pte_unmap_unlock(vmf->pte, vmf->ptl);
-out:
-       vmf->address = address;
-       vmf->pte = NULL;
-       return ret;
+       return vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff);
 }
 
 static vm_fault_t do_read_fault(struct vm_fault *vmf)
@@ -4353,7 +4275,18 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf)
                 */
                vmf->pte = NULL;
        } else {
-               /* See comment in pte_alloc_one_map() */
+               /*
+                * If a huge pmd materialized under us just retry later.  Use
+                * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead
+                * of pmd_trans_huge() to ensure the pmd didn't become
+                * pmd_trans_huge under us and then back to pmd_none, as a
+                * result of MADV_DONTNEED running immediately after a huge pmd
+                * fault in a different thread of this mm, in turn leading to a
+                * misleading pmd_trans_huge() retval. All we have to ensure is
+                * that it is a regular pmd that we can walk with
+                * pte_offset_map() and we can do that through an atomic read
+                * in C, which is what pmd_trans_unstable() provides.
+                */
                if (pmd_devmap_trans_unstable(vmf->pmd))
                        return 0;
                /*
@@ -4709,9 +4642,9 @@ int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
 }
 #endif /* __PAGETABLE_PMD_FOLDED */
 
-int follow_pte(struct mm_struct *mm, unsigned long address,
-              struct mmu_notifier_range *range, pte_t **ptepp, pmd_t **pmdpp,
-              spinlock_t **ptlp)
+int follow_invalidate_pte(struct mm_struct *mm, unsigned long address,
+                         struct mmu_notifier_range *range, pte_t **ptepp,
+                         pmd_t **pmdpp, spinlock_t **ptlp)
 {
        pgd_t *pgd;
        p4d_t *p4d;
@@ -4776,6 +4709,34 @@ out:
        return -EINVAL;
 }
 
+/**
+ * follow_pte - look up PTE at a user virtual address
+ * @mm: the mm_struct of the target address space
+ * @address: user virtual address
+ * @ptepp: location to store found PTE
+ * @ptlp: location to store the lock for the PTE
+ *
+ * On a successful return, the pointer to the PTE is stored in @ptepp;
+ * the corresponding lock is taken and its location is stored in @ptlp.
+ * The contents of the PTE are only stable until @ptlp is released;
+ * any further use, if any, must be protected against invalidation
+ * with MMU notifiers.
+ *
+ * Only IO mappings and raw PFN mappings are allowed.  The mmap semaphore
+ * should be taken for read.
+ *
+ * KVM uses this function.  While it is arguably less bad than ``follow_pfn``,
+ * it is not a good general-purpose API.
+ *
+ * Return: zero on success, -ve otherwise.
+ */
+int follow_pte(struct mm_struct *mm, unsigned long address,
+              pte_t **ptepp, spinlock_t **ptlp)
+{
+       return follow_invalidate_pte(mm, address, NULL, ptepp, NULL, ptlp);
+}
+EXPORT_SYMBOL_GPL(follow_pte);
+
 /**
  * follow_pfn - look up PFN at a user virtual address
  * @vma: memory mapping
@@ -4784,6 +4745,9 @@ out:
  *
  * Only IO mappings and raw PFN mappings are allowed.
  *
+ * This function does not allow the caller to read the permissions
+ * of the PTE.  Do not use it.
+ *
  * Return: zero and the pfn at @pfn on success, -ve otherwise.
  */
 int follow_pfn(struct vm_area_struct *vma, unsigned long address,
@@ -4796,7 +4760,7 @@ int follow_pfn(struct vm_area_struct *vma, unsigned long address,
        if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
                return ret;
 
-       ret = follow_pte(vma->vm_mm, address, NULL, &ptep, NULL, &ptl);
+       ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
        if (ret)
                return ret;
        *pfn = pte_pfn(*ptep);
@@ -4817,7 +4781,7 @@ int follow_phys(struct vm_area_struct *vma,
        if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
                goto out;
 
-       if (follow_pte(vma->vm_mm, address, NULL, &ptep, NULL, &ptl))
+       if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
                goto out;
        pte = *ptep;