From patchwork Tue Dec 18 22:10:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Andrzej Siewior X-Patchwork-Id: 154213 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp4236450ljp; Tue, 18 Dec 2018 14:11:28 -0800 (PST) X-Google-Smtp-Source: AFSGD/WuTSvCjhJvV4aj+OhEKHVQ/XNkQOmt0QE7SaWNNvJTMvI8RLBvvghYMwTYXiUwQqqOfdkk X-Received: by 2002:a63:cf02:: with SMTP id j2mr16917717pgg.113.1545171088264; Tue, 18 Dec 2018 14:11:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545171088; cv=none; d=google.com; s=arc-20160816; b=M2MV74bGecIG1jxKA7u00E6jOBGmdXKsDjVyXGIsH2MoPGI7fSBHzCLRSYEkDSJUI1 53KdkQ/OPit74ro2xbUkSJIP9Ye2hT/r4DwNI+LbPS+Z0RQpv73BnFr9Usvw1SBZwfMk dVxkeltPGWDBwtW6pSeuRfsp9+lAFu+CjR0b76sM1ePdGxWVlRxuYR8BGgblFXzJk148 1xOmfATccwI0dTkg7Jx7754dGMKVcMJzujYaooIKHM/seJs8V/4Mj8BxkA69+Czy7FoM d0H46oAslv3qbs7Gnj+aMWXfZpAtpwFXTsNkSnV8c8orEZlU9aY9LFipGOSROYXDiZ79 mTeQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=va4BPl0uFoq1vj+3U4yoGesgEZ0r3WJ9T2BRV8XJmY8=; b=Mie/cYiwiNJ7Qt10xt4V0jWPQt+wLJtODRt55GTZVPj8Itjs4rPjx8Qw7uVVHTJ3hf N4K7v3+p90HC/yUjnQlG948wz6rB3ae5JwmT6nDyvcjBrFKeTbi1C6sdkPlk5QLo3oqf dYWEaqbqXyC+4GMk9nEOqMss+Qd+4shIU8lkfyiIl+D9k4sm1XHeWRpo+DqQ2W2rgez5 2ffme9a7jrQazmiMCZtANuszP612JJAF4BitMvh1qhtjHuWgD2HaCDsdda9aBwb+tqNf S87cGuTxDeH82DVwaQ6nvjUA+8NqoHLRNet+n616oBPos2WvSoed6i/BGGk4GPlG8d6s nc3g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=stable-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 c8si13817215pgl.507.2018.12.18.14.11.27; Tue, 18 Dec 2018 14:11:28 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of stable-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 stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727617AbeLRWL0 (ORCPT + 15 others); Tue, 18 Dec 2018 17:11:26 -0500 Received: from Galois.linutronix.de ([146.0.238.70]:57206 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727316AbeLRWLZ (ORCPT ); Tue, 18 Dec 2018 17:11:25 -0500 Received: from localhost ([127.0.0.1] helo=bazinga.breakpoint.cc) by Galois.linutronix.de with esmtp (Exim 4.80) (envelope-from ) id 1gZNaB-000889-Lg; Tue, 18 Dec 2018 23:11:20 +0100 From: Sebastian Andrzej Siewior To: stable@vger.kernel.org Cc: Peter Zijlstra , Will Deacon , Thomas Gleixner , Daniel Wagner , bigeasy@linutronix.de, Waiman Long , Linus Torvalds , boqun.feng@gmail.com, linux-arm-kernel@lists.infradead.org, paulmck@linux.vnet.ibm.com, Ingo Molnar Subject: [PATCH STABLE v4.9 03/10] locking/qspinlock: Bound spinning on pending->locked transition in slowpath Date: Tue, 18 Dec 2018 23:10:42 +0100 Message-Id: <20181218221049.6816-4-bigeasy@linutronix.de> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20181218221049.6816-1-bigeasy@linutronix.de> References: <20181218221049.6816-1-bigeasy@linutronix.de> MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Will Deacon commit 6512276d97b160d90b53285bd06f7f201459a7e3 upstream. If a locker taking the qspinlock slowpath reads a lock value indicating that only the pending bit is set, then it will spin whilst the concurrent pending->locked transition takes effect. Unfortunately, there is no guarantee that such a transition will ever be observed since concurrent lockers could continuously set pending and hand over the lock amongst themselves, leading to starvation. Whilst this would probably resolve in practice, it means that it is not possible to prove liveness properties about the lock and means that lock acquisition time is unbounded. Rather than removing the pending->locked spinning from the slowpath altogether (which has been shown to heavily penalise a 2-threaded locking stress test on x86), this patch replaces the explicit spinning with a call to atomic_cond_read_relaxed and allows the architecture to provide a bound on the number of spins. For architectures that can respond to changes in cacheline state in their smp_cond_load implementation, it should be sufficient to use the default bound of 1. Suggested-by: Waiman Long Signed-off-by: Will Deacon Acked-by: Peter Zijlstra (Intel) Acked-by: Waiman Long Cc: Linus Torvalds Cc: Thomas Gleixner Cc: boqun.feng@gmail.com Cc: linux-arm-kernel@lists.infradead.org Cc: paulmck@linux.vnet.ibm.com Link: http://lkml.kernel.org/r/1524738868-31318-4-git-send-email-will.deacon@arm.com Signed-off-by: Ingo Molnar Signed-off-by: Sebastian Andrzej Siewior --- kernel/locking/qspinlock.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) -- 2.20.1 diff --git a/kernel/locking/qspinlock.c b/kernel/locking/qspinlock.c index 6fce84401dba1..a8da1fc5222eb 100644 --- a/kernel/locking/qspinlock.c +++ b/kernel/locking/qspinlock.c @@ -75,6 +75,18 @@ #define MAX_NODES 4 #endif +/* + * The pending bit spinning loop count. + * This heuristic is used to limit the number of lockword accesses + * made by atomic_cond_read_relaxed when waiting for the lock to + * transition out of the "== _Q_PENDING_VAL" state. We don't spin + * indefinitely because there's no guarantee that we'll make forward + * progress. + */ +#ifndef _Q_PENDING_LOOPS +#define _Q_PENDING_LOOPS 1 +#endif + /* * Per-CPU queue node structures; we can never have more than 4 nested * contexts: task, softirq, hardirq, nmi. @@ -422,13 +434,15 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) return; /* - * wait for in-progress pending->locked hand-overs + * Wait for in-progress pending->locked hand-overs with a bounded + * number of spins so that we guarantee forward progress. * * 0,1,0 -> 0,0,1 */ if (val == _Q_PENDING_VAL) { - while ((val = atomic_read(&lock->val)) == _Q_PENDING_VAL) - cpu_relax(); + int cnt = _Q_PENDING_LOOPS; + val = smp_cond_load_acquire(&lock->val.counter, + (VAL != _Q_PENDING_VAL) || !cnt--); } /*