Skip to contentSkip to conversation

qemu/atomic.h: add #ifdef guards for stdatomic.h

PATCH In review Alex Bennée 4 participants 9 messages Mar 26, 2020 – Mar 27, 2020 Atomic infrastructure

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qemu/atomic.h: add #ifdef guards for stdatomic.h

Deep inside the FreeBSD netmap headers we end up including stdatomic.h which clashes with qemu's atomic functions which are modelled along the C11 standard. To avoid a massive rename lets just ifdef around the problem.

---
 include/qemu/atomic.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/qemu/atomic.h +6 -0
index f9cd24c8994..ff72db51154 100644
@@ -208,11 +208,14 @@
 /* Provide shorter names for GCC atomic builtins, return old value */
 #define atomic_fetch_inc(ptr)  __atomic_fetch_add(ptr, 1, __ATOMIC_SEQ_CST)
 #define atomic_fetch_dec(ptr)  __atomic_fetch_sub(ptr, 1, __ATOMIC_SEQ_CST)
+
+#ifndef atomic_fetch_add
 #define atomic_fetch_add(ptr, n) __atomic_fetch_add(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_sub(ptr, n) __atomic_fetch_sub(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_and(ptr, n) __atomic_fetch_and(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_or(ptr, n)  __atomic_fetch_or(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_xor(ptr, n) __atomic_fetch_xor(ptr, n, __ATOMIC_SEQ_CST)
+#endif
 
 #define atomic_inc_fetch(ptr)    __atomic_add_fetch(ptr, 1, __ATOMIC_SEQ_CST)
 #define atomic_dec_fetch(ptr)    __atomic_sub_fetch(ptr, 1, __ATOMIC_SEQ_CST)
@@ -392,11 +395,14 @@
 /* Provide shorter names for GCC atomic builtins.  */
 #define atomic_fetch_inc(ptr)  __sync_fetch_and_add(ptr, 1)
 #define atomic_fetch_dec(ptr)  __sync_fetch_and_add(ptr, -1)
+
+#ifndef atomic_fetch_add
 #define atomic_fetch_add(ptr, n) __sync_fetch_and_add(ptr, n)
 #define atomic_fetch_sub(ptr, n) __sync_fetch_and_sub(ptr, n)
 #define atomic_fetch_and(ptr, n) __sync_fetch_and_and(ptr, n)
 #define atomic_fetch_or(ptr, n) __sync_fetch_and_or(ptr, n)
 #define atomic_fetch_xor(ptr, n) __sync_fetch_and_xor(ptr, n)
+#endif
 
 #define atomic_inc_fetch(ptr)  __sync_add_and_fetch(ptr, 1)
 #define atomic_dec_fetch(ptr)  __sync_add_and_fetch(ptr, -1)
Signature · 1 line
2.20.1
#1 lore raw
Quoted 17 lines · Alex Bennée

Deep inside the FreeBSD netmap headers we end up including stdatomic.h which clashes with qemu's atomic functions which are modelled along the C11 standard. To avoid a massive rename lets just ifdef around the problem.

---
 include/qemu/atomic.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/qemu/atomic.h +0 -0
index f9cd24c8994..ff72db51154 100644
@@ -208,11 +208,14 @@
 /* Provide shorter names for GCC atomic builtins, return old value */

include/qemu/atomic.h line 218

Quoted 10 lines
include/qemu/atomic.h +3 -0
 #define atomic_fetch_inc(ptr)  __atomic_fetch_add(ptr, 1, __ATOMIC_SEQ_CST)
 #define atomic_fetch_dec(ptr)  __atomic_fetch_sub(ptr, 1, __ATOMIC_SEQ_CST)
+
+#ifndef atomic_fetch_add
 #define atomic_fetch_add(ptr, n) __atomic_fetch_add(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_sub(ptr, n) __atomic_fetch_sub(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_and(ptr, n) __atomic_fetch_and(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_or(ptr, n)  __atomic_fetch_or(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_xor(ptr, n) __atomic_fetch_xor(ptr, n, __ATOMIC_SEQ_CST)
+#endif

This will work around FreeBSD's current implementation in particular, but I don't think there's anything in the C11 spec that mandates that atomic_fetch_add() and friends have to be macros and not simply functions...

thanks
-- PMM

#2 lore raw
Quoted 25 lines · Peter Maydell
Quoted 24 lines · Alex Bennée

