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