rcutree.c 73 KB

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