rcutree.c 64 KB

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