include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / arch / x86 / mm / pageattr.c
index c29ebd0..28195c3 100644 (file)
@@ -6,16 +6,19 @@
 #include <linux/bootmem.h>
 #include <linux/module.h>
 #include <linux/sched.h>
-#include <linux/slab.h>
 #include <linux/mm.h>
 #include <linux/interrupt.h>
 #include <linux/seq_file.h>
 #include <linux/debugfs.h>
+#include <linux/pfn.h>
+#include <linux/percpu.h>
+#include <linux/gfp.h>
 
 #include <asm/e820.h>
 #include <asm/processor.h>
 #include <asm/tlbflush.h>
 #include <asm/sections.h>
+#include <asm/setup.h>
 #include <asm/uaccess.h>
 #include <asm/pgalloc.h>
 #include <asm/proto.h>
  * The current flushing context - we pass it instead of 5 arguments:
  */
 struct cpa_data {
-       unsigned long   vaddr;
+       unsigned long   *vaddr;
        pgprot_t        mask_set;
        pgprot_t        mask_clr;
        int             numpages;
-       int             flushtlb;
+       int             flags;
        unsigned long   pfn;
        unsigned        force_split : 1;
+       int             curpage;
+       struct page     **pages;
 };
 
+/*
+ * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings)
+ * using cpa_lock. So that we don't allow any other cpu, with stale large tlb
+ * entries change the page attribute in parallel to some other cpu
+ * splitting a large page entry along with changing the attribute.
+ */
+static DEFINE_SPINLOCK(cpa_lock);
+
+#define CPA_FLUSHTLB 1
+#define CPA_ARRAY 2
+#define CPA_PAGES_ARRAY 4
+
+#ifdef CONFIG_PROC_FS
+static unsigned long direct_pages_count[PG_LEVEL_NUM];
+
+void update_page_count(int level, unsigned long pages)
+{
+       unsigned long flags;
+
+       /* Protect against CPA */
+       spin_lock_irqsave(&pgd_lock, flags);
+       direct_pages_count[level] += pages;
+       spin_unlock_irqrestore(&pgd_lock, flags);
+}
+
+static void split_page_count(int level)
+{
+       direct_pages_count[level]--;
+       direct_pages_count[level - 1] += PTRS_PER_PTE;
+}
+
+void arch_report_meminfo(struct seq_file *m)
+{
+       seq_printf(m, "DirectMap4k:    %8lu kB\n",
+                       direct_pages_count[PG_LEVEL_4K] << 2);
+#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
+       seq_printf(m, "DirectMap2M:    %8lu kB\n",
+                       direct_pages_count[PG_LEVEL_2M] << 11);
+#else
+       seq_printf(m, "DirectMap4M:    %8lu kB\n",
+                       direct_pages_count[PG_LEVEL_2M] << 12);
+#endif
+#ifdef CONFIG_X86_64
+       if (direct_gbpages)
+               seq_printf(m, "DirectMap1G:    %8lu kB\n",
+                       direct_pages_count[PG_LEVEL_1G] << 20);
+#endif
+}
+#else
+static inline void split_page_count(int level) { }
+#endif
+
 #ifdef CONFIG_X86_64
 
 static inline unsigned long highmap_start_pfn(void)
@@ -43,7 +100,7 @@ static inline unsigned long highmap_start_pfn(void)
 
 static inline unsigned long highmap_end_pfn(void)
 {
-       return __pa(round_up((unsigned long)_end, PMD_SIZE)) >> PAGE_SHIFT;
+       return __pa(roundup(_brk_end, PMD_SIZE)) >> PAGE_SHIFT;
 }
 
 #endif
@@ -87,6 +144,7 @@ void clflush_cache_range(void *vaddr, unsigned int size)
 
        mb();
 }
+EXPORT_SYMBOL_GPL(clflush_cache_range);
 
 static void __cpa_flush_all(void *arg)
 {
@@ -98,7 +156,7 @@ static void __cpa_flush_all(void *arg)
         */
        __flush_tlb_all();
 
-       if (cache && boot_cpu_data.x86_model >= 4)
+       if (cache && boot_cpu_data.x86 >= 4)
                wbinvd();
 }
 
@@ -106,7 +164,7 @@ static void cpa_flush_all(unsigned long cache)
 {
        BUG_ON(irqs_disabled());
 
-       on_each_cpu(__cpa_flush_all, (void *) cache, 1, 1);
+       on_each_cpu(__cpa_flush_all, (void *) cache, 1);
 }
 
 static void __cpa_flush_range(void *arg)
@@ -127,7 +185,7 @@ static void cpa_flush_range(unsigned long start, int numpages, int cache)
        BUG_ON(irqs_disabled());
        WARN_ON(PAGE_ALIGN(start) != start);
 
-       on_each_cpu(__cpa_flush_range, NULL, 1, 1);
+       on_each_cpu(__cpa_flush_range, NULL, 1);
 
        if (!cache)
                return;
@@ -149,6 +207,44 @@ static void cpa_flush_range(unsigned long start, int numpages, int cache)
        }
 }
 
