rcutree.c 58 KB

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