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