From patchwork Tue Mar 28 16:54:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Andrzej Siewior X-Patchwork-Id: 76218 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2381823vqo; Tue, 28 Mar 2023 10:20:37 -0700 (PDT) X-Google-Smtp-Source: AKy350ZykF8JJdxNPi1+RbKaPvexo+OxwjyF/cn5WkJHOLdbl+nMzH+vs9Hmqr3uMG3M1qwlDFCA X-Received: by 2002:a17:906:d207:b0:92b:b4d9:3f07 with SMTP id w7-20020a170906d20700b0092bb4d93f07mr15938013ejz.14.1680024037135; Tue, 28 Mar 2023 10:20:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680024037; cv=none; d=google.com; s=arc-20160816; b=iwwa2sepwBUMMO3Cc+dnnU4YVXk7Sh+0nVWWNOrb6iseKqVe3yncCKztfd2leZgwrl FXEMQ/pf6ltdZEY1fVFIE6mue3PMKLMk63Oak71CqzDnrAWFgkk+c1pUo2gQ1S9gBx94 uJK95/ykhThBZPbf2p3n1neq7RzZY+D15SWobug811tdW8ufv9uMIq4NX4Lj0nR78t6G N3GWrayyMgd517uT+V1bGRVQNJxraZ4ur3KTirZ9ixN8NMmSejEgZY1Q4l9PGadJNhSE jCnOxdT2FAeZ9Yuntby5LIGsTnvDzreZEgwbxa2usZoqY1s4qVTLeuU/wA/zDFklKjH0 7cSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:dkim-signature :dkim-signature:date; bh=Sk9W5B4mLybL4P5YFSo3bhVLXYVTDyOOwN7NOF+D6nQ=; b=FsiOSOQJ8VpcsvcjFONn6xuS22sCnHeWu2/d8pyXtJLMJ46Z/uyk8AXwRwPasYB56g ahcUQ654paMVlBYHZ/5qH4IkEt7XT6f84XWdb9jStVaRCUpVmx8sENltzykXTq0hyujd 5u3XTIamNhCNWSKDOgN2rkrSlnDoSOrxqHyky6b5udirZvdfMoUhObSN3Fcfp6XgjVYZ apQEKpXwLAyBlPsB+u6lj2W2ILzKBeeu1dS58V3neV7wiFj5n1olgWfvxSMztmsp4wtN dYzoaES0EOzKUUEzCVTAcqgy2snlabTqGbzX4HZRvzxtaEdkKtabcciZ34JH4+hX11bz NjRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=nH13hCV0; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f9-20020a17090631c900b009228f7d61e6si26597855ejf.580.2023.03.28.10.20.12; Tue, 28 Mar 2023 10:20:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=nH13hCV0; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230375AbjC1Qyg (ORCPT + 99 others); Tue, 28 Mar 2023 12:54:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229801AbjC1Qye (ORCPT ); Tue, 28 Mar 2023 12:54:34 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 942BB61AB for ; Tue, 28 Mar 2023 09:54:33 -0700 (PDT) Date: Tue, 28 Mar 2023 18:54:30 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1680022471; 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: in-reply-to:in-reply-to:references:references; bh=Sk9W5B4mLybL4P5YFSo3bhVLXYVTDyOOwN7NOF+D6nQ=; b=nH13hCV0vxF5SzhySjDO+gNg9Qio+BdJHSewkitpHh/ZxqQg2EGrB3Np363ObvmRuE7jmn X8t/GdfaK0BP3Z0owNSjosVR3sndUjYl/s7ZrLuLm1FiBol7VsjzMdzBIL4PAGNWGVoBiQ lVVUY1Oci541iQWL7IJ+JPDvun9xPQFDUONjAkDqHIAfRCPgrrHbtwSvulVuQLV8GevwWH DOZRke+bn0A/l4lE0QUHsweC7XAOHSDf7VqNeVqynrZnkVb6mm2ehGjZlj5HAFDu8rHlWS kmj+zh7kYk5gciZw4PDDj7wYQViFg+ZoYixetCZsJrBgCX0Cy95KqF6+iWZ+PA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1680022471; 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: in-reply-to:in-reply-to:references:references; bh=Sk9W5B4mLybL4P5YFSo3bhVLXYVTDyOOwN7NOF+D6nQ=; b=6g9ZJUlK3spzJpRqwfE/oiN6P8UR9zzDk0C9NzM1ipADGHxikilaP35kAaDjDV8KJv6vB0 t2MeNlbP85Nq1EBg== From: Sebastian Andrzej Siewior To: Thomas Gleixner , linux-kernel@vger.kernel.org Cc: Crystal Wood , John Keeping , Boqun Feng , Ingo Molnar , Peter Zijlstra , Waiman Long , Will Deacon Subject: [PATCH] locking/rtmutex: Do the trylock-slowpath with DEBUG_RT_MUTEXES enabled. Message-ID: <20230328165430.9eOXd-55@linutronix.de> References: <20230322162719.wYG1N0hh@linutronix.de> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20230322162719.wYG1N0hh@linutronix.de> X-Spam-Status: No, score=-2.5 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761632884904094883?= X-GMAIL-MSGID: =?utf-8?q?1761632884904094883?= With DEBUG_RT_MUTEXES enabled the fast-path locking (rt_mutex_cmpxchg_acquire()) always fails. This leads to the invocation of blk_flush_plug() even if the lock is not acquired which is unnecessary and avoids batch processing of requests. rt_mutex_slowtrylock() performs the trylock-slowpath and acquires the lock if possible. __rt_mutex_trylock() performs the fastpath try-lock and the slowpath trylock. The latter is not desired in the non-debug case because it fails very often even after rt_mutex_owner() reported that there is no owner. Here some numbers from a boot up + a few FS operations, hackbench: - total __rt_mutex_lock() -> __rt_mutex_trylock() invocations with no owner: 32160 - success: 189 - failed: 31971 - RT_MUTEX_HAS_WAITERS was set the whole time: 27469 - owner appeared after the wait_lock has been obtained: 4502 The slowlock trylock failed in most cases without an owner because a waiter was pending and did not acquire the lock yet. The few cases in which it succeeded were because the pending bit was cleared after the wait_lock was acquired. Based on these numbers, rt_mutex_slowtrylock() in the non-DEBUG case adds just overhead without contributing anything to the locking process. In a dist-upgrade test with DEBUG_RT_MUTEXES enabled, the here proposed rt_mutex_slowtrylock() optimisation acquired all locks with current->plug set and avoided a blk_flush_plug() invocation. Use rt_mutex_slowtrylock() in the DEBUG_RT_MUTEXES case to acquire the lock instead the disabled rt_mutex_cmpxchg_acquire(). Signed-off-by: Sebastian Andrzej Siewior --- On 2023-03-22 17:27:21 [+0100], To Thomas Gleixner wrote: > > Aside of that for CONFIG_DEBUG_RT_MUTEXES=y builds it flushes on every > > lock operation whether the lock is contended or not. > > For mutex & ww_mutex operations. rwsem is not affected by > CONFIG_DEBUG_RT_MUTEXES. As for mutex it could be mitigated by invoking > try_to_take_rt_mutex() before blk_flush_plug(). This fixes the problem. I only observed blk_flush_plug() invocations from down_read()/rwbase_read_lock() and down() which are not affected by CONFIG_DEBUG_RT_MUTEXES. I haven't observed anything in the ww-mutex path so we can ignore it or do something similar to this. kernel/locking/rtmutex.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/kernel/locking/rtmutex.c b/kernel/locking/rtmutex.c index c1bc2cb1522cb..08c599a5089a2 100644 --- a/kernel/locking/rtmutex.c +++ b/kernel/locking/rtmutex.c @@ -1698,9 +1698,18 @@ static int __sched rt_mutex_slowlock(struct rt_mutex_base *lock, static __always_inline int __rt_mutex_lock(struct rt_mutex_base *lock, unsigned int state) { + /* + * With DEBUG enabled cmpxchg trylock will always fail. Instead of + * invoking blk_flush_plug() try the trylock-slowpath first which will + * succeed if the lock is not contended. + */ +#ifdef CONFIG_DEBUG_RT_MUTEXES + if (likely(rt_mutex_slowtrylock(lock))) + return 0; +#else if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) return 0; - +#endif /* * If we are going to sleep and we have plugged IO queued, make sure to * submit it to avoid deadlocks.