From: Yuwen Chen <ywen.chen@foxmail.com>
To: ywen.chen@foxmail.com
Cc: andrealmeid@igalia.com, bigeasy@linutronix.de,
colin.i.king@gmail.com, dave@stgolabs.net, dvhart@infradead.org,
justinstitt@google.com, kernel-team@android.com,
linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
luto@mit.edu, mingo@redhat.com, morbo@google.com,
nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org,
shuah@kernel.org, tglx@linutronix.de, usama.anjum@collabora.com,
Edward Liaw <edliaw@google.com>
Subject: [PATCH v2 5/5] selftests/futex: fix the issue of abnormal test results caused by thread timing
Date: Fri, 12 Dec 2025 11:25:32 +0800 [thread overview]
Message-ID: <tencent_52B929C6A35C2BCE33F3F2BEF4BAA48A4409@qq.com> (raw)
In-Reply-To: <tencent_E8629E1FE67D7F457479179170238F07B90A@qq.com>
Fixes a race between parent and child threads in futex_requeue.
Similar to commit fbf4dec70277 ("selftests/futex: Order calls to
futex_lock_pi"), which fixed a flake in futex_lock_pi due to racing
between the parent and child threads.
The same issue can occur in the futex_requeue test, because it expects
waiterfn to make progress to futex_wait before the parent starts to
requeue. This is mitigated by the parent sleeping for WAKE_WAIT_US, but
it still fails occasionally. This can be reproduced by adding a sleep in
the waiterfn before futex_wait:
TAP version 13
1..2
not ok 1 futex_requeue simple returned: 0
not ok 2 futex_requeue simple returned: 0
not ok 3 futex_requeue many returned: 0
not ok 4 futex_requeue many returned: 0
This issue can be resolved by checking whether the child thread is in a
sleeping state. This is because when the child thread goes to sleep, it
indicates that it is waiting for the futex lock.
Fixes: 7cb5dd8e2c8c ("selftests: futex: Add futex compare requeue test")
Signed-off-by: Yuwen Chen <ywen.chen@foxmail.com>
Co-developed-by: Edward Liaw <edliaw@google.com>
Signed-off-by: Edward Liaw <edliaw@google.com>
---
.../futex/functional/futex_requeue.c | 58 ++++++++++++++++---
1 file changed, 51 insertions(+), 7 deletions(-)
diff --git a/tools/testing/selftests/futex/functional/futex_requeue.c b/tools/testing/selftests/futex/functional/futex_requeue.c
index 7a22458c7fc96..994295fac6972 100644
--- a/tools/testing/selftests/futex/functional/futex_requeue.c
+++ b/tools/testing/selftests/futex/functional/futex_requeue.c
@@ -7,6 +7,7 @@
#include <pthread.h>
#include <limits.h>
+#include <linux/compiler.h>
#include "futextest.h"
#include "../../kselftest_harness.h"
@@ -15,6 +16,7 @@
#define WAKE_WAIT_US 10000
volatile futex_t *f1;
+static pthread_barrier_t barrier;
void *waiterfn(void *arg)
{
@@ -23,28 +25,59 @@ void *waiterfn(void *arg)
to.tv_sec = 0;
to.tv_nsec = timeout_ns;
+ WRITE_ONCE(*((pid_t *)arg), gettid());
+ pthread_barrier_wait(&barrier);
+
if (futex_wait(f1, *f1, &to, 0))
printf("waiter failed errno %d\n", errno);
return NULL;
}
+static int get_thread_state(pid_t pid)
+{
+ FILE *fp;
+ char buf[80], tag[80];
+ char val = 0;
+
+ snprintf(buf, sizeof(buf), "/proc/%d/status", pid);
+ fp = fopen(buf, "r");
+ if (!fp)
+ return -1;
+
+ while (fgets(buf, sizeof(buf), fp))
+ if (fscanf(fp, "%s %c\n", tag, &val) == 2 && !strcmp(tag, "State:"))
+ break;
+
+ fclose(fp);
+ return val;
+}
+
TEST(requeue_single)
{
volatile futex_t _f1 = 0;
volatile futex_t f2 = 0;
pthread_t waiter;
- int res;
+ pid_t tids;
+ int res, state;
f1 = &_f1;
+ pthread_barrier_init(&barrier, NULL, 2);
/*
* Requeue a waiter from f1 to f2, and wake f2.
*/
- if (pthread_create(&waiter, NULL, waiterfn, NULL))
+ if (pthread_create(&waiter, NULL, waiterfn, &tids))
ksft_exit_fail_msg("pthread_create failed\n");
- usleep(WAKE_WAIT_US);
+ pthread_barrier_wait(&barrier);
+ pthread_barrier_destroy(&barrier);
+ while ((state = get_thread_state(READ_ONCE(tids))) != 'S') {
+ usleep(WAKE_WAIT_US);
+
+ if (state < 0)
+ break;
+ }
ksft_print_dbg_msg("Requeuing 1 futex from f1 to f2\n");
res = futex_cmp_requeue(f1, 0, &f2, 0, 1, 0);
@@ -71,7 +104,8 @@ TEST(requeue_multiple)
volatile futex_t _f1 = 0;
volatile futex_t f2 = 0;
pthread_t waiter[10];
- int res, i;
+ pid_t tids[10];
+ int res, i, state;
f1 = &_f1;
@@ -80,11 +114,21 @@ TEST(requeue_multiple)
* At futex_wake, wake INT_MAX (should be exactly 7).
*/
for (i = 0; i < 10; i++) {
- if (pthread_create(&waiter[i], NULL, waiterfn, NULL))
+ pthread_barrier_init(&barrier, NULL, 2);
+
+ if (pthread_create(&waiter[i], NULL, waiterfn, &tids[i]))
ksft_exit_fail_msg("pthread_create failed\n");
- }
- usleep(WAKE_WAIT_US);
+ pthread_barrier_wait(&barrier);
+ pthread_barrier_destroy(&barrier);
+
+ while ((state = get_thread_state(READ_ONCE(tids[i]))) != 'S') {
+ usleep(WAKE_WAIT_US);
+
+ if (state < 0)
+ break;
+ }
+ }
ksft_print_dbg_msg("Waking 3 futexes at f1 and requeuing 7 futexes from f1 to f2\n");
res = futex_cmp_requeue(f1, 0, &f2, 3, 7, 0);
--
2.34.1
next prev parent reply other threads:[~2025-12-12 3:25 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-12 3:21 [PATCH v2 0/5] " Yuwen Chen
2025-12-12 3:24 ` [PATCH v2 1/5] selftests/futex: reduce array declarations in the requeue_single Yuwen Chen
2025-12-16 8:38 ` Licay
2025-12-12 3:24 ` [PATCH v2 2/5] selftests/futex: use pthread_join to reclaim thread resources Yuwen Chen
2025-12-16 9:04 ` Licay
2025-12-12 3:25 ` [PATCH v2 3/5] selftests/futex: read atomic variable value with atomic_read Yuwen Chen
2025-12-16 9:14 ` Licay
2025-12-12 3:25 ` [PATCH v2 4/5] selftests/futex: introduce the declaration of atomic_t from types.h Yuwen Chen
2025-12-16 9:17 ` Licay
2025-12-12 3:25 ` Yuwen Chen [this message]
2025-12-16 9:19 ` [PATCH v2 5/5] selftests/futex: fix the issue of abnormal test results caused by thread timing Licay
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=tencent_52B929C6A35C2BCE33F3F2BEF4BAA48A4409@qq.com \
--to=ywen.chen@foxmail.com \
--cc=andrealmeid@igalia.com \
--cc=bigeasy@linutronix.de \
--cc=colin.i.king@gmail.com \
--cc=dave@stgolabs.net \
--cc=dvhart@infradead.org \
--cc=edliaw@google.com \
--cc=justinstitt@google.com \
--cc=kernel-team@android.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=luto@mit.edu \
--cc=mingo@redhat.com \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=peterz@infradead.org \
--cc=shuah@kernel.org \
--cc=tglx@linutronix.de \
--cc=usama.anjum@collabora.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®