rcutree.c 68 KB

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