1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Bootmem core functions. 4 * 5 * Copyright (c) 2020, Bytedance. 6 * 7 * Author: Muchun Song <songmuchun@bytedance.com> 8 * 9 */ 10 #include <linux/mm.h> 11 #include <linux/compiler.h> 12 #include <linux/memblock.h> 13 #include <linux/bootmem_info.h> 14 #include <linux/memory_hotplug.h> 15 #include <linux/kmemleak.h> 16 17 void get_page_bootmem(unsigned long info, struct page *page, 18 enum bootmem_type type) 19 { 20 BUG_ON(type > 0xf); 21 BUG_ON(info > (ULONG_MAX >> 4)); 22 set_page_private(page, info << 4 | type); 23 page_ref_inc(page); 24 } 25 26 void put_page_bootmem(struct page *page) 27 { 28 enum bootmem_type type = bootmem_type(page); 29 30 BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE || 31 type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE); 32 33 if (page_ref_dec_return(page) == 1) { 34 set_page_private(page, 0); 35 free_reserved_page(page); 36 } 37 } 38 39 static void __init register_page_bootmem_info_section(unsigned long start_pfn) 40 { 41 unsigned long section_nr; 42 struct mem_section *ms; 43 44 start_pfn = SECTION_ALIGN_DOWN(start_pfn); 45 section_nr = pfn_to_section_nr(start_pfn); 46 ms = __nr_to_section(section_nr); 47 48 if (!preinited_vmemmap_section(ms)) 49 register_page_bootmem_memmap(section_nr, pfn_to_page(start_pfn), 50 PAGES_PER_SECTION); 51 } 52 53 void __init register_page_bootmem_info_node(struct pglist_data *pgdat) 54 { 55 unsigned long pfn, end_pfn; 56 int node = pgdat->node_id; 57 58 pfn = pgdat->node_start_pfn; 59 end_pfn = pgdat_end_pfn(pgdat); 60 61 /* register section info */ 62 for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 63 /* 64 * Some platforms can assign the same pfn to multiple nodes - on 65 * node0 as well as nodeN. To avoid registering a pfn against 66 * multiple nodes we check that this pfn does not already 67 * reside in some other nodes. 68 */ 69 if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node)) 70 register_page_bootmem_info_section(pfn); 71 } 72 } 73