blob: 2c254d37465549b0230b543cd9ac7c4d60cdf7c1 [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>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040015#include <linux/export.h>
Yinghai Lu09325872011-02-24 14:43:05 +010016#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
Joonsoo Kimb476e292013-04-29 15:08:53 -070048 memblock_reserve(addr, size);
Yinghai Lu8bc1f912011-02-24 14:43:06 +010049 ptr = phys_to_virt(addr);
50 memset(ptr, 0, size);
Yinghai Lu8bc1f912011-02-24 14:43:06 +010051 /*
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{
Robin Holt309d0b32013-11-12 15:07:23 -080085 int order;
Yinghai Lu09325872011-02-24 14:43:05 +010086
Robin Holt309d0b32013-11-12 15:07:23 -080087 while (start < end) {
88 order = min(MAX_ORDER - 1UL, __ffs(start));
Yinghai Lu09325872011-02-24 14:43:05 +010089
Robin Holt309d0b32013-11-12 15:07:23 -080090 while (start + (1UL << order) > end)
91 order--;
Yinghai Lu09325872011-02-24 14:43:05 +010092
Robin Holt309d0b32013-11-12 15:07:23 -080093 __free_pages_bootmem(pfn_to_page(start), order);
94
95 start += (1UL << order);
Yinghai Lu09325872011-02-24 14:43:05 +010096 }
Yinghai Lu09325872011-02-24 14:43:05 +010097}
98
Yinghai Lu29f67382012-07-11 14:02:56 -070099static 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
Joonsoo Kimb4def352013-04-29 15:08:52 -0700114static unsigned long __init free_low_memory_core_early(void)
Yinghai Lu09325872011-02-24 14:43:05 +0100115{
Yinghai Lu09325872011-02-24 14:43:05 +0100116 unsigned long count = 0;
Yinghai Lu29f67382012-07-11 14:02:56 -0700117 phys_addr_t start, end, size;
Tejun Heo8a9ca342011-07-12 11:16:02 +0200118 u64 i;
Yinghai Lu09325872011-02-24 14:43:05 +0100119
Yinghai Lu29f67382012-07-11 14:02:56 -0700120 for_each_free_mem_range(i, MAX_NUMNODES, &start, &end, NULL)
121 count += __free_memory_core(start, end);
Yinghai Lu09325872011-02-24 14:43:05 +0100122
Yinghai Lu29f67382012-07-11 14:02:56 -0700123 /* 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);
Yinghai Lu09325872011-02-24 14:43:05 +0100127
128 return count;
129}
130
Jiang Liu7b4b2a02013-07-03 15:03:11 -0700131static int reset_managed_pages_done __initdata;
132
133static inline void __init reset_node_managed_pages(pg_data_t *pgdat)
Jiang Liu9feedc92012-12-12 13:52:12 -0800134{
135 struct zone *z;
136
Jiang Liu7b4b2a02013-07-03 15:03:11 -0700137 if (reset_managed_pages_done)
138 return;
Jiang Liu9feedc92012-12-12 13:52:12 -0800139 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
Jiang Liu7b4b2a02013-07-03 15:03:11 -0700140 z->managed_pages = 0;
141}
142
143void __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;
Jiang Liu9feedc92012-12-12 13:52:12 -0800150}
151
Yinghai Lu09325872011-02-24 14:43:05 +0100152/**
Yinghai Lu09325872011-02-24 14:43:05 +0100153 * free_all_bootmem - release free pages to the buddy allocator
154 *
155 * Returns the number of pages actually released.
156 */
157unsigned long __init free_all_bootmem(void)
158{
Jiang Liu0c988532013-07-03 15:03:24 -0700159 unsigned long pages;
160
Jiang Liu7b4b2a02013-07-03 15:03:11 -0700161 reset_all_zones_managed_pages();
Jiang Liu9feedc92012-12-12 13:52:12 -0800162
Yinghai Lu09325872011-02-24 14:43:05 +0100163 /*
164 * We need to use MAX_NUMNODES instead of NODE_DATA(0)->node_id
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300165 * because in some case like Node0 doesn't have RAM installed
Yinghai Lu09325872011-02-24 14:43:05 +0100166 * low ram will be on Node1
Yinghai Lu09325872011-02-24 14:43:05 +0100167 */
Jiang Liu0c988532013-07-03 15:03:24 -0700168 pages = free_low_memory_core_early();
169 totalram_pages += pages;
170
171 return pages;
Yinghai Lu09325872011-02-24 14:43:05 +0100172}
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 */
184void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
185 unsigned long size)
186{
187 kmemleak_free_part(__va(physaddr), size);
Tejun Heo24aa0782011-07-12 11:16:06 +0200188 memblock_free(physaddr, size);
Yinghai Lu09325872011-02-24 14:43:05 +0100189}
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 */
200void __init free_bootmem(unsigned long addr, unsigned long size)
201{
202 kmemleak_free_part(__va(addr), size);
Tejun Heo24aa0782011-07-12 11:16:06 +0200203 memblock_free(addr, size);
Yinghai Lu09325872011-02-24 14:43:05 +0100204}
205
206static 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
216restart:
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 */
244void * __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
252static 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 */
280void * __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
Yinghai Lu99ab7b12012-07-11 14:02:53 -0700288void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
Johannes Weinerba539862012-05-29 15:06:35 -0700289 unsigned long size,
290 unsigned long align,
291 unsigned long goal,
292 unsigned long limit)
293{
294 void *ptr;
295
296again:
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
315void * __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
324void * __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
Yinghai Lu09325872011-02-24 14:43:05 +0100339/**
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 */
354void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
355 unsigned long align, unsigned long goal)
356{
Yinghai Lu09325872011-02-24 14:43:05 +0100357 if (WARN_ON_ONCE(slab_is_available()))
358 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
359
Johannes Weinerba539862012-05-29 15:06:35 -0700360 return ___alloc_bootmem_node(pgdat, size, align, goal, 0);
Yinghai Lu09325872011-02-24 14:43:05 +0100361}
362
363void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
364 unsigned long align, unsigned long goal)
365{
Yinghai Lu09325872011-02-24 14:43:05 +0100366 return __alloc_bootmem_node(pgdat, size, align, goal);
Yinghai Lu09325872011-02-24 14:43:05 +0100367}
368
Yinghai Lu09325872011-02-24 14:43:05 +0100369#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 */
386void * __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
Yinghai Lu38fa4172013-01-24 12:20:15 -0800392void * __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
Yinghai Lu09325872011-02-24 14:43:05 +0100400/**
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 */
415void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
416 unsigned long align, unsigned long goal)
417{
Yinghai Lu09325872011-02-24 14:43:05 +0100418 if (WARN_ON_ONCE(slab_is_available()))
419 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
420
Johannes Weinerba539862012-05-29 15:06:35 -0700421 return ___alloc_bootmem_node(pgdat, size, align, goal,
422 ARCH_LOW_ADDRESS_LIMIT);
Yinghai Lu09325872011-02-24 14:43:05 +0100423}