tree.c 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210
  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_wait.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/sched.h>
  38. #include <linux/sched/debug.h>
  39. #include <linux/nmi.h>
  40. #include <linux/atomic.h>
  41. #include <linux/bitops.h>
  42. #include <linux/export.h>
  43. #include <linux/completion.h>
  44. #include <linux/moduleparam.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 <uapi/linux/sched/types.h>
  54. #include <linux/prefetch.h>
  55. #include <linux/delay.h>
  56. #include <linux/stop_machine.h>
  57. #include <linux/random.h>
  58. #include <linux/trace_events.h>
  59. #include <linux/suspend.h>
  60. #include <linux/ftrace.h>
  61. #include "tree.h"
  62. #include "rcu.h"
  63. #ifdef MODULE_PARAM_PREFIX
  64. #undef MODULE_PARAM_PREFIX
  65. #endif
  66. #define MODULE_PARAM_PREFIX "rcutree."
  67. /* Data structures. */
  68. /*
  69. * In order to export the rcu_state name to the tracing tools, it
  70. * needs to be added in the __tracepoint_string section.
  71. * This requires defining a separate variable tp_<sname>_varname
  72. * that points to the string being used, and this will allow
  73. * the tracing userspace tools to be able to decipher the string
  74. * address to the matching string.
  75. */
  76. #ifdef CONFIG_TRACING
  77. # define DEFINE_RCU_TPS(sname) \
  78. static char sname##_varname[] = #sname; \
  79. static const char *tp_##sname##_varname __used __tracepoint_string = sname##_varname;
  80. # define RCU_STATE_NAME(sname) sname##_varname
  81. #else
  82. # define DEFINE_RCU_TPS(sname)
  83. # define RCU_STATE_NAME(sname) __stringify(sname)
  84. #endif
  85. #define RCU_STATE_INITIALIZER(sname, sabbr, cr) \
  86. DEFINE_RCU_TPS(sname) \
  87. static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, sname##_data); \
  88. struct rcu_state sname##_state = { \
  89. .level = { &sname##_state.node[0] }, \
  90. .rda = &sname##_data, \
  91. .call = cr, \
  92. .gp_state = RCU_GP_IDLE, \
  93. .gpnum = 0UL - 300UL, \
  94. .completed = 0UL - 300UL, \
  95. .barrier_mutex = __MUTEX_INITIALIZER(sname##_state.barrier_mutex), \
  96. .name = RCU_STATE_NAME(sname), \
  97. .abbr = sabbr, \
  98. .exp_mutex = __MUTEX_INITIALIZER(sname##_state.exp_mutex), \
  99. .exp_wake_mutex = __MUTEX_INITIALIZER(sname##_state.exp_wake_mutex), \
  100. }
  101. RCU_STATE_INITIALIZER(rcu_sched, 's', call_rcu_sched);
  102. RCU_STATE_INITIALIZER(rcu_bh, 'b', call_rcu_bh);
  103. static struct rcu_state *const rcu_state_p;
  104. LIST_HEAD(rcu_struct_flavors);
  105. /* Dump rcu_node combining tree at boot to verify correct setup. */
  106. static bool dump_tree;
  107. module_param(dump_tree, bool, 0444);
  108. /* Control rcu_node-tree auto-balancing at boot time. */
  109. static bool rcu_fanout_exact;
  110. module_param(rcu_fanout_exact, bool, 0444);
  111. /* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */
  112. static int rcu_fanout_leaf = RCU_FANOUT_LEAF;
  113. module_param(rcu_fanout_leaf, int, 0444);
  114. int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
  115. /* Number of rcu_nodes at specified level. */
  116. int num_rcu_lvl[] = NUM_RCU_LVL_INIT;
  117. int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
  118. /* panic() on RCU Stall sysctl. */
  119. int sysctl_panic_on_rcu_stall __read_mostly;
  120. /*
  121. * The rcu_scheduler_active variable is initialized to the value
  122. * RCU_SCHEDULER_INACTIVE and transitions RCU_SCHEDULER_INIT just before the
  123. * first task is spawned. So when this variable is RCU_SCHEDULER_INACTIVE,
  124. * RCU can assume that there is but one task, allowing RCU to (for example)
  125. * optimize synchronize_rcu() to a simple barrier(). When this variable
  126. * is RCU_SCHEDULER_INIT, RCU must actually do all the hard work required
  127. * to detect real grace periods. This variable is also used to suppress
  128. * boot-time false positives from lockdep-RCU error checking. Finally, it
  129. * transitions from RCU_SCHEDULER_INIT to RCU_SCHEDULER_RUNNING after RCU
  130. * is fully initialized, including all of its kthreads having been spawned.
  131. */
  132. int rcu_scheduler_active __read_mostly;
  133. EXPORT_SYMBOL_GPL(rcu_scheduler_active);
  134. /*
  135. * The rcu_scheduler_fully_active variable transitions from zero to one
  136. * during the early_initcall() processing, which is after the scheduler
  137. * is capable of creating new tasks. So RCU processing (for example,
  138. * creating tasks for RCU priority boosting) must be delayed until after
  139. * rcu_scheduler_fully_active transitions from zero to one. We also
  140. * currently delay invocation of any RCU callbacks until after this point.
  141. *
  142. * It might later prove better for people registering RCU callbacks during
  143. * early boot to take responsibility for these callbacks, but one step at
  144. * a time.
  145. */
  146. static int rcu_scheduler_fully_active __read_mostly;
  147. static void rcu_init_new_rnp(struct rcu_node *rnp_leaf);
  148. static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf);
  149. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
  150. static void invoke_rcu_core(void);
  151. static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp);
  152. static void rcu_report_exp_rdp(struct rcu_state *rsp,
  153. struct rcu_data *rdp, bool wake);
  154. static void sync_sched_exp_online_cleanup(int cpu);
  155. /* rcuc/rcub kthread realtime priority */
  156. static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0;
  157. module_param(kthread_prio, int, 0644);
  158. /* Delay in jiffies for grace-period initialization delays, debug only. */
  159. static int gp_preinit_delay;
  160. module_param(gp_preinit_delay, int, 0444);
  161. static int gp_init_delay;
  162. module_param(gp_init_delay, int, 0444);
  163. static int gp_cleanup_delay;
  164. module_param(gp_cleanup_delay, int, 0444);
  165. /*
  166. * Number of grace periods between delays, normalized by the duration of
  167. * the delay. The longer the delay, the more the grace periods between
  168. * each delay. The reason for this normalization is that it means that,
  169. * for non-zero delays, the overall slowdown of grace periods is constant
  170. * regardless of the duration of the delay. This arrangement balances
  171. * the need for long delays to increase some race probabilities with the
  172. * need for fast grace periods to increase other race probabilities.
  173. */
  174. #define PER_RCU_NODE_PERIOD 3 /* Number of grace periods between delays. */
  175. /*
  176. * Track the rcutorture test sequence number and the update version
  177. * number within a given test. The rcutorture_testseq is incremented
  178. * on every rcutorture module load and unload, so has an odd value
  179. * when a test is running. The rcutorture_vernum is set to zero
  180. * when rcutorture starts and is incremented on each rcutorture update.
  181. * These variables enable correlating rcutorture output with the
  182. * RCU tracing information.
  183. */
  184. unsigned long rcutorture_testseq;
  185. unsigned long rcutorture_vernum;
  186. /*
  187. * Compute the mask of online CPUs for the specified rcu_node structure.
  188. * This will not be stable unless the rcu_node structure's ->lock is
  189. * held, but the bit corresponding to the current CPU will be stable
  190. * in most contexts.
  191. */
  192. unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp)
  193. {
  194. return READ_ONCE(rnp->qsmaskinitnext);
  195. }
  196. /*
  197. * Return true if an RCU grace period is in progress. The READ_ONCE()s
  198. * permit this function to be invoked without holding the root rcu_node
  199. * structure's ->lock, but of course results can be subject to change.
  200. */
  201. static int rcu_gp_in_progress(struct rcu_state *rsp)
  202. {
  203. return READ_ONCE(rsp->completed) != READ_ONCE(rsp->gpnum);
  204. }
  205. /*
  206. * Note a quiescent state. Because we do not need to know
  207. * how many quiescent states passed, just if there was at least
  208. * one since the start of the grace period, this just sets a flag.
  209. * The caller must have disabled preemption.
  210. */
  211. void rcu_sched_qs(void)
  212. {
  213. RCU_LOCKDEP_WARN(preemptible(), "rcu_sched_qs() invoked with preemption enabled!!!");
  214. if (!__this_cpu_read(rcu_sched_data.cpu_no_qs.s))
  215. return;
  216. trace_rcu_grace_period(TPS("rcu_sched"),
  217. __this_cpu_read(rcu_sched_data.gpnum),
  218. TPS("cpuqs"));
  219. __this_cpu_write(rcu_sched_data.cpu_no_qs.b.norm, false);
  220. if (!__this_cpu_read(rcu_sched_data.cpu_no_qs.b.exp))
  221. return;
  222. __this_cpu_write(rcu_sched_data.cpu_no_qs.b.exp, false);
  223. rcu_report_exp_rdp(&rcu_sched_state,
  224. this_cpu_ptr(&rcu_sched_data), true);
  225. }
  226. void rcu_bh_qs(void)
  227. {
  228. RCU_LOCKDEP_WARN(preemptible(), "rcu_bh_qs() invoked with preemption enabled!!!");
  229. if (__this_cpu_read(rcu_bh_data.cpu_no_qs.s)) {
  230. trace_rcu_grace_period(TPS("rcu_bh"),
  231. __this_cpu_read(rcu_bh_data.gpnum),
  232. TPS("cpuqs"));
  233. __this_cpu_write(rcu_bh_data.cpu_no_qs.b.norm, false);
  234. }
  235. }
  236. /*
  237. * Steal a bit from the bottom of ->dynticks for idle entry/exit
  238. * control. Initially this is for TLB flushing.
  239. */
  240. #define RCU_DYNTICK_CTRL_MASK 0x1
  241. #define RCU_DYNTICK_CTRL_CTR (RCU_DYNTICK_CTRL_MASK + 1)
  242. #ifndef rcu_eqs_special_exit
  243. #define rcu_eqs_special_exit() do { } while (0)
  244. #endif
  245. static DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
  246. .dynticks_nesting = 1,
  247. .dynticks_nmi_nesting = DYNTICK_IRQ_NONIDLE,
  248. .dynticks = ATOMIC_INIT(RCU_DYNTICK_CTRL_CTR),
  249. };
  250. /*
  251. * Record entry into an extended quiescent state. This is only to be
  252. * called when not already in an extended quiescent state.
  253. */
  254. static void rcu_dynticks_eqs_enter(void)
  255. {
  256. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  257. int seq;
  258. /*
  259. * CPUs seeing atomic_add_return() must see prior RCU read-side
  260. * critical sections, and we also must force ordering with the
  261. * next idle sojourn.
  262. */
  263. seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdtp->dynticks);
  264. /* Better be in an extended quiescent state! */
  265. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  266. (seq & RCU_DYNTICK_CTRL_CTR));
  267. /* Better not have special action (TLB flush) pending! */
  268. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  269. (seq & RCU_DYNTICK_CTRL_MASK));
  270. }
  271. /*
  272. * Record exit from an extended quiescent state. This is only to be
  273. * called from an extended quiescent state.
  274. */
  275. static void rcu_dynticks_eqs_exit(void)
  276. {
  277. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  278. int seq;
  279. /*
  280. * CPUs seeing atomic_add_return() must see prior idle sojourns,
  281. * and we also must force ordering with the next RCU read-side
  282. * critical section.
  283. */
  284. seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdtp->dynticks);
  285. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  286. !(seq & RCU_DYNTICK_CTRL_CTR));
  287. if (seq & RCU_DYNTICK_CTRL_MASK) {
  288. atomic_andnot(RCU_DYNTICK_CTRL_MASK, &rdtp->dynticks);
  289. smp_mb__after_atomic(); /* _exit after clearing mask. */
  290. /* Prefer duplicate flushes to losing a flush. */
  291. rcu_eqs_special_exit();
  292. }
  293. }
  294. /*
  295. * Reset the current CPU's ->dynticks counter to indicate that the
  296. * newly onlined CPU is no longer in an extended quiescent state.
  297. * This will either leave the counter unchanged, or increment it
  298. * to the next non-quiescent value.
  299. *
  300. * The non-atomic test/increment sequence works because the upper bits
  301. * of the ->dynticks counter are manipulated only by the corresponding CPU,
  302. * or when the corresponding CPU is offline.
  303. */
  304. static void rcu_dynticks_eqs_online(void)
  305. {
  306. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  307. if (atomic_read(&rdtp->dynticks) & RCU_DYNTICK_CTRL_CTR)
  308. return;
  309. atomic_add(RCU_DYNTICK_CTRL_CTR, &rdtp->dynticks);
  310. }
  311. /*
  312. * Is the current CPU in an extended quiescent state?
  313. *
  314. * No ordering, as we are sampling CPU-local information.
  315. */
  316. bool rcu_dynticks_curr_cpu_in_eqs(void)
  317. {
  318. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  319. return !(atomic_read(&rdtp->dynticks) & RCU_DYNTICK_CTRL_CTR);
  320. }
  321. /*
  322. * Snapshot the ->dynticks counter with full ordering so as to allow
  323. * stable comparison of this counter with past and future snapshots.
  324. */
  325. int rcu_dynticks_snap(struct rcu_dynticks *rdtp)
  326. {
  327. int snap = atomic_add_return(0, &rdtp->dynticks);
  328. return snap & ~RCU_DYNTICK_CTRL_MASK;
  329. }
  330. /*
  331. * Return true if the snapshot returned from rcu_dynticks_snap()
  332. * indicates that RCU is in an extended quiescent state.
  333. */
  334. static bool rcu_dynticks_in_eqs(int snap)
  335. {
  336. return !(snap & RCU_DYNTICK_CTRL_CTR);
  337. }
  338. /*
  339. * Return true if the CPU corresponding to the specified rcu_dynticks
  340. * structure has spent some time in an extended quiescent state since
  341. * rcu_dynticks_snap() returned the specified snapshot.
  342. */
  343. static bool rcu_dynticks_in_eqs_since(struct rcu_dynticks *rdtp, int snap)
  344. {
  345. return snap != rcu_dynticks_snap(rdtp);
  346. }
  347. /*
  348. * Do a double-increment of the ->dynticks counter to emulate a
  349. * momentary idle-CPU quiescent state.
  350. */
  351. static void rcu_dynticks_momentary_idle(void)
  352. {
  353. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  354. int special = atomic_add_return(2 * RCU_DYNTICK_CTRL_CTR,
  355. &rdtp->dynticks);
  356. /* It is illegal to call this from idle state. */
  357. WARN_ON_ONCE(!(special & RCU_DYNTICK_CTRL_CTR));
  358. }
  359. /*
  360. * Set the special (bottom) bit of the specified CPU so that it
  361. * will take special action (such as flushing its TLB) on the
  362. * next exit from an extended quiescent state. Returns true if
  363. * the bit was successfully set, or false if the CPU was not in
  364. * an extended quiescent state.
  365. */
  366. bool rcu_eqs_special_set(int cpu)
  367. {
  368. int old;
  369. int new;
  370. struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
  371. do {
  372. old = atomic_read(&rdtp->dynticks);
  373. if (old & RCU_DYNTICK_CTRL_CTR)
  374. return false;
  375. new = old | RCU_DYNTICK_CTRL_MASK;
  376. } while (atomic_cmpxchg(&rdtp->dynticks, old, new) != old);
  377. return true;
  378. }
  379. /*
  380. * Let the RCU core know that this CPU has gone through the scheduler,
  381. * which is a quiescent state. This is called when the need for a
  382. * quiescent state is urgent, so we burn an atomic operation and full
  383. * memory barriers to let the RCU core know about it, regardless of what
  384. * this CPU might (or might not) do in the near future.
  385. *
  386. * We inform the RCU core by emulating a zero-duration dyntick-idle period.
  387. *
  388. * The caller must have disabled interrupts.
  389. */
  390. static void rcu_momentary_dyntick_idle(void)
  391. {
  392. raw_cpu_write(rcu_dynticks.rcu_need_heavy_qs, false);
  393. rcu_dynticks_momentary_idle();
  394. }
  395. /*
  396. * Note a context switch. This is a quiescent state for RCU-sched,
  397. * and requires special handling for preemptible RCU.
  398. * The caller must have disabled interrupts.
  399. */
  400. void rcu_note_context_switch(bool preempt)
  401. {
  402. barrier(); /* Avoid RCU read-side critical sections leaking down. */
  403. trace_rcu_utilization(TPS("Start context switch"));
  404. rcu_sched_qs();
  405. rcu_preempt_note_context_switch(preempt);
  406. /* Load rcu_urgent_qs before other flags. */
  407. if (!smp_load_acquire(this_cpu_ptr(&rcu_dynticks.rcu_urgent_qs)))
  408. goto out;
  409. this_cpu_write(rcu_dynticks.rcu_urgent_qs, false);
  410. if (unlikely(raw_cpu_read(rcu_dynticks.rcu_need_heavy_qs)))
  411. rcu_momentary_dyntick_idle();
  412. this_cpu_inc(rcu_dynticks.rcu_qs_ctr);
  413. if (!preempt)
  414. rcu_note_voluntary_context_switch_lite(current);
  415. out:
  416. trace_rcu_utilization(TPS("End context switch"));
  417. barrier(); /* Avoid RCU read-side critical sections leaking up. */
  418. }
  419. EXPORT_SYMBOL_GPL(rcu_note_context_switch);
  420. /*
  421. * Register a quiescent state for all RCU flavors. If there is an
  422. * emergency, invoke rcu_momentary_dyntick_idle() to do a heavy-weight
  423. * dyntick-idle quiescent state visible to other CPUs (but only for those
  424. * RCU flavors in desperate need of a quiescent state, which will normally
  425. * be none of them). Either way, do a lightweight quiescent state for
  426. * all RCU flavors.
  427. *
  428. * The barrier() calls are redundant in the common case when this is
  429. * called externally, but just in case this is called from within this
  430. * file.
  431. *
  432. */
  433. void rcu_all_qs(void)
  434. {
  435. unsigned long flags;
  436. if (!raw_cpu_read(rcu_dynticks.rcu_urgent_qs))
  437. return;
  438. preempt_disable();
  439. /* Load rcu_urgent_qs before other flags. */
  440. if (!smp_load_acquire(this_cpu_ptr(&rcu_dynticks.rcu_urgent_qs))) {
  441. preempt_enable();
  442. return;
  443. }
  444. this_cpu_write(rcu_dynticks.rcu_urgent_qs, false);
  445. barrier(); /* Avoid RCU read-side critical sections leaking down. */
  446. if (unlikely(raw_cpu_read(rcu_dynticks.rcu_need_heavy_qs))) {
  447. local_irq_save(flags);
  448. rcu_momentary_dyntick_idle();
  449. local_irq_restore(flags);
  450. }
  451. if (unlikely(raw_cpu_read(rcu_sched_data.cpu_no_qs.b.exp)))
  452. rcu_sched_qs();
  453. this_cpu_inc(rcu_dynticks.rcu_qs_ctr);
  454. barrier(); /* Avoid RCU read-side critical sections leaking up. */
  455. preempt_enable();
  456. }
  457. EXPORT_SYMBOL_GPL(rcu_all_qs);
  458. #define DEFAULT_RCU_BLIMIT 10 /* Maximum callbacks per rcu_do_batch. */
  459. static long blimit = DEFAULT_RCU_BLIMIT;
  460. #define DEFAULT_RCU_QHIMARK 10000 /* If this many pending, ignore blimit. */
  461. static long qhimark = DEFAULT_RCU_QHIMARK;
  462. #define DEFAULT_RCU_QLOMARK 100 /* Once only this many pending, use blimit. */
  463. static long qlowmark = DEFAULT_RCU_QLOMARK;
  464. module_param(blimit, long, 0444);
  465. module_param(qhimark, long, 0444);
  466. module_param(qlowmark, long, 0444);
  467. static ulong jiffies_till_first_fqs = ULONG_MAX;
  468. static ulong jiffies_till_next_fqs = ULONG_MAX;
  469. static bool rcu_kick_kthreads;
  470. module_param(jiffies_till_first_fqs, ulong, 0644);
  471. module_param(jiffies_till_next_fqs, ulong, 0644);
  472. module_param(rcu_kick_kthreads, bool, 0644);
  473. /*
  474. * How long the grace period must be before we start recruiting
  475. * quiescent-state help from rcu_note_context_switch().
  476. */
  477. static ulong jiffies_till_sched_qs = HZ / 10;
  478. module_param(jiffies_till_sched_qs, ulong, 0444);
  479. static bool rcu_start_gp_advanced(struct rcu_state *rsp, struct rcu_node *rnp,
  480. struct rcu_data *rdp);
  481. static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *rsp));
  482. static void force_quiescent_state(struct rcu_state *rsp);
  483. static int rcu_pending(void);
  484. /*
  485. * Return the number of RCU batches started thus far for debug & stats.
  486. */
  487. unsigned long rcu_batches_started(void)
  488. {
  489. return rcu_state_p->gpnum;
  490. }
  491. EXPORT_SYMBOL_GPL(rcu_batches_started);
  492. /*
  493. * Return the number of RCU-sched batches started thus far for debug & stats.
  494. */
  495. unsigned long rcu_batches_started_sched(void)
  496. {
  497. return rcu_sched_state.gpnum;
  498. }
  499. EXPORT_SYMBOL_GPL(rcu_batches_started_sched);
  500. /*
  501. * Return the number of RCU BH batches started thus far for debug & stats.
  502. */
  503. unsigned long rcu_batches_started_bh(void)
  504. {
  505. return rcu_bh_state.gpnum;
  506. }
  507. EXPORT_SYMBOL_GPL(rcu_batches_started_bh);
  508. /*
  509. * Return the number of RCU batches completed thus far for debug & stats.
  510. */
  511. unsigned long rcu_batches_completed(void)
  512. {
  513. return rcu_state_p->completed;
  514. }
  515. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  516. /*
  517. * Return the number of RCU-sched batches completed thus far for debug & stats.
  518. */
  519. unsigned long rcu_batches_completed_sched(void)
  520. {
  521. return rcu_sched_state.completed;
  522. }
  523. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  524. /*
  525. * Return the number of RCU BH batches completed thus far for debug & stats.
  526. */
  527. unsigned long rcu_batches_completed_bh(void)
  528. {
  529. return rcu_bh_state.completed;
  530. }
  531. EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
  532. /*
  533. * Return the number of RCU expedited batches completed thus far for
  534. * debug & stats. Odd numbers mean that a batch is in progress, even
  535. * numbers mean idle. The value returned will thus be roughly double
  536. * the cumulative batches since boot.
  537. */
  538. unsigned long rcu_exp_batches_completed(void)
  539. {
  540. return rcu_state_p->expedited_sequence;
  541. }
  542. EXPORT_SYMBOL_GPL(rcu_exp_batches_completed);
  543. /*
  544. * Return the number of RCU-sched expedited batches completed thus far
  545. * for debug & stats. Similar to rcu_exp_batches_completed().
  546. */
  547. unsigned long rcu_exp_batches_completed_sched(void)
  548. {
  549. return rcu_sched_state.expedited_sequence;
  550. }
  551. EXPORT_SYMBOL_GPL(rcu_exp_batches_completed_sched);
  552. /*
  553. * Force a quiescent state.
  554. */
  555. void rcu_force_quiescent_state(void)
  556. {
  557. force_quiescent_state(rcu_state_p);
  558. }
  559. EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  560. /*
  561. * Force a quiescent state for RCU BH.
  562. */
  563. void rcu_bh_force_quiescent_state(void)
  564. {
  565. force_quiescent_state(&rcu_bh_state);
  566. }
  567. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  568. /*
  569. * Force a quiescent state for RCU-sched.
  570. */
  571. void rcu_sched_force_quiescent_state(void)
  572. {
  573. force_quiescent_state(&rcu_sched_state);
  574. }
  575. EXPORT_SYMBOL_GPL(rcu_sched_force_quiescent_state);
  576. /*
  577. * Show the state of the grace-period kthreads.
  578. */
  579. void show_rcu_gp_kthreads(void)
  580. {
  581. struct rcu_state *rsp;
  582. for_each_rcu_flavor(rsp) {
  583. pr_info("%s: wait state: %d ->state: %#lx\n",
  584. rsp->name, rsp->gp_state, rsp->gp_kthread->state);
  585. /* sched_show_task(rsp->gp_kthread); */
  586. }
  587. }
  588. EXPORT_SYMBOL_GPL(show_rcu_gp_kthreads);
  589. /*
  590. * Record the number of times rcutorture tests have been initiated and
  591. * terminated. This information allows the debugfs tracing stats to be
  592. * correlated to the rcutorture messages, even when the rcutorture module
  593. * is being repeatedly loaded and unloaded. In other words, we cannot
  594. * store this state in rcutorture itself.
  595. */
  596. void rcutorture_record_test_transition(void)
  597. {
  598. rcutorture_testseq++;
  599. rcutorture_vernum = 0;
  600. }
  601. EXPORT_SYMBOL_GPL(rcutorture_record_test_transition);
  602. /*
  603. * Send along grace-period-related data for rcutorture diagnostics.
  604. */
  605. void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
  606. unsigned long *gpnum, unsigned long *completed)
  607. {
  608. struct rcu_state *rsp = NULL;
  609. switch (test_type) {
  610. case RCU_FLAVOR:
  611. rsp = rcu_state_p;
  612. break;
  613. case RCU_BH_FLAVOR:
  614. rsp = &rcu_bh_state;
  615. break;
  616. case RCU_SCHED_FLAVOR:
  617. rsp = &rcu_sched_state;
  618. break;
  619. default:
  620. break;
  621. }
  622. if (rsp == NULL)
  623. return;
  624. *flags = READ_ONCE(rsp->gp_flags);
  625. *gpnum = READ_ONCE(rsp->gpnum);
  626. *completed = READ_ONCE(rsp->completed);
  627. }
  628. EXPORT_SYMBOL_GPL(rcutorture_get_gp_data);
  629. /*
  630. * Record the number of writer passes through the current rcutorture test.
  631. * This is also used to correlate debugfs tracing stats with the rcutorture
  632. * messages.
  633. */
  634. void rcutorture_record_progress(unsigned long vernum)
  635. {
  636. rcutorture_vernum++;
  637. }
  638. EXPORT_SYMBOL_GPL(rcutorture_record_progress);
  639. /*
  640. * Return the root node of the specified rcu_state structure.
  641. */
  642. static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
  643. {
  644. return &rsp->node[0];
  645. }
  646. /*
  647. * Is there any need for future grace periods?
  648. * Interrupts must be disabled. If the caller does not hold the root
  649. * rnp_node structure's ->lock, the results are advisory only.
  650. */
  651. static int rcu_future_needs_gp(struct rcu_state *rsp)
  652. {
  653. struct rcu_node *rnp = rcu_get_root(rsp);
  654. lockdep_assert_irqs_disabled();
  655. return READ_ONCE(need_future_gp_element(rnp, rnp->completed));
  656. }
  657. /*
  658. * Does the current CPU require a not-yet-started grace period?
  659. * The caller must have disabled interrupts to prevent races with
  660. * normal callback registry.
  661. */
  662. static bool
  663. cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
  664. {
  665. lockdep_assert_irqs_disabled();
  666. if (rcu_gp_in_progress(rsp))
  667. return false; /* No, a grace period is already in progress. */
  668. if (rcu_future_needs_gp(rsp))
  669. return true; /* Yes, a no-CBs CPU needs one. */
  670. if (!rcu_segcblist_is_enabled(&rdp->cblist))
  671. return false; /* No, this is a no-CBs (or offline) CPU. */
  672. if (!rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
  673. return true; /* Yes, CPU has newly registered callbacks. */
  674. if (rcu_segcblist_future_gp_needed(&rdp->cblist,
  675. READ_ONCE(rsp->completed)))
  676. return true; /* Yes, CBs for future grace period. */
  677. return false; /* No grace period needed. */
  678. }
  679. /*
  680. * Enter an RCU extended quiescent state, which can be either the
  681. * idle loop or adaptive-tickless usermode execution.
  682. *
  683. * We crowbar the ->dynticks_nmi_nesting field to zero to allow for
  684. * the possibility of usermode upcalls having messed up our count
  685. * of interrupt nesting level during the prior busy period.
  686. */
  687. static void rcu_eqs_enter(bool user)
  688. {
  689. struct rcu_state *rsp;
  690. struct rcu_data *rdp;
  691. struct rcu_dynticks *rdtp;
  692. rdtp = this_cpu_ptr(&rcu_dynticks);
  693. WRITE_ONCE(rdtp->dynticks_nmi_nesting, 0);
  694. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
  695. rdtp->dynticks_nesting == 0);
  696. if (rdtp->dynticks_nesting != 1) {
  697. rdtp->dynticks_nesting--;
  698. return;
  699. }
  700. lockdep_assert_irqs_disabled();
  701. trace_rcu_dyntick(TPS("Start"), rdtp->dynticks_nesting, 0, rdtp->dynticks);
  702. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current));
  703. for_each_rcu_flavor(rsp) {
  704. rdp = this_cpu_ptr(rsp->rda);
  705. do_nocb_deferred_wakeup(rdp);
  706. }
  707. rcu_prepare_for_idle();
  708. WRITE_ONCE(rdtp->dynticks_nesting, 0); /* Avoid irq-access tearing. */
  709. rcu_dynticks_eqs_enter();
  710. rcu_dynticks_task_enter();
  711. }
  712. /**
  713. * rcu_idle_enter - inform RCU that current CPU is entering idle
  714. *
  715. * Enter idle mode, in other words, -leave- the mode in which RCU
  716. * read-side critical sections can occur. (Though RCU read-side
  717. * critical sections can occur in irq handlers in idle, a possibility
  718. * handled by irq_enter() and irq_exit().)
  719. *
  720. * If you add or remove a call to rcu_idle_enter(), be sure to test with
  721. * CONFIG_RCU_EQS_DEBUG=y.
  722. */
  723. void rcu_idle_enter(void)
  724. {
  725. lockdep_assert_irqs_disabled();
  726. rcu_eqs_enter(false);
  727. }
  728. #ifdef CONFIG_NO_HZ_FULL
  729. /**
  730. * rcu_user_enter - inform RCU that we are resuming userspace.
  731. *
  732. * Enter RCU idle mode right before resuming userspace. No use of RCU
  733. * is permitted between this call and rcu_user_exit(). This way the
  734. * CPU doesn't need to maintain the tick for RCU maintenance purposes
  735. * when the CPU runs in userspace.
  736. *
  737. * If you add or remove a call to rcu_user_enter(), be sure to test with
  738. * CONFIG_RCU_EQS_DEBUG=y.
  739. */
  740. void rcu_user_enter(void)
  741. {
  742. lockdep_assert_irqs_disabled();
  743. rcu_eqs_enter(true);
  744. }
  745. #endif /* CONFIG_NO_HZ_FULL */
  746. /**
  747. * rcu_nmi_exit - inform RCU of exit from NMI context
  748. *
  749. * If we are returning from the outermost NMI handler that interrupted an
  750. * RCU-idle period, update rdtp->dynticks and rdtp->dynticks_nmi_nesting
  751. * to let the RCU grace-period handling know that the CPU is back to
  752. * being RCU-idle.
  753. *
  754. * If you add or remove a call to rcu_nmi_exit(), be sure to test
  755. * with CONFIG_RCU_EQS_DEBUG=y.
  756. */
  757. void rcu_nmi_exit(void)
  758. {
  759. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  760. /*
  761. * Check for ->dynticks_nmi_nesting underflow and bad ->dynticks.
  762. * (We are exiting an NMI handler, so RCU better be paying attention
  763. * to us!)
  764. */
  765. WARN_ON_ONCE(rdtp->dynticks_nmi_nesting <= 0);
  766. WARN_ON_ONCE(rcu_dynticks_curr_cpu_in_eqs());
  767. /*
  768. * If the nesting level is not 1, the CPU wasn't RCU-idle, so
  769. * leave it in non-RCU-idle state.
  770. */
  771. if (rdtp->dynticks_nmi_nesting != 1) {
  772. trace_rcu_dyntick(TPS("--="), rdtp->dynticks_nmi_nesting, rdtp->dynticks_nmi_nesting - 2, rdtp->dynticks);
  773. WRITE_ONCE(rdtp->dynticks_nmi_nesting, /* No store tearing. */
  774. rdtp->dynticks_nmi_nesting - 2);
  775. return;
  776. }
  777. /* This NMI interrupted an RCU-idle CPU, restore RCU-idleness. */
  778. trace_rcu_dyntick(TPS("Startirq"), rdtp->dynticks_nmi_nesting, 0, rdtp->dynticks);
  779. WRITE_ONCE(rdtp->dynticks_nmi_nesting, 0); /* Avoid store tearing. */
  780. rcu_dynticks_eqs_enter();
  781. }
  782. /**
  783. * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
  784. *
  785. * Exit from an interrupt handler, which might possibly result in entering
  786. * idle mode, in other words, leaving the mode in which read-side critical
  787. * sections can occur. The caller must have disabled interrupts.
  788. *
  789. * This code assumes that the idle loop never does anything that might
  790. * result in unbalanced calls to irq_enter() and irq_exit(). If your
  791. * architecture's idle loop violates this assumption, RCU will give you what
  792. * you deserve, good and hard. But very infrequently and irreproducibly.
  793. *
  794. * Use things like work queues to work around this limitation.
  795. *
  796. * You have been warned.
  797. *
  798. * If you add or remove a call to rcu_irq_exit(), be sure to test with
  799. * CONFIG_RCU_EQS_DEBUG=y.
  800. */
  801. void rcu_irq_exit(void)
  802. {
  803. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  804. lockdep_assert_irqs_disabled();
  805. if (rdtp->dynticks_nmi_nesting == 1)
  806. rcu_prepare_for_idle();
  807. rcu_nmi_exit();
  808. if (rdtp->dynticks_nmi_nesting == 0)
  809. rcu_dynticks_task_enter();
  810. }
  811. /*
  812. * Wrapper for rcu_irq_exit() where interrupts are enabled.
  813. *
  814. * If you add or remove a call to rcu_irq_exit_irqson(), be sure to test
  815. * with CONFIG_RCU_EQS_DEBUG=y.
  816. */
  817. void rcu_irq_exit_irqson(void)
  818. {
  819. unsigned long flags;
  820. local_irq_save(flags);
  821. rcu_irq_exit();
  822. local_irq_restore(flags);
  823. }
  824. /*
  825. * Exit an RCU extended quiescent state, which can be either the
  826. * idle loop or adaptive-tickless usermode execution.
  827. *
  828. * We crowbar the ->dynticks_nmi_nesting field to DYNTICK_IRQ_NONIDLE to
  829. * allow for the possibility of usermode upcalls messing up our count of
  830. * interrupt nesting level during the busy period that is just now starting.
  831. */
  832. static void rcu_eqs_exit(bool user)
  833. {
  834. struct rcu_dynticks *rdtp;
  835. long oldval;
  836. lockdep_assert_irqs_disabled();
  837. rdtp = this_cpu_ptr(&rcu_dynticks);
  838. oldval = rdtp->dynticks_nesting;
  839. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && oldval < 0);
  840. if (oldval) {
  841. rdtp->dynticks_nesting++;
  842. return;
  843. }
  844. rcu_dynticks_task_exit();
  845. rcu_dynticks_eqs_exit();
  846. rcu_cleanup_after_idle();
  847. trace_rcu_dyntick(TPS("End"), rdtp->dynticks_nesting, 1, rdtp->dynticks);
  848. WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current));
  849. WRITE_ONCE(rdtp->dynticks_nesting, 1);
  850. WRITE_ONCE(rdtp->dynticks_nmi_nesting, DYNTICK_IRQ_NONIDLE);
  851. }
  852. /**
  853. * rcu_idle_exit - inform RCU that current CPU is leaving idle
  854. *
  855. * Exit idle mode, in other words, -enter- the mode in which RCU
  856. * read-side critical sections can occur.
  857. *
  858. * If you add or remove a call to rcu_idle_exit(), be sure to test with
  859. * CONFIG_RCU_EQS_DEBUG=y.
  860. */
  861. void rcu_idle_exit(void)
  862. {
  863. unsigned long flags;
  864. local_irq_save(flags);
  865. rcu_eqs_exit(false);
  866. local_irq_restore(flags);
  867. }
  868. #ifdef CONFIG_NO_HZ_FULL
  869. /**
  870. * rcu_user_exit - inform RCU that we are exiting userspace.
  871. *
  872. * Exit RCU idle mode while entering the kernel because it can
  873. * run a RCU read side critical section anytime.
  874. *
  875. * If you add or remove a call to rcu_user_exit(), be sure to test with
  876. * CONFIG_RCU_EQS_DEBUG=y.
  877. */
  878. void rcu_user_exit(void)
  879. {
  880. rcu_eqs_exit(1);
  881. }
  882. #endif /* CONFIG_NO_HZ_FULL */
  883. /**
  884. * rcu_nmi_enter - inform RCU of entry to NMI context
  885. *
  886. * If the CPU was idle from RCU's viewpoint, update rdtp->dynticks and
  887. * rdtp->dynticks_nmi_nesting to let the RCU grace-period handling know
  888. * that the CPU is active. This implementation permits nested NMIs, as
  889. * long as the nesting level does not overflow an int. (You will probably
  890. * run out of stack space first.)
  891. *
  892. * If you add or remove a call to rcu_nmi_enter(), be sure to test
  893. * with CONFIG_RCU_EQS_DEBUG=y.
  894. */
  895. void rcu_nmi_enter(void)
  896. {
  897. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  898. long incby = 2;
  899. /* Complain about underflow. */
  900. WARN_ON_ONCE(rdtp->dynticks_nmi_nesting < 0);
  901. /*
  902. * If idle from RCU viewpoint, atomically increment ->dynticks
  903. * to mark non-idle and increment ->dynticks_nmi_nesting by one.
  904. * Otherwise, increment ->dynticks_nmi_nesting by two. This means
  905. * if ->dynticks_nmi_nesting is equal to one, we are guaranteed
  906. * to be in the outermost NMI handler that interrupted an RCU-idle
  907. * period (observation due to Andy Lutomirski).
  908. */
  909. if (rcu_dynticks_curr_cpu_in_eqs()) {
  910. rcu_dynticks_eqs_exit();
  911. incby = 1;
  912. }
  913. trace_rcu_dyntick(incby == 1 ? TPS("Endirq") : TPS("++="),
  914. rdtp->dynticks_nmi_nesting,
  915. rdtp->dynticks_nmi_nesting + incby, rdtp->dynticks);
  916. WRITE_ONCE(rdtp->dynticks_nmi_nesting, /* Prevent store tearing. */
  917. rdtp->dynticks_nmi_nesting + incby);
  918. barrier();
  919. }
  920. /**
  921. * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
  922. *
  923. * Enter an interrupt handler, which might possibly result in exiting
  924. * idle mode, in other words, entering the mode in which read-side critical
  925. * sections can occur. The caller must have disabled interrupts.
  926. *
  927. * Note that the Linux kernel is fully capable of entering an interrupt
  928. * handler that it never exits, for example when doing upcalls to user mode!
  929. * This code assumes that the idle loop never does upcalls to user mode.
  930. * If your architecture's idle loop does do upcalls to user mode (or does
  931. * anything else that results in unbalanced calls to the irq_enter() and
  932. * irq_exit() functions), RCU will give you what you deserve, good and hard.
  933. * But very infrequently and irreproducibly.
  934. *
  935. * Use things like work queues to work around this limitation.
  936. *
  937. * You have been warned.
  938. *
  939. * If you add or remove a call to rcu_irq_enter(), be sure to test with
  940. * CONFIG_RCU_EQS_DEBUG=y.
  941. */
  942. void rcu_irq_enter(void)
  943. {
  944. struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
  945. lockdep_assert_irqs_disabled();
  946. if (rdtp->dynticks_nmi_nesting == 0)
  947. rcu_dynticks_task_exit();
  948. rcu_nmi_enter();
  949. if (rdtp->dynticks_nmi_nesting == 1)
  950. rcu_cleanup_after_idle();
  951. }
  952. /*
  953. * Wrapper for rcu_irq_enter() where interrupts are enabled.
  954. *
  955. * If you add or remove a call to rcu_irq_enter_irqson(), be sure to test
  956. * with CONFIG_RCU_EQS_DEBUG=y.
  957. */
  958. void rcu_irq_enter_irqson(void)
  959. {
  960. unsigned long flags;
  961. local_irq_save(flags);
  962. rcu_irq_enter();
  963. local_irq_restore(flags);
  964. }
  965. /**
  966. * rcu_is_watching - see if RCU thinks that the current CPU is idle
  967. *
  968. * Return true if RCU is watching the running CPU, which means that this
  969. * CPU can safely enter RCU read-side critical sections. In other words,
  970. * if the current CPU is in its idle loop and is neither in an interrupt
  971. * or NMI handler, return true.
  972. */
  973. bool notrace rcu_is_watching(void)
  974. {
  975. bool ret;
  976. preempt_disable_notrace();
  977. ret = !rcu_dynticks_curr_cpu_in_eqs();
  978. preempt_enable_notrace();
  979. return ret;
  980. }
  981. EXPORT_SYMBOL_GPL(rcu_is_watching);
  982. /*
  983. * If a holdout task is actually running, request an urgent quiescent
  984. * state from its CPU. This is unsynchronized, so migrations can cause
  985. * the request to go to the wrong CPU. Which is OK, all that will happen
  986. * is that the CPU's next context switch will be a bit slower and next
  987. * time around this task will generate another request.
  988. */
  989. void rcu_request_urgent_qs_task(struct task_struct *t)
  990. {
  991. int cpu;
  992. barrier();
  993. cpu = task_cpu(t);
  994. if (!task_curr(t))
  995. return; /* This task is not running on that CPU. */
  996. smp_store_release(per_cpu_ptr(&rcu_dynticks.rcu_urgent_qs, cpu), true);
  997. }
  998. #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
  999. /*
  1000. * Is the current CPU online? Disable preemption to avoid false positives
  1001. * that could otherwise happen due to the current CPU number being sampled,
  1002. * this task being preempted, its old CPU being taken offline, resuming
  1003. * on some other CPU, then determining that its old CPU is now offline.
  1004. * It is OK to use RCU on an offline processor during initial boot, hence
  1005. * the check for rcu_scheduler_fully_active. Note also that it is OK
  1006. * for a CPU coming online to use RCU for one jiffy prior to marking itself
  1007. * online in the cpu_online_mask. Similarly, it is OK for a CPU going
  1008. * offline to continue to use RCU for one jiffy after marking itself
  1009. * offline in the cpu_online_mask. This leniency is necessary given the
  1010. * non-atomic nature of the online and offline processing, for example,
  1011. * the fact that a CPU enters the scheduler after completing the teardown
  1012. * of the CPU.
  1013. *
  1014. * This is also why RCU internally marks CPUs online during in the
  1015. * preparation phase and offline after the CPU has been taken down.
  1016. *
  1017. * Disable checking if in an NMI handler because we cannot safely report
  1018. * errors from NMI handlers anyway.
  1019. */
  1020. bool rcu_lockdep_current_cpu_online(void)
  1021. {
  1022. struct rcu_data *rdp;
  1023. struct rcu_node *rnp;
  1024. bool ret;
  1025. if (in_nmi())
  1026. return true;
  1027. preempt_disable();
  1028. rdp = this_cpu_ptr(&rcu_sched_data);
  1029. rnp = rdp->mynode;
  1030. ret = (rdp->grpmask & rcu_rnp_online_cpus(rnp)) ||
  1031. !rcu_scheduler_fully_active;
  1032. preempt_enable();
  1033. return ret;
  1034. }
  1035. EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
  1036. #endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
  1037. /**
  1038. * rcu_is_cpu_rrupt_from_idle - see if idle or immediately interrupted from idle
  1039. *
  1040. * If the current CPU is idle or running at a first-level (not nested)
  1041. * interrupt from idle, return true. The caller must have at least
  1042. * disabled preemption.
  1043. */
  1044. static int rcu_is_cpu_rrupt_from_idle(void)
  1045. {
  1046. return __this_cpu_read(rcu_dynticks.dynticks_nesting) <= 0 &&
  1047. __this_cpu_read(rcu_dynticks.dynticks_nmi_nesting) <= 1;
  1048. }
  1049. /*
  1050. * We are reporting a quiescent state on behalf of some other CPU, so
  1051. * it is our responsibility to check for and handle potential overflow
  1052. * of the rcu_node ->gpnum counter with respect to the rcu_data counters.
  1053. * After all, the CPU might be in deep idle state, and thus executing no
  1054. * code whatsoever.
  1055. */
  1056. static void rcu_gpnum_ovf(struct rcu_node *rnp, struct rcu_data *rdp)
  1057. {
  1058. raw_lockdep_assert_held_rcu_node(rnp);
  1059. if (ULONG_CMP_LT(READ_ONCE(rdp->gpnum) + ULONG_MAX / 4, rnp->gpnum))
  1060. WRITE_ONCE(rdp->gpwrap, true);
  1061. if (ULONG_CMP_LT(rdp->rcu_iw_gpnum + ULONG_MAX / 4, rnp->gpnum))
  1062. rdp->rcu_iw_gpnum = rnp->gpnum + ULONG_MAX / 4;
  1063. }
  1064. /*
  1065. * Snapshot the specified CPU's dynticks counter so that we can later
  1066. * credit them with an implicit quiescent state. Return 1 if this CPU
  1067. * is in dynticks idle mode, which is an extended quiescent state.
  1068. */
  1069. static int dyntick_save_progress_counter(struct rcu_data *rdp)
  1070. {
  1071. rdp->dynticks_snap = rcu_dynticks_snap(rdp->dynticks);
  1072. if (rcu_dynticks_in_eqs(rdp->dynticks_snap)) {
  1073. trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("dti"));
  1074. rcu_gpnum_ovf(rdp->mynode, rdp);
  1075. return 1;
  1076. }
  1077. return 0;
  1078. }
  1079. /*
  1080. * Handler for the irq_work request posted when a grace period has
  1081. * gone on for too long, but not yet long enough for an RCU CPU
  1082. * stall warning. Set state appropriately, but just complain if
  1083. * there is unexpected state on entry.
  1084. */
  1085. static void rcu_iw_handler(struct irq_work *iwp)
  1086. {
  1087. struct rcu_data *rdp;
  1088. struct rcu_node *rnp;
  1089. rdp = container_of(iwp, struct rcu_data, rcu_iw);
  1090. rnp = rdp->mynode;
  1091. raw_spin_lock_rcu_node(rnp);
  1092. if (!WARN_ON_ONCE(!rdp->rcu_iw_pending)) {
  1093. rdp->rcu_iw_gpnum = rnp->gpnum;
  1094. rdp->rcu_iw_pending = false;
  1095. }
  1096. raw_spin_unlock_rcu_node(rnp);
  1097. }
  1098. /*
  1099. * Return true if the specified CPU has passed through a quiescent
  1100. * state by virtue of being in or having passed through an dynticks
  1101. * idle state since the last call to dyntick_save_progress_counter()
  1102. * for this same CPU, or by virtue of having been offline.
  1103. */
  1104. static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
  1105. {
  1106. unsigned long jtsq;
  1107. bool *rnhqp;
  1108. bool *ruqp;
  1109. struct rcu_node *rnp = rdp->mynode;
  1110. /*
  1111. * If the CPU passed through or entered a dynticks idle phase with
  1112. * no active irq/NMI handlers, then we can safely pretend that the CPU
  1113. * already acknowledged the request to pass through a quiescent
  1114. * state. Either way, that CPU cannot possibly be in an RCU
  1115. * read-side critical section that started before the beginning
  1116. * of the current RCU grace period.
  1117. */
  1118. if (rcu_dynticks_in_eqs_since(rdp->dynticks, rdp->dynticks_snap)) {
  1119. trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("dti"));
  1120. rdp->dynticks_fqs++;
  1121. rcu_gpnum_ovf(rnp, rdp);
  1122. return 1;
  1123. }
  1124. /*
  1125. * Has this CPU encountered a cond_resched_rcu_qs() since the
  1126. * beginning of the grace period? For this to be the case,
  1127. * the CPU has to have noticed the current grace period. This
  1128. * might not be the case for nohz_full CPUs looping in the kernel.
  1129. */
  1130. jtsq = jiffies_till_sched_qs;
  1131. ruqp = per_cpu_ptr(&rcu_dynticks.rcu_urgent_qs, rdp->cpu);
  1132. if (time_after(jiffies, rdp->rsp->gp_start + jtsq) &&
  1133. READ_ONCE(rdp->rcu_qs_ctr_snap) != per_cpu(rcu_dynticks.rcu_qs_ctr, rdp->cpu) &&
  1134. READ_ONCE(rdp->gpnum) == rnp->gpnum && !rdp->gpwrap) {
  1135. trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("rqc"));
  1136. rcu_gpnum_ovf(rnp, rdp);
  1137. return 1;
  1138. } else if (time_after(jiffies, rdp->rsp->gp_start + jtsq)) {
  1139. /* Load rcu_qs_ctr before store to rcu_urgent_qs. */
  1140. smp_store_release(ruqp, true);
  1141. }
  1142. /* Check for the CPU being offline. */
  1143. if (!(rdp->grpmask & rcu_rnp_online_cpus(rnp))) {
  1144. trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("ofl"));
  1145. rdp->offline_fqs++;
  1146. rcu_gpnum_ovf(rnp, rdp);
  1147. return 1;
  1148. }
  1149. /*
  1150. * A CPU running for an extended time within the kernel can
  1151. * delay RCU grace periods. When the CPU is in NO_HZ_FULL mode,
  1152. * even context-switching back and forth between a pair of
  1153. * in-kernel CPU-bound tasks cannot advance grace periods.
  1154. * So if the grace period is old enough, make the CPU pay attention.
  1155. * Note that the unsynchronized assignments to the per-CPU
  1156. * rcu_need_heavy_qs variable are safe. Yes, setting of
  1157. * bits can be lost, but they will be set again on the next
  1158. * force-quiescent-state pass. So lost bit sets do not result
  1159. * in incorrect behavior, merely in a grace period lasting
  1160. * a few jiffies longer than it might otherwise. Because
  1161. * there are at most four threads involved, and because the
  1162. * updates are only once every few jiffies, the probability of
  1163. * lossage (and thus of slight grace-period extension) is
  1164. * quite low.
  1165. */
  1166. rnhqp = &per_cpu(rcu_dynticks.rcu_need_heavy_qs, rdp->cpu);
  1167. if (!READ_ONCE(*rnhqp) &&
  1168. (time_after(jiffies, rdp->rsp->gp_start + jtsq) ||
  1169. time_after(jiffies, rdp->rsp->jiffies_resched))) {
  1170. WRITE_ONCE(*rnhqp, true);
  1171. /* Store rcu_need_heavy_qs before rcu_urgent_qs. */
  1172. smp_store_release(ruqp, true);
  1173. rdp->rsp->jiffies_resched += jtsq; /* Re-enable beating. */
  1174. }
  1175. /*
  1176. * If more than halfway to RCU CPU stall-warning time, do a
  1177. * resched_cpu() to try to loosen things up a bit. Also check to
  1178. * see if the CPU is getting hammered with interrupts, but only
  1179. * once per grace period, just to keep the IPIs down to a dull roar.
  1180. */
  1181. if (jiffies - rdp->rsp->gp_start > rcu_jiffies_till_stall_check() / 2) {
  1182. resched_cpu(rdp->cpu);
  1183. if (IS_ENABLED(CONFIG_IRQ_WORK) &&
  1184. !rdp->rcu_iw_pending && rdp->rcu_iw_gpnum != rnp->gpnum &&
  1185. (rnp->ffmask & rdp->grpmask)) {
  1186. init_irq_work(&rdp->rcu_iw, rcu_iw_handler);
  1187. rdp->rcu_iw_pending = true;
  1188. rdp->rcu_iw_gpnum = rnp->gpnum;
  1189. irq_work_queue_on(&rdp->rcu_iw, rdp->cpu);
  1190. }
  1191. }
  1192. return 0;
  1193. }
  1194. static void record_gp_stall_check_time(struct rcu_state *rsp)
  1195. {
  1196. unsigned long j = jiffies;
  1197. unsigned long j1;
  1198. rsp->gp_start = j;
  1199. smp_wmb(); /* Record start time before stall time. */
  1200. j1 = rcu_jiffies_till_stall_check();
  1201. WRITE_ONCE(rsp->jiffies_stall, j + j1);
  1202. rsp->jiffies_resched = j + j1 / 2;
  1203. rsp->n_force_qs_gpstart = READ_ONCE(rsp->n_force_qs);
  1204. }
  1205. /*
  1206. * Convert a ->gp_state value to a character string.
  1207. */
  1208. static const char *gp_state_getname(short gs)
  1209. {
  1210. if (gs < 0 || gs >= ARRAY_SIZE(gp_state_names))
  1211. return "???";
  1212. return gp_state_names[gs];
  1213. }
  1214. /*
  1215. * Complain about starvation of grace-period kthread.
  1216. */
  1217. static void rcu_check_gp_kthread_starvation(struct rcu_state *rsp)
  1218. {
  1219. unsigned long gpa;
  1220. unsigned long j;
  1221. j = jiffies;
  1222. gpa = READ_ONCE(rsp->gp_activity);
  1223. if (j - gpa > 2 * HZ) {
  1224. pr_err("%s kthread starved for %ld jiffies! g%lu c%lu f%#x %s(%d) ->state=%#lx ->cpu=%d\n",
  1225. rsp->name, j - gpa,
  1226. rsp->gpnum, rsp->completed,
  1227. rsp->gp_flags,
  1228. gp_state_getname(rsp->gp_state), rsp->gp_state,
  1229. rsp->gp_kthread ? rsp->gp_kthread->state : ~0,
  1230. rsp->gp_kthread ? task_cpu(rsp->gp_kthread) : -1);
  1231. if (rsp->gp_kthread) {
  1232. pr_err("RCU grace-period kthread stack dump:\n");
  1233. sched_show_task(rsp->gp_kthread);
  1234. wake_up_process(rsp->gp_kthread);
  1235. }
  1236. }
  1237. }
  1238. /*
  1239. * Dump stacks of all tasks running on stalled CPUs. First try using
  1240. * NMIs, but fall back to manual remote stack tracing on architectures
  1241. * that don't support NMI-based stack dumps. The NMI-triggered stack
  1242. * traces are more accurate because they are printed by the target CPU.
  1243. */
  1244. static void rcu_dump_cpu_stacks(struct rcu_state *rsp)
  1245. {
  1246. int cpu;
  1247. unsigned long flags;
  1248. struct rcu_node *rnp;
  1249. rcu_for_each_leaf_node(rsp, rnp) {
  1250. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1251. for_each_leaf_node_possible_cpu(rnp, cpu)
  1252. if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu))
  1253. if (!trigger_single_cpu_backtrace(cpu))
  1254. dump_cpu_task(cpu);
  1255. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1256. }
  1257. }
  1258. /*
  1259. * If too much time has passed in the current grace period, and if
  1260. * so configured, go kick the relevant kthreads.
  1261. */
  1262. static void rcu_stall_kick_kthreads(struct rcu_state *rsp)
  1263. {
  1264. unsigned long j;
  1265. if (!rcu_kick_kthreads)
  1266. return;
  1267. j = READ_ONCE(rsp->jiffies_kick_kthreads);
  1268. if (time_after(jiffies, j) && rsp->gp_kthread &&
  1269. (rcu_gp_in_progress(rsp) || READ_ONCE(rsp->gp_flags))) {
  1270. WARN_ONCE(1, "Kicking %s grace-period kthread\n", rsp->name);
  1271. rcu_ftrace_dump(DUMP_ALL);
  1272. wake_up_process(rsp->gp_kthread);
  1273. WRITE_ONCE(rsp->jiffies_kick_kthreads, j + HZ);
  1274. }
  1275. }
  1276. static inline void panic_on_rcu_stall(void)
  1277. {
  1278. if (sysctl_panic_on_rcu_stall)
  1279. panic("RCU Stall\n");
  1280. }
  1281. static void print_other_cpu_stall(struct rcu_state *rsp, unsigned long gpnum)
  1282. {
  1283. int cpu;
  1284. long delta;
  1285. unsigned long flags;
  1286. unsigned long gpa;
  1287. unsigned long j;
  1288. int ndetected = 0;
  1289. struct rcu_node *rnp = rcu_get_root(rsp);
  1290. long totqlen = 0;
  1291. /* Kick and suppress, if so configured. */
  1292. rcu_stall_kick_kthreads(rsp);
  1293. if (rcu_cpu_stall_suppress)
  1294. return;
  1295. /* Only let one CPU complain about others per time interval. */
  1296. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1297. delta = jiffies - READ_ONCE(rsp->jiffies_stall);
  1298. if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) {
  1299. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1300. return;
  1301. }
  1302. WRITE_ONCE(rsp->jiffies_stall,
  1303. jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
  1304. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1305. /*
  1306. * OK, time to rat on our buddy...
  1307. * See Documentation/RCU/stallwarn.txt for info on how to debug
  1308. * RCU CPU stall warnings.
  1309. */
  1310. pr_err("INFO: %s detected stalls on CPUs/tasks:",
  1311. rsp->name);
  1312. print_cpu_stall_info_begin();
  1313. rcu_for_each_leaf_node(rsp, rnp) {
  1314. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1315. ndetected += rcu_print_task_stall(rnp);
  1316. if (rnp->qsmask != 0) {
  1317. for_each_leaf_node_possible_cpu(rnp, cpu)
  1318. if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu)) {
  1319. print_cpu_stall_info(rsp, cpu);
  1320. ndetected++;
  1321. }
  1322. }
  1323. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1324. }
  1325. print_cpu_stall_info_end();
  1326. for_each_possible_cpu(cpu)
  1327. totqlen += rcu_segcblist_n_cbs(&per_cpu_ptr(rsp->rda,
  1328. cpu)->cblist);
  1329. pr_cont("(detected by %d, t=%ld jiffies, g=%ld, c=%ld, q=%lu)\n",
  1330. smp_processor_id(), (long)(jiffies - rsp->gp_start),
  1331. (long)rsp->gpnum, (long)rsp->completed, totqlen);
  1332. if (ndetected) {
  1333. rcu_dump_cpu_stacks(rsp);
  1334. /* Complain about tasks blocking the grace period. */
  1335. rcu_print_detail_task_stall(rsp);
  1336. } else {
  1337. if (READ_ONCE(rsp->gpnum) != gpnum ||
  1338. READ_ONCE(rsp->completed) == gpnum) {
  1339. pr_err("INFO: Stall ended before state dump start\n");
  1340. } else {
  1341. j = jiffies;
  1342. gpa = READ_ONCE(rsp->gp_activity);
  1343. pr_err("All QSes seen, last %s kthread activity %ld (%ld-%ld), jiffies_till_next_fqs=%ld, root ->qsmask %#lx\n",
  1344. rsp->name, j - gpa, j, gpa,
  1345. jiffies_till_next_fqs,
  1346. rcu_get_root(rsp)->qsmask);
  1347. /* In this case, the current CPU might be at fault. */
  1348. sched_show_task(current);
  1349. }
  1350. }
  1351. rcu_check_gp_kthread_starvation(rsp);
  1352. panic_on_rcu_stall();
  1353. force_quiescent_state(rsp); /* Kick them all. */
  1354. }
  1355. static void print_cpu_stall(struct rcu_state *rsp)
  1356. {
  1357. int cpu;
  1358. unsigned long flags;
  1359. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  1360. struct rcu_node *rnp = rcu_get_root(rsp);
  1361. long totqlen = 0;
  1362. /* Kick and suppress, if so configured. */
  1363. rcu_stall_kick_kthreads(rsp);
  1364. if (rcu_cpu_stall_suppress)
  1365. return;
  1366. /*
  1367. * OK, time to rat on ourselves...
  1368. * See Documentation/RCU/stallwarn.txt for info on how to debug
  1369. * RCU CPU stall warnings.
  1370. */
  1371. pr_err("INFO: %s self-detected stall on CPU", rsp->name);
  1372. print_cpu_stall_info_begin();
  1373. raw_spin_lock_irqsave_rcu_node(rdp->mynode, flags);
  1374. print_cpu_stall_info(rsp, smp_processor_id());
  1375. raw_spin_unlock_irqrestore_rcu_node(rdp->mynode, flags);
  1376. print_cpu_stall_info_end();
  1377. for_each_possible_cpu(cpu)
  1378. totqlen += rcu_segcblist_n_cbs(&per_cpu_ptr(rsp->rda,
  1379. cpu)->cblist);
  1380. pr_cont(" (t=%lu jiffies g=%ld c=%ld q=%lu)\n",
  1381. jiffies - rsp->gp_start,
  1382. (long)rsp->gpnum, (long)rsp->completed, totqlen);
  1383. rcu_check_gp_kthread_starvation(rsp);
  1384. rcu_dump_cpu_stacks(rsp);
  1385. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1386. if (ULONG_CMP_GE(jiffies, READ_ONCE(rsp->jiffies_stall)))
  1387. WRITE_ONCE(rsp->jiffies_stall,
  1388. jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
  1389. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1390. panic_on_rcu_stall();
  1391. /*
  1392. * Attempt to revive the RCU machinery by forcing a context switch.
  1393. *
  1394. * A context switch would normally allow the RCU state machine to make
  1395. * progress and it could be we're stuck in kernel space without context
  1396. * switches for an entirely unreasonable amount of time.
  1397. */
  1398. resched_cpu(smp_processor_id());
  1399. }
  1400. static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
  1401. {
  1402. unsigned long completed;
  1403. unsigned long gpnum;
  1404. unsigned long gps;
  1405. unsigned long j;
  1406. unsigned long js;
  1407. struct rcu_node *rnp;
  1408. if ((rcu_cpu_stall_suppress && !rcu_kick_kthreads) ||
  1409. !rcu_gp_in_progress(rsp))
  1410. return;
  1411. rcu_stall_kick_kthreads(rsp);
  1412. j = jiffies;
  1413. /*
  1414. * Lots of memory barriers to reject false positives.
  1415. *
  1416. * The idea is to pick up rsp->gpnum, then rsp->jiffies_stall,
  1417. * then rsp->gp_start, and finally rsp->completed. These values
  1418. * are updated in the opposite order with memory barriers (or
  1419. * equivalent) during grace-period initialization and cleanup.
  1420. * Now, a false positive can occur if we get an new value of
  1421. * rsp->gp_start and a old value of rsp->jiffies_stall. But given
  1422. * the memory barriers, the only way that this can happen is if one
  1423. * grace period ends and another starts between these two fetches.
  1424. * Detect this by comparing rsp->completed with the previous fetch
  1425. * from rsp->gpnum.
  1426. *
  1427. * Given this check, comparisons of jiffies, rsp->jiffies_stall,
  1428. * and rsp->gp_start suffice to forestall false positives.
  1429. */
  1430. gpnum = READ_ONCE(rsp->gpnum);
  1431. smp_rmb(); /* Pick up ->gpnum first... */
  1432. js = READ_ONCE(rsp->jiffies_stall);
  1433. smp_rmb(); /* ...then ->jiffies_stall before the rest... */
  1434. gps = READ_ONCE(rsp->gp_start);
  1435. smp_rmb(); /* ...and finally ->gp_start before ->completed. */
  1436. completed = READ_ONCE(rsp->completed);
  1437. if (ULONG_CMP_GE(completed, gpnum) ||
  1438. ULONG_CMP_LT(j, js) ||
  1439. ULONG_CMP_GE(gps, js))
  1440. return; /* No stall or GP completed since entering function. */
  1441. rnp = rdp->mynode;
  1442. if (rcu_gp_in_progress(rsp) &&
  1443. (READ_ONCE(rnp->qsmask) & rdp->grpmask)) {
  1444. /* We haven't checked in, so go dump stack. */
  1445. print_cpu_stall(rsp);
  1446. } else if (rcu_gp_in_progress(rsp) &&
  1447. ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY)) {
  1448. /* They had a few time units to dump stack, so complain. */
  1449. print_other_cpu_stall(rsp, gpnum);
  1450. }
  1451. }
  1452. /**
  1453. * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
  1454. *
  1455. * Set the stall-warning timeout way off into the future, thus preventing
  1456. * any RCU CPU stall-warning messages from appearing in the current set of
  1457. * RCU grace periods.
  1458. *
  1459. * The caller must disable hard irqs.
  1460. */
  1461. void rcu_cpu_stall_reset(void)
  1462. {
  1463. struct rcu_state *rsp;
  1464. for_each_rcu_flavor(rsp)
  1465. WRITE_ONCE(rsp->jiffies_stall, jiffies + ULONG_MAX / 2);
  1466. }
  1467. /*
  1468. * Determine the value that ->completed will have at the end of the
  1469. * next subsequent grace period. This is used to tag callbacks so that
  1470. * a CPU can invoke callbacks in a timely fashion even if that CPU has
  1471. * been dyntick-idle for an extended period with callbacks under the
  1472. * influence of RCU_FAST_NO_HZ.
  1473. *
  1474. * The caller must hold rnp->lock with interrupts disabled.
  1475. */
  1476. static unsigned long rcu_cbs_completed(struct rcu_state *rsp,
  1477. struct rcu_node *rnp)
  1478. {
  1479. raw_lockdep_assert_held_rcu_node(rnp);
  1480. /*
  1481. * If RCU is idle, we just wait for the next grace period.
  1482. * But we can only be sure that RCU is idle if we are looking
  1483. * at the root rcu_node structure -- otherwise, a new grace
  1484. * period might have started, but just not yet gotten around
  1485. * to initializing the current non-root rcu_node structure.
  1486. */
  1487. if (rcu_get_root(rsp) == rnp && rnp->gpnum == rnp->completed)
  1488. return rnp->completed + 1;
  1489. /*
  1490. * If the current rcu_node structure believes that RCU is
  1491. * idle, and if the rcu_state structure does not yet reflect
  1492. * the start of a new grace period, then the next grace period
  1493. * will suffice. The memory barrier is needed to accurately
  1494. * sample the rsp->gpnum, and pairs with the second lock
  1495. * acquisition in rcu_gp_init(), which is augmented with
  1496. * smp_mb__after_unlock_lock() for this purpose.
  1497. */
  1498. if (rnp->gpnum == rnp->completed) {
  1499. smp_mb(); /* See above block comment. */
  1500. if (READ_ONCE(rsp->gpnum) == rnp->completed)
  1501. return rnp->completed + 1;
  1502. }
  1503. /*
  1504. * Otherwise, wait for a possible partial grace period and
  1505. * then the subsequent full grace period.
  1506. */
  1507. return rnp->completed + 2;
  1508. }
  1509. /*
  1510. * Trace-event helper function for rcu_start_future_gp() and
  1511. * rcu_nocb_wait_gp().
  1512. */
  1513. static void trace_rcu_future_gp(struct rcu_node *rnp, struct rcu_data *rdp,
  1514. unsigned long c, const char *s)
  1515. {
  1516. trace_rcu_future_grace_period(rdp->rsp->name, rnp->gpnum,
  1517. rnp->completed, c, rnp->level,
  1518. rnp->grplo, rnp->grphi, s);
  1519. }
  1520. /*
  1521. * Start some future grace period, as needed to handle newly arrived
  1522. * callbacks. The required future grace periods are recorded in each
  1523. * rcu_node structure's ->need_future_gp field. Returns true if there
  1524. * is reason to awaken the grace-period kthread.
  1525. *
  1526. * The caller must hold the specified rcu_node structure's ->lock.
  1527. */
  1528. static bool __maybe_unused
  1529. rcu_start_future_gp(struct rcu_node *rnp, struct rcu_data *rdp,
  1530. unsigned long *c_out)
  1531. {
  1532. unsigned long c;
  1533. bool ret = false;
  1534. struct rcu_node *rnp_root = rcu_get_root(rdp->rsp);
  1535. raw_lockdep_assert_held_rcu_node(rnp);
  1536. /*
  1537. * Pick up grace-period number for new callbacks. If this
  1538. * grace period is already marked as needed, return to the caller.
  1539. */
  1540. c = rcu_cbs_completed(rdp->rsp, rnp);
  1541. trace_rcu_future_gp(rnp, rdp, c, TPS("Startleaf"));
  1542. if (need_future_gp_element(rnp, c)) {
  1543. trace_rcu_future_gp(rnp, rdp, c, TPS("Prestartleaf"));
  1544. goto out;
  1545. }
  1546. /*
  1547. * If this rcu_node structure believes that a grace period is in
  1548. * progress, then we must wait for the one following, which is in
  1549. * "c". Because our request will be noticed at the end of the
  1550. * current grace period, we don't need to explicitly start one.
  1551. */
  1552. if (rnp->gpnum != rnp->completed) {
  1553. need_future_gp_element(rnp, c)++;
  1554. trace_rcu_future_gp(rnp, rdp, c, TPS("Startedleaf"));
  1555. goto out;
  1556. }
  1557. /*
  1558. * There might be no grace period in progress. If we don't already
  1559. * hold it, acquire the root rcu_node structure's lock in order to
  1560. * start one (if needed).
  1561. */
  1562. if (rnp != rnp_root)
  1563. raw_spin_lock_rcu_node(rnp_root);
  1564. /*
  1565. * Get a new grace-period number. If there really is no grace
  1566. * period in progress, it will be smaller than the one we obtained
  1567. * earlier. Adjust callbacks as needed.
  1568. */
  1569. c = rcu_cbs_completed(rdp->rsp, rnp_root);
  1570. if (!rcu_is_nocb_cpu(rdp->cpu))
  1571. (void)rcu_segcblist_accelerate(&rdp->cblist, c);
  1572. /*
  1573. * If the needed for the required grace period is already
  1574. * recorded, trace and leave.
  1575. */
  1576. if (need_future_gp_element(rnp_root, c)) {
  1577. trace_rcu_future_gp(rnp, rdp, c, TPS("Prestartedroot"));
  1578. goto unlock_out;
  1579. }
  1580. /* Record the need for the future grace period. */
  1581. need_future_gp_element(rnp_root, c)++;
  1582. /* If a grace period is not already in progress, start one. */
  1583. if (rnp_root->gpnum != rnp_root->completed) {
  1584. trace_rcu_future_gp(rnp, rdp, c, TPS("Startedleafroot"));
  1585. } else {
  1586. trace_rcu_future_gp(rnp, rdp, c, TPS("Startedroot"));
  1587. ret = rcu_start_gp_advanced(rdp->rsp, rnp_root, rdp);
  1588. }
  1589. unlock_out:
  1590. if (rnp != rnp_root)
  1591. raw_spin_unlock_rcu_node(rnp_root);
  1592. out:
  1593. if (c_out != NULL)
  1594. *c_out = c;
  1595. return ret;
  1596. }
  1597. /*
  1598. * Clean up any old requests for the just-ended grace period. Also return
  1599. * whether any additional grace periods have been requested.
  1600. */
  1601. static int rcu_future_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  1602. {
  1603. int c = rnp->completed;
  1604. int needmore;
  1605. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  1606. need_future_gp_element(rnp, c) = 0;
  1607. needmore = need_future_gp_element(rnp, c + 1);
  1608. trace_rcu_future_gp(rnp, rdp, c,
  1609. needmore ? TPS("CleanupMore") : TPS("Cleanup"));
  1610. return needmore;
  1611. }
  1612. /*
  1613. * Awaken the grace-period kthread for the specified flavor of RCU.
  1614. * Don't do a self-awaken, and don't bother awakening when there is
  1615. * nothing for the grace-period kthread to do (as in several CPUs
  1616. * raced to awaken, and we lost), and finally don't try to awaken
  1617. * a kthread that has not yet been created.
  1618. */
  1619. static void rcu_gp_kthread_wake(struct rcu_state *rsp)
  1620. {
  1621. if (current == rsp->gp_kthread ||
  1622. !READ_ONCE(rsp->gp_flags) ||
  1623. !rsp->gp_kthread)
  1624. return;
  1625. swake_up(&rsp->gp_wq);
  1626. }
  1627. /*
  1628. * If there is room, assign a ->completed number to any callbacks on
  1629. * this CPU that have not already been assigned. Also accelerate any
  1630. * callbacks that were previously assigned a ->completed number that has
  1631. * since proven to be too conservative, which can happen if callbacks get
  1632. * assigned a ->completed number while RCU is idle, but with reference to
  1633. * a non-root rcu_node structure. This function is idempotent, so it does
  1634. * not hurt to call it repeatedly. Returns an flag saying that we should
  1635. * awaken the RCU grace-period kthread.
  1636. *
  1637. * The caller must hold rnp->lock with interrupts disabled.
  1638. */
  1639. static bool rcu_accelerate_cbs(struct rcu_state *rsp, struct rcu_node *rnp,
  1640. struct rcu_data *rdp)
  1641. {
  1642. bool ret = false;
  1643. raw_lockdep_assert_held_rcu_node(rnp);
  1644. /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
  1645. if (!rcu_segcblist_pend_cbs(&rdp->cblist))
  1646. return false;
  1647. /*
  1648. * Callbacks are often registered with incomplete grace-period
  1649. * information. Something about the fact that getting exact
  1650. * information requires acquiring a global lock... RCU therefore
  1651. * makes a conservative estimate of the grace period number at which
  1652. * a given callback will become ready to invoke. The following
  1653. * code checks this estimate and improves it when possible, thus
  1654. * accelerating callback invocation to an earlier grace-period
  1655. * number.
  1656. */
  1657. if (rcu_segcblist_accelerate(&rdp->cblist, rcu_cbs_completed(rsp, rnp)))
  1658. ret = rcu_start_future_gp(rnp, rdp, NULL);
  1659. /* Trace depending on how much we were able to accelerate. */
  1660. if (rcu_segcblist_restempty(&rdp->cblist, RCU_WAIT_TAIL))
  1661. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("AccWaitCB"));
  1662. else
  1663. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("AccReadyCB"));
  1664. return ret;
  1665. }
  1666. /*
  1667. * Move any callbacks whose grace period has completed to the
  1668. * RCU_DONE_TAIL sublist, then compact the remaining sublists and
  1669. * assign ->completed numbers to any callbacks in the RCU_NEXT_TAIL
  1670. * sublist. This function is idempotent, so it does not hurt to
  1671. * invoke it repeatedly. As long as it is not invoked -too- often...
  1672. * Returns true if the RCU grace-period kthread needs to be awakened.
  1673. *
  1674. * The caller must hold rnp->lock with interrupts disabled.
  1675. */
  1676. static bool rcu_advance_cbs(struct rcu_state *rsp, struct rcu_node *rnp,
  1677. struct rcu_data *rdp)
  1678. {
  1679. raw_lockdep_assert_held_rcu_node(rnp);
  1680. /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
  1681. if (!rcu_segcblist_pend_cbs(&rdp->cblist))
  1682. return false;
  1683. /*
  1684. * Find all callbacks whose ->completed numbers indicate that they
  1685. * are ready to invoke, and put them into the RCU_DONE_TAIL sublist.
  1686. */
  1687. rcu_segcblist_advance(&rdp->cblist, rnp->completed);
  1688. /* Classify any remaining callbacks. */
  1689. return rcu_accelerate_cbs(rsp, rnp, rdp);
  1690. }
  1691. /*
  1692. * Update CPU-local rcu_data state to record the beginnings and ends of
  1693. * grace periods. The caller must hold the ->lock of the leaf rcu_node
  1694. * structure corresponding to the current CPU, and must have irqs disabled.
  1695. * Returns true if the grace-period kthread needs to be awakened.
  1696. */
  1697. static bool __note_gp_changes(struct rcu_state *rsp, struct rcu_node *rnp,
  1698. struct rcu_data *rdp)
  1699. {
  1700. bool ret;
  1701. bool need_gp;
  1702. raw_lockdep_assert_held_rcu_node(rnp);
  1703. /* Handle the ends of any preceding grace periods first. */
  1704. if (rdp->completed == rnp->completed &&
  1705. !unlikely(READ_ONCE(rdp->gpwrap))) {
  1706. /* No grace period end, so just accelerate recent callbacks. */
  1707. ret = rcu_accelerate_cbs(rsp, rnp, rdp);
  1708. } else {
  1709. /* Advance callbacks. */
  1710. ret = rcu_advance_cbs(rsp, rnp, rdp);
  1711. /* Remember that we saw this grace-period completion. */
  1712. rdp->completed = rnp->completed;
  1713. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpuend"));
  1714. }
  1715. if (rdp->gpnum != rnp->gpnum || unlikely(READ_ONCE(rdp->gpwrap))) {
  1716. /*
  1717. * If the current grace period is waiting for this CPU,
  1718. * set up to detect a quiescent state, otherwise don't
  1719. * go looking for one.
  1720. */
  1721. rdp->gpnum = rnp->gpnum;
  1722. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpustart"));
  1723. need_gp = !!(rnp->qsmask & rdp->grpmask);
  1724. rdp->cpu_no_qs.b.norm = need_gp;
  1725. rdp->rcu_qs_ctr_snap = __this_cpu_read(rcu_dynticks.rcu_qs_ctr);
  1726. rdp->core_needs_qs = need_gp;
  1727. zero_cpu_stall_ticks(rdp);
  1728. WRITE_ONCE(rdp->gpwrap, false);
  1729. rcu_gpnum_ovf(rnp, rdp);
  1730. }
  1731. return ret;
  1732. }
  1733. static void note_gp_changes(struct rcu_state *rsp, struct rcu_data *rdp)
  1734. {
  1735. unsigned long flags;
  1736. bool needwake;
  1737. struct rcu_node *rnp;
  1738. local_irq_save(flags);
  1739. rnp = rdp->mynode;
  1740. if ((rdp->gpnum == READ_ONCE(rnp->gpnum) &&
  1741. rdp->completed == READ_ONCE(rnp->completed) &&
  1742. !unlikely(READ_ONCE(rdp->gpwrap))) || /* w/out lock. */
  1743. !raw_spin_trylock_rcu_node(rnp)) { /* irqs already off, so later. */
  1744. local_irq_restore(flags);
  1745. return;
  1746. }
  1747. needwake = __note_gp_changes(rsp, rnp, rdp);
  1748. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1749. if (needwake)
  1750. rcu_gp_kthread_wake(rsp);
  1751. }
  1752. static void rcu_gp_slow(struct rcu_state *rsp, int delay)
  1753. {
  1754. if (delay > 0 &&
  1755. !(rsp->gpnum % (rcu_num_nodes * PER_RCU_NODE_PERIOD * delay)))
  1756. schedule_timeout_uninterruptible(delay);
  1757. }
  1758. /*
  1759. * Initialize a new grace period. Return false if no grace period required.
  1760. */
  1761. static bool rcu_gp_init(struct rcu_state *rsp)
  1762. {
  1763. unsigned long oldmask;
  1764. struct rcu_data *rdp;
  1765. struct rcu_node *rnp = rcu_get_root(rsp);
  1766. WRITE_ONCE(rsp->gp_activity, jiffies);
  1767. raw_spin_lock_irq_rcu_node(rnp);
  1768. if (!READ_ONCE(rsp->gp_flags)) {
  1769. /* Spurious wakeup, tell caller to go back to sleep. */
  1770. raw_spin_unlock_irq_rcu_node(rnp);
  1771. return false;
  1772. }
  1773. WRITE_ONCE(rsp->gp_flags, 0); /* Clear all flags: New grace period. */
  1774. if (WARN_ON_ONCE(rcu_gp_in_progress(rsp))) {
  1775. /*
  1776. * Grace period already in progress, don't start another.
  1777. * Not supposed to be able to happen.
  1778. */
  1779. raw_spin_unlock_irq_rcu_node(rnp);
  1780. return false;
  1781. }
  1782. /* Advance to a new grace period and initialize state. */
  1783. record_gp_stall_check_time(rsp);
  1784. /* Record GP times before starting GP, hence smp_store_release(). */
  1785. smp_store_release(&rsp->gpnum, rsp->gpnum + 1);
  1786. trace_rcu_grace_period(rsp->name, rsp->gpnum, TPS("start"));
  1787. raw_spin_unlock_irq_rcu_node(rnp);
  1788. /*
  1789. * Apply per-leaf buffered online and offline operations to the
  1790. * rcu_node tree. Note that this new grace period need not wait
  1791. * for subsequent online CPUs, and that quiescent-state forcing
  1792. * will handle subsequent offline CPUs.
  1793. */
  1794. rcu_for_each_leaf_node(rsp, rnp) {
  1795. rcu_gp_slow(rsp, gp_preinit_delay);
  1796. raw_spin_lock_irq_rcu_node(rnp);
  1797. if (rnp->qsmaskinit == rnp->qsmaskinitnext &&
  1798. !rnp->wait_blkd_tasks) {
  1799. /* Nothing to do on this leaf rcu_node structure. */
  1800. raw_spin_unlock_irq_rcu_node(rnp);
  1801. continue;
  1802. }
  1803. /* Record old state, apply changes to ->qsmaskinit field. */
  1804. oldmask = rnp->qsmaskinit;
  1805. rnp->qsmaskinit = rnp->qsmaskinitnext;
  1806. /* If zero-ness of ->qsmaskinit changed, propagate up tree. */
  1807. if (!oldmask != !rnp->qsmaskinit) {
  1808. if (!oldmask) /* First online CPU for this rcu_node. */
  1809. rcu_init_new_rnp(rnp);
  1810. else if (rcu_preempt_has_tasks(rnp)) /* blocked tasks */
  1811. rnp->wait_blkd_tasks = true;
  1812. else /* Last offline CPU and can propagate. */
  1813. rcu_cleanup_dead_rnp(rnp);
  1814. }
  1815. /*
  1816. * If all waited-on tasks from prior grace period are
  1817. * done, and if all this rcu_node structure's CPUs are
  1818. * still offline, propagate up the rcu_node tree and
  1819. * clear ->wait_blkd_tasks. Otherwise, if one of this
  1820. * rcu_node structure's CPUs has since come back online,
  1821. * simply clear ->wait_blkd_tasks (but rcu_cleanup_dead_rnp()
  1822. * checks for this, so just call it unconditionally).
  1823. */
  1824. if (rnp->wait_blkd_tasks &&
  1825. (!rcu_preempt_has_tasks(rnp) ||
  1826. rnp->qsmaskinit)) {
  1827. rnp->wait_blkd_tasks = false;
  1828. rcu_cleanup_dead_rnp(rnp);
  1829. }
  1830. raw_spin_unlock_irq_rcu_node(rnp);
  1831. }
  1832. /*
  1833. * Set the quiescent-state-needed bits in all the rcu_node
  1834. * structures for all currently online CPUs in breadth-first order,
  1835. * starting from the root rcu_node structure, relying on the layout
  1836. * of the tree within the rsp->node[] array. Note that other CPUs
  1837. * will access only the leaves of the hierarchy, thus seeing that no
  1838. * grace period is in progress, at least until the corresponding
  1839. * leaf node has been initialized.
  1840. *
  1841. * The grace period cannot complete until the initialization
  1842. * process finishes, because this kthread handles both.
  1843. */
  1844. rcu_for_each_node_breadth_first(rsp, rnp) {
  1845. rcu_gp_slow(rsp, gp_init_delay);
  1846. raw_spin_lock_irq_rcu_node(rnp);
  1847. rdp = this_cpu_ptr(rsp->rda);
  1848. rcu_preempt_check_blocked_tasks(rnp);
  1849. rnp->qsmask = rnp->qsmaskinit;
  1850. WRITE_ONCE(rnp->gpnum, rsp->gpnum);
  1851. if (WARN_ON_ONCE(rnp->completed != rsp->completed))
  1852. WRITE_ONCE(rnp->completed, rsp->completed);
  1853. if (rnp == rdp->mynode)
  1854. (void)__note_gp_changes(rsp, rnp, rdp);
  1855. rcu_preempt_boost_start_gp(rnp);
  1856. trace_rcu_grace_period_init(rsp->name, rnp->gpnum,
  1857. rnp->level, rnp->grplo,
  1858. rnp->grphi, rnp->qsmask);
  1859. raw_spin_unlock_irq_rcu_node(rnp);
  1860. cond_resched_rcu_qs();
  1861. WRITE_ONCE(rsp->gp_activity, jiffies);
  1862. }
  1863. return true;
  1864. }
  1865. /*
  1866. * Helper function for swait_event_idle() wakeup at force-quiescent-state
  1867. * time.
  1868. */
  1869. static bool rcu_gp_fqs_check_wake(struct rcu_state *rsp, int *gfp)
  1870. {
  1871. struct rcu_node *rnp = rcu_get_root(rsp);
  1872. /* Someone like call_rcu() requested a force-quiescent-state scan. */
  1873. *gfp = READ_ONCE(rsp->gp_flags);
  1874. if (*gfp & RCU_GP_FLAG_FQS)
  1875. return true;
  1876. /* The current grace period has completed. */
  1877. if (!READ_ONCE(rnp->qsmask) && !rcu_preempt_blocked_readers_cgp(rnp))
  1878. return true;
  1879. return false;
  1880. }
  1881. /*
  1882. * Do one round of quiescent-state forcing.
  1883. */
  1884. static void rcu_gp_fqs(struct rcu_state *rsp, bool first_time)
  1885. {
  1886. struct rcu_node *rnp = rcu_get_root(rsp);
  1887. WRITE_ONCE(rsp->gp_activity, jiffies);
  1888. rsp->n_force_qs++;
  1889. if (first_time) {
  1890. /* Collect dyntick-idle snapshots. */
  1891. force_qs_rnp(rsp, dyntick_save_progress_counter);
  1892. } else {
  1893. /* Handle dyntick-idle and offline CPUs. */
  1894. force_qs_rnp(rsp, rcu_implicit_dynticks_qs);
  1895. }
  1896. /* Clear flag to prevent immediate re-entry. */
  1897. if (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
  1898. raw_spin_lock_irq_rcu_node(rnp);
  1899. WRITE_ONCE(rsp->gp_flags,
  1900. READ_ONCE(rsp->gp_flags) & ~RCU_GP_FLAG_FQS);
  1901. raw_spin_unlock_irq_rcu_node(rnp);
  1902. }
  1903. }
  1904. /*
  1905. * Clean up after the old grace period.
  1906. */
  1907. static void rcu_gp_cleanup(struct rcu_state *rsp)
  1908. {
  1909. unsigned long gp_duration;
  1910. bool needgp = false;
  1911. int nocb = 0;
  1912. struct rcu_data *rdp;
  1913. struct rcu_node *rnp = rcu_get_root(rsp);
  1914. struct swait_queue_head *sq;
  1915. WRITE_ONCE(rsp->gp_activity, jiffies);
  1916. raw_spin_lock_irq_rcu_node(rnp);
  1917. gp_duration = jiffies - rsp->gp_start;
  1918. if (gp_duration > rsp->gp_max)
  1919. rsp->gp_max = gp_duration;
  1920. /*
  1921. * We know the grace period is complete, but to everyone else
  1922. * it appears to still be ongoing. But it is also the case
  1923. * that to everyone else it looks like there is nothing that
  1924. * they can do to advance the grace period. It is therefore
  1925. * safe for us to drop the lock in order to mark the grace
  1926. * period as completed in all of the rcu_node structures.
  1927. */
  1928. raw_spin_unlock_irq_rcu_node(rnp);
  1929. /*
  1930. * Propagate new ->completed value to rcu_node structures so
  1931. * that other CPUs don't have to wait until the start of the next
  1932. * grace period to process their callbacks. This also avoids
  1933. * some nasty RCU grace-period initialization races by forcing
  1934. * the end of the current grace period to be completely recorded in
  1935. * all of the rcu_node structures before the beginning of the next
  1936. * grace period is recorded in any of the rcu_node structures.
  1937. */
  1938. rcu_for_each_node_breadth_first(rsp, rnp) {
  1939. raw_spin_lock_irq_rcu_node(rnp);
  1940. WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp));
  1941. WARN_ON_ONCE(rnp->qsmask);
  1942. WRITE_ONCE(rnp->completed, rsp->gpnum);
  1943. rdp = this_cpu_ptr(rsp->rda);
  1944. if (rnp == rdp->mynode)
  1945. needgp = __note_gp_changes(rsp, rnp, rdp) || needgp;
  1946. /* smp_mb() provided by prior unlock-lock pair. */
  1947. nocb += rcu_future_gp_cleanup(rsp, rnp);
  1948. sq = rcu_nocb_gp_get(rnp);
  1949. raw_spin_unlock_irq_rcu_node(rnp);
  1950. rcu_nocb_gp_cleanup(sq);
  1951. cond_resched_rcu_qs();
  1952. WRITE_ONCE(rsp->gp_activity, jiffies);
  1953. rcu_gp_slow(rsp, gp_cleanup_delay);
  1954. }
  1955. rnp = rcu_get_root(rsp);
  1956. raw_spin_lock_irq_rcu_node(rnp); /* Order GP before ->completed update. */
  1957. rcu_nocb_gp_set(rnp, nocb);
  1958. /* Declare grace period done. */
  1959. WRITE_ONCE(rsp->completed, rsp->gpnum);
  1960. trace_rcu_grace_period(rsp->name, rsp->completed, TPS("end"));
  1961. rsp->gp_state = RCU_GP_IDLE;
  1962. rdp = this_cpu_ptr(rsp->rda);
  1963. /* Advance CBs to reduce false positives below. */
  1964. needgp = rcu_advance_cbs(rsp, rnp, rdp) || needgp;
  1965. if (needgp || cpu_needs_another_gp(rsp, rdp)) {
  1966. WRITE_ONCE(rsp->gp_flags, RCU_GP_FLAG_INIT);
  1967. trace_rcu_grace_period(rsp->name,
  1968. READ_ONCE(rsp->gpnum),
  1969. TPS("newreq"));
  1970. }
  1971. raw_spin_unlock_irq_rcu_node(rnp);
  1972. }
  1973. /*
  1974. * Body of kthread that handles grace periods.
  1975. */
  1976. static int __noreturn rcu_gp_kthread(void *arg)
  1977. {
  1978. bool first_gp_fqs;
  1979. int gf;
  1980. unsigned long j;
  1981. int ret;
  1982. struct rcu_state *rsp = arg;
  1983. struct rcu_node *rnp = rcu_get_root(rsp);
  1984. rcu_bind_gp_kthread();
  1985. for (;;) {
  1986. /* Handle grace-period start. */
  1987. for (;;) {
  1988. trace_rcu_grace_period(rsp->name,
  1989. READ_ONCE(rsp->gpnum),
  1990. TPS("reqwait"));
  1991. rsp->gp_state = RCU_GP_WAIT_GPS;
  1992. swait_event_idle(rsp->gp_wq, READ_ONCE(rsp->gp_flags) &
  1993. RCU_GP_FLAG_INIT);
  1994. rsp->gp_state = RCU_GP_DONE_GPS;
  1995. /* Locking provides needed memory barrier. */
  1996. if (rcu_gp_init(rsp))
  1997. break;
  1998. cond_resched_rcu_qs();
  1999. WRITE_ONCE(rsp->gp_activity, jiffies);
  2000. WARN_ON(signal_pending(current));
  2001. trace_rcu_grace_period(rsp->name,
  2002. READ_ONCE(rsp->gpnum),
  2003. TPS("reqwaitsig"));
  2004. }
  2005. /* Handle quiescent-state forcing. */
  2006. first_gp_fqs = true;
  2007. j = jiffies_till_first_fqs;
  2008. if (j > HZ) {
  2009. j = HZ;
  2010. jiffies_till_first_fqs = HZ;
  2011. }
  2012. ret = 0;
  2013. for (;;) {
  2014. if (!ret) {
  2015. rsp->jiffies_force_qs = jiffies + j;
  2016. WRITE_ONCE(rsp->jiffies_kick_kthreads,
  2017. jiffies + 3 * j);
  2018. }
  2019. trace_rcu_grace_period(rsp->name,
  2020. READ_ONCE(rsp->gpnum),
  2021. TPS("fqswait"));
  2022. rsp->gp_state = RCU_GP_WAIT_FQS;
  2023. ret = swait_event_idle_timeout(rsp->gp_wq,
  2024. rcu_gp_fqs_check_wake(rsp, &gf), j);
  2025. rsp->gp_state = RCU_GP_DOING_FQS;
  2026. /* Locking provides needed memory barriers. */
  2027. /* If grace period done, leave loop. */
  2028. if (!READ_ONCE(rnp->qsmask) &&
  2029. !rcu_preempt_blocked_readers_cgp(rnp))
  2030. break;
  2031. /* If time for quiescent-state forcing, do it. */
  2032. if (ULONG_CMP_GE(jiffies, rsp->jiffies_force_qs) ||
  2033. (gf & RCU_GP_FLAG_FQS)) {
  2034. trace_rcu_grace_period(rsp->name,
  2035. READ_ONCE(rsp->gpnum),
  2036. TPS("fqsstart"));
  2037. rcu_gp_fqs(rsp, first_gp_fqs);
  2038. first_gp_fqs = false;
  2039. trace_rcu_grace_period(rsp->name,
  2040. READ_ONCE(rsp->gpnum),
  2041. TPS("fqsend"));
  2042. cond_resched_rcu_qs();
  2043. WRITE_ONCE(rsp->gp_activity, jiffies);
  2044. ret = 0; /* Force full wait till next FQS. */
  2045. j = jiffies_till_next_fqs;
  2046. if (j > HZ) {
  2047. j = HZ;
  2048. jiffies_till_next_fqs = HZ;
  2049. } else if (j < 1) {
  2050. j = 1;
  2051. jiffies_till_next_fqs = 1;
  2052. }
  2053. } else {
  2054. /* Deal with stray signal. */
  2055. cond_resched_rcu_qs();
  2056. WRITE_ONCE(rsp->gp_activity, jiffies);
  2057. WARN_ON(signal_pending(current));
  2058. trace_rcu_grace_period(rsp->name,
  2059. READ_ONCE(rsp->gpnum),
  2060. TPS("fqswaitsig"));
  2061. ret = 1; /* Keep old FQS timing. */
  2062. j = jiffies;
  2063. if (time_after(jiffies, rsp->jiffies_force_qs))
  2064. j = 1;
  2065. else
  2066. j = rsp->jiffies_force_qs - j;
  2067. }
  2068. }
  2069. /* Handle grace-period end. */
  2070. rsp->gp_state = RCU_GP_CLEANUP;
  2071. rcu_gp_cleanup(rsp);
  2072. rsp->gp_state = RCU_GP_CLEANED;
  2073. }
  2074. }
  2075. /*
  2076. * Start a new RCU grace period if warranted, re-initializing the hierarchy
  2077. * in preparation for detecting the next grace period. The caller must hold
  2078. * the root node's ->lock and hard irqs must be disabled.
  2079. *
  2080. * Note that it is legal for a dying CPU (which is marked as offline) to
  2081. * invoke this function. This can happen when the dying CPU reports its
  2082. * quiescent state.
  2083. *
  2084. * Returns true if the grace-period kthread must be awakened.
  2085. */
  2086. static bool
  2087. rcu_start_gp_advanced(struct rcu_state *rsp, struct rcu_node *rnp,
  2088. struct rcu_data *rdp)
  2089. {
  2090. raw_lockdep_assert_held_rcu_node(rnp);
  2091. if (!rsp->gp_kthread || !cpu_needs_another_gp(rsp, rdp)) {
  2092. /*
  2093. * Either we have not yet spawned the grace-period
  2094. * task, this CPU does not need another grace period,
  2095. * or a grace period is already in progress.
  2096. * Either way, don't start a new grace period.
  2097. */
  2098. return false;
  2099. }
  2100. WRITE_ONCE(rsp->gp_flags, RCU_GP_FLAG_INIT);
  2101. trace_rcu_grace_period(rsp->name, READ_ONCE(rsp->gpnum),
  2102. TPS("newreq"));
  2103. /*
  2104. * We can't do wakeups while holding the rnp->lock, as that
  2105. * could cause possible deadlocks with the rq->lock. Defer
  2106. * the wakeup to our caller.
  2107. */
  2108. return true;
  2109. }
  2110. /*
  2111. * Similar to rcu_start_gp_advanced(), but also advance the calling CPU's
  2112. * callbacks. Note that rcu_start_gp_advanced() cannot do this because it
  2113. * is invoked indirectly from rcu_advance_cbs(), which would result in
  2114. * endless recursion -- or would do so if it wasn't for the self-deadlock
  2115. * that is encountered beforehand.
  2116. *
  2117. * Returns true if the grace-period kthread needs to be awakened.
  2118. */
  2119. static bool rcu_start_gp(struct rcu_state *rsp)
  2120. {
  2121. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  2122. struct rcu_node *rnp = rcu_get_root(rsp);
  2123. bool ret = false;
  2124. /*
  2125. * If there is no grace period in progress right now, any
  2126. * callbacks we have up to this point will be satisfied by the
  2127. * next grace period. Also, advancing the callbacks reduces the
  2128. * probability of false positives from cpu_needs_another_gp()
  2129. * resulting in pointless grace periods. So, advance callbacks
  2130. * then start the grace period!
  2131. */
  2132. ret = rcu_advance_cbs(rsp, rnp, rdp) || ret;
  2133. ret = rcu_start_gp_advanced(rsp, rnp, rdp) || ret;
  2134. return ret;
  2135. }
  2136. /*
  2137. * Report a full set of quiescent states to the specified rcu_state data
  2138. * structure. Invoke rcu_gp_kthread_wake() to awaken the grace-period
  2139. * kthread if another grace period is required. Whether we wake
  2140. * the grace-period kthread or it awakens itself for the next round
  2141. * of quiescent-state forcing, that kthread will clean up after the
  2142. * just-completed grace period. Note that the caller must hold rnp->lock,
  2143. * which is released before return.
  2144. */
  2145. static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
  2146. __releases(rcu_get_root(rsp)->lock)
  2147. {
  2148. raw_lockdep_assert_held_rcu_node(rcu_get_root(rsp));
  2149. WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
  2150. WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
  2151. raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(rsp), flags);
  2152. rcu_gp_kthread_wake(rsp);
  2153. }
  2154. /*
  2155. * Similar to rcu_report_qs_rdp(), for which it is a helper function.
  2156. * Allows quiescent states for a group of CPUs to be reported at one go
  2157. * to the specified rcu_node structure, though all the CPUs in the group
  2158. * must be represented by the same rcu_node structure (which need not be a
  2159. * leaf rcu_node structure, though it often will be). The gps parameter
  2160. * is the grace-period snapshot, which means that the quiescent states
  2161. * are valid only if rnp->gpnum is equal to gps. That structure's lock
  2162. * must be held upon entry, and it is released before return.
  2163. */
  2164. static void
  2165. rcu_report_qs_rnp(unsigned long mask, struct rcu_state *rsp,
  2166. struct rcu_node *rnp, unsigned long gps, unsigned long flags)
  2167. __releases(rnp->lock)
  2168. {
  2169. unsigned long oldmask = 0;
  2170. struct rcu_node *rnp_c;
  2171. raw_lockdep_assert_held_rcu_node(rnp);
  2172. /* Walk up the rcu_node hierarchy. */
  2173. for (;;) {
  2174. if (!(rnp->qsmask & mask) || rnp->gpnum != gps) {
  2175. /*
  2176. * Our bit has already been cleared, or the
  2177. * relevant grace period is already over, so done.
  2178. */
  2179. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2180. return;
  2181. }
  2182. WARN_ON_ONCE(oldmask); /* Any child must be all zeroed! */
  2183. WARN_ON_ONCE(rnp->level != rcu_num_lvls - 1 &&
  2184. rcu_preempt_blocked_readers_cgp(rnp));
  2185. rnp->qsmask &= ~mask;
  2186. trace_rcu_quiescent_state_report(rsp->name, rnp->gpnum,
  2187. mask, rnp->qsmask, rnp->level,
  2188. rnp->grplo, rnp->grphi,
  2189. !!rnp->gp_tasks);
  2190. if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  2191. /* Other bits still set at this level, so done. */
  2192. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2193. return;
  2194. }
  2195. mask = rnp->grpmask;
  2196. if (rnp->parent == NULL) {
  2197. /* No more levels. Exit loop holding root lock. */
  2198. break;
  2199. }
  2200. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2201. rnp_c = rnp;
  2202. rnp = rnp->parent;
  2203. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  2204. oldmask = rnp_c->qsmask;
  2205. }
  2206. /*
  2207. * Get here if we are the last CPU to pass through a quiescent
  2208. * state for this grace period. Invoke rcu_report_qs_rsp()
  2209. * to clean up and start the next grace period if one is needed.
  2210. */
  2211. rcu_report_qs_rsp(rsp, flags); /* releases rnp->lock. */
  2212. }
  2213. /*
  2214. * Record a quiescent state for all tasks that were previously queued
  2215. * on the specified rcu_node structure and that were blocking the current
  2216. * RCU grace period. The caller must hold the specified rnp->lock with
  2217. * irqs disabled, and this lock is released upon return, but irqs remain
  2218. * disabled.
  2219. */
  2220. static void rcu_report_unblock_qs_rnp(struct rcu_state *rsp,
  2221. struct rcu_node *rnp, unsigned long flags)
  2222. __releases(rnp->lock)
  2223. {
  2224. unsigned long gps;
  2225. unsigned long mask;
  2226. struct rcu_node *rnp_p;
  2227. raw_lockdep_assert_held_rcu_node(rnp);
  2228. if (rcu_state_p == &rcu_sched_state || rsp != rcu_state_p ||
  2229. rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  2230. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2231. return; /* Still need more quiescent states! */
  2232. }
  2233. rnp_p = rnp->parent;
  2234. if (rnp_p == NULL) {
  2235. /*
  2236. * Only one rcu_node structure in the tree, so don't
  2237. * try to report up to its nonexistent parent!
  2238. */
  2239. rcu_report_qs_rsp(rsp, flags);
  2240. return;
  2241. }
  2242. /* Report up the rest of the hierarchy, tracking current ->gpnum. */
  2243. gps = rnp->gpnum;
  2244. mask = rnp->grpmask;
  2245. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  2246. raw_spin_lock_rcu_node(rnp_p); /* irqs already disabled. */
  2247. rcu_report_qs_rnp(mask, rsp, rnp_p, gps, flags);
  2248. }
  2249. /*
  2250. * Record a quiescent state for the specified CPU to that CPU's rcu_data
  2251. * structure. This must be called from the specified CPU.
  2252. */
  2253. static void
  2254. rcu_report_qs_rdp(int cpu, struct rcu_state *rsp, struct rcu_data *rdp)
  2255. {
  2256. unsigned long flags;
  2257. unsigned long mask;
  2258. bool needwake;
  2259. struct rcu_node *rnp;
  2260. rnp = rdp->mynode;
  2261. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  2262. if (rdp->cpu_no_qs.b.norm || rdp->gpnum != rnp->gpnum ||
  2263. rnp->completed == rnp->gpnum || rdp->gpwrap) {
  2264. /*
  2265. * The grace period in which this quiescent state was
  2266. * recorded has ended, so don't report it upwards.
  2267. * We will instead need a new quiescent state that lies
  2268. * within the current grace period.
  2269. */
  2270. rdp->cpu_no_qs.b.norm = true; /* need qs for new gp. */
  2271. rdp->rcu_qs_ctr_snap = __this_cpu_read(rcu_dynticks.rcu_qs_ctr);
  2272. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2273. return;
  2274. }
  2275. mask = rdp->grpmask;
  2276. if ((rnp->qsmask & mask) == 0) {
  2277. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2278. } else {
  2279. rdp->core_needs_qs = false;
  2280. /*
  2281. * This GP can't end until cpu checks in, so all of our
  2282. * callbacks can be processed during the next GP.
  2283. */
  2284. needwake = rcu_accelerate_cbs(rsp, rnp, rdp);
  2285. rcu_report_qs_rnp(mask, rsp, rnp, rnp->gpnum, flags);
  2286. /* ^^^ Released rnp->lock */
  2287. if (needwake)
  2288. rcu_gp_kthread_wake(rsp);
  2289. }
  2290. }
  2291. /*
  2292. * Check to see if there is a new grace period of which this CPU
  2293. * is not yet aware, and if so, set up local rcu_data state for it.
  2294. * Otherwise, see if this CPU has just passed through its first
  2295. * quiescent state for this grace period, and record that fact if so.
  2296. */
  2297. static void
  2298. rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
  2299. {
  2300. /* Check for grace-period ends and beginnings. */
  2301. note_gp_changes(rsp, rdp);
  2302. /*
  2303. * Does this CPU still need to do its part for current grace period?
  2304. * If no, return and let the other CPUs do their part as well.
  2305. */
  2306. if (!rdp->core_needs_qs)
  2307. return;
  2308. /*
  2309. * Was there a quiescent state since the beginning of the grace
  2310. * period? If no, then exit and wait for the next call.
  2311. */
  2312. if (rdp->cpu_no_qs.b.norm)
  2313. return;
  2314. /*
  2315. * Tell RCU we are done (but rcu_report_qs_rdp() will be the
  2316. * judge of that).
  2317. */
  2318. rcu_report_qs_rdp(rdp->cpu, rsp, rdp);
  2319. }
  2320. /*
  2321. * Trace the fact that this CPU is going offline.
  2322. */
  2323. static void rcu_cleanup_dying_cpu(struct rcu_state *rsp)
  2324. {
  2325. RCU_TRACE(unsigned long mask;)
  2326. RCU_TRACE(struct rcu_data *rdp = this_cpu_ptr(rsp->rda);)
  2327. RCU_TRACE(struct rcu_node *rnp = rdp->mynode;)
  2328. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
  2329. return;
  2330. RCU_TRACE(mask = rdp->grpmask;)
  2331. trace_rcu_grace_period(rsp->name,
  2332. rnp->gpnum + 1 - !!(rnp->qsmask & mask),
  2333. TPS("cpuofl"));
  2334. }
  2335. /*
  2336. * All CPUs for the specified rcu_node structure have gone offline,
  2337. * and all tasks that were preempted within an RCU read-side critical
  2338. * section while running on one of those CPUs have since exited their RCU
  2339. * read-side critical section. Some other CPU is reporting this fact with
  2340. * the specified rcu_node structure's ->lock held and interrupts disabled.
  2341. * This function therefore goes up the tree of rcu_node structures,
  2342. * clearing the corresponding bits in the ->qsmaskinit fields. Note that
  2343. * the leaf rcu_node structure's ->qsmaskinit field has already been
  2344. * updated
  2345. *
  2346. * This function does check that the specified rcu_node structure has
  2347. * all CPUs offline and no blocked tasks, so it is OK to invoke it
  2348. * prematurely. That said, invoking it after the fact will cost you
  2349. * a needless lock acquisition. So once it has done its work, don't
  2350. * invoke it again.
  2351. */
  2352. static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf)
  2353. {
  2354. long mask;
  2355. struct rcu_node *rnp = rnp_leaf;
  2356. raw_lockdep_assert_held_rcu_node(rnp);
  2357. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
  2358. rnp->qsmaskinit || rcu_preempt_has_tasks(rnp))
  2359. return;
  2360. for (;;) {
  2361. mask = rnp->grpmask;
  2362. rnp = rnp->parent;
  2363. if (!rnp)
  2364. break;
  2365. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  2366. rnp->qsmaskinit &= ~mask;
  2367. rnp->qsmask &= ~mask;
  2368. if (rnp->qsmaskinit) {
  2369. raw_spin_unlock_rcu_node(rnp);
  2370. /* irqs remain disabled. */
  2371. return;
  2372. }
  2373. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  2374. }
  2375. }
  2376. /*
  2377. * The CPU has been completely removed, and some other CPU is reporting
  2378. * this fact from process context. Do the remainder of the cleanup.
  2379. * There can only be one CPU hotplug operation at a time, so no need for
  2380. * explicit locking.
  2381. */
  2382. static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp)
  2383. {
  2384. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  2385. struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
  2386. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
  2387. return;
  2388. /* Adjust any no-longer-needed kthreads. */
  2389. rcu_boost_kthread_setaffinity(rnp, -1);
  2390. }
  2391. /*
  2392. * Invoke any RCU callbacks that have made it to the end of their grace
  2393. * period. Thottle as specified by rdp->blimit.
  2394. */
  2395. static void rcu_do_batch(struct rcu_state *rsp, struct rcu_data *rdp)
  2396. {
  2397. unsigned long flags;
  2398. struct rcu_head *rhp;
  2399. struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
  2400. long bl, count;
  2401. /* If no callbacks are ready, just return. */
  2402. if (!rcu_segcblist_ready_cbs(&rdp->cblist)) {
  2403. trace_rcu_batch_start(rsp->name,
  2404. rcu_segcblist_n_lazy_cbs(&rdp->cblist),
  2405. rcu_segcblist_n_cbs(&rdp->cblist), 0);
  2406. trace_rcu_batch_end(rsp->name, 0,
  2407. !rcu_segcblist_empty(&rdp->cblist),
  2408. need_resched(), is_idle_task(current),
  2409. rcu_is_callbacks_kthread());
  2410. return;
  2411. }
  2412. /*
  2413. * Extract the list of ready callbacks, disabling to prevent
  2414. * races with call_rcu() from interrupt handlers. Leave the
  2415. * callback counts, as rcu_barrier() needs to be conservative.
  2416. */
  2417. local_irq_save(flags);
  2418. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  2419. bl = rdp->blimit;
  2420. trace_rcu_batch_start(rsp->name, rcu_segcblist_n_lazy_cbs(&rdp->cblist),
  2421. rcu_segcblist_n_cbs(&rdp->cblist), bl);
  2422. rcu_segcblist_extract_done_cbs(&rdp->cblist, &rcl);
  2423. local_irq_restore(flags);
  2424. /* Invoke callbacks. */
  2425. rhp = rcu_cblist_dequeue(&rcl);
  2426. for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
  2427. debug_rcu_head_unqueue(rhp);
  2428. if (__rcu_reclaim(rsp->name, rhp))
  2429. rcu_cblist_dequeued_lazy(&rcl);
  2430. /*
  2431. * Stop only if limit reached and CPU has something to do.
  2432. * Note: The rcl structure counts down from zero.
  2433. */
  2434. if (-rcl.len >= bl &&
  2435. (need_resched() ||
  2436. (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
  2437. break;
  2438. }
  2439. local_irq_save(flags);
  2440. count = -rcl.len;
  2441. trace_rcu_batch_end(rsp->name, count, !!rcl.head, need_resched(),
  2442. is_idle_task(current), rcu_is_callbacks_kthread());
  2443. /* Update counts and requeue any remaining callbacks. */
  2444. rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl);
  2445. smp_mb(); /* List handling before counting for rcu_barrier(). */
  2446. rcu_segcblist_insert_count(&rdp->cblist, &rcl);
  2447. /* Reinstate batch limit if we have worked down the excess. */
  2448. count = rcu_segcblist_n_cbs(&rdp->cblist);
  2449. if (rdp->blimit == LONG_MAX && count <= qlowmark)
  2450. rdp->blimit = blimit;
  2451. /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
  2452. if (count == 0 && rdp->qlen_last_fqs_check != 0) {
  2453. rdp->qlen_last_fqs_check = 0;
  2454. rdp->n_force_qs_snap = rsp->n_force_qs;
  2455. } else if (count < rdp->qlen_last_fqs_check - qhimark)
  2456. rdp->qlen_last_fqs_check = count;
  2457. /*
  2458. * The following usually indicates a double call_rcu(). To track
  2459. * this down, try building with CONFIG_DEBUG_OBJECTS_RCU_HEAD=y.
  2460. */
  2461. WARN_ON_ONCE(rcu_segcblist_empty(&rdp->cblist) != (count == 0));
  2462. local_irq_restore(flags);
  2463. /* Re-invoke RCU core processing if there are callbacks remaining. */
  2464. if (rcu_segcblist_ready_cbs(&rdp->cblist))
  2465. invoke_rcu_core();
  2466. }
  2467. /*
  2468. * Check to see if this CPU is in a non-context-switch quiescent state
  2469. * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
  2470. * Also schedule RCU core processing.
  2471. *
  2472. * This function must be called from hardirq context. It is normally
  2473. * invoked from the scheduling-clock interrupt.
  2474. */
  2475. void rcu_check_callbacks(int user)
  2476. {
  2477. trace_rcu_utilization(TPS("Start scheduler-tick"));
  2478. increment_cpu_stall_ticks();
  2479. if (user || rcu_is_cpu_rrupt_from_idle()) {
  2480. /*
  2481. * Get here if this CPU took its interrupt from user
  2482. * mode or from the idle loop, and if this is not a
  2483. * nested interrupt. In this case, the CPU is in
  2484. * a quiescent state, so note it.
  2485. *
  2486. * No memory barrier is required here because both
  2487. * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
  2488. * variables that other CPUs neither access nor modify,
  2489. * at least not while the corresponding CPU is online.
  2490. */
  2491. rcu_sched_qs();
  2492. rcu_bh_qs();
  2493. } else if (!in_softirq()) {
  2494. /*
  2495. * Get here if this CPU did not take its interrupt from
  2496. * softirq, in other words, if it is not interrupting
  2497. * a rcu_bh read-side critical section. This is an _bh
  2498. * critical section, so note it.
  2499. */
  2500. rcu_bh_qs();
  2501. }
  2502. rcu_preempt_check_callbacks();
  2503. if (rcu_pending())
  2504. invoke_rcu_core();
  2505. if (user)
  2506. rcu_note_voluntary_context_switch(current);
  2507. trace_rcu_utilization(TPS("End scheduler-tick"));
  2508. }
  2509. /*
  2510. * Scan the leaf rcu_node structures, processing dyntick state for any that
  2511. * have not yet encountered a quiescent state, using the function specified.
  2512. * Also initiate boosting for any threads blocked on the root rcu_node.
  2513. *
  2514. * The caller must have suppressed start of new grace periods.
  2515. */
  2516. static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *rsp))
  2517. {
  2518. int cpu;
  2519. unsigned long flags;
  2520. unsigned long mask;
  2521. struct rcu_node *rnp;
  2522. rcu_for_each_leaf_node(rsp, rnp) {
  2523. cond_resched_rcu_qs();
  2524. mask = 0;
  2525. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  2526. if (rnp->qsmask == 0) {
  2527. if (rcu_state_p == &rcu_sched_state ||
  2528. rsp != rcu_state_p ||
  2529. rcu_preempt_blocked_readers_cgp(rnp)) {
  2530. /*
  2531. * No point in scanning bits because they
  2532. * are all zero. But we might need to
  2533. * priority-boost blocked readers.
  2534. */
  2535. rcu_initiate_boost(rnp, flags);
  2536. /* rcu_initiate_boost() releases rnp->lock */
  2537. continue;
  2538. }
  2539. if (rnp->parent &&
  2540. (rnp->parent->qsmask & rnp->grpmask)) {
  2541. /*
  2542. * Race between grace-period
  2543. * initialization and task exiting RCU
  2544. * read-side critical section: Report.
  2545. */
  2546. rcu_report_unblock_qs_rnp(rsp, rnp, flags);
  2547. /* rcu_report_unblock_qs_rnp() rlses ->lock */
  2548. continue;
  2549. }
  2550. }
  2551. for_each_leaf_node_possible_cpu(rnp, cpu) {
  2552. unsigned long bit = leaf_node_cpu_bit(rnp, cpu);
  2553. if ((rnp->qsmask & bit) != 0) {
  2554. if (f(per_cpu_ptr(rsp->rda, cpu)))
  2555. mask |= bit;
  2556. }
  2557. }
  2558. if (mask != 0) {
  2559. /* Idle/offline CPUs, report (releases rnp->lock. */
  2560. rcu_report_qs_rnp(mask, rsp, rnp, rnp->gpnum, flags);
  2561. } else {
  2562. /* Nothing to do here, so just drop the lock. */
  2563. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2564. }
  2565. }
  2566. }
  2567. /*
  2568. * Force quiescent states on reluctant CPUs, and also detect which
  2569. * CPUs are in dyntick-idle mode.
  2570. */
  2571. static void force_quiescent_state(struct rcu_state *rsp)
  2572. {
  2573. unsigned long flags;
  2574. bool ret;
  2575. struct rcu_node *rnp;
  2576. struct rcu_node *rnp_old = NULL;
  2577. /* Funnel through hierarchy to reduce memory contention. */
  2578. rnp = __this_cpu_read(rsp->rda->mynode);
  2579. for (; rnp != NULL; rnp = rnp->parent) {
  2580. ret = (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) ||
  2581. !raw_spin_trylock(&rnp->fqslock);
  2582. if (rnp_old != NULL)
  2583. raw_spin_unlock(&rnp_old->fqslock);
  2584. if (ret)
  2585. return;
  2586. rnp_old = rnp;
  2587. }
  2588. /* rnp_old == rcu_get_root(rsp), rnp == NULL. */
  2589. /* Reached the root of the rcu_node tree, acquire lock. */
  2590. raw_spin_lock_irqsave_rcu_node(rnp_old, flags);
  2591. raw_spin_unlock(&rnp_old->fqslock);
  2592. if (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
  2593. raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
  2594. return; /* Someone beat us to it. */
  2595. }
  2596. WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
  2597. raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
  2598. rcu_gp_kthread_wake(rsp);
  2599. }
  2600. /*
  2601. * This does the RCU core processing work for the specified rcu_state
  2602. * and rcu_data structures. This may be called only from the CPU to
  2603. * whom the rdp belongs.
  2604. */
  2605. static void
  2606. __rcu_process_callbacks(struct rcu_state *rsp)
  2607. {
  2608. unsigned long flags;
  2609. bool needwake;
  2610. struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
  2611. WARN_ON_ONCE(!rdp->beenonline);
  2612. /* Update RCU state based on any recent quiescent states. */
  2613. rcu_check_quiescent_state(rsp, rdp);
  2614. /* Does this CPU require a not-yet-started grace period? */
  2615. local_irq_save(flags);
  2616. if (cpu_needs_another_gp(rsp, rdp)) {
  2617. raw_spin_lock_rcu_node(rcu_get_root(rsp)); /* irqs disabled. */
  2618. needwake = rcu_start_gp(rsp);
  2619. raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(rsp), flags);
  2620. if (needwake)
  2621. rcu_gp_kthread_wake(rsp);
  2622. } else {
  2623. local_irq_restore(flags);
  2624. }
  2625. /* If there are callbacks ready, invoke them. */
  2626. if (rcu_segcblist_ready_cbs(&rdp->cblist))
  2627. invoke_rcu_callbacks(rsp, rdp);
  2628. /* Do any needed deferred wakeups of rcuo kthreads. */
  2629. do_nocb_deferred_wakeup(rdp);
  2630. }
  2631. /*
  2632. * Do RCU core processing for the current CPU.
  2633. */
  2634. static __latent_entropy void rcu_process_callbacks(struct softirq_action *unused)
  2635. {
  2636. struct rcu_state *rsp;
  2637. if (cpu_is_offline(smp_processor_id()))
  2638. return;
  2639. trace_rcu_utilization(TPS("Start RCU core"));
  2640. for_each_rcu_flavor(rsp)
  2641. __rcu_process_callbacks(rsp);
  2642. trace_rcu_utilization(TPS("End RCU core"));
  2643. }
  2644. /*
  2645. * Schedule RCU callback invocation. If the specified type of RCU
  2646. * does not support RCU priority boosting, just do a direct call,
  2647. * otherwise wake up the per-CPU kernel kthread. Note that because we
  2648. * are running on the current CPU with softirqs disabled, the
  2649. * rcu_cpu_kthread_task cannot disappear out from under us.
  2650. */
  2651. static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
  2652. {
  2653. if (unlikely(!READ_ONCE(rcu_scheduler_fully_active)))
  2654. return;
  2655. if (likely(!rsp->boost)) {
  2656. rcu_do_batch(rsp, rdp);
  2657. return;
  2658. }
  2659. invoke_rcu_callbacks_kthread();
  2660. }
  2661. static void invoke_rcu_core(void)
  2662. {
  2663. if (cpu_online(smp_processor_id()))
  2664. raise_softirq(RCU_SOFTIRQ);
  2665. }
  2666. /*
  2667. * Handle any core-RCU processing required by a call_rcu() invocation.
  2668. */
  2669. static void __call_rcu_core(struct rcu_state *rsp, struct rcu_data *rdp,
  2670. struct rcu_head *head, unsigned long flags)
  2671. {
  2672. bool needwake;
  2673. /*
  2674. * If called from an extended quiescent state, invoke the RCU
  2675. * core in order to force a re-evaluation of RCU's idleness.
  2676. */
  2677. if (!rcu_is_watching())
  2678. invoke_rcu_core();
  2679. /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
  2680. if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
  2681. return;
  2682. /*
  2683. * Force the grace period if too many callbacks or too long waiting.
  2684. * Enforce hysteresis, and don't invoke force_quiescent_state()
  2685. * if some other CPU has recently done so. Also, don't bother
  2686. * invoking force_quiescent_state() if the newly enqueued callback
  2687. * is the only one waiting for a grace period to complete.
  2688. */
  2689. if (unlikely(rcu_segcblist_n_cbs(&rdp->cblist) >
  2690. rdp->qlen_last_fqs_check + qhimark)) {
  2691. /* Are we ignoring a completed grace period? */
  2692. note_gp_changes(rsp, rdp);
  2693. /* Start a new grace period if one not already started. */
  2694. if (!rcu_gp_in_progress(rsp)) {
  2695. struct rcu_node *rnp_root = rcu_get_root(rsp);
  2696. raw_spin_lock_rcu_node(rnp_root);
  2697. needwake = rcu_start_gp(rsp);
  2698. raw_spin_unlock_rcu_node(rnp_root);
  2699. if (needwake)
  2700. rcu_gp_kthread_wake(rsp);
  2701. } else {
  2702. /* Give the grace period a kick. */
  2703. rdp->blimit = LONG_MAX;
  2704. if (rsp->n_force_qs == rdp->n_force_qs_snap &&
  2705. rcu_segcblist_first_pend_cb(&rdp->cblist) != head)
  2706. force_quiescent_state(rsp);
  2707. rdp->n_force_qs_snap = rsp->n_force_qs;
  2708. rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
  2709. }
  2710. }
  2711. }
  2712. /*
  2713. * RCU callback function to leak a callback.
  2714. */
  2715. static void rcu_leak_callback(struct rcu_head *rhp)
  2716. {
  2717. }
  2718. /*
  2719. * Helper function for call_rcu() and friends. The cpu argument will
  2720. * normally be -1, indicating "currently running CPU". It may specify
  2721. * a CPU only if that CPU is a no-CBs CPU. Currently, only _rcu_barrier()
  2722. * is expected to specify a CPU.
  2723. */
  2724. static void
  2725. __call_rcu(struct rcu_head *head, rcu_callback_t func,
  2726. struct rcu_state *rsp, int cpu, bool lazy)
  2727. {
  2728. unsigned long flags;
  2729. struct rcu_data *rdp;
  2730. /* Misaligned rcu_head! */
  2731. WARN_ON_ONCE((unsigned long)head & (sizeof(void *) - 1));
  2732. if (debug_rcu_head_queue(head)) {
  2733. /*
  2734. * Probable double call_rcu(), so leak the callback.
  2735. * Use rcu:rcu_callback trace event to find the previous
  2736. * time callback was passed to __call_rcu().
  2737. */
  2738. WARN_ONCE(1, "__call_rcu(): Double-freed CB %p->%pF()!!!\n",
  2739. head, head->func);
  2740. WRITE_ONCE(head->func, rcu_leak_callback);
  2741. return;
  2742. }
  2743. head->func = func;
  2744. head->next = NULL;
  2745. local_irq_save(flags);
  2746. rdp = this_cpu_ptr(rsp->rda);
  2747. /* Add the callback to our list. */
  2748. if (unlikely(!rcu_segcblist_is_enabled(&rdp->cblist)) || cpu != -1) {
  2749. int offline;
  2750. if (cpu != -1)
  2751. rdp = per_cpu_ptr(rsp->rda, cpu);
  2752. if (likely(rdp->mynode)) {
  2753. /* Post-boot, so this should be for a no-CBs CPU. */
  2754. offline = !__call_rcu_nocb(rdp, head, lazy, flags);
  2755. WARN_ON_ONCE(offline);
  2756. /* Offline CPU, _call_rcu() illegal, leak callback. */
  2757. local_irq_restore(flags);
  2758. return;
  2759. }
  2760. /*
  2761. * Very early boot, before rcu_init(). Initialize if needed
  2762. * and then drop through to queue the callback.
  2763. */
  2764. BUG_ON(cpu != -1);
  2765. WARN_ON_ONCE(!rcu_is_watching());
  2766. if (rcu_segcblist_empty(&rdp->cblist))
  2767. rcu_segcblist_init(&rdp->cblist);
  2768. }
  2769. rcu_segcblist_enqueue(&rdp->cblist, head, lazy);
  2770. if (!lazy)
  2771. rcu_idle_count_callbacks_posted();
  2772. if (__is_kfree_rcu_offset((unsigned long)func))
  2773. trace_rcu_kfree_callback(rsp->name, head, (unsigned long)func,
  2774. rcu_segcblist_n_lazy_cbs(&rdp->cblist),
  2775. rcu_segcblist_n_cbs(&rdp->cblist));
  2776. else
  2777. trace_rcu_callback(rsp->name, head,
  2778. rcu_segcblist_n_lazy_cbs(&rdp->cblist),
  2779. rcu_segcblist_n_cbs(&rdp->cblist));
  2780. /* Go handle any RCU core processing required. */
  2781. __call_rcu_core(rsp, rdp, head, flags);
  2782. local_irq_restore(flags);
  2783. }
  2784. /**
  2785. * call_rcu_sched() - Queue an RCU for invocation after sched grace period.
  2786. * @head: structure to be used for queueing the RCU updates.
  2787. * @func: actual callback function to be invoked after the grace period
  2788. *
  2789. * The callback function will be invoked some time after a full grace
  2790. * period elapses, in other words after all currently executing RCU
  2791. * read-side critical sections have completed. call_rcu_sched() assumes
  2792. * that the read-side critical sections end on enabling of preemption
  2793. * or on voluntary preemption.
  2794. * RCU read-side critical sections are delimited by:
  2795. *
  2796. * - rcu_read_lock_sched() and rcu_read_unlock_sched(), OR
  2797. * - anything that disables preemption.
  2798. *
  2799. * These may be nested.
  2800. *
  2801. * See the description of call_rcu() for more detailed information on
  2802. * memory ordering guarantees.
  2803. */
  2804. void call_rcu_sched(struct rcu_head *head, rcu_callback_t func)
  2805. {
  2806. __call_rcu(head, func, &rcu_sched_state, -1, 0);
  2807. }
  2808. EXPORT_SYMBOL_GPL(call_rcu_sched);
  2809. /**
  2810. * call_rcu_bh() - Queue an RCU for invocation after a quicker grace period.
  2811. * @head: structure to be used for queueing the RCU updates.
  2812. * @func: actual callback function to be invoked after the grace period
  2813. *
  2814. * The callback function will be invoked some time after a full grace
  2815. * period elapses, in other words after all currently executing RCU
  2816. * read-side critical sections have completed. call_rcu_bh() assumes
  2817. * that the read-side critical sections end on completion of a softirq
  2818. * handler. This means that read-side critical sections in process
  2819. * context must not be interrupted by softirqs. This interface is to be
  2820. * used when most of the read-side critical sections are in softirq context.
  2821. * RCU read-side critical sections are delimited by:
  2822. *
  2823. * - rcu_read_lock() and rcu_read_unlock(), if in interrupt context, OR
  2824. * - rcu_read_lock_bh() and rcu_read_unlock_bh(), if in process context.
  2825. *
  2826. * These may be nested.
  2827. *
  2828. * See the description of call_rcu() for more detailed information on
  2829. * memory ordering guarantees.
  2830. */
  2831. void call_rcu_bh(struct rcu_head *head, rcu_callback_t func)
  2832. {
  2833. __call_rcu(head, func, &rcu_bh_state, -1, 0);
  2834. }
  2835. EXPORT_SYMBOL_GPL(call_rcu_bh);
  2836. /*
  2837. * Queue an RCU callback for lazy invocation after a grace period.
  2838. * This will likely be later named something like "call_rcu_lazy()",
  2839. * but this change will require some way of tagging the lazy RCU
  2840. * callbacks in the list of pending callbacks. Until then, this
  2841. * function may only be called from __kfree_rcu().
  2842. */
  2843. void kfree_call_rcu(struct rcu_head *head,
  2844. rcu_callback_t func)
  2845. {
  2846. __call_rcu(head, func, rcu_state_p, -1, 1);
  2847. }
  2848. EXPORT_SYMBOL_GPL(kfree_call_rcu);
  2849. /*
  2850. * Because a context switch is a grace period for RCU-sched and RCU-bh,
  2851. * any blocking grace-period wait automatically implies a grace period
  2852. * if there is only one CPU online at any point time during execution
  2853. * of either synchronize_sched() or synchronize_rcu_bh(). It is OK to
  2854. * occasionally incorrectly indicate that there are multiple CPUs online
  2855. * when there was in fact only one the whole time, as this just adds
  2856. * some overhead: RCU still operates correctly.
  2857. */
  2858. static inline int rcu_blocking_is_gp(void)
  2859. {
  2860. int ret;
  2861. might_sleep(); /* Check for RCU read-side critical section. */
  2862. preempt_disable();
  2863. ret = num_online_cpus() <= 1;
  2864. preempt_enable();
  2865. return ret;
  2866. }
  2867. /**
  2868. * synchronize_sched - wait until an rcu-sched grace period has elapsed.
  2869. *
  2870. * Control will return to the caller some time after a full rcu-sched
  2871. * grace period has elapsed, in other words after all currently executing
  2872. * rcu-sched read-side critical sections have completed. These read-side
  2873. * critical sections are delimited by rcu_read_lock_sched() and
  2874. * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
  2875. * local_irq_disable(), and so on may be used in place of
  2876. * rcu_read_lock_sched().
  2877. *
  2878. * This means that all preempt_disable code sequences, including NMI and
  2879. * non-threaded hardware-interrupt handlers, in progress on entry will
  2880. * have completed before this primitive returns. However, this does not
  2881. * guarantee that softirq handlers will have completed, since in some
  2882. * kernels, these handlers can run in process context, and can block.
  2883. *
  2884. * Note that this guarantee implies further memory-ordering guarantees.
  2885. * On systems with more than one CPU, when synchronize_sched() returns,
  2886. * each CPU is guaranteed to have executed a full memory barrier since the
  2887. * end of its last RCU-sched read-side critical section whose beginning
  2888. * preceded the call to synchronize_sched(). In addition, each CPU having
  2889. * an RCU read-side critical section that extends beyond the return from
  2890. * synchronize_sched() is guaranteed to have executed a full memory barrier
  2891. * after the beginning of synchronize_sched() and before the beginning of
  2892. * that RCU read-side critical section. Note that these guarantees include
  2893. * CPUs that are offline, idle, or executing in user mode, as well as CPUs
  2894. * that are executing in the kernel.
  2895. *
  2896. * Furthermore, if CPU A invoked synchronize_sched(), which returned
  2897. * to its caller on CPU B, then both CPU A and CPU B are guaranteed
  2898. * to have executed a full memory barrier during the execution of
  2899. * synchronize_sched() -- even if CPU A and CPU B are the same CPU (but
  2900. * again only if the system has more than one CPU).
  2901. */
  2902. void synchronize_sched(void)
  2903. {
  2904. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
  2905. lock_is_held(&rcu_lock_map) ||
  2906. lock_is_held(&rcu_sched_lock_map),
  2907. "Illegal synchronize_sched() in RCU-sched read-side critical section");
  2908. if (rcu_blocking_is_gp())
  2909. return;
  2910. if (rcu_gp_is_expedited())
  2911. synchronize_sched_expedited();
  2912. else
  2913. wait_rcu_gp(call_rcu_sched);
  2914. }
  2915. EXPORT_SYMBOL_GPL(synchronize_sched);
  2916. /**
  2917. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  2918. *
  2919. * Control will return to the caller some time after a full rcu_bh grace
  2920. * period has elapsed, in other words after all currently executing rcu_bh
  2921. * read-side critical sections have completed. RCU read-side critical
  2922. * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
  2923. * and may be nested.
  2924. *
  2925. * See the description of synchronize_sched() for more detailed information
  2926. * on memory ordering guarantees.
  2927. */
  2928. void synchronize_rcu_bh(void)
  2929. {
  2930. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
  2931. lock_is_held(&rcu_lock_map) ||
  2932. lock_is_held(&rcu_sched_lock_map),
  2933. "Illegal synchronize_rcu_bh() in RCU-bh read-side critical section");
  2934. if (rcu_blocking_is_gp())
  2935. return;
  2936. if (rcu_gp_is_expedited())
  2937. synchronize_rcu_bh_expedited();
  2938. else
  2939. wait_rcu_gp(call_rcu_bh);
  2940. }
  2941. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  2942. /**
  2943. * get_state_synchronize_rcu - Snapshot current RCU state
  2944. *
  2945. * Returns a cookie that is used by a later call to cond_synchronize_rcu()
  2946. * to determine whether or not a full grace period has elapsed in the
  2947. * meantime.
  2948. */
  2949. unsigned long get_state_synchronize_rcu(void)
  2950. {
  2951. /*
  2952. * Any prior manipulation of RCU-protected data must happen
  2953. * before the load from ->gpnum.
  2954. */
  2955. smp_mb(); /* ^^^ */
  2956. /*
  2957. * Make sure this load happens before the purportedly
  2958. * time-consuming work between get_state_synchronize_rcu()
  2959. * and cond_synchronize_rcu().
  2960. */
  2961. return smp_load_acquire(&rcu_state_p->gpnum);
  2962. }
  2963. EXPORT_SYMBOL_GPL(get_state_synchronize_rcu);
  2964. /**
  2965. * cond_synchronize_rcu - Conditionally wait for an RCU grace period
  2966. *
  2967. * @oldstate: return value from earlier call to get_state_synchronize_rcu()
  2968. *
  2969. * If a full RCU grace period has elapsed since the earlier call to
  2970. * get_state_synchronize_rcu(), just return. Otherwise, invoke
  2971. * synchronize_rcu() to wait for a full grace period.
  2972. *
  2973. * Yes, this function does not take counter wrap into account. But
  2974. * counter wrap is harmless. If the counter wraps, we have waited for
  2975. * more than 2 billion grace periods (and way more on a 64-bit system!),
  2976. * so waiting for one additional grace period should be just fine.
  2977. */
  2978. void cond_synchronize_rcu(unsigned long oldstate)
  2979. {
  2980. unsigned long newstate;
  2981. /*
  2982. * Ensure that this load happens before any RCU-destructive
  2983. * actions the caller might carry out after we return.
  2984. */
  2985. newstate = smp_load_acquire(&rcu_state_p->completed);
  2986. if (ULONG_CMP_GE(oldstate, newstate))
  2987. synchronize_rcu();
  2988. }
  2989. EXPORT_SYMBOL_GPL(cond_synchronize_rcu);
  2990. /**
  2991. * get_state_synchronize_sched - Snapshot current RCU-sched state
  2992. *
  2993. * Returns a cookie that is used by a later call to cond_synchronize_sched()
  2994. * to determine whether or not a full grace period has elapsed in the
  2995. * meantime.
  2996. */
  2997. unsigned long get_state_synchronize_sched(void)
  2998. {
  2999. /*
  3000. * Any prior manipulation of RCU-protected data must happen
  3001. * before the load from ->gpnum.
  3002. */
  3003. smp_mb(); /* ^^^ */
  3004. /*
  3005. * Make sure this load happens before the purportedly
  3006. * time-consuming work between get_state_synchronize_sched()
  3007. * and cond_synchronize_sched().
  3008. */
  3009. return smp_load_acquire(&rcu_sched_state.gpnum);
  3010. }
  3011. EXPORT_SYMBOL_GPL(get_state_synchronize_sched);
  3012. /**
  3013. * cond_synchronize_sched - Conditionally wait for an RCU-sched grace period
  3014. *
  3015. * @oldstate: return value from earlier call to get_state_synchronize_sched()
  3016. *
  3017. * If a full RCU-sched grace period has elapsed since the earlier call to
  3018. * get_state_synchronize_sched(), just return. Otherwise, invoke
  3019. * synchronize_sched() to wait for a full grace period.
  3020. *
  3021. * Yes, this function does not take counter wrap into account. But
  3022. * counter wrap is harmless. If the counter wraps, we have waited for
  3023. * more than 2 billion grace periods (and way more on a 64-bit system!),
  3024. * so waiting for one additional grace period should be just fine.
  3025. */
  3026. void cond_synchronize_sched(unsigned long oldstate)
  3027. {
  3028. unsigned long newstate;
  3029. /*
  3030. * Ensure that this load happens before any RCU-destructive
  3031. * actions the caller might carry out after we return.
  3032. */
  3033. newstate = smp_load_acquire(&rcu_sched_state.completed);
  3034. if (ULONG_CMP_GE(oldstate, newstate))
  3035. synchronize_sched();
  3036. }
  3037. EXPORT_SYMBOL_GPL(cond_synchronize_sched);
  3038. /*
  3039. * Check to see if there is any immediate RCU-related work to be done
  3040. * by the current CPU, for the specified type of RCU, returning 1 if so.
  3041. * The checks are in order of increasing expense: checks that can be
  3042. * carried out against CPU-local state are performed first. However,
  3043. * we must check for CPU stalls first, else we might not get a chance.
  3044. */
  3045. static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
  3046. {
  3047. struct rcu_node *rnp = rdp->mynode;
  3048. /* Check for CPU stalls, if enabled. */
  3049. check_cpu_stall(rsp, rdp);
  3050. /* Is this CPU a NO_HZ_FULL CPU that should ignore RCU? */
  3051. if (rcu_nohz_full_cpu(rsp))
  3052. return 0;
  3053. /* Is the RCU core waiting for a quiescent state from this CPU? */
  3054. if (rdp->core_needs_qs && !rdp->cpu_no_qs.b.norm)
  3055. return 1;
  3056. /* Does this CPU have callbacks ready to invoke? */
  3057. if (rcu_segcblist_ready_cbs(&rdp->cblist))
  3058. return 1;
  3059. /* Has RCU gone idle with this CPU needing another grace period? */
  3060. if (cpu_needs_another_gp(rsp, rdp))
  3061. return 1;
  3062. /* Has another RCU grace period completed? */
  3063. if (READ_ONCE(rnp->completed) != rdp->completed) /* outside lock */
  3064. return 1;
  3065. /* Has a new RCU grace period started? */
  3066. if (READ_ONCE(rnp->gpnum) != rdp->gpnum ||
  3067. unlikely(READ_ONCE(rdp->gpwrap))) /* outside lock */
  3068. return 1;
  3069. /* Does this CPU need a deferred NOCB wakeup? */
  3070. if (rcu_nocb_need_deferred_wakeup(rdp))
  3071. return 1;
  3072. /* nothing to do */
  3073. return 0;
  3074. }
  3075. /*
  3076. * Check to see if there is any immediate RCU-related work to be done
  3077. * by the current CPU, returning 1 if so. This function is part of the
  3078. * RCU implementation; it is -not- an exported member of the RCU API.
  3079. */
  3080. static int rcu_pending(void)
  3081. {
  3082. struct rcu_state *rsp;
  3083. for_each_rcu_flavor(rsp)
  3084. if (__rcu_pending(rsp, this_cpu_ptr(rsp->rda)))
  3085. return 1;
  3086. return 0;
  3087. }
  3088. /*
  3089. * Return true if the specified CPU has any callback. If all_lazy is
  3090. * non-NULL, store an indication of whether all callbacks are lazy.
  3091. * (If there are no callbacks, all of them are deemed to be lazy.)
  3092. */
  3093. static bool __maybe_unused rcu_cpu_has_callbacks(bool *all_lazy)
  3094. {
  3095. bool al = true;
  3096. bool hc = false;
  3097. struct rcu_data *rdp;
  3098. struct rcu_state *rsp;
  3099. for_each_rcu_flavor(rsp) {
  3100. rdp = this_cpu_ptr(rsp->rda);
  3101. if (rcu_segcblist_empty(&rdp->cblist))
  3102. continue;
  3103. hc = true;
  3104. if (rcu_segcblist_n_nonlazy_cbs(&rdp->cblist) || !all_lazy) {
  3105. al = false;
  3106. break;
  3107. }
  3108. }
  3109. if (all_lazy)
  3110. *all_lazy = al;
  3111. return hc;
  3112. }
  3113. /*
  3114. * Helper function for _rcu_barrier() tracing. If tracing is disabled,
  3115. * the compiler is expected to optimize this away.
  3116. */
  3117. static void _rcu_barrier_trace(struct rcu_state *rsp, const char *s,
  3118. int cpu, unsigned long done)
  3119. {
  3120. trace_rcu_barrier(rsp->name, s, cpu,
  3121. atomic_read(&rsp->barrier_cpu_count), done);
  3122. }
  3123. /*
  3124. * RCU callback function for _rcu_barrier(). If we are last, wake
  3125. * up the task executing _rcu_barrier().
  3126. */
  3127. static void rcu_barrier_callback(struct rcu_head *rhp)
  3128. {
  3129. struct rcu_data *rdp = container_of(rhp, struct rcu_data, barrier_head);
  3130. struct rcu_state *rsp = rdp->rsp;
  3131. if (atomic_dec_and_test(&rsp->barrier_cpu_count)) {
  3132. _rcu_barrier_trace(rsp, TPS("LastCB"), -1,
  3133. rsp->barrier_sequence);
  3134. complete(&rsp->barrier_completion);
  3135. } else {
  3136. _rcu_barrier_trace(rsp, TPS("CB"), -1, rsp->barrier_sequence);
  3137. }
  3138. }
  3139. /*
  3140. * Called with preemption disabled, and from cross-cpu IRQ context.
  3141. */
  3142. static void rcu_barrier_func(void *type)
  3143. {
  3144. struct rcu_state *rsp = type;
  3145. struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
  3146. _rcu_barrier_trace(rsp, TPS("IRQ"), -1, rsp->barrier_sequence);
  3147. rdp->barrier_head.func = rcu_barrier_callback;
  3148. debug_rcu_head_queue(&rdp->barrier_head);
  3149. if (rcu_segcblist_entrain(&rdp->cblist, &rdp->barrier_head, 0)) {
  3150. atomic_inc(&rsp->barrier_cpu_count);
  3151. } else {
  3152. debug_rcu_head_unqueue(&rdp->barrier_head);
  3153. _rcu_barrier_trace(rsp, TPS("IRQNQ"), -1,
  3154. rsp->barrier_sequence);
  3155. }
  3156. }
  3157. /*
  3158. * Orchestrate the specified type of RCU barrier, waiting for all
  3159. * RCU callbacks of the specified type to complete.
  3160. */
  3161. static void _rcu_barrier(struct rcu_state *rsp)
  3162. {
  3163. int cpu;
  3164. struct rcu_data *rdp;
  3165. unsigned long s = rcu_seq_snap(&rsp->barrier_sequence);
  3166. _rcu_barrier_trace(rsp, TPS("Begin"), -1, s);
  3167. /* Take mutex to serialize concurrent rcu_barrier() requests. */
  3168. mutex_lock(&rsp->barrier_mutex);
  3169. /* Did someone else do our work for us? */
  3170. if (rcu_seq_done(&rsp->barrier_sequence, s)) {
  3171. _rcu_barrier_trace(rsp, TPS("EarlyExit"), -1,
  3172. rsp->barrier_sequence);
  3173. smp_mb(); /* caller's subsequent code after above check. */
  3174. mutex_unlock(&rsp->barrier_mutex);
  3175. return;
  3176. }
  3177. /* Mark the start of the barrier operation. */
  3178. rcu_seq_start(&rsp->barrier_sequence);
  3179. _rcu_barrier_trace(rsp, TPS("Inc1"), -1, rsp->barrier_sequence);
  3180. /*
  3181. * Initialize the count to one rather than to zero in order to
  3182. * avoid a too-soon return to zero in case of a short grace period
  3183. * (or preemption of this task). Exclude CPU-hotplug operations
  3184. * to ensure that no offline CPU has callbacks queued.
  3185. */
  3186. init_completion(&rsp->barrier_completion);
  3187. atomic_set(&rsp->barrier_cpu_count, 1);
  3188. get_online_cpus();
  3189. /*
  3190. * Force each CPU with callbacks to register a new callback.
  3191. * When that callback is invoked, we will know that all of the
  3192. * corresponding CPU's preceding callbacks have been invoked.
  3193. */
  3194. for_each_possible_cpu(cpu) {
  3195. if (!cpu_online(cpu) && !rcu_is_nocb_cpu(cpu))
  3196. continue;
  3197. rdp = per_cpu_ptr(rsp->rda, cpu);
  3198. if (rcu_is_nocb_cpu(cpu)) {
  3199. if (!rcu_nocb_cpu_needs_barrier(rsp, cpu)) {
  3200. _rcu_barrier_trace(rsp, TPS("OfflineNoCB"), cpu,
  3201. rsp->barrier_sequence);
  3202. } else {
  3203. _rcu_barrier_trace(rsp, TPS("OnlineNoCB"), cpu,
  3204. rsp->barrier_sequence);
  3205. smp_mb__before_atomic();
  3206. atomic_inc(&rsp->barrier_cpu_count);
  3207. __call_rcu(&rdp->barrier_head,
  3208. rcu_barrier_callback, rsp, cpu, 0);
  3209. }
  3210. } else if (rcu_segcblist_n_cbs(&rdp->cblist)) {
  3211. _rcu_barrier_trace(rsp, TPS("OnlineQ"), cpu,
  3212. rsp->barrier_sequence);
  3213. smp_call_function_single(cpu, rcu_barrier_func, rsp, 1);
  3214. } else {
  3215. _rcu_barrier_trace(rsp, TPS("OnlineNQ"), cpu,
  3216. rsp->barrier_sequence);
  3217. }
  3218. }
  3219. put_online_cpus();
  3220. /*
  3221. * Now that we have an rcu_barrier_callback() callback on each
  3222. * CPU, and thus each counted, remove the initial count.
  3223. */
  3224. if (atomic_dec_and_test(&rsp->barrier_cpu_count))
  3225. complete(&rsp->barrier_completion);
  3226. /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
  3227. wait_for_completion(&rsp->barrier_completion);
  3228. /* Mark the end of the barrier operation. */
  3229. _rcu_barrier_trace(rsp, TPS("Inc2"), -1, rsp->barrier_sequence);
  3230. rcu_seq_end(&rsp->barrier_sequence);
  3231. /* Other rcu_barrier() invocations can now safely proceed. */
  3232. mutex_unlock(&rsp->barrier_mutex);
  3233. }
  3234. /**
  3235. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  3236. */
  3237. void rcu_barrier_bh(void)
  3238. {
  3239. _rcu_barrier(&rcu_bh_state);
  3240. }
  3241. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  3242. /**
  3243. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  3244. */
  3245. void rcu_barrier_sched(void)
  3246. {
  3247. _rcu_barrier(&rcu_sched_state);
  3248. }
  3249. EXPORT_SYMBOL_GPL(rcu_barrier_sched);
  3250. /*
  3251. * Propagate ->qsinitmask bits up the rcu_node tree to account for the
  3252. * first CPU in a given leaf rcu_node structure coming online. The caller
  3253. * must hold the corresponding leaf rcu_node ->lock with interrrupts
  3254. * disabled.
  3255. */
  3256. static void rcu_init_new_rnp(struct rcu_node *rnp_leaf)
  3257. {
  3258. long mask;
  3259. struct rcu_node *rnp = rnp_leaf;
  3260. raw_lockdep_assert_held_rcu_node(rnp);
  3261. for (;;) {
  3262. mask = rnp->grpmask;
  3263. rnp = rnp->parent;
  3264. if (rnp == NULL)
  3265. return;
  3266. raw_spin_lock_rcu_node(rnp); /* Interrupts already disabled. */
  3267. rnp->qsmaskinit |= mask;
  3268. raw_spin_unlock_rcu_node(rnp); /* Interrupts remain disabled. */
  3269. }
  3270. }
  3271. /*
  3272. * Do boot-time initialization of a CPU's per-CPU RCU data.
  3273. */
  3274. static void __init
  3275. rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp)
  3276. {
  3277. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  3278. /* Set up local state, ensuring consistent view of global state. */
  3279. rdp->grpmask = leaf_node_cpu_bit(rdp->mynode, cpu);
  3280. rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
  3281. WARN_ON_ONCE(rdp->dynticks->dynticks_nesting != 1);
  3282. WARN_ON_ONCE(rcu_dynticks_in_eqs(rcu_dynticks_snap(rdp->dynticks)));
  3283. rdp->cpu = cpu;
  3284. rdp->rsp = rsp;
  3285. rcu_boot_init_nocb_percpu_data(rdp);
  3286. }
  3287. /*
  3288. * Initialize a CPU's per-CPU RCU data. Note that only one online or
  3289. * offline event can be happening at a given time. Note also that we
  3290. * can accept some slop in the rsp->completed access due to the fact
  3291. * that this CPU cannot possibly have any RCU callbacks in flight yet.
  3292. */
  3293. static void
  3294. rcu_init_percpu_data(int cpu, struct rcu_state *rsp)
  3295. {
  3296. unsigned long flags;
  3297. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  3298. struct rcu_node *rnp = rcu_get_root(rsp);
  3299. /* Set up local state, ensuring consistent view of global state. */
  3300. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3301. rdp->qlen_last_fqs_check = 0;
  3302. rdp->n_force_qs_snap = rsp->n_force_qs;
  3303. rdp->blimit = blimit;
  3304. if (rcu_segcblist_empty(&rdp->cblist) && /* No early-boot CBs? */
  3305. !init_nocb_callback_list(rdp))
  3306. rcu_segcblist_init(&rdp->cblist); /* Re-enable callbacks. */
  3307. rdp->dynticks->dynticks_nesting = 1; /* CPU not up, no tearing. */
  3308. rcu_dynticks_eqs_online();
  3309. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  3310. /*
  3311. * Add CPU to leaf rcu_node pending-online bitmask. Any needed
  3312. * propagation up the rcu_node tree will happen at the beginning
  3313. * of the next grace period.
  3314. */
  3315. rnp = rdp->mynode;
  3316. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  3317. rdp->beenonline = true; /* We have now been online. */
  3318. rdp->gpnum = rnp->completed; /* Make CPU later note any new GP. */
  3319. rdp->completed = rnp->completed;
  3320. rdp->cpu_no_qs.b.norm = true;
  3321. rdp->rcu_qs_ctr_snap = per_cpu(rcu_dynticks.rcu_qs_ctr, cpu);
  3322. rdp->core_needs_qs = false;
  3323. rdp->rcu_iw_pending = false;
  3324. rdp->rcu_iw_gpnum = rnp->gpnum - 1;
  3325. trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpuonl"));
  3326. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3327. }
  3328. /*
  3329. * Invoked early in the CPU-online process, when pretty much all
  3330. * services are available. The incoming CPU is not present.
  3331. */
  3332. int rcutree_prepare_cpu(unsigned int cpu)
  3333. {
  3334. struct rcu_state *rsp;
  3335. for_each_rcu_flavor(rsp)
  3336. rcu_init_percpu_data(cpu, rsp);
  3337. rcu_prepare_kthreads(cpu);
  3338. rcu_spawn_all_nocb_kthreads(cpu);
  3339. return 0;
  3340. }
  3341. /*
  3342. * Update RCU priority boot kthread affinity for CPU-hotplug changes.
  3343. */
  3344. static void rcutree_affinity_setting(unsigned int cpu, int outgoing)
  3345. {
  3346. struct rcu_data *rdp = per_cpu_ptr(rcu_state_p->rda, cpu);
  3347. rcu_boost_kthread_setaffinity(rdp->mynode, outgoing);
  3348. }
  3349. /*
  3350. * Near the end of the CPU-online process. Pretty much all services
  3351. * enabled, and the CPU is now very much alive.
  3352. */
  3353. int rcutree_online_cpu(unsigned int cpu)
  3354. {
  3355. unsigned long flags;
  3356. struct rcu_data *rdp;
  3357. struct rcu_node *rnp;
  3358. struct rcu_state *rsp;
  3359. for_each_rcu_flavor(rsp) {
  3360. rdp = per_cpu_ptr(rsp->rda, cpu);
  3361. rnp = rdp->mynode;
  3362. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3363. rnp->ffmask |= rdp->grpmask;
  3364. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3365. }
  3366. if (IS_ENABLED(CONFIG_TREE_SRCU))
  3367. srcu_online_cpu(cpu);
  3368. if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
  3369. return 0; /* Too early in boot for scheduler work. */
  3370. sync_sched_exp_online_cleanup(cpu);
  3371. rcutree_affinity_setting(cpu, -1);
  3372. return 0;
  3373. }
  3374. /*
  3375. * Near the beginning of the process. The CPU is still very much alive
  3376. * with pretty much all services enabled.
  3377. */
  3378. int rcutree_offline_cpu(unsigned int cpu)
  3379. {
  3380. unsigned long flags;
  3381. struct rcu_data *rdp;
  3382. struct rcu_node *rnp;
  3383. struct rcu_state *rsp;
  3384. for_each_rcu_flavor(rsp) {
  3385. rdp = per_cpu_ptr(rsp->rda, cpu);
  3386. rnp = rdp->mynode;
  3387. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3388. rnp->ffmask &= ~rdp->grpmask;
  3389. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3390. }
  3391. rcutree_affinity_setting(cpu, cpu);
  3392. if (IS_ENABLED(CONFIG_TREE_SRCU))
  3393. srcu_offline_cpu(cpu);
  3394. return 0;
  3395. }
  3396. /*
  3397. * Near the end of the offline process. We do only tracing here.
  3398. */
  3399. int rcutree_dying_cpu(unsigned int cpu)
  3400. {
  3401. struct rcu_state *rsp;
  3402. for_each_rcu_flavor(rsp)
  3403. rcu_cleanup_dying_cpu(rsp);
  3404. return 0;
  3405. }
  3406. /*
  3407. * The outgoing CPU is gone and we are running elsewhere.
  3408. */
  3409. int rcutree_dead_cpu(unsigned int cpu)
  3410. {
  3411. struct rcu_state *rsp;
  3412. for_each_rcu_flavor(rsp) {
  3413. rcu_cleanup_dead_cpu(cpu, rsp);
  3414. do_nocb_deferred_wakeup(per_cpu_ptr(rsp->rda, cpu));
  3415. }
  3416. return 0;
  3417. }
  3418. /*
  3419. * Mark the specified CPU as being online so that subsequent grace periods
  3420. * (both expedited and normal) will wait on it. Note that this means that
  3421. * incoming CPUs are not allowed to use RCU read-side critical sections
  3422. * until this function is called. Failing to observe this restriction
  3423. * will result in lockdep splats.
  3424. *
  3425. * Note that this function is special in that it is invoked directly
  3426. * from the incoming CPU rather than from the cpuhp_step mechanism.
  3427. * This is because this function must be invoked at a precise location.
  3428. */
  3429. void rcu_cpu_starting(unsigned int cpu)
  3430. {
  3431. unsigned long flags;
  3432. unsigned long mask;
  3433. int nbits;
  3434. unsigned long oldmask;
  3435. struct rcu_data *rdp;
  3436. struct rcu_node *rnp;
  3437. struct rcu_state *rsp;
  3438. for_each_rcu_flavor(rsp) {
  3439. rdp = per_cpu_ptr(rsp->rda, cpu);
  3440. rnp = rdp->mynode;
  3441. mask = rdp->grpmask;
  3442. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3443. rnp->qsmaskinitnext |= mask;
  3444. oldmask = rnp->expmaskinitnext;
  3445. rnp->expmaskinitnext |= mask;
  3446. oldmask ^= rnp->expmaskinitnext;
  3447. nbits = bitmap_weight(&oldmask, BITS_PER_LONG);
  3448. /* Allow lockless access for expedited grace periods. */
  3449. smp_store_release(&rsp->ncpus, rsp->ncpus + nbits); /* ^^^ */
  3450. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3451. }
  3452. smp_mb(); /* Ensure RCU read-side usage follows above initialization. */
  3453. }
  3454. #ifdef CONFIG_HOTPLUG_CPU
  3455. /*
  3456. * The CPU is exiting the idle loop into the arch_cpu_idle_dead()
  3457. * function. We now remove it from the rcu_node tree's ->qsmaskinit
  3458. * bit masks.
  3459. */
  3460. static void rcu_cleanup_dying_idle_cpu(int cpu, struct rcu_state *rsp)
  3461. {
  3462. unsigned long flags;
  3463. unsigned long mask;
  3464. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  3465. struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
  3466. /* Remove outgoing CPU from mask in the leaf rcu_node structure. */
  3467. mask = rdp->grpmask;
  3468. raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */
  3469. rnp->qsmaskinitnext &= ~mask;
  3470. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3471. }
  3472. /*
  3473. * The outgoing function has no further need of RCU, so remove it from
  3474. * the list of CPUs that RCU must track.
  3475. *
  3476. * Note that this function is special in that it is invoked directly
  3477. * from the outgoing CPU rather than from the cpuhp_step mechanism.
  3478. * This is because this function must be invoked at a precise location.
  3479. */
  3480. void rcu_report_dead(unsigned int cpu)
  3481. {
  3482. struct rcu_state *rsp;
  3483. /* QS for any half-done expedited RCU-sched GP. */
  3484. preempt_disable();
  3485. rcu_report_exp_rdp(&rcu_sched_state,
  3486. this_cpu_ptr(rcu_sched_state.rda), true);
  3487. preempt_enable();
  3488. for_each_rcu_flavor(rsp)
  3489. rcu_cleanup_dying_idle_cpu(cpu, rsp);
  3490. }
  3491. /* Migrate the dead CPU's callbacks to the current CPU. */
  3492. static void rcu_migrate_callbacks(int cpu, struct rcu_state *rsp)
  3493. {
  3494. unsigned long flags;
  3495. struct rcu_data *my_rdp;
  3496. struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
  3497. struct rcu_node *rnp_root = rcu_get_root(rdp->rsp);
  3498. if (rcu_is_nocb_cpu(cpu) || rcu_segcblist_empty(&rdp->cblist))
  3499. return; /* No callbacks to migrate. */
  3500. local_irq_save(flags);
  3501. my_rdp = this_cpu_ptr(rsp->rda);
  3502. if (rcu_nocb_adopt_orphan_cbs(my_rdp, rdp, flags)) {
  3503. local_irq_restore(flags);
  3504. return;
  3505. }
  3506. raw_spin_lock_rcu_node(rnp_root); /* irqs already disabled. */
  3507. rcu_advance_cbs(rsp, rnp_root, rdp); /* Leverage recent GPs. */
  3508. rcu_advance_cbs(rsp, rnp_root, my_rdp); /* Assign GP to pending CBs. */
  3509. rcu_segcblist_merge(&my_rdp->cblist, &rdp->cblist);
  3510. WARN_ON_ONCE(rcu_segcblist_empty(&my_rdp->cblist) !=
  3511. !rcu_segcblist_n_cbs(&my_rdp->cblist));
  3512. raw_spin_unlock_irqrestore_rcu_node(rnp_root, flags);
  3513. WARN_ONCE(rcu_segcblist_n_cbs(&rdp->cblist) != 0 ||
  3514. !rcu_segcblist_empty(&rdp->cblist),
  3515. "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, 1stCB=%p\n",
  3516. cpu, rcu_segcblist_n_cbs(&rdp->cblist),
  3517. rcu_segcblist_first_cb(&rdp->cblist));
  3518. }
  3519. /*
  3520. * The outgoing CPU has just passed through the dying-idle state,
  3521. * and we are being invoked from the CPU that was IPIed to continue the
  3522. * offline operation. We need to migrate the outgoing CPU's callbacks.
  3523. */
  3524. void rcutree_migrate_callbacks(int cpu)
  3525. {
  3526. struct rcu_state *rsp;
  3527. for_each_rcu_flavor(rsp)
  3528. rcu_migrate_callbacks(cpu, rsp);
  3529. }
  3530. #endif
  3531. /*
  3532. * On non-huge systems, use expedited RCU grace periods to make suspend
  3533. * and hibernation run faster.
  3534. */
  3535. static int rcu_pm_notify(struct notifier_block *self,
  3536. unsigned long action, void *hcpu)
  3537. {
  3538. switch (action) {
  3539. case PM_HIBERNATION_PREPARE:
  3540. case PM_SUSPEND_PREPARE:
  3541. if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
  3542. rcu_expedite_gp();
  3543. break;
  3544. case PM_POST_HIBERNATION:
  3545. case PM_POST_SUSPEND:
  3546. if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
  3547. rcu_unexpedite_gp();
  3548. break;
  3549. default:
  3550. break;
  3551. }
  3552. return NOTIFY_OK;
  3553. }
  3554. /*
  3555. * Spawn the kthreads that handle each RCU flavor's grace periods.
  3556. */
  3557. static int __init rcu_spawn_gp_kthread(void)
  3558. {
  3559. unsigned long flags;
  3560. int kthread_prio_in = kthread_prio;
  3561. struct rcu_node *rnp;
  3562. struct rcu_state *rsp;
  3563. struct sched_param sp;
  3564. struct task_struct *t;
  3565. /* Force priority into range. */
  3566. if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 1)
  3567. kthread_prio = 1;
  3568. else if (kthread_prio < 0)
  3569. kthread_prio = 0;
  3570. else if (kthread_prio > 99)
  3571. kthread_prio = 99;
  3572. if (kthread_prio != kthread_prio_in)
  3573. pr_alert("rcu_spawn_gp_kthread(): Limited prio to %d from %d\n",
  3574. kthread_prio, kthread_prio_in);
  3575. rcu_scheduler_fully_active = 1;
  3576. for_each_rcu_flavor(rsp) {
  3577. t = kthread_create(rcu_gp_kthread, rsp, "%s", rsp->name);
  3578. BUG_ON(IS_ERR(t));
  3579. rnp = rcu_get_root(rsp);
  3580. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  3581. rsp->gp_kthread = t;
  3582. if (kthread_prio) {
  3583. sp.sched_priority = kthread_prio;
  3584. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  3585. }
  3586. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3587. wake_up_process(t);
  3588. }
  3589. rcu_spawn_nocb_kthreads();
  3590. rcu_spawn_boost_kthreads();
  3591. return 0;
  3592. }
  3593. early_initcall(rcu_spawn_gp_kthread);
  3594. /*
  3595. * This function is invoked towards the end of the scheduler's
  3596. * initialization process. Before this is called, the idle task might
  3597. * contain synchronous grace-period primitives (during which time, this idle
  3598. * task is booting the system, and such primitives are no-ops). After this
  3599. * function is called, any synchronous grace-period primitives are run as
  3600. * expedited, with the requesting task driving the grace period forward.
  3601. * A later core_initcall() rcu_set_runtime_mode() will switch to full
  3602. * runtime RCU functionality.
  3603. */
  3604. void rcu_scheduler_starting(void)
  3605. {
  3606. WARN_ON(num_online_cpus() != 1);
  3607. WARN_ON(nr_context_switches() > 0);
  3608. rcu_test_sync_prims();
  3609. rcu_scheduler_active = RCU_SCHEDULER_INIT;
  3610. rcu_test_sync_prims();
  3611. }
  3612. /*
  3613. * Helper function for rcu_init() that initializes one rcu_state structure.
  3614. */
  3615. static void __init rcu_init_one(struct rcu_state *rsp)
  3616. {
  3617. static const char * const buf[] = RCU_NODE_NAME_INIT;
  3618. static const char * const fqs[] = RCU_FQS_NAME_INIT;
  3619. static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
  3620. static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
  3621. int levelspread[RCU_NUM_LVLS]; /* kids/node in each level. */
  3622. int cpustride = 1;
  3623. int i;
  3624. int j;
  3625. struct rcu_node *rnp;
  3626. BUILD_BUG_ON(RCU_NUM_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
  3627. /* Silence gcc 4.8 false positive about array index out of range. */
  3628. if (rcu_num_lvls <= 0 || rcu_num_lvls > RCU_NUM_LVLS)
  3629. panic("rcu_init_one: rcu_num_lvls out of range");
  3630. /* Initialize the level-tracking arrays. */
  3631. for (i = 1; i < rcu_num_lvls; i++)
  3632. rsp->level[i] = rsp->level[i - 1] + num_rcu_lvl[i - 1];
  3633. rcu_init_levelspread(levelspread, num_rcu_lvl);
  3634. /* Initialize the elements themselves, starting from the leaves. */
  3635. for (i = rcu_num_lvls - 1; i >= 0; i--) {
  3636. cpustride *= levelspread[i];
  3637. rnp = rsp->level[i];
  3638. for (j = 0; j < num_rcu_lvl[i]; j++, rnp++) {
  3639. raw_spin_lock_init(&ACCESS_PRIVATE(rnp, lock));
  3640. lockdep_set_class_and_name(&ACCESS_PRIVATE(rnp, lock),
  3641. &rcu_node_class[i], buf[i]);
  3642. raw_spin_lock_init(&rnp->fqslock);
  3643. lockdep_set_class_and_name(&rnp->fqslock,
  3644. &rcu_fqs_class[i], fqs[i]);
  3645. rnp->gpnum = rsp->gpnum;
  3646. rnp->completed = rsp->completed;
  3647. rnp->qsmask = 0;
  3648. rnp->qsmaskinit = 0;
  3649. rnp->grplo = j * cpustride;
  3650. rnp->grphi = (j + 1) * cpustride - 1;
  3651. if (rnp->grphi >= nr_cpu_ids)
  3652. rnp->grphi = nr_cpu_ids - 1;
  3653. if (i == 0) {
  3654. rnp->grpnum = 0;
  3655. rnp->grpmask = 0;
  3656. rnp->parent = NULL;
  3657. } else {
  3658. rnp->grpnum = j % levelspread[i - 1];
  3659. rnp->grpmask = 1UL << rnp->grpnum;
  3660. rnp->parent = rsp->level[i - 1] +
  3661. j / levelspread[i - 1];
  3662. }
  3663. rnp->level = i;
  3664. INIT_LIST_HEAD(&rnp->blkd_tasks);
  3665. rcu_init_one_nocb(rnp);
  3666. init_waitqueue_head(&rnp->exp_wq[0]);
  3667. init_waitqueue_head(&rnp->exp_wq[1]);
  3668. init_waitqueue_head(&rnp->exp_wq[2]);
  3669. init_waitqueue_head(&rnp->exp_wq[3]);
  3670. spin_lock_init(&rnp->exp_lock);
  3671. }
  3672. }
  3673. init_swait_queue_head(&rsp->gp_wq);
  3674. init_swait_queue_head(&rsp->expedited_wq);
  3675. rnp = rsp->level[rcu_num_lvls - 1];
  3676. for_each_possible_cpu(i) {
  3677. while (i > rnp->grphi)
  3678. rnp++;
  3679. per_cpu_ptr(rsp->rda, i)->mynode = rnp;
  3680. rcu_boot_init_percpu_data(i, rsp);
  3681. }
  3682. list_add(&rsp->flavors, &rcu_struct_flavors);
  3683. }
  3684. /*
  3685. * Compute the rcu_node tree geometry from kernel parameters. This cannot
  3686. * replace the definitions in tree.h because those are needed to size
  3687. * the ->node array in the rcu_state structure.
  3688. */
  3689. static void __init rcu_init_geometry(void)
  3690. {
  3691. ulong d;
  3692. int i;
  3693. int rcu_capacity[RCU_NUM_LVLS];
  3694. /*
  3695. * Initialize any unspecified boot parameters.
  3696. * The default values of jiffies_till_first_fqs and
  3697. * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS
  3698. * value, which is a function of HZ, then adding one for each
  3699. * RCU_JIFFIES_FQS_DIV CPUs that might be on the system.
  3700. */
  3701. d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
  3702. if (jiffies_till_first_fqs == ULONG_MAX)
  3703. jiffies_till_first_fqs = d;
  3704. if (jiffies_till_next_fqs == ULONG_MAX)
  3705. jiffies_till_next_fqs = d;
  3706. /* If the compile-time values are accurate, just leave. */
  3707. if (rcu_fanout_leaf == RCU_FANOUT_LEAF &&
  3708. nr_cpu_ids == NR_CPUS)
  3709. return;
  3710. pr_info("RCU: Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%u\n",
  3711. rcu_fanout_leaf, nr_cpu_ids);
  3712. /*
  3713. * The boot-time rcu_fanout_leaf parameter must be at least two
  3714. * and cannot exceed the number of bits in the rcu_node masks.
  3715. * Complain and fall back to the compile-time values if this
  3716. * limit is exceeded.
  3717. */
  3718. if (rcu_fanout_leaf < 2 ||
  3719. rcu_fanout_leaf > sizeof(unsigned long) * 8) {
  3720. rcu_fanout_leaf = RCU_FANOUT_LEAF;
  3721. WARN_ON(1);
  3722. return;
  3723. }
  3724. /*
  3725. * Compute number of nodes that can be handled an rcu_node tree
  3726. * with the given number of levels.
  3727. */
  3728. rcu_capacity[0] = rcu_fanout_leaf;
  3729. for (i = 1; i < RCU_NUM_LVLS; i++)
  3730. rcu_capacity[i] = rcu_capacity[i - 1] * RCU_FANOUT;
  3731. /*
  3732. * The tree must be able to accommodate the configured number of CPUs.
  3733. * If this limit is exceeded, fall back to the compile-time values.
  3734. */
  3735. if (nr_cpu_ids > rcu_capacity[RCU_NUM_LVLS - 1]) {
  3736. rcu_fanout_leaf = RCU_FANOUT_LEAF;
  3737. WARN_ON(1);
  3738. return;
  3739. }
  3740. /* Calculate the number of levels in the tree. */
  3741. for (i = 0; nr_cpu_ids > rcu_capacity[i]; i++) {
  3742. }
  3743. rcu_num_lvls = i + 1;
  3744. /* Calculate the number of rcu_nodes at each level of the tree. */
  3745. for (i = 0; i < rcu_num_lvls; i++) {
  3746. int cap = rcu_capacity[(rcu_num_lvls - 1) - i];
  3747. num_rcu_lvl[i] = DIV_ROUND_UP(nr_cpu_ids, cap);
  3748. }
  3749. /* Calculate the total number of rcu_node structures. */
  3750. rcu_num_nodes = 0;
  3751. for (i = 0; i < rcu_num_lvls; i++)
  3752. rcu_num_nodes += num_rcu_lvl[i];
  3753. }
  3754. /*
  3755. * Dump out the structure of the rcu_node combining tree associated
  3756. * with the rcu_state structure referenced by rsp.
  3757. */
  3758. static void __init rcu_dump_rcu_node_tree(struct rcu_state *rsp)
  3759. {
  3760. int level = 0;
  3761. struct rcu_node *rnp;
  3762. pr_info("rcu_node tree layout dump\n");
  3763. pr_info(" ");
  3764. rcu_for_each_node_breadth_first(rsp, rnp) {
  3765. if (rnp->level != level) {
  3766. pr_cont("\n");
  3767. pr_info(" ");
  3768. level = rnp->level;
  3769. }
  3770. pr_cont("%d:%d ^%d ", rnp->grplo, rnp->grphi, rnp->grpnum);
  3771. }
  3772. pr_cont("\n");
  3773. }
  3774. struct workqueue_struct *rcu_gp_wq;
  3775. void __init rcu_init(void)
  3776. {
  3777. int cpu;
  3778. rcu_early_boot_tests();
  3779. rcu_bootup_announce();
  3780. rcu_init_geometry();
  3781. rcu_init_one(&rcu_bh_state);
  3782. rcu_init_one(&rcu_sched_state);
  3783. if (dump_tree)
  3784. rcu_dump_rcu_node_tree(&rcu_sched_state);
  3785. __rcu_init_preempt();
  3786. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  3787. /*
  3788. * We don't need protection against CPU-hotplug here because
  3789. * this is called early in boot, before either interrupts
  3790. * or the scheduler are operational.
  3791. */
  3792. pm_notifier(rcu_pm_notify, 0);
  3793. for_each_online_cpu(cpu) {
  3794. rcutree_prepare_cpu(cpu);
  3795. rcu_cpu_starting(cpu);
  3796. rcutree_online_cpu(cpu);
  3797. }
  3798. /* Create workqueue for expedited GPs and for Tree SRCU. */
  3799. rcu_gp_wq = alloc_workqueue("rcu_gp", WQ_MEM_RECLAIM, 0);
  3800. WARN_ON(!rcu_gp_wq);
  3801. }
  3802. #include "tree_exp.h"
  3803. #include "tree_plugin.h"