tree.c 110 KB

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