rcutree.c 89 KB

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