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;
37 * free_bootmem_late - free bootmem pages directly to page allocator
38 * @addr: starting address of the range
39 * @size: size of the range in bytes
41 * This is only useful when the bootmem allocator has already been torn
42 * down, but we are still initializing the system. Pages are given directly
43 * to the page allocator, no bootmem metadata is updated because it is gone.
45 void __init free_bootmem_late(unsigned long addr, unsigned long size)
47 unsigned long cursor, end;
49 kmemleak_free_part_phys(addr, size);
51 cursor = PFN_UP(addr);
52 end = PFN_DOWN(addr + size);
54 for (; cursor < end; cursor++) {
55 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
60 static void __init __free_pages_memory(unsigned long start, unsigned long end)
65 order = min(MAX_ORDER - 1UL, __ffs(start));
67 while (start + (1UL << order) > end)
70 __free_pages_bootmem(pfn_to_page(start), start, order);
72 start += (1UL << order);
76 static unsigned long __init __free_memory_core(phys_addr_t start,
79 unsigned long start_pfn = PFN_UP(start);
80 unsigned long end_pfn = min_t(unsigned long,
81 PFN_DOWN(end), max_low_pfn);
83 if (start_pfn >= end_pfn)
86 __free_pages_memory(start_pfn, end_pfn);
88 return end_pfn - start_pfn;
91 static unsigned long __init free_low_memory_core_early(void)
93 unsigned long count = 0;
94 phys_addr_t start, end;
97 memblock_clear_hotplug(0, -1);
99 for_each_reserved_mem_region(i, &start, &end)
100 reserve_bootmem_region(start, end);
103 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
104 * because in some case like Node0 doesn't have RAM installed
105 * low ram will be on Node1
107 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
109 count += __free_memory_core(start, end);
114 static int reset_managed_pages_done __initdata;
116 void reset_node_managed_pages(pg_data_t *pgdat)
120 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
121 z->managed_pages = 0;
124 void __init reset_all_zones_managed_pages(void)
126 struct pglist_data *pgdat;
128 if (reset_managed_pages_done)
131 for_each_online_pgdat(pgdat)
132 reset_node_managed_pages(pgdat);
134 reset_managed_pages_done = 1;
138 * free_all_bootmem - release free pages to the buddy allocator
140 * Return: the number of pages actually released.
142 unsigned long __init free_all_bootmem(void)
146 reset_all_zones_managed_pages();
148 pages = free_low_memory_core_early();
149 totalram_pages += pages;
155 * free_bootmem_node - mark a page range as usable
156 * @pgdat: node the range resides on
157 * @physaddr: starting physical address of the range
158 * @size: size of the range in bytes
160 * Partial pages will be considered reserved and left as they are.
162 * The range must reside completely on the specified node.
164 void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
167 memblock_free(physaddr, size);
171 * free_bootmem - mark a page range as usable
172 * @addr: starting physical address of the range
173 * @size: size of the range in bytes
175 * Partial pages will be considered reserved and left as they are.
177 * The range must be contiguous but may span node boundaries.
179 void __init free_bootmem(unsigned long addr, unsigned long size)
181 memblock_free(addr, size);