Commit | Line | Data |
---|---|---|
09325873 YL |
1 | /* |
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> | |
b95f1b31 | 15 | #include <linux/export.h> |
09325873 YL |
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 | ||
e782ab42 YL |
26 | #ifndef CONFIG_NEED_MULTIPLE_NODES |
27 | struct pglist_data __refdata contig_page_data; | |
28 | EXPORT_SYMBOL(contig_page_data); | |
29 | #endif | |
30 | ||
09325873 YL |
31 | unsigned long max_low_pfn; |
32 | unsigned long min_low_pfn; | |
33 | unsigned long max_pfn; | |
34 | ||
8bc1f91e YL |
35 | static 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 | ||
eb40c4c2 | 44 | addr = memblock_find_in_range_node(goal, limit, size, align, nid); |
1f5026a7 | 45 | if (!addr) |
8bc1f91e YL |
46 | return NULL; |
47 | ||
b476e295 | 48 | memblock_reserve(addr, size); |
8bc1f91e YL |
49 | ptr = phys_to_virt(addr); |
50 | memset(ptr, 0, size); | |
8bc1f91e YL |
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 | ||
09325873 YL |
59 | /* |
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 | */ | |
68 | void __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 | ||
83 | static void __init __free_pages_memory(unsigned long start, unsigned long end) | |
84 | { | |
309d0b39 | 85 | int order; |
09325873 | 86 | |
309d0b39 RH |
87 | while (start < end) { |
88 | order = min(MAX_ORDER - 1UL, __ffs(start)); | |
09325873 | 89 | |
309d0b39 RH |
90 | while (start + (1UL << order) > end) |
91 | order--; | |
09325873 | 92 | |
309d0b39 | 93 | __free_pages_bootmem(pfn_to_page(start), order); |
09325873 | 94 | |
309d0b39 RH |
95 | start += (1UL << order); |
96 | } | |
09325873 YL |
97 | } |
98 | ||
29f67386 YL |
99 | static unsigned long __init __free_memory_core(phys_addr_t start, |
100 | phys_addr_t end) | |
101 | { | |
102 | unsigned long start_pfn = PFN_UP(start); | |
103 | unsigned long end_pfn = min_t(unsigned long, | |
104 | PFN_DOWN(end), max_low_pfn); | |
105 | ||
106 | if (start_pfn > end_pfn) | |
107 | return 0; | |
108 | ||
109 | __free_pages_memory(start_pfn, end_pfn); | |
110 | ||
111 | return end_pfn - start_pfn; | |
112 | } | |
113 | ||
b4def350 | 114 | static unsigned long __init free_low_memory_core_early(void) |
09325873 | 115 | { |
09325873 | 116 | unsigned long count = 0; |
29f67386 | 117 | phys_addr_t start, end, size; |
8a9ca34c TH |
118 | u64 i; |
119 | ||
29f67386 YL |
120 | for_each_free_mem_range(i, MAX_NUMNODES, &start, &end, NULL) |
121 | count += __free_memory_core(start, end); | |
122 | ||
123 | /* free range that is used for reserved array if we allocate it */ | |
124 | size = get_allocated_memblock_reserved_regions_info(&start); | |
125 | if (size) | |
126 | count += __free_memory_core(start, start + size); | |
09325873 YL |
127 | |
128 | return count; | |
129 | } | |
130 | ||
7b4b2a0d JL |
131 | static int reset_managed_pages_done __initdata; |
132 | ||
133 | static inline void __init reset_node_managed_pages(pg_data_t *pgdat) | |
9feedc9d JL |
134 | { |
135 | struct zone *z; | |
136 | ||
7b4b2a0d JL |
137 | if (reset_managed_pages_done) |
138 | return; | |
9feedc9d | 139 | for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) |
7b4b2a0d JL |
140 | z->managed_pages = 0; |
141 | } | |
142 | ||
143 | void __init reset_all_zones_managed_pages(void) | |
144 | { | |
145 | struct pglist_data *pgdat; | |
146 | ||
147 | for_each_online_pgdat(pgdat) | |
148 | reset_node_managed_pages(pgdat); | |
149 | reset_managed_pages_done = 1; | |
9feedc9d JL |
150 | } |
151 | ||
09325873 YL |
152 | /** |
153 | * free_all_bootmem - release free pages to the buddy allocator | |
154 | * | |
155 | * Returns the number of pages actually released. | |
156 | */ | |
157 | unsigned long __init free_all_bootmem(void) | |
158 | { | |
0c988534 JL |
159 | unsigned long pages; |
160 | ||
7b4b2a0d | 161 | reset_all_zones_managed_pages(); |
9feedc9d | 162 | |
09325873 YL |
163 | /* |
164 | * We need to use MAX_NUMNODES instead of NODE_DATA(0)->node_id | |
25985edc | 165 | * because in some case like Node0 doesn't have RAM installed |
09325873 | 166 | * low ram will be on Node1 |
09325873 | 167 | */ |
0c988534 JL |
168 | pages = free_low_memory_core_early(); |
169 | totalram_pages += pages; | |
170 | ||
171 | return pages; | |
09325873 YL |
172 | } |
173 | ||
174 | /** | |
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 | |
179 | * | |
180 | * Partial pages will be considered reserved and left as they are. | |
181 | * | |
182 | * The range must reside completely on the specified node. | |
183 | */ | |
184 | void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, | |
185 | unsigned long size) | |
186 | { | |
187 | kmemleak_free_part(__va(physaddr), size); | |
24aa0788 | 188 | memblock_free(physaddr, size); |
09325873 YL |
189 | } |
190 | ||
191 | /** | |
192 | * free_bootmem - mark a page range as usable | |
193 | * @addr: starting address of the range | |
194 | * @size: size of the range in bytes | |
195 | * | |
196 | * Partial pages will be considered reserved and left as they are. | |
197 | * | |
198 | * The range must be contiguous but may span node boundaries. | |
199 | */ | |
200 | void __init free_bootmem(unsigned long addr, unsigned long size) | |
201 | { | |
202 | kmemleak_free_part(__va(addr), size); | |
24aa0788 | 203 | memblock_free(addr, size); |
09325873 YL |
204 | } |
205 | ||
206 | static void * __init ___alloc_bootmem_nopanic(unsigned long size, | |
207 | unsigned long align, | |
208 | unsigned long goal, | |
209 | unsigned long limit) | |
210 | { | |
211 | void *ptr; | |
212 | ||
213 | if (WARN_ON_ONCE(slab_is_available())) | |
214 | return kzalloc(size, GFP_NOWAIT); | |
215 | ||
216 | restart: | |
217 | ||
218 | ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, goal, limit); | |
219 | ||
220 | if (ptr) | |
221 | return ptr; | |
222 | ||
223 | if (goal != 0) { | |
224 | goal = 0; | |
225 | goto restart; | |
226 | } | |
227 | ||
228 | return NULL; | |
229 | } | |
230 | ||
231 | /** | |
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 | |
236 | * | |
237 | * The goal is dropped if it can not be satisfied and the allocation will | |
238 | * fall back to memory below @goal. | |
239 | * | |
240 | * Allocation may happen on any node in the system. | |
241 | * | |
242 | * Returns NULL on failure. | |
243 | */ | |
244 | void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align, | |
245 | unsigned long goal) | |
246 | { | |
247 | unsigned long limit = -1UL; | |
248 | ||
249 | return ___alloc_bootmem_nopanic(size, align, goal, limit); | |
250 | } | |
251 | ||
252 | static void * __init ___alloc_bootmem(unsigned long size, unsigned long align, | |
253 | unsigned long goal, unsigned long limit) | |
254 | { | |
255 | void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit); | |
256 | ||
257 | if (mem) | |
258 | return mem; | |
259 | /* | |
260 | * Whoops, we cannot satisfy the allocation request. | |
261 | */ | |
262 | printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size); | |
263 | panic("Out of memory"); | |
264 | return NULL; | |
265 | } | |
266 | ||
267 | /** | |
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 | |
272 | * | |
273 | * The goal is dropped if it can not be satisfied and the allocation will | |
274 | * fall back to memory below @goal. | |
275 | * | |
276 | * Allocation may happen on any node in the system. | |
277 | * | |
278 | * The function panics if the request can not be satisfied. | |
279 | */ | |
280 | void * __init __alloc_bootmem(unsigned long size, unsigned long align, | |
281 | unsigned long goal) | |
282 | { | |
283 | unsigned long limit = -1UL; | |
284 | ||
285 | return ___alloc_bootmem(size, align, goal, limit); | |
286 | } | |
287 | ||
99ab7b19 | 288 | void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat, |
ba539868 JW |
289 | unsigned long size, |
290 | unsigned long align, | |
291 | unsigned long goal, | |
292 | unsigned long limit) | |
293 | { | |
294 | void *ptr; | |
295 | ||
296 | again: | |
297 | ptr = __alloc_memory_core_early(pgdat->node_id, size, align, | |
298 | goal, limit); | |
299 | if (ptr) | |
300 | return ptr; | |
301 | ||
302 | ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, | |
303 | goal, limit); | |
304 | if (ptr) | |
305 | return ptr; | |
306 | ||
307 | if (goal) { | |
308 | goal = 0; | |
309 | goto again; | |
310 | } | |
311 | ||
312 | return NULL; | |
313 | } | |
314 | ||
315 | void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size, | |
316 | unsigned long align, unsigned long goal) | |
317 | { | |
318 | if (WARN_ON_ONCE(slab_is_available())) | |
319 | return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id); | |
320 | ||
321 | return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0); | |
322 | } | |
323 | ||
324 | void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size, | |
325 | unsigned long align, unsigned long goal, | |
326 | unsigned long limit) | |
327 | { | |
328 | void *ptr; | |
329 | ||
330 | ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, limit); | |
331 | if (ptr) | |
332 | return ptr; | |
333 | ||
334 | printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size); | |
335 | panic("Out of memory"); | |
336 | return NULL; | |
337 | } | |
338 | ||
09325873 YL |
339 | /** |
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 | |
345 | * | |
346 | * The goal is dropped if it can not be satisfied and the allocation will | |
347 | * fall back to memory below @goal. | |
348 | * | |
349 | * Allocation may fall back to any node in the system if the specified node | |
350 | * can not hold the requested memory. | |
351 | * | |
352 | * The function panics if the request can not be satisfied. | |
353 | */ | |
354 | void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size, | |
355 | unsigned long align, unsigned long goal) | |
356 | { | |
09325873 YL |
357 | if (WARN_ON_ONCE(slab_is_available())) |
358 | return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id); | |
359 | ||
ba539868 | 360 | return ___alloc_bootmem_node(pgdat, size, align, goal, 0); |
09325873 YL |
361 | } |
362 | ||
363 | void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size, | |
364 | unsigned long align, unsigned long goal) | |
365 | { | |
09325873 | 366 | return __alloc_bootmem_node(pgdat, size, align, goal); |
09325873 YL |
367 | } |
368 | ||
09325873 YL |
369 | #ifndef ARCH_LOW_ADDRESS_LIMIT |
370 | #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL | |
371 | #endif | |
372 | ||
373 | /** | |
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 | |
378 | * | |
379 | * The goal is dropped if it can not be satisfied and the allocation will | |
380 | * fall back to memory below @goal. | |
381 | * | |
382 | * Allocation may happen on any node in the system. | |
383 | * | |
384 | * The function panics if the request can not be satisfied. | |
385 | */ | |
386 | void * __init __alloc_bootmem_low(unsigned long size, unsigned long align, | |
387 | unsigned long goal) | |
388 | { | |
389 | return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT); | |
390 | } | |
391 | ||
38fa4175 YL |
392 | void * __init __alloc_bootmem_low_nopanic(unsigned long size, |
393 | unsigned long align, | |
394 | unsigned long goal) | |
395 | { | |
396 | return ___alloc_bootmem_nopanic(size, align, goal, | |
397 | ARCH_LOW_ADDRESS_LIMIT); | |
398 | } | |
399 | ||
09325873 YL |
400 | /** |
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 | |
406 | * | |
407 | * The goal is dropped if it can not be satisfied and the allocation will | |
408 | * fall back to memory below @goal. | |
409 | * | |
410 | * Allocation may fall back to any node in the system if the specified node | |
411 | * can not hold the requested memory. | |
412 | * | |
413 | * The function panics if the request can not be satisfied. | |
414 | */ | |
415 | void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size, | |
416 | unsigned long align, unsigned long goal) | |
417 | { | |
09325873 YL |
418 | if (WARN_ON_ONCE(slab_is_available())) |
419 | return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id); | |
420 | ||
ba539868 JW |
421 | return ___alloc_bootmem_node(pgdat, size, align, goal, |
422 | ARCH_LOW_ADDRESS_LIMIT); | |
09325873 | 423 | } |