Skip to contentSkip to conversation

mm/memory hotplug/unplug: Optimize zone->contiguous update when changes pfn range

PATCH Superseded Tianyou Li 3 participants 8 messages Dec 1, 2025 – Dec 8, 2025

Subsystems (4)

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Tianyou Li

author
#0 lore raw

PATCH v4 1/1 mm/memory hotplug/unplug: Optimize zone->contiguous update when changes pfn range

When invoke move_pfn_range_to_zone or remove_pfn_range_from_zone, it will update the zone->contiguous by checking the new zone's pfn range from the beginning to the end, regardless the previous state of the old zone. When the zone's pfn range is large, the cost of traversing the pfn range to update the zone->contiguous could be significant.

Add fast paths to quickly detect cases where zone is definitely not contiguous without scanning the new zone. The cases are: when the new range did not overlap with previous range, the contiguous should be false; if the new range adjacent with the previous range, just need to check the new range; if the new added pages could not fill the hole of previous zone, the contiguous should be false.

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      |      2s      |       80%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      33s      |      6s      |       81%      |
+----------------+------+---------------+--------------+----------------+
+----------------+------+---------------+--------------+----------------+
|                | Size | Time (before) | Time (after) | Time Reduction |
|                +------+---------------+--------------+----------------+
| Unplug Memory  | 256G |      10s      |      2s      |       80%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      34s      |      6s      |       82%      |
+----------------+------+---------------+--------------+----------------+

[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 : v6.18-rc2
    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.

---
 mm/internal.h       |  8 ++++-
 mm/memory_hotplug.c | 79 ++++++++++++++++++++++++++++++++++++++++++---
 mm/mm_init.c        | 36 +++++++++++++--------
 3 files changed, 103 insertions(+), 20 deletions(-)
mm/internal.h +7 -1
index 1561fc2ff5b8..a94928520a55 100644
@@ -730,7 +730,13 @@ static inline struct page *pageblock_pfn_to_page(unsigned long start_pfn,
 	return __pageblock_pfn_to_page(start_pfn, end_pfn, zone);
 }
 
-void set_zone_contiguous(struct zone *zone);
+enum zone_contiguous_state {
+	CONTIGUOUS_DEFINITELY_NOT = 0,
+	CONTIGUOUS_DEFINITELY = 1,
+	CONTIGUOUS_UNDETERMINED = 2,
+};
+
+void set_zone_contiguous(struct zone *zone, enum zone_contiguous_state state);
 bool pfn_range_intersects_zones(int nid, unsigned long start_pfn,
 			   unsigned long nr_pages);
 
mm/memory_hotplug.c +74 -5

119 lines not on this page · raw message

mm/mm_init.c +22 -14
index 7712d887b696..06db3fcf7f95 100644
@@ -2263,26 +2263,34 @@ void __init init_cma_pageblock(struct page *page)
 }
 #endif
 
-void set_zone_contiguous(struct zone *zone)
+void set_zone_contiguous(struct zone *zone, enum zone_contiguous_state state)
 {
 	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) {
+	if (state == CONTIGUOUS_DEFINITELY) {
+		zone->contiguous = true;
+		return;
+	} else if (state == CONTIGUOUS_DEFINITELY_NOT) {
+		// zone contiguous has already cleared as false, just return.
+		return;
+	} else if (state == CONTIGUOUS_UNDETERMINED) {
+		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));
+			block_end_pfn = min(block_end_pfn, zone_end_pfn(zone));
 
-		if (!__pageblock_pfn_to_page(block_start_pfn,
-					     block_end_pfn, zone))
-			return;
-		cond_resched();
-	}
+			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;
+		/* We confirm that there is no hole */
+		zone->contiguous = true;
+	}
 }
 
 /*
@@ -2348,7 +2356,7 @@ void __init page_alloc_init_late(void)
 		shuffle_free_memory(NODE_DATA(nid));
 
 	for_each_populated_zone(zone)
-		set_zone_contiguous(zone);
+		set_zone_contiguous(zone, CONTIGUOUS_UNDETERMINED);
 
 	/* Initialize page ext after all struct pages are initialized. */
 	if (deferred_struct_pages)
Signature · 1 line
2.47.1
#1 lore raw
Quoted 82 lines · Tianyou Li

When invoke move_pfn_range_to_zone or remove_pfn_range_from_zone, it will update the zone->contiguous by checking the new zone's pfn range from the beginning to the end, regardless the previous state of the old zone. When the zone's pfn range is large, the cost of traversing the pfn range to update the zone->contiguous could be significant.

Add fast paths to quickly detect cases where zone is definitely not contiguous without scanning the new zone. The cases are: when the new range did not overlap with previous range, the contiguous should be false; if the new range adjacent with the previous range, just need to check the new range; if the new added pages could not fill the hole of previous zone, the contiguous should be false.

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      |      2s      |       80%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      33s      |      6s      |       81%      |
+----------------+------+---------------+--------------+----------------+
+----------------+------+---------------+--------------+----------------+
|                | Size | Time (before) | Time (after) | Time Reduction |
|                +------+---------------+--------------+----------------+
| Unplug Memory  | 256G |      10s      |      2s      |       80%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      34s      |      6s      |       82%      |
+----------------+------+---------------+--------------+----------------+