+static void cpa_flush_array(unsigned long *start, int numpages, int cache,
+                           int in_flags, struct page **pages)
+{
+       unsigned int i, level;
+       unsigned long do_wbinvd = cache && numpages >= 1024; /* 4M threshold */
+
+       BUG_ON(irqs_disabled());
+
+       on_each_cpu(__cpa_flush_all, (void *) do_wbinvd, 1);
+
+       if (!cache || do_wbinvd)
+               return;
+
+       /*
+        * We only need to flush on one CPU,
+        * clflush is a MESI-coherent instruction that
+        * will cause all other CPUs to flush the same
+        * cachelines:
+        */
+       for (i = 0; i < numpages; i++) {
+               unsigned long addr;
+               pte_t *pte;
+
+               if (in_flags & CPA_PAGES_ARRAY)
+                       addr = (unsigned long)page_address(pages[i]);
+               else
+                       addr = start[i];
+
+               pte = lookup_address(addr, &level);
+
+               /*
+                * Only flush present addresses:
+                */
+               if (pte && (pte_val(*pte) & _PAGE_PRESENT))
+                       clflush_cache_range((void *)addr, PAGE_SIZE);
+       }
+}
+
 /*
  * Certain areas of memory on x86 require very specific protection flags,
  * for example the BIOS area or kernel text. Callers don't always get this
@@ -183,6 +279,43 @@ static inline pgprot_t static_protections(pgprot_t prot, unsigned long address,
                   __pa((unsigned long)__end_rodata) >> PAGE_SHIFT))
                pgprot_val(forbidden) |= _PAGE_RW;
 
+#if defined(CONFIG_X86_64) && defined(CONFIG_DEBUG_RODATA)
+       /*
+        * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
+        * kernel text mappings for the large page aligned text, rodata sections
+        * will be always read-only. For the kernel identity mappings covering
+        * the holes caused by this alignment can be anything that user asks.
+        *
+        * This will preserve the large page mappings for kernel text/data
+        * at no extra cost.
+        */
+       if (kernel_set_to_readonly &&
+           within(address, (unsigned long)_text,
+                  (unsigned long)__end_rodata_hpage_align)) {
+               unsigned int level;
+
+               /*
+                * Don't enforce the !RW mapping for the kernel text mapping,
+                * if the current mapping is already using small page mapping.
+                * No need to work hard to preserve large page mappings in this
+                * case.
+                *
+                * This also fixes the Linux Xen paravirt guest boot failure
+                * (because of unexpected read-only mappings for kernel identity
+                * mappings). In this paravirt guest case, the kernel text
+                * mapping and the kernel identity mapping share the same
+                * page-table pages. Thus we can't really use different
+                * protections for the kernel text and identity mappings. Also,
+                * these shared mappings are made of small page mappings.
+                * Thus this don't enforce !RW mapping for small page kernel
+                * text mapping logic will help Linux Xen parvirt guest boot
+                * aswell.
+                */
+               if (lookup_address(address, &level) && (level != PG_LEVEL_4K))
+                       pgprot_val(forbidden) |= _PAGE_RW;
+       }
+#endif
+
        prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
 
        return prot;
@@ -227,6 +360,7 @@ pte_t *lookup_address(unsigned long address, unsigned int *level)
 
        return pte_offset_kernel(pmd, address);
 }
+EXPORT_SYMBOL_GPL(lookup_address);
 
 /*
  * Set the new pmd in all the pgds we know about:
@@ -356,7 +490,7 @@ try_preserve_large_page(pte_t *kpte, unsigned long address,
                 */
                new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
                __set_pmd_pte(kpte, address, new_pte);
-               cpa->flushtlb = 1;
+               cpa->flags |= CPA_FLUSHTLB;
                do_split = 0;
        }
 
@@ -366,84 +500,6 @@ out_unlock:
        return do_split;
 }
 
