rcutree.c 84 KB

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