rcutree.c 88 KB

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