Hi Mike!

On 4/29/26 2:12 AM, Mike Rapoport wrote:
> From: "Mike Rapoport (Microsoft)" <[email protected]>
> 
> Convert uffd-unit-tests to use kselftest framework for reporting and
> tracking successful and failing runs.
> 
> Reviewed by: Donet Tom <[email protected]>
> 
> Reviewed-by: Mark Brown <[email protected]>
> Signed-off-by: Mike Rapoport (Microsoft) <[email protected]>
> ---
>  tools/testing/selftests/mm/uffd-unit-tests.c | 79 +++++++++++---------
>  1 file changed, 44 insertions(+), 35 deletions(-)
> 
> diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c 
> b/tools/testing/selftests/mm/uffd-unit-tests.c
> index 6f5e404a446c..1a33db281f8a 100644
> --- a/tools/testing/selftests/mm/uffd-unit-tests.c
> +++ b/tools/testing/selftests/mm/uffd-unit-tests.c
> @@ -86,39 +86,24 @@ typedef struct {
>       uffd_test_case_ops_t *test_case_ops;
>  } uffd_test_case_t;
>  
> -static void uffd_test_report(void)
> -{
> -     printf("Userfaults unit tests: pass=%u, skip=%u, fail=%u (total=%u)\n",
> -            ksft_get_pass_cnt(),
> -            ksft_get_xskip_cnt(),
> -            ksft_get_fail_cnt(),
> -            ksft_test_num());
> -}
> +static char current_test[256];
>  
>  static void uffd_test_pass(void)
>  {
> -     printf("done\n");
> -     ksft_inc_pass_cnt();
> +     ksft_test_result_pass("%s\n", current_test);
>  }
>  
>  #define  uffd_test_start(...)  do {          \
> -             printf("Testing ");             \
> -             printf(__VA_ARGS__);            \
> -             printf("... ");                 \
> -             fflush(stdout);                 \
> +             snprintf(current_test, sizeof(current_test), __VA_ARGS__); \
>       } while (0)
>  
>  #define  uffd_test_fail(...)  do {           \
> -             printf("failed [reason: ");     \
> -             printf(__VA_ARGS__);            \
> -             printf("]\n");                  \
> -             ksft_inc_fail_cnt();            \
> +             ksft_test_result_fail("%s\n", current_test);    \
>       } while (0)
>  
>  static void uffd_test_skip(const char *message)
>  {
> -     printf("skipped [reason: %s]\n", message);
> -     ksft_inc_xskip_cnt();
> +     ksft_test_result_skip("%s (%s)\n", current_test, message);
>  }
>  
>  /*
> @@ -1701,6 +1686,26 @@ static void usage(const char *prog)
>       exit(KSFT_FAIL);
>  }
>  
> +static int uffd_count_tests(int n_tests, int n_mems, const char *test_filter)
> +{
> +     uffd_test_case_t *test;
> +     int i, j, count = 0;
> +
> +     if (!test_filter)
> +             count += 2;     /* test_uffd_api(false) + test_uffd_api(true) */
> +
> +     for (i = 0; i < n_tests; i++) {
> +             test = &uffd_tests[i];
> +             if (test_filter && !strstr(test->name, test_filter))
> +                     continue;
> +             for (j = 0; j < n_mems; j++)
> +                     if (test->mem_targets & mem_types[j].mem_flag)
> +                             count++;
> +     }
> +
> +     return count;
> +}
> +
>  int main(int argc, char *argv[])
>  {
>       int n_tests = sizeof(uffd_tests) / sizeof(uffd_test_case_t);
> @@ -1730,24 +1735,31 @@ int main(int argc, char *argv[])
>               }
>       }
>  
> -     if (!test_filter && !list_only) {
> +     if (list_only) {
> +             for (i = 0; i < n_tests; i++) {
> +                     test = &uffd_tests[i];
> +                     if (test_filter && !strstr(test->name, test_filter))
> +                             continue;
> +                     printf("%s\n", test->name);
> +             }
> +             return KSFT_PASS;
> +     }
> +
> +     ksft_print_header();
> +     ksft_set_plan(uffd_count_tests(n_tests, n_mems, test_filter));
> +
> +     if (!test_filter) {
>               has_uffd = test_uffd_api(false);
>               has_uffd |= test_uffd_api(true);
>  
> -             if (!has_uffd) {
> -                     printf("Userfaultfd not supported or unprivileged, skip 
> all tests\n");
> -                     exit(KSFT_SKIP);
> -             }
> +             if (!has_uffd)
> +                     ksft_exit_skip("Userfaultfd not supported or 
> unprivileged\n");

If has_uffd is 0, two test outputs from test_uffd_api() are being
printed and the third output is from the ksft_exit_skip(). Since we have
declared the ksft plan before this, we are getting the following
diagnostic line:

# Planned tests != run tests (67 != 3)

If we remove this if (!has_uffd) check, still all the tests are
individually being skipped with a "feature missing" message and the
planned tests are being equal to the run tests. So do we really need
this check or can this be skipped?

>       }
>  
>       for (i = 0; i < n_tests; i++) {
>               test = &uffd_tests[i];
>               if (test_filter && !strstr(test->name, test_filter))
>                       continue;
> -             if (list_only) {
> -                     printf("%s\n", test->name);
> -                     continue;
> -             }
>               for (j = 0; j < n_mems; j++) {
>                       mem_type = &mem_types[j];
>  
> @@ -1794,10 +1806,7 @@ int main(int argc, char *argv[])
>               }
>       }
>  
> -     if (!list_only)
> -             uffd_test_report();
> -
> -     return ksft_get_fail_cnt() ? KSFT_FAIL : KSFT_PASS;
> +     ksft_finished();
>  }
>  
>  #else /* __NR_userfaultfd */
> @@ -1806,8 +1815,8 @@ int main(int argc, char *argv[])
>  
>  int main(void)
>  {
> -     printf("Skipping %s (missing __NR_userfaultfd)\n", __file__);
> -     return KSFT_SKIP;
> +     ksft_print_header();
> +     ksft_exit_skip("missing __NR_userfaultfd definition\n");
>  }
>  
>  #endif /* __NR_userfaultfd */


Reply via email to