Requirements.html 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head><title>A Tour Through RCU's Requirements [LWN.net]</title>
  5. <meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
  6. <h1>A Tour Through RCU's Requirements</h1>
  7. <p>Copyright IBM Corporation, 2015</p>
  8. <p>Author: Paul E.&nbsp;McKenney</p>
  9. <p><i>The initial version of this document appeared in the
  10. <a href="https://lwn.net/">LWN</a> articles
  11. <a href="https://lwn.net/Articles/652156/">here</a>,
  12. <a href="https://lwn.net/Articles/652677/">here</a>, and
  13. <a href="https://lwn.net/Articles/653326/">here</a>.</i></p>
  14. <h2>Introduction</h2>
  15. <p>
  16. Read-copy update (RCU) is a synchronization mechanism that is often
  17. used as a replacement for reader-writer locking.
  18. RCU is unusual in that updaters do not block readers,
  19. which means that RCU's read-side primitives can be exceedingly fast
  20. and scalable.
  21. In addition, updaters can make useful forward progress concurrently
  22. with readers.
  23. However, all this concurrency between RCU readers and updaters does raise
  24. the question of exactly what RCU readers are doing, which in turn
  25. raises the question of exactly what RCU's requirements are.
  26. <p>
  27. This document therefore summarizes RCU's requirements, and can be thought
  28. of as an informal, high-level specification for RCU.
  29. It is important to understand that RCU's specification is primarily
  30. empirical in nature;
  31. in fact, I learned about many of these requirements the hard way.
  32. This situation might cause some consternation, however, not only
  33. has this learning process been a lot of fun, but it has also been
  34. a great privilege to work with so many people willing to apply
  35. technologies in interesting new ways.
  36. <p>
  37. All that aside, here are the categories of currently known RCU requirements:
  38. </p>
  39. <ol>
  40. <li> <a href="#Fundamental Requirements">
  41. Fundamental Requirements</a>
  42. <li> <a href="#Fundamental Non-Requirements">Fundamental Non-Requirements</a>
  43. <li> <a href="#Parallelism Facts of Life">
  44. Parallelism Facts of Life</a>
  45. <li> <a href="#Quality-of-Implementation Requirements">
  46. Quality-of-Implementation Requirements</a>
  47. <li> <a href="#Linux Kernel Complications">
  48. Linux Kernel Complications</a>
  49. <li> <a href="#Software-Engineering Requirements">
  50. Software-Engineering Requirements</a>
  51. <li> <a href="#Other RCU Flavors">
  52. Other RCU Flavors</a>
  53. <li> <a href="#Possible Future Changes">
  54. Possible Future Changes</a>
  55. </ol>
  56. <p>
  57. This is followed by a <a href="#Summary">summary</a>,
  58. however, the answers to each quick quiz immediately follows the quiz.
  59. Select the big white space with your mouse to see the answer.
  60. <h2><a name="Fundamental Requirements">Fundamental Requirements</a></h2>
  61. <p>
  62. RCU's fundamental requirements are the closest thing RCU has to hard
  63. mathematical requirements.
  64. These are:
  65. <ol>
  66. <li> <a href="#Grace-Period Guarantee">
  67. Grace-Period Guarantee</a>
  68. <li> <a href="#Publish-Subscribe Guarantee">
  69. Publish-Subscribe Guarantee</a>
  70. <li> <a href="#Memory-Barrier Guarantees">
  71. Memory-Barrier Guarantees</a>
  72. <li> <a href="#RCU Primitives Guaranteed to Execute Unconditionally">
  73. RCU Primitives Guaranteed to Execute Unconditionally</a>
  74. <li> <a href="#Guaranteed Read-to-Write Upgrade">
  75. Guaranteed Read-to-Write Upgrade</a>
  76. </ol>
  77. <h3><a name="Grace-Period Guarantee">Grace-Period Guarantee</a></h3>
  78. <p>
  79. RCU's grace-period guarantee is unusual in being premeditated:
  80. Jack Slingwine and I had this guarantee firmly in mind when we started
  81. work on RCU (then called &ldquo;rclock&rdquo;) in the early 1990s.
  82. That said, the past two decades of experience with RCU have produced
  83. a much more detailed understanding of this guarantee.
  84. <p>
  85. RCU's grace-period guarantee allows updaters to wait for the completion
  86. of all pre-existing RCU read-side critical sections.
  87. An RCU read-side critical section
  88. begins with the marker <tt>rcu_read_lock()</tt> and ends with
  89. the marker <tt>rcu_read_unlock()</tt>.
  90. These markers may be nested, and RCU treats a nested set as one
  91. big RCU read-side critical section.
  92. Production-quality implementations of <tt>rcu_read_lock()</tt> and
  93. <tt>rcu_read_unlock()</tt> are extremely lightweight, and in
  94. fact have exactly zero overhead in Linux kernels built for production
  95. use with <tt>CONFIG_PREEMPT=n</tt>.
  96. <p>
  97. This guarantee allows ordering to be enforced with extremely low
  98. overhead to readers, for example:
  99. <blockquote>
  100. <pre>
  101. 1 int x, y;
  102. 2
  103. 3 void thread0(void)
  104. 4 {
  105. 5 rcu_read_lock();
  106. 6 r1 = READ_ONCE(x);
  107. 7 r2 = READ_ONCE(y);
  108. 8 rcu_read_unlock();
  109. 9 }
  110. 10
  111. 11 void thread1(void)
  112. 12 {
  113. 13 WRITE_ONCE(x, 1);
  114. 14 synchronize_rcu();
  115. 15 WRITE_ONCE(y, 1);
  116. 16 }
  117. </pre>
  118. </blockquote>
  119. <p>
  120. Because the <tt>synchronize_rcu()</tt> on line&nbsp;14 waits for
  121. all pre-existing readers, any instance of <tt>thread0()</tt> that
  122. loads a value of zero from <tt>x</tt> must complete before
  123. <tt>thread1()</tt> stores to <tt>y</tt>, so that instance must
  124. also load a value of zero from <tt>y</tt>.
  125. Similarly, any instance of <tt>thread0()</tt> that loads a value of
  126. one from <tt>y</tt> must have started after the
  127. <tt>synchronize_rcu()</tt> started, and must therefore also load
  128. a value of one from <tt>x</tt>.
  129. Therefore, the outcome:
  130. <blockquote>
  131. <pre>
  132. (r1 == 0 &amp;&amp; r2 == 1)
  133. </pre>
  134. </blockquote>
  135. cannot happen.
  136. <table>
  137. <tr><th>&nbsp;</th></tr>
  138. <tr><th align="left">Quick Quiz:</th></tr>
  139. <tr><td>
  140. Wait a minute!
  141. You said that updaters can make useful forward progress concurrently
  142. with readers, but pre-existing readers will block
  143. <tt>synchronize_rcu()</tt>!!!
  144. Just who are you trying to fool???
  145. </td></tr>
  146. <tr><th align="left">Answer:</th></tr>
  147. <tr><td bgcolor="#ffffff"><font color="ffffff">
  148. First, if updaters do not wish to be blocked by readers, they can use
  149. <tt>call_rcu()</tt> or <tt>kfree_rcu()</tt>, which will
  150. be discussed later.
  151. Second, even when using <tt>synchronize_rcu()</tt>, the other
  152. update-side code does run concurrently with readers, whether
  153. pre-existing or not.
  154. </font></td></tr>
  155. <tr><td>&nbsp;</td></tr>
  156. </table>
  157. <p>
  158. This scenario resembles one of the first uses of RCU in
  159. <a href="https://en.wikipedia.org/wiki/DYNIX">DYNIX/ptx</a>,
  160. which managed a distributed lock manager's transition into
  161. a state suitable for handling recovery from node failure,
  162. more or less as follows:
  163. <blockquote>
  164. <pre>
  165. 1 #define STATE_NORMAL 0
  166. 2 #define STATE_WANT_RECOVERY 1
  167. 3 #define STATE_RECOVERING 2
  168. 4 #define STATE_WANT_NORMAL 3
  169. 5
  170. 6 int state = STATE_NORMAL;
  171. 7
  172. 8 void do_something_dlm(void)
  173. 9 {
  174. 10 int state_snap;
  175. 11
  176. 12 rcu_read_lock();
  177. 13 state_snap = READ_ONCE(state);
  178. 14 if (state_snap == STATE_NORMAL)
  179. 15 do_something();
  180. 16 else
  181. 17 do_something_carefully();
  182. 18 rcu_read_unlock();
  183. 19 }
  184. 20
  185. 21 void start_recovery(void)
  186. 22 {
  187. 23 WRITE_ONCE(state, STATE_WANT_RECOVERY);
  188. 24 synchronize_rcu();
  189. 25 WRITE_ONCE(state, STATE_RECOVERING);
  190. 26 recovery();
  191. 27 WRITE_ONCE(state, STATE_WANT_NORMAL);
  192. 28 synchronize_rcu();
  193. 29 WRITE_ONCE(state, STATE_NORMAL);
  194. 30 }
  195. </pre>
  196. </blockquote>
  197. <p>
  198. The RCU read-side critical section in <tt>do_something_dlm()</tt>
  199. works with the <tt>synchronize_rcu()</tt> in <tt>start_recovery()</tt>
  200. to guarantee that <tt>do_something()</tt> never runs concurrently
  201. with <tt>recovery()</tt>, but with little or no synchronization
  202. overhead in <tt>do_something_dlm()</tt>.
  203. <table>
  204. <tr><th>&nbsp;</th></tr>
  205. <tr><th align="left">Quick Quiz:</th></tr>
  206. <tr><td>
  207. Why is the <tt>synchronize_rcu()</tt> on line&nbsp;28 needed?
  208. </td></tr>
  209. <tr><th align="left">Answer:</th></tr>
  210. <tr><td bgcolor="#ffffff"><font color="ffffff">
  211. Without that extra grace period, memory reordering could result in
  212. <tt>do_something_dlm()</tt> executing <tt>do_something()</tt>
  213. concurrently with the last bits of <tt>recovery()</tt>.
  214. </font></td></tr>
  215. <tr><td>&nbsp;</td></tr>
  216. </table>
  217. <p>
  218. In order to avoid fatal problems such as deadlocks,
  219. an RCU read-side critical section must not contain calls to
  220. <tt>synchronize_rcu()</tt>.
  221. Similarly, an RCU read-side critical section must not
  222. contain anything that waits, directly or indirectly, on completion of
  223. an invocation of <tt>synchronize_rcu()</tt>.
  224. <p>
  225. Although RCU's grace-period guarantee is useful in and of itself, with
  226. <a href="https://lwn.net/Articles/573497/">quite a few use cases</a>,
  227. it would be good to be able to use RCU to coordinate read-side
  228. access to linked data structures.
  229. For this, the grace-period guarantee is not sufficient, as can
  230. be seen in function <tt>add_gp_buggy()</tt> below.
  231. We will look at the reader's code later, but in the meantime, just think of
  232. the reader as locklessly picking up the <tt>gp</tt> pointer,
  233. and, if the value loaded is non-<tt>NULL</tt>, locklessly accessing the
  234. <tt>-&gt;a</tt> and <tt>-&gt;b</tt> fields.
  235. <blockquote>
  236. <pre>
  237. 1 bool add_gp_buggy(int a, int b)
  238. 2 {
  239. 3 p = kmalloc(sizeof(*p), GFP_KERNEL);
  240. 4 if (!p)
  241. 5 return -ENOMEM;
  242. 6 spin_lock(&amp;gp_lock);
  243. 7 if (rcu_access_pointer(gp)) {
  244. 8 spin_unlock(&amp;gp_lock);
  245. 9 return false;
  246. 10 }
  247. 11 p-&gt;a = a;
  248. 12 p-&gt;b = a;
  249. 13 gp = p; /* ORDERING BUG */
  250. 14 spin_unlock(&amp;gp_lock);
  251. 15 return true;
  252. 16 }
  253. </pre>
  254. </blockquote>
  255. <p>
  256. The problem is that both the compiler and weakly ordered CPUs are within
  257. their rights to reorder this code as follows:
  258. <blockquote>
  259. <pre>
  260. 1 bool add_gp_buggy_optimized(int a, int b)
  261. 2 {
  262. 3 p = kmalloc(sizeof(*p), GFP_KERNEL);
  263. 4 if (!p)
  264. 5 return -ENOMEM;
  265. 6 spin_lock(&amp;gp_lock);
  266. 7 if (rcu_access_pointer(gp)) {
  267. 8 spin_unlock(&amp;gp_lock);
  268. 9 return false;
  269. 10 }
  270. <b>11 gp = p; /* ORDERING BUG */
  271. 12 p-&gt;a = a;
  272. 13 p-&gt;b = a;</b>
  273. 14 spin_unlock(&amp;gp_lock);
  274. 15 return true;
  275. 16 }
  276. </pre>
  277. </blockquote>
  278. <p>
  279. If an RCU reader fetches <tt>gp</tt> just after
  280. <tt>add_gp_buggy_optimized</tt> executes line&nbsp;11,
  281. it will see garbage in the <tt>-&gt;a</tt> and <tt>-&gt;b</tt>
  282. fields.
  283. And this is but one of many ways in which compiler and hardware optimizations
  284. could cause trouble.
  285. Therefore, we clearly need some way to prevent the compiler and the CPU from
  286. reordering in this manner, which brings us to the publish-subscribe
  287. guarantee discussed in the next section.
  288. <h3><a name="Publish-Subscribe Guarantee">Publish/Subscribe Guarantee</a></h3>
  289. <p>
  290. RCU's publish-subscribe guarantee allows data to be inserted
  291. into a linked data structure without disrupting RCU readers.
  292. The updater uses <tt>rcu_assign_pointer()</tt> to insert the
  293. new data, and readers use <tt>rcu_dereference()</tt> to
  294. access data, whether new or old.
  295. The following shows an example of insertion:
  296. <blockquote>
  297. <pre>
  298. 1 bool add_gp(int a, int b)
  299. 2 {
  300. 3 p = kmalloc(sizeof(*p), GFP_KERNEL);
  301. 4 if (!p)
  302. 5 return -ENOMEM;
  303. 6 spin_lock(&amp;gp_lock);
  304. 7 if (rcu_access_pointer(gp)) {
  305. 8 spin_unlock(&amp;gp_lock);
  306. 9 return false;
  307. 10 }
  308. 11 p-&gt;a = a;
  309. 12 p-&gt;b = a;
  310. 13 rcu_assign_pointer(gp, p);
  311. 14 spin_unlock(&amp;gp_lock);
  312. 15 return true;
  313. 16 }
  314. </pre>
  315. </blockquote>
  316. <p>
  317. The <tt>rcu_assign_pointer()</tt> on line&nbsp;13 is conceptually
  318. equivalent to a simple assignment statement, but also guarantees
  319. that its assignment will
  320. happen after the two assignments in lines&nbsp;11 and&nbsp;12,
  321. similar to the C11 <tt>memory_order_release</tt> store operation.
  322. It also prevents any number of &ldquo;interesting&rdquo; compiler
  323. optimizations, for example, the use of <tt>gp</tt> as a scratch
  324. location immediately preceding the assignment.
  325. <table>
  326. <tr><th>&nbsp;</th></tr>
  327. <tr><th align="left">Quick Quiz:</th></tr>
  328. <tr><td>
  329. But <tt>rcu_assign_pointer()</tt> does nothing to prevent the
  330. two assignments to <tt>p-&gt;a</tt> and <tt>p-&gt;b</tt>
  331. from being reordered.
  332. Can't that also cause problems?
  333. </td></tr>
  334. <tr><th align="left">Answer:</th></tr>
  335. <tr><td bgcolor="#ffffff"><font color="ffffff">
  336. No, it cannot.
  337. The readers cannot see either of these two fields until
  338. the assignment to <tt>gp</tt>, by which time both fields are
  339. fully initialized.
  340. So reordering the assignments
  341. to <tt>p-&gt;a</tt> and <tt>p-&gt;b</tt> cannot possibly
  342. cause any problems.
  343. </font></td></tr>
  344. <tr><td>&nbsp;</td></tr>
  345. </table>
  346. <p>
  347. It is tempting to assume that the reader need not do anything special
  348. to control its accesses to the RCU-protected data,
  349. as shown in <tt>do_something_gp_buggy()</tt> below:
  350. <blockquote>
  351. <pre>
  352. 1 bool do_something_gp_buggy(void)
  353. 2 {
  354. 3 rcu_read_lock();
  355. 4 p = gp; /* OPTIMIZATIONS GALORE!!! */
  356. 5 if (p) {
  357. 6 do_something(p-&gt;a, p-&gt;b);
  358. 7 rcu_read_unlock();
  359. 8 return true;
  360. 9 }
  361. 10 rcu_read_unlock();
  362. 11 return false;
  363. 12 }
  364. </pre>
  365. </blockquote>
  366. <p>
  367. However, this temptation must be resisted because there are a
  368. surprisingly large number of ways that the compiler
  369. (to say nothing of
  370. <a href="https://h71000.www7.hp.com/wizard/wiz_2637.html">DEC Alpha CPUs</a>)
  371. can trip this code up.
  372. For but one example, if the compiler were short of registers, it
  373. might choose to refetch from <tt>gp</tt> rather than keeping
  374. a separate copy in <tt>p</tt> as follows:
  375. <blockquote>
  376. <pre>
  377. 1 bool do_something_gp_buggy_optimized(void)
  378. 2 {
  379. 3 rcu_read_lock();
  380. 4 if (gp) { /* OPTIMIZATIONS GALORE!!! */
  381. <b> 5 do_something(gp-&gt;a, gp-&gt;b);</b>
  382. 6 rcu_read_unlock();
  383. 7 return true;
  384. 8 }
  385. 9 rcu_read_unlock();
  386. 10 return false;
  387. 11 }
  388. </pre>
  389. </blockquote>
  390. <p>
  391. If this function ran concurrently with a series of updates that
  392. replaced the current structure with a new one,
  393. the fetches of <tt>gp-&gt;a</tt>
  394. and <tt>gp-&gt;b</tt> might well come from two different structures,
  395. which could cause serious confusion.
  396. To prevent this (and much else besides), <tt>do_something_gp()</tt> uses
  397. <tt>rcu_dereference()</tt> to fetch from <tt>gp</tt>:
  398. <blockquote>
  399. <pre>
  400. 1 bool do_something_gp(void)
  401. 2 {
  402. 3 rcu_read_lock();
  403. 4 p = rcu_dereference(gp);
  404. 5 if (p) {
  405. 6 do_something(p-&gt;a, p-&gt;b);
  406. 7 rcu_read_unlock();
  407. 8 return true;
  408. 9 }
  409. 10 rcu_read_unlock();
  410. 11 return false;
  411. 12 }
  412. </pre>
  413. </blockquote>
  414. <p>
  415. The <tt>rcu_dereference()</tt> uses volatile casts and (for DEC Alpha)
  416. memory barriers in the Linux kernel.
  417. Should a
  418. <a href="http://www.rdrop.com/users/paulmck/RCU/consume.2015.07.13a.pdf">high-quality implementation of C11 <tt>memory_order_consume</tt> [PDF]</a>
  419. ever appear, then <tt>rcu_dereference()</tt> could be implemented
  420. as a <tt>memory_order_consume</tt> load.
  421. Regardless of the exact implementation, a pointer fetched by
  422. <tt>rcu_dereference()</tt> may not be used outside of the
  423. outermost RCU read-side critical section containing that
  424. <tt>rcu_dereference()</tt>, unless protection of
  425. the corresponding data element has been passed from RCU to some
  426. other synchronization mechanism, most commonly locking or
  427. <a href="https://www.kernel.org/doc/Documentation/RCU/rcuref.txt">reference counting</a>.
  428. <p>
  429. In short, updaters use <tt>rcu_assign_pointer()</tt> and readers
  430. use <tt>rcu_dereference()</tt>, and these two RCU API elements
  431. work together to ensure that readers have a consistent view of
  432. newly added data elements.
  433. <p>
  434. Of course, it is also necessary to remove elements from RCU-protected
  435. data structures, for example, using the following process:
  436. <ol>
  437. <li> Remove the data element from the enclosing structure.
  438. <li> Wait for all pre-existing RCU read-side critical sections
  439. to complete (because only pre-existing readers can possibly have
  440. a reference to the newly removed data element).
  441. <li> At this point, only the updater has a reference to the
  442. newly removed data element, so it can safely reclaim
  443. the data element, for example, by passing it to <tt>kfree()</tt>.
  444. </ol>
  445. This process is implemented by <tt>remove_gp_synchronous()</tt>:
  446. <blockquote>
  447. <pre>
  448. 1 bool remove_gp_synchronous(void)
  449. 2 {
  450. 3 struct foo *p;
  451. 4
  452. 5 spin_lock(&amp;gp_lock);
  453. 6 p = rcu_access_pointer(gp);
  454. 7 if (!p) {
  455. 8 spin_unlock(&amp;gp_lock);
  456. 9 return false;
  457. 10 }
  458. 11 rcu_assign_pointer(gp, NULL);
  459. 12 spin_unlock(&amp;gp_lock);
  460. 13 synchronize_rcu();
  461. 14 kfree(p);
  462. 15 return true;
  463. 16 }
  464. </pre>
  465. </blockquote>
  466. <p>
  467. This function is straightforward, with line&nbsp;13 waiting for a grace
  468. period before line&nbsp;14 frees the old data element.
  469. This waiting ensures that readers will reach line&nbsp;7 of
  470. <tt>do_something_gp()</tt> before the data element referenced by
  471. <tt>p</tt> is freed.
  472. The <tt>rcu_access_pointer()</tt> on line&nbsp;6 is similar to
  473. <tt>rcu_dereference()</tt>, except that:
  474. <ol>
  475. <li> The value returned by <tt>rcu_access_pointer()</tt>
  476. cannot be dereferenced.
  477. If you want to access the value pointed to as well as
  478. the pointer itself, use <tt>rcu_dereference()</tt>
  479. instead of <tt>rcu_access_pointer()</tt>.
  480. <li> The call to <tt>rcu_access_pointer()</tt> need not be
  481. protected.
  482. In contrast, <tt>rcu_dereference()</tt> must either be
  483. within an RCU read-side critical section or in a code
  484. segment where the pointer cannot change, for example, in
  485. code protected by the corresponding update-side lock.
  486. </ol>
  487. <table>
  488. <tr><th>&nbsp;</th></tr>
  489. <tr><th align="left">Quick Quiz:</th></tr>
  490. <tr><td>
  491. Without the <tt>rcu_dereference()</tt> or the
  492. <tt>rcu_access_pointer()</tt>, what destructive optimizations
  493. might the compiler make use of?
  494. </td></tr>
  495. <tr><th align="left">Answer:</th></tr>
  496. <tr><td bgcolor="#ffffff"><font color="ffffff">
  497. Let's start with what happens to <tt>do_something_gp()</tt>
  498. if it fails to use <tt>rcu_dereference()</tt>.
  499. It could reuse a value formerly fetched from this same pointer.
  500. It could also fetch the pointer from <tt>gp</tt> in a byte-at-a-time
  501. manner, resulting in <i>load tearing</i>, in turn resulting a bytewise
  502. mash-up of two distince pointer values.
  503. It might even use value-speculation optimizations, where it makes
  504. a wrong guess, but by the time it gets around to checking the
  505. value, an update has changed the pointer to match the wrong guess.
  506. Too bad about any dereferences that returned pre-initialization garbage
  507. in the meantime!
  508. </font>
  509. <p><font color="ffffff">
  510. For <tt>remove_gp_synchronous()</tt>, as long as all modifications
  511. to <tt>gp</tt> are carried out while holding <tt>gp_lock</tt>,
  512. the above optimizations are harmless.
  513. However,
  514. with <tt>CONFIG_SPARSE_RCU_POINTER=y</tt>,
  515. <tt>sparse</tt> will complain if you
  516. define <tt>gp</tt> with <tt>__rcu</tt> and then
  517. access it without using
  518. either <tt>rcu_access_pointer()</tt> or <tt>rcu_dereference()</tt>.
  519. </font></td></tr>
  520. <tr><td>&nbsp;</td></tr>
  521. </table>
  522. <p>
  523. In short, RCU's publish-subscribe guarantee is provided by the combination
  524. of <tt>rcu_assign_pointer()</tt> and <tt>rcu_dereference()</tt>.
  525. This guarantee allows data elements to be safely added to RCU-protected
  526. linked data structures without disrupting RCU readers.
  527. This guarantee can be used in combination with the grace-period
  528. guarantee to also allow data elements to be removed from RCU-protected
  529. linked data structures, again without disrupting RCU readers.
  530. <p>
  531. This guarantee was only partially premeditated.
  532. DYNIX/ptx used an explicit memory barrier for publication, but had nothing
  533. resembling <tt>rcu_dereference()</tt> for subscription, nor did it
  534. have anything resembling the <tt>smp_read_barrier_depends()</tt>
  535. that was later subsumed into <tt>rcu_dereference()</tt>.
  536. The need for these operations made itself known quite suddenly at a
  537. late-1990s meeting with the DEC Alpha architects, back in the days when
  538. DEC was still a free-standing company.
  539. It took the Alpha architects a good hour to convince me that any sort
  540. of barrier would ever be needed, and it then took me a good <i>two</i> hours
  541. to convince them that their documentation did not make this point clear.
  542. More recent work with the C and C++ standards committees have provided
  543. much education on tricks and traps from the compiler.
  544. In short, compilers were much less tricky in the early 1990s, but in
  545. 2015, don't even think about omitting <tt>rcu_dereference()</tt>!
  546. <h3><a name="Memory-Barrier Guarantees">Memory-Barrier Guarantees</a></h3>
  547. <p>
  548. The previous section's simple linked-data-structure scenario clearly
  549. demonstrates the need for RCU's stringent memory-ordering guarantees on
  550. systems with more than one CPU:
  551. <ol>
  552. <li> Each CPU that has an RCU read-side critical section that
  553. begins before <tt>synchronize_rcu()</tt> starts is
  554. guaranteed to execute a full memory barrier between the time
  555. that the RCU read-side critical section ends and the time that
  556. <tt>synchronize_rcu()</tt> returns.
  557. Without this guarantee, a pre-existing RCU read-side critical section
  558. might hold a reference to the newly removed <tt>struct foo</tt>
  559. after the <tt>kfree()</tt> on line&nbsp;14 of
  560. <tt>remove_gp_synchronous()</tt>.
  561. <li> Each CPU that has an RCU read-side critical section that ends
  562. after <tt>synchronize_rcu()</tt> returns is guaranteed
  563. to execute a full memory barrier between the time that
  564. <tt>synchronize_rcu()</tt> begins and the time that the RCU
  565. read-side critical section begins.
  566. Without this guarantee, a later RCU read-side critical section
  567. running after the <tt>kfree()</tt> on line&nbsp;14 of
  568. <tt>remove_gp_synchronous()</tt> might
  569. later run <tt>do_something_gp()</tt> and find the
  570. newly deleted <tt>struct foo</tt>.
  571. <li> If the task invoking <tt>synchronize_rcu()</tt> remains
  572. on a given CPU, then that CPU is guaranteed to execute a full
  573. memory barrier sometime during the execution of
  574. <tt>synchronize_rcu()</tt>.
  575. This guarantee ensures that the <tt>kfree()</tt> on
  576. line&nbsp;14 of <tt>remove_gp_synchronous()</tt> really does
  577. execute after the removal on line&nbsp;11.
  578. <li> If the task invoking <tt>synchronize_rcu()</tt> migrates
  579. among a group of CPUs during that invocation, then each of the
  580. CPUs in that group is guaranteed to execute a full memory barrier
  581. sometime during the execution of <tt>synchronize_rcu()</tt>.
  582. This guarantee also ensures that the <tt>kfree()</tt> on
  583. line&nbsp;14 of <tt>remove_gp_synchronous()</tt> really does
  584. execute after the removal on
  585. line&nbsp;11, but also in the case where the thread executing the
  586. <tt>synchronize_rcu()</tt> migrates in the meantime.
  587. </ol>
  588. <table>
  589. <tr><th>&nbsp;</th></tr>
  590. <tr><th align="left">Quick Quiz:</th></tr>
  591. <tr><td>
  592. Given that multiple CPUs can start RCU read-side critical sections
  593. at any time without any ordering whatsoever, how can RCU possibly
  594. tell whether or not a given RCU read-side critical section starts
  595. before a given instance of <tt>synchronize_rcu()</tt>?
  596. </td></tr>
  597. <tr><th align="left">Answer:</th></tr>
  598. <tr><td bgcolor="#ffffff"><font color="ffffff">
  599. If RCU cannot tell whether or not a given
  600. RCU read-side critical section starts before a
  601. given instance of <tt>synchronize_rcu()</tt>,
  602. then it must assume that the RCU read-side critical section
  603. started first.
  604. In other words, a given instance of <tt>synchronize_rcu()</tt>
  605. can avoid waiting on a given RCU read-side critical section only
  606. if it can prove that <tt>synchronize_rcu()</tt> started first.
  607. </font></td></tr>
  608. <tr><td>&nbsp;</td></tr>
  609. </table>
  610. <table>
  611. <tr><th>&nbsp;</th></tr>
  612. <tr><th align="left">Quick Quiz:</th></tr>
  613. <tr><td>
  614. The first and second guarantees require unbelievably strict ordering!
  615. Are all these memory barriers <i> really</i> required?
  616. </td></tr>
  617. <tr><th align="left">Answer:</th></tr>
  618. <tr><td bgcolor="#ffffff"><font color="ffffff">
  619. Yes, they really are required.
  620. To see why the first guarantee is required, consider the following
  621. sequence of events:
  622. </font>
  623. <ol>
  624. <li> <font color="ffffff">
  625. CPU 1: <tt>rcu_read_lock()</tt>
  626. </font>
  627. <li> <font color="ffffff">
  628. CPU 1: <tt>q = rcu_dereference(gp);
  629. /* Very likely to return p. */</tt>
  630. </font>
  631. <li> <font color="ffffff">
  632. CPU 0: <tt>list_del_rcu(p);</tt>
  633. </font>
  634. <li> <font color="ffffff">
  635. CPU 0: <tt>synchronize_rcu()</tt> starts.
  636. </font>
  637. <li> <font color="ffffff">
  638. CPU 1: <tt>do_something_with(q-&gt;a);
  639. /* No smp_mb(), so might happen after kfree(). */</tt>
  640. </font>
  641. <li> <font color="ffffff">
  642. CPU 1: <tt>rcu_read_unlock()</tt>
  643. </font>
  644. <li> <font color="ffffff">
  645. CPU 0: <tt>synchronize_rcu()</tt> returns.
  646. </font>
  647. <li> <font color="ffffff">
  648. CPU 0: <tt>kfree(p);</tt>
  649. </font>
  650. </ol>
  651. <p><font color="ffffff">
  652. Therefore, there absolutely must be a full memory barrier between the
  653. end of the RCU read-side critical section and the end of the
  654. grace period.
  655. </font>
  656. <p><font color="ffffff">
  657. The sequence of events demonstrating the necessity of the second rule
  658. is roughly similar:
  659. </font>
  660. <ol>
  661. <li> <font color="ffffff">CPU 0: <tt>list_del_rcu(p);</tt>
  662. </font>
  663. <li> <font color="ffffff">CPU 0: <tt>synchronize_rcu()</tt> starts.
  664. </font>
  665. <li> <font color="ffffff">CPU 1: <tt>rcu_read_lock()</tt>
  666. </font>
  667. <li> <font color="ffffff">CPU 1: <tt>q = rcu_dereference(gp);
  668. /* Might return p if no memory barrier. */</tt>
  669. </font>
  670. <li> <font color="ffffff">CPU 0: <tt>synchronize_rcu()</tt> returns.
  671. </font>
  672. <li> <font color="ffffff">CPU 0: <tt>kfree(p);</tt>
  673. </font>
  674. <li> <font color="ffffff">
  675. CPU 1: <tt>do_something_with(q-&gt;a); /* Boom!!! */</tt>
  676. </font>
  677. <li> <font color="ffffff">CPU 1: <tt>rcu_read_unlock()</tt>
  678. </font>
  679. </ol>
  680. <p><font color="ffffff">
  681. And similarly, without a memory barrier between the beginning of the
  682. grace period and the beginning of the RCU read-side critical section,
  683. CPU&nbsp;1 might end up accessing the freelist.
  684. </font>
  685. <p><font color="ffffff">
  686. The &ldquo;as if&rdquo; rule of course applies, so that any
  687. implementation that acts as if the appropriate memory barriers
  688. were in place is a correct implementation.
  689. That said, it is much easier to fool yourself into believing
  690. that you have adhered to the as-if rule than it is to actually
  691. adhere to it!
  692. </font></td></tr>
  693. <tr><td>&nbsp;</td></tr>
  694. </table>
  695. <table>
  696. <tr><th>&nbsp;</th></tr>
  697. <tr><th align="left">Quick Quiz:</th></tr>
  698. <tr><td>
  699. You claim that <tt>rcu_read_lock()</tt> and <tt>rcu_read_unlock()</tt>
  700. generate absolutely no code in some kernel builds.
  701. This means that the compiler might arbitrarily rearrange consecutive
  702. RCU read-side critical sections.
  703. Given such rearrangement, if a given RCU read-side critical section
  704. is done, how can you be sure that all prior RCU read-side critical
  705. sections are done?
  706. Won't the compiler rearrangements make that impossible to determine?
  707. </td></tr>
  708. <tr><th align="left">Answer:</th></tr>
  709. <tr><td bgcolor="#ffffff"><font color="ffffff">
  710. In cases where <tt>rcu_read_lock()</tt> and <tt>rcu_read_unlock()</tt>
  711. generate absolutely no code, RCU infers quiescent states only at
  712. special locations, for example, within the scheduler.
  713. Because calls to <tt>schedule()</tt> had better prevent calling-code
  714. accesses to shared variables from being rearranged across the call to
  715. <tt>schedule()</tt>, if RCU detects the end of a given RCU read-side
  716. critical section, it will necessarily detect the end of all prior
  717. RCU read-side critical sections, no matter how aggressively the
  718. compiler scrambles the code.
  719. </font>
  720. <p><font color="ffffff">
  721. Again, this all assumes that the compiler cannot scramble code across
  722. calls to the scheduler, out of interrupt handlers, into the idle loop,
  723. into user-mode code, and so on.
  724. But if your kernel build allows that sort of scrambling, you have broken
  725. far more than just RCU!
  726. </font></td></tr>
  727. <tr><td>&nbsp;</td></tr>
  728. </table>
  729. <p>
  730. Note that these memory-barrier requirements do not replace the fundamental
  731. RCU requirement that a grace period wait for all pre-existing readers.
  732. On the contrary, the memory barriers called out in this section must operate in
  733. such a way as to <i>enforce</i> this fundamental requirement.
  734. Of course, different implementations enforce this requirement in different
  735. ways, but enforce it they must.
  736. <h3><a name="RCU Primitives Guaranteed to Execute Unconditionally">RCU Primitives Guaranteed to Execute Unconditionally</a></h3>
  737. <p>
  738. The common-case RCU primitives are unconditional.
  739. They are invoked, they do their job, and they return, with no possibility
  740. of error, and no need to retry.
  741. This is a key RCU design philosophy.
  742. <p>
  743. However, this philosophy is pragmatic rather than pigheaded.
  744. If someone comes up with a good justification for a particular conditional
  745. RCU primitive, it might well be implemented and added.
  746. After all, this guarantee was reverse-engineered, not premeditated.
  747. The unconditional nature of the RCU primitives was initially an
  748. accident of implementation, and later experience with synchronization
  749. primitives with conditional primitives caused me to elevate this
  750. accident to a guarantee.
  751. Therefore, the justification for adding a conditional primitive to
  752. RCU would need to be based on detailed and compelling use cases.
  753. <h3><a name="Guaranteed Read-to-Write Upgrade">Guaranteed Read-to-Write Upgrade</a></h3>
  754. <p>
  755. As far as RCU is concerned, it is always possible to carry out an
  756. update within an RCU read-side critical section.
  757. For example, that RCU read-side critical section might search for
  758. a given data element, and then might acquire the update-side
  759. spinlock in order to update that element, all while remaining
  760. in that RCU read-side critical section.
  761. Of course, it is necessary to exit the RCU read-side critical section
  762. before invoking <tt>synchronize_rcu()</tt>, however, this
  763. inconvenience can be avoided through use of the
  764. <tt>call_rcu()</tt> and <tt>kfree_rcu()</tt> API members
  765. described later in this document.
  766. <table>
  767. <tr><th>&nbsp;</th></tr>
  768. <tr><th align="left">Quick Quiz:</th></tr>
  769. <tr><td>
  770. But how does the upgrade-to-write operation exclude other readers?
  771. </td></tr>
  772. <tr><th align="left">Answer:</th></tr>
  773. <tr><td bgcolor="#ffffff"><font color="ffffff">
  774. It doesn't, just like normal RCU updates, which also do not exclude
  775. RCU readers.
  776. </font></td></tr>
  777. <tr><td>&nbsp;</td></tr>
  778. </table>
  779. <p>
  780. This guarantee allows lookup code to be shared between read-side
  781. and update-side code, and was premeditated, appearing in the earliest
  782. DYNIX/ptx RCU documentation.
  783. <h2><a name="Fundamental Non-Requirements">Fundamental Non-Requirements</a></h2>
  784. <p>
  785. RCU provides extremely lightweight readers, and its read-side guarantees,
  786. though quite useful, are correspondingly lightweight.
  787. It is therefore all too easy to assume that RCU is guaranteeing more
  788. than it really is.
  789. Of course, the list of things that RCU does not guarantee is infinitely
  790. long, however, the following sections list a few non-guarantees that
  791. have caused confusion.
  792. Except where otherwise noted, these non-guarantees were premeditated.
  793. <ol>
  794. <li> <a href="#Readers Impose Minimal Ordering">
  795. Readers Impose Minimal Ordering</a>
  796. <li> <a href="#Readers Do Not Exclude Updaters">
  797. Readers Do Not Exclude Updaters</a>
  798. <li> <a href="#Updaters Only Wait For Old Readers">
  799. Updaters Only Wait For Old Readers</a>
  800. <li> <a href="#Grace Periods Don't Partition Read-Side Critical Sections">
  801. Grace Periods Don't Partition Read-Side Critical Sections</a>
  802. <li> <a href="#Read-Side Critical Sections Don't Partition Grace Periods">
  803. Read-Side Critical Sections Don't Partition Grace Periods</a>
  804. <li> <a href="#Disabling Preemption Does Not Block Grace Periods">
  805. Disabling Preemption Does Not Block Grace Periods</a>
  806. </ol>
  807. <h3><a name="Readers Impose Minimal Ordering">Readers Impose Minimal Ordering</a></h3>
  808. <p>
  809. Reader-side markers such as <tt>rcu_read_lock()</tt> and
  810. <tt>rcu_read_unlock()</tt> provide absolutely no ordering guarantees
  811. except through their interaction with the grace-period APIs such as
  812. <tt>synchronize_rcu()</tt>.
  813. To see this, consider the following pair of threads:
  814. <blockquote>
  815. <pre>
  816. 1 void thread0(void)
  817. 2 {
  818. 3 rcu_read_lock();
  819. 4 WRITE_ONCE(x, 1);
  820. 5 rcu_read_unlock();
  821. 6 rcu_read_lock();
  822. 7 WRITE_ONCE(y, 1);
  823. 8 rcu_read_unlock();
  824. 9 }
  825. 10
  826. 11 void thread1(void)
  827. 12 {
  828. 13 rcu_read_lock();
  829. 14 r1 = READ_ONCE(y);
  830. 15 rcu_read_unlock();
  831. 16 rcu_read_lock();
  832. 17 r2 = READ_ONCE(x);
  833. 18 rcu_read_unlock();
  834. 19 }
  835. </pre>
  836. </blockquote>
  837. <p>
  838. After <tt>thread0()</tt> and <tt>thread1()</tt> execute
  839. concurrently, it is quite possible to have
  840. <blockquote>
  841. <pre>
  842. (r1 == 1 &amp;&amp; r2 == 0)
  843. </pre>
  844. </blockquote>
  845. (that is, <tt>y</tt> appears to have been assigned before <tt>x</tt>),
  846. which would not be possible if <tt>rcu_read_lock()</tt> and
  847. <tt>rcu_read_unlock()</tt> had much in the way of ordering
  848. properties.
  849. But they do not, so the CPU is within its rights
  850. to do significant reordering.
  851. This is by design: Any significant ordering constraints would slow down
  852. these fast-path APIs.
  853. <table>
  854. <tr><th>&nbsp;</th></tr>
  855. <tr><th align="left">Quick Quiz:</th></tr>
  856. <tr><td>
  857. Can't the compiler also reorder this code?
  858. </td></tr>
  859. <tr><th align="left">Answer:</th></tr>
  860. <tr><td bgcolor="#ffffff"><font color="ffffff">
  861. No, the volatile casts in <tt>READ_ONCE()</tt> and
  862. <tt>WRITE_ONCE()</tt> prevent the compiler from reordering in
  863. this particular case.
  864. </font></td></tr>
  865. <tr><td>&nbsp;</td></tr>
  866. </table>
  867. <h3><a name="Readers Do Not Exclude Updaters">Readers Do Not Exclude Updaters</a></h3>
  868. <p>
  869. Neither <tt>rcu_read_lock()</tt> nor <tt>rcu_read_unlock()</tt>
  870. exclude updates.
  871. All they do is to prevent grace periods from ending.
  872. The following example illustrates this:
  873. <blockquote>
  874. <pre>
  875. 1 void thread0(void)
  876. 2 {
  877. 3 rcu_read_lock();
  878. 4 r1 = READ_ONCE(y);
  879. 5 if (r1) {
  880. 6 do_something_with_nonzero_x();
  881. 7 r2 = READ_ONCE(x);
  882. 8 WARN_ON(!r2); /* BUG!!! */
  883. 9 }
  884. 10 rcu_read_unlock();
  885. 11 }
  886. 12
  887. 13 void thread1(void)
  888. 14 {
  889. 15 spin_lock(&amp;my_lock);
  890. 16 WRITE_ONCE(x, 1);
  891. 17 WRITE_ONCE(y, 1);
  892. 18 spin_unlock(&amp;my_lock);
  893. 19 }
  894. </pre>
  895. </blockquote>
  896. <p>
  897. If the <tt>thread0()</tt> function's <tt>rcu_read_lock()</tt>
  898. excluded the <tt>thread1()</tt> function's update,
  899. the <tt>WARN_ON()</tt> could never fire.
  900. But the fact is that <tt>rcu_read_lock()</tt> does not exclude
  901. much of anything aside from subsequent grace periods, of which
  902. <tt>thread1()</tt> has none, so the
  903. <tt>WARN_ON()</tt> can and does fire.
  904. <h3><a name="Updaters Only Wait For Old Readers">Updaters Only Wait For Old Readers</a></h3>
  905. <p>
  906. It might be tempting to assume that after <tt>synchronize_rcu()</tt>
  907. completes, there are no readers executing.
  908. This temptation must be avoided because
  909. new readers can start immediately after <tt>synchronize_rcu()</tt>
  910. starts, and <tt>synchronize_rcu()</tt> is under no
  911. obligation to wait for these new readers.
  912. <table>
  913. <tr><th>&nbsp;</th></tr>
  914. <tr><th align="left">Quick Quiz:</th></tr>
  915. <tr><td>
  916. Suppose that synchronize_rcu() did wait until <i>all</i>
  917. readers had completed instead of waiting only on
  918. pre-existing readers.
  919. For how long would the updater be able to rely on there
  920. being no readers?
  921. </td></tr>
  922. <tr><th align="left">Answer:</th></tr>
  923. <tr><td bgcolor="#ffffff"><font color="ffffff">
  924. For no time at all.
  925. Even if <tt>synchronize_rcu()</tt> were to wait until
  926. all readers had completed, a new reader might start immediately after
  927. <tt>synchronize_rcu()</tt> completed.
  928. Therefore, the code following
  929. <tt>synchronize_rcu()</tt> can <i>never</i> rely on there being
  930. no readers.
  931. </font></td></tr>
  932. <tr><td>&nbsp;</td></tr>
  933. </table>
  934. <h3><a name="Grace Periods Don't Partition Read-Side Critical Sections">
  935. Grace Periods Don't Partition Read-Side Critical Sections</a></h3>
  936. <p>
  937. It is tempting to assume that if any part of one RCU read-side critical
  938. section precedes a given grace period, and if any part of another RCU
  939. read-side critical section follows that same grace period, then all of
  940. the first RCU read-side critical section must precede all of the second.
  941. However, this just isn't the case: A single grace period does not
  942. partition the set of RCU read-side critical sections.
  943. An example of this situation can be illustrated as follows, where
  944. <tt>x</tt>, <tt>y</tt>, and <tt>z</tt> are initially all zero:
  945. <blockquote>
  946. <pre>
  947. 1 void thread0(void)
  948. 2 {
  949. 3 rcu_read_lock();
  950. 4 WRITE_ONCE(a, 1);
  951. 5 WRITE_ONCE(b, 1);
  952. 6 rcu_read_unlock();
  953. 7 }
  954. 8
  955. 9 void thread1(void)
  956. 10 {
  957. 11 r1 = READ_ONCE(a);
  958. 12 synchronize_rcu();
  959. 13 WRITE_ONCE(c, 1);
  960. 14 }
  961. 15
  962. 16 void thread2(void)
  963. 17 {
  964. 18 rcu_read_lock();
  965. 19 r2 = READ_ONCE(b);
  966. 20 r3 = READ_ONCE(c);
  967. 21 rcu_read_unlock();
  968. 22 }
  969. </pre>
  970. </blockquote>
  971. <p>
  972. It turns out that the outcome:
  973. <blockquote>
  974. <pre>
  975. (r1 == 1 &amp;&amp; r2 == 0 &amp;&amp; r3 == 1)
  976. </pre>
  977. </blockquote>
  978. is entirely possible.
  979. The following figure show how this can happen, with each circled
  980. <tt>QS</tt> indicating the point at which RCU recorded a
  981. <i>quiescent state</i> for each thread, that is, a state in which
  982. RCU knows that the thread cannot be in the midst of an RCU read-side
  983. critical section that started before the current grace period:
  984. <p><img src="GPpartitionReaders1.svg" alt="GPpartitionReaders1.svg" width="60%"></p>
  985. <p>
  986. If it is necessary to partition RCU read-side critical sections in this
  987. manner, it is necessary to use two grace periods, where the first
  988. grace period is known to end before the second grace period starts:
  989. <blockquote>
  990. <pre>
  991. 1 void thread0(void)
  992. 2 {
  993. 3 rcu_read_lock();
  994. 4 WRITE_ONCE(a, 1);
  995. 5 WRITE_ONCE(b, 1);
  996. 6 rcu_read_unlock();
  997. 7 }
  998. 8
  999. 9 void thread1(void)
  1000. 10 {
  1001. 11 r1 = READ_ONCE(a);
  1002. 12 synchronize_rcu();
  1003. 13 WRITE_ONCE(c, 1);
  1004. 14 }
  1005. 15
  1006. 16 void thread2(void)
  1007. 17 {
  1008. 18 r2 = READ_ONCE(c);
  1009. 19 synchronize_rcu();
  1010. 20 WRITE_ONCE(d, 1);
  1011. 21 }
  1012. 22
  1013. 23 void thread3(void)
  1014. 24 {
  1015. 25 rcu_read_lock();
  1016. 26 r3 = READ_ONCE(b);
  1017. 27 r4 = READ_ONCE(d);
  1018. 28 rcu_read_unlock();
  1019. 29 }
  1020. </pre>
  1021. </blockquote>
  1022. <p>
  1023. Here, if <tt>(r1 == 1)</tt>, then
  1024. <tt>thread0()</tt>'s write to <tt>b</tt> must happen
  1025. before the end of <tt>thread1()</tt>'s grace period.
  1026. If in addition <tt>(r4 == 1)</tt>, then
  1027. <tt>thread3()</tt>'s read from <tt>b</tt> must happen
  1028. after the beginning of <tt>thread2()</tt>'s grace period.
  1029. If it is also the case that <tt>(r2 == 1)</tt>, then the
  1030. end of <tt>thread1()</tt>'s grace period must precede the
  1031. beginning of <tt>thread2()</tt>'s grace period.
  1032. This mean that the two RCU read-side critical sections cannot overlap,
  1033. guaranteeing that <tt>(r3 == 1)</tt>.
  1034. As a result, the outcome:
  1035. <blockquote>
  1036. <pre>
  1037. (r1 == 1 &amp;&amp; r2 == 1 &amp;&amp; r3 == 0 &amp;&amp; r4 == 1)
  1038. </pre>
  1039. </blockquote>
  1040. cannot happen.
  1041. <p>
  1042. This non-requirement was also non-premeditated, but became apparent
  1043. when studying RCU's interaction with memory ordering.
  1044. <h3><a name="Read-Side Critical Sections Don't Partition Grace Periods">
  1045. Read-Side Critical Sections Don't Partition Grace Periods</a></h3>
  1046. <p>
  1047. It is also tempting to assume that if an RCU read-side critical section
  1048. happens between a pair of grace periods, then those grace periods cannot
  1049. overlap.
  1050. However, this temptation leads nowhere good, as can be illustrated by
  1051. the following, with all variables initially zero:
  1052. <blockquote>
  1053. <pre>
  1054. 1 void thread0(void)
  1055. 2 {
  1056. 3 rcu_read_lock();
  1057. 4 WRITE_ONCE(a, 1);
  1058. 5 WRITE_ONCE(b, 1);
  1059. 6 rcu_read_unlock();
  1060. 7 }
  1061. 8
  1062. 9 void thread1(void)
  1063. 10 {
  1064. 11 r1 = READ_ONCE(a);
  1065. 12 synchronize_rcu();
  1066. 13 WRITE_ONCE(c, 1);
  1067. 14 }
  1068. 15
  1069. 16 void thread2(void)
  1070. 17 {
  1071. 18 rcu_read_lock();
  1072. 19 WRITE_ONCE(d, 1);
  1073. 20 r2 = READ_ONCE(c);
  1074. 21 rcu_read_unlock();
  1075. 22 }
  1076. 23
  1077. 24 void thread3(void)
  1078. 25 {
  1079. 26 r3 = READ_ONCE(d);
  1080. 27 synchronize_rcu();
  1081. 28 WRITE_ONCE(e, 1);
  1082. 29 }
  1083. 30
  1084. 31 void thread4(void)
  1085. 32 {
  1086. 33 rcu_read_lock();
  1087. 34 r4 = READ_ONCE(b);
  1088. 35 r5 = READ_ONCE(e);
  1089. 36 rcu_read_unlock();
  1090. 37 }
  1091. </pre>
  1092. </blockquote>
  1093. <p>
  1094. In this case, the outcome:
  1095. <blockquote>
  1096. <pre>
  1097. (r1 == 1 &amp;&amp; r2 == 1 &amp;&amp; r3 == 1 &amp;&amp; r4 == 0 &amp&amp; r5 == 1)
  1098. </pre>
  1099. </blockquote>
  1100. is entirely possible, as illustrated below:
  1101. <p><img src="ReadersPartitionGP1.svg" alt="ReadersPartitionGP1.svg" width="100%"></p>
  1102. <p>
  1103. Again, an RCU read-side critical section can overlap almost all of a
  1104. given grace period, just so long as it does not overlap the entire
  1105. grace period.
  1106. As a result, an RCU read-side critical section cannot partition a pair
  1107. of RCU grace periods.
  1108. <table>
  1109. <tr><th>&nbsp;</th></tr>
  1110. <tr><th align="left">Quick Quiz:</th></tr>
  1111. <tr><td>
  1112. How long a sequence of grace periods, each separated by an RCU
  1113. read-side critical section, would be required to partition the RCU
  1114. read-side critical sections at the beginning and end of the chain?
  1115. </td></tr>
  1116. <tr><th align="left">Answer:</th></tr>
  1117. <tr><td bgcolor="#ffffff"><font color="ffffff">
  1118. In theory, an infinite number.
  1119. In practice, an unknown number that is sensitive to both implementation
  1120. details and timing considerations.
  1121. Therefore, even in practice, RCU users must abide by the
  1122. theoretical rather than the practical answer.
  1123. </font></td></tr>
  1124. <tr><td>&nbsp;</td></tr>
  1125. </table>
  1126. <h3><a name="Disabling Preemption Does Not Block Grace Periods">
  1127. Disabling Preemption Does Not Block Grace Periods</a></h3>
  1128. <p>
  1129. There was a time when disabling preemption on any given CPU would block
  1130. subsequent grace periods.
  1131. However, this was an accident of implementation and is not a requirement.
  1132. And in the current Linux-kernel implementation, disabling preemption
  1133. on a given CPU in fact does not block grace periods, as Oleg Nesterov
  1134. <a href="https://lkml.kernel.org/g/20150614193825.GA19582@redhat.com">demonstrated</a>.
  1135. <p>
  1136. If you need a preempt-disable region to block grace periods, you need to add
  1137. <tt>rcu_read_lock()</tt> and <tt>rcu_read_unlock()</tt>, for example
  1138. as follows:
  1139. <blockquote>
  1140. <pre>
  1141. 1 preempt_disable();
  1142. 2 rcu_read_lock();
  1143. 3 do_something();
  1144. 4 rcu_read_unlock();
  1145. 5 preempt_enable();
  1146. 6
  1147. 7 /* Spinlocks implicitly disable preemption. */
  1148. 8 spin_lock(&amp;mylock);
  1149. 9 rcu_read_lock();
  1150. 10 do_something();
  1151. 11 rcu_read_unlock();
  1152. 12 spin_unlock(&amp;mylock);
  1153. </pre>
  1154. </blockquote>
  1155. <p>
  1156. In theory, you could enter the RCU read-side critical section first,
  1157. but it is more efficient to keep the entire RCU read-side critical
  1158. section contained in the preempt-disable region as shown above.
  1159. Of course, RCU read-side critical sections that extend outside of
  1160. preempt-disable regions will work correctly, but such critical sections
  1161. can be preempted, which forces <tt>rcu_read_unlock()</tt> to do
  1162. more work.
  1163. And no, this is <i>not</i> an invitation to enclose all of your RCU
  1164. read-side critical sections within preempt-disable regions, because
  1165. doing so would degrade real-time response.
  1166. <p>
  1167. This non-requirement appeared with preemptible RCU.
  1168. If you need a grace period that waits on non-preemptible code regions, use
  1169. <a href="#Sched Flavor">RCU-sched</a>.
  1170. <h2><a name="Parallelism Facts of Life">Parallelism Facts of Life</a></h2>
  1171. <p>
  1172. These parallelism facts of life are by no means specific to RCU, but
  1173. the RCU implementation must abide by them.
  1174. They therefore bear repeating:
  1175. <ol>
  1176. <li> Any CPU or task may be delayed at any time,
  1177. and any attempts to avoid these delays by disabling
  1178. preemption, interrupts, or whatever are completely futile.
  1179. This is most obvious in preemptible user-level
  1180. environments and in virtualized environments (where
  1181. a given guest OS's VCPUs can be preempted at any time by
  1182. the underlying hypervisor), but can also happen in bare-metal
  1183. environments due to ECC errors, NMIs, and other hardware
  1184. events.
  1185. Although a delay of more than about 20 seconds can result
  1186. in splats, the RCU implementation is obligated to use
  1187. algorithms that can tolerate extremely long delays, but where
  1188. &ldquo;extremely long&rdquo; is not long enough to allow
  1189. wrap-around when incrementing a 64-bit counter.
  1190. <li> Both the compiler and the CPU can reorder memory accesses.
  1191. Where it matters, RCU must use compiler directives and
  1192. memory-barrier instructions to preserve ordering.
  1193. <li> Conflicting writes to memory locations in any given cache line
  1194. will result in expensive cache misses.
  1195. Greater numbers of concurrent writes and more-frequent
  1196. concurrent writes will result in more dramatic slowdowns.
  1197. RCU is therefore obligated to use algorithms that have
  1198. sufficient locality to avoid significant performance and
  1199. scalability problems.
  1200. <li> As a rough rule of thumb, only one CPU's worth of processing
  1201. may be carried out under the protection of any given exclusive
  1202. lock.
  1203. RCU must therefore use scalable locking designs.
  1204. <li> Counters are finite, especially on 32-bit systems.
  1205. RCU's use of counters must therefore tolerate counter wrap,
  1206. or be designed such that counter wrap would take way more
  1207. time than a single system is likely to run.
  1208. An uptime of ten years is quite possible, a runtime
  1209. of a century much less so.
  1210. As an example of the latter, RCU's dyntick-idle nesting counter
  1211. allows 54 bits for interrupt nesting level (this counter
  1212. is 64 bits even on a 32-bit system).
  1213. Overflowing this counter requires 2<sup>54</sup>
  1214. half-interrupts on a given CPU without that CPU ever going idle.
  1215. If a half-interrupt happened every microsecond, it would take
  1216. 570 years of runtime to overflow this counter, which is currently
  1217. believed to be an acceptably long time.
  1218. <li> Linux systems can have thousands of CPUs running a single
  1219. Linux kernel in a single shared-memory environment.
  1220. RCU must therefore pay close attention to high-end scalability.
  1221. </ol>
  1222. <p>
  1223. This last parallelism fact of life means that RCU must pay special
  1224. attention to the preceding facts of life.
  1225. The idea that Linux might scale to systems with thousands of CPUs would
  1226. have been met with some skepticism in the 1990s, but these requirements
  1227. would have otherwise have been unsurprising, even in the early 1990s.
  1228. <h2><a name="Quality-of-Implementation Requirements">Quality-of-Implementation Requirements</a></h2>
  1229. <p>
  1230. These sections list quality-of-implementation requirements.
  1231. Although an RCU implementation that ignores these requirements could
  1232. still be used, it would likely be subject to limitations that would
  1233. make it inappropriate for industrial-strength production use.
  1234. Classes of quality-of-implementation requirements are as follows:
  1235. <ol>
  1236. <li> <a href="#Specialization">Specialization</a>
  1237. <li> <a href="#Performance and Scalability">Performance and Scalability</a>
  1238. <li> <a href="#Composability">Composability</a>
  1239. <li> <a href="#Corner Cases">Corner Cases</a>
  1240. </ol>
  1241. <p>
  1242. These classes is covered in the following sections.
  1243. <h3><a name="Specialization">Specialization</a></h3>
  1244. <p>
  1245. RCU is and always has been intended primarily for read-mostly situations,
  1246. which means that RCU's read-side primitives are optimized, often at the
  1247. expense of its update-side primitives.
  1248. Experience thus far is captured by the following list of situations:
  1249. <ol>
  1250. <li> Read-mostly data, where stale and inconsistent data is not
  1251. a problem: RCU works great!
  1252. <li> Read-mostly data, where data must be consistent:
  1253. RCU works well.
  1254. <li> Read-write data, where data must be consistent:
  1255. RCU <i>might</i> work OK.
  1256. Or not.
  1257. <li> Write-mostly data, where data must be consistent:
  1258. RCU is very unlikely to be the right tool for the job,
  1259. with the following exceptions, where RCU can provide:
  1260. <ol type=a>
  1261. <li> Existence guarantees for update-friendly mechanisms.
  1262. <li> Wait-free read-side primitives for real-time use.
  1263. </ol>
  1264. </ol>
  1265. <p>
  1266. This focus on read-mostly situations means that RCU must interoperate
  1267. with other synchronization primitives.
  1268. For example, the <tt>add_gp()</tt> and <tt>remove_gp_synchronous()</tt>
  1269. examples discussed earlier use RCU to protect readers and locking to
  1270. coordinate updaters.
  1271. However, the need extends much farther, requiring that a variety of
  1272. synchronization primitives be legal within RCU read-side critical sections,
  1273. including spinlocks, sequence locks, atomic operations, reference
  1274. counters, and memory barriers.
  1275. <table>
  1276. <tr><th>&nbsp;</th></tr>
  1277. <tr><th align="left">Quick Quiz:</th></tr>
  1278. <tr><td>
  1279. What about sleeping locks?
  1280. </td></tr>
  1281. <tr><th align="left">Answer:</th></tr>
  1282. <tr><td bgcolor="#ffffff"><font color="ffffff">
  1283. These are forbidden within Linux-kernel RCU read-side critical
  1284. sections because it is not legal to place a quiescent state
  1285. (in this case, voluntary context switch) within an RCU read-side
  1286. critical section.
  1287. However, sleeping locks may be used within userspace RCU read-side
  1288. critical sections, and also within Linux-kernel sleepable RCU
  1289. <a href="#Sleepable RCU"><font color="ffffff">(SRCU)</font></a>
  1290. read-side critical sections.
  1291. In addition, the -rt patchset turns spinlocks into a
  1292. sleeping locks so that the corresponding critical sections
  1293. can be preempted, which also means that these sleeplockified
  1294. spinlocks (but not other sleeping locks!) may be acquire within
  1295. -rt-Linux-kernel RCU read-side critical sections.
  1296. </font>
  1297. <p><font color="ffffff">
  1298. Note that it <i>is</i> legal for a normal RCU read-side
  1299. critical section to conditionally acquire a sleeping locks
  1300. (as in <tt>mutex_trylock()</tt>), but only as long as it does
  1301. not loop indefinitely attempting to conditionally acquire that
  1302. sleeping locks.
  1303. The key point is that things like <tt>mutex_trylock()</tt>
  1304. either return with the mutex held, or return an error indication if
  1305. the mutex was not immediately available.
  1306. Either way, <tt>mutex_trylock()</tt> returns immediately without
  1307. sleeping.
  1308. </font></td></tr>
  1309. <tr><td>&nbsp;</td></tr>
  1310. </table>
  1311. <p>
  1312. It often comes as a surprise that many algorithms do not require a
  1313. consistent view of data, but many can function in that mode,
  1314. with network routing being the poster child.
  1315. Internet routing algorithms take significant time to propagate
  1316. updates, so that by the time an update arrives at a given system,
  1317. that system has been sending network traffic the wrong way for
  1318. a considerable length of time.
  1319. Having a few threads continue to send traffic the wrong way for a
  1320. few more milliseconds is clearly not a problem: In the worst case,
  1321. TCP retransmissions will eventually get the data where it needs to go.
  1322. In general, when tracking the state of the universe outside of the
  1323. computer, some level of inconsistency must be tolerated due to
  1324. speed-of-light delays if nothing else.
  1325. <p>
  1326. Furthermore, uncertainty about external state is inherent in many cases.
  1327. For example, a pair of veternarians might use heartbeat to determine
  1328. whether or not a given cat was alive.
  1329. But how long should they wait after the last heartbeat to decide that
  1330. the cat is in fact dead?
  1331. Waiting less than 400 milliseconds makes no sense because this would
  1332. mean that a relaxed cat would be considered to cycle between death
  1333. and life more than 100 times per minute.
  1334. Moreover, just as with human beings, a cat's heart might stop for
  1335. some period of time, so the exact wait period is a judgment call.
  1336. One of our pair of veternarians might wait 30 seconds before pronouncing
  1337. the cat dead, while the other might insist on waiting a full minute.
  1338. The two veternarians would then disagree on the state of the cat during
  1339. the final 30 seconds of the minute following the last heartbeat.
  1340. <p>
  1341. Interestingly enough, this same situation applies to hardware.
  1342. When push comes to shove, how do we tell whether or not some
  1343. external server has failed?
  1344. We send messages to it periodically, and declare it failed if we
  1345. don't receive a response within a given period of time.
  1346. Policy decisions can usually tolerate short
  1347. periods of inconsistency.
  1348. The policy was decided some time ago, and is only now being put into
  1349. effect, so a few milliseconds of delay is normally inconsequential.
  1350. <p>
  1351. However, there are algorithms that absolutely must see consistent data.
  1352. For example, the translation between a user-level SystemV semaphore
  1353. ID to the corresponding in-kernel data structure is protected by RCU,
  1354. but it is absolutely forbidden to update a semaphore that has just been
  1355. removed.
  1356. In the Linux kernel, this need for consistency is accommodated by acquiring
  1357. spinlocks located in the in-kernel data structure from within
  1358. the RCU read-side critical section, and this is indicated by the
  1359. green box in the figure above.
  1360. Many other techniques may be used, and are in fact used within the
  1361. Linux kernel.
  1362. <p>
  1363. In short, RCU is not required to maintain consistency, and other
  1364. mechanisms may be used in concert with RCU when consistency is required.
  1365. RCU's specialization allows it to do its job extremely well, and its
  1366. ability to interoperate with other synchronization mechanisms allows
  1367. the right mix of synchronization tools to be used for a given job.
  1368. <h3><a name="Performance and Scalability">Performance and Scalability</a></h3>
  1369. <p>
  1370. Energy efficiency is a critical component of performance today,
  1371. and Linux-kernel RCU implementations must therefore avoid unnecessarily
  1372. awakening idle CPUs.
  1373. I cannot claim that this requirement was premeditated.
  1374. In fact, I learned of it during a telephone conversation in which I
  1375. was given &ldquo;frank and open&rdquo; feedback on the importance
  1376. of energy efficiency in battery-powered systems and on specific
  1377. energy-efficiency shortcomings of the Linux-kernel RCU implementation.
  1378. In my experience, the battery-powered embedded community will consider
  1379. any unnecessary wakeups to be extremely unfriendly acts.
  1380. So much so that mere Linux-kernel-mailing-list posts are
  1381. insufficient to vent their ire.
  1382. <p>
  1383. Memory consumption is not particularly important for in most
  1384. situations, and has become decreasingly
  1385. so as memory sizes have expanded and memory
  1386. costs have plummeted.
  1387. However, as I learned from Matt Mackall's
  1388. <a href="http://elinux.org/Linux_Tiny-FAQ">bloatwatch</a>
  1389. efforts, memory footprint is critically important on single-CPU systems with
  1390. non-preemptible (<tt>CONFIG_PREEMPT=n</tt>) kernels, and thus
  1391. <a href="https://lkml.kernel.org/g/20090113221724.GA15307@linux.vnet.ibm.com">tiny RCU</a>
  1392. was born.
  1393. Josh Triplett has since taken over the small-memory banner with his
  1394. <a href="https://tiny.wiki.kernel.org/">Linux kernel tinification</a>
  1395. project, which resulted in
  1396. <a href="#Sleepable RCU">SRCU</a>
  1397. becoming optional for those kernels not needing it.
  1398. <p>
  1399. The remaining performance requirements are, for the most part,
  1400. unsurprising.
  1401. For example, in keeping with RCU's read-side specialization,
  1402. <tt>rcu_dereference()</tt> should have negligible overhead (for
  1403. example, suppression of a few minor compiler optimizations).
  1404. Similarly, in non-preemptible environments, <tt>rcu_read_lock()</tt> and
  1405. <tt>rcu_read_unlock()</tt> should have exactly zero overhead.
  1406. <p>
  1407. In preemptible environments, in the case where the RCU read-side
  1408. critical section was not preempted (as will be the case for the
  1409. highest-priority real-time process), <tt>rcu_read_lock()</tt> and
  1410. <tt>rcu_read_unlock()</tt> should have minimal overhead.
  1411. In particular, they should not contain atomic read-modify-write
  1412. operations, memory-barrier instructions, preemption disabling,
  1413. interrupt disabling, or backwards branches.
  1414. However, in the case where the RCU read-side critical section was preempted,
  1415. <tt>rcu_read_unlock()</tt> may acquire spinlocks and disable interrupts.
  1416. This is why it is better to nest an RCU read-side critical section
  1417. within a preempt-disable region than vice versa, at least in cases
  1418. where that critical section is short enough to avoid unduly degrading
  1419. real-time latencies.
  1420. <p>
  1421. The <tt>synchronize_rcu()</tt> grace-period-wait primitive is
  1422. optimized for throughput.
  1423. It may therefore incur several milliseconds of latency in addition to
  1424. the duration of the longest RCU read-side critical section.
  1425. On the other hand, multiple concurrent invocations of
  1426. <tt>synchronize_rcu()</tt> are required to use batching optimizations
  1427. so that they can be satisfied by a single underlying grace-period-wait
  1428. operation.
  1429. For example, in the Linux kernel, it is not unusual for a single
  1430. grace-period-wait operation to serve more than
  1431. <a href="https://www.usenix.org/conference/2004-usenix-annual-technical-conference/making-rcu-safe-deep-sub-millisecond-response">1,000 separate invocations</a>
  1432. of <tt>synchronize_rcu()</tt>, thus amortizing the per-invocation
  1433. overhead down to nearly zero.
  1434. However, the grace-period optimization is also required to avoid
  1435. measurable degradation of real-time scheduling and interrupt latencies.
  1436. <p>
  1437. In some cases, the multi-millisecond <tt>synchronize_rcu()</tt>
  1438. latencies are unacceptable.
  1439. In these cases, <tt>synchronize_rcu_expedited()</tt> may be used
  1440. instead, reducing the grace-period latency down to a few tens of
  1441. microseconds on small systems, at least in cases where the RCU read-side
  1442. critical sections are short.
  1443. There are currently no special latency requirements for
  1444. <tt>synchronize_rcu_expedited()</tt> on large systems, but,
  1445. consistent with the empirical nature of the RCU specification,
  1446. that is subject to change.
  1447. However, there most definitely are scalability requirements:
  1448. A storm of <tt>synchronize_rcu_expedited()</tt> invocations on 4096
  1449. CPUs should at least make reasonable forward progress.
  1450. In return for its shorter latencies, <tt>synchronize_rcu_expedited()</tt>
  1451. is permitted to impose modest degradation of real-time latency
  1452. on non-idle online CPUs.
  1453. That said, it will likely be necessary to take further steps to reduce this
  1454. degradation, hopefully to roughly that of a scheduling-clock interrupt.
  1455. <p>
  1456. There are a number of situations where even
  1457. <tt>synchronize_rcu_expedited()</tt>'s reduced grace-period
  1458. latency is unacceptable.
  1459. In these situations, the asynchronous <tt>call_rcu()</tt> can be
  1460. used in place of <tt>synchronize_rcu()</tt> as follows:
  1461. <blockquote>
  1462. <pre>
  1463. 1 struct foo {
  1464. 2 int a;
  1465. 3 int b;
  1466. 4 struct rcu_head rh;
  1467. 5 };
  1468. 6
  1469. 7 static void remove_gp_cb(struct rcu_head *rhp)
  1470. 8 {
  1471. 9 struct foo *p = container_of(rhp, struct foo, rh);
  1472. 10
  1473. 11 kfree(p);
  1474. 12 }
  1475. 13
  1476. 14 bool remove_gp_asynchronous(void)
  1477. 15 {
  1478. 16 struct foo *p;
  1479. 17
  1480. 18 spin_lock(&amp;gp_lock);
  1481. 19 p = rcu_dereference(gp);
  1482. 20 if (!p) {
  1483. 21 spin_unlock(&amp;gp_lock);
  1484. 22 return false;
  1485. 23 }
  1486. 24 rcu_assign_pointer(gp, NULL);
  1487. 25 call_rcu(&amp;p-&gt;rh, remove_gp_cb);
  1488. 26 spin_unlock(&amp;gp_lock);
  1489. 27 return true;
  1490. 28 }
  1491. </pre>
  1492. </blockquote>
  1493. <p>
  1494. A definition of <tt>struct foo</tt> is finally needed, and appears
  1495. on lines&nbsp;1-5.
  1496. The function <tt>remove_gp_cb()</tt> is passed to <tt>call_rcu()</tt>
  1497. on line&nbsp;25, and will be invoked after the end of a subsequent
  1498. grace period.
  1499. This gets the same effect as <tt>remove_gp_synchronous()</tt>,
  1500. but without forcing the updater to wait for a grace period to elapse.
  1501. The <tt>call_rcu()</tt> function may be used in a number of
  1502. situations where neither <tt>synchronize_rcu()</tt> nor
  1503. <tt>synchronize_rcu_expedited()</tt> would be legal,
  1504. including within preempt-disable code, <tt>local_bh_disable()</tt> code,
  1505. interrupt-disable code, and interrupt handlers.
  1506. However, even <tt>call_rcu()</tt> is illegal within NMI handlers
  1507. and from idle and offline CPUs.
  1508. The callback function (<tt>remove_gp_cb()</tt> in this case) will be
  1509. executed within softirq (software interrupt) environment within the
  1510. Linux kernel,
  1511. either within a real softirq handler or under the protection
  1512. of <tt>local_bh_disable()</tt>.
  1513. In both the Linux kernel and in userspace, it is bad practice to
  1514. write an RCU callback function that takes too long.
  1515. Long-running operations should be relegated to separate threads or
  1516. (in the Linux kernel) workqueues.
  1517. <table>
  1518. <tr><th>&nbsp;</th></tr>
  1519. <tr><th align="left">Quick Quiz:</th></tr>
  1520. <tr><td>
  1521. Why does line&nbsp;19 use <tt>rcu_access_pointer()</tt>?
  1522. After all, <tt>call_rcu()</tt> on line&nbsp;25 stores into the
  1523. structure, which would interact badly with concurrent insertions.
  1524. Doesn't this mean that <tt>rcu_dereference()</tt> is required?
  1525. </td></tr>
  1526. <tr><th align="left">Answer:</th></tr>
  1527. <tr><td bgcolor="#ffffff"><font color="ffffff">
  1528. Presumably the <tt>-&gt;gp_lock</tt> acquired on line&nbsp;18 excludes
  1529. any changes, including any insertions that <tt>rcu_dereference()</tt>
  1530. would protect against.
  1531. Therefore, any insertions will be delayed until after
  1532. <tt>-&gt;gp_lock</tt>
  1533. is released on line&nbsp;25, which in turn means that
  1534. <tt>rcu_access_pointer()</tt> suffices.
  1535. </font></td></tr>
  1536. <tr><td>&nbsp;</td></tr>
  1537. </table>
  1538. <p>
  1539. However, all that <tt>remove_gp_cb()</tt> is doing is
  1540. invoking <tt>kfree()</tt> on the data element.
  1541. This is a common idiom, and is supported by <tt>kfree_rcu()</tt>,
  1542. which allows &ldquo;fire and forget&rdquo; operation as shown below:
  1543. <blockquote>
  1544. <pre>
  1545. 1 struct foo {
  1546. 2 int a;
  1547. 3 int b;
  1548. 4 struct rcu_head rh;
  1549. 5 };
  1550. 6
  1551. 7 bool remove_gp_faf(void)
  1552. 8 {
  1553. 9 struct foo *p;
  1554. 10
  1555. 11 spin_lock(&amp;gp_lock);
  1556. 12 p = rcu_dereference(gp);
  1557. 13 if (!p) {
  1558. 14 spin_unlock(&amp;gp_lock);
  1559. 15 return false;
  1560. 16 }
  1561. 17 rcu_assign_pointer(gp, NULL);
  1562. 18 kfree_rcu(p, rh);
  1563. 19 spin_unlock(&amp;gp_lock);
  1564. 20 return true;
  1565. 21 }
  1566. </pre>
  1567. </blockquote>
  1568. <p>
  1569. Note that <tt>remove_gp_faf()</tt> simply invokes
  1570. <tt>kfree_rcu()</tt> and proceeds, without any need to pay any
  1571. further attention to the subsequent grace period and <tt>kfree()</tt>.
  1572. It is permissible to invoke <tt>kfree_rcu()</tt> from the same
  1573. environments as for <tt>call_rcu()</tt>.
  1574. Interestingly enough, DYNIX/ptx had the equivalents of
  1575. <tt>call_rcu()</tt> and <tt>kfree_rcu()</tt>, but not
  1576. <tt>synchronize_rcu()</tt>.
  1577. This was due to the fact that RCU was not heavily used within DYNIX/ptx,
  1578. so the very few places that needed something like
  1579. <tt>synchronize_rcu()</tt> simply open-coded it.
  1580. <table>
  1581. <tr><th>&nbsp;</th></tr>
  1582. <tr><th align="left">Quick Quiz:</th></tr>
  1583. <tr><td>
  1584. Earlier it was claimed that <tt>call_rcu()</tt> and
  1585. <tt>kfree_rcu()</tt> allowed updaters to avoid being blocked
  1586. by readers.
  1587. But how can that be correct, given that the invocation of the callback
  1588. and the freeing of the memory (respectively) must still wait for
  1589. a grace period to elapse?
  1590. </td></tr>
  1591. <tr><th align="left">Answer:</th></tr>
  1592. <tr><td bgcolor="#ffffff"><font color="ffffff">
  1593. We could define things this way, but keep in mind that this sort of
  1594. definition would say that updates in garbage-collected languages
  1595. cannot complete until the next time the garbage collector runs,
  1596. which does not seem at all reasonable.
  1597. The key point is that in most cases, an updater using either
  1598. <tt>call_rcu()</tt> or <tt>kfree_rcu()</tt> can proceed to the
  1599. next update as soon as it has invoked <tt>call_rcu()</tt> or
  1600. <tt>kfree_rcu()</tt>, without having to wait for a subsequent
  1601. grace period.
  1602. </font></td></tr>
  1603. <tr><td>&nbsp;</td></tr>
  1604. </table>
  1605. <p>
  1606. But what if the updater must wait for the completion of code to be
  1607. executed after the end of the grace period, but has other tasks
  1608. that can be carried out in the meantime?
  1609. The polling-style <tt>get_state_synchronize_rcu()</tt> and
  1610. <tt>cond_synchronize_rcu()</tt> functions may be used for this
  1611. purpose, as shown below:
  1612. <blockquote>
  1613. <pre>
  1614. 1 bool remove_gp_poll(void)
  1615. 2 {
  1616. 3 struct foo *p;
  1617. 4 unsigned long s;
  1618. 5
  1619. 6 spin_lock(&amp;gp_lock);
  1620. 7 p = rcu_access_pointer(gp);
  1621. 8 if (!p) {
  1622. 9 spin_unlock(&amp;gp_lock);
  1623. 10 return false;
  1624. 11 }
  1625. 12 rcu_assign_pointer(gp, NULL);
  1626. 13 spin_unlock(&amp;gp_lock);
  1627. 14 s = get_state_synchronize_rcu();
  1628. 15 do_something_while_waiting();
  1629. 16 cond_synchronize_rcu(s);
  1630. 17 kfree(p);
  1631. 18 return true;
  1632. 19 }
  1633. </pre>
  1634. </blockquote>
  1635. <p>
  1636. On line&nbsp;14, <tt>get_state_synchronize_rcu()</tt> obtains a
  1637. &ldquo;cookie&rdquo; from RCU,
  1638. then line&nbsp;15 carries out other tasks,
  1639. and finally, line&nbsp;16 returns immediately if a grace period has
  1640. elapsed in the meantime, but otherwise waits as required.
  1641. The need for <tt>get_state_synchronize_rcu</tt> and
  1642. <tt>cond_synchronize_rcu()</tt> has appeared quite recently,
  1643. so it is too early to tell whether they will stand the test of time.
  1644. <p>
  1645. RCU thus provides a range of tools to allow updaters to strike the
  1646. required tradeoff between latency, flexibility and CPU overhead.
  1647. <h3><a name="Composability">Composability</a></h3>
  1648. <p>
  1649. Composability has received much attention in recent years, perhaps in part
  1650. due to the collision of multicore hardware with object-oriented techniques
  1651. designed in single-threaded environments for single-threaded use.
  1652. And in theory, RCU read-side critical sections may be composed, and in
  1653. fact may be nested arbitrarily deeply.
  1654. In practice, as with all real-world implementations of composable
  1655. constructs, there are limitations.
  1656. <p>
  1657. Implementations of RCU for which <tt>rcu_read_lock()</tt>
  1658. and <tt>rcu_read_unlock()</tt> generate no code, such as
  1659. Linux-kernel RCU when <tt>CONFIG_PREEMPT=n</tt>, can be
  1660. nested arbitrarily deeply.
  1661. After all, there is no overhead.
  1662. Except that if all these instances of <tt>rcu_read_lock()</tt>
  1663. and <tt>rcu_read_unlock()</tt> are visible to the compiler,
  1664. compilation will eventually fail due to exhausting memory,
  1665. mass storage, or user patience, whichever comes first.
  1666. If the nesting is not visible to the compiler, as is the case with
  1667. mutually recursive functions each in its own translation unit,
  1668. stack overflow will result.
  1669. If the nesting takes the form of loops, either the control variable
  1670. will overflow or (in the Linux kernel) you will get an RCU CPU stall warning.
  1671. Nevertheless, this class of RCU implementations is one
  1672. of the most composable constructs in existence.
  1673. <p>
  1674. RCU implementations that explicitly track nesting depth
  1675. are limited by the nesting-depth counter.
  1676. For example, the Linux kernel's preemptible RCU limits nesting to
  1677. <tt>INT_MAX</tt>.
  1678. This should suffice for almost all practical purposes.
  1679. That said, a consecutive pair of RCU read-side critical sections
  1680. between which there is an operation that waits for a grace period
  1681. cannot be enclosed in another RCU read-side critical section.
  1682. This is because it is not legal to wait for a grace period within
  1683. an RCU read-side critical section: To do so would result either
  1684. in deadlock or
  1685. in RCU implicitly splitting the enclosing RCU read-side critical
  1686. section, neither of which is conducive to a long-lived and prosperous
  1687. kernel.
  1688. <p>
  1689. It is worth noting that RCU is not alone in limiting composability.
  1690. For example, many transactional-memory implementations prohibit
  1691. composing a pair of transactions separated by an irrevocable
  1692. operation (for example, a network receive operation).
  1693. For another example, lock-based critical sections can be composed
  1694. surprisingly freely, but only if deadlock is avoided.
  1695. <p>
  1696. In short, although RCU read-side critical sections are highly composable,
  1697. care is required in some situations, just as is the case for any other
  1698. composable synchronization mechanism.
  1699. <h3><a name="Corner Cases">Corner Cases</a></h3>
  1700. <p>
  1701. A given RCU workload might have an endless and intense stream of
  1702. RCU read-side critical sections, perhaps even so intense that there
  1703. was never a point in time during which there was not at least one
  1704. RCU read-side critical section in flight.
  1705. RCU cannot allow this situation to block grace periods: As long as
  1706. all the RCU read-side critical sections are finite, grace periods
  1707. must also be finite.
  1708. <p>
  1709. That said, preemptible RCU implementations could potentially result
  1710. in RCU read-side critical sections being preempted for long durations,
  1711. which has the effect of creating a long-duration RCU read-side
  1712. critical section.
  1713. This situation can arise only in heavily loaded systems, but systems using
  1714. real-time priorities are of course more vulnerable.
  1715. Therefore, RCU priority boosting is provided to help deal with this
  1716. case.
  1717. That said, the exact requirements on RCU priority boosting will likely
  1718. evolve as more experience accumulates.
  1719. <p>
  1720. Other workloads might have very high update rates.
  1721. Although one can argue that such workloads should instead use
  1722. something other than RCU, the fact remains that RCU must
  1723. handle such workloads gracefully.
  1724. This requirement is another factor driving batching of grace periods,
  1725. but it is also the driving force behind the checks for large numbers
  1726. of queued RCU callbacks in the <tt>call_rcu()</tt> code path.
  1727. Finally, high update rates should not delay RCU read-side critical
  1728. sections, although some read-side delays can occur when using
  1729. <tt>synchronize_rcu_expedited()</tt>, courtesy of this function's use
  1730. of <tt>try_stop_cpus()</tt>.
  1731. (In the future, <tt>synchronize_rcu_expedited()</tt> will be
  1732. converted to use lighter-weight inter-processor interrupts (IPIs),
  1733. but this will still disturb readers, though to a much smaller degree.)
  1734. <p>
  1735. Although all three of these corner cases were understood in the early
  1736. 1990s, a simple user-level test consisting of <tt>close(open(path))</tt>
  1737. in a tight loop
  1738. in the early 2000s suddenly provided a much deeper appreciation of the
  1739. high-update-rate corner case.
  1740. This test also motivated addition of some RCU code to react to high update
  1741. rates, for example, if a given CPU finds itself with more than 10,000
  1742. RCU callbacks queued, it will cause RCU to take evasive action by
  1743. more aggressively starting grace periods and more aggressively forcing
  1744. completion of grace-period processing.
  1745. This evasive action causes the grace period to complete more quickly,
  1746. but at the cost of restricting RCU's batching optimizations, thus
  1747. increasing the CPU overhead incurred by that grace period.
  1748. <h2><a name="Software-Engineering Requirements">
  1749. Software-Engineering Requirements</a></h2>
  1750. <p>
  1751. Between Murphy's Law and &ldquo;To err is human&rdquo;, it is necessary to
  1752. guard against mishaps and misuse:
  1753. <ol>
  1754. <li> It is all too easy to forget to use <tt>rcu_read_lock()</tt>
  1755. everywhere that it is needed, so kernels built with
  1756. <tt>CONFIG_PROVE_RCU=y</tt> will spat if
  1757. <tt>rcu_dereference()</tt> is used outside of an
  1758. RCU read-side critical section.
  1759. Update-side code can use <tt>rcu_dereference_protected()</tt>,
  1760. which takes a
  1761. <a href="https://lwn.net/Articles/371986/">lockdep expression</a>
  1762. to indicate what is providing the protection.
  1763. If the indicated protection is not provided, a lockdep splat
  1764. is emitted.
  1765. <p>
  1766. Code shared between readers and updaters can use
  1767. <tt>rcu_dereference_check()</tt>, which also takes a
  1768. lockdep expression, and emits a lockdep splat if neither
  1769. <tt>rcu_read_lock()</tt> nor the indicated protection
  1770. is in place.
  1771. In addition, <tt>rcu_dereference_raw()</tt> is used in those
  1772. (hopefully rare) cases where the required protection cannot
  1773. be easily described.
  1774. Finally, <tt>rcu_read_lock_held()</tt> is provided to
  1775. allow a function to verify that it has been invoked within
  1776. an RCU read-side critical section.
  1777. I was made aware of this set of requirements shortly after Thomas
  1778. Gleixner audited a number of RCU uses.
  1779. <li> A given function might wish to check for RCU-related preconditions
  1780. upon entry, before using any other RCU API.
  1781. The <tt>rcu_lockdep_assert()</tt> does this job,
  1782. asserting the expression in kernels having lockdep enabled
  1783. and doing nothing otherwise.
  1784. <li> It is also easy to forget to use <tt>rcu_assign_pointer()</tt>
  1785. and <tt>rcu_dereference()</tt>, perhaps (incorrectly)
  1786. substituting a simple assignment.
  1787. To catch this sort of error, a given RCU-protected pointer may be
  1788. tagged with <tt>__rcu</tt>, after which running sparse
  1789. with <tt>CONFIG_SPARSE_RCU_POINTER=y</tt> will complain
  1790. about simple-assignment accesses to that pointer.
  1791. Arnd Bergmann made me aware of this requirement, and also
  1792. supplied the needed
  1793. <a href="https://lwn.net/Articles/376011/">patch series</a>.
  1794. <li> Kernels built with <tt>CONFIG_DEBUG_OBJECTS_RCU_HEAD=y</tt>
  1795. will splat if a data element is passed to <tt>call_rcu()</tt>
  1796. twice in a row, without a grace period in between.
  1797. (This error is similar to a double free.)
  1798. The corresponding <tt>rcu_head</tt> structures that are
  1799. dynamically allocated are automatically tracked, but
  1800. <tt>rcu_head</tt> structures allocated on the stack
  1801. must be initialized with <tt>init_rcu_head_on_stack()</tt>
  1802. and cleaned up with <tt>destroy_rcu_head_on_stack()</tt>.
  1803. Similarly, statically allocated non-stack <tt>rcu_head</tt>
  1804. structures must be initialized with <tt>init_rcu_head()</tt>
  1805. and cleaned up with <tt>destroy_rcu_head()</tt>.
  1806. Mathieu Desnoyers made me aware of this requirement, and also
  1807. supplied the needed
  1808. <a href="https://lkml.kernel.org/g/20100319013024.GA28456@Krystal">patch</a>.
  1809. <li> An infinite loop in an RCU read-side critical section will
  1810. eventually trigger an RCU CPU stall warning splat, with
  1811. the duration of &ldquo;eventually&rdquo; being controlled by the
  1812. <tt>RCU_CPU_STALL_TIMEOUT</tt> <tt>Kconfig</tt> option, or,
  1813. alternatively, by the
  1814. <tt>rcupdate.rcu_cpu_stall_timeout</tt> boot/sysfs
  1815. parameter.
  1816. However, RCU is not obligated to produce this splat
  1817. unless there is a grace period waiting on that particular
  1818. RCU read-side critical section.
  1819. <p>
  1820. Some extreme workloads might intentionally delay
  1821. RCU grace periods, and systems running those workloads can
  1822. be booted with <tt>rcupdate.rcu_cpu_stall_suppress</tt>
  1823. to suppress the splats.
  1824. This kernel parameter may also be set via <tt>sysfs</tt>.
  1825. Furthermore, RCU CPU stall warnings are counter-productive
  1826. during sysrq dumps and during panics.
  1827. RCU therefore supplies the <tt>rcu_sysrq_start()</tt> and
  1828. <tt>rcu_sysrq_end()</tt> API members to be called before
  1829. and after long sysrq dumps.
  1830. RCU also supplies the <tt>rcu_panic()</tt> notifier that is
  1831. automatically invoked at the beginning of a panic to suppress
  1832. further RCU CPU stall warnings.
  1833. <p>
  1834. This requirement made itself known in the early 1990s, pretty
  1835. much the first time that it was necessary to debug a CPU stall.
  1836. That said, the initial implementation in DYNIX/ptx was quite
  1837. generic in comparison with that of Linux.
  1838. <li> Although it would be very good to detect pointers leaking out
  1839. of RCU read-side critical sections, there is currently no
  1840. good way of doing this.
  1841. One complication is the need to distinguish between pointers
  1842. leaking and pointers that have been handed off from RCU to
  1843. some other synchronization mechanism, for example, reference
  1844. counting.
  1845. <li> In kernels built with <tt>CONFIG_RCU_TRACE=y</tt>, RCU-related
  1846. information is provided via both debugfs and event tracing.
  1847. <li> Open-coded use of <tt>rcu_assign_pointer()</tt> and
  1848. <tt>rcu_dereference()</tt> to create typical linked
  1849. data structures can be surprisingly error-prone.
  1850. Therefore, RCU-protected
  1851. <a href="https://lwn.net/Articles/609973/#RCU List APIs">linked lists</a>
  1852. and, more recently, RCU-protected
  1853. <a href="https://lwn.net/Articles/612100/">hash tables</a>
  1854. are available.
  1855. Many other special-purpose RCU-protected data structures are
  1856. available in the Linux kernel and the userspace RCU library.
  1857. <li> Some linked structures are created at compile time, but still
  1858. require <tt>__rcu</tt> checking.
  1859. The <tt>RCU_POINTER_INITIALIZER()</tt> macro serves this
  1860. purpose.
  1861. <li> It is not necessary to use <tt>rcu_assign_pointer()</tt>
  1862. when creating linked structures that are to be published via
  1863. a single external pointer.
  1864. The <tt>RCU_INIT_POINTER()</tt> macro is provided for
  1865. this task and also for assigning <tt>NULL</tt> pointers
  1866. at runtime.
  1867. </ol>
  1868. <p>
  1869. This not a hard-and-fast list: RCU's diagnostic capabilities will
  1870. continue to be guided by the number and type of usage bugs found
  1871. in real-world RCU usage.
  1872. <h2><a name="Linux Kernel Complications">Linux Kernel Complications</a></h2>
  1873. <p>
  1874. The Linux kernel provides an interesting environment for all kinds of
  1875. software, including RCU.
  1876. Some of the relevant points of interest are as follows:
  1877. <ol>
  1878. <li> <a href="#Configuration">Configuration</a>.
  1879. <li> <a href="#Firmware Interface">Firmware Interface</a>.
  1880. <li> <a href="#Early Boot">Early Boot</a>.
  1881. <li> <a href="#Interrupts and NMIs">
  1882. Interrupts and non-maskable interrupts (NMIs)</a>.
  1883. <li> <a href="#Loadable Modules">Loadable Modules</a>.
  1884. <li> <a href="#Hotplug CPU">Hotplug CPU</a>.
  1885. <li> <a href="#Scheduler and RCU">Scheduler and RCU</a>.
  1886. <li> <a href="#Tracing and RCU">Tracing and RCU</a>.
  1887. <li> <a href="#Energy Efficiency">Energy Efficiency</a>.
  1888. <li> <a href="#Memory Efficiency">Memory Efficiency</a>.
  1889. <li> <a href="#Performance, Scalability, Response Time, and Reliability">
  1890. Performance, Scalability, Response Time, and Reliability</a>.
  1891. </ol>
  1892. <p>
  1893. This list is probably incomplete, but it does give a feel for the
  1894. most notable Linux-kernel complications.
  1895. Each of the following sections covers one of the above topics.
  1896. <h3><a name="Configuration">Configuration</a></h3>
  1897. <p>
  1898. RCU's goal is automatic configuration, so that almost nobody
  1899. needs to worry about RCU's <tt>Kconfig</tt> options.
  1900. And for almost all users, RCU does in fact work well
  1901. &ldquo;out of the box.&rdquo;
  1902. <p>
  1903. However, there are specialized use cases that are handled by
  1904. kernel boot parameters and <tt>Kconfig</tt> options.
  1905. Unfortunately, the <tt>Kconfig</tt> system will explicitly ask users
  1906. about new <tt>Kconfig</tt> options, which requires almost all of them
  1907. be hidden behind a <tt>CONFIG_RCU_EXPERT</tt> <tt>Kconfig</tt> option.
  1908. <p>
  1909. This all should be quite obvious, but the fact remains that
  1910. Linus Torvalds recently had to
  1911. <a href="https://lkml.kernel.org/g/CA+55aFy4wcCwaL4okTs8wXhGZ5h-ibecy_Meg9C4MNQrUnwMcg@mail.gmail.com">remind</a>
  1912. me of this requirement.
  1913. <h3><a name="Firmware Interface">Firmware Interface</a></h3>
  1914. <p>
  1915. In many cases, kernel obtains information about the system from the
  1916. firmware, and sometimes things are lost in translation.
  1917. Or the translation is accurate, but the original message is bogus.
  1918. <p>
  1919. For example, some systems' firmware overreports the number of CPUs,
  1920. sometimes by a large factor.
  1921. If RCU naively believed the firmware, as it used to do,
  1922. it would create too many per-CPU kthreads.
  1923. Although the resulting system will still run correctly, the extra
  1924. kthreads needlessly consume memory and can cause confusion
  1925. when they show up in <tt>ps</tt> listings.
  1926. <p>
  1927. RCU must therefore wait for a given CPU to actually come online before
  1928. it can allow itself to believe that the CPU actually exists.
  1929. The resulting &ldquo;ghost CPUs&rdquo; (which are never going to
  1930. come online) cause a number of
  1931. <a href="https://paulmck.livejournal.com/37494.html">interesting complications</a>.
  1932. <h3><a name="Early Boot">Early Boot</a></h3>
  1933. <p>
  1934. The Linux kernel's boot sequence is an interesting process,
  1935. and RCU is used early, even before <tt>rcu_init()</tt>
  1936. is invoked.
  1937. In fact, a number of RCU's primitives can be used as soon as the
  1938. initial task's <tt>task_struct</tt> is available and the
  1939. boot CPU's per-CPU variables are set up.
  1940. The read-side primitives (<tt>rcu_read_lock()</tt>,
  1941. <tt>rcu_read_unlock()</tt>, <tt>rcu_dereference()</tt>,
  1942. and <tt>rcu_access_pointer()</tt>) will operate normally very early on,
  1943. as will <tt>rcu_assign_pointer()</tt>.
  1944. <p>
  1945. Although <tt>call_rcu()</tt> may be invoked at any
  1946. time during boot, callbacks are not guaranteed to be invoked until after
  1947. the scheduler is fully up and running.
  1948. This delay in callback invocation is due to the fact that RCU does not
  1949. invoke callbacks until it is fully initialized, and this full initialization
  1950. cannot occur until after the scheduler has initialized itself to the
  1951. point where RCU can spawn and run its kthreads.
  1952. In theory, it would be possible to invoke callbacks earlier,
  1953. however, this is not a panacea because there would be severe restrictions
  1954. on what operations those callbacks could invoke.
  1955. <p>
  1956. Perhaps surprisingly, <tt>synchronize_rcu()</tt>,
  1957. <a href="#Bottom-Half Flavor"><tt>synchronize_rcu_bh()</tt></a>
  1958. (<a href="#Bottom-Half Flavor">discussed below</a>),
  1959. and
  1960. <a href="#Sched Flavor"><tt>synchronize_sched()</tt></a>
  1961. will all operate normally
  1962. during very early boot, the reason being that there is only one CPU
  1963. and preemption is disabled.
  1964. This means that the call <tt>synchronize_rcu()</tt> (or friends)
  1965. itself is a quiescent
  1966. state and thus a grace period, so the early-boot implementation can
  1967. be a no-op.
  1968. <p>
  1969. Both <tt>synchronize_rcu_bh()</tt> and <tt>synchronize_sched()</tt>
  1970. continue to operate normally through the remainder of boot, courtesy
  1971. of the fact that preemption is disabled across their RCU read-side
  1972. critical sections and also courtesy of the fact that there is still
  1973. only one CPU.
  1974. However, once the scheduler starts initializing, preemption is enabled.
  1975. There is still only a single CPU, but the fact that preemption is enabled
  1976. means that the no-op implementation of <tt>synchronize_rcu()</tt> no
  1977. longer works in <tt>CONFIG_PREEMPT=y</tt> kernels.
  1978. Therefore, as soon as the scheduler starts initializing, the early-boot
  1979. fastpath is disabled.
  1980. This means that <tt>synchronize_rcu()</tt> switches to its runtime
  1981. mode of operation where it posts callbacks, which in turn means that
  1982. any call to <tt>synchronize_rcu()</tt> will block until the corresponding
  1983. callback is invoked.
  1984. Unfortunately, the callback cannot be invoked until RCU's runtime
  1985. grace-period machinery is up and running, which cannot happen until
  1986. the scheduler has initialized itself sufficiently to allow RCU's
  1987. kthreads to be spawned.
  1988. Therefore, invoking <tt>synchronize_rcu()</tt> during scheduler
  1989. initialization can result in deadlock.
  1990. <table>
  1991. <tr><th>&nbsp;</th></tr>
  1992. <tr><th align="left">Quick Quiz:</th></tr>
  1993. <tr><td>
  1994. So what happens with <tt>synchronize_rcu()</tt> during
  1995. scheduler initialization for <tt>CONFIG_PREEMPT=n</tt>
  1996. kernels?
  1997. </td></tr>
  1998. <tr><th align="left">Answer:</th></tr>
  1999. <tr><td bgcolor="#ffffff"><font color="ffffff">
  2000. In <tt>CONFIG_PREEMPT=n</tt> kernel, <tt>synchronize_rcu()</tt>
  2001. maps directly to <tt>synchronize_sched()</tt>.
  2002. Therefore, <tt>synchronize_rcu()</tt> works normally throughout
  2003. boot in <tt>CONFIG_PREEMPT=n</tt> kernels.
  2004. However, your code must also work in <tt>CONFIG_PREEMPT=y</tt> kernels,
  2005. so it is still necessary to avoid invoking <tt>synchronize_rcu()</tt>
  2006. during scheduler initialization.
  2007. </font></td></tr>
  2008. <tr><td>&nbsp;</td></tr>
  2009. </table>
  2010. <p>
  2011. I learned of these boot-time requirements as a result of a series of
  2012. system hangs.
  2013. <h3><a name="Interrupts and NMIs">Interrupts and NMIs</a></h3>
  2014. <p>
  2015. The Linux kernel has interrupts, and RCU read-side critical sections are
  2016. legal within interrupt handlers and within interrupt-disabled regions
  2017. of code, as are invocations of <tt>call_rcu()</tt>.
  2018. <p>
  2019. Some Linux-kernel architectures can enter an interrupt handler from
  2020. non-idle process context, and then just never leave it, instead stealthily
  2021. transitioning back to process context.
  2022. This trick is sometimes used to invoke system calls from inside the kernel.
  2023. These &ldquo;half-interrupts&rdquo; mean that RCU has to be very careful
  2024. about how it counts interrupt nesting levels.
  2025. I learned of this requirement the hard way during a rewrite
  2026. of RCU's dyntick-idle code.
  2027. <p>
  2028. The Linux kernel has non-maskable interrupts (NMIs), and
  2029. RCU read-side critical sections are legal within NMI handlers.
  2030. Thankfully, RCU update-side primitives, including
  2031. <tt>call_rcu()</tt>, are prohibited within NMI handlers.
  2032. <p>
  2033. The name notwithstanding, some Linux-kernel architectures
  2034. can have nested NMIs, which RCU must handle correctly.
  2035. Andy Lutomirski
  2036. <a href="https://lkml.kernel.org/g/CALCETrXLq1y7e_dKFPgou-FKHB6Pu-r8+t-6Ds+8=va7anBWDA@mail.gmail.com">surprised me</a>
  2037. with this requirement;
  2038. he also kindly surprised me with
  2039. <a href="https://lkml.kernel.org/g/CALCETrXSY9JpW3uE6H8WYk81sg56qasA2aqmjMPsq5dOtzso=g@mail.gmail.com">an algorithm</a>
  2040. that meets this requirement.
  2041. <h3><a name="Loadable Modules">Loadable Modules</a></h3>
  2042. <p>
  2043. The Linux kernel has loadable modules, and these modules can
  2044. also be unloaded.
  2045. After a given module has been unloaded, any attempt to call
  2046. one of its functions results in a segmentation fault.
  2047. The module-unload functions must therefore cancel any
  2048. delayed calls to loadable-module functions, for example,
  2049. any outstanding <tt>mod_timer()</tt> must be dealt with
  2050. via <tt>del_timer_sync()</tt> or similar.
  2051. <p>
  2052. Unfortunately, there is no way to cancel an RCU callback;
  2053. once you invoke <tt>call_rcu()</tt>, the callback function is
  2054. going to eventually be invoked, unless the system goes down first.
  2055. Because it is normally considered socially irresponsible to crash the system
  2056. in response to a module unload request, we need some other way
  2057. to deal with in-flight RCU callbacks.
  2058. <p>
  2059. RCU therefore provides
  2060. <tt><a href="https://lwn.net/Articles/217484/">rcu_barrier()</a></tt>,
  2061. which waits until all in-flight RCU callbacks have been invoked.
  2062. If a module uses <tt>call_rcu()</tt>, its exit function should therefore
  2063. prevent any future invocation of <tt>call_rcu()</tt>, then invoke
  2064. <tt>rcu_barrier()</tt>.
  2065. In theory, the underlying module-unload code could invoke
  2066. <tt>rcu_barrier()</tt> unconditionally, but in practice this would
  2067. incur unacceptable latencies.
  2068. <p>
  2069. Nikita Danilov noted this requirement for an analogous filesystem-unmount
  2070. situation, and Dipankar Sarma incorporated <tt>rcu_barrier()</tt> into RCU.
  2071. The need for <tt>rcu_barrier()</tt> for module unloading became
  2072. apparent later.
  2073. <h3><a name="Hotplug CPU">Hotplug CPU</a></h3>
  2074. <p>
  2075. The Linux kernel supports CPU hotplug, which means that CPUs
  2076. can come and go.
  2077. It is of course illegal to use any RCU API member from an offline CPU.
  2078. This requirement was present from day one in DYNIX/ptx, but
  2079. on the other hand, the Linux kernel's CPU-hotplug implementation
  2080. is &ldquo;interesting.&rdquo;
  2081. <p>
  2082. The Linux-kernel CPU-hotplug implementation has notifiers that
  2083. are used to allow the various kernel subsystems (including RCU)
  2084. to respond appropriately to a given CPU-hotplug operation.
  2085. Most RCU operations may be invoked from CPU-hotplug notifiers,
  2086. including even normal synchronous grace-period operations
  2087. such as <tt>synchronize_rcu()</tt>.
  2088. However, expedited grace-period operations such as
  2089. <tt>synchronize_rcu_expedited()</tt> are not supported,
  2090. due to the fact that current implementations block CPU-hotplug
  2091. operations, which could result in deadlock.
  2092. <p>
  2093. In addition, all-callback-wait operations such as
  2094. <tt>rcu_barrier()</tt> are also not supported, due to the
  2095. fact that there are phases of CPU-hotplug operations where
  2096. the outgoing CPU's callbacks will not be invoked until after
  2097. the CPU-hotplug operation ends, which could also result in deadlock.
  2098. <h3><a name="Scheduler and RCU">Scheduler and RCU</a></h3>
  2099. <p>
  2100. RCU depends on the scheduler, and the scheduler uses RCU to
  2101. protect some of its data structures.
  2102. This means the scheduler is forbidden from acquiring
  2103. the runqueue locks and the priority-inheritance locks
  2104. in the middle of an outermost RCU read-side critical section unless either
  2105. (1)&nbsp;it releases them before exiting that same
  2106. RCU read-side critical section, or
  2107. (2)&nbsp;interrupts are disabled across
  2108. that entire RCU read-side critical section.
  2109. This same prohibition also applies (recursively!) to any lock that is acquired
  2110. while holding any lock to which this prohibition applies.
  2111. Adhering to this rule prevents preemptible RCU from invoking
  2112. <tt>rcu_read_unlock_special()</tt> while either runqueue or
  2113. priority-inheritance locks are held, thus avoiding deadlock.
  2114. <p>
  2115. Prior to v4.4, it was only necessary to disable preemption across
  2116. RCU read-side critical sections that acquired scheduler locks.
  2117. In v4.4, expedited grace periods started using IPIs, and these
  2118. IPIs could force a <tt>rcu_read_unlock()</tt> to take the slowpath.
  2119. Therefore, this expedited-grace-period change required disabling of
  2120. interrupts, not just preemption.
  2121. <p>
  2122. For RCU's part, the preemptible-RCU <tt>rcu_read_unlock()</tt>
  2123. implementation must be written carefully to avoid similar deadlocks.
  2124. In particular, <tt>rcu_read_unlock()</tt> must tolerate an
  2125. interrupt where the interrupt handler invokes both
  2126. <tt>rcu_read_lock()</tt> and <tt>rcu_read_unlock()</tt>.
  2127. This possibility requires <tt>rcu_read_unlock()</tt> to use
  2128. negative nesting levels to avoid destructive recursion via
  2129. interrupt handler's use of RCU.
  2130. <p>
  2131. This pair of mutual scheduler-RCU requirements came as a
  2132. <a href="https://lwn.net/Articles/453002/">complete surprise</a>.
  2133. <p>
  2134. As noted above, RCU makes use of kthreads, and it is necessary to
  2135. avoid excessive CPU-time accumulation by these kthreads.
  2136. This requirement was no surprise, but RCU's violation of it
  2137. when running context-switch-heavy workloads when built with
  2138. <tt>CONFIG_NO_HZ_FULL=y</tt>
  2139. <a href="http://www.rdrop.com/users/paulmck/scalability/paper/BareMetal.2015.01.15b.pdf">did come as a surprise [PDF]</a>.
  2140. RCU has made good progress towards meeting this requirement, even
  2141. for context-switch-have <tt>CONFIG_NO_HZ_FULL=y</tt> workloads,
  2142. but there is room for further improvement.
  2143. <h3><a name="Tracing and RCU">Tracing and RCU</a></h3>
  2144. <p>
  2145. It is possible to use tracing on RCU code, but tracing itself
  2146. uses RCU.
  2147. For this reason, <tt>rcu_dereference_raw_notrace()</tt>
  2148. is provided for use by tracing, which avoids the destructive
  2149. recursion that could otherwise ensue.
  2150. This API is also used by virtualization in some architectures,
  2151. where RCU readers execute in environments in which tracing
  2152. cannot be used.
  2153. The tracing folks both located the requirement and provided the
  2154. needed fix, so this surprise requirement was relatively painless.
  2155. <h3><a name="Energy Efficiency">Energy Efficiency</a></h3>
  2156. <p>
  2157. Interrupting idle CPUs is considered socially unacceptable,
  2158. especially by people with battery-powered embedded systems.
  2159. RCU therefore conserves energy by detecting which CPUs are
  2160. idle, including tracking CPUs that have been interrupted from idle.
  2161. This is a large part of the energy-efficiency requirement,
  2162. so I learned of this via an irate phone call.
  2163. <p>
  2164. Because RCU avoids interrupting idle CPUs, it is illegal to
  2165. execute an RCU read-side critical section on an idle CPU.
  2166. (Kernels built with <tt>CONFIG_PROVE_RCU=y</tt> will splat
  2167. if you try it.)
  2168. The <tt>RCU_NONIDLE()</tt> macro and <tt>_rcuidle</tt>
  2169. event tracing is provided to work around this restriction.
  2170. In addition, <tt>rcu_is_watching()</tt> may be used to
  2171. test whether or not it is currently legal to run RCU read-side
  2172. critical sections on this CPU.
  2173. I learned of the need for diagnostics on the one hand
  2174. and <tt>RCU_NONIDLE()</tt> on the other while inspecting
  2175. idle-loop code.
  2176. Steven Rostedt supplied <tt>_rcuidle</tt> event tracing,
  2177. which is used quite heavily in the idle loop.
  2178. However, there are some restrictions on the code placed within
  2179. <tt>RCU_NONIDLE()</tt>:
  2180. <ol>
  2181. <li> Blocking is prohibited.
  2182. In practice, this is not a serious restriction given that idle
  2183. tasks are prohibited from blocking to begin with.
  2184. <li> Although nesting <tt>RCU_NONIDLE()</tt> is permited, they cannot
  2185. nest indefinitely deeply.
  2186. However, given that they can be nested on the order of a million
  2187. deep, even on 32-bit systems, this should not be a serious
  2188. restriction.
  2189. This nesting limit would probably be reached long after the
  2190. compiler OOMed or the stack overflowed.
  2191. <li> Any code path that enters <tt>RCU_NONIDLE()</tt> must sequence
  2192. out of that same <tt>RCU_NONIDLE()</tt>.
  2193. For example, the following is grossly illegal:
  2194. <blockquote>
  2195. <pre>
  2196. 1 RCU_NONIDLE({
  2197. 2 do_something();
  2198. 3 goto bad_idea; /* BUG!!! */
  2199. 4 do_something_else();});
  2200. 5 bad_idea:
  2201. </pre>
  2202. </blockquote>
  2203. <p>
  2204. It is just as illegal to transfer control into the middle of
  2205. <tt>RCU_NONIDLE()</tt>'s argument.
  2206. Yes, in theory, you could transfer in as long as you also
  2207. transferred out, but in practice you could also expect to get sharply
  2208. worded review comments.
  2209. </ol>
  2210. <p>
  2211. It is similarly socially unacceptable to interrupt an
  2212. <tt>nohz_full</tt> CPU running in userspace.
  2213. RCU must therefore track <tt>nohz_full</tt> userspace
  2214. execution.
  2215. And in
  2216. <a href="https://lwn.net/Articles/558284/"><tt>CONFIG_NO_HZ_FULL_SYSIDLE=y</tt></a>
  2217. kernels, RCU must separately track idle CPUs on the one hand and
  2218. CPUs that are either idle or executing in userspace on the other.
  2219. In both cases, RCU must be able to sample state at two points in
  2220. time, and be able to determine whether or not some other CPU spent
  2221. any time idle and/or executing in userspace.
  2222. <p>
  2223. These energy-efficiency requirements have proven quite difficult to
  2224. understand and to meet, for example, there have been more than five
  2225. clean-sheet rewrites of RCU's energy-efficiency code, the last of
  2226. which was finally able to demonstrate
  2227. <a href="http://www.rdrop.com/users/paulmck/realtime/paper/AMPenergy.2013.04.19a.pdf">real energy savings running on real hardware [PDF]</a>.
  2228. As noted earlier,
  2229. I learned of many of these requirements via angry phone calls:
  2230. Flaming me on the Linux-kernel mailing list was apparently not
  2231. sufficient to fully vent their ire at RCU's energy-efficiency bugs!
  2232. <h3><a name="Memory Efficiency">Memory Efficiency</a></h3>
  2233. <p>
  2234. Although small-memory non-realtime systems can simply use Tiny RCU,
  2235. code size is only one aspect of memory efficiency.
  2236. Another aspect is the size of the <tt>rcu_head</tt> structure
  2237. used by <tt>call_rcu()</tt> and <tt>kfree_rcu()</tt>.
  2238. Although this structure contains nothing more than a pair of pointers,
  2239. it does appear in many RCU-protected data structures, including
  2240. some that are size critical.
  2241. The <tt>page</tt> structure is a case in point, as evidenced by
  2242. the many occurrences of the <tt>union</tt> keyword within that structure.
  2243. <p>
  2244. This need for memory efficiency is one reason that RCU uses hand-crafted
  2245. singly linked lists to track the <tt>rcu_head</tt> structures that
  2246. are waiting for a grace period to elapse.
  2247. It is also the reason why <tt>rcu_head</tt> structures do not contain
  2248. debug information, such as fields tracking the file and line of the
  2249. <tt>call_rcu()</tt> or <tt>kfree_rcu()</tt> that posted them.
  2250. Although this information might appear in debug-only kernel builds at some
  2251. point, in the meantime, the <tt>-&gt;func</tt> field will often provide
  2252. the needed debug information.
  2253. <p>
  2254. However, in some cases, the need for memory efficiency leads to even
  2255. more extreme measures.
  2256. Returning to the <tt>page</tt> structure, the <tt>rcu_head</tt> field
  2257. shares storage with a great many other structures that are used at
  2258. various points in the corresponding page's lifetime.
  2259. In order to correctly resolve certain
  2260. <a href="https://lkml.kernel.org/g/1439976106-137226-1-git-send-email-kirill.shutemov@linux.intel.com">race conditions</a>,
  2261. the Linux kernel's memory-management subsystem needs a particular bit
  2262. to remain zero during all phases of grace-period processing,
  2263. and that bit happens to map to the bottom bit of the
  2264. <tt>rcu_head</tt> structure's <tt>-&gt;next</tt> field.
  2265. RCU makes this guarantee as long as <tt>call_rcu()</tt>
  2266. is used to post the callback, as opposed to <tt>kfree_rcu()</tt>
  2267. or some future &ldquo;lazy&rdquo;
  2268. variant of <tt>call_rcu()</tt> that might one day be created for
  2269. energy-efficiency purposes.
  2270. <p>
  2271. That said, there are limits.
  2272. RCU requires that the <tt>rcu_head</tt> structure be aligned to a
  2273. two-byte boundary, and passing a misaligned <tt>rcu_head</tt>
  2274. structure to one of the <tt>call_rcu()</tt> family of functions
  2275. will result in a splat.
  2276. It is therefore necessary to exercise caution when packing
  2277. structures containing fields of type <tt>rcu_head</tt>.
  2278. Why not a four-byte or even eight-byte alignment requirement?
  2279. Because the m68k architecture provides only two-byte alignment,
  2280. and thus acts as alignment's least common denominator.
  2281. <p>
  2282. The reason for reserving the bottom bit of pointers to
  2283. <tt>rcu_head</tt> structures is to leave the door open to
  2284. &ldquo;lazy&rdquo; callbacks whose invocations can safely be deferred.
  2285. Deferring invocation could potentially have energy-efficiency
  2286. benefits, but only if the rate of non-lazy callbacks decreases
  2287. significantly for some important workload.
  2288. In the meantime, reserving the bottom bit keeps this option open
  2289. in case it one day becomes useful.
  2290. <h3><a name="Performance, Scalability, Response Time, and Reliability">
  2291. Performance, Scalability, Response Time, and Reliability</a></h3>
  2292. <p>
  2293. Expanding on the
  2294. <a href="#Performance and Scalability">earlier discussion</a>,
  2295. RCU is used heavily by hot code paths in performance-critical
  2296. portions of the Linux kernel's networking, security, virtualization,
  2297. and scheduling code paths.
  2298. RCU must therefore use efficient implementations, especially in its
  2299. read-side primitives.
  2300. To that end, it would be good if preemptible RCU's implementation
  2301. of <tt>rcu_read_lock()</tt> could be inlined, however, doing
  2302. this requires resolving <tt>#include</tt> issues with the
  2303. <tt>task_struct</tt> structure.
  2304. <p>
  2305. The Linux kernel supports hardware configurations with up to
  2306. 4096 CPUs, which means that RCU must be extremely scalable.
  2307. Algorithms that involve frequent acquisitions of global locks or
  2308. frequent atomic operations on global variables simply cannot be
  2309. tolerated within the RCU implementation.
  2310. RCU therefore makes heavy use of a combining tree based on the
  2311. <tt>rcu_node</tt> structure.
  2312. RCU is required to tolerate all CPUs continuously invoking any
  2313. combination of RCU's runtime primitives with minimal per-operation
  2314. overhead.
  2315. In fact, in many cases, increasing load must <i>decrease</i> the
  2316. per-operation overhead, witness the batching optimizations for
  2317. <tt>synchronize_rcu()</tt>, <tt>call_rcu()</tt>,
  2318. <tt>synchronize_rcu_expedited()</tt>, and <tt>rcu_barrier()</tt>.
  2319. As a general rule, RCU must cheerfully accept whatever the
  2320. rest of the Linux kernel decides to throw at it.
  2321. <p>
  2322. The Linux kernel is used for real-time workloads, especially
  2323. in conjunction with the
  2324. <a href="https://rt.wiki.kernel.org/index.php/Main_Page">-rt patchset</a>.
  2325. The real-time-latency response requirements are such that the
  2326. traditional approach of disabling preemption across RCU
  2327. read-side critical sections is inappropriate.
  2328. Kernels built with <tt>CONFIG_PREEMPT=y</tt> therefore
  2329. use an RCU implementation that allows RCU read-side critical
  2330. sections to be preempted.
  2331. This requirement made its presence known after users made it
  2332. clear that an earlier
  2333. <a href="https://lwn.net/Articles/107930/">real-time patch</a>
  2334. did not meet their needs, in conjunction with some
  2335. <a href="https://lkml.kernel.org/g/20050318002026.GA2693@us.ibm.com">RCU issues</a>
  2336. encountered by a very early version of the -rt patchset.
  2337. <p>
  2338. In addition, RCU must make do with a sub-100-microsecond real-time latency
  2339. budget.
  2340. In fact, on smaller systems with the -rt patchset, the Linux kernel
  2341. provides sub-20-microsecond real-time latencies for the whole kernel,
  2342. including RCU.
  2343. RCU's scalability and latency must therefore be sufficient for
  2344. these sorts of configurations.
  2345. To my surprise, the sub-100-microsecond real-time latency budget
  2346. <a href="http://www.rdrop.com/users/paulmck/realtime/paper/bigrt.2013.01.31a.LCA.pdf">
  2347. applies to even the largest systems [PDF]</a>,
  2348. up to and including systems with 4096 CPUs.
  2349. This real-time requirement motivated the grace-period kthread, which
  2350. also simplified handling of a number of race conditions.
  2351. <p>
  2352. RCU must avoid degrading real-time response for CPU-bound threads, whether
  2353. executing in usermode (which is one use case for
  2354. <tt>CONFIG_NO_HZ_FULL=y</tt>) or in the kernel.
  2355. That said, CPU-bound loops in the kernel must execute
  2356. <tt>cond_resched_rcu_qs()</tt> at least once per few tens of milliseconds
  2357. in order to avoid receiving an IPI from RCU.
  2358. <p>
  2359. Finally, RCU's status as a synchronization primitive means that
  2360. any RCU failure can result in arbitrary memory corruption that can be
  2361. extremely difficult to debug.
  2362. This means that RCU must be extremely reliable, which in
  2363. practice also means that RCU must have an aggressive stress-test
  2364. suite.
  2365. This stress-test suite is called <tt>rcutorture</tt>.
  2366. <p>
  2367. Although the need for <tt>rcutorture</tt> was no surprise,
  2368. the current immense popularity of the Linux kernel is posing
  2369. interesting&mdash;and perhaps unprecedented&mdash;validation
  2370. challenges.
  2371. To see this, keep in mind that there are well over one billion
  2372. instances of the Linux kernel running today, given Android
  2373. smartphones, Linux-powered televisions, and servers.
  2374. This number can be expected to increase sharply with the advent of
  2375. the celebrated Internet of Things.
  2376. <p>
  2377. Suppose that RCU contains a race condition that manifests on average
  2378. once per million years of runtime.
  2379. This bug will be occurring about three times per <i>day</i> across
  2380. the installed base.
  2381. RCU could simply hide behind hardware error rates, given that no one
  2382. should really expect their smartphone to last for a million years.
  2383. However, anyone taking too much comfort from this thought should
  2384. consider the fact that in most jurisdictions, a successful multi-year
  2385. test of a given mechanism, which might include a Linux kernel,
  2386. suffices for a number of types of safety-critical certifications.
  2387. In fact, rumor has it that the Linux kernel is already being used
  2388. in production for safety-critical applications.
  2389. I don't know about you, but I would feel quite bad if a bug in RCU
  2390. killed someone.
  2391. Which might explain my recent focus on validation and verification.
  2392. <h2><a name="Other RCU Flavors">Other RCU Flavors</a></h2>
  2393. <p>
  2394. One of the more surprising things about RCU is that there are now
  2395. no fewer than five <i>flavors</i>, or API families.
  2396. In addition, the primary flavor that has been the sole focus up to
  2397. this point has two different implementations, non-preemptible and
  2398. preemptible.
  2399. The other four flavors are listed below, with requirements for each
  2400. described in a separate section.
  2401. <ol>
  2402. <li> <a href="#Bottom-Half Flavor">Bottom-Half Flavor</a>
  2403. <li> <a href="#Sched Flavor">Sched Flavor</a>
  2404. <li> <a href="#Sleepable RCU">Sleepable RCU</a>
  2405. <li> <a href="#Tasks RCU">Tasks RCU</a>
  2406. <li> <a href="#Waiting for Multiple Grace Periods">
  2407. Waiting for Multiple Grace Periods</a>
  2408. </ol>
  2409. <h3><a name="Bottom-Half Flavor">Bottom-Half Flavor</a></h3>
  2410. <p>
  2411. The softirq-disable (AKA &ldquo;bottom-half&rdquo;,
  2412. hence the &ldquo;_bh&rdquo; abbreviations)
  2413. flavor of RCU, or <i>RCU-bh</i>, was developed by
  2414. Dipankar Sarma to provide a flavor of RCU that could withstand the
  2415. network-based denial-of-service attacks researched by Robert
  2416. Olsson.
  2417. These attacks placed so much networking load on the system
  2418. that some of the CPUs never exited softirq execution,
  2419. which in turn prevented those CPUs from ever executing a context switch,
  2420. which, in the RCU implementation of that time, prevented grace periods
  2421. from ever ending.
  2422. The result was an out-of-memory condition and a system hang.
  2423. <p>
  2424. The solution was the creation of RCU-bh, which does
  2425. <tt>local_bh_disable()</tt>
  2426. across its read-side critical sections, and which uses the transition
  2427. from one type of softirq processing to another as a quiescent state
  2428. in addition to context switch, idle, user mode, and offline.
  2429. This means that RCU-bh grace periods can complete even when some of
  2430. the CPUs execute in softirq indefinitely, thus allowing algorithms
  2431. based on RCU-bh to withstand network-based denial-of-service attacks.
  2432. <p>
  2433. Because
  2434. <tt>rcu_read_lock_bh()</tt> and <tt>rcu_read_unlock_bh()</tt>
  2435. disable and re-enable softirq handlers, any attempt to start a softirq
  2436. handlers during the
  2437. RCU-bh read-side critical section will be deferred.
  2438. In this case, <tt>rcu_read_unlock_bh()</tt>
  2439. will invoke softirq processing, which can take considerable time.
  2440. One can of course argue that this softirq overhead should be associated
  2441. with the code following the RCU-bh read-side critical section rather
  2442. than <tt>rcu_read_unlock_bh()</tt>, but the fact
  2443. is that most profiling tools cannot be expected to make this sort
  2444. of fine distinction.
  2445. For example, suppose that a three-millisecond-long RCU-bh read-side
  2446. critical section executes during a time of heavy networking load.
  2447. There will very likely be an attempt to invoke at least one softirq
  2448. handler during that three milliseconds, but any such invocation will
  2449. be delayed until the time of the <tt>rcu_read_unlock_bh()</tt>.
  2450. This can of course make it appear at first glance as if
  2451. <tt>rcu_read_unlock_bh()</tt> was executing very slowly.
  2452. <p>
  2453. The
  2454. <a href="https://lwn.net/Articles/609973/#RCU Per-Flavor API Table">RCU-bh API</a>
  2455. includes
  2456. <tt>rcu_read_lock_bh()</tt>,
  2457. <tt>rcu_read_unlock_bh()</tt>,
  2458. <tt>rcu_dereference_bh()</tt>,
  2459. <tt>rcu_dereference_bh_check()</tt>,
  2460. <tt>synchronize_rcu_bh()</tt>,
  2461. <tt>synchronize_rcu_bh_expedited()</tt>,
  2462. <tt>call_rcu_bh()</tt>,
  2463. <tt>rcu_barrier_bh()</tt>, and
  2464. <tt>rcu_read_lock_bh_held()</tt>.
  2465. <h3><a name="Sched Flavor">Sched Flavor</a></h3>
  2466. <p>
  2467. Before preemptible RCU, waiting for an RCU grace period had the
  2468. side effect of also waiting for all pre-existing interrupt
  2469. and NMI handlers.
  2470. However, there are legitimate preemptible-RCU implementations that
  2471. do not have this property, given that any point in the code outside
  2472. of an RCU read-side critical section can be a quiescent state.
  2473. Therefore, <i>RCU-sched</i> was created, which follows &ldquo;classic&rdquo;
  2474. RCU in that an RCU-sched grace period waits for for pre-existing
  2475. interrupt and NMI handlers.
  2476. In kernels built with <tt>CONFIG_PREEMPT=n</tt>, the RCU and RCU-sched
  2477. APIs have identical implementations, while kernels built with
  2478. <tt>CONFIG_PREEMPT=y</tt> provide a separate implementation for each.
  2479. <p>
  2480. Note well that in <tt>CONFIG_PREEMPT=y</tt> kernels,
  2481. <tt>rcu_read_lock_sched()</tt> and <tt>rcu_read_unlock_sched()</tt>
  2482. disable and re-enable preemption, respectively.
  2483. This means that if there was a preemption attempt during the
  2484. RCU-sched read-side critical section, <tt>rcu_read_unlock_sched()</tt>
  2485. will enter the scheduler, with all the latency and overhead entailed.
  2486. Just as with <tt>rcu_read_unlock_bh()</tt>, this can make it look
  2487. as if <tt>rcu_read_unlock_sched()</tt> was executing very slowly.
  2488. However, the highest-priority task won't be preempted, so that task
  2489. will enjoy low-overhead <tt>rcu_read_unlock_sched()</tt> invocations.
  2490. <p>
  2491. The
  2492. <a href="https://lwn.net/Articles/609973/#RCU Per-Flavor API Table">RCU-sched API</a>
  2493. includes
  2494. <tt>rcu_read_lock_sched()</tt>,
  2495. <tt>rcu_read_unlock_sched()</tt>,
  2496. <tt>rcu_read_lock_sched_notrace()</tt>,
  2497. <tt>rcu_read_unlock_sched_notrace()</tt>,
  2498. <tt>rcu_dereference_sched()</tt>,
  2499. <tt>rcu_dereference_sched_check()</tt>,
  2500. <tt>synchronize_sched()</tt>,
  2501. <tt>synchronize_rcu_sched_expedited()</tt>,
  2502. <tt>call_rcu_sched()</tt>,
  2503. <tt>rcu_barrier_sched()</tt>, and
  2504. <tt>rcu_read_lock_sched_held()</tt>.
  2505. However, anything that disables preemption also marks an RCU-sched
  2506. read-side critical section, including
  2507. <tt>preempt_disable()</tt> and <tt>preempt_enable()</tt>,
  2508. <tt>local_irq_save()</tt> and <tt>local_irq_restore()</tt>,
  2509. and so on.
  2510. <h3><a name="Sleepable RCU">Sleepable RCU</a></h3>
  2511. <p>
  2512. For well over a decade, someone saying &ldquo;I need to block within
  2513. an RCU read-side critical section&rdquo; was a reliable indication
  2514. that this someone did not understand RCU.
  2515. After all, if you are always blocking in an RCU read-side critical
  2516. section, you can probably afford to use a higher-overhead synchronization
  2517. mechanism.
  2518. However, that changed with the advent of the Linux kernel's notifiers,
  2519. whose RCU read-side critical
  2520. sections almost never sleep, but sometimes need to.
  2521. This resulted in the introduction of
  2522. <a href="https://lwn.net/Articles/202847/">sleepable RCU</a>,
  2523. or <i>SRCU</i>.
  2524. <p>
  2525. SRCU allows different domains to be defined, with each such domain
  2526. defined by an instance of an <tt>srcu_struct</tt> structure.
  2527. A pointer to this structure must be passed in to each SRCU function,
  2528. for example, <tt>synchronize_srcu(&amp;ss)</tt>, where
  2529. <tt>ss</tt> is the <tt>srcu_struct</tt> structure.
  2530. The key benefit of these domains is that a slow SRCU reader in one
  2531. domain does not delay an SRCU grace period in some other domain.
  2532. That said, one consequence of these domains is that read-side code
  2533. must pass a &ldquo;cookie&rdquo; from <tt>srcu_read_lock()</tt>
  2534. to <tt>srcu_read_unlock()</tt>, for example, as follows:
  2535. <blockquote>
  2536. <pre>
  2537. 1 int idx;
  2538. 2
  2539. 3 idx = srcu_read_lock(&amp;ss);
  2540. 4 do_something();
  2541. 5 srcu_read_unlock(&amp;ss, idx);
  2542. </pre>
  2543. </blockquote>
  2544. <p>
  2545. As noted above, it is legal to block within SRCU read-side critical sections,
  2546. however, with great power comes great responsibility.
  2547. If you block forever in one of a given domain's SRCU read-side critical
  2548. sections, then that domain's grace periods will also be blocked forever.
  2549. Of course, one good way to block forever is to deadlock, which can
  2550. happen if any operation in a given domain's SRCU read-side critical
  2551. section can block waiting, either directly or indirectly, for that domain's
  2552. grace period to elapse.
  2553. For example, this results in a self-deadlock:
  2554. <blockquote>
  2555. <pre>
  2556. 1 int idx;
  2557. 2
  2558. 3 idx = srcu_read_lock(&amp;ss);
  2559. 4 do_something();
  2560. 5 synchronize_srcu(&amp;ss);
  2561. 6 srcu_read_unlock(&amp;ss, idx);
  2562. </pre>
  2563. </blockquote>
  2564. <p>
  2565. However, if line&nbsp;5 acquired a mutex that was held across
  2566. a <tt>synchronize_srcu()</tt> for domain <tt>ss</tt>,
  2567. deadlock would still be possible.
  2568. Furthermore, if line&nbsp;5 acquired a mutex that was held across
  2569. a <tt>synchronize_srcu()</tt> for some other domain <tt>ss1</tt>,
  2570. and if an <tt>ss1</tt>-domain SRCU read-side critical section
  2571. acquired another mutex that was held across as <tt>ss</tt>-domain
  2572. <tt>synchronize_srcu()</tt>,
  2573. deadlock would again be possible.
  2574. Such a deadlock cycle could extend across an arbitrarily large number
  2575. of different SRCU domains.
  2576. Again, with great power comes great responsibility.
  2577. <p>
  2578. Unlike the other RCU flavors, SRCU read-side critical sections can
  2579. run on idle and even offline CPUs.
  2580. This ability requires that <tt>srcu_read_lock()</tt> and
  2581. <tt>srcu_read_unlock()</tt> contain memory barriers, which means
  2582. that SRCU readers will run a bit slower than would RCU readers.
  2583. It also motivates the <tt>smp_mb__after_srcu_read_unlock()</tt>
  2584. API, which, in combination with <tt>srcu_read_unlock()</tt>,
  2585. guarantees a full memory barrier.
  2586. <p>
  2587. The
  2588. <a href="https://lwn.net/Articles/609973/#RCU Per-Flavor API Table">SRCU API</a>
  2589. includes
  2590. <tt>srcu_read_lock()</tt>,
  2591. <tt>srcu_read_unlock()</tt>,
  2592. <tt>srcu_dereference()</tt>,
  2593. <tt>srcu_dereference_check()</tt>,
  2594. <tt>synchronize_srcu()</tt>,
  2595. <tt>synchronize_srcu_expedited()</tt>,
  2596. <tt>call_srcu()</tt>,
  2597. <tt>srcu_barrier()</tt>, and
  2598. <tt>srcu_read_lock_held()</tt>.
  2599. It also includes
  2600. <tt>DEFINE_SRCU()</tt>,
  2601. <tt>DEFINE_STATIC_SRCU()</tt>, and
  2602. <tt>init_srcu_struct()</tt>
  2603. APIs for defining and initializing <tt>srcu_struct</tt> structures.
  2604. <h3><a name="Tasks RCU">Tasks RCU</a></h3>
  2605. <p>
  2606. Some forms of tracing use &ldquo;tramopolines&rdquo; to handle the
  2607. binary rewriting required to install different types of probes.
  2608. It would be good to be able to free old trampolines, which sounds
  2609. like a job for some form of RCU.
  2610. However, because it is necessary to be able to install a trace
  2611. anywhere in the code, it is not possible to use read-side markers
  2612. such as <tt>rcu_read_lock()</tt> and <tt>rcu_read_unlock()</tt>.
  2613. In addition, it does not work to have these markers in the trampoline
  2614. itself, because there would need to be instructions following
  2615. <tt>rcu_read_unlock()</tt>.
  2616. Although <tt>synchronize_rcu()</tt> would guarantee that execution
  2617. reached the <tt>rcu_read_unlock()</tt>, it would not be able to
  2618. guarantee that execution had completely left the trampoline.
  2619. <p>
  2620. The solution, in the form of
  2621. <a href="https://lwn.net/Articles/607117/"><i>Tasks RCU</i></a>,
  2622. is to have implicit
  2623. read-side critical sections that are delimited by voluntary context
  2624. switches, that is, calls to <tt>schedule()</tt>,
  2625. <tt>cond_resched_rcu_qs()</tt>, and
  2626. <tt>synchronize_rcu_tasks()</tt>.
  2627. In addition, transitions to and from userspace execution also delimit
  2628. tasks-RCU read-side critical sections.
  2629. <p>
  2630. The tasks-RCU API is quite compact, consisting only of
  2631. <tt>call_rcu_tasks()</tt>,
  2632. <tt>synchronize_rcu_tasks()</tt>, and
  2633. <tt>rcu_barrier_tasks()</tt>.
  2634. <h3><a name="Waiting for Multiple Grace Periods">
  2635. Waiting for Multiple Grace Periods</a></h3>
  2636. <p>
  2637. Perhaps you have an RCU protected data structure that is accessed from
  2638. RCU read-side critical sections, from softirq handlers, and from
  2639. hardware interrupt handlers.
  2640. That is three flavors of RCU, the normal flavor, the bottom-half flavor,
  2641. and the sched flavor.
  2642. How to wait for a compound grace period?
  2643. <p>
  2644. The best approach is usually to &ldquo;just say no!&rdquo; and
  2645. insert <tt>rcu_read_lock()</tt> and <tt>rcu_read_unlock()</tt>
  2646. around each RCU read-side critical section, regardless of what
  2647. environment it happens to be in.
  2648. But suppose that some of the RCU read-side critical sections are
  2649. on extremely hot code paths, and that use of <tt>CONFIG_PREEMPT=n</tt>
  2650. is not a viable option, so that <tt>rcu_read_lock()</tt> and
  2651. <tt>rcu_read_unlock()</tt> are not free.
  2652. What then?
  2653. <p>
  2654. You <i>could</i> wait on all three grace periods in succession, as follows:
  2655. <blockquote>
  2656. <pre>
  2657. 1 synchronize_rcu();
  2658. 2 synchronize_rcu_bh();
  2659. 3 synchronize_sched();
  2660. </pre>
  2661. </blockquote>
  2662. <p>
  2663. This works, but triples the update-side latency penalty.
  2664. In cases where this is not acceptable, <tt>synchronize_rcu_mult()</tt>
  2665. may be used to wait on all three flavors of grace period concurrently:
  2666. <blockquote>
  2667. <pre>
  2668. 1 synchronize_rcu_mult(call_rcu, call_rcu_bh, call_rcu_sched);
  2669. </pre>
  2670. </blockquote>
  2671. <p>
  2672. But what if it is necessary to also wait on SRCU?
  2673. This can be done as follows:
  2674. <blockquote>
  2675. <pre>
  2676. 1 static void call_my_srcu(struct rcu_head *head,
  2677. 2 void (*func)(struct rcu_head *head))
  2678. 3 {
  2679. 4 call_srcu(&amp;my_srcu, head, func);
  2680. 5 }
  2681. 6
  2682. 7 synchronize_rcu_mult(call_rcu, call_rcu_bh, call_rcu_sched, call_my_srcu);
  2683. </pre>
  2684. </blockquote>
  2685. <p>
  2686. If you needed to wait on multiple different flavors of SRCU
  2687. (but why???), you would need to create a wrapper function resembling
  2688. <tt>call_my_srcu()</tt> for each SRCU flavor.
  2689. <table>
  2690. <tr><th>&nbsp;</th></tr>
  2691. <tr><th align="left">Quick Quiz:</th></tr>
  2692. <tr><td>
  2693. But what if I need to wait for multiple RCU flavors, but I also need
  2694. the grace periods to be expedited?
  2695. </td></tr>
  2696. <tr><th align="left">Answer:</th></tr>
  2697. <tr><td bgcolor="#ffffff"><font color="ffffff">
  2698. If you are using expedited grace periods, there should be less penalty
  2699. for waiting on them in succession.
  2700. But if that is nevertheless a problem, you can use workqueues
  2701. or multiple kthreads to wait on the various expedited grace
  2702. periods concurrently.
  2703. </font></td></tr>
  2704. <tr><td>&nbsp;</td></tr>
  2705. </table>
  2706. <p>
  2707. Again, it is usually better to adjust the RCU read-side critical sections
  2708. to use a single flavor of RCU, but when this is not feasible, you can use
  2709. <tt>synchronize_rcu_mult()</tt>.
  2710. <h2><a name="Possible Future Changes">Possible Future Changes</a></h2>
  2711. <p>
  2712. One of the tricks that RCU uses to attain update-side scalability is
  2713. to increase grace-period latency with increasing numbers of CPUs.
  2714. If this becomes a serious problem, it will be necessary to rework the
  2715. grace-period state machine so as to avoid the need for the additional
  2716. latency.
  2717. <p>
  2718. Expedited grace periods scan the CPUs, so their latency and overhead
  2719. increases with increasing numbers of CPUs.
  2720. If this becomes a serious problem on large systems, it will be necessary
  2721. to do some redesign to avoid this scalability problem.
  2722. <p>
  2723. RCU disables CPU hotplug in a few places, perhaps most notably in the
  2724. expedited grace-period and <tt>rcu_barrier()</tt> operations.
  2725. If there is a strong reason to use expedited grace periods in CPU-hotplug
  2726. notifiers, it will be necessary to avoid disabling CPU hotplug.
  2727. This would introduce some complexity, so there had better be a <i>very</i>
  2728. good reason.
  2729. <p>
  2730. The tradeoff between grace-period latency on the one hand and interruptions
  2731. of other CPUs on the other hand may need to be re-examined.
  2732. The desire is of course for zero grace-period latency as well as zero
  2733. interprocessor interrupts undertaken during an expedited grace period
  2734. operation.
  2735. While this ideal is unlikely to be achievable, it is quite possible that
  2736. further improvements can be made.
  2737. <p>
  2738. The multiprocessor implementations of RCU use a combining tree that
  2739. groups CPUs so as to reduce lock contention and increase cache locality.
  2740. However, this combining tree does not spread its memory across NUMA
  2741. nodes nor does it align the CPU groups with hardware features such
  2742. as sockets or cores.
  2743. Such spreading and alignment is currently believed to be unnecessary
  2744. because the hotpath read-side primitives do not access the combining
  2745. tree, nor does <tt>call_rcu()</tt> in the common case.
  2746. If you believe that your architecture needs such spreading and alignment,
  2747. then your architecture should also benefit from the
  2748. <tt>rcutree.rcu_fanout_leaf</tt> boot parameter, which can be set
  2749. to the number of CPUs in a socket, NUMA node, or whatever.
  2750. If the number of CPUs is too large, use a fraction of the number of
  2751. CPUs.
  2752. If the number of CPUs is a large prime number, well, that certainly
  2753. is an &ldquo;interesting&rdquo; architectural choice!
  2754. More flexible arrangements might be considered, but only if
  2755. <tt>rcutree.rcu_fanout_leaf</tt> has proven inadequate, and only
  2756. if the inadequacy has been demonstrated by a carefully run and
  2757. realistic system-level workload.
  2758. <p>
  2759. Please note that arrangements that require RCU to remap CPU numbers will
  2760. require extremely good demonstration of need and full exploration of
  2761. alternatives.
  2762. <p>
  2763. There is an embarrassingly large number of flavors of RCU, and this
  2764. number has been increasing over time.
  2765. Perhaps it will be possible to combine some at some future date.
  2766. <p>
  2767. RCU's various kthreads are reasonably recent additions.
  2768. It is quite likely that adjustments will be required to more gracefully
  2769. handle extreme loads.
  2770. It might also be necessary to be able to relate CPU utilization by
  2771. RCU's kthreads and softirq handlers to the code that instigated this
  2772. CPU utilization.
  2773. For example, RCU callback overhead might be charged back to the
  2774. originating <tt>call_rcu()</tt> instance, though probably not
  2775. in production kernels.
  2776. <h2><a name="Summary">Summary</a></h2>
  2777. <p>
  2778. This document has presented more than two decade's worth of RCU
  2779. requirements.
  2780. Given that the requirements keep changing, this will not be the last
  2781. word on this subject, but at least it serves to get an important
  2782. subset of the requirements set forth.
  2783. <h2><a name="Acknowledgments">Acknowledgments</a></h2>
  2784. I am grateful to Steven Rostedt, Lai Jiangshan, Ingo Molnar,
  2785. Oleg Nesterov, Borislav Petkov, Peter Zijlstra, Boqun Feng, and
  2786. Andy Lutomirski for their help in rendering
  2787. this article human readable, and to Michelle Rankin for her support
  2788. of this effort.
  2789. Other contributions are acknowledged in the Linux kernel's git archive.
  2790. The cartoon is copyright (c) 2013 by Melissa Broussard,
  2791. and is provided
  2792. under the terms of the Creative Commons Attribution-Share Alike 3.0
  2793. United States license.
  2794. </body></html>