From nobody Tue Dec 2 02:19:34 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.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 7D52F3612FA for ; Wed, 19 Nov 2025 13:08:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763557733; cv=none; b=OnxbnPgb8MJ0b/5FaILBDf2E9LjIhc0DD6mzFTgZ/WCEGopWa8Kmwcjjupb4vK/j/oYkdHx/UUpwNC9qSGPaDLmJSqH2NqneZqYbutfFNFAJ3eGIkhN3fwZgeZMljWzWB7AqBdoVqTqigL5UYkS1PCpcDdZ/CYqsrkpBfZ6XJOk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763557733; c=relaxed/simple; bh=EobbzjPSd7ce0PeoMfEi9EKfhaLXGdK6W7FpdY7oKOk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=A8Pu8lLuRnZphh5Ibj1aZYeAGxncd2pmlAXeiWCQUgSxAAK0HZGYjqWpNLUQ0MkNDsJU9i5DoXYZmkcwTrzmZDoCrDA2TlAUz1gC2Q97viME25wbNY/F875dcwqoBNGzjaa080yoQ6fsF16pXfYTjji0SqrvUg1d1c35MImfQWM= 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=VIs2l5yV; arc=none smtp.client-ip=192.198.163.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="VIs2l5yV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1763557731; x=1795093731; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=EobbzjPSd7ce0PeoMfEi9EKfhaLXGdK6W7FpdY7oKOk=; b=VIs2l5yVqN8KqeQx3NTqrEFXzUJ2kMhXV8X+DWV9usf2FIP0MHiCJvRp zv93e1JOSLc7fo8Kzyd2viP4lQms3QH+AO7/JxF+xGmj4JXx1+3AtK4P1 FAwSGNuXTZPCRq5Utrhe7NZ0q4qEfTPAkKUWD1ovtai4gtdKM1eE9ObGQ HHbTVxgb48zfvehLlZClmjpktA2oBepxvdzvZQbh8GpuXOoMrJ/DNq+7y SfEw4dCgPvbMfayib2E9lfUAuGJp3HiO5rwa2hbdh4NXM3x7dopYtfE7P DZyWQHH5Xn6TL6CDN5aX/lUODf3uzGmpmSn2tXaNZEYBXXWFWKYwL3MKS w==; X-CSE-ConnectionGUID: o1lyuIbUS32DAsFCG3CASQ== X-CSE-MsgGUID: D2eFgNujS7OLfTmTIz36Ng== X-IronPort-AV: E=McAfee;i="6800,10657,11617"; a="69464399" X-IronPort-AV: E=Sophos;i="6.19,315,1754982000"; d="scan'208";a="69464399" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Nov 2025 05:08:49 -0800 X-CSE-ConnectionGUID: XXVaHIofRpuc2rR4fZZopQ== X-CSE-MsgGUID: N3Y1W7EZT9mZSBRsigU3Iw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,315,1754982000"; d="scan'208";a="196003425" Received: from linux-pnp-server-27.sh.intel.com ([10.239.147.41]) by fmviesa004.fm.intel.com with ESMTP; 19 Nov 2025 05:08:45 -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 v3] mm/memory hotplug/unplug: Optimize zone->contiguous update when move pfn range Date: Wed, 19 Nov 2025 22:06:57 +0800 Message-ID: <20251119140657.3845818-1-tianyou.li@intel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20251119114252.oykrczprf3ecd7ak@master> References: <20251119114252.oykrczprf3ecd7ak@master> 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, 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=3Dhotmem0,size=3D512G,share=3Don device_add virtio-mem-pci,id=3Dvmem1,memdev=3Dhotmem0,bus=3Dport1 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=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/memory_hotplug.c | 51 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 0be83039c3b5..aed1827a2778 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -723,6 +723,51 @@ static void __meminit resize_pgdat_range(struct pglist= _data *pgdat, unsigned lon =20 } =20 +static bool __meminit check_zone_contiguous_fast(struct zone *zone, + unsigned long start_pfn, unsigned long nr_pages) +{ + const unsigned long end_pfn =3D 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 =3D 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 =3D 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 =3D=3D zone->zone_start_pfn || start_pfn =3D=3D 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 =3D 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 =3D zone->zone_pgdat; int nid =3D pgdat->node_id; - - clear_zone_contiguous(zone); + const bool fast_path =3D check_zone_contiguous_fast(zone, start_pfn, nr_p= ages); =20 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); =20 - set_zone_contiguous(zone); + if (!fast_path) + set_zone_contiguous(zone); } =20 struct auto_movable_stats { --=20 2.47.1