Skip to contentSkip to conversation

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

PATCH Superseded Yuan Liu 3 participants 5 messages Apr 1 – Apr 7

Subsystems (8)

v2 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Yuan Liu

author
#0 lore raw

PATCH v2 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 memmap (including present pages and memory holes whose memmap 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 | 11 +++++
 drivers/base/memory.c                |  6 +++
 include/linux/mmzone.h               | 44 +++++++++++++++++++
 mm/internal.h                        |  8 +---
 mm/memory_hotplug.c                  | 12 +-----
 mm/mm_init.c                         | 64 +++++++++++++++++-----------
 6 files changed, 102 insertions(+), 43 deletions(-)
Documentation/mm/physical_memory.rst +11 -0
index b76183545e5b..e47e96ef6a6d 100644
@@ -483,6 +483,17 @@ 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 memmap (present pages and
+  memory holes whose memmap 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
+  memmap exist outside the current 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 +44 -0

62 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..cd9c89de6ed2 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 the zones.
 	 */
 	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 +38 -26

96 lines not on this page · raw message

Signature · 1 line
2.47.3
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 90 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 memmap (including present pages and memory holes whose memmap 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 | 11 +++++
 drivers/base/memory.c                |  6 +++
 include/linux/mmzone.h               | 44 +++++++++++++++++++
 mm/internal.h                        |  8 +---
 mm/memory_hotplug.c                  | 12 +-----
 mm/mm_init.c                         | 64 +++++++++++++++++-----------
 6 files changed, 102 insertions(+), 43 deletions(-)
Documentation/mm/physical_memory.rst +0 -0
index b76183545e5b..e47e96ef6a6d 100644
@@ -483,6 +483,17 @@ General
   ``present_pages`` should use ``get_online_mems()`` to get a stable value. It

Documentation/mm/physical_memory.rst General · line 493

Quoted 10 lines
Documentation/mm/physical_memory.rst +8 -0
   is initialized by ``calculate_node_totalpages()``.
 
+``pages_with_online_memmap``
+  Tracks pages within the zone that have an online memmap (present pages and
+  memory holes whose memmap 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
+  memmap exist outside the current zone span. Growing the zone to cover such

Maybe add here "This can only happen during boot, when initializing the memmap of pages that do not fall into any zone span."

Documentation/mm/physical_memory.rst General · line 494

Quoted 1 line
Documentation/mm/physical_memory.rst +1 -0
+	 * we will not try to shrink the zones.

s/zone/it/ ?
[...]

Documentation/mm/physical_memory.rst General

Quoted 9 lines
Documentation/mm/physical_memory.rst +9 -0
+
+/*
+ * Initialize unavailable range [spfn, epfn) while accounting only the pages
+ * that fall within the zone span towards pages_with_online_memmap. Pages
+ * outside the zone span are still initialized but not accounted.
+ */
+static void __init init_unavailable_range_for_zone(struct zone *zone,
+						   unsigned long spfn,
+						   unsigned long epfn)

Best to use double tab to fit this into a single line

unsigned long spfn, unsigned long epfn)

^ two tabs

Documentation/mm/physical_memory.rst

Quoted 3 lines
Documentation/mm/physical_memory.rst +3 -0
+{
+	int nid = zone_to_nid(zone);
+	int zid = zone_idx(zone);

Both can be const.

Documentation/mm/physical_memory.rst

Quoted 13 lines
Documentation/mm/physical_memory.rst +13 -0
+	unsigned long in_zone_start;
+	unsigned long in_zone_end;
+
+	in_zone_start = clamp(spfn, zone->zone_start_pfn, zone_end_pfn(zone));
+	in_zone_end = clamp(epfn, zone->zone_start_pfn, zone_end_pfn(zone));
+
+	if (spfn < in_zone_start)
+		init_unavailable_range(spfn, in_zone_start, zid, nid);
+
+	if (in_zone_start < in_zone_end)
+		zone->pages_with_online_memmap +=
+			init_unavailable_range(in_zone_start, in_zone_end,
+					       zid, nid);

Best to use a temporary variable to make this easier to read.

pgcnt = init_unavailable_range(in_zone_start, ...

You can also exceed 80c a bit if it aids readability.

Documentation/mm/physical_memory.rst

Quoted 4 lines
Documentation/mm/physical_memory.rst +3 -0
+
+	if (in_zone_end < epfn)
+		init_unavailable_range(in_zone_end, epfn, zid, nid);
 }

Only nits, hoping we don't miss anything obvious (or any corner case :) ).

If Mike tells us that we are processing all pages during boot appropriately, this should work.

Thanks!

Signature · 3 lines
Cheers,

David
author
#2 lore raw
Quoted 135 lines

-----Original Message-----
From: David Hildenbrand (Arm) <[email protected]>
Sent: Thursday, April 2, 2026 10:57 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 v2] 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 10 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 memmap (including present pages and memory holes whose memmap 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.

Quoted 2 lines

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

---
 Documentation/mm/physical_memory.rst | 11 +++++
 drivers/base/memory.c                |  6 +++
 include/linux/mmzone.h               | 44 +++++++++++++++++++
 mm/internal.h                        |  8 +---
 mm/memory_hotplug.c                  | 12 +-----
 mm/mm_init.c                         | 64 +++++++++++++++++-----------
 6 files changed, 102 insertions(+), 43 deletions(-)

Review

Quoted 1 line
(quoted diff) +0 -0

b/Documentation/mm/physical_memory.rst

Documentation/mm/physical_memory.rst General · line 483

Quoted 5 lines

index b76183545e5b..e47e96ef6a6d 100644

Documentation/mm/physical_memory.rst +0 -0
@@ -483,6 +483,17 @@ General
   ``present_pages`` should use ``get_online_mems()`` to get a stable

value. It

Documentation/mm/physical_memory.rst General

Quoted 4 lines
Documentation/mm/physical_memory.rst +2 -0
   is initialized by ``calculate_node_totalpages()``.
 
+``pages_with_online_memmap``
+  Tracks pages within the zone that have an online memmap (present

pages and

Documentation/mm/physical_memory.rst General · line 488

Quoted 1 line
Documentation/mm/physical_memory.rst +1 -0
+  memory holes whose memmap has been initialized). When

``spanned_pages`` ==

Documentation/mm/physical_memory.rst General · line 489

Quoted 1 line
Documentation/mm/physical_memory.rst +1 -0
+  ``pages_with_online_memmap``, ``pfn_to_page()`` can be performed

without

Documentation/mm/physical_memory.rst General

Quoted 3 lines
Documentation/mm/physical_memory.rst +3 -0
+  further checks on any PFN within the zone span.
+
+  Note: this counter may temporarily undercount when pages with an
Quoted 6 lines

online

Documentation/mm/physical_memory.rst General · line 493

Quoted 1 line
Documentation/mm/physical_memory.rst +1 -0
+  memmap exist outside the current zone span. Growing the zone to cover

such

Maybe add here "This can only happen during boot, when initializing the memmap of pages that do not fall into any zone span."

I will add it into the next version.

Quoted 5 lines

Documentation/mm/physical_memory.rst General · line 494

Quoted 2 lines
Documentation/mm/physical_memory.rst +1 -0
+	 * we will not try to shrink the zones.

s/zone/it/ ?
[...]

Sure

Quoted 14 lines

Documentation/mm/physical_memory.rst General · line 497

Quoted 3 lines
Documentation/mm/physical_memory.rst +3 -0
+
+/*
+ * Initialize unavailable range [spfn, epfn) while accounting only the

pages

Documentation/mm/physical_memory.rst General · line 498

Quoted 1 line
Documentation/mm/physical_memory.rst +1 -0
+ * that fall within the zone span towards pages_with_online_memmap.

Pages

Documentation/mm/physical_memory.rst General

Quoted 5 lines
Documentation/mm/physical_memory.rst +5 -0
+ * outside the zone span are still initialized but not accounted.
+ */
+static void __init init_unavailable_range_for_zone(struct zone *zone,
+						   unsigned long spfn,
+						   unsigned long epfn)

Best to use double tab to fit this into a single line

Quoted 3 lines

unsigned long spfn, unsigned long epfn)

^ two tabs

Thanks, I will refine the coding style here.

Quoted 5 lines

Documentation/mm/physical_memory.rst

Quoted 3 lines
Documentation/mm/physical_memory.rst +3 -0
+{
+	int nid = zone_to_nid(zone);
+	int zid = zone_idx(zone);

Both can be const.

Yes, I will fix this next version.

Quoted 17 lines

Documentation/mm/physical_memory.rst

Quoted 4 lines
Documentation/mm/physical_memory.rst +4 -0
+	unsigned long in_zone_start;
+	unsigned long in_zone_end;
+
+	in_zone_start = clamp(spfn, zone->zone_start_pfn,

zone_end_pfn(zone));

Documentation/mm/physical_memory.rst

Quoted 9 lines
Documentation/mm/physical_memory.rst +9 -0
+	in_zone_end = clamp(epfn, zone->zone_start_pfn, zone_end_pfn(zone));
+
+	if (spfn < in_zone_start)
+		init_unavailable_range(spfn, in_zone_start, zid, nid);
+
+	if (in_zone_start < in_zone_end)
+		zone->pages_with_online_memmap +=
+			init_unavailable_range(in_zone_start, in_zone_end,
+					       zid, nid);

Best to use a temporary variable to make this easier to read.

Quoted 3 lines

pgcnt = init_unavailable_range(in_zone_start, ...

You can also exceed 80c a bit if it aids readability.

Indeed, that is a better way.

Quoted 10 lines

Documentation/mm/physical_memory.rst

Quoted 4 lines
Documentation/mm/physical_memory.rst +3 -0
+
+	if (in_zone_end < epfn)
+		init_unavailable_range(in_zone_end, epfn, zid, nid);
 }

Only nits, hoping we don't miss anything obvious (or any corner case :) ).

If Mike tells us that we are processing all pages during boot appropriately, this should work.

Okay, I will update the next version after Mike’s review, and I really appreciate your comments.

Quoted 8 lines

Thanks!

--
Cheers,

David

maintainer
#3 lore raw
In reply to Yuan Liu
Quoted 4 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.

...

Documentation/mm/physical_memory.rst General · line 487

Quoted 10 lines
Documentation/mm/physical_memory.rst +2 -0
index b76183545e5b..e47e96ef6a6d 100644
@@ -483,6 +483,17 @@ 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 memmap (present pages and

Please spell out "memory map" rather then memmap in the documentation and in the comments.

Quoted 12 lines
Documentation/mm/physical_memory.rst +9 -0
+  memory holes whose memmap 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
+  memmap exist outside the current 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

...

Quoted 18 lines
Documentation/mm/physical_memory.rst +18 -0
+/*
+ * Initialize unavailable range [spfn, epfn) while accounting only the pages
+ * that fall within the zone span towards pages_with_online_memmap. Pages
+ * outside the zone span are still initialized but not accounted.
+ */
+static void __init init_unavailable_range_for_zone(struct zone *zone,
+						   unsigned long spfn,
+						   unsigned long epfn)
+{
+	int nid = zone_to_nid(zone);
+	int zid = zone_idx(zone);
+	unsigned long in_zone_start;
+	unsigned long in_zone_end;
+
+	in_zone_start = clamp(spfn, zone->zone_start_pfn, zone_end_pfn(zone));
+	in_zone_end = clamp(epfn, zone->zone_start_pfn, zone_end_pfn(zone));
+
+	if (spfn < in_zone_start)

Documentation/mm/physical_memory.rst

Quoted 10 lines
Documentation/mm/physical_memory.rst +9 -0
+		init_unavailable_range(spfn, in_zone_start, zid, nid);
+
+	if (in_zone_start < in_zone_end)
+		zone->pages_with_online_memmap +=
+			init_unavailable_range(in_zone_start, in_zone_end,
+					       zid, nid);
+
+	if (in_zone_end < epfn)
+		init_unavailable_range(in_zone_end, epfn, zid, nid);
 }

I think we can make it simpler, see below.

Documentation/mm/physical_memory.rst static void __init memmap_init_zone_range(struct zone *zone, · line 992

Quoted 10 lines

/*

Documentation/mm/physical_memory.rst +2 -1
@@ -956,9 +986,10 @@ 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;
 
 	if (*hole_pfn < start_pfn)
-		init_unavailable_range(*hole_pfn, start_pfn, zone_id, nid);
+		init_unavailable_range_for_zone(zone, *hole_pfn, start_pfn);

Here *hole_pfn is either inside zone span or below it and in the second case it's enough to adjust page count returned by init_unavailable_range() by (zone_start_pfn - *hole_pfn).

Documentation/mm/physical_memory.rst static void __init memmap_init(void) · line 1033

Quoted 12 lines
Documentation/mm/physical_memory.rst +4 -2
 	*hole_pfn = end_pfn;
 }
@@ -996,8 +1027,11 @@ static void __init memmap_init(void)
 #else
 	end_pfn = round_up(end_pfn, MAX_ORDER_NR_PAGES);
 #endif
-	if (hole_pfn < end_pfn)
-		init_unavailable_range(hole_pfn, end_pfn, zone_id, nid);
+	if (hole_pfn < end_pfn) {
+		struct zone *zone = &NODE_DATA(nid)->node_zones[zone_id];
+
+		init_unavailable_range_for_zone(zone, hole_pfn, end_pfn);

Here we know that the range is not in any zone span.

Quoted 3 lines
Documentation/mm/physical_memory.rst +1 -0
+	}
 }
 
Signature · 2 lines
Sincerely yours,
Mike.
author
#4 lore raw
Quoted 31 lines

-----Original Message-----
From: Mike Rapoport <[email protected]>
Sent: Saturday, April 4, 2026 7:12 PM
To: Liu, Yuan1 <[email protected]>
Cc: David Hildenbrand <[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 v2] 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 2 lines

to rebuild zone->contiguous. For large zones this is a significant cost during memory hotplug and hot-unplug.

...

Review

Quoted 1 line
(quoted diff) +0 -0

b/Documentation/mm/physical_memory.rst

Documentation/mm/physical_memory.rst General · line 483

Quoted 5 lines

index b76183545e5b..e47e96ef6a6d 100644

Documentation/mm/physical_memory.rst +0 -0
@@ -483,6 +483,17 @@ General
   ``present_pages`` should use ``get_online_mems()`` to get a stable

value. It

Quoted 8 lines

Documentation/mm/physical_memory.rst General

Quoted 4 lines
Documentation/mm/physical_memory.rst +2 -0
   is initialized by ``calculate_node_totalpages()``.
 
+``pages_with_online_memmap``
+  Tracks pages within the zone that have an online memmap (present

pages and

Please spell out "memory map" rather then memmap in the documentation and in the comments.

Sure, I will fix it next version.

Quoted 63 lines

Documentation/mm/physical_memory.rst General · line 488

Quoted 1 line
Documentation/mm/physical_memory.rst +1 -0
+  memory holes whose memmap has been initialized). When

``spanned_pages`` ==

Documentation/mm/physical_memory.rst General · line 489

Quoted 1 line
Documentation/mm/physical_memory.rst +1 -0
+  ``pages_with_online_memmap``, ``pfn_to_page()`` can be performed

without

Documentation/mm/physical_memory.rst General

Quoted 3 lines
Documentation/mm/physical_memory.rst +3 -0
+  further checks on any PFN within the zone span.
+
+  Note: this counter may temporarily undercount when pages with an

online

Documentation/mm/physical_memory.rst General · line 493

Quoted 1 line
Documentation/mm/physical_memory.rst +1 -0
+  memmap exist outside the current zone span. Growing the zone to cover

such

Documentation/mm/physical_memory.rst General · line 494

Quoted 1 line
Documentation/mm/physical_memory.rst +1 -0
+  pages and later shrinking it back may result in a "too small" value.

This is

Documentation/mm/physical_memory.rst General

Quoted 4 lines
Documentation/mm/physical_memory.rst +2 -0
+  safe: it merely prevents detecting a contiguous zone.
+
 ``present_early_pages``
   The present pages existing within the zone located on memory

available since

Quoted 1 line
Documentation/mm/physical_memory.rst +0 -0
   early boot, excluding hotplugged memory. Defined only when

...

Quoted 2 lines

+/*
+ * Initialize unavailable range [spfn, epfn) while accounting only the

pages

Quoted 1 line

+ * that fall within the zone span towards pages_with_online_memmap.

Pages

Documentation/mm/physical_memory.rst

Quoted 12 lines

+ * outside the zone span are still initialized but not accounted.
+ */

Documentation/mm/physical_memory.rst +10 -0
+static void __init init_unavailable_range_for_zone(struct zone *zone,
+						   unsigned long spfn,
+						   unsigned long epfn)
+{
+	int nid = zone_to_nid(zone);
+	int zid = zone_idx(zone);
+	unsigned long in_zone_start;
+	unsigned long in_zone_end;
+
+	in_zone_start = clamp(spfn, zone->zone_start_pfn,

zone_end_pfn(zone));

Documentation/mm/physical_memory.rst

Quoted 13 lines
Documentation/mm/physical_memory.rst +12 -0
+	in_zone_end = clamp(epfn, zone->zone_start_pfn, zone_end_pfn(zone));
+
+	if (spfn < in_zone_start)
+		init_unavailable_range(spfn, in_zone_start, zid, nid);
+
+	if (in_zone_start < in_zone_end)
+		zone->pages_with_online_memmap +=
+			init_unavailable_range(in_zone_start, in_zone_end,
+					       zid, nid);
+
+	if (in_zone_end < epfn)
+		init_unavailable_range(in_zone_end, epfn, zid, nid);
 }

I think we can make it simpler, see below.

Documentation/mm/physical_memory.rst static void __init memmap_init_zone_range(struct

Quoted 2 lines

/*

Documentation/mm/physical_memory.rst +0 -0
@@ -956,9 +986,10 @@ static void __init memmap_init_zone_range(struct

zone *zone,

Quoted 5 lines
Documentation/mm/physical_memory.rst +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 7 lines

Documentation/mm/physical_memory.rst static void __init memmap_init_zone_range(struct · line 992

Quoted 3 lines
Documentation/mm/physical_memory.rst +1 -1
 	if (*hole_pfn < start_pfn)
-		init_unavailable_range(*hole_pfn, start_pfn, zone_id, nid);
+		init_unavailable_range_for_zone(zone, *hole_pfn, start_pfn);

Here *hole_pfn is either inside zone span or below it and in the second case it's enough to adjust page count returned by init_unavailable_range() by (zone_start_pfn - *hole_pfn).

Get it, I will refine it next version.

Quoted 6 lines
Quoted 6 lines
Documentation/mm/physical_memory.rst +0 -0
 	*hole_pfn = end_pfn;
 }
@@ -996,8 +1027,11 @@ static void __init memmap_init(void)
 #else
 	end_pfn = round_up(end_pfn, MAX_ORDER_NR_PAGES);
 #endif
Quoted 8 lines

Documentation/mm/physical_memory.rst static void __init memmap_init(void) · line 1033

Quoted 6 lines
Documentation/mm/physical_memory.rst +4 -2
-	if (hole_pfn < end_pfn)
-		init_unavailable_range(hole_pfn, end_pfn, zone_id, nid);
+	if (hole_pfn < end_pfn) {
+		struct zone *zone = &NODE_DATA(nid)->node_zones[zone_id];
+
+		init_unavailable_range_for_zone(zone, hole_pfn, end_pfn);

Here we know that the range is not in any zone span.

Indeed, the range here does not belong to the zone span. Thank you for your review.

Quoted 7 lines

Documentation/mm/physical_memory.rst static void __init memmap_init(void) · line 1036

Quoted 3 lines
Documentation/mm/physical_memory.rst +1 -0
+	}
 }
 

--
Sincerely yours,
Mike.