Merge git://git.kernel.org/pub/scm/linux/kernel/git/mingo/linux-2.6-sched
[sfrench/cifs-2.6.git] / arch / x86 / mm / pageattr.c
1 /*
2  * Copyright 2002 Andi Kleen, SuSE Labs.
3  * Thanks to Ben LaHaise for precious feedback.
4  */
5 #include <linux/highmem.h>
6 #include <linux/bootmem.h>
7 #include <linux/module.h>
8 #include <linux/sched.h>
9 #include <linux/slab.h>
10 #include <linux/mm.h>
11 #include <linux/interrupt.h>
12
13 #include <asm/e820.h>
14 #include <asm/processor.h>
15 #include <asm/tlbflush.h>
16 #include <asm/sections.h>
17 #include <asm/uaccess.h>
18 #include <asm/pgalloc.h>
19 #include <asm/proto.h>
20
21 /*
22  * The current flushing context - we pass it instead of 5 arguments:
23  */
24 struct cpa_data {
25         unsigned long   vaddr;
26         pgprot_t        mask_set;
27         pgprot_t        mask_clr;
28         int             numpages;
29         int             processed;
30         int             flushtlb;
31         unsigned long   pfn;
32 };
33
34 #ifdef CONFIG_X86_64
35
36 static inline unsigned long highmap_start_pfn(void)
37 {
38         return __pa(_text) >> PAGE_SHIFT;
39 }
40
41 static inline unsigned long highmap_end_pfn(void)
42 {
43         return __pa(round_up((unsigned long)_end, PMD_SIZE)) >> PAGE_SHIFT;
44 }
45
46 #endif
47
48 #ifdef CONFIG_DEBUG_PAGEALLOC
49 # define debug_pagealloc 1
50 #else
51 # define debug_pagealloc 0
52 #endif
53
54 static inline int
55 within(unsigned long addr, unsigned long start, unsigned long end)
56 {
57         return addr >= start && addr < end;
58 }
59
60 /*
61  * Flushing functions
62  */
63
64 /**
65  * clflush_cache_range - flush a cache range with clflush
66  * @addr:       virtual start address
67  * @size:       number of bytes to flush
68  *
69  * clflush is an unordered instruction which needs fencing with mfence
70  * to avoid ordering issues.
71  */
72 void clflush_cache_range(void *vaddr, unsigned int size)
73 {
74         void *vend = vaddr + size - 1;
75
76         mb();
77
78         for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
79                 clflush(vaddr);
80         /*
81          * Flush any possible final partial cacheline:
82          */
83         clflush(vend);
84
85         mb();
86 }
87
88 static void __cpa_flush_all(void *arg)
89 {
90         unsigned long cache = (unsigned long)arg;
91
92         /*
93          * Flush all to work around Errata in early athlons regarding
94          * large page flushing.
95          */
96         __flush_tlb_all();
97
98         if (cache && boot_cpu_data.x86_model >= 4)
99                 wbinvd();
100 }
101
102 static void cpa_flush_all(unsigned long cache)
103 {
104         BUG_ON(irqs_disabled());
105
106         on_each_cpu(__cpa_flush_all, (void *) cache, 1, 1);
107 }
108
109 static void __cpa_flush_range(void *arg)
110 {
111         /*
112          * We could optimize that further and do individual per page
113          * tlb invalidates for a low number of pages. Caveat: we must
114          * flush the high aliases on 64bit as well.
115          */
116         __flush_tlb_all();
117 }
118
119 static void cpa_flush_range(unsigned long start, int numpages, int cache)
120 {
121         unsigned int i, level;
122         unsigned long addr;
123
124         BUG_ON(irqs_disabled());
125         WARN_ON(PAGE_ALIGN(start) != start);
126
127         on_each_cpu(__cpa_flush_range, NULL, 1, 1);
128
129         if (!cache)
130                 return;
131
132         /*
133          * We only need to flush on one CPU,
134          * clflush is a MESI-coherent instruction that
135          * will cause all other CPUs to flush the same
136          * cachelines:
137          */
138         for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
139                 pte_t *pte = lookup_address(addr, &level);
140
141                 /*
142                  * Only flush present addresses:
143                  */
144                 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
145                         clflush_cache_range((void *) addr, PAGE_SIZE);
146         }
147 }
148
149 /*
150  * Certain areas of memory on x86 require very specific protection flags,
151  * for example the BIOS area or kernel text. Callers don't always get this
152  * right (again, ioremap() on BIOS memory is not uncommon) so this function
153  * checks and fixes these known static required protection bits.
154  */
155 static inline pgprot_t static_protections(pgprot_t prot, unsigned long address,
156                                    unsigned long pfn)
157 {
158         pgprot_t forbidden = __pgprot(0);
159
160         /*
161          * The BIOS area between 640k and 1Mb needs to be executable for
162          * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
163          */
164         if (within(pfn, BIOS_BEGIN >> PAGE_SHIFT, BIOS_END >> PAGE_SHIFT))
165                 pgprot_val(forbidden) |= _PAGE_NX;
166
167         /*
168          * The kernel text needs to be executable for obvious reasons
169          * Does not cover __inittext since that is gone later on. On
170          * 64bit we do not enforce !NX on the low mapping
171          */
172         if (within(address, (unsigned long)_text, (unsigned long)_etext))
173                 pgprot_val(forbidden) |= _PAGE_NX;
174
175         /*
176          * The .rodata section needs to be read-only. Using the pfn
177          * catches all aliases.
178          */
179         if (within(pfn, __pa((unsigned long)__start_rodata) >> PAGE_SHIFT,
180                    __pa((unsigned long)__end_rodata) >> PAGE_SHIFT))
181                 pgprot_val(forbidden) |= _PAGE_RW;
182
183         prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
184
185         return prot;
186 }
187
188 /*
189  * Lookup the page table entry for a virtual address. Return a pointer
190  * to the entry and the level of the mapping.
191  *
192  * Note: We return pud and pmd either when the entry is marked large
193  * or when the present bit is not set. Otherwise we would return a
194  * pointer to a nonexisting mapping.
195  */
196 pte_t *lookup_address(unsigned long address, unsigned int *level)
197 {
198         pgd_t *pgd = pgd_offset_k(address);
199         pud_t *pud;
200         pmd_t *pmd;
201
202         *level = PG_LEVEL_NONE;
203
204         if (pgd_none(*pgd))
205                 return NULL;
206
207         pud = pud_offset(pgd, address);
208         if (pud_none(*pud))
209                 return NULL;
210
211         *level = PG_LEVEL_1G;
212         if (pud_large(*pud) || !pud_present(*pud))
213                 return (pte_t *)pud;
214
215         pmd = pmd_offset(pud, address);
216         if (pmd_none(*pmd))
217                 return NULL;
218
219         *level = PG_LEVEL_2M;
220         if (pmd_large(*pmd) || !pmd_present(*pmd))
221                 return (pte_t *)pmd;
222
223         *level = PG_LEVEL_4K;
224
225         return pte_offset_kernel(pmd, address);
226 }
227
228 /*
229  * Set the new pmd in all the pgds we know about:
230  */
231 static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
232 {
233         /* change init_mm */
234         set_pte_atomic(kpte, pte);
235 #ifdef CONFIG_X86_32
236         if (!SHARED_KERNEL_PMD) {
237                 struct page *page;
238
239                 list_for_each_entry(page, &pgd_list, lru) {
240                         pgd_t *pgd;
241                         pud_t *pud;
242                         pmd_t *pmd;
243
244                         pgd = (pgd_t *)page_address(page) + pgd_index(address);
245                         pud = pud_offset(pgd, address);
246                         pmd = pmd_offset(pud, address);
247                         set_pte_atomic((pte_t *)pmd, pte);
248                 }
249         }
250 #endif
251 }
252
253 static int
254 try_preserve_large_page(pte_t *kpte, unsigned long address,
255                         struct cpa_data *cpa)
256 {
257         unsigned long nextpage_addr, numpages, pmask, psize, flags, addr, pfn;
258         pte_t new_pte, old_pte, *tmp;
259         pgprot_t old_prot, new_prot;
260         int i, do_split = 1;
261         unsigned int level;
262
263         spin_lock_irqsave(&pgd_lock, flags);
264         /*
265          * Check for races, another CPU might have split this page
266          * up already:
267          */
268         tmp = lookup_address(address, &level);
269         if (tmp != kpte)
270                 goto out_unlock;
271
272         switch (level) {
273         case PG_LEVEL_2M:
274                 psize = PMD_PAGE_SIZE;
275                 pmask = PMD_PAGE_MASK;
276                 break;
277 #ifdef CONFIG_X86_64
278         case PG_LEVEL_1G:
279                 psize = PUD_PAGE_SIZE;
280                 pmask = PUD_PAGE_MASK;
281                 break;
282 #endif
283         default:
284                 do_split = -EINVAL;
285                 goto out_unlock;
286         }
287
288         /*
289          * Calculate the number of pages, which fit into this large
290          * page starting at address:
291          */
292         nextpage_addr = (address + psize) & pmask;
293         numpages = (nextpage_addr - address) >> PAGE_SHIFT;
294         if (numpages < cpa->processed)
295                 cpa->processed = numpages;
296
297         /*
298          * We are safe now. Check whether the new pgprot is the same:
299          */
300         old_pte = *kpte;
301         old_prot = new_prot = pte_pgprot(old_pte);
302
303         pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
304         pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
305
306         /*
307          * old_pte points to the large page base address. So we need
308          * to add the offset of the virtual address:
309          */
310         pfn = pte_pfn(old_pte) + ((address & (psize - 1)) >> PAGE_SHIFT);
311         cpa->pfn = pfn;
312
313         new_prot = static_protections(new_prot, address, pfn);
314
315         /*
316          * We need to check the full range, whether
317          * static_protection() requires a different pgprot for one of
318          * the pages in the range we try to preserve:
319          */
320         addr = address + PAGE_SIZE;
321         pfn++;
322         for (i = 1; i < cpa->processed; i++, addr += PAGE_SIZE, pfn++) {
323                 pgprot_t chk_prot = static_protections(new_prot, addr, pfn);
324
325                 if (pgprot_val(chk_prot) != pgprot_val(new_prot))
326                         goto out_unlock;
327         }
328
329         /*
330          * If there are no changes, return. maxpages has been updated
331          * above:
332          */
333         if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
334                 do_split = 0;
335                 goto out_unlock;
336         }
337
338         /*
339          * We need to change the attributes. Check, whether we can
340          * change the large page in one go. We request a split, when
341          * the address is not aligned and the number of pages is
342          * smaller than the number of pages in the large page. Note
343          * that we limited the number of possible pages already to
344          * the number of pages in the large page.
345          */
346         if (address == (nextpage_addr - psize) && cpa->processed == numpages) {
347                 /*
348                  * The address is aligned and the number of pages
349                  * covers the full page.
350                  */
351                 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
352                 __set_pmd_pte(kpte, address, new_pte);
353                 cpa->flushtlb = 1;
354                 do_split = 0;
355         }
356
357 out_unlock:
358         spin_unlock_irqrestore(&pgd_lock, flags);
359
360         return do_split;
361 }
362
363 static LIST_HEAD(page_pool);
364 static unsigned long pool_size, pool_pages, pool_low;
365 static unsigned long pool_used, pool_failed;
366
367 static void cpa_fill_pool(struct page **ret)
368 {
369         gfp_t gfp = GFP_KERNEL;
370         unsigned long flags;
371         struct page *p;
372
373         /*
374          * Avoid recursion (on debug-pagealloc) and also signal
375          * our priority to get to these pagetables:
376          */
377         if (current->flags & PF_MEMALLOC)
378                 return;
379         current->flags |= PF_MEMALLOC;
380
381         /*
382          * Allocate atomically from atomic contexts:
383          */
384         if (in_atomic() || irqs_disabled() || debug_pagealloc)
385                 gfp =  GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
386
387         while (pool_pages < pool_size || (ret && !*ret)) {
388                 p = alloc_pages(gfp, 0);
389                 if (!p) {
390                         pool_failed++;
391                         break;
392                 }
393                 /*
394                  * If the call site needs a page right now, provide it:
395                  */
396                 if (ret && !*ret) {
397                         *ret = p;
398                         continue;
399                 }
400                 spin_lock_irqsave(&pgd_lock, flags);
401                 list_add(&p->lru, &page_pool);
402                 pool_pages++;
403                 spin_unlock_irqrestore(&pgd_lock, flags);
404         }
405
406         current->flags &= ~PF_MEMALLOC;
407 }
408
409 #define SHIFT_MB                (20 - PAGE_SHIFT)
410 #define ROUND_MB_GB             ((1 << 10) - 1)
411 #define SHIFT_MB_GB             10
412 #define POOL_PAGES_PER_GB       16
413
414 void __init cpa_init(void)
415 {
416         struct sysinfo si;
417         unsigned long gb;
418
419         si_meminfo(&si);
420         /*
421          * Calculate the number of pool pages:
422          *
423          * Convert totalram (nr of pages) to MiB and round to the next
424          * GiB. Shift MiB to Gib and multiply the result by
425          * POOL_PAGES_PER_GB:
426          */
427         if (debug_pagealloc) {
428                 gb = ((si.totalram >> SHIFT_MB) + ROUND_MB_GB) >> SHIFT_MB_GB;
429                 pool_size = POOL_PAGES_PER_GB * gb;
430         } else {
431                 pool_size = 1;
432         }
433         pool_low = pool_size;
434
435         cpa_fill_pool(NULL);
436         printk(KERN_DEBUG
437                "CPA: page pool initialized %lu of %lu pages preallocated\n",
438                pool_pages, pool_size);
439 }
440
441 static int split_large_page(pte_t *kpte, unsigned long address)
442 {
443         unsigned long flags, pfn, pfninc = 1;
444         unsigned int i, level;
445         pte_t *pbase, *tmp;
446         pgprot_t ref_prot;
447         struct page *base;
448
449         /*
450          * Get a page from the pool. The pool list is protected by the
451          * pgd_lock, which we have to take anyway for the split
452          * operation:
453          */
454         spin_lock_irqsave(&pgd_lock, flags);
455         if (list_empty(&page_pool)) {
456                 spin_unlock_irqrestore(&pgd_lock, flags);
457                 base = NULL;
458                 cpa_fill_pool(&base);
459                 if (!base)
460                         return -ENOMEM;
461                 spin_lock_irqsave(&pgd_lock, flags);
462         } else {
463                 base = list_first_entry(&page_pool, struct page, lru);
464                 list_del(&base->lru);
465                 pool_pages--;
466
467                 if (pool_pages < pool_low)
468                         pool_low = pool_pages;
469         }
470
471         /*
472          * Check for races, another CPU might have split this page
473          * up for us already:
474          */
475         tmp = lookup_address(address, &level);
476         if (tmp != kpte)
477                 goto out_unlock;
478
479         pbase = (pte_t *)page_address(base);
480 #ifdef CONFIG_X86_32
481         paravirt_alloc_pt(&init_mm, page_to_pfn(base));
482 #endif
483         ref_prot = pte_pgprot(pte_clrhuge(*kpte));
484
485 #ifdef CONFIG_X86_64
486         if (level == PG_LEVEL_1G) {
487                 pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
488                 pgprot_val(ref_prot) |= _PAGE_PSE;
489         }
490 #endif
491
492         /*
493          * Get the target pfn from the original entry:
494          */
495         pfn = pte_pfn(*kpte);
496         for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
497                 set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
498
499         /*
500          * Install the new, split up pagetable. Important details here:
501          *
502          * On Intel the NX bit of all levels must be cleared to make a
503          * page executable. See section 4.13.2 of Intel 64 and IA-32
504          * Architectures Software Developer's Manual).
505          *
506          * Mark the entry present. The current mapping might be
507          * set to not present, which we preserved above.
508          */
509         ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
510         pgprot_val(ref_prot) |= _PAGE_PRESENT;
511         __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
512         base = NULL;
513
514 out_unlock:
515         /*
516          * If we dropped out via the lookup_address check under
517          * pgd_lock then stick the page back into the pool:
518          */
519         if (base) {
520                 list_add(&base->lru, &page_pool);
521                 pool_pages++;
522         } else
523                 pool_used++;
524         spin_unlock_irqrestore(&pgd_lock, flags);
525
526         return 0;
527 }
528
529 static int __change_page_attr(struct cpa_data *cpa, int primary)
530 {
531         unsigned long address = cpa->vaddr;
532         int do_split, err;
533         unsigned int level;
534         pte_t *kpte, old_pte;
535
536 repeat:
537         kpte = lookup_address(address, &level);
538         if (!kpte)
539                 return primary ? -EINVAL : 0;
540
541         old_pte = *kpte;
542         if (!pte_val(old_pte)) {
543                 if (!primary)
544                         return 0;
545                 printk(KERN_WARNING "CPA: called for zero pte. "
546                        "vaddr = %lx cpa->vaddr = %lx\n", address,
547                        cpa->vaddr);
548                 WARN_ON(1);
549                 return -EINVAL;
550         }
551
552         if (level == PG_LEVEL_4K) {
553                 pte_t new_pte;
554                 pgprot_t new_prot = pte_pgprot(old_pte);
555                 unsigned long pfn = pte_pfn(old_pte);
556
557                 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
558                 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
559
560                 new_prot = static_protections(new_prot, address, pfn);
561
562                 /*
563                  * We need to keep the pfn from the existing PTE,
564                  * after all we're only going to change it's attributes
565                  * not the memory it points to
566                  */
567                 new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
568                 cpa->pfn = pfn;
569                 /*
570                  * Do we really change anything ?
571                  */
572                 if (pte_val(old_pte) != pte_val(new_pte)) {
573                         set_pte_atomic(kpte, new_pte);
574                         cpa->flushtlb = 1;
575                 }
576                 cpa->processed = 1;
577                 return 0;
578         }
579
580         /*
581          * Check, whether we can keep the large page intact
582          * and just change the pte:
583          */
584         do_split = try_preserve_large_page(kpte, address, cpa);
585         /*
586          * When the range fits into the existing large page,
587          * return. cp->processed and cpa->tlbflush have been updated in
588          * try_large_page:
589          */
590         if (do_split <= 0)
591                 return do_split;
592
593         /*
594          * We have to split the large page:
595          */
596         err = split_large_page(kpte, address);
597         if (!err) {
598                 cpa->flushtlb = 1;
599                 goto repeat;
600         }
601
602         return err;
603 }
604
605 static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
606
607 static int cpa_process_alias(struct cpa_data *cpa)
608 {
609         struct cpa_data alias_cpa;
610         int ret = 0;
611
612         if (cpa->pfn > max_pfn_mapped)
613                 return 0;
614
615         /*
616          * No need to redo, when the primary call touched the direct
617          * mapping already:
618          */
619         if (!within(cpa->vaddr, PAGE_OFFSET,
620                     PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
621
622                 alias_cpa = *cpa;
623                 alias_cpa.vaddr = (unsigned long) __va(cpa->pfn << PAGE_SHIFT);
624
625                 ret = __change_page_attr_set_clr(&alias_cpa, 0);
626         }
627
628 #ifdef CONFIG_X86_64
629         if (ret)
630                 return ret;
631         /*
632          * No need to redo, when the primary call touched the high
633          * mapping already:
634          */
635         if (within(cpa->vaddr, (unsigned long) _text, (unsigned long) _end))
636                 return 0;
637
638         /*
639          * If the physical address is inside the kernel map, we need
640          * to touch the high mapped kernel as well:
641          */
642         if (!within(cpa->pfn, highmap_start_pfn(), highmap_end_pfn()))
643                 return 0;
644
645         alias_cpa = *cpa;
646         alias_cpa.vaddr =
647                 (cpa->pfn << PAGE_SHIFT) + __START_KERNEL_map - phys_base;
648
649         /*
650          * The high mapping range is imprecise, so ignore the return value.
651          */
652         __change_page_attr_set_clr(&alias_cpa, 0);
653 #endif
654         return ret;
655 }
656
657 static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
658 {
659         int ret, numpages = cpa->numpages;
660
661         while (numpages) {
662                 /*
663                  * Store the remaining nr of pages for the large page
664                  * preservation check.
665                  */
666                 cpa->numpages = cpa->processed = numpages;
667
668                 ret = __change_page_attr(cpa, checkalias);
669                 if (ret)
670                         return ret;
671
672                 if (checkalias) {
673                         ret = cpa_process_alias(cpa);
674                         if (ret)
675                                 return ret;
676                 }
677
678                 /*
679                  * Adjust the number of pages with the result of the
680                  * CPA operation. Either a large page has been
681                  * preserved or a single page update happened.
682                  */
683                 BUG_ON(cpa->processed > numpages);
684                 numpages -= cpa->processed;
685                 cpa->vaddr += cpa->processed * PAGE_SIZE;
686         }
687         return 0;
688 }
689
690 static inline int cache_attr(pgprot_t attr)
691 {
692         return pgprot_val(attr) &
693                 (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
694 }
695
696 static int change_page_attr_set_clr(unsigned long addr, int numpages,
697                                     pgprot_t mask_set, pgprot_t mask_clr)
698 {
699         struct cpa_data cpa;
700         int ret, cache, checkalias;
701
702         /*
703          * Check, if we are requested to change a not supported
704          * feature:
705          */
706         mask_set = canon_pgprot(mask_set);
707         mask_clr = canon_pgprot(mask_clr);
708         if (!pgprot_val(mask_set) && !pgprot_val(mask_clr))
709                 return 0;
710
711         /* Ensure we are PAGE_SIZE aligned */
712         if (addr & ~PAGE_MASK) {
713                 addr &= PAGE_MASK;
714                 /*
715                  * People should not be passing in unaligned addresses:
716                  */
717                 WARN_ON_ONCE(1);
718         }
719
720         cpa.vaddr = addr;
721         cpa.numpages = numpages;
722         cpa.mask_set = mask_set;
723         cpa.mask_clr = mask_clr;
724         cpa.flushtlb = 0;
725
726         /* No alias checking for _NX bit modifications */
727         checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
728
729         ret = __change_page_attr_set_clr(&cpa, checkalias);
730
731         /*
732          * Check whether we really changed something:
733          */
734         if (!cpa.flushtlb)
735                 goto out;
736
737         /*
738          * No need to flush, when we did not set any of the caching
739          * attributes:
740          */
741         cache = cache_attr(mask_set);
742
743         /*
744          * On success we use clflush, when the CPU supports it to
745          * avoid the wbindv. If the CPU does not support it and in the
746          * error case we fall back to cpa_flush_all (which uses
747          * wbindv):
748          */
749         if (!ret && cpu_has_clflush)
750                 cpa_flush_range(addr, numpages, cache);
751         else
752                 cpa_flush_all(cache);
753
754 out:
755         cpa_fill_pool(NULL);
756
757         return ret;
758 }
759
760 static inline int change_page_attr_set(unsigned long addr, int numpages,
761                                        pgprot_t mask)
762 {
763         return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
764 }
765
766 static inline int change_page_attr_clear(unsigned long addr, int numpages,
767                                          pgprot_t mask)
768 {
769         return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
770 }
771
772 int set_memory_uc(unsigned long addr, int numpages)
773 {
774         return change_page_attr_set(addr, numpages,
775                                     __pgprot(_PAGE_PCD | _PAGE_PWT));
776 }
777 EXPORT_SYMBOL(set_memory_uc);
778
779 int set_memory_wb(unsigned long addr, int numpages)
780 {
781         return change_page_attr_clear(addr, numpages,
782                                       __pgprot(_PAGE_PCD | _PAGE_PWT));
783 }
784 EXPORT_SYMBOL(set_memory_wb);
785
786 int set_memory_x(unsigned long addr, int numpages)
787 {
788         return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
789 }
790 EXPORT_SYMBOL(set_memory_x);
791
792 int set_memory_nx(unsigned long addr, int numpages)
793 {
794         return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
795 }
796 EXPORT_SYMBOL(set_memory_nx);
797
798 int set_memory_ro(unsigned long addr, int numpages)
799 {
800         return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
801 }
802
803 int set_memory_rw(unsigned long addr, int numpages)
804 {
805         return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
806 }
807
808 int set_memory_np(unsigned long addr, int numpages)
809 {
810         return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
811 }
812
813 int set_pages_uc(struct page *page, int numpages)
814 {
815         unsigned long addr = (unsigned long)page_address(page);
816
817         return set_memory_uc(addr, numpages);
818 }
819 EXPORT_SYMBOL(set_pages_uc);
820
821 int set_pages_wb(struct page *page, int numpages)
822 {
823         unsigned long addr = (unsigned long)page_address(page);
824
825         return set_memory_wb(addr, numpages);
826 }
827 EXPORT_SYMBOL(set_pages_wb);
828
829 int set_pages_x(struct page *page, int numpages)
830 {
831         unsigned long addr = (unsigned long)page_address(page);
832
833         return set_memory_x(addr, numpages);
834 }
835 EXPORT_SYMBOL(set_pages_x);
836
837 int set_pages_nx(struct page *page, int numpages)
838 {
839         unsigned long addr = (unsigned long)page_address(page);
840
841         return set_memory_nx(addr, numpages);
842 }
843 EXPORT_SYMBOL(set_pages_nx);
844
845 int set_pages_ro(struct page *page, int numpages)
846 {
847         unsigned long addr = (unsigned long)page_address(page);
848
849         return set_memory_ro(addr, numpages);
850 }
851
852 int set_pages_rw(struct page *page, int numpages)
853 {
854         unsigned long addr = (unsigned long)page_address(page);
855
856         return set_memory_rw(addr, numpages);
857 }
858
859 #ifdef CONFIG_DEBUG_PAGEALLOC
860
861 static int __set_pages_p(struct page *page, int numpages)
862 {
863         struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
864                                 .numpages = numpages,
865                                 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
866                                 .mask_clr = __pgprot(0)};
867
868         return __change_page_attr_set_clr(&cpa, 1);
869 }
870
871 static int __set_pages_np(struct page *page, int numpages)
872 {
873         struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
874                                 .numpages = numpages,
875                                 .mask_set = __pgprot(0),
876                                 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW)};
877
878         return __change_page_attr_set_clr(&cpa, 1);
879 }
880
881 void kernel_map_pages(struct page *page, int numpages, int enable)
882 {
883         if (PageHighMem(page))
884                 return;
885         if (!enable) {
886                 debug_check_no_locks_freed(page_address(page),
887                                            numpages * PAGE_SIZE);
888         }
889
890         /*
891          * If page allocator is not up yet then do not call c_p_a():
892          */
893         if (!debug_pagealloc_enabled)
894                 return;
895
896         /*
897          * The return value is ignored as the calls cannot fail.
898          * Large pages are kept enabled at boot time, and are
899          * split up quickly with DEBUG_PAGEALLOC. If a splitup
900          * fails here (due to temporary memory shortage) no damage
901          * is done because we just keep the largepage intact up
902          * to the next attempt when it will likely be split up:
903          */
904         if (enable)
905                 __set_pages_p(page, numpages);
906         else
907                 __set_pages_np(page, numpages);
908
909         /*
910          * We should perform an IPI and flush all tlbs,
911          * but that can deadlock->flush only current cpu:
912          */
913         __flush_tlb_all();
914
915         /*
916          * Try to refill the page pool here. We can do this only after
917          * the tlb flush.
918          */
919         cpa_fill_pool(NULL);
920 }
921
922 #ifdef CONFIG_HIBERNATION
923
924 bool kernel_page_present(struct page *page)
925 {
926         unsigned int level;
927         pte_t *pte;
928
929         if (PageHighMem(page))
930                 return false;
931
932         pte = lookup_address((unsigned long)page_address(page), &level);
933         return (pte_val(*pte) & _PAGE_PRESENT);
934 }
935
936 #endif /* CONFIG_HIBERNATION */
937
938 #endif /* CONFIG_DEBUG_PAGEALLOC */
939
940 /*
941  * The testcases use internal knowledge of the implementation that shouldn't
942  * be exposed to the rest of the kernel. Include these directly here.
943  */
944 #ifdef CONFIG_CPA_DEBUG
945 #include "pageattr-test.c"
946 #endif