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