Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[firefly-linux-kernel-4.4.55.git] / arch / x86 / mm / pageattr.c
index a1b1c88f9caf5f6c1eaf6a8b6a95477dad36a5f8..091934e1d0d97ca26570eb9962aa1890e08e7bd8 100644 (file)
@@ -472,6 +472,19 @@ try_preserve_large_page(pte_t *kpte, unsigned long address,
        pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr);
        pgprot_val(req_prot) |= pgprot_val(cpa->mask_set);
 
+       /*
+        * Set the PSE and GLOBAL flags only if the PRESENT flag is
+        * set otherwise pmd_present/pmd_huge will return true even on
+        * a non present pmd. The canon_pgprot will clear _PAGE_GLOBAL
+        * for the ancient hardware that doesn't support it.
+        */
+       if (pgprot_val(new_prot) & _PAGE_PRESENT)
+               pgprot_val(new_prot) |= _PAGE_PSE | _PAGE_GLOBAL;
+       else
+               pgprot_val(new_prot) &= ~(_PAGE_PSE | _PAGE_GLOBAL);
+
+       new_prot = canon_pgprot(new_prot);
+
        /*
         * old_pte points to the large page base address. So we need
         * to add the offset of the virtual address:
@@ -517,7 +530,7 @@ try_preserve_large_page(pte_t *kpte, unsigned long address,
                 * The address is aligned and the number of pages
                 * covers the full page.
                 */
-               new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
+               new_pte = pfn_pte(pte_pfn(old_pte), new_prot);
                __set_pmd_pte(kpte, address, new_pte);
                cpa->flags |= CPA_FLUSHTLB;
                do_split = 0;
@@ -529,21 +542,13 @@ out_unlock:
        return do_split;
 }
 
-static int split_large_page(pte_t *kpte, unsigned long address)
+int __split_large_page(pte_t *kpte, unsigned long address, pte_t *pbase)
 {
        unsigned long pfn, pfninc = 1;
        unsigned int i, level;
-       pte_t *pbase, *tmp;
+       pte_t *tmp;
        pgprot_t ref_prot;
-       struct page *base;
-
-       if (!debug_pagealloc)
-               spin_unlock(&cpa_lock);
-       base = alloc_pages(GFP_KERNEL | __GFP_NOTRACK, 0);
-       if (!debug_pagealloc)
-               spin_lock(&cpa_lock);
-       if (!base)
-               return -ENOMEM;
+       struct page *base = virt_to_page(pbase);
 
        spin_lock(&pgd_lock);
        /*
@@ -551,10 +556,11 @@ static int split_large_page(pte_t *kpte, unsigned long address)
         * up for us already:
         */
        tmp = lookup_address(address, &level);
-       if (tmp != kpte)
-               goto out_unlock;
+       if (tmp != kpte) {
+               spin_unlock(&pgd_lock);
+               return 1;
+       }
 
-       pbase = (pte_t *)page_address(base);
        paravirt_alloc_pte(&init_mm, page_to_pfn(base));
        ref_prot = pte_pgprot(pte_clrhuge(*kpte));
        /*
@@ -568,16 +574,35 @@ static int split_large_page(pte_t *kpte, unsigned long address)
 #ifdef CONFIG_X86_64
        if (level == PG_LEVEL_1G) {
                pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
-               pgprot_val(ref_prot) |= _PAGE_PSE;
+               /*
+                * Set the PSE flags only if the PRESENT flag is set
+                * otherwise pmd_present/pmd_huge will return true
+                * even on a non present pmd.
+                */
+               if (pgprot_val(ref_prot) & _PAGE_PRESENT)
+                       pgprot_val(ref_prot) |= _PAGE_PSE;
+               else
+                       pgprot_val(ref_prot) &= ~_PAGE_PSE;
        }
 #endif
 
+       /*
+        * Set the GLOBAL flags only if the PRESENT flag is set
+        * otherwise pmd/pte_present will return true even on a non
+        * present pmd/pte. The canon_pgprot will clear _PAGE_GLOBAL
+        * for the ancient hardware that doesn't support it.
+        */
+       if (pgprot_val(ref_prot) & _PAGE_PRESENT)
+               pgprot_val(ref_prot) |= _PAGE_GLOBAL;
+       else
+               pgprot_val(ref_prot) &= ~_PAGE_GLOBAL;
+
        /*
         * Get the target pfn from the original entry:
         */
        pfn = pte_pfn(*kpte);
        for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
-               set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
+               set_pte(&pbase[i], pfn_pte(pfn, canon_pgprot(ref_prot)));
 
        if (pfn_range_is_mapped(PFN_DOWN(__pa(address)),
                                PFN_DOWN(__pa(address)) + 1))
@@ -601,17 +626,27 @@ static int split_large_page(pte_t *kpte, unsigned long address)
         * going on.
         */
        __flush_tlb_all();
+       spin_unlock(&pgd_lock);
 
-       base = NULL;
+       return 0;
+}
 
-out_unlock:
-       /*
-        * If we dropped out via the lookup_address check under
-        * pgd_lock then stick the page back into the pool:
-        */
-       if (base)
+static int split_large_page(pte_t *kpte, unsigned long address)
+{
+       pte_t *pbase;
+       struct page *base;
+
+       if (!debug_pagealloc)
+               spin_unlock(&cpa_lock);
+       base = alloc_pages(GFP_KERNEL | __GFP_NOTRACK, 0);
+       if (!debug_pagealloc)
+               spin_lock(&cpa_lock);
+       if (!base)
+               return -ENOMEM;
+
+       pbase = (pte_t *)page_address(base);
+       if (__split_large_page(kpte, address, pbase))
                __free_page(base);
-       spin_unlock(&pgd_lock);
 
        return 0;
 }
@@ -681,6 +716,18 @@ repeat:
 
                new_prot = static_protections(new_prot, address, pfn);
 
+               /*
+                * Set the GLOBAL flags only if the PRESENT flag is
+                * set otherwise pte_present will return true even on
+                * a non present pte. The canon_pgprot will clear
+                * _PAGE_GLOBAL for the ancient hardware that doesn't
+                * support it.
+                */
+               if (pgprot_val(new_prot) & _PAGE_PRESENT)
+                       pgprot_val(new_prot) |= _PAGE_GLOBAL;
+               else
+                       pgprot_val(new_prot) &= ~_PAGE_GLOBAL;
+
                /*
                 * We need to keep the pfn from the existing PTE,
                 * after all we're only going to change it's attributes