tree.c 132 KB

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