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=1795947234; b=C1GqPxOM0fNnuLdCw7jWxaeFnEcnUqxYHcOLMwjJmzy5qUavzsvC78oCIP3dn4y4W8tR 51GJ9IL+5lePuMFa6MTSarr9AsosKelOjZcznCMoFkDQx2zJiPmPqIcVQNtqhfMGgwp1R M2Yu9FJIXttZ/APP7ab55V61B+Vj8tBNXrBrgcAGYH78lyC+Ae6gqg178GphAR41UYwBc 9Kr7/Z9QxS+ex3h2fCq+saJO5OqbBW/l5y+euLztRn7OccnAxvIH0vOA455xzuYDwRXrO vV6KJTrGWeh0uqtxOVL5KQ9GsKMziq1taNA3S63nJMD9hk3wJSS18w3JQmWQnZfNE/A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795947234; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=ODk8dcXrxQGUan4M7eMjOUfEwlPPTn9p2h8blx9eHT4=; b=dCElK/KvdU5/yT0yThXKOKCMj/ozUIzVey2cRrVxglZdd8yHliCbdaAtDAkqw0Cx0bKx ilWGX92q54tcfh1b+zZnonT575G5QBXNM366oFOX//MmwOry3gW6x/P+p9AWmK9+GKACK qb2xltO8+ySotRQmghSbgAvCxuqb0n0F4g0U6WTtlihhpc+P8SV61xiYXvdV/3uDx4Mj5 bx11m1wUXYVZ4kJ5S+eypu/zzksxzFkyi/qT1OJNFpNPtSMnxDCHMp16FgvES5UYMhMQx JReb0EkMIqDzXLOw3ZV44amTq09PjizWOrdAqgAUATEOqECxm97XKxoecdwmJWJptZQ== 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=QxWd8Yqc; 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 5ABEE606A325; Sun, 29 Nov 2026 10:13:54 +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=QxWd8Yqc Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4A4161F000C1; Sun, 29 Nov 2026 10:13:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795947233; bh=ODk8dcXrxQGUan4M7eMjOUfEwlPPTn9p2h8blx9eHT4=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=QxWd8Yqc3je0PzypGQNa0YBDiVMUtYNI2dGPQ9EEIFlIUW2fJzeXWHKFfyJpN1zbK 1S1QxKgjdxrEOiyDJXomhlP/cXt49gX5WmAc05i7NJIpk264kQyw3uGOfL3XwVeVfg TYDTALC9XDrnWS73/9+1Rp5ROZGKq59ZciDLIrn+vA1rHRI6eWJtasynzWP1eu3JGY HZNI5OCH3uPp2ejp41ZDGH9GbtDEWRwwX4saOrEn1v9/sXx2MGz/dUjATE5vlQ0sS/ XW7wDLpsjVbYRNte1dOZer5n1GmiRLK8LhzC1Muhun1w/tkHa57jgJQwfJTnINlBBQ sugNc87LLMetA== From: "David Hildenbrand (Arm)" Date: Sun, 29 Nov 2026 11:06:53 +0100 Subject: [PATCH 22/32] mm: taint device sections before setting subsections valid X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261129-fixes_core_3-v1-22-f66094116801@kernel.org> References: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> In-Reply-To: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> To: Hugh Dickins , Baolin Wang , Andrew Morton , Mike Rapoport , Peter Xu , Michal Hocko , Kiryl Shutsemau , Manfred Spraul , Davidlohr Bueso , Cyrill Gorcunov , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Lorenzo Stoakes , Rik van Riel , Vlastimil Babka , Harry Yoo , Jann Horn , Yang Shi , Zach O'Keefe , Song Liu , David Stevens , Kefeng Wang , Alex Williamson , Suren Baghdasaryan , Dave Jiang , Matthew Wilcox , Miaohe Lin , David Rientjes , Shakeel Butt , Jan Kara , Mel Gorman , Jason Gunthorpe , John Hubbard , Gerd Hoffmann , Vivek Kasireddy , Dave Airlie , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Hagen Paul Pfeifer , James Bottomley , Felix Kuehling , Alex Sierra , Balbir Singh , Kairui Song , Qi Zheng , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Oscar Salvador , Muchun Song , Dan Williams , Rakie Kim , Hao Li , Christoph Lameter , Roman Gushchin , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Alexei Starovoitov , Xu Xin , Chengming Zhou , Shivank Garg Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-cxl@vger.kernel.org, linux-rt-devel@lists.linux.dev, SJ Park , "David Hildenbrand (Arm)" , stable@vger.kernel.org, Chris Mason X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5995; i=david@kernel.org; h=from:subject:message-id; bh=mRlZsNaB7GhNH3hmDRgaFMHfryjTJWgnRm9cfsPwrBQ=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLK5f+oW7xXbsOvSVK+6oHf7ePelX/6cLa8nzGRV9GJ3e GzGB45fHaUsDGIcDLJiiixBwfcNXr5cV1vtxscMM4eVCWQIAxenAEykU4+RYV3wCzuzzpNLpvjy f+xRfhC34sWJvWyck3IvnPNPvvdtLQsjw+oDfp7xOzqZPjI/SFJMSjfPM+Eo7L7xQM954cfetYa 1vAA= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A Adding ZONE_DEVICE memory to an online section can make the new subsections valid before the section is tainted. Consequently, a concurrent pfn_to_online_page() can return an uninitialized device page, triggering a BUG with CONFIG_DEBUG_VM. Let's fix it by tainting the section in fill_subsection_map() before setting the validity bits. Pair the write barrier with test_bit_acquire() in pfn_to_online_page() by introducing a new pfn_section_valid_acquire(). This has low urgency, as it requires racing with hotplug of ZONE_DEVICE memory, which is privileged. Fixes: 1f90a3477df3 ("mm: teach pfn_to_online_page() about ZONE_DEVICE section collisions") Cc: stable@vger.kernel.org Reported-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) --- include/linux/mmzone.h | 9 +++++++++ mm/memory_hotplug.c | 34 +++++++--------------------------- mm/sparse-vmemmap.c | 39 +++++++++++++++++++++++++++++---------- 3 files changed, 45 insertions(+), 37 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 6442e662aa4f4..8cbe7e28040a5 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -2235,6 +2235,15 @@ static inline int pfn_section_valid(struct mem_section *ms, unsigned long pfn) return usage ? test_bit(idx, usage->subsection_map) : 0; } +static inline int pfn_section_valid_acquire(struct mem_section *ms, + unsigned long pfn) +{ + const int idx = subsection_map_index(pfn); + struct mem_section_usage *usage = READ_ONCE(ms->usage); + + return usage ? test_bit_acquire(idx, usage->subsection_map) : 0; +} + static inline bool pfn_section_first_valid(struct mem_section *ms, unsigned long *pfn) { struct mem_section_usage *usage = READ_ONCE(ms->usage); diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 41fcdc04e5404..21e241823e22d 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -353,7 +353,13 @@ struct page *pfn_to_online_page(unsigned long pfn) if (!online_section(ms)) return NULL; - if (!pfn_section_valid(ms, pfn)) + /* + * ZONE_DEVICE subsections in an online section can only become valid + * after the corresponding pgmap was registered and the sections were + * tainted. Look at the subsection first. The acquire pairs with + * smp_wmb() in section_taint_zone_device(). + */ + if (!pfn_section_valid_acquire(ms, pfn)) return NULL; if (!online_device_section(ms)) @@ -713,19 +719,6 @@ static void __meminit resize_pgdat_range(struct pglist_data *pgdat, unsigned lon } -#ifdef CONFIG_ZONE_DEVICE -static void section_taint_zone_device(unsigned long pfn) -{ - struct mem_section *ms = __pfn_to_section(pfn); - - ms->section_mem_map |= SECTION_TAINT_ZONE_DEVICE; -} -#else -static inline void section_taint_zone_device(unsigned long pfn) -{ -} -#endif - /* * Associate the pfn range with the given zone, initializing the memmaps * and resizing the pgdat/zone data to span the added pages. After this @@ -748,19 +741,6 @@ void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, resize_zone_range(zone, start_pfn, nr_pages); resize_pgdat_range(pgdat, start_pfn, nr_pages); - /* - * Subsection population requires care in pfn_to_online_page(). - * Set the taint to enable the slow path detection of - * ZONE_DEVICE pages in an otherwise ZONE_{NORMAL,MOVABLE} - * section. - */ - if (zone_is_zone_device(zone)) { - if (!IS_ALIGNED(start_pfn, PAGES_PER_SECTION)) - section_taint_zone_device(start_pfn); - if (!IS_ALIGNED(start_pfn + nr_pages, PAGES_PER_SECTION)) - section_taint_zone_device(start_pfn + nr_pages); - } - /* * TODO now we have a visible range of pages which are not associated * with their zone properly. Not nice but set_pfnblock_migratetype() diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c index 5d7e982f94d0a..643d9d1bfa39f 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -629,26 +629,45 @@ static bool is_subsection_map_empty(struct mem_section *ms) SUBSECTIONS_PER_SECTION); } -static int fill_subsection_map(unsigned long pfn, unsigned long nr_pages) +#ifdef CONFIG_ZONE_DEVICE +static void section_taint_zone_device(struct mem_section *ms, + struct dev_pagemap *pgmap, unsigned long nr_pages) +{ + if (!pgmap || nr_pages == PAGES_PER_SECTION) + return; + + ms->section_mem_map |= SECTION_TAINT_ZONE_DEVICE; + /* Pairs with pfn_section_valid_acquire() in pfn_to_online_page(). */ + smp_wmb(); +} +#else +static void section_taint_zone_device(struct mem_section *ms, + struct dev_pagemap *pgmap, unsigned long nr_pages) +{ +} +#endif + +static int fill_subsection_map(unsigned long pfn, unsigned long nr_pages, + struct dev_pagemap *pgmap) { struct mem_section *ms = __pfn_to_section(pfn); DECLARE_BITMAP(map, SUBSECTIONS_PER_SECTION) = { 0 }; unsigned long *subsection_map; - int rc = 0; subsection_mask_set(map, pfn, nr_pages); subsection_map = &ms->usage->subsection_map[0]; if (bitmap_empty(map, SUBSECTIONS_PER_SECTION)) - rc = -EINVAL; - else if (bitmap_intersects(map, subsection_map, SUBSECTIONS_PER_SECTION)) - rc = -EEXIST; - else - bitmap_or(subsection_map, map, subsection_map, - SUBSECTIONS_PER_SECTION); + return -EINVAL; + if (bitmap_intersects(map, subsection_map, SUBSECTIONS_PER_SECTION)) + return -EEXIST; + + /* Taint the sections before marking the subsection map valid. */ + section_taint_zone_device(ms, pgmap, nr_pages); + bitmap_or(subsection_map, map, subsection_map, SUBSECTIONS_PER_SECTION); - return rc; + return 0; } /* @@ -733,7 +752,7 @@ static struct page * __meminit section_activate(int nid, unsigned long pfn, ms->usage = usage; } - rc = fill_subsection_map(pfn, nr_pages); + rc = fill_subsection_map(pfn, nr_pages, pgmap); if (rc) { if (usage) ms->usage = NULL; -- 2.56.1