-static LIST_HEAD(page_pool);
-static unsigned long pool_size, pool_pages, pool_low;
-static unsigned long pool_used, pool_failed;
-
-static void cpa_fill_pool(struct page **ret)
-{
-       gfp_t gfp = GFP_KERNEL;
-       unsigned long flags;
-       struct page *p;
-
-       /*
-        * Avoid recursion (on debug-pagealloc) and also signal
-        * our priority to get to these pagetables:
-        */
-       if (current->flags & PF_MEMALLOC)
-               return;
-       current->flags |= PF_MEMALLOC;
-
-       /*
-        * Allocate atomically from atomic contexts:
-        */
-       if (in_atomic() || irqs_disabled() || debug_pagealloc)
-               gfp =  GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
-
-       while (pool_pages < pool_size || (ret && !*ret)) {
-               p = alloc_pages(gfp, 0);
-               if (!p) {
-                       pool_failed++;
-                       break;
-               }
-               /*
-                * If the call site needs a page right now, provide it:
-                */
-               if (ret && !*ret) {
-                       *ret = p;
-                       continue;
-               }
-               spin_lock_irqsave(&pgd_lock, flags);
-               list_add(&p->lru, &page_pool);
-               pool_pages++;
-               spin_unlock_irqrestore(&pgd_lock, flags);
-       }
-
-       current->flags &= ~PF_MEMALLOC;
-}
-
-#define SHIFT_MB               (20 - PAGE_SHIFT)
-#define ROUND_MB_GB            ((1 << 10) - 1)
-#define SHIFT_MB_GB            10
-#define POOL_PAGES_PER_GB      16
-
-void __init cpa_init(void)
-{
-       struct sysinfo si;
-       unsigned long gb;
-
-       si_meminfo(&si);
-       /*
-        * Calculate the number of pool pages:
-        *
-        * Convert totalram (nr of pages) to MiB and round to the next
-        * GiB. Shift MiB to Gib and multiply the result by
-        * POOL_PAGES_PER_GB:
-        */
-       if (debug_pagealloc) {
-               gb = ((si.totalram >> SHIFT_MB) + ROUND_MB_GB) >> SHIFT_MB_GB;
-               pool_size = POOL_PAGES_PER_GB * gb;
-       } else {
-               pool_size = 1;
-       }
-       pool_low = pool_size;
-
-       cpa_fill_pool(NULL);
-       printk(KERN_DEBUG
-              "CPA: page pool initialized %lu of %lu pages preallocated\n",
-              pool_pages, pool_size);
-}
-
 static int split_large_page(pte_t *kpte, unsigned long address)
 {
        unsigned long flags, pfn, pfninc = 1;
@@ -452,28 +508,15 @@ static int split_large_page(pte_t *kpte, unsigned long address)
        pgprot_t ref_prot;
        struct page *base;
 
-       /*
-        * Get a page from the pool. The pool list is protected by the
-        * pgd_lock, which we have to take anyway for the split
-        * operation:
-        */
-       spin_lock_irqsave(&pgd_lock, flags);
-       if (list_empty(&page_pool)) {
-               spin_unlock_irqrestore(&pgd_lock, flags);
-               base = NULL;
-               cpa_fill_pool(&base);
-               if (!base)
-                       return -ENOMEM;
-               spin_lock_irqsave(&pgd_lock, flags);
-       } else {
-               base = list_first_entry(&page_pool, struct page, lru);
-               list_del(&base->lru);
-               pool_pages--;
-
-               if (pool_pages < pool_low)
-                       pool_low = pool_pages;
-       }
+       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;
 
+       spin_lock_irqsave(&pgd_lock, flags);
        /*
         * Check for races, another CPU might have split this page
         * up for us already:
@@ -483,10 +526,15 @@ static int split_large_page(pte_t *kpte, unsigned long address)
                goto out_unlock;
 
        pbase = (pte_t *)page_address(base);
-#ifdef CONFIG_X86_32
-       paravirt_alloc_pt(&init_mm, page_to_pfn(base));
-#endif
+       paravirt_alloc_pte(&init_mm, page_to_pfn(base));
        ref_prot = pte_pgprot(pte_clrhuge(*kpte));
+       /*
+        * If we ever want to utilize the PAT bit, we need to
+        * update this function to make sure it's converted from
+        * bit 12 to bit 7 when we cross from the 2MB level to
+        * the 4K level:
+        */
+       WARN_ON_ONCE(pgprot_val(ref_prot) & _PAGE_PAT_LARGE);
 
 #ifdef CONFIG_X86_64
        if (level == PG_LEVEL_1G) {
@@ -502,19 +550,35 @@ static int split_large_page(pte_t *kpte, unsigned long address)
        for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
                set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
 
+       if (address >= (unsigned long)__va(0) &&
+               address < (unsigned long)__va(max_low_pfn_mapped << PAGE_SHIFT))
+               split_page_count(level);
+
+#ifdef CONFIG_X86_64
+       if (address >= (unsigned long)__va(1UL<<32) &&
+               address < (unsigned long)__va(max_pfn_mapped << PAGE_SHIFT))
+               split_page_count(level);
+#endif
+
        /*
-        * Install the new, split up pagetable. Important details here:
+        * Install the new, split up pagetable.
         *
-        * On Intel the NX bit of all levels must be cleared to make a
-        * page executable. See section 4.13.2 of Intel 64 and IA-32
-        * Architectures Software Developer's Manual).
+        * We use the standard kernel pagetable protections for the new
+        * pagetable protections, the actual ptes set above control the
+        * primary protection behavior:
+        */
+       __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
+
+       /*
+        * Intel Atom errata AAH41 workaround.
         *
-        * Mark the entry present. The current mapping might be
-        * set to not present, which we preserved above.
+        * The real fix should be in hw or in a microcode update, but
+        * we also probabilistically try to reduce the window of having
+        * a large TLB mixed with 4K TLBs while instruction fetches are
+        * going on.
         */
-       ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
-       pgprot_val(ref_prot) |= _PAGE_PRESENT;
-       __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
+       __flush_tlb_all();
+
        base = NULL;
 
 out_unlock:
@@ -522,38 +586,67 @@ out_unlock:
         * If we dropped out via the lookup_address check under
         * pgd_lock then stick the page back into the pool:
         */
-       if (base) {
-               list_add(&base->lru, &page_pool);
-               pool_pages++;
-       } else
-               pool_used++;
+       if (base)
+               __free_page(base);
        spin_unlock_irqrestore(&pgd_lock, flags);
 
        return 0;
 }
 
+static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
+                              int primary)
+{
+       /*
+        * Ignore all non primary paths.
+        */
+       if (!primary)
+               return 0;
+
+       /*
+        * Ignore the NULL PTE for kernel identity mapping, as it is expected
+        * to have holes.
+        * Also set numpages to '1' indicating that we processed cpa req for
+        * one virtual address page and its pfn. TBD: numpages can be set based
+        * on the initial value and the level returned by lookup_address().
+        */
+       if (within(vaddr, PAGE_OFFSET,
+                  PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
+               cpa->numpages = 1;
+               cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
+               return 0;
+       } else {
+               WARN(1, KERN_WARNING "CPA: called for zero pte. "
+                       "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
+                       *cpa->vaddr);
+
+               return -EFAULT;
+       }
+}
+
 static int __change_page_attr(struct cpa_data *cpa, int primary)
 {
-       unsigned long address = cpa->vaddr;
+       unsigned long address;
        int do_split, err;
        unsigned int level;
        pte_t *kpte, old_pte;
 
+       if (cpa->flags & CPA_PAGES_ARRAY) {
+               struct page *page = cpa->pages[cpa->curpage];
+               if (unlikely(PageHighMem(page)))
+                       return 0;
+               address = (unsigned long)page_address(page);
+       } else if (cpa->flags & CPA_ARRAY)
+               address = cpa->vaddr[cpa->curpage];
+       else
+               address = *cpa->vaddr;
 repeat:
        kpte = lookup_address(address, &level);
        if (!kpte)
-               return 0;
+               return __cpa_process_fault(cpa, address, primary);
 
        old_pte = *kpte;
-       if (!pte_val(old_pte)) {
-               if (!primary)
-                       return 0;
-               printk(KERN_WARNING "CPA: called for zero pte. "
-                      "vaddr = %lx cpa->vaddr = %lx\n", address,
-                      cpa->vaddr);
-               WARN_ON(1);
-               return -EINVAL;
-       }
+       if (!pte_val(old_pte))
+               return __cpa_process_fault(cpa, address, primary);
 
        if (level == PG_LEVEL_4K) {
                pte_t new_pte;
@@ -577,7 +670,7 @@ repeat:
                 */
                if (pte_val(old_pte) != pte_val(new_pte)) {
                        set_pte_atomic(kpte, new_pte);
-                       cpa->flushtlb = 1;
+                       cpa->flags |= CPA_FLUSHTLB;
                }
                cpa->numpages = 1;
                return 0;
@@ -601,7 +694,25 @@ repeat:
         */
        err = split_large_page(kpte, address);
        if (!err) {
-               cpa->flushtlb = 1;
+               /*
+                * Do a global flush tlb after splitting the large page
+                * and before we do the actual change page attribute in the PTE.
+                *
+                * With out this, we violate the TLB application note, that says
+                * "The TLBs may contain both ordinary and large-page
+                *  translations for a 4-KByte range of linear addresses. This
+                *  may occur if software modifies the paging structures so that
+                *  the page size used for the address range changes. If the two
+                *  translations differ with respect to page frame or attributes
+                *  (e.g., permissions), processor behavior is undefined and may
+                *  be implementation-specific."
+                *
+                * We do this global tlb flush inside the cpa_lock, so that we
+                * don't allow any other cpu, with stale tlb entries change the
+                * page attribute in parallel, that also falls into the
+                * just split large page entry.
+                */
+               flush_tlb_all();
                goto repeat;
        }
 
@@ -613,51 +724,66 @@ static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
 static int cpa_process_alias(struct cpa_data *cpa)
 {
        struct cpa_data alias_cpa;
-       int ret = 0;
+       unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
+       unsigned long vaddr;
+       int ret;
 
-       if (cpa->pfn > max_pfn_mapped)
+       if (cpa->pfn >= max_pfn_mapped)
                return 0;
 
+#ifdef CONFIG_X86_64
+       if (cpa->pfn >= max_low_pfn_mapped && cpa->pfn < (1UL<<(32-PAGE_SHIFT)))
+               return 0;
+#endif
        /*
         * No need to redo, when the primary call touched the direct
         * mapping already:
         */
-       if (!within(cpa->vaddr, PAGE_OFFSET,
-                   PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
+       if (cpa->flags & CPA_PAGES_ARRAY) {
+               struct page *page = cpa->pages[cpa->curpage];
+               if (unlikely(PageHighMem(page)))
+                       return 0;
+               vaddr = (unsigned long)page_address(page);
+       } else if (cpa->flags & CPA_ARRAY)
+               vaddr = cpa->vaddr[cpa->curpage];
+       else
+               vaddr = *cpa->vaddr;
+
+       if (!(within(vaddr, PAGE_OFFSET,
+                   PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
 
                alias_cpa = *cpa;
-               alias_cpa.vaddr = (unsigned long) __va(cpa->pfn << PAGE_SHIFT);
+               alias_cpa.vaddr = &laddr;
+               alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
 
                ret = __change_page_attr_set_clr(&alias_cpa, 0);
+               if (ret)
+                       return ret;
        }
 
 #ifdef CONFIG_X86_64
-       if (ret)
-               return ret;
        /*
-        * No need to redo, when the primary call touched the high
-        * mapping already:
-        */
-       if (within(cpa->vaddr, (unsigned long) _text, (unsigned long) _end))
-               return 0;
-
-       /*
-        * If the physical address is inside the kernel map, we need
+        * If the primary call didn't touch the high mapping already
+        * and the physical address is inside the kernel map, we need
         * to touch the high mapped kernel as well:
         */
-       if (!within(cpa->pfn, highmap_start_pfn(), highmap_end_pfn()))
-               return 0;
-
-       alias_cpa = *cpa;
-       alias_cpa.vaddr =
-               (cpa->pfn << PAGE_SHIFT) + __START_KERNEL_map - phys_base;
+       if (!within(vaddr, (unsigned long)_text, _brk_end) &&
+           within(cpa->pfn, highmap_start_pfn(), highmap_end_pfn())) {
+               unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
+                                              __START_KERNEL_map - phys_base;
+               alias_cpa = *cpa;
+               alias_cpa.vaddr = &temp_cpa_vaddr;
+               alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
 
-       /*
-        * The high mapping range is imprecise, so ignore the return value.
-        */
-       __change_page_attr_set_clr(&alias_cpa, 0);
+               /*
+                * The high mapping range is imprecise, so ignore the
+                * return value.
+                */
+               __change_page_attr_set_clr(&alias_cpa, 0);
+       }
 #endif
-       return ret;
+
+       return 0;
 }
 
 static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
@@ -670,8 +796,15 @@ static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
                 * preservation check.
                 */
                cpa->numpages = numpages;
+               /* for array changes, we can't use large page */
+               if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY))
+                       cpa->numpages = 1;
 
+               if (!debug_pagealloc)
+                       spin_lock(&cpa_lock);
                ret = __change_page_attr(cpa, checkalias);
+               if (!debug_pagealloc)
+                       spin_unlock(&cpa_lock);
                if (ret)
                        return ret;
 
@@ -688,7 +821,11 @@ static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
                 */
                BUG_ON(cpa->numpages > numpages);
                numpages -= cpa->numpages;
-               cpa->vaddr += cpa->numpages * PAGE_SIZE;
+               if (cpa->flags & (CPA_PAGES_ARRAY | CPA_ARRAY))
+                       cpa->curpage++;
+               else
+                       *cpa->vaddr += cpa->numpages * PAGE_SIZE;
+
        }
        return 0;
 }
@@ -699,12 +836,14 @@ static inline int cache_attr(pgprot_t attr)
                (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
 }
 
-static int change_page_attr_set_clr(unsigned long addr, int numpages,
+static int change_page_attr_set_clr(unsigned long *addr, int numpages,
                                    pgprot_t mask_set, pgprot_t mask_clr,
-                                   int force_split)
+                                   int force_split, int in_flag,
+                                   struct page **pages)
 {
        struct cpa_data cpa;
        int ret, cache, checkalias;
+       unsigned long baddr = 0;
 
        /*
         * Check, if we are requested to change a not supported
@@ -716,21 +855,50 @@ static int change_page_attr_set_clr(unsigned long addr, int numpages,
                return 0;
 
        /* Ensure we are PAGE_SIZE aligned */
-       if (addr & ~PAGE_MASK) {
-               addr &= PAGE_MASK;
+       if (in_flag & CPA_ARRAY) {
+               int i;
+               for (i = 0; i < numpages; i++) {
+                       if (addr[i] & ~PAGE_MASK) {
+                               addr[i] &= PAGE_MASK;
+                               WARN_ON_ONCE(1);
+                       }
+               }
+       } else if (!(in_flag & CPA_PAGES_ARRAY)) {
                /*
-                * People should not be passing in unaligned addresses:
+                * in_flag of CPA_PAGES_ARRAY implies it is aligned.
+                * No need to cehck in that case
                 */
-               WARN_ON_ONCE(1);
+               if (*addr & ~PAGE_MASK) {
+                       *addr &= PAGE_MASK;
+                       /*
+                        * People should not be passing in unaligned addresses:
+                        */
+                       WARN_ON_ONCE(1);
+               }
+               /*
+                * Save address for cache flush. *addr is modified in the call
+                * to __change_page_attr_set_clr() below.
+                */
+               baddr = *addr;
        }
 
+       /* Must avoid aliasing mappings in the highmem code */
+       kmap_flush_unused();
+
+       vm_unmap_aliases();
+
        cpa.vaddr = addr;
+       cpa.pages = pages;
        cpa.numpages = numpages;
        cpa.mask_set = mask_set;
        cpa.mask_clr = mask_clr;
-       cpa.flushtlb = 0;
+       cpa.flags = 0;
+       cpa.curpage = 0;
        cpa.force_split = force_split;
 
+       if (in_flag & (CPA_ARRAY | CPA_PAGES_ARRAY))
+               cpa.flags |= in_flag;
+
        /* No alias checking for _NX bit modifications */
        checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
 
@@ -739,7 +907,7 @@ static int change_page_attr_set_clr(unsigned long addr, int numpages,
        /*
         * Check whether we really changed something:
         */
-       if (!cpa.flushtlb)
+       if (!(cpa.flags & CPA_FLUSHTLB))
                goto out;
 
        /*
@@ -754,109 +922,227 @@ static int change_page_attr_set_clr(unsigned long addr, int numpages,
         * error case we fall back to cpa_flush_all (which uses
         * wbindv):
         */
-       if (!ret && cpu_has_clflush)
-               cpa_flush_range(addr, numpages, cache);
-       else
+       if (!ret && cpu_has_clflush) {
+               if (cpa.flags & (CPA_PAGES_ARRAY | CPA_ARRAY)) {
+                       cpa_flush_array(addr, numpages, cache,
+                                       cpa.flags, pages);
+               } else
+                       cpa_flush_range(baddr, numpages, cache);
+       } else
                cpa_flush_all(cache);
 
 out:
-       cpa_fill_pool(NULL);
-
        return ret;
 }
 
-static inline int change_page_attr_set(unsigned long addr, int numpages,
+static inline int change_page_attr_set(unsigned long *addr, int numpages,
+                                      pgprot_t mask, int array)
+{
+       return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
+               (array ? CPA_ARRAY : 0), NULL);
+}
+
+static inline int change_page_attr_clear(unsigned long *addr, int numpages,
+                                        pgprot_t mask, int array)
+{
+       return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
+               (array ? CPA_ARRAY : 0), NULL);
+}
+
+static inline int cpa_set_pages_array(struct page **pages, int numpages,
                                       pgprot_t mask)
 {
-       return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0);
+       return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0,
+               CPA_PAGES_ARRAY, pages);
 }
 
-static inline int change_page_attr_clear(unsigned long addr, int numpages,
+static inline int cpa_clear_pages_array(struct page **pages, int numpages,
                                         pgprot_t mask)
 {
-       return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0);
+       return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
+               CPA_PAGES_ARRAY, pages);
 }
 
 int _set_memory_uc(unsigned long addr, int numpages)
 {
-       return change_page_attr_set(addr, numpages,
-                                   __pgprot(_PAGE_CACHE_UC));
+       /*
+        * for now UC MINUS. see comments in ioremap_nocache()
+        */
+       return change_page_attr_set(&addr, numpages,
+                                   __pgprot(_PAGE_CACHE_UC_MINUS), 0);
 }
 
 int set_memory_uc(unsigned long addr, int numpages)
 {
-       if (reserve_memtype(addr, addr + numpages * PAGE_SIZE,
-                           _PAGE_CACHE_UC, NULL))
-               return -EINVAL;
+       int ret;
 
-       return _set_memory_uc(addr, numpages);
+       /*
+        * for now UC MINUS. see comments in ioremap_nocache()
+        */
+       ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
+                           _PAGE_CACHE_UC_MINUS, NULL);
+       if (ret)
+               goto out_err;
+
+       ret = _set_memory_uc(addr, numpages);
+       if (ret)
+               goto out_free;
+
+       return 0;
+
+out_free:
+       free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
+out_err:
+       return ret;
 }
 EXPORT_SYMBOL(set_memory_uc);
 
