tree.c 133 KB

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