tree.c 132 KB

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