linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] atomic_dec_if_positive sign extension fix
@ 2007-01-14 22:55 Robert Jennings
  2007-01-14 23:09 ` Paul Mackerras
  2007-01-16 18:16 ` [PATCH][v2] " Robert Jennings
  0 siblings, 2 replies; 12+ messages in thread
From: Robert Jennings @ 2007-01-14 22:55 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: olof, paulus

Paulus,

Please apply for 2.6.20.  If an atomic counter is explicitly set to a
negative value the atomic_dec_if_positive function will decrement and
store the next smallest value in the atomic counter contrary to it's
intended operation.  

The comparison to determine if the decrement will make the result
negative is done by the "addic." operation which operates on a 64-bit
value.  Since the counter is 32-bit, we need to sign-extend before
the signed 64-bit addition and compare.  Adding 'extsw' just before
'addic.' will correct this.  

Also, I clarify the return value in the comments just to make it clear
that the value returned is always the decremented value, even if that
value is not stored back to the atomic counter.

Signed-off-by: Robert Jennings <rcj@linux.vnet.ibm.com>
---

 atomic.h |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/include/asm-powerpc/atomic.h b/include/asm-powerpc/atomic.h
index 53283e2..6238307 100644
--- a/include/asm-powerpc/atomic.h
+++ b/include/asm-powerpc/atomic.h
@@ -207,7 +207,8 @@ #define atomic_dec_and_test(v)		(atomic_
 
 /*
  * Atomically test *v and decrement if it is greater than 0.
- * The function returns the old value of *v minus 1.
+ * The function returns the old value of *v minus 1, even if 
+ * the atomic variable, v, was not decremented.
  */
 static __inline__ int atomic_dec_if_positive(atomic_t *v)
 {
@@ -216,6 +217,7 @@ static __inline__ int atomic_dec_if_posi
 	__asm__ __volatile__(
 	LWSYNC_ON_SMP
 "1:	lwarx	%0,0,%1		# atomic_dec_if_positive\n\
+	extsw	%0,%0\n\
 	addic.	%0,%0,-1\n\
 	blt-	2f\n"
 	PPC405_ERR77(0,%1)

^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2007-01-17 16:50 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-01-14 22:55 [PATCH] atomic_dec_if_positive sign extension fix Robert Jennings
2007-01-14 23:09 ` Paul Mackerras
2007-01-14 23:20   ` Olof Johansson
2007-01-14 23:32   ` Segher Boessenkool
2007-01-14 23:43     ` Segher Boessenkool
2007-01-15  8:55   ` Gabriel Paubert
2007-01-15 16:01     ` Segher Boessenkool
2007-01-16  4:28       ` Gabriel Paubert
2007-01-16  8:36         ` Segher Boessenkool
2007-01-16 18:16 ` [PATCH][v2] " Robert Jennings
2007-01-16 20:08   ` Gabriel Paubert
2007-01-17 16:50     ` [PATCH][v3] " Robert Jennings

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).