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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | #include <linux/rwsem.h> |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/init.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 12 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 14 | /* |
Tim Chen | 3cf2f34 | 2014-05-02 12:53:57 -0700 | [diff] [blame^] | 15 | * Guide to the rw_semaphore's count field for common values. |
| 16 | * (32-bit case illustrated, similar for 64-bit) |
| 17 | * |
| 18 | * 0x0000000X (1) X readers active or attempting lock, no writer waiting |
| 19 | * X = #active_readers + #readers attempting to lock |
| 20 | * (X*ACTIVE_BIAS) |
| 21 | * |
| 22 | * 0x00000000 rwsem is unlocked, and no one is waiting for the lock or |
| 23 | * attempting to read lock or write lock. |
| 24 | * |
| 25 | * 0xffff000X (1) X readers active or attempting lock, with waiters for lock |
| 26 | * X = #active readers + # readers attempting lock |
| 27 | * (X*ACTIVE_BIAS + WAITING_BIAS) |
| 28 | * (2) 1 writer attempting lock, no waiters for lock |
| 29 | * X-1 = #active readers + #readers attempting lock |
| 30 | * ((X-1)*ACTIVE_BIAS + ACTIVE_WRITE_BIAS) |
| 31 | * (3) 1 writer active, no waiters for lock |
| 32 | * X-1 = #active readers + #readers attempting lock |
| 33 | * ((X-1)*ACTIVE_BIAS + ACTIVE_WRITE_BIAS) |
| 34 | * |
| 35 | * 0xffff0001 (1) 1 reader active or attempting lock, waiters for lock |
| 36 | * (WAITING_BIAS + ACTIVE_BIAS) |
| 37 | * (2) 1 writer active or attempting lock, no waiters for lock |
| 38 | * (ACTIVE_WRITE_BIAS) |
| 39 | * |
| 40 | * 0xffff0000 (1) There are writers or readers queued but none active |
| 41 | * or in the process of attempting lock. |
| 42 | * (WAITING_BIAS) |
| 43 | * Note: writer can attempt to steal lock for this count by adding |
| 44 | * ACTIVE_WRITE_BIAS in cmpxchg and checking the old count |
| 45 | * |
| 46 | * 0xfffe0001 (1) 1 writer active, or attempting lock. Waiters on queue. |
| 47 | * (ACTIVE_WRITE_BIAS + WAITING_BIAS) |
| 48 | * |
| 49 | * Note: Readers attempt to lock by adding ACTIVE_BIAS in down_read and checking |
| 50 | * the count becomes more than 0 for successful lock acquisition, |
| 51 | * i.e. the case where there are only readers or nobody has lock. |
| 52 | * (1st and 2nd case above). |
| 53 | * |
| 54 | * Writers attempt to lock by adding ACTIVE_WRITE_BIAS in down_write and |
| 55 | * checking the count becomes ACTIVE_WRITE_BIAS for successful lock |
| 56 | * acquisition (i.e. nobody else has lock or attempts lock). If |
| 57 | * unsuccessful, in rwsem_down_write_failed, we'll check to see if there |
| 58 | * are only waiters but none active (5th case above), and attempt to |
| 59 | * steal the lock. |
| 60 | * |
| 61 | */ |
| 62 | |
| 63 | /* |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 64 | * Initialize an rwsem: |
| 65 | */ |
| 66 | void __init_rwsem(struct rw_semaphore *sem, const char *name, |
| 67 | struct lock_class_key *key) |
| 68 | { |
| 69 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 70 | /* |
| 71 | * Make sure we are not reinitializing a held semaphore: |
| 72 | */ |
| 73 | debug_check_no_locks_freed((void *)sem, sizeof(*sem)); |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 74 | lockdep_init_map(&sem->dep_map, name, key, 0); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 75 | #endif |
| 76 | sem->count = RWSEM_UNLOCKED_VALUE; |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 77 | raw_spin_lock_init(&sem->wait_lock); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 78 | INIT_LIST_HEAD(&sem->wait_list); |
| 79 | } |
| 80 | |
| 81 | EXPORT_SYMBOL(__init_rwsem); |
| 82 | |
Michel Lespinasse | e2d57f7 | 2013-05-07 06:45:49 -0700 | [diff] [blame] | 83 | enum rwsem_waiter_type { |
| 84 | RWSEM_WAITING_FOR_WRITE, |
| 85 | RWSEM_WAITING_FOR_READ |
| 86 | }; |
| 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | struct rwsem_waiter { |
| 89 | struct list_head list; |
| 90 | struct task_struct *task; |
Michel Lespinasse | e2d57f7 | 2013-05-07 06:45:49 -0700 | [diff] [blame] | 91 | enum rwsem_waiter_type type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
Michel Lespinasse | fe6e674 | 2013-05-07 06:45:59 -0700 | [diff] [blame] | 94 | enum rwsem_wake_type { |
| 95 | RWSEM_WAKE_ANY, /* Wake whatever's at head of wait list */ |
| 96 | RWSEM_WAKE_READERS, /* Wake readers only */ |
| 97 | RWSEM_WAKE_READ_OWNED /* Waker thread holds the read lock */ |
| 98 | }; |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | /* |
| 101 | * handle the lock release when processes blocked on it that can now run |
| 102 | * - if we come here from up_xxxx(), then: |
| 103 | * - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed) |
| 104 | * - 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] | 105 | * - there must be someone on the queue |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | * - the spinlock must be held by the caller |
| 107 | * - woken process blocks are discarded from the list after having task zeroed |
| 108 | * - writers are only woken if downgrading is false |
| 109 | */ |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 110 | static struct rw_semaphore * |
Michel Lespinasse | fe6e674 | 2013-05-07 06:45:59 -0700 | [diff] [blame] | 111 | __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] | 112 | { |
| 113 | struct rwsem_waiter *waiter; |
| 114 | struct task_struct *tsk; |
| 115 | struct list_head *next; |
Davidlohr Bueso | b5f5418 | 2013-05-07 06:46:02 -0700 | [diff] [blame] | 116 | long oldcount, woken, loop, adjustment; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 118 | waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list); |
Michel Lespinasse | 8cf5322 | 2013-05-07 06:45:58 -0700 | [diff] [blame] | 119 | if (waiter->type == RWSEM_WAITING_FOR_WRITE) { |
Michel Lespinasse | fe6e674 | 2013-05-07 06:45:59 -0700 | [diff] [blame] | 120 | if (wake_type == RWSEM_WAKE_ANY) |
Michel Lespinasse | 8cf5322 | 2013-05-07 06:45:58 -0700 | [diff] [blame] | 121 | /* Wake writer at the front of the queue, but do not |
| 122 | * grant it the lock yet as we want other writers |
| 123 | * to be able to steal it. Readers, on the other hand, |
| 124 | * will block as they will notice the queued writer. |
| 125 | */ |
| 126 | wake_up_process(waiter->task); |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 127 | goto out; |
Michel Lespinasse | 8cf5322 | 2013-05-07 06:45:58 -0700 | [diff] [blame] | 128 | } |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 129 | |
Michel Lespinasse | fe6e674 | 2013-05-07 06:45:59 -0700 | [diff] [blame] | 130 | /* Writers might steal the lock before we grant it to the next reader. |
| 131 | * We prefer to do the first reader grant before counting readers |
| 132 | * so we can bail out early if a writer stole the lock. |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 133 | */ |
Michel Lespinasse | fe6e674 | 2013-05-07 06:45:59 -0700 | [diff] [blame] | 134 | adjustment = 0; |
| 135 | if (wake_type != RWSEM_WAKE_READ_OWNED) { |
| 136 | adjustment = RWSEM_ACTIVE_READ_BIAS; |
| 137 | try_reader_grant: |
| 138 | oldcount = rwsem_atomic_update(adjustment, sem) - adjustment; |
| 139 | if (unlikely(oldcount < RWSEM_WAITING_BIAS)) { |
| 140 | /* A writer stole the lock. Undo our reader grant. */ |
| 141 | if (rwsem_atomic_update(-adjustment, sem) & |
| 142 | RWSEM_ACTIVE_MASK) |
| 143 | goto out; |
| 144 | /* Last active locker left. Retry waking readers. */ |
| 145 | goto try_reader_grant; |
| 146 | } |
| 147 | } |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 148 | |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 149 | /* Grant an infinite number of read locks to the readers at the front |
| 150 | * of the queue. Note we increment the 'active part' of the count by |
| 151 | * the number of readers before waking any processes up. |
| 152 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | woken = 0; |
| 154 | do { |
| 155 | woken++; |
| 156 | |
| 157 | if (waiter->list.next == &sem->wait_list) |
| 158 | break; |
| 159 | |
| 160 | waiter = list_entry(waiter->list.next, |
| 161 | struct rwsem_waiter, list); |
| 162 | |
Michel Lespinasse | e2d57f7 | 2013-05-07 06:45:49 -0700 | [diff] [blame] | 163 | } while (waiter->type != RWSEM_WAITING_FOR_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Michel Lespinasse | fe6e674 | 2013-05-07 06:45:59 -0700 | [diff] [blame] | 165 | adjustment = woken * RWSEM_ACTIVE_READ_BIAS - adjustment; |
Michel Lespinasse | e2d57f7 | 2013-05-07 06:45:49 -0700 | [diff] [blame] | 166 | if (waiter->type != RWSEM_WAITING_FOR_WRITE) |
Michel Lespinasse | fd41b33 | 2010-08-09 17:21:18 -0700 | [diff] [blame] | 167 | /* hit end of list above */ |
| 168 | adjustment -= RWSEM_WAITING_BIAS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Michel Lespinasse | fe6e674 | 2013-05-07 06:45:59 -0700 | [diff] [blame] | 170 | if (adjustment) |
| 171 | rwsem_atomic_add(adjustment, sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | next = sem->wait_list.next; |
Michel Lespinasse | 8cf5322 | 2013-05-07 06:45:58 -0700 | [diff] [blame] | 174 | loop = woken; |
| 175 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | waiter = list_entry(next, struct rwsem_waiter, list); |
| 177 | next = waiter->list.next; |
| 178 | tsk = waiter->task; |
akpm@osdl.org | d59dd46 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 179 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | waiter->task = NULL; |
| 181 | wake_up_process(tsk); |
| 182 | put_task_struct(tsk); |
Michel Lespinasse | 8cf5322 | 2013-05-07 06:45:58 -0700 | [diff] [blame] | 183 | } while (--loop); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | sem->wait_list.next = next; |
| 186 | next->prev = &sem->wait_list; |
| 187 | |
| 188 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | return sem; |
Alex Shi | ce6711f | 2013-02-05 21:11:55 +0800 | [diff] [blame] | 190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | /* |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 193 | * wait for the read lock to be granted |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | */ |
Andi Kleen | 3ebae4f | 2014-02-08 08:52:05 +0100 | [diff] [blame] | 195 | __visible |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 196 | struct rw_semaphore __sched *rwsem_down_read_failed(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | { |
Davidlohr Bueso | b5f5418 | 2013-05-07 06:46:02 -0700 | [diff] [blame] | 198 | long count, adjustment = -RWSEM_ACTIVE_READ_BIAS; |
Michel Lespinasse | a8618a0 | 2010-08-09 17:21:20 -0700 | [diff] [blame] | 199 | struct rwsem_waiter waiter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | struct task_struct *tsk = current; |
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 | /* set up my own style of waitqueue */ |
Michel Lespinasse | a8618a0 | 2010-08-09 17:21:20 -0700 | [diff] [blame] | 203 | waiter.task = tsk; |
Michel Lespinasse | da16922 | 2013-05-07 06:45:52 -0700 | [diff] [blame] | 204 | waiter.type = RWSEM_WAITING_FOR_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | get_task_struct(tsk); |
| 206 | |
Michel Lespinasse | f7dd1ce | 2013-05-07 06:45:50 -0700 | [diff] [blame] | 207 | raw_spin_lock_irq(&sem->wait_lock); |
Michel Lespinasse | fd41b33 | 2010-08-09 17:21:18 -0700 | [diff] [blame] | 208 | if (list_empty(&sem->wait_list)) |
| 209 | adjustment += RWSEM_WAITING_BIAS; |
Michel Lespinasse | a8618a0 | 2010-08-09 17:21:20 -0700 | [diff] [blame] | 210 | list_add_tail(&waiter.list, &sem->wait_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 212 | /* we're now waiting on the lock, but no longer actively locking */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | count = rwsem_atomic_update(adjustment, sem); |
| 214 | |
Michel Lespinasse | 25c3932 | 2013-05-07 06:46:00 -0700 | [diff] [blame] | 215 | /* If there are no active locks, wake the front queued process(es). |
| 216 | * |
| 217 | * If there are no writers and we are first in the queue, |
| 218 | * wake our own waiter to join the existing active readers ! |
| 219 | */ |
| 220 | if (count == RWSEM_WAITING_BIAS || |
| 221 | (count > RWSEM_WAITING_BIAS && |
| 222 | adjustment != -RWSEM_ACTIVE_READ_BIAS)) |
Michel Lespinasse | fe6e674 | 2013-05-07 06:45:59 -0700 | [diff] [blame] | 223 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 225 | raw_spin_unlock_irq(&sem->wait_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | /* wait to be given the lock */ |
Michel Lespinasse | f7dd1ce | 2013-05-07 06:45:50 -0700 | [diff] [blame] | 228 | while (true) { |
| 229 | set_task_state(tsk, TASK_UNINTERRUPTIBLE); |
Michel Lespinasse | a8618a0 | 2010-08-09 17:21:20 -0700 | [diff] [blame] | 230 | if (!waiter.task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | break; |
| 232 | schedule(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | tsk->state = TASK_RUNNING; |
| 236 | |
| 237 | return sem; |
| 238 | } |
| 239 | |
| 240 | /* |
Michel Lespinasse | 023fe4f | 2013-05-07 06:45:53 -0700 | [diff] [blame] | 241 | * wait until we successfully acquire the write lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | */ |
Andi Kleen | 3ebae4f | 2014-02-08 08:52:05 +0100 | [diff] [blame] | 243 | __visible |
Thomas Gleixner | d123375 | 2011-01-26 21:32:01 +0100 | [diff] [blame] | 244 | struct rw_semaphore __sched *rwsem_down_write_failed(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
Davidlohr Bueso | b5f5418 | 2013-05-07 06:46:02 -0700 | [diff] [blame] | 246 | long count, adjustment = -RWSEM_ACTIVE_WRITE_BIAS; |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 247 | struct rwsem_waiter waiter; |
| 248 | struct task_struct *tsk = current; |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 249 | |
| 250 | /* set up my own style of waitqueue */ |
| 251 | waiter.task = tsk; |
Michel Lespinasse | 023fe4f | 2013-05-07 06:45:53 -0700 | [diff] [blame] | 252 | waiter.type = RWSEM_WAITING_FOR_WRITE; |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 253 | |
| 254 | raw_spin_lock_irq(&sem->wait_lock); |
| 255 | if (list_empty(&sem->wait_list)) |
| 256 | adjustment += RWSEM_WAITING_BIAS; |
| 257 | list_add_tail(&waiter.list, &sem->wait_list); |
| 258 | |
| 259 | /* we're now waiting on the lock, but no longer actively locking */ |
| 260 | count = rwsem_atomic_update(adjustment, sem); |
| 261 | |
Michel Lespinasse | ed00f64 | 2013-05-07 06:45:54 -0700 | [diff] [blame] | 262 | /* If there were already threads queued before us and there are no |
| 263 | * active writers, the lock must be read owned; so we try to wake |
| 264 | * any read locks that were queued ahead of us. */ |
| 265 | if (count > RWSEM_WAITING_BIAS && |
| 266 | adjustment == -RWSEM_ACTIVE_WRITE_BIAS) |
Michel Lespinasse | fe6e674 | 2013-05-07 06:45:59 -0700 | [diff] [blame] | 267 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_READERS); |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 268 | |
Michel Lespinasse | 023fe4f | 2013-05-07 06:45:53 -0700 | [diff] [blame] | 269 | /* wait until we successfully acquire the lock */ |
Michel Lespinasse | a7d2c57 | 2013-05-07 06:45:56 -0700 | [diff] [blame] | 270 | set_task_state(tsk, TASK_UNINTERRUPTIBLE); |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 271 | while (true) { |
Michel Lespinasse | 9b0fc9c | 2013-05-07 06:45:57 -0700 | [diff] [blame] | 272 | if (!(count & RWSEM_ACTIVE_MASK)) { |
| 273 | /* Try acquiring the write lock. */ |
| 274 | count = RWSEM_ACTIVE_WRITE_BIAS; |
| 275 | if (!list_is_singular(&sem->wait_list)) |
| 276 | count += RWSEM_WAITING_BIAS; |
Davidlohr Bueso | 9607a85 | 2013-05-07 15:39:03 -0700 | [diff] [blame] | 277 | |
| 278 | if (sem->count == RWSEM_WAITING_BIAS && |
| 279 | cmpxchg(&sem->count, RWSEM_WAITING_BIAS, count) == |
Michel Lespinasse | 5ede972 | 2013-05-07 06:45:55 -0700 | [diff] [blame] | 280 | RWSEM_WAITING_BIAS) |
Michel Lespinasse | 9b0fc9c | 2013-05-07 06:45:57 -0700 | [diff] [blame] | 281 | break; |
| 282 | } |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 283 | |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 284 | raw_spin_unlock_irq(&sem->wait_lock); |
Michel Lespinasse | a7d2c57 | 2013-05-07 06:45:56 -0700 | [diff] [blame] | 285 | |
| 286 | /* Block until there are no active lockers. */ |
| 287 | do { |
| 288 | schedule(); |
| 289 | set_task_state(tsk, TASK_UNINTERRUPTIBLE); |
Michel Lespinasse | 9b0fc9c | 2013-05-07 06:45:57 -0700 | [diff] [blame] | 290 | } while ((count = sem->count) & RWSEM_ACTIVE_MASK); |
Michel Lespinasse | a7d2c57 | 2013-05-07 06:45:56 -0700 | [diff] [blame] | 291 | |
Michel Lespinasse | 023fe4f | 2013-05-07 06:45:53 -0700 | [diff] [blame] | 292 | raw_spin_lock_irq(&sem->wait_lock); |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Michel Lespinasse | 023fe4f | 2013-05-07 06:45:53 -0700 | [diff] [blame] | 295 | list_del(&waiter.list); |
| 296 | raw_spin_unlock_irq(&sem->wait_lock); |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 297 | tsk->state = TASK_RUNNING; |
| 298 | |
| 299 | return sem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /* |
| 303 | * handle waking up a waiter on the semaphore |
| 304 | * - up_read/up_write has decremented the active part of count if we come here |
| 305 | */ |
Andi Kleen | 3ebae4f | 2014-02-08 08:52:05 +0100 | [diff] [blame] | 306 | __visible |
Thomas Gleixner | d123375 | 2011-01-26 21:32:01 +0100 | [diff] [blame] | 307 | struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | { |
| 309 | unsigned long flags; |
| 310 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 311 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
| 313 | /* do nothing if list empty */ |
| 314 | if (!list_empty(&sem->wait_list)) |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 315 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 317 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | return sem; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * downgrade a write lock into a read lock |
| 324 | * - caller incremented waiting part of count and discovered it still negative |
| 325 | * - just wake up any readers at the front of the queue |
| 326 | */ |
Andi Kleen | 3ebae4f | 2014-02-08 08:52:05 +0100 | [diff] [blame] | 327 | __visible |
Thomas Gleixner | d123375 | 2011-01-26 21:32:01 +0100 | [diff] [blame] | 328 | struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { |
| 330 | unsigned long flags; |
| 331 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 332 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
| 334 | /* do nothing if list empty */ |
| 335 | if (!list_empty(&sem->wait_list)) |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 336 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 338 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | return sem; |
| 341 | } |
| 342 | |
| 343 | EXPORT_SYMBOL(rwsem_down_read_failed); |
| 344 | EXPORT_SYMBOL(rwsem_down_write_failed); |
| 345 | EXPORT_SYMBOL(rwsem_wake); |
| 346 | EXPORT_SYMBOL(rwsem_downgrade_wake); |