Skip to contentSkip to conversation

selftest/futex: Make the error check more precise for futex_numa_mpol

PATCH Superseded André Almeida 2 participants 6 messages Sep 1, 2025 – Sep 4, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

André Almeida

author reviewer
#0 lore raw

PATCH 1/2 selftest/futex: Make the error check more precise for futex_numa_mpol

Instead of just checking if the syscall failed as expected, check as well what is the error code returned, to check if it's match the expectation and it's failing in the correct error path inside the kernel.

---
This patch is aimed for 6.18

---
 .../futex/functional/futex_numa_mpol.c        | 36 +++++++++++--------
 1 file changed, 21 insertions(+), 15 deletions(-)
tools/testing/selftests/futex/functional/futex_numa_mpol.c +21 -15
index 802c15c82190..c84441751235 100644
@@ -77,7 +77,7 @@ static void join_max_threads(void)
 	}
 }
 
-static void __test_futex(void *futex_ptr, int must_fail, unsigned int futex_flags)
+static void __test_futex(void *futex_ptr, int err_value, unsigned int futex_flags)
 {
 	int to_wake, ret, i, need_exit = 0;
 
@@ -88,11 +88,17 @@ static void __test_futex(void *futex_ptr, int must_fail, unsigned int futex_flag
 
 	do {
 		ret = futex2_wake(futex_ptr, to_wake, futex_flags);
-		if (must_fail) {
-			if (ret < 0)
-				break;
-			ksft_exit_fail_msg("futex2_wake(%d, 0x%x) should fail, but didn't\n",
-					   to_wake, futex_flags);
+
+		if (err_value) {
+			if (ret >= 0)
+				ksft_exit_fail_msg("futex2_wake(%d, 0x%x) should fail, but didn't\n",
+						   to_wake, futex_flags);
+
+			if (errno != err_value)
+				ksft_exit_fail_msg("futex2_wake(%d, 0x%x) expected error was %d, but returned %d (%s)\n",
+						   to_wake, futex_flags, err_value, errno, strerror(errno));
+
+			break;
 		}
 		if (ret < 0) {
 			ksft_exit_fail_msg("Failed futex2_wake(%d, 0x%x): %m\n",
@@ -106,12 +112,12 @@ static void __test_futex(void *futex_ptr, int must_fail, unsigned int futex_flag
 	join_max_threads();
 
 	for (i = 0; i < MAX_THREADS; i++) {
-		if (must_fail && thread_args[i].result != -1) {
+		if (err_value && thread_args[i].result != -1) {
 			ksft_print_msg("Thread %d should fail but succeeded (%d)\n",
 				       i, thread_args[i].result);
 			need_exit = 1;
 		}
-		if (!must_fail && thread_args[i].result != 0) {
+		if (!err_value && thread_args[i].result != 0) {
 			ksft_print_msg("Thread %d failed (%d)\n", i, thread_args[i].result);
 			need_exit = 1;
 		}
@@ -120,14 +126,14 @@ static void __test_futex(void *futex_ptr, int must_fail, unsigned int futex_flag
 		ksft_exit_fail_msg("Aborting due to earlier errors.\n");
 }
 
-static void test_futex(void *futex_ptr, int must_fail)
+static void test_futex(void *futex_ptr, int err_value)
 {
-	__test_futex(futex_ptr, must_fail, FUTEX2_SIZE_U32 | FUTEX_PRIVATE_FLAG | FUTEX2_NUMA);
+	__test_futex(futex_ptr, err_value, FUTEX2_SIZE_U32 | FUTEX_PRIVATE_FLAG | FUTEX2_NUMA);
 }
 
-static void test_futex_mpol(void *futex_ptr, int must_fail)
+static void test_futex_mpol(void *futex_ptr, int err_value)
 {
-	__test_futex(futex_ptr, must_fail, FUTEX2_SIZE_U32 | FUTEX_PRIVATE_FLAG | FUTEX2_NUMA | FUTEX2_MPOL);
+	__test_futex(futex_ptr, err_value, FUTEX2_SIZE_U32 | FUTEX_PRIVATE_FLAG | FUTEX2_NUMA | FUTEX2_MPOL);
 }
 
 static void usage(char *prog)
@@ -184,16 +190,16 @@ int main(int argc, char *argv[])
 
 	/* FUTEX2_NUMA futex must be 8-byte aligned */
 	ksft_print_msg("Mis-aligned futex\n");
-	test_futex(futex_ptr + mem_size - 4, 1);
+	test_futex(futex_ptr + mem_size - 4, 22);
 
 	futex_numa->numa = FUTEX_NO_NODE;
 	mprotect(futex_ptr, mem_size, PROT_READ);
 	ksft_print_msg("Memory, RO\n");
-	test_futex(futex_ptr, 1);
+	test_futex(futex_ptr, 14);
 
 	mprotect(futex_ptr, mem_size, PROT_NONE);
 	ksft_print_msg("Memory, no access\n");
-	test_futex(futex_ptr, 1);
+	test_futex(futex_ptr, 14);
 
 	mprotect(futex_ptr, mem_size, PROT_READ | PROT_WRITE);
 	ksft_print_msg("Memory back to RW\n");
Signature · 1 line
2.51.0
author reviewer
#1 lore raw

PATCH 2/2 selftest/futex: Reintroduce "Memory out of range" numa_mpol's subtest

Commit d8e2f919997 ("selftests/futex: Fix some futex_numa_mpol subtests") removed the "Memory out of range" subtest due to it being dependent on the memory layout of the test process having an invalid memory address just after the *futex_ptr allocated memory.

Reintroduce this test and make it deterministic, by allocation two memory pages and marking the second one with PROT_NONE.

---
This patch is aimed for 6.18

---
 .../testing/selftests/futex/functional/futex_numa_mpol.c  | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
tools/testing/selftests/futex/functional/futex_numa_mpol.c +7 -1
index c84441751235..e4b840184b1d 100644
@@ -174,10 +174,13 @@ int main(int argc, char *argv[])
 	ksft_set_plan(1);
 
 	mem_size = sysconf(_SC_PAGE_SIZE);
-	futex_ptr = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
+	futex_ptr = mmap(NULL, mem_size * 2, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
 	if (futex_ptr == MAP_FAILED)
 		ksft_exit_fail_msg("mmap() for %d bytes failed\n", mem_size);
 
+	/* Create an invalid memory region for the "Memory out of range" test */
+	mprotect(futex_ptr + mem_size, mem_size, PROT_NONE);
+
 	futex_numa = futex_ptr;
 
 	ksft_print_msg("Regular test\n");
@@ -192,6 +195,9 @@ int main(int argc, char *argv[])
 	ksft_print_msg("Mis-aligned futex\n");
 	test_futex(futex_ptr + mem_size - 4, 22);
 
+	ksft_print_msg("Memory out of range\n");
+	test_futex(futex_ptr + mem_size, 14);
+
 	futex_numa->numa = FUTEX_NO_NODE;
 	mprotect(futex_ptr, mem_size, PROT_READ);
 	ksft_print_msg("Memory, RO\n");
Signature · 1 line
2.51.0
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] selftest/futex: Reintroduce "Memory out of range" numa_mpol's subtest

Quoted 35 lines · André Almeida

Commit d8e2f919997 ("selftests/futex: Fix some futex_numa_mpol subtests") removed the "Memory out of range" subtest due to it being dependent on the memory layout of the test process having an invalid memory address just after the *futex_ptr allocated memory.

Reintroduce this test and make it deterministic, by allocation two memory pages and marking the second one with PROT_NONE.

---
This patch is aimed for 6.18

---
  .../testing/selftests/futex/functional/futex_numa_mpol.c  | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
tools/testing/selftests/futex/functional/futex_numa_mpol.c +4 -1
index c84441751235..e4b840184b1d 100644
@@ -174,10 +174,13 @@ int main(int argc, char *argv[])
  	ksft_set_plan(1);
 
  	mem_size = sysconf(_SC_PAGE_SIZE);
-	futex_ptr = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
+	futex_ptr = mmap(NULL, mem_size * 2, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  	if (futex_ptr == MAP_FAILED)
  		ksft_exit_fail_msg("mmap() for %d bytes failed\n", mem_size);
 
+	/* Create an invalid memory region for the "Memory out of range" test */
+	mprotect(futex_ptr + mem_size, mem_size, PROT_NONE);
+
  	futex_numa = futex_ptr;
 
  	ksft_print_msg("Regular test\n");

tools/testing/selftests/futex/functional/futex_numa_mpol.c int main(int argc, char *argv[]) · line 203

Quoted 9 lines
tools/testing/selftests/futex/functional/futex_numa_mpol.c +3 -0
@@ -192,6 +195,9 @@ int main(int argc, char *argv[])
  	ksft_print_msg("Mis-aligned futex\n");
  	test_futex(futex_ptr + mem_size - 4, 22);
 
+	ksft_print_msg("Memory out of range\n");
+	test_futex(futex_ptr + mem_size, 14);
+
  	futex_numa->numa = FUTEX_NO_NODE;
  	mprotect(futex_ptr, mem_size, PROT_READ);
  	ksft_print_msg("Memory, RO\n");
#3 lore raw
In reply to André Almeida
Quoted 35 lines · André Almeida

Instead of just checking if the syscall failed as expected, check as well what is the error code returned, to check if it's match the expectation and it's failing in the correct error path inside the kernel.

---
This patch is aimed for 6.18

---
  .../futex/functional/futex_numa_mpol.c        | 36 +++++++++++--------
  1 file changed, 21 insertions(+), 15 deletions(-)
tools/testing/selftests/futex/functional/futex_numa_mpol.c +2 -6
index 802c15c82190..c84441751235 100644
@@ -77,7 +77,7 @@ static void join_max_threads(void)
  	}
  }
 
-static void __test_futex(void *futex_ptr, int must_fail, unsigned int futex_flags)
+static void __test_futex(void *futex_ptr, int err_value, unsigned int futex_flags)
  {
  	int to_wake, ret, i, need_exit = 0;
 
@@ -88,11 +88,17 @@ static void __test_futex(void *futex_ptr, int must_fail, unsigned int futex_flag
 
  	do {
  		ret = futex2_wake(futex_ptr, to_wake, futex_flags);
-		if (must_fail) {
-			if (ret < 0)
-				break;
-			ksft_exit_fail_msg("futex2_wake(%d, 0x%x) should fail, but didn't\n",
-					   to_wake, futex_flags);
+

tools/testing/selftests/futex/functional/futex_numa_mpol.c static void __test_futex(void *futex_ptr, int must_fail, unsigned int futex_flag · line 101

Quoted 10 lines
tools/testing/selftests/futex/functional/futex_numa_mpol.c +10 -0
+		if (err_value) {
+			if (ret >= 0)
+				ksft_exit_fail_msg("futex2_wake(%d, 0x%x) should fail, but didn't\n",
+						   to_wake, futex_flags);
+
+			if (errno != err_value)
+				ksft_exit_fail_msg("futex2_wake(%d, 0x%x) expected error was %d, but returned %d (%s)\n",
+						   to_wake, futex_flags, err_value, errno, strerror(errno));
+
+			break;

If (ret >= 0), the 2nd (errno != err_value) failure message will likely be printed too. Should we use "else if" so that only one error message will be printed?

Quoted 47 lines
tools/testing/selftests/futex/functional/futex_numa_mpol.c +7 -7
  		}
  		if (ret < 0) {
  			ksft_exit_fail_msg("Failed futex2_wake(%d, 0x%x): %m\n",
@@ -106,12 +112,12 @@ static void __test_futex(void *futex_ptr, int must_fail, unsigned int futex_flag
  	join_max_threads();
 
  	for (i = 0; i < MAX_THREADS; i++) {
-		if (must_fail && thread_args[i].result != -1) {
+		if (err_value && thread_args[i].result != -1) {
  			ksft_print_msg("Thread %d should fail but succeeded (%d)\n",
  				       i, thread_args[i].result);
  			need_exit = 1;
  		}
-		if (!must_fail && thread_args[i].result != 0) {
+		if (!err_value && thread_args[i].result != 0) {
  			ksft_print_msg("Thread %d failed (%d)\n", i, thread_args[i].result);
  			need_exit = 1;
  		}
@@ -120,14 +126,14 @@ static void __test_futex(void *futex_ptr, int must_fail, unsigned int futex_flag
  		ksft_exit_fail_msg("Aborting due to earlier errors.\n");
  }
 
-static void test_futex(void *futex_ptr, int must_fail)
+static void test_futex(void *futex_ptr, int err_value)
  {
-	__test_futex(futex_ptr, must_fail, FUTEX2_SIZE_U32 | FUTEX_PRIVATE_FLAG | FUTEX2_NUMA);
+	__test_futex(futex_ptr, err_value, FUTEX2_SIZE_U32 | FUTEX_PRIVATE_FLAG | FUTEX2_NUMA);
  }
 
-static void test_futex_mpol(void *futex_ptr, int must_fail)
+static void test_futex_mpol(void *futex_ptr, int err_value)
  {
-	__test_futex(futex_ptr, must_fail, FUTEX2_SIZE_U32 | FUTEX_PRIVATE_FLAG | FUTEX2_NUMA | FUTEX2_MPOL);
+	__test_futex(futex_ptr, err_value, FUTEX2_SIZE_U32 | FUTEX_PRIVATE_FLAG | FUTEX2_NUMA | FUTEX2_MPOL);
  }
 
  static void usage(char *prog)
@@ -184,16 +190,16 @@ int main(int argc, char *argv[])
 
  	/* FUTEX2_NUMA futex must be 8-byte aligned */
  	ksft_print_msg("Mis-aligned futex\n");
-	test_futex(futex_ptr + mem_size - 4, 1);
+	test_futex(futex_ptr + mem_size - 4, 22);
 
  	futex_numa->numa = FUTEX_NO_NODE;
  	mprotect(futex_ptr, mem_size, PROT_READ);
  	ksft_print_msg("Memory, RO\n");

tools/testing/selftests/futex/functional/futex_numa_mpol.c int main(int argc, char *argv[]) · line 205

Quoted 10 lines
tools/testing/selftests/futex/functional/futex_numa_mpol.c +2 -2
-	test_futex(futex_ptr, 1);
+	test_futex(futex_ptr, 14);
 
  	mprotect(futex_ptr, mem_size, PROT_NONE);
  	ksft_print_msg("Memory, no access\n");
-	test_futex(futex_ptr, 1);
+	test_futex(futex_ptr, 14);
 
  	mprotect(futex_ptr, mem_size, PROT_READ | PROT_WRITE);
  	ksft_print_msg("Memory back to RW\n");

I believe it is better to use the error number mnemonic (EINVAL & EFAULT) instead of 22 and 14 as argument to make the code easier to read.

Cheers,
Longman

author reviewer
#4 lore raw

Hi Waiman,

Thanks for the feedback!

Em 03/09/2025 14:53, Waiman Long escreveu:

Quoted 44 lines · André Almeida
Quoted 43 lines

Instead of just checking if the syscall failed as expected, check as well what is the error code returned, to check if it's match the expectation and it's failing in the correct error path inside the kernel.

---
This patch is aimed for 6.18
---
  .../futex/functional/futex_numa_mpol.c        | 36 +++++++++++--------

  1 file changed, 21 insertions(+), 15 deletions(-)
tools/testing/selftests/futex/functional/futex_numa_mpol.c +1 -1
index 802c15c82190..c84441751235 100644
@@ -77,7 +77,7 @@ static void join_max_threads(void)
       }
   }
-static void __test_futex(void *futex_ptr, int must_fail, unsigned int 
 futex_flags)
+static void __test_futex(void *futex_ptr, int err_value, unsigned int 
 futex_flags)
   {
       int to_wake, ret, i, need_exit = 0;
@@ -88,11 +88,17 @@ static void __test_futex(void *futex_ptr, int 

must_fail, unsigned int futex_flag
      do {
          ret = futex2_wake(futex_ptr, to_wake, futex_flags); -        if (must_fail) {
-            if (ret < 0)
-                break;
-            ksft_exit_fail_msg("futex2_wake(%d, 0x%x) should fail, but didn't\n",
-                       to_wake, futex_flags);
+
+        if (err_value) {
+            if (ret >= 0)
+                ksft_exit_fail_msg("futex2_wake(%d, 0x%x) should fail, but didn't\n",
+                           to_wake, futex_flags);
+
+            if (errno != err_value)

Quoted 10 lines
Quoted 6 lines

+                ksft_exit_fail_msg("futex2_wake(%d, 0x%x) expected error was %d, but returned %d (%s)\n",
+                           to_wake, futex_flags, err_value, errno, strerror(errno));
+
+            break;

If (ret >= 0), the 2nd (errno != err_value) failure message will likely be printed too. Should we use "else if" so that only one error message will be printed?

ksft_exit_fail_msg() calls exit(), so the code will exit before executing the second failure message.

If this was a ksft_test_result_error() call, then the message would be printed twice.

Quoted 49 lines
Quoted 49 lines

          }
          if (ret < 0) {
              ksft_exit_fail_msg("Failed futex2_wake(%d, 0x%x): %m\n",

tools/testing/selftests/futex/functional/futex_numa_mpol.c +0 -0
@@ -106,12 +112,12 @@ static void __test_futex(void *futex_ptr, int 

must_fail, unsigned int futex_flag
      join_max_threads();
      for (i = 0; i < MAX_THREADS; i++) {
-        if (must_fail && thread_args[i].result != -1) {
+        if (err_value && thread_args[i].result != -1) { ksft_print_msg("Thread %d should fail but succeeded (%d)\n", i, thread_args[i].result);
              need_exit = 1;
          }
-        if (!must_fail && thread_args[i].result != 0) {
+        if (!err_value && thread_args[i].result != 0) { ksft_print_msg("Thread %d failed (%d)\n", i, thread_args[i].result); need_exit = 1;
          }

tools/testing/selftests/futex/functional/futex_numa_mpol.c +0 -0
@@ -120,14 +126,14 @@ static void __test_futex(void *futex_ptr, int 

must_fail, unsigned int futex_flag ksft_exit_fail_msg("Aborting due to earlier errors.\n"); }

tools/testing/selftests/futex/functional/futex_numa_mpol.c +1 -1
-static void test_futex(void *futex_ptr, int must_fail)
+static void test_futex(void *futex_ptr, int err_value)

  {
-    __test_futex(futex_ptr, must_fail, FUTEX2_SIZE_U32 | FUTEX_PRIVATE_FLAG | FUTEX2_NUMA);
+    __test_futex(futex_ptr, err_value, FUTEX2_SIZE_U32 | FUTEX_PRIVATE_FLAG | FUTEX2_NUMA);
  }

tools/testing/selftests/futex/functional/futex_numa_mpol.c +1 -1
-static void test_futex_mpol(void *futex_ptr, int must_fail)
+static void test_futex_mpol(void *futex_ptr, int err_value)

  {
-    __test_futex(futex_ptr, must_fail, FUTEX2_SIZE_U32 | FUTEX_PRIVATE_FLAG | FUTEX2_NUMA | FUTEX2_MPOL); +    __test_futex(futex_ptr, err_value, FUTEX2_SIZE_U32 | FUTEX_PRIVATE_FLAG | FUTEX2_NUMA | FUTEX2_MPOL); }
  static void usage(char *prog)

tools/testing/selftests/futex/functional/futex_numa_mpol.c +2 -2
@@ -184,16 +190,16 @@ int main(int argc, char *argv[])
       /* FUTEX2_NUMA futex must be 8-byte aligned */
       ksft_print_msg("Mis-aligned futex\n");
-    test_futex(futex_ptr + mem_size - 4, 1);
+    test_futex(futex_ptr + mem_size - 4, 22);
       futex_numa->numa = FUTEX_NO_NODE;
       mprotect(futex_ptr, mem_size, PROT_READ);
       ksft_print_msg("Memory, RO\n");
-    test_futex(futex_ptr, 1);
+    test_futex(futex_ptr, 14);
Quoted 9 lines

tools/testing/selftests/futex/functional/futex_numa_mpol.c int main(int argc, char *argv[]) · line 205

Quoted 6 lines
tools/testing/selftests/futex/functional/futex_numa_mpol.c +1 -1
       mprotect(futex_ptr, mem_size, PROT_NONE);
       ksft_print_msg("Memory, no access\n");
-    test_futex(futex_ptr, 1);
+    test_futex(futex_ptr, 14);
       mprotect(futex_ptr, mem_size, PROT_READ | PROT_WRITE);
       ksft_print_msg("Memory back to RW\n");

I believe it is better to use the error number mnemonic (EINVAL & EFAULT) instead of 22 and 14 as argument to make the code easier to read.

Good call, applied.

Thanks!
André

Quoted 3 lines

Cheers,
Longman

#5 lore raw
Quoted 61 lines · André Almeida

Hi Waiman,

Thanks for the feedback!

Em 03/09/2025 14:53, Waiman Long escreveu:

Quoted 45 lines · André Almeida
Quoted 44 lines

Instead of just checking if the syscall failed as expected, check as well what is the error code returned, to check if it's match the expectation and it's failing in the correct error path inside the kernel.

---
This patch is aimed for 6.18
---
  .../futex/functional/futex_numa_mpol.c        | 36
+++++++++++--------

  1 file changed, 21 insertions(+), 15 deletions(-)
tools/testing/selftests/futex/functional/futex_numa_mpol.c +1 -1
index 802c15c82190..c84441751235 100644
@@ -77,7 +77,7 @@ static void join_max_threads(void)
       }
   }
-static void __test_futex(void *futex_ptr, int must_fail, unsigned 
 int futex_flags)
+static void __test_futex(void *futex_ptr, int err_value, unsigned 
 int futex_flags)
   {
       int to_wake, ret, i, need_exit = 0;
@@ -88,11 +88,17 @@ static void __test_futex(void *futex_ptr, int 

must_fail, unsigned int futex_flag
      do {
          ret = futex2_wake(futex_ptr, to_wake, futex_flags); -        if (must_fail) {
-            if (ret < 0)
-                break;
-            ksft_exit_fail_msg("futex2_wake(%d, 0x%x) should fail, but didn't\n",
-                       to_wake, futex_flags);
+
+        if (err_value) {
+            if (ret >= 0)
+                ksft_exit_fail_msg("futex2_wake(%d, 0x%x) should fail, but didn't\n",
+                           to_wake, futex_flags);
+
+            if (errno != err_value)

Quoted 10 lines
Quoted 6 lines

+                ksft_exit_fail_msg("futex2_wake(%d, 0x%x) expected error was %d, but returned %d (%s)\n",
+                           to_wake, futex_flags, err_value, errno, strerror(errno));
+
+            break;

If (ret >= 0), the 2nd (errno != err_value) failure message will likely be printed too. Should we use "else if" so that only one error message will be printed?

Quoted 3 lines

ksft_exit_fail_msg() calls exit(), so the code will exit before executing the second failure message.

If this was a  ksft_test_result_error() call, then the message would be printed twice.

I didn't realize that. Thanks for letting me know. In that case, the code should be OK.

Cheers,
Longman