Skip to content

Commit ba72b4c

Browse files
djbwtorvalds
authored andcommitted
mm/sparsemem: support sub-section hotplug
The libnvdimm sub-system has suffered a series of hacks and broken workarounds for the memory-hotplug implementation's awkward section-aligned (128MB) granularity. For example the following backtrace is emitted when attempting arch_add_memory() with physical address ranges that intersect 'System RAM' (RAM) with 'Persistent Memory' (PMEM) within a given section: # cat /proc/iomem | grep -A1 -B1 Persistent\ Memory 100000000-1ffffffff : System RAM 200000000-303ffffff : Persistent Memory (legacy) 304000000-43fffffff : System RAM 440000000-23ffffffff : Persistent Memory 2400000000-43bfffffff : Persistent Memory 2400000000-43bfffffff : namespace2.0 WARNING: CPU: 38 PID: 928 at arch/x86/mm/init_64.c:850 add_pages+0x5c/0x60 [..] RIP: 0010:add_pages+0x5c/0x60 [..] Call Trace: devm_memremap_pages+0x460/0x6e0 pmem_attach_disk+0x29e/0x680 [nd_pmem] ? nd_dax_probe+0xfc/0x120 [libnvdimm] nvdimm_bus_probe+0x66/0x160 [libnvdimm] It was discovered that the problem goes beyond RAM vs PMEM collisions as some platform produce PMEM vs PMEM collisions within a given section. The libnvdimm workaround for that case revealed that the libnvdimm section-alignment-padding implementation has been broken for a long while. A fix for that long-standing breakage introduces as many problems as it solves as it would require a backward-incompatible change to the namespace metadata interpretation. Instead of that dubious route [1], address the root problem in the memory-hotplug implementation. Note that EEXIST is no longer treated as success as that is how sparse_add_section() reports subsection collisions, it was also obviated by recent changes to perform the request_region() for 'System RAM' before arch_add_memory() in the add_memory() sequence. [1] https://lore.kernel.org/r/155000671719.348031.2347363160141119237.stgit@dwillia2-desk3.amr.corp.intel.com [[email protected]: fix deactivate_section for early sections] Link: http://lkml.kernel.org/r/[email protected] Link: http://lkml.kernel.org/r/156092354368.979959.6232443923440952359.stgit@dwillia2-desk3.amr.corp.intel.com Signed-off-by: Dan Williams <[email protected]> Signed-off-by: Oscar Salvador <[email protected]> Tested-by: Aneesh Kumar K.V <[email protected]> [ppc64] Reviewed-by: Oscar Salvador <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Vlastimil Babka <[email protected]> Cc: Logan Gunthorpe <[email protected]> Cc: Pavel Tatashin <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Jane Chu <[email protected]> Cc: Jeff Moyer <[email protected]> Cc: Jérôme Glisse <[email protected]> Cc: Jonathan Corbet <[email protected]> Cc: Mike Rapoport <[email protected]> Cc: Toshi Kani <[email protected]> Cc: Wei Yang <[email protected]> Cc: Jason Gunthorpe <[email protected]> Cc: Christoph Hellwig <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 7ea6216 commit ba72b4c

File tree

4 files changed

+141
-96
lines changed

4 files changed

+141
-96
lines changed

