srcutree.c 38 KB

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