tree.c 128 KB

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