Skip to contentSkip to conversation

mm/ksm: do not inherit ksm_zero_pages counter or PTE markers on fork

PATCH In review [email protected] 4 participants 6 messages Nov 17 – Nov 18

Subsystems (4)
lore
Share & tools
lore

[email protected]

author reviewer
#0 lore raw

PATCH 1/1 mm/ksm: do not inherit ksm_zero_pages counter or PTE markers on fork

From: "Xu Xin (vivo)" <[email protected]>

As documented in Documentation/admin-guide/mm/ksm.rst, ksm_zero_pages accounts only for zero pages actually placed by KSM. However, the current implementation wrongly accounts for forked zero-page PTEs as KSM's contribution, inflating both the child's per-mm counter and the global ksm_zero_pages counter.

Originally, commit e2942062e01d ("ksm: count all zero pages placed by KSM") marked KSM-placed zero-page PTEs with the dirty bit for unmap accounting, but forgot to clear this marker during fork(). Later, to prevent ksm_might_unmap_zero_page() from underflowing the counters on child exit[1], commit 4d6fc29f3634 ("mm/ksm: fix incorrect KSM counter handling in mm_struct during fork") kept the child's inherited per-mm counter and added it to the global counter.

Instead of inflating the counters, fix the root cause by resetting the child's mm->ksm_zero_pages to 0 in ksm_fork() and clearing the dirty bit marker via pte_mkclean() when copying a KSM zero PTE in __copy_present_ptes(). With the marker cleared, ksm_might_unmap_zero_page() will not decrement the counters on child exit, MADV_UNMERGEABLE in the child will not unnecessarily unshare inherited zero pages, and the global counter remains untouched.

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

Fixes 4d6fc29f3634 ("mm/ksm: fix incorrect KSM counter handling in mm_struct during fork")
---
 include/linux/ksm.h | 8 +++++---
 mm/memory.c         | 8 ++++++++
 2 files changed, 13 insertions(+), 3 deletions(-)
include/linux/ksm.h +5 -3
index 93793ca6bcc4..3c6db19d825e 100644
@@ -56,11 +56,13 @@ static inline void ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
 {
 	/* Adding mm to ksm is best effort on fork. */
 	if (mm_flags_test(MMF_VM_MERGEABLE, oldmm)) {
-		long nr_ksm_zero_pages = atomic_long_read(&mm->ksm_zero_pages);
-
+		/*
+		 * Child's inherited zero-page PTEs are unmarked on copy and
+		 * were not placed by KSM in the child, so reset its counter.
+		 */
 		mm->ksm_merging_pages = 0;
 		mm->ksm_rmap_items = 0;
-		atomic_long_add(nr_ksm_zero_pages, &ksm_zero_pages);
+		atomic_long_set(&mm->ksm_zero_pages, 0);
 		__ksm_enter(mm);
 	}
 }
