srcutree.c 42 KB

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