On 30.06.25 16:09, wang lian wrote:
This patch adds tests for the process_madvise(), focusing on
verifying behavior under various conditions including valid
usage and error cases.

Signed-off-by: wang lian<[email protected]>
Suggested-by: Lorenzo Stoakes <[email protected]>
Suggested-by: David Hildenbrand <[email protected]>
---

Changelog v2:
- 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

  tools/testing/selftests/mm/.gitignore     |   1 +
  tools/testing/selftests/mm/Makefile       |   1 +
  tools/testing/selftests/mm/process_madv.c | 414 ++++++++++++++++++++++
  tools/testing/selftests/mm/run_vmtests.sh |   5 +
  4 files changed, 421 insertions(+)
  create mode 100644 tools/testing/selftests/mm/process_madv.c

diff --git a/tools/testing/selftests/mm/.gitignore 
b/tools/testing/selftests/mm/.gitignore
index 911f39d634be..a8c3be02188c 100644
--- a/tools/testing/selftests/mm/.gitignore
+++ b/tools/testing/selftests/mm/.gitignore
@@ -42,6 +42,7 @@ memfd_secret
  hugetlb_dio
  pkey_sighandler_tests_32
  pkey_sighandler_tests_64
+process_madv
  soft-dirty
  split_huge_page_test
  ksm_tests
diff --git a/tools/testing/selftests/mm/Makefile 
b/tools/testing/selftests/mm/Makefile
index 2352252f3914..725612e09582 100644
--- a/tools/testing/selftests/mm/Makefile
+++ b/tools/testing/selftests/mm/Makefile
@@ -86,6 +86,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
diff --git a/tools/testing/selftests/mm/process_madv.c 
b/tools/testing/selftests/mm/process_madv.c
new file mode 100644
index 000000000000..73999c8e3570
--- /dev/null
+++ b/tools/testing/selftests/mm/process_madv.c
@@ -0,0 +1,414 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#define _GNU_SOURCE
+#include "../kselftest_harness.h"
+#include <errno.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+#include <sched.h>
+#include <sys/pidfd.h>
+#include "vm_util.h"
+
+#include "../pidfd/pidfd.h"
+
+/*
+ * Ignore the checkpatch warning, as per the C99 standard, section 7.14.1.1:
+ *
+ * "If the signal occurs other than as the result of calling the abort or raise
+ *  function, the behavior is undefined if the signal handler refers to any
+ *  object with static storage duration other than by assigning a value to an
+ *  object declared as volatile sig_atomic_t"
+ */
+static volatile sig_atomic_t signal_jump_set;
+static sigjmp_buf signal_jmp_buf;
+
+/*
+ * Ignore the checkpatch warning, we must read from x but don't want to do
+ * anything with it in order to trigger a read page fault. We therefore must 
use
+ * volatile to stop the compiler from optimising this away.
+ */
+#define FORCE_READ(x) (*(volatile typeof(x) *)x)

Instead of copying that, it should probably be moved to vm_util.h.

Essentially, also the comments for signal_jump_set are copy-pasted from guard-regions.c. Is there a way to avoid that?

For example, we could place stuff like signal_jump_set in vm_util.c instead.

+
+static void handle_fatal(int c)
+{
+       if (!signal_jump_set)
+               return;
+
+       siglongjmp(signal_jmp_buf, c);
+}

Also copy-pasted.

+
+FIXTURE(process_madvise)
+{
+       int pidfd;
+       int flag;
+};
+
+static void setup_sighandler(void)
+{
+       struct sigaction act = {
+               .sa_handler = &handle_fatal,
+               .sa_flags = SA_NODEFER,
+       };
+
+       sigemptyset(&act.sa_mask);
+       if (sigaction(SIGSEGV, &act, NULL))
+               ksft_exit_fail_perror("sigaction");
+}
+
+static void teardown_sighandler(void)
+{
+       struct sigaction act = {
+               .sa_handler = SIG_DFL,
+               .sa_flags = SA_NODEFER,
+       };
+
+       sigemptyset(&act.sa_mask);
+       sigaction(SIGSEGV, &act, NULL);
+}

These two as well.

Let's avoid that much copy-pasting.

--
Cheers,

David / dhildenb


Reply via email to