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=1795947326; b=Mx5qj6lAo5mifPP3b1cP3svAvf3iKiCTDY3lohrMIrZjsMZleg069K/vvjQFgd/rdg54 PI8NDLiXwbrqvnzOlTEWAc+IDsn6lxeVivjTN4qtpbrbF2QVZa2OQpbu5Fyn0UmGFRWCU fvDCJQeAQnzo4iLVmOl+tpTWucS4XakKly0gVjVZoHNjp+C+WqCmZVWOFdRlsRJHBVsis /0kDAZD4lGY2xWby7oTvif6pCsVVYkAGX1+aoi+os46uZdNoE3JEj/RKOr3nas9tev7yR Y31PI45xDrmwpQcdu+R3q2A3TTNgFyBJ19gvVgWgWiNuL3NknwJF/9dDnUxB2M1hpiQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795947326; 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=jwWqX3+uaWa8M/xNjyRF5eCCvUcm8oQcoSAAq2gg3PU=; b=5pfQiwXqhw8+O3YohciMOfTKznmY7Phio41/gd/DWthatq3PYgZqb2F7N9PMCPvo55x6 GsODqyvQXHJOTkD/lUwEKLLlEEDNbYjOGf86P6kqa2YgEi50kEAkfmguXilz75uId5luo GwpLtQgafShkGQ+WBQN9A5CVlLbZT/gZMOsXDhhlGtkg3Ia5kTWvIgHor9+OlvdeJSEcG pTIJ14Q9tuFCHyJ5QLr5sNy2Q9vuj8dDt7/MWvQRVs6oLPMewgs0QnWYHcNEVxqhgwL/y 26WzsbmMEyLiswyBgmNe/vbrvHb6T7fKkp2zmh51efSMqXRl/JE9ZKc3pVpPmiIDIZA== 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="A/I45vsA"; 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 AC2E8606A31C; Sun, 29 Nov 2026 10:15:26 +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=A/I45vsA Received: by smtp.kernel.org (Postfix) with ESMTPSA id 86A4D1F0089F; Sun, 29 Nov 2026 10:15:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795947325; bh=jwWqX3+uaWa8M/xNjyRF5eCCvUcm8oQcoSAAq2gg3PU=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=A/I45vsAwh39p3pU0FsTU/dMG9ZBdhIiZJmt6BSa/5Q8VKd4OYGaawGIwjyz+qw/T h4AGEZFnR/pna9AwGpqrxGqgN6yTgzts/hHInk4EliMcQ1DAav5VTFCNEZ+MTKUIkj hEdgh8erYRBiTSH/jqCy3+1f8p4hMtxTN6cAfRJS/bs7gC8HjngnAdud4UPEdksvob /wZjsVNqV39xZI6u4ERGLJL6dF1nXaqn9lMpop4uHeojCZf7xebyCJORuH79uU5Frm HUYB0mkNy6buDqS8PfYxykpRJVTtfS8RJK2ajY/ww62qfKJToyoxwF7qo6ozQmhGY4 lI8UkyTOpcAow== From: "David Hildenbrand (Arm)" Date: Sun, 29 Nov 2026 11:06:58 +0100 Subject: [PATCH 27/32] mm/ksm: copy KSM folios at swap-in without VM_MERGEABLE 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-27-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)" , Chris Mason , stable@vger.kernel.org X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2960; i=david@kernel.org; h=from:subject:message-id; bh=M8JH+XqwAoJV1zmumJV49liLIYf7lKQhzKvUIOZilxo=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLK5f+qF7fmVUtIuxVv+fm/1T//aZxX8Rld+aex01avYZ WG1rdSxo5SFQYyDQVZMkSUo+L7By5fraqvd+Jhh5rAygQxh4OIUgIlYPmX4zX76/G3juVNNFx9q meMpoyQuNMUsXUmpbcq/bouLnjEamxn+ByZ3lKxUOnXKak9qxI246v+Vv9RWKhQocYgGzIkxEPz OBwA= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A From: Chris Mason 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: when unmerging, break_ksm() skips swap entries. If the KSM folio is still in the swap cache, swapping it in after clearing VM_MERGEABLE will map the KSM folio in a VMA without VM_MERGEABLE. Consequently, rmap walks can miss such folio mappings, resulting in the KSM folio no longer being reclaimable or migratable. Worse, until ksmd notices that VM_MERGEABLE was cleared and drops the rmap_item of the process, rmap walks still look for the KSM folio at the old address. If the rmap_item belongs to the parent process instead, that will never happen. If the process calls mremap() while the kernel is migrating the 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. Let's fix it by copying KSM folios during swap-in if the VMA lacks VM_MERGEABLE, like we already do while KSM_RUN_UNMERGE is set. Note that mremap() in a VM_MERGEABLE VMA can similarly move a swap entry of a KSM folio, as break_ksm() skips swap entries. But in that case, rmap walks will find the KSM folio again once ksmd scanned the new address, and we cannot end up with a stale migration entry, so we'll leave that for another day. This has medium urgency, as an unprivileged process can trigger a use-after-free if KSM is enabled. Fixes: 5ad6468801d2 ("ksm: let shared pages be swappable") Cc: stable@vger.kernel.org Signed-off-by: Chris Mason [ david: code cleanups; clarify comment and description ] Signed-off-by: David Hildenbrand (Arm) --- mm/ksm.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 90cc83275764d..509bd78156583 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -3211,8 +3211,9 @@ struct folio *ksm_might_need_to_copy(struct folio *folio, struct page *page, * So test, and copy, the page of the slot. */ if (folio_test_ksm(folio)) { - if (folio_stable_node(folio) && - !(ksm_run & KSM_RUN_UNMERGE)) + /* Never place KSM folios into VMAs without VM_MERGEABLE. */ + if (folio_stable_node(folio) && !(ksm_run & KSM_RUN_UNMERGE) && + (vma->vm_flags & VM_MERGEABLE)) return folio; /* no need to copy it */ } else if (folio_anon_rmap_compatible(folio, page, vma, addr)) { return folio; /* still no need to copy it */ -- 2.56.1