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=1795623949; b=DlHiDcccGNc0UEaBVAZodEQSM9jZSUPfgkPlPiorI8SbJE8W11KAf7F9Ft4aGGhtji5u CpvZyYmxksMh2iQVhxo0gAWyPWvYbPvke8E2sxBAxw/qYQEJaNpKRjSqsdtbHdvsJNrFq Mbr3CoyDsCy4F2JIdsaRm5BrH/iJ5LNFyCjbHyHVYg//4JEPx2Ly2HZCmLiQcSdbB89/7 8IbXYUVx9Kaouq4t5cWoCdFN6YAZIC/b/AiNlS1zlpzonejjDzIlCdwzkqg2tyU0+/uJP 3z1mVil0BiQg4R/geNCrq1Qm212bk2yJxmOd44uCHLROmXICjWbgfKtGaqgdyhcgH9g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795623949; 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=tpQb+hU5y0NBWR9nmzywwcgcGXeIDeB6Yy3jJgTMe5c=; b=RmcNmcAbwyDAHYJabdpyDSep6XhmS8vfcp7/9f9JEHZiwU71z2qumHzKMVRRSGjB73uP 1CuXboD7OnPvYdndC36tBpkzw9IMS8LrOdILrAYWWKXaD/nFutVApIaZw0ZknyuJCoB/o m0AY129FIjzDZoDMORb8EhVPs4Fz6JPjBPIcu6p8Qvf4nF/0oqoh2SbDuCNUVt29VaCp4 JG8Q70ZpL53rONso8CfzYDCacihP8pMVCE82BEY+k7EejKTyDheXwNpneagUYKcRYWaN0 ngoJ9j3/xuDP4SfQpEMU/eCJJatVr22saWAGqr3VpVXid0JmhmaVeHTdI10sk1dM04A== 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=rWjavjen; 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 B8EE9606A321; Wed, 25 Nov 2026 16:25:49 +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=rWjavjen Received: by smtp.kernel.org (Postfix) with ESMTPSA id DCFD11F000C4; Wed, 25 Nov 2026 16:25:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795623949; bh=tpQb+hU5y0NBWR9nmzywwcgcGXeIDeB6Yy3jJgTMe5c=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=rWjavjent79NQRZVpXH83JRwltYJprI/N8XXoKJ2AIvhloDFWZXr9OGRh09qJu2OF ZicjffSP46W/lKA0IIMa3IRL1qtJxOKoFpGrBBQ0mfOwf/P4YbjNnCvDAT48mSS9ox G+2yTDG8TAfhq41Ia/K92zMUPPyopNS+uRLHDoq8= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Chris Mason , Pedro Falcato , "Gregory Price (Meta)" , Muhammad Usama Anjum , Lance Yang , Baolin Wang , "David Hildenbrand (Arm)" , Sasha Levin Subject: [PATCH 6.12 003/816] mm/khugepaged: preserve dirty bits when collapsing PTE-mapped THPs Date: Wed, 25 Nov 2026 15:25:51 +0100 Message-ID: <20261125143849.295126443@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261125143849.205329330@linuxfoundation.org> References: <20261125143849.205329330@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.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Chris Mason [ Upstream commit fe2817e3d5ea347214342c9a5cd8bcbff20c0574 ] try_collapse_pte_mapped_thp() clears PTEs without transferring their dirty bits to the folio. A shmem read fault can map a clean folio writable, so a later store only dirties the PTE. Losing that dirty bit allows reclaim to discard modified data. The assumption that regular files are read-only is also obsolete since commit 1b6444331bebb ("mm/khugepaged: enable clean pagecache folio collapse for writable files"). In any case, transferring PTE dirty bits to the folio when removing mappings (and while the folio is still mapped) is the right thing to do. Use get_and_clear_ptes() to collect the dirty bits and mark the folio dirty before unmapping it. Update the comment. This was reproduced with shmem using MADV_COLLAPSE followed by MADV_PAGEOUT. Fix generated by an LLM and sent by Chris Mason in private. Fixes: 27e1f8273113 ("khugepaged: enable collapse pmd for pte-mapped THP") Fixes: 1b6444331bebb ("mm/khugepaged: enable clean pagecache folio collapse for writable files") Cc: stable@vger.kernel.org Signed-off-by: Chris Mason [ david: adjust comment; simplify description; cover file-thp as well ] Reviewed-by: Pedro Falcato Reviewed-by: Gregory Price (Meta) Tested-by: Gregory Price (Meta) Tested-by: Muhammad Usama Anjum Link: https://patch.msgid.link/20261031-fixes_core-v3-17-d6437171ecc3@kernel.org Reviewed-by: Lance Yang Reviewed-by: Baolin Wang Signed-off-by: David Hildenbrand (Arm) [ sashal: Adapted batched get_and_clear_ptes() handling to the existing single-PTE loop using ptep_get_and_clear(). ] Signed-off-by: Sasha Levin --- mm/khugepaged.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 9b8021d440289..ea94f66cd0377 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1636,9 +1636,10 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, /* * Must clear entry, or a racing truncate may re-remove it. * TLB flush can be left until pmdp_collapse_flush() does it. - * PTE dirty? Shmem page is already dirty; file is read-only. */ - ptep_clear(mm, addr, pte); + ptent = ptep_get_and_clear(mm, addr, pte); + if (pte_dirty(ptent)) + folio_mark_dirty(folio); folio_remove_rmap_pte(folio, page, vma); nr_ptes++; } -- 2.53.0