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