Skip to contentSkip to conversation

selftests/mm: add folio_split() and filemap_get_entry() race test

PATCH Superseded Zi Yan 3 participants 7 messages Mar 19 – Mar 20

Subsystems (4)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Zi Yan

author
#0 lore raw

PATCH v3 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, where the main thread punches holes in the shmem to cause folio_split() in the kernel and a set of 16 threads reads the shmem 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 and use a single process.

Note: the initial rust to C conversion is done by Cursor.

---
From V2:
1. simplied the program by removing fork.

From V1:
1. added prctl(PR_SET_PDEATHSIG, SIGTERM) to avoid child looping forever.
2. removed page_idx % PUNCH_INTERVAL >= 0, since it is a nop. Added a comment.
3. added a child process status check to prevent parent looping forever and record that as a failure.
4. used ksft_exit_skip() instead of ksft_finished() when the program is not running as root.
5. restored THP settings properly when the program exits abnormally.

 tools/testing/selftests/mm/Makefile           |   1 +
 .../selftests/mm/folio_split_race_test.c      | 292 ++++++++++++++++++
 tools/testing/selftests/mm/run_vmtests.sh     |   2 +
 3 files changed, 295 insertions(+)
 create mode 100644 tools/testing/selftests/mm/folio_split_race_test.c
tools/testing/selftests/mm/Makefile +1 -0
index 7a5de4e9bf520..cd24596cdd27e 100644
@@ -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 +292 -0

292 lines not on this page · raw message

tools/testing/selftests/mm/run_vmtests.sh +2 -0
index 606558cc3b098..530980fdf3227 100755
@@ -553,6 +553,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
maintainer
#1 lore raw
Quoted 10 lines · On Thu, 19 Mar 2026 15:04:08 -0400 Zi Yan <[email protected]> wrote:

The added folio_split_race_test is a modified C port of the race condition test from [1]. The test creates shmem huge pages, where the main thread punches holes in the shmem to cause folio_split() in the kernel and a set of 16 threads reads the shmem 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 and use a single process.

Note: the initial rust to C conversion is done by Cursor.

Sashiko has questions:

	https://sashiko.dev/#/patchset/20260319190409.294523-1-ziy%40nvidia.com

(this is getting painful :(. I hope they're worthwhile)

author
#2 lore raw
Quoted 11 lines · Andrew Morton
Quoted 9 lines · On Thu, 19 Mar 2026 15:04:08 -0400 Zi Yan <[email protected]> wrote:

The added folio_split_race_test is a modified C port of the race condition test from [1]. The test creates shmem huge pages, where the main thread punches holes in the shmem to cause folio_split() in the kernel and a set of 16 threads reads the shmem 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 and use a single process.

Quoted 6 lines
Quoted 1 line

Note: the initial rust to C conversion is done by Cursor.

Sashiko has questions:

	https://sashiko.dev/#/patchset/20260319190409.294523-1-ziy%40nvidia.com

(this is getting painful :(. I hope they're worthwhile)

Q1: Does this test actually trigger filemap_get_entry() in the kernel?

Yes, when MADV_REMOVE triggers folio_split(), in remap_page(), after-split folios are not mapped. So subsequent accesses to these folios will trigger page fault and filemap_get_entry().

Q2: Does this loop advance the index further than intended?

Will fix it.

Q3: Is it safe to call thp_restore_settings() from a signal handler?

There is a theoretical hazard but it is unlikely to happen in practice, since it would only occur when thp_restore_settings() errors out to use printf().

Q4: Use %zu for uint64_t.

Will use %" PRIu64 " instead.

Best Regards,
Yan, Zi

#3 lore raw
In reply to Zi Yan

(-cc old email)

Quoted 9 lines · Zi Yan

The added folio_split_race_test is a modified C port of the race condition test from [1]. The test creates shmem huge pages, where the main thread punches holes in the shmem to cause folio_split() in the kernel and a set of 16 threads reads the shmem 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 and use a single process.

Quoted 7 lines

Note: the initial rust to C conversion is done by Cursor.

No .gitignore entry? :)

(Just saw it flag up locally in git status)

Linus says don't remove, but he doesn't say don't add :P

Cheers, Lorenzo

author
#4 lore raw
Quoted 20 lines · Lorenzo Stoakes (Oracle)

(-cc old email)

Quoted 9 lines · Zi Yan

The added folio_split_race_test is a modified C port of the race condition test from [1]. The test creates shmem huge pages, where the main thread punches holes in the shmem to cause folio_split() in the kernel and a set of 16 threads reads the shmem 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 and use a single process.

Quoted 7 lines

Note: the initial rust to C conversion is done by Cursor.

Quoted 5 lines

No .gitignore entry? :)

(Just saw it flag up locally in git status)

Linus says don't remove, but he doesn't say don't add :P

I did not know about that. The fixup is below. Thank you for pointing it out.

From bd164a7090c0e8b6e3013502c64c161214e38714 Mon Sep 17 00:00:00 2001
From: Zi Yan <[email protected]>
Date: Fri, 20 Mar 2026 13:56:04 -0400
Subject: [PATCH] git: add generated binary into gitignore

---
 tools/testing/selftests/mm/.gitignore | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/mm/.gitignore +1 -0
index 83ad9454dd9d1..b0c30c5ee9e30 100644
@@ -61,3 +61,4 @@ guard-regions
 merge
 prctl_thp_disable
 rmap
+folio_split_race_test
Signature · 6 lines
2.51.0



Best Regards,
Yan, Zi
#5 lore raw
Quoted 22 lines · Zi Yan
Quoted 20 lines · Lorenzo Stoakes (Oracle)

(-cc old email)

Quoted 16 lines · Zi Yan

The added folio_split_race_test is a modified C port of the race condition test from [1]. The test creates shmem huge pages, where the main thread punches holes in the shmem to cause folio_split() in the kernel and a set of 16 threads reads the shmem 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 and use a single process.

Note: the initial rust to C conversion is done by Cursor.

Quoted 7 lines
Quoted 5 lines

No .gitignore entry? :)

(Just saw it flag up locally in git status)

Linus says don't remove, but he doesn't say don't add :P

I did not know about that. The fixup is below. Thank you for pointing it out.

To be clear I'm referencing this re: the .gitignore _removals_ ;)

https://lore.kernel.org/linux-mm/CAHk-=wiHK5_oBUdUiNAaevmN9f-ORe+QBqbRefAZaw-RbgEn3w@mail.gmail.com/

Thanks! :)

