rwsem-spinlock.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. int __sched __down_read_common(struct rw_semaphore *sem, int state)
  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 up my own style of waitqueue */
  125. waiter.task = current;
  126. waiter.type = RWSEM_WAITING_FOR_READ;
  127. get_task_struct(current);
  128. list_add_tail(&waiter.list, &sem->wait_list);
  129. /* wait to be given the lock */
  130. for (;;) {
  131. if (!waiter.task)
  132. break;
  133. if (signal_pending_state(state, current))
  134. goto out_nolock;
  135. set_current_state(state);
  136. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  137. schedule();
  138. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  139. }
  140. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  141. out:
  142. return 0;
  143. out_nolock:
  144. /*
  145. * We didn't take the lock, so that there is a writer, which
  146. * is owner or the first waiter of the sem. If it's a waiter,
  147. * it will be woken by current owner. Not need to wake anybody.
  148. */
  149. list_del(&waiter.list);
  150. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  151. return -EINTR;
  152. }
  153. void __sched __down_read(struct rw_semaphore *sem)
  154. {
  155. __down_read_common(sem, TASK_UNINTERRUPTIBLE);
  156. }
  157. int __sched __down_read_killable(struct rw_semaphore *sem)
  158. {
  159. return __down_read_common(sem, TASK_KILLABLE);
  160. }
  161. /*
  162. * trylock for reading -- returns 1 if successful, 0 if contention
  163. */
  164. int __down_read_trylock(struct rw_semaphore *sem)
  165. {
  166. unsigned long flags;
  167. int ret = 0;
  168. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  169. if (sem->count >= 0 && list_empty(&sem->wait_list)) {
  170. /* granted */
  171. sem->count++;
  172. ret = 1;
  173. }
  174. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  175. return ret;
  176. }
  177. /*
  178. * get a write lock on the semaphore
  179. */
  180. int __sched __down_write_common(struct rw_semaphore *sem, int state)
  181. {
  182. struct rwsem_waiter waiter;
  183. unsigned long flags;
  184. int ret = 0;
  185. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  186. /* set up my own style of waitqueue */
  187. waiter.task = current;
  188. waiter.type = RWSEM_WAITING_FOR_WRITE;
  189. list_add_tail(&waiter.list, &sem->wait_list);
  190. /* wait for someone to release the lock */
  191. for (;;) {
  192. /*
  193. * That is the key to support write lock stealing: allows the
  194. * task already on CPU to get the lock soon rather than put
  195. * itself into sleep and waiting for system woke it or someone
  196. * else in the head of the wait list up.
  197. */
  198. if (sem->count == 0)
  199. break;
  200. if (signal_pending_state(state, current))
  201. goto out_nolock;
  202. set_current_state(state);
  203. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  204. schedule();
  205. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  206. }
  207. /* got the lock */
  208. sem->count = -1;
  209. list_del(&waiter.list);
  210. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  211. return ret;
  212. out_nolock:
  213. list_del(&waiter.list);
  214. if (!list_empty(&sem->wait_list) && sem->count >= 0)
  215. __rwsem_do_wake(sem, 0);
  216. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  217. return -EINTR;
  218. }
  219. void __sched __down_write(struct rw_semaphore *sem)
  220. {
  221. __down_write_common(sem, TASK_UNINTERRUPTIBLE);
  222. }
  223. int __sched __down_write_killable(struct rw_semaphore *sem)
  224. {
  225. return __down_write_common(sem, TASK_KILLABLE);
  226. }
  227. /*
  228. * trylock for writing -- returns 1 if successful, 0 if contention
  229. */
  230. int __down_write_trylock(struct rw_semaphore *sem)
  231. {
  232. unsigned long flags;
  233. int ret = 0;
  234. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  235. if (sem->count == 0) {
  236. /* got the lock */
  237. sem->count = -1;
  238. ret = 1;
  239. }
  240. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  241. return ret;
  242. }
  243. /*
  244. * release a read lock on the semaphore
  245. */
  246. void __up_read(struct rw_semaphore *sem)
  247. {
  248. unsigned long flags;
  249. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  250. if (--sem->count == 0 && !list_empty(&sem->wait_list))
  251. sem = __rwsem_wake_one_writer(sem);
  252. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  253. }
  254. /*
  255. * release a write lock on the semaphore
  256. */
  257. void __up_write(struct rw_semaphore *sem)
  258. {
  259. unsigned long flags;
  260. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  261. sem->count = 0;
  262. if (!list_empty(&sem->wait_list))
  263. sem = __rwsem_do_wake(sem, 1);
  264. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  265. }
  266. /*
  267. * downgrade a write lock into a read lock
  268. * - just wake up any readers at the front of the queue
  269. */
  270. void __downgrade_write(struct rw_semaphore *sem)
  271. {
  272. unsigned long flags;
  273. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  274. sem->count = 1;
  275. if (!list_empty(&sem->wait_list))
  276. sem = __rwsem_do_wake(sem, 0);
  277. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  278. }