4 * The ldisc semaphore is semantically a rw_semaphore but which enforces
5 * an alternate policy, namely:
6 * 1) Supports lock wait timeouts
7 * 2) Write waiter has priority
8 * 3) Downgrading is not supported
10 * Implementation notes:
11 * 1) Upper half of semaphore count is a wait count (differs from rwsem
12 * in that rwsem normalizes the upper half to the wait bias)
13 * 2) Lacks overflow checking
15 * The generic counting was copied and modified from include/asm-generic/rwsem.h
16 * by Paul Mackerras <paulus@samba.org>.
18 * The scheduling policy was copied and modified from lib/rwsem.c
19 * Written by David Howells (dhowells@redhat.com).
21 * This implementation incorporates the write lock stealing work of
22 * Michel Lespinasse <walken@google.com>.
24 * Copyright (C) 2013 Peter Hurley <peter@hurleysoftware.com>
26 * This file may be redistributed under the terms of the GNU General Public
30 #include <linux/list.h>
31 #include <linux/spinlock.h>
32 #include <linux/atomic.h>
33 #include <linux/tty.h>
34 #include <linux/sched.h>
35 #include <linux/sched/debug.h>
36 #include <linux/sched/task.h>
39 #ifdef CONFIG_DEBUG_LOCK_ALLOC
40 # define __acq(l, s, t, r, c, n, i) \
41 lock_acquire(&(l)->dep_map, s, t, r, c, n, i)
42 # define __rel(l, n, i) \
43 lock_release(&(l)->dep_map, n, i)
44 #define lockdep_acquire(l, s, t, i) __acq(l, s, t, 0, 1, NULL, i)
45 #define lockdep_acquire_nest(l, s, t, n, i) __acq(l, s, t, 0, 1, n, i)
46 #define lockdep_acquire_read(l, s, t, i) __acq(l, s, t, 1, 1, NULL, i)
47 #define lockdep_release(l, n, i) __rel(l, n, i)
49 # define lockdep_acquire(l, s, t, i) do { } while (0)
50 # define lockdep_acquire_nest(l, s, t, n, i) do { } while (0)
51 # define lockdep_acquire_read(l, s, t, i) do { } while (0)
52 # define lockdep_release(l, n, i) do { } while (0)
55 #ifdef CONFIG_LOCK_STAT
56 # define lock_stat(_lock, stat) lock_##stat(&(_lock)->dep_map, _RET_IP_)
58 # define lock_stat(_lock, stat) do { } while (0)
62 #if BITS_PER_LONG == 64
63 # define LDSEM_ACTIVE_MASK 0xffffffffL
65 # define LDSEM_ACTIVE_MASK 0x0000ffffL
68 #define LDSEM_UNLOCKED 0L
69 #define LDSEM_ACTIVE_BIAS 1L
70 #define LDSEM_WAIT_BIAS (-LDSEM_ACTIVE_MASK-1)
71 #define LDSEM_READ_BIAS LDSEM_ACTIVE_BIAS
72 #define LDSEM_WRITE_BIAS (LDSEM_WAIT_BIAS + LDSEM_ACTIVE_BIAS)
75 struct list_head list;
76 struct task_struct *task;
79 static inline long ldsem_atomic_update(long delta, struct ld_semaphore *sem)
81 return atomic_long_add_return(delta, (atomic_long_t *)&sem->count);
85 * ldsem_cmpxchg() updates @*old with the last-known sem->count value.
86 * Returns 1 if count was successfully changed; @*old will have @new value.
87 * Returns 0 if count was not changed; @*old will have most recent sem->count
89 static inline int ldsem_cmpxchg(long *old, long new, struct ld_semaphore *sem)
91 long tmp = atomic_long_cmpxchg(&sem->count, *old, new);
102 * Initialize an ldsem:
104 void __init_ldsem(struct ld_semaphore *sem, const char *name,
105 struct lock_class_key *key)
107 #ifdef CONFIG_DEBUG_LOCK_ALLOC
109 * Make sure we are not reinitializing a held semaphore:
111 debug_check_no_locks_freed((void *)sem, sizeof(*sem));
112 lockdep_init_map(&sem->dep_map, name, key, 0);
114 sem->count = LDSEM_UNLOCKED;
115 sem->wait_readers = 0;
116 raw_spin_lock_init(&sem->wait_lock);
117 INIT_LIST_HEAD(&sem->read_wait);
118 INIT_LIST_HEAD(&sem->write_wait);
121 static void __ldsem_wake_readers(struct ld_semaphore *sem)
123 struct ldsem_waiter *waiter, *next;
124 struct task_struct *tsk;
127 /* Try to grant read locks to all readers on the read wait list.
128 * Note the 'active part' of the count is incremented by
129 * the number of readers before waking any processes up.
131 adjust = sem->wait_readers * (LDSEM_ACTIVE_BIAS - LDSEM_WAIT_BIAS);
132 count = ldsem_atomic_update(adjust, sem);
136 if (ldsem_cmpxchg(&count, count - adjust, sem))
140 list_for_each_entry_safe(waiter, next, &sem->read_wait, list) {
142 smp_store_release(&waiter->task, NULL);
143 wake_up_process(tsk);
144 put_task_struct(tsk);
146 INIT_LIST_HEAD(&sem->read_wait);
147 sem->wait_readers = 0;
150 static inline int writer_trylock(struct ld_semaphore *sem)
152 /* only wake this writer if the active part of the count can be
153 * transitioned from 0 -> 1
155 long count = ldsem_atomic_update(LDSEM_ACTIVE_BIAS, sem);
157 if ((count & LDSEM_ACTIVE_MASK) == LDSEM_ACTIVE_BIAS)
159 if (ldsem_cmpxchg(&count, count - LDSEM_ACTIVE_BIAS, sem))
164 static void __ldsem_wake_writer(struct ld_semaphore *sem)
166 struct ldsem_waiter *waiter;
168 waiter = list_entry(sem->write_wait.next, struct ldsem_waiter, list);
169 wake_up_process(waiter->task);
173 * handle the lock release when processes blocked on it that can now run
174 * - if we come here from up_xxxx(), then:
175 * - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed)
176 * - the 'waiting part' of count (&0xffff0000) is -ve (and will still be so)
177 * - the spinlock must be held by the caller
178 * - woken process blocks are discarded from the list after having task zeroed
180 static void __ldsem_wake(struct ld_semaphore *sem)
182 if (!list_empty(&sem->write_wait))
183 __ldsem_wake_writer(sem);
184 else if (!list_empty(&sem->read_wait))
185 __ldsem_wake_readers(sem);
188 static void ldsem_wake(struct ld_semaphore *sem)
192 raw_spin_lock_irqsave(&sem->wait_lock, flags);
194 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
198 * wait for the read lock to be granted
200 static struct ld_semaphore __sched *
201 down_read_failed(struct ld_semaphore *sem, long count, long timeout)
203 struct ldsem_waiter waiter;
204 long adjust = -LDSEM_ACTIVE_BIAS + LDSEM_WAIT_BIAS;
206 /* set up my own style of waitqueue */
207 raw_spin_lock_irq(&sem->wait_lock);
209 /* Try to reverse the lock attempt but if the count has changed
210 * so that reversing fails, check if there are are no waiters,
211 * and early-out if not */
213 if (ldsem_cmpxchg(&count, count + adjust, sem))
216 raw_spin_unlock_irq(&sem->wait_lock);
221 list_add_tail(&waiter.list, &sem->read_wait);
224 waiter.task = current;
225 get_task_struct(current);
227 /* if there are no active locks, wake the new lock owner(s) */
228 if ((count & LDSEM_ACTIVE_MASK) == 0)
231 raw_spin_unlock_irq(&sem->wait_lock);
233 /* wait to be given the lock */
235 set_current_state(TASK_UNINTERRUPTIBLE);
237 if (!smp_load_acquire(&waiter.task))
241 timeout = schedule_timeout(timeout);
244 __set_current_state(TASK_RUNNING);
247 /* lock timed out but check if this task was just
248 * granted lock ownership - if so, pretend there
249 * was no timeout; otherwise, cleanup lock wait */
250 raw_spin_lock_irq(&sem->wait_lock);
252 ldsem_atomic_update(-LDSEM_WAIT_BIAS, sem);
253 list_del(&waiter.list);
254 raw_spin_unlock_irq(&sem->wait_lock);
255 put_task_struct(waiter.task);
258 raw_spin_unlock_irq(&sem->wait_lock);
265 * wait for the write lock to be granted
267 static struct ld_semaphore __sched *
268 down_write_failed(struct ld_semaphore *sem, long count, long timeout)
270 struct ldsem_waiter waiter;
271 long adjust = -LDSEM_ACTIVE_BIAS;
274 /* set up my own style of waitqueue */
275 raw_spin_lock_irq(&sem->wait_lock);
277 /* Try to reverse the lock attempt but if the count has changed
278 * so that reversing fails, check if the lock is now owned,
279 * and early-out if so */
281 if (ldsem_cmpxchg(&count, count + adjust, sem))
283 if ((count & LDSEM_ACTIVE_MASK) == LDSEM_ACTIVE_BIAS) {
284 raw_spin_unlock_irq(&sem->wait_lock);
289 list_add_tail(&waiter.list, &sem->write_wait);
291 waiter.task = current;
293 set_current_state(TASK_UNINTERRUPTIBLE);
297 raw_spin_unlock_irq(&sem->wait_lock);
298 timeout = schedule_timeout(timeout);
299 raw_spin_lock_irq(&sem->wait_lock);
300 set_current_state(TASK_UNINTERRUPTIBLE);
301 locked = writer_trylock(sem);
307 ldsem_atomic_update(-LDSEM_WAIT_BIAS, sem);
308 list_del(&waiter.list);
311 * In case of timeout, wake up every reader who gave the right of way
312 * to writer. Prevent separation readers into two groups:
313 * one that helds semaphore and another that sleeps.
314 * (in case of no contention with a writer)
316 if (!locked && list_empty(&sem->write_wait))
317 __ldsem_wake_readers(sem);
319 raw_spin_unlock_irq(&sem->wait_lock);
321 __set_current_state(TASK_RUNNING);
323 /* lock wait may have timed out */
331 static int __ldsem_down_read_nested(struct ld_semaphore *sem,
332 int subclass, long timeout)
336 lockdep_acquire_read(sem, subclass, 0, _RET_IP_);
338 count = ldsem_atomic_update(LDSEM_READ_BIAS, sem);
340 lock_stat(sem, contended);
341 if (!down_read_failed(sem, count, timeout)) {
342 lockdep_release(sem, 1, _RET_IP_);
346 lock_stat(sem, acquired);
350 static int __ldsem_down_write_nested(struct ld_semaphore *sem,
351 int subclass, long timeout)
355 lockdep_acquire(sem, subclass, 0, _RET_IP_);
357 count = ldsem_atomic_update(LDSEM_WRITE_BIAS, sem);
358 if ((count & LDSEM_ACTIVE_MASK) != LDSEM_ACTIVE_BIAS) {
359 lock_stat(sem, contended);
360 if (!down_write_failed(sem, count, timeout)) {
361 lockdep_release(sem, 1, _RET_IP_);
365 lock_stat(sem, acquired);
371 * lock for reading -- returns 1 if successful, 0 if timed out
373 int __sched ldsem_down_read(struct ld_semaphore *sem, long timeout)
376 return __ldsem_down_read_nested(sem, 0, timeout);
380 * trylock for reading -- returns 1 if successful, 0 if contention
382 int ldsem_down_read_trylock(struct ld_semaphore *sem)
384 long count = sem->count;
387 if (ldsem_cmpxchg(&count, count + LDSEM_READ_BIAS, sem)) {
388 lockdep_acquire_read(sem, 0, 1, _RET_IP_);
389 lock_stat(sem, acquired);
397 * lock for writing -- returns 1 if successful, 0 if timed out
399 int __sched ldsem_down_write(struct ld_semaphore *sem, long timeout)
402 return __ldsem_down_write_nested(sem, 0, timeout);
406 * trylock for writing -- returns 1 if successful, 0 if contention
408 int ldsem_down_write_trylock(struct ld_semaphore *sem)
410 long count = sem->count;
412 while ((count & LDSEM_ACTIVE_MASK) == 0) {
413 if (ldsem_cmpxchg(&count, count + LDSEM_WRITE_BIAS, sem)) {
414 lockdep_acquire(sem, 0, 1, _RET_IP_);
415 lock_stat(sem, acquired);
423 * release a read lock
425 void ldsem_up_read(struct ld_semaphore *sem)
429 lockdep_release(sem, 1, _RET_IP_);
431 count = ldsem_atomic_update(-LDSEM_READ_BIAS, sem);
432 if (count < 0 && (count & LDSEM_ACTIVE_MASK) == 0)
437 * release a write lock
439 void ldsem_up_write(struct ld_semaphore *sem)
443 lockdep_release(sem, 1, _RET_IP_);
445 count = ldsem_atomic_update(-LDSEM_WRITE_BIAS, sem);
451 #ifdef CONFIG_DEBUG_LOCK_ALLOC
453 int ldsem_down_read_nested(struct ld_semaphore *sem, int subclass, long timeout)
456 return __ldsem_down_read_nested(sem, subclass, timeout);
459 int ldsem_down_write_nested(struct ld_semaphore *sem, int subclass,
463 return __ldsem_down_write_nested(sem, subclass, timeout);