Skip to contentSkip to conversation

atomic: Put the fetching of the old value into the loop when doing atomic CAS

PATCH NAK Junru Shen 4 participants 4 messages Feb 24, 2022

Subsystems (2)
lore
Share & tools
lore

Junru Shen

author
#0 lore raw

PATCH 1/1 atomic: Put the fetching of the old value into the loop when doing atomic CAS

Put the acquisition of the expected value inside the loop to prevent an infinite loop when it does not match.

---
 arch/x86/include/asm/atomic64_64.h | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/x86/include/asm/atomic64_64.h +6 -3
index 7886d0578..3df04c44c 100644
@@ -207,9 +207,10 @@ static inline void arch_atomic64_and(s64 i, atomic64_t *v)
 
 static inline s64 arch_atomic64_fetch_and(s64 i, atomic64_t *v)
 {
-	s64 val = arch_atomic64_read(v);
+	s64 val;
 
 	do {
+		val = arch_atomic64_read(v);
 	} while (!arch_atomic64_try_cmpxchg(v, &val, val & i));
 	return val;
 }
@@ -225,9 +226,10 @@ static inline void arch_atomic64_or(s64 i, atomic64_t *v)
 
 static inline s64 arch_atomic64_fetch_or(s64 i, atomic64_t *v)
 {
-	s64 val = arch_atomic64_read(v);
+	s64 val;
 
 	do {
+		val = arch_atomic64_read(v);
 	} while (!arch_atomic64_try_cmpxchg(v, &val, val | i));
 	return val;
 }
@@ -243,9 +245,10 @@ static inline void arch_atomic64_xor(s64 i, atomic64_t *v)
 
 static inline s64 arch_atomic64_fetch_xor(s64 i, atomic64_t *v)
 {
-	s64 val = arch_atomic64_read(v);
+	s64 val;
 
 	do {
+		val = arch_atomic64_read(v);
 	} while (!arch_atomic64_try_cmpxchg(v, &val, val ^ i));
 	return val;
 }
Signature · 1 line
2.30.2
#1 lore raw

Hi,

Quoted 2 lines · Junru Shen

Put the acquisition of the expected value inside the loop to prevent an infinite loop when it does not match.

Is there a small test showing a infinite loop can really happen? If so, have you tested it?

Note that arch_atomic64_try_cmpxchg() will refresh 'val' if the previous value doesn't match the content of 'v'.

Regards,
Boqun

Quoted 48 lines
---
 arch/x86/include/asm/atomic64_64.h | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/x86/include/asm/atomic64_64.h +6 -3
index 7886d0578..3df04c44c 100644
@@ -207,9 +207,10 @@ static inline void arch_atomic64_and(s64 i, atomic64_t *v)
 
 static inline s64 arch_atomic64_fetch_and(s64 i, atomic64_t *v)
 {
-	s64 val = arch_atomic64_read(v);
+	s64 val;
 
 	do {
+		val = arch_atomic64_read(v);
 	} while (!arch_atomic64_try_cmpxchg(v, &val, val & i));
 	return val;
 }
@@ -225,9 +226,10 @@ static inline void arch_atomic64_or(s64 i, atomic64_t *v)
 
 static inline s64 arch_atomic64_fetch_or(s64 i, atomic64_t *v)
 {
-	s64 val = arch_atomic64_read(v);
+	s64 val;
 
 	do {
+		val = arch_atomic64_read(v);
 	} while (!arch_atomic64_try_cmpxchg(v, &val, val | i));
 	return val;
 }
@@ -243,9 +245,10 @@ static inline void arch_atomic64_xor(s64 i, atomic64_t *v)
 
 static inline s64 arch_atomic64_fetch_xor(s64 i, atomic64_t *v)
 {
-	s64 val = arch_atomic64_read(v);
+	s64 val;
 
 	do {
+		val = arch_atomic64_read(v);
 	} while (!arch_atomic64_try_cmpxchg(v, &val, val ^ i));
 	return val;
 }
Signature · 1 line
2.30.2
reviewer
#2 lore raw
In reply to Junru Shen
Quoted 2 lines · Junru Shen

Put the acquisition of the expected value inside the loop to prevent an infinite loop when it does not match.

I suspect you've found this by inspection, as I don't beleive this can happen. See below.

Quoted 10 lines
---
 arch/x86/include/asm/atomic64_64.h | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/x86/include/asm/atomic64_64.h +0 -0
index 7886d0578..3df04c44c 100644

arch/x86/include/asm/atomic64_64.h static inline void arch_atomic64_and(s64 i, atomic64_t *v) · line 214

Quoted 9 lines
arch/x86/include/asm/atomic64_64.h +2 -1
@@ -207,9 +207,10 @@ static inline void arch_atomic64_and(s64 i, atomic64_t *v)
 
 static inline s64 arch_atomic64_fetch_and(s64 i, atomic64_t *v)
 {
-	s64 val = arch_atomic64_read(v);
+	s64 val;
 
 	do {
+		val = arch_atomic64_read(v);
 	} while (!arch_atomic64_try_cmpxchg(v, &val, val & i));

^^^^
See this bit above? ----------------------------'

If arch_atomic64_try_cmpxchg() fails, it writes the value in memory back to this address, so it has already done the equivalent of arch_atomic64_read(v).

If you're seing this go wrong, it implies that arch_atomic64_try_cmpxchg() is being mis-compiled, so please provide an example and the disassembly.

Likewise for the other instances below.

Thanks,
Mark.

Quoted 29 lines
arch/x86/include/asm/atomic64_64.h +4 -2
 	return val;
 }
@@ -225,9 +226,10 @@ static inline void arch_atomic64_or(s64 i, atomic64_t *v)
 
 static inline s64 arch_atomic64_fetch_or(s64 i, atomic64_t *v)
 {
-	s64 val = arch_atomic64_read(v);
+	s64 val;
 
 	do {
+		val = arch_atomic64_read(v);
 	} while (!arch_atomic64_try_cmpxchg(v, &val, val | i));
 	return val;
 }
@@ -243,9 +245,10 @@ static inline void arch_atomic64_xor(s64 i, atomic64_t *v)
 
 static inline s64 arch_atomic64_fetch_xor(s64 i, atomic64_t *v)
 {
-	s64 val = arch_atomic64_read(v);
+	s64 val;
 
 	do {
+		val = arch_atomic64_read(v);
 	} while (!arch_atomic64_try_cmpxchg(v, &val, val ^ i));
 	return val;
 }
Signature · 1 line
2.30.2
maintainer NAK
#3 lore raw
In reply to Junru Shen
Quoted 1 line · Junru Shen

Put the acquisition of the expected value inside the loop to prevent an infinite loop when it does not match.

NAK, this is broken.