From mboxrd@z Thu Jan 1 00:00:00 1970 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: 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 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; } -/* 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; } @@ -475,7 +465,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) { /* @@ -484,7 +473,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; @@ -500,7 +489,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