tree.c 146 KB

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