Deep inside the FreeBSD netmap headers we end up including stdatomic.h which clashes with qemu's atomic functions which are modelled along the C11 standard. To avoid a massive rename lets just ifdef around the problem.

---
 include/qemu/atomic.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/qemu/atomic.h +2 -0
index f9cd24c8994..ff72db51154 100644
@@ -208,11 +208,14 @@
 /* Provide shorter names for GCC atomic builtins, return old value */
 #define atomic_fetch_inc(ptr)  __atomic_fetch_add(ptr, 1, __ATOMIC_SEQ_CST)
 #define atomic_fetch_dec(ptr)  __atomic_fetch_sub(ptr, 1, __ATOMIC_SEQ_CST)
+
+#ifndef atomic_fetch_add
 #define atomic_fetch_add(ptr, n) __atomic_fetch_add(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_sub(ptr, n) __atomic_fetch_sub(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_and(ptr, n) __atomic_fetch_and(ptr, n, __ATOMIC_SEQ_CST)
Quoted 8 lines

include/qemu/atomic.h line 218

Quoted 3 lines
include/qemu/atomic.h +1 -0
 #define atomic_fetch_or(ptr, n)  __atomic_fetch_or(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_xor(ptr, n) __atomic_fetch_xor(ptr, n, __ATOMIC_SEQ_CST)
+#endif

This will work around FreeBSD's current implementation in particular, but I don't think there's anything in the C11 spec that mandates that atomic_fetch_add() and friends have to be macros and not simply functions...

Sure there are two alternative options:

- Move to using stdatomic headers - on Linux they seem to be C++ only
- Rename all out atomic functions - seems a bit of a big patch for rc releases

I suspect we should look at option two for 5.1

Signature · 1 line
Alex Bennée
#3 lore raw
In reply to Peter Maydell
Quoted 4 lines · Peter Maydell
Quoted 4 lines

+#ifndef atomic_fetch_add
#define atomic_fetch_add(ptr, n) __atomic_fetch_add(ptr, n, __ATOMIC_SEQ_CST) #define atomic_fetch_sub(ptr, n) __atomic_fetch_sub(ptr, n, __ATOMIC_SEQ_CST) #define atomic_fetch_and(ptr, n) __atomic_fetch_and(ptr, n, __ATOMIC_SEQ_CST)

Quoted 8 lines
Quoted 3 lines

#define atomic_fetch_or(ptr, n) __atomic_fetch_or(ptr, n, __ATOMIC_SEQ_CST) #define atomic_fetch_xor(ptr, n) __atomic_fetch_xor(ptr, n, __ATOMIC_SEQ_CST)
+#endif

This will work around FreeBSD's current implementation in particular, but I don't think there's anything in the C11 spec that mandates that atomic_fetch_add() and friends have to be macros and not simply functions...

That's not a problem as long as they are all functions, the macros would simply override the function-based implementation.

Paolo

#4 lore raw
Quoted 15 lines · Paolo Bonzini
Quoted 4 lines · Peter Maydell
Quoted 4 lines

+#ifndef atomic_fetch_add
#define atomic_fetch_add(ptr, n) __atomic_fetch_add(ptr, n, __ATOMIC_SEQ_CST) #define atomic_fetch_sub(ptr, n) __atomic_fetch_sub(ptr, n, __ATOMIC_SEQ_CST) #define atomic_fetch_and(ptr, n) __atomic_fetch_and(ptr, n, __ATOMIC_SEQ_CST)

Quoted 8 lines
Quoted 3 lines

#define atomic_fetch_or(ptr, n) __atomic_fetch_or(ptr, n, __ATOMIC_SEQ_CST) #define atomic_fetch_xor(ptr, n) __atomic_fetch_xor(ptr, n, __ATOMIC_SEQ_CST)
+#endif

This will work around FreeBSD's current implementation in particular, but I don't think there's anything in the C11 spec that mandates that atomic_fetch_add() and friends have to be macros and not simply functions...

Quoted 2 lines

That's not a problem as long as they are all functions, the macros would simply override the function-based implementation.

Oh yes, so it would. I think I was also vaguely thinking in terms of FreeBSD being the leading edge of "one day most or all of our hosts will have a full stdatomic.h", so maybe we should shift to use-host-stdatomic-by-default, with the use of the gcc __atomic* as the fallback at some point ?

thanks
-- PMM

#5 lore raw
Quoted 16 lines · Peter Maydell
Quoted 15 lines · Paolo Bonzini
Quoted 12 lines · Peter Maydell

