rwsem-spinlock.c 7.1 KB

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