mm/memory_hotplug: optimize zone contiguous check when changing pfn range
Subsystems (8)
v6 cover mm/memory_hotplug: optimize zone contiguous check when changing pfn range
This series introduces a pages_with_online_memmap member into struct zone to avoid pageblock-by-pageblock scans across the entire zone and improve memory hotplug performance.
Reworked how pages_with_online_memmap accounts for PFNs in early boot memory in response to Sashiko's review comments [2], which identified potential issues with the previous implementation.
Approach
========
Add a new zone member pages_with_online_memmap that tracks the number of pages within the zone span that have an online memory map (including present pages and memory holes smaller than a subsection whose memory map has been initialized). When spanned_pages == pages_with_online_memmap the zone is contiguous and pfn_to_page() can be called on any PFN in the zone span without further pfn_valid() checks.
For early boot memory, pages_with_online_memmap is calculated in memmap_init_zone_range(). Since every PFN within a memblock region satisfies pfn_to_online_page(), the calculation aligns both boundaries of each memblock range to PAGES_PER_SUBSECTION and counts the aligned pages that fall within the zone span. If two adjacent memblock regions share a subsection (i.e. the hole between them is smaller than a subsection), the overlapping subsection pages are counted only once. For hotplugged memory, pages_with_online_memmap is updated through adjust_present_page_count(), which is called during memory online and offline.
Only pages that fall within the current zone span are accounted towards pages_with_online_memmap. A "too small" value is safe, it merely prevents detecting a contiguous zone.
The contiguity check using pages_with_online_memmap is stricter than the old pageblock-by-pageblock scan. The old set_zone_contiguous() iterated at pageblock granularity via pageblock_pfn_to_page(), so a zone could be marked contiguous even if a subsection-sized hole existed within a pageblock. The new check requires spanned_pages == pages_with_online_memmap, meaning every PFN in the zone span must satisfy pfn_to_online_page().
Performance
===========
1. For VM hotplug performance data, please refer to Patch 1.
2. This series also benefits CXL hotplug. Performance results are as follows
https://lore.kernel.org/all/20260409023552.GA2807@AE/
Tested cases
============
1. Hotplug/unplug correctness with both online_movable and online policies, including partial unplug, middle-block offline gaps, and edge-block span shrink.
2. Large scale (256G/512G) plug/unplug performance.
3. Boot-time subsection holes (aligned/unaligned, in-zone and cross-zone) with correct pages_with_online_memmap accounting.
4. kernelcore=mirror: verified no overcounting.
Patch overview
==============
This series is based on the kernelcore-mirror branch [1].
Patch 1 introduces pages_with_online_memmap to replace pageblock-by-pageblock scans across the entire zone for zone contiguity checks.
Patch 2 avoids incorrectly shrinking the zone span during memory unplug when a memory or hole boundary falls in the middle of a subsection.
v6 changes:
1. Dropped patches 1-3 from v5. The kernelcore=mirror overlap issue that caused pages_with_online_memmap to be overcounted has been resolved by the series [1].
2. Reworked pages_with_online_memmap accounting during early boot.
v5:
https://lore.kernel.org/linux-mm/[email protected]/
v4:
https://lore.kernel.org/linux-mm/[email protected]/
v3:
https://lore.kernel.org/linux-mm/[email protected]/
[1] https://git.kernel.org/pub/scm/linux/kernel/git/rppt/memblock.git/log/?h=kernelcore-mirror
[2] https://sashiko.dev/#/patchset/[email protected]
Yuan Liu (2):
mm/memory_hotplug: optimize zone contiguous check when changing pfn
range
mm/memory_hotplug: improve shrink_zone_span() subsection boundary
checks
Documentation/mm/physical_memory.rst | 13 +++++++ drivers/base/memory.c | 6 +++ include/linux/mmzone.h | 47 ++++++++++++++++++++++ mm/internal.h | 8 +--- mm/memory_hotplug.c | 54 ++++++++++++++------------ mm/mm_init.c | 58 +++++++++++++++++----------- 6 files changed, 132 insertions(+), 54 deletions(-)
base-commit: b69c2a1fa9beb4c3db24b4013e07f5cb7e7260aa
Signature · 1 line
2.47.3
PATCH v6 1/2 mm/memory_hotplug: optimize zone contiguous check when changing pfn range
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.
Add a new zone member pages_with_online_memmap that tracks the number of pages within the zone span that have an online memory map (including present pages and memory holes smaller than a subsection whose memory map has been initialized). When spanned_pages == pages_with_online_memmap the zone is contiguous and pfn_to_page() can be called on any PFN in the zone span without further pfn_valid() checks.
For early boot memory, pages_with_online_memmap is calculated in memmap_init_zone_range(). Since every PFN within a memblock region satisfies pfn_to_online_page(), the calculation aligns both boundaries of each memblock range to PAGES_PER_SUBSECTION and counts the aligned pages that fall within the zone span. If two adjacent memblock regions share a subsection (i.e. the hole between them is smaller than a subsection), the overlapping subsection pages are counted only once. For hotplugged memory, pages_with_online_memmap is updated through adjust_present_page_count(), which is called during memory online and offline.
Only pages that fall within the current zone span are accounted towards pages_with_online_memmap. A "too small" value is safe, it merely prevents detecting a contiguous zone.
The contiguity check using pages_with_online_memmap is stricter than the old pageblock-by-pageblock scan. The old set_zone_contiguous() iterated at pageblock granularity via pageblock_pfn_to_page(), so a zone could be marked contiguous even if a subsection-sized hole existed within a pageblock. The new check requiresspanned_pages == pages_with_online_memmap, meaning every PFN in the zone span must satisfy pfn_to_online_page().
The following test cases of memory hotplug for a VM [1], tested in the environment [2], show that this optimization can significantly reduce the memory hotplug time [3].
+----------------+------+---------------+--------------+----------------+ | | Size | Time (before) | Time (after) | Time Reduction | | +------+---------------+--------------+----------------+ | Plug Memory | 256G | 10s | 3s | 70% | | +------+---------------+--------------+----------------+ | | 512G | 36s | 7s | 81% | +----------------+------+---------------+--------------+----------------+
+----------------+------+---------------+--------------+----------------+ | | Size | Time (before) | Time (after) | Time Reduction | | +------+---------------+--------------+----------------+ | Unplug Memory | 256G | 11s | 4s | 64% | | +------+---------------+--------------+----------------+ | | 512G | 36s | 9s | 75% | +----------------+------+---------------+--------------+----------------+
[1] Qemu commands to hotplug 256G/512G memory for a VM:
object_add memory-backend-ram,id=hotmem0,size=256G/512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 256G/512G (Plug Memory) qom-set vmem1 requested-size 0G (Unplug Memory)
[2] Hardware : Intel Icelake server
Guest Kernel : v7.0-rc4
Qemu : v9.0.0
Launch VM :
qemu-system-x86_64 -accel kvm -cpu host \
-drive file=./Centos10_cloud.qcow2,format=qcow2,if=virtio \
-drive file=./seed.img,format=raw,if=virtio \
-smp 3,cores=3,threads=1,sockets=1,maxcpus=3 \
-m 2G,slots=10,maxmem=2052472M \
-device pcie-root-port,id=port1,bus=pcie.0,slot=1,multifunction=on \
-device pcie-root-port,id=port2,bus=pcie.0,slot=2 \
-nographic -machine q35 \
-nic user,hostfwd=tcp::3000-:22
Guest kernel auto-onlines newly added memory blocks:
echo online > /sys/devices/system/memory/auto_online_blocks[3] The time from typing the QEMU commands in [1] to when the output of 'grep MemTotal /proc/meminfo' on Guest reflects that all hotplugged memory is recognized.
--- Documentation/mm/physical_memory.rst | 13 +++++++ drivers/base/memory.c | 6 +++ include/linux/mmzone.h | 47 ++++++++++++++++++++++ mm/internal.h | 8 +--- mm/memory_hotplug.c | 12 +----- mm/mm_init.c | 58 +++++++++++++++++----------- 6 files changed, 105 insertions(+), 39 deletions(-)
Documentation/mm/physical_memory.rst +13 -0
@@ -483,6 +483,19 @@ General ``present_pages`` should use ``get_online_mems()`` to get a stable value. It is initialized by ``calculate_node_totalpages()``. +``pages_with_online_memmap`` + Tracks pages within the zone that have an online memory map (present pages + and memory holes whose memory map has been initialized). When + ``spanned_pages`` == ``pages_with_online_memmap``, ``pfn_to_page()`` can be + performed without further checks on any PFN within the zone span. + + Note: this counter may temporarily undercount when pages with an online + memory map exist outside the current zone span. This can only happen during + boot, when initializing the memory map of pages that do not fall into any + zone span. Growing the zone to cover such pages and later shrinking it back + may result in a "too small" value. This is safe: it merely prevents + detecting a contiguous zone. + ``present_early_pages`` The present pages existing within the zone located on memory available since early boot, excluding hotplugged memory. Defined only when
drivers/base/memory.c +6 -0
@@ -246,6 +246,7 @@ static int memory_block_online(struct memory_block *mem) nr_vmemmap_pages = mem->altmap->free;mem_hotplug_begin (); +clear_zone_contiguous (zone);if (nr_vmemmap_pages) { ret =mhp_init_memmap_on_memory (start_pfn, nr_vmemmap_pages, zone);if (ret)
@@ -270,6 +271,7 @@ static int memory_block_online(struct memory_block *mem) mem->zone = zone; out: +set_zone_contiguous (zone);mem_hotplug_done ();return ret; }
@@ -282,6 +284,7 @@ static int memory_block_offline(struct memory_block *mem)unsigned long start_pfn =section_nr_to_pfn (mem->start_section_nr);unsigned long nr_pages =PAGES_PER_SECTION * sections_per_block;unsigned long nr_vmemmap_pages =0 ; +struct zone *zone;int ret;if (!mem->zone)
@@ -294,7 +297,9 @@ static int memory_block_offline(struct memory_block *mem)if (mem->altmap) nr_vmemmap_pages = mem->altmap->free; + zone = mem->zone;mem_hotplug_begin (); +clear_zone_contiguous (zone);if (nr_vmemmap_pages)adjust_present_page_count (pfn_to_page (start_pfn), mem->group, -nr_vmemmap_pages);
@@ -314,6 +319,7 @@ static int memory_block_offline(struct memory_block *mem) mem->zone =NULL ; out: +set_zone_contiguous (zone);mem_hotplug_done ();return ret; }
include/linux/mmzone.h +47 -0
65 lines not on this page · raw message
mm/internal.h +1 -7
@@ -805,21 +805,15 @@ extern struct page *__pageblock_pfn_to_page(unsigned long start_pfn,static inline struct page *pageblock_pfn_to_page (unsigned long start_pfn,unsigned long end_pfn,struct zone *zone) { -if (zone->contiguous) +if (zone_is_contiguous (zone))return pfn_to_page (start_pfn);return __pageblock_pfn_to_page (start_pfn, end_pfn, zone); } -void set_zone_contiguous (struct zone *zone);bool pfn_range_intersects_zones (int nid,unsigned long start_pfn,unsigned long nr_pages); -static inline void clear_zone_contiguous (struct zone *zone) -{ - zone->contiguous = false; -} -extern int __isolate_free_page (struct page *page,unsigned int order);extern void __putback_isolated_page (struct page *page,unsigned int order,int mt);
mm/memory_hotplug.c +2 -10
@@ -557,18 +557,13 @@ void remove_pfn_range_from_zone(struct zone *zone,/* * Zone shrinking code cannot properly deal with ZONE_DEVICE. So -* we will not try to shrink the zones - which is okay as -* set_zone_contiguous() cannot deal with ZONE_DEVICE either way. +* we will not try to shrink it. */ if (zone_is_zone_device (zone))return ; -clear_zone_contiguous (zone); -shrink_zone_span (zone, start_pfn, start_pfn + nr_pages);update_pgdat_span (pgdat); - -set_zone_contiguous (zone); }/**
@@ -746,8 +741,6 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,struct pglist_data *pgdat = zone->zone_pgdat;int nid = pgdat->node_id; -clear_zone_contiguous (zone); -if (zone_is_empty (zone))init_currently_empty_zone (zone, start_pfn, nr_pages);resize_zone_range (zone, start_pfn, nr_pages);
@@ -775,8 +768,6 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,memmap_init_range (nr_pages, nid,zone_idx (zone), start_pfn,0 ,MEMINIT_HOTPLUG , altmap, migratetype, isolate_pageblock); - -set_zone_contiguous (zone); }struct auto_movable_stats {
@@ -1072,6 +1063,7 @@ void adjust_present_page_count(struct page *page, struct memory_group *group,if (early_section (__pfn_to_section (page_to_pfn (page)))) zone->present_early_pages += nr_pages; zone->present_pages += nr_pages; + zone->pages_with_online_memmap += nr_pages; zone->zone_pgdat->node_present_pages += nr_pages;if (group && movable)
mm/mm_init.c +36 -22
76 lines not on this page · raw message
Signature · 1 line
2.47.3
David Hildenbrand (Arm)
maintainerQuoted 109 lines · Yuan Liu
When
move_pfn_range_to_zone()orremove_pfn_range_from_zone()updates a zone,set_zone_contiguous()rescans the entire zone pageblock-by-pageblock to rebuildzone->contiguous. For large zones this is a significant cost during memory hotplug and hot-unplug.Add a new zone member
pages_with_online_memmapthat tracks the number of pages within the zone span that have an online memory map (including present pages and memory holes smaller than a subsection whose memory map has been initialized). Whenspanned_pages==pages_with_online_memmapthe zone is contiguous andpfn_to_page()can be called on any PFN in the zone span without furtherpfn_valid()checks.For early boot memory,
pages_with_online_memmapis calculated inmemmap_init_zone_range(). Since every PFN within a memblock region satisfiespfn_to_online_page(), the calculation aligns both boundaries of each memblock range toPAGES_PER_SUBSECTIONand counts the aligned pages that fall within the zone span. If two adjacent memblock regions share a subsection (i.e. the hole between them is smaller than a subsection), the overlapping subsection pages are counted only once. For hotplugged memory,pages_with_online_memmapis updated throughadjust_present_page_count(), which is called during memory online and offline.Only pages that fall within the current zone span are accounted towards
pages_with_online_memmap. A "too small" value is safe, it merely prevents detecting a contiguous zone.The contiguity check using
pages_with_online_memmapis stricter than the old pageblock-by-pageblock scan. The oldset_zone_contiguous()iterated at pageblock granularity viapageblock_pfn_to_page(), so a zone could be marked contiguous even if a subsection-sized hole existed within a pageblock. The new check requiresspanned_pages==pages_with_online_memmap, meaning every PFN in the zone span must satisfypfn_to_online_page().The following test cases of memory hotplug for a VM [1], tested in the environment [2], show that this optimization can significantly reduce the memory hotplug time [3].
+----------------+------+---------------+--------------+----------------+ | | Size | Time (before) | Time (after) | Time Reduction | | +------+---------------+--------------+----------------+ | Plug Memory | 256G | 10s | 3s | 70% | | +------+---------------+--------------+----------------+ | | 512G | 36s | 7s | 81% | +----------------+------+---------------+--------------+----------------++----------------+------+---------------+--------------+----------------+ | | Size | Time (before) | Time (after) | Time Reduction | | +------+---------------+--------------+----------------+ | Unplug Memory | 256G | 11s | 4s | 64% | | +------+---------------+--------------+----------------+ | | 512G | 36s | 9s | 75% | +----------------+------+---------------+--------------+----------------+[1] Qemu commands to hotplug 256G/512G memory for a VM:
object_addmemory-backend-ram,id=hotmem0,size=256G/512G,share=ondevice_addvirtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 256G/512G (Plug Memory) qom-set vmem1 requested-size 0G (Unplug Memory)[2] Hardware : Intel Icelake server
Guest Kernel : v7.0-rc4 Qemu : v9.0.0 Launch VM : qemu-system-x86_64 -accel kvm -cpu host \ -drive file=./Centos10_cloud.qcow2,format=qcow2,if=virtio \ -drive file=./seed.img,format=raw,if=virtio \ -smp 3,cores=3,threads=1,sockets=1,maxcpus=3 \ -m 2G,slots=10,maxmem=2052472M \ -device pcie-root-port,id=port1,bus=pcie.0,slot=1,multifunction=on \ -device pcie-root-port,id=port2,bus=pcie.0,slot=2 \ -nographic -machine q35 \ -nic user,hostfwd=tcp::3000-:22 Guest kernel auto-onlines newly added memory blocks: echo online > /sys/devices/system/memory/auto_online_blocks[3] The time from typing the QEMU commands in [1] to when the output of 'grep MemTotal
/proc/meminfo' on Guest reflects that all hotplugged memory is recognized.Reported-by Nanhai Zou <[email protected]>Reported-by Chen Zhang <[email protected]>Reviewed-by Pan Deng <[email protected]>Reviewed-by Jason Zeng <[email protected]>Co-developed-by Tianyou Li <[email protected]>Signed-off-by Tianyou Li <[email protected]>Signed-off-by Yuan Liu <[email protected]>--- Documentation/mm/physical_memory.rst | 13 +++++++ drivers/base/memory.c | 6 +++ include/linux/mmzone.h | 47 ++++++++++++++++++++++ mm/internal.h | 8 +--- mm/memory_hotplug.c | 12 +----- mm/mm_init.c | 58 +++++++++++++++++----------- 6 files changed, 105 insertions(+), 39 deletions(-)Documentation/mm/physical_memory.rst +2
-0@@ -483,6 +483,19 @@ General ``present_pages`` should use ``get_online_mems()`` to get a stable value. It is initialized by ``calculate_node_totalpages()``. +``pages_with_online_memmap`` + Tracks pages within the zone that have an online memory map (present pages
Documentation/mm/physical_memory.rst General · line 497
Quoted 10 lines
Documentation/mm/physical_memory.rst +10
-0+ and memory holes whose memory map has been initialized). When + ``spanned_pages`` == ``pages_with_online_memmap``, ``pfn_to_page()`` can be + performed without further checks on any PFN within the zone span. + + Note: this counter may temporarily undercount when pages with an online + memory map exist outside the current zone span. This can only happen during + boot, when initializing the memory map of pages that do not fall into any + zone span. Growing the zone to cover such pages and later shrinking it back + may result in a "too small" value. This is safe: it merely prevents + detecting a contiguous zone.
It's suboptimal that we repeat the same comment that we already have in struct zone. Can we just keep it vry simple here?
"Pages within the zone that have an online memory map: present pages and memory holes whose memory map has been initialized. See XXX for more details."
Quoted 25 lines
Documentation/mm/physical_memory.rst +1
-0+ ``present_early_pages`` The present pages existing within the zone located on memory available since early boot, excluding hotplugged memory. Defined only whendrivers/base/memory.c +2
-0@@ -246,6 +246,7 @@ static int memory_block_online(struct memory_block *mem) nr_vmemmap_pages = mem->altmap->free;mem_hotplug_begin (); +clear_zone_contiguous (zone);if (nr_vmemmap_pages) { ret =mhp_init_memmap_on_memory (start_pfn, nr_vmemmap_pages, zone);if (ret)@@ -270,6 +271,7 @@ static int memory_block_online(struct memory_block *mem) mem->zone = zone; out: +set_zone_contiguous (zone);mem_hotplug_done ();return ret; }
drivers/base/memory.c static int memory_block_offline(struct memory_block *mem) · line 287
Quoted 4 lines
drivers/base/memory.c +1
-0@@ -282,6 +284,7 @@ static int memory_block_offline(struct memory_block *mem)unsigned long start_pfn =section_nr_to_pfn (mem->start_section_nr);unsigned long nr_pages =PAGES_PER_SECTION * sections_per_block;unsigned long nr_vmemmap_pages =0 ; +struct zone *zone;
Why the temporary variable, and why not initialize it directly here? Note that
drivers/base/memory.c static int memory_block_offline(struct memory_block *mem) · line 290
Quoted 3 lines
drivers/base/memory.c +0
-0int ret;if (!mem->zone)
We already use mem->zone here. So if you add a variable, convert that one as well. But I guess we can just life without one.
Quoted 18 lines
drivers/base/memory.c +3
-0@@ -294,7 +297,9 @@ static int memory_block_offline(struct memory_block *mem)if (mem->altmap) nr_vmemmap_pages = mem->altmap->free; + zone = mem->zone;mem_hotplug_begin (); +clear_zone_contiguous (zone);if (nr_vmemmap_pages)adjust_present_page_count (pfn_to_page (start_pfn), mem->group, -nr_vmemmap_pages);@@ -314,6 +319,7 @@ static int memory_block_offline(struct memory_block *mem) mem->zone =NULL ; out: +set_zone_contiguous (zone);mem_hotplug_done ();return ret; }
[...]
drivers/base/memory.c
Quoted 6 lines
drivers/base/memory.c +6
-0+static inline void set_zone_contiguous (struct zone *zone) +{ +if (zone_is_zone_device (zone)) +return ; +if (zone->spanned_pages == zone->pages_with_online_memmap) + zone->contiguous = true;
Maybe it was already discussed (and I recall that we previously had that), but I think we really need READ_ONCE semantics here and WRITE_ONCE semantics in memory hotplug code. Otherwise concurrent updates could lead to weird things when the compiler does load-tearing.
[...]
drivers/base/memory.c
Quoted 9 lines
drivers/base/memory.c +8
-0+static void __init update_zone_online_memmap_pages (struct zone *zone, +unsigned long start_pfn, +unsigned long end_pfn, +unsigned long *hole_pfn) +{ +#ifdef CONFIG_SPARSEMEM_VMEMMAP +unsigned long zone_start_pfn = zone->zone_start_pfn; +unsigned long zone_end_pfn = zone_start_pfn + zone->spanned_pages;
These two can be const.
drivers/base/memory.c
Quoted 5 lines
drivers/base/memory.c +5
-0+unsigned long sub_start, sub_end; + + sub_start =max (ALIGN_DOWN (start_pfn,PAGES_PER_SUBSECTION ), + zone_start_pfn); + sub_end =min (ALIGN (end_pfn,PAGES_PER_SUBSECTION ), zone_end_pfn);
Hm, I don't immediately understand why we do the PAGES_PER_SUBSECTION thing here. Why is that required?
Signature · 3 lines
Cheers, David
Quoted 12 lines
-----Original Message-----
From: David Hildenbrand (Arm) <[email protected]>
Sent: Wednesday, August 5, 2026 7:53 PM
To: Liu, Yuan1 <[email protected]>; Oscar Salvador <[email protected]>; Mike Rapoport <[email protected]>; Wei Yang <[email protected]>
Cc: [email protected]; Zou, Nanhai <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; Zeng, Jason <[email protected]>; linux- [email protected]
Subject: Re: [PATCH v6 1/2]mm/memory_hotplug: optimize zone contiguous check when changing pfn range
[...]
Quoted 30 lines
Review
Quoted 2 lines
(quoted diff) +0
-0
b/Documentation/mm/physical_memory.rstDocumentation/mm/physical_memory.rst General · line 483
Quoted 5 lines
index
b76183545e5b..0aa65e6b5499100644Documentation/mm/physical_memory.rst +0
-0@@ -483,6 +483,19 @@ General ``present_pages`` should use ``get_online_mems()`` to get a stablevalue. It
Documentation/mm/physical_memory.rst General
Quoted 4 lines
Documentation/mm/physical_memory.rst +2
-0is initialized by ``calculate_node_totalpages()``. +``pages_with_online_memmap`` + Tracks pages within the zone that have an online memory map (presentpages
Documentation/mm/physical_memory.rst General · line 489
Quoted 2 lines
Documentation/mm/physical_memory.rst +2
-0+ and memory holes whose memory map has been initialized). When + ``spanned_pages`` == ``pages_with_online_memmap``, ``pfn_to_page()``can be
Documentation/mm/physical_memory.rst General
Quoted 3 lines
Documentation/mm/physical_memory.rst +3
-0+ performed without further checks on any PFN within the zone span. + + Note: this counter may temporarily undercount when pages with anonline
Documentation/mm/physical_memory.rst General · line 493
Quoted 1 line
Documentation/mm/physical_memory.rst +1
-0+ memory map exist outside the current zone span. This can only happenduring
Documentation/mm/physical_memory.rst General · line 494
Quoted 1 line
Documentation/mm/physical_memory.rst +1
-0+ boot, when initializing the memory map of pages that do not fall intoany
Documentation/mm/physical_memory.rst General · line 495
Quoted 1 line
Documentation/mm/physical_memory.rst +1
-0+ zone span. Growing the zone to cover such pages and later shrinkingit back
Documentation/mm/physical_memory.rst General · line 497
Quoted 2 lines
Documentation/mm/physical_memory.rst +2
-0+ may result in a "too small" value. This is safe: it merely prevents + detecting a contiguous zone.
Quoted 7 lines
It's suboptimal that we repeat the same comment that we already have in struct
zone. Can we just keep it vry simple here?"Pages within the zone that have an online memory map: present pages and memory
holes whose memory map has been initialized. See XXX for more details."
Thanks for pointing out, I will simplify to this short description.
Quoted 38 lines
Documentation/mm/physical_memory.rst General
Quoted 3 lines
Documentation/mm/physical_memory.rst +1
-0+ ``present_early_pages`` The present pages existing within the zone located on memoryavailable since
drivers/base/memory.c static int memory_block_online(struct memory_block
Quoted 6 lines
Documentation/mm/physical_memory.rst +0
-0early boot, excluding hotplugged memory. Defined only whendrivers/base/memory.c +0
-0@@ -246,6 +246,7 @@ static int memory_block_online(struct memory_block*mem)
drivers/base/memory.c static int memory_block_online(struct memory_block *mem)
Quoted 6 lines
drivers/base/memory.c +1
-0nr_vmemmap_pages = mem->altmap->free;mem_hotplug_begin (); +clear_zone_contiguous (zone);if (nr_vmemmap_pages) { ret = mhp_init_memmap_on_memory(start_pfn, nr_vmemmap_pages,zone);
drivers/base/memory.c static int memory_block_online(struct memory_block
Quoted 2 lines
drivers/base/memory.c +0
-0if (ret)@@ -270,6 +271,7 @@ static int memory_block_online(struct memory_block*mem)
drivers/base/memory.c static int memory_block_offline(struct memory_block
Quoted 8 lines
drivers/base/memory.c +1
-0mem->zone = zone; out: +set_zone_contiguous (zone);mem_hotplug_done ();return ret; }@@ -282,6 +284,7 @@ static int memory_block_offline(struct memory_block*mem)
drivers/base/memory.c static int memory_block_offline(struct memory_block *mem) · line 287
Quoted 4 lines
drivers/base/memory.c +1
-0unsigned long start_pfn =section_nr_to_pfn (mem->start_section_nr);unsigned long nr_pages =PAGES_PER_SECTION * sections_per_block;unsigned long nr_vmemmap_pages =0 ; +struct zone *zone;Why the temporary variable, and why not initialize it directly here? Note that
Quoted 7 lines
drivers/base/memory.c static int memory_block_offline(struct memory_block *mem) · line 290
Quoted 3 lines
drivers/base/memory.c +0
-0int ret;if (!mem->zone)We already use
mem->zonehere. So if you add a variable, convert that one as
well. But I guess we can just life without one.
Will remove the temporary variable in next version.
Quoted 23 lines
drivers/base/memory.c static int memory_block_offline(struct memory_block
Quoted 1 line
drivers/base/memory.c +0
-0@@ -294,7 +297,9 @@ static int memory_block_offline(struct memory_block*mem)
drivers/base/memory.c static int memory_block_offline(struct memory_block
Quoted 10 lines
drivers/base/memory.c +2
-0if (mem->altmap) nr_vmemmap_pages = mem->altmap->free; + zone = mem->zone;mem_hotplug_begin (); +clear_zone_contiguous (zone);if (nr_vmemmap_pages)adjust_present_page_count (pfn_to_page (start_pfn), mem->group, -nr_vmemmap_pages);@@ -314,6 +319,7 @@ static int memory_block_offline(struct memory_block*mem)
Quoted 7 lines
drivers/base/memory.c +1
-0mem->zone =NULL ; out: +set_zone_contiguous (zone);mem_hotplug_done ();return ret; }[...]
Quoted 14 lines
drivers/base/memory.c
Quoted 6 lines
drivers/base/memory.c +6
-0+static inline void set_zone_contiguous (struct zone *zone) +{ +if (zone_is_zone_device (zone)) +return ; +if (zone->spanned_pages == zone->pages_with_online_memmap) + zone->contiguous = true;Maybe it was already discussed (and I recall that we previously had that), but I
think we really needREAD_ONCEsemantics here andWRITE_ONCEsemantics in memory
hotplug code. Otherwise concurrent updates could lead to weird things when the
compiler does load-tearing.
Thanks for your guidance. I should have considered the READ_ONCE/WRITE_ONCE semantics here. I'll add them in the next Revision.
Quoted 13 lines
[...]
drivers/base/memory.c
Quoted 9 lines
drivers/base/memory.c +8
-0+static void __init update_zone_online_memmap_pages (struct zone *zone, +unsigned long start_pfn, +unsigned long end_pfn, +unsigned long *hole_pfn) +{ +#ifdef CONFIG_SPARSEMEM_VMEMMAP +unsigned long zone_start_pfn = zone->zone_start_pfn; +unsigned long zone_end_pfn = zone_start_pfn + zone->spanned_pages;These two can be const.
Will do.
Quoted 9 lines
drivers/base/memory.c
Quoted 5 lines
drivers/base/memory.c +5
-0+unsigned long sub_start, sub_end; + + sub_start =max (ALIGN_DOWN (start_pfn,PAGES_PER_SUBSECTION ), + zone_start_pfn); + sub_end =min (ALIGN (end_pfn,PAGES_PER_SUBSECTION ), zone_end_pfn);Hm, I don't immediately understand why we do the
PAGES_PER_SUBSECTIONthing
here. Why is that required?
pages_with_online_memmap counts all PFNs where pfn_to_online_page() is valid. With CONFIG_SPARSEMEM_VMEMMAP, pfn_section_valid() operates at PAGES_PER_SUBSECTION granularity — when any page in a subsection has memory, the entire subsection is valid/online. So we align to subsection boundaries to include hole pages within partially-populated subsections.
I can also add a comment in the code to make this intent clearer. /*
* With CONFIG_SPARSEMEM_VMEMMAP, pfn_section_valid() operates at
* PAGES_PER_SUBSECTION granularity, so align to subsection boundaries
* to include all PFNs for which pfn_to_online_page() is valid. */
Best Regards,
Liu, Yuan1
Quoted 3 lines
Cheers,
David
David Hildenbrand (Arm)
maintainerQuoted 24 lines
Quoted 11 lines
mm/mm_init.c void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone · line 926
Quoted 9 lines
mm/mm_init.c +8
-0+static void __init update_zone_online_memmap_pages (struct zone *zone, +unsigned long start_pfn, +unsigned long end_pfn, +unsigned long *hole_pfn) +{ +#ifdef CONFIG_SPARSEMEM_VMEMMAP +unsigned long zone_start_pfn = zone->zone_start_pfn; +unsigned long zone_end_pfn = zone_start_pfn + zone->spanned_pages;These two can be const.
Will do.
Quoted 9 lines
mm/mm_init.c void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone · line 931
Quoted 5 lines
mm/mm_init.c +5
-0+unsigned long sub_start, sub_end; + + sub_start =max (ALIGN_DOWN (start_pfn,PAGES_PER_SUBSECTION ), + zone_start_pfn); + sub_end =min (ALIGN (end_pfn,PAGES_PER_SUBSECTION ), zone_end_pfn);Hm, I don't immediately understand why we do the
PAGES_PER_SUBSECTIONthing
here. Why is that required?
Quoted 5 lines
pages_with_online_memmapcounts all PFNs wherepfn_to_online_page()is valid. WithCONFIG_SPARSEMEM_VMEMMAP,pfn_section_valid()operates atPAGES_PER_SUBSECTIONgranularity — when any page in a subsection has memory, the entire subsection is valid/online. So we align to subsection boundaries to include hole pages within partially-populated subsections.
But we must never account exceeding the zone range. So I don't understand why we would have to care about PAGES_PER_SUBSECTION here at all?
Signature · 3 lines
Cheers, David
Quoted 42 lines
-----Original Message-----
From: David Hildenbrand (Arm) <[email protected]>
Sent: Thursday, August 6, 2026 4:46 PM
To: Liu, Yuan1 <[email protected]>; Oscar Salvador <[email protected]>; Mike Rapoport <[email protected]>; Wei Yang <[email protected]>
Cc: [email protected]; Zou, Nanhai <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; Zeng, Jason <[email protected]>; linux- [email protected]
Subject: Re: [PATCH v6 1/2]mm/memory_hotplug: optimize zone contiguous check when changing pfn rangeQuoted 23 lines
Quoted 10 lines
mm/mm_init.c void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone · line 926
Quoted 8 lines
mm/mm_init.c +8
-0+static void __init update_zone_online_memmap_pages (struct zone *zone, +unsigned long start_pfn, +unsigned long end_pfn, +unsigned long *hole_pfn) +{ +#ifdef CONFIG_SPARSEMEM_VMEMMAP +unsigned long zone_start_pfn = zone->zone_start_pfn; +unsigned long zone_end_pfn = zone_start_pfn + zone->spanned_pages;These two can be const.
Will do.
Quoted 9 lines
mm/mm_init.c void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone · line 931
Quoted 5 lines
mm/mm_init.c +5
-0+unsigned long sub_start, sub_end; + + sub_start =max (ALIGN_DOWN (start_pfn,PAGES_PER_SUBSECTION ), + zone_start_pfn); + sub_end =min (ALIGN (end_pfn,PAGES_PER_SUBSECTION ), zone_end_pfn);Hm, I don't immediately understand why we do the
PAGES_PER_SUBSECTIONthing
here. Why is that required?Quoted 5 lines
pages_with_online_memmapcounts all PFNs wherepfn_to_online_page()is valid. WithCONFIG_SPARSEMEM_VMEMMAP,pfn_section_valid()operates atPAGES_PER_SUBSECTIONgranularity — when any page in a subsection has memory, the entire subsection is valid/online. So we align to subsection boundaries to include hole pages within partially-populated subsections.
Quoted 3 lines
But we must never account exceeding the zone range. So I don't understand why we
would have to care aboutPAGES_PER_SUBSECTIONhere at all?
We never account beyond the zone range, because sub_start and sub_end are still clamped to the zone boundaries after the alignment.
The subsection alignment is needed for hole PFNs between memblocks within the zone. These hole PFNs are valid for pfn_to_online_page(), because they sit in a subsection that has memory, so the whole subsection's memmap is online.
|----------- zone range -----------|
+-----------+---------+------------+
+ memblock 1| hole | memblock 2 |
+-----------+---------+------------+
^
|
|
subsection boundaryBest Regards,
Liu, Yuan
Quoted 4 lines
--
Cheers,David
David Hildenbrand (Arm)
maintainerQuoted 38 lines · On 8/6/26 11:52, Liu, Yuan1 wrote:
Quoted 23 lines
-----Original Message-----
From: David Hildenbrand (Arm) <[email protected]>
Sent: Thursday, August 6, 2026 4:46 PM
To: Liu, Yuan1 <[email protected]>; Oscar Salvador <[email protected]>; Mike Rapoport <[email protected]>; Wei Yang <[email protected]>
Cc: [email protected]; Zou, Nanhai <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; Zeng, Jason <[email protected]>; linux- [email protected]
Subject: Re: [PATCH v6 1/2]mm/memory_hotplug: optimize zone contiguous check when changing pfn rangeQuoted 9 lines
Will do.
pages_with_online_memmapcounts all PFNs wherepfn_to_online_page()is valid. WithCONFIG_SPARSEMEM_VMEMMAP,pfn_section_valid()operates atPAGES_PER_SUBSECTIONgranularity — when any page in a subsection has memory, the entire subsection is valid/online. So we align to subsection boundaries to include hole pages within partially-populated subsections.Quoted 3 lines
But we must never account exceeding the zone range. So I don't understand why we
would have to care aboutPAGES_PER_SUBSECTIONhere at all?We never account beyond the zone range, because
sub_startandsub_endare still clamped to the zone boundaries after the alignment.The subsection alignment is needed for hole PFNs between memblocks within the zone. These hole PFNs are valid for
pfn_to_online_page(), because they sit in a subsection that has memory, so the whole subsection's memmap is online.|----------- zone range -----------|
Quoted 7 lines
+-----------+---------+------------+
+ memblock 1| hole | memblock 2 |
+-----------+---------+------------+
^
|
|
subsection boundaryRight, but init_unavailable_range() can just return how many were actually initalized? Why can't we piggy-back on that?
I think we had something similar previously, why can't we use that?
We know the zone span, so we can just account all the mmap in the zone span that we initialize.
What is the problem with that?
Signature · 3 lines
Cheers, David
Quoted 66 lines
-----Original Message-----
From: David Hildenbrand (Arm) <[email protected]>
Sent: Friday, August 7, 2026 7:30 PM
To: Liu, Yuan1 <[email protected]>; Oscar Salvador <[email protected]>; Mike Rapoport <[email protected]>; Wei Yang <[email protected]>
Cc: [email protected]; Zou, Nanhai <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; Zeng, Jason <[email protected]>; linux- [email protected]
Subject: Re: [PATCH v6 1/2]mm/memory_hotplug: optimize zone contiguous check when changing pfn rangeQuoted 4 lines · On 8/6/26 11:52, Liu, Yuan1 wrote:
Quoted 4 lines
-----Original Message-----
From: David Hildenbrand (Arm) <[email protected]>
Sent: Thursday, August 6, 2026 4:46 PM
To: Liu, Yuan1 <[email protected]>; Oscar SalvadorQuoted 10 lines
Quoted 10 lines
Mike Rapoport <[email protected]>; Wei Yang <[email protected]>
Cc: [email protected]; Zou, Nanhai <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; Zeng, Jason <[email protected]>; linux- [email protected]
Subject: Re: [PATCH v6 1/2]mm/memory_hotplug: optimize zone contiguous check when changing pfn rangeQuoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Will do.
pages_with_online_memmapcounts all PFNs wherepfn_to_online_page()is valid. WithCONFIG_SPARSEMEM_VMEMMAP,pfn_section_valid()operates atPAGES_PER_SUBSECTIONgranularity — when any page in a subsection has memory, the entire subsection is valid/online. So we align tosubsection
Quoted 1 line
Quoted 1 line
Quoted 1 line
boundaries to include hole pages within partially-populated
subsections.
Quoted 2 lines
Quoted 2 lines
But we must never account exceeding the zone range. So I don't
understand
Quoted 14 lines
Quoted 2 lines
why we
would have to care aboutPAGES_PER_SUBSECTIONhere at all?We never account beyond the zone range, because
sub_startandsub_endare still clamped to the zone boundaries after the alignment.The subsection alignment is needed for hole PFNs between memblocks within the zone. These hole PFNs are valid for
pfn_to_online_page(), because they sit in a subsection that has memory, so the whole subsection's memmap is online.|----------- zone range -----------|
Quoted 7 lines
+-----------+---------+------------+ + memblock 1| hole | memblock 2 | +-----------+---------+------------+ ^ | | subsection boundaryRight, but
init_unavailable_range()can just return how many were actually initalized? Why can't we piggy-back on that?
Quoted 7 lines
I think we had something similar previously, why can't we use that?
We know the zone span, so we can just account all the mmap in the zone span
that we initialize.What is the problem with that?
Hi David
My understanding is that init_unavailable_range() initializes all PFNs that satisfy pfn_valid(), but not all of them satisfy pfn_to_online_page(), since some PFNs belong to subsections that are not online.
You previously mentioned:
pfn_valid() says early sections always have a full memmap, so even invalid subsections have a memmap. pfn_to_online_page() says an invalid subsection cannot be online and its content must be stale. for_each_valid_pfn() follows pfn_valid() semantics, and we use it to initialize memmap that is not going to be online and account it as pages_with_online_memmap, which is wrong.
The cleanest approach is to avoid allocating memmap for subsections, which also removes the special early-section handling from pfn_valid() and for_each_valid_pfn().
I also share the concerns raised by Sashiko in the analysis below [1]:
Scanners like isolate_migratepages_block() will then blindly iterate through the pageblock and access the completely uninitialized struct pages of the hole, leading to functional errors or kernel panics when reading these zero-filled structures via macros like PageHuge() or page_zone().
That's why we went with the current approach in v6 instead of your earlier suggestion. I'd really appreciate your guidance on which direction you think would be more appropriate.
[1] https://sashiko.dev/#/patchset/20260520093457.3719960-1-yuan1.liu%40intel.com
Best Regards,
Liu, Yuan
Quoted 3 lines
Cheers,
David
Quoted 100 lines · On Fri, Aug 07, 2026 at 12:15:42PM +0000, Liu, Yuan1 wrote:
Quoted 66 lines
-----Original Message-----
From: David Hildenbrand (Arm) <[email protected]>
Sent: Friday, August 7, 2026 7:30 PM
To: Liu, Yuan1 <[email protected]>; Oscar Salvador <[email protected]>; Mike Rapoport <[email protected]>; Wei Yang <[email protected]>
Cc: [email protected]; Zou, Nanhai <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; Zeng, Jason <[email protected]>; linux- [email protected]
Subject: Re: [PATCH v6 1/2]mm/memory_hotplug: optimize zone contiguous check when changing pfn rangeQuoted 4 lines · On 8/6/26 11:52, Liu, Yuan1 wrote:
Quoted 4 lines
-----Original Message-----
From: David Hildenbrand (Arm) <[email protected]>
Sent: Thursday, August 6, 2026 4:46 PM
To: Liu, Yuan1 <[email protected]>; Oscar SalvadorQuoted 10 lines
Quoted 10 lines
Mike Rapoport <[email protected]>; Wei Yang <[email protected]>
Cc: [email protected]; Zou, Nanhai <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; Zeng, Jason <[email protected]>; linux- [email protected]
Subject: Re: [PATCH v6 1/2]mm/memory_hotplug: optimize zone contiguous check when changing pfn rangeQuoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Will do.
pages_with_online_memmapcounts all PFNs wherepfn_to_online_page()is valid. WithCONFIG_SPARSEMEM_VMEMMAP,pfn_section_valid()operates atPAGES_PER_SUBSECTIONgranularity — when any page in a subsection has memory, the entire subsection is valid/online. So we align tosubsection
Quoted 1 line
Quoted 1 line
Quoted 1 line
boundaries to include hole pages within partially-populated
subsections.
Quoted 2 lines
Quoted 2 lines
But we must never account exceeding the zone range. So I don't
understand
Quoted 14 lines
Quoted 2 lines
why we
would have to care aboutPAGES_PER_SUBSECTIONhere at all?We never account beyond the zone range, because
sub_startandsub_endare still clamped to the zone boundaries after the alignment.The subsection alignment is needed for hole PFNs between memblocks within the zone. These hole PFNs are valid for
pfn_to_online_page(), because they sit in a subsection that has memory, so the whole subsection's memmap is online.|----------- zone range -----------|
Quoted 7 lines
+-----------+---------+------------+ + memblock 1| hole | memblock 2 | +-----------+---------+------------+ ^ | | subsection boundaryRight, but
init_unavailable_range()can just return how many were actually initalized? Why can't we piggy-back on that?Quoted 7 lines
I think we had something similar previously, why can't we use that?
We know the zone span, so we can just account all the mmap in the zone span
that we initialize.What is the problem with that?
Hi David
My understanding is that
init_unavailable_range()initializes all PFNs that satisfypfn_valid(), but not all of them satisfypfn_to_online_page(), since some PFNs belong to subsections that are not online.You previously mentioned:
pfn_valid()says early sections always have a full memmap, so even invalid subsections have a memmap.pfn_to_online_page()says an invalid subsection cannot be online and its content must be stale.for_each_valid_pfn()followspfn_valid()semantics, and we use it to initialize memmap that is not going to be online and account it aspages_with_online_memmap, which is wrong.The cleanest approach is to avoid allocating memmap for subsections, which also removes the special early-section handling from
pfn_valid()andfor_each_valid_pfn().I also share the concerns raised by Sashiko in the analysis below [1]:
Scanners like
isolate_migratepages_block()will then blindly iterate through the pageblock and access the completely uninitialized struct pages of the hole, leading to functional errors or kernel panics when reading these zero-filled structures via macros likePageHuge()orpage_zone().
Quoted 6 lines
That's why we went with the current approach in v6 instead of your earlier suggestion. I'd really appreciate your guidance on which direction you think would be more appropriate.
[1] https://sashiko.dev/#/patchset/20260520093457.3719960-1-yuan1.liu%40intel.com
Hi, Yuan & David
One thing I found is __pageblock_pfn_to_page() is ambiguous when pageblock contains 2 subsections.
The subsection size is fixed, which is 2^21=2M. But pageblock is not fixed size. If !CONFIG_HUGETLB_PAGE && !CONFIG_TRANSPARENT_HUGEPAGE, default pageblock size is 4K * 2 ^ 10 = 4M, which contains 2 subsection.
For a pageblock in early section, __paeblock_pfn_to_page() result is different when the hole is at the beginning or at the end.
Like what I did in [1], I punch a hole in Zone Normal with 2M size.
If the hole is at the beginning of the pageblock, the zone is non-contiguous.
[ 0.004079] memory[0x2] [0x0000000100000000-0x000000013fffffff], 0x0000000040000000 bytes on node 0 flags: 0x0 [ 0.004080] memory[0x3] [0x0000000140200000-0x00000001bfffffff], 0x000000007fe00000 bytes on node 0 flags: 0x0
If the hole is at the end of the pageblock, the zone is contiguous.
[ 0.004068] memory[0x2] [0x0000000100000000-0x00000001401fffff], 0x0000000040200000 bytes on node 0 flags: 0x0 [ 0.004068] memory[0x3] [0x0000000140400000-0x00000001bfffffff], 0x000000007fc00000 bytes on node 0 flags: 0x0
The reason is in __pageblock_pfn_to_page() we have different requirement for start_pfn and end_pfn.
Hmm... and things would be complicated as pageblock is PAGE based but subsection is size based. If we have large PAGE_SIZE...
Also when pageblock_pfn_to_page() is introduced in commit 7d49d8868336 ("mm,
compaction: reduce zone checking frequency in the migration scanner"), we assume there won't be [node0 node1 node0] interleave within a single pageblock. Would this still stand for large PAGE_SIZE?
[1]: https://lore.kernel.org/all/[email protected]/T/#u
Quoted 6 lines
Best Regards,
Liu, YuanQuoted 3 lines
Cheers,
David
Signature · 2 lines
Wei Yang Help you, Help me
David Hildenbrand (Arm)
maintainerQuoted 7 lines
Hi David
My understanding is that
init_unavailable_range()initializes all PFNs that satisfypfn_valid(), but not all of them satisfypfn_to_online_page(), since some PFNs belong to subsections that are not online.
Thanks for reminding me. I think the right direction is to finally clean up the pfn_valid() handling.
Quoted 15 lines
You previously mentioned:
pfn_valid()says early sections always have a full memmap, so even invalid subsections have a memmap.pfn_to_online_page()says an invalid subsection cannot be online and its content must be stale.for_each_valid_pfn()followspfn_valid()semantics, and we use it to initialize memmap that is not going to be online and account it aspages_with_online_memmap, which is wrong.The cleanest approach is to avoid allocating memmap for subsections, which also removes the special early-section handling from
pfn_valid()andfor_each_valid_pfn().I also share the concerns raised by Sashiko in the analysis below [1]:
Quoted 8 lines
Scanners like
isolate_migratepages_block()will then blindly iterate through the pageblock and access the completely uninitialized struct pages of the hole, leading to functional errors or kernel panics when reading these zero-filled structures via macros likePageHuge()orpage_zone().That's why we went with the current approach in v6 instead of your earlier suggestion. I'd really appreciate your guidance on which direction you think would be more appropriate.
Let me take a stab at just having pfn_valid() / for_each_valid_pfn() respecting the subsection map.
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerQuoted 31 lines · David Hildenbrand (Arm)
Quoted 7 lines
Hi David
My understanding is that
init_unavailable_range()initializes all PFNs that satisfypfn_valid(), but not all of them satisfypfn_to_online_page(), since some PFNs belong to subsections that are not online.Thanks for reminding me. I think the right direction is to finally clean up the
pfn_valid()handling.Quoted 20 lines
You previously mentioned:
pfn_valid()says early sections always have a full memmap, so even invalid subsections have a memmap.pfn_to_online_page()says an invalid subsection cannot be online and its content must be stale.for_each_valid_pfn()followspfn_valid()semantics, and we use it to initialize memmap that is not going to be online and account it aspages_with_online_memmap, which is wrong.The cleanest approach is to avoid allocating memmap for subsections, which also removes the special early-section handling from
pfn_valid()andfor_each_valid_pfn().I also share the concerns raised by Sashiko in the analysis below [1]:
Scanners like
isolate_migratepages_block()will then blindly iterate through the pageblock and access the completely uninitialized struct pages of the hole, leading to functional errors or kernel panics when reading these zero-filled structures via macros likePageHuge()orpage_zone().
Quoted 5 lines
Quoted 3 lines
That's why we went with the current approach in v6 instead of your earlier suggestion. I'd really appreciate your guidance on which direction you think would be more appropriate.
Let me take a stab at just having
pfn_valid()/for_each_valid_pfn()respecting the subsection map.
... and that turns complicated very quickly. The problem is that we have some users, in particular the buddy, that just assumes that MAX_PAGE_ORDER regions are fully accessible.
The fun begins once we have MAX_PAGE_ORDER span multiple subsections. So we'd actually want to initialize the memmap.
The pfn_valid() vs. pfn_to_online_page() inconsistency is really nasty :(
I mean, in init_unavailable_range() we could actually figure out fairly easily whether we are dealing with holes where pfn_to_online_page() would succeed.
mm/mm_init.c +3 -1
@@ -843,11 +843,13 @@ static void __init init_unavailable_range(unsigned long spfn, int zone, int node) {unsigned long pfn; -u64 pgcnt =0 ; +u64 pgcnt =0 , online_pgcnt =0 ;for_each_valid_pfn (pfn, spfn, epfn) {__init_single_page (pfn_to_page (pfn), pfn, zone, node);__SetPageReserved (pfn_to_page (pfn)); +if (pfn_to_online_page (pfn)) + online_pgcnt++; pgcnt++; }
If it's a problem performance-wise, we can always try optimizing by skipping checks within the same (sub)section.
Signature · 3 lines
Cheers, David
Quoted 98 lines
-----Original Message-----
From: David Hildenbrand (Arm) <[email protected]>
Sent: Tuesday, August 11, 2026 8:23 PM
To: Liu, Yuan1 <[email protected]>; Oscar Salvador <[email protected]>; Mike Rapoport <[email protected]>; Wei Yang <[email protected]>
Cc: [email protected]; Zou, Nanhai <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; Zeng, Jason <[email protected]>; linux- [email protected]
Subject: Re: [PATCH v6 1/2]mm/memory_hotplug: optimize zone contiguous check when changing pfn rangeQuoted 9 lines · David Hildenbrand (Arm)
Quoted 7 lines
Hi David
My understanding is that
init_unavailable_range()initializes all PFNs that satisfypfn_valid(), but not all of them satisfypfn_to_online_page(), since some PFNs belong to subsections that are not online.Thanks for reminding me. I think the right direction is to finally clean
up the
Quoted 6 lines
pfn_valid()handling.Quoted 4 lines
You previously mentioned:
pfn_valid()says early sections always have a full memmap, so eveninvalid
Quoted 1 line
Quoted 1 line
subsections have a memmap.
pfn_to_online_page()says an invalidsubsection
Quoted 1 line
Quoted 1 line
cannot be online and its content must be stale.
for_each_valid_pfn()follows
Quoted 1 line
Quoted 1 line
pfn_valid()semantics, and we use it to initialize memmap that is notgoing
Quoted 1 line
Quoted 1 line
to be online and account it as
pages_with_online_memmap, which iswrong.
Quoted 2 lines
Quoted 2 lines
The cleanest approach is to avoid allocating memmap for subsections,
which
Quoted 6 lines
Quoted 6 lines
also removes the special early-section handling from
pfn_valid()andfor_each_valid_pfn().I also share the concerns raised by Sashiko in the analysis below [1]:
Scanners like
isolate_migratepages_block()will then blindly iteratethrough
Quoted 1 line
Quoted 1 line
the pageblock and access the completely uninitialized struct pages of
the hole,
Quoted 1 line
Quoted 1 line
leading to functional errors or kernel panics when reading these
zero-filled
Quoted 6 lines
Quoted 5 lines
structures via macros like
PageHuge()orpage_zone().That's why we went with the current approach in v6 instead of your earlier suggestion. I'd really appreciate your guidance on which direction you think would be more appropriate.
Let me take a stab at just having
pfn_valid()/for_each_valid_pfn()respecting
Quoted 1 line
the subsection map.
... and that turns complicated very quickly. The problem is that we have some users,
in particular the buddy, that just assumes thatMAX_PAGE_ORDERregions are fully
accessible.The fun begins once we have
MAX_PAGE_ORDERspan multiple subsections. So we'd actually
want to initialize the memmap.The
pfn_valid()vs.pfn_to_online_page()inconsistency is really nasty :(I mean, in
init_unavailable_range()we could actually figure out fairly easily
whether we are dealing with holes wherepfn_to_online_page()would succeed.mm/mm_init.c +3
-1@@ -843,11 +843,13 @@ static void __init init_unavailable_range(unsignedlong spfn, int zone, int node) { unsigned long pfn; -u64 pgcnt =0 ; +u64 pgcnt =0 , online_pgcnt =0 ;for_each_valid_pfn (pfn, spfn, epfn) {__init_single_page (pfn_to_page (pfn), pfn, zone, node);__SetPageReserved (pfn_to_page (pfn)); +if (pfn_to_online_page (pfn)) + online_pgcnt++; pgcnt++; }
Quoted 3 lines
If it's a problem performance-wise, we can always try optimizing by skipping
checks within the same (sub)section.
Hi David
What about the following approach to skipping the check within the same (sub)section?
@@ -827,11 +827,21 @@ static void __init init_unavailable_range(unsigned long spfn,
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) {
+ unsigned long subsection = pfn & PAGE_SUBSECTION_MASK;
+
__init_single_page(pfn_to_page(pfn), pfn, zone, node);
__SetPageReserved(pfn_to_page(pfn));+ if (subsection != last_subsection) {
+ is_online = !!pfn_to_online_page(pfn);
+ last_subsection = subsection;
+ }
+ if (is_online)
+ online_pgcnt++;
pgcnt++;If this looks good, let me prepare and send out the next version series.
Best Regards,
Liu, Yuan
Quoted 4 lines
--
Cheers,David
David Hildenbrand (Arm)
maintainerQuoted 99 lines · On 8/12/26 11:17, Liu, Yuan1 wrote:
Quoted 68 lines
-----Original Message-----
From: David Hildenbrand (Arm) <[email protected]>
Sent: Tuesday, August 11, 2026 8:23 PM
To: Liu, Yuan1 <[email protected]>; Oscar Salvador <[email protected]>; Mike Rapoport <[email protected]>; Wei Yang <[email protected]>
Cc: [email protected]; Zou, Nanhai <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; Zeng, Jason <[email protected]>; linux- [email protected]
Subject: Re: [PATCH v6 1/2]mm/memory_hotplug: optimize zone contiguous check when changing pfn rangeQuoted 2 lines · David Hildenbrand (Arm)
Thanks for reminding me. I think the right direction is to finally clean
up the
Quoted 2 lines
pfn_valid()handling.invalid
subsection
follows
going
wrong.
which
through
the hole,
zero-filledQuoted 1 line
Let me take a stab at just having
pfn_valid()/for_each_valid_pfn()respecting
Quoted 1 line
the subsection map.
... and that turns complicated very quickly. The problem is that we have some users,
in particular the buddy, that just assumes thatMAX_PAGE_ORDERregions are fully
accessible.The fun begins once we have
MAX_PAGE_ORDERspan multiple subsections. So we'd actually
want to initialize the memmap.The
pfn_valid()vs.pfn_to_online_page()inconsistency is really nasty :(I mean, in
init_unavailable_range()we could actually figure out fairly easily
whether we are dealing with holes wherepfn_to_online_page()would succeed.mm/mm_init.c +3
-1@@ -843,11 +843,13 @@ static void __init init_unavailable_range(unsignedlong spfn, int zone, int node) { unsigned long pfn; -u64 pgcnt =0 ; +u64 pgcnt =0 , online_pgcnt =0 ;for_each_valid_pfn (pfn, spfn, epfn) {__init_single_page (pfn_to_page (pfn), pfn, zone, node);__SetPageReserved (pfn_to_page (pfn)); +if (pfn_to_online_page (pfn)) + online_pgcnt++; pgcnt++; }Quoted 3 lines
If it's a problem performance-wise, we can always try optimizing by skipping
checks within the same (sub)section.Hi David
What about the following approach to skipping the check within the same (sub)section?
mm/mm_init.c +11
-1@@ -827,11 +827,21 @@ static void __init init_unavailable_range(unsigned long spfn, 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) { +unsigned long subsection = pfn &PAGE_SUBSECTION_MASK ; +__init_single_page (pfn_to_page (pfn), pfn, zone, node);__SetPageReserved (pfn_to_page (pfn)); +if (subsection != last_subsection) { + is_online = !!pfn_to_online_page (pfn); + last_subsection = subsection; + } +if (is_online) + online_pgcnt++; pgcnt++;
Quoted 1 line
If this looks good, let me prepare and send out the next version series.
Yeah, something like that should do.
Signature · 3 lines
Cheers, David
Quoted 38 lines · David Hildenbrand (Arm)
Quoted 31 lines · David Hildenbrand (Arm)
Quoted 7 lines
Hi David
My understanding is that
init_unavailable_range()initializes all PFNs that satisfypfn_valid(), but not all of them satisfypfn_to_online_page(), since some PFNs belong to subsections that are not online.Thanks for reminding me. I think the right direction is to finally clean up the
pfn_valid()handling.Quoted 20 lines
You previously mentioned:
pfn_valid()says early sections always have a full memmap, so even invalid subsections have a memmap.pfn_to_online_page()says an invalid subsection cannot be online and its content must be stale.for_each_valid_pfn()followspfn_valid()semantics, and we use it to initialize memmap that is not going to be online and account it aspages_with_online_memmap, which is wrong.The cleanest approach is to avoid allocating memmap for subsections, which also removes the special early-section handling from
pfn_valid()andfor_each_valid_pfn().I also share the concerns raised by Sashiko in the analysis below [1]:
Scanners like
isolate_migratepages_block()will then blindly iterate through the pageblock and access the completely uninitialized struct pages of the hole, leading to functional errors or kernel panics when reading these zero-filled structures via macros likePageHuge()orpage_zone().Quoted 5 lines
Quoted 3 lines
That's why we went with the current approach in v6 instead of your earlier suggestion. I'd really appreciate your guidance on which direction you think would be more appropriate.
Let me take a stab at just having
pfn_valid()/for_each_valid_pfn()respecting the subsection map.
Quoted 9 lines
... and that turns complicated very quickly. The problem is that we have some users, in particular the buddy, that just assumes that
MAX_PAGE_ORDERregions are fully accessible.The fun begins once we have
MAX_PAGE_ORDERspan multiple subsections. So we'd actually want to initialize the memmap.The
pfn_valid()vs.pfn_to_online_page()inconsistency is really nasty :(
One thing I'd like to confirm.
pfn_to_online_page() is expected to return an "online page", which is in buddy, right?
Quoted 9 lines
I mean, in
init_unavailable_range()we could actually figure out fairly easily whether we are dealing with holes wherepfn_to_online_page()would succeed.mm/mm_init.c +0
-0@@ -843,11 +843,13 @@ static void __init init_unavailable_range(unsigned long spfn, int zone, int node)
mm/mm_init.c static void __init init_unavailable_range(unsigned long spfn, · line 852
Quoted 10 lines
mm/mm_init.c +3
-1{unsigned long pfn; -u64 pgcnt =0 ; +u64 pgcnt =0 , online_pgcnt =0 ;for_each_valid_pfn (pfn, spfn, epfn) {__init_single_page (pfn_to_page (pfn), pfn, zone, node);__SetPageReserved (pfn_to_page (pfn)); +if (pfn_to_online_page (pfn)) + online_pgcnt++;
But a hole in early section could still return a valid page if the hole is less than a subsection.
Is this an expected behavior?
Quoted 11 lines
mm/mm_init.c +0
-0pgcnt++; }If it's a problem performance-wise, we can always try optimizing by skipping checks within the same (sub)section.
Signature · 3 lines
Cheers, David
Signature · 2 lines
Wei Yang Help you, Help me
David Hildenbrand (Arm)
maintainerQuoted 47 lines · Wei Yang
Quoted 8 lines · David Hildenbrand (Arm)
Quoted 6 lines · David Hildenbrand (Arm)
Thanks for reminding me. I think the right direction is to finally clean up the
pfn_valid()handling.Let me take a stab at just having
pfn_valid()/for_each_valid_pfn()respecting the subsection map.Quoted 9 lines
... and that turns complicated very quickly. The problem is that we have some users, in particular the buddy, that just assumes that
MAX_PAGE_ORDERregions are fully accessible.The fun begins once we have
MAX_PAGE_ORDERspan multiple subsections. So we'd actually want to initialize the memmap.The
pfn_valid()vs.pfn_to_online_page()inconsistency is really nasty :(One thing I'd like to confirm.
pfn_to_online_page()is expected to return an "online page", which is in buddy, right?Quoted 9 lines
I mean, in
init_unavailable_range()we could actually figure out fairly easily whether we are dealing with holes wherepfn_to_online_page()would succeed.mm/mm_init.c +0
-0@@ -843,11 +843,13 @@ static void __init init_unavailable_range(unsigned long spfn, int zone, int node)mm/mm_init.c static void __init init_unavailable_range(unsigned long spfn, · line 852
Quoted 10 lines
mm/mm_init.c +3
-1{unsigned long pfn; -u64 pgcnt =0 ; +u64 pgcnt =0 , online_pgcnt =0 ;for_each_valid_pfn (pfn, spfn, epfn) {__init_single_page (pfn_to_page (pfn), pfn, zone, node);__SetPageReserved (pfn_to_page (pfn)); +if (pfn_to_online_page (pfn)) + online_pgcnt++;But a hole in early section could still return a valid page if the hole is less than a subsection.
Quoted 1 line
Is this an expected behavior?
Essentially, pfn_valid() is always true for early sections.
pfn_to_online_page() is only true in only section only for parts marked in the subsection map.
So if you have a hole that covers a subsection, it is not marked in the subsection map, pfn_valid() would be true and for_each_valid_pfn() would iterate it, whereby pfn_to_online_page() will reject it.
Zone shrinking will later simply skip that hole, because !pfn_to_online_page().
Signature · 3 lines
Cheers, David
PATCH v6 2/2 mm/memory_hotplug: improve shrink_zone_span() subsection boundary checks
In reply to Yuan LiuWhen shrinking a zone span after removing a PFN range, find_smallest_section_pfn() and find_biggest_section_pfn() only checked one edge PFN in each subsection for nid/zone matching.
If a memory or hole boundary falls in the middle of a subsection, that edge PFN may belong to a different nid/zone, causing the helpers to miss a valid PFN within that subsection.
Fix this by checking both subsection edge PFNs for nid/zone matching. Keep a single pfn_to_online_page() check per subsection, since online state is the same for all PFNs in a subsection.
--- mm/memory_hotplug.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-)
mm/memory_hotplug.c +27 -15
@@ -427,17 +427,24 @@ static unsigned long find_smallest_section_pfn(int nid, struct zone *zone, unsigned long start_pfn, unsigned long end_pfn) { -for (; start_pfn < end_pfn; start_pfn +=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (start_pfn))) -continue ; +unsigned long next_pfn; -if (unlikely (pfn_to_nid (start_pfn) != nid)) -continue ; +for (; start_pfn < end_pfn; start_pfn = next_pfn) { +unsigned long tail_pfn; -if (zone !=page_zone (pfn_to_page (start_pfn))) + next_pfn = start_pfn +PAGES_PER_SUBSECTION ; + tail_pfn = next_pfn -1 ; + +if (unlikely (!pfn_to_online_page (start_pfn)))continue ; -return start_pfn; +if (likely (pfn_to_nid (start_pfn) == nid) && + zone ==page_zone (pfn_to_page (start_pfn))) +return start_pfn; + +if (likely (pfn_to_nid (tail_pfn) == nid) && + zone ==page_zone (pfn_to_page (tail_pfn))) +return start_pfn; }return 0 ;
@@ -448,21 +455,26 @@ static unsigned long find_biggest_section_pfn(int nid, struct zone *zone, unsigned long start_pfn, unsigned long end_pfn) { -unsigned long pfn; +unsigned long pfn, prev_pfn;/* pfn is the end pfn of a memory section. */ pfn = end_pfn -1 ; -for (; pfn >= start_pfn; pfn -=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (pfn))) -continue ; +for (; pfn >= start_pfn; pfn = prev_pfn) { +unsigned long head_pfn; -if (unlikely (pfn_to_nid (pfn) != nid)) -continue ; + prev_pfn = pfn -PAGES_PER_SUBSECTION ; + head_pfn = prev_pfn +1 ; -if (zone !=page_zone (pfn_to_page (pfn))) +if (unlikely (!pfn_to_online_page (pfn)))continue ; -return pfn; +if (likely (pfn_to_nid (pfn) == nid) && + zone ==page_zone (pfn_to_page (pfn))) +return pfn; + +if (likely (pfn_to_nid (head_pfn) == nid) && + zone ==page_zone (pfn_to_page (head_pfn))) +return pfn; }return 0 ;
Signature · 1 line
2.47.3
Re: [PATCH v6 2/2] mm/memory_hotplug: improve shrink_zone_span() subsection boundary checks
Quoted 41 lines · Yuan Liu
When shrinking a zone span after removing a PFN range,
find_smallest_section_pfn()andfind_biggest_section_pfn()only checked one edge PFN in each subsection for nid/zone matching.If a memory or hole boundary falls in the middle of a subsection, that edge PFN may belong to a different nid/zone, causing the helpers to miss a valid PFN within that subsection.
Fix this by checking both subsection edge PFNs for nid/zone matching. Keep a single
pfn_to_online_page()check per subsection, since online state is the same for all PFNs in a subsection.Reviewed-by Jason Zeng <[email protected]>Signed-off-by Yuan Liu <[email protected]>---
mm/memory_hotplug.c| 42 +++++++++++++++++++++++++++---------------1 file changed, 27 insertions(+), 15 deletions(-)mm/memory_hotplug.c +7
-6@@ -427,17 +427,24 @@ static unsigned long find_smallest_section_pfn(int nid, struct zone *zone, unsigned long start_pfn, unsigned long end_pfn) { -for (; start_pfn < end_pfn; start_pfn +=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (start_pfn))) -continue ; +unsigned long next_pfn; -if (unlikely (pfn_to_nid (start_pfn) != nid)) -continue ; +for (; start_pfn < end_pfn; start_pfn = next_pfn) { +unsigned long tail_pfn; -if (zone !=page_zone (pfn_to_page (start_pfn))) + next_pfn = start_pfn +PAGES_PER_SUBSECTION ; + tail_pfn = next_pfn -1 ; + +if (unlikely (!pfn_to_online_page (start_pfn)))
mm/memory_hotplug.c static unsigned long find_smallest_section_pfn(int nid, struct zone *zone, · line 447
Quoted 10 lines
mm/memory_hotplug.c +7
-1continue ; -return start_pfn; +if (likely (pfn_to_nid (start_pfn) == nid) && + zone ==page_zone (pfn_to_page (start_pfn))) +return start_pfn; + +if (likely (pfn_to_nid (tail_pfn) == nid) && + zone ==page_zone (pfn_to_page (tail_pfn))) +return start_pfn;
Here we are checking range [start_pfn, tail_pfn]. When we come here, it means start_pfn's nid or zone doesn't match our expectation. But if tail_pfn does, why it still return start_pfn?
Quoted 40 lines
mm/memory_hotplug.c +13
-8}return 0 ;@@ -448,21 +455,26 @@ static unsigned long find_biggest_section_pfn(int nid, struct zone *zone, unsigned long start_pfn, unsigned long end_pfn) { -unsigned long pfn; +unsigned long pfn, prev_pfn;/* pfn is the end pfn of a memory section. */ pfn = end_pfn -1 ; -for (; pfn >= start_pfn; pfn -=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (pfn))) -continue ; +for (; pfn >= start_pfn; pfn = prev_pfn) { +unsigned long head_pfn; -if (unlikely (pfn_to_nid (pfn) != nid)) -continue ; + prev_pfn = pfn -PAGES_PER_SUBSECTION ; + head_pfn = prev_pfn +1 ; -if (zone !=page_zone (pfn_to_page (pfn))) +if (unlikely (!pfn_to_online_page (pfn)))continue ; -return pfn; +if (likely (pfn_to_nid (pfn) == nid) && + zone ==page_zone (pfn_to_page (pfn))) +return pfn; + +if (likely (pfn_to_nid (head_pfn) == nid) && + zone ==page_zone (pfn_to_page (head_pfn))) +return pfn; }return 0 ;Signature · 1 line
2.47.3
Signature · 2 lines
Wei Yang Help you, Help me
RE: [PATCH v6 2/2] mm/memory_hotplug: improve shrink_zone_span() subsection boundary checks
Quoted 58 lines
-----Original Message-----
From: Wei Yang <[email protected]>
Sent: Saturday, July 25, 2026 10:50 AM
To: Liu, Yuan1 <[email protected]>
Cc: David Hildenbrand <[email protected]>; Oscar Salvador <[email protected]>; Mike Rapoport <[email protected]>; Wei Yang <[email protected]>; [email protected]; Zou, Nanhai <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; Zeng, Jason <[email protected]>; [email protected]
Subject: Re: [PATCH v6 2/2]mm/memory_hotplug: improveshrink_zone_span()subsection boundary checksmm/memory_hotplug.c static unsigned long find_smallest_section_pfn(int
Quoted 23 lines · Yuan Liu
When shrinking a zone span after removing a PFN range,
find_smallest_section_pfn()andfind_biggest_section_pfn()only checked one edge PFN in each subsection for nid/zone matching.If a memory or hole boundary falls in the middle of a subsection, that edge PFN may belong to a different nid/zone, causing the helpers to miss a valid PFN within that subsection.
Fix this by checking both subsection edge PFNs for nid/zone matching. Keep a single
pfn_to_online_page()check per subsection, since online state is the same for all PFNs in a subsection.Reviewed-by Jason Zeng <[email protected]>Signed-off-by Yuan Liu <[email protected]>---
mm/memory_hotplug.c| 42 +++++++++++++++++++++++++++---------------1 file changed, 27 insertions(+), 15 deletions(-)mm/memory_hotplug.c +0
-0@@ -427,17 +427,24 @@ static unsigned long find_smallest_section_pfn(intnid, struct zone *zone,
Quoted 20 lines
mm/memory_hotplug.c +7
-6unsigned long start_pfn, unsigned long end_pfn) { -for (; start_pfn < end_pfn; start_pfn +=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (start_pfn))) -continue ; +unsigned long next_pfn; -if (unlikely (pfn_to_nid (start_pfn) != nid)) -continue ; +for (; start_pfn < end_pfn; start_pfn = next_pfn) { +unsigned long tail_pfn; -if (zone !=page_zone (pfn_to_page (start_pfn))) + next_pfn = start_pfn +PAGES_PER_SUBSECTION ; + tail_pfn = next_pfn -1 ; + +if (unlikely (!pfn_to_online_page (start_pfn)))continue ;
Quoted 14 lines
mm/memory_hotplug.c static unsigned long find_smallest_section_pfn(int nid, struct zone *zone, · line 447
Quoted 8 lines
mm/memory_hotplug.c +7
-1-return start_pfn; +if (likely (pfn_to_nid (start_pfn) == nid) && + zone ==page_zone (pfn_to_page (start_pfn))) +return start_pfn; + +if (likely (pfn_to_nid (tail_pfn) == nid) && + zone ==page_zone (pfn_to_page (tail_pfn))) +return start_pfn;Here we are checking range [
start_pfn,tail_pfn]. When we come here, it means
start_pfn's nid or zone doesn't match our expectation. But iftail_pfndoes,
why it still returnstart_pfn?
Hi Wei
If start_pfn falls into a hole while tail_pfn still belongs to a valid memblock in this zone, skipping the subsection would cause shrink_zone_span() to shrink the zone span too aggressively, excluding valid PFNs from the zone.
Since init_unavailable_range() initializes hole pages with the correct zone/nid, the start_pfn check always succeeds here, making the tail_pfn check redundant today.
That said, I wonder if it is still worth keeping this check so that shrink_zone_span() does not depend on how hole pages are initialized.
Quoted 46 lines
mm/memory_hotplug.c static unsigned long find_biggest_section_pfn(int
Quoted 5 lines
mm/memory_hotplug.c +0
-0}return 0 ;@@ -448,21 +455,26 @@ static unsigned long find_biggest_section_pfn(intnid, struct zone *zone,
Quoted 35 lines
mm/memory_hotplug.c +13
-9unsigned long start_pfn, unsigned long end_pfn) { -unsigned long pfn; +unsigned long pfn, prev_pfn;/* pfn is the end pfn of a memory section. */ pfn = end_pfn -1 ; -for (; pfn >= start_pfn; pfn -=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (pfn))) -continue ; +for (; pfn >= start_pfn; pfn = prev_pfn) { +unsigned long head_pfn; -if (unlikely (pfn_to_nid (pfn) != nid)) -continue ; + prev_pfn = pfn -PAGES_PER_SUBSECTION ; + head_pfn = prev_pfn +1 ; -if (zone !=page_zone (pfn_to_page (pfn))) +if (unlikely (!pfn_to_online_page (pfn)))continue ; -return pfn; +if (likely (pfn_to_nid (pfn) == nid) && + zone ==page_zone (pfn_to_page (pfn))) +return pfn; + +if (likely (pfn_to_nid (head_pfn) == nid) && + zone ==page_zone (pfn_to_page (head_pfn))) +return pfn; }return 0 ; --Quoted 1 line
2.47.3
--
Wei Yang
Help you, Help me
Re: [PATCH v6 2/2] mm/memory_hotplug: improve shrink_zone_span() subsection boundary checks
Quoted 80 lines · On Mon, Jul 27, 2026 at 09:49:39AM +0000, Liu, Yuan1 wrote:
Quoted 58 lines
-----Original Message-----
From: Wei Yang <[email protected]>
Sent: Saturday, July 25, 2026 10:50 AM
To: Liu, Yuan1 <[email protected]>
Cc: David Hildenbrand <[email protected]>; Oscar Salvador <[email protected]>; Mike Rapoport <[email protected]>; Wei Yang <[email protected]>; [email protected]; Zou, Nanhai <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; Zeng, Jason <[email protected]>; [email protected]
Subject: Re: [PATCH v6 2/2]mm/memory_hotplug: improveshrink_zone_span()subsection boundary checksmm/memory_hotplug.c static unsigned long find_smallest_section_pfn(int
Quoted 23 lines · Yuan Liu
When shrinking a zone span after removing a PFN range,
find_smallest_section_pfn()andfind_biggest_section_pfn()only checked one edge PFN in each subsection for nid/zone matching.If a memory or hole boundary falls in the middle of a subsection, that edge PFN may belong to a different nid/zone, causing the helpers to miss a valid PFN within that subsection.
Fix this by checking both subsection edge PFNs for nid/zone matching. Keep a single
pfn_to_online_page()check per subsection, since online state is the same for all PFNs in a subsection.Reviewed-by Jason Zeng <[email protected]>Signed-off-by Yuan Liu <[email protected]>---
mm/memory_hotplug.c| 42 +++++++++++++++++++++++++++---------------1 file changed, 27 insertions(+), 15 deletions(-)mm/memory_hotplug.c +0
-0@@ -427,17 +427,24 @@ static unsigned long find_smallest_section_pfn(intnid, struct zone *zone,
Quoted 20 lines
mm/memory_hotplug.c +7
-6unsigned long start_pfn, unsigned long end_pfn) { -for (; start_pfn < end_pfn; start_pfn +=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (start_pfn))) -continue ; +unsigned long next_pfn; -if (unlikely (pfn_to_nid (start_pfn) != nid)) -continue ; +for (; start_pfn < end_pfn; start_pfn = next_pfn) { +unsigned long tail_pfn; -if (zone !=page_zone (pfn_to_page (start_pfn))) + next_pfn = start_pfn +PAGES_PER_SUBSECTION ; + tail_pfn = next_pfn -1 ; + +if (unlikely (!pfn_to_online_page (start_pfn)))continue ;Quoted 14 lines
mm/memory_hotplug.c static unsigned long find_smallest_section_pfn(int nid, struct zone *zone, · line 447
Quoted 8 lines
mm/memory_hotplug.c +7
-1-return start_pfn; +if (likely (pfn_to_nid (start_pfn) == nid) && + zone ==page_zone (pfn_to_page (start_pfn))) +return start_pfn; + +if (likely (pfn_to_nid (tail_pfn) == nid) && + zone ==page_zone (pfn_to_page (tail_pfn))) +return start_pfn;Here we are checking range [
start_pfn,tail_pfn]. When we come here, it means
start_pfn's nid or zone doesn't match our expectation. But iftail_pfndoes,
why it still returnstart_pfn?Hi Wei
If
start_pfnfalls into a hole whiletail_pfnstill belongs to a valid memblock in this zone, skipping the subsection would causeshrink_zone_span()to shrink the zone span too aggressively, excluding valid PFNs from the zone.
Quoted 7 lines
Since
init_unavailable_range()initializes hole pages with the correct zone/nid, thestart_pfncheck always succeeds here, making thetail_pfncheck redundant today.That said, I wonder if it is still worth keeping this check so that
shrink_zone_span()does not depend on how hole pages are initialized.
Looks reasonable.
I search the discussion history, and found David suggest this fix in [1] with following statement.
Well, unless we have an odd case where the hole+memory starts in the middle of a "PAGES_PER_SUBSECTION". That would already be problematic if memory starts/ends in the middle of a PAGES_PER_SUBSECTION chunk. I don't such a case exists.
We could improve shrink_zone_span() to let find_smallest_section_pfn/find_biggest_section_pfn test the pfn_to_nid() and page_zone() not on;y on the smallest/highest pfn, but also on the highest/smallest PFN in a PAGES_PER_SUBSECTION chunk.
I am trying to understand the exact case David described, but not fully get it. Would you mind describing more, so we would make sure not missing the point.
[1]: https://lore.kernel.org/all/[email protected]/T/#u
Signature · 2 lines
Wei Yang Help you, Help me
RE: [PATCH v6 2/2] mm/memory_hotplug: improve shrink_zone_span() subsection boundary checks
Quoted 120 lines
-----Original Message-----
From: Wei Yang <[email protected]>
Sent: Thursday, July 30, 2026 10:36 AM
To: Liu, Yuan1 <[email protected]>
Cc: Wei Yang <[email protected]>; David Hildenbrand <[email protected]>; Oscar Salvador <[email protected]>; Mike Rapoport <[email protected]>; [email protected]; Zou, Nanhai <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; Zeng, Jason <[email protected]>; linux- [email protected]
Subject: Re: [PATCH v6 2/2]mm/memory_hotplug: improveshrink_zone_span()subsection boundary checksQuoted 11 lines · On Mon, Jul 27, 2026 at 09:49:39AM +0000, Liu, Yuan1 wrote:
Quoted 11 lines
-----Original Message-----
From: Wei Yang <[email protected]>
Sent: Saturday, July 25, 2026 10:50 AM
To: Liu, Yuan1 <[email protected]>
Cc: David Hildenbrand <[email protected]>; Oscar Salvador <[email protected]>; Mike Rapoport <[email protected]>; Wei Yang <[email protected]>; [email protected]; Zou, Nanhai <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; Zeng, Jason <[email protected]>; [email protected]
Subject: Re: [PATCH v6 2/2]mm/memory_hotplug: improve
shrink_zone_span()Quoted 21 lines
Quoted 21 lines
subsection boundary checks
Quoted 18 lines · Yuan Liu
When shrinking a zone span after removing a PFN range,
find_smallest_section_pfn()andfind_biggest_section_pfn()only checked one edge PFN in each subsection for nid/zone matching.If a memory or hole boundary falls in the middle of a subsection, that edge PFN may belong to a different nid/zone, causing the helpers to miss a valid PFN within that subsection.
Fix this by checking both subsection edge PFNs for nid/zone matching. Keep a single
pfn_to_online_page()check per subsection, since online state is the same for all PFNs in a subsection.Reviewed-by Jason Zeng <[email protected]>Signed-off-by Yuan Liu <[email protected]>---
mm/memory_hotplug.c| 42 +++++++++++++++++++++++++++---------------1 file changed, 27 insertions(+), 15 deletions(-)Quoted 5 lines
Quoted 5 lines
mm/memory_hotplug.c static unsigned long
Quoted 5 lines
mm/memory_hotplug.c +0
-0@@ -427,17 +427,24 @@ static unsigned longfind_smallest_section_pfn(int
Quoted 43 lines
Quoted 21 lines
nid, struct zone *zone,
Quoted 20 lines
mm/memory_hotplug.c +7
-6unsigned long start_pfn, unsigned long end_pfn) { -for (; start_pfn < end_pfn; start_pfn +=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (start_pfn))) -continue ; +unsigned long next_pfn; -if (unlikely (pfn_to_nid (start_pfn) != nid)) -continue ; +for (; start_pfn < end_pfn; start_pfn = next_pfn) { +unsigned long tail_pfn; -if (zone !=page_zone (pfn_to_page (start_pfn))) + next_pfn = start_pfn +PAGES_PER_SUBSECTION ; + tail_pfn = next_pfn -1 ; + +if (unlikely (!pfn_to_online_page (start_pfn)))continue ;Quoted 14 lines
mm/memory_hotplug.c static unsigned long find_smallest_section_pfn(int nid, struct zone *zone, · line 447
Quoted 8 lines
mm/memory_hotplug.c +7
-1-return start_pfn; +if (likely (pfn_to_nid (start_pfn) == nid) && + zone ==page_zone (pfn_to_page (start_pfn))) +return start_pfn; + +if (likely (pfn_to_nid (tail_pfn) == nid) && + zone ==page_zone (pfn_to_page (tail_pfn))) +return start_pfn;Here we are checking range [
start_pfn,tail_pfn]. When we come here, it means
start_pfn's nid or zone doesn't match our expectation. But iftail_pfndoes,
why it still returnstart_pfn?Hi Wei
If
start_pfnfalls into a hole whiletail_pfnstill belongs to a valid memblock in this zone, skipping the subsection would causeshrink_zone_span()to shrink the zone span too aggressively, excluding valid PFNs from the zone.Quoted 7 lines
Since
init_unavailable_range()initializes hole pages with the correct zone/nid, thestart_pfncheck always succeeds here, making thetail_pfncheck redundant today.That said, I wonder if it is still worth keeping this check so that
shrink_zone_span()does not depend on how hole pages are initialized.Looks reasonable.
I search the discussion history, and found David suggest this fix in [1] with
following statement.Well, unless we have an odd case where the hole+memory starts in the middle of a "
PAGES_PER_SUBSECTION". That would already be problematic if memory starts/ends in the middle of aPAGES_PER_SUBSECTIONchunk. I don't such a case exists.We could improve
shrink_zone_span()to let find_smallest_section_pfn/find_biggest_section_pfn test thepfn_to_nid()andpage_zone()not on;y on the smallest/highest pfn, but also on the highest/smallest PFN in aPAGES_PER_SUBSECTIONchunk.
Quoted 7 lines
I am trying to understand the exact case David described, but not fully get
it. Would you mind describing more, so we would make sure not missing the point.[1]: https://lore.kernel.org/all/e86fee84-08d8-4563-8596-
[email protected]/T/#u
I am not sure whether this scenario can occur on real systems, but from reading the current code it seems there may be a corner case.
For example, consider a zone whose tail ends with a single-PFN hole. init_unavailable_range() initializes this hole page and assigns its nid/zone to the adjacent next zone, since the PFN itself belongs outside the current zone.
When memory is hot-added after this hole, the hotplug granularity (PAGES_PER_SUBSECTION) extends the zone span to include the entire subsection containing the hole.
Later, when the hot-added memory is removed, find_biggest_section_pfn() examines the hole page first. Because its nid/zone belongs to the adjacent next zone, the boundary subsection may be considered removable even though it still contains boot memory pages.
This appears to be asymmetric between the zone head and the zone tail.
At the zone head (find_smallest_section_pfn()), the boundary subsection has a hole + memory layout. The hole pages belong to the current zone, so the subsection is retained.
At the zone tail (find_biggest_section_pfn()), the boundary subsection has a memory + hole layout. The trailing hole pages belong to the adjacent next zone, so the subsection may be discarded.
Quoted 3 lines
--
Wei Yang
Help you, Help me
Re: [PATCH v6 2/2] mm/memory_hotplug: improve shrink_zone_span() subsection boundary checks
Quoted 131 lines · On Thu, Jul 30, 2026 at 07:57:13AM +0000, Liu, Yuan1 wrote:
Quoted 120 lines
-----Original Message-----
From: Wei Yang <[email protected]>
Sent: Thursday, July 30, 2026 10:36 AM
To: Liu, Yuan1 <[email protected]>
Cc: Wei Yang <[email protected]>; David Hildenbrand <[email protected]>; Oscar Salvador <[email protected]>; Mike Rapoport <[email protected]>; [email protected]; Zou, Nanhai <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; Zeng, Jason <[email protected]>; linux- [email protected]
Subject: Re: [PATCH v6 2/2]mm/memory_hotplug: improveshrink_zone_span()subsection boundary checksQuoted 11 lines · On Mon, Jul 27, 2026 at 09:49:39AM +0000, Liu, Yuan1 wrote:
Quoted 11 lines
-----Original Message-----
From: Wei Yang <[email protected]>
Sent: Saturday, July 25, 2026 10:50 AM
To: Liu, Yuan1 <[email protected]>
Cc: David Hildenbrand <[email protected]>; Oscar Salvador <[email protected]>; Mike Rapoport <[email protected]>; Wei Yang <[email protected]>; [email protected]; Zou, Nanhai <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; Zeng, Jason <[email protected]>; [email protected]
Subject: Re: [PATCH v6 2/2]mm/memory_hotplug: improve
shrink_zone_span()Quoted 21 lines
Quoted 21 lines
subsection boundary checks
Quoted 18 lines · Yuan Liu
When shrinking a zone span after removing a PFN range,
find_smallest_section_pfn()andfind_biggest_section_pfn()only checked one edge PFN in each subsection for nid/zone matching.If a memory or hole boundary falls in the middle of a subsection, that edge PFN may belong to a different nid/zone, causing the helpers to miss a valid PFN within that subsection.
Fix this by checking both subsection edge PFNs for nid/zone matching. Keep a single
pfn_to_online_page()check per subsection, since online state is the same for all PFNs in a subsection.Reviewed-by Jason Zeng <[email protected]>Signed-off-by Yuan Liu <[email protected]>---
mm/memory_hotplug.c| 42 +++++++++++++++++++++++++++---------------1 file changed, 27 insertions(+), 15 deletions(-)Quoted 5 lines
Quoted 5 lines
mm/memory_hotplug.c static unsigned long
Quoted 5 lines
mm/memory_hotplug.c +0
-0@@ -427,17 +427,24 @@ static unsigned longfind_smallest_section_pfn(int
Quoted 43 lines
Quoted 21 lines
nid, struct zone *zone,
Quoted 20 lines
mm/memory_hotplug.c +7
-6unsigned long start_pfn, unsigned long end_pfn) { -for (; start_pfn < end_pfn; start_pfn +=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (start_pfn))) -continue ; +unsigned long next_pfn; -if (unlikely (pfn_to_nid (start_pfn) != nid)) -continue ; +for (; start_pfn < end_pfn; start_pfn = next_pfn) { +unsigned long tail_pfn; -if (zone !=page_zone (pfn_to_page (start_pfn))) + next_pfn = start_pfn +PAGES_PER_SUBSECTION ; + tail_pfn = next_pfn -1 ; + +if (unlikely (!pfn_to_online_page (start_pfn)))continue ;Quoted 14 lines
mm/memory_hotplug.c static unsigned long find_smallest_section_pfn(int nid, struct zone *zone, · line 447
Quoted 8 lines
mm/memory_hotplug.c +7
-1-return start_pfn; +if (likely (pfn_to_nid (start_pfn) == nid) && + zone ==page_zone (pfn_to_page (start_pfn))) +return start_pfn; + +if (likely (pfn_to_nid (tail_pfn) == nid) && + zone ==page_zone (pfn_to_page (tail_pfn))) +return start_pfn;Here we are checking range [
start_pfn,tail_pfn]. When we come here, it means
start_pfn's nid or zone doesn't match our expectation. But iftail_pfndoes,
why it still returnstart_pfn?Hi Wei
If
start_pfnfalls into a hole whiletail_pfnstill belongs to a valid memblock in this zone, skipping the subsection would causeshrink_zone_span()to shrink the zone span too aggressively, excluding valid PFNs from the zone.Quoted 7 lines
Since
init_unavailable_range()initializes hole pages with the correct zone/nid, thestart_pfncheck always succeeds here, making thetail_pfncheck redundant today.That said, I wonder if it is still worth keeping this check so that
shrink_zone_span()does not depend on how hole pages are initialized.Looks reasonable.
I search the discussion history, and found David suggest this fix in [1] with
following statement.Well, unless we have an odd case where the hole+memory starts in the middle of a "
PAGES_PER_SUBSECTION". That would already be problematic if memory starts/ends in the middle of aPAGES_PER_SUBSECTIONchunk. I don't such a case exists.We could improve
shrink_zone_span()to let find_smallest_section_pfn/find_biggest_section_pfn test thepfn_to_nid()andpage_zone()not on;y on the smallest/highest pfn, but also on the highest/smallest PFN in aPAGES_PER_SUBSECTIONchunk.Quoted 7 lines
I am trying to understand the exact case David described, but not fully get
it. Would you mind describing more, so we would make sure not missing the point.[1]: https://lore.kernel.org/all/e86fee84-08d8-4563-8596-
[email protected]/T/#uI am not sure whether this scenario can occur on real systems, but from reading the current code it seems there may be a corner case.
Quoted 9 lines
For example, consider a zone whose tail ends with a single-PFN hole.
init_unavailable_range()initializes this hole page and assigns its nid/zone to the adjacent next zone, since the PFN itself belongs outside the current zone.When memory is hot-added after this hole, the hotplug granularity (
PAGES_PER_SUBSECTION) extends the zone span to include the entire subsection containing the hole.
IIUC, for memory hotplug, the alignment is guaranteed by check_hotplug_memory_range() with min block size which is section aligned.
Quoted 7 lines
Later, when the hot-added memory is removed,
find_biggest_section_pfn()examines the hole page first. Because its nid/zone belongs to the adjacent next zone, the boundary subsection may be considered removable even though it still contains boot memory pages.This appears to be asymmetric between the zone head and the zone tail.
Quoted 8 lines
At the zone head (
find_smallest_section_pfn()), the boundary subsection has a hole + memory layout. The hole pages belong to the current zone, so the subsection is retained.At the zone tail (
find_biggest_section_pfn()), the boundary subsection has a memory + hole layout. The trailing hole pages belong to the adjacent next zone, so the subsection may be discarded.
I am trying to see what the real case it would be.
Hmm.. First I don't expect there would be memory hotplug happens between zones or nodes. So the new added memory range always sits after boot memory(early sections).
Then let's assume below memory layout after bootup.
|< Zone Normal >| |< Zone Movable >| +----------------+.....+----------------+.....+ | |hole1| |hole2| +----------------+.....+----------------+.....+
Both hole is less than subsection size, and the hole2 expand to section aligned address. According to the init_unavailable_range() during memmap_init(), both holes are init to Zone Movable.
Then we can hot-add next section and hot-online it to:
1) Zone Movable
2) Zone Normal
For case 1), if we offline this section, we run into find_biggest_section_pfn() for Zone Movable. It test the last pfn in hole2, and see it meets the condition. So Zone Movable results in spanning hole2, which is larger than original value. This is not that accurate, but still could work.
For case 2), if we offline this section, we run into find_biggest_section_pfn() for Zone Normal. Here we also have two cases based on the location of the hole:
a) hole starts at the beginning of subsection (hole + memory)
b) hole ends at the end of subsection (memory + hole)
For a), it looks good, because the subsection hole is treated as Zone Movable, and Zone Normal is correctly sized.
For b), it seems not right, as we only test subsection's last pfn. As it belongs to Zone Movable we would skip the subsection and set Normal Zone span to previous subsection. Even some of the pfn belongs to Zone Normal.
Hmm... I guess this is the case David mentioned.
And with your patch, it checks head_pfn of this subsection, so it fixes this by extend Zone Normal to just ahead Zone Movable. This works something like a), but make Zone Normal span to some pfn initialized to Zone Movable.
Based on above analysis, let's see when find_smallest_section_pfn() will have trouble.
The case in my mind is like below:
|< Zone Normal >|< Zone Movable >| +----------------+----------------+.....+----------------+ | | |hole | | +----------------+----------------+.....+----------------+
Hole should sits at the beginning of a section, otherwise offline_pages() would prevent offline the range. Then when offline those beginning Movable memory, find_smallest_section_pfn() is triggered. But as hole is init to Zone Movable, it looks good.
On summary:
* find_biggest_section_pfn() may truncate zone span range if there is a hole less than subsection sits at the end of subsection
* find_smallest_section_pfn() looks good even there is a hole less than subsection
Above is my understanding about the possible cases when re-sizing zone. In case missing some point, just let me know :-)
Signature · 2 lines
Wei Yang Help you, Help me
David Hildenbrand (Arm)
maintainerRe: [PATCH v6 2/2] mm/memory_hotplug: improve shrink_zone_span() subsection boundary checks
In reply to Yuan LiuQuoted 79 lines · Yuan Liu
When shrinking a zone span after removing a PFN range,
find_smallest_section_pfn()andfind_biggest_section_pfn()only checked one edge PFN in each subsection for nid/zone matching.If a memory or hole boundary falls in the middle of a subsection, that edge PFN may belong to a different nid/zone, causing the helpers to miss a valid PFN within that subsection.
Fix this by checking both subsection edge PFNs for nid/zone matching. Keep a single
pfn_to_online_page()check per subsection, since online state is the same for all PFNs in a subsection.Reviewed-by Jason Zeng <[email protected]>Signed-off-by Yuan Liu <[email protected]>--- mm/memory_hotplug.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-)mm/memory_hotplug.c +20
-15@@ -427,17 +427,24 @@ static unsigned long find_smallest_section_pfn(int nid, struct zone *zone, unsigned long start_pfn, unsigned long end_pfn) { -for (; start_pfn < end_pfn; start_pfn +=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (start_pfn))) -continue ; +unsigned long next_pfn; -if (unlikely (pfn_to_nid (start_pfn) != nid)) -continue ; +for (; start_pfn < end_pfn; start_pfn = next_pfn) { +unsigned long tail_pfn; -if (zone !=page_zone (pfn_to_page (start_pfn))) + next_pfn = start_pfn +PAGES_PER_SUBSECTION ; + tail_pfn = next_pfn -1 ; + +if (unlikely (!pfn_to_online_page (start_pfn)))continue ; -return start_pfn; +if (likely (pfn_to_nid (start_pfn) == nid) && + zone ==page_zone (pfn_to_page (start_pfn))) +return start_pfn; + +if (likely (pfn_to_nid (tail_pfn) == nid) && + zone ==page_zone (pfn_to_page (tail_pfn))) +return start_pfn; }return 0 ;@@ -448,21 +455,26 @@ static unsigned long find_biggest_section_pfn(int nid, struct zone *zone, unsigned long start_pfn, unsigned long end_pfn) { -unsigned long pfn; +unsigned long pfn, prev_pfn;/* pfn is the end pfn of a memory section. */ pfn = end_pfn -1 ; -for (; pfn >= start_pfn; pfn -=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (pfn))) -continue ; +for (; pfn >= start_pfn; pfn = prev_pfn) { +unsigned long head_pfn; -if (unlikely (pfn_to_nid (pfn) != nid)) -continue ; + prev_pfn = pfn -PAGES_PER_SUBSECTION ; + head_pfn = prev_pfn +1 ; -if (zone !=page_zone (pfn_to_page (pfn))) +if (unlikely (!pfn_to_online_page (pfn)))continue ; -return pfn;
mm/memory_hotplug.c static unsigned long find_biggest_section_pfn(int nid, struct zone *zone, · line 480
Quoted 10 lines
mm/memory_hotplug.c +7
-0+if (likely (pfn_to_nid (pfn) == nid) && + zone ==page_zone (pfn_to_page (pfn))) +return pfn; + +if (likely (pfn_to_nid (head_pfn) == nid) && + zone ==page_zone (pfn_to_page (head_pfn))) +return pfn; }return 0 ;
I think improving that should be patch #1.
But I think we can do much better code-wise. What about the following cleanup instead:
From 6e9ea094daf8d04c0b119b780572a846334f8164 Mon Sep 17 00:00:00 2001
From: "David Hildenbrand (Arm)" <[email protected]>
Date: Wed, 5 Aug 2026 12:52:55 +0200
Subject: [PATCH] mm/memory_hotplug: make shrink_zone_span() more robust
Let's make shrink_zone_span() more robust by checking in find_smallest_section_pfn() / find_biggest_section_pfn() that start+end of the subsection.
While at it, clean up the function heavily, factoring the core check out into subsection_overlaps_zone().
There likely is no need to check for the nid first: we require SPARSEMEM_VMEMMAP_ENABLE where pfn_to_page() is cheap, and the pfn_to_nid() on CONFIG_NUMA would do a pfn_to_page() either way. So let's just drop that for now.
--- mm/memory_hotplug.c | 59 ++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 35 deletions(-)
mm/memory_hotplug.c +24 -35
@@ -425,49 +425,39 @@ int __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,return err; } -/* find the smallest valid pfn in the range [start_pfn, end_pfn) */ -static unsigned long find_smallest_section_pfn (int nid,struct zone *zone, -unsigned long start_pfn, -unsigned long end_pfn) +static bool subsection_overlaps_zone (unsigned long pfn,struct zone *zone) { -for (; start_pfn < end_pfn; start_pfn +=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (start_pfn))) -continue ; +const unsigned long start_pfn =ALIGN_DOWN (pfn,PAGES_PER_SUBSECTION ); +const unsigned long end_pfn = start_pfn +PAGES_PER_SUBSECTION -1 ; -if (unlikely (pfn_to_nid (start_pfn) != nid)) -continue ; +/* All pages in a subsection are either online or offline. */ +if (unlikely (!pfn_to_online_page (start_pfn))) +return false; -if (zone !=page_zone (pfn_to_page (start_pfn))) -continue ; +/* Checking start+end is sufficient. */ +return zone ==page_zone (pfn_to_page (start_pfn)) || + zone ==page_zone (pfn_to_page (end_pfn)); +} -return start_pfn; +/* find the smallest valid pfn in the range [start_pfn, end_pfn) */ +static unsigned long find_smallest_section_pfn (struct zone *zone, +unsigned long start_pfn,unsigned long end_pfn) +{ +for (; start_pfn < end_pfn; start_pfn +=PAGES_PER_SUBSECTION ) { +if (subsection_overlaps_zone (start_pfn, zone)) +return start_pfn; } -return 0 ; }/* find the biggest valid pfn in the range [start_pfn, end_pfn). */ -static unsigned long find_biggest_section_pfn (int nid,struct zone *zone, -unsigned long start_pfn, -unsigned long end_pfn) +static unsigned long find_biggest_section_pfn (struct zone *zone, +unsigned long start_pfn,unsigned long end_pfn) { -unsigned long pfn; - -/* pfn is the end pfn of a memory section. */ - pfn = end_pfn -1 ; -for (; pfn >= start_pfn; pfn -=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (pfn))) -continue ; - -if (unlikely (pfn_to_nid (pfn) != nid)) -continue ; - -if (zone !=page_zone (pfn_to_page (pfn))) -continue ; - -return pfn; +for (; end_pfn >= start_pfn; end_pfn -=PAGES_PER_SUBSECTION ) { +if (subsection_overlaps_zone (end_pfn -1 , zone)) +return end_pfn -1 ; } -return 0 ; }
@@ -475,7 +465,6 @@ static void shrink_zone_span(struct zone *zone, unsigned long start_pfn, unsigned long end_pfn) {unsigned long pfn; -int nid =zone_to_nid (zone);if (zone->zone_start_pfn == start_pfn) {/*
@@ -484,7 +473,7 @@ static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,* In this case, we find second smallest valid mem_section * for shrinking zone. */ - pfn =find_smallest_section_pfn (nid, zone, end_pfn, + pfn =find_smallest_section_pfn (zone, end_pfn,zone_end_pfn (zone));if (pfn) { zone->spanned_pages =zone_end_pfn (zone) - pfn;
@@ -500,7 +489,7 @@ static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,* In this case, we find second biggest valid mem_section for * shrinking zone. */ - pfn =find_biggest_section_pfn (nid, zone, zone->zone_start_pfn, + pfn =find_biggest_section_pfn (zone, zone->zone_start_pfn, start_pfn);if (pfn) zone->spanned_pages = pfn - zone->zone_start_pfn +1 ;
Signature · 7 lines
2.43.0 -- Cheers, David
RE: [PATCH v6 2/2] mm/memory_hotplug: improve shrink_zone_span() subsection boundary checks
Quoted 242 lines
-----Original Message-----
From: David Hildenbrand (Arm) <[email protected]>
Sent: Wednesday, August 5, 2026 7:02 PM
To: Liu, Yuan1 <[email protected]>; Oscar Salvador <[email protected]>; Mike Rapoport <[email protected]>; Wei Yang <[email protected]>
Cc: [email protected]; Zou, Nanhai <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; Zeng, Jason <[email protected]>; linux- [email protected]
Subject: Re: [PATCH v6 2/2]mm/memory_hotplug: improveshrink_zone_span()subsection boundary checksmm/memory_hotplug.c static unsigned long find_smallest_section_pfn(int
Quoted 23 lines · Yuan Liu
When shrinking a zone span after removing a PFN range,
find_smallest_section_pfn()andfind_biggest_section_pfn()only checked one edge PFN in each subsection for nid/zone matching.If a memory or hole boundary falls in the middle of a subsection, that edge PFN may belong to a different nid/zone, causing the helpers to miss a valid PFN within that subsection.
Fix this by checking both subsection edge PFNs for nid/zone matching. Keep a single
pfn_to_online_page()check per subsection, since online state is the same for all PFNs in a subsection.Reviewed-by Jason Zeng <[email protected]>Signed-off-by Yuan Liu <[email protected]>--- mm/memory_hotplug.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-)mm/memory_hotplug.c +0
-0@@ -427,17 +427,24 @@ static unsigned long find_smallest_section_pfn(intnid, struct zone *zone,
mm/memory_hotplug.c static unsigned long find_biggest_section_pfn(int
Quoted 32 lines
mm/memory_hotplug.c +14
-7unsigned long start_pfn, unsigned long end_pfn) { -for (; start_pfn < end_pfn; start_pfn +=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (start_pfn))) -continue ; +unsigned long next_pfn; -if (unlikely (pfn_to_nid (start_pfn) != nid)) -continue ; +for (; start_pfn < end_pfn; start_pfn = next_pfn) { +unsigned long tail_pfn; -if (zone !=page_zone (pfn_to_page (start_pfn))) + next_pfn = start_pfn +PAGES_PER_SUBSECTION ; + tail_pfn = next_pfn -1 ; + +if (unlikely (!pfn_to_online_page (start_pfn)))continue ; -return start_pfn; +if (likely (pfn_to_nid (start_pfn) == nid) && + zone ==page_zone (pfn_to_page (start_pfn))) +return start_pfn; + +if (likely (pfn_to_nid (tail_pfn) == nid) && + zone ==page_zone (pfn_to_page (tail_pfn))) +return start_pfn; }return 0 ;@@ -448,21 +455,26 @@ static unsigned long find_biggest_section_pfn(intnid, struct zone *zone,
Quoted 24 lines
mm/memory_hotplug.c +6
-8unsigned long start_pfn, unsigned long end_pfn) { -unsigned long pfn; +unsigned long pfn, prev_pfn;/* pfn is the end pfn of a memory section. */ pfn = end_pfn -1 ; -for (; pfn >= start_pfn; pfn -=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (pfn))) -continue ; +for (; pfn >= start_pfn; pfn = prev_pfn) { +unsigned long head_pfn; -if (unlikely (pfn_to_nid (pfn) != nid)) -continue ; + prev_pfn = pfn -PAGES_PER_SUBSECTION ; + head_pfn = prev_pfn +1 ; -if (zone !=page_zone (pfn_to_page (pfn))) +if (unlikely (!pfn_to_online_page (pfn)))continue ; -return pfn;mm/memory_hotplug.c static unsigned long find_biggest_section_pfn(int · line 480
Quoted 10 lines
mm/memory_hotplug.c +7
-0+if (likely (pfn_to_nid (pfn) == nid) && + zone ==page_zone (pfn_to_page (pfn))) +return pfn; + +if (likely (pfn_to_nid (head_pfn) == nid) && + zone ==page_zone (pfn_to_page (head_pfn))) +return pfn; }return 0 ;I think improving that should be patch #1.
But I think we can do much better code-wise. What about the following cleanup instead:
From
6e9ea094daf8d04c0b119b780572a846334f8164Mon Sep 17 00:00:00 2001
From: "David Hildenbrand (Arm)" <[email protected]>
Date: Wed, 5 Aug 2026 12:52:55 +0200
Subject: [PATCH]mm/memory_hotplug: makeshrink_zone_span()more robustLet's make
shrink_zone_span()more robust by checking infind_smallest_section_pfn()/find_biggest_section_pfn()that start+end of the subsection.While at it, clean up the function heavily, factoring the core check out into
subsection_overlaps_zone().There likely is no need to check for the nid first: we require
SPARSEMEM_VMEMMAP_ENABLEwherepfn_to_page()is cheap, and thepfn_to_nid()onCONFIG_NUMAwould do apfn_to_page()either way. So let's just drop that for now.Signed-off-by David Hildenbrand (Arm) <[email protected]>--- mm/memory_hotplug.c | 59 ++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 35 deletions(-)mm/memory_hotplug.c +22
-33@@ -425,49 +425,39 @@ int __add_pages(int nid, unsigned long pfn, unsignedlong nr_pages, return err; } -/* find the smallest valid pfn in the range [start_pfn, end_pfn) */ -static unsigned long find_smallest_section_pfn (int nid,struct zone *zone, -unsigned long start_pfn, -unsigned long end_pfn) +static bool subsection_overlaps_zone(unsigned long pfn, struct zone *zone) { -for (; start_pfn < end_pfn; start_pfn +=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (start_pfn))) -continue ; +const unsigned long start_pfn =ALIGN_DOWN (pfn,PAGES_PER_SUBSECTION ); +const unsigned long end_pfn = start_pfn +PAGES_PER_SUBSECTION -1 ; -if (unlikely (pfn_to_nid (start_pfn) != nid)) -continue ; +/* All pages in a subsection are either online or offline. */ +if (unlikely (!pfn_to_online_page (start_pfn))) +return false; -if (zone !=page_zone (pfn_to_page (start_pfn))) -continue ; +/* Checking start+end is sufficient. */ +return zone ==page_zone (pfn_to_page (start_pfn)) || + zone ==page_zone (pfn_to_page (end_pfn)); +} -return start_pfn; +/* find the smallest valid pfn in the range [start_pfn, end_pfn) */ +static unsigned long find_smallest_section_pfn (struct zone *zone, +unsigned long start_pfn,unsigned long end_pfn) +{ +for (; start_pfn < end_pfn; start_pfn +=PAGES_PER_SUBSECTION ) { +if (subsection_overlaps_zone (start_pfn, zone)) +return start_pfn; } -return 0 ; }/* find the biggest valid pfn in the range [start_pfn, end_pfn). */ -static unsigned long find_biggest_section_pfn (int nid,struct zone *zone, -unsigned long start_pfn, -unsigned long end_pfn) +static unsigned long find_biggest_section_pfn (struct zone *zone, +unsigned long start_pfn,unsigned long end_pfn) { -unsigned long pfn; - -/* pfn is the end pfn of a memory section. */ - pfn = end_pfn -1 ; -for (; pfn >= start_pfn; pfn -=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (pfn))) -continue ; - -if (unlikely (pfn_to_nid (pfn) != nid)) -continue ; - -if (zone !=page_zone (pfn_to_page (pfn))) -continue ; - -return pfn; +for (; end_pfn >= start_pfn; end_pfn -=PAGES_PER_SUBSECTION ) { +if (subsection_overlaps_zone (end_pfn -1 , zone)) +return end_pfn -1 ; } -return 0 ; }@@ -475,7 +465,6 @@ static void shrink_zone_span(struct zone *zone, unsigned long start_pfn, unsigned long end_pfn) {unsigned long pfn; -int nid =zone_to_nid (zone);if (zone->zone_start_pfn == start_pfn) {mm/memory_hotplug.c +1
-1/* @@ -484,7 +473,7 @@ static void shrink_zone_span(struct zone *zone,unsigned long start_pfn, * In this case, we find second smallest valid mem_section * for shrinking zone. */ - pfn =find_smallest_section_pfn (nid, zone, end_pfn, + pfn =find_smallest_section_pfn (zone, end_pfn,zone_end_pfn (zone));if (pfn) {mm/memory_hotplug.c +1
-1zone->spanned_pages =zone_end_pfn (zone) - pfn;@@ -500,7 +489,7 @@ static void shrink_zone_span(struct zone *zone,unsigned long start_pfn, * In this case, we find second biggest valid mem_section for * shrinking zone. */ - pfn =find_biggest_section_pfn (nid, zone, zone- >zone_start_pfn, + pfn =find_biggest_section_pfn (zone, zone->zone_start_pfn, start_pfn);
mm/memory_hotplug.c static void shrink_zone_span(struct zone *zone, unsigned long start_pfn, · line 495
Quoted 2 lines
mm/memory_hotplug.c +0
-0if (pfn) zone->spanned_pages = pfn - zone->zone_start_pfn +1 ;
Hi David
Thanks for drafting this patch and for the guidance. I'll fold it into the next revision as patch 1.
Quoted 6 lines
--
Cheers,David
Re: [PATCH v6 2/2] mm/memory_hotplug: improve shrink_zone_span() subsection boundary checks
In reply to David Hildenbrand (Arm)Quoted 142 lines · David Hildenbrand (Arm)
Quoted 79 lines · Yuan Liu
When shrinking a zone span after removing a PFN range,
find_smallest_section_pfn()andfind_biggest_section_pfn()only checked one edge PFN in each subsection for nid/zone matching.If a memory or hole boundary falls in the middle of a subsection, that edge PFN may belong to a different nid/zone, causing the helpers to miss a valid PFN within that subsection.
Fix this by checking both subsection edge PFNs for nid/zone matching. Keep a single
pfn_to_online_page()check per subsection, since online state is the same for all PFNs in a subsection.Reviewed-by Jason Zeng <[email protected]>Signed-off-by Yuan Liu <[email protected]>--- mm/memory_hotplug.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-)mm/memory_hotplug.c +20
-15@@ -427,17 +427,24 @@ static unsigned long find_smallest_section_pfn(int nid, struct zone *zone, unsigned long start_pfn, unsigned long end_pfn) { -for (; start_pfn < end_pfn; start_pfn +=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (start_pfn))) -continue ; +unsigned long next_pfn; -if (unlikely (pfn_to_nid (start_pfn) != nid)) -continue ; +for (; start_pfn < end_pfn; start_pfn = next_pfn) { +unsigned long tail_pfn; -if (zone !=page_zone (pfn_to_page (start_pfn))) + next_pfn = start_pfn +PAGES_PER_SUBSECTION ; + tail_pfn = next_pfn -1 ; + +if (unlikely (!pfn_to_online_page (start_pfn)))continue ; -return start_pfn; +if (likely (pfn_to_nid (start_pfn) == nid) && + zone ==page_zone (pfn_to_page (start_pfn))) +return start_pfn; + +if (likely (pfn_to_nid (tail_pfn) == nid) && + zone ==page_zone (pfn_to_page (tail_pfn))) +return start_pfn; }return 0 ;@@ -448,21 +455,26 @@ static unsigned long find_biggest_section_pfn(int nid, struct zone *zone, unsigned long start_pfn, unsigned long end_pfn) { -unsigned long pfn; +unsigned long pfn, prev_pfn;/* pfn is the end pfn of a memory section. */ pfn = end_pfn -1 ; -for (; pfn >= start_pfn; pfn -=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (pfn))) -continue ; +for (; pfn >= start_pfn; pfn = prev_pfn) { +unsigned long head_pfn; -if (unlikely (pfn_to_nid (pfn) != nid)) -continue ; + prev_pfn = pfn -PAGES_PER_SUBSECTION ; + head_pfn = prev_pfn +1 ; -if (zone !=page_zone (pfn_to_page (pfn))) +if (unlikely (!pfn_to_online_page (pfn)))continue ; -return pfn;mm/memory_hotplug.c static unsigned long find_biggest_section_pfn(int nid, struct zone *zone, · line 480
Quoted 10 lines
mm/memory_hotplug.c +7
-0+if (likely (pfn_to_nid (pfn) == nid) && + zone ==page_zone (pfn_to_page (pfn))) +return pfn; + +if (likely (pfn_to_nid (head_pfn) == nid) && + zone ==page_zone (pfn_to_page (head_pfn))) +return pfn; }return 0 ;I think improving that should be patch #1.
But I think we can do much better code-wise. What about the following cleanup instead:
Quoted 1 line
From
6e9ea094daf8d04c0b119b780572a846334f8164Mon Sep 17 00:00:00 2001From: "David Hildenbrand (Arm)" <[email protected]>
Date: Wed, 5 Aug 2026 12:52:55 +0200
Subject: [PATCH]mm/memory_hotplug: makeshrink_zone_span()more robustLet's make
shrink_zone_span()more robust by checking infind_smallest_section_pfn()/find_biggest_section_pfn()that start+end of the subsection.While at it, clean up the function heavily, factoring the core check out into
subsection_overlaps_zone().There likely is no need to check for the nid first: we require
SPARSEMEM_VMEMMAP_ENABLEwherepfn_to_page()is cheap, and thepfn_to_nid()onCONFIG_NUMAwould do apfn_to_page()either way. So let's just drop that for now.Signed-off-by David Hildenbrand (Arm) <[email protected]>---
mm/memory_hotplug.c| 59 ++++++++++++++++++---------------------------1 file changed, 24 insertions(+), 35 deletions(-)mm/memory_hotplug.c +3
-9@@ -425,49 +425,39 @@ int __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,return err; } -/* find the smallest valid pfn in the range [start_pfn, end_pfn) */ -static unsigned long find_smallest_section_pfn (int nid,struct zone *zone, -unsigned long start_pfn, -unsigned long end_pfn) +static bool subsection_overlaps_zone (unsigned long pfn,struct zone *zone) { -for (; start_pfn < end_pfn; start_pfn +=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (start_pfn))) -continue ; +const unsigned long start_pfn =ALIGN_DOWN (pfn,PAGES_PER_SUBSECTION ); +const unsigned long end_pfn = start_pfn +PAGES_PER_SUBSECTION -1 ; -if (unlikely (pfn_to_nid (start_pfn) != nid)) -continue ;
mm/memory_hotplug.c int __add_pages(int nid, unsigned long pfn, unsigned long nr_pages, · line 440
Quoted 10 lines
mm/memory_hotplug.c +7
-2+/* All pages in a subsection are either online or offline. */ +if (unlikely (!pfn_to_online_page (start_pfn))) +return false; -if (zone !=page_zone (pfn_to_page (start_pfn))) -continue ; +/* Checking start+end is sufficient. */ +return zone ==page_zone (pfn_to_page (start_pfn)) || + zone ==page_zone (pfn_to_page (end_pfn)); +}
Hi, David
I am thinking if we could initialize each subsection to only one zone, we may reduce the complexity?
For example, in init_unavailable_range() align spfn to subsection and init the head unaligned part to previous zone?
Do you think this is reasonable?
Quoted 78 lines
mm/memory_hotplug.c +14
-24-return start_pfn; +/* find the smallest valid pfn in the range [start_pfn, end_pfn) */ +static unsigned long find_smallest_section_pfn (struct zone *zone, +unsigned long start_pfn,unsigned long end_pfn) +{ +for (; start_pfn < end_pfn; start_pfn +=PAGES_PER_SUBSECTION ) { +if (subsection_overlaps_zone (start_pfn, zone)) +return start_pfn; } -return 0 ; }/* find the biggest valid pfn in the range [start_pfn, end_pfn). */ -static unsigned long find_biggest_section_pfn (int nid,struct zone *zone, -unsigned long start_pfn, -unsigned long end_pfn) +static unsigned long find_biggest_section_pfn (struct zone *zone, +unsigned long start_pfn,unsigned long end_pfn) { -unsigned long pfn; - -/* pfn is the end pfn of a memory section. */ - pfn = end_pfn -1 ; -for (; pfn >= start_pfn; pfn -=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (pfn))) -continue ; - -if (unlikely (pfn_to_nid (pfn) != nid)) -continue ; - -if (zone !=page_zone (pfn_to_page (pfn))) -continue ; - -return pfn; +for (; end_pfn >= start_pfn; end_pfn -=PAGES_PER_SUBSECTION ) { +if (subsection_overlaps_zone (end_pfn -1 , zone)) +return end_pfn -1 ; } -return 0 ; }@@ -475,7 +465,6 @@ static void shrink_zone_span(struct zone *zone, unsigned long start_pfn, unsigned long end_pfn) {unsigned long pfn; -int nid =zone_to_nid (zone);if (zone->zone_start_pfn == start_pfn) {/* @@ -484,7 +473,7 @@ static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,* In this case, we find second smallest valid mem_section * for shrinking zone. */ - pfn =find_smallest_section_pfn (nid, zone, end_pfn, + pfn =find_smallest_section_pfn (zone, end_pfn,zone_end_pfn (zone));if (pfn) { zone->spanned_pages =zone_end_pfn (zone) - pfn;@@ -500,7 +489,7 @@ static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,* In this case, we find second biggest valid mem_section for * shrinking zone. */ - pfn =find_biggest_section_pfn (nid, zone, zone->zone_start_pfn, + pfn =find_biggest_section_pfn (zone, zone->zone_start_pfn, start_pfn);if (pfn) zone->spanned_pages = pfn - zone->zone_start_pfn +1 ;Signature · 7 lines
2.43.0 -- Cheers, David
Signature · 2 lines
Wei Yang Help you, Help me
David Hildenbrand (Arm)
maintainerRe: [PATCH v6 2/2] mm/memory_hotplug: improve shrink_zone_span() subsection boundary checks
Quoted 147 lines · Wei Yang
Quoted 146 lines · David Hildenbrand (Arm)
Quoted 79 lines · Yuan Liu
When shrinking a zone span after removing a PFN range,
find_smallest_section_pfn()andfind_biggest_section_pfn()only checked one edge PFN in each subsection for nid/zone matching.If a memory or hole boundary falls in the middle of a subsection, that edge PFN may belong to a different nid/zone, causing the helpers to miss a valid PFN within that subsection.
Fix this by checking both subsection edge PFNs for nid/zone matching. Keep a single
pfn_to_online_page()check per subsection, since online state is the same for all PFNs in a subsection.Reviewed-by Jason Zeng <[email protected]>Signed-off-by Yuan Liu <[email protected]>--- mm/memory_hotplug.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-)mm/memory_hotplug.c +20
-15@@ -427,17 +427,24 @@ static unsigned long find_smallest_section_pfn(int nid, struct zone *zone, unsigned long start_pfn, unsigned long end_pfn) { -for (; start_pfn < end_pfn; start_pfn +=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (start_pfn))) -continue ; +unsigned long next_pfn; -if (unlikely (pfn_to_nid (start_pfn) != nid)) -continue ; +for (; start_pfn < end_pfn; start_pfn = next_pfn) { +unsigned long tail_pfn; -if (zone !=page_zone (pfn_to_page (start_pfn))) + next_pfn = start_pfn +PAGES_PER_SUBSECTION ; + tail_pfn = next_pfn -1 ; + +if (unlikely (!pfn_to_online_page (start_pfn)))continue ; -return start_pfn; +if (likely (pfn_to_nid (start_pfn) == nid) && + zone ==page_zone (pfn_to_page (start_pfn))) +return start_pfn; + +if (likely (pfn_to_nid (tail_pfn) == nid) && + zone ==page_zone (pfn_to_page (tail_pfn))) +return start_pfn; }return 0 ;@@ -448,21 +455,26 @@ static unsigned long find_biggest_section_pfn(int nid, struct zone *zone, unsigned long start_pfn, unsigned long end_pfn) { -unsigned long pfn; +unsigned long pfn, prev_pfn;/* pfn is the end pfn of a memory section. */ pfn = end_pfn -1 ; -for (; pfn >= start_pfn; pfn -=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (pfn))) -continue ; +for (; pfn >= start_pfn; pfn = prev_pfn) { +unsigned long head_pfn; -if (unlikely (pfn_to_nid (pfn) != nid)) -continue ; + prev_pfn = pfn -PAGES_PER_SUBSECTION ; + head_pfn = prev_pfn +1 ; -if (zone !=page_zone (pfn_to_page (pfn))) +if (unlikely (!pfn_to_online_page (pfn)))continue ; -return pfn;mm/memory_hotplug.c static unsigned long find_biggest_section_pfn(int nid, struct zone *zone, · line 480
Quoted 10 lines
mm/memory_hotplug.c +7
-0+if (likely (pfn_to_nid (pfn) == nid) && + zone ==page_zone (pfn_to_page (pfn))) +return pfn; + +if (likely (pfn_to_nid (head_pfn) == nid) && + zone ==page_zone (pfn_to_page (head_pfn))) +return pfn; }return 0 ;I think improving that should be patch #1.
But I think we can do much better code-wise. What about the following cleanup instead:
Quoted 1 line
From
6e9ea094daf8d04c0b119b780572a846334f8164Mon Sep 17 00:00:00 2001From: "David Hildenbrand (Arm)" <[email protected]>
Date: Wed, 5 Aug 2026 12:52:55 +0200
Subject: [PATCH]mm/memory_hotplug: makeshrink_zone_span()more robustLet's make
shrink_zone_span()more robust by checking infind_smallest_section_pfn()/find_biggest_section_pfn()that start+end of the subsection.While at it, clean up the function heavily, factoring the core check out into
subsection_overlaps_zone().There likely is no need to check for the nid first: we require
SPARSEMEM_VMEMMAP_ENABLEwherepfn_to_page()is cheap, and thepfn_to_nid()onCONFIG_NUMAwould do apfn_to_page()either way. So let's just drop that for now.Signed-off-by David Hildenbrand (Arm) <[email protected]>---
mm/memory_hotplug.c| 59 ++++++++++++++++++---------------------------1 file changed, 24 insertions(+), 35 deletions(-)mm/memory_hotplug.c +6
-9@@ -425,49 +425,39 @@ int __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,return err; } -/* find the smallest valid pfn in the range [start_pfn, end_pfn) */ -static unsigned long find_smallest_section_pfn (int nid,struct zone *zone, -unsigned long start_pfn, -unsigned long end_pfn) +static bool subsection_overlaps_zone (unsigned long pfn,struct zone *zone) { -for (; start_pfn < end_pfn; start_pfn +=PAGES_PER_SUBSECTION ) { -if (unlikely (!pfn_to_online_page (start_pfn))) -continue ; +const unsigned long start_pfn =ALIGN_DOWN (pfn,PAGES_PER_SUBSECTION ); +const unsigned long end_pfn = start_pfn +PAGES_PER_SUBSECTION -1 ; -if (unlikely (pfn_to_nid (start_pfn) != nid)) -continue ; +/* All pages in a subsection are either online or offline. */ +if (unlikely (!pfn_to_online_page (start_pfn))) +return false;
Quoted 8 lines
mm/memory_hotplug.c int __add_pages(int nid, unsigned long pfn, unsigned long nr_pages, · line 440
Quoted 6 lines
mm/memory_hotplug.c +4
-2-if (zone !=page_zone (pfn_to_page (start_pfn))) -continue ; +/* Checking start+end is sufficient. */ +return zone ==page_zone (pfn_to_page (start_pfn)) || + zone ==page_zone (pfn_to_page (end_pfn)); +}Hi, David
Hi,
Quoted 8 lines
I am thinking if we could initialize each subsection to only one zone, we may reduce the complexity?
For example, in
init_unavailable_range()align spfn to subsection and init the head unaligned part to previous zone?Do you think this is reasonable?
I'm afraid of opening up another can of worms. :)
The additional page_zone(pfn_to_page(end_pfn) check is cheap with VMEMMAP, so I think we should focus on that first.
(and the patch is in general a cleanup :) )
Signature · 3 lines
Cheers, David
Hi David,
The reworked implementation updates pages_with_online_memmap in memmap_init_zone_range(), counting only PFNs that satisfy pfn_to_online_page(). This eliminates the extra PFN range walk used in the previous version.
Does this look like the right direction, or am I missing something? I'd appreciate your thoughts.
Thanks!
Best Regards,
Liu, Yuan1
Quoted 123 lines
-----Original Message-----
From: Liu, Yuan1 <[email protected]>
Sent: Thursday, July 23, 2026 4:50 PM
To: David Hildenbrand <[email protected]>; Oscar Salvador <[email protected]>; Mike Rapoport <[email protected]>; Wei Yang <[email protected]>
Cc: [email protected]; Zou, Nanhai <[email protected]>; Liu, Yuan1 <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; Zeng, Jason <[email protected]>; [email protected]
Subject: [PATCH v6 0/2]mm/memory_hotplug: optimize zone contiguous check when changing pfn rangeThis series introduces a
pages_with_online_memmapmember into struct zone to avoid pageblock-by-pageblock scans across the entire zone and improve memory hotplug performance.Reworked how
pages_with_online_memmapaccounts for PFNs in early boot memory in response to Sashiko's review comments [2], which identified potential issues with the previous implementation.Approach
========
Add a new zone memberpages_with_online_memmapthat tracks the number of pages within the zone span that have an online memory map (including present pages and memory holes smaller than a subsection whose memory map has been initialized). Whenspanned_pages==pages_with_online_memmapthe zone is contiguous andpfn_to_page()can be called on any PFN in the zone span without furtherpfn_valid()checks.For early boot memory,
pages_with_online_memmapis calculated inmemmap_init_zone_range(). Since every PFN within a memblock region satisfiespfn_to_online_page(), the calculation aligns both boundaries of each memblock range toPAGES_PER_SUBSECTIONand counts the aligned pages that fall within the zone span. If two adjacent memblock regions share a subsection (i.e. the hole between them is smaller than a subsection), the overlapping subsection pages are counted only once. For hotplugged memory,pages_with_online_memmapis updated throughadjust_present_page_count(), which is called during memory online and offline.Only pages that fall within the current zone span are accounted towards
pages_with_online_memmap. A "too small" value is safe, it merely prevents detecting a contiguous zone.The contiguity check using
pages_with_online_memmapis stricter than the old pageblock-by-pageblock scan. The oldset_zone_contiguous()iterated at pageblock granularity viapageblock_pfn_to_page(), so a zone could be marked contiguous even if a subsection-sized hole existed within a pageblock. The new check requiresspanned_pages==pages_with_online_memmap,
meaning every PFN in the zone span must satisfypfn_to_online_page().Performance
===========
1. For VM hotplug performance data, please refer to Patch 1.
2. This series also benefits CXL hotplug. Performance results are as follows
https://lore.kernel.org/all/20260409023552.GA2807@AE/Tested cases
============
1. Hotplug/unplug correctness with bothonline_movableand online policies, including partial unplug, middle-block offline gaps, and edge-block span shrink.
2. Large scale (256G/512G) plug/unplug performance.
3. Boot-time subsection holes (aligned/unaligned, in-zone and cross-zone) with correctpages_with_online_memmapaccounting.
4. kernelcore=mirror: verified no overcounting.Patch overview
==============
This series is based on the kernelcore-mirror branch [1].Patch 1 introduces
pages_with_online_memmapto replace pageblock-by-pageblock scans across the entire zone for zone contiguity checks.Patch 2 avoids incorrectly shrinking the zone span during memory unplug when a memory or hole boundary falls in the middle of a subsection.
v6 changes:
1. Dropped patches 1-3 from v5. The kernelcore=mirror overlap issue that causedpages_with_online_memmapto be overcounted has been resolved by the series [1].
2. Reworkedpages_with_online_memmapaccounting during early boot.v5:
https://lore.kernel.org/linux-mm/20260520093457.3719960-1-v4:
https://lore.kernel.org/linux-mm/20260421125508.2317429-1-v3:
https://lore.kernel.org/linux-mm/20260408031615.1831922-1-[1]
https://git.kernel.org/pub/scm/linux/kernel/git/rppt/memblock.git/log/?h=k
ernelcore-mirror
[2] https://sashiko.dev/#/patchset/20260520093457.3719960-3-
[email protected]Yuan Liu (2):
mm/memory_hotplug: optimize zone contiguous check when changing pfn
range
mm/memory_hotplug: improveshrink_zone_span()subsection boundary
checksDocumentation/mm/physical_memory.rst | 13 +++++++ drivers/base/memory.c | 6 +++ include/linux/mmzone.h | 47 ++++++++++++++++++++++ mm/internal.h | 8 +--- mm/memory_hotplug.c | 54 ++++++++++++++------------ mm/mm_init.c | 58 +++++++++++++++++----------- 6 files changed, 132 insertions(+), 54 deletions(-)base-commit:
b69c2a1fa9beb4c3db24b4013e07f5cb7e7260aa--
2.47.3