rcutree.c 86 KB

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