Re: [PATCH v2 06/14] mm/mm_init: factor out frozen compound page initialization
From: Muchun Song
Date: Mon Nov 16 2026 - 22:11:54 EST
On 2026/11/16 18:42, Mike Rapoport wrote:
Hi,
Hi Mike,
ZONE_DEVICE initializes compound struct pages while their refcounts remainDo we really need the _frozen suffix here?
frozen. The setup is open-coded in memmap_init_compound(), including the
per-page initialization and compound tail preparation.
Factor the common setup into memmap_init_compound_page_frozen(). Keep the
pgmap association and MEMORY_DEVICE_GENERIC refcount handling in the
ZONE_DEVICE-specific path.
This provides an MM-internal helper for other early memmap users to
share.
No functional change is intended.
Signed-off-by: Muchun Song <songmuchun@xxxxxxxxxxxxx>
---
v2:
- Rebase on memmap_init_page_from_template().
- Rename memmap_init_zone_device_head_page to memmap_init_zone_device_head_fields.
---
mm/mm_init.c | 114 +++++++++++++++++++++++++--------------------------
mm/mm_init.h | 2 +
2 files changed, 59 insertions(+), 57 deletions(-)
diff --git a/mm/mm_init.c b/mm/mm_init.c
index bf943ae72a3e5..f1e539aec3d91 100644
--- a/mm/mm_init.c
+++ b/mm/mm_init.c
@@ -738,6 +738,28 @@ void __meminit memmap_init_page_from_template(struct page *page,
vmemmap_optimization_verify_zone(page);
}
+void __meminit memmap_init_compound_page_frozen(struct page *head,
+ unsigned long pfn, const struct zone *zone, unsigned int order)
Maybe not. I can remove it.
+{I can't say I like this name. Essentailly we initialize ZONE_DEVICE
+ unsigned long nr_pages = 1UL << order;
+ struct page template;
+
+ memmap_init_page_common(&template, zone_idx(zone), zone_to_nid(zone));
+ prep_compound_tail(&template, head, order);
+
+ /* Initialize the head page. */
+ __init_single_page(head, pfn, zone_idx(zone), zone_to_nid(zone));
+ set_page_count(head, 0);
+ __SetPageHead(head);
+
+ if (vmemmap_optimizable_order(pfn_to_section_compound_order(pfn)))
+ nr_pages = VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES;
+ /* Initialize the tail pages from PFN-independent template page. */
+ for (unsigned long i = 1; i < nr_pages; i++)
+ memmap_init_page_from_template(head + i, pfn + i, zone, &template);
+ prep_compound_head(head, order);
+}
+
struct zone __meminit *pfn_to_zone(unsigned long pfn, int nid)
{
pg_data_t *pgdat = NODE_DATA(nid);
@@ -1063,44 +1085,39 @@ static void __init memmap_init(void)
}
#ifdef CONFIG_ZONE_DEVICE
-/*
- * With compound page geometry and when struct pages are stored in ram most
- * tail pages are reused. Consequently, the amount of unique struct pages to
- * 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.
- */
-static inline unsigned long compound_nr_pages(unsigned long pfn,
- struct dev_pagemap *pgmap)
+static void memmap_init_zone_device_head_fields(struct page *head,
+ struct dev_pagemap *pgmap)
folio here, so maybe it can be
memmap_init_zone_device_folio(struct folio *folio)
Make more sense.
{... and then we won't need this cast and the caller will just pass
- const struct mem_section *ms = __pfn_to_section(pfn);
-
- if (!section_vmemmap_optimizable(ms))
- return pgmap_vmemmap_nr(pgmap);
-
- return VMEMMAP_OPTIMIZATION_PAGES * (PAGE_SIZE / sizeof(struct page));
+ /*
+ * MEMORY_DEVICE_GENERIC pages start with a refcount of one; other
+ * ZONE_DEVICE pages raise it when handed out. Use atomic_set() because
+ * @head may be a stack template, while init_page_count() expects a
+ * memmap-backed page.
+ */
+ if (pgmap->type == MEMORY_DEVICE_GENERIC)
+ atomic_set(&head->_refcount, 1);
+ /*
+ * ZONE_DEVICE pages union ->lru with a ->pgmap back pointer and
+ * zone_device_data. It is a bug if a ZONE_DEVICE page is ever freed
+ * or placed on a driver-private list.
+ */
+ ((struct folio *)head)->pgmap = pgmap;
page_folio() here.
Right. Initially, I thought using page_folio on the template page might
be problematic when compound_info_has_mask() == true. However, I found
that the template page happens to be order-0, which avoids the issue.
So, we can safely use page_folio.
+ head->zone_device_data = NULL;_basepages sounds odd for me, maybe _pages will do?
}
-static void __ref memmap_init_compound(struct page *head,
- unsigned long head_pfn,
- unsigned long zone_idx, int nid,
- struct dev_pagemap *pgmap,
- unsigned long nr_pages)
+static void memmap_init_zone_device_basepages(unsigned long start, unsigned long end,
+ const struct zone *zone, struct dev_pagemap *pgmap)
Sounds good.
Thanks,
Muchun