tree.c 117 KB

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