Re: [RFC PATCH 3/3] locking/mutex: Optimize mutex trylock slowpath

Jason Low
2014-06-05T03:09:05Z

On Wed, 2014-06-04 at 18:10 -0700, Davidlohr Bueso wrote:
> On Wed, 2014-06-04 at 14:47 -0700, Jason Low wrote:
> > On Wed, 2014-06-04 at 13:28 -0700, Davidlohr Bueso wrote:
> > > On Wed, 2014-06-04 at 12:08 -0700, Jason Low wrote:
> > > > In __mutex_trylock_slowpath(), we acquire the wait_lock spinlock,
> > > > xchg() lock->count with -1, then set lock->count back to 0 if there
> > > > are no waiters, and return true if the prev lock count was 1.
> > > >
> > > > However, if we the mutex is already locked, then there may not be
> > > ^^ leave that out.
> > >
> > > > much point in attempting the above operations.
> > >
> > > Isn't this redundant? I mean, if we enter the slowpath its because
> > > __mutex_fastpath_trylock() already failed so we already know that the
> > > lock is taken.
> >
> > This function is really just used as an alternative method of trylock
> > for !__HAVE_ARCH_CMPXCHG. In that case, the fastpath can call directly
> > into the slowpath function, without checking for if the lock is taken.
>
> Ah, ok I hadn't seen that we do this in 32bit x86 and was wondering why
> the heck we fallback on a slowpath for something like trylock, which
> should return right away no matter what. I'd suggest explicitly
> mentioning this in the changelog. Otherwise makes sense now.

Yup, I was also initially wondering why we have a slowpath for trylock,
which prompted me to take a look into this function. I'll add some more
information about this to the changelog.

Thanks,
Jason