All of lore.kernel.org
 help / color / mirror / Atom feed
From: tip-bot for Waiman Long <tipbot@zytor.com>
To: linux-tip-commits@vger.kernel.org
Cc: paulmck@linux.vnet.ibm.com, will.deacon@arm.com,
	mingo@kernel.org, dave@stgolabs.net, tglx@linutronix.de,
	tim.c.chen@linux.intel.com, linux-kernel@vger.kernel.org,
	arnd@arndb.de, longman@redhat.com, peterz@infradead.org,
	akpm@linux-foundation.org, hpa@zytor.com, bp@alien8.de,
	torvalds@linux-foundation.org
Subject: [tip:locking/core] locking/rwsem: Optimize down_read_trylock()
Date: Wed, 3 Apr 2019 03:45:34 -0700	[thread overview]
Message-ID: <tip-0975e3df30eb5849284c01be66c2ec16d8a48114@git.kernel.org> (raw)
In-Reply-To: <20190322143008.21313-4-longman@redhat.com>

Commit-ID:  0975e3df30eb5849284c01be66c2ec16d8a48114
Gitweb:     https://git.kernel.org/tip/0975e3df30eb5849284c01be66c2ec16d8a48114
Author:     Waiman Long <longman@redhat.com>
AuthorDate: Fri, 22 Mar 2019 10:30:08 -0400
Committer:  Ingo Molnar <mingo@kernel.org>
CommitDate: Wed, 3 Apr 2019 11:42:35 +0200

locking/rwsem: Optimize down_read_trylock()

Modify __down_read_trylock() to optimize for an unlocked rwsem and make
it generate slightly better code.

Before this patch, down_read_trylock:

   0x0000000000000000 <+0>:     callq  0x5 <down_read_trylock+5>
   0x0000000000000005 <+5>:     jmp    0x18 <down_read_trylock+24>
   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 <down_read_trylock+35>
   0x0000000000000018 <+24>:    mov    (%rdi),%rdx
   0x000000000000001b <+27>:    test   %rdx,%rdx
   0x000000000000001e <+30>:    jns    0x7 <down_read_trylock+7>
   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 <down_read_trylock+5>
   0x0000000000000005 <+5>:	xor    %eax,%eax
   0x0000000000000007 <+7>:	lea    0x1(%rax),%rdx
   0x000000000000000b <+11>:	lock cmpxchg %rdx,(%rdi)
   0x0000000000000010 <+16>:	jne    0x29 <down_read_trylock+41>
   0x0000000000000012 <+18>:	mov    %gs:0x0,%rax
   0x000000000000001b <+27>:	or     $0x3,%rax
   0x000000000000001f <+31>:	mov    %rax,0x20(%rdi)
   0x0000000000000023 <+35>:	mov    $0x1,%eax
   0x0000000000000028 <+40>:	retq
   0x0000000000000029 <+41>:	test   %rax,%rax
   0x000000000000002c <+44>:	jns    0x7 <down_read_trylock+7>
   0x000000000000002e <+46>:	xor    %eax,%eax
   0x0000000000000030 <+48>:	retq

By using a rwsem microbenchmark, the down_read_trylock() rate (with a
load of 10 to lengthen the lock critical section) on a x86-64 system
before and after the patch were:

                 Before Patch    After Patch
   # of Threads     rlock           rlock
   ------------     -----           -----
        1           14,496          14,716
        2            8,644           8,453
	4            6,799           6,983
	8            5,664           7,190

On a ARM64 system, the performance results were:

                 Before Patch    After Patch
   # of Threads     rlock           rlock
   ------------     -----           -----
        1           23,676          24,488
        2            7,697           9,502
        4            4,945           3,440
        8            2,641           1,603

