Skip to contentSkip to conversation

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

PATCH Superseded Tianyou Li 4 participants 23 messages Nov 17, 2025 – Dec 1, 2025

Subsystems (4)

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Tianyou Li

author
#0 lore raw

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

When invoke move_pfn_range_to_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 |
|                +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      3s      |       70%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      33s      |      8s      |       76%      |
+----------------+------+---------------+--------------+----------------+

[1] Qemu commands to hotplug 512G memory for a VM:
object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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       |  3 +++
 mm/memory_hotplug.c | 48 ++++++++++++++++++++++++++++++++++++++++++++-
 mm/mm_init.c        | 31 ++++++++++++++++++++++-------
 3 files changed, 74 insertions(+), 8 deletions(-)
mm/internal.h +3 -0
index 1561fc2ff5b8..734caae6873c 100644
@@ -734,6 +734,9 @@ void set_zone_contiguous(struct zone *zone);
 bool pfn_range_intersects_zones(int nid, unsigned long start_pfn,
 			   unsigned long nr_pages);
 
+bool check_zone_contiguous(struct zone *zone, unsigned long start_pfn,
+			   unsigned long nr_pages);
+
 static inline void clear_zone_contiguous(struct zone *zone)
 {
 	zone->contiguous = false;
mm/memory_hotplug.c +47 -1

66 lines not on this page · raw message

mm/mm_init.c +24 -7
index 7712d887b696..04fdd949fe49 100644
@@ -2263,26 +2263,43 @@ void __init init_cma_pageblock(struct page *page)
 }
 #endif
 
-void set_zone_contiguous(struct zone *zone)
+/*
+ * Check if all pageblocks in the given PFN range belong to the given zone.
+ * The given range is expected to be within the zone's pfn range, otherwise
+ * false is returned.
+ */
+bool check_zone_contiguous(struct zone *zone, unsigned long start_pfn,
+			    unsigned long nr_pages)
 {
-	unsigned long block_start_pfn = zone->zone_start_pfn;
+	unsigned long end_pfn = start_pfn + nr_pages;
+	unsigned long block_start_pfn = start_pfn;
 	unsigned long block_end_pfn;
 
+	if (start_pfn < zone->zone_start_pfn || end_pfn > zone_end_pfn(zone))
+		return false;
+
 	block_end_pfn = pageblock_end_pfn(block_start_pfn);
-	for (; block_start_pfn < zone_end_pfn(zone);
+	for (; block_start_pfn < end_pfn;
 			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, end_pfn);
 
 		if (!__pageblock_pfn_to_page(block_start_pfn,
 					     block_end_pfn, zone))
-			return;
+			return false;
 		cond_resched();
 	}
 
-	/* We confirm that there is no hole */
-	zone->contiguous = true;
+	return true;
+}
+
+void set_zone_contiguous(struct zone *zone)
+{
+	unsigned long start_pfn = zone->zone_start_pfn;
+	unsigned long nr_pages = zone->spanned_pages;
+
+	zone->contiguous = check_zone_contiguous(zone, start_pfn, nr_pages);
 }
 
 /*
Signature · 1 line
2.47.1
#1 lore raw

Hi All,

Rebased with latest master. Add "Reported-by" from customer. Looking forward to any comments/suggestions. Thanks.

Regards,

Tianyou

Quoted 209 lines · Tianyou Li

When invoke move_pfn_range_to_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 |
|                +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      3s      |       70%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      33s      |      8s      |       76%      |
+----------------+------+---------------+--------------+----------------+

[1] Qemu commands to hotplug 512G memory for a VM:
object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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       |  3 +++
  mm/memory_hotplug.c | 48 ++++++++++++++++++++++++++++++++++++++++++++-
  mm/mm_init.c        | 31 ++++++++++++++++++++++-------
  3 files changed, 74 insertions(+), 8 deletions(-)
mm/internal.h +3 -0
index 1561fc2ff5b8..734caae6873c 100644
@@ -734,6 +734,9 @@ void set_zone_contiguous(struct zone *zone);
  bool pfn_range_intersects_zones(int nid, unsigned long start_pfn,
  			   unsigned long nr_pages);
 
+bool check_zone_contiguous(struct zone *zone, unsigned long start_pfn,
+			   unsigned long nr_pages);
+
  static inline void clear_zone_contiguous(struct zone *zone)
  {
  	zone->contiguous = false;
mm/memory_hotplug.c +47 -1
index 0be83039c3b5..96c003271b8e 100644
@@ -723,6 +723,47 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon
 
  }
 
+static void __meminit update_zone_contiguous(struct zone *zone,
+			bool old_contiguous, unsigned long old_start_pfn,
+			unsigned long old_nr_pages, unsigned long old_absent_pages,
+			unsigned long new_start_pfn, unsigned long new_nr_pages)
+{
+	unsigned long old_end_pfn = old_start_pfn + old_nr_pages;
+	unsigned long new_end_pfn = new_start_pfn + new_nr_pages;
+	unsigned long new_filled_pages = 0;
+
+	/*
+	 * If the moved pfn range does not intersect with the old zone span,
+	 * the contiguous property is surely false.
+	 */
+	if (new_end_pfn < old_start_pfn || new_start_pfn > old_end_pfn)
+		return;
+
+	/*
+	 * If the moved pfn range is adjacent to the old zone span,
+	 * check the range to the left or to the right
+	 */
+	if (new_end_pfn == old_start_pfn || new_start_pfn == old_end_pfn) {
+		zone->contiguous = old_contiguous &&
+			check_zone_contiguous(zone, new_start_pfn, new_nr_pages);
+		return;
+	}
+
+	/*
+	 * If old zone's hole larger than the new filled pages, the contiguous
+	 * property is surely false.
+	 */
+	new_filled_pages = new_end_pfn - old_start_pfn;
+	if (new_start_pfn > old_start_pfn)
+		new_filled_pages -= new_start_pfn - old_start_pfn;
+	if (new_end_pfn > old_end_pfn)
+		new_filled_pages -= new_end_pfn - old_end_pfn;
+	if (new_filled_pages < old_absent_pages)
+		return;
+
+	set_zone_contiguous(zone);
+}
+
  #ifdef CONFIG_ZONE_DEVICE
  static void section_taint_zone_device(unsigned long pfn)
  {
@@ -752,6 +793,10 @@ 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;
+	bool old_contiguous = zone->contiguous;
+	unsigned long old_start_pfn = zone->zone_start_pfn;
+	unsigned long old_nr_pages = zone->spanned_pages;
+	unsigned long old_absent_pages = zone->spanned_pages - zone->present_pages;
 
  	clear_zone_contiguous(zone);
 
@@ -783,7 +828,8 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
  			 MEMINIT_HOTPLUG, altmap, migratetype,
  			 isolate_pageblock);
 
-	set_zone_contiguous(zone);
+	update_zone_contiguous(zone, old_contiguous, old_start_pfn, old_nr_pages,
+				old_absent_pages, start_pfn, nr_pages);
  }
 
  struct auto_movable_stats {
mm/mm_init.c +24 -7
index 7712d887b696..04fdd949fe49 100644
@@ -2263,26 +2263,43 @@ void __init init_cma_pageblock(struct page *page)
  }
  #endif
 
-void set_zone_contiguous(struct zone *zone)
+/*
+ * Check if all pageblocks in the given PFN range belong to the given zone.
+ * The given range is expected to be within the zone's pfn range, otherwise
+ * false is returned.
+ */
+bool check_zone_contiguous(struct zone *zone, unsigned long start_pfn,
+			    unsigned long nr_pages)
  {
-	unsigned long block_start_pfn = zone->zone_start_pfn;
+	unsigned long end_pfn = start_pfn + nr_pages;
+	unsigned long block_start_pfn = start_pfn;
  	unsigned long block_end_pfn;
 
+	if (start_pfn < zone->zone_start_pfn || end_pfn > zone_end_pfn(zone))
+		return false;
+
  	block_end_pfn = pageblock_end_pfn(block_start_pfn);
-	for (; block_start_pfn < zone_end_pfn(zone);
+	for (; block_start_pfn < end_pfn;
  			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, end_pfn);
 
  		if (!__pageblock_pfn_to_page(block_start_pfn,
  					     block_end_pfn, zone))
-			return;
+			return false;
  		cond_resched();
  	}
 
-	/* We confirm that there is no hole */
-	zone->contiguous = true;
+	return true;
+}
+
+void set_zone_contiguous(struct zone *zone)
+{
+	unsigned long start_pfn = zone->zone_start_pfn;
+	unsigned long nr_pages = zone->spanned_pages;
+
+	zone->contiguous = check_zone_contiguous(zone, start_pfn, nr_pages);
  }
 
  /*
#2 lore raw
In reply to Tianyou Li

On 17.11.25 04:30, Tianyou Li wrote:

Sorry for the late review!

Quoted 5 lines

When invoke move_pfn_range_to_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.

Right, unfortunately we have to iterate pageblocks.

We know that hotplugged sections always belong to the same zone, so we could optimize for them as well. Only early section have to walk pageblocks.

	if (early_section(__pfn_to_section(pfn)))

We could also walk memory blocks I guess (for_each_memory_block). If mem->zone != NULL, we know the whole block spans a single zone.

Memory blocks are as small as 128 MiB on x86-64, with pageblocks being 2 MiB we would walk 64 pageblocks.

(I think we can also walk MAX_PAGE_ORDER chunks instead of pageblock chunks)

Quoted 12 lines

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

Quoted 7 lines
+----------------+------+---------------+--------------+----------------+
|                | Size | Time (before) | Time (after) | Time Reduction |
|                +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      3s      |       70%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      33s      |      8s      |       76%      |
+----------------+------+---------------+--------------+----------------+

Did not expect that to be the most expensive part, nice!

Quoted 62 lines

[1] Qemu commands to hotplug 512G memory for a VM:
object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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       |  3 +++
  mm/memory_hotplug.c | 48 ++++++++++++++++++++++++++++++++++++++++++++-
  mm/mm_init.c        | 31 ++++++++++++++++++++++-------
  3 files changed, 74 insertions(+), 8 deletions(-)
mm/internal.h +3 -0
index 1561fc2ff5b8..734caae6873c 100644
@@ -734,6 +734,9 @@ void set_zone_contiguous(struct zone *zone);
  bool pfn_range_intersects_zones(int nid, unsigned long start_pfn,
  			   unsigned long nr_pages);
 
+bool check_zone_contiguous(struct zone *zone, unsigned long start_pfn,
+			   unsigned long nr_pages);
+
  static inline void clear_zone_contiguous(struct zone *zone)
  {
  	zone->contiguous = false;
mm/memory_hotplug.c +0 -0
index 0be83039c3b5..96c003271b8e 100644

mm/memory_hotplug.c static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon · line 729

Quoted 7 lines
mm/memory_hotplug.c +4 -0
@@ -723,6 +723,47 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon
 
  }
 
+static void __meminit update_zone_contiguous(struct zone *zone,
+			bool old_contiguous, unsigned long old_start_pfn,
+			unsigned long old_nr_pages, unsigned long old_absent_pages,
+			unsigned long new_start_pfn, unsigned long new_nr_pages)

Is "old" the old zone range and "new", the new part we are adding?

In that case, old vs. new is misleading, could be interpreted as "old spanned zone range" and "new spanned zone range".

Why are we passing in old_absent_pages and not simply calculate it based on zone->present pages in here?

mm/memory_hotplug.c static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon · line 732

Quoted 3 lines
mm/memory_hotplug.c +3 -0
+{
+	unsigned long old_end_pfn = old_start_pfn + old_nr_pages;
+	unsigned long new_end_pfn = new_start_pfn + new_nr_pages;

Can both be const.

Quoted 6 lines
mm/memory_hotplug.c +6 -0
+	unsigned long new_filled_pages = 0;
+
+	/*
+	 * If the moved pfn range does not intersect with the old zone span,
+	 * the contiguous property is surely false.
+	 */

mm/memory_hotplug.c static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon · line 748

Quoted 10 lines
mm/memory_hotplug.c +10 -0
+	if (new_end_pfn < old_start_pfn || new_start_pfn > old_end_pfn)
+		return;
+
+	/*
+	 * If the moved pfn range is adjacent to the old zone span,
+	 * check the range to the left or to the right
+	 */
+	if (new_end_pfn == old_start_pfn || new_start_pfn == old_end_pfn) {
+		zone->contiguous = old_contiguous &&
+			check_zone_contiguous(zone, new_start_pfn, new_nr_pages);

It's sufficient to check that a single pageblock at the old start/end (depending where we're adding) has the same zone already.

Why are we checking the new range we are adding? That doesn't make sense unless I am missing something. We know that that one is contiguous.

Quoted 4 lines
mm/memory_hotplug.c +4 -0
+		return;
+	}
+
+	/*

mm/memory_hotplug.c static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon · line 762

Quoted 10 lines
mm/memory_hotplug.c +10 -0
+	 * If old zone's hole larger than the new filled pages, the contiguous
+	 * property is surely false.
+	 */
+	new_filled_pages = new_end_pfn - old_start_pfn;
+	if (new_start_pfn > old_start_pfn)
+		new_filled_pages -= new_start_pfn - old_start_pfn;
+	if (new_end_pfn > old_end_pfn)
+		new_filled_pages -= new_end_pfn - old_end_pfn;
+	if (new_filled_pages < old_absent_pages)
+		return;

I don't quite like the dependence on present pages here. But I guess there is no other simple way to just detect that there is a large hole in there that cannot possibly get closed.

Quoted 38 lines
mm/memory_hotplug.c +10 -1
+
+	set_zone_contiguous(zone);
+}
+
  #ifdef CONFIG_ZONE_DEVICE
  static void section_taint_zone_device(unsigned long pfn)
  {
@@ -752,6 +793,10 @@ 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;
+	bool old_contiguous = zone->contiguous;
+	unsigned long old_start_pfn = zone->zone_start_pfn;
+	unsigned long old_nr_pages = zone->spanned_pages;
+	unsigned long old_absent_pages = zone->spanned_pages - zone->present_pages;
 
  	clear_zone_contiguous(zone);
 
@@ -783,7 +828,8 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
  			 MEMINIT_HOTPLUG, altmap, migratetype,
  			 isolate_pageblock);
 
-	set_zone_contiguous(zone);
+	update_zone_contiguous(zone, old_contiguous, old_start_pfn, old_nr_pages,
+				old_absent_pages, start_pfn, nr_pages);
  }
 
  struct auto_movable_stats {
mm/mm_init.c +1 -1
index 7712d887b696..04fdd949fe49 100644
@@ -2263,26 +2263,43 @@ void __init init_cma_pageblock(struct page *page)
  }
  #endif
 
-void set_zone_contiguous(struct zone *zone)
+/*

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

Quoted 10 lines
mm/mm_init.c +8 -1
+ * Check if all pageblocks in the given PFN range belong to the given zone.
+ * The given range is expected to be within the zone's pfn range, otherwise
+ * false is returned.
+ */
+bool check_zone_contiguous(struct zone *zone, unsigned long start_pfn,
+			    unsigned long nr_pages)
  {
-	unsigned long block_start_pfn = zone->zone_start_pfn;
+	unsigned long end_pfn = start_pfn + nr_pages;
+	unsigned long block_start_pfn = start_pfn;

Can be const.

Signature · 3 lines
Cheers

David
#3 lore raw

Very appreciated for your timely review and insightful comments, David.

Quoted 14 lines · David Hildenbrand (Red Hat)

On 17.11.25 04:30, Tianyou Li wrote:

Sorry for the late review!

Quoted 5 lines

When invoke move_pfn_range_to_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.

Right, unfortunately we have to iterate pageblocks.

We know that hotplugged sections always belong to the same zone, so we could optimize for them as well. Only early section have to walk pageblocks.

Quoted 9 lines

    if (early_section(__pfn_to_section(pfn)))

We could also walk memory blocks I guess (for_each_memory_block). If mem->zone != NULL, we know the whole block spans a single zone.

Memory blocks are as small as 128 MiB on x86-64, with pageblocks being 2 MiB we would walk 64 pageblocks.

(I think we can also walk MAX_PAGE_ORDER chunks instead of pageblock chunks)

This actually point to another optimization opportunity that reduce the memory access even further to get better performance beyond this patch. This patch is to avoid the contiguous check as much as possible if we can deduce the result of contiguous with given conditions. I must confess I did not know that we can walk through memory blocks in some situations. Allow me to think through the idea and create another patch to optimize the slow path. At same time, would you mind to review the patch v2 for the fast paths situation separately?

Quoted 23 lines
Quoted 13 lines

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

Quoted 9 lines

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

|                | Size | Time (before) | Time (after) | Time Reduction |
| +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      3s      | 70%      |
| +------+---------------+--------------+----------------+
|                | 512G |      33s      |      8s      | 76%      |
+----------------+------+---------------+--------------+----------------+ 
Quoted 2 lines

Did not expect that to be the most expensive part, nice!

Thanks.

Quoted 70 lines
Quoted 61 lines

[1] Qemu commands to hotplug 512G memory for a VM:
     object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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       |  3 +++
  mm/memory_hotplug.c | 48 ++++++++++++++++++++++++++++++++++++++++++++- mm/mm_init.c        | 31 ++++++++++++++++++++++-------

  3 files changed, 74 insertions(+), 8 deletions(-)
mm/internal.h +2 -0
index 1561fc2ff5b8..734caae6873c 100644
@@ -734,6 +734,9 @@ void set_zone_contiguous(struct zone *zone);
   bool pfn_range_intersects_zones(int nid, unsigned long start_pfn,
                  unsigned long nr_pages);
   +bool check_zone_contiguous(struct zone *zone, unsigned long start_pfn,
+               unsigned long nr_pages);
+
   static inline void clear_zone_contiguous(struct zone *zone)
   {
       zone->contiguous = false;
mm/memory_hotplug.c +0 -0
index 0be83039c3b5..96c003271b8e 100644
@@ -723,6 +723,47 @@ static void __meminit resize_pgdat_range(struct 
Quoted 6 lines

pglist_data *pgdat, unsigned lon
    }
  +static void __meminit update_zone_contiguous(struct zone *zone, +            bool old_contiguous, unsigned long old_start_pfn, +            unsigned long old_nr_pages, unsigned long old_absent_pages, +            unsigned long new_start_pfn, unsigned long new_nr_pages)

Is "old" the old zone range and "new", the new part we are adding?

Quoted 3 lines

In that case, old vs. new is misleading, could be interpreted as "old spanned zone range" and "new spanned zone range".

Agreed, the naming struggles. Could I rename the 'old' to 'origin' that indicates the original spanned zone range and removes the 'new_' to indicate the added/moved pfn range? Will that be more descriptive to fit in this situation?

Quoted 1 line

Why are we passing in old_absent_pages and not simply calculate it based on zone->present pages in here?

Will do in patch v2. Previously I am a bit conservative to use zone->present_pages directly in the update_zone_contiguous function because implicitly it creates a dependency that zone->present_pages did not get updated in any of the functions before it. Allow me to send the patch v2 for your review.

Quoted 7 lines
Quoted 4 lines

+{
+    unsigned long old_end_pfn = old_start_pfn + old_nr_pages; +    unsigned long new_end_pfn = new_start_pfn + new_nr_pages;

Can both be const.

Thanks, will do in patch v2.

Quoted 20 lines
Quoted 9 lines

+    unsigned long new_filled_pages = 0;
+
+    /*
+     * If the moved pfn range does not intersect with the old zone span,
+     * the contiguous property is surely false.
+     */
+    if (new_end_pfn < old_start_pfn || new_start_pfn > old_end_pfn) +        return;

Quoted 8 lines

+
+    /*
+     * If the moved pfn range is adjacent to the old zone span, +     * check the range to the left or to the right
+     */
+    if (new_end_pfn == old_start_pfn || new_start_pfn == old_end_pfn) { +        zone->contiguous = old_contiguous &&
+            check_zone_contiguous(zone, new_start_pfn, new_nr_pages);

It's sufficient to check that a single pageblock at the old start/end (depending where we're adding) has the same zone already.

Quoted 2 lines

Why are we checking the new range we are adding? That doesn't make sense unless I am missing something. We know that that one is contiguous.

You are right. The memmap_init_range makes the new ranges contiguous with the same zone as the original zone span, because it passed with the nid and zone_idx from the original zone. In that case, should we just inherited the original contiguous property, probably even need not to check additional pageblocks?

A quick test with your idea shows significant improvements, for 256G configuration, the time reduced from 3s to 2s, and for 512G configuration, the time reduced from 8s to 6s. The new result as below:

+----------------+------+---------------+--------------+----------------+
|                | Size | Time (before) | Time (after) | Time Reduction |
| +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      2s      |  80%      |
| +------+---------------+--------------+----------------+
|                | 512G |      33s      |      6s      |  81%      |
+----------------+------+---------------+--------------+----------------+
Quoted 16 lines
Quoted 7 lines

+        return;
+    }
+
+    /*
+     * If old zone's hole larger than the new filled pages, the contiguous
+     * property is surely false.

Quoted 8 lines

+     */
+    new_filled_pages = new_end_pfn - old_start_pfn; +    if (new_start_pfn > old_start_pfn)
+        new_filled_pages -= new_start_pfn - old_start_pfn; +    if (new_end_pfn > old_end_pfn)
+        new_filled_pages -= new_end_pfn - old_end_pfn; +    if (new_filled_pages < old_absent_pages)
+        return;

Quoted 2 lines

I don't quite like the dependence on present pages here. But I guess there is no other simple way to just detect that there is a large hole in there that cannot possibly get closed.

Yes, I did not find a better solution to cover this situation simply, and I'd like to cover most of cases include inside/overlap/span of the original zone.

Quoted 43 lines
Quoted 43 lines
mm/memory_hotplug.c +4 -0
+
+    set_zone_contiguous(zone);
+}
+

  #ifdef CONFIG_ZONE_DEVICE
  static void section_taint_zone_device(unsigned long pfn) {

mm/memory_hotplug.c +4 -0
@@ -752,6 +793,10 @@ 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;
+    bool old_contiguous = zone->contiguous;
+    unsigned long old_start_pfn = zone->zone_start_pfn;
+    unsigned long old_nr_pages = zone->spanned_pages;
+    unsigned long old_absent_pages = zone->spanned_pages - 
 zone->present_pages;
         clear_zone_contiguous(zone);

  @@ -783,7 +828,8 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, MEMINIT_HOTPLUG, altmap, migratetype,
               isolate_pageblock);
  -    set_zone_contiguous(zone);
+    update_zone_contiguous(zone, old_contiguous, old_start_pfn,
old_nr_pages,
+                old_absent_pages, start_pfn, nr_pages);
  }
    struct auto_movable_stats {

mm/mm_init.c +2 -0
index 7712d887b696..04fdd949fe49 100644
@@ -2263,26 +2263,43 @@ void __init init_cma_pageblock(struct page 
 *page)
   }
   #endif
   -void set_zone_contiguous(struct zone *zone)
