Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/3] mm/ksm: fix ksm_zero_pages accounting on fork
@ 2026-11-20  7:42 Xu Xin (vivo)
  2026-11-20  7:43 ` [PATCH v2 1/3] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers " Xu Xin (vivo)
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Xu Xin (vivo) @ 2026-11-20  7:42 UTC (permalink / raw)
  To: david
  Cc: akpm, chengming.zhou, donettom, linux-kernel, linux-mm, stable,
	xu.xin

Currently, fork() wrongly inherits the parent's ksm_zero_pages counter
and keeps the KSM dirty-bit marker on zero-page PTEs, inflating both the
child's per-mm counter and the global ksm_zero_pages counter.

Patch 1 fixes this by resetting the child's mm->ksm_zero_pages to 0 in
ksm_fork() and clearing the KSM zero-page marker when copying present
PTEs, and documents the behavior.

Patch 2 fixes a false-positive bug in the existing KSM fork selftest by
removing a redundant init_global_file_handles() call in the child.

Patch 3 extends the selftest to verify that ksm_zero_pages is not
inherited across fork().

---
Changes in v2:
- Encapsulate zero-page PTE marker clearing into a dedicated
  might_clear_ksm_zero_pte() helper and simplify the code comment. (David)

- Fix compilation error by adding stub implementation of
  might_clear_ksm_zero_pte() for !CONFIG_KSM. (kernel test robot)

- Document the fork() interaction and MADV_UNMERGEABLE behavior for
  ksm_zero_pages in Documentation/admin-guide/mm/ksm.rst. (David)

- Fix a false-positive bug in the KSM fork selftest (patch 2/3) and
  extend it to verify ksm_zero_pages is not inherited on fork (patch 3/3).

Xu Xin (vivo) (3):
  mm/ksm: do not inherit ksm_zero_pages counter or PTE markers on fork
  selftests/mm: fix false-positive KSM fork test by dropping
    init_global_file_handles()
  selftests/mm: verify ksm_zero_pages is not inherited on fork

 Documentation/admin-guide/mm/ksm.rst          |  7 +++-
 include/linux/ksm.h                           | 18 ++++++++--
 mm/memory.c                                   |  2 ++
 .../selftests/mm/ksm_functional_tests.c       | 35 ++++++++++++++-----
 4 files changed, 49 insertions(+), 13 deletions(-)

-- 
2.34.1



^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 1/3] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers on fork
  2026-11-20  7:42 [PATCH v2 0/3] mm/ksm: fix ksm_zero_pages accounting on fork Xu Xin (vivo)
@ 2026-11-20  7:43 ` Xu Xin (vivo)
  2026-11-21 12:58   ` David Hildenbrand (Arm)
  2026-11-20  7:44 ` [PATCH v2 2/3] selftests/mm: fix false-positive KSM fork test by dropping init_global_file_handles() Xu Xin (vivo)
  2026-11-20  7:44 ` [PATCH v2 3/3] selftests/mm: verify ksm_zero_pages is not inherited on fork Xu Xin (vivo)
  2 siblings, 1 reply; 7+ messages in thread
From: Xu Xin (vivo) @ 2026-11-20  7:43 UTC (permalink / raw)
  To: xu.xin
  Cc: akpm, chengming.zhou, david, donettom, linux-kernel, linux-mm,
	stable

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 KSM
zero-page marker via might_clear_ksm_zero_pte() when copying present
PTEs 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/53a7bf3d-843d-4e19-b0a3-cc6852fe72c1@redhat.com/

Fixes: 4d6fc29f3634 ("mm/ksm: fix incorrect KSM counter handling in mm_struct during fork")
Cc: stable@vger.kernel.org
Signed-off-by: Xu Xin (vivo) <xu.xin@linux.dev>
---
 Documentation/admin-guide/mm/ksm.rst |  7 ++++++-
 include/linux/ksm.h                  | 18 +++++++++++++++---
 mm/memory.c                          |  2 ++
 3 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst
index c329ca747b8c..8968ae3f55c0 100644
--- a/Documentation/admin-guide/mm/ksm.rst
+++ b/Documentation/admin-guide/mm/ksm.rst
@@ -214,7 +214,12 @@ stable_node_dups
         number of duplicated KSM pages
 ksm_zero_pages
         how many zero pages that are still mapped into processes were mapped by
-        KSM when deduplicating.
+        KSM when deduplicating. Note: on fork(), the child process does not
+        inherit the ``ksm_zero_pages`` counter, and any inherited KSM-placed
+        zero-page mappings are converted into ordinary shared zero-page
+        mappings. Consequently, these mappings are neither accounted for in
+        the child's counter nor unshared when ``MADV_UNMERGEABLE`` is called
+        in the child.
 
 When ``use_zero_pages`` is/was enabled, the sum of ``pages_sharing`` +
 ``ksm_zero_pages`` represents the actual number of pages saved by KSM.
diff --git a/include/linux/ksm.h b/include/linux/ksm.h
index 596c04ca7326..62fa8ca803ed 100644
--- a/include/linux/ksm.h
+++ b/include/linux/ksm.h
@@ -32,6 +32,15 @@ void __ksm_exit(struct mm_struct *mm);
  */
 #define is_ksm_zero_pte(pte)	(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte))
 
+/* Clear KSM's marker on zero-page PTEs (e.g., on fork). */
+static inline pte_t might_clear_ksm_zero_pte(pte_t pte)
+{
+	if (is_ksm_zero_pte(pte))
+		return pte_mkclean(pte);
+
+	return pte;
+}
+
 extern atomic_long_t ksm_zero_pages;
 
 static inline void ksm_map_zero_page(struct mm_struct *mm)
@@ -57,11 +66,9 @@ 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);
-
 		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);
 	}
 }
@@ -114,6 +121,11 @@ static inline void ksm_exit(struct mm_struct *mm)
 {
 }
 
+static inline pte_t might_clear_ksm_zero_pte(pte_t pte)
+{
+	return pte;
+}
+
 static inline void ksm_might_unmap_zero_page(struct mm_struct *mm, pte_t pte)
 {
 }
diff --git a/mm/memory.c b/mm/memory.c
index d2f48d78b0d7..79ebaa1e3368 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1160,6 +1160,8 @@ static __always_inline void __copy_present_ptes(struct vm_area_struct *dst_vma,
 		pte = pte_mkclean(pte);
 	pte = pte_mkold(pte);
 
+	pte = might_clear_ksm_zero_pte(pte);
+
 	set_ptes(dst_vma->vm_mm, addr, dst_pte, pte, nr);
 }
 
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 2/3] selftests/mm: fix false-positive KSM fork test by dropping init_global_file_handles()
  2026-11-20  7:42 [PATCH v2 0/3] mm/ksm: fix ksm_zero_pages accounting on fork Xu Xin (vivo)
  2026-11-20  7:43 ` [PATCH v2 1/3] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers " Xu Xin (vivo)
