Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp565067imu; Thu, 20 Dec 2018 01:27:34 -0800 (PST) X-Google-Smtp-Source: AFSGD/WO71chVfEH7CxcfLeFIR2urvGKPcx7ITiIiELBnCI5kvqnqBafVyp7FU4WuJWmLdVQP4fz X-Received: by 2002:a62:546:: with SMTP id 67mr23034753pff.99.1545298054230; Thu, 20 Dec 2018 01:27:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545298054; cv=none; d=google.com; s=arc-20160816; b=C4/eQ0690iw9uyutyLzwuGN1FSIARd2u7W7ybPepGEtriHdx3CkUaZ7SbEx9Xiy8EX n9aA22vynsJ+SHupBhwENY897QHlqlyiOR0Nfmk7ESRD+bQQq3VB+zvNk2WhB7/2bO/B w7Sh7EgWDU2LTR1inqn1Rp3DkAVFaDO8uPznPVLwYa44KTdfAS0Cac09Djd38zX4j41P TT79pdOTv95b7oAPn7TD0dLFcKz2dJLxHHSy1cN8cL2rg9EOU9TkgGEJz8OjzZtM2+ja I7UfcdFg6mtQfi1C/O50/1yLjsb3Wy90yeoWo6WPdkLX8mBVJfeJesr3MnN1gOnmVooe zkLw== 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=Pv9ZBVMqUedDhKsRLBPfAl0AGz8lHGPiVghjyKBKiw4=; b=cLkEwvE7ak+Sko77Q1YDP5DjfK5RVSCddhrDTpQy1LqnGJTQFIc2hJ35La1bWRraw0 ulsO+Gg35NE3KGmFxQxj05fFGvau4L4nVYV5xbF+ZoDG93f4FnQJqPf/8r0dNzECt6qs 4K97ce9W7esMdxhtO9snahvXxxddD2hywA8uyzUb0wwpBwygc7XP6Pm9QhOBv2dVzqQm TiHjNVU/1D2FWoOKVeLIPU1aLS3u97J3T6eBWi6mtlWoEGFuxFEDvU6D9WUCk2JaWbhb UJzFZwJIFRvGekAPCVk1iFm0mBTRLUkFMQJ+qe18PweWGORZGhRzbluAJB/RZRd/Z6wS UvvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=UJYOpMOU; 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 u123si4645902pgb.516.2018.12.20.01.27.18; Thu, 20 Dec 2018 01:27:34 -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=UJYOpMOU; 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 S1731617AbeLTJXf (ORCPT + 99 others); Thu, 20 Dec 2018 04:23:35 -0500 Received: from mail.kernel.org ([198.145.29.99]:54474 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731601AbeLTJXd (ORCPT ); Thu, 20 Dec 2018 04:23:33 -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 8F35F20656; Thu, 20 Dec 2018 09:23:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1545297812; bh=nsZCC066HsfUR6heUfkJZaSGv2Vx60T0A5qY5oweNzQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UJYOpMOUNEhd3NyncOMzFm5HKsmli+e4UXdcYYsqUsxO9wQgkuJoz+UJaPZvqjxUd WXct4uUA1vvlEF/nyjrh80jSh0VK8q/ttOyJ6WTkyDN8umhcnGS0RPPRGGM/GOpAjJ 8RUbdEJhbyrZCUQ1fT2oRg+yvfYHXOg7yN+5aOyk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Will Deacon , "Peter Zijlstra (Intel)" , Waiman Long , 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.9 26/61] locking/qspinlock: Kill cmpxchg() loop when claiming lock from head of queue Date: Thu, 20 Dec 2018 10:18:26 +0100 Message-Id: <20181220085844.783297892@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 c61da58d8a9ba9238250a548f00826eaf44af0f7 upstream. When a queued locker reaches the head of the queue, it claims the lock by setting _Q_LOCKED_VAL in the lockword. If there isn't contention, it must also clear the tail as part of this operation so that subsequent lockers can avoid taking the slowpath altogether. Currently this is expressed as a cmpxchg() loop that practically only runs up to two iterations. This is confusing to the reader and unhelpful to the compiler. Rewrite the cmpxchg() loop without the loop, so that a failed cmpxchg() implies that there is contention and we just need to write to _Q_LOCKED_VAL without considering the rest of the lockword. 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-7-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 | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/kernel/locking/qspinlock.c b/kernel/locking/qspinlock.c index e7ab99a1f438..ba5dc86a4d83 100644 --- a/kernel/locking/qspinlock.c +++ b/kernel/locking/qspinlock.c @@ -581,24 +581,21 @@ locked: * and nobody is pending, clear the tail code and grab the lock. * Otherwise, we only need to grab the lock. */ - for (;;) { - /* In the PV case we might already have _Q_LOCKED_VAL set */ - if ((val & _Q_TAIL_MASK) != tail || (val & _Q_PENDING_MASK)) { - set_locked(lock); - break; - } + + /* In the PV case we might already have _Q_LOCKED_VAL set */ + if ((val & _Q_TAIL_MASK) == tail) { /* * The smp_cond_load_acquire() call above has provided the - * necessary acquire semantics required for locking. At most - * two iterations of this loop may be ran. + * necessary acquire semantics required for locking. */ old = atomic_cmpxchg_relaxed(&lock->val, val, _Q_LOCKED_VAL); if (old == val) - goto release; /* No contention */ - - val = old; + goto release; /* No contention */ } + /* Either somebody is queued behind us or _Q_PENDING_VAL is set */ + set_locked(lock); + /* * contended path; wait for next if not observed yet, release. */ -- 2.19.1