tree.c 132 KB

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