blob: 2037a8a047614043d7fa1221b30aa5fb49c0d58a [file] [log] [blame]
Yinghai Lu09325872011-02-24 14:43:05 +01001/*
2 * bootmem - A boot-time physical memory allocator and configurator
3 *
4 * Copyright (C) 1999 Ingo Molnar
5 * 1999 Kanoj Sarcar, SGI
6 * 2008 Johannes Weiner
7 *
8 * Access to this subsystem has to be serialized externally (which is true
9 * for the boot process anyway).
10 */
11#include <linux/init.h>
12#include <linux/pfn.h>
13#include <linux/slab.h>
14#include <linux/bootmem.h>
15#include <linux/module.h>
16#include <linux/kmemleak.h>
17#include <linux/range.h>
18#include <linux/memblock.h>
19
20#include <asm/bug.h>
21#include <asm/io.h>
22#include <asm/processor.h>
23
24#include "internal.h"
25
Yinghai Lue782ab42011-02-24 14:43:06 +010026#ifndef CONFIG_NEED_MULTIPLE_NODES
27struct pglist_data __refdata contig_page_data;
28EXPORT_SYMBOL(contig_page_data);
29#endif
30
Yinghai Lu09325872011-02-24 14:43:05 +010031unsigned long max_low_pfn;
32unsigned long min_low_pfn;
33unsigned long max_pfn;
34
Yinghai Lu8bc1f912011-02-24 14:43:06 +010035static void * __init __alloc_memory_core_early(int nid, u64 size, u64 align,
36 u64 goal, u64 limit)
37{
38 void *ptr;
39 u64 addr;
40
41 if (limit > memblock.current_limit)
42 limit = memblock.current_limit;
43
Tejun Heoeb40c4c2011-07-12 10:46:35 +020044 addr = memblock_find_in_range_node(goal, limit, size, align, nid);
Tejun Heo1f5026a2011-07-12 09:58:09 +020045 if (!addr)
Yinghai Lu8bc1f912011-02-24 14:43:06 +010046 return NULL;
47
48 ptr = phys_to_virt(addr);
49 memset(ptr, 0, size);
50 memblock_x86_reserve_range(addr, addr + size, "BOOTMEM");
51 /*
52 * The min_count is set to 0 so that bootmem allocated blocks
53 * are never reported as leaks.
54 */
55 kmemleak_alloc(ptr, size, 0, 0);
56 return ptr;
57}
58
Yinghai Lu09325872011-02-24 14:43:05 +010059/*
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
63 *
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.
67 */
68void __init free_bootmem_late(unsigned long addr, unsigned long size)
69{
70 unsigned long cursor, end;
71
72 kmemleak_free_part(__va(addr), size);
73
74 cursor = PFN_UP(addr);
75 end = PFN_DOWN(addr + size);
76
77 for (; cursor < end; cursor++) {
78 __free_pages_bootmem(pfn_to_page(cursor), 0);
79 totalram_pages++;
80 }
81}
82
83static void __init __free_pages_memory(unsigned long start, unsigned long end)
84{
85 int i;
86 unsigned long start_aligned, end_aligned;
87 int order = ilog2(BITS_PER_LONG);
88
89 start_aligned = (start + (BITS_PER_LONG - 1)) & ~(BITS_PER_LONG - 1);
90 end_aligned = end & ~(BITS_PER_LONG - 1);
91
92 if (end_aligned <= start_aligned) {
93 for (i = start; i < end; i++)
94 __free_pages_bootmem(pfn_to_page(i), 0);
95
96 return;
97 }
98
99 for (i = start; i < start_aligned; i++)
100 __free_pages_bootmem(pfn_to_page(i), 0);
101
102 for (i = start_aligned; i < end_aligned; i += BITS_PER_LONG)
103 __free_pages_bootmem(pfn_to_page(i), order);
104
105 for (i = end_aligned; i < end; i++)
106 __free_pages_bootmem(pfn_to_page(i), 0);
107}
108
Tejun Heo64a02da2011-07-12 11:16:01 +0200109unsigned long __init free_low_memory_core_early(int nodeid)
Yinghai Lu09325872011-02-24 14:43:05 +0100110{
111 int i;
112 u64 start, end;
113 unsigned long count = 0;
114 struct range *range = NULL;
115 int nr_range;
116
Tejun Heo64a02da2011-07-12 11:16:01 +0200117 nr_range = __get_free_all_memory_range(&range, nodeid, 0, max_low_pfn);
Yinghai Lu09325872011-02-24 14:43:05 +0100118
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);
124 }
125
126 return count;
127}
128
129/**
130 * free_all_bootmem_node - release a node's free pages to the buddy allocator
131 * @pgdat: node to be released
132 *
133 * Returns the number of pages actually released.
134 */
135unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
136{
137 register_page_bootmem_info_node(pgdat);
138
Tejun Heo64a02da2011-07-12 11:16:01 +0200139 /* free_low_memory_core_early(MAX_NUMNODES) will be called later */
Yinghai Lu09325872011-02-24 14:43:05 +0100140 return 0;
141}
142
143/**
144 * free_all_bootmem - release free pages to the buddy allocator
145 *
146 * Returns the number of pages actually released.
147 */
148unsigned long __init free_all_bootmem(void)
149{
150 /*
151 * We need to use MAX_NUMNODES instead of NODE_DATA(0)->node_id
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300152 * because in some case like Node0 doesn't have RAM installed
Yinghai Lu09325872011-02-24 14:43:05 +0100153 * 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
156 */
Tejun Heo64a02da2011-07-12 11:16:01 +0200157 return free_low_memory_core_early(MAX_NUMNODES);
Yinghai Lu09325872011-02-24 14:43:05 +0100158}
159
160/**
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
165 *
166 * Partial pages will be considered reserved and left as they are.
167 *
168 * The range must reside completely on the specified node.
169 */
170void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
171 unsigned long size)
172{
173 kmemleak_free_part(__va(physaddr), size);
174 memblock_x86_free_range(physaddr, physaddr + size);
175}
176
177/**
178 * free_bootmem - mark a page range as usable
179 * @addr: starting address of the range
180 * @size: size of the range in bytes
181 *
182 * Partial pages will be considered reserved and left as they are.
183 *
184 * The range must be contiguous but may span node boundaries.
185 */
186void __init free_bootmem(unsigned long addr, unsigned long size)
187{
188 kmemleak_free_part(__va(addr), size);
189 memblock_x86_free_range(addr, addr + size);
190}
191
192static void * __init ___alloc_bootmem_nopanic(unsigned long size,
193 unsigned long align,
194 unsigned long goal,
195 unsigned long limit)
196{
197 void *ptr;
198
199 if (WARN_ON_ONCE(slab_is_available()))
200 return kzalloc(size, GFP_NOWAIT);
201
202restart:
203
204 ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, goal, limit);
205
206 if (ptr)
207 return ptr;
208
209 if (goal != 0) {
210 goal = 0;
211 goto restart;
212 }
213
214 return NULL;
215}
216
217/**
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
222 *
223 * The goal is dropped if it can not be satisfied and the allocation will
224 * fall back to memory below @goal.
225 *
226 * Allocation may happen on any node in the system.
227 *
228 * Returns NULL on failure.
229 */
230void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
231 unsigned long goal)
232{
233 unsigned long limit = -1UL;
234
235 return ___alloc_bootmem_nopanic(size, align, goal, limit);
236}
237
238static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
239 unsigned long goal, unsigned long limit)
240{
241 void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
242
243 if (mem)
244 return mem;
245 /*
246 * Whoops, we cannot satisfy the allocation request.
247 */
248 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
249 panic("Out of memory");
250 return NULL;
251}
252
253/**
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
258 *
259 * The goal is dropped if it can not be satisfied and the allocation will
260 * fall back to memory below @goal.
261 *
262 * Allocation may happen on any node in the system.
263 *
264 * The function panics if the request can not be satisfied.
265 */
266void * __init __alloc_bootmem(unsigned long size, unsigned long align,
267 unsigned long goal)
268{
269 unsigned long limit = -1UL;
270
271 return ___alloc_bootmem(size, align, goal, limit);
272}
273
274/**
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
280 *
281 * The goal is dropped if it can not be satisfied and the allocation will
282 * fall back to memory below @goal.
283 *
284 * Allocation may fall back to any node in the system if the specified node
285 * can not hold the requested memory.
286 *
287 * The function panics if the request can not be satisfied.
288 */
289void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
290 unsigned long align, unsigned long goal)
291{
292 void *ptr;
293
294 if (WARN_ON_ONCE(slab_is_available()))
295 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
296
297 ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
298 goal, -1ULL);
299 if (ptr)
300 return ptr;
301
302 return __alloc_memory_core_early(MAX_NUMNODES, size, align,
303 goal, -1ULL);
304}
305
306void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
307 unsigned long align, unsigned long goal)
308{
Yinghai Lu09325872011-02-24 14:43:05 +0100309 return __alloc_bootmem_node(pgdat, size, align, goal);
Yinghai Lu09325872011-02-24 14:43:05 +0100310}
311
312#ifdef CONFIG_SPARSEMEM
313/**
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
317 *
318 * Return NULL on failure.
319 */
320void * __init alloc_bootmem_section(unsigned long size,
321 unsigned long section_nr)
322{
323 unsigned long pfn, goal, limit;
324
325 pfn = section_nr_to_pfn(section_nr);
326 goal = pfn << PAGE_SHIFT;
327 limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
328
329 return __alloc_memory_core_early(early_pfn_to_nid(pfn), size,
330 SMP_CACHE_BYTES, goal, limit);
331}
332#endif
333
334void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
335 unsigned long align, unsigned long goal)
336{
337 void *ptr;
338
339 if (WARN_ON_ONCE(slab_is_available()))
340 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
341
342 ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
343 goal, -1ULL);
344 if (ptr)
345 return ptr;
346
347 return __alloc_bootmem_nopanic(size, align, goal);
348}
349
350#ifndef ARCH_LOW_ADDRESS_LIMIT
351#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
352#endif
353
354/**
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
359 *
360 * The goal is dropped if it can not be satisfied and the allocation will
361 * fall back to memory below @goal.
362 *
363 * Allocation may happen on any node in the system.
364 *
365 * The function panics if the request can not be satisfied.
366 */
367void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
368 unsigned long goal)
369{
370 return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
371}
372
373/**
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
379 *
380 * The goal is dropped if it can not be satisfied and the allocation will
381 * fall back to memory below @goal.
382 *
383 * Allocation may fall back to any node in the system if the specified node
384 * can not hold the requested memory.
385 *
386 * The function panics if the request can not be satisfied.
387 */
388void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
389 unsigned long align, unsigned long goal)
390{
391 void *ptr;
392
393 if (WARN_ON_ONCE(slab_is_available()))
394 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
395
396 ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
397 goal, ARCH_LOW_ADDRESS_LIMIT);
398 if (ptr)
399 return ptr;
400
401 return __alloc_memory_core_early(MAX_NUMNODES, size, align,
402 goal, ARCH_LOW_ADDRESS_LIMIT);
403}