hugetlb: introduce generic version of hugetlb_free_pgd_range
[linux/fpc-iii.git] / arch / x86 / mm / pageattr.c
blob62bb30b4bd2ab67de5607d27fd2d816f2c947c8a
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/sched.h>
8 #include <linux/mm.h>
9 #include <linux/interrupt.h>
10 #include <linux/seq_file.h>
11 #include <linux/debugfs.h>
12 #include <linux/pfn.h>
13 #include <linux/percpu.h>
14 #include <linux/gfp.h>
15 #include <linux/pci.h>
16 #include <linux/vmalloc.h>
18 #include <asm/e820/api.h>
19 #include <asm/processor.h>
20 #include <asm/tlbflush.h>
21 #include <asm/sections.h>
22 #include <asm/setup.h>
23 #include <linux/uaccess.h>
24 #include <asm/pgalloc.h>
25 #include <asm/proto.h>
26 #include <asm/pat.h>
27 #include <asm/set_memory.h>
30 * The current flushing context - we pass it instead of 5 arguments:
32 struct cpa_data {
33 unsigned long *vaddr;
34 pgd_t *pgd;
35 pgprot_t mask_set;
36 pgprot_t mask_clr;
37 unsigned long numpages;
38 int flags;
39 unsigned long pfn;
40 unsigned force_split : 1,
41 force_static_prot : 1;
42 int curpage;
43 struct page **pages;
46 enum cpa_warn {
47 CPA_CONFLICT,
48 CPA_PROTECT,
49 CPA_DETECT,
52 static const int cpa_warn_level = CPA_PROTECT;
55 * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings)
56 * using cpa_lock. So that we don't allow any other cpu, with stale large tlb
57 * entries change the page attribute in parallel to some other cpu
58 * splitting a large page entry along with changing the attribute.
60 static DEFINE_SPINLOCK(cpa_lock);
62 #define CPA_FLUSHTLB 1
63 #define CPA_ARRAY 2
64 #define CPA_PAGES_ARRAY 4
65 #define CPA_NO_CHECK_ALIAS 8 /* Do not search for aliases */
67 #ifdef CONFIG_PROC_FS
68 static unsigned long direct_pages_count[PG_LEVEL_NUM];
70 void update_page_count(int level, unsigned long pages)
72 /* Protect against CPA */
73 spin_lock(&pgd_lock);
74 direct_pages_count[level] += pages;
75 spin_unlock(&pgd_lock);
78 static void split_page_count(int level)
80 if (direct_pages_count[level] == 0)
81 return;
83 direct_pages_count[level]--;
84 direct_pages_count[level - 1] += PTRS_PER_PTE;
87 void arch_report_meminfo(struct seq_file *m)
89 seq_printf(m, "DirectMap4k: %8lu kB\n",
90 direct_pages_count[PG_LEVEL_4K] << 2);
91 #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
92 seq_printf(m, "DirectMap2M: %8lu kB\n",
93 direct_pages_count[PG_LEVEL_2M] << 11);
94 #else
95 seq_printf(m, "DirectMap4M: %8lu kB\n",
96 direct_pages_count[PG_LEVEL_2M] << 12);
97 #endif
98 if (direct_gbpages)
99 seq_printf(m, "DirectMap1G: %8lu kB\n",
100 direct_pages_count[PG_LEVEL_1G] << 20);
102 #else
103 static inline void split_page_count(int level) { }
104 #endif
106 #ifdef CONFIG_X86_CPA_STATISTICS
108 static unsigned long cpa_1g_checked;
109 static unsigned long cpa_1g_sameprot;
110 static unsigned long cpa_1g_preserved;
111 static unsigned long cpa_2m_checked;
112 static unsigned long cpa_2m_sameprot;
113 static unsigned long cpa_2m_preserved;
114 static unsigned long cpa_4k_install;
116 static inline void cpa_inc_1g_checked(void)
118 cpa_1g_checked++;
121 static inline void cpa_inc_2m_checked(void)
123 cpa_2m_checked++;
126 static inline void cpa_inc_4k_install(void)
128 cpa_4k_install++;
131 static inline void cpa_inc_lp_sameprot(int level)
133 if (level == PG_LEVEL_1G)
134 cpa_1g_sameprot++;
135 else
136 cpa_2m_sameprot++;
139 static inline void cpa_inc_lp_preserved(int level)
141 if (level == PG_LEVEL_1G)
142 cpa_1g_preserved++;
143 else
144 cpa_2m_preserved++;
147 static int cpastats_show(struct seq_file *m, void *p)
149 seq_printf(m, "1G pages checked: %16lu\n", cpa_1g_checked);
150 seq_printf(m, "1G pages sameprot: %16lu\n", cpa_1g_sameprot);
151 seq_printf(m, "1G pages preserved: %16lu\n", cpa_1g_preserved);
152 seq_printf(m, "2M pages checked: %16lu\n", cpa_2m_checked);
153 seq_printf(m, "2M pages sameprot: %16lu\n", cpa_2m_sameprot);
154 seq_printf(m, "2M pages preserved: %16lu\n", cpa_2m_preserved);
155 seq_printf(m, "4K pages set-checked: %16lu\n", cpa_4k_install);
156 return 0;
159 static int cpastats_open(struct inode *inode, struct file *file)
161 return single_open(file, cpastats_show, NULL);
164 static const struct file_operations cpastats_fops = {
165 .open = cpastats_open,
166 .read = seq_read,
167 .llseek = seq_lseek,
168 .release = single_release,
171 static int __init cpa_stats_init(void)
173 debugfs_create_file("cpa_stats", S_IRUSR, arch_debugfs_dir, NULL,
174 &cpastats_fops);
175 return 0;
177 late_initcall(cpa_stats_init);
178 #else
179 static inline void cpa_inc_1g_checked(void) { }
180 static inline void cpa_inc_2m_checked(void) { }
181 static inline void cpa_inc_4k_install(void) { }
182 static inline void cpa_inc_lp_sameprot(int level) { }
183 static inline void cpa_inc_lp_preserved(int level) { }
184 #endif
187 static inline int
188 within(unsigned long addr, unsigned long start, unsigned long end)
190 return addr >= start && addr < end;
193 static inline int
194 within_inclusive(unsigned long addr, unsigned long start, unsigned long end)
196 return addr >= start && addr <= end;
199 #ifdef CONFIG_X86_64
201 static inline unsigned long highmap_start_pfn(void)
203 return __pa_symbol(_text) >> PAGE_SHIFT;
206 static inline unsigned long highmap_end_pfn(void)
208 /* Do not reference physical address outside the kernel. */
209 return __pa_symbol(roundup(_brk_end, PMD_SIZE) - 1) >> PAGE_SHIFT;
212 static bool __cpa_pfn_in_highmap(unsigned long pfn)
215 * Kernel text has an alias mapping at a high address, known
216 * here as "highmap".
218 return within_inclusive(pfn, highmap_start_pfn(), highmap_end_pfn());
221 #else
223 static bool __cpa_pfn_in_highmap(unsigned long pfn)
225 /* There is no highmap on 32-bit */
226 return false;
229 #endif
232 * Flushing functions
236 * clflush_cache_range - flush a cache range with clflush
237 * @vaddr: virtual start address
238 * @size: number of bytes to flush
240 * clflushopt is an unordered instruction which needs fencing with mfence or
241 * sfence to avoid ordering issues.
243 void clflush_cache_range(void *vaddr, unsigned int size)
245 const unsigned long clflush_size = boot_cpu_data.x86_clflush_size;
246 void *p = (void *)((unsigned long)vaddr & ~(clflush_size - 1));
247 void *vend = vaddr + size;
249 if (p >= vend)
250 return;
252 mb();
254 for (; p < vend; p += clflush_size)
255 clflushopt(p);
257 mb();
259 EXPORT_SYMBOL_GPL(clflush_cache_range);
261 void arch_invalidate_pmem(void *addr, size_t size)
263 clflush_cache_range(addr, size);
265 EXPORT_SYMBOL_GPL(arch_invalidate_pmem);
267 static void __cpa_flush_all(void *arg)
269 unsigned long cache = (unsigned long)arg;
272 * Flush all to work around Errata in early athlons regarding
273 * large page flushing.
275 __flush_tlb_all();
277 if (cache && boot_cpu_data.x86 >= 4)
278 wbinvd();
281 static void cpa_flush_all(unsigned long cache)
283 BUG_ON(irqs_disabled() && !early_boot_irqs_disabled);
285 on_each_cpu(__cpa_flush_all, (void *) cache, 1);
288 static bool __cpa_flush_range(unsigned long start, int numpages, int cache)
290 BUG_ON(irqs_disabled() && !early_boot_irqs_disabled);
292 WARN_ON(PAGE_ALIGN(start) != start);
294 if (cache && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
295 cpa_flush_all(cache);
296 return true;
299 flush_tlb_kernel_range(start, start + PAGE_SIZE * numpages);
301 return !cache;
304 static void cpa_flush_range(unsigned long start, int numpages, int cache)
306 unsigned int i, level;
307 unsigned long addr;
309 if (__cpa_flush_range(start, numpages, cache))
310 return;
313 * We only need to flush on one CPU,
314 * clflush is a MESI-coherent instruction that
315 * will cause all other CPUs to flush the same
316 * cachelines:
318 for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
319 pte_t *pte = lookup_address(addr, &level);
322 * Only flush present addresses:
324 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
325 clflush_cache_range((void *) addr, PAGE_SIZE);
329 static void cpa_flush_array(unsigned long baddr, unsigned long *start,
330 int numpages, int cache,
331 int in_flags, struct page **pages)
333 unsigned int i, level;
335 if (__cpa_flush_range(baddr, numpages, cache))
336 return;
339 * We only need to flush on one CPU,
340 * clflush is a MESI-coherent instruction that
341 * will cause all other CPUs to flush the same
342 * cachelines:
344 for (i = 0; i < numpages; i++) {
345 unsigned long addr;
346 pte_t *pte;
348 if (in_flags & CPA_PAGES_ARRAY)
349 addr = (unsigned long)page_address(pages[i]);
350 else
351 addr = start[i];
353 pte = lookup_address(addr, &level);
356 * Only flush present addresses:
358 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
359 clflush_cache_range((void *)addr, PAGE_SIZE);
363 static bool overlaps(unsigned long r1_start, unsigned long r1_end,
364 unsigned long r2_start, unsigned long r2_end)
366 return (r1_start <= r2_end && r1_end >= r2_start) ||
367 (r2_start <= r1_end && r2_end >= r1_start);
370 #ifdef CONFIG_PCI_BIOS
372 * The BIOS area between 640k and 1Mb needs to be executable for PCI BIOS
373 * based config access (CONFIG_PCI_GOBIOS) support.
375 #define BIOS_PFN PFN_DOWN(BIOS_BEGIN)
376 #define BIOS_PFN_END PFN_DOWN(BIOS_END - 1)
378 static pgprotval_t protect_pci_bios(unsigned long spfn, unsigned long epfn)
380 if (pcibios_enabled && overlaps(spfn, epfn, BIOS_PFN, BIOS_PFN_END))
381 return _PAGE_NX;
382 return 0;
384 #else
385 static pgprotval_t protect_pci_bios(unsigned long spfn, unsigned long epfn)
387 return 0;
389 #endif
392 * The .rodata section needs to be read-only. Using the pfn catches all
393 * aliases. This also includes __ro_after_init, so do not enforce until
394 * kernel_set_to_readonly is true.
396 static pgprotval_t protect_rodata(unsigned long spfn, unsigned long epfn)
398 unsigned long epfn_ro, spfn_ro = PFN_DOWN(__pa_symbol(__start_rodata));
401 * Note: __end_rodata is at page aligned and not inclusive, so
402 * subtract 1 to get the last enforced PFN in the rodata area.
404 epfn_ro = PFN_DOWN(__pa_symbol(__end_rodata)) - 1;
406 if (kernel_set_to_readonly && overlaps(spfn, epfn, spfn_ro, epfn_ro))
407 return _PAGE_RW;
408 return 0;
412 * Protect kernel text against becoming non executable by forbidding
413 * _PAGE_NX. This protects only the high kernel mapping (_text -> _etext)
414 * out of which the kernel actually executes. Do not protect the low
415 * mapping.
417 * This does not cover __inittext since that is gone after boot.
419 static pgprotval_t protect_kernel_text(unsigned long start, unsigned long end)
421 unsigned long t_end = (unsigned long)_etext - 1;
422 unsigned long t_start = (unsigned long)_text;
424 if (overlaps(start, end, t_start, t_end))
425 return _PAGE_NX;
426 return 0;
429 #if defined(CONFIG_X86_64)
431 * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
432 * kernel text mappings for the large page aligned text, rodata sections
433 * will be always read-only. For the kernel identity mappings covering the
434 * holes caused by this alignment can be anything that user asks.
436 * This will preserve the large page mappings for kernel text/data at no
437 * extra cost.
439 static pgprotval_t protect_kernel_text_ro(unsigned long start,
440 unsigned long end)
442 unsigned long t_end = (unsigned long)__end_rodata_hpage_align - 1;
443 unsigned long t_start = (unsigned long)_text;
444 unsigned int level;
446 if (!kernel_set_to_readonly || !overlaps(start, end, t_start, t_end))
447 return 0;
449 * Don't enforce the !RW mapping for the kernel text mapping, if
450 * the current mapping is already using small page mapping. No
451 * need to work hard to preserve large page mappings in this case.
453 * This also fixes the Linux Xen paravirt guest boot failure caused
454 * by unexpected read-only mappings for kernel identity
455 * mappings. In this paravirt guest case, the kernel text mapping
456 * and the kernel identity mapping share the same page-table pages,
457 * so the protections for kernel text and identity mappings have to
458 * be the same.
460 if (lookup_address(start, &level) && (level != PG_LEVEL_4K))
461 return _PAGE_RW;
462 return 0;
464 #else
465 static pgprotval_t protect_kernel_text_ro(unsigned long start,
466 unsigned long end)
468 return 0;
470 #endif
472 static inline bool conflicts(pgprot_t prot, pgprotval_t val)
474 return (pgprot_val(prot) & ~val) != pgprot_val(prot);
477 static inline void check_conflict(int warnlvl, pgprot_t prot, pgprotval_t val,
478 unsigned long start, unsigned long end,
479 unsigned long pfn, const char *txt)
481 static const char *lvltxt[] = {
482 [CPA_CONFLICT] = "conflict",
483 [CPA_PROTECT] = "protect",
484 [CPA_DETECT] = "detect",
487 if (warnlvl > cpa_warn_level || !conflicts(prot, val))
488 return;
490 pr_warn("CPA %8s %10s: 0x%016lx - 0x%016lx PFN %lx req %016llx prevent %016llx\n",
491 lvltxt[warnlvl], txt, start, end, pfn, (unsigned long long)pgprot_val(prot),
492 (unsigned long long)val);
496 * Certain areas of memory on x86 require very specific protection flags,
497 * for example the BIOS area or kernel text. Callers don't always get this
498 * right (again, ioremap() on BIOS memory is not uncommon) so this function
499 * checks and fixes these known static required protection bits.
501 static inline pgprot_t static_protections(pgprot_t prot, unsigned long start,
502 unsigned long pfn, unsigned long npg,
503 int warnlvl)
505 pgprotval_t forbidden, res;
506 unsigned long end;
509 * There is no point in checking RW/NX conflicts when the requested
510 * mapping is setting the page !PRESENT.
512 if (!(pgprot_val(prot) & _PAGE_PRESENT))
513 return prot;
515 /* Operate on the virtual address */
516 end = start + npg * PAGE_SIZE - 1;
518 res = protect_kernel_text(start, end);
519 check_conflict(warnlvl, prot, res, start, end, pfn, "Text NX");
520 forbidden = res;
522 res = protect_kernel_text_ro(start, end);
523 check_conflict(warnlvl, prot, res, start, end, pfn, "Text RO");
524 forbidden |= res;
526 /* Check the PFN directly */
527 res = protect_pci_bios(pfn, pfn + npg - 1);
528 check_conflict(warnlvl, prot, res, start, end, pfn, "PCIBIOS NX");
529 forbidden |= res;
531 res = protect_rodata(pfn, pfn + npg - 1);
532 check_conflict(warnlvl, prot, res, start, end, pfn, "Rodata RO");
533 forbidden |= res;
535 return __pgprot(pgprot_val(prot) & ~forbidden);
539 * Lookup the page table entry for a virtual address in a specific pgd.
540 * Return a pointer to the entry and the level of the mapping.
542 pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address,
543 unsigned int *level)
545 p4d_t *p4d;
546 pud_t *pud;
547 pmd_t *pmd;
549 *level = PG_LEVEL_NONE;
551 if (pgd_none(*pgd))
552 return NULL;
554 p4d = p4d_offset(pgd, address);
555 if (p4d_none(*p4d))
556 return NULL;
558 *level = PG_LEVEL_512G;
559 if (p4d_large(*p4d) || !p4d_present(*p4d))
560 return (pte_t *)p4d;
562 pud = pud_offset(p4d, address);
563 if (pud_none(*pud))
564 return NULL;
566 *level = PG_LEVEL_1G;
567 if (pud_large(*pud) || !pud_present(*pud))
568 return (pte_t *)pud;
570 pmd = pmd_offset(pud, address);
571 if (pmd_none(*pmd))
572 return NULL;
574 *level = PG_LEVEL_2M;
575 if (pmd_large(*pmd) || !pmd_present(*pmd))
576 return (pte_t *)pmd;
578 *level = PG_LEVEL_4K;
580 return pte_offset_kernel(pmd, address);
584 * Lookup the page table entry for a virtual address. Return a pointer
585 * to the entry and the level of the mapping.
587 * Note: We return pud and pmd either when the entry is marked large
588 * or when the present bit is not set. Otherwise we would return a
589 * pointer to a nonexisting mapping.
591 pte_t *lookup_address(unsigned long address, unsigned int *level)
593 return lookup_address_in_pgd(pgd_offset_k(address), address, level);
595 EXPORT_SYMBOL_GPL(lookup_address);
597 static pte_t *_lookup_address_cpa(struct cpa_data *cpa, unsigned long address,
598 unsigned int *level)
600 if (cpa->pgd)
601 return lookup_address_in_pgd(cpa->pgd + pgd_index(address),
602 address, level);
604 return lookup_address(address, level);
608 * Lookup the PMD entry for a virtual address. Return a pointer to the entry
609 * or NULL if not present.
611 pmd_t *lookup_pmd_address(unsigned long address)
613 pgd_t *pgd;
614 p4d_t *p4d;
615 pud_t *pud;
617 pgd = pgd_offset_k(address);
618 if (pgd_none(*pgd))
619 return NULL;
621 p4d = p4d_offset(pgd, address);
622 if (p4d_none(*p4d) || p4d_large(*p4d) || !p4d_present(*p4d))
623 return NULL;
625 pud = pud_offset(p4d, address);
626 if (pud_none(*pud) || pud_large(*pud) || !pud_present(*pud))
627 return NULL;
629 return pmd_offset(pud, address);
633 * This is necessary because __pa() does not work on some
634 * kinds of memory, like vmalloc() or the alloc_remap()
635 * areas on 32-bit NUMA systems. The percpu areas can
636 * end up in this kind of memory, for instance.
638 * This could be optimized, but it is only intended to be
639 * used at inititalization time, and keeping it
640 * unoptimized should increase the testing coverage for
641 * the more obscure platforms.
643 phys_addr_t slow_virt_to_phys(void *__virt_addr)
645 unsigned long virt_addr = (unsigned long)__virt_addr;
646 phys_addr_t phys_addr;
647 unsigned long offset;
648 enum pg_level level;
649 pte_t *pte;
651 pte = lookup_address(virt_addr, &level);
652 BUG_ON(!pte);
655 * pXX_pfn() returns unsigned long, which must be cast to phys_addr_t
656 * before being left-shifted PAGE_SHIFT bits -- this trick is to
657 * make 32-PAE kernel work correctly.
659 switch (level) {
660 case PG_LEVEL_1G:
661 phys_addr = (phys_addr_t)pud_pfn(*(pud_t *)pte) << PAGE_SHIFT;
662 offset = virt_addr & ~PUD_PAGE_MASK;
663 break;
664 case PG_LEVEL_2M:
665 phys_addr = (phys_addr_t)pmd_pfn(*(pmd_t *)pte) << PAGE_SHIFT;
666 offset = virt_addr & ~PMD_PAGE_MASK;
667 break;
668 default:
669 phys_addr = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
670 offset = virt_addr & ~PAGE_MASK;
673 return (phys_addr_t)(phys_addr | offset);
675 EXPORT_SYMBOL_GPL(slow_virt_to_phys);
678 * Set the new pmd in all the pgds we know about:
680 static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
682 /* change init_mm */
683 set_pte_atomic(kpte, pte);
684 #ifdef CONFIG_X86_32
685 if (!SHARED_KERNEL_PMD) {
686 struct page *page;
688 list_for_each_entry(page, &pgd_list, lru) {
689 pgd_t *pgd;
690 p4d_t *p4d;
691 pud_t *pud;
692 pmd_t *pmd;
694 pgd = (pgd_t *)page_address(page) + pgd_index(address);
695 p4d = p4d_offset(pgd, address);
696 pud = pud_offset(p4d, address);
697 pmd = pmd_offset(pud, address);
698 set_pte_atomic((pte_t *)pmd, pte);
701 #endif
704 static pgprot_t pgprot_clear_protnone_bits(pgprot_t prot)
707 * _PAGE_GLOBAL means "global page" for present PTEs.
708 * But, it is also used to indicate _PAGE_PROTNONE
709 * for non-present PTEs.
711 * This ensures that a _PAGE_GLOBAL PTE going from
712 * present to non-present is not confused as
713 * _PAGE_PROTNONE.
715 if (!(pgprot_val(prot) & _PAGE_PRESENT))
716 pgprot_val(prot) &= ~_PAGE_GLOBAL;
718 return prot;
721 static int __should_split_large_page(pte_t *kpte, unsigned long address,
722 struct cpa_data *cpa)
724 unsigned long numpages, pmask, psize, lpaddr, pfn, old_pfn;
725 pgprot_t old_prot, new_prot, req_prot, chk_prot;
726 pte_t new_pte, old_pte, *tmp;
727 enum pg_level level;
730 * Check for races, another CPU might have split this page
731 * up already:
733 tmp = _lookup_address_cpa(cpa, address, &level);
734 if (tmp != kpte)
735 return 1;
737 switch (level) {
738 case PG_LEVEL_2M:
739 old_prot = pmd_pgprot(*(pmd_t *)kpte);
740 old_pfn = pmd_pfn(*(pmd_t *)kpte);
741 cpa_inc_2m_checked();
742 break;
743 case PG_LEVEL_1G:
744 old_prot = pud_pgprot(*(pud_t *)kpte);
745 old_pfn = pud_pfn(*(pud_t *)kpte);
746 cpa_inc_1g_checked();
747 break;
748 default:
749 return -EINVAL;
752 psize = page_level_size(level);
753 pmask = page_level_mask(level);
756 * Calculate the number of pages, which fit into this large
757 * page starting at address:
759 lpaddr = (address + psize) & pmask;
760 numpages = (lpaddr - address) >> PAGE_SHIFT;
761 if (numpages < cpa->numpages)
762 cpa->numpages = numpages;
765 * We are safe now. Check whether the new pgprot is the same:
766 * Convert protection attributes to 4k-format, as cpa->mask* are set
767 * up accordingly.
769 old_pte = *kpte;
770 /* Clear PSE (aka _PAGE_PAT) and move PAT bit to correct position */
771 req_prot = pgprot_large_2_4k(old_prot);
773 pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr);
774 pgprot_val(req_prot) |= pgprot_val(cpa->mask_set);
777 * req_prot is in format of 4k pages. It must be converted to large
778 * page format: the caching mode includes the PAT bit located at
779 * different bit positions in the two formats.
781 req_prot = pgprot_4k_2_large(req_prot);
782 req_prot = pgprot_clear_protnone_bits(req_prot);
783 if (pgprot_val(req_prot) & _PAGE_PRESENT)
784 pgprot_val(req_prot) |= _PAGE_PSE;
787 * old_pfn points to the large page base pfn. So we need to add the
788 * offset of the virtual address:
790 pfn = old_pfn + ((address & (psize - 1)) >> PAGE_SHIFT);
791 cpa->pfn = pfn;
794 * Calculate the large page base address and the number of 4K pages
795 * in the large page
797 lpaddr = address & pmask;
798 numpages = psize >> PAGE_SHIFT;
801 * Sanity check that the existing mapping is correct versus the static
802 * protections. static_protections() guards against !PRESENT, so no
803 * extra conditional required here.
805 chk_prot = static_protections(old_prot, lpaddr, old_pfn, numpages,
806 CPA_CONFLICT);
808 if (WARN_ON_ONCE(pgprot_val(chk_prot) != pgprot_val(old_prot))) {
810 * Split the large page and tell the split code to
811 * enforce static protections.
813 cpa->force_static_prot = 1;
814 return 1;
818 * Optimization: If the requested pgprot is the same as the current
819 * pgprot, then the large page can be preserved and no updates are
820 * required independent of alignment and length of the requested
821 * range. The above already established that the current pgprot is
822 * correct, which in consequence makes the requested pgprot correct
823 * as well if it is the same. The static protection scan below will
824 * not come to a different conclusion.
826 if (pgprot_val(req_prot) == pgprot_val(old_prot)) {
827 cpa_inc_lp_sameprot(level);
828 return 0;
832 * If the requested range does not cover the full page, split it up
834 if (address != lpaddr || cpa->numpages != numpages)
835 return 1;
838 * Check whether the requested pgprot is conflicting with a static
839 * protection requirement in the large page.
841 new_prot = static_protections(req_prot, lpaddr, old_pfn, numpages,
842 CPA_DETECT);
845 * If there is a conflict, split the large page.
847 * There used to be a 4k wise evaluation trying really hard to
848 * preserve the large pages, but experimentation has shown, that this
849 * does not help at all. There might be corner cases which would
850 * preserve one large page occasionally, but it's really not worth the
851 * extra code and cycles for the common case.
853 if (pgprot_val(req_prot) != pgprot_val(new_prot))
854 return 1;
856 /* All checks passed. Update the large page mapping. */
857 new_pte = pfn_pte(old_pfn, new_prot);
858 __set_pmd_pte(kpte, address, new_pte);
859 cpa->flags |= CPA_FLUSHTLB;
860 cpa_inc_lp_preserved(level);
861 return 0;
864 static int should_split_large_page(pte_t *kpte, unsigned long address,
865 struct cpa_data *cpa)
867 int do_split;
869 if (cpa->force_split)
870 return 1;
872 spin_lock(&pgd_lock);
873 do_split = __should_split_large_page(kpte, address, cpa);
874 spin_unlock(&pgd_lock);
876 return do_split;
879 static void split_set_pte(struct cpa_data *cpa, pte_t *pte, unsigned long pfn,
880 pgprot_t ref_prot, unsigned long address,
881 unsigned long size)
883 unsigned int npg = PFN_DOWN(size);
884 pgprot_t prot;
887 * If should_split_large_page() discovered an inconsistent mapping,
888 * remove the invalid protection in the split mapping.
890 if (!cpa->force_static_prot)
891 goto set;
893 prot = static_protections(ref_prot, address, pfn, npg, CPA_PROTECT);
895 if (pgprot_val(prot) == pgprot_val(ref_prot))
896 goto set;
899 * If this is splitting a PMD, fix it up. PUD splits cannot be
900 * fixed trivially as that would require to rescan the newly
901 * installed PMD mappings after returning from split_large_page()
902 * so an eventual further split can allocate the necessary PTE
903 * pages. Warn for now and revisit it in case this actually
904 * happens.
906 if (size == PAGE_SIZE)
907 ref_prot = prot;
908 else
909 pr_warn_once("CPA: Cannot fixup static protections for PUD split\n");
910 set:
911 set_pte(pte, pfn_pte(pfn, ref_prot));
914 static int
915 __split_large_page(struct cpa_data *cpa, pte_t *kpte, unsigned long address,
916 struct page *base)
918 unsigned long lpaddr, lpinc, ref_pfn, pfn, pfninc = 1;
919 pte_t *pbase = (pte_t *)page_address(base);
920 unsigned int i, level;
921 pgprot_t ref_prot;
922 pte_t *tmp;
924 spin_lock(&pgd_lock);
926 * Check for races, another CPU might have split this page
927 * up for us already:
929 tmp = _lookup_address_cpa(cpa, address, &level);
930 if (tmp != kpte) {
931 spin_unlock(&pgd_lock);
932 return 1;
935 paravirt_alloc_pte(&init_mm, page_to_pfn(base));
937 switch (level) {
938 case PG_LEVEL_2M:
939 ref_prot = pmd_pgprot(*(pmd_t *)kpte);
941 * Clear PSE (aka _PAGE_PAT) and move
942 * PAT bit to correct position.
944 ref_prot = pgprot_large_2_4k(ref_prot);
945 ref_pfn = pmd_pfn(*(pmd_t *)kpte);
946 lpaddr = address & PMD_MASK;
947 lpinc = PAGE_SIZE;
948 break;
950 case PG_LEVEL_1G:
951 ref_prot = pud_pgprot(*(pud_t *)kpte);
952 ref_pfn = pud_pfn(*(pud_t *)kpte);
953 pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
954 lpaddr = address & PUD_MASK;
955 lpinc = PMD_SIZE;
957 * Clear the PSE flags if the PRESENT flag is not set
958 * otherwise pmd_present/pmd_huge will return true
959 * even on a non present pmd.
961 if (!(pgprot_val(ref_prot) & _PAGE_PRESENT))
962 pgprot_val(ref_prot) &= ~_PAGE_PSE;
963 break;
965 default:
966 spin_unlock(&pgd_lock);
967 return 1;
970 ref_prot = pgprot_clear_protnone_bits(ref_prot);
973 * Get the target pfn from the original entry:
975 pfn = ref_pfn;
976 for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc, lpaddr += lpinc)
977 split_set_pte(cpa, pbase + i, pfn, ref_prot, lpaddr, lpinc);
979 if (virt_addr_valid(address)) {
980 unsigned long pfn = PFN_DOWN(__pa(address));
982 if (pfn_range_is_mapped(pfn, pfn + 1))
983 split_page_count(level);
987 * Install the new, split up pagetable.
989 * We use the standard kernel pagetable protections for the new
990 * pagetable protections, the actual ptes set above control the
991 * primary protection behavior:
993 __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
996 * Do a global flush tlb after splitting the large page
997 * and before we do the actual change page attribute in the PTE.
999 * Without this, we violate the TLB application note, that says:
1000 * "The TLBs may contain both ordinary and large-page
1001 * translations for a 4-KByte range of linear addresses. This
1002 * may occur if software modifies the paging structures so that
1003 * the page size used for the address range changes. If the two
1004 * translations differ with respect to page frame or attributes
1005 * (e.g., permissions), processor behavior is undefined and may
1006 * be implementation-specific."
1008 * We do this global tlb flush inside the cpa_lock, so that we
1009 * don't allow any other cpu, with stale tlb entries change the
1010 * page attribute in parallel, that also falls into the
1011 * just split large page entry.
1013 flush_tlb_all();
1014 spin_unlock(&pgd_lock);
1016 return 0;
1019 static int split_large_page(struct cpa_data *cpa, pte_t *kpte,
1020 unsigned long address)
1022 struct page *base;
1024 if (!debug_pagealloc_enabled())
1025 spin_unlock(&cpa_lock);
1026 base = alloc_pages(GFP_KERNEL, 0);
1027 if (!debug_pagealloc_enabled())
1028 spin_lock(&cpa_lock);
1029 if (!base)
1030 return -ENOMEM;
1032 if (__split_large_page(cpa, kpte, address, base))
1033 __free_page(base);
1035 return 0;
1038 static bool try_to_free_pte_page(pte_t *pte)
1040 int i;
1042 for (i = 0; i < PTRS_PER_PTE; i++)
1043 if (!pte_none(pte[i]))
1044 return false;
1046 free_page((unsigned long)pte);
1047 return true;
1050 static bool try_to_free_pmd_page(pmd_t *pmd)
1052 int i;
1054 for (i = 0; i < PTRS_PER_PMD; i++)
1055 if (!pmd_none(pmd[i]))
1056 return false;
1058 free_page((unsigned long)pmd);
1059 return true;
1062 static bool unmap_pte_range(pmd_t *pmd, unsigned long start, unsigned long end)
1064 pte_t *pte = pte_offset_kernel(pmd, start);
1066 while (start < end) {
1067 set_pte(pte, __pte(0));
1069 start += PAGE_SIZE;
1070 pte++;
1073 if (try_to_free_pte_page((pte_t *)pmd_page_vaddr(*pmd))) {
1074 pmd_clear(pmd);
1075 return true;
1077 return false;
1080 static void __unmap_pmd_range(pud_t *pud, pmd_t *pmd,
1081 unsigned long start, unsigned long end)
1083 if (unmap_pte_range(pmd, start, end))
1084 if (try_to_free_pmd_page((pmd_t *)pud_page_vaddr(*pud)))
1085 pud_clear(pud);
1088 static void unmap_pmd_range(pud_t *pud, unsigned long start, unsigned long end)
1090 pmd_t *pmd = pmd_offset(pud, start);
1093 * Not on a 2MB page boundary?
1095 if (start & (PMD_SIZE - 1)) {
1096 unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
1097 unsigned long pre_end = min_t(unsigned long, end, next_page);
1099 __unmap_pmd_range(pud, pmd, start, pre_end);
1101 start = pre_end;
1102 pmd++;
1106 * Try to unmap in 2M chunks.
1108 while (end - start >= PMD_SIZE) {
1109 if (pmd_large(*pmd))
1110 pmd_clear(pmd);
1111 else
1112 __unmap_pmd_range(pud, pmd, start, start + PMD_SIZE);
1114 start += PMD_SIZE;
1115 pmd++;
1119 * 4K leftovers?
1121 if (start < end)
1122 return __unmap_pmd_range(pud, pmd, start, end);
1125 * Try again to free the PMD page if haven't succeeded above.
1127 if (!pud_none(*pud))
1128 if (try_to_free_pmd_page((pmd_t *)pud_page_vaddr(*pud)))
1129 pud_clear(pud);
1132 static void unmap_pud_range(p4d_t *p4d, unsigned long start, unsigned long end)
1134 pud_t *pud = pud_offset(p4d, start);
1137 * Not on a GB page boundary?
1139 if (start & (PUD_SIZE - 1)) {
1140 unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
1141 unsigned long pre_end = min_t(unsigned long, end, next_page);
1143 unmap_pmd_range(pud, start, pre_end);
1145 start = pre_end;
1146 pud++;
1150 * Try to unmap in 1G chunks?
1152 while (end - start >= PUD_SIZE) {
1154 if (pud_large(*pud))
1155 pud_clear(pud);
1156 else
1157 unmap_pmd_range(pud, start, start + PUD_SIZE);
1159 start += PUD_SIZE;
1160 pud++;
1164 * 2M leftovers?
1166 if (start < end)
1167 unmap_pmd_range(pud, start, end);
1170 * No need to try to free the PUD page because we'll free it in
1171 * populate_pgd's error path
1175 static int alloc_pte_page(pmd_t *pmd)
1177 pte_t *pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
1178 if (!pte)
1179 return -1;
1181 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
1182 return 0;
1185 static int alloc_pmd_page(pud_t *pud)
1187 pmd_t *pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL);
1188 if (!pmd)
1189 return -1;
1191 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
1192 return 0;
1195 static void populate_pte(struct cpa_data *cpa,
1196 unsigned long start, unsigned long end,
1197 unsigned num_pages, pmd_t *pmd, pgprot_t pgprot)
1199 pte_t *pte;
1201 pte = pte_offset_kernel(pmd, start);
1203 pgprot = pgprot_clear_protnone_bits(pgprot);
1205 while (num_pages-- && start < end) {
1206 set_pte(pte, pfn_pte(cpa->pfn, pgprot));
1208 start += PAGE_SIZE;
1209 cpa->pfn++;
1210 pte++;
1214 static long populate_pmd(struct cpa_data *cpa,
1215 unsigned long start, unsigned long end,
1216 unsigned num_pages, pud_t *pud, pgprot_t pgprot)
1218 long cur_pages = 0;
1219 pmd_t *pmd;
1220 pgprot_t pmd_pgprot;
1223 * Not on a 2M boundary?
1225 if (start & (PMD_SIZE - 1)) {
1226 unsigned long pre_end = start + (num_pages << PAGE_SHIFT);
1227 unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
1229 pre_end = min_t(unsigned long, pre_end, next_page);
1230 cur_pages = (pre_end - start) >> PAGE_SHIFT;
1231 cur_pages = min_t(unsigned int, num_pages, cur_pages);
1234 * Need a PTE page?
1236 pmd = pmd_offset(pud, start);
1237 if (pmd_none(*pmd))
1238 if (alloc_pte_page(pmd))
1239 return -1;
1241 populate_pte(cpa, start, pre_end, cur_pages, pmd, pgprot);
1243 start = pre_end;
1247 * We mapped them all?
1249 if (num_pages == cur_pages)
1250 return cur_pages;
1252 pmd_pgprot = pgprot_4k_2_large(pgprot);
1254 while (end - start >= PMD_SIZE) {
1257 * We cannot use a 1G page so allocate a PMD page if needed.
1259 if (pud_none(*pud))
1260 if (alloc_pmd_page(pud))
1261 return -1;
1263 pmd = pmd_offset(pud, start);
1265 set_pmd(pmd, pmd_mkhuge(pfn_pmd(cpa->pfn,
1266 canon_pgprot(pmd_pgprot))));
1268 start += PMD_SIZE;
1269 cpa->pfn += PMD_SIZE >> PAGE_SHIFT;
1270 cur_pages += PMD_SIZE >> PAGE_SHIFT;
1274 * Map trailing 4K pages.
1276 if (start < end) {
1277 pmd = pmd_offset(pud, start);
1278 if (pmd_none(*pmd))
1279 if (alloc_pte_page(pmd))
1280 return -1;
1282 populate_pte(cpa, start, end, num_pages - cur_pages,
1283 pmd, pgprot);
1285 return num_pages;
1288 static int populate_pud(struct cpa_data *cpa, unsigned long start, p4d_t *p4d,
1289 pgprot_t pgprot)
1291 pud_t *pud;
1292 unsigned long end;
1293 long cur_pages = 0;
1294 pgprot_t pud_pgprot;
1296 end = start + (cpa->numpages << PAGE_SHIFT);
1299 * Not on a Gb page boundary? => map everything up to it with
1300 * smaller pages.
1302 if (start & (PUD_SIZE - 1)) {
1303 unsigned long pre_end;
1304 unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
1306 pre_end = min_t(unsigned long, end, next_page);
1307 cur_pages = (pre_end - start) >> PAGE_SHIFT;
1308 cur_pages = min_t(int, (int)cpa->numpages, cur_pages);
1310 pud = pud_offset(p4d, start);
1313 * Need a PMD page?
1315 if (pud_none(*pud))
1316 if (alloc_pmd_page(pud))
1317 return -1;
1319 cur_pages = populate_pmd(cpa, start, pre_end, cur_pages,
1320 pud, pgprot);
1321 if (cur_pages < 0)
1322 return cur_pages;
1324 start = pre_end;
1327 /* We mapped them all? */
1328 if (cpa->numpages == cur_pages)
1329 return cur_pages;
1331 pud = pud_offset(p4d, start);
1332 pud_pgprot = pgprot_4k_2_large(pgprot);
1335 * Map everything starting from the Gb boundary, possibly with 1G pages
1337 while (boot_cpu_has(X86_FEATURE_GBPAGES) && end - start >= PUD_SIZE) {
1338 set_pud(pud, pud_mkhuge(pfn_pud(cpa->pfn,
1339 canon_pgprot(pud_pgprot))));
1341 start += PUD_SIZE;
1342 cpa->pfn += PUD_SIZE >> PAGE_SHIFT;
1343 cur_pages += PUD_SIZE >> PAGE_SHIFT;
1344 pud++;
1347 /* Map trailing leftover */
1348 if (start < end) {
1349 long tmp;
1351 pud = pud_offset(p4d, start);
1352 if (pud_none(*pud))
1353 if (alloc_pmd_page(pud))
1354 return -1;
1356 tmp = populate_pmd(cpa, start, end, cpa->numpages - cur_pages,
1357 pud, pgprot);
1358 if (tmp < 0)
1359 return cur_pages;
1361 cur_pages += tmp;
1363 return cur_pages;
1367 * Restrictions for kernel page table do not necessarily apply when mapping in
1368 * an alternate PGD.
1370 static int populate_pgd(struct cpa_data *cpa, unsigned long addr)
1372 pgprot_t pgprot = __pgprot(_KERNPG_TABLE);
1373 pud_t *pud = NULL; /* shut up gcc */
1374 p4d_t *p4d;
1375 pgd_t *pgd_entry;
1376 long ret;
1378 pgd_entry = cpa->pgd + pgd_index(addr);
1380 if (pgd_none(*pgd_entry)) {
1381 p4d = (p4d_t *)get_zeroed_page(GFP_KERNEL);
1382 if (!p4d)
1383 return -1;
1385 set_pgd(pgd_entry, __pgd(__pa(p4d) | _KERNPG_TABLE));
1389 * Allocate a PUD page and hand it down for mapping.
1391 p4d = p4d_offset(pgd_entry, addr);
1392 if (p4d_none(*p4d)) {
1393 pud = (pud_t *)get_zeroed_page(GFP_KERNEL);
1394 if (!pud)
1395 return -1;
1397 set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE));
1400 pgprot_val(pgprot) &= ~pgprot_val(cpa->mask_clr);
1401 pgprot_val(pgprot) |= pgprot_val(cpa->mask_set);
1403 ret = populate_pud(cpa, addr, p4d, pgprot);
1404 if (ret < 0) {
1406 * Leave the PUD page in place in case some other CPU or thread
1407 * already found it, but remove any useless entries we just
1408 * added to it.
1410 unmap_pud_range(p4d, addr,
1411 addr + (cpa->numpages << PAGE_SHIFT));
1412 return ret;
1415 cpa->numpages = ret;
1416 return 0;
1419 static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
1420 int primary)
1422 if (cpa->pgd) {
1424 * Right now, we only execute this code path when mapping
1425 * the EFI virtual memory map regions, no other users
1426 * provide a ->pgd value. This may change in the future.
1428 return populate_pgd(cpa, vaddr);
1432 * Ignore all non primary paths.
1434 if (!primary) {
1435 cpa->numpages = 1;
1436 return 0;
1440 * Ignore the NULL PTE for kernel identity mapping, as it is expected
1441 * to have holes.
1442 * Also set numpages to '1' indicating that we processed cpa req for
1443 * one virtual address page and its pfn. TBD: numpages can be set based
1444 * on the initial value and the level returned by lookup_address().
1446 if (within(vaddr, PAGE_OFFSET,
1447 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
1448 cpa->numpages = 1;
1449 cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
1450 return 0;
1452 } else if (__cpa_pfn_in_highmap(cpa->pfn)) {
1453 /* Faults in the highmap are OK, so do not warn: */
1454 return -EFAULT;
1455 } else {
1456 WARN(1, KERN_WARNING "CPA: called for zero pte. "
1457 "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
1458 *cpa->vaddr);
1460 return -EFAULT;
1464 static int __change_page_attr(struct cpa_data *cpa, int primary)
1466 unsigned long address;
1467 int do_split, err;
1468 unsigned int level;
1469 pte_t *kpte, old_pte;
1471 if (cpa->flags & CPA_PAGES_ARRAY) {
1472 struct page *page = cpa->pages[cpa->curpage];
1473 if (unlikely(PageHighMem(page)))
1474 return 0;
1475 address = (unsigned long)page_address(page);
1476 } else if (cpa->flags & CPA_ARRAY)
1477 address = cpa->vaddr[cpa->curpage];
1478 else
1479 address = *cpa->vaddr;
1480 repeat:
1481 kpte = _lookup_address_cpa(cpa, address, &level);
1482 if (!kpte)
1483 return __cpa_process_fault(cpa, address, primary);
1485 old_pte = *kpte;
1486 if (pte_none(old_pte))
1487 return __cpa_process_fault(cpa, address, primary);
1489 if (level == PG_LEVEL_4K) {
1490 pte_t new_pte;
1491 pgprot_t new_prot = pte_pgprot(old_pte);
1492 unsigned long pfn = pte_pfn(old_pte);
1494 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
1495 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
1497 cpa_inc_4k_install();
1498 new_prot = static_protections(new_prot, address, pfn, 1,
1499 CPA_PROTECT);
1501 new_prot = pgprot_clear_protnone_bits(new_prot);
1504 * We need to keep the pfn from the existing PTE,
1505 * after all we're only going to change it's attributes
1506 * not the memory it points to
1508 new_pte = pfn_pte(pfn, new_prot);
1509 cpa->pfn = pfn;
1511 * Do we really change anything ?
1513 if (pte_val(old_pte) != pte_val(new_pte)) {
1514 set_pte_atomic(kpte, new_pte);
1515 cpa->flags |= CPA_FLUSHTLB;
1517 cpa->numpages = 1;
1518 return 0;
1522 * Check, whether we can keep the large page intact
1523 * and just change the pte:
1525 do_split = should_split_large_page(kpte, address, cpa);
1527 * When the range fits into the existing large page,
1528 * return. cp->numpages and cpa->tlbflush have been updated in
1529 * try_large_page:
1531 if (do_split <= 0)
1532 return do_split;
1535 * We have to split the large page:
1537 err = split_large_page(cpa, kpte, address);
1538 if (!err)
1539 goto repeat;
1541 return err;
1544 static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
1546 static int cpa_process_alias(struct cpa_data *cpa)
1548 struct cpa_data alias_cpa;
1549 unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
1550 unsigned long vaddr;
1551 int ret;
1553 if (!pfn_range_is_mapped(cpa->pfn, cpa->pfn + 1))
1554 return 0;
1557 * No need to redo, when the primary call touched the direct
1558 * mapping already:
1560 if (cpa->flags & CPA_PAGES_ARRAY) {
1561 struct page *page = cpa->pages[cpa->curpage];
1562 if (unlikely(PageHighMem(page)))
1563 return 0;
1564 vaddr = (unsigned long)page_address(page);
1565 } else if (cpa->flags & CPA_ARRAY)
1566 vaddr = cpa->vaddr[cpa->curpage];
1567 else
1568 vaddr = *cpa->vaddr;
1570 if (!(within(vaddr, PAGE_OFFSET,
1571 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
1573 alias_cpa = *cpa;
1574 alias_cpa.vaddr = &laddr;
1575 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
1577 ret = __change_page_attr_set_clr(&alias_cpa, 0);
1578 if (ret)
1579 return ret;
1582 #ifdef CONFIG_X86_64
1584 * If the primary call didn't touch the high mapping already
1585 * and the physical address is inside the kernel map, we need
1586 * to touch the high mapped kernel as well:
1588 if (!within(vaddr, (unsigned long)_text, _brk_end) &&
1589 __cpa_pfn_in_highmap(cpa->pfn)) {
1590 unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
1591 __START_KERNEL_map - phys_base;
1592 alias_cpa = *cpa;
1593 alias_cpa.vaddr = &temp_cpa_vaddr;
1594 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
1597 * The high mapping range is imprecise, so ignore the
1598 * return value.
1600 __change_page_attr_set_clr(&alias_cpa, 0);
1602 #endif
1604 return 0;
1607 static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
1609 unsigned long numpages = cpa->numpages;
1610 int ret;
1612 while (numpages) {
1614 * Store the remaining nr of pages for the large page
1615 * preservation check.
1617 cpa->numpages = numpages;
1618 /* for array changes, we can't use large page */
1619 if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY))
1620 cpa->numpages = 1;
1622 if (!debug_pagealloc_enabled())
1623 spin_lock(&cpa_lock);
1624 ret = __change_page_attr(cpa, checkalias);
1625 if (!debug_pagealloc_enabled())
1626 spin_unlock(&cpa_lock);
1627 if (ret)
1628 return ret;
1630 if (checkalias) {
1631 ret = cpa_process_alias(cpa);
1632 if (ret)
1633 return ret;
1637 * Adjust the number of pages with the result of the
1638 * CPA operation. Either a large page has been
1639 * preserved or a single page update happened.
1641 BUG_ON(cpa->numpages > numpages || !cpa->numpages);
1642 numpages -= cpa->numpages;
1643 if (cpa->flags & (CPA_PAGES_ARRAY | CPA_ARRAY))
1644 cpa->curpage++;
1645 else
1646 *cpa->vaddr += cpa->numpages * PAGE_SIZE;
1649 return 0;
1653 * Machine check recovery code needs to change cache mode of poisoned
1654 * pages to UC to avoid speculative access logging another error. But
1655 * passing the address of the 1:1 mapping to set_memory_uc() is a fine
1656 * way to encourage a speculative access. So we cheat and flip the top
1657 * bit of the address. This works fine for the code that updates the
1658 * page tables. But at the end of the process we need to flush the cache
1659 * and the non-canonical address causes a #GP fault when used by the
1660 * CLFLUSH instruction.
1662 * But in the common case we already have a canonical address. This code
1663 * will fix the top bit if needed and is a no-op otherwise.
1665 static inline unsigned long make_addr_canonical_again(unsigned long addr)
1667 #ifdef CONFIG_X86_64
1668 return (long)(addr << 1) >> 1;
1669 #else
1670 return addr;
1671 #endif
1675 static int change_page_attr_set_clr(unsigned long *addr, int numpages,
1676 pgprot_t mask_set, pgprot_t mask_clr,
1677 int force_split, int in_flag,
1678 struct page **pages)
1680 struct cpa_data cpa;
1681 int ret, cache, checkalias;
1682 unsigned long baddr = 0;
1684 memset(&cpa, 0, sizeof(cpa));
1687 * Check, if we are requested to set a not supported
1688 * feature. Clearing non-supported features is OK.
1690 mask_set = canon_pgprot(mask_set);
1692 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
1693 return 0;
1695 /* Ensure we are PAGE_SIZE aligned */
1696 if (in_flag & CPA_ARRAY) {
1697 int i;
1698 for (i = 0; i < numpages; i++) {
1699 if (addr[i] & ~PAGE_MASK) {
1700 addr[i] &= PAGE_MASK;
1701 WARN_ON_ONCE(1);
1704 } else if (!(in_flag & CPA_PAGES_ARRAY)) {
1706 * in_flag of CPA_PAGES_ARRAY implies it is aligned.
1707 * No need to cehck in that case
1709 if (*addr & ~PAGE_MASK) {
1710 *addr &= PAGE_MASK;
1712 * People should not be passing in unaligned addresses:
1714 WARN_ON_ONCE(1);
1717 * Save address for cache flush. *addr is modified in the call
1718 * to __change_page_attr_set_clr() below.
1720 baddr = make_addr_canonical_again(*addr);
1723 /* Must avoid aliasing mappings in the highmem code */
1724 kmap_flush_unused();
1726 vm_unmap_aliases();
1728 cpa.vaddr = addr;
1729 cpa.pages = pages;
1730 cpa.numpages = numpages;
1731 cpa.mask_set = mask_set;
1732 cpa.mask_clr = mask_clr;
1733 cpa.flags = 0;
1734 cpa.curpage = 0;
1735 cpa.force_split = force_split;
1737 if (in_flag & (CPA_ARRAY | CPA_PAGES_ARRAY))
1738 cpa.flags |= in_flag;
1740 /* No alias checking for _NX bit modifications */
1741 checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
1742 /* Has caller explicitly disabled alias checking? */
1743 if (in_flag & CPA_NO_CHECK_ALIAS)
1744 checkalias = 0;
1746 ret = __change_page_attr_set_clr(&cpa, checkalias);
1749 * Check whether we really changed something:
1751 if (!(cpa.flags & CPA_FLUSHTLB))
1752 goto out;
1755 * No need to flush, when we did not set any of the caching
1756 * attributes:
1758 cache = !!pgprot2cachemode(mask_set);
1761 * On error; flush everything to be sure.
1763 if (ret) {
1764 cpa_flush_all(cache);
1765 goto out;
1768 if (cpa.flags & (CPA_PAGES_ARRAY | CPA_ARRAY)) {
1769 cpa_flush_array(baddr, addr, numpages, cache,
1770 cpa.flags, pages);
1771 } else {
1772 cpa_flush_range(baddr, numpages, cache);
1775 out:
1776 return ret;
1779 static inline int change_page_attr_set(unsigned long *addr, int numpages,
1780 pgprot_t mask, int array)
1782 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
1783 (array ? CPA_ARRAY : 0), NULL);
1786 static inline int change_page_attr_clear(unsigned long *addr, int numpages,
1787 pgprot_t mask, int array)
1789 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
1790 (array ? CPA_ARRAY : 0), NULL);
1793 static inline int cpa_set_pages_array(struct page **pages, int numpages,
1794 pgprot_t mask)
1796 return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0,
1797 CPA_PAGES_ARRAY, pages);
1800 static inline int cpa_clear_pages_array(struct page **pages, int numpages,
1801 pgprot_t mask)
1803 return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
1804 CPA_PAGES_ARRAY, pages);
1807 int _set_memory_uc(unsigned long addr, int numpages)
1810 * for now UC MINUS. see comments in ioremap_nocache()
1811 * If you really need strong UC use ioremap_uc(), but note
1812 * that you cannot override IO areas with set_memory_*() as
1813 * these helpers cannot work with IO memory.
1815 return change_page_attr_set(&addr, numpages,
1816 cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS),
1820 int set_memory_uc(unsigned long addr, int numpages)
1822 int ret;
1825 * for now UC MINUS. see comments in ioremap_nocache()
1827 ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
1828 _PAGE_CACHE_MODE_UC_MINUS, NULL);
1829 if (ret)
1830 goto out_err;
1832 ret = _set_memory_uc(addr, numpages);
1833 if (ret)
1834 goto out_free;
1836 return 0;
1838 out_free:
1839 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1840 out_err:
1841 return ret;
1843 EXPORT_SYMBOL(set_memory_uc);
1845 static int _set_memory_array(unsigned long *addr, int addrinarray,
1846 enum page_cache_mode new_type)
1848 enum page_cache_mode set_type;
1849 int i, j;
1850 int ret;
1852 for (i = 0; i < addrinarray; i++) {
1853 ret = reserve_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE,
1854 new_type, NULL);
1855 if (ret)
1856 goto out_free;
1859 /* If WC, set to UC- first and then WC */
1860 set_type = (new_type == _PAGE_CACHE_MODE_WC) ?
1861 _PAGE_CACHE_MODE_UC_MINUS : new_type;
1863 ret = change_page_attr_set(addr, addrinarray,
1864 cachemode2pgprot(set_type), 1);
1866 if (!ret && new_type == _PAGE_CACHE_MODE_WC)
1867 ret = change_page_attr_set_clr(addr, addrinarray,
1868 cachemode2pgprot(
1869 _PAGE_CACHE_MODE_WC),
1870 __pgprot(_PAGE_CACHE_MASK),
1871 0, CPA_ARRAY, NULL);
1872 if (ret)
1873 goto out_free;
1875 return 0;
1877 out_free:
1878 for (j = 0; j < i; j++)
1879 free_memtype(__pa(addr[j]), __pa(addr[j]) + PAGE_SIZE);
1881 return ret;
1884 int set_memory_array_uc(unsigned long *addr, int addrinarray)
1886 return _set_memory_array(addr, addrinarray, _PAGE_CACHE_MODE_UC_MINUS);
1888 EXPORT_SYMBOL(set_memory_array_uc);
1890 int set_memory_array_wc(unsigned long *addr, int addrinarray)
1892 return _set_memory_array(addr, addrinarray, _PAGE_CACHE_MODE_WC);
1894 EXPORT_SYMBOL(set_memory_array_wc);
1896 int set_memory_array_wt(unsigned long *addr, int addrinarray)
1898 return _set_memory_array(addr, addrinarray, _PAGE_CACHE_MODE_WT);
1900 EXPORT_SYMBOL_GPL(set_memory_array_wt);
1902 int _set_memory_wc(unsigned long addr, int numpages)
1904 int ret;
1905 unsigned long addr_copy = addr;
1907 ret = change_page_attr_set(&addr, numpages,
1908 cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS),
1910 if (!ret) {
1911 ret = change_page_attr_set_clr(&addr_copy, numpages,
1912 cachemode2pgprot(
1913 _PAGE_CACHE_MODE_WC),
1914 __pgprot(_PAGE_CACHE_MASK),
1915 0, 0, NULL);
1917 return ret;
1920 int set_memory_wc(unsigned long addr, int numpages)
1922 int ret;
1924 ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
1925 _PAGE_CACHE_MODE_WC, NULL);
1926 if (ret)
1927 return ret;
1929 ret = _set_memory_wc(addr, numpages);
1930 if (ret)
1931 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1933 return ret;
1935 EXPORT_SYMBOL(set_memory_wc);
1937 int _set_memory_wt(unsigned long addr, int numpages)
1939 return change_page_attr_set(&addr, numpages,
1940 cachemode2pgprot(_PAGE_CACHE_MODE_WT), 0);
1943 int set_memory_wt(unsigned long addr, int numpages)
1945 int ret;
1947 ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
1948 _PAGE_CACHE_MODE_WT, NULL);
1949 if (ret)
1950 return ret;
1952 ret = _set_memory_wt(addr, numpages);
1953 if (ret)
1954 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1956 return ret;
1958 EXPORT_SYMBOL_GPL(set_memory_wt);
1960 int _set_memory_wb(unsigned long addr, int numpages)
1962 /* WB cache mode is hard wired to all cache attribute bits being 0 */
1963 return change_page_attr_clear(&addr, numpages,
1964 __pgprot(_PAGE_CACHE_MASK), 0);
1967 int set_memory_wb(unsigned long addr, int numpages)
1969 int ret;
1971 ret = _set_memory_wb(addr, numpages);
1972 if (ret)
1973 return ret;
1975 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1976 return 0;
1978 EXPORT_SYMBOL(set_memory_wb);
1980 int set_memory_array_wb(unsigned long *addr, int addrinarray)
1982 int i;
1983 int ret;
1985 /* WB cache mode is hard wired to all cache attribute bits being 0 */
1986 ret = change_page_attr_clear(addr, addrinarray,
1987 __pgprot(_PAGE_CACHE_MASK), 1);
1988 if (ret)
1989 return ret;
1991 for (i = 0; i < addrinarray; i++)
1992 free_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE);
1994 return 0;
1996 EXPORT_SYMBOL(set_memory_array_wb);
1998 int set_memory_x(unsigned long addr, int numpages)
2000 if (!(__supported_pte_mask & _PAGE_NX))
2001 return 0;
2003 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
2005 EXPORT_SYMBOL(set_memory_x);
2007 int set_memory_nx(unsigned long addr, int numpages)
2009 if (!(__supported_pte_mask & _PAGE_NX))
2010 return 0;
2012 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
2014 EXPORT_SYMBOL(set_memory_nx);
2016 int set_memory_ro(unsigned long addr, int numpages)
2018 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
2021 int set_memory_rw(unsigned long addr, int numpages)
2023 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
2026 int set_memory_np(unsigned long addr, int numpages)
2028 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
2031 int set_memory_np_noalias(unsigned long addr, int numpages)
2033 int cpa_flags = CPA_NO_CHECK_ALIAS;
2035 return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
2036 __pgprot(_PAGE_PRESENT), 0,
2037 cpa_flags, NULL);
2040 int set_memory_4k(unsigned long addr, int numpages)
2042 return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
2043 __pgprot(0), 1, 0, NULL);
2046 int set_memory_nonglobal(unsigned long addr, int numpages)
2048 return change_page_attr_clear(&addr, numpages,
2049 __pgprot(_PAGE_GLOBAL), 0);
2052 int set_memory_global(unsigned long addr, int numpages)
2054 return change_page_attr_set(&addr, numpages,
2055 __pgprot(_PAGE_GLOBAL), 0);
2058 static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc)
2060 struct cpa_data cpa;
2061 unsigned long start;
2062 int ret;
2064 /* Nothing to do if memory encryption is not active */
2065 if (!mem_encrypt_active())
2066 return 0;
2068 /* Should not be working on unaligned addresses */
2069 if (WARN_ONCE(addr & ~PAGE_MASK, "misaligned address: %#lx\n", addr))
2070 addr &= PAGE_MASK;
2072 start = addr;
2074 memset(&cpa, 0, sizeof(cpa));
2075 cpa.vaddr = &addr;
2076 cpa.numpages = numpages;
2077 cpa.mask_set = enc ? __pgprot(_PAGE_ENC) : __pgprot(0);
2078 cpa.mask_clr = enc ? __pgprot(0) : __pgprot(_PAGE_ENC);
2079 cpa.pgd = init_mm.pgd;
2081 /* Must avoid aliasing mappings in the highmem code */
2082 kmap_flush_unused();
2083 vm_unmap_aliases();
2086 * Before changing the encryption attribute, we need to flush caches.
2088 cpa_flush_range(start, numpages, 1);
2090 ret = __change_page_attr_set_clr(&cpa, 1);
2093 * After changing the encryption attribute, we need to flush TLBs
2094 * again in case any speculative TLB caching occurred (but no need
2095 * to flush caches again). We could just use cpa_flush_all(), but
2096 * in case TLB flushing gets optimized in the cpa_flush_range()
2097 * path use the same logic as above.
2099 cpa_flush_range(start, numpages, 0);
2101 return ret;
2104 int set_memory_encrypted(unsigned long addr, int numpages)
2106 return __set_memory_enc_dec(addr, numpages, true);
2108 EXPORT_SYMBOL_GPL(set_memory_encrypted);
2110 int set_memory_decrypted(unsigned long addr, int numpages)
2112 return __set_memory_enc_dec(addr, numpages, false);
2114 EXPORT_SYMBOL_GPL(set_memory_decrypted);
2116 int set_pages_uc(struct page *page, int numpages)
2118 unsigned long addr = (unsigned long)page_address(page);
2120 return set_memory_uc(addr, numpages);
2122 EXPORT_SYMBOL(set_pages_uc);
2124 static int _set_pages_array(struct page **pages, int addrinarray,
2125 enum page_cache_mode new_type)
2127 unsigned long start;
2128 unsigned long end;
2129 enum page_cache_mode set_type;
2130 int i;
2131 int free_idx;
2132 int ret;
2134 for (i = 0; i < addrinarray; i++) {
2135 if (PageHighMem(pages[i]))
2136 continue;
2137 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
2138 end = start + PAGE_SIZE;
2139 if (reserve_memtype(start, end, new_type, NULL))
2140 goto err_out;
2143 /* If WC, set to UC- first and then WC */
2144 set_type = (new_type == _PAGE_CACHE_MODE_WC) ?
2145 _PAGE_CACHE_MODE_UC_MINUS : new_type;
2147 ret = cpa_set_pages_array(pages, addrinarray,
2148 cachemode2pgprot(set_type));
2149 if (!ret && new_type == _PAGE_CACHE_MODE_WC)
2150 ret = change_page_attr_set_clr(NULL, addrinarray,
2151 cachemode2pgprot(
2152 _PAGE_CACHE_MODE_WC),
2153 __pgprot(_PAGE_CACHE_MASK),
2154 0, CPA_PAGES_ARRAY, pages);
2155 if (ret)
2156 goto err_out;
2157 return 0; /* Success */
2158 err_out:
2159 free_idx = i;
2160 for (i = 0; i < free_idx; i++) {
2161 if (PageHighMem(pages[i]))
2162 continue;
2163 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
2164 end = start + PAGE_SIZE;
2165 free_memtype(start, end);
2167 return -EINVAL;
2170 int set_pages_array_uc(struct page **pages, int addrinarray)
2172 return _set_pages_array(pages, addrinarray, _PAGE_CACHE_MODE_UC_MINUS);
2174 EXPORT_SYMBOL(set_pages_array_uc);
2176 int set_pages_array_wc(struct page **pages, int addrinarray)
2178 return _set_pages_array(pages, addrinarray, _PAGE_CACHE_MODE_WC);
2180 EXPORT_SYMBOL(set_pages_array_wc);
2182 int set_pages_array_wt(struct page **pages, int addrinarray)
2184 return _set_pages_array(pages, addrinarray, _PAGE_CACHE_MODE_WT);
2186 EXPORT_SYMBOL_GPL(set_pages_array_wt);
2188 int set_pages_wb(struct page *page, int numpages)
2190 unsigned long addr = (unsigned long)page_address(page);
2192 return set_memory_wb(addr, numpages);
2194 EXPORT_SYMBOL(set_pages_wb);
2196 int set_pages_array_wb(struct page **pages, int addrinarray)
2198 int retval;
2199 unsigned long start;
2200 unsigned long end;
2201 int i;
2203 /* WB cache mode is hard wired to all cache attribute bits being 0 */
2204 retval = cpa_clear_pages_array(pages, addrinarray,
2205 __pgprot(_PAGE_CACHE_MASK));
2206 if (retval)
2207 return retval;
2209 for (i = 0; i < addrinarray; i++) {
2210 if (PageHighMem(pages[i]))
2211 continue;
2212 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
2213 end = start + PAGE_SIZE;
2214 free_memtype(start, end);
2217 return 0;
2219 EXPORT_SYMBOL(set_pages_array_wb);
2221 int set_pages_x(struct page *page, int numpages)
2223 unsigned long addr = (unsigned long)page_address(page);
2225 return set_memory_x(addr, numpages);
2227 EXPORT_SYMBOL(set_pages_x);
2229 int set_pages_nx(struct page *page, int numpages)
2231 unsigned long addr = (unsigned long)page_address(page);
2233 return set_memory_nx(addr, numpages);
2235 EXPORT_SYMBOL(set_pages_nx);
2237 int set_pages_ro(struct page *page, int numpages)
2239 unsigned long addr = (unsigned long)page_address(page);
2241 return set_memory_ro(addr, numpages);
2244 int set_pages_rw(struct page *page, int numpages)
2246 unsigned long addr = (unsigned long)page_address(page);
2248 return set_memory_rw(addr, numpages);
2251 #ifdef CONFIG_DEBUG_PAGEALLOC
2253 static int __set_pages_p(struct page *page, int numpages)
2255 unsigned long tempaddr = (unsigned long) page_address(page);
2256 struct cpa_data cpa = { .vaddr = &tempaddr,
2257 .pgd = NULL,
2258 .numpages = numpages,
2259 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
2260 .mask_clr = __pgprot(0),
2261 .flags = 0};
2264 * No alias checking needed for setting present flag. otherwise,
2265 * we may need to break large pages for 64-bit kernel text
2266 * mappings (this adds to complexity if we want to do this from
2267 * atomic context especially). Let's keep it simple!
2269 return __change_page_attr_set_clr(&cpa, 0);
2272 static int __set_pages_np(struct page *page, int numpages)
2274 unsigned long tempaddr = (unsigned long) page_address(page);
2275 struct cpa_data cpa = { .vaddr = &tempaddr,
2276 .pgd = NULL,
2277 .numpages = numpages,
2278 .mask_set = __pgprot(0),
2279 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
2280 .flags = 0};
2283 * No alias checking needed for setting not present flag. otherwise,
2284 * we may need to break large pages for 64-bit kernel text
2285 * mappings (this adds to complexity if we want to do this from
2286 * atomic context especially). Let's keep it simple!
2288 return __change_page_attr_set_clr(&cpa, 0);
2291 void __kernel_map_pages(struct page *page, int numpages, int enable)
2293 if (PageHighMem(page))
2294 return;
2295 if (!enable) {
2296 debug_check_no_locks_freed(page_address(page),
2297 numpages * PAGE_SIZE);
2301 * The return value is ignored as the calls cannot fail.
2302 * Large pages for identity mappings are not used at boot time
2303 * and hence no memory allocations during large page split.
2305 if (enable)
2306 __set_pages_p(page, numpages);
2307 else
2308 __set_pages_np(page, numpages);
2311 * We should perform an IPI and flush all tlbs,
2312 * but that can deadlock->flush only current cpu:
2314 __flush_tlb_all();
2316 arch_flush_lazy_mmu_mode();
2319 #ifdef CONFIG_HIBERNATION
2321 bool kernel_page_present(struct page *page)
2323 unsigned int level;
2324 pte_t *pte;
2326 if (PageHighMem(page))
2327 return false;
2329 pte = lookup_address((unsigned long)page_address(page), &level);
2330 return (pte_val(*pte) & _PAGE_PRESENT);
2333 #endif /* CONFIG_HIBERNATION */
2335 #endif /* CONFIG_DEBUG_PAGEALLOC */
2337 int kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address,
2338 unsigned numpages, unsigned long page_flags)
2340 int retval = -EINVAL;
2342 struct cpa_data cpa = {
2343 .vaddr = &address,
2344 .pfn = pfn,
2345 .pgd = pgd,
2346 .numpages = numpages,
2347 .mask_set = __pgprot(0),
2348 .mask_clr = __pgprot(0),
2349 .flags = 0,
2352 if (!(__supported_pte_mask & _PAGE_NX))
2353 goto out;
2355 if (!(page_flags & _PAGE_NX))
2356 cpa.mask_clr = __pgprot(_PAGE_NX);
2358 if (!(page_flags & _PAGE_RW))
2359 cpa.mask_clr = __pgprot(_PAGE_RW);
2361 if (!(page_flags & _PAGE_ENC))
2362 cpa.mask_clr = pgprot_encrypted(cpa.mask_clr);
2364 cpa.mask_set = __pgprot(_PAGE_PRESENT | page_flags);
2366 retval = __change_page_attr_set_clr(&cpa, 0);
2367 __flush_tlb_all();
2369 out:
2370 return retval;
2374 * The testcases use internal knowledge of the implementation that shouldn't
2375 * be exposed to the rest of the kernel. Include these directly here.
2377 #ifdef CONFIG_CPA_DEBUG
2378 #include "pageattr-test.c"
2379 #endif