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=1796389102; b=L9r0nBZfxnvuVOxDhzr0iWFxMuMkc8ZYtPKf+mitF6PUEKZY6T4Sp9CFNeYcr6kHgQ40 aU/zxIZtIiriWmPalkxfc7CDQBstBEclm6tyq/8GCty2UPNAAYf1FN/AZxFGmZhRXvdJs HJCSOW8kOvATtZX/d9/yU+bCAeVoVBmUTZi+yPxlUcRSiW7wY0XgQEiBZ20nX+XwpFnjj bBHhEgB7IeBEuQuUX2RbK7iru22J+6mwfsBrC2HT40/efCrQN7RX8vg9DeD4NioPi+VVK 2iOK9auJmbFujgrIkapYIbTz2b9gUZaI2ZSA37ixLKYijGKG72tAbxhcxKyw77bYqrQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796389102; 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=PoAOGi+sD9zs1hZREX3JKS87RWrm5Q2k8d1oOc3SuUM=; b=tjkdIePlgcmGmXUQJkTyCCILpvxjRGGx7xUArNQrA4FjzDRSKz+Jiinm8I6Bbn8GH0bA 2VBtS9t2mbZDX4NO1eBS9rv+6rMkZKl6auYnJ0QNOLS/ZAi0EyGa8woQ0IDy4b1zjwgV7 4fMR2c1q4NlXsUAJ+DQowwOadZXpmFPvvIZBHy71pJlNKhIJlZ+0UXPOU6KeKsP7RFwxQ 2RLyfVBtutPsc9WSchjRFjPnkS/lalVx6JQNUF0GwDtHm0+S2zti7DooRfVYqYBohcflx Z8hU3Ln4/f+pp33NEtFZ8Rd3vLm2aefGoFed4So0MrrifyA9uUtGaJi0DUw8/pPvZVg== 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=hODDByTA; 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 1AF3F606A32C; Fri, 4 Dec 2026 12:58:21 +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=hODDByTA Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9CD751F00A11; Fri, 4 Dec 2026 12:58:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796389101; bh=PoAOGi+sD9zs1hZREX3JKS87RWrm5Q2k8d1oOc3SuUM=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=hODDByTAUZfcvBgnL3pxP3/dK2gyKeCKBUJ9Y9LTxUbA3Ph0/HPV4cLOTFjRY07hR sFb1xaagSILy8F+CEif2UjYLVHaF7XLaBiadywtG1/nrAm1eX3Jub9EEwVXDlPIb04 hVSHfVkNBPRojuh+z854HyRcM9T5B17gmfTnoKLr/d3ZO5Sys7YZ0a+qpuAHppJ3uT lERl0p4iRsz5kACVCeyX9Qp2EQa4+UgTHruSQah3HThH7eUtkGCosAf+sWZ0GUh+3B TFOss+lN2EiU/yqATgnGbKTFqR/jVAd/eJ2p5I6JW45oYp7d0yu4ylJhRKNR0cOsf1 jbK2pSH0VBpgA== From: "David Hildenbrand (Arm)" Date: Fri, 04 Dec 2026 13:50:17 +0100 Subject: [PATCH v2 26/31] mm/ksm: copy KSM folios at swap-in without VM_MERGEABLE 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-26-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)" , Chris Mason , stable@vger.kernel.org X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3068; i=david@kernel.org; h=from:subject:message-id; bh=ssf4RXLmG1C0Xl99sL7iuJb6mwurCB03ik5rfghLxUM=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLKFtjNy8TfUaSj++mWXIrFtt62fFX/WqfSNi8OPPn162 n7x5GMpHaUsDGIcDLJiiixBwfcNXr5cV1vtxscMM4eVCWQIAxenAExE5z7DP40X81Vez21pf8xw 5nZ19gTHh689Vj27cOhuD+eupS+yLK8x/NN9tId/i23fzZmz9q7ZE2t83uh/wsz6trYjq+6Ihep vbeADAA== 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 ] Tested-by: Gregory Price (Meta) Reviewed-by: Gregory Price (Meta) 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