+int set_memory_array_uc(unsigned long *addr, int addrinarray)
+{
+       int i, j;
+       int ret;
+
+       /*
+        * for now UC MINUS. see comments in ioremap_nocache()
+        */
+       for (i = 0; i < addrinarray; i++) {
+               ret = reserve_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE,
+                                       _PAGE_CACHE_UC_MINUS, NULL);
+               if (ret)
+                       goto out_free;
+       }
+
+       ret = change_page_attr_set(addr, addrinarray,
+                                   __pgprot(_PAGE_CACHE_UC_MINUS), 1);
+       if (ret)
+               goto out_free;
+
+       return 0;
+
+out_free:
+       for (j = 0; j < i; j++)
+               free_memtype(__pa(addr[j]), __pa(addr[j]) + PAGE_SIZE);
+
+       return ret;
+}
+EXPORT_SYMBOL(set_memory_array_uc);
+
 int _set_memory_wc(unsigned long addr, int numpages)
 {
-       return change_page_attr_set(addr, numpages,
-                                   __pgprot(_PAGE_CACHE_WC));
+       int ret;
+       unsigned long addr_copy = addr;
+
+       ret = change_page_attr_set(&addr, numpages,
+                                   __pgprot(_PAGE_CACHE_UC_MINUS), 0);
+       if (!ret) {
+               ret = change_page_attr_set_clr(&addr_copy, numpages,
+                                              __pgprot(_PAGE_CACHE_WC),
+                                              __pgprot(_PAGE_CACHE_MASK),
+                                              0, 0, NULL);
+       }
+       return ret;
 }
 
 int set_memory_wc(unsigned long addr, int numpages)
 {
-       if (!pat_wc_enabled)
+       int ret;
+
+       if (!pat_enabled)
                return set_memory_uc(addr, numpages);
 
-       if (reserve_memtype(addr, addr + numpages * PAGE_SIZE,
-               _PAGE_CACHE_WC, NULL))
-               return -EINVAL;
+       ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
+               _PAGE_CACHE_WC, NULL);
+       if (ret)
+               goto out_err;
 
