srcu.c 9.7 KB

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