srcu.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Sleepable Read-Copy Update mechanism for mutual exclusion.
  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, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2006
  19. *
  20. * Author: Paul McKenney <paulmck@us.ibm.com>
  21. *
  22. * For detailed explanation of Read-Copy Update mechanism see -
  23. * Documentation/RCU/ *.txt
  24. *
  25. */
  26. #include <linux/module.h>
  27. #include <linux/mutex.h>
  28. #include <linux/percpu.h>
  29. #include <linux/preempt.h>
  30. #include <linux/rcupdate.h>
  31. #include <linux/sched.h>
  32. #include <linux/slab.h>
  33. #include <linux/smp.h>
  34. #include <linux/srcu.h>
  35. static int init_srcu_struct_fields(struct srcu_struct *sp)
  36. {
  37. sp->completed = 0;
  38. mutex_init(&sp->mutex);
  39. sp->per_cpu_ref = alloc_percpu(struct srcu_struct_array);
  40. return sp->per_cpu_ref ? 0 : -ENOMEM;
  41. }
  42. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  43. int __init_srcu_struct(struct srcu_struct *sp, const char *name,
  44. struct lock_class_key *key)
  45. {
  46. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  47. /* Don't re-initialize a lock while it is held. */
  48. debug_check_no_locks_freed((void *)sp, sizeof(*sp));
  49. lockdep_init_map(&sp->dep_map, name, key, 0);
  50. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  51. return init_srcu_struct_fields(sp);
  52. }
  53. EXPORT_SYMBOL_GPL(__init_srcu_struct);
  54. #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  55. /**
  56. * init_srcu_struct - initialize a sleep-RCU structure
  57. * @sp: structure to initialize.
  58. *
  59. * Must invoke this on a given srcu_struct before passing that srcu_struct
  60. * to any other function. Each srcu_struct represents a separate domain
  61. * of SRCU protection.
  62. */
  63. int init_srcu_struct(struct srcu_struct *sp)
  64. {
  65. return init_srcu_struct_fields(sp);
  66. }
  67. EXPORT_SYMBOL_GPL(init_srcu_struct);
  68. #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  69. /*
  70. * srcu_readers_active_idx -- returns approximate number of readers
  71. * active on the specified rank of per-CPU counters.
  72. */
  73. static int srcu_readers_active_idx(struct srcu_struct *sp, int idx)
  74. {
  75. int cpu;
  76. int sum;
  77. sum = 0;
  78. for_each_possible_cpu(cpu)
  79. sum += per_cpu_ptr(sp->per_cpu_ref, cpu)->c[idx];
  80. return sum;
  81. }
  82. /**
  83. * srcu_readers_active - returns approximate number of readers.
  84. * @sp: which srcu_struct to count active readers (holding srcu_read_lock).
  85. *
  86. * Note that this is not an atomic primitive, and can therefore suffer
  87. * severe errors when invoked on an active srcu_struct. That said, it
  88. * can be useful as an error check at cleanup time.
  89. */
  90. static int srcu_readers_active(struct srcu_struct *sp)
  91. {
  92. return srcu_readers_active_idx(sp, 0) + srcu_readers_active_idx(sp, 1);
  93. }
  94. /**
  95. * cleanup_srcu_struct - deconstruct a sleep-RCU structure
  96. * @sp: structure to clean up.
  97. *
  98. * Must invoke this after you are finished using a given srcu_struct that
  99. * was initialized via init_srcu_struct(), else you leak memory.
  100. */
  101. void cleanup_srcu_struct(struct srcu_struct *sp)
  102. {
  103. int sum;
  104. sum = srcu_readers_active(sp);
  105. WARN_ON(sum); /* Leakage unless caller handles error. */
  106. if (sum != 0)
  107. return;
  108. free_percpu(sp->per_cpu_ref);
  109. sp->per_cpu_ref = NULL;
  110. }
  111. EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
  112. /*
  113. * Counts the new reader in the appropriate per-CPU element of the
  114. * srcu_struct. Must be called from process context.
  115. * Returns an index that must be passed to the matching srcu_read_unlock().
  116. */
  117. int __srcu_read_lock(struct srcu_struct *sp)
  118. {
  119. int idx;
  120. preempt_disable();
  121. idx = sp->completed & 0x1;
  122. barrier(); /* ensure compiler looks -once- at sp->completed. */
  123. per_cpu_ptr(sp->per_cpu_ref, smp_processor_id())->c[idx]++;
  124. srcu_barrier(); /* ensure compiler won't misorder critical section. */
  125. preempt_enable();
  126. return idx;
  127. }
  128. EXPORT_SYMBOL_GPL(__srcu_read_lock);
  129. /*
  130. * Removes the count for the old reader from the appropriate per-CPU
  131. * element of the srcu_struct. Note that this may well be a different
  132. * CPU than that which was incremented by the corresponding srcu_read_lock().
  133. * Must be called from process context.
  134. */
  135. void __srcu_read_unlock(struct srcu_struct *sp, int idx)
  136. {
  137. preempt_disable();
  138. srcu_barrier(); /* ensure compiler won't misorder critical section. */
  139. per_cpu_ptr(sp->per_cpu_ref, smp_processor_id())->c[idx]--;
  140. preempt_enable();
  141. }
  142. EXPORT_SYMBOL_GPL(__srcu_read_unlock);
  143. /*
  144. * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
  145. */
  146. static void __synchronize_srcu(struct srcu_struct *sp, void (*sync_func)(void))
  147. {
  148. int idx;
  149. idx = sp->completed;
  150. mutex_lock(&sp->mutex);
  151. /*
  152. * Check to see if someone else did the work for us while we were
  153. * waiting to acquire the lock. We need -two- advances of
  154. * the counter, not just one. If there was but one, we might have
  155. * shown up -after- our helper's first synchronize_sched(), thus
  156. * having failed to prevent CPU-reordering races with concurrent
  157. * srcu_read_unlock()s on other CPUs (see comment below). So we
  158. * either (1) wait for two or (2) supply the second ourselves.
  159. */
  160. if ((sp->completed - idx) >= 2) {
  161. mutex_unlock(&sp->mutex);
  162. return;
  163. }
  164. sync_func(); /* Force memory barrier on all CPUs. */
  165. /*
  166. * The preceding synchronize_sched() ensures that any CPU that
  167. * sees the new value of sp->completed will also see any preceding
  168. * changes to data structures made by this CPU. This prevents
  169. * some other CPU from reordering the accesses in its SRCU
  170. * read-side critical section to precede the corresponding
  171. * srcu_read_lock() -- ensuring that such references will in
  172. * fact be protected.
  173. *
  174. * So it is now safe to do the flip.
  175. */
  176. idx = sp->completed & 0x1;
  177. sp->completed++;
  178. sync_func(); /* Force memory barrier on all CPUs. */
  179. /*
  180. * At this point, because of the preceding synchronize_sched(),
  181. * all srcu_read_lock() calls using the old counters have completed.
  182. * Their corresponding critical sections might well be still
  183. * executing, but the srcu_read_lock() primitives themselves
  184. * will have finished executing.
  185. */
  186. while (srcu_readers_active_idx(sp, idx))
  187. schedule_timeout_interruptible(1);
  188. sync_func(); /* Force memory barrier on all CPUs. */
  189. /*
  190. * The preceding synchronize_sched() forces all srcu_read_unlock()
  191. * primitives that were executing concurrently with the preceding
  192. * for_each_possible_cpu() loop to have completed by this point.
  193. * More importantly, it also forces the corresponding SRCU read-side
  194. * critical sections to have also completed, and the corresponding
  195. * references to SRCU-protected data items to be dropped.
  196. *
  197. * Note:
  198. *
  199. * Despite what you might think at first glance, the
  200. * preceding synchronize_sched() -must- be within the
  201. * critical section ended by the following mutex_unlock().
  202. * Otherwise, a task taking the early exit can race
  203. * with a srcu_read_unlock(), which might have executed
  204. * just before the preceding srcu_readers_active() check,
  205. * and whose CPU might have reordered the srcu_read_unlock()
  206. * with the preceding critical section. In this case, there
  207. * is nothing preventing the synchronize_sched() task that is
  208. * taking the early exit from freeing a data structure that
  209. * is still being referenced (out of order) by the task
  210. * doing the srcu_read_unlock().
  211. *
  212. * Alternatively, the comparison with "2" on the early exit
  213. * could be changed to "3", but this increases synchronize_srcu()
  214. * latency for bulk loads. So the current code is preferred.
  215. */
  216. mutex_unlock(&sp->mutex);
  217. }
  218. /**
  219. * synchronize_srcu - wait for prior SRCU read-side critical-section completion
  220. * @sp: srcu_struct with which to synchronize.
  221. *
  222. * Flip the completed counter, and wait for the old count to drain to zero.
  223. * As with classic RCU, the updater must use some separate means of
  224. * synchronizing concurrent updates. Can block; must be called from
  225. * process context.
  226. *
  227. * Note that it is illegal to call synchronize_srcu() from the corresponding
  228. * SRCU read-side critical section; doing so will result in deadlock.
  229. * However, it is perfectly legal to call synchronize_srcu() on one
  230. * srcu_struct from some other srcu_struct's read-side critical section.
  231. */
  232. void synchronize_srcu(struct srcu_struct *sp)
  233. {
  234. __synchronize_srcu(sp, synchronize_sched);
  235. }
  236. EXPORT_SYMBOL_GPL(synchronize_srcu);
  237. /**
  238. * synchronize_srcu_expedited - like synchronize_srcu, but less patient
  239. * @sp: srcu_struct with which to synchronize.
  240. *
  241. * Flip the completed counter, and wait for the old count to drain to zero.
  242. * As with classic RCU, the updater must use some separate means of
  243. * synchronizing concurrent updates. Can block; must be called from
  244. * process context.
  245. *
  246. * Note that it is illegal to call synchronize_srcu_expedited()
  247. * from the corresponding SRCU read-side critical section; doing so
  248. * will result in deadlock. However, it is perfectly legal to call
  249. * synchronize_srcu_expedited() on one srcu_struct from some other
  250. * srcu_struct's read-side critical section.
  251. */
  252. void synchronize_srcu_expedited(struct srcu_struct *sp)
  253. {
  254. __synchronize_srcu(sp, synchronize_sched_expedited);
  255. }
  256. EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
  257. /**
  258. * srcu_batches_completed - return batches completed.
  259. * @sp: srcu_struct on which to report batch completion.
  260. *
  261. * Report the number of batches, correlated with, but not necessarily
  262. * precisely the same as, the number of grace periods that have elapsed.
  263. */
  264. long srcu_batches_completed(struct srcu_struct *sp)
  265. {
  266. return sp->completed;
  267. }
  268. EXPORT_SYMBOL_GPL(srcu_batches_completed);