From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 834A6606A32F for ; Fri, 4 Dec 2026 12:59:17 +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 kanga.kvack.org (Postfix) id A99826B05B5; Fri, 4 Dec 2026 07:59:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A70BB6B05DF; Fri, 4 Dec 2026 07:59:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9867D6B05E0; Fri, 4 Dec 2026 07:59:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 77D2F6B05B5 for ; Fri, 4 Dec 2026 07:59:16 -0500 (EST) Received: from smtpin14.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 1E8DAA7EC7 for ; Fri, 4 Dec 2026 12:59:16 +0000 (UTC) X-FDA: 85506306312.14.F0BB1D2 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf27.hostedemail.com (Postfix) with ESMTP id 1E5FD40012 for ; Fri, 4 Dec 2026 12:59:13 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=fhuxuyhF; spf=pass (imf27.hostedemail.com: domain of david@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=david@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796389154; b=jb+OTrzpNqAQmAnULOEjQ7ZZ3z1vYncGf05qzurmP/QmxQSj37T846snkait80dZYrV8zU Y7f6Pr7BklUEfrmEW6vstd+tAA1fVrEwksv7tF5ANAtLW6sa9eVmJmMvBQZioP5Fps3vcs bDSHyqWDjpYjc+oypHIRW83oQhsSCgM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796389154; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=wmofppw0NtUfE54rwOpqXv1iGuKv1x2PITXbEmkKTrU=; b=TjFvzoAt+TR0nFTnPJSKep8uF4i9Y+j/C3YqSuaAbOuGYX28vEkqQF/oGW082+41xrk/gs +iVKlYlg9uboR+eHdbUz1rLgS24BSGJso+XaO4yzP5B6cl/zn/Mz3bBXOvzL686z62Yf1x UFgzWrPtXCqLLJBcl00V4WT+ubLIf8E= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=fhuxuyhF; spf=pass (imf27.hostedemail.com: domain of david@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=david@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 637DF42B11; Fri, 4 Dec 2026 12:59:13 +0000 (UTC) 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-Archived-By: 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 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 1E5FD40012 X-Rspam-User: X-Stat-Signature: 7kcg894o448heqhgcor9udpahaannpun X-Spam-Status: No, score=-19.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796389153-618815 X-HE-Meta: U2FsdGVkX18ecTAhkAccNZt3gCk+SrF6ooYE/x9fqcMCpFugerxb4WmGuJkRCwO3Ym8B7qYfE9pmFjJHQu9cvXEEC33e9PddrCpJRDJ8QpTUcfMt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BjZ3vi/0mtxpADbVtekbk2Npc2tTtJPfDE2HVocHltbBiJAaUTMkzExCSOnc1EVYXP7PPqy1zNSkLN47AbEIeHptd8alSQAxHj0W7HJOfmVb0VvaTArfy6hBJKfAUjgfhv//GXZqTa7nj4wkyXUUz6gBr692J0Kx1v8SwGC4cpoCbdBEYu1uCJkl8K0CcEH0bst0lTotSI4rAHJCZP5oSlrm3AhhWDm8hO/LdZjyLyp3rgdnPFugr/BHs4kDVAdDg+9E06aJ3yhtfzgrEjhstldR5UqlUhU4u20UthyGD+kFycS3ofTkiwdEh+yda2V0Rnh79DjsXl4LA1/0c2bH6HfFi4UVdCAkXD/O9ql3o1HLvXWu1Bo Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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