Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp708005imm; Wed, 18 Jul 2018 09:17:42 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdUiS0/Tbja+mQnMjhDhcDKNEYWfDPHRLwXczDb7T0M/PLJ1wsVOND2Fg3Zsl9klaa5KgdO X-Received: by 2002:a63:7b4d:: with SMTP id k13-v6mr6386066pgn.64.1531930661968; Wed, 18 Jul 2018 09:17:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531930661; cv=none; d=google.com; s=arc-20160816; b=0pmyxXon9T6kH6e6d+iUR4WUxa5j+CuGEEvr5oU4Foa9kbuCXxPUGtXWXxF26rwFOz 9R5FLl8mQvbPB3Ne1NbCzRxroKM2NWGAogBv4jB6C+eUJV0NJReQyHwrfJTWeUSGvR8C Pjv/bmJBZkgZ/0zfdAHBk1nP6bYJJGhRa0eRKCrAeEi7yJltA+nG+rySY8L3BW6Z1HGO mPLg3P2Xe6jXuYL3mOHUjweYYR51LHB7zMU5EC+PpyZqaT9LUFgJhOOqSeeEKL7vRfIh ew1JX3SMfRkuhhKYKjzqOzoTS5rmsTlx5euY06Pmxe4jqFjEiiLLDavzvEtIW/rdaXAb QUSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=J5HnQtSKLybUiWvJjcnTyYv3ba/wnfxzMcayHHdab1g=; b=F0oybX4XAMcjZNCR6QZd8I6nFlPEofNkIfomvyZnx5u4gRcymbGe0BM6c6rIuDrd3d m306HimwkAlNvicwUYWTLJvqNor3y3sKyMTFccaIScotqyop6G+DJbYN1d7BsPU7zL/O 1XjXXU6GjAGuCHH7hL0hC1VIA6z7y/5odG7Wiq17a2fJuvL8sr7qHDrFk+7vnTykNl7t s73kPgkZaCkGVRjm4Lztsq6Hd4qqvXLdIanxwrwMm08FMJUnyG+bUqQlw5Do8NOLMq8m dNUmHvWOZkOZJi2SvBSs36D/sqiSDlJgpALpnzApEIEs+oU58eh8YaMutEMHxIgz+cji LX7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=HLyuriLZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d2-v6si3532871pge.404.2018.07.18.09.17.27; Wed, 18 Jul 2018 09:17:41 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=HLyuriLZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731695AbeGRQzX (ORCPT + 99 others); Wed, 18 Jul 2018 12:55:23 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:52150 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730987AbeGRQzW (ORCPT ); Wed, 18 Jul 2018 12:55:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=J5HnQtSKLybUiWvJjcnTyYv3ba/wnfxzMcayHHdab1g=; b=HLyuriLZ3jqDAwnZhiLoQIt2K Qe7p/HnVYXsYBf0tt99i5hf9nHk7SXHPK9NoGV2DaKU6nLJmfkT7eoZmM/3Aa30CJAAq051VsWL+V +2qPUPew5TnVee2TmWaiUviMeIt1UXdVn7354xiaH37921SCDCYn1h2JOTgtj9QilkyDE+87ssq1j fyLi7rXYe+ig1+Ft+Idpw1+sMtF0NdJZULqMGiMECYyxXPNjsI1bgcuSQqn3J/Zs+zqwx7m5DmHxZ reC3Rr554vI/YN1EvUbC2u7fq9GjGL3YqiJ8exe1Mvyn3Z886u/38XOHm0z6kZBLDQGld7Fvjibnr nx7m8PBeg==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1ffp86-0000AC-Kg; Wed, 18 Jul 2018 16:16:42 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id CDF4020289330; Wed, 18 Jul 2018 18:16:39 +0200 (CEST) Date: Wed, 18 Jul 2018 18:16:39 +0200 From: Peter Zijlstra To: Waiman Long Cc: Ingo Molnar , Will Deacon , linux-kernel@vger.kernel.org, Mark Ray , Joe Mario , Scott Norton Subject: Re: [PATCH v2] locking/rwsem: Take read lock immediate if queue empty with no writer Message-ID: <20180718161639.GT2494@hirez.programming.kicks-ass.net> References: <1531506653-5244-1-git-send-email-longman@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1531506653-5244-1-git-send-email-longman@redhat.com> User-Agent: Mutt/1.10.0 (2018-05-17) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 13, 2018 at 02:30:53PM -0400, Waiman Long wrote: > It was discovered that a constant stream of readers might cause the > count to go negative most of the time after an initial trigger by a > writer even if no writer was present afterward. As a result, most of the > readers would have to go through the slowpath reducing their performance. I'm slightly confused, what happens to trigger this? (also, I'm forever confused by the whole BIAS scheme) > To avoid that from happening, an additional check is added to detect > the special case that the reader in the critical section is the only > one in the wait queue and no writer is present. When that happens, it > can just have the lock and return immediately without further action. > Other incoming readers won't see a waiter is present and be forced > into the slowpath. > > The additional code is in the slowpath and so should not have an impact > on rwsem performance. However, in the special case listed above, it may > greatly improve performance. > diff --git a/kernel/locking/rwsem-xadd.c b/kernel/locking/rwsem-xadd.c > index 3064c50..bf0570e 100644 > --- a/kernel/locking/rwsem-xadd.c > +++ b/kernel/locking/rwsem-xadd.c > @@ -233,8 +233,19 @@ static void __rwsem_mark_wake(struct rw_semaphore *sem, your diff function thingy is busted, this is in fact __rwsem_down_read_failed_common you're patching. > waiter.type = RWSEM_WAITING_FOR_READ; > > raw_spin_lock_irq(&sem->wait_lock); > - if (list_empty(&sem->wait_list)) > + if (list_empty(&sem->wait_list)) { > + /* > + * In the unlikely event that the task is the only one in > + * the wait queue and a writer isn't present, it can have > + * the lock and return immediately without going through > + * the remaining slowpath code. > + */ > + if (unlikely(atomic_long_read(&sem->count) >= 0)) { > + raw_spin_unlock_irq(&sem->wait_lock); > + return sem; > + } > adjustment += RWSEM_WAITING_BIAS; > + } > list_add_tail(&waiter.list, &sem->wait_list); So without this, we would add ourselves to the list and then immediately call __rwsem_mark_wake() on ourselves and fall through the wait-loop, ow what?