mm/memory.c +8 -0
index cf075fcec3c5..7dce96af2a51 100644
@@ -1160,6 +1160,14 @@ static __always_inline void __copy_present_ptes(struct vm_area_struct *dst_vma,
 		pte = pte_mkclean(pte);
 	pte = pte_mkold(pte);
 
+	/*
+	 * Clear the dirty-bit marker on KSM-placed zero-page PTEs in the
+	 * child so inherited mappings are not accounted against the child or
+	 * unshared by MADV_UNMERGEABLE.  Special PTEs always have nr == 1.
+	 */
+	if (is_ksm_zero_pte(pte))
+		pte = pte_mkclean(pte);
+
 	set_ptes(dst_vma->vm_mm, addr, dst_pte, pte, nr);
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 1 line · On 11/17/26 09:18, [email protected] wrote:

From: "Xu Xin (vivo)" <[email protected]>

Hi,

thanks for the report.

Quoted 6 lines

As documented in Documentation/admin-guide/mm/ksm.rst, ksm_zero_pages accounts only for zero pages actually placed by KSM. However, the current implementation wrongly accounts for forked zero-page PTEs as KSM's contribution, inflating both the child's per-mm counter and the global ksm_zero_pages counter.

But ... they were placed by KSM?

Just like any other KSM folios deduplicated before fork and then shared with the child?

So won't there be an inconsistency when MADV_UNMERGEABLE?

Quoted 34 lines

Originally, commit e2942062e01d ("ksm: count all zero pages placed by KSM") marked KSM-placed zero-page PTEs with the dirty bit for unmap accounting, but forgot to clear this marker during fork(). Later, to prevent ksm_might_unmap_zero_page() from underflowing the counters on child exit[1], commit 4d6fc29f3634 ("mm/ksm: fix incorrect KSM counter handling in mm_struct during fork") kept the child's inherited per-mm counter and added it to the global counter.

Instead of inflating the counters, fix the root cause by resetting the child's mm->ksm_zero_pages to 0 in ksm_fork() and clearing the dirty bit marker via pte_mkclean() when copying a KSM zero PTE in __copy_present_ptes(). With the marker cleared, ksm_might_unmap_zero_page() will not decrement the counters on child exit, MADV_UNMERGEABLE in the child will not unnecessarily unshare inherited zero pages, and the global counter remains untouched.

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

Fixes 4d6fc29f3634 ("mm/ksm: fix incorrect KSM counter handling in mm_struct during fork")
---
 include/linux/ksm.h | 8 +++++---
 mm/memory.c         | 8 ++++++++
 2 files changed, 13 insertions(+), 3 deletions(-)
include/linux/ksm.h +0 -0
index 93793ca6bcc4..3c6db19d825e 100644
@@ -56,11 +56,13 @@ static inline void ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
 {
 	/* Adding mm to ksm is best effort on fork. */

include/linux/ksm.h static inline void ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) · line 63

Quoted 10 lines
include/linux/ksm.h +4 -3
 	if (mm_flags_test(MMF_VM_MERGEABLE, oldmm)) {
-		long nr_ksm_zero_pages = atomic_long_read(&mm->ksm_zero_pages);
-
+		/*
+		 * Child's inherited zero-page PTEs are unmarked on copy and
+		 * were not placed by KSM in the child, so reset its counter.
+		 */
 		mm->ksm_merging_pages = 0;
 		mm->ksm_rmap_items = 0;
-		atomic_long_add(nr_ksm_zero_pages, &ksm_zero_pages);

So I think there is definitely a BUG int he existing code when we skip copying some VMAs during fork. We should have instead adjusted the counter as we duplicate them into the child process.

Quoted 23 lines
include/linux/ksm.h +1 -0
+		atomic_long_set(&mm->ksm_zero_pages, 0);
 		__ksm_enter(mm);
 	}
 }
mm/memory.c +8 -0
index cf075fcec3c5..7dce96af2a51 100644
@@ -1160,6 +1160,14 @@ static __always_inline void __copy_present_ptes(struct vm_area_struct *dst_vma,
 		pte = pte_mkclean(pte);
 	pte = pte_mkold(pte);
 
+	/*
+	 * Clear the dirty-bit marker on KSM-placed zero-page PTEs in the
+	 * child so inherited mappings are not accounted against the child or
+	 * unshared by MADV_UNMERGEABLE.  Special PTEs always have nr == 1.
+	 */
+	if (is_ksm_zero_pte(pte))
+		pte = pte_mkclean(pte);
+
 	set_ptes(dst_vma->vm_mm, addr, dst_pte, pte, nr);
 }
 
Signature · 3 lines
Cheers,

David
author reviewer
#2 lore raw
Quoted 6 lines · On 11/17/26 09:18, [email protected] wrote:
Quoted 1 line

From: "Xu Xin (vivo)" <[email protected]>

Hi,

thanks for the report.

Hi David,

Thanks for the feedback!

Quoted 8 lines
Quoted 7 lines

