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/proc_fs.h>
13 #include <linux/debugfs.h>
14 #include <linux/pfn.h>
15 #include <linux/percpu.h>
16 #include <linux/gfp.h>
17 #include <linux/pci.h>
18 #include <linux/vmalloc.h>
19 #include <linux/libnvdimm.h>
20 #include <linux/vmstat.h>
21 #include <linux/kernel.h>
22 #include <linux/cc_platform.h>
23 #include <linux/set_memory.h>
24 #include <linux/memregion.h>
26 #include <asm/e820/api.h>
27 #include <asm/processor.h>
28 #include <asm/tlbflush.h>
29 #include <asm/sections.h>
30 #include <asm/setup.h>
31 #include <linux/uaccess.h>
32 #include <asm/pgalloc.h>
33 #include <asm/proto.h>
34 #include <asm/memtype.h>
35 #include <asm/hyperv-tlfs.h>
36 #include <asm/mshyperv.h>
38 #include "../mm_internal.h"
41 * The current flushing context - we pass it instead of 5 arguments:
48 unsigned long numpages;
49 unsigned long curpage;
52 unsigned int force_split : 1,
53 force_static_prot : 1,
64 static const int cpa_warn_level = CPA_PROTECT;
67 * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings)
68 * using cpa_lock. So that we don't allow any other cpu, with stale large tlb
69 * entries change the page attribute in parallel to some other cpu
70 * splitting a large page entry along with changing the attribute.
72 static DEFINE_SPINLOCK(cpa_lock);
74 #define CPA_FLUSHTLB 1
76 #define CPA_PAGES_ARRAY 4
77 #define CPA_NO_CHECK_ALIAS 8 /* Do not search for aliases */
79 static inline pgprot_t cachemode2pgprot(enum page_cache_mode pcm)
81 return __pgprot(cachemode2protval(pcm));
85 static unsigned long direct_pages_count[PG_LEVEL_NUM];
87 void update_page_count(int level, unsigned long pages)
89 /* Protect against CPA */
91 direct_pages_count[level] += pages;
92 spin_unlock(&pgd_lock);
95 static void split_page_count(int level)
97 if (direct_pages_count[level] == 0)
100 direct_pages_count[level]--;
101 if (system_state == SYSTEM_RUNNING) {
102 if (level == PG_LEVEL_2M)
103 count_vm_event(DIRECT_MAP_LEVEL2_SPLIT);
104 else if (level == PG_LEVEL_1G)
105 count_vm_event(DIRECT_MAP_LEVEL3_SPLIT);
107 direct_pages_count[level - 1] += PTRS_PER_PTE;
110 void arch_report_meminfo(struct seq_file *m)
112 seq_printf(m, "DirectMap4k: %8lu kB\n",
113 direct_pages_count[PG_LEVEL_4K] << 2);
114 #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
115 seq_printf(m, "DirectMap2M: %8lu kB\n",
116 direct_pages_count[PG_LEVEL_2M] << 11);
118 seq_printf(m, "DirectMap4M: %8lu kB\n",
119 direct_pages_count[PG_LEVEL_2M] << 12);
122 seq_printf(m, "DirectMap1G: %8lu kB\n",
123 direct_pages_count[PG_LEVEL_1G] << 20);
126 static inline void split_page_count(int level) { }
129 #ifdef CONFIG_X86_CPA_STATISTICS
131 static unsigned long cpa_1g_checked;
132 static unsigned long cpa_1g_sameprot;
133 static unsigned long cpa_1g_preserved;
134 static unsigned long cpa_2m_checked;
135 static unsigned long cpa_2m_sameprot;
136 static unsigned long cpa_2m_preserved;
137 static unsigned long cpa_4k_install;
139 static inline void cpa_inc_1g_checked(void)
144 static inline void cpa_inc_2m_checked(void)
149 static inline void cpa_inc_4k_install(void)
151 data_race(cpa_4k_install++);
154 static inline void cpa_inc_lp_sameprot(int level)
156 if (level == PG_LEVEL_1G)
162 static inline void cpa_inc_lp_preserved(int level)
164 if (level == PG_LEVEL_1G)
170 static int cpastats_show(struct seq_file *m, void *p)
172 seq_printf(m, "1G pages checked: %16lu\n", cpa_1g_checked);
173 seq_printf(m, "1G pages sameprot: %16lu\n", cpa_1g_sameprot);
174 seq_printf(m, "1G pages preserved: %16lu\n", cpa_1g_preserved);
175 seq_printf(m, "2M pages checked: %16lu\n", cpa_2m_checked);
176 seq_printf(m, "2M pages sameprot: %16lu\n", cpa_2m_sameprot);
177 seq_printf(m, "2M pages preserved: %16lu\n", cpa_2m_preserved);
178 seq_printf(m, "4K pages set-checked: %16lu\n", cpa_4k_install);
182 static int cpastats_open(struct inode *inode, struct file *file)
184 return single_open(file, cpastats_show, NULL);
187 static const struct file_operations cpastats_fops = {
188 .open = cpastats_open,
191 .release = single_release,
194 static int __init cpa_stats_init(void)
196 debugfs_create_file("cpa_stats", S_IRUSR, arch_debugfs_dir, NULL,
200 late_initcall(cpa_stats_init);
202 static inline void cpa_inc_1g_checked(void) { }
203 static inline void cpa_inc_2m_checked(void) { }
204 static inline void cpa_inc_4k_install(void) { }
205 static inline void cpa_inc_lp_sameprot(int level) { }
206 static inline void cpa_inc_lp_preserved(int level) { }
211 within(unsigned long addr, unsigned long start, unsigned long end)
213 return addr >= start && addr < end;
217 within_inclusive(unsigned long addr, unsigned long start, unsigned long end)
219 return addr >= start && addr <= end;
225 * The kernel image is mapped into two places in the virtual address space
226 * (addresses without KASLR, of course):
228 * 1. The kernel direct map (0xffff880000000000)
229 * 2. The "high kernel map" (0xffffffff81000000)
231 * We actually execute out of #2. If we get the address of a kernel symbol, it
232 * points to #2, but almost all physical-to-virtual translations point to #1.
234 * This is so that we can have both a directmap of all physical memory *and*
235 * take full advantage of the limited (s32) immediate addressing range (2G)
238 * See Documentation/arch/x86/x86_64/mm.rst for more detail.
241 static inline unsigned long highmap_start_pfn(void)
243 return __pa_symbol(_text) >> PAGE_SHIFT;
246 static inline unsigned long highmap_end_pfn(void)
248 /* Do not reference physical address outside the kernel. */
249 return __pa_symbol(roundup(_brk_end, PMD_SIZE) - 1) >> PAGE_SHIFT;
252 static bool __cpa_pfn_in_highmap(unsigned long pfn)
255 * Kernel text has an alias mapping at a high address, known
258 return within_inclusive(pfn, highmap_start_pfn(), highmap_end_pfn());
263 static bool __cpa_pfn_in_highmap(unsigned long pfn)
265 /* There is no highmap on 32-bit */
272 * See set_mce_nospec().
274 * Machine check recovery code needs to change cache mode of poisoned pages to
275 * UC to avoid speculative access logging another error. But passing the
276 * address of the 1:1 mapping to set_memory_uc() is a fine way to encourage a
277 * speculative access. So we cheat and flip the top bit of the address. This
278 * works fine for the code that updates the page tables. But at the end of the
279 * process we need to flush the TLB and cache and the non-canonical address
280 * causes a #GP fault when used by the INVLPG and CLFLUSH instructions.
282 * But in the common case we already have a canonical address. This code
283 * will fix the top bit if needed and is a no-op otherwise.
285 static inline unsigned long fix_addr(unsigned long addr)
288 return (long)(addr << 1) >> 1;
294 static unsigned long __cpa_addr(struct cpa_data *cpa, unsigned long idx)
296 if (cpa->flags & CPA_PAGES_ARRAY) {
297 struct page *page = cpa->pages[idx];
299 if (unlikely(PageHighMem(page)))
302 return (unsigned long)page_address(page);
305 if (cpa->flags & CPA_ARRAY)
306 return cpa->vaddr[idx];
308 return *cpa->vaddr + idx * PAGE_SIZE;
315 static void clflush_cache_range_opt(void *vaddr, unsigned int size)
317 const unsigned long clflush_size = boot_cpu_data.x86_clflush_size;
318 void *p = (void *)((unsigned long)vaddr & ~(clflush_size - 1));
319 void *vend = vaddr + size;
324 for (; p < vend; p += clflush_size)
329 * clflush_cache_range - flush a cache range with clflush
330 * @vaddr: virtual start address
331 * @size: number of bytes to flush
333 * CLFLUSHOPT is an unordered instruction which needs fencing with MFENCE or
334 * SFENCE to avoid ordering issues.
336 void clflush_cache_range(void *vaddr, unsigned int size)
339 clflush_cache_range_opt(vaddr, size);
342 EXPORT_SYMBOL_GPL(clflush_cache_range);
344 #ifdef CONFIG_ARCH_HAS_PMEM_API
345 void arch_invalidate_pmem(void *addr, size_t size)
347 clflush_cache_range(addr, size);
349 EXPORT_SYMBOL_GPL(arch_invalidate_pmem);
352 #ifdef CONFIG_ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION
353 bool cpu_cache_has_invalidate_memregion(void)
355 return !cpu_feature_enabled(X86_FEATURE_HYPERVISOR);
357 EXPORT_SYMBOL_NS_GPL(cpu_cache_has_invalidate_memregion, DEVMEM);
359 int cpu_cache_invalidate_memregion(int res_desc)
361 if (WARN_ON_ONCE(!cpu_cache_has_invalidate_memregion()))
363 wbinvd_on_all_cpus();
366 EXPORT_SYMBOL_NS_GPL(cpu_cache_invalidate_memregion, DEVMEM);
369 static void __cpa_flush_all(void *arg)
371 unsigned long cache = (unsigned long)arg;
374 * Flush all to work around Errata in early athlons regarding
375 * large page flushing.
379 if (cache && boot_cpu_data.x86 >= 4)
383 static void cpa_flush_all(unsigned long cache)
385 BUG_ON(irqs_disabled() && !early_boot_irqs_disabled);
387 on_each_cpu(__cpa_flush_all, (void *) cache, 1);
390 static void __cpa_flush_tlb(void *data)
392 struct cpa_data *cpa = data;
395 for (i = 0; i < cpa->numpages; i++)
396 flush_tlb_one_kernel(fix_addr(__cpa_addr(cpa, i)));
399 static void cpa_flush(struct cpa_data *data, int cache)
401 struct cpa_data *cpa = data;
404 BUG_ON(irqs_disabled() && !early_boot_irqs_disabled);
406 if (cache && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
407 cpa_flush_all(cache);
411 if (cpa->force_flush_all || cpa->numpages > tlb_single_page_flush_ceiling)
414 on_each_cpu(__cpa_flush_tlb, cpa, 1);
420 for (i = 0; i < cpa->numpages; i++) {
421 unsigned long addr = __cpa_addr(cpa, i);
424 pte_t *pte = lookup_address(addr, &level);
427 * Only flush present addresses:
429 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
430 clflush_cache_range_opt((void *)fix_addr(addr), PAGE_SIZE);
435 static bool overlaps(unsigned long r1_start, unsigned long r1_end,
436 unsigned long r2_start, unsigned long r2_end)
438 return (r1_start <= r2_end && r1_end >= r2_start) ||
439 (r2_start <= r1_end && r2_end >= r1_start);
442 #ifdef CONFIG_PCI_BIOS
444 * The BIOS area between 640k and 1Mb needs to be executable for PCI BIOS
445 * based config access (CONFIG_PCI_GOBIOS) support.
447 #define BIOS_PFN PFN_DOWN(BIOS_BEGIN)
448 #define BIOS_PFN_END PFN_DOWN(BIOS_END - 1)
450 static pgprotval_t protect_pci_bios(unsigned long spfn, unsigned long epfn)
452 if (pcibios_enabled && overlaps(spfn, epfn, BIOS_PFN, BIOS_PFN_END))
457 static pgprotval_t protect_pci_bios(unsigned long spfn, unsigned long epfn)
464 * The .rodata section needs to be read-only. Using the pfn catches all
465 * aliases. This also includes __ro_after_init, so do not enforce until
466 * kernel_set_to_readonly is true.
468 static pgprotval_t protect_rodata(unsigned long spfn, unsigned long epfn)
470 unsigned long epfn_ro, spfn_ro = PFN_DOWN(__pa_symbol(__start_rodata));
473 * Note: __end_rodata is at page aligned and not inclusive, so
474 * subtract 1 to get the last enforced PFN in the rodata area.
476 epfn_ro = PFN_DOWN(__pa_symbol(__end_rodata)) - 1;
478 if (kernel_set_to_readonly && overlaps(spfn, epfn, spfn_ro, epfn_ro))
484 * Protect kernel text against becoming non executable by forbidding
485 * _PAGE_NX. This protects only the high kernel mapping (_text -> _etext)
486 * out of which the kernel actually executes. Do not protect the low
489 * This does not cover __inittext since that is gone after boot.
491 static pgprotval_t protect_kernel_text(unsigned long start, unsigned long end)
493 unsigned long t_end = (unsigned long)_etext - 1;
494 unsigned long t_start = (unsigned long)_text;
496 if (overlaps(start, end, t_start, t_end))
501 #if defined(CONFIG_X86_64)
503 * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
504 * kernel text mappings for the large page aligned text, rodata sections
505 * will be always read-only. For the kernel identity mappings covering the
506 * holes caused by this alignment can be anything that user asks.
508 * This will preserve the large page mappings for kernel text/data at no
511 static pgprotval_t protect_kernel_text_ro(unsigned long start,
514 unsigned long t_end = (unsigned long)__end_rodata_hpage_align - 1;
515 unsigned long t_start = (unsigned long)_text;
518 if (!kernel_set_to_readonly || !overlaps(start, end, t_start, t_end))
521 * Don't enforce the !RW mapping for the kernel text mapping, if
522 * the current mapping is already using small page mapping. No
523 * need to work hard to preserve large page mappings in this case.
525 * This also fixes the Linux Xen paravirt guest boot failure caused
526 * by unexpected read-only mappings for kernel identity
527 * mappings. In this paravirt guest case, the kernel text mapping
528 * and the kernel identity mapping share the same page-table pages,
529 * so the protections for kernel text and identity mappings have to
532 if (lookup_address(start, &level) && (level != PG_LEVEL_4K))
537 static pgprotval_t protect_kernel_text_ro(unsigned long start,
544 static inline bool conflicts(pgprot_t prot, pgprotval_t val)
546 return (pgprot_val(prot) & ~val) != pgprot_val(prot);
549 static inline void check_conflict(int warnlvl, pgprot_t prot, pgprotval_t val,
550 unsigned long start, unsigned long end,
551 unsigned long pfn, const char *txt)
553 static const char *lvltxt[] = {
554 [CPA_CONFLICT] = "conflict",
555 [CPA_PROTECT] = "protect",
556 [CPA_DETECT] = "detect",
559 if (warnlvl > cpa_warn_level || !conflicts(prot, val))
562 pr_warn("CPA %8s %10s: 0x%016lx - 0x%016lx PFN %lx req %016llx prevent %016llx\n",
563 lvltxt[warnlvl], txt, start, end, pfn, (unsigned long long)pgprot_val(prot),
564 (unsigned long long)val);
568 * Certain areas of memory on x86 require very specific protection flags,
569 * for example the BIOS area or kernel text. Callers don't always get this
570 * right (again, ioremap() on BIOS memory is not uncommon) so this function
571 * checks and fixes these known static required protection bits.
573 static inline pgprot_t static_protections(pgprot_t prot, unsigned long start,
574 unsigned long pfn, unsigned long npg,
575 unsigned long lpsize, int warnlvl)
577 pgprotval_t forbidden, res;
581 * There is no point in checking RW/NX conflicts when the requested
582 * mapping is setting the page !PRESENT.
584 if (!(pgprot_val(prot) & _PAGE_PRESENT))
587 /* Operate on the virtual address */
588 end = start + npg * PAGE_SIZE - 1;
590 res = protect_kernel_text(start, end);
591 check_conflict(warnlvl, prot, res, start, end, pfn, "Text NX");
595 * Special case to preserve a large page. If the change spawns the
596 * full large page mapping then there is no point to split it
597 * up. Happens with ftrace and is going to be removed once ftrace
598 * switched to text_poke().
600 if (lpsize != (npg * PAGE_SIZE) || (start & (lpsize - 1))) {
601 res = protect_kernel_text_ro(start, end);
602 check_conflict(warnlvl, prot, res, start, end, pfn, "Text RO");
606 /* Check the PFN directly */
607 res = protect_pci_bios(pfn, pfn + npg - 1);
608 check_conflict(warnlvl, prot, res, start, end, pfn, "PCIBIOS NX");
611 res = protect_rodata(pfn, pfn + npg - 1);
612 check_conflict(warnlvl, prot, res, start, end, pfn, "Rodata RO");
615 return __pgprot(pgprot_val(prot) & ~forbidden);
619 * Validate strict W^X semantics.
621 static inline pgprot_t verify_rwx(pgprot_t old, pgprot_t new, unsigned long start,
622 unsigned long pfn, unsigned long npg)
627 * 32-bit has some unfixable W+X issues, like EFI code
628 * and writeable data being in the same page. Disable
629 * detection and enforcement there.
631 if (IS_ENABLED(CONFIG_X86_32))
634 /* Only verify when NX is supported: */
635 if (!(__supported_pte_mask & _PAGE_NX))
638 if (!((pgprot_val(old) ^ pgprot_val(new)) & (_PAGE_RW | _PAGE_NX)))
641 if ((pgprot_val(new) & (_PAGE_RW | _PAGE_NX)) != _PAGE_RW)
644 end = start + npg * PAGE_SIZE - 1;
645 WARN_ONCE(1, "CPA detected W^X violation: %016llx -> %016llx range: 0x%016lx - 0x%016lx PFN %lx\n",
646 (unsigned long long)pgprot_val(old),
647 (unsigned long long)pgprot_val(new),
651 * For now, allow all permission change attempts by returning the
652 * attempted permissions. This can 'return old' to actively
653 * refuse the permission change at a later time.
659 * Lookup the page table entry for a virtual address in a specific pgd.
660 * Return a pointer to the entry and the level of the mapping.
662 pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address,
669 *level = PG_LEVEL_NONE;
674 p4d = p4d_offset(pgd, address);
678 *level = PG_LEVEL_512G;
679 if (p4d_leaf(*p4d) || !p4d_present(*p4d))
682 pud = pud_offset(p4d, address);
686 *level = PG_LEVEL_1G;
687 if (pud_leaf(*pud) || !pud_present(*pud))
690 pmd = pmd_offset(pud, address);
694 *level = PG_LEVEL_2M;
695 if (pmd_leaf(*pmd) || !pmd_present(*pmd))
698 *level = PG_LEVEL_4K;
700 return pte_offset_kernel(pmd, address);
704 * Lookup the page table entry for a virtual address. Return a pointer
705 * to the entry and the level of the mapping.
707 * Note: We return pud and pmd either when the entry is marked large
708 * or when the present bit is not set. Otherwise we would return a
709 * pointer to a nonexisting mapping.
711 pte_t *lookup_address(unsigned long address, unsigned int *level)
713 return lookup_address_in_pgd(pgd_offset_k(address), address, level);
715 EXPORT_SYMBOL_GPL(lookup_address);
717 static pte_t *_lookup_address_cpa(struct cpa_data *cpa, unsigned long address,
721 return lookup_address_in_pgd(cpa->pgd + pgd_index(address),
724 return lookup_address(address, level);
728 * Lookup the PMD entry for a virtual address. Return a pointer to the entry
729 * or NULL if not present.
731 pmd_t *lookup_pmd_address(unsigned long address)
737 pgd = pgd_offset_k(address);
741 p4d = p4d_offset(pgd, address);
742 if (p4d_none(*p4d) || p4d_leaf(*p4d) || !p4d_present(*p4d))
745 pud = pud_offset(p4d, address);
746 if (pud_none(*pud) || pud_leaf(*pud) || !pud_present(*pud))
749 return pmd_offset(pud, address);
753 * This is necessary because __pa() does not work on some
754 * kinds of memory, like vmalloc() or the alloc_remap()
755 * areas on 32-bit NUMA systems. The percpu areas can
756 * end up in this kind of memory, for instance.
758 * Note that as long as the PTEs are well-formed with correct PFNs, this
759 * works without checking the PRESENT bit in the leaf PTE. This is unlike
760 * the similar vmalloc_to_page() and derivatives. Callers may depend on
763 * This could be optimized, but it is only used in paths that are not perf
764 * sensitive, and keeping it unoptimized should increase the testing coverage
765 * for the more obscure platforms.
767 phys_addr_t slow_virt_to_phys(void *__virt_addr)
769 unsigned long virt_addr = (unsigned long)__virt_addr;
770 phys_addr_t phys_addr;
771 unsigned long offset;
775 pte = lookup_address(virt_addr, &level);
779 * pXX_pfn() returns unsigned long, which must be cast to phys_addr_t
780 * before being left-shifted PAGE_SHIFT bits -- this trick is to
781 * make 32-PAE kernel work correctly.
785 phys_addr = (phys_addr_t)pud_pfn(*(pud_t *)pte) << PAGE_SHIFT;
786 offset = virt_addr & ~PUD_MASK;
789 phys_addr = (phys_addr_t)pmd_pfn(*(pmd_t *)pte) << PAGE_SHIFT;
790 offset = virt_addr & ~PMD_MASK;
793 phys_addr = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
794 offset = virt_addr & ~PAGE_MASK;
797 return (phys_addr_t)(phys_addr | offset);
799 EXPORT_SYMBOL_GPL(slow_virt_to_phys);
802 * Set the new pmd in all the pgds we know about:
804 static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
807 set_pte_atomic(kpte, pte);
809 if (!SHARED_KERNEL_PMD) {
812 list_for_each_entry(page, &pgd_list, lru) {
818 pgd = (pgd_t *)page_address(page) + pgd_index(address);
819 p4d = p4d_offset(pgd, address);
820 pud = pud_offset(p4d, address);
821 pmd = pmd_offset(pud, address);
822 set_pte_atomic((pte_t *)pmd, pte);
828 static pgprot_t pgprot_clear_protnone_bits(pgprot_t prot)
831 * _PAGE_GLOBAL means "global page" for present PTEs.
832 * But, it is also used to indicate _PAGE_PROTNONE
833 * for non-present PTEs.
835 * This ensures that a _PAGE_GLOBAL PTE going from
836 * present to non-present is not confused as
839 if (!(pgprot_val(prot) & _PAGE_PRESENT))
840 pgprot_val(prot) &= ~_PAGE_GLOBAL;
845 static int __should_split_large_page(pte_t *kpte, unsigned long address,
846 struct cpa_data *cpa)
848 unsigned long numpages, pmask, psize, lpaddr, pfn, old_pfn;
849 pgprot_t old_prot, new_prot, req_prot, chk_prot;
854 * Check for races, another CPU might have split this page
857 tmp = _lookup_address_cpa(cpa, address, &level);
863 old_prot = pmd_pgprot(*(pmd_t *)kpte);
864 old_pfn = pmd_pfn(*(pmd_t *)kpte);
865 cpa_inc_2m_checked();
868 old_prot = pud_pgprot(*(pud_t *)kpte);
869 old_pfn = pud_pfn(*(pud_t *)kpte);
870 cpa_inc_1g_checked();
876 psize = page_level_size(level);
877 pmask = page_level_mask(level);
880 * Calculate the number of pages, which fit into this large
881 * page starting at address:
883 lpaddr = (address + psize) & pmask;
884 numpages = (lpaddr - address) >> PAGE_SHIFT;
885 if (numpages < cpa->numpages)
886 cpa->numpages = numpages;
889 * We are safe now. Check whether the new pgprot is the same:
890 * Convert protection attributes to 4k-format, as cpa->mask* are set
894 /* Clear PSE (aka _PAGE_PAT) and move PAT bit to correct position */
895 req_prot = pgprot_large_2_4k(old_prot);
897 pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr);
898 pgprot_val(req_prot) |= pgprot_val(cpa->mask_set);
901 * req_prot is in format of 4k pages. It must be converted to large
902 * page format: the caching mode includes the PAT bit located at
903 * different bit positions in the two formats.
905 req_prot = pgprot_4k_2_large(req_prot);
906 req_prot = pgprot_clear_protnone_bits(req_prot);
907 if (pgprot_val(req_prot) & _PAGE_PRESENT)
908 pgprot_val(req_prot) |= _PAGE_PSE;
911 * old_pfn points to the large page base pfn. So we need to add the
912 * offset of the virtual address:
914 pfn = old_pfn + ((address & (psize - 1)) >> PAGE_SHIFT);
918 * Calculate the large page base address and the number of 4K pages
921 lpaddr = address & pmask;
922 numpages = psize >> PAGE_SHIFT;
925 * Sanity check that the existing mapping is correct versus the static
926 * protections. static_protections() guards against !PRESENT, so no
927 * extra conditional required here.
929 chk_prot = static_protections(old_prot, lpaddr, old_pfn, numpages,
930 psize, CPA_CONFLICT);
932 if (WARN_ON_ONCE(pgprot_val(chk_prot) != pgprot_val(old_prot))) {
934 * Split the large page and tell the split code to
935 * enforce static protections.
937 cpa->force_static_prot = 1;
942 * Optimization: If the requested pgprot is the same as the current
943 * pgprot, then the large page can be preserved and no updates are
944 * required independent of alignment and length of the requested
945 * range. The above already established that the current pgprot is
946 * correct, which in consequence makes the requested pgprot correct
947 * as well if it is the same. The static protection scan below will
948 * not come to a different conclusion.
950 if (pgprot_val(req_prot) == pgprot_val(old_prot)) {
951 cpa_inc_lp_sameprot(level);
956 * If the requested range does not cover the full page, split it up
958 if (address != lpaddr || cpa->numpages != numpages)
962 * Check whether the requested pgprot is conflicting with a static
963 * protection requirement in the large page.
965 new_prot = static_protections(req_prot, lpaddr, old_pfn, numpages,
968 new_prot = verify_rwx(old_prot, new_prot, lpaddr, old_pfn, numpages);
971 * If there is a conflict, split the large page.
973 * There used to be a 4k wise evaluation trying really hard to
974 * preserve the large pages, but experimentation has shown, that this
975 * does not help at all. There might be corner cases which would
976 * preserve one large page occasionally, but it's really not worth the
977 * extra code and cycles for the common case.
979 if (pgprot_val(req_prot) != pgprot_val(new_prot))
982 /* All checks passed. Update the large page mapping. */
983 new_pte = pfn_pte(old_pfn, new_prot);
984 __set_pmd_pte(kpte, address, new_pte);
985 cpa->flags |= CPA_FLUSHTLB;
986 cpa_inc_lp_preserved(level);
990 static int should_split_large_page(pte_t *kpte, unsigned long address,
991 struct cpa_data *cpa)
995 if (cpa->force_split)
998 spin_lock(&pgd_lock);
999 do_split = __should_split_large_page(kpte, address, cpa);
1000 spin_unlock(&pgd_lock);
1005 static void split_set_pte(struct cpa_data *cpa, pte_t *pte, unsigned long pfn,
1006 pgprot_t ref_prot, unsigned long address,
1009 unsigned int npg = PFN_DOWN(size);
1013 * If should_split_large_page() discovered an inconsistent mapping,
1014 * remove the invalid protection in the split mapping.
1016 if (!cpa->force_static_prot)
1019 /* Hand in lpsize = 0 to enforce the protection mechanism */
1020 prot = static_protections(ref_prot, address, pfn, npg, 0, CPA_PROTECT);
1022 if (pgprot_val(prot) == pgprot_val(ref_prot))
1026 * If this is splitting a PMD, fix it up. PUD splits cannot be
1027 * fixed trivially as that would require to rescan the newly
1028 * installed PMD mappings after returning from split_large_page()
1029 * so an eventual further split can allocate the necessary PTE
1030 * pages. Warn for now and revisit it in case this actually
1033 if (size == PAGE_SIZE)
1036 pr_warn_once("CPA: Cannot fixup static protections for PUD split\n");
1038 set_pte(pte, pfn_pte(pfn, ref_prot));
1042 __split_large_page(struct cpa_data *cpa, pte_t *kpte, unsigned long address,
1045 unsigned long lpaddr, lpinc, ref_pfn, pfn, pfninc = 1;
1046 pte_t *pbase = (pte_t *)page_address(base);
1047 unsigned int i, level;
1051 spin_lock(&pgd_lock);
1053 * Check for races, another CPU might have split this page
1054 * up for us already:
1056 tmp = _lookup_address_cpa(cpa, address, &level);
1058 spin_unlock(&pgd_lock);
1062 paravirt_alloc_pte(&init_mm, page_to_pfn(base));
1066 ref_prot = pmd_pgprot(*(pmd_t *)kpte);
1068 * Clear PSE (aka _PAGE_PAT) and move
1069 * PAT bit to correct position.
1071 ref_prot = pgprot_large_2_4k(ref_prot);
1072 ref_pfn = pmd_pfn(*(pmd_t *)kpte);
1073 lpaddr = address & PMD_MASK;
1078 ref_prot = pud_pgprot(*(pud_t *)kpte);
1079 ref_pfn = pud_pfn(*(pud_t *)kpte);
1080 pfninc = PMD_SIZE >> PAGE_SHIFT;
1081 lpaddr = address & PUD_MASK;
1084 * Clear the PSE flags if the PRESENT flag is not set
1085 * otherwise pmd_present/pmd_huge will return true
1086 * even on a non present pmd.
1088 if (!(pgprot_val(ref_prot) & _PAGE_PRESENT))
1089 pgprot_val(ref_prot) &= ~_PAGE_PSE;
1093 spin_unlock(&pgd_lock);
1097 ref_prot = pgprot_clear_protnone_bits(ref_prot);
1100 * Get the target pfn from the original entry:
1103 for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc, lpaddr += lpinc)
1104 split_set_pte(cpa, pbase + i, pfn, ref_prot, lpaddr, lpinc);
1106 if (virt_addr_valid(address)) {
1107 unsigned long pfn = PFN_DOWN(__pa(address));
1109 if (pfn_range_is_mapped(pfn, pfn + 1))
1110 split_page_count(level);
1114 * Install the new, split up pagetable.
1116 * We use the standard kernel pagetable protections for the new
1117 * pagetable protections, the actual ptes set above control the
1118 * primary protection behavior:
1120 __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
1123 * Do a global flush tlb after splitting the large page
1124 * and before we do the actual change page attribute in the PTE.
1126 * Without this, we violate the TLB application note, that says:
1127 * "The TLBs may contain both ordinary and large-page
1128 * translations for a 4-KByte range of linear addresses. This
1129 * may occur if software modifies the paging structures so that
1130 * the page size used for the address range changes. If the two
1131 * translations differ with respect to page frame or attributes
1132 * (e.g., permissions), processor behavior is undefined and may
1133 * be implementation-specific."
1135 * We do this global tlb flush inside the cpa_lock, so that we
1136 * don't allow any other cpu, with stale tlb entries change the
1137 * page attribute in parallel, that also falls into the
1138 * just split large page entry.
1141 spin_unlock(&pgd_lock);
1146 static int split_large_page(struct cpa_data *cpa, pte_t *kpte,
1147 unsigned long address)
1151 if (!debug_pagealloc_enabled())
1152 spin_unlock(&cpa_lock);
1153 base = alloc_pages(GFP_KERNEL, 0);
1154 if (!debug_pagealloc_enabled())
1155 spin_lock(&cpa_lock);
1159 if (__split_large_page(cpa, kpte, address, base))
1165 static bool try_to_free_pte_page(pte_t *pte)
1169 for (i = 0; i < PTRS_PER_PTE; i++)
1170 if (!pte_none(pte[i]))
1173 free_page((unsigned long)pte);
1177 static bool try_to_free_pmd_page(pmd_t *pmd)
1181 for (i = 0; i < PTRS_PER_PMD; i++)
1182 if (!pmd_none(pmd[i]))
1185 free_page((unsigned long)pmd);
1189 static bool unmap_pte_range(pmd_t *pmd, unsigned long start, unsigned long end)
1191 pte_t *pte = pte_offset_kernel(pmd, start);
1193 while (start < end) {
1194 set_pte(pte, __pte(0));
1200 if (try_to_free_pte_page((pte_t *)pmd_page_vaddr(*pmd))) {
1207 static void __unmap_pmd_range(pud_t *pud, pmd_t *pmd,
1208 unsigned long start, unsigned long end)
1210 if (unmap_pte_range(pmd, start, end))
1211 if (try_to_free_pmd_page(pud_pgtable(*pud)))
1215 static void unmap_pmd_range(pud_t *pud, unsigned long start, unsigned long end)
1217 pmd_t *pmd = pmd_offset(pud, start);
1220 * Not on a 2MB page boundary?
1222 if (start & (PMD_SIZE - 1)) {
1223 unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
1224 unsigned long pre_end = min_t(unsigned long, end, next_page);
1226 __unmap_pmd_range(pud, pmd, start, pre_end);
1233 * Try to unmap in 2M chunks.
1235 while (end - start >= PMD_SIZE) {
1239 __unmap_pmd_range(pud, pmd, start, start + PMD_SIZE);
1249 return __unmap_pmd_range(pud, pmd, start, end);
1252 * Try again to free the PMD page if haven't succeeded above.
1254 if (!pud_none(*pud))
1255 if (try_to_free_pmd_page(pud_pgtable(*pud)))
1259 static void unmap_pud_range(p4d_t *p4d, unsigned long start, unsigned long end)
1261 pud_t *pud = pud_offset(p4d, start);
1264 * Not on a GB page boundary?
1266 if (start & (PUD_SIZE - 1)) {
1267 unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
1268 unsigned long pre_end = min_t(unsigned long, end, next_page);
1270 unmap_pmd_range(pud, start, pre_end);
1277 * Try to unmap in 1G chunks?
1279 while (end - start >= PUD_SIZE) {
1284 unmap_pmd_range(pud, start, start + PUD_SIZE);
1294 unmap_pmd_range(pud, start, end);
1297 * No need to try to free the PUD page because we'll free it in
1298 * populate_pgd's error path
1302 static int alloc_pte_page(pmd_t *pmd)
1304 pte_t *pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
1308 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
1312 static int alloc_pmd_page(pud_t *pud)
1314 pmd_t *pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL);
1318 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
1322 static void populate_pte(struct cpa_data *cpa,
1323 unsigned long start, unsigned long end,
1324 unsigned num_pages, pmd_t *pmd, pgprot_t pgprot)
1328 pte = pte_offset_kernel(pmd, start);
1330 pgprot = pgprot_clear_protnone_bits(pgprot);
1332 while (num_pages-- && start < end) {
1333 set_pte(pte, pfn_pte(cpa->pfn, pgprot));
1341 static long populate_pmd(struct cpa_data *cpa,
1342 unsigned long start, unsigned long end,
1343 unsigned num_pages, pud_t *pud, pgprot_t pgprot)
1347 pgprot_t pmd_pgprot;
1350 * Not on a 2M boundary?
1352 if (start & (PMD_SIZE - 1)) {
1353 unsigned long pre_end = start + (num_pages << PAGE_SHIFT);
1354 unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
1356 pre_end = min_t(unsigned long, pre_end, next_page);
1357 cur_pages = (pre_end - start) >> PAGE_SHIFT;
1358 cur_pages = min_t(unsigned int, num_pages, cur_pages);
1363 pmd = pmd_offset(pud, start);
1365 if (alloc_pte_page(pmd))
1368 populate_pte(cpa, start, pre_end, cur_pages, pmd, pgprot);
1374 * We mapped them all?
1376 if (num_pages == cur_pages)
1379 pmd_pgprot = pgprot_4k_2_large(pgprot);
1381 while (end - start >= PMD_SIZE) {
1384 * We cannot use a 1G page so allocate a PMD page if needed.
1387 if (alloc_pmd_page(pud))
1390 pmd = pmd_offset(pud, start);
1392 set_pmd(pmd, pmd_mkhuge(pfn_pmd(cpa->pfn,
1393 canon_pgprot(pmd_pgprot))));
1396 cpa->pfn += PMD_SIZE >> PAGE_SHIFT;
1397 cur_pages += PMD_SIZE >> PAGE_SHIFT;
1401 * Map trailing 4K pages.
1404 pmd = pmd_offset(pud, start);
1406 if (alloc_pte_page(pmd))
1409 populate_pte(cpa, start, end, num_pages - cur_pages,
1415 static int populate_pud(struct cpa_data *cpa, unsigned long start, p4d_t *p4d,
1421 pgprot_t pud_pgprot;
1423 end = start + (cpa->numpages << PAGE_SHIFT);
1426 * Not on a Gb page boundary? => map everything up to it with
1429 if (start & (PUD_SIZE - 1)) {
1430 unsigned long pre_end;
1431 unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
1433 pre_end = min_t(unsigned long, end, next_page);
1434 cur_pages = (pre_end - start) >> PAGE_SHIFT;
1435 cur_pages = min_t(int, (int)cpa->numpages, cur_pages);
1437 pud = pud_offset(p4d, start);
1443 if (alloc_pmd_page(pud))
1446 cur_pages = populate_pmd(cpa, start, pre_end, cur_pages,
1454 /* We mapped them all? */
1455 if (cpa->numpages == cur_pages)
1458 pud = pud_offset(p4d, start);
1459 pud_pgprot = pgprot_4k_2_large(pgprot);
1462 * Map everything starting from the Gb boundary, possibly with 1G pages
1464 while (boot_cpu_has(X86_FEATURE_GBPAGES) && end - start >= PUD_SIZE) {
1465 set_pud(pud, pud_mkhuge(pfn_pud(cpa->pfn,
1466 canon_pgprot(pud_pgprot))));
1469 cpa->pfn += PUD_SIZE >> PAGE_SHIFT;
1470 cur_pages += PUD_SIZE >> PAGE_SHIFT;
1474 /* Map trailing leftover */
1478 pud = pud_offset(p4d, start);
1480 if (alloc_pmd_page(pud))
1483 tmp = populate_pmd(cpa, start, end, cpa->numpages - cur_pages,
1494 * Restrictions for kernel page table do not necessarily apply when mapping in
1497 static int populate_pgd(struct cpa_data *cpa, unsigned long addr)
1499 pgprot_t pgprot = __pgprot(_KERNPG_TABLE);
1500 pud_t *pud = NULL; /* shut up gcc */
1505 pgd_entry = cpa->pgd + pgd_index(addr);
1507 if (pgd_none(*pgd_entry)) {
1508 p4d = (p4d_t *)get_zeroed_page(GFP_KERNEL);
1512 set_pgd(pgd_entry, __pgd(__pa(p4d) | _KERNPG_TABLE));
1516 * Allocate a PUD page and hand it down for mapping.
1518 p4d = p4d_offset(pgd_entry, addr);
1519 if (p4d_none(*p4d)) {
1520 pud = (pud_t *)get_zeroed_page(GFP_KERNEL);
1524 set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE));
1527 pgprot_val(pgprot) &= ~pgprot_val(cpa->mask_clr);
1528 pgprot_val(pgprot) |= pgprot_val(cpa->mask_set);
1530 ret = populate_pud(cpa, addr, p4d, pgprot);
1533 * Leave the PUD page in place in case some other CPU or thread
1534 * already found it, but remove any useless entries we just
1537 unmap_pud_range(p4d, addr,
1538 addr + (cpa->numpages << PAGE_SHIFT));
1542 cpa->numpages = ret;
1546 static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
1551 * Right now, we only execute this code path when mapping
1552 * the EFI virtual memory map regions, no other users
1553 * provide a ->pgd value. This may change in the future.
1555 return populate_pgd(cpa, vaddr);
1559 * Ignore all non primary paths.
1567 * Ignore the NULL PTE for kernel identity mapping, as it is expected
1569 * Also set numpages to '1' indicating that we processed cpa req for
1570 * one virtual address page and its pfn. TBD: numpages can be set based
1571 * on the initial value and the level returned by lookup_address().
1573 if (within(vaddr, PAGE_OFFSET,
1574 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
1576 cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
1579 } else if (__cpa_pfn_in_highmap(cpa->pfn)) {
1580 /* Faults in the highmap are OK, so do not warn: */
1583 WARN(1, KERN_WARNING "CPA: called for zero pte. "
1584 "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
1591 static int __change_page_attr(struct cpa_data *cpa, int primary)
1593 unsigned long address;
1596 pte_t *kpte, old_pte;
1598 address = __cpa_addr(cpa, cpa->curpage);
1600 kpte = _lookup_address_cpa(cpa, address, &level);
1602 return __cpa_process_fault(cpa, address, primary);
1605 if (pte_none(old_pte))
1606 return __cpa_process_fault(cpa, address, primary);
1608 if (level == PG_LEVEL_4K) {
1610 pgprot_t old_prot = pte_pgprot(old_pte);
1611 pgprot_t new_prot = pte_pgprot(old_pte);
1612 unsigned long pfn = pte_pfn(old_pte);
1614 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
1615 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
1617 cpa_inc_4k_install();
1618 /* Hand in lpsize = 0 to enforce the protection mechanism */
1619 new_prot = static_protections(new_prot, address, pfn, 1, 0,
1622 new_prot = verify_rwx(old_prot, new_prot, address, pfn, 1);
1624 new_prot = pgprot_clear_protnone_bits(new_prot);
1627 * We need to keep the pfn from the existing PTE,
1628 * after all we're only going to change its attributes
1629 * not the memory it points to
1631 new_pte = pfn_pte(pfn, new_prot);
1634 * Do we really change anything ?
1636 if (pte_val(old_pte) != pte_val(new_pte)) {
1637 set_pte_atomic(kpte, new_pte);
1638 cpa->flags |= CPA_FLUSHTLB;
1645 * Check, whether we can keep the large page intact
1646 * and just change the pte:
1648 do_split = should_split_large_page(kpte, address, cpa);
1650 * When the range fits into the existing large page,
1651 * return. cp->numpages and cpa->tlbflush have been updated in
1658 * We have to split the large page:
1660 err = split_large_page(cpa, kpte, address);
1667 static int __change_page_attr_set_clr(struct cpa_data *cpa, int primary);
1670 * Check the directmap and "high kernel map" 'aliases'.
1672 static int cpa_process_alias(struct cpa_data *cpa)
1674 struct cpa_data alias_cpa;
1675 unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
1676 unsigned long vaddr;
1679 if (!pfn_range_is_mapped(cpa->pfn, cpa->pfn + 1))
1683 * No need to redo, when the primary call touched the direct
1686 vaddr = __cpa_addr(cpa, cpa->curpage);
1687 if (!(within(vaddr, PAGE_OFFSET,
1688 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
1691 alias_cpa.vaddr = &laddr;
1692 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
1693 alias_cpa.curpage = 0;
1695 /* Directmap always has NX set, do not modify. */
1696 if (__supported_pte_mask & _PAGE_NX) {
1697 alias_cpa.mask_clr.pgprot &= ~_PAGE_NX;
1698 alias_cpa.mask_set.pgprot &= ~_PAGE_NX;
1701 cpa->force_flush_all = 1;
1703 ret = __change_page_attr_set_clr(&alias_cpa, 0);
1708 #ifdef CONFIG_X86_64
1710 * If the primary call didn't touch the high mapping already
1711 * and the physical address is inside the kernel map, we need
1712 * to touch the high mapped kernel as well:
1714 if (!within(vaddr, (unsigned long)_text, _brk_end) &&
1715 __cpa_pfn_in_highmap(cpa->pfn)) {
1716 unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
1717 __START_KERNEL_map - phys_base;
1719 alias_cpa.vaddr = &temp_cpa_vaddr;
1720 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
1721 alias_cpa.curpage = 0;
1724 * [_text, _brk_end) also covers data, do not modify NX except
1725 * in cases where the highmap is the primary target.
1727 if (__supported_pte_mask & _PAGE_NX) {
1728 alias_cpa.mask_clr.pgprot &= ~_PAGE_NX;
1729 alias_cpa.mask_set.pgprot &= ~_PAGE_NX;
1732 cpa->force_flush_all = 1;
1734 * The high mapping range is imprecise, so ignore the
1737 __change_page_attr_set_clr(&alias_cpa, 0);
1744 static int __change_page_attr_set_clr(struct cpa_data *cpa, int primary)
1746 unsigned long numpages = cpa->numpages;
1747 unsigned long rempages = numpages;
1753 if (!(pgprot_val(cpa->mask_set) | pgprot_val(cpa->mask_clr)) &&
1759 * Store the remaining nr of pages for the large page
1760 * preservation check.
1762 cpa->numpages = rempages;
1763 /* for array changes, we can't use large page */
1764 if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY))
1767 if (!debug_pagealloc_enabled())
1768 spin_lock(&cpa_lock);
1769 ret = __change_page_attr(cpa, primary);
1770 if (!debug_pagealloc_enabled())
1771 spin_unlock(&cpa_lock);
1775 if (primary && !(cpa->flags & CPA_NO_CHECK_ALIAS)) {
1776 ret = cpa_process_alias(cpa);
1782 * Adjust the number of pages with the result of the
1783 * CPA operation. Either a large page has been
1784 * preserved or a single page update happened.
1786 BUG_ON(cpa->numpages > rempages || !cpa->numpages);
1787 rempages -= cpa->numpages;
1788 cpa->curpage += cpa->numpages;
1792 /* Restore the original numpages */
1793 cpa->numpages = numpages;
1797 static int change_page_attr_set_clr(unsigned long *addr, int numpages,
1798 pgprot_t mask_set, pgprot_t mask_clr,
1799 int force_split, int in_flag,
1800 struct page **pages)
1802 struct cpa_data cpa;
1805 memset(&cpa, 0, sizeof(cpa));
1808 * Check, if we are requested to set a not supported
1809 * feature. Clearing non-supported features is OK.
1811 mask_set = canon_pgprot(mask_set);
1813 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
1816 /* Ensure we are PAGE_SIZE aligned */
1817 if (in_flag & CPA_ARRAY) {
1819 for (i = 0; i < numpages; i++) {
1820 if (addr[i] & ~PAGE_MASK) {
1821 addr[i] &= PAGE_MASK;
1825 } else if (!(in_flag & CPA_PAGES_ARRAY)) {
1827 * in_flag of CPA_PAGES_ARRAY implies it is aligned.
1828 * No need to check in that case
1830 if (*addr & ~PAGE_MASK) {
1833 * People should not be passing in unaligned addresses:
1839 /* Must avoid aliasing mappings in the highmem code */
1840 kmap_flush_unused();
1846 cpa.numpages = numpages;
1847 cpa.mask_set = mask_set;
1848 cpa.mask_clr = mask_clr;
1849 cpa.flags = in_flag;
1851 cpa.force_split = force_split;
1853 ret = __change_page_attr_set_clr(&cpa, 1);
1856 * Check whether we really changed something:
1858 if (!(cpa.flags & CPA_FLUSHTLB))
1862 * No need to flush, when we did not set any of the caching
1865 cache = !!pgprot2cachemode(mask_set);
1868 * On error; flush everything to be sure.
1871 cpa_flush_all(cache);
1875 cpa_flush(&cpa, cache);
1880 static inline int change_page_attr_set(unsigned long *addr, int numpages,
1881 pgprot_t mask, int array)
1883 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
1884 (array ? CPA_ARRAY : 0), NULL);
1887 static inline int change_page_attr_clear(unsigned long *addr, int numpages,
1888 pgprot_t mask, int array)
1890 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
1891 (array ? CPA_ARRAY : 0), NULL);
1894 static inline int cpa_set_pages_array(struct page **pages, int numpages,
1897 return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0,
1898 CPA_PAGES_ARRAY, pages);
1901 static inline int cpa_clear_pages_array(struct page **pages, int numpages,
1904 return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
1905 CPA_PAGES_ARRAY, pages);
1909 * __set_memory_prot is an internal helper for callers that have been passed
1910 * a pgprot_t value from upper layers and a reservation has already been taken.
1911 * If you want to set the pgprot to a specific page protocol, use the
1912 * set_memory_xx() functions.
1914 int __set_memory_prot(unsigned long addr, int numpages, pgprot_t prot)
1916 return change_page_attr_set_clr(&addr, numpages, prot,
1917 __pgprot(~pgprot_val(prot)), 0, 0,
1921 int _set_memory_uc(unsigned long addr, int numpages)
1924 * for now UC MINUS. see comments in ioremap()
1925 * If you really need strong UC use ioremap_uc(), but note
1926 * that you cannot override IO areas with set_memory_*() as
1927 * these helpers cannot work with IO memory.
1929 return change_page_attr_set(&addr, numpages,
1930 cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS),
1934 int set_memory_uc(unsigned long addr, int numpages)
1939 * for now UC MINUS. see comments in ioremap()
1941 ret = memtype_reserve(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
1942 _PAGE_CACHE_MODE_UC_MINUS, NULL);
1946 ret = _set_memory_uc(addr, numpages);
1953 memtype_free(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1957 EXPORT_SYMBOL(set_memory_uc);
1959 int _set_memory_wc(unsigned long addr, int numpages)
1963 ret = change_page_attr_set(&addr, numpages,
1964 cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS),
1967 ret = change_page_attr_set_clr(&addr, numpages,
1968 cachemode2pgprot(_PAGE_CACHE_MODE_WC),
1969 __pgprot(_PAGE_CACHE_MASK),
1975 int set_memory_wc(unsigned long addr, int numpages)
1979 ret = memtype_reserve(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
1980 _PAGE_CACHE_MODE_WC, NULL);
1984 ret = _set_memory_wc(addr, numpages);
1986 memtype_free(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1990 EXPORT_SYMBOL(set_memory_wc);
1992 int _set_memory_wt(unsigned long addr, int numpages)
1994 return change_page_attr_set(&addr, numpages,
1995 cachemode2pgprot(_PAGE_CACHE_MODE_WT), 0);
1998 int _set_memory_wb(unsigned long addr, int numpages)
2000 /* WB cache mode is hard wired to all cache attribute bits being 0 */
2001 return change_page_attr_clear(&addr, numpages,
2002 __pgprot(_PAGE_CACHE_MASK), 0);
2005 int set_memory_wb(unsigned long addr, int numpages)
2009 ret = _set_memory_wb(addr, numpages);
2013 memtype_free(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
2016 EXPORT_SYMBOL(set_memory_wb);
2018 /* Prevent speculative access to a page by marking it not-present */
2019 #ifdef CONFIG_X86_64
2020 int set_mce_nospec(unsigned long pfn)
2022 unsigned long decoy_addr;
2025 /* SGX pages are not in the 1:1 map */
2026 if (arch_is_platform_page(pfn << PAGE_SHIFT))
2029 * We would like to just call:
2030 * set_memory_XX((unsigned long)pfn_to_kaddr(pfn), 1);
2031 * but doing that would radically increase the odds of a
2032 * speculative access to the poison page because we'd have
2033 * the virtual address of the kernel 1:1 mapping sitting
2034 * around in registers.
2035 * Instead we get tricky. We create a non-canonical address
2036 * that looks just like the one we want, but has bit 63 flipped.
2037 * This relies on set_memory_XX() properly sanitizing any __pa()
2038 * results with __PHYSICAL_MASK or PTE_PFN_MASK.
2040 decoy_addr = (pfn << PAGE_SHIFT) + (PAGE_OFFSET ^ BIT(63));
2042 rc = set_memory_np(decoy_addr, 1);
2044 pr_warn("Could not invalidate pfn=0x%lx from 1:1 map\n", pfn);
2048 /* Restore full speculative operation to the pfn. */
2049 int clear_mce_nospec(unsigned long pfn)
2051 unsigned long addr = (unsigned long) pfn_to_kaddr(pfn);
2053 return set_memory_p(addr, 1);
2055 EXPORT_SYMBOL_GPL(clear_mce_nospec);
2056 #endif /* CONFIG_X86_64 */
2058 int set_memory_x(unsigned long addr, int numpages)
2060 if (!(__supported_pte_mask & _PAGE_NX))
2063 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
2066 int set_memory_nx(unsigned long addr, int numpages)
2068 if (!(__supported_pte_mask & _PAGE_NX))
2071 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
2074 int set_memory_ro(unsigned long addr, int numpages)
2076 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW | _PAGE_DIRTY), 0);
2079 int set_memory_rox(unsigned long addr, int numpages)
2081 pgprot_t clr = __pgprot(_PAGE_RW | _PAGE_DIRTY);
2083 if (__supported_pte_mask & _PAGE_NX)
2084 clr.pgprot |= _PAGE_NX;
2086 return change_page_attr_clear(&addr, numpages, clr, 0);
2089 int set_memory_rw(unsigned long addr, int numpages)
2091 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
2094 int set_memory_np(unsigned long addr, int numpages)
2096 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
2099 int set_memory_np_noalias(unsigned long addr, int numpages)
2101 return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
2102 __pgprot(_PAGE_PRESENT), 0,
2103 CPA_NO_CHECK_ALIAS, NULL);
2106 int set_memory_p(unsigned long addr, int numpages)
2108 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
2111 int set_memory_4k(unsigned long addr, int numpages)
2113 return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
2114 __pgprot(0), 1, 0, NULL);
2117 int set_memory_nonglobal(unsigned long addr, int numpages)
2119 return change_page_attr_clear(&addr, numpages,
2120 __pgprot(_PAGE_GLOBAL), 0);
2123 int set_memory_global(unsigned long addr, int numpages)
2125 return change_page_attr_set(&addr, numpages,
2126 __pgprot(_PAGE_GLOBAL), 0);
2130 * __set_memory_enc_pgtable() is used for the hypervisors that get
2131 * informed about "encryption" status via page tables.
2133 static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc)
2135 pgprot_t empty = __pgprot(0);
2136 struct cpa_data cpa;
2139 /* Should not be working on unaligned addresses */
2140 if (WARN_ONCE(addr & ~PAGE_MASK, "misaligned address: %#lx\n", addr))
2143 memset(&cpa, 0, sizeof(cpa));
2145 cpa.numpages = numpages;
2146 cpa.mask_set = enc ? pgprot_encrypted(empty) : pgprot_decrypted(empty);
2147 cpa.mask_clr = enc ? pgprot_decrypted(empty) : pgprot_encrypted(empty);
2148 cpa.pgd = init_mm.pgd;
2150 /* Must avoid aliasing mappings in the highmem code */
2151 kmap_flush_unused();
2154 /* Flush the caches as needed before changing the encryption attribute. */
2155 if (x86_platform.guest.enc_tlb_flush_required(enc))
2156 cpa_flush(&cpa, x86_platform.guest.enc_cache_flush_required());
2158 /* Notify hypervisor that we are about to set/clr encryption attribute. */
2159 if (!x86_platform.guest.enc_status_change_prepare(addr, numpages, enc))
2162 ret = __change_page_attr_set_clr(&cpa, 1);
2165 * After changing the encryption attribute, we need to flush TLBs again
2166 * in case any speculative TLB caching occurred (but no need to flush
2167 * caches again). We could just use cpa_flush_all(), but in case TLB
2168 * flushing gets optimized in the cpa_flush() path use the same logic
2176 /* Notify hypervisor that we have successfully set/clr encryption attribute. */
2177 if (!x86_platform.guest.enc_status_change_finish(addr, numpages, enc))
2183 WARN_ONCE(1, "CPA VMM failure to convert memory (addr=%p, numpages=%d) to %s.\n",
2184 (void *)addr, numpages, enc ? "private" : "shared");
2189 static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc)
2191 if (cc_platform_has(CC_ATTR_MEM_ENCRYPT))
2192 return __set_memory_enc_pgtable(addr, numpages, enc);
2197 int set_memory_encrypted(unsigned long addr, int numpages)
2199 return __set_memory_enc_dec(addr, numpages, true);
2201 EXPORT_SYMBOL_GPL(set_memory_encrypted);
2203 int set_memory_decrypted(unsigned long addr, int numpages)
2205 return __set_memory_enc_dec(addr, numpages, false);
2207 EXPORT_SYMBOL_GPL(set_memory_decrypted);
2209 int set_pages_uc(struct page *page, int numpages)
2211 unsigned long addr = (unsigned long)page_address(page);
2213 return set_memory_uc(addr, numpages);
2215 EXPORT_SYMBOL(set_pages_uc);
2217 static int _set_pages_array(struct page **pages, int numpages,
2218 enum page_cache_mode new_type)
2220 unsigned long start;
2222 enum page_cache_mode set_type;
2227 for (i = 0; i < numpages; i++) {
2228 if (PageHighMem(pages[i]))
2230 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
2231 end = start + PAGE_SIZE;
2232 if (memtype_reserve(start, end, new_type, NULL))
2236 /* If WC, set to UC- first and then WC */
2237 set_type = (new_type == _PAGE_CACHE_MODE_WC) ?
2238 _PAGE_CACHE_MODE_UC_MINUS : new_type;
2240 ret = cpa_set_pages_array(pages, numpages,
2241 cachemode2pgprot(set_type));
2242 if (!ret && new_type == _PAGE_CACHE_MODE_WC)
2243 ret = change_page_attr_set_clr(NULL, numpages,
2245 _PAGE_CACHE_MODE_WC),
2246 __pgprot(_PAGE_CACHE_MASK),
2247 0, CPA_PAGES_ARRAY, pages);
2250 return 0; /* Success */
2253 for (i = 0; i < free_idx; i++) {
2254 if (PageHighMem(pages[i]))
2256 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
2257 end = start + PAGE_SIZE;
2258 memtype_free(start, end);
2263 int set_pages_array_uc(struct page **pages, int numpages)
2265 return _set_pages_array(pages, numpages, _PAGE_CACHE_MODE_UC_MINUS);
2267 EXPORT_SYMBOL(set_pages_array_uc);
2269 int set_pages_array_wc(struct page **pages, int numpages)
2271 return _set_pages_array(pages, numpages, _PAGE_CACHE_MODE_WC);
2273 EXPORT_SYMBOL(set_pages_array_wc);
2275 int set_pages_wb(struct page *page, int numpages)
2277 unsigned long addr = (unsigned long)page_address(page);
2279 return set_memory_wb(addr, numpages);
2281 EXPORT_SYMBOL(set_pages_wb);
2283 int set_pages_array_wb(struct page **pages, int numpages)
2286 unsigned long start;
2290 /* WB cache mode is hard wired to all cache attribute bits being 0 */
2291 retval = cpa_clear_pages_array(pages, numpages,
2292 __pgprot(_PAGE_CACHE_MASK));
2296 for (i = 0; i < numpages; i++) {
2297 if (PageHighMem(pages[i]))
2299 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
2300 end = start + PAGE_SIZE;
2301 memtype_free(start, end);
2306 EXPORT_SYMBOL(set_pages_array_wb);
2308 int set_pages_ro(struct page *page, int numpages)
2310 unsigned long addr = (unsigned long)page_address(page);
2312 return set_memory_ro(addr, numpages);
2315 int set_pages_rw(struct page *page, int numpages)
2317 unsigned long addr = (unsigned long)page_address(page);
2319 return set_memory_rw(addr, numpages);
2322 static int __set_pages_p(struct page *page, int numpages)
2324 unsigned long tempaddr = (unsigned long) page_address(page);
2325 struct cpa_data cpa = { .vaddr = &tempaddr,
2327 .numpages = numpages,
2328 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
2329 .mask_clr = __pgprot(0),
2330 .flags = CPA_NO_CHECK_ALIAS };
2333 * No alias checking needed for setting present flag. otherwise,
2334 * we may need to break large pages for 64-bit kernel text
2335 * mappings (this adds to complexity if we want to do this from
2336 * atomic context especially). Let's keep it simple!
2338 return __change_page_attr_set_clr(&cpa, 1);
2341 static int __set_pages_np(struct page *page, int numpages)
2343 unsigned long tempaddr = (unsigned long) page_address(page);
2344 struct cpa_data cpa = { .vaddr = &tempaddr,
2346 .numpages = numpages,
2347 .mask_set = __pgprot(0),
2348 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
2349 .flags = CPA_NO_CHECK_ALIAS };
2352 * No alias checking needed for setting not present flag. otherwise,
2353 * we may need to break large pages for 64-bit kernel text
2354 * mappings (this adds to complexity if we want to do this from
2355 * atomic context especially). Let's keep it simple!
2357 return __change_page_attr_set_clr(&cpa, 1);
2360 int set_direct_map_invalid_noflush(struct page *page)
2362 return __set_pages_np(page, 1);
2365 int set_direct_map_default_noflush(struct page *page)
2367 return __set_pages_p(page, 1);
2370 #ifdef CONFIG_DEBUG_PAGEALLOC
2371 void __kernel_map_pages(struct page *page, int numpages, int enable)
2373 if (PageHighMem(page))
2376 debug_check_no_locks_freed(page_address(page),
2377 numpages * PAGE_SIZE);
2381 * The return value is ignored as the calls cannot fail.
2382 * Large pages for identity mappings are not used at boot time
2383 * and hence no memory allocations during large page split.
2386 __set_pages_p(page, numpages);
2388 __set_pages_np(page, numpages);
2391 * We should perform an IPI and flush all tlbs,
2392 * but that can deadlock->flush only current cpu.
2393 * Preemption needs to be disabled around __flush_tlb_all() due to
2394 * CR3 reload in __native_flush_tlb().
2400 arch_flush_lazy_mmu_mode();
2402 #endif /* CONFIG_DEBUG_PAGEALLOC */
2404 bool kernel_page_present(struct page *page)
2409 if (PageHighMem(page))
2412 pte = lookup_address((unsigned long)page_address(page), &level);
2413 return (pte_val(*pte) & _PAGE_PRESENT);
2416 int __init kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address,
2417 unsigned numpages, unsigned long page_flags)
2419 int retval = -EINVAL;
2421 struct cpa_data cpa = {
2425 .numpages = numpages,
2426 .mask_set = __pgprot(0),
2427 .mask_clr = __pgprot(~page_flags & (_PAGE_NX|_PAGE_RW)),
2428 .flags = CPA_NO_CHECK_ALIAS,
2431 WARN_ONCE(num_online_cpus() > 1, "Don't call after initializing SMP");
2433 if (!(__supported_pte_mask & _PAGE_NX))
2436 if (!(page_flags & _PAGE_ENC))
2437 cpa.mask_clr = pgprot_encrypted(cpa.mask_clr);
2439 cpa.mask_set = __pgprot(_PAGE_PRESENT | page_flags);
2441 retval = __change_page_attr_set_clr(&cpa, 1);
2449 * __flush_tlb_all() flushes mappings only on current CPU and hence this
2450 * function shouldn't be used in an SMP environment. Presently, it's used only
2451 * during boot (way before smp_init()) by EFI subsystem and hence is ok.
2453 int __init kernel_unmap_pages_in_pgd(pgd_t *pgd, unsigned long address,
2454 unsigned long numpages)
2459 * The typical sequence for unmapping is to find a pte through
2460 * lookup_address_in_pgd() (ideally, it should never return NULL because
2461 * the address is already mapped) and change its protections. As pfn is
2462 * the *target* of a mapping, it's not useful while unmapping.
2464 struct cpa_data cpa = {
2468 .numpages = numpages,
2469 .mask_set = __pgprot(0),
2470 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
2471 .flags = CPA_NO_CHECK_ALIAS,
2474 WARN_ONCE(num_online_cpus() > 1, "Don't call after initializing SMP");
2476 retval = __change_page_attr_set_clr(&cpa, 1);
2483 * The testcases use internal knowledge of the implementation that shouldn't
2484 * be exposed to the rest of the kernel. Include these directly here.
2486 #ifdef CONFIG_CPA_DEBUG
2487 #include "cpa-test.c"