@ 2026-11-20  7:44 ` Xu Xin (vivo)
  2026-11-21  7:45   ` sashiko-bot
  2026-11-20  7:44 ` [PATCH v2 3/3] selftests/mm: verify ksm_zero_pages is not inherited on fork Xu Xin (vivo)
  2 siblings, 1 reply; 7+ messages in thread
From: Xu Xin (vivo) @ 2026-11-20  7:44 UTC (permalink / raw)
  To: xu.xin
  Cc: akpm, chengming.zhou, david, donettom, linux-kernel, linux-mm,
	stable

In test_fork_ksm_merging_page_count(), the child process currently calls
init_global_file_handles() before reading ksm_get_self_merging_pages().
However, init_global_file_handles() calls ksm_stop(), which writes 2
(KSM_RUN_UNMERGE) to /sys/kernel/mm/ksm/run, unmerging all KSM pages
across the system and resetting the counters to 0. As a result, the test
always passes regardless of whether fork() properly resets the counter.

Remove the redundant init_global_file_handles().

Fixes: 08ff89b5659d ("selftests/mm: add fork inheritance test for ksm_merging_pages counter")
Cc: stable@vger.kernel.org
Signed-off-by: Xu Xin (vivo) <xu.xin@linux.dev>
---
 tools/testing/selftests/mm/ksm_functional_tests.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c
index 135191f99962..c20982b0aff0 100644
--- a/tools/testing/selftests/mm/ksm_functional_tests.c
+++ b/tools/testing/selftests/mm/ksm_functional_tests.c
@@ -670,7 +670,6 @@ static void test_fork_ksm_merging_page_count(void)
 
 	child_pid = fork();
 	if (!child_pid) {
-		init_global_file_handles();
 		exit(ksm_get_self_merging_pages());
 	} else if (child_pid < 0) {
 		ksft_test_result_fail("fork() failed\n");
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 3/3] selftests/mm: verify ksm_zero_pages is not inherited on fork
  2026-11-20  7:42 [PATCH v2 0/3] mm/ksm: fix ksm_zero_pages accounting on fork Xu Xin (vivo)
  2026-11-20  7:43 ` [PATCH v2 1/3] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers " Xu Xin (vivo)
  2026-11-20  7:44 ` [PATCH v2 2/3] selftests/mm: fix false-positive KSM fork test by dropping init_global_file_handles() Xu Xin (vivo)
@ 2026-11-20  7:44 ` Xu Xin (vivo)
  2026-11-21  7:45   ` sashiko-bot
  2 siblings, 1 reply; 7+ messages in thread
From: Xu Xin (vivo) @ 2026-11-20  7:44 UTC (permalink / raw)
  To: xu.xin
  Cc: akpm, chengming.zhou, david, donettom, linux-kernel, linux-mm,
	stable

Extend test_fork_ksm_merging_page_count() to enable use_zero_pages and
merge zero pages alongside non-zero pages before fork(), verifying that
neither ksm_merging_pages nor ksm_zero_pages is inherited by the child.

Signed-off-by: Xu Xin (vivo) <xu.xin@linux.dev>
---
 .../selftests/mm/ksm_functional_tests.c       | 33 ++++++++++++++-----
 1 file changed, 25 insertions(+), 8 deletions(-)

diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c
index c20982b0aff0..321f8abc85f9 100644
--- a/tools/testing/selftests/mm/ksm_functional_tests.c
+++ b/tools/testing/selftests/mm/ksm_functional_tests.c
@@ -655,40 +655,57 @@ static void test_prot_none(void)
 	munmap(map, size);
 }
 
-static void test_fork_ksm_merging_page_count(void)
+static void test_fork_ksm_statistic_count(void)
 {
 	const unsigned int size = 2 * MiB;
 	char *map;
+	char *zero_map;
 	pid_t child_pid;
 	int status;
 
 	ksft_print_msg("[RUN] %s\n", __func__);
 
+	if (ksm_use_zero_pages() < 0) {
+		ksft_test_result_skip("write \"/sys/kernel/mm/ksm/use_zero_pages\" failed\n");
+		return;
+	}
+
 	map = mmap_and_merge_range(0xcf, size, PROT_READ | PROT_WRITE, KSM_MERGE_MADVISE);
 	if (map == MAP_FAILED)
 		return;
 
+	zero_map = mmap_and_merge_range(0x00, size, PROT_READ | PROT_WRITE, KSM_MERGE_MADVISE);
+	if (zero_map == MAP_FAILED)
+		goto unmap;
+
+	if (!ksm_get_self_merging_pages() || !ksm_get_self_zero_pages()) {
+		ksft_test_result_fail("KSM failed to merge pages before fork\n");
+		goto unmap_zero;
+	}
+
 	child_pid = fork();
 	if (!child_pid) {
-		exit(ksm_get_self_merging_pages());
+		exit(!!(ksm_get_self_merging_pages() + ksm_get_self_zero_pages()));
 	} else if (child_pid < 0) {
 		ksft_test_result_fail("fork() failed\n");
-		goto unmap;
+		goto unmap_zero;
 	}
 
 	if (waitpid(child_pid, &status, 0) < 0) {
 		ksft_test_result_fail("waitpid() failed\n");
-		goto unmap;
+		goto unmap_zero;
 	}
 
 	status = WEXITSTATUS(status);
 	if (status) {
-		ksft_test_result_fail("ksm_merging_page in child: %d\n", status);
-		goto unmap;
+		ksft_test_result_fail("ksm_merging_pages or ksm_zero_pages is non-zero in child\n");
+		goto unmap_zero;
 	}
 
-	ksft_test_result_pass("ksm_merging_pages is not inherited after fork\n");
+	ksft_test_result_pass("ksm_merging_pages and ksm_zero_pages are reset after fork\n");
 
+unmap_zero:
+	munmap(zero_map, size);
 unmap:
 	ksm_stop();
 	munmap(map, size);
@@ -837,7 +854,7 @@ int main(int argc, char **argv)
 	test_prctl_fork();
 	test_prctl_fork_exec();
 	test_prctl_unmerge();
-	test_fork_ksm_merging_page_count();
+	test_fork_ksm_statistic_count();
 	test_merge_aged_pairs();
 
 	err = ksft_get_fail_cnt();
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 2/3] selftests/mm: fix false-positive KSM fork test by dropping init_global_file_handles()
  2026-11-20  7:44 ` [PATCH v2 2/3] selftests/mm: fix false-positive KSM fork test by dropping init_global_file_handles() Xu Xin (vivo)