As documented in Documentation/admin-guide/mm/ksm.rst, ksm_zero_pages accounts only for zero pages actually placed by KSM. However, the current implementation wrongly accounts for forked zero-page PTEs as KSM's contribution, inflating both the child's per-mm counter and the global ksm_zero_pages counter.

Quoted 4 lines

But ... they were placed by KSM?

Just like any other KSM folios deduplicated before fork and then shared with the child?

I see your point, but in practice, people evaluating use_zero_pages=1 often look at ksm_zero_pages to gauge how much memory KSM actually saved. Since forked pages already share the same physical frame via COW even without KSM, counting them across many forked children can give a somewhat misleading impression of KSM's actual savings.

Also, ksm_merging_pages right next to it is already reset to 0 on fork (even when regular KSM folios are shared with the child), so resetting ksm_zero_pages keeps the two counters consistent.

Quoted 3 lines

So won't there be an inconsistency when MADV_UNMERGEABLE?

As for MADV_UNMERGEABLE, leaving the child's mapping as a normal read-only shared zero page is safe and avoids extra allocations. Plus it naturally avoids the skipped-VMA (MADV_DONTFORK) accounting bug you spotted, and does so without extra per-PTE overhead.

What do you think?

Quoted 48 lines
Quoted 34 lines

Originally, commit e2942062e01d ("ksm: count all zero pages placed by KSM") marked KSM-placed zero-page PTEs with the dirty bit for unmap accounting, but forgot to clear this marker during fork(). Later, to prevent ksm_might_unmap_zero_page() from underflowing the counters on child exit[1], commit 4d6fc29f3634 ("mm/ksm: fix incorrect KSM counter handling in mm_struct during fork") kept the child's inherited per-mm counter and added it to the global counter.

Instead of inflating the counters, fix the root cause by resetting the child's mm->ksm_zero_pages to 0 in ksm_fork() and clearing the dirty bit marker via pte_mkclean() when copying a KSM zero PTE in __copy_present_ptes(). With the marker cleared, ksm_might_unmap_zero_page() will not decrement the counters on child exit, MADV_UNMERGEABLE in the child will not unnecessarily unshare inherited zero pages, and the global counter remains untouched.

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

Fixes 4d6fc29f3634 ("mm/ksm: fix incorrect KSM counter handling in mm_struct during fork")
---
 include/linux/ksm.h | 8 +++++---
 mm/memory.c         | 8 ++++++++
 2 files changed, 13 insertions(+), 3 deletions(-)
include/linux/ksm.h +0 -0
index 93793ca6bcc4..3c6db19d825e 100644
@@ -56,11 +56,13 @@ static inline void ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
 {
 	/* Adding mm to ksm is best effort on fork. */

include/linux/ksm.h static inline void ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) · line 63

