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