tree.c 111 KB

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