^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * sparse memory mappings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/mmzone.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/swapops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/dma.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Permanent SPARSEMEM data:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * 1) mem_section - memory sections, mem_map's for valid memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #ifdef CONFIG_SPARSEMEM_EXTREME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct mem_section **mem_section;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) ____cacheline_internodealigned_in_smp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) EXPORT_SYMBOL(mem_section);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #ifdef NODE_NOT_IN_PAGE_FLAGS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * If we did not store the node number in the page then we have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * do a lookup in the section_to_node_table in order to find which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * node the page belongs to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #if MAX_NUMNODES <= 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static u8 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static u16 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int page_to_nid(const struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return section_to_node_table[page_to_section(page)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) EXPORT_SYMBOL(page_to_nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static void set_section_nid(unsigned long section_nr, int nid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) section_to_node_table[section_nr] = nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #else /* !NODE_NOT_IN_PAGE_FLAGS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static inline void set_section_nid(unsigned long section_nr, int nid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #ifdef CONFIG_SPARSEMEM_EXTREME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static noinline struct mem_section __ref *sparse_index_alloc(int nid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct mem_section *section = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) unsigned long array_size = SECTIONS_PER_ROOT *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) sizeof(struct mem_section);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (slab_is_available()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) section = kzalloc_node(array_size, GFP_KERNEL, nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) section = memblock_alloc_node(array_size, SMP_CACHE_BYTES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (!section)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) panic("%s: Failed to allocate %lu bytes nid=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) __func__, array_size, nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return section;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static int __meminit sparse_index_init(unsigned long section_nr, int nid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) unsigned long root = SECTION_NR_TO_ROOT(section_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct mem_section *section;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * An existing section is possible in the sub-section hotplug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * case. First hot-add instantiates, follow-on hot-add reuses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * the existing section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * The mem_hotplug_lock resolves the apparent race below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (mem_section[root])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) section = sparse_index_alloc(nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (!section)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) mem_section[root] = section;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #else /* !SPARSEMEM_EXTREME */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static inline int sparse_index_init(unsigned long section_nr, int nid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #ifdef CONFIG_SPARSEMEM_EXTREME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) unsigned long __section_nr(struct mem_section *ms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) unsigned long root_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct mem_section *root = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) root = __nr_to_section(root_nr * SECTIONS_PER_ROOT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (!root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) VM_BUG_ON(!root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) unsigned long __section_nr(struct mem_section *ms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return (unsigned long)(ms - mem_section[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * During early boot, before section_mem_map is used for an actual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * mem_map, we use section_mem_map to store the section's NUMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * node. This keeps us from having to use another data structure. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * node information is cleared just before we store the real mem_map.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static inline unsigned long sparse_encode_early_nid(int nid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return (nid << SECTION_NID_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static inline int sparse_early_nid(struct mem_section *section)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return (section->section_mem_map >> SECTION_NID_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* Validate the physical addressing limitations of the model */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) void __meminit mminit_validate_memmodel_limits(unsigned long *start_pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) unsigned long *end_pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) unsigned long max_sparsemem_pfn = 1UL << (MAX_PHYSMEM_BITS-PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * Sanity checks - do not allow an architecture to pass
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * in larger pfns than the maximum scope of sparsemem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (*start_pfn > max_sparsemem_pfn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) "Start of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) *start_pfn, *end_pfn, max_sparsemem_pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) *start_pfn = max_sparsemem_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) *end_pfn = max_sparsemem_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) } else if (*end_pfn > max_sparsemem_pfn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) "End of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) *start_pfn, *end_pfn, max_sparsemem_pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) *end_pfn = max_sparsemem_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * There are a number of times that we loop over NR_MEM_SECTIONS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * looking for section_present() on each. But, when we have very
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * large physical address spaces, NR_MEM_SECTIONS can also be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * very large which makes the loops quite long.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * Keeping track of this gives us an easy way to break out of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * those loops early.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) unsigned long __highest_present_section_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static void section_mark_present(struct mem_section *ms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) unsigned long section_nr = __section_nr(ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (section_nr > __highest_present_section_nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) __highest_present_section_nr = section_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) ms->section_mem_map |= SECTION_MARKED_PRESENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) #define for_each_present_section_nr(start, section_nr) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) for (section_nr = next_present_section_nr(start-1); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) ((section_nr != -1) && \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) (section_nr <= __highest_present_section_nr)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) section_nr = next_present_section_nr(section_nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static inline unsigned long first_present_section_nr(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return next_present_section_nr(-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) #ifdef CONFIG_SPARSEMEM_VMEMMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static void subsection_mask_set(unsigned long *map, unsigned long pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) unsigned long nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) int idx = subsection_map_index(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) int end = subsection_map_index(pfn + nr_pages - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) bitmap_set(map, idx, end - idx + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) void __init subsection_map_init(unsigned long pfn, unsigned long nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) int end_sec = pfn_to_section_nr(pfn + nr_pages - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) unsigned long nr, start_sec = pfn_to_section_nr(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (!nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) for (nr = start_sec; nr <= end_sec; nr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct mem_section *ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) unsigned long pfns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) pfns = min(nr_pages, PAGES_PER_SECTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) - (pfn & ~PAGE_SECTION_MASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ms = __nr_to_section(nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) subsection_mask_set(ms->usage->subsection_map, pfn, pfns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) pr_debug("%s: sec: %lu pfns: %lu set(%d, %d)\n", __func__, nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) pfns, subsection_map_index(pfn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) subsection_map_index(pfn + pfns - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) pfn += pfns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) nr_pages -= pfns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) void __init subsection_map_init(unsigned long pfn, unsigned long nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) /* Record a memory area against a node. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static void __init memory_present(int nid, unsigned long start, unsigned long end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) unsigned long pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) #ifdef CONFIG_SPARSEMEM_EXTREME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (unlikely(!mem_section)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) unsigned long size, align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) size = sizeof(struct mem_section*) * NR_SECTION_ROOTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) align = 1 << (INTERNODE_CACHE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) mem_section = memblock_alloc(size, align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (!mem_section)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) __func__, size, align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) start &= PAGE_SECTION_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) mminit_validate_memmodel_limits(&start, &end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) unsigned long section = pfn_to_section_nr(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct mem_section *ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) sparse_index_init(section, nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) set_section_nid(section, nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) ms = __nr_to_section(section);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (!ms->section_mem_map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) ms->section_mem_map = sparse_encode_early_nid(nid) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) SECTION_IS_ONLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) section_mark_present(ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * Mark all memblocks as present using memory_present().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * This is a convenience function that is useful to mark all of the systems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * memory as present during initialization.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static void __init memblocks_present(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) unsigned long start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) int i, nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, &nid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) memory_present(nid, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * Subtle, we encode the real pfn into the mem_map such that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * the identity pfn - section_mem_map will return the actual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * physical page frame number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) unsigned long coded_mem_map =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) BUILD_BUG_ON(SECTION_MAP_LAST_BIT > (1UL<<PFN_SECTION_SHIFT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) BUG_ON(coded_mem_map & ~SECTION_MAP_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return coded_mem_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) #ifdef CONFIG_MEMORY_HOTPLUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * Decode mem_map from the coded memmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) /* mask off the extra low bits of information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) coded_mem_map &= SECTION_MAP_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) #endif /* CONFIG_MEMORY_HOTPLUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static void __meminit sparse_init_one_section(struct mem_section *ms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) unsigned long pnum, struct page *mem_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) struct mem_section_usage *usage, unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) ms->section_mem_map &= ~SECTION_MAP_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) | SECTION_HAS_MEM_MAP | flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) ms->usage = usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static unsigned long usemap_size(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) size_t mem_section_usage_size(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return sizeof(struct mem_section_usage) + usemap_size();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) #ifdef CONFIG_MEMORY_HOTREMOVE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static struct mem_section_usage * __init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) struct mem_section_usage *usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) unsigned long goal, limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) int nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * A page may contain usemaps for other sections preventing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * page being freed and making a section unremovable while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * other sections referencing the usemap remain active. Similarly,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * a pgdat can prevent a section being removed. If section A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * contains a pgdat and section B contains the usemap, both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * sections become inter-dependent. This allocates usemaps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * from the same section as the pgdat where possible to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * this problem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) goal = __pa(pgdat) & (PAGE_SECTION_MASK << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) limit = goal + (1UL << PA_SECTION_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) nid = early_pfn_to_nid(goal >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) usage = memblock_alloc_try_nid(size, SMP_CACHE_BYTES, goal, limit, nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (!usage && limit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) limit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) return usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) static void __init check_usemap_section_nr(int nid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) struct mem_section_usage *usage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) unsigned long usemap_snr, pgdat_snr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static unsigned long old_usemap_snr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) static unsigned long old_pgdat_snr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) struct pglist_data *pgdat = NODE_DATA(nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) int usemap_nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) /* First call */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (!old_usemap_snr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) old_usemap_snr = NR_MEM_SECTIONS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) old_pgdat_snr = NR_MEM_SECTIONS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) usemap_snr = pfn_to_section_nr(__pa(usage) >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) pgdat_snr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) if (usemap_snr == pgdat_snr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (old_usemap_snr == usemap_snr && old_pgdat_snr == pgdat_snr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) /* skip redundant message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) old_usemap_snr = usemap_snr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) old_pgdat_snr = pgdat_snr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) usemap_nid = sparse_early_nid(__nr_to_section(usemap_snr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (usemap_nid != nid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) pr_info("node %d must be removed before remove section %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) nid, usemap_snr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * There is a circular dependency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * Some platforms allow un-removable section because they will just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * gather other removable sections for dynamic partitioning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * Just notify un-removable section's number here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) pr_info("Section %ld and %ld (node %d) have a circular dependency on usemap and pgdat allocations\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) usemap_snr, pgdat_snr, nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static struct mem_section_usage * __init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) return memblock_alloc_node(size, SMP_CACHE_BYTES, pgdat->node_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) static void __init check_usemap_section_nr(int nid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) struct mem_section_usage *usage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) #endif /* CONFIG_MEMORY_HOTREMOVE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) #ifdef CONFIG_SPARSEMEM_VMEMMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) static unsigned long __init section_map_size(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) return ALIGN(sizeof(struct page) * PAGES_PER_SECTION, PMD_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static unsigned long __init section_map_size(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) return PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) struct page __init *__populate_section_memmap(unsigned long pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) unsigned long nr_pages, int nid, struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) unsigned long size = section_map_size();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) struct page *map = sparse_buffer_alloc(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) phys_addr_t addr = __pa(MAX_DMA_ADDRESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) if (map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) return map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) map = memblock_alloc_try_nid_raw(size, size, addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) MEMBLOCK_ALLOC_ACCESSIBLE, nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) if (!map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%pa\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) __func__, size, PAGE_SIZE, nid, &addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) static void *sparsemap_buf __meminitdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) static void *sparsemap_buf_end __meminitdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) static inline void __meminit sparse_buffer_free(unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) WARN_ON(!sparsemap_buf || size == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) memblock_free_early(__pa(sparsemap_buf), size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) static void __init sparse_buffer_init(unsigned long size, int nid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) phys_addr_t addr = __pa(MAX_DMA_ADDRESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) WARN_ON(sparsemap_buf); /* forgot to call sparse_buffer_fini()? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) * Pre-allocated buffer is mainly used by __populate_section_memmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * and we want it to be properly aligned to the section size - this is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * especially the case for VMEMMAP which maps memmap to PMDs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) sparsemap_buf = memblock_alloc_exact_nid_raw(size, section_map_size(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) addr, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) sparsemap_buf_end = sparsemap_buf + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) static void __init sparse_buffer_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) unsigned long size = sparsemap_buf_end - sparsemap_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (sparsemap_buf && size > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) sparse_buffer_free(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) sparsemap_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) void * __meminit sparse_buffer_alloc(unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) void *ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (sparsemap_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) ptr = (void *) roundup((unsigned long)sparsemap_buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) if (ptr + size > sparsemap_buf_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) /* Free redundant aligned space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if ((unsigned long)(ptr - sparsemap_buf) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) sparse_buffer_free((unsigned long)(ptr - sparsemap_buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) sparsemap_buf = ptr + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) return ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) void __weak __meminit vmemmap_populate_print_last(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) * Initialize sparse on a specific node. The node spans [pnum_begin, pnum_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) * And number of present sections in this node is map_count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) static void __init sparse_init_nid(int nid, unsigned long pnum_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) unsigned long pnum_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) unsigned long map_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) struct mem_section_usage *usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) unsigned long pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) struct page *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) usage = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) mem_section_usage_size() * map_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if (!usage) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) pr_err("%s: node[%d] usemap allocation failed", __func__, nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) sparse_buffer_init(map_count * section_map_size(), nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) for_each_present_section_nr(pnum_begin, pnum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) unsigned long pfn = section_nr_to_pfn(pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) if (pnum >= pnum_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) map = __populate_section_memmap(pfn, PAGES_PER_SECTION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) nid, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (!map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) pr_err("%s: node[%d] memory map backing failed. Some memory will not be available.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) __func__, nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) pnum_begin = pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) sparse_buffer_fini();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) check_usemap_section_nr(nid, usage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) sparse_init_one_section(__nr_to_section(pnum), pnum, map, usage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) SECTION_IS_EARLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) usage = (void *) usage + mem_section_usage_size();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) sparse_buffer_fini();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) /* We failed to allocate, mark all the following pnums as not present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) for_each_present_section_nr(pnum_begin, pnum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) struct mem_section *ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (pnum >= pnum_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) ms = __nr_to_section(pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) ms->section_mem_map = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * Allocate the accumulated non-linear sections, allocate a mem_map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * for each and record the physical to section mapping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) void __init sparse_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) unsigned long pnum_end, pnum_begin, map_count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) int nid_begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) memblocks_present();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) pnum_begin = first_present_section_nr();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) nid_begin = sparse_early_nid(__nr_to_section(pnum_begin));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) /* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) set_pageblock_order();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) for_each_present_section_nr(pnum_begin + 1, pnum_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) int nid = sparse_early_nid(__nr_to_section(pnum_end));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) if (nid == nid_begin) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) map_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) /* Init node with sections in range [pnum_begin, pnum_end) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) nid_begin = nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) pnum_begin = pnum_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) map_count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) /* cover the last node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) vmemmap_populate_print_last();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) #ifdef CONFIG_MEMORY_HOTPLUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) /* Mark all memory sections within the pfn range as online */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) unsigned long pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) unsigned long section_nr = pfn_to_section_nr(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) struct mem_section *ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) /* onlining code should never touch invalid ranges */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) if (WARN_ON(!valid_section_nr(section_nr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) ms = __nr_to_section(section_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) ms->section_mem_map |= SECTION_IS_ONLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) #ifdef CONFIG_MEMORY_HOTREMOVE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) /* Mark all memory sections within the pfn range as offline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) unsigned long pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) unsigned long section_nr = pfn_to_section_nr(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) struct mem_section *ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) * TODO this needs some double checking. Offlining code makes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) * sure to check pfn_valid but those checks might be just bogus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) if (WARN_ON(!valid_section_nr(section_nr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) ms = __nr_to_section(section_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) ms->section_mem_map &= ~SECTION_IS_ONLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) #ifdef CONFIG_SPARSEMEM_VMEMMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) static struct page * __meminit populate_section_memmap(unsigned long pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) unsigned long nr_pages, int nid, struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) return __populate_section_memmap(pfn, nr_pages, nid, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) unsigned long start = (unsigned long) pfn_to_page(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) unsigned long end = start + nr_pages * sizeof(struct page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) vmemmap_free(start, end, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) static void free_map_bootmem(struct page *memmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) unsigned long start = (unsigned long)memmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) vmemmap_free(start, end, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) static int clear_subsection_map(unsigned long pfn, unsigned long nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) DECLARE_BITMAP(map, SUBSECTIONS_PER_SECTION) = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) DECLARE_BITMAP(tmp, SUBSECTIONS_PER_SECTION) = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) struct mem_section *ms = __pfn_to_section(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) unsigned long *subsection_map = ms->usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) ? &ms->usage->subsection_map[0] : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) subsection_mask_set(map, pfn, nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) if (subsection_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) bitmap_and(tmp, map, subsection_map, SUBSECTIONS_PER_SECTION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) if (WARN(!subsection_map || !bitmap_equal(tmp, map, SUBSECTIONS_PER_SECTION),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) "section already deactivated (%#lx + %ld)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) pfn, nr_pages))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) bitmap_xor(subsection_map, map, subsection_map, SUBSECTIONS_PER_SECTION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) static bool is_subsection_map_empty(struct mem_section *ms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) return bitmap_empty(&ms->usage->subsection_map[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) SUBSECTIONS_PER_SECTION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) static int fill_subsection_map(unsigned long pfn, unsigned long nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) struct mem_section *ms = __pfn_to_section(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) DECLARE_BITMAP(map, SUBSECTIONS_PER_SECTION) = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) unsigned long *subsection_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) subsection_mask_set(map, pfn, nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) subsection_map = &ms->usage->subsection_map[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) if (bitmap_empty(map, SUBSECTIONS_PER_SECTION))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) else if (bitmap_intersects(map, subsection_map, SUBSECTIONS_PER_SECTION))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) rc = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) bitmap_or(subsection_map, map, subsection_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) SUBSECTIONS_PER_SECTION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) struct page * __meminit populate_section_memmap(unsigned long pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) unsigned long nr_pages, int nid, struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) return kvmalloc_node(array_size(sizeof(struct page),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) PAGES_PER_SECTION), GFP_KERNEL, nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) kvfree(pfn_to_page(pfn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) static void free_map_bootmem(struct page *memmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) unsigned long maps_section_nr, removing_section_nr, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) unsigned long magic, nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) struct page *page = virt_to_page(memmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) for (i = 0; i < nr_pages; i++, page++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) magic = (unsigned long) page->freelist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) BUG_ON(magic == NODE_INFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) removing_section_nr = page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) * When this function is called, the removing section is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) * logical offlined state. This means all pages are isolated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) * from page allocator. If removing section's memmap is placed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) * on the same section, it must not be freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) * If it is freed, page allocator may allocate it which will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) * be removed physically soon.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) if (maps_section_nr != removing_section_nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) put_page_bootmem(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) static int clear_subsection_map(unsigned long pfn, unsigned long nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) static bool is_subsection_map_empty(struct mem_section *ms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) static int fill_subsection_map(unsigned long pfn, unsigned long nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) #endif /* CONFIG_SPARSEMEM_VMEMMAP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) * To deactivate a memory region, there are 3 cases to handle across
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) * two configurations (SPARSEMEM_VMEMMAP={y,n}):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) * 1. deactivation of a partial hot-added section (only possible in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) * the SPARSEMEM_VMEMMAP=y case).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) * a) section was present at memory init.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) * b) section was hot-added post memory init.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) * 2. deactivation of a complete hot-added section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) * 3. deactivation of a complete section from memory init.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) * For 1, when subsection_map does not empty we will not be freeing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) * usage map, but still need to free the vmemmap range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) * For 2 and 3, the SPARSEMEM_VMEMMAP={y,n} cases are unified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) static void section_deactivate(unsigned long pfn, unsigned long nr_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) struct mem_section *ms = __pfn_to_section(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) bool section_is_early = early_section(ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) struct page *memmap = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) bool empty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) if (clear_subsection_map(pfn, nr_pages))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) empty = is_subsection_map_empty(ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) if (empty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) unsigned long section_nr = pfn_to_section_nr(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) * When removing an early section, the usage map is kept (as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) * usage maps of other sections fall into the same page). It
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) * will be re-used when re-adding the section - which is then no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) * longer an early section. If the usage map is PageReserved, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) * was allocated during boot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) if (!PageReserved(virt_to_page(ms->usage))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) kfree(ms->usage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) ms->usage = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) * Mark the section invalid so that valid_section()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) * return false. This prevents code from dereferencing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) * ms->usage array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) ms->section_mem_map &= ~SECTION_HAS_MEM_MAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) * The memmap of early sections is always fully populated. See
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) * section_activate() and pfn_valid() .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) if (!section_is_early)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) depopulate_section_memmap(pfn, nr_pages, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) else if (memmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) free_map_bootmem(memmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) if (empty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) ms->section_mem_map = (unsigned long)NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) static struct page * __meminit section_activate(int nid, unsigned long pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) unsigned long nr_pages, struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) struct mem_section *ms = __pfn_to_section(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) struct mem_section_usage *usage = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) struct page *memmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) if (!ms->usage) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) usage = kzalloc(mem_section_usage_size(), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) if (!usage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) ms->usage = usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) rc = fill_subsection_map(pfn, nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) if (usage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) ms->usage = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) kfree(usage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) * The early init code does not consider partially populated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) * initial sections, it simply assumes that memory will never be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) * referenced. If we hot-add memory into such a section then we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) * do not need to populate the memmap and can simply reuse what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) * is already there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) if (nr_pages < PAGES_PER_SECTION && early_section(ms))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) return pfn_to_page(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) memmap = populate_section_memmap(pfn, nr_pages, nid, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) if (!memmap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) section_deactivate(pfn, nr_pages, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) return memmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) * sparse_add_section - add a memory section, or populate an existing one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) * @nid: The node to add section on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) * @start_pfn: start pfn of the memory range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) * @nr_pages: number of pfns to add in the section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) * @altmap: device page map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) * This is only intended for hotplug.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) * Note that only VMEMMAP supports sub-section aligned hotplug,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) * the proper alignment and size are gated by check_pfn_span().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) * * 0 - On success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) * * -EEXIST - Section has been present.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) * * -ENOMEM - Out of memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) int __meminit sparse_add_section(int nid, unsigned long start_pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) unsigned long nr_pages, struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) unsigned long section_nr = pfn_to_section_nr(start_pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) struct mem_section *ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) struct page *memmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) ret = sparse_index_init(section_nr, nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) memmap = section_activate(nid, start_pfn, nr_pages, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) if (IS_ERR(memmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) return PTR_ERR(memmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) * Poison uninitialized struct pages in order to catch invalid flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) * combinations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) page_init_poison(memmap, sizeof(struct page) * nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) ms = __nr_to_section(section_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) set_section_nid(section_nr, nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) section_mark_present(ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) /* Align memmap to section boundary in the subsection case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) if (section_nr_to_pfn(section_nr) != start_pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) memmap = pfn_to_page(section_nr_to_pfn(section_nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) sparse_init_one_section(ms, section_nr, memmap, ms->usage, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) #ifdef CONFIG_MEMORY_FAILURE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) static void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) * A further optimization is to have per section refcounted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) * num_poisoned_pages. But that would need more space per memmap, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) * for now just do a quick global check to speed up this routine in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) * absence of bad pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) if (atomic_long_read(&num_poisoned_pages) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) for (i = 0; i < nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) if (PageHWPoison(&memmap[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) num_poisoned_pages_dec();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) ClearPageHWPoison(&memmap[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) void sparse_remove_section(struct mem_section *ms, unsigned long pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) unsigned long nr_pages, unsigned long map_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) clear_hwpoisoned_pages(pfn_to_page(pfn) + map_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) nr_pages - map_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) section_deactivate(pfn, nr_pages, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) #endif /* CONFIG_MEMORY_HOTPLUG */