mm/ksm: fix ksm_zero_pages accounting on fork
Subsystems (7)
Xu Xin (vivo)
reviewerv2 cover mm/ksm: fix ksm_zero_pages accounting on fork
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(-)
Signature · 1 line
2.34.1
Xu Xin (vivo)
reviewerPATCH v2 1/3 mm/ksm: do not inherit ksm_zero_pages counter or PTE markers on fork
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/[email protected]/
--- Documentation/admin-guide/mm/ksm.rst | 7 ++++++- include/linux/ksm.h | 18 +++++++++++++++--- mm/memory.c | 2 ++ 3 files changed, 23 insertions(+), 4 deletions(-)
Documentation/admin-guide/mm/ksm.rst +6 -1
@@ -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.
include/linux/ksm.h +15 -3
@@ -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) { }
mm/memory.c +2 -0
@@ -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); }
Signature · 1 line
2.34.1
Xu Xin (vivo)
reviewerPATCH v2 2/3 selftests/mm: fix false-positive KSM fork test by dropping init_global_file_handles()
In reply to Xu Xin (vivo)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().
--- tools/testing/selftests/mm/ksm_functional_tests.c | 1 - 1 file changed, 1 deletion(-)
tools/testing/selftests/mm/ksm_functional_tests.c +0 -1
@@ -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" );
Signature · 1 line
2.34.1
Xu Xin (vivo)
reviewerPATCH v2 3/3 selftests/mm: verify ksm_zero_pages is not inherited on fork
In reply to Xu Xin (vivo)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.
--- .../selftests/mm/ksm_functional_tests.c | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-)
tools/testing/selftests/mm/ksm_functional_tests.c +25 -8
@@ -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 ();
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v2 2/3] selftests/mm: fix false-positive KSM fork test by dropping init_global_file_handles()
In reply to Xu Xin (vivo)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]…
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) <[email protected]>
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().
tools/testing/selftests/mm/ksm_functional_tests.c static void test_fork_ksm_merging_page_count(void) · line 673
Quoted 10 lines
tools/testing/selftests/mm/ksm_functional_tests.c +0
-1@@ -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 ksmdtest_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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH v2 3/3] selftests/mm: verify ksm_zero_pages is not inherited on fork
In reply to Xu Xin (vivo)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() --
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) <[email protected]>
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().
Quoted 31 lines
tools/testing/selftests/mm/ksm_functional_tests.c +10
-1@@ -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;
tools/testing/selftests/mm/ksm_functional_tests.c static void test_prot_none(void) · line 688
Quoted 10 lines
tools/testing/selftests/mm/ksm_functional_tests.c +7
-1+ +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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
David Hildenbrand (Arm)
maintainerRe: [PATCH v2 1/3] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers on fork
In reply to Xu Xin (vivo)Quoted 56 lines · Xu Xin (vivo)
As documented in
Documentation/admin-guide/mm/ksm.rst,ksm_zero_pagesaccounts 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 globalksm_zero_pagescounter.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 duringfork(). Later, to preventksm_might_unmap_zero_page()from underflowing the counters on child exit[1], commit4d6fc29f3634("mm/ksm: fix incorrect KSM counter handling inmm_structduring 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_pagesto 0 inksm_fork()and clearing the KSM zero-page marker viamight_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_UNMERGEABLEin 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")Signed-off-by Xu Xin (vivo) <[email protected]>--- Documentation/admin-guide/mm/ksm.rst | 7 ++++++- include/linux/ksm.h | 18 +++++++++++++++--- mm/memory.c | 2 ++ 3 files changed, 23 insertions(+), 4 deletions(-)Documentation/admin-guide/mm/ksm.rst +6
-1@@ -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.include/linux/ksm.h +0
-0
include/linux/ksm.h void __ksm_exit(struct mm_struct *mm); · line 35
Quoted 4 lines
include/linux/ksm.h +1
-0@@ -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 ...
include/linux/ksm.h void __ksm_exit(struct mm_struct *mm); · line 37
Quoted 2 lines
include/linux/ksm.h +2
-0+static inline pte_t might_clear_ksm_zero_pte (pte_t pte) +{
Explain here *why* we are doing this.
include/linux/ksm.h void __ksm_exit(struct mm_struct *mm); · line 39
Quoted 2 lines
include/linux/ksm.h +2
-0+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:
mm/memory.c +0 -0
@@ -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);
Signature · 3 lines
Cheers, David
Re: [PATCH v2 2/3] selftests/mm: fix false-positive KSM fork test by dropping init_global_file_handles()
In reply to [email protected]Quoted 2 lines
Can ksmd race with the child process across
fork()and cause a false test failure?
Yes.
The check in patch 3 failed once with the whole series applied, while ksmd ran with pages_to_scan=2000 and sleep_millisecs=0. test_prctl_fork_exec() sets these values and does not restore them on its error paths.
With the default settings it passed 20 of 20 runs. Writing 0 to /sys/kernel/mm/ksm/run before fork() avoided the race in my own test.
Thanks,
Karl
Re: [PATCH v2 1/3] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers on fork
In reply to Xu Xin (vivo)Quoted 4 lines · Xu Xin (vivo)
With the marker cleared,
ksm_might_unmap_zero_page()will not decrement the counters on child exit,MADV_UNMERGEABLEin the child will not unnecessarily unshare inherited zero pages, and the global counter remains untouched.
Confirmed on mm-everything 758802f13004 with the smart-scan v4 series. A test merged 1024 zero pages with use_zero_pages=1 and forked. Without this patch, the child started with ksm_zero_pages=1024, the global counter doubled, and MADV_UNMERGEABLE in the child allocated new pages. With it, none of this happens.
The new check from 3/3 fails without this patch and passes with it, at the default ksmd settings. Both tests gave the same results on x86-64, i386, arm64, arm, riscv64, s390x, ppc64le, ppc64, ppc32, mips64el, mipsel, sparc64, alpha, parisc, m68k and sh4 in QEMU.
Thanks,
Karl