Merge tag 'asm-generic-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / mm / bootmem.c
CommitLineData
1da177e4 1/*
57cfc29e 2 * bootmem - A boot-time physical memory allocator and configurator
1da177e4
LT
3 *
4 * Copyright (C) 1999 Ingo Molnar
57cfc29e
JW
5 * 1999 Kanoj Sarcar, SGI
6 * 2008 Johannes Weiner
1da177e4 7 *
57cfc29e
JW
8 * Access to this subsystem has to be serialized externally (which is true
9 * for the boot process anyway).
1da177e4 10 */
1da177e4 11#include <linux/init.h>
bbc7b92e 12#include <linux/pfn.h>
5a0e3ad6 13#include <linux/slab.h>
1da177e4 14#include <linux/bootmem.h>
b95f1b31 15#include <linux/export.h>
ec3a354b 16#include <linux/kmemleak.h>
08677214 17#include <linux/range.h>
72d7c3b3 18#include <linux/memblock.h>
d85fbee8
PM
19#include <linux/bug.h>
20#include <linux/io.h>
e786e86a 21
dfd54cbc 22#include <asm/processor.h>
e786e86a 23
1da177e4
LT
24#include "internal.h"
25
e782ab42
YL
26#ifndef CONFIG_NEED_MULTIPLE_NODES
27struct pglist_data __refdata contig_page_data = {
28 .bdata = &bootmem_node_data[0]
29};
30EXPORT_SYMBOL(contig_page_data);
31#endif
32
1da177e4
LT
33unsigned long max_low_pfn;
34unsigned long min_low_pfn;
35unsigned long max_pfn;
8dd33030 36unsigned long long max_possible_pfn;
1da177e4 37
b61bfa3c
JW
38bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata;
39
636cc40c
JW
40static struct list_head bdata_list __initdata = LIST_HEAD_INIT(bdata_list);
41
2e5237da
JW
42static int bootmem_debug;
43
44static int __init bootmem_debug_setup(char *buf)
45{
46 bootmem_debug = 1;
47 return 0;
48}
49early_param("bootmem_debug", bootmem_debug_setup);
50
51#define bdebug(fmt, args...) ({ \
52 if (unlikely(bootmem_debug)) \
53 printk(KERN_INFO \
54 "bootmem::%s " fmt, \
80a914dc 55 __func__, ## args); \
2e5237da
JW
56})
57
df049a5f 58static unsigned long __init bootmap_bytes(unsigned long pages)
223e8dc9 59{
9571a982 60 unsigned long bytes = DIV_ROUND_UP(pages, 8);
223e8dc9 61
df049a5f 62 return ALIGN(bytes, sizeof(long));
223e8dc9
JW
63}
64
a66fd7da
JW
65/**
66 * bootmem_bootmap_pages - calculate bitmap size in pages
67 * @pages: number of pages the bitmap has to represent
68 */
f71bf0ca 69unsigned long __init bootmem_bootmap_pages(unsigned long pages)
1da177e4 70{
df049a5f 71 unsigned long bytes = bootmap_bytes(pages);
1da177e4 72
df049a5f 73 return PAGE_ALIGN(bytes) >> PAGE_SHIFT;
1da177e4 74}
f71bf0ca 75
679bc9fb
KH
76/*
77 * link bdata in order
78 */
69d49e68 79static void __init link_bootmem(bootmem_data_t *bdata)
679bc9fb 80{
5c2b8a16 81 bootmem_data_t *ent;
f71bf0ca 82
5c2b8a16
GS
83 list_for_each_entry(ent, &bdata_list, list) {
84 if (bdata->node_min_pfn < ent->node_min_pfn) {
85 list_add_tail(&bdata->list, &ent->list);
86 return;
87 }
679bc9fb 88 }
5c2b8a16
GS
89
90 list_add_tail(&bdata->list, &bdata_list);
679bc9fb
KH
91}
92
1da177e4
LT
93/*
94 * Called once to set up the allocator itself.
95 */
8ae04463 96static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
1da177e4
LT
97 unsigned long mapstart, unsigned long start, unsigned long end)
98{
bbc7b92e 99 unsigned long mapsize;
1da177e4 100
2dbb51c4 101 mminit_validate_memmodel_limits(&start, &end);
bbc7b92e 102 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
3560e249 103 bdata->node_min_pfn = start;
1da177e4 104 bdata->node_low_pfn = end;
679bc9fb 105 link_bootmem(bdata);
1da177e4
LT
106
107 /*
108 * Initially all pages are reserved - setup_arch() has to
109 * register free RAM areas explicitly.
110 */
df049a5f 111 mapsize = bootmap_bytes(end - start);
1da177e4
LT
112 memset(bdata->node_bootmem_map, 0xff, mapsize);
113
2e5237da
JW
114 bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n",
115 bdata - bootmem_node_data, start, mapstart, end, mapsize);
116
1da177e4
LT
117 return mapsize;
118}
119
a66fd7da
JW
120/**
121 * init_bootmem_node - register a node as boot memory
122 * @pgdat: node to register
123 * @freepfn: pfn where the bitmap for this node is to be placed
124 * @startpfn: first pfn on the node
125 * @endpfn: first pfn after the node
126 *
127 * Returns the number of bytes needed to hold the bitmap for this node.
128 */
223e8dc9
JW
129unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
130 unsigned long startpfn, unsigned long endpfn)
131{
132 return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn);
133}
134
a66fd7da
JW
135/**
136 * init_bootmem - register boot memory
137 * @start: pfn where the bitmap is to be placed
138 * @pages: number of available physical pages
139 *
140 * Returns the number of bytes needed to hold the bitmap.
141 */
223e8dc9
JW
142unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
143{
144 max_low_pfn = pages;
145 min_low_pfn = start;
146 return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages);
147}
09325873 148
9f993ac3
FT
149/*
150 * free_bootmem_late - free bootmem pages directly to page allocator
81df9bff 151 * @addr: starting physical address of the range
9f993ac3
FT
152 * @size: size of the range in bytes
153 *
154 * This is only useful when the bootmem allocator has already been torn
155 * down, but we are still initializing the system. Pages are given directly
156 * to the page allocator, no bootmem metadata is updated because it is gone.
157 */
81df9bff 158void __init free_bootmem_late(unsigned long physaddr, unsigned long size)
9f993ac3
FT
159{
160 unsigned long cursor, end;
161
81df9bff 162 kmemleak_free_part(__va(physaddr), size);
9f993ac3 163
81df9bff
JK
164 cursor = PFN_UP(physaddr);
165 end = PFN_DOWN(physaddr + size);
9f993ac3
FT
166
167 for (; cursor < end; cursor++) {
d70ddd7a 168 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
9f993ac3
FT
169 totalram_pages++;
170 }
171}
172
223e8dc9
JW
173static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
174{
175 struct page *page;
d70ddd7a 176 unsigned long *map, start, end, pages, cur, count = 0;
41546c17
JW
177
178 if (!bdata->node_bootmem_map)
179 return 0;
180
4a099fb4 181 map = bdata->node_bootmem_map;
3560e249 182 start = bdata->node_min_pfn;
41546c17
JW
183 end = bdata->node_low_pfn;
184
799f933a
JW
185 bdebug("nid=%td start=%lx end=%lx\n",
186 bdata - bootmem_node_data, start, end);
223e8dc9 187
41546c17 188 while (start < end) {
4a099fb4 189 unsigned long idx, vec;
10d73e65 190 unsigned shift;
223e8dc9 191
3560e249 192 idx = start - bdata->node_min_pfn;
10d73e65
MF
193 shift = idx & (BITS_PER_LONG - 1);
194 /*
195 * vec holds at most BITS_PER_LONG map bits,
196 * bit 0 corresponds to start.
197 */
41546c17 198 vec = ~map[idx / BITS_PER_LONG];
10d73e65
MF
199
200 if (shift) {
201 vec >>= shift;
202 if (end - start >= BITS_PER_LONG)
203 vec |= ~map[idx / BITS_PER_LONG + 1] <<
204 (BITS_PER_LONG - shift);
205 }
799f933a
JW
206 /*
207 * If we have a properly aligned and fully unreserved
208 * BITS_PER_LONG block of pages in front of us, free
209 * it in one go.
210 */
211 if (IS_ALIGNED(start, BITS_PER_LONG) && vec == ~0UL) {
41546c17
JW
212 int order = ilog2(BITS_PER_LONG);
213
d70ddd7a 214 __free_pages_bootmem(pfn_to_page(start), start, order);
223e8dc9 215 count += BITS_PER_LONG;
799f933a 216 start += BITS_PER_LONG;
41546c17 217 } else {
d70ddd7a 218 cur = start;
41546c17 219
10d73e65
MF
220 start = ALIGN(start + 1, BITS_PER_LONG);
221 while (vec && cur != start) {
41546c17 222 if (vec & 1) {
10d73e65 223 page = pfn_to_page(cur);
d70ddd7a 224 __free_pages_bootmem(page, cur, 0);
41546c17 225 count++;
223e8dc9 226 }
41546c17 227 vec >>= 1;
10d73e65 228 ++cur;
223e8dc9 229 }
223e8dc9 230 }
223e8dc9
JW
231 }
232
d70ddd7a 233 cur = bdata->node_min_pfn;
223e8dc9 234 page = virt_to_page(bdata->node_bootmem_map);
3560e249 235 pages = bdata->node_low_pfn - bdata->node_min_pfn;
41546c17
JW
236 pages = bootmem_bootmap_pages(pages);
237 count += pages;
5576646f 238 while (pages--)
d70ddd7a 239 __free_pages_bootmem(page++, cur++, 0);
1b4ace41 240 bdata->node_bootmem_map = NULL;
223e8dc9 241
2e5237da
JW
242 bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
243
223e8dc9
JW
244 return count;
245}
246
7b4b2a0d
JL
247static int reset_managed_pages_done __initdata;
248
f784a3f1 249void reset_node_managed_pages(pg_data_t *pgdat)
9feedc9d
JL
250{
251 struct zone *z;
252
9feedc9d 253 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
7b4b2a0d
JL
254 z->managed_pages = 0;
255}
256
257void __init reset_all_zones_managed_pages(void)
258{
259 struct pglist_data *pgdat;
260
f784a3f1
TC
261 if (reset_managed_pages_done)
262 return;
263
7b4b2a0d
JL
264 for_each_online_pgdat(pgdat)
265 reset_node_managed_pages(pgdat);
f784a3f1 266
7b4b2a0d 267 reset_managed_pages_done = 1;
9feedc9d
JL
268}
269
a66fd7da
JW
270/**
271 * free_all_bootmem - release free pages to the buddy allocator
272 *
273 * Returns the number of pages actually released.
274 */
223e8dc9
JW
275unsigned long __init free_all_bootmem(void)
276{
aa235fc7
YL
277 unsigned long total_pages = 0;
278 bootmem_data_t *bdata;
9feedc9d 279
7b4b2a0d 280 reset_all_zones_managed_pages();
aa235fc7
YL
281
282 list_for_each_entry(bdata, &bdata_list, list)
283 total_pages += free_all_bootmem_core(bdata);
284
0c988534
JL
285 totalram_pages += total_pages;
286
aa235fc7 287 return total_pages;
223e8dc9
JW
288}
289
d747fa4b
JW
290static void __init __free(bootmem_data_t *bdata,
291 unsigned long sidx, unsigned long eidx)
292{
293 unsigned long idx;
294
295 bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
3560e249
JW
296 sidx + bdata->node_min_pfn,
297 eidx + bdata->node_min_pfn);
d747fa4b 298
1b4ace41
CM
299 if (WARN_ON(bdata->node_bootmem_map == NULL))
300 return;
301
e2bf3cae
JW
302 if (bdata->hint_idx > sidx)
303 bdata->hint_idx = sidx;
304
d747fa4b
JW
305 for (idx = sidx; idx < eidx; idx++)
306 if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
307 BUG();
308}
309
310static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
311 unsigned long eidx, int flags)
312{
313 unsigned long idx;
314 int exclusive = flags & BOOTMEM_EXCLUSIVE;
315
316 bdebug("nid=%td start=%lx end=%lx flags=%x\n",
317 bdata - bootmem_node_data,
3560e249
JW
318 sidx + bdata->node_min_pfn,
319 eidx + bdata->node_min_pfn,
d747fa4b
JW
320 flags);
321
1b4ace41
CM
322 if (WARN_ON(bdata->node_bootmem_map == NULL))
323 return 0;
324
d747fa4b
JW
325 for (idx = sidx; idx < eidx; idx++)
326 if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
327 if (exclusive) {
328 __free(bdata, sidx, idx);
329 return -EBUSY;
330 }
331 bdebug("silent double reserve of PFN %lx\n",
3560e249 332 idx + bdata->node_min_pfn);
d747fa4b
JW
333 }
334 return 0;
335}
336
e2bf3cae
JW
337static int __init mark_bootmem_node(bootmem_data_t *bdata,
338 unsigned long start, unsigned long end,
339 int reserve, int flags)
223e8dc9
JW
340{
341 unsigned long sidx, eidx;
223e8dc9 342
e2bf3cae
JW
343 bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
344 bdata - bootmem_node_data, start, end, reserve, flags);
223e8dc9 345
3560e249 346 BUG_ON(start < bdata->node_min_pfn);
e2bf3cae 347 BUG_ON(end > bdata->node_low_pfn);
223e8dc9 348
3560e249
JW
349 sidx = start - bdata->node_min_pfn;
350 eidx = end - bdata->node_min_pfn;
223e8dc9 351
e2bf3cae
JW
352 if (reserve)
353 return __reserve(bdata, sidx, eidx, flags);
223e8dc9 354 else
e2bf3cae
JW
355 __free(bdata, sidx, eidx);
356 return 0;
357}
358
359static int __init mark_bootmem(unsigned long start, unsigned long end,
360 int reserve, int flags)
361{
362 unsigned long pos;
363 bootmem_data_t *bdata;
364
365 pos = start;
366 list_for_each_entry(bdata, &bdata_list, list) {
367 int err;
368 unsigned long max;
369
3560e249
JW
370 if (pos < bdata->node_min_pfn ||
371 pos >= bdata->node_low_pfn) {
e2bf3cae
JW
372 BUG_ON(pos != start);
373 continue;
374 }
375
376 max = min(bdata->node_low_pfn, end);
223e8dc9 377
e2bf3cae
JW
378 err = mark_bootmem_node(bdata, pos, max, reserve, flags);
379 if (reserve && err) {
380 mark_bootmem(start, pos, 0, 0);
381 return err;
382 }
223e8dc9 383
e2bf3cae
JW
384 if (max == end)
385 return 0;
386 pos = bdata->node_low_pfn;
387 }
388 BUG();
223e8dc9
JW
389}
390
a66fd7da
JW
391/**
392 * free_bootmem_node - mark a page range as usable
393 * @pgdat: node the range resides on
394 * @physaddr: starting address of the range
395 * @size: size of the range in bytes
396 *
397 * Partial pages will be considered reserved and left as they are.
398 *
e2bf3cae 399 * The range must reside completely on the specified node.
a66fd7da 400 */
223e8dc9
JW
401void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
402 unsigned long size)
403{
e2bf3cae
JW
404 unsigned long start, end;
405
ec3a354b
CM
406 kmemleak_free_part(__va(physaddr), size);
407
e2bf3cae
JW
408 start = PFN_UP(physaddr);
409 end = PFN_DOWN(physaddr + size);
410
411 mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
223e8dc9
JW
412}
413
a66fd7da
JW
414/**
415 * free_bootmem - mark a page range as usable
81df9bff 416 * @addr: starting physical address of the range
a66fd7da
JW
417 * @size: size of the range in bytes
418 *
419 * Partial pages will be considered reserved and left as they are.
420 *
e2bf3cae 421 * The range must be contiguous but may span node boundaries.
a66fd7da 422 */
81df9bff 423void __init free_bootmem(unsigned long physaddr, unsigned long size)
223e8dc9 424{
e2bf3cae 425 unsigned long start, end;
a5645a61 426
81df9bff 427 kmemleak_free_part(__va(physaddr), size);
ec3a354b 428
81df9bff
JK
429 start = PFN_UP(physaddr);
430 end = PFN_DOWN(physaddr + size);
1da177e4 431
e2bf3cae 432 mark_bootmem(start, end, 0, 0);
1da177e4
LT
433}
434
a66fd7da
JW
435/**
436 * reserve_bootmem_node - mark a page range as reserved
437 * @pgdat: node the range resides on
438 * @physaddr: starting address of the range
439 * @size: size of the range in bytes
440 * @flags: reservation flags (see linux/bootmem.h)
441 *
442 * Partial pages will be reserved.
443 *
e2bf3cae 444 * The range must reside completely on the specified node.
a66fd7da 445 */
223e8dc9
JW
446int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
447 unsigned long size, int flags)
1da177e4 448{
e2bf3cae 449 unsigned long start, end;
1da177e4 450
e2bf3cae
JW
451 start = PFN_DOWN(physaddr);
452 end = PFN_UP(physaddr + size);
453
454 return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
223e8dc9 455}
5a982cbc 456
a66fd7da 457/**
0d4ba4d7 458 * reserve_bootmem - mark a page range as reserved
a66fd7da
JW
459 * @addr: starting address of the range
460 * @size: size of the range in bytes
461 * @flags: reservation flags (see linux/bootmem.h)
462 *
463 * Partial pages will be reserved.
464 *
e2bf3cae 465 * The range must be contiguous but may span node boundaries.
a66fd7da 466 */
223e8dc9
JW
467int __init reserve_bootmem(unsigned long addr, unsigned long size,
468 int flags)
469{
e2bf3cae 470 unsigned long start, end;
1da177e4 471
e2bf3cae
JW
472 start = PFN_DOWN(addr);
473 end = PFN_UP(addr + size);
223e8dc9 474
e2bf3cae 475 return mark_bootmem(start, end, 1, flags);
1da177e4
LT
476}
477
8aa043d7
JB
478static unsigned long __init align_idx(struct bootmem_data *bdata,
479 unsigned long idx, unsigned long step)
481ebd0d
JW
480{
481 unsigned long base = bdata->node_min_pfn;
482
483 /*
484 * Align the index with respect to the node start so that the
485 * combination of both satisfies the requested alignment.
486 */
487
488 return ALIGN(base + idx, step) - base;
489}
490
8aa043d7
JB
491static unsigned long __init align_off(struct bootmem_data *bdata,
492 unsigned long off, unsigned long align)
481ebd0d
JW
493{
494 unsigned long base = PFN_PHYS(bdata->node_min_pfn);
495
496 /* Same as align_idx for byte offsets */
497
498 return ALIGN(base + off, align) - base;
499}
500
c6785b6b 501static void * __init alloc_bootmem_bdata(struct bootmem_data *bdata,
d0c4f570
TH
502 unsigned long size, unsigned long align,
503 unsigned long goal, unsigned long limit)
1da177e4 504{
0f3caba2 505 unsigned long fallback = 0;
5f2809e6
JW
506 unsigned long min, max, start, sidx, midx, step;
507
594fe1a0
JW
508 bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
509 bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
510 align, goal, limit);
511
5f2809e6
JW
512 BUG_ON(!size);
513 BUG_ON(align & (align - 1));
514 BUG_ON(limit && goal + size > limit);
1da177e4 515
7c309a64
CK
516 if (!bdata->node_bootmem_map)
517 return NULL;
518
3560e249 519 min = bdata->node_min_pfn;
5f2809e6 520 max = bdata->node_low_pfn;
9a2dc04c 521
5f2809e6
JW
522 goal >>= PAGE_SHIFT;
523 limit >>= PAGE_SHIFT;
524
525 if (limit && max > limit)
526 max = limit;
527 if (max <= min)
9a2dc04c
YL
528 return NULL;
529
5f2809e6 530 step = max(align >> PAGE_SHIFT, 1UL);
281dd25c 531
5f2809e6
JW
532 if (goal && min < goal && goal < max)
533 start = ALIGN(goal, step);
534 else
535 start = ALIGN(min, step);
1da177e4 536
481ebd0d 537 sidx = start - bdata->node_min_pfn;
3560e249 538 midx = max - bdata->node_min_pfn;
1da177e4 539
5f2809e6 540 if (bdata->hint_idx > sidx) {
0f3caba2
JW
541 /*
542 * Handle the valid case of sidx being zero and still
543 * catch the fallback below.
544 */
545 fallback = sidx + 1;
481ebd0d 546 sidx = align_idx(bdata, bdata->hint_idx, step);
5f2809e6 547 }
1da177e4 548
5f2809e6
JW
549 while (1) {
550 int merge;
551 void *region;
552 unsigned long eidx, i, start_off, end_off;
553find_block:
554 sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
481ebd0d 555 sidx = align_idx(bdata, sidx, step);
5f2809e6 556 eidx = sidx + PFN_UP(size);
ad09315c 557
5f2809e6 558 if (sidx >= midx || eidx > midx)
66d43e98 559 break;
1da177e4 560
5f2809e6
JW
561 for (i = sidx; i < eidx; i++)
562 if (test_bit(i, bdata->node_bootmem_map)) {
481ebd0d 563 sidx = align_idx(bdata, i, step);
5f2809e6
JW
564 if (sidx == i)
565 sidx += step;
566 goto find_block;
567 }
1da177e4 568
627240aa 569 if (bdata->last_end_off & (PAGE_SIZE - 1) &&
5f2809e6 570 PFN_DOWN(bdata->last_end_off) + 1 == sidx)
481ebd0d 571 start_off = align_off(bdata, bdata->last_end_off, align);
5f2809e6
JW
572 else
573 start_off = PFN_PHYS(sidx);
574
575 merge = PFN_DOWN(start_off) < sidx;
576 end_off = start_off + size;
577
578 bdata->last_end_off = end_off;
579 bdata->hint_idx = PFN_UP(end_off);
580
581 /*
582 * Reserve the area now:
583 */
d747fa4b
JW
584 if (__reserve(bdata, PFN_DOWN(start_off) + merge,
585 PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
586 BUG();
5f2809e6 587
3560e249
JW
588 region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
589 start_off);
5f2809e6 590 memset(region, 0, size);
008139d9
CM
591 /*
592 * The min_count is set to 0 so that bootmem allocated blocks
593 * are never reported as leaks.
594 */
595 kmemleak_alloc(region, size, 0, 0);
5f2809e6 596 return region;
1da177e4
LT
597 }
598
0f3caba2 599 if (fallback) {
481ebd0d 600 sidx = align_idx(bdata, fallback - 1, step);
0f3caba2
JW
601 fallback = 0;
602 goto find_block;
603 }
604
605 return NULL;
606}
607
c12ab504 608static void * __init alloc_bootmem_core(unsigned long size,
0f3caba2
JW
609 unsigned long align,
610 unsigned long goal,
611 unsigned long limit)
612{
613 bootmem_data_t *bdata;
d0c4f570 614 void *region;
0f3caba2 615
3f7dfe24
JK
616 if (WARN_ON_ONCE(slab_is_available()))
617 return kzalloc(size, GFP_NOWAIT);
0f3caba2 618
d0c4f570 619 list_for_each_entry(bdata, &bdata_list, list) {
0f3caba2
JW
620 if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
621 continue;
3560e249 622 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
0f3caba2
JW
623 break;
624
c6785b6b 625 region = alloc_bootmem_bdata(bdata, size, align, goal, limit);
0f3caba2
JW
626 if (region)
627 return region;
628 }
629
c12ab504
JW
630 return NULL;
631}
632
633static void * __init ___alloc_bootmem_nopanic(unsigned long size,
634 unsigned long align,
635 unsigned long goal,
636 unsigned long limit)
637{
638 void *ptr;
639
640restart:
641 ptr = alloc_bootmem_core(size, align, goal, limit);
642 if (ptr)
643 return ptr;
5f2809e6
JW
644 if (goal) {
645 goal = 0;
0f3caba2 646 goto restart;
5f2809e6 647 }
2e5237da 648
5f2809e6 649 return NULL;
1da177e4
LT
650}
651
a66fd7da
JW
652/**
653 * __alloc_bootmem_nopanic - allocate boot memory without panicking
654 * @size: size of the request in bytes
655 * @align: alignment of the region
656 * @goal: preferred starting address of the region
657 *
658 * The goal is dropped if it can not be satisfied and the allocation will
659 * fall back to memory below @goal.
660 *
661 * Allocation may happen on any node in the system.
662 *
663 * Returns NULL on failure.
664 */
bb0923a6 665void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
0f3caba2 666 unsigned long goal)
1da177e4 667{
08677214
YL
668 unsigned long limit = 0;
669
08677214 670 return ___alloc_bootmem_nopanic(size, align, goal, limit);
0f3caba2 671}
1da177e4 672
0f3caba2
JW
673static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
674 unsigned long goal, unsigned long limit)
675{
676 void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
677
678 if (mem)
679 return mem;
680 /*
681 * Whoops, we cannot satisfy the allocation request.
682 */
683 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
684 panic("Out of memory");
a8062231
AK
685 return NULL;
686}
1da177e4 687
a66fd7da
JW
688/**
689 * __alloc_bootmem - allocate boot memory
690 * @size: size of the request in bytes
691 * @align: alignment of the region
692 * @goal: preferred starting address of the region
693 *
694 * The goal is dropped if it can not be satisfied and the allocation will
695 * fall back to memory below @goal.
696 *
697 * Allocation may happen on any node in the system.
698 *
699 * The function panics if the request can not be satisfied.
700 */
bb0923a6
FBH
701void * __init __alloc_bootmem(unsigned long size, unsigned long align,
702 unsigned long goal)
a8062231 703{
08677214
YL
704 unsigned long limit = 0;
705
08677214 706 return ___alloc_bootmem(size, align, goal, limit);
1da177e4
LT
707}
708
99ab7b19 709void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
4cc278b7
JW
710 unsigned long size, unsigned long align,
711 unsigned long goal, unsigned long limit)
712{
713 void *ptr;
714
3f7dfe24
JK
715 if (WARN_ON_ONCE(slab_is_available()))
716 return kzalloc(size, GFP_NOWAIT);
ab381843 717again:
d0c4f570 718
c8f4a2d0
YL
719 /* do not panic in alloc_bootmem_bdata() */
720 if (limit && goal + size > limit)
721 limit = 0;
722
e9079911 723 ptr = alloc_bootmem_bdata(pgdat->bdata, size, align, goal, limit);
4cc278b7
JW
724 if (ptr)
725 return ptr;
726
ab381843
JW
727 ptr = alloc_bootmem_core(size, align, goal, limit);
728 if (ptr)
729 return ptr;
730
731 if (goal) {
732 goal = 0;
733 goto again;
734 }
735
421456ed
JW
736 return NULL;
737}
738
739void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
740 unsigned long align, unsigned long goal)
741{
742 if (WARN_ON_ONCE(slab_is_available()))
743 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
744
e9079911 745 return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
421456ed
JW
746}
747
e9079911 748void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
421456ed
JW
749 unsigned long align, unsigned long goal,
750 unsigned long limit)
751{
752 void *ptr;
753
e9079911 754 ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
421456ed
JW
755 if (ptr)
756 return ptr;
757
ab381843
JW
758 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
759 panic("Out of memory");
760 return NULL;
4cc278b7
JW
761}
762
a66fd7da
JW
763/**
764 * __alloc_bootmem_node - allocate boot memory from a specific node
765 * @pgdat: node to allocate from
766 * @size: size of the request in bytes
767 * @align: alignment of the region
768 * @goal: preferred starting address of the region
769 *
770 * The goal is dropped if it can not be satisfied and the allocation will
771 * fall back to memory below @goal.
772 *
773 * Allocation may fall back to any node in the system if the specified node
774 * can not hold the requested memory.
775 *
776 * The function panics if the request can not be satisfied.
777 */
bb0923a6
FBH
778void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
779 unsigned long align, unsigned long goal)
1da177e4 780{
c91c4773
PE
781 if (WARN_ON_ONCE(slab_is_available()))
782 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
783
e9079911 784 return ___alloc_bootmem_node(pgdat, size, align, goal, 0);
08677214
YL
785}
786
787void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
788 unsigned long align, unsigned long goal)
789{
790#ifdef MAX_DMA32_PFN
791 unsigned long end_pfn;
792
793 if (WARN_ON_ONCE(slab_is_available()))
794 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
795
796 /* update goal according ...MAX_DMA32_PFN */
83285c72 797 end_pfn = pgdat_end_pfn(pgdat);
08677214
YL
798
799 if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
800 (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
801 void *ptr;
802 unsigned long new_goal;
803
804 new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
c6785b6b 805 ptr = alloc_bootmem_bdata(pgdat->bdata, size, align,
08677214 806 new_goal, 0);
08677214
YL
807 if (ptr)
808 return ptr;
809 }
810#endif
811
812 return __alloc_bootmem_node(pgdat, size, align, goal);
813
1da177e4
LT
814}
815
dfd54cbc
HC
816#ifndef ARCH_LOW_ADDRESS_LIMIT
817#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
818#endif
008857c1 819
a66fd7da
JW
820/**
821 * __alloc_bootmem_low - allocate low boot memory
822 * @size: size of the request in bytes
823 * @align: alignment of the region
824 * @goal: preferred starting address of the region
825 *
826 * The goal is dropped if it can not be satisfied and the allocation will
827 * fall back to memory below @goal.
828 *
829 * Allocation may happen on any node in the system.
830 *
831 * The function panics if the request can not be satisfied.
832 */
bb0923a6
FBH
833void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
834 unsigned long goal)
008857c1 835{
0f3caba2 836 return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
008857c1
RT
837}
838
38fa4175
YL
839void * __init __alloc_bootmem_low_nopanic(unsigned long size,
840 unsigned long align,
841 unsigned long goal)
842{
843 return ___alloc_bootmem_nopanic(size, align, goal,
844 ARCH_LOW_ADDRESS_LIMIT);
845}
846
a66fd7da
JW
847/**
848 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
849 * @pgdat: node to allocate from
850 * @size: size of the request in bytes
851 * @align: alignment of the region
852 * @goal: preferred starting address of the region
853 *
854 * The goal is dropped if it can not be satisfied and the allocation will
855 * fall back to memory below @goal.
856 *
857 * Allocation may fall back to any node in the system if the specified node
858 * can not hold the requested memory.
859 *
860 * The function panics if the request can not be satisfied.
861 */
008857c1
RT
862void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
863 unsigned long align, unsigned long goal)
864{
c91c4773
PE
865 if (WARN_ON_ONCE(slab_is_available()))
866 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
867
e9079911
JW
868 return ___alloc_bootmem_node(pgdat, size, align,
869 goal, ARCH_LOW_ADDRESS_LIMIT);
008857c1 870}