tree.c 131 KB

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