rcutree.c 61 KB

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