selftests/mm: assorted fixes for hmm-tests
v3 cover selftests/mm: assorted fixes for hmm-tests
Hi all,
This series fixes a few issues in hmm-tests that show up when page-size and huge-page configuration differ from the hardcoded assumptions the tests were written for (PMD/THP sizing, default hugepage size, and related cases).
It also includes a fix to exclusive_cow: the test ignored the return value of fork(), so both parent and child ran the same teardown path.
Changes in v3:
- Rebased onto current mm-new.
- Patch 1: reworked the compound testcase check in response to the Shashiko review. Instead of an ASSERT_TRUE() that ORs the PMD and PUD cases on every page (which loses the expected/observed values on failure and would let a fragmented PMD+PUD mapping pass), determine the mapping level once from the first page and then enforce that every page matches it exactly with ASSERT_EQ().
- Patch 2: Unchanged from v2.
- Patch 3: No functional change from v2.
v1: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
Please let us know if you have any comments.
Thanks,
Aboorva
Aboorva Devarajan (1):
selftests/mm: fix exclusive_cow test fork() handling
Sayali Patil (2):
selftests/mm: allow PUD-level entries in compound testcase of hmm
tests
selftests/mm: remove hardcoded THP sizing assumptions in hmm tests
tools/testing/selftests/mm/hmm-tests.c | 125 +++++++++++++++++++------ 1 file changed, 95 insertions(+), 30 deletions(-)
base-commit: 801c5efa788952095586d076fed1ea4902facff8 --
2.54.0
PATCH v3 1/3 selftests/mm: allow PUD-level entries in compound testcase of hmm tests
From: Sayali Patil <[email protected]>
The HMM compound testcase currently assumes only PMD-level mappings and fails on systems where default_hugepagesz=1G is set, because the region is then reported by the device at PUD level.
Determine the mapping level (PMD or PUD) the device reports for the first page of the range and require every page to match that level exactly via ASSERT_EQ(). This accepts PUD-level mappings while preserving the expected/observed protection values printed on failure, and rejects a fragmented mapping that mixes PMD- and PUD-level entries within the same range (which a per-page OR check would have let pass).
--- tools/testing/selftests/mm/hmm-tests.c | 32 +++++++++++++++++++------- 1 file changed, 24 insertions(+), 8 deletions(-)
tools/testing/selftests/mm/hmm-tests.c +24 -8
@@ -1602,8 +1602,8 @@ TEST_F(hmm2, snapshot) }/* -* Test the hmm_range_fault() HMM_PFN_PMD flag for large pages that -* should be mapped by a large page table entry. +* Test the hmm_range_fault() handling of large pages (PMD or PUD) +* that should be mapped by a large page table entry. */ TEST_F(hmm, compound) {
@@ -1613,6 +1613,7 @@ TEST_F(hmm, compound)unsigned long default_hsize =default_huge_page_size ();int *ptr;unsigned char *m; +unsigned char prot;int ret;unsigned long i;
@@ -1648,11 +1649,20 @@ TEST_F(hmm, compound)ASSERT_EQ (ret,0 );ASSERT_EQ (buffer->cpages, npages); -/* Check what the device saw. */ +/* +* Check what the device saw. The region is backed by a single huge +* page that the device reports either at PMD or at PUD level depending +* on the configured default hugepage size. Determine that level from +* the first page and require every page in the range to match it +* exactly, so that a fragmented mapping mixing levels (or a missing +* large-page bit) is still caught and reported with its actual value. +*/ m = buffer->mirror; + prot =HMM_DMIRROR_PROT_WRITE | + ((m[0 ] &HMM_DMIRROR_PROT_PUD ) ?HMM_DMIRROR_PROT_PUD : +HMM_DMIRROR_PROT_PMD );for (i =0 ; i < npages; ++i) -ASSERT_EQ (m[i],HMM_DMIRROR_PROT_WRITE | -HMM_DMIRROR_PROT_PMD ); +ASSERT_EQ (m[i], prot);/* Make the region read-only. */ ret =mprotect (buffer->ptr, size,PROT_READ );
@@ -1663,11 +1673,17 @@ TEST_F(hmm, compound)ASSERT_EQ (ret,0 );ASSERT_EQ (buffer->cpages, npages); -/* Check what the device saw. */ +/* +* Check what the device saw after mprotect(PROT_READ). Same +* approach as above: determine the mapping level from the first +* page and require every page to match it exactly. +*/ m = buffer->mirror; + prot =HMM_DMIRROR_PROT_READ | + ((m[0 ] &HMM_DMIRROR_PROT_PUD ) ?HMM_DMIRROR_PROT_PUD : +HMM_DMIRROR_PROT_PMD );for (i =0 ; i < npages; ++i) -ASSERT_EQ (m[i],HMM_DMIRROR_PROT_READ | -HMM_DMIRROR_PROT_PMD ); +ASSERT_EQ (m[i], prot);munmap (buffer->ptr, buffer->size); buffer->ptr =NULL ;
base-commit: 801c5efa788952095586d076fed1ea4902facff8
Signature · 1 line
2.54.0
Re: [PATCH v3 1/3] selftests/mm: allow PUD-level entries in compound testcase of hmm tests
Quoted 6 lines · Aboorva Devarajan
From: Sayali Patil <[email protected]>
The HMM compound testcase currently assumes only PMD-level mappings and fails on systems where default_hugepagesz=1G is set, because the region is then reported by the device at PUD level.
Quoted 7 lines
Determine the mapping level (PMD or PUD) the device reports for the first page of the range and require every page to match that level exactly via
ASSERT_EQ(). This accepts PUD-level mappings while preserving the expected/observed protection values printed on failure, and rejects a fragmented mapping that mixes PMD- and PUD-level entries within the same range (which a per-page OR check would have let pass).
PUD is not supported with zone device private folios, the fix should be really around default_huge_page_size(), using the default pmd size.
Balbir
Re: [PATCH v3 1/3] selftests/mm: allow PUD-level entries in compound testcase of hmm tests
Quoted 15 lines · Balbir Singh
Quoted 6 lines · Aboorva Devarajan
From: Sayali Patil <[email protected]>
The HMM compound testcase currently assumes only PMD-level mappings and fails on systems where default_hugepagesz=1G is set, because the region is then reported by the device at PUD level.
Quoted 7 lines
Determine the mapping level (PMD or PUD) the device reports for the first page of the range and require every page to match that level exactly via
ASSERT_EQ(). This accepts PUD-level mappings while preserving the expected/observed protection values printed on failure, and rejects a fragmented mapping that mixes PMD- and PUD-level entries within the same range (which a per-page OR check would have let pass).
Quoted 4 lines
PUD is not supported with zone device private folios, the fix should be really around
default_huge_page_size(), using the default pmd size.Balbir
Thanks Balbir for the review.
IIUC, PUD not-supported restriction applies to the migration path (device-private folios top out at PMD), which the migrate_* tests cover but compound is purely a snapshot of the CPU page tables, where a 1G hugetlb page can shows up at PUD. I guess HMM_DMIRROR_PROT_PUD was in fact added in e478425bec93 (the same commit this patch Fixes:) to express PUD-sized snapshot entries, so handling it here is using the bit for its original purpose. I verified the v3 patch passes on a default_hugepagesz=1G boot (m[i] == WRITE|PUD), and fails without it.
But if you'd still prefer to keep this test-case strictly PMD-only, please let us know.
Regards,
Aboorva
PATCH v3 2/3 selftests/mm: remove hardcoded THP sizing assumptions in hmm tests
In reply to Aboorva DevarajanFrom: Sayali Patil <[email protected]>
migrate_partial_unmap_fault() and migrate_remap_fault() use hardcoded offsets based on a 2MB PMD size. Similarly, benchmark_thp_migration() assumes a fixed 2MB THP size when generating test buffer sizes.
Derive offsets and test sizes from the runtime PMD page size returned by read_pmd_pagesize(). If unavailable, fall back to TWOMEG. This allows the tests to adapt correctly on systems where PMD-sized THP differs from 2MB. Also replace the fixed 1MB unmap size with a PMD-relative value derived from the runtime PMD size.
On systems with larger PMD sizes, computed test buffer sizes can exceed INT_MAX. Skip such test cases to avoid overflow.
--- tools/testing/selftests/mm/hmm-tests.c | 62 ++++++++++++++++++-------- 1 file changed, 43 insertions(+), 19 deletions(-)
tools/testing/selftests/mm/hmm-tests.c +43 -19
@@ -22,6 +22,7 @@#include <strings.h> #include <time.h> #include <pthread.h> +#include <limits.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/mman.h>
@@ -2380,12 +2381,21 @@ TEST_F(hmm, migrate_partial_unmap_fault)struct hmm_buffer *buffer;unsigned long npages;unsigned long size =read_pmd_pagesize (); +unsigned long unmap_size; +unsigned long offsets[3 ];unsigned long i;void *old_ptr;void *map;int *ptr;int ret, j, use_thp; -int offsets[] = {0 ,512 *ONEKB ,ONEMEG }; + +if (!size) + size =TWOMEG ; + + unmap_size = size /2 ; + offsets[0 ] =0 ; + offsets[1 ] = size /4 ; + offsets[2 ] = size /2 ;for (use_thp =0 ; use_thp <2 ; ++use_thp) {for (j =0 ; j <ARRAY_SIZE (offsets); ++j) {
@@ -2427,12 +2437,12 @@ TEST_F(hmm, migrate_partial_unmap_fault)for (i =0 , ptr = buffer->mirror; i < size /sizeof (*ptr); ++i)ASSERT_EQ (ptr[i], i); -munmap (buffer->ptr + offsets[j],ONEMEG ); +munmap (buffer->ptr + offsets[j], unmap_size);/* Fault pages back to system memory and check them. */ for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i)if (i *sizeof (int ) < offsets[j] || - i *sizeof (int ) >= offsets[j] +ONEMEG ) + i *sizeof (int ) >= offsets[j] + unmap_size)ASSERT_EQ (ptr[i], i); buffer->ptr = old_ptr;
@@ -2446,12 +2456,19 @@ TEST_F(hmm, migrate_remap_fault)struct hmm_buffer *buffer;unsigned long npages;unsigned long size =read_pmd_pagesize (); +unsigned long offsets[3 ];unsigned long i;void *old_ptr, *new_ptr =NULL ;void *map;int *ptr;int ret, j, use_thp, dont_unmap, before; -int offsets[] = {0 ,512 *ONEKB ,ONEMEG }; + +if (!size) + size =TWOMEG ; + + offsets[0 ] =0 ; + offsets[1 ] = size /4 ; + offsets[2 ] = size /2 ;for (before =0 ; before <2 ; ++before) {for (dont_unmap =0 ; dont_unmap <2 ; ++dont_unmap) {
@@ -2854,38 +2871,45 @@ static inline int run_migration_benchmark(int fd, int use_thp, size_t buffer_siz TEST_F_TIMEOUT(hmm, benchmark_thp_migration, 120) {struct benchmark_results thp_results, regular_results; -size_t thp_size =2 *1024 *1024 ;/* 2MB - typical THP size */ +size_t thp_size =read_pmd_pagesize ();int iterations =5 ; +if (!thp_size) + thp_size =TWOMEG ; +printf ("\nHMM THP Migration Benchmark\n" );printf ("---------------------------\n" );printf ("System page size: %ld bytes\n" ,sysconf (_SC_PAGESIZE));/* Test different buffer sizes */ size_t test_sizes[] = { - thp_size /4 ,/* 512KB - smaller than THP */ - thp_size /2 ,/* 1MB - half THP */ - thp_size,/* 2MB - single THP */ - thp_size *2 ,/* 4MB - two THPs */ - thp_size *4 ,/* 8MB - four THPs */ - thp_size *8 ,/* 16MB - eight THPs */ - thp_size *128 ,/* 256MB - one twenty eight THPs */ + thp_size /4 ,/* quarter THP */ + thp_size /2 ,/* half THP */ + thp_size,/* single THP */ + thp_size *2 ,/* two THPs */ + thp_size *4 ,/* four THPs */ + thp_size *8 ,/* eight THPs */ + thp_size *128 ,/* one twenty eight THPs */ };static const char *const test_names[] = { -"Small Buffer (512KB)" , -"Half THP Size (1MB)" , -"Single THP Size (2MB)" , -"Two THP Size (4MB)" , -"Four THP Size (8MB)" , -"Eight THP Size (16MB)" , -"One twenty eight THP Size (256MB)" +"Small Buffer" , +"Half THP Size" , +"Single THP Size" , +"Two THP Size" , +"Four THP Size" , +"Eight THP Size" , +"One twenty eight THP Size" };int num_tests =ARRAY_SIZE (test_sizes);/* Run all tests */ for (int i =0 ; i < num_tests; i++) { +/* Skip test sizes exceeding INT_MAX to avoid overflow */ +if (test_sizes[i] >INT_MAX ) +break ; +/* Test with THP */ ASSERT_EQ (run_migration_benchmark (self->fd,1 , test_sizes[i], iterations, &thp_results),0 );
Signature · 1 line
2.54.0
Re: [PATCH v3 2/3] selftests/mm: remove hardcoded THP sizing assumptions in hmm tests
Quoted 13 lines · Aboorva Devarajan
From: Sayali Patil <[email protected]>
migrate_partial_unmap_fault()andmigrate_remap_fault()use hardcoded offsets based on a 2MB PMD size. Similarly,benchmark_thp_migration()assumes a fixed 2MB THP size when generating test buffer sizes.Derive offsets and test sizes from the runtime PMD page size returned by
read_pmd_pagesize(). If unavailable, fall back to TWOMEG. This allows the tests to adapt correctly on systems where PMD-sized THP differs from 2MB. Also replace the fixed 1MB unmap size with a PMD-relative value derived from the runtime PMD size.
Quoted 8 lines
On systems with larger PMD sizes, computed test buffer sizes can exceed
INT_MAX. Skip such test cases to avoid overflow.Fixes 24c2c5b8ffbd ("selftests/mm/hmm-tests: partial unmap, mremap and anon_write tests")Fixes 271a7b2e3c13 ("selftests/mm/hmm-tests: new throughput tests including THP")Signed-off-by Sayali Patil <[email protected]>Signed-off-by Aboorva Devarajan <[email protected]>---
Thanks,
PATCH v3 3/3 selftests/mm: fix exclusive_cow test fork() handling
In reply to Aboorva DevarajanThe test ignores the return value of fork(), so both the parent and the (newly created) child run the COW verification loops and then call hmm_buffer_free() before returning into the kselftest harness, which _exit()s each side. This duplicated teardown sequence has been observed to manifest as a SIGSEGV in the test child, e.g.:
hmm-tests[360141]: segfault (11) at 0 nip 10006964 lr 1000ac3c code 1 in hmm-tests[6964,10000000+30000]
Fix this by adopting the same fork()-then-wait pattern already used by the nearby anon_write_child / anon_write_child_shared tests in this file: the child performs the COW verification and then _exit(0)s so it does not run the test teardown, while the parent independently verifies COW, waits for the child, and only then frees the buffer.
--- tools/testing/selftests/mm/hmm-tests.c | 31 +++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/hmm-tests.c +28 -3
@@ -1884,6 +1884,8 @@ TEST_F(hmm, exclusive_cow)unsigned long i;int *ptr;int ret; +pid_t pid; +int status; npages =ALIGN (HMM_BUFFER_SIZE , self->page_size) >> self->page_shift;ASSERT_NE (npages,0 );
@@ -1912,14 +1914,37 @@ TEST_F(hmm, exclusive_cow)ASSERT_EQ (ret,0 );ASSERT_EQ (buffer->cpages, npages); -fork (); + pid =fork (); +if (pid ==-1 ) +ASSERT_EQ (pid,0 ); -/* Fault pages back to system memory and check them. */ +if (pid ==0 ) { +/* +* Child verifies COW independently, then _exit(0)s so it does +* not run the test teardown. A failed ASSERT_* here makes the +* harness abort() the child, so the parent sees +* !WIFEXITED(status) below and fails in turn. +*/ +for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) +ASSERT_EQ (ptr[i]++, i); + +for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) +ASSERT_EQ (ptr[i], i +1 ); + +_exit (0 ); + } + +/* Parent: also increment to verify COW works for both processes. */ for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i)ASSERT_EQ (ptr[i]++, i);for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) -ASSERT_EQ (ptr[i], i+1 ); +ASSERT_EQ (ptr[i], i +1 ); + +/* Parent: wait for child and then free the buffer. */ +ASSERT_EQ (waitpid (pid, &status,0 ), pid); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 );hmm_buffer_free (buffer); }
Signature · 1 line
2.54.0
Re: [PATCH v3 3/3] selftests/mm: fix exclusive_cow test fork() handling
Quoted 9 lines · Aboorva Devarajan
The test ignores the return value of
fork(), so both the parent and the (newly created) child run the COW verification loops and then callhmm_buffer_free()before returning into the kselftest harness, which _exit()s each side. This duplicated teardown sequence has been observed to manifest as a SIGSEGV in the test child, e.g.:hmm-tests[360141]: segfault (11) at 0 nip 10006964 lr
1000ac3ccode 1 in hmm-tests[6964,10000000+30000]
Thanks for catching this!
Quoted 56 lines
Fix this by adopting the same fork()-then-wait pattern already used by the nearby
anon_write_child/anon_write_child_sharedtests in this file: the child performs the COW verification and then _exit(0)s so it does not run the test teardown, while the parent independently verifies COW, waits for the child, and only then frees the buffer.Fixes b659baea75469 ("mm: selftests for exclusive device memory")Signed-off-by Aboorva Devarajan <[email protected]>--- tools/testing/selftests/mm/hmm-tests.c | 31 +++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-)tools/testing/selftests/mm/hmm-tests.c +22
-2@@ -1884,6 +1884,8 @@ TEST_F(hmm, exclusive_cow)unsigned long i;int *ptr;int ret; +pid_t pid; +int status; npages =ALIGN (HMM_BUFFER_SIZE , self->page_size) >> self->page_shift;ASSERT_NE (npages,0 );@@ -1912,14 +1914,37 @@ TEST_F(hmm, exclusive_cow)ASSERT_EQ (ret,0 );ASSERT_EQ (buffer->cpages, npages); -fork (); + pid =fork (); +if (pid ==-1 ) +ASSERT_EQ (pid,0 ); -/* Fault pages back to system memory and check them. */ +if (pid ==0 ) { +/* +* Child verifies COW independently, then _exit(0)s so it does +* not run the test teardown. A failed ASSERT_* here makes the +* harness abort() the child, so the parent sees +* !WIFEXITED(status) below and fails in turn. +*/ +for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) +ASSERT_EQ (ptr[i]++, i); + +for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) +ASSERT_EQ (ptr[i], i +1 ); + +_exit (0 ); + } + +/* Parent: also increment to verify COW works for both processes. */ for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i)ASSERT_EQ (ptr[i]++, i);for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i)
tools/testing/selftests/mm/hmm-tests.c TEST_F(hmm, exclusive_cow) · line 1950
Quoted 10 lines
tools/testing/selftests/mm/hmm-tests.c +6
-1-ASSERT_EQ (ptr[i], i+1 ); +ASSERT_EQ (ptr[i], i +1 ); + +/* Parent: wait for child and then free the buffer. */ +ASSERT_EQ (waitpid (pid, &status,0 ), pid); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 );hmm_buffer_free (buffer); }