srcutree.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  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. rcu_segcblist_advance(&sdp->srcu_cblist,
  403. rcu_seq_current(&sp->srcu_gp_seq));
  404. (void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
  405. rcu_seq_snap(&sp->srcu_gp_seq));
  406. smp_mb(); /* Order prior store to ->srcu_gp_seq_needed vs. GP start. */
  407. rcu_seq_start(&sp->srcu_gp_seq);
  408. state = rcu_seq_state(READ_ONCE(sp->srcu_gp_seq));
  409. WARN_ON_ONCE(state != SRCU_STATE_SCAN1);
  410. }
  411. /*
  412. * Track online CPUs to guide callback workqueue placement.
  413. */
  414. DEFINE_PER_CPU(bool, srcu_online);
  415. void srcu_online_cpu(unsigned int cpu)
  416. {
  417. WRITE_ONCE(per_cpu(srcu_online, cpu), true);
  418. }
  419. void srcu_offline_cpu(unsigned int cpu)
  420. {
  421. WRITE_ONCE(per_cpu(srcu_online, cpu), false);
  422. }
  423. /*
  424. * Place the workqueue handler on the specified CPU if online, otherwise
  425. * just run it whereever. This is useful for placing workqueue handlers
  426. * that are to invoke the specified CPU's callbacks.
  427. */
  428. static bool srcu_queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  429. struct delayed_work *dwork,
  430. unsigned long delay)
  431. {
  432. bool ret;
  433. preempt_disable();
  434. if (READ_ONCE(per_cpu(srcu_online, cpu)))
  435. ret = queue_delayed_work_on(cpu, wq, dwork, delay);
  436. else
  437. ret = queue_delayed_work(wq, dwork, delay);
  438. preempt_enable();
  439. return ret;
  440. }
  441. /*
  442. * Schedule callback invocation for the specified srcu_data structure,
  443. * if possible, on the corresponding CPU.
  444. */
  445. static void srcu_schedule_cbs_sdp(struct srcu_data *sdp, unsigned long delay)
  446. {
  447. srcu_queue_delayed_work_on(sdp->cpu, rcu_gp_wq, &sdp->work, delay);
  448. }
  449. /*
  450. * Schedule callback invocation for all srcu_data structures associated
  451. * with the specified srcu_node structure that have callbacks for the
  452. * just-completed grace period, the one corresponding to idx. If possible,
  453. * schedule this invocation on the corresponding CPUs.
  454. */
  455. static void srcu_schedule_cbs_snp(struct srcu_struct *sp, struct srcu_node *snp,
  456. unsigned long mask, unsigned long delay)
  457. {
  458. int cpu;
  459. for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) {
  460. if (!(mask & (1 << (cpu - snp->grplo))))
  461. continue;
  462. srcu_schedule_cbs_sdp(per_cpu_ptr(sp->sda, cpu), delay);
  463. }
  464. }
  465. /*
  466. * Note the end of an SRCU grace period. Initiates callback invocation
  467. * and starts a new grace period if needed.
  468. *
  469. * The ->srcu_cb_mutex acquisition does not protect any data, but
  470. * instead prevents more than one grace period from starting while we
  471. * are initiating callback invocation. This allows the ->srcu_have_cbs[]
  472. * array to have a finite number of elements.
  473. */
  474. static void srcu_gp_end(struct srcu_struct *sp)
  475. {
  476. unsigned long cbdelay;
  477. bool cbs;
  478. bool last_lvl;
  479. int cpu;
  480. unsigned long flags;
  481. unsigned long gpseq;
  482. int idx;
  483. unsigned long mask;
  484. struct srcu_data *sdp;
  485. struct srcu_node *snp;
  486. /* Prevent more than one additional grace period. */
  487. mutex_lock(&sp->srcu_cb_mutex);
  488. /* End the current grace period. */
  489. spin_lock_irq_rcu_node(sp);
  490. idx = rcu_seq_state(sp->srcu_gp_seq);
  491. WARN_ON_ONCE(idx != SRCU_STATE_SCAN2);
  492. cbdelay = srcu_get_delay(sp);
  493. sp->srcu_last_gp_end = ktime_get_mono_fast_ns();
  494. rcu_seq_end(&sp->srcu_gp_seq);
  495. gpseq = rcu_seq_current(&sp->srcu_gp_seq);
  496. if (ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, gpseq))
  497. sp->srcu_gp_seq_needed_exp = gpseq;
  498. spin_unlock_irq_rcu_node(sp);
  499. mutex_unlock(&sp->srcu_gp_mutex);
  500. /* A new grace period can start at this point. But only one. */
  501. /* Initiate callback invocation as needed. */
  502. idx = rcu_seq_ctr(gpseq) % ARRAY_SIZE(snp->srcu_have_cbs);
  503. rcu_for_each_node_breadth_first(sp, snp) {
  504. spin_lock_irq_rcu_node(snp);
  505. cbs = false;
  506. last_lvl = snp >= sp->level[rcu_num_lvls - 1];
  507. if (last_lvl)
  508. cbs = snp->srcu_have_cbs[idx] == gpseq;
  509. snp->srcu_have_cbs[idx] = gpseq;
  510. rcu_seq_set_state(&snp->srcu_have_cbs[idx], 1);
  511. if (ULONG_CMP_LT(snp->srcu_gp_seq_needed_exp, gpseq))
  512. snp->srcu_gp_seq_needed_exp = gpseq;
  513. mask = snp->srcu_data_have_cbs[idx];
  514. snp->srcu_data_have_cbs[idx] = 0;
  515. spin_unlock_irq_rcu_node(snp);
  516. if (cbs)
  517. srcu_schedule_cbs_snp(sp, snp, mask, cbdelay);
  518. /* Occasionally prevent srcu_data counter wrap. */
  519. if (!(gpseq & counter_wrap_check) && last_lvl)
  520. for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) {
  521. sdp = per_cpu_ptr(sp->sda, cpu);
  522. spin_lock_irqsave_rcu_node(sdp, flags);
  523. if (ULONG_CMP_GE(gpseq,
  524. sdp->srcu_gp_seq_needed + 100))
  525. sdp->srcu_gp_seq_needed = gpseq;
  526. if (ULONG_CMP_GE(gpseq,
  527. sdp->srcu_gp_seq_needed_exp + 100))
  528. sdp->srcu_gp_seq_needed_exp = gpseq;
  529. spin_unlock_irqrestore_rcu_node(sdp, flags);
  530. }
  531. }
  532. /* Callback initiation done, allow grace periods after next. */
  533. mutex_unlock(&sp->srcu_cb_mutex);
  534. /* Start a new grace period if needed. */
  535. spin_lock_irq_rcu_node(sp);
  536. gpseq = rcu_seq_current(&sp->srcu_gp_seq);
  537. if (!rcu_seq_state(gpseq) &&
  538. ULONG_CMP_LT(gpseq, sp->srcu_gp_seq_needed)) {
  539. srcu_gp_start(sp);
  540. spin_unlock_irq_rcu_node(sp);
  541. srcu_reschedule(sp, 0);
  542. } else {
  543. spin_unlock_irq_rcu_node(sp);
  544. }
  545. }
  546. /*
  547. * Funnel-locking scheme to scalably mediate many concurrent expedited
  548. * grace-period requests. This function is invoked for the first known
  549. * expedited request for a grace period that has already been requested,
  550. * but without expediting. To start a completely new grace period,
  551. * whether expedited or not, use srcu_funnel_gp_start() instead.
  552. */
  553. static void srcu_funnel_exp_start(struct srcu_struct *sp, struct srcu_node *snp,
  554. unsigned long s)
  555. {
  556. unsigned long flags;
  557. for (; snp != NULL; snp = snp->srcu_parent) {
  558. if (rcu_seq_done(&sp->srcu_gp_seq, s) ||
  559. ULONG_CMP_GE(READ_ONCE(snp->srcu_gp_seq_needed_exp), s))
  560. return;
  561. spin_lock_irqsave_rcu_node(snp, flags);
  562. if (ULONG_CMP_GE(snp->srcu_gp_seq_needed_exp, s)) {
  563. spin_unlock_irqrestore_rcu_node(snp, flags);
  564. return;
  565. }
  566. WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s);
  567. spin_unlock_irqrestore_rcu_node(snp, flags);
  568. }
  569. spin_lock_irqsave_rcu_node(sp, flags);
  570. if (ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, s))
  571. sp->srcu_gp_seq_needed_exp = s;
  572. spin_unlock_irqrestore_rcu_node(sp, flags);
  573. }
  574. /*
  575. * Funnel-locking scheme to scalably mediate many concurrent grace-period
  576. * requests. The winner has to do the work of actually starting grace
  577. * period s. Losers must either ensure that their desired grace-period
  578. * number is recorded on at least their leaf srcu_node structure, or they
  579. * must take steps to invoke their own callbacks.
  580. *
  581. * Note that this function also does the work of srcu_funnel_exp_start(),
  582. * in some cases by directly invoking it.
  583. */
  584. static void srcu_funnel_gp_start(struct srcu_struct *sp, struct srcu_data *sdp,
  585. unsigned long s, bool do_norm)
  586. {
  587. unsigned long flags;
  588. int idx = rcu_seq_ctr(s) % ARRAY_SIZE(sdp->mynode->srcu_have_cbs);
  589. struct srcu_node *snp = sdp->mynode;
  590. unsigned long snp_seq;
  591. /* Each pass through the loop does one level of the srcu_node tree. */
  592. for (; snp != NULL; snp = snp->srcu_parent) {
  593. if (rcu_seq_done(&sp->srcu_gp_seq, s) && snp != sdp->mynode)
  594. return; /* GP already done and CBs recorded. */
  595. spin_lock_irqsave_rcu_node(snp, flags);
  596. if (ULONG_CMP_GE(snp->srcu_have_cbs[idx], s)) {
  597. snp_seq = snp->srcu_have_cbs[idx];
  598. if (snp == sdp->mynode && snp_seq == s)
  599. snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
  600. spin_unlock_irqrestore_rcu_node(snp, flags);
  601. if (snp == sdp->mynode && snp_seq != s) {
  602. srcu_schedule_cbs_sdp(sdp, do_norm
  603. ? SRCU_INTERVAL
  604. : 0);
  605. return;
  606. }
  607. if (!do_norm)
  608. srcu_funnel_exp_start(sp, snp, s);
  609. return;
  610. }
  611. snp->srcu_have_cbs[idx] = s;
  612. if (snp == sdp->mynode)
  613. snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
  614. if (!do_norm && ULONG_CMP_LT(snp->srcu_gp_seq_needed_exp, s))
  615. snp->srcu_gp_seq_needed_exp = s;
  616. spin_unlock_irqrestore_rcu_node(snp, flags);
  617. }
  618. /* Top of tree, must ensure the grace period will be started. */
  619. spin_lock_irqsave_rcu_node(sp, flags);
  620. if (ULONG_CMP_LT(sp->srcu_gp_seq_needed, s)) {
  621. /*
  622. * Record need for grace period s. Pair with load
  623. * acquire setting up for initialization.
  624. */
  625. smp_store_release(&sp->srcu_gp_seq_needed, s); /*^^^*/
  626. }
  627. if (!do_norm && ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, s))
  628. sp->srcu_gp_seq_needed_exp = s;
  629. /* If grace period not already done and none in progress, start it. */
  630. if (!rcu_seq_done(&sp->srcu_gp_seq, s) &&
  631. rcu_seq_state(sp->srcu_gp_seq) == SRCU_STATE_IDLE) {
  632. WARN_ON_ONCE(ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed));
  633. srcu_gp_start(sp);
  634. queue_delayed_work(rcu_gp_wq, &sp->work, srcu_get_delay(sp));
  635. }
  636. spin_unlock_irqrestore_rcu_node(sp, flags);
  637. }
  638. /*
  639. * Wait until all readers counted by array index idx complete, but
  640. * loop an additional time if there is an expedited grace period pending.
  641. * The caller must ensure that ->srcu_idx is not changed while checking.
  642. */
  643. static bool try_check_zero(struct srcu_struct *sp, int idx, int trycount)
  644. {
  645. for (;;) {
  646. if (srcu_readers_active_idx_check(sp, idx))
  647. return true;
  648. if (--trycount + !srcu_get_delay(sp) <= 0)
  649. return false;
  650. udelay(SRCU_RETRY_CHECK_DELAY);
  651. }
  652. }
  653. /*
  654. * Increment the ->srcu_idx counter so that future SRCU readers will
  655. * use the other rank of the ->srcu_(un)lock_count[] arrays. This allows
  656. * us to wait for pre-existing readers in a starvation-free manner.
  657. */
  658. static void srcu_flip(struct srcu_struct *sp)
  659. {
  660. /*
  661. * Ensure that if this updater saw a given reader's increment
  662. * from __srcu_read_lock(), that reader was using an old value
  663. * of ->srcu_idx. Also ensure that if a given reader sees the
  664. * new value of ->srcu_idx, this updater's earlier scans cannot
  665. * have seen that reader's increments (which is OK, because this
  666. * grace period need not wait on that reader).
  667. */
  668. smp_mb(); /* E */ /* Pairs with B and C. */
  669. WRITE_ONCE(sp->srcu_idx, sp->srcu_idx + 1);
  670. /*
  671. * Ensure that if the updater misses an __srcu_read_unlock()
  672. * increment, that task's next __srcu_read_lock() will see the
  673. * above counter update. Note that both this memory barrier
  674. * and the one in srcu_readers_active_idx_check() provide the
  675. * guarantee for __srcu_read_lock().
  676. */
  677. smp_mb(); /* D */ /* Pairs with C. */
  678. }
  679. /*
  680. * If SRCU is likely idle, return true, otherwise return false.
  681. *
  682. * Note that it is OK for several current from-idle requests for a new
  683. * grace period from idle to specify expediting because they will all end
  684. * up requesting the same grace period anyhow. So no loss.
  685. *
  686. * Note also that if any CPU (including the current one) is still invoking
  687. * callbacks, this function will nevertheless say "idle". This is not
  688. * ideal, but the overhead of checking all CPUs' callback lists is even
  689. * less ideal, especially on large systems. Furthermore, the wakeup
  690. * can happen before the callback is fully removed, so we have no choice
  691. * but to accept this type of error.
  692. *
  693. * This function is also subject to counter-wrap errors, but let's face
  694. * it, if this function was preempted for enough time for the counters
  695. * to wrap, it really doesn't matter whether or not we expedite the grace
  696. * period. The extra overhead of a needlessly expedited grace period is
  697. * negligible when amoritized over that time period, and the extra latency
  698. * of a needlessly non-expedited grace period is similarly negligible.
  699. */
  700. static bool srcu_might_be_idle(struct srcu_struct *sp)
  701. {
  702. unsigned long curseq;
  703. unsigned long flags;
  704. struct srcu_data *sdp;
  705. unsigned long t;
  706. /* If the local srcu_data structure has callbacks, not idle. */
  707. local_irq_save(flags);
  708. sdp = this_cpu_ptr(sp->sda);
  709. if (rcu_segcblist_pend_cbs(&sdp->srcu_cblist)) {
  710. local_irq_restore(flags);
  711. return false; /* Callbacks already present, so not idle. */
  712. }
  713. local_irq_restore(flags);
  714. /*
  715. * No local callbacks, so probabalistically probe global state.
  716. * Exact information would require acquiring locks, which would
  717. * kill scalability, hence the probabalistic nature of the probe.
  718. */
  719. /* First, see if enough time has passed since the last GP. */
  720. t = ktime_get_mono_fast_ns();
  721. if (exp_holdoff == 0 ||
  722. time_in_range_open(t, sp->srcu_last_gp_end,
  723. sp->srcu_last_gp_end + exp_holdoff))
  724. return false; /* Too soon after last GP. */
  725. /* Next, check for probable idleness. */
  726. curseq = rcu_seq_current(&sp->srcu_gp_seq);
  727. smp_mb(); /* Order ->srcu_gp_seq with ->srcu_gp_seq_needed. */
  728. if (ULONG_CMP_LT(curseq, READ_ONCE(sp->srcu_gp_seq_needed)))
  729. return false; /* Grace period in progress, so not idle. */
  730. smp_mb(); /* Order ->srcu_gp_seq with prior access. */
  731. if (curseq != rcu_seq_current(&sp->srcu_gp_seq))
  732. return false; /* GP # changed, so not idle. */
  733. return true; /* With reasonable probability, idle! */
  734. }
  735. /*
  736. * SRCU callback function to leak a callback.
  737. */
  738. static void srcu_leak_callback(struct rcu_head *rhp)
  739. {
  740. }
  741. /*
  742. * Enqueue an SRCU callback on the srcu_data structure associated with
  743. * the current CPU and the specified srcu_struct structure, initiating
  744. * grace-period processing if it is not already running.
  745. *
  746. * Note that all CPUs must agree that the grace period extended beyond
  747. * all pre-existing SRCU read-side critical section. On systems with
  748. * more than one CPU, this means that when "func()" is invoked, each CPU
  749. * is guaranteed to have executed a full memory barrier since the end of
  750. * its last corresponding SRCU read-side critical section whose beginning
  751. * preceded the call to call_srcu(). It also means that each CPU executing
  752. * an SRCU read-side critical section that continues beyond the start of
  753. * "func()" must have executed a memory barrier after the call_srcu()
  754. * but before the beginning of that SRCU read-side critical section.
  755. * Note that these guarantees include CPUs that are offline, idle, or
  756. * executing in user mode, as well as CPUs that are executing in the kernel.
  757. *
  758. * Furthermore, if CPU A invoked call_srcu() and CPU B invoked the
  759. * resulting SRCU callback function "func()", then both CPU A and CPU
  760. * B are guaranteed to execute a full memory barrier during the time
  761. * interval between the call to call_srcu() and the invocation of "func()".
  762. * This guarantee applies even if CPU A and CPU B are the same CPU (but
  763. * again only if the system has more than one CPU).
  764. *
  765. * Of course, these guarantees apply only for invocations of call_srcu(),
  766. * srcu_read_lock(), and srcu_read_unlock() that are all passed the same
  767. * srcu_struct structure.
  768. */
  769. void __call_srcu(struct srcu_struct *sp, struct rcu_head *rhp,
  770. rcu_callback_t func, bool do_norm)
  771. {
  772. unsigned long flags;
  773. bool needexp = false;
  774. bool needgp = false;
  775. unsigned long s;
  776. struct srcu_data *sdp;
  777. check_init_srcu_struct(sp);
  778. if (debug_rcu_head_queue(rhp)) {
  779. /* Probable double call_srcu(), so leak the callback. */
  780. WRITE_ONCE(rhp->func, srcu_leak_callback);
  781. WARN_ONCE(1, "call_srcu(): Leaked duplicate callback\n");
  782. return;
  783. }
  784. rhp->func = func;
  785. local_irq_save(flags);
  786. sdp = this_cpu_ptr(sp->sda);
  787. spin_lock_rcu_node(sdp);
  788. rcu_segcblist_enqueue(&sdp->srcu_cblist, rhp, false);
  789. rcu_segcblist_advance(&sdp->srcu_cblist,
  790. rcu_seq_current(&sp->srcu_gp_seq));
  791. s = rcu_seq_snap(&sp->srcu_gp_seq);
  792. (void)rcu_segcblist_accelerate(&sdp->srcu_cblist, s);
  793. if (ULONG_CMP_LT(sdp->srcu_gp_seq_needed, s)) {
  794. sdp->srcu_gp_seq_needed = s;
  795. needgp = true;
  796. }
  797. if (!do_norm && ULONG_CMP_LT(sdp->srcu_gp_seq_needed_exp, s)) {
  798. sdp->srcu_gp_seq_needed_exp = s;
  799. needexp = true;
  800. }
  801. spin_unlock_irqrestore_rcu_node(sdp, flags);
  802. if (needgp)
  803. srcu_funnel_gp_start(sp, sdp, s, do_norm);
  804. else if (needexp)
  805. srcu_funnel_exp_start(sp, sdp->mynode, s);
  806. }
  807. /**
  808. * call_srcu() - Queue a callback for invocation after an SRCU grace period
  809. * @sp: srcu_struct in queue the callback
  810. * @rhp: structure to be used for queueing the SRCU callback.
  811. * @func: function to be invoked after the SRCU grace period
  812. *
  813. * The callback function will be invoked some time after a full SRCU
  814. * grace period elapses, in other words after all pre-existing SRCU
  815. * read-side critical sections have completed. However, the callback
  816. * function might well execute concurrently with other SRCU read-side
  817. * critical sections that started after call_srcu() was invoked. SRCU
  818. * read-side critical sections are delimited by srcu_read_lock() and
  819. * srcu_read_unlock(), and may be nested.
  820. *
  821. * The callback will be invoked from process context, but must nevertheless
  822. * be fast and must not block.
  823. */
  824. void call_srcu(struct srcu_struct *sp, struct rcu_head *rhp,
  825. rcu_callback_t func)
  826. {
  827. __call_srcu(sp, rhp, func, true);
  828. }
  829. EXPORT_SYMBOL_GPL(call_srcu);
  830. /*
  831. * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
  832. */
  833. static void __synchronize_srcu(struct srcu_struct *sp, bool do_norm)
  834. {
  835. struct rcu_synchronize rcu;
  836. RCU_LOCKDEP_WARN(lock_is_held(&sp->dep_map) ||
  837. lock_is_held(&rcu_bh_lock_map) ||
  838. lock_is_held(&rcu_lock_map) ||
  839. lock_is_held(&rcu_sched_lock_map),
  840. "Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section");
  841. if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
  842. return;
  843. might_sleep();
  844. check_init_srcu_struct(sp);
  845. init_completion(&rcu.completion);
  846. init_rcu_head_on_stack(&rcu.head);
  847. __call_srcu(sp, &rcu.head, wakeme_after_rcu, do_norm);
  848. wait_for_completion(&rcu.completion);
  849. destroy_rcu_head_on_stack(&rcu.head);
  850. /*
  851. * Make sure that later code is ordered after the SRCU grace
  852. * period. This pairs with the spin_lock_irq_rcu_node()
  853. * in srcu_invoke_callbacks(). Unlike Tree RCU, this is needed
  854. * because the current CPU might have been totally uninvolved with
  855. * (and thus unordered against) that grace period.
  856. */
  857. smp_mb();
  858. }
  859. /**
  860. * synchronize_srcu_expedited - Brute-force SRCU grace period
  861. * @sp: srcu_struct with which to synchronize.
  862. *
  863. * Wait for an SRCU grace period to elapse, but be more aggressive about
  864. * spinning rather than blocking when waiting.
  865. *
  866. * Note that synchronize_srcu_expedited() has the same deadlock and
  867. * memory-ordering properties as does synchronize_srcu().
  868. */
  869. void synchronize_srcu_expedited(struct srcu_struct *sp)
  870. {
  871. __synchronize_srcu(sp, rcu_gp_is_normal());
  872. }
  873. EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
  874. /**
  875. * synchronize_srcu - wait for prior SRCU read-side critical-section completion
  876. * @sp: srcu_struct with which to synchronize.
  877. *
  878. * Wait for the count to drain to zero of both indexes. To avoid the
  879. * possible starvation of synchronize_srcu(), it waits for the count of
  880. * the index=((->srcu_idx & 1) ^ 1) to drain to zero at first,
  881. * and then flip the srcu_idx and wait for the count of the other index.
  882. *
  883. * Can block; must be called from process context.
  884. *
  885. * Note that it is illegal to call synchronize_srcu() from the corresponding
  886. * SRCU read-side critical section; doing so will result in deadlock.
  887. * However, it is perfectly legal to call synchronize_srcu() on one
  888. * srcu_struct from some other srcu_struct's read-side critical section,
  889. * as long as the resulting graph of srcu_structs is acyclic.
  890. *
  891. * There are memory-ordering constraints implied by synchronize_srcu().
  892. * On systems with more than one CPU, when synchronize_srcu() returns,
  893. * each CPU is guaranteed to have executed a full memory barrier since
  894. * the end of its last corresponding SRCU-sched read-side critical section
  895. * whose beginning preceded the call to synchronize_srcu(). In addition,
  896. * each CPU having an SRCU read-side critical section that extends beyond
  897. * the return from synchronize_srcu() is guaranteed to have executed a
  898. * full memory barrier after the beginning of synchronize_srcu() and before
  899. * the beginning of that SRCU read-side critical section. Note that these
  900. * guarantees include CPUs that are offline, idle, or executing in user mode,
  901. * as well as CPUs that are executing in the kernel.
  902. *
  903. * Furthermore, if CPU A invoked synchronize_srcu(), which returned
  904. * to its caller on CPU B, then both CPU A and CPU B are guaranteed
  905. * to have executed a full memory barrier during the execution of
  906. * synchronize_srcu(). This guarantee applies even if CPU A and CPU B
  907. * are the same CPU, but again only if the system has more than one CPU.
  908. *
  909. * Of course, these memory-ordering guarantees apply only when
  910. * synchronize_srcu(), srcu_read_lock(), and srcu_read_unlock() are
  911. * passed the same srcu_struct structure.
  912. *
  913. * If SRCU is likely idle, expedite the first request. This semantic
  914. * was provided by Classic SRCU, and is relied upon by its users, so TREE
  915. * SRCU must also provide it. Note that detecting idleness is heuristic
  916. * and subject to both false positives and negatives.
  917. */
  918. void synchronize_srcu(struct srcu_struct *sp)
  919. {
  920. if (srcu_might_be_idle(sp) || rcu_gp_is_expedited())
  921. synchronize_srcu_expedited(sp);
  922. else
  923. __synchronize_srcu(sp, true);
  924. }
  925. EXPORT_SYMBOL_GPL(synchronize_srcu);
  926. /*
  927. * Callback function for srcu_barrier() use.
  928. */
  929. static void srcu_barrier_cb(struct rcu_head *rhp)
  930. {
  931. struct srcu_data *sdp;
  932. struct srcu_struct *sp;
  933. sdp = container_of(rhp, struct srcu_data, srcu_barrier_head);
  934. sp = sdp->sp;
  935. if (atomic_dec_and_test(&sp->srcu_barrier_cpu_cnt))
  936. complete(&sp->srcu_barrier_completion);
  937. }
  938. /**
  939. * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete.
  940. * @sp: srcu_struct on which to wait for in-flight callbacks.
  941. */
  942. void srcu_barrier(struct srcu_struct *sp)
  943. {
  944. int cpu;
  945. struct srcu_data *sdp;
  946. unsigned long s = rcu_seq_snap(&sp->srcu_barrier_seq);
  947. check_init_srcu_struct(sp);
  948. mutex_lock(&sp->srcu_barrier_mutex);
  949. if (rcu_seq_done(&sp->srcu_barrier_seq, s)) {
  950. smp_mb(); /* Force ordering following return. */
  951. mutex_unlock(&sp->srcu_barrier_mutex);
  952. return; /* Someone else did our work for us. */
  953. }
  954. rcu_seq_start(&sp->srcu_barrier_seq);
  955. init_completion(&sp->srcu_barrier_completion);
  956. /* Initial count prevents reaching zero until all CBs are posted. */
  957. atomic_set(&sp->srcu_barrier_cpu_cnt, 1);
  958. /*
  959. * Each pass through this loop enqueues a callback, but only
  960. * on CPUs already having callbacks enqueued. Note that if
  961. * a CPU already has callbacks enqueue, it must have already
  962. * registered the need for a future grace period, so all we
  963. * need do is enqueue a callback that will use the same
  964. * grace period as the last callback already in the queue.
  965. */
  966. for_each_possible_cpu(cpu) {
  967. sdp = per_cpu_ptr(sp->sda, cpu);
  968. spin_lock_irq_rcu_node(sdp);
  969. atomic_inc(&sp->srcu_barrier_cpu_cnt);
  970. sdp->srcu_barrier_head.func = srcu_barrier_cb;
  971. debug_rcu_head_queue(&sdp->srcu_barrier_head);
  972. if (!rcu_segcblist_entrain(&sdp->srcu_cblist,
  973. &sdp->srcu_barrier_head, 0)) {
  974. debug_rcu_head_unqueue(&sdp->srcu_barrier_head);
  975. atomic_dec(&sp->srcu_barrier_cpu_cnt);
  976. }
  977. spin_unlock_irq_rcu_node(sdp);
  978. }
  979. /* Remove the initial count, at which point reaching zero can happen. */
  980. if (atomic_dec_and_test(&sp->srcu_barrier_cpu_cnt))
  981. complete(&sp->srcu_barrier_completion);
  982. wait_for_completion(&sp->srcu_barrier_completion);
  983. rcu_seq_end(&sp->srcu_barrier_seq);
  984. mutex_unlock(&sp->srcu_barrier_mutex);
  985. }
  986. EXPORT_SYMBOL_GPL(srcu_barrier);
  987. /**
  988. * srcu_batches_completed - return batches completed.
  989. * @sp: srcu_struct on which to report batch completion.
  990. *
  991. * Report the number of batches, correlated with, but not necessarily
  992. * precisely the same as, the number of grace periods that have elapsed.
  993. */
  994. unsigned long srcu_batches_completed(struct srcu_struct *sp)
  995. {
  996. return sp->srcu_idx;
  997. }
  998. EXPORT_SYMBOL_GPL(srcu_batches_completed);
  999. /*
  1000. * Core SRCU state machine. Push state bits of ->srcu_gp_seq
  1001. * to SRCU_STATE_SCAN2, and invoke srcu_gp_end() when scan has
  1002. * completed in that state.
  1003. */
  1004. static void srcu_advance_state(struct srcu_struct *sp)
  1005. {
  1006. int idx;
  1007. mutex_lock(&sp->srcu_gp_mutex);
  1008. /*
  1009. * Because readers might be delayed for an extended period after
  1010. * fetching ->srcu_idx for their index, at any point in time there
  1011. * might well be readers using both idx=0 and idx=1. We therefore
  1012. * need to wait for readers to clear from both index values before
  1013. * invoking a callback.
  1014. *
  1015. * The load-acquire ensures that we see the accesses performed
  1016. * by the prior grace period.
  1017. */
  1018. idx = rcu_seq_state(smp_load_acquire(&sp->srcu_gp_seq)); /* ^^^ */
  1019. if (idx == SRCU_STATE_IDLE) {
  1020. spin_lock_irq_rcu_node(sp);
  1021. if (ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed)) {
  1022. WARN_ON_ONCE(rcu_seq_state(sp->srcu_gp_seq));
  1023. spin_unlock_irq_rcu_node(sp);
  1024. mutex_unlock(&sp->srcu_gp_mutex);
  1025. return;
  1026. }
  1027. idx = rcu_seq_state(READ_ONCE(sp->srcu_gp_seq));
  1028. if (idx == SRCU_STATE_IDLE)
  1029. srcu_gp_start(sp);
  1030. spin_unlock_irq_rcu_node(sp);
  1031. if (idx != SRCU_STATE_IDLE) {
  1032. mutex_unlock(&sp->srcu_gp_mutex);
  1033. return; /* Someone else started the grace period. */
  1034. }
  1035. }
  1036. if (rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) == SRCU_STATE_SCAN1) {
  1037. idx = 1 ^ (sp->srcu_idx & 1);
  1038. if (!try_check_zero(sp, idx, 1)) {
  1039. mutex_unlock(&sp->srcu_gp_mutex);
  1040. return; /* readers present, retry later. */
  1041. }
  1042. srcu_flip(sp);
  1043. rcu_seq_set_state(&sp->srcu_gp_seq, SRCU_STATE_SCAN2);
  1044. }
  1045. if (rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) == SRCU_STATE_SCAN2) {
  1046. /*
  1047. * SRCU read-side critical sections are normally short,
  1048. * so check at least twice in quick succession after a flip.
  1049. */
  1050. idx = 1 ^ (sp->srcu_idx & 1);
  1051. if (!try_check_zero(sp, idx, 2)) {
  1052. mutex_unlock(&sp->srcu_gp_mutex);
  1053. return; /* readers present, retry later. */
  1054. }
  1055. srcu_gp_end(sp); /* Releases ->srcu_gp_mutex. */
  1056. }
  1057. }
  1058. /*
  1059. * Invoke a limited number of SRCU callbacks that have passed through
  1060. * their grace period. If there are more to do, SRCU will reschedule
  1061. * the workqueue. Note that needed memory barriers have been executed
  1062. * in this task's context by srcu_readers_active_idx_check().
  1063. */
  1064. static void srcu_invoke_callbacks(struct work_struct *work)
  1065. {
  1066. bool more;
  1067. struct rcu_cblist ready_cbs;
  1068. struct rcu_head *rhp;
  1069. struct srcu_data *sdp;
  1070. struct srcu_struct *sp;
  1071. sdp = container_of(work, struct srcu_data, work.work);
  1072. sp = sdp->sp;
  1073. rcu_cblist_init(&ready_cbs);
  1074. spin_lock_irq_rcu_node(sdp);
  1075. rcu_segcblist_advance(&sdp->srcu_cblist,
  1076. rcu_seq_current(&sp->srcu_gp_seq));
  1077. if (sdp->srcu_cblist_invoking ||
  1078. !rcu_segcblist_ready_cbs(&sdp->srcu_cblist)) {
  1079. spin_unlock_irq_rcu_node(sdp);
  1080. return; /* Someone else on the job or nothing to do. */
  1081. }
  1082. /* We are on the job! Extract and invoke ready callbacks. */
  1083. sdp->srcu_cblist_invoking = true;
  1084. rcu_segcblist_extract_done_cbs(&sdp->srcu_cblist, &ready_cbs);
  1085. spin_unlock_irq_rcu_node(sdp);
  1086. rhp = rcu_cblist_dequeue(&ready_cbs);
  1087. for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) {
  1088. debug_rcu_head_unqueue(rhp);
  1089. local_bh_disable();
  1090. rhp->func(rhp);
  1091. local_bh_enable();
  1092. }
  1093. /*
  1094. * Update counts, accelerate new callbacks, and if needed,
  1095. * schedule another round of callback invocation.
  1096. */
  1097. spin_lock_irq_rcu_node(sdp);
  1098. rcu_segcblist_insert_count(&sdp->srcu_cblist, &ready_cbs);
  1099. (void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
  1100. rcu_seq_snap(&sp->srcu_gp_seq));
  1101. sdp->srcu_cblist_invoking = false;
  1102. more = rcu_segcblist_ready_cbs(&sdp->srcu_cblist);
  1103. spin_unlock_irq_rcu_node(sdp);
  1104. if (more)
  1105. srcu_schedule_cbs_sdp(sdp, 0);
  1106. }
  1107. /*
  1108. * Finished one round of SRCU grace period. Start another if there are
  1109. * more SRCU callbacks queued, otherwise put SRCU into not-running state.
  1110. */
  1111. static void srcu_reschedule(struct srcu_struct *sp, unsigned long delay)
  1112. {
  1113. bool pushgp = true;
  1114. spin_lock_irq_rcu_node(sp);
  1115. if (ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed)) {
  1116. if (!WARN_ON_ONCE(rcu_seq_state(sp->srcu_gp_seq))) {
  1117. /* All requests fulfilled, time to go idle. */
  1118. pushgp = false;
  1119. }
  1120. } else if (!rcu_seq_state(sp->srcu_gp_seq)) {
  1121. /* Outstanding request and no GP. Start one. */
  1122. srcu_gp_start(sp);
  1123. }
  1124. spin_unlock_irq_rcu_node(sp);
  1125. if (pushgp)
  1126. queue_delayed_work(rcu_gp_wq, &sp->work, delay);
  1127. }
  1128. /*
  1129. * This is the work-queue function that handles SRCU grace periods.
  1130. */
  1131. static void process_srcu(struct work_struct *work)
  1132. {
  1133. struct srcu_struct *sp;
  1134. sp = container_of(work, struct srcu_struct, work.work);
  1135. srcu_advance_state(sp);
  1136. srcu_reschedule(sp, srcu_get_delay(sp));
  1137. }
  1138. void srcutorture_get_gp_data(enum rcutorture_type test_type,
  1139. struct srcu_struct *sp, int *flags,
  1140. unsigned long *gp_seq)
  1141. {
  1142. if (test_type != SRCU_FLAVOR)
  1143. return;
  1144. *flags = 0;
  1145. *gp_seq = rcu_seq_current(&sp->srcu_gp_seq);
  1146. }
  1147. EXPORT_SYMBOL_GPL(srcutorture_get_gp_data);
  1148. void srcu_torture_stats_print(struct srcu_struct *sp, char *tt, char *tf)
  1149. {
  1150. int cpu;
  1151. int idx;
  1152. unsigned long s0 = 0, s1 = 0;
  1153. idx = sp->srcu_idx & 0x1;
  1154. pr_alert("%s%s Tree SRCU g%ld per-CPU(idx=%d):",
  1155. tt, tf, rcu_seq_current(&sp->srcu_gp_seq), idx);
  1156. for_each_possible_cpu(cpu) {
  1157. unsigned long l0, l1;
  1158. unsigned long u0, u1;
  1159. long c0, c1;
  1160. struct srcu_data *sdp;
  1161. sdp = per_cpu_ptr(sp->sda, cpu);
  1162. u0 = sdp->srcu_unlock_count[!idx];
  1163. u1 = sdp->srcu_unlock_count[idx];
  1164. /*
  1165. * Make sure that a lock is always counted if the corresponding
  1166. * unlock is counted.
  1167. */
  1168. smp_rmb();
  1169. l0 = sdp->srcu_lock_count[!idx];
  1170. l1 = sdp->srcu_lock_count[idx];
  1171. c0 = l0 - u0;
  1172. c1 = l1 - u1;
  1173. pr_cont(" %d(%ld,%ld %1p)",
  1174. cpu, c0, c1, rcu_segcblist_head(&sdp->srcu_cblist));
  1175. s0 += c0;
  1176. s1 += c1;
  1177. }
  1178. pr_cont(" T(%ld,%ld)\n", s0, s1);
  1179. }
  1180. EXPORT_SYMBOL_GPL(srcu_torture_stats_print);
  1181. static int __init srcu_bootup_announce(void)
  1182. {
  1183. pr_info("Hierarchical SRCU implementation.\n");
  1184. if (exp_holdoff != DEFAULT_SRCU_EXP_HOLDOFF)
  1185. pr_info("\tNon-default auto-expedite holdoff of %lu ns.\n", exp_holdoff);
  1186. return 0;
  1187. }
  1188. early_initcall(srcu_bootup_announce);