tree.c 131 KB

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