1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2002 Andi Kleen, SuSE Labs. 4 * Thanks to Ben LaHaise for precious feedback. 5 */ 6 #include <linux/highmem.h> 7 #include <linux/memblock.h> 8 #include <linux/sched.h> 9 #include <linux/mm.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> 25 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 36 #include "../mm_internal.h" 37 38 /* 39 * The current flushing context - we pass it instead of 5 arguments: 40 */ 41 struct cpa_data { 42 unsigned long *vaddr; 43 pgd_t *pgd; 44 pgprot_t mask_set; 45 pgprot_t mask_clr; 46 unsigned long numpages; 47 unsigned long curpage; 48 unsigned long pfn; 49 unsigned int flags; 50 unsigned int force_split : 1, 51 force_static_prot : 1, 52 force_flush_all : 1; 53 struct page **pages; 54 }; 55 56 enum cpa_warn { 57 CPA_CONFLICT, 58 CPA_PROTECT, 59 CPA_DETECT, 60 }; 61 62 static const int cpa_warn_level = CPA_PROTECT; 63 64 /* 65 * Serialize cpa() using cpa_lock so that we don't allow any other cpu, with 66 * stale large tlb entries, to change the page attribute in parallel to some 67 * other cpu splitting a large page entry along with changing the attribute. 68 */ 69 static DEFINE_SPINLOCK(cpa_lock); 70 71 #define CPA_FLUSHTLB 0x01 72 #define CPA_ARRAY 0x02 73 #define CPA_PAGES_ARRAY 0x04 74 #define CPA_NO_CHECK_ALIAS 0x08 /* Do not search for aliases */ 75 #define CPA_COLLAPSE 0x10 /* try to collapse large pages */ 76 #define CPA_DEBUG_PAGEALLOC 0x20 77 78 static inline pgprot_t cachemode2pgprot(enum page_cache_mode pcm) 79 { 80 return __pgprot(cachemode2protval(pcm)); 81 } 82 83 #ifdef CONFIG_PROC_FS 84 static unsigned long direct_pages_count[PG_LEVEL_NUM]; 85 86 void update_page_count(int level, unsigned long pages) 87 { 88 /* Protect against CPA */ 89 guard(spinlock)(&pgd_lock); 90 direct_pages_count[level] += pages; 91 } 92 93 static void split_page_count(int level) 94 { 95 if (direct_pages_count[level] == 0) 96 return; 97 98 direct_pages_count[level]--; 99 if (system_state == SYSTEM_RUNNING) { 100 if (level == PG_LEVEL_2M) 101 count_vm_event(DIRECT_MAP_LEVEL2_SPLIT); 102 else if (level == PG_LEVEL_1G) 103 count_vm_event(DIRECT_MAP_LEVEL3_SPLIT); 104 } 105 direct_pages_count[level - 1] += PTRS_PER_PTE; 106 } 107 108 static void collapse_page_count(int level) 109 { 110 direct_pages_count[level]++; 111 if (system_state == SYSTEM_RUNNING) { 112 if (level == PG_LEVEL_2M) 113 count_vm_event(DIRECT_MAP_LEVEL2_COLLAPSE); 114 else if (level == PG_LEVEL_1G) 115 count_vm_event(DIRECT_MAP_LEVEL3_COLLAPSE); 116 } 117 direct_pages_count[level - 1] -= PTRS_PER_PTE; 118 } 119 120 void arch_report_meminfo(struct seq_file *m) 121 { 122 seq_printf(m, "DirectMap4k: %8lu kB\n", 123 direct_pages_count[PG_LEVEL_4K] << 2); 124 #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE) 125 seq_printf(m, "DirectMap2M: %8lu kB\n", 126 direct_pages_count[PG_LEVEL_2M] << 11); 127 #else 128 seq_printf(m, "DirectMap4M: %8lu kB\n", 129 direct_pages_count[PG_LEVEL_2M] << 12); 130 #endif 131 if (direct_gbpages) 132 seq_printf(m, "DirectMap1G: %8lu kB\n", 133 direct_pages_count[PG_LEVEL_1G] << 20); 134 } 135 #else 136 static inline void split_page_count(int level) { } 137 static inline void collapse_page_count(int level) { } 138 #endif 139 140 #ifdef CONFIG_X86_CPA_STATISTICS 141 142 static unsigned long cpa_1g_checked; 143 static unsigned long cpa_1g_sameprot; 144 static unsigned long cpa_1g_preserved; 145 static unsigned long cpa_2m_checked; 146 static unsigned long cpa_2m_sameprot; 147 static unsigned long cpa_2m_preserved; 148 static unsigned long cpa_4k_install; 149 150 static inline void cpa_inc_1g_checked(void) 151 { 152 cpa_1g_checked++; 153 } 154 155 static inline void cpa_inc_2m_checked(void) 156 { 157 cpa_2m_checked++; 158 } 159 160 static inline void cpa_inc_4k_install(void) 161 { 162 data_race(cpa_4k_install++); 163 } 164 165 static inline void cpa_inc_lp_sameprot(int level) 166 { 167 if (level == PG_LEVEL_1G) 168 cpa_1g_sameprot++; 169 else 170 cpa_2m_sameprot++; 171 } 172 173 static inline void cpa_inc_lp_preserved(int level) 174 { 175 if (level == PG_LEVEL_1G) 176 cpa_1g_preserved++; 177 else 178 cpa_2m_preserved++; 179 } 180 181 static int cpastats_show(struct seq_file *m, void *p) 182 { 183 seq_printf(m, "1G pages checked: %16lu\n", cpa_1g_checked); 184 seq_printf(m, "1G pages sameprot: %16lu\n", cpa_1g_sameprot); 185 seq_printf(m, "1G pages preserved: %16lu\n", cpa_1g_preserved); 186 seq_printf(m, "2M pages checked: %16lu\n", cpa_2m_checked); 187 seq_printf(m, "2M pages sameprot: %16lu\n", cpa_2m_sameprot); 188 seq_printf(m, "2M pages preserved: %16lu\n", cpa_2m_preserved); 189 seq_printf(m, "4K pages set-checked: %16lu\n", cpa_4k_install); 190 return 0; 191 } 192 193 static int cpastats_open(struct inode *inode, struct file *file) 194 { 195 return single_open(file, cpastats_show, NULL); 196 } 197 198 static const struct file_operations cpastats_fops = { 199 .open = cpastats_open, 200 .read = seq_read, 201 .llseek = seq_lseek, 202 .release = single_release, 203 }; 204 205 static int __init cpa_stats_init(void) 206 { 207 debugfs_create_file("cpa_stats", S_IRUSR, arch_debugfs_dir, NULL, 208 &cpastats_fops); 209 return 0; 210 } 211 late_initcall(cpa_stats_init); 212 #else 213 static inline void cpa_inc_1g_checked(void) { } 214 static inline void cpa_inc_2m_checked(void) { } 215 static inline void cpa_inc_4k_install(void) { } 216 static inline void cpa_inc_lp_sameprot(int level) { } 217 static inline void cpa_inc_lp_preserved(int level) { } 218 #endif 219 220 221 static inline int 222 within(unsigned long addr, unsigned long start, unsigned long end) 223 { 224 return addr >= start && addr < end; 225 } 226 227 #ifdef CONFIG_X86_64 228 229 static inline int 230 within_inclusive(unsigned long addr, unsigned long start, unsigned long end) 231 { 232 return addr >= start && addr <= end; 233 } 234 235 /* 236 * The kernel image is mapped into two places in the virtual address space 237 * (addresses without KASLR, of course): 238 * 239 * 1. The kernel direct map (0xffff880000000000) 240 * 2. The "high kernel map" (0xffffffff81000000) 241 * 242 * We actually execute out of #2. If we get the address of a kernel symbol, it 243 * points to #2, but almost all physical-to-virtual translations point to #1. 244 * 245 * This is so that we can have both a directmap of all physical memory *and* 246 * take full advantage of the limited (s32) immediate addressing range (2G) 247 * of x86_64. 248 * 249 * See Documentation/arch/x86/x86_64/mm.rst for more detail. 250 */ 251 252 static inline unsigned long highmap_start_pfn(void) 253 { 254 return __pa_symbol(_text) >> PAGE_SHIFT; 255 } 256 257 static inline unsigned long highmap_end_pfn(void) 258 { 259 /* Do not reference physical address outside the kernel. */ 260 return __pa_symbol(roundup(_brk_end, PMD_SIZE) - 1) >> PAGE_SHIFT; 261 } 262 263 static bool __cpa_pfn_in_highmap(unsigned long pfn) 264 { 265 /* 266 * Kernel text has an alias mapping at a high address, known 267 * here as "highmap". 268 */ 269 return within_inclusive(pfn, highmap_start_pfn(), highmap_end_pfn()); 270 } 271 272 #else 273 274 static bool __cpa_pfn_in_highmap(unsigned long pfn) 275 { 276 /* There is no highmap on 32-bit */ 277 return false; 278 } 279 280 #endif 281 282 /* 283 * See set_mce_nospec(). 284 * 285 * Machine check recovery code needs to change cache mode of poisoned pages to 286 * UC to avoid speculative access logging another error. But passing the 287 * address of the 1:1 mapping to set_memory_uc() is a fine way to encourage a 288 * speculative access. So we cheat and flip the top bit of the address. This 289 * works fine for the code that updates the page tables. But at the end of the 290 * process we need to flush the TLB and cache and the non-canonical address 291 * causes a #GP fault when used by the INVLPG and CLFLUSH instructions. 292 * 293 * But in the common case we already have a canonical address. This code 294 * will fix the top bit if needed and is a no-op otherwise. 295 */ 296 static inline unsigned long fix_addr(unsigned long addr) 297 { 298 #ifdef CONFIG_X86_64 299 return (long)(addr << 1) >> 1; 300 #else 301 return addr; 302 #endif 303 } 304 305 static unsigned long __cpa_addr(struct cpa_data *cpa, unsigned long idx) 306 { 307 if (cpa->flags & CPA_PAGES_ARRAY) { 308 struct page *page = cpa->pages[idx]; 309 310 if (unlikely(PageHighMem(page))) 311 return 0; 312 313 return (unsigned long)page_address(page); 314 } 315 316 if (cpa->flags & CPA_ARRAY) 317 return cpa->vaddr[idx]; 318 319 return *cpa->vaddr + idx * PAGE_SIZE; 320 } 321 322 /* 323 * Flushing functions 324 */ 325 326 static void clflush_cache_range_opt(void *vaddr, unsigned int size) 327 { 328 const unsigned long clflush_size = boot_cpu_data.x86_clflush_size; 329 void *p = (void *)((unsigned long)vaddr & ~(clflush_size - 1)); 330 void *vend = vaddr + size; 331 332 if (p >= vend) 333 return; 334 335 for (; p < vend; p += clflush_size) 336 clflushopt(p); 337 } 338 339 /** 340 * clflush_cache_range - flush a cache range with clflush 341 * @vaddr: virtual start address 342 * @size: number of bytes to flush 343 * 344 * CLFLUSHOPT is an unordered instruction which needs fencing with MFENCE or 345 * SFENCE to avoid ordering issues. 346 */ 347 void clflush_cache_range(void *vaddr, unsigned int size) 348 { 349 mb(); 350 clflush_cache_range_opt(vaddr, size); 351 mb(); 352 } 353 EXPORT_SYMBOL_GPL(clflush_cache_range); 354 355 #ifdef CONFIG_ARCH_HAS_PMEM_API 356 void arch_invalidate_pmem(void *addr, size_t size) 357 { 358 clflush_cache_range(addr, size); 359 } 360 EXPORT_SYMBOL_GPL(arch_invalidate_pmem); 361 #endif 362 363 #ifdef CONFIG_ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION 364 bool cpu_cache_has_invalidate_memregion(void) 365 { 366 return !cpu_feature_enabled(X86_FEATURE_HYPERVISOR); 367 } 368 EXPORT_SYMBOL_NS_GPL(cpu_cache_has_invalidate_memregion, "DEVMEM"); 369 370 int cpu_cache_invalidate_memregion(phys_addr_t start, size_t len) 371 { 372 if (WARN_ON_ONCE(!cpu_cache_has_invalidate_memregion())) 373 return -ENXIO; 374 wbinvd_on_all_cpus(); 375 return 0; 376 } 377 EXPORT_SYMBOL_NS_GPL(cpu_cache_invalidate_memregion, "DEVMEM"); 378 #endif 379 380 static void __cpa_flush_all(void *arg) 381 { 382 unsigned long cache = (unsigned long)arg; 383 384 /* 385 * Flush all to work around Errata in early athlons regarding 386 * large page flushing. 387 */ 388 __flush_tlb_all(); 389 390 if (cache && boot_cpu_data.x86 >= 4) 391 wbinvd(); 392 } 393 394 static void cpa_flush_all(unsigned long cache) 395 { 396 BUG_ON(irqs_disabled() && !early_boot_irqs_disabled); 397 398 on_each_cpu(__cpa_flush_all, (void *) cache, 1); 399 } 400 401 static void __cpa_flush_tlb(void *data) 402 { 403 struct cpa_data *cpa = data; 404 unsigned int i; 405 406 for (i = 0; i < cpa->numpages; i++) 407 flush_tlb_one_kernel(fix_addr(__cpa_addr(cpa, i))); 408 } 409 410 static int collapse_large_pages(unsigned long addr, struct list_head *pgtables); 411 412 static void cpa_collapse_large_pages(struct cpa_data *cpa) 413 { 414 unsigned long start, addr, end; 415 struct ptdesc *ptdesc, *tmp; 416 LIST_HEAD(pgtables); 417 int collapsed = 0; 418 int i; 419 420 guard(spinlock)(&cpa_lock); 421 422 if (cpa->flags & (CPA_PAGES_ARRAY | CPA_ARRAY)) { 423 for (i = 0; i < cpa->numpages; i++) 424 collapsed += collapse_large_pages(__cpa_addr(cpa, i), 425 &pgtables); 426 } else { 427 addr = __cpa_addr(cpa, 0); 428 start = addr & PMD_MASK; 429 end = addr + PAGE_SIZE * cpa->numpages; 430 431 for (addr = start; within(addr, start, end); addr += PMD_SIZE) 432 collapsed += collapse_large_pages(addr, &pgtables); 433 } 434 435 if (!collapsed) 436 return; 437 438 flush_tlb_all(); 439 440 list_for_each_entry_safe(ptdesc, tmp, &pgtables, pt_list) { 441 list_del(&ptdesc->pt_list); 442 pagetable_free(ptdesc); 443 } 444 } 445 446 static void cpa_flush(struct cpa_data *cpa, int cache) 447 { 448 unsigned int i; 449 450 BUG_ON(irqs_disabled() && !early_boot_irqs_disabled); 451 452 if (cache && !cpu_feature_enabled(X86_FEATURE_CLFLUSH)) { 453 cpa_flush_all(cache); 454 goto collapse_large_pages; 455 } 456 457 if (cpa->force_flush_all || cpa->numpages > tlb_single_page_flush_ceiling) 458 flush_tlb_all(); 459 else 460 on_each_cpu(__cpa_flush_tlb, cpa, 1); 461 462 if (!cache) 463 goto collapse_large_pages; 464 465 mb(); 466 for (i = 0; i < cpa->numpages; i++) { 467 unsigned long addr = __cpa_addr(cpa, i); 468 unsigned int level; 469 470 pte_t *pte = lookup_address(addr, &level); 471 472 /* 473 * Only flush present addresses: 474 */ 475 if (pte && (pte_val(*pte) & _PAGE_PRESENT)) 476 clflush_cache_range_opt((void *)fix_addr(addr), PAGE_SIZE); 477 } 478 mb(); 479 480 collapse_large_pages: 481 if (cpa->flags & CPA_COLLAPSE) 482 cpa_collapse_large_pages(cpa); 483 } 484 485 static bool overlaps(unsigned long r1_start, unsigned long r1_end, 486 unsigned long r2_start, unsigned long r2_end) 487 { 488 return (r1_start <= r2_end && r1_end >= r2_start) || 489 (r2_start <= r1_end && r2_end >= r1_start); 490 } 491 492 #ifdef CONFIG_PCI_BIOS 493 /* 494 * The BIOS area between 640k and 1Mb needs to be executable for PCI BIOS 495 * based config access (CONFIG_PCI_GOBIOS) support. 496 */ 497 #define BIOS_PFN PFN_DOWN(BIOS_BEGIN) 498 #define BIOS_PFN_END PFN_DOWN(BIOS_END - 1) 499 500 static pgprotval_t protect_pci_bios(unsigned long spfn, unsigned long epfn) 501 { 502 if (pcibios_enabled && overlaps(spfn, epfn, BIOS_PFN, BIOS_PFN_END)) 503 return _PAGE_NX; 504 return 0; 505 } 506 #else 507 static pgprotval_t protect_pci_bios(unsigned long spfn, unsigned long epfn) 508 { 509 return 0; 510 } 511 #endif 512 513 /* 514 * The .rodata section needs to be read-only. Using the pfn catches all 515 * aliases. This also includes __ro_after_init, so do not enforce until 516 * kernel_set_to_readonly is true. 517 */ 518 static pgprotval_t protect_rodata(unsigned long spfn, unsigned long epfn) 519 { 520 unsigned long epfn_ro, spfn_ro = PFN_DOWN(__pa_symbol(__start_rodata)); 521 522 /* 523 * Note: __end_rodata is at page aligned and not inclusive, so 524 * subtract 1 to get the last enforced PFN in the rodata area. 525 */ 526 epfn_ro = PFN_DOWN(__pa_symbol(__end_rodata)) - 1; 527 528 if (kernel_set_to_readonly && overlaps(spfn, epfn, spfn_ro, epfn_ro)) 529 return _PAGE_RW; 530 return 0; 531 } 532 533 /* 534 * Protect kernel text against becoming non executable by forbidding 535 * _PAGE_NX. This protects only the high kernel mapping (_text -> _etext) 536 * out of which the kernel actually executes. Do not protect the low 537 * mapping. 538 * 539 * This does not cover __inittext since that is gone after boot. 540 */ 541 static pgprotval_t protect_kernel_text(unsigned long start, unsigned long end) 542 { 543 unsigned long t_end = (unsigned long)_etext - 1; 544 unsigned long t_start = (unsigned long)_text; 545 546 if (overlaps(start, end, t_start, t_end)) 547 return _PAGE_NX; 548 return 0; 549 } 550 551 #if defined(CONFIG_X86_64) 552 /* 553 * Once the kernel maps the text as RO (kernel_set_to_readonly is set), 554 * kernel text mappings for the large page aligned text, rodata sections 555 * will be always read-only. For the kernel identity mappings covering the 556 * holes caused by this alignment can be anything that user asks. 557 * 558 * This will preserve the large page mappings for kernel text/data at no 559 * extra cost. 560 */ 561 static pgprotval_t protect_kernel_text_ro(unsigned long start, 562 unsigned long end) 563 { 564 unsigned long t_end = (unsigned long)__end_rodata_hpage_align - 1; 565 unsigned long t_start = (unsigned long)_text; 566 unsigned int level; 567 568 if (!kernel_set_to_readonly || !overlaps(start, end, t_start, t_end)) 569 return 0; 570 /* 571 * Don't enforce the !RW mapping for the kernel text mapping, if 572 * the current mapping is already using small page mapping. No 573 * need to work hard to preserve large page mappings in this case. 574 * 575 * This also fixes the Linux Xen paravirt guest boot failure caused 576 * by unexpected read-only mappings for kernel identity 577 * mappings. In this paravirt guest case, the kernel text mapping 578 * and the kernel identity mapping share the same page-table pages, 579 * so the protections for kernel text and identity mappings have to 580 * be the same. 581 */ 582 if (lookup_address(start, &level) && (level != PG_LEVEL_4K)) 583 return _PAGE_RW; 584 return 0; 585 } 586 #else 587 static pgprotval_t protect_kernel_text_ro(unsigned long start, 588 unsigned long end) 589 { 590 return 0; 591 } 592 #endif 593 594 static inline bool conflicts(pgprot_t prot, pgprotval_t val) 595 { 596 return (pgprot_val(prot) & ~val) != pgprot_val(prot); 597 } 598 599 static inline void check_conflict(int warnlvl, pgprot_t prot, pgprotval_t val, 600 unsigned long start, unsigned long end, 601 unsigned long pfn, const char *txt) 602 { 603 static const char *lvltxt[] = { 604 [CPA_CONFLICT] = "conflict", 605 [CPA_PROTECT] = "protect", 606 [CPA_DETECT] = "detect", 607 }; 608 609 if (warnlvl > cpa_warn_level || !conflicts(prot, val)) 610 return; 611 612 pr_warn("CPA %8s %10s: 0x%016lx - 0x%016lx PFN %lx req %016llx prevent %016llx\n", 613 lvltxt[warnlvl], txt, start, end, pfn, (unsigned long long)pgprot_val(prot), 614 (unsigned long long)val); 615 } 616 617 /* 618 * Certain areas of memory on x86 require very specific protection flags, 619 * for example the BIOS area or kernel text. Callers don't always get this 620 * right (again, ioremap() on BIOS memory is not uncommon) so this function 621 * checks and fixes these known static required protection bits. 622 */ 623 static inline pgprot_t static_protections(pgprot_t prot, unsigned long start, 624 unsigned long pfn, unsigned long npg, 625 unsigned long lpsize, int warnlvl) 626 { 627 pgprotval_t forbidden, res; 628 unsigned long end; 629 630 /* 631 * There is no point in checking RW/NX conflicts when the requested 632 * mapping is setting the page !PRESENT. 633 */ 634 if (!(pgprot_val(prot) & _PAGE_PRESENT)) 635 return prot; 636 637 /* Operate on the virtual address */ 638 end = start + npg * PAGE_SIZE - 1; 639 640 res = protect_kernel_text(start, end); 641 check_conflict(warnlvl, prot, res, start, end, pfn, "Text NX"); 642 forbidden = res; 643 644 /* 645 * Special case to preserve a large page. If the change spawns the 646 * full large page mapping then there is no point to split it 647 * up. Happens with ftrace and is going to be removed once ftrace 648 * switched to text_poke(). 649 */ 650 if (lpsize != (npg * PAGE_SIZE) || (start & (lpsize - 1))) { 651 res = protect_kernel_text_ro(start, end); 652 check_conflict(warnlvl, prot, res, start, end, pfn, "Text RO"); 653 forbidden |= res; 654 } 655 656 /* Check the PFN directly */ 657 res = protect_pci_bios(pfn, pfn + npg - 1); 658 check_conflict(warnlvl, prot, res, start, end, pfn, "PCIBIOS NX"); 659 forbidden |= res; 660 661 res = protect_rodata(pfn, pfn + npg - 1); 662 check_conflict(warnlvl, prot, res, start, end, pfn, "Rodata RO"); 663 forbidden |= res; 664 665 return __pgprot(pgprot_val(prot) & ~forbidden); 666 } 667 668 /* 669 * Validate strict W^X semantics. 670 */ 671 static inline pgprot_t verify_rwx(pgprot_t old, pgprot_t new, unsigned long start, 672 unsigned long pfn, unsigned long npg, 673 bool nx, bool rw) 674 { 675 unsigned long end; 676 677 /* 678 * 32-bit has some unfixable W+X issues, like EFI code 679 * and writeable data being in the same page. Disable 680 * detection and enforcement there. 681 */ 682 if (IS_ENABLED(CONFIG_X86_32)) 683 return new; 684 685 /* Only verify when NX is supported: */ 686 if (!(__supported_pte_mask & _PAGE_NX)) 687 return new; 688 689 if (!((pgprot_val(old) ^ pgprot_val(new)) & (_PAGE_RW | _PAGE_NX))) 690 return new; 691 692 if ((pgprot_val(new) & (_PAGE_RW | _PAGE_NX)) != _PAGE_RW) 693 return new; 694 695 /* Non-leaf translation entries can disable writing or execution. */ 696 if (!rw || nx) 697 return new; 698 699 end = start + npg * PAGE_SIZE - 1; 700 WARN_ONCE(1, "CPA detected W^X violation: %016llx -> %016llx range: 0x%016lx - 0x%016lx PFN %lx\n", 701 (unsigned long long)pgprot_val(old), 702 (unsigned long long)pgprot_val(new), 703 start, end, pfn); 704 705 /* 706 * For now, allow all permission change attempts by returning the 707 * attempted permissions. This can 'return old' to actively 708 * refuse the permission change at a later time. 709 */ 710 return new; 711 } 712 713 /* 714 * Lookup the page table entry for a virtual address in a specific pgd. 715 * Return a pointer to the entry (or NULL if the entry does not exist), 716 * the level of the entry, and the effective NX and RW bits of all 717 * page table levels. 718 */ 719 pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned long address, 720 unsigned int *level, bool *nx, bool *rw) 721 { 722 p4d_t *p4d; 723 pud_t *pud; 724 pmd_t *pmd; 725 726 *level = PG_LEVEL_256T; 727 *nx = false; 728 *rw = true; 729 730 if (pgd_none(*pgd)) 731 return NULL; 732 733 *level = PG_LEVEL_512G; 734 *nx |= pgd_flags(*pgd) & _PAGE_NX; 735 *rw &= pgd_flags(*pgd) & _PAGE_RW; 736 737 p4d = p4d_offset(pgd, address); 738 if (p4d_none(*p4d)) 739 return NULL; 740 741 if (p4d_leaf(*p4d) || !p4d_present(*p4d)) 742 return (pte_t *)p4d; 743 744 *level = PG_LEVEL_1G; 745 *nx |= p4d_flags(*p4d) & _PAGE_NX; 746 *rw &= p4d_flags(*p4d) & _PAGE_RW; 747 748 pud = pud_offset(p4d, address); 749 if (pud_none(*pud)) 750 return NULL; 751 752 if (pud_leaf(*pud) || !pud_present(*pud)) 753 return (pte_t *)pud; 754 755 *level = PG_LEVEL_2M; 756 *nx |= pud_flags(*pud) & _PAGE_NX; 757 *rw &= pud_flags(*pud) & _PAGE_RW; 758 759 pmd = pmd_offset(pud, address); 760 if (pmd_none(*pmd)) 761 return NULL; 762 763 if (pmd_leaf(*pmd) || !pmd_present(*pmd)) 764 return (pte_t *)pmd; 765 766 *level = PG_LEVEL_4K; 767 *nx |= pmd_flags(*pmd) & _PAGE_NX; 768 *rw &= pmd_flags(*pmd) & _PAGE_RW; 769 770 return pte_offset_kernel(pmd, address); 771 } 772 773 /* 774 * Lookup the page table entry for a virtual address in a specific pgd. 775 * Return a pointer to the entry and the level of the mapping. 776 */ 777 pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address, 778 unsigned int *level) 779 { 780 bool nx, rw; 781 782 return lookup_address_in_pgd_attr(pgd, address, level, &nx, &rw); 783 } 784 785 /* 786 * Lookup the page table entry for a virtual address. Return a pointer 787 * to the entry and the level of the mapping. 788 * 789 * Note: the function returns p4d, pud or pmd either when the entry is marked 790 * large or when the present bit is not set. Otherwise it returns NULL. 791 */ 792 pte_t *lookup_address(unsigned long address, unsigned int *level) 793 { 794 return lookup_address_in_pgd(pgd_offset_k(address), address, level); 795 } 796 EXPORT_SYMBOL_GPL(lookup_address); 797 798 static pte_t *_lookup_address_cpa(struct cpa_data *cpa, unsigned long address, 799 unsigned int *level, bool *nx, bool *rw) 800 { 801 pgd_t *pgd; 802 803 if (!cpa->pgd) 804 pgd = pgd_offset_k(address); 805 else 806 pgd = cpa->pgd + pgd_index(address); 807 808 return lookup_address_in_pgd_attr(pgd, address, level, nx, rw); 809 } 810 811 /* 812 * Lookup the PMD entry for a virtual address. Return a pointer to the entry 813 * or NULL if not present. 814 */ 815 pmd_t *lookup_pmd_address(unsigned long address) 816 { 817 pgd_t *pgd; 818 p4d_t *p4d; 819 pud_t *pud; 820 821 pgd = pgd_offset_k(address); 822 if (pgd_none(*pgd)) 823 return NULL; 824 825 p4d = p4d_offset(pgd, address); 826 if (p4d_none(*p4d) || p4d_leaf(*p4d) || !p4d_present(*p4d)) 827 return NULL; 828 829 pud = pud_offset(p4d, address); 830 if (pud_none(*pud) || pud_leaf(*pud) || !pud_present(*pud)) 831 return NULL; 832 833 return pmd_offset(pud, address); 834 } 835 836 /* 837 * This is necessary because __pa() does not work on some 838 * kinds of memory, like vmalloc() or the alloc_remap() 839 * areas on 32-bit NUMA systems. The percpu areas can 840 * end up in this kind of memory, for instance. 841 * 842 * Note that as long as the PTEs are well-formed with correct PFNs, this 843 * works without checking the PRESENT bit in the leaf PTE. This is unlike 844 * the similar vmalloc_to_page() and derivatives. Callers may depend on 845 * this behavior. 846 * 847 * This could be optimized, but it is only used in paths that are not perf 848 * sensitive, and keeping it unoptimized should increase the testing coverage 849 * for the more obscure platforms. 850 */ 851 phys_addr_t slow_virt_to_phys(void *__virt_addr) 852 { 853 unsigned long virt_addr = (unsigned long)__virt_addr; 854 phys_addr_t phys_addr; 855 unsigned long offset; 856 enum pg_level level; 857 pte_t *pte; 858 859 pte = lookup_address(virt_addr, &level); 860 BUG_ON(!pte); 861 862 /* 863 * pXX_pfn() returns unsigned long, which must be cast to phys_addr_t 864 * before being left-shifted PAGE_SHIFT bits -- this trick is to 865 * make 32-PAE kernel work correctly. 866 */ 867 switch (level) { 868 case PG_LEVEL_1G: 869 phys_addr = (phys_addr_t)pud_pfn(*(pud_t *)pte) << PAGE_SHIFT; 870 offset = virt_addr & ~PUD_MASK; 871 break; 872 case PG_LEVEL_2M: 873 phys_addr = (phys_addr_t)pmd_pfn(*(pmd_t *)pte) << PAGE_SHIFT; 874 offset = virt_addr & ~PMD_MASK; 875 break; 876 default: 877 phys_addr = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT; 878 offset = virt_addr & ~PAGE_MASK; 879 } 880 881 return (phys_addr_t)(phys_addr | offset); 882 } 883 EXPORT_SYMBOL_GPL(slow_virt_to_phys); 884 885 /* 886 * Set the new pmd in all the pgds we know about: 887 */ 888 static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte) 889 { 890 /* change init_mm */ 891 set_pte_atomic(kpte, pte); 892 893 if (IS_ENABLED(CONFIG_X86_32)) { 894 struct ptdesc *ptdesc; 895 896 list_for_each_entry(ptdesc, &pgd_list, pt_list) { 897 pgd_t *pgd; 898 p4d_t *p4d; 899 pud_t *pud; 900 pmd_t *pmd; 901 902 pgd = (pgd_t *)ptdesc_address(ptdesc) + pgd_index(address); 903 p4d = p4d_offset(pgd, address); 904 pud = pud_offset(p4d, address); 905 pmd = pmd_offset(pud, address); 906 set_pte_atomic((pte_t *)pmd, pte); 907 } 908 } 909 } 910 911 static pgprot_t pgprot_clear_protnone_bits(pgprot_t prot) 912 { 913 /* 914 * _PAGE_GLOBAL means "global page" for present PTEs. 915 * But, it is also used to indicate _PAGE_PROTNONE 916 * for non-present PTEs. 917 * 918 * This ensures that a _PAGE_GLOBAL PTE going from 919 * present to non-present is not confused as 920 * _PAGE_PROTNONE. 921 */ 922 if (!(pgprot_val(prot) & _PAGE_PRESENT)) 923 pgprot_val(prot) &= ~_PAGE_GLOBAL; 924 925 return prot; 926 } 927 928 static int __should_split_large_page(pte_t *kpte, unsigned long address, 929 struct cpa_data *cpa) 930 { 931 unsigned long numpages, pmask, psize, lpaddr, pfn, old_pfn; 932 pgprot_t old_prot, new_prot, req_prot, chk_prot; 933 pte_t new_pte, *tmp; 934 enum pg_level level; 935 bool nx, rw; 936 937 /* 938 * Check for races, another CPU might have split this page 939 * up already: 940 */ 941 tmp = _lookup_address_cpa(cpa, address, &level, &nx, &rw); 942 if (tmp != kpte) 943 return 1; 944 945 switch (level) { 946 case PG_LEVEL_2M: 947 old_prot = pmd_pgprot(*(pmd_t *)kpte); 948 old_pfn = pmd_pfn(*(pmd_t *)kpte); 949 cpa_inc_2m_checked(); 950 break; 951 case PG_LEVEL_1G: 952 old_prot = pud_pgprot(*(pud_t *)kpte); 953 old_pfn = pud_pfn(*(pud_t *)kpte); 954 cpa_inc_1g_checked(); 955 break; 956 default: 957 return -EINVAL; 958 } 959 960 psize = page_level_size(level); 961 pmask = page_level_mask(level); 962 963 /* 964 * Calculate the number of pages, which fit into this large 965 * page starting at address: 966 */ 967 lpaddr = (address + psize) & pmask; 968 numpages = (lpaddr - address) >> PAGE_SHIFT; 969 if (numpages < cpa->numpages) 970 cpa->numpages = numpages; 971 972 /* 973 * We are safe now. Check whether the new pgprot is the same: 974 * Convert protection attributes to 4k-format, as cpa->mask* are set 975 * up accordingly. 976 */ 977 978 /* Clear PSE (aka _PAGE_PAT) and move PAT bit to correct position */ 979 req_prot = pgprot_large_2_4k(old_prot); 980 981 pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr); 982 pgprot_val(req_prot) |= pgprot_val(cpa->mask_set); 983 984 /* 985 * req_prot is in format of 4k pages. It must be converted to large 986 * page format: the caching mode includes the PAT bit located at 987 * different bit positions in the two formats. 988 */ 989 req_prot = pgprot_4k_2_large(req_prot); 990 req_prot = pgprot_clear_protnone_bits(req_prot); 991 if (pgprot_val(req_prot) & _PAGE_PRESENT) 992 pgprot_val(req_prot) |= _PAGE_PSE; 993 994 /* 995 * old_pfn points to the large page base pfn. So we need to add the 996 * offset of the virtual address: 997 */ 998 pfn = old_pfn + ((address & (psize - 1)) >> PAGE_SHIFT); 999 cpa->pfn = pfn; 1000 1001 /* 1002 * Calculate the large page base address and the number of 4K pages 1003 * in the large page 1004 */ 1005 lpaddr = address & pmask; 1006 numpages = psize >> PAGE_SHIFT; 1007 1008 /* 1009 * Sanity check that the existing mapping is correct versus the static 1010 * protections. static_protections() guards against !PRESENT, so no 1011 * extra conditional required here. 1012 */ 1013 chk_prot = static_protections(old_prot, lpaddr, old_pfn, numpages, 1014 psize, CPA_CONFLICT); 1015 1016 if (WARN_ON_ONCE(pgprot_val(chk_prot) != pgprot_val(old_prot))) { 1017 /* 1018 * Split the large page and tell the split code to 1019 * enforce static protections. 1020 */ 1021 cpa->force_static_prot = 1; 1022 return 1; 1023 } 1024 1025 /* 1026 * Optimization: If the requested pgprot is the same as the current 1027 * pgprot, then the large page can be preserved and no updates are 1028 * required independent of alignment and length of the requested 1029 * range. The above already established that the current pgprot is 1030 * correct, which in consequence makes the requested pgprot correct 1031 * as well if it is the same. The static protection scan below will 1032 * not come to a different conclusion. 1033 */ 1034 if (pgprot_val(req_prot) == pgprot_val(old_prot)) { 1035 cpa_inc_lp_sameprot(level); 1036 return 0; 1037 } 1038 1039 /* 1040 * If the requested range does not cover the full page, split it up 1041 */ 1042 if (address != lpaddr || cpa->numpages != numpages) 1043 return 1; 1044 1045 /* 1046 * Check whether the requested pgprot is conflicting with a static 1047 * protection requirement in the large page. 1048 */ 1049 new_prot = static_protections(req_prot, lpaddr, old_pfn, numpages, 1050 psize, CPA_DETECT); 1051 1052 new_prot = verify_rwx(old_prot, new_prot, lpaddr, old_pfn, numpages, 1053 nx, rw); 1054 1055 /* 1056 * If there is a conflict, split the large page. 1057 * 1058 * There used to be a 4k wise evaluation trying really hard to 1059 * preserve the large pages, but experimentation has shown, that this 1060 * does not help at all. There might be corner cases which would 1061 * preserve one large page occasionally, but it's really not worth the 1062 * extra code and cycles for the common case. 1063 */ 1064 if (pgprot_val(req_prot) != pgprot_val(new_prot)) 1065 return 1; 1066 1067 /* All checks passed. Update the large page mapping. */ 1068 new_pte = pfn_pte(old_pfn, new_prot); 1069 __set_pmd_pte(kpte, address, new_pte); 1070 cpa->flags |= CPA_FLUSHTLB; 1071 cpa_inc_lp_preserved(level); 1072 return 0; 1073 } 1074 1075 static int should_split_large_page(pte_t *kpte, unsigned long address, 1076 struct cpa_data *cpa) 1077 { 1078 if (cpa->force_split) 1079 return 1; 1080 1081 guard(spinlock)(&pgd_lock); 1082 return __should_split_large_page(kpte, address, cpa); 1083 } 1084 1085 static void split_set_pte(struct cpa_data *cpa, pte_t *pte, unsigned long pfn, 1086 pgprot_t ref_prot, unsigned long address, 1087 unsigned long size) 1088 { 1089 unsigned int npg = PFN_DOWN(size); 1090 pgprot_t prot; 1091 1092 /* 1093 * If should_split_large_page() discovered an inconsistent mapping, 1094 * remove the invalid protection in the split mapping. 1095 */ 1096 if (!cpa->force_static_prot) 1097 goto set; 1098 1099 /* Hand in lpsize = 0 to enforce the protection mechanism */ 1100 prot = static_protections(ref_prot, address, pfn, npg, 0, CPA_PROTECT); 1101 1102 if (pgprot_val(prot) == pgprot_val(ref_prot)) 1103 goto set; 1104 1105 /* 1106 * If this is splitting a PMD, fix it up. PUD splits cannot be 1107 * fixed trivially as that would require to rescan the newly 1108 * installed PMD mappings after returning from split_large_page() 1109 * so an eventual further split can allocate the necessary PTE 1110 * pages. Warn for now and revisit it in case this actually 1111 * happens. 1112 */ 1113 if (size == PAGE_SIZE) 1114 ref_prot = prot; 1115 else 1116 pr_warn_once("CPA: Cannot fixup static protections for PUD split\n"); 1117 set: 1118 set_pte(pte, pfn_pte(pfn, ref_prot)); 1119 } 1120 1121 static int 1122 __split_large_page(struct cpa_data *cpa, pte_t *kpte, unsigned long address, 1123 struct ptdesc *ptdesc) 1124 { 1125 unsigned long lpaddr, lpinc, ref_pfn, pfn, pfninc = 1; 1126 struct page *base = ptdesc_page(ptdesc); 1127 pte_t *pbase = (pte_t *)page_address(base); 1128 unsigned int i, level; 1129 pgprot_t ref_prot; 1130 bool nx, rw; 1131 pte_t *tmp; 1132 1133 guard(spinlock)(&pgd_lock); 1134 /* 1135 * Check for races, another CPU might have split this page 1136 * up for us already: 1137 */ 1138 tmp = _lookup_address_cpa(cpa, address, &level, &nx, &rw); 1139 if (tmp != kpte) 1140 return 1; 1141 1142 paravirt_alloc_pte(&init_mm, page_to_pfn(base)); 1143 1144 switch (level) { 1145 case PG_LEVEL_2M: 1146 ref_prot = pmd_pgprot(*(pmd_t *)kpte); 1147 /* 1148 * Clear PSE (aka _PAGE_PAT) and move 1149 * PAT bit to correct position. 1150 */ 1151 ref_prot = pgprot_large_2_4k(ref_prot); 1152 ref_pfn = pmd_pfn(*(pmd_t *)kpte); 1153 lpaddr = address & PMD_MASK; 1154 lpinc = PAGE_SIZE; 1155 break; 1156 1157 case PG_LEVEL_1G: 1158 ref_prot = pud_pgprot(*(pud_t *)kpte); 1159 ref_pfn = pud_pfn(*(pud_t *)kpte); 1160 pfninc = PMD_SIZE >> PAGE_SHIFT; 1161 lpaddr = address & PUD_MASK; 1162 lpinc = PMD_SIZE; 1163 /* 1164 * Clear the PSE flags if the PRESENT flag is not set 1165 * otherwise pmd_present() will return true even on a non 1166 * present pmd. 1167 */ 1168 if (!(pgprot_val(ref_prot) & _PAGE_PRESENT)) 1169 pgprot_val(ref_prot) &= ~_PAGE_PSE; 1170 break; 1171 1172 default: 1173 return 1; 1174 } 1175 1176 ref_prot = pgprot_clear_protnone_bits(ref_prot); 1177 1178 /* 1179 * Get the target pfn from the original entry: 1180 */ 1181 pfn = ref_pfn; 1182 for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc, lpaddr += lpinc) 1183 split_set_pte(cpa, pbase + i, pfn, ref_prot, lpaddr, lpinc); 1184 1185 if (virt_addr_valid(address)) { 1186 unsigned long pfn = PFN_DOWN(__pa(address)); 1187 1188 if (pfn_range_is_mapped(pfn, pfn + 1)) 1189 split_page_count(level); 1190 } 1191 1192 /* 1193 * Install the new, split up pagetable. 1194 * 1195 * We use the standard kernel pagetable protections for the new 1196 * pagetable protections, the actual ptes set above control the 1197 * primary protection behavior: 1198 */ 1199 __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE))); 1200 1201 /* 1202 * Do a global flush tlb after splitting the large page 1203 * and before we do the actual change page attribute in the PTE. 1204 * 1205 * Without this, we violate the TLB application note, that says: 1206 * "The TLBs may contain both ordinary and large-page 1207 * translations for a 4-KByte range of linear addresses. This 1208 * may occur if software modifies the paging structures so that 1209 * the page size used for the address range changes. If the two 1210 * translations differ with respect to page frame or attributes 1211 * (e.g., permissions), processor behavior is undefined and may 1212 * be implementation-specific." 1213 * 1214 * We do this global tlb flush inside the cpa_lock, so that we 1215 * don't allow any other cpu, with stale tlb entries change the 1216 * page attribute in parallel, that also falls into the 1217 * just split large page entry. 1218 */ 1219 flush_tlb_all(); 1220 1221 return 0; 1222 } 1223 1224 static int split_large_page(struct cpa_data *cpa, pte_t *kpte, 1225 unsigned long address) 1226 { 1227 struct ptdesc *ptdesc; 1228 1229 spin_unlock(&cpa_lock); 1230 ptdesc = pagetable_alloc(GFP_KERNEL, 0); 1231 spin_lock(&cpa_lock); 1232 if (!ptdesc) 1233 return -ENOMEM; 1234 1235 if (__split_large_page(cpa, kpte, address, ptdesc)) 1236 pagetable_free(ptdesc); 1237 1238 return 0; 1239 } 1240 1241 static int collapse_pmd_page(pmd_t *pmd, unsigned long addr, 1242 struct list_head *pgtables) 1243 { 1244 pmd_t _pmd, old_pmd; 1245 pte_t *pte, first; 1246 unsigned long pfn; 1247 pgprot_t pgprot; 1248 int i = 0; 1249 1250 if (!cpu_feature_enabled(X86_FEATURE_PSE)) 1251 return 0; 1252 1253 addr &= PMD_MASK; 1254 pte = pte_offset_kernel(pmd, addr); 1255 first = *pte; 1256 pfn = pte_pfn(first); 1257 1258 /* Make sure alignment is suitable */ 1259 if (PFN_PHYS(pfn) & ~PMD_MASK) 1260 return 0; 1261 1262 /* The page is 4k intentionally */ 1263 if (pte_flags(first) & _PAGE_KERNEL_4K) 1264 return 0; 1265 1266 /* Check that the rest of PTEs are compatible with the first one */ 1267 for (i = 1, pte++; i < PTRS_PER_PTE; i++, pte++) { 1268 pte_t entry = *pte; 1269 1270 if (!pte_present(entry)) 1271 return 0; 1272 if (pte_flags(entry) != pte_flags(first)) 1273 return 0; 1274 if (pte_pfn(entry) != pte_pfn(first) + i) 1275 return 0; 1276 } 1277 1278 old_pmd = *pmd; 1279 1280 /* Success: set up a large page */ 1281 pgprot = pgprot_4k_2_large(pte_pgprot(first)); 1282 pgprot_val(pgprot) |= _PAGE_PSE; 1283 _pmd = pfn_pmd(pfn, pgprot); 1284 set_pmd(pmd, _pmd); 1285 1286 /* Queue the page table to be freed after TLB flush */ 1287 list_add(&page_ptdesc(pmd_page(old_pmd))->pt_list, pgtables); 1288 1289 if (IS_ENABLED(CONFIG_X86_32)) { 1290 struct ptdesc *ptdesc; 1291 1292 /* Update all PGD tables to use the same large page */ 1293 list_for_each_entry(ptdesc, &pgd_list, pt_list) { 1294 pgd_t *pgd = (pgd_t *)ptdesc_address(ptdesc) + pgd_index(addr); 1295 p4d_t *p4d = p4d_offset(pgd, addr); 1296 pud_t *pud = pud_offset(p4d, addr); 1297 pmd_t *pmd = pmd_offset(pud, addr); 1298 /* Something is wrong if entries doesn't match */ 1299 if (WARN_ON(pmd_val(old_pmd) != pmd_val(*pmd))) 1300 continue; 1301 set_pmd(pmd, _pmd); 1302 } 1303 } 1304 1305 if (virt_addr_valid(addr) && pfn_range_is_mapped(pfn, pfn + 1)) 1306 collapse_page_count(PG_LEVEL_2M); 1307 1308 return 1; 1309 } 1310 1311 static int collapse_pud_page(pud_t *pud, unsigned long addr, 1312 struct list_head *pgtables) 1313 { 1314 unsigned long pfn; 1315 pmd_t *pmd, first; 1316 int i; 1317 1318 if (!direct_gbpages) 1319 return 0; 1320 1321 addr &= PUD_MASK; 1322 pmd = pmd_offset(pud, addr); 1323 first = *pmd; 1324 1325 /* 1326 * To restore PUD page all PMD entries must be large and 1327 * have suitable alignment 1328 */ 1329 pfn = pmd_pfn(first); 1330 if (!pmd_leaf(first) || (PFN_PHYS(pfn) & ~PUD_MASK)) 1331 return 0; 1332 1333 /* 1334 * To restore PUD page, all following PMDs must be compatible with the 1335 * first one. 1336 */ 1337 for (i = 1, pmd++; i < PTRS_PER_PMD; i++, pmd++) { 1338 pmd_t entry = *pmd; 1339 1340 if (!pmd_present(entry) || !pmd_leaf(entry)) 1341 return 0; 1342 if (pmd_flags(entry) != pmd_flags(first)) 1343 return 0; 1344 if (pmd_pfn(entry) != pmd_pfn(first) + i * PTRS_PER_PTE) 1345 return 0; 1346 } 1347 1348 /* Restore PUD page and queue page table to be freed after TLB flush */ 1349 list_add(&page_ptdesc(pud_page(*pud))->pt_list, pgtables); 1350 set_pud(pud, pfn_pud(pfn, pmd_pgprot(first))); 1351 1352 if (virt_addr_valid(addr) && pfn_range_is_mapped(pfn, pfn + 1)) 1353 collapse_page_count(PG_LEVEL_1G); 1354 1355 return 1; 1356 } 1357 1358 /* 1359 * Collapse PMD and PUD pages in the kernel mapping around the address where 1360 * possible. 1361 * 1362 * Caller must flush TLB and free page tables queued on the list before 1363 * touching the new entries. CPU must not see TLB entries of different size 1364 * with different attributes. 1365 */ 1366 static int collapse_large_pages(unsigned long addr, struct list_head *pgtables) 1367 { 1368 int collapsed; 1369 pgd_t *pgd; 1370 p4d_t *p4d; 1371 pud_t *pud; 1372 pmd_t *pmd; 1373 1374 addr &= PMD_MASK; 1375 1376 guard(spinlock)(&pgd_lock); 1377 pgd = pgd_offset_k(addr); 1378 if (pgd_none(*pgd)) 1379 return 0; 1380 p4d = p4d_offset(pgd, addr); 1381 if (p4d_none(*p4d)) 1382 return 0; 1383 pud = pud_offset(p4d, addr); 1384 if (!pud_present(*pud) || pud_leaf(*pud)) 1385 return 0; 1386 pmd = pmd_offset(pud, addr); 1387 if (!pmd_present(*pmd) || pmd_leaf(*pmd)) 1388 return 0; 1389 1390 collapsed = collapse_pmd_page(pmd, addr, pgtables); 1391 if (collapsed) 1392 collapsed += collapse_pud_page(pud, addr, pgtables); 1393 1394 return collapsed; 1395 } 1396 1397 static bool try_to_free_pte_page(pte_t *pte) 1398 { 1399 int i; 1400 1401 for (i = 0; i < PTRS_PER_PTE; i++) 1402 if (!pte_none(pte[i])) 1403 return false; 1404 1405 pte_free_kernel(&init_mm, pte); 1406 return true; 1407 } 1408 1409 static bool try_to_free_pmd_page(pmd_t *pmd) 1410 { 1411 int i; 1412 1413 for (i = 0; i < PTRS_PER_PMD; i++) 1414 if (!pmd_none(pmd[i])) 1415 return false; 1416 1417 pmd_free(&init_mm, pmd); 1418 return true; 1419 } 1420 1421 static bool unmap_pte_range(pmd_t *pmd, unsigned long start, unsigned long end) 1422 { 1423 pte_t *pte = pte_offset_kernel(pmd, start); 1424 1425 while (start < end) { 1426 set_pte(pte, __pte(0)); 1427 1428 start += PAGE_SIZE; 1429 pte++; 1430 } 1431 1432 if (try_to_free_pte_page((pte_t *)pmd_page_vaddr(*pmd))) { 1433 pmd_clear(pmd); 1434 return true; 1435 } 1436 return false; 1437 } 1438 1439 static void __unmap_pmd_range(pud_t *pud, pmd_t *pmd, 1440 unsigned long start, unsigned long end) 1441 { 1442 if (unmap_pte_range(pmd, start, end)) 1443 if (try_to_free_pmd_page(pud_pgtable(*pud))) 1444 pud_clear(pud); 1445 } 1446 1447 static void unmap_pmd_range(pud_t *pud, unsigned long start, unsigned long end) 1448 { 1449 pmd_t *pmd = pmd_offset(pud, start); 1450 1451 /* 1452 * Not on a 2MB page boundary? 1453 */ 1454 if (start & (PMD_SIZE - 1)) { 1455 unsigned long next_page = (start + PMD_SIZE) & PMD_MASK; 1456 unsigned long pre_end = min_t(unsigned long, end, next_page); 1457 1458 __unmap_pmd_range(pud, pmd, start, pre_end); 1459 1460 start = pre_end; 1461 pmd++; 1462 } 1463 1464 /* 1465 * Try to unmap in 2M chunks. 1466 */ 1467 while (end - start >= PMD_SIZE) { 1468 if (pmd_leaf(*pmd)) 1469 pmd_clear(pmd); 1470 else 1471 __unmap_pmd_range(pud, pmd, start, start + PMD_SIZE); 1472 1473 start += PMD_SIZE; 1474 pmd++; 1475 } 1476 1477 /* 1478 * 4K leftovers? 1479 */ 1480 if (start < end) 1481 return __unmap_pmd_range(pud, pmd, start, end); 1482 1483 /* 1484 * Try again to free the PMD page if haven't succeeded above. 1485 */ 1486 if (!pud_none(*pud)) 1487 if (try_to_free_pmd_page(pud_pgtable(*pud))) 1488 pud_clear(pud); 1489 } 1490 1491 static void unmap_pud_range(p4d_t *p4d, unsigned long start, unsigned long end) 1492 { 1493 pud_t *pud = pud_offset(p4d, start); 1494 1495 /* 1496 * Not on a GB page boundary? 1497 */ 1498 if (start & (PUD_SIZE - 1)) { 1499 unsigned long next_page = (start + PUD_SIZE) & PUD_MASK; 1500 unsigned long pre_end = min_t(unsigned long, end, next_page); 1501 1502 unmap_pmd_range(pud, start, pre_end); 1503 1504 start = pre_end; 1505 pud++; 1506 } 1507 1508 /* 1509 * Try to unmap in 1G chunks? 1510 */ 1511 while (end - start >= PUD_SIZE) { 1512 1513 if (pud_leaf(*pud)) 1514 pud_clear(pud); 1515 else 1516 unmap_pmd_range(pud, start, start + PUD_SIZE); 1517 1518 start += PUD_SIZE; 1519 pud++; 1520 } 1521 1522 /* 1523 * 2M leftovers? 1524 */ 1525 if (start < end) 1526 unmap_pmd_range(pud, start, end); 1527 1528 /* 1529 * No need to try to free the PUD page because we'll free it in 1530 * populate_pgd's error path 1531 */ 1532 } 1533 1534 static int alloc_pte_page(pmd_t *pmd) 1535 { 1536 pte_t *pte = pte_alloc_one_kernel(&init_mm); 1537 if (!pte) 1538 return -1; 1539 1540 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE)); 1541 return 0; 1542 } 1543 1544 static int alloc_pmd_page(pud_t *pud) 1545 { 1546 /* 1547 * Pass 0 as a placeholder for the second argument, since the 1548 * generic implementation of pmd_alloc_one() does not use it. 1549 */ 1550 pmd_t *pmd = pmd_alloc_one(&init_mm, 0); 1551 if (!pmd) 1552 return -1; 1553 1554 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE)); 1555 return 0; 1556 } 1557 1558 static void populate_pte(struct cpa_data *cpa, 1559 unsigned long start, unsigned long end, 1560 unsigned num_pages, pmd_t *pmd, pgprot_t pgprot) 1561 { 1562 pte_t *pte; 1563 1564 pte = pte_offset_kernel(pmd, start); 1565 1566 pgprot = pgprot_clear_protnone_bits(pgprot); 1567 1568 while (num_pages-- && start < end) { 1569 set_pte(pte, pfn_pte(cpa->pfn, pgprot)); 1570 1571 start += PAGE_SIZE; 1572 cpa->pfn++; 1573 pte++; 1574 } 1575 } 1576 1577 static long populate_pmd(struct cpa_data *cpa, 1578 unsigned long start, unsigned long end, 1579 unsigned num_pages, pud_t *pud, pgprot_t pgprot) 1580 { 1581 long cur_pages = 0; 1582 pmd_t *pmd; 1583 pgprot_t pmd_pgprot; 1584 1585 /* 1586 * Not on a 2M boundary? 1587 */ 1588 if (start & (PMD_SIZE - 1)) { 1589 unsigned long pre_end = start + (num_pages << PAGE_SHIFT); 1590 unsigned long next_page = (start + PMD_SIZE) & PMD_MASK; 1591 1592 pre_end = min_t(unsigned long, pre_end, next_page); 1593 cur_pages = (pre_end - start) >> PAGE_SHIFT; 1594 cur_pages = min_t(unsigned int, num_pages, cur_pages); 1595 1596 /* 1597 * Need a PTE page? 1598 */ 1599 pmd = pmd_offset(pud, start); 1600 if (pmd_none(*pmd)) 1601 if (alloc_pte_page(pmd)) 1602 return -1; 1603 1604 populate_pte(cpa, start, pre_end, cur_pages, pmd, pgprot); 1605 1606 start = pre_end; 1607 } 1608 1609 /* 1610 * We mapped them all? 1611 */ 1612 if (num_pages == cur_pages) 1613 return cur_pages; 1614 1615 pmd_pgprot = pgprot_4k_2_large(pgprot); 1616 1617 while (end - start >= PMD_SIZE) { 1618 1619 /* 1620 * We cannot use a 1G page so allocate a PMD page if needed. 1621 */ 1622 if (pud_none(*pud)) 1623 if (alloc_pmd_page(pud)) 1624 return -1; 1625 1626 pmd = pmd_offset(pud, start); 1627 1628 set_pmd(pmd, pmd_mkhuge(pfn_pmd(cpa->pfn, 1629 canon_pgprot(pmd_pgprot)))); 1630 1631 start += PMD_SIZE; 1632 cpa->pfn += PMD_SIZE >> PAGE_SHIFT; 1633 cur_pages += PMD_SIZE >> PAGE_SHIFT; 1634 } 1635 1636 /* 1637 * Map trailing 4K pages. 1638 */ 1639 if (start < end) { 1640 pmd = pmd_offset(pud, start); 1641 if (pmd_none(*pmd)) 1642 if (alloc_pte_page(pmd)) 1643 return -1; 1644 1645 populate_pte(cpa, start, end, num_pages - cur_pages, 1646 pmd, pgprot); 1647 } 1648 return num_pages; 1649 } 1650 1651 static int populate_pud(struct cpa_data *cpa, unsigned long start, p4d_t *p4d, 1652 pgprot_t pgprot) 1653 { 1654 pud_t *pud; 1655 unsigned long end; 1656 long cur_pages = 0; 1657 pgprot_t pud_pgprot; 1658 1659 end = start + (cpa->numpages << PAGE_SHIFT); 1660 1661 /* 1662 * Not on a Gb page boundary? => map everything up to it with 1663 * smaller pages. 1664 */ 1665 if (start & (PUD_SIZE - 1)) { 1666 unsigned long pre_end; 1667 unsigned long next_page = (start + PUD_SIZE) & PUD_MASK; 1668 1669 pre_end = min_t(unsigned long, end, next_page); 1670 cur_pages = (pre_end - start) >> PAGE_SHIFT; 1671 cur_pages = min_t(int, (int)cpa->numpages, cur_pages); 1672 1673 pud = pud_offset(p4d, start); 1674 1675 /* 1676 * Need a PMD page? 1677 */ 1678 if (pud_none(*pud)) 1679 if (alloc_pmd_page(pud)) 1680 return -1; 1681 1682 cur_pages = populate_pmd(cpa, start, pre_end, cur_pages, 1683 pud, pgprot); 1684 if (cur_pages < 0) 1685 return cur_pages; 1686 1687 start = pre_end; 1688 } 1689 1690 /* We mapped them all? */ 1691 if (cpa->numpages == cur_pages) 1692 return cur_pages; 1693 1694 pud = pud_offset(p4d, start); 1695 pud_pgprot = pgprot_4k_2_large(pgprot); 1696 1697 /* 1698 * Map everything starting from the Gb boundary, possibly with 1G pages 1699 */ 1700 while (boot_cpu_has(X86_FEATURE_GBPAGES) && end - start >= PUD_SIZE) { 1701 set_pud(pud, pud_mkhuge(pfn_pud(cpa->pfn, 1702 canon_pgprot(pud_pgprot)))); 1703 1704 start += PUD_SIZE; 1705 cpa->pfn += PUD_SIZE >> PAGE_SHIFT; 1706 cur_pages += PUD_SIZE >> PAGE_SHIFT; 1707 pud++; 1708 } 1709 1710 /* Map trailing leftover */ 1711 if (start < end) { 1712 long tmp; 1713 1714 pud = pud_offset(p4d, start); 1715 if (pud_none(*pud)) 1716 if (alloc_pmd_page(pud)) 1717 return -1; 1718 1719 tmp = populate_pmd(cpa, start, end, cpa->numpages - cur_pages, 1720 pud, pgprot); 1721 if (tmp < 0) 1722 return cur_pages; 1723 1724 cur_pages += tmp; 1725 } 1726 return cur_pages; 1727 } 1728 1729 /* 1730 * Restrictions for kernel page table do not necessarily apply when mapping in 1731 * an alternate PGD. 1732 */ 1733 static int populate_pgd(struct cpa_data *cpa, unsigned long addr) 1734 { 1735 pgprot_t pgprot = __pgprot(_KERNPG_TABLE); 1736 pud_t *pud = NULL; /* shut up gcc */ 1737 p4d_t *p4d; 1738 pgd_t *pgd_entry; 1739 long ret; 1740 1741 pgd_entry = cpa->pgd + pgd_index(addr); 1742 1743 if (pgd_none(*pgd_entry)) { 1744 /* 1745 * Pass 0 as a placeholder for the second argument, since the 1746 * generic implementation of p4d_alloc_one() does not use it. 1747 */ 1748 p4d = p4d_alloc_one(&init_mm, 0); 1749 if (!p4d) 1750 return -1; 1751 1752 set_pgd(pgd_entry, __pgd(__pa(p4d) | _KERNPG_TABLE)); 1753 } 1754 1755 /* 1756 * Allocate a PUD page and hand it down for mapping. 1757 */ 1758 p4d = p4d_offset(pgd_entry, addr); 1759 if (p4d_none(*p4d)) { 1760 /* 1761 * Pass 0 as a placeholder for the second argument, since the 1762 * generic implementation of pud_alloc_one() does not use it. 1763 */ 1764 pud = pud_alloc_one(&init_mm, 0); 1765 if (!pud) 1766 return -1; 1767 1768 set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE)); 1769 } 1770 1771 pgprot_val(pgprot) &= ~pgprot_val(cpa->mask_clr); 1772 pgprot_val(pgprot) |= pgprot_val(cpa->mask_set); 1773 1774 ret = populate_pud(cpa, addr, p4d, pgprot); 1775 if (ret < 0) { 1776 /* 1777 * Leave the PUD page in place in case some other CPU or thread 1778 * already found it, but remove any useless entries we just 1779 * added to it. 1780 */ 1781 unmap_pud_range(p4d, addr, 1782 addr + (cpa->numpages << PAGE_SHIFT)); 1783 return ret; 1784 } 1785 1786 cpa->numpages = ret; 1787 return 0; 1788 } 1789 1790 static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr, 1791 int primary) 1792 { 1793 if (cpa->pgd) { 1794 /* 1795 * Right now, we only execute this code path when mapping 1796 * the EFI virtual memory map regions, no other users 1797 * provide a ->pgd value. This may change in the future. 1798 */ 1799 return populate_pgd(cpa, vaddr); 1800 } 1801 1802 /* 1803 * Ignore all non primary paths. 1804 */ 1805 if (!primary) { 1806 cpa->numpages = 1; 1807 return 0; 1808 } 1809 1810 /* 1811 * Ignore the NULL PTE for kernel identity mapping, as it is expected 1812 * to have holes. 1813 * Also set numpages to '1' indicating that we processed cpa req for 1814 * one virtual address page and its pfn. TBD: numpages can be set based 1815 * on the initial value and the level returned by lookup_address(). 1816 */ 1817 if (within(vaddr, PAGE_OFFSET, 1818 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) { 1819 cpa->numpages = 1; 1820 cpa->pfn = __pa(vaddr) >> PAGE_SHIFT; 1821 return 0; 1822 1823 } else if (__cpa_pfn_in_highmap(cpa->pfn)) { 1824 /* Faults in the highmap are OK, so do not warn: */ 1825 return -EFAULT; 1826 } else { 1827 WARN(1, KERN_WARNING "CPA: called for zero pte. " 1828 "vaddr = %lx cpa->vaddr = %lx\n", vaddr, 1829 *cpa->vaddr); 1830 1831 return -EFAULT; 1832 } 1833 } 1834 1835 static int __change_page_attr(struct cpa_data *cpa, int primary) 1836 { 1837 unsigned long address; 1838 int do_split, err; 1839 unsigned int level; 1840 pte_t *kpte, old_pte; 1841 bool nx, rw; 1842 1843 address = __cpa_addr(cpa, cpa->curpage); 1844 repeat: 1845 kpte = _lookup_address_cpa(cpa, address, &level, &nx, &rw); 1846 if (!kpte) 1847 return __cpa_process_fault(cpa, address, primary); 1848 1849 old_pte = *kpte; 1850 if (pte_none(old_pte)) 1851 return __cpa_process_fault(cpa, address, primary); 1852 1853 if (level == PG_LEVEL_4K) { 1854 pte_t new_pte; 1855 pgprot_t old_prot = pte_pgprot(old_pte); 1856 pgprot_t new_prot = pte_pgprot(old_pte); 1857 unsigned long pfn = pte_pfn(old_pte); 1858 1859 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr); 1860 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set); 1861 1862 cpa_inc_4k_install(); 1863 /* Hand in lpsize = 0 to enforce the protection mechanism */ 1864 new_prot = static_protections(new_prot, address, pfn, 1, 0, 1865 CPA_PROTECT); 1866 1867 new_prot = verify_rwx(old_prot, new_prot, address, pfn, 1, 1868 nx, rw); 1869 1870 new_prot = pgprot_clear_protnone_bits(new_prot); 1871 1872 /* 1873 * We need to keep the pfn from the existing PTE, 1874 * after all we're only going to change its attributes 1875 * not the memory it points to 1876 */ 1877 new_pte = pfn_pte(pfn, new_prot); 1878 cpa->pfn = pfn; 1879 /* 1880 * Do we really change anything ? 1881 */ 1882 if (pte_val(old_pte) != pte_val(new_pte)) { 1883 set_pte_atomic(kpte, new_pte); 1884 cpa->flags |= CPA_FLUSHTLB; 1885 } 1886 cpa->numpages = 1; 1887 return 0; 1888 } 1889 1890 /* 1891 * Check, whether we can keep the large page intact 1892 * and just change the pte: 1893 */ 1894 do_split = should_split_large_page(kpte, address, cpa); 1895 /* 1896 * When the range fits into the existing large page, 1897 * return. cp->numpages and cpa->tlbflush have been updated in 1898 * try_large_page: 1899 */ 1900 if (do_split <= 0) 1901 return do_split; 1902 1903 /* 1904 * We have to split the large page: 1905 */ 1906 err = split_large_page(cpa, kpte, address); 1907 if (!err) 1908 goto repeat; 1909 1910 return err; 1911 } 1912 1913 static int __change_page_attr_set_clr(struct cpa_data *cpa, int primary); 1914 1915 /* 1916 * Check the directmap and "high kernel map" 'aliases'. 1917 */ 1918 static int cpa_process_alias(struct cpa_data *cpa) 1919 { 1920 struct cpa_data alias_cpa; 1921 unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT); 1922 unsigned long vaddr; 1923 int ret; 1924 1925 if (!pfn_range_is_mapped(cpa->pfn, cpa->pfn + 1)) 1926 return 0; 1927 1928 /* 1929 * No need to redo, when the primary call touched the direct 1930 * mapping already: 1931 */ 1932 vaddr = __cpa_addr(cpa, cpa->curpage); 1933 if (!(within(vaddr, PAGE_OFFSET, 1934 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) { 1935 1936 alias_cpa = *cpa; 1937 alias_cpa.vaddr = &laddr; 1938 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY); 1939 alias_cpa.curpage = 0; 1940 1941 /* Directmap always has NX set, do not modify. */ 1942 if (__supported_pte_mask & _PAGE_NX) { 1943 alias_cpa.mask_clr.pgprot &= ~_PAGE_NX; 1944 alias_cpa.mask_set.pgprot &= ~_PAGE_NX; 1945 } 1946 1947 cpa->force_flush_all = 1; 1948 1949 ret = __change_page_attr_set_clr(&alias_cpa, 0); 1950 if (ret) 1951 return ret; 1952 } 1953 1954 #ifdef CONFIG_X86_64 1955 /* 1956 * If the primary call didn't touch the high mapping already 1957 * and the physical address is inside the kernel map, we need 1958 * to touch the high mapped kernel as well: 1959 */ 1960 if (!within(vaddr, (unsigned long)_text, _brk_end) && 1961 __cpa_pfn_in_highmap(cpa->pfn)) { 1962 unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) + 1963 __START_KERNEL_map - phys_base; 1964 alias_cpa = *cpa; 1965 alias_cpa.vaddr = &temp_cpa_vaddr; 1966 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY); 1967 alias_cpa.curpage = 0; 1968 1969 /* 1970 * [_text, _brk_end) also covers data, do not modify NX except 1971 * in cases where the highmap is the primary target. 1972 */ 1973 if (__supported_pte_mask & _PAGE_NX) { 1974 alias_cpa.mask_clr.pgprot &= ~_PAGE_NX; 1975 alias_cpa.mask_set.pgprot &= ~_PAGE_NX; 1976 } 1977 1978 cpa->force_flush_all = 1; 1979 /* 1980 * The high mapping range is imprecise, so ignore the 1981 * return value. 1982 */ 1983 __change_page_attr_set_clr(&alias_cpa, 0); 1984 } 1985 #endif 1986 1987 return 0; 1988 } 1989 1990 static int __change_page_attr_set_clr(struct cpa_data *cpa, int primary) 1991 { 1992 unsigned long numpages = cpa->numpages; 1993 unsigned long rempages = numpages; 1994 bool lock = true; 1995 int ret = 0; 1996 1997 /* 1998 * No changes, easy! 1999 */ 2000 if (!(pgprot_val(cpa->mask_set) | pgprot_val(cpa->mask_clr)) && 2001 !cpa->force_split) 2002 return ret; 2003 2004 /* 2005 * DEBUG_PAGEALLOC is special; it is called from any context the 2006 * page-allocator is, which violates the normal cpa_lock locking 2007 * rules. 2008 * 2009 * However, since it is part of the page-allocator, things are still 2010 * properly serialized by the page-allocator locking and the fact that 2011 * when a page is owned by the page-allocator, it isn't owned by 2012 * anybody else. That is, you *SHOULD NOT* be calling cpa() on memory 2013 * that isn't allocated. 2014 * 2015 * Additionally, DEBUG_PAGEALLOC ensures (per probe_page_size_mask()) 2016 * that the kernel mapping is 4k pages, therefore there are no large 2017 * pages to split/collapse. 2018 * 2019 * Furthermore, the page-allocator strictly manages pages that 2020 * *exist*, avoiding pgd_lock. 2021 * 2022 * Therefore, it is safe to not take cpa_lock. 2023 */ 2024 if (debug_pagealloc_enabled() && (cpa->flags & CPA_DEBUG_PAGEALLOC)) 2025 lock = false; 2026 2027 while (rempages) { 2028 /* 2029 * Store the remaining nr of pages for the large page 2030 * preservation check. 2031 */ 2032 cpa->numpages = rempages; 2033 /* for array changes, we can't use large page */ 2034 if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY)) 2035 cpa->numpages = 1; 2036 2037 if (lock) { 2038 guard(spinlock)(&cpa_lock); 2039 ret = __change_page_attr(cpa, primary); 2040 } else { 2041 ret = __change_page_attr(cpa, primary); 2042 } 2043 if (ret) 2044 goto out; 2045 2046 if (primary && !(cpa->flags & CPA_NO_CHECK_ALIAS)) { 2047 ret = cpa_process_alias(cpa); 2048 if (ret) 2049 goto out; 2050 } 2051 2052 /* 2053 * Adjust the number of pages with the result of the 2054 * CPA operation. Either a large page has been 2055 * preserved or a single page update happened. 2056 */ 2057 BUG_ON(cpa->numpages > rempages || !cpa->numpages); 2058 rempages -= cpa->numpages; 2059 cpa->curpage += cpa->numpages; 2060 } 2061 2062 out: 2063 /* Restore the original numpages */ 2064 cpa->numpages = numpages; 2065 return ret; 2066 } 2067 2068 static int change_page_attr_set_clr(unsigned long *addr, int numpages, 2069 pgprot_t mask_set, pgprot_t mask_clr, 2070 int force_split, int in_flag, 2071 struct page **pages) 2072 { 2073 struct cpa_data cpa; 2074 int ret, cache; 2075 2076 memset(&cpa, 0, sizeof(cpa)); 2077 2078 /* 2079 * Check, if we are requested to set a not supported 2080 * feature. Clearing non-supported features is OK. 2081 */ 2082 mask_set = canon_pgprot(mask_set); 2083 2084 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split) 2085 return 0; 2086 2087 /* Ensure we are PAGE_SIZE aligned */ 2088 if (in_flag & CPA_ARRAY) { 2089 int i; 2090 for (i = 0; i < numpages; i++) { 2091 if (addr[i] & ~PAGE_MASK) { 2092 addr[i] &= PAGE_MASK; 2093 WARN_ON_ONCE(1); 2094 } 2095 } 2096 } else if (!(in_flag & CPA_PAGES_ARRAY)) { 2097 /* 2098 * in_flag of CPA_PAGES_ARRAY implies it is aligned. 2099 * No need to check in that case 2100 */ 2101 if (*addr & ~PAGE_MASK) { 2102 *addr &= PAGE_MASK; 2103 /* 2104 * People should not be passing in unaligned addresses: 2105 */ 2106 WARN_ON_ONCE(1); 2107 } 2108 } 2109 2110 /* Must avoid aliasing mappings in the highmem code */ 2111 kmap_flush_unused(); 2112 2113 vm_unmap_aliases(); 2114 2115 cpa.vaddr = addr; 2116 cpa.pages = pages; 2117 cpa.numpages = numpages; 2118 cpa.mask_set = mask_set; 2119 cpa.mask_clr = mask_clr; 2120 cpa.flags = in_flag; 2121 cpa.curpage = 0; 2122 cpa.force_split = force_split; 2123 2124 ret = __change_page_attr_set_clr(&cpa, 1); 2125 2126 /* 2127 * Check whether we really changed something: 2128 */ 2129 if (!(cpa.flags & CPA_FLUSHTLB)) 2130 goto out; 2131 2132 /* 2133 * No need to flush, when we did not set any of the caching 2134 * attributes: 2135 */ 2136 cache = !!pgprot2cachemode(mask_set); 2137 2138 /* 2139 * On error; flush everything to be sure. 2140 */ 2141 if (ret) { 2142 cpa_flush_all(cache); 2143 goto out; 2144 } 2145 2146 cpa_flush(&cpa, cache); 2147 out: 2148 return ret; 2149 } 2150 2151 static inline int change_page_attr_set(unsigned long *addr, int numpages, 2152 pgprot_t mask, int array) 2153 { 2154 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0, 2155 (array ? CPA_ARRAY : 0), NULL); 2156 } 2157 2158 static inline int change_page_attr_clear(unsigned long *addr, int numpages, 2159 pgprot_t mask, int array) 2160 { 2161 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0, 2162 (array ? CPA_ARRAY : 0), NULL); 2163 } 2164 2165 static inline int cpa_set_pages_array(struct page **pages, int numpages, 2166 pgprot_t mask) 2167 { 2168 return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0, 2169 CPA_PAGES_ARRAY, pages); 2170 } 2171 2172 static inline int cpa_clear_pages_array(struct page **pages, int numpages, 2173 pgprot_t mask) 2174 { 2175 return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0, 2176 CPA_PAGES_ARRAY, pages); 2177 } 2178 2179 int _set_memory_uc(unsigned long addr, int numpages) 2180 { 2181 /* 2182 * for now UC MINUS. see comments in ioremap() 2183 * If you really need strong UC use ioremap_uc(), but note 2184 * that you cannot override IO areas with set_memory_*() as 2185 * these helpers cannot work with IO memory. 2186 */ 2187 return change_page_attr_set(&addr, numpages, 2188 cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS), 2189 0); 2190 } 2191 2192 int set_memory_uc(unsigned long addr, int numpages) 2193 { 2194 int ret; 2195 2196 /* 2197 * for now UC MINUS. see comments in ioremap() 2198 */ 2199 ret = memtype_reserve(__pa(addr), __pa(addr) + numpages * PAGE_SIZE, 2200 _PAGE_CACHE_MODE_UC_MINUS, NULL); 2201 if (ret) 2202 goto out_err; 2203 2204 ret = _set_memory_uc(addr, numpages); 2205 if (ret) 2206 goto out_free; 2207 2208 return 0; 2209 2210 out_free: 2211 memtype_free(__pa(addr), __pa(addr) + numpages * PAGE_SIZE); 2212 out_err: 2213 return ret; 2214 } 2215 EXPORT_SYMBOL(set_memory_uc); 2216 2217 int _set_memory_wc(unsigned long addr, int numpages) 2218 { 2219 int ret; 2220 2221 ret = change_page_attr_set(&addr, numpages, 2222 cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS), 2223 0); 2224 if (!ret) { 2225 ret = change_page_attr_set_clr(&addr, numpages, 2226 cachemode2pgprot(_PAGE_CACHE_MODE_WC), 2227 __pgprot(_PAGE_CACHE_MASK), 2228 0, 0, NULL); 2229 } 2230 return ret; 2231 } 2232 2233 int set_memory_wc(unsigned long addr, int numpages) 2234 { 2235 int ret; 2236 2237 ret = memtype_reserve(__pa(addr), __pa(addr) + numpages * PAGE_SIZE, 2238 _PAGE_CACHE_MODE_WC, NULL); 2239 if (ret) 2240 return ret; 2241 2242 ret = _set_memory_wc(addr, numpages); 2243 if (ret) 2244 memtype_free(__pa(addr), __pa(addr) + numpages * PAGE_SIZE); 2245 2246 return ret; 2247 } 2248 EXPORT_SYMBOL(set_memory_wc); 2249 2250 int _set_memory_wt(unsigned long addr, int numpages) 2251 { 2252 return change_page_attr_set(&addr, numpages, 2253 cachemode2pgprot(_PAGE_CACHE_MODE_WT), 0); 2254 } 2255 2256 int _set_memory_wb(unsigned long addr, int numpages) 2257 { 2258 /* WB cache mode is hard wired to all cache attribute bits being 0 */ 2259 return change_page_attr_clear(&addr, numpages, 2260 __pgprot(_PAGE_CACHE_MASK), 0); 2261 } 2262 2263 int set_memory_wb(unsigned long addr, int numpages) 2264 { 2265 int ret; 2266 2267 ret = _set_memory_wb(addr, numpages); 2268 if (ret) 2269 return ret; 2270 2271 memtype_free(__pa(addr), __pa(addr) + numpages * PAGE_SIZE); 2272 return 0; 2273 } 2274 EXPORT_SYMBOL(set_memory_wb); 2275 2276 /* Prevent speculative access to a page by marking it not-present */ 2277 #ifdef CONFIG_X86_64 2278 int set_mce_nospec(unsigned long pfn) 2279 { 2280 unsigned long decoy_addr; 2281 int rc; 2282 2283 /* SGX pages are not in the 1:1 map */ 2284 if (arch_is_platform_page(pfn << PAGE_SHIFT)) 2285 return 0; 2286 /* 2287 * We would like to just call: 2288 * set_memory_XX((unsigned long)pfn_to_kaddr(pfn), 1); 2289 * but doing that would radically increase the odds of a 2290 * speculative access to the poison page because we'd have 2291 * the virtual address of the kernel 1:1 mapping sitting 2292 * around in registers. 2293 * Instead we get tricky. We create a non-canonical address 2294 * that looks just like the one we want, but has bit 63 flipped. 2295 * This relies on set_memory_XX() properly sanitizing any __pa() 2296 * results with __PHYSICAL_MASK or PTE_PFN_MASK. 2297 */ 2298 decoy_addr = (pfn << PAGE_SHIFT) + (PAGE_OFFSET ^ BIT(63)); 2299 2300 rc = set_memory_np(decoy_addr, 1); 2301 if (rc) 2302 pr_warn("Could not invalidate pfn=0x%lx from 1:1 map\n", pfn); 2303 return rc; 2304 } 2305 EXPORT_SYMBOL_GPL(set_mce_nospec); 2306 2307 /* Restore full speculative operation to the pfn. */ 2308 int clear_mce_nospec(unsigned long pfn) 2309 { 2310 unsigned long addr = (unsigned long) pfn_to_kaddr(pfn); 2311 2312 return set_memory_p(addr, 1); 2313 } 2314 EXPORT_SYMBOL_GPL(clear_mce_nospec); 2315 #endif /* CONFIG_X86_64 */ 2316 2317 int set_memory_x(unsigned long addr, int numpages) 2318 { 2319 if (!(__supported_pte_mask & _PAGE_NX)) 2320 return 0; 2321 2322 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0); 2323 } 2324 2325 int set_memory_nx(unsigned long addr, int numpages) 2326 { 2327 if (!(__supported_pte_mask & _PAGE_NX)) 2328 return 0; 2329 2330 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0); 2331 } 2332 2333 int set_memory_ro(unsigned long addr, int numpages) 2334 { 2335 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW | _PAGE_DIRTY), 0); 2336 } 2337 2338 int set_memory_rox(unsigned long addr, int numpages) 2339 { 2340 pgprot_t clr = __pgprot(_PAGE_RW | _PAGE_DIRTY); 2341 2342 if (__supported_pte_mask & _PAGE_NX) 2343 clr.pgprot |= _PAGE_NX; 2344 2345 return change_page_attr_set_clr(&addr, numpages, __pgprot(0), clr, 0, 2346 CPA_COLLAPSE, NULL); 2347 } 2348 2349 int set_memory_rw(unsigned long addr, int numpages) 2350 { 2351 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0); 2352 } 2353 2354 int set_memory_np(unsigned long addr, int numpages) 2355 { 2356 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0); 2357 } 2358 2359 int set_memory_np_noalias(unsigned long addr, int numpages) 2360 { 2361 return change_page_attr_set_clr(&addr, numpages, __pgprot(0), 2362 __pgprot(_PAGE_PRESENT), 0, 2363 CPA_NO_CHECK_ALIAS, NULL); 2364 } 2365 2366 int set_memory_p(unsigned long addr, int numpages) 2367 { 2368 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_PRESENT), 0); 2369 } 2370 2371 int set_memory_4k(unsigned long addr, int numpages) 2372 { 2373 return change_page_attr_set_clr(&addr, numpages, 2374 __pgprot(_PAGE_KERNEL_4K), 2375 __pgprot(0), 1, 0, NULL); 2376 } 2377 2378 int set_memory_nonglobal(unsigned long addr, int numpages) 2379 { 2380 return change_page_attr_clear(&addr, numpages, 2381 __pgprot(_PAGE_GLOBAL), 0); 2382 } 2383 2384 int set_memory_global(unsigned long addr, int numpages) 2385 { 2386 return change_page_attr_set(&addr, numpages, 2387 __pgprot(_PAGE_GLOBAL), 0); 2388 } 2389 2390 /* 2391 * __set_memory_enc_pgtable() is used for the hypervisors that get 2392 * informed about "encryption" status via page tables. 2393 */ 2394 static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc) 2395 { 2396 pgprot_t empty = __pgprot(0); 2397 struct cpa_data cpa; 2398 int ret; 2399 2400 /* Should not be working on unaligned addresses */ 2401 if (WARN_ONCE(addr & ~PAGE_MASK, "misaligned address: %#lx\n", addr)) 2402 addr &= PAGE_MASK; 2403 2404 memset(&cpa, 0, sizeof(cpa)); 2405 cpa.vaddr = &addr; 2406 cpa.numpages = numpages; 2407 cpa.mask_set = enc ? pgprot_encrypted(empty) : pgprot_decrypted(empty); 2408 cpa.mask_clr = enc ? pgprot_decrypted(empty) : pgprot_encrypted(empty); 2409 cpa.pgd = init_mm.pgd; 2410 2411 /* Must avoid aliasing mappings in the highmem code */ 2412 kmap_flush_unused(); 2413 vm_unmap_aliases(); 2414 2415 /* Flush the caches as needed before changing the encryption attribute. */ 2416 if (x86_platform.guest.enc_tlb_flush_required(enc)) 2417 cpa_flush(&cpa, x86_platform.guest.enc_cache_flush_required()); 2418 2419 /* Notify hypervisor that we are about to set/clr encryption attribute. */ 2420 ret = x86_platform.guest.enc_status_change_prepare(addr, numpages, enc); 2421 if (ret) 2422 goto vmm_fail; 2423 2424 ret = __change_page_attr_set_clr(&cpa, 1); 2425 2426 /* 2427 * After changing the encryption attribute, we need to flush TLBs again 2428 * in case any speculative TLB caching occurred (but no need to flush 2429 * caches again). We could just use cpa_flush_all(), but in case TLB 2430 * flushing gets optimized in the cpa_flush() path use the same logic 2431 * as above. 2432 */ 2433 cpa_flush(&cpa, 0); 2434 2435 if (ret) 2436 return ret; 2437 2438 /* Notify hypervisor that we have successfully set/clr encryption attribute. */ 2439 ret = x86_platform.guest.enc_status_change_finish(addr, numpages, enc); 2440 if (ret) 2441 goto vmm_fail; 2442 2443 return 0; 2444 2445 vmm_fail: 2446 WARN_ONCE(1, "CPA VMM failure to convert memory (addr=%p, numpages=%d) to %s: %d\n", 2447 (void *)addr, numpages, enc ? "private" : "shared", ret); 2448 2449 return ret; 2450 } 2451 2452 /* 2453 * The lock serializes conversions between private and shared memory. 2454 * 2455 * It is taken for read on conversion. A write lock guarantees that no 2456 * concurrent conversions are in progress. 2457 */ 2458 static DECLARE_RWSEM(mem_enc_lock); 2459 2460 /* 2461 * Stop new private<->shared conversions. 2462 * 2463 * Taking the exclusive mem_enc_lock waits for in-flight conversions to complete. 2464 * The lock is not released to prevent new conversions from being started. 2465 */ 2466 bool set_memory_enc_stop_conversion(void) 2467 { 2468 /* 2469 * In a crash scenario, sleep is not allowed. Try to take the lock. 2470 * Failure indicates that there is a race with the conversion. 2471 */ 2472 if (oops_in_progress) 2473 return down_write_trylock(&mem_enc_lock); 2474 2475 down_write(&mem_enc_lock); 2476 2477 return true; 2478 } 2479 2480 static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc) 2481 { 2482 int ret = 0; 2483 2484 if (cc_platform_has(CC_ATTR_MEM_ENCRYPT)) { 2485 if (!down_read_trylock(&mem_enc_lock)) 2486 return -EBUSY; 2487 2488 ret = __set_memory_enc_pgtable(addr, numpages, enc); 2489 2490 up_read(&mem_enc_lock); 2491 } 2492 2493 return ret; 2494 } 2495 2496 int set_memory_encrypted(unsigned long addr, int numpages) 2497 { 2498 return __set_memory_enc_dec(addr, numpages, true); 2499 } 2500 EXPORT_SYMBOL_GPL(set_memory_encrypted); 2501 2502 int set_memory_decrypted(unsigned long addr, int numpages) 2503 { 2504 return __set_memory_enc_dec(addr, numpages, false); 2505 } 2506 EXPORT_SYMBOL_GPL(set_memory_decrypted); 2507 2508 int set_pages_uc(struct page *page, int numpages) 2509 { 2510 unsigned long addr = (unsigned long)page_address(page); 2511 2512 return set_memory_uc(addr, numpages); 2513 } 2514 EXPORT_SYMBOL(set_pages_uc); 2515 2516 static int _set_pages_array(struct page **pages, int numpages, 2517 enum page_cache_mode new_type) 2518 { 2519 unsigned long start; 2520 unsigned long end; 2521 enum page_cache_mode set_type; 2522 int i; 2523 int free_idx; 2524 int ret; 2525 2526 for (i = 0; i < numpages; i++) { 2527 if (PageHighMem(pages[i])) 2528 continue; 2529 start = page_to_pfn(pages[i]) << PAGE_SHIFT; 2530 end = start + PAGE_SIZE; 2531 if (memtype_reserve(start, end, new_type, NULL)) 2532 goto err_out; 2533 } 2534 2535 /* If WC, set to UC- first and then WC */ 2536 set_type = (new_type == _PAGE_CACHE_MODE_WC) ? 2537 _PAGE_CACHE_MODE_UC_MINUS : new_type; 2538 2539 ret = cpa_set_pages_array(pages, numpages, 2540 cachemode2pgprot(set_type)); 2541 if (!ret && new_type == _PAGE_CACHE_MODE_WC) 2542 ret = change_page_attr_set_clr(NULL, numpages, 2543 cachemode2pgprot( 2544 _PAGE_CACHE_MODE_WC), 2545 __pgprot(_PAGE_CACHE_MASK), 2546 0, CPA_PAGES_ARRAY, pages); 2547 if (ret) 2548 goto err_out; 2549 return 0; /* Success */ 2550 err_out: 2551 free_idx = i; 2552 for (i = 0; i < free_idx; i++) { 2553 if (PageHighMem(pages[i])) 2554 continue; 2555 start = page_to_pfn(pages[i]) << PAGE_SHIFT; 2556 end = start + PAGE_SIZE; 2557 memtype_free(start, end); 2558 } 2559 return -EINVAL; 2560 } 2561 2562 int set_pages_array_uc(struct page **pages, int numpages) 2563 { 2564 return _set_pages_array(pages, numpages, _PAGE_CACHE_MODE_UC_MINUS); 2565 } 2566 EXPORT_SYMBOL(set_pages_array_uc); 2567 2568 int set_pages_array_wc(struct page **pages, int numpages) 2569 { 2570 return _set_pages_array(pages, numpages, _PAGE_CACHE_MODE_WC); 2571 } 2572 EXPORT_SYMBOL(set_pages_array_wc); 2573 2574 int set_pages_wb(struct page *page, int numpages) 2575 { 2576 unsigned long addr = (unsigned long)page_address(page); 2577 2578 return set_memory_wb(addr, numpages); 2579 } 2580 EXPORT_SYMBOL(set_pages_wb); 2581 2582 int set_pages_array_wb(struct page **pages, int numpages) 2583 { 2584 int retval; 2585 unsigned long start; 2586 unsigned long end; 2587 int i; 2588 2589 /* WB cache mode is hard wired to all cache attribute bits being 0 */ 2590 retval = cpa_clear_pages_array(pages, numpages, 2591 __pgprot(_PAGE_CACHE_MASK)); 2592 if (retval) 2593 return retval; 2594 2595 for (i = 0; i < numpages; i++) { 2596 if (PageHighMem(pages[i])) 2597 continue; 2598 start = page_to_pfn(pages[i]) << PAGE_SHIFT; 2599 end = start + PAGE_SIZE; 2600 memtype_free(start, end); 2601 } 2602 2603 return 0; 2604 } 2605 EXPORT_SYMBOL(set_pages_array_wb); 2606 2607 int set_pages_ro(struct page *page, int numpages) 2608 { 2609 unsigned long addr = (unsigned long)page_address(page); 2610 2611 return set_memory_ro(addr, numpages); 2612 } 2613 2614 int set_pages_rw(struct page *page, int numpages) 2615 { 2616 unsigned long addr = (unsigned long)page_address(page); 2617 2618 return set_memory_rw(addr, numpages); 2619 } 2620 2621 static int __set_pages_p(struct page *page, int numpages, unsigned int cpa_flags) 2622 { 2623 unsigned long tempaddr = (unsigned long) page_address(page); 2624 struct cpa_data cpa = { .vaddr = &tempaddr, 2625 .pgd = NULL, 2626 .numpages = numpages, 2627 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW), 2628 .mask_clr = __pgprot(0), 2629 .flags = CPA_NO_CHECK_ALIAS | cpa_flags }; 2630 2631 /* 2632 * No alias checking needed for setting present flag. otherwise, 2633 * we may need to break large pages for 64-bit kernel text 2634 * mappings (this adds to complexity if we want to do this from 2635 * atomic context especially). Let's keep it simple! 2636 */ 2637 return __change_page_attr_set_clr(&cpa, 1); 2638 } 2639 2640 static int __set_pages_np(struct page *page, int numpages, unsigned int cpa_flags) 2641 { 2642 unsigned long tempaddr = (unsigned long) page_address(page); 2643 struct cpa_data cpa = { .vaddr = &tempaddr, 2644 .pgd = NULL, 2645 .numpages = numpages, 2646 .mask_set = __pgprot(0), 2647 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY), 2648 .flags = CPA_NO_CHECK_ALIAS | cpa_flags }; 2649 2650 /* 2651 * No alias checking needed for setting not present flag. otherwise, 2652 * we may need to break large pages for 64-bit kernel text 2653 * mappings (this adds to complexity if we want to do this from 2654 * atomic context especially). Let's keep it simple! 2655 */ 2656 return __change_page_attr_set_clr(&cpa, 1); 2657 } 2658 2659 int set_direct_map_invalid_noflush(struct page *page) 2660 { 2661 return __set_pages_np(page, 1, 0); 2662 } 2663 2664 int set_direct_map_default_noflush(struct page *page) 2665 { 2666 return __set_pages_p(page, 1, 0); 2667 } 2668 2669 int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid) 2670 { 2671 if (valid) 2672 return __set_pages_p(page, nr, 0); 2673 2674 return __set_pages_np(page, nr, 0); 2675 } 2676 2677 #ifdef CONFIG_DEBUG_PAGEALLOC 2678 void __kernel_map_pages(struct page *page, int numpages, int enable) 2679 { 2680 if (PageHighMem(page)) 2681 return; 2682 if (!enable) { 2683 debug_check_no_locks_freed(page_address(page), 2684 numpages * PAGE_SIZE); 2685 } 2686 2687 /* 2688 * The return value is ignored as the calls cannot fail. 2689 * Large pages for identity mappings are not used at boot time 2690 * and hence no memory allocations during large page split. 2691 */ 2692 if (enable) 2693 __set_pages_p(page, numpages, CPA_DEBUG_PAGEALLOC); 2694 else 2695 __set_pages_np(page, numpages, CPA_DEBUG_PAGEALLOC); 2696 2697 /* 2698 * We should perform an IPI and flush all tlbs, but that can 2699 * deadlock, settle for a local flush. 2700 * 2701 * Not doing a global TLB flush means that remote CPUs will retain 2702 * stale TLB entries. In case of P->NP (on free) this means the remote 2703 * CPUs will not take the faults, making the debug scheme less 2704 * reliable. On the NP->P (on alloc) this means the remote CPUs can 2705 * take a spurious fault. However spurious_kernel_fault() will observe 2706 * *_present() and fix it up. 2707 * 2708 * Preemption needs to be disabled around __flush_tlb_all() due to CR3 2709 * reload in __native_flush_tlb(). 2710 */ 2711 preempt_disable(); 2712 __flush_tlb_all(); 2713 preempt_enable(); 2714 2715 arch_flush_lazy_mmu_mode(); 2716 } 2717 #endif /* CONFIG_DEBUG_PAGEALLOC */ 2718 2719 bool kernel_page_present(struct page *page) 2720 { 2721 unsigned int level; 2722 pte_t *pte; 2723 2724 if (PageHighMem(page)) 2725 return false; 2726 2727 pte = lookup_address((unsigned long)page_address(page), &level); 2728 return (pte_val(*pte) & _PAGE_PRESENT); 2729 } 2730 2731 int __init kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address, 2732 unsigned numpages, unsigned long page_flags) 2733 { 2734 int retval = -EINVAL; 2735 2736 struct cpa_data cpa = { 2737 .vaddr = &address, 2738 .pfn = pfn, 2739 .pgd = pgd, 2740 .numpages = numpages, 2741 .mask_set = __pgprot(0), 2742 .mask_clr = __pgprot(~page_flags & (_PAGE_NX|_PAGE_RW|_PAGE_DIRTY)), 2743 .flags = CPA_NO_CHECK_ALIAS, 2744 }; 2745 2746 WARN_ONCE(num_online_cpus() > 1, "Don't call after initializing SMP"); 2747 2748 if (!(__supported_pte_mask & _PAGE_NX)) 2749 goto out; 2750 2751 if (!(page_flags & _PAGE_ENC)) 2752 cpa.mask_clr = pgprot_encrypted(cpa.mask_clr); 2753 2754 cpa.mask_set = __pgprot(_PAGE_PRESENT | page_flags); 2755 2756 retval = __change_page_attr_set_clr(&cpa, 1); 2757 __flush_tlb_all(); 2758 2759 out: 2760 return retval; 2761 } 2762 2763 /* 2764 * __flush_tlb_all() flushes mappings only on current CPU and hence this 2765 * function shouldn't be used in an SMP environment. Presently, it's used only 2766 * during boot (way before smp_init()) by EFI subsystem and hence is ok. 2767 */ 2768 int __init kernel_unmap_pages_in_pgd(pgd_t *pgd, unsigned long address, 2769 unsigned long numpages) 2770 { 2771 int retval; 2772 2773 /* 2774 * The typical sequence for unmapping is to find a pte through 2775 * lookup_address_in_pgd() (ideally, it should never return NULL because 2776 * the address is already mapped) and change its protections. As pfn is 2777 * the *target* of a mapping, it's not useful while unmapping. 2778 */ 2779 struct cpa_data cpa = { 2780 .vaddr = &address, 2781 .pfn = 0, 2782 .pgd = pgd, 2783 .numpages = numpages, 2784 .mask_set = __pgprot(0), 2785 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY), 2786 .flags = CPA_NO_CHECK_ALIAS, 2787 }; 2788 2789 WARN_ONCE(num_online_cpus() > 1, "Don't call after initializing SMP"); 2790 2791 retval = __change_page_attr_set_clr(&cpa, 1); 2792 __flush_tlb_all(); 2793 2794 return retval; 2795 } 2796 2797 /* 2798 * The testcases use internal knowledge of the implementation that shouldn't 2799 * be exposed to the rest of the kernel. Include these directly here. 2800 */ 2801 #ifdef CONFIG_CPA_DEBUG 2802 #include "cpa-test.c" 2803 #endif 2804