Re: [PATCH 2/2] selftests/mm: add fork inheritance test for ksm_merging_pages counter
From: Donet Tom
Date: Wed Aug 27 2025 - 14:03:52 EST
Hi David
On 8/26/25 6:52 PM, David Hildenbrand wrote:
On 26.08.25 14:49, Donet Tom wrote:
Add a new selftest to verify whether the `ksm_merging_pages` counter
"to verify ... is *not* inherited" ?
I will change it in next version.
in `mm_struct` is inherited by a child process after fork. This helps
ensure correctness of KSM accounting across process creation.
Signed-off-by: Donet Tom <donettom@xxxxxxxxxxxxx>
---
.../selftests/mm/ksm_functional_tests.c | 42 ++++++++++++++++++-
1 file changed, 41 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c
b/tools/testing/selftests/mm/ksm_functional_tests.c
index 712f43c87736..d971394c9567 100644
--- a/tools/testing/selftests/mm/ksm_functional_tests.c
+++ b/tools/testing/selftests/mm/ksm_functional_tests.c
@@ -602,6 +602,45 @@ static void test_prot_none(void)
munmap(map, size);
}
+static void test_fork_ksm_merging_page(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) {
+ int mpages;
+
+ init_global_file_handles();
+ mpages = ksm_get_self_merging_pages();
+ if (mpages > 0)
+ ksft_test_result_fail("ksm_merging_page in child: %d\n",
mpages);
+
+ exit(0);
+ } else if (child_pid < 0) {
+ ksft_test_result_fail("fork() failed\n");
+ return;
+ }
+
+ if (waitpid(child_pid, &status, 0) < 0) {
+ ksft_test_result_fail("waitpid() failed\n");
+ return;
+ }
+
+ ksft_test_result_pass("ksm_merging_pages is not inherited after
fork\n");
Won't this trigger a fail in the child and a pass in the parent process?
You should likely instead let the child return the result (or the
number) and check that here.
Thanks for pointing this out. I will make the change in the next version