From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.20]) (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 9208943B4B5 for ; Tue, 18 Aug 2026 08:53:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.20 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787043207; cv=none; b=uM7FfpA9xXbBHuv2N74la4/A7zM3nCfq7dbNZxptxCQM6Zbz4ROqzG82GWY6cPidsXv37F0NZru/7g2hctZsCxU4DyAPtw/SjCtZcjujmQ2vx0ZAAKmYgC1z9o2uQHGO8UCwuG2tvbYXgj7Z/4vNIauTkERTzB8lo6NXmTiV5UY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787043207; c=relaxed/simple; bh=49j4fxstgTRL5iumzWsULPLrhk82aBqfzo4vGvGaiyg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NDvxtUMV3hmhL1y3SZ3XTXvQQe2AbsZ3zBkGIGGx8Yo/FcGkZu/e8quQSjRI59wddn5wZjHHVljokMJbNi5IXj90d56tIt9BSb4eNDQ4ahHEl6wAENkZ6GsMzvBcR8ZrH9iskNh0IjEWxPrhJYTk51AIVTrlOxXL8GLxOq4P224= 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=FQOm7/4W; arc=none smtp.client-ip=198.175.65.20 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="FQOm7/4W" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1787043205; x=1818579205; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=49j4fxstgTRL5iumzWsULPLrhk82aBqfzo4vGvGaiyg=; b=FQOm7/4WtF7OtFG5hlhhD6YGOWonPoKC/4ur/Lp3lOsVv7uLud1v56NM sdrrVDf9fUCKLUom5+TSlqe7Iv/2Fv71cwFcdcJU9Yrfqyi2xbN+bMywF qiQ8hBGbcSB61VtkqecXqnlR97uTBTbR6SUhs+2AD0kd68rv2mwiaFSp6 dt+8/heXapKFUyNVAr+Hiy8G2rhqoic9kxdOQZ1aSoO4sNEJNJGiYHnQM 4FPcM8AP2yLhAnI8oQnLLIXZGxMrtUxgH5U43oFrHRZ8ooZCi6JluJCkI ay9OTb4e/W+6hRyT7pYu766hV1raBzMPwqlkB0Zr/k+g1KtZ9VpcK9li5 Q==; X-CSE-ConnectionGUID: ap4oNmgwRSacj0fgs+v/vw== X-CSE-MsgGUID: HElocHNHQPikPL0Tqwz6wA== X-IronPort-AV: E=McAfee;i="6800,10657,11878"; a="87292221" X-IronPort-AV: E=Sophos;i="6.25,230,1779174000"; d="scan'208";a="87292221" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa112.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Aug 2026 01:53:25 -0700 X-CSE-ConnectionGUID: DTsgAjWrQmG36uO08YDwbA== X-CSE-MsgGUID: F31xNNF9Sxq373JezwsqPA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,230,1779174000"; d="scan'208";a="262539338" Received: from spr10.sh.intel.com (HELO localhost) ([10.239.23.75]) by fmviesa008.fm.intel.com with ESMTP; 18 Aug 2026 01:53: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 v7 1/2] mm/memory_hotplug: make shrink_zone_span() more robust Date: Tue, 18 Aug 2026 04:57:01 -0400 Message-ID: <20260818085702.3395529-2-yuan1.liu@intel.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260818085702.3395529-1-yuan1.liu@intel.com> References: <20260818085702.3395529-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: 8bit 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 --- 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 7ac19fab2263..cd82e79f0782 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -422,49 +422,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)); +} - 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 += PAGES_PER_SUBSECTION) { + if (subsection_overlaps_zone(start_pfn, zone)) + return start_pfn; } - return 0; } /* 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 = end_pfn - 1; - for (; pfn >= start_pfn; pfn -= PAGES_PER_SUBSECTION) { - if (unlikely(!pfn_to_online_page(pfn))) - continue; - - if (unlikely(pfn_to_nid(pfn) != nid)) - continue; - - if (zone != page_zone(pfn_to_page(pfn))) - continue; - - return pfn; + for (; end_pfn >= start_pfn; end_pfn -= PAGES_PER_SUBSECTION) { + if (subsection_overlaps_zone(end_pfn - 1, zone)) + return end_pfn - 1; } - return 0; } @@ -472,7 +462,6 @@ static void shrink_zone_span(struct zone *zone, unsigned long start_pfn, unsigned long end_pfn) { unsigned long pfn; - int nid = zone_to_nid(zone); if (zone->zone_start_pfn == start_pfn) { /* @@ -481,7 +470,7 @@ static void shrink_zone_span(struct zone *zone, unsigned long start_pfn, * In this case, we find second smallest valid mem_section * for shrinking zone. */ - pfn = find_smallest_section_pfn(nid, zone, end_pfn, + pfn = find_smallest_section_pfn(zone, end_pfn, zone_end_pfn(zone)); if (pfn) { zone->spanned_pages = zone_end_pfn(zone) - pfn; @@ -497,7 +486,7 @@ static void shrink_zone_span(struct zone *zone, unsigned long start_pfn, * In this case, we find second biggest valid mem_section for * shrinking zone. */ - pfn = find_biggest_section_pfn(nid, zone, zone->zone_start_pfn, + pfn = find_biggest_section_pfn(zone, zone->zone_start_pfn, start_pfn); if (pfn) zone->spanned_pages = pfn - zone->zone_start_pfn + 1; -- 2.47.3