mm/memory hotplug/unplug: Optimize zone->contiguous update when move pfn range
Subsystems (4)
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
@@ -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
@@ -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