Re: [PATCH 4/5] futex: Avoid taking hb lock if nothing to wakeup

From: Thomas Gleixner
Date: Mon Nov 25 2013 - 15:26:44 EST


On Mon, 25 Nov 2013, Darren Hart wrote:
> On Mon, 2013-11-25 at 20:47 +0100, Thomas Gleixner wrote:
> > > which restores the ordering guarantee, which the hash bucket lock
> > > provided so far.
> >
> > Actually that's not true by design, it just happens to work.
> >
> > atomic_inc() on x86 is a "lock incl".
> >
> > The LOCK prefix just guarantees that the cache line which is affected
> > by the INCL is locked. And it guarantees that locked operations
> > serialize all outstanding load and store operations.
> >
> > But Documentation/atomic_ops.txt says about atomic_inc():
> >
> > "One very important aspect of these two routines is that they DO NOT
> > require any explicit memory barriers. They need only perform the
> > atomic_t counter update in an SMP safe manner."
> >
> > So while this has a barrier on x86, it's not guaranteed.
>
>
> But it is guaranteed to be "in an SMP safe manner"... which I guess just
> means that two writes will not intermix bytes, but no guarantee that the
> value will be visible to other CPUs unless some kind of barrier is
> explicitly imposed.
>
> Correct?

Yep, that's what it sayes.

> > So now your code melts down to:
> >
> > write(hb->waiters) | write(uaddr)
> > mb | read(hb->waiters)
> > read(uaddr)
> >
> > I fear you simply managed to make the window small enough that your
> > testing was not longer able expose it.
>
> Does seem to be the case.

You must be aware, that between the the write(uaddr) and the
read(hb->waiters) is the syscall, i.e. a user/kernel space
transition.

sysenter/syscall have no documented barriers inside, but we don't know
whether the actual hw implementation provides one or if the memory ops
between modifying uaddr and reaching the read(hb->waiters) point
including the real atomic op on the waiter side are good enough to
paper over the issue.

Thanks,

tglx



--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/