srcutree.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  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, you can access it online at
  16. * http://www.gnu.org/licenses/gpl-2.0.html.
  17. *
  18. * Copyright (C) IBM Corporation, 2006
  19. * Copyright (C) Fujitsu, 2012
  20. *
  21. * Author: Paul McKenney <paulmck@us.ibm.com>
  22. * Lai Jiangshan <laijs@cn.fujitsu.com>
  23. *
  24. * For detailed explanation of Read-Copy Update mechanism see -
  25. * Documentation/RCU/ *.txt
  26. *
  27. */
  28. #include <linux/export.h>
  29. #include <linux/mutex.h>
  30. #include <linux/percpu.h>
  31. #include <linux/preempt.h>
  32. #include <linux/rcupdate_wait.h>
  33. #include <linux/sched.h>
  34. #include <linux/smp.h>
  35. #include <linux/delay.h>
  36. #include <linux/module.h>
  37. #include <linux/srcu.h>
  38. #include "rcu.h"
  39. #include "rcu_segcblist.h"
  40. /* Holdoff in nanoseconds for auto-expediting. */
  41. #define DEFAULT_SRCU_EXP_HOLDOFF (25 * 1000)
  42. static ulong exp_holdoff = DEFAULT_SRCU_EXP_HOLDOFF;
  43. module_param(exp_holdoff, ulong, 0444);
  44. /* Overflow-check frequency. N bits roughly says every 2**N grace periods. */
  45. static ulong counter_wrap_check = (ULONG_MAX >> 2);
  46. module_param(counter_wrap_check, ulong, 0444);
  47. static void srcu_invoke_callbacks(struct work_struct *work);
  48. static void srcu_reschedule(struct srcu_struct *sp, unsigned long delay);
  49. static void process_srcu(struct work_struct *work);
  50. /* Wrappers for lock acquisition and release, see raw_spin_lock_rcu_node(). */
  51. #define spin_lock_rcu_node(p) \
  52. do { \
  53. spin_lock(&ACCESS_PRIVATE(p, lock)); \
  54. smp_mb__after_unlock_lock(); \
  55. } while (0)
  56. #define spin_unlock_rcu_node(p) spin_unlock(&ACCESS_PRIVATE(p, lock))
  57. #define spin_lock_irq_rcu_node(p) \
  58. do { \
  59. spin_lock_irq(&ACCESS_PRIVATE(p, lock)); \
  60. smp_mb__after_unlock_lock(); \
  61. } while (0)
  62. #define spin_unlock_irq_rcu_node(p) \
  63. spin_unlock_irq(&ACCESS_PRIVATE(p, lock))
  64. #define spin_lock_irqsave_rcu_node(p, flags) \
  65. do { \
  66. spin_lock_irqsave(&ACCESS_PRIVATE(p, lock), flags); \
  67. smp_mb__after_unlock_lock(); \
  68. } while (0)
  69. #define spin_unlock_irqrestore_rcu_node(p, flags) \
  70. spin_unlock_irqrestore(&ACCESS_PRIVATE(p, lock), flags) \
  71. /*
  72. * Initialize SRCU combining tree. Note that statically allocated
  73. * srcu_struct structures might already have srcu_read_lock() and
  74. * srcu_read_unlock() running against them. So if the is_static parameter
  75. * is set, don't initialize ->srcu_lock_count[] and ->srcu_unlock_count[].
  76. */
  77. static void init_srcu_struct_nodes(struct srcu_struct *sp, bool is_static)
  78. {
  79. int cpu;
  80. int i;
  81. int level = 0;
  82. int levelspread[RCU_NUM_LVLS];
  83. struct srcu_data *sdp;
  84. struct srcu_node *snp;
  85. struct srcu_node *snp_first;
  86. /* Work out the overall tree geometry. */
  87. sp->level[0] = &sp->node[0];
  88. for (i = 1; i < rcu_num_lvls; i++)
  89. sp->level[i] = sp->level[i - 1] + num_rcu_lvl[i - 1];
  90. rcu_init_levelspread(levelspread, num_rcu_lvl);
  91. /* Each pass through this loop initializes one srcu_node structure. */
  92. rcu_for_each_node_breadth_first(sp, snp) {
  93. spin_lock_init(&ACCESS_PRIVATE(snp, lock));
  94. WARN_ON_ONCE(ARRAY_SIZE(snp->srcu_have_cbs) !=
  95. ARRAY_SIZE(snp->srcu_data_have_cbs));
  96. for (i = 0; i < ARRAY_SIZE(snp->srcu_have_cbs); i++) {
  97. snp->srcu_have_cbs[i] = 0;
  98. snp->srcu_data_have_cbs[i] = 0;
  99. }
  100. snp->srcu_gp_seq_needed_exp = 0;
  101. snp->grplo = -1;
  102. snp->grphi = -1;
  103. if (snp == &sp->node[0]) {
  104. /* Root node, special case. */
  105. snp->srcu_parent = NULL;
  106. continue;
  107. }
  108. /* Non-root node. */
  109. if (snp == sp->level[level + 1])
  110. level++;
  111. snp->srcu_parent = sp->level[level - 1] +
  112. (snp - sp->level[level]) /
  113. levelspread[level - 1];
  114. }
  115. /*
  116. * Initialize the per-CPU srcu_data array, which feeds into the
  117. * leaves of the srcu_node tree.
  118. */
  119. WARN_ON_ONCE(ARRAY_SIZE(sdp->srcu_lock_count) !=
  120. ARRAY_SIZE(sdp->srcu_unlock_count));
  121. level = rcu_num_lvls - 1;
  122. snp_first = sp->level[level];
  123. for_each_possible_cpu(cpu) {
  124. sdp = per_cpu_ptr(sp->sda, cpu);
  125. spin_lock_init(&ACCESS_PRIVATE(sdp, lock));
  126. rcu_segcblist_init(&sdp->srcu_cblist);
  127. sdp->srcu_cblist_invoking = false;
  128. sdp->srcu_gp_seq_needed = sp->srcu_gp_seq;
  129. sdp->srcu_gp_seq_needed_exp = sp->srcu_gp_seq;
  130. sdp->mynode = &snp_first[cpu / levelspread[level]];
  131. for (snp = sdp->mynode; snp != NULL; snp = snp->srcu_parent) {
  132. if (snp->grplo < 0)
  133. snp->grplo = cpu;
  134. snp->grphi = cpu;
  135. }
  136. sdp->cpu = cpu;
  137. INIT_DELAYED_WORK(&sdp->work, srcu_invoke_callbacks);
  138. sdp->sp = sp;
  139. sdp->grpmask = 1 << (cpu - sdp->mynode->grplo);
  140. if (is_static)
  141. continue;
  142. /* Dynamically allocated, better be no srcu_read_locks()! */
  143. for (i = 0; i < ARRAY_SIZE(sdp->srcu_lock_count); i++) {
  144. sdp->srcu_lock_count[i] = 0;
  145. sdp->srcu_unlock_count[i] = 0;
  146. }
  147. }
  148. }
  149. /*
  150. * Initialize non-compile-time initialized fields, including the
  151. * associated srcu_node and srcu_data structures. The is_static
  152. * parameter is passed through to init_srcu_struct_nodes(), and
  153. * also tells us that ->sda has already been wired up to srcu_data.
  154. */
  155. static int init_srcu_struct_fields(struct srcu_struct *sp, bool is_static)
  156. {
  157. mutex_init(&sp->srcu_cb_mutex);
  158. mutex_init(&sp->srcu_gp_mutex);
  159. sp->srcu_idx = 0;
  160. sp->srcu_gp_seq = 0;
  161. sp->srcu_barrier_seq = 0;
  162. mutex_init(&sp->srcu_barrier_mutex);
  163. atomic_set(&sp->srcu_barrier_cpu_cnt, 0);
  164. INIT_DELAYED_WORK(&sp->work, process_srcu);
  165. if (!is_static)
  166. sp->sda = alloc_percpu(struct srcu_data);
  167. init_srcu_struct_nodes(sp, is_static);
  168. sp->srcu_gp_seq_needed_exp = 0;
  169. sp->srcu_last_gp_end = ktime_get_mono_fast_ns();
  170. smp_store_release(&sp->srcu_gp_seq_needed, 0); /* Init done. */
  171. return sp->sda ? 0 : -ENOMEM;
  172. }
  173. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  174. int __init_srcu_struct(struct srcu_struct *sp, const char *name,
  175. struct lock_class_key *key)
  176. {
  177. /* Don't re-initialize a lock while it is held. */
  178. debug_check_no_locks_freed((void *)sp, sizeof(*sp));
  179. lockdep_init_map(&sp->dep_map, name, key, 0);
  180. spin_lock_init(&ACCESS_PRIVATE(sp, lock));
  181. return init_srcu_struct_fields(sp, false);
  182. }
  183. EXPORT_SYMBOL_GPL(__init_srcu_struct);
  184. #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  185. /**
  186. * init_srcu_struct - initialize a sleep-RCU structure
  187. * @sp: structure to initialize.
  188. *
  189. * Must invoke this on a given srcu_struct before passing that srcu_struct
  190. * to any other function. Each srcu_struct represents a separate domain
  191. * of SRCU protection.
  192. */
  193. int init_srcu_struct(struct srcu_struct *sp)
  194. {
  195. spin_lock_init(&ACCESS_PRIVATE(sp, lock));
  196. return init_srcu_struct_fields(sp, false);
  197. }
  198. EXPORT_SYMBOL_GPL(init_srcu_struct);
  199. #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  200. /*
  201. * First-use initialization of statically allocated srcu_struct
  202. * structure. Wiring up the combining tree is more than can be
  203. * done with compile-time initialization, so this check is added
  204. * to each update-side SRCU primitive. Use sp->lock, which -is-
  205. * compile-time initialized, to resolve races involving multiple
  206. * CPUs trying to garner first-use privileges.
  207. */
  208. static void check_init_srcu_struct(struct srcu_struct *sp)
  209. {
  210. unsigned long flags;
  211. WARN_ON_ONCE(rcu_scheduler_active == RCU_SCHEDULER_INIT);
  212. /* The smp_load_acquire() pairs with the smp_store_release(). */
  213. if (!rcu_seq_state(smp_load_acquire(&sp->srcu_gp_seq_needed))) /*^^^*/
  214. return; /* Already initialized. */
  215. spin_lock_irqsave_rcu_node(sp, flags);
  216. if (!rcu_seq_state(sp->srcu_gp_seq_needed)) {
  217. spin_unlock_irqrestore_rcu_node(sp, flags);
  218. return;
  219. }
  220. init_srcu_struct_fields(sp, true);
  221. spin_unlock_irqrestore_rcu_node(sp, flags);
  222. }
  223. /*
  224. * Returns approximate total of the readers' ->srcu_lock_count[] values
  225. * for the rank of per-CPU counters specified by idx.
  226. */
  227. static unsigned long srcu_readers_lock_idx(struct srcu_struct *sp, int idx)
  228. {
  229. int cpu;
  230. unsigned long sum = 0;
  231. for_each_possible_cpu(cpu) {
  232. struct srcu_data *cpuc = per_cpu_ptr(sp->sda, cpu);
  233. sum += READ_ONCE(cpuc->srcu_lock_count[idx]);
  234. }
  235. return sum;
  236. }
  237. /*
  238. * Returns approximate total of the readers' ->srcu_unlock_count[] values
  239. * for the rank of per-CPU counters specified by idx.
  240. */
  241. static unsigned long srcu_readers_unlock_idx(struct srcu_struct *sp, int idx)
  242. {
  243. int cpu;
  244. unsigned long sum = 0;
  245. for_each_possible_cpu(cpu) {
  246. struct srcu_data *cpuc = per_cpu_ptr(sp->sda, cpu);
  247. sum += READ_ONCE(cpuc->srcu_unlock_count[idx]);
  248. }
  249. return sum;
  250. }
  251. /*
  252. * Return true if the number of pre-existing readers is determined to
  253. * be zero.
  254. */
  255. static bool srcu_readers_active_idx_check(struct srcu_struct *sp, int idx)
  256. {
  257. unsigned long unlocks;
  258. unlocks = srcu_readers_unlock_idx(sp, idx);
  259. /*
  260. * Make sure that a lock is always counted if the corresponding
  261. * unlock is counted. Needs to be a smp_mb() as the read side may
  262. * contain a read from a variable that is written to before the
  263. * synchronize_srcu() in the write side. In this case smp_mb()s
  264. * A and B act like the store buffering pattern.
  265. *
  266. * This smp_mb() also pairs with smp_mb() C to prevent accesses
  267. * after the synchronize_srcu() from being executed before the
  268. * grace period ends.
  269. */
  270. smp_mb(); /* A */
  271. /*
  272. * If the locks are the same as the unlocks, then there must have
  273. * been no readers on this index at some time in between. This does
  274. * not mean that there are no more readers, as one could have read
  275. * the current index but not have incremented the lock counter yet.
  276. *
  277. * So suppose that the updater is preempted here for so long
  278. * that more than ULONG_MAX non-nested readers come and go in
  279. * the meantime. It turns out that this cannot result in overflow
  280. * because if a reader modifies its unlock count after we read it
  281. * above, then that reader's next load of ->srcu_idx is guaranteed
  282. * to get the new value, which will cause it to operate on the
  283. * other bank of counters, where it cannot contribute to the
  284. * overflow of these counters. This means that there is a maximum
  285. * of 2*NR_CPUS increments, which cannot overflow given current
  286. * systems, especially not on 64-bit systems.
  287. *
  288. * OK, how about nesting? This does impose a limit on nesting
  289. * of floor(ULONG_MAX/NR_CPUS/2), which should be sufficient,
  290. * especially on 64-bit systems.
  291. */
  292. return srcu_readers_lock_idx(sp, idx) == unlocks;
  293. }
  294. /**
  295. * srcu_readers_active - returns true if there are readers. and false
  296. * otherwise
  297. * @sp: which srcu_struct to count active readers (holding srcu_read_lock).
  298. *
  299. * Note that this is not an atomic primitive, and can therefore suffer
  300. * severe errors when invoked on an active srcu_struct. That said, it
  301. * can be useful as an error check at cleanup time.
  302. */
  303. static bool srcu_readers_active(struct srcu_struct *sp)
  304. {
  305. int cpu;
  306. unsigned long sum = 0;
  307. for_each_possible_cpu(cpu) {
  308. struct srcu_data *cpuc = per_cpu_ptr(sp->sda, cpu);
  309. sum += READ_ONCE(cpuc->srcu_lock_count[0]);
  310. sum += READ_ONCE(cpuc->srcu_lock_count[1]);
  311. sum -= READ_ONCE(cpuc->srcu_unlock_count[0]);
  312. sum -= READ_ONCE(cpuc->srcu_unlock_count[1]);
  313. }
  314. return sum;
  315. }
  316. #define SRCU_INTERVAL 1
  317. /*
  318. * Return grace-period delay, zero if there are expedited grace
  319. * periods pending, SRCU_INTERVAL otherwise.
  320. */
  321. static unsigned long srcu_get_delay(struct srcu_struct *sp)
  322. {
  323. if (ULONG_CMP_LT(READ_ONCE(sp->srcu_gp_seq),
  324. READ_ONCE(sp->srcu_gp_seq_needed_exp)))
  325. return 0;
  326. return SRCU_INTERVAL;
  327. }
  328. /* Helper for cleanup_srcu_struct() and cleanup_srcu_struct_quiesced(). */
  329. void _cleanup_srcu_struct(struct srcu_struct *sp, bool quiesced)
  330. {
  331. int cpu;
  332. if (WARN_ON(!srcu_get_delay(sp)))
  333. return; /* Just leak it! */
  334. if (WARN_ON(srcu_readers_active(sp)))
  335. return; /* Just leak it! */
  336. if (quiesced) {
  337. if (WARN_ON(delayed_work_pending(&sp->work)))
  338. return; /* Just leak it! */
  339. } else {
  340. flush_delayed_work(&sp->work);
  341. }
  342. for_each_possible_cpu(cpu)
  343. if (quiesced) {
  344. if (WARN_ON(delayed_work_pending(&per_cpu_ptr(sp->sda, cpu)->work)))
  345. return; /* Just leak it! */
  346. } else {
  347. flush_delayed_work(&per_cpu_ptr(sp->sda, cpu)->work);
  348. }
  349. if (WARN_ON(rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) != SRCU_STATE_IDLE) ||
  350. WARN_ON(srcu_readers_active(sp))) {
  351. pr_info("%s: Active srcu_struct %p state: %d\n", __func__, sp, rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)));
  352. return; /* Caller forgot to stop doing call_srcu()? */
  353. }
  354. free_percpu(sp->sda);
  355. sp->sda = NULL;
  356. }
  357. EXPORT_SYMBOL_GPL(_cleanup_srcu_struct);
  358. /*
  359. * Counts the new reader in the appropriate per-CPU element of the
  360. * srcu_struct.
  361. * Returns an index that must be passed to the matching srcu_read_unlock().
  362. */
  363. int __srcu_read_lock(struct srcu_struct *sp)
  364. {
  365. int idx;
  366. idx = READ_ONCE(sp->srcu_idx) & 0x1;
  367. this_cpu_inc(sp->sda->srcu_lock_count[idx]);
  368. smp_mb(); /* B */ /* Avoid leaking the critical section. */
  369. return idx;
  370. }
  371. EXPORT_SYMBOL_GPL(__srcu_read_lock);
  372. /*
  373. * Removes the count for the old reader from the appropriate per-CPU
  374. * element of the srcu_struct. Note that this may well be a different
  375. * CPU than that which was incremented by the corresponding srcu_read_lock().
  376. */
  377. void __srcu_read_unlock(struct srcu_struct *sp, int idx)
  378. {
  379. smp_mb(); /* C */ /* Avoid leaking the critical section. */
  380. this_cpu_inc(sp->sda->srcu_unlock_count[idx]);
  381. }
  382. EXPORT_SYMBOL_GPL(__srcu_read_unlock);
  383. /*
  384. * We use an adaptive strategy for synchronize_srcu() and especially for
  385. * synchronize_srcu_expedited(). We spin for a fixed time period
  386. * (defined below) to allow SRCU readers to exit their read-side critical
  387. * sections. If there are still some readers after a few microseconds,
  388. * we repeatedly block for 1-millisecond time periods.
  389. */
  390. #define SRCU_RETRY_CHECK_DELAY 5
  391. /*
  392. * Start an SRCU grace period.
  393. */
  394. static void srcu_gp_start(struct srcu_struct *sp)
  395. {
  396. struct srcu_data *sdp = this_cpu_ptr(sp->sda);
  397. int state;
  398. lockdep_assert_held(&ACCESS_PRIVATE(sp, lock));
  399. WARN_ON_ONCE(ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed));
  400. rcu_segcblist_advance(&sdp->srcu_cblist,
  401. rcu_seq_current(&sp->srcu_gp_seq));
  402. (void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
  403. rcu_seq_snap(&sp->srcu_gp_seq));
  404. smp_mb(); /* Order prior store to ->srcu_gp_seq_needed vs. GP start. */
  405. rcu_seq_start(&sp->srcu_gp_seq);
  406. state = rcu_seq_state(READ_ONCE(sp->srcu_gp_seq));
  407. WARN_ON_ONCE(state != SRCU_STATE_SCAN1);
  408. }
  409. /*
  410. * Track online CPUs to guide callback workqueue placement.
  411. */
  412. DEFINE_PER_CPU(bool, srcu_online);
  413. void srcu_online_cpu(unsigned int cpu)
  414. {
  415. WRITE_ONCE(per_cpu(srcu_online, cpu), true);
  416. }
  417. void srcu_offline_cpu(unsigned int cpu)
  418. {
  419. WRITE_ONCE(per_cpu(srcu_online, cpu), false);
  420. }
  421. /*
  422. * Place the workqueue handler on the specified CPU if online, otherwise
  423. * just run it whereever. This is useful for placing workqueue handlers
  424. * that are to invoke the specified CPU's callbacks.
  425. */
  426. static bool srcu_queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  427. struct delayed_work *dwork,
  428. unsigned long delay)
  429. {
  430. bool ret;
  431. preempt_disable();
  432. if (READ_ONCE(per_cpu(srcu_online, cpu)))
  433. ret = queue_delayed_work_on(cpu, wq, dwork, delay);
  434. else
  435. ret = queue_delayed_work(wq, dwork, delay);
  436. preempt_enable();
  437. return ret;
  438. }
  439. /*
  440. * Schedule callback invocation for the specified srcu_data structure,
  441. * if possible, on the corresponding CPU.
  442. */
  443. static void srcu_schedule_cbs_sdp(struct srcu_data *sdp, unsigned long delay)
  444. {
  445. srcu_queue_delayed_work_on(sdp->cpu, rcu_gp_wq, &sdp->work, delay);
  446. }
  447. /*
  448. * Schedule callback invocation for all srcu_data structures associated
  449. * with the specified srcu_node structure that have callbacks for the
  450. * just-completed grace period, the one corresponding to idx. If possible,
  451. * schedule this invocation on the corresponding CPUs.
  452. */
  453. static void srcu_schedule_cbs_snp(struct srcu_struct *sp, struct srcu_node *snp,
  454. unsigned long mask, unsigned long delay)
  455. {
  456. int cpu;
  457. for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) {
  458. if (!(mask & (1 << (cpu - snp->grplo))))
  459. continue;
  460. srcu_schedule_cbs_sdp(per_cpu_ptr(sp->sda, cpu), delay);
  461. }
  462. }
  463. /*
  464. * Note the end of an SRCU grace period. Initiates callback invocation
  465. * and starts a new grace period if needed.
  466. *
  467. * The ->srcu_cb_mutex acquisition does not protect any data, but
  468. * instead prevents more than one grace period from starting while we
  469. * are initiating callback invocation. This allows the ->srcu_have_cbs[]
  470. * array to have a finite number of elements.
  471. */
  472. static void srcu_gp_end(struct srcu_struct *sp)
  473. {
  474. unsigned long cbdelay;
  475. bool cbs;
  476. bool last_lvl;
  477. int cpu;
  478. unsigned long flags;
  479. unsigned long gpseq;
  480. int idx;
  481. unsigned long mask;
  482. struct srcu_data *sdp;
  483. struct srcu_node *snp;
  484. /* Prevent more than one additional grace period. */
  485. mutex_lock(&sp->srcu_cb_mutex);
  486. /* End the current grace period. */
  487. spin_lock_irq_rcu_node(sp);
  488. idx = rcu_seq_state(sp->srcu_gp_seq);
  489. WARN_ON_ONCE(idx != SRCU_STATE_SCAN2);
  490. cbdelay = srcu_get_delay(sp);
  491. sp->srcu_last_gp_end = ktime_get_mono_fast_ns();
  492. rcu_seq_end(&sp->srcu_gp_seq);
  493. gpseq = rcu_seq_current(&sp->srcu_gp_seq);
  494. if (ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, gpseq))
  495. sp->srcu_gp_seq_needed_exp = gpseq;
  496. spin_unlock_irq_rcu_node(sp);
  497. mutex_unlock(&sp->srcu_gp_mutex);
  498. /* A new grace period can start at this point. But only one. */
  499. /* Initiate callback invocation as needed. */
  500. idx = rcu_seq_ctr(gpseq) % ARRAY_SIZE(snp->srcu_have_cbs);
  501. rcu_for_each_node_breadth_first(sp, snp) {
  502. spin_lock_irq_rcu_node(snp);
  503. cbs = false;
  504. last_lvl = snp >= sp->level[rcu_num_lvls - 1];
  505. if (last_lvl)
  506. cbs = snp->srcu_have_cbs[idx] == gpseq;
  507. snp->srcu_have_cbs[idx] = gpseq;
  508. rcu_seq_set_state(&snp->srcu_have_cbs[idx], 1);
  509. if (ULONG_CMP_LT(snp->srcu_gp_seq_needed_exp, gpseq))
  510. snp->srcu_gp_seq_needed_exp = gpseq;
  511. mask = snp->srcu_data_have_cbs[idx];
  512. snp->srcu_data_have_cbs[idx] = 0;
  513. spin_unlock_irq_rcu_node(snp);
  514. if (cbs)
  515. srcu_schedule_cbs_snp(sp, snp, mask, cbdelay);
  516. /* Occasionally prevent srcu_data counter wrap. */
  517. if (!(gpseq & counter_wrap_check) && last_lvl)
  518. for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) {
  519. sdp = per_cpu_ptr(sp->sda, cpu);
  520. spin_lock_irqsave_rcu_node(sdp, flags);
  521. if (ULONG_CMP_GE(gpseq,
  522. sdp->srcu_gp_seq_needed + 100))
  523. sdp->srcu_gp_seq_needed = gpseq;
  524. if (ULONG_CMP_GE(gpseq,
  525. sdp->srcu_gp_seq_needed_exp + 100))
  526. sdp->srcu_gp_seq_needed_exp = gpseq;
  527. spin_unlock_irqrestore_rcu_node(sdp, flags);
  528. }
  529. }
  530. /* Callback initiation done, allow grace periods after next. */
  531. mutex_unlock(&sp->srcu_cb_mutex);
  532. /* Start a new grace period if needed. */
  533. spin_lock_irq_rcu_node(sp);
  534. gpseq = rcu_seq_current(&sp->srcu_gp_seq);
  535. if (!rcu_seq_state(gpseq) &&
  536. ULONG_CMP_LT(gpseq, sp->srcu_gp_seq_needed)) {
  537. srcu_gp_start(sp);
  538. spin_unlock_irq_rcu_node(sp);
  539. srcu_reschedule(sp, 0);
  540. } else {
  541. spin_unlock_irq_rcu_node(sp);
  542. }
  543. }
  544. /*
  545. * Funnel-locking scheme to scalably mediate many concurrent expedited
  546. * grace-period requests. This function is invoked for the first known
  547. * expedited request for a grace period that has already been requested,
  548. * but without expediting. To start a completely new grace period,
  549. * whether expedited or not, use srcu_funnel_gp_start() instead.
  550. */
  551. static void srcu_funnel_exp_start(struct srcu_struct *sp, struct srcu_node *snp,
  552. unsigned long s)
  553. {
  554. unsigned long flags;
  555. for (; snp != NULL; snp = snp->srcu_parent) {
  556. if (rcu_seq_done(&sp->srcu_gp_seq, s) ||
  557. ULONG_CMP_GE(READ_ONCE(snp->srcu_gp_seq_needed_exp), s))
  558. return;
  559. spin_lock_irqsave_rcu_node(snp, flags);
  560. if (ULONG_CMP_GE(snp->srcu_gp_seq_needed_exp, s)) {
  561. spin_unlock_irqrestore_rcu_node(snp, flags);
  562. return;
  563. }
  564. WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s);
  565. spin_unlock_irqrestore_rcu_node(snp, flags);
  566. }
  567. spin_lock_irqsave_rcu_node(sp, flags);
  568. if (ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, s))
  569. sp->srcu_gp_seq_needed_exp = s;
  570. spin_unlock_irqrestore_rcu_node(sp, flags);
  571. }
  572. /*
  573. * Funnel-locking scheme to scalably mediate many concurrent grace-period
  574. * requests. The winner has to do the work of actually starting grace
  575. * period s. Losers must either ensure that their desired grace-period
  576. * number is recorded on at least their leaf srcu_node structure, or they
  577. * must take steps to invoke their own callbacks.
  578. */
  579. static void srcu_funnel_gp_start(struct srcu_struct *sp, struct srcu_data *sdp,
  580. unsigned long s, bool do_norm)
  581. {
  582. unsigned long flags;
  583. int idx = rcu_seq_ctr(s) % ARRAY_SIZE(sdp->mynode->srcu_have_cbs);
  584. struct srcu_node *snp = sdp->mynode;
  585. unsigned long snp_seq;
  586. /* Each pass through the loop does one level of the srcu_node tree. */
  587. for (; snp != NULL; snp = snp->srcu_parent) {
  588. if (rcu_seq_done(&sp->srcu_gp_seq, s) && snp != sdp->mynode)
  589. return; /* GP already done and CBs recorded. */
  590. spin_lock_irqsave_rcu_node(snp, flags);
  591. if (ULONG_CMP_GE(snp->srcu_have_cbs[idx], s)) {
  592. snp_seq = snp->srcu_have_cbs[idx];
  593. if (snp == sdp->mynode && snp_seq == s)
  594. snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
  595. spin_unlock_irqrestore_rcu_node(snp, flags);
  596. if (snp == sdp->mynode && snp_seq != s) {
  597. srcu_schedule_cbs_sdp(sdp, do_norm
  598. ? SRCU_INTERVAL
  599. : 0);
  600. return;
  601. }
  602. if (!do_norm)
  603. srcu_funnel_exp_start(sp, snp, s);
  604. return;
  605. }
  606. snp->srcu_have_cbs[idx] = s;
  607. if (snp == sdp->mynode)
  608. snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
  609. if (!do_norm && ULONG_CMP_LT(snp->srcu_gp_seq_needed_exp, s))
  610. snp->srcu_gp_seq_needed_exp = s;
  611. spin_unlock_irqrestore_rcu_node(snp, flags);
  612. }
  613. /* Top of tree, must ensure the grace period will be started. */
  614. spin_lock_irqsave_rcu_node(sp, flags);
  615. if (ULONG_CMP_LT(sp->srcu_gp_seq_needed, s)) {
  616. /*
  617. * Record need for grace period s. Pair with load
  618. * acquire setting up for initialization.
  619. */
  620. smp_store_release(&sp->srcu_gp_seq_needed, s); /*^^^*/
  621. }
  622. if (!do_norm && ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, s))
  623. sp->srcu_gp_seq_needed_exp = s;
  624. /* If grace period not already done and none in progress, start it. */
  625. if (!rcu_seq_done(&sp->srcu_gp_seq, s) &&
  626. rcu_seq_state(sp->srcu_gp_seq) == SRCU_STATE_IDLE) {
  627. WARN_ON_ONCE(ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed));
  628. srcu_gp_start(sp);
  629. queue_delayed_work(rcu_gp_wq, &sp->work, srcu_get_delay(sp));
  630. }
  631. spin_unlock_irqrestore_rcu_node(sp, flags);
  632. }
  633. /*
  634. * Wait until all readers counted by array index idx complete, but
  635. * loop an additional time if there is an expedited grace period pending.
  636. * The caller must ensure that ->srcu_idx is not changed while checking.
  637. */
  638. static bool try_check_zero(struct srcu_struct *sp, int idx, int trycount)
  639. {
  640. for (;;) {
  641. if (srcu_readers_active_idx_check(sp, idx))
  642. return true;
  643. if (--trycount + !srcu_get_delay(sp) <= 0)
  644. return false;
  645. udelay(SRCU_RETRY_CHECK_DELAY);
  646. }
  647. }
  648. /*
  649. * Increment the ->srcu_idx counter so that future SRCU readers will
  650. * use the other rank of the ->srcu_(un)lock_count[] arrays. This allows
  651. * us to wait for pre-existing readers in a starvation-free manner.
  652. */
  653. static void srcu_flip(struct srcu_struct *sp)
  654. {
  655. /*
  656. * Ensure that if this updater saw a given reader's increment
  657. * from __srcu_read_lock(), that reader was using an old value
  658. * of ->srcu_idx. Also ensure that if a given reader sees the
  659. * new value of ->srcu_idx, this updater's earlier scans cannot
  660. * have seen that reader's increments (which is OK, because this
  661. * grace period need not wait on that reader).
  662. */
  663. smp_mb(); /* E */ /* Pairs with B and C. */
  664. WRITE_ONCE(sp->srcu_idx, sp->srcu_idx + 1);
  665. /*
  666. * Ensure that if the updater misses an __srcu_read_unlock()
  667. * increment, that task's next __srcu_read_lock() will see the
  668. * above counter update. Note that both this memory barrier
  669. * and the one in srcu_readers_active_idx_check() provide the
  670. * guarantee for __srcu_read_lock().
  671. */
  672. smp_mb(); /* D */ /* Pairs with C. */
  673. }
  674. /*
  675. * If SRCU is likely idle, return true, otherwise return false.
  676. *
  677. * Note that it is OK for several current from-idle requests for a new
  678. * grace period from idle to specify expediting because they will all end
  679. * up requesting the same grace period anyhow. So no loss.
  680. *
  681. * Note also that if any CPU (including the current one) is still invoking
  682. * callbacks, this function will nevertheless say "idle". This is not
  683. * ideal, but the overhead of checking all CPUs' callback lists is even
  684. * less ideal, especially on large systems. Furthermore, the wakeup
  685. * can happen before the callback is fully removed, so we have no choice
  686. * but to accept this type of error.
  687. *
  688. * This function is also subject to counter-wrap errors, but let's face
  689. * it, if this function was preempted for enough time for the counters
  690. * to wrap, it really doesn't matter whether or not we expedite the grace
  691. * period. The extra overhead of a needlessly expedited grace period is
  692. * negligible when amoritized over that time period, and the extra latency
  693. * of a needlessly non-expedited grace period is similarly negligible.
  694. */
  695. static bool srcu_might_be_idle(struct srcu_struct *sp)
  696. {
  697. unsigned long curseq;
  698. unsigned long flags;
  699. struct srcu_data *sdp;
  700. unsigned long t;
  701. /* If the local srcu_data structure has callbacks, not idle. */
  702. local_irq_save(flags);
  703. sdp = this_cpu_ptr(sp->sda);
  704. if (rcu_segcblist_pend_cbs(&sdp->srcu_cblist)) {
  705. local_irq_restore(flags);
  706. return false; /* Callbacks already present, so not idle. */
  707. }
  708. local_irq_restore(flags);
  709. /*
  710. * No local callbacks, so probabalistically probe global state.
  711. * Exact information would require acquiring locks, which would
  712. * kill scalability, hence the probabalistic nature of the probe.
  713. */
  714. /* First, see if enough time has passed since the last GP. */
  715. t = ktime_get_mono_fast_ns();
  716. if (exp_holdoff == 0 ||
  717. time_in_range_open(t, sp->srcu_last_gp_end,
  718. sp->srcu_last_gp_end + exp_holdoff))
  719. return false; /* Too soon after last GP. */
  720. /* Next, check for probable idleness. */
  721. curseq = rcu_seq_current(&sp->srcu_gp_seq);
  722. smp_mb(); /* Order ->srcu_gp_seq with ->srcu_gp_seq_needed. */
  723. if (ULONG_CMP_LT(curseq, READ_ONCE(sp->srcu_gp_seq_needed)))
  724. return false; /* Grace period in progress, so not idle. */
  725. smp_mb(); /* Order ->srcu_gp_seq with prior access. */
  726. if (curseq != rcu_seq_current(&sp->srcu_gp_seq))
  727. return false; /* GP # changed, so not idle. */
  728. return true; /* With reasonable probability, idle! */
  729. }
  730. /*
  731. * SRCU callback function to leak a callback.
  732. */
  733. static void srcu_leak_callback(struct rcu_head *rhp)
  734. {
  735. }
  736. /*
  737. * Enqueue an SRCU callback on the srcu_data structure associated with
  738. * the current CPU and the specified srcu_struct structure, initiating
  739. * grace-period processing if it is not already running.
  740. *
  741. * Note that all CPUs must agree that the grace period extended beyond
  742. * all pre-existing SRCU read-side critical section. On systems with
  743. * more than one CPU, this means that when "func()" is invoked, each CPU
  744. * is guaranteed to have executed a full memory barrier since the end of
  745. * its last corresponding SRCU read-side critical section whose beginning
  746. * preceded the call to call_rcu(). It also means that each CPU executing
  747. * an SRCU read-side critical section that continues beyond the start of
  748. * "func()" must have executed a memory barrier after the call_rcu()
  749. * but before the beginning of that SRCU read-side critical section.
  750. * Note that these guarantees include CPUs that are offline, idle, or
  751. * executing in user mode, as well as CPUs that are executing in the kernel.
  752. *
  753. * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
  754. * resulting SRCU callback function "func()", then both CPU A and CPU
  755. * B are guaranteed to execute a full memory barrier during the time
  756. * interval between the call to call_rcu() and the invocation of "func()".
  757. * This guarantee applies even if CPU A and CPU B are the same CPU (but
  758. * again only if the system has more than one CPU).
  759. *
  760. * Of course, these guarantees apply only for invocations of call_srcu(),
  761. * srcu_read_lock(), and srcu_read_unlock() that are all passed the same
  762. * srcu_struct structure.
  763. */
  764. void __call_srcu(struct srcu_struct *sp, struct rcu_head *rhp,
  765. rcu_callback_t func, bool do_norm)
  766. {
  767. unsigned long flags;
  768. bool needexp = false;
  769. bool needgp = false;
  770. unsigned long s;
  771. struct srcu_data *sdp;
  772. check_init_srcu_struct(sp);
  773. if (debug_rcu_head_queue(rhp)) {
  774. /* Probable double call_srcu(), so leak the callback. */
  775. WRITE_ONCE(rhp->func, srcu_leak_callback);
  776. WARN_ONCE(1, "call_srcu(): Leaked duplicate callback\n");
  777. return;
  778. }
  779. rhp->func = func;
  780. local_irq_save(flags);
  781. sdp = this_cpu_ptr(sp->sda);
  782. spin_lock_rcu_node(sdp);
  783. rcu_segcblist_enqueue(&sdp->srcu_cblist, rhp, false);
  784. rcu_segcblist_advance(&sdp->srcu_cblist,
  785. rcu_seq_current(&sp->srcu_gp_seq));
  786. s = rcu_seq_snap(&sp->srcu_gp_seq);
  787. (void)rcu_segcblist_accelerate(&sdp->srcu_cblist, s);
  788. if (ULONG_CMP_LT(sdp->srcu_gp_seq_needed, s)) {
  789. sdp->srcu_gp_seq_needed = s;
  790. needgp = true;
  791. }
  792. if (!do_norm && ULONG_CMP_LT(sdp->srcu_gp_seq_needed_exp, s)) {
  793. sdp->srcu_gp_seq_needed_exp = s;
  794. needexp = true;
  795. }
  796. spin_unlock_irqrestore_rcu_node(sdp, flags);
  797. if (needgp)
  798. srcu_funnel_gp_start(sp, sdp, s, do_norm);
  799. else if (needexp)
  800. srcu_funnel_exp_start(sp, sdp->mynode, s);
  801. }
  802. /**
  803. * call_srcu() - Queue a callback for invocation after an SRCU grace period
  804. * @sp: srcu_struct in queue the callback
  805. * @rhp: structure to be used for queueing the SRCU callback.
  806. * @func: function to be invoked after the SRCU grace period
  807. *
  808. * The callback function will be invoked some time after a full SRCU
  809. * grace period elapses, in other words after all pre-existing SRCU
  810. * read-side critical sections have completed. However, the callback
  811. * function might well execute concurrently with other SRCU read-side
  812. * critical sections that started after call_srcu() was invoked. SRCU
  813. * read-side critical sections are delimited by srcu_read_lock() and
  814. * srcu_read_unlock(), and may be nested.
  815. *
  816. * The callback will be invoked from process context, but must nevertheless
  817. * be fast and must not block.
  818. */
  819. void call_srcu(struct srcu_struct *sp, struct rcu_head *rhp,
  820. rcu_callback_t func)
  821. {
  822. __call_srcu(sp, rhp, func, true);
  823. }
  824. EXPORT_SYMBOL_GPL(call_srcu);
  825. /*
  826. * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
  827. */
  828. static void __synchronize_srcu(struct srcu_struct *sp, bool do_norm)
  829. {
  830. struct rcu_synchronize rcu;
  831. RCU_LOCKDEP_WARN(lock_is_held(&sp->dep_map) ||
  832. lock_is_held(&rcu_bh_lock_map) ||
  833. lock_is_held(&rcu_lock_map) ||
  834. lock_is_held(&rcu_sched_lock_map),
  835. "Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section");
  836. if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
  837. return;
  838. might_sleep();
  839. check_init_srcu_struct(sp);
  840. init_completion(&rcu.completion);
  841. init_rcu_head_on_stack(&rcu.head);
  842. __call_srcu(sp, &rcu.head, wakeme_after_rcu, do_norm);
  843. wait_for_completion(&rcu.completion);
  844. destroy_rcu_head_on_stack(&rcu.head);
  845. /*
  846. * Make sure that later code is ordered after the SRCU grace
  847. * period. This pairs with the spin_lock_irq_rcu_node()
  848. * in srcu_invoke_callbacks(). Unlike Tree RCU, this is needed
  849. * because the current CPU might have been totally uninvolved with
  850. * (and thus unordered against) that grace period.
  851. */
  852. smp_mb();
  853. }
  854. /**
  855. * synchronize_srcu_expedited - Brute-force SRCU grace period
  856. * @sp: srcu_struct with which to synchronize.
  857. *
  858. * Wait for an SRCU grace period to elapse, but be more aggressive about
  859. * spinning rather than blocking when waiting.
  860. *
  861. * Note that synchronize_srcu_expedited() has the same deadlock and
  862. * memory-ordering properties as does synchronize_srcu().
  863. */
  864. void synchronize_srcu_expedited(struct srcu_struct *sp)
  865. {
  866. __synchronize_srcu(sp, rcu_gp_is_normal());
  867. }
  868. EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
  869. /**
  870. * synchronize_srcu - wait for prior SRCU read-side critical-section completion
  871. * @sp: srcu_struct with which to synchronize.
  872. *
  873. * Wait for the count to drain to zero of both indexes. To avoid the
  874. * possible starvation of synchronize_srcu(), it waits for the count of
  875. * the index=((->srcu_idx & 1) ^ 1) to drain to zero at first,
  876. * and then flip the srcu_idx and wait for the count of the other index.
  877. *
  878. * Can block; must be called from process context.
  879. *
  880. * Note that it is illegal to call synchronize_srcu() from the corresponding
  881. * SRCU read-side critical section; doing so will result in deadlock.
  882. * However, it is perfectly legal to call synchronize_srcu() on one
  883. * srcu_struct from some other srcu_struct's read-side critical section,
  884. * as long as the resulting graph of srcu_structs is acyclic.
  885. *
  886. * There are memory-ordering constraints implied by synchronize_srcu().
  887. * On systems with more than one CPU, when synchronize_srcu() returns,
  888. * each CPU is guaranteed to have executed a full memory barrier since
  889. * the end of its last corresponding SRCU-sched read-side critical section
  890. * whose beginning preceded the call to synchronize_srcu(). In addition,
  891. * each CPU having an SRCU read-side critical section that extends beyond
  892. * the return from synchronize_srcu() is guaranteed to have executed a
  893. * full memory barrier after the beginning of synchronize_srcu() and before
  894. * the beginning of that SRCU read-side critical section. Note that these
  895. * guarantees include CPUs that are offline, idle, or executing in user mode,
  896. * as well as CPUs that are executing in the kernel.
  897. *
  898. * Furthermore, if CPU A invoked synchronize_srcu(), which returned
  899. * to its caller on CPU B, then both CPU A and CPU B are guaranteed
  900. * to have executed a full memory barrier during the execution of
  901. * synchronize_srcu(). This guarantee applies even if CPU A and CPU B
  902. * are the same CPU, but again only if the system has more than one CPU.
  903. *
  904. * Of course, these memory-ordering guarantees apply only when
  905. * synchronize_srcu(), srcu_read_lock(), and srcu_read_unlock() are
  906. * passed the same srcu_struct structure.
  907. *
  908. * If SRCU is likely idle, expedite the first request. This semantic
  909. * was provided by Classic SRCU, and is relied upon by its users, so TREE
  910. * SRCU must also provide it. Note that detecting idleness is heuristic
  911. * and subject to both false positives and negatives.
  912. */
  913. void synchronize_srcu(struct srcu_struct *sp)
  914. {
  915. if (srcu_might_be_idle(sp) || rcu_gp_is_expedited())
  916. synchronize_srcu_expedited(sp);
  917. else
  918. __synchronize_srcu(sp, true);
  919. }
  920. EXPORT_SYMBOL_GPL(synchronize_srcu);
  921. /*
  922. * Callback function for srcu_barrier() use.
  923. */
  924. static void srcu_barrier_cb(struct rcu_head *rhp)
  925. {
  926. struct srcu_data *sdp;
  927. struct srcu_struct *sp;
  928. sdp = container_of(rhp, struct srcu_data, srcu_barrier_head);
  929. sp = sdp->sp;
  930. if (atomic_dec_and_test(&sp->srcu_barrier_cpu_cnt))
  931. complete(&sp->srcu_barrier_completion);
  932. }
  933. /**
  934. * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete.
  935. * @sp: srcu_struct on which to wait for in-flight callbacks.
  936. */
  937. void srcu_barrier(struct srcu_struct *sp)
  938. {
  939. int cpu;
  940. struct srcu_data *sdp;
  941. unsigned long s = rcu_seq_snap(&sp->srcu_barrier_seq);
  942. check_init_srcu_struct(sp);
  943. mutex_lock(&sp->srcu_barrier_mutex);
  944. if (rcu_seq_done(&sp->srcu_barrier_seq, s)) {
  945. smp_mb(); /* Force ordering following return. */
  946. mutex_unlock(&sp->srcu_barrier_mutex);
  947. return; /* Someone else did our work for us. */
  948. }
  949. rcu_seq_start(&sp->srcu_barrier_seq);
  950. init_completion(&sp->srcu_barrier_completion);
  951. /* Initial count prevents reaching zero until all CBs are posted. */
  952. atomic_set(&sp->srcu_barrier_cpu_cnt, 1);
  953. /*
  954. * Each pass through this loop enqueues a callback, but only
  955. * on CPUs already having callbacks enqueued. Note that if
  956. * a CPU already has callbacks enqueue, it must have already
  957. * registered the need for a future grace period, so all we
  958. * need do is enqueue a callback that will use the same
  959. * grace period as the last callback already in the queue.
  960. */
  961. for_each_possible_cpu(cpu) {
  962. sdp = per_cpu_ptr(sp->sda, cpu);
  963. spin_lock_irq_rcu_node(sdp);
  964. atomic_inc(&sp->srcu_barrier_cpu_cnt);
  965. sdp->srcu_barrier_head.func = srcu_barrier_cb;
  966. debug_rcu_head_queue(&sdp->srcu_barrier_head);
  967. if (!rcu_segcblist_entrain(&sdp->srcu_cblist,
  968. &sdp->srcu_barrier_head, 0)) {
  969. debug_rcu_head_unqueue(&sdp->srcu_barrier_head);
  970. atomic_dec(&sp->srcu_barrier_cpu_cnt);
  971. }
  972. spin_unlock_irq_rcu_node(sdp);
  973. }
  974. /* Remove the initial count, at which point reaching zero can happen. */
  975. if (atomic_dec_and_test(&sp->srcu_barrier_cpu_cnt))
  976. complete(&sp->srcu_barrier_completion);
  977. wait_for_completion(&sp->srcu_barrier_completion);
  978. rcu_seq_end(&sp->srcu_barrier_seq);
  979. mutex_unlock(&sp->srcu_barrier_mutex);
  980. }
  981. EXPORT_SYMBOL_GPL(srcu_barrier);
  982. /**
  983. * srcu_batches_completed - return batches completed.
  984. * @sp: srcu_struct on which to report batch completion.
  985. *
  986. * Report the number of batches, correlated with, but not necessarily
  987. * precisely the same as, the number of grace periods that have elapsed.
  988. */
  989. unsigned long srcu_batches_completed(struct srcu_struct *sp)
  990. {
  991. return sp->srcu_idx;
  992. }
  993. EXPORT_SYMBOL_GPL(srcu_batches_completed);
  994. /*
  995. * Core SRCU state machine. Push state bits of ->srcu_gp_seq
  996. * to SRCU_STATE_SCAN2, and invoke srcu_gp_end() when scan has
  997. * completed in that state.
  998. */
  999. static void srcu_advance_state(struct srcu_struct *sp)
  1000. {
  1001. int idx;
  1002. mutex_lock(&sp->srcu_gp_mutex);
  1003. /*
  1004. * Because readers might be delayed for an extended period after
  1005. * fetching ->srcu_idx for their index, at any point in time there
  1006. * might well be readers using both idx=0 and idx=1. We therefore
  1007. * need to wait for readers to clear from both index values before
  1008. * invoking a callback.
  1009. *
  1010. * The load-acquire ensures that we see the accesses performed
  1011. * by the prior grace period.
  1012. */
  1013. idx = rcu_seq_state(smp_load_acquire(&sp->srcu_gp_seq)); /* ^^^ */
  1014. if (idx == SRCU_STATE_IDLE) {
  1015. spin_lock_irq_rcu_node(sp);
  1016. if (ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed)) {
  1017. WARN_ON_ONCE(rcu_seq_state(sp->srcu_gp_seq));
  1018. spin_unlock_irq_rcu_node(sp);
  1019. mutex_unlock(&sp->srcu_gp_mutex);
  1020. return;
  1021. }
  1022. idx = rcu_seq_state(READ_ONCE(sp->srcu_gp_seq));
  1023. if (idx == SRCU_STATE_IDLE)
  1024. srcu_gp_start(sp);
  1025. spin_unlock_irq_rcu_node(sp);
  1026. if (idx != SRCU_STATE_IDLE) {
  1027. mutex_unlock(&sp->srcu_gp_mutex);
  1028. return; /* Someone else started the grace period. */
  1029. }
  1030. }
  1031. if (rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) == SRCU_STATE_SCAN1) {
  1032. idx = 1 ^ (sp->srcu_idx & 1);
  1033. if (!try_check_zero(sp, idx, 1)) {
  1034. mutex_unlock(&sp->srcu_gp_mutex);
  1035. return; /* readers present, retry later. */
  1036. }
  1037. srcu_flip(sp);
  1038. rcu_seq_set_state(&sp->srcu_gp_seq, SRCU_STATE_SCAN2);
  1039. }
  1040. if (rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) == SRCU_STATE_SCAN2) {
  1041. /*
  1042. * SRCU read-side critical sections are normally short,
  1043. * so check at least twice in quick succession after a flip.
  1044. */
  1045. idx = 1 ^ (sp->srcu_idx & 1);
  1046. if (!try_check_zero(sp, idx, 2)) {
  1047. mutex_unlock(&sp->srcu_gp_mutex);
  1048. return; /* readers present, retry later. */
  1049. }
  1050. srcu_gp_end(sp); /* Releases ->srcu_gp_mutex. */
  1051. }
  1052. }
  1053. /*
  1054. * Invoke a limited number of SRCU callbacks that have passed through
  1055. * their grace period. If there are more to do, SRCU will reschedule
  1056. * the workqueue. Note that needed memory barriers have been executed
  1057. * in this task's context by srcu_readers_active_idx_check().
  1058. */
  1059. static void srcu_invoke_callbacks(struct work_struct *work)
  1060. {
  1061. bool more;
  1062. struct rcu_cblist ready_cbs;
  1063. struct rcu_head *rhp;
  1064. struct srcu_data *sdp;
  1065. struct srcu_struct *sp;
  1066. sdp = container_of(work, struct srcu_data, work.work);
  1067. sp = sdp->sp;
  1068. rcu_cblist_init(&ready_cbs);
  1069. spin_lock_irq_rcu_node(sdp);
  1070. rcu_segcblist_advance(&sdp->srcu_cblist,
  1071. rcu_seq_current(&sp->srcu_gp_seq));
  1072. if (sdp->srcu_cblist_invoking ||
  1073. !rcu_segcblist_ready_cbs(&sdp->srcu_cblist)) {
  1074. spin_unlock_irq_rcu_node(sdp);
  1075. return; /* Someone else on the job or nothing to do. */
  1076. }
  1077. /* We are on the job! Extract and invoke ready callbacks. */
  1078. sdp->srcu_cblist_invoking = true;
  1079. rcu_segcblist_extract_done_cbs(&sdp->srcu_cblist, &ready_cbs);
  1080. spin_unlock_irq_rcu_node(sdp);
  1081. rhp = rcu_cblist_dequeue(&ready_cbs);
  1082. for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) {
  1083. debug_rcu_head_unqueue(rhp);
  1084. local_bh_disable();
  1085. rhp->func(rhp);
  1086. local_bh_enable();
  1087. }
  1088. /*
  1089. * Update counts, accelerate new callbacks, and if needed,
  1090. * schedule another round of callback invocation.
  1091. */
  1092. spin_lock_irq_rcu_node(sdp);
  1093. rcu_segcblist_insert_count(&sdp->srcu_cblist, &ready_cbs);
  1094. (void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
  1095. rcu_seq_snap(&sp->srcu_gp_seq));
  1096. sdp->srcu_cblist_invoking = false;
  1097. more = rcu_segcblist_ready_cbs(&sdp->srcu_cblist);
  1098. spin_unlock_irq_rcu_node(sdp);
  1099. if (more)
  1100. srcu_schedule_cbs_sdp(sdp, 0);
  1101. }
  1102. /*
  1103. * Finished one round of SRCU grace period. Start another if there are
  1104. * more SRCU callbacks queued, otherwise put SRCU into not-running state.
  1105. */
  1106. static void srcu_reschedule(struct srcu_struct *sp, unsigned long delay)
  1107. {
  1108. bool pushgp = true;
  1109. spin_lock_irq_rcu_node(sp);
  1110. if (ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed)) {
  1111. if (!WARN_ON_ONCE(rcu_seq_state(sp->srcu_gp_seq))) {
  1112. /* All requests fulfilled, time to go idle. */
  1113. pushgp = false;
  1114. }
  1115. } else if (!rcu_seq_state(sp->srcu_gp_seq)) {
  1116. /* Outstanding request and no GP. Start one. */
  1117. srcu_gp_start(sp);
  1118. }
  1119. spin_unlock_irq_rcu_node(sp);
  1120. if (pushgp)
  1121. queue_delayed_work(rcu_gp_wq, &sp->work, delay);
  1122. }
  1123. /*
  1124. * This is the work-queue function that handles SRCU grace periods.
  1125. */
  1126. static void process_srcu(struct work_struct *work)
  1127. {
  1128. struct srcu_struct *sp;
  1129. sp = container_of(work, struct srcu_struct, work.work);
  1130. srcu_advance_state(sp);
  1131. srcu_reschedule(sp, srcu_get_delay(sp));
  1132. }
  1133. void srcutorture_get_gp_data(enum rcutorture_type test_type,
  1134. struct srcu_struct *sp, int *flags,
  1135. unsigned long *gpnum, unsigned long *completed)
  1136. {
  1137. if (test_type != SRCU_FLAVOR)
  1138. return;
  1139. *flags = 0;
  1140. *completed = rcu_seq_ctr(sp->srcu_gp_seq);
  1141. *gpnum = rcu_seq_ctr(sp->srcu_gp_seq_needed);
  1142. }
  1143. EXPORT_SYMBOL_GPL(srcutorture_get_gp_data);
  1144. void srcu_torture_stats_print(struct srcu_struct *sp, char *tt, char *tf)
  1145. {
  1146. int cpu;
  1147. int idx;
  1148. unsigned long s0 = 0, s1 = 0;
  1149. idx = sp->srcu_idx & 0x1;
  1150. pr_alert("%s%s Tree SRCU per-CPU(idx=%d):", tt, tf, idx);
  1151. for_each_possible_cpu(cpu) {
  1152. unsigned long l0, l1;
  1153. unsigned long u0, u1;
  1154. long c0, c1;
  1155. struct srcu_data *counts;
  1156. counts = per_cpu_ptr(sp->sda, cpu);
  1157. u0 = counts->srcu_unlock_count[!idx];
  1158. u1 = counts->srcu_unlock_count[idx];
  1159. /*
  1160. * Make sure that a lock is always counted if the corresponding
  1161. * unlock is counted.
  1162. */
  1163. smp_rmb();
  1164. l0 = counts->srcu_lock_count[!idx];
  1165. l1 = counts->srcu_lock_count[idx];
  1166. c0 = l0 - u0;
  1167. c1 = l1 - u1;
  1168. pr_cont(" %d(%ld,%ld)", cpu, c0, c1);
  1169. s0 += c0;
  1170. s1 += c1;
  1171. }
  1172. pr_cont(" T(%ld,%ld)\n", s0, s1);
  1173. }
  1174. EXPORT_SYMBOL_GPL(srcu_torture_stats_print);
  1175. static int __init srcu_bootup_announce(void)
  1176. {
  1177. pr_info("Hierarchical SRCU implementation.\n");
  1178. if (exp_holdoff != DEFAULT_SRCU_EXP_HOLDOFF)
  1179. pr_info("\tNon-default auto-expedite holdoff of %lu ns.\n", exp_holdoff);
  1180. return 0;
  1181. }
  1182. early_initcall(srcu_bootup_announce);