[PATCH v2 1/8] selftests/mm: Add -Wunreachable-code and fix warnings

Muhammad Usama Anjum posted 8 patches 2 months ago
There is a newer version of this series
[PATCH v2 1/8] selftests/mm: Add -Wunreachable-code and fix warnings
Posted by Muhammad Usama Anjum 2 months ago
Enable -Wunreachable-code flag to catch dead code and fix them.

1. Remove the dead code and write a comment instead:
hmm-tests.c:2033:3: warning: code will never be executed
[-Wunreachable-code]
                perror("Should not reach this\n");
                ^~~~~~

2. ksft_exit_fail_msg() calls exit(). Remove the dead code.
split_huge_page_test.c:301:3: warning: code will never be executed
[-Wunreachable-code]
                goto cleanup;
                ^~~~~~~~~~~~

3. Remove duplicate inline.
pkey_sighandler_tests.c:44:15: warning: duplicate 'inline' declaration
specifier [-Wduplicate-decl-specifier]
static inline __always_inline

Signed-off-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
---
 tools/testing/selftests/mm/Makefile                | 1 +
 tools/testing/selftests/mm/hmm-tests.c             | 5 ++---
 tools/testing/selftests/mm/pkey_sighandler_tests.c | 2 +-
 tools/testing/selftests/mm/split_huge_page_test.c  | 4 +---
 4 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
index d13b3cef2a2b2..23d4bf6215465 100644
--- a/tools/testing/selftests/mm/Makefile
+++ b/tools/testing/selftests/mm/Makefile
@@ -34,6 +34,7 @@ endif
 MAKEFLAGS += --no-builtin-rules
 
 CFLAGS = -Wall -O2 -I $(top_srcdir) $(EXTRA_CFLAGS) $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
+CFLAGS += -Wunreachable-code
 LDLIBS = -lrt -lpthread -lm
 
 # Some distributions (such as Ubuntu) configure GCC so that _FORTIFY_SOURCE is
diff --git a/tools/testing/selftests/mm/hmm-tests.c b/tools/testing/selftests/mm/hmm-tests.c
index 141bf63cbe05e..15aadaf24a667 100644
--- a/tools/testing/selftests/mm/hmm-tests.c
+++ b/tools/testing/selftests/mm/hmm-tests.c
@@ -2027,11 +2027,10 @@ TEST_F(hmm, hmm_cow_in_device)
 	if (pid == -1)
 		ASSERT_EQ(pid, 0);
 	if (!pid) {
-		/* Child process waitd for SIGTERM from the parent. */
+		/* Child process waits for SIGTERM from the parent. */
 		while (1) {
 		}
-		perror("Should not reach this\n");
-		exit(0);
+		/* Should not reach this */
 	}
 	/* Parent process writes to COW pages(s) and gets a
 	 * new copy in system. In case of device private pages,
diff --git a/tools/testing/selftests/mm/pkey_sighandler_tests.c b/tools/testing/selftests/mm/pkey_sighandler_tests.c
index b5e076a564c95..302fef54049c8 100644
--- a/tools/testing/selftests/mm/pkey_sighandler_tests.c
+++ b/tools/testing/selftests/mm/pkey_sighandler_tests.c
@@ -41,7 +41,7 @@ static siginfo_t siginfo = {0};
  * syscall will attempt to access the PLT in order to call a library function
  * which is protected by MPK 0 which we don't have access to.
  */