+/*
+ * Check if all pageblocks in the given PFN range belong to the 

given zone.
+ * The given range is expected to be within the zone's pfn range, otherwise
+ * false is returned.
+ */

Quoted 9 lines
Quoted 6 lines

+bool check_zone_contiguous(struct zone *zone, unsigned long start_pfn, +                unsigned long nr_pages)
  {
-    unsigned long block_start_pfn = zone->zone_start_pfn;
+    unsigned long end_pfn = start_pfn + nr_pages;
+    unsigned long block_start_pfn = start_pfn;

Can be const.

Yes, will do in patch v2.

Thanks & Regards,

Tianyou

maintainer
#4 lore raw
In reply to Tianyou Li
Quoted 103 lines · Tianyou Li

When invoke move_pfn_range_to_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 |
|                +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      3s      |       70%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      33s      |      8s      |       76%      |
+----------------+------+---------------+--------------+----------------+

[1] Qemu commands to hotplug 512G memory for a VM:
object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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       |  3 +++
 mm/memory_hotplug.c | 48 ++++++++++++++++++++++++++++++++++++++++++++-
 mm/mm_init.c        | 31 ++++++++++++++++++++++-------
 3 files changed, 74 insertions(+), 8 deletions(-)
mm/internal.h +3 -0
index 1561fc2ff5b8..734caae6873c 100644
@@ -734,6 +734,9 @@ void set_zone_contiguous(struct zone *zone);
 bool pfn_range_intersects_zones(int nid, unsigned long start_pfn,
 			   unsigned long nr_pages);
 
+bool check_zone_contiguous(struct zone *zone, unsigned long start_pfn,
+			   unsigned long nr_pages);
+
 static inline void clear_zone_contiguous(struct zone *zone)
 {
 	zone->contiguous = false;
mm/memory_hotplug.c +14 -0
index 0be83039c3b5..96c003271b8e 100644
@@ -723,6 +723,47 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon
 
 }
 
+static void __meminit update_zone_contiguous(struct zone *zone,
+			bool old_contiguous, unsigned long old_start_pfn,
+			unsigned long old_nr_pages, unsigned long old_absent_pages,
+			unsigned long new_start_pfn, unsigned long new_nr_pages)
+{
+	unsigned long old_end_pfn = old_start_pfn + old_nr_pages;
+	unsigned long new_end_pfn = new_start_pfn + new_nr_pages;
+	unsigned long new_filled_pages = 0;
+
+	/*
+	 * If the moved pfn range does not intersect with the old zone span,
+	 * the contiguous property is surely false.
+	 */
+	if (new_end_pfn < old_start_pfn || new_start_pfn > old_end_pfn)

mm/memory_hotplug.c static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon · line 749

Quoted 10 lines
mm/memory_hotplug.c +10 -0
+		return;
+
+	/*
+	 * If the moved pfn range is adjacent to the old zone span,
+	 * check the range to the left or to the right
+	 */
+	if (new_end_pfn == old_start_pfn || new_start_pfn == old_end_pfn) {
+		zone->contiguous = old_contiguous &&
+			check_zone_contiguous(zone, new_start_pfn, new_nr_pages);
+		return;

The check for adjacency of the new range to the zone can be moved to the beginning of move_pfn_range_to_zone() and it will already optimize the common case when we hotplug memory to a contiguous zone.

mm/memory_hotplug.c static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon · line 762

Quoted 13 lines
mm/memory_hotplug.c +13 -0
+	}
+
+	/*
+	 * If old zone's hole larger than the new filled pages, the contiguous
+	 * property is surely false.
+	 */
+	new_filled_pages = new_end_pfn - old_start_pfn;
+	if (new_start_pfn > old_start_pfn)
+		new_filled_pages -= new_start_pfn - old_start_pfn;
+	if (new_end_pfn > old_end_pfn)
+		new_filled_pages -= new_end_pfn - old_end_pfn;
+	if (new_filled_pages < old_absent_pages)
+		return;

Let's just check that we don't add enough pages to cover the hole

	if (nr_new_pages < old_absent_pages)
		return;

and if we do go to the slow path and walk the pageblocks.

Quoted 4 lines
mm/memory_hotplug.c +4 -0
+
+	set_zone_contiguous(zone);
+}
+
Signature · 2 lines
Sincerely yours,
Mike.
#5 lore raw

Thanks for your comments Mike. Appreciated.

Quoted 109 lines · Mike Rapoport
Quoted 108 lines · Tianyou Li

When invoke move_pfn_range_to_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 |
|                +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      3s      |       70%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      33s      |      8s      |       76%      |
+----------------+------+---------------+--------------+----------------+

[1] Qemu commands to hotplug 512G memory for a VM:
object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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       |  3 +++
  mm/memory_hotplug.c | 48 ++++++++++++++++++++++++++++++++++++++++++++-
  mm/mm_init.c        | 31 ++++++++++++++++++++++-------
  3 files changed, 74 insertions(+), 8 deletions(-)
mm/internal.h +3 -0
index 1561fc2ff5b8..734caae6873c 100644
@@ -734,6 +734,9 @@ void set_zone_contiguous(struct zone *zone);
  bool pfn_range_intersects_zones(int nid, unsigned long start_pfn,
  			   unsigned long nr_pages);
 
+bool check_zone_contiguous(struct zone *zone, unsigned long start_pfn,
+			   unsigned long nr_pages);
+
  static inline void clear_zone_contiguous(struct zone *zone)
  {
  	zone->contiguous = false;
mm/memory_hotplug.c +19 -0
index 0be83039c3b5..96c003271b8e 100644
@@ -723,6 +723,47 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon
 
  }
 
+static void __meminit update_zone_contiguous(struct zone *zone,
+			bool old_contiguous, unsigned long old_start_pfn,
+			unsigned long old_nr_pages, unsigned long old_absent_pages,
+			unsigned long new_start_pfn, unsigned long new_nr_pages)
+{
+	unsigned long old_end_pfn = old_start_pfn + old_nr_pages;
+	unsigned long new_end_pfn = new_start_pfn + new_nr_pages;
+	unsigned long new_filled_pages = 0;
+
+	/*
+	 * If the moved pfn range does not intersect with the old zone span,
+	 * the contiguous property is surely false.
+	 */
+	if (new_end_pfn < old_start_pfn || new_start_pfn > old_end_pfn)
+		return;
+
+	/*
+	 * If the moved pfn range is adjacent to the old zone span,
+	 * check the range to the left or to the right
Quoted 8 lines

mm/memory_hotplug.c static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon · line 749

Quoted 5 lines
mm/memory_hotplug.c +5 -0
+	 */
+	if (new_end_pfn == old_start_pfn || new_start_pfn == old_end_pfn) {
+		zone->contiguous = old_contiguous &&
+			check_zone_contiguous(zone, new_start_pfn, new_nr_pages);
+		return;

The check for adjacency of the new range to the zone can be moved to the beginning of move_pfn_range_to_zone() and it will already optimize the common case when we hotplug memory to a contiguous zone.

Do you mean we can separate the update_zone_contiguous logic into two parts, one for fast path at the beginning of the move_pfn_range_to_zone, and the other for slow path after the memmep_init_range?

Quoted 15 lines

mm/memory_hotplug.c static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon · line 762

Quoted 13 lines
mm/memory_hotplug.c +13 -0
+	}
+
+	/*
+	 * If old zone's hole larger than the new filled pages, the contiguous
+	 * property is surely false.
+	 */
+	new_filled_pages = new_end_pfn - old_start_pfn;
+	if (new_start_pfn > old_start_pfn)
+		new_filled_pages -= new_start_pfn - old_start_pfn;
+	if (new_end_pfn > old_end_pfn)
+		new_filled_pages -= new_end_pfn - old_end_pfn;
+	if (new_filled_pages < old_absent_pages)
+		return;

Let's just check that we don't add enough pages to cover the hole

Quoted 4 lines
	if (nr_new_pages < old_absent_pages)
		return;

and if we do go to the slow path and walk the pageblocks.

I'd like to avoid of the slow path as much as possible. The check 'if (nr_new_pages < old_absent_pages)' is more strict if overlap happens. I am OK to simplify it if there is no overlap cases or to reduce the maintaining efforts.

Thanks & Regards,

Tianyou

Quoted 4 lines
Quoted 4 lines
mm/memory_hotplug.c +4 -0
+
+	set_zone_contiguous(zone);
+}
+
#6 lore raw
In reply to Mike Rapoport

Thanks for your comments Mike. Appreciated.

Quoted 109 lines · Mike Rapoport
Quoted 108 lines · Tianyou Li

When invoke move_pfn_range_to_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 |
|                +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      3s      |       70%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      33s      |      8s      |       76%      |
+----------------+------+---------------+--------------+----------------+

[1] Qemu commands to hotplug 512G memory for a VM:
object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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       |  3 +++
  mm/memory_hotplug.c | 48 ++++++++++++++++++++++++++++++++++++++++++++-
  mm/mm_init.c        | 31 ++++++++++++++++++++++-------
  3 files changed, 74 insertions(+), 8 deletions(-)
mm/internal.h +3 -0
index 1561fc2ff5b8..734caae6873c 100644
@@ -734,6 +734,9 @@ void set_zone_contiguous(struct zone *zone);
  bool pfn_range_intersects_zones(int nid, unsigned long start_pfn,
  			   unsigned long nr_pages);
 
+bool check_zone_contiguous(struct zone *zone, unsigned long start_pfn,
+			   unsigned long nr_pages);
+
  static inline void clear_zone_contiguous(struct zone *zone)
  {
  	zone->contiguous = false;
mm/memory_hotplug.c +19 -0
index 0be83039c3b5..96c003271b8e 100644
@@ -723,6 +723,47 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon
 
  }
 
+static void __meminit update_zone_contiguous(struct zone *zone,
+			bool old_contiguous, unsigned long old_start_pfn,
+			unsigned long old_nr_pages, unsigned long old_absent_pages,
+			unsigned long new_start_pfn, unsigned long new_nr_pages)
+{
+	unsigned long old_end_pfn = old_start_pfn + old_nr_pages;
+	unsigned long new_end_pfn = new_start_pfn + new_nr_pages;
+	unsigned long new_filled_pages = 0;
+
+	/*
+	 * If the moved pfn range does not intersect with the old zone span,
+	 * the contiguous property is surely false.
+	 */
+	if (new_end_pfn < old_start_pfn || new_start_pfn > old_end_pfn)
+		return;
+
+	/*
+	 * If the moved pfn range is adjacent to the old zone span,
+	 * check the range to the left or to the right
Quoted 8 lines

mm/memory_hotplug.c static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon · line 749

Quoted 5 lines
mm/memory_hotplug.c +5 -0
+	 */
+	if (new_end_pfn == old_start_pfn || new_start_pfn == old_end_pfn) {
+		zone->contiguous = old_contiguous &&
+			check_zone_contiguous(zone, new_start_pfn, new_nr_pages);
+		return;

The check for adjacency of the new range to the zone can be moved to the beginning of move_pfn_range_to_zone() and it will already optimize the common case when we hotplug memory to a contiguous zone.

Do you mean we can separate the update_zone_contiguous logic into two parts, one for fast path at the beginning of the move_pfn_range_to_zone, and the other for slow path after the memmep_init_range?

Quoted 15 lines

mm/memory_hotplug.c static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon · line 762

Quoted 13 lines
mm/memory_hotplug.c +13 -0
+	}
+
+	/*
+	 * If old zone's hole larger than the new filled pages, the contiguous
+	 * property is surely false.
+	 */
+	new_filled_pages = new_end_pfn - old_start_pfn;
+	if (new_start_pfn > old_start_pfn)
+		new_filled_pages -= new_start_pfn - old_start_pfn;
+	if (new_end_pfn > old_end_pfn)
+		new_filled_pages -= new_end_pfn - old_end_pfn;
+	if (new_filled_pages < old_absent_pages)
+		return;

Let's just check that we don't add enough pages to cover the hole

Quoted 4 lines
	if (nr_new_pages < old_absent_pages)
		return;

and if we do go to the slow path and walk the pageblocks.

I'd like to avoid of the slow path as much as possible. The check 'if (nr_new_pages < old_absent_pages)' is more strict if overlap happens. I am OK to simplify it if there is no overlap cases or to reduce the maintaining efforts.

Thanks & Regards,

Tianyou

Quoted 4 lines
Quoted 4 lines
mm/memory_hotplug.c +4 -0
+
+	set_zone_contiguous(zone);
+}
+
#7 lore raw
Quoted 127 lines · Tianyou

Thanks for your comments Mike. Appreciated.

Quoted 113 lines · Mike Rapoport
Quoted 112 lines · Tianyou Li

When invoke move_pfn_range_to_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 |
| +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      3s      | 70%      |
| +------+---------------+--------------+----------------+
|                | 512G |      33s      |      8s      | 76%      |
+----------------+------+---------------+--------------+----------------+ 

[1] Qemu commands to hotplug 512G memory for a VM:
     object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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       |  3 +++ mm/memory_hotplug.c | 48
++++++++++++++++++++++++++++++++++++++++++++-
  mm/mm_init.c        | 31 ++++++++++++++++++++++-------

  3 files changed, 74 insertions(+), 8 deletions(-)
mm/internal.h +2 -0
index 1561fc2ff5b8..734caae6873c 100644
@@ -734,6 +734,9 @@ void set_zone_contiguous(struct zone *zone);
   bool pfn_range_intersects_zones(int nid, unsigned long start_pfn,
                  unsigned long nr_pages);
   +bool check_zone_contiguous(struct zone *zone, unsigned long start_pfn,
+               unsigned long nr_pages);
+
   static inline void clear_zone_contiguous(struct zone *zone)
   {
       zone->contiguous = false;
mm/memory_hotplug.c +0 -0
index 0be83039c3b5..96c003271b8e 100644
@@ -723,6 +723,47 @@ static void __meminit resize_pgdat_range(struct 

pglist_data *pgdat, unsigned lon
    }
  +static void __meminit update_zone_contiguous(struct zone *zone, +            bool old_contiguous, unsigned long old_start_pfn, +            unsigned long old_nr_pages, unsigned long old_absent_pages,
+            unsigned long new_start_pfn, unsigned long new_nr_pages)

mm/memory_hotplug.c +7 -0
+{
+    unsigned long old_end_pfn = old_start_pfn + old_nr_pages;
+    unsigned long new_end_pfn = new_start_pfn + new_nr_pages;
+    unsigned long new_filled_pages = 0;
+
+    /*
+     * If the moved pfn range does not intersect with the old zone 

span,
+     * the contiguous property is surely false.
+     */
+    if (new_end_pfn < old_start_pfn || new_start_pfn > old_end_pfn) +        return;
+
+    /*
+     * If the moved pfn range is adjacent to the old zone span, +     * check the range to the left or to the right
+     */

Quoted 8 lines
Quoted 5 lines

+    if (new_end_pfn == old_start_pfn || new_start_pfn == old_end_pfn) {
+        zone->contiguous = old_contiguous &&
+            check_zone_contiguous(zone, new_start_pfn, new_nr_pages); +        return;

The check for adjacency of the new range to the zone can be moved to the beginning of move_pfn_range_to_zone() and it will already optimize the common case when we hotplug memory to a contiguous zone.

Quoted 2 lines

Do you mean we can separate the update_zone_contiguous logic into two parts, one for fast path at the beginning of the move_pfn_range_to_zone, and the other for slow path after the memmep_init_range?

Re-think your idea, it's doable consider the check_zone_contiguous is not necessary. We can have a function check_zone_contiguous_fast, which need to pass the zone, start_pfn and nr_pages, return a boolean value to indicate the fast path or not. The code changes minimized. Will send the patch v2 soon.

Quoted 35 lines
Quoted 15 lines
Quoted 15 lines

+    }
+
+    /*
+     * If old zone's hole larger than the new filled pages, the contiguous
+     * property is surely false.
+     */
+    new_filled_pages = new_end_pfn - old_start_pfn;
+    if (new_start_pfn > old_start_pfn)
+        new_filled_pages -= new_start_pfn - old_start_pfn; +    if (new_end_pfn > old_end_pfn)
+        new_filled_pages -= new_end_pfn - old_end_pfn; +    if (new_filled_pages < old_absent_pages)
+        return;

Quoted 6 lines

Let's just check that we don't add enough pages to cover the hole

    if (nr_new_pages < old_absent_pages)
        return;

and if we do go to the slow path and walk the pageblocks.

I'd like to avoid of the slow path as much as possible. The check 'if (nr_new_pages < old_absent_pages)' is more strict if overlap happens. I am OK to simplify it if there is no overlap cases or to reduce the maintaining efforts.

Thanks & Regards,

Tianyou

Quoted 4 lines
Quoted 4 lines
mm/memory_hotplug.c +4 -0
+
+    set_zone_contiguous(zone);
+}
+
author
#8 lore raw

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

In reply to Mike Rapoport

When invoke move_pfn_range_to_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 |
|                +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      2s      |       80%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      33s      |      6s      |       81%      |
+----------------+------+---------------+--------------+----------------+

[1] Qemu commands to hotplug 512G memory for a VM:
object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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/memory_hotplug.c | 57 ++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 54 insertions(+), 3 deletions(-)
mm/memory_hotplug.c +54 -3
index 0be83039c3b5..8f126f20ca47 100644
@@ -723,6 +723,57 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon
 
 }
 
+static bool __meminit check_zone_contiguous_fast(struct zone *zone,
+			unsigned long start_pfn, unsigned long nr_pages)
+{
+	const unsigned long end_pfn = start_pfn + nr_pages;
+	unsigned long nr_filled_pages;
+
+	/*
+	 * Given the moved pfn range's contiguous property is always true,
+	 * under the conditional of empty zone, the contiguous property should
+	 * be true.
+	 */
+	if (zone_is_empty(zone)) {
+		zone->contiguous = true;
+		return true;
+	}
+
+	/*
+	 * If the moved pfn range does not intersect with the original zone span,
+	 * the contiguous property is surely false.
+	 */
+	if (end_pfn < zone->zone_start_pfn || start_pfn > zone_end_pfn(zone)) {
+		zone->contiguous = false;
+		return true;
+	}
+
+	/*
+	 * 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.
+	 */
+	if (end_pfn == zone->zone_start_pfn || start_pfn == zone_end_pfn(zone))
+		return true;
+
+	/*
+	 * If the original zone's hole larger than the new filled pages, the
+	 * contiguous property is surely false.
+	 */
+	nr_filled_pages = end_pfn - zone->zone_start_pfn;
+	if (start_pfn > zone->zone_start_pfn)
+		nr_filled_pages -= start_pfn - zone->zone_start_pfn;
+	if (end_pfn > zone_end_pfn(zone))
+		nr_filled_pages -= end_pfn - zone_end_pfn(zone);
+	if (nr_filled_pages < (zone->spanned_pages - zone->present_pages)) {
+		zone->contiguous = false;
+		return true;
+	}
+
+	clear_zone_contiguous(zone);
+	return false;
+}
+
 #ifdef CONFIG_ZONE_DEVICE
 static void section_taint_zone_device(unsigned long pfn)
 {
@@ -752,8 +803,7 @@ 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 bool fast_path = check_zone_contiguous_fast(zone, start_pfn, nr_pages);
 
 	if (zone_is_empty(zone))
 		init_currently_empty_zone(zone, start_pfn, nr_pages);
@@ -783,7 +833,8 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
 			 MEMINIT_HOTPLUG, altmap, migratetype,
 			 isolate_pageblock);
 
-	set_zone_contiguous(zone);
+	if (!fast_path)
+		set_zone_contiguous(zone);
 }
 
 struct auto_movable_stats {
Signature · 1 line
2.47.1
#9 lore raw

Hi All,

Patch v2 with the changes suggested from David and Mike, which simplified the code and also performance get further improved on Icelake. Appreciated for your review. Thanks.

Regards,

Tianyou

Quoted 148 lines · Tianyou Li

When invoke move_pfn_range_to_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 |
|                +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      2s      |       80%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      33s      |      6s      |       81%      |
+----------------+------+---------------+--------------+----------------+

[1] Qemu commands to hotplug 512G memory for a VM:
object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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/memory_hotplug.c | 57 ++++++++++++++++++++++++++++++++++++++++++---
  1 file changed, 54 insertions(+), 3 deletions(-)
mm/memory_hotplug.c +54 -3
index 0be83039c3b5..8f126f20ca47 100644
@@ -723,6 +723,57 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon
 
  }
 
