Received: by 10.213.65.68 with SMTP id h4csp2162902imn; Thu, 5 Apr 2018 10:01:25 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+ryGsjwZOnkqH6VQ4zRhjGf8PcrWRMN1+XYSVEJ9CweCUFuhVf7Mimh1shA1yKE2mx0Mz4 X-Received: by 2002:a17:902:207:: with SMTP id 7-v6mr23994332plc.261.1522947685732; Thu, 05 Apr 2018 10:01:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522947685; cv=none; d=google.com; s=arc-20160816; b=PmEN/bbeItAqGd5QSR9/tOcTA7Hir/nF2Th+wDHbEo/zgZenZ1gu6wntf6+OUoAZFe gFJ3P6BC0fEOlqMD/B7LLWyQ2Wj2ZpDYgczcKta/zSmPywnh1OaxGtg2fo7Zmg4GHPG8 VX8xF6ANfdeV8VvUrZfs0+6shgCWeInPe7TrMohdnF+S8G/JPe+7uvuqbj47Z74IrZdy hj5K0bpVO2A7BF0z4boAUQcKiwYm3Wir2clV/hXRJNjyd/hFFhlGaLCLllTobJvEeoVu 2EPmyuMua2c2IwjcuLjDaah4rKenbi4glj7wNwQtEjQRUSaJ68EMJDiaPQdPbr8Bbp4s kDKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=7meFwI0hsLGAO8hGmKmgQ28G1uBBJm6cf4uagWRfbkU=; b=RZi2Y7zpTbZIaux2stuNc8/x/2AB5n+7NlKjXnDmOhTbEtV0ePiMewiRc6A5+7g3nk oPoxzeWoA8A4vsapnC00kRJ3zonfibLmMgHE91KyHCRVlQhEO12/bGTRfQykIfrjzv2S Spm/p4XD6qKaa3aATSLowt+Db5jmk5Ym/Ojm3v+0sbu+Mx0nN2RGNHCICji+Is6bg4NS OYWE4Heibg/GmrsDLnYDsUcJ1QSxrHERQ6G9KlBMS5mshq3lsuAcWFj/hnanvV6CGGtE XO6lJdgD5mbV1g2CUBleSjpf0I8hat6m2lE+F2yj5SkLfY3fl8asSsR4xRBw7f45azDY i+Dg== ARC-Authentication-Results: i=1; mx.google.com; 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 b63si5722899pgc.72.2018.04.05.10.01.11; Thu, 05 Apr 2018 10:01:25 -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; 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 S1751813AbeDEQ7Y (ORCPT + 99 others); Thu, 5 Apr 2018 12:59:24 -0400 Received: from foss.arm.com ([217.140.101.70]:57404 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751469AbeDEQ64 (ORCPT ); Thu, 5 Apr 2018 12:58:56 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 2F6DB16BA; Thu, 5 Apr 2018 09:58:56 -0700 (PDT) Received: from edgewater-inn.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 01D353F7F3; Thu, 5 Apr 2018 09:58:56 -0700 (PDT) Received: by edgewater-inn.cambridge.arm.com (Postfix, from userid 1000) id 306311AE55AC; Thu, 5 Apr 2018 17:59:09 +0100 (BST) From: Will Deacon To: linux-kernel@vger.kernel.org Cc: linux-arm-kernel@lists.infradead.org, peterz@infradead.org, mingo@kernel.org, boqun.feng@gmail.com, paulmck@linux.vnet.ibm.com, catalin.marinas@arm.com, Will Deacon Subject: [PATCH 10/10] locking/qspinlock: Elide back-to-back RELEASE operations with smp_wmb() Date: Thu, 5 Apr 2018 17:59:07 +0100 Message-Id: <1522947547-24081-11-git-send-email-will.deacon@arm.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1522947547-24081-1-git-send-email-will.deacon@arm.com> References: <1522947547-24081-1-git-send-email-will.deacon@arm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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->next")). 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. Cc: Peter Zijlstra Cc: Ingo Molnar Signed-off-by: Will Deacon --- kernel/locking/qspinlock.c | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) 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_locked(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; } @@ -178,10 +178,11 @@ static __always_inline u32 xchg_tail(struct qspinlock *lock, u32 tail) for (;;) { new = (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 = atomic_cmpxchg_release(&lock->val, val, new); + old = atomic_cmpxchg_relaxed(&lock->val, val, new); if (old == val) break; @@ -340,12 +341,17 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) goto release; /* + * Ensure that the initialisation of @node is complete before we + * publish the updated tail and potentially link @node into the + * 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. */ old = xchg_tail(lock, tail); next = NULL; @@ -356,15 +362,7 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) */ if (old & _Q_TAIL_MASK) { prev = 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); + WRITE_ONCE(prev->next, node); pv_wait_node(node, prev); arch_mcs_spin_lock_contended(&node->locked); -- 2.1.4