| From: Li Wang <liwang@redhat.com> |
| Subject: mm/selftests: improve UFFD-WP feature detection in KSM test |
| Date: Sun, 22 Jun 2025 16:10:35 +0800 |
| |
| The current implementation of test_unmerge_uffd_wp() explicitly sets |
| `uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP` before calling |
| UFFDIO_API. This can cause the ioctl() call to fail with EINVAL on |
| kernels that do not support UFFD-WP, leading the test to fail |
| unnecessarily: |
| |
| # ------------------------------ |
| # running ./ksm_functional_tests |
| # ------------------------------ |
| # TAP version 13 |
| # 1..9 |
| # # [RUN] test_unmerge |
| # ok 1 Pages were unmerged |
| # # [RUN] test_unmerge_zero_pages |
| # ok 2 KSM zero pages were unmerged |
| # # [RUN] test_unmerge_discarded |
| # ok 3 Pages were unmerged |
| # # [RUN] test_unmerge_uffd_wp |
| # not ok 4 UFFDIO_API failed <----- |
| # # [RUN] test_prot_none |
| # ok 5 Pages were unmerged |
| # # [RUN] test_prctl |
| # ok 6 Setting/clearing PR_SET_MEMORY_MERGE works |
| # # [RUN] test_prctl_fork |
| # # No pages got merged |
| # # [RUN] test_prctl_fork_exec |
| # ok 7 PR_SET_MEMORY_MERGE value is inherited |
| # # [RUN] test_prctl_unmerge |
| # ok 8 Pages were unmerged |
| # Bail out! 1 out of 8 tests failed |
| # # Planned tests != run tests (9 != 8) |
| # # Totals: pass:7 fail:1 xfail:0 xpass:0 skip:0 error:0 |
| # [FAIL] |
| |
| This patch improves compatibility and error handling by: |
| |
| 1. Changes the feature check to first query supported features (features=0) |
| rather than specifically requesting WP support. |
| |
| 2. Gracefully skipping the test if: |
| - UFFDIO_API fails with EINVAL (feature not supported), or |
| - UFFD_FEATURE_PAGEFAULT_FLAG_WP is not advertised by the kernel. |
| |
| 3. Providing better diagnostics by distinguishing expected failures (e.g., |
| EINVAL) from unexpected ones and reporting them using strerror(). |
| |
| The updated logic makes the test more robust across different kernel versions |
| and configurations, while preserving existing behavior on systems that do |
| support UFFD-WP. |
| |
| Link: https://lkml.kernel.org/r/20250622081035.378164-1-liwang@redhat.com |
| Signed-off-by: Li Wang <liwang@redhat.com> |
| Cc: Aruna Ramakrishna <aruna.ramakrishna@oracle.com> |
| Cc: Bagas Sanjaya <bagasdotme@gmail.com> |
| Cc: Catalin Marinas <catalin.marinas@arm.com> |
| Cc: Dave Hansen <dave.hansen@linux.intel.com> |
| Cc: David Hildenbrand <david@redhat.com> |
| Cc: Joey Gouly <joey.gouly@arm.com> |
| Cc: Johannes Weiner <hannes@cmpxchg.org> |
| Cc: Keith Lucas <keith.lucas@oracle.com> |
| Cc: Ryan Roberts <ryan.roberts@arm.com> |
| Cc: Shuah Khan <shuah@kernel.org> |
| Signed-off-by: Andrew Morton <akpm@linux-foundation.org> |
| --- |
| |
| tools/testing/selftests/mm/ksm_functional_tests.c | 8 ++++++-- |
| 1 file changed, 6 insertions(+), 2 deletions(-) |
| |
| --- a/tools/testing/selftests/mm/ksm_functional_tests.c~mm-selftests-improve-uffd-wp-feature-detection-in-ksm-test |
| +++ a/tools/testing/selftests/mm/ksm_functional_tests.c |
| @@ -393,9 +393,13 @@ static void test_unmerge_uffd_wp(void) |
| |
| /* See if UFFD-WP is around. */ |
| uffdio_api.api = UFFD_API; |
| - uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP; |
| + uffdio_api.features = 0; |
| if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) { |
| - ksft_test_result_fail("UFFDIO_API failed\n"); |
| + if (errno == EINVAL) |
| + ksft_test_result_skip("UFFDIO_API not supported (EINVAL)\n"); |
| + else |
| + ksft_test_result_fail("UFFDIO_API failed: %s\n", strerror(errno)); |
| + |
| goto close_uffd; |
| } |
| if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP)) { |
| _ |