2 * Copyright 2002 Andi Kleen, SuSE Labs.
3 * Thanks to Ben LaHaise for precious feedback.
5 #include <linux/highmem.h>
6 #include <linux/memblock.h>
7 #include <linux/sched.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>
27 #include <asm/set_memory.h>
29 #include "mm_internal.h"
32 * The current flushing context - we pass it instead of 5 arguments:
39 unsigned long numpages;
40 unsigned long curpage;
43 unsigned int force_split : 1,
44 force_static_prot : 1;
54 static const int cpa_warn_level = CPA_PROTECT;
57 * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings)
58 * using cpa_lock. So that we don't allow any other cpu, with stale large tlb
59 * entries change the page attribute in parallel to some other cpu
60 * splitting a large page entry along with changing the attribute.
62 static DEFINE_SPINLOCK(cpa_lock);
64 #define CPA_FLUSHTLB 1
66 #define CPA_PAGES_ARRAY 4
67 #define CPA_NO_CHECK_ALIAS 8 /* Do not search for aliases */
70 static unsigned long direct_pages_count[PG_LEVEL_NUM];
72 void update_page_count(int level, unsigned long pages)
74 /* Protect against CPA */
76 direct_pages_count[level] += pages;
77 spin_unlock(&pgd_lock);
80 static void split_page_count(int level)
82 if (direct_pages_count[level] == 0)
85 direct_pages_count[level]--;
86 direct_pages_count[level - 1] += PTRS_PER_PTE;
89 void arch_report_meminfo(struct seq_file *m)
91 seq_printf(m, "DirectMap4k: %8lu kB\n",
92 direct_pages_count[PG_LEVEL_4K] << 2);
93 #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
94 seq_printf(m, "DirectMap2M: %8lu kB\n",
95 direct_pages_count[PG_LEVEL_2M] << 11);
97 seq_printf(m, "DirectMap4M: %8lu kB\n",
98 direct_pages_count[PG_LEVEL_2M] << 12);
101 seq_printf(m, "DirectMap1G: %8lu kB\n",
102 direct_pages_count[PG_LEVEL_1G] << 20);
105 static inline void split_page_count(int level) { }
108 #ifdef CONFIG_X86_CPA_STATISTICS
110 static unsigned long cpa_1g_checked;
111 static unsigned long cpa_1g_sameprot;
112 static unsigned long cpa_1g_preserved;
113 static unsigned long cpa_2m_checked;
114 static unsigned long cpa_2m_sameprot;
115 static unsigned long cpa_2m_preserved;
116 static unsigned long cpa_4k_install;
118 static inline void cpa_inc_1g_checked(void)
123 static inline void cpa_inc_2m_checked(void)
128 static inline void cpa_inc_4k_install(void)
133 static inline void cpa_inc_lp_sameprot(int level)
135 if (level == PG_LEVEL_1G)
141 static inline void cpa_inc_lp_preserved(int level)
143 if (level == PG_LEVEL_1G)
149 static int cpastats_show(struct seq_file *m, void *p)
151 seq_printf(m, "1G pages checked: %16lu\n", cpa_1g_checked);
152 seq_printf(m, "1G pages sameprot: %16lu\n", cpa_1g_sameprot);
153 seq_printf(m, "1G pages preserved: %16lu\n", cpa_1g_preserved);
154 seq_printf(m, "2M pages checked: %16lu\n", cpa_2m_checked);
155 seq_printf(m, "2M pages sameprot: %16lu\n", cpa_2m_sameprot);
156 seq_printf(m, "2M pages preserved: %16lu\n", cpa_2m_preserved);
157 seq_printf(m, "4K pages set-checked: %16lu\n", cpa_4k_install);
161 static int cpastats_open(struct inode *inode, struct file *file)
163 return single_open(file, cpastats_show, NULL);
166 static const struct file_operations cpastats_fops = {
167 .open = cpastats_open,
170 .release = single_release,
173 static int __init cpa_stats_init(void)
175 debugfs_create_file("cpa_stats", S_IRUSR, arch_debugfs_dir, NULL,
179 late_initcall(cpa_stats_init);
181 static inline void cpa_inc_1g_checked(void) { }
182 static inline void cpa_inc_2m_checked(void) { }
183 static inline void cpa_inc_4k_install(void) { }
184 static inline void cpa_inc_lp_sameprot(int level) { }
185 static inline void cpa_inc_lp_preserved(int level) { }
190 within(unsigned long addr, unsigned long start, unsigned long end)
192 return addr >= start && addr < end;
196 within_inclusive(unsigned long addr, unsigned long start, unsigned long end)
198 return addr >= start && addr <= end;
203 static inline unsigned long highmap_start_pfn(void)
205 return __pa_symbol(_text) >> PAGE_SHIFT;
208 static inline unsigned long highmap_end_pfn(void)
210 /* Do not reference physical address outside the kernel. */
211 return __pa_symbol(roundup(_brk_end, PMD_SIZE) - 1) >> PAGE_SHIFT;
214 static bool __cpa_pfn_in_highmap(unsigned long pfn)
217 * Kernel text has an alias mapping at a high address, known
220 return within_inclusive(pfn, highmap_start_pfn(), highmap_end_pfn());
225 static bool __cpa_pfn_in_highmap(unsigned long pfn)
227 /* There is no highmap on 32-bit */
233 static unsigned long __cpa_addr(struct cpa_data *cpa, unsigned long idx)
235 if (cpa->flags & CPA_PAGES_ARRAY) {
236 struct page *page = cpa->pages[idx];
238 if (unlikely(PageHighMem(page)))
241 return (unsigned long)page_address(page);
244 if (cpa->flags & CPA_ARRAY)
245 return cpa->vaddr[idx];
247 return *cpa->vaddr + idx * PAGE_SIZE;
254 static void clflush_cache_range_opt(void *vaddr, unsigned int size)
256 const unsigned long clflush_size = boot_cpu_data.x86_clflush_size;
257 void *p = (void *)((unsigned long)vaddr & ~(clflush_size - 1));
258 void *vend = vaddr + size;
263 for (; p < vend; p += clflush_size)
268 * clflush_cache_range - flush a cache range with clflush
269 * @vaddr: virtual start address
270 * @size: number of bytes to flush
272 * CLFLUSHOPT is an unordered instruction which needs fencing with MFENCE or
273 * SFENCE to avoid ordering issues.
275 void clflush_cache_range(void *vaddr, unsigned int size)
278 clflush_cache_range_opt(vaddr, size);
281 EXPORT_SYMBOL_GPL(clflush_cache_range);
283 void arch_invalidate_pmem(void *addr, size_t size)
285 clflush_cache_range(addr, size);
287 EXPORT_SYMBOL_GPL(arch_invalidate_pmem);
289 static void __cpa_flush_all(void *arg)
291 unsigned long cache = (unsigned long)arg;
294 * Flush all to work around Errata in early athlons regarding
295 * large page flushing.
299 if (cache && boot_cpu_data.x86 >= 4)
303 static void cpa_flush_all(unsigned long cache)
305 BUG_ON(irqs_disabled() && !early_boot_irqs_disabled);
307 on_each_cpu(__cpa_flush_all, (void *) cache, 1);
310 void __cpa_flush_tlb(void *data)
312 struct cpa_data *cpa = data;
315 for (i = 0; i < cpa->numpages; i++)
316 __flush_tlb_one_kernel(__cpa_addr(cpa, i));
319 static void cpa_flush(struct cpa_data *data, int cache)
321 struct cpa_data *cpa = data;
324 BUG_ON(irqs_disabled() && !early_boot_irqs_disabled);
326 if (cache && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
327 cpa_flush_all(cache);
331 if (cpa->numpages <= tlb_single_page_flush_ceiling)
332 on_each_cpu(__cpa_flush_tlb, cpa, 1);
340 for (i = 0; i < cpa->numpages; i++) {
341 unsigned long addr = __cpa_addr(cpa, i);
344 pte_t *pte = lookup_address(addr, &level);
347 * Only flush present addresses:
349 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
350 clflush_cache_range_opt((void *)addr, PAGE_SIZE);
355 static bool overlaps(unsigned long r1_start, unsigned long r1_end,
356 unsigned long r2_start, unsigned long r2_end)
358 return (r1_start <= r2_end && r1_end >= r2_start) ||
359 (r2_start <= r1_end && r2_end >= r1_start);
362 #ifdef CONFIG_PCI_BIOS
364 * The BIOS area between 640k and 1Mb needs to be executable for PCI BIOS
365 * based config access (CONFIG_PCI_GOBIOS) support.
367 #define BIOS_PFN PFN_DOWN(BIOS_BEGIN)
368 #define BIOS_PFN_END PFN_DOWN(BIOS_END - 1)
370 static pgprotval_t protect_pci_bios(unsigned long spfn, unsigned long epfn)
372 if (pcibios_enabled && overlaps(spfn, epfn, BIOS_PFN, BIOS_PFN_END))
377 static pgprotval_t protect_pci_bios(unsigned long spfn, unsigned long epfn)
384 * The .rodata section needs to be read-only. Using the pfn catches all
385 * aliases. This also includes __ro_after_init, so do not enforce until
386 * kernel_set_to_readonly is true.
388 static pgprotval_t protect_rodata(unsigned long spfn, unsigned long epfn)
390 unsigned long epfn_ro, spfn_ro = PFN_DOWN(__pa_symbol(__start_rodata));
393 * Note: __end_rodata is at page aligned and not inclusive, so
394 * subtract 1 to get the last enforced PFN in the rodata area.
396 epfn_ro = PFN_DOWN(__pa_symbol(__end_rodata)) - 1;
398 if (kernel_set_to_readonly && overlaps(spfn, epfn, spfn_ro, epfn_ro))
404 * Protect kernel text against becoming non executable by forbidding
405 * _PAGE_NX. This protects only the high kernel mapping (_text -> _etext)
406 * out of which the kernel actually executes. Do not protect the low
409 * This does not cover __inittext since that is gone after boot.
411 static pgprotval_t protect_kernel_text(unsigned long start, unsigned long end)
413 unsigned long t_end = (unsigned long)_etext - 1;
414 unsigned long t_start = (unsigned long)_text;
416 if (overlaps(start, end, t_start, t_end))
421 #if defined(CONFIG_X86_64)
423 * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
424 * kernel text mappings for the large page aligned text, rodata sections
425 * will be always read-only. For the kernel identity mappings covering the
426 * holes caused by this alignment can be anything that user asks.
428 * This will preserve the large page mappings for kernel text/data at no
431 static pgprotval_t protect_kernel_text_ro(unsigned long start,
434 unsigned long t_end = (unsigned long)__end_rodata_hpage_align - 1;
435 unsigned long t_start = (unsigned long)_text;
438 if (!kernel_set_to_readonly || !overlaps(start, end, t_start, t_end))
441 * Don't enforce the !RW mapping for the kernel text mapping, if
442 * the current mapping is already using small page mapping. No
443 * need to work hard to preserve large page mappings in this case.
445 * This also fixes the Linux Xen paravirt guest boot failure caused
446 * by unexpected read-only mappings for kernel identity
447 * mappings. In this paravirt guest case, the kernel text mapping
448 * and the kernel identity mapping share the same page-table pages,
449 * so the protections for kernel text and identity mappings have to
452 if (lookup_address(start, &level) && (level != PG_LEVEL_4K))
457 static pgprotval_t protect_kernel_text_ro(unsigned long start,
464 static inline bool conflicts(pgprot_t prot, pgprotval_t val)
466 return (pgprot_val(prot) & ~val) != pgprot_val(prot);
469 static inline void check_conflict(int warnlvl, pgprot_t prot, pgprotval_t val,
470 unsigned long start, unsigned long end,
471 unsigned long pfn, const char *txt)
473 static const char *lvltxt[] = {
474 [CPA_CONFLICT] = "conflict",
475 [CPA_PROTECT] = "protect",
476 [CPA_DETECT] = "detect",
479 if (warnlvl > cpa_warn_level || !conflicts(prot, val))
482 pr_warn("CPA %8s %10s: 0x%016lx - 0x%016lx PFN %lx req %016llx prevent %016llx\n",
483 lvltxt[warnlvl], txt, start, end, pfn, (unsigned long long)pgprot_val(prot),
484 (unsigned long long)val);
488 * Certain areas of memory on x86 require very specific protection flags,
489 * for example the BIOS area or kernel text. Callers don't always get this
490 * right (again, ioremap() on BIOS memory is not uncommon) so this function
491 * checks and fixes these known static required protection bits.
493 static inline pgprot_t static_protections(pgprot_t prot, unsigned long start,
494 unsigned long pfn, unsigned long npg,
497 pgprotval_t forbidden, res;
501 * There is no point in checking RW/NX conflicts when the requested
502 * mapping is setting the page !PRESENT.
504 if (!(pgprot_val(prot) & _PAGE_PRESENT))
507 /* Operate on the virtual address */
508 end = start + npg * PAGE_SIZE - 1;
510 res = protect_kernel_text(start, end);
511 check_conflict(warnlvl, prot, res, start, end, pfn, "Text NX");
514 res = protect_kernel_text_ro(start, end);
515 check_conflict(warnlvl, prot, res, start, end, pfn, "Text RO");
518 /* Check the PFN directly */
519 res = protect_pci_bios(pfn, pfn + npg - 1);
520 check_conflict(warnlvl, prot, res, start, end, pfn, "PCIBIOS NX");
523 res = protect_rodata(pfn, pfn + npg - 1);
524 check_conflict(warnlvl, prot, res, start, end, pfn, "Rodata RO");
527 return __pgprot(pgprot_val(prot) & ~forbidden);
531 * Lookup the page table entry for a virtual address in a specific pgd.
532 * Return a pointer to the entry and the level of the mapping.
534 pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address,
541 *level = PG_LEVEL_NONE;
546 p4d = p4d_offset(pgd, address);
550 *level = PG_LEVEL_512G;
551 if (p4d_large(*p4d) || !p4d_present(*p4d))
554 pud = pud_offset(p4d, address);
558 *level = PG_LEVEL_1G;
559 if (pud_large(*pud) || !pud_present(*pud))
562 pmd = pmd_offset(pud, address);
566 *level = PG_LEVEL_2M;
567 if (pmd_large(*pmd) || !pmd_present(*pmd))
570 *level = PG_LEVEL_4K;
572 return pte_offset_kernel(pmd, address);
576 * Lookup the page table entry for a virtual address. Return a pointer
577 * to the entry and the level of the mapping.
579 * Note: We return pud and pmd either when the entry is marked large
580 * or when the present bit is not set. Otherwise we would return a
581 * pointer to a nonexisting mapping.
583 pte_t *lookup_address(unsigned long address, unsigned int *level)
585 return lookup_address_in_pgd(pgd_offset_k(address), address, level);
587 EXPORT_SYMBOL_GPL(lookup_address);
589 static pte_t *_lookup_address_cpa(struct cpa_data *cpa, unsigned long address,
593 return lookup_address_in_pgd(cpa->pgd + pgd_index(address),
596 return lookup_address(address, level);
600 * Lookup the PMD entry for a virtual address. Return a pointer to the entry
601 * or NULL if not present.
603 pmd_t *lookup_pmd_address(unsigned long address)
609 pgd = pgd_offset_k(address);
613 p4d = p4d_offset(pgd, address);
614 if (p4d_none(*p4d) || p4d_large(*p4d) || !p4d_present(*p4d))
617 pud = pud_offset(p4d, address);
618 if (pud_none(*pud) || pud_large(*pud) || !pud_present(*pud))
621 return pmd_offset(pud, address);
625 * This is necessary because __pa() does not work on some
626 * kinds of memory, like vmalloc() or the alloc_remap()
627 * areas on 32-bit NUMA systems. The percpu areas can
628 * end up in this kind of memory, for instance.
630 * This could be optimized, but it is only intended to be
631 * used at inititalization time, and keeping it
632 * unoptimized should increase the testing coverage for
633 * the more obscure platforms.
635 phys_addr_t slow_virt_to_phys(void *__virt_addr)
637 unsigned long virt_addr = (unsigned long)__virt_addr;
638 phys_addr_t phys_addr;
639 unsigned long offset;
643 pte = lookup_address(virt_addr, &level);
647 * pXX_pfn() returns unsigned long, which must be cast to phys_addr_t
648 * before being left-shifted PAGE_SHIFT bits -- this trick is to
649 * make 32-PAE kernel work correctly.
653 phys_addr = (phys_addr_t)pud_pfn(*(pud_t *)pte) << PAGE_SHIFT;
654 offset = virt_addr & ~PUD_PAGE_MASK;
657 phys_addr = (phys_addr_t)pmd_pfn(*(pmd_t *)pte) << PAGE_SHIFT;
658 offset = virt_addr & ~PMD_PAGE_MASK;
661 phys_addr = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
662 offset = virt_addr & ~PAGE_MASK;
665 return (phys_addr_t)(phys_addr | offset);
667 EXPORT_SYMBOL_GPL(slow_virt_to_phys);
670 * Set the new pmd in all the pgds we know about:
672 static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
675 set_pte_atomic(kpte, pte);
677 if (!SHARED_KERNEL_PMD) {
680 list_for_each_entry(page, &pgd_list, lru) {
686 pgd = (pgd_t *)page_address(page) + pgd_index(address);
687 p4d = p4d_offset(pgd, address);
688 pud = pud_offset(p4d, address);
689 pmd = pmd_offset(pud, address);
690 set_pte_atomic((pte_t *)pmd, pte);
696 static pgprot_t pgprot_clear_protnone_bits(pgprot_t prot)
699 * _PAGE_GLOBAL means "global page" for present PTEs.
700 * But, it is also used to indicate _PAGE_PROTNONE
701 * for non-present PTEs.
703 * This ensures that a _PAGE_GLOBAL PTE going from
704 * present to non-present is not confused as
707 if (!(pgprot_val(prot) & _PAGE_PRESENT))
708 pgprot_val(prot) &= ~_PAGE_GLOBAL;
713 static int __should_split_large_page(pte_t *kpte, unsigned long address,
714 struct cpa_data *cpa)
716 unsigned long numpages, pmask, psize, lpaddr, pfn, old_pfn;
717 pgprot_t old_prot, new_prot, req_prot, chk_prot;
718 pte_t new_pte, old_pte, *tmp;
722 * Check for races, another CPU might have split this page
725 tmp = _lookup_address_cpa(cpa, address, &level);
731 old_prot = pmd_pgprot(*(pmd_t *)kpte);
732 old_pfn = pmd_pfn(*(pmd_t *)kpte);
733 cpa_inc_2m_checked();
736 old_prot = pud_pgprot(*(pud_t *)kpte);
737 old_pfn = pud_pfn(*(pud_t *)kpte);
738 cpa_inc_1g_checked();
744 psize = page_level_size(level);
745 pmask = page_level_mask(level);
748 * Calculate the number of pages, which fit into this large
749 * page starting at address:
751 lpaddr = (address + psize) & pmask;
752 numpages = (lpaddr - address) >> PAGE_SHIFT;
753 if (numpages < cpa->numpages)
754 cpa->numpages = numpages;
757 * We are safe now. Check whether the new pgprot is the same:
758 * Convert protection attributes to 4k-format, as cpa->mask* are set
762 /* Clear PSE (aka _PAGE_PAT) and move PAT bit to correct position */
763 req_prot = pgprot_large_2_4k(old_prot);
765 pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr);
766 pgprot_val(req_prot) |= pgprot_val(cpa->mask_set);
769 * req_prot is in format of 4k pages. It must be converted to large
770 * page format: the caching mode includes the PAT bit located at
771 * different bit positions in the two formats.
773 req_prot = pgprot_4k_2_large(req_prot);
774 req_prot = pgprot_clear_protnone_bits(req_prot);
775 if (pgprot_val(req_prot) & _PAGE_PRESENT)
776 pgprot_val(req_prot) |= _PAGE_PSE;
779 * old_pfn points to the large page base pfn. So we need to add the
780 * offset of the virtual address:
782 pfn = old_pfn + ((address & (psize - 1)) >> PAGE_SHIFT);
786 * Calculate the large page base address and the number of 4K pages
789 lpaddr = address & pmask;
790 numpages = psize >> PAGE_SHIFT;
793 * Sanity check that the existing mapping is correct versus the static
794 * protections. static_protections() guards against !PRESENT, so no
795 * extra conditional required here.
797 chk_prot = static_protections(old_prot, lpaddr, old_pfn, numpages,
800 if (WARN_ON_ONCE(pgprot_val(chk_prot) != pgprot_val(old_prot))) {
802 * Split the large page and tell the split code to
803 * enforce static protections.
805 cpa->force_static_prot = 1;
810 * Optimization: If the requested pgprot is the same as the current
811 * pgprot, then the large page can be preserved and no updates are
812 * required independent of alignment and length of the requested
813 * range. The above already established that the current pgprot is
814 * correct, which in consequence makes the requested pgprot correct
815 * as well if it is the same. The static protection scan below will
816 * not come to a different conclusion.
818 if (pgprot_val(req_prot) == pgprot_val(old_prot)) {
819 cpa_inc_lp_sameprot(level);
824 * If the requested range does not cover the full page, split it up
826 if (address != lpaddr || cpa->numpages != numpages)
830 * Check whether the requested pgprot is conflicting with a static
831 * protection requirement in the large page.
833 new_prot = static_protections(req_prot, lpaddr, old_pfn, numpages,
837 * If there is a conflict, split the large page.
839 * There used to be a 4k wise evaluation trying really hard to
840 * preserve the large pages, but experimentation has shown, that this
841 * does not help at all. There might be corner cases which would
842 * preserve one large page occasionally, but it's really not worth the
843 * extra code and cycles for the common case.
845 if (pgprot_val(req_prot) != pgprot_val(new_prot))
848 /* All checks passed. Update the large page mapping. */
849 new_pte = pfn_pte(old_pfn, new_prot);
850 __set_pmd_pte(kpte, address, new_pte);
851 cpa->flags |= CPA_FLUSHTLB;
852 cpa_inc_lp_preserved(level);
856 static int should_split_large_page(pte_t *kpte, unsigned long address,
857 struct cpa_data *cpa)
861 if (cpa->force_split)
864 spin_lock(&pgd_lock);
865 do_split = __should_split_large_page(kpte, address, cpa);
866 spin_unlock(&pgd_lock);
871 static void split_set_pte(struct cpa_data *cpa, pte_t *pte, unsigned long pfn,
872 pgprot_t ref_prot, unsigned long address,
875 unsigned int npg = PFN_DOWN(size);
879 * If should_split_large_page() discovered an inconsistent mapping,
880 * remove the invalid protection in the split mapping.
882 if (!cpa->force_static_prot)
885 prot = static_protections(ref_prot, address, pfn, npg, CPA_PROTECT);
887 if (pgprot_val(prot) == pgprot_val(ref_prot))
891 * If this is splitting a PMD, fix it up. PUD splits cannot be
892 * fixed trivially as that would require to rescan the newly
893 * installed PMD mappings after returning from split_large_page()
894 * so an eventual further split can allocate the necessary PTE
895 * pages. Warn for now and revisit it in case this actually
898 if (size == PAGE_SIZE)
901 pr_warn_once("CPA: Cannot fixup static protections for PUD split\n");
903 set_pte(pte, pfn_pte(pfn, ref_prot));
907 __split_large_page(struct cpa_data *cpa, pte_t *kpte, unsigned long address,
910 unsigned long lpaddr, lpinc, ref_pfn, pfn, pfninc = 1;
911 pte_t *pbase = (pte_t *)page_address(base);
912 unsigned int i, level;
916 spin_lock(&pgd_lock);
918 * Check for races, another CPU might have split this page
921 tmp = _lookup_address_cpa(cpa, address, &level);
923 spin_unlock(&pgd_lock);
927 paravirt_alloc_pte(&init_mm, page_to_pfn(base));
931 ref_prot = pmd_pgprot(*(pmd_t *)kpte);
933 * Clear PSE (aka _PAGE_PAT) and move
934 * PAT bit to correct position.
936 ref_prot = pgprot_large_2_4k(ref_prot);
937 ref_pfn = pmd_pfn(*(pmd_t *)kpte);
938 lpaddr = address & PMD_MASK;
943 ref_prot = pud_pgprot(*(pud_t *)kpte);
944 ref_pfn = pud_pfn(*(pud_t *)kpte);
945 pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
946 lpaddr = address & PUD_MASK;
949 * Clear the PSE flags if the PRESENT flag is not set
950 * otherwise pmd_present/pmd_huge will return true
951 * even on a non present pmd.
953 if (!(pgprot_val(ref_prot) & _PAGE_PRESENT))
954 pgprot_val(ref_prot) &= ~_PAGE_PSE;
958 spin_unlock(&pgd_lock);
962 ref_prot = pgprot_clear_protnone_bits(ref_prot);
965 * Get the target pfn from the original entry:
968 for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc, lpaddr += lpinc)
969 split_set_pte(cpa, pbase + i, pfn, ref_prot, lpaddr, lpinc);
971 if (virt_addr_valid(address)) {
972 unsigned long pfn = PFN_DOWN(__pa(address));
974 if (pfn_range_is_mapped(pfn, pfn + 1))
975 split_page_count(level);
979 * Install the new, split up pagetable.
981 * We use the standard kernel pagetable protections for the new
982 * pagetable protections, the actual ptes set above control the
983 * primary protection behavior:
985 __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
988 * Do a global flush tlb after splitting the large page
989 * and before we do the actual change page attribute in the PTE.
991 * Without this, we violate the TLB application note, that says:
992 * "The TLBs may contain both ordinary and large-page
993 * translations for a 4-KByte range of linear addresses. This
994 * may occur if software modifies the paging structures so that
995 * the page size used for the address range changes. If the two
996 * translations differ with respect to page frame or attributes
997 * (e.g., permissions), processor behavior is undefined and may
998 * be implementation-specific."
1000 * We do this global tlb flush inside the cpa_lock, so that we
1001 * don't allow any other cpu, with stale tlb entries change the
1002 * page attribute in parallel, that also falls into the
1003 * just split large page entry.
1006 spin_unlock(&pgd_lock);
1011 static int split_large_page(struct cpa_data *cpa, pte_t *kpte,
1012 unsigned long address)
1016 if (!debug_pagealloc_enabled())
1017 spin_unlock(&cpa_lock);
1018 base = alloc_pages(GFP_KERNEL, 0);
1019 if (!debug_pagealloc_enabled())
1020 spin_lock(&cpa_lock);
1024 if (__split_large_page(cpa, kpte, address, base))
1030 static bool try_to_free_pte_page(pte_t *pte)
1034 for (i = 0; i < PTRS_PER_PTE; i++)
1035 if (!pte_none(pte[i]))
1038 free_page((unsigned long)pte);
1042 static bool try_to_free_pmd_page(pmd_t *pmd)
1046 for (i = 0; i < PTRS_PER_PMD; i++)
1047 if (!pmd_none(pmd[i]))
1050 free_page((unsigned long)pmd);
1054 static bool unmap_pte_range(pmd_t *pmd, unsigned long start, unsigned long end)
1056 pte_t *pte = pte_offset_kernel(pmd, start);
1058 while (start < end) {
1059 set_pte(pte, __pte(0));
1065 if (try_to_free_pte_page((pte_t *)pmd_page_vaddr(*pmd))) {
1072 static void __unmap_pmd_range(pud_t *pud, pmd_t *pmd,
1073 unsigned long start, unsigned long end)
1075 if (unmap_pte_range(pmd, start, end))
1076 if (try_to_free_pmd_page((pmd_t *)pud_page_vaddr(*pud)))
1080 static void unmap_pmd_range(pud_t *pud, unsigned long start, unsigned long end)
1082 pmd_t *pmd = pmd_offset(pud, start);
1085 * Not on a 2MB page boundary?
1087 if (start & (PMD_SIZE - 1)) {
1088 unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
1089 unsigned long pre_end = min_t(unsigned long, end, next_page);
1091 __unmap_pmd_range(pud, pmd, start, pre_end);
1098 * Try to unmap in 2M chunks.
1100 while (end - start >= PMD_SIZE) {
1101 if (pmd_large(*pmd))
1104 __unmap_pmd_range(pud, pmd, start, start + PMD_SIZE);
1114 return __unmap_pmd_range(pud, pmd, start, end);
1117 * Try again to free the PMD page if haven't succeeded above.
1119 if (!pud_none(*pud))
1120 if (try_to_free_pmd_page((pmd_t *)pud_page_vaddr(*pud)))
1124 static void unmap_pud_range(p4d_t *p4d, unsigned long start, unsigned long end)
1126 pud_t *pud = pud_offset(p4d, start);
1129 * Not on a GB page boundary?
1131 if (start & (PUD_SIZE - 1)) {
1132 unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
1133 unsigned long pre_end = min_t(unsigned long, end, next_page);
1135 unmap_pmd_range(pud, start, pre_end);
1142 * Try to unmap in 1G chunks?
1144 while (end - start >= PUD_SIZE) {
1146 if (pud_large(*pud))
1149 unmap_pmd_range(pud, start, start + PUD_SIZE);
1159 unmap_pmd_range(pud, start, end);
1162 * No need to try to free the PUD page because we'll free it in
1163 * populate_pgd's error path
1167 static int alloc_pte_page(pmd_t *pmd)
1169 pte_t *pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
1173 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
1177 static int alloc_pmd_page(pud_t *pud)
1179 pmd_t *pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL);
1183 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
1187 static void populate_pte(struct cpa_data *cpa,
1188 unsigned long start, unsigned long end,
1189 unsigned num_pages, pmd_t *pmd, pgprot_t pgprot)
1193 pte = pte_offset_kernel(pmd, start);
1195 pgprot = pgprot_clear_protnone_bits(pgprot);
1197 while (num_pages-- && start < end) {
1198 set_pte(pte, pfn_pte(cpa->pfn, pgprot));
1206 static long populate_pmd(struct cpa_data *cpa,
1207 unsigned long start, unsigned long end,
1208 unsigned num_pages, pud_t *pud, pgprot_t pgprot)
1212 pgprot_t pmd_pgprot;
1215 * Not on a 2M boundary?
1217 if (start & (PMD_SIZE - 1)) {
1218 unsigned long pre_end = start + (num_pages << PAGE_SHIFT);
1219 unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
1221 pre_end = min_t(unsigned long, pre_end, next_page);
1222 cur_pages = (pre_end - start) >> PAGE_SHIFT;
1223 cur_pages = min_t(unsigned int, num_pages, cur_pages);
1228 pmd = pmd_offset(pud, start);
1230 if (alloc_pte_page(pmd))
1233 populate_pte(cpa, start, pre_end, cur_pages, pmd, pgprot);
1239 * We mapped them all?
1241 if (num_pages == cur_pages)
1244 pmd_pgprot = pgprot_4k_2_large(pgprot);
1246 while (end - start >= PMD_SIZE) {
1249 * We cannot use a 1G page so allocate a PMD page if needed.
1252 if (alloc_pmd_page(pud))
1255 pmd = pmd_offset(pud, start);
1257 set_pmd(pmd, pmd_mkhuge(pfn_pmd(cpa->pfn,
1258 canon_pgprot(pmd_pgprot))));
1261 cpa->pfn += PMD_SIZE >> PAGE_SHIFT;
1262 cur_pages += PMD_SIZE >> PAGE_SHIFT;
1266 * Map trailing 4K pages.
1269 pmd = pmd_offset(pud, start);
1271 if (alloc_pte_page(pmd))
1274 populate_pte(cpa, start, end, num_pages - cur_pages,
1280 static int populate_pud(struct cpa_data *cpa, unsigned long start, p4d_t *p4d,
1286 pgprot_t pud_pgprot;
1288 end = start + (cpa->numpages << PAGE_SHIFT);
1291 * Not on a Gb page boundary? => map everything up to it with
1294 if (start & (PUD_SIZE - 1)) {
1295 unsigned long pre_end;
1296 unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
1298 pre_end = min_t(unsigned long, end, next_page);
1299 cur_pages = (pre_end - start) >> PAGE_SHIFT;
1300 cur_pages = min_t(int, (int)cpa->numpages, cur_pages);
1302 pud = pud_offset(p4d, start);
1308 if (alloc_pmd_page(pud))
1311 cur_pages = populate_pmd(cpa, start, pre_end, cur_pages,
1319 /* We mapped them all? */
1320 if (cpa->numpages == cur_pages)
1323 pud = pud_offset(p4d, start);
1324 pud_pgprot = pgprot_4k_2_large(pgprot);
1327 * Map everything starting from the Gb boundary, possibly with 1G pages
1329 while (boot_cpu_has(X86_FEATURE_GBPAGES) && end - start >= PUD_SIZE) {
1330 set_pud(pud, pud_mkhuge(pfn_pud(cpa->pfn,
1331 canon_pgprot(pud_pgprot))));
1334 cpa->pfn += PUD_SIZE >> PAGE_SHIFT;
1335 cur_pages += PUD_SIZE >> PAGE_SHIFT;
1339 /* Map trailing leftover */
1343 pud = pud_offset(p4d, start);
1345 if (alloc_pmd_page(pud))
1348 tmp = populate_pmd(cpa, start, end, cpa->numpages - cur_pages,
1359 * Restrictions for kernel page table do not necessarily apply when mapping in
1362 static int populate_pgd(struct cpa_data *cpa, unsigned long addr)
1364 pgprot_t pgprot = __pgprot(_KERNPG_TABLE);
1365 pud_t *pud = NULL; /* shut up gcc */
1370 pgd_entry = cpa->pgd + pgd_index(addr);
1372 if (pgd_none(*pgd_entry)) {
1373 p4d = (p4d_t *)get_zeroed_page(GFP_KERNEL);
1377 set_pgd(pgd_entry, __pgd(__pa(p4d) | _KERNPG_TABLE));
1381 * Allocate a PUD page and hand it down for mapping.
1383 p4d = p4d_offset(pgd_entry, addr);
1384 if (p4d_none(*p4d)) {
1385 pud = (pud_t *)get_zeroed_page(GFP_KERNEL);
1389 set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE));
1392 pgprot_val(pgprot) &= ~pgprot_val(cpa->mask_clr);
1393 pgprot_val(pgprot) |= pgprot_val(cpa->mask_set);
1395 ret = populate_pud(cpa, addr, p4d, pgprot);
1398 * Leave the PUD page in place in case some other CPU or thread
1399 * already found it, but remove any useless entries we just
1402 unmap_pud_range(p4d, addr,
1403 addr + (cpa->numpages << PAGE_SHIFT));
1407 cpa->numpages = ret;
1411 static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
1416 * Right now, we only execute this code path when mapping
1417 * the EFI virtual memory map regions, no other users
1418 * provide a ->pgd value. This may change in the future.
1420 return populate_pgd(cpa, vaddr);
1424 * Ignore all non primary paths.
1432 * Ignore the NULL PTE for kernel identity mapping, as it is expected
1434 * Also set numpages to '1' indicating that we processed cpa req for
1435 * one virtual address page and its pfn. TBD: numpages can be set based
1436 * on the initial value and the level returned by lookup_address().
1438 if (within(vaddr, PAGE_OFFSET,
1439 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
1441 cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
1444 } else if (__cpa_pfn_in_highmap(cpa->pfn)) {
1445 /* Faults in the highmap are OK, so do not warn: */
1448 WARN(1, KERN_WARNING "CPA: called for zero pte. "
1449 "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
1456 static int __change_page_attr(struct cpa_data *cpa, int primary)
1458 unsigned long address;
1461 pte_t *kpte, old_pte;
1463 address = __cpa_addr(cpa, cpa->curpage);
1465 kpte = _lookup_address_cpa(cpa, address, &level);
1467 return __cpa_process_fault(cpa, address, primary);
1470 if (pte_none(old_pte))
1471 return __cpa_process_fault(cpa, address, primary);
1473 if (level == PG_LEVEL_4K) {
1475 pgprot_t new_prot = pte_pgprot(old_pte);
1476 unsigned long pfn = pte_pfn(old_pte);
1478 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
1479 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
1481 cpa_inc_4k_install();
1482 new_prot = static_protections(new_prot, address, pfn, 1,
1485 new_prot = pgprot_clear_protnone_bits(new_prot);
1488 * We need to keep the pfn from the existing PTE,
1489 * after all we're only going to change it's attributes
1490 * not the memory it points to
1492 new_pte = pfn_pte(pfn, new_prot);
1495 * Do we really change anything ?
1497 if (pte_val(old_pte) != pte_val(new_pte)) {
1498 set_pte_atomic(kpte, new_pte);
1499 cpa->flags |= CPA_FLUSHTLB;
1506 * Check, whether we can keep the large page intact
1507 * and just change the pte:
1509 do_split = should_split_large_page(kpte, address, cpa);
1511 * When the range fits into the existing large page,
1512 * return. cp->numpages and cpa->tlbflush have been updated in
1519 * We have to split the large page:
1521 err = split_large_page(cpa, kpte, address);
1528 static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
1530 static int cpa_process_alias(struct cpa_data *cpa)
1532 struct cpa_data alias_cpa;
1533 unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
1534 unsigned long vaddr;
1537 if (!pfn_range_is_mapped(cpa->pfn, cpa->pfn + 1))
1541 * No need to redo, when the primary call touched the direct
1544 vaddr = __cpa_addr(cpa, cpa->curpage);
1545 if (!(within(vaddr, PAGE_OFFSET,
1546 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
1549 alias_cpa.vaddr = &laddr;
1550 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
1551 alias_cpa.curpage = 0;
1553 ret = __change_page_attr_set_clr(&alias_cpa, 0);
1558 #ifdef CONFIG_X86_64
1560 * If the primary call didn't touch the high mapping already
1561 * and the physical address is inside the kernel map, we need
1562 * to touch the high mapped kernel as well:
1564 if (!within(vaddr, (unsigned long)_text, _brk_end) &&
1565 __cpa_pfn_in_highmap(cpa->pfn)) {
1566 unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
1567 __START_KERNEL_map - phys_base;
1569 alias_cpa.vaddr = &temp_cpa_vaddr;
1570 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
1571 alias_cpa.curpage = 0;
1574 * The high mapping range is imprecise, so ignore the
1577 __change_page_attr_set_clr(&alias_cpa, 0);
1584 static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
1586 unsigned long numpages = cpa->numpages;
1587 unsigned long rempages = numpages;
1592 * Store the remaining nr of pages for the large page
1593 * preservation check.
1595 cpa->numpages = rempages;
1596 /* for array changes, we can't use large page */
1597 if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY))
1600 if (!debug_pagealloc_enabled())
1601 spin_lock(&cpa_lock);
1602 ret = __change_page_attr(cpa, checkalias);
1603 if (!debug_pagealloc_enabled())
1604 spin_unlock(&cpa_lock);
1609 ret = cpa_process_alias(cpa);
1615 * Adjust the number of pages with the result of the
1616 * CPA operation. Either a large page has been
1617 * preserved or a single page update happened.
1619 BUG_ON(cpa->numpages > rempages || !cpa->numpages);
1620 rempages -= cpa->numpages;
1621 cpa->curpage += cpa->numpages;
1625 /* Restore the original numpages */
1626 cpa->numpages = numpages;
1631 * Machine check recovery code needs to change cache mode of poisoned
1632 * pages to UC to avoid speculative access logging another error. But
1633 * passing the address of the 1:1 mapping to set_memory_uc() is a fine
1634 * way to encourage a speculative access. So we cheat and flip the top
1635 * bit of the address. This works fine for the code that updates the
1636 * page tables. But at the end of the process we need to flush the cache
1637 * and the non-canonical address causes a #GP fault when used by the
1638 * CLFLUSH instruction.
1640 * But in the common case we already have a canonical address. This code
1641 * will fix the top bit if needed and is a no-op otherwise.
1643 static inline unsigned long make_addr_canonical_again(unsigned long addr)
1645 #ifdef CONFIG_X86_64
1646 return (long)(addr << 1) >> 1;
1653 static int change_page_attr_set_clr(unsigned long *addr, int numpages,
1654 pgprot_t mask_set, pgprot_t mask_clr,
1655 int force_split, int in_flag,
1656 struct page **pages)
1658 struct cpa_data cpa;
1659 int ret, cache, checkalias;
1661 memset(&cpa, 0, sizeof(cpa));
1664 * Check, if we are requested to set a not supported
1665 * feature. Clearing non-supported features is OK.
1667 mask_set = canon_pgprot(mask_set);
1669 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
1672 /* Ensure we are PAGE_SIZE aligned */
1673 if (in_flag & CPA_ARRAY) {
1675 for (i = 0; i < numpages; i++) {
1676 if (addr[i] & ~PAGE_MASK) {
1677 addr[i] &= PAGE_MASK;
1681 } else if (!(in_flag & CPA_PAGES_ARRAY)) {
1683 * in_flag of CPA_PAGES_ARRAY implies it is aligned.
1684 * No need to check in that case
1686 if (*addr & ~PAGE_MASK) {
1689 * People should not be passing in unaligned addresses:
1695 /* Must avoid aliasing mappings in the highmem code */
1696 kmap_flush_unused();
1702 cpa.numpages = numpages;
1703 cpa.mask_set = mask_set;
1704 cpa.mask_clr = mask_clr;
1707 cpa.force_split = force_split;
1709 if (in_flag & (CPA_ARRAY | CPA_PAGES_ARRAY))
1710 cpa.flags |= in_flag;
1712 /* No alias checking for _NX bit modifications */
1713 checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
1714 /* Has caller explicitly disabled alias checking? */
1715 if (in_flag & CPA_NO_CHECK_ALIAS)
1718 ret = __change_page_attr_set_clr(&cpa, checkalias);
1721 * Check whether we really changed something:
1723 if (!(cpa.flags & CPA_FLUSHTLB))
1727 * No need to flush, when we did not set any of the caching
1730 cache = !!pgprot2cachemode(mask_set);
1733 * On error; flush everything to be sure.
1736 cpa_flush_all(cache);
1740 cpa_flush(&cpa, cache);
1745 static inline int change_page_attr_set(unsigned long *addr, int numpages,
1746 pgprot_t mask, int array)
1748 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
1749 (array ? CPA_ARRAY : 0), NULL);
1752 static inline int change_page_attr_clear(unsigned long *addr, int numpages,
1753 pgprot_t mask, int array)
1755 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
1756 (array ? CPA_ARRAY : 0), NULL);
1759 static inline int cpa_set_pages_array(struct page **pages, int numpages,
1762 return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0,
1763 CPA_PAGES_ARRAY, pages);
1766 static inline int cpa_clear_pages_array(struct page **pages, int numpages,
1769 return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
1770 CPA_PAGES_ARRAY, pages);
1773 int _set_memory_uc(unsigned long addr, int numpages)
1776 * for now UC MINUS. see comments in ioremap_nocache()
1777 * If you really need strong UC use ioremap_uc(), but note
1778 * that you cannot override IO areas with set_memory_*() as
1779 * these helpers cannot work with IO memory.
1781 return change_page_attr_set(&addr, numpages,
1782 cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS),
1786 int set_memory_uc(unsigned long addr, int numpages)
1791 * for now UC MINUS. see comments in ioremap_nocache()
1793 ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
1794 _PAGE_CACHE_MODE_UC_MINUS, NULL);
1798 ret = _set_memory_uc(addr, numpages);
1805 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1809 EXPORT_SYMBOL(set_memory_uc);
1811 static int _set_memory_array(unsigned long *addr, int numpages,
1812 enum page_cache_mode new_type)
1814 enum page_cache_mode set_type;
1818 for (i = 0; i < numpages; i++) {
1819 ret = reserve_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE,
1825 /* If WC, set to UC- first and then WC */
1826 set_type = (new_type == _PAGE_CACHE_MODE_WC) ?
1827 _PAGE_CACHE_MODE_UC_MINUS : new_type;
1829 ret = change_page_attr_set(addr, numpages,
1830 cachemode2pgprot(set_type), 1);
1832 if (!ret && new_type == _PAGE_CACHE_MODE_WC)
1833 ret = change_page_attr_set_clr(addr, numpages,
1835 _PAGE_CACHE_MODE_WC),
1836 __pgprot(_PAGE_CACHE_MASK),
1837 0, CPA_ARRAY, NULL);
1844 for (j = 0; j < i; j++)
1845 free_memtype(__pa(addr[j]), __pa(addr[j]) + PAGE_SIZE);
1850 int set_memory_array_uc(unsigned long *addr, int numpages)
1852 return _set_memory_array(addr, numpages, _PAGE_CACHE_MODE_UC_MINUS);
1854 EXPORT_SYMBOL(set_memory_array_uc);
1856 int set_memory_array_wc(unsigned long *addr, int numpages)
1858 return _set_memory_array(addr, numpages, _PAGE_CACHE_MODE_WC);
1860 EXPORT_SYMBOL(set_memory_array_wc);
1862 int set_memory_array_wt(unsigned long *addr, int numpages)
1864 return _set_memory_array(addr, numpages, _PAGE_CACHE_MODE_WT);
1866 EXPORT_SYMBOL_GPL(set_memory_array_wt);
1868 int _set_memory_wc(unsigned long addr, int numpages)
1872 ret = change_page_attr_set(&addr, numpages,
1873 cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS),
1876 ret = change_page_attr_set_clr(&addr, numpages,
1877 cachemode2pgprot(_PAGE_CACHE_MODE_WC),
1878 __pgprot(_PAGE_CACHE_MASK),
1884 int set_memory_wc(unsigned long addr, int numpages)
1888 ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
1889 _PAGE_CACHE_MODE_WC, NULL);
1893 ret = _set_memory_wc(addr, numpages);
1895 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1899 EXPORT_SYMBOL(set_memory_wc);
1901 int _set_memory_wt(unsigned long addr, int numpages)
1903 return change_page_attr_set(&addr, numpages,
1904 cachemode2pgprot(_PAGE_CACHE_MODE_WT), 0);
1907 int set_memory_wt(unsigned long addr, int numpages)
1911 ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
1912 _PAGE_CACHE_MODE_WT, NULL);
1916 ret = _set_memory_wt(addr, numpages);
1918 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1922 EXPORT_SYMBOL_GPL(set_memory_wt);
1924 int _set_memory_wb(unsigned long addr, int numpages)
1926 /* WB cache mode is hard wired to all cache attribute bits being 0 */
1927 return change_page_attr_clear(&addr, numpages,
1928 __pgprot(_PAGE_CACHE_MASK), 0);
1931 int set_memory_wb(unsigned long addr, int numpages)
1935 ret = _set_memory_wb(addr, numpages);
1939 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1942 EXPORT_SYMBOL(set_memory_wb);
1944 int set_memory_array_wb(unsigned long *addr, int numpages)
1949 /* WB cache mode is hard wired to all cache attribute bits being 0 */
1950 ret = change_page_attr_clear(addr, numpages,
1951 __pgprot(_PAGE_CACHE_MASK), 1);
1955 for (i = 0; i < numpages; i++)
1956 free_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE);
1960 EXPORT_SYMBOL(set_memory_array_wb);
1962 int set_memory_x(unsigned long addr, int numpages)
1964 if (!(__supported_pte_mask & _PAGE_NX))
1967 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
1969 EXPORT_SYMBOL(set_memory_x);
1971 int set_memory_nx(unsigned long addr, int numpages)
1973 if (!(__supported_pte_mask & _PAGE_NX))
1976 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
1978 EXPORT_SYMBOL(set_memory_nx);
1980 int set_memory_ro(unsigned long addr, int numpages)
1982 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
1985 int set_memory_rw(unsigned long addr, int numpages)
1987 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
1990 int set_memory_np(unsigned long addr, int numpages)
1992 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
1995 int set_memory_np_noalias(unsigned long addr, int numpages)
1997 int cpa_flags = CPA_NO_CHECK_ALIAS;
1999 return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
2000 __pgprot(_PAGE_PRESENT), 0,
2004 int set_memory_4k(unsigned long addr, int numpages)
2006 return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
2007 __pgprot(0), 1, 0, NULL);
2010 int set_memory_nonglobal(unsigned long addr, int numpages)
2012 return change_page_attr_clear(&addr, numpages,
2013 __pgprot(_PAGE_GLOBAL), 0);
2016 int set_memory_global(unsigned long addr, int numpages)
2018 return change_page_attr_set(&addr, numpages,
2019 __pgprot(_PAGE_GLOBAL), 0);
2022 static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc)
2024 struct cpa_data cpa;
2027 /* Nothing to do if memory encryption is not active */
2028 if (!mem_encrypt_active())
2031 /* Should not be working on unaligned addresses */
2032 if (WARN_ONCE(addr & ~PAGE_MASK, "misaligned address: %#lx\n", addr))
2035 memset(&cpa, 0, sizeof(cpa));
2037 cpa.numpages = numpages;
2038 cpa.mask_set = enc ? __pgprot(_PAGE_ENC) : __pgprot(0);
2039 cpa.mask_clr = enc ? __pgprot(0) : __pgprot(_PAGE_ENC);
2040 cpa.pgd = init_mm.pgd;
2042 /* Must avoid aliasing mappings in the highmem code */
2043 kmap_flush_unused();
2047 * Before changing the encryption attribute, we need to flush caches.
2051 ret = __change_page_attr_set_clr(&cpa, 1);
2054 * After changing the encryption attribute, we need to flush TLBs again
2055 * in case any speculative TLB caching occurred (but no need to flush
2056 * caches again). We could just use cpa_flush_all(), but in case TLB
2057 * flushing gets optimized in the cpa_flush() path use the same logic
2065 int set_memory_encrypted(unsigned long addr, int numpages)
2067 return __set_memory_enc_dec(addr, numpages, true);
2069 EXPORT_SYMBOL_GPL(set_memory_encrypted);
2071 int set_memory_decrypted(unsigned long addr, int numpages)
2073 return __set_memory_enc_dec(addr, numpages, false);
2075 EXPORT_SYMBOL_GPL(set_memory_decrypted);
2077 int set_pages_uc(struct page *page, int numpages)
2079 unsigned long addr = (unsigned long)page_address(page);
2081 return set_memory_uc(addr, numpages);
2083 EXPORT_SYMBOL(set_pages_uc);
2085 static int _set_pages_array(struct page **pages, int numpages,
2086 enum page_cache_mode new_type)
2088 unsigned long start;
2090 enum page_cache_mode set_type;
2095 for (i = 0; i < numpages; i++) {
2096 if (PageHighMem(pages[i]))
2098 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
2099 end = start + PAGE_SIZE;
2100 if (reserve_memtype(start, end, new_type, NULL))
2104 /* If WC, set to UC- first and then WC */
2105 set_type = (new_type == _PAGE_CACHE_MODE_WC) ?
2106 _PAGE_CACHE_MODE_UC_MINUS : new_type;
2108 ret = cpa_set_pages_array(pages, numpages,
2109 cachemode2pgprot(set_type));
2110 if (!ret && new_type == _PAGE_CACHE_MODE_WC)
2111 ret = change_page_attr_set_clr(NULL, numpages,
2113 _PAGE_CACHE_MODE_WC),
2114 __pgprot(_PAGE_CACHE_MASK),
2115 0, CPA_PAGES_ARRAY, pages);
2118 return 0; /* Success */
2121 for (i = 0; i < free_idx; i++) {
2122 if (PageHighMem(pages[i]))
2124 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
2125 end = start + PAGE_SIZE;
2126 free_memtype(start, end);
2131 int set_pages_array_uc(struct page **pages, int numpages)
2133 return _set_pages_array(pages, numpages, _PAGE_CACHE_MODE_UC_MINUS);
2135 EXPORT_SYMBOL(set_pages_array_uc);
2137 int set_pages_array_wc(struct page **pages, int numpages)
2139 return _set_pages_array(pages, numpages, _PAGE_CACHE_MODE_WC);
2141 EXPORT_SYMBOL(set_pages_array_wc);
2143 int set_pages_array_wt(struct page **pages, int numpages)
2145 return _set_pages_array(pages, numpages, _PAGE_CACHE_MODE_WT);
2147 EXPORT_SYMBOL_GPL(set_pages_array_wt);
2149 int set_pages_wb(struct page *page, int numpages)
2151 unsigned long addr = (unsigned long)page_address(page);
2153 return set_memory_wb(addr, numpages);
2155 EXPORT_SYMBOL(set_pages_wb);
2157 int set_pages_array_wb(struct page **pages, int numpages)
2160 unsigned long start;
2164 /* WB cache mode is hard wired to all cache attribute bits being 0 */
2165 retval = cpa_clear_pages_array(pages, numpages,
2166 __pgprot(_PAGE_CACHE_MASK));
2170 for (i = 0; i < numpages; i++) {
2171 if (PageHighMem(pages[i]))
2173 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
2174 end = start + PAGE_SIZE;
2175 free_memtype(start, end);
2180 EXPORT_SYMBOL(set_pages_array_wb);
2182 int set_pages_x(struct page *page, int numpages)
2184 unsigned long addr = (unsigned long)page_address(page);
2186 return set_memory_x(addr, numpages);
2188 EXPORT_SYMBOL(set_pages_x);
2190 int set_pages_nx(struct page *page, int numpages)
2192 unsigned long addr = (unsigned long)page_address(page);
2194 return set_memory_nx(addr, numpages);
2196 EXPORT_SYMBOL(set_pages_nx);
2198 int set_pages_ro(struct page *page, int numpages)
2200 unsigned long addr = (unsigned long)page_address(page);
2202 return set_memory_ro(addr, numpages);
2205 int set_pages_rw(struct page *page, int numpages)
2207 unsigned long addr = (unsigned long)page_address(page);
2209 return set_memory_rw(addr, numpages);
2212 #ifdef CONFIG_DEBUG_PAGEALLOC
2214 static int __set_pages_p(struct page *page, int numpages)
2216 unsigned long tempaddr = (unsigned long) page_address(page);
2217 struct cpa_data cpa = { .vaddr = &tempaddr,
2219 .numpages = numpages,
2220 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
2221 .mask_clr = __pgprot(0),
2225 * No alias checking needed for setting present flag. otherwise,
2226 * we may need to break large pages for 64-bit kernel text
2227 * mappings (this adds to complexity if we want to do this from
2228 * atomic context especially). Let's keep it simple!
2230 return __change_page_attr_set_clr(&cpa, 0);
2233 static int __set_pages_np(struct page *page, int numpages)
2235 unsigned long tempaddr = (unsigned long) page_address(page);
2236 struct cpa_data cpa = { .vaddr = &tempaddr,
2238 .numpages = numpages,
2239 .mask_set = __pgprot(0),
2240 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
2244 * No alias checking needed for setting not present flag. otherwise,
2245 * we may need to break large pages for 64-bit kernel text
2246 * mappings (this adds to complexity if we want to do this from
2247 * atomic context especially). Let's keep it simple!
2249 return __change_page_attr_set_clr(&cpa, 0);
2252 void __kernel_map_pages(struct page *page, int numpages, int enable)
2254 if (PageHighMem(page))
2257 debug_check_no_locks_freed(page_address(page),
2258 numpages * PAGE_SIZE);
2262 * The return value is ignored as the calls cannot fail.
2263 * Large pages for identity mappings are not used at boot time
2264 * and hence no memory allocations during large page split.
2267 __set_pages_p(page, numpages);
2269 __set_pages_np(page, numpages);
2272 * We should perform an IPI and flush all tlbs,
2273 * but that can deadlock->flush only current cpu.
2274 * Preemption needs to be disabled around __flush_tlb_all() due to
2275 * CR3 reload in __native_flush_tlb().
2281 arch_flush_lazy_mmu_mode();
2284 #ifdef CONFIG_HIBERNATION
2286 bool kernel_page_present(struct page *page)
2291 if (PageHighMem(page))
2294 pte = lookup_address((unsigned long)page_address(page), &level);
2295 return (pte_val(*pte) & _PAGE_PRESENT);
2298 #endif /* CONFIG_HIBERNATION */
2300 #endif /* CONFIG_DEBUG_PAGEALLOC */
2302 int __init kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address,
2303 unsigned numpages, unsigned long page_flags)
2305 int retval = -EINVAL;
2307 struct cpa_data cpa = {
2311 .numpages = numpages,
2312 .mask_set = __pgprot(0),
2313 .mask_clr = __pgprot(0),
2317 WARN_ONCE(num_online_cpus() > 1, "Don't call after initializing SMP");
2319 if (!(__supported_pte_mask & _PAGE_NX))
2322 if (!(page_flags & _PAGE_NX))
2323 cpa.mask_clr = __pgprot(_PAGE_NX);
2325 if (!(page_flags & _PAGE_RW))
2326 cpa.mask_clr = __pgprot(_PAGE_RW);
2328 if (!(page_flags & _PAGE_ENC))
2329 cpa.mask_clr = pgprot_encrypted(cpa.mask_clr);
2331 cpa.mask_set = __pgprot(_PAGE_PRESENT | page_flags);
2333 retval = __change_page_attr_set_clr(&cpa, 0);
2341 * __flush_tlb_all() flushes mappings only on current CPU and hence this
2342 * function shouldn't be used in an SMP environment. Presently, it's used only
2343 * during boot (way before smp_init()) by EFI subsystem and hence is ok.
2345 int __init kernel_unmap_pages_in_pgd(pgd_t *pgd, unsigned long address,
2346 unsigned long numpages)
2351 * The typical sequence for unmapping is to find a pte through
2352 * lookup_address_in_pgd() (ideally, it should never return NULL because
2353 * the address is already mapped) and change it's protections. As pfn is
2354 * the *target* of a mapping, it's not useful while unmapping.
2356 struct cpa_data cpa = {
2360 .numpages = numpages,
2361 .mask_set = __pgprot(0),
2362 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
2366 WARN_ONCE(num_online_cpus() > 1, "Don't call after initializing SMP");
2368 retval = __change_page_attr_set_clr(&cpa, 0);
2375 * The testcases use internal knowledge of the implementation that shouldn't
2376 * be exposed to the rest of the kernel. Include these directly here.
2378 #ifdef CONFIG_CPA_DEBUG
2379 #include "pageattr-test.c"