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