semaphore.c 7.2 KB

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