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 __alloc_memory_core_early(int nid, u64 size, u64 align,
41 enum memblock_flags flags = choose_memblock_flags();
43 if (limit > memblock.current_limit)
44 limit = memblock.current_limit;
47 addr = memblock_find_in_range_node(size, align, goal, limit, nid,
49 if (!addr && (flags & MEMBLOCK_MIRROR)) {
50 flags &= ~MEMBLOCK_MIRROR;
51 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
58 if (memblock_reserve(addr, size))
61 ptr = phys_to_virt(addr);
64 * The min_count is set to 0 so that bootmem allocated blocks
65 * are never reported as leaks.
67 kmemleak_alloc(ptr, size, 0, 0);
72 * free_bootmem_late - free bootmem pages directly to page allocator
73 * @addr: starting address of the range
74 * @size: size of the range in bytes
76 * This is only useful when the bootmem allocator has already been torn
77 * down, but we are still initializing the system. Pages are given directly
78 * to the page allocator, no bootmem metadata is updated because it is gone.
80 void __init free_bootmem_late(unsigned long addr, unsigned long size)
82 unsigned long cursor, end;
84 kmemleak_free_part_phys(addr, size);
86 cursor = PFN_UP(addr);
87 end = PFN_DOWN(addr + size);
89 for (; cursor < end; cursor++) {
90 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
95 static void __init __free_pages_memory(unsigned long start, unsigned long end)
100 order = min(MAX_ORDER - 1UL, __ffs(start));
102 while (start + (1UL << order) > end)
105 __free_pages_bootmem(pfn_to_page(start), start, order);
107 start += (1UL << order);
111 static unsigned long __init __free_memory_core(phys_addr_t start,
114 unsigned long start_pfn = PFN_UP(start);
115 unsigned long end_pfn = min_t(unsigned long,
116 PFN_DOWN(end), max_low_pfn);
118 if (start_pfn >= end_pfn)
121 __free_pages_memory(start_pfn, end_pfn);
123 return end_pfn - start_pfn;
126 static unsigned long __init free_low_memory_core_early(void)
128 unsigned long count = 0;
129 phys_addr_t start, end;
132 memblock_clear_hotplug(0, -1);
134 for_each_reserved_mem_region(i, &start, &end)
135 reserve_bootmem_region(start, end);
138 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
139 * because in some case like Node0 doesn't have RAM installed
140 * low ram will be on Node1
142 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
144 count += __free_memory_core(start, end);
149 static int reset_managed_pages_done __initdata;
151 void reset_node_managed_pages(pg_data_t *pgdat)
155 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
156 z->managed_pages = 0;
159 void __init reset_all_zones_managed_pages(void)
161 struct pglist_data *pgdat;
163 if (reset_managed_pages_done)
166 for_each_online_pgdat(pgdat)
167 reset_node_managed_pages(pgdat);
169 reset_managed_pages_done = 1;
173 * free_all_bootmem - release free pages to the buddy allocator
175 * Return: the number of pages actually released.
177 unsigned long __init free_all_bootmem(void)
181 reset_all_zones_managed_pages();
183 pages = free_low_memory_core_early();
184 totalram_pages += pages;
190 * free_bootmem_node - mark a page range as usable
191 * @pgdat: node the range resides on
192 * @physaddr: starting physical address of the range
193 * @size: size of the range in bytes
195 * Partial pages will be considered reserved and left as they are.
197 * The range must reside completely on the specified node.
199 void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
202 memblock_free(physaddr, size);
206 * free_bootmem - mark a page range as usable
207 * @addr: starting physical address of the range
208 * @size: size of the range in bytes
210 * Partial pages will be considered reserved and left as they are.
212 * The range must be contiguous but may span node boundaries.
214 void __init free_bootmem(unsigned long addr, unsigned long size)
216 memblock_free(addr, size);
219 static void * __init ___alloc_bootmem_nopanic(unsigned long size,
226 if (WARN_ON_ONCE(slab_is_available()))
227 return kzalloc(size, GFP_NOWAIT);
231 ptr = __alloc_memory_core_early(NUMA_NO_NODE, size, align, goal, limit);
245 * __alloc_bootmem_nopanic - allocate boot memory without panicking
246 * @size: size of the request in bytes
247 * @align: alignment of the region
248 * @goal: preferred starting address of the region
250 * The goal is dropped if it can not be satisfied and the allocation will
251 * fall back to memory below @goal.
253 * Allocation may happen on any node in the system.
255 * Return: address of the allocated region or %NULL on failure.
257 void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
260 unsigned long limit = -1UL;
262 return ___alloc_bootmem_nopanic(size, align, goal, limit);
265 static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
266 unsigned long goal, unsigned long limit)
268 void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
273 * Whoops, we cannot satisfy the allocation request.
275 pr_alert("bootmem alloc of %lu bytes failed!\n", size);
276 panic("Out of memory");
281 * __alloc_bootmem - allocate boot memory
282 * @size: size of the request in bytes
283 * @align: alignment of the region
284 * @goal: preferred starting address of the region
286 * The goal is dropped if it can not be satisfied and the allocation will
287 * fall back to memory below @goal.
289 * Allocation may happen on any node in the system.
291 * The function panics if the request can not be satisfied.
293 * Return: address of the allocated region.
295 void * __init __alloc_bootmem(unsigned long size, unsigned long align,
298 unsigned long limit = -1UL;
300 return ___alloc_bootmem(size, align, goal, limit);
303 void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
312 ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
317 ptr = __alloc_memory_core_early(NUMA_NO_NODE, size, align,
330 void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
331 unsigned long align, unsigned long goal)
333 if (WARN_ON_ONCE(slab_is_available()))
334 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
336 return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
339 static void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
340 unsigned long align, unsigned long goal,
345 ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, limit);
349 pr_alert("bootmem alloc of %lu bytes failed!\n", size);
350 panic("Out of memory");
355 * __alloc_bootmem_node - allocate boot memory from a specific node
356 * @pgdat: node to allocate from
357 * @size: size of the request in bytes
358 * @align: alignment of the region
359 * @goal: preferred starting address of the region
361 * The goal is dropped if it can not be satisfied and the allocation will
362 * fall back to memory below @goal.
364 * Allocation may fall back to any node in the system if the specified node
365 * can not hold the requested memory.
367 * The function panics if the request can not be satisfied.
369 * Return: address of the allocated region.
371 void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
372 unsigned long align, unsigned long goal)
374 if (WARN_ON_ONCE(slab_is_available()))
375 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
377 return ___alloc_bootmem_node(pgdat, size, align, goal, 0);
380 void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
381 unsigned long align, unsigned long goal)
383 return __alloc_bootmem_node(pgdat, size, align, goal);
388 * __alloc_bootmem_low - allocate low boot memory
389 * @size: size of the request in bytes
390 * @align: alignment of the region
391 * @goal: preferred starting address of the region
393 * The goal is dropped if it can not be satisfied and the allocation will
394 * fall back to memory below @goal.
396 * Allocation may happen on any node in the system.
398 * The function panics if the request can not be satisfied.
400 * Return: address of the allocated region.
402 void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
405 return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);