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