From: Linus Torvalds <torvalds@linux-foundation.org>
To: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>,
"H. Peter Anvin" <hpa@zytor.com>,
Jeremy Fitzhardinge <jeremy@goop.org>,
Andrew Morton <akpm@linux-foundation.org>,
Ingo Molnar <mingo@elte.hu>, Joe Perches <joe@perches.com>,
linux-kernel@vger.kernel.org,
Steven Rostedt <rostedt@goodmis.org>
Subject: Re: [RFC PATCH] Writer-biased low-latency rwlock v8
Date: Thu, 21 Aug 2008 15:22:56 -0700 (PDT) [thread overview]
Message-ID: <alpine.LFD.1.10.0808211500530.3487@nehalem.linux-foundation.org> (raw)
In-Reply-To: <alpine.LFD.1.10.0808211402370.3487@nehalem.linux-foundation.org>
On Thu, 21 Aug 2008, Linus Torvalds wrote:
>
> rdlock:
> movl $4,%eax
> lock ; xaddl %eax,(%rdi)
> testl $3,%eax
> jne __rdlock_slowpath
> ret
Ok, so change that "testl" to "andl" to shave two bytes, and then make the
whole lock structure look something like this:
struct rwlock {
u32 fastpath;
u32 pending_readers;
u32 pending_writers;
};
and then the slowpath would look something like this:
void __rdlock_slowpath(struct rwlock *lock)
{
/* Move the read count to the pending path and undo our xadd */
atomic_add(1, &lock->pending_readers);
atomic_sub(4, &lock->fastpath);
for (;;) {
unsigned fastpath;
while (lock->pending_writers)
cpu_relax();
while ((fastpath = lock->fastpath) & 1)
cpu_relax();
/* This will also undo the contention bit */
if (atomic_cmpxchg(&lock->fastpath, fastpath, (fastpath & ~2)+4)) == fastpath);
break;
}
atomic_sub(1, &lock->pending_readers);
}
and yes, there are more atomic accesses in there than would be necessary,
in that if you can cram the whole thing into a single 64-bit word you can
do both the initial pending fixup and the final cmpxchg/pending_readers
thing as a single locked 64-bit op, but hey, the above is fairly simple.
You could also just use a spinlock to protect all the other data, of
course.
There are fairness issues here too - do you really want to wait for
pending writes every time, or just the first time through the loop? It
depends on just how much you want to prefer writers.
The slow-paths for writers is similar, and has similar issues for the
fairness issue. For example, instead of a simple "pending_writers" count,
maybe you want to use a ticket lock there, to make writers be fair among
themselves? Of course, the hope would be that there would never be that
kind of contention by pure writers, so that sounds like overdesign, but
the thing I'm trying to point out here is that this is all a separate
decision from the actual fast-path, and having fair writers doesn't
necessarily mean that the fastpath has to even know/care.
The placement of the counts is also something that can be optimized. For
example, on teh assumption that readers are much more common than writers,
I put the "pending_readers" next to the "fastpath" lock, exactly so that
you -can- do the update of both with a single 64-bit locked access, even
if the fastpath just used a 32-bit one. But then you should at least add a
"__attribute__((aligned(8)))" to the structure definition.
And maybe you want to share the slowpath between all the irq-off etc
versions, in which case you need to make the "irq enable while waiting"
thing be conditional.
And if the write path wants to wait for pending readers, and you want to
make _that_ fair too, then you want to have that punch through if the
writer is in an interrupt, to avoid deadlocks. And again, you might want
to make the reader thing be some kind of ticket-based system, so that
writers can wait for the readers that came in _before_ that writer, but
not to later ones.
But again - none of that has anything to do with the fast-path. The
fast-path remains exactly the same for all these issues.
And finally: NONE of the code is at all tested. It's all just written in
my MUA. Caveat emptor.
Linus
next prev parent reply other threads:[~2008-08-21 22:24 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-08-16 7:39 [PATCH] x86_64 : support atomic ops with 64 bits integer values Mathieu Desnoyers
2008-08-16 15:04 ` H. Peter Anvin
2008-08-16 15:43 ` Mathieu Desnoyers
2008-08-16 17:30 ` Linus Torvalds
2008-08-16 21:19 ` [RFC PATCH] Fair rwlock Mathieu Desnoyers
2008-08-16 21:33 ` Linus Torvalds
2008-08-17 7:53 ` [RFC PATCH] Fair low-latency rwlock v3 Mathieu Desnoyers
2008-08-17 16:17 ` Linus Torvalds
2008-08-17 19:10 ` [RFC PATCH] Fair low-latency rwlock v5 Mathieu Desnoyers
2008-08-17 21:30 ` [RFC PATCH] Fair low-latency rwlock v5 (updated benchmarks) Mathieu Desnoyers
2008-08-18 18:59 ` [RFC PATCH] Fair low-latency rwlock v5 Linus Torvalds
2008-08-18 23:25 ` Paul E. McKenney
2008-08-19 6:04 ` Mathieu Desnoyers
2008-08-19 7:33 ` Mathieu Desnoyers
2008-08-19 9:06 ` Mathieu Desnoyers
2008-08-19 16:48 ` Linus Torvalds
2008-08-21 20:50 ` [RFC PATCH] Writer-biased low-latency rwlock v8 Mathieu Desnoyers
2008-08-21 21:00 ` Linus Torvalds
2008-08-21 21:15 ` Linus Torvalds
2008-08-21 22:22 ` Linus Torvalds [this message]
2008-08-23 5:09 ` Mathieu Desnoyers
2008-08-23 18:02 ` Linus Torvalds
2008-08-23 20:30 ` Mathieu Desnoyers
2008-08-23 21:40 ` Linus Torvalds
2008-08-21 21:26 ` H. Peter Anvin
2008-08-21 21:41 ` Linus Torvalds
2008-08-25 19:20 ` [RFC PATCH] Fair low-latency rwlock v5 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=alpine.LFD.1.10.0808211500530.3487@nehalem.linux-foundation.org \
--to=torvalds@linux-foundation.org \
--cc=akpm@linux-foundation.org \
--cc=hpa@zytor.com \
--cc=jeremy@goop.org \
--cc=joe@perches.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mathieu.desnoyers@polymtl.ca \
--cc=mingo@elte.hu \
--cc=paulmck@linux.vnet.ibm.com \
--cc=rostedt@goodmis.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
Powered by JetHome