From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795551922; b=V0ODBMfe1kH/DiCXIku4pby4Hdj3g3mm2Ccwj2zoZE3mz7/+P0trtHLeQrCpLDDeeOzU eN7tnD0MKpSfKLkypTxuuTGjAItk0ikC6CfIYRYm/iob+S2dkc9P3FVn5h9K9GT/UxIxY Boomj/qPXdpYqHoNmT/VBUBWtqGhoA2Uy3+MUXgaUJdu/tcbvSaoBbk8xKBLg88JfWTqh xzIdepSIDBnL4AuLXKfLJ97T3idrq6BsZHwhebKg7sfWv2TAA28CbvrFWYQPHXVGX6DC4 NsVtYB4XGf+Locj5+KQ8elKz/iJJD2fR5bndiQpB4DsdELHKzJqs9CC/0eBlETbs6ag== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795551922; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=fPLQkZx0UkOeZpfIkwXtH1+egwKPbPaHWZ0gF6cwHKo=; b=Ex4Oeo9Ct38x1a1bhMXFySRdMBGix/OmUqEt0ExIZvvfpkL7s9vAdYAN/AN53GSULQJi aHiQLfRRlfdHUao7KLe7crL2ghmJeM/El2lzDVyj0eoHnQBgg8EQdIiK6Z820F6m5dTBh JVh2NWf5sp5rg72PDLEBkQa7nZnxbj7m3onPaAknpfm8T/GNMJED/shJC0pTFQYIh2r0j dBWVc526tiHJNWnKqHJcNpg7A58JP2GMDVlra7lS2CoutM4psI8weB5vkQFLnpoZUP3Ov ACkvUFJWvx/6lF5nYlMrdF/IQPHpNc2WzDns5GITcsHIycqONy3PstL5FzWKA9L2NTg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=RRSMm0c8; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 877CE606A31E for ; Tue, 24 Nov 2026 20:25:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=RRSMm0c8 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 083E21F000CA; Tue, 24 Nov 2026 20:25:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795551921; bh=fPLQkZx0UkOeZpfIkwXtH1+egwKPbPaHWZ0gF6cwHKo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=RRSMm0c8xBkAaFHQ+PelSU0lZL7TKz7Dnsg8cFiYmGGvH0a7LpiNxHAkjtYaO70Jk jyVSIab66yh3cdH4thNyRHOs0bEfdU+5QcOApLy2N+UGl37Ksk+xg1VgJAvOetNtFf rwVTEQi9nCTBuYjTIjj+nNuHB5Fhkrlj6J6eO9Zd/1SorRldp1M/tmAh9Wj68LWju8 6P3xzqljcgvI/A7onmVlOO7J5RrG9Vxbde5dynRQ5eElxsk+5ZJJfuYW15c/jd8t7Q WN+N19w3ewi4YMzCDwtBhTzBW8GJk4NK35FfTG0XSZMSKqirE1Dg15lCLTVkEjza0c e6/zECLahnlkQ== From: Sasha Levin To: stable@vger.kernel.org Cc: "Gregory Price (Meta)" , "Vlastimil Babka (SUSE)" , Zi Yan , Sasha Levin Subject: [PATCH 6.12.y] mm/page_alloc: snapshot the preferred zone against shrink rebuilds Date: Tue, 24 Nov 2026 15:25:19 -0500 Message-ID: <20261124202455.2582658-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026112434-gift-faceted-c537@gregkh> References: <2026112434-gift-faceted-c537@gregkh> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: "Gregory Price (Meta)" [ Upstream commit b3e3a760eb68449e2794ef68ab212173d9c3e8c7 ] get_page_from_freelist() and alloc_pages_bulk_noprof() reread ac->preferred_zoneref->zone at every use. Offlining a zone's last block rebuilds the zonelists in place, and the shrunk list's NULL terminator can land on the preferred slot mid-walk: CPU 0 CPU 1 get_page_from_freelist(GFP_KERNEL | __GFP_THISNODE) zone = NORMAL (slot 1) offline_pages() build_all_zonelists() [MOVABLE, NORMAL, NULL] -> [NORMAL, NULL] rmqueue(zonelist_zone(slot 1), zone) zone_statistics(NULL, zone) BUG: kernel NULL pointer dereference, address: 0000000000000060 RIP: 0010:get_page_from_freelist+0x6a5/0x15f0 Call Trace: __alloc_frozen_pages_noprof+0x125/0x330 Read the slot once. Zones are never freed, so a stale zone is safe, and a NULL slot fails the same way as walking off the end. Fixes: 11cd8638c37f ("mm, page_alloc: remove stop_machine from build_all_zonelists") Cc: stable@vger.kernel.org Signed-off-by: Gregory Price (Meta) Reviewed-by: Vlastimil Babka (SUSE) Link: https://patch.msgid.link/20261101021340.2251928-7-gourry@gourry.net Acked-by: Zi Yan Signed-off-by: Vlastimil Babka (SUSE) [ sashal: Dropped the !defrag_mode condition from the NOFRAGMENT locality check since this tree lacks defrag_mode, keeping the preferred_zone replacement. ] Signed-off-by: Sasha Levin --- mm/page_alloc.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index e9aadaf6e91b6..dc4b0d58c4d0d 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -3424,10 +3424,19 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags, { struct zoneref *z; struct zone *zone; + struct zone *preferred_zone; struct pglist_data *last_pgdat = NULL; bool last_pgdat_dirty_ok = false; bool no_fallback; + /* + * A concurrent zonelist rebuild can NULL or replace this slot. Zones + * are never freed, so reading it once keeps a usable zone. + */ + preferred_zone = zonelist_zone(ac->preferred_zoneref); + if (!preferred_zone) + return NULL; + retry: /* * Scan zonelist, looking for a zone with enough free. @@ -3474,7 +3483,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags, } if (no_fallback && nr_online_nodes > 1 && - zone != zonelist_zone(ac->preferred_zoneref)) { + zone != preferred_zone) { int local_nid; /* @@ -3482,7 +3491,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags, * fragmenting fallbacks. Locality is more important * than fragmentation avoidance. */ - local_nid = zonelist_node_idx(ac->preferred_zoneref); + local_nid = zone_to_nid(preferred_zone); if (zone_to_nid(zone) != local_nid) { alloc_flags &= ~ALLOC_NOFRAGMENT; goto retry; @@ -3533,7 +3542,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags, goto try_this_zone; if (!node_reclaim_enabled() || - !zone_allows_reclaim(zonelist_zone(ac->preferred_zoneref), zone)) + !zone_allows_reclaim(preferred_zone, zone)) continue; ret = node_reclaim(zone->zone_pgdat, gfp_mask, order); @@ -3555,7 +3564,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags, } try_this_zone: - page = rmqueue(zonelist_zone(ac->preferred_zoneref), zone, order, + page = rmqueue(preferred_zone, zone, order, gfp_mask, alloc_flags, ac->migratetype); if (page) { prep_new_page(page, order, gfp_mask, alloc_flags); @@ -4678,6 +4687,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid, struct page *page; unsigned long __maybe_unused UP_flags; struct zone *zone; + struct zone *preferred_zone; struct zoneref *z; struct per_cpu_pages *pcp; struct list_head *pcp_list; @@ -4728,6 +4738,11 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid, goto out; gfp = alloc_gfp; + /* Read once; see get_page_from_freelist(). */ + preferred_zone = zonelist_zone(ac.preferred_zoneref); + if (!preferred_zone) + goto failed; + /* Find an allowed local zone that meets the low watermark. */ z = ac.preferred_zoneref; for_next_zone_zonelist_nodemask(zone, z, ac.highest_zoneidx, ac.nodemask) { @@ -4738,8 +4753,8 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid, continue; } - if (nr_online_nodes > 1 && zone != zonelist_zone(ac.preferred_zoneref) && - zone_to_nid(zone) != zonelist_node_idx(ac.preferred_zoneref)) { + if (nr_online_nodes > 1 && zone != preferred_zone && + zone_to_nid(zone) != zone_to_nid(preferred_zone)) { goto failed; } @@ -4747,7 +4762,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid, retry_this_zone: mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK) + nr_pages; if (zone_watermark_fast(zone, 0, mark, - zonelist_zone_idx(ac.preferred_zoneref), + zone_idx(preferred_zone), alloc_flags, gfp)) { break; } @@ -4809,7 +4824,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid, pcp_trylock_finish(UP_flags); __count_zid_vm_events(PGALLOC, zone_idx(zone), nr_account); - zone_statistics(zonelist_zone(ac.preferred_zoneref), zone, nr_account); + zone_statistics(preferred_zone, zone, nr_account); out: return nr_populated; -- 2.53.0