On Wed, May 27, 2026 at 09:38:25AM +0800, Hongfu Li wrote:
> Add missing checks against mmap() return value, replace (void *)-1
> with MAP_FAILED for better readability and consistency.
> 
> Signed-off-by: Hongfu Li <[email protected]>
> ---
>  tools/testing/selftests/mm/pkey-powerpc.h        |  2 +-
>  .../testing/selftests/mm/pkey_sighandler_tests.c |  2 ++
>  tools/testing/selftests/mm/protection_keys.c     | 16 ++++++++++------
>  3 files changed, 13 insertions(+), 7 deletions(-)
> 
> diff --git a/tools/testing/selftests/mm/pkey-powerpc.h 
> b/tools/testing/selftests/mm/pkey-powerpc.h
> index 17bf2d1b0192..f482b1abcf55 100644
> --- a/tools/testing/selftests/mm/pkey-powerpc.h
> +++ b/tools/testing/selftests/mm/pkey-powerpc.h
> @@ -126,7 +126,7 @@ static inline void 
> *malloc_pkey_with_mprotect_subpage(long size, int prot, u16 p
>                       size, prot, pkey);
>       pkey_assert(pkey < NR_PKEYS);
>       ptr = mmap(NULL, size, prot, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
> -     pkey_assert(ptr != (void *)-1);
> +     pkey_assert(ptr != (void *)MAP_FAILED);
>  
>       ret = syscall(__NR_subpage_prot, ptr, size, NULL);
>       if (ret) {
> diff --git a/tools/testing/selftests/mm/pkey_sighandler_tests.c 
> b/tools/testing/selftests/mm/pkey_sighandler_tests.c
> index 231dfb079075..475aa3e3208a 100644
> --- a/tools/testing/selftests/mm/pkey_sighandler_tests.c
> +++ b/tools/testing/selftests/mm/pkey_sighandler_tests.c
> @@ -317,6 +317,7 @@ static void 
> test_sigsegv_handler_with_different_pkey_for_stack(void)
>       /* Set up alternate signal stack that will use the default MPK */
>       sigstack.ss_sp = mmap(0, STACK_SIZE, PROT_READ | PROT_WRITE,
>                             MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
> +     pkey_assert(sigstack.ss_sp != MAP_FAILED);
>       sigstack.ss_flags = 0;
>       sigstack.ss_size = STACK_SIZE;
>  
> @@ -486,6 +487,7 @@ static void test_pkru_sigreturn(void)
>       /* Set up alternate signal stack that will use the default MPK */
>       sigstack.ss_sp = mmap(0, STACK_SIZE, PROT_READ | PROT_WRITE,
>                             MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
> +     pkey_assert(sigstack.ss_sp != MAP_FAILED);
>       sigstack.ss_flags = 0;
>       sigstack.ss_size = STACK_SIZE;
>  
> diff --git a/tools/testing/selftests/mm/protection_keys.c 
> b/tools/testing/selftests/mm/protection_keys.c
> index 3c0cbf8f3eaa..ae474e65f3aa 100644
> --- a/tools/testing/selftests/mm/protection_keys.c
> +++ b/tools/testing/selftests/mm/protection_keys.c
> @@ -585,7 +585,7 @@ static void *malloc_pkey_with_mprotect(long size, int 
> prot, u16 pkey)
>                       size, prot, pkey);
>       pkey_assert(pkey < NR_PKEYS);
>       ptr = mmap(NULL, size, prot, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
> -     pkey_assert(ptr != (void *)-1);
> +     pkey_assert(ptr != MAP_FAILED);
>       ret = mprotect_pkey((void *)ptr, PAGE_SIZE, prot, pkey);
>       pkey_assert(!ret);
>       record_pkey_malloc(ptr, size, prot);
> @@ -608,7 +608,7 @@ static void *malloc_pkey_anon_huge(long size, int prot, 
> u16 pkey)
>        */
>       size = ALIGN_UP(size, HPAGE_SIZE * 2);
>       ptr = mmap(NULL, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
> -     pkey_assert(ptr != (void *)-1);
> +     pkey_assert(ptr != MAP_FAILED);
>       record_pkey_malloc(ptr, size, prot);
>       mprotect_pkey(ptr, size, prot, pkey);
>  
> @@ -688,7 +688,7 @@ static void *malloc_pkey_hugetlb(long size, int prot, u16 
> pkey)
>       size = ALIGN_UP(size, HPAGE_SIZE * 2);
>       pkey_assert(pkey < NR_PKEYS);
>       ptr = mmap(NULL, size, PROT_NONE, flags, -1, 0);
> -     pkey_assert(ptr != (void *)-1);
> +     pkey_assert(ptr != MAP_FAILED);
>       mprotect_pkey(ptr, size, prot, pkey);
>  
>       record_pkey_malloc(ptr, size, prot);
> @@ -717,7 +717,7 @@ static void *malloc_pkey(long size, int prot, u16 pkey)
>               pkey_assert(malloc_type < nr_malloc_types);
>  
>               ret = pkey_malloc[malloc_type](size, prot, pkey);
> -             pkey_assert(ret != (void *)-1);
> +             pkey_assert(ret != MAP_FAILED);
>  
>               malloc_type++;
>               if (malloc_type >= nr_malloc_types)
> @@ -1135,6 +1135,7 @@ static void arch_force_pkey_reg_init(void)
>        * doing the XSAVE size enumeration dance.
>        */
>       buf = mmap(NULL, 1*MB, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, 
> -1, 0);
> +     pkey_assert(buf != MAP_FAILED);
>  
>       /* These __builtins require compiling with -mxsave */
>  
> @@ -1693,7 +1694,10 @@ int main(void)
>               printf("running PKEY tests for unsupported CPU/OS\n");
>  
>               ptr  = mmap(NULL, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, 
> -1, 0);
> -             assert(ptr != (void *)-1);
> +             if (ptr == MAP_FAILED) {
> +                     perror("mmap");
> +                     return EXIT_FAILURE;
> +             }

Just stick to pkey_assert() everywhere, it already prints errno and exits.

>               test_mprotect_pkey_on_unsupported_cpu(ptr, 1);
>               exit(0);
>       }
> @@ -1706,5 +1710,5 @@ int main(void)
>               run_tests_once();
>  
>       printf("done (all tests OK)\n");
> -     return 0;
> +     return EXIT_SUCCESS;

Completely unrelated and not needed.

And you may want to base your work on mm-unstable to get up to date version
of protection_keys tests.

>  }
> -- 
> 2.50.1
> 

-- 
Sincerely yours,
Mike.

Reply via email to