rwsem-spinlock.c 7.7 KB

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