From nobody Sun Apr 19 04:02:33 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EBD13C43334 for ; Wed, 6 Jul 2022 14:00:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233184AbiGFOAP (ORCPT ); Wed, 6 Jul 2022 10:00:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231561AbiGFN7v (ORCPT ); Wed, 6 Jul 2022 09:59:51 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5F47718B39 for ; Wed, 6 Jul 2022 06:59:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1657115985; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=xee+8ukBmNojfXI8l7dxzoj4RSqG2t1cV1iXsHkpJW4=; b=BekWSA7gAbmPEnjaErCuTvhLE/xIe5fEwnjV4VKmJ8qjso2VnMRqGk0YdRIL3jDJjGBscN Th8VYlViiNs9Sz+9fJJs0IPfpT0hTAmmABNqeCMMcsMuF+tfMLAgLFCtRhiks9Rgxn9hkS yBaPPE0xEK2xvUZnSYJH+4/o68vfRMY= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-622-OmgxzY1MNcGFFydUWZKKMA-1; Wed, 06 Jul 2022 09:59:34 -0400 X-MC-Unique: OmgxzY1MNcGFFydUWZKKMA-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1C0A4857A87; Wed, 6 Jul 2022 13:59:34 +0000 (UTC) Received: from llong.com (unknown [10.22.8.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id A3B9840315E; Wed, 6 Jul 2022 13:59:33 +0000 (UTC) From: Waiman Long To: Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: linux-kernel@vger.kernel.org, Thomas Gleixner , Sebastian Andrzej Siewior , Juri Lelli , Mike Stowell , Waiman Long Subject: [PATCH v3] locking/rtmutex: Limit # of lock stealing for non-RT waiters Date: Wed, 6 Jul 2022 09:59:16 -0400 Message-Id: <20220706135916.980580-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Commit 48eb3f4fcfd3 ("locking/rtmutex: Implement equal priority lock stealing") allows unlimited number of lock stealing's for non-RT tasks. That can lead to lock starvation of non-RT top waiter tasks if there is a constant incoming stream of non-RT lockers. This can cause rcu_preempt self-detected stall or even task lockup in PREEMPT_RT kernel. For example, [77107.424943] rcu: INFO: rcu_preempt self-detected stall on CPU [ 1249.921363] INFO: task systemd:2178 blocked for more than 622 seconds. Avoiding this problem and ensuring forward progress by limiting the number of times that a lock can be stolen from each waiter. This patch sets a threshold of 32. That number is arbitrary and can be changed if needed. Fixes: 48eb3f4fcfd3 ("locking/rtmutex: Implement equal priority lock steali= ng") Signed-off-by: Waiman Long --- kernel/locking/rtmutex.c | 9 ++++++--- kernel/locking/rtmutex_common.h | 8 ++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) [v3: Increase threshold to 32 and add rcu_preempt self-detected stall] =20 diff --git a/kernel/locking/rtmutex.c b/kernel/locking/rtmutex.c index 7779ee8abc2a..bdddb3dc36c2 100644 --- a/kernel/locking/rtmutex.c +++ b/kernel/locking/rtmutex.c @@ -359,10 +359,13 @@ static inline bool rt_mutex_steal(struct rt_mutex_wai= ter *waiter, if (rt_prio(waiter->prio) || dl_prio(waiter->prio)) return false; =20 - return rt_mutex_waiter_equal(waiter, top_waiter); -#else - return false; + if (rt_mutex_waiter_equal(waiter, top_waiter) && + (top_waiter->nr_steals < RT_MUTEX_LOCK_STEAL_MAX)) { + top_waiter->nr_steals++; + return true; + } #endif + return false; } =20 #define __node_2_waiter(node) \ diff --git a/kernel/locking/rtmutex_common.h b/kernel/locking/rtmutex_commo= n.h index c47e8361bfb5..d5eeba955f07 100644 --- a/kernel/locking/rtmutex_common.h +++ b/kernel/locking/rtmutex_common.h @@ -26,6 +26,7 @@ * @task: task reference to the blocked task * @lock: Pointer to the rt_mutex on which the waiter blocks * @wake_state: Wakeup state to use (TASK_NORMAL or TASK_RTLOCK_WAIT) + * @nr_steals: Number of times the lock is stolen * @prio: Priority of the waiter * @deadline: Deadline of the waiter if applicable * @ww_ctx: WW context pointer @@ -36,11 +37,17 @@ struct rt_mutex_waiter { struct task_struct *task; struct rt_mutex_base *lock; unsigned int wake_state; + unsigned int nr_steals; int prio; u64 deadline; struct ww_acquire_ctx *ww_ctx; }; =20 +/* + * The maximum number of times where lock can be stolen per waiter. + */ +#define RT_MUTEX_LOCK_STEAL_MAX (1U << 5) + /** * rt_wake_q_head - Wrapper around regular wake_q_head to support * "sleeping" spinlocks on RT @@ -194,6 +201,7 @@ static inline void rt_mutex_init_waiter(struct rt_mutex= _waiter *waiter) RB_CLEAR_NODE(&waiter->tree_entry); waiter->wake_state =3D TASK_NORMAL; waiter->task =3D NULL; + waiter->nr_steals =3D 0; } =20 static inline void rt_mutex_init_rtlock_waiter(struct rt_mutex_waiter *wai= ter) --=20 2.31.1