rcutree.c 91 KB

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