semaphore.c 7.3 KB

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