[PATCH v2 2/8] selftests/mm: protection_keys: Fix dead code

Muhammad Usama Anjum posted 8 patches 2 months ago
There is a newer version of this series
[PATCH v2 2/8] selftests/mm: protection_keys: Fix dead code
Posted by Muhammad Usama Anjum 2 months ago
The while loop doesn't execute and following warning gets generated:

protection_keys.c:561:15: warning: code will never be executed
[-Wunreachable-code]
                int rpkey = alloc_random_pkey();

Let's enable the while loop such that it gets executed nr_iterations
times. Simplify the code a bit as well.

Signed-off-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
---
 tools/testing/selftests/mm/protection_keys.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/tools/testing/selftests/mm/protection_keys.c b/tools/testing/selftests/mm/protection_keys.c
index 23ebec367015f..6281d4c61b50e 100644
--- a/tools/testing/selftests/mm/protection_keys.c
+++ b/tools/testing/selftests/mm/protection_keys.c
@@ -557,13 +557,11 @@ int mprotect_pkey(void *ptr, size_t size, unsigned long orig_prot,
 	int nr_iterations = random() % 100;
 	int ret;
 
-	while (0) {
+	while (nr_iterations-- >= 0) {
 		int rpkey = alloc_random_pkey();
 		ret = sys_mprotect_pkey(ptr, size, orig_prot, pkey);
 		dprintf1("sys_mprotect_pkey(%p, %zx, prot=0x%lx, pkey=%ld) ret: %d\n",
 				ptr, size, orig_prot, pkey, ret);
-		if (nr_iterations-- < 0)
-			break;
 
 		dprintf1("%s()::%d, ret: %d pkey_reg: 0x%016llx"
 			" shadow: 0x%016llx\n",
-- 
2.39.5
Re: [PATCH v2 2/8] selftests/mm: protection_keys: Fix dead code
Posted by Kevin Brodsky 1 month, 2 weeks ago
+Dave Hansen

On 31/07/2025 18:01, Muhammad Usama Anjum wrote:
> The while loop doesn't execute and following warning gets generated:
>
> protection_keys.c:561:15: warning: code will never be executed
> [-Wunreachable-code]
>                 int rpkey = alloc_random_pkey();
>
> Let's enable the while loop such that it gets executed nr_iterations
> times. Simplify the code a bit as well.
>
> Signed-off-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
> ---
>  tools/testing/selftests/mm/protection_keys.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
>
> diff --git a/tools/testing/selftests/mm/protection_keys.c b/tools/testing/selftests/mm/protection_keys.c
> index 23ebec367015f..6281d4c61b50e 100644
> --- a/tools/testing/selftests/mm/protection_keys.c
> +++ b/tools/testing/selftests/mm/protection_keys.c
> @@ -557,13 +557,11 @@ int mprotect_pkey(void *ptr, size_t size, unsigned long orig_prot,
>  	int nr_iterations = random() % 100;
>  	int ret;
>  
> -	while (0) {
> +	while (nr_iterations-- >= 0) {

Now that is a good catch! I had never realised this whole loop was dead
code...

The question is whether we really want it. This code looked exactly this
way when it was merged [1] so it has never been run. It looks
suspiciously like debug code.

Dave, should we just remove this?

- Kevin

[1]
https://lore.kernel.org/all/20160729163024.FC5A0C2D@viggo.jf.intel.com/T/#u

>  		int rpkey = alloc_random_pkey();
>  		ret = sys_mprotect_pkey(ptr, size, orig_prot, pkey);
>  		dprintf1("sys_mprotect_pkey(%p, %zx, prot=0x%lx, pkey=%ld) ret: %d\n",
>  				ptr, size, orig_prot, pkey, ret);
> -		if (nr_iterations-- < 0)
> -			break;
>  
>  		dprintf1("%s()::%d, ret: %d pkey_reg: 0x%016llx"
>  			" shadow: 0x%016llx\n",
Re: [PATCH v2 2/8] selftests/mm: protection_keys: Fix dead code
Posted by Sidhartha Kumar 2 months ago
On 7/31/25 12:01 PM, Muhammad Usama Anjum wrote:
> The while loop doesn't execute and following warning gets generated:
> 
> protection_keys.c:561:15: warning: code will never be executed
> [-Wunreachable-code]
>                  int rpkey = alloc_random_pkey();
> 
> Let's enable the while loop such that it gets executed nr_iterations
> times. Simplify the code a bit as well.
> 
> Signed-off-by: Muhammad Usama Anjum <usama.anjum@collabora.com>

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

> ---
>   tools/testing/selftests/mm/protection_keys.c | 4 +---
>   1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/tools/testing/selftests/mm/protection_keys.c b/tools/testing/selftests/mm/protection_keys.c
> index 23ebec367015f..6281d4c61b50e 100644
> --- a/tools/testing/selftests/mm/protection_keys.c
> +++ b/tools/testing/selftests/mm/protection_keys.c
> @@ -557,13 +557,11 @@ int mprotect_pkey(void *ptr, size_t size, unsigned long orig_prot,
>   	int nr_iterations = random() % 100;
>   	int ret;
>   
> -	while (0) {
> +	while (nr_iterations-- >= 0) {
>   		int rpkey = alloc_random_pkey();
>   		ret = sys_mprotect_pkey(ptr, size, orig_prot, pkey);
>   		dprintf1("sys_mprotect_pkey(%p, %zx, prot=0x%lx, pkey=%ld) ret: %d\n",
>   				ptr, size, orig_prot, pkey, ret);
> -		if (nr_iterations-- < 0)
> -			break;
>   
>   		dprintf1("%s()::%d, ret: %d pkey_reg: 0x%016llx"
>   			" shadow: 0x%016llx\n",