From: Michel Lespinasse <walken@google.com>
To: Linus Torvalds <torvalds@linux-foundation.org>,
David Howells <dhowells@redhat.com>, Ingo Molnar <mingo@elte.hu>,
Thomas Gleixner <tglx@linutronix.de>
Cc: LKML <linux-kernel@vger.kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
Mike Waychison <mikew@google.com>,
Suleiman Souhlal <suleiman@google.com>,
Ying Han <yinghan@google.com>,
Michel Lespinasse <walken@google.com>
Subject: [PATCH 07/12] rwsem: wake queued readers when writer blocks on active read lock
Date: Tue, 11 May 2010 20:20:57 -0700 [thread overview]
Message-ID: <1273634462-2672-8-git-send-email-walken@google.com> (raw)
In-Reply-To: <1273634462-2672-1-git-send-email-walken@google.com>
This change addresses the following situation:
- Thread A acquires the rwsem for read
- Thread B tries to acquire the rwsem for write, notices there is already
an active owner for the rwsem.
- Thread C tries to acquire the rwsem for read, notices that thread B already
tried to acquire it.
- Thread C grabs the spinlock and queues itself on the wait queue.
- Thread B grabs the spinlock and queues itself behind C. At this point A is
the only remaining active owner on the rwsem.
In this situation thread B could notice that it was the last active writer
on the rwsem, and decide to wake C to let it proceed in parallel with A
since they both only want the rwsem for read.
Signed-off-by: Michel Lespinasse <walken@google.com>
---
lib/rwsem.c | 32 ++++++++++++++++++++++----------
1 files changed, 22 insertions(+), 10 deletions(-)
diff --git a/lib/rwsem.c b/lib/rwsem.c
index 9d0899b..84bbc55 100644
--- a/lib/rwsem.c
+++ b/lib/rwsem.c
@@ -36,6 +36,10 @@ struct rwsem_waiter {
#define RWSEM_WAITING_FOR_WRITE 0x00000002
};
+#define RWSEM_WAKE_ANY 0 /* Wake whatever's at head of wait list */
+#define RWSEM_WAKE_READERS 1 /* Sem is read owned by other thread */
+#define RWSEM_WAKE_READ_OWNED 2 /* Sem is read owned by caller thread */
+
/*
* handle the lock release when processes blocked on it that can now run
* - if we come here from up_xxxx(), then:
@@ -46,8 +50,8 @@ struct rwsem_waiter {
* - woken process blocks are discarded from the list after having task zeroed
* - writers are only woken if downgrading is false
*/
-static inline struct rw_semaphore *
-__rwsem_do_wake(struct rw_semaphore *sem, int downgrading)
+static struct rw_semaphore *
+__rwsem_do_wake(struct rw_semaphore *sem, int wake_type)
{
struct rwsem_waiter *waiter;
struct task_struct *tsk;
@@ -58,9 +62,9 @@ __rwsem_do_wake(struct rw_semaphore *sem, int downgrading)
if (!(waiter->flags & RWSEM_WAITING_FOR_WRITE))
goto readers_only;
- if (downgrading)
- /* Caller's lock is still active, so we can't possibly
- * succeed waking writers.
+ if (wake_type != RWSEM_WAKE_ANY)
+ /* Another active reader was observed, so wakeup is not
+ * likely to succeed. Save the atomic op.
*/
goto out;
@@ -115,7 +119,8 @@ __rwsem_do_wake(struct rw_semaphore *sem, int downgrading)
retry_readers:
oldcount = rwsem_atomic_update(adjustment, sem) - adjustment;
- if (!downgrading && (oldcount < RWSEM_WAITING_BIAS))
+ if (wake_type != RWSEM_WAKE_READ_OWNED &&
+ oldcount < RWSEM_WAITING_BIAS)
/* Someone grabbed the sem for write already */
goto undo_readers;
@@ -172,9 +177,16 @@ rwsem_down_failed_common(struct rw_semaphore *sem,
/* we're now waiting on the lock, but no longer actively read-locking */
count = rwsem_atomic_update(adjustment, sem);
- /* if there are no active locks, wake the front queued process(es) up */
+ /* if there are no active locks, wake the front queued process(es) up.
+ *
+ * or if we're called from a failed down_write(), and there were
+ * already threads queued before us, and there are no active writers,
+ * the lock must be read owned; try to wake any read locks that were
+ * queued ahead of us. */
if (!(count & RWSEM_ACTIVE_MASK))
- sem = __rwsem_do_wake(sem, 0);
+ sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY);
+ else if (adjustment > 0 && count > RWSEM_WAITING_BIAS)
+ sem = __rwsem_do_wake(sem, RWSEM_WAKE_READERS);
spin_unlock_irq(&sem->wait_lock);
@@ -230,7 +242,7 @@ asmregparm struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem)
/* do nothing if list empty */
if (!list_empty(&sem->wait_list))
- sem = __rwsem_do_wake(sem, 0);
+ sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY);
spin_unlock_irqrestore(&sem->wait_lock, flags);
@@ -250,7 +262,7 @@ asmregparm struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem)
/* do nothing if list empty */
if (!list_empty(&sem->wait_list))
- sem = __rwsem_do_wake(sem, 1);
+ sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED);
spin_unlock_irqrestore(&sem->wait_lock, flags);
--
1.7.0.1
next prev parent reply other threads:[~2010-05-12 3:22 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-05-12 3:20 [PATCH 00/12] rwsem changes + down_read_unfair() proposal Michel Lespinasse
2010-05-12 3:20 ` [PATCH 01/12] rwsem: test for no active locks in __rwsem_do_wake undo code Michel Lespinasse
2010-05-12 3:20 ` [PATCH 02/12] rwsem: use single atomic update for sem count when waking up readers Michel Lespinasse
2010-05-12 3:20 ` [PATCH 03/12] rwsem: let RWSEM_WAITING_BIAS represent any number of waiting threads Michel Lespinasse
2010-05-12 3:20 ` [PATCH 04/12] rwsem: consistently use adjustment variable Michel Lespinasse
2010-05-12 3:20 ` [PATCH 05/12] x86 rwsem: take advantage of new RWSEM_WAITING_BIAS semantics Michel Lespinasse
2010-05-12 3:20 ` [PATCH 06/12] rwsem: wake queued readers when other readers are active Michel Lespinasse
2010-05-12 3:20 ` Michel Lespinasse [this message]
2010-05-12 3:20 ` [PATCH 08/12] rwsem: smaller wrappers around rwsem_down_failed_common Michel Lespinasse
2010-05-12 3:20 ` [PATCH 09/12] generic rwsem: implement down_read_unfair Michel Lespinasse
2010-05-12 3:21 ` [PATCH 10/12] rwsem: down_read_unfair infrastructure support Michel Lespinasse
2010-05-12 3:21 ` [PATCH 11/12] x86 rwsem: down_read_unfair implementation Michel Lespinasse
2010-05-12 3:21 ` [PATCH 12/12] Use down_read_unfair() for /sys/<pid>/exe and /sys/<pid>/maps files Michel Lespinasse
2010-05-12 22:53 ` KOSAKI Motohiro
2010-05-12 23:35 ` Michel Lespinasse
2010-05-13 0:32 ` KOSAKI Motohiro
2010-05-12 10:39 ` [PATCH 01/12] rwsem: test for no active locks in __rwsem_do_wake undo code David Howells
2010-05-12 11:01 ` [PATCH 02/12] rwsem: use single atomic update for sem count when waking up readers David Howells
2010-05-13 0:54 ` Michel Lespinasse
2010-05-12 11:36 ` David Howells
2010-05-12 11:45 ` [PATCH 04/12] rwsem: consistently use adjustment variable David Howells
2010-05-13 1:12 ` Michel Lespinasse
2010-05-12 12:10 ` [PATCH 05/12] x86 rwsem: take advantage of new RWSEM_WAITING_BIAS semantics David Howells
2010-05-12 12:22 ` [PATCH 06/12] rwsem: wake queued readers when other readers are active David Howells
2010-05-13 2:39 ` Michel Lespinasse
2010-05-13 5:41 ` Michel Lespinasse
2010-05-12 12:33 ` [PATCH 07/12] rwsem: wake queued readers when writer blocks on active read lock David Howells
2010-05-12 12:36 ` [PATCH 08/12] rwsem: smaller wrappers around rwsem_down_failed_common David Howells
2010-05-12 12:42 ` David Howells
2010-05-13 2:54 ` Michel Lespinasse
2010-05-12 12:46 ` [PATCH 09/12] generic rwsem: implement down_read_unfair David Howells
2010-05-12 13:08 ` [PATCH 11/12] x86 rwsem: down_read_unfair implementation David Howells
2010-05-12 13:10 ` [PATCH 12/12] Use down_read_unfair() for /sys/<pid>/exe and /sys/<pid>/maps files David Howells
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=1273634462-2672-8-git-send-email-walken@google.com \
--to=walken@google.com \
--cc=akpm@linux-foundation.org \
--cc=dhowells@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mikew@google.com \
--cc=mingo@elte.hu \
--cc=suleiman@google.com \
--cc=tglx@linutronix.de \
--cc=torvalds@linux-foundation.org \
--cc=yinghan@google.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
Powered by JetHome