diff options
| -rw-r--r-- | include/linux/rwsem.h | 25 | ||||
| -rw-r--r-- | kernel/locking/rwsem-xadd.c | 225 | ||||
| -rw-r--r-- | kernel/locking/rwsem.c | 31 |
3 files changed, 248 insertions, 33 deletions
diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h index 03f3b05e8ec1..3e108f154cb6 100644 --- a/include/linux/rwsem.h +++ b/include/linux/rwsem.h | |||
| @@ -16,6 +16,7 @@ | |||
| 16 | 16 | ||
| 17 | #include <linux/atomic.h> | 17 | #include <linux/atomic.h> |
| 18 | 18 | ||
| 19 | struct optimistic_spin_queue; | ||
| 19 | struct rw_semaphore; | 20 | struct rw_semaphore; |
| 20 | 21 | ||
| 21 | #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK | 22 | #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK |
| @@ -23,9 +24,17 @@ struct rw_semaphore; | |||
| 23 | #else | 24 | #else |
| 24 | /* All arch specific implementations share the same struct */ | 25 | /* All arch specific implementations share the same struct */ |
| 25 | struct rw_semaphore { | 26 | struct rw_semaphore { |
| 26 | long count; | 27 | long count; |
| 27 | raw_spinlock_t wait_lock; | 28 | raw_spinlock_t wait_lock; |
| 28 | struct list_head wait_list; | 29 | struct list_head wait_list; |
| 30 | #ifdef CONFIG_SMP | ||
| 31 | /* | ||
| 32 | * Write owner. Used as a speculative check to see | ||
| 33 | * if the owner is running on the cpu. | ||
| 34 | */ | ||
| 35 | struct task_struct *owner; | ||
| 36 | struct optimistic_spin_queue *osq; /* spinner MCS lock */ | ||
| 37 | #endif | ||
| 29 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | 38 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 30 | struct lockdep_map dep_map; | 39 | struct lockdep_map dep_map; |
| 31 | #endif | 40 | #endif |
| @@ -55,11 +64,21 @@ static inline int rwsem_is_locked(struct rw_semaphore *sem) | |||
| 55 | # define __RWSEM_DEP_MAP_INIT(lockname) | 64 | # define __RWSEM_DEP_MAP_INIT(lockname) |
| 56 | #endif | 65 | #endif |
| 57 | 66 | ||
| 67 | #ifdef CONFIG_SMP | ||
| 68 | #define __RWSEM_INITIALIZER(name) \ | ||
| 69 | { RWSEM_UNLOCKED_VALUE, \ | ||
| 70 | __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \ | ||
| 71 | LIST_HEAD_INIT((name).wait_list), \ | ||
| 72 | NULL, /* owner */ \ | ||
| 73 | NULL /* mcs lock */ \ | ||
| 74 | __RWSEM_DEP_MAP_INIT(name) } | ||
| 75 | #else | ||
| 58 | #define __RWSEM_INITIALIZER(name) \ | 76 | #define __RWSEM_INITIALIZER(name) \ |
| 59 | { RWSEM_UNLOCKED_VALUE, \ | 77 | { RWSEM_UNLOCKED_VALUE, \ |
| 60 | __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \ | 78 | __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \ |
| 61 | LIST_HEAD_INIT((name).wait_list) \ | 79 | LIST_HEAD_INIT((name).wait_list) \ |
| 62 | __RWSEM_DEP_MAP_INIT(name) } | 80 | __RWSEM_DEP_MAP_INIT(name) } |
| 81 | #endif | ||
| 63 | 82 | ||
| 64 | #define DECLARE_RWSEM(name) \ | 83 | #define DECLARE_RWSEM(name) \ |
| 65 | struct rw_semaphore name = __RWSEM_INITIALIZER(name) | 84 | struct rw_semaphore name = __RWSEM_INITIALIZER(name) |
diff --git a/kernel/locking/rwsem-xadd.c b/kernel/locking/rwsem-xadd.c index b4219ff87b8c..4a75278142cd 100644 --- a/kernel/locking/rwsem-xadd.c +++ b/kernel/locking/rwsem-xadd.c | |||
| @@ -5,11 +5,17 @@ | |||
| 5 | * | 5 | * |
| 6 | * Writer lock-stealing by Alex Shi <alex.shi@intel.com> | 6 | * Writer lock-stealing by Alex Shi <alex.shi@intel.com> |
| 7 | * and Michel Lespinasse <walken@google.com> | 7 | * and Michel Lespinasse <walken@google.com> |
| 8 | * | ||
| 9 | * Optimistic spinning by Tim Chen <tim.c.chen@intel.com> | ||
| 10 | * and Davidlohr Bueso <davidlohr@hp.com>. Based on mutexes. | ||
| 8 | */ | 11 | */ |
| 9 | #include <linux/rwsem.h> | 12 | #include <linux/rwsem.h> |
| 10 | #include <linux/sched.h> | 13 | #include <linux/sched.h> |
| 11 | #include <linux/init.h> | 14 | #include <linux/init.h> |
| 12 | #include <linux/export.h> | 15 | #include <linux/export.h> |
| 16 | #include <linux/sched/rt.h> | ||
| 17 | |||
| 18 | #include "mcs_spinlock.h" | ||
| 13 | 19 | ||
| 14 | /* | 20 | /* |
| 15 | * Guide to the rw_semaphore's count field for common values. | 21 | * Guide to the rw_semaphore's count field for common values. |
| @@ -76,6 +82,10 @@ void __init_rwsem(struct rw_semaphore *sem, const char *name, | |||
| 76 | sem->count = RWSEM_UNLOCKED_VALUE; | 82 | sem->count = RWSEM_UNLOCKED_VALUE; |
| 77 | raw_spin_lock_init(&sem->wait_lock); | 83 | raw_spin_lock_init(&sem->wait_lock); |
| 78 | INIT_LIST_HEAD(&sem->wait_list); | 84 | INIT_LIST_HEAD(&sem->wait_list); |
| 85 | #ifdef CONFIG_SMP | ||
| 86 | sem->owner = NULL; | ||
| 87 | sem->osq = NULL; | ||
| 88 | #endif | ||
| 79 | } | 89 | } |
| 80 | 90 | ||
| 81 | EXPORT_SYMBOL(__init_rwsem); | 91 | EXPORT_SYMBOL(__init_rwsem); |
| @@ -190,7 +200,7 @@ __rwsem_do_wake(struct rw_semaphore *sem, enum rwsem_wake_type wake_type) | |||
| 190 | } | 200 | } |
| 191 | 201 | ||
| 192 | /* | 202 | /* |
| 193 | * wait for the read lock to be granted | 203 | * Wait for the read lock to be granted |
| 194 | */ | 204 | */ |
| 195 | __visible | 205 | __visible |
| 196 | struct rw_semaphore __sched *rwsem_down_read_failed(struct rw_semaphore *sem) | 206 | struct rw_semaphore __sched *rwsem_down_read_failed(struct rw_semaphore *sem) |
| @@ -237,64 +247,221 @@ struct rw_semaphore __sched *rwsem_down_read_failed(struct rw_semaphore *sem) | |||
| 237 | return sem; | 247 | return sem; |
| 238 | } | 248 | } |
| 239 | 249 | ||
| 250 | static inline bool rwsem_try_write_lock(long count, struct rw_semaphore *sem) | ||
| 251 | { | ||
| 252 | if (!(count & RWSEM_ACTIVE_MASK)) { | ||
| 253 | /* try acquiring the write lock */ | ||
| 254 | if (sem->count == RWSEM_WAITING_BIAS && | ||
| 255 | cmpxchg(&sem->count, RWSEM_WAITING_BIAS, | ||
| 256 | RWSEM_ACTIVE_WRITE_BIAS) == RWSEM_WAITING_BIAS) { | ||
| 257 | if (!list_is_singular(&sem->wait_list)) | ||
| 258 | rwsem_atomic_update(RWSEM_WAITING_BIAS, sem); | ||
| 259 | return true; | ||
| 260 | } | ||
| 261 | } | ||
| 262 | return false; | ||
| 263 | } | ||
| 264 | |||
| 265 | #ifdef CONFIG_SMP | ||
| 240 | /* | 266 | /* |
| 241 | * wait until we successfully acquire the write lock | 267 | * Try to acquire write lock before the writer has been put on wait queue. |
| 268 | */ | ||
| 269 | static inline bool rwsem_try_write_lock_unqueued(struct rw_semaphore *sem) | ||
| 270 | { | ||
| 271 | long old, count = ACCESS_ONCE(sem->count); | ||
| 272 | |||
| 273 | while (true) { | ||
| 274 | if (!(count == 0 || count == RWSEM_WAITING_BIAS)) | ||
| 275 | return false; | ||
| 276 | |||
| 277 | old = cmpxchg(&sem->count, count, count + RWSEM_ACTIVE_WRITE_BIAS); | ||
| 278 | if (old == count) | ||
| 279 | return true; | ||
| 280 | |||
| 281 | count = old; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 | static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem) | ||
| 286 | { | ||
| 287 | struct task_struct *owner; | ||
| 288 | bool on_cpu = true; | ||
| 289 | |||
| 290 | if (need_resched()) | ||
| 291 | return 0; | ||
| 292 | |||
| 293 | rcu_read_lock(); | ||
| 294 | owner = ACCESS_ONCE(sem->owner); | ||
| 295 | if (owner) | ||
| 296 | on_cpu = owner->on_cpu; | ||
| 297 | rcu_read_unlock(); | ||
| 298 | |||
| 299 | /* | ||
| 300 | * If sem->owner is not set, the rwsem owner may have | ||
| 301 | * just acquired it and not set the owner yet or the rwsem | ||
| 302 | * has been released. | ||
| 303 | */ | ||
| 304 | return on_cpu; | ||
| 305 | } | ||
| 306 | |||
| 307 | static inline bool owner_running(struct rw_semaphore *sem, | ||
| 308 | struct task_struct *owner) | ||
| 309 | { | ||
| 310 | if (sem->owner != owner) | ||
| 311 | return false; | ||
| 312 | |||
| 313 | /* | ||
| 314 | * Ensure we emit the owner->on_cpu, dereference _after_ checking | ||
| 315 | * sem->owner still matches owner, if that fails, owner might | ||
| 316 | * point to free()d memory, if it still matches, the rcu_read_lock() | ||
| 317 | * ensures the memory stays valid. | ||
| 318 | */ | ||
| 319 | barrier(); | ||
| 320 | |||
| 321 | return owner->on_cpu; | ||
| 322 | } | ||
| 323 | |||
| 324 | static noinline | ||
| 325 | bool rwsem_spin_on_owner(struct rw_semaphore *sem, struct task_struct *owner) | ||
| 326 | { | ||
| 327 | rcu_read_lock(); | ||
| 328 | while (owner_running(sem, owner)) { | ||
| 329 | if (need_resched()) | ||
| 330 | break; | ||
| 331 | |||
| 332 | arch_mutex_cpu_relax(); | ||
| 333 | } | ||
