xref: /linux/mm/sparse.c (revision 8a36f53fd5ec3eff01f1b637d42cbdc3821d0493)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * sparse memory mappings.
4  */
5 #include <linux/mm.h>
6 #include <linux/slab.h>
7 #include <linux/mmzone.h>
8 #include <linux/memblock.h>
9 #include <linux/compiler.h>
10 #include <linux/highmem.h>
11 #include <linux/export.h>
12 #include <linux/spinlock.h>
13 #include <linux/vmalloc.h>
14 #include <linux/swap.h>
15 #include <linux/swapops.h>
16 #include <linux/vmstat.h>
17 #include "internal.h"
18 #include "mm_init.h"
19 #include "sparse.h"
20 #include <asm/dma.h>
21 
22 /*
23  * Permanent SPARSEMEM data:
24  *
25  * 1) mem_section	- memory sections, mem_map's for valid memory
26  */
27 #ifdef CONFIG_SPARSEMEM_EXTREME
28 struct mem_section **mem_section;
29 #else
30 struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
31 	____cacheline_internodealigned_in_smp;
32 #endif
33 EXPORT_SYMBOL(mem_section);
34 
35 #ifdef NODE_NOT_IN_PAGE_FLAGS
36 /*
37  * If we did not store the node number in the page then we have to
38  * do a lookup in the section_to_node_table in order to find which
39  * node the page belongs to.
40  */
41 #if MAX_NUMNODES <= 256
42 static u8 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
43 #else
44 static u16 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
45 #endif
46 
47 int memdesc_nid(const memdesc_flags_t *mdf)
48 {
49 	return section_to_node_table[memdesc_section(mdf)];
50 }
51 EXPORT_SYMBOL(memdesc_nid);
52 
53 static void set_section_nid(unsigned long section_nr, int nid)
54 {
55 	section_to_node_table[section_nr] = nid;
56 }
57 #else /* !NODE_NOT_IN_PAGE_FLAGS */
58 static inline void set_section_nid(unsigned long section_nr, int nid)
59 {
60 }
61 #endif
62 
63 #ifdef CONFIG_SPARSEMEM_EXTREME
64 static noinline struct mem_section __ref *sparse_index_alloc(int nid)
65 {
66 	struct mem_section *section = NULL;
67 	unsigned long array_size = SECTIONS_PER_ROOT *
68 				   sizeof(struct mem_section);
69 
70 	if (slab_is_available()) {
71 		section = kzalloc_node(array_size, GFP_KERNEL, nid);
72 	} else {
73 		section = memblock_alloc_node(array_size, SMP_CACHE_BYTES,
74 					      nid);
75 		if (!section)
76 			panic("%s: Failed to allocate %lu bytes nid=%d\n",
77 			      __func__, array_size, nid);
78 	}
79 
80 	return section;
81 }
82 
83 int __meminit sparse_index_init(unsigned long section_nr, int nid)
84 {
85 	unsigned long root = SECTION_NR_TO_ROOT(section_nr);
86 	struct mem_section *section;
87 
88 	/*
89 	 * An existing section is possible in the sub-section hotplug
90 	 * case. First hot-add instantiates, follow-on hot-add reuses
91 	 * the existing section.
92 	 *
93 	 * The mem_hotplug_lock resolves the apparent race below.
94 	 */
95 	if (mem_section[root])
96 		return 0;
97 
98 	section = sparse_index_alloc(nid);
99 	if (!section)
100 		return -ENOMEM;
101 
102 	mem_section[root] = section;
103 
104 	return 0;
105 }
106 
107 static void __init sparse_extreme_init(void)
108 {
109 	const unsigned long size = sizeof(struct mem_section *) * NR_SECTION_ROOTS;
110 
111 	mem_section = memblock_alloc_or_panic(size, INTERNODE_CACHE_BYTES);
112 }
113 #else /* !SPARSEMEM_EXTREME */
114 int __meminit sparse_index_init(unsigned long section_nr, int nid)
115 {
116 	return 0;
117 }
118 
119 static void __init sparse_extreme_init(void)
120 {
121 }
122 #endif
123 
124 /*
125  * During early boot, before section_mem_map is used for an actual
126  * mem_map, we use section_mem_map to store the section's NUMA
127  * node.  This keeps us from having to use another data structure.  The
128  * node information is cleared just before we store the real mem_map.
129  */
130 static inline unsigned long sparse_encode_early_nid(int nid)
131 {
132 	return ((unsigned long)nid << SECTION_NID_SHIFT);
133 }
134 
135 static inline int sparse_early_nid(struct mem_section *section)
136 {
137 	return (section->section_mem_map >> SECTION_NID_SHIFT);
138 }
139 
140 /* Validate the physical addressing limitations of the model */
141 static void __init mminit_validate_memmodel_limits(unsigned long *start_pfn,
142 						unsigned long *end_pfn)
143 {
144 	unsigned long max_sparsemem_pfn = (DIRECT_MAP_PHYSMEM_END + 1) >> PAGE_SHIFT;
145 
146 	/*
147 	 * Sanity checks - do not allow an architecture to pass
148 	 * in larger pfns than the maximum scope of sparsemem:
149 	 */
150 	if (*start_pfn > max_sparsemem_pfn) {
151 		mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
152 			"Start of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
153 			*start_pfn, *end_pfn, max_sparsemem_pfn);
154 		WARN_ON_ONCE(1);
155 		*start_pfn = max_sparsemem_pfn;
156 		*end_pfn = max_sparsemem_pfn;
157 	} else if (*end_pfn > max_sparsemem_pfn) {
158 		mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
159 			"End of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
160 			*start_pfn, *end_pfn, max_sparsemem_pfn);
161 		WARN_ON_ONCE(1);
162 		*end_pfn = max_sparsemem_pfn;
163 	}
164 }
165 
166 /*
167  * Looping over all possible memory sections is expensive, especially if
168  * NR_MEM_SECTIONS is large but only a fraction is actually used. Keep track of
169  * the highest section number we ever used.
170  */
171 unsigned long __highest_used_section_nr;
172 
173 static inline unsigned long first_early_section_nr(void)
174 {
175 	return next_early_section_nr(-1);
176 }
177 
178 void __init sparse_sections_init(void)
179 {
180 	const unsigned long flags = SECTION_IS_EARLY | SECTION_IS_ONLINE;
181 	unsigned long pfn, start_pfn, end_pfn, section_nr;
182 	int i, nid;
183 
184 	sparse_extreme_init();
185 
186 	for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
187 		start_pfn &= PAGE_SECTION_MASK;
188 		mminit_validate_memmodel_limits(&start_pfn, &end_pfn);
189 
190 		for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
191 			struct mem_section *ms;
192 
193 			section_nr = pfn_to_section_nr(pfn);
194 			sparse_index_init(section_nr, nid);
195 			ms = __nr_to_section(section_nr);
196 			if (ms->section_mem_map)
197 				continue;
198 
199 			set_section_nid(section_nr, nid);
200 			ms->section_mem_map = sparse_encode_early_nid(nid) | flags;
201 		}
202 	}
203 	__highest_used_section_nr = section_nr;
204 }
205 #ifndef CONFIG_SPARSEMEM_VMEMMAP
206 struct page __init *__populate_section_memmap(unsigned long pfn,
207 		unsigned long nr_pages, int nid, struct vmem_altmap *altmap,
208 		struct dev_pagemap *pgmap)
209 {
210 	const unsigned long size = PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION);
211 
212 	return memmap_alloc(size, size, __pa(MAX_DMA_ADDRESS), nid);
213 }
214 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
215 
216 void __weak __meminit vmemmap_populate_print_last(void)
217 {
218 }
219 
220 static void __init sparse_metadata_init_nid(int nid,
221 		unsigned long start_section_nr, unsigned long end_section_nr,
222 		unsigned long nr_sections)
223 {
224 	struct mem_section_usage *usage;
225 	unsigned long section_nr;
226 
227 	usage = memblock_alloc_node(nr_sections * mem_section_usage_size(),
228 				    SMP_CACHE_BYTES, nid);
229 	if (!usage)
230 		panic("Failed to allocate usemap for node %d\n", nid);
231 
232 	for_each_early_section_nr(start_section_nr, section_nr) {
233 		const unsigned long pfn = section_nr_to_pfn(section_nr);
234 		struct page *mem_map;
235 
236 		if (section_nr >= end_section_nr)
237 			break;
238 
239 		mem_map = __populate_section_memmap(pfn, PAGES_PER_SECTION, nid,
240 						    NULL, NULL);
241 		if (!mem_map)
242 			panic("Failed to allocate memmap for section %lu\n",
243 			      section_nr);
244 		memmap_boot_pages_add(section_nr_vmemmap_pages(pfn, PAGES_PER_SECTION));
245 		sparse_init_one_section(__nr_to_section(section_nr), section_nr,
246 					mem_map, usage);
247 		usage = (void *)usage + mem_section_usage_size();
248 	}
249 }
250 
251 static void __init sparse_metadata_init(void)
252 {
253 	unsigned long start_section_nr = first_early_section_nr();
254 	int nid_begin = sparse_early_nid(__nr_to_section(start_section_nr));
255 	unsigned long section_nr, nr_sections = 1;
256 
257 	for_each_early_section_nr(start_section_nr + 1, section_nr) {
258 		const int nid = sparse_early_nid(__nr_to_section(section_nr));
259 
260 		if (nid == nid_begin) {
261 			nr_sections++;
262 			continue;
263 		}
264 		sparse_metadata_init_nid(nid_begin, start_section_nr,
265 					 section_nr, nr_sections);
266 		nid_begin = nid;
267 		start_section_nr = section_nr;
268 		nr_sections = 1;
269 	}
270 	sparse_metadata_init_nid(nid_begin, start_section_nr, section_nr,
271 				 nr_sections);
272 }
273 
274 /*
275  * Allocate the accumulated non-linear sections, allocate a mem_map
276  * for each and record the physical to section mapping.
277  */
278 void __init sparse_init(void)
279 {
280 	if (compound_info_has_mask()) {
281 		VM_WARN_ON_ONCE(!IS_ALIGNED((unsigned long) pfn_to_page(0),
282 				    MAX_FOLIO_VMEMMAP_ALIGN));
283 	}
284 
285 	sparse_metadata_init();
286 	sparse_init_subsection_map();
287 	vmemmap_populate_print_last();
288 }
289