rcutree.c 86 KB

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