1 // SPDX-License-Identifier: GPL-2.0
3 * sparse memory mappings.
6 #include <linux/slab.h>
7 #include <linux/mmzone.h>
8 #include <linux/memblock.h>
9 #include <linux/compiler.h>
10 #include <linux/highmem.h>
11 #include <linux/export.h>
12 #include <linux/spinlock.h>
13 #include <linux/vmalloc.h>
14 #include <linux/swap.h>
15 #include <linux/swapops.h>
16 #include <linux/bootmem_info.h>
22 * Permanent SPARSEMEM data:
24 * 1) mem_section - memory sections, mem_map's for valid memory
26 #ifdef CONFIG_SPARSEMEM_EXTREME
27 struct mem_section **mem_section;
29 struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
30 ____cacheline_internodealigned_in_smp;
32 EXPORT_SYMBOL(mem_section);
34 #ifdef NODE_NOT_IN_PAGE_FLAGS
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.
40 #if MAX_NUMNODES <= 256
41 static u8 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
43 static u16 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
46 int page_to_nid(const struct page *page)
48 return section_to_node_table[page_to_section(page)];
50 EXPORT_SYMBOL(page_to_nid);
52 static void set_section_nid(unsigned long section_nr, int nid)
54 section_to_node_table[section_nr] = nid;
56 #else /* !NODE_NOT_IN_PAGE_FLAGS */
57 static inline void set_section_nid(unsigned long section_nr, int nid)
62 #ifdef CONFIG_SPARSEMEM_EXTREME
63 static noinline struct mem_section __ref *sparse_index_alloc(int nid)
65 struct mem_section *section = NULL;
66 unsigned long array_size = SECTIONS_PER_ROOT *
67 sizeof(struct mem_section);
69 if (slab_is_available()) {
70 section = kzalloc_node(array_size, GFP_KERNEL, nid);
72 section = memblock_alloc_node(array_size, SMP_CACHE_BYTES,
75 panic("%s: Failed to allocate %lu bytes nid=%d\n",
76 __func__, array_size, nid);
82 static int __meminit sparse_index_init(unsigned long section_nr, int nid)
84 unsigned long root = SECTION_NR_TO_ROOT(section_nr);
85 struct mem_section *section;
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.
92 * The mem_hotplug_lock resolves the apparent race below.
94 if (mem_section[root])
97 section = sparse_index_alloc(nid);
101 mem_section[root] = section;
105 #else /* !SPARSEMEM_EXTREME */
106 static inline int sparse_index_init(unsigned long section_nr, int nid)
112 #ifdef CONFIG_SPARSEMEM_EXTREME
113 unsigned long __section_nr(struct mem_section *ms)
115 unsigned long root_nr;
116 struct mem_section *root = NULL;
118 for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) {
119 root = __nr_to_section(root_nr * SECTIONS_PER_ROOT);
123 if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
129 return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
132 unsigned long __section_nr(struct mem_section *ms)
134 return (unsigned long)(ms - mem_section[0]);
139 * During early boot, before section_mem_map is used for an actual
140 * mem_map, we use section_mem_map to store the section's NUMA
141 * node. This keeps us from having to use another data structure. The
142 * node information is cleared just before we store the real mem_map.
144 static inline unsigned long sparse_encode_early_nid(int nid)
146 return (nid << SECTION_NID_SHIFT);
149 static inline int sparse_early_nid(struct mem_section *section)
151 return (section->section_mem_map >> SECTION_NID_SHIFT);
154 /* Validate the physical addressing limitations of the model */
155 void __meminit mminit_validate_memmodel_limits(unsigned long *start_pfn,
156 unsigned long *end_pfn)
158 unsigned long max_sparsemem_pfn = 1UL << (MAX_PHYSMEM_BITS-PAGE_SHIFT);
161 * Sanity checks - do not allow an architecture to pass
162 * in larger pfns than the maximum scope of sparsemem:
164 if (*start_pfn > max_sparsemem_pfn) {
165 mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
166 "Start of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
167 *start_pfn, *end_pfn, max_sparsemem_pfn);
169 *start_pfn = max_sparsemem_pfn;
170 *end_pfn = max_sparsemem_pfn;
171 } else if (*end_pfn > max_sparsemem_pfn) {
172 mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
173 "End of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
174 *start_pfn, *end_pfn, max_sparsemem_pfn);
176 *end_pfn = max_sparsemem_pfn;
181 * There are a number of times that we loop over NR_MEM_SECTIONS,
182 * looking for section_present() on each. But, when we have very
183 * large physical address spaces, NR_MEM_SECTIONS can also be
184 * very large which makes the loops quite long.
186 * Keeping track of this gives us an easy way to break out of
189 unsigned long __highest_present_section_nr;
190 static void section_mark_present(struct mem_section *ms)
192 unsigned long section_nr = __section_nr(ms);
194 if (section_nr > __highest_present_section_nr)
195 __highest_present_section_nr = section_nr;
197 ms->section_mem_map |= SECTION_MARKED_PRESENT;
200 #define for_each_present_section_nr(start, section_nr) \
201 for (section_nr = next_present_section_nr(start-1); \
202 ((section_nr != -1) && \
203 (section_nr <= __highest_present_section_nr)); \
204 section_nr = next_present_section_nr(section_nr))
206 static inline unsigned long first_present_section_nr(void)
208 return next_present_section_nr(-1);
211 #ifdef CONFIG_SPARSEMEM_VMEMMAP
212 static void subsection_mask_set(unsigned long *map, unsigned long pfn,
213 unsigned long nr_pages)
215 int idx = subsection_map_index(pfn);
216 int end = subsection_map_index(pfn + nr_pages - 1);
218 bitmap_set(map, idx, end - idx + 1);
221 void __init subsection_map_init(unsigned long pfn, unsigned long nr_pages)
223 int end_sec = pfn_to_section_nr(pfn + nr_pages - 1);
224 unsigned long nr, start_sec = pfn_to_section_nr(pfn);
229 for (nr = start_sec; nr <= end_sec; nr++) {
230 struct mem_section *ms;
233 pfns = min(nr_pages, PAGES_PER_SECTION
234 - (pfn & ~PAGE_SECTION_MASK));
235 ms = __nr_to_section(nr);
236 subsection_mask_set(ms->usage->subsection_map, pfn, pfns);
238 pr_debug("%s: sec: %lu pfns: %lu set(%d, %d)\n", __func__, nr,
239 pfns, subsection_map_index(pfn),
240 subsection_map_index(pfn + pfns - 1));
247 void __init subsection_map_init(unsigned long pfn, unsigned long nr_pages)
252 /* Record a memory area against a node. */
253 static void __init memory_present(int nid, unsigned long start, unsigned long end)
257 #ifdef CONFIG_SPARSEMEM_EXTREME
258 if (unlikely(!mem_section)) {
259 unsigned long size, align;
261 size = sizeof(struct mem_section *) * NR_SECTION_ROOTS;
262 align = 1 << (INTERNODE_CACHE_SHIFT);
263 mem_section = memblock_alloc(size, align);
265 panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
266 __func__, size, align);
270 start &= PAGE_SECTION_MASK;
271 mminit_validate_memmodel_limits(&start, &end);
272 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
273 unsigned long section = pfn_to_section_nr(pfn);
274 struct mem_section *ms;
276 sparse_index_init(section, nid);
277 set_section_nid(section, nid);
279 ms = __nr_to_section(section);
280 if (!ms->section_mem_map) {
281 ms->section_mem_map = sparse_encode_early_nid(nid) |
283 section_mark_present(ms);
289 * Mark all memblocks as present using memory_present().
290 * This is a convenience function that is useful to mark all of the systems
291 * memory as present during initialization.
293 static void __init memblocks_present(void)
295 unsigned long start, end;
298 for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, &nid)
299 memory_present(nid, start, end);
303 * Subtle, we encode the real pfn into the mem_map such that
304 * the identity pfn - section_mem_map will return the actual
305 * physical page frame number.
307 static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
309 unsigned long coded_mem_map =
310 (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
311 BUILD_BUG_ON(SECTION_MAP_LAST_BIT > (1UL<<PFN_SECTION_SHIFT));
312 BUG_ON(coded_mem_map & ~SECTION_MAP_MASK);
313 return coded_mem_map;
316 #ifdef CONFIG_MEMORY_HOTPLUG
318 * Decode mem_map from the coded memmap
320 struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
322 /* mask off the extra low bits of information */
323 coded_mem_map &= SECTION_MAP_MASK;
324 return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
326 #endif /* CONFIG_MEMORY_HOTPLUG */
328 static void __meminit sparse_init_one_section(struct mem_section *ms,
329 unsigned long pnum, struct page *mem_map,
330 struct mem_section_usage *usage, unsigned long flags)
332 ms->section_mem_map &= ~SECTION_MAP_MASK;
333 ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum)
334 | SECTION_HAS_MEM_MAP | flags;
338 static unsigned long usemap_size(void)
340 return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long);
343 size_t mem_section_usage_size(void)
345 return sizeof(struct mem_section_usage) + usemap_size();
348 static inline phys_addr_t pgdat_to_phys(struct pglist_data *pgdat)
351 return __pa_symbol(pgdat);
357 #ifdef CONFIG_MEMORY_HOTREMOVE
358 static struct mem_section_usage * __init
359 sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
362 struct mem_section_usage *usage;
363 unsigned long goal, limit;
366 * A page may contain usemaps for other sections preventing the
367 * page being freed and making a section unremovable while
368 * other sections referencing the usemap remain active. Similarly,
369 * a pgdat can prevent a section being removed. If section A
370 * contains a pgdat and section B contains the usemap, both
371 * sections become inter-dependent. This allocates usemaps
372 * from the same section as the pgdat where possible to avoid
375 goal = pgdat_to_phys(pgdat) & (PAGE_SECTION_MASK << PAGE_SHIFT);
376 limit = goal + (1UL << PA_SECTION_SHIFT);
377 nid = early_pfn_to_nid(goal >> PAGE_SHIFT);
379 usage = memblock_alloc_try_nid(size, SMP_CACHE_BYTES, goal, limit, nid);
380 if (!usage && limit) {
387 static void __init check_usemap_section_nr(int nid,
388 struct mem_section_usage *usage)
390 unsigned long usemap_snr, pgdat_snr;
391 static unsigned long old_usemap_snr;
392 static unsigned long old_pgdat_snr;
393 struct pglist_data *pgdat = NODE_DATA(nid);
397 if (!old_usemap_snr) {
398 old_usemap_snr = NR_MEM_SECTIONS;
399 old_pgdat_snr = NR_MEM_SECTIONS;
402 usemap_snr = pfn_to_section_nr(__pa(usage) >> PAGE_SHIFT);
403 pgdat_snr = pfn_to_section_nr(pgdat_to_phys(pgdat) >> PAGE_SHIFT);
404 if (usemap_snr == pgdat_snr)
407 if (old_usemap_snr == usemap_snr && old_pgdat_snr == pgdat_snr)
408 /* skip redundant message */
411 old_usemap_snr = usemap_snr;
412 old_pgdat_snr = pgdat_snr;
414 usemap_nid = sparse_early_nid(__nr_to_section(usemap_snr));
415 if (usemap_nid != nid) {
416 pr_info("node %d must be removed before remove section %ld\n",
421 * There is a circular dependency.
422 * Some platforms allow un-removable section because they will just
423 * gather other removable sections for dynamic partitioning.
424 * Just notify un-removable section's number here.
426 pr_info("Section %ld and %ld (node %d) have a circular dependency on usemap and pgdat allocations\n",
427 usemap_snr, pgdat_snr, nid);
430 static struct mem_section_usage * __init
431 sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
434 return memblock_alloc_node(size, SMP_CACHE_BYTES, pgdat->node_id);
437 static void __init check_usemap_section_nr(int nid,
438 struct mem_section_usage *usage)
441 #endif /* CONFIG_MEMORY_HOTREMOVE */
443 #ifdef CONFIG_SPARSEMEM_VMEMMAP
444 static unsigned long __init section_map_size(void)
446 return ALIGN(sizeof(struct page) * PAGES_PER_SECTION, PMD_SIZE);
450 static unsigned long __init section_map_size(void)
452 return PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION);
455 struct page __init *__populate_section_memmap(unsigned long pfn,
456 unsigned long nr_pages, int nid, struct vmem_altmap *altmap)
458 unsigned long size = section_map_size();
459 struct page *map = sparse_buffer_alloc(size);
460 phys_addr_t addr = __pa(MAX_DMA_ADDRESS);
465 map = memblock_alloc_try_nid_raw(size, size, addr,
466 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
468 panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%pa\n",
469 __func__, size, PAGE_SIZE, nid, &addr);
473 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
475 static void *sparsemap_buf __meminitdata;
476 static void *sparsemap_buf_end __meminitdata;
478 static inline void __meminit sparse_buffer_free(unsigned long size)
480 WARN_ON(!sparsemap_buf || size == 0);
481 memblock_free_early(__pa(sparsemap_buf), size);
484 static void __init sparse_buffer_init(unsigned long size, int nid)
486 phys_addr_t addr = __pa(MAX_DMA_ADDRESS);
487 WARN_ON(sparsemap_buf); /* forgot to call sparse_buffer_fini()? */
489 * Pre-allocated buffer is mainly used by __populate_section_memmap
490 * and we want it to be properly aligned to the section size - this is
491 * especially the case for VMEMMAP which maps memmap to PMDs
493 sparsemap_buf = memblock_alloc_exact_nid_raw(size, section_map_size(),
494 addr, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
495 sparsemap_buf_end = sparsemap_buf + size;
498 static void __init sparse_buffer_fini(void)
500 unsigned long size = sparsemap_buf_end - sparsemap_buf;
502 if (sparsemap_buf && size > 0)
503 sparse_buffer_free(size);
504 sparsemap_buf = NULL;
507 void * __meminit sparse_buffer_alloc(unsigned long size)
512 ptr = (void *) roundup((unsigned long)sparsemap_buf, size);
513 if (ptr + size > sparsemap_buf_end)
516 /* Free redundant aligned space */
517 if ((unsigned long)(ptr - sparsemap_buf) > 0)
518 sparse_buffer_free((unsigned long)(ptr - sparsemap_buf));
519 sparsemap_buf = ptr + size;
525 void __weak __meminit vmemmap_populate_print_last(void)
530 * Initialize sparse on a specific node. The node spans [pnum_begin, pnum_end)
531 * And number of present sections in this node is map_count.
533 static void __init sparse_init_nid(int nid, unsigned long pnum_begin,
534 unsigned long pnum_end,
535 unsigned long map_count)
537 struct mem_section_usage *usage;
541 usage = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nid),
542 mem_section_usage_size() * map_count);
544 pr_err("%s: node[%d] usemap allocation failed", __func__, nid);
547 sparse_buffer_init(map_count * section_map_size(), nid);
548 for_each_present_section_nr(pnum_begin, pnum) {
549 unsigned long pfn = section_nr_to_pfn(pnum);
551 if (pnum >= pnum_end)
554 map = __populate_section_memmap(pfn, PAGES_PER_SECTION,
557 pr_err("%s: node[%d] memory map backing failed. Some memory will not be available.",
560 sparse_buffer_fini();
563 check_usemap_section_nr(nid, usage);
564 sparse_init_one_section(__nr_to_section(pnum), pnum, map, usage,
566 usage = (void *) usage + mem_section_usage_size();
568 sparse_buffer_fini();
571 /* We failed to allocate, mark all the following pnums as not present */
572 for_each_present_section_nr(pnum_begin, pnum) {
573 struct mem_section *ms;
575 if (pnum >= pnum_end)
577 ms = __nr_to_section(pnum);
578 ms->section_mem_map = 0;
583 * Allocate the accumulated non-linear sections, allocate a mem_map
584 * for each and record the physical to section mapping.
586 void __init sparse_init(void)
588 unsigned long pnum_end, pnum_begin, map_count = 1;
593 pnum_begin = first_present_section_nr();
594 nid_begin = sparse_early_nid(__nr_to_section(pnum_begin));
596 /* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */
597 set_pageblock_order();
599 for_each_present_section_nr(pnum_begin + 1, pnum_end) {
600 int nid = sparse_early_nid(__nr_to_section(pnum_end));
602 if (nid == nid_begin) {
606 /* Init node with sections in range [pnum_begin, pnum_end) */
607 sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count);
609 pnum_begin = pnum_end;
612 /* cover the last node */
613 sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count);
614 vmemmap_populate_print_last();
617 #ifdef CONFIG_MEMORY_HOTPLUG
619 /* Mark all memory sections within the pfn range as online */
620 void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
624 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
625 unsigned long section_nr = pfn_to_section_nr(pfn);
626 struct mem_section *ms;
628 /* onlining code should never touch invalid ranges */
629 if (WARN_ON(!valid_section_nr(section_nr)))
632 ms = __nr_to_section(section_nr);
633 ms->section_mem_map |= SECTION_IS_ONLINE;
637 /* Mark all memory sections within the pfn range as offline */
638 void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
642 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
643 unsigned long section_nr = pfn_to_section_nr(pfn);
644 struct mem_section *ms;
647 * TODO this needs some double checking. Offlining code makes
648 * sure to check pfn_valid but those checks might be just bogus
650 if (WARN_ON(!valid_section_nr(section_nr)))
653 ms = __nr_to_section(section_nr);
654 ms->section_mem_map &= ~SECTION_IS_ONLINE;
658 #ifdef CONFIG_SPARSEMEM_VMEMMAP
659 static struct page * __meminit populate_section_memmap(unsigned long pfn,
660 unsigned long nr_pages, int nid, struct vmem_altmap *altmap)
662 return __populate_section_memmap(pfn, nr_pages, nid, altmap);
665 static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,
666 struct vmem_altmap *altmap)
668 unsigned long start = (unsigned long) pfn_to_page(pfn);
669 unsigned long end = start + nr_pages * sizeof(struct page);
671 vmemmap_free(start, end, altmap);
673 static void free_map_bootmem(struct page *memmap)
675 unsigned long start = (unsigned long)memmap;
676 unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
678 vmemmap_free(start, end, NULL);
681 static int clear_subsection_map(unsigned long pfn, unsigned long nr_pages)
683 DECLARE_BITMAP(map, SUBSECTIONS_PER_SECTION) = { 0 };
684 DECLARE_BITMAP(tmp, SUBSECTIONS_PER_SECTION) = { 0 };
685 struct mem_section *ms = __pfn_to_section(pfn);
686 unsigned long *subsection_map = ms->usage
687 ? &ms->usage->subsection_map[0] : NULL;
689 subsection_mask_set(map, pfn, nr_pages);
691 bitmap_and(tmp, map, subsection_map, SUBSECTIONS_PER_SECTION);
693 if (WARN(!subsection_map || !bitmap_equal(tmp, map, SUBSECTIONS_PER_SECTION),
694 "section already deactivated (%#lx + %ld)\n",
698 bitmap_xor(subsection_map, map, subsection_map, SUBSECTIONS_PER_SECTION);
702 static bool is_subsection_map_empty(struct mem_section *ms)
704 return bitmap_empty(&ms->usage->subsection_map[0],
705 SUBSECTIONS_PER_SECTION);
708 static int fill_subsection_map(unsigned long pfn, unsigned long nr_pages)
710 struct mem_section *ms = __pfn_to_section(pfn);
711 DECLARE_BITMAP(map, SUBSECTIONS_PER_SECTION) = { 0 };
712 unsigned long *subsection_map;
715 subsection_mask_set(map, pfn, nr_pages);
717 subsection_map = &ms->usage->subsection_map[0];
719 if (bitmap_empty(map, SUBSECTIONS_PER_SECTION))
721 else if (bitmap_intersects(map, subsection_map, SUBSECTIONS_PER_SECTION))
724 bitmap_or(subsection_map, map, subsection_map,
725 SUBSECTIONS_PER_SECTION);
730 struct page * __meminit populate_section_memmap(unsigned long pfn,
731 unsigned long nr_pages, int nid, struct vmem_altmap *altmap)
733 return kvmalloc_node(array_size(sizeof(struct page),
734 PAGES_PER_SECTION), GFP_KERNEL, nid);
737 static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,
738 struct vmem_altmap *altmap)
740 kvfree(pfn_to_page(pfn));
743 static void free_map_bootmem(struct page *memmap)
745 unsigned long maps_section_nr, removing_section_nr, i;
746 unsigned long magic, nr_pages;
747 struct page *page = virt_to_page(memmap);
749 nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
752 for (i = 0; i < nr_pages; i++, page++) {
753 magic = (unsigned long) page->freelist;
755 BUG_ON(magic == NODE_INFO);
757 maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
758 removing_section_nr = page_private(page);
761 * When this function is called, the removing section is
762 * logical offlined state. This means all pages are isolated
763 * from page allocator. If removing section's memmap is placed
764 * on the same section, it must not be freed.
765 * If it is freed, page allocator may allocate it which will
766 * be removed physically soon.
768 if (maps_section_nr != removing_section_nr)
769 put_page_bootmem(page);
773 static int clear_subsection_map(unsigned long pfn, unsigned long nr_pages)
778 static bool is_subsection_map_empty(struct mem_section *ms)
783 static int fill_subsection_map(unsigned long pfn, unsigned long nr_pages)
787 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
790 * To deactivate a memory region, there are 3 cases to handle across
791 * two configurations (SPARSEMEM_VMEMMAP={y,n}):
793 * 1. deactivation of a partial hot-added section (only possible in
794 * the SPARSEMEM_VMEMMAP=y case).
795 * a) section was present at memory init.
796 * b) section was hot-added post memory init.
797 * 2. deactivation of a complete hot-added section.
798 * 3. deactivation of a complete section from memory init.
800 * For 1, when subsection_map does not empty we will not be freeing the
801 * usage map, but still need to free the vmemmap range.
803 * For 2 and 3, the SPARSEMEM_VMEMMAP={y,n} cases are unified
805 static void section_deactivate(unsigned long pfn, unsigned long nr_pages,
806 struct vmem_altmap *altmap)
808 struct mem_section *ms = __pfn_to_section(pfn);
809 bool section_is_early = early_section(ms);
810 struct page *memmap = NULL;
813 if (clear_subsection_map(pfn, nr_pages))
816 empty = is_subsection_map_empty(ms);
818 unsigned long section_nr = pfn_to_section_nr(pfn);
821 * When removing an early section, the usage map is kept (as the
822 * usage maps of other sections fall into the same page). It
823 * will be re-used when re-adding the section - which is then no
824 * longer an early section. If the usage map is PageReserved, it
825 * was allocated during boot.
827 if (!PageReserved(virt_to_page(ms->usage))) {
831 memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
833 * Mark the section invalid so that valid_section()
834 * return false. This prevents code from dereferencing
837 ms->section_mem_map &= ~SECTION_HAS_MEM_MAP;
841 * The memmap of early sections is always fully populated. See
842 * section_activate() and pfn_valid() .
844 if (!section_is_early)
845 depopulate_section_memmap(pfn, nr_pages, altmap);
847 free_map_bootmem(memmap);
850 ms->section_mem_map = (unsigned long)NULL;
853 static struct page * __meminit section_activate(int nid, unsigned long pfn,
854 unsigned long nr_pages, struct vmem_altmap *altmap)
856 struct mem_section *ms = __pfn_to_section(pfn);
857 struct mem_section_usage *usage = NULL;
862 usage = kzalloc(mem_section_usage_size(), GFP_KERNEL);
864 return ERR_PTR(-ENOMEM);
868 rc = fill_subsection_map(pfn, nr_pages);
877 * The early init code does not consider partially populated
878 * initial sections, it simply assumes that memory will never be
879 * referenced. If we hot-add memory into such a section then we
880 * do not need to populate the memmap and can simply reuse what
883 if (nr_pages < PAGES_PER_SECTION && early_section(ms))
884 return pfn_to_page(pfn);
886 memmap = populate_section_memmap(pfn, nr_pages, nid, altmap);
888 section_deactivate(pfn, nr_pages, altmap);
889 return ERR_PTR(-ENOMEM);
896 * sparse_add_section - add a memory section, or populate an existing one
897 * @nid: The node to add section on
898 * @start_pfn: start pfn of the memory range
899 * @nr_pages: number of pfns to add in the section
900 * @altmap: device page map
902 * This is only intended for hotplug.
904 * Note that only VMEMMAP supports sub-section aligned hotplug,
905 * the proper alignment and size are gated by check_pfn_span().
910 * * -EEXIST - Section has been present.
911 * * -ENOMEM - Out of memory.
913 int __meminit sparse_add_section(int nid, unsigned long start_pfn,
914 unsigned long nr_pages, struct vmem_altmap *altmap)
916 unsigned long section_nr = pfn_to_section_nr(start_pfn);
917 struct mem_section *ms;
921 ret = sparse_index_init(section_nr, nid);
925 memmap = section_activate(nid, start_pfn, nr_pages, altmap);
927 return PTR_ERR(memmap);
930 * Poison uninitialized struct pages in order to catch invalid flags
933 page_init_poison(memmap, sizeof(struct page) * nr_pages);
935 ms = __nr_to_section(section_nr);
936 set_section_nid(section_nr, nid);
937 section_mark_present(ms);
939 /* Align memmap to section boundary in the subsection case */
940 if (section_nr_to_pfn(section_nr) != start_pfn)
941 memmap = pfn_to_page(section_nr_to_pfn(section_nr));
942 sparse_init_one_section(ms, section_nr, memmap, ms->usage, 0);
947 #ifdef CONFIG_MEMORY_FAILURE
948 static void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
953 * A further optimization is to have per section refcounted
954 * num_poisoned_pages. But that would need more space per memmap, so
955 * for now just do a quick global check to speed up this routine in the
956 * absence of bad pages.
958 if (atomic_long_read(&num_poisoned_pages) == 0)
961 for (i = 0; i < nr_pages; i++) {
962 if (PageHWPoison(&memmap[i])) {
963 num_poisoned_pages_dec();
964 ClearPageHWPoison(&memmap[i]);
969 static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
974 void sparse_remove_section(struct mem_section *ms, unsigned long pfn,
975 unsigned long nr_pages, unsigned long map_offset,
976 struct vmem_altmap *altmap)
978 clear_hwpoisoned_pages(pfn_to_page(pfn) + map_offset,
979 nr_pages - map_offset);
980 section_deactivate(pfn, nr_pages, altmap);
982 #endif /* CONFIG_MEMORY_HOTPLUG */