rcutree.c 87 KB

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