Re: [RFC PATCH] Writer-biased low-latency rwlock v8

Linus Torvalds
2008-08-21T21:43:16Z

On Thu, 21 Aug 2008, H. Peter Anvin wrote:
>
> First of all, let me say I don't pretend to understand formally how you deal
> with overflow-after-the-fact, as unlikely as it is.

Just make sure it can't overflow. With spinlocks, you are guaranteed that
you won't have more than NR_CPU's thing, so 20 bits is pretty safe. 30
bits is ridiculously safe.

> However, it seems to me to be an easy way to avoid it. Simply by changing the
> read-test mask to $0x80000003, you will kick the code down the slow path once
> the read counter reaches $0x80000004 (2^29+1 readers), where you can do any
> necessary fixup -- or BUG() -- at leisure.

Sure, you could do things like that, but that sounds like a separate
"debugging" version, not the main one.

> This fastpath ends up being identical in size and performance to the one you
> posted, although yours could be reduced by changing the test to a testb
> instruction -- at the almost certainly unacceptable expense of taking a
> partial-register stall on the CPUs that have those.

Well, you could just change the "testl $3,%eax" into an "andl $3,%eax",
and it will be two bytes shorter with no partial register stall.

I forgot that "testl" doesn't have the byte immediate version.

Linus