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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | #include <linux/rwsem.h> |
| 9 | #include <linux/sched.h> |
| 10 | #include <linux/init.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 11 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 13 | /* |
| 14 | * Initialize an rwsem: |
| 15 | */ |
| 16 | void __init_rwsem(struct rw_semaphore *sem, const char *name, |
| 17 | struct lock_class_key *key) |
| 18 | { |
| 19 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 20 | /* |
| 21 | * Make sure we are not reinitializing a held semaphore: |
| 22 | */ |
| 23 | debug_check_no_locks_freed((void *)sem, sizeof(*sem)); |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 24 | lockdep_init_map(&sem->dep_map, name, key, 0); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 25 | #endif |
| 26 | sem->count = RWSEM_UNLOCKED_VALUE; |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 27 | raw_spin_lock_init(&sem->wait_lock); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 28 | INIT_LIST_HEAD(&sem->wait_list); |
| 29 | } |
| 30 | |
| 31 | EXPORT_SYMBOL(__init_rwsem); |
| 32 | |
Michel Lespinasse | e2d57f7 | 2013-05-07 06:45:49 -0700 | [diff] [blame] | 33 | enum rwsem_waiter_type { |
| 34 | RWSEM_WAITING_FOR_WRITE, |
| 35 | RWSEM_WAITING_FOR_READ |
| 36 | }; |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | struct rwsem_waiter { |
| 39 | struct list_head list; |
| 40 | struct task_struct *task; |
Michel Lespinasse | e2d57f7 | 2013-05-07 06:45:49 -0700 | [diff] [blame] | 41 | enum rwsem_waiter_type type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 44 | /* Wake types for __rwsem_do_wake(). Note that RWSEM_WAKE_NO_ACTIVE and |
| 45 | * RWSEM_WAKE_READ_OWNED imply that the spinlock must have been kept held |
| 46 | * since the rwsem value was observed. |
| 47 | */ |
| 48 | #define RWSEM_WAKE_ANY 0 /* Wake whatever's at head of wait list */ |
| 49 | #define RWSEM_WAKE_NO_ACTIVE 1 /* rwsem was observed with no active thread */ |
| 50 | #define RWSEM_WAKE_READ_OWNED 2 /* rwsem was observed to be read owned */ |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | /* |
| 53 | * handle the lock release when processes blocked on it that can now run |
| 54 | * - if we come here from up_xxxx(), then: |
| 55 | * - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed) |
| 56 | * - 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] | 57 | * - there must be someone on the queue |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | * - the spinlock must be held by the caller |
| 59 | * - woken process blocks are discarded from the list after having task zeroed |
| 60 | * - writers are only woken if downgrading is false |
| 61 | */ |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 62 | static struct rw_semaphore * |
| 63 | __rwsem_do_wake(struct rw_semaphore *sem, int wake_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
| 65 | struct rwsem_waiter *waiter; |
| 66 | struct task_struct *tsk; |
| 67 | struct list_head *next; |
Alex Shi | ce6711f | 2013-02-05 21:11:55 +0800 | [diff] [blame] | 68 | signed long woken, loop, adjustment; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 70 | waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list); |
Michel Lespinasse | e2d57f7 | 2013-05-07 06:45:49 -0700 | [diff] [blame] | 71 | if (waiter->type != RWSEM_WAITING_FOR_WRITE) |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 72 | goto readers_only; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 74 | if (wake_type == RWSEM_WAKE_READ_OWNED) |
Michel Lespinasse | 424acaa | 2010-08-09 17:21:19 -0700 | [diff] [blame] | 75 | /* Another active reader was observed, so wakeup is not |
| 76 | * likely to succeed. Save the atomic op. |
| 77 | */ |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 78 | goto out; |
| 79 | |
Alex Shi | ce6711f | 2013-02-05 21:11:55 +0800 | [diff] [blame] | 80 | /* Wake up the writing waiter and let the task grab the sem: */ |
| 81 | wake_up_process(waiter->task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | goto out; |
| 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | readers_only: |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 85 | /* If we come here from up_xxxx(), another thread might have reached |
| 86 | * rwsem_down_failed_common() before we acquired the spinlock and |
| 87 | * woken up a waiter, making it now active. We prefer to check for |
| 88 | * this first in order to not spend too much time with the spinlock |
| 89 | * held if we're not going to be able to wake up readers in the end. |
| 90 | * |
| 91 | * Note that we do not need to update the rwsem count: any writer |
| 92 | * trying to acquire rwsem will run rwsem_down_write_failed() due |
| 93 | * to the waiting threads and block trying to acquire the spinlock. |
| 94 | * |
| 95 | * We use a dummy atomic update in order to acquire the cache line |
| 96 | * exclusively since we expect to succeed and run the final rwsem |
| 97 | * count adjustment pretty soon. |
| 98 | */ |
| 99 | if (wake_type == RWSEM_WAKE_ANY && |
Michel Lespinasse | 424acaa | 2010-08-09 17:21:19 -0700 | [diff] [blame] | 100 | rwsem_atomic_update(0, sem) < RWSEM_WAITING_BIAS) |
| 101 | /* Someone grabbed the sem for write already */ |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 102 | goto out; |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 103 | |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 104 | /* Grant an infinite number of read locks to the readers at the front |
| 105 | * of the queue. Note we increment the 'active part' of the count by |
| 106 | * the number of readers before waking any processes up. |
| 107 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | woken = 0; |
| 109 | do { |
| 110 | woken++; |
| 111 | |
| 112 | if (waiter->list.next == &sem->wait_list) |
| 113 | break; |
| 114 | |
| 115 | waiter = list_entry(waiter->list.next, |
| 116 | struct rwsem_waiter, list); |
| 117 | |
Michel Lespinasse | e2d57f7 | 2013-05-07 06:45:49 -0700 | [diff] [blame] | 118 | } while (waiter->type != RWSEM_WAITING_FOR_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Michel Lespinasse | fd41b33 | 2010-08-09 17:21:18 -0700 | [diff] [blame] | 120 | adjustment = woken * RWSEM_ACTIVE_READ_BIAS; |
Michel Lespinasse | e2d57f7 | 2013-05-07 06:45:49 -0700 | [diff] [blame] | 121 | if (waiter->type != RWSEM_WAITING_FOR_WRITE) |
Michel Lespinasse | fd41b33 | 2010-08-09 17:21:18 -0700 | [diff] [blame] | 122 | /* hit end of list above */ |
| 123 | adjustment -= RWSEM_WAITING_BIAS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Michel Lespinasse | fd41b33 | 2010-08-09 17:21:18 -0700 | [diff] [blame] | 125 | rwsem_atomic_add(adjustment, sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | next = sem->wait_list.next; |
Michel Lespinasse | fd41b33 | 2010-08-09 17:21:18 -0700 | [diff] [blame] | 128 | for (loop = woken; loop > 0; loop--) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | waiter = list_entry(next, struct rwsem_waiter, list); |
| 130 | next = waiter->list.next; |
| 131 | tsk = waiter->task; |
akpm@osdl.org | d59dd46 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 132 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | waiter->task = NULL; |
| 134 | wake_up_process(tsk); |
| 135 | put_task_struct(tsk); |
| 136 | } |
| 137 | |
| 138 | sem->wait_list.next = next; |
| 139 | next->prev = &sem->wait_list; |
| 140 | |
| 141 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | return sem; |
Alex Shi | ce6711f | 2013-02-05 21:11:55 +0800 | [diff] [blame] | 143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Alex Shi | ce6711f | 2013-02-05 21:11:55 +0800 | [diff] [blame] | 145 | /* Try to get write sem, caller holds sem->wait_lock: */ |
| 146 | static int try_get_writer_sem(struct rw_semaphore *sem, |
| 147 | struct rwsem_waiter *waiter) |
| 148 | { |
| 149 | struct rwsem_waiter *fwaiter; |
| 150 | long oldcount, adjustment; |
| 151 | |
| 152 | /* only steal when first waiter is writing */ |
| 153 | fwaiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list); |
Michel Lespinasse | e2d57f7 | 2013-05-07 06:45:49 -0700 | [diff] [blame] | 154 | if (fwaiter->type != RWSEM_WAITING_FOR_WRITE) |
Alex Shi | ce6711f | 2013-02-05 21:11:55 +0800 | [diff] [blame] | 155 | return 0; |
| 156 | |
| 157 | adjustment = RWSEM_ACTIVE_WRITE_BIAS; |
| 158 | /* Only one waiter in the queue: */ |
| 159 | if (fwaiter == waiter && waiter->list.next == &sem->wait_list) |
| 160 | adjustment -= RWSEM_WAITING_BIAS; |
| 161 | |
| 162 | try_again_write: |
| 163 | oldcount = rwsem_atomic_update(adjustment, sem) - adjustment; |
| 164 | if (!(oldcount & RWSEM_ACTIVE_MASK)) { |
| 165 | /* No active lock: */ |
| 166 | struct task_struct *tsk = waiter->task; |
| 167 | |
| 168 | list_del(&waiter->list); |
| 169 | smp_mb(); |
| 170 | put_task_struct(tsk); |
| 171 | tsk->state = TASK_RUNNING; |
| 172 | return 1; |
| 173 | } |
| 174 | /* some one grabbed the sem already */ |
Michel Lespinasse | fd41b33 | 2010-08-09 17:21:18 -0700 | [diff] [blame] | 175 | if (rwsem_atomic_update(-adjustment, sem) & RWSEM_ACTIVE_MASK) |
Alex Shi | ce6711f | 2013-02-05 21:11:55 +0800 | [diff] [blame] | 176 | return 0; |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 177 | goto try_again_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 181 | * wait for the read lock to be granted |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | */ |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 183 | struct rw_semaphore __sched *rwsem_down_read_failed(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 185 | signed long adjustment = -RWSEM_ACTIVE_READ_BIAS; |
Michel Lespinasse | a8618a0 | 2010-08-09 17:21:20 -0700 | [diff] [blame] | 186 | struct rwsem_waiter waiter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | struct task_struct *tsk = current; |
| 188 | signed long count; |
| 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | /* set up my own style of waitqueue */ |
Michel Lespinasse | a8618a0 | 2010-08-09 17:21:20 -0700 | [diff] [blame] | 191 | waiter.task = tsk; |
Michel Lespinasse | da16922 | 2013-05-07 06:45:52 -0700 | [diff] [blame^] | 192 | waiter.type = RWSEM_WAITING_FOR_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | get_task_struct(tsk); |
| 194 | |
Michel Lespinasse | f7dd1ce | 2013-05-07 06:45:50 -0700 | [diff] [blame] | 195 | raw_spin_lock_irq(&sem->wait_lock); |
Michel Lespinasse | fd41b33 | 2010-08-09 17:21:18 -0700 | [diff] [blame] | 196 | if (list_empty(&sem->wait_list)) |
| 197 | adjustment += RWSEM_WAITING_BIAS; |
Michel Lespinasse | a8618a0 | 2010-08-09 17:21:20 -0700 | [diff] [blame] | 198 | list_add_tail(&waiter.list, &sem->wait_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 200 | /* we're now waiting on the lock, but no longer actively locking */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | count = rwsem_atomic_update(adjustment, sem); |
| 202 | |
Michel Lespinasse | da16922 | 2013-05-07 06:45:52 -0700 | [diff] [blame^] | 203 | /* If there are no active locks, wake the front queued process(es). */ |
Michel Lespinasse | 424acaa | 2010-08-09 17:21:19 -0700 | [diff] [blame] | 204 | if (count == RWSEM_WAITING_BIAS) |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 205 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_NO_ACTIVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 207 | raw_spin_unlock_irq(&sem->wait_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | /* wait to be given the lock */ |
Michel Lespinasse | f7dd1ce | 2013-05-07 06:45:50 -0700 | [diff] [blame] | 210 | while (true) { |
| 211 | set_task_state(tsk, TASK_UNINTERRUPTIBLE); |
Michel Lespinasse | a8618a0 | 2010-08-09 17:21:20 -0700 | [diff] [blame] | 212 | if (!waiter.task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | break; |
| 214 | schedule(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | tsk->state = TASK_RUNNING; |
| 218 | |
| 219 | return sem; |
| 220 | } |
| 221 | |
| 222 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | * wait for the write lock to be granted |
| 224 | */ |
Thomas Gleixner | d123375 | 2011-01-26 21:32:01 +0100 | [diff] [blame] | 225 | struct rw_semaphore __sched *rwsem_down_write_failed(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
Michel Lespinasse | 1e78277 | 2013-05-07 06:45:51 -0700 | [diff] [blame] | 227 | enum rwsem_waiter_type type = RWSEM_WAITING_FOR_WRITE; |
| 228 | signed long adjustment = -RWSEM_ACTIVE_WRITE_BIAS; |
| 229 | struct rwsem_waiter waiter; |
| 230 | struct task_struct *tsk = current; |
| 231 | signed long count; |
| 232 | |
| 233 | /* set up my own style of waitqueue */ |
| 234 | waiter.task = tsk; |
| 235 | waiter.type = type; |
| 236 | get_task_struct(tsk); |
| 237 | |
| 238 | raw_spin_lock_irq(&sem->wait_lock); |
| 239 | if (list_empty(&sem->wait_list)) |
| 240 | adjustment += RWSEM_WAITING_BIAS; |
| 241 | list_add_tail(&waiter.list, &sem->wait_list); |
| 242 | |
| 243 | /* we're now waiting on the lock, but no longer actively locking */ |
| 244 | count = rwsem_atomic_update(adjustment, sem); |
| 245 | |
| 246 | /* If there are no active locks, wake the front queued process(es) up. |
| 247 | * |
| 248 | * Alternatively, if we're called from a failed down_write(), there |
| 249 | * were already threads queued before us and there are no active |
| 250 | * writers, the lock must be read owned; so we try to wake any read |
| 251 | * locks that were queued ahead of us. */ |
| 252 | if (count == RWSEM_WAITING_BIAS) |
| 253 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_NO_ACTIVE); |
| 254 | else if (count > RWSEM_WAITING_BIAS && |
| 255 | adjustment == -RWSEM_ACTIVE_WRITE_BIAS) |
| 256 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED); |
| 257 | |
| 258 | raw_spin_unlock_irq(&sem->wait_lock); |
| 259 | |
| 260 | /* wait to be given the lock */ |
| 261 | while (true) { |
| 262 | set_task_state(tsk, TASK_UNINTERRUPTIBLE); |
| 263 | if (!waiter.task) |
| 264 | break; |
| 265 | |
| 266 | raw_spin_lock_irq(&sem->wait_lock); |
| 267 | /* Try to get the writer sem, may steal from the head writer: */ |
| 268 | if (type == RWSEM_WAITING_FOR_WRITE) |
| 269 | if (try_get_writer_sem(sem, &waiter)) { |
| 270 | raw_spin_unlock_irq(&sem->wait_lock); |
| 271 | return sem; |
| 272 | } |
| 273 | raw_spin_unlock_irq(&sem->wait_lock); |
| 274 | schedule(); |
| 275 | } |
| 276 | |
| 277 | tsk->state = TASK_RUNNING; |
| 278 | |
| 279 | return sem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /* |
| 283 | * handle waking up a waiter on the semaphore |
| 284 | * - up_read/up_write has decremented the active part of count if we come here |
| 285 | */ |
Thomas Gleixner | d123375 | 2011-01-26 21:32:01 +0100 | [diff] [blame] | 286 | struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | { |
| 288 | unsigned long flags; |
| 289 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 290 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | /* do nothing if list empty */ |
| 293 | if (!list_empty(&sem->wait_list)) |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 294 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 296 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | return sem; |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * downgrade a write lock into a read lock |
| 303 | * - caller incremented waiting part of count and discovered it still negative |
| 304 | * - just wake up any readers at the front of the queue |
| 305 | */ |
Thomas Gleixner | d123375 | 2011-01-26 21:32:01 +0100 | [diff] [blame] | 306 | struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | { |
| 308 | unsigned long flags; |
| 309 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 310 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | /* do nothing if list empty */ |
| 313 | if (!list_empty(&sem->wait_list)) |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame] | 314 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 316 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | return sem; |
| 319 | } |
| 320 | |
| 321 | EXPORT_SYMBOL(rwsem_down_read_failed); |
| 322 | EXPORT_SYMBOL(rwsem_down_write_failed); |
| 323 | EXPORT_SYMBOL(rwsem_wake); |
| 324 | EXPORT_SYMBOL(rwsem_downgrade_wake); |