Received: by 10.213.65.68 with SMTP id h4csp2784589imn; Mon, 9 Apr 2018 09:00:17 -0700 (PDT) X-Google-Smtp-Source: AIpwx49BtM0oMFfYIC3jAdPb/JxstlK/1ZjY37VBh29KnOiKh+S/qIjmZdXi8AvEPl9dxiJqPOUI X-Received: by 2002:a17:902:7d8a:: with SMTP id a10-v6mr16309539plm.268.1523289617162; Mon, 09 Apr 2018 09:00:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523289617; cv=none; d=google.com; s=arc-20160816; b=IgL4o7BxtmqckfQkelJ3y4OqftB3W4r/ePQD/udpDmQpWvwNhYgqs0sjBYuG8L4h0N 6hPqfpGd9e+pGovnT8f+ZOPqiv0zPdhKssVKuVuxf8o98clNMybzF6KHLRo9796RqgGC Jt9wPhS0R7bAG0TVFv6xtdScz7V5RkVxIU7cd1J0fazrAfTvr0b2qjQgRixxOR0Yz+sb m9f+5Hnd2KFfjo/GpZWASiPlnyFi6V5pd522fC8SCpWxqh9ybLWBmF30SyScYhCrJW+4 lwr6atdHK7ZuFd3kpzv9JLEMnqQ6GsjeGAzBWfAMZk1py23yS+ipXOtSav+HE8k5B+/n rF4Q== 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=sYtEJjEVzeQnjrQaTSwsvi8wVuhCnPT68qWGsw4Fud0=; b=hjRy757zHkWR6GgtptS/0P/IY1+kKsBRrlaJRPEizt8G7wgtQg7cwSPm8/CKCmOBPP 6N1/7LxyJpY/iP2CfY2hT5elbIXHkQAY+r+M36/D62X3ifiA4tKoPm8hz5xj5e8Nm8R9 Bn/4++PfQTzlpBTFjhn/NPXVBsXgsJnA1z2sO+YJ9oRHncxhJRWA/nehdZy06f3Fuk92 r0AmVq/dkD9iD/8qASPQfwrpRgrgep+gzpqshzq2sls0gHCTgDCsUVheil9HyrHUKLlr 7xKktUa7vEyFHST6eFGE3rAzChw+9ZJMS5HIFCa0OMe0MgKhji/7TwgZ71qZh/LX3zDR xEvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=2inp14vP; 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 l74si384035pga.798.2018.04.09.08.59.39; Mon, 09 Apr 2018 09:00:17 -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=merlin.20170209 header.b=2inp14vP; 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 S1753191AbeDIPyc (ORCPT + 99 others); Mon, 9 Apr 2018 11:54:32 -0400 Received: from merlin.infradead.org ([205.233.59.134]:55542 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752320AbeDIPya (ORCPT ); Mon, 9 Apr 2018 11:54:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.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=sYtEJjEVzeQnjrQaTSwsvi8wVuhCnPT68qWGsw4Fud0=; b=2inp14vP9rgFAYIG92ZAeNCCO hJTf4vSEVVNELj1K/65jE6ky3/+zD+5BsRSwANRBnKAIDAWvi8AozthX63ha4I2AXOKZhpPVhvSeO O2Lo3inKGuA8PUEQ2uBu/RxXpGxMySeArxvv6shvF/cLLxFXK5Z5kpuLiiLm8du83QsPToe9JyVZX pv2hX6x5jWvd+LGZTxgF5jTf1n1u9GjqQj6NetbtEmwiUcEJOpZIj9z290X6syUjbEP4bZGt/9wO3 Y6vlHlgyidmcfJV7uYlIPlmdAsdTnVp5xiPnVNjlpTW5s1SUmlvj2goUh8NCEnksLxhFht8Tpy4dK /9D3OM6cw==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1f5Z7d-0007JX-NO; Mon, 09 Apr 2018 15:54:21 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 70F6A20298CF9; Mon, 9 Apr 2018 17:54:20 +0200 (CEST) Date: Mon, 9 Apr 2018 17:54:20 +0200 From: Peter Zijlstra To: Will Deacon Cc: Waiman Long , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, mingo@kernel.org, boqun.feng@gmail.com, paulmck@linux.vnet.ibm.com, catalin.marinas@arm.com Subject: Re: [PATCH 02/10] locking/qspinlock: Remove unbounded cmpxchg loop from locking slowpath Message-ID: <20180409155420.GB4082@hirez.programming.kicks-ass.net> References: <1522947547-24081-1-git-send-email-will.deacon@arm.com> <1522947547-24081-3-git-send-email-will.deacon@arm.com> <20180409105835.GC23134@arm.com> <20180409145409.GA9661@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180409145409.GA9661@arm.com> User-Agent: Mutt/1.9.3 (2018-01-21) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 09, 2018 at 03:54:09PM +0100, Will Deacon wrote: > diff --git a/kernel/locking/qspinlock.c b/kernel/locking/qspinlock.c > index 19261af9f61e..71eb5e3a3d91 100644 > --- a/kernel/locking/qspinlock.c > +++ b/kernel/locking/qspinlock.c > @@ -139,6 +139,20 @@ static __always_inline void clear_pending_set_locked(struct qspinlock *lock) > WRITE_ONCE(lock->locked_pending, _Q_LOCKED_VAL); > } > > +/** > + * set_pending_fetch_acquire - set the pending bit and return the old lock > + * value with acquire semantics. > + * @lock: Pointer to queued spinlock structure > + * > + * *,*,* -> *,1,* > + */ > +static __always_inline u32 set_pending_fetch_acquire(struct qspinlock *lock) > +{ > + u32 val = xchg_relaxed(&lock->pending, 1) << _Q_PENDING_OFFSET; > + val |= (atomic_read_acquire(&lock->val) & ~_Q_PENDING_MASK); > + return val; > +} > @@ -289,18 +315,26 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) > return; > > /* > - * If we observe any contention; queue. > + * If we observe queueing, then queue ourselves. > */ > - if (val & ~_Q_LOCKED_MASK) > + if (val & _Q_TAIL_MASK) > goto queue; > > /* > + * We didn't see any queueing, so have one more try at snatching > + * the lock in case it became available whilst we were taking the > + * slow path. > + */ > + if (queued_spin_trylock(lock)) > + return; > + > + /* > * trylock || pending > * > * 0,0,0 -> 0,0,1 ; trylock > * 0,0,1 -> 0,1,1 ; pending > */ > + val = set_pending_fetch_acquire(lock); > if (!(val & ~_Q_LOCKED_MASK)) { So, if I remember that partial paper correctly, the atomc_read_acquire() can see 'arbitrary' old values for everything except the pending byte, which it just wrote and will fwd into our load, right? But I think coherence requires the read to not be older than the one observed by the trylock before (since it uses c-cas its acquire can be elided). I think this means we can miss a concurrent unlock vs the fetch_or. And I think that's fine, if we still see the lock set we'll needlessly 'wait' for it go become unlocked.