srcutiny.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Sleepable Read-Copy Update mechanism for mutual exclusion,
  3. * tiny version for non-preemptible single-CPU use.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, you can access it online at
  17. * http://www.gnu.org/licenses/gpl-2.0.html.
  18. *
  19. * Copyright (C) IBM Corporation, 2017
  20. *
  21. * Author: Paul McKenney <paulmck@us.ibm.com>
  22. */
  23. #include <linux/export.h>
  24. #include <linux/mutex.h>
  25. #include <linux/preempt.h>
  26. #include <linux/rcupdate_wait.h>
  27. #include <linux/sched.h>
  28. #include <linux/delay.h>
  29. #include <linux/srcu.h>
  30. #include <linux/rcu_node_tree.h>
  31. #include "rcu.h"
  32. static int init_srcu_struct_fields(struct srcu_struct *sp)
  33. {
  34. sp->srcu_lock_nesting[0] = 0;
  35. sp->srcu_lock_nesting[1] = 0;
  36. init_swait_queue_head(&sp->srcu_wq);
  37. sp->srcu_gp_seq = 0;
  38. rcu_segcblist_init(&sp->srcu_cblist);
  39. sp->srcu_gp_running = false;
  40. sp->srcu_gp_waiting = false;
  41. sp->srcu_idx = 0;
  42. INIT_WORK(&sp->srcu_work, srcu_drive_gp);
  43. return 0;
  44. }
  45. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  46. int __init_srcu_struct(struct srcu_struct *sp, const char *name,
  47. struct lock_class_key *key)
  48. {
  49. /* Don't re-initialize a lock while it is held. */
  50. debug_check_no_locks_freed((void *)sp, sizeof(*sp));
  51. lockdep_init_map(&sp->dep_map, name, key, 0);
  52. return init_srcu_struct_fields(sp);
  53. }
  54. EXPORT_SYMBOL_GPL(__init_srcu_struct);
  55. #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  56. /*
  57. * init_srcu_struct - initialize a sleep-RCU structure
  58. * @sp: structure to initialize.
  59. *
  60. * Must invoke this on a given srcu_struct before passing that srcu_struct
  61. * to any other function. Each srcu_struct represents a separate domain
  62. * of SRCU protection.
  63. */
  64. int init_srcu_struct(struct srcu_struct *sp)
  65. {
  66. return init_srcu_struct_fields(sp);
  67. }
  68. EXPORT_SYMBOL_GPL(init_srcu_struct);
  69. #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  70. /*
  71. * cleanup_srcu_struct - deconstruct a sleep-RCU structure
  72. * @sp: structure to clean up.
  73. *
  74. * Must invoke this after you are finished using a given srcu_struct that
  75. * was initialized via init_srcu_struct(), else you leak memory.
  76. */
  77. void cleanup_srcu_struct(struct srcu_struct *sp)
  78. {
  79. WARN_ON(sp->srcu_lock_nesting[0] || sp->srcu_lock_nesting[1]);
  80. flush_work(&sp->srcu_work);
  81. WARN_ON(rcu_seq_state(sp->srcu_gp_seq));
  82. WARN_ON(sp->srcu_gp_running);
  83. WARN_ON(sp->srcu_gp_waiting);
  84. WARN_ON(!rcu_segcblist_empty(&sp->srcu_cblist));
  85. }
  86. EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
  87. /*
  88. * Counts the new reader in the appropriate per-CPU element of the
  89. * srcu_struct. Must be called from process context.
  90. * Returns an index that must be passed to the matching srcu_read_unlock().
  91. */
  92. int __srcu_read_lock(struct srcu_struct *sp)
  93. {
  94. int idx;
  95. idx = READ_ONCE(sp->srcu_idx);
  96. WRITE_ONCE(sp->srcu_lock_nesting[idx], sp->srcu_lock_nesting[idx] + 1);
  97. return idx;
  98. }
  99. EXPORT_SYMBOL_GPL(__srcu_read_lock);
  100. /*
  101. * Removes the count for the old reader from the appropriate element of
  102. * the srcu_struct. Must be called from process context.
  103. */
  104. void __srcu_read_unlock(struct srcu_struct *sp, int idx)
  105. {
  106. int newval = sp->srcu_lock_nesting[idx] - 1;
  107. WRITE_ONCE(sp->srcu_lock_nesting[idx], newval);
  108. if (!newval && READ_ONCE(sp->srcu_gp_waiting))
  109. swake_up(&sp->srcu_wq);
  110. }
  111. EXPORT_SYMBOL_GPL(__srcu_read_unlock);
  112. /*
  113. * Workqueue handler to drive one grace period and invoke any callbacks
  114. * that become ready as a result. Single-CPU and !PREEMPT operation
  115. * means that we get away with murder on synchronization. ;-)
  116. */
  117. void srcu_drive_gp(struct work_struct *wp)
  118. {
  119. int idx;
  120. struct rcu_cblist ready_cbs;
  121. struct srcu_struct *sp;
  122. struct rcu_head *rhp;
  123. sp = container_of(wp, struct srcu_struct, srcu_work);
  124. if (sp->srcu_gp_running || rcu_segcblist_empty(&sp->srcu_cblist))
  125. return; /* Already running or nothing to do. */
  126. /* Tag recently arrived callbacks and wait for readers. */
  127. WRITE_ONCE(sp->srcu_gp_running, true);
  128. rcu_segcblist_accelerate(&sp->srcu_cblist,
  129. rcu_seq_snap(&sp->srcu_gp_seq));
  130. rcu_seq_start(&sp->srcu_gp_seq);
  131. idx = sp->srcu_idx;
  132. WRITE_ONCE(sp->srcu_idx, !sp->srcu_idx);
  133. WRITE_ONCE(sp->srcu_gp_waiting, true); /* srcu_read_unlock() wakes! */
  134. swait_event(sp->srcu_wq, !READ_ONCE(sp->srcu_lock_nesting[idx]));
  135. WRITE_ONCE(sp->srcu_gp_waiting, false); /* srcu_read_unlock() cheap. */
  136. rcu_seq_end(&sp->srcu_gp_seq);
  137. /* Update callback list based on GP, and invoke ready callbacks. */
  138. rcu_segcblist_advance(&sp->srcu_cblist,
  139. rcu_seq_current(&sp->srcu_gp_seq));
  140. if (rcu_segcblist_ready_cbs(&sp->srcu_cblist)) {
  141. rcu_cblist_init(&ready_cbs);
  142. local_irq_disable();
  143. rcu_segcblist_extract_done_cbs(&sp->srcu_cblist, &ready_cbs);
  144. local_irq_enable();
  145. rhp = rcu_cblist_dequeue(&ready_cbs);
  146. for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) {
  147. local_bh_disable();
  148. rhp->func(rhp);
  149. local_bh_enable();
  150. }
  151. local_irq_disable();
  152. rcu_segcblist_insert_count(&sp->srcu_cblist, &ready_cbs);
  153. local_irq_enable();
  154. }
  155. WRITE_ONCE(sp->srcu_gp_running, false);
  156. /*
  157. * If more callbacks, reschedule ourselves. This can race with
  158. * a call_srcu() at interrupt level, but the ->srcu_gp_running
  159. * checks will straighten that out.
  160. */
  161. if (!rcu_segcblist_empty(&sp->srcu_cblist))
  162. schedule_work(&sp->srcu_work);
  163. }
  164. EXPORT_SYMBOL_GPL(srcu_drive_gp);
  165. /*
  166. * Enqueue an SRCU callback on the specified srcu_struct structure,
  167. * initiating grace-period processing if it is not already running.
  168. */
  169. void call_srcu(struct srcu_struct *sp, struct rcu_head *head,
  170. rcu_callback_t func)
  171. {
  172. unsigned long flags;
  173. head->func = func;
  174. local_irq_save(flags);
  175. rcu_segcblist_enqueue(&sp->srcu_cblist, head, false);
  176. local_irq_restore(flags);
  177. if (!READ_ONCE(sp->srcu_gp_running))
  178. schedule_work(&sp->srcu_work);
  179. }
  180. EXPORT_SYMBOL_GPL(call_srcu);
  181. /*
  182. * synchronize_srcu - wait for prior SRCU read-side critical-section completion
  183. */
  184. void synchronize_srcu(struct srcu_struct *sp)
  185. {
  186. struct rcu_synchronize rs;
  187. init_rcu_head_on_stack(&rs.head);
  188. init_completion(&rs.completion);
  189. call_srcu(sp, &rs.head, wakeme_after_rcu);
  190. wait_for_completion(&rs.completion);
  191. destroy_rcu_head_on_stack(&rs.head);
  192. }
  193. EXPORT_SYMBOL_GPL(synchronize_srcu);