Skip to content

Commit 62e8a32

Browse files
Peter Zijlstraingomolnar
authored andcommitted
atomic, arch: Audit atomic_{read,set}()
This patch makes sure that atomic_{read,set}() are at least {READ,WRITE}_ONCE(). We already had the 'requirement' that atomic_read() should use ACCESS_ONCE(), and most archs had this, but a few were lacking. All are now converted to use READ_ONCE(). And, by a symmetry and general paranoia argument, upgrade atomic_set() to use WRITE_ONCE(). Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: james.hogan@imgtec.com Cc: linux-kernel@vger.kernel.org Cc: oleg@redhat.com Cc: will.deacon@arm.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 90fe651 commit 62e8a32

24 files changed

Lines changed: 53 additions & 53 deletions

File tree

‎arch/alpha/include/asm/atomic.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
#define ATOMIC_INIT(i) { (i) }
1818
#define ATOMIC64_INIT(i) { (i) }
1919

20-
#define atomic_read(v) ACCESS_ONCE((v)->counter)
21-
#define atomic64_read(v) ACCESS_ONCE((v)->counter)
20+
#define atomic_read(v) READ_ONCE((v)->counter)
21+
#define atomic64_read(v) READ_ONCE((v)->counter)
2222

23-
#define atomic_set(v,i) ((v)->counter = (i))
24-
#define atomic64_set(v,i) ((v)->counter = (i))
23+
#define atomic_set(v,i) WRITE_ONCE((v)->counter, (i))
24+
#define atomic64_set(v,i) WRITE_ONCE((v)->counter, (i))
2525

2626
/*
2727
* To get proper branch prediction for the main line, we must branch

‎arch/arc/include/asm/atomic.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
#include <asm/barrier.h>
1818
#include <asm/smp.h>
1919

20-
#define atomic_read(v) ((v)->counter)
20+
#define atomic_read(v) READ_ONCE((v)->counter)
2121

2222
#ifdef CONFIG_ARC_HAS_LLSC
2323

24-
#define atomic_set(v, i) (((v)->counter) = (i))
24+
#define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
2525

2626
#ifdef CONFIG_ARC_STAR_9000923308
2727

@@ -107,7 +107,7 @@ static inline int atomic_##op##_return(int i, atomic_t *v) \
107107
#ifndef CONFIG_SMP
108108

109109
/* violating atomic_xxx API locking protocol in UP for optimization sake */
110-
#define atomic_set(v, i) (((v)->counter) = (i))
110+
#define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
111111

112112
#else
113113

@@ -125,7 +125,7 @@ static inline void atomic_set(atomic_t *v, int i)
125125
unsigned long flags;
126126

127127
atomic_ops_lock(flags);
128-
v->counter = i;
128+
WRITE_ONCE(v->counter, i);
129129
atomic_ops_unlock(flags);
130130
}
131131

‎arch/arm/include/asm/atomic.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
* strex/ldrex monitor on some implementations. The reason we can use it for
2828
* atomic_set() is the clrex or dummy strex done on every exception return.
2929
*/
30-
#define atomic_read(v) ACCESS_ONCE((v)->counter)
31-
#define atomic_set(v,i) (((v)->counter) = (i))
30+
#define atomic_read(v) READ_ONCE((v)->counter)
31+
#define atomic_set(v,i) WRITE_ONCE(((v)->counter), (i))
3232

3333
#if __LINUX_ARM_ARCH__ >= 6
3434

‎arch/arm64/include/asm/atomic.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
#define ATOMIC_INIT(i) { (i) }
5555

5656
#define atomic_read(v) READ_ONCE((v)->counter)
57-
#define atomic_set(v, i) (((v)->counter) = (i))
57+
#define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
5858
#define atomic_xchg(v, new) xchg(&((v)->counter), (new))
5959
#define atomic_cmpxchg(v, old, new) cmpxchg(&((v)->counter), (old), (new))
6060

‎arch/avr32/include/asm/atomic.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
#define ATOMIC_INIT(i) { (i) }
2121

22-
#define atomic_read(v) ACCESS_ONCE((v)->counter)
23-
#define atomic_set(v, i) (((v)->counter) = i)
22+
#define atomic_read(v) READ_ONCE((v)->counter)
23+
#define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
2424

2525
#define ATOMIC_OP_RETURN(op, asm_op, asm_con) \
2626
static inline int __atomic_##op##_return(int i, atomic_t *v) \

‎arch/frv/include/asm/atomic.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
*/
3333

3434
#define ATOMIC_INIT(i) { (i) }
35-
#define atomic_read(v) ACCESS_ONCE((v)->counter)
36-
#define atomic_set(v, i) (((v)->counter) = (i))
35+
#define atomic_read(v) READ_ONCE((v)->counter)
36+
#define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
3737

3838
static inline int atomic_inc_return(atomic_t *v)
3939
{

‎arch/h8300/include/asm/atomic.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
#define ATOMIC_INIT(i) { (i) }
1313

14-
#define atomic_read(v) ACCESS_ONCE((v)->counter)
15-
#define atomic_set(v, i) (((v)->counter) = i)
14+
#define atomic_read(v) READ_ONCE((v)->counter)
15+
#define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
1616

1717
#include <linux/kernel.h>
1818

‎arch/hexagon/include/asm/atomic.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static inline void atomic_set(atomic_t *v, int new)
4848
*
4949
* Assumes all word reads on our architecture are atomic.
5050
*/
51-
#define atomic_read(v) ((v)->counter)
51+
#define atomic_read(v) READ_ONCE((v)->counter)
5252

5353
/**
5454
* atomic_xchg - atomic

‎arch/ia64/include/asm/atomic.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
#define ATOMIC_INIT(i) { (i) }
2222
#define ATOMIC64_INIT(i) { (i) }
2323

24-
#define atomic_read(v) ACCESS_ONCE((v)->counter)
25-
#define atomic64_read(v) ACCESS_ONCE((v)->counter)
24+
#define atomic_read(v) READ_ONCE((v)->counter)
25+
#define atomic64_read(v) READ_ONCE((v)->counter)
2626

27-
#define atomic_set(v,i) (((v)->counter) = (i))
28-
#define atomic64_set(v,i) (((v)->counter) = (i))
27+
#define atomic_set(v,i) WRITE_ONCE(((v)->counter), (i))
28+
#define atomic64_set(v,i) WRITE_ONCE(((v)->counter), (i))
2929

3030
#define ATOMIC_OP(op, c_op) \
3131
static __inline__ int \

‎arch/m32r/include/asm/atomic.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*
2929
* Atomically reads the value of @v.
3030
*/
31-
#define atomic_read(v) ACCESS_ONCE((v)->counter)
31+
#define atomic_read(v) READ_ONCE((v)->counter)
3232

3333
/**
3434
* atomic_set - set atomic variable
@@ -37,7 +37,7 @@
3737
*
3838
* Atomically sets the value of @v to @i.
3939
*/
40-
#define atomic_set(v,i) (((v)->counter) = (i))
40+
#define atomic_set(v,i) WRITE_ONCE(((v)->counter), (i))
4141

4242
#ifdef CONFIG_CHIP_M32700_TS1
4343
#define __ATOMIC_CLOBBER , "r4"

0 commit comments

Comments
 (0)