blob: a608f7a8fbd1d20b08b440d63f0491575653ad22 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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 Gortmaker8bc3bcc2011-11-16 21:29:17 -050010#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Michel Lespinassee2d57f72013-05-07 06:45:49 -070012enum rwsem_waiter_type {
13 RWSEM_WAITING_FOR_WRITE,
14 RWSEM_WAITING_FOR_READ
15};
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017struct rwsem_waiter {
18 struct list_head list;
19 struct task_struct *task;
Michel Lespinassee2d57f72013-05-07 06:45:49 -070020 enum rwsem_waiter_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021};
22
Amerigo Wang29671f22009-12-14 18:00:21 -080023int rwsem_is_locked(struct rw_semaphore *sem)
24{
25 int ret = 1;
26 unsigned long flags;
27
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +010028 if (raw_spin_trylock_irqsave(&sem->wait_lock, flags)) {
Peter Zijlstra13b9a962014-07-16 14:54:55 +020029 ret = (sem->count != 0);
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +010030 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Amerigo Wang29671f22009-12-14 18:00:21 -080031 }
32 return ret;
33}
34EXPORT_SYMBOL(rwsem_is_locked);
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * initialise the semaphore
38 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070039void __init_rwsem(struct rw_semaphore *sem, const char *name,
40 struct lock_class_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Ingo Molnar4ea21762006-07-03 00:24:53 -070042#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 Zijlstra4dfbb9d2006-10-11 01:45:14 -040047 lockdep_init_map(&sem->dep_map, name, key, 0);
Ingo Molnar4ea21762006-07-03 00:24:53 -070048#endif
Peter Zijlstra13b9a962014-07-16 14:54:55 +020049 sem->count = 0;
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +010050 raw_spin_lock_init(&sem->wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 INIT_LIST_HEAD(&sem->wait_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
Amerigo Wang118d52d2009-12-14 18:00:20 -080053EXPORT_SYMBOL(__init_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
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 */
64static 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 Torvalds1da177e2005-04-16 15:20:36 -070071 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
72
Michel Lespinassee2d57f72013-05-07 06:45:49 -070073 if (waiter->type == RWSEM_WAITING_FOR_WRITE) {
Michel Lespinasse8cf53222013-05-07 06:45:58 -070074 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 Torvalds1da177e2005-04-16 15:20:36 -070078 goto out;
79 }
80
81 /* grant an infinite number of read locks to the front of the queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 woken = 0;
Michel Lespinasse8cf53222013-05-07 06:45:58 -070083 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 struct list_head *next = waiter->list.next;
85
86 list_del(&waiter->list);
87 tsk = waiter->task;
Davidlohr Bueso49e4b2b2015-01-30 01:14:24 -080088 /*
89 * Make sure we do not wakeup the next reader before
90 * setting the nil condition to grant the next reader;
91 * otherwise we could miss the wakeup on the other
92 * side and end up sleeping again. See the pairing
93 * in rwsem_down_read_failed().
94 */
akpm@osdl.orgd59dd462005-05-01 08:58:47 -070095 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 waiter->task = NULL;
97 wake_up_process(tsk);
98 put_task_struct(tsk);
99 woken++;
Michel Lespinasse8cf53222013-05-07 06:45:58 -0700100 if (next == &sem->wait_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 break;
102 waiter = list_entry(next, struct rwsem_waiter, list);
Michel Lespinasse8cf53222013-05-07 06:45:58 -0700103 } while (waiter->type != RWSEM_WAITING_FOR_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Peter Zijlstra13b9a962014-07-16 14:54:55 +0200105 sem->count += woken;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return sem;
109}
110
111/*
112 * wake a single writer
113 */
114static inline struct rw_semaphore *
115__rwsem_wake_one_writer(struct rw_semaphore *sem)
116{
117 struct rwsem_waiter *waiter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800120 wake_up_process(waiter->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return sem;
123}
124
125/*
126 * get a read lock on the semaphore
127 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800128void __sched __down_read(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct rwsem_waiter waiter;
131 struct task_struct *tsk;
Kevin Hilman3eac4ab2010-04-07 11:52:46 -0700132 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100134 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Peter Zijlstra13b9a962014-07-16 14:54:55 +0200136 if (sem->count >= 0 && list_empty(&sem->wait_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 /* granted */
Peter Zijlstra13b9a962014-07-16 14:54:55 +0200138 sem->count++;
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100139 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 goto out;
141 }
142
143 tsk = current;
144 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
145
146 /* set up my own style of waitqueue */
147 waiter.task = tsk;
Michel Lespinassee2d57f72013-05-07 06:45:49 -0700148 waiter.type = RWSEM_WAITING_FOR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 get_task_struct(tsk);
150
151 list_add_tail(&waiter.list, &sem->wait_list);
152
153 /* we don't need to touch the semaphore struct anymore */
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100154 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 /* wait to be given the lock */
157 for (;;) {
158 if (!waiter.task)
159 break;
160 schedule();
161 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
162 }
163
Davidlohr Bueso73105992015-01-25 23:36:04 -0800164 __set_task_state(tsk, TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 out:
Ingo Molnarc4e05112006-07-03 00:24:29 -0700166 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169/*
170 * trylock for reading -- returns 1 if successful, 0 if contention
171 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800172int __down_read_trylock(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 unsigned long flags;
175 int ret = 0;
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100178 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Peter Zijlstra13b9a962014-07-16 14:54:55 +0200180 if (sem->count >= 0 && list_empty(&sem->wait_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /* granted */
Peter Zijlstra13b9a962014-07-16 14:54:55 +0200182 sem->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 ret = 1;
184 }
185
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100186 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return ret;
189}
190
191/*
192 * get a write lock on the semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 */
Michal Hockod4799602016-04-07 17:12:26 +0200194int __sched __down_write_common(struct rw_semaphore *sem, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 struct rwsem_waiter waiter;
197 struct task_struct *tsk;
Kevin Hilman3eac4ab2010-04-07 11:52:46 -0700198 unsigned long flags;
Michal Hockod4799602016-04-07 17:12:26 +0200199 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100201 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* set up my own style of waitqueue */
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800204 tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 waiter.task = tsk;
Michel Lespinassee2d57f72013-05-07 06:45:49 -0700206 waiter.type = RWSEM_WAITING_FOR_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 list_add_tail(&waiter.list, &sem->wait_list);
208
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800209 /* wait for someone to release the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 for (;;) {
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800211 /*
212 * That is the key to support write lock stealing: allows the
213 * task already on CPU to get the lock soon rather than put
214 * itself into sleep and waiting for system woke it or someone
215 * else in the head of the wait list up.
216 */
Peter Zijlstra13b9a962014-07-16 14:54:55 +0200217 if (sem->count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 break;
Niklas Casselc4060962017-02-25 01:17:53 +0100219 if (signal_pending_state(state, current))
220 goto out_nolock;
Michal Hockod4799602016-04-07 17:12:26 +0200221 set_task_state(tsk, state);
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800222 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
223 schedule();
224 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800226 /* got the lock */
Peter Zijlstra13b9a962014-07-16 14:54:55 +0200227 sem->count = -1;
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800228 list_del(&waiter.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800230 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Michal Hockod4799602016-04-07 17:12:26 +0200231
232 return ret;
Niklas Casselc4060962017-02-25 01:17:53 +0100233
234out_nolock:
235 list_del(&waiter.list);
Kirill Tkhai5497d742017-06-16 16:44:34 +0300236 if (!list_empty(&sem->wait_list) && sem->count >= 0)
237 __rwsem_do_wake(sem, 0);
Niklas Casselc4060962017-02-25 01:17:53 +0100238 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
239
240 return -EINTR;
Michal Hockod4799602016-04-07 17:12:26 +0200241}
242
243void __sched __down_write(struct rw_semaphore *sem)
244{
245 __down_write_common(sem, TASK_UNINTERRUPTIBLE);
246}
247
248int __sched __down_write_killable(struct rw_semaphore *sem)
249{
250 return __down_write_common(sem, TASK_KILLABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253/*
254 * trylock for writing -- returns 1 if successful, 0 if contention
255 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800256int __down_write_trylock(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 unsigned long flags;
259 int ret = 0;
260
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100261 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Peter Zijlstra13b9a962014-07-16 14:54:55 +0200263 if (sem->count == 0) {
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800264 /* got the lock */
Peter Zijlstra13b9a962014-07-16 14:54:55 +0200265 sem->count = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 ret = 1;
267 }
268
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100269 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return ret;
272}
273
274/*
275 * release a read lock on the semaphore
276 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800277void __up_read(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 unsigned long flags;
280
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100281 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Peter Zijlstra13b9a962014-07-16 14:54:55 +0200283 if (--sem->count == 0 && !list_empty(&sem->wait_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 sem = __rwsem_wake_one_writer(sem);
285
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100286 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/*
290 * release a write lock on the semaphore
291 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800292void __up_write(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 unsigned long flags;
295
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100296 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Peter Zijlstra13b9a962014-07-16 14:54:55 +0200298 sem->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (!list_empty(&sem->wait_list))
300 sem = __rwsem_do_wake(sem, 1);
301
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100302 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305/*
306 * downgrade a write lock into a read lock
307 * - just wake up any readers at the front of the queue
308 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800309void __downgrade_write(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 unsigned long flags;
312
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100313 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Peter Zijlstra13b9a962014-07-16 14:54:55 +0200315 sem->count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (!list_empty(&sem->wait_list))
317 sem = __rwsem_do_wake(sem, 0);
318
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100319 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321