On Tue, Dec 01, 2026 at 07:56:47PM +0800, Muchun Song wrote:
>Generic vmemmap population rejects existing PTEs in optimized tail
>ranges, even when they already map the expected shared tail page.
>On PowerPC with 64 KiB pages, multiple sparsemem sections can share
>one vmemmap PTE, so successive section population may encounter an
>existing shared tail mapping.
>
>Allow an existing optimized PTE to be reused only when it maps the
>expected shared tail page, without acquiring another reference.
>This is needed before PowerPC radix can switch to the generic HVO
>vmemmap population path.
>
>Factor this handling into a separate helper to ease a later move of
>the generic vmemmap optimization code into a dedicated source file.
>
>Signed-off-by: Muchun Song <[email protected]>
>---
> mm/sparse-vmemmap.c | 48 +++++++++++++++++++++++++--------------------
> 1 file changed, 27 insertions(+), 21 deletions(-)
>
>diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c
>index 4ac4c09b1bf8..6ec0cac8eab6 100644
>--- a/mm/sparse-vmemmap.c
>+++ b/mm/sparse-vmemmap.c
>@@ -235,15 +235,14 @@ void __meminit 
>section_vmemmap_optimization_disable(unsigned long start,
> }
> #endif
> 
>-static __meminit void *vmemmap_alloc_pte(unsigned long pfn, int node,
>-              struct vmem_altmap *altmap)
>+static int __meminit vmemmap_optimizable_pte_populate(pte_t *pte,
>+              unsigned long addr, int node)
> {
>+      const unsigned long pfn = page_to_pfn((struct page *)addr);
>+      const unsigned int order = pfn_to_section_compound_order(pfn);
>+      pte_t entry = ptep_get(pte);
>       struct zone *zone;
>       struct page *page;
>-      const unsigned int order = pfn_to_section_compound_order(pfn);
>-
>-      if (!vmemmap_optimizable_pfn(pfn, order))
>-              return vmemmap_alloc_block_buf(PAGE_SIZE, node, altmap);
> 
>       /*
>        * Before slab is available, vmemmap optimization is used for early
>@@ -257,7 +256,10 @@ static __meminit void *vmemmap_alloc_pte(unsigned long 
>pfn, int node,
>       zone = slab_is_available() ? device_zone(node) : pfn_to_zone(pfn, node);
>       page = vmemmap_shared_tail_page(order, zone);
>       if (!page)
>-              return NULL;
>+              return -ENOMEM;
>+
>+      if (!pte_none(entry))
>+              return pte_page(entry) == page ? 0 : -EOPNOTSUPP;

WARN_ON_ONCE() sounds good here, like Chris suggested, may save the day
when something goes wrong ...

And, yeah, better to flag what’s off early instead of staring at -ENOMEM :D

Otherwise, LGTM, feel free to add:

Reviewed-by: Lance Yang <[email protected]>

Reply via email to