]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - mm/bootmem.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[mirror_ubuntu-artful-kernel.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>
1da177e4 15#include <linux/module.h>
ec3a354b 16#include <linux/kmemleak.h>
08677214 17#include <linux/range.h>
e786e86a
FBH
18
19#include <asm/bug.h>
1da177e4 20#include <asm/io.h>
dfd54cbc 21#include <asm/processor.h>
e786e86a 22
1da177e4
LT
23#include "internal.h"
24
1da177e4
LT
25unsigned long max_low_pfn;
26unsigned long min_low_pfn;
27unsigned long max_pfn;
28
92aa63a5
VG
29#ifdef CONFIG_CRASH_DUMP
30/*
31 * If we have booted due to a crash, max_pfn will be a very low value. We need
32 * to know the amount of memory that the previous kernel used.
33 */
34unsigned long saved_max_pfn;
35#endif
36
08677214 37#ifndef CONFIG_NO_BOOTMEM
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{
df049a5f 60 unsigned long bytes = (pages + 7) / 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{
636cc40c 81 struct list_head *iter;
f71bf0ca 82
636cc40c
JW
83 list_for_each(iter, &bdata_list) {
84 bootmem_data_t *ent;
85
86 ent = list_entry(iter, bootmem_data_t, list);
3560e249 87 if (bdata->node_min_pfn < ent->node_min_pfn)
636cc40c 88 break;
679bc9fb 89 }
636cc40c 90 list_add_tail(&bdata->list, iter);
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}
08677214 148#endif
9f993ac3
FT
149/*
150 * free_bootmem_late - free bootmem pages directly to page allocator
151 * @addr: starting address of the range
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 */
158void __init free_bootmem_late(unsigned long addr, unsigned long size)
159{
160 unsigned long cursor, end;
161
162 kmemleak_free_part(__va(addr), size);
163
164 cursor = PFN_UP(addr);
165 end = PFN_DOWN(addr + size);
166
167 for (; cursor < end; cursor++) {
168 __free_pages_bootmem(pfn_to_page(cursor), 0);
169 totalram_pages++;
170 }
171}
172
08677214
YL
173#ifdef CONFIG_NO_BOOTMEM
174static void __init __free_pages_memory(unsigned long start, unsigned long end)
175{
176 int i;
177 unsigned long start_aligned, end_aligned;
178 int order = ilog2(BITS_PER_LONG);
179
180 start_aligned = (start + (BITS_PER_LONG - 1)) & ~(BITS_PER_LONG - 1);
181 end_aligned = end & ~(BITS_PER_LONG - 1);
182
183 if (end_aligned <= start_aligned) {
08677214
YL
184 for (i = start; i < end; i++)
185 __free_pages_bootmem(pfn_to_page(i), 0);
186
187 return;
188 }
189
08677214
YL
190 for (i = start; i < start_aligned; i++)
191 __free_pages_bootmem(pfn_to_page(i), 0);
192
193 for (i = start_aligned; i < end_aligned; i += BITS_PER_LONG)
194 __free_pages_bootmem(pfn_to_page(i), order);
195
196 for (i = end_aligned; i < end; i++)
197 __free_pages_bootmem(pfn_to_page(i), 0);
198}
199
200unsigned long __init free_all_memory_core_early(int nodeid)
201{
202 int i;
203 u64 start, end;
204 unsigned long count = 0;
205 struct range *range = NULL;
206 int nr_range;
207
208 nr_range = get_free_all_memory_range(&range, nodeid);
209
210 for (i = 0; i < nr_range; i++) {
211 start = range[i].start;
212 end = range[i].end;
213 count += end - start;
214 __free_pages_memory(start, end);
215 }
216
217 return count;
218}
219#else
223e8dc9
JW
220static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
221{
41546c17 222 int aligned;
223e8dc9 223 struct page *page;
41546c17
JW
224 unsigned long start, end, pages, count = 0;
225
226 if (!bdata->node_bootmem_map)
227 return 0;
228
3560e249 229 start = bdata->node_min_pfn;
41546c17
JW
230 end = bdata->node_low_pfn;
231
223e8dc9 232 /*
41546c17
JW
233 * If the start is aligned to the machines wordsize, we might
234 * be able to free pages in bulks of that order.
223e8dc9 235 */
41546c17 236 aligned = !(start & (BITS_PER_LONG - 1));
223e8dc9 237
41546c17
JW
238 bdebug("nid=%td start=%lx end=%lx aligned=%d\n",
239 bdata - bootmem_node_data, start, end, aligned);
223e8dc9 240
41546c17
JW
241 while (start < end) {
242 unsigned long *map, idx, vec;
223e8dc9 243
41546c17 244 map = bdata->node_bootmem_map;
3560e249 245 idx = start - bdata->node_min_pfn;
41546c17
JW
246 vec = ~map[idx / BITS_PER_LONG];
247
248 if (aligned && vec == ~0UL && start + BITS_PER_LONG < end) {
249 int order = ilog2(BITS_PER_LONG);
250
251 __free_pages_bootmem(pfn_to_page(start), order);
223e8dc9 252 count += BITS_PER_LONG;
41546c17
JW
253 } else {
254 unsigned long off = 0;
255
256 while (vec && off < BITS_PER_LONG) {
257 if (vec & 1) {
258 page = pfn_to_page(start + off);
223e8dc9 259 __free_pages_bootmem(page, 0);
41546c17 260 count++;
223e8dc9 261 }
41546c17
JW
262 vec >>= 1;
263 off++;
223e8dc9 264 }
223e8dc9 265 }
41546c17 266 start += BITS_PER_LONG;
223e8dc9
JW
267 }
268
223e8dc9 269 page = virt_to_page(bdata->node_bootmem_map);
3560e249 270 pages = bdata->node_low_pfn - bdata->node_min_pfn;
41546c17
JW
271 pages = bootmem_bootmap_pages(pages);
272 count += pages;
273 while (pages--)
274 __free_pages_bootmem(page++, 0);
223e8dc9 275
2e5237da
JW
276 bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
277
223e8dc9
JW
278 return count;
279}
08677214 280#endif
223e8dc9 281
a66fd7da
JW
282/**
283 * free_all_bootmem_node - release a node's free pages to the buddy allocator
284 * @pgdat: node to be released
285 *
286 * Returns the number of pages actually released.
287 */
223e8dc9
JW
288unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
289{
290 register_page_bootmem_info_node(pgdat);
08677214
YL
291#ifdef CONFIG_NO_BOOTMEM
292 /* free_all_memory_core_early(MAX_NUMNODES) will be called later */
293 return 0;
294#else
223e8dc9 295 return free_all_bootmem_core(pgdat->bdata);
08677214 296#endif
223e8dc9
JW
297}
298
a66fd7da
JW
299/**
300 * free_all_bootmem - release free pages to the buddy allocator
301 *
302 * Returns the number of pages actually released.
303 */
223e8dc9
JW
304unsigned long __init free_all_bootmem(void)
305{
08677214
YL
306#ifdef CONFIG_NO_BOOTMEM
307 return free_all_memory_core_early(NODE_DATA(0)->node_id);
308#else
223e8dc9 309 return free_all_bootmem_core(NODE_DATA(0)->bdata);
08677214 310#endif
223e8dc9
JW
311}
312
08677214 313#ifndef CONFIG_NO_BOOTMEM
d747fa4b
JW
314static void __init __free(bootmem_data_t *bdata,
315 unsigned long sidx, unsigned long eidx)
316{
317 unsigned long idx;
318
319 bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
3560e249
JW
320 sidx + bdata->node_min_pfn,
321 eidx + bdata->node_min_pfn);
d747fa4b 322
e2bf3cae
JW
323 if (bdata->hint_idx > sidx)
324 bdata->hint_idx = sidx;
325
d747fa4b
JW
326 for (idx = sidx; idx < eidx; idx++)
327 if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
328 BUG();
329}
330
331static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
332 unsigned long eidx, int flags)
333{
334 unsigned long idx;
335 int exclusive = flags & BOOTMEM_EXCLUSIVE;
336
337 bdebug("nid=%td start=%lx end=%lx flags=%x\n",
338 bdata - bootmem_node_data,
3560e249
JW
339 sidx + bdata->node_min_pfn,
340 eidx + bdata->node_min_pfn,
d747fa4b
JW
341 flags);
342
343 for (idx = sidx; idx < eidx; idx++)
344 if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
345 if (exclusive) {
346 __free(bdata, sidx, idx);
347 return -EBUSY;
348 }
349 bdebug("silent double reserve of PFN %lx\n",
3560e249 350 idx + bdata->node_min_pfn);
d747fa4b
JW
351 }
352 return 0;
353}
354
e2bf3cae
JW
355static int __init mark_bootmem_node(bootmem_data_t *bdata,
356 unsigned long start, unsigned long end,
357 int reserve, int flags)
223e8dc9
JW
358{
359 unsigned long sidx, eidx;
223e8dc9 360
e2bf3cae
JW
361 bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
362 bdata - bootmem_node_data, start, end, reserve, flags);
223e8dc9 363
3560e249 364 BUG_ON(start < bdata->node_min_pfn);
e2bf3cae 365 BUG_ON(end > bdata->node_low_pfn);
223e8dc9 366
3560e249
JW
367 sidx = start - bdata->node_min_pfn;
368 eidx = end - bdata->node_min_pfn;
223e8dc9 369
e2bf3cae
JW
370 if (reserve)
371 return __reserve(bdata, sidx, eidx, flags);
223e8dc9 372 else
e2bf3cae
JW
373 __free(bdata, sidx, eidx);
374 return 0;
375}
376
377static int __init mark_bootmem(unsigned long start, unsigned long end,
378 int reserve, int flags)
379{
380 unsigned long pos;
381 bootmem_data_t *bdata;
382
383 pos = start;
384 list_for_each_entry(bdata, &bdata_list, list) {
385 int err;
386 unsigned long max;
387
3560e249
JW
388 if (pos < bdata->node_min_pfn ||
389 pos >= bdata->node_low_pfn) {
e2bf3cae
JW
390 BUG_ON(pos != start);
391 continue;
392 }
393
394 max = min(bdata->node_low_pfn, end);
223e8dc9 395
e2bf3cae
JW
396 err = mark_bootmem_node(bdata, pos, max, reserve, flags);
397 if (reserve && err) {
398 mark_bootmem(start, pos, 0, 0);
399 return err;
400 }
223e8dc9 401
e2bf3cae
JW
402 if (max == end)
403 return 0;
404 pos = bdata->node_low_pfn;
405 }
406 BUG();
223e8dc9 407}
08677214 408#endif
223e8dc9 409
a66fd7da
JW
410/**
411 * free_bootmem_node - mark a page range as usable
412 * @pgdat: node the range resides on
413 * @physaddr: starting address of the range
414 * @size: size of the range in bytes
415 *
416 * Partial pages will be considered reserved and left as they are.
417 *
e2bf3cae 418 * The range must reside completely on the specified node.
a66fd7da 419 */
223e8dc9
JW
420void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
421 unsigned long size)
422{
08677214
YL
423#ifdef CONFIG_NO_BOOTMEM
424 free_early(physaddr, physaddr + size);
08677214 425#else
e2bf3cae
JW
426 unsigned long start, end;
427
ec3a354b
CM
428 kmemleak_free_part(__va(physaddr), size);
429
e2bf3cae
JW
430 start = PFN_UP(physaddr);
431 end = PFN_DOWN(physaddr + size);
432
433 mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
08677214 434#endif
223e8dc9
JW
435}
436
a66fd7da
JW
437/**
438 * free_bootmem - mark a page range as usable
439 * @addr: starting address of the range
440 * @size: size of the range in bytes
441 *
442 * Partial pages will be considered reserved and left as they are.
443 *
e2bf3cae 444 * The range must be contiguous but may span node boundaries.
a66fd7da 445 */
223e8dc9
JW
446void __init free_bootmem(unsigned long addr, unsigned long size)
447{
08677214
YL
448#ifdef CONFIG_NO_BOOTMEM
449 free_early(addr, addr + size);
08677214 450#else
e2bf3cae 451 unsigned long start, end;
a5645a61 452
ec3a354b
CM
453 kmemleak_free_part(__va(addr), size);
454
e2bf3cae
JW
455 start = PFN_UP(addr);
456 end = PFN_DOWN(addr + size);
1da177e4 457
e2bf3cae 458 mark_bootmem(start, end, 0, 0);
08677214 459#endif
1da177e4
LT
460}
461
a66fd7da
JW
462/**
463 * reserve_bootmem_node - mark a page range as reserved
464 * @pgdat: node the range resides on
465 * @physaddr: starting address of the range
466 * @size: size of the range in bytes
467 * @flags: reservation flags (see linux/bootmem.h)
468 *
469 * Partial pages will be reserved.
470 *
e2bf3cae 471 * The range must reside completely on the specified node.
a66fd7da 472 */
223e8dc9
JW
473int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
474 unsigned long size, int flags)
1da177e4 475{
08677214
YL
476#ifdef CONFIG_NO_BOOTMEM
477 panic("no bootmem");
478 return 0;
479#else
e2bf3cae 480 unsigned long start, end;
1da177e4 481
e2bf3cae
JW
482 start = PFN_DOWN(physaddr);
483 end = PFN_UP(physaddr + size);
484
485 return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
08677214 486#endif
223e8dc9 487}
5a982cbc 488
a66fd7da
JW
489/**
490 * reserve_bootmem - mark a page range as usable
491 * @addr: starting address of the range
492 * @size: size of the range in bytes
493 * @flags: reservation flags (see linux/bootmem.h)
494 *
495 * Partial pages will be reserved.
496 *
e2bf3cae 497 * The range must be contiguous but may span node boundaries.
a66fd7da 498 */
223e8dc9
JW
499int __init reserve_bootmem(unsigned long addr, unsigned long size,
500 int flags)
501{
08677214
YL
502#ifdef CONFIG_NO_BOOTMEM
503 panic("no bootmem");
504 return 0;
505#else
e2bf3cae 506 unsigned long start, end;
1da177e4 507
e2bf3cae
JW
508 start = PFN_DOWN(addr);
509 end = PFN_UP(addr + size);
223e8dc9 510
e2bf3cae 511 return mark_bootmem(start, end, 1, flags);
08677214 512#endif
1da177e4
LT
513}
514
08677214 515#ifndef CONFIG_NO_BOOTMEM
8aa043d7
JB
516static unsigned long __init align_idx(struct bootmem_data *bdata,
517 unsigned long idx, unsigned long step)
481ebd0d
JW
518{
519 unsigned long base = bdata->node_min_pfn;
520
521 /*
522 * Align the index with respect to the node start so that the
523 * combination of both satisfies the requested alignment.
524 */
525
526 return ALIGN(base + idx, step) - base;
527}
528
8aa043d7
JB
529static unsigned long __init align_off(struct bootmem_data *bdata,
530 unsigned long off, unsigned long align)
481ebd0d
JW
531{
532 unsigned long base = PFN_PHYS(bdata->node_min_pfn);
533
534 /* Same as align_idx for byte offsets */
535
536 return ALIGN(base + off, align) - base;
537}
538
d0c4f570
TH
539static void * __init alloc_bootmem_core(struct bootmem_data *bdata,
540 unsigned long size, unsigned long align,
541 unsigned long goal, unsigned long limit)
1da177e4 542{
0f3caba2 543 unsigned long fallback = 0;
5f2809e6
JW
544 unsigned long min, max, start, sidx, midx, step;
545
594fe1a0
JW
546 bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
547 bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
548 align, goal, limit);
549
5f2809e6
JW
550 BUG_ON(!size);
551 BUG_ON(align & (align - 1));
552 BUG_ON(limit && goal + size > limit);
1da177e4 553
7c309a64
CK
554 if (!bdata->node_bootmem_map)
555 return NULL;
556
3560e249 557 min = bdata->node_min_pfn;
5f2809e6 558 max = bdata->node_low_pfn;
9a2dc04c 559
5f2809e6
JW
560 goal >>= PAGE_SHIFT;
561 limit >>= PAGE_SHIFT;
562
563 if (limit && max > limit)
564 max = limit;
565 if (max <= min)
9a2dc04c
YL
566 return NULL;
567
5f2809e6 568 step = max(align >> PAGE_SHIFT, 1UL);
281dd25c 569
5f2809e6
JW
570 if (goal && min < goal && goal < max)
571 start = ALIGN(goal, step);
572 else
573 start = ALIGN(min, step);
1da177e4 574
481ebd0d 575 sidx = start - bdata->node_min_pfn;
3560e249 576 midx = max - bdata->node_min_pfn;
1da177e4 577
5f2809e6 578 if (bdata->hint_idx > sidx) {
0f3caba2
JW
579 /*
580 * Handle the valid case of sidx being zero and still
581 * catch the fallback below.
582 */
583 fallback = sidx + 1;
481ebd0d 584 sidx = align_idx(bdata, bdata->hint_idx, step);
5f2809e6 585 }
1da177e4 586
5f2809e6
JW
587 while (1) {
588 int merge;
589 void *region;
590 unsigned long eidx, i, start_off, end_off;
591find_block:
592 sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
481ebd0d 593 sidx = align_idx(bdata, sidx, step);
5f2809e6 594 eidx = sidx + PFN_UP(size);
ad09315c 595
5f2809e6 596 if (sidx >= midx || eidx > midx)
66d43e98 597 break;
1da177e4 598
5f2809e6
JW
599 for (i = sidx; i < eidx; i++)
600 if (test_bit(i, bdata->node_bootmem_map)) {
481ebd0d 601 sidx = align_idx(bdata, i, step);
5f2809e6
JW
602 if (sidx == i)
603 sidx += step;
604 goto find_block;
605 }
1da177e4 606
627240aa 607 if (bdata->last_end_off & (PAGE_SIZE - 1) &&
5f2809e6 608 PFN_DOWN(bdata->last_end_off) + 1 == sidx)
481ebd0d 609 start_off = align_off(bdata, bdata->last_end_off, align);
5f2809e6
JW
610 else
611 start_off = PFN_PHYS(sidx);
612
613 merge = PFN_DOWN(start_off) < sidx;
614 end_off = start_off + size;
615
616 bdata->last_end_off = end_off;
617 bdata->hint_idx = PFN_UP(end_off);
618
619 /*
620 * Reserve the area now:
621 */
d747fa4b
JW
622 if (__reserve(bdata, PFN_DOWN(start_off) + merge,
623 PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
624 BUG();
5f2809e6 625
3560e249
JW
626 region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
627 start_off);
5f2809e6 628 memset(region, 0, size);
008139d9
CM
629 /*
630 * The min_count is set to 0 so that bootmem allocated blocks
631 * are never reported as leaks.
632 */
633 kmemleak_alloc(region, size, 0, 0);
5f2809e6 634 return region;
1da177e4
LT
635 }
636
0f3caba2 637 if (fallback) {
481ebd0d 638 sidx = align_idx(bdata, fallback - 1, step);
0f3caba2
JW
639 fallback = 0;
640 goto find_block;
641 }
642
643 return NULL;
644}
645
d0c4f570
TH
646static void * __init alloc_arch_preferred_bootmem(bootmem_data_t *bdata,
647 unsigned long size, unsigned long align,
648 unsigned long goal, unsigned long limit)
649{
441c7e0a
PE
650 if (WARN_ON_ONCE(slab_is_available()))
651 return kzalloc(size, GFP_NOWAIT);
652
d0c4f570 653#ifdef CONFIG_HAVE_ARCH_BOOTMEM
433f13a7
JP
654 {
655 bootmem_data_t *p_bdata;
656
657 p_bdata = bootmem_arch_preferred_node(bdata, size, align,
658 goal, limit);
659 if (p_bdata)
660 return alloc_bootmem_core(p_bdata, size, align,
661 goal, limit);
662 }
d0c4f570
TH
663#endif
664 return NULL;
665}
08677214 666#endif
d0c4f570 667
0f3caba2
JW
668static void * __init ___alloc_bootmem_nopanic(unsigned long size,
669 unsigned long align,
670 unsigned long goal,
671 unsigned long limit)
672{
08677214
YL
673#ifdef CONFIG_NO_BOOTMEM
674 void *ptr;
675
676 if (WARN_ON_ONCE(slab_is_available()))
677 return kzalloc(size, GFP_NOWAIT);
678
679restart:
680
681 ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, goal, limit);
682
683 if (ptr)
684 return ptr;
685
686 if (goal != 0) {
687 goal = 0;
688 goto restart;
689 }
690
691 return NULL;
692#else
0f3caba2 693 bootmem_data_t *bdata;
d0c4f570 694 void *region;
0f3caba2
JW
695
696restart:
d0c4f570
TH
697 region = alloc_arch_preferred_bootmem(NULL, size, align, goal, limit);
698 if (region)
699 return region;
0f3caba2 700
d0c4f570 701 list_for_each_entry(bdata, &bdata_list, list) {
0f3caba2
JW
702 if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
703 continue;
3560e249 704 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
0f3caba2
JW
705 break;
706
707 region = alloc_bootmem_core(bdata, size, align, goal, limit);
708 if (region)
709 return region;
710 }
711
5f2809e6
JW
712 if (goal) {
713 goal = 0;
0f3caba2 714 goto restart;
5f2809e6 715 }
2e5237da 716
5f2809e6 717 return NULL;
08677214 718#endif
1da177e4
LT
719}
720
a66fd7da
JW
721/**
722 * __alloc_bootmem_nopanic - allocate boot memory without panicking
723 * @size: size of the request in bytes
724 * @align: alignment of the region
725 * @goal: preferred starting address of the region
726 *
727 * The goal is dropped if it can not be satisfied and the allocation will
728 * fall back to memory below @goal.
729 *
730 * Allocation may happen on any node in the system.
731 *
732 * Returns NULL on failure.
733 */
bb0923a6 734void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
0f3caba2 735 unsigned long goal)
1da177e4 736{
08677214
YL
737 unsigned long limit = 0;
738
739#ifdef CONFIG_NO_BOOTMEM
740 limit = -1UL;
741#endif
742
743 return ___alloc_bootmem_nopanic(size, align, goal, limit);
0f3caba2 744}
1da177e4 745
0f3caba2
JW
746static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
747 unsigned long goal, unsigned long limit)
748{
749 void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
750
751 if (mem)
752 return mem;
753 /*
754 * Whoops, we cannot satisfy the allocation request.
755 */
756 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
757 panic("Out of memory");
a8062231
AK
758 return NULL;
759}
1da177e4 760
a66fd7da
JW
761/**
762 * __alloc_bootmem - allocate boot memory
763 * @size: size of the request in bytes
764 * @align: alignment of the region
765 * @goal: preferred starting address of the region
766 *
767 * The goal is dropped if it can not be satisfied and the allocation will
768 * fall back to memory below @goal.
769 *
770 * Allocation may happen on any node in the system.
771 *
772 * The function panics if the request can not be satisfied.
773 */
bb0923a6
FBH
774void * __init __alloc_bootmem(unsigned long size, unsigned long align,
775 unsigned long goal)
a8062231 776{
08677214
YL
777 unsigned long limit = 0;
778
779#ifdef CONFIG_NO_BOOTMEM
780 limit = -1UL;
781#endif
782
783 return ___alloc_bootmem(size, align, goal, limit);
1da177e4
LT
784}
785
08677214 786#ifndef CONFIG_NO_BOOTMEM
4cc278b7
JW
787static void * __init ___alloc_bootmem_node(bootmem_data_t *bdata,
788 unsigned long size, unsigned long align,
789 unsigned long goal, unsigned long limit)
790{
791 void *ptr;
792
d0c4f570
TH
793 ptr = alloc_arch_preferred_bootmem(bdata, size, align, goal, limit);
794 if (ptr)
795 return ptr;
796
4cc278b7
JW
797 ptr = alloc_bootmem_core(bdata, size, align, goal, limit);
798 if (ptr)
799 return ptr;
800
801 return ___alloc_bootmem(size, align, goal, limit);
802}
08677214 803#endif
4cc278b7 804
a66fd7da
JW
805/**
806 * __alloc_bootmem_node - allocate boot memory from a specific node
807 * @pgdat: node to allocate from
808 * @size: size of the request in bytes
809 * @align: alignment of the region
810 * @goal: preferred starting address of the region
811 *
812 * The goal is dropped if it can not be satisfied and the allocation will
813 * fall back to memory below @goal.
814 *
815 * Allocation may fall back to any node in the system if the specified node
816 * can not hold the requested memory.
817 *
818 * The function panics if the request can not be satisfied.
819 */
bb0923a6
FBH
820void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
821 unsigned long align, unsigned long goal)
1da177e4 822{
c91c4773
PE
823 if (WARN_ON_ONCE(slab_is_available()))
824 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
825
08677214
YL
826#ifdef CONFIG_NO_BOOTMEM
827 return __alloc_memory_core_early(pgdat->node_id, size, align,
828 goal, -1ULL);
829#else
4cc278b7 830 return ___alloc_bootmem_node(pgdat->bdata, size, align, goal, 0);
08677214
YL
831#endif
832}
833
834void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
835 unsigned long align, unsigned long goal)
836{
837#ifdef MAX_DMA32_PFN
838 unsigned long end_pfn;
839
840 if (WARN_ON_ONCE(slab_is_available()))
841 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
842
843 /* update goal according ...MAX_DMA32_PFN */
844 end_pfn = pgdat->node_start_pfn + pgdat->node_spanned_pages;
845
846 if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
847 (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
848 void *ptr;
849 unsigned long new_goal;
850
851 new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
852#ifdef CONFIG_NO_BOOTMEM
853 ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
854 new_goal, -1ULL);
855#else
856 ptr = alloc_bootmem_core(pgdat->bdata, size, align,
857 new_goal, 0);
858#endif
859 if (ptr)
860 return ptr;
861 }
862#endif
863
864 return __alloc_bootmem_node(pgdat, size, align, goal);
865
1da177e4
LT
866}
867
e70260aa 868#ifdef CONFIG_SPARSEMEM
a66fd7da
JW
869/**
870 * alloc_bootmem_section - allocate boot memory from a specific section
871 * @size: size of the request in bytes
872 * @section_nr: sparse map section to allocate from
873 *
874 * Return NULL on failure.
875 */
e70260aa
YG
876void * __init alloc_bootmem_section(unsigned long size,
877 unsigned long section_nr)
878{
08677214
YL
879#ifdef CONFIG_NO_BOOTMEM
880 unsigned long pfn, goal, limit;
881
882 pfn = section_nr_to_pfn(section_nr);
883 goal = pfn << PAGE_SHIFT;
884 limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
885
886 return __alloc_memory_core_early(early_pfn_to_nid(pfn), size,
887 SMP_CACHE_BYTES, goal, limit);
888#else
75a56cfe
JW
889 bootmem_data_t *bdata;
890 unsigned long pfn, goal, limit;
e70260aa
YG
891
892 pfn = section_nr_to_pfn(section_nr);
75a56cfe
JW
893 goal = pfn << PAGE_SHIFT;
894 limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
895 bdata = &bootmem_node_data[early_pfn_to_nid(pfn)];
e70260aa 896
75a56cfe 897 return alloc_bootmem_core(bdata, size, SMP_CACHE_BYTES, goal, limit);
08677214 898#endif
e70260aa
YG
899}
900#endif
901
b54bbf7b
AK
902void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
903 unsigned long align, unsigned long goal)
904{
905 void *ptr;
906
c91c4773
PE
907 if (WARN_ON_ONCE(slab_is_available()))
908 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
909
08677214
YL
910#ifdef CONFIG_NO_BOOTMEM
911 ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
912 goal, -1ULL);
913#else
d0c4f570
TH
914 ptr = alloc_arch_preferred_bootmem(pgdat->bdata, size, align, goal, 0);
915 if (ptr)
916 return ptr;
917
b54bbf7b 918 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
08677214 919#endif
b54bbf7b
AK
920 if (ptr)
921 return ptr;
922
923 return __alloc_bootmem_nopanic(size, align, goal);
924}
925
dfd54cbc
HC
926#ifndef ARCH_LOW_ADDRESS_LIMIT
927#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
928#endif
008857c1 929
a66fd7da
JW
930/**
931 * __alloc_bootmem_low - allocate low boot memory
932 * @size: size of the request in bytes
933 * @align: alignment of the region
934 * @goal: preferred starting address of the region
935 *
936 * The goal is dropped if it can not be satisfied and the allocation will
937 * fall back to memory below @goal.
938 *
939 * Allocation may happen on any node in the system.
940 *
941 * The function panics if the request can not be satisfied.
942 */
bb0923a6
FBH
943void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
944 unsigned long goal)
008857c1 945{
0f3caba2 946 return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
008857c1
RT
947}
948
a66fd7da
JW
949/**
950 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
951 * @pgdat: node to allocate from
952 * @size: size of the request in bytes
953 * @align: alignment of the region
954 * @goal: preferred starting address of the region
955 *
956 * The goal is dropped if it can not be satisfied and the allocation will
957 * fall back to memory below @goal.
958 *
959 * Allocation may fall back to any node in the system if the specified node
960 * can not hold the requested memory.
961 *
962 * The function panics if the request can not be satisfied.
963 */
008857c1
RT
964void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
965 unsigned long align, unsigned long goal)
966{
c91c4773
PE
967 if (WARN_ON_ONCE(slab_is_available()))
968 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
969
08677214
YL
970#ifdef CONFIG_NO_BOOTMEM
971 return __alloc_memory_core_early(pgdat->node_id, size, align,
972 goal, ARCH_LOW_ADDRESS_LIMIT);
973#else
4cc278b7
JW
974 return ___alloc_bootmem_node(pgdat->bdata, size, align,
975 goal, ARCH_LOW_ADDRESS_LIMIT);
08677214 976#endif
008857c1 977}