rcutree.c 58 KB

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