sync.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * RCU-based infrastructure for lightweight reader-writer locking
  4. *
  5. * Copyright (c) 2015, Red Hat, Inc.
  6. *
  7. * Author: Oleg Nesterov <[email protected]>
  8. */
  9. #include <linux/rcu_sync.h>
  10. #include <linux/sched.h>
  11. enum { GP_IDLE = 0, GP_ENTER, GP_PASSED, GP_EXIT, GP_REPLAY };
  12. #define rss_lock gp_wait.lock
  13. /**
  14. * rcu_sync_init() - Initialize an rcu_sync structure
  15. * @rsp: Pointer to rcu_sync structure to be initialized
  16. */
  17. void rcu_sync_init(struct rcu_sync *rsp)
  18. {
  19. memset(rsp, 0, sizeof(*rsp));
  20. init_waitqueue_head(&rsp->gp_wait);
  21. }
  22. /**
  23. * rcu_sync_enter_start - Force readers onto slow path for multiple updates
  24. * @rsp: Pointer to rcu_sync structure to use for synchronization
  25. *
  26. * Must be called after rcu_sync_init() and before first use.
  27. *
  28. * Ensures rcu_sync_is_idle() returns false and rcu_sync_{enter,exit}()
  29. * pairs turn into NO-OPs.
  30. */
  31. void rcu_sync_enter_start(struct rcu_sync *rsp)
  32. {
  33. rsp->gp_count++;
  34. rsp->gp_state = GP_PASSED;
  35. }
  36. static void rcu_sync_func(struct rcu_head *rhp);
  37. static void rcu_sync_call(struct rcu_sync *rsp)
  38. {
  39. call_rcu_hurry(&rsp->cb_head, rcu_sync_func);
  40. }
  41. /**
  42. * rcu_sync_func() - Callback function managing reader access to fastpath
  43. * @rhp: Pointer to rcu_head in rcu_sync structure to use for synchronization
  44. *
  45. * This function is passed to call_rcu() function by rcu_sync_enter() and
  46. * rcu_sync_exit(), so that it is invoked after a grace period following the
  47. * that invocation of enter/exit.
  48. *
  49. * If it is called by rcu_sync_enter() it signals that all the readers were
  50. * switched onto slow path.
  51. *
  52. * If it is called by rcu_sync_exit() it takes action based on events that
  53. * have taken place in the meantime, so that closely spaced rcu_sync_enter()
  54. * and rcu_sync_exit() pairs need not wait for a grace period.
  55. *
  56. * If another rcu_sync_enter() is invoked before the grace period
  57. * ended, reset state to allow the next rcu_sync_exit() to let the
  58. * readers back onto their fastpaths (after a grace period). If both
  59. * another rcu_sync_enter() and its matching rcu_sync_exit() are invoked
  60. * before the grace period ended, re-invoke call_rcu() on behalf of that
  61. * rcu_sync_exit(). Otherwise, set all state back to idle so that readers
  62. * can again use their fastpaths.
  63. */
  64. static void rcu_sync_func(struct rcu_head *rhp)
  65. {
  66. struct rcu_sync *rsp = container_of(rhp, struct rcu_sync, cb_head);
  67. unsigned long flags;
  68. WARN_ON_ONCE(READ_ONCE(rsp->gp_state) == GP_IDLE);
  69. WARN_ON_ONCE(READ_ONCE(rsp->gp_state) == GP_PASSED);
  70. spin_lock_irqsave(&rsp->rss_lock, flags);
  71. if (rsp->gp_count) {
  72. /*
  73. * We're at least a GP after the GP_IDLE->GP_ENTER transition.
  74. */
  75. WRITE_ONCE(rsp->gp_state, GP_PASSED);
  76. wake_up_locked(&rsp->gp_wait);
  77. } else if (rsp->gp_state == GP_REPLAY) {
  78. /*
  79. * A new rcu_sync_exit() has happened; requeue the callback to
  80. * catch a later GP.
  81. */
  82. WRITE_ONCE(rsp->gp_state, GP_EXIT);
  83. rcu_sync_call(rsp);
  84. } else {
  85. /*
  86. * We're at least a GP after the last rcu_sync_exit(); everybody
  87. * will now have observed the write side critical section.
  88. * Let 'em rip!
  89. */
  90. WRITE_ONCE(rsp->gp_state, GP_IDLE);
  91. }
  92. spin_unlock_irqrestore(&rsp->rss_lock, flags);
  93. }
  94. /**
  95. * rcu_sync_enter() - Force readers onto slowpath
  96. * @rsp: Pointer to rcu_sync structure to use for synchronization
  97. *
  98. * This function is used by updaters who need readers to make use of
  99. * a slowpath during the update. After this function returns, all
  100. * subsequent calls to rcu_sync_is_idle() will return false, which
  101. * tells readers to stay off their fastpaths. A later call to
  102. * rcu_sync_exit() re-enables reader fastpaths.
  103. *
  104. * When called in isolation, rcu_sync_enter() must wait for a grace
  105. * period, however, closely spaced calls to rcu_sync_enter() can
  106. * optimize away the grace-period wait via a state machine implemented
  107. * by rcu_sync_enter(), rcu_sync_exit(), and rcu_sync_func().
  108. */
  109. void rcu_sync_enter(struct rcu_sync *rsp)
  110. {
  111. int gp_state;
  112. spin_lock_irq(&rsp->rss_lock);
  113. gp_state = rsp->gp_state;
  114. if (gp_state == GP_IDLE) {
  115. WRITE_ONCE(rsp->gp_state, GP_ENTER);
  116. WARN_ON_ONCE(rsp->gp_count);
  117. /*
  118. * Note that we could simply do rcu_sync_call(rsp) here and
  119. * avoid the "if (gp_state == GP_IDLE)" block below.
  120. *
  121. * However, synchronize_rcu() can be faster if rcu_expedited
  122. * or rcu_blocking_is_gp() is true.
  123. *
  124. * Another reason is that we can't wait for rcu callback if
  125. * we are called at early boot time but this shouldn't happen.
  126. */
  127. }
  128. rsp->gp_count++;
  129. spin_unlock_irq(&rsp->rss_lock);
  130. if (gp_state == GP_IDLE) {
  131. /*
  132. * See the comment above, this simply does the "synchronous"
  133. * call_rcu(rcu_sync_func) which does GP_ENTER -> GP_PASSED.
  134. */
  135. synchronize_rcu();
  136. rcu_sync_func(&rsp->cb_head);
  137. /* Not really needed, wait_event() would see GP_PASSED. */
  138. return;
  139. }
  140. wait_event(rsp->gp_wait, READ_ONCE(rsp->gp_state) >= GP_PASSED);
  141. }
  142. /**
  143. * rcu_sync_exit() - Allow readers back onto fast path after grace period
  144. * @rsp: Pointer to rcu_sync structure to use for synchronization
  145. *
  146. * This function is used by updaters who have completed, and can therefore
  147. * now allow readers to make use of their fastpaths after a grace period
  148. * has elapsed. After this grace period has completed, all subsequent
  149. * calls to rcu_sync_is_idle() will return true, which tells readers that
  150. * they can once again use their fastpaths.
  151. */
  152. void rcu_sync_exit(struct rcu_sync *rsp)
  153. {
  154. WARN_ON_ONCE(READ_ONCE(rsp->gp_state) == GP_IDLE);
  155. WARN_ON_ONCE(READ_ONCE(rsp->gp_count) == 0);
  156. spin_lock_irq(&rsp->rss_lock);
  157. if (!--rsp->gp_count) {
  158. if (rsp->gp_state == GP_PASSED) {
  159. WRITE_ONCE(rsp->gp_state, GP_EXIT);
  160. rcu_sync_call(rsp);
  161. } else if (rsp->gp_state == GP_EXIT) {
  162. WRITE_ONCE(rsp->gp_state, GP_REPLAY);
  163. }
  164. }
  165. spin_unlock_irq(&rsp->rss_lock);
  166. }
  167. /**
  168. * rcu_sync_dtor() - Clean up an rcu_sync structure
  169. * @rsp: Pointer to rcu_sync structure to be cleaned up
  170. */
  171. void rcu_sync_dtor(struct rcu_sync *rsp)
  172. {
  173. int gp_state;
  174. WARN_ON_ONCE(READ_ONCE(rsp->gp_count));
  175. WARN_ON_ONCE(READ_ONCE(rsp->gp_state) == GP_PASSED);
  176. spin_lock_irq(&rsp->rss_lock);
  177. if (rsp->gp_state == GP_REPLAY)
  178. WRITE_ONCE(rsp->gp_state, GP_EXIT);
  179. gp_state = rsp->gp_state;
  180. spin_unlock_irq(&rsp->rss_lock);
  181. if (gp_state != GP_IDLE) {
  182. rcu_barrier();
  183. WARN_ON_ONCE(rsp->gp_state != GP_IDLE);
  184. }
  185. }