Thomas Gleixner | 3e45610 | 2019-06-01 10:08:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2008 Intel Corporation |
| 4 | * Author: Matthew Wilcox <willy@linux.intel.com> |
| 5 | * |
Matthew Wilcox | 714493c | 2008-04-11 15:23:52 -0400 | [diff] [blame] | 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. |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * Some notes on the implementation: |
| 14 | * |
| 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() |
| 21 | * too. |
| 22 | * |
| 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. |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 25 | */ |
| 26 | |
| 27 | #include <linux/compiler.h> |
| 28 | #include <linux/kernel.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 29 | #include <linux/export.h> |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 30 | #include <linux/sched.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 31 | #include <linux/sched/debug.h> |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 32 | #include <linux/semaphore.h> |
| 33 | #include <linux/spinlock.h> |
Ingo Molnar | 74f4e36 | 2008-05-12 21:21:15 +0200 | [diff] [blame] | 34 | #include <linux/ftrace.h> |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 35 | |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 36 | static noinline void __down(struct semaphore *sem); |
| 37 | static noinline int __down_interruptible(struct semaphore *sem); |
Matthew Wilcox | f06d968 | 2008-03-14 13:19:33 -0400 | [diff] [blame] | 38 | static noinline int __down_killable(struct semaphore *sem); |
Mark Rustad | 3154276 | 2014-09-03 03:17:24 -0700 | [diff] [blame] | 39 | static noinline int __down_timeout(struct semaphore *sem, long timeout); |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 40 | static noinline void __up(struct semaphore *sem); |
| 41 | |
Matthew Wilcox | 714493c | 2008-04-11 15:23:52 -0400 | [diff] [blame] | 42 | /** |
| 43 | * down - acquire the semaphore |
| 44 | * @sem: the semaphore to be acquired |
| 45 | * |
| 46 | * Acquires the semaphore. If no more tasks are allowed to acquire the |
| 47 | * semaphore, calling this function will put the task to sleep until the |
| 48 | * semaphore is released. |
| 49 | * |
| 50 | * Use of this function is deprecated, please use down_interruptible() or |
| 51 | * down_killable() instead. |
| 52 | */ |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 53 | void down(struct semaphore *sem) |
| 54 | { |
| 55 | unsigned long flags; |
| 56 | |
Xiaoming Ni | 99409b9 | 2021-08-09 10:12:15 +0800 | [diff] [blame] | 57 | might_sleep(); |
Thomas Gleixner | 8292c9e1 | 2010-02-24 09:50:22 +0100 | [diff] [blame] | 58 | raw_spin_lock_irqsave(&sem->lock, flags); |
Linus Torvalds | 00b41ec | 2008-05-10 20:43:22 -0700 | [diff] [blame] | 59 | if (likely(sem->count > 0)) |
| 60 | sem->count--; |
| 61 | else |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 62 | __down(sem); |
Thomas Gleixner | 8292c9e1 | 2010-02-24 09:50:22 +0100 | [diff] [blame] | 63 | raw_spin_unlock_irqrestore(&sem->lock, flags); |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 64 | } |
| 65 | EXPORT_SYMBOL(down); |
| 66 | |
Matthew Wilcox | 714493c | 2008-04-11 15:23:52 -0400 | [diff] [blame] | 67 | /** |
| 68 | * down_interruptible - acquire the semaphore unless interrupted |
| 69 | * @sem: the semaphore to be acquired |
| 70 | * |
| 71 | * Attempts to acquire the semaphore. If no more tasks are allowed to |
| 72 | * acquire the semaphore, calling this function will put the task to sleep. |
| 73 | * If the sleep is interrupted by a signal, this function will return -EINTR. |
| 74 | * If the semaphore is successfully acquired, this function returns 0. |
| 75 | */ |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 76 | int down_interruptible(struct semaphore *sem) |
| 77 | { |
| 78 | unsigned long flags; |
| 79 | int result = 0; |
| 80 | |
Xiaoming Ni | 99409b9 | 2021-08-09 10:12:15 +0800 | [diff] [blame] | 81 | might_sleep(); |
Thomas Gleixner | 8292c9e1 | 2010-02-24 09:50:22 +0100 | [diff] [blame] | 82 | raw_spin_lock_irqsave(&sem->lock, flags); |
Linus Torvalds | 00b41ec | 2008-05-10 20:43:22 -0700 | [diff] [blame] | 83 | if (likely(sem->count > 0)) |
Ingo Molnar | bf726ea | 2008-05-08 11:53:48 +0200 | [diff] [blame] | 84 | sem->count--; |
Linus Torvalds | 00b41ec | 2008-05-10 20:43:22 -0700 | [diff] [blame] | 85 | else |
| 86 | result = __down_interruptible(sem); |
Thomas Gleixner | 8292c9e1 | 2010-02-24 09:50:22 +0100 | [diff] [blame] | 87 | raw_spin_unlock_irqrestore(&sem->lock, flags); |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 88 | |
| 89 | return result; |
| 90 | } |
| 91 | EXPORT_SYMBOL(down_interruptible); |
| 92 | |
Matthew Wilcox | 714493c | 2008-04-11 15:23:52 -0400 | [diff] [blame] | 93 | /** |
| 94 | * down_killable - acquire the semaphore unless killed |
| 95 | * @sem: the semaphore to be acquired |
| 96 | * |
| 97 | * Attempts to acquire the semaphore. If no more tasks are allowed to |
| 98 | * acquire the semaphore, calling this function will put the task to sleep. |
| 99 | * If the sleep is interrupted by a fatal signal, this function will return |
| 100 | * -EINTR. If the semaphore is successfully acquired, this function returns |
| 101 | * 0. |
| 102 | */ |
Matthew Wilcox | f06d968 | 2008-03-14 13:19:33 -0400 | [diff] [blame] | 103 | int down_killable(struct semaphore *sem) |
| 104 | { |
| 105 | unsigned long flags; |
| 106 | int result = 0; |
| 107 | |
Xiaoming Ni | 99409b9 | 2021-08-09 10:12:15 +0800 | [diff] [blame] | 108 | might_sleep(); |
Thomas Gleixner | 8292c9e1 | 2010-02-24 09:50:22 +0100 | [diff] [blame] | 109 | raw_spin_lock_irqsave(&sem->lock, flags); |
Linus Torvalds | 00b41ec | 2008-05-10 20:43:22 -0700 | [diff] [blame] | 110 | if (likely(sem->count > 0)) |
Ingo Molnar | bf726ea | 2008-05-08 11:53:48 +0200 | [diff] [blame] | 111 | sem->count--; |
Linus Torvalds | 00b41ec | 2008-05-10 20:43:22 -0700 | [diff] [blame] | 112 | else |
| 113 | result = __down_killable(sem); |
Thomas Gleixner | 8292c9e1 | 2010-02-24 09:50:22 +0100 | [diff] [blame] | 114 | raw_spin_unlock_irqrestore(&sem->lock, flags); |
Matthew Wilcox | f06d968 | 2008-03-14 13:19:33 -0400 | [diff] [blame] | 115 | |
| 116 | return result; |
| 117 | } |
| 118 | EXPORT_SYMBOL(down_killable); |
| 119 | |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 120 | /** |
| 121 | * down_trylock - try to acquire the semaphore, without waiting |
| 122 | * @sem: the semaphore to be acquired |
| 123 | * |
Lucia Rosculete | f7232f0 | 2012-03-03 16:18:47 +0200 | [diff] [blame] | 124 | * Try to acquire the semaphore atomically. Returns 0 if the semaphore has |
Randy Dunlap | c034f48 | 2021-02-25 17:21:10 -0800 | [diff] [blame] | 125 | * been acquired successfully or 1 if it cannot be acquired. |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 126 | * |
| 127 | * NOTE: This return value is inverted from both spin_trylock and |
| 128 | * mutex_trylock! Be careful about this when converting code. |
| 129 | * |
| 130 | * Unlike mutex_trylock, this function can be used from interrupt context, |
| 131 | * and the semaphore can be released by any task or interrupt. |
| 132 | */ |
| 133 | int down_trylock(struct semaphore *sem) |
| 134 | { |
| 135 | unsigned long flags; |
| 136 | int count; |
| 137 | |
Thomas Gleixner | 8292c9e1 | 2010-02-24 09:50:22 +0100 | [diff] [blame] | 138 | raw_spin_lock_irqsave(&sem->lock, flags); |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 139 | count = sem->count - 1; |
| 140 | if (likely(count >= 0)) |
| 141 | sem->count = count; |
Thomas Gleixner | 8292c9e1 | 2010-02-24 09:50:22 +0100 | [diff] [blame] | 142 | raw_spin_unlock_irqrestore(&sem->lock, flags); |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 143 | |
| 144 | return (count < 0); |
| 145 | } |
| 146 | EXPORT_SYMBOL(down_trylock); |
| 147 | |
Matthew Wilcox | 714493c | 2008-04-11 15:23:52 -0400 | [diff] [blame] | 148 | /** |
| 149 | * down_timeout - acquire the semaphore within a specified time |
| 150 | * @sem: the semaphore to be acquired |
Mark Rustad | 3154276 | 2014-09-03 03:17:24 -0700 | [diff] [blame] | 151 | * @timeout: how long to wait before failing |
Matthew Wilcox | 714493c | 2008-04-11 15:23:52 -0400 | [diff] [blame] | 152 | * |
| 153 | * Attempts to acquire the semaphore. If no more tasks are allowed to |
| 154 | * acquire the semaphore, calling this function will put the task to sleep. |
| 155 | * If the semaphore is not released within the specified number of jiffies, |
| 156 | * this function returns -ETIME. It returns 0 if the semaphore was acquired. |
| 157 | */ |
Mark Rustad | 3154276 | 2014-09-03 03:17:24 -0700 | [diff] [blame] | 158 | int down_timeout(struct semaphore *sem, long timeout) |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 159 | { |
| 160 | unsigned long flags; |
| 161 | int result = 0; |
| 162 | |
Xiaoming Ni | 99409b9 | 2021-08-09 10:12:15 +0800 | [diff] [blame] | 163 | might_sleep(); |
Thomas Gleixner | 8292c9e1 | 2010-02-24 09:50:22 +0100 | [diff] [blame] | 164 | raw_spin_lock_irqsave(&sem->lock, flags); |
Linus Torvalds | 00b41ec | 2008-05-10 20:43:22 -0700 | [diff] [blame] | 165 | if (likely(sem->count > 0)) |
Ingo Molnar | bf726ea | 2008-05-08 11:53:48 +0200 | [diff] [blame] | 166 | sem->count--; |
Linus Torvalds | 00b41ec | 2008-05-10 20:43:22 -0700 | [diff] [blame] | 167 | else |
Mark Rustad | 3154276 | 2014-09-03 03:17:24 -0700 | [diff] [blame] | 168 | result = __down_timeout(sem, timeout); |
Thomas Gleixner | 8292c9e1 | 2010-02-24 09:50:22 +0100 | [diff] [blame] | 169 | raw_spin_unlock_irqrestore(&sem->lock, flags); |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 170 | |
| 171 | return result; |
| 172 | } |
| 173 | EXPORT_SYMBOL(down_timeout); |
| 174 | |
Matthew Wilcox | 714493c | 2008-04-11 15:23:52 -0400 | [diff] [blame] | 175 | /** |
| 176 | * up - release the semaphore |
| 177 | * @sem: the semaphore to release |
| 178 | * |
| 179 | * Release the semaphore. Unlike mutexes, up() may be called from any |
| 180 | * context and even by tasks which have never called down(). |
| 181 | */ |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 182 | void up(struct semaphore *sem) |
| 183 | { |
| 184 | unsigned long flags; |
| 185 | |
Thomas Gleixner | 8292c9e1 | 2010-02-24 09:50:22 +0100 | [diff] [blame] | 186 | raw_spin_lock_irqsave(&sem->lock, flags); |
Linus Torvalds | 00b41ec | 2008-05-10 20:43:22 -0700 | [diff] [blame] | 187 | if (likely(list_empty(&sem->wait_list))) |
| 188 | sem->count++; |
| 189 | else |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 190 | __up(sem); |
Thomas Gleixner | 8292c9e1 | 2010-02-24 09:50:22 +0100 | [diff] [blame] | 191 | raw_spin_unlock_irqrestore(&sem->lock, flags); |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 192 | } |
| 193 | EXPORT_SYMBOL(up); |
| 194 | |
| 195 | /* Functions for the contended case */ |
| 196 | |
| 197 | struct semaphore_waiter { |
| 198 | struct list_head list; |
| 199 | struct task_struct *task; |
liguang | 06a6ea3 | 2013-04-30 15:28:33 -0700 | [diff] [blame] | 200 | bool up; |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | /* |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 204 | * Because this function is inlined, the 'state' parameter will be |
| 205 | * constant, and thus optimised away by the compiler. Likewise the |
| 206 | * 'timeout' parameter for the cases without timeouts. |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 207 | */ |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 208 | static inline int __sched __down_common(struct semaphore *sem, long state, |
| 209 | long timeout) |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 210 | { |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 211 | struct semaphore_waiter waiter; |
| 212 | |
Ingo Molnar | bf726ea | 2008-05-08 11:53:48 +0200 | [diff] [blame] | 213 | list_add_tail(&waiter.list, &sem->wait_list); |
Davidlohr Bueso | d269a8b | 2017-01-03 13:43:13 -0800 | [diff] [blame] | 214 | waiter.task = current; |
liguang | 06a6ea3 | 2013-04-30 15:28:33 -0700 | [diff] [blame] | 215 | waiter.up = false; |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 216 | |
| 217 | for (;;) { |
Davidlohr Bueso | d269a8b | 2017-01-03 13:43:13 -0800 | [diff] [blame] | 218 | if (signal_pending_state(state, current)) |
Linus Torvalds | 00b41ec | 2008-05-10 20:43:22 -0700 | [diff] [blame] | 219 | goto interrupted; |
liguang | c74f66c | 2013-04-30 15:28:32 -0700 | [diff] [blame] | 220 | if (unlikely(timeout <= 0)) |
Linus Torvalds | 00b41ec | 2008-05-10 20:43:22 -0700 | [diff] [blame] | 221 | goto timed_out; |
Davidlohr Bueso | 642fa44 | 2017-01-03 13:43:14 -0800 | [diff] [blame] | 222 | __set_current_state(state); |
Thomas Gleixner | 8292c9e1 | 2010-02-24 09:50:22 +0100 | [diff] [blame] | 223 | raw_spin_unlock_irq(&sem->lock); |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 224 | timeout = schedule_timeout(timeout); |
Thomas Gleixner | 8292c9e1 | 2010-02-24 09:50:22 +0100 | [diff] [blame] | 225 | raw_spin_lock_irq(&sem->lock); |
Linus Torvalds | 00b41ec | 2008-05-10 20:43:22 -0700 | [diff] [blame] | 226 | if (waiter.up) |
| 227 | return 0; |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 228 | } |
| 229 | |
Linus Torvalds | 00b41ec | 2008-05-10 20:43:22 -0700 | [diff] [blame] | 230 | timed_out: |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 231 | list_del(&waiter.list); |
Linus Torvalds | 00b41ec | 2008-05-10 20:43:22 -0700 | [diff] [blame] | 232 | return -ETIME; |
| 233 | |
| 234 | interrupted: |
| 235 | list_del(&waiter.list); |
| 236 | return -EINTR; |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static noinline void __sched __down(struct semaphore *sem) |
| 240 | { |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 241 | __down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static noinline int __sched __down_interruptible(struct semaphore *sem) |
| 245 | { |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 246 | return __down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 247 | } |
| 248 | |
Matthew Wilcox | f06d968 | 2008-03-14 13:19:33 -0400 | [diff] [blame] | 249 | static noinline int __sched __down_killable(struct semaphore *sem) |
| 250 | { |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 251 | return __down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT); |
| 252 | } |
| 253 | |
Mark Rustad | 3154276 | 2014-09-03 03:17:24 -0700 | [diff] [blame] | 254 | static noinline int __sched __down_timeout(struct semaphore *sem, long timeout) |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 255 | { |
Mark Rustad | 3154276 | 2014-09-03 03:17:24 -0700 | [diff] [blame] | 256 | return __down_common(sem, TASK_UNINTERRUPTIBLE, timeout); |
Matthew Wilcox | f06d968 | 2008-03-14 13:19:33 -0400 | [diff] [blame] | 257 | } |
| 258 | |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 259 | static noinline void __sched __up(struct semaphore *sem) |
| 260 | { |
Matthew Wilcox | b17170b | 2008-03-14 14:35:22 -0400 | [diff] [blame] | 261 | struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list, |
| 262 | struct semaphore_waiter, list); |
Linus Torvalds | 00b41ec | 2008-05-10 20:43:22 -0700 | [diff] [blame] | 263 | list_del(&waiter->list); |
liguang | 06a6ea3 | 2013-04-30 15:28:33 -0700 | [diff] [blame] | 264 | waiter->up = true; |
Matthew Wilcox | b17170b | 2008-03-14 14:35:22 -0400 | [diff] [blame] | 265 | wake_up_process(waiter->task); |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 266 | } |