qrwlock: A queue read/write lock implementation
In the interest of more useful Kconfig help, could I recommend the following text:
config QUEUE_RWLOCK
bool "Generic queue read/write lock"
depends on ARCH_QUEUE_RWLOCK
help
Use an alternative reader-writer lock (rwlock) implementation, optimized for larger NUMA systems. These locks use more memory, but perform better under high contention. (Specifically, readers waiting for a writer to release the lock will be queued rather than all spinning on the same cache line.)
The kernel will operate correctly either way; this only affects performance.
For common desktop and server systems systems with only one or two CPU sockets, the performance benefits are not worth the additional memory; say N here.
My goal is to give someone stumbling across this question for the first time in "make oldconfig" the information htey need to answer it.
That said, I think Ingo's idea for simplfying the waiting reader side is excellent and should be tried before bifurcating the implementation.
Looking at the lock system, it *seems* like that patch to __read_lock_failed is literally the *only* thing that needs changing, since the write lock/unlock is all done with relative add/sub anyway. But I keep thinking "there must have been a reason why it wasn't done that way in the first place".
Quoted 16 lines · George Spelvin
In the interest of more useful
Kconfighelp, could I recommend the following text:config
QUEUE_RWLOCK
bool "Generic queue read/write lock"
depends onARCH_QUEUE_RWLOCK
help
Use an alternative reader-writer lock (rwlock) implementation, optimized for larger NUMA systems. These locks use more memory, but perform better under high contention. (Specifically, readers waiting for a writer to release the lock will be queued rather than all spinning on the same cache line.)The kernel will operate correctly either way; this only affects performance.
Quoted 6 lines
For common desktop and server systems systems with only one or two CPU sockets, the performance benefits are not worth the additional memory; say N here.
My goal is to give someone stumbling across this question for the first time in "make oldconfig" the information htey need to answer it.
Thank for the revision, I will make such a change in the next version of my patch.
Quoted 8 lines
That said, I think Ingo's idea for simplfying the waiting reader side is excellent and should be tried before bifurcating the implementation.
Looking at the lock system, it *seems* like that patch to
__read_lock_failedis literally the *only* thing that needs changing, since the write lock/unlock is all done with relative add/sub anyway. But I keep thinking "there must have been a reason why it wasn't done that way in the first place".
As I said in my response to Ingo, that change will make the lock more unfair to the writers. However, I can run some tests to find out the performance impact of such a way on the benchmarks that I used.
Regards,
Longman
Quoted 2 lines
Thank for the revision, I will make such a change in the next version of my patch.
I'm relying on you to correct any technical errors in my description. I just meant "something more like this", not impose that exact wording.
Quoted 3 lines
As I said in my response to Ingo, that change will make the lock more unfair to the writers. However, I can run some tests to find out the performance impact of such a way on the benchmarks that I used.
You can do a lot with the basic structure if you're willing to go to XADD in the _lock_failed handlers.
Adding writer priority is possible, but at least some call sites have to be reader-priority because they're recursive or are in interrupts, and readers don't disable interrupts.
The basic solution is for writers to first detect if they're the *first* writer. If a write lock fails, look and see if it's > -RW_LOCK_BIAS.
If not, drop it and spin reading until it's >= 0, then try again. (Possibly with XADD this time to combine the acquire and add.)
But when we *are* the first writer, subtract an additional -RW_LOCK_BIAS/2. This tells pending readers "writer wating, back off!".
What readers do when they see that bit set in rwlock->lock-1 depends on whether they're fair or unfair:
- Fair readers re-increment the lock and spin waiting for it to become > -RW_LOCK_BIAS/2, at which point they try to reacquire.
- Unfair readers say "ah, that means I can go ahead, thank you!".
A waiting writer waits for the lock to equal -RW_LOCK_BIAS/2, meaning all readers have left. Then it adds -RW_LOCK_BIAS/2, meaning "write lock taken" and goes ahead.
At this point, there is a thundering horde while the held-off readers get back in line. But hopefully it overlaps with the writer's lock tenure.
When the writer drops its lock, the waiting readers go ahead, and the spinning writers advance in a thundering horde to contend for first place. (Again, the latency for this is hopefully covered by the readers' lock tenure.)
I count 531 calls to read_lock in the kernel (10 of them are in lib/locking-selftest.c, called RL). I don't have any idea how difficult it would be to divide them into read_lock_fair and read_lock_unfair.
Quoted 37 lines · George Spelvin
Quoted 2 lines
Thank for the revision, I will make such a change in the next version of my patch.
I'm relying on you to correct any technical errors in my description. I just meant "something more like this", not impose that exact wording.
Quoted 3 lines
As I said in my response to Ingo, that change will make the lock more unfair to the writers. However, I can run some tests to find out the performance impact of such a way on the benchmarks that I used.
You can do a lot with the basic structure if you're willing to go to XADD in the
_lock_failedhandlers.Adding writer priority is possible, but at least some call sites have to be reader-priority because they're recursive or are in interrupts, and readers don't disable interrupts.
The basic solution is for writers to first detect if they're the *first* writer. If a write lock fails, look and see if it's> -RW_LOCK_BIAS.
If not, drop it and spin reading until it's>= 0, then try again. (Possibly with XADD this time to combine the acquire and add.)
But when we *are* the first writer, subtract an additional -RW_LOCK_BIAS/2. This tells pending readers "writer wating, back off!".
What readers do when they see that bit set in rwlock->lock-1 depends on whether they're fair or unfair:
- Fair readers re-increment the lock and spin waiting for it to become> -RW_LOCK_BIAS/2, at which point they try to reacquire.
- Unfair readers say "ah, that means I can go ahead, thank you!".A waiting writer waits for the lock to equal -RW_LOCK_BIAS/2, meaning all readers have left. Then it adds -RW_LOCK_BIAS/2, meaning "write lock taken" and goes ahead.
Quoted 7 lines
At this point, there is a thundering horde while the held-off readers get back in line. But hopefully it overlaps with the writer's lock tenure.
When the writer drops its lock, the waiting readers go ahead, and the spinning writers advance in a thundering horde to contend for first place. (Again, the latency for this is hopefully covered by the readers' lock tenure.)
Thank for the suggestion. What you have proposed will be somewhat similar to what my new code is doing with readers/writers spinning on the cacheline without the waiting queue. I need to run some tests to see if it can really help.
Quoted 3 lines
I count 531 calls to
read_lockin the kernel (10 of them are inlib/locking-selftest.c, called RL). I don't have any idea how difficult it would be to divide them intoread_lock_fairandread_lock_unfair.
What I have in mind is to have 2 separate rwlock initializers - one for fair and one for reader-bias behavior. So the lock owners can decide what behavior do they want with a one line change.
Regards,
Longman
Quoted 3 lines
What I have in mind is to have 2 separate rwlock initializers - one for fair and one for reader-bias behavior. So the lock owners can decide what behavior do they want with a one line change.
That's definitely a nicer patch, if it will work. I was imagining that, even for a single (type of) lock, only a few uses require reader bias (because they might be recursive, or are in an interrupt), but you'd want most read_lock sites to be fair.
Deciding on a per-lock basis means that one potentially recursive call means you can't use fair queueing anywhere.
I was hoping that the number of necessary unfair calls would be small enough that making the read_lock default fair and only marking the unfair call sites would be enough.
But I don't really know until doing a survey of the calls.
Quoted 7 lines · George Spelvin
Quoted 3 lines
What I have in mind is to have 2 separate rwlock initializers - one for fair and one for reader-bias behavior. So the lock owners can decide what behavior do they want with a one line change.
That's definitely a nicer patch, if it will work. I was imagining that, even for a single (type of) lock, only a few uses require reader bias (because they might be recursive, or are in an interrupt), but you'd want most
read_locksites to be fair.
Yes, fair rwlock will be the default.
Quoted 8 lines
Deciding on a per-lock basis means that one potentially recursive call means you can't use fair queueing anywhere.
I was hoping that the number of necessary unfair calls would be small enough that making the
read_lockdefault fair and only marking the unfair call sites would be enough.But I don't really know until doing a survey of the calls.
I think so. The queue read/write lock, if merged, will be an optional feature for people to try out to see if they see any problem in any of the existing rwlock. So far, I didn't encounter any problem in my testing.
BTW, I also tried my version of the rwlock without the waiting queue. In high contention case, it performs slightly better than the __read_lock_failed changes suggested by Ingo at least for the reader-bias one. It is still not as good as the full version with the waiting queue. I should be able to provide more performance data next week.
Regards,
Longman