From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933715AbeBMP2s (ORCPT ); Tue, 13 Feb 2018 10:28:48 -0500 Received: from terminus.zytor.com ([198.137.202.136]:52885 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933515AbeBMP2p (ORCPT ); Tue, 13 Feb 2018 10:28:45 -0500 Date: Tue, 13 Feb 2018 07:27:21 -0800 From: tip-bot for Will Deacon Message-ID: Cc: tglx@linutronix.de, torvalds@linux-foundation.org, linux-kernel@vger.kernel.org, hpa@zytor.com, mingo@kernel.org, will.deacon@arm.com, peterz@infradead.org Reply-To: will.deacon@arm.com, peterz@infradead.org, hpa@zytor.com, mingo@kernel.org, torvalds@linux-foundation.org, tglx@linutronix.de, linux-kernel@vger.kernel.org In-Reply-To: <1518528177-19169-2-git-send-email-will.deacon@arm.com> References: <1518528177-19169-2-git-send-email-will.deacon@arm.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:locking/urgent] locking/qspinlock: Ensure node is initialised before updating prev->next Git-Commit-ID: 95bcade33a8af38755c9b0636e36a36ad3789fe6 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 95bcade33a8af38755c9b0636e36a36ad3789fe6 Gitweb: https://git.kernel.org/tip/95bcade33a8af38755c9b0636e36a36ad3789fe6 Author: Will Deacon AuthorDate: Tue, 13 Feb 2018 13:22:56 +0000 Committer: Ingo Molnar CommitDate: Tue, 13 Feb 2018 14:50:14 +0100 locking/qspinlock: Ensure node is initialised before updating prev->next When a locker ends up queuing on the qspinlock locking slowpath, we initialise the relevant mcs node and publish it indirectly by updating the tail portion of the lock word using xchg_tail. If we find that there was a pre-existing locker in the queue, we subsequently update their ->next field to point at our node so that we are notified when it's our turn to take the lock. This can be roughly illustrated as follows: /* Initialise the fields in node and encode a pointer to node in tail */ tail = initialise_node(node); /* * Exchange tail into the lockword using an atomic read-modify-write * operation with release semantics */ old = xchg_tail(lock, tail); /* If there was a pre-existing waiter ... */ if (old & _Q_TAIL_MASK) { prev = decode_tail(old); smp_read_barrier_depends(); /* ... then update their ->next field to point to node. WRITE_ONCE(prev->next, node); } The conditional update of prev->next therefore relies on the address dependency from the result of xchg_tail ensuring order against the prior initialisation of node. However, since the release semantics of the xchg_tail operation apply only to the write portion of the RmW, then this ordering is not guaranteed and it is possible for the CPU to return old before the writes to node have been published, consequently allowing us to point prev->next to an uninitialised node. This patch fixes the problem by making the update of prev->next a RELEASE operation, which also removes the reliance on dependency ordering. Signed-off-by: Will Deacon Acked-by: Peter Zijlstra (Intel) Cc: Linus Torvalds Cc: Thomas Gleixner Link: http://lkml.kernel.org/r/1518528177-19169-2-git-send-email-will.deacon@arm.com Signed-off-by: Ingo Molnar --- kernel/locking/qspinlock.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/kernel/locking/qspinlock.c b/kernel/locking/qspinlock.c index 38ece03..348c8ce 100644 --- a/kernel/locking/qspinlock.c +++ b/kernel/locking/qspinlock.c @@ -408,14 +408,15 @@ queue: */ if (old & _Q_TAIL_MASK) { prev = decode_tail(old); + /* - * The above xchg_tail() is also a load of @lock which - * generates, through decode_tail(), a pointer. The address - * dependency matches the RELEASE of xchg_tail() such that - * the subsequent access to @prev happens after. + * 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. */ - - WRITE_ONCE(prev->next, node); + smp_store_release(&prev->next, node); pv_wait_node(node, prev); arch_mcs_spin_lock_contended(&node->locked);