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 90437606A32D for ; Fri, 4 Dec 2026 12:58:43 +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=iXJ4z2NR Received: by kanga.kvack.org (Postfix) id A99396B05DE; Fri, 4 Dec 2026 07:58:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A6FED6B05DF; Fri, 4 Dec 2026 07:58:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 985566B05E0; Fri, 4 Dec 2026 07:58:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 70F406B05DE for ; Fri, 4 Dec 2026 07:58:42 -0500 (EST) Received: from smtpin09.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 0F25EC03D0 for ; Fri, 4 Dec 2026 12:58:42 +0000 (UTC) X-FDA: 85506304884.09.E3D1F42 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf17.hostedemail.com (Postfix) with ESMTP id 359E940007 for ; Fri, 4 Dec 2026 12:58:40 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=iXJ4z2NR; spf=pass (imf17.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=1796389120; b=8gTuaBjIwXpB7hrJKOKL1okOMOW5DmL3GZjVuRVcNHQKA7LCzXWn20MU+5QTBRbNxherOo HqRNuqMD8jHrq5H203lm2+PiBpuctjY5V/6KfaNJ4wDrpshJxJmg95V957l1y4QQSHCtn/ kK9esNcxoqT7Bz5DxxVv5haVZfUF3UI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796389120; 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=ci678X6+TD4w8s10y99DTaL5/HRx/AFCrcH+FhAJl9M=; b=tdIVQg73oWEOpa+2f9XhUDO++HW3kNE7lCqQ9whUXi73svOFlVMqvCHMjdXfhXBSjnN+SK eszcsE5VvM9VOG5mTd8dFCO/YQPbtvJRzHt5J++MtvrC6K33AjPIFZwv/Qt0ylX+rjyi6e qahnzjZwkMZs5/jsGomVyMfTP+5+H/M= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=iXJ4z2NR; spf=pass (imf17.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 6231244356; Fri, 4 Dec 2026 12:58:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7109C1F00933; Fri, 4 Dec 2026 12:58:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796389119; bh=ci678X6+TD4w8s10y99DTaL5/HRx/AFCrcH+FhAJl9M=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=iXJ4z2NR04EvYYj5Q49atEaTUyds4MOXfYkI4blcXIUv2VOgxOeCy9kcbWm2lrtLU vKesacII1QwUVL48fkoBBCDnxYjWdDLYutI63caZo+F/ZVgpWjRyqsf/gmil5J5ye9 a4u+Jp9V6JizNT4x16Mg0yv8B3KAuylYjLRzDGKwhkSIcj4REPql5K9onTcBTeye66 ATU7lMg6xxJ0eqhSGZflrusWNWeuOPHSUeGJxI9Mx4vhpK3gtNmGES9YKc/BewFtVR I0TdOTrMyeyDS2eNuk9wTU0Mv4h93MyUSVGedp5AWvYNcRSQuP5zb928i7vArxQ5b2 GHylqrGgBye5Q== From: "David Hildenbrand (Arm)" Date: Fri, 04 Dec 2026 13:50:18 +0100 Subject: [PATCH v2 27/31] mm/ksm: don't convert a shared anon folio into a KSM folio 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-27-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=8086; i=david@kernel.org; h=from:subject:message-id; bh=8zAYhg0sAtPWIY7BEQKSKZNZeDQD6AjMLVHgDwEt1xY=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLKFtjNKT3ntf10gfNLlo2ncwgXmLcH39///4n+qd0rK5 tvf1r3q7ShlYRDjYJAVU2QJCr5v8PLlutpqNz5mmDmsTCBDGLg4BWAiNlwM//OXc6S0xfIZMBy9 /4xPX3lHxewgo5TNaz079x0xFXbnOc/wV968uOF06QXHnY0799grr7jxZcam8laPVafczOK9Hjb e5AIA X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 359E940007 X-Stat-Signature: waupkyk3mc43551nhqdqbygqqs4w1ft6 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: 1796389120-8585 X-HE-Meta: U2FsdGVkX1/5nXrtgE/bbXSqSeTlUa6c44gD6Hvh9G+QTvwC+rB8O1Wuf7PxSicro153/hTtp0L93dRMFg5XK7ARW8kgbSbR4svnM0Yse/hcDw/W3OdLfbQF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uA3cDoIIql2Wqp0NnC6AtlzEpzwvVF2e/qYwaZKao1G7XYJNyEOA9wpN0NXtDot0x6GE81KyWkZlfG/u93XGvQnxBfCXPKVs996ZPfwOqU9TBTFkUABIsOzwH1cg2xwCV/osL7AV/tctyKWuS2sZVoS5CgA+L8xl5bv8wLrVwMxQVCDstnnb8IqODPErDHi63lxPvvJvhabF/0y1IEOA8WGM0dRgQdOnuazRoJ/Mggg58ANs4FynssLbJP1yVYCNMwcvQxmVOhRD9VwupnoJS80vIFB7CU76c1Dk4od53Z+mA6ufWQlNs0cWjXcP4Oaz+iSGmyLYdgf58e6ou3N6vACU= 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 So far we were under the assumption that only VM_MERGEABLE VMAs may contain KSM folios, because we unmerge when clearing VM_MERGEABLE, and KSM only merges within VM_MERGEABLE VMAs. Various infrastructure (e.g., rmap walks) relies on related properties: for example that a KSM folio is always mapped at rmap_item->address, also in child processes. We ignored one scenario: turning a COW-shared folio into a KSM folio in one process will make all processes mapping it suddenly see a KSM folio, even if VM_MERGEABLE is not set for them. Consequently, rmap walks can miss such folio mappings, resulting in the KSM folio no longer being reclaimable or migratable. Worse, if such a process calls mremap() while the kernel is migrating a KSM folio, mremap() moves the migration entry that try_to_migrate() installed, and remove_migration_ptes() will not find it at its new address. Once migration frees the source folio, the moved PTE still holds a migration entry that refers to the freed, and possibly already reused, folio: the next access through that PTE will hang, or worse. Further, if ksmd scans the child, rmap walks will not even find the parent's VMA: the child's rmap_item references the child's anon_vma, which the parent's VMA is not linked into. Let's fix it all by refusing to convert a folio into a KSM folio if it might be shared with another mm. When merging two folios that are not KSM folios yet, try both directions: as long as one folio is not shared, we can merge. Shared folios can still be merged into an existing KSM folio, the shared zeropage or an identical folio that is not shared. Only identical folios that are all shared will no longer be merged, which should be fine for the known use cases. Note that other processes might only hold a swap entry for the folio, and map it after swapping it in. As the previous commit only copies KSM folios at swap-in into VMAs without VM_MERGEABLE, also refuse to convert a folio that is in the swap cache, unless we can free the swap entry. This has medium urgency, as an unprivileged process can trigger a use-after-free if KSM is enabled. Fixes: 80e148226028 ("ksm: share anon page without allocating") Fixes: 5beb49305251 ("mm: change anon_vma linking to fix multi-process server scalability issue") Cc: stable@vger.kernel.org Reported-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) --- Documentation/admin-guide/mm/ksm.rst | 3 +++ mm/ksm.c | 47 ++++++++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst index 8968ae3f55c0a..079db28bfc5b4 100644 --- a/Documentation/admin-guide/mm/ksm.rst +++ b/Documentation/admin-guide/mm/ksm.rst @@ -27,6 +27,9 @@ KSM's merged pages were originally locked into kernel memory, but can now be swapped out just like other user pages (but sharing is broken when they are swapped back in: ksmd must rediscover their identity and merge again). +Note that KSM might not merge identical pages if each of them is already +shared between processes as a result of fork(). + Controlling KSM with madvise ============================ diff --git a/mm/ksm.c b/mm/ksm.c index 509bd78156583..fe7b3b60540db 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1494,7 +1494,9 @@ static int replace_page(struct vm_area_struct *vma, struct page *page, * @kpage: the KSM page that we want to map instead of page, * or NULL the first time when we want to use page as kpage. * - * This function returns 0 if the pages were merged, -EFAULT otherwise. + * This function returns 0 if the pages were merged, -EBUSY if page must not be + * turned into a KSM folio because another mm maps it or can still swap it in, + * -EFAULT otherwise. */ static int try_to_merge_one_page(struct vm_area_struct *vma, unsigned long addr, struct page *page, struct page *kpage) @@ -1509,6 +1511,12 @@ static int try_to_merge_one_page(struct vm_area_struct *vma, if (!folio_test_anon(folio) || folio_test_ksm(folio)) goto out; + /* Avoid lock+split if the folio is obviously shared. */ + if (!kpage && folio_maybe_mapped_shared(folio)) { + err = -EBUSY; + goto out; + } + /* * We need the folio lock to read a stable swapcache flag in * write_protect_page(). We trylock because we don't want to wait @@ -1524,6 +1532,18 @@ static int try_to_merge_one_page(struct vm_area_struct *vma, folio = page_folio(page); } + /* + * Turning a folio that is shared with other processes, or that other + * processes could still swap in, into a KSM folio can result in KSM + * folios showing up in VMAs that never had KSM enabled, or in VMAs that + * rmap walks cannot find. So just deny on any sign of sharing. + */ + if (!kpage && (folio_maybe_mapped_shared(folio) || + (folio_test_swapcache(folio) && !folio_free_swap(folio)))) { + err = -EBUSY; + goto out_unlock; + } + /* * If this anonymous page is mapped only here, its pte may need * to be write-protected. If it's mapped elsewhere, all of its @@ -1598,7 +1618,9 @@ static int try_to_merge_with_zero_page(struct ksm_rmap_item *rmap_item, * try_to_merge_with_ksm_page - like try_to_merge_two_pages, * but no new kernel page is allocated: kpage must already be a ksm page. * - * This function returns 0 if the pages were merged, -EFAULT otherwise. + * This function returns 0 if the pages were merged, -EBUSY if page must not be + * turned into a KSM folio because another mm maps it or can still swap it in, + * -EFAULT otherwise. */ static int try_to_merge_with_ksm_page(struct ksm_rmap_item *rmap_item, struct page *page, struct page *kpage) @@ -1642,7 +1664,8 @@ static int try_to_merge_with_ksm_page(struct ksm_rmap_item *rmap_item, * to be merged into one page. * * This function returns the kpage if we successfully merged two identical - * pages into one ksm page, NULL otherwise. + * pages into one ksm page, ERR_PTR(-EBUSY) if page must not be turned into a + * ksm page, NULL otherwise. * * Note that this function upgrades page to ksm page: if one of the pages * is already a ksm page, try_to_merge_with_ksm_page should be used. @@ -1655,6 +1678,8 @@ static struct folio *try_to_merge_two_pages(struct ksm_rmap_item *rmap_item, int err; err = try_to_merge_with_ksm_page(rmap_item, page, NULL); + if (err == -EBUSY) + return ERR_PTR(-EBUSY); if (!err) { err = try_to_merge_with_ksm_page(tree_rmap_item, tree_page, page); @@ -2359,8 +2384,19 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite struct folio *tree_folio; bool split; + /* + * Try merging in both directions: if one folio is shared but + * the other isn't, one direction will work. Merging will fail + * if both folios are shared. + */ kfolio = try_to_merge_two_pages(rmap_item, page, tree_rmap_item, tree_page); + if (kfolio == ERR_PTR(-EBUSY)) + kfolio = try_to_merge_two_pages(tree_rmap_item, tree_page, + rmap_item, page); + if (IS_ERR(kfolio)) + kfolio = NULL; + /* We might have split the folio. */ folio = page_folio(page); tree_folio = page_folio(tree_page); @@ -2375,7 +2411,6 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite * split_huge_page should succeed. */ split = folio == tree_folio; - folio_put(tree_folio); if (kfolio) { /* * The pages were successfully merged: insert new @@ -2401,7 +2436,9 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite break_cow(tree_rmap_item); break_cow(rmap_item); } - } else if (split) { + } + folio_put(tree_folio); + if (!kfolio && split) { /* * We are here if we tried to merge two pages and * failed because they both belonged to the same -- 2.56.1