-       return _set_memory_wc(addr, numpages);
+       ret = _set_memory_wc(addr, numpages);
+       if (ret)
+               goto out_free;
+
+       return 0;
+
+out_free:
+       free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
+out_err:
+       return ret;
 }
 EXPORT_SYMBOL(set_memory_wc);
 
 int _set_memory_wb(unsigned long addr, int numpages)
 {
-       return change_page_attr_clear(addr, numpages,
-                                     __pgprot(_PAGE_CACHE_MASK));
+       return change_page_attr_clear(&addr, numpages,
+                                     __pgprot(_PAGE_CACHE_MASK), 0);
 }
 
 int set_memory_wb(unsigned long addr, int numpages)
 {
-       free_memtype(addr, addr + numpages * PAGE_SIZE);
+       int ret;
 
-       return _set_memory_wb(addr, numpages);
+       ret = _set_memory_wb(addr, numpages);
+       if (ret)
+               return ret;
+
+       free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
+       return 0;
 }
 EXPORT_SYMBOL(set_memory_wb);
 
+int set_memory_array_wb(unsigned long *addr, int addrinarray)
+{
+       int i;
+       int ret;
+
+       ret = change_page_attr_clear(addr, addrinarray,
+                                     __pgprot(_PAGE_CACHE_MASK), 1);
+       if (ret)
+               return ret;
+
+       for (i = 0; i < addrinarray; i++)
+               free_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE);
+
+       return 0;
+}
+EXPORT_SYMBOL(set_memory_array_wb);
+
 int set_memory_x(unsigned long addr, int numpages)
 {
-       return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
+       if (!(__supported_pte_mask & _PAGE_NX))
+               return 0;
+
+       return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
 }
 EXPORT_SYMBOL(set_memory_x);
 
 int set_memory_nx(unsigned long addr, int numpages)
 {
-       return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
+       if (!(__supported_pte_mask & _PAGE_NX))
+               return 0;
+
+       return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
 }
 EXPORT_SYMBOL(set_memory_nx);
 
 int set_memory_ro(unsigned long addr, int numpages)
 {
-       return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
+       return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
 }
