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=1795947345; b=hlko+kjfQBpa771A/jM2ZItRqeAQ3GR/IQ6EXmb6/SK3+gMheqYDSYyJh0BzPTgOMSr7 +khKMGJnSkzDE4A6o4R9l1x+ZrtJs1KlXUVJadN2XGTdIqn96eystFFrr8CWVFfiysSUi sMQzS7Nx95pebhZLUgvIzSQGXLmUkPTQf+/ZDagODUk58oCpj45cRSuMfI8Zv9+yO4DLG eNWS79SghwbZyeEUeMKIbO6JjbVGe90MySl8MXHx/lOixygxjFetuodRhJyr94t7SrrYA Zq9cD5HCQikHscc7iPwihcMzWusBXDJlYY7SqpibvsmeGPF+CGr+8n2PcdAB1f1Epag== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795947345; 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=Algu8cur6K3C1RNoeVFgQyiwsQO60lszN/7QII21fnE=; b=05NTou5TPq4seeOvhSg2IM3BRa4IMRQG/bNGZhxW4bt8nXQHn9dShRwjH+S2wsOJwfOv iv0HadYSDLDyuyOf7p4wLt4HHAqozm9QxzcEujs1BRhw0Uy43kDV0LNOcr1eSyBxPlATS CW61rUeEqZp4xr50fFKAQ1KdSw+00JX2nFvNPyorIEBj/A7zKmAR9qjelWI0AVDGAUJ4i T/FIr0iiZUzprk+RsO0+k2ClT327qktCIRSuhCMp2Be1gtDyh5FOiAv69jcei9WjJbwzS Qljbxugl82Z/azygNOTe/dRTqrG9MrF5QOQhCd5nY+wWdqg1k37Oc5nzUlFbSTO0khA== 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=JRK91tAM; 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 E12A2606A31C; Sun, 29 Nov 2026 10:15:44 +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=JRK91tAM Received: by smtp.kernel.org (Postfix) with ESMTPSA id C46571F00898; Sun, 29 Nov 2026 10:15:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795947344; bh=Algu8cur6K3C1RNoeVFgQyiwsQO60lszN/7QII21fnE=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=JRK91tAMcQsVCdklfT7DWtwoyd63MENJbC5jGlfoWnSvKfksw4uWcYVGdwtHabZXI xi619TBsI4wjf/rX5p8AqGTJ6LT/3TUG/iAULI4SzZeVN/bedfxa1zZZ3LDk50+541 4i8kdGQGb/ilcPjiCpb26wamlnOBsk/u+dQn+zRaNFxCoueY6vWZkTw0A54SCxO3EO Nu20Pdn1tpbpWnX/jnj4aXqjxwaM+LfVsHHXK3OUl3Ur9bvAW5WSI/OdP+o8ct0t3r uzBODlr5ZvZt/ktv8nuTM2vgdAfjilDR262ITdehRIc/ygMN6skGrzo5uALDB1X9jL bKvVyXycG5wOA== From: "David Hildenbrand (Arm)" Date: Sun, 29 Nov 2026 11:06:59 +0100 Subject: [PATCH 28/32] mm/ksm: don't convert a shared anon folio into a KSM folio 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-28-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=7356; i=david@kernel.org; h=from:subject:message-id; bh=cJ2B91thaeMFoQjMJ8GL3ZTy4vgZtdEylDMDaJgnL00=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLK5f+pViE54cLpR4W6Wz+akbTZ1+xSaOxc3LNZtl+7+J 81dqLKoo5SFQYyDQVZMkSUo+L7By5fraqvd+Jhh5rAygQxh4OIUgIn8s2Vk+PLLVf79+3sOr/fa nfF8c/fvqzcN917fuCZ3ZW9794q9a5oZ/hmtncg+7dnHs9cjdr5z/uddKCK4eoH+g7iD8/Y1pnu lVLAAAA== X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A 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) --- mm/ksm.c | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) 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