rcutree.c 85 KB

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