From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EC42D606A330 for ; Mon, 26 Oct 2026 23:27:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=Uvcmf0vT; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Wax5YaVW DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References :Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=NHUznzbhtn3AtvFPFOcCt9tvyez2ae3rJ1KObV9N9d8=; b=Uvcmf0vTBxrsnp k6x8OJamh59BcwDI0avoGnY96aMMGpnbwkqci3jXWteiqHLe2GUPw/r+ME1SFxz/zQt6Q6sJnqpdl Pj9jmfxRFaDYg8Sj2Xxr5gPmY3POHAP7k7Xwyc9r1zl/PSWxP06FgygZ5EkoRLfSHWkmGxiWFQcRz MGKAGSthL0PhSt8w6MVyx+lqi+Ba8720ufpiygHvlJBHE0Db0DLaaFzBgYVDbN0H6NyIq6pHkMJjg lXr3lgF5CFuoKRWb6IpxNa2H6ZBavLKQfEQ21M/J61LZZT3Fc87tk+y/pqijTWqTcxv0StkXwzYcO AfF5doX23W/apFm/B7kg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xLU6f-00000003RAC-1yJM; Mon, 26 Oct 2026 23:27:57 +0000 Received: from tor.source.kernel.org ([172.105.4.254]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xLU6e-00000003R9p-104H for kexec@lists.infradead.org; Mon, 26 Oct 2026 23:27:56 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id A9AB860A5F; Mon, 26 Oct 2026 23:27:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 79B271F031E1; Mon, 26 Oct 2026 23:27:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1793057275; bh=NHUznzbhtn3AtvFPFOcCt9tvyez2ae3rJ1KObV9N9d8=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=Wax5YaVWlZWtTyF1cl336nJjKdkNmG46OgD6dxRlnHVJj8Cbu/sv1aOd3OtX/XYgv aOdlKrCsUrZ98XB3SksCIIOmcUjGLEvQtGQGJVeaemTMwpsJj9ahgppeTtpHPd40F+ jvYOYs9DHDRRfJTZb+t3Ez41u+L45karzC00vwIuxjfyYyOeMH1iBD798L0fsDEMlD IiFRsjyubm5blmZwKQSUQIsHP+XrFLH1hX38WNCQ5pdkYFPW31DiHx6N+6JdLpuvKg W8Mw8HIHbW1azLcj/SFBIkJ+dLBGpd3p8aUSBTN5r89kufzYi4k3Mxxnq80QzNL3m4 WUiPj3dN6cZMA== From: "David Hildenbrand (Arm)" Date: Tue, 27 Oct 2026 00:17:52 +0100 Subject: [PATCH 32/46] mm/memory_hotplug: avoid races in pfn_to_online_page() with dying pgmap X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261027-fixes_core-v1-32-6f74bec9a5bb@kernel.org> References: <20261027-fixes_core-v1-0-6f74bec9a5bb@kernel.org> In-Reply-To: <20261027-fixes_core-v1-0-6f74bec9a5bb@kernel.org> To: Andrew Morton , Johannes Weiner , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , Kairui Song , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Baolin Wang , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Kees Cook , Peter Zijlstra , Jann Horn , Pedro Falcato , Hugh Dickins , Pasha Tatashin , Pratyush Yadav , "Matthew Wilcox (Oracle)" , Jan Kara , Zi Yan , Lance Yang , Rik van Riel , Harry Yoo , Alistair Popple , Nico Pache , Ryan Roberts , Dev Jain , Usama Arif , Kiryl Shutsemau , Matthew Brost , John Hubbard , Peter Xu , Will Deacon , "Aneesh Kumar K.V" , Nick Piggin , Oscar Salvador , Minchan Kim , Muchun Song , Xu Xin , Chengming Zhou , Chris Li , Kemeng Shi , Nhat Pham , Youngjun Park , Gregory Price , Joshua Hahn , Rakie Kim , Ying Huang , Jason Gunthorpe Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, kexec@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-arch@vger.kernel.org, linux-cxl@vger.kernel.org, Josef Bacik , "David Hildenbrand (Arm)" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4238; i=david@kernel.org; h=from:subject:message-id; bh=HSq6X/4K+SfPFkviF2rs567Nufb8NBDKLoe9P0lK1SA=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLLu35/OsvFGcH2Wo5bmDct3yy6Eyfkcepkg7VWeznVXV OA8b+bTjlIWBjEOBlkxRZag4PsGL1+uq61242OGmcPKBDKEgYtTACbyopLhv5/tl/2M77RcfY0u aHKYGOlJqt7fkrkle85nU9N5lduDXBkZFrqs6/fxP278dlJVj395qVda2efLWs0N9UrH55Upsp/ gBgA= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A X-BeenThere: kexec@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "kexec" X-Spam-Status: No, score=-19.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Josef Bacik If ZONE_DEVICE memory shares a memory section with ordinary memory, the section is online and the pfns of the device are valid. pfn_to_online_page() tells them apart by asking get_dev_pagemap() whether the pfn belongs to a dev_pagemap. get_dev_pagemap() only returns a dev_pagemap whose reference is alive. memunmap_pages() kills the reference first, waits until all pages are idle, and then removes the memmap. During all that time the function finds nothing, and pfn_to_online_page() hands out the struct pages of the device as if they were online memory. A pfn walker reads them while they are poisoned and freed. The entry in pgmap_array is only deleted after the pages have been removed. Look at that instead of trying to get a reference. This is probably not worth CCing stable. Fix generated by an LLM and sent by Josef Bacik in private. Fixes: 1f90a3477df3 ("mm: teach pfn_to_online_page() about ZONE_DEVICE section collisions") Signed-off-by: Josef Bacik Signed-off-by: David Hildenbrand (Arm) --- include/linux/memremap.h | 6 ++++++ mm/memory_hotplug.c | 13 +++++-------- mm/memremap.c | 13 +++++++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/include/linux/memremap.h b/include/linux/memremap.h index e3c2ccf872a8a..b5e5e626c7a59 100644 --- a/include/linux/memremap.h +++ b/include/linux/memremap.h @@ -231,6 +231,7 @@ void memunmap_pages(struct dev_pagemap *pgmap); void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap); void devm_memunmap_pages(struct device *dev, struct dev_pagemap *pgmap); struct dev_pagemap *get_dev_pagemap(unsigned long pfn); +bool pfn_has_dev_pagemap(unsigned long pfn); bool pgmap_pfn_valid(struct dev_pagemap *pgmap, unsigned long pfn); unsigned long memremap_compat_align(void); @@ -284,6 +285,11 @@ static inline struct dev_pagemap *get_dev_pagemap(unsigned long pfn) return NULL; } +static inline bool pfn_has_dev_pagemap(unsigned long pfn) +{ + return false; +} + static inline bool pgmap_pfn_valid(struct dev_pagemap *pgmap, unsigned long pfn) { return false; diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 796af1028ee23..5a972d7a9e478 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -342,7 +342,6 @@ static int check_pfn_span(unsigned long pfn, unsigned long nr_pages) struct page *pfn_to_online_page(unsigned long pfn) { unsigned long nr = pfn_to_section_nr(pfn); - struct dev_pagemap *pgmap; struct mem_section *ms; BUILD_BUG_ON(IS_ENABLED(CONFIG_HAVE_ARCH_PFN_VALID)); @@ -363,14 +362,12 @@ struct page *pfn_to_online_page(unsigned long pfn) /* * Slowpath: when ZONE_DEVICE collides with * ZONE_{NORMAL,MOVABLE} within the same section some pfns in - * the section may be 'offline' but 'valid'. Only - * get_dev_pagemap() can determine sub-section online status. + * the section may be 'offline' but 'valid'. Only the presence of a + * pgmap can determine sub-section online status: it indicates a + * ZONE_DEVICE offline pfn. That includes a pgmap whose reference has + * been killed, its memmap is about to go away. */ - pgmap = get_dev_pagemap(pfn); - put_dev_pagemap(pgmap); - - /* The presence of a pgmap indicates ZONE_DEVICE offline pfn */ - if (pgmap) + if (pfn_has_dev_pagemap(pfn)) return NULL; return pfn_to_page(pfn); diff --git a/mm/memremap.c b/mm/memremap.c index accba23aef28c..a1e99a4c55c56 100644 --- a/mm/memremap.c +++ b/mm/memremap.c @@ -413,6 +413,19 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn) } EXPORT_SYMBOL_GPL(get_dev_pagemap); +/** + * pfn_has_dev_pagemap() - does @pfn belong to a dev_pagemap, dead or alive + * @pfn: page frame number to lookup page_map + * + * In contrast to get_dev_pagemap() this also finds a dev_pagemap which is on + * its way out: it is registered before its pages are added, and until they + * have been removed. + */ +bool pfn_has_dev_pagemap(unsigned long pfn) +{ + return xa_load(&pgmap_array, pfn); +} + void free_zone_device_folio(struct folio *folio) { struct dev_pagemap *pgmap = folio->pgmap; -- 2.43.0