semaphore.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/module.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 jiffies);
  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. ftrace_special(sem->count, 0, __LINE__);
  53. spin_lock_irqsave(&sem->lock, flags);
  54. if (likely(sem->count > 0))
  55. sem->count--;
  56. else
  57. __down(sem);
  58. 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. spin_lock_irqsave(&sem->lock, flags);
  75. if (likely(sem->count > 0))
  76. sem->count--;
  77. else
  78. result = __down_interruptible(sem);
  79. 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. spin_lock_irqsave(&sem->lock, flags);
  98. if (likely(sem->count > 0))
  99. sem->count--;
  100. else
  101. result = __down_killable(sem);
  102. 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 mutex 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. spin_lock_irqsave(&sem->lock, flags);
  124. count = sem->count - 1;
  125. if (likely(count >= 0))
  126. sem->count = count;
  127. 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. * @jiffies: 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 jiffies)
  142. {
  143. unsigned long flags;
  144. int result = 0;
  145. spin_lock_irqsave(&sem->lock, flags);
  146. if (likely(sem->count > 0))
  147. sem->count--;
  148. else
  149. result = __down_timeout(sem, jiffies);
  150. 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. spin_lock_irqsave(&sem->lock, flags);
  165. if (likely(list_empty(&sem->wait_list)))
  166. sem->count++;
  167. else
  168. __up(sem);
  169. 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. int 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 task_struct *task = current;
  187. struct semaphore_waiter waiter;
  188. list_add_tail(&waiter.list, &sem->wait_list);
  189. waiter.task = task;
  190. waiter.up = 0;
  191. for (;;) {
  192. if (state == TASK_INTERRUPTIBLE && signal_pending(task))
  193. goto interrupted;
  194. if (state == TASK_KILLABLE && fatal_signal_pending(task))
  195. goto interrupted;
  196. if (timeout <= 0)
  197. goto timed_out;
  198. __set_task_state(task, state);
  199. spin_unlock_irq(&sem->lock);
  200. timeout = schedule_timeout(timeout);
  201. spin_lock_irq(&sem->lock);
  202. if (waiter.up)
  203. return 0;
  204. }
  205. timed_out:
  206. list_del(&waiter.list);
  207. return -ETIME;
  208. interrupted:
  209. list_del(&waiter.list);
  210. return -EINTR;
  211. }
  212. static noinline void __sched __down(struct semaphore *sem)
  213. {
  214. __down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  215. }
  216. static noinline int __sched __down_interruptible(struct semaphore *sem)
  217. {
  218. return __down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  219. }
  220. static noinline int __sched __down_killable(struct semaphore *sem)
  221. {
  222. return __down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT);
  223. }
  224. static noinline int __sched __down_timeout(struct semaphore *sem, long jiffies)
  225. {
  226. return __down_common(sem, TASK_UNINTERRUPTIBLE, jiffies);
  227. }
  228. static noinline void __sched __up(struct semaphore *sem)
  229. {
  230. struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list,
  231. struct semaphore_waiter, list);
  232. list_del(&waiter->list);
  233. waiter->up = 1;
  234. wake_up_process(waiter->task);
  235. }