Skip to contentSkip to conversation

Xen/atomic: use static inlines instead of macros

PATCH Superseded Andrew Cooper 3 participants 7 messages Feb 21, 2014 – Feb 24, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Xen/atomic: use static inlines instead of macros

This is some coverity-inspired tidying.

Coverity has some grief analysing the call sites of atomic_read(). This is believed to be a bug in Coverity itself when expanding the nested macros, but there is no legitimate reason for it to be a macro in the first place.

This patch changes {,_}atomic_{read,set}() from being macros to being static inline functions, thus gaining some type safety.

One issue which is not immediatly obvious is that the non-atomic varients take their atomic_t at a different level of indirection to the atomic varients.

This is not suitable for _atomic_set() (when used to initialise an atomic_t) which is converted to take its parameter as a pointer. One callsite of _atomic_set() is updated, while the other two callsites are updated to ATOMIC_INIT().

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Ian Campbell <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Tim Deegan <[email protected]>

---

This is compile-tested on arm32 and 64, and functionally tested on x86_64

---
 xen/common/domain.c          |    5 ++---
 xen/include/asm-arm/atomic.h |   22 +++++++++++++++++----
 xen/include/asm-x86/atomic.h |   43 +++++++++++++++++++++++++++++++++++-------
 xen/include/xen/sched.h      |    2 +-
 4 files changed, 57 insertions(+), 15 deletions(-)
xen/common/domain.c +2 -3
index 2636fc9..a1483e4 100644
@@ -759,13 +759,12 @@ static void complete_domain_destroy(struct rcu_head *head)
 void domain_destroy(struct domain *d)
 {
     struct domain **pd;
-    atomic_t      old, new;
+    atomic_t old = ATOMIC_INIT(0);
+    atomic_t new = ATOMIC_INIT(DOMAIN_DESTROYED);
 
     BUG_ON(!d->is_dying);
 
     /* May be already destroyed, or get_domain() can race us. */
-    _atomic_set(old, 0);
-    _atomic_set(new, DOMAIN_DESTROYED);
     old = atomic_compareandswap(old, new, &d->refcnt);
     if ( _atomic_read(old) != 0 )
         return;
xen/include/asm-arm/atomic.h +18 -4
index 69c8f3f..14eacd0 100644
@@ -83,11 +83,25 @@ typedef struct { int counter; } atomic_t;
  * strex/ldrex monitor on some implementations. The reason we can use it for
  * atomic_set() is the clrex or dummy strex done on every exception return.
  */
-#define _atomic_read(v) ((v).counter)
-#define atomic_read(v)  (*(volatile int *)&(v)->counter)
+static inline int atomic_read(atomic_t *v)
+{
+    return *(volatile int *)&(v)->counter;
+}
+
+static inline int _atomic_read(atomic_t v)
+{
+    return v.counter;
+}
 
-#define _atomic_set(v,i) (((v).counter) = (i))
-#define atomic_set(v,i) (((v)->counter) = (i))
+static inline void atomic_set(atomic_t *v, int i)
+{
+    v->counter = i;
+}
+
+static inline void _atomic_set(atomic_t *v, int i)
+{
+    v->counter = i;
+}
 
 #if defined(CONFIG_ARM_32)
 # include <asm/arm32/atomic.h>
xen/include/asm-x86/atomic.h +36 -7

57 lines not on this page · raw message

xen/include/xen/sched.h +1 -1
index fb8bd36..e4b5cae 100644
@@ -468,7 +468,7 @@ static always_inline int get_domain(struct domain *d)
         old = seen;
         if ( unlikely(_atomic_read(old) & DOMAIN_DESTROYED) )
             return 0;
-        _atomic_set(new, _atomic_read(old) + 1);
+        _atomic_set(&new, _atomic_read(old) + 1);
         seen = atomic_compareandswap(old, new, &d->refcnt);
     }
     while ( unlikely(_atomic_read(seen) != _atomic_read(old)) );
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper

