From: Sasha Levin <sashal@kernel.org>
To: stable@vger.kernel.org
Cc: Chris Mason <mason@kernel.org>, Pedro Falcato <pfalcato@suse.de>,
"Gregory Price (Meta)" <gourry@gourry.net>,
Muhammad Usama Anjum <usama.anjum@arm.com>,
Lance Yang <lance.yang@linux.dev>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
"David Hildenbrand (Arm)" <david@kernel.org>,
Sasha Levin <sashal@kernel.org>
Subject: [PATCH 5.15.y] mm/khugepaged: preserve dirty bits when collapsing PTE-mapped THPs
Date: Sun, 15 Nov 2026 21:32:10 -0500 [thread overview]
Message-ID: <20261116023141.2370512-1-sashal@kernel.org> (raw)
In-Reply-To: <20261109202315-failed-5.15-fe2817e3d5ea@kernel.org>
From: Chris Mason <mason@kernel.org>
[ 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 <mason@kernel.org>
[ david: adjust comment; simplify description; cover file-thp as well ]
Reviewed-by: Pedro Falcato <pfalcato@suse.de>
Reviewed-by: Gregory Price (Meta) <gourry@gourry.net>
Tested-by: Gregory Price (Meta) <gourry@gourry.net>
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Link: https://patch.msgid.link/20261031-fixes_core-v3-17-d6437171ecc3@kernel.org
Reviewed-by: Lance Yang <lance.yang@linux.dev>
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
[ sashal: adapted batched PTE operations using folios to individual PTE operations using struct page ]
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
mm/khugepaged.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index a7a8e6bc70b01..dfe8f42b35ad5 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -1517,10 +1517,14 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr)
for (i = 0, addr = haddr, pte = start_pte;
i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
struct page *page;
+ pte_t ptent;
if (pte_none(*pte))
continue;
page = vm_normal_page(vma, addr, *pte);
+ ptent = ptep_get_and_clear(mm, addr, pte);
+ if (pte_dirty(ptent))
+ set_page_dirty(hpage);
page_remove_rmap(page, false);
}
--
2.53.0
prev parent reply other threads:[~2026-11-16 2:32 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-10 1:23 FAILED: patch "[PATCH] mm/khugepaged: preserve dirty bits when collapsing PTE-mapped THPs" failed to apply to 5.15-stable tree Sasha Levin
2026-11-16 2:32 ` Sasha Levin [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261116023141.2370512-1-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=david@kernel.org \
--cc=gourry@gourry.net \
--cc=lance.yang@linux.dev \
--cc=mason@kernel.org \
--cc=pfalcato@suse.de \
--cc=stable@vger.kernel.org \
--cc=usama.anjum@arm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox