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