Skip to contentSkip to conversation

mm/ksm: fix spurious soft-dirty bit on zero-filled page merging

PATCH In review Lance Yang 2 participants 5 messages Sep 28, 2025 – Sep 30, 2025

Subsystems (2)
lore
Share & tools
lore

Lance Yang

author
#0 lore raw

PATCH 1/1 mm/ksm: fix spurious soft-dirty bit on zero-filled page merging

From: Lance Yang <[email protected]>

When KSM merges a zero-filled page with the shared zeropage, it uses pte_mkdirty() to mark the new PTE for internal accounting. However, pte_mkdirty() unconditionally sets both the hardware dirty bit and the soft-dirty bit.

This behavior causes false positives in userspace tools like CRIU that rely on the soft-dirty mechanism for tracking memory changes.

So, preserve the correct state by reading the old PTE under the page table lock and explicitly clearing the soft-dirty bit from the new PTE if the original was not soft-dirty.

Fixes 79271476b336 ("ksm: support unsharing KSM-placed zero pages")
---
 mm/ksm.c | 3 +++
 1 file changed, 3 insertions(+)
mm/ksm.c +3 -0
index 04019a15b25d..e34516b8fbe4 100644
@@ -1403,6 +1403,9 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,
 		 * the dirty bit in zero page's PTE is set.
 		 */
 		newpte = pte_mkdirty(pte_mkspecial(pfn_pte(page_to_pfn(kpage), vma->vm_page_prot)));
+		if (!pte_soft_dirty(ptep_get(ptep)))
+			newpte = pte_clear_soft_dirty(newpte);
+
 		ksm_map_zero_page(mm);
 		/*
 		 * We're replacing an anonymous page with a zero page, which is
Signature · 1 line
2.49.0
#1 lore raw
Quoted 7 lines · Lance Yang

From: Lance Yang <[email protected]>

When KSM merges a zero-filled page with the shared zeropage, it uses pte_mkdirty() to mark the new PTE for internal accounting. However, pte_mkdirty() unconditionally sets both the hardware dirty bit and the soft-dirty bit.

Right, that's one think we should clean up at one point.

Quoted 2 lines

This behavior causes false positives in userspace tools like CRIU that rely on the soft-dirty mechanism for tracking memory changes.

IIRC, false positives are not a problem. We get them all of the time when merging VMAs etc.

So I am not sure if this here is really worth fixing. Soft-dirty is not, and never will be false-positive free.

Signature · 3 lines
Cheers

David / dhildenb
author
#2 lore raw
Quoted 10 lines · David Hildenbrand
Quoted 7 lines · Lance Yang

From: Lance Yang <[email protected]>

When KSM merges a zero-filled page with the shared zeropage, it uses pte_mkdirty() to mark the new PTE for internal accounting. However, pte_mkdirty() unconditionally sets both the hardware dirty bit and the soft-dirty bit.

Right, that's one think we should clean up at one point.

Got it. I’ll take a look when I get a chance ;)

Quoted 5 lines
Quoted 3 lines

This behavior causes false positives in userspace tools like CRIU that rely on the soft-dirty mechanism for tracking memory changes.

IIRC, false positives are not a problem. We get them all of the time when merging VMAs etc.

Right, Indeed.

Quoted 1 line

So I am not sure if this here is really worth fixing. Soft-dirty is not, and never will be false-positive free.

Makes sense to me. It doesn’t seem worth the trouble to fix. Let’s go ahead and drop it.

Thanks,
Lance

#3 lore raw
Quoted 23 lines · Lance Yang
Quoted 10 lines · David Hildenbrand
Quoted 7 lines · Lance Yang

From: Lance Yang <[email protected]>

When KSM merges a zero-filled page with the shared zeropage, it uses pte_mkdirty() to mark the new PTE for internal accounting. However, pte_mkdirty() unconditionally sets both the hardware dirty bit and the soft-dirty bit.

Right, that's one think we should clean up at one point.

Got it. I’ll take a look when I get a chance ;)

Quoted 6 lines
Quoted 3 lines

This behavior causes false positives in userspace tools like CRIU that rely on the soft-dirty mechanism for tracking memory changes.

IIRC, false positives are not a problem. We get them all of the time when merging VMAs etc.

Quoted 7 lines

Right, Indeed.

Quoted 2 lines

So I am not sure if this here is really worth fixing. Soft-dirty is not, and never will be false-positive free.

Makes sense to me. It doesn’t seem worth the trouble to fix. Let’s go ahead and drop it.

Yeah, I would prefer if we can just decouple soft-dirty from dirty handling at some point. I recall I had a use case around PageAnonExclusive at some point.

Signature · 3 lines
Cheers

David / dhildenb
author
#4 lore raw
Quoted 26 lines · David Hildenbrand
Quoted 25 lines · Lance Yang
Quoted 10 lines · David Hildenbrand
Quoted 7 lines · Lance Yang

From: Lance Yang <[email protected]>

When KSM merges a zero-filled page with the shared zeropage, it uses pte_mkdirty() to mark the new PTE for internal accounting. However, pte_mkdirty() unconditionally sets both the hardware dirty bit and the soft-dirty bit.

Right, that's one think we should clean up at one point.

Got it. I’ll take a look when I get a chance ;)

Quoted 6 lines
Quoted 3 lines

This behavior causes false positives in userspace tools like CRIU that rely on the soft-dirty mechanism for tracking memory changes.

IIRC, false positives are not a problem. We get them all of the time when merging VMAs etc.

Right, Indeed.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

So I am not sure if this here is really worth fixing. Soft-dirty is not, and never will be false-positive free.

Makes sense to me. It doesn’t seem worth the trouble to fix. Let’s go ahead and drop it.

Yeah, I would prefer if we can just decouple soft-dirty from dirty handling at some point. I recall I had a use case around PageAnonExclusive at some point.

Cool. I'll give it a try ;)