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=1796389220; b=uyHJwb5yDDIlcq+4c4NT1y27Jw8sWr8HqWZ9G1M30H1nELfSxYfYekqZ2vObikN8bdm0 el/jPOWrq7z07n6vsOOvINJUrYTTXwlJsjXdKOfzF1X2QWPsgqdqIrZGoKWvRTMwkSx4n 8/buWVc2mfwV2dt0xGLUKNzli3epQASHy0jNxsal5N0/AHgTZ4GScM9qipg4P+rTk9kBZ q6FAhptCcf6JSqvD/dNfdY/VAtqUcgdFS0NLdlijSAlsy6tw7EgUZzi4892Bk0cBKWILj Q+uPshQ1QMSCBQNGz0o6yfsXYyn1g4bpu/5tLMyeardwz2clg5lsle/mDl+hs69oF7A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796389220; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=374rojk9gTR7v2FgxImu66R0mEYxmLFttT2IpvB5N4o=; b=v36KybTWW2njJUP+Sv56imA/aEqOkl4TV9bDSebTZ2sn7NP++7ENQV8/4bCgzXHQs6hq Hy3VFOQJzyqac4yN7cj4lf4LrbtcWWQJa/1sekPNyqYQF0Y3BGVvZXwomQ1BT0zqRCUbi aIiuGSvy4CjFYLFN+ZzmV/wMY4OTFWfeVWDE9K0uusEFDMhfo7rmkGbadnrcIN8QCzipe aBFkXxk6V8trDVfO1fKkuj496qLE0wdBc4tuRiuyfNeyqy3Ey+C3gWpbVl1VHphoHWRTE n8JJLojZUkwYqy38Q2kLAjg9xq4ZBVJFjyUmelfwWd+WSg+rQtDUCWrRA5SZKna9M2g== 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=lV2L1jgK; 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 66926606A332; Fri, 4 Dec 2026 13:00:20 +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=lV2L1jgK Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2442A1F000C2; Fri, 4 Dec 2026 13:00:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796389220; bh=374rojk9gTR7v2FgxImu66R0mEYxmLFttT2IpvB5N4o=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=lV2L1jgKjFnEE0vxVNwYzkkpG1Api1RXDl7ABqZ4YA7wWmD8mKyhc+1fdlvBOiaB6 Oi/g7+TxAv42O+uP3r/1HZmeRbIb3dsWjBVYoHXNLIGsp2u9zo6NWcSFQZmAwH5uJa hBKJZX28xnZiUz+2DnS+11cYDbnQ6FHTZQ90xl9oPr0BOr1aPaYA3JonkFjDPmf2VB q/gq+mWRgwEpjXDHK5HenmnC3owMQcUhfrxIJs5UHBelE6v8I/Rf18o4pysjRkOqTI ISZW9s0PHUQzybwy7Ravs5L9njjgPzgCDwbyG1y29LR+pLHz4tU7hbXRqaeQ6H2OTv jFdnxGx1jltqA== Date: Fri, 4 Dec 2026 13:00:00 +0000 From: "Lorenzo Stoakes (ARM)" To: "David Hildenbrand (Arm)" Cc: 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 , 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 , 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 , Chris Mason , stable@vger.kernel.org Subject: Re: [PATCH v2 26/31] mm/ksm: copy KSM folios at swap-in without VM_MERGEABLE Message-ID: References: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> <20261204-fixes_core_3-v2-26-9aad65727632@kernel.org> 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=us-ascii Content-Disposition: inline In-Reply-To: <20261204-fixes_core_3-v2-26-9aad65727632@kernel.org> On Fri, Dec 04, 2026 at 01:50:17PM +0100, David Hildenbrand (Arm) wrote: > 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) Oops send the tag while you were respinning I think, so: Reviewed-by: Lorenzo Stoakes (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 > -- Cheers, Lorenzo