]> git.karo-electronics.de Git - karo-tx-linux.git/blob - mm/bootmem.c
DTS update
[karo-tx-linux.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
20 #include <asm/bug.h>
21 #include <asm/io.h>
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 EXPORT_SYMBOL(min_low_pfn);
36 unsigned long max_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), 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 start, end, pages, count = 0;
177
178         if (!bdata->node_bootmem_map)
179                 return 0;
180
181         start = bdata->node_min_pfn;
182         end = bdata->node_low_pfn;
183
184         bdebug("nid=%td start=%lx end=%lx\n",
185                 bdata - bootmem_node_data, start, end);
186
187         while (start < end) {
188                 unsigned long *map, idx, vec;
189                 unsigned shift;
190
191                 map = bdata->node_bootmem_map;
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), order);
215                         count += BITS_PER_LONG;
216                         start += BITS_PER_LONG;
217                 } else {
218                         unsigned long 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, 0);
225                                         count++;
226                                 }
227                                 vec >>= 1;
228                                 ++cur;
229                         }
230                 }
231         }
232
233         page = virt_to_page(bdata->node_bootmem_map);
234         pages = bdata->node_low_pfn - bdata->node_min_pfn;
235         pages = bootmem_bootmap_pages(pages);
236         count += pages;
237         while (pages--)
238                 __free_pages_bootmem(page++, 0);
239
240         bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
241
242         return count;
243 }
244
245 static void reset_node_lowmem_managed_pages(pg_data_t *pgdat)
246 {
247         struct zone *z;
248
249         /*
250          * In free_area_init_core(), highmem zone's managed_pages is set to
251          * present_pages, and bootmem allocator doesn't allocate from highmem
252          * zones. So there's no need to recalculate managed_pages because all
253          * highmem pages will be managed by the buddy system. Here highmem
254          * zone also includes highmem movable zone.
255          */
256         for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
257                 if (!is_highmem(z))
258                         z->managed_pages = 0;
259 }
260
261 /**
262  * free_all_bootmem_node - release a node's free pages to the buddy allocator
263  * @pgdat: node to be released
264  *
265  * Returns the number of pages actually released.
266  */
267 unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
268 {
269         register_page_bootmem_info_node(pgdat);
270         reset_node_lowmem_managed_pages(pgdat);
271         return free_all_bootmem_core(pgdat->bdata);
272 }
273
274 /**
275  * free_all_bootmem - release free pages to the buddy allocator
276  *
277  * Returns the number of pages actually released.
278  */
279 unsigned long __init free_all_bootmem(void)
280 {
281         unsigned long total_pages = 0;
282         bootmem_data_t *bdata;
283         struct pglist_data *pgdat;
284
285         for_each_online_pgdat(pgdat)
286                 reset_node_lowmem_managed_pages(pgdat);
287
288         list_for_each_entry(bdata, &bdata_list, list)
289                 total_pages += free_all_bootmem_core(bdata);
290
291         return total_pages;
292 }
293
294 static void __init __free(bootmem_data_t *bdata,
295                         unsigned long sidx, unsigned long eidx)
296 {
297         unsigned long idx;
298
299         bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
300                 sidx + bdata->node_min_pfn,
301                 eidx + bdata->node_min_pfn);
302
303         if (bdata->hint_idx > sidx)
304                 bdata->hint_idx = sidx;
305
306         for (idx = sidx; idx < eidx; idx++)
307                 if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
308                         BUG();
309 }
310
311 static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
312                         unsigned long eidx, int flags)
313 {
314         unsigned long idx;
315         int exclusive = flags & BOOTMEM_EXCLUSIVE;
316
317         bdebug("nid=%td start=%lx end=%lx flags=%x\n",
318                 bdata - bootmem_node_data,
319                 sidx + bdata->node_min_pfn,
320                 eidx + bdata->node_min_pfn,
321                 flags);
322
323         for (idx = sidx; idx < eidx; idx++)
324                 if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
325                         if (exclusive) {
326                                 __free(bdata, sidx, idx);
327                                 return -EBUSY;
328                         }
329                         bdebug("silent double reserve of PFN %lx\n",
330                                 idx + bdata->node_min_pfn);
331                 }
332         return 0;
333 }
334
335 static int __init mark_bootmem_node(bootmem_data_t *bdata,
336                                 unsigned long start, unsigned long end,
337                                 int reserve, int flags)
338 {
339         unsigned long sidx, eidx;
340
341         bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
342                 bdata - bootmem_node_data, start, end, reserve, flags);
343
344         BUG_ON(start < bdata->node_min_pfn);
345         BUG_ON(end > bdata->node_low_pfn);
346
347         sidx = start - bdata->node_min_pfn;
348         eidx = end - bdata->node_min_pfn;
349
350         if (reserve)
351                 return __reserve(bdata, sidx, eidx, flags);
352         else
353                 __free(bdata, sidx, eidx);
354         return 0;
355 }
356
357 static int __init mark_bootmem(unsigned long start, unsigned long end,
358                                 int reserve, int flags)
359 {
360         unsigned long pos;
361         bootmem_data_t *bdata;
362
363         pos = start;
364         list_for_each_entry(bdata, &bdata_list, list) {
365                 int err;
366                 unsigned long max;
367
368                 if (pos < bdata->node_min_pfn ||
369                     pos >= bdata->node_low_pfn) {
370                         BUG_ON(pos != start);
371                         continue;
372                 }
373
374                 max = min(bdata->node_low_pfn, end);
375
376                 err = mark_bootmem_node(bdata, pos, max, reserve, flags);
377                 if (reserve && err) {
378                         mark_bootmem(start, pos, 0, 0);
379                         return err;
380                 }
381
382                 if (max == end)
383                         return 0;
384                 pos = bdata->node_low_pfn;
385         }
386         BUG();
387 }
388
389 /**
390  * free_bootmem_node - mark a page range as usable
391  * @pgdat: node the range resides on
392  * @physaddr: starting address of the range
393  * @size: size of the range in bytes
394  *
395  * Partial pages will be considered reserved and left as they are.
396  *
397  * The range must reside completely on the specified node.
398  */
399 void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
400                               unsigned long size)
401 {
402         unsigned long start, end;
403
404         kmemleak_free_part(__va(physaddr), size);
405
406         start = PFN_UP(physaddr);
407         end = PFN_DOWN(physaddr + size);
408
409         mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
410 }
411
412 /**
413  * free_bootmem - mark a page range as usable
414  * @addr: starting physical address of the range
415  * @size: size of the range in bytes
416  *
417  * Partial pages will be considered reserved and left as they are.
418  *
419  * The range must be contiguous but may span node boundaries.
420  */
421 void __init free_bootmem(unsigned long physaddr, unsigned long size)
422 {
423         unsigned long start, end;
424
425         kmemleak_free_part(__va(physaddr), size);
426
427         start = PFN_UP(physaddr);
428         end = PFN_DOWN(physaddr + size);
429
430         mark_bootmem(start, end, 0, 0);
431 }
432
433 /**
434  * reserve_bootmem_node - mark a page range as reserved
435  * @pgdat: node the range resides on
436  * @physaddr: starting address of the range
437  * @size: size of the range in bytes
438  * @flags: reservation flags (see linux/bootmem.h)
439  *
440  * Partial pages will be reserved.
441  *
442  * The range must reside completely on the specified node.
443  */
444 int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
445                                  unsigned long size, int flags)
446 {
447         unsigned long start, end;
448
449         start = PFN_DOWN(physaddr);
450         end = PFN_UP(physaddr + size);
451
452         return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
453 }
454
455 /**
456  * reserve_bootmem - mark a page range as reserved
457  * @addr: starting address of the range
458  * @size: size of the range in bytes
459  * @flags: reservation flags (see linux/bootmem.h)
460  *
461  * Partial pages will be reserved.
462  *
463  * The range must be contiguous but may span node boundaries.
464  */
465 int __init reserve_bootmem(unsigned long addr, unsigned long size,
466                             int flags)
467 {
468         unsigned long start, end;
469
470         start = PFN_DOWN(addr);
471         end = PFN_UP(addr + size);
472
473         return mark_bootmem(start, end, 1, flags);
474 }
475
476 static unsigned long __init align_idx(struct bootmem_data *bdata,
477                                       unsigned long idx, unsigned long step)
478 {
479         unsigned long base = bdata->node_min_pfn;
480
481         /*
482          * Align the index with respect to the node start so that the
483          * combination of both satisfies the requested alignment.
484          */
485
486         return ALIGN(base + idx, step) - base;
487 }
488
489 static unsigned long __init align_off(struct bootmem_data *bdata,
490                                       unsigned long off, unsigned long align)
491 {
492         unsigned long base = PFN_PHYS(bdata->node_min_pfn);
493
494         /* Same as align_idx for byte offsets */
495
496         return ALIGN(base + off, align) - base;
497 }
498
499 static void * __init alloc_bootmem_bdata(struct bootmem_data *bdata,
500                                         unsigned long size, unsigned long align,
501                                         unsigned long goal, unsigned long limit)
502 {
503         unsigned long fallback = 0;
504         unsigned long min, max, start, sidx, midx, step;
505
506         bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
507                 bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
508                 align, goal, limit);
509
510         BUG_ON(!size);
511         BUG_ON(align & (align - 1));
512         BUG_ON(limit && goal + size > limit);
513
514         if (!bdata->node_bootmem_map)
515                 return NULL;
516
517         min = bdata->node_min_pfn;
518         max = bdata->node_low_pfn;
519
520         goal >>= PAGE_SHIFT;
521         limit >>= PAGE_SHIFT;
522
523         if (limit && max > limit)
524                 max = limit;
525         if (max <= min)
526                 return NULL;
527
528         step = max(align >> PAGE_SHIFT, 1UL);
529
530         if (goal && min < goal && goal < max)
531                 start = ALIGN(goal, step);
532         else
533                 start = ALIGN(min, step);
534
535         sidx = start - bdata->node_min_pfn;
536         midx = max - bdata->node_min_pfn;
537
538         if (bdata->hint_idx > sidx) {
539                 /*
540                  * Handle the valid case of sidx being zero and still
541                  * catch the fallback below.
542                  */
543                 fallback = sidx + 1;
544                 sidx = align_idx(bdata, bdata->hint_idx, step);
545         }
546
547         while (1) {
548                 int merge;
549                 void *region;
550                 unsigned long eidx, i, start_off, end_off;
551 find_block:
552                 sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
553                 sidx = align_idx(bdata, sidx, step);
554                 eidx = sidx + PFN_UP(size);
555
556                 if (sidx >= midx || eidx > midx)
557                         break;
558
559                 for (i = sidx; i < eidx; i++)
560                         if (test_bit(i, bdata->node_bootmem_map)) {
561                                 sidx = align_idx(bdata, i, step);
562                                 if (sidx == i)
563                                         sidx += step;
564                                 goto find_block;
565                         }
566
567                 if (bdata->last_end_off & (PAGE_SIZE - 1) &&
568                                 PFN_DOWN(bdata->last_end_off) + 1 == sidx)
569                         start_off = align_off(bdata, bdata->last_end_off, align);
570                 else
571                         start_off = PFN_PHYS(sidx);
572
573                 merge = PFN_DOWN(start_off) < sidx;
574                 end_off = start_off + size;
575
576                 bdata->last_end_off = end_off;
577                 bdata->hint_idx = PFN_UP(end_off);
578
579                 /*
580                  * Reserve the area now:
581                  */
582                 if (__reserve(bdata, PFN_DOWN(start_off) + merge,
583                                 PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
584                         BUG();
585
586                 region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
587                                 start_off);
588                 memset(region, 0, size);
589                 /*
590                  * The min_count is set to 0 so that bootmem allocated blocks
591                  * are never reported as leaks.
592                  */
593                 kmemleak_alloc(region, size, 0, 0);
594                 return region;
595         }
596
597         if (fallback) {
598                 sidx = align_idx(bdata, fallback - 1, step);
599                 fallback = 0;
600                 goto find_block;
601         }
602
603         return NULL;
604 }
605
606 static void * __init alloc_bootmem_core(unsigned long size,
607                                         unsigned long align,
608                                         unsigned long goal,
609                                         unsigned long limit)
610 {
611         bootmem_data_t *bdata;
612         void *region;
613
614         if (WARN_ON_ONCE(slab_is_available()))
615                 return kzalloc(size, GFP_NOWAIT);
616
617         list_for_each_entry(bdata, &bdata_list, list) {
618                 if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
619                         continue;
620                 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
621                         break;
622
623                 region = alloc_bootmem_bdata(bdata, size, align, goal, limit);
624                 if (region)
625                         return region;
626         }
627
628         return NULL;
629 }
630
631 static void * __init ___alloc_bootmem_nopanic(unsigned long size,
632                                               unsigned long align,
633                                               unsigned long goal,
634                                               unsigned long limit)
635 {
636         void *ptr;
637
638 restart:
639         ptr = alloc_bootmem_core(size, align, goal, limit);
640         if (ptr)
641                 return ptr;
642         if (goal) {
643                 goal = 0;
644                 goto restart;
645         }
646
647         return NULL;
648 }
649
650 /**
651  * __alloc_bootmem_nopanic - allocate boot memory without panicking
652  * @size: size of the request in bytes
653  * @align: alignment of the region
654  * @goal: preferred starting address of the region
655  *
656  * The goal is dropped if it can not be satisfied and the allocation will
657  * fall back to memory below @goal.
658  *
659  * Allocation may happen on any node in the system.
660  *
661  * Returns NULL on failure.
662  */
663 void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
664                                         unsigned long goal)
665 {
666         unsigned long limit = 0;
667
668         return ___alloc_bootmem_nopanic(size, align, goal, limit);
669 }
670
671 static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
672                                         unsigned long goal, unsigned long limit)
673 {
674         void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
675
676         if (mem)
677                 return mem;
678         /*
679          * Whoops, we cannot satisfy the allocation request.
680          */
681         printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
682         panic("Out of memory");
683         return NULL;
684 }
685
686 /**
687  * __alloc_bootmem - allocate boot memory
688  * @size: size of the request in bytes
689  * @align: alignment of the region
690  * @goal: preferred starting address of the region
691  *
692  * The goal is dropped if it can not be satisfied and the allocation will
693  * fall back to memory below @goal.
694  *
695  * Allocation may happen on any node in the system.
696  *
697  * The function panics if the request can not be satisfied.
698  */
699 void * __init __alloc_bootmem(unsigned long size, unsigned long align,
700                               unsigned long goal)
701 {
702         unsigned long limit = 0;
703
704         return ___alloc_bootmem(size, align, goal, limit);
705 }
706
707 void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
708                                 unsigned long size, unsigned long align,
709                                 unsigned long goal, unsigned long limit)
710 {
711         void *ptr;
712
713         if (WARN_ON_ONCE(slab_is_available()))
714                 return kzalloc(size, GFP_NOWAIT);
715 again:
716
717         /* do not panic in alloc_bootmem_bdata() */
718         if (limit && goal + size > limit)
719                 limit = 0;
720
721         ptr = alloc_bootmem_bdata(pgdat->bdata, size, align, goal, limit);
722         if (ptr)
723                 return ptr;
724
725         ptr = alloc_bootmem_core(size, align, goal, limit);
726         if (ptr)
727                 return ptr;
728
729         if (goal) {
730                 goal = 0;
731                 goto again;
732         }
733
734         return NULL;
735 }
736
737 void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
738                                    unsigned long align, unsigned long goal)
739 {
740         if (WARN_ON_ONCE(slab_is_available()))
741                 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
742
743         return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
744 }
745
746 void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
747                                     unsigned long align, unsigned long goal,
748                                     unsigned long limit)
749 {
750         void *ptr;
751
752         ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
753         if (ptr)
754                 return ptr;
755
756         printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
757         panic("Out of memory");
758         return NULL;
759 }
760
761 /**
762  * __alloc_bootmem_node - allocate boot memory from a specific node
763  * @pgdat: node to allocate from
764  * @size: size of the request in bytes
765  * @align: alignment of the region
766  * @goal: preferred starting address of the region
767  *
768  * The goal is dropped if it can not be satisfied and the allocation will
769  * fall back to memory below @goal.
770  *
771  * Allocation may fall back to any node in the system if the specified node
772  * can not hold the requested memory.
773  *
774  * The function panics if the request can not be satisfied.
775  */
776 void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
777                                    unsigned long align, unsigned long goal)
778 {
779         if (WARN_ON_ONCE(slab_is_available()))
780                 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
781
782         return  ___alloc_bootmem_node(pgdat, size, align, goal, 0);
783 }
784
785 void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
786                                    unsigned long align, unsigned long goal)
787 {
788 #ifdef MAX_DMA32_PFN
789         unsigned long end_pfn;
790
791         if (WARN_ON_ONCE(slab_is_available()))
792                 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
793
794         /* update goal according ...MAX_DMA32_PFN */
795         end_pfn = pgdat->node_start_pfn + pgdat->node_spanned_pages;
796
797         if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
798             (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
799                 void *ptr;
800                 unsigned long new_goal;
801
802                 new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
803                 ptr = alloc_bootmem_bdata(pgdat->bdata, size, align,
804                                                  new_goal, 0);
805                 if (ptr)
806                         return ptr;
807         }
808 #endif
809
810         return __alloc_bootmem_node(pgdat, size, align, goal);
811
812 }
813
814 #ifndef ARCH_LOW_ADDRESS_LIMIT
815 #define ARCH_LOW_ADDRESS_LIMIT  0xffffffffUL
816 #endif
817
818 /**
819  * __alloc_bootmem_low - allocate low boot memory
820  * @size: size of the request in bytes
821  * @align: alignment of the region
822  * @goal: preferred starting address of the region
823  *
824  * The goal is dropped if it can not be satisfied and the allocation will
825  * fall back to memory below @goal.
826  *
827  * Allocation may happen on any node in the system.
828  *
829  * The function panics if the request can not be satisfied.
830  */
831 void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
832                                   unsigned long goal)
833 {
834         return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
835 }
836
837 void * __init __alloc_bootmem_low_nopanic(unsigned long size,
838                                           unsigned long align,
839                                           unsigned long goal)
840 {
841         return ___alloc_bootmem_nopanic(size, align, goal,
842                                         ARCH_LOW_ADDRESS_LIMIT);
843 }
844
845 /**
846  * __alloc_bootmem_low_node - allocate low boot memory from a specific node
847  * @pgdat: node to allocate from
848  * @size: size of the request in bytes
849  * @align: alignment of the region
850  * @goal: preferred starting address of the region
851  *
852  * The goal is dropped if it can not be satisfied and the allocation will
853  * fall back to memory below @goal.
854  *
855  * Allocation may fall back to any node in the system if the specified node
856  * can not hold the requested memory.
857  *
858  * The function panics if the request can not be satisfied.
859  */
860 void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
861                                        unsigned long align, unsigned long goal)
862 {
863         if (WARN_ON_ONCE(slab_is_available()))
864                 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
865
866         return ___alloc_bootmem_node(pgdat, size, align,
867                                      goal, ARCH_LOW_ADDRESS_LIMIT);
868 }