linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/1] selftests/futex: Check for shmget support at runtime
@ 2025-08-07 12:00 Wake Liu
  2025-08-07 12:00 ` [PATCH v2 1/1] " Wake Liu
  0 siblings, 1 reply; 4+ messages in thread
From: Wake Liu @ 2025-08-07 12:00 UTC (permalink / raw)
  To: tglx, mingo, shuah, linux-kernel, linux-kselftest
  Cc: peterz, dvhart, dave, andrealmeid, wakel

Changes in v2:
- Restore RET_FAIL assignments in error paths to ensure the test's exit
  code accurately reflects the failure status.

Wake Liu (1):
  selftests/futex: Check for shmget support at runtime

 .../selftests/futex/functional/futex_wait.c   | 49 +++++++------
 .../selftests/futex/functional/futex_waitv.c  | 73 ++++++++++++-------
 2 files changed, 73 insertions(+), 49 deletions(-)

-- 
2.50.1.703.g449372360f-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/1] selftests/futex: Check for shmget support at runtime
  2025-08-07 12:00 [PATCH v2 0/1] selftests/futex: Check for shmget support at runtime Wake Liu
@ 2025-08-07 12:00 ` Wake Liu
  2025-08-07 12:14   ` Thomas Weißschuh
  0 siblings, 1 reply; 4+ messages in thread
From: Wake Liu @ 2025-08-07 12:00 UTC (permalink / raw)
  To: tglx, mingo, shuah, linux-kernel, linux-kselftest
  Cc: peterz, dvhart, dave, andrealmeid, wakel

The futex tests `futex_wait.c` and `futex_waitv.c` rely on the `shmget()`
syscall, which may not be available if the kernel is built without
System V IPC support (CONFIG_SYSVIPC=n). This can lead to test
failures on such systems.

This patch modifies the tests to check for `shmget()` support at
runtime by calling it and checking for an `ENOSYS` error. If `shmget()`
is not supported, the tests are skipped with a clear message,
improving the user experience and preventing false negatives.

This approach is more robust than relying on compile-time checks and
ensures that the tests run only when the required kernel features are
present.

Signed-off-by: Wake Liu <wakel@google.com>
---
 .../selftests/futex/functional/futex_wait.c   | 49 +++++++------
 .../selftests/futex/functional/futex_waitv.c  | 73 ++++++++++++-------
 2 files changed, 73 insertions(+), 49 deletions(-)

diff --git a/tools/testing/selftests/futex/functional/futex_wait.c b/tools/testing/selftests/futex/functional/futex_wait.c
index 685140d9b93d..17a465313a59 100644
--- a/tools/testing/selftests/futex/functional/futex_wait.c
+++ b/tools/testing/selftests/futex/functional/futex_wait.c
@@ -48,7 +48,7 @@ static void *waiterfn(void *arg)
 int main(int argc, char *argv[])
 {
 	int res, ret = RET_PASS, fd, c, shm_id;
-	u_int32_t f_private = 0, *shared_data;
+	u_int32_t f_private = 0, *shared_data = NULL;
 	unsigned int flags = FUTEX_PRIVATE_FLAG;
 	pthread_t waiter;
 	void *shm;
@@ -96,32 +96,35 @@ int main(int argc, char *argv[])
 	/* Testing an anon page shared memory */
 	shm_id = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
 	if (shm_id < 0) {
-		perror("shmget");
-		exit(1);
-	}
-
-	shared_data = shmat(shm_id, NULL, 0);
+		if (errno == ENOSYS) {
+			ksft_test_result_skip("Kernel does not support System V shared memory\n");
+		} else {
+			ksft_test_result_fail("shmget() failed with error: %s\n", strerror(errno));
+			ret = RET_FAIL;
+		}
+	} else {
+		shared_data = shmat(shm_id, NULL, 0);
 
-	*shared_data = 0;
-	futex = shared_data;
+		*shared_data = 0;
+		futex = shared_data;
 
-	info("Calling shared (page anon) futex_wait on futex: %p\n", futex);
-	if (pthread_create(&waiter, NULL, waiterfn, NULL))
-		error("pthread_create failed\n", errno);
+		info("Calling shared (page anon) futex_wait on futex: %p\n", futex);
+		if (pthread_create(&waiter, NULL, waiterfn, NULL))
+			error("pthread_create failed\n", errno);
 
-	usleep(WAKE_WAIT_US);
+		usleep(WAKE_WAIT_US);
 
-	info("Calling shared (page anon) futex_wake on futex: %p\n", futex);
-	res = futex_wake(futex, 1, 0);
-	if (res != 1) {
-		ksft_test_result_fail("futex_wake shared (page anon) returned: %d %s\n",
-				      errno, strerror(errno));
-		ret = RET_FAIL;
-	} else {
-		ksft_test_result_pass("futex_wake shared (page anon) succeeds\n");
+		info("Calling shared (page anon) futex_wake on futex: %p\n", futex);
+		res = futex_wake(futex, 1, 0);
+		if (res != 1) {
+			ksft_test_result_fail("futex_wake shared (page anon) returned: %d %s\n",
+					      errno, strerror(errno));
+			ret = RET_FAIL;
+		} else {
+			ksft_test_result_pass("futex_wake shared (page anon) succeeds\n");
+		}
 	}
 
-
 	/* Testing a file backed shared memory */
 	fd = open(SHM_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
 	if (fd < 0) {
@@ -161,7 +164,8 @@ int main(int argc, char *argv[])
 	}
 
 	/* Freeing resources */
-	shmdt(shared_data);
+	if (shared_data)
+		shmdt(shared_data);
 	munmap(shm, sizeof(f_private));
 	remove(SHM_PATH);
 	close(fd);
@@ -169,3 +173,4 @@ int main(int argc, char *argv[])
 	ksft_print_cnts();
 	return ret;
 }
+
diff --git a/tools/testing/selftests/futex/functional/futex_waitv.c b/tools/testing/selftests/futex/functional/futex_waitv.c
index a94337f677e1..034dbfef40cb 100644
--- a/tools/testing/selftests/futex/functional/futex_waitv.c
+++ b/tools/testing/selftests/futex/functional/futex_waitv.c
@@ -110,40 +110,58 @@ int main(int argc, char *argv[])
 	}
 
 	/* Shared waitv */
-	for (i = 0; i < NR_FUTEXES; i++) {
-		int shm_id = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
-
-		if (shm_id < 0) {
-			perror("shmget");
-			exit(1);
+	bool shm_supported = true;
+	int shm_id = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
+
+	if (shm_id < 0) {
+		if (errno == ENOSYS) {
+			shm_supported = false;
+			ksft_test_result_skip("Kernel does not support System V shared memory\n");
+		} else {
+			ksft_test_result_fail("shmget() failed with error: %s\n", strerror(errno));
+			ret = RET_FAIL;
+			shm_supported = false;
 		}
+	} else {
+		shmctl(shm_id, IPC_RMID, NULL);
+	}
 
-		unsigned int *shared_data = shmat(shm_id, NULL, 0);
+	if (shm_supported) {
+		for (i = 0; i < NR_FUTEXES; i++) {
+			int shm_id = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
 
-		*shared_data = 0;
-		waitv[i].uaddr = (uintptr_t)shared_data;
-		waitv[i].flags = FUTEX_32;
-		waitv[i].val = 0;
-		waitv[i].__reserved = 0;
-	}
+			if (shm_id < 0) {
+				perror("shmget");
+				exit(1);
+			}
 
-	if (pthread_create(&waiter, NULL, waiterfn, NULL))
-		error("pthread_create failed\n", errno);
+			unsigned int *shared_data = shmat(shm_id, NULL, 0);
 
-	usleep(WAKE_WAIT_US);
+			*shared_data = 0;
+			waitv[i].uaddr = (uintptr_t)shared_data;
+			waitv[i].flags = FUTEX_32;
+			waitv[i].val = 0;
+			waitv[i].__reserved = 0;
+		}
 
-	res = futex_wake(u64_to_ptr(waitv[NR_FUTEXES - 1].uaddr), 1, 0);
-	if (res != 1) {
-		ksft_test_result_fail("futex_wake shared returned: %d %s\n",
-				      res ? errno : res,
-				      res ? strerror(errno) : "");
-		ret = RET_FAIL;
-	} else {
-		ksft_test_result_pass("futex_waitv shared\n");
-	}
+		if (pthread_create(&waiter, NULL, waiterfn, NULL))
+			error("pthread_create failed\n", errno);
 
-	for (i = 0; i < NR_FUTEXES; i++)
-		shmdt(u64_to_ptr(waitv[i].uaddr));
+		usleep(WAKE_WAIT_US);
+
+		res = futex_wake(u64_to_ptr(waitv[NR_FUTEXES - 1].uaddr), 1, 0);
+        if (res != 1) {
+            ksft_test_result_fail("futex_wake shared returned: %d %s\n",
+                                  res ? errno : res,
+                                  res ? strerror(errno) : "");
+            ret = RET_FAIL;
+        } else {
+			ksft_test_result_pass("futex_waitv shared\n");
+		}
+
+		for (i = 0; i < NR_FUTEXES; i++)
+			shmdt(u64_to_ptr(waitv[i].uaddr));
+	}
 
 	/* Testing a waiter without FUTEX_32 flag */
 	waitv[0].flags = FUTEX_PRIVATE_FLAG;
@@ -235,3 +253,4 @@ int main(int argc, char *argv[])
 	ksft_print_cnts();
 	return ret;
 }
+
-- 
2.50.1.703.g449372360f-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 1/1] selftests/futex: Check for shmget support at runtime
  2025-08-07 12:00 ` [PATCH v2 1/1] " Wake Liu