[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 : v6.18-rc2
     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.

---
  mm/internal.h       |  8 ++++-
  mm/memory_hotplug.c | 79 ++++++++++++++++++++++++++++++++++++++++++---
  mm/mm_init.c        | 36 +++++++++++++--------
  3 files changed, 103 insertions(+), 20 deletions(-)
mm/internal.h +0 -0
index 1561fc2ff5b8..a94928520a55 100644

mm/internal.h static inline struct page *pageblock_pfn_to_page(unsigned long start_pfn, · line 736

Quoted 8 lines
mm/internal.h +4 -1
@@ -730,7 +730,13 @@ static inline struct page *pageblock_pfn_to_page(unsigned long start_pfn,
  	return __pageblock_pfn_to_page(start_pfn, end_pfn, zone);
  }
 
-void set_zone_contiguous(struct zone *zone);
+enum zone_contiguous_state {
+	CONTIGUOUS_DEFINITELY_NOT = 0,
+	CONTIGUOUS_DEFINITELY = 1,
+	CONTIGUOUS_UNDETERMINED = 2,

No need for the values.

mm/internal.h static inline struct page *pageblock_pfn_to_page(unsigned long start_pfn, · line 737

Quoted 1 line
mm/internal.h +1 -0
+};

I don't like that the defines don't match the enum name (zone_c... vs. CONT... ).

Essentially you want a "yes / no / maybe" tristate. I don't think we have an existing type for that, unfortunately.

enum zone_contig_state {
ZONE_CONTIG_YES,
ZONE_CONTIG_NO,
ZONE_CONTIG_MAYBE,
};

Maybe someone reading along has a better idea.

Quoted 25 lines
mm/internal.h +2 -0
+
+void set_zone_contiguous(struct zone *zone, enum zone_contiguous_state state);
  bool pfn_range_intersects_zones(int nid, unsigned long start_pfn,
  			   unsigned long nr_pages);
 
mm/memory_hotplug.c +12 -0
index 0be83039c3b5..b74e558ce822 100644
@@ -544,6 +544,32 @@ static void update_pgdat_span(struct pglist_data *pgdat)
  	pgdat->node_spanned_pages = node_end_pfn - node_start_pfn;
  }
 
+static enum zone_contiguous_state __meminit clear_zone_contiguous_for_shrinking(
+		struct zone *zone, unsigned long start_pfn, unsigned long nr_pages)
+{
+	const unsigned long end_pfn = start_pfn + nr_pages;
+	enum zone_contiguous_state result = CONTIGUOUS_UNDETERMINED;
+
+	/*
+	 * If the removed pfn range inside the original zone span, the contiguous
+	 * property is surely false.
+	 */
+	if (start_pfn > zone->zone_start_pfn && end_pfn < zone_end_pfn(zone))
+		result = CONTIGUOUS_DEFINITELY_NOT;

mm/memory_hotplug.c static void update_pgdat_span(struct pglist_data *pgdat) · line 568

Quoted 10 lines
mm/memory_hotplug.c +10 -0
+
+	/*
+	 * If the removed pfn range is at the beginning or end of the
+	 * original zone span, the contiguous property is preserved when
+	 * the original zone is contiguous.
+	 */
+	else if (start_pfn == zone->zone_start_pfn || end_pfn == zone_end_pfn(zone))
+		result = zone->contiguous ?
+			CONTIGUOUS_DEFINITELY : CONTIGUOUS_UNDETERMINED;
+

See my comment below on how to make this readable.

Quoted 16 lines
mm/memory_hotplug.c +5 -0
+	clear_zone_contiguous(zone);
+	return result;
+}
+
  void remove_pfn_range_from_zone(struct zone *zone,
  				      unsigned long start_pfn,
  				      unsigned long nr_pages)
@@ -551,6 +577,7 @@ void remove_pfn_range_from_zone(struct zone *zone,
  	const unsigned long end_pfn = start_pfn + nr_pages;
  	struct pglist_data *pgdat = zone->zone_pgdat;
  	unsigned long pfn, cur_nr_pages;
+	enum zone_contiguous_state contiguous_state = CONTIGUOUS_UNDETERMINED;
 
  	/* Poison struct pages because they are now uninitialized again. */
  	for (pfn = start_pfn; pfn < end_pfn; pfn += cur_nr_pages) {

mm/memory_hotplug.c void remove_pfn_range_from_zone(struct zone *zone, · line 603

Quoted 7 lines
mm/memory_hotplug.c +2 -1
@@ -571,12 +598,13 @@ void remove_pfn_range_from_zone(struct zone *zone,
  	if (zone_is_zone_device(zone))
  		return;
 
-	clear_zone_contiguous(zone);
+	contiguous_state = clear_zone_contiguous_for_shrinking(
+				zone, start_pfn, nr_pages);
 

Reading this again, I wonder whether it would be nicer to have something like:

new_contig_state = zone_contig_state_after_shrinking(); clear_zone_contiguous(zone);

or sth like that. Similar for the growing case.

Quoted 13 lines
mm/memory_hotplug.c +2 -1
  	shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
  	update_pgdat_span(pgdat);
 
-	set_zone_contiguous(zone);
+	set_zone_contiguous(zone, contiguous_state);
  }
 
  /**
@@ -736,6 +764,47 @@ static inline void section_taint_zone_device(unsigned long pfn)
  }
  #endif
 
+static enum zone_contiguous_state __meminit clear_zone_contiguous_for_growing(

mm/memory_hotplug.c static inline void section_taint_zone_device(unsigned long pfn) · line 777

Quoted 10 lines
mm/memory_hotplug.c +10 -0
+		struct zone *zone, unsigned long start_pfn, unsigned long nr_pages)
+{
+	const unsigned long end_pfn = start_pfn + nr_pages;
+	enum zone_contiguous_state result = CONTIGUOUS_UNDETERMINED;
+
+	/*
+	 * Given the moved pfn range's contiguous property is always true,
+	 * under the conditional of empty zone, the contiguous property should
+	 * be true.
+	 */

I don't think that comment is required.

Quoted 16 lines
mm/memory_hotplug.c +16 -0
+	if (zone_is_empty(zone))
+		result = CONTIGUOUS_DEFINITELY;
+
+	/*
+	 * If the moved pfn range does not intersect with the original zone span,
+	 * the contiguous property is surely false.
+	 */
+	else if (end_pfn < zone->zone_start_pfn || start_pfn > zone_end_pfn(zone))
+		result = CONTIGUOUS_DEFINITELY_NOT;
+
+	/*
+	 * If the moved pfn range is adjacent to the original zone span, given
+	 * the moved pfn range's contiguous property is always true, the zone's
+	 * contiguous property inherited from the original value.
+	 */
+	else if (end_pfn == zone->zone_start_pfn || start_pfn == zone_end_pfn(zone))

mm/memory_hotplug.c static inline void section_taint_zone_device(unsigned long pfn) · line 803

Quoted 10 lines
mm/memory_hotplug.c +10 -0
+		result = zone->contiguous ?
+			CONTIGUOUS_DEFINITELY : CONTIGUOUS_DEFINITELY_NOT;
+
+	/*
+	 * If the original zone's hole larger than the moved pages in the range,
+	 * the contiguous property is surely false.
+	 */
+	else if (nr_pages < (zone->spanned_pages - zone->present_pages))
+		result = CONTIGUOUS_DEFINITELY_NOT;
+

This is a bit unreadable :)

if (zone_is_empty(zone)) {
	result = CONTIGUOUS_DEFINITELY;
} else if (...) {
	/* ... */
	...
} else if (...) {
	...
}
Quoted 68 lines
mm/memory_hotplug.c +7 -3
+	clear_zone_contiguous(zone);
+	return result;
+}
+
  /*
   * Associate the pfn range with the given zone, initializing the memmaps
   * and resizing the pgdat/zone data to span the added pages. After this
@@ -752,8 +821,8 @@ 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);
+	const enum zone_contiguous_state contiguous_state =
+		clear_zone_contiguous_for_growing(zone, start_pfn, nr_pages);
 
  	if (zone_is_empty(zone))
  		init_currently_empty_zone(zone, start_pfn, nr_pages);
@@ -783,7 +852,7 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
  			 MEMINIT_HOTPLUG, altmap, migratetype,
  			 isolate_pageblock);
 
-	set_zone_contiguous(zone);
+	set_zone_contiguous(zone, contiguous_state);
  }
 
  struct auto_movable_stats {
mm/mm_init.c +16 -11
index 7712d887b696..06db3fcf7f95 100644
@@ -2263,26 +2263,34 @@ void __init init_cma_pageblock(struct page *page)
  }
  #endif
 
-void set_zone_contiguous(struct zone *zone)
+void set_zone_contiguous(struct zone *zone, enum zone_contiguous_state state)
  {
  	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) {
+	if (state == CONTIGUOUS_DEFINITELY) {
+		zone->contiguous = true;
+		return;
+	} else if (state == CONTIGUOUS_DEFINITELY_NOT) {
+		// zone contiguous has already cleared as false, just return.
+		return;
+	} else if (state == CONTIGUOUS_UNDETERMINED) {
+		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));
+			block_end_pfn = min(block_end_pfn, zone_end_pfn(zone));
 
-		if (!__pageblock_pfn_to_page(block_start_pfn,
-					     block_end_pfn, zone))
-			return;
-		cond_resched();
-	}
+			if (!__pageblock_pfn_to_page(block_start_pfn,
+						block_end_pfn, zone))
+				return;

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

Quoted 10 lines
mm/mm_init.c +5 -2
+			cond_resched();
+		}
 
-	/* We confirm that there is no hole */
-	zone->contiguous = true;
+		/* We confirm that there is no hole */
+		zone->contiguous = true;
+	}
  }
 

switch (state) {
case CONTIGUOUS_DEFINITELY:

	zone->contiguous = true;
	return;
case CONTIGUOUS_DEFINITELY_NOT:
	return;
default:
	break;
}

... unchanged logic.

Quoted 10 lines
mm/mm_init.c +1 -1
  /*
@@ -2348,7 +2356,7 @@ void __init page_alloc_init_late(void)
  		shuffle_free_memory(NODE_DATA(nid));
 
  	for_each_populated_zone(zone)
-		set_zone_contiguous(zone);
+		set_zone_contiguous(zone, CONTIGUOUS_UNDETERMINED);
 
  	/* Initialize page ext after all struct pages are initialized. */
  	if (deferred_struct_pages)
Signature · 3 lines
Cheers

David
#2 lore raw

Thanks David for the detailed review comments. I will change the code accordingly after one or two days, in case any other review changes needed to accommodate. Very appreciated.

Quoted 82 lines · David Hildenbrand (Red Hat)
Quoted 81 lines · Tianyou Li

When invoke move_pfn_range_to_zone or remove_pfn_range_from_zone, it will update the zone->contiguous by checking the new zone's pfn range from the beginning to the end, regardless the previous state of the old zone. When the zone's pfn range is large, the cost of traversing the pfn range to update the zone->contiguous could be significant.

Add fast paths to quickly detect cases where zone is definitely not contiguous without scanning the new zone. The cases are: when the new range did not overlap with previous range, the contiguous should be false; if the new range adjacent with the previous range, just need to check the new range; if the new added pages could not fill the hole of previous zone, the contiguous should be false.

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      |      2s      | 80%      |
| +------+---------------+--------------+----------------+
|                | 512G |      33s      |      6s      | 81%      |
+----------------+------+---------------+--------------+----------------+ 

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

|                | Size | Time (before) | Time (after) | Time Reduction |
| +------+---------------+--------------+----------------+
| Unplug Memory  | 256G |      10s      |      2s      | 80%      |
| +------+---------------+--------------+----------------+
|                | 512G |      34s      |      6s      | 82%      |
+----------------+------+---------------+--------------+----------------+ 

[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 : v6.18-rc2
     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.

---
  mm/internal.h       |  8 ++++-
  mm/memory_hotplug.c | 79 ++++++++++++++++++++++++++++++++++++++++++--- mm/mm_init.c        | 36 +++++++++++++--------

  3 files changed, 103 insertions(+), 20 deletions(-)
Quoted 16 lines

mm/internal.h static inline struct page · line 737

Quoted 13 lines
mm/internal.h +4 -0
index 1561fc2ff5b8..a94928520a55 100644
@@ -730,7 +730,13 @@ static inline struct page 
 *pageblock_pfn_to_page(unsigned long start_pfn,
       return __pageblock_pfn_to_page(start_pfn, end_pfn, zone);
   }
   -void set_zone_contiguous(struct zone *zone);
+enum zone_contiguous_state {
+    CONTIGUOUS_DEFINITELY_NOT = 0,
+    CONTIGUOUS_DEFINITELY = 1,
+    CONTIGUOUS_UNDETERMINED = 2,

No need for the values.

Got it.

Quoted 6 lines

mm/internal.h static inline struct page *pageblock_pfn_to_page(unsigned long start_pfn, · line 737

Quoted 1 line
mm/internal.h +1 -0
+};

I don't like that the defines don't match the enum name (zone_c... vs. CONT... ).

Essentially you want a "yes / no / maybe" tristate. I don't think we have an existing type for that, unfortunately.

Quoted 8 lines

enum zone_contig_state {
    ZONE_CONTIG_YES,
    ZONE_CONTIG_NO,
    ZONE_CONTIG_MAYBE,
};

Maybe someone reading along has a better idea.

I agree it's better. Will wait for a day or two to make the change.

Quoted 66 lines
Quoted 31 lines
mm/internal.h +2 -0
+
+void set_zone_contiguous(struct zone *zone, enum 
 zone_contiguous_state state);
   bool pfn_range_intersects_zones(int nid, unsigned long start_pfn,

                 unsigned long nr_pages);
  diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 0be83039c3b5..b74e558ce822 100644

mm/memory_hotplug.c +0 -0
@@ -544,6 +544,32 @@ static void update_pgdat_span(struct pglist_data 
 *pgdat)
       pgdat->node_spanned_pages = node_end_pfn - node_start_pfn;
   }

  +static enum zone_contiguous_state __meminit clear_zone_contiguous_for_shrinking( +        struct zone *zone, unsigned long start_pfn, unsigned long nr_pages)

mm/memory_hotplug.c +6 -0
+{
+    const unsigned long end_pfn = start_pfn + nr_pages;
+    enum zone_contiguous_state result = CONTIGUOUS_UNDETERMINED;
+
+    /*
+     * If the removed pfn range inside the original zone span, the 

contiguous
+     * property is surely false.
+     */
+    if (start_pfn > zone->zone_start_pfn && end_pfn < zone_end_pfn(zone))
+        result = CONTIGUOUS_DEFINITELY_NOT;
+
+    /*
+     * If the removed pfn range is at the beginning or end of the

Quoted 8 lines

+     * original zone span, the contiguous property is preserved when +     * the original zone is contiguous.
+     */
+    else if (start_pfn == zone->zone_start_pfn || end_pfn == zone_end_pfn(zone))
+        result = zone->contiguous ?
+            CONTIGUOUS_DEFINITELY : CONTIGUOUS_UNDETERMINED; +

See my comment below on how to make this readable.

Quoted 16 lines

+    clear_zone_contiguous(zone);
+    return result;

mm/memory_hotplug.c +2 -0
+}
+

  void remove_pfn_range_from_zone(struct zone *zone, unsigned long start_pfn, unsigned long nr_pages)

mm/memory_hotplug.c +1 -0
@@ -551,6 +577,7 @@ void remove_pfn_range_from_zone(struct zone *zone,
       const unsigned long end_pfn = start_pfn + nr_pages;
       struct pglist_data *pgdat = zone->zone_pgdat;
       unsigned long pfn, cur_nr_pages;
+    enum zone_contiguous_state contiguous_state = 
 CONTIGUOUS_UNDETERMINED;
         /* Poison struct pages because they are now uninitialized again. */
       for (pfn = start_pfn; pfn < end_pfn; pfn += cur_nr_pages) {

mm/memory_hotplug.c void remove_pfn_range_from_zone(struct zone *zone, · line 602

Quoted 5 lines
mm/memory_hotplug.c +2 -0
@@ -571,12 +598,13 @@ void remove_pfn_range_from_zone(struct zone *zone,
       if (zone_is_zone_device(zone))
           return;
   -    clear_zone_contiguous(zone);
+    contiguous_state = clear_zone_contiguous_for_shrinking(
+                zone, start_pfn, nr_pages);

Reading this again, I wonder whether it would be nicer to have something like:

Quoted 5 lines

new_contig_state = zone_contig_state_after_shrinking(); clear_zone_contiguous(zone);

or sth like that. Similar for the growing case.

In both shrinking and growing case, separate the clear_zone_contiguous from the logic of zone state check, right?

Quoted 17 lines
Quoted 17 lines
mm/memory_hotplug.c +1 -0
       shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
       update_pgdat_span(pgdat);
   -    set_zone_contiguous(zone);
+    set_zone_contiguous(zone, contiguous_state);
   }
     /**
@@ -736,6 +764,47 @@ static inline void 
 section_taint_zone_device(unsigned long pfn)
   }
   #endif

  +static enum zone_contiguous_state __meminit clear_zone_contiguous_for_growing( +        struct zone *zone, unsigned long start_pfn, unsigned long nr_pages)

mm/memory_hotplug.c +4 -0
+{
+    const unsigned long end_pfn = start_pfn + nr_pages;
+    enum zone_contiguous_state result = CONTIGUOUS_UNDETERMINED;
+
Quoted 9 lines
Quoted 6 lines

+    /*
+     * Given the moved pfn range's contiguous property is always true, +     * under the conditional of empty zone, the contiguous property should
+     * be true.
+     */

I don't think that comment is required.

Got it.

Quoted 35 lines
Quoted 24 lines

+    if (zone_is_empty(zone))
+        result = CONTIGUOUS_DEFINITELY;
+
+    /*
+     * If the moved pfn range does not intersect with the original zone span,
+     * the contiguous property is surely false.
+     */
+    else if (end_pfn < zone->zone_start_pfn || start_pfn > zone_end_pfn(zone))
+        result = CONTIGUOUS_DEFINITELY_NOT;
+
+    /*
+     * If the moved pfn range is adjacent to the original zone span, given
+     * the moved pfn range's contiguous property is always true, the zone's
+     * contiguous property inherited from the original value. +     */
+    else if (end_pfn == zone->zone_start_pfn || start_pfn == zone_end_pfn(zone))
+        result = zone->contiguous ?
+            CONTIGUOUS_DEFINITELY : CONTIGUOUS_DEFINITELY_NOT; +

Quoted 8 lines

+    /*
+     * If the original zone's hole larger than the moved pages in the range,
+     * the contiguous property is surely false.
+     */
+    else if (nr_pages < (zone->spanned_pages - zone->present_pages)) +        result = CONTIGUOUS_DEFINITELY_NOT;
+

This is a bit unreadable :)

Quoted 9 lines

if (zone_is_empty(zone)) {
    result = CONTIGUOUS_DEFINITELY;
} else if (...) {
    /* ... */
    ...
} else if (...) {
    ...
}

Yes, I am thinking of that during coding. I was keeping that way for a 'better looking'. I agree that will not be a good case for maintaining the code. Thanks.

Quoted 85 lines
Quoted 67 lines

+    clear_zone_contiguous(zone);
+    return result;

mm/memory_hotplug.c +2 -0
+}
+

  /*
   * Associate the pfn range with the given zone, initializing the memmaps
   * and resizing the pgdat/zone data to span the added pages. After this

mm/memory_hotplug.c +3 -2
@@ -752,8 +821,8 @@ 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);
+    const enum zone_contiguous_state contiguous_state =
+        clear_zone_contiguous_for_growing(zone, start_pfn, nr_pages);
         if (zone_is_empty(zone))
           init_currently_empty_zone(zone, start_pfn, nr_pages);
@@ -783,7 +852,7 @@ void move_pfn_range_to_zone(struct zone *zone, 
 unsigned long start_pfn,
                MEMINIT_HOTPLUG, altmap, migratetype,
                isolate_pageblock);
   -    set_zone_contiguous(zone);
+    set_zone_contiguous(zone, contiguous_state);
   }
     struct auto_movable_stats {
mm/mm_init.c +18 -7
index 7712d887b696..06db3fcf7f95 100644
@@ -2263,26 +2263,34 @@ void __init init_cma_pageblock(struct page 
 *page)
   }
   #endif
   -void set_zone_contiguous(struct zone *zone)
+void set_zone_contiguous(struct zone *zone, enum 
 zone_contiguous_state state)
   {
       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) {
+    if (state == CONTIGUOUS_DEFINITELY) {
+        zone->contiguous = true;
+        return;
+    } else if (state == CONTIGUOUS_DEFINITELY_NOT) {
+        // zone contiguous has already cleared as false, just return.
+        return;
+    } else if (state == CONTIGUOUS_UNDETERMINED) {
+        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));
+            block_end_pfn = min(block_end_pfn, zone_end_pfn(zone));
   -        if (!__pageblock_pfn_to_page(block_start_pfn,
-                         block_end_pfn, zone))
-            return;
-        cond_resched();
-    }
+            if (!__pageblock_pfn_to_page(block_start_pfn,
+                        block_end_pfn, zone))
+                return;
+            cond_resched();
+        }
Quoted 6 lines

  -    /* We confirm that there is no hole */
-    zone->contiguous = true;
+        /* We confirm that there is no hole */
+        zone->contiguous = true;
+    }
  }

switch (state) {
case CONTIGUOUS_DEFINITELY:
    zone->contiguous = true;
    return;
case CONTIGUOUS_DEFINITELY_NOT:
    return;
default:
    break;
}

Quoted 2 lines

... unchanged logic.

Will do. Thanks.

Quoted 10 lines

mm/mm_init.c void __init page_alloc_init_late(void) · line 2360

Quoted 8 lines

  /*

mm/mm_init.c +1 -1
@@ -2348,7 +2356,7 @@ void __init page_alloc_init_late(void)
           shuffle_free_memory(NODE_DATA(nid));
         for_each_populated_zone(zone)
-        set_zone_contiguous(zone);
+        set_zone_contiguous(zone, CONTIGUOUS_UNDETERMINED);
         /* Initialize page ext after all struct pages are initialized. */
       if (deferred_struct_pages)

Regards,

Tianyou

#3 lore raw
Quoted 14 lines
Quoted 14 lines
Quoted 1 line

+};

