srcu.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2006
  19. *
  20. * Author: Paul McKenney <paulmck@us.ibm.com>
  21. *
  22. * For detailed explanation of Read-Copy Update mechanism see -
  23. * Documentation/RCU/ *.txt
  24. *
  25. */
  26. #include <linux/export.h>
  27. #include <linux/mutex.h>
  28. #include <linux/percpu.h>
  29. #include <linux/preempt.h>
  30. #include <linux/rcupdate.h>
  31. #include <linux/sched.h>
  32. #include <linux/smp.h>
  33. #include <linux/delay.h>
  34. #include <linux/srcu.h>
  35. /*
  36. * Initialize an rcu_batch structure to empty.
  37. */
  38. static inline void rcu_batch_init(struct rcu_batch *b)
  39. {
  40. b->head = NULL;
  41. b->tail = &b->head;
  42. }
  43. /*
  44. * Enqueue a callback onto the tail of the specified rcu_batch structure.
  45. */
  46. static inline void rcu_batch_queue(struct rcu_batch *b, struct rcu_head *head)
  47. {
  48. *b->tail = head;
  49. b->tail = &head->next;
  50. }
  51. /*
  52. * Is the specified rcu_batch structure empty?
  53. */
  54. static inline bool rcu_batch_empty(struct rcu_batch *b)
  55. {
  56. return b->tail == &b->head;
  57. }
  58. /*
  59. * Remove the callback at the head of the specified rcu_batch structure
  60. * and return a pointer to it, or return NULL if the structure is empty.
  61. */
  62. static inline struct rcu_head *rcu_batch_dequeue(struct rcu_batch *b)
  63. {
  64. struct rcu_head *head;
  65. if (rcu_batch_empty(b))
  66. return NULL;
  67. head = b->head;
  68. b->head = head->next;
  69. if (b->tail == &head->next)
  70. rcu_batch_init(b);
  71. return head;
  72. }
  73. /*
  74. * Move all callbacks from the rcu_batch structure specified by "from" to
  75. * the structure specified by "to".
  76. */
  77. static inline void rcu_batch_move(struct rcu_batch *to, struct rcu_batch *from)
  78. {
  79. if (!rcu_batch_empty(from)) {
  80. *to->tail = from->head;
  81. to->tail = from->tail;
  82. rcu_batch_init(from);
  83. }
  84. }
  85. /* single-thread state-machine */
  86. static void process_srcu(struct work_struct *work);
  87. static int init_srcu_struct_fields(struct srcu_struct *sp)
  88. {
  89. sp->completed = 0;
  90. spin_lock_init(&sp->queue_lock);
  91. sp->running = false;
  92. rcu_batch_init(&sp->batch_queue);
  93. rcu_batch_init(&sp->batch_check0);
  94. rcu_batch_init(&sp->batch_check1);
  95. rcu_batch_init(&sp->batch_done);
  96. INIT_DELAYED_WORK(&sp->work, process_srcu);
  97. sp->per_cpu_ref = alloc_percpu(struct srcu_struct_array);
  98. return sp->per_cpu_ref ? 0 : -ENOMEM;
  99. }
  100. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  101. int __init_srcu_struct(struct srcu_struct *sp, const char *name,
  102. struct lock_class_key *key)
  103. {
  104. /* Don't re-initialize a lock while it is held. */
  105. debug_check_no_locks_freed((void *)sp, sizeof(*sp));
  106. lockdep_init_map(&sp->dep_map, name, key, 0);
  107. return init_srcu_struct_fields(sp);
  108. }
  109. EXPORT_SYMBOL_GPL(__init_srcu_struct);
  110. #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  111. /**
  112. * init_srcu_struct - initialize a sleep-RCU structure
  113. * @sp: structure to initialize.
  114. *
  115. * Must invoke this on a given srcu_struct before passing that srcu_struct
  116. * to any other function. Each srcu_struct represents a separate domain
  117. * of SRCU protection.
  118. */
  119. int init_srcu_struct(struct srcu_struct *sp)
  120. {
  121. return init_srcu_struct_fields(sp);
  122. }
  123. EXPORT_SYMBOL_GPL(init_srcu_struct);
  124. #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  125. /*
  126. * Returns approximate total of the readers' ->seq[] values for the
  127. * rank of per-CPU counters specified by idx.
  128. */
  129. static unsigned long srcu_readers_seq_idx(struct srcu_struct *sp, int idx)
  130. {
  131. int cpu;
  132. unsigned long sum = 0;
  133. unsigned long t;
  134. for_each_possible_cpu(cpu) {
  135. t = ACCESS_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->seq[idx]);
  136. sum += t;
  137. }
  138. return sum;
  139. }
  140. /*
  141. * Returns approximate number of readers active on the specified rank
  142. * of the per-CPU ->c[] counters.
  143. */
  144. static unsigned long srcu_readers_active_idx(struct srcu_struct *sp, int idx)
  145. {
  146. int cpu;
  147. unsigned long sum = 0;
  148. unsigned long t;
  149. for_each_possible_cpu(cpu) {
  150. t = ACCESS_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->c[idx]);
  151. sum += t;
  152. }
  153. return sum;
  154. }
  155. /*
  156. * Return true if the number of pre-existing readers is determined to
  157. * be stably zero. An example unstable zero can occur if the call
  158. * to srcu_readers_active_idx() misses an __srcu_read_lock() increment,
  159. * but due to task migration, sees the corresponding __srcu_read_unlock()
  160. * decrement. This can happen because srcu_readers_active_idx() takes
  161. * time to sum the array, and might in fact be interrupted or preempted
  162. * partway through the summation.
  163. */
  164. static bool srcu_readers_active_idx_check(struct srcu_struct *sp, int idx)
  165. {
  166. unsigned long seq;
  167. seq = srcu_readers_seq_idx(sp, idx);
  168. /*
  169. * The following smp_mb() A pairs with the smp_mb() B located in
  170. * __srcu_read_lock(). This pairing ensures that if an
  171. * __srcu_read_lock() increments its counter after the summation
  172. * in srcu_readers_active_idx(), then the corresponding SRCU read-side
  173. * critical section will see any changes made prior to the start
  174. * of the current SRCU grace period.
  175. *
  176. * Also, if the above call to srcu_readers_seq_idx() saw the
  177. * increment of ->seq[], then the call to srcu_readers_active_idx()
  178. * must see the increment of ->c[].
  179. */
  180. smp_mb(); /* A */
  181. /*
  182. * Note that srcu_readers_active_idx() can incorrectly return
  183. * zero even though there is a pre-existing reader throughout.
  184. * To see this, suppose that task A is in a very long SRCU
  185. * read-side critical section that started on CPU 0, and that
  186. * no other reader exists, so that the sum of the counters
  187. * is equal to one. Then suppose that task B starts executing
  188. * srcu_readers_active_idx(), summing up to CPU 1, and then that
  189. * task C starts reading on CPU 0, so that its increment is not
  190. * summed, but finishes reading on CPU 2, so that its decrement
  191. * -is- summed. Then when task B completes its sum, it will
  192. * incorrectly get zero, despite the fact that task A has been
  193. * in its SRCU read-side critical section the whole time.
  194. *
  195. * We therefore do a validation step should srcu_readers_active_idx()
  196. * return zero.
  197. */
  198. if (srcu_readers_active_idx(sp, idx) != 0)
  199. return false;
  200. /*
  201. * The remainder of this function is the validation step.
  202. * The following smp_mb() D pairs with the smp_mb() C in
  203. * __srcu_read_unlock(). If the __srcu_read_unlock() was seen
  204. * by srcu_readers_active_idx() above, then any destructive
  205. * operation performed after the grace period will happen after
  206. * the corresponding SRCU read-side critical section.
  207. *
  208. * Note that there can be at most NR_CPUS worth of readers using
  209. * the old index, which is not enough to overflow even a 32-bit
  210. * integer. (Yes, this does mean that systems having more than
  211. * a billion or so CPUs need to be 64-bit systems.) Therefore,
  212. * the sum of the ->seq[] counters cannot possibly overflow.
  213. * Therefore, the only way that the return values of the two
  214. * calls to srcu_readers_seq_idx() can be equal is if there were
  215. * no increments of the corresponding rank of ->seq[] counts
  216. * in the interim. But the missed-increment scenario laid out
  217. * above includes an increment of the ->seq[] counter by
  218. * the corresponding __srcu_read_lock(). Therefore, if this
  219. * scenario occurs, the return values from the two calls to
  220. * srcu_readers_seq_idx() will differ, and thus the validation
  221. * step below suffices.
  222. */
  223. smp_mb(); /* D */
  224. return srcu_readers_seq_idx(sp, idx) == seq;
  225. }
  226. /**
  227. * srcu_readers_active - returns approximate number of readers.
  228. * @sp: which srcu_struct to count active readers (holding srcu_read_lock).
  229. *
  230. * Note that this is not an atomic primitive, and can therefore suffer
  231. * severe errors when invoked on an active srcu_struct. That said, it
  232. * can be useful as an error check at cleanup time.
  233. */
  234. static int srcu_readers_active(struct srcu_struct *sp)
  235. {
  236. int cpu;
  237. unsigned long sum = 0;
  238. for_each_possible_cpu(cpu) {
  239. sum += ACCESS_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->c[0]);
  240. sum += ACCESS_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->c[1]);
  241. }
  242. return sum;
  243. }
  244. /**
  245. * cleanup_srcu_struct - deconstruct a sleep-RCU structure
  246. * @sp: structure to clean up.
  247. *
  248. * Must invoke this after you are finished using a given srcu_struct that
  249. * was initialized via init_srcu_struct(), else you leak memory.
  250. */
  251. void cleanup_srcu_struct(struct srcu_struct *sp)
  252. {
  253. int sum;
  254. sum = srcu_readers_active(sp);
  255. WARN_ON(sum); /* Leakage unless caller handles error. */
  256. if (sum != 0)
  257. return;
  258. free_percpu(sp->per_cpu_ref);
  259. sp->per_cpu_ref = NULL;
  260. }
  261. EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
  262. /*
  263. * Counts the new reader in the appropriate per-CPU element of the
  264. * srcu_struct. Must be called from process context.
  265. * Returns an index that must be passed to the matching srcu_read_unlock().
  266. */
  267. int __srcu_read_lock(struct srcu_struct *sp)
  268. {
  269. int idx;
  270. preempt_disable();
  271. idx = rcu_dereference_index_check(sp->completed,
  272. rcu_read_lock_sched_held()) & 0x1;
  273. ACCESS_ONCE(this_cpu_ptr(sp->per_cpu_ref)->c[idx]) += 1;
  274. smp_mb(); /* B */ /* Avoid leaking the critical section. */
  275. ACCESS_ONCE(this_cpu_ptr(sp->per_cpu_ref)->seq[idx]) += 1;
  276. preempt_enable();
  277. return idx;
  278. }
  279. EXPORT_SYMBOL_GPL(__srcu_read_lock);
  280. /*
  281. * Removes the count for the old reader from the appropriate per-CPU
  282. * element of the srcu_struct. Note that this may well be a different
  283. * CPU than that which was incremented by the corresponding srcu_read_lock().
  284. * Must be called from process context.
  285. */
  286. void __srcu_read_unlock(struct srcu_struct *sp, int idx)
  287. {
  288. preempt_disable();
  289. smp_mb(); /* C */ /* Avoid leaking the critical section. */
  290. ACCESS_ONCE(this_cpu_ptr(sp->per_cpu_ref)->c[idx]) -= 1;
  291. preempt_enable();
  292. }
  293. EXPORT_SYMBOL_GPL(__srcu_read_unlock);
  294. /*
  295. * We use an adaptive strategy for synchronize_srcu() and especially for
  296. * synchronize_srcu_expedited(). We spin for a fixed time period
  297. * (defined below) to allow SRCU readers to exit their read-side critical
  298. * sections. If there are still some readers after 10 microseconds,
  299. * we repeatedly block for 1-millisecond time periods. This approach
  300. * has done well in testing, so there is no need for a config parameter.
  301. */
  302. #define SRCU_RETRY_CHECK_DELAY 5
  303. #define SYNCHRONIZE_SRCU_TRYCOUNT 2
  304. #define SYNCHRONIZE_SRCU_EXP_TRYCOUNT 12
  305. /*
  306. * @@@ Wait until all pre-existing readers complete. Such readers
  307. * will have used the index specified by "idx".
  308. * the caller should ensures the ->completed is not changed while checking
  309. * and idx = (->completed & 1) ^ 1
  310. */
  311. static bool try_check_zero(struct srcu_struct *sp, int idx, int trycount)
  312. {
  313. for (;;) {
  314. if (srcu_readers_active_idx_check(sp, idx))
  315. return true;
  316. if (--trycount <= 0)
  317. return false;
  318. udelay(SRCU_RETRY_CHECK_DELAY);
  319. }
  320. }
  321. /*
  322. * Increment the ->completed counter so that future SRCU readers will
  323. * use the other rank of the ->c[] and ->seq[] arrays. This allows
  324. * us to wait for pre-existing readers in a starvation-free manner.
  325. */
  326. static void srcu_flip(struct srcu_struct *sp)
  327. {
  328. sp->completed++;
  329. }
  330. /*
  331. * Enqueue an SRCU callback on the specified srcu_struct structure,
  332. * initiating grace-period processing if it is not already running.
  333. */
  334. void call_srcu(struct srcu_struct *sp, struct rcu_head *head,
  335. void (*func)(struct rcu_head *head))
  336. {
  337. unsigned long flags;
  338. head->next = NULL;
  339. head->func = func;
  340. spin_lock_irqsave(&sp->queue_lock, flags);
  341. rcu_batch_queue(&sp->batch_queue, head);
  342. if (!sp->running) {
  343. sp->running = true;
  344. queue_delayed_work(system_nrt_wq, &sp->work, 0);
  345. }
  346. spin_unlock_irqrestore(&sp->queue_lock, flags);
  347. }
  348. EXPORT_SYMBOL_GPL(call_srcu);
  349. struct rcu_synchronize {
  350. struct rcu_head head;
  351. struct completion completion;
  352. };
  353. /*
  354. * Awaken the corresponding synchronize_srcu() instance now that a
  355. * grace period has elapsed.
  356. */
  357. static void wakeme_after_rcu(struct rcu_head *head)
  358. {
  359. struct rcu_synchronize *rcu;
  360. rcu = container_of(head, struct rcu_synchronize, head);
  361. complete(&rcu->completion);
  362. }
  363. static void srcu_advance_batches(struct srcu_struct *sp, int trycount);
  364. static void srcu_reschedule(struct srcu_struct *sp);
  365. /*
  366. * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
  367. */
  368. static void __synchronize_srcu(struct srcu_struct *sp, int trycount)
  369. {
  370. struct rcu_synchronize rcu;
  371. struct rcu_head *head = &rcu.head;
  372. bool done = false;
  373. rcu_lockdep_assert(!lock_is_held(&sp->dep_map) &&
  374. !lock_is_held(&rcu_bh_lock_map) &&
  375. !lock_is_held(&rcu_lock_map) &&
  376. !lock_is_held(&rcu_sched_lock_map),
  377. "Illegal synchronize_srcu() in same-type SRCU (or RCU) read-side critical section");
  378. init_completion(&rcu.completion);
  379. head->next = NULL;
  380. head->func = wakeme_after_rcu;
  381. spin_lock_irq(&sp->queue_lock);
  382. if (!sp->running) {
  383. /* steal the processing owner */
  384. sp->running = true;
  385. rcu_batch_queue(&sp->batch_check0, head);
  386. spin_unlock_irq(&sp->queue_lock);
  387. srcu_advance_batches(sp, trycount);
  388. if (!rcu_batch_empty(&sp->batch_done)) {
  389. BUG_ON(sp->batch_done.head != head);
  390. rcu_batch_dequeue(&sp->batch_done);
  391. done = true;
  392. }
  393. /* give the processing owner to work_struct */
  394. srcu_reschedule(sp);
  395. } else {
  396. rcu_batch_queue(&sp->batch_queue, head);
  397. spin_unlock_irq(&sp->queue_lock);
  398. }
  399. if (!done)
  400. wait_for_completion(&rcu.completion);
  401. }
  402. /**
  403. * synchronize_srcu - wait for prior SRCU read-side critical-section completion
  404. * @sp: srcu_struct with which to synchronize.
  405. *
  406. * Flip the completed counter, and wait for the old count to drain to zero.
  407. * As with classic RCU, the updater must use some separate means of
  408. * synchronizing concurrent updates. Can block; must be called from
  409. * process context.
  410. *
  411. * Note that it is illegal to call synchronize_srcu() from the corresponding
  412. * SRCU read-side critical section; doing so will result in deadlock.
  413. * However, it is perfectly legal to call synchronize_srcu() on one
  414. * srcu_struct from some other srcu_struct's read-side critical section.
  415. */
  416. void synchronize_srcu(struct srcu_struct *sp)
  417. {
  418. __synchronize_srcu(sp, SYNCHRONIZE_SRCU_TRYCOUNT);
  419. }
  420. EXPORT_SYMBOL_GPL(synchronize_srcu);
  421. /**
  422. * synchronize_srcu_expedited - Brute-force SRCU grace period
  423. * @sp: srcu_struct with which to synchronize.
  424. *
  425. * Wait for an SRCU grace period to elapse, but be more aggressive about
  426. * spinning rather than blocking when waiting.
  427. *
  428. * Note that it is illegal to call this function while holding any lock
  429. * that is acquired by a CPU-hotplug notifier. It is also illegal to call
  430. * synchronize_srcu_expedited() from the corresponding SRCU read-side
  431. * critical section; doing so will result in deadlock. However, it is
  432. * perfectly legal to call synchronize_srcu_expedited() on one srcu_struct
  433. * from some other srcu_struct's read-side critical section, as long as
  434. * the resulting graph of srcu_structs is acyclic.
  435. */
  436. void synchronize_srcu_expedited(struct srcu_struct *sp)
  437. {
  438. __synchronize_srcu(sp, SYNCHRONIZE_SRCU_EXP_TRYCOUNT);
  439. }
  440. EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
  441. /**
  442. * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete.
  443. */
  444. void srcu_barrier(struct srcu_struct *sp)
  445. {
  446. synchronize_srcu(sp);
  447. }
  448. EXPORT_SYMBOL_GPL(srcu_barrier);
  449. /**
  450. * srcu_batches_completed - return batches completed.
  451. * @sp: srcu_struct on which to report batch completion.
  452. *
  453. * Report the number of batches, correlated with, but not necessarily
  454. * precisely the same as, the number of grace periods that have elapsed.
  455. */
  456. long srcu_batches_completed(struct srcu_struct *sp)
  457. {
  458. return sp->completed;
  459. }
  460. EXPORT_SYMBOL_GPL(srcu_batches_completed);
  461. #define SRCU_CALLBACK_BATCH 10
  462. #define SRCU_INTERVAL 1
  463. /*
  464. * Move any new SRCU callbacks to the first stage of the SRCU grace
  465. * period pipeline.
  466. */
  467. static void srcu_collect_new(struct srcu_struct *sp)
  468. {
  469. if (!rcu_batch_empty(&sp->batch_queue)) {
  470. spin_lock_irq(&sp->queue_lock);
  471. rcu_batch_move(&sp->batch_check0, &sp->batch_queue);
  472. spin_unlock_irq(&sp->queue_lock);
  473. }
  474. }
  475. /*
  476. * Core SRCU state machine. Advance callbacks from ->batch_check0 to
  477. * ->batch_check1 and then to ->batch_done as readers drain.
  478. */
  479. static void srcu_advance_batches(struct srcu_struct *sp, int trycount)
  480. {
  481. int idx = 1 ^ (sp->completed & 1);
  482. /*
  483. * Because readers might be delayed for an extended period after
  484. * fetching ->completed for their index, at any point in time there
  485. * might well be readers using both idx=0 and idx=1. We therefore
  486. * need to wait for readers to clear from both index values before
  487. * invoking a callback.
  488. */
  489. if (rcu_batch_empty(&sp->batch_check0) &&
  490. rcu_batch_empty(&sp->batch_check1))
  491. return; /* no callbacks need to be advanced */
  492. if (!try_check_zero(sp, idx, trycount))
  493. return; /* failed to advance, will try after SRCU_INTERVAL */
  494. /*
  495. * The callbacks in ->batch_check1 have already done with their
  496. * first zero check and flip back when they were enqueued on
  497. * ->batch_check0 in a previous invocation of srcu_advance_batches().
  498. * (Presumably try_check_zero() returned false during that
  499. * invocation, leaving the callbacks stranded on ->batch_check1.)
  500. * They are therefore ready to invoke, so move them to ->batch_done.
  501. */
  502. rcu_batch_move(&sp->batch_done, &sp->batch_check1);
  503. if (rcu_batch_empty(&sp->batch_check0))
  504. return; /* no callbacks need to be advanced */
  505. srcu_flip(sp);
  506. /*
  507. * The callbacks in ->batch_check0 just finished their
  508. * first check zero and flip, so move them to ->batch_check1
  509. * for future checking on the other idx.
  510. */
  511. rcu_batch_move(&sp->batch_check1, &sp->batch_check0);
  512. /*
  513. * SRCU read-side critical sections are normally short, so check
  514. * at least twice in quick succession after a flip.
  515. */
  516. trycount = trycount < 2 ? 2 : trycount;
  517. if (!try_check_zero(sp, idx^1, trycount))
  518. return; /* failed to advance, will try after SRCU_INTERVAL */
  519. /*
  520. * The callbacks in ->batch_check1 have now waited for all
  521. * pre-existing readers using both idx values. They are therefore
  522. * ready to invoke, so move them to ->batch_done.
  523. */
  524. rcu_batch_move(&sp->batch_done, &sp->batch_check1);
  525. }
  526. /*
  527. * Invoke a limited number of SRCU callbacks that have passed through
  528. * their grace period. If there are more to do, SRCU will reschedule
  529. * the workqueue.
  530. */
  531. static void srcu_invoke_callbacks(struct srcu_struct *sp)
  532. {
  533. int i;
  534. struct rcu_head *head;
  535. for (i = 0; i < SRCU_CALLBACK_BATCH; i++) {
  536. head = rcu_batch_dequeue(&sp->batch_done);
  537. if (!head)
  538. break;
  539. local_bh_disable();
  540. head->func(head);
  541. local_bh_enable();
  542. }
  543. }
  544. /*
  545. * Finished one round of SRCU grace period. Start another if there are
  546. * more SRCU callbacks queued, otherwise put SRCU into not-running state.
  547. */
  548. static void srcu_reschedule(struct srcu_struct *sp)
  549. {
  550. bool pending = true;
  551. if (rcu_batch_empty(&sp->batch_done) &&
  552. rcu_batch_empty(&sp->batch_check1) &&
  553. rcu_batch_empty(&sp->batch_check0) &&
  554. rcu_batch_empty(&sp->batch_queue)) {
  555. spin_lock_irq(&sp->queue_lock);
  556. if (rcu_batch_empty(&sp->batch_done) &&
  557. rcu_batch_empty(&sp->batch_check1) &&
  558. rcu_batch_empty(&sp->batch_check0) &&
  559. rcu_batch_empty(&sp->batch_queue)) {
  560. sp->running = false;
  561. pending = false;
  562. }
  563. spin_unlock_irq(&sp->queue_lock);
  564. }
  565. if (pending)
  566. queue_delayed_work(system_nrt_wq, &sp->work, SRCU_INTERVAL);
  567. }
  568. /*
  569. * This is the work-queue function that handles SRCU grace periods.
  570. */
  571. static void process_srcu(struct work_struct *work)
  572. {
  573. struct srcu_struct *sp;
  574. sp = container_of(work, struct srcu_struct, work.work);
  575. srcu_collect_new(sp);
  576. srcu_advance_batches(sp, 1);
  577. srcu_invoke_callbacks(sp);
  578. srcu_reschedule(sp);
  579. }