sync.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * RCU-based infrastructure for lightweight reader-writer locking
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, you can access it online at
  16. * http://www.gnu.org/licenses/gpl-2.0.html.
  17. *
  18. * Copyright (c) 2015, Red Hat, Inc.
  19. *
  20. * Author: Oleg Nesterov <oleg@redhat.com>
  21. */
  22. #include <linux/rcu_sync.h>
  23. #include <linux/sched.h>
  24. enum { GP_IDLE = 0, GP_PENDING, GP_PASSED };
  25. enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY };
  26. #define rss_lock gp_wait.lock
  27. /**
  28. * rcu_sync_init() - Initialize an rcu_sync structure
  29. * @rsp: Pointer to rcu_sync structure to be initialized
  30. * @type: Flavor of RCU with which to synchronize rcu_sync structure
  31. */
  32. void rcu_sync_init(struct rcu_sync *rsp, enum rcu_sync_type type)
  33. {
  34. memset(rsp, 0, sizeof(*rsp));
  35. init_waitqueue_head(&rsp->gp_wait);
  36. switch (type) {
  37. case RCU_SYNC:
  38. rsp->sync = synchronize_rcu;
  39. rsp->call = call_rcu;
  40. break;
  41. case RCU_SCHED_SYNC:
  42. rsp->sync = synchronize_sched;
  43. rsp->call = call_rcu_sched;
  44. break;
  45. case RCU_BH_SYNC:
  46. rsp->sync = synchronize_rcu_bh;
  47. rsp->call = call_rcu_bh;
  48. break;
  49. }
  50. }
  51. /**
  52. * rcu_sync_enter() - Force readers onto slowpath
  53. * @rsp: Pointer to rcu_sync structure to use for synchronization
  54. *
  55. * This function is used by updaters who need readers to make use of
  56. * a slowpath during the update. After this function returns, all
  57. * subsequent calls to rcu_sync_is_idle() will return false, which
  58. * tells readers to stay off their fastpaths. A later call to
  59. * rcu_sync_exit() re-enables reader slowpaths.
  60. *
  61. * When called in isolation, rcu_sync_enter() must wait for a grace
  62. * period, however, closely spaced calls to rcu_sync_enter() can
  63. * optimize away the grace-period wait via a state machine implemented
  64. * by rcu_sync_enter(), rcu_sync_exit(), and rcu_sync_func().
  65. */
  66. void rcu_sync_enter(struct rcu_sync *rsp)
  67. {
  68. bool need_wait, need_sync;
  69. spin_lock_irq(&rsp->rss_lock);
  70. need_wait = rsp->gp_count++;
  71. need_sync = rsp->gp_state == GP_IDLE;
  72. if (need_sync)
  73. rsp->gp_state = GP_PENDING;
  74. spin_unlock_irq(&rsp->rss_lock);
  75. BUG_ON(need_wait && need_sync);
  76. if (need_sync) {
  77. rsp->sync();
  78. rsp->gp_state = GP_PASSED;
  79. wake_up_all(&rsp->gp_wait);
  80. } else if (need_wait) {
  81. wait_event(rsp->gp_wait, rsp->gp_state == GP_PASSED);
  82. } else {
  83. /*
  84. * Possible when there's a pending CB from a rcu_sync_exit().
  85. * Nobody has yet been allowed the 'fast' path and thus we can
  86. * avoid doing any sync(). The callback will get 'dropped'.
  87. */
  88. BUG_ON(rsp->gp_state != GP_PASSED);
  89. }
  90. }
  91. /**
  92. * rcu_sync_func() - Callback function managing reader access to fastpath
  93. * @rsp: Pointer to rcu_sync structure to use for synchronization
  94. *
  95. * This function is passed to one of the call_rcu() functions by
  96. * rcu_sync_exit(), so that it is invoked after a grace period following the
  97. * that invocation of rcu_sync_exit(). It takes action based on events that
  98. * have taken place in the meantime, so that closely spaced rcu_sync_enter()
  99. * and rcu_sync_exit() pairs need not wait for a grace period.
  100. *
  101. * If another rcu_sync_enter() is invoked before the grace period
  102. * ended, reset state to allow the next rcu_sync_exit() to let the
  103. * readers back onto their fastpaths (after a grace period). If both
  104. * another rcu_sync_enter() and its matching rcu_sync_exit() are invoked
  105. * before the grace period ended, re-invoke call_rcu() on behalf of that
  106. * rcu_sync_exit(). Otherwise, set all state back to idle so that readers
  107. * can again use their fastpaths.
  108. */
  109. static void rcu_sync_func(struct rcu_head *rcu)
  110. {
  111. struct rcu_sync *rsp = container_of(rcu, struct rcu_sync, cb_head);
  112. unsigned long flags;
  113. BUG_ON(rsp->gp_state != GP_PASSED);
  114. BUG_ON(rsp->cb_state == CB_IDLE);
  115. spin_lock_irqsave(&rsp->rss_lock, flags);
  116. if (rsp->gp_count) {
  117. /*
  118. * A new rcu_sync_begin() has happened; drop the callback.
  119. */
  120. rsp->cb_state = CB_IDLE;
  121. } else if (rsp->cb_state == CB_REPLAY) {
  122. /*
  123. * A new rcu_sync_exit() has happened; requeue the callback
  124. * to catch a later GP.
  125. */
  126. rsp->cb_state = CB_PENDING;
  127. rsp->call(&rsp->cb_head, rcu_sync_func);
  128. } else {
  129. /*
  130. * We're at least a GP after rcu_sync_exit(); eveybody will now
  131. * have observed the write side critical section. Let 'em rip!.
  132. */
  133. rsp->cb_state = CB_IDLE;
  134. rsp->gp_state = GP_IDLE;
  135. }
  136. spin_unlock_irqrestore(&rsp->rss_lock, flags);
  137. }
  138. /**
  139. * rcu_sync_exit() - Allow readers back onto fast patch after grace period
  140. * @rsp: Pointer to rcu_sync structure to use for synchronization
  141. *
  142. * This function is used by updaters who have completed, and can therefore
  143. * now allow readers to make use of their fastpaths after a grace period
  144. * has elapsed. After this grace period has completed, all subsequent
  145. * calls to rcu_sync_is_idle() will return true, which tells readers that
  146. * they can once again use their fastpaths.
  147. */
  148. void rcu_sync_exit(struct rcu_sync *rsp)
  149. {
  150. spin_lock_irq(&rsp->rss_lock);
  151. if (!--rsp->gp_count) {
  152. if (rsp->cb_state == CB_IDLE) {
  153. rsp->cb_state = CB_PENDING;
  154. rsp->call(&rsp->cb_head, rcu_sync_func);
  155. } else if (rsp->cb_state == CB_PENDING) {
  156. rsp->cb_state = CB_REPLAY;
  157. }
  158. }
  159. spin_unlock_irq(&rsp->rss_lock);
  160. }