+EXPORT_SYMBOL_GPL(set_memory_ro);
 
 int set_memory_rw(unsigned long addr, int numpages)
 {
-       return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
+       return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
 }
+EXPORT_SYMBOL_GPL(set_memory_rw);
 
 int set_memory_np(unsigned long addr, int numpages)
 {
-       return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
+       return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
 }
 
 int set_memory_4k(unsigned long addr, int numpages)
 {
-       return change_page_attr_set_clr(addr, numpages, __pgprot(0),
-                                       __pgprot(0), 1);
+       return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
+                                       __pgprot(0), 1, 0, NULL);
 }
 
 int set_pages_uc(struct page *page, int numpages)
@@ -867,6 +1153,39 @@ int set_pages_uc(struct page *page, int numpages)
 }
 EXPORT_SYMBOL(set_pages_uc);
 
+int set_pages_array_uc(struct page **pages, int addrinarray)
+{
+       unsigned long start;
+       unsigned long end;
+       int i;
+       int free_idx;
+
+       for (i = 0; i < addrinarray; i++) {
+               if (PageHighMem(pages[i]))
+                       continue;
+               start = page_to_pfn(pages[i]) << PAGE_SHIFT;
+               end = start + PAGE_SIZE;
+               if (reserve_memtype(start, end, _PAGE_CACHE_UC_MINUS, NULL))
+                       goto err_out;
+       }
+
+       if (cpa_set_pages_array(pages, addrinarray,
+                       __pgprot(_PAGE_CACHE_UC_MINUS)) == 0) {
+               return 0; /* Success */
+       }
+err_out:
+       free_idx = i;
+       for (i = 0; i < free_idx; i++) {
+               if (PageHighMem(pages[i]))
+                       continue;
+               start = page_to_pfn(pages[i]) << PAGE_SHIFT;
+               end = start + PAGE_SIZE;
+               free_memtype(start, end);
+       }
+       return -EINVAL;
+}
+EXPORT_SYMBOL(set_pages_array_uc);
+
 int set_pages_wb(struct page *page, int numpages)
 {
        unsigned long addr = (unsigned long)page_address(page);
@@ -875,6 +1194,30 @@ int set_pages_wb(struct page *page, int numpages)
 }
 EXPORT_SYMBOL(set_pages_wb);
 
