sync.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #ifdef CONFIG_PROVE_RCU
  25. #define __INIT_HELD(func) .held = func,
  26. #else
  27. #define __INIT_HELD(func)
  28. #endif
  29. static const struct {
  30. void (*sync)(void);
  31. void (*call)(struct rcu_head *, void (*)(struct rcu_head *));
  32. void (*wait)(void);
  33. #ifdef CONFIG_PROVE_RCU
  34. int (*held)(void);
  35. #endif
  36. } gp_ops[] = {
  37. [RCU_SYNC] = {
  38. .sync = synchronize_rcu,
  39. .call = call_rcu,
  40. .wait = rcu_barrier,
  41. __INIT_HELD(rcu_read_lock_held)
  42. },
  43. [RCU_SCHED_SYNC] = {
  44. .sync = synchronize_sched,
  45. .call = call_rcu_sched,
  46. .wait = rcu_barrier_sched,
  47. __INIT_HELD(rcu_read_lock_sched_held)
  48. },
  49. [RCU_BH_SYNC] = {
  50. .sync = synchronize_rcu_bh,
  51. .call = call_rcu_bh,
  52. .wait = rcu_barrier_bh,
  53. __INIT_HELD(rcu_read_lock_bh_held)
  54. },
  55. };
  56. enum { GP_IDLE = 0, GP_PENDING, GP_PASSED };
  57. enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY };
  58. #define rss_lock gp_wait.lock
  59. #ifdef CONFIG_PROVE_RCU
  60. void rcu_sync_lockdep_assert(struct rcu_sync *rsp)
  61. {
  62. RCU_LOCKDEP_WARN(!gp_ops[rsp->gp_type].held(),
  63. "suspicious rcu_sync_is_idle() usage");
  64. }
  65. #endif
  66. /**
  67. * rcu_sync_init() - Initialize an rcu_sync structure
  68. * @rsp: Pointer to rcu_sync structure to be initialized
  69. * @type: Flavor of RCU with which to synchronize rcu_sync structure
  70. */
  71. void rcu_sync_init(struct rcu_sync *rsp, enum rcu_sync_type type)
  72. {
  73. memset(rsp, 0, sizeof(*rsp));
  74. init_waitqueue_head(&rsp->gp_wait);
  75. rsp->gp_type = type;
  76. }
  77. /**
  78. * rcu_sync_enter() - Force readers onto slowpath
  79. * @rsp: Pointer to rcu_sync structure to use for synchronization
  80. *
  81. * This function is used by updaters who need readers to make use of
  82. * a slowpath during the update. After this function returns, all
  83. * subsequent calls to rcu_sync_is_idle() will return false, which
  84. * tells readers to stay off their fastpaths. A later call to
  85. * rcu_sync_exit() re-enables reader slowpaths.
  86. *
  87. * When called in isolation, rcu_sync_enter() must wait for a grace
  88. * period, however, closely spaced calls to rcu_sync_enter() can
  89. * optimize away the grace-period wait via a state machine implemented
  90. * by rcu_sync_enter(), rcu_sync_exit(), and rcu_sync_func().
  91. */
  92. void rcu_sync_enter(struct rcu_sync *rsp)
  93. {
  94. bool need_wait, need_sync;
  95. spin_lock_irq(&rsp->rss_lock);
  96. need_wait = rsp->gp_count++;
  97. need_sync = rsp->gp_state == GP_IDLE;
  98. if (need_sync)
  99. rsp->gp_state = GP_PENDING;
  100. spin_unlock_irq(&rsp->rss_lock);
  101. BUG_ON(need_wait && need_sync);
  102. if (need_sync) {
  103. gp_ops[rsp->gp_type].sync();
  104. rsp->gp_state = GP_PASSED;
  105. wake_up_all(&rsp->gp_wait);
  106. } else if (need_wait) {
  107. wait_event(rsp->gp_wait, rsp->gp_state == GP_PASSED);
  108. } else {
  109. /*
  110. * Possible when there's a pending CB from a rcu_sync_exit().
  111. * Nobody has yet been allowed the 'fast' path and thus we can
  112. * avoid doing any sync(). The callback will get 'dropped'.
  113. */
  114. BUG_ON(rsp->gp_state != GP_PASSED);
  115. }
  116. }
  117. /**
  118. * rcu_sync_func() - Callback function managing reader access to fastpath
  119. * @rsp: Pointer to rcu_sync structure to use for synchronization
  120. *
  121. * This function is passed to one of the call_rcu() functions by
  122. * rcu_sync_exit(), so that it is invoked after a grace period following the
  123. * that invocation of rcu_sync_exit(). It takes action based on events that
  124. * have taken place in the meantime, so that closely spaced rcu_sync_enter()
  125. * and rcu_sync_exit() pairs need not wait for a grace period.
  126. *
  127. * If another rcu_sync_enter() is invoked before the grace period
  128. * ended, reset state to allow the next rcu_sync_exit() to let the
  129. * readers back onto their fastpaths (after a grace period). If both
  130. * another rcu_sync_enter() and its matching rcu_sync_exit() are invoked
  131. * before the grace period ended, re-invoke call_rcu() on behalf of that
  132. * rcu_sync_exit(). Otherwise, set all state back to idle so that readers
  133. * can again use their fastpaths.
  134. */
  135. static void rcu_sync_func(struct rcu_head *rcu)
  136. {
  137. struct rcu_sync *rsp = container_of(rcu, struct rcu_sync, cb_head);
  138. unsigned long flags;
  139. BUG_ON(rsp->gp_state != GP_PASSED);
  140. BUG_ON(rsp->cb_state == CB_IDLE);
  141. spin_lock_irqsave(&rsp->rss_lock, flags);
  142. if (rsp->gp_count) {
  143. /*
  144. * A new rcu_sync_begin() has happened; drop the callback.
  145. */
  146. rsp->cb_state = CB_IDLE;
  147. } else if (rsp->cb_state == CB_REPLAY) {
  148. /*
  149. * A new rcu_sync_exit() has happened; requeue the callback
  150. * to catch a later GP.
  151. */
  152. rsp->cb_state = CB_PENDING;
  153. gp_ops[rsp->gp_type].call(&rsp->cb_head, rcu_sync_func);
  154. } else {
  155. /*
  156. * We're at least a GP after rcu_sync_exit(); eveybody will now
  157. * have observed the write side critical section. Let 'em rip!.
  158. */
  159. rsp->cb_state = CB_IDLE;
  160. rsp->gp_state = GP_IDLE;
  161. }
  162. spin_unlock_irqrestore(&rsp->rss_lock, flags);
  163. }
  164. /**
  165. * rcu_sync_exit() - Allow readers back onto fast patch after grace period
  166. * @rsp: Pointer to rcu_sync structure to use for synchronization
  167. *
  168. * This function is used by updaters who have completed, and can therefore
  169. * now allow readers to make use of their fastpaths after a grace period
  170. * has elapsed. After this grace period has completed, all subsequent
  171. * calls to rcu_sync_is_idle() will return true, which tells readers that
  172. * they can once again use their fastpaths.
  173. */
  174. void rcu_sync_exit(struct rcu_sync *rsp)
  175. {
  176. spin_lock_irq(&rsp->rss_lock);
  177. if (!--rsp->gp_count) {
  178. if (rsp->cb_state == CB_IDLE) {
  179. rsp->cb_state = CB_PENDING;
  180. gp_ops[rsp->gp_type].call(&rsp->cb_head, rcu_sync_func);
  181. } else if (rsp->cb_state == CB_PENDING) {
  182. rsp->cb_state = CB_REPLAY;
  183. }
  184. }
  185. spin_unlock_irq(&rsp->rss_lock);
  186. }
  187. /**
  188. * rcu_sync_dtor() - Clean up an rcu_sync structure
  189. * @rsp: Pointer to rcu_sync structure to be cleaned up
  190. */
  191. void rcu_sync_dtor(struct rcu_sync *rsp)
  192. {
  193. int cb_state;
  194. BUG_ON(rsp->gp_count);
  195. spin_lock_irq(&rsp->rss_lock);
  196. if (rsp->cb_state == CB_REPLAY)
  197. rsp->cb_state = CB_PENDING;
  198. cb_state = rsp->cb_state;
  199. spin_unlock_irq(&rsp->rss_lock);
  200. if (cb_state != CB_IDLE) {
  201. gp_ops[rsp->gp_type].wait();
  202. BUG_ON(rsp->cb_state != CB_IDLE);
  203. }
  204. }