mm/ksm: Fix incorrect accounting of KSM counters during fork.
Subsystems (5)
v3 cover mm/ksm: Fix incorrect accounting of KSM counters during fork.
The first patch in this series fixes the incorrect accounting of KSM counters such as ksm_merging_pages, ksm_rmap_items, and the global ksm_zero_pages during fork.
The following patch added a selftest to verify the ksm_merging_pages counter was updated correctly during fork.
Test Results
============
Without the first patch
-----------------------
# [RUN] test_fork_ksm_merging_page_count
not ok 10 ksm_merging_page in child: 32
With the first patch
--------------------
# [RUN] test_fork_ksm_merging_page_count
ok 10 ksm_merging_pages is not inherited after fork
Changes:
V2 -> v3:
- Added Reviewed-by and Acked-by tags
- Added cleanup on exit in ksm_merging_pages test.
- Dropped test_fork_global_ksm_zero_pages_count because it can give false failures if multiple processes attempt KSM zero-page merging and unmerging during test execution.
v2: https://lore.kernel.org/all/[email protected]/#t
Donet Tom (2):
mm/ksm: Fix incorrect KSM counter handling in mm_struct during fork selftests/mm: Added fork inheritance test for ksm_merging_pages
counter
include/linux/ksm.h | 8 +++- .../selftests/mm/ksm_functional_tests.c | 43 ++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-)
Signature · 1 line
2.51.0
PATCH v3 1/2 mm/ksm: Fix incorrect KSM counter handling in mm_struct during fork
Currently, the KSM-related counters in mm_struct, such as ksm_merging_pages, ksm_rmap_items, and ksm_zero_pages, are inherited by the child process during fork. This results in inconsistent accounting.
When a process uses KSM, identical pages are merged and an rmap item is created for each merged page. The ksm_merging_pages and ksm_rmap_items counters are updated accordingly. However, after a fork, these counters are copied to the child while the corresponding rmap items are not. As a result, when the child later triggers an unmerge, there are no rmap items present in the child, so the counters remain stale, leading to incorrect accounting.
A similar issue exists with ksm_zero_pages, which maintains both a global counter and a per-process counter. During fork, the per-process counter is inherited by the child, but the global counter is not incremented. Since the child also references zero pages, the global counter should be updated as well. Otherwise, during zero-page unmerge, both the global and per-process counters are decremented, causing the global counter to become inconsistent.
To fix this, ksm_merging_pages and ksm_rmap_items are reset to 0 during fork, and the global ksm_zero_pages counter is updated with the per-process ksm_zero_pages value inherited by the child. This ensures that KSM statistics remain accurate and reflect the activity of each process correctly.
--- include/linux/ksm.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
include/linux/ksm.h +7 -1
@@ -56,8 +56,14 @@ static inline long mm_ksm_zero_pages(struct mm_struct *mm)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)) +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);__ksm_enter (mm); + } }static inline int ksm_execve (struct mm_struct *mm)
Signature · 1 line
2.51.0
PATCH v3 2/2 selftests/mm: Added fork inheritance test for ksm_merging_pages counter
In reply to Donet TomAdded a new selftest to verify whether the ksm_merging_pages counter in mm_struct is not inherited by a child process after fork. This helps ensure correctness of KSM accounting across process creation.
--- .../selftests/mm/ksm_functional_tests.c | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-)
tools/testing/selftests/mm/ksm_functional_tests.c +42 -1
@@ -602,6 +602,46 @@ static void test_prot_none(void)munmap (map, size); } +static void test_fork_ksm_merging_page_count (void ) +{ +const unsigned int size =2 * MiB; +char *map; +pid_t child_pid; +int status; + +ksft_print_msg ("[RUN] %s\n" , __func__); + + map =mmap_and_merge_range (0xcf , size,PROT_READ |PROT_WRITE ,KSM_MERGE_MADVISE ); +if (map ==MAP_FAILED ) +return ; + + 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" ); + goto unmap; + } + +if (waitpid (child_pid, &status,0 ) <0 ) { +ksft_test_result_fail ("waitpid() failed\n" ); + goto unmap; + } + + status =WEXITSTATUS (status); +if (status) { +ksft_test_result_fail ("ksm_merging_page in child: %d\n" , status); + goto unmap; + } + +ksft_test_result_pass ("ksm_merging_pages is not inherited after fork\n" ); + +unmap: +ksm_stop (); +munmap (map, size); +} +static void init_global_file_handles (void ) { mem_fd =open ("/proc/self/mem" ,O_RDWR );
@@ -620,7 +660,7 @@ static void init_global_file_handles(void)int main (int argc,char **argv) { -unsigned int tests =8 ; +unsigned int tests =9 ;int err;if (argc >1 && !strcmp (argv[1 ],FORK_EXEC_CHILD_PRG_NAME )) {
@@ -652,6 +692,7 @@ int main(int argc, char **argv)test_prctl_fork ();test_prctl_fork_exec ();test_prctl_unmerge (); +test_fork_ksm_merging_page_count (); err =ksft_get_fail_cnt ();if (err)
Signature · 1 line
2.51.0