blob: 6a018e49b7be3494eebed4a7a7ae340f223ed991 [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
35#ifdef CONFIG_CRASH_DUMP
36/*
37 * If we have booted due to a crash, max_pfn will be a very low value. We need
38 * to know the amount of memory that the previous kernel used.
39 */
40unsigned long saved_max_pfn;
41#endif
42
43/*
44 * free_bootmem_late - free bootmem pages directly to page allocator
45 * @addr: starting address of the range
46 * @size: size of the range in bytes
47 *
48 * This is only useful when the bootmem allocator has already been torn
49 * down, but we are still initializing the system. Pages are given directly
50 * to the page allocator, no bootmem metadata is updated because it is gone.
51 */
52void __init free_bootmem_late(unsigned long addr, unsigned long size)
53{
54 unsigned long cursor, end;
55
56 kmemleak_free_part(__va(addr), size);
57
58 cursor = PFN_UP(addr);
59 end = PFN_DOWN(addr + size);
60
61 for (; cursor < end; cursor++) {
62 __free_pages_bootmem(pfn_to_page(cursor), 0);
63 totalram_pages++;
64 }
65}
66
67static void __init __free_pages_memory(unsigned long start, unsigned long end)
68{
69 int i;
70 unsigned long start_aligned, end_aligned;
71 int order = ilog2(BITS_PER_LONG);
72
73 start_aligned = (start + (BITS_PER_LONG - 1)) & ~(BITS_PER_LONG - 1);
74 end_aligned = end & ~(BITS_PER_LONG - 1);
75
76 if (end_aligned <= start_aligned) {
77 for (i = start; i < end; i++)
78 __free_pages_bootmem(pfn_to_page(i), 0);
79
80 return;
81 }
82
83 for (i = start; i < start_aligned; i++)
84 __free_pages_bootmem(pfn_to_page(i), 0);
85
86 for (i = start_aligned; i < end_aligned; i += BITS_PER_LONG)
87 __free_pages_bootmem(pfn_to_page(i), order);
88
89 for (i = end_aligned; i < end; i++)
90 __free_pages_bootmem(pfn_to_page(i), 0);
91}
92
93unsigned long __init free_all_memory_core_early(int nodeid)
94{
95 int i;
96 u64 start, end;
97 unsigned long count = 0;
98 struct range *range = NULL;
99 int nr_range;
100
101 nr_range = get_free_all_memory_range(&range, nodeid);
102
103 for (i = 0; i < nr_range; i++) {
104 start = range[i].start;
105 end = range[i].end;
106 count += end - start;
107 __free_pages_memory(start, end);
108 }
109
110 return count;
111}
112
113/**
114 * free_all_bootmem_node - release a node's free pages to the buddy allocator
115 * @pgdat: node to be released
116 *
117 * Returns the number of pages actually released.
118 */
119unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
120{
121 register_page_bootmem_info_node(pgdat);
122
123 /* free_all_memory_core_early(MAX_NUMNODES) will be called later */
124 return 0;
125}
126
127/**
128 * free_all_bootmem - release free pages to the buddy allocator
129 *
130 * Returns the number of pages actually released.
131 */
132unsigned long __init free_all_bootmem(void)
133{
134 /*
135 * We need to use MAX_NUMNODES instead of NODE_DATA(0)->node_id
136 * because in some case like Node0 doesnt have RAM installed
137 * low ram will be on Node1
138 * Use MAX_NUMNODES will make sure all ranges in early_node_map[]
139 * will be used instead of only Node0 related
140 */
141 return free_all_memory_core_early(MAX_NUMNODES);
142}
143
144/**
145 * free_bootmem_node - mark a page range as usable
146 * @pgdat: node the range resides on
147 * @physaddr: starting address of the range
148 * @size: size of the range in bytes
149 *
150 * Partial pages will be considered reserved and left as they are.
151 *
152 * The range must reside completely on the specified node.
153 */
154void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
155 unsigned long size)
156{
157 kmemleak_free_part(__va(physaddr), size);
158 memblock_x86_free_range(physaddr, physaddr + size);
159}
160
161/**
162 * free_bootmem - mark a page range as usable
163 * @addr: 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 be contiguous but may span node boundaries.
169 */
170void __init free_bootmem(unsigned long addr, unsigned long size)
171{
172 kmemleak_free_part(__va(addr), size);
173 memblock_x86_free_range(addr, addr + size);
174}
175
176static void * __init ___alloc_bootmem_nopanic(unsigned long size,
177 unsigned long align,
178 unsigned long goal,
179 unsigned long limit)
180{
181 void *ptr;
182
183 if (WARN_ON_ONCE(slab_is_available()))
184 return kzalloc(size, GFP_NOWAIT);
185
186restart:
187
188 ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, goal, limit);
189
190 if (ptr)
191 return ptr;
192
193 if (goal != 0) {
194 goal = 0;
195 goto restart;
196 }
197
198 return NULL;
199}
200
201/**
202 * __alloc_bootmem_nopanic - allocate boot memory without panicking
203 * @size: size of the request in bytes
204 * @align: alignment of the region
205 * @goal: preferred starting address of the region
206 *
207 * The goal is dropped if it can not be satisfied and the allocation will
208 * fall back to memory below @goal.
209 *
210 * Allocation may happen on any node in the system.
211 *
212 * Returns NULL on failure.
213 */
214void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
215 unsigned long goal)
216{
217 unsigned long limit = -1UL;
218
219 return ___alloc_bootmem_nopanic(size, align, goal, limit);
220}
221
222static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
223 unsigned long goal, unsigned long limit)
224{
225 void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
226
227 if (mem)
228 return mem;
229 /*
230 * Whoops, we cannot satisfy the allocation request.
231 */
232 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
233 panic("Out of memory");
234 return NULL;
235}
236
237/**
238 * __alloc_bootmem - allocate boot memory
239 * @size: size of the request in bytes
240 * @align: alignment of the region
241 * @goal: preferred starting address of the region
242 *
243 * The goal is dropped if it can not be satisfied and the allocation will
244 * fall back to memory below @goal.
245 *
246 * Allocation may happen on any node in the system.
247 *
248 * The function panics if the request can not be satisfied.
249 */
250void * __init __alloc_bootmem(unsigned long size, unsigned long align,
251 unsigned long goal)
252{
253 unsigned long limit = -1UL;
254
255 return ___alloc_bootmem(size, align, goal, limit);
256}
257
258/**
259 * __alloc_bootmem_node - allocate boot memory from a specific node
260 * @pgdat: node to allocate from
261 * @size: size of the request in bytes
262 * @align: alignment of the region
263 * @goal: preferred starting address of the region
264 *
265 * The goal is dropped if it can not be satisfied and the allocation will
266 * fall back to memory below @goal.
267 *
268 * Allocation may fall back to any node in the system if the specified node
269 * can not hold the requested memory.
270 *
271 * The function panics if the request can not be satisfied.
272 */
273void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
274 unsigned long align, unsigned long goal)
275{
276 void *ptr;
277
278 if (WARN_ON_ONCE(slab_is_available()))
279 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
280
281 ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
282 goal, -1ULL);
283 if (ptr)
284 return ptr;
285
286 return __alloc_memory_core_early(MAX_NUMNODES, size, align,
287 goal, -1ULL);
288}
289
290void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
291 unsigned long align, unsigned long goal)
292{
293#ifdef MAX_DMA32_PFN
294 unsigned long end_pfn;
295
296 if (WARN_ON_ONCE(slab_is_available()))
297 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
298
299 /* update goal according ...MAX_DMA32_PFN */
300 end_pfn = pgdat->node_start_pfn + pgdat->node_spanned_pages;
301
302 if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
303 (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
304 void *ptr;
305 unsigned long new_goal;
306
307 new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
308 ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
309 new_goal, -1ULL);
310 if (ptr)
311 return ptr;
312 }
313#endif
314
315 return __alloc_bootmem_node(pgdat, size, align, goal);
316
317}
318
319#ifdef CONFIG_SPARSEMEM
320/**
321 * alloc_bootmem_section - allocate boot memory from a specific section
322 * @size: size of the request in bytes
323 * @section_nr: sparse map section to allocate from
324 *
325 * Return NULL on failure.
326 */
327void * __init alloc_bootmem_section(unsigned long size,
328 unsigned long section_nr)
329{
330 unsigned long pfn, goal, limit;
331
332 pfn = section_nr_to_pfn(section_nr);
333 goal = pfn << PAGE_SHIFT;
334 limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
335
336 return __alloc_memory_core_early(early_pfn_to_nid(pfn), size,
337 SMP_CACHE_BYTES, goal, limit);
338}
339#endif
340
341void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
342 unsigned long align, unsigned long goal)
343{
344 void *ptr;
345
346 if (WARN_ON_ONCE(slab_is_available()))
347 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
348
349 ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
350 goal, -1ULL);
351 if (ptr)
352 return ptr;
353
354 return __alloc_bootmem_nopanic(size, align, goal);
355}
356
357#ifndef ARCH_LOW_ADDRESS_LIMIT
358#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
359#endif
360
361/**
362 * __alloc_bootmem_low - allocate low boot memory
363 * @size: size of the request in bytes
364 * @align: alignment of the region
365 * @goal: preferred starting address of the region
366 *
367 * The goal is dropped if it can not be satisfied and the allocation will
368 * fall back to memory below @goal.
369 *
370 * Allocation may happen on any node in the system.
371 *
372 * The function panics if the request can not be satisfied.
373 */
374void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
375 unsigned long goal)
376{
377 return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
378}
379
380/**
381 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
382 * @pgdat: node to allocate from
383 * @size: size of the request in bytes
384 * @align: alignment of the region
385 * @goal: preferred starting address of the region
386 *
387 * The goal is dropped if it can not be satisfied and the allocation will
388 * fall back to memory below @goal.
389 *
390 * Allocation may fall back to any node in the system if the specified node
391 * can not hold the requested memory.
392 *
393 * The function panics if the request can not be satisfied.
394 */
395void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
396 unsigned long align, unsigned long goal)
397{
398 void *ptr;
399
400 if (WARN_ON_ONCE(slab_is_available()))
401 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
402
403 ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
404 goal, ARCH_LOW_ADDRESS_LIMIT);
405 if (ptr)
406 return ptr;
407
408 return __alloc_memory_core_early(MAX_NUMNODES, size, align,
409 goal, ARCH_LOW_ADDRESS_LIMIT);
410}