rcutree.c 86 KB

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