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=1795947398; b=OvtHJ+8umS9crFnSkWfL8xPeVtGIzQrWHNW9V6LpEoK7MTiv7GaBOUcZsWxYv432eyhR KYmfGDSnG1Ub0HC4s/KEC30XYZEaRTO7Z4SzMGVeX9mlAwykxi1mPcquigUOrQ6ipjhFD ps1ht2J4vERwXvo/48wEyQ1DOMHh00g6Q2S57GZSPrSSjLckYaiG0Nu61F9jvEZHZNjVv CvzRJaB0UfyMiBjtWbv74rKiJAbzawwPxFkf3KGfyM0xoIR494tded14vLhqdKhHVtiF4 nrHBOKOuJBEL9ARu7+frM0oX3utE4SFO0i9sUAfuKVfNWaio22JLrI4cu//XXbeO7ew== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795947398; 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=zlGpAs4urz2HHLNzM2+ms1IEwdLG7JDtS9OiHTkWYI8=; b=49BxIFXUcfeSK6zln3F7Mx426v0q2NV96rf89JvIOKbcso6f3VUMvSBbL6+TM/pb5ENl j/l1Aid52Kvm+sLhcJ3s+4M1+sAm5f8eRhhSIM1f1XxOGLooHeYkccXxgp42xoYeQm5CI b/opuXUOKK8i0GGKl0BpjBikzIf7nC1f2yKKCjNnetUI+FE/1MljqGfDS8OHJ+5oWGrLg 04XYJXFdC62Wdk1b91xnyWSX8DM6VvUnM3rlK86n5lRqelJ/WgwDTVTgchPcNXC4OoX/A XpwXC5YKM0H1sVGuPz2UdHyDlHqqDzBnsnISjgbtR+aLi/nKUI6wvIFkT5/ObyV/c5w== 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=fcgU2FTN; 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 CB18E606A31C; Sun, 29 Nov 2026 10:16:38 +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=fcgU2FTN Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3EBE11F00898; Sun, 29 Nov 2026 10:16:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795947398; bh=zlGpAs4urz2HHLNzM2+ms1IEwdLG7JDtS9OiHTkWYI8=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=fcgU2FTNXXyLJ9KlW8+koEAhK4WZE1l8KXt6Kfy0oFYR3tWnd/a2QG1widbk4TZtr /I6u6iRVq5ksojfAcCyKL1yEdfzCwAsalSIyGylzYPQnGOkMPuVI9SDh1mUCv7dCc6 FH7sOsDaLH61KOFBpYhyCghPvGSD/zf2cUnYDMgXWKIGOHrTC7F9nbN7ub8UnlvuNE xxgLE7cvbJLb0vmLanxrJd3KnYlISo80gmHw8IO6vSS1+bMhl62m1wR2SombefiOCa Z/4cf4V0oTq1Oua3qMLYX/88vBgV3kEQcdsgroQeU7InpiPoMVqHz57E4zcbACjTBX giG/S67Bu+i7A== From: "David Hildenbrand (Arm)" Date: Sun, 29 Nov 2026 11:07:02 +0100 Subject: [PATCH 31/32] mm/migrate_device: don't collect KSM folios 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 Message-Id: <20261129-fixes_core_3-v1-31-f66094116801@kernel.org> References: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> In-Reply-To: <20261129-fixes_core_3-v1-0-f66094116801@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=2628; i=david@kernel.org; h=from:subject:message-id; bh=BLfe9onF3oAU+UID3FNzbdsyK/SCyd41Rqu2586iBlc=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLK5f+rZCr0/einO/Or0TKl99Z6qD/5/3WalHlh8qrmky /+CnsOMjlIWBjEOBlkxRZag4PsGL1+uq61242OGmcPKBDKEgYtTACbSKs7w3+vErLOsUp6fzvpe 4Zv6qudhsvG+Ste/a6Xcp2xKOmfIt5DhF/MbD551t+Y/c59UmfD+fKB/18vL+2Yp8h+99Nm+YYe oJxsA 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 ] 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 c6473f2e5388b..f98ea2291d532 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -418,6 +418,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