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,
kernel test robot <lkp@intel.com>
Subject: [PATCH v2 4/5] selftests/futex: introduce the declaration of atomic_t from types.h
Date: Fri, 12 Dec 2025 11:25:19 +0800 [thread overview]
Message-ID: <tencent_FD541759D11337DCF8B2EBCE79435EF21706@qq.com> (raw)
In-Reply-To: <tencent_E8629E1FE67D7F457479179170238F07B90A@qq.com>
Introduce the header files specifically prepared for test programs
from the tools/include directory. And solve the problem that
atomic_read and atomic_set may not be safe in a multi - threaded
environment.
Signed-off-by: Yuwen Chen <ywen.chen@foxmail.com>
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202512112344.gsyPl2ag-lkp@intel.com/
---
.../selftests/futex/functional/Makefile | 2 +-
.../testing/selftests/futex/include/atomic.h | 25 +++++++++----------
2 files changed, 13 insertions(+), 14 deletions(-)
diff --git a/tools/testing/selftests/futex/functional/Makefile b/tools/testing/selftests/futex/functional/Makefile
index 490ace1f017e8..8589917a4b126 100644
--- a/tools/testing/selftests/futex/functional/Makefile
+++ b/tools/testing/selftests/futex/functional/Makefile
@@ -2,7 +2,7 @@
PKG_CONFIG ?= pkg-config
LIBNUMA_TEST = $(shell sh -c "$(PKG_CONFIG) numa --atleast-version 2.0.16 > /dev/null 2>&1 && echo SUFFICIENT || echo NO")
-INCLUDES := -I../include -I../../ $(KHDR_INCLUDES)
+INCLUDES := -I../include -I../../ $(KHDR_INCLUDES) -I../../../../include
CFLAGS := $(CFLAGS) -g -O2 -Wall -pthread -D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64 $(INCLUDES) $(KHDR_INCLUDES) -DLIBNUMA_VER_$(LIBNUMA_TEST)=1
LDLIBS := -lpthread -lrt -lnuma
diff --git a/tools/testing/selftests/futex/include/atomic.h b/tools/testing/selftests/futex/include/atomic.h
index c0dccb1b966ba..b23e1a50949a7 100644
--- a/tools/testing/selftests/futex/include/atomic.h
+++ b/tools/testing/selftests/futex/include/atomic.h
@@ -18,9 +18,8 @@
#ifndef _ATOMIC_H
#define _ATOMIC_H
-typedef struct {
- volatile int val;
-} atomic_t;
+#include <linux/types.h>
+#include <linux/compiler.h>
#define ATOMIC_INITIALIZER { 0 }
@@ -30,36 +29,36 @@ typedef struct {
* @oldval: The expected value of the futex
* @newval: The new value to try and assign the futex
*
- * Return the old value of addr->val.
+ * Return the old value of addr->counter.
*/
static inline int
atomic_cmpxchg(atomic_t *addr, int oldval, int newval)
{
- return __sync_val_compare_and_swap(&addr->val, oldval, newval);
+ return __sync_val_compare_and_swap(&addr->counter, oldval, newval);
}
/**
* atomic_inc() - Atomic incrememnt
* @addr: Address of the variable to increment
*
- * Return the new value of addr->val.
+ * Return the new value of addr->counter.
*/
static inline int
atomic_inc(atomic_t *addr)
{
- return __sync_add_and_fetch(&addr->val, 1);
+ return __sync_add_and_fetch(&addr->counter, 1);
}
/**
* atomic_dec() - Atomic decrement
* @addr: Address of the variable to decrement
*
- * Return the new value of addr-val.
+ * Return the new value of addr-counter.
*/
static inline int
atomic_dec(atomic_t *addr)
{
- return __sync_sub_and_fetch(&addr->val, 1);
+ return __sync_sub_and_fetch(&addr->counter, 1);
}
/**
@@ -67,12 +66,12 @@ atomic_dec(atomic_t *addr)
* @addr: Address of the variable to set
* @newval: New value for the atomic_t
*
- * Return the new value of addr->val.
+ * Return the new value of addr->counter.
*/
static inline int
atomic_set(atomic_t *addr, int newval)
{
- addr->val = newval;
+ WRITE_ONCE(addr->counter, newval);
return newval;
}
@@ -80,12 +79,12 @@ atomic_set(atomic_t *addr, int newval)
* atomic_read() - Atomic read
* @addr: Address of the variable to read
*
- * Return the value of addr->val.
+ * Return the value of addr->counter.
*/
static inline int
atomic_read(atomic_t *addr)
{
- return addr->val;
+ return READ_ONCE(addr->counter);
}
#endif
--
2.34.1
next prev 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 ` [PATCH v2 3/5] selftests/futex: read atomic variable value with atomic_read Yuwen Chen
2025-12-16 9:14 ` Licay
2025-12-12 3:25 ` Yuwen Chen [this message]
2025-12-16 9:17 ` [PATCH v2 4/5] selftests/futex: introduce the declaration of atomic_t from types.h 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_FD541759D11337DCF8B2EBCE79435EF21706@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=lkp@intel.com \
--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®