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