Skip to content

Commit 08ff89b

Browse files
donettom-1akpm00
authored andcommitted
selftests/mm: add fork inheritance test for ksm_merging_pages counter
Add 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. Link: https://lkml.kernel.org/r/e7bb17d374133bd31a3e423aa9e46e1122e74971.1758648700.git.donettom@linux.ibm.com Signed-off-by: Donet Tom <donettom@linux.ibm.com> Acked-by: David Hildenbrand <david@redhat.com> Cc: Aboorva Devarajan <aboorvad@linux.ibm.com> Cc: Chengming Zhou <chengming.zhou@linux.dev> Cc: "Ritesh Harjani (IBM)" <ritesh.list@gmail.com> Cc: Wei Yang <richard.weiyang@gmail.com> Cc: xu xin <xu.xin16@zte.com.cn> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 4d6fc29 commit 08ff89b

1 file changed

Lines changed: 42 additions & 1 deletion

File tree

tools/testing/selftests/mm/ksm_functional_tests.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,46 @@ static void test_prot_none(void)
602602
munmap(map, size);
603603
}
604604

605+
static void test_fork_ksm_merging_page_count(void)
606+
{
607+
const unsigned int size = 2 * MiB;
608+
char *map;
609+
pid_t child_pid;
610+
int status;
611+
612+
ksft_print_msg("[RUN] %s\n", __func__);
613+
614+
map = mmap_and_merge_range(0xcf, size, PROT_READ | PROT_WRITE, KSM_MERGE_MADVISE);
615+
if (map == MAP_FAILED)
616+
return;
617+
618+
child_pid = fork();
619+
if (!child_pid) {
620+
init_global_file_handles();
621+
exit(ksm_get_self_merging_pages());
622+
} else if (child_pid < 0) {
623+
ksft_test_result_fail("fork() failed\n");
624+
goto unmap;
625+
}
626+
627+
if (waitpid(child_pid, &status, 0) < 0) {
628+
ksft_test_result_fail("waitpid() failed\n");
629+
goto unmap;
630+
}
631+
632+
status = WEXITSTATUS(status);
633+
if (status) {
634+
ksft_test_result_fail("ksm_merging_page in child: %d\n", status);
635+
goto unmap;
636+
}
637+
638+
ksft_test_result_pass("ksm_merging_pages is not inherited after fork\n");
639+
640+
unmap:
641+
ksm_stop();
642+
munmap(map, size);
643+
}
644+
605645
static void init_global_file_handles(void)
606646
{
607647
mem_fd = open("/proc/self/mem", O_RDWR);
@@ -620,7 +660,7 @@ static void init_global_file_handles(void)
620660

621661
int main(int argc, char **argv)
622662
{
623-
unsigned int tests = 8;
663+
unsigned int tests = 9;
624664
int err;
625665

626666
if (argc > 1 && !strcmp(argv[1], FORK_EXEC_CHILD_PRG_NAME)) {
@@ -652,6 +692,7 @@ int main(int argc, char **argv)
652692
test_prctl_fork();
653693
test_prctl_fork_exec();
654694
test_prctl_unmerge();
695+
test_fork_ksm_merging_page_count();
655696

656697
err = ksft_get_fail_cnt();
657698
if (err)

0 commit comments

Comments
 (0)