I don't like that the defines don't match the enum name (zone_c... vs. CONT... ).

Essentially you want a "yes / no / maybe" tristate. I don't think we have an existing type for that, unfortunately.

enum zone_contig_state {
    ZONE_CONTIG_YES,
    ZONE_CONTIG_NO,
    ZONE_CONTIG_MAYBE,
};

Quoted 5 lines
Quoted 2 lines

Maybe someone reading along has a better idea.

I agree it's better. Will wait for a day or two to make the change.

Yes, good idea. No needs to rush at this point because the merge window just opened up.

Quoted 70 lines
Quoted 70 lines
Quoted 33 lines
(quoted diff) +2 -0
+
+void set_zone_contiguous(struct zone *zone, enum

zone_contiguous_state state);
  bool pfn_range_intersects_zones(int nid, unsigned long start_pfn, unsigned long nr_pages);
  diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index 0be83039c3b5..b74e558ce822 100644

mm/memory_hotplug.c +0 -0
@@ -544,6 +544,32 @@ static void update_pgdat_span(struct pglist_data
 *pgdat)
       pgdat->node_spanned_pages = node_end_pfn - node_start_pfn;
   }
   +static enum zone_contiguous_state __meminit

clear_zone_contiguous_for_shrinking(
+        struct zone *zone, unsigned long start_pfn, unsigned long nr_pages)

