srcutiny.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_segcblist.h"
  32. #include "rcu.h"
  33. static int init_srcu_struct_fields(struct srcu_struct *sp)
  34. {
  35. sp->srcu_lock_nesting[0] = 0;
  36. sp->srcu_lock_nesting[1] = 0;
  37. init_swait_queue_head(&sp->srcu_wq);
  38. sp->srcu_cb_head = NULL;
  39. sp->srcu_cb_tail = &sp->srcu_cb_head;
  40. sp->srcu_gp_running = false;
  41. sp->srcu_gp_waiting = false;
  42. sp->srcu_idx = 0;
  43. INIT_WORK(&sp->srcu_work, srcu_drive_gp);
  44. return 0;
  45. }
  46. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  47. int __init_srcu_struct(struct srcu_struct *sp, const char *name,
  48. struct lock_class_key *key)
  49. {
  50. /* Don't re-initialize a lock while it is held. */
  51. debug_check_no_locks_freed((void *)sp, sizeof(*sp));
  52. lockdep_init_map(&sp->dep_map, name, key, 0);
  53. return init_srcu_struct_fields(sp);
  54. }
  55. EXPORT_SYMBOL_GPL(__init_srcu_struct);
  56. #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  57. /*
  58. * init_srcu_struct - initialize a sleep-RCU structure
  59. * @sp: structure to initialize.
  60. *
  61. * Must invoke this on a given srcu_struct before passing that srcu_struct
  62. * to any other function. Each srcu_struct represents a separate domain
  63. * of SRCU protection.
  64. */
  65. int init_srcu_struct(struct srcu_struct *sp)
  66. {
  67. return init_srcu_struct_fields(sp);
  68. }
  69. EXPORT_SYMBOL_GPL(init_srcu_struct);
  70. #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  71. /*
  72. * cleanup_srcu_struct - deconstruct a sleep-RCU structure
  73. * @sp: structure to clean up.
  74. *
  75. * Must invoke this after you are finished using a given srcu_struct that
  76. * was initialized via init_srcu_struct(), else you leak memory.
  77. */
  78. void cleanup_srcu_struct(struct srcu_struct *sp)
  79. {
  80. WARN_ON(sp->srcu_lock_nesting[0] || sp->srcu_lock_nesting[1]);
  81. flush_work(&sp->srcu_work);
  82. WARN_ON(sp->srcu_gp_running);
  83. WARN_ON(sp->srcu_gp_waiting);
  84. WARN_ON(sp->srcu_cb_head);
  85. WARN_ON(&sp->srcu_cb_head != sp->srcu_cb_tail);
  86. }
  87. EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
  88. /*
  89. * Removes the count for the old reader from the appropriate element of
  90. * the srcu_struct.
  91. */
  92. void __srcu_read_unlock(struct srcu_struct *sp, int idx)
  93. {
  94. int newval = sp->srcu_lock_nesting[idx] - 1;
  95. WRITE_ONCE(sp->srcu_lock_nesting[idx], newval);
  96. if (!newval && READ_ONCE(sp->srcu_gp_waiting))
  97. swake_up(&sp->srcu_wq);
  98. }
  99. EXPORT_SYMBOL_GPL(__srcu_read_unlock);
  100. /*
  101. * Workqueue handler to drive one grace period and invoke any callbacks
  102. * that become ready as a result. Single-CPU and !PREEMPT operation
  103. * means that we get away with murder on synchronization. ;-)
  104. */
  105. void srcu_drive_gp(struct work_struct *wp)
  106. {
  107. int idx;
  108. struct rcu_head *lh;
  109. struct rcu_head *rhp;
  110. struct srcu_struct *sp;
  111. sp = container_of(wp, struct srcu_struct, srcu_work);
  112. if (sp->srcu_gp_running || !READ_ONCE(sp->srcu_cb_head))
  113. return; /* Already running or nothing to do. */
  114. /* Remove recently arrived callbacks and wait for readers. */
  115. WRITE_ONCE(sp->srcu_gp_running, true);
  116. local_irq_disable();
  117. lh = sp->srcu_cb_head;
  118. sp->srcu_cb_head = NULL;
  119. sp->srcu_cb_tail = &sp->srcu_cb_head;
  120. local_irq_enable();
  121. idx = sp->srcu_idx;
  122. WRITE_ONCE(sp->srcu_idx, !sp->srcu_idx);
  123. WRITE_ONCE(sp->srcu_gp_waiting, true); /* srcu_read_unlock() wakes! */
  124. swait_event(sp->srcu_wq, !READ_ONCE(sp->srcu_lock_nesting[idx]));
  125. WRITE_ONCE(sp->srcu_gp_waiting, false); /* srcu_read_unlock() cheap. */
  126. /* Invoke the callbacks we removed above. */
  127. while (lh) {
  128. rhp = lh;
  129. lh = lh->next;
  130. local_bh_disable();
  131. rhp->func(rhp);
  132. local_bh_enable();
  133. }
  134. /*
  135. * Enable rescheduling, and if there are more callbacks,
  136. * reschedule ourselves. This can race with a call_srcu()
  137. * at interrupt level, but the ->srcu_gp_running checks will
  138. * straighten that out.
  139. */
  140. WRITE_ONCE(sp->srcu_gp_running, false);
  141. if (READ_ONCE(sp->srcu_cb_head))
  142. schedule_work(&sp->srcu_work);
  143. }
  144. EXPORT_SYMBOL_GPL(srcu_drive_gp);
  145. /*
  146. * Enqueue an SRCU callback on the specified srcu_struct structure,
  147. * initiating grace-period processing if it is not already running.
  148. */
  149. void call_srcu(struct srcu_struct *sp, struct rcu_head *rhp,
  150. rcu_callback_t func)
  151. {
  152. unsigned long flags;
  153. rhp->func = func;
  154. rhp->next = NULL;
  155. local_irq_save(flags);
  156. *sp->srcu_cb_tail = rhp;
  157. sp->srcu_cb_tail = &rhp->next;
  158. local_irq_restore(flags);
  159. if (!READ_ONCE(sp->srcu_gp_running))
  160. schedule_work(&sp->srcu_work);
  161. }
  162. EXPORT_SYMBOL_GPL(call_srcu);
  163. /*
  164. * synchronize_srcu - wait for prior SRCU read-side critical-section completion
  165. */
  166. void synchronize_srcu(struct srcu_struct *sp)
  167. {
  168. struct rcu_synchronize rs;
  169. init_rcu_head_on_stack(&rs.head);
  170. init_completion(&rs.completion);
  171. call_srcu(sp, &rs.head, wakeme_after_rcu);
  172. wait_for_completion(&rs.completion);
  173. destroy_rcu_head_on_stack(&rs.head);
  174. }
  175. EXPORT_SYMBOL_GPL(synchronize_srcu);