+int set_pages_array_wb(struct page **pages, int addrinarray)
+{
+       int retval;
+       unsigned long start;
+       unsigned long end;
+       int i;
+
+       retval = cpa_clear_pages_array(pages, addrinarray,
+                       __pgprot(_PAGE_CACHE_MASK));
+       if (retval)
+               return retval;
+
+       for (i = 0; i < addrinarray; i++) {
+               if (PageHighMem(pages[i]))
+                       continue;
+               start = page_to_pfn(pages[i]) << PAGE_SHIFT;
+               end = start + PAGE_SIZE;
+               free_memtype(start, end);
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL(set_pages_array_wb);
+
 int set_pages_x(struct page *page, int numpages)
 {
        unsigned long addr = (unsigned long)page_address(page);
@@ -909,22 +1252,38 @@ int set_pages_rw(struct page *page, int numpages)
 
 static int __set_pages_p(struct page *page, int numpages)
 {
-       struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
+       unsigned long tempaddr = (unsigned long) page_address(page);
+       struct cpa_data cpa = { .vaddr = &tempaddr,
                                .numpages = numpages,
                                .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
-                               .mask_clr = __pgprot(0)};
+                               .mask_clr = __pgprot(0),
+                               .flags = 0};
 
-       return __change_page_attr_set_clr(&cpa, 1);
+       /*
+        * No alias checking needed for setting present flag. otherwise,
+        * we may need to break large pages for 64-bit kernel text
+        * mappings (this adds to complexity if we want to do this from
+        * atomic context especially). Let's keep it simple!
+        */
+       return __change_page_attr_set_clr(&cpa, 0);
 }
 
 static int __set_pages_np(struct page *page, int numpages)
 {
-       struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
+       unsigned long tempaddr = (unsigned long) page_address(page);
+       struct cpa_data cpa = { .vaddr = &tempaddr,
                                .numpages = numpages,
                                .mask_set = __pgprot(0),
-                               .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW)};
+                               .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
+                               .flags = 0};
 
-       return __change_page_attr_set_clr(&cpa, 1);
+       /*
+        * No alias checking needed for setting not present flag. otherwise,
+        * we may need to break large pages for 64-bit kernel text
+        * mappings (this adds to complexity if we want to do this from
+        * atomic context especially). Let's keep it simple!
+        */
+       return __change_page_attr_set_clr(&cpa, 0);
 }
 
 void kernel_map_pages(struct page *page, int numpages, int enable)
