From: Yuwen Chen <ywen.chen@foxmail.com>
To: licayy@foxmail.com
Cc: akpm@linux-foundation.org, andrealmeid@igalia.com,
dave@stgolabs.net, dbueso@suse.de, dvhart@infradead.org,
edliaw@google.com, hpa@linux.intel.com, justinstitt@google.com,
kernel-team@android.com, licayy@outlook.com,
linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
llvm@lists.linux.dev, 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, ywen.chen@foxmail.com
Subject: Re: [PATCH 3/3] selftests/futex: fix the issue of abnormal test results caused by thread timing
Date: Mon, 15 Dec 2025 15:50:33 +0800 [thread overview]
Message-ID: <tencent_38F3843C852F8CAECF22386E81F151D7A207@qq.com> (raw)
In-Reply-To: <tencent_735CA2D4910E781E7F514A7DED34A5CE2E0A@qq.com>
on Fri, 12 Dec 2025 12:30:20 +0800, Licay wrote:
> 1. Avoid Parsing /proc
> The current approach uses get_thread_state() to read /proc/$pid/status, which isn't very reliable.
> A better way would be to have waiterfn directly signal when it's ready using atomic operations.
I haven't found a better way to check whether a process has entered the
sleep state without using the proc interface for the time being. The kernel
probably doesn't provide a system call to obtain whether a process has
entered the sleep state.
> 2. Use Atomic Counting Instead of Polling Thread State
> Before entering futex_wait, waiterfn can atomically increment a counter. The parent thread then just waits for this counter to reach the expected value.
> This is much simpler and avoids the overhead of checking /proc repeatedly.
Using atomic instructions does not guarantee that the process is waiting in futex_wait.
> 3. Use Standard Atomic Types
> Replace the custom READ_ONCE/WRITE_ONCE macros with standard <stdatomic.h> types like atomic_int.
> It's cleaner and more portable across different platforms.
Only two operations, atomic_set and atomic_read, are used here. Using
WRITE_ONCE and READ_ONCE can avoid introducing too many dependencies.
> Here's the basic idea:
> - Add a global atomic_int ready_count variable
> - In waiterfn: atomic_fetch_add(&ready_count, 1) right before futex_wait()
> - Parent thread: spin-wait until atomic_load(&ready_count) reaches the expected value
Similarly, using atomic_inc before futex_wait does not guarantee that
the thread has reached the futex_wait execution point.
Thank you very much for your reply.
prev parent reply other threads:[~2025-12-15 7:50 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-11 8:36 [PATCH 0/3] " Yuwen Chen
2025-12-11 8:38 ` [PATCH 1/3] selftests/futex: reduce array declarations in the requeue_single Yuwen Chen
2025-12-11 8:38 ` [PATCH 2/3] selftests/futex: use pthread_join to reclaim thread resources Yuwen Chen
2025-12-11 8:38 ` [PATCH 3/3] selftests/futex: fix the issue of abnormal test results caused by thread timing Yuwen Chen
2025-12-12 4:30 ` Licay
2025-12-15 7:50 ` Yuwen Chen [this message]
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_38F3843C852F8CAECF22386E81F151D7A207@qq.com \
--to=ywen.chen@foxmail.com \
--cc=akpm@linux-foundation.org \
--cc=andrealmeid@igalia.com \
--cc=dave@stgolabs.net \
--cc=dbueso@suse.de \
--cc=dvhart@infradead.org \
--cc=edliaw@google.com \
--cc=hpa@linux.intel.com \
--cc=justinstitt@google.com \
--cc=kernel-team@android.com \
--cc=licayy@foxmail.com \
--cc=licayy@outlook.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--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®