Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753842AbcLFPgS (ORCPT ); Tue, 6 Dec 2016 10:36:18 -0500 Received: from merlin.infradead.org ([205.233.59.134]:36902 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752892AbcLFPgQ (ORCPT ); Tue, 6 Dec 2016 10:36:16 -0500 Date: Tue, 6 Dec 2016 16:36:06 +0100 From: Peter Zijlstra To: Nicolai =?iso-8859-1?Q?H=E4hnle?= Cc: linux-kernel@vger.kernel.org, Nicolai =?iso-8859-1?Q?H=E4hnle?= , Ingo Molnar , Maarten Lankhorst , Daniel Vetter , Chris Wilson , dri-devel@lists.freedesktop.org Subject: Re: [PATCH v2 05/11] locking/ww_mutex: Add waiters in stamp order Message-ID: <20161206153606.GW3045@worktop.programming.kicks-ass.net> 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.22.1 (2013-10-16) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1550 Lines: 55 On Thu, Dec 01, 2016 at 03:06:48PM +0100, Nicolai H?hnle wrote: > +static inline int __sched > +__ww_mutex_add_waiter(struct mutex_waiter *waiter, > + struct mutex *lock, > + struct ww_acquire_ctx *ww_ctx) > +{ > + struct mutex_waiter *cur; > + > + if (!ww_ctx) { > + list_add_tail(&waiter->list, &lock->wait_list); > + return 0; > + } > + > + /* > + * Add the waiter before the first waiter with a higher stamp. > + * Waiters without a context are skipped to avoid starving > + * them. > + */ > + list_for_each_entry(cur, &lock->wait_list, list) { > + if (!cur->ww_ctx) > + continue; > + > + if (__ww_mutex_stamp_after(ww_ctx, cur->ww_ctx)) { > + /* Back off immediately if necessary. */ > + if (ww_ctx->acquired > 0) { > +#ifdef CONFIG_DEBUG_MUTEXES > + struct ww_mutex *ww; > + > + ww = container_of(lock, struct ww_mutex, base); > + DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock); > + ww_ctx->contending_lock = ww; > +#endif > + return -EDEADLK; > + } > + > + continue; > + } > + > + list_add_tail(&waiter->list, &cur->list); > + return 0; > + } > + > + list_add_tail(&waiter->list, &lock->wait_list); > + return 0; > +} So you keep the list in order of stamp, and in general stamps come in, in-order. That is, barring races on concurrent ww_mutex_lock(), things are already ordered. So it doesn't make sense to scan the entire list forwards, that's almost guarantees you scan the entire list every single time. Or am I reading this wrong? Which in itself is a hint a comment might be in place.