From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F026B27CCE0; Sun, 5 Jul 2026 19:54:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783281271; cv=none; b=Im1nNVqvtJQt3rYlzx94rxs9tAvhZexTS6bTiowrCk++pFHeC3R6IXo77+Zi54yw2UngKnIO4TYie/qRrRLUgc3XjMsOVUbDHGk7tkv5Zhpz3BXI0Bfm5hpiT7y6sn8nJMHm+Xgn7Tgd4oxShbVRnqofyOgEDhf0P0VSoRB7sQc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783281271; c=relaxed/simple; bh=v3dx3/lYe0fAHEwuGi0Cwd2Wchi3GJlf9BlHrmo8d1w=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=jJP/slJd1daTwMqiy0BlObU4TAqs1Rg8nQ4BJBB7WzCgs6lnUEMcm0iR5M0COAw0fIR2Wt7Oj2xVRioc21wygA230ChM7zbv3thDdAAwjWr68IOyg4W1II1keEjjmLOvZm1v2NEsOti5aD4DBvt/mMnG7AkdoB/b4/bsD/xI3Ww= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=4PkpCg7s; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=RfaO8G93; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="4PkpCg7s"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="RfaO8G93" Date: Sun, 05 Jul 2026 19:54:21 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1783281263; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Fq5DWF/LOxiUXCAbKhn4y7w+kr9XhHceBNdHcKVv63Q=; b=4PkpCg7sVjfIwKXAca4u+Gzvq97evfM6VR3C3dHK0wF3KYY2RqZ2ExrKhZ7ICnmfe13mlI ubze4GemStMMUO23RGSREPs7efbV891qO/KSQsATLF/kvyHP+ibnKI036evexNTQfcKu2I E7p4Vs61Rd4wFulzObdQqmJ8kS3+JGxhBPSRc5TKdu74QiAIqbiUr/9nHuwSDVEU5esxDI 2KqpG1unDByRHBnr6XsJnuu1qXaiCQ7sUQeZEzwWdjARYPrJKyXl7bBspgsKjE4RNltMCF JOBupy8IMyQIt+MSVYdLu9PQ5m8v056657FYDAVIbUZsCDTbGD8vqWSlrIbivA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1783281263; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Fq5DWF/LOxiUXCAbKhn4y7w+kr9XhHceBNdHcKVv63Q=; b=RfaO8G93DlyDMoU2fYPsrqLFeTiEvcqJ6tRasYD0kTvCy2Abzf9nmqEaGakZp3Y/QrycQz MxrfcvX5zUKYRgBA== From: "tip-bot2 for Yuwen Chen" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: locking/futex] selftests/futex: Provide thread creation and synchronization helpers Cc: Yuwen Chen , Thomas Gleixner , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <178328126192.744054.13774253277647992098.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The following commit has been merged into the locking/futex branch of tip: Commit-ID: 86620fb9d37b9f5aaae7c7cb27a173463d961cdc Gitweb: https://git.kernel.org/tip/86620fb9d37b9f5aaae7c7cb27a173463d9= 61cdc Author: Yuwen Chen AuthorDate: Mon, 18 May 2026 10:16:40 +08:00 Committer: Thomas Gleixner 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 Signed-off-by: Thomas Gleixner Link: https://patch.msgid.link/tencent_4C90BE98BBC48B38B9FBC6A95C45CF49F309@q= q.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/testin= g/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 =20 LOCAL_HDRS :=3D \ ../include/futextest.h \ - ../include/atomic.h + ../include/atomic.h \ + ../include/futex_thread.h TEST_GEN_PROGS :=3D \ futex_wait_timeout \ futex_wait_wouldblock \ diff --git a/tools/testing/selftests/futex/include/futex_thread.h b/tools/tes= ting/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 +#include +#include +#include +#include + +#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 =3D WAIT_FOR_THREAD_USECS / WAIT_THREAD_RETRIES; + char buf[80] =3D ""; + + for (int i =3D 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 =3D arg; + + t->tid =3D gettid(); + pthread_barrier_wait(&t->barrier); + t->retval =3D t->threadfn(t->arg); + return NULL; +} + +/** + * futex_wait_for_thread - Wait for the child thread to sleep in the futex c= ontext + * @t: Thread handle. + * @_metadata: Test metadata for TH_LOG() context + */ +static inline int futex_wait_for_thread(struct futex_thread *t, struct __tes= t_metadata *_metadata) +{ + char fname[80]; + FILE *fp; + int res; + + snprintf(fname, sizeof(fname), "/proc/%d/wchan", t->tid); + fp =3D fopen(fname, "r"); + if (!fp) { + /* If /proc/... is not available, sleep */ + if (errno !=3D ENOENT) + return errno; + TH_LOG("/proc/$PID/wchan not accessible, continue with sleep()"); + sleep(WAIT_FOR_THREAD_SECS); + return 0; + } + + res =3D __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 =3D 0; + t->threadfn =3D threadfn; + t->arg =3D arg; + + if (pthread_create(&t->thread, NULL, __futex_thread_fn, t) < 0) { + int ret =3D 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