1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2002 Andi Kleen, SuSE Labs.
4 * Thanks to Ben LaHaise for precious feedback.
6 #include <linux/highmem.h>
7 #include <linux/memblock.h>
8 #include <linux/sched.h>
10 #include <linux/interrupt.h>
11 #include <linux/seq_file.h>
12 #include <linux/debugfs.h>
13 #include <linux/pfn.h>
14 #include <linux/percpu.h>
15 #include <linux/gfp.h>
16 #include <linux/pci.h>
17 #include <linux/vmalloc.h>
18 #include <linux/libnvdimm.h>
20 #include <asm/e820/api.h>
21 #include <asm/processor.h>
22 #include <asm/tlbflush.h>
23 #include <asm/sections.h>
24 #include <asm/setup.h>
25 #include <linux/uaccess.h>
26 #include <asm/pgalloc.h>
27 #include <asm/proto.h>
28 #include <asm/memtype.h>
29 #include <asm/set_memory.h>
31 #include "../mm_internal.h"
34 * The current flushing context - we pass it instead of 5 arguments:
41 unsigned long numpages;
42 unsigned long curpage;
45 unsigned int force_split : 1,
46 force_static_prot : 1,
57 static const int cpa_warn_level = CPA_PROTECT;
60 * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings)
61 * using cpa_lock. So that we don't allow any other cpu, with stale large tlb
62 * entries change the page attribute in parallel to some other cpu
63 * splitting a large page entry along with changing the attribute.
65 static DEFINE_SPINLOCK(cpa_lock);
67 #define CPA_FLUSHTLB 1
69 #define CPA_PAGES_ARRAY 4
70 #define CPA_NO_CHECK_ALIAS 8 /* Do not search for aliases */
72 static inline pgprot_t cachemode2pgprot(enum page_cache_mode pcm)
74 return __pgprot(cachemode2protval(pcm));
78 static unsigned long direct_pages_count[PG_LEVEL_NUM];
80 void update_page_count(int level, unsigned long pages)
82 /* Protect against CPA */
84 direct_pages_count[level] += pages;
85 spin_unlock(&pgd_lock);
88 static void split_page_count(int level)
90 if (direct_pages_count[level] == 0)
93 direct_pages_count[level]--;
94 direct_pages_count[level - 1] += PTRS_PER_PTE;
97 void arch_report_meminfo(struct seq_file *m)
99 seq_printf(m, "DirectMap4k: %8lu kB\n",
100 direct_pages_count[PG_LEVEL_4K] << 2);
101 #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
102 seq_printf(m, "DirectMap2M: %8lu kB\n",
103 direct_pages_count[PG_LEVEL_2M] << 11);
105 seq_printf(m, "DirectMap4M: %8lu kB\n",
106 direct_pages_count[PG_LEVEL_2M] << 12);
109 seq_printf(m, "DirectMap1G: %8lu kB\n",
110 direct_pages_count[PG_LEVEL_1G] << 20);
113 static inline void split_page_count(int level) { }
116 #ifdef CONFIG_X86_CPA_STATISTICS
118 static unsigned long cpa_1g_checked;
119 static unsigned long cpa_1g_sameprot;
120 static unsigned long cpa_1g_preserved;
121 static unsigned long cpa_2m_checked;
122 static unsigned long cpa_2m_sameprot;
123 static unsigned long cpa_2m_preserved;
124 static unsigned long cpa_4k_install;
126 static inline void cpa_inc_1g_checked(void)
131 static inline void cpa_inc_2m_checked(void)
136 static inline void cpa_inc_4k_install(void)
138 data_race(cpa_4k_install++);
141 static inline void cpa_inc_lp_sameprot(int level)
143 if (level == PG_LEVEL_1G)
149 static inline void cpa_inc_lp_preserved(int level)
151 if (level == PG_LEVEL_1G)
157 static int cpastats_show(struct seq_file *m, void *p)
159 seq_printf(m, "1G pages checked: %16lu\n", cpa_1g_checked);
160 seq_printf(m, "1G pages sameprot: %16lu\n", cpa_1g_sameprot);
161 seq_printf(m, "1G pages preserved: %16lu\n", cpa_1g_preserved);
162 seq_printf(m, "2M pages checked: %16lu\n", cpa_2m_checked);
163 seq_printf(m, "2M pages sameprot: %16lu\n", cpa_2m_sameprot);
164 seq_printf(m, "2M pages preserved: %16lu\n", cpa_2m_preserved);
165 seq_printf(m, "4K pages set-checked: %16lu\n", cpa_4k_install);
169 static int cpastats_open(struct inode *inode, struct file *file)
171 return single_open(file, cpastats_show, NULL);
174 static const struct file_operations cpastats_fops = {
175 .open = cpastats_open,
178 .release = single_release,
181 static int __init cpa_stats_init(void)
183 debugfs_create_file("cpa_stats", S_IRUSR, arch_debugfs_dir, NULL,
187 late_initcall(cpa_stats_init);
189 static inline void cpa_inc_1g_checked(void) { }
190 static inline void cpa_inc_2m_checked(void) { }
191 static inline void cpa_inc_4k_install(void) { }
192 static inline void cpa_inc_lp_sameprot(int level) { }
193 static inline void cpa_inc_lp_preserved(int level) { }
198 within(unsigned long addr, unsigned long start, unsigned long end)
200 return addr >= start && addr < end;
204 within_inclusive(unsigned long addr, unsigned long start, unsigned long end)
206 return addr >= start && addr <= end;
211 static inline unsigned long highmap_start_pfn(void)
213 return __pa_symbol(_text) >> PAGE_SHIFT;
216 static inline unsigned long highmap_end_pfn(void)
218 /* Do not reference physical address outside the kernel. */
219 return __pa_symbol(roundup(_brk_end, PMD_SIZE) - 1) >> PAGE_SHIFT;
222 static bool __cpa_pfn_in_highmap(unsigned long pfn)
225 * Kernel text has an alias mapping at a high address, known
228 return within_inclusive(pfn, highmap_start_pfn(), highmap_end_pfn());
233 static bool __cpa_pfn_in_highmap(unsigned long pfn)
235 /* There is no highmap on 32-bit */
242 * See set_mce_nospec().
244 * Machine check recovery code needs to change cache mode of poisoned pages to
245 * UC to avoid speculative access logging another error. But passing the
246 * address of the 1:1 mapping to set_memory_uc() is a fine way to encourage a
247 * speculative access. So we cheat and flip the top bit of the address. This
248 * works fine for the code that updates the page tables. But at the end of the
249 * process we need to flush the TLB and cache and the non-canonical address
250 * causes a #GP fault when used by the INVLPG and CLFLUSH instructions.
252 * But in the common case we already have a canonical address. This code
253 * will fix the top bit if needed and is a no-op otherwise.
255 static inline unsigned long fix_addr(unsigned long addr)
258 return (long)(addr << 1) >> 1;
264 static unsigned long __cpa_addr(struct cpa_data *cpa, unsigned long idx)
266 if (cpa->flags & CPA_PAGES_ARRAY) {
267 struct page *page = cpa->pages[idx];
269 if (unlikely(PageHighMem(page)))
272 return (unsigned long)page_address(page);
275 if (cpa->flags & CPA_ARRAY)
276 return cpa->vaddr[idx];
278 return *cpa->vaddr + idx * PAGE_SIZE;
285 static void clflush_cache_range_opt(void *vaddr, unsigned int size)
287 const unsigned long clflush_size = boot_cpu_data.x86_clflush_size;
288 void *p = (void *)((unsigned long)vaddr & ~(clflush_size - 1));
289 void *vend = vaddr + size;
294 for (; p < vend; p += clflush_size)
299 * clflush_cache_range - flush a cache range with clflush
300 * @vaddr: virtual start address
301 * @size: number of bytes to flush
303 * CLFLUSHOPT is an unordered instruction which needs fencing with MFENCE or
304 * SFENCE to avoid ordering issues.
306 void clflush_cache_range(void *vaddr, unsigned int size)
309 clflush_cache_range_opt(vaddr, size);
312 EXPORT_SYMBOL_GPL(clflush_cache_range);
314 #ifdef CONFIG_ARCH_HAS_PMEM_API
315 void arch_invalidate_pmem(void *addr, size_t size)
317 clflush_cache_range(addr, size);
319 EXPORT_SYMBOL_GPL(arch_invalidate_pmem);
322 static void __cpa_flush_all(void *arg)
324 unsigned long cache = (unsigned long)arg;
327 * Flush all to work around Errata in early athlons regarding
328 * large page flushing.
332 if (cache && boot_cpu_data.x86 >= 4)
336 static void cpa_flush_all(unsigned long cache)
338 BUG_ON(irqs_disabled() && !early_boot_irqs_disabled);
340 on_each_cpu(__cpa_flush_all, (void *) cache, 1);
343 static void __cpa_flush_tlb(void *data)
345 struct cpa_data *cpa = data;
348 for (i = 0; i < cpa->numpages; i++)
349 flush_tlb_one_kernel(fix_addr(__cpa_addr(cpa, i)));
352 static void cpa_flush(struct cpa_data *data, int cache)
354 struct cpa_data *cpa = data;
357 BUG_ON(irqs_disabled() && !early_boot_irqs_disabled);
359 if (cache && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
360 cpa_flush_all(cache);
364 if (cpa->force_flush_all || cpa->numpages > tlb_single_page_flush_ceiling)
367 on_each_cpu(__cpa_flush_tlb, cpa, 1);
373 for (i = 0; i < cpa->numpages; i++) {
374 unsigned long addr = __cpa_addr(cpa, i);
377 pte_t *pte = lookup_address(addr, &level);
380 * Only flush present addresses:
382 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
383 clflush_cache_range_opt((void *)fix_addr(addr), PAGE_SIZE);
388 static bool overlaps(unsigned long r1_start, unsigned long r1_end,
389 unsigned long r2_start, unsigned long r2_end)
391 return (r1_start <= r2_end && r1_end >= r2_start) ||
392 (r2_start <= r1_end && r2_end >= r1_start);
395 #ifdef CONFIG_PCI_BIOS
397 * The BIOS area between 640k and 1Mb needs to be executable for PCI BIOS
398 * based config access (CONFIG_PCI_GOBIOS) support.
400 #define BIOS_PFN PFN_DOWN(BIOS_BEGIN)
401 #define BIOS_PFN_END PFN_DOWN(BIOS_END - 1)
403 static pgprotval_t protect_pci_bios(unsigned long spfn, unsigned long epfn)
405 if (pcibios_enabled && overlaps(spfn, epfn, BIOS_PFN, BIOS_PFN_END))
410 static pgprotval_t protect_pci_bios(unsigned long spfn, unsigned long epfn)
417 * The .rodata section needs to be read-only. Using the pfn catches all
418 * aliases. This also includes __ro_after_init, so do not enforce until
419 * kernel_set_to_readonly is true.
421 static pgprotval_t protect_rodata(unsigned long spfn, unsigned long epfn)
423 unsigned long epfn_ro, spfn_ro = PFN_DOWN(__pa_symbol(__start_rodata));
426 * Note: __end_rodata is at page aligned and not inclusive, so
427 * subtract 1 to get the last enforced PFN in the rodata area.
429 epfn_ro = PFN_DOWN(__pa_symbol(__end_rodata)) - 1;
431 if (kernel_set_to_readonly && overlaps(spfn, epfn, spfn_ro, epfn_ro))
437 * Protect kernel text against becoming non executable by forbidding
438 * _PAGE_NX. This protects only the high kernel mapping (_text -> _etext)
439 * out of which the kernel actually executes. Do not protect the low
442 * This does not cover __inittext since that is gone after boot.
444 static pgprotval_t protect_kernel_text(unsigned long start, unsigned long end)
446 unsigned long t_end = (unsigned long)_etext - 1;
447 unsigned long t_start = (unsigned long)_text;
449 if (overlaps(start, end, t_start, t_end))
454 #if defined(CONFIG_X86_64)
456 * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
457 * kernel text mappings for the large page aligned text, rodata sections
458 * will be always read-only. For the kernel identity mappings covering the
459 * holes caused by this alignment can be anything that user asks.
461 * This will preserve the large page mappings for kernel text/data at no
464 static pgprotval_t protect_kernel_text_ro(unsigned long start,
467 unsigned long t_end = (unsigned long)__end_rodata_hpage_align - 1;
468 unsigned long t_start = (unsigned long)_text;
471 if (!kernel_set_to_readonly || !overlaps(start, end, t_start, t_end))
474 * Don't enforce the !RW mapping for the kernel text mapping, if
475 * the current mapping is already using small page mapping. No
476 * need to work hard to preserve large page mappings in this case.
478 * This also fixes the Linux Xen paravirt guest boot failure caused
479 * by unexpected read-only mappings for kernel identity
480 * mappings. In this paravirt guest case, the kernel text mapping
481 * and the kernel identity mapping share the same page-table pages,
482 * so the protections for kernel text and identity mappings have to
485 if (lookup_address(start, &level) && (level != PG_LEVEL_4K))
490 static pgprotval_t protect_kernel_text_ro(unsigned long start,
497 static inline bool conflicts(pgprot_t prot, pgprotval_t val)
499 return (pgprot_val(prot) & ~val) != pgprot_val(prot);
502 static inline void check_conflict(int warnlvl, pgprot_t prot, pgprotval_t val,
503 unsigned long start, unsigned long end,
504 unsigned long pfn, const char *txt)
506 static const char *lvltxt[] = {
507 [CPA_CONFLICT] = "conflict",
508 [CPA_PROTECT] = "protect",
509 [CPA_DETECT] = "detect",
512 if (warnlvl > cpa_warn_level || !conflicts(prot, val))
515 pr_warn("CPA %8s %10s: 0x%016lx - 0x%016lx PFN %lx req %016llx prevent %016llx\n",
516 lvltxt[warnlvl], txt, start, end, pfn, (unsigned long long)pgprot_val(prot),
517 (unsigned long long)val);
521 * Certain areas of memory on x86 require very specific protection flags,
522 * for example the BIOS area or kernel text. Callers don't always get this
523 * right (again, ioremap() on BIOS memory is not uncommon) so this function
524 * checks and fixes these known static required protection bits.
526 static inline pgprot_t static_protections(pgprot_t prot, unsigned long start,
527 unsigned long pfn, unsigned long npg,
528 unsigned long lpsize, int warnlvl)
530 pgprotval_t forbidden, res;
534 * There is no point in checking RW/NX conflicts when the requested
535 * mapping is setting the page !PRESENT.
537 if (!(pgprot_val(prot) & _PAGE_PRESENT))
540 /* Operate on the virtual address */
541 end = start + npg * PAGE_SIZE - 1;
543 res = protect_kernel_text(start, end);
544 check_conflict(warnlvl, prot, res, start, end, pfn, "Text NX");
548 * Special case to preserve a large page. If the change spawns the
549 * full large page mapping then there is no point to split it
550 * up. Happens with ftrace and is going to be removed once ftrace
551 * switched to text_poke().
553 if (lpsize != (npg * PAGE_SIZE) || (start & (lpsize - 1))) {
554 res = protect_kernel_text_ro(start, end);
555 check_conflict(warnlvl, prot, res, start, end, pfn, "Text RO");
559 /* Check the PFN directly */
560 res = protect_pci_bios(pfn, pfn + npg - 1);
561 check_conflict(warnlvl, prot, res, start, end, pfn, "PCIBIOS NX");
564 res = protect_rodata(pfn, pfn + npg - 1);
565 check_conflict(warnlvl, prot, res, start, end, pfn, "Rodata RO");
568 return __pgprot(pgprot_val(prot) & ~forbidden);
572 * Lookup the page table entry for a virtual address in a specific pgd.
573 * Return a pointer to the entry and the level of the mapping.
575 pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address,
582 *level = PG_LEVEL_NONE;
587 p4d = p4d_offset(pgd, address);
591 *level = PG_LEVEL_512G;
592 if (p4d_large(*p4d) || !p4d_present(*p4d))
595 pud = pud_offset(p4d, address);
599 *level = PG_LEVEL_1G;
600 if (pud_large(*pud) || !pud_present(*pud))
603 pmd = pmd_offset(pud, address);
607 *level = PG_LEVEL_2M;
608 if (pmd_large(*pmd) || !pmd_present(*pmd))
611 *level = PG_LEVEL_4K;
613 return pte_offset_kernel(pmd, address);
617 * Lookup the page table entry for a virtual address. Return a pointer
618 * to the entry and the level of the mapping.
620 * Note: We return pud and pmd either when the entry is marked large
621 * or when the present bit is not set. Otherwise we would return a
622 * pointer to a nonexisting mapping.
624 pte_t *lookup_address(unsigned long address, unsigned int *level)
626 return lookup_address_in_pgd(pgd_offset_k(address), address, level);
628 EXPORT_SYMBOL_GPL(lookup_address);
631 * Lookup the page table entry for a virtual address in a given mm. Return a
632 * pointer to the entry and the level of the mapping.
634 pte_t *lookup_address_in_mm(struct mm_struct *mm, unsigned long address,
637 return lookup_address_in_pgd(pgd_offset(mm, address), address, level);
639 EXPORT_SYMBOL_GPL(lookup_address_in_mm);
641 static pte_t *_lookup_address_cpa(struct cpa_data *cpa, unsigned long address,
645 return lookup_address_in_pgd(cpa->pgd + pgd_index(address),
648 return lookup_address(address, level);
652 * Lookup the PMD entry for a virtual address. Return a pointer to the entry
653 * or NULL if not present.
655 pmd_t *lookup_pmd_address(unsigned long address)
661 pgd = pgd_offset_k(address);
665 p4d = p4d_offset(pgd, address);
666 if (p4d_none(*p4d) || p4d_large(*p4d) || !p4d_present(*p4d))
669 pud = pud_offset(p4d, address);
670 if (pud_none(*pud) || pud_large(*pud) || !pud_present(*pud))
673 return pmd_offset(pud, address);
677 * This is necessary because __pa() does not work on some
678 * kinds of memory, like vmalloc() or the alloc_remap()
679 * areas on 32-bit NUMA systems. The percpu areas can
680 * end up in this kind of memory, for instance.
682 * This could be optimized, but it is only intended to be
683 * used at inititalization time, and keeping it
684 * unoptimized should increase the testing coverage for
685 * the more obscure platforms.
687 phys_addr_t slow_virt_to_phys(void *__virt_addr)
689 unsigned long virt_addr = (unsigned long)__virt_addr;
690 phys_addr_t phys_addr;
691 unsigned long offset;
695 pte = lookup_address(virt_addr, &level);
699 * pXX_pfn() returns unsigned long, which must be cast to phys_addr_t
700 * before being left-shifted PAGE_SHIFT bits -- this trick is to
701 * make 32-PAE kernel work correctly.
705 phys_addr = (phys_addr_t)pud_pfn(*(pud_t *)pte) << PAGE_SHIFT;
706 offset = virt_addr & ~PUD_PAGE_MASK;
709 phys_addr = (phys_addr_t)pmd_pfn(*(pmd_t *)pte) << PAGE_SHIFT;
710 offset = virt_addr & ~PMD_PAGE_MASK;
713 phys_addr = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
714 offset = virt_addr & ~PAGE_MASK;
717 return (phys_addr_t)(phys_addr | offset);
719 EXPORT_SYMBOL_GPL(slow_virt_to_phys);
722 * Set the new pmd in all the pgds we know about:
724 static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
727 set_pte_atomic(kpte, pte);
729 if (!SHARED_KERNEL_PMD) {
732 list_for_each_entry(page, &pgd_list, lru) {
738 pgd = (pgd_t *)page_address(page) + pgd_index(address);
739 p4d = p4d_offset(pgd, address);
740 pud = pud_offset(p4d, address);
741 pmd = pmd_offset(pud, address);
742 set_pte_atomic((pte_t *)pmd, pte);
748 static pgprot_t pgprot_clear_protnone_bits(pgprot_t prot)
751 * _PAGE_GLOBAL means "global page" for present PTEs.
752 * But, it is also used to indicate _PAGE_PROTNONE
753 * for non-present PTEs.
755 * This ensures that a _PAGE_GLOBAL PTE going from
756 * present to non-present is not confused as
759 if (!(pgprot_val(prot) & _PAGE_PRESENT))
760 pgprot_val(prot) &= ~_PAGE_GLOBAL;
765 static int __should_split_large_page(pte_t *kpte, unsigned long address,
766 struct cpa_data *cpa)
768 unsigned long numpages, pmask, psize, lpaddr, pfn, old_pfn;
769 pgprot_t old_prot, new_prot, req_prot, chk_prot;
774 * Check for races, another CPU might have split this page
777 tmp = _lookup_address_cpa(cpa, address, &level);
783 old_prot = pmd_pgprot(*(pmd_t *)kpte);
784 old_pfn = pmd_pfn(*(pmd_t *)kpte);
785 cpa_inc_2m_checked();
788 old_prot = pud_pgprot(*(pud_t *)kpte);
789 old_pfn = pud_pfn(*(pud_t *)kpte);
790 cpa_inc_1g_checked();
796 psize = page_level_size(level);
797 pmask = page_level_mask(level);
800 * Calculate the number of pages, which fit into this large
801 * page starting at address:
803 lpaddr = (address + psize) & pmask;
804 numpages = (lpaddr - address) >> PAGE_SHIFT;
805 if (numpages < cpa->numpages)
806 cpa->numpages = numpages;
809 * We are safe now. Check whether the new pgprot is the same:
810 * Convert protection attributes to 4k-format, as cpa->mask* are set
814 /* Clear PSE (aka _PAGE_PAT) and move PAT bit to correct position */
815 req_prot = pgprot_large_2_4k(old_prot);
817 pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr);
818 pgprot_val(req_prot) |= pgprot_val(cpa->mask_set);
821 * req_prot is in format of 4k pages. It must be converted to large
822 * page format: the caching mode includes the PAT bit located at
823 * different bit positions in the two formats.
825 req_prot = pgprot_4k_2_large(req_prot);
826 req_prot = pgprot_clear_protnone_bits(req_prot);
827 if (pgprot_val(req_prot) & _PAGE_PRESENT)
828 pgprot_val(req_prot) |= _PAGE_PSE;
831 * old_pfn points to the large page base pfn. So we need to add the
832 * offset of the virtual address:
834 pfn = old_pfn + ((address & (psize - 1)) >> PAGE_SHIFT);
838 * Calculate the large page base address and the number of 4K pages
841 lpaddr = address & pmask;
842 numpages = psize >> PAGE_SHIFT;
845 * Sanity check that the existing mapping is correct versus the static
846 * protections. static_protections() guards against !PRESENT, so no
847 * extra conditional required here.
849 chk_prot = static_protections(old_prot, lpaddr, old_pfn, numpages,
850 psize, CPA_CONFLICT);
852 if (WARN_ON_ONCE(pgprot_val(chk_prot) != pgprot_val(old_prot))) {
854 * Split the large page and tell the split code to
855 * enforce static protections.
857 cpa->force_static_prot = 1;
862 * Optimization: If the requested pgprot is the same as the current
863 * pgprot, then the large page can be preserved and no updates are
864 * required independent of alignment and length of the requested
865 * range. The above already established that the current pgprot is
866 * correct, which in consequence makes the requested pgprot correct
867 * as well if it is the same. The static protection scan below will
868 * not come to a different conclusion.
870 if (pgprot_val(req_prot) == pgprot_val(old_prot)) {
871 cpa_inc_lp_sameprot(level);
876 * If the requested range does not cover the full page, split it up
878 if (address != lpaddr || cpa->numpages != numpages)
882 * Check whether the requested pgprot is conflicting with a static
883 * protection requirement in the large page.
885 new_prot = static_protections(req_prot, lpaddr, old_pfn, numpages,
889 * If there is a conflict, split the large page.
891 * There used to be a 4k wise evaluation trying really hard to
892 * preserve the large pages, but experimentation has shown, that this
893 * does not help at all. There might be corner cases which would
894 * preserve one large page occasionally, but it's really not worth the
895 * extra code and cycles for the common case.
897 if (pgprot_val(req_prot) != pgprot_val(new_prot))
900 /* All checks passed. Update the large page mapping. */
901 new_pte = pfn_pte(old_pfn, new_prot);
902 __set_pmd_pte(kpte, address, new_pte);
903 cpa->flags |= CPA_FLUSHTLB;
904 cpa_inc_lp_preserved(level);
908 static int should_split_large_page(pte_t *kpte, unsigned long address,
909 struct cpa_data *cpa)
913 if (cpa->force_split)
916 spin_lock(&pgd_lock);
917 do_split = __should_split_large_page(kpte, address, cpa);
918 spin_unlock(&pgd_lock);
923 static void split_set_pte(struct cpa_data *cpa, pte_t *pte, unsigned long pfn,
924 pgprot_t ref_prot, unsigned long address,
927 unsigned int npg = PFN_DOWN(size);
931 * If should_split_large_page() discovered an inconsistent mapping,
932 * remove the invalid protection in the split mapping.
934 if (!cpa->force_static_prot)
937 /* Hand in lpsize = 0 to enforce the protection mechanism */
938 prot = static_protections(ref_prot, address, pfn, npg, 0, CPA_PROTECT);
940 if (pgprot_val(prot) == pgprot_val(ref_prot))
944 * If this is splitting a PMD, fix it up. PUD splits cannot be
945 * fixed trivially as that would require to rescan the newly
946 * installed PMD mappings after returning from split_large_page()
947 * so an eventual further split can allocate the necessary PTE
948 * pages. Warn for now and revisit it in case this actually
951 if (size == PAGE_SIZE)
954 pr_warn_once("CPA: Cannot fixup static protections for PUD split\n");
956 set_pte(pte, pfn_pte(pfn, ref_prot));
960 __split_large_page(struct cpa_data *cpa, pte_t *kpte, unsigned long address,
963 unsigned long lpaddr, lpinc, ref_pfn, pfn, pfninc = 1;
964 pte_t *pbase = (pte_t *)page_address(base);
965 unsigned int i, level;
969 spin_lock(&pgd_lock);
971 * Check for races, another CPU might have split this page
974 tmp = _lookup_address_cpa(cpa, address, &level);
976 spin_unlock(&pgd_lock);
980 paravirt_alloc_pte(&init_mm, page_to_pfn(base));
984 ref_prot = pmd_pgprot(*(pmd_t *)kpte);
986 * Clear PSE (aka _PAGE_PAT) and move
987 * PAT bit to correct position.
989 ref_prot = pgprot_large_2_4k(ref_prot);
990 ref_pfn = pmd_pfn(*(pmd_t *)kpte);
991 lpaddr = address & PMD_MASK;
996 ref_prot = pud_pgprot(*(pud_t *)kpte);
997 ref_pfn = pud_pfn(*(pud_t *)kpte);
998 pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
999 lpaddr = address & PUD_MASK;
1002 * Clear the PSE flags if the PRESENT flag is not set
1003 * otherwise pmd_present/pmd_huge will return true
1004 * even on a non present pmd.
1006 if (!(pgprot_val(ref_prot) & _PAGE_PRESENT))
1007 pgprot_val(ref_prot) &= ~_PAGE_PSE;
1011 spin_unlock(&pgd_lock);
1015 ref_prot = pgprot_clear_protnone_bits(ref_prot);
1018 * Get the target pfn from the original entry:
1021 for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc, lpaddr += lpinc)
1022 split_set_pte(cpa, pbase + i, pfn, ref_prot, lpaddr, lpinc);
1024 if (virt_addr_valid(address)) {
1025 unsigned long pfn = PFN_DOWN(__pa(address));
1027 if (pfn_range_is_mapped(pfn, pfn + 1))
1028 split_page_count(level);
1032 * Install the new, split up pagetable.
1034 * We use the standard kernel pagetable protections for the new
1035 * pagetable protections, the actual ptes set above control the
1036 * primary protection behavior:
1038 __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
1041 * Do a global flush tlb after splitting the large page
1042 * and before we do the actual change page attribute in the PTE.
1044 * Without this, we violate the TLB application note, that says:
1045 * "The TLBs may contain both ordinary and large-page
1046 * translations for a 4-KByte range of linear addresses. This
1047 * may occur if software modifies the paging structures so that
1048 * the page size used for the address range changes. If the two
1049 * translations differ with respect to page frame or attributes
1050 * (e.g., permissions), processor behavior is undefined and may
1051 * be implementation-specific."
1053 * We do this global tlb flush inside the cpa_lock, so that we
1054 * don't allow any other cpu, with stale tlb entries change the
1055 * page attribute in parallel, that also falls into the
1056 * just split large page entry.
1059 spin_unlock(&pgd_lock);
1064 static int split_large_page(struct cpa_data *cpa, pte_t *kpte,
1065 unsigned long address)
1069 if (!debug_pagealloc_enabled())
1070 spin_unlock(&cpa_lock);
1071 base = alloc_pages(GFP_KERNEL, 0);
1072 if (!debug_pagealloc_enabled())
1073 spin_lock(&cpa_lock);
1077 if (__split_large_page(cpa, kpte, address, base))
1083 static bool try_to_free_pte_page(pte_t *pte)
1087 for (i = 0; i < PTRS_PER_PTE; i++)
1088 if (!pte_none(pte[i]))
1091 free_page((unsigned long)pte);
1095 static bool try_to_free_pmd_page(pmd_t *pmd)
1099 for (i = 0; i < PTRS_PER_PMD; i++)
1100 if (!pmd_none(pmd[i]))
1103 free_page((unsigned long)pmd);
1107 static bool unmap_pte_range(pmd_t *pmd, unsigned long start, unsigned long end)
1109 pte_t *pte = pte_offset_kernel(pmd, start);
1111 while (start < end) {
1112 set_pte(pte, __pte(0));
1118 if (try_to_free_pte_page((pte_t *)pmd_page_vaddr(*pmd))) {
1125 static void __unmap_pmd_range(pud_t *pud, pmd_t *pmd,
1126 unsigned long start, unsigned long end)
1128 if (unmap_pte_range(pmd, start, end))
1129 if (try_to_free_pmd_page((pmd_t *)pud_page_vaddr(*pud)))
1133 static void unmap_pmd_range(pud_t *pud, unsigned long start, unsigned long end)
1135 pmd_t *pmd = pmd_offset(pud, start);
1138 * Not on a 2MB page boundary?
1140 if (start & (PMD_SIZE - 1)) {
1141 unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
1142 unsigned long pre_end = min_t(unsigned long, end, next_page);
1144 __unmap_pmd_range(pud, pmd, start, pre_end);
1151 * Try to unmap in 2M chunks.
1153 while (end - start >= PMD_SIZE) {
1154 if (pmd_large(*pmd))
1157 __unmap_pmd_range(pud, pmd, start, start + PMD_SIZE);
1167 return __unmap_pmd_range(pud, pmd, start, end);
1170 * Try again to free the PMD page if haven't succeeded above.
1172 if (!pud_none(*pud))
1173 if (try_to_free_pmd_page((pmd_t *)pud_page_vaddr(*pud)))
1177 static void unmap_pud_range(p4d_t *p4d, unsigned long start, unsigned long end)
1179 pud_t *pud = pud_offset(p4d, start);
1182 * Not on a GB page boundary?
1184 if (start & (PUD_SIZE - 1)) {
1185 unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
1186 unsigned long pre_end = min_t(unsigned long, end, next_page);
1188 unmap_pmd_range(pud, start, pre_end);
1195 * Try to unmap in 1G chunks?
1197 while (end - start >= PUD_SIZE) {
1199 if (pud_large(*pud))
1202 unmap_pmd_range(pud, start, start + PUD_SIZE);
1212 unmap_pmd_range(pud, start, end);
1215 * No need to try to free the PUD page because we'll free it in
1216 * populate_pgd's error path
1220 static int alloc_pte_page(pmd_t *pmd)
1222 pte_t *pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
1226 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
1230 static int alloc_pmd_page(pud_t *pud)
1232 pmd_t *pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL);
1236 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
1240 static void populate_pte(struct cpa_data *cpa,
1241 unsigned long start, unsigned long end,
1242 unsigned num_pages, pmd_t *pmd, pgprot_t pgprot)
1246 pte = pte_offset_kernel(pmd, start);
1248 pgprot = pgprot_clear_protnone_bits(pgprot);
1250 while (num_pages-- && start < end) {
1251 set_pte(pte, pfn_pte(cpa->pfn, pgprot));
1259 static long populate_pmd(struct cpa_data *cpa,
1260 unsigned long start, unsigned long end,
1261 unsigned num_pages, pud_t *pud, pgprot_t pgprot)
1265 pgprot_t pmd_pgprot;
1268 * Not on a 2M boundary?
1270 if (start & (PMD_SIZE - 1)) {
1271 unsigned long pre_end = start + (num_pages << PAGE_SHIFT);
1272 unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
1274 pre_end = min_t(unsigned long, pre_end, next_page);
1275 cur_pages = (pre_end - start) >> PAGE_SHIFT;
1276 cur_pages = min_t(unsigned int, num_pages, cur_pages);
1281 pmd = pmd_offset(pud, start);
1283 if (alloc_pte_page(pmd))
1286 populate_pte(cpa, start, pre_end, cur_pages, pmd, pgprot);
1292 * We mapped them all?
1294 if (num_pages == cur_pages)
1297 pmd_pgprot = pgprot_4k_2_large(pgprot);
1299 while (end - start >= PMD_SIZE) {
1302 * We cannot use a 1G page so allocate a PMD page if needed.
1305 if (alloc_pmd_page(pud))
1308 pmd = pmd_offset(pud, start);
1310 set_pmd(pmd, pmd_mkhuge(pfn_pmd(cpa->pfn,
1311 canon_pgprot(pmd_pgprot))));
1314 cpa->pfn += PMD_SIZE >> PAGE_SHIFT;
1315 cur_pages += PMD_SIZE >> PAGE_SHIFT;
1319 * Map trailing 4K pages.
1322 pmd = pmd_offset(pud, start);
1324 if (alloc_pte_page(pmd))
1327 populate_pte(cpa, start, end, num_pages - cur_pages,
1333 static int populate_pud(struct cpa_data *cpa, unsigned long start, p4d_t *p4d,
1339 pgprot_t pud_pgprot;
1341 end = start + (cpa->numpages << PAGE_SHIFT);
1344 * Not on a Gb page boundary? => map everything up to it with
1347 if (start & (PUD_SIZE - 1)) {
1348 unsigned long pre_end;
1349 unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
1351 pre_end = min_t(unsigned long, end, next_page);
1352 cur_pages = (pre_end - start) >> PAGE_SHIFT;
1353 cur_pages = min_t(int, (int)cpa->numpages, cur_pages);
1355 pud = pud_offset(p4d, start);
1361 if (alloc_pmd_page(pud))
1364 cur_pages = populate_pmd(cpa, start, pre_end, cur_pages,
1372 /* We mapped them all? */
1373 if (cpa->numpages == cur_pages)
1376 pud = pud_offset(p4d, start);
1377 pud_pgprot = pgprot_4k_2_large(pgprot);
1380 * Map everything starting from the Gb boundary, possibly with 1G pages
1382 while (boot_cpu_has(X86_FEATURE_GBPAGES) && end - start >= PUD_SIZE) {
1383 set_pud(pud, pud_mkhuge(pfn_pud(cpa->pfn,
1384 canon_pgprot(pud_pgprot))));
1387 cpa->pfn += PUD_SIZE >> PAGE_SHIFT;
1388 cur_pages += PUD_SIZE >> PAGE_SHIFT;
1392 /* Map trailing leftover */
1396 pud = pud_offset(p4d, start);
1398 if (alloc_pmd_page(pud))
1401 tmp = populate_pmd(cpa, start, end, cpa->numpages - cur_pages,
1412 * Restrictions for kernel page table do not necessarily apply when mapping in
1415 static int populate_pgd(struct cpa_data *cpa, unsigned long addr)
1417 pgprot_t pgprot = __pgprot(_KERNPG_TABLE);
1418 pud_t *pud = NULL; /* shut up gcc */
1423 pgd_entry = cpa->pgd + pgd_index(addr);
1425 if (pgd_none(*pgd_entry)) {
1426 p4d = (p4d_t *)get_zeroed_page(GFP_KERNEL);
1430 set_pgd(pgd_entry, __pgd(__pa(p4d) | _KERNPG_TABLE));
1434 * Allocate a PUD page and hand it down for mapping.
1436 p4d = p4d_offset(pgd_entry, addr);
1437 if (p4d_none(*p4d)) {
1438 pud = (pud_t *)get_zeroed_page(GFP_KERNEL);
1442 set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE));
1445 pgprot_val(pgprot) &= ~pgprot_val(cpa->mask_clr);
1446 pgprot_val(pgprot) |= pgprot_val(cpa->mask_set);
1448 ret = populate_pud(cpa, addr, p4d, pgprot);
1451 * Leave the PUD page in place in case some other CPU or thread
1452 * already found it, but remove any useless entries we just
1455 unmap_pud_range(p4d, addr,
1456 addr + (cpa->numpages << PAGE_SHIFT));
1460 cpa->numpages = ret;
1464 static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
1469 * Right now, we only execute this code path when mapping
1470 * the EFI virtual memory map regions, no other users
1471 * provide a ->pgd value. This may change in the future.
1473 return populate_pgd(cpa, vaddr);
1477 * Ignore all non primary paths.
1485 * Ignore the NULL PTE for kernel identity mapping, as it is expected
1487 * Also set numpages to '1' indicating that we processed cpa req for
1488 * one virtual address page and its pfn. TBD: numpages can be set based
1489 * on the initial value and the level returned by lookup_address().
1491 if (within(vaddr, PAGE_OFFSET,
1492 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
1494 cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
1497 } else if (__cpa_pfn_in_highmap(cpa->pfn)) {
1498 /* Faults in the highmap are OK, so do not warn: */
1501 WARN(1, KERN_WARNING "CPA: called for zero pte. "
1502 "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
1509 static int __change_page_attr(struct cpa_data *cpa, int primary)
1511 unsigned long address;
1514 pte_t *kpte, old_pte;
1516 address = __cpa_addr(cpa, cpa->curpage);
1518 kpte = _lookup_address_cpa(cpa, address, &level);
1520 return __cpa_process_fault(cpa, address, primary);
1523 if (pte_none(old_pte))
1524 return __cpa_process_fault(cpa, address, primary);
1526 if (level == PG_LEVEL_4K) {
1528 pgprot_t new_prot = pte_pgprot(old_pte);
1529 unsigned long pfn = pte_pfn(old_pte);
1531 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
1532 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
1534 cpa_inc_4k_install();
1535 /* Hand in lpsize = 0 to enforce the protection mechanism */
1536 new_prot = static_protections(new_prot, address, pfn, 1, 0,
1539 new_prot = pgprot_clear_protnone_bits(new_prot);
1542 * We need to keep the pfn from the existing PTE,
1543 * after all we're only going to change it's attributes
1544 * not the memory it points to
1546 new_pte = pfn_pte(pfn, new_prot);
1549 * Do we really change anything ?
1551 if (pte_val(old_pte) != pte_val(new_pte)) {
1552 set_pte_atomic(kpte, new_pte);
1553 cpa->flags |= CPA_FLUSHTLB;
1560 * Check, whether we can keep the large page intact
1561 * and just change the pte:
1563 do_split = should_split_large_page(kpte, address, cpa);
1565 * When the range fits into the existing large page,
1566 * return. cp->numpages and cpa->tlbflush have been updated in
1573 * We have to split the large page:
1575 err = split_large_page(cpa, kpte, address);
1582 static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
1584 static int cpa_process_alias(struct cpa_data *cpa)
1586 struct cpa_data alias_cpa;
1587 unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
1588 unsigned long vaddr;
1591 if (!pfn_range_is_mapped(cpa->pfn, cpa->pfn + 1))
1595 * No need to redo, when the primary call touched the direct
1598 vaddr = __cpa_addr(cpa, cpa->curpage);
1599 if (!(within(vaddr, PAGE_OFFSET,
1600 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
1603 alias_cpa.vaddr = &laddr;
1604 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
1605 alias_cpa.curpage = 0;
1607 cpa->force_flush_all = 1;
1609 ret = __change_page_attr_set_clr(&alias_cpa, 0);
1614 #ifdef CONFIG_X86_64
1616 * If the primary call didn't touch the high mapping already
1617 * and the physical address is inside the kernel map, we need
1618 * to touch the high mapped kernel as well:
1620 if (!within(vaddr, (unsigned long)_text, _brk_end) &&
1621 __cpa_pfn_in_highmap(cpa->pfn)) {
1622 unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
1623 __START_KERNEL_map - phys_base;
1625 alias_cpa.vaddr = &temp_cpa_vaddr;
1626 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
1627 alias_cpa.curpage = 0;
1629 cpa->force_flush_all = 1;
1631 * The high mapping range is imprecise, so ignore the
1634 __change_page_attr_set_clr(&alias_cpa, 0);
1641 static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
1643 unsigned long numpages = cpa->numpages;
1644 unsigned long rempages = numpages;
1649 * Store the remaining nr of pages for the large page
1650 * preservation check.
1652 cpa->numpages = rempages;
1653 /* for array changes, we can't use large page */
1654 if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY))
1657 if (!debug_pagealloc_enabled())
1658 spin_lock(&cpa_lock);
1659 ret = __change_page_attr(cpa, checkalias);
1660 if (!debug_pagealloc_enabled())
1661 spin_unlock(&cpa_lock);
1666 ret = cpa_process_alias(cpa);
1672 * Adjust the number of pages with the result of the
1673 * CPA operation. Either a large page has been
1674 * preserved or a single page update happened.
1676 BUG_ON(cpa->numpages > rempages || !cpa->numpages);
1677 rempages -= cpa->numpages;
1678 cpa->curpage += cpa->numpages;
1682 /* Restore the original numpages */
1683 cpa->numpages = numpages;
1687 static int change_page_attr_set_clr(unsigned long *addr, int numpages,
1688 pgprot_t mask_set, pgprot_t mask_clr,
1689 int force_split, int in_flag,
1690 struct page **pages)
1692 struct cpa_data cpa;
1693 int ret, cache, checkalias;
1695 memset(&cpa, 0, sizeof(cpa));
1698 * Check, if we are requested to set a not supported
1699 * feature. Clearing non-supported features is OK.
1701 mask_set = canon_pgprot(mask_set);
1703 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
1706 /* Ensure we are PAGE_SIZE aligned */
1707 if (in_flag & CPA_ARRAY) {
1709 for (i = 0; i < numpages; i++) {
1710 if (addr[i] & ~PAGE_MASK) {
1711 addr[i] &= PAGE_MASK;
1715 } else if (!(in_flag & CPA_PAGES_ARRAY)) {
1717 * in_flag of CPA_PAGES_ARRAY implies it is aligned.
1718 * No need to check in that case
1720 if (*addr & ~PAGE_MASK) {
1723 * People should not be passing in unaligned addresses:
1729 /* Must avoid aliasing mappings in the highmem code */
1730 kmap_flush_unused();
1736 cpa.numpages = numpages;
1737 cpa.mask_set = mask_set;
1738 cpa.mask_clr = mask_clr;
1741 cpa.force_split = force_split;
1743 if (in_flag & (CPA_ARRAY | CPA_PAGES_ARRAY))
1744 cpa.flags |= in_flag;
1746 /* No alias checking for _NX bit modifications */
1747 checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
1748 /* Has caller explicitly disabled alias checking? */
1749 if (in_flag & CPA_NO_CHECK_ALIAS)
1752 ret = __change_page_attr_set_clr(&cpa, checkalias);
1755 * Check whether we really changed something:
1757 if (!(cpa.flags & CPA_FLUSHTLB))
1761 * No need to flush, when we did not set any of the caching
1764 cache = !!pgprot2cachemode(mask_set);
1767 * On error; flush everything to be sure.
1770 cpa_flush_all(cache);
1774 cpa_flush(&cpa, cache);
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,
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,
1803 return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
1804 CPA_PAGES_ARRAY, pages);
1808 * _set_memory_prot is an internal helper for callers that have been passed
1809 * a pgprot_t value from upper layers and a reservation has already been taken.
1810 * If you want to set the pgprot to a specific page protocol, use the
1811 * set_memory_xx() functions.
1813 int __set_memory_prot(unsigned long addr, int numpages, pgprot_t prot)
1815 return change_page_attr_set_clr(&addr, numpages, prot,
1816 __pgprot(~pgprot_val(prot)), 0, 0,
1820 int _set_memory_uc(unsigned long addr, int numpages)
1823 * for now UC MINUS. see comments in ioremap()
1824 * If you really need strong UC use ioremap_uc(), but note
1825 * that you cannot override IO areas with set_memory_*() as
1826 * these helpers cannot work with IO memory.
1828 return change_page_attr_set(&addr, numpages,
1829 cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS),
1833 int set_memory_uc(unsigned long addr, int numpages)
1838 * for now UC MINUS. see comments in ioremap()
1840 ret = memtype_reserve(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
1841 _PAGE_CACHE_MODE_UC_MINUS, NULL);
1845 ret = _set_memory_uc(addr, numpages);
1852 memtype_free(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1856 EXPORT_SYMBOL(set_memory_uc);
1858 int _set_memory_wc(unsigned long addr, int numpages)
1862 ret = change_page_attr_set(&addr, numpages,
1863 cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS),
1866 ret = change_page_attr_set_clr(&addr, numpages,
1867 cachemode2pgprot(_PAGE_CACHE_MODE_WC),
1868 __pgprot(_PAGE_CACHE_MASK),
1874 int set_memory_wc(unsigned long addr, int numpages)
1878 ret = memtype_reserve(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
1879 _PAGE_CACHE_MODE_WC, NULL);
1883 ret = _set_memory_wc(addr, numpages);
1885 memtype_free(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1889 EXPORT_SYMBOL(set_memory_wc);
1891 int _set_memory_wt(unsigned long addr, int numpages)
1893 return change_page_attr_set(&addr, numpages,
1894 cachemode2pgprot(_PAGE_CACHE_MODE_WT), 0);
1897 int _set_memory_wb(unsigned long addr, int numpages)
1899 /* WB cache mode is hard wired to all cache attribute bits being 0 */
1900 return change_page_attr_clear(&addr, numpages,
1901 __pgprot(_PAGE_CACHE_MASK), 0);
1904 int set_memory_wb(unsigned long addr, int numpages)
1908 ret = _set_memory_wb(addr, numpages);
1912 memtype_free(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1915 EXPORT_SYMBOL(set_memory_wb);
1917 int set_memory_x(unsigned long addr, int numpages)
1919 if (!(__supported_pte_mask & _PAGE_NX))
1922 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
1925 int set_memory_nx(unsigned long addr, int numpages)
1927 if (!(__supported_pte_mask & _PAGE_NX))
1930 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
1933 int set_memory_ro(unsigned long addr, int numpages)
1935 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
1938 int set_memory_rw(unsigned long addr, int numpages)
1940 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
1943 int set_memory_np(unsigned long addr, int numpages)
1945 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
1948 int set_memory_np_noalias(unsigned long addr, int numpages)
1950 int cpa_flags = CPA_NO_CHECK_ALIAS;
1952 return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
1953 __pgprot(_PAGE_PRESENT), 0,
1957 int set_memory_4k(unsigned long addr, int numpages)
1959 return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
1960 __pgprot(0), 1, 0, NULL);
1963 int set_memory_nonglobal(unsigned long addr, int numpages)
1965 return change_page_attr_clear(&addr, numpages,
1966 __pgprot(_PAGE_GLOBAL), 0);
1969 int set_memory_global(unsigned long addr, int numpages)
1971 return change_page_attr_set(&addr, numpages,
1972 __pgprot(_PAGE_GLOBAL), 0);
1975 static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc)
1977 struct cpa_data cpa;
1980 /* Nothing to do if memory encryption is not active */
1981 if (!mem_encrypt_active())
1984 /* Should not be working on unaligned addresses */
1985 if (WARN_ONCE(addr & ~PAGE_MASK, "misaligned address: %#lx\n", addr))
1988 memset(&cpa, 0, sizeof(cpa));
1990 cpa.numpages = numpages;
1991 cpa.mask_set = enc ? __pgprot(_PAGE_ENC) : __pgprot(0);
1992 cpa.mask_clr = enc ? __pgprot(0) : __pgprot(_PAGE_ENC);
1993 cpa.pgd = init_mm.pgd;
1995 /* Must avoid aliasing mappings in the highmem code */
1996 kmap_flush_unused();
2000 * Before changing the encryption attribute, we need to flush caches.
2002 cpa_flush(&cpa, !this_cpu_has(X86_FEATURE_SME_COHERENT));
2004 ret = __change_page_attr_set_clr(&cpa, 1);
2007 * After changing the encryption attribute, we need to flush TLBs again
2008 * in case any speculative TLB caching occurred (but no need to flush
2009 * caches again). We could just use cpa_flush_all(), but in case TLB
2010 * flushing gets optimized in the cpa_flush() path use the same logic
2018 int set_memory_encrypted(unsigned long addr, int numpages)
2020 return __set_memory_enc_dec(addr, numpages, true);
2022 EXPORT_SYMBOL_GPL(set_memory_encrypted);
2024 int set_memory_decrypted(unsigned long addr, int numpages)
2026 return __set_memory_enc_dec(addr, numpages, false);
2028 EXPORT_SYMBOL_GPL(set_memory_decrypted);
2030 int set_pages_uc(struct page *page, int numpages)
2032 unsigned long addr = (unsigned long)page_address(page);
2034 return set_memory_uc(addr, numpages);
2036 EXPORT_SYMBOL(set_pages_uc);
2038 static int _set_pages_array(struct page **pages, int numpages,
2039 enum page_cache_mode new_type)
2041 unsigned long start;
2043 enum page_cache_mode set_type;
2048 for (i = 0; i < numpages; i++) {
2049 if (PageHighMem(pages[i]))
2051 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
2052 end = start + PAGE_SIZE;
2053 if (memtype_reserve(start, end, new_type, NULL))
2057 /* If WC, set to UC- first and then WC */
2058 set_type = (new_type == _PAGE_CACHE_MODE_WC) ?
2059 _PAGE_CACHE_MODE_UC_MINUS : new_type;
2061 ret = cpa_set_pages_array(pages, numpages,
2062 cachemode2pgprot(set_type));
2063 if (!ret && new_type == _PAGE_CACHE_MODE_WC)
2064 ret = change_page_attr_set_clr(NULL, numpages,
2066 _PAGE_CACHE_MODE_WC),
2067 __pgprot(_PAGE_CACHE_MASK),
2068 0, CPA_PAGES_ARRAY, pages);
2071 return 0; /* Success */
2074 for (i = 0; i < free_idx; i++) {
2075 if (PageHighMem(pages[i]))
2077 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
2078 end = start + PAGE_SIZE;
2079 memtype_free(start, end);
2084 int set_pages_array_uc(struct page **pages, int numpages)
2086 return _set_pages_array(pages, numpages, _PAGE_CACHE_MODE_UC_MINUS);
2088 EXPORT_SYMBOL(set_pages_array_uc);
2090 int set_pages_array_wc(struct page **pages, int numpages)
2092 return _set_pages_array(pages, numpages, _PAGE_CACHE_MODE_WC);
2094 EXPORT_SYMBOL(set_pages_array_wc);
2096 int set_pages_array_wt(struct page **pages, int numpages)
2098 return _set_pages_array(pages, numpages, _PAGE_CACHE_MODE_WT);
2100 EXPORT_SYMBOL_GPL(set_pages_array_wt);
2102 int set_pages_wb(struct page *page, int numpages)
2104 unsigned long addr = (unsigned long)page_address(page);
2106 return set_memory_wb(addr, numpages);
2108 EXPORT_SYMBOL(set_pages_wb);
2110 int set_pages_array_wb(struct page **pages, int numpages)
2113 unsigned long start;
2117 /* WB cache mode is hard wired to all cache attribute bits being 0 */
2118 retval = cpa_clear_pages_array(pages, numpages,
2119 __pgprot(_PAGE_CACHE_MASK));
2123 for (i = 0; i < numpages; i++) {
2124 if (PageHighMem(pages[i]))
2126 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
2127 end = start + PAGE_SIZE;
2128 memtype_free(start, end);
2133 EXPORT_SYMBOL(set_pages_array_wb);
2135 int set_pages_ro(struct page *page, int numpages)
2137 unsigned long addr = (unsigned long)page_address(page);
2139 return set_memory_ro(addr, numpages);
2142 int set_pages_rw(struct page *page, int numpages)
2144 unsigned long addr = (unsigned long)page_address(page);
2146 return set_memory_rw(addr, numpages);
2149 static int __set_pages_p(struct page *page, int numpages)
2151 unsigned long tempaddr = (unsigned long) page_address(page);
2152 struct cpa_data cpa = { .vaddr = &tempaddr,
2154 .numpages = numpages,
2155 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
2156 .mask_clr = __pgprot(0),
2160 * No alias checking needed for setting present flag. otherwise,
2161 * we may need to break large pages for 64-bit kernel text
2162 * mappings (this adds to complexity if we want to do this from
2163 * atomic context especially). Let's keep it simple!
2165 return __change_page_attr_set_clr(&cpa, 0);
2168 static int __set_pages_np(struct page *page, int numpages)
2170 unsigned long tempaddr = (unsigned long) page_address(page);
2171 struct cpa_data cpa = { .vaddr = &tempaddr,
2173 .numpages = numpages,
2174 .mask_set = __pgprot(0),
2175 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
2179 * No alias checking needed for setting not present flag. otherwise,
2180 * we may need to break large pages for 64-bit kernel text
2181 * mappings (this adds to complexity if we want to do this from
2182 * atomic context especially). Let's keep it simple!
2184 return __change_page_attr_set_clr(&cpa, 0);
2187 int set_direct_map_invalid_noflush(struct page *page)
2189 return __set_pages_np(page, 1);
2192 int set_direct_map_default_noflush(struct page *page)
2194 return __set_pages_p(page, 1);
2197 #ifdef CONFIG_DEBUG_PAGEALLOC
2198 void __kernel_map_pages(struct page *page, int numpages, int enable)
2200 if (PageHighMem(page))
2203 debug_check_no_locks_freed(page_address(page),
2204 numpages * PAGE_SIZE);
2208 * The return value is ignored as the calls cannot fail.
2209 * Large pages for identity mappings are not used at boot time
2210 * and hence no memory allocations during large page split.
2213 __set_pages_p(page, numpages);
2215 __set_pages_np(page, numpages);
2218 * We should perform an IPI and flush all tlbs,
2219 * but that can deadlock->flush only current cpu.
2220 * Preemption needs to be disabled around __flush_tlb_all() due to
2221 * CR3 reload in __native_flush_tlb().
2227 arch_flush_lazy_mmu_mode();
2230 #ifdef CONFIG_HIBERNATION
2231 bool kernel_page_present(struct page *page)
2236 if (PageHighMem(page))
2239 pte = lookup_address((unsigned long)page_address(page), &level);
2240 return (pte_val(*pte) & _PAGE_PRESENT);
2242 #endif /* CONFIG_HIBERNATION */
2243 #endif /* CONFIG_DEBUG_PAGEALLOC */
2245 int __init kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address,
2246 unsigned numpages, unsigned long page_flags)
2248 int retval = -EINVAL;
2250 struct cpa_data cpa = {
2254 .numpages = numpages,
2255 .mask_set = __pgprot(0),
2256 .mask_clr = __pgprot(~page_flags & (_PAGE_NX|_PAGE_RW)),
2260 WARN_ONCE(num_online_cpus() > 1, "Don't call after initializing SMP");
2262 if (!(__supported_pte_mask & _PAGE_NX))
2265 if (!(page_flags & _PAGE_ENC))
2266 cpa.mask_clr = pgprot_encrypted(cpa.mask_clr);
2268 cpa.mask_set = __pgprot(_PAGE_PRESENT | page_flags);
2270 retval = __change_page_attr_set_clr(&cpa, 0);
2278 * __flush_tlb_all() flushes mappings only on current CPU and hence this
2279 * function shouldn't be used in an SMP environment. Presently, it's used only
2280 * during boot (way before smp_init()) by EFI subsystem and hence is ok.
2282 int __init kernel_unmap_pages_in_pgd(pgd_t *pgd, unsigned long address,
2283 unsigned long numpages)
2288 * The typical sequence for unmapping is to find a pte through
2289 * lookup_address_in_pgd() (ideally, it should never return NULL because
2290 * the address is already mapped) and change it's protections. As pfn is
2291 * the *target* of a mapping, it's not useful while unmapping.
2293 struct cpa_data cpa = {
2297 .numpages = numpages,
2298 .mask_set = __pgprot(0),
2299 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
2303 WARN_ONCE(num_online_cpus() > 1, "Don't call after initializing SMP");
2305 retval = __change_page_attr_set_clr(&cpa, 0);
2312 * The testcases use internal knowledge of the implementation that shouldn't
2313 * be exposed to the rest of the kernel. Include these directly here.
2315 #ifdef CONFIG_CPA_DEBUG
2316 #include "cpa-test.c"