tree.c 144 KB

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