1 // SPDX-License-Identifier: GPL-2.0
5 * The ldisc semaphore is semantically a rw_semaphore but which enforces
6 * an alternate policy, namely:
7 * 1) Supports lock wait timeouts
8 * 2) Write waiter has priority
9 * 3) Downgrading is not supported
11 * Implementation notes:
12 * 1) Upper half of semaphore count is a wait count (differs from rwsem
13 * in that rwsem normalizes the upper half to the wait bias)
14 * 2) Lacks overflow checking
16 * The generic counting was copied and modified from include/asm-generic/rwsem.h
17 * by Paul Mackerras <paulus@samba.org>.
19 * The scheduling policy was copied and modified from lib/rwsem.c
20 * Written by David Howells (dhowells@redhat.com).
22 * This implementation incorporates the write lock stealing work of
23 * Michel Lespinasse <walken@google.com>.
25 * Copyright (C) 2013 Peter Hurley <peter@hurleysoftware.com>
28 #include <linux/list.h>
29 #include <linux/spinlock.h>
30 #include <linux/atomic.h>
31 #include <linux/tty.h>
32 #include <linux/sched.h>
33 #include <linux/sched/debug.h>
34 #include <linux/sched/task.h>
37 #ifdef CONFIG_DEBUG_LOCK_ALLOC
38 # define __acq(l, s, t, r, c, n, i) \
39 lock_acquire(&(l)->dep_map, s, t, r, c, n, i)
40 # define __rel(l, n, i) \
41 lock_release(&(l)->dep_map, n, i)
42 #define lockdep_acquire(l, s, t, i) __acq(l, s, t, 0, 1, NULL, i)
43 #define lockdep_acquire_nest(l, s, t, n, i) __acq(l, s, t, 0, 1, n, i)
44 #define lockdep_acquire_read(l, s, t, i) __acq(l, s, t, 1, 1, NULL, i)
45 #define lockdep_release(l, n, i) __rel(l, n, i)
47 # define lockdep_acquire(l, s, t, i) do { } while (0)
48 # define lockdep_acquire_nest(l, s, t, n, i) do { } while (0)
49 # define lockdep_acquire_read(l, s, t, i) do { } while (0)
50 # define lockdep_release(l, n, i) do { } while (0)
53 #ifdef CONFIG_LOCK_STAT
54 # define lock_stat(_lock, stat) lock_##stat(&(_lock)->dep_map, _RET_IP_)
56 # define lock_stat(_lock, stat) do { } while (0)
60 #if BITS_PER_LONG == 64
61 # define LDSEM_ACTIVE_MASK 0xffffffffL
63 # define LDSEM_ACTIVE_MASK 0x0000ffffL
66 #define LDSEM_UNLOCKED 0L
67 #define LDSEM_ACTIVE_BIAS 1L
68 #define LDSEM_WAIT_BIAS (-LDSEM_ACTIVE_MASK-1)
69 #define LDSEM_READ_BIAS LDSEM_ACTIVE_BIAS
70 #define LDSEM_WRITE_BIAS (LDSEM_WAIT_BIAS + LDSEM_ACTIVE_BIAS)
73 struct list_head list
;
74 struct task_struct
*task
;
77 static inline long ldsem_atomic_update(long delta
, struct ld_semaphore
*sem
)
79 return atomic_long_add_return(delta
, (atomic_long_t
*)&sem
->count
);
83 * ldsem_cmpxchg() updates @*old with the last-known sem->count value.
84 * Returns 1 if count was successfully changed; @*old will have @new value.
85 * Returns 0 if count was not changed; @*old will have most recent sem->count
87 static inline int ldsem_cmpxchg(long *old
, long new, struct ld_semaphore
*sem
)
89 long tmp
= atomic_long_cmpxchg(&sem
->count
, *old
, new);
100 * Initialize an ldsem:
102 void __init_ldsem(struct ld_semaphore
*sem
, const char *name
,
103 struct lock_class_key
*key
)
105 #ifdef CONFIG_DEBUG_LOCK_ALLOC
107 * Make sure we are not reinitializing a held semaphore:
109 debug_check_no_locks_freed((void *)sem
, sizeof(*sem
));
110 lockdep_init_map(&sem
->dep_map
, name
, key
, 0);
112 sem
->count
= LDSEM_UNLOCKED
;
113 sem
->wait_readers
= 0;
114 raw_spin_lock_init(&sem
->wait_lock
);
115 INIT_LIST_HEAD(&sem
->read_wait
);
116 INIT_LIST_HEAD(&sem
->write_wait
);
119 static void __ldsem_wake_readers(struct ld_semaphore
*sem
)
121 struct ldsem_waiter
*waiter
, *next
;
122 struct task_struct
*tsk
;
125 /* Try to grant read locks to all readers on the read wait list.
126 * Note the 'active part' of the count is incremented by
127 * the number of readers before waking any processes up.
129 adjust
= sem
->wait_readers
* (LDSEM_ACTIVE_BIAS
- LDSEM_WAIT_BIAS
);
130 count
= ldsem_atomic_update(adjust
, sem
);
134 if (ldsem_cmpxchg(&count
, count
- adjust
, sem
))
138 list_for_each_entry_safe(waiter
, next
, &sem
->read_wait
, list
) {
142 wake_up_process(tsk
);
143 put_task_struct(tsk
);
145 INIT_LIST_HEAD(&sem
->read_wait
);
146 sem
->wait_readers
= 0;
149 static inline int writer_trylock(struct ld_semaphore
*sem
)
151 /* only wake this writer if the active part of the count can be
152 * transitioned from 0 -> 1
154 long count
= ldsem_atomic_update(LDSEM_ACTIVE_BIAS
, sem
);
156 if ((count
& LDSEM_ACTIVE_MASK
) == LDSEM_ACTIVE_BIAS
)
158 if (ldsem_cmpxchg(&count
, count
- LDSEM_ACTIVE_BIAS
, sem
))
163 static void __ldsem_wake_writer(struct ld_semaphore
*sem
)
165 struct ldsem_waiter
*waiter
;
167 waiter
= list_entry(sem
->write_wait
.next
, struct ldsem_waiter
, list
);
168 wake_up_process(waiter
->task
);
172 * handle the lock release when processes blocked on it that can now run
173 * - if we come here from up_xxxx(), then:
174 * - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed)
175 * - the 'waiting part' of count (&0xffff0000) is -ve (and will still be so)
176 * - the spinlock must be held by the caller
177 * - woken process blocks are discarded from the list after having task zeroed
179 static void __ldsem_wake(struct ld_semaphore
*sem
)
181 if (!list_empty(&sem
->write_wait
))
182 __ldsem_wake_writer(sem
);
183 else if (!list_empty(&sem
->read_wait
))
184 __ldsem_wake_readers(sem
);
187 static void ldsem_wake(struct ld_semaphore
*sem
)
191 raw_spin_lock_irqsave(&sem
->wait_lock
, flags
);
193 raw_spin_unlock_irqrestore(&sem
->wait_lock
, flags
);
197 * wait for the read lock to be granted
199 static struct ld_semaphore __sched
*
200 down_read_failed(struct ld_semaphore
*sem
, long count
, long timeout
)
202 struct ldsem_waiter waiter
;
203 long adjust
= -LDSEM_ACTIVE_BIAS
+ LDSEM_WAIT_BIAS
;
205 /* set up my own style of waitqueue */
206 raw_spin_lock_irq(&sem
->wait_lock
);
208 /* Try to reverse the lock attempt but if the count has changed
209 * so that reversing fails, check if there are are no waiters,
210 * and early-out if not */
212 if (ldsem_cmpxchg(&count
, count
+ adjust
, sem
))
215 raw_spin_unlock_irq(&sem
->wait_lock
);
220 list_add_tail(&waiter
.list
, &sem
->read_wait
);
223 waiter
.task
= current
;
224 get_task_struct(current
);
226 /* if there are no active locks, wake the new lock owner(s) */
227 if ((count
& LDSEM_ACTIVE_MASK
) == 0)
230 raw_spin_unlock_irq(&sem
->wait_lock
);
232 /* wait to be given the lock */
234 set_current_state(TASK_UNINTERRUPTIBLE
);
240 timeout
= schedule_timeout(timeout
);
243 __set_current_state(TASK_RUNNING
);
246 /* lock timed out but check if this task was just
247 * granted lock ownership - if so, pretend there
248 * was no timeout; otherwise, cleanup lock wait */
249 raw_spin_lock_irq(&sem
->wait_lock
);
251 ldsem_atomic_update(-LDSEM_WAIT_BIAS
, sem
);
252 list_del(&waiter
.list
);
253 raw_spin_unlock_irq(&sem
->wait_lock
);
254 put_task_struct(waiter
.task
);
257 raw_spin_unlock_irq(&sem
->wait_lock
);
264 * wait for the write lock to be granted
266 static struct ld_semaphore __sched
*
267 down_write_failed(struct ld_semaphore
*sem
, long count
, long timeout
)
269 struct ldsem_waiter waiter
;
270 long adjust
= -LDSEM_ACTIVE_BIAS
;
273 /* set up my own style of waitqueue */
274 raw_spin_lock_irq(&sem
->wait_lock
);
276 /* Try to reverse the lock attempt but if the count has changed
277 * so that reversing fails, check if the lock is now owned,
278 * and early-out if so */
280 if (ldsem_cmpxchg(&count
, count
+ adjust
, sem
))
282 if ((count
& LDSEM_ACTIVE_MASK
) == LDSEM_ACTIVE_BIAS
) {
283 raw_spin_unlock_irq(&sem
->wait_lock
);
288 list_add_tail(&waiter
.list
, &sem
->write_wait
);
290 waiter
.task
= current
;
292 set_current_state(TASK_UNINTERRUPTIBLE
);
296 raw_spin_unlock_irq(&sem
->wait_lock
);
297 timeout
= schedule_timeout(timeout
);
298 raw_spin_lock_irq(&sem
->wait_lock
);
299 set_current_state(TASK_UNINTERRUPTIBLE
);
300 locked
= writer_trylock(sem
);
306 ldsem_atomic_update(-LDSEM_WAIT_BIAS
, sem
);
307 list_del(&waiter
.list
);
308 raw_spin_unlock_irq(&sem
->wait_lock
);
310 __set_current_state(TASK_RUNNING
);
312 /* lock wait may have timed out */
320 static int __ldsem_down_read_nested(struct ld_semaphore
*sem
,
321 int subclass
, long timeout
)
325 lockdep_acquire_read(sem
, subclass
, 0, _RET_IP_
);
327 count
= ldsem_atomic_update(LDSEM_READ_BIAS
, sem
);
329 lock_stat(sem
, contended
);
330 if (!down_read_failed(sem
, count
, timeout
)) {
331 lockdep_release(sem
, 1, _RET_IP_
);
335 lock_stat(sem
, acquired
);
339 static int __ldsem_down_write_nested(struct ld_semaphore
*sem
,
340 int subclass
, long timeout
)
344 lockdep_acquire(sem
, subclass
, 0, _RET_IP_
);
346 count
= ldsem_atomic_update(LDSEM_WRITE_BIAS
, sem
);
347 if ((count
& LDSEM_ACTIVE_MASK
) != LDSEM_ACTIVE_BIAS
) {
348 lock_stat(sem
, contended
);
349 if (!down_write_failed(sem
, count
, timeout
)) {
350 lockdep_release(sem
, 1, _RET_IP_
);
354 lock_stat(sem
, acquired
);
360 * lock for reading -- returns 1 if successful, 0 if timed out
362 int __sched
ldsem_down_read(struct ld_semaphore
*sem
, long timeout
)
365 return __ldsem_down_read_nested(sem
, 0, timeout
);
369 * trylock for reading -- returns 1 if successful, 0 if contention
371 int ldsem_down_read_trylock(struct ld_semaphore
*sem
)
373 long count
= sem
->count
;
376 if (ldsem_cmpxchg(&count
, count
+ LDSEM_READ_BIAS
, sem
)) {
377 lockdep_acquire_read(sem
, 0, 1, _RET_IP_
);
378 lock_stat(sem
, acquired
);
386 * lock for writing -- returns 1 if successful, 0 if timed out
388 int __sched
ldsem_down_write(struct ld_semaphore
*sem
, long timeout
)
391 return __ldsem_down_write_nested(sem
, 0, timeout
);
395 * trylock for writing -- returns 1 if successful, 0 if contention
397 int ldsem_down_write_trylock(struct ld_semaphore
*sem
)
399 long count
= sem
->count
;
401 while ((count
& LDSEM_ACTIVE_MASK
) == 0) {
402 if (ldsem_cmpxchg(&count
, count
+ LDSEM_WRITE_BIAS
, sem
)) {
403 lockdep_acquire(sem
, 0, 1, _RET_IP_
);
404 lock_stat(sem
, acquired
);
412 * release a read lock
414 void ldsem_up_read(struct ld_semaphore
*sem
)
418 lockdep_release(sem
, 1, _RET_IP_
);
420 count
= ldsem_atomic_update(-LDSEM_READ_BIAS
, sem
);
421 if (count
< 0 && (count
& LDSEM_ACTIVE_MASK
) == 0)
426 * release a write lock
428 void ldsem_up_write(struct ld_semaphore
*sem
)
432 lockdep_release(sem
, 1, _RET_IP_
);
434 count
= ldsem_atomic_update(-LDSEM_WRITE_BIAS
, sem
);
440 #ifdef CONFIG_DEBUG_LOCK_ALLOC
442 int ldsem_down_read_nested(struct ld_semaphore
*sem
, int subclass
, long timeout
)
445 return __ldsem_down_read_nested(sem
, subclass
, timeout
);
448 int ldsem_down_write_nested(struct ld_semaphore
*sem
, int subclass
,
452 return __ldsem_down_write_nested(sem
, subclass
, timeout
);