@ 2026-11-21  7:45   ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-21  7:45 UTC (permalink / raw)
  To: Xu Xin (vivo); +Cc: akpm, chengming.zhou, david, donettom, linux-mm

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] selftests/mm: exit() truncates page count in
  test_fork_ksm_merging_page_count()
- [Medium] selftests/mm: ksmd race across fork() in
  test_fork_ksm_merging_page_count()
--

commit 320ca42efa1f04c6ac42a52e155a4cd5bc860e0e
Author: Xu Xin (vivo) <xu.xin@linux.dev>

selftests/mm: fix false-positive KSM fork test by dropping init_global_file_handles()

This commit removes the init_global_file_handles() call from the child
process in test_fork_ksm_merging_page_count() so it does not invoke
ksm_stop() before reading ksm_get_self_merging_pages().

> diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c
> index 135191f99962d..c20982b0aff0e 100644
> --- a/tools/testing/selftests/mm/ksm_functional_tests.c
> +++ b/tools/testing/selftests/mm/ksm_functional_tests.c
> @@ -670,7 +670,6 @@ static void test_fork_ksm_merging_page_count(void)
>  
>  	child_pid = fork();
>  	if (!child_pid) {
> -		init_global_file_handles();
>  		exit(ksm_get_self_merging_pages());

[Severity: Medium]
Does exit(ksm_get_self_merging_pages()) truncate the page count to 0 on
4 KiB page systems?

test_fork_ksm_merging_page_count() merges a 2 MiB region, which is 512
(0x200) pages on 4 KiB page systems (or 256 pages on 8 KiB page
systems).  Because exit() and WEXITSTATUS() only preserve the low 8 bits
of the exit code, an inherited count of 512 is truncated to 0 and the
test still falsely passes when fork() fails to reset
mm->ksm_merging_pages.

Also, does ksm_stop() actually reset an inherited ksm_merging_pages
counter in the child as described in the commit message?
unmerge_and_remove_all_rmap_items() only decrements
rmap_item->mm->ksm_merging_pages when removing rmap_items from
mm_slot->rmap_list, which is empty in a newly forked child.

A later commit in this series, commit "selftests/mm: verify
ksm_zero_pages is not inherited on fork", converts the exit argument to
a boolean via !!, but doing exit(!!ksm_get_self_merging_pages()) here is
needed so this stable-tagged fix works on its own.

[Severity: Medium]
Can ksmd race with the child process across fork() and cause a false
test failure?

mmap_and_merge_range() leaves ksmd running in KSM_RUN_MERGE mode, and
ksm_fork() adds the child mm to ksmd's scan list:

Parent                            ksmd
test_fork_ksm_merging_page_count()
  mmap_and_merge_range()
    ksm_start()
  fork()
    dup_mmap()
      ksm_fork()
        child_mm->ksm_merging_pages = 0
        __ksm_enter(child_mm)
                                  ksm_do_scan()
                                    cmp_and_merge_page()
                                      try_to_merge_with_ksm_page()
                                      stable_tree_append()
                                        child_mm->ksm_merging_pages++
  Child:
  ksm_get_self_merging_pages()  <-- reads non-zero count

Because the child's forked PTEs already map the merged KSM folio,
try_to_merge_one_page() succeeds on the first scan pass ("ksm page
forked") and stable_tree_append() increments
child_mm->ksm_merging_pages before the child reads
/proc/self/ksm_merging_pages.  Pausing ksmd by writing 0 (KSM_RUN_STOP)
to /sys/kernel/mm/ksm/run after mmap_and_merge_range() and before fork()
would avoid this race without unmerging pages.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120074230.40269-1-xu.xin@linux.dev?part=2


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 3/3] selftests/mm: verify ksm_zero_pages is not inherited on fork
  2026-11-20  7:44 ` [PATCH v2 3/3] selftests/mm: verify ksm_zero_pages is not inherited on fork Xu Xin (vivo)
@ 2026-11-21  7:45   ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-21  7:45 UTC (permalink / raw)
  To: Xu Xin (vivo); +Cc: akpm, chengming.zhou, david, donettom, linux-mm

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] selftests/mm: unchecked negative error returns in
  test_fork_ksm_statistic_count()
--

commit a4b057316a412b00cfd864404acbc77cd671159c
Author: Xu Xin (vivo) <xu.xin@linux.dev>

selftests/mm: verify ksm_zero_pages is not inherited on fork

This commit renames test_fork_ksm_merging_page_count() to
test_fork_ksm_statistic_count() and extends it to verify that neither
ksm_merging_pages nor ksm_zero_pages is inherited across fork().

> diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c
> index c20982b0aff0e..321f8abc85f91 100644
> --- a/tools/testing/selftests/mm/ksm_functional_tests.c
> +++ b/tools/testing/selftests/mm/ksm_functional_tests.c
> @@ -655,40 +655,57 @@ static void test_prot_none(void)
>  	munmap(map, size);
>  }
>  
> -static void test_fork_ksm_merging_page_count(void)
> +static void test_fork_ksm_statistic_count(void)
>  {
>  	const unsigned int size = 2 * MiB;
>  	char *map;
> +	char *zero_map;
>  	pid_t child_pid;
>  	int status;
>  
>  	ksft_print_msg("[RUN] %s\n", __func__);
>  
> +	if (ksm_use_zero_pages() < 0) {
> +		ksft_test_result_skip("write \"/sys/kernel/mm/ksm/use_zero_pages\" failed\n");
> +		return;
> +	}
> +
>  	map = mmap_and_merge_range(0xcf, size, PROT_READ | PROT_WRITE, KSM_MERGE_MADVISE);
>  	if (map == MAP_FAILED)
>  		return;
>  
> +	zero_map = mmap_and_merge_range(0x00, size, PROT_READ | PROT_WRITE, KSM_MERGE_MADVISE);
> +	if (zero_map == MAP_FAILED)
> +		goto unmap;
> +
> +	if (!ksm_get_self_merging_pages() || !ksm_get_self_zero_pages()) {
> +		ksft_test_result_fail("KSM failed to merge pages before fork\n");
> +		goto unmap_zero;
> +	}
> +
>  	child_pid = fork();
>  	if (!child_pid) {
> -		exit(ksm_get_self_merging_pages());
> +		exit(!!(ksm_get_self_merging_pages() + ksm_get_self_zero_pages()));

[Severity: Low]
Can negative error returns from ksm_get_self_zero_pages() or
ksm_get_self_merging_pages() bypass the pre-fork check and corrupt the
child status check?

Both helpers return a negative error code on open() or pread() failure
(for example when /proc/self/ksm_stat is unavailable, which
test_unmerge_zero_pages() checks for with ksm_get_self_zero_pages() < 0
to skip the test).  Because a negative value is non-zero, testing with !
treats the error as a valid positive page count before fork(), and
summing the two signed values in the child either causes the test to fail
instead of skipping or allows a negative error return to cancel out an
equal positive page count.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120074230.40269-1-xu.xin@linux.dev?part=3


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/3] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers on fork
  2026-11-20  7:43 ` [PATCH v2 1/3] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers " Xu Xin (vivo)
@ 2026-11-21 12:58   ` David Hildenbrand (Arm)
  0 siblings, 0 replies; 7+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-21 12:58 UTC (permalink / raw)
  To: Xu Xin (vivo)
  Cc: akpm, chengming.zhou, donettom, linux-kernel, linux-mm, stable

On 11/20/26 08:43, Xu Xin (vivo) wrote:
> 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 KSM
> zero-page marker via might_clear_ksm_zero_pte() when copying present
> PTEs 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/53a7bf3d-843d-4e19-b0a3-cc6852fe72c1@redhat.com/
> 
> Fixes: 4d6fc29f3634 ("mm/ksm: fix incorrect KSM counter handling in mm_struct during fork")
> Cc: stable@vger.kernel.org
> Signed-off-by: Xu Xin (vivo) <xu.xin@linux.dev>
> ---
>  Documentation/admin-guide/mm/ksm.rst |  7 ++++++-
>  include/linux/ksm.h                  | 18 +++++++++++++++---
>  mm/memory.c                          |  2 ++
>  3 files changed, 23 insertions(+), 4 deletions(-)
> 
> diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst
> index c329ca747b8c..8968ae3f55c0 100644
> --- a/Documentation/admin-guide/mm/ksm.rst
> +++ b/Documentation/admin-guide/mm/ksm.rst
> @@ -214,7 +214,12 @@ stable_node_dups
>          number of duplicated KSM pages
>  ksm_zero_pages
>          how many zero pages that are still mapped into processes were mapped by
> -        KSM when deduplicating.
> +        KSM when deduplicating. Note: on fork(), the child process does not
> +        inherit the ``ksm_zero_pages`` counter, and any inherited KSM-placed
> +        zero-page mappings are converted into ordinary shared zero-page
> +        mappings. Consequently, these mappings are neither accounted for in
> +        the child's counter nor unshared when ``MADV_UNMERGEABLE`` is called
> +        in the child.
>  
>  When ``use_zero_pages`` is/was enabled, the sum of ``pages_sharing`` +
>  ``ksm_zero_pages`` represents the actual number of pages saved by KSM.
> diff --git a/include/linux/ksm.h b/include/linux/ksm.h
> index 596c04ca7326..62fa8ca803ed 100644
> --- a/include/linux/ksm.h
> +++ b/include/linux/ksm.h
> @@ -32,6 +32,15 @@ void __ksm_exit(struct mm_struct *mm);
>   */
>  #define is_ksm_zero_pte(pte)	(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte))
>  
> +/* Clear KSM's marker on zero-page PTEs (e.g., on fork). */

This function would be better called "ksm_fork_pte"

Drop this comment and instead ...

> +static inline pte_t might_clear_ksm_zero_pte(pte_t pte)
> +{

Explain here *why* we are doing this.

> +	if (is_ksm_zero_pte(pte))
> +		return pte_mkclean(pte);> +

But I'd move the check into the caller and only clear here.

IOW, the following on the caller site:

diff --git a/mm/memory.c b/mm/memory.c
index ad5511b2e949a..ee4545a181598 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1184,8 +1184,11 @@ copy_present_ptes(struct vm_area_struct *dst_vma, struct
vm_area_struct *src_vma
        int err, nr;

        page = vm_normal_page(src_vma, addr, pte);
-       if (unlikely(!page))
+       if (unlikely(!page)) {
+               if (unlikely(is_ksm_zero_pte(pte)))
+                       pte = ksm_fork_pte(pte);
                goto copy_pte;
+       }

        folio = page_folio(page);


-- 
Cheers,

David


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-21 12:58 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20  7:42 [PATCH v2 0/3] mm/ksm: fix ksm_zero_pages accounting on fork Xu Xin (vivo)
2026-11-20  7:43 ` [PATCH v2 1/3] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers " Xu Xin (vivo)
2026-11-21 12:58   ` David Hildenbrand (Arm)
2026-11-20  7:44 ` [PATCH v2 2/3] selftests/mm: fix false-positive KSM fork test by dropping init_global_file_handles() Xu Xin (vivo)
2026-11-21  7:45   ` sashiko-bot
2026-11-20  7:44 ` [PATCH v2 3/3] selftests/mm: verify ksm_zero_pages is not inherited on fork Xu Xin (vivo)
2026-11-21  7:45   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox