Skip to contentSkip to conversation

selftests/futex: Check for shmget support at runtime

PATCH Superseded Wake Liu 2 participants 4 messages Aug 7, 2025 – Aug 9, 2025

Subsystems (2)
lore
Share & tools
lore

Wake Liu

author
#0 lore raw

v2 cover selftests/futex: Check for shmget support at runtime

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(-)
Signature · 1 line
2.50.1.703.g449372360f-goog
author
#1 lore raw

PATCH v2 1/1 selftests/futex: Check for shmget support at runtime

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.

---
 .../selftests/futex/functional/futex_wait.c   | 49 +++++++------
 .../selftests/futex/functional/futex_waitv.c  | 73 ++++++++++++-------
 2 files changed, 73 insertions(+), 49 deletions(-)
tools/testing/selftests/futex/functional/futex_wait.c +27 -22
index 685140d9b93d..17a465313a59 100644
@@ -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;
 }
+
tools/testing/selftests/futex/functional/futex_waitv.c +46 -27

89 lines not on this page · raw message

Signature · 1 line
2.50.1.703.g449372360f-goog
#2 lore raw
Quoted 37 lines · Wake Liu

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.

---
 .../selftests/futex/functional/futex_wait.c   | 49 +++++++------
 .../selftests/futex/functional/futex_waitv.c  | 73 ++++++++++++-------
 2 files changed, 73 insertions(+), 49 deletions(-)
tools/testing/selftests/futex/functional/futex_wait.c +1 -1
index 685140d9b93d..17a465313a59 100644
@@ -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) {

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

Quoted 10 lines
tools/testing/selftests/futex/functional/futex_wait.c +5 -5
-		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.

Quoted 3 lines
tools/testing/selftests/futex/functional/futex_wait.c +3 -0
+		}
+	} else {
+		shared_data = shmat(shm_id, NULL, 0);

(...)

author
#3 lore raw

PATCH v4 1/1 selftests/futex: Check for shmget support at runtime

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().

---
 .../selftests/futex/functional/futex_wait.c   | 51 +++++++------
 .../selftests/futex/functional/futex_waitv.c  | 74 +++++++++++--------
 2 files changed, 72 insertions(+), 53 deletions(-)
tools/testing/selftests/futex/functional/futex_wait.c +27 -24
index 685140d9b93d..ffe9a94c1267 100644
@@ -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();
 }
+
tools/testing/selftests/futex/functional/futex_waitv.c +45 -29

91 lines not on this page · raw message

Signature · 1 line
2.50.1.703.g449372360f-goog