From nobody Sat Jun 13 19:12:13 2026 Received: from out162-62-57-87.mail.qq.com (out162-62-57-87.mail.qq.com [162.62.57.87]) (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 D27182BE7CD for ; Wed, 6 May 2026 03:25:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=162.62.57.87 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778037919; cv=none; b=f88Z7FU9s8fJuqhnUXfra70978Fkxk1rRMGFuC+H+uePxSnmZIJ7katYKz+tHbUP38UHrKkgaA2Ty6cLvEEFGR/kYi3vrdhap8PLmx+ONsqId6853xBT+loma2IWkQqyb93TEUzNNUNk+E8rbBspZsg9KsEoNCUXSotJ9nDRZs0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778037919; c=relaxed/simple; bh=KdlpfSzdLJS+ZnHAHkWjeMNR9b0/cOrGq7rjgLErlAo=; h=Message-ID:From:To:Cc:Subject:Date:In-Reply-To:References: MIME-Version; b=eyQIbzOj+aQMragc757KPJ5TkWZWKZcH5dlTXDM9VnI+f0iyes/7a/WYu8snJs8pedby3qTUJDvVoAzcQc2PjbV7eAZsXTl0HFDivg0fJoESUr3/JkoJc1WO0gzb2RWIQ+dSniHXvApS1KuGJaJrKGejHm8Whn60k49Karem9yQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=foxmail.com; spf=pass smtp.mailfrom=foxmail.com; dkim=pass (1024-bit key) header.d=foxmail.com header.i=@foxmail.com header.b=xjEALyfz; arc=none smtp.client-ip=162.62.57.87 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=foxmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=foxmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=foxmail.com header.i=@foxmail.com header.b="xjEALyfz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1778037906; bh=8NGz/HoD6RdoWzonlE7LVL6nviLzdmhAaLYAVCOwKog=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=xjEALyfzMDFcAbxKQwcoiP5kmLcit9WeEfaukI5SS7vAHLCK0xdakcGDm4Op9Ijl1 vCDuqjJt44Xx23AuT8djVMcLJmq52XO3MLCNUuodrzQc58H1kG35vHcyjclbFPvnr5 xEwerDD8KH2Ct6wFcFxHzpgWpoOWvrcwoYrhmFU4= Received: from meizu-Precision-3660.meizu.com ([112.91.84.73]) by newxmesmtplogicsvrszb51-1.qq.com (NewEsmtp) with SMTP id 64222ABF; Wed, 06 May 2026 11:25:02 +0800 X-QQ-mid: xmsmtpt1778037902t57huedkl Message-ID: X-QQ-XMAILINFO: Obm66aqhQKBkoE5tQjYNTCKi8gk+9C/GXMNqepNnI6Pxb5QG9Qm0fCjAPl/sbW XrV+2i8lGn5tdyxPvz+N/hbOrBMPxRZJFxBrZv4L7c88NIPR/o4ntUw9NDlM6zNKFCeEGKTsOzMr kEcFfND+sqdj6aSBwOnPqcIIFeYYEuMnQ2vg2g7HUCokVflIuU7De8cVQTajAVP07FqKrX45bRR2 hw6Yq1/mUSkgXi0ckwBWgGlll9pv9b0NtY/BgHjBGsrT218U4ccQ8xXwYXxmPnobZW5TLX1vR/gC v0z0Z3ndshnVqy1OtQh8+kWQJC6W//birL0xfy9tdrWVp/kXxy/0DSvhsifjtZVqlV7qPRYTiu7z KKfsFmfF1OO7U53u+7saSNEdtfBOa5UpW6JadLB/mTaFcd29sTJMtUlKvXaeZV72IjbWubas0Tyt zMPNON/OCXeB3piWRk0zeU4u9zF+eHr2QR0FVKFo4SV5lqtoHisS/Z0N8cME1Tw+BTQw9s1TdOp9 1C1n5IH0raKYNVatKPgTjqsRdZ++iIQisSruJy9+UViRME+pnimddYBydRKkACeAIdWIyqoVafTR YWmH8TFcbiQC6yfkULbd1H1wGCMLn0IRjKSWQtIUmAt5Oivn9VE7UGnDlRKgEZlY77qyJs1OvEB8 GKlax6oV/qnHeIEgll46702xn1MT8K62t6BwfFgJhViOO+Fu2flbjpcgg86DJ//VvcNCQL7XqHia 2l+0tSBFgTKhVcw5VVx2Wrj3WtSpjrR98VFMaBD522dIgu/2WftQqovohJWacXTTHSYU30Osxv0R eVy+WzDA8krh3NaDp6I7cYyizGBP8XuSz3Mbj/DoN75TKkBLlcDSjbhGYtl/ikgFT2medCSwUB4r nysi5eKz2ILt3/sgFiAvOP6JPa5/VwLNv7V0NArmAmoL/55h2HBYgPiRzDe4PsN3IZzdjBBUQtPL 7xxOnxZbaOrDpNKEN8k+CpdNa39E3Nrr196atVjrOofdHM7AMQCFipPYAOrdNNfCrWsDlDgeRuUc e/HtHPZTatSnKLzKFIIIV9Z5tGkhWd/iw2V+Lzy9Ij656d9S9lHmMeJn9vb9eYvmKgpJNRt0Sub+ 2RYVNIWbayGgxoXDGx9VvdPKRuXu9eY3nTSj0MCLmxDX0uN3HgJS+T1y+ENYgKOBvTdPf5rCvJLR sJ/A0= X-QQ-XMRINFO: Mp0Kj//9VHAxzExpfF+O8yhSrljjwrznVg== From: Yuwen Chen To: ywen.chen@foxmail.com Cc: akpm@linux-foundation.org, andrealmeid@igalia.com, bigeasy@linutronix.de, colin.i.king@gmail.com, dave@stgolabs.net, dvhart@infradead.org, edliaw@google.com, justinstitt@google.com, kernel-team@android.com, licayy@foxmail.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@kernel.org, usama.anjum@collabora.com, wakel@google.com Subject: [PATCH v6 1/2] selftests/futex: implement the interfaces related to threads Date: Wed, 6 May 2026 11:25:01 +0800 X-OQ-MSGID: <20260506032501.3984323-1-ywen.chen@foxmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" There are timing issues in the use of threads in some unit test cases for futex. A potentially good solution is to check whether the thread is in the sleep state after creating it. A file named futex_thread.h is added, in which several thread-related functions are implemented to facilitate the solution of this problem. Signed-off-by: Yuwen Chen --- .../selftests/futex/include/futex_thread.h | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 tools/testing/selftests/futex/include/futex_thread.h diff --git a/tools/testing/selftests/futex/include/futex_thread.h b/tools/t= esting/selftests/futex/include/futex_thread.h new file mode 100644 index 0000000000000..7520ea8e8f885 --- /dev/null +++ b/tools/testing/selftests/futex/include/futex_thread.h @@ -0,0 +1,109 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef _FUTEX_THREAD_H +#define _FUTEX_THREAD_H +#include +#include +#include +#include +#include + +#define USEC_PER_SEC 1000000L +#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 int __wait_for_thread(FILE *fp, int timeout_us) +{ + char buf[80] =3D ""; + int i; + + for (i =3D 0; i < WAIT_THREAD_RETRIES; i++) { + if (!fgets(buf, sizeof(buf), fp)) + return -EIO; + if (!strncmp(buf, "futex", 5)) + return 0; + usleep(timeout_us / WAIT_THREAD_RETRIES); + 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= context + * @t: Thread handle. + * @timeout_us: The timeout for waiting for the thread to enter the sleep = state. + */ +static int futex_wait_for_thread(struct futex_thread *t, int timeout_us) +{ + char fname[80]; + FILE *fp; + int res; + + snprintf(fname, sizeof(fname), "/proc/%d/wchan", t->tid); + fp =3D fopen(fname, "r"); + if (!fp) { + usleep(USEC_PER_SEC / 10); /* 100ms */ + return -EIO; + } + + res =3D __wait_for_thread(fp, timeout_us); + if (res =3D=3D -EIO) + usleep(USEC_PER_SEC / 10); /* 100ms */ + + 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 int futex_thread_create(struct futex_thread *t, int (*threadfn)(voi= d *), void *arg) +{ + int ret; + + pthread_barrier_init(&t->barrier, NULL, 2); + t->tid =3D 0; + t->threadfn =3D threadfn; + t->arg =3D arg; + + ret =3D pthread_create(&t->thread, NULL, __futex_thread_fn, t); + if (ret) + 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 int futex_thread_destroy(struct futex_thread *t) +{ + pthread_join(t->thread, NULL); + pthread_barrier_destroy(&t->barrier); + return t->retval; +} + +#endif --=20 2.34.1 From nobody Sat Jun 13 19:12:13 2026 Received: from out162-62-57-210.mail.qq.com (out162-62-57-210.mail.qq.com [162.62.57.210]) (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 B4B8A2C21F4 for ; Wed, 6 May 2026 03:25:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=162.62.57.210 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778037945; cv=none; b=Iez1+wqqDd1iD1qs7m5LyMSWJ/PBgm2txT24JxH1VhOfMgupMIBlXpl8AigENocGoSthHmA9Iie04EKL6a3mbXDE/P58sT76V7ToKJZdULt1nk6aJLZb2G2i92gqS7PdWN9QB/FPySnjyfkkl2dlXwRltyehTNn4iwuUcmBZuDc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778037945; c=relaxed/simple; bh=JSbQVp3uOsviHeOGn/YCNXJLDIgQazpWad8Ou8j2eOk=; h=Message-ID:From:To:Cc:Subject:Date:In-Reply-To:References: MIME-Version; b=p99oOHtphn6HuQho65NMkyCnNYHSdzpUa8CSMFqrUk1OAA55Zz295whFweOyz1dWe8RSZfBxCpL7h4Y6HDZXnlzyiv13+AFnW8w++NNoxtSN5jm0ZUeB3wK4Dioy7pgyNSCi3shpA3bl2kul9PYcswKyfH1QaLc9tsetcZqAWMk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=foxmail.com; spf=pass smtp.mailfrom=foxmail.com; dkim=pass (1024-bit key) header.d=foxmail.com header.i=@foxmail.com header.b=WLF7Ynt5; arc=none smtp.client-ip=162.62.57.210 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=foxmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=foxmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=foxmail.com header.i=@foxmail.com header.b="WLF7Ynt5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1778037940; bh=VO1X5lXNktXqNg9NuvRxBS3DscdbZQigmnrJ8kj5irc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=WLF7Ynt52exvivmVR8FVqU6qj6w1LqtZb7e7Fk1cESB4QSPM4/CGttFkP4F61cFaV 251W1bjCwfwWjY4IXSVL42VhwEz/wEbjVkK8mm7ALWvSfzx6rOYRZet/uL2VY269b4 DlnL3jtsBSgkSmkmEvpkwQ1FcFNFlz6Fx9PAQhRo= Received: from meizu-Precision-3660.meizu.com ([14.21.33.153]) by newxmesmtplogicsvrszb51-0.qq.com (NewE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0LynCO5KDcnV9avXeW0/ms+dYZsr2jsN/FSyrhNJMwueNL+AoA4nE5xHfZ+/e91iTbKfNexO6nA eWSsjuHPUNrr5+YC0W1I8IP2sG4SnsTYfGSRUq7cAa/2Kh1b2ju2XWp0L9q6CDsy5tF+ELlHK8JQ ml0MOPzsLbjop/gdFo/RgVfFREVkkRBWeRUPN7YI45jTiTpcfKtrvywwz2Tdrb7zIsifnzskPTh2 +0WPD+bmsgL0Ezi+DrKKzq9qCByNlKQc4eNd4= X-QQ-XMRINFO: OD9hHCdaPRBwH5bRRRw8tsiH4UAatJqXfg== From: Yuwen Chen To: ywen.chen@foxmail.com Cc: akpm@linux-foundation.org, andrealmeid@igalia.com, bigeasy@linutronix.de, colin.i.king@gmail.com, dave@stgolabs.net, dvhart@infradead.org, edliaw@google.com, justinstitt@google.com, kernel-team@android.com, licayy@foxmail.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@kernel.org, usama.anjum@collabora.com, wakel@google.com Subject: [PATCH v6 2/2] selftests/futex: fix the failed futex_requeue test issue Date: Wed, 6 May 2026 11:25:35 +0800 X-OQ-MSGID: <20260506032535.3984357-1-ywen.chen@foxmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This test item has extremely high requirements for timing and can only pass the test under specific conditions. The following situations will lead to test failure: MainThread Thread1 | pthread_create-------------------- | | futex_cmp_requeue | | futex_wait | | If the child thread is not waiting in the futex_wait function when the main thread reaches the futex_cmp_requeue function, the test will fail. An attempt is made to avoid this problem by checking whether the child thread is in a sleeping state in the main thread. Fixes: 7cb5dd8e2c8c ("selftests: futex: Add futex compare requeue test") Signed-off-by: Yuwen Chen Co-developed-by: Edward Liaw Signed-off-by: Edward Liaw --- .../futex/functional/futex_requeue.c | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/tools/testing/selftests/futex/functional/futex_requeue.c b/too= ls/testing/selftests/futex/functional/futex_requeue.c index dcf0d5f2f3122..ce10030e6f657 100644 --- a/tools/testing/selftests/futex/functional/futex_requeue.c +++ b/tools/testing/selftests/futex/functional/futex_requeue.c @@ -9,50 +9,60 @@ #include =20 #include "futextest.h" +#include "futex_thread.h" #include "kselftest_harness.h" =20 -#define timeout_ns 30000000 -#define WAKE_WAIT_US 10000 +#define FUTEX_WAIT_TIMEOUT_SECS 1 +#define WAIT_THREAD_CREATE_TIMEOUT_USECS (USEC_PER_SEC) =20 volatile futex_t *f1; =20 -void *waiterfn(void *arg) +static int waiterfn(void *arg) { struct timespec to; =20 - to.tv_sec =3D 0; - to.tv_nsec =3D timeout_ns; + to.tv_sec =3D FUTEX_WAIT_TIMEOUT_SECS; + to.tv_nsec =3D 0; =20 - if (futex_wait(f1, *f1, &to, 0)) + if (futex_wait(f1, *f1, &to, 0)) { printf("waiter failed errno %d\n", errno); + return -errno; + } =20 - return NULL; + return 0; } =20 TEST(requeue_single) { + struct futex_thread waiter; volatile futex_t _f1 =3D 0; volatile futex_t f2 =3D 0; - pthread_t waiter[10]; =20 f1 =3D &_f1; =20 /* * Requeue a waiter from f1 to f2, and wake f2. */ - ASSERT_EQ(0, pthread_create(&waiter[0], NULL, waiterfn, NULL)); + ASSERT_EQ(0, futex_thread_create(&waiter, waiterfn, NULL)); =20 - usleep(WAKE_WAIT_US); + /* + * Even if the waiting fails because the proc file system is not mounted,= this + * test item can still succeed. Therefore, there is no need to check the + * return value here. + */ + futex_wait_for_thread(&waiter, WAIT_THREAD_CREATE_TIMEOUT_USECS); =20 EXPECT_EQ(1, futex_cmp_requeue(f1, 0, &f2, 0, 1, 0)); EXPECT_EQ(1, futex_wake(&f2, 1, 0)); + + EXPECT_EQ(0, futex_thread_destroy(&waiter)); } =20 TEST(requeue_multiple) { + struct futex_thread waiter[10]; volatile futex_t _f1 =3D 0; volatile futex_t f2 =3D 0; - pthread_t waiter[10]; int i; =20 f1 =3D &_f1; @@ -62,12 +72,16 @@ TEST(requeue_multiple) * At futex_wake, wake INT_MAX (should be exactly 7). */ for (i =3D 0; i < 10; i++) - ASSERT_EQ(0, pthread_create(&waiter[i], NULL, waiterfn, NULL)); + ASSERT_EQ(0, futex_thread_create(&waiter[i], waiterfn, NULL)); =20 - usleep(WAKE_WAIT_US); + for (i =3D 0; i < 10; i++) + futex_wait_for_thread(&waiter[i], WAIT_THREAD_CREATE_TIMEOUT_USECS / 10); =20 EXPECT_EQ(10, futex_cmp_requeue(f1, 0, &f2, 3, 7, 0)); EXPECT_EQ(7, futex_wake(&f2, INT_MAX, 0)); + + for (i =3D 0; i < 10; i++) + EXPECT_EQ(0, futex_thread_destroy(&waiter[i])); } =20 TEST_HARNESS_MAIN --=20 2.34.1