1 // SPDX-License-Identifier: GPL-2.0
3 * bootmem - A boot-time physical memory allocator and configurator
5 * Copyright (C) 1999 Ingo Molnar
6 * 1999 Kanoj Sarcar, SGI
9 * Access to this subsystem has to be serialized externally (which is true
10 * for the boot process anyway).
12 #include <linux/init.h>
13 #include <linux/pfn.h>
14 #include <linux/slab.h>
15 #include <linux/export.h>
16 #include <linux/kmemleak.h>
17 #include <linux/range.h>
18 #include <linux/memblock.h>
19 #include <linux/bootmem.h>
26 #ifndef CONFIG_NEED_MULTIPLE_NODES
27 struct pglist_data __refdata contig_page_data;
28 EXPORT_SYMBOL(contig_page_data);
31 unsigned long max_low_pfn;
32 unsigned long min_low_pfn;
33 unsigned long max_pfn;
34 unsigned long long max_possible_pfn;
36 static void __init __free_pages_memory(unsigned long start, unsigned long end)
41 order = min(MAX_ORDER - 1UL, __ffs(start));
43 while (start + (1UL << order) > end)
46 __free_pages_bootmem(pfn_to_page(start), start, order);
48 start += (1UL << order);
52 static unsigned long __init __free_memory_core(phys_addr_t start,
55 unsigned long start_pfn = PFN_UP(start);
56 unsigned long end_pfn = min_t(unsigned long,
57 PFN_DOWN(end), max_low_pfn);
59 if (start_pfn >= end_pfn)
62 __free_pages_memory(start_pfn, end_pfn);
64 return end_pfn - start_pfn;
67 static unsigned long __init free_low_memory_core_early(void)
69 unsigned long count = 0;
70 phys_addr_t start, end;
73 memblock_clear_hotplug(0, -1);
75 for_each_reserved_mem_region(i, &start, &end)
76 reserve_bootmem_region(start, end);
79 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
80 * because in some case like Node0 doesn't have RAM installed
81 * low ram will be on Node1
83 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
85 count += __free_memory_core(start, end);
90 static int reset_managed_pages_done __initdata;
92 void reset_node_managed_pages(pg_data_t *pgdat)
96 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
100 void __init reset_all_zones_managed_pages(void)
102 struct pglist_data *pgdat;
104 if (reset_managed_pages_done)
107 for_each_online_pgdat(pgdat)
108 reset_node_managed_pages(pgdat);
110 reset_managed_pages_done = 1;
114 * free_all_bootmem - release free pages to the buddy allocator
116 * Return: the number of pages actually released.
118 unsigned long __init free_all_bootmem(void)
122 reset_all_zones_managed_pages();
124 pages = free_low_memory_core_early();
125 totalram_pages += pages;