include/linux/memory_hotplug.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ extern void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
348348
extern bool is_memblock_offlined(struct memory_block *mem);
349349
extern int sparse_add_section(int nid, unsigned long pfn,
350350
unsigned long nr_pages, struct vmem_altmap *altmap);
351-
extern void sparse_remove_one_section(struct mem_section *ms,
351+
extern void sparse_remove_section(struct mem_section *ms,
352352
unsigned long pfn, unsigned long nr_pages,
353353
unsigned long map_offset, struct vmem_altmap *altmap);
354354
extern struct page *sparse_decode_mem_map(unsigned long coded_mem_map,

mm/memory_hotplug.c

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -252,18 +252,6 @@ void __init register_page_bootmem_info_node(struct pglist_data *pgdat)
252252
}
253253
#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
254254

255-
static int __meminit __add_section(int nid, unsigned long pfn,
256-
unsigned long nr_pages, struct vmem_altmap *altmap)
257-
{
258-
int ret;
259-
260-
if (pfn_valid(pfn))
261-
return -EEXIST;
262-
263-
ret = sparse_add_section(nid, pfn, nr_pages, altmap);
264-
return ret < 0 ? ret : 0;
265-
}
266-
267255
static int check_pfn_span(unsigned long pfn, unsigned long nr_pages,
268256
const char *reason)
269257
{
@@ -327,18 +315,11 @@ int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
327315

328316
pfns = min(nr_pages, PAGES_PER_SECTION
329317
- (pfn & ~PAGE_SECTION_MASK));
330-
err = __add_section(nid, pfn, pfns, altmap);
318+
err = sparse_add_section(nid, pfn, pfns, altmap);
319+
if (err)
320+
break;
331321
pfn += pfns;
332322
nr_pages -= pfns;
333-
334-
/*
335-
* EEXIST is finally dealt with by ioresource collision
336-
* check. see add_memory() => register_memory_resource()
337-
* Warning will be printed if there is collision.
338-
*/
339-
if (err && (err != -EEXIST))
340-
break;
341-
err = 0;
342323
cond_resched();
343324
}
344325
vmemmap_populate_print_last();
@@ -541,7 +522,7 @@ static void __remove_section(struct zone *zone, unsigned long pfn,
541522
return;
542523

543524
__remove_zone(zone, pfn, nr_pages);
544-
sparse_remove_one_section(ms, pfn, nr_pages, map_offset, altmap);
525+
sparse_remove_section(ms, pfn, nr_pages, map_offset, altmap);
545526
}
546527

547528
/**

mm/page_alloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5974,7 +5974,7 @@ void __ref memmap_init_zone_device(struct zone *zone,
59745974
* pfn out of zone.
59755975
*
59765976
* Please note that MEMMAP_HOTPLUG path doesn't clear memmap
5977-
* because this is done early in sparse_add_one_section
5977+
* because this is done early in section_activate()
59785978
*/
59795979
if (!(pfn & (pageblock_nr_pages - 1))) {
59805980
set_pageblock_migratetype(page, MIGRATE_MOVABLE);

mm/sparse.c

Lines changed: 135 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,15 @@ static int __meminit sparse_index_init(unsigned long section_nr, int nid)
8383
unsigned long root = SECTION_NR_TO_ROOT(section_nr);
8484
struct mem_section *section;
8585

86+
/*
87+
* An existing section is possible in the sub-section hotplug
88+
* case. First hot-add instantiates, follow-on hot-add reuses
89+
* the existing section.
90+
*
91+
* The mem_hotplug_lock resolves the apparent race below.
92+
*/
8693
if (mem_section[root])
87-
return -EEXIST;
94+
return 0;
8895

8996
section = sparse_index_alloc(nid);
9097
if (!section)
@@ -715,10 +722,120 @@ static void free_map_bootmem(struct page *memmap)
715722
}
716723
#endif /* CONFIG_SPARSEMEM_VMEMMAP */
717724

725+
static void section_deactivate(unsigned long pfn, unsigned long nr_pages,
726+
struct vmem_altmap *altmap)
727+
{
728+
DECLARE_BITMAP(map, SUBSECTIONS_PER_SECTION) = { 0 };
729+
DECLARE_BITMAP(tmp, SUBSECTIONS_PER_SECTION) = { 0 };
730+
struct mem_section *ms = __pfn_to_section(pfn);
731+
bool section_is_early = early_section(ms);
732+
struct page *memmap = NULL;
733+
unsigned long *subsection_map = ms->usage
734+
? &ms->usage->subsection_map[0] : NULL;
735+
736+
subsection_mask_set(map, pfn, nr_pages);
737+
if (subsection_map)
738+
bitmap_and(tmp, map, subsection_map, SUBSECTIONS_PER_SECTION);
739+
740+
if (WARN(!subsection_map || !bitmap_equal(tmp, map, SUBSECTIONS_PER_SECTION),
741+
"section already deactivated (%#lx + %ld)\n",
742+
pfn, nr_pages))
743+
return;
744+
745+
/*
746+
* There are 3 cases to handle across two configurations
747+
* (SPARSEMEM_VMEMMAP={y,n}):
748+
*
749+
* 1/ deactivation of a partial hot-added section (only possible
750+
* in the SPARSEMEM_VMEMMAP=y case).
751+
* a/ section was present at memory init
752+
* b/ section was hot-added post memory init
753+
* 2/ deactivation of a complete hot-added section
754+
* 3/ deactivation of a complete section from memory init
755+
*
756+
* For 1/, when subsection_map does not empty we will not be
757+
* freeing the usage map, but still need to free the vmemmap
758+
* range.
759+
*
760+
* For 2/ and 3/ the SPARSEMEM_VMEMMAP={y,n} cases are unified
761+
*/
762+
bitmap_xor(subsection_map, map, subsection_map, SUBSECTIONS_PER_SECTION);
763+
if (bitmap_empty(subsection_map, SUBSECTIONS_PER_SECTION)) {
764+
unsigned long section_nr = pfn_to_section_nr(pfn);
765+
766+
if (!section_is_early) {
767+
kfree(ms->usage);
768+
ms->usage = NULL;
769+
}
770+
memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
771+
ms->section_mem_map = sparse_encode_mem_map(NULL, section_nr);
772+
}
773+
774+
if (section_is_early && memmap)
775+
free_map_bootmem(memmap);
776+
else
777+
depopulate_section_memmap(pfn, nr_pages, altmap);
778+
}
779+
780+
static struct page * __meminit section_activate(int nid, unsigned long pfn,
781+
unsigned long nr_pages, struct vmem_altmap *altmap)
782+
{
783+
DECLARE_BITMAP(map, SUBSECTIONS_PER_SECTION) = { 0 };
784+
struct mem_section *ms = __pfn_to_section(pfn);
785+
struct mem_section_usage *usage = NULL;
786+
unsigned long *subsection_map;
787+
struct page *memmap;
788+
int rc = 0;
789+
790+
subsection_mask_set(map, pfn, nr_pages);
791+
792+
if (!ms->usage) {
793+
usage = kzalloc(mem_section_usage_size(), GFP_KERNEL);
794+
if (!usage)
795+
return ERR_PTR(-ENOMEM);
796+
ms->usage = usage;
797+
}
798+
subsection_map = &ms->usage->subsection_map[0];
799+
800+
if (bitmap_empty(map, SUBSECTIONS_PER_SECTION))
801+
rc = -EINVAL;
802+
else if (bitmap_intersects(map, subsection_map, SUBSECTIONS_PER_SECTION))
803+
rc = -EEXIST;
804+
else
805+
bitmap_or(subsection_map, map, subsection_map,
806+
SUBSECTIONS_PER_SECTION);
807+
808+
if (rc) {
809+
if (usage)
810+
ms->usage = NULL;
811+
kfree(usage);
812+
return ERR_PTR(rc);
813+
}
814+
815+
/*
816+
* The early init code does not consider partially populated
817+
* initial sections, it simply assumes that memory will never be
818+
* referenced. If we hot-add memory into such a section then we
819+
* do not need to populate the memmap and can simply reuse what
820+
* is already there.
821+
*/
822+
if (nr_pages < PAGES_PER_SECTION && early_section(ms))
823+
return pfn_to_page(pfn);
824+
825+
memmap = populate_section_memmap(pfn, nr_pages, nid, altmap);
826+
if (!memmap) {
827+
section_deactivate(pfn, nr_pages, altmap);
828+
return ERR_PTR(-ENOMEM);
829+
}
830+
831+
return memmap;
832+
}
833+
718834
/**
719-
* sparse_add_one_section - add a memory section
835+
* sparse_add_section - add a memory section, or populate an existing one
720836
* @nid: The node to add section on
721837
* @start_pfn: start pfn of the memory range
838+
* @nr_pages: number of pfns to add in the section
722839
* @altmap: device page map
723840
*
724841
* This is only intended for hotplug.
@@ -732,51 +849,34 @@ int __meminit sparse_add_section(int nid, unsigned long start_pfn,
732849
unsigned long nr_pages, struct vmem_altmap *altmap)
733850
{
734851
unsigned long section_nr = pfn_to_section_nr(start_pfn);
735-
struct mem_section_usage *usage;
736852
struct mem_section *ms;
737853
struct page *memmap;
738854
int ret;
739855

740-
/*
741-
* no locking for this, because it does its own
742-
* plus, it does a kmalloc
743-
*/
744856
ret = sparse_index_init(section_nr, nid);
745-
if (ret < 0 && ret != -EEXIST)
857+
if (ret < 0)
746858
return ret;
747-
ret = 0;
748-
memmap = populate_section_memmap(start_pfn, PAGES_PER_SECTION, nid,
749-
altmap);
750-
if (!memmap)
751-
return -ENOMEM;
752-
usage = kzalloc(mem_section_usage_size(), GFP_KERNEL);
753-
if (!usage) {
754-
depopulate_section_memmap(start_pfn, PAGES_PER_SECTION, altmap);
755-
return -ENOMEM;
756-
}
757859

758-
ms = __pfn_to_section(start_pfn);
759-
if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
760-
ret = -EEXIST;
761-
goto out;
762-
}
860+
memmap = section_activate(nid, start_pfn, nr_pages, altmap);
861+
if (IS_ERR(memmap))
862+
return PTR_ERR(memmap);
763863

