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/export.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 EXPORT_SYMBOL(min_low_pfn);
36 unsigned long 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 = DIV_ROUND_UP(pages, 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)
83 list_for_each_entry(ent, &bdata_list, list) {
84 if (bdata->node_min_pfn < ent->node_min_pfn) {
85 list_add_tail(&bdata->list, &ent->list);
90 list_add_tail(&bdata->list, &bdata_list);
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 physical 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 physaddr, unsigned long size)
160 unsigned long cursor, end;
162 kmemleak_free_part(__va(physaddr), size);
164 cursor = PFN_UP(physaddr);
165 end = PFN_DOWN(physaddr + 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)
176 unsigned long start, end, pages, count = 0;
178 if (!bdata->node_bootmem_map)
181 start = bdata->node_min_pfn;
182 end = bdata->node_low_pfn;
184 bdebug("nid=%td start=%lx end=%lx\n",
185 bdata - bootmem_node_data, start, end);
187 while (start < end) {
188 unsigned long *map, idx, vec;
191 map = bdata->node_bootmem_map;
192 idx = start - bdata->node_min_pfn;
193 shift = idx & (BITS_PER_LONG - 1);
195 * vec holds at most BITS_PER_LONG map bits,
196 * bit 0 corresponds to start.
198 vec = ~map[idx / BITS_PER_LONG];
202 if (end - start >= BITS_PER_LONG)
203 vec |= ~map[idx / BITS_PER_LONG + 1] <<
204 (BITS_PER_LONG - shift);
207 * If we have a properly aligned and fully unreserved
208 * BITS_PER_LONG block of pages in front of us, free
211 if (IS_ALIGNED(start, BITS_PER_LONG) && vec == ~0UL) {
212 int order = ilog2(BITS_PER_LONG);
214 __free_pages_bootmem(pfn_to_page(start), order);
215 count += BITS_PER_LONG;
216 start += BITS_PER_LONG;
218 unsigned long cur = start;
220 start = ALIGN(start + 1, BITS_PER_LONG);
221 while (vec && cur != start) {
223 page = pfn_to_page(cur);
224 __free_pages_bootmem(page, 0);
233 page = virt_to_page(bdata->node_bootmem_map);
234 pages = bdata->node_low_pfn - bdata->node_min_pfn;
235 pages = bootmem_bootmap_pages(pages);
238 __free_pages_bootmem(page++, 0);
240 bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
245 static void reset_node_lowmem_managed_pages(pg_data_t *pgdat)
250 * In free_area_init_core(), highmem zone's managed_pages is set to
251 * present_pages, and bootmem allocator doesn't allocate from highmem
252 * zones. So there's no need to recalculate managed_pages because all
253 * highmem pages will be managed by the buddy system. Here highmem
254 * zone also includes highmem movable zone.
256 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
258 z->managed_pages = 0;
262 * free_all_bootmem_node - release a node's free pages to the buddy allocator
263 * @pgdat: node to be released
265 * Returns the number of pages actually released.
267 unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
269 register_page_bootmem_info_node(pgdat);
270 reset_node_lowmem_managed_pages(pgdat);
271 return free_all_bootmem_core(pgdat->bdata);
275 * free_all_bootmem - release free pages to the buddy allocator
277 * Returns the number of pages actually released.
279 unsigned long __init free_all_bootmem(void)
281 unsigned long total_pages = 0;
282 bootmem_data_t *bdata;
283 struct pglist_data *pgdat;
285 for_each_online_pgdat(pgdat)
286 reset_node_lowmem_managed_pages(pgdat);
288 list_for_each_entry(bdata, &bdata_list, list)
289 total_pages += free_all_bootmem_core(bdata);
294 static void __init __free(bootmem_data_t *bdata,
295 unsigned long sidx, unsigned long eidx)
299 bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
300 sidx + bdata->node_min_pfn,
301 eidx + bdata->node_min_pfn);
303 if (bdata->hint_idx > sidx)
304 bdata->hint_idx = sidx;
306 for (idx = sidx; idx < eidx; idx++)
307 if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
311 static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
312 unsigned long eidx, int flags)
315 int exclusive = flags & BOOTMEM_EXCLUSIVE;
317 bdebug("nid=%td start=%lx end=%lx flags=%x\n",
318 bdata - bootmem_node_data,
319 sidx + bdata->node_min_pfn,
320 eidx + bdata->node_min_pfn,
323 for (idx = sidx; idx < eidx; idx++)
324 if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
326 __free(bdata, sidx, idx);
329 bdebug("silent double reserve of PFN %lx\n",
330 idx + bdata->node_min_pfn);
335 static int __init mark_bootmem_node(bootmem_data_t *bdata,
336 unsigned long start, unsigned long end,
337 int reserve, int flags)
339 unsigned long sidx, eidx;
341 bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
342 bdata - bootmem_node_data, start, end, reserve, flags);
344 BUG_ON(start < bdata->node_min_pfn);
345 BUG_ON(end > bdata->node_low_pfn);
347 sidx = start - bdata->node_min_pfn;
348 eidx = end - bdata->node_min_pfn;
351 return __reserve(bdata, sidx, eidx, flags);
353 __free(bdata, sidx, eidx);
357 static int __init mark_bootmem(unsigned long start, unsigned long end,
358 int reserve, int flags)
361 bootmem_data_t *bdata;
364 list_for_each_entry(bdata, &bdata_list, list) {
368 if (pos < bdata->node_min_pfn ||
369 pos >= bdata->node_low_pfn) {
370 BUG_ON(pos != start);
374 max = min(bdata->node_low_pfn, end);
376 err = mark_bootmem_node(bdata, pos, max, reserve, flags);
377 if (reserve && err) {
378 mark_bootmem(start, pos, 0, 0);
384 pos = bdata->node_low_pfn;
390 * free_bootmem_node - mark a page range as usable
391 * @pgdat: node the range resides on
392 * @physaddr: starting address of the range
393 * @size: size of the range in bytes
395 * Partial pages will be considered reserved and left as they are.
397 * The range must reside completely on the specified node.
399 void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
402 unsigned long start, end;
404 kmemleak_free_part(__va(physaddr), size);
406 start = PFN_UP(physaddr);
407 end = PFN_DOWN(physaddr + size);
409 mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
413 * free_bootmem - mark a page range as usable
414 * @addr: starting physical address of the range
415 * @size: size of the range in bytes
417 * Partial pages will be considered reserved and left as they are.
419 * The range must be contiguous but may span node boundaries.
421 void __init free_bootmem(unsigned long physaddr, unsigned long size)
423 unsigned long start, end;
425 kmemleak_free_part(__va(physaddr), size);
427 start = PFN_UP(physaddr);
428 end = PFN_DOWN(physaddr + size);
430 mark_bootmem(start, end, 0, 0);
434 * reserve_bootmem_node - mark a page range as reserved
435 * @pgdat: node the range resides on
436 * @physaddr: starting address of the range
437 * @size: size of the range in bytes
438 * @flags: reservation flags (see linux/bootmem.h)
440 * Partial pages will be reserved.
442 * The range must reside completely on the specified node.
444 int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
445 unsigned long size, int flags)
447 unsigned long start, end;
449 start = PFN_DOWN(physaddr);
450 end = PFN_UP(physaddr + size);
452 return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
456 * reserve_bootmem - mark a page range as reserved
457 * @addr: starting address of the range
458 * @size: size of the range in bytes
459 * @flags: reservation flags (see linux/bootmem.h)
461 * Partial pages will be reserved.
463 * The range must be contiguous but may span node boundaries.
465 int __init reserve_bootmem(unsigned long addr, unsigned long size,
468 unsigned long start, end;
470 start = PFN_DOWN(addr);
471 end = PFN_UP(addr + size);
473 return mark_bootmem(start, end, 1, flags);
476 static unsigned long __init align_idx(struct bootmem_data *bdata,
477 unsigned long idx, unsigned long step)
479 unsigned long base = bdata->node_min_pfn;
482 * Align the index with respect to the node start so that the
483 * combination of both satisfies the requested alignment.
486 return ALIGN(base + idx, step) - base;
489 static unsigned long __init align_off(struct bootmem_data *bdata,
490 unsigned long off, unsigned long align)
492 unsigned long base = PFN_PHYS(bdata->node_min_pfn);
494 /* Same as align_idx for byte offsets */
496 return ALIGN(base + off, align) - base;
499 static void * __init alloc_bootmem_bdata(struct bootmem_data *bdata,
500 unsigned long size, unsigned long align,
501 unsigned long goal, unsigned long limit)
503 unsigned long fallback = 0;
504 unsigned long min, max, start, sidx, midx, step;
506 bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
507 bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
511 BUG_ON(align & (align - 1));
512 BUG_ON(limit && goal + size > limit);
514 if (!bdata->node_bootmem_map)
517 min = bdata->node_min_pfn;
518 max = bdata->node_low_pfn;
521 limit >>= PAGE_SHIFT;
523 if (limit && max > limit)
528 step = max(align >> PAGE_SHIFT, 1UL);
530 if (goal && min < goal && goal < max)
531 start = ALIGN(goal, step);
533 start = ALIGN(min, step);
535 sidx = start - bdata->node_min_pfn;
536 midx = max - bdata->node_min_pfn;
538 if (bdata->hint_idx > sidx) {
540 * Handle the valid case of sidx being zero and still
541 * catch the fallback below.
544 sidx = align_idx(bdata, bdata->hint_idx, step);
550 unsigned long eidx, i, start_off, end_off;
552 sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
553 sidx = align_idx(bdata, sidx, step);
554 eidx = sidx + PFN_UP(size);
556 if (sidx >= midx || eidx > midx)
559 for (i = sidx; i < eidx; i++)
560 if (test_bit(i, bdata->node_bootmem_map)) {
561 sidx = align_idx(bdata, i, step);
567 if (bdata->last_end_off & (PAGE_SIZE - 1) &&
568 PFN_DOWN(bdata->last_end_off) + 1 == sidx)
569 start_off = align_off(bdata, bdata->last_end_off, align);
571 start_off = PFN_PHYS(sidx);
573 merge = PFN_DOWN(start_off) < sidx;
574 end_off = start_off + size;
576 bdata->last_end_off = end_off;
577 bdata->hint_idx = PFN_UP(end_off);
580 * Reserve the area now:
582 if (__reserve(bdata, PFN_DOWN(start_off) + merge,
583 PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
586 region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
588 memset(region, 0, size);
590 * The min_count is set to 0 so that bootmem allocated blocks
591 * are never reported as leaks.
593 kmemleak_alloc(region, size, 0, 0);
598 sidx = align_idx(bdata, fallback - 1, step);
606 static void * __init alloc_bootmem_core(unsigned long size,
611 bootmem_data_t *bdata;
614 if (WARN_ON_ONCE(slab_is_available()))
615 return kzalloc(size, GFP_NOWAIT);
617 list_for_each_entry(bdata, &bdata_list, list) {
618 if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
620 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
623 region = alloc_bootmem_bdata(bdata, size, align, goal, limit);
631 static void * __init ___alloc_bootmem_nopanic(unsigned long size,
639 ptr = alloc_bootmem_core(size, align, goal, limit);
651 * __alloc_bootmem_nopanic - allocate boot memory without panicking
652 * @size: size of the request in bytes
653 * @align: alignment of the region
654 * @goal: preferred starting address of the region
656 * The goal is dropped if it can not be satisfied and the allocation will
657 * fall back to memory below @goal.
659 * Allocation may happen on any node in the system.
661 * Returns NULL on failure.
663 void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
666 unsigned long limit = 0;
668 return ___alloc_bootmem_nopanic(size, align, goal, limit);
671 static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
672 unsigned long goal, unsigned long limit)
674 void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
679 * Whoops, we cannot satisfy the allocation request.
681 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
682 panic("Out of memory");
687 * __alloc_bootmem - allocate boot memory
688 * @size: size of the request in bytes
689 * @align: alignment of the region
690 * @goal: preferred starting address of the region
692 * The goal is dropped if it can not be satisfied and the allocation will
693 * fall back to memory below @goal.
695 * Allocation may happen on any node in the system.
697 * The function panics if the request can not be satisfied.
699 void * __init __alloc_bootmem(unsigned long size, unsigned long align,
702 unsigned long limit = 0;
704 return ___alloc_bootmem(size, align, goal, limit);
707 void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
708 unsigned long size, unsigned long align,
709 unsigned long goal, unsigned long limit)
713 if (WARN_ON_ONCE(slab_is_available()))
714 return kzalloc(size, GFP_NOWAIT);
717 /* do not panic in alloc_bootmem_bdata() */
718 if (limit && goal + size > limit)
721 ptr = alloc_bootmem_bdata(pgdat->bdata, size, align, goal, limit);
725 ptr = alloc_bootmem_core(size, align, goal, limit);
737 void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
738 unsigned long align, unsigned long goal)
740 if (WARN_ON_ONCE(slab_is_available()))
741 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
743 return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
746 void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
747 unsigned long align, unsigned long goal,
752 ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
756 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
757 panic("Out of memory");
762 * __alloc_bootmem_node - allocate boot memory from a specific node
763 * @pgdat: node to allocate from
764 * @size: size of the request in bytes
765 * @align: alignment of the region
766 * @goal: preferred starting address of the region
768 * The goal is dropped if it can not be satisfied and the allocation will
769 * fall back to memory below @goal.
771 * Allocation may fall back to any node in the system if the specified node
772 * can not hold the requested memory.
774 * The function panics if the request can not be satisfied.
776 void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
777 unsigned long align, unsigned long goal)
779 if (WARN_ON_ONCE(slab_is_available()))
780 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
782 return ___alloc_bootmem_node(pgdat, size, align, goal, 0);
785 void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
786 unsigned long align, unsigned long goal)
789 unsigned long end_pfn;
791 if (WARN_ON_ONCE(slab_is_available()))
792 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
794 /* update goal according ...MAX_DMA32_PFN */
795 end_pfn = pgdat->node_start_pfn + pgdat->node_spanned_pages;
797 if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
798 (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
800 unsigned long new_goal;
802 new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
803 ptr = alloc_bootmem_bdata(pgdat->bdata, size, align,
810 return __alloc_bootmem_node(pgdat, size, align, goal);
814 #ifndef ARCH_LOW_ADDRESS_LIMIT
815 #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
819 * __alloc_bootmem_low - allocate low boot memory
820 * @size: size of the request in bytes
821 * @align: alignment of the region
822 * @goal: preferred starting address of the region
824 * The goal is dropped if it can not be satisfied and the allocation will
825 * fall back to memory below @goal.
827 * Allocation may happen on any node in the system.
829 * The function panics if the request can not be satisfied.
831 void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
834 return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
837 void * __init __alloc_bootmem_low_nopanic(unsigned long size,
841 return ___alloc_bootmem_nopanic(size, align, goal,
842 ARCH_LOW_ADDRESS_LIMIT);
846 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
847 * @pgdat: node to allocate from
848 * @size: size of the request in bytes
849 * @align: alignment of the region
850 * @goal: preferred starting address of the region
852 * The goal is dropped if it can not be satisfied and the allocation will
853 * fall back to memory below @goal.
855 * Allocation may fall back to any node in the system if the specified node
856 * can not hold the requested memory.
858 * The function panics if the request can not be satisfied.
860 void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
861 unsigned long align, unsigned long goal)
863 if (WARN_ON_ONCE(slab_is_available()))
864 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
866 return ___alloc_bootmem_node(pgdat, size, align,
867 goal, ARCH_LOW_ADDRESS_LIMIT);