efivars: Allow disabling use as a pstore backend
[linux/fpc-iii.git] / mm / nobootmem.c
blob07c08c4db1d2e718bcf737c3f29717258ddc9765
1 /*
2 * bootmem - A boot-time physical memory allocator and configurator
4 * Copyright (C) 1999 Ingo Molnar
5 * 1999 Kanoj Sarcar, SGI
6 * 2008 Johannes Weiner
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>
20 #include <asm/bug.h>
21 #include <asm/io.h>
22 #include <asm/processor.h>
24 #include "internal.h"
26 #ifndef CONFIG_NEED_MULTIPLE_NODES
27 struct pglist_data __refdata contig_page_data;
28 EXPORT_SYMBOL(contig_page_data);
29 #endif
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,
36 u64 goal, u64 limit)
38 void *ptr;
39 u64 addr;
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)
47 return NULL;
49 ptr = phys_to_virt(addr);
50 memset(ptr, 0, size);
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);
57 return ptr;
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);
80 totalram_pages++;
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);
96 return;
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)
111 int i;
112 u64 start, end;
113 unsigned long count = 0;
114 struct range *range = NULL;
115 int nr_range;
117 nr_range = get_free_all_memory_range(&range, nodeid);
119 for (i = 0; i < nr_range; i++) {
120 start = range[i].start;
121 end = range[i].end;
122 count += end - start;
123 __free_pages_memory(start, end);
126 return count;
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 */
140 return 0;
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,
171 unsigned long size)
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,
193 unsigned long align,
194 unsigned long goal,
195 unsigned long limit)
197 void *ptr;
199 if (WARN_ON_ONCE(slab_is_available()))
200 return kzalloc(size, GFP_NOWAIT);
202 restart:
204 ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, goal, limit);
206 if (ptr)
207 return ptr;
209 if (goal != 0) {
210 goal = 0;
211 goto restart;
214 return NULL;
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,
231 unsigned long goal)
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);
243 if (mem)
244 return mem;
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");
250 return NULL;
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,
267 unsigned long goal)
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)
292 void *ptr;
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,
298 goal, -1ULL);
299 if (ptr)
300 return ptr;
302 return __alloc_memory_core_early(MAX_NUMNODES, size, align,
303 goal, -1ULL);
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);
332 #endif
334 void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
335 unsigned long align, unsigned long goal)
337 void *ptr;
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,
343 goal, -1ULL);
344 if (ptr)
345 return ptr;
347 return __alloc_bootmem_nopanic(size, align, goal);
350 #ifndef ARCH_LOW_ADDRESS_LIMIT
351 #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
352 #endif
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,
368 unsigned long goal)
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)
391 void *ptr;
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);
398 if (ptr)
399 return ptr;
401 return __alloc_memory_core_early(MAX_NUMNODES, size, align,
402 goal, ARCH_LOW_ADDRESS_LIMIT);