[PATCH 29/49] mm: extract pfn_to_zone() helper

Muchun Song
2026-04-05T12:56:35Z

Extract pfn_to_zone() from sparse-vmemmap.c to mm_init.c and use it
in __init_page_from_nid(). This removes duplicated code for finding
the zone for a given PFN.

Signed-off-by: Muchun Song <songmuchun@bytedance.com>
---
mm/internal.h | 1 +
mm/mm_init.c | 28 ++++++++++++++++------------
mm/sparse-vmemmap.c | 14 --------------
3 files changed, 17 insertions(+), 26 deletions(-)

diff --git a/mm/internal.h b/mm/internal.h
index 27c06250d6b8..b569d8309f4d 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -1350,6 +1350,7 @@ static inline bool deferred_pages_enabled(void)
}
#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */

+struct zone *pfn_to_zone(unsigned long pfn, int nid);
void init_deferred_page(unsigned long pfn, int nid);

enum mminit_level {
diff --git a/mm/mm_init.c b/mm/mm_init.c
index b47f65425bc1..e47d08b63154 100644
--- a/mm/mm_init.c
+++ b/mm/mm_init.c
@@ -687,24 +687,28 @@ static __meminit void pageblock_migratetype_init_range(unsigned long pfn,
}
}

+struct zone __meminit *pfn_to_zone(unsigned long pfn, int nid)
+{
+ pg_data_t *pgdat = NODE_DATA(nid);
+
+ for (enum zone_type zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
+ struct zone *zone = &pgdat->node_zones[zone_type];
+
+ if (zone_spans_pfn(zone, pfn))
+ return zone;
+ }
+
+ return NULL;
+}
+
#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
/*
* Initialize a reserved page unconditionally, finding its zone first.
*/
static void __meminit __init_page_from_nid(unsigned long pfn, int nid)
{
- pg_data_t *pgdat;
- int zid;
-
- pgdat = NODE_DATA(nid);
-
- for (zid = 0; zid < MAX_NR_ZONES; zid++) {
- struct zone *zone = &pgdat->node_zones[zid];
-
- if (zone_spans_pfn(zone, pfn))
- break;
- }
- __init_single_page(pfn_to_page(pfn), pfn, zid, nid);
+ __init_single_page(pfn_to_page(pfn), pfn,
+ zone_idx(pfn_to_zone(pfn, nid)), nid);

if (pageblock_aligned(pfn))
init_pageblock_migratetype(pfn_to_page(pfn), MIGRATE_MOVABLE,
diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c
index d266bcf45b5c..9da49b0d03f0 100644
--- a/mm/sparse-vmemmap.c
+++ b/mm/sparse-vmemmap.c
@@ -143,20 +143,6 @@ void __meminit vmemmap_verify(pte_t *pte, int node,
start, end - 1);
}

-static struct zone __meminit *pfn_to_zone(unsigned long pfn, int nid)
-{
- pg_data_t *pgdat = NODE_DATA(nid);
-
- for (enum zone_type zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
- struct zone *zone = &pgdat->node_zones[zone_type];
-
- if (zone_spans_pfn(zone, pfn))
- return zone;
- }
-
- return NULL;
-}
-
static __meminit struct page *vmemmap_get_tail(unsigned int order, struct zone *zone);

static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node,
--
2.20.1