public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: tip-bot for Jason Low <tipbot@zytor.com>
To: linux-tip-commits@vger.kernel.org
Cc: heiko.carstens@de.ibm.com, tony.luck@intel.com,
	tim.c.chen@linux.intel.com, paulmck@linux.vnet.ibm.com,
	dave@stgolabs.net, akpm@linux-foundation.org,
	torvalds@linux-foundation.org, ink@jurassic.park.msu.ru,
	hpa@zytor.com, arnd@arndb.de, schwidefsky@de.ibm.com,
	peterz@infradead.org, cl@linux.com, linux-kernel@vger.kernel.org,
	jason.low2@hpe.com, tglx@linutronix.de, fenghua.yu@intel.com,
	peter@hurleysoftware.com, jason.low2@hp.com, rth@twiddle.net,
	mattst88@gmail.com, Waiman.Long@hpe.com, terry.rudd@hpe.com,
	mingo@kernel.org
Subject: [tip:locking/core] locking/rwsem: Optimize write lock by reducing operations in slowpath
Date: Fri, 3 Jun 2016 03:55:45 -0700	[thread overview]
Message-ID: <tip-c0fcb6c2d332041256dc55d8a1ec3c0a2d0befb8@git.kernel.org> (raw)
In-Reply-To: <1463445486-16078-2-git-send-email-jason.low2@hpe.com>

Commit-ID:  c0fcb6c2d332041256dc55d8a1ec3c0a2d0befb8
Gitweb:     http://git.kernel.org/tip/c0fcb6c2d332041256dc55d8a1ec3c0a2d0befb8
Author:     Jason Low <jason.low2@hpe.com>
AuthorDate: Mon, 16 May 2016 17:38:00 -0700
Committer:  Ingo Molnar <mingo@kernel.org>
CommitDate: Fri, 3 Jun 2016 09:47:13 +0200

locking/rwsem: Optimize write lock by reducing operations in slowpath

When acquiring the rwsem write lock in the slowpath, we first try
to set count to RWSEM_WAITING_BIAS. When that is successful,
we then atomically add the RWSEM_WAITING_BIAS in cases where
there are other tasks on the wait list. This causes write lock
operations to often issue multiple atomic operations.

We can instead make the list_is_singular() check first, and then
set the count accordingly, so that we issue at most 1 atomic
operation when acquiring the write lock and reduce unnecessary
cacheline contention.

Signed-off-by: Jason Low <jason.low2@hpe.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Waiman Long<Waiman.Long@hpe.com>
Acked-by: Davidlohr Bueso <dave@stgolabs.net>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Christoph Lameter <cl@linux.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
Cc: Jason Low <jason.low2@hp.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Matt Turner <mattst88@gmail.com>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Hurley <peter@hurleysoftware.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Richard Henderson <rth@twiddle.net>
Cc: Terry Rudd <terry.rudd@hpe.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Tony Luck <tony.luck@intel.com>
Link: http://lkml.kernel.org/r/1463445486-16078-2-git-send-email-jason.low2@hpe.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
---
 kernel/locking/rwsem-xadd.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/kernel/locking/rwsem-xadd.c b/kernel/locking/rwsem-xadd.c
index fcbf75a..b957da7 100644
--- a/kernel/locking/rwsem-xadd.c
+++ b/kernel/locking/rwsem-xadd.c
@@ -261,17 +261,28 @@ struct rw_semaphore __sched *rwsem_down_read_failed(struct rw_semaphore *sem)
 }
 EXPORT_SYMBOL(rwsem_down_read_failed);
 
