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=1796389172; b=ofppef9+qgNZmBbO4uqQmx+2JdWUAbtXgG3kG4robF6Ivc9Mshxskj/fbzb1B+fOwHVD 1cSBsk0ogSrYbhxtPmn6Ufyy2gdtBxjWmiU+A3wsB5+4ddFwKm7XVH9UvE4qG+39MsBiD PY4xen3v1o/KDr+/v4iBBnxEsMXwogNAdJ5O0diX35K3XuKA4FbvXtMi/u5hoX99W0uTY DiDRGPbmzMLiCkCpfKauPYNaXIPV5txVBc4rfzPT+CNTOiltuAV1xcxMaR1kz5hSehpmT dEPK66IvjB13hLPcp+KoyMmaBYTZQ6a/jjpx3ZBVZfjORwLxs1a5ma/DR9vKzGRdmgQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796389172; 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=fUTFvM/cgClcyS9vZr3s3oKInowWtxQf5dzlwbRAkRg=; b=cTm8MpVrB6SoYQx5QovmvucN67vuOUFLINAxQ9s8L0ABQ/KROoKyXZmR9RL3j6zXzefU 68+BuiXfDlCbyp2GyGxjDZophpN2/fruc+q90/qeSGQ2mwDl53dfboJig6YitDEJN84ua EzhQqc5CpHCy+CU16fsaiNYWsW4wFJVctGwRTPyyNquapkJKieR7SXGXVrwa5LmzPaj1k hxmRzAltGj4rIQDQX1S9g771lrUnO0p/t9pH6Dodb8ukXBiM8BBNsUsb3TERhPixl37AV OXC5gO2hK3ceKpgiXzNVK1v9MFrzgndIZDpd377wdKuS3XnEbvESWvsKSr6+e7PGxOw== 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=Uk4FVoj1; 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 B93E5606A32D; Fri, 4 Dec 2026 12:59:32 +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=Uk4FVoj1 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2DD221F0093A; Fri, 4 Dec 2026 12:59:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796389171; bh=fUTFvM/cgClcyS9vZr3s3oKInowWtxQf5dzlwbRAkRg=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=Uk4FVoj1ee7v+vBSweprYkXulIDJoKWULVvUGFNg0330WJNhvaz48Ju9PvrdkNnCm 3ZSdTQQAbDPbXHepAH23RJANepiJQArSZKDZQRTCLgiANbgddDtbkqzqNDWAjaO1cT 2JjMsmKuuCkerg1zygGOywY44I2ezSwZ3ej6niAhnohtkFUAYK1fmy+v+TWwvxZqId dy73X+uGVu2PyN6rxI+NI86OT8dRm9ki/pB9cc8KA2KybgIkk3eHQL+yLfLgOB085Z /8WgxReQdFTwFYWMf+nbKvhI+vZFr5gEmRgBlVuwdCk+ZURkpc5dJI31ohRGRJYFbO DKVrsWM8rZA0A== From: "David Hildenbrand (Arm)" Date: Fri, 04 Dec 2026 13:50:21 +0100 Subject: [PATCH v2 30/31] mm/migrate_device: don't collect KSM folios 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-30-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=2832; i=david@kernel.org; h=from:subject:message-id; bh=CvwW4G9xrZ2EqloLfBbEJ6aFTPNt7Qz32/wO0eFCyec=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLKFtjO+a8tIT7pydc3/GWF5sae/WVzz+1acHs0yX+r9j 3kGErf4OkpZGMQ4GGTFFFmCgu8bvHy5rrbajY8ZZg4rE8gQBi5OAZhIRggjQ2uKW/qyfeb1W/lf Wv9gaRX+Vf/v/ckNLO9Zdd/qzlrP7M7I8GJ+UPLKr65ujAx31rnLXjz131u26liYw9OnvHNvbl9 xhh8A X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A From: Chris Mason KSM can add page table mappings to a KSM folio without holding the folio lock, and only makes them visible to rmap walks afterwards, when adding the rmap_item to the stable node in stable_tree_append(). migrate_vma_collect_pmd() finds such a fresh mapping through its page table walk, locks the folio and replaces the PTE by a migration entry. But when restoring the migration entries, remove_migration_ptes() relies on an rmap walk, and rmap_walk_ksm() might not yet find that PTE: ksmd migrate_vma_setup() ==== =================== try_to_merge_with_ksm_page() replace_page() PTE maps kfolio migrate_vma_collect_pmd() folio_trylock(kfolio) PTE = migration entry migrate_device_unmap() remove_migration_ptes() rmap_walk_ksm() misses it folio_unlock(kfolio) folio_lock(kfolio) stable_tree_append() Consequently, we can leave a stale migration entry behind. If migration succeeded, the migration entry will refer to the freed source folio, and waiting on that migration entry will trigger a use-after-free. Let's fix it by not collecting KSM folios in migrate_vma_collect_pmd(). This has medium urgency, as it can result in a use-after-free, but might be hard to trigger in practice. Fixes: 8c3328f1f36a ("mm/migrate: migrate_vma() unmap page from vma while collecting pages") Cc: stable@vger.kernel.org Signed-off-by: Chris Mason [ david: clarify comment; simplify and clarify description ] Acked-by: Balbir Singh Tested-by: Gregory Price (Meta) Reviewed-by: Gregory Price (Meta) Reviewed-by: Alistair Popple Signed-off-by: David Hildenbrand (Arm) --- mm/migrate_device.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 964549222ed7c..40deb7722e8ac 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -411,6 +411,18 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, bool anon_exclusive; pte_t swp_pte; + /* + * rmap_walk_ksm() may not find this PTE to restore + * the migration entry later. + */ + if (folio_test_ksm(folio)) { + if (fault_folio != folio) + folio_unlock(folio); + folio_put(folio); + mpfn = 0; + goto next; + } + if (pte_present(pte)) flush_cache_page(vma, addr, pte_pfn(pte)); anon_exclusive = folio_test_anon(folio) && -- 2.56.1