rcutree.c 71 KB

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