From: Michel Lespinasse <walken@google.com>
To: Linus Torvalds <torvalds@linux-foundation.org>,
David Howells <dhowells@redhat.com>,
Peter Zijlstra <a.p.zijlstra@chello.nl>,
Ingo Molnar <mingo@kernel.org>, Rik van Riel <riel@redhat.com>,
Davidlohr Bueso <davidlohr.bueso@hp.com>,
Peter Hurley <peter@hurleysoftware.com>
Cc: Alex Shi <alex.shi@intel.com>,
Yuanhan Liu <yuanhan.liu@linux.intel.com>,
Andrew Morton <akpm@linux-foundation.org>,
linux-kernel@vger.kernel.org
Subject: [PATCH v3 10/14] rwsem: simplify __rwsem_do_wake
Date: Tue, 7 May 2013 06:45:58 -0700 [thread overview]
Message-ID: <1367934362-10433-11-git-send-email-walken@google.com> (raw)
In-Reply-To: <1367934362-10433-1-git-send-email-walken@google.com>
This is mostly for cleanup value:
- We don't need several gotos to handle the case where the first
waiter is a writer. Two simple tests will do (and generate very
similar code).
- In the remainder of the function, we know the first waiter is a reader,
so we don't have to double check that. We can use do..while loops
to iterate over the readers to wake (generates slightly better code).
Signed-off-by: Michel Lespinasse <walken@google.com>
Reviewed-by: Peter Hurley <peter@hurleysoftware.com>
Acked-by: Davidlohr Bueso <davidlohr.bueso@hp.com>
---
lib/rwsem-spinlock.c | 23 +++++++----------------
lib/rwsem.c | 26 ++++++++++++--------------
2 files changed, 19 insertions(+), 30 deletions(-)
diff --git a/lib/rwsem-spinlock.c b/lib/rwsem-spinlock.c
index 5f117f37ac0a..9be8a9144978 100644
--- a/lib/rwsem-spinlock.c
+++ b/lib/rwsem-spinlock.c
@@ -70,26 +70,17 @@ __rwsem_do_wake(struct rw_semaphore *sem, int wakewrite)
waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
- if (!wakewrite) {
- if (waiter->type == RWSEM_WAITING_FOR_WRITE)
- goto out;
- goto dont_wake_writers;
- }
-
- /*
- * as we support write lock stealing, we can't set sem->activity
- * to -1 here to indicate we get the lock. Instead, we wake it up
- * to let it go get it again.
- */
if (waiter->type == RWSEM_WAITING_FOR_WRITE) {
- wake_up_process(waiter->task);
+ if (wakewrite)
+ /* Wake up a writer. Note that we do not grant it the
+ * lock - it will have to acquire it when it runs. */
+ wake_up_process(waiter->task);
goto out;
}
/* grant an infinite number of read locks to the front of the queue */
- dont_wake_writers:
woken = 0;
- while (waiter->type == RWSEM_WAITING_FOR_READ) {
+ do {
struct list_head *next = waiter->list.next;
list_del(&waiter->list);
@@ -99,10 +90,10 @@ __rwsem_do_wake(struct rw_semaphore *sem, int wakewrite)
wake_up_process(tsk);
put_task_struct(tsk);
woken++;
- if (list_empty(&sem->wait_list))
+ if (next == &sem->wait_list)
break;
waiter = list_entry(next, struct rwsem_waiter, list);
- }
+ } while (waiter->type != RWSEM_WAITING_FOR_WRITE);
sem->activity += woken;
diff --git a/lib/rwsem.c b/lib/rwsem.c
index 0d50e46d5b0c..9a675fa9d78e 100644
--- a/lib/rwsem.c
+++ b/lib/rwsem.c
@@ -68,20 +68,17 @@ __rwsem_do_wake(struct rw_semaphore *sem, int wake_type)
signed long woken, loop, adjustment;
waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
- if (waiter->type != RWSEM_WAITING_FOR_WRITE)
- goto readers_only;
-
- if (wake_type == RWSEM_WAKE_READ_OWNED)
- /* Another active reader was observed, so wakeup is not
- * likely to succeed. Save the atomic op.
- */
+ if (waiter->type == RWSEM_WAITING_FOR_WRITE) {
+ if (wake_type != RWSEM_WAKE_READ_OWNED)
+ /* Wake writer at the front of the queue, but do not
+ * grant it the lock yet as we want other writers
+ * to be able to steal it. Readers, on the other hand,
+ * will block as they will notice the queued writer.
+ */
+ wake_up_process(waiter->task);
goto out;
+ }
- /* Wake up the writing waiter and let the task grab the sem: */
- wake_up_process(waiter->task);
- goto out;
-
- readers_only:
/* If we come here from up_xxxx(), another thread might have reached
* rwsem_down_failed_common() before we acquired the spinlock and
* woken up a waiter, making it now active. We prefer to check for
@@ -125,7 +122,8 @@ __rwsem_do_wake(struct rw_semaphore *sem, int wake_type)
rwsem_atomic_add(adjustment, sem);
next = sem->wait_list.next;
- for (loop = woken; loop > 0; loop--) {
+ loop = woken;
+ do {
waiter = list_entry(next, struct rwsem_waiter, list);
next = waiter->list.next;
tsk = waiter->task;
@@ -133,7 +131,7 @@ __rwsem_do_wake(struct rw_semaphore *sem, int wake_type)
waiter->task = NULL;
wake_up_process(tsk);
put_task_struct(tsk);
- }
+ } while (--loop);
sem->wait_list.next = next;
next->prev = &sem->wait_list;
--
1.8.2.1
next prev parent reply other threads:[~2013-05-07 13:48 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-05-07 13:45 [PATCH v3 00/14] rwsem fast-path write lock stealing Michel Lespinasse
2013-05-07 13:45 ` [PATCH v3 01/14] rwsem: make the waiter type an enumeration rather than a bitmask Michel Lespinasse
2013-05-07 13:45 ` [PATCH v3 02/14] rwsem: shorter spinlocked section in rwsem_down_failed_common() Michel Lespinasse
2013-05-07 13:45 ` [PATCH v3 03/14] rwsem: move rwsem_down_failed_common code into rwsem_down_{read,write}_failed Michel Lespinasse
2013-05-07 13:45 ` [PATCH v3 04/14] rwsem: simplify rwsem_down_read_failed Michel Lespinasse
2013-05-07 13:45 ` [PATCH v3 05/14] rwsem: simplify rwsem_down_write_failed Michel Lespinasse
2013-05-07 13:45 ` [PATCH v3 06/14] rwsem: more agressive lock stealing in rwsem_down_write_failed Michel Lespinasse
2013-05-07 13:45 ` [PATCH v3 07/14] rwsem: use cmpxchg for trying to steal write lock Michel Lespinasse
2013-05-07 13:45 ` [PATCH v3 08/14] rwsem: avoid taking wait_lock in rwsem_down_write_failed Michel Lespinasse
2013-05-07 13:45 ` [PATCH v3 09/14] rwsem: skip initial trylock " Michel Lespinasse
2013-05-07 13:45 ` Michel Lespinasse [this message]
2013-05-07 13:45 ` [PATCH v3 11/14] rwsem: implement support for write lock stealing on the fastpath Michel Lespinasse
2013-05-07 13:46 ` [PATCH v3 12/14] rwsem: do not block readers at head of queue if other readers are active Michel Lespinasse
2013-05-07 13:46 ` [PATCH v3 13/14] x86 rwsem: avoid taking slow path when stealing write lock Michel Lespinasse
2013-05-07 13:46 ` [PATCH v3 14/14] rwsem: no need for explicit signed longs Michel Lespinasse
2013-05-07 22:39 ` [PATCH v3 15/14] rwsem: check counter to avoid cmpxchg calls Davidlohr Bueso
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=1367934362-10433-11-git-send-email-walken@google.com \
--to=walken@google.com \
--cc=a.p.zijlstra@chello.nl \
--cc=akpm@linux-foundation.org \
--cc=alex.shi@intel.com \
--cc=davidlohr.bueso@hp.com \
--cc=dhowells@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=peter@hurleysoftware.com \
--cc=riel@redhat.com \
--cc=torvalds@linux-foundation.org \
--cc=yuanhan.liu@linux.intel.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
all inboxes | Powered by JetHome®