tree.c 144 KB

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