rcutree.c 57 KB

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