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 unsigned long max_low_pfn;
27 unsigned long min_low_pfn;
28 unsigned long max_pfn;
30 #ifdef CONFIG_CRASH_DUMP
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.
35 unsigned long saved_max_pfn;
38 bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata;
40 static struct list_head bdata_list __initdata = LIST_HEAD_INIT(bdata_list);
42 static int bootmem_debug;
44 static int __init bootmem_debug_setup(char *buf)
49 early_param("bootmem_debug", bootmem_debug_setup);
51 #define bdebug(fmt, args...) ({ \
52 if (unlikely(bootmem_debug)) \
58 static unsigned long __init bootmap_bytes(unsigned long pages)
60 unsigned long bytes = (pages + 7) / 8;
62 return ALIGN(bytes, sizeof(long));
66 * bootmem_bootmap_pages - calculate bitmap size in pages
67 * @pages: number of pages the bitmap has to represent
69 unsigned long __init bootmem_bootmap_pages(unsigned long pages)
71 unsigned long bytes = bootmap_bytes(pages);
73 return PAGE_ALIGN(bytes) >> PAGE_SHIFT;
79 static void __init link_bootmem(bootmem_data_t *bdata)
81 struct list_head *iter;
83 list_for_each(iter, &bdata_list) {
86 ent = list_entry(iter, bootmem_data_t, list);
87 if (bdata->node_min_pfn < ent->node_min_pfn)
90 list_add_tail(&bdata->list, iter);
94 * Called once to set up the allocator itself.
96 static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
97 unsigned long mapstart, unsigned long start, unsigned long end)
99 unsigned long mapsize;
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;
108 * Initially all pages are reserved - setup_arch() has to
109 * register free RAM areas explicitly.
111 mapsize = bootmap_bytes(end - start);
112 memset(bdata->node_bootmem_map, 0xff, mapsize);
114 bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n",
115 bdata - bootmem_node_data, start, mapstart, end, mapsize);
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
127 * Returns the number of bytes needed to hold the bitmap for this node.
129 unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
130 unsigned long startpfn, unsigned long endpfn)
132 return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn);
136 * init_bootmem - register boot memory
137 * @start: pfn where the bitmap is to be placed
138 * @pages: number of available physical pages
140 * Returns the number of bytes needed to hold the bitmap.
142 unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
146 return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages);
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
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.
158 void __init free_bootmem_late(unsigned long addr, unsigned long size)
160 unsigned long cursor, end;
162 kmemleak_free_part(__va(addr), size);
164 cursor = PFN_UP(addr);
165 end = PFN_DOWN(addr + size);
167 for (; cursor < end; cursor++) {
168 __free_pages_bootmem(pfn_to_page(cursor), 0);
173 static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
177 unsigned long start, end, pages, count = 0;
179 if (!bdata->node_bootmem_map)
182 start = bdata->node_min_pfn;
183 end = bdata->node_low_pfn;
186 * If the start is aligned to the machines wordsize, we might
187 * be able to free pages in bulks of that order.
189 aligned = !(start & (BITS_PER_LONG - 1));
191 bdebug("nid=%td start=%lx end=%lx aligned=%d\n",
192 bdata - bootmem_node_data, start, end, aligned);
194 while (start < end) {
195 unsigned long *map, idx, vec;
197 map = bdata->node_bootmem_map;
198 idx = start - bdata->node_min_pfn;
199 vec = ~map[idx / BITS_PER_LONG];
201 if (aligned && vec == ~0UL && start + BITS_PER_LONG < end) {
202 int order = ilog2(BITS_PER_LONG);
204 __free_pages_bootmem(pfn_to_page(start), order);
205 count += BITS_PER_LONG;
207 unsigned long off = 0;
209 while (vec && off < BITS_PER_LONG) {
211 page = pfn_to_page(start + off);
212 __free_pages_bootmem(page, 0);
219 start += BITS_PER_LONG;
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);
227 __free_pages_bootmem(page++, 0);
229 bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
235 * free_all_bootmem_node - release a node's free pages to the buddy allocator
236 * @pgdat: node to be released
238 * Returns the number of pages actually released.
240 unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
242 register_page_bootmem_info_node(pgdat);
243 return free_all_bootmem_core(pgdat->bdata);
247 * free_all_bootmem - release free pages to the buddy allocator
249 * Returns the number of pages actually released.
251 unsigned long __init free_all_bootmem(void)
253 unsigned long total_pages = 0;
254 bootmem_data_t *bdata;
256 list_for_each_entry(bdata, &bdata_list, list)
257 total_pages += free_all_bootmem_core(bdata);
262 static void __init __free(bootmem_data_t *bdata,
263 unsigned long sidx, unsigned long eidx)
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);
271 if (bdata->hint_idx > sidx)
272 bdata->hint_idx = sidx;
274 for (idx = sidx; idx < eidx; idx++)
275 if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
279 static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
280 unsigned long eidx, int flags)
283 int exclusive = flags & BOOTMEM_EXCLUSIVE;
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,
291 for (idx = sidx; idx < eidx; idx++)
292 if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
294 __free(bdata, sidx, idx);
297 bdebug("silent double reserve of PFN %lx\n",
298 idx + bdata->node_min_pfn);
303 static int __init mark_bootmem_node(bootmem_data_t *bdata,
304 unsigned long start, unsigned long end,
305 int reserve, int flags)
307 unsigned long sidx, eidx;
309 bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
310 bdata - bootmem_node_data, start, end, reserve, flags);
312 BUG_ON(start < bdata->node_min_pfn);
313 BUG_ON(end > bdata->node_low_pfn);
315 sidx = start - bdata->node_min_pfn;
316 eidx = end - bdata->node_min_pfn;
319 return __reserve(bdata, sidx, eidx, flags);
321 __free(bdata, sidx, eidx);
325 static int __init mark_bootmem(unsigned long start, unsigned long end,
326 int reserve, int flags)
329 bootmem_data_t *bdata;
332 list_for_each_entry(bdata, &bdata_list, list) {
336 if (pos < bdata->node_min_pfn ||
337 pos >= bdata->node_low_pfn) {
338 BUG_ON(pos != start);
342 max = min(bdata->node_low_pfn, end);
344 err = mark_bootmem_node(bdata, pos, max, reserve, flags);
345 if (reserve && err) {
346 mark_bootmem(start, pos, 0, 0);
352 pos = bdata->node_low_pfn;
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
363 * Partial pages will be considered reserved and left as they are.
365 * The range must reside completely on the specified node.
367 void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
370 unsigned long start, end;
372 kmemleak_free_part(__va(physaddr), size);
374 start = PFN_UP(physaddr);
375 end = PFN_DOWN(physaddr + size);
377 mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
381 * free_bootmem - mark a page range as usable
382 * @addr: starting address of the range
383 * @size: size of the range in bytes
385 * Partial pages will be considered reserved and left as they are.
387 * The range must be contiguous but may span node boundaries.
389 void __init free_bootmem(unsigned long addr, unsigned long size)
391 unsigned long start, end;
393 kmemleak_free_part(__va(addr), size);
395 start = PFN_UP(addr);
396 end = PFN_DOWN(addr + size);
398 mark_bootmem(start, end, 0, 0);
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)
408 * Partial pages will be reserved.
410 * The range must reside completely on the specified node.
412 int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
413 unsigned long size, int flags)
415 unsigned long start, end;
417 start = PFN_DOWN(physaddr);
418 end = PFN_UP(physaddr + size);
420 return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
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)
429 * Partial pages will be reserved.
431 * The range must be contiguous but may span node boundaries.
433 int __init reserve_bootmem(unsigned long addr, unsigned long size,
436 unsigned long start, end;
438 start = PFN_DOWN(addr);
439 end = PFN_UP(addr + size);
441 return mark_bootmem(start, end, 1, flags);
444 int __weak __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
447 return reserve_bootmem(phys, len, flags);
450 static unsigned long __init align_idx(struct bootmem_data *bdata,
451 unsigned long idx, unsigned long step)
453 unsigned long base = bdata->node_min_pfn;
456 * Align the index with respect to the node start so that the
457 * combination of both satisfies the requested alignment.
460 return ALIGN(base + idx, step) - base;
463 static unsigned long __init align_off(struct bootmem_data *bdata,
464 unsigned long off, unsigned long align)
466 unsigned long base = PFN_PHYS(bdata->node_min_pfn);
468 /* Same as align_idx for byte offsets */
470 return ALIGN(base + off, align) - base;
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)
477 unsigned long fallback = 0;
478 unsigned long min, max, start, sidx, midx, step;
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,
485 BUG_ON(align & (align - 1));
486 BUG_ON(limit && goal + size > limit);
488 if (!bdata->node_bootmem_map)
491 min = bdata->node_min_pfn;
492 max = bdata->node_low_pfn;
495 limit >>= PAGE_SHIFT;
497 if (limit && max > limit)
502 step = max(align >> PAGE_SHIFT, 1UL);
504 if (goal && min < goal && goal < max)
505 start = ALIGN(goal, step);
507 start = ALIGN(min, step);
509 sidx = start - bdata->node_min_pfn;
510 midx = max - bdata->node_min_pfn;
512 if (bdata->hint_idx > sidx) {
514 * Handle the valid case of sidx being zero and still
515 * catch the fallback below.
518 sidx = align_idx(bdata, bdata->hint_idx, step);
524 unsigned long eidx, i, start_off, end_off;
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);
530 if (sidx >= midx || eidx > midx)
533 for (i = sidx; i < eidx; i++)
534 if (test_bit(i, bdata->node_bootmem_map)) {
535 sidx = align_idx(bdata, i, step);
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);
545 start_off = PFN_PHYS(sidx);
547 merge = PFN_DOWN(start_off) < sidx;
548 end_off = start_off + size;
550 bdata->last_end_off = end_off;
551 bdata->hint_idx = PFN_UP(end_off);
554 * Reserve the area now:
556 if (__reserve(bdata, PFN_DOWN(start_off) + merge,
557 PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
560 region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
562 memset(region, 0, size);
564 * The min_count is set to 0 so that bootmem allocated blocks
565 * are never reported as leaks.
567 kmemleak_alloc(region, size, 0, 0);
572 sidx = align_idx(bdata, fallback - 1, step);
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)
584 if (WARN_ON_ONCE(slab_is_available()))
585 return kzalloc(size, GFP_NOWAIT);
587 #ifdef CONFIG_HAVE_ARCH_BOOTMEM
589 bootmem_data_t *p_bdata;
591 p_bdata = bootmem_arch_preferred_node(bdata, size, align,
594 return alloc_bootmem_core(p_bdata, size, align,
601 static void * __init ___alloc_bootmem_nopanic(unsigned long size,
606 bootmem_data_t *bdata;
610 region = alloc_arch_preferred_bootmem(NULL, size, align, goal, limit);
614 list_for_each_entry(bdata, &bdata_list, list) {
615 if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
617 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
620 region = alloc_bootmem_core(bdata, size, align, goal, limit);
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
639 * The goal is dropped if it can not be satisfied and the allocation will
640 * fall back to memory below @goal.
642 * Allocation may happen on any node in the system.
644 * Returns NULL on failure.
646 void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
649 unsigned long limit = 0;
651 return ___alloc_bootmem_nopanic(size, align, goal, limit);
654 static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
655 unsigned long goal, unsigned long limit)
657 void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
662 * Whoops, we cannot satisfy the allocation request.
664 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
665 panic("Out of memory");
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
675 * The goal is dropped if it can not be satisfied and the allocation will
676 * fall back to memory below @goal.
678 * Allocation may happen on any node in the system.
680 * The function panics if the request can not be satisfied.
682 void * __init __alloc_bootmem(unsigned long size, unsigned long align,
685 unsigned long limit = 0;
687 return ___alloc_bootmem(size, align, goal, limit);
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)
696 ptr = alloc_arch_preferred_bootmem(bdata, size, align, goal, limit);
700 ptr = alloc_bootmem_core(bdata, size, align, goal, limit);
704 return ___alloc_bootmem(size, align, goal, limit);
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
714 * The goal is dropped if it can not be satisfied and the allocation will
715 * fall back to memory below @goal.
717 * Allocation may fall back to any node in the system if the specified node
718 * can not hold the requested memory.
720 * The function panics if the request can not be satisfied.
722 void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
723 unsigned long align, unsigned long goal)
725 if (WARN_ON_ONCE(slab_is_available()))
726 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
728 return ___alloc_bootmem_node(pgdat->bdata, size, align, goal, 0);
731 void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
732 unsigned long align, unsigned long goal)
735 unsigned long end_pfn;
737 if (WARN_ON_ONCE(slab_is_available()))
738 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
740 /* update goal according ...MAX_DMA32_PFN */
741 end_pfn = pgdat->node_start_pfn + pgdat->node_spanned_pages;
743 if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
744 (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
746 unsigned long new_goal;
748 new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
749 ptr = alloc_bootmem_core(pgdat->bdata, size, align,
756 return __alloc_bootmem_node(pgdat, size, align, goal);
760 #ifdef CONFIG_SPARSEMEM
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
766 * Return NULL on failure.
768 void * __init alloc_bootmem_section(unsigned long size,
769 unsigned long section_nr)
771 bootmem_data_t *bdata;
772 unsigned long pfn, goal, limit;
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)];
779 return alloc_bootmem_core(bdata, size, SMP_CACHE_BYTES, goal, limit);
783 void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
784 unsigned long align, unsigned long goal)
788 if (WARN_ON_ONCE(slab_is_available()))
789 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
791 ptr = alloc_arch_preferred_bootmem(pgdat->bdata, size, align, goal, 0);
795 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
799 return __alloc_bootmem_nopanic(size, align, goal);
802 #ifndef ARCH_LOW_ADDRESS_LIMIT
803 #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
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
812 * The goal is dropped if it can not be satisfied and the allocation will
813 * fall back to memory below @goal.
815 * Allocation may happen on any node in the system.
817 * The function panics if the request can not be satisfied.
819 void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
822 return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
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
832 * The goal is dropped if it can not be satisfied and the allocation will
833 * fall back to memory below @goal.
835 * Allocation may fall back to any node in the system if the specified node
836 * can not hold the requested memory.
838 * The function panics if the request can not be satisfied.
840 void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
841 unsigned long align, unsigned long goal)
843 if (WARN_ON_ONCE(slab_is_available()))
844 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
846 return ___alloc_bootmem_node(pgdat->bdata, size, align,
847 goal, ARCH_LOW_ADDRESS_LIMIT);