Skip to contentSkip to conversation

mm/ksm: keep soft-dirty when merging a page

PATCH Applied Samarth Narang 4 participants 5 messages Oct 21 – Oct 26

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 mm/ksm: keep soft-dirty when merging a page

replace_page() maps the KSM page with a PTE built from scratch, so the soft-dirty bit of the PTE it replaces is lost. A page written since "clear_refs 4" then no longer shows as soft-dirty once it is merged, and a soft-dirty based dumper treats it as unchanged.

Copy the soft-dirty bit from the original PTE to the new PTE.

Fixes 0f8975ec4db2 ("mm: soft-dirty bits for user memory changes tracking")
Assisted-by LLM

---

Notes:
v2: https://lore.kernel.org/all/[email protected]/

Changes since v2:
- Keep only the soft-dirty fix, as David suggested; Lance will be handling the uffd-wp/RWP part.
- Fixes tag reduced to the soft-dirty commit.
- Dropped the Tested-by tag, since the patch changed.

 mm/ksm.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/ksm.c +4 -0
index dbfb8f98b189..910a1307ac9f 100644
@@ -1463,6 +1463,10 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,
 		dec_mm_counter(mm, MM_ANONPAGES);
 	}
 
+	/* Preserve soft-dirty when replacing the page. */
+	if (pte_soft_dirty(orig_pte))
+		newpte = pte_mksoft_dirty(newpte);
+
 	flush_cache_page(vma, addr, pte_pfn(ptep_get(ptep)));
 	/*
 	 * No need to notify as we are replacing a read only page with another

base-commit: fcec4f4bcc1101dbd7e3aff267856b3bbfe128c6

Signature · 1 line
2.53.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 5 lines · Samarth Narang

replace_page() maps the KSM page with a PTE built from scratch, so the soft-dirty bit of the PTE it replaces is lost. A page written since "clear_refs 4" then no longer shows as soft-dirty once it is merged, and a soft-dirty based dumper treats it as unchanged.

Quoted 7 lines

Copy the soft-dirty bit from the original PTE to the new PTE.

Fixes 0f8975ec4db2 ("mm: soft-dirty bits for user memory changes tracking")
Assisted-by LLM

---

LGTM!

maintainer
#2 lore raw
In reply to Samarth Narang
Quoted 6 lines · On Wed, 21 Oct 2026 11:21:57 -0700 Samarth Narang <[email protected]>…

replace_page() maps the KSM page with a PTE built from scratch, so the soft-dirty bit of the PTE it replaces is lost. A page written since "clear_refs 4" then no longer shows as soft-dirty once it is merged, and a soft-dirty based dumper treats it as unchanged.

Copy the soft-dirty bit from the original PTE to the new PTE.

Thanks. My LLM review thing says:

: There's a separate, pre-existing problem with the KSM zero-page : path which is worth pointing out.
:
: When use_zero_pages is enabled, replace_page() constructs the : replacement PTE using:
:
: newpte = pte_mkdirty(pte_mkspecial(pfn_pte(...)));
:
: The dirty bit is deliberately set so that KSM can distinguish its : own zero-page mappings from ordinary zero-page mappings. However, : on x86, pte_mkdirty() sets both _PAGE_DIRTY and _PAGE_SOFT_DIRTY. :
: Consequently, a process can clear its soft-dirty bits through : /proc/PID/clear_refs, perform no further writes, and subsequently : have a page marked soft-dirty simply because KSM merged an all-zero : page. Userspace then sees bit 55 set in /proc/PID/pagemap even though : the page hasn't been modified since tracking was reset.
:
: Your patch doesn't introduce this problem, but it doesn't fix it : either. The new pte_soft_dirty(orig_pte) check cannot help in the : zero-page case because pte_mkdirty() has already set the bit in : newpte.
:
: I don't think this should hold up v3. Losing soft-dirty when merging : ordinary pages is a more serious problem, since it can cause : incremental checkpointing to miss modified pages. The zero-page : case instead produces false positives, causing unnecessary copying. :
: It would be worth addressing separately, preserving KSM's internal : zero-page marker without manufacturing a soft-dirty indication.

#3 lore raw
Quoted 7 lines · Andrew Morton
Quoted 5 lines · On Wed, 21 Oct 2026 11:21:57 -0700 Samarth Narang <[email protected]>…

replace_page() maps the KSM page with a PTE built from scratch, so the soft-dirty bit of the PTE it replaces is lost. A page written since "clear_refs 4" then no longer shows as soft-dirty once it is merged, and a soft-dirty based dumper treats it as unchanged.

Quoted 6 lines
Quoted 1 line

Copy the soft-dirty bit from the original PTE to the new PTE.

Thanks. My LLM review thing says:

: There's a separate, pre-existing problem with the KSM zero-page : path which is worth pointing out.

Cool, more AI schlop.

TL;dr Sashiko raised this before and I commented why we don't care IIRC.

Signature · 3 lines
Cheers,

David
maintainer Acked-by on v3 1/1
#4 lore raw
In reply to Samarth Narang
Quoted 30 lines · Samarth Narang

replace_page() maps the KSM page with a PTE built from scratch, so the soft-dirty bit of the PTE it replaces is lost. A page written since "clear_refs 4" then no longer shows as soft-dirty once it is merged, and a soft-dirty based dumper treats it as unchanged.

Copy the soft-dirty bit from the original PTE to the new PTE.

Fixes 0f8975ec4db2 ("mm: soft-dirty bits for user memory changes tracking")
Assisted-by LLM

---

Notes:
v2: https://lore.kernel.org/all/[email protected]/

Changes since v2:
- Keep only the soft-dirty fix, as David suggested; Lance will be handling the uffd-wp/RWP part.
- Fixes tag reduced to the soft-dirty commit.
- Dropped the Tested-by tag, since the patch changed.

 mm/ksm.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/ksm.c +0 -0
index dbfb8f98b189..910a1307ac9f 100644

mm/ksm.c static int replace_page(struct vm_area_struct *vma, struct page *page, · line 1469

Quoted 7 lines
mm/ksm.c +4 -0
@@ -1463,6 +1463,10 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,
 		dec_mm_counter(mm, MM_ANONPAGES);
 	}
 
+	/* Preserve soft-dirty when replacing the page. */
+	if (pte_soft_dirty(orig_pte))
+		newpte = pte_mksoft_dirty(newpte);
+
Signature · 3 lines
Cheers,

David