Skip to contentSkip to conversation

selftests/mm: add process_madvise() tests

PATCH Superseded wang lian 2 participants 5 messages Jul 14, 2025 – Jul 16, 2025

Subsystems (3)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

wang lian

author
#0 lore raw

PATCH v5 1/1 selftests/mm: add process_madvise() tests

Add tests for process_madvise(), focusing on verifying behavior under various conditions including valid usage and error cases.

---
Changelog v5:
- Refactor the remote_collapse test to concentrate on its primary goal confirming the successful remote invocation of process_madvise() on a child process.
- Split the validation logic for invalid pidfds out of the remote test and into two new (exited_process_pidfd and bad_pidfd).
- Based mm-new branch, can ensure clean application

Changelog v4: https://lore.kernel.org/lkml/[email protected]/
- Refine resource cleanup logic in test teardown to be more robust.
- Improve remote_collapse test to correctly handle different THP (Transparent Huge Page) policies ('always', 'madvise', 'never'), including handling race conditions with khugepaged.
- Resolve build errors

Changelog v3: https://lore.kernel.org/lkml/[email protected]/
- Rebased onto the latest mm-stable branch to ensure clean application.
- Refactor common signal handling logic into vm_util to reduce code duplication.
- Improve test robustness and diagnostics based on community feedback.
- Address minor code style and script corrections.

Changelog v2: https://lore.kernel.org/lkml/[email protected]/
- Drop MADV_DONTNEED tests based on feedback.
- Focus solely on process_madvise() syscall.
- Improve error handling and structure.
- Add future-proof flag test.
- Style and comment cleanups.

-V1: https://lore.kernel.org/lkml/[email protected]/

 tools/testing/selftests/mm/.gitignore     |   1 +
 tools/testing/selftests/mm/Makefile       |   1 +
 tools/testing/selftests/mm/process_madv.c | 304 ++++++++++++++++++++++
 tools/testing/selftests/mm/run_vmtests.sh |   5 +
 4 files changed, 311 insertions(+)
 create mode 100644 tools/testing/selftests/mm/process_madv.c
tools/testing/selftests/mm/.gitignore +1 -0
index f2dafa0b700b..e7b23a8a05fe 100644
@@ -21,6 +21,7 @@ on-fault-limit
 transhuge-stress
 pagemap_ioctl
 pfnmap
+process_madv
 *.tmp*
 protection_keys
 protection_keys_32
tools/testing/selftests/mm/Makefile +1 -0
index ae6f994d3add..d13b3cef2a2b 100644
@@ -85,6 +85,7 @@ TEST_GEN_FILES += mseal_test
 TEST_GEN_FILES += on-fault-limit
 TEST_GEN_FILES += pagemap_ioctl
 TEST_GEN_FILES += pfnmap
+TEST_GEN_FILES += process_madv
 TEST_GEN_FILES += thuge-gen
 TEST_GEN_FILES += transhuge-stress
 TEST_GEN_FILES += uffd-stress
tools/testing/selftests/mm/process_madv.c new file +304 -0

304 lines not on this page · raw message

tools/testing/selftests/mm/run_vmtests.sh +5 -0
index a38c984103ce..471e539d82b8 100755
@@ -65,6 +65,8 @@ separated by spaces:
 	test pagemap_scan IOCTL
 - pfnmap
 	tests for VM_PFNMAP handling
+- process_madv
+	test for process_madv
 - cow
 	test copy-on-write semantics
 - thp
@@ -425,6 +427,9 @@ CATEGORY="madv_guard" run_test ./guard-regions
 # MADV_POPULATE_READ and MADV_POPULATE_WRITE tests
 CATEGORY="madv_populate" run_test ./madv_populate
 
+# PROCESS_MADV test
+CATEGORY="process_madv" run_test ./process_madv
+
 CATEGORY="vma_merge" run_test ./merge
 
 if [ -x ./memfd_secret ]