Quoted 10 lines
include/linux/ksm.h +4 -3
 	if (mm_flags_test(MMF_VM_MERGEABLE, oldmm)) {
-		long nr_ksm_zero_pages = atomic_long_read(&mm->ksm_zero_pages);
-
+		/*
+		 * Child's inherited zero-page PTEs are unmarked on copy and
+		 * were not placed by KSM in the child, so reset its counter.
+		 */
 		mm->ksm_merging_pages = 0;
 		mm->ksm_rmap_items = 0;
-		atomic_long_add(nr_ksm_zero_pages, &ksm_zero_pages);

So I think there is definitely a BUG int he existing code when we skip copying some VMAs during fork. We should have instead adjusted the counter as we duplicate them into the child process.

#3 lore raw
In reply to [email protected]
kernel test robot noticed the following build errors:

Hi,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/xu-xin-linux-dev/mm-ksm-do-not-inherit-ksm_zero_pages-counter-or-PTE-markers-on-fork/20261117-161821
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20261117081821.7211-1-xu.xin%40linux.dev
patch subject: [PATCH] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers on fork
config: hexagon-allnoconfig (https://download.01.org/0day-ci/archive/20261117/[email protected]/config)
compiler: clang version 24.0.0git (https://github.com/llvm/llvm-project a69be15ed58ed48738a69ec4c7d0f3e8c7dbe4ce)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261117/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> mm/memory.c:1168:6: error: call to undeclared function 'is_ksm_zero_pte'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    1168 |         if (is_ksm_zero_pte(pte))
         |             ^

1 error generated.

vim +/is_ksm_zero_pte +1168 mm/memory.c

1123
1124 static __always_inline void __copy_present_ptes(struct vm_area_struct *dst_vma,
1125 struct vm_area_struct *src_vma, pte_t *dst_pte, pte_t *src_pte,
1126 pte_t pte, unsigned long addr, int nr)
1127 {
1128 struct mm_struct *src_mm = src_vma->vm_mm;
1129 bool writable;
1130
1131 /*
1132 * Snapshot writability before the RWP-disarm rewrite below: when the
1133 * child is not RWP-armed, pte_modify(pte, dst_vma->vm_page_prot) can
1134 * silently drop _PAGE_RW from a resolved (no-marker) writable PTE,
1135 * so a later pte_write(pte) check would skip the COW wrprotect and
1136 * leave the parent writable over a folio shared with the child.
1137 */
1138 writable = pte_write(pte);
1139
1140 /*
1141 * Child is not RWP-armed: restore accessible protection so the
1142 * inherited PAGE_NONE does not cost a fault on first read. Gate on
1143 * pte_uffd(pte) so unrelated PAGE_NONE markers (e.g. NUMA balancing)
1144 * are not normalised away.
1145 */
1146 if (!userfaultfd_protected(dst_vma)) {
1147 if (userfaultfd_rwp(src_vma) && pte_uffd(pte))
1148 pte = pte_modify(pte, dst_vma->vm_page_prot);
1149 pte = pte_clear_uffd(pte);
1150 }
1151
1152 /* If it's a COW mapping, write protect it both processes. */
1153 if (vma_is_cow_mapping(src_vma) && writable) {
1154 wrprotect_ptes(src_mm, addr, src_pte, nr);
1155 pte = pte_wrprotect(pte);
1156 }
1157
1158 /* If it's a shared mapping, mark it clean in the child. */
1159 if (src_vma->vm_flags & VM_SHARED)
1160 pte = pte_mkclean(pte);
1161 pte = pte_mkold(pte);
1162
1163 /*
1164 * Clear the dirty-bit marker on KSM-placed zero-page PTEs in the
1165 * child so inherited mappings are not accounted against the child or
1166 * unshared by MADV_UNMERGEABLE. Special PTEs always have nr == 1.
1167 */

> 1168		if (is_ksm_zero_pte(pte))

1169 pte = pte_mkclean(pte);
1170
1171 set_ptes(dst_vma->vm_mm, addr, dst_pte, pte, nr);
1172 }
1173

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw
In reply to [email protected]
kernel test robot noticed the following build errors:

Hi,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/xu-xin-linux-dev/mm-ksm-do-not-inherit-ksm_zero_pages-counter-or-PTE-markers-on-fork/20261117-161821
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20261117081821.7211-1-xu.xin%40linux.dev
patch subject: [PATCH] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers on fork
config: arc-allnoconfig (https://download.01.org/0day-ci/archive/20261118/[email protected]/config)
compiler: arc-linux-gcc (GCC) 16.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261118/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

mm/memory.c: In function '__copy_present_ptes':

>> mm/memory.c:1168:13: error: implicit declaration of function 'is_ksm_zero_pte' [-Wimplicit-function-declaration]
    1168 |         if (is_ksm_zero_pte(pte))
         |             ^~~~~~~~~~~~~~~

vim +/is_ksm_zero_pte +1168 mm/memory.c

1123
1124 static __always_inline void __copy_present_ptes(struct vm_area_struct *dst_vma,
1125 struct vm_area_struct *src_vma, pte_t *dst_pte, pte_t *src_pte,
1126 pte_t pte, unsigned long addr, int nr)
1127 {
1128 struct mm_struct *src_mm = src_vma->vm_mm;
1129 bool writable;
1130
1131 /*
1132 * Snapshot writability before the RWP-disarm rewrite below: when the
1133 * child is not RWP-armed, pte_modify(pte, dst_vma->vm_page_prot) can
1134 * silently drop _PAGE_RW from a resolved (no-marker) writable PTE,
1135 * so a later pte_write(pte) check would skip the COW wrprotect and
1136 * leave the parent writable over a folio shared with the child.
1137 */
1138 writable = pte_write(pte);
1139
1140 /*
1141 * Child is not RWP-armed: restore accessible protection so the
1142 * inherited PAGE_NONE does not cost a fault on first read. Gate on
1143 * pte_uffd(pte) so unrelated PAGE_NONE markers (e.g. NUMA balancing)
1144 * are not normalised away.
1145 */
1146 if (!userfaultfd_protected(dst_vma)) {
1147 if (userfaultfd_rwp(src_vma) && pte_uffd(pte))
1148 pte = pte_modify(pte, dst_vma->vm_page_prot);
1149 pte = pte_clear_uffd(pte);
1150 }
1151
1152 /* If it's a COW mapping, write protect it both processes. */
1153 if (vma_is_cow_mapping(src_vma) && writable) {
1154 wrprotect_ptes(src_mm, addr, src_pte, nr);
1155 pte = pte_wrprotect(pte);
1156 }
1157
1158 /* If it's a shared mapping, mark it clean in the child. */
1159 if (src_vma->vm_flags & VM_SHARED)
1160 pte = pte_mkclean(pte);
1161 pte = pte_mkold(pte);
1162
1163 /*
1164 * Clear the dirty-bit marker on KSM-placed zero-page PTEs in the
1165 * child so inherited mappings are not accounted against the child or
1166 * unshared by MADV_UNMERGEABLE. Special PTEs always have nr == 1.
1167 */

> 1168		if (is_ksm_zero_pte(pte))

1169 pte = pte_mkclean(pte);
1170
1171 set_ptes(dst_vma->vm_mm, addr, dst_pte, pte, nr);
1172 }
1173

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#5 lore raw
In reply to [email protected]
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] mm/ksm: implicit declaration of is_ksm_zero_pte() when CONFIG_KSM=n --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] mm/ksm: implicit declaration of is_ksm_zero_pte() when CONFIG_KSM=n
--

commit 113c3050b4809182bceaba3e1d17c686dedf4a41
Author: Xu Xin (vivo) <[email protected]>

mm/ksm: do not inherit ksm_zero_pages counter or PTE markers on fork

This commit resets the child's ksm_zero_pages counter in ksm_fork() and clears the dirty-bit marker on KSM-placed zero-page PTEs in __copy_present_ptes() during fork.

Quoted 5 lines
mm/memory.c +0 -0
index d2f48d78b0d7b..4c845b38d4a01 100644

mm/memory.c static __always_inline void __copy_present_ptes(struct vm_area_struct *dst_vma, · line 1169

Quoted 10 lines
mm/memory.c +7 -0
@@ -1160,6 +1160,14 @@ static __always_inline void __copy_present_ptes(struct vm_area_struct *dst_vma,
 		pte = pte_mkclean(pte);
 	pte = pte_mkold(pte);
 
+	/*
+	 * Clear the dirty-bit marker on KSM-placed zero-page PTEs in the
+	 * child so inherited mappings are not accounted against the child or
+	 * unshared by MADV_UNMERGEABLE.  Special PTEs always have nr == 1.
+	 */
+	if (is_ksm_zero_pte(pte))
+		pte = pte_mkclean(pte);

[Severity: High]
Does this break the build when CONFIG_KSM is disabled? is_ksm_zero_pte() is only defined inside the #ifdef CONFIG_KSM block in include/linux/ksm.h, with no stub for !CONFIG_KSM.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1