From patchwork Mon Feb 11 19:31:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 10806741 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4BB206C2 for ; Mon, 11 Feb 2019 19:32:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 36A432B093 for ; Mon, 11 Feb 2019 19:32:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 29F0E2B0A8; Mon, 11 Feb 2019 19:32:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id CF33C2B093 for ; Mon, 11 Feb 2019 19:32:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=K3BbThtILBsQKnSCkX9m6PHujhtF2PEdGACE3e6BFRM=; b=O9+EJ/IB2xt0YD6VvPQ5T0rXkS PkMq+H4OgSYsycXHWbLKynrQQpG138r0jfbdyxrBcviJggbIW3qadIiRGScKgmOf+GH+QjBPiiQ08 ZG3IbzgUzE1NOku/l6ERHY1xtIxCGBNl8A581qez/9TP7TjllZ/mbMNiTYC5jjfmhLt+vN1F2nhLC zhGuOf/fNiaSSctjD/XYBQbjV9n1AxTJaGGVpFFp65PBEdNEJGBz0Xi9NDGh3iChiWzXNxqNRWH7m uOehvoBv40tlZYpEa85py5mdjVkHQmW22O313EbPRfWssSboLPtuS8+VaSPZbSxU9h1HD76tZBAon xu8uLCVw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gtHJY-0006fB-KG; Mon, 11 Feb 2019 19:32:24 +0000 Received: from mx1.redhat.com ([209.132.183.28]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gtHJ2-0006BT-N9 for linux-arm-kernel@lists.infradead.org; Mon, 11 Feb 2019 19:31:57 +0000 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D62ECC0CD165; Mon, 11 Feb 2019 19:31:51 +0000 (UTC) Received: from llong.com (dhcp-17-59.bos.redhat.com [10.18.17.59]) by smtp.corp.redhat.com (Postfix) with ESMTP id BA6275C236; Mon, 11 Feb 2019 19:31:49 +0000 (UTC) From: Waiman Long To: Peter Zijlstra , Ingo Molnar , Will Deacon , Thomas Gleixner Subject: [PATCH v2 2/2] locking/rwsem: Optimize down_read_trylock() Date: Mon, 11 Feb 2019 14:31:26 -0500 Message-Id: <1549913486-16799-3-git-send-email-longman@redhat.com> In-Reply-To: <1549913486-16799-1-git-send-email-longman@redhat.com> References: <1549913486-16799-1-git-send-email-longman@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Mon, 11 Feb 2019 19:31:52 +0000 (UTC) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190211_113153_046100_F911A0F9 X-CRM114-Status: GOOD ( 14.79 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arch@vger.kernel.org, linux-xtensa@linux-xtensa.org, Davidlohr Bueso , linux-ia64@vger.kernel.org, Tim Chen , Arnd Bergmann , linux-sh@vger.kernel.org, linux-hexagon@vger.kernel.org, x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org, Linus Torvalds , Borislav Petkov , linux-alpha@vger.kernel.org, sparclinux@vger.kernel.org, Waiman Long , Andrew Morton , linuxppc-dev@lists.ozlabs.org, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Modify __down_read_trylock() to make it generate slightly better code (smaller and maybe a tiny bit faster). Before this patch, down_read_trylock: 0x0000000000000000 <+0>: callq 0x5 0x0000000000000005 <+5>: jmp 0x18 0x0000000000000007 <+7>: lea 0x1(%rdx),%rcx 0x000000000000000b <+11>: mov %rdx,%rax 0x000000000000000e <+14>: lock cmpxchg %rcx,(%rdi) 0x0000000000000013 <+19>: cmp %rax,%rdx 0x0000000000000016 <+22>: je 0x23 0x0000000000000018 <+24>: mov (%rdi),%rdx 0x000000000000001b <+27>: test %rdx,%rdx 0x000000000000001e <+30>: jns 0x7 0x0000000000000020 <+32>: xor %eax,%eax 0x0000000000000022 <+34>: retq 0x0000000000000023 <+35>: mov %gs:0x0,%rax 0x000000000000002c <+44>: or $0x3,%rax 0x0000000000000030 <+48>: mov %rax,0x20(%rdi) 0x0000000000000034 <+52>: mov $0x1,%eax 0x0000000000000039 <+57>: retq After patch, down_read_trylock: 0x0000000000000000 <+0>: callq 0x5 0x0000000000000005 <+5>: mov (%rdi),%rax 0x0000000000000008 <+8>: test %rax,%rax 0x000000000000000b <+11>: js 0x2f 0x000000000000000d <+13>: lea 0x1(%rax),%rdx 0x0000000000000011 <+17>: lock cmpxchg %rdx,(%rdi) 0x0000000000000016 <+22>: jne 0x8 0x0000000000000018 <+24>: mov %gs:0x0,%rax 0x0000000000000021 <+33>: or $0x3,%rax 0x0000000000000025 <+37>: mov %rax,0x20(%rdi) 0x0000000000000029 <+41>: mov $0x1,%eax 0x000000000000002e <+46>: retq 0x000000000000002f <+47>: xor %eax,%eax 0x0000000000000031 <+49>: retq By using a rwsem microbenchmark, the down_read_trylock() rate on a x86-64 system before and after the patch were: Before Patch After Patch # of Threads rlock rlock ------------ ----- ----- 1 27,787 28,259 2 8,359 9,234 On a ARM64 system, the performance results were: Before Patch After Patch # of Threads rlock rlock ------------ ----- ----- 1 24,155 25,000 2 6,820 8,699 Suggested-by: Peter Zijlstra Signed-off-by: Waiman Long --- kernel/locking/rwsem.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kernel/locking/rwsem.h b/kernel/locking/rwsem.h index 067e265..028bc33 100644 --- a/kernel/locking/rwsem.h +++ b/kernel/locking/rwsem.h @@ -175,11 +175,11 @@ static inline int __down_read_killable(struct rw_semaphore *sem) static inline int __down_read_trylock(struct rw_semaphore *sem) { - long tmp; + long tmp = atomic_long_read(&sem->count); - while ((tmp = atomic_long_read(&sem->count)) >= 0) { - if (tmp == atomic_long_cmpxchg_acquire(&sem->count, tmp, - tmp + RWSEM_ACTIVE_READ_BIAS)) { + while (tmp >= 0) { + if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp, + tmp + RWSEM_ACTIVE_READ_BIAS)) { return 1; } }