Skip to contentSkip to conversation

Xen/atomic: use static inlines instead of macros

PATCH In review Andrew Cooper 5 participants 6 messages Mar 28, 2014 – Apr 1, 2014

lore
Share & tools
lore
#0 lore raw

PATCH v2 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 immediately obvious is that the non-atomic variants take their atomic_t at a different level of indirection to the atomic variants.

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

v2: spelling fixes in commit message, and remove some redundant brackets

---
 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 ad8a1b6..b414a7d 100644
@@ -754,13 +754,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..cb72547 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 b9ba379..92b4846 100644
@@ -484,7 +484,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 27 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 11:55, <[email protected]> wrote:

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 immediately obvious is that the non-atomic variants take
their atomic_t at a different level of indirection to the atomic variants.

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 5 lines

---

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

v2: spelling fixes in commit message, and remove some redundant brackets

I see you left even the "non-atomic atomic ops" as inline functions, other than suggested on v1. I'll leave it to Keir to decide whether the resulting larger change is worthwhile the little benefit.

Jan

#2 lore raw
Quoted 29 lines · Jan Beulich
Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 11:55, <[email protected]> wrote:

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 immediately obvious is that the non-atomic variants take
their atomic_t at a different level of indirection to the atomic variants.

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 9 lines
Quoted 3 lines

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

v2: spelling fixes in commit message, and remove some redundant brackets

I see you left even the "non-atomic atomic ops" as inline functions, other than suggested on v1. I'll leave it to Keir to decide whether the resulting larger change is worthwhile the little benefit.

Jan

I did explain why I disagreed with leaving the non-atomic as macros. Any optimising compiler will generate the same code from either option, but the static inline functions provide much more informative error messages.

~Andrew

Reviewed-by on v2 1/1
#3 lore raw
In reply to Andrew Cooper
Quoted 12 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 immediately obvious is that the non-atomic variants take their atomic_t at a different level of indirection to the atomic variants.

Quoted 6 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().

FWIW, I approve of converting both kinds of accessors to inlines, just on general principles.

Acked-by
#4 lore raw
Quoted 20 lines · Tim Deegan
Quoted 18 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 immediately obvious is that the non-atomic variants take their atomic_t at a different level of indirection to the atomic variants.

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 7 lines
Quoted 1 line

FWIW, I approve of converting both kinds of accessors to inlines, just on general principles.

Agreed, functions are preferable to macros.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Acked-by on v2 1/1
#5 lore raw
In reply to Andrew Cooper
Quoted 12 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 immediately obvious is that the non-atomic variants take their atomic_t at a different level of indirection to the atomic variants.

Quoted 8 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]>

For the arm bits:

I am assuming that someone else will be committing.