rcutree.c 77 KB

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