mm: remove __GFP_NOFAIL is deprecated comment
[linux-2.6-block.git] / mm / bootmem.c
1 /*
2  *  bootmem - A boot-time physical memory allocator and configurator
3  *
4  *  Copyright (C) 1999 Ingo Molnar
5  *                1999 Kanoj Sarcar, SGI
6  *                2008 Johannes Weiner
7  *
8  * Access to this subsystem has to be serialized externally (which is true
9  * for the boot process anyway).
10  */
11 #include <linux/init.h>
12 #include <linux/pfn.h>
13 #include <linux/slab.h>
14 #include <linux/bootmem.h>
15 #include <linux/export.h>
16 #include <linux/kmemleak.h>
17 #include <linux/range.h>
18 #include <linux/memblock.h>
19 #include <linux/bug.h>
20 #include <linux/io.h>
21
22 #include <asm/processor.h>
23
24 #include "internal.h"
25
26 #ifndef CONFIG_NEED_MULTIPLE_NODES
27 struct pglist_data __refdata contig_page_data = {
28         .bdata = &bootmem_node_data[0]
29 };
30 EXPORT_SYMBOL(contig_page_data);
31 #endif
32
33 unsigned long max_low_pfn;
34 unsigned long min_low_pfn;
35 unsigned long max_pfn;
36 unsigned long long max_possible_pfn;
37
38 bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata;
39
40 static struct list_head bdata_list __initdata = LIST_HEAD_INIT(bdata_list);
41
42 static int bootmem_debug;
43
44 static int __init bootmem_debug_setup(char *buf)
45 {
46         bootmem_debug = 1;
47         return 0;
48 }
49 early_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,             \
55                         __func__, ## args);             \
56 })
57
58 static unsigned long __init bootmap_bytes(unsigned long pages)
59 {
60         unsigned long bytes = DIV_ROUND_UP(pages, 8);
61
62         return ALIGN(bytes, sizeof(long));
63 }
64
65 /**
66  * bootmem_bootmap_pages - calculate bitmap size in pages
67  * @pages: number of pages the bitmap has to represent
68  */
69 unsigned long __init bootmem_bootmap_pages(unsigned long pages)
70 {
71         unsigned long bytes = bootmap_bytes(pages);
72
73         return PAGE_ALIGN(bytes) >> PAGE_SHIFT;
74 }
75
76 /*
77  * link bdata in order
78  */
79 static void __init link_bootmem(bootmem_data_t *bdata)
80 {
81         bootmem_data_t *ent;
82
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                 }
88         }
89
90         list_add_tail(&bdata->list, &bdata_list);
91 }
92
93 /*
94  * Called once to set up the allocator itself.
95  */
96 static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
97         unsigned long mapstart, unsigned long start, unsigned long end)
98 {
99         unsigned long mapsize;
100
101         mminit_validate_memmodel_limits(&start, &end);
102         bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
103         bdata->node_min_pfn = start;
104         bdata->node_low_pfn = end;
105         link_bootmem(bdata);
106
107         /*
108          * Initially all pages are reserved - setup_arch() has to
109          * register free RAM areas explicitly.
110          */
111         mapsize = bootmap_bytes(end - start);
112         memset(bdata->node_bootmem_map, 0xff, mapsize);
113
114         bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n",
115                 bdata - bootmem_node_data, start, mapstart, end, mapsize);
116
117         return mapsize;
118 }
119
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  */
129 unsigned 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
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  */
142 unsigned 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 }
148
149 /*
150  * free_bootmem_late - free bootmem pages directly to page allocator
151  * @addr: starting physical 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  */
158 void __init free_bootmem_late(unsigned long physaddr, unsigned long size)
159 {
160         unsigned long cursor, end;
161
162         kmemleak_free_part(__va(physaddr), size);
163
164         cursor = PFN_UP(physaddr);
165         end = PFN_DOWN(physaddr + size);
166
167         for (; cursor < end; cursor++) {
168                 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
169                 totalram_pages++;
170         }
171 }
172
173 static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
174 {
175         struct page *page;
176         unsigned long *map, start, end, pages, cur, count = 0;
177
178         if (!bdata->node_bootmem_map)
179                 return 0;
180
181         map = bdata->node_bootmem_map;
182         start = bdata->node_min_pfn;
183         end = bdata->node_low_pfn;
184
185         bdebug("nid=%td start=%lx end=%lx\n",
186                 bdata - bootmem_node_data, start, end);
187
188         while (start < end) {
189                 unsigned long idx, vec;
190                 unsigned shift;
191
192                 idx = start - bdata->node_min_pfn;
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                  */
198                 vec = ~map[idx / BITS_PER_LONG];
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                 }
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) {
212                         int order = ilog2(BITS_PER_LONG);
213
214                         __free_pages_bootmem(pfn_to_page(start), start, order);
215                         count += BITS_PER_LONG;
216                         start += BITS_PER_LONG;
217                 } else {
218                         cur = start;
219
220                         start = ALIGN(start + 1, BITS_PER_LONG);
221                         while (vec && cur != start) {
222                                 if (vec & 1) {
223                                         page = pfn_to_page(cur);
224                                         __free_pages_bootmem(page, cur, 0);
225                                         count++;
226                                 }
227                                 vec >>= 1;
228                                 ++cur;
229                         }
230                 }
231         }
232
233         cur = bdata->node_min_pfn;
234         page = virt_to_page(bdata->node_bootmem_map);
235         pages = bdata->node_low_pfn - bdata->node_min_pfn;
236         pages = bootmem_bootmap_pages(pages);
237         count += pages;
238         while (pages--)
239                 __free_pages_bootmem(page++, cur++, 0);
240         bdata->node_bootmem_map = NULL;
241
242         bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
243
244         return count;
245 }
246
247 static int reset_managed_pages_done __initdata;
248
249 void reset_node_managed_pages(pg_data_t *pgdat)
250 {
251         struct zone *z;
252
253         for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
254                 z->managed_pages = 0;
255 }
256
257 void __init reset_all_zones_managed_pages(void)
258 {
259         struct pglist_data *pgdat;
260
261         if (reset_managed_pages_done)
262                 return;
263
264         for_each_online_pgdat(pgdat)
265                 reset_node_managed_pages(pgdat);
266
267         reset_managed_pages_done = 1;
268 }
269
270 /**
271  * free_all_bootmem - release free pages to the buddy allocator
272  *
273  * Returns the number of pages actually released.
274  */
275 unsigned long __init free_all_bootmem(void)
276 {
277         unsigned long total_pages = 0;
278         bootmem_data_t *bdata;
279
280         reset_all_zones_managed_pages();
281
282         list_for_each_entry(bdata, &bdata_list, list)
283                 total_pages += free_all_bootmem_core(bdata);
284
285         totalram_pages += total_pages;
286
287         return total_pages;
288 }
289
290 static 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,
296                 sidx + bdata->node_min_pfn,
297                 eidx + bdata->node_min_pfn);
298
299         if (WARN_ON(bdata->node_bootmem_map == NULL))
300                 return;
301
302         if (bdata->hint_idx > sidx)
303                 bdata->hint_idx = sidx;
304
305         for (idx = sidx; idx < eidx; idx++)
306                 if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
307                         BUG();
308 }
309
310 static 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,
318                 sidx + bdata->node_min_pfn,
319                 eidx + bdata->node_min_pfn,
320                 flags);
321
322         if (WARN_ON(bdata->node_bootmem_map == NULL))
323                 return 0;
324
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",
332                                 idx + bdata->node_min_pfn);
333                 }
334         return 0;
335 }
336
337 static int __init mark_bootmem_node(bootmem_data_t *bdata,
338                                 unsigned long start, unsigned long end,
339                                 int reserve, int flags)
340 {
341         unsigned long sidx, eidx;
342
343         bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
344                 bdata - bootmem_node_data, start, end, reserve, flags);
345
346         BUG_ON(start < bdata->node_min_pfn);
347         BUG_ON(end > bdata->node_low_pfn);
348
349         sidx = start - bdata->node_min_pfn;
350         eidx = end - bdata->node_min_pfn;
351
352         if (reserve)
353                 return __reserve(bdata, sidx, eidx, flags);
354         else
355                 __free(bdata, sidx, eidx);
356         return 0;
357 }
358
359 static 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
370                 if (pos < bdata->node_min_pfn ||
371                     pos >= bdata->node_low_pfn) {
372                         BUG_ON(pos != start);
373                         continue;
374                 }
375
376                 max = min(bdata->node_low_pfn, end);
377
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                 }
383
384                 if (max == end)
385                         return 0;
386                 pos = bdata->node_low_pfn;
387         }
388         BUG();
389 }
390
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  *
399  * The range must reside completely on the specified node.
400  */
401 void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
402                               unsigned long size)
403 {
404         unsigned long start, end;
405
406         kmemleak_free_part(__va(physaddr), size);
407
408         start = PFN_UP(physaddr);
409         end = PFN_DOWN(physaddr + size);
410
411         mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
412 }
413
414 /**
415  * free_bootmem - mark a page range as usable
416  * @addr: starting physical address of the range
417  * @size: size of the range in bytes
418  *
419  * Partial pages will be considered reserved and left as they are.
420  *
421  * The range must be contiguous but may span node boundaries.
422  */
423 void __init free_bootmem(unsigned long physaddr, unsigned long size)
424 {
425         unsigned long start, end;
426
427         kmemleak_free_part(__va(physaddr), size);
428
429         start = PFN_UP(physaddr);
430         end = PFN_DOWN(physaddr + size);
431
432         mark_bootmem(start, end, 0, 0);
433 }
434
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  *
444  * The range must reside completely on the specified node.
445  */
446 int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
447                                  unsigned long size, int flags)
448 {
449         unsigned long start, end;
450
451         start = PFN_DOWN(physaddr);
452         end = PFN_UP(physaddr + size);
453
454         return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
455 }
456
457 /**
458  * reserve_bootmem - mark a page range as reserved
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  *
465  * The range must be contiguous but may span node boundaries.
466  */
467 int __init reserve_bootmem(unsigned long addr, unsigned long size,
468                             int flags)
469 {
470         unsigned long start, end;
471
472         start = PFN_DOWN(addr);
473         end = PFN_UP(addr + size);
474
475         return mark_bootmem(start, end, 1, flags);
476 }
477
478 static unsigned long __init align_idx(struct bootmem_data *bdata,
479                                       unsigned long idx, unsigned long step)
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
491 static unsigned long __init align_off(struct bootmem_data *bdata,
492                                       unsigned long off, unsigned long align)
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
501 static void * __init alloc_bootmem_bdata(struct bootmem_data *bdata,
502                                         unsigned long size, unsigned long align,
503                                         unsigned long goal, unsigned long limit)
504 {
505         unsigned long fallback = 0;
506         unsigned long min, max, start, sidx, midx, step;
507
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
512         BUG_ON(!size);
513         BUG_ON(align & (align - 1));
514         BUG_ON(limit && goal + size > limit);
515
516         if (!bdata->node_bootmem_map)
517                 return NULL;
518
519         min = bdata->node_min_pfn;
520         max = bdata->node_low_pfn;
521
522         goal >>= PAGE_SHIFT;
523         limit >>= PAGE_SHIFT;
524
525         if (limit && max > limit)
526                 max = limit;
527         if (max <= min)
528                 return NULL;
529
530         step = max(align >> PAGE_SHIFT, 1UL);
531
532         if (goal && min < goal && goal < max)
533                 start = ALIGN(goal, step);
534         else
535                 start = ALIGN(min, step);
536
537         sidx = start - bdata->node_min_pfn;
538         midx = max - bdata->node_min_pfn;
539
540         if (bdata->hint_idx > sidx) {
541                 /*
542                  * Handle the valid case of sidx being zero and still
543                  * catch the fallback below.
544                  */
545                 fallback = sidx + 1;
546                 sidx = align_idx(bdata, bdata->hint_idx, step);
547         }
548
549         while (1) {
550                 int merge;
551                 void *region;
552                 unsigned long eidx, i, start_off, end_off;
553 find_block:
554                 sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
555                 sidx = align_idx(bdata, sidx, step);
556                 eidx = sidx + PFN_UP(size);
557
558                 if (sidx >= midx || eidx > midx)
559                         break;
560
561                 for (i = sidx; i < eidx; i++)
562                         if (test_bit(i, bdata->node_bootmem_map)) {
563                                 sidx = align_idx(bdata, i, step);
564                                 if (sidx == i)
565                                         sidx += step;
566                                 goto find_block;
567                         }
568
569                 if (bdata->last_end_off & (PAGE_SIZE - 1) &&
570                                 PFN_DOWN(bdata->last_end_off) + 1 == sidx)
571                         start_off = align_off(bdata, bdata->last_end_off, align);
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                  */
584                 if (__reserve(bdata, PFN_DOWN(start_off) + merge,
585                                 PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
586                         BUG();
587
588                 region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
589                                 start_off);
590                 memset(region, 0, size);
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);
596                 return region;
597         }
598
599         if (fallback) {
600                 sidx = align_idx(bdata, fallback - 1, step);
601                 fallback = 0;
602                 goto find_block;
603         }
604
605         return NULL;
606 }
607
608 static void * __init alloc_bootmem_core(unsigned long size,
609                                         unsigned long align,
610                                         unsigned long goal,
611                                         unsigned long limit)
612 {
613         bootmem_data_t *bdata;
614         void *region;
615
616         if (WARN_ON_ONCE(slab_is_available()))
617                 return kzalloc(size, GFP_NOWAIT);
618
619         list_for_each_entry(bdata, &bdata_list, list) {
620                 if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
621                         continue;
622                 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
623                         break;
624
625                 region = alloc_bootmem_bdata(bdata, size, align, goal, limit);
626                 if (region)
627                         return region;
628         }
629
630         return NULL;
631 }
632
633 static 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
640 restart:
641         ptr = alloc_bootmem_core(size, align, goal, limit);
642         if (ptr)
643                 return ptr;
644         if (goal) {
645                 goal = 0;
646                 goto restart;
647         }
648
649         return NULL;
650 }
651
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  */
665 void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
666                                         unsigned long goal)
667 {
668         unsigned long limit = 0;
669
670         return ___alloc_bootmem_nopanic(size, align, goal, limit);
671 }
672
673 static 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");
685         return NULL;
686 }
687
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  */
701 void * __init __alloc_bootmem(unsigned long size, unsigned long align,
702                               unsigned long goal)
703 {
704         unsigned long limit = 0;
705
706         return ___alloc_bootmem(size, align, goal, limit);
707 }
708
709 void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
710                                 unsigned long size, unsigned long align,
711                                 unsigned long goal, unsigned long limit)
712 {
713         void *ptr;
714
715         if (WARN_ON_ONCE(slab_is_available()))
716                 return kzalloc(size, GFP_NOWAIT);
717 again:
718
719         /* do not panic in alloc_bootmem_bdata() */
720         if (limit && goal + size > limit)
721                 limit = 0;
722
723         ptr = alloc_bootmem_bdata(pgdat->bdata, size, align, goal, limit);
724         if (ptr)
725                 return ptr;
726
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
736         return NULL;
737 }
738
739 void * __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
745         return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
746 }
747
748 void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
749                                     unsigned long align, unsigned long goal,
750                                     unsigned long limit)
751 {
752         void *ptr;
753
754         ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
755         if (ptr)
756                 return ptr;
757
758         printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
759         panic("Out of memory");
760         return NULL;
761 }
762
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  */
778 void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
779                                    unsigned long align, unsigned long goal)
780 {
781         if (WARN_ON_ONCE(slab_is_available()))
782                 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
783
784         return  ___alloc_bootmem_node(pgdat, size, align, goal, 0);
785 }
786
787 void * __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 */
797         end_pfn = pgdat_end_pfn(pgdat);
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;
805                 ptr = alloc_bootmem_bdata(pgdat->bdata, size, align,
806                                                  new_goal, 0);
807                 if (ptr)
808                         return ptr;
809         }
810 #endif
811
812         return __alloc_bootmem_node(pgdat, size, align, goal);
813
814 }
815
816 #ifndef ARCH_LOW_ADDRESS_LIMIT
817 #define ARCH_LOW_ADDRESS_LIMIT  0xffffffffUL
818 #endif
819
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  */
833 void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
834                                   unsigned long goal)
835 {
836         return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
837 }
838
839 void * __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
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  */
862 void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
863                                        unsigned long align, unsigned long goal)
864 {
865         if (WARN_ON_ONCE(slab_is_available()))
866                 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
867
868         return ___alloc_bootmem_node(pgdat, size, align,
869                                      goal, ARCH_LOW_ADDRESS_LIMIT);
870 }