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>
19 #include <linux/bug.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
;
36 unsigned long long max_possible_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
), cursor
, 0);
173 static unsigned long __init
free_all_bootmem_core(bootmem_data_t
*bdata
)
176 unsigned long *map
, start
, end
, pages
, cur
, count
= 0;
178 if (!bdata
->node_bootmem_map
)
181 map
= bdata
->node_bootmem_map
;
182 start
= bdata
->node_min_pfn
;
183 end
= bdata
->node_low_pfn
;
185 bdebug("nid=%td start=%lx end=%lx\n",
186 bdata
- bootmem_node_data
, start
, end
);
188 while (start
< end
) {
189 unsigned long idx
, vec
;
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
), start
, order
);
215 count
+= BITS_PER_LONG
;
216 start
+= BITS_PER_LONG
;
220 start
= ALIGN(start
+ 1, BITS_PER_LONG
);
221 while (vec
&& cur
!= start
) {
223 page
= pfn_to_page(cur
);
224 __free_pages_bootmem(page
, cur
, 0);
233 cur
= bdata
->node_min_pfn
;
234 page
= virt_to_page(bdata
->node_bootmem_map
);
235 pages
= bdata
->node_low_pfn
- bdata
->node_min_pfn
;
236 pages
= bootmem_bootmap_pages(pages
);
239 __free_pages_bootmem(page
++, cur
++, 0);
240 bdata
->node_bootmem_map
= NULL
;
242 bdebug("nid=%td released=%lx\n", bdata
- bootmem_node_data
, count
);
247 static int reset_managed_pages_done __initdata
;
249 void reset_node_managed_pages(pg_data_t
*pgdat
)
253 for (z
= pgdat
->node_zones
; z
< pgdat
->node_zones
+ MAX_NR_ZONES
; z
++)
254 z
->managed_pages
= 0;
257 void __init
reset_all_zones_managed_pages(void)
259 struct pglist_data
*pgdat
;
261 if (reset_managed_pages_done
)
264 for_each_online_pgdat(pgdat
)
265 reset_node_managed_pages(pgdat
);
267 reset_managed_pages_done
= 1;
271 * free_all_bootmem - release free pages to the buddy allocator
273 * Returns the number of pages actually released.
275 unsigned long __init
free_all_bootmem(void)
277 unsigned long total_pages
= 0;
278 bootmem_data_t
*bdata
;
280 reset_all_zones_managed_pages();
282 list_for_each_entry(bdata
, &bdata_list
, list
)
283 total_pages
+= free_all_bootmem_core(bdata
);
285 totalram_pages
+= total_pages
;
290 static void __init
__free(bootmem_data_t
*bdata
,
291 unsigned long sidx
, unsigned long eidx
)
295 bdebug("nid=%td start=%lx end=%lx\n", bdata
- bootmem_node_data
,
296 sidx
+ bdata
->node_min_pfn
,
297 eidx
+ bdata
->node_min_pfn
);
299 if (WARN_ON(bdata
->node_bootmem_map
== NULL
))
302 if (bdata
->hint_idx
> sidx
)
303 bdata
->hint_idx
= sidx
;
305 for (idx
= sidx
; idx
< eidx
; idx
++)
306 if (!test_and_clear_bit(idx
, bdata
->node_bootmem_map
))
310 static int __init
__reserve(bootmem_data_t
*bdata
, unsigned long sidx
,
311 unsigned long eidx
, int flags
)
314 int exclusive
= flags
& BOOTMEM_EXCLUSIVE
;
316 bdebug("nid=%td start=%lx end=%lx flags=%x\n",
317 bdata
- bootmem_node_data
,
318 sidx
+ bdata
->node_min_pfn
,
319 eidx
+ bdata
->node_min_pfn
,
322 if (WARN_ON(bdata
->node_bootmem_map
== NULL
))
325 for (idx
= sidx
; idx
< eidx
; idx
++)
326 if (test_and_set_bit(idx
, bdata
->node_bootmem_map
)) {
328 __free(bdata
, sidx
, idx
);
331 bdebug("silent double reserve of PFN %lx\n",
332 idx
+ bdata
->node_min_pfn
);
337 static int __init
mark_bootmem_node(bootmem_data_t
*bdata
,
338 unsigned long start
, unsigned long end
,
339 int reserve
, int flags
)
341 unsigned long sidx
, eidx
;
343 bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
344 bdata
- bootmem_node_data
, start
, end
, reserve
, flags
);
346 BUG_ON(start
< bdata
->node_min_pfn
);
347 BUG_ON(end
> bdata
->node_low_pfn
);
349 sidx
= start
- bdata
->node_min_pfn
;
350 eidx
= end
- bdata
->node_min_pfn
;
353 return __reserve(bdata
, sidx
, eidx
, flags
);
355 __free(bdata
, sidx
, eidx
);
359 static int __init
mark_bootmem(unsigned long start
, unsigned long end
,
360 int reserve
, int flags
)
363 bootmem_data_t
*bdata
;
366 list_for_each_entry(bdata
, &bdata_list
, list
) {
370 if (pos
< bdata
->node_min_pfn
||
371 pos
>= bdata
->node_low_pfn
) {
372 BUG_ON(pos
!= start
);
376 max
= min(bdata
->node_low_pfn
, end
);
378 err
= mark_bootmem_node(bdata
, pos
, max
, reserve
, flags
);
379 if (reserve
&& err
) {
380 mark_bootmem(start
, pos
, 0, 0);
386 pos
= bdata
->node_low_pfn
;
392 * free_bootmem_node - mark a page range as usable
393 * @pgdat: node the range resides on
394 * @physaddr: starting address of the range
395 * @size: size of the range in bytes
397 * Partial pages will be considered reserved and left as they are.
399 * The range must reside completely on the specified node.
401 void __init
free_bootmem_node(pg_data_t
*pgdat
, unsigned long physaddr
,
404 unsigned long start
, end
;
406 kmemleak_free_part(__va(physaddr
), size
);
408 start
= PFN_UP(physaddr
);
409 end
= PFN_DOWN(physaddr
+ size
);
411 mark_bootmem_node(pgdat
->bdata
, start
, end
, 0, 0);
415 * free_bootmem - mark a page range as usable
416 * @addr: starting physical address of the range
417 * @size: size of the range in bytes
419 * Partial pages will be considered reserved and left as they are.
421 * The range must be contiguous but may span node boundaries.
423 void __init
free_bootmem(unsigned long physaddr
, unsigned long size
)
425 unsigned long start
, end
;
427 kmemleak_free_part(__va(physaddr
), size
);
429 start
= PFN_UP(physaddr
);
430 end
= PFN_DOWN(physaddr
+ size
);
432 mark_bootmem(start
, end
, 0, 0);
436 * reserve_bootmem_node - mark a page range as reserved
437 * @pgdat: node the range resides on
438 * @physaddr: starting address of the range
439 * @size: size of the range in bytes
440 * @flags: reservation flags (see linux/bootmem.h)
442 * Partial pages will be reserved.
444 * The range must reside completely on the specified node.
446 int __init
reserve_bootmem_node(pg_data_t
*pgdat
, unsigned long physaddr
,
447 unsigned long size
, int flags
)
449 unsigned long start
, end
;
451 start
= PFN_DOWN(physaddr
);
452 end
= PFN_UP(physaddr
+ size
);
454 return mark_bootmem_node(pgdat
->bdata
, start
, end
, 1, flags
);
458 * reserve_bootmem - mark a page range as reserved
459 * @addr: starting address of the range
460 * @size: size of the range in bytes
461 * @flags: reservation flags (see linux/bootmem.h)
463 * Partial pages will be reserved.
465 * The range must be contiguous but may span node boundaries.
467 int __init
reserve_bootmem(unsigned long addr
, unsigned long size
,
470 unsigned long start
, end
;
472 start
= PFN_DOWN(addr
);
473 end
= PFN_UP(addr
+ size
);
475 return mark_bootmem(start
, end
, 1, flags
);
478 static unsigned long __init
align_idx(struct bootmem_data
*bdata
,
479 unsigned long idx
, unsigned long step
)
481 unsigned long base
= bdata
->node_min_pfn
;
484 * Align the index with respect to the node start so that the
485 * combination of both satisfies the requested alignment.
488 return ALIGN(base
+ idx
, step
) - base
;
491 static unsigned long __init
align_off(struct bootmem_data
*bdata
,
492 unsigned long off
, unsigned long align
)
494 unsigned long base
= PFN_PHYS(bdata
->node_min_pfn
);
496 /* Same as align_idx for byte offsets */
498 return ALIGN(base
+ off
, align
) - base
;
501 static void * __init
alloc_bootmem_bdata(struct bootmem_data
*bdata
,
502 unsigned long size
, unsigned long align
,
503 unsigned long goal
, unsigned long limit
)
505 unsigned long fallback
= 0;
506 unsigned long min
, max
, start
, sidx
, midx
, step
;
508 bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
509 bdata
- bootmem_node_data
, size
, PAGE_ALIGN(size
) >> PAGE_SHIFT
,
513 BUG_ON(align
& (align
- 1));
514 BUG_ON(limit
&& goal
+ size
> limit
);
516 if (!bdata
->node_bootmem_map
)
519 min
= bdata
->node_min_pfn
;
520 max
= bdata
->node_low_pfn
;
523 limit
>>= PAGE_SHIFT
;
525 if (limit
&& max
> limit
)
530 step
= max(align
>> PAGE_SHIFT
, 1UL);
532 if (goal
&& min
< goal
&& goal
< max
)
533 start
= ALIGN(goal
, step
);
535 start
= ALIGN(min
, step
);
537 sidx
= start
- bdata
->node_min_pfn
;
538 midx
= max
- bdata
->node_min_pfn
;
540 if (bdata
->hint_idx
> sidx
) {
542 * Handle the valid case of sidx being zero and still
543 * catch the fallback below.
546 sidx
= align_idx(bdata
, bdata
->hint_idx
, step
);
552 unsigned long eidx
, i
, start_off
, end_off
;
554 sidx
= find_next_zero_bit(bdata
->node_bootmem_map
, midx
, sidx
);
555 sidx
= align_idx(bdata
, sidx
, step
);
556 eidx
= sidx
+ PFN_UP(size
);
558 if (sidx
>= midx
|| eidx
> midx
)
561 for (i
= sidx
; i
< eidx
; i
++)
562 if (test_bit(i
, bdata
->node_bootmem_map
)) {
563 sidx
= align_idx(bdata
, i
, step
);
569 if (bdata
->last_end_off
& (PAGE_SIZE
- 1) &&
570 PFN_DOWN(bdata
->last_end_off
) + 1 == sidx
)
571 start_off
= align_off(bdata
, bdata
->last_end_off
, align
);
573 start_off
= PFN_PHYS(sidx
);
575 merge
= PFN_DOWN(start_off
) < sidx
;
576 end_off
= start_off
+ size
;
578 bdata
->last_end_off
= end_off
;
579 bdata
->hint_idx
= PFN_UP(end_off
);
582 * Reserve the area now:
584 if (__reserve(bdata
, PFN_DOWN(start_off
) + merge
,
585 PFN_UP(end_off
), BOOTMEM_EXCLUSIVE
))
588 region
= phys_to_virt(PFN_PHYS(bdata
->node_min_pfn
) +
590 memset(region
, 0, size
);
592 * The min_count is set to 0 so that bootmem allocated blocks
593 * are never reported as leaks.
595 kmemleak_alloc(region
, size
, 0, 0);
600 sidx
= align_idx(bdata
, fallback
- 1, step
);
608 static void * __init
alloc_bootmem_core(unsigned long size
,
613 bootmem_data_t
*bdata
;
616 if (WARN_ON_ONCE(slab_is_available()))
617 return kzalloc(size
, GFP_NOWAIT
);
619 list_for_each_entry(bdata
, &bdata_list
, list
) {
620 if (goal
&& bdata
->node_low_pfn
<= PFN_DOWN(goal
))
622 if (limit
&& bdata
->node_min_pfn
>= PFN_DOWN(limit
))
625 region
= alloc_bootmem_bdata(bdata
, size
, align
, goal
, limit
);
633 static void * __init
___alloc_bootmem_nopanic(unsigned long size
,
641 ptr
= alloc_bootmem_core(size
, align
, goal
, limit
);
653 * __alloc_bootmem_nopanic - allocate boot memory without panicking
654 * @size: size of the request in bytes
655 * @align: alignment of the region
656 * @goal: preferred starting address of the region
658 * The goal is dropped if it can not be satisfied and the allocation will
659 * fall back to memory below @goal.
661 * Allocation may happen on any node in the system.
663 * Returns NULL on failure.
665 void * __init
__alloc_bootmem_nopanic(unsigned long size
, unsigned long align
,
668 unsigned long limit
= 0;
670 return ___alloc_bootmem_nopanic(size
, align
, goal
, limit
);
673 static void * __init
___alloc_bootmem(unsigned long size
, unsigned long align
,
674 unsigned long goal
, unsigned long limit
)
676 void *mem
= ___alloc_bootmem_nopanic(size
, align
, goal
, limit
);
681 * Whoops, we cannot satisfy the allocation request.
683 printk(KERN_ALERT
"bootmem alloc of %lu bytes failed!\n", size
);
684 panic("Out of memory");
689 * __alloc_bootmem - allocate boot memory
690 * @size: size of the request in bytes
691 * @align: alignment of the region
692 * @goal: preferred starting address of the region
694 * The goal is dropped if it can not be satisfied and the allocation will
695 * fall back to memory below @goal.
697 * Allocation may happen on any node in the system.
699 * The function panics if the request can not be satisfied.
701 void * __init
__alloc_bootmem(unsigned long size
, unsigned long align
,
704 unsigned long limit
= 0;
706 return ___alloc_bootmem(size
, align
, goal
, limit
);
709 void * __init
___alloc_bootmem_node_nopanic(pg_data_t
*pgdat
,
710 unsigned long size
, unsigned long align
,
711 unsigned long goal
, unsigned long limit
)
715 if (WARN_ON_ONCE(slab_is_available()))
716 return kzalloc(size
, GFP_NOWAIT
);
719 /* do not panic in alloc_bootmem_bdata() */
720 if (limit
&& goal
+ size
> limit
)
723 ptr
= alloc_bootmem_bdata(pgdat
->bdata
, size
, align
, goal
, limit
);
727 ptr
= alloc_bootmem_core(size
, align
, goal
, limit
);
739 void * __init
__alloc_bootmem_node_nopanic(pg_data_t
*pgdat
, unsigned long size
,
740 unsigned long align
, unsigned long goal
)
742 if (WARN_ON_ONCE(slab_is_available()))
743 return kzalloc_node(size
, GFP_NOWAIT
, pgdat
->node_id
);
745 return ___alloc_bootmem_node_nopanic(pgdat
, size
, align
, goal
, 0);
748 void * __init
___alloc_bootmem_node(pg_data_t
*pgdat
, unsigned long size
,
749 unsigned long align
, unsigned long goal
,
754 ptr
= ___alloc_bootmem_node_nopanic(pgdat
, size
, align
, goal
, 0);
758 printk(KERN_ALERT
"bootmem alloc of %lu bytes failed!\n", size
);
759 panic("Out of memory");
764 * __alloc_bootmem_node - allocate boot memory from a specific node
765 * @pgdat: node to allocate from
766 * @size: size of the request in bytes
767 * @align: alignment of the region
768 * @goal: preferred starting address of the region
770 * The goal is dropped if it can not be satisfied and the allocation will
771 * fall back to memory below @goal.
773 * Allocation may fall back to any node in the system if the specified node
774 * can not hold the requested memory.
776 * The function panics if the request can not be satisfied.
778 void * __init
__alloc_bootmem_node(pg_data_t
*pgdat
, unsigned long size
,
779 unsigned long align
, unsigned long goal
)
781 if (WARN_ON_ONCE(slab_is_available()))
782 return kzalloc_node(size
, GFP_NOWAIT
, pgdat
->node_id
);
784 return ___alloc_bootmem_node(pgdat
, size
, align
, goal
, 0);
787 void * __init
__alloc_bootmem_node_high(pg_data_t
*pgdat
, unsigned long size
,
788 unsigned long align
, unsigned long goal
)
791 unsigned long end_pfn
;
793 if (WARN_ON_ONCE(slab_is_available()))
794 return kzalloc_node(size
, GFP_NOWAIT
, pgdat
->node_id
);
796 /* update goal according ...MAX_DMA32_PFN */
797 end_pfn
= pgdat_end_pfn(pgdat
);
799 if (end_pfn
> MAX_DMA32_PFN
+ (128 >> (20 - PAGE_SHIFT
)) &&
800 (goal
>> PAGE_SHIFT
) < MAX_DMA32_PFN
) {
802 unsigned long new_goal
;
804 new_goal
= MAX_DMA32_PFN
<< PAGE_SHIFT
;
805 ptr
= alloc_bootmem_bdata(pgdat
->bdata
, size
, align
,
812 return __alloc_bootmem_node(pgdat
, size
, align
, goal
);
816 #ifndef ARCH_LOW_ADDRESS_LIMIT
817 #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
821 * __alloc_bootmem_low - allocate low boot memory
822 * @size: size of the request in bytes
823 * @align: alignment of the region
824 * @goal: preferred starting address of the region
826 * The goal is dropped if it can not be satisfied and the allocation will
827 * fall back to memory below @goal.
829 * Allocation may happen on any node in the system.
831 * The function panics if the request can not be satisfied.
833 void * __init
__alloc_bootmem_low(unsigned long size
, unsigned long align
,
836 return ___alloc_bootmem(size
, align
, goal
, ARCH_LOW_ADDRESS_LIMIT
);
839 void * __init
__alloc_bootmem_low_nopanic(unsigned long size
,
843 return ___alloc_bootmem_nopanic(size
, align
, goal
,
844 ARCH_LOW_ADDRESS_LIMIT
);
848 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
849 * @pgdat: node to allocate from
850 * @size: size of the request in bytes
851 * @align: alignment of the region
852 * @goal: preferred starting address of the region
854 * The goal is dropped if it can not be satisfied and the allocation will
855 * fall back to memory below @goal.
857 * Allocation may fall back to any node in the system if the specified node
858 * can not hold the requested memory.
860 * The function panics if the request can not be satisfied.
862 void * __init
__alloc_bootmem_low_node(pg_data_t
*pgdat
, unsigned long size
,
863 unsigned long align
, unsigned long goal
)
865 if (WARN_ON_ONCE(slab_is_available()))
866 return kzalloc_node(size
, GFP_NOWAIT
, pgdat
->node_id
);
868 return ___alloc_bootmem_node(pgdat
, size
, align
,
869 goal
, ARCH_LOW_ADDRESS_LIMIT
);