sync.c 7.1 KB

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