1 // SPDX-License-Identifier: GPL-2.0
3 * Bootmem core functions.
5 * Copyright (c) 2020, Bytedance.
7 * Author: Muchun Song <songmuchun@bytedance.com>
11 #include <linux/compiler.h>
12 #include <linux/memblock.h>
13 #include <linux/bootmem_info.h>
14 #include <linux/memory_hotplug.h>
16 void get_page_bootmem(unsigned long info, struct page *page, unsigned long type)
20 set_page_private(page, info);
24 void put_page_bootmem(struct page *page)
26 unsigned long type = page->index;
28 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
29 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
31 if (page_ref_dec_return(page) == 1) {
33 ClearPagePrivate(page);
34 set_page_private(page, 0);
35 INIT_LIST_HEAD(&page->lru);
36 free_reserved_page(page);
40 #ifndef CONFIG_SPARSEMEM_VMEMMAP
41 static void __init register_page_bootmem_info_section(unsigned long start_pfn)
43 unsigned long mapsize, section_nr, i;
44 struct mem_section *ms;
45 struct page *page, *memmap;
46 struct mem_section_usage *usage;
48 section_nr = pfn_to_section_nr(start_pfn);
49 ms = __nr_to_section(section_nr);
51 /* Get section's memmap address */
52 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
55 * Get page for the memmap's phys address
56 * XXX: need more consideration for sparse_vmemmap...
58 page = virt_to_page(memmap);
59 mapsize = sizeof(struct page) * PAGES_PER_SECTION;
60 mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
62 /* remember memmap's page */
63 for (i = 0; i < mapsize; i++, page++)
64 get_page_bootmem(section_nr, page, SECTION_INFO);
67 page = virt_to_page(usage);
69 mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT;
71 for (i = 0; i < mapsize; i++, page++)
72 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
75 #else /* CONFIG_SPARSEMEM_VMEMMAP */
76 static void __init register_page_bootmem_info_section(unsigned long start_pfn)
78 unsigned long mapsize, section_nr, i;
79 struct mem_section *ms;
80 struct page *page, *memmap;
81 struct mem_section_usage *usage;
83 section_nr = pfn_to_section_nr(start_pfn);
84 ms = __nr_to_section(section_nr);
86 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
88 register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
91 page = virt_to_page(usage);
93 mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT;
95 for (i = 0; i < mapsize; i++, page++)
96 get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
98 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
100 void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
102 unsigned long i, pfn, end_pfn, nr_pages;
103 int node = pgdat->node_id;
106 nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
107 page = virt_to_page(pgdat);
109 for (i = 0; i < nr_pages; i++, page++)
110 get_page_bootmem(node, page, NODE_INFO);
112 pfn = pgdat->node_start_pfn;
113 end_pfn = pgdat_end_pfn(pgdat);
115 /* register section info */
116 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
118 * Some platforms can assign the same pfn to multiple nodes - on
119 * node0 as well as nodeN. To avoid registering a pfn against
120 * multiple nodes we check that this pfn does not already
121 * reside in some other nodes.
123 if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node))
124 register_page_bootmem_info_section(pfn);