+static bool __meminit check_zone_contiguous_fast(struct zone *zone,
+			unsigned long start_pfn, unsigned long nr_pages)
+{
+	const unsigned long end_pfn = start_pfn + nr_pages;
+	unsigned long nr_filled_pages;
+
+	/*
+	 * Given the moved pfn range's contiguous property is always true,
+	 * under the conditional of empty zone, the contiguous property should
+	 * be true.
+	 */
+	if (zone_is_empty(zone)) {
+		zone->contiguous = true;
+		return true;
+	}
+
+	/*
+	 * If the moved pfn range does not intersect with the original zone span,
+	 * the contiguous property is surely false.
+	 */
+	if (end_pfn < zone->zone_start_pfn || start_pfn > zone_end_pfn(zone)) {
+		zone->contiguous = false;
+		return true;
+	}
+
+	/*
+	 * 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.
+	 */
+	if (end_pfn == zone->zone_start_pfn || start_pfn == zone_end_pfn(zone))
+		return true;
+
+	/*
+	 * If the original zone's hole larger than the new filled pages, the
+	 * contiguous property is surely false.
+	 */
+	nr_filled_pages = end_pfn - zone->zone_start_pfn;
+	if (start_pfn > zone->zone_start_pfn)
+		nr_filled_pages -= start_pfn - zone->zone_start_pfn;
+	if (end_pfn > zone_end_pfn(zone))
+		nr_filled_pages -= end_pfn - zone_end_pfn(zone);
+	if (nr_filled_pages < (zone->spanned_pages - zone->present_pages)) {
+		zone->contiguous = false;
+		return true;
+	}
+
+	clear_zone_contiguous(zone);
+	return false;
+}
+
  #ifdef CONFIG_ZONE_DEVICE
  static void section_taint_zone_device(unsigned long pfn)
  {
@@ -752,8 +803,7 @@ 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 bool fast_path = check_zone_contiguous_fast(zone, start_pfn, nr_pages);
 
  	if (zone_is_empty(zone))
  		init_currently_empty_zone(zone, start_pfn, nr_pages);
@@ -783,7 +833,8 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
  			 MEMINIT_HOTPLUG, altmap, migratetype,
  			 isolate_pageblock);
 
