srcutree.c 43 KB

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