Skip to contentSkip to conversation

mm: Unify device DAX and HugeTLB vmemmap population paths

PATCH Superseded Muchun Song 5 participants 30 messages Sep 29 – Oct 8

Subsystems (5)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover mm: Unify device DAX and HugeTLB vmemmap population paths

This v3 is based on mm-new commit 2ddb90ee544a, which contains v5 of "mm: Switch device DAX to section-based vmemmap optimization" [1].

This series is split out from the earlier, larger series "mm: Generalize HVO for HugeTLB and device DAX" [2]. While the parent series generalizes vmemmap optimization across HugeTLB and device DAX, this subset addresses a single, self-contained step: unifying their vmemmap population paths.

After the preceding Device DAX conversion, both HugeTLB and Device DAX describe optimized vmemmap mappings through memory-section metadata and use per-zone shared tail vmemmap pages. The generic code, however, still carries a Device DAX-specific population flag and compound-page population path, along with arguments and helpers needed only by that path.

This series first removes VMEMMAP_POPULATE_DAX and moves selection and reference handling for the shared tail page into the common vmemmap population path. It then removes the generic Device DAX-specific compound-page population path and routes section vmemmap population through vmemmap_populate(). The powerpc radix path continues to use its architecture-specific compound-page population implementation for optimizable sections.

The remaining patches remove the unused ptpfn argument, open-code vmemmap_populate_address() now that no caller needs its returned PTE, and add a warning for inconsistent zone initialization of shared tail vmemmap pages.

This is the fourth smaller step toward the broader HVO generalization. After this series, HugeTLB and Device DAX use the same population model instead of parallel generic paths, while powerpc keeps its architecture-specific implementation.

[1] https://lore.kernel.org/[email protected]/
[2] https://lore.kernel.org/[email protected]/

v3:
- Rebase onto mm-new commit 2ddb90ee544a, which contains v5 of the Device DAX section-based vmemmap series.
- Document the exact dependency order as suggested by David Hildenbrand and Ritesh Harjani.
- Use the public order-based optimization predicate in the powerpc path.
- Collect Acked-by tags from Qi Zheng.

v2: https://lore.kernel.org/[email protected]/
- Expand the comments around the slab initialization boundary as suggested by Qi Zheng.
- Collect Acked-by tags from Qi Zheng.
- Defer the patch making vmemmap_populate_compound_pages() static to a follow-up series that cleans up the powerpc vmemmap population code.
- Add a new patch to warn about mismatched zone IDs between a page and its shared tail vmemmap page.

v1: https://lore.kernel.org/[email protected]/

Muchun Song (6):
mm/sparse-vmemmap: drop VMEMMAP_POPULATE_DAX mm/sparse-vmemmap: support device DAX in common vmemmap path mm/sparse-vmemmap: drop Device DAX-specific population path mm/sparse-vmemmap: remove the unused ptpfn argument mm/sparse-vmemmap: open-code vmemmap_populate_address() mm/mm_init: add zone mismatch warning during page init

 arch/powerpc/mm/book3s64/radix_pgtable.c |   3 +
 mm/mm_init.c                             |   5 +-
 mm/sparse-vmemmap.c                      | 208 ++++++-----------------
 3 files changed, 63 insertions(+), 153 deletions(-)

base-commit: 2ddb90ee544ae97215afc4698dc223293997cc43

Signature · 1 line
2.54.0
#1 lore raw

PATCH v3 1/6 mm/sparse-vmemmap: drop VMEMMAP_POPULATE_DAX

VMEMMAP_POPULATE_DAX currently distinguishes DAX vmemmap population in two places: it keeps allocations on the normal path and takes a reference when a backing page is supplied for reuse.

After Device DAX switched to the common per-zone shared tail page, both conditions can be determined locally. DAX supplies ptpfn for every shared tail mapping and requests an allocation only for compound head mappings, whose PFNs are not optimizable. Therefore, vmemmap_optimizable_pfn() alone selects the correct allocation path.

When ptpfn is supplied, the caller is reusing an existing backing page. Once the slab allocator is available, take a reference for each reused mapping to balance the release performed by vmemmap_free(). Although the buddy allocator is available before slab, no vmemmap population occurs in that interval. Earlier mappings are backed by memblock/reserved memory and do not need page reference accounting.

Remove VMEMMAP_POPULATE_DAX and the flags argument from the vmemmap population helpers.

---
v2:
- Explain why the buddy-to-slab initialization gap is safe (suggested by Qi Zheng)
- Collect Acked-by from Qi Zheng

---
 mm/sparse-vmemmap.c | 36 +++++++++++++-----------------------
 1 file changed, 13 insertions(+), 23 deletions(-)
mm/sparse-vmemmap.c +13 -23
index 111efc802465..8219abc6c3e5 100644
@@ -32,12 +32,6 @@
 #include <asm/dma.h>
 #include <asm/tlbflush.h>
 
-/*
- * Flags for vmemmap_populate_range and friends.
- */
-/* Vmemmap population for ZONE_DEVICE compound pages */
-#define VMEMMAP_POPULATE_DAX		0x0001
-
 #include "internal.h"
 #include "mm_init.h"
 #include "sparse.h"
@@ -237,7 +231,7 @@ struct page __ref *vmemmap_shared_tail_page(unsigned int order, struct zone *zon
 #endif
 
 static __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node,
-		struct vmem_altmap *altmap, unsigned long flags)
+		struct vmem_altmap *altmap)
 {
 	struct zone *zone;
 	struct page *page;
@@ -247,7 +241,7 @@ static __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node,
 	 * Device DAX still relies on vmemmap_populate_compound_pages() for
 	 * head/first-tail allocation and tail-page reuse.
 	 */
-	if (!vmemmap_optimizable_pfn(pfn) || flags & VMEMMAP_POPULATE_DAX)
+	if (!vmemmap_optimizable_pfn(pfn))
 		return vmemmap_alloc_block_buf(PAGE_SIZE, node, altmap);
 
 	zone = pfn_to_zone(pfn, node);
@@ -259,8 +253,7 @@ static __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node,
 }
 
 static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node,