764864
/*
765865
* Poison uninitialized struct pages in order to catch invalid flags
766866
* combinations.
767867
*/
768-
page_init_poison(memmap, sizeof(struct page) * PAGES_PER_SECTION);
868+
page_init_poison(pfn_to_page(start_pfn), sizeof(struct page) * nr_pages);
769869

870+
ms = __pfn_to_section(start_pfn);
770871
set_section_nid(section_nr, nid);
771872
section_mark_present(ms);
772-
sparse_init_one_section(ms, section_nr, memmap, usage, 0);
773873

774-
out:
775-
if (ret < 0) {
776-
kfree(usage);
777-
depopulate_section_memmap(start_pfn, PAGES_PER_SECTION, altmap);
778-
}
779-
return ret;
874+
/* Align memmap to section boundary in the subsection case */
875+
if (section_nr_to_pfn(section_nr) != start_pfn)
876+
memmap = pfn_to_kaddr(section_nr_to_pfn(section_nr));
877+
sparse_init_one_section(ms, section_nr, memmap, ms->usage, 0);
878+
879+
return 0;
780880
}
781881

782882
#ifdef CONFIG_MEMORY_FAILURE
@@ -809,48 +909,12 @@ static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
809909
}
810910
#endif
811911

812-
static void free_section_usage(struct mem_section *ms, struct page *memmap,
813-
struct mem_section_usage *usage, unsigned long pfn,
814-
unsigned long nr_pages, struct vmem_altmap *altmap)
815-
{
816-
if (!usage)
817-
return;
818-
819-
/*
820-
* Check to see if allocation came from hot-plug-add
821-
*/
822-
if (!early_section(ms)) {
823-
kfree(usage);
824-
if (memmap)
825-
depopulate_section_memmap(pfn, nr_pages, altmap);
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-
834-
if (memmap)
835-
free_map_bootmem(memmap);
836-
}
837-
838-
void sparse_remove_one_section(struct mem_section *ms, unsigned long pfn,
912+
void sparse_remove_section(struct mem_section *ms, unsigned long pfn,
839913
unsigned long nr_pages, unsigned long map_offset,
840914
struct vmem_altmap *altmap)
841915
{
842-
struct page *memmap = NULL;
843-
struct mem_section_usage *usage = NULL;
844-
845-
if (ms->section_mem_map) {
846-
usage = ms->usage;
847-
memmap = sparse_decode_mem_map(ms->section_mem_map,
848-
__section_nr(ms));
849-
ms->section_mem_map = 0;
850-
ms->usage = NULL;
851-
}
852-
853-
clear_hwpoisoned_pages(memmap + map_offset, nr_pages - map_offset);
854-
free_section_usage(ms, memmap, usage, pfn, nr_pages, altmap);
916+
clear_hwpoisoned_pages(pfn_to_page(pfn) + map_offset,
917+
nr_pages - map_offset);
918+
section_deactivate(pfn, nr_pages, altmap);
855919
}
856920
#endif /* CONFIG_MEMORY_HOTPLUG */

0 commit comments

Comments
 (0)