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=1795947381; b=JxjaDpFEMZiooE1OgZHCjOud88/tTZ2IUBUhoqifrbPbTjpxvL9row1ShpLxbjP+yFe5 mqw76PHeHJpVZF3rWFkls0lv04WrGfiYd4+xTn9Corb85Z8jAHoyukYZu8SGzfO+V924/ o6ovP3JYauwpvax36U3VvpA0WmVMuQOzJSB077GML9i4OhaA6MKkkptQ2Dluoxgr7tpPu my/prtpcFfXDwOb6gMPZbTrA+Dh5ep/jeAUiEbaTjQoSTlbiz2LIK7BWg0XVHnvyyV9+3 eY4T3WVeXY/Tq3CMW7v63/zGmadWmEIeh5iai9EqZkUi3VwbiIOVV69QKbD4tylVzOw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795947381; 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=7SOLWywnS1YGYszspR2gepWpPyi39NWjcMWuYou0e2o=; b=OEHdRP6N6FajgXfp1uI0wK6a9MKl0BX52BAB8DiluNvd8C2QxLHrAPv88L7Aepd9oocZ 6cu17mkOtgy2yszNrzgdWW7c0zUjLBL6B8whsBlYHRS5HrhiQXOxoVMLfO45hKQSbfvvq klYsP/9Yz3xHgKF/21JXmfLQlEE0/6tP40hoJsMowAvqMvvQiR7USQ8gXfPyFOcUBnjtL R+L2+gxLnQpdL0cGdba8QE5hgHchaiaQeAZR1rcXp13pA1q68nSR8sjrh3gzbMntML5vP Dz++dXKZpCwll9iOnVwHsjlXapa0Ev6NbxRPCr+kYg8himNWGy1J2loGEmuWHvo0rmw== 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=mj0McRD3; 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 306B0606A323; Sun, 29 Nov 2026 10:16:20 +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=mj0McRD3 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3FD1C1F0089A; Sun, 29 Nov 2026 10:16:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795947380; bh=7SOLWywnS1YGYszspR2gepWpPyi39NWjcMWuYou0e2o=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=mj0McRD3SkxhRZoZXzSqv8IA1JaFQHV4v1H/z/fC9cPN9M/FV713iW0ORDoyFGxa9 ZEkQjgPybBcYtq/Hw2Qrldc6dQzi8Rwjof0HifLHZ3PqfenYreRQXPx1CbhVq1YfAX Mtfi0kk2LyNa/9kErcIQEjHBQM4SzWAwwwYwBQGygRoCL1vc4R3SVpMVJ4G0ty/xhQ 02cRpScnmeAjGUs//mT/IHcvfAdxyG9wnwQssE7Xto3u5fTbhGFKSQS1XE2vwSgROc HQyqguWdsDkyQZ9sm2ExuLUtHc9ihQDQYqjSHFFvTvzDQzVrnR+f2xM89Mok4ItTqT A0seUq2SeaQNQ== From: "David Hildenbrand (Arm)" Date: Sun, 29 Nov 2026 11:07:01 +0100 Subject: [PATCH 30/32] mm/migrate: fix KSM oddity by checking expected refcount without mappings 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-30-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=5312; i=david@kernel.org; h=from:subject:message-id; bh=1yX6k9D4lD59saFJIWFNqSw53skOx2qPMb3pvSwf//4=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLK5f+rxsbwO961blPD19yTtt8Uis9eeVXdTjzM2YXh5n /u16OtvHaUsDGIcDLJiiixBwfcNXr5cV1vtxscMM4eVCWQIAxenAEzkrhHDP+U+xqmFO6yjJyhZ H/PwcDh7rloqppf764+bGYl7w5bz7WFkWBHLKygSfyt/yYWCLZe7Tq87Xueq6XBrXaHeBCmtDY+ LGQE= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A KSM can add page table mappings to an unmapped folio while it is locked and getting migrated. While that is completely inconsistent with everything else in MM and should be reworked, changing that is a bigger task. When ksmd establishes a new page table mapping, it holds a reference on the KSM folio from stable_tree_search() to stable_tree_append(), and relies on migration to back off due to the raised reference. However, __migrate_folio() considers the mapcount as well, assuming that it is simply 0. A mapping that goes away after the mapcount is read and before the refcount is read can hide ksmd's reference: refcount mapcount unmapped, held by migration and ksmd 2 0 replace_page() 3 1 expected_count = mapcount + 1 = 2 write fault, COW 2 0 folio_ref_count() == expected_count Consequently, folio_migrate_ksm() clears the stable node of the old folio, crashing ksmd when dereferencing a NULL pointer. Let's fix it for now by not considering page table mappings in __migrate_folio(). This has medium urgency, as ksmd can crash due to a NULL pointer dereference, although the race should be very hard to hit. Fixes: 86ebd50224c0 ("mm: add folio_expected_ref_count() for reference count calculation") Cc: stable@vger.kernel.org Reported-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) --- include/linux/mm.h | 62 ++++++++++++++++++++++++++++++++++-------------------- mm/migrate.c | 2 +- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index d09173a94e9cd..a8cb634a227d3 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3212,6 +3212,42 @@ static inline bool folio_maybe_mapped_shared(struct folio *folio) return test_bit(FOLIO_MM_IDS_SHARED_BITNUM, &folio->_mm_ids); } +/** + * folio_expected_unmapped_ref_count - expected refcount ignoring page table + * mappings + * @folio: the folio + * + * Like folio_expected_ref_count(), but without references from page table + * mappings: any page table mapping will be detected as an unexpected reference. + * + * Returns: the expected folio refcount, excluding page table mappings. + */ +static inline int folio_expected_unmapped_ref_count(const struct folio *folio) +{ + const int order = folio_order(folio); + int ref_count = 0; + + if (WARN_ON_ONCE(page_has_type(&folio->page) && !folio_test_hugetlb(folio))) + return 0; + + /* One reference per page from the swapcache. */ + ref_count += folio_test_swapcache(folio) << order; + + if (!folio_test_anon(folio)) { + /* + * One reference per page from the pagecache. + * Use data_race() since folio might not be locked. + */ + ref_count += !!data_race(folio->mapping) << order; + /* + * One reference from filesystem private data. + */ + ref_count += folio_has_attached_private(folio); + } + + return ref_count; +} + /** * folio_expected_ref_count - calculate the expected folio refcount * @folio: the folio @@ -3226,7 +3262,8 @@ static inline bool folio_maybe_mapped_shared(struct folio *folio) * the LRU cache does not apply. * * Calling this function on an unmapped folio -- !folio_mapped() -- that is - * locked will return a stable result. + * locked will return a stable result, unless it is a KSM folio: KSM can map + * these without the folio lock. * * Calling this function on a mapped folio will not result in a stable result, * because nothing stops additional page table mappings from coming (e.g., @@ -3247,29 +3284,8 @@ static inline bool folio_maybe_mapped_shared(struct folio *folio) */ static inline int folio_expected_ref_count(const struct folio *folio) { - const int order = folio_order(folio); - int ref_count = 0; - - if (WARN_ON_ONCE(page_has_type(&folio->page) && !folio_test_hugetlb(folio))) - return 0; - - /* One reference per page from the swapcache. */ - ref_count += folio_test_swapcache(folio) << order; - - if (!folio_test_anon(folio)) { - /* - * One reference per page from the pagecache. - * Use data_race() since folio might not be locked. - */ - ref_count += !!data_race(folio->mapping) << order; - /* - * One reference from filesystem private data. - */ - ref_count += folio_has_attached_private(folio); - } - /* One reference per page table mapping. */ - return ref_count + folio_mapcount(folio); + return folio_expected_unmapped_ref_count(folio) + folio_mapcount(folio); } #ifndef HAVE_ARCH_MAKE_FOLIO_ACCESSIBLE diff --git a/mm/migrate.c b/mm/migrate.c index bae9b801d7fb2..fbe12c4cd8abd 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -914,7 +914,7 @@ static int __migrate_folio(struct address_space *mapping, struct folio *dst, struct folio *src, void *src_private, enum migrate_mode mode) { - int rc, expected_count = folio_expected_ref_count(src) + 1; + int rc, expected_count = folio_expected_unmapped_ref_count(src) + 1; /* Check whether src does not have extra refs before we do more work */ if (folio_ref_count(src) != expected_count) -- 2.56.1