-static inline __always_inline
+static __always_inline
 long syscall_raw(long n, long a1, long a2, long a3, long a4, long a5, long a6)
 {
 	unsigned long ret;
diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c
index 05de1fc0005b7..a85b2e393e4e8 100644
--- a/tools/testing/selftests/mm/split_huge_page_test.c
+++ b/tools/testing/selftests/mm/split_huge_page_test.c
@@ -296,10 +296,8 @@ void split_file_backed_thp(int order)
 		ksft_exit_fail_msg("Unable to create a tmpfs for testing\n");
 
 	status = snprintf(testfile, INPUT_MAX, "%s/thp_file", tmpfs_loc);
-	if (status >= INPUT_MAX) {
+	if (status >= INPUT_MAX)
 		ksft_exit_fail_msg("Fail to create file-backed THP split testing file\n");
-		goto cleanup;
-	}
 
 	fd = open(testfile, O_CREAT|O_RDWR, 0664);
 	if (fd == -1) {
-- 
2.39.5
Re: [PATCH v2 1/8] selftests/mm: Add -Wunreachable-code and fix warnings
Posted by Kevin Brodsky 1 month, 2 weeks ago
On 31/07/2025 18:01, Muhammad Usama Anjum wrote:
> [...]
>
> diff --git a/tools/testing/selftests/mm/pkey_sighandler_tests.c b/tools/testing/selftests/mm/pkey_sighandler_tests.c
> index b5e076a564c95..302fef54049c8 100644
> --- a/tools/testing/selftests/mm/pkey_sighandler_tests.c
> +++ b/tools/testing/selftests/mm/pkey_sighandler_tests.c
> @@ -41,7 +41,7 @@ static siginfo_t siginfo = {0};
>   * syscall will attempt to access the PLT in order to call a library function
>   * which is protected by MPK 0 which we don't have access to.
>   */
> -static inline __always_inline
> +static __always_inline

Thanks for this, I had this fix locally but never got to post it!

>  long syscall_raw(long n, long a1, long a2, long a3, long a4, long a5, long a6)
>  {
>  	unsigned long ret;
> diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c
> index 05de1fc0005b7..a85b2e393e4e8 100644
> --- a/tools/testing/selftests/mm/split_huge_page_test.c
> +++ b/tools/testing/selftests/mm/split_huge_page_test.c
> @@ -296,10 +296,8 @@ void split_file_backed_thp(int order)
>  		ksft_exit_fail_msg("Unable to create a tmpfs for testing\n");
>  
>  	status = snprintf(testfile, INPUT_MAX, "%s/thp_file", tmpfs_loc);
> -	if (status >= INPUT_MAX) {
> +	if (status >= INPUT_MAX)
>  		ksft_exit_fail_msg("Fail to create file-backed THP split testing file\n");
> -		goto cleanup;

At that point the mount() call has succeeded so I think we do want to
keep the goto and just print the error message instead of calling
replace ksft_exit_fail_msg().

- Kevin

> -	}
>  
>  	fd = open(testfile, O_CREAT|O_RDWR, 0664);
>  	if (fd == -1) {
Re: [PATCH v2 1/8] selftests/mm: Add -Wunreachable-code and fix warnings
Posted by Muhammad Usama Anjum 1 month, 2 weeks ago
On 8/18/25 1:10 PM, Kevin Brodsky wrote:
> On 31/07/2025 18:01, Muhammad Usama Anjum wrote:
>> [...]
>>
>> diff --git a/tools/testing/selftests/mm/pkey_sighandler_tests.c b/tools/testing/selftests/mm/pkey_sighandler_tests.c
>> index b5e076a564c95..302fef54049c8 100644
>> --- a/tools/testing/selftests/mm/pkey_sighandler_tests.c
>> +++ b/tools/testing/selftests/mm/pkey_sighandler_tests.c
>> @@ -41,7 +41,7 @@ static siginfo_t siginfo = {0};
>>   * syscall will attempt to access the PLT in order to call a library function
>>   * which is protected by MPK 0 which we don't have access to.
>>   */
>> -static inline __always_inline
>> +static __always_inline
> 
> Thanks for this, I had this fix locally but never got to post it!
> 
>>  long syscall_raw(long n, long a1, long a2, long a3, long a4, long a5, long a6)
>>  {
>>  	unsigned long ret;
>> diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c
>> index 05de1fc0005b7..a85b2e393e4e8 100644
>> --- a/tools/testing/selftests/mm/split_huge_page_test.c
>> +++ b/tools/testing/selftests/mm/split_huge_page_test.c
>> @@ -296,10 +296,8 @@ void split_file_backed_thp(int order)
>>  		ksft_exit_fail_msg("Unable to create a tmpfs for testing\n");
>>  
>>  	status = snprintf(testfile, INPUT_MAX, "%s/thp_file", tmpfs_loc);
>> -	if (status >= INPUT_MAX) {
>> +	if (status >= INPUT_MAX)
>>  		ksft_exit_fail_msg("Fail to create file-backed THP split testing file\n");
>> -		goto cleanup;
> 
> At that point the mount() call has succeeded so I think we do want to
> keep the goto and just print the error message instead of calling
> replace ksft_exit_fail_msg().
The cleanup tag does cleanup and then calls ksft_exit_fail_msg() without printing the
actual reason of failure. So current flow seems good where information about the error
is being printed.

> 
> - Kevin
> 
>> -	}
>>  
>>  	fd = open(testfile, O_CREAT|O_RDWR, 0664);
>>  	if (fd == -1) {


-- 
---
Thanks,
Usama
Re: [PATCH v2 1/8] selftests/mm: Add -Wunreachable-code and fix warnings
Posted by Kevin Brodsky 1 month, 2 weeks ago
On 21/08/2025 08:19, Muhammad Usama Anjum wrote:
>>> [...]
>>>
>>> diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c
>>> index 05de1fc0005b7..a85b2e393e4e8 100644
>>> --- a/tools/testing/selftests/mm/split_huge_page_test.c
>>> +++ b/tools/testing/selftests/mm/split_huge_page_test.c
>>> @@ -296,10 +296,8 @@ void split_file_backed_thp(int order)
>>>  		ksft_exit_fail_msg("Unable to create a tmpfs for testing\n");
>>>  
>>>  	status = snprintf(testfile, INPUT_MAX, "%s/thp_file", tmpfs_loc);
>>> -	if (status >= INPUT_MAX) {
>>> +	if (status >= INPUT_MAX)
>>>  		ksft_exit_fail_msg("Fail to create file-backed THP split testing file\n");
>>> -		goto cleanup;
>> At that point the mount() call has succeeded so I think we do want to
>> keep the goto and just print the error message instead of calling
>> replace ksft_exit_fail_msg().
> The cleanup tag does cleanup and then calls ksft_exit_fail_msg() without printing the
> actual reason of failure. So current flow seems good where information about the error
> is being printed.

My point is that with this patch, ksft_exit_fail_msg() still causes the
test to exit right away without the cleanup steps (i.e.
unmounting/removing the directory) being executed. Printing a specific
error message is good but we should also run the cleanup steps.

- Kevin
Re: [PATCH v2 1/8] selftests/mm: Add -Wunreachable-code and fix warnings
Posted by Sidhartha Kumar 2 months ago
On 7/31/25 12:01 PM, Muhammad Usama Anjum wrote:
> Enable -Wunreachable-code flag to catch dead code and fix them.
> 
> 1. Remove the dead code and write a comment instead:
> hmm-tests.c:2033:3: warning: code will never be executed
> [-Wunreachable-code]
>                  perror("Should not reach this\n");
>                  ^~~~~~
> 
> 2. ksft_exit_fail_msg() calls exit(). Remove the dead code.
> split_huge_page_test.c:301:3: warning: code will never be executed
> [-Wunreachable-code]
>                  goto cleanup;
>                  ^~~~~~~~~~~~
> 
> 3. Remove duplicate inline.
> pkey_sighandler_tests.c:44:15: warning: duplicate 'inline' declaration
> specifier [-Wduplicate-decl-specifier]
> static inline __always_inline
> 
> Signed-off-by: Muhammad Usama Anjum <usama.anjum@collabora.com>

Reviewed-by: Sidhartha Kumar <sidhartha.kumar@oracle.com>

> ---
>   tools/testing/selftests/mm/Makefile                | 1 +
>   tools/testing/selftests/mm/hmm-tests.c             | 5 ++---
>   tools/testing/selftests/mm/pkey_sighandler_tests.c | 2 +-
>   tools/testing/selftests/mm/split_huge_page_test.c  | 4 +---
>   4 files changed, 5 insertions(+), 7 deletions(-)
> 
> diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
> index d13b3cef2a2b2..23d4bf6215465 100644
> --- a/tools/testing/selftests/mm/Makefile
> +++ b/tools/testing/selftests/mm/Makefile
> @@ -34,6 +34,7 @@ endif
>   MAKEFLAGS += --no-builtin-rules
>   
>   CFLAGS = -Wall -O2 -I $(top_srcdir) $(EXTRA_CFLAGS) $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
> +CFLAGS += -Wunreachable-code
>   LDLIBS = -lrt -lpthread -lm
>   
>   # Some distributions (such as Ubuntu) configure GCC so that _FORTIFY_SOURCE is
> diff --git a/tools/testing/selftests/mm/hmm-tests.c b/tools/testing/selftests/mm/hmm-tests.c
> index 141bf63cbe05e..15aadaf24a667 100644
> --- a/tools/testing/selftests/mm/hmm-tests.c
> +++ b/tools/testing/selftests/mm/hmm-tests.c
> @@ -2027,11 +2027,10 @@ TEST_F(hmm, hmm_cow_in_device)
>   	if (pid == -1)
>   		ASSERT_EQ(pid, 0);
>   	if (!pid) {
> -		/* Child process waitd for SIGTERM from the parent. */
> +		/* Child process waits for SIGTERM from the parent. */
>   		while (1) {
>   		}
> -		perror("Should not reach this\n");
> -		exit(0);
> +		/* Should not reach this */
>   	}
>   	/* Parent process writes to COW pages(s) and gets a
>   	 * new copy in system. In case of device private pages,
> diff --git a/tools/testing/selftests/mm/pkey_sighandler_tests.c b/tools/testing/selftests/mm/pkey_sighandler_tests.c
> index b5e076a564c95..302fef54049c8 100644
> --- a/tools/testing/selftests/mm/pkey_sighandler_tests.c
> +++ b/tools/testing/selftests/mm/pkey_sighandler_tests.c
> @@ -41,7 +41,7 @@ static siginfo_t siginfo = {0};
>    * syscall will attempt to access the PLT in order to call a library function
>    * which is protected by MPK 0 which we don't have access to.
>    */
> -static inline __always_inline
> +static __always_inline
>   long syscall_raw(long n, long a1, long a2, long a3, long a4, long a5, long a6)
>   {
>   	unsigned long ret;
> diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c
> index 05de1fc0005b7..a85b2e393e4e8 100644
> --- a/tools/testing/selftests/mm/split_huge_page_test.c
> +++ b/tools/testing/selftests/mm/split_huge_page_test.c
> @@ -296,10 +296,8 @@ void split_file_backed_thp(int order)
>   		ksft_exit_fail_msg("Unable to create a tmpfs for testing\n");
>   
>   	status = snprintf(testfile, INPUT_MAX, "%s/thp_file", tmpfs_loc);
> -	if (status >= INPUT_MAX) {
> +	if (status >= INPUT_MAX)
>   		ksft_exit_fail_msg("Fail to create file-backed THP split testing file\n");
> -		goto cleanup;
> -	}
>   
>   	fd = open(testfile, O_CREAT|O_RDWR, 0664);
>   	if (fd == -1) {