rcutree.c 57 KB

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