From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752767AbcAVKWs (ORCPT ); Fri, 22 Jan 2016 05:22:48 -0500 Received: from g1t6225.austin.hp.com ([15.73.96.126]:34294 "EHLO g1t6225.austin.hp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751529AbcAVKWm (ORCPT ); Fri, 22 Jan 2016 05:22:42 -0500 Message-ID: <1453458019.9727.8.camel@j-VirtualBox> Subject: Re: [PATCH RFC] locking/mutexes: don't spin on owner when wait list is not NULL. From: Jason Low To: Peter Zijlstra Cc: Waiman Long , Ding Tianhong , Ingo Molnar , "linux-kernel@vger.kernel.org" , Davidlohr Bueso , Linus Torvalds , "Paul E. McKenney" , Thomas Gleixner , Will Deacon , Tim Chen , Waiman Long , jason.low2@hp.com Date: Fri, 22 Jan 2016 02:20:19 -0800 In-Reply-To: <20160122085422.GO6357@twins.programming.kicks-ass.net> References: <56A0A4ED.3070308@huawei.com> <56A1638A.7050202@hpe.com> <20160122085422.GO6357@twins.programming.kicks-ass.net> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.10.4-0ubuntu2 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 2016-01-22 at 09:54 +0100, Peter Zijlstra wrote: > On Thu, Jan 21, 2016 at 06:02:34PM -0500, Waiman Long wrote: > > This patch attempts to fix this live-lock condition by enabling the > > a woken task in the wait list to enter optimistic spinning loop itself > > with precedence over the ones in the OSQ. This should prevent the > > live-lock > > condition from happening. > > > So I think having the top waiter going back in to contend on the OSQ is > an excellent idea, but I'm not sure the wlh_spinning thing is important. > > The OSQ itself is FIFO fair, and the waiters retain the wait_list > position. So having the top wait_list entry contending on the OSQ > ensures we cannot starve (I think). Right, and we can also avoid needing to add that extra field to the mutex structure. Before calling optimistic spinning, we do want to check if the lock is available to avoid unnecessary OSQ overhead though. So maybe the following would be sufficient: --- kernel/locking/mutex.c | 7 +++++++ 1 files changed, 7 insertions(+), 0 deletions(-) diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index 0551c21..ead0bd1 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -543,6 +543,8 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, lock_contended(&lock->dep_map, ip); for (;;) { + bool acquired = false; + /* * Lets try to take the lock again - this is needed even if * we get here for the first time (shortly after failing to @@ -577,7 +579,12 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, /* didn't get the lock, go to sleep: */ spin_unlock_mutex(&lock->wait_lock, flags); schedule_preempt_disabled(); + + if (mutex_is_locked(lock)) + acquired = mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx); spin_lock_mutex(&lock->wait_lock, flags); + if (acquired) + break; } __set_task_state(task, TASK_RUNNING); -- 1.7.2.5