[PATCH v2] selftests/mm: handle child status safely in khugepaged

From: Muhammad Mahdi Amirpour

Date: Mon Nov 02 2026 - 16:02:30 EST


collapse_fork(), collapse_fork_compound() and
collapse_max_ptes_shared() take the child result from
WEXITSTATUS() without checking WIFEXITED(). For a child
killed by a signal WEXITSTATUS() is 0, which is KSFT_PASS:
the parent runs its own checks and the test reports ok even
though the child crashed.

Fail the test when the child did not exit normally, and print
the signal that killed it. Also fail when fork() fails,
instead of reading an uninitialised status.

Use get_child_status() in all four fork tests, and use
wait_for_child() to handle fork() and waitpid() in the
three blocking-wait tests.

Fixes: bd212a7dbdb1 ("kselftest: mm: return fail when child test result is fail in khugepaged")
Signed-off-by: Muhammad Mahdi Amirpour <ampr.mhd@xxxxxxxxx>
---
tools/testing/selftests/mm/khugepaged.c | 61 +++++++++++++++++++------
1 file changed, 48 insertions(+), 13 deletions(-)

diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
index 1bc7acc66a46..b90daf008239 100644
--- a/tools/testing/selftests/mm/khugepaged.c
+++ b/tools/testing/selftests/mm/khugepaged.c
@@ -1004,9 +1004,44 @@ static void collapse_compound_extreme(struct collapse_context *c, struct mem_ops
ksft_test_result_report(exit_status, "%s\n", __func__);
}

-static void collapse_fork(struct collapse_context *c, struct mem_ops *ops)
+static int get_child_status(int wstatus)
+{
+ char buf[64];
+
+ if (WIFEXITED(wstatus))
+ return WEXITSTATUS(wstatus);
+
+ if (WIFSIGNALED(wstatus)) {
+ snprintf(buf, sizeof(buf), "Child killed by signal %d",
+ WTERMSIG(wstatus));
+ fail(buf);
+ } else {
+ fail("Child terminated abnormally");
+ }
+
+ return KSFT_FAIL;
+}
+
+static int wait_for_child(pid_t pid)
{
int wstatus;
+
+ if (pid < 0) {
+ fail("fork() failed");
+ return KSFT_FAIL;
+ }
+
+ if (waitpid(pid, &wstatus, 0) == -1) {
+ fail("waitpid() failed");
+ return KSFT_FAIL;
+ }
+
+ return get_child_status(wstatus);
+}
+
+static void collapse_fork(struct collapse_context *c, struct mem_ops *ops)
+{
+ pid_t pid;
void *p;

p = ops->setup_area(1);
@@ -1019,7 +1054,8 @@ static void collapse_fork(struct collapse_context *c, struct mem_ops *ops)
fail("Fail");

ksft_print_msg("Share small page over fork()...");
- if (!fork()) {
+ pid = fork();
+ if (pid == 0) {
/* Do not touch settings on child exit */
if (ops->check_huge(p, hpage_pmd_size, 0, hpage_pmd_size))
success("OK");
@@ -1035,8 +1071,7 @@ static void collapse_fork(struct collapse_context *c, struct mem_ops *ops)
_exit(exit_status);
}

- wait(&wstatus);
- exit_status = WEXITSTATUS(wstatus);
+ exit_status = wait_for_child(pid);
if (exit_status == KSFT_FAIL)
goto out;

@@ -1053,12 +1088,13 @@ static void collapse_fork(struct collapse_context *c, struct mem_ops *ops)

static void collapse_fork_compound(struct collapse_context *c, struct mem_ops *ops)
{
- int wstatus;
+ pid_t pid;
void *p;

p = alloc_hpage(ops);
ksft_print_msg("Share huge page over fork()...");
- if (!fork()) {
+ pid = fork();
+ if (pid == 0) {
/* Do not touch settings on child exit */
if (ops->check_huge(p, hpage_pmd_size, 1, hpage_pmd_size))
success("OK");
@@ -1085,8 +1121,7 @@ static void collapse_fork_compound(struct collapse_context *c, struct mem_ops *o
_exit(exit_status);
}

- wait(&wstatus);
- exit_status = WEXITSTATUS(wstatus);
+ exit_status = wait_for_child(pid);
if (exit_status == KSFT_FAIL)
goto out;

@@ -1104,12 +1139,13 @@ static void collapse_fork_compound(struct collapse_context *c, struct mem_ops *o
static void collapse_max_ptes_shared(struct collapse_context *c, struct mem_ops *ops)
{
int max_ptes_shared = thp_read_num("khugepaged/max_ptes_shared");
- int wstatus;
+ pid_t pid;
void *p;

p = alloc_hpage(ops);
ksft_print_msg("Share huge page over fork()...");
- if (!fork()) {
+ pid = fork();
+ if (pid == 0) {
/* Do not touch settings on child exit */
if (ops->check_huge(p, hpage_pmd_size, 1, hpage_pmd_size))
success("OK");
@@ -1146,8 +1182,7 @@ static void collapse_max_ptes_shared(struct collapse_context *c, struct mem_ops
_exit(exit_status);
}

- wait(&wstatus);
- exit_status = WEXITSTATUS(wstatus);
+ exit_status = wait_for_child(pid);
if (exit_status == KSFT_FAIL)
goto out;

@@ -1248,7 +1283,7 @@ static void collapse_fork_cow_race(struct collapse_context *c, struct mem_ops *o
for (; i < n; i++)
ip[i * stride] = i + 0xbeef0000;
/* A child that died reading the racing pages is a failure, not a zero */
- child_status = WIFEXITED(wstatus) ? WEXITSTATUS(wstatus) : KSFT_FAIL;
+ child_status = get_child_status(wstatus);

ksft_print_msg("Check the parent sees only its own writes...");
for (i = 0; i < n; i++)
--
2.55.0