tree.c 130 KB

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