rcutree.c 101 KB

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