qrwlock: Introducing a queue read/write lock implementation
Subsystems (4)
v2 cover qrwlock: Introducing a queue read/write lock implementation
v1->v2:
- Improve lock fastpath performance.
- Optionally provide classic read/write lock behavior for backward compatibility.
- Use xadd instead of cmpxchg for fair reader code path to make it immute to reader contention.
- Run more performance testing.
As mentioned in the LWN article http://lwn.net/Articles/364583/, the classic read/write lock suffer from an unfairness problem that it is possible for a stream of incoming readers to block a waiting writer from getting the lock for a long time. Also, a waiting reader/writer contending a rwlock in local memory will have a higher chance of acquiring the lock than a reader/writer in remote node.
This patch set introduces a queue-based read/write lock implementation that can largely solve this unfairness problem while, at the same time, can optionally provide the classic read/write lock behavior for those locks that need it. This new queue read/write lock is both fairer than the classic read/write lock and will be faster in lock contention situations.
The queue write lock can also be used as a replacement for ticket spinlocks that are highly contended if lock size increase is not an issue.
There is no change in the interface. By just selecting the QUEUE_RWLOCK config parameter during the configuration phase, the classic read/write lock will be replaced by the new queue read/write lock. This will made the systems more deterministic and faster in lock contention situations. In uncontended cases, the queue read/write lock may be a bit slower than the classic one depending on the exact mix of read and write locking primitives. Given the fact that locking overhead is typically a very small percentage of the total CPU time in uncontended cases, there won't be any noticeable degraduation in performance with this replacement.
This patch set currently provides queue read/write lock support on x86 architecture only. Support for other architectures can be added later on once proper testing is done.
Waiman Long (3):
qrwlock: A queue read/write lock implementation qrwlock x86: Enable x86 to use queue read/write lock
qrwlock: Optionally enable classic read/write lock behavior
arch/x86/Kconfig | 3 + arch/x86/include/asm/spinlock.h | 2 + arch/x86/include/asm/spinlock_types.h | 4 + include/asm-generic/qrwlock.h | 251 +++++++++++++++++++++++++++++++++ include/linux/rwlock.h | 15 ++ include/linux/rwlock_types.h | 12 ++- include/linux/spinlock_types.h | 4 + lib/Kconfig | 23 +++ lib/Makefile | 1 + lib/qrwlock.c | 194 +++++++++++++++++++++++++ lib/spinlock_debug.c | 20 +++ 11 files changed, 528 insertions(+), 1 deletions(-) create mode 100644 include/asm-generic/qrwlock.h create mode 100644 lib/qrwlock.c
PATCH v2 1/3 qrwlock: A queue read/write lock implementation
This patch introduces a read/write lock implementation that put waiting readers and writers into a queue instead of actively contending the lock like the classic read/write lock. This will improve performance in highly contended situation by reducing the cache line bouncing effect.
In addition, the queue read/write lock is more deterministic in the sense that late comers jumping ahead and stealing the lock is unlikely even though there is still a very small chance for lock stealing to happen if the reader or writer comes at the right moment. Other than that, lock granting is done in a FIFO manner. As a result, it is possible to determine a maximum time period after which the waiting is over and the lock can be acquired. The only downside is the size increase in the lock structure by 4 bytes for 32-bit systems and by 12 bytes for 64-bit systems.
This patch allows the optional replacement of architecture specific implementation of read/write lock by this generic version of queue read/write lock. Two new config parameters are introduced:
1. QUEUE_RWLOCK
A select-able option that enables the building and replacement of architecture specific read/write lock by queue read/write lock.
2. ARCH_QUEUE_RWLOCK
Have to be defined in arch/$(ARCH)/Kconfig to enable QUEUE_RWLOCK option. This option, by itself, will not enable the queue read/write lock feature.
The queue read/write lock can also provide the classic read/write lock behavior of a steady stream of readers stealing the read lock from a waiting writer by using a different initializer. This behavior can be beneficial for workloads that is reader heavy and the writers can wait or when deviation from the classic behavior can cause problem.
The queue read/write lock readers have two slightly different locking code paths - one for fair readers and one for classic readers. The fair readers can use xadd to increment the reader count as the readers can back off when they find the writer field set. This makes the fair readers code path less affected by delay due to reader contention.
The classic readers with lock stealing cannot use xadd as they can't be certain if the writer has seen the reader count going to 0 and hence owns the lock. So they have to use cmpxchg for this purpose. This makes the classic readers subject to delay caused by reader contention. However, the ability to steal the lock from a waiting writer also make them less susceptible to contention delay caused by the writer. So the two types of readers are somewhat comparable in speed depending on the exact mix of reader and writer activities.
In term of single-thread performance (no contention), a 256K lock/unlock loop was run on a 2.4GHz and 2.93Ghz Westmere as well as a 3.4GHz Ivy-Bridge x86-64 CPU. The following table shows the average time (in ns) for a single lock/unlock sequence (including the looping and timing overhead):
Lock Type 2.4GHz 2.93GHz 3.4GHz
--------- ------ ------- ------
Ticket spinlock 14.9 12.1 10.6
Read lock 17.0 13.5 12.2
Write lock 17.2 13.5 12.2
Queue fair read lock 20.2 16.5 15.7
Queue classic read lock 22.4 19.0 16.3
Queue write lock 9.3 7.8 7.4
While the queue read lock is about 1.5X the time of a spinlock, the queue write lock is just about 2/3 of the time of that spinlock. Comparing to the classic read/write lock, the queue read lock is about 1/3 slower while the queue write lock is about slightly more than 1/3 faster.
With lock contention, the speed of each individual lock/unlock function is less important than the amount of contention-induced delays.
To investigate the performance characteristics of the queue read/write lock compared with the classic read/write lock, the fserver and new_fserver benchmarks with ext4 filesystem of the AIM7 test suite were used on a 8-socket x86-64 system with 80 cores. The read/write lock in contention was the j_state_lock of the journal_s structure in include/linux/jbd2.h.
The following kernels were used:
1) Vanilla 3.10.1
2) 3.10.1 with qrwlock patch
3) 3.10.1 with qrwlock patch and classic behavior for j_state_lock (classic behavior - readers can steal lock from waiting writer)
The following table shows the averaged results in the 200-1000 user ranges:
+-----------------+-------+-------+-------+-------+-------+-------+ | Kernel | 1 | 2 | 3 | 1 | 2 | 3 | | HT | on | on | on | off | off | off | +-----------------+-------+-------+-------+-------+-------+-------+ | fserver JPM |308872 |372662 |298547 |460371 |463712 |454369 | | % change from 1 | 0% |+20.7% | -3.3% | 0% | +0.7% | -1.3% | +-----------------+-------+-------+-------+-------+-------+-------+ | new_fserver JPM |314730 |385665 |321122 |441284 |441606 |441579 | | % change from 1 | 0% |+22.5% | +2.0% | 0% | +0.1% | +0.1% | +-----------------+-------+-------+-------+-------+-------+-------+
The following table shows the averaged results in the 1100-2000 user ranges:
+-----------------+-------+-------+-------+-------+-------+-------+ | Kernel | 1 | 2 | 3 | 1 | 2 | 3 | | HT | on | on | on | off | off | off | +-----------------+-------+-------+-------+-------+-------+-------+ | fserver JPM |263151 |332506 |325699 |472254 |472166 |464351 | | % change from 1 | 0% |+26.4% |+23.8% | 0% | 0.0% | -1.7% | +-----------------+-------+-------+-------+-------+-------+-------+ | new-fserver JPM |264615 |322803 |297902 |445516 |450811 |452229 | | % change from 1 | 0% |+22.0% |+12.6% | 0% | +1.2% | +1.5% | +-----------------+-------+-------+-------+-------+-------+-------+
The following table show the amount of CPU time spent on the fastpaths and slowpaths of the read and write lock for each of the different kernels listed above as reported by perf with the fserver workload at 1500 users:
+-----------------+-------+-------+-------+-------+-------+-------+ | Kernel | 1 | 2 | 3 | 1 | 2 | 3 | | HT | on | on | on | off | off | off | +-----------------+-------+-------+-------+-------+-------+-------+ | Read fastpath | 0.83% | 0.18% | 0.19% | 0.22% | 0.21% | 0.22% | | Read slowpath | 13.4% | 18.9% | 18.2% | 0.28% | 0.52% | 0.55% | | Write fastpath | 0.10% | 0.01% | 0.02% | 0.01% | 0.01% | 0.01% | | Write slowpath | 11.1% | 0.05% | 0.14% | 0.08% | 0.00% | 0.00% | +-----------------+-------+-------+-------+-------+-------+-------+
The small write slowpath numbers for queue read/write lock indicates that it is a reader heavy lock with writers probably holding the lock a lot longer than the readers. The queue read/write lock of both favors causes a big reduction in the amount of time spent by the writers to get the lock. Even though the readers need to wait longer, it is more than compensated by the reduction in writers' time.
To see how the queue write lock can be used as a replacement for ticket spinlock (just like rwsem can be used as replacement of mutex), the mb_cache_spinlock in fs/mbcache.c, which is a bottleneck in the disk workload (ext4 FS) of the AIM7 benchmark, was converted to both a queue write lock and a regular write lock.
The following kernels were used when running on a 8-socket 80-core DL980 system:
1) Vanila 3.10.1
2) 3.10.1 with mb_cache_spinlock replaced by queue write lock
3) 3.10.1 with mb_cache_spinlock replaced by classic write lock
The following table shows the averaged JPM results for the disk workload:
+----------+--------+--------+--------+--------+--------+--------+ | Kernel | 1 | 2 | 3 | 1 | 2 | 3 | | HT | off | off | off | on | on | on | +----------+--------+--------+--------+--------+--------+--------+ | | User Range 10 - 100 | +----------+--------+--------+--------+--------+--------+--------+ | disk JPM | 439561 | 609084 | 614491 | 441911 | 572606 | 625140 | | % change | 0% | +38.6% | +39.8% | 0% | +29.6% | +41.5% | +----------+--------+--------+--------+--------+--------+--------+ | | User Range 200 - 1000 | +----------+--------+--------+--------+--------+--------+--------+ | disk JPM | 223976 | 363216 | 394828 | 207602 | 331567 | 350162 | | % change | 0% | +62.1% | +76.3% | 0% | +59.7% | +68.7% | +----------+--------+--------+--------+--------+--------+--------+ | | User Range 1100 - 2000 | +----------+--------+--------+--------+--------+--------+--------+ | disk JPM | 216317 | 324617 | 326147 | 200440 | 294092 | 258985 | | % change | 0% | +50.1% | +50.8% | 0% | +46.7% | +29.2% | +----------+--------+--------+--------+--------+--------+--------+
The amount of time spent in mbcache lock contention as reported by perf for the disk workload at 1000 users were listed in the following table.
+----------------+-------+-------+-------+-------+-------+-------+ | Kernel | 1 | 2 | 3 | 1 | 2 | 3 | | HT | off | off | off | on | on | on | +----------------+-------+-------+-------+-------+-------+-------+ | _raw_spin_lock | 76.9% | - | - | 72.1% | - | - | | wlock fastpath | - | 0.09% | 0.53% | - | 0.06% | 0.39% | | wlock slowpath | - | 68.1% | 62.8% | - | 67.5% | 60.2% | +----------------+-------+-------+-------+-------+-------+-------+
The higher amount of time spent in the classic write lock fastpath indicates a higher level contention at the lock level. Fortunately, the lock is in a separate cacheline from the other data manipulated by mbcache. Hence, its performance isn't affected that much by contention in the lock. It seems like classic write lock can response to lock availability most rapidly followed by queue write lock and then spinlock.
The following table show the performance data for the same set of kernels on a 2-socket 12-core system with HT on:
+----------+---------+---------+---------+ | Kernel | 1 | 2 | 3 | | HT | on | on | on | +----------+---------+---------+---------+ | | User Range 10 - 100 | +----------+---------+---------+---------+ | disk JPM | 1805396 | 1800737 | 1834068 | | % change | 0% | -0.3% | +1.6% | +----------+---------+---------+---------+ | | User Range 200 - 1000 | +----------+---------+---------+---------+ | disk JPM | 2697514 | 2746543 | 2709771 | | % change | 0% | +1.8% | +0.5% | +----------+---------+---------+---------+ | | User Range 1100 - 2000 | +----------+---------+---------+---------+ | disk JPM | 2700273 | 2771454 | 2734525 | | % change | 0% | +2.6% | +1.3% | +----------+---------+---------+---------+
Apparently, the classic write lock performs even better than the queue write lock in some cases. However, the queue write lock is fair, but the classic write lock is not. So it is not a very good replacement for ticket spinlock.
--- include/asm-generic/qrwlock.h | 251 +++++++++++++++++++++++++++++++++++++++++ lib/Kconfig | 23 ++++ lib/Makefile | 1 + lib/qrwlock.c | 194 +++++++++++++++++++++++++++++++ 4 files changed, 469 insertions(+), 0 deletions(-) create mode 100644 include/asm-generic/qrwlock.h create mode 100644 lib/qrwlock.c
include/asm-generic/qrwlock.h new file +251 -0
@@ -0,0 +1,251 @@ +/* +* Queue read/write lock +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* (C) Copyright 2013 Hewlett-Packard Development Company, L.P. +* +* Authors: Waiman Long <[email protected]> +*/ +#ifndef __ASM_GENERIC_QRWLOCK_H +#define __ASM_GENERIC_QRWLOCK_H + +#include <linux/types.h> +#include <asm/cmpxchg.h> +#include <asm/barrier.h> +#include <asm/processor.h> +#include <asm/byteorder.h> + +#if !defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN) +#error "Missing either LITTLE_ENDIAN or BIG_ENDIAN definition." +#endif + +#if (CONFIG_NR_CPUS <65536 ) +typedef u16 __nrcpu_t ; +typedef u32 __nrcpupair_t ; +#define QRW_READER_BIAS (1U << 16) +#else +typedef u32 __nrcpu_t ; +typedef u64 __nrcpupair_t ; +#define QRW_READER_BIAS (1UL << 32) +#endif + +/* +* The queue read/write lock data structure +* +* Read lock stealing can only happen when there is at least one reader +* holding the read lock. When the read lock stealing flag is set, it mimics +* the behavior of the classical read/write lock at the expense that a +* perpetual stream of readers could starve a writer for a long period of time. +* That behavior, however, may be beneficial to a workload that is reader heavy +* with slow writers, and the writers can wait without undesirable consequence. +* It is also useful for read/write locks that may not work properly if the +* behavior deviates from the classical one. This flag should only be set +* at initialization time. +* +* The layout of the structure is endian-sensitive to make sure that adding +* QRW_READER_BIAS to the rw field to increment the reader count won't +* disturb the writer and the rsteal fields. +*/ +#ifndef _QUEUE_RWLOCK_NOWAITQ +struct qrwnode { +struct qrwnode *next; +bool wait;/* Waiting flag */ +}; +# define _DEFINE_QRWNODE (v) struct qrwnode v +# define _INIT_WAITQ , .waitq = NULL +#else +# define _DEFINE_QRWNODE (v) +# define _INIT_WAITQ +#endif + +typedef struct qrwlock { +union qrwcnts { +struct { +#ifdef __LITTLE_ENDIAN +u8 writer;/* Set if a writer is waiting */ +u8 rsteal;/* Read lock stealing flag */ +__nrcpu_t readers;/* Number of active readers */ +#else +__nrcpu_t readers;/* Number of active readers */ +u8 rsteal;/* Read lock stealing flag */ +u8 writer;/* Set if a writer is waiting */ +#endif + }; +__nrcpupair_t rw;/* Reader/writer number pair */ + } cnts; +_DEFINE_QRWNODE (*waitq);/* Tail of waiting queue */ +}arch_rwlock_t ; + +/* +* External function declarations +*/ +extern void queue_read_lock_slowpath (struct qrwlock *lock,int dec_reader); +extern void queue_write_lock_slowpath (struct qrwlock *lock); + +/** +* queue_read_can_lock- would read_trylock() succeed? +* @lock: Pointer to queue read/writer lock structure +*/ +static inline int queue_read_can_lock (struct qrwlock *lock) +{ +return lock->cnts.writer ==0 ; +} + +/** +* queue_write_can_lock- would write_trylock() succeed? +* @lock: Pointer to queue read/writer lock structure +*/ +static inline int queue_write_can_lock (struct qrwlock *lock) +{ +return (lock->cnts.writer ==0 ) && (lock->cnts.readers ==0 ); +} + +/** +* queue_read_lock - acquire read lock of a queue read/write lock +* @lock: Pointer to queue read/writer lock structure +* +* For fair queue read/write lock, xadd can be used to increment the reader +* count to remove failure cases due to reader contention. If a writer is now +* present, the slowpath will have to decrement the reader count first before +* waiting. For classic behavior queue read/write lock, we have to make sure +* that there is no illegal reader lock stealing by not incrementing reader +* count when writer is present. +*/ +static inline void queue_read_lock (struct qrwlock *lock) +{ +union qrwcnts old; +int dec_reader =0 ;/* Decrement reader count */ + + old.rw =ACCESS_ONCE (lock->cnts.rw); +if (likely (!old.writer && !old.rsteal)) { +/* Fair queue rwlock reader */ + old.rw =xadd (&lock->cnts.rw,QRW_READER_BIAS ); +if (likely (!old.writer)) +return ; + dec_reader =1 ; + }else if (likely (!old.writer)) { +/* Classic behavior queue rwlock reader */ +union qrwcnts new; + + new.rw = old.rw; + new.readers++; +if (likely (cmpxchg (&lock->cnts.rw, old.rw, new.rw) == old.rw)) +return ; + } +queue_read_lock_slowpath (lock, dec_reader); +} + +/** +* queue_write_lock - acquire write lock of a queue read/write lock +* @lock : Pointer to queue read/writer lock structure +*/ +static inline void queue_write_lock (struct qrwlock *lock) +{ +if (likely (!ACCESS_ONCE (lock->cnts.writer))) { +/* +* Atomically set the writer to 1, then wait until reader +* count goes to 0. +*/ +if (likely (xchg (&lock->cnts.writer,1 ) ==0 )) { +while (ACCESS_ONCE (lock->cnts.readers)) +cpu_relax (); +return ; + } + } +queue_write_lock_slowpath (lock); +} + +/* +* queue_read_trylock - try to acquire read lock of a queue read/write lock +* @lock : Pointer to queue read/writer lock structure +* Return: 1 if lock acquired, 0 if failed +*/ +static inline int queue_read_trylock (struct qrwlock *lock) +{ +union qrwcnts old, new; + + old.rw =ACCESS_ONCE (lock->cnts.rw); +if (unlikely (old.writer)) +return 0 ; + new.rw = old.rw; + new.readers++; +if (cmpxchg (&lock->cnts.rw, old.rw, new.rw) == old.rw) +return 1 ; +return 0 ; +} + +/** +* queue_write_trylock - try to acquire write lock of a queue read/write lock +* @lock : Pointer to queue read/writer lock structure +* Return: 1 if lock acquired, 0 if failed +*/ +static inline int queue_write_trylock (struct qrwlock *lock) +{ +union qrwcnts old, new; + + old.rw =ACCESS_ONCE (lock->cnts.rw); +if (!old.rw) { +/* +* Atomically set the writer to 1 if readers = 0 +*/ + new.rw = old.rw; + new.writer =1 ; +if (cmpxchg (&lock->cnts.rw, old.rw, new.rw) == old.rw) +return 1 ; + } +return 0 ; +} + +/** +* queue_read_unlock - release read lock of a queue read/write lock +* @lock : Pointer to queue read/writer lock structure +*/ +static inline void queue_read_unlock (struct qrwlock *lock) +{ +/* +* Atomically decrement the reader count +*/ +add_smp (&lock->cnts.readers,-1 ); +} + +/** +* queue_write_unlock - release write lock of a queue read/write lock +* @lock : Pointer to queue read/writer lock structure +*/ +static inline void queue_write_unlock (struct qrwlock *lock) +{ +ACCESS_ONCE (lock->cnts.writer) =0 ; +smp_wmb (); +} + +/* +* Initializier +*/ +#define __ARCH_RW_LOCK_UNLOCKED { .cnts = { .rw = 0 } _INIT_WAITQ } +#define __ARCH_RW_LOCK_UNLOCKED_RSTEAL \ + { .cnts = {{ .writer = 0, .rsteal = 1, .readers = 0 }} _INIT_WAITQ } +#define __ARCH_RW_LOCK_UNLOCKED_CLASSIC __ARCH_RW_LOCK_UNLOCKED_RSTEAL + +/* +* Remapping read/write lock architecture specific functions to the +* corresponding queue read/write lock functions. +*/ +#define arch_read_can_lock (l) queue_read_can_lock(l) +#define arch_write_can_lock (l) queue_write_can_lock(l) +#define arch_read_lock (l) queue_read_lock(l) +#define arch_write_lock (l) queue_write_lock(l) +#define arch_read_trylock (l) queue_read_trylock(l) +#define arch_write_trylock (l) queue_write_trylock(l) +#define arch_read_unlock (l) queue_read_unlock(l) +#define arch_write_unlock (l) queue_write_unlock(l) + +#endif /* __ASM_GENERIC_QRWLOCK_H */
lib/Kconfig +23 -0
@@ -412,6 +412,29 @@ config SIGNATURE Implementation is done using GnuPG MPI library # +# Generic queue read/write lock +# +config QUEUE_RWLOCK + bool "Generic queue read/write lock" + depends on ARCH_QUEUE_RWLOCK + default n + help + Use an alternative read/write lock (rwlock) implementation + that are fairer and more optmized for larger NUMA systems. + These locks use more memory, but is fair to both readers + and writers and perform better under high contention. + Specifically, waiting readers and writers will be queued + up and granted lock in FIFO order rather than all spinning + on the same cache line to compete for the lock. + + The kernel will operate correctly either way; this only + affects performance and lock fairness. + + For common desktop and server systems systems with only one + or two CPU sockets and small memory, the performance and + lock fairness benefits may not worth the additional memory. + +# # libfdt files, only selected if needed. # config LIBFDT
lib/Makefile +1 -0
@@ -187,3 +187,4 @@ quiet_cmd_build_OID_registry = GEN $@ clean-files += oid_registry_data.c obj-$(CONFIG_UCS2_STRING) += ucs2_string.o +obj-$(CONFIG_QUEUE_RWLOCK) += qrwlock.o
lib/qrwlock.c new file +194 -0
194 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH v2 2/3 qrwlock x86: Enable x86 to use queue read/write lock
In reply to Waiman LongThis patch makes the necessary changes at the x86 architecture specific layer to enable the presence of the CONFIG_QUEUE_RWLOCK kernel option which can be used to replace the classic read/write lock by the queue read/write lock.
The turning on of ARCH_QUEUE_RWLOCK config option does not mean that the classic read/write lock will be replaced. It only means that the architecture supports the replacement. Actual replacement will only happen if the QUEUE_RWLOCK config option is also set.
--- arch/x86/Kconfig | 3 +++ arch/x86/include/asm/spinlock.h | 2 ++ arch/x86/include/asm/spinlock_types.h | 4 ++++ 3 files changed, 9 insertions(+), 0 deletions(-)
arch/x86/Kconfig +3 -0
@@ -2344,6 +2344,9 @@ config X86_DMA_REMAP bool depends on STA2X11 +config ARCH_QUEUE_RWLOCK + def_bool y + source "net/Kconfig" source "drivers/Kconfig"
arch/x86/include/asm/spinlock.h +2 -0
@@ -137,6 +137,7 @@ static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)cpu_relax (); } +#ifndef CONFIG_QUEUE_RWLOCK /* * Read-write spinlocks, allowing multiple readers * but only one writer.
@@ -219,6 +220,7 @@ static inline void arch_write_unlock(arch_rwlock_t *rw) asm volatile(LOCK_PREFIX WRITE_LOCK_ADD(%1) "%0" : "+m" (rw->write) : "i" (RW_LOCK_BIAS) : "memory"); } +#endif /* CONFIG_QUEUE_RWLOCK */ #define arch_read_lock_flags (lock, flags) arch_read_lock(lock)#define arch_write_lock_flags (lock, flags) arch_write_lock(lock)
arch/x86/include/asm/spinlock_types.h +4 -0
@@ -28,6 +28,10 @@ typedef struct arch_spinlock {#define __ARCH_SPIN_LOCK_UNLOCKED { { 0 } } +#ifdef CONFIG_QUEUE_RWLOCK +#include <asm-generic/qrwlock.h> +#else #include <asm/rwlock.h> +#endif #endif /* _ASM_X86_SPINLOCK_TYPES_H */
Signature · 1 line
1.7.1
PATCH v2 3/3 qrwlock: Optionally enable classic read/write lock behavior
In reply to Waiman LongBy default, queue read/write lock is fair with respect to both the readers and writers. However, there are situations where a bias towards readers can increase throughput especially for reader-heavy situations. There may also be cases where deviation from the classic read/write lock behavior may cause problem like recursive read lock in an interrupt handler with a waiting writer. Using the classic behavior, however, will cause the queue read/write lock to lose some of the fairness attribute that it has.
This patch enables lock owners to decide what behavior do they want for their read/write lock by using the appropriate initializer. Two types of initializers will be provided:
1. Default - fair to both readers and writers
2. Classic - readers that come after a waiting writer can steal the lock
The classic initializers have a "_classic" suffix. If the queue read/write lock feature is not enabled, the classic initializer will be the same as the default initializer.
--- include/linux/rwlock.h | 15 +++++++++++++++ include/linux/rwlock_types.h | 12 +++++++++++- include/linux/spinlock_types.h | 4 ++++ lib/spinlock_debug.c | 20 ++++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletions(-)
include/linux/rwlock.h +15 -0
@@ -23,9 +23,24 @@ do { \ \ __rwlock_init((lock), #lock, &__key); \ } while (0) + +# ifdef CONFIG_QUEUE_RWLOCK +extern void __rwlock_init_classic (rwlock_t *lock,const char *name, +struct lock_class_key *key); +# define rwlock_init_classic (lock) \ +do { \ + static struct lock_class_key __key; \ + \ + __rwlock_init_classic((lock), #lock, &__key); \ +} while (0) +# else +# define __rwlock_init_classic (l,n,k) __rwlock_init(l,n,k) +# endif /* CONFIG_QUEUE_RWLOCK */ #else # define rwlock_init (lock) \ do { *(lock) = __RW_LOCK_UNLOCKED(lock); } while (0) +# define rwlock_init_classic (lock) \ + do { *(lock) = __RW_LOCK_UNLOCKED_CLASSIC(lock); } while (0)#endif #ifdef CONFIG_DEBUG_SPINLOCK
include/linux/rwlock_types.h +11 -1
@@ -37,12 +37,22 @@ typedef struct { .owner = SPINLOCK_OWNER_INIT, \ .owner_cpu = -1, \ RW_DEP_MAP_INIT(lockname) } +#define __RW_LOCK_UNLOCKED_CLASSIC (lockname) \ + (rwlock_t) { .raw_lock = __ARCH_RW_LOCK_UNLOCKED_CLASSIC,\ + .magic = RWLOCK_MAGIC, \ + .owner = SPINLOCK_OWNER_INIT, \ + .owner_cpu = -1, \ + RW_DEP_MAP_INIT(lockname) }#else #define __RW_LOCK_UNLOCKED (lockname) \ (rwlock_t) { .raw_lock = __ARCH_RW_LOCK_UNLOCKED, \ RW_DEP_MAP_INIT(lockname) } +#define __RW_LOCK_UNLOCKED_CLASSIC (lockname) \ + (rwlock_t) { .raw_lock = __ARCH_RW_LOCK_UNLOCKED_CLASSIC,\ + RW_DEP_MAP_INIT(lockname) }#endif -#define DEFINE_RWLOCK (x) rwlock_t x = __RW_LOCK_UNLOCKED(x) +#define DEFINE_RWLOCK (x) rwlock_t x = __RW_LOCK_UNLOCKED(x) +#define DEFINE_RWLOCK_CLASSIC (x) rwlock_t x = __RW_LOCK_UNLOCKED_CLASSIC(x)#endif /* __LINUX_RWLOCK_TYPES_H */
include/linux/spinlock_types.h +4 -0
@@ -85,4 +85,8 @@ typedef struct spinlock {#include <linux/rwlock_types.h> +#ifndef __ARCH_RW_LOCK_UNLOCKED_CLASSIC +#define __ARCH_RW_LOCK_UNLOCKED_CLASSIC __ARCH_RW_LOCK_UNLOCKED +#endif +#endif /* __LINUX_SPINLOCK_TYPES_H */
lib/spinlock_debug.c +20 -0
@@ -49,6 +49,26 @@ void __rwlock_init(rwlock_t *lock, const char *name,EXPORT_SYMBOL (__rwlock_init); +#ifdef CONFIG_QUEUE_RWLOCK +void __rwlock_init_classic (rwlock_t *lock,const char *name, +struct lock_class_key *key) +{ +#ifdef CONFIG_DEBUG_LOCK_ALLOC +/* +* Make sure we are not reinitializing a held lock: +*/ +debug_check_no_locks_freed ((void *)lock,sizeof (*lock)); +lockdep_init_map (&lock->dep_map, name, key,0 ); +#endif + lock->raw_lock = (arch_rwlock_t ) __ARCH_RW_LOCK_UNLOCKED_CLASSIC; + lock->magic =RWLOCK_MAGIC ; + lock->owner =SPINLOCK_OWNER_INIT ; + lock->owner_cpu =-1 ; +} + +EXPORT_SYMBOL (__rwlock_init_classic); +#endif /* CONFIG_QUEUE_RWLOCK */ +static void spin_dump (raw_spinlock_t *lock,const char *msg) {struct task_struct *owner =NULL ;
Signature · 1 line
1.7.1