sync.c 7.0 KB

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