rcutree.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899
  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 "rcutree.h"
  51. /* Data structures. */
  52. static struct lock_class_key rcu_node_class[NUM_RCU_LVLS];
  53. #define RCU_STATE_INITIALIZER(name) { \
  54. .level = { &name.node[0] }, \
  55. .levelcnt = { \
  56. NUM_RCU_LVL_0, /* root of hierarchy. */ \
  57. NUM_RCU_LVL_1, \
  58. NUM_RCU_LVL_2, \
  59. NUM_RCU_LVL_3, \
  60. NUM_RCU_LVL_4, /* == MAX_RCU_LVLS */ \
  61. }, \
  62. .signaled = RCU_GP_IDLE, \
  63. .gpnum = -300, \
  64. .completed = -300, \
  65. .onofflock = __SPIN_LOCK_UNLOCKED(&name.onofflock), \
  66. .orphan_cbs_list = NULL, \
  67. .orphan_cbs_tail = &name.orphan_cbs_list, \
  68. .orphan_qlen = 0, \
  69. .fqslock = __SPIN_LOCK_UNLOCKED(&name.fqslock), \
  70. .n_force_qs = 0, \
  71. .n_force_qs_ngp = 0, \
  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 int rcu_scheduler_active __read_mostly;
  78. /*
  79. * Return true if an RCU grace period is in progress. The ACCESS_ONCE()s
  80. * permit this function to be invoked without holding the root rcu_node
  81. * structure's ->lock, but of course results can be subject to change.
  82. */
  83. static int rcu_gp_in_progress(struct rcu_state *rsp)
  84. {
  85. return ACCESS_ONCE(rsp->completed) != ACCESS_ONCE(rsp->gpnum);
  86. }
  87. /*
  88. * Note a quiescent state. Because we do not need to know
  89. * how many quiescent states passed, just if there was at least
  90. * one since the start of the grace period, this just sets a flag.
  91. */
  92. void rcu_sched_qs(int cpu)
  93. {
  94. struct rcu_data *rdp;
  95. rdp = &per_cpu(rcu_sched_data, cpu);
  96. rdp->passed_quiesc_completed = rdp->gpnum - 1;
  97. barrier();
  98. rdp->passed_quiesc = 1;
  99. rcu_preempt_note_context_switch(cpu);
  100. }
  101. void rcu_bh_qs(int cpu)
  102. {
  103. struct rcu_data *rdp;
  104. rdp = &per_cpu(rcu_bh_data, cpu);
  105. rdp->passed_quiesc_completed = rdp->gpnum - 1;
  106. barrier();
  107. rdp->passed_quiesc = 1;
  108. }
  109. #ifdef CONFIG_NO_HZ
  110. DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
  111. .dynticks_nesting = 1,
  112. .dynticks = 1,
  113. };
  114. #endif /* #ifdef CONFIG_NO_HZ */
  115. static int blimit = 10; /* Maximum callbacks per softirq. */
  116. static int qhimark = 10000; /* If this many pending, ignore blimit. */
  117. static int qlowmark = 100; /* Once only this many pending, use blimit. */
  118. module_param(blimit, int, 0);
  119. module_param(qhimark, int, 0);
  120. module_param(qlowmark, int, 0);
  121. static void force_quiescent_state(struct rcu_state *rsp, int relaxed);
  122. static int rcu_pending(int cpu);
  123. /*
  124. * Return the number of RCU-sched batches processed thus far for debug & stats.
  125. */
  126. long rcu_batches_completed_sched(void)
  127. {
  128. return rcu_sched_state.completed;
  129. }
  130. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  131. /*
  132. * Return the number of RCU BH batches processed thus far for debug & stats.
  133. */
  134. long rcu_batches_completed_bh(void)
  135. {
  136. return rcu_bh_state.completed;
  137. }
  138. EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
  139. /*
  140. * Does the CPU have callbacks ready to be invoked?
  141. */
  142. static int
  143. cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp)
  144. {
  145. return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL];
  146. }
  147. /*
  148. * Does the current CPU require a yet-as-unscheduled grace period?
  149. */
  150. static int
  151. cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
  152. {
  153. return *rdp->nxttail[RCU_DONE_TAIL] && !rcu_gp_in_progress(rsp);
  154. }
  155. /*
  156. * Return the root node of the specified rcu_state structure.
  157. */
  158. static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
  159. {
  160. return &rsp->node[0];
  161. }
  162. #ifdef CONFIG_SMP
  163. /*
  164. * If the specified CPU is offline, tell the caller that it is in
  165. * a quiescent state. Otherwise, whack it with a reschedule IPI.
  166. * Grace periods can end up waiting on an offline CPU when that
  167. * CPU is in the process of coming online -- it will be added to the
  168. * rcu_node bitmasks before it actually makes it online. The same thing
  169. * can happen while a CPU is in the process of coming online. Because this
  170. * race is quite rare, we check for it after detecting that the grace
  171. * period has been delayed rather than checking each and every CPU
  172. * each and every time we start a new grace period.
  173. */
  174. static int rcu_implicit_offline_qs(struct rcu_data *rdp)
  175. {
  176. /*
  177. * If the CPU is offline, it is in a quiescent state. We can
  178. * trust its state not to change because interrupts are disabled.
  179. */
  180. if (cpu_is_offline(rdp->cpu)) {
  181. rdp->offline_fqs++;
  182. return 1;
  183. }
  184. /* If preemptable RCU, no point in sending reschedule IPI. */
  185. if (rdp->preemptable)
  186. return 0;
  187. /* The CPU is online, so send it a reschedule IPI. */
  188. if (rdp->cpu != smp_processor_id())
  189. smp_send_reschedule(rdp->cpu);
  190. else
  191. set_need_resched();
  192. rdp->resched_ipi++;
  193. return 0;
  194. }
  195. #endif /* #ifdef CONFIG_SMP */
  196. #ifdef CONFIG_NO_HZ
  197. /**
  198. * rcu_enter_nohz - inform RCU that current CPU is entering nohz
  199. *
  200. * Enter nohz mode, in other words, -leave- the mode in which RCU
  201. * read-side critical sections can occur. (Though RCU read-side
  202. * critical sections can occur in irq handlers in nohz mode, a possibility
  203. * handled by rcu_irq_enter() and rcu_irq_exit()).
  204. */
  205. void rcu_enter_nohz(void)
  206. {
  207. unsigned long flags;
  208. struct rcu_dynticks *rdtp;
  209. smp_mb(); /* CPUs seeing ++ must see prior RCU read-side crit sects */
  210. local_irq_save(flags);
  211. rdtp = &__get_cpu_var(rcu_dynticks);
  212. rdtp->dynticks++;
  213. rdtp->dynticks_nesting--;
  214. WARN_ON_ONCE(rdtp->dynticks & 0x1);
  215. local_irq_restore(flags);
  216. }
  217. /*
  218. * rcu_exit_nohz - inform RCU that current CPU is leaving nohz
  219. *
  220. * Exit nohz mode, in other words, -enter- the mode in which RCU
  221. * read-side critical sections normally occur.
  222. */
  223. void rcu_exit_nohz(void)
  224. {
  225. unsigned long flags;
  226. struct rcu_dynticks *rdtp;
  227. local_irq_save(flags);
  228. rdtp = &__get_cpu_var(rcu_dynticks);
  229. rdtp->dynticks++;
  230. rdtp->dynticks_nesting++;
  231. WARN_ON_ONCE(!(rdtp->dynticks & 0x1));
  232. local_irq_restore(flags);
  233. smp_mb(); /* CPUs seeing ++ must see later RCU read-side crit sects */
  234. }
  235. /**
  236. * rcu_nmi_enter - inform RCU of entry to NMI context
  237. *
  238. * If the CPU was idle with dynamic ticks active, and there is no
  239. * irq handler running, this updates rdtp->dynticks_nmi to let the
  240. * RCU grace-period handling know that the CPU is active.
  241. */
  242. void rcu_nmi_enter(void)
  243. {
  244. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  245. if (rdtp->dynticks & 0x1)
  246. return;
  247. rdtp->dynticks_nmi++;
  248. WARN_ON_ONCE(!(rdtp->dynticks_nmi & 0x1));
  249. smp_mb(); /* CPUs seeing ++ must see later RCU read-side crit sects */
  250. }
  251. /**
  252. * rcu_nmi_exit - inform RCU of exit from NMI context
  253. *
  254. * If the CPU was idle with dynamic ticks active, and there is no
  255. * irq handler running, this updates rdtp->dynticks_nmi to let the
  256. * RCU grace-period handling know that the CPU is no longer active.
  257. */
  258. void rcu_nmi_exit(void)
  259. {
  260. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  261. if (rdtp->dynticks & 0x1)
  262. return;
  263. smp_mb(); /* CPUs seeing ++ must see prior RCU read-side crit sects */
  264. rdtp->dynticks_nmi++;
  265. WARN_ON_ONCE(rdtp->dynticks_nmi & 0x1);
  266. }
  267. /**
  268. * rcu_irq_enter - inform RCU of entry to hard irq context
  269. *
  270. * If the CPU was idle with dynamic ticks active, this updates the
  271. * rdtp->dynticks to let the RCU handling know that the CPU is active.
  272. */
  273. void rcu_irq_enter(void)
  274. {
  275. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  276. if (rdtp->dynticks_nesting++)
  277. return;
  278. rdtp->dynticks++;
  279. WARN_ON_ONCE(!(rdtp->dynticks & 0x1));
  280. smp_mb(); /* CPUs seeing ++ must see later RCU read-side crit sects */
  281. }
  282. /**
  283. * rcu_irq_exit - inform RCU of exit from hard irq context
  284. *
  285. * If the CPU was idle with dynamic ticks active, update the rdp->dynticks
  286. * to put let the RCU handling be aware that the CPU is going back to idle
  287. * with no ticks.
  288. */
  289. void rcu_irq_exit(void)
  290. {
  291. struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
  292. if (--rdtp->dynticks_nesting)
  293. return;
  294. smp_mb(); /* CPUs seeing ++ must see prior RCU read-side crit sects */
  295. rdtp->dynticks++;
  296. WARN_ON_ONCE(rdtp->dynticks & 0x1);
  297. /* If the interrupt queued a callback, get out of dyntick mode. */
  298. if (__get_cpu_var(rcu_sched_data).nxtlist ||
  299. __get_cpu_var(rcu_bh_data).nxtlist)
  300. set_need_resched();
  301. }
  302. #ifdef CONFIG_SMP
  303. /*
  304. * Snapshot the specified CPU's dynticks counter so that we can later
  305. * credit them with an implicit quiescent state. Return 1 if this CPU
  306. * is in dynticks idle mode, which is an extended quiescent state.
  307. */
  308. static int dyntick_save_progress_counter(struct rcu_data *rdp)
  309. {
  310. int ret;
  311. int snap;
  312. int snap_nmi;
  313. snap = rdp->dynticks->dynticks;
  314. snap_nmi = rdp->dynticks->dynticks_nmi;
  315. smp_mb(); /* Order sampling of snap with end of grace period. */
  316. rdp->dynticks_snap = snap;
  317. rdp->dynticks_nmi_snap = snap_nmi;
  318. ret = ((snap & 0x1) == 0) && ((snap_nmi & 0x1) == 0);
  319. if (ret)
  320. rdp->dynticks_fqs++;
  321. return ret;
  322. }
  323. /*
  324. * Return true if the specified CPU has passed through a quiescent
  325. * state by virtue of being in or having passed through an dynticks
  326. * idle state since the last call to dyntick_save_progress_counter()
  327. * for this same CPU.
  328. */
  329. static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
  330. {
  331. long curr;
  332. long curr_nmi;
  333. long snap;
  334. long snap_nmi;
  335. curr = rdp->dynticks->dynticks;
  336. snap = rdp->dynticks_snap;
  337. curr_nmi = rdp->dynticks->dynticks_nmi;
  338. snap_nmi = rdp->dynticks_nmi_snap;
  339. smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
  340. /*
  341. * If the CPU passed through or entered a dynticks idle phase with
  342. * no active irq/NMI handlers, then we can safely pretend that the CPU
  343. * already acknowledged the request to pass through a quiescent
  344. * state. Either way, that CPU cannot possibly be in an RCU
  345. * read-side critical section that started before the beginning
  346. * of the current RCU grace period.
  347. */
  348. if ((curr != snap || (curr & 0x1) == 0) &&
  349. (curr_nmi != snap_nmi || (curr_nmi & 0x1) == 0)) {
  350. rdp->dynticks_fqs++;
  351. return 1;
  352. }
  353. /* Go check for the CPU being offline. */
  354. return rcu_implicit_offline_qs(rdp);
  355. }
  356. #endif /* #ifdef CONFIG_SMP */
  357. #else /* #ifdef CONFIG_NO_HZ */
  358. #ifdef CONFIG_SMP
  359. static int dyntick_save_progress_counter(struct rcu_data *rdp)
  360. {
  361. return 0;
  362. }
  363. static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
  364. {
  365. return rcu_implicit_offline_qs(rdp);
  366. }
  367. #endif /* #ifdef CONFIG_SMP */
  368. #endif /* #else #ifdef CONFIG_NO_HZ */
  369. #ifdef CONFIG_RCU_CPU_STALL_DETECTOR
  370. static void record_gp_stall_check_time(struct rcu_state *rsp)
  371. {
  372. rsp->gp_start = jiffies;
  373. rsp->jiffies_stall = jiffies + RCU_SECONDS_TILL_STALL_CHECK;
  374. }
  375. static void print_other_cpu_stall(struct rcu_state *rsp)
  376. {
  377. int cpu;
  378. long delta;
  379. unsigned long flags;
  380. struct rcu_node *rnp = rcu_get_root(rsp);
  381. /* Only let one CPU complain about others per time interval. */
  382. spin_lock_irqsave(&rnp->lock, flags);
  383. delta = jiffies - rsp->jiffies_stall;
  384. if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) {
  385. spin_unlock_irqrestore(&rnp->lock, flags);
  386. return;
  387. }
  388. rsp->jiffies_stall = jiffies + RCU_SECONDS_TILL_STALL_RECHECK;
  389. /*
  390. * Now rat on any tasks that got kicked up to the root rcu_node
  391. * due to CPU offlining.
  392. */
  393. rcu_print_task_stall(rnp);
  394. spin_unlock_irqrestore(&rnp->lock, flags);
  395. /* OK, time to rat on our buddy... */
  396. printk(KERN_ERR "INFO: RCU detected CPU stalls:");
  397. rcu_for_each_leaf_node(rsp, rnp) {
  398. rcu_print_task_stall(rnp);
  399. if (rnp->qsmask == 0)
  400. continue;
  401. for (cpu = 0; cpu <= rnp->grphi - rnp->grplo; cpu++)
  402. if (rnp->qsmask & (1UL << cpu))
  403. printk(" %d", rnp->grplo + cpu);
  404. }
  405. printk(" (detected by %d, t=%ld jiffies)\n",
  406. smp_processor_id(), (long)(jiffies - rsp->gp_start));
  407. trigger_all_cpu_backtrace();
  408. force_quiescent_state(rsp, 0); /* Kick them all. */
  409. }
  410. static void print_cpu_stall(struct rcu_state *rsp)
  411. {
  412. unsigned long flags;
  413. struct rcu_node *rnp = rcu_get_root(rsp);
  414. printk(KERN_ERR "INFO: RCU detected CPU %d stall (t=%lu jiffies)\n",
  415. smp_processor_id(), jiffies - rsp->gp_start);
  416. trigger_all_cpu_backtrace();
  417. spin_lock_irqsave(&rnp->lock, flags);
  418. if ((long)(jiffies - rsp->jiffies_stall) >= 0)
  419. rsp->jiffies_stall =
  420. jiffies + RCU_SECONDS_TILL_STALL_RECHECK;
  421. spin_unlock_irqrestore(&rnp->lock, flags);
  422. set_need_resched(); /* kick ourselves to get things going. */
  423. }
  424. static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
  425. {
  426. long delta;
  427. struct rcu_node *rnp;
  428. delta = jiffies - rsp->jiffies_stall;
  429. rnp = rdp->mynode;
  430. if ((rnp->qsmask & rdp->grpmask) && delta >= 0) {
  431. /* We haven't checked in, so go dump stack. */
  432. print_cpu_stall(rsp);
  433. } else if (rcu_gp_in_progress(rsp) && delta >= RCU_STALL_RAT_DELAY) {
  434. /* They had two time units to dump stack, so complain. */
  435. print_other_cpu_stall(rsp);
  436. }
  437. }
  438. #else /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
  439. static void record_gp_stall_check_time(struct rcu_state *rsp)
  440. {
  441. }
  442. static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
  443. {
  444. }
  445. #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
  446. /*
  447. * Update CPU-local rcu_data state to record the newly noticed grace period.
  448. * This is used both when we started the grace period and when we notice
  449. * that someone else started the grace period. The caller must hold the
  450. * ->lock of the leaf rcu_node structure corresponding to the current CPU,
  451. * and must have irqs disabled.
  452. */
  453. static void __note_new_gpnum(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
  454. {
  455. if (rdp->gpnum != rnp->gpnum) {
  456. rdp->qs_pending = 1;
  457. rdp->passed_quiesc = 0;
  458. rdp->gpnum = rnp->gpnum;
  459. }
  460. }
  461. static void note_new_gpnum(struct rcu_state *rsp, struct rcu_data *rdp)
  462. {
  463. unsigned long flags;
  464. struct rcu_node *rnp;
  465. local_irq_save(flags);
  466. rnp = rdp->mynode;
  467. if (rdp->gpnum == ACCESS_ONCE(rnp->gpnum) || /* outside lock. */
  468. !spin_trylock(&rnp->lock)) { /* irqs already off, retry later. */
  469. local_irq_restore(flags);
  470. return;
  471. }
  472. __note_new_gpnum(rsp, rnp, rdp);
  473. spin_unlock_irqrestore(&rnp->lock, flags);
  474. }
  475. /*
  476. * Did someone else start a new RCU grace period start since we last
  477. * checked? Update local state appropriately if so. Must be called
  478. * on the CPU corresponding to rdp.
  479. */
  480. static int
  481. check_for_new_grace_period(struct rcu_state *rsp, struct rcu_data *rdp)
  482. {
  483. unsigned long flags;
  484. int ret = 0;
  485. local_irq_save(flags);
  486. if (rdp->gpnum != rsp->gpnum) {
  487. note_new_gpnum(rsp, rdp);
  488. ret = 1;
  489. }
  490. local_irq_restore(flags);
  491. return ret;
  492. }
  493. /*
  494. * Advance this CPU's callbacks, but only if the current grace period
  495. * has ended. This may be called only from the CPU to whom the rdp
  496. * belongs. In addition, the corresponding leaf rcu_node structure's
  497. * ->lock must be held by the caller, with irqs disabled.
  498. */
  499. static void
  500. __rcu_process_gp_end(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
  501. {
  502. /* Did another grace period end? */
  503. if (rdp->completed != rnp->completed) {
  504. /* Advance callbacks. No harm if list empty. */
  505. rdp->nxttail[RCU_DONE_TAIL] = rdp->nxttail[RCU_WAIT_TAIL];
  506. rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_READY_TAIL];
  507. rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  508. /* Remember that we saw this grace-period completion. */
  509. rdp->completed = rnp->completed;
  510. }
  511. }
  512. /*
  513. * Advance this CPU's callbacks, but only if the current grace period
  514. * has ended. This may be called only from the CPU to whom the rdp
  515. * belongs.
  516. */
  517. static void
  518. rcu_process_gp_end(struct rcu_state *rsp, struct rcu_data *rdp)
  519. {
  520. unsigned long flags;
  521. struct rcu_node *rnp;
  522. local_irq_save(flags);
  523. rnp = rdp->mynode;
  524. if (rdp->completed == ACCESS_ONCE(rnp->completed) || /* outside lock. */
  525. !spin_trylock(&rnp->lock)) { /* irqs already off, retry later. */
  526. local_irq_restore(flags);
  527. return;
  528. }
  529. __rcu_process_gp_end(rsp, rnp, rdp);
  530. spin_unlock_irqrestore(&rnp->lock, flags);
  531. }
  532. /*
  533. * Do per-CPU grace-period initialization for running CPU. The caller
  534. * must hold the lock of the leaf rcu_node structure corresponding to
  535. * this CPU.
  536. */
  537. static void
  538. rcu_start_gp_per_cpu(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
  539. {
  540. /* Prior grace period ended, so advance callbacks for current CPU. */
  541. __rcu_process_gp_end(rsp, rnp, rdp);
  542. /*
  543. * Because this CPU just now started the new grace period, we know
  544. * that all of its callbacks will be covered by this upcoming grace
  545. * period, even the ones that were registered arbitrarily recently.
  546. * Therefore, advance all outstanding callbacks to RCU_WAIT_TAIL.
  547. *
  548. * Other CPUs cannot be sure exactly when the grace period started.
  549. * Therefore, their recently registered callbacks must pass through
  550. * an additional RCU_NEXT_READY stage, so that they will be handled
  551. * by the next RCU grace period.
  552. */
  553. rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  554. rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  555. /* Set state so that this CPU will detect the next quiescent state. */
  556. __note_new_gpnum(rsp, rnp, rdp);
  557. }
  558. /*
  559. * Start a new RCU grace period if warranted, re-initializing the hierarchy
  560. * in preparation for detecting the next grace period. The caller must hold
  561. * the root node's ->lock, which is released before return. Hard irqs must
  562. * be disabled.
  563. */
  564. static void
  565. rcu_start_gp(struct rcu_state *rsp, unsigned long flags)
  566. __releases(rcu_get_root(rsp)->lock)
  567. {
  568. struct rcu_data *rdp = rsp->rda[smp_processor_id()];
  569. struct rcu_node *rnp = rcu_get_root(rsp);
  570. if (!cpu_needs_another_gp(rsp, rdp)) {
  571. if (rnp->completed == rsp->completed) {
  572. spin_unlock_irqrestore(&rnp->lock, flags);
  573. return;
  574. }
  575. spin_unlock(&rnp->lock); /* irqs remain disabled. */
  576. /*
  577. * Propagate new ->completed value to rcu_node structures
  578. * so that other CPUs don't have to wait until the start
  579. * of the next grace period to process their callbacks.
  580. */
  581. rcu_for_each_node_breadth_first(rsp, rnp) {
  582. spin_lock(&rnp->lock); /* irqs already disabled. */
  583. rnp->completed = rsp->completed;
  584. spin_unlock(&rnp->lock); /* irqs remain disabled. */
  585. }
  586. local_irq_restore(flags);
  587. return;
  588. }
  589. /* Advance to a new grace period and initialize state. */
  590. rsp->gpnum++;
  591. WARN_ON_ONCE(rsp->signaled == RCU_GP_INIT);
  592. rsp->signaled = RCU_GP_INIT; /* Hold off force_quiescent_state. */
  593. rsp->jiffies_force_qs = jiffies + RCU_JIFFIES_TILL_FORCE_QS;
  594. record_gp_stall_check_time(rsp);
  595. /* Special-case the common single-level case. */
  596. if (NUM_RCU_NODES == 1) {
  597. rcu_preempt_check_blocked_tasks(rnp);
  598. rnp->qsmask = rnp->qsmaskinit;
  599. rnp->gpnum = rsp->gpnum;
  600. rnp->completed = rsp->completed;
  601. rsp->signaled = RCU_SIGNAL_INIT; /* force_quiescent_state OK. */
  602. rcu_start_gp_per_cpu(rsp, rnp, rdp);
  603. spin_unlock_irqrestore(&rnp->lock, flags);
  604. return;
  605. }
  606. spin_unlock(&rnp->lock); /* leave irqs disabled. */
  607. /* Exclude any concurrent CPU-hotplug operations. */
  608. spin_lock(&rsp->onofflock); /* irqs already disabled. */
  609. /*
  610. * Set the quiescent-state-needed bits in all the rcu_node
  611. * structures for all currently online CPUs in breadth-first
  612. * order, starting from the root rcu_node structure. This
  613. * operation relies on the layout of the hierarchy within the
  614. * rsp->node[] array. Note that other CPUs will access only
  615. * the leaves of the hierarchy, which still indicate that no
  616. * grace period is in progress, at least until the corresponding
  617. * leaf node has been initialized. In addition, we have excluded
  618. * CPU-hotplug operations.
  619. *
  620. * Note that the grace period cannot complete until we finish
  621. * the initialization process, as there will be at least one
  622. * qsmask bit set in the root node until that time, namely the
  623. * one corresponding to this CPU, due to the fact that we have
  624. * irqs disabled.
  625. */
  626. rcu_for_each_node_breadth_first(rsp, rnp) {
  627. spin_lock(&rnp->lock); /* irqs already disabled. */
  628. rcu_preempt_check_blocked_tasks(rnp);
  629. rnp->qsmask = rnp->qsmaskinit;
  630. rnp->gpnum = rsp->gpnum;
  631. rnp->completed = rsp->completed;
  632. if (rnp == rdp->mynode)
  633. rcu_start_gp_per_cpu(rsp, rnp, rdp);
  634. spin_unlock(&rnp->lock); /* irqs remain disabled. */
  635. }
  636. rnp = rcu_get_root(rsp);
  637. spin_lock(&rnp->lock); /* irqs already disabled. */
  638. rsp->signaled = RCU_SIGNAL_INIT; /* force_quiescent_state now OK. */
  639. spin_unlock(&rnp->lock); /* irqs remain disabled. */
  640. spin_unlock_irqrestore(&rsp->onofflock, flags);
  641. }
  642. /*
  643. * Report a full set of quiescent states to the specified rcu_state
  644. * data structure. This involves cleaning up after the prior grace
  645. * period and letting rcu_start_gp() start up the next grace period
  646. * if one is needed. Note that the caller must hold rnp->lock, as
  647. * required by rcu_start_gp(), which will release it.
  648. */
  649. static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
  650. __releases(rcu_get_root(rsp)->lock)
  651. {
  652. WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
  653. rsp->completed = rsp->gpnum;
  654. rsp->signaled = RCU_GP_IDLE;
  655. rcu_start_gp(rsp, flags); /* releases root node's rnp->lock. */
  656. }
  657. /*
  658. * Similar to rcu_report_qs_rdp(), for which it is a helper function.
  659. * Allows quiescent states for a group of CPUs to be reported at one go
  660. * to the specified rcu_node structure, though all the CPUs in the group
  661. * must be represented by the same rcu_node structure (which need not be
  662. * a leaf rcu_node structure, though it often will be). That structure's
  663. * lock must be held upon entry, and it is released before return.
  664. */
  665. static void
  666. rcu_report_qs_rnp(unsigned long mask, struct rcu_state *rsp,
  667. struct rcu_node *rnp, unsigned long flags)
  668. __releases(rnp->lock)
  669. {
  670. struct rcu_node *rnp_c;
  671. /* Walk up the rcu_node hierarchy. */
  672. for (;;) {
  673. if (!(rnp->qsmask & mask)) {
  674. /* Our bit has already been cleared, so done. */
  675. spin_unlock_irqrestore(&rnp->lock, flags);
  676. return;
  677. }
  678. rnp->qsmask &= ~mask;
  679. if (rnp->qsmask != 0 || rcu_preempted_readers(rnp)) {
  680. /* Other bits still set at this level, so done. */
  681. spin_unlock_irqrestore(&rnp->lock, flags);
  682. return;
  683. }
  684. mask = rnp->grpmask;
  685. if (rnp->parent == NULL) {
  686. /* No more levels. Exit loop holding root lock. */
  687. break;
  688. }
  689. spin_unlock_irqrestore(&rnp->lock, flags);
  690. rnp_c = rnp;
  691. rnp = rnp->parent;
  692. spin_lock_irqsave(&rnp->lock, flags);
  693. WARN_ON_ONCE(rnp_c->qsmask);
  694. }
  695. /*
  696. * Get here if we are the last CPU to pass through a quiescent
  697. * state for this grace period. Invoke rcu_report_qs_rsp()
  698. * to clean up and start the next grace period if one is needed.
  699. */
  700. rcu_report_qs_rsp(rsp, flags); /* releases rnp->lock. */
  701. }
  702. /*
  703. * Record a quiescent state for the specified CPU to that CPU's rcu_data
  704. * structure. This must be either called from the specified CPU, or
  705. * called when the specified CPU is known to be offline (and when it is
  706. * also known that no other CPU is concurrently trying to help the offline
  707. * CPU). The lastcomp argument is used to make sure we are still in the
  708. * grace period of interest. We don't want to end the current grace period
  709. * based on quiescent states detected in an earlier grace period!
  710. */
  711. static void
  712. rcu_report_qs_rdp(int cpu, struct rcu_state *rsp, struct rcu_data *rdp, long lastcomp)
  713. {
  714. unsigned long flags;
  715. unsigned long mask;
  716. struct rcu_node *rnp;
  717. rnp = rdp->mynode;
  718. spin_lock_irqsave(&rnp->lock, flags);
  719. if (lastcomp != rnp->completed) {
  720. /*
  721. * Someone beat us to it for this grace period, so leave.
  722. * The race with GP start is resolved by the fact that we
  723. * hold the leaf rcu_node lock, so that the per-CPU bits
  724. * cannot yet be initialized -- so we would simply find our
  725. * CPU's bit already cleared in rcu_report_qs_rnp() if this
  726. * race occurred.
  727. */
  728. rdp->passed_quiesc = 0; /* try again later! */
  729. spin_unlock_irqrestore(&rnp->lock, flags);
  730. return;
  731. }
  732. mask = rdp->grpmask;
  733. if ((rnp->qsmask & mask) == 0) {
  734. spin_unlock_irqrestore(&rnp->lock, flags);
  735. } else {
  736. rdp->qs_pending = 0;
  737. /*
  738. * This GP can't end until cpu checks in, so all of our
  739. * callbacks can be processed during the next GP.
  740. */
  741. rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
  742. rcu_report_qs_rnp(mask, rsp, rnp, flags); /* rlses rnp->lock */
  743. }
  744. }
  745. /*
  746. * Check to see if there is a new grace period of which this CPU
  747. * is not yet aware, and if so, set up local rcu_data state for it.
  748. * Otherwise, see if this CPU has just passed through its first
  749. * quiescent state for this grace period, and record that fact if so.
  750. */
  751. static void
  752. rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
  753. {
  754. /* If there is now a new grace period, record and return. */
  755. if (check_for_new_grace_period(rsp, rdp))
  756. return;
  757. /*
  758. * Does this CPU still need to do its part for current grace period?
  759. * If no, return and let the other CPUs do their part as well.
  760. */
  761. if (!rdp->qs_pending)
  762. return;
  763. /*
  764. * Was there a quiescent state since the beginning of the grace
  765. * period? If no, then exit and wait for the next call.
  766. */
  767. if (!rdp->passed_quiesc)
  768. return;
  769. /*
  770. * Tell RCU we are done (but rcu_report_qs_rdp() will be the
  771. * judge of that).
  772. */
  773. rcu_report_qs_rdp(rdp->cpu, rsp, rdp, rdp->passed_quiesc_completed);
  774. }
  775. #ifdef CONFIG_HOTPLUG_CPU
  776. /*
  777. * Move a dying CPU's RCU callbacks to the ->orphan_cbs_list for the
  778. * specified flavor of RCU. The callbacks will be adopted by the next
  779. * _rcu_barrier() invocation or by the CPU_DEAD notifier, whichever
  780. * comes first. Because this is invoked from the CPU_DYING notifier,
  781. * irqs are already disabled.
  782. */
  783. static void rcu_send_cbs_to_orphanage(struct rcu_state *rsp)
  784. {
  785. int i;
  786. struct rcu_data *rdp = rsp->rda[smp_processor_id()];
  787. if (rdp->nxtlist == NULL)
  788. return; /* irqs disabled, so comparison is stable. */
  789. spin_lock(&rsp->onofflock); /* irqs already disabled. */
  790. *rsp->orphan_cbs_tail = rdp->nxtlist;
  791. rsp->orphan_cbs_tail = rdp->nxttail[RCU_NEXT_TAIL];
  792. rdp->nxtlist = NULL;
  793. for (i = 0; i < RCU_NEXT_SIZE; i++)
  794. rdp->nxttail[i] = &rdp->nxtlist;
  795. rsp->orphan_qlen += rdp->qlen;
  796. rdp->qlen = 0;
  797. spin_unlock(&rsp->onofflock); /* irqs remain disabled. */
  798. }
  799. /*
  800. * Adopt previously orphaned RCU callbacks.
  801. */
  802. static void rcu_adopt_orphan_cbs(struct rcu_state *rsp)
  803. {
  804. unsigned long flags;
  805. struct rcu_data *rdp;
  806. spin_lock_irqsave(&rsp->onofflock, flags);
  807. rdp = rsp->rda[smp_processor_id()];
  808. if (rsp->orphan_cbs_list == NULL) {
  809. spin_unlock_irqrestore(&rsp->onofflock, flags);
  810. return;
  811. }
  812. *rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_cbs_list;
  813. rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_cbs_tail;
  814. rdp->qlen += rsp->orphan_qlen;
  815. rsp->orphan_cbs_list = NULL;
  816. rsp->orphan_cbs_tail = &rsp->orphan_cbs_list;
  817. rsp->orphan_qlen = 0;
  818. spin_unlock_irqrestore(&rsp->onofflock, flags);
  819. }
  820. /*
  821. * Remove the outgoing CPU from the bitmasks in the rcu_node hierarchy
  822. * and move all callbacks from the outgoing CPU to the current one.
  823. */
  824. static void __rcu_offline_cpu(int cpu, struct rcu_state *rsp)
  825. {
  826. unsigned long flags;
  827. unsigned long mask;
  828. int need_quiet = 0;
  829. struct rcu_data *rdp = rsp->rda[cpu];
  830. struct rcu_node *rnp;
  831. /* Exclude any attempts to start a new grace period. */
  832. spin_lock_irqsave(&rsp->onofflock, flags);
  833. /* Remove the outgoing CPU from the masks in the rcu_node hierarchy. */
  834. rnp = rdp->mynode; /* this is the outgoing CPU's rnp. */
  835. mask = rdp->grpmask; /* rnp->grplo is constant. */
  836. do {
  837. spin_lock(&rnp->lock); /* irqs already disabled. */
  838. rnp->qsmaskinit &= ~mask;
  839. if (rnp->qsmaskinit != 0) {
  840. if (rnp != rdp->mynode)
  841. spin_unlock(&rnp->lock); /* irqs remain disabled. */
  842. break;
  843. }
  844. if (rnp == rdp->mynode)
  845. need_quiet = rcu_preempt_offline_tasks(rsp, rnp, rdp);
  846. else
  847. spin_unlock(&rnp->lock); /* irqs remain disabled. */
  848. mask = rnp->grpmask;
  849. rnp = rnp->parent;
  850. } while (rnp != NULL);
  851. /*
  852. * We still hold the leaf rcu_node structure lock here, and
  853. * irqs are still disabled. The reason for this subterfuge is
  854. * because invoking rcu_report_unblock_qs_rnp() with ->onofflock
  855. * held leads to deadlock.
  856. */
  857. spin_unlock(&rsp->onofflock); /* irqs remain disabled. */
  858. rnp = rdp->mynode;
  859. if (need_quiet)
  860. rcu_report_unblock_qs_rnp(rnp, flags);
  861. else
  862. spin_unlock_irqrestore(&rnp->lock, flags);
  863. rcu_adopt_orphan_cbs(rsp);
  864. }
  865. /*
  866. * Remove the specified CPU from the RCU hierarchy and move any pending
  867. * callbacks that it might have to the current CPU. This code assumes
  868. * that at least one CPU in the system will remain running at all times.
  869. * Any attempt to offline -all- CPUs is likely to strand RCU callbacks.
  870. */
  871. static void rcu_offline_cpu(int cpu)
  872. {
  873. __rcu_offline_cpu(cpu, &rcu_sched_state);
  874. __rcu_offline_cpu(cpu, &rcu_bh_state);
  875. rcu_preempt_offline_cpu(cpu);
  876. }
  877. #else /* #ifdef CONFIG_HOTPLUG_CPU */
  878. static void rcu_send_cbs_to_orphanage(struct rcu_state *rsp)
  879. {
  880. }
  881. static void rcu_adopt_orphan_cbs(struct rcu_state *rsp)
  882. {
  883. }
  884. static void rcu_offline_cpu(int cpu)
  885. {
  886. }
  887. #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  888. /*
  889. * Invoke any RCU callbacks that have made it to the end of their grace
  890. * period. Thottle as specified by rdp->blimit.
  891. */
  892. static void rcu_do_batch(struct rcu_state *rsp, struct rcu_data *rdp)
  893. {
  894. unsigned long flags;
  895. struct rcu_head *next, *list, **tail;
  896. int count;
  897. /* If no callbacks are ready, just return.*/
  898. if (!cpu_has_callbacks_ready_to_invoke(rdp))
  899. return;
  900. /*
  901. * Extract the list of ready callbacks, disabling to prevent
  902. * races with call_rcu() from interrupt handlers.
  903. */
  904. local_irq_save(flags);
  905. list = rdp->nxtlist;
  906. rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL];
  907. *rdp->nxttail[RCU_DONE_TAIL] = NULL;
  908. tail = rdp->nxttail[RCU_DONE_TAIL];
  909. for (count = RCU_NEXT_SIZE - 1; count >= 0; count--)
  910. if (rdp->nxttail[count] == rdp->nxttail[RCU_DONE_TAIL])
  911. rdp->nxttail[count] = &rdp->nxtlist;
  912. local_irq_restore(flags);
  913. /* Invoke callbacks. */
  914. count = 0;
  915. while (list) {
  916. next = list->next;
  917. prefetch(next);
  918. list->func(list);
  919. list = next;
  920. if (++count >= rdp->blimit)
  921. break;
  922. }
  923. local_irq_save(flags);
  924. /* Update count, and requeue any remaining callbacks. */
  925. rdp->qlen -= count;
  926. if (list != NULL) {
  927. *tail = rdp->nxtlist;
  928. rdp->nxtlist = list;
  929. for (count = 0; count < RCU_NEXT_SIZE; count++)
  930. if (&rdp->nxtlist == rdp->nxttail[count])
  931. rdp->nxttail[count] = tail;
  932. else
  933. break;
  934. }
  935. /* Reinstate batch limit if we have worked down the excess. */
  936. if (rdp->blimit == LONG_MAX && rdp->qlen <= qlowmark)
  937. rdp->blimit = blimit;
  938. /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
  939. if (rdp->qlen == 0 && rdp->qlen_last_fqs_check != 0) {
  940. rdp->qlen_last_fqs_check = 0;
  941. rdp->n_force_qs_snap = rsp->n_force_qs;
  942. } else if (rdp->qlen < rdp->qlen_last_fqs_check - qhimark)
  943. rdp->qlen_last_fqs_check = rdp->qlen;
  944. local_irq_restore(flags);
  945. /* Re-raise the RCU softirq if there are callbacks remaining. */
  946. if (cpu_has_callbacks_ready_to_invoke(rdp))
  947. raise_softirq(RCU_SOFTIRQ);
  948. }
  949. /*
  950. * Check to see if this CPU is in a non-context-switch quiescent state
  951. * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
  952. * Also schedule the RCU softirq handler.
  953. *
  954. * This function must be called with hardirqs disabled. It is normally
  955. * invoked from the scheduling-clock interrupt. If rcu_pending returns
  956. * false, there is no point in invoking rcu_check_callbacks().
  957. */
  958. void rcu_check_callbacks(int cpu, int user)
  959. {
  960. if (!rcu_pending(cpu))
  961. return; /* if nothing for RCU to do. */
  962. if (user ||
  963. (idle_cpu(cpu) && rcu_scheduler_active &&
  964. !in_softirq() && hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
  965. /*
  966. * Get here if this CPU took its interrupt from user
  967. * mode or from the idle loop, and if this is not a
  968. * nested interrupt. In this case, the CPU is in
  969. * a quiescent state, so note it.
  970. *
  971. * No memory barrier is required here because both
  972. * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
  973. * variables that other CPUs neither access nor modify,
  974. * at least not while the corresponding CPU is online.
  975. */
  976. rcu_sched_qs(cpu);
  977. rcu_bh_qs(cpu);
  978. } else if (!in_softirq()) {
  979. /*
  980. * Get here if this CPU did not take its interrupt from
  981. * softirq, in other words, if it is not interrupting
  982. * a rcu_bh read-side critical section. This is an _bh
  983. * critical section, so note it.
  984. */
  985. rcu_bh_qs(cpu);
  986. }
  987. rcu_preempt_check_callbacks(cpu);
  988. raise_softirq(RCU_SOFTIRQ);
  989. }
  990. #ifdef CONFIG_SMP
  991. /*
  992. * Scan the leaf rcu_node structures, processing dyntick state for any that
  993. * have not yet encountered a quiescent state, using the function specified.
  994. * Returns 1 if the current grace period ends while scanning (possibly
  995. * because we made it end).
  996. */
  997. static int rcu_process_dyntick(struct rcu_state *rsp, long lastcomp,
  998. int (*f)(struct rcu_data *))
  999. {
  1000. unsigned long bit;
  1001. int cpu;
  1002. unsigned long flags;
  1003. unsigned long mask;
  1004. struct rcu_node *rnp;
  1005. rcu_for_each_leaf_node(rsp, rnp) {
  1006. mask = 0;
  1007. spin_lock_irqsave(&rnp->lock, flags);
  1008. if (rnp->completed != lastcomp) {
  1009. spin_unlock_irqrestore(&rnp->lock, flags);
  1010. return 1;
  1011. }
  1012. if (rnp->qsmask == 0) {
  1013. spin_unlock_irqrestore(&rnp->lock, flags);
  1014. continue;
  1015. }
  1016. cpu = rnp->grplo;
  1017. bit = 1;
  1018. for (; cpu <= rnp->grphi; cpu++, bit <<= 1) {
  1019. if ((rnp->qsmask & bit) != 0 && f(rsp->rda[cpu]))
  1020. mask |= bit;
  1021. }
  1022. if (mask != 0 && rnp->completed == lastcomp) {
  1023. /* rcu_report_qs_rnp() releases rnp->lock. */
  1024. rcu_report_qs_rnp(mask, rsp, rnp, flags);
  1025. continue;
  1026. }
  1027. spin_unlock_irqrestore(&rnp->lock, flags);
  1028. }
  1029. return 0;
  1030. }
  1031. /*
  1032. * Force quiescent states on reluctant CPUs, and also detect which
  1033. * CPUs are in dyntick-idle mode.
  1034. */
  1035. static void force_quiescent_state(struct rcu_state *rsp, int relaxed)
  1036. {
  1037. unsigned long flags;
  1038. long lastcomp;
  1039. struct rcu_node *rnp = rcu_get_root(rsp);
  1040. u8 signaled;
  1041. u8 forcenow;
  1042. if (!rcu_gp_in_progress(rsp))
  1043. return; /* No grace period in progress, nothing to force. */
  1044. if (!spin_trylock_irqsave(&rsp->fqslock, flags)) {
  1045. rsp->n_force_qs_lh++; /* Inexact, can lose counts. Tough! */
  1046. return; /* Someone else is already on the job. */
  1047. }
  1048. if (relaxed &&
  1049. (long)(rsp->jiffies_force_qs - jiffies) >= 0)
  1050. goto unlock_ret; /* no emergency and done recently. */
  1051. rsp->n_force_qs++;
  1052. spin_lock(&rnp->lock);
  1053. lastcomp = rsp->gpnum - 1;
  1054. signaled = rsp->signaled;
  1055. rsp->jiffies_force_qs = jiffies + RCU_JIFFIES_TILL_FORCE_QS;
  1056. if(!rcu_gp_in_progress(rsp)) {
  1057. rsp->n_force_qs_ngp++;
  1058. spin_unlock(&rnp->lock);
  1059. goto unlock_ret; /* no GP in progress, time updated. */
  1060. }
  1061. spin_unlock(&rnp->lock);
  1062. switch (signaled) {
  1063. case RCU_GP_IDLE:
  1064. case RCU_GP_INIT:
  1065. break; /* grace period idle or initializing, ignore. */
  1066. case RCU_SAVE_DYNTICK:
  1067. if (RCU_SIGNAL_INIT != RCU_SAVE_DYNTICK)
  1068. break; /* So gcc recognizes the dead code. */
  1069. /* Record dyntick-idle state. */
  1070. if (rcu_process_dyntick(rsp, lastcomp,
  1071. dyntick_save_progress_counter))
  1072. goto unlock_ret;
  1073. /* fall into next case. */
  1074. case RCU_SAVE_COMPLETED:
  1075. /* Update state, record completion counter. */
  1076. forcenow = 0;
  1077. spin_lock(&rnp->lock);
  1078. if (lastcomp + 1 == rsp->gpnum &&
  1079. lastcomp == rsp->completed &&
  1080. rsp->signaled == signaled) {
  1081. rsp->signaled = RCU_FORCE_QS;
  1082. rsp->completed_fqs = lastcomp;
  1083. forcenow = signaled == RCU_SAVE_COMPLETED;
  1084. }
  1085. spin_unlock(&rnp->lock);
  1086. if (!forcenow)
  1087. break;
  1088. /* fall into next case. */
  1089. case RCU_FORCE_QS:
  1090. /* Check dyntick-idle state, send IPI to laggarts. */
  1091. if (rcu_process_dyntick(rsp, rsp->completed_fqs,
  1092. rcu_implicit_dynticks_qs))
  1093. goto unlock_ret;
  1094. /* Leave state in case more forcing is required. */
  1095. break;
  1096. }
  1097. unlock_ret:
  1098. spin_unlock_irqrestore(&rsp->fqslock, flags);
  1099. }
  1100. #else /* #ifdef CONFIG_SMP */
  1101. static void force_quiescent_state(struct rcu_state *rsp, int relaxed)
  1102. {
  1103. set_need_resched();
  1104. }
  1105. #endif /* #else #ifdef CONFIG_SMP */
  1106. /*
  1107. * This does the RCU processing work from softirq context for the
  1108. * specified rcu_state and rcu_data structures. This may be called
  1109. * only from the CPU to whom the rdp belongs.
  1110. */
  1111. static void
  1112. __rcu_process_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
  1113. {
  1114. unsigned long flags;
  1115. WARN_ON_ONCE(rdp->beenonline == 0);
  1116. /*
  1117. * If an RCU GP has gone long enough, go check for dyntick
  1118. * idle CPUs and, if needed, send resched IPIs.
  1119. */
  1120. if ((long)(ACCESS_ONCE(rsp->jiffies_force_qs) - jiffies) < 0)
  1121. force_quiescent_state(rsp, 1);
  1122. /*
  1123. * Advance callbacks in response to end of earlier grace
  1124. * period that some other CPU ended.
  1125. */
  1126. rcu_process_gp_end(rsp, rdp);
  1127. /* Update RCU state based on any recent quiescent states. */
  1128. rcu_check_quiescent_state(rsp, rdp);
  1129. /* Does this CPU require a not-yet-started grace period? */
  1130. if (cpu_needs_another_gp(rsp, rdp)) {
  1131. spin_lock_irqsave(&rcu_get_root(rsp)->lock, flags);
  1132. rcu_start_gp(rsp, flags); /* releases above lock */
  1133. }
  1134. /* If there are callbacks ready, invoke them. */
  1135. rcu_do_batch(rsp, rdp);
  1136. }
  1137. /*
  1138. * Do softirq processing for the current CPU.
  1139. */
  1140. static void rcu_process_callbacks(struct softirq_action *unused)
  1141. {
  1142. /*
  1143. * Memory references from any prior RCU read-side critical sections
  1144. * executed by the interrupted code must be seen before any RCU
  1145. * grace-period manipulations below.
  1146. */
  1147. smp_mb(); /* See above block comment. */
  1148. __rcu_process_callbacks(&rcu_sched_state,
  1149. &__get_cpu_var(rcu_sched_data));
  1150. __rcu_process_callbacks(&rcu_bh_state, &__get_cpu_var(rcu_bh_data));
  1151. rcu_preempt_process_callbacks();
  1152. /*
  1153. * Memory references from any later RCU read-side critical sections
  1154. * executed by the interrupted code must be seen after any RCU
  1155. * grace-period manipulations above.
  1156. */
  1157. smp_mb(); /* See above block comment. */
  1158. }
  1159. static void
  1160. __call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu),
  1161. struct rcu_state *rsp)
  1162. {
  1163. unsigned long flags;
  1164. struct rcu_data *rdp;
  1165. head->func = func;
  1166. head->next = NULL;
  1167. smp_mb(); /* Ensure RCU update seen before callback registry. */
  1168. /*
  1169. * Opportunistically note grace-period endings and beginnings.
  1170. * Note that we might see a beginning right after we see an
  1171. * end, but never vice versa, since this CPU has to pass through
  1172. * a quiescent state betweentimes.
  1173. */
  1174. local_irq_save(flags);
  1175. rdp = rsp->rda[smp_processor_id()];
  1176. rcu_process_gp_end(rsp, rdp);
  1177. check_for_new_grace_period(rsp, rdp);
  1178. /* Add the callback to our list. */
  1179. *rdp->nxttail[RCU_NEXT_TAIL] = head;
  1180. rdp->nxttail[RCU_NEXT_TAIL] = &head->next;
  1181. /* Start a new grace period if one not already started. */
  1182. if (!rcu_gp_in_progress(rsp)) {
  1183. unsigned long nestflag;
  1184. struct rcu_node *rnp_root = rcu_get_root(rsp);
  1185. spin_lock_irqsave(&rnp_root->lock, nestflag);
  1186. rcu_start_gp(rsp, nestflag); /* releases rnp_root->lock. */
  1187. }
  1188. /*
  1189. * Force the grace period if too many callbacks or too long waiting.
  1190. * Enforce hysteresis, and don't invoke force_quiescent_state()
  1191. * if some other CPU has recently done so. Also, don't bother
  1192. * invoking force_quiescent_state() if the newly enqueued callback
  1193. * is the only one waiting for a grace period to complete.
  1194. */
  1195. if (unlikely(++rdp->qlen > rdp->qlen_last_fqs_check + qhimark)) {
  1196. rdp->blimit = LONG_MAX;
  1197. if (rsp->n_force_qs == rdp->n_force_qs_snap &&
  1198. *rdp->nxttail[RCU_DONE_TAIL] != head)
  1199. force_quiescent_state(rsp, 0);
  1200. rdp->n_force_qs_snap = rsp->n_force_qs;
  1201. rdp->qlen_last_fqs_check = rdp->qlen;
  1202. } else if ((long)(ACCESS_ONCE(rsp->jiffies_force_qs) - jiffies) < 0)
  1203. force_quiescent_state(rsp, 1);
  1204. local_irq_restore(flags);
  1205. }
  1206. /*
  1207. * Queue an RCU-sched callback for invocation after a grace period.
  1208. */
  1209. void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  1210. {
  1211. __call_rcu(head, func, &rcu_sched_state);
  1212. }
  1213. EXPORT_SYMBOL_GPL(call_rcu_sched);
  1214. /*
  1215. * Queue an RCU for invocation after a quicker grace period.
  1216. */
  1217. void call_rcu_bh(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  1218. {
  1219. __call_rcu(head, func, &rcu_bh_state);
  1220. }
  1221. EXPORT_SYMBOL_GPL(call_rcu_bh);
  1222. /**
  1223. * synchronize_sched - wait until an rcu-sched grace period has elapsed.
  1224. *
  1225. * Control will return to the caller some time after a full rcu-sched
  1226. * grace period has elapsed, in other words after all currently executing
  1227. * rcu-sched read-side critical sections have completed. These read-side
  1228. * critical sections are delimited by rcu_read_lock_sched() and
  1229. * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
  1230. * local_irq_disable(), and so on may be used in place of
  1231. * rcu_read_lock_sched().
  1232. *
  1233. * This means that all preempt_disable code sequences, including NMI and
  1234. * hardware-interrupt handlers, in progress on entry will have completed
  1235. * before this primitive returns. However, this does not guarantee that
  1236. * softirq handlers will have completed, since in some kernels, these
  1237. * handlers can run in process context, and can block.
  1238. *
  1239. * This primitive provides the guarantees made by the (now removed)
  1240. * synchronize_kernel() API. In contrast, synchronize_rcu() only
  1241. * guarantees that rcu_read_lock() sections will have completed.
  1242. * In "classic RCU", these two guarantees happen to be one and
  1243. * the same, but can differ in realtime RCU implementations.
  1244. */
  1245. void synchronize_sched(void)
  1246. {
  1247. struct rcu_synchronize rcu;
  1248. if (rcu_blocking_is_gp())
  1249. return;
  1250. init_completion(&rcu.completion);
  1251. /* Will wake me after RCU finished. */
  1252. call_rcu_sched(&rcu.head, wakeme_after_rcu);
  1253. /* Wait for it. */
  1254. wait_for_completion(&rcu.completion);
  1255. }
  1256. EXPORT_SYMBOL_GPL(synchronize_sched);
  1257. /**
  1258. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  1259. *
  1260. * Control will return to the caller some time after a full rcu_bh grace
  1261. * period has elapsed, in other words after all currently executing rcu_bh
  1262. * read-side critical sections have completed. RCU read-side critical
  1263. * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
  1264. * and may be nested.
  1265. */
  1266. void synchronize_rcu_bh(void)
  1267. {
  1268. struct rcu_synchronize rcu;
  1269. if (rcu_blocking_is_gp())
  1270. return;
  1271. init_completion(&rcu.completion);
  1272. /* Will wake me after RCU finished. */
  1273. call_rcu_bh(&rcu.head, wakeme_after_rcu);
  1274. /* Wait for it. */
  1275. wait_for_completion(&rcu.completion);
  1276. }
  1277. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  1278. /*
  1279. * Check to see if there is any immediate RCU-related work to be done
  1280. * by the current CPU, for the specified type of RCU, returning 1 if so.
  1281. * The checks are in order of increasing expense: checks that can be
  1282. * carried out against CPU-local state are performed first. However,
  1283. * we must check for CPU stalls first, else we might not get a chance.
  1284. */
  1285. static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
  1286. {
  1287. struct rcu_node *rnp = rdp->mynode;
  1288. rdp->n_rcu_pending++;
  1289. /* Check for CPU stalls, if enabled. */
  1290. check_cpu_stall(rsp, rdp);
  1291. /* Is the RCU core waiting for a quiescent state from this CPU? */
  1292. if (rdp->qs_pending) {
  1293. rdp->n_rp_qs_pending++;
  1294. return 1;
  1295. }
  1296. /* Does this CPU have callbacks ready to invoke? */
  1297. if (cpu_has_callbacks_ready_to_invoke(rdp)) {
  1298. rdp->n_rp_cb_ready++;
  1299. return 1;
  1300. }
  1301. /* Has RCU gone idle with this CPU needing another grace period? */
  1302. if (cpu_needs_another_gp(rsp, rdp)) {
  1303. rdp->n_rp_cpu_needs_gp++;
  1304. return 1;
  1305. }
  1306. /* Has another RCU grace period completed? */
  1307. if (ACCESS_ONCE(rnp->completed) != rdp->completed) { /* outside lock */
  1308. rdp->n_rp_gp_completed++;
  1309. return 1;
  1310. }
  1311. /* Has a new RCU grace period started? */
  1312. if (ACCESS_ONCE(rnp->gpnum) != rdp->gpnum) { /* outside lock */
  1313. rdp->n_rp_gp_started++;
  1314. return 1;
  1315. }
  1316. /* Has an RCU GP gone long enough to send resched IPIs &c? */
  1317. if (rcu_gp_in_progress(rsp) &&
  1318. ((long)(ACCESS_ONCE(rsp->jiffies_force_qs) - jiffies) < 0)) {
  1319. rdp->n_rp_need_fqs++;
  1320. return 1;
  1321. }
  1322. /* nothing to do */
  1323. rdp->n_rp_need_nothing++;
  1324. return 0;
  1325. }
  1326. /*
  1327. * Check to see if there is any immediate RCU-related work to be done
  1328. * by the current CPU, returning 1 if so. This function is part of the
  1329. * RCU implementation; it is -not- an exported member of the RCU API.
  1330. */
  1331. static int rcu_pending(int cpu)
  1332. {
  1333. return __rcu_pending(&rcu_sched_state, &per_cpu(rcu_sched_data, cpu)) ||
  1334. __rcu_pending(&rcu_bh_state, &per_cpu(rcu_bh_data, cpu)) ||
  1335. rcu_preempt_pending(cpu);
  1336. }
  1337. /*
  1338. * Check to see if any future RCU-related work will need to be done
  1339. * by the current CPU, even if none need be done immediately, returning
  1340. * 1 if so. This function is part of the RCU implementation; it is -not-
  1341. * an exported member of the RCU API.
  1342. */
  1343. int rcu_needs_cpu(int cpu)
  1344. {
  1345. /* RCU callbacks either ready or pending? */
  1346. return per_cpu(rcu_sched_data, cpu).nxtlist ||
  1347. per_cpu(rcu_bh_data, cpu).nxtlist ||
  1348. rcu_preempt_needs_cpu(cpu);
  1349. }
  1350. /*
  1351. * This function is invoked towards the end of the scheduler's initialization
  1352. * process. Before this is called, the idle task might contain
  1353. * RCU read-side critical sections (during which time, this idle
  1354. * task is booting the system). After this function is called, the
  1355. * idle tasks are prohibited from containing RCU read-side critical
  1356. * sections.
  1357. */
  1358. void rcu_scheduler_starting(void)
  1359. {
  1360. WARN_ON(num_online_cpus() != 1);
  1361. WARN_ON(nr_context_switches() > 0);
  1362. rcu_scheduler_active = 1;
  1363. }
  1364. static DEFINE_PER_CPU(struct rcu_head, rcu_barrier_head) = {NULL};
  1365. static atomic_t rcu_barrier_cpu_count;
  1366. static DEFINE_MUTEX(rcu_barrier_mutex);
  1367. static struct completion rcu_barrier_completion;
  1368. static void rcu_barrier_callback(struct rcu_head *notused)
  1369. {
  1370. if (atomic_dec_and_test(&rcu_barrier_cpu_count))
  1371. complete(&rcu_barrier_completion);
  1372. }
  1373. /*
  1374. * Called with preemption disabled, and from cross-cpu IRQ context.
  1375. */
  1376. static void rcu_barrier_func(void *type)
  1377. {
  1378. int cpu = smp_processor_id();
  1379. struct rcu_head *head = &per_cpu(rcu_barrier_head, cpu);
  1380. void (*call_rcu_func)(struct rcu_head *head,
  1381. void (*func)(struct rcu_head *head));
  1382. atomic_inc(&rcu_barrier_cpu_count);
  1383. call_rcu_func = type;
  1384. call_rcu_func(head, rcu_barrier_callback);
  1385. }
  1386. /*
  1387. * Orchestrate the specified type of RCU barrier, waiting for all
  1388. * RCU callbacks of the specified type to complete.
  1389. */
  1390. static void _rcu_barrier(struct rcu_state *rsp,
  1391. void (*call_rcu_func)(struct rcu_head *head,
  1392. void (*func)(struct rcu_head *head)))
  1393. {
  1394. BUG_ON(in_interrupt());
  1395. /* Take mutex to serialize concurrent rcu_barrier() requests. */
  1396. mutex_lock(&rcu_barrier_mutex);
  1397. init_completion(&rcu_barrier_completion);
  1398. /*
  1399. * Initialize rcu_barrier_cpu_count to 1, then invoke
  1400. * rcu_barrier_func() on each CPU, so that each CPU also has
  1401. * incremented rcu_barrier_cpu_count. Only then is it safe to
  1402. * decrement rcu_barrier_cpu_count -- otherwise the first CPU
  1403. * might complete its grace period before all of the other CPUs
  1404. * did their increment, causing this function to return too
  1405. * early.
  1406. */
  1407. atomic_set(&rcu_barrier_cpu_count, 1);
  1408. preempt_disable(); /* stop CPU_DYING from filling orphan_cbs_list */
  1409. rcu_adopt_orphan_cbs(rsp);
  1410. on_each_cpu(rcu_barrier_func, (void *)call_rcu_func, 1);
  1411. preempt_enable(); /* CPU_DYING can again fill orphan_cbs_list */
  1412. if (atomic_dec_and_test(&rcu_barrier_cpu_count))
  1413. complete(&rcu_barrier_completion);
  1414. wait_for_completion(&rcu_barrier_completion);
  1415. mutex_unlock(&rcu_barrier_mutex);
  1416. }
  1417. /**
  1418. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  1419. */
  1420. void rcu_barrier_bh(void)
  1421. {
  1422. _rcu_barrier(&rcu_bh_state, call_rcu_bh);
  1423. }
  1424. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  1425. /**
  1426. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  1427. */
  1428. void rcu_barrier_sched(void)
  1429. {
  1430. _rcu_barrier(&rcu_sched_state, call_rcu_sched);
  1431. }
  1432. EXPORT_SYMBOL_GPL(rcu_barrier_sched);
  1433. /*
  1434. * Do boot-time initialization of a CPU's per-CPU RCU data.
  1435. */
  1436. static void __init
  1437. rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp)
  1438. {
  1439. unsigned long flags;
  1440. int i;
  1441. struct rcu_data *rdp = rsp->rda[cpu];
  1442. struct rcu_node *rnp = rcu_get_root(rsp);
  1443. /* Set up local state, ensuring consistent view of global state. */
  1444. spin_lock_irqsave(&rnp->lock, flags);
  1445. rdp->grpmask = 1UL << (cpu - rdp->mynode->grplo);
  1446. rdp->nxtlist = NULL;
  1447. for (i = 0; i < RCU_NEXT_SIZE; i++)
  1448. rdp->nxttail[i] = &rdp->nxtlist;
  1449. rdp->qlen = 0;
  1450. #ifdef CONFIG_NO_HZ
  1451. rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
  1452. #endif /* #ifdef CONFIG_NO_HZ */
  1453. rdp->cpu = cpu;
  1454. spin_unlock_irqrestore(&rnp->lock, flags);
  1455. }
  1456. /*
  1457. * Initialize a CPU's per-CPU RCU data. Note that only one online or
  1458. * offline event can be happening at a given time. Note also that we
  1459. * can accept some slop in the rsp->completed access due to the fact
  1460. * that this CPU cannot possibly have any RCU callbacks in flight yet.
  1461. */
  1462. static void __cpuinit
  1463. rcu_init_percpu_data(int cpu, struct rcu_state *rsp, int preemptable)
  1464. {
  1465. unsigned long flags;
  1466. unsigned long mask;
  1467. struct rcu_data *rdp = rsp->rda[cpu];
  1468. struct rcu_node *rnp = rcu_get_root(rsp);
  1469. /* Set up local state, ensuring consistent view of global state. */
  1470. spin_lock_irqsave(&rnp->lock, flags);
  1471. rdp->passed_quiesc = 0; /* We could be racing with new GP, */
  1472. rdp->qs_pending = 1; /* so set up to respond to current GP. */
  1473. rdp->beenonline = 1; /* We have now been online. */
  1474. rdp->preemptable = preemptable;
  1475. rdp->qlen_last_fqs_check = 0;
  1476. rdp->n_force_qs_snap = rsp->n_force_qs;
  1477. rdp->blimit = blimit;
  1478. spin_unlock(&rnp->lock); /* irqs remain disabled. */
  1479. /*
  1480. * A new grace period might start here. If so, we won't be part
  1481. * of it, but that is OK, as we are currently in a quiescent state.
  1482. */
  1483. /* Exclude any attempts to start a new GP on large systems. */
  1484. spin_lock(&rsp->onofflock); /* irqs already disabled. */
  1485. /* Add CPU to rcu_node bitmasks. */
  1486. rnp = rdp->mynode;
  1487. mask = rdp->grpmask;
  1488. do {
  1489. /* Exclude any attempts to start a new GP on small systems. */
  1490. spin_lock(&rnp->lock); /* irqs already disabled. */
  1491. rnp->qsmaskinit |= mask;
  1492. mask = rnp->grpmask;
  1493. if (rnp == rdp->mynode) {
  1494. rdp->gpnum = rnp->completed; /* if GP in progress... */
  1495. rdp->completed = rnp->completed;
  1496. rdp->passed_quiesc_completed = rnp->completed - 1;
  1497. }
  1498. spin_unlock(&rnp->lock); /* irqs already disabled. */
  1499. rnp = rnp->parent;
  1500. } while (rnp != NULL && !(rnp->qsmaskinit & mask));
  1501. spin_unlock_irqrestore(&rsp->onofflock, flags);
  1502. }
  1503. static void __cpuinit rcu_online_cpu(int cpu)
  1504. {
  1505. rcu_init_percpu_data(cpu, &rcu_sched_state, 0);
  1506. rcu_init_percpu_data(cpu, &rcu_bh_state, 0);
  1507. rcu_preempt_init_percpu_data(cpu);
  1508. }
  1509. /*
  1510. * Handle CPU online/offline notification events.
  1511. */
  1512. static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
  1513. unsigned long action, void *hcpu)
  1514. {
  1515. long cpu = (long)hcpu;
  1516. switch (action) {
  1517. case CPU_UP_PREPARE:
  1518. case CPU_UP_PREPARE_FROZEN:
  1519. rcu_online_cpu(cpu);
  1520. break;
  1521. case CPU_DYING:
  1522. case CPU_DYING_FROZEN:
  1523. /*
  1524. * preempt_disable() in _rcu_barrier() prevents stop_machine(),
  1525. * so when "on_each_cpu(rcu_barrier_func, (void *)type, 1);"
  1526. * returns, all online cpus have queued rcu_barrier_func().
  1527. * The dying CPU clears its cpu_online_mask bit and
  1528. * moves all of its RCU callbacks to ->orphan_cbs_list
  1529. * in the context of stop_machine(), so subsequent calls
  1530. * to _rcu_barrier() will adopt these callbacks and only
  1531. * then queue rcu_barrier_func() on all remaining CPUs.
  1532. */
  1533. rcu_send_cbs_to_orphanage(&rcu_bh_state);
  1534. rcu_send_cbs_to_orphanage(&rcu_sched_state);
  1535. rcu_preempt_send_cbs_to_orphanage();
  1536. break;
  1537. case CPU_DEAD:
  1538. case CPU_DEAD_FROZEN:
  1539. case CPU_UP_CANCELED:
  1540. case CPU_UP_CANCELED_FROZEN:
  1541. rcu_offline_cpu(cpu);
  1542. break;
  1543. default:
  1544. break;
  1545. }
  1546. return NOTIFY_OK;
  1547. }
  1548. /*
  1549. * Compute the per-level fanout, either using the exact fanout specified
  1550. * or balancing the tree, depending on CONFIG_RCU_FANOUT_EXACT.
  1551. */
  1552. #ifdef CONFIG_RCU_FANOUT_EXACT
  1553. static void __init rcu_init_levelspread(struct rcu_state *rsp)
  1554. {
  1555. int i;
  1556. for (i = NUM_RCU_LVLS - 1; i >= 0; i--)
  1557. rsp->levelspread[i] = CONFIG_RCU_FANOUT;
  1558. }
  1559. #else /* #ifdef CONFIG_RCU_FANOUT_EXACT */
  1560. static void __init rcu_init_levelspread(struct rcu_state *rsp)
  1561. {
  1562. int ccur;
  1563. int cprv;
  1564. int i;
  1565. cprv = NR_CPUS;
  1566. for (i = NUM_RCU_LVLS - 1; i >= 0; i--) {
  1567. ccur = rsp->levelcnt[i];
  1568. rsp->levelspread[i] = (cprv + ccur - 1) / ccur;
  1569. cprv = ccur;
  1570. }
  1571. }
  1572. #endif /* #else #ifdef CONFIG_RCU_FANOUT_EXACT */
  1573. /*
  1574. * Helper function for rcu_init() that initializes one rcu_state structure.
  1575. */
  1576. static void __init rcu_init_one(struct rcu_state *rsp)
  1577. {
  1578. int cpustride = 1;
  1579. int i;
  1580. int j;
  1581. struct rcu_node *rnp;
  1582. /* Initialize the level-tracking arrays. */
  1583. for (i = 1; i < NUM_RCU_LVLS; i++)
  1584. rsp->level[i] = rsp->level[i - 1] + rsp->levelcnt[i - 1];
  1585. rcu_init_levelspread(rsp);
  1586. /* Initialize the elements themselves, starting from the leaves. */
  1587. for (i = NUM_RCU_LVLS - 1; i >= 0; i--) {
  1588. cpustride *= rsp->levelspread[i];
  1589. rnp = rsp->level[i];
  1590. for (j = 0; j < rsp->levelcnt[i]; j++, rnp++) {
  1591. spin_lock_init(&rnp->lock);
  1592. lockdep_set_class(&rnp->lock, &rcu_node_class[i]);
  1593. rnp->gpnum = 0;
  1594. rnp->qsmask = 0;
  1595. rnp->qsmaskinit = 0;
  1596. rnp->grplo = j * cpustride;
  1597. rnp->grphi = (j + 1) * cpustride - 1;
  1598. if (rnp->grphi >= NR_CPUS)
  1599. rnp->grphi = NR_CPUS - 1;
  1600. if (i == 0) {
  1601. rnp->grpnum = 0;
  1602. rnp->grpmask = 0;
  1603. rnp->parent = NULL;
  1604. } else {
  1605. rnp->grpnum = j % rsp->levelspread[i - 1];
  1606. rnp->grpmask = 1UL << rnp->grpnum;
  1607. rnp->parent = rsp->level[i - 1] +
  1608. j / rsp->levelspread[i - 1];
  1609. }
  1610. rnp->level = i;
  1611. INIT_LIST_HEAD(&rnp->blocked_tasks[0]);
  1612. INIT_LIST_HEAD(&rnp->blocked_tasks[1]);
  1613. }
  1614. }
  1615. }
  1616. /*
  1617. * Helper macro for __rcu_init() and __rcu_init_preempt(). To be used
  1618. * nowhere else! Assigns leaf node pointers into each CPU's rcu_data
  1619. * structure.
  1620. */
  1621. #define RCU_INIT_FLAVOR(rsp, rcu_data) \
  1622. do { \
  1623. int i; \
  1624. int j; \
  1625. struct rcu_node *rnp; \
  1626. \
  1627. rcu_init_one(rsp); \
  1628. rnp = (rsp)->level[NUM_RCU_LVLS - 1]; \
  1629. j = 0; \
  1630. for_each_possible_cpu(i) { \
  1631. if (i > rnp[j].grphi) \
  1632. j++; \
  1633. per_cpu(rcu_data, i).mynode = &rnp[j]; \
  1634. (rsp)->rda[i] = &per_cpu(rcu_data, i); \
  1635. rcu_boot_init_percpu_data(i, rsp); \
  1636. } \
  1637. } while (0)
  1638. void __init rcu_init(void)
  1639. {
  1640. int i;
  1641. rcu_bootup_announce();
  1642. #ifdef CONFIG_RCU_CPU_STALL_DETECTOR
  1643. printk(KERN_INFO "RCU-based detection of stalled CPUs is enabled.\n");
  1644. #endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
  1645. #if NUM_RCU_LVL_4 != 0
  1646. printk(KERN_INFO "Experimental four-level hierarchy is enabled.\n");
  1647. #endif /* #if NUM_RCU_LVL_4 != 0 */
  1648. RCU_INIT_FLAVOR(&rcu_sched_state, rcu_sched_data);
  1649. RCU_INIT_FLAVOR(&rcu_bh_state, rcu_bh_data);
  1650. __rcu_init_preempt();
  1651. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  1652. /*
  1653. * We don't need protection against CPU-hotplug here because
  1654. * this is called early in boot, before either interrupts
  1655. * or the scheduler are operational.
  1656. */
  1657. cpu_notifier(rcu_cpu_notify, 0);
  1658. for_each_online_cpu(i)
  1659. rcu_cpu_notify(NULL, CPU_UP_PREPARE, (void *)(long)i);
  1660. }
  1661. #include "rcutree_plugin.h"