Signature · 1 line
2.43.0
#1 lore raw
Quoted 9 lines · wang lian
tools/testing/selftests/mm/process_madv.c new file +7 -0
+TEST_F(process_madvise, remote_collapse)
+{
 
+	self->child_pid = fork();
+	ASSERT_NE(self->child_pid, -1);
 
+	ret = read(pipe_info[0], &info, sizeof(info));
+	if (ret <= 0) {
+		waitpid(self->child_pid, NULL, 0);

tools/testing/selftests/mm/process_madv.c line 208

Quoted 10 lines
tools/testing/selftests/mm/process_madv.c new file +9 -0
+		SKIP(return, "Failed to read child info from pipe.\n");
+	}
+	ASSERT_EQ(ret, sizeof(info));
 
+cleanup:
+	/* Cleanup */
+	kill(self->child_pid, SIGKILL);
+	waitpid(self->child_pid, NULL, 0);
+	if (pidfd >= 0)
+		close(pidfd);

The cleanup here won't get run if we skip or assert, skipping will return immediately (you could replace the return with a 'goto cleanup') and the asserts will exit the test immediately. This will mean we leak the child. This isn't an issue for things that are memory mapped or tracked with file descriptors, the harness will for a new child for each test so anything that's cleaned up with the process will be handled, but that doesn't apply to child processes.

I think doing the child setup in a fixture should DTRT but I haven't gone through in full detail to verify that this is the case.

author
#2 lore raw
Quoted 28 lines · wang lian
Quoted 8 lines
tools/testing/selftests/mm/process_madv.c new file +8 -0
+TEST_F(process_madvise, remote_collapse)
+{
+	self->child_pid = fork();
+	ASSERT_NE(self->child_pid, -1);
+
+	ret = read(pipe_info[0], &info, sizeof(info));
+	if (ret <= 0) {
+		waitpid(self->child_pid, NULL, 0);

tools/testing/selftests/mm/process_madv.c line 208

Quoted 10 lines
tools/testing/selftests/mm/process_madv.c new file +10 -0
+		SKIP(return, "Failed to read child info from pipe.\n");
+	}
+	ASSERT_EQ(ret, sizeof(info));
+
+cleanup:
+	/* Cleanup */
+	kill(self->child_pid, SIGKILL);
+	waitpid(self->child_pid, NULL, 0);
+	if (pidfd >= 0)
+		close(pidfd);

The cleanup here won't get run if we skip or assert, skipping will return immediately (you could replace the return with a 'goto cleanup') and the asserts will exit the test immediately. This will mean we leak the child. This isn't an issue for things that are memory mapped or tracked with file descriptors, the harness will for a new child for each test so anything that's cleaned up with the process will be handled, but that doesn't apply to child processes.

Quoted 2 lines

I think doing the child setup in a fixture should DTRT but I haven't gone through in full detail to verify that this is the case.

Thanks a lot for pointing this out — it's a very reasonable concern.

Fortunately, this situation is handled by FIXTURE_TEARDOWN_PARENT,
which reliably takes care of cleanup when the test exits early via ASSERT_* or SKIP().

During earlier testing (in v3), I did run into an issue where a missing cleanup led to run_vmtests hanging,which prompted me to make sure that subsequent versions correctly rely on the fixture teardown mechanism for child process termination.

So while your concern definitely makes sense, this specific case should be well-covered by the existing teardown logic.

Thanks again for the careful review!

Best regards,
wang lian

#3 lore raw
Quoted 15 lines · wang lian
Quoted 11 lines · wang lian

tools/testing/selftests/mm/process_madv.c line 208

Quoted 6 lines
tools/testing/selftests/mm/process_madv.c new file +5 -0
+	/* Cleanup */
+	kill(self->child_pid, SIGKILL);
+	waitpid(self->child_pid, NULL, 0);
+	if (pidfd >= 0)
+		close(pidfd);

The cleanup here won't get run if we skip or assert, skipping will return immediately (you could replace the return with a 'goto cleanup') and the asserts will exit the test immediately. This will mean we leak

Fortunately, this situation is handled by FIXTURE_TEARDOWN_PARENT,
which reliably takes care of cleanup when the test exits early via ASSERT_* or SKIP().

Quoted 6 lines

During earlier testing (in v3), I did run into an issue where a missing cleanup led to run_vmtests hanging,which prompted me to make sure that subsequent versions correctly rely on the fixture teardown mechanism for child process termination.

So while your concern definitely makes sense, this specific case should be well-covered by the existing teardown logic.

Are you sure the parent cleanup sees variables set in the child and actually takes effect? We fork() the child so it should be a new VM which means that values stored there won't be seen by the parent, it'll see whatever values it had before the fork().

It does also seem like bad practice to have duplicated cleanup code in both the test and the fixture cleanups, the fixture cleanup always runs so we should just use that all the time if it's in use (that's the whole idea really). Indeed as things stand since the cleanup in the test doesn't reset self->child_pid so if the cleanup at the fixture level does anything there's a minor risk that we might race with the PID being reused and kill some new task

The fixture teardown handler also doesn't do the close(pidfd), either that's redundant for the in test cleanup or should be in the fixture (given that it's a child process it should be redundant as any open file descriptors are closed when the process exits).

author
#4 lore raw
Quoted 36 lines · wang lian
Quoted 15 lines
Quoted 11 lines · wang lian

tools/testing/selftests/mm/process_madv.c line 208

Quoted 6 lines
tools/testing/selftests/mm/process_madv.c new file +5 -0
+	/* Cleanup */
+	kill(self->child_pid, SIGKILL);
+	waitpid(self->child_pid, NULL, 0);
+	if (pidfd >= 0)
+		close(pidfd);

The cleanup here won't get run if we skip or assert, skipping will return immediately (you could replace the return with a 'goto cleanup') and the asserts will exit the test immediately. This will mean we leak

Fortunately, this situation is handled by FIXTURE_TEARDOWN_PARENT,
which reliably takes care of cleanup when the test exits early via ASSERT_* or SKIP().

Quoted 6 lines

During earlier testing (in v3), I did run into an issue where a missing cleanup led to run_vmtests hanging,which prompted me to make sure that subsequent versions correctly rely on the fixture teardown mechanism for child process termination.

So while your concern definitely makes sense, this specific case should be well-covered by the existing teardown logic.

Are you sure the parent cleanup sees variables set in the child and actually takes effect? We fork() the child so it should be a new VM which means that values stored there won't be seen by the parent, it'll see whatever values it had before the fork().

It does also seem like bad practice to have duplicated cleanup code in both the test and the fixture cleanups, the fixture cleanup always runs so we should just use that all the time if it's in use (that's the whole idea really). Indeed as things stand since the cleanup in the test doesn't reset self->child_pid so if the cleanup at the fixture level does anything there's a minor risk that we might race with the PID being reused and kill some new task

Quoted 4 lines

The fixture teardown handler also doesn't do the close(pidfd), either that's redundant for the in test cleanup or should be in the fixture (given that it's a child process it should be redundant as any open file descriptors are closed when the process exits).

You're absolutely right — thank you for pointing this out.

The reason for the redundant in-test cleanup was due to a previous issue during development (in v3), where an ASSERT_EQ() failure at the end of the test left the child process lingering, causing run_vmtests to hang. To address that at the time, I added explicit cleanup in the test body.

However, as you correctly observed, relying on test-local variables after fork() is flawed, and duplicating cleanup both in the test and in the fixture teardown is not ideal.

The fixture teardown reliably handles cleanup and is the proper place for all resource release logic, including the close(pidfd) if it's needed.

I'll remove the redundant test-body cleanup and consolidate everything into the fixture teardown in the next version.
Thanks again for the detailed and helpful review.

Best regards,
wang lian