bootmem: revisit bitmap size calculations
[safe/jmp/linux-2.6] / 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/bootmem.h>
14 #include <linux/module.h>
15
16 #include <asm/bug.h>
17 #include <asm/io.h>
18 #include <asm/processor.h>
19
20 #include "internal.h"
21
22 unsigned long max_low_pfn;
23 unsigned long min_low_pfn;
24 unsigned long max_pfn;
25
26 static LIST_HEAD(bdata_list);
27 #ifdef CONFIG_CRASH_DUMP
28 /*
29  * If we have booted due to a crash, max_pfn will be a very low value. We need
30  * to know the amount of memory that the previous kernel used.
31  */
32 unsigned long saved_max_pfn;
33 #endif
34
35 bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata;
36
37 static int bootmem_debug;
38
39 static int __init bootmem_debug_setup(char *buf)
40 {
41         bootmem_debug = 1;
42         return 0;
43 }
44 early_param("bootmem_debug", bootmem_debug_setup);
45
46 #define bdebug(fmt, args...) ({                         \
47         if (unlikely(bootmem_debug))                    \
48                 printk(KERN_INFO                        \
49                         "bootmem::%s " fmt,             \
50                         __FUNCTION__, ## args);         \
51 })
52
53 static unsigned long __init bootmap_bytes(unsigned long pages)
54 {
55         unsigned long bytes = (pages + 7) / 8;
56
57         return ALIGN(bytes, sizeof(long));
58 }
59
60 /**
61  * bootmem_bootmap_pages - calculate bitmap size in pages
62  * @pages: number of pages the bitmap has to represent
63  */
64 unsigned long __init bootmem_bootmap_pages(unsigned long pages)
65 {
66         unsigned long bytes = bootmap_bytes(pages);
67
68         return PAGE_ALIGN(bytes) >> PAGE_SHIFT;
69 }
70
71 /*
72  * link bdata in order
73  */
74 static void __init link_bootmem(bootmem_data_t *bdata)
75 {
76         bootmem_data_t *ent;
77
78         if (list_empty(&bdata_list)) {
79                 list_add(&bdata->list, &bdata_list);
80                 return;
81         }
82         /* insert in order */
83         list_for_each_entry(ent, &bdata_list, list) {
84                 if (bdata->node_boot_start < ent->node_boot_start) {
85                         list_add_tail(&bdata->list, &ent->list);
86                         return;
87                 }
88         }
89         list_add_tail(&bdata->list, &bdata_list);
90 }
91
92 /*
93  * Called once to set up the allocator itself.
94  */
95 static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
96         unsigned long mapstart, unsigned long start, unsigned long end)
97 {
98         unsigned long mapsize;
99
100         mminit_validate_memmodel_limits(&start, &end);
101         bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
102         bdata->node_boot_start = PFN_PHYS(start);
103         bdata->node_low_pfn = end;
104         link_bootmem(bdata);
105
106         /*
107          * Initially all pages are reserved - setup_arch() has to
108          * register free RAM areas explicitly.
109          */
110         mapsize = bootmap_bytes(end - start);
111         memset(bdata->node_bootmem_map, 0xff, mapsize);
112
113         bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n",
114                 bdata - bootmem_node_data, start, mapstart, end, mapsize);
115
116         return mapsize;
117 }
118
119 /**
120  * init_bootmem_node - register a node as boot memory
121  * @pgdat: node to register
122  * @freepfn: pfn where the bitmap for this node is to be placed
123  * @startpfn: first pfn on the node
124  * @endpfn: first pfn after the node
125  *
126  * Returns the number of bytes needed to hold the bitmap for this node.
127  */
128 unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
129                                 unsigned long startpfn, unsigned long endpfn)
130 {
131         return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn);
132 }
133
134 /**
135  * init_bootmem - register boot memory
136  * @start: pfn where the bitmap is to be placed
137  * @pages: number of available physical pages
138  *
139  * Returns the number of bytes needed to hold the bitmap.
140  */
141 unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
142 {
143         max_low_pfn = pages;
144         min_low_pfn = start;
145         return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages);
146 }
147
148 static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
149 {
150         struct page *page;
151         unsigned long pfn;
152         unsigned long i, count;
153         unsigned long idx, pages;
154         unsigned long *map;
155         int gofast = 0;
156
157         BUG_ON(!bdata->node_bootmem_map);
158
159         count = 0;
160         /* first extant page of the node */
161         pfn = PFN_DOWN(bdata->node_boot_start);
162         idx = bdata->node_low_pfn - pfn;
163         map = bdata->node_bootmem_map;
164         /*
165          * Check if we are aligned to BITS_PER_LONG pages.  If so, we might
166          * be able to free page orders of that size at once.
167          */
168         if (!(pfn & (BITS_PER_LONG-1)))
169                 gofast = 1;
170
171         for (i = 0; i < idx; ) {
172                 unsigned long v = ~map[i / BITS_PER_LONG];
173
174                 if (gofast && v == ~0UL) {
175                         int order;
176
177                         page = pfn_to_page(pfn);
178                         count += BITS_PER_LONG;
179                         order = ffs(BITS_PER_LONG) - 1;
180                         __free_pages_bootmem(page, order);
181                         i += BITS_PER_LONG;
182                         page += BITS_PER_LONG;
183                 } else if (v) {
184                         unsigned long m;
185
186                         page = pfn_to_page(pfn);
187                         for (m = 1; m && i < idx; m<<=1, page++, i++) {
188                                 if (v & m) {
189                                         count++;
190                                         __free_pages_bootmem(page, 0);
191                                 }
192                         }
193                 } else {
194                         i += BITS_PER_LONG;
195                 }
196                 pfn += BITS_PER_LONG;
197         }
198
199         /*
200          * Now free the allocator bitmap itself, it's not
201          * needed anymore:
202          */
203         page = virt_to_page(bdata->node_bootmem_map);
204         pages = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
205         idx = bootmem_bootmap_pages(pages);
206         for (i = 0; i < idx; i++, page++)
207                 __free_pages_bootmem(page, 0);
208         count += i;
209         bdata->node_bootmem_map = NULL;
210
211         bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
212
213         return count;
214 }
215
216 /**
217  * free_all_bootmem_node - release a node's free pages to the buddy allocator
218  * @pgdat: node to be released
219  *
220  * Returns the number of pages actually released.
221  */
222 unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
223 {
224         register_page_bootmem_info_node(pgdat);
225         return free_all_bootmem_core(pgdat->bdata);
226 }
227
228 /**
229  * free_all_bootmem - release free pages to the buddy allocator
230  *
231  * Returns the number of pages actually released.
232  */
233 unsigned long __init free_all_bootmem(void)
234 {
235         return free_all_bootmem_core(NODE_DATA(0)->bdata);
236 }
237
238 static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
239                                      unsigned long size)
240 {
241         unsigned long sidx, eidx;
242         unsigned long i;
243
244         BUG_ON(!size);
245
246         /* out range */
247         if (addr + size < bdata->node_boot_start ||
248                 PFN_DOWN(addr) > bdata->node_low_pfn)
249                 return;
250         /*
251          * round down end of usable mem, partially free pages are
252          * considered reserved.
253          */
254
255         if (addr >= bdata->node_boot_start && addr < bdata->last_success)
256                 bdata->last_success = addr;
257
258         /*
259          * Round up to index to the range.
260          */
261         if (PFN_UP(addr) > PFN_DOWN(bdata->node_boot_start))
262                 sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start);
263         else
264                 sidx = 0;
265
266         eidx = PFN_DOWN(addr + size - bdata->node_boot_start);
267         if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
268                 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
269
270         bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
271                 sidx + PFN_DOWN(bdata->node_boot_start),
272                 eidx + PFN_DOWN(bdata->node_boot_start));
273
274         for (i = sidx; i < eidx; i++) {
275                 if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
276                         BUG();
277         }
278 }
279
280 /**
281  * free_bootmem_node - mark a page range as usable
282  * @pgdat: node the range resides on
283  * @physaddr: starting address of the range
284  * @size: size of the range in bytes
285  *
286  * Partial pages will be considered reserved and left as they are.
287  *
288  * Only physical pages that actually reside on @pgdat are marked.
289  */
290 void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
291                               unsigned long size)
292 {
293         free_bootmem_core(pgdat->bdata, physaddr, size);
294 }
295
296 /**
297  * free_bootmem - mark a page range as usable
298  * @addr: starting address of the range
299  * @size: size of the range in bytes
300  *
301  * Partial pages will be considered reserved and left as they are.
302  *
303  * All physical pages within the range are marked, no matter what
304  * node they reside on.
305  */
306 void __init free_bootmem(unsigned long addr, unsigned long size)
307 {
308         bootmem_data_t *bdata;
309         list_for_each_entry(bdata, &bdata_list, list)
310                 free_bootmem_core(bdata, addr, size);
311 }
312
313 /*
314  * Marks a particular physical memory range as unallocatable. Usable RAM
315  * might be used for boot-time allocations - or it might get added
316  * to the free page pool later on.
317  */
318 static int __init can_reserve_bootmem_core(bootmem_data_t *bdata,
319                         unsigned long addr, unsigned long size, int flags)
320 {
321         unsigned long sidx, eidx;
322         unsigned long i;
323
324         BUG_ON(!size);
325
326         /* out of range, don't hold other */
327         if (addr + size < bdata->node_boot_start ||
328                 PFN_DOWN(addr) > bdata->node_low_pfn)
329                 return 0;
330
331         /*
332          * Round up to index to the range.
333          */
334         if (addr > bdata->node_boot_start)
335                 sidx= PFN_DOWN(addr - bdata->node_boot_start);
336         else
337                 sidx = 0;
338
339         eidx = PFN_UP(addr + size - bdata->node_boot_start);
340         if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
341                 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
342
343         for (i = sidx; i < eidx; i++) {
344                 if (test_bit(i, bdata->node_bootmem_map)) {
345                         if (flags & BOOTMEM_EXCLUSIVE)
346                                 return -EBUSY;
347                 }
348         }
349
350         return 0;
351
352 }
353
354 static void __init reserve_bootmem_core(bootmem_data_t *bdata,
355                         unsigned long addr, unsigned long size, int flags)
356 {
357         unsigned long sidx, eidx;
358         unsigned long i;
359
360         BUG_ON(!size);
361
362         /* out of range */
363         if (addr + size < bdata->node_boot_start ||
364                 PFN_DOWN(addr) > bdata->node_low_pfn)
365                 return;
366
367         /*
368          * Round up to index to the range.
369          */
370         if (addr > bdata->node_boot_start)
371                 sidx= PFN_DOWN(addr - bdata->node_boot_start);
372         else
373                 sidx = 0;
374
375         eidx = PFN_UP(addr + size - bdata->node_boot_start);
376         if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
377                 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
378
379         bdebug("nid=%td start=%lx end=%lx flags=%x\n",
380                 bdata - bootmem_node_data,
381                 sidx + PFN_DOWN(bdata->node_boot_start),
382                 eidx + PFN_DOWN(bdata->node_boot_start),
383                 flags);
384
385         for (i = sidx; i < eidx; i++)
386                 if (test_and_set_bit(i, bdata->node_bootmem_map))
387                         bdebug("hm, page %lx reserved twice.\n",
388                                 PFN_DOWN(bdata->node_boot_start) + i);
389 }
390
391 /**
392  * reserve_bootmem_node - mark a page range as reserved
393  * @pgdat: node the range resides on
394  * @physaddr: starting address of the range
395  * @size: size of the range in bytes
396  * @flags: reservation flags (see linux/bootmem.h)
397  *
398  * Partial pages will be reserved.
399  *
400  * Only physical pages that actually reside on @pgdat are marked.
401  */
402 int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
403                                  unsigned long size, int flags)
404 {
405         int ret;
406
407         ret = can_reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
408         if (ret < 0)
409                 return -ENOMEM;
410         reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
411         return 0;
412 }
413
414 #ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
415 /**
416  * reserve_bootmem - mark a page range as usable
417  * @addr: starting address of the range
418  * @size: size of the range in bytes
419  * @flags: reservation flags (see linux/bootmem.h)
420  *
421  * Partial pages will be reserved.
422  *
423  * All physical pages within the range are marked, no matter what
424  * node they reside on.
425  */
426 int __init reserve_bootmem(unsigned long addr, unsigned long size,
427                             int flags)
428 {
429         bootmem_data_t *bdata;
430         int ret;
431
432         list_for_each_entry(bdata, &bdata_list, list) {
433                 ret = can_reserve_bootmem_core(bdata, addr, size, flags);
434                 if (ret < 0)
435                         return ret;
436         }
437         list_for_each_entry(bdata, &bdata_list, list)
438                 reserve_bootmem_core(bdata, addr, size, flags);
439
440         return 0;
441 }
442 #endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
443
444 /*
445  * We 'merge' subsequent allocations to save space. We might 'lose'
446  * some fraction of a page if allocations cannot be satisfied due to
447  * size constraints on boxes where there is physical RAM space
448  * fragmentation - in these cases (mostly large memory boxes) this
449  * is not a problem.
450  *
451  * On low memory boxes we get it right in 100% of the cases.
452  *
453  * alignment has to be a power of 2 value.
454  *
455  * NOTE:  This function is _not_ reentrant.
456  */
457 static void * __init
458 alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
459                 unsigned long align, unsigned long goal, unsigned long limit)
460 {
461         unsigned long areasize, preferred;
462         unsigned long i, start = 0, incr, eidx, end_pfn;
463         void *ret;
464         unsigned long node_boot_start;
465         void *node_bootmem_map;
466
467         if (!size) {
468                 printk("alloc_bootmem_core(): zero-sized request\n");
469                 BUG();
470         }
471         BUG_ON(align & (align-1));
472
473         /* on nodes without memory - bootmem_map is NULL */
474         if (!bdata->node_bootmem_map)
475                 return NULL;
476
477         bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
478                 bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
479                 align, goal, limit);
480
481         /* bdata->node_boot_start is supposed to be (12+6)bits alignment on x86_64 ? */
482         node_boot_start = bdata->node_boot_start;
483         node_bootmem_map = bdata->node_bootmem_map;
484         if (align) {
485                 node_boot_start = ALIGN(bdata->node_boot_start, align);
486                 if (node_boot_start > bdata->node_boot_start)
487                         node_bootmem_map = (unsigned long *)bdata->node_bootmem_map +
488                             PFN_DOWN(node_boot_start - bdata->node_boot_start)/BITS_PER_LONG;
489         }
490
491         if (limit && node_boot_start >= limit)
492                 return NULL;
493
494         end_pfn = bdata->node_low_pfn;
495         limit = PFN_DOWN(limit);
496         if (limit && end_pfn > limit)
497                 end_pfn = limit;
498
499         eidx = end_pfn - PFN_DOWN(node_boot_start);
500
501         /*
502          * We try to allocate bootmem pages above 'goal'
503          * first, then we try to allocate lower pages.
504          */
505         preferred = 0;
506         if (goal && PFN_DOWN(goal) < end_pfn) {
507                 if (goal > node_boot_start)
508                         preferred = goal - node_boot_start;
509
510                 if (bdata->last_success > node_boot_start &&
511                         bdata->last_success - node_boot_start >= preferred)
512                         if (!limit || (limit && limit > bdata->last_success))
513                                 preferred = bdata->last_success - node_boot_start;
514         }
515
516         preferred = PFN_DOWN(ALIGN(preferred, align));
517         areasize = (size + PAGE_SIZE-1) / PAGE_SIZE;
518         incr = align >> PAGE_SHIFT ? : 1;
519
520 restart_scan:
521         for (i = preferred; i < eidx;) {
522                 unsigned long j;
523
524                 i = find_next_zero_bit(node_bootmem_map, eidx, i);
525                 i = ALIGN(i, incr);
526                 if (i >= eidx)
527                         break;
528                 if (test_bit(i, node_bootmem_map)) {
529                         i += incr;
530                         continue;
531                 }
532                 for (j = i + 1; j < i + areasize; ++j) {
533                         if (j >= eidx)
534                                 goto fail_block;
535                         if (test_bit(j, node_bootmem_map))
536                                 goto fail_block;
537                 }
538                 start = i;
539                 goto found;
540         fail_block:
541                 i = ALIGN(j, incr);
542                 if (i == j)
543                         i += incr;
544         }
545
546         if (preferred > 0) {
547                 preferred = 0;
548                 goto restart_scan;
549         }
550         return NULL;
551
552 found:
553         bdata->last_success = PFN_PHYS(start) + node_boot_start;
554         BUG_ON(start >= eidx);
555
556         /*
557          * Is the next page of the previous allocation-end the start
558          * of this allocation's buffer? If yes then we can 'merge'
559          * the previous partial page with this allocation.
560          */
561         if (align < PAGE_SIZE &&
562             bdata->last_offset && bdata->last_pos+1 == start) {
563                 unsigned long offset, remaining_size;
564                 offset = ALIGN(bdata->last_offset, align);
565                 BUG_ON(offset > PAGE_SIZE);
566                 remaining_size = PAGE_SIZE - offset;
567                 if (size < remaining_size) {
568                         areasize = 0;
569                         /* last_pos unchanged */
570                         bdata->last_offset = offset + size;
571                         ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
572                                            offset + node_boot_start);
573                 } else {
574                         remaining_size = size - remaining_size;
575                         areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE;
576                         ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
577                                            offset + node_boot_start);
578                         bdata->last_pos = start + areasize - 1;
579                         bdata->last_offset = remaining_size;
580                 }
581                 bdata->last_offset &= ~PAGE_MASK;
582         } else {
583                 bdata->last_pos = start + areasize - 1;
584                 bdata->last_offset = size & ~PAGE_MASK;
585                 ret = phys_to_virt(start * PAGE_SIZE + node_boot_start);
586         }
587
588         bdebug("nid=%td start=%lx end=%lx\n",
589                 bdata - bootmem_node_data,
590                 start + PFN_DOWN(bdata->node_boot_start),
591                 start + areasize + PFN_DOWN(bdata->node_boot_start));
592
593         /*
594          * Reserve the area now:
595          */
596         for (i = start; i < start + areasize; i++)
597                 if (unlikely(test_and_set_bit(i, node_bootmem_map)))
598                         BUG();
599         memset(ret, 0, size);
600         return ret;
601 }
602
603 /**
604  * __alloc_bootmem_nopanic - allocate boot memory without panicking
605  * @size: size of the request in bytes
606  * @align: alignment of the region
607  * @goal: preferred starting address of the region
608  *
609  * The goal is dropped if it can not be satisfied and the allocation will
610  * fall back to memory below @goal.
611  *
612  * Allocation may happen on any node in the system.
613  *
614  * Returns NULL on failure.
615  */
616 void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
617                                       unsigned long goal)
618 {
619         bootmem_data_t *bdata;
620         void *ptr;
621
622         list_for_each_entry(bdata, &bdata_list, list) {
623                 ptr = alloc_bootmem_core(bdata, size, align, goal, 0);
624                 if (ptr)
625                         return ptr;
626         }
627         return NULL;
628 }
629
630 /**
631  * __alloc_bootmem - allocate boot memory
632  * @size: size of the request in bytes
633  * @align: alignment of the region
634  * @goal: preferred starting address of the region
635  *
636  * The goal is dropped if it can not be satisfied and the allocation will
637  * fall back to memory below @goal.
638  *
639  * Allocation may happen on any node in the system.
640  *
641  * The function panics if the request can not be satisfied.
642  */
643 void * __init __alloc_bootmem(unsigned long size, unsigned long align,
644                               unsigned long goal)
645 {
646         void *mem = __alloc_bootmem_nopanic(size,align,goal);
647
648         if (mem)
649                 return mem;
650         /*
651          * Whoops, we cannot satisfy the allocation request.
652          */
653         printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
654         panic("Out of memory");
655         return NULL;
656 }
657
658 /**
659  * __alloc_bootmem_node - allocate boot memory from a specific node
660  * @pgdat: node to allocate from
661  * @size: size of the request in bytes
662  * @align: alignment of the region
663  * @goal: preferred starting address of the region
664  *
665  * The goal is dropped if it can not be satisfied and the allocation will
666  * fall back to memory below @goal.
667  *
668  * Allocation may fall back to any node in the system if the specified node
669  * can not hold the requested memory.
670  *
671  * The function panics if the request can not be satisfied.
672  */
673 void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
674                                    unsigned long align, unsigned long goal)
675 {
676         void *ptr;
677
678         ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
679         if (ptr)
680                 return ptr;
681
682         return __alloc_bootmem(size, align, goal);
683 }
684
685 #ifdef CONFIG_SPARSEMEM
686 /**
687  * alloc_bootmem_section - allocate boot memory from a specific section
688  * @size: size of the request in bytes
689  * @section_nr: sparse map section to allocate from
690  *
691  * Return NULL on failure.
692  */
693 void * __init alloc_bootmem_section(unsigned long size,
694                                     unsigned long section_nr)
695 {
696         void *ptr;
697         unsigned long limit, goal, start_nr, end_nr, pfn;
698         struct pglist_data *pgdat;
699
700         pfn = section_nr_to_pfn(section_nr);
701         goal = PFN_PHYS(pfn);
702         limit = PFN_PHYS(section_nr_to_pfn(section_nr + 1)) - 1;
703         pgdat = NODE_DATA(early_pfn_to_nid(pfn));
704         ptr = alloc_bootmem_core(pgdat->bdata, size, SMP_CACHE_BYTES, goal,
705                                 limit);
706
707         if (!ptr)
708                 return NULL;
709
710         start_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr)));
711         end_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr) + size));
712         if (start_nr != section_nr || end_nr != section_nr) {
713                 printk(KERN_WARNING "alloc_bootmem failed on section %ld.\n",
714                        section_nr);
715                 free_bootmem_core(pgdat->bdata, __pa(ptr), size);
716                 ptr = NULL;
717         }
718
719         return ptr;
720 }
721 #endif
722
723 void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
724                                    unsigned long align, unsigned long goal)
725 {
726         void *ptr;
727
728         ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
729         if (ptr)
730                 return ptr;
731
732         return __alloc_bootmem_nopanic(size, align, goal);
733 }
734
735 #ifndef ARCH_LOW_ADDRESS_LIMIT
736 #define ARCH_LOW_ADDRESS_LIMIT  0xffffffffUL
737 #endif
738
739 /**
740  * __alloc_bootmem_low - allocate low boot memory
741  * @size: size of the request in bytes
742  * @align: alignment of the region
743  * @goal: preferred starting address of the region
744  *
745  * The goal is dropped if it can not be satisfied and the allocation will
746  * fall back to memory below @goal.
747  *
748  * Allocation may happen on any node in the system.
749  *
750  * The function panics if the request can not be satisfied.
751  */
752 void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
753                                   unsigned long goal)
754 {
755         bootmem_data_t *bdata;
756         void *ptr;
757
758         list_for_each_entry(bdata, &bdata_list, list) {
759                 ptr = alloc_bootmem_core(bdata, size, align, goal,
760                                         ARCH_LOW_ADDRESS_LIMIT);
761                 if (ptr)
762                         return ptr;
763         }
764
765         /*
766          * Whoops, we cannot satisfy the allocation request.
767          */
768         printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size);
769         panic("Out of low memory");
770         return NULL;
771 }
772
773 /**
774  * __alloc_bootmem_low_node - allocate low boot memory from a specific node
775  * @pgdat: node to allocate from
776  * @size: size of the request in bytes
777  * @align: alignment of the region
778  * @goal: preferred starting address of the region
779  *
780  * The goal is dropped if it can not be satisfied and the allocation will
781  * fall back to memory below @goal.
782  *
783  * Allocation may fall back to any node in the system if the specified node
784  * can not hold the requested memory.
785  *
786  * The function panics if the request can not be satisfied.
787  */
788 void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
789                                        unsigned long align, unsigned long goal)
790 {
791         return alloc_bootmem_core(pgdat->bdata, size, align, goal,
792                                 ARCH_LOW_ADDRESS_LIMIT);
793 }