Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp80085pxb; Mon, 13 Sep 2021 13:28:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx62bEoM5Fm++CZASw7lNJtPTSEqFrn6AObSJ8VsrpoN4IHp5rt+0MGCW71KbbgvIFOj7iq X-Received: by 2002:a17:906:eca7:: with SMTP id qh7mr9662151ejb.45.1631564937258; Mon, 13 Sep 2021 13:28:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631564937; cv=none; d=google.com; s=arc-20160816; b=Fp5ty88g3fkysk5e6/zDXjq3hhhr9tRRnaM3jkOBqtIUFOE16rwRuojyxQI6HjB+Iu TpgAIAsTVOQnzytrLwSPC7Nht816GCjX55zh3CG464n4Loiq1nrV7QIANrd2BlOMN36F cNlhc0vQ+tAZA+bOVksgHZVMSx6zsWbsU8UMIRc+EBBYW6ojgsDfxSM/o5e8MiYoCaE/ HoZdvrQNqeIvWcw3TJStBsKDXHN/K2dMBKr58p6U4u+e7n4EXNSJ4OvYGA+d7gwWVs4P jgButBfVqpCwYxj5CazNeYmBQH++bFzzOd1/agcRwm612WokX/7mfx6nh94RtqwD/zO6 oFuA== 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=cUmJwRffNo8qNBUsEaLR8+Ju05zQeyvBfqCr4bxtcNU=; b=xQ6htbKF7sog4FdAXhyPNKSIEpy/MnjTiOfIYcNT4PFNXU7eLFOvm5FVnhUprEPg8t ifkeVkmRX4LD0GXG/CKlmLZj7Jo7XigR5TrtDckBQOeMMhaZFbQChhbYEBZaVH/JJbza 9ysQjlXWqfh/dT1IrG4zMjjeKqcWOxuhK8uKnw3m/ZhCto3cy7I2+XDZ8TzGhZOdzrXD GhpbD3Q6uQBQ+HIKZzXT8dXwgXA3mJNJekUd/TLtUsVebpYmwpiSPt+gtITon4Su7q3M 8mEWhb3uafNW8Q7NaDf+wNXlR0XImzrUzHxL8r7U+MexZi7AqbbA2Ok8aioOnV1W9AIm FNng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=YPC6y7Kj; 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 z4si9648246ejl.76.2021.09.13.13.28.33; Mon, 13 Sep 2021 13:28:57 -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=YPC6y7Kj; 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 S244586AbhIMNxX (ORCPT + 99 others); Mon, 13 Sep 2021 09:53:23 -0400 Received: from mail.kernel.org ([198.145.29.99]:56780 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244534AbhIMNtd (ORCPT ); Mon, 13 Sep 2021 09:49:33 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 81852615E2; Mon, 13 Sep 2021 13:33:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631539998; bh=w1dl9n3VmvajdyqzoIJkmuvzpccfkq+oNpxW6tnphF0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YPC6y7Kjb87eTAsCaxp6x9D91hiL1bYx1/oHoH+CctkDaM7MDfM4EV+L0swpIkkE8 R54Jx2bNeGCpjruu/1b+b0h9lz1J8LeHRto8s4yuqnnfxvHtVCnEHX3HAOSmIN36Vc sOSVJGDvsJ4r7Aum2taEbRV76XJ3q+8k+8AEFEs4= 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.13 001/300] locking/mutex: Fix HANDOFF condition Date: Mon, 13 Sep 2021 15:11:02 +0200 Message-Id: <20210913131109.309104153@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210913131109.253835823@linuxfoundation.org> References: <20210913131109.253835823@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 013e1b08a1bf..a03d3d3ff886 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -928,7 +928,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; @@ -1007,6 +1006,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 @@ -1035,15 +1036,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