Skip to contentSkip to conversation

selftests/mm: allocate nr_pages based on pmd size for uffd pmd_move tests

PATCH In review John J. Coleman 4 participants 6 messages Nov 13 – Nov 18

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/mm: allocate nr_pages based on pmd size for uffd pmd_move tests

When executing the uffd-unit-test, the "move-pmd on anon" test will fail on aarch64+64k with a segmentation fault because the initial allocated memory region is too small. This only happens with the move-pmd* tests because the architecture base page size is still utilized even though the memory region is specific to the size indicated by the pmd.

Patch 9f5c2aec3388 ("selftests/mm: fix uffd move-pmd test crash on 64K base page systems") implements an initial fix to the segmentation fault by skipping the test if the allocated memory region is not large enough for the number of pages x page size requested. Since this will skip over the aarch64+64k pmd tests, this patch is intended to resolve the problem with 64k pages by pre-allocating the memory region with 2x the pmd page size (for area_src and area_dst) for only the move_pmd tests by using the uffd_test_case_ops pre_alloc hook.

Fixes a2bf6a9ca805 ("selftests/mm: add UFFDIO_MOVE ioctl test")

---

I initially included patches V1 and V2 through the linked email thread. V1 and V2 modified UFFD_TEST_MEM_SIZE contingent upon base page size and a multiplier. However, sashiko review observed alignment issues with powerpc64 with 2MiB and 16MiB pmd sizes as well as 32bit type overflows.

Subseqent to sashiko review I decided to make a significant change hence why I created a new patch rather than continuing with V3. This patch leaves UFFD_TEST_MEM_SIZE at the 32MiB arbitrary value and utilizes the pre_alloc callback within the uffd_test_case_ops object to set nr_pages based on 2x retrieved PMD size divided by the base page size for both source and destination test allocated memory regions.

 tools/testing/selftests/mm/uffd-unit-tests.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/testing/selftests/mm/uffd-unit-tests.c +15 -0
index 2a97ba1d6feb1b35d6281449fd1917793a64fab1..0846de62689f710bab320e399d0bb5849b9408ff 100644
@@ -2226,6 +2226,20 @@ static void uffd_mmap_changing_test(uffd_global_test_opts_t *gopts, uffd_test_ar
 	uffd_test_pass();
 }
 
+static int set_pmd_pagesize_nr_pages(uffd_global_test_opts_t *gopts, const char **errmsg)
+{
+	uint64_t pmd_pagesize = read_pmd_pagesize();
+	if (!pmd_pagesize) {
+		if(errmsg)
+			*errmsg = "failed to retrieve pmd page size";
+		return -1;
+	}
+
+	gopts->nr_pages = (2 * pmd_pagesize) / gopts->page_size;
+
+	return 0;
+}
+
 static int prevent_hugepages(uffd_global_test_opts_t *gopts, const char **errmsg)
 {
 	/* This should be done before source area is populated */
@@ -2259,6 +2273,7 @@ struct uffd_test_case_ops uffd_move_test_case_ops = {
 };
 
 struct uffd_test_case_ops uffd_move_test_pmd_case_ops = {
+	.pre_alloc = set_pmd_pagesize_nr_pages,
 	.post_alloc = request_hugepages,
 };
 
Signature · 1 line
2.56.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] selftests/mm: build failure from calling removed read_pmd_pagesize() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] selftests/mm: build failure from calling removed read_pmd_pagesize()
--

commit baaff8c2bca44f86cbdf0bcae24ee9317be43974
Author: John J. Coleman <[email protected]>

selftests/mm: allocate nr_pages based on pmd size for uffd pmd_move tests

This commit adds a pre_alloc callback for the move-pmd uffd unit tests to size nr_pages to twice the PMD page size divided by the base page size.

tools/testing/selftests/mm/uffd-unit-tests.c static void uffd_mmap_changing_test(uffd_global_test_opts_t *gopts, uffd_test_ar · line 2489

Quoted 11 lines
tools/testing/selftests/mm/uffd-unit-tests.c +3 -0
index 9641f4730a355..9668dccc11599 100644
@@ -2484,6 +2484,20 @@ static void uffd_mmap_changing_test(uffd_global_test_opts_t *gopts, uffd_test_ar
 	uffd_test_pass();
 }
 
+static int set_pmd_pagesize_nr_pages(uffd_global_test_opts_t *gopts, const char **errmsg)
+{
+	uint64_t pmd_pagesize = read_pmd_pagesize();

[Severity: Medium]
Should this call pmd_psize() instead of read_pmd_pagesize()? Commit 3fe017016527 ("kselftest: mm: rename read_pmd_pagesize() to pmd_psize()") renamed this helper, so calling read_pmd_pagesize() breaks the build of uffd-unit-tests.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
Quoted 24 lines · On Sat, 14 Nov 2026 04:01:42 +0000 [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] selftests/mm: build failure from calling removed read_pmd_pagesize()
--

commit baaff8c2bca44f86cbdf0bcae24ee9317be43974
Author: John J. Coleman <[email protected]>

selftests/mm: allocate nr_pages based on pmd size for uffd pmd_move tests

This commit adds a pre_alloc callback for the move-pmd uffd unit tests to size nr_pages to twice the PMD page size divided by the base page size.

Quoted 8 lines
tools/testing/selftests/mm/uffd-unit-tests.c +0 -0
index 9641f4730a355..9668dccc11599 100644
@@ -2484,6 +2484,20 @@ static void uffd_mmap_changing_test(uffd_global_test_opts_t *gopts, uffd_test_ar
 	uffd_test_pass();
 }
 
Quoted 9 lines

tools/testing/selftests/mm/uffd-unit-tests.c static void uffd_mmap_changing_test(uffd_global_test_opts_t *gopts, uffd_test_ar · line 2489

Quoted 3 lines
tools/testing/selftests/mm/uffd-unit-tests.c +3 -0
+static int set_pmd_pagesize_nr_pages(uffd_global_test_opts_t *gopts, const char **errmsg)
+{
+	uint64_t pmd_pagesize = read_pmd_pagesize();

[Severity: Medium]
Should this call pmd_psize() instead of read_pmd_pagesize()? Commit 3fe017016527 ("kselftest: mm: rename read_pmd_pagesize() to pmd_psize()") renamed this helper, so calling read_pmd_pagesize() breaks the build of uffd-unit-tests.

yup, that's the sort of thing which I get to fix.

my reviewbot said:

: The pre_alloc hook fixes the sizing problem. But on 4K-page, : 2MiB-PMD systems it shrinks each test area from 32 MiB to 4 MiB. : After alignment, move-pmd usually exercises one PMD-sized move : instead of about fifteen. Please retain the existing minimum:
:
: gopts->nr_pages = MAX(UFFD_TEST_MEM_SIZE, 2 * pmd_pagesize) /
: gopts->page_size;
:
: On arm64 with 64K pages, source initialization touches 1 GiB. : The test also does not verify a huge PMD mapping: MADV_HUGEPAGE is : advisory, and UFFDIO_MOVE can use its PTE path. That coverage : limitation predates this patch.

So I came up with the below addition. John, can you please check it?

From: Andrew Morton <[email protected]>
Subject: selftests/mm: retain move-pmd coverage and verify huge PMDs
Date: Fri, 13 Nov 2026 20:35:51 -0800

The move-pmd tests need room for a PMD-aligned range. John J. Coleman's patch [1] provides two PMD spans, but replaces the 32 MiB default with 4 MiB on 4K-page, 2MiB-PMD systems. With unaligned mmap addresses, that reduces the test from about fifteen PMD-sized moves to one. Retain 32 MiB as the minimum.

MADV_HUGEPAGE is advisory. If the source is PTE-mapped, a PMD-sized UFFDIO_MOVE can succeed through move_pages_ptes() without exercising the huge-PMD path. Collapse the first aligned source PMD and check its mapping before either PMD test. Report a skip when a huge PMD cannot be obtained, rather than reporting a pass for a PTE-only run.

Fixes a2bf6a9ca805 ("selftests/mm: add UFFDIO_MOVE ioctl test")

---
This is a fixup on top of John J. Coleman's patch [1].

My LLM-driven live-kernel runtime testbot said:

Result: PASS. On a two-node x86-64 7.4.0-rc1 guest, all three move-pmd selftests passed with and without the patch. A kprobe recorded 3 entries into move_pages_huge_pmd() without the patch and 17 with it, confirming the additional huge-PMD path coverage.

A targeted allocation failure made MADV_COLLAPSE return ENOMEM; the patched test skipped as expected, while its no-fault control passed. An independent UFFDIO_MOVE partial-progress and cleanup probe passed. No test failure was observed.

 tools/testing/selftests/mm/uffd-unit-tests.c |   25 ++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
tools/testing/selftests/mm/uffd-unit-tests.c renamed from tools/testing/selftests/mm/uffd-unit-tests.c~selftests-mm-retain-move-pmd-coverage-and-verify-huge-pmds +24 -1
@@ -2124,8 +2124,28 @@ static void uffd_move_test(uffd_global_t
 	uffd_move_test_common(gopts, targs, gopts->page_size, uffd_move_handle_fault);
 }
 
+/*
+ * A PMD-sized UFFDIO_MOVE can also move PTEs. Make sure these tests
+ * start with a huge PMD, rather than passing through the PTE path.
+ */
+static bool uffd_move_pmd_ready(uffd_global_test_opts_t *gopts)
+{
+	unsigned long pmd_size = pmd_psize();
+	char *src = ALIGN_UP(gopts->area_src, pmd_size);
+
+	if (madvise(src, pmd_size, MADV_COLLAPSE) ||
+	    !check_huge_anon(src, pmd_size, 1, pmd_size)) {
+		uffd_test_skip("could not obtain a huge PMD for the source");
+		return false;
+	}
+
+	return true;
+}
+
 static void uffd_move_pmd_test(uffd_global_test_opts_t *gopts, uffd_test_args_t *targs)
 {
+	if (!uffd_move_pmd_ready(gopts))
+		return;
 	if (madvise(gopts->area_dst, gopts->nr_pages * gopts->page_size, MADV_HUGEPAGE))
 		err("madvise(MADV_HUGEPAGE) failure");
 	uffd_move_test_common(gopts, targs, pmd_psize(),
@@ -2225,6 +2245,8 @@ static void uffd_move_pmd_huge_zeropage_
 
 static void uffd_move_pmd_split_test(uffd_global_test_opts_t *gopts, uffd_test_args_t *targs)
 {
+	if (!uffd_move_pmd_ready(gopts))
+		return;
 	if (madvise(gopts->area_dst, gopts->nr_pages * gopts->page_size, MADV_NOHUGEPAGE))
 		err("madvise(MADV_NOHUGEPAGE) failure");
 	uffd_move_test_common(gopts, targs, pmd_psize(),
@@ -2493,7 +2515,8 @@ static int set_pmd_pagesize_nr_pages(uff
 		return -1;
 	}
 
-	gopts->nr_pages = (2 * pmd_pagesize) / gopts->page_size;
+	gopts->nr_pages = MAX(UFFD_TEST_MEM_SIZE, 2 * pmd_pagesize) /
+			  gopts->page_size;
 
 	return 0;
 }

_

#3 lore raw

Hi Andrew, thank you for checking this! And me-- for missing that MAX() input especially. I reviewed your patch and ran a test with the changes on my own systems as well, and it works great for me.

Tested-by on v1 1/1
#4 lore raw
In reply to John J. Coleman

Hi John,

Quoted 15 lines · John J. Coleman

When executing the uffd-unit-test, the "move-pmd on anon" test will fail on aarch64+64k with a segmentation fault because the initial allocated memory region is too small. This only happens with the move-pmd* tests because the architecture base page size is still utilized even though the memory region is specific to the size indicated by the pmd.

Patch 9f5c2aec3388 ("selftests/mm: fix uffd move-pmd test crash on 64K base page systems") implements an initial fix to the segmentation fault by skipping the test if the allocated memory region is not large enough for the number of pages x page size requested. Since this will skip over the aarch64+64k pmd tests, this patch is intended to resolve the problem with 64k pages by pre-allocating the memory region with 2x the pmd page size (for area_src and area_dst) for only the move_pmd tests by using the uffd_test_case_ops pre_alloc hook.

Quoted 5 lines
Fixes a2bf6a9ca805 ("selftests/mm: add UFFDIO_MOVE ioctl test")

Thanks for chasing it. I've tested mm-new (which already contains this patch) on my 64k-page-size box.

Before this patch:

ok 14 # SKIP move-pmd on anon (chunk size does not fit in the test area) ok 15 # SKIP move-pmd-huge-zeropage on anon (not enough pages for one PMD-sized move) ok 16 # SKIP move-pmd-split on anon (chunk size does not fit in the test area)

After this patch:

ok 14 move-pmd on anon
ok 15 move-pmd-huge-zeropage on anon
ok 16 move-pmd-split on anon

So,

Thanks,
Zenghui