selftests/mm: fix userfaultfd tests with CONFIG_PTE_MARKER_UFFD_WP=n
Subsystems (4)
cover selftests/mm: fix userfaultfd tests with CONFIG_PTE_MARKER_UFFD_WP=n
From: Kalesh Singh <[email protected]>
With CONFIG_PTE_MARKER_UFFD_WP=n, userfaultfd write protection only works on anonymous memory. uffd-wp-mremap then fails its shmem and hugetlb cases, and uffd-unit-tests skips anonymous runs that would pass. Patch 1 skips the unsupported uffd-wp-mremap cases and patch 2 filters uffd-unit-tests' required features per memory type.
Tested on arm64 device: with CONFIG_PTE_MARKER_UFFD_WP=n, uffd-wp-mremap goes from 26 failures to none and uffd-unit-tests passes 78 tests instead of 70; with =y, the results are unchanged.
Kalesh Singh (2):
selftests/mm: uffd-wp-mremap: skip unsupported shmem and hugetlb cases selftests/mm: uffd-unit-tests: filter required features per memory
type
tools/testing/selftests/mm/uffd-unit-tests.c | 42 +++++++++++++++++--- tools/testing/selftests/mm/uffd-wp-mremap.c | 13 ++++-- 2 files changed, 46 insertions(+), 9 deletions(-)
base-commit: 4843f6873f7c4835e62f2c73ba0fd538e57a2f22
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH 1/2 selftests/mm: uffd-wp-mremap: skip unsupported shmem and hugetlb cases
From: Kalesh Singh <[email protected]>
uffd-wp-mremap checks only for UFFD_FEATURE_PAGEFAULT_FLAG_WP before it runs. With CONFIG_PTE_MARKER_UFFD_WP=n the kernel keeps that feature but supports write protection on anonymous memory only, so the shmem and hugetlb cases fail with EINVAL from UFFDIO_REGISTER instead of skipping. Skip them when UFFD_FEATURE_WP_HUGETLBFS_SHMEM is missing.
--- tools/testing/selftests/mm/uffd-wp-mremap.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
tools/testing/selftests/mm/uffd-wp-mremap.c +9 -4
@@ -18,15 +18,14 @@ static int nr_thpsizes;static size_t thpsizes[20 ];static int nr_hugetlbsizes;static unsigned long hugetlbsizes[10 ]; +static uint64_t uffd_features;static void check_uffd_wp_feature_supported (void ) { -uint64_t features =0 ; - -if (uffd_get_features (&features)) +if (uffd_get_features (&uffd_features))ksft_exit_skip ("failed to get available features (%d)\n" , errno); -if (!(features &UFFD_FEATURE_PAGEFAULT_FLAG_WP )) +if (!(uffd_features &UFFD_FEATURE_PAGEFAULT_FLAG_WP ))ksft_exit_skip ("uffd-wp feature not supported\n" ); }
@@ -180,6 +179,12 @@ static void test_one_folio(uffd_global_test_opts_t *gopts, size_t size, bool pri swapout ?"true" :"false" , hugetlb ? "true" : "false"); +if ((!private || hugetlb) && + !(uffd_features &UFFD_FEATURE_WP_HUGETLBFS_SHMEM )) { +ksft_test_result_skip ("uffd-wp on shmem and hugetlbfs is not supported\n" ); +return ; + } +/* Allocate a folio of required size and type. */ mem =alloc_one_folio (size, private, hugetlb);if (!mem) {
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH 2/2 selftests/mm: uffd-unit-tests: filter required features per memory type
In reply to [email protected]From: Kalesh Singh <[email protected]>
CONFIG_PTE_MARKER_UFFD_WP defaults to y but can be turned off, and userfaultfd write protection then works only on anonymous memory. The kernel still reports UFFD_FEATURE_PAGEFAULT_FLAG_WP but no longer UFFD_FEATURE_WP_HUGETLBFS_SHMEM.
uffd-unit-tests checks a test's whole feature mask for every memory type, so the tests that run on all memory types and require UFFD_FEATURE_WP_HUGETLBFS_SHMEM (register-ioctls, the wp-fork tests, rwp-wp-exclusive, sigbus-wp and events-wp) skip their anonymous runs too. Check and request only the features that apply to the memory type under test, and make minor-wp require UFFD_FEATURE_WP_HUGETLBFS_SHMEM, as it write-protects shmem and hugetlbfs.
--- tools/testing/selftests/mm/uffd-unit-tests.c | 42 +++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-)
tools/testing/selftests/mm/uffd-unit-tests.c +37 -5
@@ -176,15 +176,44 @@ static void test_uffd_api(bool use_dev) } -static bool uffd_feature_supported (uffd_test_case_t *test) +/* +* Filter test->uffd_feature_required down to the features needed for @mem_type. +* For example, UFFD_FEATURE_WP_HUGETLBFS_SHMEM (gated on +* CONFIG_PTE_MARKER_UFFD_WP) only applies to shmem and hugetlbfs, so anonymous +* subtests should still run when it is absent. +*/ +static uint64_t uffd_test_features (const uffd_test_case_t *test, +const mem_type_t *mem_type) +{ +const uint64_t shmem =UFFD_FEATURE_MISSING_SHMEM | +UFFD_FEATURE_MINOR_SHMEM ; +const uint64_t hugetlb =UFFD_FEATURE_MISSING_HUGETLBFS | +UFFD_FEATURE_MINOR_HUGETLBFS ; +uint64_t features = test->uffd_feature_required; + +switch (mem_type->mem_flag) { +case MEM_ANON : +return features & ~(shmem | hugetlb | +UFFD_FEATURE_WP_HUGETLBFS_SHMEM ); +case MEM_SHMEM : +case MEM_SHMEM_PRIVATE : +return features & ~hugetlb; +case MEM_HUGETLB : +case MEM_HUGETLB_PRIVATE : +return features & ~shmem; +default : +return features; + } +} + +static bool uffd_feature_supported (uint64_t required) {uint64_t features;if (uffd_get_features (&features))return false; -return (features & test->uffd_feature_required) == - test->uffd_feature_required; +return (features & required) == required; }static int pagemap_open (void )
@@ -2417,6 +2446,7 @@ uffd_test_case_t uffd_tests[] = { .uffd_feature_required =UFFD_FEATURE_MINOR_HUGETLBFS |UFFD_FEATURE_MINOR_SHMEM |UFFD_FEATURE_PAGEFAULT_FLAG_WP | + UFFD_FEATURE_WP_HUGETLBFS_SHMEM |/* * HACK: here we leveraged WP_UNPOPULATED to detect whether * minor mode supports wr-protect. There's no feature flag
@@ -2636,6 +2666,7 @@ int main(int argc, char *argv[])mem_type_t *mem_type;uffd_test_args_t args;const char *errmsg; +uint64_t features;int i, j, opt;while ((opt =getopt (argc, argv,"f:hl" )) !=-1 ) {
@@ -2711,11 +2742,12 @@ int main(int argc, char *argv[])/* Initialize test arguments */ args.mem_type = mem_type; -if (!uffd_feature_supported (test)) { + features =uffd_test_features (test, mem_type); +if (!uffd_feature_supported (features)) {uffd_test_skip ("feature missing" );continue ; } -if (uffd_test_ctx_init (&gopts, test->uffd_feature_required, &errmsg)) { +if (uffd_test_ctx_init (&gopts, features, &errmsg)) {uffd_test_skip (errmsg);continue ; }
Signature · 1 line
2.56.1.750.gf70dc682cf-goog