Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp1894130imm; Thu, 14 Jun 2018 05:42:27 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLYCpyJos/iwTZA6lhM6MEotOqmkKOHlMl3NPSAky/3hqV0saIionDmwdCtl5ZWh6PLOAqs X-Received: by 2002:a17:902:b488:: with SMTP id y8-v6mr2846093plr.157.1528980147606; Thu, 14 Jun 2018 05:42:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528980147; cv=none; d=google.com; s=arc-20160816; b=Abp4NIKKox5tgln7BF/0AgMoR3WdAZAjQIo2hW2l5U9TK6cAy42aiQkHW9jQtjr65p LyYIZAAqYHLrBIHY66zFsPbzVn+7okapoPkRdnGp2lZYv5E2VLOkuF8dv7PTGxR+ZADs Tdy6wQxTXX9GtLRl4GbgW0aulKTklFQT9gXzM18MHqHtnAAq1wwcGsRoO3XOC38ENnxl K2mp48+rwwymSrHKuCeaLn0DovhaGh9SPio20jjrnJSHbhcZAAsJHRyu/dR9jAPs0r/Y AYvj5ITP7SmL6eGnSqqoDXe9rkaav7yYssTJZxP3xvk+02ucqAgAz5EaVWrZCa7v1sFV qC9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=7Vnfygi+S4e1dpA/p8MvyS+CjWNGrNPu+tph0TNeRc4=; b=HcRdVwyTmJ41SJPcG91Qa6tbP3VO/JzkgjmY+f6ElW31IMSmvox6fBbFvuiyh/J8oX DqkO1SIzfIZUEqHgfu10nZVOX/4v0MRyjWVOnDjI6piIFWW6fOQRKsU0CM6olxfoS3rm xOEcujO0W4YHukKkqdk+uLhEx8IVtsiJA+cy6cvhC7lUIQ5II5n+88GvNqOp9XMDrUAl YDBPnCFdxIXauuOmcsxXSJ90Aaj8+UxUjp0UP2bsLSYjFr9jgF9AXxNSXid19h4a+BQ/ RXACZFQxye6Notvt0EYQM1KulFZ0IcuGLft0eNAfsU16FlPlRkAbIwIkzNnFlMkMJOxh 8ntA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b="JV7HBtU/"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 67-v6si4250035pge.373.2018.06.14.05.42.13; Thu, 14 Jun 2018 05:42:27 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b="JV7HBtU/"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755077AbeFNMlu (ORCPT + 99 others); Thu, 14 Jun 2018 08:41:50 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:33714 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754809AbeFNMls (ORCPT ); Thu, 14 Jun 2018 08:41:48 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=7Vnfygi+S4e1dpA/p8MvyS+CjWNGrNPu+tph0TNeRc4=; b=JV7HBtU/eH6RkKBZK57FH/fuy kThslC+rx+KIaf5616RmQIczhoaW1BY3G1QR+mFJX8WUmOUbM/dFZsNVkky0zmLTryiN8wjSqu6cK x+J3wm15AccDw/7ShBzX62rOI/YZB5cMu77RSi+UfJVrbdLiEI7H3V7SUTMFxAtQsZwYmQSkDgj+Z uAJ+NeCTT8EskzThnUYwua0jwPn7YGbMgcpXF/mjnGBiHbrKGrHqNiQZ5Liw0dERACRW0uAt2vSSg TaBdjCcmni3LRCQCy/y8ndFXw/Id/Puxvidwb9p37LIGX+p5HXZv1SWzBF6/ooDbf071eWbki46zU K85v0bD4Q==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1fTRZD-00007p-Rg; Thu, 14 Jun 2018 12:41:32 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id AB7AE201EA7C7; Thu, 14 Jun 2018 14:41:29 +0200 (CEST) Date: Thu, 14 Jun 2018 14:41:29 +0200 From: Peter Zijlstra To: Thomas Hellstrom Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Ingo Molnar , Jonathan Corbet , Gustavo Padovan , Maarten Lankhorst , Sean Paul , David Airlie , Davidlohr Bueso , "Paul E. McKenney" , Josh Triplett , Thomas Gleixner , Kate Stewart , Philippe Ombredanne , Greg Kroah-Hartman , linux-doc@vger.kernel.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org Subject: Re: [PATCH v2 1/2] locking: Implement an algorithm choice for Wound-Wait mutexes Message-ID: <20180614124129.GA12198@hirez.programming.kicks-ass.net> References: <20180614072922.8114-1-thellstrom@vmware.com> <20180614072922.8114-2-thellstrom@vmware.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180614072922.8114-2-thellstrom@vmware.com> User-Agent: Mutt/1.9.5 (2018-04-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 14, 2018 at 09:29:21AM +0200, Thomas Hellstrom wrote: > +static bool __ww_mutex_wound(struct mutex *lock, > + struct ww_acquire_ctx *ww_ctx, > + struct ww_acquire_ctx *hold_ctx) > +{ > + struct task_struct *owner = __mutex_owner(lock); > + > + lockdep_assert_held(&lock->wait_lock); > + > + if (owner && hold_ctx && __ww_ctx_stamp_after(hold_ctx, ww_ctx) && > + ww_ctx->acquired > 0) { > + hold_ctx->wounded = 1; > + > + /* > + * wake_up_process() paired with set_current_state() inserts > + * sufficient barriers to make sure @owner either sees it's > + * wounded or has a wakeup pending to re-read the wounded > + * state. > + * > + * The value of hold_ctx->wounded in > + * __ww_mutex_lock_check_stamp(); > + */ > + if (owner != current) > + wake_up_process(owner); > + > + return true; > + } > + > + return false; > +} > @@ -338,12 +377,18 @@ ww_mutex_set_context_fastpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) > * and keep spinning, or it will acquire wait_lock, add itself > * to waiter list and sleep. > */ > - smp_mb(); /* ^^^ */ > + smp_mb(); /* See comments above and below. */ > > /* > - * Check if lock is contended, if not there is nobody to wake up > + * Check if lock is contended, if not there is nobody to wake up. > + * We can use list_empty() unlocked here since it only compares a > + * list_head field pointer to the address of the list head > + * itself, similarly to how list_empty() can be considered RCU-safe. > + * The memory barrier above pairs with the memory barrier in > + * __ww_mutex_add_waiter and makes sure lock->ctx is visible before > + * we check for waiters. > */ > - if (likely(!(atomic_long_read(&lock->base.owner) & MUTEX_FLAG_WAITERS))) > + if (likely(list_empty(&lock->base.wait_list))) > return; > OK, so what happens is that if we see !empty list, we take wait_lock, if we end up in __ww_mutex_wound() we must really have !empty wait-list. It can however still see !owner because __mutex_unlock_slowpath() can clear the owner field. But if owner is set, it must stay valid because FLAG_WAITERS and we're holding wait_lock. So the wake_up_process() is in fact safe. Let me put that in a comment.