srcutree.c 43 KB

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