@ 2025-08-07 12:14   ` Thomas Weißschuh
  2025-08-09 10:28     ` [PATCH v4] " Wake Liu
  0 siblings, 1 reply; 4+ messages in thread
From: Thomas Weißschuh @ 2025-08-07 12:14 UTC (permalink / raw)
  To: Wake Liu
  Cc: tglx, mingo, shuah, linux-kernel, linux-kselftest, peterz, dvhart,
	dave, andrealmeid

On Thu, Aug 07, 2025 at 08:00:42PM +0800, Wake Liu wrote:
> The futex tests `futex_wait.c` and `futex_waitv.c` rely on the `shmget()`
> syscall, which may not be available if the kernel is built without
> System V IPC support (CONFIG_SYSVIPC=n). This can lead to test
> failures on such systems.
> 
> This patch modifies the tests to check for `shmget()` support at
> runtime by calling it and checking for an `ENOSYS` error. If `shmget()`
> is not supported, the tests are skipped with a clear message,
> improving the user experience and preventing false negatives.
> 
> This approach is more robust than relying on compile-time checks and
> ensures that the tests run only when the required kernel features are
> present.
> 
> Signed-off-by: Wake Liu <wakel@google.com>
> ---
>  .../selftests/futex/functional/futex_wait.c   | 49 +++++++------
>  .../selftests/futex/functional/futex_waitv.c  | 73 ++++++++++++-------
>  2 files changed, 73 insertions(+), 49 deletions(-)
> 
> diff --git a/tools/testing/selftests/futex/functional/futex_wait.c b/tools/testing/selftests/futex/functional/futex_wait.c
> index 685140d9b93d..17a465313a59 100644
> --- a/tools/testing/selftests/futex/functional/futex_wait.c
> +++ b/tools/testing/selftests/futex/functional/futex_wait.c
> @@ -48,7 +48,7 @@ static void *waiterfn(void *arg)
>  int main(int argc, char *argv[])
>  {
>  	int res, ret = RET_PASS, fd, c, shm_id;
> -	u_int32_t f_private = 0, *shared_data;
> +	u_int32_t f_private = 0, *shared_data = NULL;
>  	unsigned int flags = FUTEX_PRIVATE_FLAG;
>  	pthread_t waiter;
>  	void *shm;
> @@ -96,32 +96,35 @@ int main(int argc, char *argv[])
>  	/* Testing an anon page shared memory */
>  	shm_id = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
>  	if (shm_id < 0) {
> -		perror("shmget");
> -		exit(1);
> -	}
> -
> -	shared_data = shmat(shm_id, NULL, 0);
> +		if (errno == ENOSYS) {
> +			ksft_test_result_skip("Kernel does not support System V shared memory\n");
> +		} else {
> +			ksft_test_result_fail("shmget() failed with error: %s\n", strerror(errno));
> +			ret = RET_FAIL;

kselftest.h is already keeping track of the failure status.
Just call ksft_finished() at the end.
Also the whole perror()/exit(1) pattern doesn't really make sense in a kselftest.

> +		}
> +	} else {
> +		shared_data = shmat(shm_id, NULL, 0);

(...)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v4] selftests/futex: Check for shmget support at runtime
  2025-08-07 12:14   ` Thomas Weißschuh
@ 2025-08-09 10:28     ` Wake Liu
  0 siblings, 0 replies; 4+ messages in thread
