selftests/mm: Add process_madvise() tests
Subsystems (3)
PATCH v2 1/1 selftests/mm: Add process_madvise() tests
This patch adds tests for the process_madvise(), focusing on verifying behavior under various conditions including valid usage and error cases.
---
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
tools/testing/selftests/mm/.gitignore +1 -0
@@ -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
tools/testing/selftests/mm/Makefile +1 -0
@@ -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
tools/testing/selftests/mm/process_madv.c new file +414 -0
415 lines not on this page · raw message
tools/testing/selftests/mm/run_vmtests.sh +5 -0
@@ -61,6 +61,8 @@ separated by spaces: ksm tests that require >=2 NUMA nodes - pkey memory protection key tests +- process_madvise + test process_madvise - soft_dirty test soft dirty page bit semantics - pagemap
@@ -424,6 +426,9 @@ CATEGORY="hmm" run_test bash ./test_hmm.sh smoke # MADV_GUARD_INSTALL and MADV_GUARD_REMOVE tests CATEGORY="madv_guard" run_test ./guard-regions +# PROCESS_MADVISE TEST +CATEGORY="process_madv" run_test ./process_madv + # MADV_DONTNEED and PROCESS_DONTNEED tests CATEGORY="madv_dontneed" run_test ./madv_dontneed
Signature · 1 line
2.43.0
Hi Wang,
Quoted 7 lines · On Mon, 30 Jun 2025 22:09:57 +0800 wang lian <[email protected]> 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]>
I left a few trivial comments below, but overall this looks useful to me. Thank you :)
FYI, 'git am' of this patch on latest mm-new fails. Maybe this is not based on the latest mm-new? But, I also found Andrew added this to mm tree:
https://lore.kernel.org/[email protected] . Maybe Andrew did rebase on his own?
[...]
Quoted 2 lines
tools/testing/selftests/mm/process_madv.c new file +0
-0
[...]
tools/testing/selftests/mm/process_madv.c line 122
Quoted 4 lines
tools/testing/selftests/mm/process_madv.c new file +4
-0+/* Try and read from a buffer, return true if no fatal signal. */ +static bool try_read_buf (char *ptr) +{ +return try_access_buf (ptr, false);
Seems this is the only caller of try_access_buf(). How about removing 'write' argument of try_access_buf() and its handling?
Quoted 7 lines
tools/testing/selftests/mm/process_madv.c new file +7
-0+} + +TEST_F(process_madvise, basic) +{ +const unsigned long pagesize = (unsigned long )sysconf (_SC_PAGESIZE); +const int madvise_pages =4 ; +char *map;
tools/testing/selftests/mm/process_madv.c line 139
Quoted 10 lines
tools/testing/selftests/mm/process_madv.c new file +10
-0+ssize_t ret; +struct iovec vec[madvise_pages]; + +/* +* Create a single large mapping. We will pick pages from this +* mapping to advise on. This ensures we test non-contiguous iovecs. +*/ + map =mmap (NULL , pagesize *10 ,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +ASSERT_NE (map,MAP_FAILED );
So this will make this test marked as failed, for mmap() failure, which doesn't really mean something in process_madvise() is wrong. What about using ksft_exit_skip() with failure check, instead?
Quoted 34 lines
tools/testing/selftests/mm/process_madv.c new file +34
-0+ +/* Fill the entire region with a known pattern. */ +memset (map,'A' , pagesize *10 ); + +/* +* Setup the iovec to point to 4 non-contiguous pages +* within the mapping. +*/ + vec[0 ].iov_base = &map[0 * pagesize]; + vec[0 ].iov_len = pagesize; + vec[1 ].iov_base = &map[3 * pagesize]; + vec[1 ].iov_len = pagesize; + vec[2 ].iov_base = &map[5 * pagesize]; + vec[2 ].iov_len = pagesize; + vec[3 ].iov_base = &map[8 * pagesize]; + vec[3 ].iov_len = pagesize; + + ret =sys_process_madvise (PIDFD_SELF , vec, madvise_pages,MADV_DONTNEED , +0 ); +if (ret ==-1 && errno ==EPERM ) +ksft_exit_skip ( +"process_madvise() unsupported or permission denied, try running as root.\n" ); +else if (errno ==EINVAL ) +ksft_exit_skip ( +"process_madvise() unsupported or parameter invalid, please check arguments.\n" ); + +/* The call should succeed and report the total bytes processed. */ +ASSERT_EQ (ret, madvise_pages * pagesize); + +/* Check that advised pages are now zero. */ +for (int i =0 ; i < madvise_pages; i++) { +char *advised_page = (char *)vec[i].iov_base; + +/* Access should be successful (kernel provides a new page). */
tools/testing/selftests/mm/process_madv.c line 183
Quoted 10 lines
tools/testing/selftests/mm/process_madv.c new file +10
-0+ASSERT_TRUE (try_read_buf (advised_page)); +/* Content must be 0, not 'A'. */ +ASSERT_EQ (*advised_page,0 ); + } + +/* Check that an un-advised page in between is still 'A'. */ +char *unadvised_page = &map[1 * pagesize]; + +ASSERT_TRUE (try_read_buf (unadvised_page)); +ASSERT_EQ (*unadvised_page,'A' );
What about using memcpy() and memcmp() for testing full bytes? That could do more complete test, and reduce unnecessary volatile and helper functions?
tools/testing/selftests/mm/process_madv.c line 186
Quoted 3 lines
tools/testing/selftests/mm/process_madv.c new file +3
-0+ +/* Cleanup. */ +ASSERT_EQ (munmap (map, pagesize *10 ),0 );
Again, I think ksft_exit_skip() might be a better approach.
Quoted 4 lines
tools/testing/selftests/mm/process_madv.c new file +4
-0+} + +static long get_smaps_anon_huge_pages (pid_t pid,void *addr) +{
tools/testing/selftests/mm/process_madv.c line 200
Quoted 10 lines
tools/testing/selftests/mm/process_madv.c new file +10
-0+char smaps_path[64 ]; +char *line =NULL ; +unsigned long start, end; +long anon_huge_kb; +size_t len; +FILE *f; +bool in_vma; + + in_vma = false; +sprintf (smaps_path,"/proc/%d/smaps" , pid);
I understand this is just a test code, but... What about using the safer one, snprintf()?
Quoted 26 lines
tools/testing/selftests/mm/process_madv.c new file +26
-0+ f =fopen (smaps_path,"r" ); +if (!f) +return -1 ; + +while (getline (&line, &len, f) !=-1 ) { +/* Check if the line describes a VMA range */ +if (sscanf (line,"%lx-%lx" , &start, &end) ==2 ) { +if ((unsigned long )addr >= start && + (unsigned long )addr < end) + in_vma = true; +else + in_vma = false; +continue ; + } + +/* If we are in the correct VMA, look for the AnonHugePages field */ +if (in_vma && +sscanf (line,"AnonHugePages: %ld kB" , &anon_huge_kb) ==1 ) +break ; + } + +free (line); +fclose (f); + +return (anon_huge_kb >0 ) ? (anon_huge_kb *1024 ) :0 ; +}
[...]
Quoted 2 lines
+TEST_HARNESS_MAIN
\ No newline at end of file
checkpatch.pl complaints having no newline at the end of the file.
tools/testing/selftests/mm/run_vmtests.sh separated by spaces: · line 64
Quoted 9 lines
tools/testing/selftests/mm/run_vmtests.sh +1
-0@@ -61,6 +61,8 @@ separated by spaces: ksm tests that require >=2 NUMA nodes - pkey memory protection key tests +- process_madvise
Shouldn't this match with the real category name (process_madv) ?
Quoted 16 lines
tools/testing/selftests/mm/run_vmtests.sh +4
-0+ test process_madvise - soft_dirty test soft dirty page bit semantics - pagemap@@ -424,6 +426,9 @@ CATEGORY="hmm" run_test bash ./test_hmm.sh smoke # MADV_GUARD_INSTALL and MADV_GUARD_REMOVE tests CATEGORY="madv_guard" run_test ./guard-regions +# PROCESS_MADVISE TEST +CATEGORY="process_madv" run_test ./process_madv + # MADV_DONTNEED and PROCESS_DONTNEED tests CATEGORY="madv_dontneed" run_test ./madv_dontneedSignature · 1 line
2.43.0
Thanks,
SJ
Hi SeongJae Park,
Thank you very much for your thorough review and encouraging feedback!
I will address all of these in the next version.
Thanks again for your time and valuable input!
Best regards,
Wang Lian
Quoted 80 lines · wang lian
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:
- DropMADV_DONTNEEDtests based on feedback
- Focus solely onprocess_madvise()syscall
- Improve error handling and structure
- Add future-proof flag test
- Style and comment cleanupstools/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.ctools/testing/selftests/mm/.gitignore +1
-0@@ -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_teststools/testing/selftests/mm/Makefile +1
-0@@ -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-stresstools/testing/selftests/mm/process_madv.c new file +27
-0@@ -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"
tools/testing/selftests/mm/process_madv.c line 37
Quoted 10 lines
tools/testing/selftests/mm/process_madv.c new file +10
-0+*/ +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.
tools/testing/selftests/mm/process_madv.c line 45
Quoted 8 lines
tools/testing/selftests/mm/process_madv.c new file +8
-0+ +static void handle_fatal (int c) +{ +if (!signal_jump_set) +return ; + +siglongjmp (signal_jmp_buf, c); +}
Also copy-pasted.
Quoted 19 lines
tools/testing/selftests/mm/process_madv.c new file +19
-0+ +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" ); +} +
tools/testing/selftests/mm/process_madv.c line 74
Quoted 10 lines
tools/testing/selftests/mm/process_madv.c new file +10
-0+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.
Signature · 3 lines
Cheers, David / dhildenb