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
,
41 if (limit
> memblock
.current_limit
)
42 limit
= memblock
.current_limit
;
44 addr
= memblock_find_in_range_node(goal
, limit
, size
, align
, nid
);
48 memblock_reserve(addr
, size
);
49 ptr
= phys_to_virt(addr
);
52 * The min_count is set to 0 so that bootmem allocated blocks
53 * are never reported as leaks.
55 kmemleak_alloc(ptr
, size
, 0, 0);
60 * free_bootmem_late - free bootmem pages directly to page allocator
61 * @addr: starting address of the range
62 * @size: size of the range in bytes
64 * This is only useful when the bootmem allocator has already been torn
65 * down, but we are still initializing the system. Pages are given directly
66 * to the page allocator, no bootmem metadata is updated because it is gone.
68 void __init
free_bootmem_late(unsigned long addr
, unsigned long size
)
70 unsigned long cursor
, end
;
72 kmemleak_free_part(__va(addr
), size
);
74 cursor
= PFN_UP(addr
);
75 end
= PFN_DOWN(addr
+ size
);
77 for (; cursor
< end
; cursor
++) {
78 __free_pages_bootmem(pfn_to_page(cursor
), 0);
83 static void __init
__free_pages_memory(unsigned long start
, unsigned long end
)
88 order
= min(MAX_ORDER
- 1UL, __ffs(start
));
90 while (start
+ (1UL << order
) > end
)
93 __free_pages_bootmem(pfn_to_page(start
), order
);
95 start
+= (1UL << order
);
99 static unsigned long __init
__free_memory_core(phys_addr_t start
,
102 unsigned long start_pfn
= PFN_UP(start
);
103 unsigned long end_pfn
= min_t(unsigned long,
104 PFN_DOWN(end
), max_low_pfn
);
106 if (start_pfn
> end_pfn
)
109 __free_pages_memory(start_pfn
, end_pfn
);
111 return end_pfn
- start_pfn
;
114 static unsigned long __init
free_low_memory_core_early(void)
116 unsigned long count
= 0;
117 phys_addr_t start
, end
, size
;
120 for_each_free_mem_range(i
, MAX_NUMNODES
, &start
, &end
, NULL
)
121 count
+= __free_memory_core(start
, end
);
123 /* free range that is used for reserved array if we allocate it */
124 size
= get_allocated_memblock_reserved_regions_info(&start
);
126 count
+= __free_memory_core(start
, start
+ size
);
131 static int reset_managed_pages_done __initdata
;
133 static inline void __init
reset_node_managed_pages(pg_data_t
*pgdat
)
137 if (reset_managed_pages_done
)
139 for (z
= pgdat
->node_zones
; z
< pgdat
->node_zones
+ MAX_NR_ZONES
; z
++)
140 z
->managed_pages
= 0;
143 void __init
reset_all_zones_managed_pages(void)
145 struct pglist_data
*pgdat
;
147 for_each_online_pgdat(pgdat
)
148 reset_node_managed_pages(pgdat
);
149 reset_managed_pages_done
= 1;
153 * free_all_bootmem - release free pages to the buddy allocator
155 * Returns the number of pages actually released.
157 unsigned long __init
free_all_bootmem(void)
161 reset_all_zones_managed_pages();
164 * We need to use MAX_NUMNODES instead of NODE_DATA(0)->node_id
165 * because in some case like Node0 doesn't have RAM installed
166 * low ram will be on Node1
168 pages
= free_low_memory_core_early();
169 totalram_pages
+= pages
;
175 * free_bootmem_node - mark a page range as usable
176 * @pgdat: node the range resides on
177 * @physaddr: starting address of the range
178 * @size: size of the range in bytes
180 * Partial pages will be considered reserved and left as they are.
182 * The range must reside completely on the specified node.
184 void __init
free_bootmem_node(pg_data_t
*pgdat
, unsigned long physaddr
,
187 kmemleak_free_part(__va(physaddr
), size
);
188 memblock_free(physaddr
, size
);
192 * free_bootmem - mark a page range as usable
193 * @addr: starting address of the range
194 * @size: size of the range in bytes
196 * Partial pages will be considered reserved and left as they are.
198 * The range must be contiguous but may span node boundaries.
200 void __init
free_bootmem(unsigned long addr
, unsigned long size
)
202 kmemleak_free_part(__va(addr
), size
);
203 memblock_free(addr
, size
);
206 static void * __init
___alloc_bootmem_nopanic(unsigned long size
,
213 if (WARN_ON_ONCE(slab_is_available()))
214 return kzalloc(size
, GFP_NOWAIT
);
218 ptr
= __alloc_memory_core_early(MAX_NUMNODES
, size
, align
, goal
, limit
);
232 * __alloc_bootmem_nopanic - allocate boot memory without panicking
233 * @size: size of the request in bytes
234 * @align: alignment of the region
235 * @goal: preferred starting address of the region
237 * The goal is dropped if it can not be satisfied and the allocation will
238 * fall back to memory below @goal.
240 * Allocation may happen on any node in the system.
242 * Returns NULL on failure.
244 void * __init
__alloc_bootmem_nopanic(unsigned long size
, unsigned long align
,
247 unsigned long limit
= -1UL;
249 return ___alloc_bootmem_nopanic(size
, align
, goal
, limit
);
252 static void * __init
___alloc_bootmem(unsigned long size
, unsigned long align
,
253 unsigned long goal
, unsigned long limit
)
255 void *mem
= ___alloc_bootmem_nopanic(size
, align
, goal
, limit
);
260 * Whoops, we cannot satisfy the allocation request.
262 printk(KERN_ALERT
"bootmem alloc of %lu bytes failed!\n", size
);
263 panic("Out of memory");
268 * __alloc_bootmem - allocate boot memory
269 * @size: size of the request in bytes
270 * @align: alignment of the region
271 * @goal: preferred starting address of the region
273 * The goal is dropped if it can not be satisfied and the allocation will
274 * fall back to memory below @goal.
276 * Allocation may happen on any node in the system.
278 * The function panics if the request can not be satisfied.
280 void * __init
__alloc_bootmem(unsigned long size
, unsigned long align
,
283 unsigned long limit
= -1UL;
285 return ___alloc_bootmem(size
, align
, goal
, limit
);
288 void * __init
___alloc_bootmem_node_nopanic(pg_data_t
*pgdat
,
297 ptr
= __alloc_memory_core_early(pgdat
->node_id
, size
, align
,
302 ptr
= __alloc_memory_core_early(MAX_NUMNODES
, size
, align
,
315 void * __init
__alloc_bootmem_node_nopanic(pg_data_t
*pgdat
, unsigned long size
,
316 unsigned long align
, unsigned long goal
)
318 if (WARN_ON_ONCE(slab_is_available()))
319 return kzalloc_node(size
, GFP_NOWAIT
, pgdat
->node_id
);
321 return ___alloc_bootmem_node_nopanic(pgdat
, size
, align
, goal
, 0);
324 void * __init
___alloc_bootmem_node(pg_data_t
*pgdat
, unsigned long size
,
325 unsigned long align
, unsigned long goal
,
330 ptr
= ___alloc_bootmem_node_nopanic(pgdat
, size
, align
, goal
, limit
);
334 printk(KERN_ALERT
"bootmem alloc of %lu bytes failed!\n", size
);
335 panic("Out of memory");
340 * __alloc_bootmem_node - allocate boot memory from a specific node
341 * @pgdat: node to allocate from
342 * @size: size of the request in bytes
343 * @align: alignment of the region
344 * @goal: preferred starting address of the region
346 * The goal is dropped if it can not be satisfied and the allocation will
347 * fall back to memory below @goal.
349 * Allocation may fall back to any node in the system if the specified node
350 * can not hold the requested memory.
352 * The function panics if the request can not be satisfied.
354 void * __init
__alloc_bootmem_node(pg_data_t
*pgdat
, unsigned long size
,
355 unsigned long align
, unsigned long goal
)
357 if (WARN_ON_ONCE(slab_is_available()))
358 return kzalloc_node(size
, GFP_NOWAIT
, pgdat
->node_id
);
360 return ___alloc_bootmem_node(pgdat
, size
, align
, goal
, 0);
363 void * __init
__alloc_bootmem_node_high(pg_data_t
*pgdat
, unsigned long size
,
364 unsigned long align
, unsigned long goal
)
366 return __alloc_bootmem_node(pgdat
, size
, align
, goal
);
369 #ifndef ARCH_LOW_ADDRESS_LIMIT
370 #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
374 * __alloc_bootmem_low - allocate low boot memory
375 * @size: size of the request in bytes
376 * @align: alignment of the region
377 * @goal: preferred starting address of the region
379 * The goal is dropped if it can not be satisfied and the allocation will
380 * fall back to memory below @goal.
382 * Allocation may happen on any node in the system.
384 * The function panics if the request can not be satisfied.
386 void * __init
__alloc_bootmem_low(unsigned long size
, unsigned long align
,
389 return ___alloc_bootmem(size
, align
, goal
, ARCH_LOW_ADDRESS_LIMIT
);
392 void * __init
__alloc_bootmem_low_nopanic(unsigned long size
,
396 return ___alloc_bootmem_nopanic(size
, align
, goal
,
397 ARCH_LOW_ADDRESS_LIMIT
);
401 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
402 * @pgdat: node to allocate from
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 fall back to any node in the system if the specified node
411 * can not hold the requested memory.
413 * The function panics if the request can not be satisfied.
415 void * __init
__alloc_bootmem_low_node(pg_data_t
*pgdat
, unsigned long size
,
416 unsigned long align
, unsigned long goal
)
418 if (WARN_ON_ONCE(slab_is_available()))
419 return kzalloc_node(size
, GFP_NOWAIT
, pgdat
->node_id
);
421 return ___alloc_bootmem_node(pgdat
, size
, align
, goal
,
422 ARCH_LOW_ADDRESS_LIMIT
);