tree.c 187 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Read-Copy Update mechanism for mutual exclusion (tree-based version)
  4. *
  5. * Copyright IBM Corporation, 2008
  6. *
  7. * Authors: Dipankar Sarma <dipankar@in.ibm.com>
  8. * Manfred Spraul <manfred@colorfullife.com>
  9. * Paul E. McKenney <paulmck@linux.ibm.com>
  10. *
  11. * Based on the original work by Paul McKenney <paulmck@linux.ibm.com>
  12. * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
  13. *
  14. * For detailed explanation of Read-Copy Update mechanism see -
  15. * Documentation/RCU
  16. */
  17. #define pr_fmt(fmt) "rcu: " fmt
  18. #include <linux/types.h>
  19. #include <linux/kernel.h>
  20. #include <linux/init.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/smp.h>
  23. #include <linux/rcupdate_wait.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/sched.h>
  26. #include <linux/sched/debug.h>
  27. #include <linux/nmi.h>
  28. #include <linux/atomic.h>
  29. #include <linux/bitops.h>
  30. #include <linux/export.h>
  31. #include <linux/completion.h>
  32. #include <linux/kmemleak.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/panic.h>
  35. #include <linux/panic_notifier.h>
  36. #include <linux/percpu.h>
  37. #include <linux/notifier.h>
  38. #include <linux/cpu.h>
  39. #include <linux/mutex.h>
  40. #include <linux/time.h>
  41. #include <linux/kernel_stat.h>
  42. #include <linux/wait.h>
  43. #include <linux/kthread.h>
  44. #include <uapi/linux/sched/types.h>
  45. #include <linux/prefetch.h>
  46. #include <linux/delay.h>
  47. #include <linux/random.h>
  48. #include <linux/trace_events.h>
  49. #include <linux/suspend.h>
  50. #include <linux/ftrace.h>
  51. #include <linux/tick.h>
  52. #include <linux/sysrq.h>
  53. #include <linux/kprobes.h>
  54. #include <linux/gfp.h>
  55. #include <linux/oom.h>
  56. #include <linux/smpboot.h>
  57. #include <linux/jiffies.h>
  58. #include <linux/slab.h>
  59. #include <linux/sched/isolation.h>
  60. #include <linux/sched/clock.h>
  61. #include <linux/vmalloc.h>
  62. #include <linux/mm.h>
  63. #include <linux/kasan.h>
  64. #include <linux/context_tracking.h>
  65. #include "../time/tick-internal.h"
  66. #include "tree.h"
  67. #include "rcu.h"
  68. #ifdef MODULE_PARAM_PREFIX
  69. #undef MODULE_PARAM_PREFIX
  70. #endif
  71. #define MODULE_PARAM_PREFIX "rcutree."
  72. /* Data structures. */
  73. static void rcu_sr_normal_gp_cleanup_work(struct work_struct *);
  74. static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, rcu_data) = {
  75. .gpwrap = true,
  76. };
  77. static struct rcu_state rcu_state = {
  78. .level = { &rcu_state.node[0] },
  79. .gp_state = RCU_GP_IDLE,
  80. .gp_seq = (0UL - 300UL) << RCU_SEQ_CTR_SHIFT,
  81. .barrier_mutex = __MUTEX_INITIALIZER(rcu_state.barrier_mutex),
  82. .barrier_lock = __RAW_SPIN_LOCK_UNLOCKED(rcu_state.barrier_lock),
  83. .name = RCU_NAME,
  84. .abbr = RCU_ABBR,
  85. .exp_mutex = __MUTEX_INITIALIZER(rcu_state.exp_mutex),
  86. .exp_wake_mutex = __MUTEX_INITIALIZER(rcu_state.exp_wake_mutex),
  87. .ofl_lock = __ARCH_SPIN_LOCK_UNLOCKED,
  88. .srs_cleanup_work = __WORK_INITIALIZER(rcu_state.srs_cleanup_work,
  89. rcu_sr_normal_gp_cleanup_work),
  90. .srs_cleanups_pending = ATOMIC_INIT(0),
  91. #ifdef CONFIG_RCU_NOCB_CPU
  92. .nocb_mutex = __MUTEX_INITIALIZER(rcu_state.nocb_mutex),
  93. #endif
  94. };
  95. /* Dump rcu_node combining tree at boot to verify correct setup. */
  96. static bool dump_tree;
  97. module_param(dump_tree, bool, 0444);
  98. /* By default, use RCU_SOFTIRQ instead of rcuc kthreads. */
  99. static bool use_softirq = !IS_ENABLED(CONFIG_PREEMPT_RT);
  100. #ifndef CONFIG_PREEMPT_RT
  101. module_param(use_softirq, bool, 0444);
  102. #endif
  103. /* Control rcu_node-tree auto-balancing at boot time. */
  104. static bool rcu_fanout_exact;
  105. module_param(rcu_fanout_exact, bool, 0444);
  106. /* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */
  107. static int rcu_fanout_leaf = RCU_FANOUT_LEAF;
  108. module_param(rcu_fanout_leaf, int, 0444);
  109. int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
  110. /* Number of rcu_nodes at specified level. */
  111. int num_rcu_lvl[] = NUM_RCU_LVL_INIT;
  112. int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
  113. /*
  114. * The rcu_scheduler_active variable is initialized to the value
  115. * RCU_SCHEDULER_INACTIVE and transitions RCU_SCHEDULER_INIT just before the
  116. * first task is spawned. So when this variable is RCU_SCHEDULER_INACTIVE,
  117. * RCU can assume that there is but one task, allowing RCU to (for example)
  118. * optimize synchronize_rcu() to a simple barrier(). When this variable
  119. * is RCU_SCHEDULER_INIT, RCU must actually do all the hard work required
  120. * to detect real grace periods. This variable is also used to suppress
  121. * boot-time false positives from lockdep-RCU error checking. Finally, it
  122. * transitions from RCU_SCHEDULER_INIT to RCU_SCHEDULER_RUNNING after RCU
  123. * is fully initialized, including all of its kthreads having been spawned.
  124. */
  125. int rcu_scheduler_active __read_mostly;
  126. EXPORT_SYMBOL_GPL(rcu_scheduler_active);
  127. /*
  128. * The rcu_scheduler_fully_active variable transitions from zero to one
  129. * during the early_initcall() processing, which is after the scheduler
  130. * is capable of creating new tasks. So RCU processing (for example,
  131. * creating tasks for RCU priority boosting) must be delayed until after
  132. * rcu_scheduler_fully_active transitions from zero to one. We also
  133. * currently delay invocation of any RCU callbacks until after this point.
  134. *
  135. * It might later prove better for people registering RCU callbacks during
  136. * early boot to take responsibility for these callbacks, but one step at
  137. * a time.
  138. */
  139. static int rcu_scheduler_fully_active __read_mostly;
  140. static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
  141. unsigned long gps, unsigned long flags);
  142. static struct task_struct *rcu_boost_task(struct rcu_node *rnp);
  143. static void invoke_rcu_core(void);
  144. static void rcu_report_exp_rdp(struct rcu_data *rdp);
  145. static void sync_sched_exp_online_cleanup(int cpu);
  146. static void check_cb_ovld_locked(struct rcu_data *rdp, struct rcu_node *rnp);
  147. static bool rcu_rdp_is_offloaded(struct rcu_data *rdp);
  148. static bool rcu_rdp_cpu_online(struct rcu_data *rdp);
  149. static bool rcu_init_invoked(void);
  150. static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf);
  151. static void rcu_init_new_rnp(struct rcu_node *rnp_leaf);
  152. /*
  153. * rcuc/rcub/rcuop kthread realtime priority. The "rcuop"
  154. * real-time priority(enabling/disabling) is controlled by
  155. * the extra CONFIG_RCU_NOCB_CPU_CB_BOOST configuration.
  156. */
  157. static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0;
  158. module_param(kthread_prio, int, 0444);
  159. /* Delay in jiffies for grace-period initialization delays, debug only. */
  160. static int gp_preinit_delay;
  161. module_param(gp_preinit_delay, int, 0444);
  162. static int gp_init_delay;
  163. module_param(gp_init_delay, int, 0444);
  164. static int gp_cleanup_delay;
  165. module_param(gp_cleanup_delay, int, 0444);
  166. static int nohz_full_patience_delay;
  167. module_param(nohz_full_patience_delay, int, 0444);
  168. static int nohz_full_patience_delay_jiffies;
  169. // Add delay to rcu_read_unlock() for strict grace periods.
  170. static int rcu_unlock_delay;
  171. #ifdef CONFIG_RCU_STRICT_GRACE_PERIOD
  172. module_param(rcu_unlock_delay, int, 0444);
  173. #endif
  174. /*
  175. * This rcu parameter is runtime-read-only. It reflects
  176. * a minimum allowed number of objects which can be cached
  177. * per-CPU. Object size is equal to one page. This value
  178. * can be changed at boot time.
  179. */
  180. static int rcu_min_cached_objs = 5;
  181. module_param(rcu_min_cached_objs, int, 0444);
  182. // A page shrinker can ask for pages to be freed to make them
  183. // available for other parts of the system. This usually happens
  184. // under low memory conditions, and in that case we should also
  185. // defer page-cache filling for a short time period.
  186. //
  187. // The default value is 5 seconds, which is long enough to reduce
  188. // interference with the shrinker while it asks other systems to
  189. // drain their caches.
  190. static int rcu_delay_page_cache_fill_msec = 5000;
  191. module_param(rcu_delay_page_cache_fill_msec, int, 0444);
  192. /* Retrieve RCU kthreads priority for rcutorture */
  193. int rcu_get_gp_kthreads_prio(void)
  194. {
  195. return kthread_prio;
  196. }
  197. EXPORT_SYMBOL_GPL(rcu_get_gp_kthreads_prio);
  198. /*
  199. * Number of grace periods between delays, normalized by the duration of
  200. * the delay. The longer the delay, the more the grace periods between
  201. * each delay. The reason for this normalization is that it means that,
  202. * for non-zero delays, the overall slowdown of grace periods is constant
  203. * regardless of the duration of the delay. This arrangement balances
  204. * the need for long delays to increase some race probabilities with the
  205. * need for fast grace periods to increase other race probabilities.
  206. */
  207. #define PER_RCU_NODE_PERIOD 3 /* Number of grace periods between delays for debugging. */
  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(void)
  214. {
  215. return rcu_seq_state(rcu_seq_current(&rcu_state.gp_seq));
  216. }
  217. /*
  218. * Return the number of callbacks queued on the specified CPU.
  219. * Handles both the nocbs and normal cases.
  220. */
  221. static long rcu_get_n_cbs_cpu(int cpu)
  222. {
  223. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  224. if (rcu_segcblist_is_enabled(&rdp->cblist))
  225. return rcu_segcblist_n_cbs(&rdp->cblist);
  226. return 0;
  227. }
  228. /**
  229. * rcu_softirq_qs - Provide a set of RCU quiescent states in softirq processing
  230. *
  231. * Mark a quiescent state for RCU, Tasks RCU, and Tasks Trace RCU.
  232. * This is a special-purpose function to be used in the softirq
  233. * infrastructure and perhaps the occasional long-running softirq
  234. * handler.
  235. *
  236. * Note that from RCU's viewpoint, a call to rcu_softirq_qs() is
  237. * equivalent to momentarily completely enabling preemption. For
  238. * example, given this code::
  239. *
  240. * local_bh_disable();
  241. * do_something();
  242. * rcu_softirq_qs(); // A
  243. * do_something_else();
  244. * local_bh_enable(); // B
  245. *
  246. * A call to synchronize_rcu() that began concurrently with the
  247. * call to do_something() would be guaranteed to wait only until
  248. * execution reached statement A. Without that rcu_softirq_qs(),
  249. * that same synchronize_rcu() would instead be guaranteed to wait
  250. * until execution reached statement B.
  251. */
  252. void rcu_softirq_qs(void)
  253. {
  254. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
  255. lock_is_held(&rcu_lock_map) ||
  256. lock_is_held(&rcu_sched_lock_map),
  257. "Illegal rcu_softirq_qs() in RCU read-side critical section");
  258. rcu_qs();
  259. rcu_preempt_deferred_qs(current);
  260. rcu_tasks_qs(current, false);
  261. }
  262. /*
  263. * Reset the current CPU's RCU_WATCHING counter to indicate that the
  264. * newly onlined CPU is no longer in an extended quiescent state.
  265. * This will either leave the counter unchanged, or increment it
  266. * to the next non-quiescent value.
  267. *
  268. * The non-atomic test/increment sequence works because the upper bits
  269. * of the ->state variable are manipulated only by the corresponding CPU,
  270. * or when the corresponding CPU is offline.
  271. */
  272. static void rcu_watching_online(void)
  273. {
  274. if (ct_rcu_watching() & CT_RCU_WATCHING)
  275. return;
  276. ct_state_inc(CT_RCU_WATCHING);
  277. }
  278. /*
  279. * Return true if the snapshot returned from ct_rcu_watching()
  280. * indicates that RCU is in an extended quiescent state.
  281. */
  282. static bool rcu_watching_snap_in_eqs(int snap)
  283. {
  284. return !(snap & CT_RCU_WATCHING);
  285. }
  286. /**
  287. * rcu_watching_snap_stopped_since() - Has RCU stopped watching a given CPU
  288. * since the specified @snap?
  289. *
  290. * @rdp: The rcu_data corresponding to the CPU for which to check EQS.
  291. * @snap: rcu_watching snapshot taken when the CPU wasn't in an EQS.
  292. *
  293. * Returns true if the CPU corresponding to @rdp has spent some time in an
  294. * extended quiescent state since @snap. Note that this doesn't check if it
  295. * /still/ is in an EQS, just that it went through one since @snap.
  296. *
  297. * This is meant to be used in a loop waiting for a CPU to go through an EQS.
  298. */
  299. static bool rcu_watching_snap_stopped_since(struct rcu_data *rdp, int snap)
  300. {
  301. /*
  302. * The first failing snapshot is already ordered against the accesses
  303. * performed by the remote CPU after it exits idle.
  304. *
  305. * The second snapshot therefore only needs to order against accesses
  306. * performed by the remote CPU prior to entering idle and therefore can
  307. * rely solely on acquire semantics.
  308. */
  309. if (WARN_ON_ONCE(rcu_watching_snap_in_eqs(snap)))
  310. return true;
  311. return snap != ct_rcu_watching_cpu_acquire(rdp->cpu);
  312. }
  313. /*
  314. * Return true if the referenced integer is zero while the specified
  315. * CPU remains within a single extended quiescent state.
  316. */
  317. bool rcu_watching_zero_in_eqs(int cpu, int *vp)
  318. {
  319. int snap;
  320. // If not quiescent, force back to earlier extended quiescent state.
  321. snap = ct_rcu_watching_cpu(cpu) & ~CT_RCU_WATCHING;
  322. smp_rmb(); // Order CT state and *vp reads.
  323. if (READ_ONCE(*vp))
  324. return false; // Non-zero, so report failure;
  325. smp_rmb(); // Order *vp read and CT state re-read.
  326. // If still in the same extended quiescent state, we are good!
  327. return snap == ct_rcu_watching_cpu(cpu);
  328. }
  329. /*
  330. * Let the RCU core know that this CPU has gone through the scheduler,
  331. * which is a quiescent state. This is called when the need for a
  332. * quiescent state is urgent, so we burn an atomic operation and full
  333. * memory barriers to let the RCU core know about it, regardless of what
  334. * this CPU might (or might not) do in the near future.
  335. *
  336. * We inform the RCU core by emulating a zero-duration dyntick-idle period.
  337. *
  338. * The caller must have disabled interrupts and must not be idle.
  339. */
  340. notrace void rcu_momentary_eqs(void)
  341. {
  342. int seq;
  343. raw_cpu_write(rcu_data.rcu_need_heavy_qs, false);
  344. seq = ct_state_inc(2 * CT_RCU_WATCHING);
  345. /* It is illegal to call this from idle state. */
  346. WARN_ON_ONCE(!(seq & CT_RCU_WATCHING));
  347. rcu_preempt_deferred_qs(current);
  348. }
  349. EXPORT_SYMBOL_GPL(rcu_momentary_eqs);
  350. /**
  351. * rcu_is_cpu_rrupt_from_idle - see if 'interrupted' from idle
  352. *
  353. * If the current CPU is idle and running at a first-level (not nested)
  354. * interrupt, or directly, from idle, return true.
  355. *
  356. * The caller must have at least disabled IRQs.
  357. */
  358. static int rcu_is_cpu_rrupt_from_idle(void)
  359. {
  360. long nesting;
  361. /*
  362. * Usually called from the tick; but also used from smp_function_call()
  363. * for expedited grace periods. This latter can result in running from
  364. * the idle task, instead of an actual IPI.
  365. */
  366. lockdep_assert_irqs_disabled();
  367. /* Check for counter underflows */
  368. RCU_LOCKDEP_WARN(ct_nesting() < 0,
  369. "RCU nesting counter underflow!");
  370. RCU_LOCKDEP_WARN(ct_nmi_nesting() <= 0,
  371. "RCU nmi_nesting counter underflow/zero!");
  372. /* Are we at first interrupt nesting level? */
  373. nesting = ct_nmi_nesting();
  374. if (nesting > 1)
  375. return false;
  376. /*
  377. * If we're not in an interrupt, we must be in the idle task!
  378. */
  379. WARN_ON_ONCE(!nesting && !is_idle_task(current));
  380. /* Does CPU appear to be idle from an RCU standpoint? */
  381. return ct_nesting() == 0;
  382. }
  383. #define DEFAULT_RCU_BLIMIT (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD) ? 1000 : 10)
  384. // Maximum callbacks per rcu_do_batch ...
  385. #define DEFAULT_MAX_RCU_BLIMIT 10000 // ... even during callback flood.
  386. static long blimit = DEFAULT_RCU_BLIMIT;
  387. #define DEFAULT_RCU_QHIMARK 10000 // If this many pending, ignore blimit.
  388. static long qhimark = DEFAULT_RCU_QHIMARK;
  389. #define DEFAULT_RCU_QLOMARK 100 // Once only this many pending, use blimit.
  390. static long qlowmark = DEFAULT_RCU_QLOMARK;
  391. #define DEFAULT_RCU_QOVLD_MULT 2
  392. #define DEFAULT_RCU_QOVLD (DEFAULT_RCU_QOVLD_MULT * DEFAULT_RCU_QHIMARK)
  393. static long qovld = DEFAULT_RCU_QOVLD; // If this many pending, hammer QS.
  394. static long qovld_calc = -1; // No pre-initialization lock acquisitions!
  395. module_param(blimit, long, 0444);
  396. module_param(qhimark, long, 0444);
  397. module_param(qlowmark, long, 0444);
  398. module_param(qovld, long, 0444);
  399. static ulong jiffies_till_first_fqs = IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD) ? 0 : ULONG_MAX;
  400. static ulong jiffies_till_next_fqs = ULONG_MAX;
  401. static bool rcu_kick_kthreads;
  402. static int rcu_divisor = 7;
  403. module_param(rcu_divisor, int, 0644);
  404. /* Force an exit from rcu_do_batch() after 3 milliseconds. */
  405. static long rcu_resched_ns = 3 * NSEC_PER_MSEC;
  406. module_param(rcu_resched_ns, long, 0644);
  407. /*
  408. * How long the grace period must be before we start recruiting
  409. * quiescent-state help from rcu_note_context_switch().
  410. */
  411. static ulong jiffies_till_sched_qs = ULONG_MAX;
  412. module_param(jiffies_till_sched_qs, ulong, 0444);
  413. static ulong jiffies_to_sched_qs; /* See adjust_jiffies_till_sched_qs(). */
  414. module_param(jiffies_to_sched_qs, ulong, 0444); /* Display only! */
  415. /*
  416. * Make sure that we give the grace-period kthread time to detect any
  417. * idle CPUs before taking active measures to force quiescent states.
  418. * However, don't go below 100 milliseconds, adjusted upwards for really
  419. * large systems.
  420. */
  421. static void adjust_jiffies_till_sched_qs(void)
  422. {
  423. unsigned long j;
  424. /* If jiffies_till_sched_qs was specified, respect the request. */
  425. if (jiffies_till_sched_qs != ULONG_MAX) {
  426. WRITE_ONCE(jiffies_to_sched_qs, jiffies_till_sched_qs);
  427. return;
  428. }
  429. /* Otherwise, set to third fqs scan, but bound below on large system. */
  430. j = READ_ONCE(jiffies_till_first_fqs) +
  431. 2 * READ_ONCE(jiffies_till_next_fqs);
  432. if (j < HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV)
  433. j = HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
  434. pr_info("RCU calculated value of scheduler-enlistment delay is %ld jiffies.\n", j);
  435. WRITE_ONCE(jiffies_to_sched_qs, j);
  436. }
  437. static int param_set_first_fqs_jiffies(const char *val, const struct kernel_param *kp)
  438. {
  439. ulong j;
  440. int ret = kstrtoul(val, 0, &j);
  441. if (!ret) {
  442. WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : j);
  443. adjust_jiffies_till_sched_qs();
  444. }
  445. return ret;
  446. }
  447. static int param_set_next_fqs_jiffies(const char *val, const struct kernel_param *kp)
  448. {
  449. ulong j;
  450. int ret = kstrtoul(val, 0, &j);
  451. if (!ret) {
  452. WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : (j ?: 1));
  453. adjust_jiffies_till_sched_qs();
  454. }
  455. return ret;
  456. }
  457. static const struct kernel_param_ops first_fqs_jiffies_ops = {
  458. .set = param_set_first_fqs_jiffies,
  459. .get = param_get_ulong,
  460. };
  461. static const struct kernel_param_ops next_fqs_jiffies_ops = {
  462. .set = param_set_next_fqs_jiffies,
  463. .get = param_get_ulong,
  464. };
  465. module_param_cb(jiffies_till_first_fqs, &first_fqs_jiffies_ops, &jiffies_till_first_fqs, 0644);
  466. module_param_cb(jiffies_till_next_fqs, &next_fqs_jiffies_ops, &jiffies_till_next_fqs, 0644);
  467. module_param(rcu_kick_kthreads, bool, 0644);
  468. static void force_qs_rnp(int (*f)(struct rcu_data *rdp));
  469. static int rcu_pending(int user);
  470. /*
  471. * Return the number of RCU GPs completed thus far for debug & stats.
  472. */
  473. unsigned long rcu_get_gp_seq(void)
  474. {
  475. return READ_ONCE(rcu_state.gp_seq);
  476. }
  477. EXPORT_SYMBOL_GPL(rcu_get_gp_seq);
  478. /*
  479. * Return the number of RCU expedited batches completed thus far for
  480. * debug & stats. Odd numbers mean that a batch is in progress, even
  481. * numbers mean idle. The value returned will thus be roughly double
  482. * the cumulative batches since boot.
  483. */
  484. unsigned long rcu_exp_batches_completed(void)
  485. {
  486. return rcu_state.expedited_sequence;
  487. }
  488. EXPORT_SYMBOL_GPL(rcu_exp_batches_completed);
  489. /*
  490. * Return the root node of the rcu_state structure.
  491. */
  492. static struct rcu_node *rcu_get_root(void)
  493. {
  494. return &rcu_state.node[0];
  495. }
  496. /*
  497. * Send along grace-period-related data for rcutorture diagnostics.
  498. */
  499. void rcutorture_get_gp_data(int *flags, unsigned long *gp_seq)
  500. {
  501. *flags = READ_ONCE(rcu_state.gp_flags);
  502. *gp_seq = rcu_seq_current(&rcu_state.gp_seq);
  503. }
  504. EXPORT_SYMBOL_GPL(rcutorture_get_gp_data);
  505. #if defined(CONFIG_NO_HZ_FULL) && (!defined(CONFIG_GENERIC_ENTRY) || !defined(CONFIG_KVM_XFER_TO_GUEST_WORK))
  506. /*
  507. * An empty function that will trigger a reschedule on
  508. * IRQ tail once IRQs get re-enabled on userspace/guest resume.
  509. */
  510. static void late_wakeup_func(struct irq_work *work)
  511. {
  512. }
  513. static DEFINE_PER_CPU(struct irq_work, late_wakeup_work) =
  514. IRQ_WORK_INIT(late_wakeup_func);
  515. /*
  516. * If either:
  517. *
  518. * 1) the task is about to enter in guest mode and $ARCH doesn't support KVM generic work
  519. * 2) the task is about to enter in user mode and $ARCH doesn't support generic entry.
  520. *
  521. * In these cases the late RCU wake ups aren't supported in the resched loops and our
  522. * last resort is to fire a local irq_work that will trigger a reschedule once IRQs
  523. * get re-enabled again.
  524. */
  525. noinstr void rcu_irq_work_resched(void)
  526. {
  527. struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
  528. if (IS_ENABLED(CONFIG_GENERIC_ENTRY) && !(current->flags & PF_VCPU))
  529. return;
  530. if (IS_ENABLED(CONFIG_KVM_XFER_TO_GUEST_WORK) && (current->flags & PF_VCPU))
  531. return;
  532. instrumentation_begin();
  533. if (do_nocb_deferred_wakeup(rdp) && need_resched()) {
  534. irq_work_queue(this_cpu_ptr(&late_wakeup_work));
  535. }
  536. instrumentation_end();
  537. }
  538. #endif /* #if defined(CONFIG_NO_HZ_FULL) && (!defined(CONFIG_GENERIC_ENTRY) || !defined(CONFIG_KVM_XFER_TO_GUEST_WORK)) */
  539. #ifdef CONFIG_PROVE_RCU
  540. /**
  541. * rcu_irq_exit_check_preempt - Validate that scheduling is possible
  542. */
  543. void rcu_irq_exit_check_preempt(void)
  544. {
  545. lockdep_assert_irqs_disabled();
  546. RCU_LOCKDEP_WARN(ct_nesting() <= 0,
  547. "RCU nesting counter underflow/zero!");
  548. RCU_LOCKDEP_WARN(ct_nmi_nesting() !=
  549. CT_NESTING_IRQ_NONIDLE,
  550. "Bad RCU nmi_nesting counter\n");
  551. RCU_LOCKDEP_WARN(!rcu_is_watching_curr_cpu(),
  552. "RCU in extended quiescent state!");
  553. }
  554. #endif /* #ifdef CONFIG_PROVE_RCU */
  555. #ifdef CONFIG_NO_HZ_FULL
  556. /**
  557. * __rcu_irq_enter_check_tick - Enable scheduler tick on CPU if RCU needs it.
  558. *
  559. * The scheduler tick is not normally enabled when CPUs enter the kernel
  560. * from nohz_full userspace execution. After all, nohz_full userspace
  561. * execution is an RCU quiescent state and the time executing in the kernel
  562. * is quite short. Except of course when it isn't. And it is not hard to
  563. * cause a large system to spend tens of seconds or even minutes looping
  564. * in the kernel, which can cause a number of problems, include RCU CPU
  565. * stall warnings.
  566. *
  567. * Therefore, if a nohz_full CPU fails to report a quiescent state
  568. * in a timely manner, the RCU grace-period kthread sets that CPU's
  569. * ->rcu_urgent_qs flag with the expectation that the next interrupt or
  570. * exception will invoke this function, which will turn on the scheduler
  571. * tick, which will enable RCU to detect that CPU's quiescent states,
  572. * for example, due to cond_resched() calls in CONFIG_PREEMPT=n kernels.
  573. * The tick will be disabled once a quiescent state is reported for
  574. * this CPU.
  575. *
  576. * Of course, in carefully tuned systems, there might never be an
  577. * interrupt or exception. In that case, the RCU grace-period kthread
  578. * will eventually cause one to happen. However, in less carefully
  579. * controlled environments, this function allows RCU to get what it
  580. * needs without creating otherwise useless interruptions.
  581. */
  582. void __rcu_irq_enter_check_tick(void)
  583. {
  584. struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
  585. // If we're here from NMI there's nothing to do.
  586. if (in_nmi())
  587. return;
  588. RCU_LOCKDEP_WARN(!rcu_is_watching_curr_cpu(),
  589. "Illegal rcu_irq_enter_check_tick() from extended quiescent state");
  590. if (!tick_nohz_full_cpu(rdp->cpu) ||
  591. !READ_ONCE(rdp->rcu_urgent_qs) ||
  592. READ_ONCE(rdp->rcu_forced_tick)) {
  593. // RCU doesn't need nohz_full help from this CPU, or it is
  594. // already getting that help.
  595. return;
  596. }
  597. // We get here only when not in an extended quiescent state and
  598. // from interrupts (as opposed to NMIs). Therefore, (1) RCU is
  599. // already watching and (2) The fact that we are in an interrupt
  600. // handler and that the rcu_node lock is an irq-disabled lock
  601. // prevents self-deadlock. So we can safely recheck under the lock.
  602. // Note that the nohz_full state currently cannot change.
  603. raw_spin_lock_rcu_node(rdp->mynode);
  604. if (READ_ONCE(rdp->rcu_urgent_qs) && !rdp->rcu_forced_tick) {
  605. // A nohz_full CPU is in the kernel and RCU needs a
  606. // quiescent state. Turn on the tick!
  607. WRITE_ONCE(rdp->rcu_forced_tick, true);
  608. tick_dep_set_cpu(rdp->cpu, TICK_DEP_BIT_RCU);
  609. }
  610. raw_spin_unlock_rcu_node(rdp->mynode);
  611. }
  612. NOKPROBE_SYMBOL(__rcu_irq_enter_check_tick);
  613. #endif /* CONFIG_NO_HZ_FULL */
  614. /*
  615. * Check to see if any future non-offloaded RCU-related work will need
  616. * to be done by the current CPU, even if none need be done immediately,
  617. * returning 1 if so. This function is part of the RCU implementation;
  618. * it is -not- an exported member of the RCU API. This is used by
  619. * the idle-entry code to figure out whether it is safe to disable the
  620. * scheduler-clock interrupt.
  621. *
  622. * Just check whether or not this CPU has non-offloaded RCU callbacks
  623. * queued.
  624. */
  625. int rcu_needs_cpu(void)
  626. {
  627. return !rcu_segcblist_empty(&this_cpu_ptr(&rcu_data)->cblist) &&
  628. !rcu_rdp_is_offloaded(this_cpu_ptr(&rcu_data));
  629. }
  630. /*
  631. * If any sort of urgency was applied to the current CPU (for example,
  632. * the scheduler-clock interrupt was enabled on a nohz_full CPU) in order
  633. * to get to a quiescent state, disable it.
  634. */
  635. static void rcu_disable_urgency_upon_qs(struct rcu_data *rdp)
  636. {
  637. raw_lockdep_assert_held_rcu_node(rdp->mynode);
  638. WRITE_ONCE(rdp->rcu_urgent_qs, false);
  639. WRITE_ONCE(rdp->rcu_need_heavy_qs, false);
  640. if (tick_nohz_full_cpu(rdp->cpu) && rdp->rcu_forced_tick) {
  641. tick_dep_clear_cpu(rdp->cpu, TICK_DEP_BIT_RCU);
  642. WRITE_ONCE(rdp->rcu_forced_tick, false);
  643. }
  644. }
  645. /**
  646. * rcu_is_watching - RCU read-side critical sections permitted on current CPU?
  647. *
  648. * Return @true if RCU is watching the running CPU and @false otherwise.
  649. * An @true return means that this CPU can safely enter RCU read-side
  650. * critical sections.
  651. *
  652. * Although calls to rcu_is_watching() from most parts of the kernel
  653. * will return @true, there are important exceptions. For example, if the
  654. * current CPU is deep within its idle loop, in kernel entry/exit code,
  655. * or offline, rcu_is_watching() will return @false.
  656. *
  657. * Make notrace because it can be called by the internal functions of
  658. * ftrace, and making this notrace removes unnecessary recursion calls.
  659. */
  660. notrace bool rcu_is_watching(void)
  661. {
  662. bool ret;
  663. preempt_disable_notrace();
  664. ret = rcu_is_watching_curr_cpu();
  665. preempt_enable_notrace();
  666. return ret;
  667. }
  668. EXPORT_SYMBOL_GPL(rcu_is_watching);
  669. /*
  670. * If a holdout task is actually running, request an urgent quiescent
  671. * state from its CPU. This is unsynchronized, so migrations can cause
  672. * the request to go to the wrong CPU. Which is OK, all that will happen
  673. * is that the CPU's next context switch will be a bit slower and next
  674. * time around this task will generate another request.
  675. */
  676. void rcu_request_urgent_qs_task(struct task_struct *t)
  677. {
  678. int cpu;
  679. barrier();
  680. cpu = task_cpu(t);
  681. if (!task_curr(t))
  682. return; /* This task is not running on that CPU. */
  683. smp_store_release(per_cpu_ptr(&rcu_data.rcu_urgent_qs, cpu), true);
  684. }
  685. /*
  686. * When trying to report a quiescent state on behalf of some other CPU,
  687. * it is our responsibility to check for and handle potential overflow
  688. * of the rcu_node ->gp_seq counter with respect to the rcu_data counters.
  689. * After all, the CPU might be in deep idle state, and thus executing no
  690. * code whatsoever.
  691. */
  692. static void rcu_gpnum_ovf(struct rcu_node *rnp, struct rcu_data *rdp)
  693. {
  694. raw_lockdep_assert_held_rcu_node(rnp);
  695. if (ULONG_CMP_LT(rcu_seq_current(&rdp->gp_seq) + ULONG_MAX / 4,
  696. rnp->gp_seq))
  697. WRITE_ONCE(rdp->gpwrap, true);
  698. if (ULONG_CMP_LT(rdp->rcu_iw_gp_seq + ULONG_MAX / 4, rnp->gp_seq))
  699. rdp->rcu_iw_gp_seq = rnp->gp_seq + ULONG_MAX / 4;
  700. }
  701. /*
  702. * Snapshot the specified CPU's RCU_WATCHING counter so that we can later
  703. * credit them with an implicit quiescent state. Return 1 if this CPU
  704. * is in dynticks idle mode, which is an extended quiescent state.
  705. */
  706. static int rcu_watching_snap_save(struct rcu_data *rdp)
  707. {
  708. /*
  709. * Full ordering between remote CPU's post idle accesses and updater's
  710. * accesses prior to current GP (and also the started GP sequence number)
  711. * is enforced by rcu_seq_start() implicit barrier and even further by
  712. * smp_mb__after_unlock_lock() barriers chained all the way throughout the
  713. * rnp locking tree since rcu_gp_init() and up to the current leaf rnp
  714. * locking.
  715. *
  716. * Ordering between remote CPU's pre idle accesses and post grace period
  717. * updater's accesses is enforced by the below acquire semantic.
  718. */
  719. rdp->watching_snap = ct_rcu_watching_cpu_acquire(rdp->cpu);
  720. if (rcu_watching_snap_in_eqs(rdp->watching_snap)) {
  721. trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti"));
  722. rcu_gpnum_ovf(rdp->mynode, rdp);
  723. return 1;
  724. }
  725. return 0;
  726. }
  727. #ifndef arch_irq_stat_cpu
  728. #define arch_irq_stat_cpu(cpu) 0
  729. #endif
  730. /*
  731. * Returns positive if the specified CPU has passed through a quiescent state
  732. * by virtue of being in or having passed through an dynticks idle state since
  733. * the last call to rcu_watching_snap_save() for this same CPU, or by
  734. * virtue of having been offline.
  735. *
  736. * Returns negative if the specified CPU needs a force resched.
  737. *
  738. * Returns zero otherwise.
  739. */
  740. static int rcu_watching_snap_recheck(struct rcu_data *rdp)
  741. {
  742. unsigned long jtsq;
  743. int ret = 0;
  744. struct rcu_node *rnp = rdp->mynode;
  745. /*
  746. * If the CPU passed through or entered a dynticks idle phase with
  747. * no active irq/NMI handlers, then we can safely pretend that the CPU
  748. * already acknowledged the request to pass through a quiescent
  749. * state. Either way, that CPU cannot possibly be in an RCU
  750. * read-side critical section that started before the beginning
  751. * of the current RCU grace period.
  752. */
  753. if (rcu_watching_snap_stopped_since(rdp, rdp->watching_snap)) {
  754. trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti"));
  755. rcu_gpnum_ovf(rnp, rdp);
  756. return 1;
  757. }
  758. /*
  759. * Complain if a CPU that is considered to be offline from RCU's
  760. * perspective has not yet reported a quiescent state. After all,
  761. * the offline CPU should have reported a quiescent state during
  762. * the CPU-offline process, or, failing that, by rcu_gp_init()
  763. * if it ran concurrently with either the CPU going offline or the
  764. * last task on a leaf rcu_node structure exiting its RCU read-side
  765. * critical section while all CPUs corresponding to that structure
  766. * are offline. This added warning detects bugs in any of these
  767. * code paths.
  768. *
  769. * The rcu_node structure's ->lock is held here, which excludes
  770. * the relevant portions the CPU-hotplug code, the grace-period
  771. * initialization code, and the rcu_read_unlock() code paths.
  772. *
  773. * For more detail, please refer to the "Hotplug CPU" section
  774. * of RCU's Requirements documentation.
  775. */
  776. if (WARN_ON_ONCE(!rcu_rdp_cpu_online(rdp))) {
  777. struct rcu_node *rnp1;
  778. pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n",
  779. __func__, rnp->grplo, rnp->grphi, rnp->level,
  780. (long)rnp->gp_seq, (long)rnp->completedqs);
  781. for (rnp1 = rnp; rnp1; rnp1 = rnp1->parent)
  782. pr_info("%s: %d:%d ->qsmask %#lx ->qsmaskinit %#lx ->qsmaskinitnext %#lx ->rcu_gp_init_mask %#lx\n",
  783. __func__, rnp1->grplo, rnp1->grphi, rnp1->qsmask, rnp1->qsmaskinit, rnp1->qsmaskinitnext, rnp1->rcu_gp_init_mask);
  784. pr_info("%s %d: %c online: %ld(%d) offline: %ld(%d)\n",
  785. __func__, rdp->cpu, ".o"[rcu_rdp_cpu_online(rdp)],
  786. (long)rdp->rcu_onl_gp_seq, rdp->rcu_onl_gp_state,
  787. (long)rdp->rcu_ofl_gp_seq, rdp->rcu_ofl_gp_state);
  788. return 1; /* Break things loose after complaining. */
  789. }
  790. /*
  791. * A CPU running for an extended time within the kernel can
  792. * delay RCU grace periods: (1) At age jiffies_to_sched_qs,
  793. * set .rcu_urgent_qs, (2) At age 2*jiffies_to_sched_qs, set
  794. * both .rcu_need_heavy_qs and .rcu_urgent_qs. Note that the
  795. * unsynchronized assignments to the per-CPU rcu_need_heavy_qs
  796. * variable are safe because the assignments are repeated if this
  797. * CPU failed to pass through a quiescent state. This code
  798. * also checks .jiffies_resched in case jiffies_to_sched_qs
  799. * is set way high.
  800. */
  801. jtsq = READ_ONCE(jiffies_to_sched_qs);
  802. if (!READ_ONCE(rdp->rcu_need_heavy_qs) &&
  803. (time_after(jiffies, rcu_state.gp_start + jtsq * 2) ||
  804. time_after(jiffies, rcu_state.jiffies_resched) ||
  805. rcu_state.cbovld)) {
  806. WRITE_ONCE(rdp->rcu_need_heavy_qs, true);
  807. /* Store rcu_need_heavy_qs before rcu_urgent_qs. */
  808. smp_store_release(&rdp->rcu_urgent_qs, true);
  809. } else if (time_after(jiffies, rcu_state.gp_start + jtsq)) {
  810. WRITE_ONCE(rdp->rcu_urgent_qs, true);
  811. }
  812. /*
  813. * NO_HZ_FULL CPUs can run in-kernel without rcu_sched_clock_irq!
  814. * The above code handles this, but only for straight cond_resched().
  815. * And some in-kernel loops check need_resched() before calling
  816. * cond_resched(), which defeats the above code for CPUs that are
  817. * running in-kernel with scheduling-clock interrupts disabled.
  818. * So hit them over the head with the resched_cpu() hammer!
  819. */
  820. if (tick_nohz_full_cpu(rdp->cpu) &&
  821. (time_after(jiffies, READ_ONCE(rdp->last_fqs_resched) + jtsq * 3) ||
  822. rcu_state.cbovld)) {
  823. WRITE_ONCE(rdp->rcu_urgent_qs, true);
  824. WRITE_ONCE(rdp->last_fqs_resched, jiffies);
  825. ret = -1;
  826. }
  827. /*
  828. * If more than halfway to RCU CPU stall-warning time, invoke
  829. * resched_cpu() more frequently to try to loosen things up a bit.
  830. * Also check to see if the CPU is getting hammered with interrupts,
  831. * but only once per grace period, just to keep the IPIs down to
  832. * a dull roar.
  833. */
  834. if (time_after(jiffies, rcu_state.jiffies_resched)) {
  835. if (time_after(jiffies,
  836. READ_ONCE(rdp->last_fqs_resched) + jtsq)) {
  837. WRITE_ONCE(rdp->last_fqs_resched, jiffies);
  838. ret = -1;
  839. }
  840. if (IS_ENABLED(CONFIG_IRQ_WORK) &&
  841. !rdp->rcu_iw_pending && rdp->rcu_iw_gp_seq != rnp->gp_seq &&
  842. (rnp->ffmask & rdp->grpmask)) {
  843. rdp->rcu_iw_pending = true;
  844. rdp->rcu_iw_gp_seq = rnp->gp_seq;
  845. irq_work_queue_on(&rdp->rcu_iw, rdp->cpu);
  846. }
  847. if (rcu_cpu_stall_cputime && rdp->snap_record.gp_seq != rdp->gp_seq) {
  848. int cpu = rdp->cpu;
  849. struct rcu_snap_record *rsrp;
  850. struct kernel_cpustat *kcsp;
  851. kcsp = &kcpustat_cpu(cpu);
  852. rsrp = &rdp->snap_record;
  853. rsrp->cputime_irq = kcpustat_field(kcsp, CPUTIME_IRQ, cpu);
  854. rsrp->cputime_softirq = kcpustat_field(kcsp, CPUTIME_SOFTIRQ, cpu);
  855. rsrp->cputime_system = kcpustat_field(kcsp, CPUTIME_SYSTEM, cpu);
  856. rsrp->nr_hardirqs = kstat_cpu_irqs_sum(cpu) + arch_irq_stat_cpu(cpu);
  857. rsrp->nr_softirqs = kstat_cpu_softirqs_sum(cpu);
  858. rsrp->nr_csw = nr_context_switches_cpu(cpu);
  859. rsrp->jiffies = jiffies;
  860. rsrp->gp_seq = rdp->gp_seq;
  861. }
  862. }
  863. return ret;
  864. }
  865. /* Trace-event wrapper function for trace_rcu_future_grace_period. */
  866. static void trace_rcu_this_gp(struct rcu_node *rnp, struct rcu_data *rdp,
  867. unsigned long gp_seq_req, const char *s)
  868. {
  869. trace_rcu_future_grace_period(rcu_state.name, READ_ONCE(rnp->gp_seq),
  870. gp_seq_req, rnp->level,
  871. rnp->grplo, rnp->grphi, s);
  872. }
  873. /*
  874. * rcu_start_this_gp - Request the start of a particular grace period
  875. * @rnp_start: The leaf node of the CPU from which to start.
  876. * @rdp: The rcu_data corresponding to the CPU from which to start.
  877. * @gp_seq_req: The gp_seq of the grace period to start.
  878. *
  879. * Start the specified grace period, as needed to handle newly arrived
  880. * callbacks. The required future grace periods are recorded in each
  881. * rcu_node structure's ->gp_seq_needed field. Returns true if there
  882. * is reason to awaken the grace-period kthread.
  883. *
  884. * The caller must hold the specified rcu_node structure's ->lock, which
  885. * is why the caller is responsible for waking the grace-period kthread.
  886. *
  887. * Returns true if the GP thread needs to be awakened else false.
  888. */
  889. static bool rcu_start_this_gp(struct rcu_node *rnp_start, struct rcu_data *rdp,
  890. unsigned long gp_seq_req)
  891. {
  892. bool ret = false;
  893. struct rcu_node *rnp;
  894. /*
  895. * Use funnel locking to either acquire the root rcu_node
  896. * structure's lock or bail out if the need for this grace period
  897. * has already been recorded -- or if that grace period has in
  898. * fact already started. If there is already a grace period in
  899. * progress in a non-leaf node, no recording is needed because the
  900. * end of the grace period will scan the leaf rcu_node structures.
  901. * Note that rnp_start->lock must not be released.
  902. */
  903. raw_lockdep_assert_held_rcu_node(rnp_start);
  904. trace_rcu_this_gp(rnp_start, rdp, gp_seq_req, TPS("Startleaf"));
  905. for (rnp = rnp_start; 1; rnp = rnp->parent) {
  906. if (rnp != rnp_start)
  907. raw_spin_lock_rcu_node(rnp);
  908. if (ULONG_CMP_GE(rnp->gp_seq_needed, gp_seq_req) ||
  909. rcu_seq_started(&rnp->gp_seq, gp_seq_req) ||
  910. (rnp != rnp_start &&
  911. rcu_seq_state(rcu_seq_current(&rnp->gp_seq)))) {
  912. trace_rcu_this_gp(rnp, rdp, gp_seq_req,
  913. TPS("Prestarted"));
  914. goto unlock_out;
  915. }
  916. WRITE_ONCE(rnp->gp_seq_needed, gp_seq_req);
  917. if (rcu_seq_state(rcu_seq_current(&rnp->gp_seq))) {
  918. /*
  919. * We just marked the leaf or internal node, and a
  920. * grace period is in progress, which means that
  921. * rcu_gp_cleanup() will see the marking. Bail to
  922. * reduce contention.
  923. */
  924. trace_rcu_this_gp(rnp_start, rdp, gp_seq_req,
  925. TPS("Startedleaf"));
  926. goto unlock_out;
  927. }
  928. if (rnp != rnp_start && rnp->parent != NULL)
  929. raw_spin_unlock_rcu_node(rnp);
  930. if (!rnp->parent)
  931. break; /* At root, and perhaps also leaf. */
  932. }
  933. /* If GP already in progress, just leave, otherwise start one. */
  934. if (rcu_gp_in_progress()) {
  935. trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedleafroot"));
  936. goto unlock_out;
  937. }
  938. trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedroot"));
  939. WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags | RCU_GP_FLAG_INIT);
  940. WRITE_ONCE(rcu_state.gp_req_activity, jiffies);
  941. if (!READ_ONCE(rcu_state.gp_kthread)) {
  942. trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("NoGPkthread"));
  943. goto unlock_out;
  944. }
  945. trace_rcu_grace_period(rcu_state.name, data_race(rcu_state.gp_seq), TPS("newreq"));
  946. ret = true; /* Caller must wake GP kthread. */
  947. unlock_out:
  948. /* Push furthest requested GP to leaf node and rcu_data structure. */
  949. if (ULONG_CMP_LT(gp_seq_req, rnp->gp_seq_needed)) {
  950. WRITE_ONCE(rnp_start->gp_seq_needed, rnp->gp_seq_needed);
  951. WRITE_ONCE(rdp->gp_seq_needed, rnp->gp_seq_needed);
  952. }
  953. if (rnp != rnp_start)
  954. raw_spin_unlock_rcu_node(rnp);
  955. return ret;
  956. }
  957. /*
  958. * Clean up any old requests for the just-ended grace period. Also return
  959. * whether any additional grace periods have been requested.
  960. */
  961. static bool rcu_future_gp_cleanup(struct rcu_node *rnp)
  962. {
  963. bool needmore;
  964. struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
  965. needmore = ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed);
  966. if (!needmore)
  967. rnp->gp_seq_needed = rnp->gp_seq; /* Avoid counter wrap. */
  968. trace_rcu_this_gp(rnp, rdp, rnp->gp_seq,
  969. needmore ? TPS("CleanupMore") : TPS("Cleanup"));
  970. return needmore;
  971. }
  972. static void swake_up_one_online_ipi(void *arg)
  973. {
  974. struct swait_queue_head *wqh = arg;
  975. swake_up_one(wqh);
  976. }
  977. static void swake_up_one_online(struct swait_queue_head *wqh)
  978. {
  979. int cpu = get_cpu();
  980. /*
  981. * If called from rcutree_report_cpu_starting(), wake up
  982. * is dangerous that late in the CPU-down hotplug process. The
  983. * scheduler might queue an ignored hrtimer. Defer the wake up
  984. * to an online CPU instead.
  985. */
  986. if (unlikely(cpu_is_offline(cpu))) {
  987. int target;
  988. target = cpumask_any_and(housekeeping_cpumask(HK_TYPE_RCU),
  989. cpu_online_mask);
  990. smp_call_function_single(target, swake_up_one_online_ipi,
  991. wqh, 0);
  992. put_cpu();
  993. } else {
  994. put_cpu();
  995. swake_up_one(wqh);
  996. }
  997. }
  998. /*
  999. * Awaken the grace-period kthread. Don't do a self-awaken (unless in an
  1000. * interrupt or softirq handler, in which case we just might immediately
  1001. * sleep upon return, resulting in a grace-period hang), and don't bother
  1002. * awakening when there is nothing for the grace-period kthread to do
  1003. * (as in several CPUs raced to awaken, we lost), and finally don't try
  1004. * to awaken a kthread that has not yet been created. If all those checks
  1005. * are passed, track some debug information and awaken.
  1006. *
  1007. * So why do the self-wakeup when in an interrupt or softirq handler
  1008. * in the grace-period kthread's context? Because the kthread might have
  1009. * been interrupted just as it was going to sleep, and just after the final
  1010. * pre-sleep check of the awaken condition. In this case, a wakeup really
  1011. * is required, and is therefore supplied.
  1012. */
  1013. static void rcu_gp_kthread_wake(void)
  1014. {
  1015. struct task_struct *t = READ_ONCE(rcu_state.gp_kthread);
  1016. if ((current == t && !in_hardirq() && !in_serving_softirq()) ||
  1017. !READ_ONCE(rcu_state.gp_flags) || !t)
  1018. return;
  1019. WRITE_ONCE(rcu_state.gp_wake_time, jiffies);
  1020. WRITE_ONCE(rcu_state.gp_wake_seq, READ_ONCE(rcu_state.gp_seq));
  1021. swake_up_one_online(&rcu_state.gp_wq);
  1022. }
  1023. /*
  1024. * If there is room, assign a ->gp_seq number to any callbacks on this
  1025. * CPU that have not already been assigned. Also accelerate any callbacks
  1026. * that were previously assigned a ->gp_seq number that has since proven
  1027. * to be too conservative, which can happen if callbacks get assigned a
  1028. * ->gp_seq number while RCU is idle, but with reference to a non-root
  1029. * rcu_node structure. This function is idempotent, so it does not hurt
  1030. * to call it repeatedly. Returns an flag saying that we should awaken
  1031. * the RCU grace-period kthread.
  1032. *
  1033. * The caller must hold rnp->lock with interrupts disabled.
  1034. */
  1035. static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
  1036. {
  1037. unsigned long gp_seq_req;
  1038. bool ret = false;
  1039. rcu_lockdep_assert_cblist_protected(rdp);
  1040. raw_lockdep_assert_held_rcu_node(rnp);
  1041. /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
  1042. if (!rcu_segcblist_pend_cbs(&rdp->cblist))
  1043. return false;
  1044. trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbPreAcc"));
  1045. /*
  1046. * Callbacks are often registered with incomplete grace-period
  1047. * information. Something about the fact that getting exact
  1048. * information requires acquiring a global lock... RCU therefore
  1049. * makes a conservative estimate of the grace period number at which
  1050. * a given callback will become ready to invoke. The following
  1051. * code checks this estimate and improves it when possible, thus
  1052. * accelerating callback invocation to an earlier grace-period
  1053. * number.
  1054. */
  1055. gp_seq_req = rcu_seq_snap(&rcu_state.gp_seq);
  1056. if (rcu_segcblist_accelerate(&rdp->cblist, gp_seq_req))
  1057. ret = rcu_start_this_gp(rnp, rdp, gp_seq_req);
  1058. /* Trace depending on how much we were able to accelerate. */
  1059. if (rcu_segcblist_restempty(&rdp->cblist, RCU_WAIT_TAIL))
  1060. trace_rcu_grace_period(rcu_state.name, gp_seq_req, TPS("AccWaitCB"));
  1061. else
  1062. trace_rcu_grace_period(rcu_state.name, gp_seq_req, TPS("AccReadyCB"));
  1063. trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbPostAcc"));
  1064. return ret;
  1065. }
  1066. /*
  1067. * Similar to rcu_accelerate_cbs(), but does not require that the leaf
  1068. * rcu_node structure's ->lock be held. It consults the cached value
  1069. * of ->gp_seq_needed in the rcu_data structure, and if that indicates
  1070. * that a new grace-period request be made, invokes rcu_accelerate_cbs()
  1071. * while holding the leaf rcu_node structure's ->lock.
  1072. */
  1073. static void rcu_accelerate_cbs_unlocked(struct rcu_node *rnp,
  1074. struct rcu_data *rdp)
  1075. {
  1076. unsigned long c;
  1077. bool needwake;
  1078. rcu_lockdep_assert_cblist_protected(rdp);
  1079. c = rcu_seq_snap(&rcu_state.gp_seq);
  1080. if (!READ_ONCE(rdp->gpwrap) && ULONG_CMP_GE(rdp->gp_seq_needed, c)) {
  1081. /* Old request still live, so mark recent callbacks. */
  1082. (void)rcu_segcblist_accelerate(&rdp->cblist, c);
  1083. return;
  1084. }
  1085. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  1086. needwake = rcu_accelerate_cbs(rnp, rdp);
  1087. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  1088. if (needwake)
  1089. rcu_gp_kthread_wake();
  1090. }
  1091. /*
  1092. * Move any callbacks whose grace period has completed to the
  1093. * RCU_DONE_TAIL sublist, then compact the remaining sublists and
  1094. * assign ->gp_seq numbers to any callbacks in the RCU_NEXT_TAIL
  1095. * sublist. This function is idempotent, so it does not hurt to
  1096. * invoke it repeatedly. As long as it is not invoked -too- often...
  1097. * Returns true if the RCU grace-period kthread needs to be awakened.
  1098. *
  1099. * The caller must hold rnp->lock with interrupts disabled.
  1100. */
  1101. static bool rcu_advance_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
  1102. {
  1103. rcu_lockdep_assert_cblist_protected(rdp);
  1104. raw_lockdep_assert_held_rcu_node(rnp);
  1105. /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
  1106. if (!rcu_segcblist_pend_cbs(&rdp->cblist))
  1107. return false;
  1108. /*
  1109. * Find all callbacks whose ->gp_seq numbers indicate that they
  1110. * are ready to invoke, and put them into the RCU_DONE_TAIL sublist.
  1111. */
  1112. rcu_segcblist_advance(&rdp->cblist, rnp->gp_seq);
  1113. /* Classify any remaining callbacks. */
  1114. return rcu_accelerate_cbs(rnp, rdp);
  1115. }
  1116. /*
  1117. * Move and classify callbacks, but only if doing so won't require
  1118. * that the RCU grace-period kthread be awakened.
  1119. */
  1120. static void __maybe_unused rcu_advance_cbs_nowake(struct rcu_node *rnp,
  1121. struct rcu_data *rdp)
  1122. {
  1123. rcu_lockdep_assert_cblist_protected(rdp);
  1124. if (!rcu_seq_state(rcu_seq_current(&rnp->gp_seq)) || !raw_spin_trylock_rcu_node(rnp))
  1125. return;
  1126. // The grace period cannot end while we hold the rcu_node lock.
  1127. if (rcu_seq_state(rcu_seq_current(&rnp->gp_seq)))
  1128. WARN_ON_ONCE(rcu_advance_cbs(rnp, rdp));
  1129. raw_spin_unlock_rcu_node(rnp);
  1130. }
  1131. /*
  1132. * In CONFIG_RCU_STRICT_GRACE_PERIOD=y kernels, attempt to generate a
  1133. * quiescent state. This is intended to be invoked when the CPU notices
  1134. * a new grace period.
  1135. */
  1136. static void rcu_strict_gp_check_qs(void)
  1137. {
  1138. if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD)) {
  1139. rcu_read_lock();
  1140. rcu_read_unlock();
  1141. }
  1142. }
  1143. /*
  1144. * Update CPU-local rcu_data state to record the beginnings and ends of
  1145. * grace periods. The caller must hold the ->lock of the leaf rcu_node
  1146. * structure corresponding to the current CPU, and must have irqs disabled.
  1147. * Returns true if the grace-period kthread needs to be awakened.
  1148. */
  1149. static bool __note_gp_changes(struct rcu_node *rnp, struct rcu_data *rdp)
  1150. {
  1151. bool ret = false;
  1152. bool need_qs;
  1153. const bool offloaded = rcu_rdp_is_offloaded(rdp);
  1154. raw_lockdep_assert_held_rcu_node(rnp);
  1155. if (rdp->gp_seq == rnp->gp_seq)
  1156. return false; /* Nothing to do. */
  1157. /* Handle the ends of any preceding grace periods first. */
  1158. if (rcu_seq_completed_gp(rdp->gp_seq, rnp->gp_seq) ||
  1159. unlikely(READ_ONCE(rdp->gpwrap))) {
  1160. if (!offloaded)
  1161. ret = rcu_advance_cbs(rnp, rdp); /* Advance CBs. */
  1162. rdp->core_needs_qs = false;
  1163. trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuend"));
  1164. } else {
  1165. if (!offloaded)
  1166. ret = rcu_accelerate_cbs(rnp, rdp); /* Recent CBs. */
  1167. if (rdp->core_needs_qs)
  1168. rdp->core_needs_qs = !!(rnp->qsmask & rdp->grpmask);
  1169. }
  1170. /* Now handle the beginnings of any new-to-this-CPU grace periods. */
  1171. if (rcu_seq_new_gp(rdp->gp_seq, rnp->gp_seq) ||
  1172. unlikely(READ_ONCE(rdp->gpwrap))) {
  1173. /*
  1174. * If the current grace period is waiting for this CPU,
  1175. * set up to detect a quiescent state, otherwise don't
  1176. * go looking for one.
  1177. */
  1178. trace_rcu_grace_period(rcu_state.name, rnp->gp_seq, TPS("cpustart"));
  1179. need_qs = !!(rnp->qsmask & rdp->grpmask);
  1180. rdp->cpu_no_qs.b.norm = need_qs;
  1181. rdp->core_needs_qs = need_qs;
  1182. zero_cpu_stall_ticks(rdp);
  1183. }
  1184. rdp->gp_seq = rnp->gp_seq; /* Remember new grace-period state. */
  1185. if (ULONG_CMP_LT(rdp->gp_seq_needed, rnp->gp_seq_needed) || rdp->gpwrap)
  1186. WRITE_ONCE(rdp->gp_seq_needed, rnp->gp_seq_needed);
  1187. if (IS_ENABLED(CONFIG_PROVE_RCU) && READ_ONCE(rdp->gpwrap))
  1188. WRITE_ONCE(rdp->last_sched_clock, jiffies);
  1189. WRITE_ONCE(rdp->gpwrap, false);
  1190. rcu_gpnum_ovf(rnp, rdp);
  1191. return ret;
  1192. }
  1193. static void note_gp_changes(struct rcu_data *rdp)
  1194. {
  1195. unsigned long flags;
  1196. bool needwake;
  1197. struct rcu_node *rnp;
  1198. local_irq_save(flags);
  1199. rnp = rdp->mynode;
  1200. if ((rdp->gp_seq == rcu_seq_current(&rnp->gp_seq) &&
  1201. !unlikely(READ_ONCE(rdp->gpwrap))) || /* w/out lock. */
  1202. !raw_spin_trylock_rcu_node(rnp)) { /* irqs already off, so later. */
  1203. local_irq_restore(flags);
  1204. return;
  1205. }
  1206. needwake = __note_gp_changes(rnp, rdp);
  1207. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1208. rcu_strict_gp_check_qs();
  1209. if (needwake)
  1210. rcu_gp_kthread_wake();
  1211. }
  1212. static atomic_t *rcu_gp_slow_suppress;
  1213. /* Register a counter to suppress debugging grace-period delays. */
  1214. void rcu_gp_slow_register(atomic_t *rgssp)
  1215. {
  1216. WARN_ON_ONCE(rcu_gp_slow_suppress);
  1217. WRITE_ONCE(rcu_gp_slow_suppress, rgssp);
  1218. }
  1219. EXPORT_SYMBOL_GPL(rcu_gp_slow_register);
  1220. /* Unregister a counter, with NULL for not caring which. */
  1221. void rcu_gp_slow_unregister(atomic_t *rgssp)
  1222. {
  1223. WARN_ON_ONCE(rgssp && rgssp != rcu_gp_slow_suppress && rcu_gp_slow_suppress != NULL);
  1224. WRITE_ONCE(rcu_gp_slow_suppress, NULL);
  1225. }
  1226. EXPORT_SYMBOL_GPL(rcu_gp_slow_unregister);
  1227. static bool rcu_gp_slow_is_suppressed(void)
  1228. {
  1229. atomic_t *rgssp = READ_ONCE(rcu_gp_slow_suppress);
  1230. return rgssp && atomic_read(rgssp);
  1231. }
  1232. static void rcu_gp_slow(int delay)
  1233. {
  1234. if (!rcu_gp_slow_is_suppressed() && delay > 0 &&
  1235. !(rcu_seq_ctr(rcu_state.gp_seq) % (rcu_num_nodes * PER_RCU_NODE_PERIOD * delay)))
  1236. schedule_timeout_idle(delay);
  1237. }
  1238. static unsigned long sleep_duration;
  1239. /* Allow rcutorture to stall the grace-period kthread. */
  1240. void rcu_gp_set_torture_wait(int duration)
  1241. {
  1242. if (IS_ENABLED(CONFIG_RCU_TORTURE_TEST) && duration > 0)
  1243. WRITE_ONCE(sleep_duration, duration);
  1244. }
  1245. EXPORT_SYMBOL_GPL(rcu_gp_set_torture_wait);
  1246. /* Actually implement the aforementioned wait. */
  1247. static void rcu_gp_torture_wait(void)
  1248. {
  1249. unsigned long duration;
  1250. if (!IS_ENABLED(CONFIG_RCU_TORTURE_TEST))
  1251. return;
  1252. duration = xchg(&sleep_duration, 0UL);
  1253. if (duration > 0) {
  1254. pr_alert("%s: Waiting %lu jiffies\n", __func__, duration);
  1255. schedule_timeout_idle(duration);
  1256. pr_alert("%s: Wait complete\n", __func__);
  1257. }
  1258. }
  1259. /*
  1260. * Handler for on_each_cpu() to invoke the target CPU's RCU core
  1261. * processing.
  1262. */
  1263. static void rcu_strict_gp_boundary(void *unused)
  1264. {
  1265. invoke_rcu_core();
  1266. }
  1267. // Make the polled API aware of the beginning of a grace period.
  1268. static void rcu_poll_gp_seq_start(unsigned long *snap)
  1269. {
  1270. struct rcu_node *rnp = rcu_get_root();
  1271. if (rcu_scheduler_active != RCU_SCHEDULER_INACTIVE)
  1272. raw_lockdep_assert_held_rcu_node(rnp);
  1273. // If RCU was idle, note beginning of GP.
  1274. if (!rcu_seq_state(rcu_state.gp_seq_polled))
  1275. rcu_seq_start(&rcu_state.gp_seq_polled);
  1276. // Either way, record current state.
  1277. *snap = rcu_state.gp_seq_polled;
  1278. }
  1279. // Make the polled API aware of the end of a grace period.
  1280. static void rcu_poll_gp_seq_end(unsigned long *snap)
  1281. {
  1282. struct rcu_node *rnp = rcu_get_root();
  1283. if (rcu_scheduler_active != RCU_SCHEDULER_INACTIVE)
  1284. raw_lockdep_assert_held_rcu_node(rnp);
  1285. // If the previously noted GP is still in effect, record the
  1286. // end of that GP. Either way, zero counter to avoid counter-wrap
  1287. // problems.
  1288. if (*snap && *snap == rcu_state.gp_seq_polled) {
  1289. rcu_seq_end(&rcu_state.gp_seq_polled);
  1290. rcu_state.gp_seq_polled_snap = 0;
  1291. rcu_state.gp_seq_polled_exp_snap = 0;
  1292. } else {
  1293. *snap = 0;
  1294. }
  1295. }
  1296. // Make the polled API aware of the beginning of a grace period, but
  1297. // where caller does not hold the root rcu_node structure's lock.
  1298. static void rcu_poll_gp_seq_start_unlocked(unsigned long *snap)
  1299. {
  1300. unsigned long flags;
  1301. struct rcu_node *rnp = rcu_get_root();
  1302. if (rcu_init_invoked()) {
  1303. if (rcu_scheduler_active != RCU_SCHEDULER_INACTIVE)
  1304. lockdep_assert_irqs_enabled();
  1305. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1306. }
  1307. rcu_poll_gp_seq_start(snap);
  1308. if (rcu_init_invoked())
  1309. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1310. }
  1311. // Make the polled API aware of the end of a grace period, but where
  1312. // caller does not hold the root rcu_node structure's lock.
  1313. static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap)
  1314. {
  1315. unsigned long flags;
  1316. struct rcu_node *rnp = rcu_get_root();
  1317. if (rcu_init_invoked()) {
  1318. if (rcu_scheduler_active != RCU_SCHEDULER_INACTIVE)
  1319. lockdep_assert_irqs_enabled();
  1320. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1321. }
  1322. rcu_poll_gp_seq_end(snap);
  1323. if (rcu_init_invoked())
  1324. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  1325. }
  1326. /*
  1327. * There is a single llist, which is used for handling
  1328. * synchronize_rcu() users' enqueued rcu_synchronize nodes.
  1329. * Within this llist, there are two tail pointers:
  1330. *
  1331. * wait tail: Tracks the set of nodes, which need to
  1332. * wait for the current GP to complete.
  1333. * done tail: Tracks the set of nodes, for which grace
  1334. * period has elapsed. These nodes processing
  1335. * will be done as part of the cleanup work
  1336. * execution by a kworker.
  1337. *
  1338. * At every grace period init, a new wait node is added
  1339. * to the llist. This wait node is used as wait tail
  1340. * for this new grace period. Given that there are a fixed
  1341. * number of wait nodes, if all wait nodes are in use
  1342. * (which can happen when kworker callback processing
  1343. * is delayed) and additional grace period is requested.
  1344. * This means, a system is slow in processing callbacks.
  1345. *
  1346. * TODO: If a slow processing is detected, a first node
  1347. * in the llist should be used as a wait-tail for this
  1348. * grace period, therefore users which should wait due
  1349. * to a slow process are handled by _this_ grace period
  1350. * and not next.
  1351. *
  1352. * Below is an illustration of how the done and wait
  1353. * tail pointers move from one set of rcu_synchronize nodes
  1354. * to the other, as grace periods start and finish and
  1355. * nodes are processed by kworker.
  1356. *
  1357. *
  1358. * a. Initial llist callbacks list:
  1359. *
  1360. * +----------+ +--------+ +-------+
  1361. * | | | | | |
  1362. * | head |---------> | cb2 |--------->| cb1 |
  1363. * | | | | | |
  1364. * +----------+ +--------+ +-------+
  1365. *
  1366. *
  1367. *
  1368. * b. New GP1 Start:
  1369. *
  1370. * WAIT TAIL
  1371. * |
  1372. * |
  1373. * v
  1374. * +----------+ +--------+ +--------+ +-------+
  1375. * | | | | | | | |
  1376. * | head ------> wait |------> cb2 |------> | cb1 |
  1377. * | | | head1 | | | | |
  1378. * +----------+ +--------+ +--------+ +-------+
  1379. *
  1380. *
  1381. *
  1382. * c. GP completion:
  1383. *
  1384. * WAIT_TAIL == DONE_TAIL
  1385. *
  1386. * DONE TAIL
  1387. * |
  1388. * |
  1389. * v
  1390. * +----------+ +--------+ +--------+ +-------+
  1391. * | | | | | | | |
  1392. * | head ------> wait |------> cb2 |------> | cb1 |
  1393. * | | | head1 | | | | |
  1394. * +----------+ +--------+ +--------+ +-------+
  1395. *
  1396. *
  1397. *
  1398. * d. New callbacks and GP2 start:
  1399. *
  1400. * WAIT TAIL DONE TAIL
  1401. * | |
  1402. * | |
  1403. * v v
  1404. * +----------+ +------+ +------+ +------+ +-----+ +-----+ +-----+
  1405. * | | | | | | | | | | | | | |
  1406. * | head ------> wait |--->| cb4 |--->| cb3 |--->|wait |--->| cb2 |--->| cb1 |
  1407. * | | | head2| | | | | |head1| | | | |
  1408. * +----------+ +------+ +------+ +------+ +-----+ +-----+ +-----+
  1409. *
  1410. *
  1411. *
  1412. * e. GP2 completion:
  1413. *
  1414. * WAIT_TAIL == DONE_TAIL
  1415. * DONE TAIL
  1416. * |
  1417. * |
  1418. * v
  1419. * +----------+ +------+ +------+ +------+ +-----+ +-----+ +-----+
  1420. * | | | | | | | | | | | | | |
  1421. * | head ------> wait |--->| cb4 |--->| cb3 |--->|wait |--->| cb2 |--->| cb1 |
  1422. * | | | head2| | | | | |head1| | | | |
  1423. * +----------+ +------+ +------+ +------+ +-----+ +-----+ +-----+
  1424. *
  1425. *
  1426. * While the llist state transitions from d to e, a kworker
  1427. * can start executing rcu_sr_normal_gp_cleanup_work() and
  1428. * can observe either the old done tail (@c) or the new
  1429. * done tail (@e). So, done tail updates and reads need
  1430. * to use the rel-acq semantics. If the concurrent kworker
  1431. * observes the old done tail, the newly queued work
  1432. * execution will process the updated done tail. If the
  1433. * concurrent kworker observes the new done tail, then
  1434. * the newly queued work will skip processing the done
  1435. * tail, as workqueue semantics guarantees that the new
  1436. * work is executed only after the previous one completes.
  1437. *
  1438. * f. kworker callbacks processing complete:
  1439. *
  1440. *
  1441. * DONE TAIL
  1442. * |
  1443. * |
  1444. * v
  1445. * +----------+ +--------+
  1446. * | | | |
  1447. * | head ------> wait |
  1448. * | | | head2 |
  1449. * +----------+ +--------+
  1450. *
  1451. */
  1452. static bool rcu_sr_is_wait_head(struct llist_node *node)
  1453. {
  1454. return &(rcu_state.srs_wait_nodes)[0].node <= node &&
  1455. node <= &(rcu_state.srs_wait_nodes)[SR_NORMAL_GP_WAIT_HEAD_MAX - 1].node;
  1456. }
  1457. static struct llist_node *rcu_sr_get_wait_head(void)
  1458. {
  1459. struct sr_wait_node *sr_wn;
  1460. int i;
  1461. for (i = 0; i < SR_NORMAL_GP_WAIT_HEAD_MAX; i++) {
  1462. sr_wn = &(rcu_state.srs_wait_nodes)[i];
  1463. if (!atomic_cmpxchg_acquire(&sr_wn->inuse, 0, 1))
  1464. return &sr_wn->node;
  1465. }
  1466. return NULL;
  1467. }
  1468. static void rcu_sr_put_wait_head(struct llist_node *node)
  1469. {
  1470. struct sr_wait_node *sr_wn = container_of(node, struct sr_wait_node, node);
  1471. atomic_set_release(&sr_wn->inuse, 0);
  1472. }
  1473. /* Disabled by default. */
  1474. static int rcu_normal_wake_from_gp;
  1475. module_param(rcu_normal_wake_from_gp, int, 0644);
  1476. static struct workqueue_struct *sync_wq;
  1477. static void rcu_sr_normal_complete(struct llist_node *node)
  1478. {
  1479. struct rcu_synchronize *rs = container_of(
  1480. (struct rcu_head *) node, struct rcu_synchronize, head);
  1481. unsigned long oldstate = (unsigned long) rs->head.func;
  1482. WARN_ONCE(IS_ENABLED(CONFIG_PROVE_RCU) &&
  1483. !poll_state_synchronize_rcu(oldstate),
  1484. "A full grace period is not passed yet: %lu",
  1485. rcu_seq_diff(get_state_synchronize_rcu(), oldstate));
  1486. /* Finally. */
  1487. complete(&rs->completion);
  1488. }
  1489. static void rcu_sr_normal_gp_cleanup_work(struct work_struct *work)
  1490. {
  1491. struct llist_node *done, *rcu, *next, *head;
  1492. /*
  1493. * This work execution can potentially execute
  1494. * while a new done tail is being updated by
  1495. * grace period kthread in rcu_sr_normal_gp_cleanup().
  1496. * So, read and updates of done tail need to
  1497. * follow acq-rel semantics.
  1498. *
  1499. * Given that wq semantics guarantees that a single work
  1500. * cannot execute concurrently by multiple kworkers,
  1501. * the done tail list manipulations are protected here.
  1502. */
  1503. done = smp_load_acquire(&rcu_state.srs_done_tail);
  1504. if (WARN_ON_ONCE(!done))
  1505. return;
  1506. WARN_ON_ONCE(!rcu_sr_is_wait_head(done));
  1507. head = done->next;
  1508. done->next = NULL;
  1509. /*
  1510. * The dummy node, which is pointed to by the
  1511. * done tail which is acq-read above is not removed
  1512. * here. This allows lockless additions of new
  1513. * rcu_synchronize nodes in rcu_sr_normal_add_req(),
  1514. * while the cleanup work executes. The dummy
  1515. * nodes is removed, in next round of cleanup
  1516. * work execution.
  1517. */
  1518. llist_for_each_safe(rcu, next, head) {
  1519. if (!rcu_sr_is_wait_head(rcu)) {
  1520. rcu_sr_normal_complete(rcu);
  1521. continue;
  1522. }
  1523. rcu_sr_put_wait_head(rcu);
  1524. }
  1525. /* Order list manipulations with atomic access. */
  1526. atomic_dec_return_release(&rcu_state.srs_cleanups_pending);
  1527. }
  1528. /*
  1529. * Helper function for rcu_gp_cleanup().
  1530. */
  1531. static void rcu_sr_normal_gp_cleanup(void)
  1532. {
  1533. struct llist_node *wait_tail, *next = NULL, *rcu = NULL;
  1534. int done = 0;
  1535. wait_tail = rcu_state.srs_wait_tail;
  1536. if (wait_tail == NULL)
  1537. return;
  1538. rcu_state.srs_wait_tail = NULL;
  1539. ASSERT_EXCLUSIVE_WRITER(rcu_state.srs_wait_tail);
  1540. WARN_ON_ONCE(!rcu_sr_is_wait_head(wait_tail));
  1541. /*
  1542. * Process (a) and (d) cases. See an illustration.
  1543. */
  1544. llist_for_each_safe(rcu, next, wait_tail->next) {
  1545. if (rcu_sr_is_wait_head(rcu))
  1546. break;
  1547. rcu_sr_normal_complete(rcu);
  1548. // It can be last, update a next on this step.
  1549. wait_tail->next = next;
  1550. if (++done == SR_MAX_USERS_WAKE_FROM_GP)
  1551. break;
  1552. }
  1553. /*
  1554. * Fast path, no more users to process except putting the second last
  1555. * wait head if no inflight-workers. If there are in-flight workers,
  1556. * they will remove the last wait head.
  1557. *
  1558. * Note that the ACQUIRE orders atomic access with list manipulation.
  1559. */
  1560. if (wait_tail->next && wait_tail->next->next == NULL &&
  1561. rcu_sr_is_wait_head(wait_tail->next) &&
  1562. !atomic_read_acquire(&rcu_state.srs_cleanups_pending)) {
  1563. rcu_sr_put_wait_head(wait_tail->next);
  1564. wait_tail->next = NULL;
  1565. }
  1566. /* Concurrent sr_normal_gp_cleanup work might observe this update. */
  1567. ASSERT_EXCLUSIVE_WRITER(rcu_state.srs_done_tail);
  1568. smp_store_release(&rcu_state.srs_done_tail, wait_tail);
  1569. /*
  1570. * We schedule a work in order to perform a final processing
  1571. * of outstanding users(if still left) and releasing wait-heads
  1572. * added by rcu_sr_normal_gp_init() call.
  1573. */
  1574. if (wait_tail->next) {
  1575. atomic_inc(&rcu_state.srs_cleanups_pending);
  1576. if (!queue_work(sync_wq, &rcu_state.srs_cleanup_work))
  1577. atomic_dec(&rcu_state.srs_cleanups_pending);
  1578. }
  1579. }
  1580. /*
  1581. * Helper function for rcu_gp_init().
  1582. */
  1583. static bool rcu_sr_normal_gp_init(void)
  1584. {
  1585. struct llist_node *first;
  1586. struct llist_node *wait_head;
  1587. bool start_new_poll = false;
  1588. first = READ_ONCE(rcu_state.srs_next.first);
  1589. if (!first || rcu_sr_is_wait_head(first))
  1590. return start_new_poll;
  1591. wait_head = rcu_sr_get_wait_head();
  1592. if (!wait_head) {
  1593. // Kick another GP to retry.
  1594. start_new_poll = true;
  1595. return start_new_poll;
  1596. }
  1597. /* Inject a wait-dummy-node. */
  1598. llist_add(wait_head, &rcu_state.srs_next);
  1599. /*
  1600. * A waiting list of rcu_synchronize nodes should be empty on
  1601. * this step, since a GP-kthread, rcu_gp_init() -> gp_cleanup(),
  1602. * rolls it over. If not, it is a BUG, warn a user.
  1603. */
  1604. WARN_ON_ONCE(rcu_state.srs_wait_tail != NULL);
  1605. rcu_state.srs_wait_tail = wait_head;
  1606. ASSERT_EXCLUSIVE_WRITER(rcu_state.srs_wait_tail);
  1607. return start_new_poll;
  1608. }
  1609. static void rcu_sr_normal_add_req(struct rcu_synchronize *rs)
  1610. {
  1611. llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next);
  1612. }
  1613. /*
  1614. * Initialize a new grace period. Return false if no grace period required.
  1615. */
  1616. static noinline_for_stack bool rcu_gp_init(void)
  1617. {
  1618. unsigned long flags;
  1619. unsigned long oldmask;
  1620. unsigned long mask;
  1621. struct rcu_data *rdp;
  1622. struct rcu_node *rnp = rcu_get_root();
  1623. bool start_new_poll;
  1624. WRITE_ONCE(rcu_state.gp_activity, jiffies);
  1625. raw_spin_lock_irq_rcu_node(rnp);
  1626. if (!rcu_state.gp_flags) {
  1627. /* Spurious wakeup, tell caller to go back to sleep. */
  1628. raw_spin_unlock_irq_rcu_node(rnp);
  1629. return false;
  1630. }
  1631. WRITE_ONCE(rcu_state.gp_flags, 0); /* Clear all flags: New GP. */
  1632. if (WARN_ON_ONCE(rcu_gp_in_progress())) {
  1633. /*
  1634. * Grace period already in progress, don't start another.
  1635. * Not supposed to be able to happen.
  1636. */
  1637. raw_spin_unlock_irq_rcu_node(rnp);
  1638. return false;
  1639. }
  1640. /* Advance to a new grace period and initialize state. */
  1641. record_gp_stall_check_time();
  1642. /*
  1643. * A new wait segment must be started before gp_seq advanced, so
  1644. * that previous gp waiters won't observe the new gp_seq.
  1645. */
  1646. start_new_poll = rcu_sr_normal_gp_init();
  1647. /* Record GP times before starting GP, hence rcu_seq_start(). */
  1648. rcu_seq_start(&rcu_state.gp_seq);
  1649. ASSERT_EXCLUSIVE_WRITER(rcu_state.gp_seq);
  1650. trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("start"));
  1651. rcu_poll_gp_seq_start(&rcu_state.gp_seq_polled_snap);
  1652. raw_spin_unlock_irq_rcu_node(rnp);
  1653. /*
  1654. * The "start_new_poll" is set to true, only when this GP is not able
  1655. * to handle anything and there are outstanding users. It happens when
  1656. * the rcu_sr_normal_gp_init() function was not able to insert a dummy
  1657. * separator to the llist, because there were no left any dummy-nodes.
  1658. *
  1659. * Number of dummy-nodes is fixed, it could be that we are run out of
  1660. * them, if so we start a new pool request to repeat a try. It is rare
  1661. * and it means that a system is doing a slow processing of callbacks.
  1662. */
  1663. if (start_new_poll)
  1664. (void) start_poll_synchronize_rcu();
  1665. /*
  1666. * Apply per-leaf buffered online and offline operations to
  1667. * the rcu_node tree. Note that this new grace period need not
  1668. * wait for subsequent online CPUs, and that RCU hooks in the CPU
  1669. * offlining path, when combined with checks in this function,
  1670. * will handle CPUs that are currently going offline or that will
  1671. * go offline later. Please also refer to "Hotplug CPU" section
  1672. * of RCU's Requirements documentation.
  1673. */
  1674. WRITE_ONCE(rcu_state.gp_state, RCU_GP_ONOFF);
  1675. /* Exclude CPU hotplug operations. */
  1676. rcu_for_each_leaf_node(rnp) {
  1677. local_irq_disable();
  1678. arch_spin_lock(&rcu_state.ofl_lock);
  1679. raw_spin_lock_rcu_node(rnp);
  1680. if (rnp->qsmaskinit == rnp->qsmaskinitnext &&
  1681. !rnp->wait_blkd_tasks) {
  1682. /* Nothing to do on this leaf rcu_node structure. */
  1683. raw_spin_unlock_rcu_node(rnp);
  1684. arch_spin_unlock(&rcu_state.ofl_lock);
  1685. local_irq_enable();
  1686. continue;
  1687. }
  1688. /* Record old state, apply changes to ->qsmaskinit field. */
  1689. oldmask = rnp->qsmaskinit;
  1690. rnp->qsmaskinit = rnp->qsmaskinitnext;
  1691. /* If zero-ness of ->qsmaskinit changed, propagate up tree. */
  1692. if (!oldmask != !rnp->qsmaskinit) {
  1693. if (!oldmask) { /* First online CPU for rcu_node. */
  1694. if (!rnp->wait_blkd_tasks) /* Ever offline? */
  1695. rcu_init_new_rnp(rnp);
  1696. } else if (rcu_preempt_has_tasks(rnp)) {
  1697. rnp->wait_blkd_tasks = true; /* blocked tasks */
  1698. } else { /* Last offline CPU and can propagate. */
  1699. rcu_cleanup_dead_rnp(rnp);
  1700. }
  1701. }
  1702. /*
  1703. * If all waited-on tasks from prior grace period are
  1704. * done, and if all this rcu_node structure's CPUs are
  1705. * still offline, propagate up the rcu_node tree and
  1706. * clear ->wait_blkd_tasks. Otherwise, if one of this
  1707. * rcu_node structure's CPUs has since come back online,
  1708. * simply clear ->wait_blkd_tasks.
  1709. */
  1710. if (rnp->wait_blkd_tasks &&
  1711. (!rcu_preempt_has_tasks(rnp) || rnp->qsmaskinit)) {
  1712. rnp->wait_blkd_tasks = false;
  1713. if (!rnp->qsmaskinit)
  1714. rcu_cleanup_dead_rnp(rnp);
  1715. }
  1716. raw_spin_unlock_rcu_node(rnp);
  1717. arch_spin_unlock(&rcu_state.ofl_lock);
  1718. local_irq_enable();
  1719. }
  1720. rcu_gp_slow(gp_preinit_delay); /* Races with CPU hotplug. */
  1721. /*
  1722. * Set the quiescent-state-needed bits in all the rcu_node
  1723. * structures for all currently online CPUs in breadth-first
  1724. * order, starting from the root rcu_node structure, relying on the
  1725. * layout of the tree within the rcu_state.node[] array. Note that
  1726. * other CPUs will access only the leaves of the hierarchy, thus
  1727. * seeing that no grace period is in progress, at least until the
  1728. * corresponding leaf node has been initialized.
  1729. *
  1730. * The grace period cannot complete until the initialization
  1731. * process finishes, because this kthread handles both.
  1732. */
  1733. WRITE_ONCE(rcu_state.gp_state, RCU_GP_INIT);
  1734. rcu_for_each_node_breadth_first(rnp) {
  1735. rcu_gp_slow(gp_init_delay);
  1736. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  1737. rdp = this_cpu_ptr(&rcu_data);
  1738. rcu_preempt_check_blocked_tasks(rnp);
  1739. rnp->qsmask = rnp->qsmaskinit;
  1740. WRITE_ONCE(rnp->gp_seq, rcu_state.gp_seq);
  1741. if (rnp == rdp->mynode)
  1742. (void)__note_gp_changes(rnp, rdp);
  1743. rcu_preempt_boost_start_gp(rnp);
  1744. trace_rcu_grace_period_init(rcu_state.name, rnp->gp_seq,
  1745. rnp->level, rnp->grplo,
  1746. rnp->grphi, rnp->qsmask);
  1747. /* Quiescent states for tasks on any now-offline CPUs. */
  1748. mask = rnp->qsmask & ~rnp->qsmaskinitnext;
  1749. rnp->rcu_gp_init_mask = mask;
  1750. if ((mask || rnp->wait_blkd_tasks) && rcu_is_leaf_node(rnp))
  1751. rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
  1752. else
  1753. raw_spin_unlock_irq_rcu_node(rnp);
  1754. cond_resched_tasks_rcu_qs();
  1755. WRITE_ONCE(rcu_state.gp_activity, jiffies);
  1756. }
  1757. // If strict, make all CPUs aware of new grace period.
  1758. if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
  1759. on_each_cpu(rcu_strict_gp_boundary, NULL, 0);
  1760. return true;
  1761. }
  1762. /*
  1763. * Helper function for swait_event_idle_exclusive() wakeup at force-quiescent-state
  1764. * time.
  1765. */
  1766. static bool rcu_gp_fqs_check_wake(int *gfp)
  1767. {
  1768. struct rcu_node *rnp = rcu_get_root();
  1769. // If under overload conditions, force an immediate FQS scan.
  1770. if (*gfp & RCU_GP_FLAG_OVLD)
  1771. return true;
  1772. // Someone like call_rcu() requested a force-quiescent-state scan.
  1773. *gfp = READ_ONCE(rcu_state.gp_flags);
  1774. if (*gfp & RCU_GP_FLAG_FQS)
  1775. return true;
  1776. // The current grace period has completed.
  1777. if (!READ_ONCE(rnp->qsmask) && !rcu_preempt_blocked_readers_cgp(rnp))
  1778. return true;
  1779. return false;
  1780. }
  1781. /*
  1782. * Do one round of quiescent-state forcing.
  1783. */
  1784. static void rcu_gp_fqs(bool first_time)
  1785. {
  1786. int nr_fqs = READ_ONCE(rcu_state.nr_fqs_jiffies_stall);
  1787. struct rcu_node *rnp = rcu_get_root();
  1788. WRITE_ONCE(rcu_state.gp_activity, jiffies);
  1789. WRITE_ONCE(rcu_state.n_force_qs, rcu_state.n_force_qs + 1);
  1790. WARN_ON_ONCE(nr_fqs > 3);
  1791. /* Only countdown nr_fqs for stall purposes if jiffies moves. */
  1792. if (nr_fqs) {
  1793. if (nr_fqs == 1) {
  1794. WRITE_ONCE(rcu_state.jiffies_stall,
  1795. jiffies + rcu_jiffies_till_stall_check());
  1796. }
  1797. WRITE_ONCE(rcu_state.nr_fqs_jiffies_stall, --nr_fqs);
  1798. }
  1799. if (first_time) {
  1800. /* Collect dyntick-idle snapshots. */
  1801. force_qs_rnp(rcu_watching_snap_save);
  1802. } else {
  1803. /* Handle dyntick-idle and offline CPUs. */
  1804. force_qs_rnp(rcu_watching_snap_recheck);
  1805. }
  1806. /* Clear flag to prevent immediate re-entry. */
  1807. if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) {
  1808. raw_spin_lock_irq_rcu_node(rnp);
  1809. WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags & ~RCU_GP_FLAG_FQS);
  1810. raw_spin_unlock_irq_rcu_node(rnp);
  1811. }
  1812. }
  1813. /*
  1814. * Loop doing repeated quiescent-state forcing until the grace period ends.
  1815. */
  1816. static noinline_for_stack void rcu_gp_fqs_loop(void)
  1817. {
  1818. bool first_gp_fqs = true;
  1819. int gf = 0;
  1820. unsigned long j;
  1821. int ret;
  1822. struct rcu_node *rnp = rcu_get_root();
  1823. j = READ_ONCE(jiffies_till_first_fqs);
  1824. if (rcu_state.cbovld)
  1825. gf = RCU_GP_FLAG_OVLD;
  1826. ret = 0;
  1827. for (;;) {
  1828. if (rcu_state.cbovld) {
  1829. j = (j + 2) / 3;
  1830. if (j <= 0)
  1831. j = 1;
  1832. }
  1833. if (!ret || time_before(jiffies + j, rcu_state.jiffies_force_qs)) {
  1834. WRITE_ONCE(rcu_state.jiffies_force_qs, jiffies + j);
  1835. /*
  1836. * jiffies_force_qs before RCU_GP_WAIT_FQS state
  1837. * update; required for stall checks.
  1838. */
  1839. smp_wmb();
  1840. WRITE_ONCE(rcu_state.jiffies_kick_kthreads,
  1841. jiffies + (j ? 3 * j : 2));
  1842. }
  1843. trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
  1844. TPS("fqswait"));
  1845. WRITE_ONCE(rcu_state.gp_state, RCU_GP_WAIT_FQS);
  1846. (void)swait_event_idle_timeout_exclusive(rcu_state.gp_wq,
  1847. rcu_gp_fqs_check_wake(&gf), j);
  1848. rcu_gp_torture_wait();
  1849. WRITE_ONCE(rcu_state.gp_state, RCU_GP_DOING_FQS);
  1850. /* Locking provides needed memory barriers. */
  1851. /*
  1852. * Exit the loop if the root rcu_node structure indicates that the grace period
  1853. * has ended, leave the loop. The rcu_preempt_blocked_readers_cgp(rnp) check
  1854. * is required only for single-node rcu_node trees because readers blocking
  1855. * the current grace period are queued only on leaf rcu_node structures.
  1856. * For multi-node trees, checking the root node's ->qsmask suffices, because a
  1857. * given root node's ->qsmask bit is cleared only when all CPUs and tasks from
  1858. * the corresponding leaf nodes have passed through their quiescent state.
  1859. */
  1860. if (!READ_ONCE(rnp->qsmask) &&
  1861. !rcu_preempt_blocked_readers_cgp(rnp))
  1862. break;
  1863. /* If time for quiescent-state forcing, do it. */
  1864. if (!time_after(rcu_state.jiffies_force_qs, jiffies) ||
  1865. (gf & (RCU_GP_FLAG_FQS | RCU_GP_FLAG_OVLD))) {
  1866. trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
  1867. TPS("fqsstart"));
  1868. rcu_gp_fqs(first_gp_fqs);
  1869. gf = 0;
  1870. if (first_gp_fqs) {
  1871. first_gp_fqs = false;
  1872. gf = rcu_state.cbovld ? RCU_GP_FLAG_OVLD : 0;
  1873. }
  1874. trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
  1875. TPS("fqsend"));
  1876. cond_resched_tasks_rcu_qs();
  1877. WRITE_ONCE(rcu_state.gp_activity, jiffies);
  1878. ret = 0; /* Force full wait till next FQS. */
  1879. j = READ_ONCE(jiffies_till_next_fqs);
  1880. } else {
  1881. /* Deal with stray signal. */
  1882. cond_resched_tasks_rcu_qs();
  1883. WRITE_ONCE(rcu_state.gp_activity, jiffies);
  1884. WARN_ON(signal_pending(current));
  1885. trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
  1886. TPS("fqswaitsig"));
  1887. ret = 1; /* Keep old FQS timing. */
  1888. j = jiffies;
  1889. if (time_after(jiffies, rcu_state.jiffies_force_qs))
  1890. j = 1;
  1891. else
  1892. j = rcu_state.jiffies_force_qs - j;
  1893. gf = 0;
  1894. }
  1895. }
  1896. }
  1897. /*
  1898. * Clean up after the old grace period.
  1899. */
  1900. static noinline void rcu_gp_cleanup(void)
  1901. {
  1902. int cpu;
  1903. bool needgp = false;
  1904. unsigned long gp_duration;
  1905. unsigned long new_gp_seq;
  1906. bool offloaded;
  1907. struct rcu_data *rdp;
  1908. struct rcu_node *rnp = rcu_get_root();
  1909. struct swait_queue_head *sq;
  1910. WRITE_ONCE(rcu_state.gp_activity, jiffies);
  1911. raw_spin_lock_irq_rcu_node(rnp);
  1912. rcu_state.gp_end = jiffies;
  1913. gp_duration = rcu_state.gp_end - rcu_state.gp_start;
  1914. if (gp_duration > rcu_state.gp_max)
  1915. rcu_state.gp_max = gp_duration;
  1916. /*
  1917. * We know the grace period is complete, but to everyone else
  1918. * it appears to still be ongoing. But it is also the case
  1919. * that to everyone else it looks like there is nothing that
  1920. * they can do to advance the grace period. It is therefore
  1921. * safe for us to drop the lock in order to mark the grace
  1922. * period as completed in all of the rcu_node structures.
  1923. */
  1924. rcu_poll_gp_seq_end(&rcu_state.gp_seq_polled_snap);
  1925. raw_spin_unlock_irq_rcu_node(rnp);
  1926. /*
  1927. * Propagate new ->gp_seq value to rcu_node structures so that
  1928. * other CPUs don't have to wait until the start of the next grace
  1929. * period to process their callbacks. This also avoids some nasty
  1930. * RCU grace-period initialization races by forcing the end of
  1931. * the current grace period to be completely recorded in all of
  1932. * the rcu_node structures before the beginning of the next grace
  1933. * period is recorded in any of the rcu_node structures.
  1934. */
  1935. new_gp_seq = rcu_state.gp_seq;
  1936. rcu_seq_end(&new_gp_seq);
  1937. rcu_for_each_node_breadth_first(rnp) {
  1938. raw_spin_lock_irq_rcu_node(rnp);
  1939. if (WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)))
  1940. dump_blkd_tasks(rnp, 10);
  1941. WARN_ON_ONCE(rnp->qsmask);
  1942. WRITE_ONCE(rnp->gp_seq, new_gp_seq);
  1943. if (!rnp->parent)
  1944. smp_mb(); // Order against failing poll_state_synchronize_rcu_full().
  1945. rdp = this_cpu_ptr(&rcu_data);
  1946. if (rnp == rdp->mynode)
  1947. needgp = __note_gp_changes(rnp, rdp) || needgp;
  1948. /* smp_mb() provided by prior unlock-lock pair. */
  1949. needgp = rcu_future_gp_cleanup(rnp) || needgp;
  1950. // Reset overload indication for CPUs no longer overloaded
  1951. if (rcu_is_leaf_node(rnp))
  1952. for_each_leaf_node_cpu_mask(rnp, cpu, rnp->cbovldmask) {
  1953. rdp = per_cpu_ptr(&rcu_data, cpu);
  1954. check_cb_ovld_locked(rdp, rnp);
  1955. }
  1956. sq = rcu_nocb_gp_get(rnp);
  1957. raw_spin_unlock_irq_rcu_node(rnp);
  1958. rcu_nocb_gp_cleanup(sq);
  1959. cond_resched_tasks_rcu_qs();
  1960. WRITE_ONCE(rcu_state.gp_activity, jiffies);
  1961. rcu_gp_slow(gp_cleanup_delay);
  1962. }
  1963. rnp = rcu_get_root();
  1964. raw_spin_lock_irq_rcu_node(rnp); /* GP before ->gp_seq update. */
  1965. /* Declare grace period done, trace first to use old GP number. */
  1966. trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("end"));
  1967. rcu_seq_end(&rcu_state.gp_seq);
  1968. ASSERT_EXCLUSIVE_WRITER(rcu_state.gp_seq);
  1969. WRITE_ONCE(rcu_state.gp_state, RCU_GP_IDLE);
  1970. /* Check for GP requests since above loop. */
  1971. rdp = this_cpu_ptr(&rcu_data);
  1972. if (!needgp && ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed)) {
  1973. trace_rcu_this_gp(rnp, rdp, rnp->gp_seq_needed,
  1974. TPS("CleanupMore"));
  1975. needgp = true;
  1976. }
  1977. /* Advance CBs to reduce false positives below. */
  1978. offloaded = rcu_rdp_is_offloaded(rdp);
  1979. if ((offloaded || !rcu_accelerate_cbs(rnp, rdp)) && needgp) {
  1980. // We get here if a grace period was needed (“needgp”)
  1981. // and the above call to rcu_accelerate_cbs() did not set
  1982. // the RCU_GP_FLAG_INIT bit in ->gp_state (which records
  1983. // the need for another grace period).  The purpose
  1984. // of the “offloaded” check is to avoid invoking
  1985. // rcu_accelerate_cbs() on an offloaded CPU because we do not
  1986. // hold the ->nocb_lock needed to safely access an offloaded
  1987. // ->cblist.  We do not want to acquire that lock because
  1988. // it can be heavily contended during callback floods.
  1989. WRITE_ONCE(rcu_state.gp_flags, RCU_GP_FLAG_INIT);
  1990. WRITE_ONCE(rcu_state.gp_req_activity, jiffies);
  1991. trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("newreq"));
  1992. } else {
  1993. // We get here either if there is no need for an
  1994. // additional grace period or if rcu_accelerate_cbs() has
  1995. // already set the RCU_GP_FLAG_INIT bit in ->gp_flags. 
  1996. // So all we need to do is to clear all of the other
  1997. // ->gp_flags bits.
  1998. WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags & RCU_GP_FLAG_INIT);
  1999. }
  2000. raw_spin_unlock_irq_rcu_node(rnp);
  2001. // Make synchronize_rcu() users aware of the end of old grace period.
  2002. rcu_sr_normal_gp_cleanup();
  2003. // If strict, make all CPUs aware of the end of the old grace period.
  2004. if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
  2005. on_each_cpu(rcu_strict_gp_boundary, NULL, 0);
  2006. }
  2007. /*
  2008. * Body of kthread that handles grace periods.
  2009. */
  2010. static int __noreturn rcu_gp_kthread(void *unused)
  2011. {
  2012. rcu_bind_gp_kthread();
  2013. for (;;) {
  2014. /* Handle grace-period start. */
  2015. for (;;) {
  2016. trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
  2017. TPS("reqwait"));
  2018. WRITE_ONCE(rcu_state.gp_state, RCU_GP_WAIT_GPS);
  2019. swait_event_idle_exclusive(rcu_state.gp_wq,
  2020. READ_ONCE(rcu_state.gp_flags) &
  2021. RCU_GP_FLAG_INIT);
  2022. rcu_gp_torture_wait();
  2023. WRITE_ONCE(rcu_state.gp_state, RCU_GP_DONE_GPS);
  2024. /* Locking provides needed memory barrier. */
  2025. if (rcu_gp_init())
  2026. break;
  2027. cond_resched_tasks_rcu_qs();
  2028. WRITE_ONCE(rcu_state.gp_activity, jiffies);
  2029. WARN_ON(signal_pending(current));
  2030. trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
  2031. TPS("reqwaitsig"));
  2032. }
  2033. /* Handle quiescent-state forcing. */
  2034. rcu_gp_fqs_loop();
  2035. /* Handle grace-period end. */
  2036. WRITE_ONCE(rcu_state.gp_state, RCU_GP_CLEANUP);
  2037. rcu_gp_cleanup();
  2038. WRITE_ONCE(rcu_state.gp_state, RCU_GP_CLEANED);
  2039. }
  2040. }
  2041. /*
  2042. * Report a full set of quiescent states to the rcu_state data structure.
  2043. * Invoke rcu_gp_kthread_wake() to awaken the grace-period kthread if
  2044. * another grace period is required. Whether we wake the grace-period
  2045. * kthread or it awakens itself for the next round of quiescent-state
  2046. * forcing, that kthread will clean up after the just-completed grace
  2047. * period. Note that the caller must hold rnp->lock, which is released
  2048. * before return.
  2049. */
  2050. static void rcu_report_qs_rsp(unsigned long flags)
  2051. __releases(rcu_get_root()->lock)
  2052. {
  2053. raw_lockdep_assert_held_rcu_node(rcu_get_root());
  2054. WARN_ON_ONCE(!rcu_gp_in_progress());
  2055. WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags | RCU_GP_FLAG_FQS);
  2056. raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(), flags);
  2057. rcu_gp_kthread_wake();
  2058. }
  2059. /*
  2060. * Similar to rcu_report_qs_rdp(), for which it is a helper function.
  2061. * Allows quiescent states for a group of CPUs to be reported at one go
  2062. * to the specified rcu_node structure, though all the CPUs in the group
  2063. * must be represented by the same rcu_node structure (which need not be a
  2064. * leaf rcu_node structure, though it often will be). The gps parameter
  2065. * is the grace-period snapshot, which means that the quiescent states
  2066. * are valid only if rnp->gp_seq is equal to gps. That structure's lock
  2067. * must be held upon entry, and it is released before return.
  2068. *
  2069. * As a special case, if mask is zero, the bit-already-cleared check is
  2070. * disabled. This allows propagating quiescent state due to resumed tasks
  2071. * during grace-period initialization.
  2072. */
  2073. static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
  2074. unsigned long gps, unsigned long flags)
  2075. __releases(rnp->lock)
  2076. {
  2077. unsigned long oldmask = 0;
  2078. struct rcu_node *rnp_c;
  2079. raw_lockdep_assert_held_rcu_node(rnp);
  2080. /* Walk up the rcu_node hierarchy. */
  2081. for (;;) {
  2082. if ((!(rnp->qsmask & mask) && mask) || rnp->gp_seq != gps) {
  2083. /*
  2084. * Our bit has already been cleared, or the
  2085. * relevant grace period is already over, so done.
  2086. */
  2087. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2088. return;
  2089. }
  2090. WARN_ON_ONCE(oldmask); /* Any child must be all zeroed! */
  2091. WARN_ON_ONCE(!rcu_is_leaf_node(rnp) &&
  2092. rcu_preempt_blocked_readers_cgp(rnp));
  2093. WRITE_ONCE(rnp->qsmask, rnp->qsmask & ~mask);
  2094. trace_rcu_quiescent_state_report(rcu_state.name, rnp->gp_seq,
  2095. mask, rnp->qsmask, rnp->level,
  2096. rnp->grplo, rnp->grphi,
  2097. !!rnp->gp_tasks);
  2098. if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  2099. /* Other bits still set at this level, so done. */
  2100. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2101. return;
  2102. }
  2103. rnp->completedqs = rnp->gp_seq;
  2104. mask = rnp->grpmask;
  2105. if (rnp->parent == NULL) {
  2106. /* No more levels. Exit loop holding root lock. */
  2107. break;
  2108. }
  2109. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2110. rnp_c = rnp;
  2111. rnp = rnp->parent;
  2112. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  2113. oldmask = READ_ONCE(rnp_c->qsmask);
  2114. }
  2115. /*
  2116. * Get here if we are the last CPU to pass through a quiescent
  2117. * state for this grace period. Invoke rcu_report_qs_rsp()
  2118. * to clean up and start the next grace period if one is needed.
  2119. */
  2120. rcu_report_qs_rsp(flags); /* releases rnp->lock. */
  2121. }
  2122. /*
  2123. * Record a quiescent state for all tasks that were previously queued
  2124. * on the specified rcu_node structure and that were blocking the current
  2125. * RCU grace period. The caller must hold the corresponding rnp->lock with
  2126. * irqs disabled, and this lock is released upon return, but irqs remain
  2127. * disabled.
  2128. */
  2129. static void __maybe_unused
  2130. rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
  2131. __releases(rnp->lock)
  2132. {
  2133. unsigned long gps;
  2134. unsigned long mask;
  2135. struct rcu_node *rnp_p;
  2136. raw_lockdep_assert_held_rcu_node(rnp);
  2137. if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_PREEMPT_RCU)) ||
  2138. WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)) ||
  2139. rnp->qsmask != 0) {
  2140. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2141. return; /* Still need more quiescent states! */
  2142. }
  2143. rnp->completedqs = rnp->gp_seq;
  2144. rnp_p = rnp->parent;
  2145. if (rnp_p == NULL) {
  2146. /*
  2147. * Only one rcu_node structure in the tree, so don't
  2148. * try to report up to its nonexistent parent!
  2149. */
  2150. rcu_report_qs_rsp(flags);
  2151. return;
  2152. }
  2153. /* Report up the rest of the hierarchy, tracking current ->gp_seq. */
  2154. gps = rnp->gp_seq;
  2155. mask = rnp->grpmask;
  2156. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  2157. raw_spin_lock_rcu_node(rnp_p); /* irqs already disabled. */
  2158. rcu_report_qs_rnp(mask, rnp_p, gps, flags);
  2159. }
  2160. /*
  2161. * Record a quiescent state for the specified CPU to that CPU's rcu_data
  2162. * structure. This must be called from the specified CPU.
  2163. */
  2164. static void
  2165. rcu_report_qs_rdp(struct rcu_data *rdp)
  2166. {
  2167. unsigned long flags;
  2168. unsigned long mask;
  2169. struct rcu_node *rnp;
  2170. WARN_ON_ONCE(rdp->cpu != smp_processor_id());
  2171. rnp = rdp->mynode;
  2172. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  2173. if (rdp->cpu_no_qs.b.norm || rdp->gp_seq != rnp->gp_seq ||
  2174. rdp->gpwrap) {
  2175. /*
  2176. * The grace period in which this quiescent state was
  2177. * recorded has ended, so don't report it upwards.
  2178. * We will instead need a new quiescent state that lies
  2179. * within the current grace period.
  2180. */
  2181. rdp->cpu_no_qs.b.norm = true; /* need qs for new gp. */
  2182. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2183. return;
  2184. }
  2185. mask = rdp->grpmask;
  2186. rdp->core_needs_qs = false;
  2187. if ((rnp->qsmask & mask) == 0) {
  2188. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2189. } else {
  2190. /*
  2191. * This GP can't end until cpu checks in, so all of our
  2192. * callbacks can be processed during the next GP.
  2193. *
  2194. * NOCB kthreads have their own way to deal with that...
  2195. */
  2196. if (!rcu_rdp_is_offloaded(rdp)) {
  2197. /*
  2198. * The current GP has not yet ended, so it
  2199. * should not be possible for rcu_accelerate_cbs()
  2200. * to return true. So complain, but don't awaken.
  2201. */
  2202. WARN_ON_ONCE(rcu_accelerate_cbs(rnp, rdp));
  2203. }
  2204. rcu_disable_urgency_upon_qs(rdp);
  2205. rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
  2206. /* ^^^ Released rnp->lock */
  2207. }
  2208. }
  2209. /*
  2210. * Check to see if there is a new grace period of which this CPU
  2211. * is not yet aware, and if so, set up local rcu_data state for it.
  2212. * Otherwise, see if this CPU has just passed through its first
  2213. * quiescent state for this grace period, and record that fact if so.
  2214. */
  2215. static void
  2216. rcu_check_quiescent_state(struct rcu_data *rdp)
  2217. {
  2218. /* Check for grace-period ends and beginnings. */
  2219. note_gp_changes(rdp);
  2220. /*
  2221. * Does this CPU still need to do its part for current grace period?
  2222. * If no, return and let the other CPUs do their part as well.
  2223. */
  2224. if (!rdp->core_needs_qs)
  2225. return;
  2226. /*
  2227. * Was there a quiescent state since the beginning of the grace
  2228. * period? If no, then exit and wait for the next call.
  2229. */
  2230. if (rdp->cpu_no_qs.b.norm)
  2231. return;
  2232. /*
  2233. * Tell RCU we are done (but rcu_report_qs_rdp() will be the
  2234. * judge of that).
  2235. */
  2236. rcu_report_qs_rdp(rdp);
  2237. }
  2238. /* Return true if callback-invocation time limit exceeded. */
  2239. static bool rcu_do_batch_check_time(long count, long tlimit,
  2240. bool jlimit_check, unsigned long jlimit)
  2241. {
  2242. // Invoke local_clock() only once per 32 consecutive callbacks.
  2243. return unlikely(tlimit) &&
  2244. (!likely(count & 31) ||
  2245. (IS_ENABLED(CONFIG_RCU_DOUBLE_CHECK_CB_TIME) &&
  2246. jlimit_check && time_after(jiffies, jlimit))) &&
  2247. local_clock() >= tlimit;
  2248. }
  2249. /*
  2250. * Invoke any RCU callbacks that have made it to the end of their grace
  2251. * period. Throttle as specified by rdp->blimit.
  2252. */
  2253. static void rcu_do_batch(struct rcu_data *rdp)
  2254. {
  2255. long bl;
  2256. long count = 0;
  2257. int div;
  2258. bool __maybe_unused empty;
  2259. unsigned long flags;
  2260. unsigned long jlimit;
  2261. bool jlimit_check = false;
  2262. long pending;
  2263. struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
  2264. struct rcu_head *rhp;
  2265. long tlimit = 0;
  2266. /* If no callbacks are ready, just return. */
  2267. if (!rcu_segcblist_ready_cbs(&rdp->cblist)) {
  2268. trace_rcu_batch_start(rcu_state.name,
  2269. rcu_segcblist_n_cbs(&rdp->cblist), 0);
  2270. trace_rcu_batch_end(rcu_state.name, 0,
  2271. !rcu_segcblist_empty(&rdp->cblist),
  2272. need_resched(), is_idle_task(current),
  2273. rcu_is_callbacks_kthread(rdp));
  2274. return;
  2275. }
  2276. /*
  2277. * Extract the list of ready callbacks, disabling IRQs to prevent
  2278. * races with call_rcu() from interrupt handlers. Leave the
  2279. * callback counts, as rcu_barrier() needs to be conservative.
  2280. *
  2281. * Callbacks execution is fully ordered against preceding grace period
  2282. * completion (materialized by rnp->gp_seq update) thanks to the
  2283. * smp_mb__after_unlock_lock() upon node locking required for callbacks
  2284. * advancing. In NOCB mode this ordering is then further relayed through
  2285. * the nocb locking that protects both callbacks advancing and extraction.
  2286. */
  2287. rcu_nocb_lock_irqsave(rdp, flags);
  2288. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  2289. pending = rcu_segcblist_get_seglen(&rdp->cblist, RCU_DONE_TAIL);
  2290. div = READ_ONCE(rcu_divisor);
  2291. div = div < 0 ? 7 : div > sizeof(long) * 8 - 2 ? sizeof(long) * 8 - 2 : div;
  2292. bl = max(rdp->blimit, pending >> div);
  2293. if ((in_serving_softirq() || rdp->rcu_cpu_kthread_status == RCU_KTHREAD_RUNNING) &&
  2294. (IS_ENABLED(CONFIG_RCU_DOUBLE_CHECK_CB_TIME) || unlikely(bl > 100))) {
  2295. const long npj = NSEC_PER_SEC / HZ;
  2296. long rrn = READ_ONCE(rcu_resched_ns);
  2297. rrn = rrn < NSEC_PER_MSEC ? NSEC_PER_MSEC : rrn > NSEC_PER_SEC ? NSEC_PER_SEC : rrn;
  2298. tlimit = local_clock() + rrn;
  2299. jlimit = jiffies + (rrn + npj + 1) / npj;
  2300. jlimit_check = true;
  2301. }
  2302. trace_rcu_batch_start(rcu_state.name,
  2303. rcu_segcblist_n_cbs(&rdp->cblist), bl);
  2304. rcu_segcblist_extract_done_cbs(&rdp->cblist, &rcl);
  2305. if (rcu_rdp_is_offloaded(rdp))
  2306. rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
  2307. trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbDequeued"));
  2308. rcu_nocb_unlock_irqrestore(rdp, flags);
  2309. /* Invoke callbacks. */
  2310. tick_dep_set_task(current, TICK_DEP_BIT_RCU);
  2311. rhp = rcu_cblist_dequeue(&rcl);
  2312. for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
  2313. rcu_callback_t f;
  2314. count++;
  2315. debug_rcu_head_unqueue(rhp);
  2316. rcu_lock_acquire(&rcu_callback_map);
  2317. trace_rcu_invoke_callback(rcu_state.name, rhp);
  2318. f = rhp->func;
  2319. debug_rcu_head_callback(rhp);
  2320. WRITE_ONCE(rhp->func, (rcu_callback_t)0L);
  2321. f(rhp);
  2322. rcu_lock_release(&rcu_callback_map);
  2323. /*
  2324. * Stop only if limit reached and CPU has something to do.
  2325. */
  2326. if (in_serving_softirq()) {
  2327. if (count >= bl && (need_resched() || !is_idle_task(current)))
  2328. break;
  2329. /*
  2330. * Make sure we don't spend too much time here and deprive other
  2331. * softirq vectors of CPU cycles.
  2332. */
  2333. if (rcu_do_batch_check_time(count, tlimit, jlimit_check, jlimit))
  2334. break;
  2335. } else {
  2336. // In rcuc/rcuoc context, so no worries about
  2337. // depriving other softirq vectors of CPU cycles.
  2338. local_bh_enable();
  2339. lockdep_assert_irqs_enabled();
  2340. cond_resched_tasks_rcu_qs();
  2341. lockdep_assert_irqs_enabled();
  2342. local_bh_disable();
  2343. // But rcuc kthreads can delay quiescent-state
  2344. // reporting, so check time limits for them.
  2345. if (rdp->rcu_cpu_kthread_status == RCU_KTHREAD_RUNNING &&
  2346. rcu_do_batch_check_time(count, tlimit, jlimit_check, jlimit)) {
  2347. rdp->rcu_cpu_has_work = 1;
  2348. break;
  2349. }
  2350. }
  2351. }
  2352. rcu_nocb_lock_irqsave(rdp, flags);
  2353. rdp->n_cbs_invoked += count;
  2354. trace_rcu_batch_end(rcu_state.name, count, !!rcl.head, need_resched(),
  2355. is_idle_task(current), rcu_is_callbacks_kthread(rdp));
  2356. /* Update counts and requeue any remaining callbacks. */
  2357. rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl);
  2358. rcu_segcblist_add_len(&rdp->cblist, -count);
  2359. /* Reinstate batch limit if we have worked down the excess. */
  2360. count = rcu_segcblist_n_cbs(&rdp->cblist);
  2361. if (rdp->blimit >= DEFAULT_MAX_RCU_BLIMIT && count <= qlowmark)
  2362. rdp->blimit = blimit;
  2363. /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
  2364. if (count == 0 && rdp->qlen_last_fqs_check != 0) {
  2365. rdp->qlen_last_fqs_check = 0;
  2366. rdp->n_force_qs_snap = READ_ONCE(rcu_state.n_force_qs);
  2367. } else if (count < rdp->qlen_last_fqs_check - qhimark)
  2368. rdp->qlen_last_fqs_check = count;
  2369. /*
  2370. * The following usually indicates a double call_rcu(). To track
  2371. * this down, try building with CONFIG_DEBUG_OBJECTS_RCU_HEAD=y.
  2372. */
  2373. empty = rcu_segcblist_empty(&rdp->cblist);
  2374. WARN_ON_ONCE(count == 0 && !empty);
  2375. WARN_ON_ONCE(!IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
  2376. count != 0 && empty);
  2377. WARN_ON_ONCE(count == 0 && rcu_segcblist_n_segment_cbs(&rdp->cblist) != 0);
  2378. WARN_ON_ONCE(!empty && rcu_segcblist_n_segment_cbs(&rdp->cblist) == 0);
  2379. rcu_nocb_unlock_irqrestore(rdp, flags);
  2380. tick_dep_clear_task(current, TICK_DEP_BIT_RCU);
  2381. }
  2382. /*
  2383. * This function is invoked from each scheduling-clock interrupt,
  2384. * and checks to see if this CPU is in a non-context-switch quiescent
  2385. * state, for example, user mode or idle loop. It also schedules RCU
  2386. * core processing. If the current grace period has gone on too long,
  2387. * it will ask the scheduler to manufacture a context switch for the sole
  2388. * purpose of providing the needed quiescent state.
  2389. */
  2390. void rcu_sched_clock_irq(int user)
  2391. {
  2392. unsigned long j;
  2393. if (IS_ENABLED(CONFIG_PROVE_RCU)) {
  2394. j = jiffies;
  2395. WARN_ON_ONCE(time_before(j, __this_cpu_read(rcu_data.last_sched_clock)));
  2396. __this_cpu_write(rcu_data.last_sched_clock, j);
  2397. }
  2398. trace_rcu_utilization(TPS("Start scheduler-tick"));
  2399. lockdep_assert_irqs_disabled();
  2400. raw_cpu_inc(rcu_data.ticks_this_gp);
  2401. /* The load-acquire pairs with the store-release setting to true. */
  2402. if (smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs))) {
  2403. /* Idle and userspace execution already are quiescent states. */
  2404. if (!rcu_is_cpu_rrupt_from_idle() && !user) {
  2405. set_tsk_need_resched(current);
  2406. set_preempt_need_resched();
  2407. }
  2408. __this_cpu_write(rcu_data.rcu_urgent_qs, false);
  2409. }
  2410. rcu_flavor_sched_clock_irq(user);
  2411. if (rcu_pending(user))
  2412. invoke_rcu_core();
  2413. if (user || rcu_is_cpu_rrupt_from_idle())
  2414. rcu_note_voluntary_context_switch(current);
  2415. lockdep_assert_irqs_disabled();
  2416. trace_rcu_utilization(TPS("End scheduler-tick"));
  2417. }
  2418. /*
  2419. * Scan the leaf rcu_node structures. For each structure on which all
  2420. * CPUs have reported a quiescent state and on which there are tasks
  2421. * blocking the current grace period, initiate RCU priority boosting.
  2422. * Otherwise, invoke the specified function to check dyntick state for
  2423. * each CPU that has not yet reported a quiescent state.
  2424. */
  2425. static void force_qs_rnp(int (*f)(struct rcu_data *rdp))
  2426. {
  2427. int cpu;
  2428. unsigned long flags;
  2429. struct rcu_node *rnp;
  2430. rcu_state.cbovld = rcu_state.cbovldnext;
  2431. rcu_state.cbovldnext = false;
  2432. rcu_for_each_leaf_node(rnp) {
  2433. unsigned long mask = 0;
  2434. unsigned long rsmask = 0;
  2435. cond_resched_tasks_rcu_qs();
  2436. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  2437. rcu_state.cbovldnext |= !!rnp->cbovldmask;
  2438. if (rnp->qsmask == 0) {
  2439. if (rcu_preempt_blocked_readers_cgp(rnp)) {
  2440. /*
  2441. * No point in scanning bits because they
  2442. * are all zero. But we might need to
  2443. * priority-boost blocked readers.
  2444. */
  2445. rcu_initiate_boost(rnp, flags);
  2446. /* rcu_initiate_boost() releases rnp->lock */
  2447. continue;
  2448. }
  2449. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2450. continue;
  2451. }
  2452. for_each_leaf_node_cpu_mask(rnp, cpu, rnp->qsmask) {
  2453. struct rcu_data *rdp;
  2454. int ret;
  2455. rdp = per_cpu_ptr(&rcu_data, cpu);
  2456. ret = f(rdp);
  2457. if (ret > 0) {
  2458. mask |= rdp->grpmask;
  2459. rcu_disable_urgency_upon_qs(rdp);
  2460. }
  2461. if (ret < 0)
  2462. rsmask |= rdp->grpmask;
  2463. }
  2464. if (mask != 0) {
  2465. /* Idle/offline CPUs, report (releases rnp->lock). */
  2466. rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
  2467. } else {
  2468. /* Nothing to do here, so just drop the lock. */
  2469. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  2470. }
  2471. for_each_leaf_node_cpu_mask(rnp, cpu, rsmask)
  2472. resched_cpu(cpu);
  2473. }
  2474. }
  2475. /*
  2476. * Force quiescent states on reluctant CPUs, and also detect which
  2477. * CPUs are in dyntick-idle mode.
  2478. */
  2479. void rcu_force_quiescent_state(void)
  2480. {
  2481. unsigned long flags;
  2482. bool ret;
  2483. struct rcu_node *rnp;
  2484. struct rcu_node *rnp_old = NULL;
  2485. if (!rcu_gp_in_progress())
  2486. return;
  2487. /* Funnel through hierarchy to reduce memory contention. */
  2488. rnp = raw_cpu_read(rcu_data.mynode);
  2489. for (; rnp != NULL; rnp = rnp->parent) {
  2490. ret = (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) ||
  2491. !raw_spin_trylock(&rnp->fqslock);
  2492. if (rnp_old != NULL)
  2493. raw_spin_unlock(&rnp_old->fqslock);
  2494. if (ret)
  2495. return;
  2496. rnp_old = rnp;
  2497. }
  2498. /* rnp_old == rcu_get_root(), rnp == NULL. */
  2499. /* Reached the root of the rcu_node tree, acquire lock. */
  2500. raw_spin_lock_irqsave_rcu_node(rnp_old, flags);
  2501. raw_spin_unlock(&rnp_old->fqslock);
  2502. if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) {
  2503. raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
  2504. return; /* Someone beat us to it. */
  2505. }
  2506. WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags | RCU_GP_FLAG_FQS);
  2507. raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
  2508. rcu_gp_kthread_wake();
  2509. }
  2510. EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  2511. // Workqueue handler for an RCU reader for kernels enforcing struct RCU
  2512. // grace periods.
  2513. static void strict_work_handler(struct work_struct *work)
  2514. {
  2515. rcu_read_lock();
  2516. rcu_read_unlock();
  2517. }
  2518. /* Perform RCU core processing work for the current CPU. */
  2519. static __latent_entropy void rcu_core(void)
  2520. {
  2521. unsigned long flags;
  2522. struct rcu_data *rdp = raw_cpu_ptr(&rcu_data);
  2523. struct rcu_node *rnp = rdp->mynode;
  2524. if (cpu_is_offline(smp_processor_id()))
  2525. return;
  2526. trace_rcu_utilization(TPS("Start RCU core"));
  2527. WARN_ON_ONCE(!rdp->beenonline);
  2528. /* Report any deferred quiescent states if preemption enabled. */
  2529. if (IS_ENABLED(CONFIG_PREEMPT_COUNT) && (!(preempt_count() & PREEMPT_MASK))) {
  2530. rcu_preempt_deferred_qs(current);
  2531. } else if (rcu_preempt_need_deferred_qs(current)) {
  2532. set_tsk_need_resched(current);
  2533. set_preempt_need_resched();
  2534. }
  2535. /* Update RCU state based on any recent quiescent states. */
  2536. rcu_check_quiescent_state(rdp);
  2537. /* No grace period and unregistered callbacks? */
  2538. if (!rcu_gp_in_progress() &&
  2539. rcu_segcblist_is_enabled(&rdp->cblist) && !rcu_rdp_is_offloaded(rdp)) {
  2540. local_irq_save(flags);
  2541. if (!rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
  2542. rcu_accelerate_cbs_unlocked(rnp, rdp);
  2543. local_irq_restore(flags);
  2544. }
  2545. rcu_check_gp_start_stall(rnp, rdp, rcu_jiffies_till_stall_check());
  2546. /* If there are callbacks ready, invoke them. */
  2547. if (!rcu_rdp_is_offloaded(rdp) && rcu_segcblist_ready_cbs(&rdp->cblist) &&
  2548. likely(READ_ONCE(rcu_scheduler_fully_active))) {
  2549. rcu_do_batch(rdp);
  2550. /* Re-invoke RCU core processing if there are callbacks remaining. */
  2551. if (rcu_segcblist_ready_cbs(&rdp->cblist))
  2552. invoke_rcu_core();
  2553. }
  2554. /* Do any needed deferred wakeups of rcuo kthreads. */
  2555. do_nocb_deferred_wakeup(rdp);
  2556. trace_rcu_utilization(TPS("End RCU core"));
  2557. // If strict GPs, schedule an RCU reader in a clean environment.
  2558. if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
  2559. queue_work_on(rdp->cpu, rcu_gp_wq, &rdp->strict_work);
  2560. }
  2561. static void rcu_core_si(void)
  2562. {
  2563. rcu_core();
  2564. }
  2565. static void rcu_wake_cond(struct task_struct *t, int status)
  2566. {
  2567. /*
  2568. * If the thread is yielding, only wake it when this
  2569. * is invoked from idle
  2570. */
  2571. if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)))
  2572. wake_up_process(t);
  2573. }
  2574. static void invoke_rcu_core_kthread(void)
  2575. {
  2576. struct task_struct *t;
  2577. unsigned long flags;
  2578. local_irq_save(flags);
  2579. __this_cpu_write(rcu_data.rcu_cpu_has_work, 1);
  2580. t = __this_cpu_read(rcu_data.rcu_cpu_kthread_task);
  2581. if (t != NULL && t != current)
  2582. rcu_wake_cond(t, __this_cpu_read(rcu_data.rcu_cpu_kthread_status));
  2583. local_irq_restore(flags);
  2584. }
  2585. /*
  2586. * Wake up this CPU's rcuc kthread to do RCU core processing.
  2587. */
  2588. static void invoke_rcu_core(void)
  2589. {
  2590. if (!cpu_online(smp_processor_id()))
  2591. return;
  2592. if (use_softirq)
  2593. raise_softirq(RCU_SOFTIRQ);
  2594. else
  2595. invoke_rcu_core_kthread();
  2596. }
  2597. static void rcu_cpu_kthread_park(unsigned int cpu)
  2598. {
  2599. per_cpu(rcu_data.rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  2600. }
  2601. static int rcu_cpu_kthread_should_run(unsigned int cpu)
  2602. {
  2603. return __this_cpu_read(rcu_data.rcu_cpu_has_work);
  2604. }
  2605. /*
  2606. * Per-CPU kernel thread that invokes RCU callbacks. This replaces
  2607. * the RCU softirq used in configurations of RCU that do not support RCU
  2608. * priority boosting.
  2609. */
  2610. static void rcu_cpu_kthread(unsigned int cpu)
  2611. {
  2612. unsigned int *statusp = this_cpu_ptr(&rcu_data.rcu_cpu_kthread_status);
  2613. char work, *workp = this_cpu_ptr(&rcu_data.rcu_cpu_has_work);
  2614. unsigned long *j = this_cpu_ptr(&rcu_data.rcuc_activity);
  2615. int spincnt;
  2616. trace_rcu_utilization(TPS("Start CPU kthread@rcu_run"));
  2617. for (spincnt = 0; spincnt < 10; spincnt++) {
  2618. WRITE_ONCE(*j, jiffies);
  2619. local_bh_disable();
  2620. *statusp = RCU_KTHREAD_RUNNING;
  2621. local_irq_disable();
  2622. work = *workp;
  2623. WRITE_ONCE(*workp, 0);
  2624. local_irq_enable();
  2625. if (work)
  2626. rcu_core();
  2627. local_bh_enable();
  2628. if (!READ_ONCE(*workp)) {
  2629. trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  2630. *statusp = RCU_KTHREAD_WAITING;
  2631. return;
  2632. }
  2633. }
  2634. *statusp = RCU_KTHREAD_YIELDING;
  2635. trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  2636. schedule_timeout_idle(2);
  2637. trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  2638. *statusp = RCU_KTHREAD_WAITING;
  2639. WRITE_ONCE(*j, jiffies);
  2640. }
  2641. static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  2642. .store = &rcu_data.rcu_cpu_kthread_task,
  2643. .thread_should_run = rcu_cpu_kthread_should_run,
  2644. .thread_fn = rcu_cpu_kthread,
  2645. .thread_comm = "rcuc/%u",
  2646. .setup = rcu_cpu_kthread_setup,
  2647. .park = rcu_cpu_kthread_park,
  2648. };
  2649. /*
  2650. * Spawn per-CPU RCU core processing kthreads.
  2651. */
  2652. static int __init rcu_spawn_core_kthreads(void)
  2653. {
  2654. int cpu;
  2655. for_each_possible_cpu(cpu)
  2656. per_cpu(rcu_data.rcu_cpu_has_work, cpu) = 0;
  2657. if (use_softirq)
  2658. return 0;
  2659. WARN_ONCE(smpboot_register_percpu_thread(&rcu_cpu_thread_spec),
  2660. "%s: Could not start rcuc kthread, OOM is now expected behavior\n", __func__);
  2661. return 0;
  2662. }
  2663. static void rcutree_enqueue(struct rcu_data *rdp, struct rcu_head *head, rcu_callback_t func)
  2664. {
  2665. rcu_segcblist_enqueue(&rdp->cblist, head);
  2666. if (__is_kvfree_rcu_offset((unsigned long)func))
  2667. trace_rcu_kvfree_callback(rcu_state.name, head,
  2668. (unsigned long)func,
  2669. rcu_segcblist_n_cbs(&rdp->cblist));
  2670. else
  2671. trace_rcu_callback(rcu_state.name, head,
  2672. rcu_segcblist_n_cbs(&rdp->cblist));
  2673. trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCBQueued"));
  2674. }
  2675. /*
  2676. * Handle any core-RCU processing required by a call_rcu() invocation.
  2677. */
  2678. static void call_rcu_core(struct rcu_data *rdp, struct rcu_head *head,
  2679. rcu_callback_t func, unsigned long flags)
  2680. {
  2681. rcutree_enqueue(rdp, head, func);
  2682. /*
  2683. * If called from an extended quiescent state, invoke the RCU
  2684. * core in order to force a re-evaluation of RCU's idleness.
  2685. */
  2686. if (!rcu_is_watching())
  2687. invoke_rcu_core();
  2688. /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
  2689. if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
  2690. return;
  2691. /*
  2692. * Force the grace period if too many callbacks or too long waiting.
  2693. * Enforce hysteresis, and don't invoke rcu_force_quiescent_state()
  2694. * if some other CPU has recently done so. Also, don't bother
  2695. * invoking rcu_force_quiescent_state() if the newly enqueued callback
  2696. * is the only one waiting for a grace period to complete.
  2697. */
  2698. if (unlikely(rcu_segcblist_n_cbs(&rdp->cblist) >
  2699. rdp->qlen_last_fqs_check + qhimark)) {
  2700. /* Are we ignoring a completed grace period? */
  2701. note_gp_changes(rdp);
  2702. /* Start a new grace period if one not already started. */
  2703. if (!rcu_gp_in_progress()) {
  2704. rcu_accelerate_cbs_unlocked(rdp->mynode, rdp);
  2705. } else {
  2706. /* Give the grace period a kick. */
  2707. rdp->blimit = DEFAULT_MAX_RCU_BLIMIT;
  2708. if (READ_ONCE(rcu_state.n_force_qs) == rdp->n_force_qs_snap &&
  2709. rcu_segcblist_first_pend_cb(&rdp->cblist) != head)
  2710. rcu_force_quiescent_state();
  2711. rdp->n_force_qs_snap = READ_ONCE(rcu_state.n_force_qs);
  2712. rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
  2713. }
  2714. }
  2715. }
  2716. /*
  2717. * RCU callback function to leak a callback.
  2718. */
  2719. static void rcu_leak_callback(struct rcu_head *rhp)
  2720. {
  2721. }
  2722. /*
  2723. * Check and if necessary update the leaf rcu_node structure's
  2724. * ->cbovldmask bit corresponding to the current CPU based on that CPU's
  2725. * number of queued RCU callbacks. The caller must hold the leaf rcu_node
  2726. * structure's ->lock.
  2727. */
  2728. static void check_cb_ovld_locked(struct rcu_data *rdp, struct rcu_node *rnp)
  2729. {
  2730. raw_lockdep_assert_held_rcu_node(rnp);
  2731. if (qovld_calc <= 0)
  2732. return; // Early boot and wildcard value set.
  2733. if (rcu_segcblist_n_cbs(&rdp->cblist) >= qovld_calc)
  2734. WRITE_ONCE(rnp->cbovldmask, rnp->cbovldmask | rdp->grpmask);
  2735. else
  2736. WRITE_ONCE(rnp->cbovldmask, rnp->cbovldmask & ~rdp->grpmask);
  2737. }
  2738. /*
  2739. * Check and if necessary update the leaf rcu_node structure's
  2740. * ->cbovldmask bit corresponding to the current CPU based on that CPU's
  2741. * number of queued RCU callbacks. No locks need be held, but the
  2742. * caller must have disabled interrupts.
  2743. *
  2744. * Note that this function ignores the possibility that there are a lot
  2745. * of callbacks all of which have already seen the end of their respective
  2746. * grace periods. This omission is due to the need for no-CBs CPUs to
  2747. * be holding ->nocb_lock to do this check, which is too heavy for a
  2748. * common-case operation.
  2749. */
  2750. static void check_cb_ovld(struct rcu_data *rdp)
  2751. {
  2752. struct rcu_node *const rnp = rdp->mynode;
  2753. if (qovld_calc <= 0 ||
  2754. ((rcu_segcblist_n_cbs(&rdp->cblist) >= qovld_calc) ==
  2755. !!(READ_ONCE(rnp->cbovldmask) & rdp->grpmask)))
  2756. return; // Early boot wildcard value or already set correctly.
  2757. raw_spin_lock_rcu_node(rnp);
  2758. check_cb_ovld_locked(rdp, rnp);
  2759. raw_spin_unlock_rcu_node(rnp);
  2760. }
  2761. static void
  2762. __call_rcu_common(struct rcu_head *head, rcu_callback_t func, bool lazy_in)
  2763. {
  2764. static atomic_t doublefrees;
  2765. unsigned long flags;
  2766. bool lazy;
  2767. struct rcu_data *rdp;
  2768. /* Misaligned rcu_head! */
  2769. WARN_ON_ONCE((unsigned long)head & (sizeof(void *) - 1));
  2770. /* Avoid NULL dereference if callback is NULL. */
  2771. if (WARN_ON_ONCE(!func))
  2772. return;
  2773. if (debug_rcu_head_queue(head)) {
  2774. /*
  2775. * Probable double call_rcu(), so leak the callback.
  2776. * Use rcu:rcu_callback trace event to find the previous
  2777. * time callback was passed to call_rcu().
  2778. */
  2779. if (atomic_inc_return(&doublefrees) < 4) {
  2780. pr_err("%s(): Double-freed CB %p->%pS()!!! ", __func__, head, head->func);
  2781. mem_dump_obj(head);
  2782. }
  2783. WRITE_ONCE(head->func, rcu_leak_callback);
  2784. return;
  2785. }
  2786. head->func = func;
  2787. head->next = NULL;
  2788. kasan_record_aux_stack_noalloc(head);
  2789. local_irq_save(flags);
  2790. rdp = this_cpu_ptr(&rcu_data);
  2791. lazy = lazy_in && !rcu_async_should_hurry();
  2792. /* Add the callback to our list. */
  2793. if (unlikely(!rcu_segcblist_is_enabled(&rdp->cblist))) {
  2794. // This can trigger due to call_rcu() from offline CPU:
  2795. WARN_ON_ONCE(rcu_scheduler_active != RCU_SCHEDULER_INACTIVE);
  2796. WARN_ON_ONCE(!rcu_is_watching());
  2797. // Very early boot, before rcu_init(). Initialize if needed
  2798. // and then drop through to queue the callback.
  2799. if (rcu_segcblist_empty(&rdp->cblist))
  2800. rcu_segcblist_init(&rdp->cblist);
  2801. }
  2802. check_cb_ovld(rdp);
  2803. if (unlikely(rcu_rdp_is_offloaded(rdp)))
  2804. call_rcu_nocb(rdp, head, func, flags, lazy);
  2805. else
  2806. call_rcu_core(rdp, head, func, flags);
  2807. local_irq_restore(flags);
  2808. }
  2809. #ifdef CONFIG_RCU_LAZY
  2810. static bool enable_rcu_lazy __read_mostly = !IS_ENABLED(CONFIG_RCU_LAZY_DEFAULT_OFF);
  2811. module_param(enable_rcu_lazy, bool, 0444);
  2812. /**
  2813. * call_rcu_hurry() - Queue RCU callback for invocation after grace period, and
  2814. * flush all lazy callbacks (including the new one) to the main ->cblist while
  2815. * doing so.
  2816. *
  2817. * @head: structure to be used for queueing the RCU updates.
  2818. * @func: actual callback function to be invoked after the grace period
  2819. *
  2820. * The callback function will be invoked some time after a full grace
  2821. * period elapses, in other words after all pre-existing RCU read-side
  2822. * critical sections have completed.
  2823. *
  2824. * Use this API instead of call_rcu() if you don't want the callback to be
  2825. * invoked after very long periods of time, which can happen on systems without
  2826. * memory pressure and on systems which are lightly loaded or mostly idle.
  2827. * This function will cause callbacks to be invoked sooner than later at the
  2828. * expense of extra power. Other than that, this function is identical to, and
  2829. * reuses call_rcu()'s logic. Refer to call_rcu() for more details about memory
  2830. * ordering and other functionality.
  2831. */
  2832. void call_rcu_hurry(struct rcu_head *head, rcu_callback_t func)
  2833. {
  2834. __call_rcu_common(head, func, false);
  2835. }
  2836. EXPORT_SYMBOL_GPL(call_rcu_hurry);
  2837. #else
  2838. #define enable_rcu_lazy false
  2839. #endif
  2840. /**
  2841. * call_rcu() - Queue an RCU callback for invocation after a grace period.
  2842. * By default the callbacks are 'lazy' and are kept hidden from the main
  2843. * ->cblist to prevent starting of grace periods too soon.
  2844. * If you desire grace periods to start very soon, use call_rcu_hurry().
  2845. *
  2846. * @head: structure to be used for queueing the RCU updates.
  2847. * @func: actual callback function to be invoked after the grace period
  2848. *
  2849. * The callback function will be invoked some time after a full grace
  2850. * period elapses, in other words after all pre-existing RCU read-side
  2851. * critical sections have completed. However, the callback function
  2852. * might well execute concurrently with RCU read-side critical sections
  2853. * that started after call_rcu() was invoked.
  2854. *
  2855. * RCU read-side critical sections are delimited by rcu_read_lock()
  2856. * and rcu_read_unlock(), and may be nested. In addition, but only in
  2857. * v5.0 and later, regions of code across which interrupts, preemption,
  2858. * or softirqs have been disabled also serve as RCU read-side critical
  2859. * sections. This includes hardware interrupt handlers, softirq handlers,
  2860. * and NMI handlers.
  2861. *
  2862. * Note that all CPUs must agree that the grace period extended beyond
  2863. * all pre-existing RCU read-side critical section. On systems with more
  2864. * than one CPU, this means that when "func()" is invoked, each CPU is
  2865. * guaranteed to have executed a full memory barrier since the end of its
  2866. * last RCU read-side critical section whose beginning preceded the call
  2867. * to call_rcu(). It also means that each CPU executing an RCU read-side
  2868. * critical section that continues beyond the start of "func()" must have
  2869. * executed a memory barrier after the call_rcu() but before the beginning
  2870. * of that RCU read-side critical section. Note that these guarantees
  2871. * include CPUs that are offline, idle, or executing in user mode, as
  2872. * well as CPUs that are executing in the kernel.
  2873. *
  2874. * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
  2875. * resulting RCU callback function "func()", then both CPU A and CPU B are
  2876. * guaranteed to execute a full memory barrier during the time interval
  2877. * between the call to call_rcu() and the invocation of "func()" -- even
  2878. * if CPU A and CPU B are the same CPU (but again only if the system has
  2879. * more than one CPU).
  2880. *
  2881. * Implementation of these memory-ordering guarantees is described here:
  2882. * Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.rst.
  2883. */
  2884. void call_rcu(struct rcu_head *head, rcu_callback_t func)
  2885. {
  2886. __call_rcu_common(head, func, enable_rcu_lazy);
  2887. }
  2888. EXPORT_SYMBOL_GPL(call_rcu);
  2889. static struct workqueue_struct *rcu_reclaim_wq;
  2890. /* Maximum number of jiffies to wait before draining a batch. */
  2891. #define KFREE_DRAIN_JIFFIES (5 * HZ)
  2892. #define KFREE_N_BATCHES 2
  2893. #define FREE_N_CHANNELS 2
  2894. /**
  2895. * struct kvfree_rcu_bulk_data - single block to store kvfree_rcu() pointers
  2896. * @list: List node. All blocks are linked between each other
  2897. * @gp_snap: Snapshot of RCU state for objects placed to this bulk
  2898. * @nr_records: Number of active pointers in the array
  2899. * @records: Array of the kvfree_rcu() pointers
  2900. */
  2901. struct kvfree_rcu_bulk_data {
  2902. struct list_head list;
  2903. struct rcu_gp_oldstate gp_snap;
  2904. unsigned long nr_records;
  2905. void *records[] __counted_by(nr_records);
  2906. };
  2907. /*
  2908. * This macro defines how many entries the "records" array
  2909. * will contain. It is based on the fact that the size of
  2910. * kvfree_rcu_bulk_data structure becomes exactly one page.
  2911. */
  2912. #define KVFREE_BULK_MAX_ENTR \
  2913. ((PAGE_SIZE - sizeof(struct kvfree_rcu_bulk_data)) / sizeof(void *))
  2914. /**
  2915. * struct kfree_rcu_cpu_work - single batch of kfree_rcu() requests
  2916. * @rcu_work: Let queue_rcu_work() invoke workqueue handler after grace period
  2917. * @head_free: List of kfree_rcu() objects waiting for a grace period
  2918. * @head_free_gp_snap: Grace-period snapshot to check for attempted premature frees.
  2919. * @bulk_head_free: Bulk-List of kvfree_rcu() objects waiting for a grace period
  2920. * @krcp: Pointer to @kfree_rcu_cpu structure
  2921. */
  2922. struct kfree_rcu_cpu_work {
  2923. struct rcu_work rcu_work;
  2924. struct rcu_head *head_free;
  2925. struct rcu_gp_oldstate head_free_gp_snap;
  2926. struct list_head bulk_head_free[FREE_N_CHANNELS];
  2927. struct kfree_rcu_cpu *krcp;
  2928. };
  2929. /**
  2930. * struct kfree_rcu_cpu - batch up kfree_rcu() requests for RCU grace period
  2931. * @head: List of kfree_rcu() objects not yet waiting for a grace period
  2932. * @head_gp_snap: Snapshot of RCU state for objects placed to "@head"
  2933. * @bulk_head: Bulk-List of kvfree_rcu() objects not yet waiting for a grace period
  2934. * @krw_arr: Array of batches of kfree_rcu() objects waiting for a grace period
  2935. * @lock: Synchronize access to this structure
  2936. * @monitor_work: Promote @head to @head_free after KFREE_DRAIN_JIFFIES
  2937. * @initialized: The @rcu_work fields have been initialized
  2938. * @head_count: Number of objects in rcu_head singular list
  2939. * @bulk_count: Number of objects in bulk-list
  2940. * @bkvcache:
  2941. * A simple cache list that contains objects for reuse purpose.
  2942. * In order to save some per-cpu space the list is singular.
  2943. * Even though it is lockless an access has to be protected by the
  2944. * per-cpu lock.
  2945. * @page_cache_work: A work to refill the cache when it is empty
  2946. * @backoff_page_cache_fill: Delay cache refills
  2947. * @work_in_progress: Indicates that page_cache_work is running
  2948. * @hrtimer: A hrtimer for scheduling a page_cache_work
  2949. * @nr_bkv_objs: number of allocated objects at @bkvcache.
  2950. *
  2951. * This is a per-CPU structure. The reason that it is not included in
  2952. * the rcu_data structure is to permit this code to be extracted from
  2953. * the RCU files. Such extraction could allow further optimization of
  2954. * the interactions with the slab allocators.
  2955. */
  2956. struct kfree_rcu_cpu {
  2957. // Objects queued on a linked list
  2958. // through their rcu_head structures.
  2959. struct rcu_head *head;
  2960. unsigned long head_gp_snap;
  2961. atomic_t head_count;
  2962. // Objects queued on a bulk-list.
  2963. struct list_head bulk_head[FREE_N_CHANNELS];
  2964. atomic_t bulk_count[FREE_N_CHANNELS];
  2965. struct kfree_rcu_cpu_work krw_arr[KFREE_N_BATCHES];
  2966. raw_spinlock_t lock;
  2967. struct delayed_work monitor_work;
  2968. bool initialized;
  2969. struct delayed_work page_cache_work;
  2970. atomic_t backoff_page_cache_fill;
  2971. atomic_t work_in_progress;
  2972. struct hrtimer hrtimer;
  2973. struct llist_head bkvcache;
  2974. int nr_bkv_objs;
  2975. };
  2976. static DEFINE_PER_CPU(struct kfree_rcu_cpu, krc) = {
  2977. .lock = __RAW_SPIN_LOCK_UNLOCKED(krc.lock),
  2978. };
  2979. static __always_inline void
  2980. debug_rcu_bhead_unqueue(struct kvfree_rcu_bulk_data *bhead)
  2981. {
  2982. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  2983. int i;
  2984. for (i = 0; i < bhead->nr_records; i++)
  2985. debug_rcu_head_unqueue((struct rcu_head *)(bhead->records[i]));
  2986. #endif
  2987. }
  2988. static inline struct kfree_rcu_cpu *
  2989. krc_this_cpu_lock(unsigned long *flags)
  2990. {
  2991. struct kfree_rcu_cpu *krcp;
  2992. local_irq_save(*flags); // For safely calling this_cpu_ptr().
  2993. krcp = this_cpu_ptr(&krc);
  2994. raw_spin_lock(&krcp->lock);
  2995. return krcp;
  2996. }
  2997. static inline void
  2998. krc_this_cpu_unlock(struct kfree_rcu_cpu *krcp, unsigned long flags)
  2999. {
  3000. raw_spin_unlock_irqrestore(&krcp->lock, flags);
  3001. }
  3002. static inline struct kvfree_rcu_bulk_data *
  3003. get_cached_bnode(struct kfree_rcu_cpu *krcp)
  3004. {
  3005. if (!krcp->nr_bkv_objs)
  3006. return NULL;
  3007. WRITE_ONCE(krcp->nr_bkv_objs, krcp->nr_bkv_objs - 1);
  3008. return (struct kvfree_rcu_bulk_data *)
  3009. llist_del_first(&krcp->bkvcache);
  3010. }
  3011. static inline bool
  3012. put_cached_bnode(struct kfree_rcu_cpu *krcp,
  3013. struct kvfree_rcu_bulk_data *bnode)
  3014. {
  3015. // Check the limit.
  3016. if (krcp->nr_bkv_objs >= rcu_min_cached_objs)
  3017. return false;
  3018. llist_add((struct llist_node *) bnode, &krcp->bkvcache);
  3019. WRITE_ONCE(krcp->nr_bkv_objs, krcp->nr_bkv_objs + 1);
  3020. return true;
  3021. }
  3022. static int
  3023. drain_page_cache(struct kfree_rcu_cpu *krcp)
  3024. {
  3025. unsigned long flags;
  3026. struct llist_node *page_list, *pos, *n;
  3027. int freed = 0;
  3028. if (!rcu_min_cached_objs)
  3029. return 0;
  3030. raw_spin_lock_irqsave(&krcp->lock, flags);
  3031. page_list = llist_del_all(&krcp->bkvcache);
  3032. WRITE_ONCE(krcp->nr_bkv_objs, 0);
  3033. raw_spin_unlock_irqrestore(&krcp->lock, flags);
  3034. llist_for_each_safe(pos, n, page_list) {
  3035. free_page((unsigned long)pos);
  3036. freed++;
  3037. }
  3038. return freed;
  3039. }
  3040. static void
  3041. kvfree_rcu_bulk(struct kfree_rcu_cpu *krcp,
  3042. struct kvfree_rcu_bulk_data *bnode, int idx)
  3043. {
  3044. unsigned long flags;
  3045. int i;
  3046. if (!WARN_ON_ONCE(!poll_state_synchronize_rcu_full(&bnode->gp_snap))) {
  3047. debug_rcu_bhead_unqueue(bnode);
  3048. rcu_lock_acquire(&rcu_callback_map);
  3049. if (idx == 0) { // kmalloc() / kfree().
  3050. trace_rcu_invoke_kfree_bulk_callback(
  3051. rcu_state.name, bnode->nr_records,
  3052. bnode->records);
  3053. kfree_bulk(bnode->nr_records, bnode->records);
  3054. } else { // vmalloc() / vfree().
  3055. for (i = 0; i < bnode->nr_records; i++) {
  3056. trace_rcu_invoke_kvfree_callback(
  3057. rcu_state.name, bnode->records[i], 0);
  3058. vfree(bnode->records[i]);
  3059. }
  3060. }
  3061. rcu_lock_release(&rcu_callback_map);
  3062. }
  3063. raw_spin_lock_irqsave(&krcp->lock, flags);
  3064. if (put_cached_bnode(krcp, bnode))
  3065. bnode = NULL;
  3066. raw_spin_unlock_irqrestore(&krcp->lock, flags);
  3067. if (bnode)
  3068. free_page((unsigned long) bnode);
  3069. cond_resched_tasks_rcu_qs();
  3070. }
  3071. static void
  3072. kvfree_rcu_list(struct rcu_head *head)
  3073. {
  3074. struct rcu_head *next;
  3075. for (; head; head = next) {
  3076. void *ptr = (void *) head->func;
  3077. unsigned long offset = (void *) head - ptr;
  3078. next = head->next;
  3079. debug_rcu_head_unqueue((struct rcu_head *)ptr);
  3080. rcu_lock_acquire(&rcu_callback_map);
  3081. trace_rcu_invoke_kvfree_callback(rcu_state.name, head, offset);
  3082. if (!WARN_ON_ONCE(!__is_kvfree_rcu_offset(offset)))
  3083. kvfree(ptr);
  3084. rcu_lock_release(&rcu_callback_map);
  3085. cond_resched_tasks_rcu_qs();
  3086. }
  3087. }
  3088. /*
  3089. * This function is invoked in workqueue context after a grace period.
  3090. * It frees all the objects queued on ->bulk_head_free or ->head_free.
  3091. */
  3092. static void kfree_rcu_work(struct work_struct *work)
  3093. {
  3094. unsigned long flags;
  3095. struct kvfree_rcu_bulk_data *bnode, *n;
  3096. struct list_head bulk_head[FREE_N_CHANNELS];
  3097. struct rcu_head *head;
  3098. struct kfree_rcu_cpu *krcp;
  3099. struct kfree_rcu_cpu_work *krwp;
  3100. struct rcu_gp_oldstate head_gp_snap;
  3101. int i;
  3102. krwp = container_of(to_rcu_work(work),
  3103. struct kfree_rcu_cpu_work, rcu_work);
  3104. krcp = krwp->krcp;
  3105. raw_spin_lock_irqsave(&krcp->lock, flags);
  3106. // Channels 1 and 2.
  3107. for (i = 0; i < FREE_N_CHANNELS; i++)
  3108. list_replace_init(&krwp->bulk_head_free[i], &bulk_head[i]);
  3109. // Channel 3.
  3110. head = krwp->head_free;
  3111. krwp->head_free = NULL;
  3112. head_gp_snap = krwp->head_free_gp_snap;
  3113. raw_spin_unlock_irqrestore(&krcp->lock, flags);
  3114. // Handle the first two channels.
  3115. for (i = 0; i < FREE_N_CHANNELS; i++) {
  3116. // Start from the tail page, so a GP is likely passed for it.
  3117. list_for_each_entry_safe(bnode, n, &bulk_head[i], list)
  3118. kvfree_rcu_bulk(krcp, bnode, i);
  3119. }
  3120. /*
  3121. * This is used when the "bulk" path can not be used for the
  3122. * double-argument of kvfree_rcu(). This happens when the
  3123. * page-cache is empty, which means that objects are instead
  3124. * queued on a linked list through their rcu_head structures.
  3125. * This list is named "Channel 3".
  3126. */
  3127. if (head && !WARN_ON_ONCE(!poll_state_synchronize_rcu_full(&head_gp_snap)))
  3128. kvfree_rcu_list(head);
  3129. }
  3130. static bool
  3131. need_offload_krc(struct kfree_rcu_cpu *krcp)
  3132. {
  3133. int i;
  3134. for (i = 0; i < FREE_N_CHANNELS; i++)
  3135. if (!list_empty(&krcp->bulk_head[i]))
  3136. return true;
  3137. return !!READ_ONCE(krcp->head);
  3138. }
  3139. static bool
  3140. need_wait_for_krwp_work(struct kfree_rcu_cpu_work *krwp)
  3141. {
  3142. int i;
  3143. for (i = 0; i < FREE_N_CHANNELS; i++)
  3144. if (!list_empty(&krwp->bulk_head_free[i]))
  3145. return true;
  3146. return !!krwp->head_free;
  3147. }
  3148. static int krc_count(struct kfree_rcu_cpu *krcp)
  3149. {
  3150. int sum = atomic_read(&krcp->head_count);
  3151. int i;
  3152. for (i = 0; i < FREE_N_CHANNELS; i++)
  3153. sum += atomic_read(&krcp->bulk_count[i]);
  3154. return sum;
  3155. }
  3156. static void
  3157. __schedule_delayed_monitor_work(struct kfree_rcu_cpu *krcp)
  3158. {
  3159. long delay, delay_left;
  3160. delay = krc_count(krcp) >= KVFREE_BULK_MAX_ENTR ? 1:KFREE_DRAIN_JIFFIES;
  3161. if (delayed_work_pending(&krcp->monitor_work)) {
  3162. delay_left = krcp->monitor_work.timer.expires - jiffies;
  3163. if (delay < delay_left)
  3164. mod_delayed_work(rcu_reclaim_wq, &krcp->monitor_work, delay);
  3165. return;
  3166. }
  3167. queue_delayed_work(rcu_reclaim_wq, &krcp->monitor_work, delay);
  3168. }
  3169. static void
  3170. schedule_delayed_monitor_work(struct kfree_rcu_cpu *krcp)
  3171. {
  3172. unsigned long flags;
  3173. raw_spin_lock_irqsave(&krcp->lock, flags);
  3174. __schedule_delayed_monitor_work(krcp);
  3175. raw_spin_unlock_irqrestore(&krcp->lock, flags);
  3176. }
  3177. static void
  3178. kvfree_rcu_drain_ready(struct kfree_rcu_cpu *krcp)
  3179. {
  3180. struct list_head bulk_ready[FREE_N_CHANNELS];
  3181. struct kvfree_rcu_bulk_data *bnode, *n;
  3182. struct rcu_head *head_ready = NULL;
  3183. unsigned long flags;
  3184. int i;
  3185. raw_spin_lock_irqsave(&krcp->lock, flags);
  3186. for (i = 0; i < FREE_N_CHANNELS; i++) {
  3187. INIT_LIST_HEAD(&bulk_ready[i]);
  3188. list_for_each_entry_safe_reverse(bnode, n, &krcp->bulk_head[i], list) {
  3189. if (!poll_state_synchronize_rcu_full(&bnode->gp_snap))
  3190. break;
  3191. atomic_sub(bnode->nr_records, &krcp->bulk_count[i]);
  3192. list_move(&bnode->list, &bulk_ready[i]);
  3193. }
  3194. }
  3195. if (krcp->head && poll_state_synchronize_rcu(krcp->head_gp_snap)) {
  3196. head_ready = krcp->head;
  3197. atomic_set(&krcp->head_count, 0);
  3198. WRITE_ONCE(krcp->head, NULL);
  3199. }
  3200. raw_spin_unlock_irqrestore(&krcp->lock, flags);
  3201. for (i = 0; i < FREE_N_CHANNELS; i++) {
  3202. list_for_each_entry_safe(bnode, n, &bulk_ready[i], list)
  3203. kvfree_rcu_bulk(krcp, bnode, i);
  3204. }
  3205. if (head_ready)
  3206. kvfree_rcu_list(head_ready);
  3207. }
  3208. /*
  3209. * Return: %true if a work is queued, %false otherwise.
  3210. */
  3211. static bool
  3212. kvfree_rcu_queue_batch(struct kfree_rcu_cpu *krcp)
  3213. {
  3214. unsigned long flags;
  3215. bool queued = false;
  3216. int i, j;
  3217. raw_spin_lock_irqsave(&krcp->lock, flags);
  3218. // Attempt to start a new batch.
  3219. for (i = 0; i < KFREE_N_BATCHES; i++) {
  3220. struct kfree_rcu_cpu_work *krwp = &(krcp->krw_arr[i]);
  3221. // Try to detach bulk_head or head and attach it, only when
  3222. // all channels are free. Any channel is not free means at krwp
  3223. // there is on-going rcu work to handle krwp's free business.
  3224. if (need_wait_for_krwp_work(krwp))
  3225. continue;
  3226. // kvfree_rcu_drain_ready() might handle this krcp, if so give up.
  3227. if (need_offload_krc(krcp)) {
  3228. // Channel 1 corresponds to the SLAB-pointer bulk path.
  3229. // Channel 2 corresponds to vmalloc-pointer bulk path.
  3230. for (j = 0; j < FREE_N_CHANNELS; j++) {
  3231. if (list_empty(&krwp->bulk_head_free[j])) {
  3232. atomic_set(&krcp->bulk_count[j], 0);
  3233. list_replace_init(&krcp->bulk_head[j],
  3234. &krwp->bulk_head_free[j]);
  3235. }
  3236. }
  3237. // Channel 3 corresponds to both SLAB and vmalloc
  3238. // objects queued on the linked list.
  3239. if (!krwp->head_free) {
  3240. krwp->head_free = krcp->head;
  3241. get_state_synchronize_rcu_full(&krwp->head_free_gp_snap);
  3242. atomic_set(&krcp->head_count, 0);
  3243. WRITE_ONCE(krcp->head, NULL);
  3244. }
  3245. // One work is per one batch, so there are three
  3246. // "free channels", the batch can handle. Break
  3247. // the loop since it is done with this CPU thus
  3248. // queuing an RCU work is _always_ success here.
  3249. queued = queue_rcu_work(rcu_reclaim_wq, &krwp->rcu_work);
  3250. WARN_ON_ONCE(!queued);
  3251. break;
  3252. }
  3253. }
  3254. raw_spin_unlock_irqrestore(&krcp->lock, flags);
  3255. return queued;
  3256. }
  3257. /*
  3258. * This function is invoked after the KFREE_DRAIN_JIFFIES timeout.
  3259. */
  3260. static void kfree_rcu_monitor(struct work_struct *work)
  3261. {
  3262. struct kfree_rcu_cpu *krcp = container_of(work,
  3263. struct kfree_rcu_cpu, monitor_work.work);
  3264. // Drain ready for reclaim.
  3265. kvfree_rcu_drain_ready(krcp);
  3266. // Queue a batch for a rest.
  3267. kvfree_rcu_queue_batch(krcp);
  3268. // If there is nothing to detach, it means that our job is
  3269. // successfully done here. In case of having at least one
  3270. // of the channels that is still busy we should rearm the
  3271. // work to repeat an attempt. Because previous batches are
  3272. // still in progress.
  3273. if (need_offload_krc(krcp))
  3274. schedule_delayed_monitor_work(krcp);
  3275. }
  3276. static enum hrtimer_restart
  3277. schedule_page_work_fn(struct hrtimer *t)
  3278. {
  3279. struct kfree_rcu_cpu *krcp =
  3280. container_of(t, struct kfree_rcu_cpu, hrtimer);
  3281. queue_delayed_work(system_highpri_wq, &krcp->page_cache_work, 0);
  3282. return HRTIMER_NORESTART;
  3283. }
  3284. static void fill_page_cache_func(struct work_struct *work)
  3285. {
  3286. struct kvfree_rcu_bulk_data *bnode;
  3287. struct kfree_rcu_cpu *krcp =
  3288. container_of(work, struct kfree_rcu_cpu,
  3289. page_cache_work.work);
  3290. unsigned long flags;
  3291. int nr_pages;
  3292. bool pushed;
  3293. int i;
  3294. nr_pages = atomic_read(&krcp->backoff_page_cache_fill) ?
  3295. 1 : rcu_min_cached_objs;
  3296. for (i = READ_ONCE(krcp->nr_bkv_objs); i < nr_pages; i++) {
  3297. bnode = (struct kvfree_rcu_bulk_data *)
  3298. __get_free_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
  3299. if (!bnode)
  3300. break;
  3301. raw_spin_lock_irqsave(&krcp->lock, flags);
  3302. pushed = put_cached_bnode(krcp, bnode);
  3303. raw_spin_unlock_irqrestore(&krcp->lock, flags);
  3304. if (!pushed) {
  3305. free_page((unsigned long) bnode);
  3306. break;
  3307. }
  3308. }
  3309. atomic_set(&krcp->work_in_progress, 0);
  3310. atomic_set(&krcp->backoff_page_cache_fill, 0);
  3311. }
  3312. static void
  3313. run_page_cache_worker(struct kfree_rcu_cpu *krcp)
  3314. {
  3315. // If cache disabled, bail out.
  3316. if (!rcu_min_cached_objs)
  3317. return;
  3318. if (rcu_scheduler_active == RCU_SCHEDULER_RUNNING &&
  3319. !atomic_xchg(&krcp->work_in_progress, 1)) {
  3320. if (atomic_read(&krcp->backoff_page_cache_fill)) {
  3321. queue_delayed_work(rcu_reclaim_wq,
  3322. &krcp->page_cache_work,
  3323. msecs_to_jiffies(rcu_delay_page_cache_fill_msec));
  3324. } else {
  3325. hrtimer_init(&krcp->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  3326. krcp->hrtimer.function = schedule_page_work_fn;
  3327. hrtimer_start(&krcp->hrtimer, 0, HRTIMER_MODE_REL);
  3328. }
  3329. }
  3330. }
  3331. // Record ptr in a page managed by krcp, with the pre-krc_this_cpu_lock()
  3332. // state specified by flags. If can_alloc is true, the caller must
  3333. // be schedulable and not be holding any locks or mutexes that might be
  3334. // acquired by the memory allocator or anything that it might invoke.
  3335. // Returns true if ptr was successfully recorded, else the caller must
  3336. // use a fallback.
  3337. static inline bool
  3338. add_ptr_to_bulk_krc_lock(struct kfree_rcu_cpu **krcp,
  3339. unsigned long *flags, void *ptr, bool can_alloc)
  3340. {
  3341. struct kvfree_rcu_bulk_data *bnode;
  3342. int idx;
  3343. *krcp = krc_this_cpu_lock(flags);
  3344. if (unlikely(!(*krcp)->initialized))
  3345. return false;
  3346. idx = !!is_vmalloc_addr(ptr);
  3347. bnode = list_first_entry_or_null(&(*krcp)->bulk_head[idx],
  3348. struct kvfree_rcu_bulk_data, list);
  3349. /* Check if a new block is required. */
  3350. if (!bnode || bnode->nr_records == KVFREE_BULK_MAX_ENTR) {
  3351. bnode = get_cached_bnode(*krcp);
  3352. if (!bnode && can_alloc) {
  3353. krc_this_cpu_unlock(*krcp, *flags);
  3354. // __GFP_NORETRY - allows a light-weight direct reclaim
  3355. // what is OK from minimizing of fallback hitting point of
  3356. // view. Apart of that it forbids any OOM invoking what is
  3357. // also beneficial since we are about to release memory soon.
  3358. //
  3359. // __GFP_NOMEMALLOC - prevents from consuming of all the
  3360. // memory reserves. Please note we have a fallback path.
  3361. //
  3362. // __GFP_NOWARN - it is supposed that an allocation can
  3363. // be failed under low memory or high memory pressure
  3364. // scenarios.
  3365. bnode = (struct kvfree_rcu_bulk_data *)
  3366. __get_free_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
  3367. raw_spin_lock_irqsave(&(*krcp)->lock, *flags);
  3368. }
  3369. if (!bnode)
  3370. return false;
  3371. // Initialize the new block and attach it.
  3372. bnode->nr_records = 0;
  3373. list_add(&bnode->list, &(*krcp)->bulk_head[idx]);
  3374. }
  3375. // Finally insert and update the GP for this page.
  3376. bnode->nr_records++;
  3377. bnode->records[bnode->nr_records - 1] = ptr;
  3378. get_state_synchronize_rcu_full(&bnode->gp_snap);
  3379. atomic_inc(&(*krcp)->bulk_count[idx]);
  3380. return true;
  3381. }
  3382. /*
  3383. * Queue a request for lazy invocation of the appropriate free routine
  3384. * after a grace period. Please note that three paths are maintained,
  3385. * two for the common case using arrays of pointers and a third one that
  3386. * is used only when the main paths cannot be used, for example, due to
  3387. * memory pressure.
  3388. *
  3389. * Each kvfree_call_rcu() request is added to a batch. The batch will be drained
  3390. * every KFREE_DRAIN_JIFFIES number of jiffies. All the objects in the batch will
  3391. * be free'd in workqueue context. This allows us to: batch requests together to
  3392. * reduce the number of grace periods during heavy kfree_rcu()/kvfree_rcu() load.
  3393. */
  3394. void kvfree_call_rcu(struct rcu_head *head, void *ptr)
  3395. {
  3396. unsigned long flags;
  3397. struct kfree_rcu_cpu *krcp;
  3398. bool success;
  3399. /*
  3400. * Please note there is a limitation for the head-less
  3401. * variant, that is why there is a clear rule for such
  3402. * objects: it can be used from might_sleep() context
  3403. * only. For other places please embed an rcu_head to
  3404. * your data.
  3405. */
  3406. if (!head)
  3407. might_sleep();
  3408. // Queue the object but don't yet schedule the batch.
  3409. if (debug_rcu_head_queue(ptr)) {
  3410. // Probable double kfree_rcu(), just leak.
  3411. WARN_ONCE(1, "%s(): Double-freed call. rcu_head %p\n",
  3412. __func__, head);
  3413. // Mark as success and leave.
  3414. return;
  3415. }
  3416. kasan_record_aux_stack_noalloc(ptr);
  3417. success = add_ptr_to_bulk_krc_lock(&krcp, &flags, ptr, !head);
  3418. if (!success) {
  3419. run_page_cache_worker(krcp);
  3420. if (head == NULL)
  3421. // Inline if kvfree_rcu(one_arg) call.
  3422. goto unlock_return;
  3423. head->func = ptr;
  3424. head->next = krcp->head;
  3425. WRITE_ONCE(krcp->head, head);
  3426. atomic_inc(&krcp->head_count);
  3427. // Take a snapshot for this krcp.
  3428. krcp->head_gp_snap = get_state_synchronize_rcu();
  3429. success = true;
  3430. }
  3431. /*
  3432. * The kvfree_rcu() caller considers the pointer freed at this point
  3433. * and likely removes any references to it. Since the actual slab
  3434. * freeing (and kmemleak_free()) is deferred, tell kmemleak to ignore
  3435. * this object (no scanning or false positives reporting).
  3436. */
  3437. kmemleak_ignore(ptr);
  3438. // Set timer to drain after KFREE_DRAIN_JIFFIES.
  3439. if (rcu_scheduler_active == RCU_SCHEDULER_RUNNING)
  3440. __schedule_delayed_monitor_work(krcp);
  3441. unlock_return:
  3442. krc_this_cpu_unlock(krcp, flags);
  3443. /*
  3444. * Inline kvfree() after synchronize_rcu(). We can do
  3445. * it from might_sleep() context only, so the current
  3446. * CPU can pass the QS state.
  3447. */
  3448. if (!success) {
  3449. debug_rcu_head_unqueue((struct rcu_head *) ptr);
  3450. synchronize_rcu();
  3451. kvfree(ptr);
  3452. }
  3453. }
  3454. EXPORT_SYMBOL_GPL(kvfree_call_rcu);
  3455. /**
  3456. * kvfree_rcu_barrier - Wait until all in-flight kvfree_rcu() complete.
  3457. *
  3458. * Note that a single argument of kvfree_rcu() call has a slow path that
  3459. * triggers synchronize_rcu() following by freeing a pointer. It is done
  3460. * before the return from the function. Therefore for any single-argument
  3461. * call that will result in a kfree() to a cache that is to be destroyed
  3462. * during module exit, it is developer's responsibility to ensure that all
  3463. * such calls have returned before the call to kmem_cache_destroy().
  3464. */
  3465. void kvfree_rcu_barrier(void)
  3466. {
  3467. struct kfree_rcu_cpu_work *krwp;
  3468. struct kfree_rcu_cpu *krcp;
  3469. bool queued;
  3470. int i, cpu;
  3471. /*
  3472. * Firstly we detach objects and queue them over an RCU-batch
  3473. * for all CPUs. Finally queued works are flushed for each CPU.
  3474. *
  3475. * Please note. If there are outstanding batches for a particular
  3476. * CPU, those have to be finished first following by queuing a new.
  3477. */
  3478. for_each_possible_cpu(cpu) {
  3479. krcp = per_cpu_ptr(&krc, cpu);
  3480. /*
  3481. * Check if this CPU has any objects which have been queued for a
  3482. * new GP completion. If not(means nothing to detach), we are done
  3483. * with it. If any batch is pending/running for this "krcp", below
  3484. * per-cpu flush_rcu_work() waits its completion(see last step).
  3485. */
  3486. if (!need_offload_krc(krcp))
  3487. continue;
  3488. while (1) {
  3489. /*
  3490. * If we are not able to queue a new RCU work it means:
  3491. * - batches for this CPU are still in flight which should
  3492. * be flushed first and then repeat;
  3493. * - no objects to detach, because of concurrency.
  3494. */
  3495. queued = kvfree_rcu_queue_batch(krcp);
  3496. /*
  3497. * Bail out, if there is no need to offload this "krcp"
  3498. * anymore. As noted earlier it can run concurrently.
  3499. */
  3500. if (queued || !need_offload_krc(krcp))
  3501. break;
  3502. /* There are ongoing batches. */
  3503. for (i = 0; i < KFREE_N_BATCHES; i++) {
  3504. krwp = &(krcp->krw_arr[i]);
  3505. flush_rcu_work(&krwp->rcu_work);
  3506. }
  3507. }
  3508. }
  3509. /*
  3510. * Now we guarantee that all objects are flushed.
  3511. */
  3512. for_each_possible_cpu(cpu) {
  3513. krcp = per_cpu_ptr(&krc, cpu);
  3514. /*
  3515. * A monitor work can drain ready to reclaim objects
  3516. * directly. Wait its completion if running or pending.
  3517. */
  3518. cancel_delayed_work_sync(&krcp->monitor_work);
  3519. for (i = 0; i < KFREE_N_BATCHES; i++) {
  3520. krwp = &(krcp->krw_arr[i]);
  3521. flush_rcu_work(&krwp->rcu_work);
  3522. }
  3523. }
  3524. }
  3525. EXPORT_SYMBOL_GPL(kvfree_rcu_barrier);
  3526. static unsigned long
  3527. kfree_rcu_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  3528. {
  3529. int cpu;
  3530. unsigned long count = 0;
  3531. /* Snapshot count of all CPUs */
  3532. for_each_possible_cpu(cpu) {
  3533. struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
  3534. count += krc_count(krcp);
  3535. count += READ_ONCE(krcp->nr_bkv_objs);
  3536. atomic_set(&krcp->backoff_page_cache_fill, 1);
  3537. }
  3538. return count == 0 ? SHRINK_EMPTY : count;
  3539. }
  3540. static unsigned long
  3541. kfree_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
  3542. {
  3543. int cpu, freed = 0;
  3544. for_each_possible_cpu(cpu) {
  3545. int count;
  3546. struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
  3547. count = krc_count(krcp);
  3548. count += drain_page_cache(krcp);
  3549. kfree_rcu_monitor(&krcp->monitor_work.work);
  3550. sc->nr_to_scan -= count;
  3551. freed += count;
  3552. if (sc->nr_to_scan <= 0)
  3553. break;
  3554. }
  3555. return freed == 0 ? SHRINK_STOP : freed;
  3556. }
  3557. void __init kfree_rcu_scheduler_running(void)
  3558. {
  3559. int cpu;
  3560. for_each_possible_cpu(cpu) {
  3561. struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
  3562. if (need_offload_krc(krcp))
  3563. schedule_delayed_monitor_work(krcp);
  3564. }
  3565. }
  3566. /*
  3567. * During early boot, any blocking grace-period wait automatically
  3568. * implies a grace period.
  3569. *
  3570. * Later on, this could in theory be the case for kernels built with
  3571. * CONFIG_SMP=y && CONFIG_PREEMPTION=y running on a single CPU, but this
  3572. * is not a common case. Furthermore, this optimization would cause
  3573. * the rcu_gp_oldstate structure to expand by 50%, so this potential
  3574. * grace-period optimization is ignored once the scheduler is running.
  3575. */
  3576. static int rcu_blocking_is_gp(void)
  3577. {
  3578. if (rcu_scheduler_active != RCU_SCHEDULER_INACTIVE) {
  3579. might_sleep();
  3580. return false;
  3581. }
  3582. return true;
  3583. }
  3584. /*
  3585. * Helper function for the synchronize_rcu() API.
  3586. */
  3587. static void synchronize_rcu_normal(void)
  3588. {
  3589. struct rcu_synchronize rs;
  3590. trace_rcu_sr_normal(rcu_state.name, &rs.head, TPS("request"));
  3591. if (!READ_ONCE(rcu_normal_wake_from_gp)) {
  3592. wait_rcu_gp(call_rcu_hurry);
  3593. goto trace_complete_out;
  3594. }
  3595. init_rcu_head_on_stack(&rs.head);
  3596. init_completion(&rs.completion);
  3597. /*
  3598. * This code might be preempted, therefore take a GP
  3599. * snapshot before adding a request.
  3600. */
  3601. if (IS_ENABLED(CONFIG_PROVE_RCU))
  3602. rs.head.func = (void *) get_state_synchronize_rcu();
  3603. rcu_sr_normal_add_req(&rs);
  3604. /* Kick a GP and start waiting. */
  3605. (void) start_poll_synchronize_rcu();
  3606. /* Now we can wait. */
  3607. wait_for_completion(&rs.completion);
  3608. destroy_rcu_head_on_stack(&rs.head);
  3609. trace_complete_out:
  3610. trace_rcu_sr_normal(rcu_state.name, &rs.head, TPS("complete"));
  3611. }
  3612. /**
  3613. * synchronize_rcu - wait until a grace period has elapsed.
  3614. *
  3615. * Control will return to the caller some time after a full grace
  3616. * period has elapsed, in other words after all currently executing RCU
  3617. * read-side critical sections have completed. Note, however, that
  3618. * upon return from synchronize_rcu(), the caller might well be executing
  3619. * concurrently with new RCU read-side critical sections that began while
  3620. * synchronize_rcu() was waiting.
  3621. *
  3622. * RCU read-side critical sections are delimited by rcu_read_lock()
  3623. * and rcu_read_unlock(), and may be nested. In addition, but only in
  3624. * v5.0 and later, regions of code across which interrupts, preemption,
  3625. * or softirqs have been disabled also serve as RCU read-side critical
  3626. * sections. This includes hardware interrupt handlers, softirq handlers,
  3627. * and NMI handlers.
  3628. *
  3629. * Note that this guarantee implies further memory-ordering guarantees.
  3630. * On systems with more than one CPU, when synchronize_rcu() returns,
  3631. * each CPU is guaranteed to have executed a full memory barrier since
  3632. * the end of its last RCU read-side critical section whose beginning
  3633. * preceded the call to synchronize_rcu(). In addition, each CPU having
  3634. * an RCU read-side critical section that extends beyond the return from
  3635. * synchronize_rcu() is guaranteed to have executed a full memory barrier
  3636. * after the beginning of synchronize_rcu() and before the beginning of
  3637. * that RCU read-side critical section. Note that these guarantees include
  3638. * CPUs that are offline, idle, or executing in user mode, as well as CPUs
  3639. * that are executing in the kernel.
  3640. *
  3641. * Furthermore, if CPU A invoked synchronize_rcu(), which returned
  3642. * to its caller on CPU B, then both CPU A and CPU B are guaranteed
  3643. * to have executed a full memory barrier during the execution of
  3644. * synchronize_rcu() -- even if CPU A and CPU B are the same CPU (but
  3645. * again only if the system has more than one CPU).
  3646. *
  3647. * Implementation of these memory-ordering guarantees is described here:
  3648. * Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.rst.
  3649. */
  3650. void synchronize_rcu(void)
  3651. {
  3652. unsigned long flags;
  3653. struct rcu_node *rnp;
  3654. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
  3655. lock_is_held(&rcu_lock_map) ||
  3656. lock_is_held(&rcu_sched_lock_map),
  3657. "Illegal synchronize_rcu() in RCU read-side critical section");
  3658. if (!rcu_blocking_is_gp()) {
  3659. if (rcu_gp_is_expedited())
  3660. synchronize_rcu_expedited();
  3661. else
  3662. synchronize_rcu_normal();
  3663. return;
  3664. }
  3665. // Context allows vacuous grace periods.
  3666. // Note well that this code runs with !PREEMPT && !SMP.
  3667. // In addition, all code that advances grace periods runs at
  3668. // process level. Therefore, this normal GP overlaps with other
  3669. // normal GPs only by being fully nested within them, which allows
  3670. // reuse of ->gp_seq_polled_snap.
  3671. rcu_poll_gp_seq_start_unlocked(&rcu_state.gp_seq_polled_snap);
  3672. rcu_poll_gp_seq_end_unlocked(&rcu_state.gp_seq_polled_snap);
  3673. // Update the normal grace-period counters to record
  3674. // this grace period, but only those used by the boot CPU.
  3675. // The rcu_scheduler_starting() will take care of the rest of
  3676. // these counters.
  3677. local_irq_save(flags);
  3678. WARN_ON_ONCE(num_online_cpus() > 1);
  3679. rcu_state.gp_seq += (1 << RCU_SEQ_CTR_SHIFT);
  3680. for (rnp = this_cpu_ptr(&rcu_data)->mynode; rnp; rnp = rnp->parent)
  3681. rnp->gp_seq_needed = rnp->gp_seq = rcu_state.gp_seq;
  3682. local_irq_restore(flags);
  3683. }
  3684. EXPORT_SYMBOL_GPL(synchronize_rcu);
  3685. /**
  3686. * get_completed_synchronize_rcu_full - Return a full pre-completed polled state cookie
  3687. * @rgosp: Place to put state cookie
  3688. *
  3689. * Stores into @rgosp a value that will always be treated by functions
  3690. * like poll_state_synchronize_rcu_full() as a cookie whose grace period
  3691. * has already completed.
  3692. */
  3693. void get_completed_synchronize_rcu_full(struct rcu_gp_oldstate *rgosp)
  3694. {
  3695. rgosp->rgos_norm = RCU_GET_STATE_COMPLETED;
  3696. rgosp->rgos_exp = RCU_GET_STATE_COMPLETED;
  3697. }
  3698. EXPORT_SYMBOL_GPL(get_completed_synchronize_rcu_full);
  3699. /**
  3700. * get_state_synchronize_rcu - Snapshot current RCU state
  3701. *
  3702. * Returns a cookie that is used by a later call to cond_synchronize_rcu()
  3703. * or poll_state_synchronize_rcu() to determine whether or not a full
  3704. * grace period has elapsed in the meantime.
  3705. */
  3706. unsigned long get_state_synchronize_rcu(void)
  3707. {
  3708. /*
  3709. * Any prior manipulation of RCU-protected data must happen
  3710. * before the load from ->gp_seq.
  3711. */
  3712. smp_mb(); /* ^^^ */
  3713. return rcu_seq_snap(&rcu_state.gp_seq_polled);
  3714. }
  3715. EXPORT_SYMBOL_GPL(get_state_synchronize_rcu);
  3716. /**
  3717. * get_state_synchronize_rcu_full - Snapshot RCU state, both normal and expedited
  3718. * @rgosp: location to place combined normal/expedited grace-period state
  3719. *
  3720. * Places the normal and expedited grace-period states in @rgosp. This
  3721. * state value can be passed to a later call to cond_synchronize_rcu_full()
  3722. * or poll_state_synchronize_rcu_full() to determine whether or not a
  3723. * grace period (whether normal or expedited) has elapsed in the meantime.
  3724. * The rcu_gp_oldstate structure takes up twice the memory of an unsigned
  3725. * long, but is guaranteed to see all grace periods. In contrast, the
  3726. * combined state occupies less memory, but can sometimes fail to take
  3727. * grace periods into account.
  3728. *
  3729. * This does not guarantee that the needed grace period will actually
  3730. * start.
  3731. */
  3732. void get_state_synchronize_rcu_full(struct rcu_gp_oldstate *rgosp)
  3733. {
  3734. /*
  3735. * Any prior manipulation of RCU-protected data must happen
  3736. * before the loads from ->gp_seq and ->expedited_sequence.
  3737. */
  3738. smp_mb(); /* ^^^ */
  3739. // Yes, rcu_state.gp_seq, not rnp_root->gp_seq, the latter's use
  3740. // in poll_state_synchronize_rcu_full() notwithstanding. Use of
  3741. // the latter here would result in too-short grace periods due to
  3742. // interactions with newly onlined CPUs.
  3743. rgosp->rgos_norm = rcu_seq_snap(&rcu_state.gp_seq);
  3744. rgosp->rgos_exp = rcu_seq_snap(&rcu_state.expedited_sequence);
  3745. }
  3746. EXPORT_SYMBOL_GPL(get_state_synchronize_rcu_full);
  3747. /*
  3748. * Helper function for start_poll_synchronize_rcu() and
  3749. * start_poll_synchronize_rcu_full().
  3750. */
  3751. static void start_poll_synchronize_rcu_common(void)
  3752. {
  3753. unsigned long flags;
  3754. bool needwake;
  3755. struct rcu_data *rdp;
  3756. struct rcu_node *rnp;
  3757. lockdep_assert_irqs_enabled();
  3758. local_irq_save(flags);
  3759. rdp = this_cpu_ptr(&rcu_data);
  3760. rnp = rdp->mynode;
  3761. raw_spin_lock_rcu_node(rnp); // irqs already disabled.
  3762. // Note it is possible for a grace period to have elapsed between
  3763. // the above call to get_state_synchronize_rcu() and the below call
  3764. // to rcu_seq_snap. This is OK, the worst that happens is that we
  3765. // get a grace period that no one needed. These accesses are ordered
  3766. // by smp_mb(), and we are accessing them in the opposite order
  3767. // from which they are updated at grace-period start, as required.
  3768. needwake = rcu_start_this_gp(rnp, rdp, rcu_seq_snap(&rcu_state.gp_seq));
  3769. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  3770. if (needwake)
  3771. rcu_gp_kthread_wake();
  3772. }
  3773. /**
  3774. * start_poll_synchronize_rcu - Snapshot and start RCU grace period
  3775. *
  3776. * Returns a cookie that is used by a later call to cond_synchronize_rcu()
  3777. * or poll_state_synchronize_rcu() to determine whether or not a full
  3778. * grace period has elapsed in the meantime. If the needed grace period
  3779. * is not already slated to start, notifies RCU core of the need for that
  3780. * grace period.
  3781. *
  3782. * Interrupts must be enabled for the case where it is necessary to awaken
  3783. * the grace-period kthread.
  3784. */
  3785. unsigned long start_poll_synchronize_rcu(void)
  3786. {
  3787. unsigned long gp_seq = get_state_synchronize_rcu();
  3788. start_poll_synchronize_rcu_common();
  3789. return gp_seq;
  3790. }
  3791. EXPORT_SYMBOL_GPL(start_poll_synchronize_rcu);
  3792. /**
  3793. * start_poll_synchronize_rcu_full - Take a full snapshot and start RCU grace period
  3794. * @rgosp: value from get_state_synchronize_rcu_full() or start_poll_synchronize_rcu_full()
  3795. *
  3796. * Places the normal and expedited grace-period states in *@rgos. This
  3797. * state value can be passed to a later call to cond_synchronize_rcu_full()
  3798. * or poll_state_synchronize_rcu_full() to determine whether or not a
  3799. * grace period (whether normal or expedited) has elapsed in the meantime.
  3800. * If the needed grace period is not already slated to start, notifies
  3801. * RCU core of the need for that grace period.
  3802. *
  3803. * Interrupts must be enabled for the case where it is necessary to awaken
  3804. * the grace-period kthread.
  3805. */
  3806. void start_poll_synchronize_rcu_full(struct rcu_gp_oldstate *rgosp)
  3807. {
  3808. get_state_synchronize_rcu_full(rgosp);
  3809. start_poll_synchronize_rcu_common();
  3810. }
  3811. EXPORT_SYMBOL_GPL(start_poll_synchronize_rcu_full);
  3812. /**
  3813. * poll_state_synchronize_rcu - Has the specified RCU grace period completed?
  3814. * @oldstate: value from get_state_synchronize_rcu() or start_poll_synchronize_rcu()
  3815. *
  3816. * If a full RCU grace period has elapsed since the earlier call from
  3817. * which @oldstate was obtained, return @true, otherwise return @false.
  3818. * If @false is returned, it is the caller's responsibility to invoke this
  3819. * function later on until it does return @true. Alternatively, the caller
  3820. * can explicitly wait for a grace period, for example, by passing @oldstate
  3821. * to either cond_synchronize_rcu() or cond_synchronize_rcu_expedited()
  3822. * on the one hand or by directly invoking either synchronize_rcu() or
  3823. * synchronize_rcu_expedited() on the other.
  3824. *
  3825. * Yes, this function does not take counter wrap into account.
  3826. * But counter wrap is harmless. If the counter wraps, we have waited for
  3827. * more than a billion grace periods (and way more on a 64-bit system!).
  3828. * Those needing to keep old state values for very long time periods
  3829. * (many hours even on 32-bit systems) should check them occasionally and
  3830. * either refresh them or set a flag indicating that the grace period has
  3831. * completed. Alternatively, they can use get_completed_synchronize_rcu()
  3832. * to get a guaranteed-completed grace-period state.
  3833. *
  3834. * In addition, because oldstate compresses the grace-period state for
  3835. * both normal and expedited grace periods into a single unsigned long,
  3836. * it can miss a grace period when synchronize_rcu() runs concurrently
  3837. * with synchronize_rcu_expedited(). If this is unacceptable, please
  3838. * instead use the _full() variant of these polling APIs.
  3839. *
  3840. * This function provides the same memory-ordering guarantees that
  3841. * would be provided by a synchronize_rcu() that was invoked at the call
  3842. * to the function that provided @oldstate, and that returned at the end
  3843. * of this function.
  3844. */
  3845. bool poll_state_synchronize_rcu(unsigned long oldstate)
  3846. {
  3847. if (oldstate == RCU_GET_STATE_COMPLETED ||
  3848. rcu_seq_done_exact(&rcu_state.gp_seq_polled, oldstate)) {
  3849. smp_mb(); /* Ensure GP ends before subsequent accesses. */
  3850. return true;
  3851. }
  3852. return false;
  3853. }
  3854. EXPORT_SYMBOL_GPL(poll_state_synchronize_rcu);
  3855. /**
  3856. * poll_state_synchronize_rcu_full - Has the specified RCU grace period completed?
  3857. * @rgosp: value from get_state_synchronize_rcu_full() or start_poll_synchronize_rcu_full()
  3858. *
  3859. * If a full RCU grace period has elapsed since the earlier call from
  3860. * which *rgosp was obtained, return @true, otherwise return @false.
  3861. * If @false is returned, it is the caller's responsibility to invoke this
  3862. * function later on until it does return @true. Alternatively, the caller
  3863. * can explicitly wait for a grace period, for example, by passing @rgosp
  3864. * to cond_synchronize_rcu() or by directly invoking synchronize_rcu().
  3865. *
  3866. * Yes, this function does not take counter wrap into account.
  3867. * But counter wrap is harmless. If the counter wraps, we have waited
  3868. * for more than a billion grace periods (and way more on a 64-bit
  3869. * system!). Those needing to keep rcu_gp_oldstate values for very
  3870. * long time periods (many hours even on 32-bit systems) should check
  3871. * them occasionally and either refresh them or set a flag indicating
  3872. * that the grace period has completed. Alternatively, they can use
  3873. * get_completed_synchronize_rcu_full() to get a guaranteed-completed
  3874. * grace-period state.
  3875. *
  3876. * This function provides the same memory-ordering guarantees that would
  3877. * be provided by a synchronize_rcu() that was invoked at the call to
  3878. * the function that provided @rgosp, and that returned at the end of this
  3879. * function. And this guarantee requires that the root rcu_node structure's
  3880. * ->gp_seq field be checked instead of that of the rcu_state structure.
  3881. * The problem is that the just-ending grace-period's callbacks can be
  3882. * invoked between the time that the root rcu_node structure's ->gp_seq
  3883. * field is updated and the time that the rcu_state structure's ->gp_seq
  3884. * field is updated. Therefore, if a single synchronize_rcu() is to
  3885. * cause a subsequent poll_state_synchronize_rcu_full() to return @true,
  3886. * then the root rcu_node structure is the one that needs to be polled.
  3887. */
  3888. bool poll_state_synchronize_rcu_full(struct rcu_gp_oldstate *rgosp)
  3889. {
  3890. struct rcu_node *rnp = rcu_get_root();
  3891. smp_mb(); // Order against root rcu_node structure grace-period cleanup.
  3892. if (rgosp->rgos_norm == RCU_GET_STATE_COMPLETED ||
  3893. rcu_seq_done_exact(&rnp->gp_seq, rgosp->rgos_norm) ||
  3894. rgosp->rgos_exp == RCU_GET_STATE_COMPLETED ||
  3895. rcu_seq_done_exact(&rcu_state.expedited_sequence, rgosp->rgos_exp)) {
  3896. smp_mb(); /* Ensure GP ends before subsequent accesses. */
  3897. return true;
  3898. }
  3899. return false;
  3900. }
  3901. EXPORT_SYMBOL_GPL(poll_state_synchronize_rcu_full);
  3902. /**
  3903. * cond_synchronize_rcu - Conditionally wait for an RCU grace period
  3904. * @oldstate: value from get_state_synchronize_rcu(), start_poll_synchronize_rcu(), or start_poll_synchronize_rcu_expedited()
  3905. *
  3906. * If a full RCU grace period has elapsed since the earlier call to
  3907. * get_state_synchronize_rcu() or start_poll_synchronize_rcu(), just return.
  3908. * Otherwise, invoke synchronize_rcu() to wait for a full grace period.
  3909. *
  3910. * Yes, this function does not take counter wrap into account.
  3911. * But counter wrap is harmless. If the counter wraps, we have waited for
  3912. * more than 2 billion grace periods (and way more on a 64-bit system!),
  3913. * so waiting for a couple of additional grace periods should be just fine.
  3914. *
  3915. * This function provides the same memory-ordering guarantees that
  3916. * would be provided by a synchronize_rcu() that was invoked at the call
  3917. * to the function that provided @oldstate and that returned at the end
  3918. * of this function.
  3919. */
  3920. void cond_synchronize_rcu(unsigned long oldstate)
  3921. {
  3922. if (!poll_state_synchronize_rcu(oldstate))
  3923. synchronize_rcu();
  3924. }
  3925. EXPORT_SYMBOL_GPL(cond_synchronize_rcu);
  3926. /**
  3927. * cond_synchronize_rcu_full - Conditionally wait for an RCU grace period
  3928. * @rgosp: value from get_state_synchronize_rcu_full(), start_poll_synchronize_rcu_full(), or start_poll_synchronize_rcu_expedited_full()
  3929. *
  3930. * If a full RCU grace period has elapsed since the call to
  3931. * get_state_synchronize_rcu_full(), start_poll_synchronize_rcu_full(),
  3932. * or start_poll_synchronize_rcu_expedited_full() from which @rgosp was
  3933. * obtained, just return. Otherwise, invoke synchronize_rcu() to wait
  3934. * for a full grace period.
  3935. *
  3936. * Yes, this function does not take counter wrap into account.
  3937. * But counter wrap is harmless. If the counter wraps, we have waited for
  3938. * more than 2 billion grace periods (and way more on a 64-bit system!),
  3939. * so waiting for a couple of additional grace periods should be just fine.
  3940. *
  3941. * This function provides the same memory-ordering guarantees that
  3942. * would be provided by a synchronize_rcu() that was invoked at the call
  3943. * to the function that provided @rgosp and that returned at the end of
  3944. * this function.
  3945. */
  3946. void cond_synchronize_rcu_full(struct rcu_gp_oldstate *rgosp)
  3947. {
  3948. if (!poll_state_synchronize_rcu_full(rgosp))
  3949. synchronize_rcu();
  3950. }
  3951. EXPORT_SYMBOL_GPL(cond_synchronize_rcu_full);
  3952. /*
  3953. * Check to see if there is any immediate RCU-related work to be done by
  3954. * the current CPU, returning 1 if so and zero otherwise. The checks are
  3955. * in order of increasing expense: checks that can be carried out against
  3956. * CPU-local state are performed first. However, we must check for CPU
  3957. * stalls first, else we might not get a chance.
  3958. */
  3959. static int rcu_pending(int user)
  3960. {
  3961. bool gp_in_progress;
  3962. struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
  3963. struct rcu_node *rnp = rdp->mynode;
  3964. lockdep_assert_irqs_disabled();
  3965. /* Check for CPU stalls, if enabled. */
  3966. check_cpu_stall(rdp);
  3967. /* Does this CPU need a deferred NOCB wakeup? */
  3968. if (rcu_nocb_need_deferred_wakeup(rdp, RCU_NOCB_WAKE))
  3969. return 1;
  3970. /* Is this a nohz_full CPU in userspace or idle? (Ignore RCU if so.) */
  3971. gp_in_progress = rcu_gp_in_progress();
  3972. if ((user || rcu_is_cpu_rrupt_from_idle() ||
  3973. (gp_in_progress &&
  3974. time_before(jiffies, READ_ONCE(rcu_state.gp_start) +
  3975. nohz_full_patience_delay_jiffies))) &&
  3976. rcu_nohz_full_cpu())
  3977. return 0;
  3978. /* Is the RCU core waiting for a quiescent state from this CPU? */
  3979. if (rdp->core_needs_qs && !rdp->cpu_no_qs.b.norm && gp_in_progress)
  3980. return 1;
  3981. /* Does this CPU have callbacks ready to invoke? */
  3982. if (!rcu_rdp_is_offloaded(rdp) &&
  3983. rcu_segcblist_ready_cbs(&rdp->cblist))
  3984. return 1;
  3985. /* Has RCU gone idle with this CPU needing another grace period? */
  3986. if (!gp_in_progress && rcu_segcblist_is_enabled(&rdp->cblist) &&
  3987. !rcu_rdp_is_offloaded(rdp) &&
  3988. !rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
  3989. return 1;
  3990. /* Have RCU grace period completed or started? */
  3991. if (rcu_seq_current(&rnp->gp_seq) != rdp->gp_seq ||
  3992. unlikely(READ_ONCE(rdp->gpwrap))) /* outside lock */
  3993. return 1;
  3994. /* nothing to do */
  3995. return 0;
  3996. }
  3997. /*
  3998. * Helper function for rcu_barrier() tracing. If tracing is disabled,
  3999. * the compiler is expected to optimize this away.
  4000. */
  4001. static void rcu_barrier_trace(const char *s, int cpu, unsigned long done)
  4002. {
  4003. trace_rcu_barrier(rcu_state.name, s, cpu,
  4004. atomic_read(&rcu_state.barrier_cpu_count), done);
  4005. }
  4006. /*
  4007. * RCU callback function for rcu_barrier(). If we are last, wake
  4008. * up the task executing rcu_barrier().
  4009. *
  4010. * Note that the value of rcu_state.barrier_sequence must be captured
  4011. * before the atomic_dec_and_test(). Otherwise, if this CPU is not last,
  4012. * other CPUs might count the value down to zero before this CPU gets
  4013. * around to invoking rcu_barrier_trace(), which might result in bogus
  4014. * data from the next instance of rcu_barrier().
  4015. */
  4016. static void rcu_barrier_callback(struct rcu_head *rhp)
  4017. {
  4018. unsigned long __maybe_unused s = rcu_state.barrier_sequence;
  4019. rhp->next = rhp; // Mark the callback as having been invoked.
  4020. if (atomic_dec_and_test(&rcu_state.barrier_cpu_count)) {
  4021. rcu_barrier_trace(TPS("LastCB"), -1, s);
  4022. complete(&rcu_state.barrier_completion);
  4023. } else {
  4024. rcu_barrier_trace(TPS("CB"), -1, s);
  4025. }
  4026. }
  4027. /*
  4028. * If needed, entrain an rcu_barrier() callback on rdp->cblist.
  4029. */
  4030. static void rcu_barrier_entrain(struct rcu_data *rdp)
  4031. {
  4032. unsigned long gseq = READ_ONCE(rcu_state.barrier_sequence);
  4033. unsigned long lseq = READ_ONCE(rdp->barrier_seq_snap);
  4034. bool wake_nocb = false;
  4035. bool was_alldone = false;
  4036. lockdep_assert_held(&rcu_state.barrier_lock);
  4037. if (rcu_seq_state(lseq) || !rcu_seq_state(gseq) || rcu_seq_ctr(lseq) != rcu_seq_ctr(gseq))
  4038. return;
  4039. rcu_barrier_trace(TPS("IRQ"), -1, rcu_state.barrier_sequence);
  4040. rdp->barrier_head.func = rcu_barrier_callback;
  4041. debug_rcu_head_queue(&rdp->barrier_head);
  4042. rcu_nocb_lock(rdp);
  4043. /*
  4044. * Flush bypass and wakeup rcuog if we add callbacks to an empty regular
  4045. * queue. This way we don't wait for bypass timer that can reach seconds
  4046. * if it's fully lazy.
  4047. */
  4048. was_alldone = rcu_rdp_is_offloaded(rdp) && !rcu_segcblist_pend_cbs(&rdp->cblist);
  4049. WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, jiffies, false));
  4050. wake_nocb = was_alldone && rcu_segcblist_pend_cbs(&rdp->cblist);
  4051. if (rcu_segcblist_entrain(&rdp->cblist, &rdp->barrier_head)) {
  4052. atomic_inc(&rcu_state.barrier_cpu_count);
  4053. } else {
  4054. debug_rcu_head_unqueue(&rdp->barrier_head);
  4055. rcu_barrier_trace(TPS("IRQNQ"), -1, rcu_state.barrier_sequence);
  4056. }
  4057. rcu_nocb_unlock(rdp);
  4058. if (wake_nocb)
  4059. wake_nocb_gp(rdp, false);
  4060. smp_store_release(&rdp->barrier_seq_snap, gseq);
  4061. }
  4062. /*
  4063. * Called with preemption disabled, and from cross-cpu IRQ context.
  4064. */
  4065. static void rcu_barrier_handler(void *cpu_in)
  4066. {
  4067. uintptr_t cpu = (uintptr_t)cpu_in;
  4068. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  4069. lockdep_assert_irqs_disabled();
  4070. WARN_ON_ONCE(cpu != rdp->cpu);
  4071. WARN_ON_ONCE(cpu != smp_processor_id());
  4072. raw_spin_lock(&rcu_state.barrier_lock);
  4073. rcu_barrier_entrain(rdp);
  4074. raw_spin_unlock(&rcu_state.barrier_lock);
  4075. }
  4076. /**
  4077. * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
  4078. *
  4079. * Note that this primitive does not necessarily wait for an RCU grace period
  4080. * to complete. For example, if there are no RCU callbacks queued anywhere
  4081. * in the system, then rcu_barrier() is within its rights to return
  4082. * immediately, without waiting for anything, much less an RCU grace period.
  4083. */
  4084. void rcu_barrier(void)
  4085. {
  4086. uintptr_t cpu;
  4087. unsigned long flags;
  4088. unsigned long gseq;
  4089. struct rcu_data *rdp;
  4090. unsigned long s = rcu_seq_snap(&rcu_state.barrier_sequence);
  4091. rcu_barrier_trace(TPS("Begin"), -1, s);
  4092. /* Take mutex to serialize concurrent rcu_barrier() requests. */
  4093. mutex_lock(&rcu_state.barrier_mutex);
  4094. /* Did someone else do our work for us? */
  4095. if (rcu_seq_done(&rcu_state.barrier_sequence, s)) {
  4096. rcu_barrier_trace(TPS("EarlyExit"), -1, rcu_state.barrier_sequence);
  4097. smp_mb(); /* caller's subsequent code after above check. */
  4098. mutex_unlock(&rcu_state.barrier_mutex);
  4099. return;
  4100. }
  4101. /* Mark the start of the barrier operation. */
  4102. raw_spin_lock_irqsave(&rcu_state.barrier_lock, flags);
  4103. rcu_seq_start(&rcu_state.barrier_sequence);
  4104. gseq = rcu_state.barrier_sequence;
  4105. rcu_barrier_trace(TPS("Inc1"), -1, rcu_state.barrier_sequence);
  4106. /*
  4107. * Initialize the count to two rather than to zero in order
  4108. * to avoid a too-soon return to zero in case of an immediate
  4109. * invocation of the just-enqueued callback (or preemption of
  4110. * this task). Exclude CPU-hotplug operations to ensure that no
  4111. * offline non-offloaded CPU has callbacks queued.
  4112. */
  4113. init_completion(&rcu_state.barrier_completion);
  4114. atomic_set(&rcu_state.barrier_cpu_count, 2);
  4115. raw_spin_unlock_irqrestore(&rcu_state.barrier_lock, flags);
  4116. /*
  4117. * Force each CPU with callbacks to register a new callback.
  4118. * When that callback is invoked, we will know that all of the
  4119. * corresponding CPU's preceding callbacks have been invoked.
  4120. */
  4121. for_each_possible_cpu(cpu) {
  4122. rdp = per_cpu_ptr(&rcu_data, cpu);
  4123. retry:
  4124. if (smp_load_acquire(&rdp->barrier_seq_snap) == gseq)
  4125. continue;
  4126. raw_spin_lock_irqsave(&rcu_state.barrier_lock, flags);
  4127. if (!rcu_segcblist_n_cbs(&rdp->cblist)) {
  4128. WRITE_ONCE(rdp->barrier_seq_snap, gseq);
  4129. raw_spin_unlock_irqrestore(&rcu_state.barrier_lock, flags);
  4130. rcu_barrier_trace(TPS("NQ"), cpu, rcu_state.barrier_sequence);
  4131. continue;
  4132. }
  4133. if (!rcu_rdp_cpu_online(rdp)) {
  4134. rcu_barrier_entrain(rdp);
  4135. WARN_ON_ONCE(READ_ONCE(rdp->barrier_seq_snap) != gseq);
  4136. raw_spin_unlock_irqrestore(&rcu_state.barrier_lock, flags);
  4137. rcu_barrier_trace(TPS("OfflineNoCBQ"), cpu, rcu_state.barrier_sequence);
  4138. continue;
  4139. }
  4140. raw_spin_unlock_irqrestore(&rcu_state.barrier_lock, flags);
  4141. if (smp_call_function_single(cpu, rcu_barrier_handler, (void *)cpu, 1)) {
  4142. schedule_timeout_uninterruptible(1);
  4143. goto retry;
  4144. }
  4145. WARN_ON_ONCE(READ_ONCE(rdp->barrier_seq_snap) != gseq);
  4146. rcu_barrier_trace(TPS("OnlineQ"), cpu, rcu_state.barrier_sequence);
  4147. }
  4148. /*
  4149. * Now that we have an rcu_barrier_callback() callback on each
  4150. * CPU, and thus each counted, remove the initial count.
  4151. */
  4152. if (atomic_sub_and_test(2, &rcu_state.barrier_cpu_count))
  4153. complete(&rcu_state.barrier_completion);
  4154. /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
  4155. wait_for_completion(&rcu_state.barrier_completion);
  4156. /* Mark the end of the barrier operation. */
  4157. rcu_barrier_trace(TPS("Inc2"), -1, rcu_state.barrier_sequence);
  4158. rcu_seq_end(&rcu_state.barrier_sequence);
  4159. gseq = rcu_state.barrier_sequence;
  4160. for_each_possible_cpu(cpu) {
  4161. rdp = per_cpu_ptr(&rcu_data, cpu);
  4162. WRITE_ONCE(rdp->barrier_seq_snap, gseq);
  4163. }
  4164. /* Other rcu_barrier() invocations can now safely proceed. */
  4165. mutex_unlock(&rcu_state.barrier_mutex);
  4166. }
  4167. EXPORT_SYMBOL_GPL(rcu_barrier);
  4168. static unsigned long rcu_barrier_last_throttle;
  4169. /**
  4170. * rcu_barrier_throttled - Do rcu_barrier(), but limit to one per second
  4171. *
  4172. * This can be thought of as guard rails around rcu_barrier() that
  4173. * permits unrestricted userspace use, at least assuming the hardware's
  4174. * try_cmpxchg() is robust. There will be at most one call per second to
  4175. * rcu_barrier() system-wide from use of this function, which means that
  4176. * callers might needlessly wait a second or three.
  4177. *
  4178. * This is intended for use by test suites to avoid OOM by flushing RCU
  4179. * callbacks from the previous test before starting the next. See the
  4180. * rcutree.do_rcu_barrier module parameter for more information.
  4181. *
  4182. * Why not simply make rcu_barrier() more scalable? That might be
  4183. * the eventual endpoint, but let's keep it simple for the time being.
  4184. * Note that the module parameter infrastructure serializes calls to a
  4185. * given .set() function, but should concurrent .set() invocation ever be
  4186. * possible, we are ready!
  4187. */
  4188. static void rcu_barrier_throttled(void)
  4189. {
  4190. unsigned long j = jiffies;
  4191. unsigned long old = READ_ONCE(rcu_barrier_last_throttle);
  4192. unsigned long s = rcu_seq_snap(&rcu_state.barrier_sequence);
  4193. while (time_in_range(j, old, old + HZ / 16) ||
  4194. !try_cmpxchg(&rcu_barrier_last_throttle, &old, j)) {
  4195. schedule_timeout_idle(HZ / 16);
  4196. if (rcu_seq_done(&rcu_state.barrier_sequence, s)) {
  4197. smp_mb(); /* caller's subsequent code after above check. */
  4198. return;
  4199. }
  4200. j = jiffies;
  4201. old = READ_ONCE(rcu_barrier_last_throttle);
  4202. }
  4203. rcu_barrier();
  4204. }
  4205. /*
  4206. * Invoke rcu_barrier_throttled() when a rcutree.do_rcu_barrier
  4207. * request arrives. We insist on a true value to allow for possible
  4208. * future expansion.
  4209. */
  4210. static int param_set_do_rcu_barrier(const char *val, const struct kernel_param *kp)
  4211. {
  4212. bool b;
  4213. int ret;
  4214. if (rcu_scheduler_active != RCU_SCHEDULER_RUNNING)
  4215. return -EAGAIN;
  4216. ret = kstrtobool(val, &b);
  4217. if (!ret && b) {
  4218. atomic_inc((atomic_t *)kp->arg);
  4219. rcu_barrier_throttled();
  4220. atomic_dec((atomic_t *)kp->arg);
  4221. }
  4222. return ret;
  4223. }
  4224. /*
  4225. * Output the number of outstanding rcutree.do_rcu_barrier requests.
  4226. */
  4227. static int param_get_do_rcu_barrier(char *buffer, const struct kernel_param *kp)
  4228. {
  4229. return sprintf(buffer, "%d\n", atomic_read((atomic_t *)kp->arg));
  4230. }
  4231. static const struct kernel_param_ops do_rcu_barrier_ops = {
  4232. .set = param_set_do_rcu_barrier,
  4233. .get = param_get_do_rcu_barrier,
  4234. };
  4235. static atomic_t do_rcu_barrier;
  4236. module_param_cb(do_rcu_barrier, &do_rcu_barrier_ops, &do_rcu_barrier, 0644);
  4237. /*
  4238. * Compute the mask of online CPUs for the specified rcu_node structure.
  4239. * This will not be stable unless the rcu_node structure's ->lock is
  4240. * held, but the bit corresponding to the current CPU will be stable
  4241. * in most contexts.
  4242. */
  4243. static unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp)
  4244. {
  4245. return READ_ONCE(rnp->qsmaskinitnext);
  4246. }
  4247. /*
  4248. * Is the CPU corresponding to the specified rcu_data structure online
  4249. * from RCU's perspective? This perspective is given by that structure's
  4250. * ->qsmaskinitnext field rather than by the global cpu_online_mask.
  4251. */
  4252. static bool rcu_rdp_cpu_online(struct rcu_data *rdp)
  4253. {
  4254. return !!(rdp->grpmask & rcu_rnp_online_cpus(rdp->mynode));
  4255. }
  4256. bool rcu_cpu_online(int cpu)
  4257. {
  4258. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  4259. return rcu_rdp_cpu_online(rdp);
  4260. }
  4261. #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
  4262. /*
  4263. * Is the current CPU online as far as RCU is concerned?
  4264. *
  4265. * Disable preemption to avoid false positives that could otherwise
  4266. * happen due to the current CPU number being sampled, this task being
  4267. * preempted, its old CPU being taken offline, resuming on some other CPU,
  4268. * then determining that its old CPU is now offline.
  4269. *
  4270. * Disable checking if in an NMI handler because we cannot safely
  4271. * report errors from NMI handlers anyway. In addition, it is OK to use
  4272. * RCU on an offline processor during initial boot, hence the check for
  4273. * rcu_scheduler_fully_active.
  4274. */
  4275. bool rcu_lockdep_current_cpu_online(void)
  4276. {
  4277. struct rcu_data *rdp;
  4278. bool ret = false;
  4279. if (in_nmi() || !rcu_scheduler_fully_active)
  4280. return true;
  4281. preempt_disable_notrace();
  4282. rdp = this_cpu_ptr(&rcu_data);
  4283. /*
  4284. * Strictly, we care here about the case where the current CPU is
  4285. * in rcutree_report_cpu_starting() and thus has an excuse for rdp->grpmask
  4286. * not being up to date. So arch_spin_is_locked() might have a
  4287. * false positive if it's held by some *other* CPU, but that's
  4288. * OK because that just means a false *negative* on the warning.
  4289. */
  4290. if (rcu_rdp_cpu_online(rdp) || arch_spin_is_locked(&rcu_state.ofl_lock))
  4291. ret = true;
  4292. preempt_enable_notrace();
  4293. return ret;
  4294. }
  4295. EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
  4296. #endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
  4297. // Has rcu_init() been invoked? This is used (for example) to determine
  4298. // whether spinlocks may be acquired safely.
  4299. static bool rcu_init_invoked(void)
  4300. {
  4301. return !!READ_ONCE(rcu_state.n_online_cpus);
  4302. }
  4303. /*
  4304. * All CPUs for the specified rcu_node structure have gone offline,
  4305. * and all tasks that were preempted within an RCU read-side critical
  4306. * section while running on one of those CPUs have since exited their RCU
  4307. * read-side critical section. Some other CPU is reporting this fact with
  4308. * the specified rcu_node structure's ->lock held and interrupts disabled.
  4309. * This function therefore goes up the tree of rcu_node structures,
  4310. * clearing the corresponding bits in the ->qsmaskinit fields. Note that
  4311. * the leaf rcu_node structure's ->qsmaskinit field has already been
  4312. * updated.
  4313. *
  4314. * This function does check that the specified rcu_node structure has
  4315. * all CPUs offline and no blocked tasks, so it is OK to invoke it
  4316. * prematurely. That said, invoking it after the fact will cost you
  4317. * a needless lock acquisition. So once it has done its work, don't
  4318. * invoke it again.
  4319. */
  4320. static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf)
  4321. {
  4322. long mask;
  4323. struct rcu_node *rnp = rnp_leaf;
  4324. raw_lockdep_assert_held_rcu_node(rnp_leaf);
  4325. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
  4326. WARN_ON_ONCE(rnp_leaf->qsmaskinit) ||
  4327. WARN_ON_ONCE(rcu_preempt_has_tasks(rnp_leaf)))
  4328. return;
  4329. for (;;) {
  4330. mask = rnp->grpmask;
  4331. rnp = rnp->parent;
  4332. if (!rnp)
  4333. break;
  4334. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  4335. rnp->qsmaskinit &= ~mask;
  4336. /* Between grace periods, so better already be zero! */
  4337. WARN_ON_ONCE(rnp->qsmask);
  4338. if (rnp->qsmaskinit) {
  4339. raw_spin_unlock_rcu_node(rnp);
  4340. /* irqs remain disabled. */
  4341. return;
  4342. }
  4343. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  4344. }
  4345. }
  4346. /*
  4347. * Propagate ->qsinitmask bits up the rcu_node tree to account for the
  4348. * first CPU in a given leaf rcu_node structure coming online. The caller
  4349. * must hold the corresponding leaf rcu_node ->lock with interrupts
  4350. * disabled.
  4351. */
  4352. static void rcu_init_new_rnp(struct rcu_node *rnp_leaf)
  4353. {
  4354. long mask;
  4355. long oldmask;
  4356. struct rcu_node *rnp = rnp_leaf;
  4357. raw_lockdep_assert_held_rcu_node(rnp_leaf);
  4358. WARN_ON_ONCE(rnp->wait_blkd_tasks);
  4359. for (;;) {
  4360. mask = rnp->grpmask;
  4361. rnp = rnp->parent;
  4362. if (rnp == NULL)
  4363. return;
  4364. raw_spin_lock_rcu_node(rnp); /* Interrupts already disabled. */
  4365. oldmask = rnp->qsmaskinit;
  4366. rnp->qsmaskinit |= mask;
  4367. raw_spin_unlock_rcu_node(rnp); /* Interrupts remain disabled. */
  4368. if (oldmask)
  4369. return;
  4370. }
  4371. }
  4372. /*
  4373. * Do boot-time initialization of a CPU's per-CPU RCU data.
  4374. */
  4375. static void __init
  4376. rcu_boot_init_percpu_data(int cpu)
  4377. {
  4378. struct context_tracking *ct = this_cpu_ptr(&context_tracking);
  4379. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  4380. /* Set up local state, ensuring consistent view of global state. */
  4381. rdp->grpmask = leaf_node_cpu_bit(rdp->mynode, cpu);
  4382. INIT_WORK(&rdp->strict_work, strict_work_handler);
  4383. WARN_ON_ONCE(ct->nesting != 1);
  4384. WARN_ON_ONCE(rcu_watching_snap_in_eqs(ct_rcu_watching_cpu(cpu)));
  4385. rdp->barrier_seq_snap = rcu_state.barrier_sequence;
  4386. rdp->rcu_ofl_gp_seq = rcu_state.gp_seq;
  4387. rdp->rcu_ofl_gp_state = RCU_GP_CLEANED;
  4388. rdp->rcu_onl_gp_seq = rcu_state.gp_seq;
  4389. rdp->rcu_onl_gp_state = RCU_GP_CLEANED;
  4390. rdp->last_sched_clock = jiffies;
  4391. rdp->cpu = cpu;
  4392. rcu_boot_init_nocb_percpu_data(rdp);
  4393. }
  4394. struct kthread_worker *rcu_exp_gp_kworker;
  4395. static void rcu_spawn_exp_par_gp_kworker(struct rcu_node *rnp)
  4396. {
  4397. struct kthread_worker *kworker;
  4398. const char *name = "rcu_exp_par_gp_kthread_worker/%d";
  4399. struct sched_param param = { .sched_priority = kthread_prio };
  4400. int rnp_index = rnp - rcu_get_root();
  4401. if (rnp->exp_kworker)
  4402. return;
  4403. kworker = kthread_create_worker(0, name, rnp_index);
  4404. if (IS_ERR_OR_NULL(kworker)) {
  4405. pr_err("Failed to create par gp kworker on %d/%d\n",
  4406. rnp->grplo, rnp->grphi);
  4407. return;
  4408. }
  4409. WRITE_ONCE(rnp->exp_kworker, kworker);
  4410. if (IS_ENABLED(CONFIG_RCU_EXP_KTHREAD))
  4411. sched_setscheduler_nocheck(kworker->task, SCHED_FIFO, &param);
  4412. }
  4413. static struct task_struct *rcu_exp_par_gp_task(struct rcu_node *rnp)
  4414. {
  4415. struct kthread_worker *kworker = READ_ONCE(rnp->exp_kworker);
  4416. if (!kworker)
  4417. return NULL;
  4418. return kworker->task;
  4419. }
  4420. static void __init rcu_start_exp_gp_kworker(void)
  4421. {
  4422. const char *name = "rcu_exp_gp_kthread_worker";
  4423. struct sched_param param = { .sched_priority = kthread_prio };
  4424. rcu_exp_gp_kworker = kthread_create_worker(0, name);
  4425. if (IS_ERR_OR_NULL(rcu_exp_gp_kworker)) {
  4426. pr_err("Failed to create %s!\n", name);
  4427. rcu_exp_gp_kworker = NULL;
  4428. return;
  4429. }
  4430. if (IS_ENABLED(CONFIG_RCU_EXP_KTHREAD))
  4431. sched_setscheduler_nocheck(rcu_exp_gp_kworker->task, SCHED_FIFO, &param);
  4432. }
  4433. static void rcu_spawn_rnp_kthreads(struct rcu_node *rnp)
  4434. {
  4435. if (rcu_scheduler_fully_active) {
  4436. mutex_lock(&rnp->kthread_mutex);
  4437. rcu_spawn_one_boost_kthread(rnp);
  4438. rcu_spawn_exp_par_gp_kworker(rnp);
  4439. mutex_unlock(&rnp->kthread_mutex);
  4440. }
  4441. }
  4442. /*
  4443. * Invoked early in the CPU-online process, when pretty much all services
  4444. * are available. The incoming CPU is not present.
  4445. *
  4446. * Initializes a CPU's per-CPU RCU data. Note that only one online or
  4447. * offline event can be happening at a given time. Note also that we can
  4448. * accept some slop in the rsp->gp_seq access due to the fact that this
  4449. * CPU cannot possibly have any non-offloaded RCU callbacks in flight yet.
  4450. * And any offloaded callbacks are being numbered elsewhere.
  4451. */
  4452. int rcutree_prepare_cpu(unsigned int cpu)
  4453. {
  4454. unsigned long flags;
  4455. struct context_tracking *ct = per_cpu_ptr(&context_tracking, cpu);
  4456. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  4457. struct rcu_node *rnp = rcu_get_root();
  4458. /* Set up local state, ensuring consistent view of global state. */
  4459. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  4460. rdp->qlen_last_fqs_check = 0;
  4461. rdp->n_force_qs_snap = READ_ONCE(rcu_state.n_force_qs);
  4462. rdp->blimit = blimit;
  4463. ct->nesting = 1; /* CPU not up, no tearing. */
  4464. raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
  4465. /*
  4466. * Only non-NOCB CPUs that didn't have early-boot callbacks need to be
  4467. * (re-)initialized.
  4468. */
  4469. if (!rcu_segcblist_is_enabled(&rdp->cblist))
  4470. rcu_segcblist_init(&rdp->cblist); /* Re-enable callbacks. */
  4471. /*
  4472. * Add CPU to leaf rcu_node pending-online bitmask. Any needed
  4473. * propagation up the rcu_node tree will happen at the beginning
  4474. * of the next grace period.
  4475. */
  4476. rnp = rdp->mynode;
  4477. raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
  4478. rdp->gp_seq = READ_ONCE(rnp->gp_seq);
  4479. rdp->gp_seq_needed = rdp->gp_seq;
  4480. rdp->cpu_no_qs.b.norm = true;
  4481. rdp->core_needs_qs = false;
  4482. rdp->rcu_iw_pending = false;
  4483. rdp->rcu_iw = IRQ_WORK_INIT_HARD(rcu_iw_handler);
  4484. rdp->rcu_iw_gp_seq = rdp->gp_seq - 1;
  4485. trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuonl"));
  4486. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  4487. rcu_preempt_deferred_qs_init(rdp);
  4488. rcu_spawn_rnp_kthreads(rnp);
  4489. rcu_spawn_cpu_nocb_kthread(cpu);
  4490. ASSERT_EXCLUSIVE_WRITER(rcu_state.n_online_cpus);
  4491. WRITE_ONCE(rcu_state.n_online_cpus, rcu_state.n_online_cpus + 1);
  4492. return 0;
  4493. }
  4494. /*
  4495. * Update kthreads affinity during CPU-hotplug changes.
  4496. *
  4497. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  4498. * served by the rcu_node in question. The CPU hotplug lock is still
  4499. * held, so the value of rnp->qsmaskinit will be stable.
  4500. *
  4501. * We don't include outgoingcpu in the affinity set, use -1 if there is
  4502. * no outgoing CPU. If there are no CPUs left in the affinity set,
  4503. * this function allows the kthread to execute on any CPU.
  4504. *
  4505. * Any future concurrent calls are serialized via ->kthread_mutex.
  4506. */
  4507. static void rcutree_affinity_setting(unsigned int cpu, int outgoingcpu)
  4508. {
  4509. cpumask_var_t cm;
  4510. unsigned long mask;
  4511. struct rcu_data *rdp;
  4512. struct rcu_node *rnp;
  4513. struct task_struct *task_boost, *task_exp;
  4514. rdp = per_cpu_ptr(&rcu_data, cpu);
  4515. rnp = rdp->mynode;
  4516. task_boost = rcu_boost_task(rnp);
  4517. task_exp = rcu_exp_par_gp_task(rnp);
  4518. /*
  4519. * If CPU is the boot one, those tasks are created later from early
  4520. * initcall since kthreadd must be created first.
  4521. */
  4522. if (!task_boost && !task_exp)
  4523. return;
  4524. if (!zalloc_cpumask_var(&cm, GFP_KERNEL))
  4525. return;
  4526. mutex_lock(&rnp->kthread_mutex);
  4527. mask = rcu_rnp_online_cpus(rnp);
  4528. for_each_leaf_node_possible_cpu(rnp, cpu)
  4529. if ((mask & leaf_node_cpu_bit(rnp, cpu)) &&
  4530. cpu != outgoingcpu)
  4531. cpumask_set_cpu(cpu, cm);
  4532. cpumask_and(cm, cm, housekeeping_cpumask(HK_TYPE_RCU));
  4533. if (cpumask_empty(cm)) {
  4534. cpumask_copy(cm, housekeeping_cpumask(HK_TYPE_RCU));
  4535. if (outgoingcpu >= 0)
  4536. cpumask_clear_cpu(outgoingcpu, cm);
  4537. }
  4538. if (task_exp)
  4539. set_cpus_allowed_ptr(task_exp, cm);
  4540. if (task_boost)
  4541. set_cpus_allowed_ptr(task_boost, cm);
  4542. mutex_unlock(&rnp->kthread_mutex);
  4543. free_cpumask_var(cm);
  4544. }
  4545. /*
  4546. * Has the specified (known valid) CPU ever been fully online?
  4547. */
  4548. bool rcu_cpu_beenfullyonline(int cpu)
  4549. {
  4550. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  4551. return smp_load_acquire(&rdp->beenonline);
  4552. }
  4553. /*
  4554. * Near the end of the CPU-online process. Pretty much all services
  4555. * enabled, and the CPU is now very much alive.
  4556. */
  4557. int rcutree_online_cpu(unsigned int cpu)
  4558. {
  4559. unsigned long flags;
  4560. struct rcu_data *rdp;
  4561. struct rcu_node *rnp;
  4562. rdp = per_cpu_ptr(&rcu_data, cpu);
  4563. rnp = rdp->mynode;
  4564. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  4565. rnp->ffmask |= rdp->grpmask;
  4566. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  4567. if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
  4568. return 0; /* Too early in boot for scheduler work. */
  4569. sync_sched_exp_online_cleanup(cpu);
  4570. rcutree_affinity_setting(cpu, -1);
  4571. // Stop-machine done, so allow nohz_full to disable tick.
  4572. tick_dep_clear(TICK_DEP_BIT_RCU);
  4573. return 0;
  4574. }
  4575. /*
  4576. * Mark the specified CPU as being online so that subsequent grace periods
  4577. * (both expedited and normal) will wait on it. Note that this means that
  4578. * incoming CPUs are not allowed to use RCU read-side critical sections
  4579. * until this function is called. Failing to observe this restriction
  4580. * will result in lockdep splats.
  4581. *
  4582. * Note that this function is special in that it is invoked directly
  4583. * from the incoming CPU rather than from the cpuhp_step mechanism.
  4584. * This is because this function must be invoked at a precise location.
  4585. * This incoming CPU must not have enabled interrupts yet.
  4586. *
  4587. * This mirrors the effects of rcutree_report_cpu_dead().
  4588. */
  4589. void rcutree_report_cpu_starting(unsigned int cpu)
  4590. {
  4591. unsigned long mask;
  4592. struct rcu_data *rdp;
  4593. struct rcu_node *rnp;
  4594. bool newcpu;
  4595. lockdep_assert_irqs_disabled();
  4596. rdp = per_cpu_ptr(&rcu_data, cpu);
  4597. if (rdp->cpu_started)
  4598. return;
  4599. rdp->cpu_started = true;
  4600. rnp = rdp->mynode;
  4601. mask = rdp->grpmask;
  4602. arch_spin_lock(&rcu_state.ofl_lock);
  4603. rcu_watching_online();
  4604. raw_spin_lock(&rcu_state.barrier_lock);
  4605. raw_spin_lock_rcu_node(rnp);
  4606. WRITE_ONCE(rnp->qsmaskinitnext, rnp->qsmaskinitnext | mask);
  4607. raw_spin_unlock(&rcu_state.barrier_lock);
  4608. newcpu = !(rnp->expmaskinitnext & mask);
  4609. rnp->expmaskinitnext |= mask;
  4610. /* Allow lockless access for expedited grace periods. */
  4611. smp_store_release(&rcu_state.ncpus, rcu_state.ncpus + newcpu); /* ^^^ */
  4612. ASSERT_EXCLUSIVE_WRITER(rcu_state.ncpus);
  4613. rcu_gpnum_ovf(rnp, rdp); /* Offline-induced counter wrap? */
  4614. rdp->rcu_onl_gp_seq = READ_ONCE(rcu_state.gp_seq);
  4615. rdp->rcu_onl_gp_state = READ_ONCE(rcu_state.gp_state);
  4616. /* An incoming CPU should never be blocking a grace period. */
  4617. if (WARN_ON_ONCE(rnp->qsmask & mask)) { /* RCU waiting on incoming CPU? */
  4618. /* rcu_report_qs_rnp() *really* wants some flags to restore */
  4619. unsigned long flags;
  4620. local_irq_save(flags);
  4621. rcu_disable_urgency_upon_qs(rdp);
  4622. /* Report QS -after- changing ->qsmaskinitnext! */
  4623. rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
  4624. } else {
  4625. raw_spin_unlock_rcu_node(rnp);
  4626. }
  4627. arch_spin_unlock(&rcu_state.ofl_lock);
  4628. smp_store_release(&rdp->beenonline, true);
  4629. smp_mb(); /* Ensure RCU read-side usage follows above initialization. */
  4630. }
  4631. /*
  4632. * The outgoing function has no further need of RCU, so remove it from
  4633. * the rcu_node tree's ->qsmaskinitnext bit masks.
  4634. *
  4635. * Note that this function is special in that it is invoked directly
  4636. * from the outgoing CPU rather than from the cpuhp_step mechanism.
  4637. * This is because this function must be invoked at a precise location.
  4638. *
  4639. * This mirrors the effect of rcutree_report_cpu_starting().
  4640. */
  4641. void rcutree_report_cpu_dead(void)
  4642. {
  4643. unsigned long flags;
  4644. unsigned long mask;
  4645. struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
  4646. struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
  4647. /*
  4648. * IRQS must be disabled from now on and until the CPU dies, or an interrupt
  4649. * may introduce a new READ-side while it is actually off the QS masks.
  4650. */
  4651. lockdep_assert_irqs_disabled();
  4652. // Do any dangling deferred wakeups.
  4653. do_nocb_deferred_wakeup(rdp);
  4654. rcu_preempt_deferred_qs(current);
  4655. /* Remove outgoing CPU from mask in the leaf rcu_node structure. */
  4656. mask = rdp->grpmask;
  4657. arch_spin_lock(&rcu_state.ofl_lock);
  4658. raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */
  4659. rdp->rcu_ofl_gp_seq = READ_ONCE(rcu_state.gp_seq);
  4660. rdp->rcu_ofl_gp_state = READ_ONCE(rcu_state.gp_state);
  4661. if (rnp->qsmask & mask) { /* RCU waiting on outgoing CPU? */
  4662. /* Report quiescent state -before- changing ->qsmaskinitnext! */
  4663. rcu_disable_urgency_upon_qs(rdp);
  4664. rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
  4665. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  4666. }
  4667. WRITE_ONCE(rnp->qsmaskinitnext, rnp->qsmaskinitnext & ~mask);
  4668. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  4669. arch_spin_unlock(&rcu_state.ofl_lock);
  4670. rdp->cpu_started = false;
  4671. }
  4672. #ifdef CONFIG_HOTPLUG_CPU
  4673. /*
  4674. * The outgoing CPU has just passed through the dying-idle state, and we
  4675. * are being invoked from the CPU that was IPIed to continue the offline
  4676. * operation. Migrate the outgoing CPU's callbacks to the current CPU.
  4677. */
  4678. void rcutree_migrate_callbacks(int cpu)
  4679. {
  4680. unsigned long flags;
  4681. struct rcu_data *my_rdp;
  4682. struct rcu_node *my_rnp;
  4683. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  4684. bool needwake;
  4685. if (rcu_rdp_is_offloaded(rdp))
  4686. return;
  4687. raw_spin_lock_irqsave(&rcu_state.barrier_lock, flags);
  4688. if (rcu_segcblist_empty(&rdp->cblist)) {
  4689. raw_spin_unlock_irqrestore(&rcu_state.barrier_lock, flags);
  4690. return; /* No callbacks to migrate. */
  4691. }
  4692. WARN_ON_ONCE(rcu_rdp_cpu_online(rdp));
  4693. rcu_barrier_entrain(rdp);
  4694. my_rdp = this_cpu_ptr(&rcu_data);
  4695. my_rnp = my_rdp->mynode;
  4696. rcu_nocb_lock(my_rdp); /* irqs already disabled. */
  4697. WARN_ON_ONCE(!rcu_nocb_flush_bypass(my_rdp, NULL, jiffies, false));
  4698. raw_spin_lock_rcu_node(my_rnp); /* irqs already disabled. */
  4699. /* Leverage recent GPs and set GP for new callbacks. */
  4700. needwake = rcu_advance_cbs(my_rnp, rdp) ||
  4701. rcu_advance_cbs(my_rnp, my_rdp);
  4702. rcu_segcblist_merge(&my_rdp->cblist, &rdp->cblist);
  4703. raw_spin_unlock(&rcu_state.barrier_lock); /* irqs remain disabled. */
  4704. needwake = needwake || rcu_advance_cbs(my_rnp, my_rdp);
  4705. rcu_segcblist_disable(&rdp->cblist);
  4706. WARN_ON_ONCE(rcu_segcblist_empty(&my_rdp->cblist) != !rcu_segcblist_n_cbs(&my_rdp->cblist));
  4707. check_cb_ovld_locked(my_rdp, my_rnp);
  4708. if (rcu_rdp_is_offloaded(my_rdp)) {
  4709. raw_spin_unlock_rcu_node(my_rnp); /* irqs remain disabled. */
  4710. __call_rcu_nocb_wake(my_rdp, true, flags);
  4711. } else {
  4712. rcu_nocb_unlock(my_rdp); /* irqs remain disabled. */
  4713. raw_spin_unlock_rcu_node(my_rnp); /* irqs remain disabled. */
  4714. }
  4715. local_irq_restore(flags);
  4716. if (needwake)
  4717. rcu_gp_kthread_wake();
  4718. lockdep_assert_irqs_enabled();
  4719. WARN_ONCE(rcu_segcblist_n_cbs(&rdp->cblist) != 0 ||
  4720. !rcu_segcblist_empty(&rdp->cblist),
  4721. "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, 1stCB=%p\n",
  4722. cpu, rcu_segcblist_n_cbs(&rdp->cblist),
  4723. rcu_segcblist_first_cb(&rdp->cblist));
  4724. }
  4725. /*
  4726. * The CPU has been completely removed, and some other CPU is reporting
  4727. * this fact from process context. Do the remainder of the cleanup.
  4728. * There can only be one CPU hotplug operation at a time, so no need for
  4729. * explicit locking.
  4730. */
  4731. int rcutree_dead_cpu(unsigned int cpu)
  4732. {
  4733. ASSERT_EXCLUSIVE_WRITER(rcu_state.n_online_cpus);
  4734. WRITE_ONCE(rcu_state.n_online_cpus, rcu_state.n_online_cpus - 1);
  4735. // Stop-machine done, so allow nohz_full to disable tick.
  4736. tick_dep_clear(TICK_DEP_BIT_RCU);
  4737. return 0;
  4738. }
  4739. /*
  4740. * Near the end of the offline process. Trace the fact that this CPU
  4741. * is going offline.
  4742. */
  4743. int rcutree_dying_cpu(unsigned int cpu)
  4744. {
  4745. bool blkd;
  4746. struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
  4747. struct rcu_node *rnp = rdp->mynode;
  4748. blkd = !!(READ_ONCE(rnp->qsmask) & rdp->grpmask);
  4749. trace_rcu_grace_period(rcu_state.name, READ_ONCE(rnp->gp_seq),
  4750. blkd ? TPS("cpuofl-bgp") : TPS("cpuofl"));
  4751. return 0;
  4752. }
  4753. /*
  4754. * Near the beginning of the process. The CPU is still very much alive
  4755. * with pretty much all services enabled.
  4756. */
  4757. int rcutree_offline_cpu(unsigned int cpu)
  4758. {
  4759. unsigned long flags;
  4760. struct rcu_data *rdp;
  4761. struct rcu_node *rnp;
  4762. rdp = per_cpu_ptr(&rcu_data, cpu);
  4763. rnp = rdp->mynode;
  4764. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  4765. rnp->ffmask &= ~rdp->grpmask;
  4766. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  4767. rcutree_affinity_setting(cpu, cpu);
  4768. // nohz_full CPUs need the tick for stop-machine to work quickly
  4769. tick_dep_set(TICK_DEP_BIT_RCU);
  4770. return 0;
  4771. }
  4772. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  4773. /*
  4774. * On non-huge systems, use expedited RCU grace periods to make suspend
  4775. * and hibernation run faster.
  4776. */
  4777. static int rcu_pm_notify(struct notifier_block *self,
  4778. unsigned long action, void *hcpu)
  4779. {
  4780. switch (action) {
  4781. case PM_HIBERNATION_PREPARE:
  4782. case PM_SUSPEND_PREPARE:
  4783. rcu_async_hurry();
  4784. rcu_expedite_gp();
  4785. break;
  4786. case PM_POST_HIBERNATION:
  4787. case PM_POST_SUSPEND:
  4788. rcu_unexpedite_gp();
  4789. rcu_async_relax();
  4790. break;
  4791. default:
  4792. break;
  4793. }
  4794. return NOTIFY_OK;
  4795. }
  4796. /*
  4797. * Spawn the kthreads that handle RCU's grace periods.
  4798. */
  4799. static int __init rcu_spawn_gp_kthread(void)
  4800. {
  4801. unsigned long flags;
  4802. struct rcu_node *rnp;
  4803. struct sched_param sp;
  4804. struct task_struct *t;
  4805. struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
  4806. rcu_scheduler_fully_active = 1;
  4807. t = kthread_create(rcu_gp_kthread, NULL, "%s", rcu_state.name);
  4808. if (WARN_ONCE(IS_ERR(t), "%s: Could not start grace-period kthread, OOM is now expected behavior\n", __func__))
  4809. return 0;
  4810. if (kthread_prio) {
  4811. sp.sched_priority = kthread_prio;
  4812. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  4813. }
  4814. rnp = rcu_get_root();
  4815. raw_spin_lock_irqsave_rcu_node(rnp, flags);
  4816. WRITE_ONCE(rcu_state.gp_activity, jiffies);
  4817. WRITE_ONCE(rcu_state.gp_req_activity, jiffies);
  4818. // Reset .gp_activity and .gp_req_activity before setting .gp_kthread.
  4819. smp_store_release(&rcu_state.gp_kthread, t); /* ^^^ */
  4820. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  4821. wake_up_process(t);
  4822. /* This is a pre-SMP initcall, we expect a single CPU */
  4823. WARN_ON(num_online_cpus() > 1);
  4824. /*
  4825. * Those kthreads couldn't be created on rcu_init() -> rcutree_prepare_cpu()
  4826. * due to rcu_scheduler_fully_active.
  4827. */
  4828. rcu_spawn_cpu_nocb_kthread(smp_processor_id());
  4829. rcu_spawn_rnp_kthreads(rdp->mynode);
  4830. rcu_spawn_core_kthreads();
  4831. /* Create kthread worker for expedited GPs */
  4832. rcu_start_exp_gp_kworker();
  4833. return 0;
  4834. }
  4835. early_initcall(rcu_spawn_gp_kthread);
  4836. /*
  4837. * This function is invoked towards the end of the scheduler's
  4838. * initialization process. Before this is called, the idle task might
  4839. * contain synchronous grace-period primitives (during which time, this idle
  4840. * task is booting the system, and such primitives are no-ops). After this
  4841. * function is called, any synchronous grace-period primitives are run as
  4842. * expedited, with the requesting task driving the grace period forward.
  4843. * A later core_initcall() rcu_set_runtime_mode() will switch to full
  4844. * runtime RCU functionality.
  4845. */
  4846. void rcu_scheduler_starting(void)
  4847. {
  4848. unsigned long flags;
  4849. struct rcu_node *rnp;
  4850. WARN_ON(num_online_cpus() != 1);
  4851. WARN_ON(nr_context_switches() > 0);
  4852. rcu_test_sync_prims();
  4853. // Fix up the ->gp_seq counters.
  4854. local_irq_save(flags);
  4855. rcu_for_each_node_breadth_first(rnp)
  4856. rnp->gp_seq_needed = rnp->gp_seq = rcu_state.gp_seq;
  4857. local_irq_restore(flags);
  4858. // Switch out of early boot mode.
  4859. rcu_scheduler_active = RCU_SCHEDULER_INIT;
  4860. rcu_test_sync_prims();
  4861. }
  4862. /*
  4863. * Helper function for rcu_init() that initializes the rcu_state structure.
  4864. */
  4865. static void __init rcu_init_one(void)
  4866. {
  4867. static const char * const buf[] = RCU_NODE_NAME_INIT;
  4868. static const char * const fqs[] = RCU_FQS_NAME_INIT;
  4869. static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
  4870. static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
  4871. int levelspread[RCU_NUM_LVLS]; /* kids/node in each level. */
  4872. int cpustride = 1;
  4873. int i;
  4874. int j;
  4875. struct rcu_node *rnp;
  4876. BUILD_BUG_ON(RCU_NUM_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
  4877. /* Silence gcc 4.8 false positive about array index out of range. */
  4878. if (rcu_num_lvls <= 0 || rcu_num_lvls > RCU_NUM_LVLS)
  4879. panic("rcu_init_one: rcu_num_lvls out of range");
  4880. /* Initialize the level-tracking arrays. */
  4881. for (i = 1; i < rcu_num_lvls; i++)
  4882. rcu_state.level[i] =
  4883. rcu_state.level[i - 1] + num_rcu_lvl[i - 1];
  4884. rcu_init_levelspread(levelspread, num_rcu_lvl);
  4885. /* Initialize the elements themselves, starting from the leaves. */
  4886. for (i = rcu_num_lvls - 1; i >= 0; i--) {
  4887. cpustride *= levelspread[i];
  4888. rnp = rcu_state.level[i];
  4889. for (j = 0; j < num_rcu_lvl[i]; j++, rnp++) {
  4890. raw_spin_lock_init(&ACCESS_PRIVATE(rnp, lock));
  4891. lockdep_set_class_and_name(&ACCESS_PRIVATE(rnp, lock),
  4892. &rcu_node_class[i], buf[i]);
  4893. raw_spin_lock_init(&rnp->fqslock);
  4894. lockdep_set_class_and_name(&rnp->fqslock,
  4895. &rcu_fqs_class[i], fqs[i]);
  4896. rnp->gp_seq = rcu_state.gp_seq;
  4897. rnp->gp_seq_needed = rcu_state.gp_seq;
  4898. rnp->completedqs = rcu_state.gp_seq;
  4899. rnp->qsmask = 0;
  4900. rnp->qsmaskinit = 0;
  4901. rnp->grplo = j * cpustride;
  4902. rnp->grphi = (j + 1) * cpustride - 1;
  4903. if (rnp->grphi >= nr_cpu_ids)
  4904. rnp->grphi = nr_cpu_ids - 1;
  4905. if (i == 0) {
  4906. rnp->grpnum = 0;
  4907. rnp->grpmask = 0;
  4908. rnp->parent = NULL;
  4909. } else {
  4910. rnp->grpnum = j % levelspread[i - 1];
  4911. rnp->grpmask = BIT(rnp->grpnum);
  4912. rnp->parent = rcu_state.level[i - 1] +
  4913. j / levelspread[i - 1];
  4914. }
  4915. rnp->level = i;
  4916. INIT_LIST_HEAD(&rnp->blkd_tasks);
  4917. rcu_init_one_nocb(rnp);
  4918. init_waitqueue_head(&rnp->exp_wq[0]);
  4919. init_waitqueue_head(&rnp->exp_wq[1]);
  4920. init_waitqueue_head(&rnp->exp_wq[2]);
  4921. init_waitqueue_head(&rnp->exp_wq[3]);
  4922. spin_lock_init(&rnp->exp_lock);
  4923. mutex_init(&rnp->kthread_mutex);
  4924. raw_spin_lock_init(&rnp->exp_poll_lock);
  4925. rnp->exp_seq_poll_rq = RCU_GET_STATE_COMPLETED;
  4926. INIT_WORK(&rnp->exp_poll_wq, sync_rcu_do_polled_gp);
  4927. }
  4928. }
  4929. init_swait_queue_head(&rcu_state.gp_wq);
  4930. init_swait_queue_head(&rcu_state.expedited_wq);
  4931. rnp = rcu_first_leaf_node();
  4932. for_each_possible_cpu(i) {
  4933. while (i > rnp->grphi)
  4934. rnp++;
  4935. per_cpu_ptr(&rcu_data, i)->mynode = rnp;
  4936. per_cpu_ptr(&rcu_data, i)->barrier_head.next =
  4937. &per_cpu_ptr(&rcu_data, i)->barrier_head;
  4938. rcu_boot_init_percpu_data(i);
  4939. }
  4940. }
  4941. /*
  4942. * Force priority from the kernel command-line into range.
  4943. */
  4944. static void __init sanitize_kthread_prio(void)
  4945. {
  4946. int kthread_prio_in = kthread_prio;
  4947. if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 2
  4948. && IS_BUILTIN(CONFIG_RCU_TORTURE_TEST))
  4949. kthread_prio = 2;
  4950. else if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 1)
  4951. kthread_prio = 1;
  4952. else if (kthread_prio < 0)
  4953. kthread_prio = 0;
  4954. else if (kthread_prio > 99)
  4955. kthread_prio = 99;
  4956. if (kthread_prio != kthread_prio_in)
  4957. pr_alert("%s: Limited prio to %d from %d\n",
  4958. __func__, kthread_prio, kthread_prio_in);
  4959. }
  4960. /*
  4961. * Compute the rcu_node tree geometry from kernel parameters. This cannot
  4962. * replace the definitions in tree.h because those are needed to size
  4963. * the ->node array in the rcu_state structure.
  4964. */
  4965. void rcu_init_geometry(void)
  4966. {
  4967. ulong d;
  4968. int i;
  4969. static unsigned long old_nr_cpu_ids;
  4970. int rcu_capacity[RCU_NUM_LVLS];
  4971. static bool initialized;
  4972. if (initialized) {
  4973. /*
  4974. * Warn if setup_nr_cpu_ids() had not yet been invoked,
  4975. * unless nr_cpus_ids == NR_CPUS, in which case who cares?
  4976. */
  4977. WARN_ON_ONCE(old_nr_cpu_ids != nr_cpu_ids);
  4978. return;
  4979. }
  4980. old_nr_cpu_ids = nr_cpu_ids;
  4981. initialized = true;
  4982. /*
  4983. * Initialize any unspecified boot parameters.
  4984. * The default values of jiffies_till_first_fqs and
  4985. * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS
  4986. * value, which is a function of HZ, then adding one for each
  4987. * RCU_JIFFIES_FQS_DIV CPUs that might be on the system.
  4988. */
  4989. d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
  4990. if (jiffies_till_first_fqs == ULONG_MAX)
  4991. jiffies_till_first_fqs = d;
  4992. if (jiffies_till_next_fqs == ULONG_MAX)
  4993. jiffies_till_next_fqs = d;
  4994. adjust_jiffies_till_sched_qs();
  4995. /* If the compile-time values are accurate, just leave. */
  4996. if (rcu_fanout_leaf == RCU_FANOUT_LEAF &&
  4997. nr_cpu_ids == NR_CPUS)
  4998. return;
  4999. pr_info("Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%u\n",
  5000. rcu_fanout_leaf, nr_cpu_ids);
  5001. /*
  5002. * The boot-time rcu_fanout_leaf parameter must be at least two
  5003. * and cannot exceed the number of bits in the rcu_node masks.
  5004. * Complain and fall back to the compile-time values if this
  5005. * limit is exceeded.
  5006. */
  5007. if (rcu_fanout_leaf < 2 ||
  5008. rcu_fanout_leaf > sizeof(unsigned long) * 8) {
  5009. rcu_fanout_leaf = RCU_FANOUT_LEAF;
  5010. WARN_ON(1);
  5011. return;
  5012. }
  5013. /*
  5014. * Compute number of nodes that can be handled an rcu_node tree
  5015. * with the given number of levels.
  5016. */
  5017. rcu_capacity[0] = rcu_fanout_leaf;
  5018. for (i = 1; i < RCU_NUM_LVLS; i++)
  5019. rcu_capacity[i] = rcu_capacity[i - 1] * RCU_FANOUT;
  5020. /*
  5021. * The tree must be able to accommodate the configured number of CPUs.
  5022. * If this limit is exceeded, fall back to the compile-time values.
  5023. */
  5024. if (nr_cpu_ids > rcu_capacity[RCU_NUM_LVLS - 1]) {
  5025. rcu_fanout_leaf = RCU_FANOUT_LEAF;
  5026. WARN_ON(1);
  5027. return;
  5028. }
  5029. /* Calculate the number of levels in the tree. */
  5030. for (i = 0; nr_cpu_ids > rcu_capacity[i]; i++) {
  5031. }
  5032. rcu_num_lvls = i + 1;
  5033. /* Calculate the number of rcu_nodes at each level of the tree. */
  5034. for (i = 0; i < rcu_num_lvls; i++) {
  5035. int cap = rcu_capacity[(rcu_num_lvls - 1) - i];
  5036. num_rcu_lvl[i] = DIV_ROUND_UP(nr_cpu_ids, cap);
  5037. }
  5038. /* Calculate the total number of rcu_node structures. */
  5039. rcu_num_nodes = 0;
  5040. for (i = 0; i < rcu_num_lvls; i++)
  5041. rcu_num_nodes += num_rcu_lvl[i];
  5042. }
  5043. /*
  5044. * Dump out the structure of the rcu_node combining tree associated
  5045. * with the rcu_state structure.
  5046. */
  5047. static void __init rcu_dump_rcu_node_tree(void)
  5048. {
  5049. int level = 0;
  5050. struct rcu_node *rnp;
  5051. pr_info("rcu_node tree layout dump\n");
  5052. pr_info(" ");
  5053. rcu_for_each_node_breadth_first(rnp) {
  5054. if (rnp->level != level) {
  5055. pr_cont("\n");
  5056. pr_info(" ");
  5057. level = rnp->level;
  5058. }
  5059. pr_cont("%d:%d ^%d ", rnp->grplo, rnp->grphi, rnp->grpnum);
  5060. }
  5061. pr_cont("\n");
  5062. }
  5063. struct workqueue_struct *rcu_gp_wq;
  5064. static void __init kfree_rcu_batch_init(void)
  5065. {
  5066. int cpu;
  5067. int i, j;
  5068. struct shrinker *kfree_rcu_shrinker;
  5069. rcu_reclaim_wq = alloc_workqueue("kvfree_rcu_reclaim",
  5070. WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
  5071. WARN_ON(!rcu_reclaim_wq);
  5072. /* Clamp it to [0:100] seconds interval. */
  5073. if (rcu_delay_page_cache_fill_msec < 0 ||
  5074. rcu_delay_page_cache_fill_msec > 100 * MSEC_PER_SEC) {
  5075. rcu_delay_page_cache_fill_msec =
  5076. clamp(rcu_delay_page_cache_fill_msec, 0,
  5077. (int) (100 * MSEC_PER_SEC));
  5078. pr_info("Adjusting rcutree.rcu_delay_page_cache_fill_msec to %d ms.\n",
  5079. rcu_delay_page_cache_fill_msec);
  5080. }
  5081. for_each_possible_cpu(cpu) {
  5082. struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
  5083. for (i = 0; i < KFREE_N_BATCHES; i++) {
  5084. INIT_RCU_WORK(&krcp->krw_arr[i].rcu_work, kfree_rcu_work);
  5085. krcp->krw_arr[i].krcp = krcp;
  5086. for (j = 0; j < FREE_N_CHANNELS; j++)
  5087. INIT_LIST_HEAD(&krcp->krw_arr[i].bulk_head_free[j]);
  5088. }
  5089. for (i = 0; i < FREE_N_CHANNELS; i++)
  5090. INIT_LIST_HEAD(&krcp->bulk_head[i]);
  5091. INIT_DELAYED_WORK(&krcp->monitor_work, kfree_rcu_monitor);
  5092. INIT_DELAYED_WORK(&krcp->page_cache_work, fill_page_cache_func);
  5093. krcp->initialized = true;
  5094. }
  5095. kfree_rcu_shrinker = shrinker_alloc(0, "rcu-kfree");
  5096. if (!kfree_rcu_shrinker) {
  5097. pr_err("Failed to allocate kfree_rcu() shrinker!\n");
  5098. return;
  5099. }
  5100. kfree_rcu_shrinker->count_objects = kfree_rcu_shrink_count;
  5101. kfree_rcu_shrinker->scan_objects = kfree_rcu_shrink_scan;
  5102. shrinker_register(kfree_rcu_shrinker);
  5103. }
  5104. void __init rcu_init(void)
  5105. {
  5106. int cpu = smp_processor_id();
  5107. rcu_early_boot_tests();
  5108. kfree_rcu_batch_init();
  5109. rcu_bootup_announce();
  5110. sanitize_kthread_prio();
  5111. rcu_init_geometry();
  5112. rcu_init_one();
  5113. if (dump_tree)
  5114. rcu_dump_rcu_node_tree();
  5115. if (use_softirq)
  5116. open_softirq(RCU_SOFTIRQ, rcu_core_si);
  5117. /*
  5118. * We don't need protection against CPU-hotplug here because
  5119. * this is called early in boot, before either interrupts
  5120. * or the scheduler are operational.
  5121. */
  5122. pm_notifier(rcu_pm_notify, 0);
  5123. WARN_ON(num_online_cpus() > 1); // Only one CPU this early in boot.
  5124. rcutree_prepare_cpu(cpu);
  5125. rcutree_report_cpu_starting(cpu);
  5126. rcutree_online_cpu(cpu);
  5127. /* Create workqueue for Tree SRCU and for expedited GPs. */
  5128. rcu_gp_wq = alloc_workqueue("rcu_gp", WQ_MEM_RECLAIM, 0);
  5129. WARN_ON(!rcu_gp_wq);
  5130. sync_wq = alloc_workqueue("sync_wq", WQ_MEM_RECLAIM, 0);
  5131. WARN_ON(!sync_wq);
  5132. /* Fill in default value for rcutree.qovld boot parameter. */
  5133. /* -After- the rcu_node ->lock fields are initialized! */
  5134. if (qovld < 0)
  5135. qovld_calc = DEFAULT_RCU_QOVLD_MULT * qhimark;
  5136. else
  5137. qovld_calc = qovld;
  5138. // Kick-start in case any polled grace periods started early.
  5139. (void)start_poll_synchronize_rcu_expedited();
  5140. rcu_test_sync_prims();
  5141. tasks_cblist_init_generic();
  5142. }
  5143. #include "tree_stall.h"
  5144. #include "tree_exp.h"
  5145. #include "tree_nocb.h"
  5146. #include "tree_plugin.h"