1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/arch/x86_64/mm/init.c 4 * 5 * Copyright (C) 1995 Linus Torvalds 6 * Copyright (C) 2000 Pavel Machek <pavel@ucw.cz> 7 * Copyright (C) 2002,2003 Andi Kleen <ak@suse.de> 8 */ 9 10 #include <linux/signal.h> 11 #include <linux/sched.h> 12 #include <linux/kernel.h> 13 #include <linux/errno.h> 14 #include <linux/string.h> 15 #include <linux/types.h> 16 #include <linux/ptrace.h> 17 #include <linux/mman.h> 18 #include <linux/mm.h> 19 #include <linux/swap.h> 20 #include <linux/smp.h> 21 #include <linux/init.h> 22 #include <linux/initrd.h> 23 #include <linux/pagemap.h> 24 #include <linux/memblock.h> 25 #include <linux/proc_fs.h> 26 #include <linux/pci.h> 27 #include <linux/pfn.h> 28 #include <linux/poison.h> 29 #include <linux/dma-mapping.h> 30 #include <linux/memory.h> 31 #include <linux/memory_hotplug.h> 32 #include <linux/memremap.h> 33 #include <linux/nmi.h> 34 #include <linux/gfp.h> 35 #include <linux/kcore.h> 36 #include <linux/bootmem_info.h> 37 38 #include <asm/processor.h> 39 #include <asm/bios_ebda.h> 40 #include <linux/uaccess.h> 41 #include <asm/pgalloc.h> 42 #include <asm/dma.h> 43 #include <asm/fixmap.h> 44 #include <asm/e820/api.h> 45 #include <asm/apic.h> 46 #include <asm/tlb.h> 47 #include <asm/mmu_context.h> 48 #include <asm/proto.h> 49 #include <asm/smp.h> 50 #include <asm/sections.h> 51 #include <asm/kdebug.h> 52 #include <asm/numa.h> 53 #include <asm/set_memory.h> 54 #include <asm/init.h> 55 #include <asm/uv/uv.h> 56 #include <asm/setup.h> 57 #include <asm/ftrace.h> 58 59 #include "mm_internal.h" 60 61 #include "ident_map.c" 62 63 #define DEFINE_POPULATE(fname, type1, type2, init) \ 64 static inline void fname##_init(struct mm_struct *mm, \ 65 type1##_t *arg1, type2##_t *arg2, bool init) \ 66 { \ 67 if (init) \ 68 fname##_safe(mm, arg1, arg2); \ 69 else \ 70 fname(mm, arg1, arg2); \ 71 } 72 73 DEFINE_POPULATE(p4d_populate, p4d, pud, init) 74 DEFINE_POPULATE(pgd_populate, pgd, p4d, init) 75 DEFINE_POPULATE(pud_populate, pud, pmd, init) 76 DEFINE_POPULATE(pmd_populate_kernel, pmd, pte, init) 77 78 #define DEFINE_ENTRY(type1, type2, init) \ 79 static inline void set_##type1##_init(type1##_t *arg1, \ 80 type2##_t arg2, bool init) \ 81 { \ 82 if (init) \ 83 set_##type1##_safe(arg1, arg2); \ 84 else \ 85 set_##type1(arg1, arg2); \ 86 } 87 88 DEFINE_ENTRY(p4d, p4d, init) 89 DEFINE_ENTRY(pud, pud, init) 90 DEFINE_ENTRY(pmd, pmd, init) 91 DEFINE_ENTRY(pte, pte, init) 92 93 static inline pgprot_t prot_sethuge(pgprot_t prot) 94 { 95 WARN_ON_ONCE(pgprot_val(prot) & _PAGE_PAT); 96 97 return __pgprot(pgprot_val(prot) | _PAGE_PSE); 98 } 99 100 /* 101 * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the 102 * physical space so we can cache the place of the first one and move 103 * around without checking the pgd every time. 104 */ 105 106 /* Bits supported by the hardware: */ 107 pteval_t __supported_pte_mask __read_mostly = ~0; 108 /* Bits allowed in normal kernel mappings: */ 109 pteval_t __default_kernel_pte_mask __read_mostly = ~0; 110 EXPORT_SYMBOL_GPL(__supported_pte_mask); 111 /* Used in PAGE_KERNEL_* macros which are reasonably used out-of-tree: */ 112 EXPORT_SYMBOL(__default_kernel_pte_mask); 113 114 int force_personality32; 115 116 /* 117 * noexec32=on|off 118 * Control non executable heap for 32bit processes. 119 * 120 * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default) 121 * off PROT_READ implies PROT_EXEC 122 */ 123 static int __init nonx32_setup(char *str) 124 { 125 if (!strcmp(str, "on")) 126 force_personality32 &= ~READ_IMPLIES_EXEC; 127 else if (!strcmp(str, "off")) 128 force_personality32 |= READ_IMPLIES_EXEC; 129 return 1; 130 } 131 __setup("noexec32=", nonx32_setup); 132 133 static void sync_global_pgds_l5(unsigned long start, unsigned long end) 134 { 135 unsigned long addr; 136 137 for (addr = start; addr <= end; addr = ALIGN(addr + 1, PGDIR_SIZE)) { 138 const pgd_t *pgd_ref = pgd_offset_k(addr); 139 struct page *page; 140 141 /* Check for overflow */ 142 if (addr < start) 143 break; 144 145 if (pgd_none(*pgd_ref)) 146 continue; 147 148 spin_lock(&pgd_lock); 149 list_for_each_entry(page, &pgd_list, lru) { 150 pgd_t *pgd; 151 spinlock_t *pgt_lock; 152 153 pgd = (pgd_t *)page_address(page) + pgd_index(addr); 154 /* the pgt_lock only for Xen */ 155 pgt_lock = &pgd_page_get_mm(page)->page_table_lock; 156 spin_lock(pgt_lock); 157 158 if (!pgd_none(*pgd_ref) && !pgd_none(*pgd)) 159 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref)); 160 161 if (pgd_none(*pgd)) 162 set_pgd(pgd, *pgd_ref); 163 164 spin_unlock(pgt_lock); 165 } 166 spin_unlock(&pgd_lock); 167 } 168 } 169 170 static void sync_global_pgds_l4(unsigned long start, unsigned long end) 171 { 172 unsigned long addr; 173 174 for (addr = start; addr <= end; addr = ALIGN(addr + 1, PGDIR_SIZE)) { 175 pgd_t *pgd_ref = pgd_offset_k(addr); 176 const p4d_t *p4d_ref; 177 struct page *page; 178 179 /* 180 * With folded p4d, pgd_none() is always false, we need to 181 * handle synchronization on p4d level. 182 */ 183 MAYBE_BUILD_BUG_ON(pgd_none(*pgd_ref)); 184 p4d_ref = p4d_offset(pgd_ref, addr); 185 186 if (p4d_none(*p4d_ref)) 187 continue; 188 189 spin_lock(&pgd_lock); 190 list_for_each_entry(page, &pgd_list, lru) { 191 pgd_t *pgd; 192 p4d_t *p4d; 193 spinlock_t *pgt_lock; 194 195 pgd = (pgd_t *)page_address(page) + pgd_index(addr); 196 p4d = p4d_offset(pgd, addr); 197 /* the pgt_lock only for Xen */ 198 pgt_lock = &pgd_page_get_mm(page)->page_table_lock; 199 spin_lock(pgt_lock); 200 201 if (!p4d_none(*p4d_ref) && !p4d_none(*p4d)) 202 BUG_ON(p4d_pgtable(*p4d) 203 != p4d_pgtable(*p4d_ref)); 204 205 if (p4d_none(*p4d)) 206 set_p4d(p4d, *p4d_ref); 207 208 spin_unlock(pgt_lock); 209 } 210 spin_unlock(&pgd_lock); 211 } 212 } 213 214 /* 215 * When memory was added make sure all the processes MM have 216 * suitable PGD entries in the local PGD level page. 217 */ 218 static void sync_global_pgds(unsigned long start, unsigned long end) 219 { 220 if (pgtable_l5_enabled()) 221 sync_global_pgds_l5(start, end); 222 else 223 sync_global_pgds_l4(start, end); 224 } 225 226 /* 227 * NOTE: This function is marked __ref because it calls __init function 228 * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0. 229 */ 230 static __ref void *spp_getpage(void) 231 { 232 void *ptr; 233 234 if (after_bootmem) 235 ptr = (void *) get_zeroed_page(GFP_ATOMIC); 236 else 237 ptr = memblock_alloc(PAGE_SIZE, PAGE_SIZE); 238 239 if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) { 240 panic("set_pte_phys: cannot allocate page data %s\n", 241 after_bootmem ? "after bootmem" : ""); 242 } 243 244 pr_debug("spp_getpage %p\n", ptr); 245 246 return ptr; 247 } 248 249 static p4d_t *fill_p4d(pgd_t *pgd, unsigned long vaddr) 250 { 251 if (pgd_none(*pgd)) { 252 p4d_t *p4d = (p4d_t *)spp_getpage(); 253 pgd_populate(&init_mm, pgd, p4d); 254 if (p4d != p4d_offset(pgd, 0)) 255 printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n", 256 p4d, p4d_offset(pgd, 0)); 257 } 258 return p4d_offset(pgd, vaddr); 259 } 260 261 static pud_t *fill_pud(p4d_t *p4d, unsigned long vaddr) 262 { 263 if (p4d_none(*p4d)) { 264 pud_t *pud = (pud_t *)spp_getpage(); 265 p4d_populate(&init_mm, p4d, pud); 266 if (pud != pud_offset(p4d, 0)) 267 printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n", 268 pud, pud_offset(p4d, 0)); 269 } 270 return pud_offset(p4d, vaddr); 271 } 272 273 static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr) 274 { 275 if (pud_none(*pud)) { 276 pmd_t *pmd = (pmd_t *) spp_getpage(); 277 pud_populate(&init_mm, pud, pmd); 278 if (pmd != pmd_offset(pud, 0)) 279 printk(KERN_ERR "PAGETABLE BUG #02! %p <-> %p\n", 280 pmd, pmd_offset(pud, 0)); 281 } 282 return pmd_offset(pud, vaddr); 283 } 284 285 static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr) 286 { 287 if (pmd_none(*pmd)) { 288 pte_t *pte = (pte_t *) spp_getpage(); 289 pmd_populate_kernel(&init_mm, pmd, pte); 290 if (pte != pte_offset_kernel(pmd, 0)) 291 printk(KERN_ERR "PAGETABLE BUG #03!\n"); 292 } 293 return pte_offset_kernel(pmd, vaddr); 294 } 295 296 static void __set_pte_vaddr(pud_t *pud, unsigned long vaddr, pte_t new_pte) 297 { 298 pmd_t *pmd = fill_pmd(pud, vaddr); 299 pte_t *pte = fill_pte(pmd, vaddr); 300 301 set_pte(pte, new_pte); 302 303 /* 304 * It's enough to flush this one mapping. 305 * (PGE mappings get flushed as well) 306 */ 307 flush_tlb_one_kernel(vaddr); 308 } 309 310 void set_pte_vaddr_p4d(p4d_t *p4d_page, unsigned long vaddr, pte_t new_pte) 311 { 312 p4d_t *p4d = p4d_page + p4d_index(vaddr); 313 pud_t *pud = fill_pud(p4d, vaddr); 314 315 __set_pte_vaddr(pud, vaddr, new_pte); 316 } 317 318 void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte) 319 { 320 pud_t *pud = pud_page + pud_index(vaddr); 321 322 __set_pte_vaddr(pud, vaddr, new_pte); 323 } 324 325 void set_pte_vaddr(unsigned long vaddr, pte_t pteval) 326 { 327 pgd_t *pgd; 328 p4d_t *p4d_page; 329 330 pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval)); 331 332 pgd = pgd_offset_k(vaddr); 333 if (pgd_none(*pgd)) { 334 printk(KERN_ERR 335 "PGD FIXMAP MISSING, it should be setup in head.S!\n"); 336 return; 337 } 338 339 p4d_page = p4d_offset(pgd, 0); 340 set_pte_vaddr_p4d(p4d_page, vaddr, pteval); 341 } 342 343 pmd_t * __init populate_extra_pmd(unsigned long vaddr) 344 { 345 pgd_t *pgd; 346 p4d_t *p4d; 347 pud_t *pud; 348 349 pgd = pgd_offset_k(vaddr); 350 p4d = fill_p4d(pgd, vaddr); 351 pud = fill_pud(p4d, vaddr); 352 return fill_pmd(pud, vaddr); 353 } 354 355 pte_t * __init populate_extra_pte(unsigned long vaddr) 356 { 357 pmd_t *pmd; 358 359 pmd = populate_extra_pmd(vaddr); 360 return fill_pte(pmd, vaddr); 361 } 362 363 /* 364 * Create large page table mappings for a range of physical addresses. 365 */ 366 static void __init __init_extra_mapping(unsigned long phys, unsigned long size, 367 enum page_cache_mode cache) 368 { 369 pgd_t *pgd; 370 p4d_t *p4d; 371 pud_t *pud; 372 pmd_t *pmd; 373 pgprot_t prot; 374 375 pgprot_val(prot) = pgprot_val(PAGE_KERNEL_LARGE) | 376 protval_4k_2_large(cachemode2protval(cache)); 377 BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK)); 378 for (; size; phys += PMD_SIZE, size -= PMD_SIZE) { 379 pgd = pgd_offset_k((unsigned long)__va(phys)); 380 if (pgd_none(*pgd)) { 381 p4d = (p4d_t *) spp_getpage(); 382 set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE | 383 _PAGE_USER)); 384 } 385 p4d = p4d_offset(pgd, (unsigned long)__va(phys)); 386 if (p4d_none(*p4d)) { 387 pud = (pud_t *) spp_getpage(); 388 set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE | 389 _PAGE_USER)); 390 } 391 pud = pud_offset(p4d, (unsigned long)__va(phys)); 392 if (pud_none(*pud)) { 393 pmd = (pmd_t *) spp_getpage(); 394 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE | 395 _PAGE_USER)); 396 } 397 pmd = pmd_offset(pud, phys); 398 BUG_ON(!pmd_none(*pmd)); 399 set_pmd(pmd, __pmd(phys | pgprot_val(prot))); 400 } 401 } 402 403 void __init init_extra_mapping_wb(unsigned long phys, unsigned long size) 404 { 405 __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_WB); 406 } 407 408 void __init init_extra_mapping_uc(unsigned long phys, unsigned long size) 409 { 410 __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_UC); 411 } 412 413 /* 414 * The head.S code sets up the kernel high mapping: 415 * 416 * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text) 417 * 418 * phys_base holds the negative offset to the kernel, which is added 419 * to the compile time generated pmds. This results in invalid pmds up 420 * to the point where we hit the physaddr 0 mapping. 421 * 422 * We limit the mappings to the region from _text to _brk_end. _brk_end 423 * is rounded up to the 2MB boundary. This catches the invalid pmds as 424 * well, as they are located before _text: 425 */ 426 void __init cleanup_highmap(void) 427 { 428 unsigned long vaddr = __START_KERNEL_map; 429 unsigned long vaddr_end = __START_KERNEL_map + KERNEL_IMAGE_SIZE; 430 unsigned long end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1; 431 pmd_t *pmd = level2_kernel_pgt; 432 433 /* 434 * Native path, max_pfn_mapped is not set yet. 435 * Xen has valid max_pfn_mapped set in 436 * arch/x86/xen/mmu.c:xen_setup_kernel_pagetable(). 437 */ 438 if (max_pfn_mapped) 439 vaddr_end = __START_KERNEL_map + (max_pfn_mapped << PAGE_SHIFT); 440 441 for (; vaddr + PMD_SIZE - 1 < vaddr_end; pmd++, vaddr += PMD_SIZE) { 442 if (pmd_none(*pmd)) 443 continue; 444 if (vaddr < (unsigned long) _text || vaddr > end) 445 set_pmd(pmd, __pmd(0)); 446 } 447 } 448 449 /* 450 * Create PTE level page table mapping for physical addresses. 451 * It returns the last physical address mapped. 452 */ 453 static unsigned long __meminit 454 phys_pte_init(pte_t *pte_page, unsigned long paddr, unsigned long paddr_end, 455 pgprot_t prot, bool init) 456 { 457 unsigned long pages = 0, paddr_next; 458 unsigned long paddr_last = paddr_end; 459 pte_t *pte; 460 int i; 461 462 pte = pte_page + pte_index(paddr); 463 i = pte_index(paddr); 464 465 for (; i < PTRS_PER_PTE; i++, paddr = paddr_next, pte++) { 466 paddr_next = (paddr & PAGE_MASK) + PAGE_SIZE; 467 if (paddr >= paddr_end) { 468 if (!after_bootmem && 469 !e820__mapped_any(paddr & PAGE_MASK, paddr_next, 470 E820_TYPE_RAM) && 471 !e820__mapped_any(paddr & PAGE_MASK, paddr_next, 472 E820_TYPE_RESERVED_KERN) && 473 !e820__mapped_any(paddr & PAGE_MASK, paddr_next, 474 E820_TYPE_ACPI)) 475 set_pte_init(pte, __pte(0), init); 476 continue; 477 } 478 479 /* 480 * We will re-use the existing mapping. 481 * Xen for example has some special requirements, like mapping 482 * pagetable pages as RO. So assume someone who pre-setup 483 * these mappings are more intelligent. 484 */ 485 if (!pte_none(*pte)) { 486 if (!after_bootmem) 487 pages++; 488 continue; 489 } 490 491 if (0) 492 pr_info(" pte=%p addr=%lx pte=%016lx\n", pte, paddr, 493 pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL).pte); 494 pages++; 495 set_pte_init(pte, pfn_pte(paddr >> PAGE_SHIFT, prot), init); 496 paddr_last = (paddr & PAGE_MASK) + PAGE_SIZE; 497 } 498 499 update_page_count(PG_LEVEL_4K, pages); 500 501 return paddr_last; 502 } 503 504 /* 505 * Create PMD level page table mapping for physical addresses. The virtual 506 * and physical address have to be aligned at this level. 507 * It returns the last physical address mapped. 508 */ 509 static unsigned long __meminit 510 phys_pmd_init(pmd_t *pmd_page, unsigned long paddr, unsigned long paddr_end, 511 unsigned long page_size_mask, pgprot_t prot, bool init) 512 { 513 unsigned long pages = 0, paddr_next; 514 unsigned long paddr_last = paddr_end; 515 516 int i = pmd_index(paddr); 517 518 for (; i < PTRS_PER_PMD; i++, paddr = paddr_next) { 519 pmd_t *pmd = pmd_page + pmd_index(paddr); 520 pte_t *pte; 521 pgprot_t new_prot = prot; 522 523 paddr_next = (paddr & PMD_MASK) + PMD_SIZE; 524 if (paddr >= paddr_end) { 525 if (!after_bootmem && 526 !e820__mapped_any(paddr & PMD_MASK, paddr_next, 527 E820_TYPE_RAM) && 528 !e820__mapped_any(paddr & PMD_MASK, paddr_next, 529 E820_TYPE_RESERVED_KERN) && 530 !e820__mapped_any(paddr & PMD_MASK, paddr_next, 531 E820_TYPE_ACPI)) 532 set_pmd_init(pmd, __pmd(0), init); 533 continue; 534 } 535 536 if (!pmd_none(*pmd)) { 537 if (!pmd_leaf(*pmd)) { 538 spin_lock(&init_mm.page_table_lock); 539 pte = (pte_t *)pmd_page_vaddr(*pmd); 540 paddr_last = phys_pte_init(pte, paddr, 541 paddr_end, prot, 542 init); 543 spin_unlock(&init_mm.page_table_lock); 544 continue; 545 } 546 /* 547 * If we are ok with PG_LEVEL_2M mapping, then we will 548 * use the existing mapping, 549 * 550 * Otherwise, we will split the large page mapping but 551 * use the same existing protection bits except for 552 * large page, so that we don't violate Intel's TLB 553 * Application note (317080) which says, while changing 554 * the page sizes, new and old translations should 555 * not differ with respect to page frame and 556 * attributes. 557 */ 558 if (page_size_mask & (1 << PG_LEVEL_2M)) { 559 if (!after_bootmem) 560 pages++; 561 paddr_last = paddr_next; 562 continue; 563 } 564 new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd)); 565 } 566 567 if (page_size_mask & (1<<PG_LEVEL_2M)) { 568 pages++; 569 spin_lock(&init_mm.page_table_lock); 570 set_pmd_init(pmd, 571 pfn_pmd(paddr >> PAGE_SHIFT, prot_sethuge(prot)), 572 init); 573 spin_unlock(&init_mm.page_table_lock); 574 paddr_last = paddr_next; 575 continue; 576 } 577 578 pte = alloc_low_page(); 579 paddr_last = phys_pte_init(pte, paddr, paddr_end, new_prot, init); 580 581 spin_lock(&init_mm.page_table_lock); 582 pmd_populate_kernel_init(&init_mm, pmd, pte, init); 583 spin_unlock(&init_mm.page_table_lock); 584 } 585 update_page_count(PG_LEVEL_2M, pages); 586 return paddr_last; 587 } 588 589 /* 590 * Create PUD level page table mapping for physical addresses. The virtual 591 * and physical address do not have to be aligned at this level. KASLR can 592 * randomize virtual addresses up to this level. 593 * It returns the last physical address mapped. 594 */ 595 static unsigned long __meminit 596 phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end, 597 unsigned long page_size_mask, pgprot_t _prot, bool init) 598 { 599 unsigned long pages = 0, paddr_next; 600 unsigned long paddr_last = paddr_end; 601 unsigned long vaddr = (unsigned long)__va(paddr); 602 int i = pud_index(vaddr); 603 604 for (; i < PTRS_PER_PUD; i++, paddr = paddr_next) { 605 pud_t *pud; 606 pmd_t *pmd; 607 pgprot_t prot = _prot; 608 609 vaddr = (unsigned long)__va(paddr); 610 pud = pud_page + pud_index(vaddr); 611 paddr_next = (paddr & PUD_MASK) + PUD_SIZE; 612 613 if (paddr >= paddr_end) { 614 if (!after_bootmem && 615 !e820__mapped_any(paddr & PUD_MASK, paddr_next, 616 E820_TYPE_RAM) && 617 !e820__mapped_any(paddr & PUD_MASK, paddr_next, 618 E820_TYPE_RESERVED_KERN) && 619 !e820__mapped_any(paddr & PUD_MASK, paddr_next, 620 E820_TYPE_ACPI)) 621 set_pud_init(pud, __pud(0), init); 622 continue; 623 } 624 625 if (!pud_none(*pud)) { 626 if (!pud_leaf(*pud)) { 627 pmd = pmd_offset(pud, 0); 628 paddr_last = phys_pmd_init(pmd, paddr, 629 paddr_end, 630 page_size_mask, 631 prot, init); 632 continue; 633 } 634 /* 635 * If we are ok with PG_LEVEL_1G mapping, then we will 636 * use the existing mapping. 637 * 638 * Otherwise, we will split the gbpage mapping but use 639 * the same existing protection bits except for large 640 * page, so that we don't violate Intel's TLB 641 * Application note (317080) which says, while changing 642 * the page sizes, new and old translations should 643 * not differ with respect to page frame and 644 * attributes. 645 */ 646 if (page_size_mask & (1 << PG_LEVEL_1G)) { 647 if (!after_bootmem) 648 pages++; 649 paddr_last = paddr_next; 650 continue; 651 } 652 prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud)); 653 } 654 655 if (page_size_mask & (1<<PG_LEVEL_1G)) { 656 pages++; 657 spin_lock(&init_mm.page_table_lock); 658 set_pud_init(pud, 659 pfn_pud(paddr >> PAGE_SHIFT, prot_sethuge(prot)), 660 init); 661 spin_unlock(&init_mm.page_table_lock); 662 paddr_last = paddr_next; 663 continue; 664 } 665 666 pmd = alloc_low_page(); 667 paddr_last = phys_pmd_init(pmd, paddr, paddr_end, 668 page_size_mask, prot, init); 669 670 spin_lock(&init_mm.page_table_lock); 671 pud_populate_init(&init_mm, pud, pmd, init); 672 spin_unlock(&init_mm.page_table_lock); 673 } 674 675 update_page_count(PG_LEVEL_1G, pages); 676 677 return paddr_last; 678 } 679 680 static unsigned long __meminit 681 phys_p4d_init(p4d_t *p4d_page, unsigned long paddr, unsigned long paddr_end, 682 unsigned long page_size_mask, pgprot_t prot, bool init) 683 { 684 unsigned long vaddr, vaddr_end, vaddr_next, paddr_next, paddr_last; 685 686 paddr_last = paddr_end; 687 vaddr = (unsigned long)__va(paddr); 688 vaddr_end = (unsigned long)__va(paddr_end); 689 690 if (!pgtable_l5_enabled()) 691 return phys_pud_init((pud_t *) p4d_page, paddr, paddr_end, 692 page_size_mask, prot, init); 693 694 for (; vaddr < vaddr_end; vaddr = vaddr_next) { 695 p4d_t *p4d = p4d_page + p4d_index(vaddr); 696 pud_t *pud; 697 698 vaddr_next = (vaddr & P4D_MASK) + P4D_SIZE; 699 paddr = __pa(vaddr); 700 701 if (paddr >= paddr_end) { 702 paddr_next = __pa(vaddr_next); 703 if (!after_bootmem && 704 !e820__mapped_any(paddr & P4D_MASK, paddr_next, 705 E820_TYPE_RAM) && 706 !e820__mapped_any(paddr & P4D_MASK, paddr_next, 707 E820_TYPE_RESERVED_KERN) && 708 !e820__mapped_any(paddr & P4D_MASK, paddr_next, 709 E820_TYPE_ACPI)) 710 set_p4d_init(p4d, __p4d(0), init); 711 continue; 712 } 713 714 if (!p4d_none(*p4d)) { 715 pud = pud_offset(p4d, 0); 716 paddr_last = phys_pud_init(pud, paddr, __pa(vaddr_end), 717 page_size_mask, prot, init); 718 continue; 719 } 720 721 pud = alloc_low_page(); 722 paddr_last = phys_pud_init(pud, paddr, __pa(vaddr_end), 723 page_size_mask, prot, init); 724 725 spin_lock(&init_mm.page_table_lock); 726 p4d_populate_init(&init_mm, p4d, pud, init); 727 spin_unlock(&init_mm.page_table_lock); 728 } 729 730 return paddr_last; 731 } 732 733 static unsigned long __meminit 734 __kernel_physical_mapping_init(unsigned long paddr_start, 735 unsigned long paddr_end, 736 unsigned long page_size_mask, 737 pgprot_t prot, bool init) 738 { 739 bool pgd_changed = false; 740 unsigned long vaddr, vaddr_start, vaddr_end, vaddr_next, paddr_last; 741 742 paddr_last = paddr_end; 743 vaddr = (unsigned long)__va(paddr_start); 744 vaddr_end = (unsigned long)__va(paddr_end); 745 vaddr_start = vaddr; 746 747 for (; vaddr < vaddr_end; vaddr = vaddr_next) { 748 pgd_t *pgd = pgd_offset_k(vaddr); 749 p4d_t *p4d; 750 751 vaddr_next = (vaddr & PGDIR_MASK) + PGDIR_SIZE; 752 753 if (pgd_val(*pgd)) { 754 p4d = (p4d_t *)pgd_page_vaddr(*pgd); 755 paddr_last = phys_p4d_init(p4d, __pa(vaddr), 756 __pa(vaddr_end), 757 page_size_mask, 758 prot, init); 759 continue; 760 } 761 762 p4d = alloc_low_page(); 763 paddr_last = phys_p4d_init(p4d, __pa(vaddr), __pa(vaddr_end), 764 page_size_mask, prot, init); 765 766 spin_lock(&init_mm.page_table_lock); 767 if (pgtable_l5_enabled()) 768 pgd_populate_init(&init_mm, pgd, p4d, init); 769 else 770 p4d_populate_init(&init_mm, p4d_offset(pgd, vaddr), 771 (pud_t *) p4d, init); 772 773 spin_unlock(&init_mm.page_table_lock); 774 pgd_changed = true; 775 } 776 777 if (pgd_changed) 778 sync_global_pgds(vaddr_start, vaddr_end - 1); 779 780 return paddr_last; 781 } 782 783 784 /* 785 * Create page table mapping for the physical memory for specific physical 786 * addresses. Note that it can only be used to populate non-present entries. 787 * The virtual and physical addresses have to be aligned on PMD level 788 * down. It returns the last physical address mapped. 789 */ 790 unsigned long __meminit 791 kernel_physical_mapping_init(unsigned long paddr_start, 792 unsigned long paddr_end, 793 unsigned long page_size_mask, pgprot_t prot) 794 { 795 return __kernel_physical_mapping_init(paddr_start, paddr_end, 796 page_size_mask, prot, true); 797 } 798 799 /* 800 * This function is similar to kernel_physical_mapping_init() above with the 801 * exception that it uses set_{pud,pmd}() instead of the set_{pud,pte}_safe() 802 * when updating the mapping. The caller is responsible to flush the TLBs after 803 * the function returns. 804 */ 805 unsigned long __meminit 806 kernel_physical_mapping_change(unsigned long paddr_start, 807 unsigned long paddr_end, 808 unsigned long page_size_mask) 809 { 810 return __kernel_physical_mapping_init(paddr_start, paddr_end, 811 page_size_mask, PAGE_KERNEL, 812 false); 813 } 814 815 #ifndef CONFIG_NUMA 816 void __init initmem_init(void) 817 { 818 memblock_set_node(0, PHYS_ADDR_MAX, &memblock.memory, 0); 819 } 820 #endif 821 822 void __init paging_init(void) 823 { 824 sparse_init(); 825 826 /* 827 * clear the default setting with node 0 828 * note: don't use nodes_clear here, that is really clearing when 829 * numa support is not compiled in, and later node_set_state 830 * will not set it back. 831 */ 832 node_clear_state(0, N_MEMORY); 833 node_clear_state(0, N_NORMAL_MEMORY); 834 835 zone_sizes_init(); 836 } 837 838 #ifdef CONFIG_SPARSEMEM_VMEMMAP 839 #define PAGE_UNUSED 0xFD 840 841 /* 842 * The unused vmemmap range, which was not yet memset(PAGE_UNUSED), ranges 843 * from unused_pmd_start to next PMD_SIZE boundary. 844 */ 845 static unsigned long unused_pmd_start __meminitdata; 846 847 static void __meminit vmemmap_flush_unused_pmd(void) 848 { 849 if (!unused_pmd_start) 850 return; 851 /* 852 * Clears (unused_pmd_start, PMD_END] 853 */ 854 memset((void *)unused_pmd_start, PAGE_UNUSED, 855 ALIGN(unused_pmd_start, PMD_SIZE) - unused_pmd_start); 856 unused_pmd_start = 0; 857 } 858 859 #ifdef CONFIG_MEMORY_HOTPLUG 860 /* Returns true if the PMD is completely unused and thus it can be freed */ 861 static bool __meminit vmemmap_pmd_is_unused(unsigned long addr, unsigned long end) 862 { 863 unsigned long start = ALIGN_DOWN(addr, PMD_SIZE); 864 865 /* 866 * Flush the unused range cache to ensure that memchr_inv() will work 867 * for the whole range. 868 */ 869 vmemmap_flush_unused_pmd(); 870 memset((void *)addr, PAGE_UNUSED, end - addr); 871 872 return !memchr_inv((void *)start, PAGE_UNUSED, PMD_SIZE); 873 } 874 #endif 875 876 static void __meminit __vmemmap_use_sub_pmd(unsigned long start) 877 { 878 /* 879 * As we expect to add in the same granularity as we remove, it's 880 * sufficient to mark only some piece used to block the memmap page from 881 * getting removed when removing some other adjacent memmap (just in 882 * case the first memmap never gets initialized e.g., because the memory 883 * block never gets onlined). 884 */ 885 memset((void *)start, 0, sizeof(struct page)); 886 } 887 888 static void __meminit vmemmap_use_sub_pmd(unsigned long start, unsigned long end) 889 { 890 /* 891 * We only optimize if the new used range directly follows the 892 * previously unused range (esp., when populating consecutive sections). 893 */ 894 if (unused_pmd_start == start) { 895 if (likely(IS_ALIGNED(end, PMD_SIZE))) 896 unused_pmd_start = 0; 897 else 898 unused_pmd_start = end; 899 return; 900 } 901 902 /* 903 * If the range does not contiguously follows previous one, make sure 904 * to mark the unused range of the previous one so it can be removed. 905 */ 906 vmemmap_flush_unused_pmd(); 907 __vmemmap_use_sub_pmd(start); 908 } 909 910 911 static void __meminit vmemmap_use_new_sub_pmd(unsigned long start, unsigned long end) 912 { 913 const unsigned long page = ALIGN_DOWN(start, PMD_SIZE); 914 915 vmemmap_flush_unused_pmd(); 916 917 /* 918 * Could be our memmap page is filled with PAGE_UNUSED already from a 919 * previous remove. Make sure to reset it. 920 */ 921 __vmemmap_use_sub_pmd(start); 922 923 /* 924 * Mark with PAGE_UNUSED the unused parts of the new memmap range 925 */ 926 if (!IS_ALIGNED(start, PMD_SIZE)) 927 memset((void *)page, PAGE_UNUSED, start - page); 928 929 /* 930 * We want to avoid memset(PAGE_UNUSED) when populating the vmemmap of 931 * consecutive sections. Remember for the last added PMD where the 932 * unused range begins. 933 */ 934 if (!IS_ALIGNED(end, PMD_SIZE)) 935 unused_pmd_start = end; 936 } 937 #endif 938 939 /* 940 * Memory hotplug specific functions 941 */ 942 #ifdef CONFIG_MEMORY_HOTPLUG 943 /* 944 * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need 945 * updating. 946 */ 947 static void update_end_of_memory_vars(u64 start, u64 size) 948 { 949 unsigned long end_pfn = PFN_UP(start + size); 950 951 if (end_pfn > max_pfn) { 952 max_pfn = end_pfn; 953 max_low_pfn = end_pfn; 954 high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1; 955 } 956 } 957 958 int add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages, 959 struct mhp_params *params) 960 { 961 unsigned long end = ((start_pfn + nr_pages) << PAGE_SHIFT) - 1; 962 int ret; 963 964 if (WARN_ON_ONCE(end > DIRECT_MAP_PHYSMEM_END)) 965 return -ERANGE; 966 967 ret = __add_pages(nid, start_pfn, nr_pages, params); 968 WARN_ON_ONCE(ret); 969 970 /* update max_pfn, max_low_pfn and high_memory */ 971 update_end_of_memory_vars(start_pfn << PAGE_SHIFT, 972 nr_pages << PAGE_SHIFT); 973 974 return ret; 975 } 976 977 int arch_add_memory(int nid, u64 start, u64 size, 978 struct mhp_params *params) 979 { 980 unsigned long start_pfn = start >> PAGE_SHIFT; 981 unsigned long nr_pages = size >> PAGE_SHIFT; 982 983 init_memory_mapping(start, start + size, params->pgprot); 984 985 return add_pages(nid, start_pfn, nr_pages, params); 986 } 987 988 static void free_reserved_pages(struct page *page, unsigned long nr_pages) 989 { 990 while (nr_pages--) 991 free_reserved_page(page++); 992 } 993 994 static void __meminit free_pagetable(struct page *page, int order) 995 { 996 /* bootmem page has reserved flag */ 997 if (PageReserved(page)) { 998 unsigned long nr_pages = 1 << order; 999 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE 1000 enum bootmem_type type = bootmem_type(page); 1001 1002 if (type == SECTION_INFO || type == MIX_SECTION_INFO) { 1003 while (nr_pages--) 1004 put_page_bootmem(page++); 1005 } else { 1006 free_reserved_pages(page, nr_pages); 1007 } 1008 #else 1009 free_reserved_pages(page, nr_pages); 1010 #endif 1011 } else { 1012 free_pages((unsigned long)page_address(page), order); 1013 } 1014 } 1015 1016 static void __meminit free_hugepage_table(struct page *page, 1017 struct vmem_altmap *altmap) 1018 { 1019 if (altmap) 1020 vmem_altmap_free(altmap, PMD_SIZE / PAGE_SIZE); 1021 else 1022 free_pagetable(page, get_order(PMD_SIZE)); 1023 } 1024 1025 static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd) 1026 { 1027 pte_t *pte; 1028 int i; 1029 1030 for (i = 0; i < PTRS_PER_PTE; i++) { 1031 pte = pte_start + i; 1032 if (!pte_none(*pte)) 1033 return; 1034 } 1035 1036 /* free a pte table */ 1037 free_pagetable(pmd_page(*pmd), 0); 1038 spin_lock(&init_mm.page_table_lock); 1039 pmd_clear(pmd); 1040 spin_unlock(&init_mm.page_table_lock); 1041 } 1042 1043 static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud) 1044 { 1045 pmd_t *pmd; 1046 int i; 1047 1048 for (i = 0; i < PTRS_PER_PMD; i++) { 1049 pmd = pmd_start + i; 1050 if (!pmd_none(*pmd)) 1051 return; 1052 } 1053 1054 /* free a pmd table */ 1055 free_pagetable(pud_page(*pud), 0); 1056 spin_lock(&init_mm.page_table_lock); 1057 pud_clear(pud); 1058 spin_unlock(&init_mm.page_table_lock); 1059 } 1060 1061 static void __meminit free_pud_table(pud_t *pud_start, p4d_t *p4d) 1062 { 1063 pud_t *pud; 1064 int i; 1065 1066 for (i = 0; i < PTRS_PER_PUD; i++) { 1067 pud = pud_start + i; 1068 if (!pud_none(*pud)) 1069 return; 1070 } 1071 1072 /* free a pud table */ 1073 free_pagetable(p4d_page(*p4d), 0); 1074 spin_lock(&init_mm.page_table_lock); 1075 p4d_clear(p4d); 1076 spin_unlock(&init_mm.page_table_lock); 1077 } 1078 1079 static void __meminit 1080 remove_pte_table(pte_t *pte_start, unsigned long addr, unsigned long end, 1081 bool direct) 1082 { 1083 unsigned long next, pages = 0; 1084 pte_t *pte; 1085 phys_addr_t phys_addr; 1086 1087 pte = pte_start + pte_index(addr); 1088 for (; addr < end; addr = next, pte++) { 1089 next = (addr + PAGE_SIZE) & PAGE_MASK; 1090 if (next > end) 1091 next = end; 1092 1093 if (!pte_present(*pte)) 1094 continue; 1095 1096 /* 1097 * We mapped [0,1G) memory as identity mapping when 1098 * initializing, in arch/x86/kernel/head_64.S. These 1099 * pagetables cannot be removed. 1100 */ 1101 phys_addr = pte_val(*pte) + (addr & PAGE_MASK); 1102 if (phys_addr < (phys_addr_t)0x40000000) 1103 return; 1104 1105 if (!direct) 1106 free_pagetable(pte_page(*pte), 0); 1107 1108 spin_lock(&init_mm.page_table_lock); 1109 pte_clear(&init_mm, addr, pte); 1110 spin_unlock(&init_mm.page_table_lock); 1111 1112 /* For non-direct mapping, pages means nothing. */ 1113 pages++; 1114 } 1115 1116 /* Call free_pte_table() in remove_pmd_table(). */ 1117 flush_tlb_all(); 1118 if (direct) 1119 update_page_count(PG_LEVEL_4K, -pages); 1120 } 1121 1122 static void __meminit 1123 remove_pmd_table(pmd_t *pmd_start, unsigned long addr, unsigned long end, 1124 bool direct, struct vmem_altmap *altmap) 1125 { 1126 unsigned long next, pages = 0; 1127 pte_t *pte_base; 1128 pmd_t *pmd; 1129 1130 pmd = pmd_start + pmd_index(addr); 1131 for (; addr < end; addr = next, pmd++) { 1132 next = pmd_addr_end(addr, end); 1133 1134 if (!pmd_present(*pmd)) 1135 continue; 1136 1137 if (pmd_leaf(*pmd)) { 1138 if (IS_ALIGNED(addr, PMD_SIZE) && 1139 IS_ALIGNED(next, PMD_SIZE)) { 1140 if (!direct) 1141 free_hugepage_table(pmd_page(*pmd), 1142 altmap); 1143 1144 spin_lock(&init_mm.page_table_lock); 1145 pmd_clear(pmd); 1146 spin_unlock(&init_mm.page_table_lock); 1147 pages++; 1148 } 1149 #ifdef CONFIG_SPARSEMEM_VMEMMAP 1150 else if (vmemmap_pmd_is_unused(addr, next)) { 1151 free_hugepage_table(pmd_page(*pmd), 1152 altmap); 1153 spin_lock(&init_mm.page_table_lock); 1154 pmd_clear(pmd); 1155 spin_unlock(&init_mm.page_table_lock); 1156 } 1157 #endif 1158 continue; 1159 } 1160 1161 pte_base = (pte_t *)pmd_page_vaddr(*pmd); 1162 remove_pte_table(pte_base, addr, next, direct); 1163 free_pte_table(pte_base, pmd); 1164 } 1165 1166 /* Call free_pmd_table() in remove_pud_table(). */ 1167 if (direct) 1168 update_page_count(PG_LEVEL_2M, -pages); 1169 } 1170 1171 static void __meminit 1172 remove_pud_table(pud_t *pud_start, unsigned long addr, unsigned long end, 1173 struct vmem_altmap *altmap, bool direct) 1174 { 1175 unsigned long next, pages = 0; 1176 pmd_t *pmd_base; 1177 pud_t *pud; 1178 1179 pud = pud_start + pud_index(addr); 1180 for (; addr < end; addr = next, pud++) { 1181 next = pud_addr_end(addr, end); 1182 1183 if (!pud_present(*pud)) 1184 continue; 1185 1186 if (pud_leaf(*pud) && 1187 IS_ALIGNED(addr, PUD_SIZE) && 1188 IS_ALIGNED(next, PUD_SIZE)) { 1189 spin_lock(&init_mm.page_table_lock); 1190 pud_clear(pud); 1191 spin_unlock(&init_mm.page_table_lock); 1192 pages++; 1193 continue; 1194 } 1195 1196 pmd_base = pmd_offset(pud, 0); 1197 remove_pmd_table(pmd_base, addr, next, direct, altmap); 1198 free_pmd_table(pmd_base, pud); 1199 } 1200 1201 if (direct) 1202 update_page_count(PG_LEVEL_1G, -pages); 1203 } 1204 1205 static void __meminit 1206 remove_p4d_table(p4d_t *p4d_start, unsigned long addr, unsigned long end, 1207 struct vmem_altmap *altmap, bool direct) 1208 { 1209 unsigned long next, pages = 0; 1210 pud_t *pud_base; 1211 p4d_t *p4d; 1212 1213 p4d = p4d_start + p4d_index(addr); 1214 for (; addr < end; addr = next, p4d++) { 1215 next = p4d_addr_end(addr, end); 1216 1217 if (!p4d_present(*p4d)) 1218 continue; 1219 1220 BUILD_BUG_ON(p4d_leaf(*p4d)); 1221 1222 pud_base = pud_offset(p4d, 0); 1223 remove_pud_table(pud_base, addr, next, altmap, direct); 1224 /* 1225 * For 4-level page tables we do not want to free PUDs, but in the 1226 * 5-level case we should free them. This code will have to change 1227 * to adapt for boot-time switching between 4 and 5 level page tables. 1228 */ 1229 if (pgtable_l5_enabled()) 1230 free_pud_table(pud_base, p4d); 1231 } 1232 1233 if (direct) 1234 update_page_count(PG_LEVEL_512G, -pages); 1235 } 1236 1237 /* start and end are both virtual address. */ 1238 static void __meminit 1239 remove_pagetable(unsigned long start, unsigned long end, bool direct, 1240 struct vmem_altmap *altmap) 1241 { 1242 unsigned long next; 1243 unsigned long addr; 1244 pgd_t *pgd; 1245 p4d_t *p4d; 1246 1247 for (addr = start; addr < end; addr = next) { 1248 next = pgd_addr_end(addr, end); 1249 1250 pgd = pgd_offset_k(addr); 1251 if (!pgd_present(*pgd)) 1252 continue; 1253 1254 p4d = p4d_offset(pgd, 0); 1255 remove_p4d_table(p4d, addr, next, altmap, direct); 1256 } 1257 1258 flush_tlb_all(); 1259 } 1260 1261 void __ref vmemmap_free(unsigned long start, unsigned long end, 1262 struct vmem_altmap *altmap) 1263 { 1264 VM_BUG_ON(!PAGE_ALIGNED(start)); 1265 VM_BUG_ON(!PAGE_ALIGNED(end)); 1266 1267 remove_pagetable(start, end, false, altmap); 1268 } 1269 1270 static void __meminit 1271 kernel_physical_mapping_remove(unsigned long start, unsigned long end) 1272 { 1273 start = (unsigned long)__va(start); 1274 end = (unsigned long)__va(end); 1275 1276 remove_pagetable(start, end, true, NULL); 1277 } 1278 1279 void __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) 1280 { 1281 unsigned long start_pfn = start >> PAGE_SHIFT; 1282 unsigned long nr_pages = size >> PAGE_SHIFT; 1283 1284 __remove_pages(start_pfn, nr_pages, altmap); 1285 kernel_physical_mapping_remove(start, start + size); 1286 } 1287 #endif /* CONFIG_MEMORY_HOTPLUG */ 1288 1289 static struct kcore_list kcore_vsyscall; 1290 1291 static void __init register_page_bootmem_info(void) 1292 { 1293 #if defined(CONFIG_NUMA) || defined(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP) 1294 int i; 1295 1296 for_each_online_node(i) 1297 register_page_bootmem_info_node(NODE_DATA(i)); 1298 #endif 1299 } 1300 1301 /* 1302 * Pre-allocates page-table pages for the vmalloc area in the kernel page-table. 1303 * Only the level which needs to be synchronized between all page-tables is 1304 * allocated because the synchronization can be expensive. 1305 */ 1306 static void __init preallocate_vmalloc_pages(void) 1307 { 1308 unsigned long addr; 1309 const char *lvl; 1310 1311 for (addr = VMALLOC_START; addr <= VMEMORY_END; addr = ALIGN(addr + 1, PGDIR_SIZE)) { 1312 pgd_t *pgd = pgd_offset_k(addr); 1313 p4d_t *p4d; 1314 pud_t *pud; 1315 1316 lvl = "p4d"; 1317 p4d = p4d_alloc(&init_mm, pgd, addr); 1318 if (!p4d) 1319 goto failed; 1320 1321 if (pgtable_l5_enabled()) 1322 continue; 1323 1324 /* 1325 * The goal here is to allocate all possibly required 1326 * hardware page tables pointed to by the top hardware 1327 * level. 1328 * 1329 * On 4-level systems, the P4D layer is folded away and 1330 * the above code does no preallocation. Below, go down 1331 * to the pud _software_ level to ensure the second 1332 * hardware level is allocated on 4-level systems too. 1333 */ 1334 lvl = "pud"; 1335 pud = pud_alloc(&init_mm, p4d, addr); 1336 if (!pud) 1337 goto failed; 1338 } 1339 1340 return; 1341 1342 failed: 1343 1344 /* 1345 * The pages have to be there now or they will be missing in 1346 * process page-tables later. 1347 */ 1348 panic("Failed to pre-allocate %s pages for vmalloc area\n", lvl); 1349 } 1350 1351 void __init mem_init(void) 1352 { 1353 pci_iommu_alloc(); 1354 1355 /* clear_bss() already clear the empty_zero_page */ 1356 1357 /* this will put all memory onto the freelists */ 1358 memblock_free_all(); 1359 after_bootmem = 1; 1360 x86_init.hyper.init_after_bootmem(); 1361 1362 /* 1363 * Must be done after boot memory is put on freelist, because here we 1364 * might set fields in deferred struct pages that have not yet been 1365 * initialized, and memblock_free_all() initializes all the reserved 1366 * deferred pages for us. 1367 */ 1368 register_page_bootmem_info(); 1369 1370 /* Register memory areas for /proc/kcore */ 1371 if (get_gate_vma(&init_mm)) 1372 kclist_add(&kcore_vsyscall, (void *)VSYSCALL_ADDR, PAGE_SIZE, KCORE_USER); 1373 1374 preallocate_vmalloc_pages(); 1375 } 1376 1377 int kernel_set_to_readonly; 1378 1379 void mark_rodata_ro(void) 1380 { 1381 unsigned long start = PFN_ALIGN(_text); 1382 unsigned long rodata_start = PFN_ALIGN(__start_rodata); 1383 unsigned long end = (unsigned long)__end_rodata_hpage_align; 1384 unsigned long text_end = PFN_ALIGN(_etext); 1385 unsigned long rodata_end = PFN_ALIGN(__end_rodata); 1386 unsigned long all_end; 1387 1388 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n", 1389 (end - start) >> 10); 1390 set_memory_ro(start, (end - start) >> PAGE_SHIFT); 1391 1392 kernel_set_to_readonly = 1; 1393 1394 /* 1395 * The rodata/data/bss/brk section (but not the kernel text!) 1396 * should also be not-executable. 1397 * 1398 * We align all_end to PMD_SIZE because the existing mapping 1399 * is a full PMD. If we would align _brk_end to PAGE_SIZE we 1400 * split the PMD and the reminder between _brk_end and the end 1401 * of the PMD will remain mapped executable. 1402 * 1403 * Any PMD which was setup after the one which covers _brk_end 1404 * has been zapped already via cleanup_highmem(). 1405 */ 1406 all_end = roundup((unsigned long)_brk_end, PMD_SIZE); 1407 set_memory_nx(text_end, (all_end - text_end) >> PAGE_SHIFT); 1408 1409 set_ftrace_ops_ro(); 1410 1411 #ifdef CONFIG_CPA_DEBUG 1412 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end); 1413 set_memory_rw(start, (end-start) >> PAGE_SHIFT); 1414 1415 printk(KERN_INFO "Testing CPA: again\n"); 1416 set_memory_ro(start, (end-start) >> PAGE_SHIFT); 1417 #endif 1418 1419 free_kernel_image_pages("unused kernel image (text/rodata gap)", 1420 (void *)text_end, (void *)rodata_start); 1421 free_kernel_image_pages("unused kernel image (rodata/data gap)", 1422 (void *)rodata_end, (void *)_sdata); 1423 } 1424 1425 /* 1426 * Block size is the minimum amount of memory which can be hotplugged or 1427 * hotremoved. It must be power of two and must be equal or larger than 1428 * MIN_MEMORY_BLOCK_SIZE. 1429 */ 1430 #define MAX_BLOCK_SIZE (2UL << 30) 1431 1432 /* Amount of ram needed to start using large blocks */ 1433 #define MEM_SIZE_FOR_LARGE_BLOCK (64UL << 30) 1434 1435 /* Adjustable memory block size */ 1436 static unsigned long set_memory_block_size; 1437 int __init set_memory_block_size_order(unsigned int order) 1438 { 1439 unsigned long size = 1UL << order; 1440 1441 if (size > MEM_SIZE_FOR_LARGE_BLOCK || size < MIN_MEMORY_BLOCK_SIZE) 1442 return -EINVAL; 1443 1444 set_memory_block_size = size; 1445 return 0; 1446 } 1447 1448 static unsigned long probe_memory_block_size(void) 1449 { 1450 unsigned long boot_mem_end = max_pfn << PAGE_SHIFT; 1451 unsigned long bz; 1452 1453 /* If memory block size has been set, then use it */ 1454 bz = set_memory_block_size; 1455 if (bz) 1456 goto done; 1457 1458 /* Use regular block if RAM is smaller than MEM_SIZE_FOR_LARGE_BLOCK */ 1459 if (boot_mem_end < MEM_SIZE_FOR_LARGE_BLOCK) { 1460 bz = MIN_MEMORY_BLOCK_SIZE; 1461 goto done; 1462 } 1463 1464 /* 1465 * Use max block size to minimize overhead on bare metal, where 1466 * alignment for memory hotplug isn't a concern. 1467 */ 1468 if (!boot_cpu_has(X86_FEATURE_HYPERVISOR)) { 1469 bz = MAX_BLOCK_SIZE; 1470 goto done; 1471 } 1472 1473 /* Find the largest allowed block size that aligns to memory end */ 1474 for (bz = MAX_BLOCK_SIZE; bz > MIN_MEMORY_BLOCK_SIZE; bz >>= 1) { 1475 if (IS_ALIGNED(boot_mem_end, bz)) 1476 break; 1477 } 1478 done: 1479 pr_info("x86/mm: Memory block size: %ldMB\n", bz >> 20); 1480 1481 return bz; 1482 } 1483 1484 static unsigned long memory_block_size_probed; 1485 unsigned long memory_block_size_bytes(void) 1486 { 1487 if (!memory_block_size_probed) 1488 memory_block_size_probed = probe_memory_block_size(); 1489 1490 return memory_block_size_probed; 1491 } 1492 1493 #ifdef CONFIG_SPARSEMEM_VMEMMAP 1494 /* 1495 * Initialise the sparsemem vmemmap using huge-pages at the PMD level. 1496 */ 1497 static long __meminitdata addr_start, addr_end; 1498 static void __meminitdata *p_start, *p_end; 1499 static int __meminitdata node_start; 1500 1501 void __meminit vmemmap_set_pmd(pmd_t *pmd, void *p, int node, 1502 unsigned long addr, unsigned long next) 1503 { 1504 pte_t entry; 1505 1506 entry = pfn_pte(__pa(p) >> PAGE_SHIFT, 1507 PAGE_KERNEL_LARGE); 1508 set_pmd(pmd, __pmd(pte_val(entry))); 1509 1510 /* check to see if we have contiguous blocks */ 1511 if (p_end != p || node_start != node) { 1512 if (p_start) 1513 pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n", 1514 addr_start, addr_end-1, p_start, p_end-1, node_start); 1515 addr_start = addr; 1516 node_start = node; 1517 p_start = p; 1518 } 1519 1520 addr_end = addr + PMD_SIZE; 1521 p_end = p + PMD_SIZE; 1522 1523 if (!IS_ALIGNED(addr, PMD_SIZE) || 1524 !IS_ALIGNED(next, PMD_SIZE)) 1525 vmemmap_use_new_sub_pmd(addr, next); 1526 } 1527 1528 int __meminit vmemmap_check_pmd(pmd_t *pmd, int node, 1529 unsigned long addr, unsigned long next) 1530 { 1531 int large = pmd_leaf(*pmd); 1532 1533 if (pmd_leaf(*pmd)) { 1534 vmemmap_verify((pte_t *)pmd, node, addr, next); 1535 vmemmap_use_sub_pmd(addr, next); 1536 } 1537 1538 return large; 1539 } 1540 1541 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, 1542 struct vmem_altmap *altmap) 1543 { 1544 int err; 1545 1546 VM_BUG_ON(!PAGE_ALIGNED(start)); 1547 VM_BUG_ON(!PAGE_ALIGNED(end)); 1548 1549 if (end - start < PAGES_PER_SECTION * sizeof(struct page)) 1550 err = vmemmap_populate_basepages(start, end, node, NULL); 1551 else if (boot_cpu_has(X86_FEATURE_PSE)) 1552 err = vmemmap_populate_hugepages(start, end, node, altmap); 1553 else if (altmap) { 1554 pr_err_once("%s: no cpu support for altmap allocations\n", 1555 __func__); 1556 err = -ENOMEM; 1557 } else 1558 err = vmemmap_populate_basepages(start, end, node, NULL); 1559 if (!err) 1560 sync_global_pgds(start, end - 1); 1561 return err; 1562 } 1563 1564 #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE 1565 void register_page_bootmem_memmap(unsigned long section_nr, 1566 struct page *start_page, unsigned long nr_pages) 1567 { 1568 unsigned long addr = (unsigned long)start_page; 1569 unsigned long end = (unsigned long)(start_page + nr_pages); 1570 unsigned long next; 1571 pgd_t *pgd; 1572 p4d_t *p4d; 1573 pud_t *pud; 1574 pmd_t *pmd; 1575 unsigned int nr_pmd_pages; 1576 struct page *page; 1577 1578 for (; addr < end; addr = next) { 1579 pte_t *pte = NULL; 1580 1581 pgd = pgd_offset_k(addr); 1582 if (pgd_none(*pgd)) { 1583 next = (addr + PAGE_SIZE) & PAGE_MASK; 1584 continue; 1585 } 1586 get_page_bootmem(section_nr, pgd_page(*pgd), MIX_SECTION_INFO); 1587 1588 p4d = p4d_offset(pgd, addr); 1589 if (p4d_none(*p4d)) { 1590 next = (addr + PAGE_SIZE) & PAGE_MASK; 1591 continue; 1592 } 1593 get_page_bootmem(section_nr, p4d_page(*p4d), MIX_SECTION_INFO); 1594 1595 pud = pud_offset(p4d, addr); 1596 if (pud_none(*pud)) { 1597 next = (addr + PAGE_SIZE) & PAGE_MASK; 1598 continue; 1599 } 1600 get_page_bootmem(section_nr, pud_page(*pud), MIX_SECTION_INFO); 1601 1602 if (!boot_cpu_has(X86_FEATURE_PSE)) { 1603 next = (addr + PAGE_SIZE) & PAGE_MASK; 1604 pmd = pmd_offset(pud, addr); 1605 if (pmd_none(*pmd)) 1606 continue; 1607 get_page_bootmem(section_nr, pmd_page(*pmd), 1608 MIX_SECTION_INFO); 1609 1610 pte = pte_offset_kernel(pmd, addr); 1611 if (pte_none(*pte)) 1612 continue; 1613 get_page_bootmem(section_nr, pte_page(*pte), 1614 SECTION_INFO); 1615 } else { 1616 next = pmd_addr_end(addr, end); 1617 1618 pmd = pmd_offset(pud, addr); 1619 if (pmd_none(*pmd)) 1620 continue; 1621 1622 nr_pmd_pages = 1 << get_order(PMD_SIZE); 1623 page = pmd_page(*pmd); 1624 while (nr_pmd_pages--) 1625 get_page_bootmem(section_nr, page++, 1626 SECTION_INFO); 1627 } 1628 } 1629 } 1630 #endif 1631 1632 void __meminit vmemmap_populate_print_last(void) 1633 { 1634 if (p_start) { 1635 pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n", 1636 addr_start, addr_end-1, p_start, p_end-1, node_start); 1637 p_start = NULL; 1638 p_end = NULL; 1639 node_start = 0; 1640 } 1641 } 1642 #endif 1643