Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* rwsem.c: R/W semaphores: contention handling functions |
| 2 | * |
| 3 | * Written by David Howells (dhowells@redhat.com). |
| 4 | * Derived from arch/i386/kernel/semaphore.c |
Alex Shi | ce6711f | 2013-02-05 21:11:55 +0800 | [diff] [blame] | 5 | * |
| 6 | * Writer lock-stealing by Alex Shi <alex.shi@intel.com> |
Michel Lespinasse | fe6e674 | 2013-05-07 06:45:59 -0700 | [diff] [blame] | 7 | * and Michel Lespinasse <walken@google.com> |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 8 | * |
| 9 | * Optimistic spinning by Tim Chen <tim.c.chen@intel.com> |
| 10 | * and Davidlohr Bueso <davidlohr@hp.com>. Based on mutexes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
| 12 | #include <linux/rwsem.h> |
| 13 | #include <linux/sched.h> |
| 14 | #include <linux/init.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 15 | #include <linux/export.h> |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 16 | #include <linux/sched/rt.h> |
| 17 | |
| 18 | #include "mcs_spinlock.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 20 | /* |
Tim Chen | 3cf2f34 | 2014-05-02 12:53:57 -0700 | [diff] [blame] | 21 | * Guide to the rw_semaphore's count field for common values. |
| 22 | * (32-bit case illustrated, similar for 64-bit) |
| 23 | * |
| 24 | * 0x0000000X (1) X readers active or attempting lock, no writer waiting |
| 25 | * X = #active_readers + #readers attempting to lock |
| 26 | * (X*ACTIVE_BIAS) |
| 27 | * |
| 28 | * 0x00000000 rwsem is unlocked, and no one is waiting for the lock or |
| 29 | * attempting to read lock or write lock. |
| 30 | * |
| 31 | * 0xffff000X (1) X readers active or attempting lock, with waiters for lock |
| 32 | * X = #active readers + # readers attempting lock |
| 33 | * (X*ACTIVE_BIAS + WAITING_BIAS) |
| 34 | * (2) 1 writer attempting lock, no waiters for lock |
| 35 | * X-1 = #active readers + #readers attempting lock |
| 36 | * ((X-1)*ACTIVE_BIAS + ACTIVE_WRITE_BIAS) |
| 37 | * (3) 1 writer active, no waiters for lock |
| 38 | * X-1 = #active readers + #readers attempting lock |
| 39 | * ((X-1)*ACTIVE_BIAS + ACTIVE_WRITE_BIAS) |
| 40 | * |
| 41 | * 0xffff0001 (1) 1 reader active or attempting lock, waiters for lock |
| 42 | * (WAITING_BIAS + ACTIVE_BIAS) |
| 43 | * (2) 1 writer active or attempting lock, no waiters for lock |
| 44 | * (ACTIVE_WRITE_BIAS) |
| 45 | * |
| 46 | * 0xffff0000 (1) There are writers or readers queued but none active |
| 47 | * or in the process of attempting lock. |
| 48 | * (WAITING_BIAS) |
| 49 | * Note: writer can attempt to steal lock for this count by adding |
| 50 | * ACTIVE_WRITE_BIAS in cmpxchg and checking the old count |
| 51 | * |
| 52 | * 0xfffe0001 (1) 1 writer active, or attempting lock. Waiters on queue. |
| 53 | * (ACTIVE_WRITE_BIAS + WAITING_BIAS) |
| 54 | * |
| 55 | * Note: Readers attempt to lock by adding ACTIVE_BIAS in down_read and checking |
| 56 | * the count becomes more than 0 for successful lock acquisition, |
| 57 | * i.e. the case where there are only readers or nobody has lock. |
| 58 | * (1st and 2nd case above). |
| 59 | * |
| 60 | * Writers attempt to lock by adding ACTIVE_WRITE_BIAS in down_write and |
| 61 | * checking the count becomes ACTIVE_WRITE_BIAS for successful lock |
| 62 | * acquisition (i.e. nobody else has lock or attempts lock). If |
| 63 | * unsuccessful, in rwsem_down_write_failed, we'll check to see if there |
| 64 | * are only waiters but none active (5th case above), and attempt to |
| 65 | * steal the lock. |
| 66 | * |
| 67 | */ |
| 68 | |
| 69 | /* |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 70 | * Initialize an rwsem: |
| 71 | */ |
| 72 | void __init_rwsem(struct rw_semaphore *sem, const char *name, |
| 73 | struct lock_class_key *key) |
| 74 | { |
| 75 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 76 | /* |
| 77 | * Make sure we are not reinitializing a held semaphore: |
| 78 | */ |
| 79 | debug_check_no_locks_freed((void *)sem, sizeof(*sem)); |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 80 | lockdep_init_map(&sem->dep_map, name, key, 0); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 81 | #endif |
| 82 | sem->count = RWSEM_UNLOCKED_VALUE; |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 83 | raw_spin_lock_init(&sem->wait_lock); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 84 | INIT_LIST_HEAD(&sem->wait_list); |
Davidlohr Bueso | 5db6c6f | 2014-07-11 14:00:06 -0700 | [diff] [blame] | 85 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 86 | sem->owner = NULL; |
Jason Low | 4d9d951 | 2014-07-14 10:27:50 -0700 | [diff] [blame] | 87 | osq_lock_init(&sem->osq); |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 88 | #endif |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | EXPORT_SYMBOL(__init_rwsem); |
| 92 | |
Michel Lespinasse | e2d57f7 | 2013-05-07 06:45:49 -0700 | [diff] [blame] | 93 | enum rwsem_waiter_type { |
| 94 | RWSEM_WAITING_FOR_WRITE, |
| 95 | RWSEM_WAITING_FOR_READ |
| 96 | }; |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | struct rwsem_waiter { |
| 99 | struct list_head list; |
| 100 | struct task_struct *task; |
Michel Lespinasse | e2d57f7 | 2013-05-07 06:45:49 -0700 | [diff] [blame] | 101 | enum rwsem_waiter_type type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
Michel Lespinasse | fe6e674 | 2013-05-07 06:45:59 -0700 | [diff] [blame] | 104 | enum rwsem_wake_type { |
| 105 | RWSEM_WAKE_ANY, /* Wake whatever's at head of wait list */ |
| 106 | RWSEM_WAKE_READERS, /* Wake readers only */ |
| 107 | RWSEM_WAKE_READ_OWNED /* Waker thread holds the read lock */ |
| 108 | }; |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | /* |
| 111 | * handle the lock release when processes blocked on it that can now run |
| 112 | * - if we come here from up_xxxx(), then: |
| 113 | * - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed) |
| 114 | * - the 'waiting part' of count (&0xffff0000) is -ve (and will still be so) |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 115 | * - there must be someone on the queue |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | * - the spinlock must be held by the caller |
| 117 | * - woken process blocks are discarded from the list after having task zeroed |
| 118 | * - writers are only woken if downgrading is false |
| 119 | */ |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 120 | static struct rw_semaphore * |
Michel Lespinasse | fe6e674 | 2013-05-07 06:45:59 -0700 | [diff] [blame] | 121 | __rwsem_do_wake(struct rw_semaphore *sem, enum rwsem_wake_type wake_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
| 123 | struct rwsem_waiter *waiter; |
| 124 | struct task_struct *tsk; |
| 125 | struct list_head *next; |
Davidlohr Bueso | b5f5418 | 2013-05-07 06:46:02 -0700 | [diff] [blame] | 126 | long oldcount, woken, loop, adjustment; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 128 | waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list); |
Michel Lespinasse | 8cf5322 | 2013-05-07 06:45:58 -0700 | [diff] [blame] | 129 | if (waiter->type == RWSEM_WAITING_FOR_WRITE) { |
Michel Lespinasse | fe6e674 | 2013-05-07 06:45:59 -0700 | [diff] [blame] | 130 | if (wake_type == RWSEM_WAKE_ANY) |
Michel Lespinasse | 8cf5322 | 2013-05-07 06:45:58 -0700 | [diff] [blame] | 131 | /* Wake writer at the front of the queue, but do not |
| 132 | * grant it the lock yet as we want other writers |
| 133 | * to be able to steal it. Readers, on the other hand, |
| 134 | * will block as they will notice the queued writer. |
| 135 | */ |
| 136 | wake_up_process(waiter->task); |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 137 | goto out; |
Michel Lespinasse | 8cf5322 | 2013-05-07 06:45:58 -0700 | [diff] [blame] | 138 | } |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 139 | |
Michel Lespinasse | fe6e674 | 2013-05-07 06:45:59 -0700 | [diff] [blame] | 140 | /* Writers might steal the lock before we grant it to the next reader. |
| 141 | * We prefer to do the first reader grant before counting readers |
| 142 | * so we can bail out early if a writer stole the lock. |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 143 | */ |
Michel Lespinasse | fe6e674 | 2013-05-07 06:45:59 -0700 | [diff] [blame] | 144 | adjustment = 0; |
| 145 | if (wake_type != RWSEM_WAKE_READ_OWNED) { |
| 146 | adjustment = RWSEM_ACTIVE_READ_BIAS; |
| 147 | try_reader_grant: |
| 148 | oldcount = rwsem_atomic_update(adjustment, sem) - adjustment; |
| 149 | if (unlikely(oldcount < RWSEM_WAITING_BIAS)) { |
| 150 | /* A writer stole the lock. Undo our reader grant. */ |
| 151 | if (rwsem_atomic_update(-adjustment, sem) & |
| 152 | RWSEM_ACTIVE_MASK) |
| 153 | goto out; |
| 154 | /* Last active locker left. Retry waking readers. */ |
| 155 | goto try_reader_grant; |
| 156 | } |
| 157 | } |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 158 | |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 159 | /* Grant an infinite number of read locks to the readers at the front |
| 160 | * of the queue. Note we increment the 'active part' of the count by |
| 161 | * the number of readers before waking any processes up. |
| 162 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | woken = 0; |
| 164 | do { |
| 165 | woken++; |
| 166 | |
| 167 | if (waiter->list.next == &sem->wait_list) |
| 168 | break; |
| 169 | |
| 170 | waiter = list_entry(waiter->list.next, |
| 171 | struct rwsem_waiter, list); |
| 172 | |
Michel Lespinasse | e2d57f7 | 2013-05-07 06:45:49 -0700 | [diff] [blame] | 173 | } while (waiter->type != RWSEM_WAITING_FOR_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Michel Lespinasse | fe6e674 | 2013-05-07 06:45:59 -0700 | [diff] [blame] | 175 | adjustment = woken * RWSEM_ACTIVE_READ_BIAS - adjustment; |
Michel Lespinasse | e2d57f7 | 2013-05-07 06:45:49 -0700 | [diff] [blame] | 176 | if (waiter->type != RWSEM_WAITING_FOR_WRITE) |
Michel Lespinasse | fd41b33 | 2010-08-09 17:21:18 -0700 | [diff] [blame] | 177 | /* hit end of list above */ |
| 178 | adjustment -= RWSEM_WAITING_BIAS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Michel Lespinasse | fe6e674 | 2013-05-07 06:45:59 -0700 | [diff] [blame] | 180 | if (adjustment) |
| 181 | rwsem_atomic_add(adjustment, sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
| 183 | next = sem->wait_list.next; |
Michel Lespinasse | 8cf5322 | 2013-05-07 06:45:58 -0700 | [diff] [blame] | 184 | loop = woken; |
| 185 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | waiter = list_entry(next, struct rwsem_waiter, list); |
| 187 | next = waiter->list.next; |
| 188 | tsk = waiter->task; |
akpm@osdl.org | d59dd46 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 189 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | waiter->task = NULL; |
| 191 | wake_up_process(tsk); |
| 192 | put_task_struct(tsk); |
Michel Lespinasse | 8cf5322 | 2013-05-07 06:45:58 -0700 | [diff] [blame] | 193 | } while (--loop); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
| 195 | sem->wait_list.next = next; |
| 196 | next->prev = &sem->wait_list; |
| 197 | |
| 198 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | return sem; |
Alex Shi | ce6711f | 2013-02-05 21:11:55 +0800 | [diff] [blame] | 200 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | /* |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 203 | * Wait for the read lock to be granted |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | */ |
Andi Kleen | 3ebae4f | 2014-02-08 08:52:05 +0100 | [diff] [blame] | 205 | __visible |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 206 | struct rw_semaphore __sched *rwsem_down_read_failed(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
Davidlohr Bueso | b5f5418 | 2013-05-07 06:46:02 -0700 | [diff] [blame] | 208 | long count, adjustment = -RWSEM_ACTIVE_READ_BIAS; |
Michel Lespinasse | a8618a0 | 2010-08-09 17:21:20 -0700 | [diff] [blame] | 209 | struct rwsem_waiter waiter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | struct task_struct *tsk = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | /* set up my own style of waitqueue */ |
Michel Lespinasse | a8618a0 | 2010-08-09 17:21:20 -0700 | [diff] [blame] | 213 | waiter.task = tsk; |
Michel Lespinasse | da16922 | 2013-05-07 06:45:52 -0700 | [diff] [blame] | 214 | waiter.type = RWSEM_WAITING_FOR_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | get_task_struct(tsk); |
| 216 | |
Michel Lespinasse | f7dd1ce | 2013-05-07 06:45:50 -0700 | [diff] [blame] | 217 | raw_spin_lock_irq(&sem->wait_lock); |
Michel Lespinasse | fd41b33 | 2010-08-09 17:21:18 -0700 | [diff] [blame] | 218 | if (list_empty(&sem->wait_list)) |
| 219 | adjustment += RWSEM_WAITING_BIAS; |
Michel Lespinasse | a8618a0 | 2010-08-09 17:21:20 -0700 | [diff] [blame] | 220 | list_add_tail(&waiter.list, &sem->wait_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 222 | /* we're now waiting on the lock, but no longer actively locking */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | count = rwsem_atomic_update(adjustment, sem); |
| 224 | |
Michel Lespinasse | 25c3932 | 2013-05-07 06:46:00 -0700 | [diff] [blame] | 225 | /* If there are no active locks, wake the front queued process(es). |
| 226 | * |
| 227 | * If there are no writers and we are first in the queue, |
| 228 | * wake our own waiter to join the existing active readers ! |
| 229 | */ |
| 230 | if (count == RWSEM_WAITING_BIAS || |
| 231 | (count > RWSEM_WAITING_BIAS && |
| 232 | adjustment != -RWSEM_ACTIVE_READ_BIAS)) |
Michel Lespinasse | fe6e674 | 2013-05-07 06:45:59 -0700 | [diff] [blame] | 233 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 235 | raw_spin_unlock_irq(&sem->wait_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | /* wait to be given the lock */ |
Michel Lespinasse | f7dd1ce | 2013-05-07 06:45:50 -0700 | [diff] [blame] | 238 | while (true) { |
| 239 | set_task_state(tsk, TASK_UNINTERRUPTIBLE); |
Michel Lespinasse | a8618a0 | 2010-08-09 17:21:20 -0700 | [diff] [blame] | 240 | if (!waiter.task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | break; |
| 242 | schedule(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | tsk->state = TASK_RUNNING; |
| 246 | |
| 247 | return sem; |
| 248 | } |
Davidlohr Bueso | db0e716 | 2014-09-11 22:34:25 -0700 | [diff] [blame^] | 249 | EXPORT_SYMBOL(rwsem_down_read_failed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 251 | static inline bool rwsem_try_write_lock(long count, struct rw_semaphore *sem) |
| 252 | { |
| 253 | if (!(count & RWSEM_ACTIVE_MASK)) { |
| 254 | /* try acquiring the write lock */ |
| 255 | if (sem->count == RWSEM_WAITING_BIAS && |
| 256 | cmpxchg(&sem->count, RWSEM_WAITING_BIAS, |
| 257 | RWSEM_ACTIVE_WRITE_BIAS) == RWSEM_WAITING_BIAS) { |
| 258 | if (!list_is_singular(&sem->wait_list)) |
| 259 | rwsem_atomic_update(RWSEM_WAITING_BIAS, sem); |
| 260 | return true; |
| 261 | } |
| 262 | } |
| 263 | return false; |
| 264 | } |
| 265 | |
Davidlohr Bueso | 5db6c6f | 2014-07-11 14:00:06 -0700 | [diff] [blame] | 266 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | /* |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 268 | * Try to acquire write lock before the writer has been put on wait queue. |
| 269 | */ |
| 270 | static inline bool rwsem_try_write_lock_unqueued(struct rw_semaphore *sem) |
| 271 | { |
| 272 | long old, count = ACCESS_ONCE(sem->count); |
| 273 | |
| 274 | while (true) { |
| 275 | if (!(count == 0 || count == RWSEM_WAITING_BIAS)) |
| 276 | return false; |
| 277 | |
| 278 | old = cmpxchg(&sem->count, count, count + RWSEM_ACTIVE_WRITE_BIAS); |
| 279 | if (old == count) |
| 280 | return true; |
| 281 | |
| 282 | count = old; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem) |
| 287 | { |
| 288 | struct task_struct *owner; |
Jason Low | 37e9562 | 2014-07-04 20:49:32 -0700 | [diff] [blame] | 289 | bool on_cpu = false; |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 290 | |
| 291 | if (need_resched()) |
Jason Low | 37e9562 | 2014-07-04 20:49:32 -0700 | [diff] [blame] | 292 | return false; |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 293 | |
| 294 | rcu_read_lock(); |
| 295 | owner = ACCESS_ONCE(sem->owner); |
| 296 | if (owner) |
| 297 | on_cpu = owner->on_cpu; |
| 298 | rcu_read_unlock(); |
| 299 | |
| 300 | /* |
Jason Low | 37e9562 | 2014-07-04 20:49:32 -0700 | [diff] [blame] | 301 | * If sem->owner is not set, yet we have just recently entered the |
| 302 | * slowpath, then there is a possibility reader(s) may have the lock. |
| 303 | * To be safe, avoid spinning in these situations. |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 304 | */ |
| 305 | return on_cpu; |
| 306 | } |
| 307 | |
| 308 | static inline bool owner_running(struct rw_semaphore *sem, |
| 309 | struct task_struct *owner) |
| 310 | { |
| 311 | if (sem->owner != owner) |
| 312 | return false; |
| 313 | |
| 314 | /* |
| 315 | * Ensure we emit the owner->on_cpu, dereference _after_ checking |
| 316 | * sem->owner still matches owner, if that fails, owner might |
| 317 | * point to free()d memory, if it still matches, the rcu_read_lock() |
| 318 | * ensures the memory stays valid. |
| 319 | */ |
| 320 | barrier(); |
| 321 | |
| 322 | return owner->on_cpu; |
| 323 | } |
| 324 | |
| 325 | static noinline |
| 326 | bool rwsem_spin_on_owner(struct rw_semaphore *sem, struct task_struct *owner) |
| 327 | { |
| 328 | rcu_read_lock(); |
| 329 | while (owner_running(sem, owner)) { |
| 330 | if (need_resched()) |
| 331 | break; |
| 332 | |
Davidlohr Bueso | 3a6bfbc | 2014-06-29 15:09:33 -0700 | [diff] [blame] | 333 | cpu_relax_lowlatency(); |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 334 | } |
| 335 | rcu_read_unlock(); |
| 336 | |
| 337 | /* |
| 338 | * We break out the loop above on need_resched() or when the |
| 339 | * owner changed, which is a sign for heavy contention. Return |
| 340 | * success only when sem->owner is NULL. |
| 341 | */ |
| 342 | return sem->owner == NULL; |
| 343 | } |
| 344 | |
| 345 | static bool rwsem_optimistic_spin(struct rw_semaphore *sem) |
| 346 | { |
| 347 | struct task_struct *owner; |
| 348 | bool taken = false; |
| 349 | |
| 350 | preempt_disable(); |
| 351 | |
| 352 | /* sem->wait_lock should not be held when doing optimistic spinning */ |
| 353 | if (!rwsem_can_spin_on_owner(sem)) |
| 354 | goto done; |
| 355 | |
| 356 | if (!osq_lock(&sem->osq)) |
| 357 | goto done; |
| 358 | |
| 359 | while (true) { |
| 360 | owner = ACCESS_ONCE(sem->owner); |
| 361 | if (owner && !rwsem_spin_on_owner(sem, owner)) |
| 362 | break; |
| 363 | |
| 364 | /* wait_lock will be acquired if write_lock is obtained */ |
| 365 | if (rwsem_try_write_lock_unqueued(sem)) { |
| 366 | taken = true; |
| 367 | break; |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * When there's no owner, we might have preempted between the |
| 372 | * owner acquiring the lock and setting the owner field. If |
| 373 | * we're an RT task that will live-lock because we won't let |
| 374 | * the owner complete. |
| 375 | */ |
| 376 | if (!owner && (need_resched() || rt_task(current))) |
| 377 | break; |
| 378 | |
| 379 | /* |
| 380 | * The cpu_relax() call is a compiler barrier which forces |
| 381 | * everything in this loop to be re-loaded. We don't need |
| 382 | * memory barriers as we'll eventually observe the right |
| 383 | * values at the cost of a few extra spins. |
| 384 | */ |
Davidlohr Bueso | 3a6bfbc | 2014-06-29 15:09:33 -0700 | [diff] [blame] | 385 | cpu_relax_lowlatency(); |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 386 | } |
| 387 | osq_unlock(&sem->osq); |
| 388 | done: |
| 389 | preempt_enable(); |
| 390 | return taken; |
| 391 | } |
| 392 | |
| 393 | #else |
| 394 | static bool rwsem_optimistic_spin(struct rw_semaphore *sem) |
| 395 | { |
| 396 | return false; |
| 397 | } |
| 398 | #endif |
| 399 | |
| 400 | /* |
| 401 | * Wait until we successfully acquire the write lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | */ |
Andi Kleen | 3ebae4f | 2014-02-08 08:52:05 +0100 | [diff] [blame] | 403 | __visible |
Thomas Gleixner | d123375 | 2011-01-26 21:32:01 +0100 | [diff] [blame] | 404 | struct rw_semaphore __sched *rwsem_down_write_failed(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | { |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 406 | long count; |
| 407 | bool waiting = true; /* any queued threads before us */ |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 408 | struct rwsem_waiter waiter; |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 409 | |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 410 | /* undo write bias from down_write operation, stop active locking */ |
| 411 | count = rwsem_atomic_update(-RWSEM_ACTIVE_WRITE_BIAS, sem); |
| 412 | |
| 413 | /* do optimistic spinning and steal lock if possible */ |
| 414 | if (rwsem_optimistic_spin(sem)) |
| 415 | return sem; |
| 416 | |
| 417 | /* |
| 418 | * Optimistic spinning failed, proceed to the slowpath |
| 419 | * and block until we can acquire the sem. |
| 420 | */ |
| 421 | waiter.task = current; |
Michel Lespinasse | 023fe4f | 2013-05-07 06:45:53 -0700 | [diff] [blame] | 422 | waiter.type = RWSEM_WAITING_FOR_WRITE; |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 423 | |
| 424 | raw_spin_lock_irq(&sem->wait_lock); |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 425 | |
| 426 | /* account for this before adding a new element to the list */ |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 427 | if (list_empty(&sem->wait_list)) |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 428 | waiting = false; |
| 429 | |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 430 | list_add_tail(&waiter.list, &sem->wait_list); |
| 431 | |
| 432 | /* we're now waiting on the lock, but no longer actively locking */ |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 433 | if (waiting) { |
| 434 | count = ACCESS_ONCE(sem->count); |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 435 | |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 436 | /* |
Andrew Morton | 0cc3d01 | 2014-06-04 20:19:48 +0200 | [diff] [blame] | 437 | * If there were already threads queued before us and there are |
| 438 | * no active writers, the lock must be read owned; so we try to |
| 439 | * wake any read locks that were queued ahead of us. |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 440 | */ |
| 441 | if (count > RWSEM_WAITING_BIAS) |
| 442 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_READERS); |
| 443 | |
| 444 | } else |
| 445 | count = rwsem_atomic_update(RWSEM_WAITING_BIAS, sem); |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 446 | |
Michel Lespinasse | 023fe4f | 2013-05-07 06:45:53 -0700 | [diff] [blame] | 447 | /* wait until we successfully acquire the lock */ |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 448 | set_current_state(TASK_UNINTERRUPTIBLE); |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 449 | while (true) { |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 450 | if (rwsem_try_write_lock(count, sem)) |
| 451 | break; |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 452 | raw_spin_unlock_irq(&sem->wait_lock); |
Michel Lespinasse | a7d2c57 | 2013-05-07 06:45:56 -0700 | [diff] [blame] | 453 | |
| 454 | /* Block until there are no active lockers. */ |
| 455 | do { |
| 456 | schedule(); |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 457 | set_current_state(TASK_UNINTERRUPTIBLE); |
Michel Lespinasse | 9b0fc9c | 2013-05-07 06:45:57 -0700 | [diff] [blame] | 458 | } while ((count = sem->count) & RWSEM_ACTIVE_MASK); |
Michel Lespinasse | a7d2c57 | 2013-05-07 06:45:56 -0700 | [diff] [blame] | 459 | |
Michel Lespinasse | 023fe4f | 2013-05-07 06:45:53 -0700 | [diff] [blame] | 460 | raw_spin_lock_irq(&sem->wait_lock); |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 461 | } |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 462 | __set_current_state(TASK_RUNNING); |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 463 | |
Michel Lespinasse | 023fe4f | 2013-05-07 06:45:53 -0700 | [diff] [blame] | 464 | list_del(&waiter.list); |
| 465 | raw_spin_unlock_irq(&sem->wait_lock); |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 466 | |
| 467 | return sem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
Davidlohr Bueso | db0e716 | 2014-09-11 22:34:25 -0700 | [diff] [blame^] | 469 | EXPORT_SYMBOL(rwsem_down_write_failed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
| 471 | /* |
| 472 | * handle waking up a waiter on the semaphore |
| 473 | * - up_read/up_write has decremented the active part of count if we come here |
| 474 | */ |
Andi Kleen | 3ebae4f | 2014-02-08 08:52:05 +0100 | [diff] [blame] | 475 | __visible |
Thomas Gleixner | d123375 | 2011-01-26 21:32:01 +0100 | [diff] [blame] | 476 | struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | { |
| 478 | unsigned long flags; |
| 479 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 480 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
| 482 | /* do nothing if list empty */ |
| 483 | if (!list_empty(&sem->wait_list)) |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 484 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 486 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | return sem; |
| 489 | } |
Davidlohr Bueso | db0e716 | 2014-09-11 22:34:25 -0700 | [diff] [blame^] | 490 | EXPORT_SYMBOL(rwsem_wake); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | /* |
| 493 | * downgrade a write lock into a read lock |
| 494 | * - caller incremented waiting part of count and discovered it still negative |
| 495 | * - just wake up any readers at the front of the queue |
| 496 | */ |
Andi Kleen | 3ebae4f | 2014-02-08 08:52:05 +0100 | [diff] [blame] | 497 | __visible |
Thomas Gleixner | d123375 | 2011-01-26 21:32:01 +0100 | [diff] [blame] | 498 | struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | { |
| 500 | unsigned long flags; |
| 501 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 502 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
| 504 | /* do nothing if list empty */ |
| 505 | if (!list_empty(&sem->wait_list)) |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 506 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 508 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | return sem; |
| 511 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | EXPORT_SYMBOL(rwsem_downgrade_wake); |