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=1793057275; b=NnVy+6ivi46SFUtDkImX++x+wOfKx85JGUnbsM+/DMWXZ34OE4vlMMyWzUEu+Ac0T0Xu KPUHepoVkt0WSLxPCOodBxdVyaP2nsD01YpHQEtzFX9wlmmMW/KL63ibyPT/gyz5m0pYB zVqNN/p5fjtahuSiCbm7Pyn2YLYeozBVlU917/QxYuz7RtsgR37MFx3BjZWfHgaYSusKj mb2Q26jLAK+rRiiLwg9vQdKQhstGq1MhSXQ4iI9E5Gj+KC6WpO15chH9i1GzAkebtKOks /ArnT8tFE0DKarStZ/n9wS/Y83Z/74THfx8ZC81PJinm5ZTPoRJt9AQuXBGiOOrKS9w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793057275; 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=NHUznzbhtn3AtvFPFOcCt9tvyez2ae3rJ1KObV9N9d8=; b=rr0uxIJMzHlIWKKF33je/9GjYN5UuRGyfeMWLcaO/3YHa2obJbvCTQvHCV5FceksILfX YHysIHD173YDejxs1nbYjHz1h2zUnEq2YSwPeC29TMvL6Jj4i5A7u4OxKGNfY2/crgHgN CJCrvx5+sSbO4MFPxIWT0VDN4XCyoRvotaAaAFSEB4ahgipSAQ9hfMQHf5PjVDbasFhv1 zm8lAqsAL3gCotupzdEm8eQlL/PMRolKp0QlzzQy1Q/8DACvJz2ys1BfeNsffcfsq9z7H PYOPFs6T3DRMGLq4E0VYYtESG5rFcZefkL7dG7mpgOSsXq/iAw5co8p0YXaDfL1tYOg== 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=Wax5YaVW; 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 834A8606A329; Mon, 26 Oct 2026 23:27:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Wax5YaVW 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-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: <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 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