rcutree.c 79 KB

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