rcutree.c 87 KB

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