-	set_zone_contiguous(zone);
+	if (!fast_path)
+		set_zone_contiguous(zone);
  }
 
  struct auto_movable_stats {
#10 lore raw
Quoted 5 lines · Tianyou

Hi All,

Patch v2 with the changes suggested from David and Mike, which simplified the code and also performance get further improved on Icelake. Appreciated for your review. Thanks.

Now about to review v3 :)

Signature · 3 lines
Cheers

David
#11 lore raw

Thanks David. Appreciated.

Regards,

Tianyou

Quoted 9 lines · David Hildenbrand (Red Hat)
Quoted 5 lines · Tianyou

Hi All,

Patch v2 with the changes suggested from David and Mike, which simplified the code and also performance get further improved on Icelake. Appreciated for your review. Thanks.

Now about to review v3 :)

#12 lore raw
In reply to Tianyou Li
Quoted 17 lines · Tianyou Li

When invoke move_pfn_range_to_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].

Quoted 8 lines
+----------------+------+---------------+--------------+----------------+
|                | Size | Time (before) | Time (after) | Time Reduction |
|                +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      2s      |       80%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      33s      |      6s      |       81%      |
+----------------+------+---------------+--------------+----------------+

Nice

Quoted 86 lines

[1] Qemu commands to hotplug 512G memory for a VM:
object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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/memory_hotplug.c | 57 ++++++++++++++++++++++++++++++++++++++++++---

1 file changed, 54 insertions(+), 3 deletions(-)
mm/memory_hotplug.c +37 -0
index 0be83039c3b5..8f126f20ca47 100644
@@ -723,6 +723,57 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon
 
 }
 
+static bool __meminit check_zone_contiguous_fast(struct zone *zone,
+			unsigned long start_pfn, unsigned long nr_pages)
+{
+	const unsigned long end_pfn = start_pfn + nr_pages;
+	unsigned long nr_filled_pages;
+
+	/*
+	 * Given the moved pfn range's contiguous property is always true,
+	 * under the conditional of empty zone, the contiguous property should
+	 * be true.
+	 */
+	if (zone_is_empty(zone)) {
+		zone->contiguous = true;
+		return true;
+	}
+
+	/*
+	 * If the moved pfn range does not intersect with the original zone span,
+	 * the contiguous property is surely false.
+	 */
+	if (end_pfn < zone->zone_start_pfn || start_pfn > zone_end_pfn(zone)) {
+		zone->contiguous = false;
+		return true;
+	}
+
+	/*
+	 * 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.
+	 */
+	if (end_pfn == zone->zone_start_pfn || start_pfn == zone_end_pfn(zone))
+		return true;
+
+	/*
+	 * If the original zone's hole larger than the new filled pages, the
+	 * contiguous property is surely false.
+	 */

mm/memory_hotplug.c static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon · line 772

Quoted 10 lines
mm/memory_hotplug.c +10 -0
+	nr_filled_pages = end_pfn - zone->zone_start_pfn;
+	if (start_pfn > zone->zone_start_pfn)
+		nr_filled_pages -= start_pfn - zone->zone_start_pfn;
+	if (end_pfn > zone_end_pfn(zone))
+		nr_filled_pages -= end_pfn - zone_end_pfn(zone);
+	if (nr_filled_pages < (zone->spanned_pages - zone->present_pages)) {
+		zone->contiguous = false;
+		return true;
+	}
+

Mike's suggestion is easier for me to understand :-)

Quoted 30 lines
mm/memory_hotplug.c +7 -3
+	clear_zone_contiguous(zone);
+	return false;
+}
+
 #ifdef CONFIG_ZONE_DEVICE
 static void section_taint_zone_device(unsigned long pfn)
 {
@@ -752,8 +803,7 @@ 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 bool fast_path = check_zone_contiguous_fast(zone, start_pfn, nr_pages);
 
 	if (zone_is_empty(zone))
 		init_currently_empty_zone(zone, start_pfn, nr_pages);
@@ -783,7 +833,8 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
 			 MEMINIT_HOTPLUG, altmap, migratetype,
 			 isolate_pageblock);
 
