From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.222.174 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796137026; b=FfbOG6CYTaI8xnWSEuIa2BaRhLGyDJHz1MdfLwB9vQY7suR2HHy5s8QonJmvm9pjFzvX vPNAF6NLzYeLKS4pqFd4PYXrmyZAdvZ/ZHpFagW3BnEJOcbfQQd3q6TQtni2/JZrRuNJt ozs+FDkpqwxN1CRVwyq8IQbYLA/ifZ9/hl8DxwmyVHaA4R5AK3lxOVwXyR44DoqO4Qzqi URATuL1k0Q/xpseiWszuEphRfWynWJ4oBkCtaLtNIvWDGVmLEvOm/Avre96LBtHhTKVsm 40wVqWIQ1XegtJAk9Ch1BghzAZ8tm8O3SAGpidyws3nwMNknZS6xR+ebvAajiJraFog== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796137026; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: MIME-Version:Content-Type:Content-Transfer-Encoding:Subject:From:To: Cc:In-Reply-To:References:Date:Message-Id:X-Mailer; bh=XEfF+0S01mFizWwmMDSGUtkkqNWT9IjeYcaH6xu72wo=; b=s6/o8fXtNvbBOR7RK0xt9HiKlEIrU3EPDsZ05sZMBqBPgwmWO5mHBbKu8sIu6QD2IJOA kHC+RnNMm24LE8mWrzUk560AdCqio3OfpjvW7hP1+aVH8cV9yFfhlzbh3bGc8s9Fr5fIi gl+Xd+Nxf6fnA/a/yd55Td04JCSW8pAaAg9clSRl/t1e3e+WbCrmFz8nx4L4j9SzzLPfZ wmbBnQ40EXFoV1rRvDadWV4UuhZz9DiAuHD+xQTopEN/syYtp0sjXgWKB2JAMG2c4D8p1 mmv89Td8qqWL6Y6DMW8JGI/5lEmBabePtuvaITON53Sfo+C7rxzEzR7ySGmTHsPSG7Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=gourry.net; spf=pass smtp.mailfrom=gourry.net; dkim=pass header.d=gourry.net header.i=@gourry.net header.a=rsa-sha256 header.s=google header.b=kpF2Mq7n; arc=none smtp.remote-ip=209.85.222.174 Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 81B36606A329 for ; Tue, 1 Dec 2026 14:57:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 81B36606A329 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gourry.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gourry.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gourry.net header.i=@gourry.net header.a=rsa-sha256 header.s=google header.b=kpF2Mq7n Received: by mail-qk1-f174.google.com with SMTP id af79cd13be357-949680e6d83so1101310385a.3 for ; Tue, 01 Dec 2026 06:57:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gourry.net; s=google; t=1796137024; x=1796741824; darn=vger.kernel.org; h=message-id:date:references:in-reply-to:cc:to:from:subject :content-transfer-encoding:content-type:mime-version:from:to:cc :subject:date:message-id:reply-to:content-type; bh=XEfF+0S01mFizWwmMDSGUtkkqNWT9IjeYcaH6xu72wo=; b=kpF2Mq7nESFFrj74IhLObCUH7qlCca2WINP+WZJWkdpfBr6uQuP3vlF7RMVGL64sFg XdxxUWRYb7NcrBYPmAFw6RApsIvx+0Flb5QAgGtIQeQ8d44HU3NhHPBSCyPK1/CplHqW BgVfSlL+jYP2rXgCyI7WTHBYkHP6QBd66X1b5FDMEKo5BqpNgXgyiYanuB7ngBN/DXli zKRfKJsg2NJvU6Mo95ToID07XfoPA0ztjpKWkBnKarwnkEEh7SlK0KtVxcmdt3V5iKpF lGrRQnrcZElfK35+kDu9qAuas90PP8nGvAwcQFvUsYILPSSM7HmZREykX0jSAwSUWq0p A1zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796137024; x=1796741824; h=message-id:date:references:in-reply-to:cc:to:from:subject :content-transfer-encoding:content-type:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=XEfF+0S01mFizWwmMDSGUtkkqNWT9IjeYcaH6xu72wo=; b=pwZog/R3n+TpwRr+OA3iBmS48M7lQGnvUJshDuPADmKB3M8QDDaONXPV+ddN8DEkmP 8hGgNoGpY8IliGCJSZR6BjQmp1a69JRW7z5QjmWgYxfRNp9sNUQBmRqsEt5HMPfjoTp4 68OlvTEwcgOQrddOhNtlDPO3b0utsmMwSKGOPLKkL03kbS/zS9VNlM2Noj7UVWCWVv6+ 0Ea4xcfb4Nz84ooLktH+UAlUxuyufGYg/ejvWvcEhW4X/3CY+GdzZASAW70nPiaXxDnl npHxIwadXnnVbQAyD+bjav261XB44qMQaCsV2zNSurXElErz0cuRrMVxSYPuQUVdKsQ4 suXQ== X-Forwarded-Encrypted: i=1; AL+hErG8p3Yx/9oGqrMjBznlQ74L7DnX5lXK2Y+dmdUID62LqhRcJvlbBKdlPdGwDLBy2f0fG7oFRxPDEctb9UI=@vger.kernel.org X-Gm-Message-State: APuU4Z+UpsfWEGUBpZ4o5ks32r9E2F8St43YOzVRVYR1Wevq6D1zQV4G zqNli2yDm93mfC0Yaka6f2dH0BmmWw/eBMl2Z/hW5qT283NXE0DQVhYHzhCipoQe0G4= X-Gm-Gg: AZFdTZeMSdgwP2C9VLYxfNEGat1/eN3zEAcb9DQsbb3T6O/E1/kZ9I5kfwxHtwlNCMC pepjTpBqLu85iLmZb3bu6dFCl90He521972fz/o8l4obeOgEX+o34D1kUB2PECth83djWgNNE3C L9dSA/LSVc0M6T8BaQInxN08sEPOQNXLa/vNqZPdixnpVECvbkM9cO5fcG8VyLhL8e8tKyBZYa+ 2pxyR24Edo34t3NnpMHkVlp33W/L86jd+JnrYCpZyteVI8SaYR8GVnqKL4xdNGNd3yCO6FDWptf Z6hhfn569bVajfnPvFTXPB4snxVqXhhpIwm8LaFwS0d+pCr5fq+gGjh5QQngmHC/amQoMxTVi4A yLw/iSZ22xZh4lrqbM9/KIT7/DknISNMVKbTaqtq5KIAL+XfyJw+vEMa1/at/O9eMLBkPoN7exO WiIi2lqItqN1Qa97Tb1JiZQLgv8bq8lb6mqP7i8lPkn+P3NKyl+eIhfrPrB8uapVC6fM7NHImE+ C5RwTIXMyhKMaS/hKVyunZEABTfidMiLdWG5qYbWC74GtTn/dWfWZsP8NkHWocYUNZfWWze/xjX uGywLzt632A84Bwjpv/ZaiHzxO6RsxNqvS5JLqpRzE4FCLXjI9ONZdGe9KSQBXrT1V409LARrjL sIYSDwljGaGBtVwnNEkYig5gW9vWn X-Received: by 2002:a05:620a:700e:b0:949:735c:90c4 with SMTP id af79cd13be357-949bab1df97mr6514796685a.70.1796137024391; Tue, 01 Dec 2026 06:57:04 -0800 (PST) Received: from [192.168.50.231] (pool-71-191-145-202.washdc.fios.verizon.net. [71.191.145.202]) by smtp.gmail.com with ESMTPSA id af79cd13be357-94a0175c67fsm1824728485a.31.2026.12.01.06.56.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 06:57:03 -0800 (PST) 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 Subject: Re: [PATCH 27/32] mm/ksm: copy KSM folios at swap-in without VM_MERGEABLE From: "Gregory Price (Meta)" 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 , 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 , 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 In-Reply-To: <20261129-fixes_core_3-v1-27-f66094116801@kernel.org> References: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> <20261129-fixes_core_3-v1-27-f66094116801@kernel.org> Date: Tue, 01 Dec 2026 09:56:54 -0500 Message-Id: <179613701470.412063.16914787842119276918.b4-reply@b4> X-Mailer: b4 0.17-dev On 2026-11-29 11:06 +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 ] > Signed-off-by: David Hildenbrand (Arm) I could reproduce a stale KPF_KSM flag through some convoluted interactions with vmsplice, swap, and toggling ksm. This patch does fix that. In theory this could generate a UAF or the hang as suggested, but I couldn't find a way to reproduce that. This otherwise does seem fine. I am now a little worried that toggling KSM can generate some other degenerate race conditions that aren't otherwise covered (haven't looked at the other ksm patches yet). Tested-by: Gregory Price (Meta) Reviewed-by: Gregory Price (Meta) -- ~Gregory