+/*
+ * This function must be called with the sem->wait_lock held to prevent
+ * race conditions between checking the rwsem wait list and setting the
+ * sem->count accordingly.
+ */
 static inline bool rwsem_try_write_lock(long count, struct rw_semaphore *sem)
 {
 	/*
-	 * Try acquiring the write lock. Check count first in order
-	 * to reduce unnecessary expensive cmpxchg() operations.
+	 * Avoid trying to acquire write lock if count isn't RWSEM_WAITING_BIAS.
 	 */
-	if (count == RWSEM_WAITING_BIAS &&
-	    cmpxchg_acquire(&sem->count, RWSEM_WAITING_BIAS,
-		    RWSEM_ACTIVE_WRITE_BIAS) == RWSEM_WAITING_BIAS) {
-		if (!list_is_singular(&sem->wait_list))
-			rwsem_atomic_update(RWSEM_WAITING_BIAS, sem);
+	if (count != RWSEM_WAITING_BIAS)
+		return false;
+
+	/*
+	 * Acquire the lock by trying to set it to ACTIVE_WRITE_BIAS. If there
+	 * are other tasks on the wait list, we need to add on WAITING_BIAS.
+	 */
+	count = list_is_singular(&sem->wait_list) ?
+			RWSEM_ACTIVE_WRITE_BIAS :
+			RWSEM_ACTIVE_WRITE_BIAS + RWSEM_WAITING_BIAS;
+
+	if (cmpxchg_acquire(&sem->count, RWSEM_WAITING_BIAS, count) == RWSEM_WAITING_BIAS) {
 		rwsem_set_owner(sem);
 		return true;
 	}

  reply	other threads:[~2016-06-03 10:57 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-17  0:37 [RFC][PATCH 0/7] locking/rwsem: Convert rwsem count to atomic_long_t Jason Low
2016-05-17  0:38 ` [RFC][PATCH 1/7] locking/rwsem: Optimize write lock by reducing operations in slowpath Jason Low
2016-06-03 10:55   ` tip-bot for Jason Low [this message]
2016-05-17  0:38 ` [RFC][PATCH 2/7] locking/rwsem: Convert sem->count to atomic_long_t Jason Low
2016-05-17  0:38 ` [RFC][PATCH 3/7] locking,x86: Remove x86 rwsem add and rwsem update Jason Low
2016-05-17  0:38 ` [RFC][PATCH 4/7] locking,alpha: Remove Alpha " Jason Low
2016-05-17  0:38 ` [RFC][PATCH 5/7] locking,ia64: Remove ia64 " Jason Low
2016-05-17  0:38 ` [RFC][PATCH 6/7] locking,s390: Remove s390 " Jason Low
2016-05-17  0:38 ` [RFC][PATCH 7/7] locking,asm-generic: Remove generic rwsem add and rwsem update definitions Jason Low
2016-05-17  1:12 ` [RFC][PATCH 0/7] locking/rwsem: Convert rwsem count to atomic_long_t Linus Torvalds
2016-05-17 11:09   ` Peter Zijlstra
2016-05-17 17:06     ` Jason Low
2016-05-20  6:18     ` Davidlohr Bueso
2016-06-03  8:04     ` Ingo Molnar
2016-06-03 12:00       ` Peter Zijlstra
2016-06-03 12:12         ` Peter Zijlstra
2016-06-03 23:31         ` Linus Torvalds
2016-06-03 18:09       ` Jason Low
2016-06-03 18:48         ` Peter Zijlstra
2016-06-03 22:36         ` Peter Zijlstra
2016-06-03 23:04           ` Jason Low

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-c0fcb6c2d332041256dc55d8a1ec3c0a2d0befb8@git.kernel.org \
    --to=tipbot@zytor.com \
    --cc=Waiman.Long@hpe.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=cl@linux.com \
    --cc=dave@stgolabs.net \
    --cc=fenghua.yu@intel.com \
    --cc=heiko.carstens@de.ibm.com \
    --cc=hpa@zytor.com \
    --cc=ink@jurassic.park.msu.ru \
    --cc=jason.low2@hp.com \
    --cc=jason.low2@hpe.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=mattst88@gmail.com \
    --cc=mingo@kernel.org \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=peter@hurleysoftware.com \
    --cc=peterz@infradead.org \
    --cc=rth@twiddle.net \
    --cc=schwidefsky@de.ibm.com \
    --cc=terry.rudd@hpe.com \
    --cc=tglx@linutronix.de \
    --cc=tim.c.chen@linux.intel.com \
    --cc=tony.luck@intel.com \
    --cc=torvalds@linux-foundation.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox