srcu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. /*
  39. * Initialize an rcu_batch structure to empty.
  40. */
  41. static inline void rcu_batch_init(struct rcu_batch *b)
  42. {
  43. b->head = NULL;
  44. b->tail = &b->head;
  45. }
  46. /*
  47. * Enqueue a callback onto the tail of the specified rcu_batch structure.
  48. */
  49. static inline void rcu_batch_queue(struct rcu_batch *b, struct rcu_head *head)
  50. {
  51. *b->tail = head;
  52. b->tail = &head->next;
  53. }
  54. /*
  55. * Is the specified rcu_batch structure empty?
  56. */
  57. static inline bool rcu_batch_empty(struct rcu_batch *b)
  58. {
  59. return b->tail == &b->head;
  60. }
  61. /*
  62. * Are all batches empty for the specified srcu_struct?
  63. */
  64. static inline bool rcu_all_batches_empty(struct srcu_struct *sp)
  65. {
  66. return rcu_batch_empty(&sp->batch_done) &&
  67. rcu_batch_empty(&sp->batch_check1) &&
  68. rcu_batch_empty(&sp->batch_check0) &&
  69. rcu_batch_empty(&sp->batch_queue);
  70. }
  71. /*
  72. * Remove the callback at the head of the specified rcu_batch structure
  73. * and return a pointer to it, or return NULL if the structure is empty.
  74. */
  75. static inline struct rcu_head *rcu_batch_dequeue(struct rcu_batch *b)
  76. {
  77. struct rcu_head *head;
  78. if (rcu_batch_empty(b))
  79. return NULL;
  80. head = b->head;
  81. b->head = head->next;
  82. if (b->tail == &head->next)
  83. rcu_batch_init(b);
  84. return head;
  85. }
  86. /*
  87. * Move all callbacks from the rcu_batch structure specified by "from" to
  88. * the structure specified by "to".
  89. */
  90. static inline void rcu_batch_move(struct rcu_batch *to, struct rcu_batch *from)
  91. {
  92. if (!rcu_batch_empty(from)) {
  93. *to->tail = from->head;
  94. to->tail = from->tail;
  95. rcu_batch_init(from);
  96. }
  97. }
  98. static int init_srcu_struct_fields(struct srcu_struct *sp)
  99. {
  100. sp->completed = 0;
  101. spin_lock_init(&sp->queue_lock);
  102. sp->srcu_state = SRCU_STATE_IDLE;
  103. rcu_batch_init(&sp->batch_queue);
  104. rcu_batch_init(&sp->batch_check0);
  105. rcu_batch_init(&sp->batch_check1);
  106. rcu_batch_init(&sp->batch_done);
  107. INIT_DELAYED_WORK(&sp->work, process_srcu);
  108. sp->per_cpu_ref = alloc_percpu(struct srcu_array);
  109. return sp->per_cpu_ref ? 0 : -ENOMEM;
  110. }
  111. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  112. int __init_srcu_struct(struct srcu_struct *sp, const char *name,
  113. struct lock_class_key *key)
  114. {
  115. /* Don't re-initialize a lock while it is held. */
  116. debug_check_no_locks_freed((void *)sp, sizeof(*sp));
  117. lockdep_init_map(&sp->dep_map, name, key, 0);
  118. return init_srcu_struct_fields(sp);
  119. }
  120. EXPORT_SYMBOL_GPL(__init_srcu_struct);
  121. #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  122. /**
  123. * init_srcu_struct - initialize a sleep-RCU structure
  124. * @sp: structure to initialize.
  125. *
  126. * Must invoke this on a given srcu_struct before passing that srcu_struct
  127. * to any other function. Each srcu_struct represents a separate domain
  128. * of SRCU protection.
  129. */
  130. int init_srcu_struct(struct srcu_struct *sp)
  131. {
  132. return init_srcu_struct_fields(sp);
  133. }
  134. EXPORT_SYMBOL_GPL(init_srcu_struct);
  135. #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  136. /*
  137. * Returns approximate total of the readers' ->lock_count[] values for the
  138. * rank of per-CPU counters specified by idx.
  139. */
  140. static unsigned long srcu_readers_lock_idx(struct srcu_struct *sp, int idx)
  141. {
  142. int cpu;
  143. unsigned long sum = 0;
  144. for_each_possible_cpu(cpu) {
  145. struct srcu_array *cpuc = per_cpu_ptr(sp->per_cpu_ref, cpu);
  146. sum += READ_ONCE(cpuc->lock_count[idx]);
  147. }
  148. return sum;
  149. }
  150. /*
  151. * Returns approximate total of the readers' ->unlock_count[] values for the
  152. * rank of per-CPU counters specified by idx.
  153. */
  154. static unsigned long srcu_readers_unlock_idx(struct srcu_struct *sp, int idx)
  155. {
  156. int cpu;
  157. unsigned long sum = 0;
  158. for_each_possible_cpu(cpu) {
  159. struct srcu_array *cpuc = per_cpu_ptr(sp->per_cpu_ref, cpu);
  160. sum += READ_ONCE(cpuc->unlock_count[idx]);
  161. }
  162. return sum;
  163. }
  164. /*
  165. * Return true if the number of pre-existing readers is determined to
  166. * be zero.
  167. */
  168. static bool srcu_readers_active_idx_check(struct srcu_struct *sp, int idx)
  169. {
  170. unsigned long unlocks;
  171. unlocks = srcu_readers_unlock_idx(sp, idx);
  172. /*
  173. * Make sure that a lock is always counted if the corresponding unlock
  174. * is counted. Needs to be a smp_mb() as the read side may contain a
  175. * read from a variable that is written to before the synchronize_srcu()
  176. * in the write side. In this case smp_mb()s A and B act like the store
  177. * buffering pattern.
  178. *
  179. * This smp_mb() also pairs with smp_mb() C to prevent accesses after the
  180. * synchronize_srcu() from being executed before the grace period ends.
  181. */
  182. smp_mb(); /* A */
  183. /*
  184. * If the locks are the same as the unlocks, then there must have
  185. * been no readers on this index at some time in between. This does not
  186. * mean that there are no more readers, as one could have read the
  187. * current index but not have incremented the lock counter yet.
  188. *
  189. * Possible bug: There is no guarantee that there haven't been ULONG_MAX
  190. * increments of ->lock_count[] since the unlocks were counted, meaning
  191. * that this could return true even if there are still active readers.
  192. * Since there are no memory barriers around srcu_flip(), the CPU is not
  193. * required to increment ->completed before running
  194. * srcu_readers_unlock_idx(), which means that there could be an
  195. * arbitrarily large number of critical sections that execute after
  196. * srcu_readers_unlock_idx() but use the old value of ->completed.
  197. */
  198. return srcu_readers_lock_idx(sp, idx) == unlocks;
  199. }
  200. /**
  201. * srcu_readers_active - returns true if there are readers. and false
  202. * otherwise
  203. * @sp: which srcu_struct to count active readers (holding srcu_read_lock).
  204. *
  205. * Note that this is not an atomic primitive, and can therefore suffer
  206. * severe errors when invoked on an active srcu_struct. That said, it
  207. * can be useful as an error check at cleanup time.
  208. */
  209. static bool srcu_readers_active(struct srcu_struct *sp)
  210. {
  211. int cpu;
  212. unsigned long sum = 0;
  213. for_each_possible_cpu(cpu) {
  214. struct srcu_array *cpuc = per_cpu_ptr(sp->per_cpu_ref, cpu);
  215. sum += READ_ONCE(cpuc->lock_count[0]);
  216. sum += READ_ONCE(cpuc->lock_count[1]);
  217. sum -= READ_ONCE(cpuc->unlock_count[0]);
  218. sum -= READ_ONCE(cpuc->unlock_count[1]);
  219. }
  220. return sum;
  221. }
  222. /**
  223. * cleanup_srcu_struct - deconstruct a sleep-RCU structure
  224. * @sp: structure to clean up.
  225. *
  226. * Must invoke this only after you are finished using a given srcu_struct
  227. * that was initialized via init_srcu_struct(). This code does some
  228. * probabalistic checking, spotting late uses of srcu_read_lock(),
  229. * synchronize_srcu(), synchronize_srcu_expedited(), and call_srcu().
  230. * If any such late uses are detected, the per-CPU memory associated with
  231. * the srcu_struct is simply leaked and WARN_ON() is invoked. If the
  232. * caller frees the srcu_struct itself, a use-after-free crash will likely
  233. * ensue, but at least there will be a warning printed.
  234. */
  235. void cleanup_srcu_struct(struct srcu_struct *sp)
  236. {
  237. if (WARN_ON(srcu_readers_active(sp)))
  238. return; /* Leakage unless caller handles error. */
  239. if (WARN_ON(!rcu_all_batches_empty(sp)))
  240. return; /* Leakage unless caller handles error. */
  241. flush_delayed_work(&sp->work);
  242. if (WARN_ON(READ_ONCE(sp->srcu_state) != SRCU_STATE_IDLE))
  243. return; /* Caller forgot to stop doing call_srcu()? */
  244. free_percpu(sp->per_cpu_ref);
  245. sp->per_cpu_ref = NULL;
  246. }
  247. EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
  248. /*
  249. * Counts the new reader in the appropriate per-CPU element of the
  250. * srcu_struct. Must be called from process context.
  251. * Returns an index that must be passed to the matching srcu_read_unlock().
  252. */
  253. int __srcu_read_lock(struct srcu_struct *sp)
  254. {
  255. int idx;
  256. idx = READ_ONCE(sp->completed) & 0x1;
  257. __this_cpu_inc(sp->per_cpu_ref->lock_count[idx]);
  258. smp_mb(); /* B */ /* Avoid leaking the critical section. */
  259. return idx;
  260. }
  261. EXPORT_SYMBOL_GPL(__srcu_read_lock);
  262. /*
  263. * Removes the count for the old reader from the appropriate per-CPU
  264. * element of the srcu_struct. Note that this may well be a different
  265. * CPU than that which was incremented by the corresponding srcu_read_lock().
  266. * Must be called from process context.
  267. */
  268. void __srcu_read_unlock(struct srcu_struct *sp, int idx)
  269. {
  270. smp_mb(); /* C */ /* Avoid leaking the critical section. */
  271. this_cpu_inc(sp->per_cpu_ref->unlock_count[idx]);
  272. }
  273. EXPORT_SYMBOL_GPL(__srcu_read_unlock);
  274. /*
  275. * We use an adaptive strategy for synchronize_srcu() and especially for
  276. * synchronize_srcu_expedited(). We spin for a fixed time period
  277. * (defined below) to allow SRCU readers to exit their read-side critical
  278. * sections. If there are still some readers after 10 microseconds,
  279. * we repeatedly block for 1-millisecond time periods. This approach
  280. * has done well in testing, so there is no need for a config parameter.
  281. */
  282. #define SRCU_RETRY_CHECK_DELAY 5
  283. #define SYNCHRONIZE_SRCU_TRYCOUNT 2
  284. #define SYNCHRONIZE_SRCU_EXP_TRYCOUNT 12
  285. /*
  286. * @@@ Wait until all pre-existing readers complete. Such readers
  287. * will have used the index specified by "idx".
  288. * the caller should ensures the ->completed is not changed while checking
  289. * and idx = (->completed & 1) ^ 1
  290. */
  291. static bool try_check_zero(struct srcu_struct *sp, int idx, int trycount)
  292. {
  293. for (;;) {
  294. if (srcu_readers_active_idx_check(sp, idx))
  295. return true;
  296. if (--trycount <= 0)
  297. return false;
  298. udelay(SRCU_RETRY_CHECK_DELAY);
  299. }
  300. }
  301. /*
  302. * Increment the ->completed counter so that future SRCU readers will
  303. * use the other rank of the ->(un)lock_count[] arrays. This allows
  304. * us to wait for pre-existing readers in a starvation-free manner.
  305. */
  306. static void srcu_flip(struct srcu_struct *sp)
  307. {
  308. WRITE_ONCE(sp->completed, sp->completed + 1);
  309. /*
  310. * Ensure that if the updater misses an __srcu_read_unlock()
  311. * increment, that task's next __srcu_read_lock() will see the
  312. * above counter update. Note that both this memory barrier
  313. * and the one in srcu_readers_active_idx_check() provide the
  314. * guarantee for __srcu_read_lock().
  315. */
  316. smp_mb(); /* D */ /* Pairs with C. */
  317. }
  318. /*
  319. * Enqueue an SRCU callback on the specified srcu_struct structure,
  320. * initiating grace-period processing if it is not already running.
  321. *
  322. * Note that all CPUs must agree that the grace period extended beyond
  323. * all pre-existing SRCU read-side critical section. On systems with
  324. * more than one CPU, this means that when "func()" is invoked, each CPU
  325. * is guaranteed to have executed a full memory barrier since the end of
  326. * its last corresponding SRCU read-side critical section whose beginning
  327. * preceded the call to call_rcu(). It also means that each CPU executing
  328. * an SRCU read-side critical section that continues beyond the start of
  329. * "func()" must have executed a memory barrier after the call_rcu()
  330. * but before the beginning of that SRCU read-side critical section.
  331. * Note that these guarantees include CPUs that are offline, idle, or
  332. * executing in user mode, as well as CPUs that are executing in the kernel.
  333. *
  334. * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
  335. * resulting SRCU callback function "func()", then both CPU A and CPU
  336. * B are guaranteed to execute a full memory barrier during the time
  337. * interval between the call to call_rcu() and the invocation of "func()".
  338. * This guarantee applies even if CPU A and CPU B are the same CPU (but
  339. * again only if the system has more than one CPU).
  340. *
  341. * Of course, these guarantees apply only for invocations of call_srcu(),
  342. * srcu_read_lock(), and srcu_read_unlock() that are all passed the same
  343. * srcu_struct structure.
  344. */
  345. void call_srcu(struct srcu_struct *sp, struct rcu_head *head,
  346. rcu_callback_t func)
  347. {
  348. unsigned long flags;
  349. head->next = NULL;
  350. head->func = func;
  351. spin_lock_irqsave(&sp->queue_lock, flags);
  352. smp_mb__after_unlock_lock(); /* Caller's prior accesses before GP. */
  353. rcu_batch_queue(&sp->batch_queue, head);
  354. if (READ_ONCE(sp->srcu_state) == SRCU_STATE_IDLE) {
  355. WRITE_ONCE(sp->srcu_state, SRCU_STATE_SCAN1);
  356. queue_delayed_work(system_power_efficient_wq, &sp->work, 0);
  357. }
  358. spin_unlock_irqrestore(&sp->queue_lock, flags);
  359. }
  360. EXPORT_SYMBOL_GPL(call_srcu);
  361. static void srcu_reschedule(struct srcu_struct *sp, unsigned long delay);
  362. /*
  363. * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
  364. */
  365. static void __synchronize_srcu(struct srcu_struct *sp, int trycount)
  366. {
  367. struct rcu_synchronize rcu;
  368. struct rcu_head *head = &rcu.head;
  369. RCU_LOCKDEP_WARN(lock_is_held(&sp->dep_map) ||
  370. lock_is_held(&rcu_bh_lock_map) ||
  371. lock_is_held(&rcu_lock_map) ||
  372. lock_is_held(&rcu_sched_lock_map),
  373. "Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section");
  374. if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
  375. return;
  376. might_sleep();
  377. init_completion(&rcu.completion);
  378. head->next = NULL;
  379. head->func = wakeme_after_rcu;
  380. spin_lock_irq(&sp->queue_lock);
  381. smp_mb__after_unlock_lock(); /* Caller's prior accesses before GP. */
  382. if (READ_ONCE(sp->srcu_state) == SRCU_STATE_IDLE) {
  383. /* steal the processing owner */
  384. WRITE_ONCE(sp->srcu_state, SRCU_STATE_SCAN1);
  385. rcu_batch_queue(&sp->batch_check0, head);
  386. spin_unlock_irq(&sp->queue_lock);
  387. /* give the processing owner to work_struct */
  388. srcu_reschedule(sp, 0);
  389. } else {
  390. rcu_batch_queue(&sp->batch_queue, head);
  391. spin_unlock_irq(&sp->queue_lock);
  392. }
  393. wait_for_completion(&rcu.completion);
  394. smp_mb(); /* Caller's later accesses after GP. */
  395. }
  396. /**
  397. * synchronize_srcu - wait for prior SRCU read-side critical-section completion
  398. * @sp: srcu_struct with which to synchronize.
  399. *
  400. * Wait for the count to drain to zero of both indexes. To avoid the
  401. * possible starvation of synchronize_srcu(), it waits for the count of
  402. * the index=((->completed & 1) ^ 1) to drain to zero at first,
  403. * and then flip the completed and wait for the count of the other index.
  404. *
  405. * Can block; must be called from process context.
  406. *
  407. * Note that it is illegal to call synchronize_srcu() from the corresponding
  408. * SRCU read-side critical section; doing so will result in deadlock.
  409. * However, it is perfectly legal to call synchronize_srcu() on one
  410. * srcu_struct from some other srcu_struct's read-side critical section,
  411. * as long as the resulting graph of srcu_structs is acyclic.
  412. *
  413. * There are memory-ordering constraints implied by synchronize_srcu().
  414. * On systems with more than one CPU, when synchronize_srcu() returns,
  415. * each CPU is guaranteed to have executed a full memory barrier since
  416. * the end of its last corresponding SRCU-sched read-side critical section
  417. * whose beginning preceded the call to synchronize_srcu(). In addition,
  418. * each CPU having an SRCU read-side critical section that extends beyond
  419. * the return from synchronize_srcu() is guaranteed to have executed a
  420. * full memory barrier after the beginning of synchronize_srcu() and before
  421. * the beginning of that SRCU read-side critical section. Note that these
  422. * guarantees include CPUs that are offline, idle, or executing in user mode,
  423. * as well as CPUs that are executing in the kernel.
  424. *
  425. * Furthermore, if CPU A invoked synchronize_srcu(), which returned
  426. * to its caller on CPU B, then both CPU A and CPU B are guaranteed
  427. * to have executed a full memory barrier during the execution of
  428. * synchronize_srcu(). This guarantee applies even if CPU A and CPU B
  429. * are the same CPU, but again only if the system has more than one CPU.
  430. *
  431. * Of course, these memory-ordering guarantees apply only when
  432. * synchronize_srcu(), srcu_read_lock(), and srcu_read_unlock() are
  433. * passed the same srcu_struct structure.
  434. */
  435. void synchronize_srcu(struct srcu_struct *sp)
  436. {
  437. __synchronize_srcu(sp, (rcu_gp_is_expedited() && !rcu_gp_is_normal())
  438. ? SYNCHRONIZE_SRCU_EXP_TRYCOUNT
  439. : SYNCHRONIZE_SRCU_TRYCOUNT);
  440. }
  441. EXPORT_SYMBOL_GPL(synchronize_srcu);
  442. /**
  443. * synchronize_srcu_expedited - Brute-force SRCU grace period
  444. * @sp: srcu_struct with which to synchronize.
  445. *
  446. * Wait for an SRCU grace period to elapse, but be more aggressive about
  447. * spinning rather than blocking when waiting.
  448. *
  449. * Note that synchronize_srcu_expedited() has the same deadlock and
  450. * memory-ordering properties as does synchronize_srcu().
  451. */
  452. void synchronize_srcu_expedited(struct srcu_struct *sp)
  453. {
  454. __synchronize_srcu(sp, SYNCHRONIZE_SRCU_EXP_TRYCOUNT);
  455. }
  456. EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
  457. /**
  458. * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete.
  459. * @sp: srcu_struct on which to wait for in-flight callbacks.
  460. */
  461. void srcu_barrier(struct srcu_struct *sp)
  462. {
  463. synchronize_srcu(sp);
  464. }
  465. EXPORT_SYMBOL_GPL(srcu_barrier);
  466. /**
  467. * srcu_batches_completed - return batches completed.
  468. * @sp: srcu_struct on which to report batch completion.
  469. *
  470. * Report the number of batches, correlated with, but not necessarily
  471. * precisely the same as, the number of grace periods that have elapsed.
  472. */
  473. unsigned long srcu_batches_completed(struct srcu_struct *sp)
  474. {
  475. return sp->completed;
  476. }
  477. EXPORT_SYMBOL_GPL(srcu_batches_completed);
  478. #define SRCU_CALLBACK_BATCH 10
  479. #define SRCU_INTERVAL 1
  480. /*
  481. * Move any new SRCU callbacks to the first stage of the SRCU grace
  482. * period pipeline.
  483. */
  484. static void srcu_collect_new(struct srcu_struct *sp)
  485. {
  486. if (!rcu_batch_empty(&sp->batch_queue)) {
  487. spin_lock_irq(&sp->queue_lock);
  488. rcu_batch_move(&sp->batch_check0, &sp->batch_queue);
  489. spin_unlock_irq(&sp->queue_lock);
  490. }
  491. }
  492. /*
  493. * Core SRCU state machine. Advance callbacks from ->batch_check0 to
  494. * ->batch_check1 and then to ->batch_done as readers drain.
  495. */
  496. static void srcu_advance_batches(struct srcu_struct *sp, int trycount)
  497. {
  498. int idx;
  499. WARN_ON_ONCE(sp->srcu_state == SRCU_STATE_IDLE);
  500. /*
  501. * Because readers might be delayed for an extended period after
  502. * fetching ->completed for their index, at any point in time there
  503. * might well be readers using both idx=0 and idx=1. We therefore
  504. * need to wait for readers to clear from both index values before
  505. * invoking a callback.
  506. */
  507. if (sp->srcu_state == SRCU_STATE_DONE)
  508. WRITE_ONCE(sp->srcu_state, SRCU_STATE_SCAN1);
  509. if (sp->srcu_state == SRCU_STATE_SCAN1) {
  510. idx = 1 ^ (sp->completed & 1);
  511. if (!try_check_zero(sp, idx, trycount))
  512. return; /* readers present, retry after SRCU_INTERVAL */
  513. /*
  514. * The callbacks in ->batch_check1 have already done
  515. * with their first zero check and flip back when they were
  516. * enqueued on ->batch_check0 in a previous invocation of
  517. * srcu_advance_batches(). (Presumably try_check_zero()
  518. * returned false during that invocation, leaving the
  519. * callbacks stranded on ->batch_check1.) They are therefore
  520. * ready to invoke, so move them to ->batch_done.
  521. */
  522. rcu_batch_move(&sp->batch_done, &sp->batch_check1);
  523. srcu_flip(sp);
  524. /*
  525. * The callbacks in ->batch_check0 just finished their
  526. * first check zero and flip, so move them to ->batch_check1
  527. * for future checking on the other idx.
  528. */
  529. rcu_batch_move(&sp->batch_check1, &sp->batch_check0);
  530. WRITE_ONCE(sp->srcu_state, SRCU_STATE_SCAN2);
  531. }
  532. if (sp->srcu_state == SRCU_STATE_SCAN2) {
  533. /*
  534. * SRCU read-side critical sections are normally short,
  535. * so check at least twice in quick succession after a flip.
  536. */
  537. idx = 1 ^ (sp->completed & 1);
  538. trycount = trycount < 2 ? 2 : trycount;
  539. if (!try_check_zero(sp, idx, trycount))
  540. return; /* readers present, retry after SRCU_INTERVAL */
  541. /*
  542. * The callbacks in ->batch_check1 have now waited for
  543. * all pre-existing readers using both idx values. They are
  544. * therefore ready to invoke, so move them to ->batch_done.
  545. */
  546. rcu_batch_move(&sp->batch_done, &sp->batch_check1);
  547. WRITE_ONCE(sp->srcu_state, SRCU_STATE_DONE);
  548. }
  549. }
  550. /*
  551. * Invoke a limited number of SRCU callbacks that have passed through
  552. * their grace period. If there are more to do, SRCU will reschedule
  553. * the workqueue. Note that needed memory barriers have been executed
  554. * in this task's context by srcu_readers_active_idx_check().
  555. */
  556. static void srcu_invoke_callbacks(struct srcu_struct *sp)
  557. {
  558. int i;
  559. struct rcu_head *head;
  560. for (i = 0; i < SRCU_CALLBACK_BATCH; i++) {
  561. head = rcu_batch_dequeue(&sp->batch_done);
  562. if (!head)
  563. break;
  564. local_bh_disable();
  565. head->func(head);
  566. local_bh_enable();
  567. }
  568. }
  569. /*
  570. * Finished one round of SRCU grace period. Start another if there are
  571. * more SRCU callbacks queued, otherwise put SRCU into not-running state.
  572. */
  573. static void srcu_reschedule(struct srcu_struct *sp, unsigned long delay)
  574. {
  575. bool pending = true;
  576. if (rcu_all_batches_empty(sp)) {
  577. spin_lock_irq(&sp->queue_lock);
  578. if (rcu_all_batches_empty(sp) &&
  579. READ_ONCE(sp->srcu_state) == SRCU_STATE_DONE) {
  580. WRITE_ONCE(sp->srcu_state, SRCU_STATE_IDLE);
  581. pending = false;
  582. }
  583. spin_unlock_irq(&sp->queue_lock);
  584. }
  585. if (pending)
  586. queue_delayed_work(system_power_efficient_wq, &sp->work, delay);
  587. }
  588. /*
  589. * This is the work-queue function that handles SRCU grace periods.
  590. */
  591. void process_srcu(struct work_struct *work)
  592. {
  593. struct srcu_struct *sp;
  594. sp = container_of(work, struct srcu_struct, work.work);
  595. srcu_collect_new(sp);
  596. srcu_advance_batches(sp, 1);
  597. srcu_invoke_callbacks(sp);
  598. srcu_reschedule(sp, SRCU_INTERVAL);
  599. }
  600. EXPORT_SYMBOL_GPL(process_srcu);