* [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; 4+ 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] 4+ 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-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, 0 replies; 4+ 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] 4+ 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-20 7:44 ` [PATCH v2 3/3] selftests/mm: verify ksm_zero_pages is not inherited on fork Xu Xin (vivo)
2 siblings, 0 replies; 4+ 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] 4+ 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)
2 siblings, 0 replies; 4+ 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] 4+ messages in thread
end of thread, other threads:[~2026-11-20 7:45 UTC | newest]
Thread overview: 4+ 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-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)
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.