Skip to contentSkip to conversation

mm/memory hotplug/unplug: Optimize zone contiguous check when changing pfn range

PATCH Superseded Yuan Liu 4 participants 23 messages Apr 8 – Apr 28

Subsystems (8)

v3 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Yuan Liu

author
#0 lore raw

PATCH v3 1/1 mm/memory hotplug/unplug: 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 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.

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 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                         | 42 ++++++++++---------------
 6 files changed, 86 insertions(+), 42 deletions(-)
Documentation/mm/physical_memory.rst +13 -0
index b76183545e5b..0aa65e6b5499 100644
@@ -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
index a3091924918b..2b6b4e5508af 100644
@@ -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
index cb0af847d7d9..92fee035c3f2 100644
@@ -793,21 +793,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
index bc805029da51..3f73fcb042cf 100644
@@ -565,18 +565,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);
 }
 
 /**
@@ -753,8 +748,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);
@@ -782,8 +775,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 {
@@ -1079,6 +1070,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 +17 -25

67 lines not on this page · raw message

Signature · 1 line
2.47.3
#1 lore raw
Quoted 77 lines · Yuan Liu

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 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.

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 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.

---

[...]

Quoted 23 lines
mm/mm_init.c +3 -1
@@ -842,7 +842,7 @@ overlap_memmap_init(unsigned long zone, unsigned long *pfn)
  *   zone/node above the hole except for the trailing pages in the last
  *   section that will be appended to the zone/node below.
  */