mm/memory_hotplug.c +6 -0
+{
+    const unsigned long end_pfn = start_pfn + nr_pages;
+    enum zone_contiguous_state result = CONTIGUOUS_UNDETERMINED;
+
+    /*
+     * If the removed pfn range inside the original zone span, the

contiguous
+     * property is surely false.
+     */
+    if (start_pfn > zone->zone_start_pfn && end_pfn < zone_end_pfn(zone))
+        result = CONTIGUOUS_DEFINITELY_NOT;
+
+    /*
+     * If the removed pfn range is at the beginning or end of the

Quoted 8 lines

+     * original zone span, the contiguous property is preserved when +     * the original zone is contiguous.
+     */
+    else if (start_pfn == zone->zone_start_pfn || end_pfn == zone_end_pfn(zone))
+        result = zone->contiguous ?
+            CONTIGUOUS_DEFINITELY : CONTIGUOUS_UNDETERMINED; +

See my comment below on how to make this readable.

Quoted 17 lines

+    clear_zone_contiguous(zone);
+    return result;

mm/memory_hotplug.c +3 -0
+}
+
   void remove_pfn_range_from_zone(struct zone *zone,
                         unsigned long start_pfn,
                         unsigned long nr_pages)
@@ -551,6 +577,7 @@ void remove_pfn_range_from_zone(struct zone *zone,
       const unsigned long end_pfn = start_pfn + nr_pages;
       struct pglist_data *pgdat = zone->zone_pgdat;
       unsigned long pfn, cur_nr_pages;
+    enum zone_contiguous_state contiguous_state =
 CONTIGUOUS_UNDETERMINED;
         /* Poison struct pages because they are now uninitialized

again. */
      for (pfn = start_pfn; pfn < end_pfn; pfn += cur_nr_pages) {

mm/memory_hotplug.c void remove_pfn_range_from_zone(struct zone *zone, · line 602

Quoted 5 lines
mm/memory_hotplug.c +2 -0
@@ -571,12 +598,13 @@ void remove_pfn_range_from_zone(struct zone *zone,
       if (zone_is_zone_device(zone))
           return;
   -    clear_zone_contiguous(zone);
+    contiguous_state = clear_zone_contiguous_for_shrinking(
+                zone, start_pfn, nr_pages);

Reading this again, I wonder whether it would be nicer to have something like:

Quoted 8 lines
Quoted 5 lines

new_contig_state = zone_contig_state_after_shrinking(); clear_zone_contiguous(zone);

or sth like that. Similar for the growing case.

In both shrinking and growing case, separate the clear_zone_contiguous from the logic of zone state check, right?

Yes, I think that makes it look a bit nicer.

Signature · 3 lines
Cheers

David
#4 lore raw
Quoted 14 lines · David Hildenbrand (Red Hat)
Quoted 14 lines
Quoted 14 lines
Quoted 1 line

+};

I don't like that the defines don't match the enum name (zone_c... vs. CONT... ).

Essentially you want a "yes / no / maybe" tristate. I don't think we have an existing type for that, unfortunately.

enum zone_contig_state {
     ZONE_CONTIG_YES,
     ZONE_CONTIG_NO,
     ZONE_CONTIG_MAYBE,
};

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Maybe someone reading along has a better idea.

I agree it's better. Will wait for a day or two to make the change.

Yes, good idea. No needs to rush at this point because the merge window just opened up.

Got it. Allow me to take one more day then complete the patch v5 with sufficient testing.

Quoted 74 lines
Quoted 74 lines
Quoted 74 lines
Quoted 33 lines
(quoted diff) +2 -0
+
+void set_zone_contiguous(struct zone *zone, enum

zone_contiguous_state state);
   bool pfn_range_intersects_zones(int nid, unsigned long start_pfn, unsigned long nr_pages);
   diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 0be83039c3b5..b74e558ce822 100644

mm/memory_hotplug.c +0 -0
@@ -544,6 +544,32 @@ static void update_pgdat_span(struct pglist_data
 *pgdat)
        pgdat->node_spanned_pages = node_end_pfn - node_start_pfn;
    }

   +static enum zone_contiguous_state __meminit clear_zone_contiguous_for_shrinking(
+        struct zone *zone, unsigned long start_pfn, unsigned long nr_pages)

mm/memory_hotplug.c +6 -0
+{
+    const unsigned long end_pfn = start_pfn + nr_pages;
+    enum zone_contiguous_state result = CONTIGUOUS_UNDETERMINED;
+
+    /*
+     * If the removed pfn range inside the original zone span, the

contiguous
+     * property is surely false.
+     */
+    if (start_pfn > zone->zone_start_pfn && end_pfn < zone_end_pfn(zone))
+        result = CONTIGUOUS_DEFINITELY_NOT;
+
+    /*
+     * If the removed pfn range is at the beginning or end of the

Quoted 8 lines

+     * original zone span, the contiguous property is preserved when +     * the original zone is contiguous.
+     */
+    else if (start_pfn == zone->zone_start_pfn || end_pfn == zone_end_pfn(zone))
+        result = zone->contiguous ?
+            CONTIGUOUS_DEFINITELY : CONTIGUOUS_UNDETERMINED; +

See my comment below on how to make this readable.

Quoted 17 lines

+    clear_zone_contiguous(zone);
+    return result;

mm/memory_hotplug.c +2 -0
+}
+

   void remove_pfn_range_from_zone(struct zone *zone, unsigned long start_pfn, unsigned long nr_pages)

mm/memory_hotplug.c +1 -0
@@ -551,6 +577,7 @@ void remove_pfn_range_from_zone(struct zone *zone,
        const unsigned long end_pfn = start_pfn + nr_pages;
        struct pglist_data *pgdat = zone->zone_pgdat;
        unsigned long pfn, cur_nr_pages;
+    enum zone_contiguous_state contiguous_state =
 CONTIGUOUS_UNDETERMINED;
          /* Poison struct pages because they are now uninitialized

again. */
       for (pfn = start_pfn; pfn < end_pfn; pfn += cur_nr_pages) {

mm/memory_hotplug.c void remove_pfn_range_from_zone(struct zone · line 603

Quoted 6 lines
mm/memory_hotplug.c +2 -0
@@ -571,12 +598,13 @@ void remove_pfn_range_from_zone(struct zone 
 *zone,
        if (zone_is_zone_device(zone))
            return;
    -    clear_zone_contiguous(zone);
+    contiguous_state = clear_zone_contiguous_for_shrinking(
+                zone, start_pfn, nr_pages);

Reading this again, I wonder whether it would be nicer to have something like:

new_contig_state = zone_contig_state_after_shrinking(); clear_zone_contiguous(zone);

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

or sth like that. Similar for the growing case.

In both shrinking and growing case, separate the clear_zone_contiguous from the logic of zone state check, right?

Yes, I think that makes it look a bit nicer.

Thanks for the confirmation David. Noted and will do. Thanks.

#5 lore raw

Sorry for the delayed. We were working on a suspected issue and now the patch v5 is out. Thanks for your review in advance.

Regards,

Tianyou

Quoted 117 lines · Tianyou
Quoted 14 lines · David Hildenbrand (Red Hat)
Quoted 14 lines
Quoted 14 lines
Quoted 1 line

+};

I don't like that the defines don't match the enum name (zone_c... vs. CONT... ).

Essentially you want a "yes / no / maybe" tristate. I don't think we have an existing type for that, unfortunately.

enum zone_contig_state {
     ZONE_CONTIG_YES,
     ZONE_CONTIG_NO,
     ZONE_CONTIG_MAYBE,
};

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Maybe someone reading along has a better idea.

I agree it's better. Will wait for a day or two to make the change.

Yes, good idea. No needs to rush at this point because the merge window just opened up.

Got it. Allow me to take one more day then complete the patch v5 with sufficient testing.

Quoted 75 lines
Quoted 75 lines
Quoted 75 lines
Quoted 33 lines
(quoted diff) +2 -0
+
+void set_zone_contiguous(struct zone *zone, enum

zone_contiguous_state state);
   bool pfn_range_intersects_zones(int nid, unsigned long start_pfn, unsigned long nr_pages);
   diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 0be83039c3b5..b74e558ce822 100644

mm/memory_hotplug.c +0 -0
@@ -544,6 +544,32 @@ static void update_pgdat_span(struct pglist_data
 *pgdat)
        pgdat->node_spanned_pages = node_end_pfn - node_start_pfn;
    }

   +static enum zone_contiguous_state __meminit clear_zone_contiguous_for_shrinking(
+        struct zone *zone, unsigned long start_pfn, unsigned long nr_pages)

mm/memory_hotplug.c +6 -0
+{
+    const unsigned long end_pfn = start_pfn + nr_pages;
+    enum zone_contiguous_state result = CONTIGUOUS_UNDETERMINED;
+
+    /*
+     * If the removed pfn range inside the original zone span, the

contiguous
+     * property is surely false.
+     */
+    if (start_pfn > zone->zone_start_pfn && end_pfn < zone_end_pfn(zone))
+        result = CONTIGUOUS_DEFINITELY_NOT;
+
+    /*
+     * If the removed pfn range is at the beginning or end of the

Quoted 8 lines

+     * original zone span, the contiguous property is preserved when +     * the original zone is contiguous.
+     */
+    else if (start_pfn == zone->zone_start_pfn || end_pfn == zone_end_pfn(zone))
+        result = zone->contiguous ?
+            CONTIGUOUS_DEFINITELY : CONTIGUOUS_UNDETERMINED; +

See my comment below on how to make this readable.

Quoted 18 lines

+    clear_zone_contiguous(zone);
+    return result;

mm/memory_hotplug.c +2 -0
+}
+

   void remove_pfn_range_from_zone(struct zone *zone, unsigned long start_pfn, unsigned long nr_pages)

mm/memory_hotplug.c +1 -0
@@ -551,6 +577,7 @@ void remove_pfn_range_from_zone(struct zone 
 *zone,
        const unsigned long end_pfn = start_pfn + nr_pages;
        struct pglist_data *pgdat = zone->zone_pgdat;
        unsigned long pfn, cur_nr_pages;
+    enum zone_contiguous_state contiguous_state =
 CONTIGUOUS_UNDETERMINED;
          /* Poison struct pages because they are now uninitialized

again. */
       for (pfn = start_pfn; pfn < end_pfn; pfn += cur_nr_pages) {

mm/memory_hotplug.c void remove_pfn_range_from_zone(struct zone · line 603

Quoted 6 lines
mm/memory_hotplug.c +2 -0
@@ -571,12 +598,13 @@ void remove_pfn_range_from_zone(struct zone 
 *zone,
        if (zone_is_zone_device(zone))
            return;
    -    clear_zone_contiguous(zone);
+    contiguous_state = clear_zone_contiguous_for_shrinking(
+                zone, start_pfn, nr_pages);

Reading this again, I wonder whether it would be nicer to have something like:

new_contig_state = zone_contig_state_after_shrinking(); clear_zone_contiguous(zone);

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

or sth like that. Similar for the growing case.

In both shrinking and growing case, separate the clear_zone_contiguous from the logic of zone state check, right?

Yes, I think that makes it look a bit nicer.

Thanks for the confirmation David. Noted and will do. Thanks.

maintainer
#6 lore raw
In reply to David Hildenbrand (Red Hat)
Quoted 1 line · David Hildenbrand (Red Hat)

On 12/1/25 14:22, Tianyou Li wrote:

...

Quoted 20 lines
Quoted 11 lines

void remove_pfn_range_from_zone(struct zone *zone,
unsigned long start_pfn, unsigned long nr_pages)

mm/memory_hotplug.c +1 -0
@@ -551,6 +577,7 @@ void remove_pfn_range_from_zone(struct zone *zone,
  	const unsigned long end_pfn = start_pfn + nr_pages;
  	struct pglist_data *pgdat = zone->zone_pgdat;
  	unsigned long pfn, cur_nr_pages;
+	enum zone_contiguous_state contiguous_state = CONTIGUOUS_UNDETERMINED;
  	/* Poison struct pages because they are now uninitialized again. */
  	for (pfn = start_pfn; pfn < end_pfn; pfn += cur_nr_pages) {

mm/memory_hotplug.c void remove_pfn_range_from_zone(struct zone *zone, · line 601

Quoted 5 lines
mm/memory_hotplug.c +2 -1
@@ -571,12 +598,13 @@ void remove_pfn_range_from_zone(struct zone *zone,
  	if (zone_is_zone_device(zone))
  		return;
-	clear_zone_contiguous(zone);
+	contiguous_state = clear_zone_contiguous_for_shrinking(
+				zone, start_pfn, nr_pages);

Reading this again, I wonder whether it would be nicer to have something like:

Quoted 2 lines

new_contig_state = zone_contig_state_after_shrinking(); clear_zone_contiguous(zone);

+1

Quoted 43 lines

mm/memory_hotplug.c static inline void section_taint_zone_device(unsigned long pfn) · line 777

Quoted 11 lines
mm/memory_hotplug.c +11 -0
+static enum zone_contiguous_state __meminit clear_zone_contiguous_for_growing(
+		struct zone *zone, unsigned long start_pfn, unsigned long nr_pages)
+{
+	const unsigned long end_pfn = start_pfn + nr_pages;
+	enum zone_contiguous_state result = CONTIGUOUS_UNDETERMINED;
+
+	/*
+	 * Given the moved pfn range's contiguous property is always true,
+	 * under the conditional of empty zone, the contiguous property should
+	 * be true.
+	 */

I don't think that comment is required.

Quoted 16 lines
mm/memory_hotplug.c +16 -0
+	if (zone_is_empty(zone))
+		result = CONTIGUOUS_DEFINITELY;
+
+	/*
+	 * If the moved pfn range does not intersect with the original zone span,
+	 * the contiguous property is surely false.
+	 */
+	else if (end_pfn < zone->zone_start_pfn || start_pfn > zone_end_pfn(zone))
+		result = CONTIGUOUS_DEFINITELY_NOT;
+
+	/*
+	 * If the moved pfn range is adjacent to the original zone span, given
+	 * the moved pfn range's contiguous property is always true, the zone's
+	 * contiguous property inherited from the original value.
+	 */
+	else if (end_pfn == zone->zone_start_pfn || start_pfn == zone_end_pfn(zone))

mm/memory_hotplug.c static inline void section_taint_zone_device(unsigned long pfn) · line 803

Quoted 10 lines
mm/memory_hotplug.c +10 -0
+		result = zone->contiguous ?
+			CONTIGUOUS_DEFINITELY : CONTIGUOUS_DEFINITELY_NOT;
+
+	/*
+	 * If the original zone's hole larger than the moved pages in the range,
+	 * the contiguous property is surely false.
+	 */
+	else if (nr_pages < (zone->spanned_pages - zone->present_pages))
+		result = CONTIGUOUS_DEFINITELY_NOT;
+

This is a bit unreadable :)

Quoted 8 lines
if (zone_is_empty(zone)) {
	result = CONTIGUOUS_DEFINITELY;
} else if (...) {
	/* ... */
	...
} else if (...) {
	...
}

If we update zone state outside this function it can be even simpler:

	if (zone_is_empty(zone))
		return CONTIGUOUS_DEFINITELY;

	if (nr_pages < (zone->spanned_pages - zone->present_pages))
		return CONTIGUOUS_DEFINITELY_NOT;

etc.

Quoted 37 lines
Quoted 37 lines
mm/memory_hotplug.c +7 -3
+	clear_zone_contiguous(zone);
+	return result;
+}
+
  /*
   * Associate the pfn range with the given zone, initializing the memmaps
   * and resizing the pgdat/zone data to span the added pages. After this
@@ -752,8 +821,8 @@ 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);
+	const enum zone_contiguous_state contiguous_state =
+		clear_zone_contiguous_for_growing(zone, start_pfn, nr_pages);
  	if (zone_is_empty(zone))
  		init_currently_empty_zone(zone, start_pfn, nr_pages);
@@ -783,7 +852,7 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
  			 MEMINIT_HOTPLUG, altmap, migratetype,
  			 isolate_pageblock);
-	set_zone_contiguous(zone);
+	set_zone_contiguous(zone, contiguous_state);
  }
  struct auto_movable_stats {
mm/mm_init.c +1 -2
index 7712d887b696..06db3fcf7f95 100644
@@ -2263,26 +2263,34 @@ void __init init_cma_pageblock(struct page *page)
  }
  #endif
-void set_zone_contiguous(struct zone *zone)
+void set_zone_contiguous(struct zone *zone, enum zone_contiguous_state state)
  {
  	unsigned long block_start_pfn = zone->zone_start_pfn;
  	unsigned long block_end_pfn;
-	block_end_pfn = pageblock_end_pfn(block_start_pfn);
Quoted 8 lines

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

Quoted 8 lines
mm/mm_init.c +5 -3
-	for (; block_start_pfn < zone_end_pfn(zone);
-			block_start_pfn = block_end_pfn,
-			 block_end_pfn += pageblock_nr_pages) {
+	if (state == CONTIGUOUS_DEFINITELY) {
+		zone->contiguous = true;
+		return;
+	} else if (state == CONTIGUOUS_DEFINITELY_NOT) {
+		// zone contiguous has already cleared as false, just return.

Please no C++ style comments.

Quoted 28 lines
Quoted 14 lines
mm/mm_init.c +8 -6
+		return;
+	} else if (state == CONTIGUOUS_UNDETERMINED) {
+		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));
+			block_end_pfn = min(block_end_pfn, zone_end_pfn(zone));
-		if (!__pageblock_pfn_to_page(block_start_pfn,
-					     block_end_pfn, zone))
-			return;
-		cond_resched();
-	}
+			if (!__pageblock_pfn_to_page(block_start_pfn,

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

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

switch (state) {
case CONTIGUOUS_DEFINITELY:

Quoted 8 lines
	zone->contiguous = true;
	return;
case CONTIGUOUS_DEFINITELY_NOT:
	return;
default:
	break;
}

... unchanged logic.

I was going to suggest rather to drop 'else'

	if (state == CONTIGUOUS_DEFINITELY) {
		zone->contiguous = true;
		return;
	}

	if (state == CONTIGUOUS_DEFINITELY_NOT) 
		return;

	... unchanged logic.

but I don't feel strongly about it.

Signature · 2 lines
Sincerely yours,
Mike.
#7 lore raw
Quoted 78 lines · Mike Rapoport
Quoted 1 line · David Hildenbrand (Red Hat)

On 12/1/25 14:22, Tianyou Li wrote:

...

Quoted 19 lines
Quoted 11 lines

void remove_pfn_range_from_zone(struct zone *zone,
unsigned long start_pfn, unsigned long nr_pages)

mm/memory_hotplug.c +1 -0
@@ -551,6 +577,7 @@ void remove_pfn_range_from_zone(struct zone *zone,
   	const unsigned long end_pfn = start_pfn + nr_pages;
   	struct pglist_data *pgdat = zone->zone_pgdat;
   	unsigned long pfn, cur_nr_pages;
+	enum zone_contiguous_state contiguous_state = CONTIGUOUS_UNDETERMINED;
   	/* Poison struct pages because they are now uninitialized again. */
   	for (pfn = start_pfn; pfn < end_pfn; pfn += cur_nr_pages) {

mm/memory_hotplug.c void remove_pfn_range_from_zone(struct zone *zone, · line 601

Quoted 5 lines
mm/memory_hotplug.c +2 -1
@@ -571,12 +598,13 @@ void remove_pfn_range_from_zone(struct zone *zone,
   	if (zone_is_zone_device(zone))
   		return;
-	clear_zone_contiguous(zone);
+	contiguous_state = clear_zone_contiguous_for_shrinking(
+				zone, start_pfn, nr_pages);

Reading this again, I wonder whether it would be nicer to have something like:

Quoted 2 lines

new_contig_state = zone_contig_state_after_shrinking(); clear_zone_contiguous(zone);

+1

Quoted 41 lines

mm/memory_hotplug.c static inline void section_taint_zone_device(unsigned long pfn) · line 777

Quoted 11 lines
mm/memory_hotplug.c +11 -0
+static enum zone_contiguous_state __meminit clear_zone_contiguous_for_growing(
+		struct zone *zone, unsigned long start_pfn, unsigned long nr_pages)
+{
+	const unsigned long end_pfn = start_pfn + nr_pages;
+	enum zone_contiguous_state result = CONTIGUOUS_UNDETERMINED;
+
+	/*
+	 * Given the moved pfn range's contiguous property is always true,
+	 * under the conditional of empty zone, the contiguous property should
+	 * be true.
+	 */

I don't think that comment is required.

Quoted 16 lines
mm/memory_hotplug.c +16 -0
+	if (zone_is_empty(zone))
+		result = CONTIGUOUS_DEFINITELY;
+
+	/*
+	 * If the moved pfn range does not intersect with the original zone span,
+	 * the contiguous property is surely false.
+	 */
+	else if (end_pfn < zone->zone_start_pfn || start_pfn > zone_end_pfn(zone))
+		result = CONTIGUOUS_DEFINITELY_NOT;
+
+	/*
+	 * If the moved pfn range is adjacent to the original zone span, given
+	 * the moved pfn range's contiguous property is always true, the zone's
+	 * contiguous property inherited from the original value.
+	 */
+	else if (end_pfn == zone->zone_start_pfn || start_pfn == zone_end_pfn(zone))

mm/memory_hotplug.c static inline void section_taint_zone_device(unsigned long pfn) · line 803

Quoted 10 lines
mm/memory_hotplug.c +10 -0
+		result = zone->contiguous ?
+			CONTIGUOUS_DEFINITELY : CONTIGUOUS_DEFINITELY_NOT;
+
+	/*
+	 * If the original zone's hole larger than the moved pages in the range,
+	 * the contiguous property is surely false.
+	 */
+	else if (nr_pages < (zone->spanned_pages - zone->present_pages))
+		result = CONTIGUOUS_DEFINITELY_NOT;
+

This is a bit unreadable :)

Quoted 8 lines
if (zone_is_empty(zone)) {
	result = CONTIGUOUS_DEFINITELY;
} else if (...) {
	/* ... */
	...
} else if (...) {
	...
}

If we update zone state outside this function it can be even simpler:

Quoted 8 lines
	if (zone_is_empty(zone))
		return CONTIGUOUS_DEFINITELY;

	if (nr_pages < (zone->spanned_pages - zone->present_pages))
		return CONTIGUOUS_DEFINITELY_NOT;

etc.

Thanks Mike. It's doable, consider clear_zone_contiguous moved out.

Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
mm/memory_hotplug.c +7 -3
+	clear_zone_contiguous(zone);
+	return result;
+}
+
   /*
    * Associate the pfn range with the given zone, initializing the memmaps
    * and resizing the pgdat/zone data to span the added pages. After this
@@ -752,8 +821,8 @@ 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);
+	const enum zone_contiguous_state contiguous_state =
+		clear_zone_contiguous_for_growing(zone, start_pfn, nr_pages);
   	if (zone_is_empty(zone))
   		init_currently_empty_zone(zone, start_pfn, nr_pages);
@@ -783,7 +852,7 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
   			 MEMINIT_HOTPLUG, altmap, migratetype,
   			 isolate_pageblock);
-	set_zone_contiguous(zone);
+	set_zone_contiguous(zone, contiguous_state);
   }
   struct auto_movable_stats {
mm/mm_init.c +1 -3
index 7712d887b696..06db3fcf7f95 100644
@@ -2263,26 +2263,34 @@ void __init init_cma_pageblock(struct page *page)
   }
   #endif
-void set_zone_contiguous(struct zone *zone)
+void set_zone_contiguous(struct zone *zone, enum zone_contiguous_state state)
   {
   	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);
Quoted 8 lines
Quoted 7 lines

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

Quoted 7 lines
mm/mm_init.c +5 -2
-			block_start_pfn = block_end_pfn,
-			 block_end_pfn += pageblock_nr_pages) {
+	if (state == CONTIGUOUS_DEFINITELY) {
+		zone->contiguous = true;
+		return;
+	} else if (state == CONTIGUOUS_DEFINITELY_NOT) {
+		// zone contiguous has already cleared as false, just return.

Please no C++ style comments.

Got it. Thanks.

Quoted 47 lines
Quoted 27 lines
Quoted 14 lines
mm/mm_init.c +8 -6
+		return;
+	} else if (state == CONTIGUOUS_UNDETERMINED) {
+		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));
+			block_end_pfn = min(block_end_pfn, zone_end_pfn(zone));
-		if (!__pageblock_pfn_to_page(block_start_pfn,
-					     block_end_pfn, zone))
-			return;
-		cond_resched();
-	}
+			if (!__pageblock_pfn_to_page(block_start_pfn,

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

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

switch (state) {
case CONTIGUOUS_DEFINITELY:

Quoted 8 lines
	zone->contiguous = true;
	return;
case CONTIGUOUS_DEFINITELY_NOT:
	return;
default:
	break;
}

... unchanged logic.

I was going to suggest rather to drop 'else'

	if (state == CONTIGUOUS_DEFINITELY) {
		zone->contiguous = true;
		return;
	}

	if (state == CONTIGUOUS_DEFINITELY_NOT)
		return;

	... unchanged logic.
Quoted 2 lines

but I don't feel strongly about it.

Got it. Thanks.