Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1686795imm; Thu, 27 Sep 2018 00:30:54 -0700 (PDT) X-Google-Smtp-Source: ACcGV63lScpq867Gg0LqYfYhQEyjXUHjYOhDUEnIjPz7nWLOUxGa6XDs4Hc+y9MICoyvd4tLTE3T X-Received: by 2002:a63:6054:: with SMTP id u81-v6mr8642876pgb.74.1538033454606; Thu, 27 Sep 2018 00:30:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538033454; cv=none; d=google.com; s=arc-20160816; b=hxu9r7FzKqLol+i23O8CPpixgAXJTn6bTu4c3ckrdk/QRBhYEGROGq8Lm7lt+bNbti 4XXCNf4ZG/bVSY3z2XOYkwcP+Crgsn3Qa2iV3JT40mdGmpR2XWrrhhmXh7n+lBn9O7f5 MncEqrD44BSnGd9kMpC1+aEPd8ow0ku0kJTwKNpAjCYtKpJcz0ImbJ6vxTLP6rrpDEuo cY13M9HddQf8gaB+WWISaR8UhZHnzmXHZYWYl1scD0muAS3Zos1UF2diO/AHEEol4gE7 GGp2MnAkdajLb3bIvFvtlJqyTFN2ThdhHeRwiMwbrkKRHD5+2kDWcwVkY1XZ5S+X1voV rmXA== 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; bh=m9MVQbR/JWpYgSlTVBv4mPTLo5xSZapF3twV2vdOXZs=; b=FmfUi+/UmPsUdYLyTW2T21qBR7/XGWwZ0LdyS8Q1Egbhy27zBUN9e1UbX+1w3y+A58 piSlhOLJj2oYjmZgilw0+Fl3A7goPjFW/oGAT2UQAvIV/i0gu0Ha33bQqYOv6sDh+UPW AI+99WauhgfnhkQJv1twD+l2vCLj6/q3myJV8pM8fXFJOjJnVa/SjFEP9CfTMp59VfiE SvtPVhKEHGp3vLskaqNElqiFjFR3gjcy1vdjJjqA+LsxZG5R6aIYsL67GKxMTGxGg3CO P4WrSFW4wKVgyKCyNio1p9aUAtGLn8CCANXWOL9sgIqMoSsNukJodL1i/pjKD3exhHec yY1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=C94Ha9mg; 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 k28-v6si1314028pgf.308.2018.09.27.00.30.39; Thu, 27 Sep 2018 00:30:54 -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=C94Ha9mg; 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 S1727090AbeI0NrR (ORCPT + 99 others); Thu, 27 Sep 2018 09:47:17 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:51158 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726820AbeI0NrQ (ORCPT ); Thu, 27 Sep 2018 09:47:16 -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=m9MVQbR/JWpYgSlTVBv4mPTLo5xSZapF3twV2vdOXZs=; b=C94Ha9mg3DaBnb14pgVpbeqql /AeLMNFOKnDbwFcVBz8jF+BoA+UHSN/yerUsZtYFqQEmQIWskIv2SBvVQV5C6/9MMqo61ez3TJ2eR DKq6GmP8cEl9NNwdv/aBJIH21nyRZhE9upjlIZLezhwpmYiNyDRhFJZaEKFHyNl9YI3cuc+I8TLVC WOD9QCnGdy9WCvnVM4wAs+ESQbqolSb+C4eQewAe0n7WE70vQC/Zq3i+WmPrVKPw4gCGnApoQTJ6r R+xzioLBCqTuk0Kyxs2VbYOhVrpvcwB5YXY747Pb2RIwiqMoTqzRP5UNtabodL4pjm99kBQAOzRDN qNPDCyXyw==; 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 1g5QkT-0004WK-Ex; Thu, 27 Sep 2018 07:30:21 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 116F620696FC8; Thu, 27 Sep 2018 09:29:52 +0200 (CEST) Date: Thu, 27 Sep 2018 09:29:52 +0200 From: Peter Zijlstra To: Waiman Long Cc: will.deacon@arm.com, mingo@kernel.org, linux-kernel@vger.kernel.org, andrea.parri@amarulasolutions.com, tglx@linutronix.de Subject: Re: [RFC][PATCH 3/3] locking/qspinlock: Optimize for x86 Message-ID: <20180927072952.GB7455@hirez.programming.kicks-ass.net> References: <20180926110117.405325143@infradead.org> <20180926111307.513429499@infradead.org> <396f7363-0f28-0fdb-7a48-ae16b0c8e82f@redhat.com> <20180926175418.GB5254@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180926175418.GB5254@hirez.programming.kicks-ass.net> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 26, 2018 at 07:54:18PM +0200, Peter Zijlstra wrote: > On Wed, Sep 26, 2018 at 12:30:36PM -0400, Waiman Long wrote: > > On 09/26/2018 07:01 AM, Peter Zijlstra wrote: > > > On x86 we cannot do fetch_or with a single instruction and end up > > > using a cmpxchg loop, this reduces determinism. Replace the fetch_or > > > with a very tricky composite xchg8 + load. > > > > > > The basic idea is that we use xchg8 to test-and-set the pending bit > > > (when it is a byte) and then a load to fetch the whole word. Using > > > two instructions of course opens a window we previously did not have. > > > In particular the ordering between pending and tail is of interrest, > > > because that is where the split happens. > > > > > > The claim is that if we order them, it all works out just fine. There > > > are two specific cases where the pending,tail state changes: > > > > > > - when the 3rd lock(er) comes in and finds pending set, it'll queue > > > and set tail; since we set tail while pending is set, the ordering > > > is split is not important (and not fundamentally different form > > > fetch_or). [*] > > > > The split can cause some changes in behavior. The 3rd locker observes > > the pending bit and set tail. The split load of the 2nd locker may make > > it observe the tail and backout of the pending loop. As a result, the > > 2nd locker will acquire the lock after the third locker in this case. > > That won't happen with the original code. > > > > I am not saying this is a problem. It is just something we should take > > note on. > > Right, good one. Yes that can happen. I think I remember why I 'discounted' that scenario; the same can happen with the fetch_or() but in a timing scenario. Consider: CPU 0 CPU 1 CPU 2 CPU 3 lock (->0,0,1) lock trylock (fail) tas-pending (->0,1,1) wait-!locked lock trylock (fail) tas-pending (fail) unlock (->0,1,0) acquire (->0,0,1) lock trylock (fail) (A) xchg-tail tas-pending -> (x,1,1) (B) xchg-tail And then, depending on A/B x is either 0 or !0. So the ordering of the concurrent lock attempts is always (obviously) subject to timing, the split xchg-load thing just makes it 'worse'.