From: Wake Liu @ 2025-08-09 10:28 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Shuah Khan
  Cc: Peter Zijlstra, Darren Hart, Davidlohr Bueso, André Almeida,
	Wake Liu, linux-kernel, linux-kselftest

The futex tests `futex_wait.c` and `futex_waitv.c` rely on the `shmget()`
syscall, which may not be available if the kernel is built without
System V IPC support (CONFIG_SYSVIPC=n). This can lead to test
failures on such systems.

This patch modifies the tests to check for `shmget()` support at
runtime by calling it and checking for an `ENOSYS` error. If `shmget()`
is not supported, the tests are skipped with a clear message,
improving the user experience and preventing false negatives.

This approach is more robust than relying on compile-time checks and
ensures that the tests run only when the required kernel features are
present.

---
+V3 -> V4:
- Use ksft_finished() to simplify test exit.
- Replace perror()/exit() with ksft_exit_fail_perror().

Signed-off-by: Wake Liu <wakel@google.com>
---
 .../selftests/futex/functional/futex_wait.c   | 51 +++++++------
 .../selftests/futex/functional/futex_waitv.c  | 74 +++++++++++--------
 2 files changed, 72 insertions(+), 53 deletions(-)

diff --git a/tools/testing/selftests/futex/functional/futex_wait.c b/tools/testing/selftests/futex/functional/futex_wait.c
index 685140d9b93d..ffe9a94c1267 100644
--- a/tools/testing/selftests/futex/functional/futex_wait.c
+++ b/tools/testing/selftests/futex/functional/futex_wait.c
@@ -48,7 +48,7 @@ static void *waiterfn(void *arg)
 int main(int argc, char *argv[])
 {
 	int res, ret = RET_PASS, fd, c, shm_id;
-	u_int32_t f_private = 0, *shared_data;
+	u_int32_t f_private = 0, *shared_data = NULL;
 	unsigned int flags = FUTEX_PRIVATE_FLAG;
 	pthread_t waiter;
 	void *shm;
@@ -96,32 +96,34 @@ int main(int argc, char *argv[])
 	/* Testing an anon page shared memory */
 	shm_id = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
 	if (shm_id < 0) {
-		perror("shmget");
-		exit(1);
-	}
+		if (errno == ENOSYS) {
+			ksft_test_result_skip("Kernel does not support System V shared memory\n");
+		} else {
+            ksft_test_result_fail("shmget() failed with error: %s\n", strerror(errno));
+        }
+	} else {
+		shared_data = shmat(shm_id, NULL, 0);
 
-	shared_data = shmat(shm_id, NULL, 0);
+		*shared_data = 0;
+		futex = shared_data;
 
-	*shared_data = 0;
-	futex = shared_data;
+		info("Calling shared (page anon) futex_wait on futex: %p\n", futex);
+		if (pthread_create(&waiter, NULL, waiterfn, NULL))
+			error("pthread_create failed\n", errno);
 
-	info("Calling shared (page anon) futex_wait on futex: %p\n", futex);
-	if (pthread_create(&waiter, NULL, waiterfn, NULL))
-		error("pthread_create failed\n", errno);
-
-	usleep(WAKE_WAIT_US);
+		usleep(WAKE_WAIT_US);
 
-	info("Calling shared (page anon) futex_wake on futex: %p\n", futex);
-	res = futex_wake(futex, 1, 0);
-	if (res != 1) {
-		ksft_test_result_fail("futex_wake shared (page anon) returned: %d %s\n",
-				      errno, strerror(errno));
-		ret = RET_FAIL;
-	} else {
-		ksft_test_result_pass("futex_wake shared (page anon) succeeds\n");
+		info("Calling shared (page anon) futex_wake on futex: %p\n", futex);
+		res = futex_wake(futex, 1, 0);
+		if (res != 1) {
+			ksft_test_result_fail("futex_wake shared (page anon) returned: %d %s\n",
+					      errno, strerror(errno));
+			ret = RET_FAIL;
+		} else {
+			ksft_test_result_pass("futex_wake shared (page anon) succeeds\n");
+		}
 	}
 
-
 	/* Testing a file backed shared memory */
 	fd = open(SHM_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
 	if (fd < 0) {
@@ -161,11 +163,12 @@ int main(int argc, char *argv[])
 	}
 
 	/* Freeing resources */
-	shmdt(shared_data);
+	if (shared_data)
+		shmdt(shared_data);
 	munmap(shm, sizeof(f_private));
 	remove(SHM_PATH);
 	close(fd);
 
-	ksft_print_cnts();
-	return ret;
+	ksft_finished();
 }
+
diff --git a/tools/testing/selftests/futex/functional/futex_waitv.c b/tools/testing/selftests/futex/functional/futex_waitv.c
index a94337f677e1..681d78ee869f 100644
--- a/tools/testing/selftests/futex/functional/futex_waitv.c
+++ b/tools/testing/selftests/futex/functional/futex_waitv.c
@@ -110,40 +110,56 @@ int main(int argc, char *argv[])
 	}
 
 	/* Shared waitv */
