rcutree.c 90 KB

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