rwsem-spinlock.c 6.7 KB

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