[PATCH v2 4/4] selftests/futex: Remove the shared memory segments of the futex tests

From: Mathieu Desnoyers

Date: Sun Nov 08 2026 - 09:50:56 EST


anon_page in futex_wait, and shared_waitv in futex_waitv, create System V
shared memory segments with shmget(IPC_PRIVATE). They detach them with
shmdt(), but never remove them. So each run leaks one segment for
anon_page, and 30 for shared_waitv. After about 136 runs of futex_waitv,
shmget() fails, at the default limit of 4096 segments.

Mark each segment for removal with shmctl(IPC_RMID) once it is attached.
The kernel then removes it at the last detach, or when the test exits.

With this patch, futex_waitv passed 2000 runs in a row in each of two
8-CPU VMs, and futex_wait 2000 runs.

Fixes: c3d128581f64 ("selftests: futex: Add futex wait test")
Fixes: 5e59c1d1c78c ("selftests: futex: Add sys_futex_waitv() test")
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
---
tools/testing/selftests/futex/functional/futex_wait.c | 2 ++
tools/testing/selftests/futex/functional/futex_waitv.c | 2 ++
2 files changed, 4 insertions(+)

diff --git a/tools/testing/selftests/futex/functional/futex_wait.c b/tools/testing/selftests/futex/functional/futex_wait.c
index 4bf7d8ee238b..4b7550aff4d0 100644
--- a/tools/testing/selftests/futex/functional/futex_wait.c
+++ b/tools/testing/selftests/futex/functional/futex_wait.c
@@ -115,6 +115,8 @@ TEST(anon_page)
ASSERT_NE(shared_data, (void *)-1)
TH_LOG("shmat failed: %s", strerror(errno));
}
+ /* The segment goes away once detached. */
+ shmctl(shm_id, IPC_RMID, NULL);

*shared_data = 0;
futex = shared_data;
diff --git a/tools/testing/selftests/futex/functional/futex_waitv.c b/tools/testing/selftests/futex/functional/futex_waitv.c
index ccfa31fd20fa..94ada55b83cf 100644
--- a/tools/testing/selftests/futex/functional/futex_waitv.c
+++ b/tools/testing/selftests/futex/functional/futex_waitv.c
@@ -114,6 +114,8 @@ TEST(shared_waitv)

unsigned int *shared_data = shmat(shm_id, NULL, 0);

+ /* The segment goes away once detached. */
+ shmctl(shm_id, IPC_RMID, NULL);
*shared_data = 0;
waitv[i].uaddr = (uintptr_t)shared_data;
waitv[i].flags = FUTEX_32;
--
2.47.3