Skip to content

Commit 0374af1

Browse files
Stefan Roeschakpm00
authored andcommitted
mm/ksm: test case for prctl fork/exec workflow
This adds a new test case to the ksm functional tests to make sure that the KSM setting is inherited by the child process when doing a fork/exec. Link: https://lkml.kernel.org/r/20230922211141.320789-3-shr@devkernel.io Signed-off-by: Stefan Roesch <shr@devkernel.io> Reviewed-by: David Hildenbrand <david@redhat.com> Cc: Carl Klemm <carl@uvos.xyz> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Rik van Riel <riel@surriel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 3c6f33b commit 0374af1

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

tools/testing/selftests/mm/ksm_functional_tests.c

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#define KiB 1024u
2828
#define MiB (1024 * KiB)
29+
#define FORK_EXEC_CHILD_PRG_NAME "ksm_fork_exec_child"
2930

3031
static int mem_fd;
3132
static int ksm_fd;
@@ -479,6 +480,64 @@ static void test_prctl_fork(void)
479480
ksft_test_result_pass("PR_SET_MEMORY_MERGE value is inherited\n");
480481
}
481482

483+
static int ksm_fork_exec_child(void)
484+
{
485+
/* Test if KSM is enabled for the process. */
486+
return prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0) == 1;
487+
}
488+
489+
static void test_prctl_fork_exec(void)
490+
{
491+
int ret, status;
492+
pid_t child_pid;
493+
494+
ksft_print_msg("[RUN] %s\n", __func__);
495+
496+
ret = prctl(PR_SET_MEMORY_MERGE, 1, 0, 0, 0);
497+
if (ret < 0 && errno == EINVAL) {
498+
ksft_test_result_skip("PR_SET_MEMORY_MERGE not supported\n");
499+
return;
500+
} else if (ret) {
501+
ksft_test_result_fail("PR_SET_MEMORY_MERGE=1 failed\n");
502+
return;
503+
}
504+
505+
child_pid = fork();
506+
if (child_pid == -1) {
507+
ksft_test_result_skip("fork() failed\n");
508+
return;
509+
} else if (child_pid == 0) {
510+
char *prg_name = "./ksm_functional_tests";
511+
char *argv_for_program[] = { prg_name, FORK_EXEC_CHILD_PRG_NAME };
512+
513+
execv(prg_name, argv_for_program);
514+
return;
515+
}
516+
517+
if (waitpid(child_pid, &status, 0) > 0) {
518+
if (WIFEXITED(status)) {
519+
status = WEXITSTATUS(status);
520+
if (status) {
521+
ksft_test_result_fail("KSM not enabled\n");
522+
return;
523+
}
524+
} else {
525+
ksft_test_result_fail("program didn't terminate normally\n");
526+
return;
527+
}
528+
} else {
529+
ksft_test_result_fail("waitpid() failed\n");
530+
return;
531+
}
532+
533+
if (prctl(PR_SET_MEMORY_MERGE, 0, 0, 0, 0)) {
534+
ksft_test_result_fail("PR_SET_MEMORY_MERGE=0 failed\n");
535+
return;
536+
}
537+
538+
ksft_test_result_pass("PR_SET_MEMORY_MERGE value is inherited\n");
539+
}
540+
482541
static void test_prctl_unmerge(void)
483542
{
484543
const unsigned int size = 2 * MiB;
@@ -536,9 +595,13 @@ static void test_prot_none(void)
536595

537596
int main(int argc, char **argv)
538597
{
539-
unsigned int tests = 7;
598+
unsigned int tests = 8;
540599
int err;
541600

601+
if (argc > 1 && !strcmp(argv[1], FORK_EXEC_CHILD_PRG_NAME)) {
602+
exit(ksm_fork_exec_child() == 1 ? 0 : 1);
603+
}
604+
542605
#ifdef __NR_userfaultfd
543606
tests++;
544607
#endif
@@ -576,6 +639,7 @@ int main(int argc, char **argv)
576639

577640
test_prctl();
578641
test_prctl_fork();
642+
test_prctl_fork_exec();
579643
test_prctl_unmerge();
580644

581645
err = ksft_get_fail_cnt();

0 commit comments

Comments
 (0)