@@ -944,11 +1303,8 @@ void kernel_map_pages(struct page *page, int numpages, int enable)
 
        /*
         * The return value is ignored as the calls cannot fail.
-        * Large pages are kept enabled at boot time, and are
-        * split up quickly with DEBUG_PAGEALLOC. If a splitup
-        * fails here (due to temporary memory shortage) no damage
-        * is done because we just keep the largepage intact up
-        * to the next attempt when it will likely be split up:
+        * Large pages for identity mappings are not used at boot time
+        * and hence no memory allocations during large page split.
         */
        if (enable)
                __set_pages_p(page, numpages);
@@ -960,53 +1316,8 @@ void kernel_map_pages(struct page *page, int numpages, int enable)
         * but that can deadlock->flush only current cpu:
         */
        __flush_tlb_all();
-
-       /*
-        * Try to refill the page pool here. We can do this only after
-        * the tlb flush.
-        */
-       cpa_fill_pool(NULL);
 }
 
-#ifdef CONFIG_DEBUG_FS
-static int dpa_show(struct seq_file *m, void *v)
-{
-       seq_puts(m, "DEBUG_PAGEALLOC\n");
-       seq_printf(m, "pool_size     : %lu\n", pool_size);
-       seq_printf(m, "pool_pages    : %lu\n", pool_pages);
-       seq_printf(m, "pool_low      : %lu\n", pool_low);
-       seq_printf(m, "pool_used     : %lu\n", pool_used);
-       seq_printf(m, "pool_failed   : %lu\n", pool_failed);
-
-       return 0;
-}
-
-static int dpa_open(struct inode *inode, struct file *filp)
-{
-       return single_open(filp, dpa_show, NULL);
-}
-
-static const struct file_operations dpa_fops = {
-       .open           = dpa_open,
-       .read           = seq_read,
-       .llseek         = seq_lseek,
-       .release        = single_release,
-};
-
-static int __init debug_pagealloc_proc_init(void)
-{
-       struct dentry *de;
-
-       de = debugfs_create_file("debug_pagealloc", 0600, NULL, NULL,
-                                &dpa_fops);
-       if (!de)
-               return -ENOMEM;
-
-       return 0;
-}
-__initcall(debug_pagealloc_proc_init);
-#endif
-
 #ifdef CONFIG_HIBERNATION
 
 bool kernel_page_present(struct page *page)