summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/futex
diff options
context:
space:
mode:
authorYuwen Chen <ywen.chen@foxmail.com>2026-05-18 10:16:54 +0800
committerThomas Gleixner <tglx@kernel.org>2026-07-20 20:58:51 +0200
commit23dd3f884926d945c1f6a7510f5b95164f2906e2 (patch)
treeaad76ed717ac2a4bf881a9bc97ad7cb55f914a8f /tools/testing/selftests/futex
parent6c4a1b972643d72bd26f674677df8b5a6a3fe94f (diff)
downloadlinux-next-23dd3f884926d945c1f6a7510f5b95164f2906e2.tar.gz
linux-next-23dd3f884926d945c1f6a7510f5b95164f2906e2.zip
selftests/futex: Use thread synchronization helpers instead of usleep()
This test uses usleep() to delay the main thread after creating the test thread[s] under the assumption that they already are blocked on the futex when the main thread continues. That "works" on otherwise idle systems, but fails under load resulting in failed selftests because the requeue operation starts before the waiters reached the kernel. Replace the usleep() waits by the new thread synchronization helpers to cure that. [ tglx: Adapted to test harness changes, fixed coding style, sanitized the timeout handling and rewrote change log. Co-developed-by: Edward Liaw <edliaw@google.com> Signed-off-by: Yuwen Chen <ywen.chen@foxmail.com> Signed-off-by: Edward Liaw <edliaw@google.com> Signed-off-by: Thomas Gleixner <tglx@kernel.org> Link: https://patch.msgid.link/tencent_76B9DE9C9FE5C57F6D74B5149970DF8CCF0A@qq.com
Diffstat (limited to 'tools/testing/selftests/futex')
-rw-r--r--tools/testing/selftests/futex/functional/futex_requeue.c47
1 files changed, 28 insertions, 19 deletions
diff --git a/tools/testing/selftests/futex/functional/futex_requeue.c b/tools/testing/selftests/futex/functional/futex_requeue.c
index f4b8dc1eccf1..cc31f051765d 100644
--- a/tools/testing/selftests/futex/functional/futex_requeue.c
+++ b/tools/testing/selftests/futex/functional/futex_requeue.c
@@ -10,21 +10,25 @@
#include <string.h>
#include "futextest.h"
+#include "futex_thread.h"
#include "kselftest_harness.h"
-#define timeout_ns 30000000
-#define WAKE_WAIT_US 10000
+struct waiter_args {
+ struct __test_metadata *_metadata;
+ unsigned int n_threads;
+};
volatile futex_t *f1;
-void *waiterfn(void *arg)
+static int waiterfn(void *arg)
{
- struct __test_metadata *_metadata = (struct __test_metadata *)arg;
- struct timespec to;
+ struct __test_metadata *_metadata;
+ struct waiter_args *wargs = arg;
+ struct timespec to = { };
int res;
- to.tv_sec = 0;
- to.tv_nsec = timeout_ns;
+ _metadata = wargs->_metadata;
+ to.tv_sec = (wargs->n_threads + 1) * WAIT_FOR_THREAD_SECS;
res = futex_wait(f1, *f1, &to, 0);
if (res) {
@@ -32,37 +36,39 @@ void *waiterfn(void *arg)
TH_LOG("waiter failed errno %d: %s", errno, strerror(errno));
}
- return NULL;
+ return 0;
}
TEST(requeue_single)
{
+ struct waiter_args wargs = { ._metadata = _metadata, .n_threads = 1 };
+ struct futex_thread waiter;
volatile futex_t _f1 = 0;
volatile futex_t f2 = 0;
- pthread_t waiter[10];
f1 = &_f1;
/*
* Requeue a waiter from f1 to f2, and wake f2.
*/
- ASSERT_EQ(pthread_create(&waiter[0], NULL, waiterfn, _metadata), 0)
+ ASSERT_EQ(futex_thread_create(&waiter, waiterfn, &wargs), 0)
TH_LOG("pthread_create failed");
- usleep(WAKE_WAIT_US);
+ ASSERT_EQ(futex_wait_for_thread(&waiter, _metadata), 0)
+ TH_LOG("Wait for thread failed");
EXPECT_EQ(futex_cmp_requeue(f1, 0, &f2, 0, 1, 0), 1);
EXPECT_EQ(futex_wake(&f2, 1, 0), 1);
- pthread_join(waiter[0], NULL);
+ EXPECT_EQ(futex_thread_destroy(&waiter), 0);
}
TEST(requeue_multiple)
{
+ struct waiter_args wargs = { ._metadata = _metadata, .n_threads = 10 };
+ struct futex_thread waiter[10];
volatile futex_t _f1 = 0;
volatile futex_t f2 = 0;
- pthread_t waiter[10];
- int i;
f1 = &_f1;
@@ -70,18 +76,21 @@ TEST(requeue_multiple)
* Create 10 waiters at f1. At futex_requeue, wake 3 and requeue 7.
* At futex_wake, wake INT_MAX (should be exactly 7).
*/
- for (i = 0; i < 10; i++) {
- ASSERT_EQ(pthread_create(&waiter[i], NULL, waiterfn, _metadata), 0)
+ for (int i = 0; i < 10; i++) {
+ ASSERT_EQ(futex_thread_create(&waiter[i], waiterfn, &wargs), 0)
TH_LOG("pthread_create failed for waiter %d", i);
}
- usleep(WAKE_WAIT_US);
+ for (int i = 0; i < 10; i++) {
+ ASSERT_EQ(futex_wait_for_thread(&waiter[i], _metadata), 0)
+ TH_LOG("Wait for waiter thread %d failed", i);
+ }
EXPECT_EQ(futex_cmp_requeue(f1, 0, &f2, 3, 7, 0), 10);
EXPECT_EQ(futex_wake(&f2, INT_MAX, 0), 7);
- for (i = 0; i < 10; i++)
- pthread_join(waiter[i], NULL);
+ for (int i = 0; i < 10; i++)
+ EXPECT_EQ(futex_thread_destroy(&waiter[i]), 0);
}
TEST_HARNESS_MAIN