]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
d41dee36 AW |
2 | /* |
3 | * sparse memory mappings. | |
4 | */ | |
d41dee36 | 5 | #include <linux/mm.h> |
5a0e3ad6 | 6 | #include <linux/slab.h> |
d41dee36 | 7 | #include <linux/mmzone.h> |
97ad1087 | 8 | #include <linux/memblock.h> |
3b32123d | 9 | #include <linux/compiler.h> |
0b0acbec | 10 | #include <linux/highmem.h> |
b95f1b31 | 11 | #include <linux/export.h> |
28ae55c9 | 12 | #include <linux/spinlock.h> |
0b0acbec | 13 | #include <linux/vmalloc.h> |
3b32123d | 14 | |
0c0a4a51 | 15 | #include "internal.h" |
d41dee36 | 16 | #include <asm/dma.h> |
8f6aac41 CL |
17 | #include <asm/pgalloc.h> |
18 | #include <asm/pgtable.h> | |
d41dee36 AW |
19 | |
20 | /* | |
21 | * Permanent SPARSEMEM data: | |
22 | * | |
23 | * 1) mem_section - memory sections, mem_map's for valid memory | |
24 | */ | |
3e347261 | 25 | #ifdef CONFIG_SPARSEMEM_EXTREME |
83e3c487 | 26 | struct mem_section **mem_section; |
3e347261 BP |
27 | #else |
28 | struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT] | |
22fc6ecc | 29 | ____cacheline_internodealigned_in_smp; |
3e347261 BP |
30 | #endif |
31 | EXPORT_SYMBOL(mem_section); | |
32 | ||
89689ae7 CL |
33 | #ifdef NODE_NOT_IN_PAGE_FLAGS |
34 | /* | |
35 | * If we did not store the node number in the page then we have to | |
36 | * do a lookup in the section_to_node_table in order to find which | |
37 | * node the page belongs to. | |
38 | */ | |
39 | #if MAX_NUMNODES <= 256 | |
40 | static u8 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned; | |
41 | #else | |
42 | static u16 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned; | |
43 | #endif | |
44 | ||
33dd4e0e | 45 | int page_to_nid(const struct page *page) |
89689ae7 CL |
46 | { |
47 | return section_to_node_table[page_to_section(page)]; | |
48 | } | |
49 | EXPORT_SYMBOL(page_to_nid); | |
85770ffe AW |
50 | |
51 | static void set_section_nid(unsigned long section_nr, int nid) | |
52 | { | |
53 | section_to_node_table[section_nr] = nid; | |
54 | } | |
55 | #else /* !NODE_NOT_IN_PAGE_FLAGS */ | |
56 | static inline void set_section_nid(unsigned long section_nr, int nid) | |
57 | { | |
58 | } | |
89689ae7 CL |
59 | #endif |
60 | ||
3e347261 | 61 | #ifdef CONFIG_SPARSEMEM_EXTREME |
bd721ea7 | 62 | static noinline struct mem_section __ref *sparse_index_alloc(int nid) |
28ae55c9 DH |
63 | { |
64 | struct mem_section *section = NULL; | |
65 | unsigned long array_size = SECTIONS_PER_ROOT * | |
66 | sizeof(struct mem_section); | |
67 | ||
b95046b0 MH |
68 | if (slab_is_available()) |
69 | section = kzalloc_node(array_size, GFP_KERNEL, nid); | |
70 | else | |
7e1c4e27 MR |
71 | section = memblock_alloc_node(array_size, SMP_CACHE_BYTES, |
72 | nid); | |
28ae55c9 DH |
73 | |
74 | return section; | |
3e347261 | 75 | } |
802f192e | 76 | |
a3142c8e | 77 | static int __meminit sparse_index_init(unsigned long section_nr, int nid) |
802f192e | 78 | { |
28ae55c9 DH |
79 | unsigned long root = SECTION_NR_TO_ROOT(section_nr); |
80 | struct mem_section *section; | |
802f192e BP |
81 | |
82 | if (mem_section[root]) | |
28ae55c9 | 83 | return -EEXIST; |
3e347261 | 84 | |
28ae55c9 | 85 | section = sparse_index_alloc(nid); |
af0cd5a7 WC |
86 | if (!section) |
87 | return -ENOMEM; | |
28ae55c9 DH |
88 | |
89 | mem_section[root] = section; | |
c1c95183 | 90 | |
9d1936cf | 91 | return 0; |
28ae55c9 DH |
92 | } |
93 | #else /* !SPARSEMEM_EXTREME */ | |
94 | static inline int sparse_index_init(unsigned long section_nr, int nid) | |
95 | { | |
96 | return 0; | |
802f192e | 97 | } |
28ae55c9 DH |
98 | #endif |
99 | ||
91fd8b95 | 100 | #ifdef CONFIG_SPARSEMEM_EXTREME |
4ca644d9 DH |
101 | int __section_nr(struct mem_section* ms) |
102 | { | |
103 | unsigned long root_nr; | |
83e3c487 | 104 | struct mem_section *root = NULL; |
4ca644d9 | 105 | |
12783b00 MK |
106 | for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) { |
107 | root = __nr_to_section(root_nr * SECTIONS_PER_ROOT); | |
4ca644d9 DH |
108 | if (!root) |
109 | continue; | |
110 | ||
111 | if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT))) | |
112 | break; | |
113 | } | |
114 | ||
83e3c487 | 115 | VM_BUG_ON(!root); |
db36a461 | 116 | |
4ca644d9 DH |
117 | return (root_nr * SECTIONS_PER_ROOT) + (ms - root); |
118 | } | |
91fd8b95 ZC |
119 | #else |
120 | int __section_nr(struct mem_section* ms) | |
121 | { | |
122 | return (int)(ms - mem_section[0]); | |
123 | } | |
124 | #endif | |
4ca644d9 | 125 | |
30c253e6 AW |
126 | /* |
127 | * During early boot, before section_mem_map is used for an actual | |
128 | * mem_map, we use section_mem_map to store the section's NUMA | |
129 | * node. This keeps us from having to use another data structure. The | |
130 | * node information is cleared just before we store the real mem_map. | |
131 | */ | |
132 | static inline unsigned long sparse_encode_early_nid(int nid) | |
133 | { | |
134 | return (nid << SECTION_NID_SHIFT); | |
135 | } | |
136 | ||
137 | static inline int sparse_early_nid(struct mem_section *section) | |
138 | { | |
139 | return (section->section_mem_map >> SECTION_NID_SHIFT); | |
140 | } | |
141 | ||
2dbb51c4 MG |
142 | /* Validate the physical addressing limitations of the model */ |
143 | void __meminit mminit_validate_memmodel_limits(unsigned long *start_pfn, | |
144 | unsigned long *end_pfn) | |
d41dee36 | 145 | { |
2dbb51c4 | 146 | unsigned long max_sparsemem_pfn = 1UL << (MAX_PHYSMEM_BITS-PAGE_SHIFT); |
d41dee36 | 147 | |
bead9a3a IM |
148 | /* |
149 | * Sanity checks - do not allow an architecture to pass | |
150 | * in larger pfns than the maximum scope of sparsemem: | |
151 | */ | |
2dbb51c4 MG |
152 | if (*start_pfn > max_sparsemem_pfn) { |
153 | mminit_dprintk(MMINIT_WARNING, "pfnvalidation", | |
154 | "Start of range %lu -> %lu exceeds SPARSEMEM max %lu\n", | |
155 | *start_pfn, *end_pfn, max_sparsemem_pfn); | |
156 | WARN_ON_ONCE(1); | |
157 | *start_pfn = max_sparsemem_pfn; | |
158 | *end_pfn = max_sparsemem_pfn; | |
ef161a98 | 159 | } else if (*end_pfn > max_sparsemem_pfn) { |
2dbb51c4 MG |
160 | mminit_dprintk(MMINIT_WARNING, "pfnvalidation", |
161 | "End of range %lu -> %lu exceeds SPARSEMEM max %lu\n", | |
162 | *start_pfn, *end_pfn, max_sparsemem_pfn); | |
163 | WARN_ON_ONCE(1); | |
164 | *end_pfn = max_sparsemem_pfn; | |
165 | } | |
166 | } | |
167 | ||
c4e1be9e DH |
168 | /* |
169 | * There are a number of times that we loop over NR_MEM_SECTIONS, | |
170 | * looking for section_present() on each. But, when we have very | |
171 | * large physical address spaces, NR_MEM_SECTIONS can also be | |
172 | * very large which makes the loops quite long. | |
173 | * | |
174 | * Keeping track of this gives us an easy way to break out of | |
175 | * those loops early. | |
176 | */ | |
177 | int __highest_present_section_nr; | |
178 | static void section_mark_present(struct mem_section *ms) | |
179 | { | |
180 | int section_nr = __section_nr(ms); | |
181 | ||
182 | if (section_nr > __highest_present_section_nr) | |
183 | __highest_present_section_nr = section_nr; | |
184 | ||
185 | ms->section_mem_map |= SECTION_MARKED_PRESENT; | |
186 | } | |
187 | ||
188 | static inline int next_present_section_nr(int section_nr) | |
189 | { | |
190 | do { | |
191 | section_nr++; | |
192 | if (present_section_nr(section_nr)) | |
193 | return section_nr; | |
d538c164 | 194 | } while ((section_nr <= __highest_present_section_nr)); |
c4e1be9e DH |
195 | |
196 | return -1; | |
197 | } | |
198 | #define for_each_present_section_nr(start, section_nr) \ | |
199 | for (section_nr = next_present_section_nr(start-1); \ | |
200 | ((section_nr >= 0) && \ | |
c4e1be9e DH |
201 | (section_nr <= __highest_present_section_nr)); \ |
202 | section_nr = next_present_section_nr(section_nr)) | |
203 | ||
85c77f79 PT |
204 | static inline unsigned long first_present_section_nr(void) |
205 | { | |
206 | return next_present_section_nr(-1); | |
207 | } | |
208 | ||
2dbb51c4 MG |
209 | /* Record a memory area against a node. */ |
210 | void __init memory_present(int nid, unsigned long start, unsigned long end) | |
211 | { | |
212 | unsigned long pfn; | |
bead9a3a | 213 | |
629a359b KS |
214 | #ifdef CONFIG_SPARSEMEM_EXTREME |
215 | if (unlikely(!mem_section)) { | |
216 | unsigned long size, align; | |
217 | ||
d09cfbbf | 218 | size = sizeof(struct mem_section*) * NR_SECTION_ROOTS; |
629a359b | 219 | align = 1 << (INTERNODE_CACHE_SHIFT); |
eb31d559 | 220 | mem_section = memblock_alloc(size, align); |
629a359b KS |
221 | } |
222 | #endif | |
223 | ||
d41dee36 | 224 | start &= PAGE_SECTION_MASK; |
2dbb51c4 | 225 | mminit_validate_memmodel_limits(&start, &end); |
d41dee36 AW |
226 | for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) { |
227 | unsigned long section = pfn_to_section_nr(pfn); | |
802f192e BP |
228 | struct mem_section *ms; |
229 | ||
230 | sparse_index_init(section, nid); | |
85770ffe | 231 | set_section_nid(section, nid); |
802f192e BP |
232 | |
233 | ms = __nr_to_section(section); | |
c4e1be9e | 234 | if (!ms->section_mem_map) { |
2d070eab MH |
235 | ms->section_mem_map = sparse_encode_early_nid(nid) | |
236 | SECTION_IS_ONLINE; | |
c4e1be9e DH |
237 | section_mark_present(ms); |
238 | } | |
d41dee36 AW |
239 | } |
240 | } | |
241 | ||
29751f69 AW |
242 | /* |
243 | * Subtle, we encode the real pfn into the mem_map such that | |
244 | * the identity pfn - section_mem_map will return the actual | |
245 | * physical page frame number. | |
246 | */ | |
247 | static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum) | |
248 | { | |
def9b71e PT |
249 | unsigned long coded_mem_map = |
250 | (unsigned long)(mem_map - (section_nr_to_pfn(pnum))); | |
251 | BUILD_BUG_ON(SECTION_MAP_LAST_BIT > (1UL<<PFN_SECTION_SHIFT)); | |
252 | BUG_ON(coded_mem_map & ~SECTION_MAP_MASK); | |
253 | return coded_mem_map; | |
29751f69 AW |
254 | } |
255 | ||
256 | /* | |
ea01ea93 | 257 | * Decode mem_map from the coded memmap |
29751f69 | 258 | */ |
29751f69 AW |
259 | struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum) |
260 | { | |
ea01ea93 BP |
261 | /* mask off the extra low bits of information */ |
262 | coded_mem_map &= SECTION_MAP_MASK; | |
29751f69 AW |
263 | return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum); |
264 | } | |
265 | ||
4e40987f | 266 | static void __meminit sparse_init_one_section(struct mem_section *ms, |
5c0e3066 MG |
267 | unsigned long pnum, struct page *mem_map, |
268 | unsigned long *pageblock_bitmap) | |
29751f69 | 269 | { |
30c253e6 | 270 | ms->section_mem_map &= ~SECTION_MAP_MASK; |
540557b9 AW |
271 | ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum) | |
272 | SECTION_HAS_MEM_MAP; | |
5c0e3066 | 273 | ms->pageblock_flags = pageblock_bitmap; |
29751f69 AW |
274 | } |
275 | ||
04753278 | 276 | unsigned long usemap_size(void) |
5c0e3066 | 277 | { |
60a7a88d | 278 | return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long); |
5c0e3066 MG |
279 | } |
280 | ||
281 | #ifdef CONFIG_MEMORY_HOTPLUG | |
282 | static unsigned long *__kmalloc_section_usemap(void) | |
283 | { | |
284 | return kmalloc(usemap_size(), GFP_KERNEL); | |
285 | } | |
286 | #endif /* CONFIG_MEMORY_HOTPLUG */ | |
287 | ||
48c90682 YG |
288 | #ifdef CONFIG_MEMORY_HOTREMOVE |
289 | static unsigned long * __init | |
a4322e1b | 290 | sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat, |
238305bb | 291 | unsigned long size) |
48c90682 | 292 | { |
99ab7b19 YL |
293 | unsigned long goal, limit; |
294 | unsigned long *p; | |
295 | int nid; | |
48c90682 YG |
296 | /* |
297 | * A page may contain usemaps for other sections preventing the | |
298 | * page being freed and making a section unremovable while | |
c800bcd5 | 299 | * other sections referencing the usemap remain active. Similarly, |
48c90682 YG |
300 | * a pgdat can prevent a section being removed. If section A |
301 | * contains a pgdat and section B contains the usemap, both | |
302 | * sections become inter-dependent. This allocates usemaps | |
303 | * from the same section as the pgdat where possible to avoid | |
304 | * this problem. | |
305 | */ | |
07b4e2bc | 306 | goal = __pa(pgdat) & (PAGE_SECTION_MASK << PAGE_SHIFT); |
99ab7b19 YL |
307 | limit = goal + (1UL << PA_SECTION_SHIFT); |
308 | nid = early_pfn_to_nid(goal >> PAGE_SHIFT); | |
309 | again: | |
eb31d559 | 310 | p = memblock_alloc_try_nid_nopanic(size, |
bb016b84 SS |
311 | SMP_CACHE_BYTES, goal, limit, |
312 | nid); | |
99ab7b19 YL |
313 | if (!p && limit) { |
314 | limit = 0; | |
315 | goto again; | |
316 | } | |
317 | return p; | |
48c90682 YG |
318 | } |
319 | ||
320 | static void __init check_usemap_section_nr(int nid, unsigned long *usemap) | |
321 | { | |
322 | unsigned long usemap_snr, pgdat_snr; | |
83e3c487 KS |
323 | static unsigned long old_usemap_snr; |
324 | static unsigned long old_pgdat_snr; | |
48c90682 YG |
325 | struct pglist_data *pgdat = NODE_DATA(nid); |
326 | int usemap_nid; | |
327 | ||
83e3c487 KS |
328 | /* First call */ |
329 | if (!old_usemap_snr) { | |
330 | old_usemap_snr = NR_MEM_SECTIONS; | |
331 | old_pgdat_snr = NR_MEM_SECTIONS; | |
332 | } | |
333 | ||
48c90682 YG |
334 | usemap_snr = pfn_to_section_nr(__pa(usemap) >> PAGE_SHIFT); |
335 | pgdat_snr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT); | |
336 | if (usemap_snr == pgdat_snr) | |
337 | return; | |
338 | ||
339 | if (old_usemap_snr == usemap_snr && old_pgdat_snr == pgdat_snr) | |
340 | /* skip redundant message */ | |
341 | return; | |
342 | ||
343 | old_usemap_snr = usemap_snr; | |
344 | old_pgdat_snr = pgdat_snr; | |
345 | ||
346 | usemap_nid = sparse_early_nid(__nr_to_section(usemap_snr)); | |
347 | if (usemap_nid != nid) { | |
1170532b JP |
348 | pr_info("node %d must be removed before remove section %ld\n", |
349 | nid, usemap_snr); | |
48c90682 YG |
350 | return; |
351 | } | |
352 | /* | |
353 | * There is a circular dependency. | |
354 | * Some platforms allow un-removable section because they will just | |
355 | * gather other removable sections for dynamic partitioning. | |
356 | * Just notify un-removable section's number here. | |
357 | */ | |
1170532b JP |
358 | pr_info("Section %ld and %ld (node %d) have a circular dependency on usemap and pgdat allocations\n", |
359 | usemap_snr, pgdat_snr, nid); | |
48c90682 YG |
360 | } |
361 | #else | |
362 | static unsigned long * __init | |
a4322e1b | 363 | sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat, |
238305bb | 364 | unsigned long size) |
48c90682 | 365 | { |
eb31d559 | 366 | return memblock_alloc_node_nopanic(size, pgdat->node_id); |
48c90682 YG |
367 | } |
368 | ||
369 | static void __init check_usemap_section_nr(int nid, unsigned long *usemap) | |
370 | { | |
371 | } | |
372 | #endif /* CONFIG_MEMORY_HOTREMOVE */ | |
373 | ||
35fd1eb1 | 374 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
afda57bc | 375 | static unsigned long __init section_map_size(void) |
35fd1eb1 PT |
376 | { |
377 | return ALIGN(sizeof(struct page) * PAGES_PER_SECTION, PMD_SIZE); | |
378 | } | |
379 | ||
380 | #else | |
afda57bc | 381 | static unsigned long __init section_map_size(void) |
e131c06b PT |
382 | { |
383 | return PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION); | |
384 | } | |
385 | ||
7b73d978 CH |
386 | struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid, |
387 | struct vmem_altmap *altmap) | |
29751f69 | 388 | { |
e131c06b PT |
389 | unsigned long size = section_map_size(); |
390 | struct page *map = sparse_buffer_alloc(size); | |
391 | ||
392 | if (map) | |
393 | return map; | |
29751f69 | 394 | |
eb31d559 | 395 | map = memblock_alloc_try_nid(size, |
bb016b84 | 396 | PAGE_SIZE, __pa(MAX_DMA_ADDRESS), |
97ad1087 | 397 | MEMBLOCK_ALLOC_ACCESSIBLE, nid); |
8f6aac41 CL |
398 | return map; |
399 | } | |
400 | #endif /* !CONFIG_SPARSEMEM_VMEMMAP */ | |
401 | ||
35fd1eb1 PT |
402 | static void *sparsemap_buf __meminitdata; |
403 | static void *sparsemap_buf_end __meminitdata; | |
404 | ||
afda57bc | 405 | static void __init sparse_buffer_init(unsigned long size, int nid) |
35fd1eb1 PT |
406 | { |
407 | WARN_ON(sparsemap_buf); /* forgot to call sparse_buffer_fini()? */ | |
408 | sparsemap_buf = | |
eb31d559 | 409 | memblock_alloc_try_nid_raw(size, PAGE_SIZE, |
35fd1eb1 | 410 | __pa(MAX_DMA_ADDRESS), |
97ad1087 | 411 | MEMBLOCK_ALLOC_ACCESSIBLE, nid); |
35fd1eb1 PT |
412 | sparsemap_buf_end = sparsemap_buf + size; |
413 | } | |
414 | ||
afda57bc | 415 | static void __init sparse_buffer_fini(void) |
35fd1eb1 PT |
416 | { |
417 | unsigned long size = sparsemap_buf_end - sparsemap_buf; | |
418 | ||
419 | if (sparsemap_buf && size > 0) | |
420 | memblock_free_early(__pa(sparsemap_buf), size); | |
421 | sparsemap_buf = NULL; | |
422 | } | |
423 | ||
424 | void * __meminit sparse_buffer_alloc(unsigned long size) | |
425 | { | |
426 | void *ptr = NULL; | |
427 | ||
428 | if (sparsemap_buf) { | |
429 | ptr = PTR_ALIGN(sparsemap_buf, size); | |
430 | if (ptr + size > sparsemap_buf_end) | |
431 | ptr = NULL; | |
432 | else | |
433 | sparsemap_buf = ptr + size; | |
434 | } | |
435 | return ptr; | |
436 | } | |
437 | ||
3b32123d | 438 | void __weak __meminit vmemmap_populate_print_last(void) |
c2b91e2e YL |
439 | { |
440 | } | |
a4322e1b | 441 | |
85c77f79 PT |
442 | /* |
443 | * Initialize sparse on a specific node. The node spans [pnum_begin, pnum_end) | |
444 | * And number of present sections in this node is map_count. | |
445 | */ | |
446 | static void __init sparse_init_nid(int nid, unsigned long pnum_begin, | |
447 | unsigned long pnum_end, | |
448 | unsigned long map_count) | |
449 | { | |
450 | unsigned long pnum, usemap_longs, *usemap; | |
451 | struct page *map; | |
452 | ||
453 | usemap_longs = BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS); | |
454 | usemap = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nid), | |
455 | usemap_size() * | |
456 | map_count); | |
457 | if (!usemap) { | |
458 | pr_err("%s: node[%d] usemap allocation failed", __func__, nid); | |
459 | goto failed; | |
460 | } | |
461 | sparse_buffer_init(map_count * section_map_size(), nid); | |
462 | for_each_present_section_nr(pnum_begin, pnum) { | |
463 | if (pnum >= pnum_end) | |
464 | break; | |
465 | ||
466 | map = sparse_mem_map_populate(pnum, nid, NULL); | |
467 | if (!map) { | |
468 | pr_err("%s: node[%d] memory map backing failed. Some memory will not be available.", | |
469 | __func__, nid); | |
470 | pnum_begin = pnum; | |
471 | goto failed; | |
472 | } | |
473 | check_usemap_section_nr(nid, usemap); | |
474 | sparse_init_one_section(__nr_to_section(pnum), pnum, map, usemap); | |
475 | usemap += usemap_longs; | |
476 | } | |
477 | sparse_buffer_fini(); | |
478 | return; | |
479 | failed: | |
480 | /* We failed to allocate, mark all the following pnums as not present */ | |
481 | for_each_present_section_nr(pnum_begin, pnum) { | |
482 | struct mem_section *ms; | |
483 | ||
484 | if (pnum >= pnum_end) | |
485 | break; | |
486 | ms = __nr_to_section(pnum); | |
487 | ms->section_mem_map = 0; | |
488 | } | |
489 | } | |
490 | ||
491 | /* | |
492 | * Allocate the accumulated non-linear sections, allocate a mem_map | |
493 | * for each and record the physical to section mapping. | |
494 | */ | |
2a3cb8ba | 495 | void __init sparse_init(void) |
85c77f79 PT |
496 | { |
497 | unsigned long pnum_begin = first_present_section_nr(); | |
498 | int nid_begin = sparse_early_nid(__nr_to_section(pnum_begin)); | |
499 | unsigned long pnum_end, map_count = 1; | |
500 | ||
501 | /* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */ | |
502 | set_pageblock_order(); | |
503 | ||
504 | for_each_present_section_nr(pnum_begin + 1, pnum_end) { | |
505 | int nid = sparse_early_nid(__nr_to_section(pnum_end)); | |
506 | ||
507 | if (nid == nid_begin) { | |
508 | map_count++; | |
509 | continue; | |
510 | } | |
511 | /* Init node with sections in range [pnum_begin, pnum_end) */ | |
512 | sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count); | |
513 | nid_begin = nid; | |
514 | pnum_begin = pnum_end; | |
515 | map_count = 1; | |
516 | } | |
517 | /* cover the last node */ | |
518 | sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count); | |
519 | vmemmap_populate_print_last(); | |
520 | } | |
521 | ||
193faea9 | 522 | #ifdef CONFIG_MEMORY_HOTPLUG |
2d070eab MH |
523 | |
524 | /* Mark all memory sections within the pfn range as online */ | |
525 | void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn) | |
526 | { | |
527 | unsigned long pfn; | |
528 | ||
529 | for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { | |
b4ccec41 | 530 | unsigned long section_nr = pfn_to_section_nr(pfn); |
2d070eab MH |
531 | struct mem_section *ms; |
532 | ||
533 | /* onlining code should never touch invalid ranges */ | |
534 | if (WARN_ON(!valid_section_nr(section_nr))) | |
535 | continue; | |
536 | ||
537 | ms = __nr_to_section(section_nr); | |
538 | ms->section_mem_map |= SECTION_IS_ONLINE; | |
539 | } | |
540 | } | |
541 | ||
542 | #ifdef CONFIG_MEMORY_HOTREMOVE | |
543 | /* Mark all memory sections within the pfn range as online */ | |
544 | void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn) | |
545 | { | |
546 | unsigned long pfn; | |
547 | ||
548 | for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { | |
27227c73 | 549 | unsigned long section_nr = pfn_to_section_nr(pfn); |
2d070eab MH |
550 | struct mem_section *ms; |
551 | ||
552 | /* | |
553 | * TODO this needs some double checking. Offlining code makes | |
554 | * sure to check pfn_valid but those checks might be just bogus | |
555 | */ | |
556 | if (WARN_ON(!valid_section_nr(section_nr))) | |
557 | continue; | |
558 | ||
559 | ms = __nr_to_section(section_nr); | |
560 | ms->section_mem_map &= ~SECTION_IS_ONLINE; | |
561 | } | |
562 | } | |
563 | #endif | |
564 | ||
98f3cfc1 | 565 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
7b73d978 CH |
566 | static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid, |
567 | struct vmem_altmap *altmap) | |
98f3cfc1 YG |
568 | { |
569 | /* This will make the necessary allocations eventually. */ | |
7b73d978 | 570 | return sparse_mem_map_populate(pnum, nid, altmap); |
98f3cfc1 | 571 | } |
24b6d416 CH |
572 | static void __kfree_section_memmap(struct page *memmap, |
573 | struct vmem_altmap *altmap) | |
98f3cfc1 | 574 | { |
0aad818b | 575 | unsigned long start = (unsigned long)memmap; |
85b35fea | 576 | unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION); |
0aad818b | 577 | |
24b6d416 | 578 | vmemmap_free(start, end, altmap); |
98f3cfc1 | 579 | } |
4edd7cef | 580 | #ifdef CONFIG_MEMORY_HOTREMOVE |
81556b02 | 581 | static void free_map_bootmem(struct page *memmap) |
0c0a4a51 | 582 | { |
0aad818b | 583 | unsigned long start = (unsigned long)memmap; |
81556b02 | 584 | unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION); |
0aad818b | 585 | |
24b6d416 | 586 | vmemmap_free(start, end, NULL); |
0c0a4a51 | 587 | } |
4edd7cef | 588 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
98f3cfc1 | 589 | #else |
85b35fea | 590 | static struct page *__kmalloc_section_memmap(void) |
0b0acbec DH |
591 | { |
592 | struct page *page, *ret; | |
85b35fea | 593 | unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION; |
0b0acbec | 594 | |
f2d0aa5b | 595 | page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size)); |
0b0acbec DH |
596 | if (page) |
597 | goto got_map_page; | |
598 | ||
599 | ret = vmalloc(memmap_size); | |
600 | if (ret) | |
601 | goto got_map_ptr; | |
602 | ||
603 | return NULL; | |
604 | got_map_page: | |
605 | ret = (struct page *)pfn_to_kaddr(page_to_pfn(page)); | |
606 | got_map_ptr: | |
0b0acbec DH |
607 | |
608 | return ret; | |
609 | } | |
610 | ||
7b73d978 CH |
611 | static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid, |
612 | struct vmem_altmap *altmap) | |
98f3cfc1 | 613 | { |
85b35fea | 614 | return __kmalloc_section_memmap(); |
98f3cfc1 YG |
615 | } |
616 | ||
24b6d416 CH |
617 | static void __kfree_section_memmap(struct page *memmap, |
618 | struct vmem_altmap *altmap) | |
0b0acbec | 619 | { |
9e2779fa | 620 | if (is_vmalloc_addr(memmap)) |
0b0acbec DH |
621 | vfree(memmap); |
622 | else | |
623 | free_pages((unsigned long)memmap, | |
85b35fea | 624 | get_order(sizeof(struct page) * PAGES_PER_SECTION)); |
0b0acbec | 625 | } |
0c0a4a51 | 626 | |
4edd7cef | 627 | #ifdef CONFIG_MEMORY_HOTREMOVE |
81556b02 | 628 | static void free_map_bootmem(struct page *memmap) |
0c0a4a51 YG |
629 | { |
630 | unsigned long maps_section_nr, removing_section_nr, i; | |
81556b02 | 631 | unsigned long magic, nr_pages; |
ae64ffca | 632 | struct page *page = virt_to_page(memmap); |
0c0a4a51 | 633 | |
81556b02 ZY |
634 | nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page)) |
635 | >> PAGE_SHIFT; | |
636 | ||
0c0a4a51 | 637 | for (i = 0; i < nr_pages; i++, page++) { |
ddffe98d | 638 | magic = (unsigned long) page->freelist; |
0c0a4a51 YG |
639 | |
640 | BUG_ON(magic == NODE_INFO); | |
641 | ||
642 | maps_section_nr = pfn_to_section_nr(page_to_pfn(page)); | |
857e522a | 643 | removing_section_nr = page_private(page); |
0c0a4a51 YG |
644 | |
645 | /* | |
646 | * When this function is called, the removing section is | |
647 | * logical offlined state. This means all pages are isolated | |
648 | * from page allocator. If removing section's memmap is placed | |
649 | * on the same section, it must not be freed. | |
650 | * If it is freed, page allocator may allocate it which will | |
651 | * be removed physically soon. | |
652 | */ | |
653 | if (maps_section_nr != removing_section_nr) | |
654 | put_page_bootmem(page); | |
655 | } | |
656 | } | |
4edd7cef | 657 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
98f3cfc1 | 658 | #endif /* CONFIG_SPARSEMEM_VMEMMAP */ |
0b0acbec | 659 | |
29751f69 AW |
660 | /* |
661 | * returns the number of sections whose mem_maps were properly | |
662 | * set. If this is <=0, then that means that the passed-in | |
663 | * map was not consumed and must be freed. | |
664 | */ | |
7b73d978 CH |
665 | int __meminit sparse_add_one_section(struct pglist_data *pgdat, |
666 | unsigned long start_pfn, struct vmem_altmap *altmap) | |
29751f69 | 667 | { |
0b0acbec | 668 | unsigned long section_nr = pfn_to_section_nr(start_pfn); |
0b0acbec DH |
669 | struct mem_section *ms; |
670 | struct page *memmap; | |
5c0e3066 | 671 | unsigned long *usemap; |
0b0acbec DH |
672 | unsigned long flags; |
673 | int ret; | |
29751f69 | 674 | |
0b0acbec DH |
675 | /* |
676 | * no locking for this, because it does its own | |
677 | * plus, it does a kmalloc | |
678 | */ | |
bbd06825 WC |
679 | ret = sparse_index_init(section_nr, pgdat->node_id); |
680 | if (ret < 0 && ret != -EEXIST) | |
681 | return ret; | |
4e40987f | 682 | ret = 0; |
7b73d978 | 683 | memmap = kmalloc_section_memmap(section_nr, pgdat->node_id, altmap); |
bbd06825 WC |
684 | if (!memmap) |
685 | return -ENOMEM; | |
5c0e3066 | 686 | usemap = __kmalloc_section_usemap(); |
bbd06825 | 687 | if (!usemap) { |
24b6d416 | 688 | __kfree_section_memmap(memmap, altmap); |
bbd06825 WC |
689 | return -ENOMEM; |
690 | } | |
0b0acbec DH |
691 | |
692 | pgdat_resize_lock(pgdat, &flags); | |
29751f69 | 693 | |
0b0acbec DH |
694 | ms = __pfn_to_section(start_pfn); |
695 | if (ms->section_mem_map & SECTION_MARKED_PRESENT) { | |
696 | ret = -EEXIST; | |
697 | goto out; | |
698 | } | |
5c0e3066 | 699 | |
d0dc12e8 PT |
700 | /* |
701 | * Poison uninitialized struct pages in order to catch invalid flags | |
702 | * combinations. | |
703 | */ | |
f682a97a | 704 | page_init_poison(memmap, sizeof(struct page) * PAGES_PER_SECTION); |
3ac19f8e | 705 | |
c4e1be9e | 706 | section_mark_present(ms); |
4e40987f | 707 | sparse_init_one_section(ms, section_nr, memmap, usemap); |
0b0acbec | 708 | |
0b0acbec DH |
709 | out: |
710 | pgdat_resize_unlock(pgdat, &flags); | |
4e40987f | 711 | if (ret < 0) { |
bbd06825 | 712 | kfree(usemap); |
24b6d416 | 713 | __kfree_section_memmap(memmap, altmap); |
bbd06825 | 714 | } |
0b0acbec | 715 | return ret; |
29751f69 | 716 | } |
ea01ea93 | 717 | |
f3deb687 | 718 | #ifdef CONFIG_MEMORY_HOTREMOVE |
95a4774d WC |
719 | #ifdef CONFIG_MEMORY_FAILURE |
720 | static void clear_hwpoisoned_pages(struct page *memmap, int nr_pages) | |
721 | { | |
722 | int i; | |
723 | ||
724 | if (!memmap) | |
725 | return; | |
726 | ||
4b94ffdc | 727 | for (i = 0; i < nr_pages; i++) { |
95a4774d | 728 | if (PageHWPoison(&memmap[i])) { |
293c07e3 | 729 | atomic_long_sub(1, &num_poisoned_pages); |
95a4774d WC |
730 | ClearPageHWPoison(&memmap[i]); |
731 | } | |
732 | } | |
733 | } | |
734 | #else | |
735 | static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages) | |
736 | { | |
737 | } | |
738 | #endif | |
739 | ||
24b6d416 CH |
740 | static void free_section_usemap(struct page *memmap, unsigned long *usemap, |
741 | struct vmem_altmap *altmap) | |
4edd7cef DR |
742 | { |
743 | struct page *usemap_page; | |
4edd7cef DR |
744 | |
745 | if (!usemap) | |
746 | return; | |
747 | ||
748 | usemap_page = virt_to_page(usemap); | |
749 | /* | |
750 | * Check to see if allocation came from hot-plug-add | |
751 | */ | |
752 | if (PageSlab(usemap_page) || PageCompound(usemap_page)) { | |
753 | kfree(usemap); | |
754 | if (memmap) | |
24b6d416 | 755 | __kfree_section_memmap(memmap, altmap); |
4edd7cef DR |
756 | return; |
757 | } | |
758 | ||
759 | /* | |
760 | * The usemap came from bootmem. This is packed with other usemaps | |
761 | * on the section which has pgdat at boot time. Just keep it as is now. | |
762 | */ | |
763 | ||
81556b02 ZY |
764 | if (memmap) |
765 | free_map_bootmem(memmap); | |
4edd7cef DR |
766 | } |
767 | ||
4b94ffdc | 768 | void sparse_remove_one_section(struct zone *zone, struct mem_section *ms, |
24b6d416 | 769 | unsigned long map_offset, struct vmem_altmap *altmap) |
ea01ea93 BP |
770 | { |
771 | struct page *memmap = NULL; | |
cd099682 TC |
772 | unsigned long *usemap = NULL, flags; |
773 | struct pglist_data *pgdat = zone->zone_pgdat; | |
ea01ea93 | 774 | |
cd099682 | 775 | pgdat_resize_lock(pgdat, &flags); |
ea01ea93 BP |
776 | if (ms->section_mem_map) { |
777 | usemap = ms->pageblock_flags; | |
778 | memmap = sparse_decode_mem_map(ms->section_mem_map, | |
779 | __section_nr(ms)); | |
780 | ms->section_mem_map = 0; | |
781 | ms->pageblock_flags = NULL; | |
782 | } | |
cd099682 | 783 | pgdat_resize_unlock(pgdat, &flags); |
ea01ea93 | 784 | |
4b94ffdc DW |
785 | clear_hwpoisoned_pages(memmap + map_offset, |
786 | PAGES_PER_SECTION - map_offset); | |
24b6d416 | 787 | free_section_usemap(memmap, usemap, altmap); |
ea01ea93 | 788 | } |
4edd7cef DR |
789 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
790 | #endif /* CONFIG_MEMORY_HOTPLUG */ |