Re: [PATCH v7 2/2] mm/memory_hotplug: optimize zone contiguous check when changing pfn range

From: David Hildenbrand (Arm)

Date: Wed Aug 19 2026 - 12:00:35 EST



> /*
> * Only struct pages that correspond to ranges defined by memblock.memory
> * are zeroed and initialized by going through __init_single_page() during
> @@ -822,22 +844,27 @@ void __meminit init_deferred_page(unsigned long pfn, int nid)
> * zone/node above the hole except for the trailing pages in the last
> * section that will be appended to the zone/node below.
> */
> -static void __init init_unavailable_range(unsigned long spfn,
> - unsigned long epfn,
> - int zone, int node)
> +static unsigned long __init init_unavailable_range(unsigned long spfn,
> + unsigned long epfn,
> + int zone, int node)
> {
> unsigned long pfn;
> - u64 pgcnt = 0;
> + u64 pgcnt = 0, online_pgcnt = 0;
> + unsigned long last_subsection = -1;
> + bool is_online = false;
>
> for_each_valid_pfn(pfn, spfn, epfn) {
> __init_single_page(pfn_to_page(pfn), pfn, zone, node);
> __SetPageReserved(pfn_to_page(pfn));
> + if (unavailable_pfn_is_online(pfn, &last_subsection, &is_online))
> + online_pgcnt++;

Can we avoid these helpers?

const unsigned long subsection = pfn & PAGE_SUBSECTION_MASK;

/* We can have section-sized online holes with VMEMMAP. */
if (IS_ENMABLED(CONFIG_SPARSEMEM_VMEMMAP) &&
subsection != last_subsection) {
is_online = pfn_to_online_page(pfn);
last_subsection = subsection;
}
if (is_online)
online_pgcnt++;

An alternative is an inner loop that just walks in SUBSECTION chunks until epfn.
That would probably be even cleaner and faster.

I remember !vmemmap always only has early sections when they are actually
online. We could extent the comment to clarify that.


(I have patches to clean that init code up)


--
Cheers,

David