Tree-RCU-Memory-Ordering.rst 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. ======================================================
  2. A Tour Through TREE_RCU's Grace-Period Memory Ordering
  3. ======================================================
  4. August 8, 2017
  5. This article was contributed by Paul E. McKenney
  6. Introduction
  7. ============
  8. This document gives a rough visual overview of how Tree RCU's
  9. grace-period memory ordering guarantee is provided.
  10. What Is Tree RCU's Grace Period Memory Ordering Guarantee?
  11. ==========================================================
  12. RCU grace periods provide extremely strong memory-ordering guarantees
  13. for non-idle non-offline code.
  14. Any code that happens after the end of a given RCU grace period is guaranteed
  15. to see the effects of all accesses prior to the beginning of that grace
  16. period that are within RCU read-side critical sections.
  17. Similarly, any code that happens before the beginning of a given RCU grace
  18. period is guaranteed to not see the effects of all accesses following the end
  19. of that grace period that are within RCU read-side critical sections.
  20. Note well that RCU-sched read-side critical sections include any region
  21. of code for which preemption is disabled.
  22. Given that each individual machine instruction can be thought of as
  23. an extremely small region of preemption-disabled code, one can think of
  24. ``synchronize_rcu()`` as ``smp_mb()`` on steroids.
  25. RCU updaters use this guarantee by splitting their updates into
  26. two phases, one of which is executed before the grace period and
  27. the other of which is executed after the grace period.
  28. In the most common use case, phase one removes an element from
  29. a linked RCU-protected data structure, and phase two frees that element.
  30. For this to work, any readers that have witnessed state prior to the
  31. phase-one update (in the common case, removal) must not witness state
  32. following the phase-two update (in the common case, freeing).
  33. The RCU implementation provides this guarantee using a network
  34. of lock-based critical sections, memory barriers, and per-CPU
  35. processing, as is described in the following sections.
  36. Tree RCU Grace Period Memory Ordering Building Blocks
  37. =====================================================
  38. The workhorse for RCU's grace-period memory ordering is the
  39. critical section for the ``rcu_node`` structure's
  40. ``->lock``. These critical sections use helper functions for lock
  41. acquisition, including ``raw_spin_lock_rcu_node()``,
  42. ``raw_spin_lock_irq_rcu_node()``, and ``raw_spin_lock_irqsave_rcu_node()``.
  43. Their lock-release counterparts are ``raw_spin_unlock_rcu_node()``,
  44. ``raw_spin_unlock_irq_rcu_node()``, and
  45. ``raw_spin_unlock_irqrestore_rcu_node()``, respectively.
  46. For completeness, a ``raw_spin_trylock_rcu_node()`` is also provided.
  47. The key point is that the lock-acquisition functions, including
  48. ``raw_spin_trylock_rcu_node()``, all invoke ``smp_mb__after_unlock_lock()``
  49. immediately after successful acquisition of the lock.
  50. Therefore, for any given ``rcu_node`` structure, any access
  51. happening before one of the above lock-release functions will be seen
  52. by all CPUs as happening before any access happening after a later
  53. one of the above lock-acquisition functions.
  54. Furthermore, any access happening before one of the
  55. above lock-release function on any given CPU will be seen by all
  56. CPUs as happening before any access happening after a later one
  57. of the above lock-acquisition functions executing on that same CPU,
  58. even if the lock-release and lock-acquisition functions are operating
  59. on different ``rcu_node`` structures.
  60. Tree RCU uses these two ordering guarantees to form an ordering
  61. network among all CPUs that were in any way involved in the grace
  62. period, including any CPUs that came online or went offline during
  63. the grace period in question.
  64. The following litmus test exhibits the ordering effects of these
  65. lock-acquisition and lock-release functions::
  66. 1 int x, y, z;
  67. 2
  68. 3 void task0(void)
  69. 4 {
  70. 5 raw_spin_lock_rcu_node(rnp);
  71. 6 WRITE_ONCE(x, 1);
  72. 7 r1 = READ_ONCE(y);
  73. 8 raw_spin_unlock_rcu_node(rnp);
  74. 9 }
  75. 10
  76. 11 void task1(void)
  77. 12 {
  78. 13 raw_spin_lock_rcu_node(rnp);
  79. 14 WRITE_ONCE(y, 1);
  80. 15 r2 = READ_ONCE(z);
  81. 16 raw_spin_unlock_rcu_node(rnp);
  82. 17 }
  83. 18
  84. 19 void task2(void)
  85. 20 {
  86. 21 WRITE_ONCE(z, 1);
  87. 22 smp_mb();
  88. 23 r3 = READ_ONCE(x);
  89. 24 }
  90. 25
  91. 26 WARN_ON(r1 == 0 && r2 == 0 && r3 == 0);
  92. The ``WARN_ON()`` is evaluated at "the end of time",
  93. after all changes have propagated throughout the system.
  94. Without the ``smp_mb__after_unlock_lock()`` provided by the
  95. acquisition functions, this ``WARN_ON()`` could trigger, for example
  96. on PowerPC.
  97. The ``smp_mb__after_unlock_lock()`` invocations prevent this
  98. ``WARN_ON()`` from triggering.
  99. +-----------------------------------------------------------------------+
  100. | **Quick Quiz**: |
  101. +-----------------------------------------------------------------------+
  102. | But the chain of rcu_node-structure lock acquisitions guarantees |
  103. | that new readers will see all of the updater's pre-grace-period |
  104. | accesses and also guarantees that the updater's post-grace-period |
  105. | accesses will see all of the old reader's accesses. So why do we |
  106. | need all of those calls to smp_mb__after_unlock_lock()? |
  107. +-----------------------------------------------------------------------+
  108. | **Answer**: |
  109. +-----------------------------------------------------------------------+
  110. | Because we must provide ordering for RCU's polling grace-period |
  111. | primitives, for example, get_state_synchronize_rcu() and |
  112. | poll_state_synchronize_rcu(). Consider this code:: |
  113. | |
  114. | CPU 0 CPU 1 |
  115. | ---- ---- |
  116. | WRITE_ONCE(X, 1) WRITE_ONCE(Y, 1) |
  117. | g = get_state_synchronize_rcu() smp_mb() |
  118. | while (!poll_state_synchronize_rcu(g)) r1 = READ_ONCE(X) |
  119. | continue; |
  120. | r0 = READ_ONCE(Y) |
  121. | |
  122. | RCU guarantees that the outcome r0 == 0 && r1 == 0 will not |
  123. | happen, even if CPU 1 is in an RCU extended quiescent state |
  124. | (idle or offline) and thus won't interact directly with the RCU |
  125. | core processing at all. |
  126. +-----------------------------------------------------------------------+
  127. This approach must be extended to include idle CPUs, which need
  128. RCU's grace-period memory ordering guarantee to extend to any
  129. RCU read-side critical sections preceding and following the current
  130. idle sojourn.
  131. This case is handled by calls to the strongly ordered
  132. ``atomic_add_return()`` read-modify-write atomic operation that
  133. is invoked within ``ct_kernel_exit_state()`` at idle-entry
  134. time and within ``ct_kernel_enter_state()`` at idle-exit time.
  135. The grace-period kthread invokes first ``ct_rcu_watching_cpu_acquire()``
  136. (preceded by a full memory barrier) and ``rcu_watching_snap_stopped_since()``
  137. (both of which rely on acquire semantics) to detect idle CPUs.
  138. +-----------------------------------------------------------------------+
  139. | **Quick Quiz**: |
  140. +-----------------------------------------------------------------------+
  141. | But what about CPUs that remain offline for the entire grace period? |
  142. +-----------------------------------------------------------------------+
  143. | **Answer**: |
  144. +-----------------------------------------------------------------------+
  145. | Such CPUs will be offline at the beginning of the grace period, so |
  146. | the grace period won't expect quiescent states from them. Races |
  147. | between grace-period start and CPU-hotplug operations are mediated |
  148. | by the CPU's leaf ``rcu_node`` structure's ``->lock`` as described |
  149. | above. |
  150. +-----------------------------------------------------------------------+
  151. The approach must be extended to handle one final case, that of waking a
  152. task blocked in ``synchronize_rcu()``. This task might be affined to
  153. a CPU that is not yet aware that the grace period has ended, and thus
  154. might not yet be subject to the grace period's memory ordering.
  155. Therefore, there is an ``smp_mb()`` after the return from
  156. ``wait_for_completion()`` in the ``synchronize_rcu()`` code path.
  157. +-----------------------------------------------------------------------+
  158. | **Quick Quiz**: |
  159. +-----------------------------------------------------------------------+
  160. | What? Where??? I don't see any ``smp_mb()`` after the return from |
  161. | ``wait_for_completion()``!!! |
  162. +-----------------------------------------------------------------------+
  163. | **Answer**: |
  164. +-----------------------------------------------------------------------+
  165. | That would be because I spotted the need for that ``smp_mb()`` during |
  166. | the creation of this documentation, and it is therefore unlikely to |
  167. | hit mainline before v4.14. Kudos to Lance Roy, Will Deacon, Peter |
  168. | Zijlstra, and Jonathan Cameron for asking questions that sensitized |
  169. | me to the rather elaborate sequence of events that demonstrate the |
  170. | need for this memory barrier. |
  171. +-----------------------------------------------------------------------+
  172. Tree RCU's grace--period memory-ordering guarantees rely most heavily on
  173. the ``rcu_node`` structure's ``->lock`` field, so much so that it is
  174. necessary to abbreviate this pattern in the diagrams in the next
  175. section. For example, consider the ``rcu_prepare_for_idle()`` function
  176. shown below, which is one of several functions that enforce ordering of
  177. newly arrived RCU callbacks against future grace periods:
  178. ::
  179. 1 static void rcu_prepare_for_idle(void)
  180. 2 {
  181. 3 bool needwake;
  182. 4 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
  183. 5 struct rcu_node *rnp;
  184. 6 int tne;
  185. 7
  186. 8 lockdep_assert_irqs_disabled();
  187. 9 if (rcu_rdp_is_offloaded(rdp))
  188. 10 return;
  189. 11
  190. 12 /* Handle nohz enablement switches conservatively. */
  191. 13 tne = READ_ONCE(tick_nohz_active);
  192. 14 if (tne != rdp->tick_nohz_enabled_snap) {
  193. 15 if (!rcu_segcblist_empty(&rdp->cblist))
  194. 16 invoke_rcu_core(); /* force nohz to see update. */
  195. 17 rdp->tick_nohz_enabled_snap = tne;
  196. 18 return;
  197. 19 }
  198. 20 if (!tne)
  199. 21 return;
  200. 22
  201. 23 /*
  202. 24 * If we have not yet accelerated this jiffy, accelerate all
  203. 25 * callbacks on this CPU.
  204. 26 */
  205. 27 if (rdp->last_accelerate == jiffies)
  206. 28 return;
  207. 29 rdp->last_accelerate = jiffies;
  208. 30 if (rcu_segcblist_pend_cbs(&rdp->cblist)) {
  209. 31 rnp = rdp->mynode;
  210. 32 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  211. 33 needwake = rcu_accelerate_cbs(rnp, rdp);
  212. 34 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  213. 35 if (needwake)
  214. 36 rcu_gp_kthread_wake();
  215. 37 }
  216. 38 }
  217. But the only part of ``rcu_prepare_for_idle()`` that really matters for
  218. this discussion are lines 32–34. We will therefore abbreviate this
  219. function as follows:
  220. .. kernel-figure:: rcu_node-lock.svg
  221. The box represents the ``rcu_node`` structure's ``->lock`` critical
  222. section, with the double line on top representing the additional
  223. ``smp_mb__after_unlock_lock()``.
  224. Tree RCU Grace Period Memory Ordering Components
  225. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  226. Tree RCU's grace-period memory-ordering guarantee is provided by a
  227. number of RCU components:
  228. #. `Callback Registry`_
  229. #. `Grace-Period Initialization`_
  230. #. `Self-Reported Quiescent States`_
  231. #. `Dynamic Tick Interface`_
  232. #. `CPU-Hotplug Interface`_
  233. #. `Forcing Quiescent States`_
  234. #. `Grace-Period Cleanup`_
  235. #. `Callback Invocation`_
  236. Each of the following section looks at the corresponding component in
  237. detail.
  238. Callback Registry
  239. ^^^^^^^^^^^^^^^^^
  240. If RCU's grace-period guarantee is to mean anything at all, any access
  241. that happens before a given invocation of ``call_rcu()`` must also
  242. happen before the corresponding grace period. The implementation of this
  243. portion of RCU's grace period guarantee is shown in the following
  244. figure:
  245. .. kernel-figure:: TreeRCU-callback-registry.svg
  246. Because ``call_rcu()`` normally acts only on CPU-local state, it
  247. provides no ordering guarantees, either for itself or for phase one of
  248. the update (which again will usually be removal of an element from an
  249. RCU-protected data structure). It simply enqueues the ``rcu_head``
  250. structure on a per-CPU list, which cannot become associated with a grace
  251. period until a later call to ``rcu_accelerate_cbs()``, as shown in the
  252. diagram above.
  253. One set of code paths shown on the left invokes ``rcu_accelerate_cbs()``
  254. via ``note_gp_changes()``, either directly from ``call_rcu()`` (if the
  255. current CPU is inundated with queued ``rcu_head`` structures) or more
  256. likely from an ``RCU_SOFTIRQ`` handler. Another code path in the middle
  257. is taken only in kernels built with ``CONFIG_RCU_FAST_NO_HZ=y``, which
  258. invokes ``rcu_accelerate_cbs()`` via ``rcu_prepare_for_idle()``. The
  259. final code path on the right is taken only in kernels built with
  260. ``CONFIG_HOTPLUG_CPU=y``, which invokes ``rcu_accelerate_cbs()`` via
  261. ``rcu_advance_cbs()``, ``rcu_migrate_callbacks``,
  262. ``rcutree_migrate_callbacks()``, and ``takedown_cpu()``, which in turn
  263. is invoked on a surviving CPU after the outgoing CPU has been completely
  264. offlined.
  265. There are a few other code paths within grace-period processing that
  266. opportunistically invoke ``rcu_accelerate_cbs()``. However, either way,
  267. all of the CPU's recently queued ``rcu_head`` structures are associated
  268. with a future grace-period number under the protection of the CPU's lead
  269. ``rcu_node`` structure's ``->lock``. In all cases, there is full
  270. ordering against any prior critical section for that same ``rcu_node``
  271. structure's ``->lock``, and also full ordering against any of the
  272. current task's or CPU's prior critical sections for any ``rcu_node``
  273. structure's ``->lock``.
  274. The next section will show how this ordering ensures that any accesses
  275. prior to the ``call_rcu()`` (particularly including phase one of the
  276. update) happen before the start of the corresponding grace period.
  277. +-----------------------------------------------------------------------+
  278. | **Quick Quiz**: |
  279. +-----------------------------------------------------------------------+
  280. | But what about ``synchronize_rcu()``? |
  281. +-----------------------------------------------------------------------+
  282. | **Answer**: |
  283. +-----------------------------------------------------------------------+
  284. | The ``synchronize_rcu()`` passes ``call_rcu()`` to ``wait_rcu_gp()``, |
  285. | which invokes it. So either way, it eventually comes down to |
  286. | ``call_rcu()``. |
  287. +-----------------------------------------------------------------------+
  288. Grace-Period Initialization
  289. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  290. Grace-period initialization is carried out by the grace-period kernel
  291. thread, which makes several passes over the ``rcu_node`` tree within the
  292. ``rcu_gp_init()`` function. This means that showing the full flow of
  293. ordering through the grace-period computation will require duplicating
  294. this tree. If you find this confusing, please note that the state of the
  295. ``rcu_node`` changes over time, just like Heraclitus's river. However,
  296. to keep the ``rcu_node`` river tractable, the grace-period kernel
  297. thread's traversals are presented in multiple parts, starting in this
  298. section with the various phases of grace-period initialization.
  299. The first ordering-related grace-period initialization action is to
  300. advance the ``rcu_state`` structure's ``->gp_seq`` grace-period-number
  301. counter, as shown below:
  302. .. kernel-figure:: TreeRCU-gp-init-1.svg
  303. The actual increment is carried out using ``smp_store_release()``, which
  304. helps reject false-positive RCU CPU stall detection. Note that only the
  305. root ``rcu_node`` structure is touched.
  306. The first pass through the ``rcu_node`` tree updates bitmasks based on
  307. CPUs having come online or gone offline since the start of the previous
  308. grace period. In the common case where the number of online CPUs for
  309. this ``rcu_node`` structure has not transitioned to or from zero, this
  310. pass will scan only the leaf ``rcu_node`` structures. However, if the
  311. number of online CPUs for a given leaf ``rcu_node`` structure has
  312. transitioned from zero, ``rcu_init_new_rnp()`` will be invoked for the
  313. first incoming CPU. Similarly, if the number of online CPUs for a given
  314. leaf ``rcu_node`` structure has transitioned to zero,
  315. ``rcu_cleanup_dead_rnp()`` will be invoked for the last outgoing CPU.
  316. The diagram below shows the path of ordering if the leftmost
  317. ``rcu_node`` structure onlines its first CPU and if the next
  318. ``rcu_node`` structure has no online CPUs (or, alternatively if the
  319. leftmost ``rcu_node`` structure offlines its last CPU and if the next
  320. ``rcu_node`` structure has no online CPUs).
  321. .. kernel-figure:: TreeRCU-gp-init-2.svg
  322. The final ``rcu_gp_init()`` pass through the ``rcu_node`` tree traverses
  323. breadth-first, setting each ``rcu_node`` structure's ``->gp_seq`` field
  324. to the newly advanced value from the ``rcu_state`` structure, as shown
  325. in the following diagram.
  326. .. kernel-figure:: TreeRCU-gp-init-3.svg
  327. This change will also cause each CPU's next call to
  328. ``__note_gp_changes()`` to notice that a new grace period has started,
  329. as described in the next section. But because the grace-period kthread
  330. started the grace period at the root (with the advancing of the
  331. ``rcu_state`` structure's ``->gp_seq`` field) before setting each leaf
  332. ``rcu_node`` structure's ``->gp_seq`` field, each CPU's observation of
  333. the start of the grace period will happen after the actual start of the
  334. grace period.
  335. +-----------------------------------------------------------------------+
  336. | **Quick Quiz**: |
  337. +-----------------------------------------------------------------------+
  338. | But what about the CPU that started the grace period? Why wouldn't it |
  339. | see the start of the grace period right when it started that grace |
  340. | period? |
  341. +-----------------------------------------------------------------------+
  342. | **Answer**: |
  343. +-----------------------------------------------------------------------+
  344. | In some deep philosophical and overly anthromorphized sense, yes, the |
  345. | CPU starting the grace period is immediately aware of having done so. |
  346. | However, if we instead assume that RCU is not self-aware, then even |
  347. | the CPU starting the grace period does not really become aware of the |
  348. | start of this grace period until its first call to |
  349. | ``__note_gp_changes()``. On the other hand, this CPU potentially gets |
  350. | early notification because it invokes ``__note_gp_changes()`` during |
  351. | its last ``rcu_gp_init()`` pass through its leaf ``rcu_node`` |
  352. | structure. |
  353. +-----------------------------------------------------------------------+
  354. Self-Reported Quiescent States
  355. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  356. When all entities that might block the grace period have reported
  357. quiescent states (or as described in a later section, had quiescent
  358. states reported on their behalf), the grace period can end. Online
  359. non-idle CPUs report their own quiescent states, as shown in the
  360. following diagram:
  361. .. kernel-figure:: TreeRCU-qs.svg
  362. This is for the last CPU to report a quiescent state, which signals the
  363. end of the grace period. Earlier quiescent states would push up the
  364. ``rcu_node`` tree only until they encountered an ``rcu_node`` structure
  365. that is waiting for additional quiescent states. However, ordering is
  366. nevertheless preserved because some later quiescent state will acquire
  367. that ``rcu_node`` structure's ``->lock``.
  368. Any number of events can lead up to a CPU invoking ``note_gp_changes``
  369. (or alternatively, directly invoking ``__note_gp_changes()``), at which
  370. point that CPU will notice the start of a new grace period while holding
  371. its leaf ``rcu_node`` lock. Therefore, all execution shown in this
  372. diagram happens after the start of the grace period. In addition, this
  373. CPU will consider any RCU read-side critical section that started before
  374. the invocation of ``__note_gp_changes()`` to have started before the
  375. grace period, and thus a critical section that the grace period must
  376. wait on.
  377. +-----------------------------------------------------------------------+
  378. | **Quick Quiz**: |
  379. +-----------------------------------------------------------------------+
  380. | But a RCU read-side critical section might have started after the |
  381. | beginning of the grace period (the advancing of ``->gp_seq`` from |
  382. | earlier), so why should the grace period wait on such a critical |
  383. | section? |
  384. +-----------------------------------------------------------------------+
  385. | **Answer**: |
  386. +-----------------------------------------------------------------------+
  387. | It is indeed not necessary for the grace period to wait on such a |
  388. | critical section. However, it is permissible to wait on it. And it is |
  389. | furthermore important to wait on it, as this lazy approach is far |
  390. | more scalable than a “big bang” all-at-once grace-period start could |
  391. | possibly be. |
  392. +-----------------------------------------------------------------------+
  393. If the CPU does a context switch, a quiescent state will be noted by
  394. ``rcu_note_context_switch()`` on the left. On the other hand, if the CPU
  395. takes a scheduler-clock interrupt while executing in usermode, a
  396. quiescent state will be noted by ``rcu_sched_clock_irq()`` on the right.
  397. Either way, the passage through a quiescent state will be noted in a
  398. per-CPU variable.
  399. The next time an ``RCU_SOFTIRQ`` handler executes on this CPU (for
  400. example, after the next scheduler-clock interrupt), ``rcu_core()`` will
  401. invoke ``rcu_check_quiescent_state()``, which will notice the recorded
  402. quiescent state, and invoke ``rcu_report_qs_rdp()``. If
  403. ``rcu_report_qs_rdp()`` verifies that the quiescent state really does
  404. apply to the current grace period, it invokes ``rcu_report_rnp()`` which
  405. traverses up the ``rcu_node`` tree as shown at the bottom of the
  406. diagram, clearing bits from each ``rcu_node`` structure's ``->qsmask``
  407. field, and propagating up the tree when the result is zero.
  408. Note that traversal passes upwards out of a given ``rcu_node`` structure
  409. only if the current CPU is reporting the last quiescent state for the
  410. subtree headed by that ``rcu_node`` structure. A key point is that if a
  411. CPU's traversal stops at a given ``rcu_node`` structure, then there will
  412. be a later traversal by another CPU (or perhaps the same one) that
  413. proceeds upwards from that point, and the ``rcu_node`` ``->lock``
  414. guarantees that the first CPU's quiescent state happens before the
  415. remainder of the second CPU's traversal. Applying this line of thought
  416. repeatedly shows that all CPUs' quiescent states happen before the last
  417. CPU traverses through the root ``rcu_node`` structure, the “last CPU”
  418. being the one that clears the last bit in the root ``rcu_node``
  419. structure's ``->qsmask`` field.
  420. Dynamic Tick Interface
  421. ^^^^^^^^^^^^^^^^^^^^^^
  422. Due to energy-efficiency considerations, RCU is forbidden from
  423. disturbing idle CPUs. CPUs are therefore required to notify RCU when
  424. entering or leaving idle state, which they do via fully ordered
  425. value-returning atomic operations on a per-CPU variable. The ordering
  426. effects are as shown below:
  427. .. kernel-figure:: TreeRCU-dyntick.svg
  428. The RCU grace-period kernel thread samples the per-CPU idleness variable
  429. while holding the corresponding CPU's leaf ``rcu_node`` structure's
  430. ``->lock``. This means that any RCU read-side critical sections that
  431. precede the idle period (the oval near the top of the diagram above)
  432. will happen before the end of the current grace period. Similarly, the
  433. beginning of the current grace period will happen before any RCU
  434. read-side critical sections that follow the idle period (the oval near
  435. the bottom of the diagram above).
  436. Plumbing this into the full grace-period execution is described
  437. `below <Forcing Quiescent States_>`__.
  438. CPU-Hotplug Interface
  439. ^^^^^^^^^^^^^^^^^^^^^
  440. RCU is also forbidden from disturbing offline CPUs, which might well be
  441. powered off and removed from the system completely. CPUs are therefore
  442. required to notify RCU of their comings and goings as part of the
  443. corresponding CPU hotplug operations. The ordering effects are shown
  444. below:
  445. .. kernel-figure:: TreeRCU-hotplug.svg
  446. Because CPU hotplug operations are much less frequent than idle
  447. transitions, they are heavier weight, and thus acquire the CPU's leaf
  448. ``rcu_node`` structure's ``->lock`` and update this structure's
  449. ``->qsmaskinitnext``. The RCU grace-period kernel thread samples this
  450. mask to detect CPUs having gone offline since the beginning of this
  451. grace period.
  452. Plumbing this into the full grace-period execution is described
  453. `below <Forcing Quiescent States_>`__.
  454. Forcing Quiescent States
  455. ^^^^^^^^^^^^^^^^^^^^^^^^
  456. As noted above, idle and offline CPUs cannot report their own quiescent
  457. states, and therefore the grace-period kernel thread must do the
  458. reporting on their behalf. This process is called “forcing quiescent
  459. states”, it is repeated every few jiffies, and its ordering effects are
  460. shown below:
  461. .. kernel-figure:: TreeRCU-gp-fqs.svg
  462. Each pass of quiescent state forcing is guaranteed to traverse the leaf
  463. ``rcu_node`` structures, and if there are no new quiescent states due to
  464. recently idled and/or offlined CPUs, then only the leaves are traversed.
  465. However, if there is a newly offlined CPU as illustrated on the left or
  466. a newly idled CPU as illustrated on the right, the corresponding
  467. quiescent state will be driven up towards the root. As with
  468. self-reported quiescent states, the upwards driving stops once it
  469. reaches an ``rcu_node`` structure that has quiescent states outstanding
  470. from other CPUs.
  471. +-----------------------------------------------------------------------+
  472. | **Quick Quiz**: |
  473. +-----------------------------------------------------------------------+
  474. | The leftmost drive to root stopped before it reached the root |
  475. | ``rcu_node`` structure, which means that there are still CPUs |
  476. | subordinate to that structure on which the current grace period is |
  477. | waiting. Given that, how is it possible that the rightmost drive to |
  478. | root ended the grace period? |
  479. +-----------------------------------------------------------------------+
  480. | **Answer**: |
  481. +-----------------------------------------------------------------------+
  482. | Good analysis! It is in fact impossible in the absence of bugs in |
  483. | RCU. But this diagram is complex enough as it is, so simplicity |
  484. | overrode accuracy. You can think of it as poetic license, or you can |
  485. | think of it as misdirection that is resolved in the |
  486. | `stitched-together diagram <Putting It All Together_>`__. |
  487. +-----------------------------------------------------------------------+
  488. Grace-Period Cleanup
  489. ^^^^^^^^^^^^^^^^^^^^
  490. Grace-period cleanup first scans the ``rcu_node`` tree breadth-first
  491. advancing all the ``->gp_seq`` fields, then it advances the
  492. ``rcu_state`` structure's ``->gp_seq`` field. The ordering effects are
  493. shown below:
  494. .. kernel-figure:: TreeRCU-gp-cleanup.svg
  495. As indicated by the oval at the bottom of the diagram, once grace-period
  496. cleanup is complete, the next grace period can begin.
  497. +-----------------------------------------------------------------------+
  498. | **Quick Quiz**: |
  499. +-----------------------------------------------------------------------+
  500. | But when precisely does the grace period end? |
  501. +-----------------------------------------------------------------------+
  502. | **Answer**: |
  503. +-----------------------------------------------------------------------+
  504. | There is no useful single point at which the grace period can be said |
  505. | to end. The earliest reasonable candidate is as soon as the last CPU |
  506. | has reported its quiescent state, but it may be some milliseconds |
  507. | before RCU becomes aware of this. The latest reasonable candidate is |
  508. | once the ``rcu_state`` structure's ``->gp_seq`` field has been |
  509. | updated, but it is quite possible that some CPUs have already |
  510. | completed phase two of their updates by that time. In short, if you |
  511. | are going to work with RCU, you need to learn to embrace uncertainty. |
  512. +-----------------------------------------------------------------------+
  513. Callback Invocation
  514. ^^^^^^^^^^^^^^^^^^^
  515. Once a given CPU's leaf ``rcu_node`` structure's ``->gp_seq`` field has
  516. been updated, that CPU can begin invoking its RCU callbacks that were
  517. waiting for this grace period to end. These callbacks are identified by
  518. ``rcu_advance_cbs()``, which is usually invoked by
  519. ``__note_gp_changes()``. As shown in the diagram below, this invocation
  520. can be triggered by the scheduling-clock interrupt
  521. (``rcu_sched_clock_irq()`` on the left) or by idle entry
  522. (``rcu_cleanup_after_idle()`` on the right, but only for kernels build
  523. with ``CONFIG_RCU_FAST_NO_HZ=y``). Either way, ``RCU_SOFTIRQ`` is
  524. raised, which results in ``rcu_do_batch()`` invoking the callbacks,
  525. which in turn allows those callbacks to carry out (either directly or
  526. indirectly via wakeup) the needed phase-two processing for each update.
  527. .. kernel-figure:: TreeRCU-callback-invocation.svg
  528. Please note that callback invocation can also be prompted by any number
  529. of corner-case code paths, for example, when a CPU notes that it has
  530. excessive numbers of callbacks queued. In all cases, the CPU acquires
  531. its leaf ``rcu_node`` structure's ``->lock`` before invoking callbacks,
  532. which preserves the required ordering against the newly completed grace
  533. period.
  534. However, if the callback function communicates to other CPUs, for
  535. example, doing a wakeup, then it is that function's responsibility to
  536. maintain ordering. For example, if the callback function wakes up a task
  537. that runs on some other CPU, proper ordering must in place in both the
  538. callback function and the task being awakened. To see why this is
  539. important, consider the top half of the `grace-period
  540. cleanup`_ diagram. The callback might be
  541. running on a CPU corresponding to the leftmost leaf ``rcu_node``
  542. structure, and awaken a task that is to run on a CPU corresponding to
  543. the rightmost leaf ``rcu_node`` structure, and the grace-period kernel
  544. thread might not yet have reached the rightmost leaf. In this case, the
  545. grace period's memory ordering might not yet have reached that CPU, so
  546. again the callback function and the awakened task must supply proper
  547. ordering.
  548. Putting It All Together
  549. ~~~~~~~~~~~~~~~~~~~~~~~
  550. A stitched-together diagram is here:
  551. .. kernel-figure:: TreeRCU-gp.svg
  552. Legal Statement
  553. ~~~~~~~~~~~~~~~
  554. This work represents the view of the author and does not necessarily
  555. represent the view of IBM.
  556. Linux is a registered trademark of Linus Torvalds.
  557. Other company, product, and service names may be trademarks or service
  558. marks of others.