rcutree.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310
  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 <asm/atomic.h>
  40. #include <linux/bitops.h>
  41. #include <linux/module.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 "rcutree.h"
  53. /* Data structures. */
  54. static struct lock_class_key rcu_node_class[NUM_RCU_LVLS];
  55. #define RCU_STATE_INITIALIZER(structname) { \
  56. .level = { &structname.node[0] }, \
  57. .levelcnt = { \
  58. NUM_RCU_LVL_0, /* root of hierarchy. */ \
  59. NUM_RCU_LVL_1, \
  60. NUM_RCU_LVL_2, \
  61. NUM_RCU_LVL_3, \
  62. NUM_RCU_LVL_4, /* == MAX_RCU_LVLS */ \
  63. }, \
  64. .signaled = RCU_GP_IDLE, \
  65. .gpnum = -300, \
  66. .completed = -300, \
  67. .onofflock = __RAW_SPIN_LOCK_UNLOCKED(&structname.onofflock), \
  68. .fqslock = __RAW_SPIN_LOCK_UNLOCKED(&structname.fqslock), \
  69. .n_force_qs = 0, \
  70. .n_force_qs_ngp = 0, \
  71. .name = #structname, \
  72. }
  73. struct rcu_state rcu_sched_state = RCU_STATE_INITIALIZER(rcu_sched_state);
  74. DEFINE_PER_CPU(struct rcu_data, rcu_sched_data);
  75. struct rcu_state rcu_bh_state = RCU_STATE_INITIALIZER(rcu_bh_state);
  76. DEFINE_PER_CPU(struct rcu_data, rcu_bh_data);
  77. static struct rcu_state *rcu_state;
  78. int rcu_scheduler_active __read_mostly;
  79. EXPORT_SYMBOL_GPL(rcu_scheduler_active);
  80. /*
  81. * Control variables for per-CPU and per-rcu_node kthreads. These
  82. * handle all flavors of RCU.
  83. */
  84. static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  85. static DEFINE_PER_CPU(wait_queue_head_t, rcu_cpu_wq);
  86. static DEFINE_PER_CPU(char, rcu_cpu_has_work);
  87. static char rcu_kthreads_spawnable;
  88. static void rcu_node_kthread_setaffinity(struct rcu_node *rnp);
  89. static void invoke_rcu_cpu_kthread(void);
  90. #define RCU_KTHREAD_PRIO 1 /* RT priority for per-CPU kthreads. */
  91. /*
  92. * Return true if an RCU grace period is in progress. The ACCESS_ONCE()s
  93. * permit this function to be invoked without holding the root rcu_node
  94. * structure's ->lock, but of course results can be subject to change.
  95. */
  96. static int rcu_gp_in_progress(struct rcu_state *rsp)
  97. {
  98. return ACCESS_ONCE(rsp->completed) != ACCESS_ONCE(rsp->gpnum);
  99. }
  100. /*
  101. * Note a quiescent state. Because we do not need to know
  102. * how many quiescent states passed, just if there was at least
  103. * one since the start of the grace period, this just sets a flag.
  104. */
  105. void rcu_sched_qs(int cpu)
  106. {
  107. struct rcu_data *rdp = &per_cpu(rcu_sched_data, cpu);
  108. rdp->passed_quiesc_completed = rdp->gpnum - 1;
  109. barrier();
  110. rdp->passed_quiesc = 1;
  111. }
  112. void rcu_bh_qs(int cpu)
  113. {
  114. struct rcu_data *rdp = &per_cpu(rcu_bh_data, cpu);
  115. rdp->passed_quiesc_completed = rdp->gpnum - 1;
  116. barrier();
  117. rdp->passed_quiesc = 1;
  118. }
  119. /*
  120. * Note a context switch. This is a quiescent state for RCU-sched,
  121. * and requires special handling for preemptible RCU.
  122. */
  123. void rcu_note_context_switch(int cpu)
  124. {
  125. rcu_sched_qs(cpu);
  126. rcu_preempt_note_context_switch(cpu);
  127. }
  128. #ifdef CONFIG_NO_HZ
  129. DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
  130. .dynticks_nesting = 1,
  131. .dynticks = ATOMIC_INIT(1),
  132. };
  133. #endif /* #ifdef CONFIG_NO_HZ */
  134. static int blimit = 10; /* Maximum callbacks per softirq. */
  135. static int qhimark = 10000; /* If this many pending, ignore blimit. */
  136. static int qlowmark = 100; /* Once only this many pending, use blimit. */
  137. module_param(blimit, int, 0);
  138. module_param(qhimark, int, 0);
  139. module_param(qlowmark, int, 0);
  140. int rcu_cpu_stall_suppress __read_mostly;
  141. module_param(rcu_cpu_stall_suppress, int, 0644);
  142. static void force_quiescent_state(struct rcu_state *rsp, int relaxed);
  143. static int rcu_pending(int cpu);
  144. /*
  145. * Return the number of RCU-sched batches processed thus far for debug & stats.
  146. */
  147. long rcu_batches_completed_sched(void)
  148. {
  149. return rcu_sched_state.completed;
  150. }
  151. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  152. /*
  153. * Return the number of RCU BH batches processed thus far for debug & stats.
  154. */
  155. long rcu_batches_completed_bh(void)
  156. {
  157. return rcu_bh_state.completed;
  158. }
  159. EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
  160. /*
  161. * Force a quiescent state for RCU BH.
  162. */
  163. void rcu_bh_force_quiescent_state(void)
  164. {
  165. force_quiescent_state(&rcu_bh_state, 0);
  166. }
  167. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  168. /*
  169. * Force a quiescent state for RCU-sched.
  170. */
  171. void rcu_sched_force_quiescent_state(void)
  172. {
  173. force_quiescent_state(&rcu_sched_state, 0);
  174. }
  175. EXPORT_SYMBOL_GPL(rcu_sched_force_quiescent_state);
  176. /*
  177. * Does the CPU have callbacks ready to be invoked?
  178. */
  179. static int
  180. cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp)
  181. {
  182. return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL];
  183. }
  184. /*
  185. * Does the current CPU require a yet-as-unscheduled grace period?
  186. */
  187. static int
  188. cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
  189. {
  190. return *rdp->nxttail[RCU_DONE_TAIL] && !rcu_gp_in_progress(rsp);
  191. }
  192. /*
  193. * Return the root node of the specified rcu_state structure.
  194. */
  195. static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
  196. {
  197. return &rsp->node[0];
  198. }
  199. #ifdef CONFIG_SMP
  200. /*
  201. * If the specified CPU is offline, tell the caller that it is in
  202. * a quiescent state. Otherwise, whack it with a reschedule IPI.
  203. * Grace periods can end up waiting on an offline CPU when that
  204. * CPU is in the process of coming online -- it will be added to the
  205. * rcu_node bitmasks before it actually makes it online. The same thing
  206. * can happen while a CPU is in the process of coming online. Because this
  207. * race is quite rare, we check for it after detecting that the grace
  208. * period has been delayed rather than checking each and every CPU
  209. * each and every time we start a new grace period.
  210. */
  211. static int rcu_implicit_offline_qs(struct rcu_data *rdp)
  212. {
  213. /*
  214. * If the CPU is offline, it is in a quiescent state. We can
  215. * trust its state not to change because interrupts are disabled.
  216. */
  217. if (cpu_is_offline(rdp->cpu)) {
  218. rdp->offline_fqs++;
  219. return 1;
  220. }
  221. /* If preemptable RCU, no point in sending reschedule IPI. */
  222. if (rdp->preemptable)
  223. return 0;
  224. /* The CPU is online, so send it a reschedule IPI. */
  225. if (rdp->cpu != smp_processor_id())
  226. smp_send_reschedule(rdp->cpu);
  227. else
  228. set_need_resched();
  229. rdp->resched_ipi++;
  230. return 0;
  231. }
  232. #endif /* #ifdef CONFIG_SMP */
  233. #ifdef CONFIG_NO_HZ
  234. /**
  235. * rcu_enter_nohz - inform RCU that current CPU is entering nohz
  236. *
  237. * Enter nohz mode, in other words, -leave- the mode in which RCU
  238. * read-side critical sections can occur. (Though RCU read-side
  239. * critical sections can occur in irq handlers in nohz mode, a possibility
  240. * handled by rcu_irq_enter() and rcu_irq_exit()).
  241. */
  242. void rcu_enter_nohz(void)
  243. {
  244. unsigned long flags;
  245. struct rcu_dynticks *rdtp;
  246. local_irq_save(flags);
  247. rdtp = &__get_cpu_var(rcu_dynticks);
  248. if (--rdtp->dynticks_nesting) {
  249. local_irq_restore(flags);
  250. return;
  251. }
  252. /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
  253. smp_mb__before_atomic_inc(); /* See above. */
  254. atomic_inc(&rdtp->dynticks);
  255. smp_mb__after_atomic_inc(); /* Force ordering with next sojourn. */
  256. WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
  257. local_irq_restore(flags);
  258. /* If the interrupt queued a callback, get out of dyntick mode. */
  259. if (in_irq() &&
  260. (__get_cpu_var(rcu_sched_data).nxtlist ||
  261. __get_cpu_var(rcu_bh_data).nxtlist ||
  262. rcu_preempt_needs_cpu(smp_processor_id())))
  263. set_need_resched();
  264. }
  265. /*
  266. * rcu_exit_nohz - inform RCU that current CPU is leaving nohz
  267. *
  268. * Exit nohz mode, in other words, -enter- the mode in which RCU
  269. * read-side critical sections normally occur.
  270. */
  271. void rcu_exit_nohz(void)
  272. {
  273. unsigned long flags;
  274. struct rcu_dynticks *rdtp;
  275. local_irq_save(flags);
  276. rdtp = &__get_cpu_var(rcu_dynticks);
  277. if (rdtp->dynticks_nesting++) {
  278. local_irq_restore(flags);
  279. return;
  280. }
  281. smp_mb__before_atomic_inc(); /* Force ordering w/previous sojourn. */
  282. atomic_inc(&rdtp->dynticks);
  283. /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
  284. smp_mb__after_atomic_inc(); /* See above. */
  285. WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
  286. local_irq_restore(flags);
  287. }
  288. /**
  289. * rcu_nmi_enter - inform RCU of entry to NMI context
  290. *
  291. * If the CPU was idle with dynamic ticks active, and there is no
  292. * irq handler running, this updates rdtp->dynticks_nmi to let the
  293. * RCU grace-period handling know that the CPU is active.
  294. */
  295. void rcu_nmi_enter(void)
  296. {
  297. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  298. if (rdtp->dynticks_nmi_nesting == 0 &&
  299. (atomic_read(&rdtp->dynticks) & 0x1))
  300. return;
  301. rdtp->dynticks_nmi_nesting++;
  302. smp_mb__before_atomic_inc(); /* Force delay from prior write. */
  303. atomic_inc(&rdtp->dynticks);
  304. /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
  305. smp_mb__after_atomic_inc(); /* See above. */
  306. WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
  307. }
  308. /**
  309. * rcu_nmi_exit - inform RCU of exit from NMI context
  310. *
  311. * If the CPU was idle with dynamic ticks active, and there is no
  312. * irq handler running, this updates rdtp->dynticks_nmi to let the
  313. * RCU grace-period handling know that the CPU is no longer active.
  314. */
  315. void rcu_nmi_exit(void)
  316. {
  317. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  318. if (rdtp->dynticks_nmi_nesting == 0 ||
  319. --rdtp->dynticks_nmi_nesting != 0)
  320. return;
  321. /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
  322. smp_mb__before_atomic_inc(); /* See above. */
  323. atomic_inc(&rdtp->dynticks);
  324. smp_mb__after_atomic_inc(); /* Force delay to next write. */
  325. WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
  326. }
  327. /**
  328. * rcu_irq_enter - inform RCU of entry to hard irq context
  329. *
  330. * If the CPU was idle with dynamic ticks active, this updates the
  331. * rdtp->dynticks to let the RCU handling know that the CPU is active.
  332. */
  333. void rcu_irq_enter(void)
  334. {
  335. rcu_exit_nohz();
  336. }
  337. /**
  338. * rcu_irq_exit - inform RCU of exit from hard irq context
  339. *
  340. * If the CPU was idle with dynamic ticks active, update the rdp->dynticks
  341. * to put let the RCU handling be aware that the CPU is going back to idle
  342. * with no ticks.
  343. */
  344. void rcu_irq_exit(void)
  345. {
  346. rcu_enter_nohz();
  347. }
  348. #ifdef CONFIG_SMP
  349. /*
  350. * Snapshot the specified CPU's dynticks counter so that we can later
  351. * credit them with an implicit quiescent state. Return 1 if this CPU
  352. * is in dynticks idle mode, which is an extended quiescent state.
  353. */
  354. static int dyntick_save_progress_counter(struct rcu_data *rdp)
  355. {
  356. rdp->dynticks_snap = atomic_add_return(0, &rdp->dynticks->dynticks);
  357. return 0;
  358. }
  359. /*
  360. * Return true if the specified CPU has passed through a quiescent
  361. * state by virtue of being in or having passed through an dynticks
  362. * idle state since the last call to dyntick_save_progress_counter()
  363. * for this same CPU.
  364. */
  365. static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
  366. {
  367. unsigned long curr;
  368. unsigned long snap;
  369. curr = (unsigned long)atomic_add_return(0, &rdp->dynticks->dynticks);
  370. snap = (unsigned long)rdp->dynticks_snap;
  371. /*
  372. * If the CPU passed through or entered a dynticks idle phase with
  373. * no active irq/NMI handlers, then we can safely pretend that the CPU
  374. * already acknowledged the request to pass through a quiescent
  375. * state. Either way, that CPU cannot possibly be in an RCU
  376. * read-side critical section that started before the beginning
  377. * of the current RCU grace period.
  378. */
  379. if ((curr & 0x1) == 0 || ULONG_CMP_GE(curr, snap + 2)) {
  380. rdp->dynticks_fqs++;
  381. return 1;
  382. }
  383. /* Go check for the CPU being offline. */
  384. return rcu_implicit_offline_qs(rdp);
  385. }
  386. #endif /* #ifdef CONFIG_SMP */
  387. #else /* #ifdef CONFIG_NO_HZ */
  388. #ifdef CONFIG_SMP
  389. static int dyntick_save_progress_counter(struct rcu_data *rdp)
  390. {
  391. return 0;
  392. }
  393. static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
  394. {
  395. return rcu_implicit_offline_qs(rdp);
  396. }
  397. #endif /* #ifdef CONFIG_SMP */
  398. #endif /* #else #ifdef CONFIG_NO_HZ */
  399. int rcu_cpu_stall_suppress __read_mostly;
  400. static void record_gp_stall_check_time(struct rcu_state *rsp)
  401. {
  402. rsp->gp_start = jiffies;
  403. rsp->jiffies_stall = jiffies + RCU_SECONDS_TILL_STALL_CHECK;
  404. }
  405. static void print_other_cpu_stall(struct rcu_state *rsp)
  406. {
  407. int cpu;
  408. long delta;
  409. unsigned long flags;
  410. struct rcu_node *rnp = rcu_get_root(rsp);
  411. /* Only let one CPU complain about others per time interval. */
  412. raw_spin_lock_irqsave(&rnp->lock, flags);
  413. delta = jiffies - rsp->jiffies_stall;
  414. if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) {
  415. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  416. return;
  417. }
  418. rsp->jiffies_stall = jiffies + RCU_SECONDS_TILL_STALL_RECHECK;
  419. /*
  420. * Now rat on any tasks that got kicked up to the root rcu_node
  421. * due to CPU offlining.
  422. */
  423. rcu_print_task_stall(rnp);
  424. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  425. /*
  426. * OK, time to rat on our buddy...
  427. * See Documentation/RCU/stallwarn.txt for info on how to debug
  428. * RCU CPU stall warnings.
  429. */
  430. printk(KERN_ERR "INFO: %s detected stalls on CPUs/tasks: {",
  431. rsp->name);
  432. rcu_for_each_leaf_node(rsp, rnp) {
  433. raw_spin_lock_irqsave(&rnp->lock, flags);
  434. rcu_print_task_stall(rnp);
  435. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  436. if (rnp->qsmask == 0)
  437. continue;
  438. for (cpu = 0; cpu <= rnp->grphi - rnp->grplo; cpu++)
  439. if (rnp->qsmask & (1UL << cpu))
  440. printk(" %d", rnp->grplo + cpu);
  441. }
  442. printk("} (detected by %d, t=%ld jiffies)\n",
  443. smp_processor_id(), (long)(jiffies - rsp->gp_start));
  444. trigger_all_cpu_backtrace();
  445. /* If so configured, complain about tasks blocking the grace period. */
  446. rcu_print_detail_task_stall(rsp);
  447. force_quiescent_state(rsp, 0); /* Kick them all. */
  448. }
  449. static void print_cpu_stall(struct rcu_state *rsp)
  450. {
  451. unsigned long flags;
  452. struct rcu_node *rnp = rcu_get_root(rsp);
  453. /*
  454. * OK, time to rat on ourselves...
  455. * See Documentation/RCU/stallwarn.txt for info on how to debug
  456. * RCU CPU stall warnings.
  457. */
  458. printk(KERN_ERR "INFO: %s detected stall on CPU %d (t=%lu jiffies)\n",
  459. rsp->name, smp_processor_id(), jiffies - rsp->gp_start);
  460. trigger_all_cpu_backtrace();
  461. raw_spin_lock_irqsave(&rnp->lock, flags);
  462. if (ULONG_CMP_GE(jiffies, rsp->jiffies_stall))
  463. rsp->jiffies_stall =
  464. jiffies + RCU_SECONDS_TILL_STALL_RECHECK;
  465. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  466. set_need_resched(); /* kick ourselves to get things going. */
  467. }
  468. static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
  469. {
  470. long delta;
  471. struct rcu_node *rnp;
  472. if (rcu_cpu_stall_suppress)
  473. return;
  474. delta = jiffies - ACCESS_ONCE(rsp->jiffies_stall);
  475. rnp = rdp->mynode;
  476. if ((ACCESS_ONCE(rnp->qsmask) & rdp->grpmask) && delta >= 0) {
  477. /* We haven't checked in, so go dump stack. */
  478. print_cpu_stall(rsp);
  479. } else if (rcu_gp_in_progress(rsp) && delta >= RCU_STALL_RAT_DELAY) {
  480. /* They had two time units to dump stack, so complain. */
  481. print_other_cpu_stall(rsp);
  482. }
  483. }
  484. static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr)
  485. {
  486. rcu_cpu_stall_suppress = 1;
  487. return NOTIFY_DONE;
  488. }
  489. /**
  490. * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
  491. *
  492. * Set the stall-warning timeout way off into the future, thus preventing
  493. * any RCU CPU stall-warning messages from appearing in the current set of
  494. * RCU grace periods.
  495. *
  496. * The caller must disable hard irqs.
  497. */
  498. void rcu_cpu_stall_reset(void)
  499. {
  500. rcu_sched_state.jiffies_stall = jiffies + ULONG_MAX / 2;
  501. rcu_bh_state.jiffies_stall = jiffies + ULONG_MAX / 2;
  502. rcu_preempt_stall_reset();
  503. }
  504. static struct notifier_block rcu_panic_block = {
  505. .notifier_call = rcu_panic,
  506. };
  507. static void __init check_cpu_stall_init(void)
  508. {
  509. atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block);
  510. }
  511. /*
  512. * Update CPU-local rcu_data state to record the newly noticed grace period.
  513. * This is used both when we started the grace period and when we notice
  514. * that someone else started the grace period. The caller must hold the
  515. * ->lock of the leaf rcu_node structure corresponding to the current CPU,
  516. * and must have irqs disabled.
  517. */
  518. static void __note_new_gpnum(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
  519. {
  520. if (rdp->gpnum != rnp->gpnum) {
  521. /*
  522. * If the current grace period is waiting for this CPU,
  523. * set up to detect a quiescent state, otherwise don't
  524. * go looking for one.
  525. */
  526. rdp->gpnum = rnp->gpnum;
  527. if (rnp->qsmask & rdp->grpmask) {
  528. rdp->qs_pending = 1;
  529. rdp->passed_quiesc = 0;
  530. } else
  531. rdp->qs_pending = 0;
  532. }
  533. }
  534. static void note_new_gpnum(struct rcu_state *rsp, struct rcu_data *rdp)
  535. {
  536. unsigned long flags;
  537. struct rcu_node *rnp;
  538. local_irq_save(flags);
  539. rnp = rdp->mynode;
  540. if (rdp->gpnum == ACCESS_ONCE(rnp->gpnum) || /* outside lock. */
  541. !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */
  542. local_irq_restore(flags);
  543. return;
  544. }
  545. __note_new_gpnum(rsp, rnp, rdp);
  546. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  547. }
  548. /*
  549. * Did someone else start a new RCU grace period start since we last
  550. * checked? Update local state appropriately if so. Must be called
  551. * on the CPU corresponding to rdp.
  552. */
  553. static int
  554. check_for_new_grace_period(struct rcu_state *rsp, struct rcu_data *rdp)
  555. {
  556. unsigned long flags;
  557. int ret = 0;
  558. local_irq_save(flags);
  559. if (rdp->gpnum != rsp->gpnum) {
  560. note_new_gpnum(rsp, rdp);
  561. ret = 1;
  562. }
  563. local_irq_restore(flags);
  564. return ret;
  565. }
  566. /*
  567. * Advance this CPU's callbacks, but only if the current grace period
  568. * has ended. This may be called only from the CPU to whom the rdp
  569. * belongs. In addition, the corresponding leaf rcu_node structure's
  570. * ->lock must be held by the caller, with irqs disabled.
  571. */
  572. static void
  573. __rcu_process_gp_end(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
  574. {
  575. /* Did another grace period end? */
  576. if (rdp->completed != rnp->completed) {
  577. /* Advance callbacks. No harm if list empty. */
  578. rdp->nxttail[RCU_DONE_TAIL] = rdp->nxttail[RCU_WAIT_TAIL];
  579. rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_READY_TAIL];
  580. rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  581. /* Remember that we saw this grace-period completion. */
  582. rdp->completed = rnp->completed;
  583. /*
  584. * If we were in an extended quiescent state, we may have
  585. * missed some grace periods that others CPUs handled on
  586. * our behalf. Catch up with this state to avoid noting
  587. * spurious new grace periods. If another grace period
  588. * has started, then rnp->gpnum will have advanced, so
  589. * we will detect this later on.
  590. */
  591. if (ULONG_CMP_LT(rdp->gpnum, rdp->completed))
  592. rdp->gpnum = rdp->completed;
  593. /*
  594. * If RCU does not need a quiescent state from this CPU,
  595. * then make sure that this CPU doesn't go looking for one.
  596. */
  597. if ((rnp->qsmask & rdp->grpmask) == 0)
  598. rdp->qs_pending = 0;
  599. }
  600. }
  601. /*
  602. * Advance this CPU's callbacks, but only if the current grace period
  603. * has ended. This may be called only from the CPU to whom the rdp
  604. * belongs.
  605. */
  606. static void
  607. rcu_process_gp_end(struct rcu_state *rsp, struct rcu_data *rdp)
  608. {
  609. unsigned long flags;
  610. struct rcu_node *rnp;
  611. local_irq_save(flags);
  612. rnp = rdp->mynode;
  613. if (rdp->completed == ACCESS_ONCE(rnp->completed) || /* outside lock. */
  614. !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */
  615. local_irq_restore(flags);
  616. return;
  617. }
  618. __rcu_process_gp_end(rsp, rnp, rdp);
  619. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  620. }
  621. /*
  622. * Do per-CPU grace-period initialization for running CPU. The caller
  623. * must hold the lock of the leaf rcu_node structure corresponding to
  624. * this CPU.
  625. */
  626. static void
  627. rcu_start_gp_per_cpu(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
  628. {
  629. /* Prior grace period ended, so advance callbacks for current CPU. */
  630. __rcu_process_gp_end(rsp, rnp, rdp);
  631. /*
  632. * Because this CPU just now started the new grace period, we know
  633. * that all of its callbacks will be covered by this upcoming grace
  634. * period, even the ones that were registered arbitrarily recently.
  635. * Therefore, advance all outstanding callbacks to RCU_WAIT_TAIL.
  636. *
  637. * Other CPUs cannot be sure exactly when the grace period started.
  638. * Therefore, their recently registered callbacks must pass through
  639. * an additional RCU_NEXT_READY stage, so that they will be handled
  640. * by the next RCU grace period.
  641. */
  642. rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  643. rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  644. /* Set state so that this CPU will detect the next quiescent state. */
  645. __note_new_gpnum(rsp, rnp, rdp);
  646. }
  647. /*
  648. * Start a new RCU grace period if warranted, re-initializing the hierarchy
  649. * in preparation for detecting the next grace period. The caller must hold
  650. * the root node's ->lock, which is released before return. Hard irqs must
  651. * be disabled.
  652. */
  653. static void
  654. rcu_start_gp(struct rcu_state *rsp, unsigned long flags)
  655. __releases(rcu_get_root(rsp)->lock)
  656. {
  657. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  658. struct rcu_node *rnp = rcu_get_root(rsp);
  659. if (!cpu_needs_another_gp(rsp, rdp) || rsp->fqs_active) {
  660. if (cpu_needs_another_gp(rsp, rdp))
  661. rsp->fqs_need_gp = 1;
  662. if (rnp->completed == rsp->completed) {
  663. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  664. return;
  665. }
  666. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  667. /*
  668. * Propagate new ->completed value to rcu_node structures
  669. * so that other CPUs don't have to wait until the start
  670. * of the next grace period to process their callbacks.
  671. */
  672. rcu_for_each_node_breadth_first(rsp, rnp) {
  673. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  674. rnp->completed = rsp->completed;
  675. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  676. }
  677. local_irq_restore(flags);
  678. return;
  679. }
  680. /* Advance to a new grace period and initialize state. */
  681. rsp->gpnum++;
  682. WARN_ON_ONCE(rsp->signaled == RCU_GP_INIT);
  683. rsp->signaled = RCU_GP_INIT; /* Hold off force_quiescent_state. */
  684. rsp->jiffies_force_qs = jiffies + RCU_JIFFIES_TILL_FORCE_QS;
  685. record_gp_stall_check_time(rsp);
  686. /* Special-case the common single-level case. */
  687. if (NUM_RCU_NODES == 1) {
  688. rcu_preempt_check_blocked_tasks(rnp);
  689. rnp->qsmask = rnp->qsmaskinit;
  690. rnp->gpnum = rsp->gpnum;
  691. rnp->completed = rsp->completed;
  692. rsp->signaled = RCU_SIGNAL_INIT; /* force_quiescent_state OK. */
  693. rcu_start_gp_per_cpu(rsp, rnp, rdp);
  694. rcu_preempt_boost_start_gp(rnp);
  695. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  696. return;
  697. }
  698. raw_spin_unlock(&rnp->lock); /* leave irqs disabled. */
  699. /* Exclude any concurrent CPU-hotplug operations. */
  700. raw_spin_lock(&rsp->onofflock); /* irqs already disabled. */
  701. /*
  702. * Set the quiescent-state-needed bits in all the rcu_node
  703. * structures for all currently online CPUs in breadth-first
  704. * order, starting from the root rcu_node structure. This
  705. * operation relies on the layout of the hierarchy within the
  706. * rsp->node[] array. Note that other CPUs will access only
  707. * the leaves of the hierarchy, which still indicate that no
  708. * grace period is in progress, at least until the corresponding
  709. * leaf node has been initialized. In addition, we have excluded
  710. * CPU-hotplug operations.
  711. *
  712. * Note that the grace period cannot complete until we finish
  713. * the initialization process, as there will be at least one
  714. * qsmask bit set in the root node until that time, namely the
  715. * one corresponding to this CPU, due to the fact that we have
  716. * irqs disabled.
  717. */
  718. rcu_for_each_node_breadth_first(rsp, rnp) {
  719. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  720. rcu_preempt_check_blocked_tasks(rnp);
  721. rnp->qsmask = rnp->qsmaskinit;
  722. rnp->gpnum = rsp->gpnum;
  723. rnp->completed = rsp->completed;
  724. if (rnp == rdp->mynode)
  725. rcu_start_gp_per_cpu(rsp, rnp, rdp);
  726. rcu_preempt_boost_start_gp(rnp);
  727. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  728. }
  729. rnp = rcu_get_root(rsp);
  730. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  731. rsp->signaled = RCU_SIGNAL_INIT; /* force_quiescent_state now OK. */
  732. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  733. raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
  734. }
  735. /*
  736. * Report a full set of quiescent states to the specified rcu_state
  737. * data structure. This involves cleaning up after the prior grace
  738. * period and letting rcu_start_gp() start up the next grace period
  739. * if one is needed. Note that the caller must hold rnp->lock, as
  740. * required by rcu_start_gp(), which will release it.
  741. */
  742. static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
  743. __releases(rcu_get_root(rsp)->lock)
  744. {
  745. WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
  746. /*
  747. * Ensure that all grace-period and pre-grace-period activity
  748. * is seen before the assignment to rsp->completed.
  749. */
  750. smp_mb(); /* See above block comment. */
  751. rsp->completed = rsp->gpnum;
  752. rsp->signaled = RCU_GP_IDLE;
  753. rcu_start_gp(rsp, flags); /* releases root node's rnp->lock. */
  754. }
  755. /*
  756. * Similar to rcu_report_qs_rdp(), for which it is a helper function.
  757. * Allows quiescent states for a group of CPUs to be reported at one go
  758. * to the specified rcu_node structure, though all the CPUs in the group
  759. * must be represented by the same rcu_node structure (which need not be
  760. * a leaf rcu_node structure, though it often will be). That structure's
  761. * lock must be held upon entry, and it is released before return.
  762. */
  763. static void
  764. rcu_report_qs_rnp(unsigned long mask, struct rcu_state *rsp,
  765. struct rcu_node *rnp, unsigned long flags)
  766. __releases(rnp->lock)
  767. {
  768. struct rcu_node *rnp_c;
  769. /* Walk up the rcu_node hierarchy. */
  770. for (;;) {
  771. if (!(rnp->qsmask & mask)) {
  772. /* Our bit has already been cleared, so done. */
  773. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  774. return;
  775. }
  776. rnp->qsmask &= ~mask;
  777. if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  778. /* Other bits still set at this level, so done. */
  779. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  780. return;
  781. }
  782. mask = rnp->grpmask;
  783. if (rnp->parent == NULL) {
  784. /* No more levels. Exit loop holding root lock. */
  785. break;
  786. }
  787. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  788. rnp_c = rnp;
  789. rnp = rnp->parent;
  790. raw_spin_lock_irqsave(&rnp->lock, flags);
  791. WARN_ON_ONCE(rnp_c->qsmask);
  792. }
  793. /*
  794. * Get here if we are the last CPU to pass through a quiescent
  795. * state for this grace period. Invoke rcu_report_qs_rsp()
  796. * to clean up and start the next grace period if one is needed.
  797. */
  798. rcu_report_qs_rsp(rsp, flags); /* releases rnp->lock. */
  799. }
  800. /*
  801. * Record a quiescent state for the specified CPU to that CPU's rcu_data
  802. * structure. This must be either called from the specified CPU, or
  803. * called when the specified CPU is known to be offline (and when it is
  804. * also known that no other CPU is concurrently trying to help the offline
  805. * CPU). The lastcomp argument is used to make sure we are still in the
  806. * grace period of interest. We don't want to end the current grace period
  807. * based on quiescent states detected in an earlier grace period!
  808. */
  809. static void
  810. rcu_report_qs_rdp(int cpu, struct rcu_state *rsp, struct rcu_data *rdp, long lastcomp)
  811. {
  812. unsigned long flags;
  813. unsigned long mask;
  814. struct rcu_node *rnp;
  815. rnp = rdp->mynode;
  816. raw_spin_lock_irqsave(&rnp->lock, flags);
  817. if (lastcomp != rnp->completed) {
  818. /*
  819. * Someone beat us to it for this grace period, so leave.
  820. * The race with GP start is resolved by the fact that we
  821. * hold the leaf rcu_node lock, so that the per-CPU bits
  822. * cannot yet be initialized -- so we would simply find our
  823. * CPU's bit already cleared in rcu_report_qs_rnp() if this
  824. * race occurred.
  825. */
  826. rdp->passed_quiesc = 0; /* try again later! */
  827. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  828. return;
  829. }
  830. mask = rdp->grpmask;
  831. if ((rnp->qsmask & mask) == 0) {
  832. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  833. } else {
  834. rdp->qs_pending = 0;
  835. /*
  836. * This GP can't end until cpu checks in, so all of our
  837. * callbacks can be processed during the next GP.
  838. */
  839. rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  840. rcu_report_qs_rnp(mask, rsp, rnp, flags); /* rlses rnp->lock */
  841. }
  842. }
  843. /*
  844. * Check to see if there is a new grace period of which this CPU
  845. * is not yet aware, and if so, set up local rcu_data state for it.
  846. * Otherwise, see if this CPU has just passed through its first
  847. * quiescent state for this grace period, and record that fact if so.
  848. */
  849. static void
  850. rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
  851. {
  852. /* If there is now a new grace period, record and return. */
  853. if (check_for_new_grace_period(rsp, rdp))
  854. return;
  855. /*
  856. * Does this CPU still need to do its part for current grace period?
  857. * If no, return and let the other CPUs do their part as well.
  858. */
  859. if (!rdp->qs_pending)
  860. return;
  861. /*
  862. * Was there a quiescent state since the beginning of the grace
  863. * period? If no, then exit and wait for the next call.
  864. */
  865. if (!rdp->passed_quiesc)
  866. return;
  867. /*
  868. * Tell RCU we are done (but rcu_report_qs_rdp() will be the
  869. * judge of that).
  870. */
  871. rcu_report_qs_rdp(rdp->cpu, rsp, rdp, rdp->passed_quiesc_completed);
  872. }
  873. #ifdef CONFIG_HOTPLUG_CPU
  874. /*
  875. * Move a dying CPU's RCU callbacks to online CPU's callback list.
  876. * Synchronization is not required because this function executes
  877. * in stop_machine() context.
  878. */
  879. static void rcu_send_cbs_to_online(struct rcu_state *rsp)
  880. {
  881. int i;
  882. /* current DYING CPU is cleared in the cpu_online_mask */
  883. int receive_cpu = cpumask_any(cpu_online_mask);
  884. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  885. struct rcu_data *receive_rdp = per_cpu_ptr(rsp->rda, receive_cpu);
  886. if (rdp->nxtlist == NULL)
  887. return; /* irqs disabled, so comparison is stable. */
  888. *receive_rdp->nxttail[RCU_NEXT_TAIL] = rdp->nxtlist;
  889. receive_rdp->nxttail[RCU_NEXT_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  890. receive_rdp->qlen += rdp->qlen;
  891. receive_rdp->n_cbs_adopted += rdp->qlen;
  892. rdp->n_cbs_orphaned += rdp->qlen;
  893. rdp->nxtlist = NULL;
  894. for (i = 0; i < RCU_NEXT_SIZE; i++)
  895. rdp->nxttail[i] = &rdp->nxtlist;
  896. rdp->qlen = 0;
  897. }
  898. /*
  899. * Remove the outgoing CPU from the bitmasks in the rcu_node hierarchy
  900. * and move all callbacks from the outgoing CPU to the current one.
  901. * There can only be one CPU hotplug operation at a time, so no other
  902. * CPU can be attempting to update rcu_cpu_kthread_task.
  903. */
  904. static void __rcu_offline_cpu(int cpu, struct rcu_state *rsp)
  905. {
  906. unsigned long flags;
  907. unsigned long mask;
  908. int need_report = 0;
  909. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  910. struct rcu_node *rnp;
  911. struct task_struct *t;
  912. /* Stop the CPU's kthread. */
  913. t = per_cpu(rcu_cpu_kthread_task, cpu);
  914. if (t != NULL) {
  915. per_cpu(rcu_cpu_kthread_task, cpu) = NULL;
  916. kthread_stop(t);
  917. }
  918. /* Exclude any attempts to start a new grace period. */
  919. raw_spin_lock_irqsave(&rsp->onofflock, flags);
  920. /* Remove the outgoing CPU from the masks in the rcu_node hierarchy. */
  921. rnp = rdp->mynode; /* this is the outgoing CPU's rnp. */
  922. mask = rdp->grpmask; /* rnp->grplo is constant. */
  923. do {
  924. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  925. rnp->qsmaskinit &= ~mask;
  926. if (rnp->qsmaskinit != 0) {
  927. if (rnp != rdp->mynode)
  928. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  929. break;
  930. }
  931. if (rnp == rdp->mynode)
  932. need_report = rcu_preempt_offline_tasks(rsp, rnp, rdp);
  933. else
  934. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  935. mask = rnp->grpmask;
  936. rnp = rnp->parent;
  937. } while (rnp != NULL);
  938. /*
  939. * We still hold the leaf rcu_node structure lock here, and
  940. * irqs are still disabled. The reason for this subterfuge is
  941. * because invoking rcu_report_unblock_qs_rnp() with ->onofflock
  942. * held leads to deadlock.
  943. */
  944. raw_spin_unlock(&rsp->onofflock); /* irqs remain disabled. */
  945. rnp = rdp->mynode;
  946. if (need_report & RCU_OFL_TASKS_NORM_GP)
  947. rcu_report_unblock_qs_rnp(rnp, flags);
  948. else
  949. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  950. if (need_report & RCU_OFL_TASKS_EXP_GP)
  951. rcu_report_exp_rnp(rsp, rnp);
  952. /*
  953. * If there are no more online CPUs for this rcu_node structure,
  954. * kill the rcu_node structure's kthread. Otherwise, adjust its
  955. * affinity.
  956. */
  957. t = rnp->node_kthread_task;
  958. if (t != NULL &&
  959. rnp->qsmaskinit == 0) {
  960. raw_spin_lock_irqsave(&rnp->lock, flags);
  961. rnp->node_kthread_task = NULL;
  962. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  963. kthread_stop(t);
  964. rcu_stop_boost_kthread(rnp);
  965. } else
  966. rcu_node_kthread_setaffinity(rnp);
  967. }
  968. /*
  969. * Remove the specified CPU from the RCU hierarchy and move any pending
  970. * callbacks that it might have to the current CPU. This code assumes
  971. * that at least one CPU in the system will remain running at all times.
  972. * Any attempt to offline -all- CPUs is likely to strand RCU callbacks.
  973. */
  974. static void rcu_offline_cpu(int cpu)
  975. {
  976. __rcu_offline_cpu(cpu, &rcu_sched_state);
  977. __rcu_offline_cpu(cpu, &rcu_bh_state);
  978. rcu_preempt_offline_cpu(cpu);
  979. }
  980. #else /* #ifdef CONFIG_HOTPLUG_CPU */
  981. static void rcu_send_cbs_to_online(struct rcu_state *rsp)
  982. {
  983. }
  984. static void rcu_offline_cpu(int cpu)
  985. {
  986. }
  987. #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  988. /*
  989. * Invoke any RCU callbacks that have made it to the end of their grace
  990. * period. Thottle as specified by rdp->blimit.
  991. */
  992. static void rcu_do_batch(struct rcu_state *rsp, struct rcu_data *rdp)
  993. {
  994. unsigned long flags;
  995. struct rcu_head *next, *list, **tail;
  996. int count;
  997. /* If no callbacks are ready, just return.*/
  998. if (!cpu_has_callbacks_ready_to_invoke(rdp))
  999. return;
  1000. /*
  1001. * Extract the list of ready callbacks, disabling to prevent
  1002. * races with call_rcu() from interrupt handlers.
  1003. */
  1004. local_irq_save(flags);
  1005. list = rdp->nxtlist;
  1006. rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL];
  1007. *rdp->nxttail[RCU_DONE_TAIL] = NULL;
  1008. tail = rdp->nxttail[RCU_DONE_TAIL];
  1009. for (count = RCU_NEXT_SIZE - 1; count >= 0; count--)
  1010. if (rdp->nxttail[count] == rdp->nxttail[RCU_DONE_TAIL])
  1011. rdp->nxttail[count] = &rdp->nxtlist;
  1012. local_irq_restore(flags);
  1013. /* Invoke callbacks. */
  1014. count = 0;
  1015. while (list) {
  1016. next = list->next;
  1017. prefetch(next);
  1018. debug_rcu_head_unqueue(list);
  1019. list->func(list);
  1020. list = next;
  1021. if (++count >= rdp->blimit)
  1022. break;
  1023. }
  1024. local_irq_save(flags);
  1025. /* Update count, and requeue any remaining callbacks. */
  1026. rdp->qlen -= count;
  1027. rdp->n_cbs_invoked += count;
  1028. if (list != NULL) {
  1029. *tail = rdp->nxtlist;
  1030. rdp->nxtlist = list;
  1031. for (count = 0; count < RCU_NEXT_SIZE; count++)
  1032. if (&rdp->nxtlist == rdp->nxttail[count])
  1033. rdp->nxttail[count] = tail;
  1034. else
  1035. break;
  1036. }
  1037. /* Reinstate batch limit if we have worked down the excess. */
  1038. if (rdp->blimit == LONG_MAX && rdp->qlen <= qlowmark)
  1039. rdp->blimit = blimit;
  1040. /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
  1041. if (rdp->qlen == 0 && rdp->qlen_last_fqs_check != 0) {
  1042. rdp->qlen_last_fqs_check = 0;
  1043. rdp->n_force_qs_snap = rsp->n_force_qs;
  1044. } else if (rdp->qlen < rdp->qlen_last_fqs_check - qhimark)
  1045. rdp->qlen_last_fqs_check = rdp->qlen;
  1046. local_irq_restore(flags);
  1047. /* Re-raise the RCU softirq if there are callbacks remaining. */
  1048. if (cpu_has_callbacks_ready_to_invoke(rdp))
  1049. invoke_rcu_cpu_kthread();
  1050. }
  1051. /*
  1052. * Check to see if this CPU is in a non-context-switch quiescent state
  1053. * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
  1054. * Also schedule the RCU softirq handler.
  1055. *
  1056. * This function must be called with hardirqs disabled. It is normally
  1057. * invoked from the scheduling-clock interrupt. If rcu_pending returns
  1058. * false, there is no point in invoking rcu_check_callbacks().
  1059. */
  1060. void rcu_check_callbacks(int cpu, int user)
  1061. {
  1062. if (user ||
  1063. (idle_cpu(cpu) && rcu_scheduler_active &&
  1064. !in_softirq() && hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
  1065. /*
  1066. * Get here if this CPU took its interrupt from user
  1067. * mode or from the idle loop, and if this is not a
  1068. * nested interrupt. In this case, the CPU is in
  1069. * a quiescent state, so note it.
  1070. *
  1071. * No memory barrier is required here because both
  1072. * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
  1073. * variables that other CPUs neither access nor modify,
  1074. * at least not while the corresponding CPU is online.
  1075. */
  1076. rcu_sched_qs(cpu);
  1077. rcu_bh_qs(cpu);
  1078. } else if (!in_softirq()) {
  1079. /*
  1080. * Get here if this CPU did not take its interrupt from
  1081. * softirq, in other words, if it is not interrupting
  1082. * a rcu_bh read-side critical section. This is an _bh
  1083. * critical section, so note it.
  1084. */
  1085. rcu_bh_qs(cpu);
  1086. }
  1087. rcu_preempt_check_callbacks(cpu);
  1088. if (rcu_pending(cpu))
  1089. invoke_rcu_cpu_kthread();
  1090. }
  1091. #ifdef CONFIG_SMP
  1092. /*
  1093. * Scan the leaf rcu_node structures, processing dyntick state for any that
  1094. * have not yet encountered a quiescent state, using the function specified.
  1095. * Also initiate boosting for any threads blocked on the root rcu_node.
  1096. *
  1097. * The caller must have suppressed start of new grace periods.
  1098. */
  1099. static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *))
  1100. {
  1101. unsigned long bit;
  1102. int cpu;
  1103. unsigned long flags;
  1104. unsigned long mask;
  1105. struct rcu_node *rnp;
  1106. rcu_for_each_leaf_node(rsp, rnp) {
  1107. mask = 0;
  1108. raw_spin_lock_irqsave(&rnp->lock, flags);
  1109. if (!rcu_gp_in_progress(rsp)) {
  1110. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1111. return;
  1112. }
  1113. if (rnp->qsmask == 0) {
  1114. rcu_initiate_boost(rnp);
  1115. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1116. continue;
  1117. }
  1118. cpu = rnp->grplo;
  1119. bit = 1;
  1120. for (; cpu <= rnp->grphi; cpu++, bit <<= 1) {
  1121. if ((rnp->qsmask & bit) != 0 &&
  1122. f(per_cpu_ptr(rsp->rda, cpu)))
  1123. mask |= bit;
  1124. }
  1125. if (mask != 0) {
  1126. /* rcu_report_qs_rnp() releases rnp->lock. */
  1127. rcu_report_qs_rnp(mask, rsp, rnp, flags);
  1128. continue;
  1129. }
  1130. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1131. }
  1132. rnp = rcu_get_root(rsp);
  1133. raw_spin_lock_irqsave(&rnp->lock, flags);
  1134. if (rnp->qsmask == 0)
  1135. rcu_initiate_boost(rnp);
  1136. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1137. }
  1138. /*
  1139. * Force quiescent states on reluctant CPUs, and also detect which
  1140. * CPUs are in dyntick-idle mode.
  1141. */
  1142. static void force_quiescent_state(struct rcu_state *rsp, int relaxed)
  1143. {
  1144. unsigned long flags;
  1145. struct rcu_node *rnp = rcu_get_root(rsp);
  1146. if (!rcu_gp_in_progress(rsp))
  1147. return; /* No grace period in progress, nothing to force. */
  1148. if (!raw_spin_trylock_irqsave(&rsp->fqslock, flags)) {
  1149. rsp->n_force_qs_lh++; /* Inexact, can lose counts. Tough! */
  1150. return; /* Someone else is already on the job. */
  1151. }
  1152. if (relaxed && ULONG_CMP_GE(rsp->jiffies_force_qs, jiffies))
  1153. goto unlock_fqs_ret; /* no emergency and done recently. */
  1154. rsp->n_force_qs++;
  1155. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  1156. rsp->jiffies_force_qs = jiffies + RCU_JIFFIES_TILL_FORCE_QS;
  1157. if(!rcu_gp_in_progress(rsp)) {
  1158. rsp->n_force_qs_ngp++;
  1159. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  1160. goto unlock_fqs_ret; /* no GP in progress, time updated. */
  1161. }
  1162. rsp->fqs_active = 1;
  1163. switch (rsp->signaled) {
  1164. case RCU_GP_IDLE:
  1165. case RCU_GP_INIT:
  1166. break; /* grace period idle or initializing, ignore. */
  1167. case RCU_SAVE_DYNTICK:
  1168. if (RCU_SIGNAL_INIT != RCU_SAVE_DYNTICK)
  1169. break; /* So gcc recognizes the dead code. */
  1170. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  1171. /* Record dyntick-idle state. */
  1172. force_qs_rnp(rsp, dyntick_save_progress_counter);
  1173. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  1174. if (rcu_gp_in_progress(rsp))
  1175. rsp->signaled = RCU_FORCE_QS;
  1176. break;
  1177. case RCU_FORCE_QS:
  1178. /* Check dyntick-idle state, send IPI to laggarts. */
  1179. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  1180. force_qs_rnp(rsp, rcu_implicit_dynticks_qs);
  1181. /* Leave state in case more forcing is required. */
  1182. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  1183. break;
  1184. }
  1185. rsp->fqs_active = 0;
  1186. if (rsp->fqs_need_gp) {
  1187. raw_spin_unlock(&rsp->fqslock); /* irqs remain disabled */
  1188. rsp->fqs_need_gp = 0;
  1189. rcu_start_gp(rsp, flags); /* releases rnp->lock */
  1190. return;
  1191. }
  1192. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  1193. unlock_fqs_ret:
  1194. raw_spin_unlock_irqrestore(&rsp->fqslock, flags);
  1195. }
  1196. #else /* #ifdef CONFIG_SMP */
  1197. static void force_quiescent_state(struct rcu_state *rsp, int relaxed)
  1198. {
  1199. set_need_resched();
  1200. }
  1201. #endif /* #else #ifdef CONFIG_SMP */
  1202. /*
  1203. * This does the RCU processing work from softirq context for the
  1204. * specified rcu_state and rcu_data structures. This may be called
  1205. * only from the CPU to whom the rdp belongs.
  1206. */
  1207. static void
  1208. __rcu_process_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
  1209. {
  1210. unsigned long flags;
  1211. WARN_ON_ONCE(rdp->beenonline == 0);
  1212. /*
  1213. * If an RCU GP has gone long enough, go check for dyntick
  1214. * idle CPUs and, if needed, send resched IPIs.
  1215. */
  1216. if (ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs), jiffies))
  1217. force_quiescent_state(rsp, 1);
  1218. /*
  1219. * Advance callbacks in response to end of earlier grace
  1220. * period that some other CPU ended.
  1221. */
  1222. rcu_process_gp_end(rsp, rdp);
  1223. /* Update RCU state based on any recent quiescent states. */
  1224. rcu_check_quiescent_state(rsp, rdp);
  1225. /* Does this CPU require a not-yet-started grace period? */
  1226. if (cpu_needs_another_gp(rsp, rdp)) {
  1227. raw_spin_lock_irqsave(&rcu_get_root(rsp)->lock, flags);
  1228. rcu_start_gp(rsp, flags); /* releases above lock */
  1229. }
  1230. /* If there are callbacks ready, invoke them. */
  1231. rcu_do_batch(rsp, rdp);
  1232. }
  1233. /*
  1234. * Do softirq processing for the current CPU.
  1235. */
  1236. static void rcu_process_callbacks(void)
  1237. {
  1238. __rcu_process_callbacks(&rcu_sched_state,
  1239. &__get_cpu_var(rcu_sched_data));
  1240. __rcu_process_callbacks(&rcu_bh_state, &__get_cpu_var(rcu_bh_data));
  1241. rcu_preempt_process_callbacks();
  1242. /* If we are last CPU on way to dyntick-idle mode, accelerate it. */
  1243. rcu_needs_cpu_flush();
  1244. }
  1245. /*
  1246. * Wake up the current CPU's kthread. This replaces raise_softirq()
  1247. * in earlier versions of RCU. Note that because we are running on
  1248. * the current CPU with interrupts disabled, the rcu_cpu_kthread_task
  1249. * cannot disappear out from under us.
  1250. */
  1251. static void invoke_rcu_cpu_kthread(void)
  1252. {
  1253. unsigned long flags;
  1254. wait_queue_head_t *q;
  1255. int cpu;
  1256. local_irq_save(flags);
  1257. cpu = smp_processor_id();
  1258. per_cpu(rcu_cpu_has_work, cpu) = 1;
  1259. if (per_cpu(rcu_cpu_kthread_task, cpu) == NULL) {
  1260. local_irq_restore(flags);
  1261. return;
  1262. }
  1263. q = &per_cpu(rcu_cpu_wq, cpu);
  1264. wake_up(q);
  1265. local_irq_restore(flags);
  1266. }
  1267. /*
  1268. * Wake up the specified per-rcu_node-structure kthread.
  1269. * The caller must hold ->lock.
  1270. */
  1271. static void invoke_rcu_node_kthread(struct rcu_node *rnp)
  1272. {
  1273. struct task_struct *t;
  1274. t = rnp->node_kthread_task;
  1275. if (t != NULL)
  1276. wake_up_process(t);
  1277. }
  1278. /*
  1279. * Timer handler to initiate the waking up of per-CPU kthreads that
  1280. * have yielded the CPU due to excess numbers of RCU callbacks.
  1281. * We wake up the per-rcu_node kthread, which in turn will wake up
  1282. * the booster kthread.
  1283. */
  1284. static void rcu_cpu_kthread_timer(unsigned long arg)
  1285. {
  1286. unsigned long flags;
  1287. struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, arg);
  1288. struct rcu_node *rnp = rdp->mynode;
  1289. raw_spin_lock_irqsave(&rnp->lock, flags);
  1290. rnp->wakemask |= rdp->grpmask;
  1291. invoke_rcu_node_kthread(rnp);
  1292. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1293. }
  1294. /*
  1295. * Drop to non-real-time priority and yield, but only after posting a
  1296. * timer that will cause us to regain our real-time priority if we
  1297. * remain preempted. Either way, we restore our real-time priority
  1298. * before returning.
  1299. */
  1300. static void rcu_yield(void (*f)(unsigned long), unsigned long arg)
  1301. {
  1302. struct sched_param sp;
  1303. struct timer_list yield_timer;
  1304. setup_timer_on_stack(&yield_timer, f, arg);
  1305. mod_timer(&yield_timer, jiffies + 2);
  1306. sp.sched_priority = 0;
  1307. sched_setscheduler_nocheck(current, SCHED_NORMAL, &sp);
  1308. schedule();
  1309. sp.sched_priority = RCU_KTHREAD_PRIO;
  1310. sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  1311. del_timer(&yield_timer);
  1312. }
  1313. /*
  1314. * Handle cases where the rcu_cpu_kthread() ends up on the wrong CPU.
  1315. * This can happen while the corresponding CPU is either coming online
  1316. * or going offline. We cannot wait until the CPU is fully online
  1317. * before starting the kthread, because the various notifier functions
  1318. * can wait for RCU grace periods. So we park rcu_cpu_kthread() until
  1319. * the corresponding CPU is online.
  1320. *
  1321. * Return 1 if the kthread needs to stop, 0 otherwise.
  1322. *
  1323. * Caller must disable bh. This function can momentarily enable it.
  1324. */
  1325. static int rcu_cpu_kthread_should_stop(int cpu)
  1326. {
  1327. while (cpu_is_offline(cpu) ||
  1328. !cpumask_equal(&current->cpus_allowed, cpumask_of(cpu)) ||
  1329. smp_processor_id() != cpu) {
  1330. if (kthread_should_stop())
  1331. return 1;
  1332. local_bh_enable();
  1333. schedule_timeout_uninterruptible(1);
  1334. if (!cpumask_equal(&current->cpus_allowed, cpumask_of(cpu)))
  1335. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  1336. local_bh_disable();
  1337. }
  1338. return 0;
  1339. }
  1340. /*
  1341. * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  1342. * earlier RCU softirq.
  1343. */
  1344. static int rcu_cpu_kthread(void *arg)
  1345. {
  1346. int cpu = (int)(long)arg;
  1347. unsigned long flags;
  1348. int spincnt = 0;
  1349. wait_queue_head_t *wqp = &per_cpu(rcu_cpu_wq, cpu);
  1350. char work;
  1351. char *workp = &per_cpu(rcu_cpu_has_work, cpu);
  1352. for (;;) {
  1353. wait_event_interruptible(*wqp,
  1354. *workp != 0 || kthread_should_stop());
  1355. local_bh_disable();
  1356. if (rcu_cpu_kthread_should_stop(cpu)) {
  1357. local_bh_enable();
  1358. break;
  1359. }
  1360. local_irq_save(flags);
  1361. work = *workp;
  1362. *workp = 0;
  1363. local_irq_restore(flags);
  1364. if (work)
  1365. rcu_process_callbacks();
  1366. local_bh_enable();
  1367. if (*workp != 0)
  1368. spincnt++;
  1369. else
  1370. spincnt = 0;
  1371. if (spincnt > 10) {
  1372. rcu_yield(rcu_cpu_kthread_timer, (unsigned long)cpu);
  1373. spincnt = 0;
  1374. }
  1375. }
  1376. return 0;
  1377. }
  1378. /*
  1379. * Spawn a per-CPU kthread, setting up affinity and priority.
  1380. * Because the CPU hotplug lock is held, no other CPU will be attempting
  1381. * to manipulate rcu_cpu_kthread_task. There might be another CPU
  1382. * attempting to access it during boot, but the locking in kthread_bind()
  1383. * will enforce sufficient ordering.
  1384. */
  1385. static int __cpuinit rcu_spawn_one_cpu_kthread(int cpu)
  1386. {
  1387. struct sched_param sp;
  1388. struct task_struct *t;
  1389. if (!rcu_kthreads_spawnable ||
  1390. per_cpu(rcu_cpu_kthread_task, cpu) != NULL)
  1391. return 0;
  1392. t = kthread_create(rcu_cpu_kthread, (void *)(long)cpu, "rcuc%d", cpu);
  1393. if (IS_ERR(t))
  1394. return PTR_ERR(t);
  1395. kthread_bind(t, cpu);
  1396. WARN_ON_ONCE(per_cpu(rcu_cpu_kthread_task, cpu) != NULL);
  1397. per_cpu(rcu_cpu_kthread_task, cpu) = t;
  1398. wake_up_process(t);
  1399. sp.sched_priority = RCU_KTHREAD_PRIO;
  1400. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  1401. return 0;
  1402. }
  1403. /*
  1404. * Per-rcu_node kthread, which is in charge of waking up the per-CPU
  1405. * kthreads when needed. We ignore requests to wake up kthreads
  1406. * for offline CPUs, which is OK because force_quiescent_state()
  1407. * takes care of this case.
  1408. */
  1409. static int rcu_node_kthread(void *arg)
  1410. {
  1411. int cpu;
  1412. unsigned long flags;
  1413. unsigned long mask;
  1414. struct rcu_node *rnp = (struct rcu_node *)arg;
  1415. struct sched_param sp;
  1416. struct task_struct *t;
  1417. for (;;) {
  1418. wait_event_interruptible(rnp->node_wq, rnp->wakemask != 0 ||
  1419. kthread_should_stop());
  1420. if (kthread_should_stop())
  1421. break;
  1422. raw_spin_lock_irqsave(&rnp->lock, flags);
  1423. mask = rnp->wakemask;
  1424. rnp->wakemask = 0;
  1425. rcu_initiate_boost(rnp);
  1426. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1427. for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask >>= 1) {
  1428. if ((mask & 0x1) == 0)
  1429. continue;
  1430. preempt_disable();
  1431. t = per_cpu(rcu_cpu_kthread_task, cpu);
  1432. if (!cpu_online(cpu) || t == NULL) {
  1433. preempt_enable();
  1434. continue;
  1435. }
  1436. per_cpu(rcu_cpu_has_work, cpu) = 1;
  1437. sp.sched_priority = RCU_KTHREAD_PRIO;
  1438. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  1439. preempt_enable();
  1440. }
  1441. }
  1442. return 0;
  1443. }
  1444. /*
  1445. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  1446. * served by the rcu_node in question. The CPU hotplug lock is still
  1447. * held, so the value of rnp->qsmaskinit will be stable.
  1448. */
  1449. static void rcu_node_kthread_setaffinity(struct rcu_node *rnp)
  1450. {
  1451. cpumask_var_t cm;
  1452. int cpu;
  1453. unsigned long mask = rnp->qsmaskinit;
  1454. if (rnp->node_kthread_task == NULL || mask == 0)
  1455. return;
  1456. if (!alloc_cpumask_var(&cm, GFP_KERNEL))
  1457. return;
  1458. cpumask_clear(cm);
  1459. for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask >>= 1)
  1460. if (mask & 0x1)
  1461. cpumask_set_cpu(cpu, cm);
  1462. set_cpus_allowed_ptr(rnp->node_kthread_task, cm);
  1463. rcu_boost_kthread_setaffinity(rnp, cm);
  1464. free_cpumask_var(cm);
  1465. }
  1466. /*
  1467. * Spawn a per-rcu_node kthread, setting priority and affinity.
  1468. * Called during boot before online/offline can happen, or, if
  1469. * during runtime, with the main CPU-hotplug locks held. So only
  1470. * one of these can be executing at a time.
  1471. */
  1472. static int __cpuinit rcu_spawn_one_node_kthread(struct rcu_state *rsp,
  1473. struct rcu_node *rnp)
  1474. {
  1475. unsigned long flags;
  1476. int rnp_index = rnp - &rsp->node[0];
  1477. struct sched_param sp;
  1478. struct task_struct *t;
  1479. if (!rcu_kthreads_spawnable ||
  1480. rnp->qsmaskinit == 0)
  1481. return 0;
  1482. if (rnp->node_kthread_task == NULL) {
  1483. t = kthread_create(rcu_node_kthread, (void *)rnp,
  1484. "rcun%d", rnp_index);
  1485. if (IS_ERR(t))
  1486. return PTR_ERR(t);
  1487. raw_spin_lock_irqsave(&rnp->lock, flags);
  1488. rnp->node_kthread_task = t;
  1489. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1490. wake_up_process(t);
  1491. sp.sched_priority = 99;
  1492. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  1493. }
  1494. return rcu_spawn_one_boost_kthread(rsp, rnp, rnp_index);
  1495. }
  1496. /*
  1497. * Spawn all kthreads -- called as soon as the scheduler is running.
  1498. */
  1499. static int __init rcu_spawn_kthreads(void)
  1500. {
  1501. int cpu;
  1502. struct rcu_node *rnp;
  1503. rcu_kthreads_spawnable = 1;
  1504. for_each_possible_cpu(cpu) {
  1505. init_waitqueue_head(&per_cpu(rcu_cpu_wq, cpu));
  1506. per_cpu(rcu_cpu_has_work, cpu) = 0;
  1507. if (cpu_online(cpu))
  1508. (void)rcu_spawn_one_cpu_kthread(cpu);
  1509. }
  1510. rnp = rcu_get_root(rcu_state);
  1511. init_waitqueue_head(&rnp->node_wq);
  1512. rcu_init_boost_waitqueue(rnp);
  1513. (void)rcu_spawn_one_node_kthread(rcu_state, rnp);
  1514. if (NUM_RCU_NODES > 1)
  1515. rcu_for_each_leaf_node(rcu_state, rnp) {
  1516. init_waitqueue_head(&rnp->node_wq);
  1517. rcu_init_boost_waitqueue(rnp);
  1518. (void)rcu_spawn_one_node_kthread(rcu_state, rnp);
  1519. }
  1520. return 0;
  1521. }
  1522. early_initcall(rcu_spawn_kthreads);
  1523. static void
  1524. __call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu),
  1525. struct rcu_state *rsp)
  1526. {
  1527. unsigned long flags;
  1528. struct rcu_data *rdp;
  1529. debug_rcu_head_queue(head);
  1530. head->func = func;
  1531. head->next = NULL;
  1532. smp_mb(); /* Ensure RCU update seen before callback registry. */
  1533. /*
  1534. * Opportunistically note grace-period endings and beginnings.
  1535. * Note that we might see a beginning right after we see an
  1536. * end, but never vice versa, since this CPU has to pass through
  1537. * a quiescent state betweentimes.
  1538. */
  1539. local_irq_save(flags);
  1540. rdp = this_cpu_ptr(rsp->rda);
  1541. /* Add the callback to our list. */
  1542. *rdp->nxttail[RCU_NEXT_TAIL] = head;
  1543. rdp->nxttail[RCU_NEXT_TAIL] = &head->next;
  1544. /*
  1545. * Force the grace period if too many callbacks or too long waiting.
  1546. * Enforce hysteresis, and don't invoke force_quiescent_state()
  1547. * if some other CPU has recently done so. Also, don't bother
  1548. * invoking force_quiescent_state() if the newly enqueued callback
  1549. * is the only one waiting for a grace period to complete.
  1550. */
  1551. if (unlikely(++rdp->qlen > rdp->qlen_last_fqs_check + qhimark)) {
  1552. /* Are we ignoring a completed grace period? */
  1553. rcu_process_gp_end(rsp, rdp);
  1554. check_for_new_grace_period(rsp, rdp);
  1555. /* Start a new grace period if one not already started. */
  1556. if (!rcu_gp_in_progress(rsp)) {
  1557. unsigned long nestflag;
  1558. struct rcu_node *rnp_root = rcu_get_root(rsp);
  1559. raw_spin_lock_irqsave(&rnp_root->lock, nestflag);
  1560. rcu_start_gp(rsp, nestflag); /* rlses rnp_root->lock */
  1561. } else {
  1562. /* Give the grace period a kick. */
  1563. rdp->blimit = LONG_MAX;
  1564. if (rsp->n_force_qs == rdp->n_force_qs_snap &&
  1565. *rdp->nxttail[RCU_DONE_TAIL] != head)
  1566. force_quiescent_state(rsp, 0);
  1567. rdp->n_force_qs_snap = rsp->n_force_qs;
  1568. rdp->qlen_last_fqs_check = rdp->qlen;
  1569. }
  1570. } else if (ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs), jiffies))
  1571. force_quiescent_state(rsp, 1);
  1572. local_irq_restore(flags);
  1573. }
  1574. /*
  1575. * Queue an RCU-sched callback for invocation after a grace period.
  1576. */
  1577. void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  1578. {
  1579. __call_rcu(head, func, &rcu_sched_state);
  1580. }
  1581. EXPORT_SYMBOL_GPL(call_rcu_sched);
  1582. /*
  1583. * Queue an RCU for invocation after a quicker grace period.
  1584. */
  1585. void call_rcu_bh(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  1586. {
  1587. __call_rcu(head, func, &rcu_bh_state);
  1588. }
  1589. EXPORT_SYMBOL_GPL(call_rcu_bh);
  1590. /**
  1591. * synchronize_sched - wait until an rcu-sched grace period has elapsed.
  1592. *
  1593. * Control will return to the caller some time after a full rcu-sched
  1594. * grace period has elapsed, in other words after all currently executing
  1595. * rcu-sched read-side critical sections have completed. These read-side
  1596. * critical sections are delimited by rcu_read_lock_sched() and
  1597. * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
  1598. * local_irq_disable(), and so on may be used in place of
  1599. * rcu_read_lock_sched().
  1600. *
  1601. * This means that all preempt_disable code sequences, including NMI and
  1602. * hardware-interrupt handlers, in progress on entry will have completed
  1603. * before this primitive returns. However, this does not guarantee that
  1604. * softirq handlers will have completed, since in some kernels, these
  1605. * handlers can run in process context, and can block.
  1606. *
  1607. * This primitive provides the guarantees made by the (now removed)
  1608. * synchronize_kernel() API. In contrast, synchronize_rcu() only
  1609. * guarantees that rcu_read_lock() sections will have completed.
  1610. * In "classic RCU", these two guarantees happen to be one and
  1611. * the same, but can differ in realtime RCU implementations.
  1612. */
  1613. void synchronize_sched(void)
  1614. {
  1615. struct rcu_synchronize rcu;
  1616. if (rcu_blocking_is_gp())
  1617. return;
  1618. init_rcu_head_on_stack(&rcu.head);
  1619. init_completion(&rcu.completion);
  1620. /* Will wake me after RCU finished. */
  1621. call_rcu_sched(&rcu.head, wakeme_after_rcu);
  1622. /* Wait for it. */
  1623. wait_for_completion(&rcu.completion);
  1624. destroy_rcu_head_on_stack(&rcu.head);
  1625. }
  1626. EXPORT_SYMBOL_GPL(synchronize_sched);
  1627. /**
  1628. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  1629. *
  1630. * Control will return to the caller some time after a full rcu_bh grace
  1631. * period has elapsed, in other words after all currently executing rcu_bh
  1632. * read-side critical sections have completed. RCU read-side critical
  1633. * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
  1634. * and may be nested.
  1635. */
  1636. void synchronize_rcu_bh(void)
  1637. {
  1638. struct rcu_synchronize rcu;
  1639. if (rcu_blocking_is_gp())
  1640. return;
  1641. init_rcu_head_on_stack(&rcu.head);
  1642. init_completion(&rcu.completion);
  1643. /* Will wake me after RCU finished. */
  1644. call_rcu_bh(&rcu.head, wakeme_after_rcu);
  1645. /* Wait for it. */
  1646. wait_for_completion(&rcu.completion);
  1647. destroy_rcu_head_on_stack(&rcu.head);
  1648. }
  1649. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  1650. /*
  1651. * Check to see if there is any immediate RCU-related work to be done
  1652. * by the current CPU, for the specified type of RCU, returning 1 if so.
  1653. * The checks are in order of increasing expense: checks that can be
  1654. * carried out against CPU-local state are performed first. However,
  1655. * we must check for CPU stalls first, else we might not get a chance.
  1656. */
  1657. static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
  1658. {
  1659. struct rcu_node *rnp = rdp->mynode;
  1660. rdp->n_rcu_pending++;
  1661. /* Check for CPU stalls, if enabled. */
  1662. check_cpu_stall(rsp, rdp);
  1663. /* Is the RCU core waiting for a quiescent state from this CPU? */
  1664. if (rdp->qs_pending && !rdp->passed_quiesc) {
  1665. /*
  1666. * If force_quiescent_state() coming soon and this CPU
  1667. * needs a quiescent state, and this is either RCU-sched
  1668. * or RCU-bh, force a local reschedule.
  1669. */
  1670. rdp->n_rp_qs_pending++;
  1671. if (!rdp->preemptable &&
  1672. ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs) - 1,
  1673. jiffies))
  1674. set_need_resched();
  1675. } else if (rdp->qs_pending && rdp->passed_quiesc) {
  1676. rdp->n_rp_report_qs++;
  1677. return 1;
  1678. }
  1679. /* Does this CPU have callbacks ready to invoke? */
  1680. if (cpu_has_callbacks_ready_to_invoke(rdp)) {
  1681. rdp->n_rp_cb_ready++;
  1682. return 1;
  1683. }
  1684. /* Has RCU gone idle with this CPU needing another grace period? */
  1685. if (cpu_needs_another_gp(rsp, rdp)) {
  1686. rdp->n_rp_cpu_needs_gp++;
  1687. return 1;
  1688. }
  1689. /* Has another RCU grace period completed? */
  1690. if (ACCESS_ONCE(rnp->completed) != rdp->completed) { /* outside lock */
  1691. rdp->n_rp_gp_completed++;
  1692. return 1;
  1693. }
  1694. /* Has a new RCU grace period started? */
  1695. if (ACCESS_ONCE(rnp->gpnum) != rdp->gpnum) { /* outside lock */
  1696. rdp->n_rp_gp_started++;
  1697. return 1;
  1698. }
  1699. /* Has an RCU GP gone long enough to send resched IPIs &c? */
  1700. if (rcu_gp_in_progress(rsp) &&
  1701. ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs), jiffies)) {
  1702. rdp->n_rp_need_fqs++;
  1703. return 1;
  1704. }
  1705. /* nothing to do */
  1706. rdp->n_rp_need_nothing++;
  1707. return 0;
  1708. }
  1709. /*
  1710. * Check to see if there is any immediate RCU-related work to be done
  1711. * by the current CPU, returning 1 if so. This function is part of the
  1712. * RCU implementation; it is -not- an exported member of the RCU API.
  1713. */
  1714. static int rcu_pending(int cpu)
  1715. {
  1716. return __rcu_pending(&rcu_sched_state, &per_cpu(rcu_sched_data, cpu)) ||
  1717. __rcu_pending(&rcu_bh_state, &per_cpu(rcu_bh_data, cpu)) ||
  1718. rcu_preempt_pending(cpu);
  1719. }
  1720. /*
  1721. * Check to see if any future RCU-related work will need to be done
  1722. * by the current CPU, even if none need be done immediately, returning
  1723. * 1 if so.
  1724. */
  1725. static int rcu_needs_cpu_quick_check(int cpu)
  1726. {
  1727. /* RCU callbacks either ready or pending? */
  1728. return per_cpu(rcu_sched_data, cpu).nxtlist ||
  1729. per_cpu(rcu_bh_data, cpu).nxtlist ||
  1730. rcu_preempt_needs_cpu(cpu);
  1731. }
  1732. static DEFINE_PER_CPU(struct rcu_head, rcu_barrier_head) = {NULL};
  1733. static atomic_t rcu_barrier_cpu_count;
  1734. static DEFINE_MUTEX(rcu_barrier_mutex);
  1735. static struct completion rcu_barrier_completion;
  1736. static void rcu_barrier_callback(struct rcu_head *notused)
  1737. {
  1738. if (atomic_dec_and_test(&rcu_barrier_cpu_count))
  1739. complete(&rcu_barrier_completion);
  1740. }
  1741. /*
  1742. * Called with preemption disabled, and from cross-cpu IRQ context.
  1743. */
  1744. static void rcu_barrier_func(void *type)
  1745. {
  1746. int cpu = smp_processor_id();
  1747. struct rcu_head *head = &per_cpu(rcu_barrier_head, cpu);
  1748. void (*call_rcu_func)(struct rcu_head *head,
  1749. void (*func)(struct rcu_head *head));
  1750. atomic_inc(&rcu_barrier_cpu_count);
  1751. call_rcu_func = type;
  1752. call_rcu_func(head, rcu_barrier_callback);
  1753. }
  1754. /*
  1755. * Orchestrate the specified type of RCU barrier, waiting for all
  1756. * RCU callbacks of the specified type to complete.
  1757. */
  1758. static void _rcu_barrier(struct rcu_state *rsp,
  1759. void (*call_rcu_func)(struct rcu_head *head,
  1760. void (*func)(struct rcu_head *head)))
  1761. {
  1762. BUG_ON(in_interrupt());
  1763. /* Take mutex to serialize concurrent rcu_barrier() requests. */
  1764. mutex_lock(&rcu_barrier_mutex);
  1765. init_completion(&rcu_barrier_completion);
  1766. /*
  1767. * Initialize rcu_barrier_cpu_count to 1, then invoke
  1768. * rcu_barrier_func() on each CPU, so that each CPU also has
  1769. * incremented rcu_barrier_cpu_count. Only then is it safe to
  1770. * decrement rcu_barrier_cpu_count -- otherwise the first CPU
  1771. * might complete its grace period before all of the other CPUs
  1772. * did their increment, causing this function to return too
  1773. * early. Note that on_each_cpu() disables irqs, which prevents
  1774. * any CPUs from coming online or going offline until each online
  1775. * CPU has queued its RCU-barrier callback.
  1776. */
  1777. atomic_set(&rcu_barrier_cpu_count, 1);
  1778. on_each_cpu(rcu_barrier_func, (void *)call_rcu_func, 1);
  1779. if (atomic_dec_and_test(&rcu_barrier_cpu_count))
  1780. complete(&rcu_barrier_completion);
  1781. wait_for_completion(&rcu_barrier_completion);
  1782. mutex_unlock(&rcu_barrier_mutex);
  1783. }
  1784. /**
  1785. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  1786. */
  1787. void rcu_barrier_bh(void)
  1788. {
  1789. _rcu_barrier(&rcu_bh_state, call_rcu_bh);
  1790. }
  1791. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  1792. /**
  1793. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  1794. */
  1795. void rcu_barrier_sched(void)
  1796. {
  1797. _rcu_barrier(&rcu_sched_state, call_rcu_sched);
  1798. }
  1799. EXPORT_SYMBOL_GPL(rcu_barrier_sched);
  1800. /*
  1801. * Do boot-time initialization of a CPU's per-CPU RCU data.
  1802. */
  1803. static void __init
  1804. rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp)
  1805. {
  1806. unsigned long flags;
  1807. int i;
  1808. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  1809. struct rcu_node *rnp = rcu_get_root(rsp);
  1810. /* Set up local state, ensuring consistent view of global state. */
  1811. raw_spin_lock_irqsave(&rnp->lock, flags);
  1812. rdp->grpmask = 1UL << (cpu - rdp->mynode->grplo);
  1813. rdp->nxtlist = NULL;
  1814. for (i = 0; i < RCU_NEXT_SIZE; i++)
  1815. rdp->nxttail[i] = &rdp->nxtlist;
  1816. rdp->qlen = 0;
  1817. #ifdef CONFIG_NO_HZ
  1818. rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
  1819. #endif /* #ifdef CONFIG_NO_HZ */
  1820. rdp->cpu = cpu;
  1821. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1822. }
  1823. /*
  1824. * Initialize a CPU's per-CPU RCU data. Note that only one online or
  1825. * offline event can be happening at a given time. Note also that we
  1826. * can accept some slop in the rsp->completed access due to the fact
  1827. * that this CPU cannot possibly have any RCU callbacks in flight yet.
  1828. */
  1829. static void __cpuinit
  1830. rcu_init_percpu_data(int cpu, struct rcu_state *rsp, int preemptable)
  1831. {
  1832. unsigned long flags;
  1833. unsigned long mask;
  1834. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  1835. struct rcu_node *rnp = rcu_get_root(rsp);
  1836. /* Set up local state, ensuring consistent view of global state. */
  1837. raw_spin_lock_irqsave(&rnp->lock, flags);
  1838. rdp->passed_quiesc = 0; /* We could be racing with new GP, */
  1839. rdp->qs_pending = 1; /* so set up to respond to current GP. */
  1840. rdp->beenonline = 1; /* We have now been online. */
  1841. rdp->preemptable = preemptable;
  1842. rdp->qlen_last_fqs_check = 0;
  1843. rdp->n_force_qs_snap = rsp->n_force_qs;
  1844. rdp->blimit = blimit;
  1845. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  1846. /*
  1847. * A new grace period might start here. If so, we won't be part
  1848. * of it, but that is OK, as we are currently in a quiescent state.
  1849. */
  1850. /* Exclude any attempts to start a new GP on large systems. */
  1851. raw_spin_lock(&rsp->onofflock); /* irqs already disabled. */
  1852. /* Add CPU to rcu_node bitmasks. */
  1853. rnp = rdp->mynode;
  1854. mask = rdp->grpmask;
  1855. do {
  1856. /* Exclude any attempts to start a new GP on small systems. */
  1857. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  1858. rnp->qsmaskinit |= mask;
  1859. mask = rnp->grpmask;
  1860. if (rnp == rdp->mynode) {
  1861. rdp->gpnum = rnp->completed; /* if GP in progress... */
  1862. rdp->completed = rnp->completed;
  1863. rdp->passed_quiesc_completed = rnp->completed - 1;
  1864. }
  1865. raw_spin_unlock(&rnp->lock); /* irqs already disabled. */
  1866. rnp = rnp->parent;
  1867. } while (rnp != NULL && !(rnp->qsmaskinit & mask));
  1868. raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
  1869. }
  1870. static void __cpuinit rcu_online_cpu(int cpu)
  1871. {
  1872. rcu_init_percpu_data(cpu, &rcu_sched_state, 0);
  1873. rcu_init_percpu_data(cpu, &rcu_bh_state, 0);
  1874. rcu_preempt_init_percpu_data(cpu);
  1875. }
  1876. static void __cpuinit rcu_online_kthreads(int cpu)
  1877. {
  1878. struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, cpu);
  1879. struct rcu_node *rnp = rdp->mynode;
  1880. /* Fire up the incoming CPU's kthread and leaf rcu_node kthread. */
  1881. if (rcu_kthreads_spawnable) {
  1882. (void)rcu_spawn_one_cpu_kthread(cpu);
  1883. if (rnp->node_kthread_task == NULL)
  1884. (void)rcu_spawn_one_node_kthread(rcu_state, rnp);
  1885. }
  1886. }
  1887. /*
  1888. * Handle CPU online/offline notification events.
  1889. */
  1890. static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
  1891. unsigned long action, void *hcpu)
  1892. {
  1893. long cpu = (long)hcpu;
  1894. struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, cpu);
  1895. struct rcu_node *rnp = rdp->mynode;
  1896. switch (action) {
  1897. case CPU_UP_PREPARE:
  1898. case CPU_UP_PREPARE_FROZEN:
  1899. rcu_online_cpu(cpu);
  1900. rcu_online_kthreads(cpu);
  1901. break;
  1902. case CPU_ONLINE:
  1903. rcu_node_kthread_setaffinity(rnp);
  1904. break;
  1905. case CPU_DYING:
  1906. case CPU_DYING_FROZEN:
  1907. /*
  1908. * The whole machine is "stopped" except this CPU, so we can
  1909. * touch any data without introducing corruption. We send the
  1910. * dying CPU's callbacks to an arbitrarily chosen online CPU.
  1911. */
  1912. rcu_send_cbs_to_online(&rcu_bh_state);
  1913. rcu_send_cbs_to_online(&rcu_sched_state);
  1914. rcu_preempt_send_cbs_to_online();
  1915. break;
  1916. case CPU_DEAD:
  1917. case CPU_DEAD_FROZEN:
  1918. case CPU_UP_CANCELED:
  1919. case CPU_UP_CANCELED_FROZEN:
  1920. rcu_offline_cpu(cpu);
  1921. break;
  1922. default:
  1923. break;
  1924. }
  1925. return NOTIFY_OK;
  1926. }
  1927. /*
  1928. * This function is invoked towards the end of the scheduler's initialization
  1929. * process. Before this is called, the idle task might contain
  1930. * RCU read-side critical sections (during which time, this idle
  1931. * task is booting the system). After this function is called, the
  1932. * idle tasks are prohibited from containing RCU read-side critical
  1933. * sections. This function also enables RCU lockdep checking.
  1934. */
  1935. void rcu_scheduler_starting(void)
  1936. {
  1937. WARN_ON(num_online_cpus() != 1);
  1938. WARN_ON(nr_context_switches() > 0);
  1939. rcu_scheduler_active = 1;
  1940. }
  1941. /*
  1942. * Compute the per-level fanout, either using the exact fanout specified
  1943. * or balancing the tree, depending on CONFIG_RCU_FANOUT_EXACT.
  1944. */
  1945. #ifdef CONFIG_RCU_FANOUT_EXACT
  1946. static void __init rcu_init_levelspread(struct rcu_state *rsp)
  1947. {
  1948. int i;
  1949. for (i = NUM_RCU_LVLS - 1; i > 0; i--)
  1950. rsp->levelspread[i] = CONFIG_RCU_FANOUT;
  1951. rsp->levelspread[0] = RCU_FANOUT_LEAF;
  1952. }
  1953. #else /* #ifdef CONFIG_RCU_FANOUT_EXACT */
  1954. static void __init rcu_init_levelspread(struct rcu_state *rsp)
  1955. {
  1956. int ccur;
  1957. int cprv;
  1958. int i;
  1959. cprv = NR_CPUS;
  1960. for (i = NUM_RCU_LVLS - 1; i >= 0; i--) {
  1961. ccur = rsp->levelcnt[i];
  1962. rsp->levelspread[i] = (cprv + ccur - 1) / ccur;
  1963. cprv = ccur;
  1964. }
  1965. }
  1966. #endif /* #else #ifdef CONFIG_RCU_FANOUT_EXACT */
  1967. /*
  1968. * Helper function for rcu_init() that initializes one rcu_state structure.
  1969. */
  1970. static void __init rcu_init_one(struct rcu_state *rsp,
  1971. struct rcu_data __percpu *rda)
  1972. {
  1973. static char *buf[] = { "rcu_node_level_0",
  1974. "rcu_node_level_1",
  1975. "rcu_node_level_2",
  1976. "rcu_node_level_3" }; /* Match MAX_RCU_LVLS */
  1977. int cpustride = 1;
  1978. int i;
  1979. int j;
  1980. struct rcu_node *rnp;
  1981. BUILD_BUG_ON(MAX_RCU_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
  1982. /* Initialize the level-tracking arrays. */
  1983. for (i = 1; i < NUM_RCU_LVLS; i++)
  1984. rsp->level[i] = rsp->level[i - 1] + rsp->levelcnt[i - 1];
  1985. rcu_init_levelspread(rsp);
  1986. /* Initialize the elements themselves, starting from the leaves. */
  1987. for (i = NUM_RCU_LVLS - 1; i >= 0; i--) {
  1988. cpustride *= rsp->levelspread[i];
  1989. rnp = rsp->level[i];
  1990. for (j = 0; j < rsp->levelcnt[i]; j++, rnp++) {
  1991. raw_spin_lock_init(&rnp->lock);
  1992. lockdep_set_class_and_name(&rnp->lock,
  1993. &rcu_node_class[i], buf[i]);
  1994. rnp->gpnum = 0;
  1995. rnp->qsmask = 0;
  1996. rnp->qsmaskinit = 0;
  1997. rnp->grplo = j * cpustride;
  1998. rnp->grphi = (j + 1) * cpustride - 1;
  1999. if (rnp->grphi >= NR_CPUS)
  2000. rnp->grphi = NR_CPUS - 1;
  2001. if (i == 0) {
  2002. rnp->grpnum = 0;
  2003. rnp->grpmask = 0;
  2004. rnp->parent = NULL;
  2005. } else {
  2006. rnp->grpnum = j % rsp->levelspread[i - 1];
  2007. rnp->grpmask = 1UL << rnp->grpnum;
  2008. rnp->parent = rsp->level[i - 1] +
  2009. j / rsp->levelspread[i - 1];
  2010. }
  2011. rnp->level = i;
  2012. INIT_LIST_HEAD(&rnp->blkd_tasks);
  2013. }
  2014. }
  2015. rsp->rda = rda;
  2016. rnp = rsp->level[NUM_RCU_LVLS - 1];
  2017. for_each_possible_cpu(i) {
  2018. while (i > rnp->grphi)
  2019. rnp++;
  2020. per_cpu_ptr(rsp->rda, i)->mynode = rnp;
  2021. rcu_boot_init_percpu_data(i, rsp);
  2022. }
  2023. }
  2024. void __init rcu_init(void)
  2025. {
  2026. int cpu;
  2027. rcu_bootup_announce();
  2028. rcu_init_one(&rcu_sched_state, &rcu_sched_data);
  2029. rcu_init_one(&rcu_bh_state, &rcu_bh_data);
  2030. __rcu_init_preempt();
  2031. /*
  2032. * We don't need protection against CPU-hotplug here because
  2033. * this is called early in boot, before either interrupts
  2034. * or the scheduler are operational.
  2035. */
  2036. cpu_notifier(rcu_cpu_notify, 0);
  2037. for_each_online_cpu(cpu)
  2038. rcu_cpu_notify(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
  2039. check_cpu_stall_init();
  2040. }
  2041. #include "rcutree_plugin.h"