include/qemu/atomic.h line 218

Quoted 7 lines
include/qemu/atomic.h +2 -0
+#ifndef atomic_fetch_add
 #define atomic_fetch_add(ptr, n) __atomic_fetch_add(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_sub(ptr, n) __atomic_fetch_sub(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_and(ptr, n) __atomic_fetch_and(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_or(ptr, n)  __atomic_fetch_or(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_xor(ptr, n) __atomic_fetch_xor(ptr, n, __ATOMIC_SEQ_CST)
+#endif

This will work around FreeBSD's current implementation in particular, but I don't think there's anything in the C11 spec that mandates that atomic_fetch_add() and friends have to be macros and not simply functions...

Quoted 8 lines
Quoted 2 lines

That's not a problem as long as they are all functions, the macros would simply override the function-based implementation.

Oh yes, so it would. I think I was also vaguely thinking in terms of FreeBSD being the leading edge of "one day most or all of our hosts will have a full stdatomic.h", so maybe we should shift to use-host-stdatomic-by-default, with the use of the gcc __atomic* as the fallback at some point ?

At some point but I suspect not right now.

So what's the conclusion for this patch? Are people happy with it as a sticking plaster I can apply to the bounced testing PR?

Signature · 1 line
Alex Bennée
Reviewed-by
#6 lore raw
Quoted 28 lines · Alex Bennée
Quoted 16 lines · Peter Maydell
Quoted 15 lines · Paolo Bonzini
Quoted 12 lines · Peter Maydell

include/qemu/atomic.h line 218

Quoted 7 lines
include/qemu/atomic.h +2 -0
+#ifndef atomic_fetch_add
 #define atomic_fetch_add(ptr, n) __atomic_fetch_add(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_sub(ptr, n) __atomic_fetch_sub(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_and(ptr, n) __atomic_fetch_and(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_or(ptr, n)  __atomic_fetch_or(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_xor(ptr, n) __atomic_fetch_xor(ptr, n, __ATOMIC_SEQ_CST)
+#endif

This will work around FreeBSD's current implementation in particular, but I don't think there's anything in the C11 spec that mandates that atomic_fetch_add() and friends have to be macros and not simply functions...

Quoted 8 lines
Quoted 2 lines

That's not a problem as long as they are all functions, the macros would simply override the function-based implementation.

Oh yes, so it would. I think I was also vaguely thinking in terms of FreeBSD being the leading edge of "one day most or all of our hosts will have a full stdatomic.h", so maybe we should shift to use-host-stdatomic-by-default, with the use of the gcc __atomic* as the fallback at some point ?

Quoted 5 lines

At some point but I suspect not right now.

So what's the conclusion for this patch? Are people happy with it as a sticking plaster I can apply to the bounced testing PR?

I guess I'm happy with it. Have a

r~

#7 lore raw
In reply to Alex Bennée

include/qemu/atomic.h line 218

Quoted 8 lines · Alex Bennée
include/qemu/atomic.h +3 -0
+
+#ifndef atomic_fetch_add
 #define atomic_fetch_add(ptr, n) __atomic_fetch_add(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_sub(ptr, n) __atomic_fetch_sub(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_and(ptr, n) __atomic_fetch_and(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_or(ptr, n)  __atomic_fetch_or(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_xor(ptr, n) __atomic_fetch_xor(ptr, n, __ATOMIC_SEQ_CST)
+#endif

Do we really get sequential consistency from <stdatomic.h>? Should we not in fact #undef as a workaround instead?

r~

#8 lore raw
Quoted 9 lines · Richard Henderson

include/qemu/atomic.h line 218

Quoted 8 lines
include/qemu/atomic.h +3 -0
+
+#ifndef atomic_fetch_add
 #define atomic_fetch_add(ptr, n) __atomic_fetch_add(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_sub(ptr, n) __atomic_fetch_sub(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_and(ptr, n) __atomic_fetch_and(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_or(ptr, n)  __atomic_fetch_or(ptr, n, __ATOMIC_SEQ_CST)
 #define atomic_fetch_xor(ptr, n) __atomic_fetch_xor(ptr, n, __ATOMIC_SEQ_CST)
+#endif

Do we really get sequential consistency from <stdatomic.h>?

Yes, it's the default value (to pass a memory order you need atomic_fetch_*_explicit).

Paolo

Quoted 1 line

Should we not in fact #undef as a workaround instead?