srcutree.c 33 KB

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