From nobody Sat Sep 26 13:47:35 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) (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 8BA5E39EF0B for ; Tue, 1 Sep 2026 05:26:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.8 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788240382; cv=none; b=A0srsILpMhB1wP8xdfxf9JwsD69WbjpZRiIGapmkq1IK4x2+umVToWLlDos+E/PO6shc+6pI9UMLEM1ugwE78gGC1GznNiwK3BWrHKHTh4KNxQcRHrjetNo6Wtz3O5MDZzvnAD9bFMnI2UCWyQy5C9fnkPP7GAy3oeSgiqT1E2s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788240382; c=relaxed/simple; bh=LC+ES9Zs+P62Cj43NmP5rnYbecoNxXcSCIKuWyYw+ws=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qVFlkuAn5wUrRcxLyKYdqfax94aZRsy42AR8OJi4Abg5TPTsDcEAqc0OE6vFE96fiKA9GVMlZI0kioUcYiA9yCVBqG0O2RydHm7fBNUbdquI+qrrZrllHQm4ykX7XKYnAllf6Xwu8FdO8OkKoq0C9LFliw6nOysChlQB8SbkfRc= 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=ArgoAFNu; arc=none smtp.client-ip=192.198.163.8 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="ArgoAFNu" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1788240380; x=1819776380; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LC+ES9Zs+P62Cj43NmP5rnYbecoNxXcSCIKuWyYw+ws=; b=ArgoAFNut0Iq+7iK0ylGUoTwvWcuKwM5hnagn7Be2WERktiTs59Letm6 1/dGvsnRmjlx6YB1WLG/b9hm622SDH+6KZ4vkp5ubZphXGTaly3C+W6C+ FHf2yCUlf1E/oiwuHRbEVObt0gjzMwIDRJNw3ni6Z0KM11LqlFCfHxS2J qWFSOqJBntViMn0nYV8MI/kaI76K4EE370EQ+mKQ9m9Q5EyejHDyFIs+i 6iVFErIBLsyKv36AV57xoi2zjdWeZUATdO2twKSGGnmvTecr1pW1NVa8h P+FlkvEU4ASowKLz+5BtAodSTowewSSPTDEmdPpnMg+mgUt6UrIZNnVgM Q==; X-CSE-ConnectionGUID: FqmYMNrQRy6Q8nWcW/UomQ== X-CSE-MsgGUID: ESYegDWURRCVLRrVm5mhiw== X-IronPort-AV: E=McAfee;i="6800,10657,11892"; a="106178885" X-IronPort-AV: E=Sophos;i="6.25,255,1779174000"; d="scan'208";a="106178885" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Aug 2026 22:26:20 -0700 X-CSE-ConnectionGUID: sx7L6WB1ShKyXMiIStET/A== X-CSE-MsgGUID: r5Qnre0GT9GQmfAOd0TZCg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,255,1779174000"; d="scan'208";a="292482084" Received: from spr10.sh.intel.com (HELO localhost) ([10.239.23.75]) by fmviesa002.fm.intel.com with ESMTP; 31 Aug 2026 22:26:17 -0700 From: Yuan Liu To: David Hildenbrand , Oscar Salvador , Mike Rapoport , Wei Yang Cc: linux-mm@kvack.org, Nanhai Zou , Chen Zhang , Yuan Liu , Jason Zeng , Chen Yu , Pan Deng , Tianyou Li , linux-kernel@vger.kernel.org Subject: [PATCH v8 1/2] mm/memory_hotplug: make shrink_zone_span() more robust Date: Tue, 1 Sep 2026 01:29:49 -0400 Message-ID: <20260901052950.3284540-2-yuan1.liu@intel.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260901052950.3284540-1-yuan1.liu@intel.com> References: <20260901052950.3284540-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" From: "David Hildenbrand (Arm)" Let's make shrink_zone_span() more robust by checking in find_smallest_section_pfn() / find_biggest_section_pfn() that the start and end PFNs of the subsection are within the zone. While at it, clean up the function by factoring the core check out into subsection_overlaps_zone(). There likely is no need to check the nid first. We require SPARSEMEM_VMEMMAP_ENABLE, where pfn_to_page() is cheap, and pfn_to_nid() on CONFIG_NUMA would call pfn_to_page() either way. So let's just drop that for now. Signed-off-by: David Hildenbrand (Arm) Tested-by: Yuan Liu Signed-off-by: Yuan Liu Reviewed-by: Wei Yang --- mm/memory_hotplug.c | 59 ++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 35 deletions(-) diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 226ab9cb078a..9f19876ec3ec 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -425,49 +425,39 @@ int __add_pages(int nid, unsigned long pfn, unsigned = long nr_pages, return err; } =20 -/* find the smallest valid pfn in the range [start_pfn, end_pfn) */ -static unsigned long find_smallest_section_pfn(int nid, struct zone *zone, - unsigned long start_pfn, - unsigned long end_pfn) +static bool subsection_overlaps_zone(unsigned long pfn, struct zone *zone) { - for (; start_pfn < end_pfn; start_pfn +=3D PAGES_PER_SUBSECTION) { - if (unlikely(!pfn_to_online_page(start_pfn))) - continue; + const unsigned long start_pfn =3D ALIGN_DOWN(pfn, PAGES_PER_SUBSECTION); + const unsigned long end_pfn =3D start_pfn + PAGES_PER_SUBSECTION - 1; =20 - if (unlikely(pfn_to_nid(start_pfn) !=3D nid)) - continue; + /* All pages in a subsection are either online or offline. */ + if (unlikely(!pfn_to_online_page(start_pfn))) + return false; =20 - if (zone !=3D page_zone(pfn_to_page(start_pfn))) - continue; + /* Checking start+end is sufficient. */ + return zone =3D=3D page_zone(pfn_to_page(start_pfn)) || + zone =3D=3D page_zone(pfn_to_page(end_pfn)); +} =20 - return start_pfn; +/* find the smallest valid pfn in the range [start_pfn, end_pfn) */ +static unsigned long find_smallest_section_pfn(struct zone *zone, + unsigned long start_pfn, unsigned long end_pfn) +{ + for (; start_pfn < end_pfn; start_pfn +=3D PAGES_PER_SUBSECTION) { + if (subsection_overlaps_zone(start_pfn, zone)) + return start_pfn; } - return 0; } =20 /* find the biggest valid pfn in the range [start_pfn, end_pfn). */ -static unsigned long find_biggest_section_pfn(int nid, struct zone *zone, - unsigned long start_pfn, - unsigned long end_pfn) +static unsigned long find_biggest_section_pfn(struct zone *zone, + unsigned long start_pfn, unsigned long end_pfn) { - unsigned long pfn; - - /* 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; - - if (unlikely(pfn_to_nid(pfn) !=3D nid)) - continue; - - if (zone !=3D page_zone(pfn_to_page(pfn))) - continue; - - return pfn; + for (; end_pfn > start_pfn; end_pfn -=3D PAGES_PER_SUBSECTION) { + if (subsection_overlaps_zone(end_pfn - 1, zone)) + return end_pfn - 1; } - return 0; } =20 @@ -475,7 +465,6 @@ static void shrink_zone_span(struct zone *zone, unsigne= d long start_pfn, unsigned long end_pfn) { unsigned long pfn; - int nid =3D zone_to_nid(zone); =20 if (zone->zone_start_pfn =3D=3D start_pfn) { /* @@ -484,7 +473,7 @@ static void shrink_zone_span(struct zone *zone, unsigne= d long start_pfn, * In this case, we find second smallest valid mem_section * for shrinking zone. */ - pfn =3D find_smallest_section_pfn(nid, zone, end_pfn, + pfn =3D find_smallest_section_pfn(zone, end_pfn, zone_end_pfn(zone)); if (pfn) { zone->spanned_pages =3D zone_end_pfn(zone) - pfn; @@ -500,7 +489,7 @@ static void shrink_zone_span(struct zone *zone, unsigne= d long start_pfn, * In this case, we find second biggest valid mem_section for * shrinking zone. */ - pfn =3D find_biggest_section_pfn(nid, zone, zone->zone_start_pfn, + pfn =3D find_biggest_section_pfn(zone, zone->zone_start_pfn, start_pfn); if (pfn) zone->spanned_pages =3D pfn - zone->zone_start_pfn + 1; --=20 2.47.3 From nobody Sat Sep 26 13:47:35 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) (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 0B98B3A05C2 for ; Tue, 1 Sep 2026 05:26:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.8 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788240387; cv=none; b=X4quBV95smmlBD/u/x/bcwT4sLPmFj9qUvqqTWJ2x4e/wnwaq2qQTtfh8nrRGUay0t/0ACGRr267tiZLA98lrmPnb2tJ7WCyNzWmBY9yqW0+b8U0wRCsAtGgnOznhNSOmNFowhqBYWzoJYz7pCD0xiIwaec+aVl/AutHXEs0F1o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788240387; c=relaxed/simple; bh=RoaZidmG7b7/mz0yVDo9ZctXO8An3zNHM3in1PWNKYo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=k1eqdainfPKSFZc1nF20f4VEt4dgN1DYxCv+9xKn3SIrecDXkHz4QjXzJQcQjNkFu0v8HU+ExyopBJma9hPyJDzvSEqS2MSBKqf+57MYdpHbz1m9muAVdjlqcY9SIkn8EAhosWD8Xj5KMiAJTlUkgJ3XRbRAIj3vSP+7BtpheNQ= 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=LJrtnEu6; arc=none smtp.client-ip=192.198.163.8 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="LJrtnEu6" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1788240385; x=1819776385; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RoaZidmG7b7/mz0yVDo9ZctXO8An3zNHM3in1PWNKYo=; b=LJrtnEu6S3ZYgfuNsORmN8yVFAcfSJ51nfZcHgbUm4NjDiUn9bXrmgBI LjfJeSz8v5CjurRR6CxRPPXtIYEfiL57BJrKy9zKLsX3Q409LcsjEC2l2 kAaf5xq0F8R6648lMktu02aRa9WUn0/cA/2wN8LtouANsi3SJX0CIMz+C papPQhYyCVskV+2+qtfncw2WtfLUZiVWcUCeRb75O6zlY+AmoeqYCPDEi t4huoNjoom++cWOvJ8EJE+Mmp5BIh0BGXL7SOyKVIzOM0Vo9XltQT+D62 JWntOuDlPk0rndXA0gIQoMV8IgiOYSqJApeqVlpIEFg2r8P9QpWg0/mxe g==; X-CSE-ConnectionGUID: 81mFyJ5PTDStuKtUGdKlUg== X-CSE-MsgGUID: rTdKVcj8SW2daOm//88T+Q== X-IronPort-AV: E=McAfee;i="6800,10657,11892"; a="106178903" X-IronPort-AV: E=Sophos;i="6.25,255,1779174000"; d="scan'208";a="106178903" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Aug 2026 22:26:24 -0700 X-CSE-ConnectionGUID: FjQQqBa3RAC7vKUvQaze4g== X-CSE-MsgGUID: 8P5bcomPRs6UhkCAGUXvtA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,255,1779174000"; d="scan'208";a="292482115" Received: from spr10.sh.intel.com (HELO localhost) ([10.239.23.75]) by fmviesa002.fm.intel.com with ESMTP; 31 Aug 2026 22:26:21 -0700 From: Yuan Liu To: David Hildenbrand , Oscar Salvador , Mike Rapoport , Wei Yang Cc: linux-mm@kvack.org, Nanhai Zou , Chen Zhang , Yuan Liu , Jason Zeng , Chen Yu , Pan Deng , Tianyou Li , linux-kernel@vger.kernel.org Subject: [PATCH v8 2/2] mm/memory_hotplug: optimize zone contiguous check when changing pfn range Date: Tue, 1 Sep 2026 01:29:50 -0400 Message-ID: <20260901052950.3284540-3-yuan1.liu@intel.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260901052950.3284540-1-yuan1.liu@intel.com> References: <20260901052950.3284540-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 whose memory map has been initialized and for which pfn_to_online_page() succeeds. For early boot memory, pages_with_online_memmap is calculated in memmap_init_zone_range(). PFNs initialized by memmap_init_range() are included in pages_with_online_memmap, and hole PFNs for which pfn_to_online_page() succeeds are also counted in init_unavailable_range(). For hotplugged memory, pages_with_online_memmap is updated through adjust_present_page_count(), which is called during memory online and offline operations. When spanned_pages =3D=3D pages_with_online_memmap, every PFN in the zone span has a valid memmap entry, so pfn_to_page() can be called for any PFN within the zone span without an additional pfn_valid() check. The 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 value that is too small. This is safe, as 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.3-rc1 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: Jason Zeng Reviewed-by: Chen Yu Reviewed-by: Pan Deng Co-developed-by: Tianyou Li Signed-off-by: Tianyou Li Signed-off-by: Yuan Liu --- Documentation/mm/physical_memory.rst | 6 +++ drivers/base/memory.c | 7 +++- include/linux/mmzone.h | 48 +++++++++++++++++++++ mm/memory_hotplug.c | 12 +----- mm/mm_init.c | 63 +++++++++++++++------------- mm/mm_init.h | 6 --- mm/page_alloc.h | 2 +- 7 files changed, 98 insertions(+), 46 deletions(-) diff --git a/Documentation/mm/physical_memory.rst b/Documentation/mm/physic= al_memory.rst index a09407d72973..2e67e8b23a99 100644 --- a/Documentation/mm/physical_memory.rst +++ b/Documentation/mm/physical_memory.rst @@ -480,6 +480,12 @@ 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`` + Pages within the zone that have an online memory map: present pages and + memory holes whose memory map has been initialized and + ``pfn_to_online_page()`` succeeds. See the comment for + ``pages_with_online_memmap`` in ``include/linux/mmzone.h`` for more deta= ils. + ``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 5eead3346f1e..28f9503f6a71 100644 --- a/drivers/base/memory.c +++ b/drivers/base/memory.c @@ -255,6 +255,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) @@ -279,6 +280,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; } @@ -304,6 +306,7 @@ static int memory_block_offline(struct memory_block *me= m) nr_vmemmap_pages =3D mem->altmap->free; =20 mem_hotplug_begin(); + clear_zone_contiguous(mem->zone); if (nr_vmemmap_pages) adjust_present_page_count(pfn_to_page(start_pfn), mem->group, -nr_vmemmap_pages); @@ -321,8 +324,10 @@ static int memory_block_offline(struct memory_block *m= em) if (nr_vmemmap_pages) mhp_deinit_memmap_on_memory(start_pfn, nr_vmemmap_pages); =20 - mem->zone =3D NULL; out: + set_zone_contiguous(mem->zone); + if (!ret) + mem->zone =3D NULL; mem_hotplug_done(); return ret; } diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 94f9c3ff5416..7bfb871d6344 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -1043,6 +1043,21 @@ 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 and pfn_to_online_page() + * succeeds. 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 @@ -1067,6 +1082,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 @@ -1694,6 +1710,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 READ_ONCE(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) + WRITE_ONCE(zone->contiguous, true); +} + +static inline void clear_zone_contiguous(struct zone *zone) +{ + WRITE_ONCE(zone->contiguous, 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/memory_hotplug.c b/mm/memory_hotplug.c index 9f19876ec3ec..100941d1b828 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -549,18 +549,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 /** @@ -738,8 +733,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); @@ -767,8 +760,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 { @@ -1064,6 +1055,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 1533aebafb68..d40a8ff23370 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -817,22 +817,39 @@ void __meminit init_deferred_page(unsigned long pfn, = int nid) * zone/node above the hole except for the trailing pages in the last * section that will be appended to the zone/node below. */ -static void __init init_unavailable_range(unsigned long spfn, - unsigned long epfn, - int zone, int node) +static unsigned long __init init_unavailable_range(unsigned long spfn, + unsigned long epfn, + int zone, int node) { + unsigned long next_chunk_pfn __maybe_unused =3D spfn; unsigned long pfn; - u64 pgcnt =3D 0; + u64 online_pgcnt =3D 0, pgcnt =3D 0; + bool is_online =3D true; =20 for_each_valid_pfn(pfn, spfn, epfn) { __init_single_page(pfn_to_page(pfn), pfn, zone, node); __SetPageReserved(pfn_to_page(pfn)); pgcnt++; + + /* + * With vmemmap, at this stage all pages in an early section + * have a valid memmap and are marked as online. However, only + * subsections in the subsection map are actually online. + */ +#ifdef CONFIG_SPARSEMEM_VMEMMAP + if (pfn >=3D next_chunk_pfn) { + is_online =3D pfn_section_valid(__pfn_to_section(pfn), pfn); + next_chunk_pfn =3D min(SUBSECTION_ALIGN_UP(pfn + 1), epfn); + } +#endif + if (is_online) + online_pgcnt++; } =20 if (pgcnt) pr_info("On node %d, zone %s: %lld pages in unavailable ranges\n", node, zone_names[zone], pgcnt); + return online_pgcnt; } =20 /* @@ -930,9 +947,21 @@ static void __init memmap_init_zone_range(struct zone = *zone, memmap_init_range(end_pfn - start_pfn, nid, zone_id, start_pfn, zone_end_pfn, MEMINIT_EARLY, NULL, MIGRATE_MOVABLE, false); + zone->pages_with_online_memmap +=3D end_pfn - start_pfn; =20 - if (*hole_pfn < start_pfn) - init_unavailable_range(*hole_pfn, start_pfn, zone_id, nid); + if (*hole_pfn < start_pfn) { + unsigned long hole_start_pfn =3D *hole_pfn; + unsigned long pgcnt; + + if (hole_start_pfn < zone_start_pfn) { + init_unavailable_range(hole_start_pfn, zone_start_pfn, + zone_id, nid); + hole_start_pfn =3D zone_start_pfn; + } + pgcnt =3D init_unavailable_range(hole_start_pfn, start_pfn, + zone_id, nid); + zone->pages_with_online_memmap +=3D pgcnt; + } =20 *hole_pfn =3D end_pfn; } @@ -2188,28 +2217,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 diff --git a/mm/mm_init.h b/mm/mm_init.h index 39f75df9be1c..520d53ecc68d 100644 --- a/mm/mm_init.h +++ b/mm/mm_init.h @@ -20,15 +20,9 @@ struct vmem_altmap; /* perform sanity checks on struct pages being allocated or freed */ DECLARE_STATIC_KEY_MAYBE(CONFIG_DEBUG_VM, check_pages_enabled); =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; -} - void memblock_free_pages(unsigned long pfn, unsigned int order); =20 void *memmap_alloc(phys_addr_t size, phys_addr_t align, phys_addr_t min_ad= dr, diff --git a/mm/page_alloc.h b/mm/page_alloc.h index b9259deddb59..d4182f7cb7dd 100644 --- a/mm/page_alloc.h +++ b/mm/page_alloc.h @@ -214,7 +214,7 @@ extern struct page *__pageblock_pfn_to_page(unsigned lo= ng 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 2.47.3