linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Waiman Long <Waiman.Long@hpe.com>
To: Peter Zijlstra <peterz@infradead.org>, Ingo Molnar <mingo@redhat.com>
Cc: linux-kernel@vger.kernel.org, Davidlohr Bueso <dave@stgolabs.net>,
	Jason Low <jason.low2@hp.com>, Dave Chinner <david@fromorbit.com>,
	Peter Hurley <peter@hurleysoftware.com>,
	"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>,
	Scott J Norton <scott.norton@hpe.com>,
	Douglas Hatch <doug.hatch@hpe.com>,
	Waiman Long <Waiman.Long@hpe.com>
Subject: [PATCH v4 5/5] locking/rwsem: Streamline the rwsem_optimistic_spin() code
Date: Tue, 17 May 2016 21:26:23 -0400	[thread overview]
Message-ID: <1463534783-38814-6-git-send-email-Waiman.Long@hpe.com> (raw)
In-Reply-To: <1463534783-38814-1-git-send-email-Waiman.Long@hpe.com>

This patch moves the owner loading and checking code entirely inside of
rwsem_spin_on_owner() to simplify the logic of rwsem_optimistic_spin()
loop.

Suggested-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Waiman Long <Waiman.Long@hpe.com>
Reviewed-by: Peter Hurley <peter@hurleysoftware.com>
---
 kernel/locking/rwsem-xadd.c |   38 ++++++++++++++++++++------------------
 1 files changed, 20 insertions(+), 18 deletions(-)

diff --git a/kernel/locking/rwsem-xadd.c b/kernel/locking/rwsem-xadd.c
index e3a7e06..a85a2bd 100644
--- a/kernel/locking/rwsem-xadd.c
+++ b/kernel/locking/rwsem-xadd.c
@@ -332,9 +332,16 @@ done:
 	return ret;
 }
 
-static noinline
-bool rwsem_spin_on_owner(struct rw_semaphore *sem, struct task_struct *owner)
+/*
+ * Return true only if we can still spin on the owner field of the rwsem.
+ */
+static noinline bool rwsem_spin_on_owner(struct rw_semaphore *sem)
 {
+	struct task_struct *owner = READ_ONCE(sem->owner);
+
+	if (!rwsem_owner_is_writer(owner))
+		goto out;
+
 	rcu_read_lock();
 	while (sem->owner == owner) {
 		/*
@@ -354,7 +361,7 @@ bool rwsem_spin_on_owner(struct rw_semaphore *sem, struct task_struct *owner)
 		cpu_relax_lowlatency();
 	}
 	rcu_read_unlock();
-
+out:
 	/*
 	 * If there is a new owner or the owner is not set, we continue
 	 * spinning.
@@ -364,7 +371,6 @@ bool rwsem_spin_on_owner(struct rw_semaphore *sem, struct task_struct *owner)
 
 static bool rwsem_optimistic_spin(struct rw_semaphore *sem)
 {
-	struct task_struct *owner;
 	bool taken = false;
 
 	preempt_disable();
@@ -376,21 +382,17 @@ static bool rwsem_optimistic_spin(struct rw_semaphore *sem)
 	if (!osq_lock(&sem->osq))
 		goto done;
 
-	while (true) {
-		owner = READ_ONCE(sem->owner);
+	/*
+	 * Optimistically spin on the owner field and attempt to acquire the
+	 * lock whenever the owner changes. Spinning will be stopped when:
+	 *  1) the owning writer isn't running; or
+	 *  2) readers own the lock as we can't determine if they are
+	 *     actively running or not.
+	 */
+	while (rwsem_spin_on_owner(sem)) {
 		/*
-		 * Don't spin if
-		 * 1) the owner is a reader as we we can't determine if the
-		 *    reader is actively running or not.
-		 * 2) The rwsem_spin_on_owner() returns false which means
-		 *    the owner isn't running.
+		 * Try to acquire the lock
 		 */
-		if (rwsem_owner_is_reader(owner) ||
-		   (rwsem_owner_is_writer(owner) &&
-		   !rwsem_spin_on_owner(sem, owner)))
-			break;
-
-		/* wait_lock will be acquired if write_lock is obtained */
 		if (rwsem_try_write_lock_unqueued(sem)) {
 			taken = true;
 			break;
@@ -402,7 +404,7 @@ static bool rwsem_optimistic_spin(struct rw_semaphore *sem)
 		 * we're an RT task that will live-lock because we won't let
 		 * the owner complete.
 		 */
-		if (!owner && (need_resched() || rt_task(current)))
+		if (!sem->owner && (need_resched() || rt_task(current)))
 			break;
 
 		/*
-- 
1.7.1

  parent reply	other threads:[~2016-05-18  1:27 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-18  1:26 [PATCH v4 0/5] [PATCH v3 0/4] locking/rwsem: Add reader-owned state to the owner field Waiman Long
2016-05-18  1:26 ` [PATCH v4 1/5] " Waiman Long
2016-06-06 17:18   ` Davidlohr Bueso
2016-06-06 20:03     ` Waiman Long
2016-06-06 21:02     ` Peter Zijlstra
2016-06-06 21:49       ` Waiman Long
2016-06-08 14:25   ` [tip:locking/core] " tip-bot for Waiman Long
2016-05-18  1:26 ` [PATCH v4 2/5] locking/rwsem: Protect all writes to owner by WRITE_ONCE() Waiman Long
2016-05-18 14:04   ` Davidlohr Bueso
2016-05-18 17:21     ` [PATCH v4 2/5] locking/rwsem: Protect all writes to owner by WRITE_ONCE Jason Low
2016-05-18 18:29       ` Waiman Long
2016-05-18 19:58         ` Jason Low
2016-05-19 22:21           ` Jason Low
2016-05-20 20:26             ` Waiman Long
2016-05-21 16:04           ` Peter Hurley
2016-05-22 10:42             ` Peter Zijlstra
2016-05-23 18:46             ` Jason Low
2016-05-23 19:44               ` Davidlohr Bueso
2016-05-23 20:15                 ` Paul E. McKenney
2016-05-23 21:04                   ` Davidlohr Bueso
2016-05-25  1:25               ` Waiman Long
2016-05-18 17:23     ` [PATCH v4 2/5] locking/rwsem: Protect all writes to owner by WRITE_ONCE() Jason Low
2016-06-08 14:25   ` [tip:locking/core] " tip-bot for Waiman Long
2016-05-18  1:26 ` [PATCH v4 3/5] locking/rwsem: Don't wake up one's own task Waiman Long
2016-05-18 10:30   ` Peter Zijlstra
2016-05-18 16:04     ` Waiman Long
2016-05-18  1:26 ` [PATCH v4 4/5] locking/rwsem: Improve reader wakeup code Waiman Long
2016-06-08 14:25   ` [tip:locking/core] " tip-bot for Waiman Long
2016-05-18  1:26 ` Waiman Long [this message]
2016-06-08 14:26   ` [tip:locking/core] locking/rwsem: Streamline the rwsem_optimistic_spin() code tip-bot for Waiman Long
2016-05-18 10:52 ` [PATCH v4 0/5] [PATCH v3 0/4] locking/rwsem: Add reader-owned state to the owner field Peter Zijlstra

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=1463534783-38814-6-git-send-email-Waiman.Long@hpe.com \
    --to=waiman.long@hpe.com \
    --cc=dave@stgolabs.net \
    --cc=david@fromorbit.com \
    --cc=doug.hatch@hpe.com \
    --cc=jason.low2@hp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=peter@hurleysoftware.com \
    --cc=peterz@infradead.org \
    --cc=scott.norton@hpe.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).