Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* rwsem-spinlock.c: R/W semaphores: contention handling functions for |
| 2 | * generic spinlock implementation |
| 3 | * |
| 4 | * Copyright (c) 2001 David Howells (dhowells@redhat.com). |
| 5 | * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de> |
| 6 | * - Derived also from comments by Linus |
| 7 | */ |
| 8 | #include <linux/rwsem.h> |
| 9 | #include <linux/sched.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 10 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
| 12 | struct rwsem_waiter { |
| 13 | struct list_head list; |
| 14 | struct task_struct *task; |
| 15 | unsigned int flags; |
| 16 | #define RWSEM_WAITING_FOR_READ 0x00000001 |
| 17 | #define RWSEM_WAITING_FOR_WRITE 0x00000002 |
| 18 | }; |
| 19 | |
Amerigo Wang | 29671f2 | 2009-12-14 18:00:21 -0800 | [diff] [blame] | 20 | int rwsem_is_locked(struct rw_semaphore *sem) |
| 21 | { |
| 22 | int ret = 1; |
| 23 | unsigned long flags; |
| 24 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 25 | if (raw_spin_trylock_irqsave(&sem->wait_lock, flags)) { |
Amerigo Wang | 29671f2 | 2009-12-14 18:00:21 -0800 | [diff] [blame] | 26 | ret = (sem->activity != 0); |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 27 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
Amerigo Wang | 29671f2 | 2009-12-14 18:00:21 -0800 | [diff] [blame] | 28 | } |
| 29 | return ret; |
| 30 | } |
| 31 | EXPORT_SYMBOL(rwsem_is_locked); |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | /* |
| 34 | * initialise the semaphore |
| 35 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 36 | void __init_rwsem(struct rw_semaphore *sem, const char *name, |
| 37 | struct lock_class_key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 39 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 40 | /* |
| 41 | * Make sure we are not reinitializing a held semaphore: |
| 42 | */ |
| 43 | debug_check_no_locks_freed((void *)sem, sizeof(*sem)); |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 44 | lockdep_init_map(&sem->dep_map, name, key, 0); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 45 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | sem->activity = 0; |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 47 | raw_spin_lock_init(&sem->wait_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | INIT_LIST_HEAD(&sem->wait_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
Amerigo Wang | 118d52d | 2009-12-14 18:00:20 -0800 | [diff] [blame] | 50 | EXPORT_SYMBOL(__init_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * handle the lock release when processes blocked on it that can now run |
| 54 | * - if we come here, then: |
| 55 | * - the 'active count' _reached_ zero |
| 56 | * - the 'waiting count' is non-zero |
| 57 | * - the spinlock must be held by the caller |
| 58 | * - woken process blocks are discarded from the list after having task zeroed |
| 59 | * - writers are only woken if wakewrite is non-zero |
| 60 | */ |
| 61 | static inline struct rw_semaphore * |
| 62 | __rwsem_do_wake(struct rw_semaphore *sem, int wakewrite) |
| 63 | { |
| 64 | struct rwsem_waiter *waiter; |
| 65 | struct task_struct *tsk; |
| 66 | int woken; |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list); |
| 69 | |
| 70 | if (!wakewrite) { |
| 71 | if (waiter->flags & RWSEM_WAITING_FOR_WRITE) |
| 72 | goto out; |
| 73 | goto dont_wake_writers; |
| 74 | } |
| 75 | |
Yuanhan Liu | 41ef8f8 | 2013-02-01 18:59:16 +0800 | [diff] [blame] | 76 | /* |
| 77 | * as we support write lock stealing, we can't set sem->activity |
| 78 | * to -1 here to indicate we get the lock. Instead, we wake it up |
| 79 | * to let it go get it again. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | */ |
| 81 | if (waiter->flags & RWSEM_WAITING_FOR_WRITE) { |
Yuanhan Liu | 41ef8f8 | 2013-02-01 18:59:16 +0800 | [diff] [blame] | 82 | wake_up_process(waiter->task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | goto out; |
| 84 | } |
| 85 | |
| 86 | /* grant an infinite number of read locks to the front of the queue */ |
| 87 | dont_wake_writers: |
| 88 | woken = 0; |
| 89 | while (waiter->flags & RWSEM_WAITING_FOR_READ) { |
| 90 | struct list_head *next = waiter->list.next; |
| 91 | |
| 92 | list_del(&waiter->list); |
| 93 | tsk = waiter->task; |
akpm@osdl.org | d59dd46 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 94 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | waiter->task = NULL; |
| 96 | wake_up_process(tsk); |
| 97 | put_task_struct(tsk); |
| 98 | woken++; |
| 99 | if (list_empty(&sem->wait_list)) |
| 100 | break; |
| 101 | waiter = list_entry(next, struct rwsem_waiter, list); |
| 102 | } |
| 103 | |
| 104 | sem->activity += woken; |
| 105 | |
| 106 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | return sem; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * wake a single writer |
| 112 | */ |
| 113 | static inline struct rw_semaphore * |
| 114 | __rwsem_wake_one_writer(struct rw_semaphore *sem) |
| 115 | { |
| 116 | struct rwsem_waiter *waiter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list); |
Yuanhan Liu | 41ef8f8 | 2013-02-01 18:59:16 +0800 | [diff] [blame] | 119 | wake_up_process(waiter->task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | return sem; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * get a read lock on the semaphore |
| 126 | */ |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 127 | void __sched __down_read(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
| 129 | struct rwsem_waiter waiter; |
| 130 | struct task_struct *tsk; |
Kevin Hilman | 3eac4ab | 2010-04-07 11:52:46 -0700 | [diff] [blame] | 131 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 133 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | if (sem->activity >= 0 && list_empty(&sem->wait_list)) { |
| 136 | /* granted */ |
| 137 | sem->activity++; |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 138 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | goto out; |
| 140 | } |
| 141 | |
| 142 | tsk = current; |
| 143 | set_task_state(tsk, TASK_UNINTERRUPTIBLE); |
| 144 | |
| 145 | /* set up my own style of waitqueue */ |
| 146 | waiter.task = tsk; |
| 147 | waiter.flags = RWSEM_WAITING_FOR_READ; |
| 148 | get_task_struct(tsk); |
| 149 | |
| 150 | list_add_tail(&waiter.list, &sem->wait_list); |
| 151 | |
| 152 | /* we don't need to touch the semaphore struct anymore */ |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 153 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
| 155 | /* wait to be given the lock */ |
| 156 | for (;;) { |
| 157 | if (!waiter.task) |
| 158 | break; |
| 159 | schedule(); |
| 160 | set_task_state(tsk, TASK_UNINTERRUPTIBLE); |
| 161 | } |
| 162 | |
| 163 | tsk->state = TASK_RUNNING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | out: |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 165 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* |
| 169 | * trylock for reading -- returns 1 if successful, 0 if contention |
| 170 | */ |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 171 | int __down_read_trylock(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
| 173 | unsigned long flags; |
| 174 | int ret = 0; |
| 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 177 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
| 179 | if (sem->activity >= 0 && list_empty(&sem->wait_list)) { |
| 180 | /* granted */ |
| 181 | sem->activity++; |
| 182 | ret = 1; |
| 183 | } |
| 184 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 185 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * get a write lock on the semaphore |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | */ |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 193 | void __sched __down_write_nested(struct rw_semaphore *sem, int subclass) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | { |
| 195 | struct rwsem_waiter waiter; |
| 196 | struct task_struct *tsk; |
Kevin Hilman | 3eac4ab | 2010-04-07 11:52:46 -0700 | [diff] [blame] | 197 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 199 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | /* set up my own style of waitqueue */ |
Yuanhan Liu | 41ef8f8 | 2013-02-01 18:59:16 +0800 | [diff] [blame] | 202 | tsk = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | waiter.task = tsk; |
| 204 | waiter.flags = RWSEM_WAITING_FOR_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | list_add_tail(&waiter.list, &sem->wait_list); |
| 206 | |
Yuanhan Liu | 41ef8f8 | 2013-02-01 18:59:16 +0800 | [diff] [blame] | 207 | /* wait for someone to release the lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | for (;;) { |
Yuanhan Liu | 41ef8f8 | 2013-02-01 18:59:16 +0800 | [diff] [blame] | 209 | /* |
| 210 | * That is the key to support write lock stealing: allows the |
| 211 | * task already on CPU to get the lock soon rather than put |
| 212 | * itself into sleep and waiting for system woke it or someone |
| 213 | * else in the head of the wait list up. |
| 214 | */ |
| 215 | if (sem->activity == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | set_task_state(tsk, TASK_UNINTERRUPTIBLE); |
Yuanhan Liu | 41ef8f8 | 2013-02-01 18:59:16 +0800 | [diff] [blame] | 218 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
| 219 | schedule(); |
| 220 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
Yuanhan Liu | 41ef8f8 | 2013-02-01 18:59:16 +0800 | [diff] [blame] | 222 | /* got the lock */ |
| 223 | sem->activity = -1; |
| 224 | list_del(&waiter.list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Yuanhan Liu | 41ef8f8 | 2013-02-01 18:59:16 +0800 | [diff] [blame] | 226 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 229 | void __sched __down_write(struct rw_semaphore *sem) |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 230 | { |
| 231 | __down_write_nested(sem, 0); |
| 232 | } |
| 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | /* |
| 235 | * trylock for writing -- returns 1 if successful, 0 if contention |
| 236 | */ |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 237 | int __down_write_trylock(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
| 239 | unsigned long flags; |
| 240 | int ret = 0; |
| 241 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 242 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Yuanhan Liu | 41ef8f8 | 2013-02-01 18:59:16 +0800 | [diff] [blame] | 244 | if (sem->activity == 0) { |
| 245 | /* got the lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | sem->activity = -1; |
| 247 | ret = 1; |
| 248 | } |
| 249 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 250 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * release a read lock on the semaphore |
| 257 | */ |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 258 | void __up_read(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | { |
| 260 | unsigned long flags; |
| 261 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 262 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
| 264 | if (--sem->activity == 0 && !list_empty(&sem->wait_list)) |
| 265 | sem = __rwsem_wake_one_writer(sem); |
| 266 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 267 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* |
| 271 | * release a write lock on the semaphore |
| 272 | */ |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 273 | void __up_write(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
| 275 | unsigned long flags; |
| 276 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 277 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
| 279 | sem->activity = 0; |
| 280 | if (!list_empty(&sem->wait_list)) |
| 281 | sem = __rwsem_do_wake(sem, 1); |
| 282 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 283 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /* |
| 287 | * downgrade a write lock into a read lock |
| 288 | * - just wake up any readers at the front of the queue |
| 289 | */ |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 290 | void __downgrade_write(struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
| 292 | unsigned long flags; |
| 293 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 294 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
| 296 | sem->activity = 1; |
| 297 | if (!list_empty(&sem->wait_list)) |
| 298 | sem = __rwsem_do_wake(sem, 0); |
| 299 | |
Thomas Gleixner | ddb6c9b | 2010-02-24 09:54:54 +0100 | [diff] [blame] | 300 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | |