* [PATCH,SPARC] make sparc32 arch_write_unlock() match the sparc64 version
@ 2011-08-15 11:09 Mikael Pettersson
2011-08-15 13:31 ` Josip Rodin
0 siblings, 1 reply; 4+ messages in thread
From: Mikael Pettersson @ 2011-08-15 11:09 UTC (permalink / raw)
To: sparclinux; +Cc: linux-kernel
The sparc32 version of arch_write_unlock() is just a plain assignment.
Unfortunately this allows the compiler to schedule side-effects in a
protected region to occur after the HW-level unlock, which is broken.
E.g., the following trivial test case gets miscompiled:
#include <linux/spinlock.h>
rwlock_t lock;
int counter;
void foo(void) { write_lock(&lock); ++counter; write_unlock(&lock); }
Fixed by adding a compiler memory barrier to arch_write_unlock(). The
sparc64 version combines the barrier and assignment into a single asm(),
so that's what I did here as well.
Compiled-tested with a sparc32 SMP kernel.
Signed-off-by: Mikael Pettersson <mikpe@it.uu.se>
---
--- linux-3.1-rc2/arch/sparc/include/asm/spinlock_32.h.~1~ 2011-07-22 12:01:08.000000000 +0200
+++ linux-3.1-rc2/arch/sparc/include/asm/spinlock_32.h 2011-08-15 11:43:49.000000000 +0200
@@ -131,6 +131,15 @@ static inline void arch_write_lock(arch_
*(volatile __u32 *)&lp->lock = ~0U;
}
+static void inline arch_write_unlock(arch_rwlock_t *lock)
+{
+ __asm__ __volatile__(
+" st %%g0, [%0]"
+ : /* no outputs */
+ : "r" (lock)
+ : "memory");
+}
+
static inline int arch_write_trylock(arch_rwlock_t *rw)
{
unsigned int val;
@@ -175,7 +184,7 @@ static inline int __arch_read_trylock(ar
res; \
})
-#define arch_write_unlock(rw) do { (rw)->lock = 0; } while(0)
+#define arch_write_unlock(rw) arch_write_unlock(rw)
#define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
#define arch_read_lock_flags(rw, flags) arch_read_lock(rw)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH,SPARC] make sparc32 arch_write_unlock() match the sparc64 version
2011-08-15 11:09 [PATCH,SPARC] make sparc32 arch_write_unlock() match the sparc64 version Mikael Pettersson
@ 2011-08-15 13:31 ` Josip Rodin
2011-08-15 14:32 ` Mikael Pettersson
0 siblings, 1 reply; 4+ messages in thread
From: Josip Rodin @ 2011-08-15 13:31 UTC (permalink / raw)
To: Mikael Pettersson; +Cc: sparclinux, linux-kernel
On Mon, Aug 15, 2011 at 01:09:09PM +0200, Mikael Pettersson wrote:
> The sparc32 version of arch_write_unlock() is just a plain assignment.
> Unfortunately this allows the compiler to schedule side-effects in a
> protected region to occur after the HW-level unlock, which is broken.
> E.g., the following trivial test case gets miscompiled:
>
> #include <linux/spinlock.h>
> rwlock_t lock;
> int counter;
> void foo(void) { write_lock(&lock); ++counter; write_unlock(&lock); }
>
> Fixed by adding a compiler memory barrier to arch_write_unlock(). The
> sparc64 version combines the barrier and assignment into a single asm(),
> so that's what I did here as well.
>
> Compiled-tested with a sparc32 SMP kernel.
>
> Signed-off-by: Mikael Pettersson <mikpe@it.uu.se>
> ---
> --- linux-3.1-rc2/arch/sparc/include/asm/spinlock_32.h.~1~ 2011-07-22 12:01:08.000000000 +0200
> +++ linux-3.1-rc2/arch/sparc/include/asm/spinlock_32.h 2011-08-15 11:43:49.000000000 +0200
> @@ -131,6 +131,15 @@ static inline void arch_write_lock(arch_
> *(volatile __u32 *)&lp->lock = ~0U;
> }
>
> +static void inline arch_write_unlock(arch_rwlock_t *lock)
> +{
> + __asm__ __volatile__(
> +" st %%g0, [%0]"
> + : /* no outputs */
> + : "r" (lock)
> + : "memory");
> +}
> +
> static inline int arch_write_trylock(arch_rwlock_t *rw)
> {
> unsigned int val;
> @@ -175,7 +184,7 @@ static inline int __arch_read_trylock(ar
> res; \
> })
>
> -#define arch_write_unlock(rw) do { (rw)->lock = 0; } while(0)
> +#define arch_write_unlock(rw) arch_write_unlock(rw)
>
> #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
> #define arch_read_lock_flags(rw, flags) arch_read_lock(rw)
Why keep the tautological define? Just wondering.
--
2. That which causes joy or happiness.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH,SPARC] make sparc32 arch_write_unlock() match the sparc64 version
2011-08-15 13:31 ` Josip Rodin
@ 2011-08-15 14:32 ` Mikael Pettersson
2011-08-15 15:24 ` Sam Ravnborg
0 siblings, 1 reply; 4+ messages in thread
From: Mikael Pettersson @ 2011-08-15 14:32 UTC (permalink / raw)
To: Josip Rodin; +Cc: Mikael Pettersson, sparclinux, linux-kernel
Josip Rodin writes:
> On Mon, Aug 15, 2011 at 01:09:09PM +0200, Mikael Pettersson wrote:
> > The sparc32 version of arch_write_unlock() is just a plain assignment.
> > Unfortunately this allows the compiler to schedule side-effects in a
> > protected region to occur after the HW-level unlock, which is broken.
> > E.g., the following trivial test case gets miscompiled:
> >
> > #include <linux/spinlock.h>
> > rwlock_t lock;
> > int counter;
> > void foo(void) { write_lock(&lock); ++counter; write_unlock(&lock); }
> >
> > Fixed by adding a compiler memory barrier to arch_write_unlock(). The
> > sparc64 version combines the barrier and assignment into a single asm(),
> > so that's what I did here as well.
> >
> > Compiled-tested with a sparc32 SMP kernel.
> >
> > Signed-off-by: Mikael Pettersson <mikpe@it.uu.se>
> > ---
> > --- linux-3.1-rc2/arch/sparc/include/asm/spinlock_32.h.~1~ 2011-07-22 12:01:08.000000000 +0200
> > +++ linux-3.1-rc2/arch/sparc/include/asm/spinlock_32.h 2011-08-15 11:43:49.000000000 +0200
> > @@ -131,6 +131,15 @@ static inline void arch_write_lock(arch_
> > *(volatile __u32 *)&lp->lock = ~0U;
> > }
> >
> > +static void inline arch_write_unlock(arch_rwlock_t *lock)
> > +{
> > + __asm__ __volatile__(
> > +" st %%g0, [%0]"
> > + : /* no outputs */
> > + : "r" (lock)
> > + : "memory");
> > +}
> > +
> > static inline int arch_write_trylock(arch_rwlock_t *rw)
> > {
> > unsigned int val;
> > @@ -175,7 +184,7 @@ static inline int __arch_read_trylock(ar
> > res; \
> > })
> >
> > -#define arch_write_unlock(rw) do { (rw)->lock = 0; } while(0)
> > +#define arch_write_unlock(rw) arch_write_unlock(rw)
> >
> > #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
> > #define arch_read_lock_flags(rw, flags) arch_read_lock(rw)
>
> Why keep the tautological define? Just wondering.
Only because sparc64 does it that way. I now see that no other
arch has the #define, so perhaps that bit should be deleted (from
both sparc64 and sparc32).
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH,SPARC] make sparc32 arch_write_unlock() match the sparc64 version
2011-08-15 14:32 ` Mikael Pettersson
@ 2011-08-15 15:24 ` Sam Ravnborg
0 siblings, 0 replies; 4+ messages in thread
From: Sam Ravnborg @ 2011-08-15 15:24 UTC (permalink / raw)
To: Mikael Pettersson; +Cc: Josip Rodin, sparclinux, linux-kernel
Hi Mikael.
> > > @@ -175,7 +184,7 @@ static inline int __arch_read_trylock(ar
> > > res; \
> > > })
> > >
> > > -#define arch_write_unlock(rw) do { (rw)->lock = 0; } while(0)
> > > +#define arch_write_unlock(rw) arch_write_unlock(rw)
> > >
> > > #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
> > > #define arch_read_lock_flags(rw, flags) arch_read_lock(rw)
> >
> > Why keep the tautological define? Just wondering.
>
> Only because sparc64 does it that way. I now see that no other
> arch has the #define, so perhaps that bit should be deleted (from
> both sparc64 and sparc32).
Please kill the extra define in both sparc32 and sparc64.
Preferably in two separate patches.
For the subject of the patch please use:
[PATCH] sparc32: bla bla
Because when davem apply the patch everything in [] is zapped,
and it is good to see in the shortlog that this is a sparc32 specific patch.
For sparc64 we sometimes use "sparc64: bla bal", and sometimes use
"sparc: bla bla".
I prefer the first but no strong feelings.
And btw thanks for looking at this! Looks like a very difficult bug to nail.
Sam
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2011-08-15 15:24 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-15 11:09 [PATCH,SPARC] make sparc32 arch_write_unlock() match the sparc64 version Mikael Pettersson
2011-08-15 13:31 ` Josip Rodin
2011-08-15 14:32 ` Mikael Pettersson
2011-08-15 15:24 ` Sam Ravnborg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®