-				       struct vmem_altmap *altmap,
-				       unsigned long ptpfn, unsigned long flags)
+		struct vmem_altmap *altmap, unsigned long ptpfn)
 {
 	pte_t *pte = pte_offset_kernel(pmd, addr);
 	unsigned long pfn = page_to_pfn((struct page *)addr);
@@ -269,7 +262,7 @@ static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, in
 		pte_t entry;
 
 		if (ptpfn == (unsigned long)-1) {
-			void *p = vmemmap_alloc_pte(pfn, node, altmap, flags);
+			void *p = vmemmap_alloc_pte(pfn, node, altmap);
 
 			if (!p)
 				return NULL;
@@ -287,7 +280,7 @@ static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, in
 			 * Use try_get_page() to prevent the shared page refcount
 			 * from overflowing.
 			 */
-			if ((flags & VMEMMAP_POPULATE_DAX) &&
+			if (slab_is_available() &&
 			    !try_get_page(pfn_to_page(ptpfn)))
 				return NULL;
 		}
@@ -351,8 +344,7 @@ static pgd_t * __meminit vmemmap_pgd_populate(unsigned long addr, int node)
 
 static pte_t * __meminit vmemmap_populate_address(unsigned long addr, int node,
 					      struct vmem_altmap *altmap,
-					      unsigned long ptpfn,
-					      unsigned long flags)
+					      unsigned long ptpfn)
 {
 	pgd_t *pgd;
 	p4d_t *p4d;
@@ -372,7 +364,7 @@ static pte_t * __meminit vmemmap_populate_address(unsigned long addr, int node,
 	pmd = vmemmap_pmd_populate(pud, addr, node);
 	if (!pmd)
 		return NULL;
-	pte = vmemmap_pte_populate(pmd, addr, node, altmap, ptpfn, flags);
+	pte = vmemmap_pte_populate(pmd, addr, node, altmap, ptpfn);
 	if (!pte)
 		return NULL;
 	vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
@@ -383,15 +375,14 @@ static pte_t * __meminit vmemmap_populate_address(unsigned long addr, int node,
 static int __meminit vmemmap_populate_range(unsigned long start,
 					    unsigned long end, int node,
 					    struct vmem_altmap *altmap,
-					    unsigned long ptpfn,
-					    unsigned long flags)
+					    unsigned long ptpfn)
 {
 	unsigned long addr = start;
 	pte_t *pte;
 
 	for (; addr < end; addr += PAGE_SIZE) {
 		pte = vmemmap_populate_address(addr, node, altmap,
-					       ptpfn, flags);
+					       ptpfn);
 		if (!pte)
 			return -ENOMEM;
 	}
@@ -402,7 +393,7 @@ static int __meminit vmemmap_populate_range(unsigned long start,
 int __meminit vmemmap_populate_basepages(unsigned long start, unsigned long end,
 					 int node, struct vmem_altmap *altmap)
 {
-	return vmemmap_populate_range(start, end, node, altmap, -1, 0);
+	return vmemmap_populate_range(start, end, node, altmap, -1);
 }
 
 /*
@@ -531,7 +522,6 @@ static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
 	unsigned long size, addr;
 	pte_t *pte;
 	int rc;
-	unsigned long flags = VMEMMAP_POPULATE_DAX;
 	struct page *page;
 	unsigned int order = pfn_to_section_compound_order(start_pfn);
 
@@ -541,14 +531,14 @@ static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
 
 	if (reuse_compound_section(start_pfn, pgmap))
 		return vmemmap_populate_range(start, end, node, NULL,
-					      page_to_pfn(page), flags);
+					      page_to_pfn(page));
 
 	size = min(end - start, (1UL << order) * sizeof(struct page));
 	for (addr = start; addr < end; addr += size) {
 		unsigned long next, last = addr + size;
 
 		/* Populate the head page vmemmap page */
-		pte = vmemmap_populate_address(addr, node, NULL, -1, flags);
+		pte = vmemmap_populate_address(addr, node, NULL, -1);
 		if (!pte)
 			return -ENOMEM;
 
@@ -558,7 +548,7 @@ static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
 		 */
 		next = addr + PAGE_SIZE;
 		rc = vmemmap_populate_range(next, last, node, NULL,
-					    page_to_pfn(page), flags);
+					    page_to_pfn(page));
 		if (rc)
 			return -ENOMEM;
 	}
Signature · 1 line
2.54.0
Reviewed-by on v3 1/6
#2 lore raw

Re: [PATCH v3 1/6] mm/sparse-vmemmap: drop VMEMMAP_POPULATE_DAX

Quoted 17 lines · Muchun Song

VMEMMAP_POPULATE_DAX currently distinguishes DAX vmemmap population in two places: it keeps allocations on the normal path and takes a reference when a backing page is supplied for reuse.

After Device DAX switched to the common per-zone shared tail page, both conditions can be determined locally. DAX supplies ptpfn for every shared tail mapping and requests an allocation only for compound head mappings, whose PFNs are not optimizable. Therefore, vmemmap_optimizable_pfn() alone selects the correct allocation path.

When ptpfn is supplied, the caller is reusing an existing backing page. Once the slab allocator is available, take a reference for each reused mapping to balance the release performed by vmemmap_free(). Although the buddy allocator is available before slab, no vmemmap population occurs in that interval. Earlier mappings are backed by memblock/reserved memory and do not need page reference accounting.

Quoted 6 lines

Remove VMEMMAP_POPULATE_DAX and the flags argument from the vmemmap population helpers.

---

LGTM! Feel free to add:

#3 lore raw

PATCH v3 2/6 mm/sparse-vmemmap: support device DAX in common vmemmap path

In reply to Muchun Song

The common vmemmap population path cannot yet handle optimized Device DAX mappings on its own. It uses pfn_to_zone() to find the shared tail page, but Device DAX populates its vmemmap at runtime before the ZONE_DEVICE span is initialized.

Teach the common path to use device_zone() for runtime optimized vmemmap population while retaining pfn_to_zone() for early boot. This allows the same path to support both early boot mappings and Device DAX.

The backing PFN supplied by the Device DAX-specific population path is no longer used, allowing the redundant lookup and population code to be removed later.

---
v3:
- Collect Acked-by from Qi Zheng

v2:
- Expand comments around slab initialization to explain zone lookup and page refcounting (suggested by Qi Zheng)

---
 mm/sparse-vmemmap.c | 64 +++++++++++++++++++++++++--------------------
 1 file changed, 35 insertions(+), 29 deletions(-)
mm/sparse-vmemmap.c +35 -29
index 8219abc6c3e5..ee4c113ca938 100644
@@ -237,18 +237,43 @@ static __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node,
 	struct page *page;
 	const unsigned int order = pfn_to_section_compound_order(pfn);
 
-	/*
-	 * Device DAX still relies on vmemmap_populate_compound_pages() for
-	 * head/first-tail allocation and tail-page reuse.
-	 */
 	if (!vmemmap_optimizable_pfn(pfn))
 		return vmemmap_alloc_block_buf(PAGE_SIZE, node, altmap);
 
-	zone = pfn_to_zone(pfn, node);
+	/*
+	 * Before slab is available, vmemmap optimization is used for early
+	 * system RAM, whose zone can be determined from the PFN.
+	 *
+	 * Once slab is available, only ZONE_DEVICE memory reaches this
+	 * optimized population path. Its zone span has not been initialized
+	 * while its vmemmap is being populated, so pfn_to_zone() cannot be
+	 * used. Obtain ZONE_DEVICE directly from the node instead.
+	 */
+	zone = slab_is_available() ? device_zone(node) : pfn_to_zone(pfn, node);
 	page = vmemmap_shared_tail_page(order, zone);
 	if (!page)
 		return NULL;
 
+	/*
+	 * During early vmemmap population, the shared tail vmemmap backing
+	 * page is allocated from memblock before its struct page can safely
+	 * participate in page refcounting. Therefore, no reference can be
+	 * held for each shared PTE mapping, and the mappings must be unshared
+	 * before the vmemmap is depopulated.
+	 *
+	 * Once slab is available, the shared backing page is allocated from
+	 * the buddy allocator and can be refcounted. Hold one reference for
+	 * each shared PTE mapping. The architecture vmemmap teardown drops
+	 * the reference through __free_pages() when removing the mapping,
+	 * preventing the backing page from being freed while it is shared.
+	 *
+	 * The backing page may be shared by enough PTE mappings to exhaust
+	 * the positive range of its reference count. Stop populating the
+	 * vmemmap if another reference cannot be acquired.
+	 */
+	if (slab_is_available() && !try_get_page(page))
+		return NULL;
+
 	return page_address(page);
 }
 
@@ -260,31 +285,12 @@ static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, in
 
 	if (pte_none(ptep_get(pte))) {
 		pte_t entry;
+		void *p = vmemmap_alloc_pte(pfn, node, altmap);
 
-		if (ptpfn == (unsigned long)-1) {
-			void *p = vmemmap_alloc_pte(pfn, node, altmap);
-
-			if (!p)
-				return NULL;
-			ptpfn = PHYS_PFN(__pa(p));
-		} else {
-			/*
-			 * When a PTE/PMD entry is freed from the init_mm
-			 * there's a free_pages() call to this page allocated
-			 * above. Thus this try_get_page() is paired with the
-			 * put_page_testzero() on the freeing path.
-			 * This can only called by certain ZONE_DEVICE path,
-			 * and through vmemmap_populate_compound_pages() when
-			 * slab is available.
-			 *
-			 * Use try_get_page() to prevent the shared page refcount
-			 * from overflowing.
-			 */
-			if (slab_is_available() &&
-			    !try_get_page(pfn_to_page(ptpfn)))
-				return NULL;
-		}
-		entry = pfn_pte(ptpfn, PAGE_KERNEL);
+		if (!p)
+			return NULL;
+
+		entry = pfn_pte(PHYS_PFN(__pa(p)), PAGE_KERNEL);
 		set_pte_at(&init_mm, addr, pte, entry);
 	} else if (WARN_ON_ONCE(vmemmap_optimizable_pfn(pfn)))
 		return NULL;
Signature · 1 line
2.54.0
#4 lore raw

Re: [PATCH v3 2/6] mm/sparse-vmemmap: support device DAX in common vmemmap path

Quoted 65 lines · Muchun Song

The common vmemmap population path cannot yet handle optimized Device DAX mappings on its own. It uses pfn_to_zone() to find the shared tail page, but Device DAX populates its vmemmap at runtime before the ZONE_DEVICE span is initialized.

Teach the common path to use device_zone() for runtime optimized vmemmap population while retaining pfn_to_zone() for early boot. This allows the same path to support both early boot mappings and Device DAX.

The backing PFN supplied by the Device DAX-specific population path is no longer used, allowing the redundant lookup and population code to be removed later.

---
v3:
- Collect Acked-by from Qi Zheng

v2:
- Expand comments around slab initialization to explain zone lookup and page refcounting (suggested by Qi Zheng)
---
mm/sparse-vmemmap.c | 64 +++++++++++++++++++++++++--------------------

1 file changed, 35 insertions(+), 29 deletions(-)
mm/sparse-vmemmap.c +19 -5
index 8219abc6c3e5..ee4c113ca938 100644
@@ -237,18 +237,43 @@ static __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node,
 	struct page *page;
 	const unsigned int order = pfn_to_section_compound_order(pfn);
 
-	/*
-	 * Device DAX still relies on vmemmap_populate_compound_pages() for
-	 * head/first-tail allocation and tail-page reuse.
-	 */
 	if (!vmemmap_optimizable_pfn(pfn))
 		return vmemmap_alloc_block_buf(PAGE_SIZE, node, altmap);
 
-	zone = pfn_to_zone(pfn, node);
+	/*
+	 * Before slab is available, vmemmap optimization is used for early
+	 * system RAM, whose zone can be determined from the PFN.
+	 *
+	 * Once slab is available, only ZONE_DEVICE memory reaches this
+	 * optimized population path. Its zone span has not been initialized
+	 * while its vmemmap is being populated, so pfn_to_zone() cannot be
+	 * used. Obtain ZONE_DEVICE directly from the node instead.
+	 */
+	zone = slab_is_available() ? device_zone(node) : pfn_to_zone(pfn, node);
 	page = vmemmap_shared_tail_page(order, zone);
 	if (!page)
 		return NULL;
 
+	/*
+	 * During early vmemmap population, the shared tail vmemmap backing
+	 * page is allocated from memblock before its struct page can safely
+	 * participate in page refcounting. Therefore, no reference can be
+	 * held for each shared PTE mapping, and the mappings must be unshared
+	 * before the vmemmap is depopulated.
+	 *
+	 * Once slab is available, the shared backing page is allocated from
+	 * the buddy allocator and can be refcounted. Hold one reference for

mm/sparse-vmemmap.c static __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node, · line 275

Quoted 10 lines
mm/sparse-vmemmap.c +10 -0
+	 * each shared PTE mapping. The architecture vmemmap teardown drops
+	 * the reference through __free_pages() when removing the mapping,
+	 * preventing the backing page from being freed while it is shared.
+	 *
+	 * The backing page may be shared by enough PTE mappings to exhaust
+	 * the positive range of its reference count. Stop populating the
+	 * vmemmap if another reference cannot be acquired.
+	 */
+	if (slab_is_available() && !try_get_page(page))
+		return NULL;

BTW, shouldn't __add_pages() undo the earlier sections on a population failure? Say the first section is added successfully, but populating the next one fails, e.g. due to an allocation failure:

void *memremap_pages(struct dev_pagemap *pgmap, int nid) {
...

	const int nr_range = pgmap->nr_range;
	int error, i;
...
	pgmap->nr_range = 0;
	error = 0;
	for (i = 0; i < nr_range; i++) {
		error = pagemap_range(pgmap, &params, i, nid);
		if (error)
			break;
		pgmap->nr_range++;
	}

	if (i < nr_range) {
		memunmap_pages(pgmap);
		pgmap->nr_range = nr_range;
		return ERR_PTR(error);
	}

...
}

We still need to undo the sections already added in the failed range, though ... memunmap_pages() won't touch those, since it only removes completed ranges.

If the range starts at a section boundary, we'd hit -EEXIST in fill_subsection_map() on retry while those subsection bits are still set.

The old DAX path had this issue too. Could we roll back [start_pfn, pfn) in __add_pages() as a separate fix? Something like this:

---8<---

mm/memory_hotplug.c +5 -0
index 796af1028ee2..16a0a2c885bc 100644
@@ -380,6 +380,7 @@ EXPORT_SYMBOL_GPL(pfn_to_online_page);
 int __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
 		struct mhp_params *params)
 {
+	const unsigned long start_pfn = pfn;
 	const unsigned long end_pfn = pfn + nr_pages;
 	unsigned long cur_nr_pages;
 	int err;
@@ -417,6 +418,10 @@ int __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
 			break;
 		cond_resched();
 	}
+
+	/* Roll back the sections added before the failure. */
+	if (err && pfn != start_pfn)
+		__remove_pages(start_pfn, pfn - start_pfn, altmap, params->pgmap);
 	vmemmap_populate_print_last();
 	return err;
 }

--

Hope I haven't missed anything :)

Cheers, Lance

Quoted 45 lines
mm/sparse-vmemmap.c +6 -24
+
 	return page_address(page);
 }
 
@@ -260,31 +285,12 @@ static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, in
 
 	if (pte_none(ptep_get(pte))) {
 		pte_t entry;
+		void *p = vmemmap_alloc_pte(pfn, node, altmap);
 
-		if (ptpfn == (unsigned long)-1) {
-			void *p = vmemmap_alloc_pte(pfn, node, altmap);
-
-			if (!p)
-				return NULL;
-			ptpfn = PHYS_PFN(__pa(p));
-		} else {
-			/*
-			 * When a PTE/PMD entry is freed from the init_mm
-			 * there's a free_pages() call to this page allocated
-			 * above. Thus this try_get_page() is paired with the
-			 * put_page_testzero() on the freeing path.
-			 * This can only called by certain ZONE_DEVICE path,
-			 * and through vmemmap_populate_compound_pages() when
-			 * slab is available.
-			 *
-			 * Use try_get_page() to prevent the shared page refcount
-			 * from overflowing.
-			 */
-			if (slab_is_available() &&
-			    !try_get_page(pfn_to_page(ptpfn)))
-				return NULL;
-		}
-		entry = pfn_pte(ptpfn, PAGE_KERNEL);
+		if (!p)
+			return NULL;
+
+		entry = pfn_pte(PHYS_PFN(__pa(p)), PAGE_KERNEL);
 		set_pte_at(&init_mm, addr, pte, entry);
 	} else if (WARN_ON_ONCE(vmemmap_optimizable_pfn(pfn)))
 		return NULL;
Signature · 1 line
2.54.0
#5 lore raw

Re: [PATCH v3 2/6] mm/sparse-vmemmap: support device DAX in common vmemmap path

Quoted 145 lines · Lance Yang
Quoted 65 lines · Muchun Song

The common vmemmap population path cannot yet handle optimized Device DAX mappings on its own. It uses pfn_to_zone() to find the shared tail page, but Device DAX populates its vmemmap at runtime before the ZONE_DEVICE span is initialized.

Teach the common path to use device_zone() for runtime optimized vmemmap population while retaining pfn_to_zone() for early boot. This allows the same path to support both early boot mappings and Device DAX.

The backing PFN supplied by the Device DAX-specific population path is no longer used, allowing the redundant lookup and population code to be removed later.

---
v3:
- Collect Acked-by from Qi Zheng

v2:
- Expand comments around slab initialization to explain zone lookup and page refcounting (suggested by Qi Zheng)
---
mm/sparse-vmemmap.c | 64 +++++++++++++++++++++++++--------------------

1 file changed, 35 insertions(+), 29 deletions(-)
mm/sparse-vmemmap.c +19 -5
index 8219abc6c3e5..ee4c113ca938 100644
@@ -237,18 +237,43 @@ static __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node,
 struct page *page;
 const unsigned int order = pfn_to_section_compound_order(pfn);
 
- /*
-  * Device DAX still relies on vmemmap_populate_compound_pages() for
-  * head/first-tail allocation and tail-page reuse.
-  */
 if (!vmemmap_optimizable_pfn(pfn))
 return vmemmap_alloc_block_buf(PAGE_SIZE, node, altmap);
 
- zone = pfn_to_zone(pfn, node);
+ /*
+  * Before slab is available, vmemmap optimization is used for early
+  * system RAM, whose zone can be determined from the PFN.
+  *
+  * Once slab is available, only ZONE_DEVICE memory reaches this
+  * optimized population path. Its zone span has not been initialized
+  * while its vmemmap is being populated, so pfn_to_zone() cannot be
+  * used. Obtain ZONE_DEVICE directly from the node instead.
+  */
+ zone = slab_is_available() ? device_zone(node) : pfn_to_zone(pfn, node);
 page = vmemmap_shared_tail_page(order, zone);
 if (!page)
 return NULL;
 
+ /*
+  * During early vmemmap population, the shared tail vmemmap backing
+  * page is allocated from memblock before its struct page can safely
+  * participate in page refcounting. Therefore, no reference can be
+  * held for each shared PTE mapping, and the mappings must be unshared
+  * before the vmemmap is depopulated.
+  *
+  * Once slab is available, the shared backing page is allocated from
+  * the buddy allocator and can be refcounted. Hold one reference for

mm/sparse-vmemmap.c static __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node, · line 275

Quoted 10 lines
mm/sparse-vmemmap.c +10 -0
+  * each shared PTE mapping. The architecture vmemmap teardown drops
+  * the reference through __free_pages() when removing the mapping,
+  * preventing the backing page from being freed while it is shared.
+  *
+  * The backing page may be shared by enough PTE mappings to exhaust
+  * the positive range of its reference count. Stop populating the
+  * vmemmap if another reference cannot be acquired.
+  */
+ if (slab_is_available() && !try_get_page(page))
+ return NULL;

BTW, shouldn't __add_pages() undo the earlier sections on a population failure? Say the first section is added successfully, but populating the next one fails, e.g. due to an allocation failure:

void *memremap_pages(struct dev_pagemap *pgmap, int nid)

{
	...
	const int nr_range = pgmap->nr_range;
	int error, i;
	...
	pgmap->nr_range = 0;
	error = 0;
	for (i = 0; i < nr_range; i++) {
		error = pagemap_range(pgmap, &params, i, nid);
		if (error)
			break;
		pgmap->nr_range++;
	}

	if (i < nr_range) {
		memunmap_pages(pgmap);
		pgmap->nr_range = nr_range;
		return ERR_PTR(error);
	}

...
}

We still need to undo the sections already added in the failed range, though ... memunmap_pages() won't touch those, since it only removes completed ranges.

If the range starts at a section boundary, we'd hit -EEXIST in fill_subsection_map() on retry while those subsection bits are still set.

The old DAX path had this issue too. Could we roll back [start_pfn, pfn) in __add_pages() as a separate fix? Something like this:

---8<---

mm/memory_hotplug.c +5 -1
index 796af1028ee2..16a0a2c885bc 100644
@@ -380,6 +380,7 @@ EXPORT_SYMBOL_GPL(pfn_to_online_page);
 int __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
 struct mhp_params *params)
 {
+ 	const unsigned long start_pfn = pfn;
 	const unsigned long end_pfn = pfn + nr_pages;
 	unsigned long cur_nr_pages;
 	int err;
@@ -417,6 +418,10 @@ int __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
 			break;
 		cond_resched();
 }
+
+ 	/* Roll back the sections added before the failure. */
+ 	if (err && pfn != start_pfn)
+ 		__remove_pages(start_pfn, pfn - start_pfn, altmap, params->pgmap);
 	vmemmap_populate_print_last();
 	return err;
 }
--
Quoted 1 line

Hope I haven't missed anything :)

Good catch. This is indeed a pre-existing issue, and the old DAX path was affected as well.

Your proposed fix looks correct to me. I would slightly prefer keeping the rollback close to the failure:

            err = sparse_add_section(nid, pfn, cur_nr_pages, altmap,
                                     params->pgmap);
            if (err) {
                    __remove_pages(start_pfn, pfn - start_pfn, altmap,
                                   params->pgmap);
                    break;
            }

If the first section fails, this simply calls __remove_pages() with an empty range, which is a harmless no-op.

Would you mind sending this as a separate bug fix? I will ACK it.

Thanks,
Muchun

#6 lore raw

Re: [PATCH v3 2/6] mm/sparse-vmemmap: support device DAX in common vmemmap path

In reply to Lance Yang
Quoted 145 lines · Lance Yang
Quoted 65 lines · Muchun Song

The common vmemmap population path cannot yet handle optimized Device DAX mappings on its own. It uses pfn_to_zone() to find the shared tail page, but Device DAX populates its vmemmap at runtime before the ZONE_DEVICE span is initialized.

Teach the common path to use device_zone() for runtime optimized vmemmap population while retaining pfn_to_zone() for early boot. This allows the same path to support both early boot mappings and Device DAX.

The backing PFN supplied by the Device DAX-specific population path is no longer used, allowing the redundant lookup and population code to be removed later.

---
v3:
- Collect Acked-by from Qi Zheng

v2:
- Expand comments around slab initialization to explain zone lookup and page refcounting (suggested by Qi Zheng)
---
mm/sparse-vmemmap.c | 64 +++++++++++++++++++++++++--------------------

1 file changed, 35 insertions(+), 29 deletions(-)
mm/sparse-vmemmap.c +19 -5
index 8219abc6c3e5..ee4c113ca938 100644
@@ -237,18 +237,43 @@ static __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node,
 struct page *page;
 const unsigned int order = pfn_to_section_compound_order(pfn);
 
- /*
-  * Device DAX still relies on vmemmap_populate_compound_pages() for
-  * head/first-tail allocation and tail-page reuse.
-  */
 if (!vmemmap_optimizable_pfn(pfn))
 return vmemmap_alloc_block_buf(PAGE_SIZE, node, altmap);
 
- zone = pfn_to_zone(pfn, node);
+ /*
+  * Before slab is available, vmemmap optimization is used for early
+  * system RAM, whose zone can be determined from the PFN.
+  *
+  * Once slab is available, only ZONE_DEVICE memory reaches this
+  * optimized population path. Its zone span has not been initialized
+  * while its vmemmap is being populated, so pfn_to_zone() cannot be
+  * used. Obtain ZONE_DEVICE directly from the node instead.
+  */
+ zone = slab_is_available() ? device_zone(node) : pfn_to_zone(pfn, node);
 page = vmemmap_shared_tail_page(order, zone);
 if (!page)
 return NULL;
 
