selftests/mm: add folio_split() and filemap_get_entry() race test
Subsystems (4)
PATCH 1/1 selftests/mm: add folio_split() and filemap_get_entry() race test
The added folio_split_race_test is a modified C port of the race condition test from [1]. The test creates shmem huge pages shared by both a parent and a child processes, where the parent process punches holes in the shmem to cause folio_split() in the kernel and the child process reads the shmem in 16 threads to cause filemap_get_entry() in the kernel. filemap_get_entry() reads the folio and xarray split by folio_split() locklessly. The original test[2] is written in rust and uses memfd (shmem backed). This C port uses shmem directly.
Note: the initial rust to C conversion is done by Cursor.
--- tools/testing/selftests/mm/Makefile | 1 + .../selftests/mm/folio_split_race_test.c | 380 ++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 2 + 3 files changed, 383 insertions(+) create mode 100644 tools/testing/selftests/mm/folio_split_race_test.c
tools/testing/selftests/mm/Makefile +1 -0
@@ -105,6 +105,7 @@ TEST_GEN_FILES += droppable TEST_GEN_FILES += guard-regions TEST_GEN_FILES += merge TEST_GEN_FILES += rmap +TEST_GEN_FILES += folio_split_race_test ifneq ($(ARCH),arm64) TEST_GEN_FILES += soft-dirty
tools/testing/selftests/mm/folio_split_race_test.c new file +380 -0
380 lines not on this page · raw message
tools/testing/selftests/mm/run_vmtests.sh +2 -0
@@ -555,6 +555,8 @@ if [ -n "${MOUNTED_XFS}" ]; then rm -f ${XFS_IMG} fi +CATEGORY="thp" run_test ./folio_split_race_test + CATEGORY="migration" run_test ./migration CATEGORY="mkdirty" run_test ./mkdirty
Signature · 1 line
2.51.0
Andrew Morton
maintainerQuoted 8 lines · On Fri, 13 Mar 2026 07:40:37 -0400 Zi Yan <[email protected]> wrote:
The added
folio_split_race_testis a modified C port of the race condition test from [1]. The test creates shmem huge pages shared by both a parent and a child processes, where the parent process punches holes in the shmem to causefolio_split()in the kernel and the child process reads the shmem in 16 threads to causefilemap_get_entry()in the kernel.filemap_get_entry()reads the folio and xarray split byfolio_split()locklessly. The original test[2] is written in rust and uses memfd (shmem backed). This C port uses shmem directly.
Cool.
Quoted 1 line
Note: the initial rust to C conversion is done by Cursor.
Hence the CaMeLcAsE "struct SharedCtl". Fair enough.
Quoted 2 lines
I couldn't immediately find any licensing info there. Bas and Adam, please advise? And sending your Signed-off-by's would be appropriate.
Thanks.
Quoted 11 lines · Andrew Morton
Quoted 8 lines · On Fri, 13 Mar 2026 07:40:37 -0400 Zi Yan <[email protected]> wrote:
The added
folio_split_race_testis a modified C port of the race condition test from [1]. The test creates shmem huge pages shared by both a parent and a child processes, where the parent process punches holes in the shmem to causefolio_split()in the kernel and the child process reads the shmem in 16 threads to causefilemap_get_entry()in the kernel.filemap_get_entry()reads the folio and xarray split byfolio_split()locklessly. The original test[2] is written in rust and uses memfd (shmem backed). This C port uses shmem directly.
Quoted 5 lines
Cool.
Quoted 1 line
Note: the initial rust to C conversion is done by Cursor.
Hence the CaMeLcAsE "struct SharedCtl". Fair enough.
Will change it to struct shared_ctl.
I also got some AI feedback[1] on this test program and will fix them in a new version. I will send the new one late next week, so that I can get feedback from human. :)
I will post the AI feedback inline in a separate email.
[1] https://sashiko.dev/#/patchset/20260313114037.3593642-1-ziy%40nvidia.com
Quoted 8 lines
Quoted 3 lines
I couldn't immediately find any licensing info there. Bas and Adam, please advise? And sending your Signed-off-by's would be appropriate.
Thanks.
--
Best Regards,
Yan, Zi
Quoted 16 lines · Zi Yan
The added
folio_split_race_testis a modified C port of the race condition test from [1]. The test creates shmem huge pages shared by both a parent and a child processes, where the parent process punches holes in the shmem to causefolio_split()in the kernel and the child process reads the shmem in 16 threads to causefilemap_get_entry()in the kernel.filemap_get_entry()reads the folio and xarray split byfolio_split()locklessly. The original test[2] is written in rust and uses memfd (shmem backed). This C port uses shmem directly.Note: the initial rust to C conversion is done by Cursor.
Link https://lore.kernel.org/all/CAKNNEtw5_kZomhkugedKMPOG-sxs5Q5OLumWJdiWXv+C9Yct0w@mail.gmail.com/ [1] open hereSigned-off-by Zi Yan <[email protected]>Cc Bas van Dijk <[email protected]>Cc Adam Bratschi-Kaye <[email protected]>
Quoted 6 lines
--- tools/testing/selftests/mm/Makefile | 1 + .../selftests/mm/folio_split_race_test.c | 380 ++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 2 + 3 files changed, 383 insertions(+) create mode 100644 tools/testing/selftests/mm/folio_split_race_test.c
I got some AI feedback from[1].
[1] https://sashiko.dev/#/patchset/20260313114037.3593642-1-ziy%40nvidia.com
<snip>
Quoted 5 lines
tools/testing/selftests/mm/folio_split_race_test.c new file +0
-0
<snip>
tools/testing/selftests/mm/folio_split_race_test.c line 123
Quoted 11 lines
tools/testing/selftests/mm/folio_split_race_test.c new file +11
-0+static void *reader_thread (void *arg) +{ +struct reader_arg *ra = (struct reader_arg *)arg; +unsigned char *base = ra->base; +struct SharedCtl *ctl = ra->ctl; +int tid = ra->tid; +atomic_size_t *failures = ra->failures; +atomic_size_t *verified = ra->verified; +size_t page_idx; + +while (atomic_load_explicit (&ctl->stop, memory_order_acquire) ==0 ) {
If parent exits early without setting ctl->stop to 1, child will loop forever.
I will add prctl(PR_SET_PDEATHSIG, SIGTERM) at the beginning of child, so that child gets TERM when parent exits.
tools/testing/selftests/mm/folio_split_race_test.c line 127
Quoted 4 lines
tools/testing/selftests/mm/folio_split_race_test.c new file +4
-0+for (page_idx = (size_t )tid; page_idx <TOTAL_PAGES ; + page_idx +=NUM_READER_THREADS ) { +if (page_idx %PUNCH_INTERVAL >=0 && + page_idx %PUNCH_INTERVAL <PUNCH_SIZE_FACTOR )
page_idx % PUNCH_INTERVAL >= 0 is a nop, since page_idx is unsigned.
I will leave it as is to show the range of page_idx % PUNCH_INTERVAL.
<snip>
Quoted 12 lines
tools/testing/selftests/mm/folio_split_race_test.c new file +12
-0+/* Run a single iteration. Returns total number of corrupted pages. */ +static size_t run_iteration (void ) +{ +struct SharedCtl *ctl; +pid_t pid; +unsigned char *parent_base; +bool *is_punched; +size_t i; +size_t child_failures, child_verified, parent_failures; +int status; +size_t n_punched =0 ; +
<snip>
tools/testing/selftests/mm/folio_split_race_test.c line 268
Quoted 4 lines
tools/testing/selftests/mm/folio_split_race_test.c new file +4
-0+ +/* ---- Parent process ---- */ +while (atomic_load_explicit (&ctl->ready, memory_order_acquire) ==0 ) +usleep (1000 );
If child exits/crashes without setting ctl->ready, parent can loop forever.
I will add waitpid(pid, &status, WNOHANG) == pid to check child status and break if child is gone.
<snip>
tools/testing/selftests/mm/folio_split_race_test.c line 302
Quoted 8 lines
tools/testing/selftests/mm/folio_split_race_test.c new file +8
-0+ +atomic_store_explicit (&ctl->stop,1 , memory_order_release); + +if (waitpid (pid, &status,0 ) != pid) +ksft_exit_fail_msg ("waitpid failed\n" ); + + child_failures =atomic_load_explicit (&ctl->child_failures, + memory_order_acquire);
If child exits abnormally without setting ctl->child_failures, we will miss an issue.
I will inspect child exit status and fail the test if child exits abnormally.
<snip>
Quoted 6 lines
tools/testing/selftests/mm/folio_split_race_test.c new file +6
-0+int main (void ) +{ +size_t iter; +size_t failures; +struct thp_settings current_settings; +bool failed = false;
tools/testing/selftests/mm/folio_split_race_test.c line 337
Quoted 10 lines
tools/testing/selftests/mm/folio_split_race_test.c new file +10
-0+ +ksft_print_header (); + +if (!thp_is_enabled ()) +ksft_exit_skip ("Transparent Hugepages not available\n" ); + +if (geteuid () !=0 ) { +ksft_print_msg ("Please run the benchmark as root\n" ); +ksft_finished (); + }
ksft_finished() here leads to a PASS status.
Will use ksft_exit_skip() instead.
Quoted 8 lines
tools/testing/selftests/mm/folio_split_race_test.c new file +8
-0+ +thp_save_settings (); +thp_read_settings (¤t_settings); + current_settings.shmem_enabled =SHMEM_ADVISE ; +thp_write_settings (¤t_settings); + +ksft_set_plan (1 ); +
<snip>
tools/testing/selftests/mm/folio_split_race_test.c line 359
Quoted 2 lines
tools/testing/selftests/mm/folio_split_race_test.c new file +2
-0+for (iter =1 ; iter <=NUM_ITERATIONS ; iter++) { + failures =run_iteration ();
run_iteration() has several ksft_exit_fail_msg() or if it crashes, THP settings are not restored.
Will add signal handlers and atexit() to call thp_restore_settings().
Quoted 11 lines
tools/testing/selftests/mm/folio_split_race_test.c new file +11
-0+if (failures >0 ) { + failed = true; +ksft_print_msg ( +"FAILED on iteration %zu: %zu pages corrupted by cross-process MADV_REMOVE!\n" , + iter, failures); +break ; + } + } + +thp_restore_settings (); +
tools/testing/selftests/mm/folio_split_race_test.c line 380
Quoted 10 lines
tools/testing/selftests/mm/folio_split_race_test.c new file +10
-0+if (failed) { +ksft_test_result_fail ("Test failed\n" ); +ksft_exit_fail (); + }else { +ksft_test_result_pass ("All %d iterations passed\n" ,NUM_ITERATIONS ); +ksft_exit_pass (); + } + +return 0 ; +}
--
Best Regards,
Yan, Zi
Quoted 5 lines · Zi Yan
The added
folio_split_race_testis a modified C port of the race condition test from [1]. The test creates shmem huge pages shared by both a parent and a child processes, where the parent process punches holes in the shmem to causefolio_split()in the kernel and the child process reads the shmem in 16 threads to causefilemap_get_entry()in the kernel.
Note that the fork isn't actually necessary to reproduce the issue. I had it working as
well with the reading and punching in the same process, but just with two separate
mappings. Not that it makes much of a difference - just in case you wanted to simplify
the test.
Quoted 2 lines · Andrew Morton
I couldn't immediately find any licensing info there. Bas and Adam, please advise?
GPL-2.0 license is fine by me.
Quoted 1 line
And sending your Signed-off-by's would be appropriate.
No problem:
Thanks,
Adam
No problem for me either.
I'll add a GPL-2.0 license to the repo.
Quoted 30 lines · Adam Bratschi-Kaye
Quoted 5 lines · Zi Yan
The added
folio_split_race_testis a modified C port of the race condition test from [1]. The test creates shmem huge pages shared by both a parent and a child processes, where the parent process punches holes in the shmem to causefolio_split()in the kernel and the child process reads the shmem in 16 threads to causefilemap_get_entry()in the kernel.Note that the fork isn't actually necessary to reproduce the issue. I had it working as
well with the reading and punching in the same process, but just with two separate
mappings. Not that it makes much of a difference - just in case you wanted to simplify
the test.Quoted 2 lines · Andrew Morton
I couldn't immediately find any licensing info there. Bas and Adam, please advise?
GPL-2.0 license is fine by me.
Quoted 1 line
And sending your Signed-off-by's would be appropriate.
No problem:
Signed-off-by Adam Bratschi-Kaye <[email protected]>Thanks,
Adam