tree.c 129 KB

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