Skip to contentSkip to conversation

x86: cmpxchg_double: Add missing memory clobber

PATCH In review Pranith Kumar 3 participants 5 messages Oct 6, 2015 – Oct 7, 2015 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: cmpxchg_double: Add missing memory clobber

We are reading from memory locations pointed to by p1 and p2 in the asm block. Add a memory clobber flag to make gcc aware of this.

---
 arch/x86/include/asm/cmpxchg.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/include/asm/cmpxchg.h +2 -1
index 4a2e5bc..3e83949 100644
@@ -214,7 +214,8 @@ extern void __add_wrong_size(void)
 		     : "=a" (__ret), "+d" (__old2),			\
 		       "+m" (*(p1)), "+m" (*(p2))			\
 		     : "i" (2 * sizeof(long)), "a" (__old1),		\
-		       "b" (__new1), "c" (__new2));			\
+		       "b" (__new1), "c" (__new2)			\
+		     : "memory");					\
 	__ret;								\
 })
 
Signature · 1 line
2.5.3
#1 lore raw
Quoted 13 lines · Pranith Kumar

We are reading from memory locations pointed to by p1 and p2 in the asm block. Add a memory clobber flag to make gcc aware of this.

---
 arch/x86/include/asm/cmpxchg.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/include/asm/cmpxchg.h +0 -0
index 4a2e5bc..3e83949 100644

arch/x86/include/asm/cmpxchg.h extern void __add_wrong_size(void) · line 220

Quoted 8 lines
arch/x86/include/asm/cmpxchg.h +2 -1
@@ -214,7 +214,8 @@ extern void __add_wrong_size(void)
 		     : "=a" (__ret), "+d" (__old2),			\
 		       "+m" (*(p1)), "+m" (*(p2))			\
 		     : "i" (2 * sizeof(long)), "a" (__old1),		\
-		       "b" (__new1), "c" (__new2));			\
+		       "b" (__new1), "c" (__new2)			\
+		     : "memory");					\
 	__ret;								\
 })

NAK. We already have the "+m" for exactly this reason; adding an explicit memory clobber should only be used to prevent movement of *other* memory operations around this one (i.e. a barrier).

-hpa

#2 lore raw
Quoted 5 lines · H. Peter Anvin

NAK. We already have the "+m" for exactly this reason; adding an explicit memory clobber should only be used to prevent movement of *other* memory operations around this one (i.e. a barrier).

OK. If that is so, can you please explain why we need it in the __raw_cmpxchg() case? I think it is a good idea to make cmpxchg() and cmpxchg_double() have similar barrier semantics.

Thanks!

Signature · 1 line
Pranith
#3 lore raw
Quoted 11 lines · Pranith Kumar
Quoted 5 lines · H. Peter Anvin

NAK. We already have the "+m" for exactly this reason; adding an explicit memory clobber should only be used to prevent movement of *other* memory operations around this one (i.e. a barrier).

OK. If that is so, can you please explain why we need it in the __raw_cmpxchg() case? I think it is a good idea to make cmpxchg() and cmpxchg_double() have similar barrier semantics.

OK, it is a bit of a mess. We use the same macros for locked operations (__cmpxchg and __sync_cmpxchg) and unlocked operations (__cmpxchg_local). For locked operations we generally want a compiler barrier, although there are exceptions. I'm wondering if it would be better to add an explicit barrier(); to the locked versions.

However, I think one of the major uses for cmpxchg_double() is for page table manipulation, and for that it isn't clear that a compiler barrier is needed nor desired.

On the other hand, perhaps all of this is false optimization and we should just add the memory clobber. The real issue is the impact on the _local variants.

-hpa

#4 lore raw
Quoted 3 lines · H. Peter Anvin

However, I think one of the major uses for cmpxchg_double() is for page table manipulation, and for that it isn't clear that a compiler barrier is needed nor desired.

See mm/slub.c, that uses cmpxchg_double() (the LOCK prefixed one) and one would expect that to also include a compiler barrier.