Skip to content

Commit 8b8546d

Browse files
aigourenshengakpm00
authored andcommitted
selftests/mm:fix test_prctl_fork_exec return failure
After calling fork() in test_prctl_fork_exec(), the global variable ksm_full_scans_fd is initialized to 0 in the child process upon entering the main function of ./ksm_functional_tests. In the function call chain test_child_ksm() -> __mmap_and_merge_range -> ksm_merge-> ksm_get_full_scans, start_scans = ksm_get_full_scans() will return an error. Therefore, the value of ksm_full_scans_fd needs to be initialized before calling test_child_ksm in the child process. Link: https://lkml.kernel.org/r/20240617052934.5834-1-shechenglong001@gmail.com Signed-off-by: aigourensheng <shechenglong001@gmail.com> Acked-by: David Hildenbrand <david@redhat.com> Cc: Shuah Khan <shuah@kernel.org> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent ff20230 commit 8b8546d

1 file changed

Lines changed: 22 additions & 16 deletions

File tree

tools/testing/selftests/mm/ksm_functional_tests.c

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -656,12 +656,33 @@ static void test_prot_none(void)
656656
munmap(map, size);
657657
}
658658

659+
static void init_global_file_handles(void)
660+
{
661+
mem_fd = open("/proc/self/mem", O_RDWR);
662+
if (mem_fd < 0)
663+
ksft_exit_fail_msg("opening /proc/self/mem failed\n");
664+
ksm_fd = open("/sys/kernel/mm/ksm/run", O_RDWR);
665+
if (ksm_fd < 0)
666+
ksft_exit_skip("open(\"/sys/kernel/mm/ksm/run\") failed\n");
667+
ksm_full_scans_fd = open("/sys/kernel/mm/ksm/full_scans", O_RDONLY);
668+
if (ksm_full_scans_fd < 0)
669+
ksft_exit_skip("open(\"/sys/kernel/mm/ksm/full_scans\") failed\n");
670+
pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
671+
if (pagemap_fd < 0)
672+
ksft_exit_skip("open(\"/proc/self/pagemap\") failed\n");
673+
proc_self_ksm_stat_fd = open("/proc/self/ksm_stat", O_RDONLY);
674+
proc_self_ksm_merging_pages_fd = open("/proc/self/ksm_merging_pages",
675+
O_RDONLY);
676+
ksm_use_zero_pages_fd = open("/sys/kernel/mm/ksm/use_zero_pages", O_RDWR);
677+
}
678+
659679
int main(int argc, char **argv)
660680
{
661681
unsigned int tests = 8;
662682
int err;
663683

664684
if (argc > 1 && !strcmp(argv[1], FORK_EXEC_CHILD_PRG_NAME)) {
685+
init_global_file_handles();
665686
exit(test_child_ksm());
666687
}
667688

@@ -674,22 +695,7 @@ int main(int argc, char **argv)
674695

675696
pagesize = getpagesize();
676697

677-
mem_fd = open("/proc/self/mem", O_RDWR);
678-
if (mem_fd < 0)
679-
ksft_exit_fail_msg("opening /proc/self/mem failed\n");
680-
ksm_fd = open("/sys/kernel/mm/ksm/run", O_RDWR);
681-
if (ksm_fd < 0)
682-
ksft_exit_skip("open(\"/sys/kernel/mm/ksm/run\") failed\n");
683-
ksm_full_scans_fd = open("/sys/kernel/mm/ksm/full_scans", O_RDONLY);
684-
if (ksm_full_scans_fd < 0)
685-
ksft_exit_skip("open(\"/sys/kernel/mm/ksm/full_scans\") failed\n");
686-
pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
687-
if (pagemap_fd < 0)
688-
ksft_exit_skip("open(\"/proc/self/pagemap\") failed\n");
689-
proc_self_ksm_stat_fd = open("/proc/self/ksm_stat", O_RDONLY);
690-
proc_self_ksm_merging_pages_fd = open("/proc/self/ksm_merging_pages",
691-
O_RDONLY);
692-
ksm_use_zero_pages_fd = open("/sys/kernel/mm/ksm/use_zero_pages", O_RDWR);
698+
init_global_file_handles();
693699

694700
test_unmerge();
695701
test_unmerge_zero_pages();

0 commit comments

Comments
 (0)