Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp576493imu; Thu, 20 Dec 2018 01:41:50 -0800 (PST) X-Google-Smtp-Source: AFSGD/WWYoykCWqJFAeDjGvC/0T7uDTsSpl5zFppNBNHdkiS/p/co5prd7C+R1d7GBLt8lJpG1du X-Received: by 2002:a62:d0c1:: with SMTP id p184mr23471409pfg.245.1545298909956; Thu, 20 Dec 2018 01:41:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545298909; cv=none; d=google.com; s=arc-20160816; b=We9xtZHbRuES93PJE6xSXIomJCsOdLUPUyO6NMa9PBv/mXP2VeCOh1E7rSbtx7Nx5d Nt06BQjrluPG4++oI9AzKppN4o7W0CUeqF7Mi5gQDbAdHTPIdTDpGdK9rX0dROKjXQLP 8iD44ystyy3qAhNQi8aDd31z2aWdtq2tzwHeirg4kBkgQQJb/Ikw8gwRjMEXdVQ1tKka 3TXlqGHEXhwCGvuGg27zweqb6qg2td/A+ygyltDqWgF7F54F9DwPb2XmQOtmz1axsKTN obNkNHAmq6scNDv2b7QT8wauopk7eHaQZyaUE5ddeQc6ZpMT0VKylPrfjlCrMUxSsRS1 pqwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=7aWNDh0oUkK9nnOy28LJX74zJuFfb410YKj3ejVFMqU=; b=pNdoloZ1yO1/VUsvokuRRKXwIWEejTIHkUFlrBQSlsziJahaFTGDbO8uDuZnlfF1zd CN/iEUMcsvPnr6KCOjEHXUmCMSKHb2+JokfUC5PiJ7RMQh4TaDYo3bIVtPoo5ckl1LGR 3JCtG7PJ4yPJJ62LdQrU89rMlSybiz6Ec4WL8yAPennCUoIOltV5VtwEDuzZB8fXEbp1 rc0C73Ch+WW9k5JcUpcn0IHFdE1dJwXi7qSlplB2ECx/9/XC0tLT95pTsRbW7hRVebcu dUbG8rwTywukLZSaCUTICF6tp2/kcDgIn+b9ZUqLwv7tWwiawhjF2nkF38QqWvLBgtJF hZVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=fXlZ5dvG; 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 y191si17200079pgd.4.2018.12.20.01.41.34; Thu, 20 Dec 2018 01:41:49 -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=fXlZ5dvG; 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 S1732296AbeLTJkH (ORCPT + 99 others); Thu, 20 Dec 2018 04:40:07 -0500 Received: from mail.kernel.org ([198.145.29.99]:40492 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730182AbeLTJY7 (ORCPT ); Thu, 20 Dec 2018 04:24:59 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (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 30DA221852; Thu, 20 Dec 2018 09:24:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1545297898; bh=4wBFu3iuro4VsXgYa69Np6MyHACuQzW9CMlxNlzNMV0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fXlZ5dvGu5Nb3+nbWpPqPc0fnOoo2bbmflpSvTOfFbwMF/rZWIkwr65NOm3HI/nBs eiD2+TCw60M194zySSPWUB977PBWTTRDObMRhJQqGm8FHiJaLTrSs8wyAo4La5EGFB 6X1UMdc7Zyo5TvPfMvALuG40hSnl0yhabwj5e1yU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Will Deacon , "Peter Zijlstra (Intel)" , Linus Torvalds , Thomas Gleixner , Ingo Molnar , Sebastian Andrzej Siewior , Sasha Levin Subject: [PATCH 4.9 21/61] locking/qspinlock: Ensure node is initialised before updating prev->next Date: Thu, 20 Dec 2018 10:18:21 +0100 Message-Id: <20181220085844.590865428@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20181220085843.743900603@linuxfoundation.org> References: <20181220085843.743900603@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ commit 95bcade33a8af38755c9b0636e36a36ad3789fe6 upstream. 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 Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: Sasha Levin --- 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 8710fbe8d26c..6fce84401dba 100644 --- a/kernel/locking/qspinlock.c +++ b/kernel/locking/qspinlock.c @@ -532,14 +532,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); -- 2.19.1