semaphore.c 7.6 KB

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