Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758451AbcLAQAo (ORCPT ); Thu, 1 Dec 2016 11:00:44 -0500 Received: from mail.fireflyinternet.com ([109.228.58.192]:52083 "EHLO fireflyinternet.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754943AbcLAQAn (ORCPT ); Thu, 1 Dec 2016 11:00:43 -0500 X-Default-Received-SPF: pass (skip=forwardok (res=PASS)) x-ip-name=78.156.65.138; Date: Thu, 1 Dec 2016 15:59:39 +0000 From: Chris Wilson To: Nicolai =?iso-8859-1?Q?H=E4hnle?= Cc: linux-kernel@vger.kernel.org, Nicolai =?iso-8859-1?Q?H=E4hnle?= , Peter Zijlstra , Ingo Molnar , Maarten Lankhorst , Daniel Vetter , dri-devel@lists.freedesktop.org Subject: Re: [PATCH v2 05/11] locking/ww_mutex: Add waiters in stamp order Message-ID: <20161201155939.GA24069@nuc-i3427.alporthouse.com> Mail-Followup-To: Chris Wilson , Nicolai =?iso-8859-1?Q?H=E4hnle?= , linux-kernel@vger.kernel.org, Nicolai =?iso-8859-1?Q?H=E4hnle?= , Peter Zijlstra , Ingo Molnar , Maarten Lankhorst , Daniel Vetter , dri-devel@lists.freedesktop.org References: <1480601214-26583-1-git-send-email-nhaehnle@gmail.com> <1480601214-26583-6-git-send-email-nhaehnle@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1480601214-26583-6-git-send-email-nhaehnle@gmail.com> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3133 Lines: 94 On Thu, Dec 01, 2016 at 03:06:48PM +0100, Nicolai H?hnle wrote: > @@ -677,15 +722,25 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, > debug_mutex_lock_common(lock, &waiter); > debug_mutex_add_waiter(lock, &waiter, task); > > - /* add waiting tasks to the end of the waitqueue (FIFO): */ > - list_add_tail(&waiter.list, &lock->wait_list); > + lock_contended(&lock->dep_map, ip); > + > + if (!use_ww_ctx) { > + /* add waiting tasks to the end of the waitqueue (FIFO): */ > + list_add_tail(&waiter.list, &lock->wait_list); > + } else { > + /* Add in stamp order, waking up waiters that must back off. */ > + ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx); > + if (ret) > + goto err_early_backoff; > + > + waiter.ww_ctx = ww_ctx; > + } > + > waiter.task = task; Would an unconditional waiter.ww_ctx = ww_ctx be chep enough? (Same cacheline write and all that?) Makes the above clearer in that you have if (!ww_ctx) { list_add_tail(); } else { ret = __ww_mutex_add_waiter(); /* no need to handle !ww_ctx */ if (ret) goto err_early_backoff; } waiter.ww_ctx = ww_ctx; waiter.task = task; > > if (__mutex_waiter_is_first(lock, &waiter)) > __mutex_set_flag(lock, MUTEX_FLAG_WAITERS); > > - lock_contended(&lock->dep_map, ip); > - > set_task_state(task, state); > for (;;) { > /* > @@ -693,8 +748,12 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, > * mutex_unlock() handing the lock off to us, do a trylock > * before testing the error conditions to make sure we pick up > * the handoff. > + * > + * For w/w locks, we always need to do this even if we're not > + * currently the first waiter, because we may have been the > + * first waiter during the unlock. > */ > - if (__mutex_trylock(lock, first)) > + if (__mutex_trylock(lock, use_ww_ctx || first)) I'm not certain about the magic of first vs HANDOFF. Afaict, first == HANDOFF and this patch breaks that relationship. I think you need to add bool handoff; as a separate tracker to first. > goto acquired; > > /* > @@ -716,7 +775,20 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, > spin_unlock_mutex(&lock->wait_lock, flags); > schedule_preempt_disabled(); > > - if (!first && __mutex_waiter_is_first(lock, &waiter)) { > + if (use_ww_ctx && ww_ctx) { > + /* > + * Always re-check whether we're in first position. We > + * don't want to spin if another task with a lower > + * stamp has taken our position. > + * > + * We also may have to set the handoff flag again, if > + * our position at the head was temporarily taken away. Comment makes sense. Ah. Should this be just if (use_ww_ctx) { /* always recheck... */ ? Except that !ww_ctx are never gazzumped in the list, so if they are first, then they are always first. Could you explain that as well (about why !ww_ctx is special here but not above). And then it can even be reduced to if (ww_ctx) {} to match the first chunk if the revision is acceptable. -Chris -- Chris Wilson, Intel Open Source Technology Centre