From nobody Wed Dec 17 06:05:35 2025 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A18A02749DC for ; Mon, 15 Dec 2025 12:07:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765800450; cv=none; b=CZ23Smmj/+giFlAYoxl0RLFLYLJgtbP1hwSNixkLWBgCyW65y+w0E5ZZm1bNZITNnUzEcEntwRtgQzN8/qTbPZyOhbF1ItRfCYUaNXnN7VPyycF14m5PdUgPWCQ8+5EHAYjoG6bowgsnfTuOCgr7vKjENETImyQ7qXCXYDz/wZ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765800450; c=relaxed/simple; bh=fwS7+TY4Z+VbLw0aqSOc6vicVlUfjt/T3wdMK/TkQIQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XlXpCdnmojLHUfvMB5D5ymSx2l628n6atWDG0iR36+UalTdLu51c2c9NknwvPi7GfTuCeEPnOuyzHH+EPogruQZICtAAeWudmhwN9o4i70nyxdXpbWX+VO+NfGPXyjHyDMfyWb4Q4setJ8XPSeuFb1U+O10UVfhXZ+z/WKbaXNg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=nQQBWlXt; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="nQQBWlXt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1765800449; x=1797336449; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fwS7+TY4Z+VbLw0aqSOc6vicVlUfjt/T3wdMK/TkQIQ=; b=nQQBWlXtml2VFsrJZ+fBpPRo4C4xmhIr2yh4ZXmxI7OmUkOoUrxCcwJ6 yDN5FxFZP1qH5tvxm2qpqLNtlkqgDWcHy9ME8tPfSTqiPpTCBhZtEygy3 kttMCm6TLEoC1L6hXtVOqLKP2+Z3WzTBBr+uvY/8qrlZz57u/riHsueki kV5KYCaxUG3v5Ndyx9sNyYS79z6lmwysRy7wwBzgSK06+AW9pQK3NqTUy Ok2ouveZB7DIxBU5DQ9vRYKRPhMgdBQ8tawT59MGCqoQMSz8QHy0x6cHi HM3pRvpL+Xqcog7SFoX3jcFLC8/J9ZtSnBPX+XD00N7stUFEz/mT+F1yn Q==; X-CSE-ConnectionGUID: 8nByalqyS+K8JDXe235mVg== X-CSE-MsgGUID: v4P3hRN1QhaD5rFxiCUbKw== X-IronPort-AV: E=McAfee;i="6800,10657,11642"; a="79154759" X-IronPort-AV: E=Sophos;i="6.21,150,1763452800"; d="scan'208";a="79154759" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Dec 2025 04:07:28 -0800 X-CSE-ConnectionGUID: 9L6dNAMGQC6ZqXwlo151iQ== X-CSE-MsgGUID: RoG3HVTvQr2MI8esHDf0xQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.21,150,1763452800"; d="scan'208";a="228357216" Received: from linux-pnp-server-27.sh.intel.com ([10.239.147.41]) by orviesa002.jf.intel.com with ESMTP; 15 Dec 2025 04:07:24 -0800 From: Tianyou Li To: David Hildenbrand , Oscar Salvador , Mike Rapoport , Wei Yang Cc: linux-mm@kvack.org, Yong Hu , Nanhai Zou , Yuan Liu , Tim Chen , Qiuxu Zhuo , Yu C Chen , Pan Deng , Tianyou Li , Chen Zhang , linux-kernel@vger.kernel.org Subject: [PATCH v6 1/2] mm/memory hotplug/unplug: Optimize zone->contiguous update when changes pfn range Date: Mon, 15 Dec 2025 21:04:36 +0800 Message-ID: <20251215130437.3914342-2-tianyou.li@intel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20251215130437.3914342-1-tianyou.li@intel.com> References: <20251215130437.3914342-1-tianyou.li@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When invoke move_pfn_range_to_zone or remove_pfn_range_from_zone, it will update the zone->contiguous by checking the new zone's pfn range from the beginning to the end, regardless the previous state of the old zone. When the zone's pfn range is large, the cost of traversing the pfn range to update the zone->contiguous could be significant. Add fast paths to quickly detect cases where zone is definitely not contiguous without scanning the new zone. The cases are: when the new range did not overlap with previous range, the contiguous should be false; if the new range adjacent with the previous range, just need to check the new range; if the new added pages could not fill the hole of previous zone, the contiguous should be false. The following test cases of memory hotplug for a VM [1], tested in the environment [2], show that this optimization can significantly reduce the memory hotplug time [3]. +----------------+------+---------------+--------------+----------------+ | | Size | Time (before) | Time (after) | Time Reduction | | +------+---------------+--------------+----------------+ | Plug Memory | 256G | 10s | 2s | 80% | | +------+---------------+--------------+----------------+ | | 512G | 33s | 6s | 81% | +----------------+------+---------------+--------------+----------------+ +----------------+------+---------------+--------------+----------------+ | | Size | Time (before) | Time (after) | Time Reduction | | +------+---------------+--------------+----------------+ | Unplug Memory | 256G | 10s | 2s | 80% | | +------+---------------+--------------+----------------+ | | 512G | 34s | 6s | 82% | +----------------+------+---------------+--------------+----------------+ [1] Qemu commands to hotplug 256G/512G memory for a VM: object_add memory-backend-ram,id=3Dhotmem0,size=3D256G/512G,share=3Don device_add virtio-mem-pci,id=3Dvmem1,memdev=3Dhotmem0,bus=3Dport1 qom-set vmem1 requested-size 256G/512G (Plug Memory) qom-set vmem1 requested-size 0G (Unplug Memory) [2] Hardware : Intel Icelake server Guest Kernel : v6.18-rc2 Qemu : v9.0.0 Launch VM : qemu-system-x86_64 -accel kvm -cpu host \ -drive file=3D./Centos10_cloud.qcow2,format=3Dqcow2,if=3Dvirtio \ -drive file=3D./seed.img,format=3Draw,if=3Dvirtio \ -smp 3,cores=3D3,threads=3D1,sockets=3D1,maxcpus=3D3 \ -m 2G,slots=3D10,maxmem=3D2052472M \ -device pcie-root-port,id=3Dport1,bus=3Dpcie.0,slot=3D1,multifunction= =3Don \ -device pcie-root-port,id=3Dport2,bus=3Dpcie.0,slot=3D2 \ -nographic -machine q35 \ -nic user,hostfwd=3Dtcp::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. Reported-by: Nanhai Zou Reported-by: Chen Zhang Tested-by: Yuan Liu Reviewed-by: Tim Chen Reviewed-by: Qiuxu Zhuo Reviewed-by: Yu C Chen Reviewed-by: Pan Deng Reviewed-by: Nanhai Zou Reviewed-by: Yuan Liu Signed-off-by: Tianyou Li --- mm/internal.h | 8 +++++- mm/memory_hotplug.c | 65 ++++++++++++++++++++++++++++++++++++++++++--- mm/mm_init.c | 12 +++++++-- 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 1561fc2ff5b8..1b5bba6526d4 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -730,7 +730,13 @@ static inline struct page *pageblock_pfn_to_page(unsig= ned long start_pfn, return __pageblock_pfn_to_page(start_pfn, end_pfn, zone); } =20 -void set_zone_contiguous(struct zone *zone); +enum zone_contig_state { + ZONE_CONTIG_YES, + ZONE_CONTIG_NO, + ZONE_CONTIG_MAYBE, +}; + +void set_zone_contiguous(struct zone *zone, enum zone_contig_state state); bool pfn_range_intersects_zones(int nid, unsigned long start_pfn, unsigned long nr_pages); =20 diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 0be83039c3b5..12839032ad42 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -544,6 +544,28 @@ static void update_pgdat_span(struct pglist_data *pgda= t) pgdat->node_spanned_pages =3D node_end_pfn - node_start_pfn; } =20 +static enum zone_contig_state zone_contig_state_after_shrinking(struct zon= e *zone, + unsigned long start_pfn, unsigned long nr_pages) +{ + const unsigned long end_pfn =3D start_pfn + nr_pages; + + /* + * If the removed pfn range inside the original zone span, the contiguous + * property is surely false. + */ + if (start_pfn > zone->zone_start_pfn && end_pfn < zone_end_pfn(zone)) + return ZONE_CONTIG_NO; + + /* If the removed pfn range is at the beginning or end of the + * original zone span, the contiguous property is preserved when + * the original zone is contiguous. + */ + if (start_pfn =3D=3D zone->zone_start_pfn || end_pfn =3D=3D zone_end_pfn(= zone)) + return zone->contiguous ? ZONE_CONTIG_YES : ZONE_CONTIG_MAYBE; + + return ZONE_CONTIG_MAYBE; +} + void remove_pfn_range_from_zone(struct zone *zone, unsigned long start_pfn, unsigned long nr_pages) @@ -551,6 +573,7 @@ void remove_pfn_range_from_zone(struct zone *zone, const unsigned long end_pfn =3D start_pfn + nr_pages; struct pglist_data *pgdat =3D zone->zone_pgdat; unsigned long pfn, cur_nr_pages; + enum zone_contig_state new_contiguous_state =3D ZONE_CONTIG_MAYBE; =20 /* Poison struct pages because they are now uninitialized again. */ for (pfn =3D start_pfn; pfn < end_pfn; pfn +=3D cur_nr_pages) { @@ -571,12 +594,14 @@ void remove_pfn_range_from_zone(struct zone *zone, if (zone_is_zone_device(zone)) return; =20 + new_contiguous_state =3D zone_contig_state_after_shrinking(zone, start_pf= n, + nr_pages); clear_zone_contiguous(zone); =20 shrink_zone_span(zone, start_pfn, start_pfn + nr_pages); update_pgdat_span(pgdat); =20 - set_zone_contiguous(zone); + set_zone_contiguous(zone, new_contiguous_state); } =20 /** @@ -736,6 +761,39 @@ static inline void section_taint_zone_device(unsigned = long pfn) } #endif =20 +static enum zone_contig_state zone_contig_state_after_growing(struct zone = *zone, + unsigned long start_pfn, unsigned long nr_pages) +{ + const unsigned long end_pfn =3D start_pfn + nr_pages; + + if (zone_is_empty(zone)) + return ZONE_CONTIG_YES; + + /* + * 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)) + return ZONE_CONTIG_NO; + + /* + * 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 =3D=3D zone->zone_start_pfn || start_pfn =3D=3D zone_end_pfn(= zone)) + return zone->contiguous ? ZONE_CONTIG_YES : ZONE_CONTIG_NO; + + /* + * 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)) + return ZONE_CONTIG_NO; + + return ZONE_CONTIG_MAYBE; +} + /* * Associate the pfn range with the given zone, initializing the memmaps * and resizing the pgdat/zone data to span the added pages. After this @@ -752,7 +810,8 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned= long start_pfn, { struct pglist_data *pgdat =3D zone->zone_pgdat; int nid =3D pgdat->node_id; - + const enum zone_contig_state new_contiguous_state =3D + zone_contig_state_after_growing(zone, start_pfn, nr_pages); clear_zone_contiguous(zone); =20 if (zone_is_empty(zone)) @@ -783,7 +842,7 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned= long start_pfn, MEMINIT_HOTPLUG, altmap, migratetype, isolate_pageblock); =20 - set_zone_contiguous(zone); + set_zone_contiguous(zone, new_contiguous_state); } =20 struct auto_movable_stats { diff --git a/mm/mm_init.c b/mm/mm_init.c index 7712d887b696..8a56b838913e 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -2263,11 +2263,19 @@ void __init init_cma_pageblock(struct page *page) } #endif =20 -void set_zone_contiguous(struct zone *zone) +void set_zone_contiguous(struct zone *zone, enum zone_contig_state state) { unsigned long block_start_pfn =3D zone->zone_start_pfn; unsigned long block_end_pfn; =20 + if (state =3D=3D ZONE_CONTIG_YES) { + zone->contiguous =3D true; + return; + } + + if (state =3D=3D ZONE_CONTIG_NO) + return; + block_end_pfn =3D pageblock_end_pfn(block_start_pfn); for (; block_start_pfn < zone_end_pfn(zone); block_start_pfn =3D block_end_pfn, @@ -2348,7 +2356,7 @@ void __init page_alloc_init_late(void) shuffle_free_memory(NODE_DATA(nid)); =20 for_each_populated_zone(zone) - set_zone_contiguous(zone); + set_zone_contiguous(zone, ZONE_CONTIG_MAYBE); =20 /* Initialize page ext after all struct pages are initialized. */ if (deferred_struct_pages) --=20 2.47.1