The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Yuan Liu <yuan1.liu@intel.com>,
	Oscar Salvador <osalvador@suse.de>,
	Mike Rapoport <rppt@kernel.org>,
	Wei Yang <richard.weiyang@gmail.com>
Cc: linux-mm@kvack.org, Nanhai Zou <nanhai.zou@intel.com>,
	Pan Deng <pan.deng@intel.com>, Tianyou Li <tianyou.li@intel.com>,
	Chen Zhang <zhangchen.kidd@jd.com>,
	Jason Zeng <jason.zeng@intel.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v6 1/2] mm/memory_hotplug: optimize zone contiguous check when changing pfn range
Date: Wed, 5 Aug 2026 13:53:23 +0200	[thread overview]
Message-ID: <a15107de-ab16-433a-a504-72c3e8dbf2a6@kernel.org> (raw)
In-Reply-To: <20260723084946.189392-2-yuan1.liu@intel.com>

On 7/23/26 10:49, Yuan Liu wrote:
> 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 == pages_with_online_memmap
> 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 == 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=hotmem0,size=256G/512G,share=on
>     device_add virtio-mem-pci,id=vmem1,memdev=hotmem0,bus=port1
>     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=./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.
> 
> Reported-by: Nanhai Zou <nanhai.zou@intel.com>
> Reported-by: Chen Zhang <zhangchen.kidd@jd.com>
> Reviewed-by: Pan Deng <pan.deng@intel.com>
> Reviewed-by: Jason Zeng <jason.zeng@intel.com>
> Co-developed-by: Tianyou Li <tianyou.li@intel.com>
> Signed-off-by: Tianyou Li <tianyou.li@intel.com>
> Signed-off-by: Yuan Liu <yuan1.liu@intel.com>
> ---
>  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/physical_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()``.
>  
> +``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`` == ``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.

It's suboptimal that we repeat the same comment that we already have in struct
zone. Can we just keep it vry simple here?

"Pages within the zone that have an online memory map: present pages and memory
holes whose memory map has been initialized. See XXX for more details."

> +
>  ``present_early_pages``
>    The present pages existing within the zone located on memory available since
>    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 = mem->altmap->free;
>  
>  	mem_hotplug_begin();
> +	clear_zone_contiguous(zone);
>  	if (nr_vmemmap_pages) {
>  		ret = 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)
>  
>  	mem->zone = zone;
>  out:
> +	set_zone_contiguous(zone);
>  	mem_hotplug_done();
>  	return ret;
>  }
> @@ -282,6 +284,7 @@ static int memory_block_offline(struct memory_block *mem)
>  	unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
>  	unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
>  	unsigned long nr_vmemmap_pages = 0;
> +	struct zone *zone;

Why the temporary variable, and why not initialize it directly here? Note that

>  	int ret;
>  
>  	if (!mem->zone)

We already use mem->zone here. So if you add a variable, convert that one as
well. But I guess we can just life without one.

> @@ -294,7 +297,9 @@ static int memory_block_offline(struct memory_block *mem)
>  	if (mem->altmap)
>  		nr_vmemmap_pages = mem->altmap->free;
>  
> +	zone = 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 *mem)
>  
>  	mem->zone = NULL;
>  out:
> +	set_zone_contiguous(zone);
>  	mem_hotplug_done();
>  	return ret;
>  }

[...]

> +static inline void set_zone_contiguous(struct zone *zone)
> +{
> +	if (zone_is_zone_device(zone))
> +		return;
> +	if (zone->spanned_pages == zone->pages_with_online_memmap)
> +		zone->contiguous = true;

Maybe it was already discussed (and I recall that we previously had that), but I
think we really need READ_ONCE semantics here and WRITE_ONCE semantics in memory
hotplug code. Otherwise concurrent updates could lead to weird things when the
compiler does load-tearing.


[...]

>  
> +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 = zone->zone_start_pfn;
> +	unsigned long zone_end_pfn = zone_start_pfn + zone->spanned_pages;

These two can be const.

> +	unsigned long sub_start, sub_end;
> +
> +	sub_start = max(ALIGN_DOWN(start_pfn, PAGES_PER_SUBSECTION),
> +			zone_start_pfn);
> +	sub_end = min(ALIGN(end_pfn, PAGES_PER_SUBSECTION), zone_end_pfn);

Hm, I don't immediately understand why we do the PAGES_PER_SUBSECTION thing
here. Why is that required?
-- 
Cheers,

David

  reply	other threads:[~2026-08-05 11:53 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-23  8:49 [PATCH v6 0/2] mm/memory_hotplug: optimize zone contiguous check when changing pfn range Yuan Liu
2026-07-23  8:49 ` [PATCH v6 1/2] " Yuan Liu
2026-08-05 11:53   ` David Hildenbrand (Arm) [this message]
2026-08-06  7:23     ` Liu, Yuan1
2026-08-06  8:45       ` David Hildenbrand (Arm)
2026-08-06  9:52         ` Liu, Yuan1
2026-07-23  8:49 ` [PATCH v6 2/2] mm/memory_hotplug: improve shrink_zone_span() subsection boundary checks Yuan Liu
2026-07-25  2:49   ` Wei Yang
2026-07-27  9:49     ` Liu, Yuan1
2026-07-30  2:36       ` Wei Yang
2026-07-30  7:57         ` Liu, Yuan1
2026-08-01  0:59           ` Wei Yang
2026-08-05 11:02   ` David Hildenbrand (Arm)
2026-08-06  7:14     ` Liu, Yuan1
2026-08-05  9:40 ` [PATCH v6 0/2] mm/memory_hotplug: optimize zone contiguous check when changing pfn range Liu, Yuan1

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a15107de-ab16-433a-a504-72c3e8dbf2a6@kernel.org \
    --to=david@kernel.org \
    --cc=jason.zeng@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=nanhai.zou@intel.com \
    --cc=osalvador@suse.de \
    --cc=pan.deng@intel.com \
    --cc=richard.weiyang@gmail.com \
    --cc=rppt@kernel.org \
    --cc=tianyou.li@intel.com \
    --cc=yuan1.liu@intel.com \
    --cc=zhangchen.kidd@jd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox