1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2008 Intel Corporation
4 * Author: Matthew Wilcox <willy@linux.intel.com>
6 * This file implements counting semaphores.
7 * A counting semaphore may be acquired 'n' times before sleeping.
8 * See mutex.c for single-acquisition sleeping locks which enforce
9 * rules which allow code to be debugged more easily.
13 * Some notes on the implementation:
15 * The spinlock controls access to the other members of the semaphore.
16 * down_trylock() and up() can be called from interrupt context, so we
17 * have to disable interrupts when taking the lock. It turns out various
18 * parts of the kernel expect to be able to use down() on a semaphore in
19 * interrupt context when they know it will succeed, so we have to use
20 * irqsave variants for down(), down_interruptible() and down_killable()
23 * The ->count variable represents how many more tasks can acquire this
24 * semaphore. If it's zero, there may be tasks waiting on the wait_list.
27 #include <linux/compiler.h>
28 #include <linux/kernel.h>
29 #include <linux/export.h>
30 #include <linux/sched.h>
31 #include <linux/sched/debug.h>
32 #include <linux/semaphore.h>
33 #include <linux/spinlock.h>
34 #include <linux/ftrace.h>
35 #include <trace/events/lock.h>
37 static noinline void __down(struct semaphore *sem);
38 static noinline int __down_interruptible(struct semaphore *sem);
39 static noinline int __down_killable(struct semaphore *sem);
40 static noinline int __down_timeout(struct semaphore *sem, long timeout);
41 static noinline void __up(struct semaphore *sem);
44 * down - acquire the semaphore
45 * @sem: the semaphore to be acquired
47 * Acquires the semaphore. If no more tasks are allowed to acquire the
48 * semaphore, calling this function will put the task to sleep until the
49 * semaphore is released.
51 * Use of this function is deprecated, please use down_interruptible() or
52 * down_killable() instead.
54 void __sched down(struct semaphore *sem)
59 raw_spin_lock_irqsave(&sem->lock, flags);
60 if (likely(sem->count > 0))
64 raw_spin_unlock_irqrestore(&sem->lock, flags);
69 * down_interruptible - acquire the semaphore unless interrupted
70 * @sem: the semaphore to be acquired
72 * Attempts to acquire the semaphore. If no more tasks are allowed to
73 * acquire the semaphore, calling this function will put the task to sleep.
74 * If the sleep is interrupted by a signal, this function will return -EINTR.
75 * If the semaphore is successfully acquired, this function returns 0.
77 int __sched down_interruptible(struct semaphore *sem)
83 raw_spin_lock_irqsave(&sem->lock, flags);
84 if (likely(sem->count > 0))
87 result = __down_interruptible(sem);
88 raw_spin_unlock_irqrestore(&sem->lock, flags);
92 EXPORT_SYMBOL(down_interruptible);
95 * down_killable - acquire the semaphore unless killed
96 * @sem: the semaphore to be acquired
98 * Attempts to acquire the semaphore. If no more tasks are allowed to
99 * acquire the semaphore, calling this function will put the task to sleep.
100 * If the sleep is interrupted by a fatal signal, this function will return
101 * -EINTR. If the semaphore is successfully acquired, this function returns
104 int __sched down_killable(struct semaphore *sem)
110 raw_spin_lock_irqsave(&sem->lock, flags);
111 if (likely(sem->count > 0))
114 result = __down_killable(sem);
115 raw_spin_unlock_irqrestore(&sem->lock, flags);
119 EXPORT_SYMBOL(down_killable);
122 * down_trylock - try to acquire the semaphore, without waiting
123 * @sem: the semaphore to be acquired
125 * Try to acquire the semaphore atomically. Returns 0 if the semaphore has
126 * been acquired successfully or 1 if it cannot be acquired.
128 * NOTE: This return value is inverted from both spin_trylock and
129 * mutex_trylock! Be careful about this when converting code.
131 * Unlike mutex_trylock, this function can be used from interrupt context,
132 * and the semaphore can be released by any task or interrupt.
134 int __sched down_trylock(struct semaphore *sem)
139 raw_spin_lock_irqsave(&sem->lock, flags);
140 count = sem->count - 1;
141 if (likely(count >= 0))
143 raw_spin_unlock_irqrestore(&sem->lock, flags);
147 EXPORT_SYMBOL(down_trylock);
150 * down_timeout - acquire the semaphore within a specified time
151 * @sem: the semaphore to be acquired
152 * @timeout: how long to wait before failing
154 * Attempts to acquire the semaphore. If no more tasks are allowed to
155 * acquire the semaphore, calling this function will put the task to sleep.
156 * If the semaphore is not released within the specified number of jiffies,
157 * this function returns -ETIME. It returns 0 if the semaphore was acquired.
159 int __sched down_timeout(struct semaphore *sem, long timeout)
165 raw_spin_lock_irqsave(&sem->lock, flags);
166 if (likely(sem->count > 0))
169 result = __down_timeout(sem, timeout);
170 raw_spin_unlock_irqrestore(&sem->lock, flags);
174 EXPORT_SYMBOL(down_timeout);
177 * up - release the semaphore
178 * @sem: the semaphore to release
180 * Release the semaphore. Unlike mutexes, up() may be called from any
181 * context and even by tasks which have never called down().
183 void __sched up(struct semaphore *sem)
187 raw_spin_lock_irqsave(&sem->lock, flags);
188 if (likely(list_empty(&sem->wait_list)))
192 raw_spin_unlock_irqrestore(&sem->lock, flags);
196 /* Functions for the contended case */
198 struct semaphore_waiter {
199 struct list_head list;
200 struct task_struct *task;
205 * Because this function is inlined, the 'state' parameter will be
206 * constant, and thus optimised away by the compiler. Likewise the
207 * 'timeout' parameter for the cases without timeouts.
209 static inline int __sched ___down_common(struct semaphore *sem, long state,
212 struct semaphore_waiter waiter;
214 list_add_tail(&waiter.list, &sem->wait_list);
215 waiter.task = current;
219 if (signal_pending_state(state, current))
221 if (unlikely(timeout <= 0))
223 __set_current_state(state);
224 raw_spin_unlock_irq(&sem->lock);
225 timeout = schedule_timeout(timeout);
226 raw_spin_lock_irq(&sem->lock);
232 list_del(&waiter.list);
236 list_del(&waiter.list);
240 static inline int __sched __down_common(struct semaphore *sem, long state,
245 trace_contention_begin(sem, 0);
246 ret = ___down_common(sem, state, timeout);
247 trace_contention_end(sem, ret);
252 static noinline void __sched __down(struct semaphore *sem)
254 __down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
257 static noinline int __sched __down_interruptible(struct semaphore *sem)
259 return __down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
262 static noinline int __sched __down_killable(struct semaphore *sem)
264 return __down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT);
267 static noinline int __sched __down_timeout(struct semaphore *sem, long timeout)
269 return __down_common(sem, TASK_UNINTERRUPTIBLE, timeout);
272 static noinline void __sched __up(struct semaphore *sem)
274 struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list,
275 struct semaphore_waiter, list);
276 list_del(&waiter->list);
278 wake_up_process(waiter->task);