tree.c 143 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, you can access it online at
  16. * http://www.gnu.org/licenses/gpl-2.0.html.
  17. *
  18. * Copyright IBM Corporation, 2008
  19. *
  20. * Authors: Dipankar Sarma <dipankar@in.ibm.com>
  21. * Manfred Spraul <manfred@colorfullife.com>
  22. * Paul E. McKenney <paulmck@linux.vnet.ibm.com> Hierarchical version
  23. *
  24. * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
  25. * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
  26. *
  27. * For detailed explanation of Read-Copy Update mechanism see -
  28. * Documentation/RCU
  29. */
  30. #include <linux/types.h>
  31. #include <linux/kernel.h>
  32. #include <linux/init.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/smp.h>
  35. #include <linux/rcupdate.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/sched.h>
  38. #include <linux/nmi.h>
  39. #include <linux/atomic.h>
  40. #include <linux/bitops.h>
  41. #include <linux/export.h>
  42. #include <linux/completion.h>
  43. #include <linux/moduleparam.h>
  44. #include <linux/module.h>
  45. #include <linux/percpu.h>
  46. #include <linux/notifier.h>
  47. #include <linux/cpu.h>
  48. #include <linux/mutex.h>
  49. #include <linux/time.h>
  50. #include <linux/kernel_stat.h>
  51. #include <linux/wait.h>
  52. #include <linux/kthread.h>
  53. #include <linux/prefetch.h>
  54. #include <linux/delay.h>
  55. #include <linux/stop_machine.h>
  56. #include <linux/random.h>
  57. #include <linux/trace_events.h>
  58. #include <linux/suspend.h>
  59. #include "tree.h"
  60. #include "rcu.h"
  61. MODULE_ALIAS("rcutree");
  62. #ifdef MODULE_PARAM_PREFIX
  63. #undef MODULE_PARAM_PREFIX
  64. #endif
  65. #define MODULE_PARAM_PREFIX "rcutree."
  66. /* Data structures. */
  67. /*
  68. * In order to export the rcu_state name to the tracing tools, it
  69. * needs to be added in the __tracepoint_string section.
  70. * This requires defining a separate variable tp_<sname>_varname
  71. * that points to the string being used, and this will allow
  72. * the tracing userspace tools to be able to decipher the string
  73. * address to the matching string.
  74. */
  75. #ifdef CONFIG_TRACING
  76. # define DEFINE_RCU_TPS(sname) \
  77. static char sname##_varname[] = #sname; \
  78. static const char *tp_##sname##_varname __used __tracepoint_string = sname##_varname;
  79. # define RCU_STATE_NAME(sname) sname##_varname
  80. #else
  81. # define DEFINE_RCU_TPS(sname)
  82. # define RCU_STATE_NAME(sname) __stringify(sname)
  83. #endif
  84. #define RCU_STATE_INITIALIZER(sname, sabbr, cr) \
  85. DEFINE_RCU_TPS(sname) \
  86. static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, sname##_data); \
  87. struct rcu_state sname##_state = { \
  88. .level = { &sname##_state.node[0] }, \
  89. .rda = &sname##_data, \
  90. .call = cr, \
  91. .gp_state = RCU_GP_IDLE, \
  92. .gpnum = 0UL - 300UL, \
  93. .completed = 0UL - 300UL, \
  94. .orphan_lock = __RAW_SPIN_LOCK_UNLOCKED(&sname##_state.orphan_lock), \
  95. .orphan_nxttail = &sname##_state.orphan_nxtlist, \
  96. .orphan_donetail = &sname##_state.orphan_donelist, \
  97. .barrier_mutex = __MUTEX_INITIALIZER(sname##_state.barrier_mutex), \
  98. .name = RCU_STATE_NAME(sname), \
  99. .abbr = sabbr, \
  100. }
  101. RCU_STATE_INITIALIZER(rcu_sched, 's', call_rcu_sched);
  102. RCU_STATE_INITIALIZER(rcu_bh, 'b', call_rcu_bh);
  103. static struct rcu_state *const rcu_state_p;
  104. LIST_HEAD(rcu_struct_flavors);
  105. /* Dump rcu_node combining tree at boot to verify correct setup. */
  106. static bool dump_tree;
  107. module_param(dump_tree, bool, 0444);
  108. /* Control rcu_node-tree auto-balancing at boot time. */
  109. static bool rcu_fanout_exact;
  110. module_param(rcu_fanout_exact, bool, 0444);
  111. /* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */
  112. static int rcu_fanout_leaf = RCU_FANOUT_LEAF;
  113. module_param(rcu_fanout_leaf, int, 0444);
  114. int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
  115. /* Number of rcu_nodes at specified level. */
  116. static int num_rcu_lvl[] = NUM_RCU_LVL_INIT;
  117. int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
  118. /*
  119. * The rcu_scheduler_active variable transitions from zero to one just
  120. * before the first task is spawned. So when this variable is zero, RCU
  121. * can assume that there is but one task, allowing RCU to (for example)
  122. * optimize synchronize_sched() to a simple barrier(). When this variable
  123. * is one, RCU must actually do all the hard work required to detect real
  124. * grace periods. This variable is also used to suppress boot-time false
  125. * positives from lockdep-RCU error checking.
  126. */
  127. int rcu_scheduler_active __read_mostly;
  128. EXPORT_SYMBOL_GPL(rcu_scheduler_active);
  129. /*
  130. * The rcu_scheduler_fully_active variable transitions from zero to one
  131. * during the early_initcall() processing, which is after the scheduler
  132. * is capable of creating new tasks. So RCU processing (for example,
  133. * creating tasks for RCU priority boosting) must be delayed until after
  134. * rcu_scheduler_fully_active transitions from zero to one. We also
  135. * currently delay invocation of any RCU callbacks until after this point.
  136. *
  137. * It might later prove better for people registering RCU callbacks during
  138. * early boot to take responsibility for these callbacks, but one step at
  139. * a time.
  140. */
  141. static int rcu_scheduler_fully_active __read_mostly;
  142. static void rcu_init_new_rnp(struct rcu_node *rnp_leaf);
  143. static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf);
  144. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
  145. static void invoke_rcu_core(void);
  146. static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp);
  147. static void rcu_report_exp_rdp(struct rcu_state *rsp,
  148. struct rcu_data *rdp, bool wake);
  149. /* rcuc/rcub kthread realtime priority */
  150. #ifdef CONFIG_RCU_KTHREAD_PRIO
  151. static int kthread_prio = CONFIG_RCU_KTHREAD_PRIO;
  152. #else /* #ifdef CONFIG_RCU_KTHREAD_PRIO */
  153. static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0;
  154. #endif /* #else #ifdef CONFIG_RCU_KTHREAD_PRIO */
  155. module_param(kthread_prio, int, 0644);
  156. /* Delay in jiffies for grace-period initialization delays, debug only. */
  157. #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT
  158. static int gp_preinit_delay = CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT_DELAY;
  159. module_param(gp_preinit_delay, int, 0644);
  160. #else /* #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT */
  161. static const int gp_preinit_delay;
  162. #endif /* #else #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT */
  163. #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_INIT
  164. static int gp_init_delay = CONFIG_RCU_TORTURE_TEST_SLOW_INIT_DELAY;
  165. module_param(gp_init_delay, int, 0644);
  166. #else /* #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_INIT */
  167. static const int gp_init_delay;
  168. #endif /* #else #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_INIT */
  169. #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP
  170. static int gp_cleanup_delay = CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP_DELAY;
  171. module_param(gp_cleanup_delay, int, 0644);
  172. #else /* #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP */
  173. static const int gp_cleanup_delay;
  174. #endif /* #else #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP */
  175. /*
  176. * Number of grace periods between delays, normalized by the duration of
  177. * the delay. The longer the the delay, the more the grace periods between
  178. * each delay. The reason for this normalization is that it means that,
  179. * for non-zero delays, the overall slowdown of grace periods is constant
  180. * regardless of the duration of the delay. This arrangement balances
  181. * the need for long delays to increase some race probabilities with the
  182. * need for fast grace periods to increase other race probabilities.
  183. */
  184. #define PER_RCU_NODE_PERIOD 3 /* Number of grace periods between delays. */
  185. /*
  186. * Track the rcutorture test sequence number and the update version
  187. * number within a given test. The rcutorture_testseq is incremented
  188. * on every rcutorture module load and unload, so has an odd value
  189. * when a test is running. The rcutorture_vernum is set to zero
  190. * when rcutorture starts and is incremented on each rcutorture update.
  191. * These variables enable correlating rcutorture output with the
  192. * RCU tracing information.
  193. */
  194. unsigned long rcutorture_testseq;
  195. unsigned long rcutorture_vernum;
  196. /*
  197. * Compute the mask of online CPUs for the specified rcu_node structure.
  198. * This will not be stable unless the rcu_node structure's ->lock is
  199. * held, but the bit corresponding to the current CPU will be stable
  200. * in most contexts.
  201. */
  202. unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp)
  203. {
  204. return READ_ONCE(rnp->qsmaskinitnext);
  205. }
  206. /*
  207. * Return true if an RCU grace period is in progress. The READ_ONCE()s
  208. * permit this function to be invoked without holding the root rcu_node
  209. * structure's ->lock, but of course results can be subject to change.
  210. */
  211. static int rcu_gp_in_progress(struct rcu_state *rsp)
  212. {
  213. return READ_ONCE(rsp->completed) != READ_ONCE(rsp->gpnum);
  214. }
  215. /*
  216. * Note a quiescent state. Because we do not need to know
  217. * how many quiescent states passed, just if there was at least
  218. * one since the start of the grace period, this just sets a flag.
  219. * The caller must have disabled preemption.
  220. */
  221. void rcu_sched_qs(void)
  222. {
  223. if (!__this_cpu_read(rcu_sched_data.cpu_no_qs.s))
  224. return;
  225. trace_rcu_grace_period(TPS("rcu_sched"),
  226. __this_cpu_read(rcu_sched_data.gpnum),
  227. TPS("cpuqs"));
  228. __this_cpu_write(rcu_sched_data.cpu_no_qs.b.norm, false);
  229. if (!__this_cpu_read(rcu_sched_data.cpu_no_qs.b.exp))
  230. return;
  231. __this_cpu_write(rcu_sched_data.cpu_no_qs.b.exp, false);
  232. rcu_report_exp_rdp(&rcu_sched_state,
  233. this_cpu_ptr(&rcu_sched_data), true);
  234. }
  235. void rcu_bh_qs(void)
  236. {
  237. if (__this_cpu_read(rcu_bh_data.cpu_no_qs.s)) {
  238. trace_rcu_grace_period(TPS("rcu_bh"),
  239. __this_cpu_read(rcu_bh_data.gpnum),
  240. TPS("cpuqs"));
  241. __this_cpu_write(rcu_bh_data.cpu_no_qs.b.norm, false);
  242. }
  243. }
  244. static DEFINE_PER_CPU(int, rcu_sched_qs_mask);
  245. static DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
  246. .dynticks_nesting = DYNTICK_TASK_EXIT_IDLE,
  247. .dynticks = ATOMIC_INIT(1),
  248. #ifdef CONFIG_NO_HZ_FULL_SYSIDLE
  249. .dynticks_idle_nesting = DYNTICK_TASK_NEST_VALUE,
  250. .dynticks_idle = ATOMIC_INIT(1),
  251. #endif /* #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
  252. };
  253. DEFINE_PER_CPU_SHARED_ALIGNED(unsigned long, rcu_qs_ctr);
  254. EXPORT_PER_CPU_SYMBOL_GPL(rcu_qs_ctr);
  255. /*
  256. * Let the RCU core know that this CPU has gone through the scheduler,
  257. * which is a quiescent state. This is called when the need for a
  258. * quiescent state is urgent, so we burn an atomic operation and full
  259. * memory barriers to let the RCU core know about it, regardless of what
  260. * this CPU might (or might not) do in the near future.
  261. *
  262. * We inform the RCU core by emulating a zero-duration dyntick-idle
  263. * period, which we in turn do by incrementing the ->dynticks counter
  264. * by two.
  265. *
  266. * The caller must have disabled interrupts.
  267. */
  268. static void rcu_momentary_dyntick_idle(void)
  269. {
  270. struct rcu_data *rdp;
  271. struct rcu_dynticks *rdtp;
  272. int resched_mask;
  273. struct rcu_state *rsp;
  274. /*
  275. * Yes, we can lose flag-setting operations. This is OK, because
  276. * the flag will be set again after some delay.
  277. */
  278. resched_mask = raw_cpu_read(rcu_sched_qs_mask);
  279. raw_cpu_write(rcu_sched_qs_mask, 0);
  280. /* Find the flavor that needs a quiescent state. */
  281. for_each_rcu_flavor(rsp) {
  282. rdp = raw_cpu_ptr(rsp->rda);
  283. if (!(resched_mask & rsp->flavor_mask))
  284. continue;
  285. smp_mb(); /* rcu_sched_qs_mask before cond_resched_completed. */
  286. if (READ_ONCE(rdp->mynode->completed) !=
  287. READ_ONCE(rdp->cond_resched_completed))
  288. continue;
  289. /*
  290. * Pretend to be momentarily idle for the quiescent state.
  291. * This allows the grace-period kthread to record the
  292. * quiescent state, with no need for this CPU to do anything
  293. * further.
  294. */
  295. rdtp = this_cpu_ptr(&rcu_dynticks);
  296. smp_mb__before_atomic(); /* Earlier stuff before QS. */
  297. atomic_add(2, &rdtp->dynticks); /* QS. */
  298. smp_mb__after_atomic(); /* Later stuff after QS. */
  299. break;
  300. }
  301. }
  302. /*
  303. * Note a context switch. This is a quiescent state for RCU-sched,
  304. * and requires special handling for preemptible RCU.
  305. * The caller must have disabled interrupts.
  306. */
  307. void rcu_note_context_switch(void)
  308. {
  309. barrier(); /* Avoid RCU read-side critical sections leaking down. */
  310. trace_rcu_utilization(TPS("Start context switch"));
  311. rcu_sched_qs();
  312. rcu_preempt_note_context_switch();
  313. if (unlikely(raw_cpu_read(rcu_sched_qs_mask)))
  314. rcu_momentary_dyntick_idle();
  315. trace_rcu_utilization(TPS("End context switch"));
  316. barrier(); /* Avoid RCU read-side critical sections leaking up. */
  317. }
  318. EXPORT_SYMBOL_GPL(rcu_note_context_switch);
  319. /*
  320. * Register a quiescent state for all RCU flavors. If there is an
  321. * emergency, invoke rcu_momentary_dyntick_idle() to do a heavy-weight
  322. * dyntick-idle quiescent state visible to other CPUs (but only for those
  323. * RCU flavors in desperate need of a quiescent state, which will normally
  324. * be none of them). Either way, do a lightweight quiescent state for
  325. * all RCU flavors.
  326. *
  327. * The barrier() calls are redundant in the common case when this is
  328. * called externally, but just in case this is called from within this
  329. * file.
  330. *
  331. */
  332. void rcu_all_qs(void)
  333. {
  334. unsigned long flags;
  335. barrier(); /* Avoid RCU read-side critical sections leaking down. */
  336. if (unlikely(raw_cpu_read(rcu_sched_qs_mask))) {
  337. local_irq_save(flags);
  338. rcu_momentary_dyntick_idle();
  339. local_irq_restore(flags);
  340. }
  341. this_cpu_inc(rcu_qs_ctr);
  342. barrier(); /* Avoid RCU read-side critical sections leaking up. */
  343. }
  344. EXPORT_SYMBOL_GPL(rcu_all_qs);
  345. static long blimit = 10; /* Maximum callbacks per rcu_do_batch. */
  346. static long qhimark = 10000; /* If this many pending, ignore blimit. */
  347. static long qlowmark = 100; /* Once only this many pending, use blimit. */
  348. module_param(blimit, long, 0444);
  349. module_param(qhimark, long, 0444);
  350. module_param(qlowmark, long, 0444);
  351. static ulong jiffies_till_first_fqs = ULONG_MAX;
  352. static ulong jiffies_till_next_fqs = ULONG_MAX;
  353. static bool rcu_kick_kthreads;
  354. module_param(jiffies_till_first_fqs, ulong, 0644);
  355. module_param(jiffies_till_next_fqs, ulong, 0644);
  356. module_param(rcu_kick_kthreads, bool, 0644);
  357. /*
  358. * How long the grace period must be before we start recruiting
  359. * quiescent-state help from rcu_note_context_switch().
  360. */
  361. static ulong jiffies_till_sched_qs = HZ / 20;
  362. module_param(jiffies_till_sched_qs, ulong, 0644);
  363. static bool rcu_start_gp_advanced(struct rcu_state *rsp, struct rcu_node *rnp,
  364. struct rcu_data *rdp);
  365. static void force_qs_rnp(struct rcu_state *rsp,
  366. int (*f)(struct rcu_data *rsp, bool *isidle,
  367. unsigned long *maxj),
  368. bool *isidle, unsigned long *maxj);
  369. static void force_quiescent_state(struct rcu_state *rsp);
  370. static int rcu_pending(void);
  371. /*
  372. * Return the number of RCU batches started thus far for debug & stats.
  373. */
  374. unsigned long rcu_batches_started(void)
  375. {
  376. return rcu_state_p->gpnum;
  377. }
  378. EXPORT_SYMBOL_GPL(rcu_batches_started);
  379. /*
  380. * Return the number of RCU-sched batches started thus far for debug & stats.
  381. */
  382. unsigned long rcu_batches_started_sched(void)
  383. {
  384. return rcu_sched_state.gpnum;
  385. }
  386. EXPORT_SYMBOL_GPL(rcu_batches_started_sched);
  387. /*
  388. * Return the number of RCU BH batches started thus far for debug & stats.
  389. */
  390. unsigned long rcu_batches_started_bh(void)
  391. {
  392. return rcu_bh_state.gpnum;
  393. }
  394. EXPORT_SYMBOL_GPL(rcu_batches_started_bh);
  395. /*
  396. * Return the number of RCU batches completed thus far for debug & stats.
  397. */
  398. unsigned long rcu_batches_completed(void)
  399. {
  400. return rcu_state_p->completed;
  401. }
  402. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  403. /*
  404. * Return the number of RCU-sched batches completed thus far for debug & stats.
  405. */
  406. unsigned long rcu_batches_completed_sched(void)
  407. {
  408. return rcu_sched_state.completed;
  409. }
  410. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  411. /*
  412. * Return the number of RCU BH batches completed thus far for debug & stats.
  413. */
  414. unsigned long rcu_batches_completed_bh(void)
  415. {
  416. return rcu_bh_state.completed;
  417. }
  418. EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
  419. /*
  420. * Force a quiescent state.
  421. */
  422. void rcu_force_quiescent_state(void)
  423. {
  424. force_quiescent_state(rcu_state_p);
  425. }
  426. EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  427. /*
  428. * Force a quiescent state for RCU BH.
  429. */
  430. void rcu_bh_force_quiescent_state(void)
  431. {
  432. force_quiescent_state(&rcu_bh_state);
  433. }
  434. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  435. /*
  436. * Force a quiescent state for RCU-sched.
  437. */
  438. void rcu_sched_force_quiescent_state(void)
  439. {
  440. force_quiescent_state(&rcu_sched_state);
  441. }
  442. EXPORT_SYMBOL_GPL(rcu_sched_force_quiescent_state);
  443. /*
  444. * Show the state of the grace-period kthreads.
  445. */
  446. void show_rcu_gp_kthreads(void)
  447. {
  448. struct rcu_state *rsp;
  449. for_each_rcu_flavor(rsp) {
  450. pr_info("%s: wait state: %d ->state: %#lx\n",
  451. rsp->name, rsp->gp_state, rsp->gp_kthread->state);
  452. /* sched_show_task(rsp->gp_kthread); */
  453. }
  454. }
  455. EXPORT_SYMBOL_GPL(show_rcu_gp_kthreads);
  456. /*
  457. * Record the number of times rcutorture tests have been initiated and
  458. * terminated. This information allows the debugfs tracing stats to be
  459. * correlated to the rcutorture messages, even when the rcutorture module
  460. * is being repeatedly loaded and unloaded. In other words, we cannot
  461. * store this state in rcutorture itself.
  462. */
  463. void rcutorture_record_test_transition(void)
  464. {
  465. rcutorture_testseq++;
  466. rcutorture_vernum = 0;
  467. }
  468. EXPORT_SYMBOL_GPL(rcutorture_record_test_transition);
  469. /*
  470. * Send along grace-period-related data for rcutorture diagnostics.
  471. */
  472. void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
  473. unsigned long *gpnum, unsigned long *completed)
  474. {
  475. struct rcu_state *rsp = NULL;
  476. switch (test_type) {
  477. case RCU_FLAVOR:
  478. rsp = rcu_state_p;
  479. break;
  480. case RCU_BH_FLAVOR:
  481. rsp = &rcu_bh_state;
  482. break;
  483. case RCU_SCHED_FLAVOR:
  484. rsp = &rcu_sched_state;
  485. break;
  486. default:
  487. break;
  488. }
  489. if (rsp != NULL) {
  490. *flags = READ_ONCE(rsp->gp_flags);
  491. *gpnum = READ_ONCE(rsp->gpnum);
  492. *completed = READ_ONCE(rsp->completed);
  493. return;
  494. }
  495. *flags = 0;
  496. *gpnum = 0;
  497. *completed = 0;
  498. }
  499. EXPORT_SYMBOL_GPL(rcutorture_get_gp_data);
  500. /*
  501. * Record the number of writer passes through the current rcutorture test.
  502. * This is also used to correlate debugfs tracing stats with the rcutorture
  503. * messages.
  504. */
  505. void rcutorture_record_progress(unsigned long vernum)
  506. {
  507. rcutorture_vernum++;
  508. }
  509. EXPORT_SYMBOL_GPL(rcutorture_record_progress);
  510. /*
  511. * Does the CPU have callbacks ready to be invoked?
  512. */
  513. static int
  514. cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp)
  515. {
  516. return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL] &&
  517. rdp->nxttail[RCU_DONE_TAIL] != NULL;
  518. }
  519. /*
  520. * Return the root node of the specified rcu_state structure.
  521. */
  522. static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
  523. {
  524. return &rsp->node[0];
  525. }
  526. /*
  527. * Is there any need for future grace periods?
  528. * Interrupts must be disabled. If the caller does not hold the root
  529. * rnp_node structure's ->lock, the results are advisory only.
  530. */
  531. static int rcu_future_needs_gp(struct rcu_state *rsp)
  532. {
  533. struct rcu_node *rnp = rcu_get_root(rsp);
  534. int idx = (READ_ONCE(rnp->completed) + 1) & 0x1;
  535. int *fp = &rnp->need_future_gp[idx];
  536. return READ_ONCE(*fp);
  537. }
  538. /*
  539. * Does the current CPU require a not-yet-started grace period?
  540. * The caller must have disabled interrupts to prevent races with
  541. * normal callback registry.
  542. */
  543. static bool
  544. cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
  545. {
  546. int i;
  547. if (rcu_gp_in_progress(rsp))
  548. return false; /* No, a grace period is already in progress. */
  549. if (rcu_future_needs_gp(rsp))
  550. return true; /* Yes, a no-CBs CPU needs one. */
  551. if (!rdp->nxttail[RCU_NEXT_TAIL])
  552. return false; /* No, this is a no-CBs (or offline) CPU. */
  553. if (*rdp->nxttail[RCU_NEXT_READY_TAIL])
  554. return true; /* Yes, CPU has newly registered callbacks. */
  555. for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++)
  556. if (rdp->nxttail[i - 1] != rdp->nxttail[i] &&
  557. ULONG_CMP_LT(READ_ONCE(rsp->completed),
  558. rdp->nxtcompleted[i]))
  559. return true; /* Yes, CBs for future grace period. */
  560. return false; /* No grace period needed. */
  561. }
  562. /*
  563. * rcu_eqs_enter_common - current CPU is moving towards extended quiescent state
  564. *
  565. * If the new value of the ->dynticks_nesting counter now is zero,
  566. * we really have entered idle, and must do the appropriate accounting.
  567. * The caller must have disabled interrupts.
  568. */
  569. static void rcu_eqs_enter_common(long long oldval, bool user)
  570. {
  571. struct rcu_state *rsp;
  572. struct rcu_data *rdp;
  573. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  574. trace_rcu_dyntick(TPS("Start"), oldval, rdtp->dynticks_nesting);
  575. if (IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  576. !user && !is_idle_task(current)) {
  577. struct task_struct *idle __maybe_unused =
  578. idle_task(smp_processor_id());
  579. trace_rcu_dyntick(TPS("Error on entry: not idle task"), oldval, 0);
  580. rcu_ftrace_dump(DUMP_ORIG);
  581. WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
  582. current->pid, current->comm,
  583. idle->pid, idle->comm); /* must be idle task! */
  584. }
  585. for_each_rcu_flavor(rsp) {
  586. rdp = this_cpu_ptr(rsp->rda);
  587. do_nocb_deferred_wakeup(rdp);
  588. }
  589. rcu_prepare_for_idle();
  590. /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
  591. smp_mb__before_atomic(); /* See above. */
  592. atomic_inc(&rdtp->dynticks);
  593. smp_mb__after_atomic(); /* Force ordering with next sojourn. */
  594. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  595. atomic_read(&rdtp->dynticks) & 0x1);
  596. rcu_dynticks_task_enter();
  597. /*
  598. * It is illegal to enter an extended quiescent state while
  599. * in an RCU read-side critical section.
  600. */
  601. RCU_LOCKDEP_WARN(lock_is_held(&rcu_lock_map),
  602. "Illegal idle entry in RCU read-side critical section.");
  603. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map),
  604. "Illegal idle entry in RCU-bh read-side critical section.");
  605. RCU_LOCKDEP_WARN(lock_is_held(&rcu_sched_lock_map),
  606. "Illegal idle entry in RCU-sched read-side critical section.");
  607. }
  608. /*
  609. * Enter an RCU extended quiescent state, which can be either the
  610. * idle loop or adaptive-tickless usermode execution.
  611. */
  612. static void rcu_eqs_enter(bool user)
  613. {
  614. long long oldval;
  615. struct rcu_dynticks *rdtp;
  616. rdtp = this_cpu_ptr(&rcu_dynticks);
  617. oldval = rdtp->dynticks_nesting;
  618. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  619. (oldval & DYNTICK_TASK_NEST_MASK) == 0);
  620. if ((oldval & DYNTICK_TASK_NEST_MASK) == DYNTICK_TASK_NEST_VALUE) {
  621. rdtp->dynticks_nesting = 0;
  622. rcu_eqs_enter_common(oldval, user);
  623. } else {
  624. rdtp->dynticks_nesting -= DYNTICK_TASK_NEST_VALUE;
  625. }
  626. }
  627. /**
  628. * rcu_idle_enter - inform RCU that current CPU is entering idle
  629. *
  630. * Enter idle mode, in other words, -leave- the mode in which RCU
  631. * read-side critical sections can occur. (Though RCU read-side
  632. * critical sections can occur in irq handlers in idle, a possibility
  633. * handled by irq_enter() and irq_exit().)
  634. *
  635. * We crowbar the ->dynticks_nesting field to zero to allow for
  636. * the possibility of usermode upcalls having messed up our count
  637. * of interrupt nesting level during the prior busy period.
  638. */
  639. void rcu_idle_enter(void)
  640. {
  641. unsigned long flags;
  642. local_irq_save(flags);
  643. rcu_eqs_enter(false);
  644. rcu_sysidle_enter(0);
  645. local_irq_restore(flags);
  646. }
  647. EXPORT_SYMBOL_GPL(rcu_idle_enter);
  648. #ifdef CONFIG_NO_HZ_FULL
  649. /**
  650. * rcu_user_enter - inform RCU that we are resuming userspace.
  651. *
  652. * Enter RCU idle mode right before resuming userspace. No use of RCU
  653. * is permitted between this call and rcu_user_exit(). This way the
  654. * CPU doesn't need to maintain the tick for RCU maintenance purposes
  655. * when the CPU runs in userspace.
  656. */
  657. void rcu_user_enter(void)
  658. {
  659. rcu_eqs_enter(1);
  660. }
  661. #endif /* CONFIG_NO_HZ_FULL */
  662. /**
  663. * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
  664. *
  665. * Exit from an interrupt handler, which might possibly result in entering
  666. * idle mode, in other words, leaving the mode in which read-side critical
  667. * sections can occur. The caller must have disabled interrupts.
  668. *
  669. * This code assumes that the idle loop never does anything that might
  670. * result in unbalanced calls to irq_enter() and irq_exit(). If your
  671. * architecture violates this assumption, RCU will give you what you
  672. * deserve, good and hard. But very infrequently and irreproducibly.
  673. *
  674. * Use things like work queues to work around this limitation.
  675. *
  676. * You have been warned.
  677. */
  678. void rcu_irq_exit(void)
  679. {
  680. long long oldval;
  681. struct rcu_dynticks *rdtp;
  682. RCU_LOCKDEP_WARN(!irqs_disabled(), "rcu_irq_exit() invoked with irqs enabled!!!");
  683. rdtp = this_cpu_ptr(&rcu_dynticks);
  684. oldval = rdtp->dynticks_nesting;
  685. rdtp->dynticks_nesting--;
  686. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  687. rdtp->dynticks_nesting < 0);
  688. if (rdtp->dynticks_nesting)
  689. trace_rcu_dyntick(TPS("--="), oldval, rdtp->dynticks_nesting);
  690. else
  691. rcu_eqs_enter_common(oldval, true);
  692. rcu_sysidle_enter(1);
  693. }
  694. /*
  695. * Wrapper for rcu_irq_exit() where interrupts are enabled.
  696. */
  697. void rcu_irq_exit_irqson(void)
  698. {
  699. unsigned long flags;
  700. local_irq_save(flags);
  701. rcu_irq_exit();
  702. local_irq_restore(flags);
  703. }
  704. /*
  705. * rcu_eqs_exit_common - current CPU moving away from extended quiescent state
  706. *
  707. * If the new value of the ->dynticks_nesting counter was previously zero,
  708. * we really have exited idle, and must do the appropriate accounting.
  709. * The caller must have disabled interrupts.
  710. */
  711. static void rcu_eqs_exit_common(long long oldval, int user)
  712. {
  713. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  714. rcu_dynticks_task_exit();
  715. smp_mb__before_atomic(); /* Force ordering w/previous sojourn. */
  716. atomic_inc(&rdtp->dynticks);
  717. /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
  718. smp_mb__after_atomic(); /* See above. */
  719. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  720. !(atomic_read(&rdtp->dynticks) & 0x1));
  721. rcu_cleanup_after_idle();
  722. trace_rcu_dyntick(TPS("End"), oldval, rdtp->dynticks_nesting);
  723. if (IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  724. !user && !is_idle_task(current)) {
  725. struct task_struct *idle __maybe_unused =
  726. idle_task(smp_processor_id());
  727. trace_rcu_dyntick(TPS("Error on exit: not idle task"),
  728. oldval, rdtp->dynticks_nesting);
  729. rcu_ftrace_dump(DUMP_ORIG);
  730. WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
  731. current->pid, current->comm,
  732. idle->pid, idle->comm); /* must be idle task! */
  733. }
  734. }
  735. /*
  736. * Exit an RCU extended quiescent state, which can be either the
  737. * idle loop or adaptive-tickless usermode execution.
  738. */
  739. static void rcu_eqs_exit(bool user)
  740. {
  741. struct rcu_dynticks *rdtp;
  742. long long oldval;
  743. rdtp = this_cpu_ptr(&rcu_dynticks);
  744. oldval = rdtp->dynticks_nesting;
  745. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && oldval < 0);
  746. if (oldval & DYNTICK_TASK_NEST_MASK) {
  747. rdtp->dynticks_nesting += DYNTICK_TASK_NEST_VALUE;
  748. } else {
  749. rdtp->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE;
  750. rcu_eqs_exit_common(oldval, user);
  751. }
  752. }
  753. /**
  754. * rcu_idle_exit - inform RCU that current CPU is leaving idle
  755. *
  756. * Exit idle mode, in other words, -enter- the mode in which RCU
  757. * read-side critical sections can occur.
  758. *
  759. * We crowbar the ->dynticks_nesting field to DYNTICK_TASK_NEST to
  760. * allow for the possibility of usermode upcalls messing up our count
  761. * of interrupt nesting level during the busy period that is just
  762. * now starting.
  763. */
  764. void rcu_idle_exit(void)
  765. {
  766. unsigned long flags;
  767. local_irq_save(flags);
  768. rcu_eqs_exit(false);
  769. rcu_sysidle_exit(0);
  770. local_irq_restore(flags);
  771. }
  772. EXPORT_SYMBOL_GPL(rcu_idle_exit);
  773. #ifdef CONFIG_NO_HZ_FULL
  774. /**
  775. * rcu_user_exit - inform RCU that we are exiting userspace.
  776. *
  777. * Exit RCU idle mode while entering the kernel because it can
  778. * run a RCU read side critical section anytime.
  779. */
  780. void rcu_user_exit(void)
  781. {
  782. rcu_eqs_exit(1);
  783. }
  784. #endif /* CONFIG_NO_HZ_FULL */
  785. /**
  786. * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
  787. *
  788. * Enter an interrupt handler, which might possibly result in exiting
  789. * idle mode, in other words, entering the mode in which read-side critical
  790. * sections can occur. The caller must have disabled interrupts.
  791. *
  792. * Note that the Linux kernel is fully capable of entering an interrupt
  793. * handler that it never exits, for example when doing upcalls to
  794. * user mode! This code assumes that the idle loop never does upcalls to
  795. * user mode. If your architecture does do upcalls from the idle loop (or
  796. * does anything else that results in unbalanced calls to the irq_enter()
  797. * and irq_exit() functions), RCU will give you what you deserve, good
  798. * and hard. But very infrequently and irreproducibly.
  799. *
  800. * Use things like work queues to work around this limitation.
  801. *
  802. * You have been warned.
  803. */
  804. void rcu_irq_enter(void)
  805. {
  806. struct rcu_dynticks *rdtp;
  807. long long oldval;
  808. RCU_LOCKDEP_WARN(!irqs_disabled(), "rcu_irq_enter() invoked with irqs enabled!!!");
  809. rdtp = this_cpu_ptr(&rcu_dynticks);
  810. oldval = rdtp->dynticks_nesting;
  811. rdtp->dynticks_nesting++;
  812. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  813. rdtp->dynticks_nesting == 0);
  814. if (oldval)
  815. trace_rcu_dyntick(TPS("++="), oldval, rdtp->dynticks_nesting);
  816. else
  817. rcu_eqs_exit_common(oldval, true);
  818. rcu_sysidle_exit(1);
  819. }
  820. /*
  821. * Wrapper for rcu_irq_enter() where interrupts are enabled.
  822. */
  823. void rcu_irq_enter_irqson(void)
  824. {
  825. unsigned long flags;
  826. local_irq_save(flags);
  827. rcu_irq_enter();
  828. local_irq_restore(flags);
  829. }
  830. /**
  831. * rcu_nmi_enter - inform RCU of entry to NMI context
  832. *
  833. * If the CPU was idle from RCU's viewpoint, update rdtp->dynticks and
  834. * rdtp->dynticks_nmi_nesting to let the RCU grace-period handling know
  835. * that the CPU is active. This implementation permits nested NMIs, as
  836. * long as the nesting level does not overflow an int. (You will probably
  837. * run out of stack space first.)
  838. */
  839. void rcu_nmi_enter(void)
  840. {
  841. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  842. int incby = 2;
  843. /* Complain about underflow. */
  844. WARN_ON_ONCE(rdtp->dynticks_nmi_nesting < 0);
  845. /*
  846. * If idle from RCU viewpoint, atomically increment ->dynticks
  847. * to mark non-idle and increment ->dynticks_nmi_nesting by one.
  848. * Otherwise, increment ->dynticks_nmi_nesting by two. This means
  849. * if ->dynticks_nmi_nesting is equal to one, we are guaranteed
  850. * to be in the outermost NMI handler that interrupted an RCU-idle
  851. * period (observation due to Andy Lutomirski).
  852. */
  853. if (!(atomic_read(&rdtp->dynticks) & 0x1)) {
  854. smp_mb__before_atomic(); /* Force delay from prior write. */
  855. atomic_inc(&rdtp->dynticks);
  856. /* atomic_inc() before later RCU read-side crit sects */
  857. smp_mb__after_atomic(); /* See above. */
  858. WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
  859. incby = 1;
  860. }
  861. rdtp->dynticks_nmi_nesting += incby;
  862. barrier();
  863. }
  864. /**
  865. * rcu_nmi_exit - inform RCU of exit from NMI context
  866. *
  867. * If we are returning from the outermost NMI handler that interrupted an
  868. * RCU-idle period, update rdtp->dynticks and rdtp->dynticks_nmi_nesting
  869. * to let the RCU grace-period handling know that the CPU is back to
  870. * being RCU-idle.
  871. */
  872. void rcu_nmi_exit(void)
  873. {
  874. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  875. /*
  876. * Check for ->dynticks_nmi_nesting underflow and bad ->dynticks.
  877. * (We are exiting an NMI handler, so RCU better be paying attention
  878. * to us!)
  879. */
  880. WARN_ON_ONCE(rdtp->dynticks_nmi_nesting <= 0);
  881. WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
  882. /*
  883. * If the nesting level is not 1, the CPU wasn't RCU-idle, so
  884. * leave it in non-RCU-idle state.
  885. */
  886. if (rdtp->dynticks_nmi_nesting != 1) {
  887. rdtp->dynticks_nmi_nesting -= 2;
  888. return;
  889. }
  890. /* This NMI interrupted an RCU-idle CPU, restore RCU-idleness. */
  891. rdtp->dynticks_nmi_nesting = 0;
  892. /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
  893. smp_mb__before_atomic(); /* See above. */
  894. atomic_inc(&rdtp->dynticks);
  895. smp_mb__after_atomic(); /* Force delay to next write. */
  896. WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
  897. }
  898. /**
  899. * __rcu_is_watching - are RCU read-side critical sections safe?
  900. *
  901. * Return true if RCU is watching the running CPU, which means that
  902. * this CPU can safely enter RCU read-side critical sections. Unlike
  903. * rcu_is_watching(), the caller of __rcu_is_watching() must have at
  904. * least disabled preemption.
  905. */
  906. bool notrace __rcu_is_watching(void)
  907. {
  908. return atomic_read(this_cpu_ptr(&rcu_dynticks.dynticks)) & 0x1;
  909. }
  910. /**
  911. * rcu_is_watching - see if RCU thinks that the current CPU is idle
  912. *
  913. * If the current CPU is in its idle loop and is neither in an interrupt
  914. * or NMI handler, return true.
  915. */
  916. bool notrace rcu_is_watching(void)
  917. {
  918. bool ret;
  919. preempt_disable_notrace();
  920. ret = __rcu_is_watching();
  921. preempt_enable_notrace();
  922. return ret;
  923. }
  924. EXPORT_SYMBOL_GPL(rcu_is_watching);
  925. #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
  926. /*
  927. * Is the current CPU online? Disable preemption to avoid false positives
  928. * that could otherwise happen due to the current CPU number being sampled,
  929. * this task being preempted, its old CPU being taken offline, resuming
  930. * on some other CPU, then determining that its old CPU is now offline.
  931. * It is OK to use RCU on an offline processor during initial boot, hence
  932. * the check for rcu_scheduler_fully_active. Note also that it is OK
  933. * for a CPU coming online to use RCU for one jiffy prior to marking itself
  934. * online in the cpu_online_mask. Similarly, it is OK for a CPU going
  935. * offline to continue to use RCU for one jiffy after marking itself
  936. * offline in the cpu_online_mask. This leniency is necessary given the
  937. * non-atomic nature of the online and offline processing, for example,
  938. * the fact that a CPU enters the scheduler after completing the CPU_DYING
  939. * notifiers.
  940. *
  941. * This is also why RCU internally marks CPUs online during the
  942. * CPU_UP_PREPARE phase and offline during the CPU_DEAD phase.
  943. *
  944. * Disable checking if in an NMI handler because we cannot safely report
  945. * errors from NMI handlers anyway.
  946. */
  947. bool rcu_lockdep_current_cpu_online(void)
  948. {
  949. struct rcu_data *rdp;
  950. struct rcu_node *rnp;
  951. bool ret;
  952. if (in_nmi())
  953. return true;
  954. preempt_disable();
  955. rdp = this_cpu_ptr(&rcu_sched_data);
  956. rnp = rdp->mynode;
  957. ret = (rdp->grpmask & rcu_rnp_online_cpus(rnp)) ||
  958. !rcu_scheduler_fully_active;
  959. preempt_enable();
  960. return ret;
  961. }
  962. EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
  963. #endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
  964. /**
  965. * rcu_is_cpu_rrupt_from_idle - see if idle or immediately interrupted from idle
  966. *
  967. * If the current CPU is idle or running at a first-level (not nested)
  968. * interrupt from idle, return true. The caller must have at least
  969. * disabled preemption.
  970. */
  971. static int rcu_is_cpu_rrupt_from_idle(void)
  972. {
  973. return __this_cpu_read(rcu_dynticks.dynticks_nesting) <= 1;
  974. }
  975. /*
  976. * Snapshot the specified CPU's dynticks counter so that we can later
  977. * credit them with an implicit quiescent state. Return 1 if this CPU
  978. * is in dynticks idle mode, which is an extended quiescent state.
  979. */
  980. static int dyntick_save_progress_counter(struct rcu_data *rdp,
  981. bool *isidle, unsigned long *maxj)
  982. {
  983. rdp->dynticks_snap = atomic_add_return(0, &rdp->dynticks->dynticks);
  984. rcu_sysidle_check_cpu(rdp, isidle, maxj);
  985. if ((rdp->dynticks_snap & 0x1) == 0) {
  986. trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("dti"));
  987. if (ULONG_CMP_LT(READ_ONCE(rdp->gpnum) + ULONG_MAX / 4,
  988. rdp->mynode->gpnum))
  989. WRITE_ONCE(rdp->gpwrap, true);
  990. return 1;
  991. }
  992. return 0;
  993. }
  994. /*
  995. * Return true if the specified CPU has passed through a quiescent
  996. * state by virtue of being in or having passed through an dynticks
  997. * idle state since the last call to dyntick_save_progress_counter()
  998. * for this same CPU, or by virtue of having been offline.
  999. */
  1000. static int rcu_implicit_dynticks_qs(struct rcu_data *rdp,
  1001. bool *isidle, unsigned long *maxj)
  1002. {
  1003. unsigned int curr;
  1004. int *rcrmp;
  1005. unsigned int snap;
  1006. curr = (unsigned int)atomic_add_return(0, &rdp->dynticks->dynticks);
  1007. snap = (unsigned int)rdp->dynticks_snap;
  1008. /*
  1009. * If the CPU passed through or entered a dynticks idle phase with
  1010. * no active irq/NMI handlers, then we can safely pretend that the CPU
  1011. * already acknowledged the request to pass through a quiescent
  1012. * state. Either way, that CPU cannot possibly be in an RCU
  1013. * read-side critical section that started before the beginning
  1014. * of the current RCU grace period.
  1015. */
  1016. if ((curr & 0x1) == 0 || UINT_CMP_GE(curr, snap + 2)) {
  1017. trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("dti"));
  1018. rdp->dynticks_fqs++;
  1019. return 1;
  1020. }
  1021. /*
  1022. * Check for the CPU being offline, but only if the grace period
  1023. * is old enough. We don't need to worry about the CPU changing
  1024. * state: If we see it offline even once, it has been through a
  1025. * quiescent state.
  1026. *
  1027. * The reason for insisting that the grace period be at least
  1028. * one jiffy old is that CPUs that are not quite online and that
  1029. * have just gone offline can still execute RCU read-side critical
  1030. * sections.
  1031. */
  1032. if (ULONG_CMP_GE(rdp->rsp->gp_start + 2, jiffies))
  1033. return 0; /* Grace period is not old enough. */
  1034. barrier();
  1035. if (cpu_is_offline(rdp->cpu)) {
  1036. trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("ofl"));
  1037. rdp->offline_fqs++;
  1038. return 1;
  1039. }
  1040. /*
  1041. * A CPU running for an extended time within the kernel can
  1042. * delay RCU grace periods. When the CPU is in NO_HZ_FULL mode,
  1043. * even context-switching back and forth between a pair of
  1044. * in-kernel CPU-bound tasks cannot advance grace periods.
  1045. * So if the grace period is old enough, make the CPU pay attention.
  1046. * Note that the unsynchronized assignments to the per-CPU
  1047. * rcu_sched_qs_mask variable are safe. Yes, setting of
  1048. * bits can be lost, but they will be set again on the next
  1049. * force-quiescent-state pass. So lost bit sets do not result
  1050. * in incorrect behavior, merely in a grace period lasting
  1051. * a few jiffies longer than it might otherwise. Because
  1052. * there are at most four threads involved, and because the
  1053. * updates are only once every few jiffies, the probability of
  1054. * lossage (and thus of slight grace-period extension) is
  1055. * quite low.
  1056. *
  1057. * Note that if the jiffies_till_sched_qs boot/sysfs parameter
  1058. * is set too high, we override with half of the RCU CPU stall
  1059. * warning delay.
  1060. */
  1061. rcrmp = &per_cpu(rcu_sched_qs_mask, rdp->cpu);
  1062. if (ULONG_CMP_GE(jiffies,
  1063. rdp->rsp->gp_start + jiffies_till_sched_qs) ||
  1064. ULONG_CMP_GE(jiffies, rdp->rsp->jiffies_resched)) {
  1065. if (!(READ_ONCE(*rcrmp) & rdp->rsp->flavor_mask)) {
  1066. WRITE_ONCE(rdp->cond_resched_completed,
  1067. READ_ONCE(rdp->mynode->completed));
  1068. smp_mb(); /* ->cond_resched_completed before *rcrmp. */
  1069. WRITE_ONCE(*rcrmp,
  1070. READ_ONCE(*rcrmp) + rdp->rsp->flavor_mask);
  1071. }
  1072. rdp->rsp->jiffies_resched += 5; /* Re-enable beating. */
  1073. }
  1074. /* And if it has been a really long time, kick the CPU as well. */
  1075. if (ULONG_CMP_GE(jiffies,
  1076. rdp->rsp->gp_start + 2 * jiffies_till_sched_qs) ||
  1077. ULONG_CMP_GE(jiffies, rdp->rsp->gp_start + jiffies_till_sched_qs))
  1078. resched_cpu(rdp->cpu); /* Force CPU into scheduler. */
  1079. return 0;
  1080. }
  1081. static void record_gp_stall_check_time(struct rcu_state *rsp)
  1082. {
  1083. unsigned long j = jiffies;
  1084. unsigned long j1;
  1085. rsp->gp_start = j;
  1086. smp_wmb(); /* Record start time before stall time. */
  1087. j1 = rcu_jiffies_till_stall_check();
  1088. WRITE_ONCE(rsp->jiffies_stall, j + j1);
  1089. rsp->jiffies_resched = j + j1 / 2;
  1090. rsp->n_force_qs_gpstart = READ_ONCE(rsp->n_force_qs);
  1091. }
  1092. /*
  1093. * Convert a ->gp_state value to a character string.
  1094. */
  1095. static const char *gp_state_getname(short gs)
  1096. {
  1097. if (gs < 0 || gs >= ARRAY_SIZE(gp_state_names))
  1098. return "???";
  1099. return gp_state_names[gs];
  1100. }
  1101. /*
  1102. * Complain about starvation of grace-period kthread.
  1103. */
  1104. static void rcu_check_gp_kthread_starvation(struct rcu_state *rsp)
  1105. {
  1106. unsigned long gpa;
  1107. unsigned long j;
  1108. j = jiffies;
  1109. gpa = READ_ONCE(rsp->gp_activity);
  1110. if (j - gpa > 2 * HZ) {
  1111. pr_err("%s kthread starved for %ld jiffies! g%lu c%lu f%#x %s(%d) ->state=%#lx\n",
  1112. rsp->name, j - gpa,
  1113. rsp->gpnum, rsp->completed,
  1114. rsp->gp_flags,
  1115. gp_state_getname(rsp->gp_state), rsp->gp_state,
  1116. rsp->gp_kthread ? rsp->gp_kthread->state : ~0);
  1117. if (rsp->gp_kthread) {
  1118. sched_show_task(rsp->gp_kthread);
  1119. wake_up_process(rsp->gp_kthread);
  1120. }
  1121. }
  1122. }
  1123. /*
  1124. * Dump stacks of all tasks running on stalled CPUs.
  1125. */
  1126. static void rcu_dump_cpu_stacks(struct rcu_state *rsp)
  1127. {
  1128. int cpu;
  1129. unsigned long flags;
  1130. struct rcu_node *rnp;
  1131. rcu_for_each_leaf_node(rsp, rnp) {
  1132. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1133. if (rnp->qsmask != 0) {
  1134. for (cpu = 0; cpu <= rnp->grphi - rnp->grplo; cpu++)
  1135. if (rnp->qsmask & (1UL << cpu))
  1136. dump_cpu_task(rnp->grplo + cpu);
  1137. }
  1138. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1139. }
  1140. }
  1141. /*
  1142. * If too much time has passed in the current grace period, and if
  1143. * so configured, go kick the relevant kthreads.
  1144. */
  1145. static void rcu_stall_kick_kthreads(struct rcu_state *rsp)
  1146. {
  1147. unsigned long j;
  1148. if (!rcu_kick_kthreads)
  1149. return;
  1150. j = READ_ONCE(rsp->jiffies_kick_kthreads);
  1151. if (time_after(jiffies, j) && rsp->gp_kthread) {
  1152. WARN_ONCE(1, "Kicking %s grace-period kthread\n", rsp->name);
  1153. wake_up_process(rsp->gp_kthread);
  1154. WRITE_ONCE(rsp->jiffies_kick_kthreads, j + HZ);
  1155. }
  1156. }
  1157. static void print_other_cpu_stall(struct rcu_state *rsp, unsigned long gpnum)
  1158. {
  1159. int cpu;
  1160. long delta;
  1161. unsigned long flags;
  1162. unsigned long gpa;
  1163. unsigned long j;
  1164. int ndetected = 0;
  1165. struct rcu_node *rnp = rcu_get_root(rsp);
  1166. long totqlen = 0;
  1167. /* Kick and suppress, if so configured. */
  1168. rcu_stall_kick_kthreads(rsp);
  1169. if (rcu_cpu_stall_suppress)
  1170. return;
  1171. /* Only let one CPU complain about others per time interval. */
  1172. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1173. delta = jiffies - READ_ONCE(rsp->jiffies_stall);
  1174. if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) {
  1175. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1176. return;
  1177. }
  1178. WRITE_ONCE(rsp->jiffies_stall,
  1179. jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
  1180. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1181. /*
  1182. * OK, time to rat on our buddy...
  1183. * See Documentation/RCU/stallwarn.txt for info on how to debug
  1184. * RCU CPU stall warnings.
  1185. */
  1186. pr_err("INFO: %s detected stalls on CPUs/tasks:",
  1187. rsp->name);
  1188. print_cpu_stall_info_begin();
  1189. rcu_for_each_leaf_node(rsp, rnp) {
  1190. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1191. ndetected += rcu_print_task_stall(rnp);
  1192. if (rnp->qsmask != 0) {
  1193. for (cpu = 0; cpu <= rnp->grphi - rnp->grplo; cpu++)
  1194. if (rnp->qsmask & (1UL << cpu)) {
  1195. print_cpu_stall_info(rsp,
  1196. rnp->grplo + cpu);
  1197. ndetected++;
  1198. }
  1199. }
  1200. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1201. }
  1202. print_cpu_stall_info_end();
  1203. for_each_possible_cpu(cpu)
  1204. totqlen += per_cpu_ptr(rsp->rda, cpu)->qlen;
  1205. pr_cont("(detected by %d, t=%ld jiffies, g=%ld, c=%ld, q=%lu)\n",
  1206. smp_processor_id(), (long)(jiffies - rsp->gp_start),
  1207. (long)rsp->gpnum, (long)rsp->completed, totqlen);
  1208. if (ndetected) {
  1209. rcu_dump_cpu_stacks(rsp);
  1210. } else {
  1211. if (READ_ONCE(rsp->gpnum) != gpnum ||
  1212. READ_ONCE(rsp->completed) == gpnum) {
  1213. pr_err("INFO: Stall ended before state dump start\n");
  1214. } else {
  1215. j = jiffies;
  1216. gpa = READ_ONCE(rsp->gp_activity);
  1217. pr_err("All QSes seen, last %s kthread activity %ld (%ld-%ld), jiffies_till_next_fqs=%ld, root ->qsmask %#lx\n",
  1218. rsp->name, j - gpa, j, gpa,
  1219. jiffies_till_next_fqs,
  1220. rcu_get_root(rsp)->qsmask);
  1221. /* In this case, the current CPU might be at fault. */
  1222. sched_show_task(current);
  1223. }
  1224. }
  1225. /* Complain about tasks blocking the grace period. */
  1226. rcu_print_detail_task_stall(rsp);
  1227. rcu_check_gp_kthread_starvation(rsp);
  1228. force_quiescent_state(rsp); /* Kick them all. */
  1229. }
  1230. static void print_cpu_stall(struct rcu_state *rsp)
  1231. {
  1232. int cpu;
  1233. unsigned long flags;
  1234. struct rcu_node *rnp = rcu_get_root(rsp);
  1235. long totqlen = 0;
  1236. /* Kick and suppress, if so configured. */
  1237. rcu_stall_kick_kthreads(rsp);
  1238. if (rcu_cpu_stall_suppress)
  1239. return;
  1240. /*
  1241. * OK, time to rat on ourselves...
  1242. * See Documentation/RCU/stallwarn.txt for info on how to debug
  1243. * RCU CPU stall warnings.
  1244. */
  1245. pr_err("INFO: %s self-detected stall on CPU", rsp->name);
  1246. print_cpu_stall_info_begin();
  1247. print_cpu_stall_info(rsp, smp_processor_id());
  1248. print_cpu_stall_info_end();
  1249. for_each_possible_cpu(cpu)
  1250. totqlen += per_cpu_ptr(rsp->rda, cpu)->qlen;
  1251. pr_cont(" (t=%lu jiffies g=%ld c=%ld q=%lu)\n",
  1252. jiffies - rsp->gp_start,
  1253. (long)rsp->gpnum, (long)rsp->completed, totqlen);
  1254. rcu_check_gp_kthread_starvation(rsp);
  1255. rcu_dump_cpu_stacks(rsp);
  1256. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1257. if (ULONG_CMP_GE(jiffies, READ_ONCE(rsp->jiffies_stall)))
  1258. WRITE_ONCE(rsp->jiffies_stall,
  1259. jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
  1260. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1261. /*
  1262. * Attempt to revive the RCU machinery by forcing a context switch.
  1263. *
  1264. * A context switch would normally allow the RCU state machine to make
  1265. * progress and it could be we're stuck in kernel space without context
  1266. * switches for an entirely unreasonable amount of time.
  1267. */
  1268. resched_cpu(smp_processor_id());
  1269. }
  1270. static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
  1271. {
  1272. unsigned long completed;
  1273. unsigned long gpnum;
  1274. unsigned long gps;
  1275. unsigned long j;
  1276. unsigned long js;
  1277. struct rcu_node *rnp;
  1278. if ((rcu_cpu_stall_suppress && !rcu_kick_kthreads) ||
  1279. !rcu_gp_in_progress(rsp))
  1280. return;
  1281. rcu_stall_kick_kthreads(rsp);
  1282. j = jiffies;
  1283. /*
  1284. * Lots of memory barriers to reject false positives.
  1285. *
  1286. * The idea is to pick up rsp->gpnum, then rsp->jiffies_stall,
  1287. * then rsp->gp_start, and finally rsp->completed. These values
  1288. * are updated in the opposite order with memory barriers (or
  1289. * equivalent) during grace-period initialization and cleanup.
  1290. * Now, a false positive can occur if we get an new value of
  1291. * rsp->gp_start and a old value of rsp->jiffies_stall. But given
  1292. * the memory barriers, the only way that this can happen is if one
  1293. * grace period ends and another starts between these two fetches.
  1294. * Detect this by comparing rsp->completed with the previous fetch
  1295. * from rsp->gpnum.
  1296. *
  1297. * Given this check, comparisons of jiffies, rsp->jiffies_stall,
  1298. * and rsp->gp_start suffice to forestall false positives.
  1299. */
  1300. gpnum = READ_ONCE(rsp->gpnum);
  1301. smp_rmb(); /* Pick up ->gpnum first... */
  1302. js = READ_ONCE(rsp->jiffies_stall);
  1303. smp_rmb(); /* ...then ->jiffies_stall before the rest... */
  1304. gps = READ_ONCE(rsp->gp_start);
  1305. smp_rmb(); /* ...and finally ->gp_start before ->completed. */
  1306. completed = READ_ONCE(rsp->completed);
  1307. if (ULONG_CMP_GE(completed, gpnum) ||
  1308. ULONG_CMP_LT(j, js) ||
  1309. ULONG_CMP_GE(gps, js))
  1310. return; /* No stall or GP completed since entering function. */
  1311. rnp = rdp->mynode;
  1312. if (rcu_gp_in_progress(rsp) &&
  1313. (READ_ONCE(rnp->qsmask) & rdp->grpmask)) {
  1314. /* We haven't checked in, so go dump stack. */
  1315. print_cpu_stall(rsp);
  1316. } else if (rcu_gp_in_progress(rsp) &&
  1317. ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY)) {
  1318. /* They had a few time units to dump stack, so complain. */
  1319. print_other_cpu_stall(rsp, gpnum);
  1320. }
  1321. }
  1322. /**
  1323. * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
  1324. *
  1325. * Set the stall-warning timeout way off into the future, thus preventing
  1326. * any RCU CPU stall-warning messages from appearing in the current set of
  1327. * RCU grace periods.
  1328. *
  1329. * The caller must disable hard irqs.
  1330. */
  1331. void rcu_cpu_stall_reset(void)
  1332. {
  1333. struct rcu_state *rsp;
  1334. for_each_rcu_flavor(rsp)
  1335. WRITE_ONCE(rsp->jiffies_stall, jiffies + ULONG_MAX / 2);
  1336. }
  1337. /*
  1338. * Initialize the specified rcu_data structure's default callback list
  1339. * to empty. The default callback list is the one that is not used by
  1340. * no-callbacks CPUs.
  1341. */
  1342. static void init_default_callback_list(struct rcu_data *rdp)
  1343. {
  1344. int i;
  1345. rdp->nxtlist = NULL;
  1346. for (i = 0; i < RCU_NEXT_SIZE; i++)
  1347. rdp->nxttail[i] = &rdp->nxtlist;
  1348. }
  1349. /*
  1350. * Initialize the specified rcu_data structure's callback list to empty.
  1351. */
  1352. static void init_callback_list(struct rcu_data *rdp)
  1353. {
  1354. if (init_nocb_callback_list(rdp))
  1355. return;
  1356. init_default_callback_list(rdp);
  1357. }
  1358. /*
  1359. * Determine the value that ->completed will have at the end of the
  1360. * next subsequent grace period. This is used to tag callbacks so that
  1361. * a CPU can invoke callbacks in a timely fashion even if that CPU has
  1362. * been dyntick-idle for an extended period with callbacks under the
  1363. * influence of RCU_FAST_NO_HZ.
  1364. *
  1365. * The caller must hold rnp->lock with interrupts disabled.
  1366. */
  1367. static unsigned long rcu_cbs_completed(struct rcu_state *rsp,
  1368. struct rcu_node *rnp)
  1369. {
  1370. /*
  1371. * If RCU is idle, we just wait for the next grace period.
  1372. * But we can only be sure that RCU is idle if we are looking
  1373. * at the root rcu_node structure -- otherwise, a new grace
  1374. * period might have started, but just not yet gotten around
  1375. * to initializing the current non-root rcu_node structure.
  1376. */
  1377. if (rcu_get_root(rsp) == rnp && rnp->gpnum == rnp->completed)
  1378. return rnp->completed + 1;
  1379. /*
  1380. * Otherwise, wait for a possible partial grace period and
  1381. * then the subsequent full grace period.
  1382. */
  1383. return rnp->completed + 2;
  1384. }
  1385. /*
  1386. * Trace-event helper function for rcu_start_future_gp() and
  1387. * rcu_nocb_wait_gp().
  1388. */
  1389. static void trace_rcu_future_gp(struct rcu_node *rnp, struct rcu_data *rdp,
  1390. unsigned long c, const char *s)
  1391. {
  1392. trace_rcu_future_grace_period(rdp->rsp->name, rnp->gpnum,
  1393. rnp->completed, c, rnp->level,
  1394. rnp->grplo, rnp->grphi, s);
  1395. }
  1396. /*
  1397. * Start some future grace period, as needed to handle newly arrived
  1398. * callbacks. The required future grace periods are recorded in each
  1399. * rcu_node structure's ->need_future_gp field. Returns true if there
  1400. * is reason to awaken the grace-period kthread.
  1401. *
  1402. * The caller must hold the specified rcu_node structure's ->lock.
  1403. */
  1404. static bool __maybe_unused
  1405. rcu_start_future_gp(struct rcu_node *rnp, struct rcu_data *rdp,
  1406. unsigned long *c_out)
  1407. {
  1408. unsigned long c;
  1409. int i;
  1410. bool ret = false;
  1411. struct rcu_node *rnp_root = rcu_get_root(rdp->rsp);
  1412. /*
  1413. * Pick up grace-period number for new callbacks. If this
  1414. * grace period is already marked as needed, return to the caller.
  1415. */
  1416. c = rcu_cbs_completed(rdp->rsp, rnp);
  1417. trace_rcu_future_gp(rnp, rdp, c, TPS("Startleaf"));
  1418. if (rnp->need_future_gp[c & 0x1]) {
  1419. trace_rcu_future_gp(rnp, rdp, c, TPS("Prestartleaf"));
  1420. goto out;
  1421. }
  1422. /*
  1423. * If either this rcu_node structure or the root rcu_node structure
  1424. * believe that a grace period is in progress, then we must wait
  1425. * for the one following, which is in "c". Because our request
  1426. * will be noticed at the end of the current grace period, we don't
  1427. * need to explicitly start one. We only do the lockless check
  1428. * of rnp_root's fields if the current rcu_node structure thinks
  1429. * there is no grace period in flight, and because we hold rnp->lock,
  1430. * the only possible change is when rnp_root's two fields are
  1431. * equal, in which case rnp_root->gpnum might be concurrently
  1432. * incremented. But that is OK, as it will just result in our
  1433. * doing some extra useless work.
  1434. */
  1435. if (rnp->gpnum != rnp->completed ||
  1436. READ_ONCE(rnp_root->gpnum) != READ_ONCE(rnp_root->completed)) {
  1437. rnp->need_future_gp[c & 0x1]++;
  1438. trace_rcu_future_gp(rnp, rdp, c, TPS("Startedleaf"));
  1439. goto out;
  1440. }
  1441. /*
  1442. * There might be no grace period in progress. If we don't already
  1443. * hold it, acquire the root rcu_node structure's lock in order to
  1444. * start one (if needed).
  1445. */
  1446. if (rnp != rnp_root)
  1447. raw_spin_lock_rcu_node(rnp_root);
  1448. /*
  1449. * Get a new grace-period number. If there really is no grace
  1450. * period in progress, it will be smaller than the one we obtained
  1451. * earlier. Adjust callbacks as needed. Note that even no-CBs
  1452. * CPUs have a ->nxtcompleted[] array, so no no-CBs checks needed.
  1453. */
  1454. c = rcu_cbs_completed(rdp->rsp, rnp_root);
  1455. for (i = RCU_DONE_TAIL; i < RCU_NEXT_TAIL; i++)
  1456. if (ULONG_CMP_LT(c, rdp->nxtcompleted[i]))
  1457. rdp->nxtcompleted[i] = c;
  1458. /*
  1459. * If the needed for the required grace period is already
  1460. * recorded, trace and leave.
  1461. */
  1462. if (rnp_root->need_future_gp[c & 0x1]) {
  1463. trace_rcu_future_gp(rnp, rdp, c, TPS("Prestartedroot"));
  1464. goto unlock_out;
  1465. }
  1466. /* Record the need for the future grace period. */
  1467. rnp_root->need_future_gp[c & 0x1]++;
  1468. /* If a grace period is not already in progress, start one. */
  1469. if (rnp_root->gpnum != rnp_root->completed) {
  1470. trace_rcu_future_gp(rnp, rdp, c, TPS("Startedleafroot"));
  1471. } else {
  1472. trace_rcu_future_gp(rnp, rdp, c, TPS("Startedroot"));
  1473. ret = rcu_start_gp_advanced(rdp->rsp, rnp_root, rdp);
  1474. }
  1475. unlock_out:
  1476. if (rnp != rnp_root)
  1477. raw_spin_unlock_rcu_node(rnp_root);
  1478. out:
  1479. if (c_out != NULL)
  1480. *c_out = c;
  1481. return ret;
  1482. }
  1483. /*
  1484. * Clean up any old requests for the just-ended grace period. Also return
  1485. * whether any additional grace periods have been requested. Also invoke
  1486. * rcu_nocb_gp_cleanup() in order to wake up any no-callbacks kthreads
  1487. * waiting for this grace period to complete.
  1488. */
  1489. static int rcu_future_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  1490. {
  1491. int c = rnp->completed;
  1492. int needmore;
  1493. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  1494. rnp->need_future_gp[c & 0x1] = 0;
  1495. needmore = rnp->need_future_gp[(c + 1) & 0x1];
  1496. trace_rcu_future_gp(rnp, rdp, c,
  1497. needmore ? TPS("CleanupMore") : TPS("Cleanup"));
  1498. return needmore;
  1499. }
  1500. /*
  1501. * Awaken the grace-period kthread for the specified flavor of RCU.
  1502. * Don't do a self-awaken, and don't bother awakening when there is
  1503. * nothing for the grace-period kthread to do (as in several CPUs
  1504. * raced to awaken, and we lost), and finally don't try to awaken
  1505. * a kthread that has not yet been created.
  1506. */
  1507. static void rcu_gp_kthread_wake(struct rcu_state *rsp)
  1508. {
  1509. if (current == rsp->gp_kthread ||
  1510. !READ_ONCE(rsp->gp_flags) ||
  1511. !rsp->gp_kthread)
  1512. return;
  1513. swake_up(&rsp->gp_wq);
  1514. }
  1515. /*
  1516. * If there is room, assign a ->completed number to any callbacks on
  1517. * this CPU that have not already been assigned. Also accelerate any
  1518. * callbacks that were previously assigned a ->completed number that has
  1519. * since proven to be too conservative, which can happen if callbacks get
  1520. * assigned a ->completed number while RCU is idle, but with reference to
  1521. * a non-root rcu_node structure. This function is idempotent, so it does
  1522. * not hurt to call it repeatedly. Returns an flag saying that we should
  1523. * awaken the RCU grace-period kthread.
  1524. *
  1525. * The caller must hold rnp->lock with interrupts disabled.
  1526. */
  1527. static bool rcu_accelerate_cbs(struct rcu_state *rsp, struct rcu_node *rnp,
  1528. struct rcu_data *rdp)
  1529. {
  1530. unsigned long c;
  1531. int i;
  1532. bool ret;
  1533. /* If the CPU has no callbacks, nothing to do. */
  1534. if (!rdp->nxttail[RCU_NEXT_TAIL] || !*rdp->nxttail[RCU_DONE_TAIL])
  1535. return false;
  1536. /*
  1537. * Starting from the sublist containing the callbacks most
  1538. * recently assigned a ->completed number and working down, find the
  1539. * first sublist that is not assignable to an upcoming grace period.
  1540. * Such a sublist has something in it (first two tests) and has
  1541. * a ->completed number assigned that will complete sooner than
  1542. * the ->completed number for newly arrived callbacks (last test).
  1543. *
  1544. * The key point is that any later sublist can be assigned the
  1545. * same ->completed number as the newly arrived callbacks, which
  1546. * means that the callbacks in any of these later sublist can be
  1547. * grouped into a single sublist, whether or not they have already
  1548. * been assigned a ->completed number.
  1549. */
  1550. c = rcu_cbs_completed(rsp, rnp);
  1551. for (i = RCU_NEXT_TAIL - 1; i > RCU_DONE_TAIL; i--)
  1552. if (rdp->nxttail[i] != rdp->nxttail[i - 1] &&
  1553. !ULONG_CMP_GE(rdp->nxtcompleted[i], c))
  1554. break;
  1555. /*
  1556. * If there are no sublist for unassigned callbacks, leave.
  1557. * At the same time, advance "i" one sublist, so that "i" will
  1558. * index into the sublist where all the remaining callbacks should
  1559. * be grouped into.
  1560. */
  1561. if (++i >= RCU_NEXT_TAIL)
  1562. return false;
  1563. /*
  1564. * Assign all subsequent callbacks' ->completed number to the next
  1565. * full grace period and group them all in the sublist initially
  1566. * indexed by "i".
  1567. */
  1568. for (; i <= RCU_NEXT_TAIL; i++) {
  1569. rdp->nxttail[i] = rdp->nxttail[RCU_NEXT_TAIL];
  1570. rdp->nxtcompleted[i] = c;
  1571. }
  1572. /* Record any needed additional grace periods. */
  1573. ret = rcu_start_future_gp(rnp, rdp, NULL);
  1574. /* Trace depending on how much we were able to accelerate. */
  1575. if (!*rdp->nxttail[RCU_WAIT_TAIL])
  1576. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("AccWaitCB"));
  1577. else
  1578. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("AccReadyCB"));
  1579. return ret;
  1580. }
  1581. /*
  1582. * Move any callbacks whose grace period has completed to the
  1583. * RCU_DONE_TAIL sublist, then compact the remaining sublists and
  1584. * assign ->completed numbers to any callbacks in the RCU_NEXT_TAIL
  1585. * sublist. This function is idempotent, so it does not hurt to
  1586. * invoke it repeatedly. As long as it is not invoked -too- often...
  1587. * Returns true if the RCU grace-period kthread needs to be awakened.
  1588. *
  1589. * The caller must hold rnp->lock with interrupts disabled.
  1590. */
  1591. static bool rcu_advance_cbs(struct rcu_state *rsp, struct rcu_node *rnp,
  1592. struct rcu_data *rdp)
  1593. {
  1594. int i, j;
  1595. /* If the CPU has no callbacks, nothing to do. */
  1596. if (!rdp->nxttail[RCU_NEXT_TAIL] || !*rdp->nxttail[RCU_DONE_TAIL])
  1597. return false;
  1598. /*
  1599. * Find all callbacks whose ->completed numbers indicate that they
  1600. * are ready to invoke, and put them into the RCU_DONE_TAIL sublist.
  1601. */
  1602. for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++) {
  1603. if (ULONG_CMP_LT(rnp->completed, rdp->nxtcompleted[i]))
  1604. break;
  1605. rdp->nxttail[RCU_DONE_TAIL] = rdp->nxttail[i];
  1606. }
  1607. /* Clean up any sublist tail pointers that were misordered above. */
  1608. for (j = RCU_WAIT_TAIL; j < i; j++)
  1609. rdp->nxttail[j] = rdp->nxttail[RCU_DONE_TAIL];
  1610. /* Copy down callbacks to fill in empty sublists. */
  1611. for (j = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++, j++) {
  1612. if (rdp->nxttail[j] == rdp->nxttail[RCU_NEXT_TAIL])
  1613. break;
  1614. rdp->nxttail[j] = rdp->nxttail[i];
  1615. rdp->nxtcompleted[j] = rdp->nxtcompleted[i];
  1616. }
  1617. /* Classify any remaining callbacks. */
  1618. return rcu_accelerate_cbs(rsp, rnp, rdp);
  1619. }
  1620. /*
  1621. * Update CPU-local rcu_data state to record the beginnings and ends of
  1622. * grace periods. The caller must hold the ->lock of the leaf rcu_node
  1623. * structure corresponding to the current CPU, and must have irqs disabled.
  1624. * Returns true if the grace-period kthread needs to be awakened.
  1625. */
  1626. static bool __note_gp_changes(struct rcu_state *rsp, struct rcu_node *rnp,
  1627. struct rcu_data *rdp)
  1628. {
  1629. bool ret;
  1630. /* Handle the ends of any preceding grace periods first. */
  1631. if (rdp->completed == rnp->completed &&
  1632. !unlikely(READ_ONCE(rdp->gpwrap))) {
  1633. /* No grace period end, so just accelerate recent callbacks. */
  1634. ret = rcu_accelerate_cbs(rsp, rnp, rdp);
  1635. } else {
  1636. /* Advance callbacks. */
  1637. ret = rcu_advance_cbs(rsp, rnp, rdp);
  1638. /* Remember that we saw this grace-period completion. */
  1639. rdp->completed = rnp->completed;
  1640. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpuend"));
  1641. }
  1642. if (rdp->gpnum != rnp->gpnum || unlikely(READ_ONCE(rdp->gpwrap))) {
  1643. /*
  1644. * If the current grace period is waiting for this CPU,
  1645. * set up to detect a quiescent state, otherwise don't
  1646. * go looking for one.
  1647. */
  1648. rdp->gpnum = rnp->gpnum;
  1649. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpustart"));
  1650. rdp->cpu_no_qs.b.norm = true;
  1651. rdp->rcu_qs_ctr_snap = __this_cpu_read(rcu_qs_ctr);
  1652. rdp->core_needs_qs = !!(rnp->qsmask & rdp->grpmask);
  1653. zero_cpu_stall_ticks(rdp);
  1654. WRITE_ONCE(rdp->gpwrap, false);
  1655. }
  1656. return ret;
  1657. }
  1658. static void note_gp_changes(struct rcu_state *rsp, struct rcu_data *rdp)
  1659. {
  1660. unsigned long flags;
  1661. bool needwake;
  1662. struct rcu_node *rnp;
  1663. local_irq_save(flags);
  1664. rnp = rdp->mynode;
  1665. if ((rdp->gpnum == READ_ONCE(rnp->gpnum) &&
  1666. rdp->completed == READ_ONCE(rnp->completed) &&
  1667. !unlikely(READ_ONCE(rdp->gpwrap))) || /* w/out lock. */
  1668. !raw_spin_trylock_rcu_node(rnp)) { /* irqs already off, so later. */
  1669. local_irq_restore(flags);
  1670. return;
  1671. }
  1672. needwake = __note_gp_changes(rsp, rnp, rdp);
  1673. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1674. if (needwake)
  1675. rcu_gp_kthread_wake(rsp);
  1676. }
  1677. static void rcu_gp_slow(struct rcu_state *rsp, int delay)
  1678. {
  1679. if (delay > 0 &&
  1680. !(rsp->gpnum % (rcu_num_nodes * PER_RCU_NODE_PERIOD * delay)))
  1681. schedule_timeout_uninterruptible(delay);
  1682. }
  1683. /*
  1684. * Initialize a new grace period. Return false if no grace period required.
  1685. */
  1686. static bool rcu_gp_init(struct rcu_state *rsp)
  1687. {
  1688. unsigned long oldmask;
  1689. struct rcu_data *rdp;
  1690. struct rcu_node *rnp = rcu_get_root(rsp);
  1691. WRITE_ONCE(rsp->gp_activity, jiffies);
  1692. raw_spin_lock_irq_rcu_node(rnp);
  1693. if (!READ_ONCE(rsp->gp_flags)) {
  1694. /* Spurious wakeup, tell caller to go back to sleep. */
  1695. raw_spin_unlock_irq_rcu_node(rnp);
  1696. return false;
  1697. }
  1698. WRITE_ONCE(rsp->gp_flags, 0); /* Clear all flags: New grace period. */
  1699. if (WARN_ON_ONCE(rcu_gp_in_progress(rsp))) {
  1700. /*
  1701. * Grace period already in progress, don't start another.
  1702. * Not supposed to be able to happen.
  1703. */
  1704. raw_spin_unlock_irq_rcu_node(rnp);
  1705. return false;
  1706. }
  1707. /* Advance to a new grace period and initialize state. */
  1708. record_gp_stall_check_time(rsp);
  1709. /* Record GP times before starting GP, hence smp_store_release(). */
  1710. smp_store_release(&rsp->gpnum, rsp->gpnum + 1);
  1711. trace_rcu_grace_period(rsp->name, rsp->gpnum, TPS("start"));
  1712. raw_spin_unlock_irq_rcu_node(rnp);
  1713. /*
  1714. * Apply per-leaf buffered online and offline operations to the
  1715. * rcu_node tree. Note that this new grace period need not wait
  1716. * for subsequent online CPUs, and that quiescent-state forcing
  1717. * will handle subsequent offline CPUs.
  1718. */
  1719. rcu_for_each_leaf_node(rsp, rnp) {
  1720. rcu_gp_slow(rsp, gp_preinit_delay);
  1721. raw_spin_lock_irq_rcu_node(rnp);
  1722. if (rnp->qsmaskinit == rnp->qsmaskinitnext &&
  1723. !rnp->wait_blkd_tasks) {
  1724. /* Nothing to do on this leaf rcu_node structure. */
  1725. raw_spin_unlock_irq_rcu_node(rnp);
  1726. continue;
  1727. }
  1728. /* Record old state, apply changes to ->qsmaskinit field. */
  1729. oldmask = rnp->qsmaskinit;
  1730. rnp->qsmaskinit = rnp->qsmaskinitnext;
  1731. /* If zero-ness of ->qsmaskinit changed, propagate up tree. */
  1732. if (!oldmask != !rnp->qsmaskinit) {
  1733. if (!oldmask) /* First online CPU for this rcu_node. */
  1734. rcu_init_new_rnp(rnp);
  1735. else if (rcu_preempt_has_tasks(rnp)) /* blocked tasks */
  1736. rnp->wait_blkd_tasks = true;
  1737. else /* Last offline CPU and can propagate. */
  1738. rcu_cleanup_dead_rnp(rnp);
  1739. }
  1740. /*
  1741. * If all waited-on tasks from prior grace period are
  1742. * done, and if all this rcu_node structure's CPUs are
  1743. * still offline, propagate up the rcu_node tree and
  1744. * clear ->wait_blkd_tasks. Otherwise, if one of this
  1745. * rcu_node structure's CPUs has since come back online,
  1746. * simply clear ->wait_blkd_tasks (but rcu_cleanup_dead_rnp()
  1747. * checks for this, so just call it unconditionally).
  1748. */
  1749. if (rnp->wait_blkd_tasks &&
  1750. (!rcu_preempt_has_tasks(rnp) ||
  1751. rnp->qsmaskinit)) {
  1752. rnp->wait_blkd_tasks = false;
  1753. rcu_cleanup_dead_rnp(rnp);
  1754. }
  1755. raw_spin_unlock_irq_rcu_node(rnp);
  1756. }
  1757. /*
  1758. * Set the quiescent-state-needed bits in all the rcu_node
  1759. * structures for all currently online CPUs in breadth-first order,
  1760. * starting from the root rcu_node structure, relying on the layout
  1761. * of the tree within the rsp->node[] array. Note that other CPUs
  1762. * will access only the leaves of the hierarchy, thus seeing that no
  1763. * grace period is in progress, at least until the corresponding
  1764. * leaf node has been initialized. In addition, we have excluded
  1765. * CPU-hotplug operations.
  1766. *
  1767. * The grace period cannot complete until the initialization
  1768. * process finishes, because this kthread handles both.
  1769. */
  1770. rcu_for_each_node_breadth_first(rsp, rnp) {
  1771. rcu_gp_slow(rsp, gp_init_delay);
  1772. raw_spin_lock_irq_rcu_node(rnp);
  1773. rdp = this_cpu_ptr(rsp->rda);
  1774. rcu_preempt_check_blocked_tasks(rnp);
  1775. rnp->qsmask = rnp->qsmaskinit;
  1776. WRITE_ONCE(rnp->gpnum, rsp->gpnum);
  1777. if (WARN_ON_ONCE(rnp->completed != rsp->completed))
  1778. WRITE_ONCE(rnp->completed, rsp->completed);
  1779. if (rnp == rdp->mynode)
  1780. (void)__note_gp_changes(rsp, rnp, rdp);
  1781. rcu_preempt_boost_start_gp(rnp);
  1782. trace_rcu_grace_period_init(rsp->name, rnp->gpnum,
  1783. rnp->level, rnp->grplo,
  1784. rnp->grphi, rnp->qsmask);
  1785. raw_spin_unlock_irq_rcu_node(rnp);
  1786. cond_resched_rcu_qs();
  1787. WRITE_ONCE(rsp->gp_activity, jiffies);
  1788. }
  1789. return true;
  1790. }
  1791. /*
  1792. * Helper function for wait_event_interruptible_timeout() wakeup
  1793. * at force-quiescent-state time.
  1794. */
  1795. static bool rcu_gp_fqs_check_wake(struct rcu_state *rsp, int *gfp)
  1796. {
  1797. struct rcu_node *rnp = rcu_get_root(rsp);
  1798. /* Someone like call_rcu() requested a force-quiescent-state scan. */
  1799. *gfp = READ_ONCE(rsp->gp_flags);
  1800. if (*gfp & RCU_GP_FLAG_FQS)
  1801. return true;
  1802. /* The current grace period has completed. */
  1803. if (!READ_ONCE(rnp->qsmask) && !rcu_preempt_blocked_readers_cgp(rnp))
  1804. return true;
  1805. return false;
  1806. }
  1807. /*
  1808. * Do one round of quiescent-state forcing.
  1809. */
  1810. static void rcu_gp_fqs(struct rcu_state *rsp, bool first_time)
  1811. {
  1812. bool isidle = false;
  1813. unsigned long maxj;
  1814. struct rcu_node *rnp = rcu_get_root(rsp);
  1815. WRITE_ONCE(rsp->gp_activity, jiffies);
  1816. rsp->n_force_qs++;
  1817. if (first_time) {
  1818. /* Collect dyntick-idle snapshots. */
  1819. if (is_sysidle_rcu_state(rsp)) {
  1820. isidle = true;
  1821. maxj = jiffies - ULONG_MAX / 4;
  1822. }
  1823. force_qs_rnp(rsp, dyntick_save_progress_counter,
  1824. &isidle, &maxj);
  1825. rcu_sysidle_report_gp(rsp, isidle, maxj);
  1826. } else {
  1827. /* Handle dyntick-idle and offline CPUs. */
  1828. isidle = true;
  1829. force_qs_rnp(rsp, rcu_implicit_dynticks_qs, &isidle, &maxj);
  1830. }
  1831. /* Clear flag to prevent immediate re-entry. */
  1832. if (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
  1833. raw_spin_lock_irq_rcu_node(rnp);
  1834. WRITE_ONCE(rsp->gp_flags,
  1835. READ_ONCE(rsp->gp_flags) & ~RCU_GP_FLAG_FQS);
  1836. raw_spin_unlock_irq_rcu_node(rnp);
  1837. }
  1838. }
  1839. /*
  1840. * Clean up after the old grace period.
  1841. */
  1842. static void rcu_gp_cleanup(struct rcu_state *rsp)
  1843. {
  1844. unsigned long gp_duration;
  1845. bool needgp = false;
  1846. int nocb = 0;
  1847. struct rcu_data *rdp;
  1848. struct rcu_node *rnp = rcu_get_root(rsp);
  1849. struct swait_queue_head *sq;
  1850. WRITE_ONCE(rsp->gp_activity, jiffies);
  1851. raw_spin_lock_irq_rcu_node(rnp);
  1852. gp_duration = jiffies - rsp->gp_start;
  1853. if (gp_duration > rsp->gp_max)
  1854. rsp->gp_max = gp_duration;
  1855. /*
  1856. * We know the grace period is complete, but to everyone else
  1857. * it appears to still be ongoing. But it is also the case
  1858. * that to everyone else it looks like there is nothing that
  1859. * they can do to advance the grace period. It is therefore
  1860. * safe for us to drop the lock in order to mark the grace
  1861. * period as completed in all of the rcu_node structures.
  1862. */
  1863. raw_spin_unlock_irq_rcu_node(rnp);
  1864. /*
  1865. * Propagate new ->completed value to rcu_node structures so
  1866. * that other CPUs don't have to wait until the start of the next
  1867. * grace period to process their callbacks. This also avoids
  1868. * some nasty RCU grace-period initialization races by forcing
  1869. * the end of the current grace period to be completely recorded in
  1870. * all of the rcu_node structures before the beginning of the next
  1871. * grace period is recorded in any of the rcu_node structures.
  1872. */
  1873. rcu_for_each_node_breadth_first(rsp, rnp) {
  1874. raw_spin_lock_irq_rcu_node(rnp);
  1875. WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp));
  1876. WARN_ON_ONCE(rnp->qsmask);
  1877. WRITE_ONCE(rnp->completed, rsp->gpnum);
  1878. rdp = this_cpu_ptr(rsp->rda);
  1879. if (rnp == rdp->mynode)
  1880. needgp = __note_gp_changes(rsp, rnp, rdp) || needgp;
  1881. /* smp_mb() provided by prior unlock-lock pair. */
  1882. nocb += rcu_future_gp_cleanup(rsp, rnp);
  1883. sq = rcu_nocb_gp_get(rnp);
  1884. raw_spin_unlock_irq_rcu_node(rnp);
  1885. rcu_nocb_gp_cleanup(sq);
  1886. cond_resched_rcu_qs();
  1887. WRITE_ONCE(rsp->gp_activity, jiffies);
  1888. rcu_gp_slow(rsp, gp_cleanup_delay);
  1889. }
  1890. rnp = rcu_get_root(rsp);
  1891. raw_spin_lock_irq_rcu_node(rnp); /* Order GP before ->completed update. */
  1892. rcu_nocb_gp_set(rnp, nocb);
  1893. /* Declare grace period done. */
  1894. WRITE_ONCE(rsp->completed, rsp->gpnum);
  1895. trace_rcu_grace_period(rsp->name, rsp->completed, TPS("end"));
  1896. rsp->gp_state = RCU_GP_IDLE;
  1897. rdp = this_cpu_ptr(rsp->rda);
  1898. /* Advance CBs to reduce false positives below. */
  1899. needgp = rcu_advance_cbs(rsp, rnp, rdp) || needgp;
  1900. if (needgp || cpu_needs_another_gp(rsp, rdp)) {
  1901. WRITE_ONCE(rsp->gp_flags, RCU_GP_FLAG_INIT);
  1902. trace_rcu_grace_period(rsp->name,
  1903. READ_ONCE(rsp->gpnum),
  1904. TPS("newreq"));
  1905. }
  1906. raw_spin_unlock_irq_rcu_node(rnp);
  1907. }
  1908. /*
  1909. * Body of kthread that handles grace periods.
  1910. */
  1911. static int __noreturn rcu_gp_kthread(void *arg)
  1912. {
  1913. bool first_gp_fqs;
  1914. int gf;
  1915. unsigned long j;
  1916. int ret;
  1917. struct rcu_state *rsp = arg;
  1918. struct rcu_node *rnp = rcu_get_root(rsp);
  1919. rcu_bind_gp_kthread();
  1920. for (;;) {
  1921. /* Handle grace-period start. */
  1922. for (;;) {
  1923. trace_rcu_grace_period(rsp->name,
  1924. READ_ONCE(rsp->gpnum),
  1925. TPS("reqwait"));
  1926. rsp->gp_state = RCU_GP_WAIT_GPS;
  1927. swait_event_interruptible(rsp->gp_wq,
  1928. READ_ONCE(rsp->gp_flags) &
  1929. RCU_GP_FLAG_INIT);
  1930. rsp->gp_state = RCU_GP_DONE_GPS;
  1931. /* Locking provides needed memory barrier. */
  1932. if (rcu_gp_init(rsp))
  1933. break;
  1934. cond_resched_rcu_qs();
  1935. WRITE_ONCE(rsp->gp_activity, jiffies);
  1936. WARN_ON(signal_pending(current));
  1937. trace_rcu_grace_period(rsp->name,
  1938. READ_ONCE(rsp->gpnum),
  1939. TPS("reqwaitsig"));
  1940. }
  1941. /* Handle quiescent-state forcing. */
  1942. first_gp_fqs = true;
  1943. j = jiffies_till_first_fqs;
  1944. if (j > HZ) {
  1945. j = HZ;
  1946. jiffies_till_first_fqs = HZ;
  1947. }
  1948. ret = 0;
  1949. for (;;) {
  1950. if (!ret) {
  1951. rsp->jiffies_force_qs = jiffies + j;
  1952. WRITE_ONCE(rsp->jiffies_kick_kthreads,
  1953. jiffies + 3 * j);
  1954. }
  1955. trace_rcu_grace_period(rsp->name,
  1956. READ_ONCE(rsp->gpnum),
  1957. TPS("fqswait"));
  1958. rsp->gp_state = RCU_GP_WAIT_FQS;
  1959. ret = swait_event_interruptible_timeout(rsp->gp_wq,
  1960. rcu_gp_fqs_check_wake(rsp, &gf), j);
  1961. rsp->gp_state = RCU_GP_DOING_FQS;
  1962. /* Locking provides needed memory barriers. */
  1963. /* If grace period done, leave loop. */
  1964. if (!READ_ONCE(rnp->qsmask) &&
  1965. !rcu_preempt_blocked_readers_cgp(rnp))
  1966. break;
  1967. /* If time for quiescent-state forcing, do it. */
  1968. if (ULONG_CMP_GE(jiffies, rsp->jiffies_force_qs) ||
  1969. (gf & RCU_GP_FLAG_FQS)) {
  1970. trace_rcu_grace_period(rsp->name,
  1971. READ_ONCE(rsp->gpnum),
  1972. TPS("fqsstart"));
  1973. rcu_gp_fqs(rsp, first_gp_fqs);
  1974. first_gp_fqs = false;
  1975. trace_rcu_grace_period(rsp->name,
  1976. READ_ONCE(rsp->gpnum),
  1977. TPS("fqsend"));
  1978. cond_resched_rcu_qs();
  1979. WRITE_ONCE(rsp->gp_activity, jiffies);
  1980. ret = 0; /* Force full wait till next FQS. */
  1981. j = jiffies_till_next_fqs;
  1982. if (j > HZ) {
  1983. j = HZ;
  1984. jiffies_till_next_fqs = HZ;
  1985. } else if (j < 1) {
  1986. j = 1;
  1987. jiffies_till_next_fqs = 1;
  1988. }
  1989. } else {
  1990. /* Deal with stray signal. */
  1991. cond_resched_rcu_qs();
  1992. WRITE_ONCE(rsp->gp_activity, jiffies);
  1993. WARN_ON(signal_pending(current));
  1994. trace_rcu_grace_period(rsp->name,
  1995. READ_ONCE(rsp->gpnum),
  1996. TPS("fqswaitsig"));
  1997. ret = 1; /* Keep old FQS timing. */
  1998. j = jiffies;
  1999. if (time_after(jiffies, rsp->jiffies_force_qs))
  2000. j = 1;
  2001. else
  2002. j = rsp->jiffies_force_qs - j;
  2003. }
  2004. }
  2005. /* Handle grace-period end. */
  2006. rsp->gp_state = RCU_GP_CLEANUP;
  2007. rcu_gp_cleanup(rsp);
  2008. rsp->gp_state = RCU_GP_CLEANED;
  2009. }
  2010. }
  2011. /*
  2012. * Start a new RCU grace period if warranted, re-initializing the hierarchy
  2013. * in preparation for detecting the next grace period. The caller must hold
  2014. * the root node's ->lock and hard irqs must be disabled.
  2015. *
  2016. * Note that it is legal for a dying CPU (which is marked as offline) to
  2017. * invoke this function. This can happen when the dying CPU reports its
  2018. * quiescent state.
  2019. *
  2020. * Returns true if the grace-period kthread must be awakened.
  2021. */
  2022. static bool
  2023. rcu_start_gp_advanced(struct rcu_state *rsp, struct rcu_node *rnp,
  2024. struct rcu_data *rdp)
  2025. {
  2026. if (!rsp->gp_kthread || !cpu_needs_another_gp(rsp, rdp)) {
  2027. /*
  2028. * Either we have not yet spawned the grace-period
  2029. * task, this CPU does not need another grace period,
  2030. * or a grace period is already in progress.
  2031. * Either way, don't start a new grace period.
  2032. */
  2033. return false;
  2034. }
  2035. WRITE_ONCE(rsp->gp_flags, RCU_GP_FLAG_INIT);
  2036. trace_rcu_grace_period(rsp->name, READ_ONCE(rsp->gpnum),
  2037. TPS("newreq"));
  2038. /*
  2039. * We can't do wakeups while holding the rnp->lock, as that
  2040. * could cause possible deadlocks with the rq->lock. Defer
  2041. * the wakeup to our caller.
  2042. */
  2043. return true;
  2044. }
  2045. /*
  2046. * Similar to rcu_start_gp_advanced(), but also advance the calling CPU's
  2047. * callbacks. Note that rcu_start_gp_advanced() cannot do this because it
  2048. * is invoked indirectly from rcu_advance_cbs(), which would result in
  2049. * endless recursion -- or would do so if it wasn't for the self-deadlock
  2050. * that is encountered beforehand.
  2051. *
  2052. * Returns true if the grace-period kthread needs to be awakened.
  2053. */
  2054. static bool rcu_start_gp(struct rcu_state *rsp)
  2055. {
  2056. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  2057. struct rcu_node *rnp = rcu_get_root(rsp);
  2058. bool ret = false;
  2059. /*
  2060. * If there is no grace period in progress right now, any
  2061. * callbacks we have up to this point will be satisfied by the
  2062. * next grace period. Also, advancing the callbacks reduces the
  2063. * probability of false positives from cpu_needs_another_gp()
  2064. * resulting in pointless grace periods. So, advance callbacks
  2065. * then start the grace period!
  2066. */
  2067. ret = rcu_advance_cbs(rsp, rnp, rdp) || ret;
  2068. ret = rcu_start_gp_advanced(rsp, rnp, rdp) || ret;
  2069. return ret;
  2070. }
  2071. /*
  2072. * Report a full set of quiescent states to the specified rcu_state data
  2073. * structure. Invoke rcu_gp_kthread_wake() to awaken the grace-period
  2074. * kthread if another grace period is required. Whether we wake
  2075. * the grace-period kthread or it awakens itself for the next round
  2076. * of quiescent-state forcing, that kthread will clean up after the
  2077. * just-completed grace period. Note that the caller must hold rnp->lock,
  2078. * which is released before return.
  2079. */
  2080. static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
  2081. __releases(rcu_get_root(rsp)->lock)
  2082. {
  2083. WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
  2084. WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
  2085. raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(rsp), flags);
  2086. swake_up(&rsp->gp_wq); /* Memory barrier implied by swake_up() path. */
  2087. }
  2088. /*
  2089. * Similar to rcu_report_qs_rdp(), for which it is a helper function.
  2090. * Allows quiescent states for a group of CPUs to be reported at one go
  2091. * to the specified rcu_node structure, though all the CPUs in the group
  2092. * must be represented by the same rcu_node structure (which need not be a
  2093. * leaf rcu_node structure, though it often will be). The gps parameter
  2094. * is the grace-period snapshot, which means that the quiescent states
  2095. * are valid only if rnp->gpnum is equal to gps. That structure's lock
  2096. * must be held upon entry, and it is released before return.
  2097. */
  2098. static void
  2099. rcu_report_qs_rnp(unsigned long mask, struct rcu_state *rsp,
  2100. struct rcu_node *rnp, unsigned long gps, unsigned long flags)
  2101. __releases(rnp->lock)
  2102. {
  2103. unsigned long oldmask = 0;
  2104. struct rcu_node *rnp_c;
  2105. /* Walk up the rcu_node hierarchy. */
  2106. for (;;) {
  2107. if (!(rnp->qsmask & mask) || rnp->gpnum != gps) {
  2108. /*
  2109. * Our bit has already been cleared, or the
  2110. * relevant grace period is already over, so done.
  2111. */
  2112. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2113. return;
  2114. }
  2115. WARN_ON_ONCE(oldmask); /* Any child must be all zeroed! */
  2116. rnp->qsmask &= ~mask;
  2117. trace_rcu_quiescent_state_report(rsp->name, rnp->gpnum,
  2118. mask, rnp->qsmask, rnp->level,
  2119. rnp->grplo, rnp->grphi,
  2120. !!rnp->gp_tasks);
  2121. if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  2122. /* Other bits still set at this level, so done. */
  2123. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2124. return;
  2125. }
  2126. mask = rnp->grpmask;
  2127. if (rnp->parent == NULL) {
  2128. /* No more levels. Exit loop holding root lock. */
  2129. break;
  2130. }
  2131. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2132. rnp_c = rnp;
  2133. rnp = rnp->parent;
  2134. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  2135. oldmask = rnp_c->qsmask;
  2136. }
  2137. /*
  2138. * Get here if we are the last CPU to pass through a quiescent
  2139. * state for this grace period. Invoke rcu_report_qs_rsp()
  2140. * to clean up and start the next grace period if one is needed.
  2141. */
  2142. rcu_report_qs_rsp(rsp, flags); /* releases rnp->lock. */
  2143. }
  2144. /*
  2145. * Record a quiescent state for all tasks that were previously queued
  2146. * on the specified rcu_node structure and that were blocking the current
  2147. * RCU grace period. The caller must hold the specified rnp->lock with
  2148. * irqs disabled, and this lock is released upon return, but irqs remain
  2149. * disabled.
  2150. */
  2151. static void rcu_report_unblock_qs_rnp(struct rcu_state *rsp,
  2152. struct rcu_node *rnp, unsigned long flags)
  2153. __releases(rnp->lock)
  2154. {
  2155. unsigned long gps;
  2156. unsigned long mask;
  2157. struct rcu_node *rnp_p;
  2158. if (rcu_state_p == &rcu_sched_state || rsp != rcu_state_p ||
  2159. rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  2160. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2161. return; /* Still need more quiescent states! */
  2162. }
  2163. rnp_p = rnp->parent;
  2164. if (rnp_p == NULL) {
  2165. /*
  2166. * Only one rcu_node structure in the tree, so don't
  2167. * try to report up to its nonexistent parent!
  2168. */
  2169. rcu_report_qs_rsp(rsp, flags);
  2170. return;
  2171. }
  2172. /* Report up the rest of the hierarchy, tracking current ->gpnum. */
  2173. gps = rnp->gpnum;
  2174. mask = rnp->grpmask;
  2175. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  2176. raw_spin_lock_rcu_node(rnp_p); /* irqs already disabled. */
  2177. rcu_report_qs_rnp(mask, rsp, rnp_p, gps, flags);
  2178. }
  2179. /*
  2180. * Record a quiescent state for the specified CPU to that CPU's rcu_data
  2181. * structure. This must be called from the specified CPU.
  2182. */
  2183. static void
  2184. rcu_report_qs_rdp(int cpu, struct rcu_state *rsp, struct rcu_data *rdp)
  2185. {
  2186. unsigned long flags;
  2187. unsigned long mask;
  2188. bool needwake;
  2189. struct rcu_node *rnp;
  2190. rnp = rdp->mynode;
  2191. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  2192. if ((rdp->cpu_no_qs.b.norm &&
  2193. rdp->rcu_qs_ctr_snap == __this_cpu_read(rcu_qs_ctr)) ||
  2194. rdp->gpnum != rnp->gpnum || rnp->completed == rnp->gpnum ||
  2195. rdp->gpwrap) {
  2196. /*
  2197. * The grace period in which this quiescent state was
  2198. * recorded has ended, so don't report it upwards.
  2199. * We will instead need a new quiescent state that lies
  2200. * within the current grace period.
  2201. */
  2202. rdp->cpu_no_qs.b.norm = true; /* need qs for new gp. */
  2203. rdp->rcu_qs_ctr_snap = __this_cpu_read(rcu_qs_ctr);
  2204. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2205. return;
  2206. }
  2207. mask = rdp->grpmask;
  2208. if ((rnp->qsmask & mask) == 0) {
  2209. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2210. } else {
  2211. rdp->core_needs_qs = false;
  2212. /*
  2213. * This GP can't end until cpu checks in, so all of our
  2214. * callbacks can be processed during the next GP.
  2215. */
  2216. needwake = rcu_accelerate_cbs(rsp, rnp, rdp);
  2217. rcu_report_qs_rnp(mask, rsp, rnp, rnp->gpnum, flags);
  2218. /* ^^^ Released rnp->lock */
  2219. if (needwake)
  2220. rcu_gp_kthread_wake(rsp);
  2221. }
  2222. }
  2223. /*
  2224. * Check to see if there is a new grace period of which this CPU
  2225. * is not yet aware, and if so, set up local rcu_data state for it.
  2226. * Otherwise, see if this CPU has just passed through its first
  2227. * quiescent state for this grace period, and record that fact if so.
  2228. */
  2229. static void
  2230. rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
  2231. {
  2232. /* Check for grace-period ends and beginnings. */
  2233. note_gp_changes(rsp, rdp);
  2234. /*
  2235. * Does this CPU still need to do its part for current grace period?
  2236. * If no, return and let the other CPUs do their part as well.
  2237. */
  2238. if (!rdp->core_needs_qs)
  2239. return;
  2240. /*
  2241. * Was there a quiescent state since the beginning of the grace
  2242. * period? If no, then exit and wait for the next call.
  2243. */
  2244. if (rdp->cpu_no_qs.b.norm &&
  2245. rdp->rcu_qs_ctr_snap == __this_cpu_read(rcu_qs_ctr))
  2246. return;
  2247. /*
  2248. * Tell RCU we are done (but rcu_report_qs_rdp() will be the
  2249. * judge of that).
  2250. */
  2251. rcu_report_qs_rdp(rdp->cpu, rsp, rdp);
  2252. }
  2253. /*
  2254. * Send the specified CPU's RCU callbacks to the orphanage. The
  2255. * specified CPU must be offline, and the caller must hold the
  2256. * ->orphan_lock.
  2257. */
  2258. static void
  2259. rcu_send_cbs_to_orphanage(int cpu, struct rcu_state *rsp,
  2260. struct rcu_node *rnp, struct rcu_data *rdp)
  2261. {
  2262. /* No-CBs CPUs do not have orphanable callbacks. */
  2263. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) || rcu_is_nocb_cpu(rdp->cpu))
  2264. return;
  2265. /*
  2266. * Orphan the callbacks. First adjust the counts. This is safe
  2267. * because _rcu_barrier() excludes CPU-hotplug operations, so it
  2268. * cannot be running now. Thus no memory barrier is required.
  2269. */
  2270. if (rdp->nxtlist != NULL) {
  2271. rsp->qlen_lazy += rdp->qlen_lazy;
  2272. rsp->qlen += rdp->qlen;
  2273. rdp->n_cbs_orphaned += rdp->qlen;
  2274. rdp->qlen_lazy = 0;
  2275. WRITE_ONCE(rdp->qlen, 0);
  2276. }
  2277. /*
  2278. * Next, move those callbacks still needing a grace period to
  2279. * the orphanage, where some other CPU will pick them up.
  2280. * Some of the callbacks might have gone partway through a grace
  2281. * period, but that is too bad. They get to start over because we
  2282. * cannot assume that grace periods are synchronized across CPUs.
  2283. * We don't bother updating the ->nxttail[] array yet, instead
  2284. * we just reset the whole thing later on.
  2285. */
  2286. if (*rdp->nxttail[RCU_DONE_TAIL] != NULL) {
  2287. *rsp->orphan_nxttail = *rdp->nxttail[RCU_DONE_TAIL];
  2288. rsp->orphan_nxttail = rdp->nxttail[RCU_NEXT_TAIL];
  2289. *rdp->nxttail[RCU_DONE_TAIL] = NULL;
  2290. }
  2291. /*
  2292. * Then move the ready-to-invoke callbacks to the orphanage,
  2293. * where some other CPU will pick them up. These will not be
  2294. * required to pass though another grace period: They are done.
  2295. */
  2296. if (rdp->nxtlist != NULL) {
  2297. *rsp->orphan_donetail = rdp->nxtlist;
  2298. rsp->orphan_donetail = rdp->nxttail[RCU_DONE_TAIL];
  2299. }
  2300. /*
  2301. * Finally, initialize the rcu_data structure's list to empty and
  2302. * disallow further callbacks on this CPU.
  2303. */
  2304. init_callback_list(rdp);
  2305. rdp->nxttail[RCU_NEXT_TAIL] = NULL;
  2306. }
  2307. /*
  2308. * Adopt the RCU callbacks from the specified rcu_state structure's
  2309. * orphanage. The caller must hold the ->orphan_lock.
  2310. */
  2311. static void rcu_adopt_orphan_cbs(struct rcu_state *rsp, unsigned long flags)
  2312. {
  2313. int i;
  2314. struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
  2315. /* No-CBs CPUs are handled specially. */
  2316. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
  2317. rcu_nocb_adopt_orphan_cbs(rsp, rdp, flags))
  2318. return;
  2319. /* Do the accounting first. */
  2320. rdp->qlen_lazy += rsp->qlen_lazy;
  2321. rdp->qlen += rsp->qlen;
  2322. rdp->n_cbs_adopted += rsp->qlen;
  2323. if (rsp->qlen_lazy != rsp->qlen)
  2324. rcu_idle_count_callbacks_posted();
  2325. rsp->qlen_lazy = 0;
  2326. rsp->qlen = 0;
  2327. /*
  2328. * We do not need a memory barrier here because the only way we
  2329. * can get here if there is an rcu_barrier() in flight is if
  2330. * we are the task doing the rcu_barrier().
  2331. */
  2332. /* First adopt the ready-to-invoke callbacks. */
  2333. if (rsp->orphan_donelist != NULL) {
  2334. *rsp->orphan_donetail = *rdp->nxttail[RCU_DONE_TAIL];
  2335. *rdp->nxttail[RCU_DONE_TAIL] = rsp->orphan_donelist;
  2336. for (i = RCU_NEXT_SIZE - 1; i >= RCU_DONE_TAIL; i--)
  2337. if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL])
  2338. rdp->nxttail[i] = rsp->orphan_donetail;
  2339. rsp->orphan_donelist = NULL;
  2340. rsp->orphan_donetail = &rsp->orphan_donelist;
  2341. }
  2342. /* And then adopt the callbacks that still need a grace period. */
  2343. if (rsp->orphan_nxtlist != NULL) {
  2344. *rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxtlist;
  2345. rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxttail;
  2346. rsp->orphan_nxtlist = NULL;
  2347. rsp->orphan_nxttail = &rsp->orphan_nxtlist;
  2348. }
  2349. }
  2350. /*
  2351. * Trace the fact that this CPU is going offline.
  2352. */
  2353. static void rcu_cleanup_dying_cpu(struct rcu_state *rsp)
  2354. {
  2355. RCU_TRACE(unsigned long mask);
  2356. RCU_TRACE(struct rcu_data *rdp = this_cpu_ptr(rsp->rda));
  2357. RCU_TRACE(struct rcu_node *rnp = rdp->mynode);
  2358. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
  2359. return;
  2360. RCU_TRACE(mask = rdp->grpmask);
  2361. trace_rcu_grace_period(rsp->name,
  2362. rnp->gpnum + 1 - !!(rnp->qsmask & mask),
  2363. TPS("cpuofl"));
  2364. }
  2365. /*
  2366. * All CPUs for the specified rcu_node structure have gone offline,
  2367. * and all tasks that were preempted within an RCU read-side critical
  2368. * section while running on one of those CPUs have since exited their RCU
  2369. * read-side critical section. Some other CPU is reporting this fact with
  2370. * the specified rcu_node structure's ->lock held and interrupts disabled.
  2371. * This function therefore goes up the tree of rcu_node structures,
  2372. * clearing the corresponding bits in the ->qsmaskinit fields. Note that
  2373. * the leaf rcu_node structure's ->qsmaskinit field has already been
  2374. * updated
  2375. *
  2376. * This function does check that the specified rcu_node structure has
  2377. * all CPUs offline and no blocked tasks, so it is OK to invoke it
  2378. * prematurely. That said, invoking it after the fact will cost you
  2379. * a needless lock acquisition. So once it has done its work, don't
  2380. * invoke it again.
  2381. */
  2382. static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf)
  2383. {
  2384. long mask;
  2385. struct rcu_node *rnp = rnp_leaf;
  2386. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
  2387. rnp->qsmaskinit || rcu_preempt_has_tasks(rnp))
  2388. return;
  2389. for (;;) {
  2390. mask = rnp->grpmask;
  2391. rnp = rnp->parent;
  2392. if (!rnp)
  2393. break;
  2394. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  2395. rnp->qsmaskinit &= ~mask;
  2396. rnp->qsmask &= ~mask;
  2397. if (rnp->qsmaskinit) {
  2398. raw_spin_unlock_rcu_node(rnp);
  2399. /* irqs remain disabled. */
  2400. return;
  2401. }
  2402. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  2403. }
  2404. }
  2405. /*
  2406. * The CPU has been completely removed, and some other CPU is reporting
  2407. * this fact from process context. Do the remainder of the cleanup,
  2408. * including orphaning the outgoing CPU's RCU callbacks, and also
  2409. * adopting them. There can only be one CPU hotplug operation at a time,
  2410. * so no other CPU can be attempting to update rcu_cpu_kthread_task.
  2411. */
  2412. static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp)
  2413. {
  2414. unsigned long flags;
  2415. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  2416. struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
  2417. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
  2418. return;
  2419. /* Adjust any no-longer-needed kthreads. */
  2420. rcu_boost_kthread_setaffinity(rnp, -1);
  2421. /* Orphan the dead CPU's callbacks, and adopt them if appropriate. */
  2422. raw_spin_lock_irqsave(&rsp->orphan_lock, flags);
  2423. rcu_send_cbs_to_orphanage(cpu, rsp, rnp, rdp);
  2424. rcu_adopt_orphan_cbs(rsp, flags);
  2425. raw_spin_unlock_irqrestore(&rsp->orphan_lock, flags);
  2426. WARN_ONCE(rdp->qlen != 0 || rdp->nxtlist != NULL,
  2427. "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, nxtlist=%p\n",
  2428. cpu, rdp->qlen, rdp->nxtlist);
  2429. }
  2430. /*
  2431. * Invoke any RCU callbacks that have made it to the end of their grace
  2432. * period. Thottle as specified by rdp->blimit.
  2433. */
  2434. static void rcu_do_batch(struct rcu_state *rsp, struct rcu_data *rdp)
  2435. {
  2436. unsigned long flags;
  2437. struct rcu_head *next, *list, **tail;
  2438. long bl, count, count_lazy;
  2439. int i;
  2440. /* If no callbacks are ready, just return. */
  2441. if (!cpu_has_callbacks_ready_to_invoke(rdp)) {
  2442. trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, 0);
  2443. trace_rcu_batch_end(rsp->name, 0, !!READ_ONCE(rdp->nxtlist),
  2444. need_resched(), is_idle_task(current),
  2445. rcu_is_callbacks_kthread());
  2446. return;
  2447. }
  2448. /*
  2449. * Extract the list of ready callbacks, disabling to prevent
  2450. * races with call_rcu() from interrupt handlers.
  2451. */
  2452. local_irq_save(flags);
  2453. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  2454. bl = rdp->blimit;
  2455. trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, bl);
  2456. list = rdp->nxtlist;
  2457. rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL];
  2458. *rdp->nxttail[RCU_DONE_TAIL] = NULL;
  2459. tail = rdp->nxttail[RCU_DONE_TAIL];
  2460. for (i = RCU_NEXT_SIZE - 1; i >= 0; i--)
  2461. if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL])
  2462. rdp->nxttail[i] = &rdp->nxtlist;
  2463. local_irq_restore(flags);
  2464. /* Invoke callbacks. */
  2465. count = count_lazy = 0;
  2466. while (list) {
  2467. next = list->next;
  2468. prefetch(next);
  2469. debug_rcu_head_unqueue(list);
  2470. if (__rcu_reclaim(rsp->name, list))
  2471. count_lazy++;
  2472. list = next;
  2473. /* Stop only if limit reached and CPU has something to do. */
  2474. if (++count >= bl &&
  2475. (need_resched() ||
  2476. (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
  2477. break;
  2478. }
  2479. local_irq_save(flags);
  2480. trace_rcu_batch_end(rsp->name, count, !!list, need_resched(),
  2481. is_idle_task(current),
  2482. rcu_is_callbacks_kthread());
  2483. /* Update count, and requeue any remaining callbacks. */
  2484. if (list != NULL) {
  2485. *tail = rdp->nxtlist;
  2486. rdp->nxtlist = list;
  2487. for (i = 0; i < RCU_NEXT_SIZE; i++)
  2488. if (&rdp->nxtlist == rdp->nxttail[i])
  2489. rdp->nxttail[i] = tail;
  2490. else
  2491. break;
  2492. }
  2493. smp_mb(); /* List handling before counting for rcu_barrier(). */
  2494. rdp->qlen_lazy -= count_lazy;
  2495. WRITE_ONCE(rdp->qlen, rdp->qlen - count);
  2496. rdp->n_cbs_invoked += count;
  2497. /* Reinstate batch limit if we have worked down the excess. */
  2498. if (rdp->blimit == LONG_MAX && rdp->qlen <= qlowmark)
  2499. rdp->blimit = blimit;
  2500. /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
  2501. if (rdp->qlen == 0 && rdp->qlen_last_fqs_check != 0) {
  2502. rdp->qlen_last_fqs_check = 0;
  2503. rdp->n_force_qs_snap = rsp->n_force_qs;
  2504. } else if (rdp->qlen < rdp->qlen_last_fqs_check - qhimark)
  2505. rdp->qlen_last_fqs_check = rdp->qlen;
  2506. WARN_ON_ONCE((rdp->nxtlist == NULL) != (rdp->qlen == 0));
  2507. local_irq_restore(flags);
  2508. /* Re-invoke RCU core processing if there are callbacks remaining. */
  2509. if (cpu_has_callbacks_ready_to_invoke(rdp))
  2510. invoke_rcu_core();
  2511. }
  2512. /*
  2513. * Check to see if this CPU is in a non-context-switch quiescent state
  2514. * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
  2515. * Also schedule RCU core processing.
  2516. *
  2517. * This function must be called from hardirq context. It is normally
  2518. * invoked from the scheduling-clock interrupt. If rcu_pending returns
  2519. * false, there is no point in invoking rcu_check_callbacks().
  2520. */
  2521. void rcu_check_callbacks(int user)
  2522. {
  2523. trace_rcu_utilization(TPS("Start scheduler-tick"));
  2524. increment_cpu_stall_ticks();
  2525. if (user || rcu_is_cpu_rrupt_from_idle()) {
  2526. /*
  2527. * Get here if this CPU took its interrupt from user
  2528. * mode or from the idle loop, and if this is not a
  2529. * nested interrupt. In this case, the CPU is in
  2530. * a quiescent state, so note it.
  2531. *
  2532. * No memory barrier is required here because both
  2533. * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
  2534. * variables that other CPUs neither access nor modify,
  2535. * at least not while the corresponding CPU is online.
  2536. */
  2537. rcu_sched_qs();
  2538. rcu_bh_qs();
  2539. } else if (!in_softirq()) {
  2540. /*
  2541. * Get here if this CPU did not take its interrupt from
  2542. * softirq, in other words, if it is not interrupting
  2543. * a rcu_bh read-side critical section. This is an _bh
  2544. * critical section, so note it.
  2545. */
  2546. rcu_bh_qs();
  2547. }
  2548. rcu_preempt_check_callbacks();
  2549. if (rcu_pending())
  2550. invoke_rcu_core();
  2551. if (user)
  2552. rcu_note_voluntary_context_switch(current);
  2553. trace_rcu_utilization(TPS("End scheduler-tick"));
  2554. }
  2555. /*
  2556. * Scan the leaf rcu_node structures, processing dyntick state for any that
  2557. * have not yet encountered a quiescent state, using the function specified.
  2558. * Also initiate boosting for any threads blocked on the root rcu_node.
  2559. *
  2560. * The caller must have suppressed start of new grace periods.
  2561. */
  2562. static void force_qs_rnp(struct rcu_state *rsp,
  2563. int (*f)(struct rcu_data *rsp, bool *isidle,
  2564. unsigned long *maxj),
  2565. bool *isidle, unsigned long *maxj)
  2566. {
  2567. unsigned long bit;
  2568. int cpu;
  2569. unsigned long flags;
  2570. unsigned long mask;
  2571. struct rcu_node *rnp;
  2572. rcu_for_each_leaf_node(rsp, rnp) {
  2573. cond_resched_rcu_qs();
  2574. mask = 0;
  2575. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  2576. if (rnp->qsmask == 0) {
  2577. if (rcu_state_p == &rcu_sched_state ||
  2578. rsp != rcu_state_p ||
  2579. rcu_preempt_blocked_readers_cgp(rnp)) {
  2580. /*
  2581. * No point in scanning bits because they
  2582. * are all zero. But we might need to
  2583. * priority-boost blocked readers.
  2584. */
  2585. rcu_initiate_boost(rnp, flags);
  2586. /* rcu_initiate_boost() releases rnp->lock */
  2587. continue;
  2588. }
  2589. if (rnp->parent &&
  2590. (rnp->parent->qsmask & rnp->grpmask)) {
  2591. /*
  2592. * Race between grace-period
  2593. * initialization and task exiting RCU
  2594. * read-side critical section: Report.
  2595. */
  2596. rcu_report_unblock_qs_rnp(rsp, rnp, flags);
  2597. /* rcu_report_unblock_qs_rnp() rlses ->lock */
  2598. continue;
  2599. }
  2600. }
  2601. cpu = rnp->grplo;
  2602. bit = 1;
  2603. for (; cpu <= rnp->grphi; cpu++, bit <<= 1) {
  2604. if ((rnp->qsmask & bit) != 0) {
  2605. if (f(per_cpu_ptr(rsp->rda, cpu), isidle, maxj))
  2606. mask |= bit;
  2607. }
  2608. }
  2609. if (mask != 0) {
  2610. /* Idle/offline CPUs, report (releases rnp->lock. */
  2611. rcu_report_qs_rnp(mask, rsp, rnp, rnp->gpnum, flags);
  2612. } else {
  2613. /* Nothing to do here, so just drop the lock. */
  2614. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2615. }
  2616. }
  2617. }
  2618. /*
  2619. * Force quiescent states on reluctant CPUs, and also detect which
  2620. * CPUs are in dyntick-idle mode.
  2621. */
  2622. static void force_quiescent_state(struct rcu_state *rsp)
  2623. {
  2624. unsigned long flags;
  2625. bool ret;
  2626. struct rcu_node *rnp;
  2627. struct rcu_node *rnp_old = NULL;
  2628. /* Funnel through hierarchy to reduce memory contention. */
  2629. rnp = __this_cpu_read(rsp->rda->mynode);
  2630. for (; rnp != NULL; rnp = rnp->parent) {
  2631. ret = (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) ||
  2632. !raw_spin_trylock(&rnp->fqslock);
  2633. if (rnp_old != NULL)
  2634. raw_spin_unlock(&rnp_old->fqslock);
  2635. if (ret) {
  2636. rsp->n_force_qs_lh++;
  2637. return;
  2638. }
  2639. rnp_old = rnp;
  2640. }
  2641. /* rnp_old == rcu_get_root(rsp), rnp == NULL. */
  2642. /* Reached the root of the rcu_node tree, acquire lock. */
  2643. raw_spin_lock_irqsave_rcu_node(rnp_old, flags);
  2644. raw_spin_unlock(&rnp_old->fqslock);
  2645. if (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
  2646. rsp->n_force_qs_lh++;
  2647. raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
  2648. return; /* Someone beat us to it. */
  2649. }
  2650. WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
  2651. raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
  2652. swake_up(&rsp->gp_wq); /* Memory barrier implied by swake_up() path. */
  2653. }
  2654. /*
  2655. * This does the RCU core processing work for the specified rcu_state
  2656. * and rcu_data structures. This may be called only from the CPU to
  2657. * whom the rdp belongs.
  2658. */
  2659. static void
  2660. __rcu_process_callbacks(struct rcu_state *rsp)
  2661. {
  2662. unsigned long flags;
  2663. bool needwake;
  2664. struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
  2665. WARN_ON_ONCE(rdp->beenonline == 0);
  2666. /* Update RCU state based on any recent quiescent states. */
  2667. rcu_check_quiescent_state(rsp, rdp);
  2668. /* Does this CPU require a not-yet-started grace period? */
  2669. local_irq_save(flags);
  2670. if (cpu_needs_another_gp(rsp, rdp)) {
  2671. raw_spin_lock_rcu_node(rcu_get_root(rsp)); /* irqs disabled. */
  2672. needwake = rcu_start_gp(rsp);
  2673. raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(rsp), flags);
  2674. if (needwake)
  2675. rcu_gp_kthread_wake(rsp);
  2676. } else {
  2677. local_irq_restore(flags);
  2678. }
  2679. /* If there are callbacks ready, invoke them. */
  2680. if (cpu_has_callbacks_ready_to_invoke(rdp))
  2681. invoke_rcu_callbacks(rsp, rdp);
  2682. /* Do any needed deferred wakeups of rcuo kthreads. */
  2683. do_nocb_deferred_wakeup(rdp);
  2684. }
  2685. /*
  2686. * Do RCU core processing for the current CPU.
  2687. */
  2688. static void rcu_process_callbacks(struct softirq_action *unused)
  2689. {
  2690. struct rcu_state *rsp;
  2691. if (cpu_is_offline(smp_processor_id()))
  2692. return;
  2693. trace_rcu_utilization(TPS("Start RCU core"));
  2694. for_each_rcu_flavor(rsp)
  2695. __rcu_process_callbacks(rsp);
  2696. trace_rcu_utilization(TPS("End RCU core"));
  2697. }
  2698. /*
  2699. * Schedule RCU callback invocation. If the specified type of RCU
  2700. * does not support RCU priority boosting, just do a direct call,
  2701. * otherwise wake up the per-CPU kernel kthread. Note that because we
  2702. * are running on the current CPU with softirqs disabled, the
  2703. * rcu_cpu_kthread_task cannot disappear out from under us.
  2704. */
  2705. static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
  2706. {
  2707. if (unlikely(!READ_ONCE(rcu_scheduler_fully_active)))
  2708. return;
  2709. if (likely(!rsp->boost)) {
  2710. rcu_do_batch(rsp, rdp);
  2711. return;
  2712. }
  2713. invoke_rcu_callbacks_kthread();
  2714. }
  2715. static void invoke_rcu_core(void)
  2716. {
  2717. if (cpu_online(smp_processor_id()))
  2718. raise_softirq(RCU_SOFTIRQ);
  2719. }
  2720. /*
  2721. * Handle any core-RCU processing required by a call_rcu() invocation.
  2722. */
  2723. static void __call_rcu_core(struct rcu_state *rsp, struct rcu_data *rdp,
  2724. struct rcu_head *head, unsigned long flags)
  2725. {
  2726. bool needwake;
  2727. /*
  2728. * If called from an extended quiescent state, invoke the RCU
  2729. * core in order to force a re-evaluation of RCU's idleness.
  2730. */
  2731. if (!rcu_is_watching())
  2732. invoke_rcu_core();
  2733. /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
  2734. if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
  2735. return;
  2736. /*
  2737. * Force the grace period if too many callbacks or too long waiting.
  2738. * Enforce hysteresis, and don't invoke force_quiescent_state()
  2739. * if some other CPU has recently done so. Also, don't bother
  2740. * invoking force_quiescent_state() if the newly enqueued callback
  2741. * is the only one waiting for a grace period to complete.
  2742. */
  2743. if (unlikely(rdp->qlen > rdp->qlen_last_fqs_check + qhimark)) {
  2744. /* Are we ignoring a completed grace period? */
  2745. note_gp_changes(rsp, rdp);
  2746. /* Start a new grace period if one not already started. */
  2747. if (!rcu_gp_in_progress(rsp)) {
  2748. struct rcu_node *rnp_root = rcu_get_root(rsp);
  2749. raw_spin_lock_rcu_node(rnp_root);
  2750. needwake = rcu_start_gp(rsp);
  2751. raw_spin_unlock_rcu_node(rnp_root);
  2752. if (needwake)
  2753. rcu_gp_kthread_wake(rsp);
  2754. } else {
  2755. /* Give the grace period a kick. */
  2756. rdp->blimit = LONG_MAX;
  2757. if (rsp->n_force_qs == rdp->n_force_qs_snap &&
  2758. *rdp->nxttail[RCU_DONE_TAIL] != head)
  2759. force_quiescent_state(rsp);
  2760. rdp->n_force_qs_snap = rsp->n_force_qs;
  2761. rdp->qlen_last_fqs_check = rdp->qlen;
  2762. }
  2763. }
  2764. }
  2765. /*
  2766. * RCU callback function to leak a callback.
  2767. */
  2768. static void rcu_leak_callback(struct rcu_head *rhp)
  2769. {
  2770. }
  2771. /*
  2772. * Helper function for call_rcu() and friends. The cpu argument will
  2773. * normally be -1, indicating "currently running CPU". It may specify
  2774. * a CPU only if that CPU is a no-CBs CPU. Currently, only _rcu_barrier()
  2775. * is expected to specify a CPU.
  2776. */
  2777. static void
  2778. __call_rcu(struct rcu_head *head, rcu_callback_t func,
  2779. struct rcu_state *rsp, int cpu, bool lazy)
  2780. {
  2781. unsigned long flags;
  2782. struct rcu_data *rdp;
  2783. WARN_ON_ONCE((unsigned long)head & 0x1); /* Misaligned rcu_head! */
  2784. if (debug_rcu_head_queue(head)) {
  2785. /* Probable double call_rcu(), so leak the callback. */
  2786. WRITE_ONCE(head->func, rcu_leak_callback);
  2787. WARN_ONCE(1, "__call_rcu(): Leaked duplicate callback\n");
  2788. return;
  2789. }
  2790. head->func = func;
  2791. head->next = NULL;
  2792. /*
  2793. * Opportunistically note grace-period endings and beginnings.
  2794. * Note that we might see a beginning right after we see an
  2795. * end, but never vice versa, since this CPU has to pass through
  2796. * a quiescent state betweentimes.
  2797. */
  2798. local_irq_save(flags);
  2799. rdp = this_cpu_ptr(rsp->rda);
  2800. /* Add the callback to our list. */
  2801. if (unlikely(rdp->nxttail[RCU_NEXT_TAIL] == NULL) || cpu != -1) {
  2802. int offline;
  2803. if (cpu != -1)
  2804. rdp = per_cpu_ptr(rsp->rda, cpu);
  2805. if (likely(rdp->mynode)) {
  2806. /* Post-boot, so this should be for a no-CBs CPU. */
  2807. offline = !__call_rcu_nocb(rdp, head, lazy, flags);
  2808. WARN_ON_ONCE(offline);
  2809. /* Offline CPU, _call_rcu() illegal, leak callback. */
  2810. local_irq_restore(flags);
  2811. return;
  2812. }
  2813. /*
  2814. * Very early boot, before rcu_init(). Initialize if needed
  2815. * and then drop through to queue the callback.
  2816. */
  2817. BUG_ON(cpu != -1);
  2818. WARN_ON_ONCE(!rcu_is_watching());
  2819. if (!likely(rdp->nxtlist))
  2820. init_default_callback_list(rdp);
  2821. }
  2822. WRITE_ONCE(rdp->qlen, rdp->qlen + 1);
  2823. if (lazy)
  2824. rdp->qlen_lazy++;
  2825. else
  2826. rcu_idle_count_callbacks_posted();
  2827. smp_mb(); /* Count before adding callback for rcu_barrier(). */
  2828. *rdp->nxttail[RCU_NEXT_TAIL] = head;
  2829. rdp->nxttail[RCU_NEXT_TAIL] = &head->next;
  2830. if (__is_kfree_rcu_offset((unsigned long)func))
  2831. trace_rcu_kfree_callback(rsp->name, head, (unsigned long)func,
  2832. rdp->qlen_lazy, rdp->qlen);
  2833. else
  2834. trace_rcu_callback(rsp->name, head, rdp->qlen_lazy, rdp->qlen);
  2835. /* Go handle any RCU core processing required. */
  2836. __call_rcu_core(rsp, rdp, head, flags);
  2837. local_irq_restore(flags);
  2838. }
  2839. /*
  2840. * Queue an RCU-sched callback for invocation after a grace period.
  2841. */
  2842. void call_rcu_sched(struct rcu_head *head, rcu_callback_t func)
  2843. {
  2844. __call_rcu(head, func, &rcu_sched_state, -1, 0);
  2845. }
  2846. EXPORT_SYMBOL_GPL(call_rcu_sched);
  2847. /*
  2848. * Queue an RCU callback for invocation after a quicker grace period.
  2849. */
  2850. void call_rcu_bh(struct rcu_head *head, rcu_callback_t func)
  2851. {
  2852. __call_rcu(head, func, &rcu_bh_state, -1, 0);
  2853. }
  2854. EXPORT_SYMBOL_GPL(call_rcu_bh);
  2855. /*
  2856. * Queue an RCU callback for lazy invocation after a grace period.
  2857. * This will likely be later named something like "call_rcu_lazy()",
  2858. * but this change will require some way of tagging the lazy RCU
  2859. * callbacks in the list of pending callbacks. Until then, this
  2860. * function may only be called from __kfree_rcu().
  2861. */
  2862. void kfree_call_rcu(struct rcu_head *head,
  2863. rcu_callback_t func)
  2864. {
  2865. __call_rcu(head, func, rcu_state_p, -1, 1);
  2866. }
  2867. EXPORT_SYMBOL_GPL(kfree_call_rcu);
  2868. /*
  2869. * Because a context switch is a grace period for RCU-sched and RCU-bh,
  2870. * any blocking grace-period wait automatically implies a grace period
  2871. * if there is only one CPU online at any point time during execution
  2872. * of either synchronize_sched() or synchronize_rcu_bh(). It is OK to
  2873. * occasionally incorrectly indicate that there are multiple CPUs online
  2874. * when there was in fact only one the whole time, as this just adds
  2875. * some overhead: RCU still operates correctly.
  2876. */
  2877. static inline int rcu_blocking_is_gp(void)
  2878. {
  2879. int ret;
  2880. might_sleep(); /* Check for RCU read-side critical section. */
  2881. preempt_disable();
  2882. ret = num_online_cpus() <= 1;
  2883. preempt_enable();
  2884. return ret;
  2885. }
  2886. /**
  2887. * synchronize_sched - wait until an rcu-sched grace period has elapsed.
  2888. *
  2889. * Control will return to the caller some time after a full rcu-sched
  2890. * grace period has elapsed, in other words after all currently executing
  2891. * rcu-sched read-side critical sections have completed. These read-side
  2892. * critical sections are delimited by rcu_read_lock_sched() and
  2893. * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
  2894. * local_irq_disable(), and so on may be used in place of
  2895. * rcu_read_lock_sched().
  2896. *
  2897. * This means that all preempt_disable code sequences, including NMI and
  2898. * non-threaded hardware-interrupt handlers, in progress on entry will
  2899. * have completed before this primitive returns. However, this does not
  2900. * guarantee that softirq handlers will have completed, since in some
  2901. * kernels, these handlers can run in process context, and can block.
  2902. *
  2903. * Note that this guarantee implies further memory-ordering guarantees.
  2904. * On systems with more than one CPU, when synchronize_sched() returns,
  2905. * each CPU is guaranteed to have executed a full memory barrier since the
  2906. * end of its last RCU-sched read-side critical section whose beginning
  2907. * preceded the call to synchronize_sched(). In addition, each CPU having
  2908. * an RCU read-side critical section that extends beyond the return from
  2909. * synchronize_sched() is guaranteed to have executed a full memory barrier
  2910. * after the beginning of synchronize_sched() and before the beginning of
  2911. * that RCU read-side critical section. Note that these guarantees include
  2912. * CPUs that are offline, idle, or executing in user mode, as well as CPUs
  2913. * that are executing in the kernel.
  2914. *
  2915. * Furthermore, if CPU A invoked synchronize_sched(), which returned
  2916. * to its caller on CPU B, then both CPU A and CPU B are guaranteed
  2917. * to have executed a full memory barrier during the execution of
  2918. * synchronize_sched() -- even if CPU A and CPU B are the same CPU (but
  2919. * again only if the system has more than one CPU).
  2920. *
  2921. * This primitive provides the guarantees made by the (now removed)
  2922. * synchronize_kernel() API. In contrast, synchronize_rcu() only
  2923. * guarantees that rcu_read_lock() sections will have completed.
  2924. * In "classic RCU", these two guarantees happen to be one and
  2925. * the same, but can differ in realtime RCU implementations.
  2926. */
  2927. void synchronize_sched(void)
  2928. {
  2929. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
  2930. lock_is_held(&rcu_lock_map) ||
  2931. lock_is_held(&rcu_sched_lock_map),
  2932. "Illegal synchronize_sched() in RCU-sched read-side critical section");
  2933. if (rcu_blocking_is_gp())
  2934. return;
  2935. if (rcu_gp_is_expedited())
  2936. synchronize_sched_expedited();
  2937. else
  2938. wait_rcu_gp(call_rcu_sched);
  2939. }
  2940. EXPORT_SYMBOL_GPL(synchronize_sched);
  2941. /**
  2942. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  2943. *
  2944. * Control will return to the caller some time after a full rcu_bh grace
  2945. * period has elapsed, in other words after all currently executing rcu_bh
  2946. * read-side critical sections have completed. RCU read-side critical
  2947. * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
  2948. * and may be nested.
  2949. *
  2950. * See the description of synchronize_sched() for more detailed information
  2951. * on memory ordering guarantees.
  2952. */
  2953. void synchronize_rcu_bh(void)
  2954. {
  2955. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
  2956. lock_is_held(&rcu_lock_map) ||
  2957. lock_is_held(&rcu_sched_lock_map),
  2958. "Illegal synchronize_rcu_bh() in RCU-bh read-side critical section");
  2959. if (rcu_blocking_is_gp())
  2960. return;
  2961. if (rcu_gp_is_expedited())
  2962. synchronize_rcu_bh_expedited();
  2963. else
  2964. wait_rcu_gp(call_rcu_bh);
  2965. }
  2966. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  2967. /**
  2968. * get_state_synchronize_rcu - Snapshot current RCU state
  2969. *
  2970. * Returns a cookie that is used by a later call to cond_synchronize_rcu()
  2971. * to determine whether or not a full grace period has elapsed in the
  2972. * meantime.
  2973. */
  2974. unsigned long get_state_synchronize_rcu(void)
  2975. {
  2976. /*
  2977. * Any prior manipulation of RCU-protected data must happen
  2978. * before the load from ->gpnum.
  2979. */
  2980. smp_mb(); /* ^^^ */
  2981. /*
  2982. * Make sure this load happens before the purportedly
  2983. * time-consuming work between get_state_synchronize_rcu()
  2984. * and cond_synchronize_rcu().
  2985. */
  2986. return smp_load_acquire(&rcu_state_p->gpnum);
  2987. }
  2988. EXPORT_SYMBOL_GPL(get_state_synchronize_rcu);
  2989. /**
  2990. * cond_synchronize_rcu - Conditionally wait for an RCU grace period
  2991. *
  2992. * @oldstate: return value from earlier call to get_state_synchronize_rcu()
  2993. *
  2994. * If a full RCU grace period has elapsed since the earlier call to
  2995. * get_state_synchronize_rcu(), just return. Otherwise, invoke
  2996. * synchronize_rcu() to wait for a full grace period.
  2997. *
  2998. * Yes, this function does not take counter wrap into account. But
  2999. * counter wrap is harmless. If the counter wraps, we have waited for
  3000. * more than 2 billion grace periods (and way more on a 64-bit system!),
  3001. * so waiting for one additional grace period should be just fine.
  3002. */
  3003. void cond_synchronize_rcu(unsigned long oldstate)
  3004. {
  3005. unsigned long newstate;
  3006. /*
  3007. * Ensure that this load happens before any RCU-destructive
  3008. * actions the caller might carry out after we return.
  3009. */
  3010. newstate = smp_load_acquire(&rcu_state_p->completed);
  3011. if (ULONG_CMP_GE(oldstate, newstate))
  3012. synchronize_rcu();
  3013. }
  3014. EXPORT_SYMBOL_GPL(cond_synchronize_rcu);
  3015. /**
  3016. * get_state_synchronize_sched - Snapshot current RCU-sched state
  3017. *
  3018. * Returns a cookie that is used by a later call to cond_synchronize_sched()
  3019. * to determine whether or not a full grace period has elapsed in the
  3020. * meantime.
  3021. */
  3022. unsigned long get_state_synchronize_sched(void)
  3023. {
  3024. /*
  3025. * Any prior manipulation of RCU-protected data must happen
  3026. * before the load from ->gpnum.
  3027. */
  3028. smp_mb(); /* ^^^ */
  3029. /*
  3030. * Make sure this load happens before the purportedly
  3031. * time-consuming work between get_state_synchronize_sched()
  3032. * and cond_synchronize_sched().
  3033. */
  3034. return smp_load_acquire(&rcu_sched_state.gpnum);
  3035. }
  3036. EXPORT_SYMBOL_GPL(get_state_synchronize_sched);
  3037. /**
  3038. * cond_synchronize_sched - Conditionally wait for an RCU-sched grace period
  3039. *
  3040. * @oldstate: return value from earlier call to get_state_synchronize_sched()
  3041. *
  3042. * If a full RCU-sched grace period has elapsed since the earlier call to
  3043. * get_state_synchronize_sched(), just return. Otherwise, invoke
  3044. * synchronize_sched() to wait for a full grace period.
  3045. *
  3046. * Yes, this function does not take counter wrap into account. But
  3047. * counter wrap is harmless. If the counter wraps, we have waited for
  3048. * more than 2 billion grace periods (and way more on a 64-bit system!),
  3049. * so waiting for one additional grace period should be just fine.
  3050. */
  3051. void cond_synchronize_sched(unsigned long oldstate)
  3052. {
  3053. unsigned long newstate;
  3054. /*
  3055. * Ensure that this load happens before any RCU-destructive
  3056. * actions the caller might carry out after we return.
  3057. */
  3058. newstate = smp_load_acquire(&rcu_sched_state.completed);
  3059. if (ULONG_CMP_GE(oldstate, newstate))
  3060. synchronize_sched();
  3061. }
  3062. EXPORT_SYMBOL_GPL(cond_synchronize_sched);
  3063. /* Adjust sequence number for start of update-side operation. */
  3064. static void rcu_seq_start(unsigned long *sp)
  3065. {
  3066. WRITE_ONCE(*sp, *sp + 1);
  3067. smp_mb(); /* Ensure update-side operation after counter increment. */
  3068. WARN_ON_ONCE(!(*sp & 0x1));
  3069. }
  3070. /* Adjust sequence number for end of update-side operation. */
  3071. static void rcu_seq_end(unsigned long *sp)
  3072. {
  3073. smp_mb(); /* Ensure update-side operation before counter increment. */
  3074. WRITE_ONCE(*sp, *sp + 1);
  3075. WARN_ON_ONCE(*sp & 0x1);
  3076. }
  3077. /* Take a snapshot of the update side's sequence number. */
  3078. static unsigned long rcu_seq_snap(unsigned long *sp)
  3079. {
  3080. unsigned long s;
  3081. s = (READ_ONCE(*sp) + 3) & ~0x1;
  3082. smp_mb(); /* Above access must not bleed into critical section. */
  3083. return s;
  3084. }
  3085. /*
  3086. * Given a snapshot from rcu_seq_snap(), determine whether or not a
  3087. * full update-side operation has occurred.
  3088. */
  3089. static bool rcu_seq_done(unsigned long *sp, unsigned long s)
  3090. {
  3091. return ULONG_CMP_GE(READ_ONCE(*sp), s);
  3092. }
  3093. /* Wrapper functions for expedited grace periods. */
  3094. static void rcu_exp_gp_seq_start(struct rcu_state *rsp)
  3095. {
  3096. rcu_seq_start(&rsp->expedited_sequence);
  3097. }
  3098. static void rcu_exp_gp_seq_end(struct rcu_state *rsp)
  3099. {
  3100. rcu_seq_end(&rsp->expedited_sequence);
  3101. smp_mb(); /* Ensure that consecutive grace periods serialize. */
  3102. }
  3103. static unsigned long rcu_exp_gp_seq_snap(struct rcu_state *rsp)
  3104. {
  3105. smp_mb(); /* Caller's modifications seen first by other CPUs. */
  3106. return rcu_seq_snap(&rsp->expedited_sequence);
  3107. }
  3108. static bool rcu_exp_gp_seq_done(struct rcu_state *rsp, unsigned long s)
  3109. {
  3110. return rcu_seq_done(&rsp->expedited_sequence, s);
  3111. }
  3112. /*
  3113. * Reset the ->expmaskinit values in the rcu_node tree to reflect any
  3114. * recent CPU-online activity. Note that these masks are not cleared
  3115. * when CPUs go offline, so they reflect the union of all CPUs that have
  3116. * ever been online. This means that this function normally takes its
  3117. * no-work-to-do fastpath.
  3118. */
  3119. static void sync_exp_reset_tree_hotplug(struct rcu_state *rsp)
  3120. {
  3121. bool done;
  3122. unsigned long flags;
  3123. unsigned long mask;
  3124. unsigned long oldmask;
  3125. int ncpus = READ_ONCE(rsp->ncpus);
  3126. struct rcu_node *rnp;
  3127. struct rcu_node *rnp_up;
  3128. /* If no new CPUs onlined since last time, nothing to do. */
  3129. if (likely(ncpus == rsp->ncpus_snap))
  3130. return;
  3131. rsp->ncpus_snap = ncpus;
  3132. /*
  3133. * Each pass through the following loop propagates newly onlined
  3134. * CPUs for the current rcu_node structure up the rcu_node tree.
  3135. */
  3136. rcu_for_each_leaf_node(rsp, rnp) {
  3137. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3138. if (rnp->expmaskinit == rnp->expmaskinitnext) {
  3139. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3140. continue; /* No new CPUs, nothing to do. */
  3141. }
  3142. /* Update this node's mask, track old value for propagation. */
  3143. oldmask = rnp->expmaskinit;
  3144. rnp->expmaskinit = rnp->expmaskinitnext;
  3145. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3146. /* If was already nonzero, nothing to propagate. */
  3147. if (oldmask)
  3148. continue;
  3149. /* Propagate the new CPU up the tree. */
  3150. mask = rnp->grpmask;
  3151. rnp_up = rnp->parent;
  3152. done = false;
  3153. while (rnp_up) {
  3154. raw_spin_lock_irqsave_rcu_node(rnp_up, flags);
  3155. if (rnp_up->expmaskinit)
  3156. done = true;
  3157. rnp_up->expmaskinit |= mask;
  3158. raw_spin_unlock_irqrestore_rcu_node(rnp_up, flags);
  3159. if (done)
  3160. break;
  3161. mask = rnp_up->grpmask;
  3162. rnp_up = rnp_up->parent;
  3163. }
  3164. }
  3165. }
  3166. /*
  3167. * Reset the ->expmask values in the rcu_node tree in preparation for
  3168. * a new expedited grace period.
  3169. */
  3170. static void __maybe_unused sync_exp_reset_tree(struct rcu_state *rsp)
  3171. {
  3172. unsigned long flags;
  3173. struct rcu_node *rnp;
  3174. sync_exp_reset_tree_hotplug(rsp);
  3175. rcu_for_each_node_breadth_first(rsp, rnp) {
  3176. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3177. WARN_ON_ONCE(rnp->expmask);
  3178. rnp->expmask = rnp->expmaskinit;
  3179. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3180. }
  3181. }
  3182. /*
  3183. * Return non-zero if there is no RCU expedited grace period in progress
  3184. * for the specified rcu_node structure, in other words, if all CPUs and
  3185. * tasks covered by the specified rcu_node structure have done their bit
  3186. * for the current expedited grace period. Works only for preemptible
  3187. * RCU -- other RCU implementation use other means.
  3188. *
  3189. * Caller must hold the root rcu_node's exp_funnel_mutex.
  3190. */
  3191. static int sync_rcu_preempt_exp_done(struct rcu_node *rnp)
  3192. {
  3193. return rnp->exp_tasks == NULL &&
  3194. READ_ONCE(rnp->expmask) == 0;
  3195. }
  3196. /*
  3197. * Report the exit from RCU read-side critical section for the last task
  3198. * that queued itself during or before the current expedited preemptible-RCU
  3199. * grace period. This event is reported either to the rcu_node structure on
  3200. * which the task was queued or to one of that rcu_node structure's ancestors,
  3201. * recursively up the tree. (Calm down, calm down, we do the recursion
  3202. * iteratively!)
  3203. *
  3204. * Caller must hold the root rcu_node's exp_funnel_mutex and the
  3205. * specified rcu_node structure's ->lock.
  3206. */
  3207. static void __rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
  3208. bool wake, unsigned long flags)
  3209. __releases(rnp->lock)
  3210. {
  3211. unsigned long mask;
  3212. for (;;) {
  3213. if (!sync_rcu_preempt_exp_done(rnp)) {
  3214. if (!rnp->expmask)
  3215. rcu_initiate_boost(rnp, flags);
  3216. else
  3217. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3218. break;
  3219. }
  3220. if (rnp->parent == NULL) {
  3221. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3222. if (wake) {
  3223. smp_mb(); /* EGP done before wake_up(). */
  3224. swake_up(&rsp->expedited_wq);
  3225. }
  3226. break;
  3227. }
  3228. mask = rnp->grpmask;
  3229. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled */
  3230. rnp = rnp->parent;
  3231. raw_spin_lock_rcu_node(rnp); /* irqs already disabled */
  3232. WARN_ON_ONCE(!(rnp->expmask & mask));
  3233. rnp->expmask &= ~mask;
  3234. }
  3235. }
  3236. /*
  3237. * Report expedited quiescent state for specified node. This is a
  3238. * lock-acquisition wrapper function for __rcu_report_exp_rnp().
  3239. *
  3240. * Caller must hold the root rcu_node's exp_funnel_mutex.
  3241. */
  3242. static void __maybe_unused rcu_report_exp_rnp(struct rcu_state *rsp,
  3243. struct rcu_node *rnp, bool wake)
  3244. {
  3245. unsigned long flags;
  3246. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3247. __rcu_report_exp_rnp(rsp, rnp, wake, flags);
  3248. }
  3249. /*
  3250. * Report expedited quiescent state for multiple CPUs, all covered by the
  3251. * specified leaf rcu_node structure. Caller must hold the root
  3252. * rcu_node's exp_funnel_mutex.
  3253. */
  3254. static void rcu_report_exp_cpu_mult(struct rcu_state *rsp, struct rcu_node *rnp,
  3255. unsigned long mask, bool wake)
  3256. {
  3257. unsigned long flags;
  3258. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3259. if (!(rnp->expmask & mask)) {
  3260. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3261. return;
  3262. }
  3263. rnp->expmask &= ~mask;
  3264. __rcu_report_exp_rnp(rsp, rnp, wake, flags); /* Releases rnp->lock. */
  3265. }
  3266. /*
  3267. * Report expedited quiescent state for specified rcu_data (CPU).
  3268. * Caller must hold the root rcu_node's exp_funnel_mutex.
  3269. */
  3270. static void rcu_report_exp_rdp(struct rcu_state *rsp, struct rcu_data *rdp,
  3271. bool wake)
  3272. {
  3273. rcu_report_exp_cpu_mult(rsp, rdp->mynode, rdp->grpmask, wake);
  3274. }
  3275. /* Common code for synchronize_{rcu,sched}_expedited() work-done checking. */
  3276. static bool sync_exp_work_done(struct rcu_state *rsp, struct rcu_node *rnp,
  3277. struct rcu_data *rdp,
  3278. atomic_long_t *stat, unsigned long s)
  3279. {
  3280. if (rcu_exp_gp_seq_done(rsp, s)) {
  3281. if (rnp)
  3282. mutex_unlock(&rnp->exp_funnel_mutex);
  3283. else if (rdp)
  3284. mutex_unlock(&rdp->exp_funnel_mutex);
  3285. /* Ensure test happens before caller kfree(). */
  3286. smp_mb__before_atomic(); /* ^^^ */
  3287. atomic_long_inc(stat);
  3288. return true;
  3289. }
  3290. return false;
  3291. }
  3292. /*
  3293. * Funnel-lock acquisition for expedited grace periods. Returns a
  3294. * pointer to the root rcu_node structure, or NULL if some other
  3295. * task did the expedited grace period for us.
  3296. */
  3297. static struct rcu_node *exp_funnel_lock(struct rcu_state *rsp, unsigned long s)
  3298. {
  3299. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, raw_smp_processor_id());
  3300. struct rcu_node *rnp0;
  3301. struct rcu_node *rnp1 = NULL;
  3302. /*
  3303. * First try directly acquiring the root lock in order to reduce
  3304. * latency in the common case where expedited grace periods are
  3305. * rare. We check mutex_is_locked() to avoid pathological levels of
  3306. * memory contention on ->exp_funnel_mutex in the heavy-load case.
  3307. */
  3308. rnp0 = rcu_get_root(rsp);
  3309. if (!mutex_is_locked(&rnp0->exp_funnel_mutex)) {
  3310. if (mutex_trylock(&rnp0->exp_funnel_mutex)) {
  3311. if (sync_exp_work_done(rsp, rnp0, NULL,
  3312. &rdp->expedited_workdone0, s))
  3313. return NULL;
  3314. return rnp0;
  3315. }
  3316. }
  3317. /*
  3318. * Each pass through the following loop works its way
  3319. * up the rcu_node tree, returning if others have done the
  3320. * work or otherwise falls through holding the root rnp's
  3321. * ->exp_funnel_mutex. The mapping from CPU to rcu_node structure
  3322. * can be inexact, as it is just promoting locality and is not
  3323. * strictly needed for correctness.
  3324. */
  3325. if (sync_exp_work_done(rsp, NULL, NULL, &rdp->expedited_workdone1, s))
  3326. return NULL;
  3327. mutex_lock(&rdp->exp_funnel_mutex);
  3328. rnp0 = rdp->mynode;
  3329. for (; rnp0 != NULL; rnp0 = rnp0->parent) {
  3330. if (sync_exp_work_done(rsp, rnp1, rdp,
  3331. &rdp->expedited_workdone2, s))
  3332. return NULL;
  3333. mutex_lock(&rnp0->exp_funnel_mutex);
  3334. if (rnp1)
  3335. mutex_unlock(&rnp1->exp_funnel_mutex);
  3336. else
  3337. mutex_unlock(&rdp->exp_funnel_mutex);
  3338. rnp1 = rnp0;
  3339. }
  3340. if (sync_exp_work_done(rsp, rnp1, rdp,
  3341. &rdp->expedited_workdone3, s))
  3342. return NULL;
  3343. return rnp1;
  3344. }
  3345. /* Invoked on each online non-idle CPU for expedited quiescent state. */
  3346. static void sync_sched_exp_handler(void *data)
  3347. {
  3348. struct rcu_data *rdp;
  3349. struct rcu_node *rnp;
  3350. struct rcu_state *rsp = data;
  3351. rdp = this_cpu_ptr(rsp->rda);
  3352. rnp = rdp->mynode;
  3353. if (!(READ_ONCE(rnp->expmask) & rdp->grpmask) ||
  3354. __this_cpu_read(rcu_sched_data.cpu_no_qs.b.exp))
  3355. return;
  3356. __this_cpu_write(rcu_sched_data.cpu_no_qs.b.exp, true);
  3357. resched_cpu(smp_processor_id());
  3358. }
  3359. /* Send IPI for expedited cleanup if needed at end of CPU-hotplug operation. */
  3360. static void sync_sched_exp_online_cleanup(int cpu)
  3361. {
  3362. struct rcu_data *rdp;
  3363. int ret;
  3364. struct rcu_node *rnp;
  3365. struct rcu_state *rsp = &rcu_sched_state;
  3366. rdp = per_cpu_ptr(rsp->rda, cpu);
  3367. rnp = rdp->mynode;
  3368. if (!(READ_ONCE(rnp->expmask) & rdp->grpmask))
  3369. return;
  3370. ret = smp_call_function_single(cpu, sync_sched_exp_handler, rsp, 0);
  3371. WARN_ON_ONCE(ret);
  3372. }
  3373. /*
  3374. * Select the nodes that the upcoming expedited grace period needs
  3375. * to wait for.
  3376. */
  3377. static void sync_rcu_exp_select_cpus(struct rcu_state *rsp,
  3378. smp_call_func_t func)
  3379. {
  3380. int cpu;
  3381. unsigned long flags;
  3382. unsigned long mask;
  3383. unsigned long mask_ofl_test;
  3384. unsigned long mask_ofl_ipi;
  3385. int ret;
  3386. struct rcu_node *rnp;
  3387. sync_exp_reset_tree(rsp);
  3388. rcu_for_each_leaf_node(rsp, rnp) {
  3389. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3390. /* Each pass checks a CPU for identity, offline, and idle. */
  3391. mask_ofl_test = 0;
  3392. for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++) {
  3393. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  3394. struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
  3395. if (raw_smp_processor_id() == cpu ||
  3396. !(atomic_add_return(0, &rdtp->dynticks) & 0x1))
  3397. mask_ofl_test |= rdp->grpmask;
  3398. }
  3399. mask_ofl_ipi = rnp->expmask & ~mask_ofl_test;
  3400. /*
  3401. * Need to wait for any blocked tasks as well. Note that
  3402. * additional blocking tasks will also block the expedited
  3403. * GP until such time as the ->expmask bits are cleared.
  3404. */
  3405. if (rcu_preempt_has_tasks(rnp))
  3406. rnp->exp_tasks = rnp->blkd_tasks.next;
  3407. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3408. /* IPI the remaining CPUs for expedited quiescent state. */
  3409. mask = 1;
  3410. for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask <<= 1) {
  3411. if (!(mask_ofl_ipi & mask))
  3412. continue;
  3413. retry_ipi:
  3414. ret = smp_call_function_single(cpu, func, rsp, 0);
  3415. if (!ret) {
  3416. mask_ofl_ipi &= ~mask;
  3417. continue;
  3418. }
  3419. /* Failed, raced with offline. */
  3420. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3421. if (cpu_online(cpu) &&
  3422. (rnp->expmask & mask)) {
  3423. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3424. schedule_timeout_uninterruptible(1);
  3425. if (cpu_online(cpu) &&
  3426. (rnp->expmask & mask))
  3427. goto retry_ipi;
  3428. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3429. }
  3430. if (!(rnp->expmask & mask))
  3431. mask_ofl_ipi &= ~mask;
  3432. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3433. }
  3434. /* Report quiescent states for those that went offline. */
  3435. mask_ofl_test |= mask_ofl_ipi;
  3436. if (mask_ofl_test)
  3437. rcu_report_exp_cpu_mult(rsp, rnp, mask_ofl_test, false);
  3438. }
  3439. }
  3440. static void synchronize_sched_expedited_wait(struct rcu_state *rsp)
  3441. {
  3442. int cpu;
  3443. unsigned long jiffies_stall;
  3444. unsigned long jiffies_start;
  3445. unsigned long mask;
  3446. int ndetected;
  3447. struct rcu_node *rnp;
  3448. struct rcu_node *rnp_root = rcu_get_root(rsp);
  3449. int ret;
  3450. jiffies_stall = rcu_jiffies_till_stall_check();
  3451. jiffies_start = jiffies;
  3452. for (;;) {
  3453. ret = swait_event_timeout(
  3454. rsp->expedited_wq,
  3455. sync_rcu_preempt_exp_done(rnp_root),
  3456. jiffies_stall);
  3457. if (ret > 0 || sync_rcu_preempt_exp_done(rnp_root))
  3458. return;
  3459. if (ret < 0) {
  3460. /* Hit a signal, disable CPU stall warnings. */
  3461. swait_event(rsp->expedited_wq,
  3462. sync_rcu_preempt_exp_done(rnp_root));
  3463. return;
  3464. }
  3465. pr_err("INFO: %s detected expedited stalls on CPUs/tasks: {",
  3466. rsp->name);
  3467. ndetected = 0;
  3468. rcu_for_each_leaf_node(rsp, rnp) {
  3469. ndetected = rcu_print_task_exp_stall(rnp);
  3470. mask = 1;
  3471. for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask <<= 1) {
  3472. struct rcu_data *rdp;
  3473. if (!(rnp->expmask & mask))
  3474. continue;
  3475. ndetected++;
  3476. rdp = per_cpu_ptr(rsp->rda, cpu);
  3477. pr_cont(" %d-%c%c%c", cpu,
  3478. "O."[cpu_online(cpu)],
  3479. "o."[!!(rdp->grpmask & rnp->expmaskinit)],
  3480. "N."[!!(rdp->grpmask & rnp->expmaskinitnext)]);
  3481. }
  3482. mask <<= 1;
  3483. }
  3484. pr_cont(" } %lu jiffies s: %lu root: %#lx/%c\n",
  3485. jiffies - jiffies_start, rsp->expedited_sequence,
  3486. rnp_root->expmask, ".T"[!!rnp_root->exp_tasks]);
  3487. if (!ndetected) {
  3488. pr_err("blocking rcu_node structures:");
  3489. rcu_for_each_node_breadth_first(rsp, rnp) {
  3490. if (rnp == rnp_root)
  3491. continue; /* printed unconditionally */
  3492. if (sync_rcu_preempt_exp_done(rnp))
  3493. continue;
  3494. pr_cont(" l=%u:%d-%d:%#lx/%c",
  3495. rnp->level, rnp->grplo, rnp->grphi,
  3496. rnp->expmask,
  3497. ".T"[!!rnp->exp_tasks]);
  3498. }
  3499. pr_cont("\n");
  3500. }
  3501. rcu_for_each_leaf_node(rsp, rnp) {
  3502. mask = 1;
  3503. for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask <<= 1) {
  3504. if (!(rnp->expmask & mask))
  3505. continue;
  3506. dump_cpu_task(cpu);
  3507. }
  3508. }
  3509. jiffies_stall = 3 * rcu_jiffies_till_stall_check() + 3;
  3510. }
  3511. }
  3512. /**
  3513. * synchronize_sched_expedited - Brute-force RCU-sched grace period
  3514. *
  3515. * Wait for an RCU-sched grace period to elapse, but use a "big hammer"
  3516. * approach to force the grace period to end quickly. This consumes
  3517. * significant time on all CPUs and is unfriendly to real-time workloads,
  3518. * so is thus not recommended for any sort of common-case code. In fact,
  3519. * if you are using synchronize_sched_expedited() in a loop, please
  3520. * restructure your code to batch your updates, and then use a single
  3521. * synchronize_sched() instead.
  3522. *
  3523. * This implementation can be thought of as an application of sequence
  3524. * locking to expedited grace periods, but using the sequence counter to
  3525. * determine when someone else has already done the work instead of for
  3526. * retrying readers.
  3527. */
  3528. void synchronize_sched_expedited(void)
  3529. {
  3530. unsigned long s;
  3531. struct rcu_node *rnp;
  3532. struct rcu_state *rsp = &rcu_sched_state;
  3533. /* If only one CPU, this is automatically a grace period. */
  3534. if (rcu_blocking_is_gp())
  3535. return;
  3536. /* If expedited grace periods are prohibited, fall back to normal. */
  3537. if (rcu_gp_is_normal()) {
  3538. wait_rcu_gp(call_rcu_sched);
  3539. return;
  3540. }
  3541. /* Take a snapshot of the sequence number. */
  3542. s = rcu_exp_gp_seq_snap(rsp);
  3543. rnp = exp_funnel_lock(rsp, s);
  3544. if (rnp == NULL)
  3545. return; /* Someone else did our work for us. */
  3546. rcu_exp_gp_seq_start(rsp);
  3547. sync_rcu_exp_select_cpus(rsp, sync_sched_exp_handler);
  3548. synchronize_sched_expedited_wait(rsp);
  3549. rcu_exp_gp_seq_end(rsp);
  3550. mutex_unlock(&rnp->exp_funnel_mutex);
  3551. }
  3552. EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
  3553. /*
  3554. * Check to see if there is any immediate RCU-related work to be done
  3555. * by the current CPU, for the specified type of RCU, returning 1 if so.
  3556. * The checks are in order of increasing expense: checks that can be
  3557. * carried out against CPU-local state are performed first. However,
  3558. * we must check for CPU stalls first, else we might not get a chance.
  3559. */
  3560. static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
  3561. {
  3562. struct rcu_node *rnp = rdp->mynode;
  3563. rdp->n_rcu_pending++;
  3564. /* Check for CPU stalls, if enabled. */
  3565. check_cpu_stall(rsp, rdp);
  3566. /* Is this CPU a NO_HZ_FULL CPU that should ignore RCU? */
  3567. if (rcu_nohz_full_cpu(rsp))
  3568. return 0;
  3569. /* Is the RCU core waiting for a quiescent state from this CPU? */
  3570. if (rcu_scheduler_fully_active &&
  3571. rdp->core_needs_qs && rdp->cpu_no_qs.b.norm &&
  3572. rdp->rcu_qs_ctr_snap == __this_cpu_read(rcu_qs_ctr)) {
  3573. rdp->n_rp_core_needs_qs++;
  3574. } else if (rdp->core_needs_qs &&
  3575. (!rdp->cpu_no_qs.b.norm ||
  3576. rdp->rcu_qs_ctr_snap != __this_cpu_read(rcu_qs_ctr))) {
  3577. rdp->n_rp_report_qs++;
  3578. return 1;
  3579. }
  3580. /* Does this CPU have callbacks ready to invoke? */
  3581. if (cpu_has_callbacks_ready_to_invoke(rdp)) {
  3582. rdp->n_rp_cb_ready++;
  3583. return 1;
  3584. }
  3585. /* Has RCU gone idle with this CPU needing another grace period? */
  3586. if (cpu_needs_another_gp(rsp, rdp)) {
  3587. rdp->n_rp_cpu_needs_gp++;
  3588. return 1;
  3589. }
  3590. /* Has another RCU grace period completed? */
  3591. if (READ_ONCE(rnp->completed) != rdp->completed) { /* outside lock */
  3592. rdp->n_rp_gp_completed++;
  3593. return 1;
  3594. }
  3595. /* Has a new RCU grace period started? */
  3596. if (READ_ONCE(rnp->gpnum) != rdp->gpnum ||
  3597. unlikely(READ_ONCE(rdp->gpwrap))) { /* outside lock */
  3598. rdp->n_rp_gp_started++;
  3599. return 1;
  3600. }
  3601. /* Does this CPU need a deferred NOCB wakeup? */
  3602. if (rcu_nocb_need_deferred_wakeup(rdp)) {
  3603. rdp->n_rp_nocb_defer_wakeup++;
  3604. return 1;
  3605. }
  3606. /* nothing to do */
  3607. rdp->n_rp_need_nothing++;
  3608. return 0;
  3609. }
  3610. /*
  3611. * Check to see if there is any immediate RCU-related work to be done
  3612. * by the current CPU, returning 1 if so. This function is part of the
  3613. * RCU implementation; it is -not- an exported member of the RCU API.
  3614. */
  3615. static int rcu_pending(void)
  3616. {
  3617. struct rcu_state *rsp;
  3618. for_each_rcu_flavor(rsp)
  3619. if (__rcu_pending(rsp, this_cpu_ptr(rsp->rda)))
  3620. return 1;
  3621. return 0;
  3622. }
  3623. /*
  3624. * Return true if the specified CPU has any callback. If all_lazy is
  3625. * non-NULL, store an indication of whether all callbacks are lazy.
  3626. * (If there are no callbacks, all of them are deemed to be lazy.)
  3627. */
  3628. static bool __maybe_unused rcu_cpu_has_callbacks(bool *all_lazy)
  3629. {
  3630. bool al = true;
  3631. bool hc = false;
  3632. struct rcu_data *rdp;
  3633. struct rcu_state *rsp;
  3634. for_each_rcu_flavor(rsp) {
  3635. rdp = this_cpu_ptr(rsp->rda);
  3636. if (!rdp->nxtlist)
  3637. continue;
  3638. hc = true;
  3639. if (rdp->qlen != rdp->qlen_lazy || !all_lazy) {
  3640. al = false;
  3641. break;
  3642. }
  3643. }
  3644. if (all_lazy)
  3645. *all_lazy = al;
  3646. return hc;
  3647. }
  3648. /*
  3649. * Helper function for _rcu_barrier() tracing. If tracing is disabled,
  3650. * the compiler is expected to optimize this away.
  3651. */
  3652. static void _rcu_barrier_trace(struct rcu_state *rsp, const char *s,
  3653. int cpu, unsigned long done)
  3654. {
  3655. trace_rcu_barrier(rsp->name, s, cpu,
  3656. atomic_read(&rsp->barrier_cpu_count), done);
  3657. }
  3658. /*
  3659. * RCU callback function for _rcu_barrier(). If we are last, wake
  3660. * up the task executing _rcu_barrier().
  3661. */
  3662. static void rcu_barrier_callback(struct rcu_head *rhp)
  3663. {
  3664. struct rcu_data *rdp = container_of(rhp, struct rcu_data, barrier_head);
  3665. struct rcu_state *rsp = rdp->rsp;
  3666. if (atomic_dec_and_test(&rsp->barrier_cpu_count)) {
  3667. _rcu_barrier_trace(rsp, "LastCB", -1, rsp->barrier_sequence);
  3668. complete(&rsp->barrier_completion);
  3669. } else {
  3670. _rcu_barrier_trace(rsp, "CB", -1, rsp->barrier_sequence);
  3671. }
  3672. }
  3673. /*
  3674. * Called with preemption disabled, and from cross-cpu IRQ context.
  3675. */
  3676. static void rcu_barrier_func(void *type)
  3677. {
  3678. struct rcu_state *rsp = type;
  3679. struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
  3680. _rcu_barrier_trace(rsp, "IRQ", -1, rsp->barrier_sequence);
  3681. atomic_inc(&rsp->barrier_cpu_count);
  3682. rsp->call(&rdp->barrier_head, rcu_barrier_callback);
  3683. }
  3684. /*
  3685. * Orchestrate the specified type of RCU barrier, waiting for all
  3686. * RCU callbacks of the specified type to complete.
  3687. */
  3688. static void _rcu_barrier(struct rcu_state *rsp)
  3689. {
  3690. int cpu;
  3691. struct rcu_data *rdp;
  3692. unsigned long s = rcu_seq_snap(&rsp->barrier_sequence);
  3693. _rcu_barrier_trace(rsp, "Begin", -1, s);
  3694. /* Take mutex to serialize concurrent rcu_barrier() requests. */
  3695. mutex_lock(&rsp->barrier_mutex);
  3696. /* Did someone else do our work for us? */
  3697. if (rcu_seq_done(&rsp->barrier_sequence, s)) {
  3698. _rcu_barrier_trace(rsp, "EarlyExit", -1, rsp->barrier_sequence);
  3699. smp_mb(); /* caller's subsequent code after above check. */
  3700. mutex_unlock(&rsp->barrier_mutex);
  3701. return;
  3702. }
  3703. /* Mark the start of the barrier operation. */
  3704. rcu_seq_start(&rsp->barrier_sequence);
  3705. _rcu_barrier_trace(rsp, "Inc1", -1, rsp->barrier_sequence);
  3706. /*
  3707. * Initialize the count to one rather than to zero in order to
  3708. * avoid a too-soon return to zero in case of a short grace period
  3709. * (or preemption of this task). Exclude CPU-hotplug operations
  3710. * to ensure that no offline CPU has callbacks queued.
  3711. */
  3712. init_completion(&rsp->barrier_completion);
  3713. atomic_set(&rsp->barrier_cpu_count, 1);
  3714. get_online_cpus();
  3715. /*
  3716. * Force each CPU with callbacks to register a new callback.
  3717. * When that callback is invoked, we will know that all of the
  3718. * corresponding CPU's preceding callbacks have been invoked.
  3719. */
  3720. for_each_possible_cpu(cpu) {
  3721. if (!cpu_online(cpu) && !rcu_is_nocb_cpu(cpu))
  3722. continue;
  3723. rdp = per_cpu_ptr(rsp->rda, cpu);
  3724. if (rcu_is_nocb_cpu(cpu)) {
  3725. if (!rcu_nocb_cpu_needs_barrier(rsp, cpu)) {
  3726. _rcu_barrier_trace(rsp, "OfflineNoCB", cpu,
  3727. rsp->barrier_sequence);
  3728. } else {
  3729. _rcu_barrier_trace(rsp, "OnlineNoCB", cpu,
  3730. rsp->barrier_sequence);
  3731. smp_mb__before_atomic();
  3732. atomic_inc(&rsp->barrier_cpu_count);
  3733. __call_rcu(&rdp->barrier_head,
  3734. rcu_barrier_callback, rsp, cpu, 0);
  3735. }
  3736. } else if (READ_ONCE(rdp->qlen)) {
  3737. _rcu_barrier_trace(rsp, "OnlineQ", cpu,
  3738. rsp->barrier_sequence);
  3739. smp_call_function_single(cpu, rcu_barrier_func, rsp, 1);
  3740. } else {
  3741. _rcu_barrier_trace(rsp, "OnlineNQ", cpu,
  3742. rsp->barrier_sequence);
  3743. }
  3744. }
  3745. put_online_cpus();
  3746. /*
  3747. * Now that we have an rcu_barrier_callback() callback on each
  3748. * CPU, and thus each counted, remove the initial count.
  3749. */
  3750. if (atomic_dec_and_test(&rsp->barrier_cpu_count))
  3751. complete(&rsp->barrier_completion);
  3752. /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
  3753. wait_for_completion(&rsp->barrier_completion);
  3754. /* Mark the end of the barrier operation. */
  3755. _rcu_barrier_trace(rsp, "Inc2", -1, rsp->barrier_sequence);
  3756. rcu_seq_end(&rsp->barrier_sequence);
  3757. /* Other rcu_barrier() invocations can now safely proceed. */
  3758. mutex_unlock(&rsp->barrier_mutex);
  3759. }
  3760. /**
  3761. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  3762. */
  3763. void rcu_barrier_bh(void)
  3764. {
  3765. _rcu_barrier(&rcu_bh_state);
  3766. }
  3767. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  3768. /**
  3769. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  3770. */
  3771. void rcu_barrier_sched(void)
  3772. {
  3773. _rcu_barrier(&rcu_sched_state);
  3774. }
  3775. EXPORT_SYMBOL_GPL(rcu_barrier_sched);
  3776. /*
  3777. * Propagate ->qsinitmask bits up the rcu_node tree to account for the
  3778. * first CPU in a given leaf rcu_node structure coming online. The caller
  3779. * must hold the corresponding leaf rcu_node ->lock with interrrupts
  3780. * disabled.
  3781. */
  3782. static void rcu_init_new_rnp(struct rcu_node *rnp_leaf)
  3783. {
  3784. long mask;
  3785. struct rcu_node *rnp = rnp_leaf;
  3786. for (;;) {
  3787. mask = rnp->grpmask;
  3788. rnp = rnp->parent;
  3789. if (rnp == NULL)
  3790. return;
  3791. raw_spin_lock_rcu_node(rnp); /* Interrupts already disabled. */
  3792. rnp->qsmaskinit |= mask;
  3793. raw_spin_unlock_rcu_node(rnp); /* Interrupts remain disabled. */
  3794. }
  3795. }
  3796. /*
  3797. * Do boot-time initialization of a CPU's per-CPU RCU data.
  3798. */
  3799. static void __init
  3800. rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp)
  3801. {
  3802. unsigned long flags;
  3803. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  3804. struct rcu_node *rnp = rcu_get_root(rsp);
  3805. /* Set up local state, ensuring consistent view of global state. */
  3806. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3807. rdp->grpmask = 1UL << (cpu - rdp->mynode->grplo);
  3808. rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
  3809. WARN_ON_ONCE(rdp->dynticks->dynticks_nesting != DYNTICK_TASK_EXIT_IDLE);
  3810. WARN_ON_ONCE(atomic_read(&rdp->dynticks->dynticks) != 1);
  3811. rdp->cpu = cpu;
  3812. rdp->rsp = rsp;
  3813. mutex_init(&rdp->exp_funnel_mutex);
  3814. rcu_boot_init_nocb_percpu_data(rdp);
  3815. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3816. }
  3817. /*
  3818. * Initialize a CPU's per-CPU RCU data. Note that only one online or
  3819. * offline event can be happening at a given time. Note also that we
  3820. * can accept some slop in the rsp->completed access due to the fact
  3821. * that this CPU cannot possibly have any RCU callbacks in flight yet.
  3822. */
  3823. static void
  3824. rcu_init_percpu_data(int cpu, struct rcu_state *rsp)
  3825. {
  3826. unsigned long flags;
  3827. unsigned long mask;
  3828. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  3829. struct rcu_node *rnp = rcu_get_root(rsp);
  3830. /* Set up local state, ensuring consistent view of global state. */
  3831. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3832. rdp->qlen_last_fqs_check = 0;
  3833. rdp->n_force_qs_snap = rsp->n_force_qs;
  3834. rdp->blimit = blimit;
  3835. if (!rdp->nxtlist)
  3836. init_callback_list(rdp); /* Re-enable callbacks on this CPU. */
  3837. rdp->dynticks->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE;
  3838. rcu_sysidle_init_percpu_data(rdp->dynticks);
  3839. atomic_set(&rdp->dynticks->dynticks,
  3840. (atomic_read(&rdp->dynticks->dynticks) & ~0x1) + 1);
  3841. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  3842. /*
  3843. * Add CPU to leaf rcu_node pending-online bitmask. Any needed
  3844. * propagation up the rcu_node tree will happen at the beginning
  3845. * of the next grace period.
  3846. */
  3847. rnp = rdp->mynode;
  3848. mask = rdp->grpmask;
  3849. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  3850. rnp->qsmaskinitnext |= mask;
  3851. rnp->expmaskinitnext |= mask;
  3852. if (!rdp->beenonline)
  3853. WRITE_ONCE(rsp->ncpus, READ_ONCE(rsp->ncpus) + 1);
  3854. rdp->beenonline = true; /* We have now been online. */
  3855. rdp->gpnum = rnp->completed; /* Make CPU later note any new GP. */
  3856. rdp->completed = rnp->completed;
  3857. rdp->cpu_no_qs.b.norm = true;
  3858. rdp->rcu_qs_ctr_snap = per_cpu(rcu_qs_ctr, cpu);
  3859. rdp->core_needs_qs = false;
  3860. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpuonl"));
  3861. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3862. }
  3863. static void rcu_prepare_cpu(int cpu)
  3864. {
  3865. struct rcu_state *rsp;
  3866. for_each_rcu_flavor(rsp)
  3867. rcu_init_percpu_data(cpu, rsp);
  3868. }
  3869. #ifdef CONFIG_HOTPLUG_CPU
  3870. /*
  3871. * The CPU is exiting the idle loop into the arch_cpu_idle_dead()
  3872. * function. We now remove it from the rcu_node tree's ->qsmaskinit
  3873. * bit masks.
  3874. * The CPU is exiting the idle loop into the arch_cpu_idle_dead()
  3875. * function. We now remove it from the rcu_node tree's ->qsmaskinit
  3876. * bit masks.
  3877. */
  3878. static void rcu_cleanup_dying_idle_cpu(int cpu, struct rcu_state *rsp)
  3879. {
  3880. unsigned long flags;
  3881. unsigned long mask;
  3882. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  3883. struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
  3884. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
  3885. return;
  3886. /* Remove outgoing CPU from mask in the leaf rcu_node structure. */
  3887. mask = rdp->grpmask;
  3888. raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */
  3889. rnp->qsmaskinitnext &= ~mask;
  3890. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3891. }
  3892. void rcu_report_dead(unsigned int cpu)
  3893. {
  3894. struct rcu_state *rsp;
  3895. /* QS for any half-done expedited RCU-sched GP. */
  3896. preempt_disable();
  3897. rcu_report_exp_rdp(&rcu_sched_state,
  3898. this_cpu_ptr(rcu_sched_state.rda), true);
  3899. preempt_enable();
  3900. for_each_rcu_flavor(rsp)
  3901. rcu_cleanup_dying_idle_cpu(cpu, rsp);
  3902. }
  3903. #endif
  3904. /*
  3905. * Handle CPU online/offline notification events.
  3906. */
  3907. int rcu_cpu_notify(struct notifier_block *self,
  3908. unsigned long action, void *hcpu)
  3909. {
  3910. long cpu = (long)hcpu;
  3911. struct rcu_data *rdp = per_cpu_ptr(rcu_state_p->rda, cpu);
  3912. struct rcu_node *rnp = rdp->mynode;
  3913. struct rcu_state *rsp;
  3914. switch (action) {
  3915. case CPU_UP_PREPARE:
  3916. case CPU_UP_PREPARE_FROZEN:
  3917. rcu_prepare_cpu(cpu);
  3918. rcu_prepare_kthreads(cpu);
  3919. rcu_spawn_all_nocb_kthreads(cpu);
  3920. break;
  3921. case CPU_ONLINE:
  3922. case CPU_DOWN_FAILED:
  3923. sync_sched_exp_online_cleanup(cpu);
  3924. rcu_boost_kthread_setaffinity(rnp, -1);
  3925. break;
  3926. case CPU_DOWN_PREPARE:
  3927. rcu_boost_kthread_setaffinity(rnp, cpu);
  3928. break;
  3929. case CPU_DYING:
  3930. case CPU_DYING_FROZEN:
  3931. for_each_rcu_flavor(rsp)
  3932. rcu_cleanup_dying_cpu(rsp);
  3933. break;
  3934. case CPU_DEAD:
  3935. case CPU_DEAD_FROZEN:
  3936. case CPU_UP_CANCELED:
  3937. case CPU_UP_CANCELED_FROZEN:
  3938. for_each_rcu_flavor(rsp) {
  3939. rcu_cleanup_dead_cpu(cpu, rsp);
  3940. do_nocb_deferred_wakeup(per_cpu_ptr(rsp->rda, cpu));
  3941. }
  3942. break;
  3943. default:
  3944. break;
  3945. }
  3946. return NOTIFY_OK;
  3947. }
  3948. static int rcu_pm_notify(struct notifier_block *self,
  3949. unsigned long action, void *hcpu)
  3950. {
  3951. switch (action) {
  3952. case PM_HIBERNATION_PREPARE:
  3953. case PM_SUSPEND_PREPARE:
  3954. if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
  3955. rcu_expedite_gp();
  3956. break;
  3957. case PM_POST_HIBERNATION:
  3958. case PM_POST_SUSPEND:
  3959. if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
  3960. rcu_unexpedite_gp();
  3961. break;
  3962. default:
  3963. break;
  3964. }
  3965. return NOTIFY_OK;
  3966. }
  3967. /*
  3968. * Spawn the kthreads that handle each RCU flavor's grace periods.
  3969. */
  3970. static int __init rcu_spawn_gp_kthread(void)
  3971. {
  3972. unsigned long flags;
  3973. int kthread_prio_in = kthread_prio;
  3974. struct rcu_node *rnp;
  3975. struct rcu_state *rsp;
  3976. struct sched_param sp;
  3977. struct task_struct *t;
  3978. /* Force priority into range. */
  3979. if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 1)
  3980. kthread_prio = 1;
  3981. else if (kthread_prio < 0)
  3982. kthread_prio = 0;
  3983. else if (kthread_prio > 99)
  3984. kthread_prio = 99;
  3985. if (kthread_prio != kthread_prio_in)
  3986. pr_alert("rcu_spawn_gp_kthread(): Limited prio to %d from %d\n",
  3987. kthread_prio, kthread_prio_in);
  3988. rcu_scheduler_fully_active = 1;
  3989. for_each_rcu_flavor(rsp) {
  3990. t = kthread_create(rcu_gp_kthread, rsp, "%s", rsp->name);
  3991. BUG_ON(IS_ERR(t));
  3992. rnp = rcu_get_root(rsp);
  3993. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3994. rsp->gp_kthread = t;
  3995. if (kthread_prio) {
  3996. sp.sched_priority = kthread_prio;
  3997. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  3998. }
  3999. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  4000. wake_up_process(t);
  4001. }
  4002. rcu_spawn_nocb_kthreads();
  4003. rcu_spawn_boost_kthreads();
  4004. return 0;
  4005. }
  4006. early_initcall(rcu_spawn_gp_kthread);
  4007. /*
  4008. * This function is invoked towards the end of the scheduler's initialization
  4009. * process. Before this is called, the idle task might contain
  4010. * RCU read-side critical sections (during which time, this idle
  4011. * task is booting the system). After this function is called, the
  4012. * idle tasks are prohibited from containing RCU read-side critical
  4013. * sections. This function also enables RCU lockdep checking.
  4014. */
  4015. void rcu_scheduler_starting(void)
  4016. {
  4017. WARN_ON(num_online_cpus() != 1);
  4018. WARN_ON(nr_context_switches() > 0);
  4019. rcu_scheduler_active = 1;
  4020. }
  4021. /*
  4022. * Compute the per-level fanout, either using the exact fanout specified
  4023. * or balancing the tree, depending on the rcu_fanout_exact boot parameter.
  4024. */
  4025. static void __init rcu_init_levelspread(int *levelspread, const int *levelcnt)
  4026. {
  4027. int i;
  4028. if (rcu_fanout_exact) {
  4029. levelspread[rcu_num_lvls - 1] = rcu_fanout_leaf;
  4030. for (i = rcu_num_lvls - 2; i >= 0; i--)
  4031. levelspread[i] = RCU_FANOUT;
  4032. } else {
  4033. int ccur;
  4034. int cprv;
  4035. cprv = nr_cpu_ids;
  4036. for (i = rcu_num_lvls - 1; i >= 0; i--) {
  4037. ccur = levelcnt[i];
  4038. levelspread[i] = (cprv + ccur - 1) / ccur;
  4039. cprv = ccur;
  4040. }
  4041. }
  4042. }
  4043. /*
  4044. * Helper function for rcu_init() that initializes one rcu_state structure.
  4045. */
  4046. static void __init rcu_init_one(struct rcu_state *rsp)
  4047. {
  4048. static const char * const buf[] = RCU_NODE_NAME_INIT;
  4049. static const char * const fqs[] = RCU_FQS_NAME_INIT;
  4050. static const char * const exp[] = RCU_EXP_NAME_INIT;
  4051. static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
  4052. static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
  4053. static struct lock_class_key rcu_exp_class[RCU_NUM_LVLS];
  4054. static u8 fl_mask = 0x1;
  4055. int levelcnt[RCU_NUM_LVLS]; /* # nodes in each level. */
  4056. int levelspread[RCU_NUM_LVLS]; /* kids/node in each level. */
  4057. int cpustride = 1;
  4058. int i;
  4059. int j;
  4060. struct rcu_node *rnp;
  4061. BUILD_BUG_ON(RCU_NUM_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
  4062. /* Silence gcc 4.8 false positive about array index out of range. */
  4063. if (rcu_num_lvls <= 0 || rcu_num_lvls > RCU_NUM_LVLS)
  4064. panic("rcu_init_one: rcu_num_lvls out of range");
  4065. /* Initialize the level-tracking arrays. */
  4066. for (i = 0; i < rcu_num_lvls; i++)
  4067. levelcnt[i] = num_rcu_lvl[i];
  4068. for (i = 1; i < rcu_num_lvls; i++)
  4069. rsp->level[i] = rsp->level[i - 1] + levelcnt[i - 1];
  4070. rcu_init_levelspread(levelspread, levelcnt);
  4071. rsp->flavor_mask = fl_mask;
  4072. fl_mask <<= 1;
  4073. /* Initialize the elements themselves, starting from the leaves. */
  4074. for (i = rcu_num_lvls - 1; i >= 0; i--) {
  4075. cpustride *= levelspread[i];
  4076. rnp = rsp->level[i];
  4077. for (j = 0; j < levelcnt[i]; j++, rnp++) {
  4078. raw_spin_lock_init(&ACCESS_PRIVATE(rnp, lock));
  4079. lockdep_set_class_and_name(&ACCESS_PRIVATE(rnp, lock),
  4080. &rcu_node_class[i], buf[i]);
  4081. raw_spin_lock_init(&rnp->fqslock);
  4082. lockdep_set_class_and_name(&rnp->fqslock,
  4083. &rcu_fqs_class[i], fqs[i]);
  4084. rnp->gpnum = rsp->gpnum;
  4085. rnp->completed = rsp->completed;
  4086. rnp->qsmask = 0;
  4087. rnp->qsmaskinit = 0;
  4088. rnp->grplo = j * cpustride;
  4089. rnp->grphi = (j + 1) * cpustride - 1;
  4090. if (rnp->grphi >= nr_cpu_ids)
  4091. rnp->grphi = nr_cpu_ids - 1;
  4092. if (i == 0) {
  4093. rnp->grpnum = 0;
  4094. rnp->grpmask = 0;
  4095. rnp->parent = NULL;
  4096. } else {
  4097. rnp->grpnum = j % levelspread[i - 1];
  4098. rnp->grpmask = 1UL << rnp->grpnum;
  4099. rnp->parent = rsp->level[i - 1] +
  4100. j / levelspread[i - 1];
  4101. }
  4102. rnp->level = i;
  4103. INIT_LIST_HEAD(&rnp->blkd_tasks);
  4104. rcu_init_one_nocb(rnp);
  4105. mutex_init(&rnp->exp_funnel_mutex);
  4106. lockdep_set_class_and_name(&rnp->exp_funnel_mutex,
  4107. &rcu_exp_class[i], exp[i]);
  4108. }
  4109. }
  4110. init_swait_queue_head(&rsp->gp_wq);
  4111. init_swait_queue_head(&rsp->expedited_wq);
  4112. rnp = rsp->level[rcu_num_lvls - 1];
  4113. for_each_possible_cpu(i) {
  4114. while (i > rnp->grphi)
  4115. rnp++;
  4116. per_cpu_ptr(rsp->rda, i)->mynode = rnp;
  4117. rcu_boot_init_percpu_data(i, rsp);
  4118. }
  4119. list_add(&rsp->flavors, &rcu_struct_flavors);
  4120. }
  4121. /*
  4122. * Compute the rcu_node tree geometry from kernel parameters. This cannot
  4123. * replace the definitions in tree.h because those are needed to size
  4124. * the ->node array in the rcu_state structure.
  4125. */
  4126. static void __init rcu_init_geometry(void)
  4127. {
  4128. ulong d;
  4129. int i;
  4130. int rcu_capacity[RCU_NUM_LVLS];
  4131. /*
  4132. * Initialize any unspecified boot parameters.
  4133. * The default values of jiffies_till_first_fqs and
  4134. * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS
  4135. * value, which is a function of HZ, then adding one for each
  4136. * RCU_JIFFIES_FQS_DIV CPUs that might be on the system.
  4137. */
  4138. d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
  4139. if (jiffies_till_first_fqs == ULONG_MAX)
  4140. jiffies_till_first_fqs = d;
  4141. if (jiffies_till_next_fqs == ULONG_MAX)
  4142. jiffies_till_next_fqs = d;
  4143. /* If the compile-time values are accurate, just leave. */
  4144. if (rcu_fanout_leaf == RCU_FANOUT_LEAF &&
  4145. nr_cpu_ids == NR_CPUS)
  4146. return;
  4147. pr_info("RCU: Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%d\n",
  4148. rcu_fanout_leaf, nr_cpu_ids);
  4149. /*
  4150. * The boot-time rcu_fanout_leaf parameter must be at least two
  4151. * and cannot exceed the number of bits in the rcu_node masks.
  4152. * Complain and fall back to the compile-time values if this
  4153. * limit is exceeded.
  4154. */
  4155. if (rcu_fanout_leaf < 2 ||
  4156. rcu_fanout_leaf > sizeof(unsigned long) * 8) {
  4157. rcu_fanout_leaf = RCU_FANOUT_LEAF;
  4158. WARN_ON(1);
  4159. return;
  4160. }
  4161. /*
  4162. * Compute number of nodes that can be handled an rcu_node tree
  4163. * with the given number of levels.
  4164. */
  4165. rcu_capacity[0] = rcu_fanout_leaf;
  4166. for (i = 1; i < RCU_NUM_LVLS; i++)
  4167. rcu_capacity[i] = rcu_capacity[i - 1] * RCU_FANOUT;
  4168. /*
  4169. * The tree must be able to accommodate the configured number of CPUs.
  4170. * If this limit is exceeded, fall back to the compile-time values.
  4171. */
  4172. if (nr_cpu_ids > rcu_capacity[RCU_NUM_LVLS - 1]) {
  4173. rcu_fanout_leaf = RCU_FANOUT_LEAF;
  4174. WARN_ON(1);
  4175. return;
  4176. }
  4177. /* Calculate the number of levels in the tree. */
  4178. for (i = 0; nr_cpu_ids > rcu_capacity[i]; i++) {
  4179. }
  4180. rcu_num_lvls = i + 1;
  4181. /* Calculate the number of rcu_nodes at each level of the tree. */
  4182. for (i = 0; i < rcu_num_lvls; i++) {
  4183. int cap = rcu_capacity[(rcu_num_lvls - 1) - i];
  4184. num_rcu_lvl[i] = DIV_ROUND_UP(nr_cpu_ids, cap);
  4185. }
  4186. /* Calculate the total number of rcu_node structures. */
  4187. rcu_num_nodes = 0;
  4188. for (i = 0; i < rcu_num_lvls; i++)
  4189. rcu_num_nodes += num_rcu_lvl[i];
  4190. }
  4191. /*
  4192. * Dump out the structure of the rcu_node combining tree associated
  4193. * with the rcu_state structure referenced by rsp.
  4194. */
  4195. static void __init rcu_dump_rcu_node_tree(struct rcu_state *rsp)
  4196. {
  4197. int level = 0;
  4198. struct rcu_node *rnp;
  4199. pr_info("rcu_node tree layout dump\n");
  4200. pr_info(" ");
  4201. rcu_for_each_node_breadth_first(rsp, rnp) {
  4202. if (rnp->level != level) {
  4203. pr_cont("\n");
  4204. pr_info(" ");
  4205. level = rnp->level;
  4206. }
  4207. pr_cont("%d:%d ^%d ", rnp->grplo, rnp->grphi, rnp->grpnum);
  4208. }
  4209. pr_cont("\n");
  4210. }
  4211. void __init rcu_init(void)
  4212. {
  4213. int cpu;
  4214. rcu_early_boot_tests();
  4215. rcu_bootup_announce();
  4216. rcu_init_geometry();
  4217. rcu_init_one(&rcu_bh_state);
  4218. rcu_init_one(&rcu_sched_state);
  4219. if (dump_tree)
  4220. rcu_dump_rcu_node_tree(&rcu_sched_state);
  4221. __rcu_init_preempt();
  4222. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  4223. /*
  4224. * We don't need protection against CPU-hotplug here because
  4225. * this is called early in boot, before either interrupts
  4226. * or the scheduler are operational.
  4227. */
  4228. cpu_notifier(rcu_cpu_notify, 0);
  4229. pm_notifier(rcu_pm_notify, 0);
  4230. for_each_online_cpu(cpu)
  4231. rcu_cpu_notify(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
  4232. }
  4233. #include "tree_plugin.h"