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