Received: by 2002:a05:6a10:6d25:0:0:0:0 with SMTP id gq37csp1835767pxb; Mon, 13 Sep 2021 06:36:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyZVKeRR1sB/AqKuhxXlC+NPNgyuCLfXsxw4smLDaSm4HmvXuMRVq+r0DwhZbqpVzNo46b5 X-Received: by 2002:a05:6638:1352:: with SMTP id u18mr9887018jad.147.1631540203262; Mon, 13 Sep 2021 06:36:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631540203; cv=none; d=google.com; s=arc-20160816; b=da6sN3s1NgZC/bgQQFo9FlZTjJ508UVeIL/YDicyLe8Mvjs5QseGVYG8qA9I7AC4zV OVdXgqYm5+146H/plLSyXuLJ0mgev6BpIebJNMMQpwtuN4CV22bu0M5jGiSImBP+8wrI mE0MySEh3KH8WhvM3sBJ31WZR+HMxrbjf/LUCzTCBwOaz9D+wmXgspjbdhpAIahOLrzP vPZYniqS9tK0SD3ws/i8YFuAX5E1kLc/PCp4cnAjzu6ytEgEI1/g68iJFhW6syDp1vv8 wXbHcqCTyTXkc/Wzej+LCaxwRpOtOEBbvcAOdtXAy338n240r0AzuVxfIT4eFbVOpHxu W6KQ== 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=LypbKWDZrcN7FkkxxxGI1wDrgtOLaLqS2517QtwOQwo=; b=WYUJDv8DAgBHkiN4bTZ9JYaCFsZzgDs1Cpw8Jlit846tQ2BrSz0ONuUVSxlG3UujlJ N++CiNpjNdzryl1qCqRO5fT60C0I6NKgP9pbIlSMkji8yrbpp9s7xE8JRAqyabbJ1X5W ghwS/K3fhBeqng3RfqpHl0PZnWGWOD7yzz2co390V/n1VNp0+5x1zEYFcOkVN/VRPwl1 l2WHlkZPmgLMsD+uC3D7Pq+Zmt/6BU+5qLeL10d7CBmCAN1LfBhyiwfwodwpxI2jL74M l+LGXwKmIBc2Wxs4bQHVnFGafY87Q+CEk79qdT9rOZbOO/75A07tFK2tdaTzIfS3xTS7 Ao2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=YfKnLdYD; 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 l6si7318046jac.65.2021.09.13.06.36.30; Mon, 13 Sep 2021 06:36:43 -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=YfKnLdYD; 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 S242741AbhIMNeW (ORCPT + 99 others); Mon, 13 Sep 2021 09:34:22 -0400 Received: from mail.kernel.org ([198.145.29.99]:34836 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240144AbhIMNT4 (ORCPT ); Mon, 13 Sep 2021 09:19:56 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id AAFE261103; Mon, 13 Sep 2021 13:17:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631539068; bh=8GMfLaMzTij4R1fqr5DnHUDWQf97gN4MxHWyK995UkE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YfKnLdYDrB2swM3eqLkI0IjQdLzGgLJixZ5pMlmDa2V4QJ56tu7IBpzh2vIeYjNgK dcWhvSQcz3hpFttWfbPRUIEcGdlvVpcKu4+GX1brELyONH2JrbGssCn8afaxXXfkQ0 ZBHduBmZ9+9KhNQe0uFUso7wUy+j+OUQ5FbP1k/s= 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 5.4 001/144] locking/mutex: Fix HANDOFF condition Date: Mon, 13 Sep 2021 15:13:02 +0200 Message-Id: <20210913131048.024763675@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210913131047.974309396@linuxfoundation.org> References: <20210913131047.974309396@linuxfoundation.org> User-Agent: quilt/0.66 X-stable: review X-Patchwork-Hint: ignore 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 c0c7784f074b..b02fff28221f 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -938,7 +938,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; @@ -1017,6 +1016,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 @@ -1045,15 +1046,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