Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3317749pxk; Mon, 7 Sep 2020 09:22:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyU5EeWwdoPaEG7M0de9cg+5NsZ2Aova6hrCpyoCQeUOgto7wJk1qvhHbK6X+51D00vJtFp X-Received: by 2002:a17:906:cd0d:: with SMTP id oz13mr21363697ejb.212.1599495744284; Mon, 07 Sep 2020 09:22:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599495744; cv=none; d=google.com; s=arc-20160816; b=jjN8TU+QXyRBHzG0qX7cqzoIgApCUmjDd7PjwGuh2XBuJwFkwSCxtE8THIQdFb1Lmo TmT5mQoHK/3vyLEuHS8VFshmf47V/M53Mud21EJjlxIid1ohVs7vOiKLCo/KZzRJk6BG ZUc0H/8o3XCKdiqFs3/P8lQoumjeTz5znR1LD9XT4t5ANhxBtJMAgLvno0fU7mqIeeRz 6cBeKmd4oSfck+Exvdfse0Bfs5GG9Aeptn66XP2/SPo24S9eVTu0JwwNwTG8t/zIGV1J xcy3OY0vzGlEIYL5P2Dgk6KBaMhuc++TELKCBooghb2MSJjgUqi6maYv2ppuv1luCHpp 6+aA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=uYzgV0OU6lvvwl8lnBzm/noOMOF/q9cYFi/lBGcvJGs=; b=QQuEoTIWaBkLpLf76iHvP/K9Nl6Pk+NBlFWQvJy/hiWoXluR1yY89nmOz8c6hFr3h9 bxn1JO0kB8tTTF1n18C4n36nEvOP/wYWi57FFx/a8xNrYPr7GgBdwdniRoZnd1ygVXIz h8nP6j9bVFXkxBC0GON59dCdCcaEFoxN4/qKS5w3H6/9uWqRnOtoAMkn5djfUItfeGnF ZeLS7ogxCryKH2HPTowuSDtx5hixMLhmkzVVpR4IKL8GKRVBsHDUMcbv2gY8OqWZXF3X 8hui8qO0BfM2Kai6BmU7viI+P72k3UYtlAHmRLn3Ze+7bi9PPVVsM+RHcuGpHlVUOgsM +Q3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=xo9Jhn0i; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d3si10253168ejw.357.2020.09.07.09.22.01; Mon, 07 Sep 2020 09:22:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=xo9Jhn0i; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730539AbgIGQVD (ORCPT + 99 others); Mon, 7 Sep 2020 12:21:03 -0400 Received: from mail.kernel.org ([198.145.29.99]:38078 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730518AbgIGQUg (ORCPT ); Mon, 7 Sep 2020 12:20:36 -0400 Received: from willie-the-truck (236.31.169.217.in-addr.arpa [217.169.31.236]) (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 DDFF2207DE; Mon, 7 Sep 2020 16:20:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1599495635; bh=zCSFPl/bVyJEOtJqE/X/0a0Sh3/8r5uhJmDZTmPKcmQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=xo9Jhn0iwN69oqre/7j5ssBiF2Lf5ngeRytPZ8gUVsMXkYmU93SFnKWw67dKBqfKR qRPxZ0/iIenaYqUL6DIBDDIiGBuePXta0Am5i9258BeJOUIktD6PFYRpfPMSLb3UCB aMHNAXvV6lFwpVmcqGBZncZlp0rbi5gwqGZv32LU= Date: Mon, 7 Sep 2020 17:20:31 +0100 From: Will Deacon To: Sultan Alsawaf Cc: Peter Zijlstra , Ingo Molnar , linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/2] locking/mutex: Don't hog RCU read lock while optimistically spinning Message-ID: <20200907162031.GA13172@willie-the-truck> References: <20200807191636.75045-1-sultan@kerneltoast.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200807191636.75045-1-sultan@kerneltoast.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Aug 07, 2020 at 12:16:35PM -0700, Sultan Alsawaf wrote: > From: Sultan Alsawaf > > There's no reason to hold an RCU read lock the entire time while > optimistically spinning for a mutex lock. This can needlessly lengthen > RCU grace periods and slow down synchronize_rcu() when it doesn't brute > force the RCU grace period via rcupdate.rcu_expedited=1. Would be good to demonstrate this with numbers if you can. > Signed-off-by: Sultan Alsawaf > --- > kernel/locking/mutex.c | 25 +++++++++++++++++-------- > 1 file changed, 17 insertions(+), 8 deletions(-) > > diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c > index 5352ce50a97e..cc5676712458 100644 > --- a/kernel/locking/mutex.c > +++ b/kernel/locking/mutex.c > @@ -552,21 +552,31 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner, > { > bool ret = true; > > - rcu_read_lock(); > - while (__mutex_owner(lock) == owner) { > + for (;;) { > + unsigned int cpu; > + bool same_owner; > + > /* > - * Ensure we emit the owner->on_cpu, dereference _after_ > - * checking lock->owner still matches owner. If that fails, > + * Ensure lock->owner still matches owner. If that fails, > * owner might point to freed memory. If it still matches, > * the rcu_read_lock() ensures the memory stays valid. > */ > - barrier(); > + rcu_read_lock(); > + same_owner = __mutex_owner(lock) == owner; > + if (same_owner) { > + ret = owner->on_cpu; > + if (ret) > + cpu = task_cpu(owner); > + } > + rcu_read_unlock(); Are you sure this doesn't break the ww mutex spinning? That thing also goes and looks at the owner, but now it's called outside of the read-side critical section. Will