From nobody Mon May 25 05:56:02 2026 Received: from out203-205-221-231.mail.qq.com (out203-205-221-231.mail.qq.com [203.205.221.231]) (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 593032D2385 for ; Mon, 18 May 2026 02:16:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=203.205.221.231 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779070611; cv=none; b=gP+hnJrnDqbnC1TbPYkiRnTOntqJvD/xYtkfBSnsVMJhuA/yFBOWM2rWfsaM38GciD7GWBIFec5ek0qKV3CeVAu0BgiO1r/BG0WyxGPLbvCtTpE2RNXz/74HPhNFYxRIJp4ZNj/01w8CJ+wh1cbXAG2kIJj9bI9HnczEAccVGzc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779070611; c=relaxed/simple; bh=D2XPhB4t7SdSvpZyS8CbT4SxHBam6QagXIpE0qa8t7Q=; h=Message-ID:From:To:Cc:Subject:Date:In-Reply-To:References: MIME-Version; b=CjXalvfnV1FG/iaoSgfPkNwbglwCTL4KuodUSVTZA90qQxPoNyOhrV86ZWUUaCRuGr2T8sd1K9xe9Za6MoG8BwpEYBXquwlUdprfbAdEHj5LxqAnS4S19G3PfxzgXEyb1ON7nJT3w/mvjCO8Bzg8x+GQ+PrzqduTOf/TIBuBGZg= 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=lnHqXfbY; arc=none smtp.client-ip=203.205.221.231 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="lnHqXfbY" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1779070605; bh=3Em7e9AKHtqBdVL7JeC4OerFIv/XC0EWeNGtGktKRrs=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=lnHqXfbYOeBk8CFjGbfhz5QyLhmzdUXaew/KEoD680t82FoLq08UgVKRtVva0D9RN cL38QTNj10TLW1Wzdq/btxZBvENFldfO31eNGBbiLovu9zlPhRjGibFdFZSJaKx3xH DEf7e4Gjbh3x+Hcbnl1r47AY8Sd+UZMK7tV5ysP4= Received: from meizu-Precision-3660.meizu.com ([112.91.84.73]) by newxmesmtplogicsvrsza63-0.qq.com (NewEsmtp) with SMTP id 429A827F; Mon, 18 May 2026 10:16:41 +0800 X-QQ-mid: xmsmtpt1779070601tkhm5oe5u Message-ID: X-QQ-XMAILINFO: OTdDBJCpIhk9pa2jx3Og2KXRtYXRrN/+P1oqYyvfkGzg2BYKFa/P8A4DgStuTo l0pCaLM3anLEIGHcHPXCUIxYDPEOkBofPFMU6zD29pDREIT0m2edOsEYvm66k6bakHWxQGt8bL05 IgHXMeFR5zzwkHYMBYEIX9YB5ZbiRGtqk4E8KJVhgCRXKVpN1kJ14flMs0xwROM+wkjT9t48p0gG MVEfG7VIVlRyXLgdRIZxRcSGtjCsN3B93oXaNb+34lgIcQcIfztGNqTBHPZreanFXGFfmc+FBqjb xmwckCP8qrktUoaeH5DYXVXPwYJKELtztWnxKpGnhCyHigivpJ9oOvDlBv8vC/Bn4C2yIllxVo3t e075OPUzDUkxUGQB4B6pVkQV90FdVzAsxpPq0dKZ9q5t2UNuonQT1RPprGpTwj9MGtwhtHYM+aho FxNwnLcnXiogCUnpucqpXpRD5Q7sMyGlb0EEJVNlOF29IjVYUbSm1sERQAOXufMj1qtlLp4Bbp58 NyFt/v6yEm44qq9vHjeb6vJNi7flogjZplpv3tDZChVt38FGaxL0iiZrGbR84No+igX5N7qyhbs6 iY7/eCfHGs3g60wsgwi8e2TddGdfLMH6H4t2yZC5WJPTTtX1UjgxD8GyRdPxSRc+t4xwvoRLnV9O JAmxugMa0YTXOAIcZPku+liLZdB/MWtRvTnBAkihazDonw+I4Uwhmnkm/tDV9SpW99MjIbtg+c9z hgpk76qE53bKyIZBCJ4Rx/5Wwrj6eqy4Ft9dvFt0uGIEuUal7qCnRuBwcOH7b12zTNPMf0oTuk/0 7+EKTYK2RynsrjELEpG19sUsiS3y6j7itnFaphS9GqOcOghZwb7cBuHNaMUx6lbI4Dsbmeh24iZL PWh95vmXHlOwfmr/gICBam8hg0G9INByNeCT9b4c4qPB3+YeMx0eaxn4SHCyHKc53HcmH3ZTWeZq LselztCQqz9vzAMn3Ep2cgvQBSX/wCaUPT+xd2miL4M7fepsVxzFM43OrXoEmK6UchOsNXc6/Fvb LIriNJaWGpwMcEX7drJtrcrjKthCj8T4HQrOw/cDgKVrDv0ObxEWBcmWlVCfQhFsZjTbzaWAVL98 UxEgDiGfoktrZKB3E7kDgDmsbRw/2OYJlwmUvAOCByUOp8LCon4zu0AnwatTFJ3j/5GJpI X-QQ-XMRINFO: M/715EihBoGS47X28/vv4NpnfpeBLnr4Qg== 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 v8 1/2] selftests/futex: implement the interfaces related to threads Date: Mon, 18 May 2026 10:16:40 +0800 X-OQ-MSGID: <20260518021640.283725-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/functional/Makefile | 3 +- .../selftests/futex/include/futex_thread.h | 114 ++++++++++++++++++ 2 files changed, 116 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/test= ing/selftests/futex/functional/Makefile index 5c1c824f97400..724e8216e41b1 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/t= esting/selftests/futex/include/futex_thread.h new file mode 100644 index 0000000000000..49ae687012fe5 --- /dev/null +++ b/tools/testing/selftests/futex/include/futex_thread.h @@ -0,0 +1,114 @@ +/* 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 inline int __wait_for_thread(FILE *fp, int timeout_us) +{ + char buf[80] =3D ""; + int i; + int sleep_time_us =3D timeout_us / WAIT_THREAD_RETRIES; + + if (sleep_time_us <=3D 0) + sleep_time_us =3D 1; + + 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(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= context + * @t: Thread handle. + * @timeout_us: The timeout for waiting for the thread to enter the sleep = state. + */ +static inline int futex_wait_for_thread(struct futex_thread *t, int timeou= t_us) +{ + char fname[80]; + FILE *fp; + int res; + + snprintf(fname, sizeof(fname), "/proc/%d/wchan", t->tid); + fp =3D fopen(fname, "r"); + if (!fp) { + return -EIO; + } + + res =3D __wait_for_thread(fp, timeout_us); + 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 (*thread= fn)(void *), void *arg) +{ + int ret; + + ret =3D pthread_barrier_init(&t->barrier, NULL, 2); + if (ret) + return ret; + + 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) { + 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 --=20 2.34.1 From nobody Mon May 25 05:56:02 2026 Received: from out203-205-221-245.mail.qq.com (out203-205-221-245.mail.qq.com [203.205.221.245]) (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 B03B82D978A for ; Mon, 18 May 2026 02:18:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=203.205.221.245 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779070704; cv=none; b=a8MMys1UbTbungkBS+CWcmAaOkDi/SrRvMJF2d8bTs2dGqdRv1/O8jbJpXSticNgDhiwJ6EVpxD6U+4aInVCydQibHdA9oMu76SiUc04+gESvCY9youYbDiRkKABIOdB1IjDL7mfTy/s/7yle0+c6ViE0btNNlK5xjbVf++IZRg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779070704; c=relaxed/simple; bh=kiimDOy8K1RcrH9fCFwQSpDdZgdjEdQlY9HTMWH0EE0=; h=Message-ID:From:To:Cc:Subject:Date:In-Reply-To:References: MIME-Version; b=CO9fVGanNmvIOnHFoGJTLPcKfFotjq4AWDY+vt/o0bPsMtweLDuJQVCaUA0NT39ekBLfpUUyF4/Dw3V+kUh98EWkXzewsXYSenBgcoRskH9ydG+qfTiO3Ai2QF4sGG/SsxsgWO6463usN1DcFua7F2SR3O+aa7cfTQyepuSMY00= 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=fwlEqmT+; arc=none smtp.client-ip=203.205.221.245 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="fwlEqmT+" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1779070695; bh=tqOhEFF8B+3EbZ5NKyKKRj1/WAHZTcK7PwtfwJZfUmI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=fwlEqmT+SIB8FDt25RMfY1BdJafoUrejeajDIGhvH13+vXslkY88O8V5b78oH+Isz nlvIDDVehoyzaADUUXNnHCYkHrod/5idg5PxsRa9vwIpIEtQjki7JdSVJhvt/F9lDR i4iOnFaIHENmKG8daIu9NOTCG4Kt6B1IDYgYreWc= Received: from meizu-Precision-3660.meizu.com ([14.21.33.153]) by newxmesmtplogicsvrszb51-1.qq.com (NewEsmtp) with SMTP id 43793282; Mon, 18 May 2026 10:16:55 +0800 X-QQ-mid: xmsmtpt1779070615tezs8qgj0 Message-ID: X-QQ-XMAILINFO: NiqzJuKCE4mBboAVIOjc0NuyQBpJURSEaxMyRgKdQlDgCkQjlabBkEdDf22Cut tWoxO9G4tBjU8I/PAFRsQ1DawSMIKiBaA1FC9FBkXbFrgQ5hkP8dP1i7GoU1og9cW/nNeJ0NasGi gytuMNkMEwx7GVIowVOA3MHzIWcUVC0g+QRtqtsLKp0KmT1NmghnR/CEdBOEElWOvdaj9NvAaPMd dujnIxbfjfbieUhF5BHkt82H0VGcuyQlt0xpUpojLoaX5Q3G+LjL3JtKG/o/JVAWObMhaUtrGdMq uBdoNh2E+0ksyLIQLSzska1jKS3I+WbdMIlB3CK9Ec/DyBVGpVSpo4km8ZCm3at1fmolX2jgBIfL AZODqhZJIFYsyXrvpoSCh1hENSDT21ITXevsvxjBNLG4llALmNKj8gTbDBqelEPrbzL0crHL+IxE v+cHdMWE0Lh3qY0i1aDyRXEYuQPosIabZqaWEC72R0QoxbzKe7wLqQFUL3xCrfzCtr11xz9L2Zfy jiddlzjQKpF2tYteCpBIKeKoF3rFfKoygZSi93QKiubpx45HVlvrOzlcchCdMHscZjRj9zfvTFZ3 xB3TQOYtmTIUr2V1/7+pEDmCNa3uWhcelBW0/Rgze7Ics6cF3wVXFrvr0fOD+x84XhWTLkmOqYKw Wz4YisN8upTsV8SxCGuI8V+ompP+CNSRjCrsdr8DCQpfxCzAT4zYaJW8vAji4raw82Yngm61Iv63 8ps86t+mOLA+jdkS9TeAm62zDuc7qqMruAZo0e28k4RZCuVvTCPQS0gWQUACOzuqQ7N+SmR5Tpc5 H+kN+OTRJWEIlT3VNTFvHxzb2wtM+j/sdq7WzzgRdstWcEXaVJWOvTSjYrKaf0IHhOX6gnOgCpnx xYKEs7CCFHSCFEFTWJ2fHlFsC8/mQSfbakBWw+jV+Rh2CS7754HQ+vqAOpK5AgK9WM4MpCTd4Q3A GFwo1LXpzarfafmnvk6WoVkUy4B03zdiWQntc7R+tp/xTWDmW0treHtGbY2TN0H7d40Ug3GIUe9e OAHkjR1gm11AesuU2mpEeDdGT1HZazMfRRWeGPKny99dgLTcm0Sx39dSWXe4NxUxuKhnJFY+R8B/ sCJhp9DYI6V8Rn4QXPEohzAP4LHP55KPe+WDeJlNtWJOvv1IcZwlQM2JgD0VBPKpdibUCl72uzfC 7Gqs8= X-QQ-XMRINFO: NI4Ajvh11aEjEMj13RCX7UuhPEoou2bs1g== 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 v8 2/2] selftests/futex: fix the failed futex_requeue test issue Date: Mon, 18 May 2026 10:16:54 +0800 X-OQ-MSGID: <20260518021654.283747-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 --- v7->v8: 1. Increase FUTEX_WAIT_TIMEOUT_SECS to 2 seconds to avoid timeouts. .../futex/functional/futex_requeue.c | 46 +++++++++++++------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/tools/testing/selftests/futex/functional/futex_requeue.c b/too= ls/testing/selftests/futex/functional/futex_requeue.c index dcf0d5f2f3122..5c849ccf16e26 100644 --- a/tools/testing/selftests/futex/functional/futex_requeue.c +++ b/tools/testing/selftests/futex/functional/futex_requeue.c @@ -9,51 +9,59 @@ #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 2 +#define WAIT_THREAD_CREATE_TIMEOUT_USECS (USEC_PER_SEC / 2) /* 500ms */ =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]; + int ret; =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); + ret =3D futex_wait_for_thread(&waiter, WAIT_THREAD_CREATE_TIMEOUT_USECS); + if (ret < 0) + usleep(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; + int i, ret; =20 f1 =3D &_f1; =20 @@ -62,12 +70,22 @@ 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++) { + ret =3D futex_wait_for_thread(&waiter[i], WAIT_THREAD_CREATE_TIMEOUT_USE= CS / 10); + if (ret < 0) { + /* /proc not available, give all threads time to enter futex wait */ + usleep(WAIT_THREAD_CREATE_TIMEOUT_USECS); + break; + } + } =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