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
= find_memory_core_early(nid
, size
, align
, goal
, limit
);
46 if (addr
== MEMBLOCK_ERROR
)
49 ptr
= phys_to_virt(addr
);
51 memblock_x86_reserve_range(addr
, addr
+ size
, "BOOTMEM");
53 * The min_count is set to 0 so that bootmem allocated blocks
54 * are never reported as leaks.
56 kmemleak_alloc(ptr
, size
, 0, 0);
61 * free_bootmem_late - free bootmem pages directly to page allocator
62 * @addr: starting address of the range
63 * @size: size of the range in bytes
65 * This is only useful when the bootmem allocator has already been torn
66 * down, but we are still initializing the system. Pages are given directly
67 * to the page allocator, no bootmem metadata is updated because it is gone.
69 void __init
free_bootmem_late(unsigned long addr
, unsigned long size
)
71 unsigned long cursor
, end
;
73 kmemleak_free_part(__va(addr
), size
);
75 cursor
= PFN_UP(addr
);
76 end
= PFN_DOWN(addr
+ size
);
78 for (; cursor
< end
; cursor
++) {
79 __free_pages_bootmem(pfn_to_page(cursor
), 0);
84 static void __init
__free_pages_memory(unsigned long start
, unsigned long end
)
86 unsigned long i
, start_aligned
, end_aligned
;
87 int order
= ilog2(BITS_PER_LONG
);
89 start_aligned
= (start
+ (BITS_PER_LONG
- 1)) & ~(BITS_PER_LONG
- 1);
90 end_aligned
= end
& ~(BITS_PER_LONG
- 1);
92 if (end_aligned
<= start_aligned
) {
93 for (i
= start
; i
< end
; i
++)
94 __free_pages_bootmem(pfn_to_page(i
), 0);
99 for (i
= start
; i
< start_aligned
; i
++)
100 __free_pages_bootmem(pfn_to_page(i
), 0);
102 for (i
= start_aligned
; i
< end_aligned
; i
+= BITS_PER_LONG
)
103 __free_pages_bootmem(pfn_to_page(i
), order
);
105 for (i
= end_aligned
; i
< end
; i
++)
106 __free_pages_bootmem(pfn_to_page(i
), 0);
109 unsigned long __init
free_all_memory_core_early(int nodeid
)
113 unsigned long count
= 0;
114 struct range
*range
= NULL
;
117 nr_range
= get_free_all_memory_range(&range
, nodeid
);
119 for (i
= 0; i
< nr_range
; i
++) {
120 start
= range
[i
].start
;
122 count
+= end
- start
;
123 __free_pages_memory(start
, end
);
130 * free_all_bootmem_node - release a node's free pages to the buddy allocator
131 * @pgdat: node to be released
133 * Returns the number of pages actually released.
135 unsigned long __init
free_all_bootmem_node(pg_data_t
*pgdat
)
137 register_page_bootmem_info_node(pgdat
);
139 /* free_all_memory_core_early(MAX_NUMNODES) will be called later */
144 * free_all_bootmem - release free pages to the buddy allocator
146 * Returns the number of pages actually released.
148 unsigned long __init
free_all_bootmem(void)
151 * We need to use MAX_NUMNODES instead of NODE_DATA(0)->node_id
152 * because in some case like Node0 doesn't have RAM installed
153 * low ram will be on Node1
154 * Use MAX_NUMNODES will make sure all ranges in early_node_map[]
155 * will be used instead of only Node0 related
157 return free_all_memory_core_early(MAX_NUMNODES
);
161 * free_bootmem_node - mark a page range as usable
162 * @pgdat: node the range resides on
163 * @physaddr: starting address of the range
164 * @size: size of the range in bytes
166 * Partial pages will be considered reserved and left as they are.
168 * The range must reside completely on the specified node.
170 void __init
free_bootmem_node(pg_data_t
*pgdat
, unsigned long physaddr
,
173 kmemleak_free_part(__va(physaddr
), size
);
174 memblock_x86_free_range(physaddr
, physaddr
+ size
);
178 * free_bootmem - mark a page range as usable
179 * @addr: starting address of the range
180 * @size: size of the range in bytes
182 * Partial pages will be considered reserved and left as they are.
184 * The range must be contiguous but may span node boundaries.
186 void __init
free_bootmem(unsigned long addr
, unsigned long size
)
188 kmemleak_free_part(__va(addr
), size
);
189 memblock_x86_free_range(addr
, addr
+ size
);
192 static void * __init
___alloc_bootmem_nopanic(unsigned long size
,
199 if (WARN_ON_ONCE(slab_is_available()))
200 return kzalloc(size
, GFP_NOWAIT
);
204 ptr
= __alloc_memory_core_early(MAX_NUMNODES
, size
, align
, goal
, limit
);
218 * __alloc_bootmem_nopanic - allocate boot memory without panicking
219 * @size: size of the request in bytes
220 * @align: alignment of the region
221 * @goal: preferred starting address of the region
223 * The goal is dropped if it can not be satisfied and the allocation will
224 * fall back to memory below @goal.
226 * Allocation may happen on any node in the system.
228 * Returns NULL on failure.
230 void * __init
__alloc_bootmem_nopanic(unsigned long size
, unsigned long align
,
233 unsigned long limit
= -1UL;
235 return ___alloc_bootmem_nopanic(size
, align
, goal
, limit
);
238 static void * __init
___alloc_bootmem(unsigned long size
, unsigned long align
,
239 unsigned long goal
, unsigned long limit
)
241 void *mem
= ___alloc_bootmem_nopanic(size
, align
, goal
, limit
);
246 * Whoops, we cannot satisfy the allocation request.
248 printk(KERN_ALERT
"bootmem alloc of %lu bytes failed!\n", size
);
249 panic("Out of memory");
254 * __alloc_bootmem - allocate boot memory
255 * @size: size of the request in bytes
256 * @align: alignment of the region
257 * @goal: preferred starting address of the region
259 * The goal is dropped if it can not be satisfied and the allocation will
260 * fall back to memory below @goal.
262 * Allocation may happen on any node in the system.
264 * The function panics if the request can not be satisfied.
266 void * __init
__alloc_bootmem(unsigned long size
, unsigned long align
,
269 unsigned long limit
= -1UL;
271 return ___alloc_bootmem(size
, align
, goal
, limit
);
275 * __alloc_bootmem_node - allocate boot memory from a specific node
276 * @pgdat: node to allocate from
277 * @size: size of the request in bytes
278 * @align: alignment of the region
279 * @goal: preferred starting address of the region
281 * The goal is dropped if it can not be satisfied and the allocation will
282 * fall back to memory below @goal.
284 * Allocation may fall back to any node in the system if the specified node
285 * can not hold the requested memory.
287 * The function panics if the request can not be satisfied.
289 void * __init
__alloc_bootmem_node(pg_data_t
*pgdat
, unsigned long size
,
290 unsigned long align
, unsigned long goal
)
294 if (WARN_ON_ONCE(slab_is_available()))
295 return kzalloc_node(size
, GFP_NOWAIT
, pgdat
->node_id
);
297 ptr
= __alloc_memory_core_early(pgdat
->node_id
, size
, align
,
302 return __alloc_memory_core_early(MAX_NUMNODES
, size
, align
,
306 void * __init
__alloc_bootmem_node_high(pg_data_t
*pgdat
, unsigned long size
,
307 unsigned long align
, unsigned long goal
)
309 return __alloc_bootmem_node(pgdat
, size
, align
, goal
);
312 #ifdef CONFIG_SPARSEMEM
314 * alloc_bootmem_section - allocate boot memory from a specific section
315 * @size: size of the request in bytes
316 * @section_nr: sparse map section to allocate from
318 * Return NULL on failure.
320 void * __init
alloc_bootmem_section(unsigned long size
,
321 unsigned long section_nr
)
323 unsigned long pfn
, goal
, limit
;
325 pfn
= section_nr_to_pfn(section_nr
);
326 goal
= pfn
<< PAGE_SHIFT
;
327 limit
= section_nr_to_pfn(section_nr
+ 1) << PAGE_SHIFT
;
329 return __alloc_memory_core_early(early_pfn_to_nid(pfn
), size
,
330 SMP_CACHE_BYTES
, goal
, limit
);
334 void * __init
__alloc_bootmem_node_nopanic(pg_data_t
*pgdat
, unsigned long size
,
335 unsigned long align
, unsigned long goal
)
339 if (WARN_ON_ONCE(slab_is_available()))
340 return kzalloc_node(size
, GFP_NOWAIT
, pgdat
->node_id
);
342 ptr
= __alloc_memory_core_early(pgdat
->node_id
, size
, align
,
347 return __alloc_bootmem_nopanic(size
, align
, goal
);
350 #ifndef ARCH_LOW_ADDRESS_LIMIT
351 #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
355 * __alloc_bootmem_low - allocate low boot memory
356 * @size: size of the request in bytes
357 * @align: alignment of the region
358 * @goal: preferred starting address of the region
360 * The goal is dropped if it can not be satisfied and the allocation will
361 * fall back to memory below @goal.
363 * Allocation may happen on any node in the system.
365 * The function panics if the request can not be satisfied.
367 void * __init
__alloc_bootmem_low(unsigned long size
, unsigned long align
,
370 return ___alloc_bootmem(size
, align
, goal
, ARCH_LOW_ADDRESS_LIMIT
);
374 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
375 * @pgdat: node to allocate from
376 * @size: size of the request in bytes
377 * @align: alignment of the region
378 * @goal: preferred starting address of the region
380 * The goal is dropped if it can not be satisfied and the allocation will
381 * fall back to memory below @goal.
383 * Allocation may fall back to any node in the system if the specified node
384 * can not hold the requested memory.
386 * The function panics if the request can not be satisfied.
388 void * __init
__alloc_bootmem_low_node(pg_data_t
*pgdat
, unsigned long size
,
389 unsigned long align
, unsigned long goal
)
393 if (WARN_ON_ONCE(slab_is_available()))
394 return kzalloc_node(size
, GFP_NOWAIT
, pgdat
->node_id
);
396 ptr
= __alloc_memory_core_early(pgdat
->node_id
, size
, align
,
397 goal
, ARCH_LOW_ADDRESS_LIMIT
);
401 return __alloc_memory_core_early(MAX_NUMNODES
, size
, align
,
402 goal
, ARCH_LOW_ADDRESS_LIMIT
);