rcutree.c 98 KB

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