tree.c 142 KB

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