+ /*
+  * During early vmemmap population, the shared tail vmemmap backing
+  * page is allocated from memblock before its struct page can safely
+  * participate in page refcounting. Therefore, no reference can be
+  * held for each shared PTE mapping, and the mappings must be unshared
+  * before the vmemmap is depopulated.
+  *
+  * Once slab is available, the shared backing page is allocated from
+  * the buddy allocator and can be refcounted. Hold one reference for

mm/sparse-vmemmap.c static __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node, · line 275

Quoted 10 lines
mm/sparse-vmemmap.c +10 -0
+  * each shared PTE mapping. The architecture vmemmap teardown drops
+  * the reference through __free_pages() when removing the mapping,
+  * preventing the backing page from being freed while it is shared.
+  *
+  * The backing page may be shared by enough PTE mappings to exhaust
+  * the positive range of its reference count. Stop populating the
+  * vmemmap if another reference cannot be acquired.
+  */
+ if (slab_is_available() && !try_get_page(page))
+ return NULL;

BTW, shouldn't __add_pages() undo the earlier sections on a population failure? Say the first section is added successfully, but populating the next one fails, e.g. due to an allocation failure:

void *memremap_pages(struct dev_pagemap *pgmap, int nid)

{
	...
	const int nr_range = pgmap->nr_range;
	int error, i;
	...
	pgmap->nr_range = 0;
	error = 0;
	for (i = 0; i < nr_range; i++) {
		error = pagemap_range(pgmap, &params, i, nid);
		if (error)
			break;
		pgmap->nr_range++;
	}

	if (i < nr_range) {
		memunmap_pages(pgmap);
		pgmap->nr_range = nr_range;
		return ERR_PTR(error);
	}

...
}

We still need to undo the sections already added in the failed range, though ... memunmap_pages() won't touch those, since it only removes completed ranges.

If the range starts at a section boundary, we'd hit -EEXIST in fill_subsection_map() on retry while those subsection bits are still set.

The old DAX path had this issue too. Could we roll back [start_pfn, pfn) in __add_pages() as a separate fix? Something like this:

---8<---

mm/memory_hotplug.c +5 -1
index 796af1028ee2..16a0a2c885bc 100644
@@ -380,6 +380,7 @@ EXPORT_SYMBOL_GPL(pfn_to_online_page);
 int __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
 struct mhp_params *params)
 {
+ 	const unsigned long start_pfn = pfn;
 	const unsigned long end_pfn = pfn + nr_pages;
 	unsigned long cur_nr_pages;
 	int err;
@@ -417,6 +418,10 @@ int __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
 			break;
 		cond_resched();
 }
+
+ 	/* Roll back the sections added before the failure. */
+ 	if (err && pfn != start_pfn)
+ 		__remove_pages(start_pfn, pfn - start_pfn, altmap, params->pgmap);
 	vmemmap_populate_print_last();
 	return err;
 }
--
Quoted 1 line

Hope I haven't missed anything :)

Good catch. This is indeed a pre-existing issue, and the old DAX path was affected as well.

Your proposed fix looks correct to me. I would slightly prefer keeping the rollback close to the failure:

           err = sparse_add_section(nid, pfn, cur_nr_pages, altmap,
                                    params->pgmap);
           if (err) {
                   __remove_pages(start_pfn, pfn - start_pfn, altmap,
                                  params->pgmap);
                   break;
           }

If the first section fails, this simply calls __remove_pages() with an empty range, which is a harmless no-op.

Would you mind sending this as a separate bug fix? I will ACK it.

Thanks,
Muchun

#7 lore raw

Re: [PATCH v3 2/6] mm/sparse-vmemmap: support device DAX in common vmemmap path

On 2026/9/30 19:11, Muchun Song wrote:
[...]

Quoted 4 lines

Your proposed fix looks correct to me. I would slightly prefer keeping the rollback close to the failure:

Quoted 8 lines
            err = sparse_add_section(nid, pfn, cur_nr_pages, altmap,
                                     params->pgmap);
            if (err) {
                    __remove_pages(start_pfn, pfn - start_pfn, altmap,
                                   params->pgmap);
                    break;
            }

Cool. Will shamelessly steal this approach :D

Quoted 4 lines

If the first section fails, this simply calls __remove_pages() with an empty range, which is a harmless no-op.

Would you mind sending this as a separate bug fix? I will ACK it.

Certainly! Lance

Reviewed-by on v3 2/6
#8 lore raw

Re: [PATCH v3 2/6] mm/sparse-vmemmap: support device DAX in common vmemmap path

In reply to Muchun Song
Quoted 9 lines · Muchun Song

The common vmemmap population path cannot yet handle optimized Device DAX mappings on its own. It uses pfn_to_zone() to find the shared tail page, but Device DAX populates its vmemmap at runtime before the ZONE_DEVICE span is initialized.

Teach the common path to use device_zone() for runtime optimized vmemmap population while retaining pfn_to_zone() for early boot. This allows the same path to support both early boot mappings and Device DAX.

Quoted 7 lines

The backing PFN supplied by the Device DAX-specific population path is no longer used, allowing the redundant lookup and population code to be removed later.

---

Nothing jumped out at me, feel free to add:

#9 lore raw

PATCH v3 3/6 mm/sparse-vmemmap: drop Device DAX-specific population path

In reply to Muchun Song

The common vmemmap path selects the shared page for optimized mappings itself, so Device DAX no longer needs vmemmap_populate_compound_pages() to find a shared tail page and pass its backing PFN through the generic population helpers.

Remove the Device DAX-specific population path and let section memmap population always use vmemmap_populate(). The powerpc retains an architecture-specific compound-page implementation, so select it directly from radix__vmemmap_populate() for optimizable sections.

---
v3:
- Use the public order-based optimization predicate in the powerpc path

v2:
- Collect Acked-by from Qi Zheng

---
 arch/powerpc/mm/book3s64/radix_pgtable.c |  3 +
 mm/mm_init.c                             |  2 +-
 mm/sparse-vmemmap.c                      | 71 +-----------------------
 3 files changed, 5 insertions(+), 71 deletions(-)
arch/powerpc/mm/book3s64/radix_pgtable.c +3 -0
index 9ca28e4a610a..fd13fa91e5c0 100644
@@ -1122,7 +1122,10 @@ int __meminit radix__vmemmap_populate(unsigned long start, unsigned long end, in
 	pud_t *pud;
 	pmd_t *pmd;
 	pte_t *pte;
+	unsigned long pfn = page_to_pfn((struct page *)start);
 
+	if (vmemmap_optimizable_order(pfn_to_section_compound_order(pfn)))
+		return vmemmap_populate_compound_pages(pfn, start, end, node, NULL);
 	/*
 	 * If altmap is present, Make sure we align the start vmemmap addr
 	 * to PAGE_SIZE so that we calculate the correct start_pfn in
mm/mm_init.c +1 -1
index 56bb4567a494..1650d6bc1211 100644
@@ -1046,7 +1046,7 @@ static void zone_device_page_init_from_template(struct page *page,
  * initialize is a lot smaller that the total amount of struct pages being
  * mapped. This is a paired / mild layering violation with explicit knowledge
  * of how the sparse_vmemmap internals handle compound pages in the lack
- * of an altmap. See vmemmap_populate_compound_pages().
+ * of an altmap.
  */
 static inline unsigned long compound_nr_pages(unsigned long pfn,
 					      struct dev_pagemap *pgmap)
mm/sparse-vmemmap.c +1 -70

83 lines not on this page · raw message

Signature · 1 line
2.54.0
Reviewed-by on v3 3/6
#10 lore raw

Re: [PATCH v3 3/6] mm/sparse-vmemmap: drop Device DAX-specific population path

Quoted 5 lines · Muchun Song

The common vmemmap path selects the shared page for optimized mappings itself, so Device DAX no longer needs vmemmap_populate_compound_pages() to find a shared tail page and pass its backing PFN through the generic population helpers.

Quoted 8 lines

Remove the Device DAX-specific population path and let section memmap population always use vmemmap_populate(). The powerpc retains an architecture-specific compound-page implementation, so select it directly from radix__vmemmap_populate() for optimizable sections.

---

LGTM! Feel free to add:

#11 lore raw

PATCH v3 4/6 mm/sparse-vmemmap: remove the unused ptpfn argument

In reply to Muchun Song

vmemmap_pte_populate() no longer uses ptpfn as an input. Drop the argument to simplify the code.

---
v2:
- Collect Acked-by from Qi Zheng

---
 mm/sparse-vmemmap.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
mm/sparse-vmemmap.c +6 -9
index dee95e520376..5fd2df2b516f 100644
@@ -278,7 +278,7 @@ static __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node,
 }
 
 static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node,
