mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yuwen Chen <ywen.chen@foxmail.com>
To: ywen.chen@foxmail.com
Cc: andrealmeid@igalia.com, bigeasy@linutronix.de,
	colin.i.king@gmail.com, dave@stgolabs.net, dvhart@infradead.org,
	justinstitt@google.com, kernel-team@android.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@linutronix.de, usama.anjum@collabora.com
Subject: [PATCH v2 3/5] selftests/futex: read atomic variable value with atomic_read
Date: Fri, 12 Dec 2025 11:25:07 +0800	[thread overview]
Message-ID: <tencent_7DA95D2C454DDCF1E381AE2EE7F7793A5F08@qq.com> (raw)
In-Reply-To: <tencent_E8629E1FE67D7F457479179170238F07B90A@qq.com>

Directly reading the internal value of atomic_t externally does
not meet the encapsulation requirements. A new function
atomic_read is added to obtain the value of an atomic variable.

Signed-off-by: Yuwen Chen <ywen.chen@foxmail.com>
---
 .../selftests/futex/functional/futex_requeue_pi.c    | 12 ++++++------
 .../functional/futex_requeue_pi_signal_restart.c     |  4 ++--
 tools/testing/selftests/futex/include/atomic.h       | 12 ++++++++++++
 3 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/tools/testing/selftests/futex/functional/futex_requeue_pi.c b/tools/testing/selftests/futex/functional/futex_requeue_pi.c
index f299d75848cd4..5d845b052891b 100644
--- a/tools/testing/selftests/futex/functional/futex_requeue_pi.c
+++ b/tools/testing/selftests/futex/functional/futex_requeue_pi.c
@@ -201,7 +201,7 @@ void *broadcast_wakerfn(void *arg)
 	int i = 0;
 
 	ksft_print_dbg_msg("Waker: waiting for waiters to block\n");
-	while (waiters_blocked.val < THREAD_MAX)
+	while (atomic_read(&waiters_blocked) < THREAD_MAX)
 		usleep(1000);
 	usleep(1000);
 