Quoted 25 lines

From bd164a7090c0e8b6e3013502c64c161214e38714 Mon Sep 17 00:00:00 2001
From: Zi Yan <[email protected]>
Date: Fri, 20 Mar 2026 13:56:04 -0400
Subject: [PATCH] git: add generated binary into gitignore

---
 tools/testing/selftests/mm/.gitignore | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/mm/.gitignore +1 -1
index 83ad9454dd9d1..b0c30c5ee9e30 100644
@@ -61,3 +61,4 @@ guard-regions
 merge
 prctl_thp_disable
 rmap
+folio_split_race_test
--

2.51.0

Quoted 2 lines

Best Regards,
Yan, Zi

Cheers, Lorenzo

author
#6 lore raw
Quoted 29 lines · Lorenzo Stoakes (Oracle)
Quoted 28 lines · Zi Yan
Quoted 25 lines · Lorenzo Stoakes (Oracle)

(-cc old email)

Quoted 9 lines · Zi Yan

The added folio_split_race_test is a modified C port of the race condition test from [1]. The test creates shmem huge pages, where the main thread punches holes in the shmem to cause folio_split() in the kernel and a set of 16 threads reads the shmem 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 and use a single process.

Quoted 7 lines

Note: the initial rust to C conversion is done by Cursor.

No .gitignore entry? :)

(Just saw it flag up locally in git status)

Linus says don't remove, but he doesn't say don't add :P

Quoted 7 lines
Quoted 1 line

I did not know about that. The fixup is below. Thank you for pointing it out.

To be clear I'm referencing this re: the .gitignore _removals_ ;)

https://lore.kernel.org/linux-mm/CAHk-=wiHK5_oBUdUiNAaevmN9f-ORe+QBqbRefAZaw-RbgEn3w@mail.gmail.com/

Thanks! :)

I did not know .gitignore file exists in that directory. Also good to know about the do-not-remove rule.

Quoted 26 lines
Quoted 26 lines

From bd164a7090c0e8b6e3013502c64c161214e38714 Mon Sep 17 00:00:00 2001
From: Zi Yan <[email protected]>
Date: Fri, 20 Mar 2026 13:56:04 -0400
Subject: [PATCH] git: add generated binary into gitignore

---
 tools/testing/selftests/mm/.gitignore | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/mm/.gitignore +1 -1
index 83ad9454dd9d1..b0c30c5ee9e30 100644
@@ -61,3 +61,4 @@ guard-regions
 merge
 prctl_thp_disable
 rmap
+folio_split_race_test
--

2.51.0

Quoted 4 lines
Quoted 2 lines

Best Regards,
Yan, Zi

Cheers, Lorenzo

Best Regards,
Yan, Zi