rcutree.c 54 KB

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