tree.c 108 KB

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