rcutree.c 99 KB

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