-		struct vmem_altmap *altmap, unsigned long ptpfn)
+		struct vmem_altmap *altmap)
 {
 	pte_t *pte = pte_offset_kernel(pmd, addr);
 	unsigned long pfn = page_to_pfn((struct page *)addr);
@@ -349,8 +349,7 @@ static pgd_t * __meminit vmemmap_pgd_populate(unsigned long addr, int node)
 }
 
 static pte_t * __meminit vmemmap_populate_address(unsigned long addr, int node,
-					      struct vmem_altmap *altmap,
-					      unsigned long ptpfn)
+						  struct vmem_altmap *altmap)
 {
 	pgd_t *pgd;
 	p4d_t *p4d;
@@ -370,7 +369,7 @@ static pte_t * __meminit vmemmap_populate_address(unsigned long addr, int node,
 	pmd = vmemmap_pmd_populate(pud, addr, node);
 	if (!pmd)
 		return NULL;
-	pte = vmemmap_pte_populate(pmd, addr, node, altmap, ptpfn);
+	pte = vmemmap_pte_populate(pmd, addr, node, altmap);
 	if (!pte)
 		return NULL;
 	vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
@@ -380,15 +379,13 @@ static pte_t * __meminit vmemmap_populate_address(unsigned long addr, int node,
 
 static int __meminit vmemmap_populate_range(unsigned long start,
 					    unsigned long end, int node,
-					    struct vmem_altmap *altmap,
-					    unsigned long ptpfn)
+					    struct vmem_altmap *altmap)
 {
 	unsigned long addr = start;
 	pte_t *pte;
 
 	for (; addr < end; addr += PAGE_SIZE) {
-		pte = vmemmap_populate_address(addr, node, altmap,
-					       ptpfn);
+		pte = vmemmap_populate_address(addr, node, altmap);
 		if (!pte)
 			return -ENOMEM;
 	}
@@ -399,7 +396,7 @@ static int __meminit vmemmap_populate_range(unsigned long start,
 int __meminit vmemmap_populate_basepages(unsigned long start, unsigned long end,
 					 int node, struct vmem_altmap *altmap)
 {
-	return vmemmap_populate_range(start, end, node, altmap, -1);
+	return vmemmap_populate_range(start, end, node, altmap);
 }
 
 /*
Signature · 1 line
2.54.0
#13 lore raw

PATCH v3 5/6 mm/sparse-vmemmap: open-code vmemmap_populate_address()

In reply to Muchun Song

vmemmap_populate_address() no longer has any callers that need the returned PTE. Its only remaining user, vmemmap_populate_range(), only checks whether population succeeded.

Open-code vmemmap_populate_address() directly in vmemmap_populate_basepages(), remove the now-redundant range helper, and return -ENOMEM directly on failure.

---
v2:
- Collect Acked-by from Qi Zheng

---
 mm/sparse-vmemmap.c | 54 ++++++++++++++-------------------------------
 1 file changed, 17 insertions(+), 37 deletions(-)
mm/sparse-vmemmap.c +17 -37
index 5fd2df2b516f..9a58323dd628 100644
@@ -348,8 +348,8 @@ static pgd_t * __meminit vmemmap_pgd_populate(unsigned long addr, int node)
 	return pgd;
 }
 
-static pte_t * __meminit vmemmap_populate_address(unsigned long addr, int node,
-						  struct vmem_altmap *altmap)
+int __meminit vmemmap_populate_basepages(unsigned long start, unsigned long end,
+					 int node, struct vmem_altmap *altmap)
 {
 	pgd_t *pgd;
 	p4d_t *p4d;
@@ -357,48 +357,28 @@ static pte_t * __meminit vmemmap_populate_address(unsigned long addr, int node,
 	pmd_t *pmd;
 	pte_t *pte;
 
-	pgd = vmemmap_pgd_populate(addr, node);
-	if (!pgd)
-		return NULL;
-	p4d = vmemmap_p4d_populate(pgd, addr, node);
-	if (!p4d)
-		return NULL;
-	pud = vmemmap_pud_populate(p4d, addr, node);
-	if (!pud)
-		return NULL;
-	pmd = vmemmap_pmd_populate(pud, addr, node);
-	if (!pmd)
-		return NULL;
-	pte = vmemmap_pte_populate(pmd, addr, node, altmap);
-	if (!pte)
-		return NULL;
-	vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
-
-	return pte;
-}
-
-static int __meminit vmemmap_populate_range(unsigned long start,
-					    unsigned long end, int node,
-					    struct vmem_altmap *altmap)
-{
-	unsigned long addr = start;
-	pte_t *pte;
-
-	for (; addr < end; addr += PAGE_SIZE) {
-		pte = vmemmap_populate_address(addr, node, altmap);
+	for (unsigned long addr = start; addr < end; addr += PAGE_SIZE) {
+		pgd = vmemmap_pgd_populate(addr, node);
+		if (!pgd)
+			return -ENOMEM;
+		p4d = vmemmap_p4d_populate(pgd, addr, node);
+		if (!p4d)
+			return -ENOMEM;
+		pud = vmemmap_pud_populate(p4d, addr, node);
+		if (!pud)
+			return -ENOMEM;
+		pmd = vmemmap_pmd_populate(pud, addr, node);
+		if (!pmd)
+			return -ENOMEM;
+		pte = vmemmap_pte_populate(pmd, addr, node, altmap);
 		if (!pte)
 			return -ENOMEM;
+		vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
 	}
 
 	return 0;
 }
 
-int __meminit vmemmap_populate_basepages(unsigned long start, unsigned long end,
-					 int node, struct vmem_altmap *altmap)
-{
-	return vmemmap_populate_range(start, end, node, altmap);
-}
-
 /*
  * Write protect the mirrored tail page structs for HVO. This will be
  * called from the hugetlb code when gathering and initializing the
Signature · 1 line
2.54.0
Reviewed-by on v3 5/6
#14 lore raw

Re: [PATCH v3 5/6] mm/sparse-vmemmap: open-code vmemmap_populate_address()

Quoted 11 lines · Muchun Song

vmemmap_populate_address() no longer has any callers that need the returned PTE. Its only remaining user, vmemmap_populate_range(), only checks whether population succeeded.

Open-code vmemmap_populate_address() directly in vmemmap_populate_basepages(), remove the now-redundant range helper, and return -ENOMEM directly on failure.

---

Much simpler without the extra wrappers :)

#15 lore raw

PATCH v3 6/6 mm/mm_init: add zone mismatch warning during page init

In reply to Muchun Song

For vmemmap-optimized sections, tail struct pages may be backed by shared vmemmap pages. Those shared pages must carry the same page zone ID as the struct pages initialized for the section.

Warn in __init_single_page() if the shared tail page has a different page_zone_id(), which would indicate inconsistent initialization.

---
v3:
- Collect Acked-by from Qi Zheng

v2:
- New patch.

---
 mm/mm_init.c | 3 +++
 1 file changed, 3 insertions(+)
mm/mm_init.c +3 -0
index 1650d6bc1211..bd02e8d06965 100644
@@ -609,6 +609,9 @@ void __meminit __init_single_page(struct page *page, unsigned long pfn,
 	if (!is_highmem_idx(zone))
 		set_page_address(page, __va(pfn << PAGE_SHIFT));
 #endif
+	VM_WARN_ON_ONCE(vmemmap_optimizable_order(pfn_to_section_compound_order(pfn)) &&
+			page_zone_id(page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) !=
+			page_zone_id(page));
 }
 
 #ifdef CONFIG_NUMA
Signature · 1 line
2.54.0
maintainer
#16 lore raw

Re: [PATCH v3 6/6] mm/mm_init: add zone mismatch warning during page init

Hi Muchun,

Quoted 24 lines · Muchun Song

For vmemmap-optimized sections, tail struct pages may be backed by shared vmemmap pages. Those shared pages must carry the same page zone ID as the struct pages initialized for the section.

Warn in __init_single_page() if the shared tail page has a different page_zone_id(), which would indicate inconsistent initialization.

---
v3:
- Collect Acked-by from Qi Zheng

v2:
- New patch.

---
 mm/mm_init.c | 3 +++
 1 file changed, 3 insertions(+)
mm/mm_init.c +0 -0
index 1650d6bc1211..bd02e8d06965 100644

mm/mm_init.c void __meminit __init_single_page(struct page *page, unsigned long pfn, · line 614

Quoted 6 lines
mm/mm_init.c +3 -0
@@ -609,6 +609,9 @@ void __meminit __init_single_page(struct page *page, unsigned long pfn,
 	if (!is_highmem_idx(zone))
 		set_page_address(page, __va(pfn << PAGE_SHIFT));
 #endif
+	VM_WARN_ON_ONCE(vmemmap_optimizable_order(pfn_to_section_compound_order(pfn)) &&
+			page_zone_id(page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) !=
+			page_zone_id(page));

Hmm, page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES is initialized a tad later than page so it'll have stale data in the page->flags, won't it?

Quoted 6 lines
mm/mm_init.c +0 -0
 }
 
 #ifdef CONFIG_NUMA
Signature · 1 line
2.54.0
Signature · 2 lines
Sincerely yours,
Mike.
#17 lore raw

Re: [PATCH v3 6/6] mm/mm_init: add zone mismatch warning during page init

Quoted 22 lines · Mike Rapoport

Hi Muchun,

Quoted 19 lines · Muchun Song

For vmemmap-optimized sections, tail struct pages may be backed by shared vmemmap pages. Those shared pages must carry the same page zone ID as the struct pages initialized for the section.

Warn in __init_single_page() if the shared tail page has a different page_zone_id(), which would indicate inconsistent initialization.

---
v3:
- Collect Acked-by from Qi Zheng

v2:
- New patch.

---
 mm/mm_init.c | 3 +++
 1 file changed, 3 insertions(+)
Quoted 14 lines

mm/mm_init.c void __meminit __init_single_page(struct page *page, unsigned long pfn, · line 614

Quoted 11 lines
mm/mm_init.c +3 -0
index 1650d6bc1211..bd02e8d06965 100644
@@ -609,6 +609,9 @@ void __meminit __init_single_page(struct page *page, unsigned long pfn,
 	if (!is_highmem_idx(zone))
 		set_page_address(page, __va(pfn << PAGE_SHIFT));
 #endif
+	VM_WARN_ON_ONCE(vmemmap_optimizable_order(pfn_to_section_compound_order(pfn)) &&
+			page_zone_id(page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) !=
+			page_zone_id(page));

Hmm, page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES is initialized a tad later than page so it'll have stale data in the page->flags, won't it?

IIUC, the shared tail struct pages already initialized by vmemmap_shared_tail_page() during vmemmap population, no? It calls set_page_node() and set_page_zone() there :)

Cheers, Lance

#18 lore raw

Re: [PATCH v3 6/6] mm/mm_init: add zone mismatch warning during page init

In reply to Mike Rapoport
Quoted 5 lines · Mike Rapoport

Hi Muchun,

Hi,

Quoted 21 lines · Muchun Song
Quoted 19 lines

For vmemmap-optimized sections, tail struct pages may be backed by shared vmemmap pages. Those shared pages must carry the same page zone ID as the struct pages initialized for the section.

Warn in __init_single_page() if the shared tail page has a different page_zone_id(), which would indicate inconsistent initialization.

---
v3:
- Collect Acked-by from Qi Zheng

v2:
- New patch.
---
mm/mm_init.c | 3 +++

1 file changed, 3 insertions(+)
Quoted 14 lines

mm/mm_init.c void __meminit __init_single_page(struct page *page, unsigned long pfn, · line 614

Quoted 11 lines
mm/mm_init.c +3 -0
index 1650d6bc1211..bd02e8d06965 100644
@@ -609,6 +609,9 @@ void __meminit __init_single_page(struct page *page, unsigned long pfn,
 if (!is_highmem_idx(zone))
 	set_page_address(page, __va(pfn << PAGE_SHIFT));
 #endif
+ 	VM_WARN_ON_ONCE(vmemmap_optimizable_order(pfn_to_section_compound_order(pfn)) &&
+ 			page_zone_id(page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) !=
+ 			page_zone_id(page));

Hmm, page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES is initialized a tad later than page so it'll have stale data in the page->flags, won't it?

Lance is right. The shared tail struct pages are already initialized by vmemmap_shared_tail_page() during vmemmap population, so they're not stale. The head 64 struct pages are initialized later — right here, after vmemmap population.

Thanks,
Muchun

Quoted 11 lines
Quoted 7 lines
mm/mm_init.c +0 -0
 }
 
 #ifdef CONFIG_NUMA
Signature · 1 line
2.54.0
Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#19 lore raw

Re: [PATCH v3 6/6] mm/mm_init: add zone mismatch warning during page init

Quoted 44 lines · Muchun Song
Quoted 5 lines · Mike Rapoport

Hi Muchun,

Hi,

Quoted 21 lines · Muchun Song
Quoted 19 lines

For vmemmap-optimized sections, tail struct pages may be backed by shared vmemmap pages. Those shared pages must carry the same page zone ID as the struct pages initialized for the section.

Warn in __init_single_page() if the shared tail page has a different page_zone_id(), which would indicate inconsistent initialization.

---
v3:
- Collect Acked-by from Qi Zheng

v2:
- New patch.
---
mm/mm_init.c | 3 +++

1 file changed, 3 insertions(+)
Quoted 14 lines

mm/mm_init.c void __meminit __init_single_page(struct page *page, unsigned long pfn, · line 614

Quoted 11 lines
mm/mm_init.c +3 -0
index 1650d6bc1211..bd02e8d06965 100644
@@ -609,6 +609,9 @@ void __meminit __init_single_page(struct page *page, unsigned long pfn,
 if (!is_highmem_idx(zone))
 	set_page_address(page, __va(pfn << PAGE_SHIFT));
 #endif
+ 	VM_WARN_ON_ONCE(vmemmap_optimizable_order(pfn_to_section_compound_order(pfn)) &&
+ 			page_zone_id(page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) !=
+ 			page_zone_id(page));

Hmm, page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES is initialized a tad later than page so it'll have stale data in the page->flags, won't it?

Quoted 4 lines

Lance is right. The shared tail struct pages are already initialized by vmemmap_shared_tail_page() during vmemmap population, so they're not stale. The head 64 struct pages are initialized later — right here, after vmemmap population.

Still it looks out of place here, can this check be done in sparse-vmemmap somehow?

Quoted 16 lines

Thanks,
Muchun

Quoted 11 lines
Quoted 7 lines
mm/mm_init.c +0 -0
 }
 
 #ifdef CONFIG_NUMA
Signature · 1 line
2.54.0
Signature · 2 lines
Sincerely yours,
Mike.
Signature · 2 lines
Sincerely yours,
Mike.
#20 lore raw

Re: [PATCH v3 6/6] mm/mm_init: add zone mismatch warning during page init

Quoted 49 lines · Mike Rapoport
Quoted 44 lines · Muchun Song
Quoted 5 lines · Mike Rapoport

Hi Muchun,

Hi,

Quoted 35 lines · Muchun Song
Quoted 24 lines

For vmemmap-optimized sections, tail struct pages may be backed by shared vmemmap pages. Those shared pages must carry the same page zone ID as the struct pages initialized for the section.

Warn in __init_single_page() if the shared tail page has a different page_zone_id(), which would indicate inconsistent initialization.

---
v3:
- Collect Acked-by from Qi Zheng

v2:
- New patch.
---
mm/mm_init.c | 3 +++

1 file changed, 3 insertions(+)
mm/mm_init.c +0 -0
index 1650d6bc1211..bd02e8d06965 100644

mm/mm_init.c void __meminit __init_single_page(struct page *page, unsigned long pfn, · line 614

Quoted 6 lines
mm/mm_init.c +3 -0
@@ -609,6 +609,9 @@ void __meminit __init_single_page(struct page *page, unsigned long pfn,
 if (!is_highmem_idx(zone))
 set_page_address(page, __va(pfn << PAGE_SHIFT));
 #endif
+ VM_WARN_ON_ONCE(vmemmap_optimizable_order(pfn_to_section_compound_order(pfn)) &&
+ page_zone_id(page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) !=
+ page_zone_id(page));

Hmm, page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES is initialized a tad later than page so it'll have stale data in the page->flags, won't it?

Quoted 7 lines
Quoted 4 lines

Lance is right. The shared tail struct pages are already initialized by vmemmap_shared_tail_page() during vmemmap population, so they're not stale. The head 64 struct pages are initialized later — right here, after vmemmap population.

Still it looks out of place here, can this check be done in sparse-vmemmap somehow?

The struct page entries of a vmemmap-optimizable compound page are currently initialized in two stages. During vmemmap population, the shared tail entries are initialized first. The retained head area—normally 64—is initialized later through __init_single_page().

This warning connects the two stages: while initializing the retained entries in the second stage, it verifies that their zone information is consistent with the shared entries initialized in the first stage. Therefore, the same check cannot be performed during vmemmap population.

I am planning to first unify the HugeTLB and Device DAX compound-page initialization through a common helper [1]. Once that work is complete, maybe it will be easy to move the initialization of the retained head area into vmemmap population. With both the retained and shared entries initialized in the same stage, there will be no cross-stage inconsistency to check, and this warning can be removed.

Would keeping the check here for now and removing it as part of that follow-up sound reasonable to you?

[1] https://lore.kernel.org/[email protected]/

Thanks,
Muchun

Quoted 21 lines
Quoted 17 lines

Thanks,
Muchun

Quoted 11 lines
Quoted 7 lines
mm/mm_init.c +0 -0
 }
 
 #ifdef CONFIG_NUMA
Signature · 1 line
2.54.0
Signature · 2 lines
Sincerely yours,
Mike.
Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#21 lore raw

Re: [PATCH v3 6/6] mm/mm_init: add zone mismatch warning during page init

Quoted 45 lines · Muchun Song
Quoted 17 lines · Mike Rapoport
Quoted 16 lines
Quoted 15 lines

mm/mm_init.c void __meminit __init_single_page(struct page *page, unsigned long pfn, · line 614

Quoted 12 lines
mm/mm_init.c +3 -0
index 1650d6bc1211..bd02e8d06965 100644
@@ -609,6 +609,9 @@ void __meminit __init_single_page(struct page *page, unsigned long pfn,
 if (!is_highmem_idx(zone))
 set_page_address(page, __va(pfn << PAGE_SHIFT));
 #endif
+ VM_WARN_ON_ONCE(vmemmap_optimizable_order(pfn_to_section_compound_order(pfn)) &&
+ page_zone_id(page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) !=
+ page_zone_id(page));

Hmm, page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES is initialized a tad later than page so it'll have stale data in the page->flags, won't it?

Quoted 7 lines
Quoted 4 lines

Lance is right. The shared tail struct pages are already initialized by vmemmap_shared_tail_page() during vmemmap population, so they're not stale. The head 64 struct pages are initialized later — right here, after vmemmap population.

Still it looks out of place here, can this check be done in sparse-vmemmap somehow?

The struct page entries of a vmemmap-optimizable compound page are currently initialized in two stages. During vmemmap population, the shared tail entries are initialized first. The retained head area—normally 64—is initialized later through __init_single_page().

This warning connects the two stages: while initializing the retained entries in the second stage, it verifies that their zone information is consistent with the shared entries initialized in the first stage. Therefore, the same check cannot be performed during vmemmap population.

I am planning to first unify the HugeTLB and Device DAX compound-page initialization through a common helper [1]. Once that work is complete, maybe it will be easy to move the initialization of the retained head area into vmemmap population. With both the retained and shared entries initialized in the same stage, there will be no cross-stage inconsistency to check, and this warning can be removed.

Quoted 2 lines

Would keeping the check here for now and removing it as part of that follow-up sound reasonable to you?

While it feels really out of place in __init_single_page(), but having it memmap_init_range() close to the if that skips shared tail pages makes sense.

What do you say?

Quoted 4 lines

[1] https://lore.kernel.org/[email protected]/

Thanks,
Muchun

Signature · 2 lines
Sincerely yours,
Mike.
#22 lore raw

Re: [PATCH v3 6/6] mm/mm_init: add zone mismatch warning during page init

Quoted 50 lines · Mike Rapoport
Quoted 45 lines · Muchun Song
Quoted 17 lines · Mike Rapoport
Quoted 16 lines
Quoted 15 lines

mm/mm_init.c void __meminit __init_single_page(struct page *page, unsigned long pfn, · line 614

Quoted 12 lines
mm/mm_init.c +3 -0
index 1650d6bc1211..bd02e8d06965 100644
@@ -609,6 +609,9 @@ void __meminit __init_single_page(struct page *page, unsigned long pfn,
 if (!is_highmem_idx(zone))
 set_page_address(page, __va(pfn << PAGE_SHIFT));
 #endif
+ VM_WARN_ON_ONCE(vmemmap_optimizable_order(pfn_to_section_compound_order(pfn)) &&
+ page_zone_id(page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) !=
+ page_zone_id(page));

Hmm, page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES is initialized a tad later than page so it'll have stale data in the page->flags, won't it?

Quoted 7 lines
Quoted 4 lines

Lance is right. The shared tail struct pages are already initialized by vmemmap_shared_tail_page() during vmemmap population, so they're not stale. The head 64 struct pages are initialized later — right here, after vmemmap population.

Still it looks out of place here, can this check be done in sparse-vmemmap somehow?

The struct page entries of a vmemmap-optimizable compound page are currently initialized in two stages. During vmemmap population, the shared tail entries are initialized first. The retained head area—normally 64—is initialized later through __init_single_page().

This warning connects the two stages: while initializing the retained entries in the second stage, it verifies that their zone information is consistent with the shared entries initialized in the first stage. Therefore, the same check cannot be performed during vmemmap population.

I am planning to first unify the HugeTLB and Device DAX compound-page initialization through a common helper [1]. Once that work is complete, maybe it will be easy to move the initialization of the retained head area into vmemmap population. With both the retained and shared entries initialized in the same stage, there will be no cross-stage inconsistency to check, and this warning can be removed.

Quoted 8 lines
Quoted 2 lines

Would keeping the check here for now and removing it as part of that follow-up sound reasonable to you?

While it feels really out of place in __init_single_page(), but having it memmap_init_range() close to the if that skips shared tail pages makes sense.

What do you say?

Sorry for the late reply because of traveling to LPC.

Putting the check in memmap_init_range() would validate the invariant for the shared-tail range skipped there. It would not, however, cover all vmemmap-optimized initialization
paths: optimized Device DAX bypasses memmap_init_range() when there is no altmap, and deferred boot-time HugeTLB initialization may also initialize retained entries elsewhere.

If the intention is to validate only the skip in memmap_init_range(), moving it there makes sense. If we want the warning to cover both HugeTLB and Device DAX generally, it still needs to remain in __init_single_page() or be called from the individual initialization paths.

Thanks,
Muchun

Quoted 9 lines
Quoted 5 lines

[1] https://lore.kernel.org/[email protected]/

Thanks,
Muchun

Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#23 lore raw

Re: [PATCH v3 6/6] mm/mm_init: add zone mismatch warning during page init

Quoted 67 lines · Muchun Song
Quoted 48 lines · Mike Rapoport
Quoted 45 lines · Muchun Song
Quoted 17 lines · Mike Rapoport
Quoted 16 lines
Quoted 15 lines

mm/mm_init.c void __meminit __init_single_page(struct page *page, unsigned long pfn, · line 614

Quoted 12 lines
mm/mm_init.c +3 -0
index 1650d6bc1211..bd02e8d06965 100644
@@ -609,6 +609,9 @@ void __meminit __init_single_page(struct page *page, unsigned long pfn,
 if (!is_highmem_idx(zone))
 set_page_address(page, __va(pfn << PAGE_SHIFT));
 #endif
+ VM_WARN_ON_ONCE(vmemmap_optimizable_order(pfn_to_section_compound_order(pfn)) &&
+ page_zone_id(page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) !=
+ page_zone_id(page));

Hmm, page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES is initialized a tad later than page so it'll have stale data in the page->flags, won't it?

Quoted 7 lines
Quoted 4 lines

Lance is right. The shared tail struct pages are already initialized by vmemmap_shared_tail_page() during vmemmap population, so they're not stale. The head 64 struct pages are initialized later — right here, after vmemmap population.

Still it looks out of place here, can this check be done in sparse-vmemmap somehow?

The struct page entries of a vmemmap-optimizable compound page are currently initialized in two stages. During vmemmap population, the shared tail entries are initialized first. The retained head area—normally 64—is initialized later through __init_single_page().

This warning connects the two stages: while initializing the retained entries in the second stage, it verifies that their zone information is consistent with the shared entries initialized in the first stage. Therefore, the same check cannot be performed during vmemmap population.

I am planning to first unify the HugeTLB and Device DAX compound-page initialization through a common helper [1]. Once that work is complete, maybe it will be easy to move the initialization of the retained head area into vmemmap population. With both the retained and shared entries initialized in the same stage, there will be no cross-stage inconsistency to check, and this warning can be removed.

Quoted 8 lines
Quoted 2 lines

Would keeping the check here for now and removing it as part of that follow-up sound reasonable to you?

While it feels really out of place in __init_single_page(), but having it memmap_init_range() close to the if that skips shared tail pages makes sense.

What do you say?

Sorry for the late reply because of traveling to LPC.

Putting the check in memmap_init_range() would validate the invariant for the shared-tail range skipped there. It would not, however, cover all vmemmap-optimized initialization
paths: optimized Device DAX bypasses memmap_init_range() when there is no altmap, and deferred boot-time HugeTLB initialization may also initialize retained entries elsewhere.

Quoted 5 lines

If the intention is to validate only the skip in memmap_init_range(), moving it there makes sense. If we want the warning to cover both HugeTLB and Device DAX generally, it still needs to remain in __init_single_page() or be called from the individual initialization paths.

Yeah, putting this into all the callers sucks too :(

With all the callers,__init_single_page() is the right place for this check, but I think we can add a static inline in sparse.h, like e.g.

vmemmap_optimization_verify_zone(page, pfn)

to make it a bit nicer.

Quoted 13 lines

Thanks,
Muchun

Quoted 9 lines
Quoted 5 lines

[1] https://lore.kernel.org/[email protected]/

Thanks,
Muchun

Signature · 2 lines
Sincerely yours,
Mike.
Signature · 2 lines
Sincerely yours,
Mike.
#24 lore raw

Re: [PATCH v3 6/6] mm/mm_init: add zone mismatch warning during page init

Quoted 78 lines · Mike Rapoport
Quoted 67 lines · Muchun Song
Quoted 48 lines · Mike Rapoport
Quoted 45 lines · Muchun Song
Quoted 17 lines · Mike Rapoport
Quoted 16 lines
Quoted 15 lines

mm/mm_init.c void __meminit __init_single_page(struct page *page, unsigned long pfn, · line 614

Quoted 12 lines
mm/mm_init.c +3 -0
index 1650d6bc1211..bd02e8d06965 100644
@@ -609,6 +609,9 @@ void __meminit __init_single_page(struct page *page, unsigned long pfn,
 if (!is_highmem_idx(zone))
 set_page_address(page, __va(pfn << PAGE_SHIFT));
 #endif
+ VM_WARN_ON_ONCE(vmemmap_optimizable_order(pfn_to_section_compound_order(pfn)) &&
+ page_zone_id(page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) !=
+ page_zone_id(page));

Hmm, page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES is initialized a tad later than page so it'll have stale data in the page->flags, won't it?

Quoted 7 lines
Quoted 4 lines

Lance is right. The shared tail struct pages are already initialized by vmemmap_shared_tail_page() during vmemmap population, so they're not stale. The head 64 struct pages are initialized later — right here, after vmemmap population.

Still it looks out of place here, can this check be done in sparse-vmemmap somehow?

The struct page entries of a vmemmap-optimizable compound page are currently initialized in two stages. During vmemmap population, the shared tail entries are initialized first. The retained head area—normally 64—is initialized later through __init_single_page().

This warning connects the two stages: while initializing the retained entries in the second stage, it verifies that their zone information is consistent with the shared entries initialized in the first stage. Therefore, the same check cannot be performed during vmemmap population.

I am planning to first unify the HugeTLB and Device DAX compound-page initialization through a common helper [1]. Once that work is complete, maybe it will be easy to move the initialization of the retained head area into vmemmap population. With both the retained and shared entries initialized in the same stage, there will be no cross-stage inconsistency to check, and this warning can be removed.

Quoted 8 lines
Quoted 2 lines

Would keeping the check here for now and removing it as part of that follow-up sound reasonable to you?

While it feels really out of place in __init_single_page(), but having it memmap_init_range() close to the if that skips shared tail pages makes sense.

What do you say?

Sorry for the late reply because of traveling to LPC.

Putting the check in memmap_init_range() would validate the invariant for the shared-tail range skipped there. It would not, however, cover all vmemmap-optimized initialization
paths: optimized Device DAX bypasses memmap_init_range() when there is no altmap, and deferred boot-time HugeTLB initialization may also initialize retained entries elsewhere.

Quoted 5 lines

If the intention is to validate only the skip in memmap_init_range(), moving it there makes sense. If we want the warning to cover both HugeTLB and Device DAX generally, it still needs to remain in __init_single_page() or be called from the individual initialization paths.

Quoted 8 lines

Yeah, putting this into all the callers sucks too :(

With all the callers,__init_single_page() is the right place for this check, but I think we can add a static inline in sparse.h, like e.g.

vmemmap_optimization_verify_zone(page, pfn)

to make it a bit nicer.

Yeah, factoring this into a helper makes sense.

Since it currently has only one caller, how about keeping it as a local static inline in mm_init.c instead of putting it in sparse.h?

Thanks,
Muchun

Quoted 18 lines
Quoted 14 lines

Thanks,
Muchun

Quoted 9 lines
Quoted 5 lines

[1] https://lore.kernel.org/[email protected]/

Thanks,
Muchun

Signature · 2 lines
Sincerely yours,
Mike.
Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#25 lore raw

Re: [PATCH v3 6/6] mm/mm_init: add zone mismatch warning during page init

Quoted 81 lines · Muchun Song
Quoted 81 lines · Mike Rapoport
Quoted 67 lines · Muchun Song
Quoted 48 lines · Mike Rapoport
Quoted 45 lines · Muchun Song
Quoted 17 lines · Mike Rapoport
Quoted 16 lines
Quoted 15 lines

mm/mm_init.c void __meminit __init_single_page(struct page *page, unsigned long pfn, · line 614

Quoted 12 lines
mm/mm_init.c +3 -0
index 1650d6bc1211..bd02e8d06965 100644
@@ -609,6 +609,9 @@ void __meminit __init_single_page(struct page *page, unsigned long pfn,
 if (!is_highmem_idx(zone))
 set_page_address(page, __va(pfn << PAGE_SHIFT));
 #endif
+ VM_WARN_ON_ONCE(vmemmap_optimizable_order(pfn_to_section_compound_order(pfn)) &&
+ page_zone_id(page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) !=
+ page_zone_id(page));

Hmm, page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES is initialized a tad later than page so it'll have stale data in the page->flags, won't it?

Quoted 7 lines
Quoted 4 lines

Lance is right. The shared tail struct pages are already initialized by vmemmap_shared_tail_page() during vmemmap population, so they're not stale. The head 64 struct pages are initialized later — right here, after vmemmap population.

Still it looks out of place here, can this check be done in sparse-vmemmap somehow?

The struct page entries of a vmemmap-optimizable compound page are currently initialized in two stages. During vmemmap population, the shared tail entries are initialized first. The retained head area—normally 64—is initialized later through __init_single_page().

This warning connects the two stages: while initializing the retained entries in the second stage, it verifies that their zone information is consistent with the shared entries initialized in the first stage. Therefore, the same check cannot be performed during vmemmap population.

I am planning to first unify the HugeTLB and Device DAX compound-page initialization through a common helper [1]. Once that work is complete, maybe it will be easy to move the initialization of the retained head area into vmemmap population. With both the retained and shared entries initialized in the same stage, there will be no cross-stage inconsistency to check, and this warning can be removed.

Quoted 8 lines
Quoted 2 lines

Would keeping the check here for now and removing it as part of that follow-up sound reasonable to you?

While it feels really out of place in __init_single_page(), but having it memmap_init_range() close to the if that skips shared tail pages makes sense.

What do you say?

Sorry for the late reply because of traveling to LPC.

Putting the check in memmap_init_range() would validate the invariant for the shared-tail range skipped there. It would not, however, cover all vmemmap-optimized initialization
paths: optimized Device DAX bypasses memmap_init_range() when there is no altmap, and deferred boot-time HugeTLB initialization may also initialize retained entries elsewhere.

Quoted 5 lines

If the intention is to validate only the skip in memmap_init_range(), moving it there makes sense. If we want the warning to cover both HugeTLB and Device DAX generally, it still needs to remain in __init_single_page() or be called from the individual initialization paths.

Yeah, putting this into all the callers sucks too :(

With all the callers,__init_single_page() is the right place for this check, but I think we can add a static inline in sparse.h, like e.g.

Quoted 8 lines
Quoted 3 lines

vmemmap_optimization_verify_zone(page, pfn)

to make it a bit nicer.

Yeah, factoring this into a helper makes sense.

Since it currently has only one caller, how about keeping it as a local static inline in mm_init.c instead of putting it in sparse.h?

Presuming we don't expect new callers any time soon let's put it just above __init_single_page().

Signature · 2 lines
Sincerely yours,
Mike.
#26 lore raw

Re: [PATCH v3 6/6] mm/mm_init: add zone mismatch warning during page init

Quoted 90 lines · Mike Rapoport
Quoted 85 lines · Muchun Song
Quoted 84 lines · Mike Rapoport
Quoted 67 lines · Muchun Song
Quoted 48 lines · Mike Rapoport
Quoted 45 lines · Muchun Song
Quoted 17 lines · Mike Rapoport
Quoted 16 lines
Quoted 15 lines

mm/mm_init.c void __meminit __init_single_page(struct page *page, unsigned long pfn, · line 614

Quoted 12 lines
mm/mm_init.c +3 -0
index 1650d6bc1211..bd02e8d06965 100644
@@ -609,6 +609,9 @@ void __meminit __init_single_page(struct page *page, unsigned long pfn,
 if (!is_highmem_idx(zone))
 set_page_address(page, __va(pfn << PAGE_SHIFT));
 #endif
+ VM_WARN_ON_ONCE(vmemmap_optimizable_order(pfn_to_section_compound_order(pfn)) &&
+ page_zone_id(page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) !=
+ page_zone_id(page));

Hmm, page + VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES is initialized a tad later than page so it'll have stale data in the page->flags, won't it?

Quoted 7 lines
Quoted 4 lines

Lance is right. The shared tail struct pages are already initialized by vmemmap_shared_tail_page() during vmemmap population, so they're not stale. The head 64 struct pages are initialized later — right here, after vmemmap population.

Still it looks out of place here, can this check be done in sparse-vmemmap somehow?

The struct page entries of a vmemmap-optimizable compound page are currently initialized in two stages. During vmemmap population, the shared tail entries are initialized first. The retained head area—normally 64—is initialized later through __init_single_page().

This warning connects the two stages: while initializing the retained entries in the second stage, it verifies that their zone information is consistent with the shared entries initialized in the first stage. Therefore, the same check cannot be performed during vmemmap population.

I am planning to first unify the HugeTLB and Device DAX compound-page initialization through a common helper [1]. Once that work is complete, maybe it will be easy to move the initialization of the retained head area into vmemmap population. With both the retained and shared entries initialized in the same stage, there will be no cross-stage inconsistency to check, and this warning can be removed.

Quoted 8 lines
Quoted 2 lines

Would keeping the check here for now and removing it as part of that follow-up sound reasonable to you?

While it feels really out of place in __init_single_page(), but having it memmap_init_range() close to the if that skips shared tail pages makes sense.

What do you say?

Sorry for the late reply because of traveling to LPC.

Putting the check in memmap_init_range() would validate the invariant for the shared-tail range skipped there. It would not, however, cover all vmemmap-optimized initialization
paths: optimized Device DAX bypasses memmap_init_range() when there is no altmap, and deferred boot-time HugeTLB initialization may also initialize retained entries elsewhere.

Quoted 5 lines

If the intention is to validate only the skip in memmap_init_range(), moving it there makes sense. If we want the warning to cover both HugeTLB and Device DAX generally, it still needs to remain in __init_single_page() or be called from the individual initialization paths.

Yeah, putting this into all the callers sucks too :(

With all the callers,__init_single_page() is the right place for this check, but I think we can add a static inline in sparse.h, like e.g.

vmemmap_optimization_verify_zone(page, pfn)

to make it a bit nicer.

Quoted 7 lines
Quoted 4 lines

Yeah, factoring this into a helper makes sense.

Since it currently has only one caller, how about keeping it as a local static inline in mm_init.c instead of putting it in sparse.h?

Presuming we don't expect new callers any time soon let's put it just above __init_single_page().

Make sense.

Thanks,
Muchun

Quoted 4 lines
Signature · 3 lines
-- 
Sincerely yours,
Mike.
Reviewed-by on v3 6/6
#27 lore raw

Re: [PATCH v3 6/6] mm/mm_init: add zone mismatch warning during page init

In reply to Muchun Song
Quoted 10 lines · Muchun Song

For vmemmap-optimized sections, tail struct pages may be backed by shared vmemmap pages. Those shared pages must carry the same page zone ID as the struct pages initialized for the section.

Warn in __init_single_page() if the shared tail page has a different page_zone_id(), which would indicate inconsistent initialization.

---

Useful sanity check :) Feel free to add:

maintainer
#28 lore raw
In reply to Muchun Song
Quoted 27 lines · On Tue, 29 Sep 2026 13:32:25 +0800 Muchun Song <[email protected]> wrote:

This v3 is based on mm-new commit 2ddb90ee544a, which contains v5 of "mm: Switch device DAX to section-based vmemmap optimization" [1].

This series is split out from the earlier, larger series "mm: Generalize HVO for HugeTLB and device DAX" [2]. While the parent series generalizes vmemmap optimization across HugeTLB and device DAX, this subset addresses a single, self-contained step: unifying their vmemmap population paths.

After the preceding Device DAX conversion, both HugeTLB and Device DAX describe optimized vmemmap mappings through memory-section metadata and use per-zone shared tail vmemmap pages. The generic code, however, still carries a Device DAX-specific population flag and compound-page population path, along with arguments and helpers needed only by that path.

This series first removes VMEMMAP_POPULATE_DAX and moves selection and reference handling for the shared tail page into the common vmemmap population path. It then removes the generic Device DAX-specific compound-page population path and routes section vmemmap population through vmemmap_populate(). The powerpc radix path continues to use its architecture-specific compound-page population implementation for optimizable sections.

The remaining patches remove the unused ptpfn argument, open-code vmemmap_populate_address() now that no caller needs its returned PTE, and add a warning for inconsistent zone initialization of shared tail vmemmap pages.

Quoted 4 lines

This is the fourth smaller step toward the broader HVO generalization. After this series, HugeTLB and Device DAX use the same population model instead of parallel generic paths, while powerpc keeps its architecture-specific implementation.

afaict the whole series is "no functional changes intended". Apart from [6/6]'s new WARN_ON.

And [3/6] is the one which could cause unintended fuctional changes!

Thanks. I'll queue it. Reluctantly. We're up to 688 MM patches this cycle and it's time to stop.

#29 lore raw
Quoted 33 lines · Andrew Morton
Quoted 27 lines · On Tue, 29 Sep 2026 13:32:25 +0800 Muchun Song <[email protected]> wrote:

This v3 is based on mm-new commit 2ddb90ee544a, which contains v5 of "mm: Switch device DAX to section-based vmemmap optimization" [1].

This series is split out from the earlier, larger series "mm: Generalize HVO for HugeTLB and device DAX" [2]. While the parent series generalizes vmemmap optimization across HugeTLB and device DAX, this subset addresses a single, self-contained step: unifying their vmemmap population paths.

After the preceding Device DAX conversion, both HugeTLB and Device DAX describe optimized vmemmap mappings through memory-section metadata and use per-zone shared tail vmemmap pages. The generic code, however, still carries a Device DAX-specific population flag and compound-page population path, along with arguments and helpers needed only by that path.

This series first removes VMEMMAP_POPULATE_DAX and moves selection and reference handling for the shared tail page into the common vmemmap population path. It then removes the generic Device DAX-specific compound-page population path and routes section vmemmap population through vmemmap_populate(). The powerpc radix path continues to use its architecture-specific compound-page population implementation for optimizable sections.

The remaining patches remove the unused ptpfn argument, open-code vmemmap_populate_address() now that no caller needs its returned PTE, and add a warning for inconsistent zone initialization of shared tail vmemmap pages.

Quoted 7 lines
Quoted 4 lines

This is the fourth smaller step toward the broader HVO generalization. After this series, HugeTLB and Device DAX use the same population model instead of parallel generic paths, while powerpc keeps its architecture-specific implementation.

afaict the whole series is "no functional changes intended". Apart from [6/6]'s new WARN_ON.

Yes.

Quoted 2 lines

And [3/6] is the one which could cause unintended fuctional changes!

Understood. However, the resulting vmemmap layout is intended to remain unchanged.

Quoted 3 lines

Thanks. I'll queue it. Reluctantly. We're up to 688 MM patches this cycle and it's time to stop.

Thanks for taking it despite the load.

Muchun,
Thanks.