1 // SPDX-License-Identifier: GPL-2.0
3 * bootmem - A boot-time physical memory allocator and configurator
5 * Copyright (C) 1999 Ingo Molnar
6 * 1999 Kanoj Sarcar, SGI
9 * Access to this subsystem has to be serialized externally (which is true
10 * for the boot process anyway).
12 #include <linux/init.h>
13 #include <linux/pfn.h>
14 #include <linux/slab.h>
15 #include <linux/export.h>
16 #include <linux/kmemleak.h>
17 #include <linux/range.h>
18 #include <linux/bug.h>
20 #include <linux/bootmem.h>
25 * DOC: bootmem overview
27 * Bootmem is a boot-time physical memory allocator and configurator.
29 * It is used early in the boot process before the page allocator is
32 * Bootmem is based on the most basic of allocators, a First Fit
33 * allocator which uses a bitmap to represent memory. If a bit is 1,
34 * the page is allocated and 0 if unallocated. To satisfy allocations
35 * of sizes smaller than a page, the allocator records the Page Frame
36 * Number (PFN) of the last allocation and the offset the allocation
37 * ended at. Subsequent small allocations are merged together and
38 * stored on the same page.
40 * The information used by the bootmem allocator is represented by
41 * :c:type:`struct bootmem_data`. An array to hold up to %MAX_NUMNODES
42 * such structures is statically allocated and then it is discarded
43 * when the system initialization completes. Each entry in this array
44 * corresponds to a node with memory. For UMA systems only entry 0 is
47 * The bootmem allocator is initialized during early architecture
48 * specific setup. Each architecture is required to supply a
49 * :c:func:`setup_arch` function which, among other tasks, is
50 * responsible for acquiring the necessary parameters to initialise
51 * the boot memory allocator. These parameters define limits of usable
54 * * @min_low_pfn - the lowest PFN that is available in the system
55 * * @max_low_pfn - the highest PFN that may be addressed by low
56 * memory (%ZONE_NORMAL)
57 * * @max_pfn - the last PFN available to the system.
59 * After those limits are determined, the :c:func:`init_bootmem` or
60 * :c:func:`init_bootmem_node` function should be called to initialize
61 * the bootmem allocator. The UMA case should use the `init_bootmem`
62 * function. It will initialize ``contig_page_data`` structure that
63 * represents the only memory node in the system. In the NUMA case the
64 * `init_bootmem_node` function should be called to initialize the
65 * bootmem allocator for each node.
67 * Once the allocator is set up, it is possible to use either single
68 * node or NUMA variant of the allocation APIs.
71 #ifndef CONFIG_NEED_MULTIPLE_NODES
72 struct pglist_data __refdata contig_page_data = {
73 .bdata = &bootmem_node_data[0]
75 EXPORT_SYMBOL(contig_page_data);
78 unsigned long max_low_pfn;
79 unsigned long min_low_pfn;
80 unsigned long max_pfn;
81 unsigned long long max_possible_pfn;
83 bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata;
85 static struct list_head bdata_list __initdata = LIST_HEAD_INIT(bdata_list);
87 static int bootmem_debug;
89 static int __init bootmem_debug_setup(char *buf)
94 early_param("bootmem_debug", bootmem_debug_setup);
96 #define bdebug(fmt, args...) ({ \
97 if (unlikely(bootmem_debug)) \
98 pr_info("bootmem::%s " fmt, \
102 static unsigned long __init bootmap_bytes(unsigned long pages)
104 unsigned long bytes = DIV_ROUND_UP(pages, BITS_PER_BYTE);
106 return ALIGN(bytes, sizeof(long));
110 * bootmem_bootmap_pages - calculate bitmap size in pages
111 * @pages: number of pages the bitmap has to represent
113 * Return: the number of pages needed to hold the bitmap.
115 unsigned long __init bootmem_bootmap_pages(unsigned long pages)
117 unsigned long bytes = bootmap_bytes(pages);
119 return PAGE_ALIGN(bytes) >> PAGE_SHIFT;
123 * link bdata in order
125 static void __init link_bootmem(bootmem_data_t *bdata)
129 list_for_each_entry(ent, &bdata_list, list) {
130 if (bdata->node_min_pfn < ent->node_min_pfn) {
131 list_add_tail(&bdata->list, &ent->list);
136 list_add_tail(&bdata->list, &bdata_list);
140 * Called once to set up the allocator itself.
142 static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
143 unsigned long mapstart, unsigned long start, unsigned long end)
145 unsigned long mapsize;
147 mminit_validate_memmodel_limits(&start, &end);
148 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
149 bdata->node_min_pfn = start;
150 bdata->node_low_pfn = end;
154 * Initially all pages are reserved - setup_arch() has to
155 * register free RAM areas explicitly.
157 mapsize = bootmap_bytes(end - start);
158 memset(bdata->node_bootmem_map, 0xff, mapsize);
160 bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n",
161 bdata - bootmem_node_data, start, mapstart, end, mapsize);
167 * init_bootmem_node - register a node as boot memory
168 * @pgdat: node to register
169 * @freepfn: pfn where the bitmap for this node is to be placed
170 * @startpfn: first pfn on the node
171 * @endpfn: first pfn after the node
173 * Return: the number of bytes needed to hold the bitmap for this node.
175 unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
176 unsigned long startpfn, unsigned long endpfn)
178 return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn);
182 * init_bootmem - register boot memory
183 * @start: pfn where the bitmap is to be placed
184 * @pages: number of available physical pages
186 * Return: the number of bytes needed to hold the bitmap.
188 unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
192 return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages);
195 void __init free_bootmem_late(unsigned long physaddr, unsigned long size)
197 unsigned long cursor, end;
199 kmemleak_free_part_phys(physaddr, size);
201 cursor = PFN_UP(physaddr);
202 end = PFN_DOWN(physaddr + size);
204 for (; cursor < end; cursor++) {
205 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
210 static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
213 unsigned long *map, start, end, pages, cur, count = 0;
215 if (!bdata->node_bootmem_map)
218 map = bdata->node_bootmem_map;
219 start = bdata->node_min_pfn;
220 end = bdata->node_low_pfn;
222 bdebug("nid=%td start=%lx end=%lx\n",
223 bdata - bootmem_node_data, start, end);
225 while (start < end) {
226 unsigned long idx, vec;
229 idx = start - bdata->node_min_pfn;
230 shift = idx & (BITS_PER_LONG - 1);
232 * vec holds at most BITS_PER_LONG map bits,
233 * bit 0 corresponds to start.
235 vec = ~map[idx / BITS_PER_LONG];
239 if (end - start >= BITS_PER_LONG)
240 vec |= ~map[idx / BITS_PER_LONG + 1] <<
241 (BITS_PER_LONG - shift);
244 * If we have a properly aligned and fully unreserved
245 * BITS_PER_LONG block of pages in front of us, free
248 if (IS_ALIGNED(start, BITS_PER_LONG) && vec == ~0UL) {
249 int order = ilog2(BITS_PER_LONG);
251 __free_pages_bootmem(pfn_to_page(start), start, order);
252 count += BITS_PER_LONG;
253 start += BITS_PER_LONG;
257 start = ALIGN(start + 1, BITS_PER_LONG);
258 while (vec && cur != start) {
260 page = pfn_to_page(cur);
261 __free_pages_bootmem(page, cur, 0);
270 cur = bdata->node_min_pfn;
271 page = virt_to_page(bdata->node_bootmem_map);
272 pages = bdata->node_low_pfn - bdata->node_min_pfn;
273 pages = bootmem_bootmap_pages(pages);
276 __free_pages_bootmem(page++, cur++, 0);
277 bdata->node_bootmem_map = NULL;
279 bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
284 static int reset_managed_pages_done __initdata;
286 void reset_node_managed_pages(pg_data_t *pgdat)
290 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
291 z->managed_pages = 0;
294 void __init reset_all_zones_managed_pages(void)
296 struct pglist_data *pgdat;
298 if (reset_managed_pages_done)
301 for_each_online_pgdat(pgdat)
302 reset_node_managed_pages(pgdat);
304 reset_managed_pages_done = 1;
307 unsigned long __init free_all_bootmem(void)
309 unsigned long total_pages = 0;
310 bootmem_data_t *bdata;
312 reset_all_zones_managed_pages();
314 list_for_each_entry(bdata, &bdata_list, list)
315 total_pages += free_all_bootmem_core(bdata);
317 totalram_pages += total_pages;
322 static void __init __free(bootmem_data_t *bdata,
323 unsigned long sidx, unsigned long eidx)
327 bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
328 sidx + bdata->node_min_pfn,
329 eidx + bdata->node_min_pfn);
331 if (WARN_ON(bdata->node_bootmem_map == NULL))
334 if (bdata->hint_idx > sidx)
335 bdata->hint_idx = sidx;
337 for (idx = sidx; idx < eidx; idx++)
338 if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
342 static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
343 unsigned long eidx, int flags)
346 int exclusive = flags & BOOTMEM_EXCLUSIVE;
348 bdebug("nid=%td start=%lx end=%lx flags=%x\n",
349 bdata - bootmem_node_data,
350 sidx + bdata->node_min_pfn,
351 eidx + bdata->node_min_pfn,
354 if (WARN_ON(bdata->node_bootmem_map == NULL))
357 for (idx = sidx; idx < eidx; idx++)
358 if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
360 __free(bdata, sidx, idx);
363 bdebug("silent double reserve of PFN %lx\n",
364 idx + bdata->node_min_pfn);
369 static int __init mark_bootmem_node(bootmem_data_t *bdata,
370 unsigned long start, unsigned long end,
371 int reserve, int flags)
373 unsigned long sidx, eidx;
375 bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
376 bdata - bootmem_node_data, start, end, reserve, flags);
378 BUG_ON(start < bdata->node_min_pfn);
379 BUG_ON(end > bdata->node_low_pfn);
381 sidx = start - bdata->node_min_pfn;
382 eidx = end - bdata->node_min_pfn;
385 return __reserve(bdata, sidx, eidx, flags);
387 __free(bdata, sidx, eidx);
391 static int __init mark_bootmem(unsigned long start, unsigned long end,
392 int reserve, int flags)
395 bootmem_data_t *bdata;
398 list_for_each_entry(bdata, &bdata_list, list) {
402 if (pos < bdata->node_min_pfn ||
403 pos >= bdata->node_low_pfn) {
404 BUG_ON(pos != start);
408 max = min(bdata->node_low_pfn, end);
410 err = mark_bootmem_node(bdata, pos, max, reserve, flags);
411 if (reserve && err) {
412 mark_bootmem(start, pos, 0, 0);
418 pos = bdata->node_low_pfn;
423 void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
426 unsigned long start, end;
428 kmemleak_free_part_phys(physaddr, size);
430 start = PFN_UP(physaddr);
431 end = PFN_DOWN(physaddr + size);
433 mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
436 void __init free_bootmem(unsigned long physaddr, unsigned long size)
438 unsigned long start, end;
440 kmemleak_free_part_phys(physaddr, size);
442 start = PFN_UP(physaddr);
443 end = PFN_DOWN(physaddr + size);
445 mark_bootmem(start, end, 0, 0);
449 * reserve_bootmem_node - mark a page range as reserved
450 * @pgdat: node the range resides on
451 * @physaddr: starting address of the range
452 * @size: size of the range in bytes
453 * @flags: reservation flags (see linux/bootmem.h)
455 * Partial pages will be reserved.
457 * The range must reside completely on the specified node.
459 * Return: 0 on success, -errno on failure.
461 int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
462 unsigned long size, int flags)
464 unsigned long start, end;
466 start = PFN_DOWN(physaddr);
467 end = PFN_UP(physaddr + size);
469 return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
473 * reserve_bootmem - mark a page range as reserved
474 * @addr: starting address of the range
475 * @size: size of the range in bytes
476 * @flags: reservation flags (see linux/bootmem.h)
478 * Partial pages will be reserved.
480 * The range must be contiguous but may span node boundaries.
482 * Return: 0 on success, -errno on failure.
484 int __init reserve_bootmem(unsigned long addr, unsigned long size,
487 unsigned long start, end;
489 start = PFN_DOWN(addr);
490 end = PFN_UP(addr + size);
492 return mark_bootmem(start, end, 1, flags);
495 static unsigned long __init align_idx(struct bootmem_data *bdata,
496 unsigned long idx, unsigned long step)
498 unsigned long base = bdata->node_min_pfn;
501 * Align the index with respect to the node start so that the
502 * combination of both satisfies the requested alignment.
505 return ALIGN(base + idx, step) - base;
508 static unsigned long __init align_off(struct bootmem_data *bdata,
509 unsigned long off, unsigned long align)
511 unsigned long base = PFN_PHYS(bdata->node_min_pfn);
513 /* Same as align_idx for byte offsets */
515 return ALIGN(base + off, align) - base;
518 static void * __init alloc_bootmem_bdata(struct bootmem_data *bdata,
519 unsigned long size, unsigned long align,
520 unsigned long goal, unsigned long limit)
522 unsigned long fallback = 0;
523 unsigned long min, max, start, sidx, midx, step;
525 bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
526 bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
530 BUG_ON(align & (align - 1));
531 BUG_ON(limit && goal + size > limit);
533 if (!bdata->node_bootmem_map)
536 min = bdata->node_min_pfn;
537 max = bdata->node_low_pfn;
540 limit >>= PAGE_SHIFT;
542 if (limit && max > limit)
547 step = max(align >> PAGE_SHIFT, 1UL);
549 if (goal && min < goal && goal < max)
550 start = ALIGN(goal, step);
552 start = ALIGN(min, step);
554 sidx = start - bdata->node_min_pfn;
555 midx = max - bdata->node_min_pfn;
557 if (bdata->hint_idx > sidx) {
559 * Handle the valid case of sidx being zero and still
560 * catch the fallback below.
563 sidx = align_idx(bdata, bdata->hint_idx, step);
569 unsigned long eidx, i, start_off, end_off;
571 sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
572 sidx = align_idx(bdata, sidx, step);
573 eidx = sidx + PFN_UP(size);
575 if (sidx >= midx || eidx > midx)
578 for (i = sidx; i < eidx; i++)
579 if (test_bit(i, bdata->node_bootmem_map)) {
580 sidx = align_idx(bdata, i, step);
586 if (bdata->last_end_off & (PAGE_SIZE - 1) &&
587 PFN_DOWN(bdata->last_end_off) + 1 == sidx)
588 start_off = align_off(bdata, bdata->last_end_off, align);
590 start_off = PFN_PHYS(sidx);
592 merge = PFN_DOWN(start_off) < sidx;
593 end_off = start_off + size;
595 bdata->last_end_off = end_off;
596 bdata->hint_idx = PFN_UP(end_off);
599 * Reserve the area now:
601 if (__reserve(bdata, PFN_DOWN(start_off) + merge,
602 PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
605 region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
607 memset(region, 0, size);
609 * The min_count is set to 0 so that bootmem allocated blocks
610 * are never reported as leaks.
612 kmemleak_alloc(region, size, 0, 0);
617 sidx = align_idx(bdata, fallback - 1, step);
625 static void * __init alloc_bootmem_core(unsigned long size,
630 bootmem_data_t *bdata;
633 if (WARN_ON_ONCE(slab_is_available()))
634 return kzalloc(size, GFP_NOWAIT);
636 list_for_each_entry(bdata, &bdata_list, list) {
637 if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
639 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
642 region = alloc_bootmem_bdata(bdata, size, align, goal, limit);
650 static void * __init ___alloc_bootmem_nopanic(unsigned long size,
658 ptr = alloc_bootmem_core(size, align, goal, limit);
669 void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
672 unsigned long limit = 0;
674 return ___alloc_bootmem_nopanic(size, align, goal, limit);
677 static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
678 unsigned long goal, unsigned long limit)
680 void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
685 * Whoops, we cannot satisfy the allocation request.
687 pr_alert("bootmem alloc of %lu bytes failed!\n", size);
688 panic("Out of memory");
692 void * __init __alloc_bootmem(unsigned long size, unsigned long align,
695 unsigned long limit = 0;
697 return ___alloc_bootmem(size, align, goal, limit);
700 void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
701 unsigned long size, unsigned long align,
702 unsigned long goal, unsigned long limit)
706 if (WARN_ON_ONCE(slab_is_available()))
707 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
710 /* do not panic in alloc_bootmem_bdata() */
711 if (limit && goal + size > limit)
714 ptr = alloc_bootmem_bdata(pgdat->bdata, size, align, goal, limit);
718 ptr = alloc_bootmem_core(size, align, goal, limit);
730 void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
731 unsigned long align, unsigned long goal)
733 return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
736 void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
737 unsigned long align, unsigned long goal,
742 ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
746 pr_alert("bootmem alloc of %lu bytes failed!\n", size);
747 panic("Out of memory");
751 void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
752 unsigned long align, unsigned long goal)
754 if (WARN_ON_ONCE(slab_is_available()))
755 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
757 return ___alloc_bootmem_node(pgdat, size, align, goal, 0);
760 void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
761 unsigned long align, unsigned long goal)
764 unsigned long end_pfn;
766 if (WARN_ON_ONCE(slab_is_available()))
767 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
769 /* update goal according ...MAX_DMA32_PFN */
770 end_pfn = pgdat_end_pfn(pgdat);
772 if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
773 (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
775 unsigned long new_goal;
777 new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
778 ptr = alloc_bootmem_bdata(pgdat->bdata, size, align,
785 return __alloc_bootmem_node(pgdat, size, align, goal);
789 void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
792 return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
795 void * __init __alloc_bootmem_low_nopanic(unsigned long size,
799 return ___alloc_bootmem_nopanic(size, align, goal,
800 ARCH_LOW_ADDRESS_LIMIT);
803 void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
804 unsigned long align, unsigned long goal)
806 if (WARN_ON_ONCE(slab_is_available()))
807 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
809 return ___alloc_bootmem_node(pgdat, size, align,
810 goal, ARCH_LOW_ADDRESS_LIMIT);