rwsem-spinlock.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* rwsem-spinlock.c: R/W semaphores: contention handling functions for
  2. * generic spinlock implementation
  3. *
  4. * Copyright (c) 2001 David Howells (dhowells@redhat.com).
  5. * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de>
  6. * - Derived also from comments by Linus
  7. */
  8. #include <linux/rwsem.h>
  9. #include <linux/sched.h>
  10. #include <linux/export.h>
  11. enum rwsem_waiter_type {
  12. RWSEM_WAITING_FOR_WRITE,
  13. RWSEM_WAITING_FOR_READ
  14. };
  15. struct rwsem_waiter {
  16. struct list_head list;
  17. struct task_struct *task;
  18. enum rwsem_waiter_type type;
  19. };
  20. int rwsem_is_locked(struct rw_semaphore *sem)
  21. {
  22. int ret = 1;
  23. unsigned long flags;
  24. if (raw_spin_trylock_irqsave(&sem->wait_lock, flags)) {
  25. ret = (sem->count != 0);
  26. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  27. }
  28. return ret;
  29. }
  30. EXPORT_SYMBOL(rwsem_is_locked);
  31. /*
  32. * initialise the semaphore
  33. */
  34. void __init_rwsem(struct rw_semaphore *sem, const char *name,
  35. struct lock_class_key *key)
  36. {
  37. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  38. /*
  39. * Make sure we are not reinitializing a held semaphore:
  40. */
  41. debug_check_no_locks_freed((void *)sem, sizeof(*sem));
  42. lockdep_init_map(&sem->dep_map, name, key, 0);
  43. #endif
  44. sem->count = 0;
  45. raw_spin_lock_init(&sem->wait_lock);
  46. INIT_LIST_HEAD(&sem->wait_list);
  47. }
  48. EXPORT_SYMBOL(__init_rwsem);
  49. /*
  50. * handle the lock release when processes blocked on it that can now run
  51. * - if we come here, then:
  52. * - the 'active count' _reached_ zero
  53. * - the 'waiting count' is non-zero
  54. * - the spinlock must be held by the caller
  55. * - woken process blocks are discarded from the list after having task zeroed
  56. * - writers are only woken if wakewrite is non-zero
  57. */
  58. static inline struct rw_semaphore *
  59. __rwsem_do_wake(struct rw_semaphore *sem, int wakewrite)
  60. {
  61. struct rwsem_waiter *waiter;
  62. struct task_struct *tsk;
  63. int woken;
  64. waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
  65. if (waiter->type == RWSEM_WAITING_FOR_WRITE) {
  66. if (wakewrite)
  67. /* Wake up a writer. Note that we do not grant it the
  68. * lock - it will have to acquire it when it runs. */
  69. wake_up_process(waiter->task);
  70. goto out;
  71. }
  72. /* grant an infinite number of read locks to the front of the queue */
  73. woken = 0;
  74. do {
  75. struct list_head *next = waiter->list.next;
  76. list_del(&waiter->list);
  77. tsk = waiter->task;
  78. /*
  79. * Make sure we do not wakeup the next reader before
  80. * setting the nil condition to grant the next reader;
  81. * otherwise we could miss the wakeup on the other
  82. * side and end up sleeping again. See the pairing
  83. * in rwsem_down_read_failed().
  84. */
  85. smp_mb();
  86. waiter->task = NULL;
  87. wake_up_process(tsk);
  88. put_task_struct(tsk);
  89. woken++;
  90. if (next == &sem->wait_list)
  91. break;
  92. waiter = list_entry(next, struct rwsem_waiter, list);
  93. } while (waiter->type != RWSEM_WAITING_FOR_WRITE);
  94. sem->count += woken;
  95. out:
  96. return sem;
  97. }
  98. /*
  99. * wake a single writer
  100. */
  101. static inline struct rw_semaphore *
  102. __rwsem_wake_one_writer(struct rw_semaphore *sem)
  103. {
  104. struct rwsem_waiter *waiter;
  105. waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
  106. wake_up_process(waiter->task);
  107. return sem;
  108. }
  109. /*
  110. * get a read lock on the semaphore
  111. */
  112. void __sched __down_read(struct rw_semaphore *sem)
  113. {
  114. struct rwsem_waiter waiter;
  115. struct task_struct *tsk;
  116. unsigned long flags;
  117. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  118. if (sem->count >= 0 && list_empty(&sem->wait_list)) {
  119. /* granted */
  120. sem->count++;
  121. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  122. goto out;
  123. }
  124. tsk = current;
  125. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  126. /* set up my own style of waitqueue */
  127. waiter.task = tsk;
  128. waiter.type = RWSEM_WAITING_FOR_READ;
  129. get_task_struct(tsk);
  130. list_add_tail(&waiter.list, &sem->wait_list);
  131. /* we don't need to touch the semaphore struct anymore */
  132. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  133. /* wait to be given the lock */
  134. for (;;) {
  135. if (!waiter.task)
  136. break;
  137. schedule();
  138. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  139. }
  140. __set_task_state(tsk, TASK_RUNNING);
  141. out:
  142. ;
  143. }
  144. /*
  145. * trylock for reading -- returns 1 if successful, 0 if contention
  146. */
  147. int __down_read_trylock(struct rw_semaphore *sem)
  148. {
  149. unsigned long flags;
  150. int ret = 0;
  151. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  152. if (sem->count >= 0 && list_empty(&sem->wait_list)) {
  153. /* granted */
  154. sem->count++;
  155. ret = 1;
  156. }
  157. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  158. return ret;
  159. }
  160. /*
  161. * get a write lock on the semaphore
  162. */
  163. int __sched __down_write_common(struct rw_semaphore *sem, int state)
  164. {
  165. struct rwsem_waiter waiter;
  166. struct task_struct *tsk;
  167. unsigned long flags;
  168. int ret = 0;
  169. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  170. /* set up my own style of waitqueue */
  171. tsk = current;
  172. waiter.task = tsk;
  173. waiter.type = RWSEM_WAITING_FOR_WRITE;
  174. list_add_tail(&waiter.list, &sem->wait_list);
  175. /* wait for someone to release the lock */
  176. for (;;) {
  177. /*
  178. * That is the key to support write lock stealing: allows the
  179. * task already on CPU to get the lock soon rather than put
  180. * itself into sleep and waiting for system woke it or someone
  181. * else in the head of the wait list up.
  182. */
  183. if (sem->count == 0)
  184. break;
  185. if (signal_pending_state(state, current))
  186. goto out_nolock;
  187. set_task_state(tsk, state);
  188. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  189. schedule();
  190. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  191. }
  192. /* got the lock */
  193. sem->count = -1;
  194. list_del(&waiter.list);
  195. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  196. return ret;
  197. out_nolock:
  198. list_del(&waiter.list);
  199. if (!list_empty(&sem->wait_list) && sem->count >= 0)
  200. __rwsem_do_wake(sem, 0);
  201. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  202. return -EINTR;
  203. }
  204. void __sched __down_write(struct rw_semaphore *sem)
  205. {
  206. __down_write_common(sem, TASK_UNINTERRUPTIBLE);
  207. }
  208. int __sched __down_write_killable(struct rw_semaphore *sem)
  209. {
  210. return __down_write_common(sem, TASK_KILLABLE);
  211. }
  212. /*
  213. * trylock for writing -- returns 1 if successful, 0 if contention
  214. */
  215. int __down_write_trylock(struct rw_semaphore *sem)
  216. {
  217. unsigned long flags;
  218. int ret = 0;
  219. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  220. if (sem->count == 0) {
  221. /* got the lock */
  222. sem->count = -1;
  223. ret = 1;
  224. }
  225. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  226. return ret;
  227. }
  228. /*
  229. * release a read lock on the semaphore
  230. */
  231. void __up_read(struct rw_semaphore *sem)
  232. {
  233. unsigned long flags;
  234. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  235. if (--sem->count == 0 && !list_empty(&sem->wait_list))
  236. sem = __rwsem_wake_one_writer(sem);
  237. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  238. }
  239. /*
  240. * release a write lock on the semaphore
  241. */
  242. void __up_write(struct rw_semaphore *sem)
  243. {
  244. unsigned long flags;
  245. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  246. sem->count = 0;
  247. if (!list_empty(&sem->wait_list))
  248. sem = __rwsem_do_wake(sem, 1);
  249. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  250. }
  251. /*
  252. * downgrade a write lock into a read lock
  253. * - just wake up any readers at the front of the queue
  254. */
  255. void __downgrade_write(struct rw_semaphore *sem)
  256. {
  257. unsigned long flags;
  258. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  259. sem->count = 1;
  260. if (!list_empty(&sem->wait_list))
  261. sem = __rwsem_do_wake(sem, 0);
  262. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  263. }