tree.c 113 KB

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