From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935051AbcJHMBn (ORCPT ); Sat, 8 Oct 2016 08:01:43 -0400 Received: from Galois.linutronix.de ([146.0.238.70]:46183 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754882AbcJHMBf (ORCPT ); Sat, 8 Oct 2016 08:01:35 -0400 Date: Sat, 8 Oct 2016 13:58:07 +0200 (CEST) From: Thomas Gleixner To: Peter Zijlstra cc: Linus Torvalds , Waiman Long , Jason Low , Ding Tianhong , Will Deacon , Ingo Molnar , Imre Deak , Linux Kernel Mailing List , Davidlohr Bueso , Tim Chen , Terry Rudd , "Paul E. McKenney" , Chris Wilson , Daniel Vetter , Rob Clark Subject: Re: [PATCH -v4 1/8] locking/drm: Kill mutex trickery In-Reply-To: <20161007154351.GL3117@twins.programming.kicks-ass.net> Message-ID: References: <20161007145243.361481786@infradead.org> <20161007150210.927453282@infradead.org> <20161007154351.GL3117@twins.programming.kicks-ass.net> User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 7 Oct 2016, Peter Zijlstra wrote: > On Fri, Oct 07, 2016 at 04:52:44PM +0200, Peter Zijlstra wrote: > > Poking at lock internals is not cool. Since I'm going to change the > > implementation this will break, take it out. > > > So something like the below would serve as a replacement for your > previous hacks. Is this API something acceptable to people? Ingo, > Thomas? > > --- > include/linux/mutex.h | 25 +++++++++++++++++++++++++ > 1 file changed, 25 insertions(+) > > diff --git a/include/linux/mutex.h b/include/linux/mutex.h > index 4d3bccabbea5..afcff2c85957 100644 > --- a/include/linux/mutex.h > +++ b/include/linux/mutex.h > @@ -189,4 +189,29 @@ extern void mutex_unlock(struct mutex *lock); > > extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock); > > +enum mutex_trylock_recursive_enum { > + mutex_trylock_failed = 0, > + mutex_trylock_success = 1, > + mutex_trylock_recursive, Upper case enum symbols please, if at all. > +}; > + > +/** > + * mutex_trylock_recursive - trylock variant that allows recursive locking > + * @lock: mutex to be locked > + * > + * > + * Returns: > + * mutex_trylock_failed - trylock failed, > + * mutex_trylock_success - lock acquired, > + * mutex_trylock_recursive - we already owned the lock. > + */ > +static inline enum mutex_trylock_recursive_enum > +mutex_trylock_recursive(struct mutex *lock) > +{ > + if (unlikely(__mutex_owner(lock) == current)) > + return mutex_trylock_recursive; > + > + return mutex_trylock(lock); > +} Hmm. I'm not a great fan of this, because that requires an conditional unlock mechanism. res = trylock_recursive(lock); if (res == FAILED) goto out; ..... if (res == SUCCESS) unlock(lock); While if you actually keep track of recursion you can do: if (!trylock_recursive(lock)) goto out; .... unlock_recursive(lock); or even: lock_recursive(lock); unlock_recursive(lock); That's making lock/trylock and unlock symetric, so its obvious in the source what's going on and the recursion tracking allows for better debugability. Thanks, tglx