srcu.c 20 KB

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