Re: [PATCH -v3 4/8] locking, arch: Update spin_unlock_wait()

From: Peter Zijlstra
Date: Wed Jun 01 2016 - 07:39:06 EST


On Wed, Jun 01, 2016 at 12:24:32PM +0100, Will Deacon wrote:
> > --- a/arch/arm/include/asm/spinlock.h
> > +++ b/arch/arm/include/asm/spinlock.h
> > @@ -50,8 +50,22 @@ static inline void dsb_sev(void)
> > * memory.
> > */
> >
> > -#define arch_spin_unlock_wait(lock) \
> > - do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0)
> > +static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
> > +{
> > + u16 owner = READ_ONCE(lock->tickets.owner);
> > +
> > + smp_rmb();
>
> (so you can remove this barrier)

*poof* in a cloud of bit smoke it goes..

> > + for (;;) {
> > + arch_spinlock_t tmp = READ_ONCE(*lock);
> > +
> > + if (tmp.tickets.owner == tmp.tickets.next ||
> > + tmp.tickets.owner != owner)
>
> This is interesting... on arm64, I actually wait until I observe the
> lock being free, but here you also break if the owner has changed, on
> the assumption that an unlock happened and we just didn't explicitly
> see the lock in a free state. Now, what stops the initial read of
> owner being speculated by the CPU at the dawn of time, and this loop
> consequently returning early because at some point (before we called
> arch_spin_unlock_wait) the lock was unlocked?

The user needs to be aware; take for instance the scenario explained
here:

lkml.kernel.org/r/20160526135406.GK3192@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

or the PF_EXITING spin_unlock_wait in do_exit.

In both cases we only need to wait for any in-flight critical section
that might not have observed our recent change. Any further critical
sections are guaranteed to have observed our change and will behave
accordingly.

Note that other architectures already had the wait for one ticket
completion (notably x86) semantics.