rcutree.c 102 KB

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