From: "tip-bot2 for Yuwen Chen" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: Yuwen Chen <ywen.chen@foxmail.com>,
Thomas Gleixner <tglx@kernel.org>,
x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: locking/futex] selftests/futex: Provide thread creation and synchronization helpers
Date: Sun, 05 Jul 2026 19:54:21 -0000 [thread overview]
Message-ID: <178328126192.744054.13774253277647992098.tip-bot2@tip-bot2> (raw)
In-Reply-To: <tencent_4C90BE98BBC48B38B9FBC6A95C45CF49F309@qq.com>
The following commit has been merged into the locking/futex branch of tip:
Commit-ID: 86620fb9d37b9f5aaae7c7cb27a173463d961cdc
Gitweb: https://git.kernel.org/tip/86620fb9d37b9f5aaae7c7cb27a173463d961cdc
Author: Yuwen Chen <ywen.chen@foxmail.com>
AuthorDate: Mon, 18 May 2026 10:16:40 +08:00
Committer: Thomas Gleixner <tglx@kernel.org>
CommitterDate: Sun, 05 Jul 2026 21:49:18 +02:00
selftests/futex: Provide thread creation and synchronization helpers
There are timing issues in the use of threads in some selftests for futexes
as the tests rely on timed waits to ensure that the other test thread[s]
reached the lock wait function in the kernel.
That "works" on halfways idle systems, but fails under load which results
in tests failing.
Provide a set of helper functions to create test threads and to wait for
them to reach the lock wait by monitoring /proc/$PID/wchan.
[ tglx: Fixup coding style, move the timeout into the helper, adapt to test
harness changes and massage change log ]
Signed-off-by: Yuwen Chen <ywen.chen@foxmail.com>
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Link: https://patch.msgid.link/tencent_4C90BE98BBC48B38B9FBC6A95C45CF49F309@qq.com
---
tools/testing/selftests/futex/functional/Makefile | 3 +-
tools/testing/selftests/futex/include/futex_thread.h | 115 ++++++++++-
2 files changed, 117 insertions(+), 1 deletion(-)
create mode 100644 tools/testing/selftests/futex/include/futex_thread.h
diff --git a/tools/testing/selftests/futex/functional/Makefile b/tools/testing/selftests/futex/functional/Makefile
index d59faf7..a03bd5a 100644
--- a/tools/testing/selftests/futex/functional/Makefile
+++ b/tools/testing/selftests/futex/functional/Makefile
@@ -11,7 +11,8 @@ endif
LOCAL_HDRS := \
../include/futextest.h \
- ../include/atomic.h
+ ../include/atomic.h \
+ ../include/futex_thread.h
TEST_GEN_PROGS := \
futex_wait_timeout \
futex_wait_wouldblock \
diff --git a/tools/testing/selftests/futex/include/futex_thread.h b/tools/testing/selftests/futex/include/futex_thread.h
new file mode 100644
index 0000000..f71b5b6
--- /dev/null
+++ b/tools/testing/selftests/futex/include/futex_thread.h
@@ -0,0 +1,115 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef _FUTEX_THREAD_H
+#define _FUTEX_THREAD_H
+#include <errno.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "kselftest_harness.h"
+
+#define USEC_PER_SEC 1000000L
+#define WAIT_FOR_THREAD_SECS 2
+#define WAIT_FOR_THREAD_USECS (WAIT_FOR_THREAD_SECS * USEC_PER_SEC)
+#define WAIT_THREAD_RETRIES 100
+
+struct futex_thread {
+ pthread_t thread;
+ pthread_barrier_t barrier;
+ pid_t tid;
+ int (*threadfn)(void *arg);
+ void *arg;
+ int retval;
+};
+
+static inline int __wait_for_thread(FILE *fp)
+{
+ unsigned int sleep_time_us = WAIT_FOR_THREAD_USECS / WAIT_THREAD_RETRIES;
+ char buf[80] = "";
+
+ for (int i = 0; i < WAIT_THREAD_RETRIES; i++) {
+ if (!fgets(buf, sizeof(buf), fp))
+ return EIO;
+ if (!strncmp(buf, "futex", 5))
+ return 0;
+ usleep(sleep_time_us);
+ rewind(fp);
+ }
+ return ETIMEDOUT;
+}
+
+static void *__futex_thread_fn(void *arg)
+{
+ struct futex_thread *t = arg;
+
+ t->tid = gettid();
+ pthread_barrier_wait(&t->barrier);
+ t->retval = t->threadfn(t->arg);
+ return NULL;
+}
+
+/**
+ * futex_wait_for_thread - Wait for the child thread to sleep in the futex context
+ * @t: Thread handle.
+ * @_metadata: Test metadata for TH_LOG() context
+ */
+static inline int futex_wait_for_thread(struct futex_thread *t, struct __test_metadata *_metadata)
+{
+ char fname[80];
+ FILE *fp;
+ int res;
+
+ snprintf(fname, sizeof(fname), "/proc/%d/wchan", t->tid);
+ fp = fopen(fname, "r");
+ if (!fp) {
+ /* If /proc/... is not available, sleep */
+ if (errno != ENOENT)
+ return errno;
+ TH_LOG("/proc/$PID/wchan not accessible, continue with sleep()");
+ sleep(WAIT_FOR_THREAD_SECS);
+ return 0;
+ }
+
+ res = __wait_for_thread(fp);
+ fclose(fp);
+ return res;
+}
+
+/**
+ * futex_thread_create - Create a new thread for testing.
+ * @t: The handle of the newly created thread.
+ * @threadfn: The new thread starts execution by invoking threadfn
+ * @arg: The parameters passed to threadfn.
+ */
+static inline int futex_thread_create(struct futex_thread *t, int (*threadfn)(void *), void *arg)
+{
+ pthread_barrier_init(&t->barrier, NULL, 2);
+
+ t->tid = 0;
+ t->threadfn = threadfn;
+ t->arg = arg;
+
+ if (pthread_create(&t->thread, NULL, __futex_thread_fn, t) < 0) {
+ int ret = errno;
+ pthread_barrier_destroy(&t->barrier);
+ return ret;
+ }
+
+ pthread_barrier_wait(&t->barrier);
+ return 0;
+}
+
+/**
+ * futex_thread_destroy - Wait for and reclaim the resources of the thread.
+ * @t: Thread handle.
+ */
+static inline int futex_thread_destroy(struct futex_thread *t)
+{
+ pthread_join(t->thread, NULL);
+ pthread_barrier_destroy(&t->barrier);
+ return t->retval;
+}
+
+#endif
next prev parent reply other threads:[~2026-07-05 19:54 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-18 2:15 [PATCH v8 0/2] selftests/futex: fix the failed futex_requeue test issue Yuwen Chen
2026-05-18 2:16 ` [PATCH v8 1/2] selftests/futex: implement the interfaces related to threads Yuwen Chen
2026-07-05 19:54 ` tip-bot2 for Yuwen Chen [this message]
2026-07-20 19:00 ` [tip: locking/futex] selftests/futex: Provide thread creation and synchronization helpers tip-bot2 for Yuwen Chen
2026-05-18 2:16 ` [PATCH v8 2/2] selftests/futex: fix the failed futex_requeue test issue Yuwen Chen
2026-05-19 2:06 ` Yuwen Chen
2026-07-05 19:54 ` [tip: locking/futex] selftests/futex: Use thread synchronization helpers instead of usleep() tip-bot2 for Yuwen Chen
2026-07-07 22:28 ` [PATCH v8 2/2] selftests/futex: fix the failed futex_requeue test issue Mark Brown
2026-07-07 23:31 ` Thomas Gleixner
2026-07-07 23:48 ` Mark Brown
2026-07-08 1:54 ` Yuwen Chen
2026-07-08 8:08 ` Thomas Gleixner
2026-07-14 17:41 ` Mark Brown
2026-07-20 19:00 ` [tip: locking/futex] selftests/futex: Use thread synchronization helpers instead of usleep() tip-bot2 for Yuwen Chen
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=178328126192.744054.13774253277647992098.tip-bot2@tip-bot2 \
--to=tip-bot2@linutronix.de \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-tip-commits@vger.kernel.org \
--cc=tglx@kernel.org \
--cc=x86@kernel.org \
--cc=ywen.chen@foxmail.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®