2006-11-30 21:17:10

by Mathieu Desnoyers

[permalink] [raw]
Subject: [PATCH] atomic_cmpxchg return type error

Hi,

I just noticed that a atomic_cmpxchg, that would be given an atomic64_t
parameter, would cast the return value as a (int). In the typical use of this
primitive, the result would be that the 32 MSB would be lost when comparing
against the original value. It also affects atomic_add_unless. Note that there
is no atomic64_cmpxchg nor atomic64_add_unless, which might make things a
little clearer.

Here is a quick fix for this against 2.6.18.

Regards,

Mathieu

-- BEGIN --
--- a/include/asm-x86_64/atomic.h
+++ b/include/asm-x86_64/atomic.h
@@ -388,7 +388,8 @@ static __inline__ long atomic64_sub_retu
#define atomic64_inc_return(v) (atomic64_add_return(1,v))
#define atomic64_dec_return(v) (atomic64_sub_return(1,v))

-#define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
+#define atomic_cmpxchg(v, old, new) \
+ ((__typeof__((v)->counter))cmpxchg(&((v)->counter), old, new))
#define atomic_xchg(v, new) (xchg(&((v)->counter), new))

/**
@@ -402,7 +403,7 @@ #define atomic_xchg(v, new) (xchg(&((v)-
*/
#define atomic_add_unless(v, a, u) \
({ \
- int c, old; \
+ __typeof__((v)->counter) c, old; \
c = atomic_read(v); \
for (;;) { \
if (unlikely(c == (u))) \
-- END --


OpenPGP public key: http://krystal.dyndns.org:8080/key/compudj.gpg
Key fingerprint: 8CD5 52C3 8E3C 4140 715F BA06 3F25 A8FE 3BAE 9A68


2006-11-30 23:23:57

by Andi Kleen

[permalink] [raw]
Subject: Re: [PATCH] atomic_cmpxchg return type error

On Thu, Nov 30, 2006 at 04:17:06PM -0500, Mathieu Desnoyers wrote:
> Hi,
>
> I just noticed that a atomic_cmpxchg, that would be given an atomic64_t
> parameter, would cast the return value as a (int). In the typical use of this
> primitive, the result would be that the 32 MSB would be lost when comparing
> against the original value. It also affects atomic_add_unless. Note that there
> is no atomic64_cmpxchg nor atomic64_add_unless, which might make things a
> little clearer.

Normally you're supposed to only use atomic64_* with atomic64_t

While it works for most by accident on x86 to just use atomic_*
with atomic64_t it's not portable to other architectures.

If you want to use those with atomic64_t suitable macros would need
to be added on all architectures.

-Andi