srcutree.c 41 KB

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