Received: by 2002:a05:6520:4d:b0:139:a872:a4c9 with SMTP id i13csp2563995lkm; Mon, 20 Sep 2021 18:49:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwoo8z6bhHwlkxpPzubn71yvYTJJo4KRE9J9WlNwDUBC16qktUZHDDkGQU/l3NdfCF/sRSH X-Received: by 2002:a05:6638:348e:: with SMTP id t14mr22179563jal.66.1632188893292; Mon, 20 Sep 2021 18:48:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632188893; cv=none; d=google.com; s=arc-20160816; b=fYC8gPrHDRFk9gdL8xn5nP/dhozuJv2YZKr5GiE0QLNELCwi6CgSOi95R5vt67SZlh OzvCZkpAi/k4qRsQowOeowfJGm2+lix87LVu9ovk4EKK6tLyAamnowe2nI8Y/qNyrmOU FYmZNFyZpfsBBaTn+x9GFOXhM92rx5yDkHxYDwLQi4q6SnyveB/rMh2n9WxjFpOV2rfx 4DFbozYaI9FXPd1zUgeXHaGQXeb/voiTfDM81pxk52NLTHd7bVY88VA+JP/6sRL0uwpF m7opVdWpMLMvHyp4dRqzfycVXux8oa24Dnc7gnF6M12LrwMOVl+H3sM4EZJ2+7s6zMgy lPqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=JvmHaUqLiAD8pWJRtZ6uIBky6yeJPusfCCXWQYo0/f8=; b=QczC+BM5qp9haAbefCAOW4ltT/xX4Jg5W3l0blB9FyXApUDqhxog5CNdwD2rnvGdTB oVrxRXyOvVuxZ7DcVgUy4YcERiNcaZ+IJDFcvX0/y4hwdoJ4Z9n4BeOo27Tvt2cduOt2 KX/lsuG5Yd6IqbkJsIM+k5bNC5MshGWe9Od8Q7hRbBkYAsSH7fgbQ6zwDWp/7lNH3iTf r4eiLyCVkXrQv1zdwrex/5xoMUA/2vrsAL9NmIm5/pE2VU+ypbRcWgu/RT3xBn2XRJy/ ctwRLWysh1dCglxOk5bBcOK23EMIPArP2gGgN1FpKcVTd9eOXoog/fp9iBd3aNl2XIW0 +l0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=RNrPvGKS; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e6si13779349ilu.163.2021.09.20.18.48.02; Mon, 20 Sep 2021 18:48:13 -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=@linuxfoundation.org header.s=korg header.b=RNrPvGKS; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348734AbhITRkQ (ORCPT + 99 others); Mon, 20 Sep 2021 13:40:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:42456 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352054AbhITRhf (ORCPT ); Mon, 20 Sep 2021 13:37:35 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id B3C8C61B40; Mon, 20 Sep 2021 17:06:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1632157620; bh=dGLfehhFTy0eJ6fGMxY1QVBpciXrFxL5nfABSAi8V0Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RNrPvGKSIrtxPBSWTNflRJa109CRnLBCwhFFrgSArDe0xBCjDdoSGf+hqMn4wzmzC jM+Gw48ob5K38L7Oq/CGm0lhqRVs2KfOQDiR+TvWxKT9XL3zb6T3TPdGByKrzpolnF oya/i8qRtmKRDUL7MElmHrria8hLhTStN6+EUs6o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Yanfei Xu , "Peter Zijlstra (Intel)" , Waiman Long , Sasha Levin Subject: [PATCH 4.19 032/293] locking/mutex: Fix HANDOFF condition Date: Mon, 20 Sep 2021 18:39:54 +0200 Message-Id: <20210920163934.366112555@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210920163933.258815435@linuxfoundation.org> References: <20210920163933.258815435@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Peter Zijlstra [ Upstream commit 048661a1f963e9517630f080687d48af79ed784c ] Yanfei reported that setting HANDOFF should not depend on recomputing @first, only on @first state. Which would then give: if (ww_ctx || !first) first = __mutex_waiter_is_first(lock, &waiter); if (first) __mutex_set_flag(lock, MUTEX_FLAG_HANDOFF); But because 'ww_ctx || !first' is basically 'always' and the test for first is relatively cheap, omit that first branch entirely. Reported-by: Yanfei Xu Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Waiman Long Reviewed-by: Yanfei Xu Link: https://lore.kernel.org/r/20210630154114.896786297@infradead.org Signed-off-by: Sasha Levin --- kernel/locking/mutex.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index 354151fef06a..fbc62d360419 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -911,7 +911,6 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx) { struct mutex_waiter waiter; - bool first = false; struct ww_mutex *ww; int ret; @@ -986,6 +985,8 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, set_current_state(state); for (;;) { + bool first; + /* * Once we hold wait_lock, we're serialized against * mutex_unlock() handing the lock off to us, do a trylock @@ -1014,15 +1015,9 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, spin_unlock(&lock->wait_lock); schedule_preempt_disabled(); - /* - * ww_mutex needs to always recheck its position since its waiter - * list is not FIFO ordered. - */ - if (ww_ctx || !first) { - first = __mutex_waiter_is_first(lock, &waiter); - if (first) - __mutex_set_flag(lock, MUTEX_FLAG_HANDOFF); - } + first = __mutex_waiter_is_first(lock, &waiter); + if (first) + __mutex_set_flag(lock, MUTEX_FLAG_HANDOFF); set_current_state(state); /* -- 2.30.2