rcutree.c 102 KB

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