@@ -218,7 +218,7 @@ void *broadcast_wakerfn(void *arg)
 		ksft_exit_fail_msg("FUTEX_CMP_REQUEUE_PI failed\n");
 	} else if (++i < MAX_WAKE_ITERS) {
 		task_count += args->ret;
-		if (task_count < THREAD_MAX - waiters_woken.val)
+		if (task_count < THREAD_MAX - atomic_read(&waiters_woken))
 			goto continue_requeue;
 	} else {
 		ksft_exit_fail_msg("max broadcast iterations (%d) reached with %d/%d tasks woken or requeued\n",
@@ -247,13 +247,13 @@ void *signal_wakerfn(void *arg)
 	int i = 0;
 
 	ksft_print_dbg_msg("Waker: waiting for waiters to block\n");
-	while (waiters_blocked.val < THREAD_MAX)
+	while (atomic_read(&waiters_blocked) < THREAD_MAX)
 		usleep(1000);
 	usleep(1000);
 
-	while (task_count < THREAD_MAX && waiters_woken.val < THREAD_MAX) {
+	while (task_count < THREAD_MAX && atomic_read(&waiters_woken) < THREAD_MAX) {
 		ksft_print_dbg_msg("task_count: %d, waiters_woken: %d\n",
-		     task_count, waiters_woken.val);
+		     task_count, atomic_read(&waiters_woken));
 		if (args->lock) {
 			ksft_print_dbg_msg("Calling FUTEX_LOCK_PI on mutex=%x @ %p\n",
 			    f2, &f2);
@@ -293,7 +293,7 @@ void *signal_wakerfn(void *arg)
 		args->ret = task_count;
 
 	ksft_print_dbg_msg("Waker: exiting with %d\n", args->ret);
-	ksft_print_dbg_msg("Waker: waiters_woken: %d\n", waiters_woken.val);
+	ksft_print_dbg_msg("Waker: waiters_woken: %d\n", atomic_read(&waiters_woken));
 	pthread_exit((void *)&args->ret);
 }
 
diff --git a/tools/testing/selftests/futex/functional/futex_requeue_pi_signal_restart.c b/tools/testing/selftests/futex/functional/futex_requeue_pi_signal_restart.c
index e34ee0f9ebccd..29f93916b46da 100644
--- a/tools/testing/selftests/futex/functional/futex_requeue_pi_signal_restart.c
+++ b/tools/testing/selftests/futex/functional/futex_requeue_pi_signal_restart.c
@@ -70,7 +70,7 @@ int create_rt_thread(pthread_t *pth, void*(*func)(void *), void *arg,
 void handle_signal(int signo)
 {
 	ksft_print_dbg_msg("signal received %s requeue\n",
-	     requeued.val ? "after" : "prior to");
+	     atomic_read(&requeued) ? "after" : "prior to");
 }
 
 void *waiterfn(void *arg)
@@ -83,7 +83,7 @@ void *waiterfn(void *arg)
 	old_val = f1;
 	res = futex_wait_requeue_pi(&f1, old_val, &(f2), NULL,
 				    FUTEX_PRIVATE_FLAG);
-	if (!requeued.val || errno != EWOULDBLOCK) {
+	if (!atomic_read(&requeued) || errno != EWOULDBLOCK) {
 		ksft_test_result_fail("unexpected return from futex_wait_requeue_pi: %d (%s)\n",
 		     res, strerror(errno));
 		ksft_print_dbg_msg("w2:futex: %x\n", f2);
diff --git a/tools/testing/selftests/futex/include/atomic.h b/tools/testing/selftests/futex/include/atomic.h
index 428bcd921bb53..c0dccb1b966ba 100644
--- a/tools/testing/selftests/futex/include/atomic.h
+++ b/tools/testing/selftests/futex/include/atomic.h
@@ -76,4 +76,16 @@ atomic_set(atomic_t *addr, int newval)
 	return newval;
 }
 
+/**
+ * atomic_read() - Atomic read
+ * @addr:	Address of the variable to read
+ *
+ * Return the value of addr->val.
+ */
+static inline int
+atomic_read(atomic_t *addr)
+{
+	return addr->val;
+}
+
 #endif
-- 
2.34.1


  parent reply	other threads:[~2025-12-12  3:25 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-12  3:21 [PATCH v2 0/5] selftests/futex: fix the issue of abnormal test results caused by thread timing Yuwen Chen
2025-12-12  3:24 ` [PATCH v2 1/5] selftests/futex: reduce array declarations in the requeue_single Yuwen Chen
2025-12-16  8:38   ` Licay
2025-12-12  3:24 ` [PATCH v2 2/5] selftests/futex: use pthread_join to reclaim thread resources Yuwen Chen
2025-12-16  9:04   ` Licay
2025-12-12  3:25 ` Yuwen Chen [this message]
2025-12-16  9:14   ` [PATCH v2 3/5] selftests/futex: read atomic variable value with atomic_read Licay
2025-12-12  3:25 ` [PATCH v2 4/5] selftests/futex: introduce the declaration of atomic_t from types.h Yuwen Chen
2025-12-16  9:17   ` Licay
2025-12-12  3:25 ` [PATCH v2 5/5] selftests/futex: fix the issue of abnormal test results caused by thread timing Yuwen Chen
2025-12-16  9:19   ` Licay

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_7DA95D2C454DDCF1E381AE2EE7F7793A5F08@qq.com \
    --to=ywen.chen@foxmail.com \
    --cc=andrealmeid@igalia.com \
    --cc=bigeasy@linutronix.de \
    --cc=colin.i.king@gmail.com \
    --cc=dave@stgolabs.net \
    --cc=dvhart@infradead.org \
    --cc=justinstitt@google.com \
    --cc=kernel-team@android.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --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®