-	set_zone_contiguous(zone);
+	if (!fast_path)
+		set_zone_contiguous(zone);
 }
 
 struct auto_movable_stats {
Signature · 1 line
2.47.1
Signature · 2 lines
Wei Yang
Help you, Help me
#13 lore raw
Quoted 26 lines · Wei Yang
Quoted 17 lines · Tianyou Li

When invoke move_pfn_range_to_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].

Quoted 8 lines
+----------------+------+---------------+--------------+----------------+
|                | Size | Time (before) | Time (after) | Time Reduction |
|                +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      2s      |       80%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      33s      |      6s      |       81%      |
+----------------+------+---------------+--------------+----------------+
Quoted 1 line

Nice

Thanks for your time to review.

Quoted 89 lines
Quoted 89 lines

[1] Qemu commands to hotplug 512G memory for a VM:
object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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/memory_hotplug.c | 57 ++++++++++++++++++++++++++++++++++++++++++---

1 file changed, 54 insertions(+), 3 deletions(-)
mm/memory_hotplug.c +40 -0
index 0be83039c3b5..8f126f20ca47 100644
@@ -723,6 +723,57 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon
 
 }
 
+static bool __meminit check_zone_contiguous_fast(struct zone *zone,
+			unsigned long start_pfn, unsigned long nr_pages)
+{
+	const unsigned long end_pfn = start_pfn + nr_pages;
+	unsigned long nr_filled_pages;
+
+	/*
+	 * Given the moved pfn range's contiguous property is always true,
+	 * under the conditional of empty zone, the contiguous property should
+	 * be true.
+	 */
+	if (zone_is_empty(zone)) {
+		zone->contiguous = true;
+		return true;
+	}
+
+	/*
+	 * If the moved pfn range does not intersect with the original zone span,
+	 * the contiguous property is surely false.
+	 */
+	if (end_pfn < zone->zone_start_pfn || start_pfn > zone_end_pfn(zone)) {
+		zone->contiguous = false;
+		return true;
+	}
+
+	/*
+	 * 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.
+	 */
+	if (end_pfn == zone->zone_start_pfn || start_pfn == zone_end_pfn(zone))
+		return true;
+
+	/*
+	 * If the original zone's hole larger than the new filled pages, the
+	 * contiguous property is surely false.
+	 */
+	nr_filled_pages = end_pfn - zone->zone_start_pfn;
+	if (start_pfn > zone->zone_start_pfn)
+		nr_filled_pages -= start_pfn - zone->zone_start_pfn;
Quoted 8 lines

mm/memory_hotplug.c static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon · line 772

Quoted 7 lines
mm/memory_hotplug.c +7 -0
+	if (end_pfn > zone_end_pfn(zone))
+		nr_filled_pages -= end_pfn - zone_end_pfn(zone);
+	if (nr_filled_pages < (zone->spanned_pages - zone->present_pages)) {
+		zone->contiguous = false;
+		return true;
+	}
+

Mike's suggestion is easier for me to understand :-)

OK :-), with the clear votes now, I will change it in patch v3 real quick. Thanks.

Quoted 30 lines
Quoted 30 lines
mm/memory_hotplug.c +7 -3
+	clear_zone_contiguous(zone);
+	return false;
+}
+
 #ifdef CONFIG_ZONE_DEVICE
 static void section_taint_zone_device(unsigned long pfn)
 {
@@ -752,8 +803,7 @@ 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 bool fast_path = check_zone_contiguous_fast(zone, start_pfn, nr_pages);
 
 	if (zone_is_empty(zone))
 		init_currently_empty_zone(zone, start_pfn, nr_pages);
@@ -783,7 +833,8 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
 			 MEMINIT_HOTPLUG, altmap, migratetype,
 			 isolate_pageblock);
 
-	set_zone_contiguous(zone);
+	if (!fast_path)
+		set_zone_contiguous(zone);
 }
 
 struct auto_movable_stats {
Signature · 1 line
2.47.1
#14 lore raw
Quoted 133 lines · Tianyou
Quoted 26 lines · Wei Yang
Quoted 17 lines · Tianyou Li

When invoke move_pfn_range_to_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].

Quoted 8 lines
+----------------+------+---------------+--------------+----------------+
|                | Size | Time (before) | Time (after) | Time Reduction |
|                +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      2s      |       80%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      33s      |      6s      |       81%      |
+----------------+------+---------------+--------------+----------------+
Quoted 1 line

Nice

Thanks for your time to review.

Quoted 89 lines
Quoted 89 lines

[1] Qemu commands to hotplug 512G memory for a VM:
object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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/memory_hotplug.c | 57 ++++++++++++++++++++++++++++++++++++++++++---

1 file changed, 54 insertions(+), 3 deletions(-)
mm/memory_hotplug.c +40 -0
index 0be83039c3b5..8f126f20ca47 100644
@@ -723,6 +723,57 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon
 
 }
 
+static bool __meminit check_zone_contiguous_fast(struct zone *zone,
+			unsigned long start_pfn, unsigned long nr_pages)
+{
+	const unsigned long end_pfn = start_pfn + nr_pages;
+	unsigned long nr_filled_pages;
+
+	/*
+	 * Given the moved pfn range's contiguous property is always true,
+	 * under the conditional of empty zone, the contiguous property should
+	 * be true.
+	 */
+	if (zone_is_empty(zone)) {
+		zone->contiguous = true;
+		return true;
+	}
+
+	/*
+	 * If the moved pfn range does not intersect with the original zone span,
+	 * the contiguous property is surely false.
+	 */
+	if (end_pfn < zone->zone_start_pfn || start_pfn > zone_end_pfn(zone)) {
+		zone->contiguous = false;
+		return true;
+	}
+
+	/*
+	 * 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.
+	 */
+	if (end_pfn == zone->zone_start_pfn || start_pfn == zone_end_pfn(zone))
+		return true;
+
+	/*
+	 * If the original zone's hole larger than the new filled pages, the
+	 * contiguous property is surely false.
+	 */
+	nr_filled_pages = end_pfn - zone->zone_start_pfn;
+	if (start_pfn > zone->zone_start_pfn)
+		nr_filled_pages -= start_pfn - zone->zone_start_pfn;
Quoted 8 lines

mm/memory_hotplug.c static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon · line 772

Quoted 7 lines
mm/memory_hotplug.c +7 -0
+	if (end_pfn > zone_end_pfn(zone))
+		nr_filled_pages -= end_pfn - zone_end_pfn(zone);
+	if (nr_filled_pages < (zone->spanned_pages - zone->present_pages)) {
+		zone->contiguous = false;
+		return true;
+	}
+

Mike's suggestion is easier for me to understand :-)

Quoted 3 lines

OK :-), with the clear votes now, I will change it in patch v3 real quick. Thanks.

Thanks for your effort.

While maybe wait a little for v3, let's see other's comment on v2 :-)

Signature · 2 lines
Wei Yang
Help you, Help me
#15 lore raw
Quoted 131 lines · Wei Yang
Quoted 130 lines · Tianyou
Quoted 26 lines · Wei Yang
Quoted 17 lines · Tianyou Li

When invoke move_pfn_range_to_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].

Quoted 8 lines
+----------------+------+---------------+--------------+----------------+
|                | Size | Time (before) | Time (after) | Time Reduction |
|                +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      2s      |       80%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      33s      |      6s      |       81%      |
+----------------+------+---------------+--------------+----------------+
Quoted 1 line

Nice

Thanks for your time to review.

Quoted 97 lines
Quoted 86 lines

[1] Qemu commands to hotplug 512G memory for a VM:
object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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/memory_hotplug.c | 57 ++++++++++++++++++++++++++++++++++++++++++---

1 file changed, 54 insertions(+), 3 deletions(-)
mm/memory_hotplug.c +37 -0
index 0be83039c3b5..8f126f20ca47 100644
@@ -723,6 +723,57 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon
 
 }
 
+static bool __meminit check_zone_contiguous_fast(struct zone *zone,
+			unsigned long start_pfn, unsigned long nr_pages)
+{
+	const unsigned long end_pfn = start_pfn + nr_pages;
+	unsigned long nr_filled_pages;
+
+	/*
+	 * Given the moved pfn range's contiguous property is always true,
+	 * under the conditional of empty zone, the contiguous property should
+	 * be true.
+	 */
+	if (zone_is_empty(zone)) {
+		zone->contiguous = true;
+		return true;
+	}
+
+	/*
+	 * If the moved pfn range does not intersect with the original zone span,
+	 * the contiguous property is surely false.
+	 */
+	if (end_pfn < zone->zone_start_pfn || start_pfn > zone_end_pfn(zone)) {
+		zone->contiguous = false;
+		return true;
+	}
+
+	/*
+	 * 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.
+	 */
+	if (end_pfn == zone->zone_start_pfn || start_pfn == zone_end_pfn(zone))
+		return true;
+
+	/*
+	 * If the original zone's hole larger than the new filled pages, the
+	 * contiguous property is surely false.
+	 */

mm/memory_hotplug.c static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon · line 772

Quoted 10 lines
mm/memory_hotplug.c +10 -0
+	nr_filled_pages = end_pfn - zone->zone_start_pfn;
+	if (start_pfn > zone->zone_start_pfn)
+		nr_filled_pages -= start_pfn - zone->zone_start_pfn;
+	if (end_pfn > zone_end_pfn(zone))
+		nr_filled_pages -= end_pfn - zone_end_pfn(zone);
+	if (nr_filled_pages < (zone->spanned_pages - zone->present_pages)) {
+		zone->contiguous = false;
+		return true;
+	}
+

Mike's suggestion is easier for me to understand :-)

Quoted 6 lines
Quoted 3 lines

OK :-), with the clear votes now, I will change it in patch v3 real quick. Thanks.

