1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * mm_init.c - Memory initialisation verification and debugging 4 * 5 * Copyright 2008 IBM Corporation, 2008 6 * Author Mel Gorman <mel@csn.ul.ie> 7 * 8 */ 9 #include <linux/kernel.h> 10 #include <linux/init.h> 11 #include <linux/kobject.h> 12 #include <linux/export.h> 13 #include <linux/memory.h> 14 #include <linux/notifier.h> 15 #include <linux/sched.h> 16 #include <linux/mman.h> 17 #include <linux/memblock.h> 18 #include <linux/page-isolation.h> 19 #include <linux/padata.h> 20 #include <linux/nmi.h> 21 #include <linux/buffer_head.h> 22 #include <linux/kmemleak.h> 23 #include <linux/kfence.h> 24 #include <linux/page_ext.h> 25 #include <linux/pti.h> 26 #include <linux/pgtable.h> 27 #include <linux/stackdepot.h> 28 #include <linux/swap.h> 29 #include <linux/cma.h> 30 #include <linux/crash_dump.h> 31 #include <linux/execmem.h> 32 #include <linux/vmstat.h> 33 #include <linux/kexec_handover.h> 34 #include <linux/hugetlb.h> 35 #include "internal.h" 36 #include "slab.h" 37 #include "shuffle.h" 38 39 #include <asm/setup.h> 40 41 #ifndef CONFIG_NUMA 42 unsigned long max_mapnr; 43 EXPORT_SYMBOL(max_mapnr); 44 45 struct page *mem_map; 46 EXPORT_SYMBOL(mem_map); 47 #endif 48 49 /* 50 * high_memory defines the upper bound on direct map memory, then end 51 * of ZONE_NORMAL. 52 */ 53 void *high_memory; 54 EXPORT_SYMBOL(high_memory); 55 56 #ifdef CONFIG_DEBUG_MEMORY_INIT 57 int __meminitdata mminit_loglevel; 58 59 /* The zonelists are simply reported, validation is manual. */ 60 void __init mminit_verify_zonelist(void) 61 { 62 int nid; 63 64 if (mminit_loglevel < MMINIT_VERIFY) 65 return; 66 67 for_each_online_node(nid) { 68 pg_data_t *pgdat = NODE_DATA(nid); 69 struct zone *zone; 70 struct zoneref *z; 71 struct zonelist *zonelist; 72 int i, listid, zoneid; 73 74 for (i = 0; i < MAX_ZONELISTS * MAX_NR_ZONES; i++) { 75 76 /* Identify the zone and nodelist */ 77 zoneid = i % MAX_NR_ZONES; 78 listid = i / MAX_NR_ZONES; 79 zonelist = &pgdat->node_zonelists[listid]; 80 zone = &pgdat->node_zones[zoneid]; 81 if (!populated_zone(zone)) 82 continue; 83 84 /* Print information about the zonelist */ 85 printk(KERN_DEBUG "mminit::zonelist %s %d:%s = ", 86 listid > 0 ? "thisnode" : "general", nid, 87 zone->name); 88 89 /* Iterate the zonelist */ 90 for_each_zone_zonelist(zone, z, zonelist, zoneid) 91 pr_cont("%d:%s ", zone_to_nid(zone), zone->name); 92 pr_cont("\n"); 93 } 94 } 95 } 96 97 void __init mminit_verify_pageflags_layout(void) 98 { 99 int shift, width; 100 unsigned long or_mask, add_mask; 101 102 shift = BITS_PER_LONG; 103 width = shift - NR_NON_PAGEFLAG_BITS; 104 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_widths", 105 "Section %d Node %d Zone %d Lastcpupid %d Kasantag %d Gen %d Tier %d Flags %d\n", 106 SECTIONS_WIDTH, 107 NODES_WIDTH, 108 ZONES_WIDTH, 109 LAST_CPUPID_WIDTH, 110 KASAN_TAG_WIDTH, 111 LRU_GEN_WIDTH, 112 LRU_REFS_WIDTH, 113 NR_PAGEFLAGS); 114 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_shifts", 115 "Section %d Node %d Zone %d Lastcpupid %d Kasantag %d\n", 116 SECTIONS_SHIFT, 117 NODES_SHIFT, 118 ZONES_SHIFT, 119 LAST_CPUPID_SHIFT, 120 KASAN_TAG_WIDTH); 121 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_pgshifts", 122 "Section %lu Node %lu Zone %lu Lastcpupid %lu Kasantag %lu\n", 123 (unsigned long)SECTIONS_PGSHIFT, 124 (unsigned long)NODES_PGSHIFT, 125 (unsigned long)ZONES_PGSHIFT, 126 (unsigned long)LAST_CPUPID_PGSHIFT, 127 (unsigned long)KASAN_TAG_PGSHIFT); 128 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_nodezoneid", 129 "Node/Zone ID: %lu -> %lu\n", 130 (unsigned long)(ZONEID_PGOFF + ZONEID_SHIFT), 131 (unsigned long)ZONEID_PGOFF); 132 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_usage", 133 "location: %d -> %d layout %d -> %d unused %d -> %d page-flags\n", 134 shift, width, width, NR_PAGEFLAGS, NR_PAGEFLAGS, 0); 135 #ifdef NODE_NOT_IN_PAGE_FLAGS 136 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_nodeflags", 137 "Node not in page flags"); 138 #endif 139 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS 140 mminit_dprintk(MMINIT_TRACE, "pageflags_layout_nodeflags", 141 "Last cpupid not in page flags"); 142 #endif 143 144 if (SECTIONS_WIDTH) { 145 shift -= SECTIONS_WIDTH; 146 BUG_ON(shift != SECTIONS_PGSHIFT); 147 } 148 if (NODES_WIDTH) { 149 shift -= NODES_WIDTH; 150 BUG_ON(shift != NODES_PGSHIFT); 151 } 152 if (ZONES_WIDTH) { 153 shift -= ZONES_WIDTH; 154 BUG_ON(shift != ZONES_PGSHIFT); 155 } 156 157 /* Check for bitmask overlaps */ 158 or_mask = (ZONES_MASK << ZONES_PGSHIFT) | 159 (NODES_MASK << NODES_PGSHIFT) | 160 (SECTIONS_MASK << SECTIONS_PGSHIFT); 161 add_mask = (ZONES_MASK << ZONES_PGSHIFT) + 162 (NODES_MASK << NODES_PGSHIFT) + 163 (SECTIONS_MASK << SECTIONS_PGSHIFT); 164 BUG_ON(or_mask != add_mask); 165 } 166 167 static __init int set_mminit_loglevel(char *str) 168 { 169 get_option(&str, &mminit_loglevel); 170 return 0; 171 } 172 early_param("mminit_loglevel", set_mminit_loglevel); 173 #endif /* CONFIG_DEBUG_MEMORY_INIT */ 174 175 struct kobject *mm_kobj; 176 177 #ifdef CONFIG_SMP 178 s32 vm_committed_as_batch = 32; 179 180 void mm_compute_batch(int overcommit_policy) 181 { 182 u64 memsized_batch; 183 s32 nr = num_present_cpus(); 184 s32 batch = max_t(s32, nr*2, 32); 185 unsigned long ram_pages = totalram_pages(); 186 187 /* 188 * For policy OVERCOMMIT_NEVER, set batch size to 0.4% of 189 * (total memory/#cpus), and lift it to 25% for other policies 190 * to easy the possible lock contention for percpu_counter 191 * vm_committed_as, while the max limit is INT_MAX 192 */ 193 if (overcommit_policy == OVERCOMMIT_NEVER) 194 memsized_batch = min_t(u64, ram_pages/nr/256, INT_MAX); 195 else 196 memsized_batch = min_t(u64, ram_pages/nr/4, INT_MAX); 197 198 vm_committed_as_batch = max_t(s32, memsized_batch, batch); 199 } 200 201 static int __meminit mm_compute_batch_notifier(struct notifier_block *self, 202 unsigned long action, void *arg) 203 { 204 switch (action) { 205 case MEM_ONLINE: 206 case MEM_OFFLINE: 207 mm_compute_batch(sysctl_overcommit_memory); 208 break; 209 default: 210 break; 211 } 212 return NOTIFY_OK; 213 } 214 215 static int __init mm_compute_batch_init(void) 216 { 217 mm_compute_batch(sysctl_overcommit_memory); 218 hotplug_memory_notifier(mm_compute_batch_notifier, MM_COMPUTE_BATCH_PRI); 219 return 0; 220 } 221 222 __initcall(mm_compute_batch_init); 223 224 #endif 225 226 static int __init mm_sysfs_init(void) 227 { 228 mm_kobj = kobject_create_and_add("mm", kernel_kobj); 229 if (!mm_kobj) 230 return -ENOMEM; 231 232 return 0; 233 } 234 postcore_initcall(mm_sysfs_init); 235 236 static unsigned long arch_zone_lowest_possible_pfn[MAX_NR_ZONES] __initdata; 237 static unsigned long arch_zone_highest_possible_pfn[MAX_NR_ZONES] __initdata; 238 static unsigned long zone_movable_pfn[MAX_NUMNODES] __initdata; 239 240 static unsigned long required_kernelcore __initdata; 241 static unsigned long required_kernelcore_percent __initdata; 242 static unsigned long required_movablecore __initdata; 243 static unsigned long required_movablecore_percent __initdata; 244 245 static unsigned long nr_kernel_pages __initdata; 246 static unsigned long nr_all_pages __initdata; 247 248 static bool deferred_struct_pages __meminitdata; 249 250 static DEFINE_PER_CPU(struct per_cpu_nodestat, boot_nodestats); 251 252 static int __init cmdline_parse_core(char *p, unsigned long *core, 253 unsigned long *percent) 254 { 255 unsigned long long coremem; 256 char *endptr; 257 258 if (!p) 259 return -EINVAL; 260 261 /* Value may be a percentage of total memory, otherwise bytes */ 262 coremem = simple_strtoull(p, &endptr, 0); 263 if (*endptr == '%') { 264 /* Paranoid check for percent values greater than 100 */ 265 WARN_ON(coremem > 100); 266 267 *percent = coremem; 268 } else { 269 coremem = memparse(p, &p); 270 /* Paranoid check that UL is enough for the coremem value */ 271 WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX); 272 273 *core = coremem >> PAGE_SHIFT; 274 *percent = 0UL; 275 } 276 return 0; 277 } 278 279 bool mirrored_kernelcore __initdata_memblock; 280 281 /* 282 * kernelcore=size sets the amount of memory for use for allocations that 283 * cannot be reclaimed or migrated. 284 */ 285 static int __init cmdline_parse_kernelcore(char *p) 286 { 287 /* parse kernelcore=mirror */ 288 if (parse_option_str(p, "mirror")) { 289 mirrored_kernelcore = true; 290 return 0; 291 } 292 293 return cmdline_parse_core(p, &required_kernelcore, 294 &required_kernelcore_percent); 295 } 296 early_param("kernelcore", cmdline_parse_kernelcore); 297 298 /* 299 * movablecore=size sets the amount of memory for use for allocations that 300 * can be reclaimed or migrated. 301 */ 302 static int __init cmdline_parse_movablecore(char *p) 303 { 304 return cmdline_parse_core(p, &required_movablecore, 305 &required_movablecore_percent); 306 } 307 early_param("movablecore", cmdline_parse_movablecore); 308 309 /* 310 * early_calculate_totalpages() 311 * Sum pages in active regions for movable zone. 312 * Populate N_MEMORY for calculating usable_nodes. 313 */ 314 static unsigned long __init early_calculate_totalpages(void) 315 { 316 unsigned long totalpages = 0; 317 unsigned long start_pfn, end_pfn; 318 int i, nid; 319 320 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) { 321 unsigned long pages = end_pfn - start_pfn; 322 323 totalpages += pages; 324 if (pages) 325 node_set_state(nid, N_MEMORY); 326 } 327 return totalpages; 328 } 329 330 /* 331 * This finds a zone that can be used for ZONE_MOVABLE pages. The 332 * assumption is made that zones within a node are ordered in monotonic 333 * increasing memory addresses so that the "highest" populated zone is used 334 */ 335 static void __init find_usable_zone_for_movable(void) 336 { 337 int zone_index; 338 for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) { 339 if (zone_index == ZONE_MOVABLE) 340 continue; 341 342 if (arch_zone_highest_possible_pfn[zone_index] > 343 arch_zone_lowest_possible_pfn[zone_index]) 344 break; 345 } 346 347 VM_BUG_ON(zone_index == -1); 348 movable_zone = zone_index; 349 } 350 351 /* 352 * Find the PFN the Movable zone begins in each node. Kernel memory 353 * is spread evenly between nodes as long as the nodes have enough 354 * memory. When they don't, some nodes will have more kernelcore than 355 * others 356 */ 357 static void __init find_zone_movable_pfns_for_nodes(void) 358 { 359 int i, nid; 360 unsigned long usable_startpfn; 361 unsigned long kernelcore_node, kernelcore_remaining; 362 /* save the state before borrow the nodemask */ 363 nodemask_t saved_node_state = node_states[N_MEMORY]; 364 unsigned long totalpages = early_calculate_totalpages(); 365 int usable_nodes = nodes_weight(node_states[N_MEMORY]); 366 struct memblock_region *r; 367 368 /* Need to find movable_zone earlier when movable_node is specified. */ 369 find_usable_zone_for_movable(); 370 371 /* 372 * If movable_node is specified, ignore kernelcore and movablecore 373 * options. 374 */ 375 if (movable_node_is_enabled()) { 376 for_each_mem_region(r) { 377 if (!memblock_is_hotpluggable(r)) 378 continue; 379 380 nid = memblock_get_region_node(r); 381 382 usable_startpfn = memblock_region_memory_base_pfn(r); 383 zone_movable_pfn[nid] = zone_movable_pfn[nid] ? 384 min(usable_startpfn, zone_movable_pfn[nid]) : 385 usable_startpfn; 386 } 387 388 goto out2; 389 } 390 391 /* 392 * If kernelcore=mirror is specified, ignore movablecore option 393 */ 394 if (mirrored_kernelcore) { 395 bool mem_below_4gb_not_mirrored = false; 396 397 if (!memblock_has_mirror()) { 398 pr_warn("The system has no mirror memory, ignore kernelcore=mirror.\n"); 399 goto out; 400 } 401 402 if (is_kdump_kernel()) { 403 pr_warn("The system is under kdump, ignore kernelcore=mirror.\n"); 404 goto out; 405 } 406 407 for_each_mem_region(r) { 408 if (memblock_is_mirror(r)) 409 continue; 410 411 nid = memblock_get_region_node(r); 412 413 usable_startpfn = memblock_region_memory_base_pfn(r); 414 415 if (usable_startpfn < PHYS_PFN(SZ_4G)) { 416 mem_below_4gb_not_mirrored = true; 417 continue; 418 } 419 420 zone_movable_pfn[nid] = zone_movable_pfn[nid] ? 421 min(usable_startpfn, zone_movable_pfn[nid]) : 422 usable_startpfn; 423 } 424 425 if (mem_below_4gb_not_mirrored) 426 pr_warn("This configuration results in unmirrored kernel memory.\n"); 427 428 goto out2; 429 } 430 431 /* 432 * If kernelcore=nn% or movablecore=nn% was specified, calculate the 433 * amount of necessary memory. 434 */ 435 if (required_kernelcore_percent) 436 required_kernelcore = (totalpages * 100 * required_kernelcore_percent) / 437 10000UL; 438 if (required_movablecore_percent) 439 required_movablecore = (totalpages * 100 * required_movablecore_percent) / 440 10000UL; 441 442 /* 443 * If movablecore= was specified, calculate what size of 444 * kernelcore that corresponds so that memory usable for 445 * any allocation type is evenly spread. If both kernelcore 446 * and movablecore are specified, then the value of kernelcore 447 * will be used for required_kernelcore if it's greater than 448 * what movablecore would have allowed. 449 */ 450 if (required_movablecore) { 451 unsigned long corepages; 452 453 /* 454 * Round-up so that ZONE_MOVABLE is at least as large as what 455 * was requested by the user 456 */ 457 required_movablecore = 458 round_up(required_movablecore, MAX_ORDER_NR_PAGES); 459 required_movablecore = min(totalpages, required_movablecore); 460 corepages = totalpages - required_movablecore; 461 462 required_kernelcore = max(required_kernelcore, corepages); 463 } 464 465 /* 466 * If kernelcore was not specified or kernelcore size is larger 467 * than totalpages, there is no ZONE_MOVABLE. 468 */ 469 if (!required_kernelcore || required_kernelcore >= totalpages) 470 goto out; 471 472 /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */ 473 usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone]; 474 475 restart: 476 /* Spread kernelcore memory as evenly as possible throughout nodes */ 477 kernelcore_node = required_kernelcore / usable_nodes; 478 for_each_node_state(nid, N_MEMORY) { 479 unsigned long start_pfn, end_pfn; 480 481 /* 482 * Recalculate kernelcore_node if the division per node 483 * now exceeds what is necessary to satisfy the requested 484 * amount of memory for the kernel 485 */ 486 if (required_kernelcore < kernelcore_node) 487 kernelcore_node = required_kernelcore / usable_nodes; 488 489 /* 490 * As the map is walked, we track how much memory is usable 491 * by the kernel using kernelcore_remaining. When it is 492 * 0, the rest of the node is usable by ZONE_MOVABLE 493 */ 494 kernelcore_remaining = kernelcore_node; 495 496 /* Go through each range of PFNs within this node */ 497 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) { 498 unsigned long size_pages; 499 500 start_pfn = max(start_pfn, zone_movable_pfn[nid]); 501 if (start_pfn >= end_pfn) 502 continue; 503 504 /* Account for what is only usable for kernelcore */ 505 if (start_pfn < usable_startpfn) { 506 unsigned long kernel_pages; 507 kernel_pages = min(end_pfn, usable_startpfn) 508 - start_pfn; 509 510 kernelcore_remaining -= min(kernel_pages, 511 kernelcore_remaining); 512 required_kernelcore -= min(kernel_pages, 513 required_kernelcore); 514 515 /* Continue if range is now fully accounted */ 516 if (end_pfn <= usable_startpfn) { 517 518 /* 519 * Push zone_movable_pfn to the end so 520 * that if we have to rebalance 521 * kernelcore across nodes, we will 522 * not double account here 523 */ 524 zone_movable_pfn[nid] = end_pfn; 525 continue; 526 } 527 start_pfn = usable_startpfn; 528 } 529 530 /* 531 * The usable PFN range for ZONE_MOVABLE is from 532 * start_pfn->end_pfn. Calculate size_pages as the 533 * number of pages used as kernelcore 534 */ 535 size_pages = end_pfn - start_pfn; 536 if (size_pages > kernelcore_remaining) 537 size_pages = kernelcore_remaining; 538 zone_movable_pfn[nid] = start_pfn + size_pages; 539 540 /* 541 * Some kernelcore has been met, update counts and 542 * break if the kernelcore for this node has been 543 * satisfied 544 */ 545 required_kernelcore -= min(required_kernelcore, 546 size_pages); 547 kernelcore_remaining -= size_pages; 548 if (!kernelcore_remaining) 549 break; 550 } 551 } 552 553 /* 554 * If there is still required_kernelcore, we do another pass with one 555 * less node in the count. This will push zone_movable_pfn[nid] further 556 * along on the nodes that still have memory until kernelcore is 557 * satisfied 558 */ 559 usable_nodes--; 560 if (usable_nodes && required_kernelcore > usable_nodes) 561 goto restart; 562 563 out2: 564 /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */ 565 for_each_node_state(nid, N_MEMORY) { 566 unsigned long start_pfn, end_pfn; 567 568 zone_movable_pfn[nid] = 569 round_up(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES); 570 571 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn); 572 if (zone_movable_pfn[nid] >= end_pfn) 573 zone_movable_pfn[nid] = 0; 574 } 575 576 out: 577 /* restore the node_state */ 578 node_states[N_MEMORY] = saved_node_state; 579 } 580 581 void __meminit __init_single_page(struct page *page, unsigned long pfn, 582 unsigned long zone, int nid) 583 { 584 mm_zero_struct_page(page); 585 set_page_links(page, zone, nid, pfn); 586 init_page_count(page); 587 atomic_set(&page->_mapcount, -1); 588 page_cpupid_reset_last(page); 589 page_kasan_tag_reset(page); 590 591 INIT_LIST_HEAD(&page->lru); 592 #ifdef WANT_PAGE_VIRTUAL 593 /* The shift won't overflow because ZONE_NORMAL is below 4G. */ 594 if (!is_highmem_idx(zone)) 595 set_page_address(page, __va(pfn << PAGE_SHIFT)); 596 #endif 597 } 598 599 #ifdef CONFIG_NUMA 600 /* 601 * During memory init memblocks map pfns to nids. The search is expensive and 602 * this caches recent lookups. The implementation of __early_pfn_to_nid 603 * treats start/end as pfns. 604 */ 605 struct mminit_pfnnid_cache { 606 unsigned long last_start; 607 unsigned long last_end; 608 int last_nid; 609 }; 610 611 static struct mminit_pfnnid_cache early_pfnnid_cache __meminitdata; 612 613 /* 614 * Required by SPARSEMEM. Given a PFN, return what node the PFN is on. 615 */ 616 static int __meminit __early_pfn_to_nid(unsigned long pfn, 617 struct mminit_pfnnid_cache *state) 618 { 619 unsigned long start_pfn, end_pfn; 620 int nid; 621 622 if (state->last_start <= pfn && pfn < state->last_end) 623 return state->last_nid; 624 625 nid = memblock_search_pfn_nid(pfn, &start_pfn, &end_pfn); 626 if (nid != NUMA_NO_NODE) { 627 state->last_start = start_pfn; 628 state->last_end = end_pfn; 629 state->last_nid = nid; 630 } 631 632 return nid; 633 } 634 635 int __meminit early_pfn_to_nid(unsigned long pfn) 636 { 637 static DEFINE_SPINLOCK(early_pfn_lock); 638 int nid; 639 640 spin_lock(&early_pfn_lock); 641 nid = __early_pfn_to_nid(pfn, &early_pfnnid_cache); 642 if (nid < 0) 643 nid = first_online_node; 644 spin_unlock(&early_pfn_lock); 645 646 return nid; 647 } 648 649 bool hashdist = HASHDIST_DEFAULT; 650 651 static int __init set_hashdist(char *str) 652 { 653 return kstrtobool(str, &hashdist) == 0; 654 } 655 __setup("hashdist=", set_hashdist); 656 657 static inline void fixup_hashdist(void) 658 { 659 if (num_node_state(N_MEMORY) == 1) 660 hashdist = false; 661 } 662 #else 663 static inline void fixup_hashdist(void) {} 664 #endif /* CONFIG_NUMA */ 665 666 /* 667 * Initialize a reserved page unconditionally, finding its zone first. 668 */ 669 void __meminit __init_page_from_nid(unsigned long pfn, int nid) 670 { 671 pg_data_t *pgdat; 672 int zid; 673 674 pgdat = NODE_DATA(nid); 675 676 for (zid = 0; zid < MAX_NR_ZONES; zid++) { 677 struct zone *zone = &pgdat->node_zones[zid]; 678 679 if (zone_spans_pfn(zone, pfn)) 680 break; 681 } 682 __init_single_page(pfn_to_page(pfn), pfn, zid, nid); 683 684 if (pageblock_aligned(pfn)) 685 init_pageblock_migratetype(pfn_to_page(pfn), MIGRATE_MOVABLE, 686 false); 687 } 688 689 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT 690 static inline void pgdat_set_deferred_range(pg_data_t *pgdat) 691 { 692 pgdat->first_deferred_pfn = ULONG_MAX; 693 } 694 695 /* Returns true if the struct page for the pfn is initialised */ 696 static inline bool __meminit early_page_initialised(unsigned long pfn, int nid) 697 { 698 if (node_online(nid) && pfn >= NODE_DATA(nid)->first_deferred_pfn) 699 return false; 700 701 return true; 702 } 703 704 /* 705 * Returns true when the remaining initialisation should be deferred until 706 * later in the boot cycle when it can be parallelised. 707 */ 708 static bool __meminit 709 defer_init(int nid, unsigned long pfn, unsigned long end_pfn) 710 { 711 static unsigned long prev_end_pfn, nr_initialised; 712 713 if (early_page_ext_enabled()) 714 return false; 715 716 /* Always populate low zones for address-constrained allocations */ 717 if (end_pfn < pgdat_end_pfn(NODE_DATA(nid))) 718 return false; 719 720 if (NODE_DATA(nid)->first_deferred_pfn != ULONG_MAX) 721 return true; 722 723 /* 724 * prev_end_pfn static that contains the end of previous zone 725 * No need to protect because called very early in boot before smp_init. 726 */ 727 if (prev_end_pfn != end_pfn) { 728 prev_end_pfn = end_pfn; 729 nr_initialised = 0; 730 } 731 732 /* 733 * We start only with one section of pages, more pages are added as 734 * needed until the rest of deferred pages are initialized. 735 */ 736 nr_initialised++; 737 if ((nr_initialised > PAGES_PER_SECTION) && 738 (pfn & (PAGES_PER_SECTION - 1)) == 0) { 739 NODE_DATA(nid)->first_deferred_pfn = pfn; 740 return true; 741 } 742 return false; 743 } 744 745 static void __meminit __init_deferred_page(unsigned long pfn, int nid) 746 { 747 if (early_page_initialised(pfn, nid)) 748 return; 749 750 __init_page_from_nid(pfn, nid); 751 } 752 #else 753 static inline void pgdat_set_deferred_range(pg_data_t *pgdat) {} 754 755 static inline bool early_page_initialised(unsigned long pfn, int nid) 756 { 757 return true; 758 } 759 760 static inline bool defer_init(int nid, unsigned long pfn, unsigned long end_pfn) 761 { 762 return false; 763 } 764 765 static inline void __init_deferred_page(unsigned long pfn, int nid) 766 { 767 } 768 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ 769 770 void __meminit init_deferred_page(unsigned long pfn, int nid) 771 { 772 __init_deferred_page(pfn, nid); 773 } 774 775 /* 776 * Initialised pages do not have PageReserved set. This function is 777 * called for each range allocated by the bootmem allocator and 778 * marks the pages PageReserved. The remaining valid pages are later 779 * sent to the buddy page allocator. 780 */ 781 void __meminit reserve_bootmem_region(phys_addr_t start, 782 phys_addr_t end, int nid) 783 { 784 unsigned long pfn; 785 786 for_each_valid_pfn(pfn, PFN_DOWN(start), PFN_UP(end)) { 787 struct page *page = pfn_to_page(pfn); 788 789 __init_deferred_page(pfn, nid); 790 791 /* 792 * no need for atomic set_bit because the struct 793 * page is not visible yet so nobody should 794 * access it yet. 795 */ 796 __SetPageReserved(page); 797 } 798 } 799 800 /* If zone is ZONE_MOVABLE but memory is mirrored, it is an overlapped init */ 801 static bool __meminit 802 overlap_memmap_init(unsigned long zone, unsigned long *pfn) 803 { 804 static struct memblock_region *r; 805 806 if (mirrored_kernelcore && zone == ZONE_MOVABLE) { 807 if (!r || *pfn >= memblock_region_memory_end_pfn(r)) { 808 for_each_mem_region(r) { 809 if (*pfn < memblock_region_memory_end_pfn(r)) 810 break; 811 } 812 } 813 if (*pfn >= memblock_region_memory_base_pfn(r) && 814 memblock_is_mirror(r)) { 815 *pfn = memblock_region_memory_end_pfn(r); 816 return true; 817 } 818 } 819 return false; 820 } 821 822 /* 823 * Only struct pages that correspond to ranges defined by memblock.memory 824 * are zeroed and initialized by going through __init_single_page() during 825 * memmap_init_zone_range(). 826 * 827 * But, there could be struct pages that correspond to holes in 828 * memblock.memory. This can happen because of the following reasons: 829 * - physical memory bank size is not necessarily the exact multiple of the 830 * arbitrary section size 831 * - early reserved memory may not be listed in memblock.memory 832 * - non-memory regions covered by the contiguous flatmem mapping 833 * - memory layouts defined with memmap= kernel parameter may not align 834 * nicely with memmap sections 835 * 836 * Explicitly initialize those struct pages so that: 837 * - PG_Reserved is set 838 * - zone and node links point to zone and node that span the page if the 839 * hole is in the middle of a zone 840 * - zone and node links point to adjacent zone/node if the hole falls on 841 * the zone boundary; the pages in such holes will be prepended to the 842 * zone/node above the hole except for the trailing pages in the last 843 * section that will be appended to the zone/node below. 844 */ 845 static void __init init_unavailable_range(unsigned long spfn, 846 unsigned long epfn, 847 int zone, int node) 848 { 849 unsigned long pfn; 850 u64 pgcnt = 0; 851 852 for_each_valid_pfn(pfn, spfn, epfn) { 853 __init_single_page(pfn_to_page(pfn), pfn, zone, node); 854 __SetPageReserved(pfn_to_page(pfn)); 855 pgcnt++; 856 } 857 858 if (pgcnt) 859 pr_info("On node %d, zone %s: %lld pages in unavailable ranges\n", 860 node, zone_names[zone], pgcnt); 861 } 862 863 /* 864 * Initially all pages are reserved - free ones are freed 865 * up by memblock_free_all() once the early boot process is 866 * done. Non-atomic initialization, single-pass. 867 * 868 * All aligned pageblocks are initialized to the specified migratetype 869 * (usually MIGRATE_MOVABLE). Besides setting the migratetype, no related 870 * zone stats (e.g., nr_isolate_pageblock) are touched. 871 */ 872 void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone, 873 unsigned long start_pfn, unsigned long zone_end_pfn, 874 enum meminit_context context, 875 struct vmem_altmap *altmap, int migratetype, 876 bool isolate_pageblock) 877 { 878 unsigned long pfn, end_pfn = start_pfn + size; 879 struct page *page; 880 881 if (highest_memmap_pfn < end_pfn - 1) 882 highest_memmap_pfn = end_pfn - 1; 883 884 #ifdef CONFIG_ZONE_DEVICE 885 /* 886 * Honor reservation requested by the driver for this ZONE_DEVICE 887 * memory. We limit the total number of pages to initialize to just 888 * those that might contain the memory mapping. We will defer the 889 * ZONE_DEVICE page initialization until after we have released 890 * the hotplug lock. 891 */ 892 if (zone == ZONE_DEVICE) { 893 if (!altmap) 894 return; 895 896 if (start_pfn == altmap->base_pfn) 897 start_pfn += altmap->reserve; 898 end_pfn = altmap->base_pfn + vmem_altmap_offset(altmap); 899 } 900 #endif 901 902 for (pfn = start_pfn; pfn < end_pfn; ) { 903 /* 904 * There can be holes in boot-time mem_map[]s handed to this 905 * function. They do not exist on hotplugged memory. 906 */ 907 if (context == MEMINIT_EARLY) { 908 if (overlap_memmap_init(zone, &pfn)) 909 continue; 910 if (defer_init(nid, pfn, zone_end_pfn)) { 911 deferred_struct_pages = true; 912 break; 913 } 914 } 915 916 page = pfn_to_page(pfn); 917 __init_single_page(page, pfn, zone, nid); 918 if (context == MEMINIT_HOTPLUG) { 919 #ifdef CONFIG_ZONE_DEVICE 920 if (zone == ZONE_DEVICE) 921 __SetPageReserved(page); 922 else 923 #endif 924 __SetPageOffline(page); 925 } 926 927 /* 928 * Usually, we want to mark the pageblock MIGRATE_MOVABLE, 929 * such that unmovable allocations won't be scattered all 930 * over the place during system boot. 931 */ 932 if (pageblock_aligned(pfn)) { 933 init_pageblock_migratetype(page, migratetype, 934 isolate_pageblock); 935 cond_resched(); 936 } 937 pfn++; 938 } 939 } 940 941 static void __init memmap_init_zone_range(struct zone *zone, 942 unsigned long start_pfn, 943 unsigned long end_pfn, 944 unsigned long *hole_pfn) 945 { 946 unsigned long zone_start_pfn = zone->zone_start_pfn; 947 unsigned long zone_end_pfn = zone_start_pfn + zone->spanned_pages; 948 int nid = zone_to_nid(zone), zone_id = zone_idx(zone); 949 950 start_pfn = clamp(start_pfn, zone_start_pfn, zone_end_pfn); 951 end_pfn = clamp(end_pfn, zone_start_pfn, zone_end_pfn); 952 953 if (start_pfn >= end_pfn) 954 return; 955 956 memmap_init_range(end_pfn - start_pfn, nid, zone_id, start_pfn, 957 zone_end_pfn, MEMINIT_EARLY, NULL, MIGRATE_MOVABLE, 958 false); 959 960 if (*hole_pfn < start_pfn) 961 init_unavailable_range(*hole_pfn, start_pfn, zone_id, nid); 962 963 *hole_pfn = end_pfn; 964 } 965 966 static void __init memmap_init(void) 967 { 968 unsigned long start_pfn, end_pfn; 969 unsigned long hole_pfn = 0; 970 int i, j, zone_id = 0, nid; 971 972 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) { 973 struct pglist_data *node = NODE_DATA(nid); 974 975 for (j = 0; j < MAX_NR_ZONES; j++) { 976 struct zone *zone = node->node_zones + j; 977 978 if (!populated_zone(zone)) 979 continue; 980 981 memmap_init_zone_range(zone, start_pfn, end_pfn, 982 &hole_pfn); 983 zone_id = j; 984 } 985 } 986 987 /* 988 * Initialize the memory map for hole in the range [memory_end, 989 * section_end] for SPARSEMEM and in the range [memory_end, memmap_end] 990 * for FLATMEM. 991 * Append the pages in this hole to the highest zone in the last 992 * node. 993 */ 994 #ifdef CONFIG_SPARSEMEM 995 end_pfn = round_up(end_pfn, PAGES_PER_SECTION); 996 #else 997 end_pfn = round_up(end_pfn, MAX_ORDER_NR_PAGES); 998 #endif 999 if (hole_pfn < end_pfn) 1000 init_unavailable_range(hole_pfn, end_pfn, zone_id, nid); 1001 } 1002 1003 #ifdef CONFIG_ZONE_DEVICE 1004 static void __ref __init_zone_device_page(struct page *page, unsigned long pfn, 1005 unsigned long zone_idx, int nid, 1006 struct dev_pagemap *pgmap) 1007 { 1008 1009 __init_single_page(page, pfn, zone_idx, nid); 1010 1011 /* 1012 * Mark page reserved as it will need to wait for onlining 1013 * phase for it to be fully associated with a zone. 1014 * 1015 * We can use the non-atomic __set_bit operation for setting 1016 * the flag as we are still initializing the pages. 1017 */ 1018 __SetPageReserved(page); 1019 1020 /* 1021 * ZONE_DEVICE pages union ->lru with a ->pgmap back pointer 1022 * and zone_device_data. It is a bug if a ZONE_DEVICE page is 1023 * ever freed or placed on a driver-private list. 1024 */ 1025 page_folio(page)->pgmap = pgmap; 1026 page->zone_device_data = NULL; 1027 1028 /* 1029 * Mark the block movable so that blocks are reserved for 1030 * movable at startup. This will force kernel allocations 1031 * to reserve their blocks rather than leaking throughout 1032 * the address space during boot when many long-lived 1033 * kernel allocations are made. 1034 * 1035 * Please note that MEMINIT_HOTPLUG path doesn't clear memmap 1036 * because this is done early in section_activate() 1037 */ 1038 if (pageblock_aligned(pfn)) { 1039 init_pageblock_migratetype(page, MIGRATE_MOVABLE, false); 1040 cond_resched(); 1041 } 1042 1043 /* 1044 * ZONE_DEVICE pages other than MEMORY_TYPE_GENERIC are released 1045 * directly to the driver page allocator which will set the page count 1046 * to 1 when allocating the page. 1047 * 1048 * MEMORY_TYPE_GENERIC and MEMORY_TYPE_FS_DAX pages automatically have 1049 * their refcount reset to one whenever they are freed (ie. after 1050 * their refcount drops to 0). 1051 */ 1052 switch (pgmap->type) { 1053 case MEMORY_DEVICE_FS_DAX: 1054 case MEMORY_DEVICE_PRIVATE: 1055 case MEMORY_DEVICE_COHERENT: 1056 case MEMORY_DEVICE_PCI_P2PDMA: 1057 set_page_count(page, 0); 1058 break; 1059 1060 case MEMORY_DEVICE_GENERIC: 1061 break; 1062 } 1063 } 1064 1065 /* 1066 * With compound page geometry and when struct pages are stored in ram most 1067 * tail pages are reused. Consequently, the amount of unique struct pages to 1068 * initialize is a lot smaller that the total amount of struct pages being 1069 * mapped. This is a paired / mild layering violation with explicit knowledge 1070 * of how the sparse_vmemmap internals handle compound pages in the lack 1071 * of an altmap. See vmemmap_populate_compound_pages(). 1072 */ 1073 static inline unsigned long compound_nr_pages(struct vmem_altmap *altmap, 1074 struct dev_pagemap *pgmap) 1075 { 1076 if (!vmemmap_can_optimize(altmap, pgmap)) 1077 return pgmap_vmemmap_nr(pgmap); 1078 1079 return VMEMMAP_RESERVE_NR * (PAGE_SIZE / sizeof(struct page)); 1080 } 1081 1082 static void __ref memmap_init_compound(struct page *head, 1083 unsigned long head_pfn, 1084 unsigned long zone_idx, int nid, 1085 struct dev_pagemap *pgmap, 1086 unsigned long nr_pages) 1087 { 1088 unsigned long pfn, end_pfn = head_pfn + nr_pages; 1089 unsigned int order = pgmap->vmemmap_shift; 1090 1091 /* 1092 * We have to initialize the pages, including setting up page links. 1093 * prep_compound_page() does not take care of that, so instead we 1094 * open-code prep_compound_page() so we can take care of initializing 1095 * the pages in the same go. 1096 */ 1097 __SetPageHead(head); 1098 for (pfn = head_pfn + 1; pfn < end_pfn; pfn++) { 1099 struct page *page = pfn_to_page(pfn); 1100 1101 __init_zone_device_page(page, pfn, zone_idx, nid, pgmap); 1102 prep_compound_tail(head, pfn - head_pfn); 1103 set_page_count(page, 0); 1104 } 1105 prep_compound_head(head, order); 1106 } 1107 1108 void __ref memmap_init_zone_device(struct zone *zone, 1109 unsigned long start_pfn, 1110 unsigned long nr_pages, 1111 struct dev_pagemap *pgmap) 1112 { 1113 unsigned long pfn, end_pfn = start_pfn + nr_pages; 1114 struct pglist_data *pgdat = zone->zone_pgdat; 1115 struct vmem_altmap *altmap = pgmap_altmap(pgmap); 1116 unsigned int pfns_per_compound = pgmap_vmemmap_nr(pgmap); 1117 unsigned long zone_idx = zone_idx(zone); 1118 unsigned long start = jiffies; 1119 int nid = pgdat->node_id; 1120 1121 if (WARN_ON_ONCE(!pgmap || zone_idx != ZONE_DEVICE)) 1122 return; 1123 1124 /* 1125 * The call to memmap_init should have already taken care 1126 * of the pages reserved for the memmap, so we can just jump to 1127 * the end of that region and start processing the device pages. 1128 */ 1129 if (altmap) { 1130 start_pfn = altmap->base_pfn + vmem_altmap_offset(altmap); 1131 nr_pages = end_pfn - start_pfn; 1132 } 1133 1134 for (pfn = start_pfn; pfn < end_pfn; pfn += pfns_per_compound) { 1135 struct page *page = pfn_to_page(pfn); 1136 1137 __init_zone_device_page(page, pfn, zone_idx, nid, pgmap); 1138 1139 if (pfns_per_compound == 1) 1140 continue; 1141 1142 memmap_init_compound(page, pfn, zone_idx, nid, pgmap, 1143 compound_nr_pages(altmap, pgmap)); 1144 } 1145 1146 pr_debug("%s initialised %lu pages in %ums\n", __func__, 1147 nr_pages, jiffies_to_msecs(jiffies - start)); 1148 } 1149 #endif 1150 1151 /* 1152 * The zone ranges provided by the architecture do not include ZONE_MOVABLE 1153 * because it is sized independent of architecture. Unlike the other zones, 1154 * the starting point for ZONE_MOVABLE is not fixed. It may be different 1155 * in each node depending on the size of each node and how evenly kernelcore 1156 * is distributed. This helper function adjusts the zone ranges 1157 * provided by the architecture for a given node by using the end of the 1158 * highest usable zone for ZONE_MOVABLE. This preserves the assumption that 1159 * zones within a node are in order of monotonic increases memory addresses 1160 */ 1161 static void __init adjust_zone_range_for_zone_movable(int nid, 1162 unsigned long zone_type, 1163 unsigned long node_end_pfn, 1164 unsigned long *zone_start_pfn, 1165 unsigned long *zone_end_pfn) 1166 { 1167 /* Only adjust if ZONE_MOVABLE is on this node */ 1168 if (zone_movable_pfn[nid]) { 1169 /* Size ZONE_MOVABLE */ 1170 if (zone_type == ZONE_MOVABLE) { 1171 *zone_start_pfn = zone_movable_pfn[nid]; 1172 *zone_end_pfn = min(node_end_pfn, 1173 arch_zone_highest_possible_pfn[movable_zone]); 1174 1175 /* Adjust for ZONE_MOVABLE starting within this range */ 1176 } else if (!mirrored_kernelcore && 1177 *zone_start_pfn < zone_movable_pfn[nid] && 1178 *zone_end_pfn > zone_movable_pfn[nid]) { 1179 *zone_end_pfn = zone_movable_pfn[nid]; 1180 1181 /* Check if this whole range is within ZONE_MOVABLE */ 1182 } else if (*zone_start_pfn >= zone_movable_pfn[nid]) 1183 *zone_start_pfn = *zone_end_pfn; 1184 } 1185 } 1186 1187 /* 1188 * Return the number of holes in a range on a node. If nid is MAX_NUMNODES, 1189 * then all holes in the requested range will be accounted for. 1190 */ 1191 static unsigned long __init __absent_pages_in_range(int nid, 1192 unsigned long range_start_pfn, 1193 unsigned long range_end_pfn) 1194 { 1195 unsigned long nr_absent = range_end_pfn - range_start_pfn; 1196 unsigned long start_pfn, end_pfn; 1197 int i; 1198 1199 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) { 1200 start_pfn = clamp(start_pfn, range_start_pfn, range_end_pfn); 1201 end_pfn = clamp(end_pfn, range_start_pfn, range_end_pfn); 1202 nr_absent -= end_pfn - start_pfn; 1203 } 1204 return nr_absent; 1205 } 1206 1207 /** 1208 * absent_pages_in_range - Return number of page frames in holes within a range 1209 * @start_pfn: The start PFN to start searching for holes 1210 * @end_pfn: The end PFN to stop searching for holes 1211 * 1212 * Return: the number of pages frames in memory holes within a range. 1213 */ 1214 unsigned long __init absent_pages_in_range(unsigned long start_pfn, 1215 unsigned long end_pfn) 1216 { 1217 return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn); 1218 } 1219 1220 /* Return the number of page frames in holes in a zone on a node */ 1221 static unsigned long __init zone_absent_pages_in_node(int nid, 1222 unsigned long zone_type, 1223 unsigned long zone_start_pfn, 1224 unsigned long zone_end_pfn) 1225 { 1226 unsigned long nr_absent; 1227 1228 /* zone is empty, we don't have any absent pages */ 1229 if (zone_start_pfn == zone_end_pfn) 1230 return 0; 1231 1232 nr_absent = __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn); 1233 1234 /* 1235 * ZONE_MOVABLE handling. 1236 * Treat pages to be ZONE_MOVABLE in ZONE_NORMAL as absent pages 1237 * and vice versa. 1238 */ 1239 if (mirrored_kernelcore && zone_movable_pfn[nid]) { 1240 unsigned long start_pfn, end_pfn; 1241 struct memblock_region *r; 1242 1243 for_each_mem_region(r) { 1244 start_pfn = clamp(memblock_region_memory_base_pfn(r), 1245 zone_start_pfn, zone_end_pfn); 1246 end_pfn = clamp(memblock_region_memory_end_pfn(r), 1247 zone_start_pfn, zone_end_pfn); 1248 1249 if (zone_type == ZONE_MOVABLE && 1250 memblock_is_mirror(r)) 1251 nr_absent += end_pfn - start_pfn; 1252 1253 if (zone_type == ZONE_NORMAL && 1254 !memblock_is_mirror(r)) 1255 nr_absent += end_pfn - start_pfn; 1256 } 1257 } 1258 1259 return nr_absent; 1260 } 1261 1262 /* 1263 * Return the number of pages a zone spans in a node, including holes 1264 * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node() 1265 */ 1266 static unsigned long __init zone_spanned_pages_in_node(int nid, 1267 unsigned long zone_type, 1268 unsigned long node_start_pfn, 1269 unsigned long node_end_pfn, 1270 unsigned long *zone_start_pfn, 1271 unsigned long *zone_end_pfn) 1272 { 1273 unsigned long zone_low = arch_zone_lowest_possible_pfn[zone_type]; 1274 unsigned long zone_high = arch_zone_highest_possible_pfn[zone_type]; 1275 1276 /* Get the start and end of the zone */ 1277 *zone_start_pfn = clamp(node_start_pfn, zone_low, zone_high); 1278 *zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high); 1279 adjust_zone_range_for_zone_movable(nid, zone_type, node_end_pfn, 1280 zone_start_pfn, zone_end_pfn); 1281 1282 /* Check that this node has pages within the zone's required range */ 1283 if (*zone_end_pfn < node_start_pfn || *zone_start_pfn > node_end_pfn) 1284 return 0; 1285 1286 /* Move the zone boundaries inside the node if necessary */ 1287 *zone_end_pfn = min(*zone_end_pfn, node_end_pfn); 1288 *zone_start_pfn = max(*zone_start_pfn, node_start_pfn); 1289 1290 /* Return the spanned pages */ 1291 return *zone_end_pfn - *zone_start_pfn; 1292 } 1293 1294 static void __init reset_memoryless_node_totalpages(struct pglist_data *pgdat) 1295 { 1296 struct zone *z; 1297 1298 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) { 1299 z->zone_start_pfn = 0; 1300 z->spanned_pages = 0; 1301 z->present_pages = 0; 1302 #if defined(CONFIG_MEMORY_HOTPLUG) 1303 z->present_early_pages = 0; 1304 #endif 1305 } 1306 1307 pgdat->node_spanned_pages = 0; 1308 pgdat->node_present_pages = 0; 1309 pr_debug("On node %d totalpages: 0\n", pgdat->node_id); 1310 } 1311 1312 static void __init calc_nr_kernel_pages(void) 1313 { 1314 unsigned long start_pfn, end_pfn; 1315 phys_addr_t start_addr, end_addr; 1316 u64 u; 1317 #ifdef CONFIG_HIGHMEM 1318 unsigned long high_zone_low = arch_zone_lowest_possible_pfn[ZONE_HIGHMEM]; 1319 #endif 1320 1321 for_each_free_mem_range(u, NUMA_NO_NODE, MEMBLOCK_NONE, &start_addr, &end_addr, NULL) { 1322 start_pfn = PFN_UP(start_addr); 1323 end_pfn = PFN_DOWN(end_addr); 1324 1325 if (start_pfn < end_pfn) { 1326 nr_all_pages += end_pfn - start_pfn; 1327 #ifdef CONFIG_HIGHMEM 1328 start_pfn = clamp(start_pfn, 0, high_zone_low); 1329 end_pfn = clamp(end_pfn, 0, high_zone_low); 1330 #endif 1331 nr_kernel_pages += end_pfn - start_pfn; 1332 } 1333 } 1334 } 1335 1336 static void __init calculate_node_totalpages(struct pglist_data *pgdat, 1337 unsigned long node_start_pfn, 1338 unsigned long node_end_pfn) 1339 { 1340 unsigned long realtotalpages = 0, totalpages = 0; 1341 enum zone_type i; 1342 1343 for (i = 0; i < MAX_NR_ZONES; i++) { 1344 struct zone *zone = pgdat->node_zones + i; 1345 unsigned long zone_start_pfn, zone_end_pfn; 1346 unsigned long spanned, absent; 1347 unsigned long real_size; 1348 1349 spanned = zone_spanned_pages_in_node(pgdat->node_id, i, 1350 node_start_pfn, 1351 node_end_pfn, 1352 &zone_start_pfn, 1353 &zone_end_pfn); 1354 absent = zone_absent_pages_in_node(pgdat->node_id, i, 1355 zone_start_pfn, 1356 zone_end_pfn); 1357 1358 real_size = spanned - absent; 1359 1360 if (spanned) 1361 zone->zone_start_pfn = zone_start_pfn; 1362 else 1363 zone->zone_start_pfn = 0; 1364 zone->spanned_pages = spanned; 1365 zone->present_pages = real_size; 1366 #if defined(CONFIG_MEMORY_HOTPLUG) 1367 zone->present_early_pages = real_size; 1368 #endif 1369 1370 totalpages += spanned; 1371 realtotalpages += real_size; 1372 } 1373 1374 pgdat->node_spanned_pages = totalpages; 1375 pgdat->node_present_pages = realtotalpages; 1376 pr_debug("On node %d totalpages: %lu\n", pgdat->node_id, realtotalpages); 1377 } 1378 1379 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1380 static void pgdat_init_split_queue(struct pglist_data *pgdat) 1381 { 1382 struct deferred_split *ds_queue = &pgdat->deferred_split_queue; 1383 1384 spin_lock_init(&ds_queue->split_queue_lock); 1385 INIT_LIST_HEAD(&ds_queue->split_queue); 1386 ds_queue->split_queue_len = 0; 1387 } 1388 #else 1389 static void pgdat_init_split_queue(struct pglist_data *pgdat) {} 1390 #endif 1391 1392 #ifdef CONFIG_COMPACTION 1393 static void pgdat_init_kcompactd(struct pglist_data *pgdat) 1394 { 1395 init_waitqueue_head(&pgdat->kcompactd_wait); 1396 } 1397 #else 1398 static void pgdat_init_kcompactd(struct pglist_data *pgdat) {} 1399 #endif 1400 1401 static void __meminit pgdat_init_internals(struct pglist_data *pgdat) 1402 { 1403 int i; 1404 1405 pgdat_resize_init(pgdat); 1406 pgdat_kswapd_lock_init(pgdat); 1407 1408 pgdat_init_split_queue(pgdat); 1409 pgdat_init_kcompactd(pgdat); 1410 1411 init_waitqueue_head(&pgdat->kswapd_wait); 1412 init_waitqueue_head(&pgdat->pfmemalloc_wait); 1413 1414 for (i = 0; i < NR_VMSCAN_THROTTLE; i++) 1415 init_waitqueue_head(&pgdat->reclaim_wait[i]); 1416 1417 pgdat_page_ext_init(pgdat); 1418 lruvec_init(&pgdat->__lruvec); 1419 } 1420 1421 static void __meminit zone_init_internals(struct zone *zone, enum zone_type idx, int nid, 1422 unsigned long remaining_pages) 1423 { 1424 atomic_long_set(&zone->managed_pages, remaining_pages); 1425 zone_set_nid(zone, nid); 1426 zone->name = zone_names[idx]; 1427 zone->zone_pgdat = NODE_DATA(nid); 1428 spin_lock_init(&zone->lock); 1429 zone_seqlock_init(zone); 1430 zone_pcp_init(zone); 1431 } 1432 1433 static void __meminit zone_init_free_lists(struct zone *zone) 1434 { 1435 unsigned int order, t; 1436 for_each_migratetype_order(order, t) { 1437 INIT_LIST_HEAD(&zone->free_area[order].free_list[t]); 1438 zone->free_area[order].nr_free = 0; 1439 } 1440 1441 #ifdef CONFIG_UNACCEPTED_MEMORY 1442 INIT_LIST_HEAD(&zone->unaccepted_pages); 1443 #endif 1444 } 1445 1446 void __meminit init_currently_empty_zone(struct zone *zone, 1447 unsigned long zone_start_pfn, 1448 unsigned long size) 1449 { 1450 struct pglist_data *pgdat = zone->zone_pgdat; 1451 int zone_idx = zone_idx(zone) + 1; 1452 1453 if (zone_idx > pgdat->nr_zones) 1454 pgdat->nr_zones = zone_idx; 1455 1456 zone->zone_start_pfn = zone_start_pfn; 1457 1458 mminit_dprintk(MMINIT_TRACE, "memmap_init", 1459 "Initialising map node %d zone %lu pfns %lu -> %lu\n", 1460 pgdat->node_id, 1461 (unsigned long)zone_idx(zone), 1462 zone_start_pfn, (zone_start_pfn + size)); 1463 1464 zone_init_free_lists(zone); 1465 zone->initialized = 1; 1466 } 1467 1468 #ifndef CONFIG_SPARSEMEM 1469 /* 1470 * Calculate the size of the zone->pageblock_flags rounded to an unsigned long 1471 * Start by making sure zonesize is a multiple of pageblock_order by rounding 1472 * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally 1473 * round what is now in bits to nearest long in bits, then return it in 1474 * bytes. 1475 */ 1476 static unsigned long __init usemap_size(unsigned long zone_start_pfn, unsigned long zonesize) 1477 { 1478 unsigned long usemapsize; 1479 1480 zonesize += zone_start_pfn & (pageblock_nr_pages-1); 1481 usemapsize = round_up(zonesize, pageblock_nr_pages); 1482 usemapsize = usemapsize >> pageblock_order; 1483 usemapsize *= NR_PAGEBLOCK_BITS; 1484 usemapsize = round_up(usemapsize, BITS_PER_LONG); 1485 1486 return usemapsize / BITS_PER_BYTE; 1487 } 1488 1489 static void __ref setup_usemap(struct zone *zone) 1490 { 1491 unsigned long usemapsize = usemap_size(zone->zone_start_pfn, 1492 zone->spanned_pages); 1493 zone->pageblock_flags = NULL; 1494 if (usemapsize) { 1495 zone->pageblock_flags = 1496 memblock_alloc_node(usemapsize, SMP_CACHE_BYTES, 1497 zone_to_nid(zone)); 1498 if (!zone->pageblock_flags) 1499 panic("Failed to allocate %ld bytes for zone %s pageblock flags on node %d\n", 1500 usemapsize, zone->name, zone_to_nid(zone)); 1501 } 1502 } 1503 #else 1504 static inline void setup_usemap(struct zone *zone) {} 1505 #endif /* CONFIG_SPARSEMEM */ 1506 1507 #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE 1508 1509 /* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */ 1510 void __init set_pageblock_order(void) 1511 { 1512 unsigned int order = PAGE_BLOCK_MAX_ORDER; 1513 1514 /* Check that pageblock_nr_pages has not already been setup */ 1515 if (pageblock_order) 1516 return; 1517 1518 /* Don't let pageblocks exceed the maximum allocation granularity. */ 1519 if (HPAGE_SHIFT > PAGE_SHIFT && HUGETLB_PAGE_ORDER < order) 1520 order = HUGETLB_PAGE_ORDER; 1521 1522 /* 1523 * Assume the largest contiguous order of interest is a huge page. 1524 * This value may be variable depending on boot parameters on powerpc. 1525 */ 1526 pageblock_order = order; 1527 } 1528 #else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */ 1529 1530 /* 1531 * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order() 1532 * is unused as pageblock_order is set at compile-time. See 1533 * include/linux/pageblock-flags.h for the values of pageblock_order based on 1534 * the kernel config 1535 */ 1536 void __init set_pageblock_order(void) 1537 { 1538 } 1539 1540 #endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */ 1541 1542 /* 1543 * Set up the zone data structures 1544 * - init pgdat internals 1545 * - init all zones belonging to this node 1546 * 1547 * NOTE: this function is only called during memory hotplug 1548 */ 1549 #ifdef CONFIG_MEMORY_HOTPLUG 1550 void __ref free_area_init_core_hotplug(struct pglist_data *pgdat) 1551 { 1552 int nid = pgdat->node_id; 1553 enum zone_type z; 1554 int cpu; 1555 1556 pgdat_init_internals(pgdat); 1557 1558 if (pgdat->per_cpu_nodestats == &boot_nodestats) 1559 pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat); 1560 1561 /* 1562 * Reset the nr_zones, order and highest_zoneidx before reuse. 1563 * Note that kswapd will init kswapd_highest_zoneidx properly 1564 * when it starts in the near future. 1565 */ 1566 pgdat->nr_zones = 0; 1567 pgdat->kswapd_order = 0; 1568 pgdat->kswapd_highest_zoneidx = 0; 1569 pgdat->node_start_pfn = 0; 1570 pgdat->node_present_pages = 0; 1571 1572 for_each_online_cpu(cpu) { 1573 struct per_cpu_nodestat *p; 1574 1575 p = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu); 1576 memset(p, 0, sizeof(*p)); 1577 } 1578 1579 /* 1580 * When memory is hot-added, all the memory is in offline state. So 1581 * clear all zones' present_pages and managed_pages because they will 1582 * be updated in online_pages() and offline_pages(). 1583 */ 1584 for (z = 0; z < MAX_NR_ZONES; z++) { 1585 struct zone *zone = pgdat->node_zones + z; 1586 1587 zone->present_pages = 0; 1588 zone_init_internals(zone, z, nid, 0); 1589 } 1590 } 1591 #endif 1592 1593 static void __init free_area_init_core(struct pglist_data *pgdat) 1594 { 1595 enum zone_type j; 1596 int nid = pgdat->node_id; 1597 1598 pgdat_init_internals(pgdat); 1599 pgdat->per_cpu_nodestats = &boot_nodestats; 1600 1601 for (j = 0; j < MAX_NR_ZONES; j++) { 1602 struct zone *zone = pgdat->node_zones + j; 1603 unsigned long size = zone->spanned_pages; 1604 1605 /* 1606 * Initialize zone->managed_pages as 0 , it will be reset 1607 * when memblock allocator frees pages into buddy system. 1608 */ 1609 zone_init_internals(zone, j, nid, zone->present_pages); 1610 1611 if (!size) 1612 continue; 1613 1614 setup_usemap(zone); 1615 init_currently_empty_zone(zone, zone->zone_start_pfn, size); 1616 } 1617 } 1618 1619 void __init *memmap_alloc(phys_addr_t size, phys_addr_t align, 1620 phys_addr_t min_addr, int nid, bool exact_nid) 1621 { 1622 void *ptr; 1623 1624 /* 1625 * Kmemleak will explicitly scan mem_map by traversing all valid 1626 * `struct *page`,so memblock does not need to be added to the scan list. 1627 */ 1628 if (exact_nid) 1629 ptr = memblock_alloc_exact_nid_raw(size, align, min_addr, 1630 MEMBLOCK_ALLOC_NOLEAKTRACE, 1631 nid); 1632 else 1633 ptr = memblock_alloc_try_nid_raw(size, align, min_addr, 1634 MEMBLOCK_ALLOC_NOLEAKTRACE, 1635 nid); 1636 1637 if (ptr && size > 0) 1638 page_init_poison(ptr, size); 1639 1640 return ptr; 1641 } 1642 1643 #ifdef CONFIG_FLATMEM 1644 static void __init alloc_node_mem_map(struct pglist_data *pgdat) 1645 { 1646 unsigned long start, offset, size, end; 1647 struct page *map; 1648 1649 /* Skip empty nodes */ 1650 if (!pgdat->node_spanned_pages) 1651 return; 1652 1653 start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1); 1654 offset = pgdat->node_start_pfn - start; 1655 /* 1656 * The zone's endpoints aren't required to be MAX_PAGE_ORDER 1657 * aligned but the node_mem_map endpoints must be in order 1658 * for the buddy allocator to function correctly. 1659 */ 1660 end = ALIGN(pgdat_end_pfn(pgdat), MAX_ORDER_NR_PAGES); 1661 size = (end - start) * sizeof(struct page); 1662 map = memmap_alloc(size, SMP_CACHE_BYTES, MEMBLOCK_LOW_LIMIT, 1663 pgdat->node_id, false); 1664 if (!map) 1665 panic("Failed to allocate %ld bytes for node %d memory map\n", 1666 size, pgdat->node_id); 1667 pgdat->node_mem_map = map + offset; 1668 memmap_boot_pages_add(DIV_ROUND_UP(size, PAGE_SIZE)); 1669 pr_debug("%s: node %d, pgdat %08lx, node_mem_map %08lx\n", 1670 __func__, pgdat->node_id, (unsigned long)pgdat, 1671 (unsigned long)pgdat->node_mem_map); 1672 1673 /* the global mem_map is just set as node 0's */ 1674 WARN_ON(pgdat != NODE_DATA(0)); 1675 1676 mem_map = pgdat->node_mem_map; 1677 if (page_to_pfn(mem_map) != pgdat->node_start_pfn) 1678 mem_map -= offset; 1679 1680 max_mapnr = end - start; 1681 } 1682 #else 1683 static inline void alloc_node_mem_map(struct pglist_data *pgdat) { } 1684 #endif /* CONFIG_FLATMEM */ 1685 1686 /** 1687 * get_pfn_range_for_nid - Return the start and end page frames for a node 1688 * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned. 1689 * @start_pfn: Passed by reference. On return, it will have the node start_pfn. 1690 * @end_pfn: Passed by reference. On return, it will have the node end_pfn. 1691 * 1692 * It returns the start and end page frame of a node based on information 1693 * provided by memblock_set_node(). If called for a node 1694 * with no available memory, the start and end PFNs will be 0. 1695 */ 1696 void __init get_pfn_range_for_nid(unsigned int nid, 1697 unsigned long *start_pfn, unsigned long *end_pfn) 1698 { 1699 unsigned long this_start_pfn, this_end_pfn; 1700 int i; 1701 1702 *start_pfn = -1UL; 1703 *end_pfn = 0; 1704 1705 for_each_mem_pfn_range(i, nid, &this_start_pfn, &this_end_pfn, NULL) { 1706 *start_pfn = min(*start_pfn, this_start_pfn); 1707 *end_pfn = max(*end_pfn, this_end_pfn); 1708 } 1709 1710 if (*start_pfn == -1UL) 1711 *start_pfn = 0; 1712 } 1713 1714 static void __init free_area_init_node(int nid) 1715 { 1716 pg_data_t *pgdat = NODE_DATA(nid); 1717 unsigned long start_pfn = 0; 1718 unsigned long end_pfn = 0; 1719 1720 /* pg_data_t should be reset to zero when it's allocated */ 1721 WARN_ON(pgdat->nr_zones || pgdat->kswapd_highest_zoneidx); 1722 1723 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn); 1724 1725 pgdat->node_id = nid; 1726 pgdat->node_start_pfn = start_pfn; 1727 pgdat->per_cpu_nodestats = NULL; 1728 1729 if (start_pfn != end_pfn) { 1730 pr_info("Initmem setup node %d [mem %#018Lx-%#018Lx]\n", nid, 1731 (u64)start_pfn << PAGE_SHIFT, 1732 end_pfn ? ((u64)end_pfn << PAGE_SHIFT) - 1 : 0); 1733 1734 calculate_node_totalpages(pgdat, start_pfn, end_pfn); 1735 } else { 1736 pr_info("Initmem setup node %d as memoryless\n", nid); 1737 1738 reset_memoryless_node_totalpages(pgdat); 1739 } 1740 1741 alloc_node_mem_map(pgdat); 1742 pgdat_set_deferred_range(pgdat); 1743 1744 free_area_init_core(pgdat); 1745 lru_gen_init_pgdat(pgdat); 1746 } 1747 1748 /* Any regular or high memory on that node ? */ 1749 static void __init check_for_memory(pg_data_t *pgdat) 1750 { 1751 enum zone_type zone_type; 1752 1753 for (zone_type = 0; zone_type <= ZONE_MOVABLE - 1; zone_type++) { 1754 struct zone *zone = &pgdat->node_zones[zone_type]; 1755 if (populated_zone(zone)) { 1756 if (IS_ENABLED(CONFIG_HIGHMEM)) 1757 node_set_state(pgdat->node_id, N_HIGH_MEMORY); 1758 if (zone_type <= ZONE_NORMAL) 1759 node_set_state(pgdat->node_id, N_NORMAL_MEMORY); 1760 break; 1761 } 1762 } 1763 } 1764 1765 #if MAX_NUMNODES > 1 1766 /* 1767 * Figure out the number of possible node ids. 1768 */ 1769 void __init setup_nr_node_ids(void) 1770 { 1771 unsigned int highest; 1772 1773 highest = find_last_bit(node_possible_map.bits, MAX_NUMNODES); 1774 nr_node_ids = highest + 1; 1775 } 1776 #endif 1777 1778 /* 1779 * Some architectures, e.g. ARC may have ZONE_HIGHMEM below ZONE_NORMAL. For 1780 * such cases we allow max_zone_pfn sorted in the descending order 1781 */ 1782 static bool arch_has_descending_max_zone_pfns(void) 1783 { 1784 return IS_ENABLED(CONFIG_ARC) && !IS_ENABLED(CONFIG_ARC_HAS_PAE40); 1785 } 1786 1787 static void __init set_high_memory(void) 1788 { 1789 phys_addr_t highmem = memblock_end_of_DRAM(); 1790 1791 /* 1792 * Some architectures (e.g. ARM) set high_memory very early and 1793 * use it in arch setup code. 1794 * If an architecture already set high_memory don't overwrite it 1795 */ 1796 if (high_memory) 1797 return; 1798 1799 #ifdef CONFIG_HIGHMEM 1800 if (arch_has_descending_max_zone_pfns() || 1801 highmem > PFN_PHYS(arch_zone_lowest_possible_pfn[ZONE_HIGHMEM])) 1802 highmem = PFN_PHYS(arch_zone_lowest_possible_pfn[ZONE_HIGHMEM]); 1803 #endif 1804 1805 high_memory = phys_to_virt(highmem - 1) + 1; 1806 } 1807 1808 /** 1809 * free_area_init - Initialise all pg_data_t and zone data 1810 * @max_zone_pfn: an array of max PFNs for each zone 1811 * 1812 * This will call free_area_init_node() for each active node in the system. 1813 * Using the page ranges provided by memblock_set_node(), the size of each 1814 * zone in each node and their holes is calculated. If the maximum PFN 1815 * between two adjacent zones match, it is assumed that the zone is empty. 1816 * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed 1817 * that arch_max_dma32_pfn has no pages. It is also assumed that a zone 1818 * starts where the previous one ended. For example, ZONE_DMA32 starts 1819 * at arch_max_dma_pfn. 1820 */ 1821 void __init free_area_init(unsigned long *max_zone_pfn) 1822 { 1823 unsigned long start_pfn, end_pfn; 1824 int i, nid, zone; 1825 bool descending; 1826 1827 /* Record where the zone boundaries are */ 1828 memset(arch_zone_lowest_possible_pfn, 0, 1829 sizeof(arch_zone_lowest_possible_pfn)); 1830 memset(arch_zone_highest_possible_pfn, 0, 1831 sizeof(arch_zone_highest_possible_pfn)); 1832 1833 start_pfn = PHYS_PFN(memblock_start_of_DRAM()); 1834 descending = arch_has_descending_max_zone_pfns(); 1835 1836 for (i = 0; i < MAX_NR_ZONES; i++) { 1837 if (descending) 1838 zone = MAX_NR_ZONES - i - 1; 1839 else 1840 zone = i; 1841 1842 if (zone == ZONE_MOVABLE) 1843 continue; 1844 1845 end_pfn = max(max_zone_pfn[zone], start_pfn); 1846 arch_zone_lowest_possible_pfn[zone] = start_pfn; 1847 arch_zone_highest_possible_pfn[zone] = end_pfn; 1848 1849 start_pfn = end_pfn; 1850 } 1851 1852 /* Find the PFNs that ZONE_MOVABLE begins at in each node */ 1853 memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn)); 1854 find_zone_movable_pfns_for_nodes(); 1855 1856 /* Print out the zone ranges */ 1857 pr_info("Zone ranges:\n"); 1858 for (i = 0; i < MAX_NR_ZONES; i++) { 1859 if (i == ZONE_MOVABLE) 1860 continue; 1861 pr_info(" %-8s ", zone_names[i]); 1862 if (arch_zone_lowest_possible_pfn[i] == 1863 arch_zone_highest_possible_pfn[i]) 1864 pr_cont("empty\n"); 1865 else 1866 pr_cont("[mem %#018Lx-%#018Lx]\n", 1867 (u64)arch_zone_lowest_possible_pfn[i] 1868 << PAGE_SHIFT, 1869 ((u64)arch_zone_highest_possible_pfn[i] 1870 << PAGE_SHIFT) - 1); 1871 } 1872 1873 /* Print out the PFNs ZONE_MOVABLE begins at in each node */ 1874 pr_info("Movable zone start for each node\n"); 1875 for (i = 0; i < MAX_NUMNODES; i++) { 1876 if (zone_movable_pfn[i]) 1877 pr_info(" Node %d: %#018Lx\n", i, 1878 (u64)zone_movable_pfn[i] << PAGE_SHIFT); 1879 } 1880 1881 /* 1882 * Print out the early node map, and initialize the 1883 * subsection-map relative to active online memory ranges to 1884 * enable future "sub-section" extensions of the memory map. 1885 */ 1886 pr_info("Early memory node ranges\n"); 1887 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) { 1888 pr_info(" node %3d: [mem %#018Lx-%#018Lx]\n", nid, 1889 (u64)start_pfn << PAGE_SHIFT, 1890 ((u64)end_pfn << PAGE_SHIFT) - 1); 1891 subsection_map_init(start_pfn, end_pfn - start_pfn); 1892 } 1893 1894 /* Initialise every node */ 1895 mminit_verify_pageflags_layout(); 1896 setup_nr_node_ids(); 1897 set_pageblock_order(); 1898 1899 for_each_node(nid) { 1900 pg_data_t *pgdat; 1901 1902 if (!node_online(nid)) 1903 alloc_offline_node_data(nid); 1904 1905 pgdat = NODE_DATA(nid); 1906 free_area_init_node(nid); 1907 1908 /* 1909 * No sysfs hierarchy will be created via register_node() 1910 *for memory-less node because here it's not marked as N_MEMORY 1911 *and won't be set online later. The benefit is userspace 1912 *program won't be confused by sysfs files/directories of 1913 *memory-less node. The pgdat will get fully initialized by 1914 *hotadd_init_pgdat() when memory is hotplugged into this node. 1915 */ 1916 if (pgdat->node_present_pages) { 1917 node_set_state(nid, N_MEMORY); 1918 check_for_memory(pgdat); 1919 } 1920 } 1921 1922 for_each_node_state(nid, N_MEMORY) 1923 sparse_vmemmap_init_nid_late(nid); 1924 1925 calc_nr_kernel_pages(); 1926 memmap_init(); 1927 1928 /* disable hash distribution for systems with a single node */ 1929 fixup_hashdist(); 1930 1931 set_high_memory(); 1932 } 1933 1934 /** 1935 * node_map_pfn_alignment - determine the maximum internode alignment 1936 * 1937 * This function should be called after node map is populated and sorted. 1938 * It calculates the maximum power of two alignment which can distinguish 1939 * all the nodes. 1940 * 1941 * For example, if all nodes are 1GiB and aligned to 1GiB, the return value 1942 * would indicate 1GiB alignment with (1 << (30 - PAGE_SHIFT)). If the 1943 * nodes are shifted by 256MiB, 256MiB. Note that if only the last node is 1944 * shifted, 1GiB is enough and this function will indicate so. 1945 * 1946 * This is used to test whether pfn -> nid mapping of the chosen memory 1947 * model has fine enough granularity to avoid incorrect mapping for the 1948 * populated node map. 1949 * 1950 * Return: the determined alignment in pfn's. 0 if there is no alignment 1951 * requirement (single node). 1952 */ 1953 unsigned long __init node_map_pfn_alignment(void) 1954 { 1955 unsigned long accl_mask = 0, last_end = 0; 1956 unsigned long start, end, mask; 1957 int last_nid = NUMA_NO_NODE; 1958 int i, nid; 1959 1960 for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, &nid) { 1961 if (!start || last_nid < 0 || last_nid == nid) { 1962 last_nid = nid; 1963 last_end = end; 1964 continue; 1965 } 1966 1967 /* 1968 * Start with a mask granular enough to pin-point to the 1969 * start pfn and tick off bits one-by-one until it becomes 1970 * too coarse to separate the current node from the last. 1971 */ 1972 mask = ~((1 << __ffs(start)) - 1); 1973 while (mask && last_end <= (start & (mask << 1))) 1974 mask <<= 1; 1975 1976 /* accumulate all internode masks */ 1977 accl_mask |= mask; 1978 } 1979 1980 /* convert mask to number of pages */ 1981 return ~accl_mask + 1; 1982 } 1983 1984 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT 1985 static void __init deferred_free_pages(unsigned long pfn, 1986 unsigned long nr_pages) 1987 { 1988 struct page *page; 1989 unsigned long i; 1990 1991 if (!nr_pages) 1992 return; 1993 1994 page = pfn_to_page(pfn); 1995 1996 /* Free a large naturally-aligned chunk if possible */ 1997 if (nr_pages == MAX_ORDER_NR_PAGES && IS_MAX_ORDER_ALIGNED(pfn)) { 1998 for (i = 0; i < nr_pages; i += pageblock_nr_pages) 1999 init_pageblock_migratetype(page + i, MIGRATE_MOVABLE, 2000 false); 2001 __free_pages_core(page, MAX_PAGE_ORDER, MEMINIT_EARLY); 2002 return; 2003 } 2004 2005 /* Accept chunks smaller than MAX_PAGE_ORDER upfront */ 2006 accept_memory(PFN_PHYS(pfn), nr_pages * PAGE_SIZE); 2007 2008 for (i = 0; i < nr_pages; i++, page++, pfn++) { 2009 if (pageblock_aligned(pfn)) 2010 init_pageblock_migratetype(page, MIGRATE_MOVABLE, 2011 false); 2012 __free_pages_core(page, 0, MEMINIT_EARLY); 2013 } 2014 } 2015 2016 /* Completion tracking for deferred_init_memmap() threads */ 2017 static atomic_t pgdat_init_n_undone __initdata; 2018 static __initdata DECLARE_COMPLETION(pgdat_init_all_done_comp); 2019 2020 static inline void __init pgdat_init_report_one_done(void) 2021 { 2022 if (atomic_dec_and_test(&pgdat_init_n_undone)) 2023 complete(&pgdat_init_all_done_comp); 2024 } 2025 2026 /* 2027 * Initialize struct pages. We minimize pfn page lookups and scheduler checks 2028 * by performing it only once every MAX_ORDER_NR_PAGES. 2029 * Return number of pages initialized. 2030 */ 2031 static unsigned long __init deferred_init_pages(struct zone *zone, 2032 unsigned long pfn, unsigned long end_pfn) 2033 { 2034 int nid = zone_to_nid(zone); 2035 unsigned long nr_pages = end_pfn - pfn; 2036 int zid = zone_idx(zone); 2037 struct page *page = pfn_to_page(pfn); 2038 2039 for (; pfn < end_pfn; pfn++, page++) 2040 __init_single_page(page, pfn, zid, nid); 2041 return nr_pages; 2042 } 2043 2044 /* 2045 * Initialize and free pages. 2046 * 2047 * At this point reserved pages and struct pages that correspond to holes in 2048 * memblock.memory are already intialized so every free range has a valid 2049 * memory map around it. 2050 * This ensures that access of pages that are ahead of the range being 2051 * initialized (computing buddy page in __free_one_page()) always reads a valid 2052 * struct page. 2053 * 2054 * In order to try and improve CPU cache locality we have the loop broken along 2055 * max page order boundaries. 2056 */ 2057 static unsigned long __init 2058 deferred_init_memmap_chunk(unsigned long start_pfn, unsigned long end_pfn, 2059 struct zone *zone) 2060 { 2061 int nid = zone_to_nid(zone); 2062 unsigned long nr_pages = 0; 2063 phys_addr_t start, end; 2064 u64 i = 0; 2065 2066 for_each_free_mem_range(i, nid, 0, &start, &end, NULL) { 2067 unsigned long spfn = PFN_UP(start); 2068 unsigned long epfn = PFN_DOWN(end); 2069 2070 if (spfn >= end_pfn) 2071 break; 2072 2073 spfn = max(spfn, start_pfn); 2074 epfn = min(epfn, end_pfn); 2075 2076 while (spfn < epfn) { 2077 unsigned long mo_pfn = ALIGN(spfn + 1, MAX_ORDER_NR_PAGES); 2078 unsigned long chunk_end = min(mo_pfn, epfn); 2079 2080 nr_pages += deferred_init_pages(zone, spfn, chunk_end); 2081 deferred_free_pages(spfn, chunk_end - spfn); 2082 2083 spfn = chunk_end; 2084 2085 if (irqs_disabled()) 2086 touch_nmi_watchdog(); 2087 else 2088 cond_resched(); 2089 } 2090 } 2091 2092 return nr_pages; 2093 } 2094 2095 static void __init 2096 deferred_init_memmap_job(unsigned long start_pfn, unsigned long end_pfn, 2097 void *arg) 2098 { 2099 struct zone *zone = arg; 2100 2101 deferred_init_memmap_chunk(start_pfn, end_pfn, zone); 2102 } 2103 2104 static unsigned int __init 2105 deferred_page_init_max_threads(const struct cpumask *node_cpumask) 2106 { 2107 return max(cpumask_weight(node_cpumask), 1U); 2108 } 2109 2110 /* Initialise remaining memory on a node */ 2111 static int __init deferred_init_memmap(void *data) 2112 { 2113 pg_data_t *pgdat = data; 2114 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 2115 int max_threads = deferred_page_init_max_threads(cpumask); 2116 unsigned long first_init_pfn, last_pfn, flags; 2117 unsigned long start = jiffies; 2118 struct zone *zone; 2119 2120 /* Bind memory initialisation thread to a local node if possible */ 2121 if (!cpumask_empty(cpumask)) 2122 set_cpus_allowed_ptr(current, cpumask); 2123 2124 pgdat_resize_lock(pgdat, &flags); 2125 first_init_pfn = pgdat->first_deferred_pfn; 2126 if (first_init_pfn == ULONG_MAX) { 2127 pgdat_resize_unlock(pgdat, &flags); 2128 pgdat_init_report_one_done(); 2129 return 0; 2130 } 2131 2132 /* Sanity check boundaries */ 2133 BUG_ON(pgdat->first_deferred_pfn < pgdat->node_start_pfn); 2134 BUG_ON(pgdat->first_deferred_pfn > pgdat_end_pfn(pgdat)); 2135 pgdat->first_deferred_pfn = ULONG_MAX; 2136 2137 /* 2138 * Once we unlock here, the zone cannot be grown anymore, thus if an 2139 * interrupt thread must allocate this early in boot, zone must be 2140 * pre-grown prior to start of deferred page initialization. 2141 */ 2142 pgdat_resize_unlock(pgdat, &flags); 2143 2144 /* Only the highest zone is deferred */ 2145 zone = pgdat->node_zones + pgdat->nr_zones - 1; 2146 last_pfn = SECTION_ALIGN_UP(zone_end_pfn(zone)); 2147 2148 struct padata_mt_job job = { 2149 .thread_fn = deferred_init_memmap_job, 2150 .fn_arg = zone, 2151 .start = first_init_pfn, 2152 .size = last_pfn - first_init_pfn, 2153 .align = PAGES_PER_SECTION, 2154 .min_chunk = PAGES_PER_SECTION, 2155 .max_threads = max_threads, 2156 .numa_aware = false, 2157 }; 2158 2159 padata_do_multithreaded(&job); 2160 2161 /* Sanity check that the next zone really is unpopulated */ 2162 WARN_ON(pgdat->nr_zones < MAX_NR_ZONES && populated_zone(++zone)); 2163 2164 pr_info("node %d deferred pages initialised in %ums\n", 2165 pgdat->node_id, jiffies_to_msecs(jiffies - start)); 2166 2167 pgdat_init_report_one_done(); 2168 return 0; 2169 } 2170 2171 /* 2172 * If this zone has deferred pages, try to grow it by initializing enough 2173 * deferred pages to satisfy the allocation specified by order, rounded up to 2174 * the nearest PAGES_PER_SECTION boundary. So we're adding memory in increments 2175 * of SECTION_SIZE bytes by initializing struct pages in increments of 2176 * PAGES_PER_SECTION * sizeof(struct page) bytes. 2177 * 2178 * Return true when zone was grown, otherwise return false. We return true even 2179 * when we grow less than requested, to let the caller decide if there are 2180 * enough pages to satisfy the allocation. 2181 */ 2182 bool __init deferred_grow_zone(struct zone *zone, unsigned int order) 2183 { 2184 unsigned long nr_pages_needed = SECTION_ALIGN_UP(1 << order); 2185 pg_data_t *pgdat = zone->zone_pgdat; 2186 unsigned long first_deferred_pfn = pgdat->first_deferred_pfn; 2187 unsigned long spfn, epfn, flags; 2188 unsigned long nr_pages = 0; 2189 2190 /* Only the last zone may have deferred pages */ 2191 if (zone_end_pfn(zone) != pgdat_end_pfn(pgdat)) 2192 return false; 2193 2194 pgdat_resize_lock(pgdat, &flags); 2195 2196 /* 2197 * If someone grew this zone while we were waiting for spinlock, return 2198 * true, as there might be enough pages already. 2199 */ 2200 if (first_deferred_pfn != pgdat->first_deferred_pfn) { 2201 pgdat_resize_unlock(pgdat, &flags); 2202 return true; 2203 } 2204 2205 /* 2206 * Initialize at least nr_pages_needed in section chunks. 2207 * If a section has less free memory than nr_pages_needed, the next 2208 * section will be also initialized. 2209 * Note, that it still does not guarantee that allocation of order can 2210 * be satisfied if the sections are fragmented because of memblock 2211 * allocations. 2212 */ 2213 for (spfn = first_deferred_pfn, epfn = SECTION_ALIGN_UP(spfn + 1); 2214 nr_pages < nr_pages_needed && spfn < zone_end_pfn(zone); 2215 spfn = epfn, epfn += PAGES_PER_SECTION) { 2216 nr_pages += deferred_init_memmap_chunk(spfn, epfn, zone); 2217 } 2218 2219 /* 2220 * There were no pages to initialize and free which means the zone's 2221 * memory map is completely initialized. 2222 */ 2223 pgdat->first_deferred_pfn = nr_pages ? spfn : ULONG_MAX; 2224 2225 pgdat_resize_unlock(pgdat, &flags); 2226 2227 return nr_pages > 0; 2228 } 2229 2230 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ 2231 2232 #ifdef CONFIG_CMA 2233 void __init init_cma_reserved_pageblock(struct page *page) 2234 { 2235 unsigned i = pageblock_nr_pages; 2236 struct page *p = page; 2237 2238 do { 2239 __ClearPageReserved(p); 2240 set_page_count(p, 0); 2241 } while (++p, --i); 2242 2243 init_pageblock_migratetype(page, MIGRATE_CMA, false); 2244 set_page_refcounted(page); 2245 /* pages were reserved and not allocated */ 2246 clear_page_tag_ref(page); 2247 __free_pages(page, pageblock_order); 2248 2249 adjust_managed_page_count(page, pageblock_nr_pages); 2250 page_zone(page)->cma_pages += pageblock_nr_pages; 2251 } 2252 /* 2253 * Similar to above, but only set the migrate type and stats. 2254 */ 2255 void __init init_cma_pageblock(struct page *page) 2256 { 2257 init_pageblock_migratetype(page, MIGRATE_CMA, false); 2258 adjust_managed_page_count(page, pageblock_nr_pages); 2259 page_zone(page)->cma_pages += pageblock_nr_pages; 2260 } 2261 #endif 2262 2263 void set_zone_contiguous(struct zone *zone) 2264 { 2265 unsigned long block_start_pfn = zone->zone_start_pfn; 2266 unsigned long block_end_pfn; 2267 2268 block_end_pfn = pageblock_end_pfn(block_start_pfn); 2269 for (; block_start_pfn < zone_end_pfn(zone); 2270 block_start_pfn = block_end_pfn, 2271 block_end_pfn += pageblock_nr_pages) { 2272 2273 block_end_pfn = min(block_end_pfn, zone_end_pfn(zone)); 2274 2275 if (!__pageblock_pfn_to_page(block_start_pfn, 2276 block_end_pfn, zone)) 2277 return; 2278 cond_resched(); 2279 } 2280 2281 /* We confirm that there is no hole */ 2282 zone->contiguous = true; 2283 } 2284 2285 /* 2286 * Check if a PFN range intersects multiple zones on one or more 2287 * NUMA nodes. Specify the @nid argument if it is known that this 2288 * PFN range is on one node, NUMA_NO_NODE otherwise. 2289 */ 2290 bool pfn_range_intersects_zones(int nid, unsigned long start_pfn, 2291 unsigned long nr_pages) 2292 { 2293 struct zone *zone, *izone = NULL; 2294 2295 for_each_zone(zone) { 2296 if (nid != NUMA_NO_NODE && zone_to_nid(zone) != nid) 2297 continue; 2298 2299 if (zone_intersects(zone, start_pfn, nr_pages)) { 2300 if (izone != NULL) 2301 return true; 2302 izone = zone; 2303 } 2304 2305 } 2306 2307 return false; 2308 } 2309 2310 static void __init mem_init_print_info(void); 2311 void __init page_alloc_init_late(void) 2312 { 2313 struct zone *zone; 2314 int nid; 2315 2316 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT 2317 2318 /* There will be num_node_state(N_MEMORY) threads */ 2319 atomic_set(&pgdat_init_n_undone, num_node_state(N_MEMORY)); 2320 for_each_node_state(nid, N_MEMORY) { 2321 kthread_run(deferred_init_memmap, NODE_DATA(nid), "pgdatinit%d", nid); 2322 } 2323 2324 /* Block until all are initialised */ 2325 wait_for_completion(&pgdat_init_all_done_comp); 2326 2327 /* 2328 * We initialized the rest of the deferred pages. Permanently disable 2329 * on-demand struct page initialization. 2330 */ 2331 static_branch_disable(&deferred_pages); 2332 2333 /* Reinit limits that are based on free pages after the kernel is up */ 2334 files_maxfiles_init(); 2335 #endif 2336 2337 /* Accounting of total+free memory is stable at this point. */ 2338 mem_init_print_info(); 2339 buffer_init(); 2340 2341 /* Discard memblock private memory */ 2342 memblock_discard(); 2343 2344 for_each_node_state(nid, N_MEMORY) 2345 shuffle_free_memory(NODE_DATA(nid)); 2346 2347 for_each_populated_zone(zone) 2348 set_zone_contiguous(zone); 2349 2350 /* Initialize page ext after all struct pages are initialized. */ 2351 if (deferred_struct_pages) 2352 page_ext_init(); 2353 2354 page_alloc_sysctl_init(); 2355 } 2356 2357 /* 2358 * Adaptive scale is meant to reduce sizes of hash tables on large memory 2359 * machines. As memory size is increased the scale is also increased but at 2360 * slower pace. Starting from ADAPT_SCALE_BASE (64G), every time memory 2361 * quadruples the scale is increased by one, which means the size of hash table 2362 * only doubles, instead of quadrupling as well. 2363 * Because 32-bit systems cannot have large physical memory, where this scaling 2364 * makes sense, it is disabled on such platforms. 2365 */ 2366 #if __BITS_PER_LONG > 32 2367 #define ADAPT_SCALE_BASE (64ul << 30) 2368 #define ADAPT_SCALE_SHIFT 2 2369 #define ADAPT_SCALE_NPAGES (ADAPT_SCALE_BASE >> PAGE_SHIFT) 2370 #endif 2371 2372 /* 2373 * allocate a large system hash table from bootmem 2374 * - it is assumed that the hash table must contain an exact power-of-2 2375 * quantity of entries 2376 * - limit is the number of hash buckets, not the total allocation size 2377 */ 2378 void *__init alloc_large_system_hash(const char *tablename, 2379 unsigned long bucketsize, 2380 unsigned long numentries, 2381 int scale, 2382 int flags, 2383 unsigned int *_hash_shift, 2384 unsigned int *_hash_mask, 2385 unsigned long low_limit, 2386 unsigned long high_limit) 2387 { 2388 unsigned long long max = high_limit; 2389 unsigned long log2qty, size; 2390 void *table; 2391 gfp_t gfp_flags; 2392 bool virt; 2393 bool huge; 2394 2395 /* allow the kernel cmdline to have a say */ 2396 if (!numentries) { 2397 /* round applicable memory size up to nearest megabyte */ 2398 numentries = nr_kernel_pages; 2399 2400 /* It isn't necessary when PAGE_SIZE >= 1MB */ 2401 if (PAGE_SIZE < SZ_1M) 2402 numentries = round_up(numentries, SZ_1M / PAGE_SIZE); 2403 2404 #if __BITS_PER_LONG > 32 2405 if (!high_limit) { 2406 unsigned long adapt; 2407 2408 for (adapt = ADAPT_SCALE_NPAGES; adapt < numentries; 2409 adapt <<= ADAPT_SCALE_SHIFT) 2410 scale++; 2411 } 2412 #endif 2413 2414 /* limit to 1 bucket per 2^scale bytes of low memory */ 2415 if (scale > PAGE_SHIFT) 2416 numentries >>= (scale - PAGE_SHIFT); 2417 else 2418 numentries <<= (PAGE_SHIFT - scale); 2419 2420 if (unlikely((numentries * bucketsize) < PAGE_SIZE)) 2421 numentries = PAGE_SIZE / bucketsize; 2422 } 2423 numentries = roundup_pow_of_two(numentries); 2424 2425 /* limit allocation size to 1/16 total memory by default */ 2426 if (max == 0) { 2427 max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4; 2428 do_div(max, bucketsize); 2429 } 2430 max = min(max, 0x80000000ULL); 2431 2432 if (numentries < low_limit) 2433 numentries = low_limit; 2434 if (numentries > max) 2435 numentries = max; 2436 2437 log2qty = ilog2(numentries); 2438 2439 gfp_flags = (flags & HASH_ZERO) ? GFP_ATOMIC | __GFP_ZERO : GFP_ATOMIC; 2440 do { 2441 virt = false; 2442 size = bucketsize << log2qty; 2443 if (flags & HASH_EARLY) { 2444 if (flags & HASH_ZERO) 2445 table = memblock_alloc(size, SMP_CACHE_BYTES); 2446 else 2447 table = memblock_alloc_raw(size, 2448 SMP_CACHE_BYTES); 2449 } else if (get_order(size) > MAX_PAGE_ORDER || hashdist) { 2450 table = vmalloc_huge(size, gfp_flags); 2451 virt = true; 2452 if (table) 2453 huge = is_vm_area_hugepages(table); 2454 } else { 2455 /* 2456 * If bucketsize is not a power-of-two, we may free 2457 * some pages at the end of hash table which 2458 * alloc_pages_exact() automatically does 2459 */ 2460 table = alloc_pages_exact(size, gfp_flags); 2461 kmemleak_alloc(table, size, 1, gfp_flags); 2462 } 2463 } while (!table && size > PAGE_SIZE && --log2qty); 2464 2465 if (!table) 2466 panic("Failed to allocate %s hash table\n", tablename); 2467 2468 pr_info("%s hash table entries: %ld (order: %d, %lu bytes, %s)\n", 2469 tablename, 1UL << log2qty, get_order(size), size, 2470 virt ? (huge ? "vmalloc hugepage" : "vmalloc") : "linear"); 2471 2472 if (_hash_shift) 2473 *_hash_shift = log2qty; 2474 if (_hash_mask) 2475 *_hash_mask = (1 << log2qty) - 1; 2476 2477 return table; 2478 } 2479 2480 void __init memblock_free_pages(struct page *page, unsigned long pfn, 2481 unsigned int order) 2482 { 2483 if (IS_ENABLED(CONFIG_DEFERRED_STRUCT_PAGE_INIT)) { 2484 int nid = early_pfn_to_nid(pfn); 2485 2486 if (!early_page_initialised(pfn, nid)) 2487 return; 2488 } 2489 2490 if (!kmsan_memblock_free_pages(page, order)) { 2491 /* KMSAN will take care of these pages. */ 2492 return; 2493 } 2494 2495 /* pages were reserved and not allocated */ 2496 clear_page_tag_ref(page); 2497 __free_pages_core(page, order, MEMINIT_EARLY); 2498 } 2499 2500 DEFINE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, init_on_alloc); 2501 EXPORT_SYMBOL(init_on_alloc); 2502 2503 DEFINE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_FREE_DEFAULT_ON, init_on_free); 2504 EXPORT_SYMBOL(init_on_free); 2505 2506 static bool _init_on_alloc_enabled_early __read_mostly 2507 = IS_ENABLED(CONFIG_INIT_ON_ALLOC_DEFAULT_ON); 2508 static int __init early_init_on_alloc(char *buf) 2509 { 2510 2511 return kstrtobool(buf, &_init_on_alloc_enabled_early); 2512 } 2513 early_param("init_on_alloc", early_init_on_alloc); 2514 2515 static bool _init_on_free_enabled_early __read_mostly 2516 = IS_ENABLED(CONFIG_INIT_ON_FREE_DEFAULT_ON); 2517 static int __init early_init_on_free(char *buf) 2518 { 2519 return kstrtobool(buf, &_init_on_free_enabled_early); 2520 } 2521 early_param("init_on_free", early_init_on_free); 2522 2523 DEFINE_STATIC_KEY_MAYBE(CONFIG_DEBUG_VM, check_pages_enabled); 2524 2525 static bool check_pages_enabled_early __initdata; 2526 2527 static int __init early_check_pages(char *buf) 2528 { 2529 return kstrtobool(buf, &check_pages_enabled_early); 2530 } 2531 early_param("check_pages", early_check_pages); 2532 2533 /* 2534 * Enable static keys related to various memory debugging and hardening options. 2535 * Some override others, and depend on early params that are evaluated in the 2536 * order of appearance. So we need to first gather the full picture of what was 2537 * enabled, and then make decisions. 2538 */ 2539 static void __init mem_debugging_and_hardening_init(void) 2540 { 2541 bool page_poisoning_requested = false; 2542 bool want_check_pages = check_pages_enabled_early; 2543 2544 #ifdef CONFIG_PAGE_POISONING 2545 /* 2546 * Page poisoning is debug page alloc for some arches. If 2547 * either of those options are enabled, enable poisoning. 2548 */ 2549 if (page_poisoning_enabled() || 2550 (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) && 2551 debug_pagealloc_enabled())) { 2552 static_branch_enable(&_page_poisoning_enabled); 2553 page_poisoning_requested = true; 2554 want_check_pages = true; 2555 } 2556 #endif 2557 2558 if ((_init_on_alloc_enabled_early || _init_on_free_enabled_early) && 2559 page_poisoning_requested) { 2560 pr_info("mem auto-init: CONFIG_PAGE_POISONING is on, " 2561 "will take precedence over init_on_alloc and init_on_free\n"); 2562 _init_on_alloc_enabled_early = false; 2563 _init_on_free_enabled_early = false; 2564 } 2565 2566 if (_init_on_alloc_enabled_early) { 2567 want_check_pages = true; 2568 static_branch_enable(&init_on_alloc); 2569 } else { 2570 static_branch_disable(&init_on_alloc); 2571 } 2572 2573 if (_init_on_free_enabled_early) { 2574 want_check_pages = true; 2575 static_branch_enable(&init_on_free); 2576 } else { 2577 static_branch_disable(&init_on_free); 2578 } 2579 2580 if (IS_ENABLED(CONFIG_KMSAN) && 2581 (_init_on_alloc_enabled_early || _init_on_free_enabled_early)) 2582 pr_info("mem auto-init: please make sure init_on_alloc and init_on_free are disabled when running KMSAN\n"); 2583 2584 #ifdef CONFIG_DEBUG_PAGEALLOC 2585 if (debug_pagealloc_enabled()) { 2586 want_check_pages = true; 2587 static_branch_enable(&_debug_pagealloc_enabled); 2588 2589 if (debug_guardpage_minorder()) 2590 static_branch_enable(&_debug_guardpage_enabled); 2591 } 2592 #endif 2593 2594 /* 2595 * Any page debugging or hardening option also enables sanity checking 2596 * of struct pages being allocated or freed. With CONFIG_DEBUG_VM it's 2597 * enabled already. 2598 */ 2599 if (!IS_ENABLED(CONFIG_DEBUG_VM) && want_check_pages) 2600 static_branch_enable(&check_pages_enabled); 2601 } 2602 2603 /* Report memory auto-initialization states for this boot. */ 2604 static void __init report_meminit(void) 2605 { 2606 const char *stack; 2607 2608 if (IS_ENABLED(CONFIG_INIT_STACK_ALL_PATTERN)) 2609 stack = "all(pattern)"; 2610 else if (IS_ENABLED(CONFIG_INIT_STACK_ALL_ZERO)) 2611 stack = "all(zero)"; 2612 else 2613 stack = "off"; 2614 2615 pr_info("mem auto-init: stack:%s, heap alloc:%s, heap free:%s\n", 2616 stack, str_on_off(want_init_on_alloc(GFP_KERNEL)), 2617 str_on_off(want_init_on_free())); 2618 if (want_init_on_free()) 2619 pr_info("mem auto-init: clearing system memory may take some time...\n"); 2620 } 2621 2622 static void __init mem_init_print_info(void) 2623 { 2624 unsigned long physpages, codesize, datasize, rosize, bss_size; 2625 unsigned long init_code_size, init_data_size; 2626 2627 physpages = get_num_physpages(); 2628 codesize = _etext - _stext; 2629 datasize = _edata - _sdata; 2630 rosize = __end_rodata - __start_rodata; 2631 bss_size = __bss_stop - __bss_start; 2632 init_data_size = __init_end - __init_begin; 2633 init_code_size = _einittext - _sinittext; 2634 2635 /* 2636 * Detect special cases and adjust section sizes accordingly: 2637 * 1) .init.* may be embedded into .data sections 2638 * 2) .init.text.* may be out of [__init_begin, __init_end], 2639 * please refer to arch/tile/kernel/vmlinux.lds.S. 2640 * 3) .rodata.* may be embedded into .text or .data sections. 2641 */ 2642 #define adj_init_size(start, end, size, pos, adj) \ 2643 do { \ 2644 if (&start[0] <= &pos[0] && &pos[0] < &end[0] && size > adj) \ 2645 size -= adj; \ 2646 } while (0) 2647 2648 adj_init_size(__init_begin, __init_end, init_data_size, 2649 _sinittext, init_code_size); 2650 adj_init_size(_stext, _etext, codesize, _sinittext, init_code_size); 2651 adj_init_size(_sdata, _edata, datasize, __init_begin, init_data_size); 2652 adj_init_size(_stext, _etext, codesize, __start_rodata, rosize); 2653 adj_init_size(_sdata, _edata, datasize, __start_rodata, rosize); 2654 2655 #undef adj_init_size 2656 2657 pr_info("Memory: %luK/%luK available (%luK kernel code, %luK rwdata, %luK rodata, %luK init, %luK bss, %luK reserved, %luK cma-reserved" 2658 #ifdef CONFIG_HIGHMEM 2659 ", %luK highmem" 2660 #endif 2661 ")\n", 2662 K(nr_free_pages()), K(physpages), 2663 codesize / SZ_1K, datasize / SZ_1K, rosize / SZ_1K, 2664 (init_data_size + init_code_size) / SZ_1K, bss_size / SZ_1K, 2665 K(physpages - totalram_pages() - totalcma_pages), 2666 K(totalcma_pages) 2667 #ifdef CONFIG_HIGHMEM 2668 , K(totalhigh_pages()) 2669 #endif 2670 ); 2671 } 2672 2673 void __init __weak arch_mm_preinit(void) 2674 { 2675 } 2676 2677 void __init __weak mem_init(void) 2678 { 2679 } 2680 2681 /* 2682 * Set up kernel memory allocators 2683 */ 2684 void __init mm_core_init(void) 2685 { 2686 arch_mm_preinit(); 2687 hugetlb_bootmem_alloc(); 2688 2689 /* Initializations relying on SMP setup */ 2690 BUILD_BUG_ON(MAX_ZONELISTS > 2); 2691 build_all_zonelists(NULL); 2692 page_alloc_init_cpuhp(); 2693 alloc_tag_sec_init(); 2694 /* 2695 * page_ext requires contiguous pages, 2696 * bigger than MAX_PAGE_ORDER unless SPARSEMEM. 2697 */ 2698 page_ext_init_flatmem(); 2699 mem_debugging_and_hardening_init(); 2700 kfence_alloc_pool_and_metadata(); 2701 report_meminit(); 2702 kmsan_init_shadow(); 2703 stack_depot_early_init(); 2704 2705 /* 2706 * KHO memory setup must happen while memblock is still active, but 2707 * as close as possible to buddy initialization 2708 */ 2709 kho_memory_init(); 2710 2711 memblock_free_all(); 2712 mem_init(); 2713 kmem_cache_init(); 2714 /* 2715 * page_owner must be initialized after buddy is ready, and also after 2716 * slab is ready so that stack_depot_init() works properly 2717 */ 2718 page_ext_init_flatmem_late(); 2719 kmemleak_init(); 2720 ptlock_cache_init(); 2721 pgtable_cache_init(); 2722 debug_objects_mem_init(); 2723 vmalloc_init(); 2724 /* If no deferred init page_ext now, as vmap is fully initialized */ 2725 if (!deferred_struct_pages) 2726 page_ext_init(); 2727 /* Should be run before the first non-init thread is created */ 2728 init_espfix_bsp(); 2729 /* Should be run after espfix64 is set up. */ 2730 pti_init(); 2731 kmsan_init_runtime(); 2732 mm_cache_init(); 2733 execmem_init(); 2734 } 2735