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=1796389153; b=ztaZAAv56i0WkCDze9ePlY3I72xuC0QjHpsvNQdq8u65eshKO4B2N2Daevs7NBUNMP42 2osff7B9bpgjolP2vfoNoQjgvQ/pvHcUCE92hnt/sd7qjVf0dLx7kruScR/Bos1IXHX+K ZsYRNDzxVicPtsJUOVA+KRw6GUo9El/Kc/spMldfOb3TGsO3vPROpdSIFLa+oLUb+HXn7 p9MYr46J/zQwX7taGjnIXyx0/4Uy5wn4pgo/04JHoq68+hM3g5ZP0+uIxyU/jBWY9yQZS qnIwChTI4RmcfAYqf0iNpfUSYGHn8BwJI3LudWPkOXs2/anD2ZXRE9fu37liBmoa5kg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796389153; 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=wmofppw0NtUfE54rwOpqXv1iGuKv1x2PITXbEmkKTrU=; b=YrbbZhopg1dy6qCKBkAdzRUzxOMoGgkuv2AR8bayP72Ye9wj5iFd1Kqaygh+iTxBIGqf 2AV1oD2XaHVRbtZDUTsEYBp7mltYxJxza7geaaW06bzbaAkI+2Hu6dzGKch9iS4Kun4Gk ybMVKPsV10RQwEHeZ/8NoWIQ0Lup/KVC3YTvgfq2LKwaANtacoHi4zGVBVmt1fsdBJBK9 tghizDg7tN00xBV14eUtRfAL+QYm9teabR89T6knjhJZNEy8n7YY+Z8yCBOF79OCT2cyG 8/MgmqIEObJGvKTpf6A6znz4VBKpDp8eKp+eKBzz07Xdx3Tc45y/QkzMYUOeUhgkIzw== 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=fhuxuyhF; 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 6A89E606A322; Fri, 4 Dec 2026 12:59:13 +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=fhuxuyhF Received: by smtp.kernel.org (Postfix) with ESMTPSA id EB0FF1F00939; Fri, 4 Dec 2026 12:58:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796389153; bh=wmofppw0NtUfE54rwOpqXv1iGuKv1x2PITXbEmkKTrU=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=fhuxuyhFjHfbicutkq+ultYHKWIPtyuA6JkDCOTOa/iKc3c4WYvmQl8eHYA3CYiJG HNUXoXD41OE9iPTEVHZ3wn7j4AH6+MzSQ3mibhjVNkjAqcu18+hKsyDMvOmutHMXmV M36FL5VTFmCBKWRD2BcEy614AvBec/uUZcOhdmDENIYQ+90pDxwQSj5VyyNtoZMPfP 6md6GbIQtubRmtOW56XoPreNMa+Km+AedDibZow3Ph1cVqP+IgLm8j8/RCniUUmb4P h0acECQaFQ4OCk91unUF48fEzZcXL8lbQMtl3UHm2p4z42SAWdzj3otr2PdbFA1b9J s0fSHQBOv7rWQ== From: "David Hildenbrand (Arm)" Date: Fri, 04 Dec 2026 13:50:20 +0100 Subject: [PATCH v2 29/31] mm/migrate: fix KSM oddity by checking expected refcount without mappings 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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261204-fixes_core_3-v2-29-9aad65727632@kernel.org> References: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> In-Reply-To: <20261204-fixes_core_3-v2-0-9aad65727632@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=5348; i=david@kernel.org; h=from:subject:message-id; bh=GhMM4LQV+DoLWGJep6T346X51RLgr0MlqmXYiTghHsg=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLKFtjOydOVZOXuk57v02CZrdi076T9b8c1TruXbpxx8m njrv7Z+RykLgxgHg6yYIktQ8H2Dly/X1Va78THDzGFlAhnCwMUpABP5+pWRYctn0xXZZ31FdiaZ 7mj5z5KTvWdZc7CURu8SP6MTMkINExkZ1j5e/idGfpfPSaUbvqoZRn1qUxmfdMywc/9w5ui03MJ 2ZgA= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A KSM can add page table mappings to a folio that the migration code has locked and unmapped. 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 Reviewed-by: Zi Yan 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 62c720c2d4542..3826e51b1bc59 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3217,6 +3217,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 @@ -3231,7 +3267,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., @@ -3252,29 +3289,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 2fe4cd936e71b..4743bd76e93f4 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -917,7 +917,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