semaphore.c 7.2 KB

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