Thanks for your effort.

While maybe wait a little for v3, let's see other's comment on v2 :-)

Thanks. While I am less worried if it might change back -:) In the scenarios we tested, the overlap case did not happen. I agree Mike's suggestion do simplify the code and reduce the maintaining efforts.

Regards,

Tianyou

Quoted 1 line
author
#16 lore raw

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

In reply to Wei Yang

When invoke move_pfn_range_to_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 |
|                +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      2s      |       80%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      33s      |      6s      |       81%      |
+----------------+------+---------------+--------------+----------------+

[1] Qemu commands to hotplug 512G memory for a VM:
object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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/memory_hotplug.c | 51 ++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 48 insertions(+), 3 deletions(-)
mm/memory_hotplug.c +48 -3
index 0be83039c3b5..aed1827a2778 100644
@@ -723,6 +723,51 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon
 
 }
 
+static bool __meminit check_zone_contiguous_fast(struct zone *zone,
+			unsigned long start_pfn, unsigned long nr_pages)
+{
+	const unsigned long end_pfn = start_pfn + nr_pages;
+
+	/*
+	 * Given the moved pfn range's contiguous property is always true,
+	 * under the conditional of empty zone, the contiguous property should
+	 * be true.
+	 */
+	if (zone_is_empty(zone)) {
+		zone->contiguous = true;
+		return true;
+	}
+
+	/*
+	 * If the moved pfn range does not intersect with the original zone span,
+	 * the contiguous property is surely false.
+	 */
+	if (end_pfn < zone->zone_start_pfn || start_pfn > zone_end_pfn(zone)) {
+		zone->contiguous = false;
+		return true;
+	}
+
+	/*
+	 * 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.
+	 */
+	if (end_pfn == zone->zone_start_pfn || start_pfn == zone_end_pfn(zone))
+		return true;
+
+	/*
+	 * If the original zone's hole larger than the moved pages in the range,
+	 * the contiguous property is surely false.
+	 */
+	if (nr_pages < (zone->spanned_pages - zone->present_pages)) {
+		zone->contiguous = false;
+		return true;
+	}
+
+	clear_zone_contiguous(zone);
+	return false;
+}
+
 #ifdef CONFIG_ZONE_DEVICE
 static void section_taint_zone_device(unsigned long pfn)
 {
@@ -752,8 +797,7 @@ 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 bool fast_path = check_zone_contiguous_fast(zone, start_pfn, nr_pages);
 
 	if (zone_is_empty(zone))
 		init_currently_empty_zone(zone, start_pfn, nr_pages);
@@ -783,7 +827,8 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
 			 MEMINIT_HOTPLUG, altmap, migratetype,
 			 isolate_pageblock);
 
-	set_zone_contiguous(zone);
+	if (!fast_path)
+		set_zone_contiguous(zone);
 }
 
 struct auto_movable_stats {
Signature · 1 line
2.47.1
maintainer
#17 lore raw

Hi,

Please start a new thread when sending a new version of a patch next time. And as Wei mentioned, wait a bit for the discussion on vN to settle before sending vN+1.

Quoted 76 lines · Tianyou Li

When invoke move_pfn_range_to_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 |
|                +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      2s      |       80%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      33s      |      6s      |       81%      |
+----------------+------+---------------+--------------+----------------+

[1] Qemu commands to hotplug 512G memory for a VM:
object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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/memory_hotplug.c | 51 ++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 48 insertions(+), 3 deletions(-)
mm/memory_hotplug.c +2 -0
index 0be83039c3b5..aed1827a2778 100644
@@ -723,6 +723,51 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon
 
 }
 
+static bool __meminit check_zone_contiguous_fast(struct zone *zone,
+			unsigned long start_pfn, unsigned long nr_pages)

mm/memory_hotplug.c static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon · line 737

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

I don't think it's safe to set zone->contiguous until the end of move_pfn_range_to_zone(). See commit feee6b298916 ("mm/memory_hotplug:
shrink zones when offlining memory").

check_zone_contiguous_fast() should only check if the zone remains contiguous after hotplug or it's certainly discontinuous, but should not set zone->contiguous. It still must be cleared before resizing the zone and set after the initialization of the memory map.

Quoted 59 lines
mm/memory_hotplug.c +36 -3
+		return true;
+	}
+
+	/*
+	 * If the moved pfn range does not intersect with the original zone span,
+	 * the contiguous property is surely false.
+	 */
+	if (end_pfn < zone->zone_start_pfn || start_pfn > zone_end_pfn(zone)) {
+		zone->contiguous = false;
+		return true;
+	}
+
+	/*
+	 * 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.
+	 */
+	if (end_pfn == zone->zone_start_pfn || start_pfn == zone_end_pfn(zone))
+		return true;
+
+	/*
+	 * If the original zone's hole larger than the moved pages in the range,
+	 * the contiguous property is surely false.
+	 */
+	if (nr_pages < (zone->spanned_pages - zone->present_pages)) {
+		zone->contiguous = false;
+		return true;
+	}
+
+	clear_zone_contiguous(zone);
+	return false;
+}
+
 #ifdef CONFIG_ZONE_DEVICE
 static void section_taint_zone_device(unsigned long pfn)
 {
@@ -752,8 +797,7 @@ 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 bool fast_path = check_zone_contiguous_fast(zone, start_pfn, nr_pages);
 
 	if (zone_is_empty(zone))
 		init_currently_empty_zone(zone, start_pfn, nr_pages);
@@ -783,7 +827,8 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
 			 MEMINIT_HOTPLUG, altmap, migratetype,
 			 isolate_pageblock);
 
-	set_zone_contiguous(zone);
+	if (!fast_path)
+		set_zone_contiguous(zone);
 }
 
 struct auto_movable_stats {
Signature · 1 line
2.47.1
Signature · 2 lines
Sincerely yours,
Mike.
#18 lore raw

Hi Mike,

Thanks for your review. Appreciated.

Quoted 3 lines · Mike Rapoport

Hi,

Please start a new thread when sending a new version of a patch next time.

Got it.

Quoted 2 lines

And as Wei mentioned, wait a bit for the discussion on vN to settle before sending vN+1.

Will do. Thanks.

Quoted 91 lines · Tianyou Li
Quoted 76 lines

When invoke move_pfn_range_to_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 |
|                +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      2s      |       80%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      33s      |      6s      |       81%      |
+----------------+------+---------------+--------------+----------------+

[1] Qemu commands to hotplug 512G memory for a VM:
object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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/memory_hotplug.c | 51 ++++++++++++++++++++++++++++++++++++++++++---
  1 file changed, 48 insertions(+), 3 deletions(-)
mm/memory_hotplug.c +2 -0
index 0be83039c3b5..aed1827a2778 100644
@@ -723,6 +723,51 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon
 
  }
 
+static bool __meminit check_zone_contiguous_fast(struct zone *zone,
+			unsigned long start_pfn, unsigned long nr_pages)

mm/memory_hotplug.c static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon · line 737

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

I don't think it's safe to set zone->contiguous until the end of move_pfn_range_to_zone(). See commit feee6b298916 ("mm/memory_hotplug:
shrink zones when offlining memory").

Quoted 4 lines

check_zone_contiguous_fast() should only check if the zone remains contiguous after hotplug or it's certainly discontinuous, but should not set zone->contiguous. It still must be cleared before resizing the zone and set after the initialization of the memory map.

Thanks for the pointer. Allow me to learn more about the context and get back to you soon. Thanks.

Quoted 59 lines
Quoted 59 lines
mm/memory_hotplug.c +36 -3
+		return true;
+	}
+
+	/*
+	 * If the moved pfn range does not intersect with the original zone span,
+	 * the contiguous property is surely false.
+	 */
+	if (end_pfn < zone->zone_start_pfn || start_pfn > zone_end_pfn(zone)) {
+		zone->contiguous = false;
+		return true;
+	}
+
+	/*
+	 * 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.
+	 */
+	if (end_pfn == zone->zone_start_pfn || start_pfn == zone_end_pfn(zone))
+		return true;
+
+	/*
+	 * If the original zone's hole larger than the moved pages in the range,
+	 * the contiguous property is surely false.
+	 */
+	if (nr_pages < (zone->spanned_pages - zone->present_pages)) {
+		zone->contiguous = false;
+		return true;
+	}
+
+	clear_zone_contiguous(zone);
+	return false;
+}
+
  #ifdef CONFIG_ZONE_DEVICE
  static void section_taint_zone_device(unsigned long pfn)
  {
@@ -752,8 +797,7 @@ 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 bool fast_path = check_zone_contiguous_fast(zone, start_pfn, nr_pages);
 
  	if (zone_is_empty(zone))
  		init_currently_empty_zone(zone, start_pfn, nr_pages);
@@ -783,7 +827,8 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
  			 MEMINIT_HOTPLUG, altmap, migratetype,
  			 isolate_pageblock);
 
