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 6736D606A330 for ; Fri, 4 Dec 2026 13:00:24 +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 kanga.kvack.org (Postfix) id 7B5636B05E4; Fri, 4 Dec 2026 08:00:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 78C856B05E5; Fri, 4 Dec 2026 08:00:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6A2816B05E6; Fri, 4 Dec 2026 08:00:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 3E7046B05E4 for ; Fri, 4 Dec 2026 08:00:23 -0500 (EST) Received: from smtpin25.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C43BAA7ECE for ; Fri, 4 Dec 2026 13:00:22 +0000 (UTC) X-FDA: 85506309084.25.8158C2F Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf14.hostedemail.com (Postfix) with ESMTP id 1AB5E100016 for ; Fri, 4 Dec 2026 13:00:20 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=lV2L1jgK; spf=pass (imf14.hostedemail.com: domain of ljs@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=ljs@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=1796389221; b=QI/ETYnFh+i9RF/5Vk+lj63gz/iZTh6apmmTthJnWtXtKXjRilyjKojWtoGrQJtfy2vuqK 16a4q/ZH1k/dGO+Mdof4hCrn2H5iFx6WWpM7OKgL2ht0Gj9VO0SZ9PvKBcXjZpJHWPNBsf sS0w2cve732U39nhxu5l60QjP/5ZGtE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796389221; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=374rojk9gTR7v2FgxImu66R0mEYxmLFttT2IpvB5N4o=; b=LLXFXx9vmq9KbO79vUHDqGp6yN94bNRudS8lZgjQX3EJRffOVekDp3zISCkQqSwSeDAKyx 7pS2tzYi5i8bCf9SoprUNaaIW4aDH1gEYTUOlSMv2qnpBOeSpENLzWh87812BhMOuJDfJs eSXaojnUNxIjNWXQgm1Oyp/GDwwDYLc= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=lV2L1jgK; spf=pass (imf14.hostedemail.com: domain of ljs@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=ljs@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 607A744EC6; Fri, 4 Dec 2026 13:00:20 +0000 (UTC) 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-Archived-By: 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> X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 1AB5E100016 X-Rspam-User: X-Stat-Signature: sh5imxas1wd3zqiu6mekin5ckrekioxr X-Spam-Status: No, score=-16.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 autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796389220-386207 X-HE-Meta: U2FsdGVkX18kjzxBaGOJ1F73rjhznZVhZi21EWgR8wpd72fPFidP77m2H6Ys8YvUGyQ3UdMKcS9W4U/sPKLkw+qqNIWhF8ViYOCOJb96UIifwtoDdA3TDkICnUR1PvWPRfNv1Gv0wcFOQJUK86TSrA/hfZ5eOc/DEo9hjgTLp+f4fn1UfOdwIOfjDvqR8VeSonlkvbZGZ3FHD1WmX52rBOQ/trPRH002qp0CPrbpX5ZiTTG2GRj8Bw20nLvP7yiMPPJRkFqwibZJyfTUS7KwxzIq29u+xumEaV2cRMyXhcVdQf92yTl2Q/jzmxzSZ6ZBMc9xzni/DKcyn/CcrJTtJlU36Z8xnjv5WWY06u9y71P44WYA00oTv3imo/JR8pTehbiqIXZaxNgDJz4MEJ/SMG8vzyp+P2/XIYc+7IjFut6sQYSF45zVhgXlH/w88ef0GYH/qTT8X4mM262YMH3KwK6hRqYasQtkrB7PphTWFjKCMNXKGC6rD1niCAttTwq66H9qTyruEI9au/4p1L1hLUSwN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aH9EXj3P5SCj4J+M20xu2F58vdlnO1H4EedQmlI0o7tti3Rsi7aK/4NhoC3fmiJOX6+WgBPyQzmxWcjhg== 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 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