Skip to contentSkip to conversation

x86: Use volatile semantics for atomic_set()

RFC PATCH In review Pranith Kumar 2 participants 4 messages Sep 22, 2014 – Sep 23, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 x86: Use volatile semantics for atomic_set()

All the atomic operations have volatile semantics to prevent compiler reordering. Enforce the same semantics for atomic_set() too.

---
 arch/x86/include/asm/atomic.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/atomic.h +1 -1
index bf20c81..fa92e93 100644
@@ -36,7 +36,7 @@ static inline int atomic_read(const atomic_t *v)
  */
 static inline void atomic_set(atomic_t *v, int i)
 {
-	v->counter = i;
+	ACCESS_ONCE(v->counter) = i;
 }
 
 /**
Signature · 1 line
2.1.0
maintainer
#1 lore raw
Quoted 2 lines · Pranith Kumar

All the atomic operations have volatile semantics to prevent compiler reordering. Enforce the same semantics for atomic_set() too.

To what point? atomic_set() is typically not what you expect it to anyhow.

#2 lore raw
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Pranith Kumar

All the atomic operations have volatile semantics to prevent compiler reordering. Enforce the same semantics for atomic_set() too.

To what point? atomic_set() is typically not what you expect it to anyhow.

This is the only atomic operation which allows compiler reordering. I am not sure why this exception was made. Any ideas as to why we do this?

As I mentioned, this patch removes this inconsistency by disallowing compiler reordering.

Signature · 1 line
Pranith
maintainer
#3 lore raw
Quoted 11 lines · Pranith Kumar
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Pranith Kumar

All the atomic operations have volatile semantics to prevent compiler reordering. Enforce the same semantics for atomic_set() too.

To what point? atomic_set() is typically not what you expect it to anyhow.

This is the only atomic operation which allows compiler reordering. I am not sure why this exception was made. Any ideas as to why we do this?

Because it typically doesn't matter. And you should never use atomic_set() where it does matter.