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

From: Mike Rapoport

Date: Tue Sep 15 2026 - 15:01:58 EST


On Tue, Sep 15, 2026 at 05:20:07PM +0200, David Hildenbrand (Arm) wrote:
> On 9/15/26 08:03, Mike Rapoport wrote:
> > Hi,
> >
> > On Mon, Sep 14, 2026 at 03:29:29AM -0400, Yuan Liu wrote:
> >> When move_pfn_range_to_zone() or remove_pfn_range_from_zone() updates a
> >> zone, set_zone_contiguous() rescans the entire zone pageblock-by-pageblock
> >> to rebuild zone->contiguous. For large zones this is a significant cost
> >> during memory hotplug and hot-unplug.
> >>
> >> diff --git a/mm/mm_init.c b/mm/mm_init.c
> >> index 1533aebafb68..d40a8ff23370 100644
> >> --- a/mm/mm_init.c
> >> +++ b/mm/mm_init.c
> >> @@ -817,22 +817,39 @@ 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 next_chunk_pfn __maybe_unused = spfn;
> >> unsigned long pfn;
> >> - u64 pgcnt = 0;
> >> + u64 online_pgcnt = 0, pgcnt = 0;
> >> + bool is_online = true;
> >>
> >> for_each_valid_pfn(pfn, spfn, epfn) {
> >> __init_single_page(pfn_to_page(pfn), pfn, zone, node);
> >> __SetPageReserved(pfn_to_page(pfn));
> >> pgcnt++;
> >> +
> >> + /*
> >> + * With vmemmap, at this stage all pages in an early section
> >> + * have a valid memmap and are marked as online. However, only
> >> + * subsections in the subsection map are actually online.
> >> + */
> >
> > I'm having trouble parsing this comment. Shouldn't is say that some
> > subsections in a section can be offline because of holes?
>
>
> Maybe extending that a bit we could do
>
> "With CONFIG_SPARSEMEM_VMEMMAP, early sections have a valid memmap for all PFNs
> and the sections are marked online. However, we might have offline subsections
> in such early sections, indicated by the subsection map. While we must
> initialize the entire valid memmap, account only the online pages according to
> the subsection map as online (see pfn_to_online_page())."

init_unavailable_range() already has large comment on top, adding something
like that there makes perfect sense to me :)

How about a bit different version:

* The function counts pages that should be added to
* zone->pages_with_online_memmap.
* With CONFIG_SPARSEMEM_VMEMMAP there could be offline subsections even though
* the entire memory map is valid and all the early sections are online.
* Count only pages in online subsections (see pfn_to_online_page()).

> --
> Cheers,
>
> David

--
Sincerely yours,
Mike.