semaphore.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2008 Intel Corporation
  4. * Author: Matthew Wilcox <willy@linux.intel.com>
  5. *
  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. * Some notes on the implementation:
  13. *
  14. * The spinlock controls access to the other members of the semaphore.
  15. * down_trylock() and up() can be called from interrupt context, so we
  16. * have to disable interrupts when taking the lock. It turns out various
  17. * parts of the kernel expect to be able to use down() on a semaphore in
  18. * interrupt context when they know it will succeed, so we have to use
  19. * irqsave variants for down(), down_interruptible() and down_killable()
  20. * too.
  21. *
  22. * The ->count variable represents how many more tasks can acquire this
  23. * semaphore. If it's zero, there may be tasks waiting on the wait_list.
  24. */
  25. #include <linux/compiler.h>
  26. #include <linux/kernel.h>
  27. #include <linux/export.h>
  28. #include <linux/sched.h>
  29. #include <linux/sched/debug.h>
  30. #include <linux/sched/wake_q.h>
  31. #include <linux/semaphore.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/ftrace.h>
  34. #include <trace/events/lock.h>
  35. static noinline void __down(struct semaphore *sem);
  36. static noinline int __down_interruptible(struct semaphore *sem);
  37. static noinline int __down_killable(struct semaphore *sem);
  38. static noinline int __down_timeout(struct semaphore *sem, long timeout);
  39. static noinline void __up(struct semaphore *sem, struct wake_q_head *wake_q);
  40. /**
  41. * down - acquire the semaphore
  42. * @sem: the semaphore to be acquired
  43. *
  44. * Acquires the semaphore. If no more tasks are allowed to acquire the
  45. * semaphore, calling this function will put the task to sleep until the
  46. * semaphore is released.
  47. *
  48. * Use of this function is deprecated, please use down_interruptible() or
  49. * down_killable() instead.
  50. */
  51. void __sched down(struct semaphore *sem)
  52. {
  53. unsigned long flags;
  54. might_sleep();
  55. raw_spin_lock_irqsave(&sem->lock, flags);
  56. if (likely(sem->count > 0))
  57. sem->count--;
  58. else
  59. __down(sem);
  60. raw_spin_unlock_irqrestore(&sem->lock, flags);
  61. }
  62. EXPORT_SYMBOL(down);
  63. /**
  64. * down_interruptible - acquire the semaphore unless interrupted
  65. * @sem: the semaphore to be acquired
  66. *
  67. * Attempts to acquire the semaphore. If no more tasks are allowed to
  68. * acquire the semaphore, calling this function will put the task to sleep.
  69. * If the sleep is interrupted by a signal, this function will return -EINTR.
  70. * If the semaphore is successfully acquired, this function returns 0.
  71. */
  72. int __sched down_interruptible(struct semaphore *sem)
  73. {
  74. unsigned long flags;
  75. int result = 0;
  76. might_sleep();
  77. raw_spin_lock_irqsave(&sem->lock, flags);
  78. if (likely(sem->count > 0))
  79. sem->count--;
  80. else
  81. result = __down_interruptible(sem);
  82. raw_spin_unlock_irqrestore(&sem->lock, flags);
  83. return result;
  84. }
  85. EXPORT_SYMBOL(down_interruptible);
  86. /**
  87. * down_killable - acquire the semaphore unless killed
  88. * @sem: the semaphore to be acquired
  89. *
  90. * Attempts to acquire the semaphore. If no more tasks are allowed to
  91. * acquire the semaphore, calling this function will put the task to sleep.
  92. * If the sleep is interrupted by a fatal signal, this function will return
  93. * -EINTR. If the semaphore is successfully acquired, this function returns
  94. * 0.
  95. */
  96. int __sched down_killable(struct semaphore *sem)
  97. {
  98. unsigned long flags;
  99. int result = 0;
  100. might_sleep();
  101. raw_spin_lock_irqsave(&sem->lock, flags);
  102. if (likely(sem->count > 0))
  103. sem->count--;
  104. else
  105. result = __down_killable(sem);
  106. raw_spin_unlock_irqrestore(&sem->lock, flags);
  107. return result;
  108. }
  109. EXPORT_SYMBOL(down_killable);
  110. /**
  111. * down_trylock - try to acquire the semaphore, without waiting
  112. * @sem: the semaphore to be acquired
  113. *
  114. * Try to acquire the semaphore atomically. Returns 0 if the semaphore has
  115. * been acquired successfully or 1 if it cannot be acquired.
  116. *
  117. * NOTE: This return value is inverted from both spin_trylock and
  118. * mutex_trylock! Be careful about this when converting code.
  119. *
  120. * Unlike mutex_trylock, this function can be used from interrupt context,
  121. * and the semaphore can be released by any task or interrupt.
  122. */
  123. int __sched down_trylock(struct semaphore *sem)
  124. {
  125. unsigned long flags;
  126. int count;
  127. raw_spin_lock_irqsave(&sem->lock, flags);
  128. count = sem->count - 1;
  129. if (likely(count >= 0))
  130. sem->count = count;
  131. raw_spin_unlock_irqrestore(&sem->lock, flags);
  132. return (count < 0);
  133. }
  134. EXPORT_SYMBOL(down_trylock);
  135. /**
  136. * down_timeout - acquire the semaphore within a specified time
  137. * @sem: the semaphore to be acquired
  138. * @timeout: how long to wait before failing
  139. *
  140. * Attempts to acquire the semaphore. If no more tasks are allowed to
  141. * acquire the semaphore, calling this function will put the task to sleep.
  142. * If the semaphore is not released within the specified number of jiffies,
  143. * this function returns -ETIME. It returns 0 if the semaphore was acquired.
  144. */
  145. int __sched down_timeout(struct semaphore *sem, long timeout)
  146. {
  147. unsigned long flags;
  148. int result = 0;
  149. might_sleep();
  150. raw_spin_lock_irqsave(&sem->lock, flags);
  151. if (likely(sem->count > 0))
  152. sem->count--;
  153. else
  154. result = __down_timeout(sem, timeout);
  155. raw_spin_unlock_irqrestore(&sem->lock, flags);
  156. return result;
  157. }
  158. EXPORT_SYMBOL(down_timeout);
  159. /**
  160. * up - release the semaphore
  161. * @sem: the semaphore to release
  162. *
  163. * Release the semaphore. Unlike mutexes, up() may be called from any
  164. * context and even by tasks which have never called down().
  165. */
  166. void __sched up(struct semaphore *sem)
  167. {
  168. unsigned long flags;
  169. DEFINE_WAKE_Q(wake_q);
  170. raw_spin_lock_irqsave(&sem->lock, flags);
  171. if (likely(list_empty(&sem->wait_list)))
  172. sem->count++;
  173. else
  174. __up(sem, &wake_q);
  175. raw_spin_unlock_irqrestore(&sem->lock, flags);
  176. if (!wake_q_empty(&wake_q))
  177. wake_up_q(&wake_q);
  178. }
  179. EXPORT_SYMBOL(up);
  180. /* Functions for the contended case */
  181. struct semaphore_waiter {
  182. struct list_head list;
  183. struct task_struct *task;
  184. bool up;
  185. };
  186. /*
  187. * Because this function is inlined, the 'state' parameter will be
  188. * constant, and thus optimised away by the compiler. Likewise the
  189. * 'timeout' parameter for the cases without timeouts.
  190. */
  191. static inline int __sched ___down_common(struct semaphore *sem, long state,
  192. long timeout)
  193. {
  194. struct semaphore_waiter waiter;
  195. list_add_tail(&waiter.list, &sem->wait_list);
  196. waiter.task = current;
  197. waiter.up = false;
  198. for (;;) {
  199. if (signal_pending_state(state, current))
  200. goto interrupted;
  201. if (unlikely(timeout <= 0))
  202. goto timed_out;
  203. __set_current_state(state);
  204. raw_spin_unlock_irq(&sem->lock);
  205. timeout = schedule_timeout(timeout);
  206. raw_spin_lock_irq(&sem->lock);
  207. if (waiter.up)
  208. return 0;
  209. }
  210. timed_out:
  211. list_del(&waiter.list);
  212. return -ETIME;
  213. interrupted:
  214. list_del(&waiter.list);
  215. return -EINTR;
  216. }
  217. static inline int __sched __down_common(struct semaphore *sem, long state,
  218. long timeout)
  219. {
  220. int ret;
  221. trace_contention_begin(sem, 0);
  222. ret = ___down_common(sem, state, timeout);
  223. trace_contention_end(sem, ret);
  224. return ret;
  225. }
  226. static noinline void __sched __down(struct semaphore *sem)
  227. {
  228. __down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  229. }
  230. static noinline int __sched __down_interruptible(struct semaphore *sem)
  231. {
  232. return __down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  233. }
  234. static noinline int __sched __down_killable(struct semaphore *sem)
  235. {
  236. return __down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT);
  237. }
  238. static noinline int __sched __down_timeout(struct semaphore *sem, long timeout)
  239. {
  240. return __down_common(sem, TASK_UNINTERRUPTIBLE, timeout);
  241. }
  242. static noinline void __sched __up(struct semaphore *sem,
  243. struct wake_q_head *wake_q)
  244. {
  245. struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list,
  246. struct semaphore_waiter, list);
  247. list_del(&waiter->list);
  248. waiter->up = true;
  249. wake_q_add(wake_q, waiter->task);
  250. }