tree.c 133 KB

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