From nobody Fri Jul 24 21:53:01 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (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 4874D40BCC1 for ; Thu, 23 Jul 2026 08:45:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784796353; cv=none; b=DKGrTTRr1pkS8ZlIeH/bKz4WhosM6dNJkRptSpgMELrOAJB+srK62GRbM8DIN/kmQ79bXPvxbwVns7bGYqsWvtEylJ5+2jCooJGs6dgxVRENz0PChZ4EGysD5EwOn4W3lOjw6Wcp6BWggiwjV7GLzu5NFfwVHPM+U8PeZhvBjLc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784796353; c=relaxed/simple; bh=8WfoTOawSaGZIDDYaRMLxR1K1pmVgTzFDcoX39JzD1s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LFx05BHQ3gpwnNDGclqH0b8qLgOeKbJMlbiXJDFLc/ZzHRF0z75J2gwk0z14v5K4RxPa2RAENwcZIE5KnSotZ1zJby2tJmjC9jK9UCoaoDUfLG0nkYzfdp4T/1aggBXuljjoW635tPNyr24H3ka7gC49SsL2MLXwT3r06/VlK28= 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=TSmt9xoS; arc=none smtp.client-ip=198.175.65.9 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="TSmt9xoS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1784796351; x=1816332351; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8WfoTOawSaGZIDDYaRMLxR1K1pmVgTzFDcoX39JzD1s=; b=TSmt9xoSAOYg5OzgCWbox2QUo+sVmsrIV9QfM7coIxmUI/AGFXYFIx4F w2BuevRfBYlxv1RWb2GyhRX5xbQaDgJ1caT5AjYQyNaYri0dJbO09DjAK c5OBy/jFARy8eS8p3OmwJxb40R3i2OCp8DhHWeuW5qOocSzCWsuDnftfQ 6nngZ1sZd8HvyazsRI4763aikIkjX1Varm/59QMJF3O7Rwj32qYImljpm 3wcZ0sUxNvkYMV8tunWWtV4BwBfQSkXN31KiU0NWaSzr8cKZjvwPbNBHF pQ8KjswrgcGZkVxVngdhApEPdsbrDG75vuvWQKDwIu6ZvVbQCc/gv+EhI g==; X-CSE-ConnectionGUID: L5e3xkbpTPO4DU0uziTxzA== X-CSE-MsgGUID: TPUmpcaCT+u6nPxFsvAT6w== X-IronPort-AV: E=McAfee;i="6800,10657,11854"; a="108234128" X-IronPort-AV: E=Sophos;i="6.25,180,1779174000"; d="scan'208";a="108234128" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jul 2026 01:45:51 -0700 X-CSE-ConnectionGUID: bArUnfzoT9q+iKkUkoUI1A== X-CSE-MsgGUID: 4qg/ypOPSl2hUO78xsq42w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,180,1779174000"; d="scan'208";a="262126115" Received: from spr10.sh.intel.com (HELO localhost) ([10.239.23.75]) by orviesa004.jf.intel.com with ESMTP; 23 Jul 2026 01:45:48 -0700 From: Yuan Liu To: David Hildenbrand , Oscar Salvador , Mike Rapoport , Wei Yang Cc: linux-mm@kvack.org, Nanhai Zou , Yuan Liu , Pan Deng , Tianyou Li , Chen Zhang , Jason Zeng , linux-kernel@vger.kernel.org Subject: [PATCH v6 1/2] mm/memory_hotplug: optimize zone contiguous check when changing pfn range Date: Thu, 23 Jul 2026 04:49:45 -0400 Message-ID: <20260723084946.189392-2-yuan1.liu@intel.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260723084946.189392-1-yuan1.liu@intel.com> References: <20260723084946.189392-1-yuan1.liu@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 move_pfn_range_to_zone() or remove_pfn_range_from_zone() updates a zone, set_zone_contiguous() rescans the entire zone pageblock-by-pageblock to rebuild zone->contiguous. For large zones this is a significant cost during memory hotplug and hot-unplug. Add a new zone member pages_with_online_memmap that tracks the number of pages within the zone span that have an online memory map (including present pages and memory holes smaller than a subsection whose memory map has been initialized). When spanned_pages =3D=3D pages_with_online_memm= ap the zone is contiguous and pfn_to_page() can be called on any PFN in the zone span without further pfn_valid() checks. For early boot memory, pages_with_online_memmap is calculated in memmap_init_zone_range(). Since every PFN within a memblock region satisfies pfn_to_online_page(), the calculation aligns both boundaries of each memblock range to PAGES_PER_SUBSECTION and counts the aligned pages that fall within the zone span. If two adjacent memblock regions share a subsection (i.e. the hole between them is smaller than a subsection), the overlapping subsection pages are counted only once. For hotplugged memory, pages_with_online_memmap is updated through adjust_present_page_count(), which is called during memory online and offline. Only pages that fall within the current zone span are accounted towards pages_with_online_memmap. A "too small" value is safe, it merely prevents detecting a contiguous zone. The contiguity check using pages_with_online_memmap is stricter than the old pageblock-by-pageblock scan. The old set_zone_contiguous() iterated at pageblock granularity via pageblock_pfn_to_page(), so a zone could be marked contiguous even if a subsection-sized hole existed within a pageblock. The new check requires spanned_pages =3D=3D pages_with_online_memmap, meaning every PFN in the zone span must satisfy pfn_to_online_page(). 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 | 3s | 70% | | +------+---------------+--------------+----------------+ | | 512G | 36s | 7s | 81% | +----------------+------+---------------+--------------+----------------+ +----------------+------+---------------+--------------+----------------+ | | Size | Time (before) | Time (after) | Time Reduction | | +------+---------------+--------------+----------------+ | Unplug Memory | 256G | 11s | 4s | 64% | | +------+---------------+--------------+----------------+ | | 512G | 36s | 9s | 75% | +----------------+------+---------------+--------------+----------------+ [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 : v7.0-rc4 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 Reviewed-by: Pan Deng Reviewed-by: Jason Zeng Co-developed-by: Tianyou Li Signed-off-by: Tianyou Li Signed-off-by: Yuan Liu --- Documentation/mm/physical_memory.rst | 13 +++++++ drivers/base/memory.c | 6 +++ include/linux/mmzone.h | 47 ++++++++++++++++++++++ mm/internal.h | 8 +--- mm/memory_hotplug.c | 12 +----- mm/mm_init.c | 58 +++++++++++++++++----------- 6 files changed, 105 insertions(+), 39 deletions(-) diff --git a/Documentation/mm/physical_memory.rst b/Documentation/mm/physic= al_memory.rst index b76183545e5b..0aa65e6b5499 100644 --- a/Documentation/mm/physical_memory.rst +++ b/Documentation/mm/physical_memory.rst @@ -483,6 +483,19 @@ General ``present_pages`` should use ``get_online_mems()`` to get a stable value= . It is initialized by ``calculate_node_totalpages()``. =20 +``pages_with_online_memmap`` + Tracks pages within the zone that have an online memory map (present pag= es + and memory holes whose memory map has been initialized). When + ``spanned_pages`` =3D=3D ``pages_with_online_memmap``, ``pfn_to_page()``= can be + performed without further checks on any PFN within the zone span. + + Note: this counter may temporarily undercount when pages with an online + memory map exist outside the current zone span. This can only happen dur= ing + boot, when initializing the memory map of pages that do not fall into any + zone span. Growing the zone to cover such pages and later shrinking it b= ack + may result in a "too small" value. This is safe: it merely prevents + detecting a contiguous zone. + ``present_early_pages`` The present pages existing within the zone located on memory available s= ince early boot, excluding hotplugged memory. Defined only when diff --git a/drivers/base/memory.c b/drivers/base/memory.c index bcfe2d9f4adb..237ace435372 100644 --- a/drivers/base/memory.c +++ b/drivers/base/memory.c @@ -246,6 +246,7 @@ static int memory_block_online(struct memory_block *mem) nr_vmemmap_pages =3D mem->altmap->free; =20 mem_hotplug_begin(); + clear_zone_contiguous(zone); if (nr_vmemmap_pages) { ret =3D mhp_init_memmap_on_memory(start_pfn, nr_vmemmap_pages, zone); if (ret) @@ -270,6 +271,7 @@ static int memory_block_online(struct memory_block *mem) =20 mem->zone =3D zone; out: + set_zone_contiguous(zone); mem_hotplug_done(); return ret; } @@ -282,6 +284,7 @@ static int memory_block_offline(struct memory_block *me= m) unsigned long start_pfn =3D section_nr_to_pfn(mem->start_section_nr); unsigned long nr_pages =3D PAGES_PER_SECTION * sections_per_block; unsigned long nr_vmemmap_pages =3D 0; + struct zone *zone; int ret; =20 if (!mem->zone) @@ -294,7 +297,9 @@ static int memory_block_offline(struct memory_block *me= m) if (mem->altmap) nr_vmemmap_pages =3D mem->altmap->free; =20 + zone =3D mem->zone; mem_hotplug_begin(); + clear_zone_contiguous(zone); if (nr_vmemmap_pages) adjust_present_page_count(pfn_to_page(start_pfn), mem->group, -nr_vmemmap_pages); @@ -314,6 +319,7 @@ static int memory_block_offline(struct memory_block *me= m) =20 mem->zone =3D NULL; out: + set_zone_contiguous(zone); mem_hotplug_done(); return ret; } diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index ca2712187147..ac1467ac3336 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -1032,6 +1032,20 @@ struct zone { * cma pages is present pages that are assigned for CMA use * (MIGRATE_CMA). * + * pages_with_online_memmap tracks pages within the zone that have + * an online memory map (present pages and memory holes whose memory + * map has been initialized). When spanned_pages =3D=3D + * pages_with_online_memmap, pfn_to_page() can be performed without + * further checks on any PFN within the zone span. + * + * Note: this counter may temporarily undercount when pages with an + * online memory map exist outside the current zone span. This can + * only happen during boot, when initializing the memory map of + * pages that do not fall into any zone span. Growing the zone to + * cover such pages and later shrinking it back may result in a + * "too small" value. This is safe: it merely prevents detecting a + * contiguous zone. + * * So present_pages may be used by memory hotplug or memory power * management logic to figure out unmanaged pages by checking * (present_pages - managed_pages). And managed_pages should be used @@ -1056,6 +1070,7 @@ struct zone { atomic_long_t managed_pages; unsigned long spanned_pages; unsigned long present_pages; + unsigned long pages_with_online_memmap; #if defined(CONFIG_MEMORY_HOTPLUG) unsigned long present_early_pages; #endif @@ -1681,6 +1696,38 @@ static inline bool zone_is_zone_device(const struct = zone *zone) } #endif =20 +/** + * zone_is_contiguous - test whether a zone is contiguous + * @zone: the zone to test. + * + * In a contiguous zone, it is valid to call pfn_to_page() on any PFN in t= he + * spanned zone without requiring pfn_valid() or pfn_to_online_page() chec= ks. + * + * Note that missing synchronization with memory offlining makes any PFN + * traversal prone to races. + * + * ZONE_DEVICE zones are always marked non-contiguous. + * + * Return: true if contiguous, otherwise false. + */ +static inline bool zone_is_contiguous(const struct zone *zone) +{ + return zone->contiguous; +} + +static inline void set_zone_contiguous(struct zone *zone) +{ + if (zone_is_zone_device(zone)) + return; + if (zone->spanned_pages =3D=3D zone->pages_with_online_memmap) + zone->contiguous =3D true; +} + +static inline void clear_zone_contiguous(struct zone *zone) +{ + zone->contiguous =3D false; +} + /* * Returns true if a zone has pages managed by the buddy allocator. * All the reclaim decisions have to use this function rather than diff --git a/mm/internal.h b/mm/internal.h index 181e79f1d6a2..f932b7577c92 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -805,21 +805,15 @@ extern struct page *__pageblock_pfn_to_page(unsigned = long start_pfn, static inline struct page *pageblock_pfn_to_page(unsigned long start_pfn, unsigned long end_pfn, struct zone *zone) { - if (zone->contiguous) + if (zone_is_contiguous(zone)) return pfn_to_page(start_pfn); =20 return __pageblock_pfn_to_page(start_pfn, end_pfn, zone); } =20 -void set_zone_contiguous(struct zone *zone); bool pfn_range_intersects_zones(int nid, unsigned long start_pfn, unsigned long nr_pages); =20 -static inline void clear_zone_contiguous(struct zone *zone) -{ - zone->contiguous =3D false; -} - extern int __isolate_free_page(struct page *page, unsigned int order); extern void __putback_isolated_page(struct page *page, unsigned int order, int mt); diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 7ac19fab2263..4c699fd9c479 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -557,18 +557,13 @@ void remove_pfn_range_from_zone(struct zone *zone, =20 /* * Zone shrinking code cannot properly deal with ZONE_DEVICE. So - * we will not try to shrink the zones - which is okay as - * set_zone_contiguous() cannot deal with ZONE_DEVICE either way. + * we will not try to shrink it. */ if (zone_is_zone_device(zone)) return; =20 - clear_zone_contiguous(zone); - shrink_zone_span(zone, start_pfn, start_pfn + nr_pages); update_pgdat_span(pgdat); - - set_zone_contiguous(zone); } =20 /** @@ -746,8 +741,6 @@ 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; =20 - clear_zone_contiguous(zone); - if (zone_is_empty(zone)) init_currently_empty_zone(zone, start_pfn, nr_pages); resize_zone_range(zone, start_pfn, nr_pages); @@ -775,8 +768,6 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned= long start_pfn, memmap_init_range(nr_pages, nid, zone_idx(zone), start_pfn, 0, MEMINIT_HOTPLUG, altmap, migratetype, isolate_pageblock); - - set_zone_contiguous(zone); } =20 struct auto_movable_stats { @@ -1072,6 +1063,7 @@ void adjust_present_page_count(struct page *page, str= uct memory_group *group, if (early_section(__pfn_to_section(page_to_pfn(page)))) zone->present_early_pages +=3D nr_pages; zone->present_pages +=3D nr_pages; + zone->pages_with_online_memmap +=3D nr_pages; zone->zone_pgdat->node_present_pages +=3D nr_pages; =20 if (group && movable) diff --git a/mm/mm_init.c b/mm/mm_init.c index f1afe023e4e7..73d1fde17040 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -916,6 +916,40 @@ void __meminit memmap_init_range(unsigned long size, i= nt nid, unsigned long zone } } =20 +static void __init update_zone_online_memmap_pages(struct zone *zone, + unsigned long start_pfn, + unsigned long end_pfn, + unsigned long *hole_pfn) +{ +#ifdef CONFIG_SPARSEMEM_VMEMMAP + unsigned long zone_start_pfn =3D zone->zone_start_pfn; + unsigned long zone_end_pfn =3D zone_start_pfn + zone->spanned_pages; + unsigned long sub_start, sub_end; + + sub_start =3D max(ALIGN_DOWN(start_pfn, PAGES_PER_SUBSECTION), + zone_start_pfn); + sub_end =3D min(ALIGN(end_pfn, PAGES_PER_SUBSECTION), zone_end_pfn); + + /* + * For an in-zone hole smaller than a subsection, the adjacent + * memblock ranges share a subsection. Avoid counting the same + * subsection's pages twice. + */ + if (*hole_pfn > zone_start_pfn) { + unsigned long prev_sub_end; + + prev_sub_end =3D min(ALIGN(*hole_pfn, PAGES_PER_SUBSECTION), + zone_end_pfn); + sub_start =3D max(sub_start, prev_sub_end); + } + + if (sub_start < sub_end) + zone->pages_with_online_memmap +=3D sub_end - sub_start; +#else + zone->pages_with_online_memmap +=3D end_pfn - start_pfn; +#endif +} + static void __init memmap_init_zone_range(struct zone *zone, unsigned long start_pfn, unsigned long end_pfn, @@ -932,6 +966,8 @@ static void __init memmap_init_zone_range(struct zone *= zone, if (start_pfn >=3D end_pfn) return; =20 + update_zone_online_memmap_pages(zone, start_pfn, end_pfn, hole_pfn); + memmap_init_range(end_pfn - start_pfn, nid, zone_id, start_pfn, zone_end_pfn, MEMINIT_EARLY, NULL, mt, false); =20 @@ -2195,28 +2231,6 @@ void __init init_cma_pageblock(struct page *page) } #endif =20 -void set_zone_contiguous(struct zone *zone) -{ - unsigned long block_start_pfn =3D zone->zone_start_pfn; - unsigned long block_end_pfn; - - 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, - block_end_pfn +=3D pageblock_nr_pages) { - - block_end_pfn =3D min(block_end_pfn, zone_end_pfn(zone)); - - if (!__pageblock_pfn_to_page(block_start_pfn, - block_end_pfn, zone)) - return; - cond_resched(); - } - - /* We confirm that there is no hole */ - zone->contiguous =3D true; -} - /* * Check if a PFN range intersects multiple zones on one or more * NUMA nodes. Specify the @nid argument if it is known that this --=20 2.47.3 From nobody Fri Jul 24 21:53:01 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) (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 5B1A540B39F for ; Thu, 23 Jul 2026 08:45:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784796356; cv=none; b=bpE1Yy0qhJRKU6wkKQbZ83jIx8KpImGdgFfEhAjR8oFKI99sRXAlm98VnL03TNYap1DEwY222ckYmqyI1cl5//SIlIBjkhD4kVLoACiDAhzm6aEMdwgPj4g8cqNzd/Rfh2tT/PmWrzvdlwNMsKUtKu15sBdHlSVcOELmPD62tl8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784796356; c=relaxed/simple; bh=7rbY+wmZoNdck+weN1iewQCGpYuqzUNthhhl13Abfnc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ux0fCv3qyHtb/aEqGHxrIIjmWrAtqLv2yVGvhO2SgkdfLQru3v0RA3W2vcIKZZEKOSj0kPSwQPd/nhRv8uCiKIDzqJ9GCGXaPtCsV8d/8MOY55UbZzhtdzsu0ToyMmjbKvTO2Gbbpezjn1N0VmM3ku90bgcGb1+R//kg4ZjQPfM= 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=DJ74iq71; arc=none smtp.client-ip=192.198.163.16 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="DJ74iq71" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1784796355; x=1816332355; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7rbY+wmZoNdck+weN1iewQCGpYuqzUNthhhl13Abfnc=; b=DJ74iq716ELiCikcGhqTXRbWslafIfwkXDX5YbTmCAe86gBNHPJUTFNV 5TFgbMUdoMHCxdRtDNgs2ojDlAfJnml14Sr/PqSRChO7E0rXOuiZrkF8c A2axZ/DCc+57SAB6/YKhbbLsVchgxRR1mEBLpyKclo8Opyp/5tbtMHZOM S1E7mw+UnqB8MhNfKzFy9u8Res950jY08PzS8jua1H0ubQU2vFLxh0hmd /0JFrtyLtvlzuRLy1AZgb3kcxtVPwiL6RMoXM9SsvEo4xCy7bVEY96gvu M2p5ClCizN2XXDw/x82nGnH+aBtr8JYcRbS2rm7b5oHTWWOrMOifXV00Z Q==; X-CSE-ConnectionGUID: WpW0GD3bSn2Bd+q/fwGd7Q== X-CSE-MsgGUID: Tz9AI+IlRf60TEQqc7sBOQ== X-IronPort-AV: E=McAfee;i="6800,10657,11854"; a="72986450" X-IronPort-AV: E=Sophos;i="6.25,180,1779174000"; d="scan'208";a="72986450" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jul 2026 01:45:54 -0700 X-CSE-ConnectionGUID: FUhMTWgORFmJguJuC3tvsA== X-CSE-MsgGUID: ID6ghIrQTSmIBXZJFQdXXQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,180,1779174000"; d="scan'208";a="254966703" Received: from spr10.sh.intel.com (HELO localhost) ([10.239.23.75]) by fmviesa007.fm.intel.com with ESMTP; 23 Jul 2026 01:45:52 -0700 From: Yuan Liu To: David Hildenbrand , Oscar Salvador , Mike Rapoport , Wei Yang Cc: linux-mm@kvack.org, Nanhai Zou , Yuan Liu , Pan Deng , Tianyou Li , Chen Zhang , Jason Zeng , linux-kernel@vger.kernel.org Subject: [PATCH v6 2/2] mm/memory_hotplug: improve shrink_zone_span() subsection boundary checks Date: Thu, 23 Jul 2026 04:49:46 -0400 Message-ID: <20260723084946.189392-3-yuan1.liu@intel.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260723084946.189392-1-yuan1.liu@intel.com> References: <20260723084946.189392-1-yuan1.liu@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 shrinking a zone span after removing a PFN range, find_smallest_section_pfn() and find_biggest_section_pfn() only checked one edge PFN in each subsection for nid/zone matching. If a memory or hole boundary falls in the middle of a subsection, that edge PFN may belong to a different nid/zone, causing the helpers to miss a valid PFN within that subsection. Fix this by checking both subsection edge PFNs for nid/zone matching. Keep a single pfn_to_online_page() check per subsection, since online state is the same for all PFNs in a subsection. Reviewed-by: Jason Zeng Signed-off-by: Yuan Liu --- mm/memory_hotplug.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 4c699fd9c479..3a281d595207 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -427,17 +427,24 @@ static unsigned long find_smallest_section_pfn(int ni= d, struct zone *zone, unsigned long start_pfn, unsigned long end_pfn) { - for (; start_pfn < end_pfn; start_pfn +=3D PAGES_PER_SUBSECTION) { - if (unlikely(!pfn_to_online_page(start_pfn))) - continue; + unsigned long next_pfn; =20 - if (unlikely(pfn_to_nid(start_pfn) !=3D nid)) - continue; + for (; start_pfn < end_pfn; start_pfn =3D next_pfn) { + unsigned long tail_pfn; =20 - if (zone !=3D page_zone(pfn_to_page(start_pfn))) + next_pfn =3D start_pfn + PAGES_PER_SUBSECTION; + tail_pfn =3D next_pfn - 1; + + if (unlikely(!pfn_to_online_page(start_pfn))) continue; =20 - return start_pfn; + if (likely(pfn_to_nid(start_pfn) =3D=3D nid) && + zone =3D=3D page_zone(pfn_to_page(start_pfn))) + return start_pfn; + + if (likely(pfn_to_nid(tail_pfn) =3D=3D nid) && + zone =3D=3D page_zone(pfn_to_page(tail_pfn))) + return start_pfn; } =20 return 0; @@ -448,21 +455,26 @@ static unsigned long find_biggest_section_pfn(int nid= , struct zone *zone, unsigned long start_pfn, unsigned long end_pfn) { - unsigned long pfn; + unsigned long pfn, prev_pfn; =20 /* pfn is the end pfn of a memory section. */ pfn =3D end_pfn - 1; - for (; pfn >=3D start_pfn; pfn -=3D PAGES_PER_SUBSECTION) { - if (unlikely(!pfn_to_online_page(pfn))) - continue; + for (; pfn >=3D start_pfn; pfn =3D prev_pfn) { + unsigned long head_pfn; =20 - if (unlikely(pfn_to_nid(pfn) !=3D nid)) - continue; + prev_pfn =3D pfn - PAGES_PER_SUBSECTION; + head_pfn =3D prev_pfn + 1; =20 - if (zone !=3D page_zone(pfn_to_page(pfn))) + if (unlikely(!pfn_to_online_page(pfn))) continue; =20 - return pfn; + if (likely(pfn_to_nid(pfn) =3D=3D nid) && + zone =3D=3D page_zone(pfn_to_page(pfn))) + return pfn; + + if (likely(pfn_to_nid(head_pfn) =3D=3D nid) && + zone =3D=3D page_zone(pfn_to_page(head_pfn))) + return pfn; } =20 return 0; --=20 2.47.3