For the uncontended case (1 thread), the new down_read_trylock() is a
little bit faster. For the contended cases, the new down_read_trylock()
perform pretty well in x86-64, but performance degrades at high
contention level on ARM64.

Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Waiman Long <longman@redhat.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Will Deacon <will.deacon@arm.com>
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-c6x-dev@linux-c6x.org
Cc: linux-m68k@lists.linux-m68k.org
Cc: linux-riscv@lists.infradead.org
Cc: linux-um@lists.infradead.org
Cc: linux-xtensa@linux-xtensa.org
Cc: linuxppc-dev@lists.ozlabs.org
Cc: nios2-dev@lists.rocketboards.org
Cc: openrisc@lists.librecores.org
Cc: uclinux-h8-devel@lists.sourceforge.jp
Link: https://lkml.kernel.org/r/20190322143008.21313-4-longman@redhat.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
---
 kernel/locking/rwsem.h | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/kernel/locking/rwsem.h b/kernel/locking/rwsem.h
index 45ee00236e03..1f5775aa6a1d 100644
--- a/kernel/locking/rwsem.h
+++ b/kernel/locking/rwsem.h
@@ -174,14 +174,17 @@ static inline int __down_read_killable(struct rw_semaphore *sem)
 
 static inline int __down_read_trylock(struct rw_semaphore *sem)
 {
-	long tmp;
+	/*
+	 * Optimize for the case when the rwsem is not locked at all.
+	 */
+	long tmp = RWSEM_UNLOCKED_VALUE;
 
-	while ((tmp = atomic_long_read(&sem->count)) >= 0) {
-		if (tmp == atomic_long_cmpxchg_acquire(&sem->count, tmp,
-				   tmp + RWSEM_ACTIVE_READ_BIAS)) {
+	do {
+		if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp,
+					tmp + RWSEM_ACTIVE_READ_BIAS)) {
 			return 1;
 		}
-	}
+	} while (tmp >= 0);
 	return 0;
 }
 

  parent reply	other threads:[~2019-04-03 10:47 UTC|newest]

