percpu-rwsem.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/atomic.h>
  3. #include <linux/percpu.h>
  4. #include <linux/wait.h>
  5. #include <linux/lockdep.h>
  6. #include <linux/percpu-rwsem.h>
  7. #include <linux/rcupdate.h>
  8. #include <linux/sched.h>
  9. #include <linux/sched/task.h>
  10. #include <linux/sched/debug.h>
  11. #include <linux/errno.h>
  12. #include <trace/events/lock.h>
  13. int __percpu_init_rwsem(struct percpu_rw_semaphore *sem,
  14. const char *name, struct lock_class_key *key)
  15. {
  16. sem->read_count = alloc_percpu(int);
  17. if (unlikely(!sem->read_count))
  18. return -ENOMEM;
  19. rcu_sync_init(&sem->rss);
  20. rcuwait_init(&sem->writer);
  21. init_waitqueue_head(&sem->waiters);
  22. atomic_set(&sem->block, 0);
  23. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  24. debug_check_no_locks_freed((void *)sem, sizeof(*sem));
  25. lockdep_init_map(&sem->dep_map, name, key, 0);
  26. #endif
  27. return 0;
  28. }
  29. EXPORT_SYMBOL_GPL(__percpu_init_rwsem);
  30. void percpu_free_rwsem(struct percpu_rw_semaphore *sem)
  31. {
  32. /*
  33. * XXX: temporary kludge. The error path in alloc_super()
  34. * assumes that percpu_free_rwsem() is safe after kzalloc().
  35. */
  36. if (!sem->read_count)
  37. return;
  38. rcu_sync_dtor(&sem->rss);
  39. free_percpu(sem->read_count);
  40. sem->read_count = NULL; /* catch use after free bugs */
  41. }
  42. EXPORT_SYMBOL_GPL(percpu_free_rwsem);
  43. static bool __percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
  44. {
  45. this_cpu_inc(*sem->read_count);
  46. /*
  47. * Due to having preemption disabled the decrement happens on
  48. * the same CPU as the increment, avoiding the
  49. * increment-on-one-CPU-and-decrement-on-another problem.
  50. *
  51. * If the reader misses the writer's assignment of sem->block, then the
  52. * writer is guaranteed to see the reader's increment.
  53. *
  54. * Conversely, any readers that increment their sem->read_count after
  55. * the writer looks are guaranteed to see the sem->block value, which
  56. * in turn means that they are guaranteed to immediately decrement
  57. * their sem->read_count, so that it doesn't matter that the writer
  58. * missed them.
  59. */
  60. smp_mb(); /* A matches D */
  61. /*
  62. * If !sem->block the critical section starts here, matched by the
  63. * release in percpu_up_write().
  64. */
  65. if (likely(!atomic_read_acquire(&sem->block)))
  66. return true;
  67. this_cpu_dec(*sem->read_count);
  68. /* Prod writer to re-evaluate readers_active_check() */
  69. rcuwait_wake_up(&sem->writer);
  70. return false;
  71. }
  72. static inline bool __percpu_down_write_trylock(struct percpu_rw_semaphore *sem)
  73. {
  74. if (atomic_read(&sem->block))
  75. return false;
  76. return atomic_xchg(&sem->block, 1) == 0;
  77. }
  78. static bool __percpu_rwsem_trylock(struct percpu_rw_semaphore *sem, bool reader)
  79. {
  80. if (reader) {
  81. bool ret;
  82. preempt_disable();
  83. ret = __percpu_down_read_trylock(sem);
  84. preempt_enable();
  85. return ret;
  86. }
  87. return __percpu_down_write_trylock(sem);
  88. }
  89. /*
  90. * The return value of wait_queue_entry::func means:
  91. *
  92. * <0 - error, wakeup is terminated and the error is returned
  93. * 0 - no wakeup, a next waiter is tried
  94. * >0 - woken, if EXCLUSIVE, counted towards @nr_exclusive.
  95. *
  96. * We use EXCLUSIVE for both readers and writers to preserve FIFO order,
  97. * and play games with the return value to allow waking multiple readers.
  98. *
  99. * Specifically, we wake readers until we've woken a single writer, or until a
  100. * trylock fails.
  101. */
  102. static int percpu_rwsem_wake_function(struct wait_queue_entry *wq_entry,
  103. unsigned int mode, int wake_flags,
  104. void *key)
  105. {
  106. bool reader = wq_entry->flags & WQ_FLAG_CUSTOM;
  107. struct percpu_rw_semaphore *sem = key;
  108. struct task_struct *p;
  109. /* concurrent against percpu_down_write(), can get stolen */
  110. if (!__percpu_rwsem_trylock(sem, reader))
  111. return 1;
  112. p = get_task_struct(wq_entry->private);
  113. list_del_init(&wq_entry->entry);
  114. smp_store_release(&wq_entry->private, NULL);
  115. wake_up_process(p);
  116. put_task_struct(p);
  117. return !reader; /* wake (readers until) 1 writer */
  118. }
  119. static void percpu_rwsem_wait(struct percpu_rw_semaphore *sem, bool reader)
  120. {
  121. DEFINE_WAIT_FUNC(wq_entry, percpu_rwsem_wake_function);
  122. bool wait;
  123. spin_lock_irq(&sem->waiters.lock);
  124. /*
  125. * Serialize against the wakeup in percpu_up_write(), if we fail
  126. * the trylock, the wakeup must see us on the list.
  127. */
  128. wait = !__percpu_rwsem_trylock(sem, reader);
  129. if (wait) {
  130. wq_entry.flags |= WQ_FLAG_EXCLUSIVE | reader * WQ_FLAG_CUSTOM;
  131. __add_wait_queue_entry_tail(&sem->waiters, &wq_entry);
  132. }
  133. spin_unlock_irq(&sem->waiters.lock);
  134. while (wait) {
  135. set_current_state(TASK_UNINTERRUPTIBLE);
  136. if (!smp_load_acquire(&wq_entry.private))
  137. break;
  138. schedule();
  139. }
  140. __set_current_state(TASK_RUNNING);
  141. }
  142. bool __sched __percpu_down_read(struct percpu_rw_semaphore *sem, bool try)
  143. {
  144. if (__percpu_down_read_trylock(sem))
  145. return true;
  146. if (try)
  147. return false;
  148. trace_contention_begin(sem, LCB_F_PERCPU | LCB_F_READ);
  149. preempt_enable();
  150. percpu_rwsem_wait(sem, /* .reader = */ true);
  151. preempt_disable();
  152. trace_contention_end(sem, 0);
  153. return true;
  154. }
  155. EXPORT_SYMBOL_GPL(__percpu_down_read);
  156. #define per_cpu_sum(var) \
  157. ({ \
  158. typeof(var) __sum = 0; \
  159. int cpu; \
  160. compiletime_assert_atomic_type(__sum); \
  161. for_each_possible_cpu(cpu) \
  162. __sum += per_cpu(var, cpu); \
  163. __sum; \
  164. })
  165. bool percpu_is_read_locked(struct percpu_rw_semaphore *sem)
  166. {
  167. return per_cpu_sum(*sem->read_count) != 0 && !atomic_read(&sem->block);
  168. }
  169. EXPORT_SYMBOL_GPL(percpu_is_read_locked);
  170. /*
  171. * Return true if the modular sum of the sem->read_count per-CPU variable is
  172. * zero. If this sum is zero, then it is stable due to the fact that if any
  173. * newly arriving readers increment a given counter, they will immediately
  174. * decrement that same counter.
  175. *
  176. * Assumes sem->block is set.
  177. */
  178. static bool readers_active_check(struct percpu_rw_semaphore *sem)
  179. {
  180. if (per_cpu_sum(*sem->read_count) != 0)
  181. return false;
  182. /*
  183. * If we observed the decrement; ensure we see the entire critical
  184. * section.
  185. */
  186. smp_mb(); /* C matches B */
  187. return true;
  188. }
  189. void __sched percpu_down_write(struct percpu_rw_semaphore *sem)
  190. {
  191. bool contended = false;
  192. might_sleep();
  193. rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
  194. /* Notify readers to take the slow path. */
  195. rcu_sync_enter(&sem->rss);
  196. /*
  197. * Try set sem->block; this provides writer-writer exclusion.
  198. * Having sem->block set makes new readers block.
  199. */
  200. if (!__percpu_down_write_trylock(sem)) {
  201. trace_contention_begin(sem, LCB_F_PERCPU | LCB_F_WRITE);
  202. percpu_rwsem_wait(sem, /* .reader = */ false);
  203. contended = true;
  204. }
  205. /* smp_mb() implied by __percpu_down_write_trylock() on success -- D matches A */
  206. /*
  207. * If they don't see our store of sem->block, then we are guaranteed to
  208. * see their sem->read_count increment, and therefore will wait for
  209. * them.
  210. */
  211. /* Wait for all active readers to complete. */
  212. rcuwait_wait_event(&sem->writer, readers_active_check(sem), TASK_UNINTERRUPTIBLE);
  213. if (contended)
  214. trace_contention_end(sem, 0);
  215. }
  216. EXPORT_SYMBOL_GPL(percpu_down_write);
  217. void percpu_up_write(struct percpu_rw_semaphore *sem)
  218. {
  219. rwsem_release(&sem->dep_map, _RET_IP_);
  220. /*
  221. * Signal the writer is done, no fast path yet.
  222. *
  223. * One reason that we cannot just immediately flip to readers_fast is
  224. * that new readers might fail to see the results of this writer's
  225. * critical section.
  226. *
  227. * Therefore we force it through the slow path which guarantees an
  228. * acquire and thereby guarantees the critical section's consistency.
  229. */
  230. atomic_set_release(&sem->block, 0);
  231. /*
  232. * Prod any pending reader/writer to make progress.
  233. */
  234. __wake_up(&sem->waiters, TASK_NORMAL, 1, sem);
  235. /*
  236. * Once this completes (at least one RCU-sched grace period hence) the
  237. * reader fast path will be available again. Safe to use outside the
  238. * exclusive write lock because its counting.
  239. */
  240. rcu_sync_exit(&sem->rss);
  241. }
  242. EXPORT_SYMBOL_GPL(percpu_up_write);