From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.14]) (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 52D963C8731 for ; Wed, 20 May 2026 09:35:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.14 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779269737; cv=none; b=nQs58jcRB5Y7kC1uF0k4eOUa/3NBephf0XfvSZOR8EUw9UkW8Qr+Ch3O5P0dCPbwbmVulE8pJPLRJJKqYCkFSa6L7Pu8Bl+gvZ7pUqyyMJA0/tQRy17NxKsBfD9EOdLwaIMum7B/gLHZCPQjjfcyOna3O5guqHzqBmw8i8+BZ1E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779269737; c=relaxed/simple; bh=XavP1vUJllc7NA9zCR5LarxqZxkiJyUBCYCui0GExNc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mUdlxqDd9YeHZj5gYWzhRXVKtjIYYt2W3AJ/5AIDsYax7OSNIAnHLVF/Dj9Y4V7KXg85zUEUe45Z6VJxhQNCdFBIoJsAHgEe/yFhkQdVH1GpEaG27GqfyLfnF24tEJbvb0vi3gfrJj+evQWcDvPXRsv2WdE/jZ8fr/FoSx3MwQg= 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=cteyFSB2; arc=none smtp.client-ip=192.198.163.14 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="cteyFSB2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1779269735; x=1810805735; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=XavP1vUJllc7NA9zCR5LarxqZxkiJyUBCYCui0GExNc=; b=cteyFSB2mQzheUdnPvotq97eYX045WCL081VHTxxLL8TSsxx+mdU7weq F3W56Kue8Sfwk8tbSsw6GE6TbqgahO2ItYHZiR8u0qnK2bC9KfSueVNHT 34clyOvCZjaVwS7LEy5J0JhJHTov8YchEccKSDYuN2SJCryletl86zeEX 6VOMwxWWuxfXP7BRbEqFTLMEAAlk9T4tfaJfEcELW9IDJDEBIykh7WL66 eLwJ4DSEg0tSwOwkHuOSI2mbqMh9TMhQUS59/Mnpr0ffRhtqZNPuZDYE3 X91P3cXDRme9RQ98rWknDrlqYFvVn8WWw2oXx8ERHgUTmDTBusfPiM8OU w==; X-CSE-ConnectionGUID: lmkmFEpkSCiTMXeVceGOdA== X-CSE-MsgGUID: CVjqcBzuT4OZusOOSpMFzg== X-IronPort-AV: E=McAfee;i="6800,10657,11791"; a="80196886" X-IronPort-AV: E=Sophos;i="6.23,244,1770624000"; d="scan'208";a="80196886" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by fmvoesa108.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 May 2026 02:35:35 -0700 X-CSE-ConnectionGUID: Vm3WiHyLQSmXacs/f4mnbQ== X-CSE-MsgGUID: RAgbtuazSsGhzDvr0km2mg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.23,244,1770624000"; d="scan'208";a="235672601" Received: from spr10.sh.intel.com (HELO localhost) ([10.239.23.75]) by fmviesa006.fm.intel.com with ESMTP; 20 May 2026 02:35:31 -0700 From: Yuan Liu To: David Hildenbrand , Oscar Salvador , Mike Rapoport , Wei Yang Cc: linux-mm@kvack.org, Yong Hu , Nanhai Zou , Yuan Liu , Tim Chen , Qiuxu Zhuo , Yu C Chen , Pan Deng , Tianyou Li , Chen Zhang , Jason Zeng , linux-kernel@vger.kernel.org Subject: [PATCH v5 5/5] mm/memory_hotplug: improve shrink_zone_span() subsection boundary checks Date: Wed, 20 May 2026 05:34:57 -0400 Message-ID: <20260520093457.3719960-6-yuan1.liu@intel.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260520093457.3719960-1-yuan1.liu@intel.com> References: <20260520093457.3719960-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 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: Wei Yang Reviewed-by: Jason Zeng Signed-off-by: Yuan Liu --- 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 fbe863441761..20b61f70cd81 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; -- 2.47.3