semaphore.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/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 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 task_struct *task = current;
  186. struct semaphore_waiter waiter;
  187. list_add_tail(&waiter.list, &sem->wait_list);
  188. waiter.task = task;
  189. waiter.up = false;
  190. for (;;) {
  191. if (signal_pending_state(state, task))
  192. goto interrupted;
  193. if (unlikely(timeout <= 0))
  194. goto timed_out;
  195. __set_task_state(task, 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. }