Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp566474imu; Thu, 20 Dec 2018 01:29:31 -0800 (PST) X-Google-Smtp-Source: AFSGD/WjsOYp+1MVN7O3nCZhErn2nr96IUUA5Hq6nMRIi+SscpkNfWNuWY8jJa6FrnmkZ5CeQxvs X-Received: by 2002:a62:7e93:: with SMTP id z141mr23184014pfc.239.1545298171485; Thu, 20 Dec 2018 01:29:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545298171; cv=none; d=google.com; s=arc-20160816; b=GkbuRGduTuSRMHlxbs98hJE++lLPxaHbIGQKo/uVoKVo6ltMO5eMD0Y/NZ3XB+2TxK dlKwyzTyIpUZaKI9NCxB0H9ly0nzoE2Y7GOFzyun8jyMidLakJhMpAN0EoRHQ849TKan 64rdHfZrYkZQuudONawWh8VsYkRASWHpmIpgXginGTJ9uFsYjl7JcBXgu52RvMWtep0e 32pozpunbPxPvdbazalpF0OEHeh/Y59YtWHCCF1WKQK8vsh1RRIoKvIu3PeqpY/w1qFJ gMJXyHO/B1l4c/ytKUPDUtQ66vp/pz1b85uPNMZiMKdVpGTPKys6ZutCfzwHpLa//4x2 FmeQ== 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=wXFIxNMfZE6u2vNOJdMk/7JXlTYKyVirjY4l3wFklEY=; b=YpxJDQ1OYvo3wMR0eg39G8N4k+TQFDx6QjLf73B+byAIkYA4lZyf8paJbBS3OQ6s6f 2RSd1qoGgzfRKTSso0BLQEBVC9KzreW+1bD5MqugRysaWosgKi0aUwSe8863rGxvblYF rGbPKFxyjsOP+c1KFvCue0vGcf3QQ/eh/3hLjrSeW39AvAGaiJP4hfs8N+CJK0frHVHG 7PDU11AepkDkkywxkAZSr/S6n8nCx5Za6eEzxcu123nh5Z1BoY3WGFYRWYj3btFbP95B OF9fNjLOQg34UlYEC1YjW5k/Ye8JmF03ElY3LOG/51H8brSuS3E3MtrfPF2EOUUr6P7h l6ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=qFUiiKjf; 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 b91si16769232plb.11.2018.12.20.01.29.15; Thu, 20 Dec 2018 01:29:31 -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=qFUiiKjf; 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 S1732067AbeLTJ0O (ORCPT + 99 others); Thu, 20 Dec 2018 04:26:14 -0500 Received: from mail.kernel.org ([198.145.29.99]:51982 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732038AbeLTJ0K (ORCPT ); Thu, 20 Dec 2018 04:26:10 -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 C91E521852; Thu, 20 Dec 2018 09:26:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1545297969; bh=rn5pU5ghmL3ypDq+GsBYcQ44BoqIWc3MzRFo9MhUMDQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qFUiiKjfIBJvHdTI8xMK69tCW8UOrPrWqhLk8RMIkeqPomnOT8OkS+nWYbylnf31F sat6EE8584lfCork/S9S0foHbr2vvZ3xhUJlJLL/Wm29rcgk52StTQ8AJnQ0cRX+t9 vy0e+7P5v3Qpzlyo6urJ7licPyy/pzByAjmrReSE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Waiman Long , Will Deacon , "Peter Zijlstra (Intel)" , Linus Torvalds , Thomas Gleixner , boqun.feng@gmail.com, linux-arm-kernel@lists.infradead.org, paulmck@linux.vnet.ibm.com, Ingo Molnar , Sebastian Andrzej Siewior , Sasha Levin Subject: [PATCH 4.14 22/72] locking/qspinlock: Bound spinning on pending->locked transition in slowpath Date: Thu, 20 Dec 2018 10:18:21 +0100 Message-Id: <20181220085923.214216778@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20181220085922.332225035@linuxfoundation.org> References: <20181220085922.332225035@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.14-stable review patch. If anyone has any objections, please let me know. ------------------ 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 Signed-off-by: Sasha Levin --- kernel/locking/qspinlock.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/kernel/locking/qspinlock.c b/kernel/locking/qspinlock.c index d880296245c5..18161264227a 100644 --- a/kernel/locking/qspinlock.c +++ b/kernel/locking/qspinlock.c @@ -76,6 +76,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. @@ -306,13 +318,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--); } /* -- 2.19.1