Linux 4.15.6
[linux/fpc-iii.git] / kernel / locking / rwsem-spinlock.c
bloba7ffb2a96ede0b9938671e6278647181542f7102
1 // SPDX-License-Identifier: GPL-2.0
2 /* rwsem-spinlock.c: R/W semaphores: contention handling functions for
3 * generic spinlock implementation
5 * Copyright (c) 2001 David Howells (dhowells@redhat.com).
6 * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de>
7 * - Derived also from comments by Linus
8 */
9 #include <linux/rwsem.h>
10 #include <linux/sched/signal.h>
11 #include <linux/sched/debug.h>
12 #include <linux/export.h>
14 enum rwsem_waiter_type {
15 RWSEM_WAITING_FOR_WRITE,
16 RWSEM_WAITING_FOR_READ
19 struct rwsem_waiter {
20 struct list_head list;
21 struct task_struct *task;
22 enum rwsem_waiter_type type;
25 int rwsem_is_locked(struct rw_semaphore *sem)
27 int ret = 1;
28 unsigned long flags;
30 if (raw_spin_trylock_irqsave(&sem->wait_lock, flags)) {
31 ret = (sem->count != 0);
32 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
34 return ret;
36 EXPORT_SYMBOL(rwsem_is_locked);
39 * initialise the semaphore
41 void __init_rwsem(struct rw_semaphore *sem, const char *name,
42 struct lock_class_key *key)
44 #ifdef CONFIG_DEBUG_LOCK_ALLOC
46 * Make sure we are not reinitializing a held semaphore:
48 debug_check_no_locks_freed((void *)sem, sizeof(*sem));
49 lockdep_init_map(&sem->dep_map, name, key, 0);
50 #endif
51 sem->count = 0;
52 raw_spin_lock_init(&sem->wait_lock);
53 INIT_LIST_HEAD(&sem->wait_list);
55 EXPORT_SYMBOL(__init_rwsem);
58 * handle the lock release when processes blocked on it that can now run
59 * - if we come here, then:
60 * - the 'active count' _reached_ zero
61 * - the 'waiting count' is non-zero
62 * - the spinlock must be held by the caller
63 * - woken process blocks are discarded from the list after having task zeroed
64 * - writers are only woken if wakewrite is non-zero
66 static inline struct rw_semaphore *
67 __rwsem_do_wake(struct rw_semaphore *sem, int wakewrite)
69 struct rwsem_waiter *waiter;
70 struct task_struct *tsk;
71 int woken;
73 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
75 if (waiter->type == RWSEM_WAITING_FOR_WRITE) {
76 if (wakewrite)
77 /* Wake up a writer. Note that we do not grant it the
78 * lock - it will have to acquire it when it runs. */
79 wake_up_process(waiter->task);
80 goto out;
83 /* grant an infinite number of read locks to the front of the queue */
84 woken = 0;
85 do {
86 struct list_head *next = waiter->list.next;
88 list_del(&waiter->list);
89 tsk = waiter->task;
91 * Make sure we do not wakeup the next reader before
92 * setting the nil condition to grant the next reader;
93 * otherwise we could miss the wakeup on the other
94 * side and end up sleeping again. See the pairing
95 * in rwsem_down_read_failed().
97 smp_mb();
98 waiter->task = NULL;
99 wake_up_process(tsk);
100 put_task_struct(tsk);
101 woken++;
102 if (next == &sem->wait_list)
103 break;
104 waiter = list_entry(next, struct rwsem_waiter, list);
105 } while (waiter->type != RWSEM_WAITING_FOR_WRITE);
107 sem->count += woken;
109 out:
110 return sem;
114 * wake a single writer
116 static inline struct rw_semaphore *
117 __rwsem_wake_one_writer(struct rw_semaphore *sem)
119 struct rwsem_waiter *waiter;
121 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
122 wake_up_process(waiter->task);
124 return sem;
128 * get a read lock on the semaphore
130 int __sched __down_read_common(struct rw_semaphore *sem, int state)
132 struct rwsem_waiter waiter;
133 unsigned long flags;
135 raw_spin_lock_irqsave(&sem->wait_lock, flags);
137 if (sem->count >= 0 && list_empty(&sem->wait_list)) {
138 /* granted */
139 sem->count++;
140 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
141 goto out;
144 /* set up my own style of waitqueue */
145 waiter.task = current;
146 waiter.type = RWSEM_WAITING_FOR_READ;
147 get_task_struct(current);
149 list_add_tail(&waiter.list, &sem->wait_list);
151 /* wait to be given the lock */
152 for (;;) {
153 if (!waiter.task)
154 break;
155 if (signal_pending_state(state, current))
156 goto out_nolock;
157 set_current_state(state);
158 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
159 schedule();
160 raw_spin_lock_irqsave(&sem->wait_lock, flags);
163 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
164 out:
165 return 0;
167 out_nolock:
169 * We didn't take the lock, so that there is a writer, which
170 * is owner or the first waiter of the sem. If it's a waiter,
171 * it will be woken by current owner. Not need to wake anybody.
173 list_del(&waiter.list);
174 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
175 return -EINTR;
178 void __sched __down_read(struct rw_semaphore *sem)
180 __down_read_common(sem, TASK_UNINTERRUPTIBLE);
183 int __sched __down_read_killable(struct rw_semaphore *sem)
185 return __down_read_common(sem, TASK_KILLABLE);
189 * trylock for reading -- returns 1 if successful, 0 if contention
191 int __down_read_trylock(struct rw_semaphore *sem)
193 unsigned long flags;
194 int ret = 0;
197 raw_spin_lock_irqsave(&sem->wait_lock, flags);
199 if (sem->count >= 0 && list_empty(&sem->wait_list)) {
200 /* granted */
201 sem->count++;
202 ret = 1;
205 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
207 return ret;
211 * get a write lock on the semaphore
213 int __sched __down_write_common(struct rw_semaphore *sem, int state)
215 struct rwsem_waiter waiter;
216 unsigned long flags;
217 int ret = 0;
219 raw_spin_lock_irqsave(&sem->wait_lock, flags);
221 /* set up my own style of waitqueue */
222 waiter.task = current;
223 waiter.type = RWSEM_WAITING_FOR_WRITE;
224 list_add_tail(&waiter.list, &sem->wait_list);
226 /* wait for someone to release the lock */
227 for (;;) {
229 * That is the key to support write lock stealing: allows the
230 * task already on CPU to get the lock soon rather than put
231 * itself into sleep and waiting for system woke it or someone
232 * else in the head of the wait list up.
234 if (sem->count == 0)
235 break;
236 if (signal_pending_state(state, current))
237 goto out_nolock;
239 set_current_state(state);
240 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
241 schedule();
242 raw_spin_lock_irqsave(&sem->wait_lock, flags);
244 /* got the lock */
245 sem->count = -1;
246 list_del(&waiter.list);
248 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
250 return ret;
252 out_nolock:
253 list_del(&waiter.list);
254 if (!list_empty(&sem->wait_list) && sem->count >= 0)
255 __rwsem_do_wake(sem, 0);
256 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
258 return -EINTR;
261 void __sched __down_write(struct rw_semaphore *sem)
263 __down_write_common(sem, TASK_UNINTERRUPTIBLE);
266 int __sched __down_write_killable(struct rw_semaphore *sem)
268 return __down_write_common(sem, TASK_KILLABLE);
272 * trylock for writing -- returns 1 if successful, 0 if contention
274 int __down_write_trylock(struct rw_semaphore *sem)
276 unsigned long flags;
277 int ret = 0;
279 raw_spin_lock_irqsave(&sem->wait_lock, flags);
281 if (sem->count == 0) {
282 /* got the lock */
283 sem->count = -1;
284 ret = 1;
287 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
289 return ret;
293 * release a read lock on the semaphore
295 void __up_read(struct rw_semaphore *sem)
297 unsigned long flags;
299 raw_spin_lock_irqsave(&sem->wait_lock, flags);
301 if (--sem->count == 0 && !list_empty(&sem->wait_list))
302 sem = __rwsem_wake_one_writer(sem);
304 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
308 * release a write lock on the semaphore
310 void __up_write(struct rw_semaphore *sem)
312 unsigned long flags;
314 raw_spin_lock_irqsave(&sem->wait_lock, flags);
316 sem->count = 0;
317 if (!list_empty(&sem->wait_list))
318 sem = __rwsem_do_wake(sem, 1);
320 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
324 * downgrade a write lock into a read lock
325 * - just wake up any readers at the front of the queue
327 void __downgrade_write(struct rw_semaphore *sem)
329 unsigned long flags;
331 raw_spin_lock_irqsave(&sem->wait_lock, flags);
333 sem->count = 1;
334 if (!list_empty(&sem->wait_list))
335 sem = __rwsem_do_wake(sem, 0);
337 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);