sync.c 5.8 KB

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