2 * bootmem - A boot-time physical memory allocator and configurator
4 * Copyright (C) 1999 Ingo Molnar
5 * 1999 Kanoj Sarcar, SGI
8 * Access to this subsystem has to be serialized externally (which is true
9 * for the boot process anyway).
11 #include <linux/init.h>
12 #include <linux/pfn.h>
13 #include <linux/slab.h>
14 #include <linux/bootmem.h>
15 #include <linux/export.h>
16 #include <linux/kmemleak.h>
17 #include <linux/range.h>
18 #include <linux/memblock.h>
22 #include <asm/processor.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;
35 static void * __init __alloc_memory_core_early(int nid, u64 size, u64 align,
40 ulong flags = choose_memblock_flags();
42 if (limit > memblock.current_limit)
43 limit = memblock.current_limit;
46 addr = memblock_find_in_range_node(size, align, goal, limit, nid,
48 if (!addr && (flags & MEMBLOCK_MIRROR)) {
49 flags &= ~MEMBLOCK_MIRROR;
50 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
57 if (memblock_reserve(addr, size))
60 ptr = phys_to_virt(addr);
63 * The min_count is set to 0 so that bootmem allocated blocks
64 * are never reported as leaks.
66 kmemleak_alloc(ptr, size, 0, 0);
71 * free_bootmem_late - free bootmem pages directly to page allocator
72 * @addr: starting address of the range
73 * @size: size of the range in bytes
75 * This is only useful when the bootmem allocator has already been torn
76 * down, but we are still initializing the system. Pages are given directly
77 * to the page allocator, no bootmem metadata is updated because it is gone.
79 void __init free_bootmem_late(unsigned long addr, unsigned long size)
81 unsigned long cursor, end;
83 kmemleak_free_part(__va(addr), size);
85 cursor = PFN_UP(addr);
86 end = PFN_DOWN(addr + size);
88 for (; cursor < end; cursor++) {
89 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
94 static void __init __free_pages_memory(unsigned long start, unsigned long end)
99 order = min(MAX_ORDER - 1UL, __ffs(start));
101 while (start + (1UL << order) > end)
104 __free_pages_bootmem(pfn_to_page(start), start, order);
106 start += (1UL << order);
110 static unsigned long __init __free_memory_core(phys_addr_t start,
113 unsigned long start_pfn = PFN_UP(start);
114 unsigned long end_pfn = min_t(unsigned long,
115 PFN_DOWN(end), max_low_pfn);
117 if (start_pfn > end_pfn)
120 __free_pages_memory(start_pfn, end_pfn);
122 return end_pfn - start_pfn;
125 static unsigned long __init free_low_memory_core_early(void)
127 unsigned long count = 0;
128 phys_addr_t start, end;
131 memblock_clear_hotplug(0, -1);
133 for_each_reserved_mem_region(i, &start, &end)
134 reserve_bootmem_region(start, end);
136 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
138 count += __free_memory_core(start, end);
140 #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
144 /* Free memblock.reserved array if it was allocated */
145 size = get_allocated_memblock_reserved_regions_info(&start);
147 count += __free_memory_core(start, start + size);
149 /* Free memblock.memory array if it was allocated */
150 size = get_allocated_memblock_memory_regions_info(&start);
152 count += __free_memory_core(start, start + size);
159 static int reset_managed_pages_done __initdata;
161 void reset_node_managed_pages(pg_data_t *pgdat)
165 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
166 z->managed_pages = 0;
169 void __init reset_all_zones_managed_pages(void)
171 struct pglist_data *pgdat;
173 if (reset_managed_pages_done)
176 for_each_online_pgdat(pgdat)
177 reset_node_managed_pages(pgdat);
179 reset_managed_pages_done = 1;
183 * free_all_bootmem - release free pages to the buddy allocator
185 * Returns the number of pages actually released.
187 unsigned long __init free_all_bootmem(void)
191 reset_all_zones_managed_pages();
194 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
195 * because in some case like Node0 doesn't have RAM installed
196 * low ram will be on Node1
198 pages = free_low_memory_core_early();
199 totalram_pages += pages;
205 * free_bootmem_node - mark a page range as usable
206 * @pgdat: node the range resides on
207 * @physaddr: starting 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 reside completely on the specified node.
214 void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
217 memblock_free(physaddr, size);
221 * free_bootmem - mark a page range as usable
222 * @addr: starting address of the range
223 * @size: size of the range in bytes
225 * Partial pages will be considered reserved and left as they are.
227 * The range must be contiguous but may span node boundaries.
229 void __init free_bootmem(unsigned long addr, unsigned long size)
231 memblock_free(addr, size);
234 static void * __init ___alloc_bootmem_nopanic(unsigned long size,
241 if (WARN_ON_ONCE(slab_is_available()))
242 return kzalloc(size, GFP_NOWAIT);
246 ptr = __alloc_memory_core_early(NUMA_NO_NODE, size, align, goal, limit);
260 * __alloc_bootmem_nopanic - allocate boot memory without panicking
261 * @size: size of the request in bytes
262 * @align: alignment of the region
263 * @goal: preferred starting address of the region
265 * The goal is dropped if it can not be satisfied and the allocation will
266 * fall back to memory below @goal.
268 * Allocation may happen on any node in the system.
270 * Returns NULL on failure.
272 void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
275 unsigned long limit = -1UL;
277 return ___alloc_bootmem_nopanic(size, align, goal, limit);
280 static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
281 unsigned long goal, unsigned long limit)
283 void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
288 * Whoops, we cannot satisfy the allocation request.
290 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
291 panic("Out of memory");
296 * __alloc_bootmem - allocate boot memory
297 * @size: size of the request in bytes
298 * @align: alignment of the region
299 * @goal: preferred starting address of the region
301 * The goal is dropped if it can not be satisfied and the allocation will
302 * fall back to memory below @goal.
304 * Allocation may happen on any node in the system.
306 * The function panics if the request can not be satisfied.
308 void * __init __alloc_bootmem(unsigned long size, unsigned long align,
311 unsigned long limit = -1UL;
313 return ___alloc_bootmem(size, align, goal, limit);
316 void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
325 ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
330 ptr = __alloc_memory_core_early(NUMA_NO_NODE, size, align,
343 void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
344 unsigned long align, unsigned long goal)
346 if (WARN_ON_ONCE(slab_is_available()))
347 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
349 return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
352 static void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
353 unsigned long align, unsigned long goal,
358 ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, limit);
362 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
363 panic("Out of memory");
368 * __alloc_bootmem_node - allocate boot memory from a specific node
369 * @pgdat: node to allocate from
370 * @size: size of the request in bytes
371 * @align: alignment of the region
372 * @goal: preferred starting address of the region
374 * The goal is dropped if it can not be satisfied and the allocation will
375 * fall back to memory below @goal.
377 * Allocation may fall back to any node in the system if the specified node
378 * can not hold the requested memory.
380 * The function panics if the request can not be satisfied.
382 void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
383 unsigned long align, unsigned long goal)
385 if (WARN_ON_ONCE(slab_is_available()))
386 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
388 return ___alloc_bootmem_node(pgdat, size, align, goal, 0);
391 void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
392 unsigned long align, unsigned long goal)
394 return __alloc_bootmem_node(pgdat, size, align, goal);
397 #ifndef ARCH_LOW_ADDRESS_LIMIT
398 #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
402 * __alloc_bootmem_low - allocate low boot memory
403 * @size: size of the request in bytes
404 * @align: alignment of the region
405 * @goal: preferred starting address of the region
407 * The goal is dropped if it can not be satisfied and the allocation will
408 * fall back to memory below @goal.
410 * Allocation may happen on any node in the system.
412 * The function panics if the request can not be satisfied.
414 void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
417 return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
420 void * __init __alloc_bootmem_low_nopanic(unsigned long size,
424 return ___alloc_bootmem_nopanic(size, align, goal,
425 ARCH_LOW_ADDRESS_LIMIT);
429 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
430 * @pgdat: node to allocate from
431 * @size: size of the request in bytes
432 * @align: alignment of the region
433 * @goal: preferred starting address of the region
435 * The goal is dropped if it can not be satisfied and the allocation will
436 * fall back to memory below @goal.
438 * Allocation may fall back to any node in the system if the specified node
439 * can not hold the requested memory.
441 * The function panics if the request can not be satisfied.
443 void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
444 unsigned long align, unsigned long goal)
446 if (WARN_ON_ONCE(slab_is_available()))
447 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
449 return ___alloc_bootmem_node(pgdat, size, align, goal,
450 ARCH_LOW_ADDRESS_LIMIT);