[PATCH 22/32] mm: taint device sections before setting subsections valid
From: David Hildenbrand (Arm)
Date: Sun Nov 29 2026 - 05:13:59 EST
Adding ZONE_DEVICE memory to an online section can make the new
subsections valid before the section is tainted.
Consequently, a concurrent pfn_to_online_page() can return an
uninitialized device page, triggering a BUG with CONFIG_DEBUG_VM.
Let's fix it by tainting the section in fill_subsection_map() before
setting the validity bits. Pair the write barrier with test_bit_acquire()
in pfn_to_online_page() by introducing a new pfn_section_valid_acquire().
This has low urgency, as it requires racing with hotplug of ZONE_DEVICE
memory, which is privileged.
Fixes: 1f90a3477df3 ("mm: teach pfn_to_online_page() about ZONE_DEVICE section collisions")
Cc: stable@xxxxxxxxxxxxxxx
Reported-by: Chris Mason <mason@xxxxxxxxxx>
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
include/linux/mmzone.h | 9 +++++++++
mm/memory_hotplug.c | 34 +++++++---------------------------
mm/sparse-vmemmap.c | 39 +++++++++++++++++++++++++++++----------
3 files changed, 45 insertions(+), 37 deletions(-)
diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 6442e662aa4f4..8cbe7e28040a5 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -2235,6 +2235,15 @@ static inline int pfn_section_valid(struct mem_section *ms, unsigned long pfn)
return usage ? test_bit(idx, usage->subsection_map) : 0;
}
+static inline int pfn_section_valid_acquire(struct mem_section *ms,
+ unsigned long pfn)
+{
+ const int idx = subsection_map_index(pfn);
+ struct mem_section_usage *usage = READ_ONCE(ms->usage);
+
+ return usage ? test_bit_acquire(idx, usage->subsection_map) : 0;
+}
+
static inline bool pfn_section_first_valid(struct mem_section *ms, unsigned long *pfn)
{
struct mem_section_usage *usage = READ_ONCE(ms->usage);
diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index 41fcdc04e5404..21e241823e22d 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -353,7 +353,13 @@ struct page *pfn_to_online_page(unsigned long pfn)
if (!online_section(ms))
return NULL;
- if (!pfn_section_valid(ms, pfn))
+ /*
+ * ZONE_DEVICE subsections in an online section can only become valid
+ * after the corresponding pgmap was registered and the sections were
+ * tainted. Look at the subsection first. The acquire pairs with
+ * smp_wmb() in section_taint_zone_device().
+ */
+ if (!pfn_section_valid_acquire(ms, pfn))
return NULL;
if (!online_device_section(ms))
@@ -713,19 +719,6 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon
}
-#ifdef CONFIG_ZONE_DEVICE
-static void section_taint_zone_device(unsigned long pfn)
-{
- struct mem_section *ms = __pfn_to_section(pfn);
-
- ms->section_mem_map |= SECTION_TAINT_ZONE_DEVICE;
-}
-#else
-static inline void section_taint_zone_device(unsigned long pfn)
-{
-}
-#endif
-
/*
* Associate the pfn range with the given zone, initializing the memmaps
* and resizing the pgdat/zone data to span the added pages. After this
@@ -748,19 +741,6 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
resize_zone_range(zone, start_pfn, nr_pages);
resize_pgdat_range(pgdat, start_pfn, nr_pages);
- /*
- * Subsection population requires care in pfn_to_online_page().
- * Set the taint to enable the slow path detection of
- * ZONE_DEVICE pages in an otherwise ZONE_{NORMAL,MOVABLE}
- * section.
- */
- if (zone_is_zone_device(zone)) {
- if (!IS_ALIGNED(start_pfn, PAGES_PER_SECTION))
- section_taint_zone_device(start_pfn);
- if (!IS_ALIGNED(start_pfn + nr_pages, PAGES_PER_SECTION))
- section_taint_zone_device(start_pfn + nr_pages);
- }
-
/*
* TODO now we have a visible range of pages which are not associated
* with their zone properly. Not nice but set_pfnblock_migratetype()
diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c
index 5d7e982f94d0a..643d9d1bfa39f 100644
--- a/mm/sparse-vmemmap.c
+++ b/mm/sparse-vmemmap.c
@@ -629,26 +629,45 @@ static bool is_subsection_map_empty(struct mem_section *ms)
SUBSECTIONS_PER_SECTION);
}
-static int fill_subsection_map(unsigned long pfn, unsigned long nr_pages)
+#ifdef CONFIG_ZONE_DEVICE
+static void section_taint_zone_device(struct mem_section *ms,
+ struct dev_pagemap *pgmap, unsigned long nr_pages)
+{
+ if (!pgmap || nr_pages == PAGES_PER_SECTION)
+ return;
+
+ ms->section_mem_map |= SECTION_TAINT_ZONE_DEVICE;
+ /* Pairs with pfn_section_valid_acquire() in pfn_to_online_page(). */
+ smp_wmb();
+}
+#else
+static void section_taint_zone_device(struct mem_section *ms,
+ struct dev_pagemap *pgmap, unsigned long nr_pages)
+{
+}
+#endif
+
+static int fill_subsection_map(unsigned long pfn, unsigned long nr_pages,
+ struct dev_pagemap *pgmap)
{
struct mem_section *ms = __pfn_to_section(pfn);
DECLARE_BITMAP(map, SUBSECTIONS_PER_SECTION) = { 0 };
unsigned long *subsection_map;
- int rc = 0;
subsection_mask_set(map, pfn, nr_pages);
subsection_map = &ms->usage->subsection_map[0];
if (bitmap_empty(map, SUBSECTIONS_PER_SECTION))
- rc = -EINVAL;
- else if (bitmap_intersects(map, subsection_map, SUBSECTIONS_PER_SECTION))
- rc = -EEXIST;
- else
- bitmap_or(subsection_map, map, subsection_map,
- SUBSECTIONS_PER_SECTION);
+ return -EINVAL;
+ if (bitmap_intersects(map, subsection_map, SUBSECTIONS_PER_SECTION))
+ return -EEXIST;
+
+ /* Taint the sections before marking the subsection map valid. */
+ section_taint_zone_device(ms, pgmap, nr_pages);
+ bitmap_or(subsection_map, map, subsection_map, SUBSECTIONS_PER_SECTION);
- return rc;
+ return 0;
}
/*
@@ -733,7 +752,7 @@ static struct page * __meminit section_activate(int nid, unsigned long pfn,
ms->usage = usage;
}
- rc = fill_subsection_map(pfn, nr_pages);
+ rc = fill_subsection_map(pfn, nr_pages, pgmap);
if (rc) {
if (usage)
ms->usage = NULL;
--
2.56.1