-	for (i = 0; i < NR_FUTEXES; i++) {
-		int shm_id = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
+	bool shm_supported = true;
+	int shm_id = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
+
+	if (shm_id < 0) {
+		if (errno == ENOSYS) {
+			shm_supported = false;
+			ksft_test_result_skip("Kernel does not support System V shared memory\n");
+		} else {
+            ksft_test_result_fail("shmget() failed with error: %s\n", strerror(errno));
+            shm_supported = false;
+        }
+	} else {
+		shmctl(shm_id, IPC_RMID, NULL);
+	}
 
-		if (shm_id < 0) {
-			perror("shmget");
-			exit(1);
-		}
+	if (shm_supported) {
+		for (i = 0; i < NR_FUTEXES; i++) {
+			int shm_id = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
 
-		unsigned int *shared_data = shmat(shm_id, NULL, 0);
+			if (shm_id < 0) {
+				ksft_exit_fail_perror("shmget");
+			}
 
-		*shared_data = 0;
-		waitv[i].uaddr = (uintptr_t)shared_data;
-		waitv[i].flags = FUTEX_32;
-		waitv[i].val = 0;
-		waitv[i].__reserved = 0;
-	}
+			unsigned int *shared_data = shmat(shm_id, NULL, 0);
 
-	if (pthread_create(&waiter, NULL, waiterfn, NULL))
-		error("pthread_create failed\n", errno);
+			*shared_data = 0;
+			waitv[i].uaddr = (uintptr_t)shared_data;
+			waitv[i].flags = FUTEX_32;
+			waitv[i].val = 0;
+			waitv[i].__reserved = 0;
+		}
 
-	usleep(WAKE_WAIT_US);
+		if (pthread_create(&waiter, NULL, waiterfn, NULL))
+			error("pthread_create failed\n", errno);
 
-	res = futex_wake(u64_to_ptr(waitv[NR_FUTEXES - 1].uaddr), 1, 0);
-	if (res != 1) {
-		ksft_test_result_fail("futex_wake shared returned: %d %s\n",
-				      res ? errno : res,
-				      res ? strerror(errno) : "");
-		ret = RET_FAIL;
-	} else {
-		ksft_test_result_pass("futex_waitv shared\n");
-	}
+		usleep(WAKE_WAIT_US);
 
-	for (i = 0; i < NR_FUTEXES; i++)
-		shmdt(u64_to_ptr(waitv[i].uaddr));
+		res = futex_wake(u64_to_ptr(waitv[NR_FUTEXES - 1].uaddr), 1, 0);
+        if (res != 1) {
+            ksft_test_result_fail("futex_wake shared returned: %d %s\n",
+                                  res ? errno : res,
+                                  res ? strerror(errno) : "");
+            ret = RET_FAIL;
+        } else {
+			ksft_test_result_pass("futex_waitv shared\n");
+		}
+
+		for (i = 0; i < NR_FUTEXES; i++)
+			shmdt(u64_to_ptr(waitv[i].uaddr));
+	}
 
 	/* Testing a waiter without FUTEX_32 flag */
 	waitv[0].flags = FUTEX_PRIVATE_FLAG;
@@ -232,6 +248,6 @@ int main(int argc, char *argv[])
 		ksft_test_result_pass("futex_waitv invalid clockid\n");
 	}
 
-	ksft_print_cnts();
-	return ret;
+	ksft_finished();
 }
+
-- 
2.50.1.703.g449372360f-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-08-09 10:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-07 12:00 [PATCH v2 0/1] selftests/futex: Check for shmget support at runtime Wake Liu
2025-08-07 12:00 ` [PATCH v2 1/1] " Wake Liu
2025-08-07 12:14   ` Thomas Weißschuh
2025-08-09 10:28     ` [PATCH v4] " Wake Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).