2022-07-11 17:47:08

by Chih-En Lin

[permalink] [raw]
Subject: [PATCH] tools: Upgrade atomic_set() to use WRITE_ONCE()

Since the atomic operation in the tools directory is from the kernel
source [1]. And from the commit [2], atomic_set() is at least
WRITE_ONCE() in the kernel source. atomic_{read,set}() should be
symmetry with {READ, WRITE}_ONCE() [3]. To be synchronous with the
source, atomic_set() should use WRITE_ONCE() rather than plain access.

[1] commit da6d8567512d ("tools include: Add basic atomic.h implementation from the kernel sources")
[2] commit 62e8a3258bda ("atomic, arch: Audit atomic_{read,set}()")
[3] commit 6aa7de059173 ("locking/atomics: COCCINELLE/treewide: Convert trivial ACCESS_ONCE() patterns to READ_ONCE()/WRITE_ONCE()")

Signed-off-by: asas1asas200 <[email protected]>
Signed-off-by: Chih-En Lin <[email protected]>
---
tools/arch/x86/include/asm/atomic.h | 2 +-
tools/include/asm-generic/atomic-gcc.h | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/arch/x86/include/asm/atomic.h b/tools/arch/x86/include/asm/atomic.h
index 1f5e26aae9fc..4dcf4e9baecc 100644
--- a/tools/arch/x86/include/asm/atomic.h
+++ b/tools/arch/x86/include/asm/atomic.h
@@ -37,7 +37,7 @@ static inline int atomic_read(const atomic_t *v)
*/
static inline void atomic_set(atomic_t *v, int i)
{
- v->counter = i;
+ WRITE_ONCE(v->counter, i);
}

/**
diff --git a/tools/include/asm-generic/atomic-gcc.h b/tools/include/asm-generic/atomic-gcc.h
index 4c1966f7c77a..df84af8012e6 100644
--- a/tools/include/asm-generic/atomic-gcc.h
+++ b/tools/include/asm-generic/atomic-gcc.h
@@ -34,7 +34,7 @@ static inline int atomic_read(const atomic_t *v)
*/
static inline void atomic_set(atomic_t *v, int i)
{
- v->counter = i;
+ WRITE_ONCE(v->counter, i);
}

/**
--
2.25.1