From: Zi Yan <ziy@nvidia.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: David Hildenbrand <david@kernel.org>,
Lorenzo Stoakes <lorenzo.stoakes@oracle.com>,
Zi Yan <ziy@nvidia.com>, Hugh Dickins <hughd@google.com>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
"Liam R. Howlett" <Liam.Howlett@oracle.com>,
Nico Pache <npache@redhat.com>,
Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
Barry Song <baohua@kernel.org>, Lance Yang <lance.yang@linux.dev>,
Matthew Wilcox <willy@infradead.org>,
Bas van Dijk <bas@dfinity.org>,
Eero Kelly <eero.kelly@dfinity.org>,
Andrew Battat <andrew.battat@dfinity.org>,
Adam Bratschi-Kaye <adam.bratschikaye@dfinity.org>,
<linux-mm@kvack.org>, <linux-kernel@vger.kernel.org>,
<linux-fsdevel@vger.kernel.org>
Subject: Re: [PATCH] selftests/mm: add folio_split() and filemap_get_entry() race test
Date: Sat, 14 Mar 2026 20:03:23 -0400 [thread overview]
Message-ID: <EC71CE3C-08D9-48FE-95CB-B6A43B4BDB9C@nvidia.com> (raw)
In-Reply-To: <20260313114037.3593642-1-ziy@nvidia.com>
On 13 Mar 2026, at 7:40, Zi Yan 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 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.
>
> Link: https://lore.kernel.org/all/CAKNNEtw5_kZomhkugedKMPOG-sxs5Q5OLumWJdiWXv+C9Yct0w@mail.gmail.com/ [1]
> Link: https://github.com/dfinity/thp-madv-remove-test [2]
> Signed-off-by: Zi Yan <ziy@nvidia.com>
> Cc: Bas van Dijk <bas@dfinity.org>
> Cc: Adam Bratschi-Kaye <adam.bratschikaye@dfinity.org>
> ---
> 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>
> diff --git a/tools/testing/selftests/mm/folio_split_race_test.c b/tools/testing/selftests/mm/folio_split_race_test.c
> new file mode 100644
> index 0000000000000..cf5a5666bab77
> --- /dev/null
> +++ b/tools/testing/selftests/mm/folio_split_race_test.c
<snip>
> +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.
> + 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>
> +/* 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>
> +
> + /* ---- 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>
> +
> + 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>
> +int main(void)
> +{
> + size_t iter;
> + size_t failures;
> + struct thp_settings current_settings;
> + bool failed = false;
> +
> + 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.
> +
> + thp_save_settings();
> + thp_read_settings(¤t_settings);
> + current_settings.shmem_enabled = SHMEM_ADVISE;
> + thp_write_settings(¤t_settings);
> +
> + ksft_set_plan(1);
> +
<snip>
> + 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().
> + 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();
> +
> + 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
next prev parent reply other threads:[~2026-03-15 0:03 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-13 11:40 Zi Yan
2026-03-14 22:27 ` Andrew Morton
2026-03-14 23:44 ` Zi Yan
2026-03-15 0:03 ` Zi Yan [this message]
2026-03-17 7:31 ` [External Sender] " Adam Bratschi-Kaye
2026-03-17 9:43 ` Bas van Dijk
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=EC71CE3C-08D9-48FE-95CB-B6A43B4BDB9C@nvidia.com \
--to=ziy@nvidia.com \
--cc=Liam.Howlett@oracle.com \
--cc=adam.bratschikaye@dfinity.org \
--cc=akpm@linux-foundation.org \
--cc=andrew.battat@dfinity.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=bas@dfinity.org \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=eero.kelly@dfinity.org \
--cc=hughd@google.com \
--cc=lance.yang@linux.dev \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=npache@redhat.com \
--cc=ryan.roberts@arm.com \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox