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
;
78 * Initialize an ldsem:
80 void __init_ldsem(struct ld_semaphore
*sem
, const char *name
,
81 struct lock_class_key
*key
)
83 #ifdef CONFIG_DEBUG_LOCK_ALLOC
85 * Make sure we are not reinitializing a held semaphore:
87 debug_check_no_locks_freed((void *)sem
, sizeof(*sem
));
88 lockdep_init_map(&sem
->dep_map
, name
, key
, 0);
90 atomic_long_set(&sem
->count
, LDSEM_UNLOCKED
);
91 sem
->wait_readers
= 0;
92 raw_spin_lock_init(&sem
->wait_lock
);
93 INIT_LIST_HEAD(&sem
->read_wait
);
94 INIT_LIST_HEAD(&sem
->write_wait
);
97 static void __ldsem_wake_readers(struct ld_semaphore
*sem
)
99 struct ldsem_waiter
*waiter
, *next
;
100 struct task_struct
*tsk
;
104 * Try to grant read locks to all readers on the read wait list.
105 * Note the 'active part' of the count is incremented by
106 * the number of readers before waking any processes up.
108 adjust
= sem
->wait_readers
* (LDSEM_ACTIVE_BIAS
- LDSEM_WAIT_BIAS
);
109 count
= atomic_long_add_return(adjust
, &sem
->count
);
113 if (atomic_long_try_cmpxchg(&sem
->count
, &count
, count
- adjust
))
117 list_for_each_entry_safe(waiter
, next
, &sem
->read_wait
, list
) {
121 wake_up_process(tsk
);
122 put_task_struct(tsk
);
124 INIT_LIST_HEAD(&sem
->read_wait
);
125 sem
->wait_readers
= 0;
128 static inline int writer_trylock(struct ld_semaphore
*sem
)
131 * Only wake this writer if the active part of the count can be
132 * transitioned from 0 -> 1
134 long count
= atomic_long_add_return(LDSEM_ACTIVE_BIAS
, &sem
->count
);
136 if ((count
& LDSEM_ACTIVE_MASK
) == LDSEM_ACTIVE_BIAS
)
138 if (atomic_long_try_cmpxchg(&sem
->count
, &count
, count
- LDSEM_ACTIVE_BIAS
))
143 static void __ldsem_wake_writer(struct ld_semaphore
*sem
)
145 struct ldsem_waiter
*waiter
;
147 waiter
= list_entry(sem
->write_wait
.next
, struct ldsem_waiter
, list
);
148 wake_up_process(waiter
->task
);
152 * handle the lock release when processes blocked on it that can now run
153 * - if we come here from up_xxxx(), then:
154 * - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed)
155 * - the 'waiting part' of count (&0xffff0000) is -ve (and will still be so)
156 * - the spinlock must be held by the caller
157 * - woken process blocks are discarded from the list after having task zeroed
159 static void __ldsem_wake(struct ld_semaphore
*sem
)
161 if (!list_empty(&sem
->write_wait
))
162 __ldsem_wake_writer(sem
);
163 else if (!list_empty(&sem
->read_wait
))
164 __ldsem_wake_readers(sem
);
167 static void ldsem_wake(struct ld_semaphore
*sem
)
171 raw_spin_lock_irqsave(&sem
->wait_lock
, flags
);
173 raw_spin_unlock_irqrestore(&sem
->wait_lock
, flags
);
177 * wait for the read lock to be granted
179 static struct ld_semaphore __sched
*
180 down_read_failed(struct ld_semaphore
*sem
, long count
, long timeout
)
182 struct ldsem_waiter waiter
;
183 long adjust
= -LDSEM_ACTIVE_BIAS
+ LDSEM_WAIT_BIAS
;
185 /* set up my own style of waitqueue */
186 raw_spin_lock_irq(&sem
->wait_lock
);
189 * Try to reverse the lock attempt but if the count has changed
190 * so that reversing fails, check if there are are no waiters,
191 * and early-out if not
194 if (atomic_long_try_cmpxchg(&sem
->count
, &count
, count
+ adjust
)) {
199 raw_spin_unlock_irq(&sem
->wait_lock
);
204 list_add_tail(&waiter
.list
, &sem
->read_wait
);
207 waiter
.task
= current
;
208 get_task_struct(current
);
210 /* if there are no active locks, wake the new lock owner(s) */
211 if ((count
& LDSEM_ACTIVE_MASK
) == 0)
214 raw_spin_unlock_irq(&sem
->wait_lock
);
216 /* wait to be given the lock */
218 set_current_state(TASK_UNINTERRUPTIBLE
);
224 timeout
= schedule_timeout(timeout
);
227 __set_current_state(TASK_RUNNING
);
231 * Lock timed out but check if this task was just
232 * granted lock ownership - if so, pretend there
233 * was no timeout; otherwise, cleanup lock wait.
235 raw_spin_lock_irq(&sem
->wait_lock
);
237 atomic_long_add_return(-LDSEM_WAIT_BIAS
, &sem
->count
);
238 list_del(&waiter
.list
);
239 raw_spin_unlock_irq(&sem
->wait_lock
);
240 put_task_struct(waiter
.task
);
243 raw_spin_unlock_irq(&sem
->wait_lock
);
250 * wait for the write lock to be granted
252 static struct ld_semaphore __sched
*
253 down_write_failed(struct ld_semaphore
*sem
, long count
, long timeout
)
255 struct ldsem_waiter waiter
;
256 long adjust
= -LDSEM_ACTIVE_BIAS
;
259 /* set up my own style of waitqueue */
260 raw_spin_lock_irq(&sem
->wait_lock
);
263 * Try to reverse the lock attempt but if the count has changed
264 * so that reversing fails, check if the lock is now owned,
265 * and early-out if so.
268 if (atomic_long_try_cmpxchg(&sem
->count
, &count
, count
+ adjust
))
270 if ((count
& LDSEM_ACTIVE_MASK
) == LDSEM_ACTIVE_BIAS
) {
271 raw_spin_unlock_irq(&sem
->wait_lock
);
276 list_add_tail(&waiter
.list
, &sem
->write_wait
);
278 waiter
.task
= current
;
280 set_current_state(TASK_UNINTERRUPTIBLE
);
284 raw_spin_unlock_irq(&sem
->wait_lock
);
285 timeout
= schedule_timeout(timeout
);
286 raw_spin_lock_irq(&sem
->wait_lock
);
287 set_current_state(TASK_UNINTERRUPTIBLE
);
288 locked
= writer_trylock(sem
);
294 atomic_long_add_return(-LDSEM_WAIT_BIAS
, &sem
->count
);
295 list_del(&waiter
.list
);
298 * In case of timeout, wake up every reader who gave the right of way
299 * to writer. Prevent separation readers into two groups:
300 * one that helds semaphore and another that sleeps.
301 * (in case of no contention with a writer)
303 if (!locked
&& list_empty(&sem
->write_wait
))
304 __ldsem_wake_readers(sem
);
306 raw_spin_unlock_irq(&sem
->wait_lock
);
308 __set_current_state(TASK_RUNNING
);
310 /* lock wait may have timed out */
318 static int __ldsem_down_read_nested(struct ld_semaphore
*sem
,
319 int subclass
, long timeout
)
323 lockdep_acquire_read(sem
, subclass
, 0, _RET_IP_
);
325 count
= atomic_long_add_return(LDSEM_READ_BIAS
, &sem
->count
);
327 lock_stat(sem
, contended
);
328 if (!down_read_failed(sem
, count
, timeout
)) {
329 lockdep_release(sem
, 1, _RET_IP_
);
333 lock_stat(sem
, acquired
);
337 static int __ldsem_down_write_nested(struct ld_semaphore
*sem
,
338 int subclass
, long timeout
)
342 lockdep_acquire(sem
, subclass
, 0, _RET_IP_
);
344 count
= atomic_long_add_return(LDSEM_WRITE_BIAS
, &sem
->count
);
345 if ((count
& LDSEM_ACTIVE_MASK
) != LDSEM_ACTIVE_BIAS
) {
346 lock_stat(sem
, contended
);
347 if (!down_write_failed(sem
, count
, timeout
)) {
348 lockdep_release(sem
, 1, _RET_IP_
);
352 lock_stat(sem
, acquired
);
358 * lock for reading -- returns 1 if successful, 0 if timed out
360 int __sched
ldsem_down_read(struct ld_semaphore
*sem
, long timeout
)
363 return __ldsem_down_read_nested(sem
, 0, timeout
);
367 * trylock for reading -- returns 1 if successful, 0 if contention
369 int ldsem_down_read_trylock(struct ld_semaphore
*sem
)
371 long count
= atomic_long_read(&sem
->count
);
374 if (atomic_long_try_cmpxchg(&sem
->count
, &count
, count
+ LDSEM_READ_BIAS
)) {
375 lockdep_acquire_read(sem
, 0, 1, _RET_IP_
);
376 lock_stat(sem
, acquired
);
384 * lock for writing -- returns 1 if successful, 0 if timed out
386 int __sched
ldsem_down_write(struct ld_semaphore
*sem
, long timeout
)
389 return __ldsem_down_write_nested(sem
, 0, timeout
);
393 * trylock for writing -- returns 1 if successful, 0 if contention
395 int ldsem_down_write_trylock(struct ld_semaphore
*sem
)
397 long count
= atomic_long_read(&sem
->count
);
399 while ((count
& LDSEM_ACTIVE_MASK
) == 0) {
400 if (atomic_long_try_cmpxchg(&sem
->count
, &count
, count
+ LDSEM_WRITE_BIAS
)) {
401 lockdep_acquire(sem
, 0, 1, _RET_IP_
);
402 lock_stat(sem
, acquired
);
410 * release a read lock
412 void ldsem_up_read(struct ld_semaphore
*sem
)
416 lockdep_release(sem
, 1, _RET_IP_
);
418 count
= atomic_long_add_return(-LDSEM_READ_BIAS
, &sem
->count
);
419 if (count
< 0 && (count
& LDSEM_ACTIVE_MASK
) == 0)
424 * release a write lock
426 void ldsem_up_write(struct ld_semaphore
*sem
)
430 lockdep_release(sem
, 1, _RET_IP_
);
432 count
= atomic_long_add_return(-LDSEM_WRITE_BIAS
, &sem
->count
);
438 #ifdef CONFIG_DEBUG_LOCK_ALLOC
440 int ldsem_down_read_nested(struct ld_semaphore
*sem
, int subclass
, long timeout
)
443 return __ldsem_down_read_nested(sem
, subclass
, timeout
);
446 int ldsem_down_write_nested(struct ld_semaphore
*sem
, int subclass
,
450 return __ldsem_down_write_nested(sem
, subclass
, timeout
);