2 * bootmem - A boot-time physical memory allocator and configurator
4 * Copyright (C) 1999 Ingo Molnar
5 * 1999 Kanoj Sarcar, SGI
8 * Access to this subsystem has to be serialized externally (which is true
9 * for the boot process anyway).
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>
22 #include <asm/processor.h>
26 #ifndef CONFIG_NEED_MULTIPLE_NODES
27 struct pglist_data __refdata contig_page_data = {
28 .bdata = &bootmem_node_data[0]
30 EXPORT_SYMBOL(contig_page_data);
33 unsigned long max_low_pfn;
34 unsigned long min_low_pfn;
35 unsigned long max_pfn;
37 #ifdef CONFIG_CRASH_DUMP
39 * If we have booted due to a crash, max_pfn will be a very low value. We need
40 * to know the amount of memory that the previous kernel used.
42 unsigned long saved_max_pfn;
45 bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata;
47 static struct list_head bdata_list __initdata = LIST_HEAD_INIT(bdata_list);
49 static int bootmem_debug;
51 static int __init bootmem_debug_setup(char *buf)
56 early_param("bootmem_debug", bootmem_debug_setup);
58 #define bdebug(fmt, args...) ({ \
59 if (unlikely(bootmem_debug)) \
65 static unsigned long __init bootmap_bytes(unsigned long pages)
67 unsigned long bytes = (pages + 7) / 8;
69 return ALIGN(bytes, sizeof(long));
73 * bootmem_bootmap_pages - calculate bitmap size in pages
74 * @pages: number of pages the bitmap has to represent
76 unsigned long __init bootmem_bootmap_pages(unsigned long pages)
78 unsigned long bytes = bootmap_bytes(pages);
80 return PAGE_ALIGN(bytes) >> PAGE_SHIFT;
86 static void __init link_bootmem(bootmem_data_t *bdata)
88 struct list_head *iter;
90 list_for_each(iter, &bdata_list) {
93 ent = list_entry(iter, bootmem_data_t, list);
94 if (bdata->node_min_pfn < ent->node_min_pfn)
97 list_add_tail(&bdata->list, iter);
101 * Called once to set up the allocator itself.
103 static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
104 unsigned long mapstart, unsigned long start, unsigned long end)
106 unsigned long mapsize;
108 mminit_validate_memmodel_limits(&start, &end);
109 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
110 bdata->node_min_pfn = start;
111 bdata->node_low_pfn = end;
115 * Initially all pages are reserved - setup_arch() has to
116 * register free RAM areas explicitly.
118 mapsize = bootmap_bytes(end - start);
119 memset(bdata->node_bootmem_map, 0xff, mapsize);
121 bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n",
122 bdata - bootmem_node_data, start, mapstart, end, mapsize);
128 * init_bootmem_node - register a node as boot memory
129 * @pgdat: node to register
130 * @freepfn: pfn where the bitmap for this node is to be placed
131 * @startpfn: first pfn on the node
132 * @endpfn: first pfn after the node
134 * Returns the number of bytes needed to hold the bitmap for this node.
136 unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
137 unsigned long startpfn, unsigned long endpfn)
139 return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn);
143 * init_bootmem - register boot memory
144 * @start: pfn where the bitmap is to be placed
145 * @pages: number of available physical pages
147 * Returns the number of bytes needed to hold the bitmap.
149 unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
153 return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages);
157 * free_bootmem_late - free bootmem pages directly to page allocator
158 * @addr: starting address of the range
159 * @size: size of the range in bytes
161 * This is only useful when the bootmem allocator has already been torn
162 * down, but we are still initializing the system. Pages are given directly
163 * to the page allocator, no bootmem metadata is updated because it is gone.
165 void __init free_bootmem_late(unsigned long addr, unsigned long size)
167 unsigned long cursor, end;
169 kmemleak_free_part(__va(addr), size);
171 cursor = PFN_UP(addr);
172 end = PFN_DOWN(addr + size);
174 for (; cursor < end; cursor++) {
175 __free_pages_bootmem(pfn_to_page(cursor), 0);
180 static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
184 unsigned long start, end, pages, count = 0;
186 if (!bdata->node_bootmem_map)
189 start = bdata->node_min_pfn;
190 end = bdata->node_low_pfn;
193 * If the start is aligned to the machines wordsize, we might
194 * be able to free pages in bulks of that order.
196 aligned = !(start & (BITS_PER_LONG - 1));
198 bdebug("nid=%td start=%lx end=%lx aligned=%d\n",
199 bdata - bootmem_node_data, start, end, aligned);
201 while (start < end) {
202 unsigned long *map, idx, vec;
204 map = bdata->node_bootmem_map;
205 idx = start - bdata->node_min_pfn;
206 vec = ~map[idx / BITS_PER_LONG];
208 if (aligned && vec == ~0UL && start + BITS_PER_LONG < end) {
209 int order = ilog2(BITS_PER_LONG);
211 __free_pages_bootmem(pfn_to_page(start), order);
212 count += BITS_PER_LONG;
214 unsigned long off = 0;
216 while (vec && off < BITS_PER_LONG) {
218 page = pfn_to_page(start + off);
219 __free_pages_bootmem(page, 0);
226 start += BITS_PER_LONG;
229 page = virt_to_page(bdata->node_bootmem_map);
230 pages = bdata->node_low_pfn - bdata->node_min_pfn;
231 pages = bootmem_bootmap_pages(pages);
234 __free_pages_bootmem(page++, 0);
236 bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
242 * free_all_bootmem_node - release a node's free pages to the buddy allocator
243 * @pgdat: node to be released
245 * Returns the number of pages actually released.
247 unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
249 register_page_bootmem_info_node(pgdat);
250 return free_all_bootmem_core(pgdat->bdata);
254 * free_all_bootmem - release free pages to the buddy allocator
256 * Returns the number of pages actually released.
258 unsigned long __init free_all_bootmem(void)
260 unsigned long total_pages = 0;
261 bootmem_data_t *bdata;
263 list_for_each_entry(bdata, &bdata_list, list)
264 total_pages += free_all_bootmem_core(bdata);
269 static void __init __free(bootmem_data_t *bdata,
270 unsigned long sidx, unsigned long eidx)
274 bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
275 sidx + bdata->node_min_pfn,
276 eidx + bdata->node_min_pfn);
278 if (bdata->hint_idx > sidx)
279 bdata->hint_idx = sidx;
281 for (idx = sidx; idx < eidx; idx++)
282 if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
286 static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
287 unsigned long eidx, int flags)
290 int exclusive = flags & BOOTMEM_EXCLUSIVE;
292 bdebug("nid=%td start=%lx end=%lx flags=%x\n",
293 bdata - bootmem_node_data,
294 sidx + bdata->node_min_pfn,
295 eidx + bdata->node_min_pfn,
298 for (idx = sidx; idx < eidx; idx++)
299 if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
301 __free(bdata, sidx, idx);
304 bdebug("silent double reserve of PFN %lx\n",
305 idx + bdata->node_min_pfn);
310 static int __init mark_bootmem_node(bootmem_data_t *bdata,
311 unsigned long start, unsigned long end,
312 int reserve, int flags)
314 unsigned long sidx, eidx;
316 bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
317 bdata - bootmem_node_data, start, end, reserve, flags);
319 BUG_ON(start < bdata->node_min_pfn);
320 BUG_ON(end > bdata->node_low_pfn);
322 sidx = start - bdata->node_min_pfn;
323 eidx = end - bdata->node_min_pfn;
326 return __reserve(bdata, sidx, eidx, flags);
328 __free(bdata, sidx, eidx);
332 static int __init mark_bootmem(unsigned long start, unsigned long end,
333 int reserve, int flags)
336 bootmem_data_t *bdata;
339 list_for_each_entry(bdata, &bdata_list, list) {
343 if (pos < bdata->node_min_pfn ||
344 pos >= bdata->node_low_pfn) {
345 BUG_ON(pos != start);
349 max = min(bdata->node_low_pfn, end);
351 err = mark_bootmem_node(bdata, pos, max, reserve, flags);
352 if (reserve && err) {
353 mark_bootmem(start, pos, 0, 0);
359 pos = bdata->node_low_pfn;
365 * free_bootmem_node - mark a page range as usable
366 * @pgdat: node the range resides on
367 * @physaddr: starting address of the range
368 * @size: size of the range in bytes
370 * Partial pages will be considered reserved and left as they are.
372 * The range must reside completely on the specified node.
374 void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
377 unsigned long start, end;
379 kmemleak_free_part(__va(physaddr), size);
381 start = PFN_UP(physaddr);
382 end = PFN_DOWN(physaddr + size);
384 mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
388 * free_bootmem - mark a page range as usable
389 * @addr: starting address of the range
390 * @size: size of the range in bytes
392 * Partial pages will be considered reserved and left as they are.
394 * The range must be contiguous but may span node boundaries.
396 void __init free_bootmem(unsigned long addr, unsigned long size)
398 unsigned long start, end;
400 kmemleak_free_part(__va(addr), size);
402 start = PFN_UP(addr);
403 end = PFN_DOWN(addr + size);
405 mark_bootmem(start, end, 0, 0);
409 * reserve_bootmem_node - mark a page range as reserved
410 * @pgdat: node the range resides on
411 * @physaddr: starting address of the range
412 * @size: size of the range in bytes
413 * @flags: reservation flags (see linux/bootmem.h)
415 * Partial pages will be reserved.
417 * The range must reside completely on the specified node.
419 int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
420 unsigned long size, int flags)
422 unsigned long start, end;
424 start = PFN_DOWN(physaddr);
425 end = PFN_UP(physaddr + size);
427 return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
431 * reserve_bootmem - mark a page range as usable
432 * @addr: starting address of the range
433 * @size: size of the range in bytes
434 * @flags: reservation flags (see linux/bootmem.h)
436 * Partial pages will be reserved.
438 * The range must be contiguous but may span node boundaries.
440 int __init reserve_bootmem(unsigned long addr, unsigned long size,
443 unsigned long start, end;
445 start = PFN_DOWN(addr);
446 end = PFN_UP(addr + size);
448 return mark_bootmem(start, end, 1, flags);
451 int __weak __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
454 return reserve_bootmem(phys, len, flags);
457 static unsigned long __init align_idx(struct bootmem_data *bdata,
458 unsigned long idx, unsigned long step)
460 unsigned long base = bdata->node_min_pfn;
463 * Align the index with respect to the node start so that the
464 * combination of both satisfies the requested alignment.
467 return ALIGN(base + idx, step) - base;
470 static unsigned long __init align_off(struct bootmem_data *bdata,
471 unsigned long off, unsigned long align)
473 unsigned long base = PFN_PHYS(bdata->node_min_pfn);
475 /* Same as align_idx for byte offsets */
477 return ALIGN(base + off, align) - base;
480 static void * __init alloc_bootmem_core(struct bootmem_data *bdata,
481 unsigned long size, unsigned long align,
482 unsigned long goal, unsigned long limit)
484 unsigned long fallback = 0;
485 unsigned long min, max, start, sidx, midx, step;
487 bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
488 bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
492 BUG_ON(align & (align - 1));
493 BUG_ON(limit && goal + size > limit);
495 if (!bdata->node_bootmem_map)
498 min = bdata->node_min_pfn;
499 max = bdata->node_low_pfn;
502 limit >>= PAGE_SHIFT;
504 if (limit && max > limit)
509 step = max(align >> PAGE_SHIFT, 1UL);
511 if (goal && min < goal && goal < max)
512 start = ALIGN(goal, step);
514 start = ALIGN(min, step);
516 sidx = start - bdata->node_min_pfn;
517 midx = max - bdata->node_min_pfn;
519 if (bdata->hint_idx > sidx) {
521 * Handle the valid case of sidx being zero and still
522 * catch the fallback below.
525 sidx = align_idx(bdata, bdata->hint_idx, step);
531 unsigned long eidx, i, start_off, end_off;
533 sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
534 sidx = align_idx(bdata, sidx, step);
535 eidx = sidx + PFN_UP(size);
537 if (sidx >= midx || eidx > midx)
540 for (i = sidx; i < eidx; i++)
541 if (test_bit(i, bdata->node_bootmem_map)) {
542 sidx = align_idx(bdata, i, step);
548 if (bdata->last_end_off & (PAGE_SIZE - 1) &&
549 PFN_DOWN(bdata->last_end_off) + 1 == sidx)
550 start_off = align_off(bdata, bdata->last_end_off, align);
552 start_off = PFN_PHYS(sidx);
554 merge = PFN_DOWN(start_off) < sidx;
555 end_off = start_off + size;
557 bdata->last_end_off = end_off;
558 bdata->hint_idx = PFN_UP(end_off);
561 * Reserve the area now:
563 if (__reserve(bdata, PFN_DOWN(start_off) + merge,
564 PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
567 region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
569 memset(region, 0, size);
571 * The min_count is set to 0 so that bootmem allocated blocks
572 * are never reported as leaks.
574 kmemleak_alloc(region, size, 0, 0);
579 sidx = align_idx(bdata, fallback - 1, step);
587 static void * __init alloc_arch_preferred_bootmem(bootmem_data_t *bdata,
588 unsigned long size, unsigned long align,
589 unsigned long goal, unsigned long limit)
591 if (WARN_ON_ONCE(slab_is_available()))
592 return kzalloc(size, GFP_NOWAIT);
594 #ifdef CONFIG_HAVE_ARCH_BOOTMEM
596 bootmem_data_t *p_bdata;
598 p_bdata = bootmem_arch_preferred_node(bdata, size, align,
601 return alloc_bootmem_core(p_bdata, size, align,
608 static void * __init ___alloc_bootmem_nopanic(unsigned long size,
613 bootmem_data_t *bdata;
617 region = alloc_arch_preferred_bootmem(NULL, size, align, goal, limit);
621 list_for_each_entry(bdata, &bdata_list, list) {
622 if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
624 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
627 region = alloc_bootmem_core(bdata, size, align, goal, limit);
641 * __alloc_bootmem_nopanic - allocate boot memory without panicking
642 * @size: size of the request in bytes
643 * @align: alignment of the region
644 * @goal: preferred starting address of the region
646 * The goal is dropped if it can not be satisfied and the allocation will
647 * fall back to memory below @goal.
649 * Allocation may happen on any node in the system.
651 * Returns NULL on failure.
653 void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
656 unsigned long limit = 0;
658 return ___alloc_bootmem_nopanic(size, align, goal, limit);
661 static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
662 unsigned long goal, unsigned long limit)
664 void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
669 * Whoops, we cannot satisfy the allocation request.
671 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
672 panic("Out of memory");
677 * __alloc_bootmem - allocate boot memory
678 * @size: size of the request in bytes
679 * @align: alignment of the region
680 * @goal: preferred starting address of the region
682 * The goal is dropped if it can not be satisfied and the allocation will
683 * fall back to memory below @goal.
685 * Allocation may happen on any node in the system.
687 * The function panics if the request can not be satisfied.
689 void * __init __alloc_bootmem(unsigned long size, unsigned long align,
692 unsigned long limit = 0;
694 return ___alloc_bootmem(size, align, goal, limit);
697 static void * __init ___alloc_bootmem_node(bootmem_data_t *bdata,
698 unsigned long size, unsigned long align,
699 unsigned long goal, unsigned long limit)
703 ptr = alloc_arch_preferred_bootmem(bdata, size, align, goal, limit);
707 ptr = alloc_bootmem_core(bdata, size, align, goal, limit);
711 return ___alloc_bootmem(size, align, goal, limit);
715 * __alloc_bootmem_node - allocate boot memory from a specific node
716 * @pgdat: node to allocate from
717 * @size: size of the request in bytes
718 * @align: alignment of the region
719 * @goal: preferred starting address of the region
721 * The goal is dropped if it can not be satisfied and the allocation will
722 * fall back to memory below @goal.
724 * Allocation may fall back to any node in the system if the specified node
725 * can not hold the requested memory.
727 * The function panics if the request can not be satisfied.
729 void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
730 unsigned long align, unsigned long goal)
732 if (WARN_ON_ONCE(slab_is_available()))
733 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
735 return ___alloc_bootmem_node(pgdat->bdata, size, align, goal, 0);
738 void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
739 unsigned long align, unsigned long goal)
742 unsigned long end_pfn;
744 if (WARN_ON_ONCE(slab_is_available()))
745 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
747 /* update goal according ...MAX_DMA32_PFN */
748 end_pfn = pgdat->node_start_pfn + pgdat->node_spanned_pages;
750 if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
751 (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
753 unsigned long new_goal;
755 new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
756 ptr = alloc_bootmem_core(pgdat->bdata, size, align,
763 return __alloc_bootmem_node(pgdat, size, align, goal);
767 #ifdef CONFIG_SPARSEMEM
769 * alloc_bootmem_section - allocate boot memory from a specific section
770 * @size: size of the request in bytes
771 * @section_nr: sparse map section to allocate from
773 * Return NULL on failure.
775 void * __init alloc_bootmem_section(unsigned long size,
776 unsigned long section_nr)
778 bootmem_data_t *bdata;
779 unsigned long pfn, goal, limit;
781 pfn = section_nr_to_pfn(section_nr);
782 goal = pfn << PAGE_SHIFT;
783 limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
784 bdata = &bootmem_node_data[early_pfn_to_nid(pfn)];
786 return alloc_bootmem_core(bdata, size, SMP_CACHE_BYTES, goal, limit);
790 void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
791 unsigned long align, unsigned long goal)
795 if (WARN_ON_ONCE(slab_is_available()))
796 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
798 ptr = alloc_arch_preferred_bootmem(pgdat->bdata, size, align, goal, 0);
802 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
806 return __alloc_bootmem_nopanic(size, align, goal);
809 #ifndef ARCH_LOW_ADDRESS_LIMIT
810 #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
814 * __alloc_bootmem_low - allocate low boot memory
815 * @size: size of the request in bytes
816 * @align: alignment of the region
817 * @goal: preferred starting address of the region
819 * The goal is dropped if it can not be satisfied and the allocation will
820 * fall back to memory below @goal.
822 * Allocation may happen on any node in the system.
824 * The function panics if the request can not be satisfied.
826 void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
829 return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
833 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
834 * @pgdat: node to allocate from
835 * @size: size of the request in bytes
836 * @align: alignment of the region
837 * @goal: preferred starting address of the region
839 * The goal is dropped if it can not be satisfied and the allocation will
840 * fall back to memory below @goal.
842 * Allocation may fall back to any node in the system if the specified node
843 * can not hold the requested memory.
845 * The function panics if the request can not be satisfied.
847 void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
848 unsigned long align, unsigned long goal)
850 if (WARN_ON_ONCE(slab_is_available()))
851 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
853 return ___alloc_bootmem_node(pgdat->bdata, size, align,
854 goal, ARCH_LOW_ADDRESS_LIMIT);