-	set_zone_contiguous(zone);
+	if (!fast_path)
+		set_zone_contiguous(zone);
  }
 
  struct auto_movable_stats {
Signature · 1 line
2.47.1
#19 lore raw
In reply to Tianyou Li
Quoted 133 lines · Tianyou Li

When invoke move_pfn_range_to_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 |
|                +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      2s      |       80%      |
|                +------+---------------+--------------+----------------+
|                | 512G |      33s      |      6s      |       81%      |
+----------------+------+---------------+--------------+----------------+

[1] Qemu commands to hotplug 512G memory for a VM:
object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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/memory_hotplug.c | 51 ++++++++++++++++++++++++++++++++++++++++++---
  1 file changed, 48 insertions(+), 3 deletions(-)
mm/memory_hotplug.c +46 -2
index 0be83039c3b5..aed1827a2778 100644
@@ -723,6 +723,51 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon
 
  }
 
+static bool __meminit check_zone_contiguous_fast(struct zone *zone,
+			unsigned long start_pfn, unsigned long nr_pages)
+{
+	const unsigned long end_pfn = start_pfn + nr_pages;
+
+	/*
+	 * Given the moved pfn range's contiguous property is always true,
+	 * under the conditional of empty zone, the contiguous property should
+	 * be true.
+	 */
+	if (zone_is_empty(zone)) {
+		zone->contiguous = true;
+		return true;
+	}
+
+	/*
+	 * If the moved pfn range does not intersect with the original zone span,
+	 * the contiguous property is surely false.
+	 */
+	if (end_pfn < zone->zone_start_pfn || start_pfn > zone_end_pfn(zone)) {
+		zone->contiguous = false;
+		return true;
+	}
+
+	/*
+	 * 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.
+	 */
+	if (end_pfn == zone->zone_start_pfn || start_pfn == zone_end_pfn(zone))
+		return true;
+
+	/*
+	 * If the original zone's hole larger than the moved pages in the range,
+	 * the contiguous property is surely false.
+	 */
+	if (nr_pages < (zone->spanned_pages - zone->present_pages)) {
+		zone->contiguous = false;
+		return true;
+	}
+
+	clear_zone_contiguous(zone);
+	return false;
+}
+
  #ifdef CONFIG_ZONE_DEVICE
  static void section_taint_zone_device(unsigned long pfn)
  {
@@ -752,8 +797,7 @@ 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 bool fast_path = check_zone_contiguous_fast(zone, start_pfn, nr_pages);
 
  	if (zone_is_empty(zone))
  		init_currently_empty_zone(zone, start_pfn, nr_pages);

mm/memory_hotplug.c void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, · line 834

Quoted 9 lines
mm/memory_hotplug.c +2 -1
@@ -783,7 +827,8 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
  			 MEMINIT_HOTPLUG, altmap, migratetype,
  			 isolate_pageblock);
 
-	set_zone_contiguous(zone);
+	if (!fast_path)
+		set_zone_contiguous(zone);
  }
 
  struct auto_movable_stats {

Agreed with Mike that we should keep clearing+resetting the bit.

Also, I don't particularly enjoy the "fast_path" terminology. Probably we want in the end something high-level like:

bool definetly_contig;

definetly_contig = clear_zone_contiguous_for_growing(zone, start_pfn, nr_pages);

...

set_zone_contiguous(zone, definetly_contig);

We could do something similar on the removal path then, where the zone will for sure stay contiguous if we are removing the first/last part.

bool definetly_contig;

stays_contiguous = clear_zone_contiguous_for_shrinking(zone, start_pfn, nr_pages);

...

set_zone_contiguous(zone, definetly_contig);

If we can come up for a better name for definetly_contig that would be nice.

Signature · 3 lines
Cheers

David
#20 lore raw

Thanks for your review David.

Quoted 145 lines · David Hildenbrand (Red Hat)
Quoted 144 lines · Tianyou Li

When invoke move_pfn_range_to_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 |
| +------+---------------+--------------+----------------+
| Memory Hotplug | 256G |      10s      |      2s      | 80%      |
| +------+---------------+--------------+----------------+
|                | 512G |      33s      |      6s      | 81%      |
+----------------+------+---------------+--------------+----------------+ 

[1] Qemu commands to hotplug 512G memory for a VM:
     object_add memory-backend-ram,id=hotmem0,size=512G,share=on device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1 qom-set vmem1 requested-size 512G

[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/memory_hotplug.c | 51 ++++++++++++++++++++++++++++++++++++++++++---

  1 file changed, 48 insertions(+), 3 deletions(-)
mm/memory_hotplug.c +0 -0
index 0be83039c3b5..aed1827a2778 100644
@@ -723,6 +723,51 @@ static void __meminit resize_pgdat_range(struct 

pglist_data *pgdat, unsigned lon
    }
  +static bool __meminit check_zone_contiguous_fast(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;
+
+    /*
+     * Given the moved pfn range's contiguous property is always true,
+     * under the conditional of empty zone, the contiguous property 

should
+     * be true.
+     */
+    if (zone_is_empty(zone)) {
+        zone->contiguous = true;
+        return true;
+    }
+
+    /*
+     * If the moved pfn range does not intersect with the original zone span,
+     * the contiguous property is surely false.
+     */
+    if (end_pfn < zone->zone_start_pfn || start_pfn > zone_end_pfn(zone)) {
+        zone->contiguous = false;
+        return true;
+    }
+
+    /*
+     * 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. +     */
+    if (end_pfn == zone->zone_start_pfn || start_pfn == zone_end_pfn(zone))
+        return true;
+
+    /*
+     * If the original zone's hole larger than the moved pages in the range,
+     * the contiguous property is surely false.
+     */
+    if (nr_pages < (zone->spanned_pages - zone->present_pages)) { +        zone->contiguous = false;
+        return true;
+    }

mm/memory_hotplug.c +5 -0
+
+    clear_zone_contiguous(zone);
+    return false;
+}
+

  #ifdef CONFIG_ZONE_DEVICE
  static void section_taint_zone_device(unsigned long pfn) {

mm/memory_hotplug.c +1 -2
@@ -752,8 +797,7 @@ 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 bool fast_path = check_zone_contiguous_fast(zone, 
 start_pfn, nr_pages);
         if (zone_is_empty(zone))

          init_currently_empty_zone(zone, start_pfn, nr_pages);

mm/memory_hotplug.c +0 -0
@@ -783,7 +827,8 @@ void move_pfn_range_to_zone(struct zone *zone, 
 unsigned long start_pfn,
                MEMINIT_HOTPLUG, altmap, migratetype,
Quoted 8 lines

mm/memory_hotplug.c void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, · line 834

Quoted 6 lines
mm/memory_hotplug.c +2 -0
                isolate_pageblock);
   -    set_zone_contiguous(zone);
+    if (!fast_path)
+        set_zone_contiguous(zone);
   }
     struct auto_movable_stats {

Agreed with Mike that we should keep clearing+resetting the bit.

Got it. Worked with Yuan Liu to understand the risk that if set the zone->contiguous before the pfn range fully initialized. It seems pageblock_pfn_to_page code path could be affect thus potentially it is not safe.

Quoted 21 lines

Also, I don't particularly enjoy the "fast_path" terminology. Probably we want in the end something high-level like:

bool definetly_contig;

definetly_contig = clear_zone_contiguous_for_growing(zone, start_pfn, nr_pages);

...

set_zone_contiguous(zone, definetly_contig);

We could do something similar on the removal path then, where the zone will for sure stay contiguous if we are removing the first/last part.

bool definetly_contig;

stays_contiguous = clear_zone_contiguous_for_shrinking(zone, start_pfn, nr_pages);

Quoted 8 lines

...

set_zone_contiguous(zone, definetly_contig);

If we can come up for a better name for definetly_contig that would be nice.

Instead of a bool value, could the clear_zone_contiguous_for_growing and clear_zone_contiguous_for_shrinking return a enum value to indicate one of the three states: 1. DEFINITELY_CONTIGUOUS; 2. DEFINITELY_NOT_CONTIGUOUS; 3. UNDETERMINED_CONTIGUOUS? The set_zone_contiguous took the state and skip the contiguous check if  DEFINITELY_CONTIGUOUS or DEFINITELY_NOT_CONTIGUOUS.

Regards,

Tianyou

#21 lore raw
Quoted 7 lines

Instead of a bool value, could the clear_zone_contiguous_for_growing and clear_zone_contiguous_for_shrinking return a enum value to indicate one of the three states: 1. DEFINITELY_CONTIGUOUS;
2. DEFINITELY_NOT_CONTIGUOUS; 3. UNDETERMINED_CONTIGUOUS? The set_zone_contiguous took the state and skip the contiguous check if DEFINITELY_CONTIGUOUS or DEFINITELY_NOT_CONTIGUOUS.

I had the exact same thought while writing my rely, so it's worth investigating.

If that helps to come up with even better+descriptive variable/function names, even better :)

Signature · 3 lines
Cheers

David
#22 lore raw

Thanks David for your time to review.

Quoted 8 lines · David Hildenbrand (Red Hat)
Quoted 7 lines

Instead of a bool value, could the clear_zone_contiguous_for_growing and clear_zone_contiguous_for_shrinking return a enum value to indicate one of the three states: 1. DEFINITELY_CONTIGUOUS;
2. DEFINITELY_NOT_CONTIGUOUS; 3. UNDETERMINED_CONTIGUOUS? The set_zone_contiguous took the state and skip the contiguous check if DEFINITELY_CONTIGUOUS or DEFINITELY_NOT_CONTIGUOUS.

Quoted 4 lines

I had the exact same thought while writing my rely, so it's worth investigating.

If that helps to come up with even better+descriptive variable/function names, even better :)

I've created a patch v4 for review in a new thread as previously suggested, Yuan Liu added the test result for memory plug and unplug. Welcome for any comments or suggestions. Appreciated.

Regards,

Tianyou