rcutree.c 70 KB

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