tree.c 133 KB

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