]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - mm/mprotect.c
Merge branch 'uaccess-work.iov_iter' of git://git.kernel.org/pub/scm/linux/kernel...
[karo-tx-linux.git] / mm / mprotect.c
index f9c07f54dd62d928187985554695c3c1117c75a7..1a8c9ca83e48ec9a998ea0882e24c58dee0d3dd1 100644 (file)
 
 #include "internal.h"
 
-/*
- * For a prot_numa update we only hold mmap_sem for read so there is a
- * potential race with faulting where a pmd was temporarily none. This
- * function checks for a transhuge pmd under the appropriate lock. It
- * returns a pte if it was successfully locked or NULL if it raced with
- * a transhuge insertion.
- */
-static pte_t *lock_pte_protection(struct vm_area_struct *vma, pmd_t *pmd,
-                       unsigned long addr, int prot_numa, spinlock_t **ptl)
-{
-       pte_t *pte;
-       spinlock_t *pmdl;
-
-       /* !prot_numa is protected by mmap_sem held for write */
-       if (!prot_numa)
-               return pte_offset_map_lock(vma->vm_mm, pmd, addr, ptl);
-
-       pmdl = pmd_lock(vma->vm_mm, pmd);
-       if (unlikely(pmd_trans_huge(*pmd) || pmd_none(*pmd))) {
-               spin_unlock(pmdl);
-               return NULL;
-       }
-
-       pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, ptl);
-       spin_unlock(pmdl);
-       return pte;
-}
-
 static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
                unsigned long addr, unsigned long end, pgprot_t newprot,
                int dirty_accountable, int prot_numa)
@@ -71,10 +43,22 @@ static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
        unsigned long pages = 0;
        int target_node = NUMA_NO_NODE;
 
-       pte = lock_pte_protection(vma, pmd, addr, prot_numa, &ptl);
-       if (!pte)
+       /*
+        * Can be called with only the mmap_sem for reading by
+        * prot_numa so we must check the pmd isn't constantly
+        * changing from under us from pmd_none to pmd_trans_huge
+        * and/or the other way around.
+        */
+       if (pmd_trans_unstable(pmd))
                return 0;
 
+       /*
+        * The pmd points to a regular pte so the pmd can't change
+        * from under us even if the mmap_sem is only hold for
+        * reading.
+        */
+       pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
+
        /* Get target node for single threaded private VMAs */
        if (prot_numa && !(vma->vm_flags & VM_SHARED) &&
            atomic_read(&vma->vm_mm->mm_users) == 1)
@@ -113,7 +97,7 @@ static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
                        ptent = ptep_modify_prot_start(mm, addr, pte);
                        ptent = pte_modify(ptent, newprot);
                        if (preserve_write)
-                               ptent = pte_mkwrite(ptent);
+                               ptent = pte_mk_savedwrite(ptent);
 
                        /* Avoid taking write faults for known dirty pages */
                        if (dirty_accountable && pte_dirty(ptent) &&
@@ -177,8 +161,6 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
                if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
                        if (next - addr != HPAGE_PMD_SIZE) {
                                __split_huge_pmd(vma, pmd, addr, false, NULL);
-                               if (pmd_trans_unstable(pmd))
-                                       continue;
                        } else {
                                int nr_ptes = change_huge_pmd(vma, pmd, addr,
                                                newprot, prot_numa);
@@ -209,14 +191,14 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
 }
 
 static inline unsigned long change_pud_range(struct vm_area_struct *vma,
-               pgd_t *pgd, unsigned long addr, unsigned long end,
+               p4d_t *p4d, unsigned long addr, unsigned long end,
                pgprot_t newprot, int dirty_accountable, int prot_numa)
 {
        pud_t *pud;
        unsigned long next;
        unsigned long pages = 0;
 
-       pud = pud_offset(pgd, addr);
+       pud = pud_offset(p4d, addr);
        do {
                next = pud_addr_end(addr, end);
                if (pud_none_or_clear_bad(pud))
@@ -228,6 +210,26 @@ static inline unsigned long change_pud_range(struct vm_area_struct *vma,
        return pages;
 }
 
+static inline unsigned long change_p4d_range(struct vm_area_struct *vma,
+               pgd_t *pgd, unsigned long addr, unsigned long end,
+               pgprot_t newprot, int dirty_accountable, int prot_numa)
+{
+       p4d_t *p4d;
+       unsigned long next;
+       unsigned long pages = 0;
+
+       p4d = p4d_offset(pgd, addr);
+       do {
+               next = p4d_addr_end(addr, end);
+               if (p4d_none_or_clear_bad(p4d))
+                       continue;
+               pages += change_pud_range(vma, p4d, addr, next, newprot,
+                                dirty_accountable, prot_numa);
+       } while (p4d++, addr = next, addr != end);
+
+       return pages;
+}
+
 static unsigned long change_protection_range(struct vm_area_struct *vma,
                unsigned long addr, unsigned long end, pgprot_t newprot,
                int dirty_accountable, int prot_numa)
@@ -246,7 +248,7 @@ static unsigned long change_protection_range(struct vm_area_struct *vma,
                next = pgd_addr_end(addr, end);
                if (pgd_none_or_clear_bad(pgd))
                        continue;
-               pages += change_pud_range(vma, pgd, addr, next, newprot,
+               pages += change_p4d_range(vma, pgd, addr, next, newprot,
                                 dirty_accountable, prot_numa);
        } while (pgd++, addr = next, addr != end);