rwsem-spinlock.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. void __sched __down_write_nested(struct rw_semaphore *sem, int subclass)
  164. {
  165. struct rwsem_waiter waiter;
  166. struct task_struct *tsk;
  167. unsigned long flags;
  168. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  169. /* set up my own style of waitqueue */
  170. tsk = current;
  171. waiter.task = tsk;
  172. waiter.type = RWSEM_WAITING_FOR_WRITE;
  173. list_add_tail(&waiter.list, &sem->wait_list);
  174. /* wait for someone to release the lock */
  175. for (;;) {
  176. /*
  177. * That is the key to support write lock stealing: allows the
  178. * task already on CPU to get the lock soon rather than put
  179. * itself into sleep and waiting for system woke it or someone
  180. * else in the head of the wait list up.
  181. */
  182. if (sem->count == 0)
  183. break;
  184. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  185. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  186. schedule();
  187. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  188. }
  189. /* got the lock */
  190. sem->count = -1;
  191. list_del(&waiter.list);
  192. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  193. }
  194. void __sched __down_write(struct rw_semaphore *sem)
  195. {
  196. __down_write_nested(sem, 0);
  197. }
  198. /*
  199. * trylock for writing -- returns 1 if successful, 0 if contention
  200. */
  201. int __down_write_trylock(struct rw_semaphore *sem)
  202. {
  203. unsigned long flags;
  204. int ret = 0;
  205. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  206. if (sem->count == 0) {
  207. /* got the lock */
  208. sem->count = -1;
  209. ret = 1;
  210. }
  211. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  212. return ret;
  213. }
  214. /*
  215. * release a read lock on the semaphore
  216. */
  217. void __up_read(struct rw_semaphore *sem)
  218. {
  219. unsigned long flags;
  220. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  221. if (--sem->count == 0 && !list_empty(&sem->wait_list))
  222. sem = __rwsem_wake_one_writer(sem);
  223. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  224. }
  225. /*
  226. * release a write lock on the semaphore
  227. */
  228. void __up_write(struct rw_semaphore *sem)
  229. {
  230. unsigned long flags;
  231. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  232. sem->count = 0;
  233. if (!list_empty(&sem->wait_list))
  234. sem = __rwsem_do_wake(sem, 1);
  235. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  236. }
  237. /*
  238. * downgrade a write lock into a read lock
  239. * - just wake up any readers at the front of the queue
  240. */
  241. void __downgrade_write(struct rw_semaphore *sem)
  242. {
  243. unsigned long flags;
  244. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  245. sem->count = 1;
  246. if (!list_empty(&sem->wait_list))
  247. sem = __rwsem_do_wake(sem, 0);
  248. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  249. }