rcutree.c 57 KB

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