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=1795038301; b=StUvHORe/V16vD6OkVhd3clD20SaIvRdAD9hJFs9GISVGWd+xEjh7AOsZFZnTTX6lrWy bLLe0r49JdrWasI8Z/6oB9IHT6oSX2xIs+OTe5FIdRjOrs900FoQDcLi9WE915vOLT4wE Tz4z2ne63BoBduFaifK9scVKp4LlyQY6Ctja9KKXoz88oSE2F6ByxIzrdb7EYFb5Bnz9j N0huIk4WbD2hYmVFKcN0qkgUMrJ77aofOsRWUF+UX7M03Q0C36eZiupcMRT+s/aTNl5YN iomR8ujgxGRmzNVgYPtankeIFkIube6megELVFMbExgz6QcfXnLhA1JWHE+oE1cCbVw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795038301; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=ISKmvrvPxEcCCwRCCvmWN+SSB5OFz3gObjGno/DGn9U=; b=arwhSRtAcG4bhFMLhHUU84tah7E/kGv0T7O0q9STYA61ETBZgLPvL3NspPzUEkDu9PX8 5ErH6PHZR8AP6ee3Q+MbHTNNo3l5baTt4L90pwHc+hl7aFCoACaYNeYiVozZTLHJiLYu5 viY/Vgc0QT/gnJoavcozQOf7SeHpaGw8lAJTe+HKZJjzoZNEt2KVUjlGF9JeaCfeLbYsX zoVE+u0KBfj+3h2nzt2o8TLHXw0snqZ+kRLG9KFJIFmmG2M8MZNKMlwRruzFKo+aTkDdQ RyMpsCoV9/C2njLnumnDMcFBx+ntYslH5TzZA0qUrsegi9qJNiTYSgICKgYDIVl6z9A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=vAtQjaEN; 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 60D0D606A32A; Wed, 18 Nov 2026 21:45:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=vAtQjaEN Received: by smtp.kernel.org (Postfix) with ESMTPSA id 779251F000C4; Wed, 18 Nov 2026 21:45:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795038301; bh=ISKmvrvPxEcCCwRCCvmWN+SSB5OFz3gObjGno/DGn9U=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=vAtQjaENDvIR8wBV/iKt/Ic620ZCKlRggA/UkEHwm6SNxVG0EE+ykuqZJbtdPPNmt Iz8S1pDPrSA3AIarBsxpYsXk5dIvJJA2kuuNqVhoWp24dlCtnqMuPmdXhPQusSbPgc q2DPjs+hM/sQfr7BiOELxAT9JWTDzDqcefMKq1bw= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Pedro Falcato , Andrew Morton , Samarth Narang , "Gregory Price (Meta)" , Zi Yan , "Lorenzo Stoakes (ARM)" , "David Hildenbrand (Arm)" , SJ Park , "Liam R. Howlett" , Vlastimil Babka , Jann Horn , Minchan Kim , Michal Hocko , Johannes Weiner , Sasha Levin Subject: [PATCH 6.6 1654/3318] mm/madvise: atomically clear the young bit on THP MADV_COLD Date: Wed, 18 Nov 2026 15:37:56 +0100 Message-ID: <20261118150411.387266621@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261118150313.527275416@linuxfoundation.org> References: <20261118150313.527275416@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Pedro Falcato [ Upstream commit 246c8cc33438a71c28f9b86baa492d9a0a130316 ] As-is, madvise_cold_or_pageout_pte_range() discards the hardware young bit by invalidating the PMD, doing pmd_mkold() and subsequently writing to the pmd again. However, it does so in a racy manner, looking at a possibly-stale snapshot. It's entirely possible that a write elsewhere set the hardware D bit, which this logic now loses. Fix it by using pmdp_test_and_clear_young(). This is an atomic modification of the PMD entry, and thus safe against any possible racing writes. As a (nice) side-effect, this elides one non-required TLB flush in pmdp_invalidate(). This bug can interact with the swapping system in complicated ways, leading to data loss. For instance, if a folio is marked lazyfree, but was later read from, and then MADV_COLD'd while racing a write to that same THP, dropping the dirty bit may lead to data loss as reclaim/MADV_PAGEOUT drops the "clean" anon folio: T0 T1 ================================================================ MADV_FREE(thp) | folio marked lazyfree | read(thp) | PMD marked young by hardware | | MADV_COLD(thp) write(thp) | if (pmd_young(pmd)) { PMD marked dirty by hardware | set(pmdp, pmd_mkold(pmd)); MADV_PAGEOUT/reclaim | dirty bit may or may not be | set | reclaim/pageout may drop | the folio | Link: https://lore.kernel.org/20261013191719.95502-1-pfalcato@suse.de Fixes: 9c276cc65a58 ("mm: introduce MADV_COLD") Signed-off-by: Pedro Falcato Signed-off-by: Andrew Morton Reported-by: Samarth Narang Closes: https://lore.kernel.org/linux-mm/20261011132227.413217-1-samarth.colleges@gmail.com/ Reviewed-by: Gregory Price (Meta) Acked-by: Zi Yan Reviewed-by: Lorenzo Stoakes (ARM) Acked-by: David Hildenbrand (Arm) Reviewed-by: SJ Park Cc: Liam R. Howlett Cc: Vlastimil Babka Cc: Jann Horn Cc: Minchan Kim Cc: Michal Hocko Cc: Johannes Weiner Cc: [ sashal: Retained pmd_young(orig_pmd) without the !pageout guard because the corresponding reclaim optimization is absent. ] Signed-off-by: Sasha Levin --- mm/madvise.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/mm/madvise.c b/mm/madvise.c index 209d0cc950139..e29bd73de12ae 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -404,10 +404,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, } if (pmd_young(orig_pmd)) { - pmdp_invalidate(vma, addr, pmd); - orig_pmd = pmd_mkold(orig_pmd); - - set_pmd_at(mm, addr, pmd, orig_pmd); + pmdp_test_and_clear_young(vma, addr, pmd); tlb_remove_pmd_tlb_entry(tlb, pmd, addr); } -- 2.53.0