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=1796475606; b=TdGervjZiLDJNQog5wsjYZAIGdB0IQADQyOCb7sERYoFNFcowc8r6fLPHJV7yvdZrKEz AJZDwzCGi+q99LEdfprN4oiBk4g0TTvGCdER2Wl/34Sm23IZ782h29s7Ohpw0qzmAPNJo wCvsftctOC4WrKQOEXzjNyxlURF9cHlAbJ2OvrEHz5eflYHknAxCMU8ljVM+a1zY8kLua nqKtcPf9Y/SGbTmb+kUQzWViNB2GiFkLp7yqJtQh86NebxuW5fzuXGBvdMNsvKRJr0YRc LH4Nw3xa0g70eKiEoU5o8VJxVHZYMTQ3q5jlM9liCB0uo9AJ4Uoo+iW0mjXzIo0EdyQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796475606; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=51PxdaM8Tmq68MUBLr6yt6+V+NMT3LqyOW49/p4K4Kg=; b=g0DGW6Q6RXoxSaxqlDAi8dIev4ORrYOlAwRLGGMPf7H9HaRxSDNpJCn70xb5TRuVT5JJ ukD38jN7o9bywOr8hUA4Z/23vmyG9EYGQbfwd41cwPsuJo1vH7Lyk4P/RXK4MlRyx2qDH SmV2MrEztNm1YeMG2jQE4WyrSVbsR+Vl8IHP88hXyuOr4MV9K2mrTIXkwTtt90RDeLv9W 94bXYzRmJjaWLL1TRWYvfhrCwuPkrGcO2FJ+2A/Vnoj2/IJAsccr/tAv+97zK2tLejj/v gqFVn38rheMeJYmssDlbFjtDHjnJ7atJpuHMBDHOMG8Gsq0fTWAVMdHNTrIffmvnwCg== 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=csWPwYHO; 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 50F96606A336; Sat, 5 Dec 2026 13:00:06 +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=csWPwYHO Received: by smtp.kernel.org (Postfix) with ESMTPSA id F060E1F00A15; Sat, 5 Dec 2026 13:00:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796475605; bh=51PxdaM8Tmq68MUBLr6yt6+V+NMT3LqyOW49/p4K4Kg=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=csWPwYHO6Kptegp1z67mtdh7820xb4IAiTAMc7e//ICyKcqSbR8AMu5FvbCfTHVpE 7WsiTDMfiO2w6XNCUrkW+JVzL3Or27ydCTSLXjb9mniTbYVAerXm7RyE4zyqxRSkYR qhstIjPoRf7yTKYPFie4kTkviokLmsSrpSY6WM+5HH9XaMf0Uy0BVB341xIKbrXNAw uiucD/GkwyZIV87jLsBX0AK7Fk9y0Sgnehnlt2tl2TlTq5nTifogUNajyB1M65F18X zS6unK3H0xzK83jZzXnk5tSV0IETDmBn2YyK5hq0HMnvdNKX0LfuvDOjlZFZdcPBvI BVEn3kltquy7w== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v2 28/31] mm/ksm: use a migration counter to detect KSM folio migration Reply-To: sashiko-reviews@lists.linux.dev To: =?utf-8?b?RGF2aWQgSGlsZGVuYnJhbmQgKEFybSk=?= , "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" , =?utf-8?b?WmFjaCBPJ0tlZWZl?= , "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-cxl@vger.kernel.org, linux-rt-devel@lists.linux.dev, "SJ Park" , "Chris Mason" , linux-kernel@vger.kernel.org In-Reply-To: <20261204-fixes_core_3-v2-28-9aad65727632@kernel.org> References: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> <20261204-fixes_core_3-v2-28-9aad65727632@kernel.org> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sat, 05 Dec 2026 13:00:02 +0000 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm/ksm: torn 64-bit kpfn_seq in ksm_get_folio() breaks 32-bit SMP safety -- commit 121b2ee344a6ed62766dc4e36462c4e689247791 Author: David Hildenbrand (Arm) mm/ksm: use a migration counter to detect KSM folio migration This commit converts stable_node->kpfn into a u64 kpfn_seq field that packs a migration counter into the upper bits so ksm_get_folio() can detect a KSM folio that migrated away and back to the same PFN. > diff --git a/mm/ksm.c b/mm/ksm.c > index fe7b3b60540db..33c270aae3285 100644 > --- a/mm/ksm.c > +++ b/mm/ksm.c [ ... ] > @@ -969,13 +1009,13 @@ static struct folio *ksm_get_folio(struct ksm_stab= le_node *stable_node, > { > struct folio *folio; > void *expected_mapping; > - unsigned long kpfn; > + u64 kpfn_seq; > =20 > expected_mapping =3D (void *)((unsigned long)stable_node | > FOLIO_MAPPING_KSM); > again: > - kpfn =3D READ_ONCE(stable_node->kpfn); /* Address dependency. */ > - folio =3D pfn_folio(kpfn); > + kpfn_seq =3D READ_ONCE(stable_node->kpfn_seq); /* Address dependency. */ > + folio =3D pfn_folio(ksm_kpfn_pfn(kpfn_seq)); > if (READ_ONCE(folio->mapping) !=3D expected_mapping) > goto stale; > =20 > @@ -1031,10 +1071,12 @@ static struct folio *ksm_get_folio(struct ksm_sta= ble_node *stable_node, > * We come here from above when folio->mapping or the swapcache flag > * suggests that the node is stale; but it might be under migration. > * We need smp_rmb(), matching the smp_wmb() in folio_migrate_ksm(), > - * before checking whether node->kpfn has been changed. > + * before checking whether node->kpfn_seq has been changed. Due to the > + * migration counter, a ksm page that migrated back to the same pfn > + * changes node->kpfn_seq as well. > */ > smp_rmb(); > - if (READ_ONCE(stable_node->kpfn) !=3D kpfn) > + if (READ_ONCE(stable_node->kpfn_seq) !=3D kpfn_seq) > goto again; > remove_node_from_stable_tree(stable_node); > return NULL; [Severity: Medium] Can torn 64-bit accesses to stable_node->kpfn_seq on 32-bit SMP still cause ksm_get_folio() to free a live stable_node? CONFIG_KSM only depends on MMU, so it can be enabled on 32-bit SMP kernels where 64-bit READ_ONCE() and WRITE_ONCE() split into two 32-bit accesses (lo for the PFN, hi for the migration counter). With a low-word-first access order, two migrations back to the same PFN can produce the same torn value on both reads: CPU0 (ksm_get_folio) CPU1 (folio_migrate_ksm) /* kpfn_seq =3D (hi=3D0, lo=3DA) */ READ_ONCE(kpfn_seq): lo =3D A migrate A -> B: WRITE_ONCE: lo =3D B, hi =3D 1 smp_wmb() folio_set_stable_node(A, NULL) READ_ONCE(kpfn_seq): hi =3D 1 /* torn kpfn_seq =3D (1, A) */ folio->mapping !=3D expected goto stale; smp_rmb() migrate B -> A: folio(A)->mapping =3D expected WRITE_ONCE: lo =3D A /* (1, A) */ READ_ONCE(kpfn_seq) -> (1, A) /* matches torn kpfn_seq */ remove_node_from_stable_tree() free_stable_node() WRITE_ONCE: hi =3D 2 <-- write-after-f= ree This leaves the live mapped folio at A pointing to the freed stable_node, causing a write-after-free when CPU1 finishes the store and a use-after-free in callers such as rmap_walk_ksm(). On weakly-ordered 32-bit SMP, ksm_kpfn_pfn() also discards the upper 32-bit word when computing the folio address, so the address dependency orders only lo before READ_ONCE(folio->mapping); a delayed load of hi can observe hi =3D 2 from the second migration and match the stale re-read. Using atomic64_t (atomic64_read() / atomic64_set()) for kpfn_seq, or restricting CONFIG_KSM to depend on 64BIT, would avoid torn 64-bit accesses on 32-bit with no overhead on 64-bit. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261204-fixes_core= _3-v2-0-9aad65727632@kernel.org?part=3D28