-static void __init init_unavailable_range(unsigned long spfn,
+static unsigned long __init init_unavailable_range(unsigned long spfn,
 					  unsigned long epfn,
 					  int zone, int node)
 {
@@ -858,6 +858,7 @@ static void __init init_unavailable_range(unsigned long spfn,
 	if (pgcnt)
 		pr_info("On node %d, zone %s: %lld pages in unavailable ranges\n",
 			node, zone_names[zone], pgcnt);
+	return pgcnt;
 }
 
 /*
@@ -956,9 +957,22 @@ static void __init memmap_init_zone_range(struct zone *zone,
 	memmap_init_range(end_pfn - start_pfn, nid, zone_id, start_pfn,
 			  zone_end_pfn, MEMINIT_EARLY, NULL, MIGRATE_MOVABLE,
 			  false);
+	zone->pages_with_online_memmap += end_pfn - start_pfn;
 

mm/mm_init.c static void __init memmap_init_zone_range(struct zone *zone, · line 969

Quoted 10 lines
mm/mm_init.c +8 -2
-	if (*hole_pfn < start_pfn)
-		init_unavailable_range(*hole_pfn, start_pfn, zone_id, nid);
+	if (*hole_pfn < start_pfn) {
+		unsigned long pgcnt;
+
+		if (*hole_pfn < zone_start_pfn) {
+			init_unavailable_range(*hole_pfn, zone_start_pfn,
+					       zone_id, nid);
+			pgcnt = init_unavailable_range(zone_start_pfn,
+					start_pfn, zone_id, nid);

Indentation of parameters.

mm/mm_init.c static void __init memmap_init_zone_range(struct zone *zone, · line 972

Quoted 3 lines
mm/mm_init.c +3 -0
+		} else {
+			pgcnt = init_unavailable_range(*hole_pfn, start_pfn,
+					zone_id, nid);

Same here.

mm/mm_init.c static void __init memmap_init_zone_range(struct zone *zone, · line 975

Quoted 3 lines
mm/mm_init.c +3 -0
+		}
+		zone->pages_with_online_memmap += pgcnt;
+	}

Maybe something like the following could make it nicer to read, just a thought.

unsigned long hole_start_pfn = *hole_pfn;

if (hole_start_pfn < zone_start_pfn) {
	init_unavailable_range(hole_start_pfn, zone_start_pfn,
			       zone_id, nid);
	hole_start_pfn = zone_start_pfn;
}
pgcnt = init_unavailable_range(hole_start_pfn, start_pfn,
			       zone_id, nid);

LGTM, thanks!

Signature · 3 lines
Cheers,

David
author
#2 lore raw
Quoted 139 lines

-----Original Message-----
From: David Hildenbrand (Arm) <[email protected]>
Sent: Wednesday, April 8, 2026 3:36 PM
To: Liu, Yuan1 <[email protected]>; Oscar Salvador <[email protected]>; Mike Rapoport <[email protected]>; Wei Yang <[email protected]>
Cc: [email protected]; Hu, Yong <[email protected]>; Zou, Nanhai <[email protected]>; Tim Chen <[email protected]>; Zhuo, Qiuxu <[email protected]>; Chen, Yu C <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; [email protected]
Subject: Re: [PATCH v3] mm/memory hotplug/unplug: Optimize zone contiguous check when changing pfn range

Quoted 2 lines · Yuan Liu

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

Quoted 5 lines

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

Quoted 7 lines

pages and memory holes 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.

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

Quoted 4 lines

detecting a contiguous zone.

The following test cases of memory hotplug for a VM [1], tested in the environment [2], show that this optimization can significantly reduce

the

Quoted 3 lines

memory hotplug time [3].

+----------------+------+---------------+--------------+----------------

+

Quoted 1 line

| | Size | Time (before) | Time (after) | Time Reduction

|

Quoted 1 line

| +------+---------------+--------------+----------------

+

Quoted 1 line

| Plug Memory | 256G | 10s | 3s | 70%

|

Quoted 1 line

| +------+---------------+--------------+----------------

+

Quoted 1 line

| | 512G | 36s | 7s | 81%

|

Quoted 1 line

+----------------+------+---------------+--------------+----------------

+

Quoted 2 lines

+----------------+------+---------------+--------------+----------------

+

Quoted 1 line

| | Size | Time (before) | Time (after) | Time Reduction

|

Quoted 1 line

| +------+---------------+--------------+----------------

+

Quoted 1 line

| Unplug Memory | 256G | 11s | 4s | 64%

|

Quoted 1 line

| +------+---------------+--------------+----------------

+

Quoted 1 line

| | 512G | 36s | 9s | 75%

|

Quoted 1 line

+----------------+------+---------------+--------------+----------------

+

Quoted 42 lines

[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.

---

[...]

Review overlap_memmap_init(unsigned long zone, unsigned

Quoted 1 line
(quoted diff) +0 -0
@@ -842,7 +842,7 @@ overlap_memmap_init(unsigned long zone, unsigned

long *pfn)

mm/mm_init.c static void __init init_unavailable_range(unsigned

Quoted 9 lines
mm/mm_init.c +1 -1
  *   zone/node above the hole except for the trailing pages in the last
  *   section that will be appended to the zone/node below.
  */
-static void __init init_unavailable_range(unsigned long spfn,
+static unsigned long __init init_unavailable_range(unsigned long spfn,
 					  unsigned long epfn,
 					  int zone, int node)
 {
@@ -858,6 +858,7 @@ static void __init init_unavailable_range(unsigned

long spfn,

Review static void __init init_unavailable_range(unsigned · line 859

Quoted 2 lines
(quoted diff) +0 -0
 	if (pgcnt)
 		pr_info("On node %d, zone %s: %lld pages in unavailable

ranges\n",

mm/mm_init.c static void __init memmap_init_zone_range(struct

Quoted 6 lines
mm/mm_init.c +1 -0
 			node, zone_names[zone], pgcnt);
+	return pgcnt;
 }
 
 /*
@@ -956,9 +957,22 @@ static void __init memmap_init_zone_range(struct

zone *zone,

Quoted 5 lines
mm/mm_init.c +1 -0
 	memmap_init_range(end_pfn - start_pfn, nid, zone_id, start_pfn,
 			  zone_end_pfn, MEMINIT_EARLY, NULL, MIGRATE_MOVABLE,
 			  false);
+	zone->pages_with_online_memmap += end_pfn - start_pfn;
 
Quoted 12 lines

mm/mm_init.c static void __init memmap_init_zone_range(struct zone *zone, · line 969

Quoted 10 lines
mm/mm_init.c +8 -2
-	if (*hole_pfn < start_pfn)
-		init_unavailable_range(*hole_pfn, start_pfn, zone_id, nid);
+	if (*hole_pfn < start_pfn) {
+		unsigned long pgcnt;
+
+		if (*hole_pfn < zone_start_pfn) {
+			init_unavailable_range(*hole_pfn, zone_start_pfn,
+					       zone_id, nid);
+			pgcnt = init_unavailable_range(zone_start_pfn,
+					start_pfn, zone_id, nid);

Indentation of parameters.

Got it, I'll fix the indentation.

Quoted 7 lines

mm/mm_init.c static void __init memmap_init_zone_range(struct zone *zone, · line 972

Quoted 4 lines
mm/mm_init.c +3 -0
+		} else {
+			pgcnt = init_unavailable_range(*hole_pfn, start_pfn,
+					zone_id, nid);

Same here.

Sure

Quoted 10 lines

mm/mm_init.c static void __init memmap_init_zone_range(struct zone *zone, · line 975

Quoted 3 lines
mm/mm_init.c +3 -0
+		}
+		zone->pages_with_online_memmap += pgcnt;
+	}

Maybe something like the following could make it nicer to read, just a thought.

unsigned long hole_start_pfn = *hole_pfn;

Quoted 7 lines
if (hole_start_pfn < zone_start_pfn) {
	init_unavailable_range(hole_start_pfn, zone_start_pfn,
			       zone_id, nid);
	hole_start_pfn = zone_start_pfn;
}
pgcnt = init_unavailable_range(hole_start_pfn, start_pfn,
			       zone_id, nid);

Yes, this looks better. I'll apply your suggestion

Quoted 2 lines

LGTM, thanks!

Thanks for the feedback, I'll include these changes in the next version

Quoted 4 lines

--
Cheers,

David

#3 lore raw
Quoted 15 lines
Quoted 7 lines

Maybe something like the following could make it nicer to read, just a thought.

unsigned long hole_start_pfn = *hole_pfn;

Quoted 7 lines
if (hole_start_pfn < zone_start_pfn) {
	init_unavailable_range(hole_start_pfn, zone_start_pfn,
			       zone_id, nid);
	hole_start_pfn = zone_start_pfn;
}
pgcnt = init_unavailable_range(hole_start_pfn, start_pfn,
			       zone_id, nid);
Quoted 1 line

Yes, this looks better. I'll apply your suggestion

Best to wait for Mike's comments first! :)

Signature · 3 lines
Cheers,

David
author
#4 lore raw
Quoted 28 lines

-----Original Message-----
From: David Hildenbrand (Arm) <[email protected]>
Sent: Wednesday, April 8, 2026 8:31 PM
To: Liu, Yuan1 <[email protected]>; Oscar Salvador <[email protected]>; Mike Rapoport <[email protected]>; Wei Yang <[email protected]>
Cc: [email protected]; Hu, Yong <[email protected]>; Zou, Nanhai <[email protected]>; Tim Chen <[email protected]>; Zhuo, Qiuxu <[email protected]>; Chen, Yu C <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; [email protected]
Subject: Re: [PATCH v3] mm/memory hotplug/unplug: Optimize zone contiguous check when changing pfn range

Quoted 14 lines
Quoted 13 lines

Maybe something like the following could make it nicer to read, just a thought.

unsigned long hole_start_pfn = *hole_pfn;

if (hole_start_pfn < zone_start_pfn) {
	init_unavailable_range(hole_start_pfn, zone_start_pfn,
			       zone_id, nid);
	hole_start_pfn = zone_start_pfn;
}
pgcnt = init_unavailable_range(hole_start_pfn, start_pfn,
			       zone_id, nid);
Quoted 3 lines
Quoted 1 line

Yes, this looks better. I'll apply your suggestion

Best to wait for Mike's comments first! :)

Sure, I'll wait for Mike's comments before sending the next version.

Quoted 4 lines

--
Cheers,

David

maintainer
#5 lore raw
In reply to David Hildenbrand (Arm)
Quoted 135 lines · David Hildenbrand (Arm)
Quoted 77 lines · Yuan Liu

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 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.

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 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.

---

[...]

Quoted 23 lines
mm/mm_init.c +3 -1
@@ -842,7 +842,7 @@ overlap_memmap_init(unsigned long zone, unsigned long *pfn)
  *   zone/node above the hole except for the trailing pages in the last
  *   section that will be appended to the zone/node below.
  */
-static void __init init_unavailable_range(unsigned long spfn,
+static unsigned long __init init_unavailable_range(unsigned long spfn,
 					  unsigned long epfn,
 					  int zone, int node)
 {
@@ -858,6 +858,7 @@ static void __init init_unavailable_range(unsigned long spfn,
 	if (pgcnt)
 		pr_info("On node %d, zone %s: %lld pages in unavailable ranges\n",
 			node, zone_names[zone], pgcnt);
+	return pgcnt;
 }
 
 /*
@@ -956,9 +957,22 @@ static void __init memmap_init_zone_range(struct zone *zone,
 	memmap_init_range(end_pfn - start_pfn, nid, zone_id, start_pfn,
 			  zone_end_pfn, MEMINIT_EARLY, NULL, MIGRATE_MOVABLE,
 			  false);
+	zone->pages_with_online_memmap += end_pfn - start_pfn;
 

mm/mm_init.c static void __init memmap_init_zone_range(struct zone *zone, · line 969

Quoted 10 lines
mm/mm_init.c +8 -2
-	if (*hole_pfn < start_pfn)
-		init_unavailable_range(*hole_pfn, start_pfn, zone_id, nid);
+	if (*hole_pfn < start_pfn) {
+		unsigned long pgcnt;
+
+		if (*hole_pfn < zone_start_pfn) {
+			init_unavailable_range(*hole_pfn, zone_start_pfn,
+					       zone_id, nid);
+			pgcnt = init_unavailable_range(zone_start_pfn,
+					start_pfn, zone_id, nid);

Indentation of parameters.

mm/mm_init.c static void __init memmap_init_zone_range(struct zone *zone, · line 972

Quoted 3 lines
mm/mm_init.c +3 -0
+		} else {
+			pgcnt = init_unavailable_range(*hole_pfn, start_pfn,
+					zone_id, nid);

Same here.

mm/mm_init.c static void __init memmap_init_zone_range(struct zone *zone, · line 975

Quoted 3 lines
mm/mm_init.c +3 -0
+		}
+		zone->pages_with_online_memmap += pgcnt;
+	}

Maybe something like the following could make it nicer to read, just a thought.

unsigned long hole_start_pfn = *hole_pfn;

Quoted 8 lines
if (hole_start_pfn < zone_start_pfn) {
	init_unavailable_range(hole_start_pfn, zone_start_pfn,
			       zone_id, nid);
	hole_start_pfn = zone_start_pfn;
}
pgcnt = init_unavailable_range(hole_start_pfn, start_pfn,
			       zone_id, nid);

Yeah, this looks better :)

sashiko had several comments
https://sashiko.dev/#/patchset/20260408031615.1831922-1-yuan1.liu%40intel.com

I skipped the ones related to hotplug, but in the mm_init part the comment about zones that can have overlapping physical spans when mirrored kernelcore is enabled seems valid.

Quoted 3 lines
Signature · 2 lines
Cheers,
David
Signature · 2 lines
Sincerely yours,
Mike.
#6 lore raw
Quoted 145 lines · Mike Rapoport
Quoted 135 lines · David Hildenbrand (Arm)
Quoted 77 lines · Yuan Liu

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 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.

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 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.

---

[...]

Quoted 23 lines
mm/mm_init.c +3 -1
@@ -842,7 +842,7 @@ overlap_memmap_init(unsigned long zone, unsigned long *pfn)
  *   zone/node above the hole except for the trailing pages in the last
  *   section that will be appended to the zone/node below.
  */
-static void __init init_unavailable_range(unsigned long spfn,
+static unsigned long __init init_unavailable_range(unsigned long spfn,
 					  unsigned long epfn,
 					  int zone, int node)
 {
@@ -858,6 +858,7 @@ static void __init init_unavailable_range(unsigned long spfn,
 	if (pgcnt)
 		pr_info("On node %d, zone %s: %lld pages in unavailable ranges\n",
 			node, zone_names[zone], pgcnt);
+	return pgcnt;
 }
 
 /*
@@ -956,9 +957,22 @@ static void __init memmap_init_zone_range(struct zone *zone,
 	memmap_init_range(end_pfn - start_pfn, nid, zone_id, start_pfn,
 			  zone_end_pfn, MEMINIT_EARLY, NULL, MIGRATE_MOVABLE,
 			  false);
+	zone->pages_with_online_memmap += end_pfn - start_pfn;
 

mm/mm_init.c static void __init memmap_init_zone_range(struct zone *zone, · line 969

Quoted 10 lines
mm/mm_init.c +8 -2
-	if (*hole_pfn < start_pfn)
-		init_unavailable_range(*hole_pfn, start_pfn, zone_id, nid);
+	if (*hole_pfn < start_pfn) {
+		unsigned long pgcnt;
+
+		if (*hole_pfn < zone_start_pfn) {
+			init_unavailable_range(*hole_pfn, zone_start_pfn,
+					       zone_id, nid);
+			pgcnt = init_unavailable_range(zone_start_pfn,
+					start_pfn, zone_id, nid);

Indentation of parameters.

mm/mm_init.c static void __init memmap_init_zone_range(struct zone *zone, · line 972

Quoted 3 lines
mm/mm_init.c +3 -0
+		} else {
+			pgcnt = init_unavailable_range(*hole_pfn, start_pfn,
+					zone_id, nid);

Same here.

mm/mm_init.c static void __init memmap_init_zone_range(struct zone *zone, · line 975

Quoted 3 lines
mm/mm_init.c +3 -0
+		}
+		zone->pages_with_online_memmap += pgcnt;
+	}

Maybe something like the following could make it nicer to read, just a thought.

unsigned long hole_start_pfn = *hole_pfn;

Quoted 8 lines
if (hole_start_pfn < zone_start_pfn) {
	init_unavailable_range(hole_start_pfn, zone_start_pfn,
			       zone_id, nid);
	hole_start_pfn = zone_start_pfn;
}
pgcnt = init_unavailable_range(hole_start_pfn, start_pfn,
			       zone_id, nid);
Quoted 8 lines

Yeah, this looks better :)

sashiko had several comments
https://sashiko.dev/#/patchset/20260408031615.1831922-1-yuan1.liu%40intel.com

I skipped the ones related to hotplug, but in the mm_init part the comment about zones that can have overlapping physical spans when mirrored kernelcore is enabled seems valid.

The set_zone_contiguous/clear_zone_contiguous can be ignored I think.

The comment about shrink_zone_span() is likely not realistic. shrink_zone_span() would not shrink over boot holes.

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.

No need to test pfn_to_online_page() twice, as that is the same result for all pages in a PAGES_PER_SUBSECTION chunk.

Signature · 3 lines
Cheers,

David
author
#7 lore raw
Quoted 192 lines

-----Original Message-----
From: David Hildenbrand (Arm) <[email protected]>
Sent: Thursday, April 9, 2026 11:08 PM
To: Mike Rapoport <[email protected]>
Cc: Liu, Yuan1 <[email protected]>; Oscar Salvador <[email protected]>; Wei Yang <[email protected]>; [email protected]; Hu, Yong <[email protected]>; Zou, Nanhai <[email protected]>; Tim Chen <[email protected]>; Zhuo, Qiuxu <[email protected]>; Chen, Yu C <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; linux- [email protected]
Subject: Re: [PATCH v3] mm/memory hotplug/unplug: Optimize zone contiguous check when changing pfn range

Quoted 3 lines · Mike Rapoport
Quoted 2 lines · David Hildenbrand (Arm)
Quoted 1 line · Yuan Liu

When move_pfn_range_to_zone() or remove_pfn_range_from_zone() updates

a

Quoted 1 line
Quoted 1 line
Quoted 1 line

zone, set_zone_contiguous() rescans the entire zone pageblock-by-

pageblock

Quoted 1 line
Quoted 1 line
Quoted 1 line

to rebuild zone->contiguous. For large zones this is a significant

cost

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

during memory hotplug and hot-unplug.

Add a new zone member pages_with_online_memmap that tracks the number

of

Quoted 1 line
Quoted 1 line
Quoted 1 line

pages within the zone span that have an online memory map (including

present

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

pages and memory holes 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

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

pfn_valid() checks.

Only pages that fall within the current zone span are accounted

towards

Quoted 1 line
Quoted 1 line
Quoted 1 line

pages_with_online_memmap. A "too small" value is safe, it merely

prevents

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

detecting a contiguous zone.

The following test cases of memory hotplug for a VM [1], tested in the environment [2], show that this optimization can significantly reduce

the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

memory hotplug time [3].

+----------------+------+---------------+--------------+--------------

--+

Quoted 1 line
Quoted 1 line
Quoted 1 line

| | Size | Time (before) | Time (after) | Time

Reduction |

Quoted 1 line
Quoted 1 line
Quoted 1 line

| +------+---------------+--------------+--------------

--+

Quoted 1 line
Quoted 1 line
Quoted 1 line

| Plug Memory | 256G | 10s | 3s | 70%

|

Quoted 1 line
Quoted 1 line
Quoted 1 line

| +------+---------------+--------------+--------------

--+

Quoted 1 line
Quoted 1 line
Quoted 1 line

| | 512G | 36s | 7s | 81%

|

Quoted 1 line
Quoted 1 line
Quoted 1 line

+----------------+------+---------------+--------------+--------------

--+

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+----------------+------+---------------+--------------+--------------

--+

Quoted 1 line
Quoted 1 line
Quoted 1 line

| | Size | Time (before) | Time (after) | Time

Reduction |

Quoted 1 line
Quoted 1 line
Quoted 1 line

| +------+---------------+--------------+--------------

--+

Quoted 1 line
Quoted 1 line
Quoted 1 line

| Unplug Memory | 256G | 11s | 4s | 64%

|

Quoted 1 line
Quoted 1 line
Quoted 1 line

| +------+---------------+--------------+--------------

--+

Quoted 1 line
Quoted 1 line
Quoted 1 line

| | 512G | 36s | 9s | 75%

|

Quoted 1 line
Quoted 1 line
Quoted 1 line

+----------------+------+---------------+--------------+--------------

--+

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

[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
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
    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

\

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
    -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

Quoted 1 line
Quoted 1 line
Quoted 1 line

'grep MemTotal /proc/meminfo' on Guest reflects that all

hotplugged

Quoted 16 lines
Quoted 16 lines
Quoted 15 lines

memory is recognized.

---

Quoted 3 lines
Quoted 3 lines

[...]

Review overlap_memmap_init(unsigned long zone, unsigned

Quoted 1 line
(quoted diff) +0 -0
@@ -842,7 +842,7 @@ overlap_memmap_init(unsigned long zone, unsigned

long *pfn)

Quoted 1 line
Quoted 1 line

Review overlap_memmap_init(unsigned long zone, unsigned · line 842

Quoted 1 line
(quoted diff) +0 -0
  *   zone/node above the hole except for the trailing pages in the

last

Quoted 4 lines
Quoted 4 lines

mm/mm_init.c overlap_memmap_init(unsigned long zone, unsigned long *pfn)

Quoted 4 lines
mm/mm_init.c +1 -1
  *   section that will be appended to the zone/node below.
  */
-static void __init init_unavailable_range(unsigned long spfn,
+static unsigned long __init init_unavailable_range(unsigned long

spfn,

Quoted 4 lines
Quoted 4 lines

mm/mm_init.c static void __init init_unavailable_range(unsigned

Quoted 4 lines
mm/mm_init.c +0 -0
 					  unsigned long epfn,
 					  int zone, int node)
 {
@@ -858,6 +858,7 @@ static void __init init_unavailable_range(unsigned

long spfn,

Quoted 2 lines
Quoted 2 lines

Review static void __init init_unavailable_range(unsigned · line 859

Quoted 2 lines
(quoted diff) +0 -0
 	if (pgcnt)
 		pr_info("On node %d, zone %s: %lld pages in unavailable

ranges\n",

Quoted 6 lines
Quoted 6 lines

mm/mm_init.c static void __init memmap_init_zone_range(struct

Quoted 6 lines
mm/mm_init.c +1 -0
 			node, zone_names[zone], pgcnt);
+	return pgcnt;
 }
 
 /*
@@ -956,9 +957,22 @@ static void __init memmap_init_zone_range(struct

zone *zone,

Quoted 45 lines
Quoted 44 lines
Quoted 5 lines
mm/mm_init.c +1 -0
 	memmap_init_range(end_pfn - start_pfn, nid, zone_id, start_pfn,
 			  zone_end_pfn, MEMINIT_EARLY, NULL, MIGRATE_MOVABLE,
 			  false);
+	zone->pages_with_online_memmap += end_pfn - start_pfn;
 

mm/mm_init.c static void __init memmap_init_zone_range(struct zone *zone, · line 969

Quoted 10 lines
mm/mm_init.c +8 -2
-	if (*hole_pfn < start_pfn)
-		init_unavailable_range(*hole_pfn, start_pfn, zone_id, nid);
+	if (*hole_pfn < start_pfn) {
+		unsigned long pgcnt;
+
+		if (*hole_pfn < zone_start_pfn) {
+			init_unavailable_range(*hole_pfn, zone_start_pfn,
+					       zone_id, nid);
+			pgcnt = init_unavailable_range(zone_start_pfn,
+					start_pfn, zone_id, nid);

Indentation of parameters.

mm/mm_init.c static void __init memmap_init_zone_range(struct zone *zone, · line 972

Quoted 3 lines
mm/mm_init.c +3 -0
+		} else {
+			pgcnt = init_unavailable_range(*hole_pfn, start_pfn,
+					zone_id, nid);

Same here.

mm/mm_init.c static void __init memmap_init_zone_range(struct zone *zone, · line 975

Quoted 3 lines
mm/mm_init.c +3 -0
+		}
+		zone->pages_with_online_memmap += pgcnt;
+	}

Maybe something like the following could make it nicer to read, just a thought.

unsigned long hole_start_pfn = *hole_pfn;

if (hole_start_pfn < zone_start_pfn) {
	init_unavailable_range(hole_start_pfn, zone_start_pfn,
			       zone_id, nid);
	hole_start_pfn = zone_start_pfn;
}
pgcnt = init_unavailable_range(hole_start_pfn, start_pfn,
			       zone_id, nid);
Quoted 10 lines
Quoted 4 lines

Yeah, this looks better :)

sashiko had several comments
https://sashiko.dev/#/patchset/20260408031615.1831922-1-

yuan1.liu%40intel.com

Quoted 2 lines

I skipped the ones related to hotplug, but in the mm_init part the

comment

Quoted 2 lines

about zones that can have overlapping physical spans when mirrored kernelcore is enabled seems valid.

Hi David & Mike

I’ve spent some time working through these issues to better understand them. For the overlapping physical spans(mirrored kernelcore), should I avoid counting overlap_memmap_init in memmap_init_range in the next version?
For example, change it as follows:

+unsigned long __meminit
+memmap_init_range(unsigned long size, int nid, unsigned long zone,
+ unsigned long start_pfn,
+ unsigned long zone_end_pfn,
enum meminit_context context,
struct vmem_altmap *altmap, int migratetype, bool isolate_pageblock)

 {
        unsigned long pfn, end_pfn = start_pfn + size;

+ unsigned long nr_init = 0;
struct page *page;

if (highest_memmap_pfn < end_pfn - 1)
@@ -893,7 +897,7 @@ void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone

        if (zone == ZONE_DEVICE) {
                if (!altmap)

- return;
+ return 0;

                if (start_pfn == altmap->base_pfn)
                        start_pfn += altmap->reserve;
@@ -911,6 +915,7 @@ void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone
                        if (defer_init(nid, pfn, zone_end_pfn)) {
                                deferred_struct_pages = true;
+                               nr_init += end_pfn - pfn;
                                break;
                        }
                }
@@ -935,8 +940,10 @@ void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone
                        cond_resched();
                }
+               nr_init++;
                pfn++;
        }

+ return nr_init;
}

@ -954,13 +962,21 @@ static void __init memmap_init_zone_range(struct zone *zone,
if (start_pfn >= end_pfn)
return;
+ nr_pages = memmap_init_range(end_pfn - start_pfn, nid, zone_id,
+ start_pfn, zone_end_pfn, MEMINIT_EARLY,
+ NULL, MIGRATE_MOVABLE, false);
+ zone->pages_with_online_memmap += nr_pages;
+

Quoted 10 lines

The set_zone_contiguous/clear_zone_contiguous can be ignored I think.

The comment about shrink_zone_span() is likely not realistic. shrink_zone_span() would not shrink over boot holes.

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.

Quoted 7 lines

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.

No need to test pfn_to_online_page() twice, as that is the same result for all pages in a PAGES_PER_SUBSECTION chunk.

Hi David

I think I understand your point.
Just to confirm, should the improvement to shrink_zone_span be included in this patch?

Quoted 4 lines

--
Cheers,

David

#8 lore raw
Quoted 88 lines · On 4/14/26 09:06, Liu, Yuan1 wrote:
Quoted 48 lines

-----Original Message-----
From: David Hildenbrand (Arm) <[email protected]>
Sent: Thursday, April 9, 2026 11:08 PM
To: Mike Rapoport <[email protected]>
Cc: Liu, Yuan1 <[email protected]>; Oscar Salvador <[email protected]>; Wei Yang <[email protected]>; [email protected]; Hu, Yong <[email protected]>; Zou, Nanhai <[email protected]>; Tim Chen <[email protected]>; Zhuo, Qiuxu <[email protected]>; Chen, Yu C <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; linux- [email protected]
Subject: Re: [PATCH v3] mm/memory hotplug/unplug: Optimize zone contiguous check when changing pfn range

On 4/9/26 16:40, Mike Rapoport wrote:
a
pageblock
cost
of
present
further
towards
prevents
the
--+
Reduction |
--+
|
--+
|
--+
--+
Reduction |
--+
|
--+
|
--+
\
of
hotplugged
long *pfn)
last
spfn,
long spfn,
ranges\n",
zone *zone,

Quoted 1 line
Quoted 10 lines
Quoted 4 lines

Yeah, this looks better :)

sashiko had several comments
https://sashiko.dev/#/patchset/20260408031615.1831922-1-

yuan1.liu%40intel.com

Quoted 2 lines

I skipped the ones related to hotplug, but in the mm_init part the

comment

Quoted 2 lines

about zones that can have overlapping physical spans when mirrored kernelcore is enabled seems valid.

Hi David & Mike

I’ve spent some time working through these issues to better understand them. For the overlapping physical spans(mirrored kernelcore), should I avoid counting overlap_memmap_init in memmap_init_range in the next version?
For example, change it as follows:

(quoted diff) +6 -1
+unsigned long __meminit
+memmap_init_range(unsigned long size, int nid, unsigned long zone,
+               unsigned long start_pfn,
+               unsigned long zone_end_pfn,
                enum meminit_context context,
                struct vmem_altmap *altmap, int migratetype,
                bool isolate_pageblock)
 {
        unsigned long pfn, end_pfn = start_pfn + size;
+       unsigned long nr_init = 0;
        struct page *page;
 
        if (highest_memmap_pfn < end_pfn - 1)
@@ -893,7 +897,7 @@ void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone
        if (zone == ZONE_DEVICE) {
                if (!altmap)
-                       return;
+                       return 0;
 
                if (start_pfn == altmap->base_pfn)
                        start_pfn += altmap->reserve;

Review void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone · line 917

Quoted 3 lines
(quoted diff) +1 -0
@@ -911,6 +915,7 @@ void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone
                        if (defer_init(nid, pfn, zone_end_pfn)) {
                                deferred_struct_pages = true;
+                               nr_init += end_pfn - pfn;

It's confusing. Could the remaining range also include overlapping inits?

Maybe the whole "skip overlapping init" should actually be handled on a higher level?

I guess we'd want to skip any memblock_is_mirror(r) regions entirely.

@Mike?

Quoted 36 lines
(quoted diff) +2 -0
                                break;
                        }
                }
@@ -935,8 +940,10 @@ void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone
                        cond_resched();
                }
+               nr_init++;
                pfn++;
        }
+       return nr_init;
 }
 
 @ -954,13 +962,21 @@ static void __init memmap_init_zone_range(struct zone *zone,
        if (start_pfn >= end_pfn)
                return;
(quoted diff) +5 -0
+       nr_pages = memmap_init_range(end_pfn - start_pfn, nid, zone_id,
+                                    start_pfn, zone_end_pfn, MEMINIT_EARLY,
+                                    NULL, MIGRATE_MOVABLE, false);
+       zone->pages_with_online_memmap += nr_pages;
+
Quoted 16 lines

The set_zone_contiguous/clear_zone_contiguous can be ignored I think.

The comment about shrink_zone_span() is likely not realistic. shrink_zone_span() would not shrink over boot holes.

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.

Quoted 7 lines
Quoted 2 lines

No need to test pfn_to_online_page() twice, as that is the same result for all pages in a PAGES_PER_SUBSECTION chunk.

Hi David

I think I understand your point.
Just to confirm, should the improvement to shrink_zone_span be included in this patch?

I would do it in a separate patch that you move before this patch.

Signature · 3 lines
Cheers,

David
author
#9 lore raw
Quoted 47 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

sashiko had several comments
https://sashiko.dev/#/patchset/20260408031615.1831922-1-

yuan1.liu%40intel.com

Quoted 1 line
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

I skipped the ones related to hotplug, but in the mm_init part the

comment

Quoted 2 lines

about zones that can have overlapping physical spans when mirrored kernelcore is enabled seems valid.

Hi David & Mike

I’ve spent some time working through these issues to better understand

them.

Quoted 1 line

For the overlapping physical spans(mirrored kernelcore), should I avoid

counting

Review void __meminit memmap_init_range(unsigned long size,

Quoted 17 lines

overlap_memmap_init in memmap_init_range in the next version? For example, change it as follows:

(quoted diff) +5 -0
+unsigned long __meminit
+memmap_init_range(unsigned long size, int nid, unsigned long zone,
+               unsigned long start_pfn,
+               unsigned long zone_end_pfn,
                enum meminit_context context,
                struct vmem_altmap *altmap, int migratetype,
                bool isolate_pageblock)
 {
        unsigned long pfn, end_pfn = start_pfn + size;
+       unsigned long nr_init = 0;
        struct page *page;
 
        if (highest_memmap_pfn < end_pfn - 1)
@@ -893,7 +897,7 @@ void __meminit memmap_init_range(unsigned long size,

int nid, unsigned long zone

Review void __meminit memmap_init_range(unsigned long size,

Quoted 8 lines
(quoted diff) +1 -1
        if (zone == ZONE_DEVICE) {
                if (!altmap)
-                       return;
+                       return 0;
 
                if (start_pfn == altmap->base_pfn)
                        start_pfn += altmap->reserve;
@@ -911,6 +915,7 @@ void __meminit memmap_init_range(unsigned long size,

int nid, unsigned long zone

Review void __meminit memmap_init_range(unsigned long size, · line 917

Quoted 3 lines
(quoted diff) +1 -0
                        if (defer_init(nid, pfn, zone_end_pfn)) {
                                deferred_struct_pages = true;
+                               nr_init += end_pfn - pfn;
Quoted 8 lines

It's confusing. Could the remaining range also include overlapping inits?

Maybe the whole "skip overlapping init" should actually be handled on a higher level?

I guess we'd want to skip any memblock_is_mirror(r) regions entirely.

@Mike?

Hi Mike

David suggested moving the overlap handling to a higher level and skipping memblock_is_mirror() regions entirely. I think this makes sense.

Would this work for you, or do you have a different preference?

Something like this
static void __init memmap_init(void)
{
...

    for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
        struct pglist_data *node = NODE_DATA(nid);
        bool is_mirror = mirrored_kernelcore && 
				 memblock_is_mirror(&memblock.memory.regions[i]);

        for (j = 0; j < MAX_NR_ZONES; j++) {
        ...
	      if (is_mirror && j == ZONE_MOVABLE)
                continue;

           memmap_init_zone_range(zone, start_pfn, end_pfn,
                                  &hole_pfn);

Best Regards,
Liu, Yuan1

#10 lore raw
Quoted 46 lines · On 4/17/26 08:34, Liu, Yuan1 wrote:
Quoted 46 lines
Quoted 5 lines

Hi David & Mike

I’ve spent some time working through these issues to better understand

them.

Quoted 1 line

For the overlapping physical spans(mirrored kernelcore), should I avoid

counting

Review void __meminit memmap_init_range(unsigned long size,

Quoted 17 lines

overlap_memmap_init in memmap_init_range in the next version? For example, change it as follows:

(quoted diff) +5 -0
+unsigned long __meminit
+memmap_init_range(unsigned long size, int nid, unsigned long zone,
+               unsigned long start_pfn,
+               unsigned long zone_end_pfn,
                enum meminit_context context,
                struct vmem_altmap *altmap, int migratetype,
                bool isolate_pageblock)
 {
        unsigned long pfn, end_pfn = start_pfn + size;
+       unsigned long nr_init = 0;
        struct page *page;
 
        if (highest_memmap_pfn < end_pfn - 1)
@@ -893,7 +897,7 @@ void __meminit memmap_init_range(unsigned long size,

int nid, unsigned long zone

Review void __meminit memmap_init_range(unsigned long size,

Quoted 8 lines
(quoted diff) +1 -1
        if (zone == ZONE_DEVICE) {
                if (!altmap)
-                       return;
+                       return 0;
 
                if (start_pfn == altmap->base_pfn)
                        start_pfn += altmap->reserve;
@@ -911,6 +915,7 @@ void __meminit memmap_init_range(unsigned long size,

int nid, unsigned long zone

Review void __meminit memmap_init_range(unsigned long size, · line 917

Quoted 3 lines
(quoted diff) +1 -0
                        if (defer_init(nid, pfn, zone_end_pfn)) {
                                deferred_struct_pages = true;
+                               nr_init += end_pfn - pfn;

It's confusing. Could the remaining range also include overlapping inits?

Maybe the whole "skip overlapping init" should actually be handled on a higher level?

I guess we'd want to skip any memblock_is_mirror(r) regions entirely.

Quoted 8 lines
Quoted 1 line

@Mike?

Hi Mike

David suggested moving the overlap handling to a higher level and skipping memblock_is_mirror() regions entirely. I think this makes sense.

Would this work for you, or do you have a different preference?

Would that allow for removing overlap_memmap_init() entirely? The more I look at that function, the more I hate it with passion :D

Signature · 3 lines
Cheers,

David
author
#11 lore raw
Quoted 69 lines

-----Original Message-----
From: David Hildenbrand (Arm) <[email protected]>
Sent: Friday, April 17, 2026 5:00 PM
To: Liu, Yuan1 <[email protected]>; Mike Rapoport <[email protected]>
Cc: Oscar Salvador <[email protected]>; Wei Yang <[email protected]>; [email protected]; Hu, Yong <[email protected]>; Zou, Nanhai <[email protected]>; Tim Chen <[email protected]>; Zhuo, Qiuxu <[email protected]>; Chen, Yu C <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; linux- [email protected]
Subject: Re: [PATCH v3] mm/memory hotplug/unplug: Optimize zone contiguous check when changing pfn range

Quoted 7 lines · On 4/17/26 08:34, Liu, Yuan1 wrote:
Quoted 7 lines
Quoted 5 lines

Hi David & Mike

I’ve spent some time working through these issues to better understand

them.

Quoted 1 line

For the overlapping physical spans(mirrored kernelcore), should I

avoid

Quoted 16 lines
Quoted 16 lines

counting

Quoted 15 lines

overlap_memmap_init in memmap_init_range in the next version? For example, change it as follows:

(quoted diff) +5 -0
+unsigned long __meminit
+memmap_init_range(unsigned long size, int nid, unsigned long zone,
+               unsigned long start_pfn,
+               unsigned long zone_end_pfn,
                enum meminit_context context,
                struct vmem_altmap *altmap, int migratetype,
                bool isolate_pageblock)
 {
        unsigned long pfn, end_pfn = start_pfn + size;
+       unsigned long nr_init = 0;
        struct page *page;
Quoted 2 lines
Quoted 2 lines

Review void __meminit memmap_init_range(unsigned long

Quoted 2 lines

if (highest_memmap_pfn < end_pfn - 1)

(quoted diff) +0 -0
@@ -893,7 +897,7 @@ void __meminit memmap_init_range(unsigned long

size,

Quoted 9 lines
Quoted 9 lines

int nid, unsigned long zone

Review void __meminit memmap_init_range(unsigned long

Quoted 8 lines
(quoted diff) +1 -1
        if (zone == ZONE_DEVICE) {
                if (!altmap)
-                       return;
+                       return 0;
 
                if (start_pfn == altmap->base_pfn)
                        start_pfn += altmap->reserve;
@@ -911,6 +915,7 @@ void __meminit memmap_init_range(unsigned long

size,

Quoted 6 lines
Quoted 6 lines

int nid, unsigned long zone

Review void __meminit memmap_init_range(unsigned long · line 917

Quoted 3 lines
(quoted diff) +1 -0
                        if (defer_init(nid, pfn, zone_end_pfn)) {
                                deferred_struct_pages = true;
+                               nr_init += end_pfn - pfn;

It's confusing. Could the remaining range also include overlapping

inits?

Quoted 10 lines
Quoted 7 lines

Maybe the whole "skip overlapping init" should actually be handled on a higher level?

I guess we'd want to skip any memblock_is_mirror(r) regions entirely.

@Mike?

Hi Mike

Quoted 8 lines
Quoted 2 lines

David suggested moving the overlap handling to a higher level and skipping memblock_is_mirror() regions entirely. I think this makes

sense.

Quoted 2 lines

Would this work for you, or do you have a different preference?

Would that allow for removing overlap_memmap_init() entirely? The more I look at that function, the more I hate it with passion :D

Yes, I think overlap_memmap_init can be removed, since we no longer need to check mirror PFNs in memmap_init_range and instead can check mirror regions directly

Quoted 4 lines

--
Cheers,

David

maintainer
#12 lore raw
In reply to Liu, Yuan1
Quoted 54 lines · On Fri, Apr 17, 2026 at 06:34:50AM +0000, Liu, Yuan1 wrote:
Quoted 54 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

sashiko had several comments
https://sashiko.dev/#/patchset/20260408031615.1831922-1-

yuan1.liu%40intel.com

Quoted 1 line
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

I skipped the ones related to hotplug, but in the mm_init part the

comment

Quoted 2 lines

about zones that can have overlapping physical spans when mirrored kernelcore is enabled seems valid.

Hi David & Mike

I’ve spent some time working through these issues to better understand

them.

Quoted 1 line

For the overlapping physical spans(mirrored kernelcore), should I avoid

counting

Review void __meminit memmap_init_range(unsigned long size,

Quoted 17 lines

overlap_memmap_init in memmap_init_range in the next version? For example, change it as follows:

(quoted diff) +5 -0
+unsigned long __meminit
+memmap_init_range(unsigned long size, int nid, unsigned long zone,
+               unsigned long start_pfn,
+               unsigned long zone_end_pfn,
                enum meminit_context context,
                struct vmem_altmap *altmap, int migratetype,
                bool isolate_pageblock)
 {
        unsigned long pfn, end_pfn = start_pfn + size;
+       unsigned long nr_init = 0;
        struct page *page;
 
        if (highest_memmap_pfn < end_pfn - 1)
@@ -893,7 +897,7 @@ void __meminit memmap_init_range(unsigned long size,

int nid, unsigned long zone

Review void __meminit memmap_init_range(unsigned long size,

Quoted 8 lines
(quoted diff) +1 -1
        if (zone == ZONE_DEVICE) {
                if (!altmap)
-                       return;
+                       return 0;
 
                if (start_pfn == altmap->base_pfn)
                        start_pfn += altmap->reserve;
@@ -911,6 +915,7 @@ void __meminit memmap_init_range(unsigned long size,

int nid, unsigned long zone

Review void __meminit memmap_init_range(unsigned long size, · line 917

Quoted 3 lines
(quoted diff) +1 -0
                        if (defer_init(nid, pfn, zone_end_pfn)) {
                                deferred_struct_pages = true;
+                               nr_init += end_pfn - pfn;

It's confusing. Could the remaining range also include overlapping inits?

Maybe the whole "skip overlapping init" should actually be handled on a higher level?

I guess we'd want to skip any memblock_is_mirror(r) regions entirely.

Quoted 8 lines
Quoted 1 line

@Mike?

Hi Mike

David suggested moving the overlap handling to a higher level and skipping memblock_is_mirror() regions entirely. I think this makes sense.

Would this work for you, or do you have a different preference?

Looks about right :)

Quoted 8 lines

Something like this
static void __init memmap_init(void)
{
...

    for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
        struct pglist_data *node = NODE_DATA(nid);
        bool is_mirror = mirrored_kernelcore && 
				 memblock_is_mirror(&memblock.memory.regions[i]);

I'd add a local memblock_region variable.

Quoted 10 lines
        for (j = 0; j < MAX_NR_ZONES; j++) {
        ...
	      if (is_mirror && j == ZONE_MOVABLE)
                continue;

           memmap_init_zone_range(zone, start_pfn, end_pfn,
                                  &hole_pfn);

Best Regards,
Liu, Yuan1

Signature · 2 lines
Sincerely yours,
Mike.
author
#13 lore raw
Quoted 79 lines

-----Original Message-----
From: Mike Rapoport <[email protected]>
Sent: Monday, April 20, 2026 10:03 PM
To: Liu, Yuan1 <[email protected]>
Cc: David Hildenbrand (Arm) <[email protected]>; Oscar Salvador <[email protected]>; Wei Yang <[email protected]>; linux- [email protected]; Hu, Yong <[email protected]>; Zou, Nanhai <[email protected]>; Tim Chen <[email protected]>; Zhuo, Qiuxu <[email protected]>; Chen, Yu C <[email protected]>; Deng, Pan <[email protected]>; Li, Tianyou <[email protected]>; Chen Zhang <[email protected]>; [email protected]
Subject: Re: [PATCH v3] mm/memory hotplug/unplug: Optimize zone contiguous check when changing pfn range

Quoted 5 lines · On Fri, Apr 17, 2026 at 06:34:50AM +0000, Liu, Yuan1 wrote:
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

sashiko had several comments
https://sashiko.dev/#/patchset/20260408031615.1831922-1-

yuan1.liu%40intel.com

Quoted 1 line
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

I skipped the ones related to hotplug, but in the mm_init part the

comment

Quoted 2 lines

about zones that can have overlapping physical spans when mirrored kernelcore is enabled seems valid.

Hi David & Mike

I’ve spent some time working through these issues to better

understand

Quoted 2 lines
Quoted 2 lines

them.

Quoted 1 line

For the overlapping physical spans(mirrored kernelcore), should I

avoid

Quoted 16 lines
Quoted 16 lines

counting

Quoted 15 lines

overlap_memmap_init in memmap_init_range in the next version? For example, change it as follows:

(quoted diff) +5 -0
+unsigned long __meminit
+memmap_init_range(unsigned long size, int nid, unsigned long zone,
+               unsigned long start_pfn,
+               unsigned long zone_end_pfn,
                enum meminit_context context,
                struct vmem_altmap *altmap, int migratetype,
                bool isolate_pageblock)
 {
        unsigned long pfn, end_pfn = start_pfn + size;
+       unsigned long nr_init = 0;
        struct page *page;
Quoted 2 lines
Quoted 2 lines

Review void __meminit memmap_init_range(unsigned long

Quoted 2 lines

if (highest_memmap_pfn < end_pfn - 1)

(quoted diff) +0 -0
@@ -893,7 +897,7 @@ void __meminit memmap_init_range(unsigned long

size,

Quoted 9 lines
Quoted 9 lines

int nid, unsigned long zone

Review void __meminit memmap_init_range(unsigned long

Quoted 8 lines
(quoted diff) +1 -1
        if (zone == ZONE_DEVICE) {
                if (!altmap)
-                       return;
+                       return 0;
 
                if (start_pfn == altmap->base_pfn)
                        start_pfn += altmap->reserve;
@@ -911,6 +915,7 @@ void __meminit memmap_init_range(unsigned long

size,

Quoted 6 lines
Quoted 6 lines

int nid, unsigned long zone

Review void __meminit memmap_init_range(unsigned long · line 917

Quoted 3 lines
(quoted diff) +1 -0
                        if (defer_init(nid, pfn, zone_end_pfn)) {
                                deferred_struct_pages = true;
+                               nr_init += end_pfn - pfn;

It's confusing. Could the remaining range also include overlapping

inits?

Quoted 2 lines
Quoted 2 lines

Maybe the whole "skip overlapping init" should actually be handled on

a

Quoted 8 lines
Quoted 5 lines

higher level?

I guess we'd want to skip any memblock_is_mirror(r) regions entirely.

@Mike?

Hi Mike

Quoted 7 lines
Quoted 2 lines

David suggested moving the overlap handling to a higher level and skipping memblock_is_mirror() regions entirely. I think this makes

sense.

Quoted 2 lines

Would this work for you, or do you have a different preference?

Looks about right :)

Thanks for the review. I’ll keep working on the patch.

Quoted 11 lines
Quoted 5 lines

Something like this
static void __init memmap_init(void)
{
...
for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid)

{

Quoted 3 lines
        struct pglist_data *node = NODE_DATA(nid);
        bool is_mirror = mirrored_kernelcore &&
				 memblock_is_mirror(&memblock.memory.regions[i]);

I'd add a local memblock_region variable.

I’ll refine this in the next version.

Quoted 14 lines
Quoted 10 lines
        for (j = 0; j < MAX_NR_ZONES; j++) {
        ...
	      if (is_mirror && j == ZONE_MOVABLE)
                continue;

           memmap_init_zone_range(zone, start_pfn, end_pfn,
                                  &hole_pfn);

Best Regards,
Liu, Yuan1

--
Sincerely yours,
Mike.

author
#14 lore raw
In reply to Liu, Yuan1

[...]

Quoted 10 lines
Quoted 10 lines

The set_zone_contiguous/clear_zone_contiguous can be ignored I think.

The comment about shrink_zone_span() is likely not realistic. shrink_zone_span() would not shrink over boot holes.

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.

Quoted 7 lines
Quoted 7 lines

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.

No need to test pfn_to_online_page() twice, as that is the same result for all pages in a PAGES_PER_SUBSECTION chunk.

Hi David

Sorry, I still have one question.

As I understand it, when a hole and memory fall within the same subsection,
we check the nid and zone on both sides of the subsection to avoid incorrect shrink. In this case, it seems the hole would always share the same zone and nid as the memory within that subsection.

I may be missing something, but I’m not entirely clear on the use case for this optimization. Could you help clarify?

Below is my proposed change. Please let me know if I’ve understood this correctly.

mm/memory_hotplug.c +17 -6
@@ -435,14 +435,20 @@ 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) {
+       unsigned long next_pfn;
+
+       for (; start_pfn < end_pfn; start_pfn = next_pfn) {
+               next_pfn = start_pfn + PAGES_PER_SUBSECTION;
+
                if (unlikely(!pfn_to_online_page(start_pfn)))
                        continue;
 
-               if (unlikely(pfn_to_nid(start_pfn) != nid))
+               if (unlikely(pfn_to_nid(start_pfn) != nid &&
+                            pfn_to_nid(next_pfn - 1) != nid))
                        continue;
 
-               if (zone != page_zone(pfn_to_page(start_pfn)))
+               if (zone != page_zone(pfn_to_page(start_pfn)) &&
+                   zone != page_zone(pfn_to_page(next_pfn - 1)))
                        continue;
 
                return start_pfn;
@@ -457,17 +463,22 @@ static unsigned long find_biggest_section_pfn(int nid, struct zone *zone,
                                    unsigned long end_pfn)
 {
        unsigned long pfn;
+       unsigned long next_pfn;
 
        /* pfn is the end pfn of a memory section. */
        pfn = end_pfn - 1;
-       for (; pfn >= start_pfn; pfn -= PAGES_PER_SUBSECTION) {
+       for (; pfn >= start_pfn; pfn = next_pfn) {
+               next_pfn = pfn - PAGES_PER_SUBSECTION;
+
                if (unlikely(!pfn_to_online_page(pfn)))
                        continue;
 
-               if (unlikely(pfn_to_nid(pfn) != nid))
+               if (unlikely(pfn_to_nid(pfn) != nid &&
+                            pfn_to_nid(next_pfn + 1) != nid))
                        continue;
 
-               if (zone != page_zone(pfn_to_page(pfn)))
+               if (zone != page_zone(pfn_to_page(pfn)) &&
+                   zone != page_zone(pfn_to_page(next_pfn + 1)))
                        continue;
 
                return pfn;
Quoted 5 lines
Quoted 5 lines

--
Cheers,

David

#15 lore raw
In reply to Yuan Liu

On Tue, Apr 07, 2026 at 11:16:15PM -0400, Yuan Liu wrote:
[...]

Quoted 13 lines
mm/mm_init.c +0 -12
-void set_zone_contiguous(struct zone *zone)
-{
-	unsigned long block_start_pfn = zone->zone_start_pfn;
-	unsigned long block_end_pfn;
-
-	block_end_pfn = pageblock_end_pfn(block_start_pfn);
-	for (; block_start_pfn < zone_end_pfn(zone);
-			block_start_pfn = block_end_pfn,
-			 block_end_pfn += pageblock_nr_pages) {
-
-		block_end_pfn = min(block_end_pfn, zone_end_pfn(zone));
-

mm/mm_init.c void __init init_cma_pageblock(struct page *page) · line 2285

Quoted 10 lines
mm/mm_init.c +0 -10
-		if (!__pageblock_pfn_to_page(block_start_pfn,
-					     block_end_pfn, zone))
-			return;
-		cond_resched();
-	}
-
-	/* We confirm that there is no hole */
-	zone->contiguous = true;
-}
-

Hi,

I may see a behavioral change after this patch.

* An originally non-contiguous zone would be detected as contiguous after this patch.

My test setup:

Did test in a qemu with 6G memory with memblock_debug enabled. And adjust the /proc/zoneinfo to display zone->contiguous field.

Originally, memblock_dump shows:

     MEMBLOCK configuration:
      memory size = 0x000000017ff7dc00 reserved size = 0x0000000005a9d9c2
      memory.cnt  = 0x3
      memory[0x0]     [0x0000000000001000-0x000000000009efff], 0x000000000009e000 bytes on node 0 flags: 0x0
      memory[0x1]     [0x0000000000100000-0x00000000bffdefff], 0x00000000bfedf000 bytes on node 0 flags: 0x0

+- memory[0x2] [0x0000000100000000-0x00000001bfffffff], 0x00000000c0000000 bytes on node 1 flags: 0x0

And zone range shows:

     Zone ranges:
       DMA      [mem 0x0000000000001000-0x0000000000ffffff]
       DMA32    [mem 0x0000000001000000-0x00000000ffffffff]
       Normal   [mem 0x0000000100000000-0x00000001bfffffff]     <--- entire last memblock region

With the last memblock region fits in Node 1 Zone Normal.

Then I punch a hole in this region with 2M(subsection) size with following change, to mimic there is a hole in memory range:

    @@ -1372,5 +1372,8 @@ __init void e820__memblock_setup(void)
            /* Throw away partial pages: */
            memblock_trim_memory(PAGE_SIZE);
     
    +       memblock_remove(0x140000000, 0x200000);
    +
            memblock_dump_all();
     }

Then the memblock dump shows:

     MEMBLOCK configuration:
      memory size = 0x000000017fd7dc00 reserved size = 0x0000000005a97 9c2
      memory.cnt  = 0x4
      memory[0x0]     [0x0000000000001000-0x000000000009efff], 0x000000000009e000 bytes on node 0 flags: 0x0
      memory[0x1]     [0x0000000000100000-0x00000000bffdefff], 0x00000000bfedf000 bytes on node 0 flags: 0x0
  +-  memory[0x2]     [0x0000000100000000-0x000000013fffffff], 0x0000000040000000 bytes on node 1 flags: 0x0
  +-  memory[0x3]     [0x0000000140200000-0x00000001bfffffff], 0x000000007fe00000 bytes on node 1 flags: 0x0

We can see the original one memblock region is divided into two, with a hole of 2M in the middle.

Not sure this is a reasonable mimic of memory hole. Also I tried to punch a larger hole, e.g. 10M, still see the behavioral change.

The /proc/zoneinfo result:

    w/o patch

    Node 1, zone   Normal
      pages free     469271
            boost    0
            min      8567
            low      10708
            high     12849
            promo    14990
            spanned  786432
            present  785920
            contigu  0           <--- zone is non-contiguous
            managed  766024
            cma      0
    
    with patch

    Node 1, zone   Normal
      pages free     121098
            boost    0
            min      8665
            low      10831
            high     12997
            promo    15163
            spanned  786432
            present  785920
            contigu  1           <--- zone is contiguous
            managed  773041
            cma      0

This shows we treat Node 1 Zone Normal as non-contiguous before, but treat it a contiguous zone after this patch.

Reason:

set_zone_contiguous()
__pageblock_pfn_to_page()
pfn_to_online_page()
pfn_section_valid() <--- check subsection

When SPARSEMEM_VMEMMEP is set, pfn_section_valid() checks subsection bit to decide if it is valid. For a hole, the corresponding bit is not set. So it is non-contiguous before the patch.

After this patch, the memory map in this hole also contributes to pages_with_online_memmap, so it is treated as contiguous.

Some question:

I suspect with !SPARSEMEM_VMEMMEP, we always treat Zone Normal as contiguous, because we don't set subsection. So it looks the behavior is different from SPARSEMEM_VMEMMEP. But I didn't manage to build kernel with !SPARSEMEM_VMEMMEP to verify.

I see the discussion on defining zone->contiguous as safe to use pfn_to_page() for the whole zone. For this purpose, current change looks good to me. Since we do allocate and init memory map for holes.

But pageblock_pfn_to_page() is used for compaction and other. A pfn with memory map but no actual memory seems not guarantee to be a usable page. So the correct usage of pageblock_pfn_to_page() is after pageblock_pfn_to_page() return a page, we should validate each page in the range before using? I am a little lost here.

Quoted 5 lines

/*
* Check if a PFN range intersects multiple zones on one or more
* NUMA nodes. Specify the @nid argument if it is known that this

Signature · 1 line
2.47.3
Signature · 2 lines
Wei Yang
Help you, Help me
#16 lore raw
Quoted 24 lines

With the last memblock region fits in Node 1 Zone Normal.

Then I punch a hole in this region with 2M(subsection) size with following change, to mimic there is a hole in memory range:

    @@ -1372,5 +1372,8 @@ __init void e820__memblock_setup(void)
            /* Throw away partial pages: */
            memblock_trim_memory(PAGE_SIZE);
     
    +       memblock_remove(0x140000000, 0x200000);
    +
            memblock_dump_all();
     }

Then the memblock dump shows:

     MEMBLOCK configuration:
      memory size = 0x000000017fd7dc00 reserved size = 0x0000000005a97 9c2
      memory.cnt  = 0x4
      memory[0x0]     [0x0000000000001000-0x000000000009efff], 0x000000000009e000 bytes on node 0 flags: 0x0
      memory[0x1]     [0x0000000000100000-0x00000000bffdefff], 0x00000000bfedf000 bytes on node 0 flags: 0x0
  +-  memory[0x2]     [0x0000000100000000-0x000000013fffffff], 0x0000000040000000 bytes on node 1 flags: 0x0
  +-  memory[0x3]     [0x0000000140200000-0x00000001bfffffff], 0x000000007fe00000 bytes on node 1 flags: 0x0
Quoted 2 lines

We can see the original one memblock region is divided into two, with a hole of 2M in the middle.

Yes, that makes sense.

Quoted 46 lines

Not sure this is a reasonable mimic of memory hole. Also I tried to punch a larger hole, e.g. 10M, still see the behavioral change.

The /proc/zoneinfo result:

    w/o patch

    Node 1, zone   Normal
      pages free     469271
            boost    0
            min      8567
            low      10708
            high     12849
            promo    14990
            spanned  786432
            present  785920
            contigu  0           <--- zone is non-contiguous
            managed  766024
            cma      0
    
    with patch

    Node 1, zone   Normal
      pages free     121098
            boost    0
            min      8665
            low      10831
            high     12997
            promo    15163
            spanned  786432
            present  785920
            contigu  1           <--- zone is contiguous
            managed  773041
            cma      0

This shows we treat Node 1 Zone Normal as non-contiguous before, but treat it a contiguous zone after this patch.

Reason:

set_zone_contiguous()
__pageblock_pfn_to_page()
pfn_to_online_page()
pfn_section_valid() <--- check subsection

Quoted 6 lines

When SPARSEMEM_VMEMMEP is set, pfn_section_valid() checks subsection bit to decide if it is valid. For a hole, the corresponding bit is not set. So it is non-contiguous before the patch.

After this patch, the memory map in this hole also contributes to pages_with_online_memmap, so it is treated as contiguous.

That means that mm init code actually initialized a memmap, so there is a memmap there that is properly initialized?

So init_unavailable_range()->for_each_valid_pfn() processed these sub-section holes I guess.

subsection_map_init() takes care of initializing the subsections. That happens before memmap_init() in free_area_init().

Is there a problem in for_each_valid_pfn()?

And I think there is in first_valid_pfn:

	if (valid_section(ms) &&
	    (early_section(ms) || pfn_section_first_valid(ms, &pfn))) {
		rcu_read_unlock_sched();
		return pfn;
	}

The PFN is valid, but we actually care about whether it will be online. So likely, we should skip over sub-sections here also for early sections (even though the memmap exist, nobody should be looking at it, just like for an offline memory section).

Quoted 11 lines

Some question:

I suspect with !SPARSEMEM_VMEMMEP, we always treat Zone Normal as contiguous, because we don't set subsection. So it looks the behavior is different from SPARSEMEM_VMEMMEP. But I didn't manage to build kernel with !SPARSEMEM_VMEMMEP to verify.

I see the discussion on defining zone->contiguous as safe to use pfn_to_page() for the whole zone. For this purpose, current change looks good to me. Since we do allocate and init memory map for holes.

Right.

Quoted 6 lines

But pageblock_pfn_to_page() is used for compaction and other. A pfn with memory map but no actual memory seems not guarantee to be a usable page. So the correct usage of pageblock_pfn_to_page() is after pageblock_pfn_to_page() return a page, we should validate each page in the range before using? I am a little lost here.

These non-existent pages (holes) are no different than allocated un-movable memory. So compaction code must deal with them. Just like smaller memory holes that don't cover a full memory section.

Signature · 3 lines
Cheers,

David
#17 lore raw
Quoted 79 lines · David Hildenbrand (Arm)
Quoted 24 lines

With the last memblock region fits in Node 1 Zone Normal.

Then I punch a hole in this region with 2M(subsection) size with following change, to mimic there is a hole in memory range:

    @@ -1372,5 +1372,8 @@ __init void e820__memblock_setup(void)
            /* Throw away partial pages: */
            memblock_trim_memory(PAGE_SIZE);
     
    +       memblock_remove(0x140000000, 0x200000);
    +
            memblock_dump_all();
     }

Then the memblock dump shows:

     MEMBLOCK configuration:
      memory size = 0x000000017fd7dc00 reserved size = 0x0000000005a97 9c2
      memory.cnt  = 0x4
      memory[0x0]     [0x0000000000001000-0x000000000009efff], 0x000000000009e000 bytes on node 0 flags: 0x0
      memory[0x1]     [0x0000000000100000-0x00000000bffdefff], 0x00000000bfedf000 bytes on node 0 flags: 0x0
  +-  memory[0x2]     [0x0000000100000000-0x000000013fffffff], 0x0000000040000000 bytes on node 1 flags: 0x0
  +-  memory[0x3]     [0x0000000140200000-0x00000001bfffffff], 0x000000007fe00000 bytes on node 1 flags: 0x0
Quoted 2 lines

We can see the original one memblock region is divided into two, with a hole of 2M in the middle.

Yes, that makes sense.

Quoted 50 lines

Not sure this is a reasonable mimic of memory hole. Also I tried to punch a larger hole, e.g. 10M, still see the behavioral change.

The /proc/zoneinfo result:

    w/o patch

    Node 1, zone   Normal
      pages free     469271
            boost    0
            min      8567
            low      10708
            high     12849
            promo    14990
            spanned  786432
            present  785920
            contigu  0           <--- zone is non-contiguous
            managed  766024
            cma      0
    
    with patch

    Node 1, zone   Normal
      pages free     121098
            boost    0
            min      8665
            low      10831
            high     12997
            promo    15163
            spanned  786432
            present  785920
            contigu  1           <--- zone is contiguous
            managed  773041
            cma      0

This shows we treat Node 1 Zone Normal as non-contiguous before, but treat it a contiguous zone after this patch.

Reason:

set_zone_contiguous()
__pageblock_pfn_to_page()
pfn_to_online_page()
pfn_section_valid() <--- check subsection

When SPARSEMEM_VMEMMEP is set, pfn_section_valid() checks subsection bit to decide if it is valid. For a hole, the corresponding bit is not set. So it is non-contiguous before the patch.

Quoted 9 lines
Quoted 2 lines

After this patch, the memory map in this hole also contributes to pages_with_online_memmap, so it is treated as contiguous.

That means that mm init code actually initialized a memmap, so there is a memmap there that is properly initialized?

So init_unavailable_range()->for_each_valid_pfn() processed these sub-section holes I guess.

Yes, I think so.

When memmap_init()->for_each_mem_pfn_range() iterate on the last memblock region, init_unavailable_range() will init the hole.

Quoted 3 lines

subsection_map_init() takes care of initializing the subsections. That happens before memmap_init() in free_area_init().

Yes. I guess you mean sparse_init_subsection_map().

Quoted 4 lines

Is there a problem in for_each_valid_pfn()?

And I think there is in first_valid_pfn:

You mean there is a problem in first_valid_pfn?

Quoted 11 lines
	if (valid_section(ms) &&
	    (early_section(ms) || pfn_section_first_valid(ms, &pfn))) {
		rcu_read_unlock_sched();
		return pfn;
	}

The PFN is valid, but we actually care about whether it will be online. So likely, we should skip over sub-sections here also for early sections (even though the memmap exist, nobody should be looking at it, just like for an offline memory section).

And it should be like below?

	if (valid_section(ms) &&
	    pfn_section_first_valid(ms, &pfn)) {
		rcu_read_unlock_sched();
		return pfn;
	}

IIUC, this would skip hole and leave allocated memory map uninitialized. And then those pages won't contribute to pages_with_online_memmap, which further leave the zone non-contiguous.

But we want zone to be contiguous when we have a hole like this, right?

Sorry, I don't follow here.

Quoted 21 lines
Quoted 11 lines

Some question:

I suspect with !SPARSEMEM_VMEMMEP, we always treat Zone Normal as contiguous, because we don't set subsection. So it looks the behavior is different from SPARSEMEM_VMEMMEP. But I didn't manage to build kernel with !SPARSEMEM_VMEMMEP to verify.

I see the discussion on defining zone->contiguous as safe to use pfn_to_page() for the whole zone. For this purpose, current change looks good to me. Since we do allocate and init memory map for holes.

Right.

Quoted 6 lines

But pageblock_pfn_to_page() is used for compaction and other. A pfn with memory map but no actual memory seems not guarantee to be a usable page. So the correct usage of pageblock_pfn_to_page() is after pageblock_pfn_to_page() return a page, we should validate each page in the range before using? I am a little lost here.

Quoted 4 lines

These non-existent pages (holes) are no different than allocated un-movable memory. So compaction code must deal with them. Just like smaller memory holes that don't cover a full memory section.

Thanks for explanation.

Quoted 4 lines
Signature · 3 lines
Cheers,

David
Signature · 2 lines
Wei Yang
Help you, Help me
#18 lore raw
Quoted 99 lines · Wei Yang
Quoted 79 lines · David Hildenbrand (Arm)
Quoted 24 lines

With the last memblock region fits in Node 1 Zone Normal.

Then I punch a hole in this region with 2M(subsection) size with following change, to mimic there is a hole in memory range:

    @@ -1372,5 +1372,8 @@ __init void e820__memblock_setup(void)
            /* Throw away partial pages: */
            memblock_trim_memory(PAGE_SIZE);
     
    +       memblock_remove(0x140000000, 0x200000);
    +
            memblock_dump_all();
     }

Then the memblock dump shows:

     MEMBLOCK configuration:
      memory size = 0x000000017fd7dc00 reserved size = 0x0000000005a97 9c2
      memory.cnt  = 0x4
      memory[0x0]     [0x0000000000001000-0x000000000009efff], 0x000000000009e000 bytes on node 0 flags: 0x0
      memory[0x1]     [0x0000000000100000-0x00000000bffdefff], 0x00000000bfedf000 bytes on node 0 flags: 0x0
  +-  memory[0x2]     [0x0000000100000000-0x000000013fffffff], 0x0000000040000000 bytes on node 1 flags: 0x0
  +-  memory[0x3]     [0x0000000140200000-0x00000001bfffffff], 0x000000007fe00000 bytes on node 1 flags: 0x0
Quoted 2 lines

We can see the original one memblock region is divided into two, with a hole of 2M in the middle.

Yes, that makes sense.

Quoted 50 lines

Not sure this is a reasonable mimic of memory hole. Also I tried to punch a larger hole, e.g. 10M, still see the behavioral change.

The /proc/zoneinfo result:

    w/o patch

    Node 1, zone   Normal
      pages free     469271
            boost    0
            min      8567
            low      10708
            high     12849
            promo    14990
            spanned  786432
            present  785920
            contigu  0           <--- zone is non-contiguous
            managed  766024
            cma      0
    
    with patch

    Node 1, zone   Normal
      pages free     121098
            boost    0
            min      8665
            low      10831
            high     12997
            promo    15163
            spanned  786432
            present  785920
            contigu  1           <--- zone is contiguous
            managed  773041
            cma      0

This shows we treat Node 1 Zone Normal as non-contiguous before, but treat it a contiguous zone after this patch.

Reason:

set_zone_contiguous()
__pageblock_pfn_to_page()
pfn_to_online_page()
pfn_section_valid() <--- check subsection

When SPARSEMEM_VMEMMEP is set, pfn_section_valid() checks subsection bit to decide if it is valid. For a hole, the corresponding bit is not set. So it is non-contiguous before the patch.

Quoted 9 lines
Quoted 2 lines

After this patch, the memory map in this hole also contributes to pages_with_online_memmap, so it is treated as contiguous.

That means that mm init code actually initialized a memmap, so there is a memmap there that is properly initialized?

So init_unavailable_range()->for_each_valid_pfn() processed these sub-section holes I guess.

Yes, I think so.

When memmap_init()->for_each_mem_pfn_range() iterate on the last memblock region, init_unavailable_range() will init the hole.

Quoted 3 lines

subsection_map_init() takes care of initializing the subsections. That happens before memmap_init() in free_area_init().

Quoted 8 lines

Yes. I guess you mean sparse_init_subsection_map().

Quoted 4 lines

Is there a problem in for_each_valid_pfn()?

And I think there is in first_valid_pfn:

You mean there is a problem in first_valid_pfn?

That is my theory.

Quoted 13 lines
Quoted 7 lines
	if (valid_section(ms) &&
	    (early_section(ms) || pfn_section_first_valid(ms, &pfn))) {
		rcu_read_unlock_sched();
		return pfn;
	}
Quoted 5 lines

The PFN is valid, but we actually care about whether it will be online. So likely, we should skip over sub-sections here also for early sections (even though the memmap exist, nobody should be looking at it, just like for an offline memory section).

Quoted 7 lines

And it should be like below?

	if (valid_section(ms) &&
	    pfn_section_first_valid(ms, &pfn)) {
		rcu_read_unlock_sched();
		return pfn;
	}

Probably, yes. We have to understand if other users would be negatively affected.

Quoted 4 lines

IIUC, this would skip hole and leave allocated memory map uninitialized. And then those pages won't contribute to pages_with_online_memmap, which further leave the zone non-contiguous.

Yes.

Quoted 2 lines

But we want zone to be contiguous when we have a hole like this, right?

Not if a subsection is marked invalid. That's why the existing scenario is that it will not be contiguous.

Note that Wei reported that it was not contiguous but would now be contiguous.

If you have a DAX device the plugs into that hole through memremap_pages()->pagemap_range(), I think this could cause problems. I doubt that this would happen in practice for such small holes, but if they would be bigger, or at the start/end of the range, it could be problematic.

Signature · 3 lines
Cheers,

David
#19 lore raw
Quoted 140 lines · David Hildenbrand (Arm)
Quoted 99 lines · Wei Yang
Quoted 79 lines · David Hildenbrand (Arm)
Quoted 24 lines

With the last memblock region fits in Node 1 Zone Normal.

Then I punch a hole in this region with 2M(subsection) size with following change, to mimic there is a hole in memory range:

    @@ -1372,5 +1372,8 @@ __init void e820__memblock_setup(void)
            /* Throw away partial pages: */
            memblock_trim_memory(PAGE_SIZE);
     
    +       memblock_remove(0x140000000, 0x200000);
    +
            memblock_dump_all();
     }

Then the memblock dump shows:

     MEMBLOCK configuration:
      memory size = 0x000000017fd7dc00 reserved size = 0x0000000005a97 9c2
      memory.cnt  = 0x4
      memory[0x0]     [0x0000000000001000-0x000000000009efff], 0x000000000009e000 bytes on node 0 flags: 0x0
      memory[0x1]     [0x0000000000100000-0x00000000bffdefff], 0x00000000bfedf000 bytes on node 0 flags: 0x0
  +-  memory[0x2]     [0x0000000100000000-0x000000013fffffff], 0x0000000040000000 bytes on node 1 flags: 0x0
  +-  memory[0x3]     [0x0000000140200000-0x00000001bfffffff], 0x000000007fe00000 bytes on node 1 flags: 0x0
Quoted 2 lines

We can see the original one memblock region is divided into two, with a hole of 2M in the middle.

Yes, that makes sense.

Quoted 50 lines

Not sure this is a reasonable mimic of memory hole. Also I tried to punch a larger hole, e.g. 10M, still see the behavioral change.

The /proc/zoneinfo result:

    w/o patch

    Node 1, zone   Normal
      pages free     469271
            boost    0
            min      8567
            low      10708
            high     12849
            promo    14990
            spanned  786432
            present  785920
            contigu  0           <--- zone is non-contiguous
            managed  766024
            cma      0
    
    with patch

    Node 1, zone   Normal
      pages free     121098
            boost    0
            min      8665
            low      10831
            high     12997
            promo    15163
            spanned  786432
            present  785920
            contigu  1           <--- zone is contiguous
            managed  773041
            cma      0

This shows we treat Node 1 Zone Normal as non-contiguous before, but treat it a contiguous zone after this patch.

Reason:

set_zone_contiguous()
__pageblock_pfn_to_page()
pfn_to_online_page()
pfn_section_valid() <--- check subsection

When SPARSEMEM_VMEMMEP is set, pfn_section_valid() checks subsection bit to decide if it is valid. For a hole, the corresponding bit is not set. So it is non-contiguous before the patch.

Quoted 9 lines
Quoted 2 lines

After this patch, the memory map in this hole also contributes to pages_with_online_memmap, so it is treated as contiguous.

That means that mm init code actually initialized a memmap, so there is a memmap there that is properly initialized?

So init_unavailable_range()->for_each_valid_pfn() processed these sub-section holes I guess.

Yes, I think so.

When memmap_init()->for_each_mem_pfn_range() iterate on the last memblock region, init_unavailable_range() will init the hole.

Quoted 3 lines

subsection_map_init() takes care of initializing the subsections. That happens before memmap_init() in free_area_init().

Quoted 8 lines

Yes. I guess you mean sparse_init_subsection_map().

Quoted 4 lines

Is there a problem in for_each_valid_pfn()?

And I think there is in first_valid_pfn:

You mean there is a problem in first_valid_pfn?

That is my theory.

Quoted 13 lines
Quoted 7 lines
	if (valid_section(ms) &&
	    (early_section(ms) || pfn_section_first_valid(ms, &pfn))) {
		rcu_read_unlock_sched();
		return pfn;
	}
Quoted 5 lines

The PFN is valid, but we actually care about whether it will be online. So likely, we should skip over sub-sections here also for early sections (even though the memmap exist, nobody should be looking at it, just like for an offline memory section).

Quoted 7 lines

And it should be like below?

	if (valid_section(ms) &&
	    pfn_section_first_valid(ms, &pfn)) {
		rcu_read_unlock_sched();
		return pfn;
	}

Probably, yes. We have to understand if other users would be negatively affected.

Quoted 4 lines

IIUC, this would skip hole and leave allocated memory map uninitialized. And then those pages won't contribute to pages_with_online_memmap, which further leave the zone non-contiguous.

Quoted 8 lines

Yes.

Quoted 2 lines

But we want zone to be contiguous when we have a hole like this, right?

Not if a subsection is marked invalid. That's why the existing scenario is that it will not be contiguous.

Let me try to understand.

During previous discussion[1], we want to define "zone->contiguous guarantee pfn_to_page() is valid on the complete zone". So this definition is not true now, since we found the behavioral change.

Because __pageblock_pfn_to_page() won't treat range with hole as contiguous, detected by pfn_to_online_page() on invalid subsection.

[1]: https://lkml.org/lkml/2026/2/9/550

Now you are thinking the problem is in the iteration function, for_each_valid_pfn(), used in init_unavaiable_range(). It should only take valid subsections into consideration, even for early sections.

So your concern is if we change first_valid_pfn(), it will affect other users.

If the above understanding is correct, maybe we can use spanned == present to do the trick? Because holes are marked subsection invalid and holes are counted into absent.

But I see the mirrored_kernel thing, not fully understand yet. This is the reason to prevent spanned == present approach?

Quoted 10 lines

Note that Wei reported that it was not contiguous but would now be contiguous.

If you have a DAX device the plugs into that hole through memremap_pages()->pagemap_range(), I think this could cause problems. I doubt that this would happen in practice for such small holes, but if they would be bigger, or at the start/end of the range, it could be problematic.

Not fully understand yet, will take a look into it.

Quoted 4 lines
Signature · 3 lines
Cheers,

David
Signature · 2 lines
Wei Yang
Help you, Help me
#20 lore raw
Quoted 52 lines · Wei Yang
Quoted 33 lines · David Hildenbrand (Arm)
Quoted 11 lines · Wei Yang

Yes, I think so.

When memmap_init()->for_each_mem_pfn_range() iterate on the last memblock region, init_unavailable_range() will init the hole.

Yes. I guess you mean sparse_init_subsection_map().

You mean there is a problem in first_valid_pfn?

That is my theory.

Quoted 9 lines

And it should be like below?

	if (valid_section(ms) &&
	    pfn_section_first_valid(ms, &pfn)) {
		rcu_read_unlock_sched();
		return pfn;
	}

Probably, yes. We have to understand if other users would be negatively affected.

Quoted 4 lines

IIUC, this would skip hole and leave allocated memory map uninitialized. And then those pages won't contribute to pages_with_online_memmap, which further leave the zone non-contiguous.

Quoted 8 lines

Yes.

Quoted 2 lines

But we want zone to be contiguous when we have a hole like this, right?

Not if a subsection is marked invalid. That's why the existing scenario is that it will not be contiguous.

Let me try to understand.

During previous discussion[1], we want to define "zone->contiguous guarantee pfn_to_page() is valid on the complete zone". So this definition is not true now, since we found the behavioral change.

Because __pageblock_pfn_to_page() won't treat range with hole as contiguous, detected by pfn_to_online_page() on invalid subsection.

Quoted 7 lines

[1]: https://lkml.org/lkml/2026/2/9/550

Now you are thinking the problem is in the iteration function, for_each_valid_pfn(), used in init_unavaiable_range(). It should only take valid subsections into consideration, even for early sections.

So your concern is if we change first_valid_pfn(), it will affect other users.

Let me clarify: We created a bit of a mess.

pfn_valid() says: "early sections always have a full memmap, so even invalid subsections have a memmap".

pfn_to_online_page() says "it's an invalid subsection, so it sure can't be online and the content must be stale"

for_each_valid_pfn() obeys to pfn_valid() semantics, and we use it to initialize the memmap (that's not going to be online) and accounts it with this patch here as "pages_with_online_memmap", which is wrong.

The *cleanest* thing would be to not handle early sections in a special way. That is, don't allocate a memmap for subsections. That would remove the special early handling from pfn_valid() and for_each_valid_pfn().

As an easy way forward, maybe we can just make pfn_valid() / for_each_valid_pfn() behave just like pfn_to_online_page():

include/linux/mmzone.h +6 -7
index e6858b13a0be..bfa5c3df5ee2 100644
@@ -2253,6 +2253,10 @@ void sparse_init_early_section(int nid, struct page *map, unsigned long pnum,
  * there is actual usable memory at that @pfn. The struct page may
  * represent a hole or an unusable page frame.
  *
+ * Note that this function returns "0" for PFNs that fall into
+ * invalid subsections as part of early sections, even though there would
+ * currently be a memmap allocated (that should not be touched).
+ *
  * Return: 1 for PFNs that have memory map entries and 0 otherwise
  */
 static inline int pfn_valid(unsigned long pfn)
@@ -2277,11 +2281,7 @@ static inline int pfn_valid(unsigned long pfn)
                rcu_read_unlock_sched();
                return 0;
        }
-       /*
-        * Traditionally early sections always returned pfn_valid() for
-        * the entire section-sized span.
-        */
-       ret = early_section(ms) || pfn_section_valid(ms, pfn);
+       ret = pfn_section_valid(ms, pfn);
        rcu_read_unlock_sched();
 
        return ret;
@@ -2297,8 +2297,7 @@ static inline unsigned long first_valid_pfn(unsigned long pfn, unsigned long end
        while (nr <= __highest_present_section_nr && pfn < end_pfn) {
                struct mem_section *ms = __pfn_to_section(pfn);
 
-               if (valid_section(ms) &&
-                   (early_section(ms) || pfn_section_first_valid(ms, &pfn))) {
+               if (valid_section(ms) && pfn_section_first_valid(ms, &pfn)) {
                        rcu_read_unlock_sched();
                        return pfn;
                }
mm/sparse-vmemmap.c +2 -2
index 330579365a0f..6d0bbf8a2159 100644
@@ -775,8 +775,8 @@ static void section_deactivate(unsigned long pfn, unsigned long nr_pages,
        }
 
        /*
-        * The memmap of early sections is always fully populated. See
-        * section_activate() and pfn_valid() .
+        * The memmap of early sections is currently always fully populated. See
+        * section_activate().
         */
        if (!section_is_early) {
                memmap_pages_add(-1L * (DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE)));

It would now return !pfn_valid() on subsection-sized holes. The price is another test_bit(idx, usage->subsection_map) check for early sections. If that's a real problem, we could add a section flag that just caches "full valid section".

Quoted 7 lines

If the above understanding is correct, maybe we can use spanned == present to do the trick? Because holes are marked subsection invalid and holes are counted into absent.

But I see the mirrored_kernel thing, not fully understand yet. This is the reason to prevent spanned == present approach?

I think we have to rework the way that mirrored .... stuff is handled. It's way too intrusive, and the memmap init special casing deep down in there is just nasty.

I'd think that we should just skip over mirrored part on a higher level.

My understanding is: mirror memory has two physical ranges that mirror each other. We expose that memory to ZONE_MOABLE. But only one of the two physical ranges is actually used by the buddy, has a memmap etc.

The other (mirrored) range should just be ignored entirely.

Signature · 3 lines
Cheers,

David
#21 lore raw
In reply to Yuan Liu

On Tue, Apr 07, 2026 at 11:16:15PM -0400, Yuan Liu wrote:
[...]

Quoted 6 lines

/*

mm/mm_init.c +0 -0
@@ -956,9 +957,22 @@ static void __init memmap_init_zone_range(struct zone *zone,
 	memmap_init_range(end_pfn - start_pfn, nid, zone_id, start_pfn,
 			  zone_end_pfn, MEMINIT_EARLY, NULL, MIGRATE_MOVABLE,
 			  false);

mm/mm_init.c static void __init memmap_init_zone_range(struct zone *zone, · line 967

Quoted 10 lines
mm/mm_init.c +7 -2
+	zone->pages_with_online_memmap += end_pfn - start_pfn;
 
-	if (*hole_pfn < start_pfn)
-		init_unavailable_range(*hole_pfn, start_pfn, zone_id, nid);
+	if (*hole_pfn < start_pfn) {
+		unsigned long pgcnt;
+
+		if (*hole_pfn < zone_start_pfn) {
+			init_unavailable_range(*hole_pfn, zone_start_pfn,
+					       zone_id, nid);

One question here:

init_unavailable_range(zone_id)
__init_single_page(zone_id)
set_page_links(zone_id)
set_page_zone(page, zone_id)

[hole_pfn, zone_start_pfn) doesn't belong to this zone. Would this be a problem?

But it seems we have already done this for a long time.

Quoted 11 lines
mm/mm_init.c +8 -0
+			pgcnt = init_unavailable_range(zone_start_pfn,
+					start_pfn, zone_id, nid);
+		} else {
+			pgcnt = init_unavailable_range(*hole_pfn, start_pfn,
+					zone_id, nid);
+		}
+		zone->pages_with_online_memmap += pgcnt;
+	}
 
 	*hole_pfn = end_pfn;
 }
Signature · 2 lines
Wei Yang
Help you, Help me
#22 lore raw
Quoted 26 lines · Wei Yang

On Tue, Apr 07, 2026 at 11:16:15PM -0400, Yuan Liu wrote:
[...]

Quoted 6 lines

/*

mm/mm_init.c +0 -0
@@ -956,9 +957,22 @@ static void __init memmap_init_zone_range(struct zone *zone,
 	memmap_init_range(end_pfn - start_pfn, nid, zone_id, start_pfn,
 			  zone_end_pfn, MEMINIT_EARLY, NULL, MIGRATE_MOVABLE,
 			  false);

mm/mm_init.c static void __init memmap_init_zone_range(struct zone *zone, · line 967

Quoted 10 lines
mm/mm_init.c +7 -2
+	zone->pages_with_online_memmap += end_pfn - start_pfn;
 
-	if (*hole_pfn < start_pfn)
-		init_unavailable_range(*hole_pfn, start_pfn, zone_id, nid);
+	if (*hole_pfn < start_pfn) {
+		unsigned long pgcnt;
+
+		if (*hole_pfn < zone_start_pfn) {
+			init_unavailable_range(*hole_pfn, zone_start_pfn,
+					       zone_id, nid);

One question here:

init_unavailable_range(zone_id)
__init_single_page(zone_id)
set_page_links(zone_id)
set_page_zone(page, zone_id)

Quoted 4 lines

[hole_pfn, zone_start_pfn) doesn't belong to this zone. Would this be a problem?

But it seems we have already done this for a long time.

Right, we just want "some" node/zone for holes that are indicated as having an online memmap.

Signature · 3 lines
Cheers,

David