Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp535928ybl; Thu, 23 Jan 2020 03:24:05 -0800 (PST) X-Google-Smtp-Source: APXvYqxql48UvOVnUhhSkvU8RlOEvpLN0cUr3oMvoTBkrHVcr0VWEbsmXHuLYNMPkSCxCF83pGFg X-Received: by 2002:a9d:5c8a:: with SMTP id a10mr10110242oti.95.1579778645070; Thu, 23 Jan 2020 03:24:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579778645; cv=none; d=google.com; s=arc-20160816; b=AJHQ7Bbz3BxKYrLEutDyiGNSHLS2lig4GJK/sXdIMGpGuJ64nqyx4bQJS6jm9cpOFa /QtyBlXL6euVyS46MjdFnZShnhwvliTsLEkIsFLgYeiXNEcnF9JER2/azae9kt3EIG81 1r/K8k3amRlnmBG/BVjs/eQnNvVkd+MrSNGKQT/AW5ItwYPXhsgzXLEqUV//jxIJt/IJ mJ+jDXPI/K2f8dicsgqMvtqTVxb+tFA1NeagzaO0IRVw7oZrYPfuDL+fhqzdxeCOdJ83 kGVFzHS3a2hcBw0s1Mb1SzBbSCIU+fggDFOZ3t/5l1utqxnGtEJgPl8WPbe/7iEkdqlW w4CQ== 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=ojTS7IQUmVJNQcS2uL89T5i6w7tUFqD1HIYgCrsHp2Y=; b=GXSYJwocpeNRijgdtczyXIihgG/pMg5E+1HF+0vnEIdhC0WKrp2VJyprzm269DcRMY zq3Yg9CobblPzzlMqsoGA7bzap0J34rSbgWqDDKXQbqRjkhWKp/hK/0l7jXKGLJnk5dj WY+HbRxVV3mRbbzpFCdLkP2inB+KflsVNbCLmuio/O75NBWsa40NHc2vDdS3hTlxcVkM Xob1tdRzOYwGTQuj4gJZtGXhhKg/z31g5cW+B9jAZr6mJ3T64p9nUeXC1bQN1SXbEB3T qMVG0xo6VSN3bh8F4u+ulNFV31I3LkszPOn+N2koluo2lVrs7kA9S4Pq32ilEddFCpll HdnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="upMqE3/O"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e1si28384otr.8.2020.01.23.03.23.53; Thu, 23 Jan 2020 03:24:05 -0800 (PST) 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=pass header.i=@kernel.org header.s=default header.b="upMqE3/O"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728731AbgAWLW7 (ORCPT + 99 others); Thu, 23 Jan 2020 06:22:59 -0500 Received: from mail.kernel.org ([198.145.29.99]:42800 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726771AbgAWLW6 (ORCPT ); Thu, 23 Jan 2020 06:22:58 -0500 Received: from willie-the-truck (236.31.169.217.in-addr.arpa [217.169.31.236]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 08E1A24125; Thu, 23 Jan 2020 11:22:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1579778578; bh=LgKFx4P5ScxA81AGbhPO/gALIBjOXsGoed8zoyS3Rjs=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=upMqE3/OImgO279sANVZ36MBMt8DKsuTiatp5+5AUVIg+ANe3LuJGpBT+JD3oWGPs Ys4oYxg810eKeuk0MHapCHsn83c8BXqeBreMaF23wqytnAak5rWFgLupnIl/i+RNcb BrnuFNunk+GQ4S9r1hmOsNE5isiH2yY5oWgZmPx8= Date: Thu, 23 Jan 2020 11:22:51 +0000 From: Will Deacon To: Peter Zijlstra Cc: Alex Kogan , linux-arch@vger.kernel.org, guohanjun@huawei.com, arnd@arndb.de, dave.dice@oracle.com, jglauber@marvell.com, x86@kernel.org, will.deacon@arm.com, linux@armlinux.org.uk, steven.sistare@oracle.com, linux-kernel@vger.kernel.org, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, longman@redhat.com, tglx@linutronix.de, daniel.m.jordan@oracle.com, linux-arm-kernel@lists.infradead.org Subject: Re: [PATCH v9 3/5] locking/qspinlock: Introduce CNA into the slow path of qspinlock Message-ID: <20200123112251.GC18991@willie-the-truck> References: <20200115035920.54451-1-alex.kogan@oracle.com> <20200115035920.54451-4-alex.kogan@oracle.com> <20200123092658.GC14879@hirez.programming.kicks-ass.net> <20200123100635.GE14946@hirez.programming.kicks-ass.net> <20200123101649.GF14946@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200123101649.GF14946@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 Thu, Jan 23, 2020 at 11:16:49AM +0100, Peter Zijlstra wrote: > On Thu, Jan 23, 2020 at 11:06:35AM +0100, Peter Zijlstra wrote: > > On Thu, Jan 23, 2020 at 10:26:58AM +0100, Peter Zijlstra wrote: > > > On Tue, Jan 14, 2020 at 10:59:18PM -0500, Alex Kogan wrote: > > > > +/* this function is called only when the primary queue is empty */ > > > > +static inline bool cna_try_change_tail(struct qspinlock *lock, u32 val, > > > > + struct mcs_spinlock *node) > > > > +{ > > > > + struct mcs_spinlock *head_2nd, *tail_2nd; > > > > + u32 new; > > > > + > > > > + /* If the secondary queue is empty, do what MCS does. */ > > > > + if (node->locked <= 1) > > > > + return __try_clear_tail(lock, val, node); > > > > + > > > > + /* > > > > + * Try to update the tail value to the last node in the secondary queue. > > > > + * If successful, pass the lock to the first thread in the secondary > > > > + * queue. Doing those two actions effectively moves all nodes from the > > > > + * secondary queue into the main one. > > > > + */ > > > > + tail_2nd = decode_tail(node->locked); > > > > + head_2nd = tail_2nd->next; > > > > + new = ((struct cna_node *)tail_2nd)->encoded_tail + _Q_LOCKED_VAL; > > > > + > > > > + if (atomic_try_cmpxchg_relaxed(&lock->val, &val, new)) { > > > > + /* > > > > + * Try to reset @next in tail_2nd to NULL, but no need to check > > > > + * the result - if failed, a new successor has updated it. > > > > + */ > > > > > > I think you actually have an ordering bug here; the load of head_2nd > > > *must* happen before the atomic_try_cmpxchg(), otherwise it might > > > observe the new next and clear a valid next pointer. > > > > > > What would be the best fix for that; I'm thinking: > > > > > > head_2nd = smp_load_acquire(&tail_2nd->next); > > > > > > Will? > > > > Hmm, given we've not passed the lock around yet; why wouldn't something > > like this work: > > > > smp_store_release(&tail_2nd->next, NULL); > > Argh, make that: > > tail_2nd->next = NULL; > > smp_wmb(); > > > if (!atomic_try_cmpxchg_relaxed(&lock, &val, new)) { ... or could you drop the smp_wmb() and make this atomic_try_cmpxchg_release()? To be honest, I've failed to understand the code prior to your changes in this area: it appears to reply on a control-dependency from the two cmpxchg_relaxed() calls (which isn't sufficient to order the store parts afaict) and I also don't get how we deal with a transiently circular primary queue. Will