Thread overview: 108+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-22 14:30 [PATCH v5 0/3] locking/rwsem: Rwsem rearchitecture part 0 Waiman Long
2019-03-22 14:30 ` Waiman Long
2019-03-22 14:30 ` [OpenRISC] " Waiman Long
2019-03-22 14:30 ` Waiman Long
2019-03-22 14:30 ` Waiman Long
2019-03-22 14:30 ` Waiman Long
2019-03-22 14:30 ` Waiman Long
2019-03-22 14:30 ` Waiman Long
2019-03-22 14:30 ` Waiman Long
2019-03-22 14:30 ` [PATCH v5 1/3] locking/rwsem: Remove arch specific rwsem files Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 14:30   ` [OpenRISC] " Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 17:01   ` Linus Torvalds
2019-03-22 17:01     ` Linus Torvalds
2019-03-22 17:01     ` Linus Torvalds
2019-03-22 17:01     ` [OpenRISC] " Linus Torvalds
2019-03-22 17:01     ` Linus Torvalds
2019-03-22 17:01     ` Linus Torvalds
2019-03-22 17:01     ` Linus Torvalds
2019-03-22 17:23     ` Waiman Long
2019-03-22 17:23       ` Waiman Long
2019-03-22 17:23       ` [OpenRISC] " Waiman Long
2019-03-22 17:23       ` Waiman Long
2019-03-22 17:23       ` Waiman Long
2019-03-22 17:23       ` Waiman Long
2019-03-22 17:23       ` Waiman Long
2019-03-22 19:30     ` Davidlohr Bueso
2019-03-22 19:30       ` Davidlohr Bueso
2019-03-22 19:30       ` Davidlohr Bueso
2019-03-22 19:30       ` [OpenRISC] " Davidlohr Bueso
2019-03-22 19:30       ` Davidlohr Bueso
2019-03-22 19:30       ` Davidlohr Bueso
2019-03-22 19:30       ` Davidlohr Bueso
2019-03-22 19:30       ` Davidlohr Bueso
2019-03-22 20:27       ` Waiman Long
2019-03-22 20:27         ` Waiman Long
2019-03-22 20:27         ` [OpenRISC] " Waiman Long
2019-03-22 20:27         ` Waiman Long
2019-03-22 20:27         ` Waiman Long
2019-03-22 20:27         ` Waiman Long
2019-03-22 20:27         ` Waiman Long
2019-04-03 10:44   ` [tip:locking/core] " tip-bot for Waiman Long
2019-04-03 12:47     ` Peter Zijlstra
2019-04-03 12:55   ` tip-bot for Waiman Long
2019-03-22 14:30 ` [PATCH v5 2/3] locking/rwsem: Remove rwsem-spinlock.c & use rwsem-xadd.c for all archs Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 14:30   ` [OpenRISC] " Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 17:07   ` Linus Torvalds
2019-03-22 17:07     ` Linus Torvalds
2019-03-22 17:07     ` [OpenRISC] " Linus Torvalds
2019-03-22 17:07     ` Linus Torvalds
2019-03-22 17:07     ` Linus Torvalds
2019-03-22 17:07     ` Linus Torvalds
2019-03-22 17:07     ` Linus Torvalds
2019-04-03 10:44   ` [tip:locking/core] " tip-bot for Waiman Long
2019-04-03 12:55   ` tip-bot for Waiman Long
2019-03-22 14:30 ` [PATCH v5 3/3] locking/rwsem: Optimize down_read_trylock() Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 14:30   ` [OpenRISC] " Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 14:30   ` Waiman Long
2019-03-22 17:08   ` Linus Torvalds
2019-03-22 17:08     ` Linus Torvalds
2019-03-22 17:08     ` Linus Torvalds
2019-03-22 17:08     ` [OpenRISC] " Linus Torvalds
2019-03-22 17:08     ` Linus Torvalds
2019-03-22 17:08     ` Linus Torvalds
2019-03-22 17:08     ` Linus Torvalds
2019-03-22 17:25   ` Russell King - ARM Linux admin
2019-03-22 17:25     ` Russell King - ARM Linux admin
2019-03-22 17:25     ` Russell King - ARM Linux admin
2019-03-22 17:25     ` [OpenRISC] " Russell King - ARM Linux admin
2019-03-22 17:25     ` Russell King - ARM Linux admin
2019-03-22 17:25     ` Russell King - ARM Linux admin
2019-03-22 17:25     ` Russell King - ARM Linux admin
2019-03-22 17:25     ` Russell King - ARM Linux admin
2019-03-22 17:41     ` Waiman Long
2019-03-22 17:41       ` Waiman Long
2019-03-22 17:41       ` Waiman Long
2019-03-22 17:41       ` [OpenRISC] " Waiman Long
2019-03-22 17:41       ` Waiman Long
2019-03-22 17:41       ` Waiman Long
2019-03-22 17:41       ` Waiman Long
2019-03-25 15:25   ` Christophe Leroy
2019-03-25 15:25     ` Christophe Leroy
2019-03-25 15:25     ` Christophe Leroy
2019-03-25 15:25     ` [OpenRISC] " Christophe Leroy
2019-03-25 15:25     ` Christophe Leroy
2019-03-25 15:25     ` Christophe Leroy
2019-03-25 15:25     ` Christophe Leroy
2019-04-03 10:45   ` tip-bot for Waiman Long [this message]
2019-04-03 12:56   ` [tip:locking/core] " tip-bot for Waiman Long

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tip-0975e3df30eb5849284c01be66c2ec16d8a48114@git.kernel.org \
    --to=tipbot@zytor.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=bp@alien8.de \
    --cc=dave@stgolabs.net \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mingo@kernel.org \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=tim.c.chen@linux.intel.com \
    --cc=torvalds@linux-foundation.org \
    --cc=will.deacon@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.