[PATCH 09/11] tools/testing/selftests: have CoW self test use MREMAP_RELOCATE_ANON

From: Lorenzo Stoakes
Date: Mon Jun 09 2025 - 09:30:45 EST


It is useful to have the CoW self-test invoke MREMAP_RELOCATE_ANON on
partial THP mappings, as this triggers folio split code paths and asserts
that this behaves correctly.

Add an additional set of tests to explicitly do so.

Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@xxxxxxxxxx>
---
tools/testing/selftests/mm/cow.c | 23 +++++++++++++++++++----
1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/mm/cow.c b/tools/testing/selftests/mm/cow.c
index dbbcc5eb3dce..c483bfd4269e 100644
--- a/tools/testing/selftests/mm/cow.c
+++ b/tools/testing/selftests/mm/cow.c
@@ -845,13 +845,14 @@ enum thp_run {
THP_RUN_SINGLE_PTE,
THP_RUN_SINGLE_PTE_SWAPOUT,
THP_RUN_PARTIAL_MREMAP,
+ THP_RUN_PARTIAL_MREMAP_RELOCATE_ANON,
THP_RUN_PARTIAL_SHARED,
};

static void do_run_with_thp(test_fn fn, enum thp_run thp_run, size_t thpsize)
{
char *mem, *mmap_mem, *tmp, *mremap_mem = MAP_FAILED;
- size_t size, mmap_size, mremap_size;
+ size_t size, mmap_size, mremap_size, mremap_flags;
int ret;

/* For alignment purposes, we need twice the thp size. */
@@ -927,6 +928,7 @@ static void do_run_with_thp(test_fn fn, enum thp_run thp_run, size_t thpsize)
size = pagesize;
break;
case THP_RUN_PARTIAL_MREMAP:
+ case THP_RUN_PARTIAL_MREMAP_RELOCATE_ANON:
/*
* Remap half of the THP. We need some new memory location
* for that.
@@ -939,8 +941,13 @@ static void do_run_with_thp(test_fn fn, enum thp_run thp_run, size_t thpsize)
log_test_result(KSFT_FAIL);
goto munmap;
}
- tmp = mremap(mem + mremap_size, mremap_size, mremap_size,
- MREMAP_MAYMOVE | MREMAP_FIXED, mremap_mem);
+
+ mremap_flags = MREMAP_MAYMOVE | MREMAP_FIXED;
+ if (thp_run == THP_RUN_PARTIAL_MREMAP_RELOCATE_ANON)
+ mremap_flags |= MREMAP_RELOCATE_ANON;
+
+ tmp = sys_mremap(mem + mremap_size, mremap_size, mremap_size,
+ mremap_flags, mremap_mem);
if (tmp != mremap_mem) {
ksft_perror("mremap() failed");
log_test_result(KSFT_FAIL);
@@ -1052,6 +1059,13 @@ static void run_with_partial_mremap_thp(test_fn fn, const char *desc, size_t siz
do_run_with_thp(fn, THP_RUN_PARTIAL_MREMAP, size);
}

+static void run_with_partial_mremap_relocate_anon_thp(test_fn fn, const char *desc, size_t size)
+{
+ ksft_print_msg("[RUN] %s ... with partially mremap(MREMAP_RELOCATE_ANON)'ed THP (%zu kB)\n",
+ desc, size / 1024);
+ do_run_with_thp(fn, THP_RUN_PARTIAL_MREMAP_RELOCATE_ANON, size);
+}
+
static void run_with_partial_shared_thp(test_fn fn, const char *desc, size_t size)
{
log_test_start("%s ... with partially shared THP (%zu kB)",
@@ -1247,6 +1261,7 @@ static void run_anon_test_case(struct test_case const *test_case)
run_with_single_pte_of_thp(test_case->fn, test_case->desc, size);
run_with_single_pte_of_thp_swap(test_case->fn, test_case->desc, size);
run_with_partial_mremap_thp(test_case->fn, test_case->desc, size);
+ run_with_partial_mremap_relocate_anon_thp(test_case->fn, test_case->desc, size);
run_with_partial_shared_thp(test_case->fn, test_case->desc, size);

thp_pop_settings();
@@ -1270,7 +1285,7 @@ static int tests_per_anon_test_case(void)
{
int tests = 2 + nr_hugetlbsizes;

- tests += 6 * nr_thpsizes;
+ tests += 7 * nr_thpsizes;
if (pmdsize)
tests += 2;
return tests;
--
2.49.0