Received: by 10.213.65.68 with SMTP id h4csp318234imn; Fri, 6 Apr 2018 22:46:22 -0700 (PDT) X-Google-Smtp-Source: AIpwx49SaR29ZcSm5yiBxrPAq6t8rIC1vYSBEHraVkOVI3B6pg9l9t7HFY52IcEwB7pitOit0E7f X-Received: by 2002:a17:902:8e83:: with SMTP id bg3-v6mr30634340plb.144.1523079982849; Fri, 06 Apr 2018 22:46:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523079982; cv=none; d=google.com; s=arc-20160816; b=n0QemN/9UFuWFugYdOUkp8CMVAmaw+DIl3u2brYa1F9f+fkebLH0/E34hedYYAB+HL I97XOq9Y1mM14ibYQoU7vUlHE/YHhFH7kaRjeZn9ge610tLfEAwE/ztkrTYFEBNQdqCJ qytEoKwRqqXm3N8UdE6zjrBvp5dIVLmeh8dqWqiA0Z3+2dM7DBRdw1KGYgNCI+fzHCgM HEh8zoDxDLVLrQajLCvttkAl6VJZDVhXXHRzVXPXogIPPb6jUcF1a+4Vq2J1v84YXafk sM2J7p1CO4iT2F4reW3EzAbN3n/xEn+lApN3cOamZkLsb/F+aP88W/a8D96fFaoJDR5s IV+g== 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=iZYSM2nJQ+hejzqHI+PQoA/nKJ0Dy4c6Ul9NA5cpBLY=; b=tW1t+regb3G/VmCjsYE2mfTdFlqfjudTiyq7PZH1TSzW+jF225ZHFW2KAjBt40Ggff Q6uA6Z4SXNgRU4OacH2GSx/XM+4ubMzewHaf9iL27FbwNsUQHnjI5hMTJIB/2mSv89Z7 HBVwOLvg2t3edD+sPAAEFlxp+iAM2VoKo5ZwhJP2y0jjslL3hDKsEDfQhg7h2dDKLZwV WGOK8mEvirGJ0LiHc5tKMdGKHOOqyZiM51YGOBHX651Vxfwi3Z4Zr9fwVL4C8SDbNQ3y /1MF3G7jFMprja8ydU49uJy/hsE5DYDkQYH9MFMS/fUPn9ai1z/0frPBHbsBDBECvRGp VVzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=HuDtw31Y; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t3si8277460pgq.237.2018.04.06.22.45.45; Fri, 06 Apr 2018 22:46:22 -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=pass header.i=@gmail.com header.s=20161025 header.b=HuDtw31Y; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751128AbeDGFnE (ORCPT + 99 others); Sat, 7 Apr 2018 01:43:04 -0400 Received: from mail-wm0-f67.google.com ([74.125.82.67]:50882 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750941AbeDGFnD (ORCPT ); Sat, 7 Apr 2018 01:43:03 -0400 Received: by mail-wm0-f67.google.com with SMTP id t67so7128255wmt.0 for ; Fri, 06 Apr 2018 22:43:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=iZYSM2nJQ+hejzqHI+PQoA/nKJ0Dy4c6Ul9NA5cpBLY=; b=HuDtw31Y8WvPTYANWy9jygl+Ej5BmQg6b/bg3uWvpj5RcPFaHGFwL6CgNywSqowh7S yFHTCmUDUAybuyS9d9a3EqZSaUT3u8pxF4ZW2pIU1hFgdOlltjvSRaG8Fag4OL4SKnqp dNc3bKizsfy2f+Yb7MhXo7dLtz4FvLdDjQLlbH6Iazpld0Zc7wycAj5oYr6qto/H+r1K 6vaU9lcXyz59vHv3bzWxzvq/SVB96/y00V//BHX/9B++qVTRF1Y23DM44CPx56JkTPlf TA7m47RnmUXOM4t59vbdfoIW9uUNathwJTmcCywFiVtARXrRunD81ItmjIx667ilRMyW uCXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=iZYSM2nJQ+hejzqHI+PQoA/nKJ0Dy4c6Ul9NA5cpBLY=; b=AbIWnJ9uhgXAAimYc2LcylvX/m+U84PINP7CvUuM+rZt5AFhaU2FxaIrewakg/Ygnl CzC+qPgqWPGM3brslgKalFl5OemK+VS4L7hWh7TnrUKcVsKO7CQsW+Gj/EXqak0wi9If tcxJhb7tgtAyKG31+7KQXxXozwAP4t7J8hBYAokoalUV01yFhQi695BEZbpEd7LnX/ah BGvZgEFxoXkIJx8kPIQaKIZX2NFFE0lBw/41h08nG9s+G9r1a65ETLAIwKuQZJnk42Qr AiT3Gth4PqiWkwj+cD8U7SpRyhuTVXOxVASlvSFUB59jm/Cmg3RzlJ7W5pOwkoz9ELRv 6mQg== X-Gm-Message-State: ALQs6tDI+98NDKJIjKjDYWJ55MteRRP0gq0qUwt0WJTGFZ3YshPSWGrD vIhRwroaLkcbfkdPCudjhFXJ+0qJ X-Received: by 10.80.146.170 with SMTP id k39mr10593309eda.110.1523079781929; Fri, 06 Apr 2018 22:43:01 -0700 (PDT) Received: from auth1-smtp.messagingengine.com (auth1-smtp.messagingengine.com. [66.111.4.227]) by smtp.gmail.com with ESMTPSA id b8sm6665575edi.14.2018.04.06.22.43.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Apr 2018 22:43:01 -0700 (PDT) Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailauth.nyi.internal (Postfix) with ESMTP id 7134321333; Sat, 7 Apr 2018 01:42:59 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Sat, 07 Apr 2018 01:42:59 -0400 X-ME-Sender: Received: from localhost (unknown [45.32.128.109]) by mail.messagingengine.com (Postfix) with ESMTPA id B6CAF10255; Sat, 7 Apr 2018 01:42:58 -0400 (EDT) Date: Sat, 7 Apr 2018 13:47:11 +0800 From: Boqun Feng To: Will Deacon Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, peterz@infradead.org, mingo@kernel.org, paulmck@linux.vnet.ibm.com, catalin.marinas@arm.com Subject: Re: [PATCH 10/10] locking/qspinlock: Elide back-to-back RELEASE operations with smp_wmb() Message-ID: <20180407054711.rldyfcmni2wtblyu@tardis> References: <1522947547-24081-1-git-send-email-will.deacon@arm.com> <1522947547-24081-11-git-send-email-will.deacon@arm.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="hchqiumxxg5z2323" Content-Disposition: inline In-Reply-To: <1522947547-24081-11-git-send-email-will.deacon@arm.com> User-Agent: NeoMutt/20171215 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --hchqiumxxg5z2323 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Apr 05, 2018 at 05:59:07PM +0100, Will Deacon wrote: > The qspinlock slowpath must ensure that the MCS node is fully initialised > before it can be reached by another other CPU. This is currently enforced > by using a RELEASE operation when updating the tail and also when linking > the node into the waitqueue (since the control dependency off xchg_tail > is insufficient to enforce sufficient ordering -- see 95bcade33a8a > ("locking/qspinlock: Ensure node is initialised before updating prev->nex= t")). >=20 > Back-to-back RELEASE operations may be expensive on some architectures, > particularly those that implement them using fences under the hood. We > can replace the two RELEASE operations with a single smp_wmb() fence and > use RELAXED operations for the subsequent publishing of the node. >=20 > Cc: Peter Zijlstra > Cc: Ingo Molnar > Signed-off-by: Will Deacon > --- > kernel/locking/qspinlock.c | 32 +++++++++++++++----------------- > 1 file changed, 15 insertions(+), 17 deletions(-) >=20 > diff --git a/kernel/locking/qspinlock.c b/kernel/locking/qspinlock.c > index 3ad8786a47e2..42c61f7b37c5 100644 > --- a/kernel/locking/qspinlock.c > +++ b/kernel/locking/qspinlock.c > @@ -141,10 +141,10 @@ static __always_inline void clear_pending_set_locke= d(struct qspinlock *lock) > static __always_inline u32 xchg_tail(struct qspinlock *lock, u32 tail) > { > /* > - * Use release semantics to make sure that the MCS node is properly > - * initialized before changing the tail code. > + * We can use relaxed semantics since the caller ensures that the > + * MCS node is properly initialized before updating the tail. > */ > - return (u32)xchg_release(&lock->tail, > + return (u32)xchg_relaxed(&lock->tail, > tail >> _Q_TAIL_OFFSET) << _Q_TAIL_OFFSET; > } > =20 > @@ -178,10 +178,11 @@ static __always_inline u32 xchg_tail(struct qspinlo= ck *lock, u32 tail) > for (;;) { > new =3D (val & _Q_LOCKED_PENDING_MASK) | tail; > /* > - * Use release semantics to make sure that the MCS node is > - * properly initialized before changing the tail code. > + * We can use relaxed semantics since the caller ensures that > + * the MCS node is properly initialized before updating the > + * tail. > */ > - old =3D atomic_cmpxchg_release(&lock->val, val, new); > + old =3D atomic_cmpxchg_relaxed(&lock->val, val, new); > if (old =3D=3D val) > break; > =20 > @@ -340,12 +341,17 @@ void queued_spin_lock_slowpath(struct qspinlock *lo= ck, u32 val) > goto release; > =20 > /* > + * Ensure that the initialisation of @node is complete before we > + * publish the updated tail and potentially link @node into the I think it might be better if we mention exactly where we "publish the updated tail" and "link @node", how about: * publish the update tail via xchg_tail() and potentially link * @node into the waitqueue via WRITE_ONCE(->next,..) below. and also add comments below like: > + * waitqueue. > + */ > + smp_wmb(); > + > + /* > * We have already touched the queueing cacheline; don't bother with > * pending stuff. > * > * p,*,* -> n,*,* > - * > - * RELEASE, such that the stores to @node must be complete. * publish the updated tail > */ > old =3D xchg_tail(lock, tail); > next =3D NULL; > @@ -356,15 +362,7 @@ void queued_spin_lock_slowpath(struct qspinlock *loc= k, u32 val) > */ > if (old & _Q_TAIL_MASK) { > prev =3D decode_tail(old); > - > - /* > - * We must ensure that the stores to @node are observed before > - * the write to prev->next. The address dependency from > - * xchg_tail is not sufficient to ensure this because the read > - * component of xchg_tail is unordered with respect to the > - * initialisation of @node. > - */ > - smp_store_release(&prev->next, node); /* Eventually link @node to the wait queue */ =09 Thoughts? Regards, Boqun > + WRITE_ONCE(prev->next, node); > =20 > pv_wait_node(node, prev); > arch_mcs_spin_lock_contended(&node->locked); > --=20 > 2.1.4 >=20 --hchqiumxxg5z2323 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCAAdFiEEj5IosQTPz8XU1wRHSXnow7UH+rgFAlrIW1wACgkQSXnow7UH +rgkAQf/SS2aZeHDOVr/tDVYJegkRpetMU7oWjRIZpASQLwwvdYwNO5SpiyOHZXO rYvoKW7GR/DLHdq9HyCclqURYNQ7RoyNTjK8VikbNOgMi661e8CktwB/hmCDQLfu H+2mgM4/jZWJ++yPVFZzi7VUK3mHOkK5iqFGxLseSIKAyqiN/oMKZkhPWo3fo9NO VeWCNU5Alhs+ZI1fCos+giafuVIxb+w2n9n8a8kxzieF69CcU2wbDcCdM8BWXLrY M8zls2UtyOKroxhSKGs0bA2y7aRAWEuDr7hAaXr2PoJmy8dLTGrSjrFaoV/bGWQO peJP4nTLQ/z442/xjSGWuVnEACd42Q== =p9TP -----END PGP SIGNATURE----- --hchqiumxxg5z2323--