Commit 8e049ef0 authored by Paul Mackerras's avatar Paul Mackerras Committed by Ingo Molnar

x86: atomic64: Code atomic(64)_read and atomic(64)_set in C not CPP

Occasionally we get bugs where atomic_read or atomic_set are
used on atomic64_t variables or vice versa.  These bugs don't
generate warnings on x86 because atomic_read and atomic_set are
coded as macros rather than C functions, so we don't get any
type-checking on their arguments; similarly for atomic64_read
and atomic64_set in 64-bit kernels.

This converts them to C functions so that the arguments are
type-checked and bugs like this will get caught more easily. It
also converts atomic_cmpxchg and atomic_xchg, and
atomic64_cmpxchg and atomic64_xchg on 64-bit, so we get
type-checking on their arguments too.

Compiling a typical 64-bit x86 config, this generates no new
warnings, and the vmlinux text is 86 bytes smaller.

Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: David Howells <dhowells@redhat.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>
LKML-Reference: <alpine.LFD.2.01.0907021653030.3210@localhost.localdomain>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent 199e2378
...@@ -31,7 +31,10 @@ static inline int atomic_read(const atomic_t *v) ...@@ -31,7 +31,10 @@ static inline int atomic_read(const atomic_t *v)
* *
* Atomically sets the value of @v to @i. * Atomically sets the value of @v to @i.
*/ */
#define atomic_set(v, i) (((v)->counter) = (i)) static inline void atomic_set(atomic_t *v, int i)
{
v->counter = i;
}
/** /**
* atomic_add - add integer to atomic variable * atomic_add - add integer to atomic variable
...@@ -203,8 +206,15 @@ static inline int atomic_sub_return(int i, atomic_t *v) ...@@ -203,8 +206,15 @@ static inline int atomic_sub_return(int i, atomic_t *v)
return atomic_add_return(-i, v); return atomic_add_return(-i, v);
} }
#define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new))) static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
#define atomic_xchg(v, new) (xchg(&((v)->counter), (new))) {
return cmpxchg(&v->counter, old, new);
}
static inline int atomic_xchg(atomic_t *v, int new)
{
return xchg(&v->counter, new);
}
/** /**
* atomic_add_unless - add unless the number is already a given value * atomic_add_unless - add unless the number is already a given value
......
...@@ -18,7 +18,10 @@ ...@@ -18,7 +18,10 @@
* *
* Atomically reads the value of @v. * Atomically reads the value of @v.
*/ */
#define atomic_read(v) ((v)->counter) static inline int atomic_read(const atomic_t *v)
{
return v->counter;
}
/** /**
* atomic_set - set atomic variable * atomic_set - set atomic variable
...@@ -27,7 +30,10 @@ ...@@ -27,7 +30,10 @@
* *
* Atomically sets the value of @v to @i. * Atomically sets the value of @v to @i.
*/ */
#define atomic_set(v, i) (((v)->counter) = (i)) static inline void atomic_set(atomic_t *v, int i)
{
v->counter = i;
}
/** /**
* atomic_add - add integer to atomic variable * atomic_add - add integer to atomic variable
...@@ -192,7 +198,10 @@ static inline int atomic_sub_return(int i, atomic_t *v) ...@@ -192,7 +198,10 @@ static inline int atomic_sub_return(int i, atomic_t *v)
* Atomically reads the value of @v. * Atomically reads the value of @v.
* Doesn't imply a read memory barrier. * Doesn't imply a read memory barrier.
*/ */
#define atomic64_read(v) ((v)->counter) static inline long atomic64_read(const atomic64_t *v)
{
return v->counter;
}
/** /**
* atomic64_set - set atomic64 variable * atomic64_set - set atomic64 variable
...@@ -201,7 +210,10 @@ static inline int atomic_sub_return(int i, atomic_t *v) ...@@ -201,7 +210,10 @@ static inline int atomic_sub_return(int i, atomic_t *v)
* *
* Atomically sets the value of @v to @i. * Atomically sets the value of @v to @i.
*/ */
#define atomic64_set(v, i) (((v)->counter) = (i)) static inline void atomic64_set(atomic64_t *v, long i)
{
v->counter = i;
}
/** /**
* atomic64_add - add integer to atomic64 variable * atomic64_add - add integer to atomic64 variable
...@@ -355,11 +367,25 @@ static inline long atomic64_sub_return(long i, atomic64_t *v) ...@@ -355,11 +367,25 @@ static inline long atomic64_sub_return(long i, atomic64_t *v)
#define atomic64_inc_return(v) (atomic64_add_return(1, (v))) #define atomic64_inc_return(v) (atomic64_add_return(1, (v)))
#define atomic64_dec_return(v) (atomic64_sub_return(1, (v))) #define atomic64_dec_return(v) (atomic64_sub_return(1, (v)))
#define atomic64_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new))) static inline long atomic64_cmpxchg(atomic64_t *v, long old, long new)
#define atomic64_xchg(v, new) (xchg(&((v)->counter), new)) {
return cmpxchg(&v->counter, old, new);
}
static inline long atomic64_xchg(atomic64_t *v, long new)
{
return xchg(&v->counter, new);
}
#define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new))) static inline long atomic_cmpxchg(atomic_t *v, int old, int new)
#define atomic_xchg(v, new) (xchg(&((v)->counter), (new))) {
return cmpxchg(&v->counter, old, new);
}
static inline long atomic_xchg(atomic_t *v, int new)
{
return xchg(&v->counter, new);
}
/** /**
* atomic_add_unless - add unless the number is a given value * atomic_add_unless - add unless the number is a given value
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment