The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Wei Yang <richard.weiyang@gmail.com>
Cc: Yuan Liu <yuan1.liu@intel.com>,
	Oscar Salvador <osalvador@suse.de>,
	Mike Rapoport <rppt@kernel.org>,
	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 2/2] mm/memory_hotplug: improve shrink_zone_span() subsection boundary checks
Date: Fri, 7 Aug 2026 13:25:20 +0200	[thread overview]
Message-ID: <c8c81d9f-347d-4d9f-a9e2-e03c3122ef15@kernel.org> (raw)
In-Reply-To: <20260807032205.ykhxbapx6nhg5hpt@master>

On 8/7/26 05:22, Wei Yang wrote:
> On Wed, Aug 05, 2026 at 01:02:29PM +0200, David Hildenbrand (Arm) wrote:
>> On 7/23/26 10:49, Yuan Liu wrote:
>>> 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 <jason.zeng@intel.com>
>>> Signed-off-by: Yuan Liu <yuan1.liu@intel.com>
>>> ---
>>>  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 nid, struct zone *zone,
>>>  				     unsigned long start_pfn,
>>>  				     unsigned long end_pfn)
>>>  {
>>> -	for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SUBSECTION) {
>>> -		if (unlikely(!pfn_to_online_page(start_pfn)))
>>> -			continue;
>>> +	unsigned long next_pfn;
>>>  
>>> -		if (unlikely(pfn_to_nid(start_pfn) != nid))
>>> -			continue;
>>> +	for (; start_pfn < end_pfn; start_pfn = next_pfn) {
>>> +		unsigned long tail_pfn;
>>>  
>>> -		if (zone != page_zone(pfn_to_page(start_pfn)))
>>> +		next_pfn = start_pfn + PAGES_PER_SUBSECTION;
>>> +		tail_pfn = next_pfn - 1;
>>> +
>>> +		if (unlikely(!pfn_to_online_page(start_pfn)))
>>>  			continue;
>>>  
>>> -		return start_pfn;
>>> +		if (likely(pfn_to_nid(start_pfn) == nid) &&
>>> +		    zone == page_zone(pfn_to_page(start_pfn)))
>>> +			return start_pfn;
>>> +
>>> +		if (likely(pfn_to_nid(tail_pfn) == nid) &&
>>> +		    zone == page_zone(pfn_to_page(tail_pfn)))
>>> +			return start_pfn;
>>>  	}
>>>  
>>>  	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;
>>>  
>>>  	/* pfn is the end pfn of a memory section. */
>>>  	pfn = end_pfn - 1;
>>> -	for (; pfn >= start_pfn; pfn -= PAGES_PER_SUBSECTION) {
>>> -		if (unlikely(!pfn_to_online_page(pfn)))
>>> -			continue;
>>> +	for (; pfn >= start_pfn; pfn = prev_pfn) {
>>> +		unsigned long head_pfn;
>>>  
>>> -		if (unlikely(pfn_to_nid(pfn) != nid))
>>> -			continue;
>>> +		prev_pfn = pfn - PAGES_PER_SUBSECTION;
>>> +		head_pfn = prev_pfn + 1;
>>>  
>>> -		if (zone != page_zone(pfn_to_page(pfn)))
>>> +		if (unlikely(!pfn_to_online_page(pfn)))
>>>  			continue;
>>>  
>>> -		return pfn;
>>> +		if (likely(pfn_to_nid(pfn) == nid) &&
>>> +		    zone == page_zone(pfn_to_page(pfn)))
>>> +			return pfn;
>>> +
>>> +		if (likely(pfn_to_nid(head_pfn) == nid) &&
>>> +		    zone == page_zone(pfn_to_page(head_pfn)))
>>> +			return pfn;
>>>  	}
>>>  
>>>  	return 0;
>>
>>
>> I think improving that should be patch #1.
>>
>> But I think we can do much better code-wise. What about the following
>> cleanup instead:
>>
>>
>> >From 6e9ea094daf8d04c0b119b780572a846334f8164 Mon Sep 17 00:00:00 2001
>> From: "David Hildenbrand (Arm)" <david@kernel.org>
>> Date: Wed, 5 Aug 2026 12:52:55 +0200
>> Subject: [PATCH] mm/memory_hotplug: make shrink_zone_span() more robust
>>
>> Let's make shrink_zone_span() more robust by checking in
>> find_smallest_section_pfn() / find_biggest_section_pfn() that start+end
>> of the subsection.
>>
>> While at it, clean up the function heavily, factoring the core check
>> out into subsection_overlaps_zone().
>>
>> There likely is no need to check for the nid first: we require
>> SPARSEMEM_VMEMMAP_ENABLE where pfn_to_page() is cheap, and the
>> pfn_to_nid() on CONFIG_NUMA would do a pfn_to_page() either way. So
>> let's just drop that for now.
>>
>> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
>> ---
>> 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 226ab9cb078ad..79937f9672d01 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;
>> }
>>
>> -/* 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 += PAGES_PER_SUBSECTION) {
>> -		if (unlikely(!pfn_to_online_page(start_pfn)))
>> -			continue;
>> +	const unsigned long start_pfn = ALIGN_DOWN(pfn, PAGES_PER_SUBSECTION);
>> +	const unsigned long end_pfn = start_pfn + PAGES_PER_SUBSECTION - 1;
>>
>> -		if (unlikely(pfn_to_nid(start_pfn) != nid))
>> -			continue;
>> +	/* All pages in a subsection are either online or offline. */
>> +	if (unlikely(!pfn_to_online_page(start_pfn)))
>> +		return false;
>>
>> -		if (zone != page_zone(pfn_to_page(start_pfn)))
>> -			continue;
>> +	/* Checking start+end is sufficient. */
>> +	return zone == page_zone(pfn_to_page(start_pfn)) ||
>> +	       zone == page_zone(pfn_to_page(end_pfn));
>> +}
> 
> Hi, David

Hi,

> 
> I am thinking if we could initialize each subsection to only one zone, we may
> reduce the complexity?
> 
> For example, in init_unavailable_range() align spfn to subsection and init the
> head unaligned part to previous zone?
> 
> Do you think this is reasonable?

I'm afraid of opening up another can of worms. :)

The additional page_zone(pfn_to_page(end_pfn) check is cheap with VMEMMAP, so I
think we should focus on that first.

(and the patch is in general a cleanup :) )

-- 
Cheers,

David

  reply	other threads:[~2026-08-07 11:25 UTC|newest]

Thread overview: 19+ 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)
2026-08-06  7:23     ` Liu, Yuan1
2026-08-06  8:45       ` David Hildenbrand (Arm)
2026-08-06  9:52         ` Liu, Yuan1
2026-08-07 11:29           ` David Hildenbrand (Arm)
2026-08-07 12:15             ` 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-07  3:22     ` Wei Yang
2026-08-07 11:25       ` David Hildenbrand (Arm) [this message]
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=c8c81d9f-347d-4d9f-a9e2-e03c3122ef15@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