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 |
| 5 | */ |
| 6 | #include <linux/rwsem.h> |
| 7 | #include <linux/sched.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/module.h> |
| 10 | |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 11 | /* |
| 12 | * Initialize an rwsem: |
| 13 | */ |
| 14 | void __init_rwsem(struct rw_semaphore *sem, const char *name, |
| 15 | struct lock_class_key *key) |
| 16 | { |
| 17 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 18 | /* |
| 19 | * Make sure we are not reinitializing a held semaphore: |
| 20 | */ |
| 21 | debug_check_no_locks_freed((void *)sem, sizeof(*sem)); |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 22 | lockdep_init_map(&sem->dep_map, name, key, 0); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 23 | #endif |
| 24 | sem->count = RWSEM_UNLOCKED_VALUE; |
| 25 | spin_lock_init(&sem->wait_lock); |
| 26 | INIT_LIST_HEAD(&sem->wait_list); |
| 27 | } |
| 28 | |
| 29 | EXPORT_SYMBOL(__init_rwsem); |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | struct rwsem_waiter { |
| 32 | struct list_head list; |
| 33 | struct task_struct *task; |
| 34 | unsigned int flags; |
| 35 | #define RWSEM_WAITING_FOR_READ 0x00000001 |
| 36 | #define RWSEM_WAITING_FOR_WRITE 0x00000002 |
| 37 | }; |
| 38 | |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame^] | 39 | /* Wake types for __rwsem_do_wake(). Note that RWSEM_WAKE_NO_ACTIVE and |
| 40 | * RWSEM_WAKE_READ_OWNED imply that the spinlock must have been kept held |
| 41 | * since the rwsem value was observed. |
| 42 | */ |
| 43 | #define RWSEM_WAKE_ANY 0 /* Wake whatever's at head of wait list */ |
| 44 | #define RWSEM_WAKE_NO_ACTIVE 1 /* rwsem was observed with no active thread */ |
| 45 | #define RWSEM_WAKE_READ_OWNED 2 /* rwsem was observed to be read owned */ |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /* |
| 48 | * handle the lock release when processes blocked on it that can now run |
| 49 | * - if we come here from up_xxxx(), then: |
| 50 | * - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed) |
| 51 | * - 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] | 52 | * - there must be someone on the queue |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | * - the spinlock must be held by the caller |
| 54 | * - woken process blocks are discarded from the list after having task zeroed |
| 55 | * - writers are only woken if downgrading is false |
| 56 | */ |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame^] | 57 | static struct rw_semaphore * |
| 58 | __rwsem_do_wake(struct rw_semaphore *sem, int wake_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
| 60 | struct rwsem_waiter *waiter; |
| 61 | struct task_struct *tsk; |
| 62 | struct list_head *next; |
| 63 | signed long oldcount, woken, loop; |
| 64 | |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 65 | waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list); |
| 66 | if (!(waiter->flags & RWSEM_WAITING_FOR_WRITE)) |
| 67 | goto readers_only; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame^] | 69 | if (wake_type == RWSEM_WAKE_READ_OWNED) |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 70 | goto out; |
| 71 | |
| 72 | /* There's a writer at the front of the queue - try to grant it the |
| 73 | * write lock. However, we only wake this writer if we can transition |
| 74 | * the active part of the count from 0 -> 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | */ |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 76 | try_again_write: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | oldcount = rwsem_atomic_update(RWSEM_ACTIVE_BIAS, sem) |
| 78 | - RWSEM_ACTIVE_BIAS; |
| 79 | if (oldcount & RWSEM_ACTIVE_MASK) |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 80 | /* Someone grabbed the sem already */ |
| 81 | goto undo_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | /* We must be careful not to touch 'waiter' after we set ->task = NULL. |
| 84 | * It is an allocated on the waiter's stack and may become invalid at |
| 85 | * any time after that point (due to a wakeup from another source). |
| 86 | */ |
| 87 | list_del(&waiter->list); |
| 88 | tsk = waiter->task; |
akpm@osdl.org | d59dd46 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 89 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | waiter->task = NULL; |
| 91 | wake_up_process(tsk); |
| 92 | put_task_struct(tsk); |
| 93 | goto out; |
| 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | readers_only: |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame^] | 96 | /* If we come here from up_xxxx(), another thread might have reached |
| 97 | * rwsem_down_failed_common() before we acquired the spinlock and |
| 98 | * woken up a waiter, making it now active. We prefer to check for |
| 99 | * this first in order to not spend too much time with the spinlock |
| 100 | * held if we're not going to be able to wake up readers in the end. |
| 101 | * |
| 102 | * Note that we do not need to update the rwsem count: any writer |
| 103 | * trying to acquire rwsem will run rwsem_down_write_failed() due |
| 104 | * to the waiting threads and block trying to acquire the spinlock. |
| 105 | * |
| 106 | * We use a dummy atomic update in order to acquire the cache line |
| 107 | * exclusively since we expect to succeed and run the final rwsem |
| 108 | * count adjustment pretty soon. |
| 109 | */ |
| 110 | if (wake_type == RWSEM_WAKE_ANY && |
| 111 | (rwsem_atomic_update(0, sem) & RWSEM_ACTIVE_MASK)) |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 112 | /* Someone grabbed the sem already */ |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame^] | 113 | goto out; |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 114 | |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 115 | /* Grant an infinite number of read locks to the readers at the front |
| 116 | * of the queue. Note we increment the 'active part' of the count by |
| 117 | * the number of readers before waking any processes up. |
| 118 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | woken = 0; |
| 120 | do { |
| 121 | woken++; |
| 122 | |
| 123 | if (waiter->list.next == &sem->wait_list) |
| 124 | break; |
| 125 | |
| 126 | waiter = list_entry(waiter->list.next, |
| 127 | struct rwsem_waiter, list); |
| 128 | |
| 129 | } while (waiter->flags & RWSEM_WAITING_FOR_READ); |
| 130 | |
| 131 | loop = woken; |
| 132 | woken *= RWSEM_ACTIVE_BIAS - RWSEM_WAITING_BIAS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | rwsem_atomic_add(woken, sem); |
| 135 | |
| 136 | next = sem->wait_list.next; |
| 137 | for (; loop > 0; loop--) { |
| 138 | waiter = list_entry(next, struct rwsem_waiter, list); |
| 139 | next = waiter->list.next; |
| 140 | tsk = waiter->task; |
akpm@osdl.org | d59dd46 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 141 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | waiter->task = NULL; |
| 143 | wake_up_process(tsk); |
| 144 | put_task_struct(tsk); |
| 145 | } |
| 146 | |
| 147 | sem->wait_list.next = next; |
| 148 | next->prev = &sem->wait_list; |
| 149 | |
| 150 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | return sem; |
| 152 | |
Michel Lespinasse | 91af708 | 2010-05-12 11:38:45 +0100 | [diff] [blame] | 153 | /* undo the change to the active count, but check for a transition |
| 154 | * 1->0 */ |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 155 | undo_write: |
Michel Lespinasse | 91af708 | 2010-05-12 11:38:45 +0100 | [diff] [blame] | 156 | if (rwsem_atomic_update(-RWSEM_ACTIVE_BIAS, sem) & RWSEM_ACTIVE_MASK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | goto out; |
Michel Lespinasse | 345af7b | 2010-08-09 17:21:15 -0700 | [diff] [blame] | 158 | goto try_again_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* |
| 162 | * wait for a lock to be granted |
| 163 | */ |
Livio Soares | c7af77b | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 164 | static struct rw_semaphore __sched * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | rwsem_down_failed_common(struct rw_semaphore *sem, |
| 166 | struct rwsem_waiter *waiter, signed long adjustment) |
| 167 | { |
| 168 | struct task_struct *tsk = current; |
| 169 | signed long count; |
| 170 | |
| 171 | set_task_state(tsk, TASK_UNINTERRUPTIBLE); |
| 172 | |
| 173 | /* set up my own style of waitqueue */ |
| 174 | spin_lock_irq(&sem->wait_lock); |
| 175 | waiter->task = tsk; |
| 176 | get_task_struct(tsk); |
| 177 | |
| 178 | list_add_tail(&waiter->list, &sem->wait_list); |
| 179 | |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame^] | 180 | /* we're now waiting on the lock, but no longer actively locking */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | count = rwsem_atomic_update(adjustment, sem); |
| 182 | |
| 183 | /* if there are no active locks, wake the front queued process(es) up */ |
| 184 | if (!(count & RWSEM_ACTIVE_MASK)) |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame^] | 185 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_NO_ACTIVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | spin_unlock_irq(&sem->wait_lock); |
| 188 | |
| 189 | /* wait to be given the lock */ |
| 190 | for (;;) { |
| 191 | if (!waiter->task) |
| 192 | break; |
| 193 | schedule(); |
| 194 | set_task_state(tsk, TASK_UNINTERRUPTIBLE); |
| 195 | } |
| 196 | |
| 197 | tsk->state = TASK_RUNNING; |
| 198 | |
| 199 | return sem; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * wait for the read lock to be granted |
| 204 | */ |
Ingo Molnar | d50efc6 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 205 | asmregparm struct rw_semaphore __sched * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | rwsem_down_read_failed(struct rw_semaphore *sem) |
| 207 | { |
| 208 | struct rwsem_waiter waiter; |
| 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | waiter.flags = RWSEM_WAITING_FOR_READ; |
| 211 | rwsem_down_failed_common(sem, &waiter, |
| 212 | RWSEM_WAITING_BIAS - RWSEM_ACTIVE_BIAS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | return sem; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * wait for the write lock to be granted |
| 218 | */ |
Ingo Molnar | d50efc6 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 219 | asmregparm struct rw_semaphore __sched * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | rwsem_down_write_failed(struct rw_semaphore *sem) |
| 221 | { |
| 222 | struct rwsem_waiter waiter; |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | waiter.flags = RWSEM_WAITING_FOR_WRITE; |
| 225 | rwsem_down_failed_common(sem, &waiter, -RWSEM_ACTIVE_BIAS); |
| 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | return sem; |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * handle waking up a waiter on the semaphore |
| 232 | * - up_read/up_write has decremented the active part of count if we come here |
| 233 | */ |
Ingo Molnar | d50efc6 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 234 | asmregparm struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
| 236 | unsigned long flags; |
| 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | spin_lock_irqsave(&sem->wait_lock, flags); |
| 239 | |
| 240 | /* do nothing if list empty */ |
| 241 | if (!list_empty(&sem->wait_list)) |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame^] | 242 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
| 244 | spin_unlock_irqrestore(&sem->wait_lock, flags); |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | return sem; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * downgrade a write lock into a read lock |
| 251 | * - caller incremented waiting part of count and discovered it still negative |
| 252 | * - just wake up any readers at the front of the queue |
| 253 | */ |
Ingo Molnar | d50efc6 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 254 | asmregparm struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
| 256 | unsigned long flags; |
| 257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | spin_lock_irqsave(&sem->wait_lock, flags); |
| 259 | |
| 260 | /* do nothing if list empty */ |
| 261 | if (!list_empty(&sem->wait_list)) |
Michel Lespinasse | 70bdc6e | 2010-08-09 17:21:17 -0700 | [diff] [blame^] | 262 | sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
| 264 | spin_unlock_irqrestore(&sem->wait_lock, flags); |
| 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | return sem; |
| 267 | } |
| 268 | |
| 269 | EXPORT_SYMBOL(rwsem_down_read_failed); |
| 270 | EXPORT_SYMBOL(rwsem_down_write_failed); |
| 271 | EXPORT_SYMBOL(rwsem_wake); |
| 272 | EXPORT_SYMBOL(rwsem_downgrade_wake); |