tree.c 140 KB

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