This is some coverity-inspired tidying.

Coverity has some grief analysing the call sites of atomic_read(). This is believed to be a bug in Coverity itself when expanding the nested macros, but
there is no legitimate reason for it to be a macro in the first place.

This patch changes {,_}atomic_{read,set}() from being macros to being static inline functions, thus gaining some type safety.

Quoted 8 lines

One issue which is not immediatly obvious is that the non-atomic varients take
their atomic_t at a different level of indirection to the atomic varients.

This is not suitable for _atomic_set() (when used to initialise an atomic_t) which is converted to take its parameter as a pointer. One callsite of _atomic_set() is updated, while the other two callsites are updated to ATOMIC_INIT().

Did you consider leaving these "non-atomic atomic ops" untouched (as they don't involve macro nesting), altering only the "real" ones?

Jan

#2 lore raw
Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper

This is some coverity-inspired tidying.

Coverity has some grief analysing the call sites of atomic_read(). This is believed to be a bug in Coverity itself when expanding the nested macros, but
there is no legitimate reason for it to be a macro in the first place.

This patch changes {,_}atomic_{read,set}() from being macros to being static inline functions, thus gaining some type safety.

One issue which is not immediatly obvious is that the non-atomic varients take
their atomic_t at a different level of indirection to the atomic varients.

Quoted 9 lines
Quoted 4 lines

This is not suitable for _atomic_set() (when used to initialise an atomic_t) which is converted to take its parameter as a pointer. One callsite of _atomic_set() is updated, while the other two callsites are updated to ATOMIC_INIT().

Did you consider leaving these "non-atomic atomic ops" untouched (as they don't involve macro nesting), altering only the "real" ones?

Jan

Yes, but for the sake of three updates at callsites, I felt the benefits outweighed the costs.

~Andrew

#3 lore raw
Quoted 21 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper

This is some coverity-inspired tidying.

Coverity has some grief analysing the call sites of atomic_read(). This is believed to be a bug in Coverity itself when expanding the nested macros, but
there is no legitimate reason for it to be a macro in the first place.

This patch changes {,_}atomic_{read,set}() from being macros to being static inline functions, thus gaining some type safety.

One issue which is not immediatly obvious is that the non-atomic varients take
their atomic_t at a different level of indirection to the atomic varients.

This is not suitable for _atomic_set() (when used to initialise an atomic_t) which is converted to take its parameter as a pointer. One callsite of _atomic_set() is updated, while the other two callsites are updated to ATOMIC_INIT().

Quoted 5 lines
Quoted 2 lines

Did you consider leaving these "non-atomic atomic ops" untouched (as they don't involve macro nesting), altering only the "real" ones?

Yes, but for the sake of three updates at callsites, I felt the benefits outweighed the costs.

Except that I don't really see much of a benefit here - the type safety argument doesn't really count all that much, considering that a wrongly used type would need to have a suitable field named "counter", which is unlikely enough to not worry much.

Jan

#4 lore raw
Quoted 23 lines · Jan Beulich
Quoted 23 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 21 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper

This is some coverity-inspired tidying.

Coverity has some grief analysing the call sites of atomic_read(). This is believed to be a bug in Coverity itself when expanding the nested macros, but
there is no legitimate reason for it to be a macro in the first place.

This patch changes {,_}atomic_{read,set}() from being macros to being static inline functions, thus gaining some type safety.

Quoted 8 lines

One issue which is not immediatly obvious is that the non-atomic varients take
their atomic_t at a different level of indirection to the atomic varients.

This is not suitable for _atomic_set() (when used to initialise an atomic_t) which is converted to take its parameter as a pointer. One callsite of _atomic_set() is updated, while the other two callsites are updated to ATOMIC_INIT().

Did you consider leaving these "non-atomic atomic ops" untouched (as they don't involve macro nesting), altering only the "real" ones?

Quoted 9 lines
Quoted 2 lines

Yes, but for the sake of three updates at callsites, I felt the benefits outweighed the costs.

Except that I don't really see much of a benefit here - the type safety argument doesn't really count all that much, considering that a wrongly used type would need to have a suitable field named "counter", which is unlikely enough to not worry much.

Jan

An error message of "Expeted atomic_t *, got <something else>" is substantially more useful than "<something> doesn't have .counter" or . being an invalid operator in context.

~Andrew

#5 lore raw
In reply to Andrew Cooper
Quoted 11 lines · Andrew Cooper

This is some coverity-inspired tidying.

Coverity has some grief analysing the call sites of atomic_read(). This is believed to be a bug in Coverity itself when expanding the nested macros, but there is no legitimate reason for it to be a macro in the first place.

This patch changes {,_}atomic_{read,set}() from being macros to being static inline functions, thus gaining some type safety.

One issue which is not immediatly obvious is that the non-atomic varients take their atomic_t at a different level of indirection to the atomic varients.

"variants" and "immediately"

Quoted 12 lines

This is not suitable for _atomic_set() (when used to initialise an atomic_t) which is converted to take its parameter as a pointer. One callsite of _atomic_set() is updated, while the other two callsites are updated to ATOMIC_INIT().

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Ian Campbell <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Tim Deegan <[email protected]>
Quoted 3 lines

---

This is compile-tested on arm32 and 64

Thanks!

xen/include/asm-arm/atomic.h typedef struct { int counter; } atomic_t; · line 104

Quoted 9 lines
xen/include/asm-arm/atomic.h +9 -0
+static inline void atomic_set(atomic_t *v, int i)
+{
+    v->counter = i;
+}
+
+static inline void _atomic_set(atomic_t *v, int i)
+{
+    v->counter = i;
+}

Are these now the same on purpose? (previously one took a pointer and the other the actual variable).

I don't have any especially strong feelings on the patch generally. If x86 is going to change then I suppose ARM might as well do so for consistency.

Ian.

#6 lore raw
Quoted 7 lines · Ian Campbell
Quoted 6 lines · Andrew Cooper

This is some coverity-inspired tidying.

Coverity has some grief analysing the call sites of atomic_read(). This is believed to be a bug in Coverity itself when expanding the nested macros, but there is no legitimate reason for it to be a macro in the first place.

Quoted 6 lines
Quoted 5 lines

This patch changes {,_}atomic_{read,set}() from being macros to being static inline functions, thus gaining some type safety.

One issue which is not immediatly obvious is that the non-atomic varients take their atomic_t at a different level of indirection to the atomic varients.

"variants" and "immediately"

Oops - I did correct these, then sent the previous patch.

Quoted 30 lines
Quoted 13 lines

This is not suitable for _atomic_set() (when used to initialise an atomic_t) which is converted to take its parameter as a pointer. One callsite of _atomic_set() is updated, while the other two callsites are updated to ATOMIC_INIT().

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Ian Campbell <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Tim Deegan <[email protected]>
Quoted 3 lines

---

This is compile-tested on arm32 and 64

Thanks!

xen/include/asm-arm/atomic.h typedef struct { int counter; } atomic_t; · line 104

Quoted 9 lines
xen/include/asm-arm/atomic.h +9 -0
+static inline void atomic_set(atomic_t *v, int i)
+{
+    v->counter = i;
+}
+
+static inline void _atomic_set(atomic_t *v, int i)
+{
+    v->counter = i;
+}

Are these now the same on purpose? (previously one took a pointer and the other the actual variable).

Quoted 8 lines

I don't have any especially strong feelings on the patch generally. If x86 is going to change then I suppose ARM might as well do so for consistency.

Ian.

_atomic_set(stack_var, val) makes it a functional noop, and crucially, leaves the caller with an untouchted atomic_t

I had to change the indirection to make x86 work, before checking arm and finding that the two were identical. They are used in common code where the difference is required in x86.

~Andrew