tree.c 129 KB

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