core.c 221 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419
  1. /*
  2. * Performance events core code:
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra
  7. * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  8. *
  9. * For licensing details see kernel-base/COPYING
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/mm.h>
  13. #include <linux/cpu.h>
  14. #include <linux/smp.h>
  15. #include <linux/idr.h>
  16. #include <linux/file.h>
  17. #include <linux/poll.h>
  18. #include <linux/slab.h>
  19. #include <linux/hash.h>
  20. #include <linux/tick.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/dcache.h>
  23. #include <linux/percpu.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/reboot.h>
  26. #include <linux/vmstat.h>
  27. #include <linux/device.h>
  28. #include <linux/export.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/hardirq.h>
  31. #include <linux/rculist.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/syscalls.h>
  34. #include <linux/anon_inodes.h>
  35. #include <linux/kernel_stat.h>
  36. #include <linux/cgroup.h>
  37. #include <linux/perf_event.h>
  38. #include <linux/trace_events.h>
  39. #include <linux/hw_breakpoint.h>
  40. #include <linux/mm_types.h>
  41. #include <linux/module.h>
  42. #include <linux/mman.h>
  43. #include <linux/compat.h>
  44. #include <linux/bpf.h>
  45. #include <linux/filter.h>
  46. #include "internal.h"
  47. #include <asm/irq_regs.h>
  48. typedef int (*remote_function_f)(void *);
  49. struct remote_function_call {
  50. struct task_struct *p;
  51. remote_function_f func;
  52. void *info;
  53. int ret;
  54. };
  55. static void remote_function(void *data)
  56. {
  57. struct remote_function_call *tfc = data;
  58. struct task_struct *p = tfc->p;
  59. if (p) {
  60. tfc->ret = -EAGAIN;
  61. if (task_cpu(p) != smp_processor_id() || !task_curr(p))
  62. return;
  63. }
  64. tfc->ret = tfc->func(tfc->info);
  65. }
  66. /**
  67. * task_function_call - call a function on the cpu on which a task runs
  68. * @p: the task to evaluate
  69. * @func: the function to be called
  70. * @info: the function call argument
  71. *
  72. * Calls the function @func when the task is currently running. This might
  73. * be on the current CPU, which just calls the function directly
  74. *
  75. * returns: @func return value, or
  76. * -ESRCH - when the process isn't running
  77. * -EAGAIN - when the process moved away
  78. */
  79. static int
  80. task_function_call(struct task_struct *p, remote_function_f func, void *info)
  81. {
  82. struct remote_function_call data = {
  83. .p = p,
  84. .func = func,
  85. .info = info,
  86. .ret = -ESRCH, /* No such (running) process */
  87. };
  88. if (task_curr(p))
  89. smp_call_function_single(task_cpu(p), remote_function, &data, 1);
  90. return data.ret;
  91. }
  92. /**
  93. * cpu_function_call - call a function on the cpu
  94. * @func: the function to be called
  95. * @info: the function call argument
  96. *
  97. * Calls the function @func on the remote cpu.
  98. *
  99. * returns: @func return value or -ENXIO when the cpu is offline
  100. */
  101. static int cpu_function_call(int cpu, remote_function_f func, void *info)
  102. {
  103. struct remote_function_call data = {
  104. .p = NULL,
  105. .func = func,
  106. .info = info,
  107. .ret = -ENXIO, /* No such CPU */
  108. };
  109. smp_call_function_single(cpu, remote_function, &data, 1);
  110. return data.ret;
  111. }
  112. static inline struct perf_cpu_context *
  113. __get_cpu_context(struct perf_event_context *ctx)
  114. {
  115. return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
  116. }
  117. static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
  118. struct perf_event_context *ctx)
  119. {
  120. raw_spin_lock(&cpuctx->ctx.lock);
  121. if (ctx)
  122. raw_spin_lock(&ctx->lock);
  123. }
  124. static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
  125. struct perf_event_context *ctx)
  126. {
  127. if (ctx)
  128. raw_spin_unlock(&ctx->lock);
  129. raw_spin_unlock(&cpuctx->ctx.lock);
  130. }
  131. #define TASK_TOMBSTONE ((void *)-1L)
  132. static bool is_kernel_event(struct perf_event *event)
  133. {
  134. return READ_ONCE(event->owner) == TASK_TOMBSTONE;
  135. }
  136. /*
  137. * On task ctx scheduling...
  138. *
  139. * When !ctx->nr_events a task context will not be scheduled. This means
  140. * we can disable the scheduler hooks (for performance) without leaving
  141. * pending task ctx state.
  142. *
  143. * This however results in two special cases:
  144. *
  145. * - removing the last event from a task ctx; this is relatively straight
  146. * forward and is done in __perf_remove_from_context.
  147. *
  148. * - adding the first event to a task ctx; this is tricky because we cannot
  149. * rely on ctx->is_active and therefore cannot use event_function_call().
  150. * See perf_install_in_context().
  151. *
  152. * This is because we need a ctx->lock serialized variable (ctx->is_active)
  153. * to reliably determine if a particular task/context is scheduled in. The
  154. * task_curr() use in task_function_call() is racy in that a remote context
  155. * switch is not a single atomic operation.
  156. *
  157. * As is, the situation is 'safe' because we set rq->curr before we do the
  158. * actual context switch. This means that task_curr() will fail early, but
  159. * we'll continue spinning on ctx->is_active until we've passed
  160. * perf_event_task_sched_out().
  161. *
  162. * Without this ctx->lock serialized variable we could have race where we find
  163. * the task (and hence the context) would not be active while in fact they are.
  164. *
  165. * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set.
  166. */
  167. typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *,
  168. struct perf_event_context *, void *);
  169. struct event_function_struct {
  170. struct perf_event *event;
  171. event_f func;
  172. void *data;
  173. };
  174. static int event_function(void *info)
  175. {
  176. struct event_function_struct *efs = info;
  177. struct perf_event *event = efs->event;
  178. struct perf_event_context *ctx = event->ctx;
  179. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  180. struct perf_event_context *task_ctx = cpuctx->task_ctx;
  181. int ret = 0;
  182. WARN_ON_ONCE(!irqs_disabled());
  183. perf_ctx_lock(cpuctx, task_ctx);
  184. /*
  185. * Since we do the IPI call without holding ctx->lock things can have
  186. * changed, double check we hit the task we set out to hit.
  187. */
  188. if (ctx->task) {
  189. if (ctx->task != current) {
  190. ret = -EAGAIN;
  191. goto unlock;
  192. }
  193. /*
  194. * We only use event_function_call() on established contexts,
  195. * and event_function() is only ever called when active (or
  196. * rather, we'll have bailed in task_function_call() or the
  197. * above ctx->task != current test), therefore we must have
  198. * ctx->is_active here.
  199. */
  200. WARN_ON_ONCE(!ctx->is_active);
  201. /*
  202. * And since we have ctx->is_active, cpuctx->task_ctx must
  203. * match.
  204. */
  205. WARN_ON_ONCE(task_ctx != ctx);
  206. } else {
  207. WARN_ON_ONCE(&cpuctx->ctx != ctx);
  208. }
  209. efs->func(event, cpuctx, ctx, efs->data);
  210. unlock:
  211. perf_ctx_unlock(cpuctx, task_ctx);
  212. return ret;
  213. }
  214. static void event_function_local(struct perf_event *event, event_f func, void *data)
  215. {
  216. struct event_function_struct efs = {
  217. .event = event,
  218. .func = func,
  219. .data = data,
  220. };
  221. int ret = event_function(&efs);
  222. WARN_ON_ONCE(ret);
  223. }
  224. static void event_function_call(struct perf_event *event, event_f func, void *data)
  225. {
  226. struct perf_event_context *ctx = event->ctx;
  227. struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */
  228. struct event_function_struct efs = {
  229. .event = event,
  230. .func = func,
  231. .data = data,
  232. };
  233. if (!event->parent) {
  234. /*
  235. * If this is a !child event, we must hold ctx::mutex to
  236. * stabilize the the event->ctx relation. See
  237. * perf_event_ctx_lock().
  238. */
  239. lockdep_assert_held(&ctx->mutex);
  240. }
  241. if (!task) {
  242. cpu_function_call(event->cpu, event_function, &efs);
  243. return;
  244. }
  245. again:
  246. if (task == TASK_TOMBSTONE)
  247. return;
  248. if (!task_function_call(task, event_function, &efs))
  249. return;
  250. raw_spin_lock_irq(&ctx->lock);
  251. /*
  252. * Reload the task pointer, it might have been changed by
  253. * a concurrent perf_event_context_sched_out().
  254. */
  255. task = ctx->task;
  256. if (task != TASK_TOMBSTONE) {
  257. if (ctx->is_active) {
  258. raw_spin_unlock_irq(&ctx->lock);
  259. goto again;
  260. }
  261. func(event, NULL, ctx, data);
  262. }
  263. raw_spin_unlock_irq(&ctx->lock);
  264. }
  265. #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
  266. PERF_FLAG_FD_OUTPUT |\
  267. PERF_FLAG_PID_CGROUP |\
  268. PERF_FLAG_FD_CLOEXEC)
  269. /*
  270. * branch priv levels that need permission checks
  271. */
  272. #define PERF_SAMPLE_BRANCH_PERM_PLM \
  273. (PERF_SAMPLE_BRANCH_KERNEL |\
  274. PERF_SAMPLE_BRANCH_HV)
  275. enum event_type_t {
  276. EVENT_FLEXIBLE = 0x1,
  277. EVENT_PINNED = 0x2,
  278. EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
  279. };
  280. /*
  281. * perf_sched_events : >0 events exist
  282. * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
  283. */
  284. struct static_key_deferred perf_sched_events __read_mostly;
  285. static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
  286. static DEFINE_PER_CPU(int, perf_sched_cb_usages);
  287. static atomic_t nr_mmap_events __read_mostly;
  288. static atomic_t nr_comm_events __read_mostly;
  289. static atomic_t nr_task_events __read_mostly;
  290. static atomic_t nr_freq_events __read_mostly;
  291. static atomic_t nr_switch_events __read_mostly;
  292. static LIST_HEAD(pmus);
  293. static DEFINE_MUTEX(pmus_lock);
  294. static struct srcu_struct pmus_srcu;
  295. /*
  296. * perf event paranoia level:
  297. * -1 - not paranoid at all
  298. * 0 - disallow raw tracepoint access for unpriv
  299. * 1 - disallow cpu events for unpriv
  300. * 2 - disallow kernel profiling for unpriv
  301. */
  302. int sysctl_perf_event_paranoid __read_mostly = 1;
  303. /* Minimum for 512 kiB + 1 user control page */
  304. int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
  305. /*
  306. * max perf event sample rate
  307. */
  308. #define DEFAULT_MAX_SAMPLE_RATE 100000
  309. #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
  310. #define DEFAULT_CPU_TIME_MAX_PERCENT 25
  311. int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
  312. static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
  313. static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
  314. static int perf_sample_allowed_ns __read_mostly =
  315. DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
  316. static void update_perf_cpu_limits(void)
  317. {
  318. u64 tmp = perf_sample_period_ns;
  319. tmp *= sysctl_perf_cpu_time_max_percent;
  320. do_div(tmp, 100);
  321. ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
  322. }
  323. static int perf_rotate_context(struct perf_cpu_context *cpuctx);
  324. int perf_proc_update_handler(struct ctl_table *table, int write,
  325. void __user *buffer, size_t *lenp,
  326. loff_t *ppos)
  327. {
  328. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  329. if (ret || !write)
  330. return ret;
  331. max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
  332. perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
  333. update_perf_cpu_limits();
  334. return 0;
  335. }
  336. int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
  337. int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
  338. void __user *buffer, size_t *lenp,
  339. loff_t *ppos)
  340. {
  341. int ret = proc_dointvec(table, write, buffer, lenp, ppos);
  342. if (ret || !write)
  343. return ret;
  344. update_perf_cpu_limits();
  345. return 0;
  346. }
  347. /*
  348. * perf samples are done in some very critical code paths (NMIs).
  349. * If they take too much CPU time, the system can lock up and not
  350. * get any real work done. This will drop the sample rate when
  351. * we detect that events are taking too long.
  352. */
  353. #define NR_ACCUMULATED_SAMPLES 128
  354. static DEFINE_PER_CPU(u64, running_sample_length);
  355. static void perf_duration_warn(struct irq_work *w)
  356. {
  357. u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
  358. u64 avg_local_sample_len;
  359. u64 local_samples_len;
  360. local_samples_len = __this_cpu_read(running_sample_length);
  361. avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
  362. printk_ratelimited(KERN_WARNING
  363. "perf interrupt took too long (%lld > %lld), lowering "
  364. "kernel.perf_event_max_sample_rate to %d\n",
  365. avg_local_sample_len, allowed_ns >> 1,
  366. sysctl_perf_event_sample_rate);
  367. }
  368. static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
  369. void perf_sample_event_took(u64 sample_len_ns)
  370. {
  371. u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
  372. u64 avg_local_sample_len;
  373. u64 local_samples_len;
  374. if (allowed_ns == 0)
  375. return;
  376. /* decay the counter by 1 average sample */
  377. local_samples_len = __this_cpu_read(running_sample_length);
  378. local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
  379. local_samples_len += sample_len_ns;
  380. __this_cpu_write(running_sample_length, local_samples_len);
  381. /*
  382. * note: this will be biased artifically low until we have
  383. * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
  384. * from having to maintain a count.
  385. */
  386. avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
  387. if (avg_local_sample_len <= allowed_ns)
  388. return;
  389. if (max_samples_per_tick <= 1)
  390. return;
  391. max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
  392. sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
  393. perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
  394. update_perf_cpu_limits();
  395. if (!irq_work_queue(&perf_duration_work)) {
  396. early_printk("perf interrupt took too long (%lld > %lld), lowering "
  397. "kernel.perf_event_max_sample_rate to %d\n",
  398. avg_local_sample_len, allowed_ns >> 1,
  399. sysctl_perf_event_sample_rate);
  400. }
  401. }
  402. static atomic64_t perf_event_id;
  403. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  404. enum event_type_t event_type);
  405. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  406. enum event_type_t event_type,
  407. struct task_struct *task);
  408. static void update_context_time(struct perf_event_context *ctx);
  409. static u64 perf_event_time(struct perf_event *event);
  410. void __weak perf_event_print_debug(void) { }
  411. extern __weak const char *perf_pmu_name(void)
  412. {
  413. return "pmu";
  414. }
  415. static inline u64 perf_clock(void)
  416. {
  417. return local_clock();
  418. }
  419. static inline u64 perf_event_clock(struct perf_event *event)
  420. {
  421. return event->clock();
  422. }
  423. #ifdef CONFIG_CGROUP_PERF
  424. static inline bool
  425. perf_cgroup_match(struct perf_event *event)
  426. {
  427. struct perf_event_context *ctx = event->ctx;
  428. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  429. /* @event doesn't care about cgroup */
  430. if (!event->cgrp)
  431. return true;
  432. /* wants specific cgroup scope but @cpuctx isn't associated with any */
  433. if (!cpuctx->cgrp)
  434. return false;
  435. /*
  436. * Cgroup scoping is recursive. An event enabled for a cgroup is
  437. * also enabled for all its descendant cgroups. If @cpuctx's
  438. * cgroup is a descendant of @event's (the test covers identity
  439. * case), it's a match.
  440. */
  441. return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
  442. event->cgrp->css.cgroup);
  443. }
  444. static inline void perf_detach_cgroup(struct perf_event *event)
  445. {
  446. css_put(&event->cgrp->css);
  447. event->cgrp = NULL;
  448. }
  449. static inline int is_cgroup_event(struct perf_event *event)
  450. {
  451. return event->cgrp != NULL;
  452. }
  453. static inline u64 perf_cgroup_event_time(struct perf_event *event)
  454. {
  455. struct perf_cgroup_info *t;
  456. t = per_cpu_ptr(event->cgrp->info, event->cpu);
  457. return t->time;
  458. }
  459. static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
  460. {
  461. struct perf_cgroup_info *info;
  462. u64 now;
  463. now = perf_clock();
  464. info = this_cpu_ptr(cgrp->info);
  465. info->time += now - info->timestamp;
  466. info->timestamp = now;
  467. }
  468. static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
  469. {
  470. struct perf_cgroup *cgrp_out = cpuctx->cgrp;
  471. if (cgrp_out)
  472. __update_cgrp_time(cgrp_out);
  473. }
  474. static inline void update_cgrp_time_from_event(struct perf_event *event)
  475. {
  476. struct perf_cgroup *cgrp;
  477. /*
  478. * ensure we access cgroup data only when needed and
  479. * when we know the cgroup is pinned (css_get)
  480. */
  481. if (!is_cgroup_event(event))
  482. return;
  483. cgrp = perf_cgroup_from_task(current, event->ctx);
  484. /*
  485. * Do not update time when cgroup is not active
  486. */
  487. if (cgrp == event->cgrp)
  488. __update_cgrp_time(event->cgrp);
  489. }
  490. static inline void
  491. perf_cgroup_set_timestamp(struct task_struct *task,
  492. struct perf_event_context *ctx)
  493. {
  494. struct perf_cgroup *cgrp;
  495. struct perf_cgroup_info *info;
  496. /*
  497. * ctx->lock held by caller
  498. * ensure we do not access cgroup data
  499. * unless we have the cgroup pinned (css_get)
  500. */
  501. if (!task || !ctx->nr_cgroups)
  502. return;
  503. cgrp = perf_cgroup_from_task(task, ctx);
  504. info = this_cpu_ptr(cgrp->info);
  505. info->timestamp = ctx->timestamp;
  506. }
  507. #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
  508. #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
  509. /*
  510. * reschedule events based on the cgroup constraint of task.
  511. *
  512. * mode SWOUT : schedule out everything
  513. * mode SWIN : schedule in based on cgroup for next
  514. */
  515. static void perf_cgroup_switch(struct task_struct *task, int mode)
  516. {
  517. struct perf_cpu_context *cpuctx;
  518. struct pmu *pmu;
  519. unsigned long flags;
  520. /*
  521. * disable interrupts to avoid geting nr_cgroup
  522. * changes via __perf_event_disable(). Also
  523. * avoids preemption.
  524. */
  525. local_irq_save(flags);
  526. /*
  527. * we reschedule only in the presence of cgroup
  528. * constrained events.
  529. */
  530. list_for_each_entry_rcu(pmu, &pmus, entry) {
  531. cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  532. if (cpuctx->unique_pmu != pmu)
  533. continue; /* ensure we process each cpuctx once */
  534. /*
  535. * perf_cgroup_events says at least one
  536. * context on this CPU has cgroup events.
  537. *
  538. * ctx->nr_cgroups reports the number of cgroup
  539. * events for a context.
  540. */
  541. if (cpuctx->ctx.nr_cgroups > 0) {
  542. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  543. perf_pmu_disable(cpuctx->ctx.pmu);
  544. if (mode & PERF_CGROUP_SWOUT) {
  545. cpu_ctx_sched_out(cpuctx, EVENT_ALL);
  546. /*
  547. * must not be done before ctxswout due
  548. * to event_filter_match() in event_sched_out()
  549. */
  550. cpuctx->cgrp = NULL;
  551. }
  552. if (mode & PERF_CGROUP_SWIN) {
  553. WARN_ON_ONCE(cpuctx->cgrp);
  554. /*
  555. * set cgrp before ctxsw in to allow
  556. * event_filter_match() to not have to pass
  557. * task around
  558. * we pass the cpuctx->ctx to perf_cgroup_from_task()
  559. * because cgorup events are only per-cpu
  560. */
  561. cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx);
  562. cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
  563. }
  564. perf_pmu_enable(cpuctx->ctx.pmu);
  565. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  566. }
  567. }
  568. local_irq_restore(flags);
  569. }
  570. static inline void perf_cgroup_sched_out(struct task_struct *task,
  571. struct task_struct *next)
  572. {
  573. struct perf_cgroup *cgrp1;
  574. struct perf_cgroup *cgrp2 = NULL;
  575. rcu_read_lock();
  576. /*
  577. * we come here when we know perf_cgroup_events > 0
  578. * we do not need to pass the ctx here because we know
  579. * we are holding the rcu lock
  580. */
  581. cgrp1 = perf_cgroup_from_task(task, NULL);
  582. cgrp2 = perf_cgroup_from_task(next, NULL);
  583. /*
  584. * only schedule out current cgroup events if we know
  585. * that we are switching to a different cgroup. Otherwise,
  586. * do no touch the cgroup events.
  587. */
  588. if (cgrp1 != cgrp2)
  589. perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
  590. rcu_read_unlock();
  591. }
  592. static inline void perf_cgroup_sched_in(struct task_struct *prev,
  593. struct task_struct *task)
  594. {
  595. struct perf_cgroup *cgrp1;
  596. struct perf_cgroup *cgrp2 = NULL;
  597. rcu_read_lock();
  598. /*
  599. * we come here when we know perf_cgroup_events > 0
  600. * we do not need to pass the ctx here because we know
  601. * we are holding the rcu lock
  602. */
  603. cgrp1 = perf_cgroup_from_task(task, NULL);
  604. cgrp2 = perf_cgroup_from_task(prev, NULL);
  605. /*
  606. * only need to schedule in cgroup events if we are changing
  607. * cgroup during ctxsw. Cgroup events were not scheduled
  608. * out of ctxsw out if that was not the case.
  609. */
  610. if (cgrp1 != cgrp2)
  611. perf_cgroup_switch(task, PERF_CGROUP_SWIN);
  612. rcu_read_unlock();
  613. }
  614. static inline int perf_cgroup_connect(int fd, struct perf_event *event,
  615. struct perf_event_attr *attr,
  616. struct perf_event *group_leader)
  617. {
  618. struct perf_cgroup *cgrp;
  619. struct cgroup_subsys_state *css;
  620. struct fd f = fdget(fd);
  621. int ret = 0;
  622. if (!f.file)
  623. return -EBADF;
  624. css = css_tryget_online_from_dir(f.file->f_path.dentry,
  625. &perf_event_cgrp_subsys);
  626. if (IS_ERR(css)) {
  627. ret = PTR_ERR(css);
  628. goto out;
  629. }
  630. cgrp = container_of(css, struct perf_cgroup, css);
  631. event->cgrp = cgrp;
  632. /*
  633. * all events in a group must monitor
  634. * the same cgroup because a task belongs
  635. * to only one perf cgroup at a time
  636. */
  637. if (group_leader && group_leader->cgrp != cgrp) {
  638. perf_detach_cgroup(event);
  639. ret = -EINVAL;
  640. }
  641. out:
  642. fdput(f);
  643. return ret;
  644. }
  645. static inline void
  646. perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
  647. {
  648. struct perf_cgroup_info *t;
  649. t = per_cpu_ptr(event->cgrp->info, event->cpu);
  650. event->shadow_ctx_time = now - t->timestamp;
  651. }
  652. static inline void
  653. perf_cgroup_defer_enabled(struct perf_event *event)
  654. {
  655. /*
  656. * when the current task's perf cgroup does not match
  657. * the event's, we need to remember to call the
  658. * perf_mark_enable() function the first time a task with
  659. * a matching perf cgroup is scheduled in.
  660. */
  661. if (is_cgroup_event(event) && !perf_cgroup_match(event))
  662. event->cgrp_defer_enabled = 1;
  663. }
  664. static inline void
  665. perf_cgroup_mark_enabled(struct perf_event *event,
  666. struct perf_event_context *ctx)
  667. {
  668. struct perf_event *sub;
  669. u64 tstamp = perf_event_time(event);
  670. if (!event->cgrp_defer_enabled)
  671. return;
  672. event->cgrp_defer_enabled = 0;
  673. event->tstamp_enabled = tstamp - event->total_time_enabled;
  674. list_for_each_entry(sub, &event->sibling_list, group_entry) {
  675. if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
  676. sub->tstamp_enabled = tstamp - sub->total_time_enabled;
  677. sub->cgrp_defer_enabled = 0;
  678. }
  679. }
  680. }
  681. #else /* !CONFIG_CGROUP_PERF */
  682. static inline bool
  683. perf_cgroup_match(struct perf_event *event)
  684. {
  685. return true;
  686. }
  687. static inline void perf_detach_cgroup(struct perf_event *event)
  688. {}
  689. static inline int is_cgroup_event(struct perf_event *event)
  690. {
  691. return 0;
  692. }
  693. static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
  694. {
  695. return 0;
  696. }
  697. static inline void update_cgrp_time_from_event(struct perf_event *event)
  698. {
  699. }
  700. static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
  701. {
  702. }
  703. static inline void perf_cgroup_sched_out(struct task_struct *task,
  704. struct task_struct *next)
  705. {
  706. }
  707. static inline void perf_cgroup_sched_in(struct task_struct *prev,
  708. struct task_struct *task)
  709. {
  710. }
  711. static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
  712. struct perf_event_attr *attr,
  713. struct perf_event *group_leader)
  714. {
  715. return -EINVAL;
  716. }
  717. static inline void
  718. perf_cgroup_set_timestamp(struct task_struct *task,
  719. struct perf_event_context *ctx)
  720. {
  721. }
  722. void
  723. perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
  724. {
  725. }
  726. static inline void
  727. perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
  728. {
  729. }
  730. static inline u64 perf_cgroup_event_time(struct perf_event *event)
  731. {
  732. return 0;
  733. }
  734. static inline void
  735. perf_cgroup_defer_enabled(struct perf_event *event)
  736. {
  737. }
  738. static inline void
  739. perf_cgroup_mark_enabled(struct perf_event *event,
  740. struct perf_event_context *ctx)
  741. {
  742. }
  743. #endif
  744. /*
  745. * set default to be dependent on timer tick just
  746. * like original code
  747. */
  748. #define PERF_CPU_HRTIMER (1000 / HZ)
  749. /*
  750. * function must be called with interrupts disbled
  751. */
  752. static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
  753. {
  754. struct perf_cpu_context *cpuctx;
  755. int rotations = 0;
  756. WARN_ON(!irqs_disabled());
  757. cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
  758. rotations = perf_rotate_context(cpuctx);
  759. raw_spin_lock(&cpuctx->hrtimer_lock);
  760. if (rotations)
  761. hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
  762. else
  763. cpuctx->hrtimer_active = 0;
  764. raw_spin_unlock(&cpuctx->hrtimer_lock);
  765. return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
  766. }
  767. static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
  768. {
  769. struct hrtimer *timer = &cpuctx->hrtimer;
  770. struct pmu *pmu = cpuctx->ctx.pmu;
  771. u64 interval;
  772. /* no multiplexing needed for SW PMU */
  773. if (pmu->task_ctx_nr == perf_sw_context)
  774. return;
  775. /*
  776. * check default is sane, if not set then force to
  777. * default interval (1/tick)
  778. */
  779. interval = pmu->hrtimer_interval_ms;
  780. if (interval < 1)
  781. interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
  782. cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
  783. raw_spin_lock_init(&cpuctx->hrtimer_lock);
  784. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
  785. timer->function = perf_mux_hrtimer_handler;
  786. }
  787. static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
  788. {
  789. struct hrtimer *timer = &cpuctx->hrtimer;
  790. struct pmu *pmu = cpuctx->ctx.pmu;
  791. unsigned long flags;
  792. /* not for SW PMU */
  793. if (pmu->task_ctx_nr == perf_sw_context)
  794. return 0;
  795. raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
  796. if (!cpuctx->hrtimer_active) {
  797. cpuctx->hrtimer_active = 1;
  798. hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
  799. hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
  800. }
  801. raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
  802. return 0;
  803. }
  804. void perf_pmu_disable(struct pmu *pmu)
  805. {
  806. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  807. if (!(*count)++)
  808. pmu->pmu_disable(pmu);
  809. }
  810. void perf_pmu_enable(struct pmu *pmu)
  811. {
  812. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  813. if (!--(*count))
  814. pmu->pmu_enable(pmu);
  815. }
  816. static DEFINE_PER_CPU(struct list_head, active_ctx_list);
  817. /*
  818. * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
  819. * perf_event_task_tick() are fully serialized because they're strictly cpu
  820. * affine and perf_event_ctx{activate,deactivate} are called with IRQs
  821. * disabled, while perf_event_task_tick is called from IRQ context.
  822. */
  823. static void perf_event_ctx_activate(struct perf_event_context *ctx)
  824. {
  825. struct list_head *head = this_cpu_ptr(&active_ctx_list);
  826. WARN_ON(!irqs_disabled());
  827. WARN_ON(!list_empty(&ctx->active_ctx_list));
  828. list_add(&ctx->active_ctx_list, head);
  829. }
  830. static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
  831. {
  832. WARN_ON(!irqs_disabled());
  833. WARN_ON(list_empty(&ctx->active_ctx_list));
  834. list_del_init(&ctx->active_ctx_list);
  835. }
  836. static void get_ctx(struct perf_event_context *ctx)
  837. {
  838. WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
  839. }
  840. static void free_ctx(struct rcu_head *head)
  841. {
  842. struct perf_event_context *ctx;
  843. ctx = container_of(head, struct perf_event_context, rcu_head);
  844. kfree(ctx->task_ctx_data);
  845. kfree(ctx);
  846. }
  847. static void put_ctx(struct perf_event_context *ctx)
  848. {
  849. if (atomic_dec_and_test(&ctx->refcount)) {
  850. if (ctx->parent_ctx)
  851. put_ctx(ctx->parent_ctx);
  852. if (ctx->task && ctx->task != TASK_TOMBSTONE)
  853. put_task_struct(ctx->task);
  854. call_rcu(&ctx->rcu_head, free_ctx);
  855. }
  856. }
  857. /*
  858. * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
  859. * perf_pmu_migrate_context() we need some magic.
  860. *
  861. * Those places that change perf_event::ctx will hold both
  862. * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
  863. *
  864. * Lock ordering is by mutex address. There are two other sites where
  865. * perf_event_context::mutex nests and those are:
  866. *
  867. * - perf_event_exit_task_context() [ child , 0 ]
  868. * perf_event_exit_event()
  869. * put_event() [ parent, 1 ]
  870. *
  871. * - perf_event_init_context() [ parent, 0 ]
  872. * inherit_task_group()
  873. * inherit_group()
  874. * inherit_event()
  875. * perf_event_alloc()
  876. * perf_init_event()
  877. * perf_try_init_event() [ child , 1 ]
  878. *
  879. * While it appears there is an obvious deadlock here -- the parent and child
  880. * nesting levels are inverted between the two. This is in fact safe because
  881. * life-time rules separate them. That is an exiting task cannot fork, and a
  882. * spawning task cannot (yet) exit.
  883. *
  884. * But remember that that these are parent<->child context relations, and
  885. * migration does not affect children, therefore these two orderings should not
  886. * interact.
  887. *
  888. * The change in perf_event::ctx does not affect children (as claimed above)
  889. * because the sys_perf_event_open() case will install a new event and break
  890. * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
  891. * concerned with cpuctx and that doesn't have children.
  892. *
  893. * The places that change perf_event::ctx will issue:
  894. *
  895. * perf_remove_from_context();
  896. * synchronize_rcu();
  897. * perf_install_in_context();
  898. *
  899. * to affect the change. The remove_from_context() + synchronize_rcu() should
  900. * quiesce the event, after which we can install it in the new location. This
  901. * means that only external vectors (perf_fops, prctl) can perturb the event
  902. * while in transit. Therefore all such accessors should also acquire
  903. * perf_event_context::mutex to serialize against this.
  904. *
  905. * However; because event->ctx can change while we're waiting to acquire
  906. * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
  907. * function.
  908. *
  909. * Lock order:
  910. * task_struct::perf_event_mutex
  911. * perf_event_context::mutex
  912. * perf_event::child_mutex;
  913. * perf_event_context::lock
  914. * perf_event::mmap_mutex
  915. * mmap_sem
  916. */
  917. static struct perf_event_context *
  918. perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
  919. {
  920. struct perf_event_context *ctx;
  921. again:
  922. rcu_read_lock();
  923. ctx = ACCESS_ONCE(event->ctx);
  924. if (!atomic_inc_not_zero(&ctx->refcount)) {
  925. rcu_read_unlock();
  926. goto again;
  927. }
  928. rcu_read_unlock();
  929. mutex_lock_nested(&ctx->mutex, nesting);
  930. if (event->ctx != ctx) {
  931. mutex_unlock(&ctx->mutex);
  932. put_ctx(ctx);
  933. goto again;
  934. }
  935. return ctx;
  936. }
  937. static inline struct perf_event_context *
  938. perf_event_ctx_lock(struct perf_event *event)
  939. {
  940. return perf_event_ctx_lock_nested(event, 0);
  941. }
  942. static void perf_event_ctx_unlock(struct perf_event *event,
  943. struct perf_event_context *ctx)
  944. {
  945. mutex_unlock(&ctx->mutex);
  946. put_ctx(ctx);
  947. }
  948. /*
  949. * This must be done under the ctx->lock, such as to serialize against
  950. * context_equiv(), therefore we cannot call put_ctx() since that might end up
  951. * calling scheduler related locks and ctx->lock nests inside those.
  952. */
  953. static __must_check struct perf_event_context *
  954. unclone_ctx(struct perf_event_context *ctx)
  955. {
  956. struct perf_event_context *parent_ctx = ctx->parent_ctx;
  957. lockdep_assert_held(&ctx->lock);
  958. if (parent_ctx)
  959. ctx->parent_ctx = NULL;
  960. ctx->generation++;
  961. return parent_ctx;
  962. }
  963. static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
  964. {
  965. /*
  966. * only top level events have the pid namespace they were created in
  967. */
  968. if (event->parent)
  969. event = event->parent;
  970. return task_tgid_nr_ns(p, event->ns);
  971. }
  972. static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
  973. {
  974. /*
  975. * only top level events have the pid namespace they were created in
  976. */
  977. if (event->parent)
  978. event = event->parent;
  979. return task_pid_nr_ns(p, event->ns);
  980. }
  981. /*
  982. * If we inherit events we want to return the parent event id
  983. * to userspace.
  984. */
  985. static u64 primary_event_id(struct perf_event *event)
  986. {
  987. u64 id = event->id;
  988. if (event->parent)
  989. id = event->parent->id;
  990. return id;
  991. }
  992. /*
  993. * Get the perf_event_context for a task and lock it.
  994. *
  995. * This has to cope with with the fact that until it is locked,
  996. * the context could get moved to another task.
  997. */
  998. static struct perf_event_context *
  999. perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
  1000. {
  1001. struct perf_event_context *ctx;
  1002. retry:
  1003. /*
  1004. * One of the few rules of preemptible RCU is that one cannot do
  1005. * rcu_read_unlock() while holding a scheduler (or nested) lock when
  1006. * part of the read side critical section was irqs-enabled -- see
  1007. * rcu_read_unlock_special().
  1008. *
  1009. * Since ctx->lock nests under rq->lock we must ensure the entire read
  1010. * side critical section has interrupts disabled.
  1011. */
  1012. local_irq_save(*flags);
  1013. rcu_read_lock();
  1014. ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
  1015. if (ctx) {
  1016. /*
  1017. * If this context is a clone of another, it might
  1018. * get swapped for another underneath us by
  1019. * perf_event_task_sched_out, though the
  1020. * rcu_read_lock() protects us from any context
  1021. * getting freed. Lock the context and check if it
  1022. * got swapped before we could get the lock, and retry
  1023. * if so. If we locked the right context, then it
  1024. * can't get swapped on us any more.
  1025. */
  1026. raw_spin_lock(&ctx->lock);
  1027. if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
  1028. raw_spin_unlock(&ctx->lock);
  1029. rcu_read_unlock();
  1030. local_irq_restore(*flags);
  1031. goto retry;
  1032. }
  1033. if (ctx->task == TASK_TOMBSTONE ||
  1034. !atomic_inc_not_zero(&ctx->refcount)) {
  1035. raw_spin_unlock(&ctx->lock);
  1036. ctx = NULL;
  1037. } else {
  1038. WARN_ON_ONCE(ctx->task != task);
  1039. }
  1040. }
  1041. rcu_read_unlock();
  1042. if (!ctx)
  1043. local_irq_restore(*flags);
  1044. return ctx;
  1045. }
  1046. /*
  1047. * Get the context for a task and increment its pin_count so it
  1048. * can't get swapped to another task. This also increments its
  1049. * reference count so that the context can't get freed.
  1050. */
  1051. static struct perf_event_context *
  1052. perf_pin_task_context(struct task_struct *task, int ctxn)
  1053. {
  1054. struct perf_event_context *ctx;
  1055. unsigned long flags;
  1056. ctx = perf_lock_task_context(task, ctxn, &flags);
  1057. if (ctx) {
  1058. ++ctx->pin_count;
  1059. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1060. }
  1061. return ctx;
  1062. }
  1063. static void perf_unpin_context(struct perf_event_context *ctx)
  1064. {
  1065. unsigned long flags;
  1066. raw_spin_lock_irqsave(&ctx->lock, flags);
  1067. --ctx->pin_count;
  1068. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1069. }
  1070. /*
  1071. * Update the record of the current time in a context.
  1072. */
  1073. static void update_context_time(struct perf_event_context *ctx)
  1074. {
  1075. u64 now = perf_clock();
  1076. ctx->time += now - ctx->timestamp;
  1077. ctx->timestamp = now;
  1078. }
  1079. static u64 perf_event_time(struct perf_event *event)
  1080. {
  1081. struct perf_event_context *ctx = event->ctx;
  1082. if (is_cgroup_event(event))
  1083. return perf_cgroup_event_time(event);
  1084. return ctx ? ctx->time : 0;
  1085. }
  1086. /*
  1087. * Update the total_time_enabled and total_time_running fields for a event.
  1088. * The caller of this function needs to hold the ctx->lock.
  1089. */
  1090. static void update_event_times(struct perf_event *event)
  1091. {
  1092. struct perf_event_context *ctx = event->ctx;
  1093. u64 run_end;
  1094. if (event->state < PERF_EVENT_STATE_INACTIVE ||
  1095. event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
  1096. return;
  1097. /*
  1098. * in cgroup mode, time_enabled represents
  1099. * the time the event was enabled AND active
  1100. * tasks were in the monitored cgroup. This is
  1101. * independent of the activity of the context as
  1102. * there may be a mix of cgroup and non-cgroup events.
  1103. *
  1104. * That is why we treat cgroup events differently
  1105. * here.
  1106. */
  1107. if (is_cgroup_event(event))
  1108. run_end = perf_cgroup_event_time(event);
  1109. else if (ctx->is_active)
  1110. run_end = ctx->time;
  1111. else
  1112. run_end = event->tstamp_stopped;
  1113. event->total_time_enabled = run_end - event->tstamp_enabled;
  1114. if (event->state == PERF_EVENT_STATE_INACTIVE)
  1115. run_end = event->tstamp_stopped;
  1116. else
  1117. run_end = perf_event_time(event);
  1118. event->total_time_running = run_end - event->tstamp_running;
  1119. }
  1120. /*
  1121. * Update total_time_enabled and total_time_running for all events in a group.
  1122. */
  1123. static void update_group_times(struct perf_event *leader)
  1124. {
  1125. struct perf_event *event;
  1126. update_event_times(leader);
  1127. list_for_each_entry(event, &leader->sibling_list, group_entry)
  1128. update_event_times(event);
  1129. }
  1130. static struct list_head *
  1131. ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
  1132. {
  1133. if (event->attr.pinned)
  1134. return &ctx->pinned_groups;
  1135. else
  1136. return &ctx->flexible_groups;
  1137. }
  1138. /*
  1139. * Add a event from the lists for its context.
  1140. * Must be called with ctx->mutex and ctx->lock held.
  1141. */
  1142. static void
  1143. list_add_event(struct perf_event *event, struct perf_event_context *ctx)
  1144. {
  1145. lockdep_assert_held(&ctx->lock);
  1146. WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
  1147. event->attach_state |= PERF_ATTACH_CONTEXT;
  1148. /*
  1149. * If we're a stand alone event or group leader, we go to the context
  1150. * list, group events are kept attached to the group so that
  1151. * perf_group_detach can, at all times, locate all siblings.
  1152. */
  1153. if (event->group_leader == event) {
  1154. struct list_head *list;
  1155. if (is_software_event(event))
  1156. event->group_flags |= PERF_GROUP_SOFTWARE;
  1157. list = ctx_group_list(event, ctx);
  1158. list_add_tail(&event->group_entry, list);
  1159. }
  1160. if (is_cgroup_event(event))
  1161. ctx->nr_cgroups++;
  1162. list_add_rcu(&event->event_entry, &ctx->event_list);
  1163. ctx->nr_events++;
  1164. if (event->attr.inherit_stat)
  1165. ctx->nr_stat++;
  1166. ctx->generation++;
  1167. }
  1168. /*
  1169. * Initialize event state based on the perf_event_attr::disabled.
  1170. */
  1171. static inline void perf_event__state_init(struct perf_event *event)
  1172. {
  1173. event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
  1174. PERF_EVENT_STATE_INACTIVE;
  1175. }
  1176. static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
  1177. {
  1178. int entry = sizeof(u64); /* value */
  1179. int size = 0;
  1180. int nr = 1;
  1181. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1182. size += sizeof(u64);
  1183. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1184. size += sizeof(u64);
  1185. if (event->attr.read_format & PERF_FORMAT_ID)
  1186. entry += sizeof(u64);
  1187. if (event->attr.read_format & PERF_FORMAT_GROUP) {
  1188. nr += nr_siblings;
  1189. size += sizeof(u64);
  1190. }
  1191. size += entry * nr;
  1192. event->read_size = size;
  1193. }
  1194. static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
  1195. {
  1196. struct perf_sample_data *data;
  1197. u16 size = 0;
  1198. if (sample_type & PERF_SAMPLE_IP)
  1199. size += sizeof(data->ip);
  1200. if (sample_type & PERF_SAMPLE_ADDR)
  1201. size += sizeof(data->addr);
  1202. if (sample_type & PERF_SAMPLE_PERIOD)
  1203. size += sizeof(data->period);
  1204. if (sample_type & PERF_SAMPLE_WEIGHT)
  1205. size += sizeof(data->weight);
  1206. if (sample_type & PERF_SAMPLE_READ)
  1207. size += event->read_size;
  1208. if (sample_type & PERF_SAMPLE_DATA_SRC)
  1209. size += sizeof(data->data_src.val);
  1210. if (sample_type & PERF_SAMPLE_TRANSACTION)
  1211. size += sizeof(data->txn);
  1212. event->header_size = size;
  1213. }
  1214. /*
  1215. * Called at perf_event creation and when events are attached/detached from a
  1216. * group.
  1217. */
  1218. static void perf_event__header_size(struct perf_event *event)
  1219. {
  1220. __perf_event_read_size(event,
  1221. event->group_leader->nr_siblings);
  1222. __perf_event_header_size(event, event->attr.sample_type);
  1223. }
  1224. static void perf_event__id_header_size(struct perf_event *event)
  1225. {
  1226. struct perf_sample_data *data;
  1227. u64 sample_type = event->attr.sample_type;
  1228. u16 size = 0;
  1229. if (sample_type & PERF_SAMPLE_TID)
  1230. size += sizeof(data->tid_entry);
  1231. if (sample_type & PERF_SAMPLE_TIME)
  1232. size += sizeof(data->time);
  1233. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  1234. size += sizeof(data->id);
  1235. if (sample_type & PERF_SAMPLE_ID)
  1236. size += sizeof(data->id);
  1237. if (sample_type & PERF_SAMPLE_STREAM_ID)
  1238. size += sizeof(data->stream_id);
  1239. if (sample_type & PERF_SAMPLE_CPU)
  1240. size += sizeof(data->cpu_entry);
  1241. event->id_header_size = size;
  1242. }
  1243. static bool perf_event_validate_size(struct perf_event *event)
  1244. {
  1245. /*
  1246. * The values computed here will be over-written when we actually
  1247. * attach the event.
  1248. */
  1249. __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
  1250. __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
  1251. perf_event__id_header_size(event);
  1252. /*
  1253. * Sum the lot; should not exceed the 64k limit we have on records.
  1254. * Conservative limit to allow for callchains and other variable fields.
  1255. */
  1256. if (event->read_size + event->header_size +
  1257. event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
  1258. return false;
  1259. return true;
  1260. }
  1261. static void perf_group_attach(struct perf_event *event)
  1262. {
  1263. struct perf_event *group_leader = event->group_leader, *pos;
  1264. /*
  1265. * We can have double attach due to group movement in perf_event_open.
  1266. */
  1267. if (event->attach_state & PERF_ATTACH_GROUP)
  1268. return;
  1269. event->attach_state |= PERF_ATTACH_GROUP;
  1270. if (group_leader == event)
  1271. return;
  1272. WARN_ON_ONCE(group_leader->ctx != event->ctx);
  1273. if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
  1274. !is_software_event(event))
  1275. group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
  1276. list_add_tail(&event->group_entry, &group_leader->sibling_list);
  1277. group_leader->nr_siblings++;
  1278. perf_event__header_size(group_leader);
  1279. list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
  1280. perf_event__header_size(pos);
  1281. }
  1282. /*
  1283. * Remove a event from the lists for its context.
  1284. * Must be called with ctx->mutex and ctx->lock held.
  1285. */
  1286. static void
  1287. list_del_event(struct perf_event *event, struct perf_event_context *ctx)
  1288. {
  1289. struct perf_cpu_context *cpuctx;
  1290. WARN_ON_ONCE(event->ctx != ctx);
  1291. lockdep_assert_held(&ctx->lock);
  1292. /*
  1293. * We can have double detach due to exit/hot-unplug + close.
  1294. */
  1295. if (!(event->attach_state & PERF_ATTACH_CONTEXT))
  1296. return;
  1297. event->attach_state &= ~PERF_ATTACH_CONTEXT;
  1298. if (is_cgroup_event(event)) {
  1299. ctx->nr_cgroups--;
  1300. /*
  1301. * Because cgroup events are always per-cpu events, this will
  1302. * always be called from the right CPU.
  1303. */
  1304. cpuctx = __get_cpu_context(ctx);
  1305. /*
  1306. * If there are no more cgroup events then clear cgrp to avoid
  1307. * stale pointer in update_cgrp_time_from_cpuctx().
  1308. */
  1309. if (!ctx->nr_cgroups)
  1310. cpuctx->cgrp = NULL;
  1311. }
  1312. ctx->nr_events--;
  1313. if (event->attr.inherit_stat)
  1314. ctx->nr_stat--;
  1315. list_del_rcu(&event->event_entry);
  1316. if (event->group_leader == event)
  1317. list_del_init(&event->group_entry);
  1318. update_group_times(event);
  1319. /*
  1320. * If event was in error state, then keep it
  1321. * that way, otherwise bogus counts will be
  1322. * returned on read(). The only way to get out
  1323. * of error state is by explicit re-enabling
  1324. * of the event
  1325. */
  1326. if (event->state > PERF_EVENT_STATE_OFF)
  1327. event->state = PERF_EVENT_STATE_OFF;
  1328. ctx->generation++;
  1329. }
  1330. static void perf_group_detach(struct perf_event *event)
  1331. {
  1332. struct perf_event *sibling, *tmp;
  1333. struct list_head *list = NULL;
  1334. /*
  1335. * We can have double detach due to exit/hot-unplug + close.
  1336. */
  1337. if (!(event->attach_state & PERF_ATTACH_GROUP))
  1338. return;
  1339. event->attach_state &= ~PERF_ATTACH_GROUP;
  1340. /*
  1341. * If this is a sibling, remove it from its group.
  1342. */
  1343. if (event->group_leader != event) {
  1344. list_del_init(&event->group_entry);
  1345. event->group_leader->nr_siblings--;
  1346. goto out;
  1347. }
  1348. if (!list_empty(&event->group_entry))
  1349. list = &event->group_entry;
  1350. /*
  1351. * If this was a group event with sibling events then
  1352. * upgrade the siblings to singleton events by adding them
  1353. * to whatever list we are on.
  1354. */
  1355. list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
  1356. if (list)
  1357. list_move_tail(&sibling->group_entry, list);
  1358. sibling->group_leader = sibling;
  1359. /* Inherit group flags from the previous leader */
  1360. sibling->group_flags = event->group_flags;
  1361. WARN_ON_ONCE(sibling->ctx != event->ctx);
  1362. }
  1363. out:
  1364. perf_event__header_size(event->group_leader);
  1365. list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
  1366. perf_event__header_size(tmp);
  1367. }
  1368. static bool is_orphaned_event(struct perf_event *event)
  1369. {
  1370. return event->state == PERF_EVENT_STATE_EXIT;
  1371. }
  1372. static inline int pmu_filter_match(struct perf_event *event)
  1373. {
  1374. struct pmu *pmu = event->pmu;
  1375. return pmu->filter_match ? pmu->filter_match(event) : 1;
  1376. }
  1377. static inline int
  1378. event_filter_match(struct perf_event *event)
  1379. {
  1380. return (event->cpu == -1 || event->cpu == smp_processor_id())
  1381. && perf_cgroup_match(event) && pmu_filter_match(event);
  1382. }
  1383. static void
  1384. event_sched_out(struct perf_event *event,
  1385. struct perf_cpu_context *cpuctx,
  1386. struct perf_event_context *ctx)
  1387. {
  1388. u64 tstamp = perf_event_time(event);
  1389. u64 delta;
  1390. WARN_ON_ONCE(event->ctx != ctx);
  1391. lockdep_assert_held(&ctx->lock);
  1392. /*
  1393. * An event which could not be activated because of
  1394. * filter mismatch still needs to have its timings
  1395. * maintained, otherwise bogus information is return
  1396. * via read() for time_enabled, time_running:
  1397. */
  1398. if (event->state == PERF_EVENT_STATE_INACTIVE
  1399. && !event_filter_match(event)) {
  1400. delta = tstamp - event->tstamp_stopped;
  1401. event->tstamp_running += delta;
  1402. event->tstamp_stopped = tstamp;
  1403. }
  1404. if (event->state != PERF_EVENT_STATE_ACTIVE)
  1405. return;
  1406. perf_pmu_disable(event->pmu);
  1407. event->state = PERF_EVENT_STATE_INACTIVE;
  1408. if (event->pending_disable) {
  1409. event->pending_disable = 0;
  1410. event->state = PERF_EVENT_STATE_OFF;
  1411. }
  1412. event->tstamp_stopped = tstamp;
  1413. event->pmu->del(event, 0);
  1414. event->oncpu = -1;
  1415. if (!is_software_event(event))
  1416. cpuctx->active_oncpu--;
  1417. if (!--ctx->nr_active)
  1418. perf_event_ctx_deactivate(ctx);
  1419. if (event->attr.freq && event->attr.sample_freq)
  1420. ctx->nr_freq--;
  1421. if (event->attr.exclusive || !cpuctx->active_oncpu)
  1422. cpuctx->exclusive = 0;
  1423. perf_pmu_enable(event->pmu);
  1424. }
  1425. static void
  1426. group_sched_out(struct perf_event *group_event,
  1427. struct perf_cpu_context *cpuctx,
  1428. struct perf_event_context *ctx)
  1429. {
  1430. struct perf_event *event;
  1431. int state = group_event->state;
  1432. event_sched_out(group_event, cpuctx, ctx);
  1433. /*
  1434. * Schedule out siblings (if any):
  1435. */
  1436. list_for_each_entry(event, &group_event->sibling_list, group_entry)
  1437. event_sched_out(event, cpuctx, ctx);
  1438. if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
  1439. cpuctx->exclusive = 0;
  1440. }
  1441. #define DETACH_GROUP 0x01UL
  1442. #define DETACH_STATE 0x02UL
  1443. /*
  1444. * Cross CPU call to remove a performance event
  1445. *
  1446. * We disable the event on the hardware level first. After that we
  1447. * remove it from the context list.
  1448. */
  1449. static void
  1450. __perf_remove_from_context(struct perf_event *event,
  1451. struct perf_cpu_context *cpuctx,
  1452. struct perf_event_context *ctx,
  1453. void *info)
  1454. {
  1455. unsigned long flags = (unsigned long)info;
  1456. event_sched_out(event, cpuctx, ctx);
  1457. if (flags & DETACH_GROUP)
  1458. perf_group_detach(event);
  1459. list_del_event(event, ctx);
  1460. if (flags & DETACH_STATE)
  1461. event->state = PERF_EVENT_STATE_EXIT;
  1462. if (!ctx->nr_events && ctx->is_active) {
  1463. ctx->is_active = 0;
  1464. if (ctx->task) {
  1465. WARN_ON_ONCE(cpuctx->task_ctx != ctx);
  1466. cpuctx->task_ctx = NULL;
  1467. }
  1468. }
  1469. }
  1470. /*
  1471. * Remove the event from a task's (or a CPU's) list of events.
  1472. *
  1473. * If event->ctx is a cloned context, callers must make sure that
  1474. * every task struct that event->ctx->task could possibly point to
  1475. * remains valid. This is OK when called from perf_release since
  1476. * that only calls us on the top-level context, which can't be a clone.
  1477. * When called from perf_event_exit_task, it's OK because the
  1478. * context has been detached from its task.
  1479. */
  1480. static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
  1481. {
  1482. lockdep_assert_held(&event->ctx->mutex);
  1483. event_function_call(event, __perf_remove_from_context, (void *)flags);
  1484. }
  1485. /*
  1486. * Cross CPU call to disable a performance event
  1487. */
  1488. static void __perf_event_disable(struct perf_event *event,
  1489. struct perf_cpu_context *cpuctx,
  1490. struct perf_event_context *ctx,
  1491. void *info)
  1492. {
  1493. if (event->state < PERF_EVENT_STATE_INACTIVE)
  1494. return;
  1495. update_context_time(ctx);
  1496. update_cgrp_time_from_event(event);
  1497. update_group_times(event);
  1498. if (event == event->group_leader)
  1499. group_sched_out(event, cpuctx, ctx);
  1500. else
  1501. event_sched_out(event, cpuctx, ctx);
  1502. event->state = PERF_EVENT_STATE_OFF;
  1503. }
  1504. /*
  1505. * Disable a event.
  1506. *
  1507. * If event->ctx is a cloned context, callers must make sure that
  1508. * every task struct that event->ctx->task could possibly point to
  1509. * remains valid. This condition is satisifed when called through
  1510. * perf_event_for_each_child or perf_event_for_each because they
  1511. * hold the top-level event's child_mutex, so any descendant that
  1512. * goes to exit will block in perf_event_exit_event().
  1513. *
  1514. * When called from perf_pending_event it's OK because event->ctx
  1515. * is the current context on this CPU and preemption is disabled,
  1516. * hence we can't get into perf_event_task_sched_out for this context.
  1517. */
  1518. static void _perf_event_disable(struct perf_event *event)
  1519. {
  1520. struct perf_event_context *ctx = event->ctx;
  1521. raw_spin_lock_irq(&ctx->lock);
  1522. if (event->state <= PERF_EVENT_STATE_OFF) {
  1523. raw_spin_unlock_irq(&ctx->lock);
  1524. return;
  1525. }
  1526. raw_spin_unlock_irq(&ctx->lock);
  1527. event_function_call(event, __perf_event_disable, NULL);
  1528. }
  1529. void perf_event_disable_local(struct perf_event *event)
  1530. {
  1531. event_function_local(event, __perf_event_disable, NULL);
  1532. }
  1533. /*
  1534. * Strictly speaking kernel users cannot create groups and therefore this
  1535. * interface does not need the perf_event_ctx_lock() magic.
  1536. */
  1537. void perf_event_disable(struct perf_event *event)
  1538. {
  1539. struct perf_event_context *ctx;
  1540. ctx = perf_event_ctx_lock(event);
  1541. _perf_event_disable(event);
  1542. perf_event_ctx_unlock(event, ctx);
  1543. }
  1544. EXPORT_SYMBOL_GPL(perf_event_disable);
  1545. static void perf_set_shadow_time(struct perf_event *event,
  1546. struct perf_event_context *ctx,
  1547. u64 tstamp)
  1548. {
  1549. /*
  1550. * use the correct time source for the time snapshot
  1551. *
  1552. * We could get by without this by leveraging the
  1553. * fact that to get to this function, the caller
  1554. * has most likely already called update_context_time()
  1555. * and update_cgrp_time_xx() and thus both timestamp
  1556. * are identical (or very close). Given that tstamp is,
  1557. * already adjusted for cgroup, we could say that:
  1558. * tstamp - ctx->timestamp
  1559. * is equivalent to
  1560. * tstamp - cgrp->timestamp.
  1561. *
  1562. * Then, in perf_output_read(), the calculation would
  1563. * work with no changes because:
  1564. * - event is guaranteed scheduled in
  1565. * - no scheduled out in between
  1566. * - thus the timestamp would be the same
  1567. *
  1568. * But this is a bit hairy.
  1569. *
  1570. * So instead, we have an explicit cgroup call to remain
  1571. * within the time time source all along. We believe it
  1572. * is cleaner and simpler to understand.
  1573. */
  1574. if (is_cgroup_event(event))
  1575. perf_cgroup_set_shadow_time(event, tstamp);
  1576. else
  1577. event->shadow_ctx_time = tstamp - ctx->timestamp;
  1578. }
  1579. #define MAX_INTERRUPTS (~0ULL)
  1580. static void perf_log_throttle(struct perf_event *event, int enable);
  1581. static void perf_log_itrace_start(struct perf_event *event);
  1582. static int
  1583. event_sched_in(struct perf_event *event,
  1584. struct perf_cpu_context *cpuctx,
  1585. struct perf_event_context *ctx)
  1586. {
  1587. u64 tstamp = perf_event_time(event);
  1588. int ret = 0;
  1589. lockdep_assert_held(&ctx->lock);
  1590. if (event->state <= PERF_EVENT_STATE_OFF)
  1591. return 0;
  1592. event->state = PERF_EVENT_STATE_ACTIVE;
  1593. event->oncpu = smp_processor_id();
  1594. /*
  1595. * Unthrottle events, since we scheduled we might have missed several
  1596. * ticks already, also for a heavily scheduling task there is little
  1597. * guarantee it'll get a tick in a timely manner.
  1598. */
  1599. if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
  1600. perf_log_throttle(event, 1);
  1601. event->hw.interrupts = 0;
  1602. }
  1603. /*
  1604. * The new state must be visible before we turn it on in the hardware:
  1605. */
  1606. smp_wmb();
  1607. perf_pmu_disable(event->pmu);
  1608. perf_set_shadow_time(event, ctx, tstamp);
  1609. perf_log_itrace_start(event);
  1610. if (event->pmu->add(event, PERF_EF_START)) {
  1611. event->state = PERF_EVENT_STATE_INACTIVE;
  1612. event->oncpu = -1;
  1613. ret = -EAGAIN;
  1614. goto out;
  1615. }
  1616. event->tstamp_running += tstamp - event->tstamp_stopped;
  1617. if (!is_software_event(event))
  1618. cpuctx->active_oncpu++;
  1619. if (!ctx->nr_active++)
  1620. perf_event_ctx_activate(ctx);
  1621. if (event->attr.freq && event->attr.sample_freq)
  1622. ctx->nr_freq++;
  1623. if (event->attr.exclusive)
  1624. cpuctx->exclusive = 1;
  1625. out:
  1626. perf_pmu_enable(event->pmu);
  1627. return ret;
  1628. }
  1629. static int
  1630. group_sched_in(struct perf_event *group_event,
  1631. struct perf_cpu_context *cpuctx,
  1632. struct perf_event_context *ctx)
  1633. {
  1634. struct perf_event *event, *partial_group = NULL;
  1635. struct pmu *pmu = ctx->pmu;
  1636. u64 now = ctx->time;
  1637. bool simulate = false;
  1638. if (group_event->state == PERF_EVENT_STATE_OFF)
  1639. return 0;
  1640. pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
  1641. if (event_sched_in(group_event, cpuctx, ctx)) {
  1642. pmu->cancel_txn(pmu);
  1643. perf_mux_hrtimer_restart(cpuctx);
  1644. return -EAGAIN;
  1645. }
  1646. /*
  1647. * Schedule in siblings as one group (if any):
  1648. */
  1649. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  1650. if (event_sched_in(event, cpuctx, ctx)) {
  1651. partial_group = event;
  1652. goto group_error;
  1653. }
  1654. }
  1655. if (!pmu->commit_txn(pmu))
  1656. return 0;
  1657. group_error:
  1658. /*
  1659. * Groups can be scheduled in as one unit only, so undo any
  1660. * partial group before returning:
  1661. * The events up to the failed event are scheduled out normally,
  1662. * tstamp_stopped will be updated.
  1663. *
  1664. * The failed events and the remaining siblings need to have
  1665. * their timings updated as if they had gone thru event_sched_in()
  1666. * and event_sched_out(). This is required to get consistent timings
  1667. * across the group. This also takes care of the case where the group
  1668. * could never be scheduled by ensuring tstamp_stopped is set to mark
  1669. * the time the event was actually stopped, such that time delta
  1670. * calculation in update_event_times() is correct.
  1671. */
  1672. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  1673. if (event == partial_group)
  1674. simulate = true;
  1675. if (simulate) {
  1676. event->tstamp_running += now - event->tstamp_stopped;
  1677. event->tstamp_stopped = now;
  1678. } else {
  1679. event_sched_out(event, cpuctx, ctx);
  1680. }
  1681. }
  1682. event_sched_out(group_event, cpuctx, ctx);
  1683. pmu->cancel_txn(pmu);
  1684. perf_mux_hrtimer_restart(cpuctx);
  1685. return -EAGAIN;
  1686. }
  1687. /*
  1688. * Work out whether we can put this event group on the CPU now.
  1689. */
  1690. static int group_can_go_on(struct perf_event *event,
  1691. struct perf_cpu_context *cpuctx,
  1692. int can_add_hw)
  1693. {
  1694. /*
  1695. * Groups consisting entirely of software events can always go on.
  1696. */
  1697. if (event->group_flags & PERF_GROUP_SOFTWARE)
  1698. return 1;
  1699. /*
  1700. * If an exclusive group is already on, no other hardware
  1701. * events can go on.
  1702. */
  1703. if (cpuctx->exclusive)
  1704. return 0;
  1705. /*
  1706. * If this group is exclusive and there are already
  1707. * events on the CPU, it can't go on.
  1708. */
  1709. if (event->attr.exclusive && cpuctx->active_oncpu)
  1710. return 0;
  1711. /*
  1712. * Otherwise, try to add it if all previous groups were able
  1713. * to go on.
  1714. */
  1715. return can_add_hw;
  1716. }
  1717. static void add_event_to_ctx(struct perf_event *event,
  1718. struct perf_event_context *ctx)
  1719. {
  1720. u64 tstamp = perf_event_time(event);
  1721. list_add_event(event, ctx);
  1722. perf_group_attach(event);
  1723. event->tstamp_enabled = tstamp;
  1724. event->tstamp_running = tstamp;
  1725. event->tstamp_stopped = tstamp;
  1726. }
  1727. static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
  1728. struct perf_event_context *ctx);
  1729. static void
  1730. ctx_sched_in(struct perf_event_context *ctx,
  1731. struct perf_cpu_context *cpuctx,
  1732. enum event_type_t event_type,
  1733. struct task_struct *task);
  1734. static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
  1735. struct perf_event_context *ctx,
  1736. struct task_struct *task)
  1737. {
  1738. cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
  1739. if (ctx)
  1740. ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
  1741. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
  1742. if (ctx)
  1743. ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
  1744. }
  1745. static void ctx_resched(struct perf_cpu_context *cpuctx,
  1746. struct perf_event_context *task_ctx)
  1747. {
  1748. perf_pmu_disable(cpuctx->ctx.pmu);
  1749. if (task_ctx)
  1750. task_ctx_sched_out(cpuctx, task_ctx);
  1751. cpu_ctx_sched_out(cpuctx, EVENT_ALL);
  1752. perf_event_sched_in(cpuctx, task_ctx, current);
  1753. perf_pmu_enable(cpuctx->ctx.pmu);
  1754. }
  1755. /*
  1756. * Cross CPU call to install and enable a performance event
  1757. *
  1758. * Must be called with ctx->mutex held
  1759. */
  1760. static int __perf_install_in_context(void *info)
  1761. {
  1762. struct perf_event_context *ctx = info;
  1763. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1764. struct perf_event_context *task_ctx = cpuctx->task_ctx;
  1765. raw_spin_lock(&cpuctx->ctx.lock);
  1766. if (ctx->task) {
  1767. raw_spin_lock(&ctx->lock);
  1768. /*
  1769. * If we hit the 'wrong' task, we've since scheduled and
  1770. * everything should be sorted, nothing to do!
  1771. */
  1772. task_ctx = ctx;
  1773. if (ctx->task != current)
  1774. goto unlock;
  1775. /*
  1776. * If task_ctx is set, it had better be to us.
  1777. */
  1778. WARN_ON_ONCE(cpuctx->task_ctx != ctx && cpuctx->task_ctx);
  1779. } else if (task_ctx) {
  1780. raw_spin_lock(&task_ctx->lock);
  1781. }
  1782. ctx_resched(cpuctx, task_ctx);
  1783. unlock:
  1784. perf_ctx_unlock(cpuctx, task_ctx);
  1785. return 0;
  1786. }
  1787. /*
  1788. * Attach a performance event to a context
  1789. */
  1790. static void
  1791. perf_install_in_context(struct perf_event_context *ctx,
  1792. struct perf_event *event,
  1793. int cpu)
  1794. {
  1795. struct task_struct *task = NULL;
  1796. lockdep_assert_held(&ctx->mutex);
  1797. event->ctx = ctx;
  1798. if (event->cpu != -1)
  1799. event->cpu = cpu;
  1800. /*
  1801. * Installing events is tricky because we cannot rely on ctx->is_active
  1802. * to be set in case this is the nr_events 0 -> 1 transition.
  1803. *
  1804. * So what we do is we add the event to the list here, which will allow
  1805. * a future context switch to DTRT and then send a racy IPI. If the IPI
  1806. * fails to hit the right task, this means a context switch must have
  1807. * happened and that will have taken care of business.
  1808. */
  1809. raw_spin_lock_irq(&ctx->lock);
  1810. task = ctx->task;
  1811. /*
  1812. * Worse, we cannot even rely on the ctx actually existing anymore. If
  1813. * between find_get_context() and perf_install_in_context() the task
  1814. * went through perf_event_exit_task() its dead and we should not be
  1815. * adding new events.
  1816. */
  1817. if (task == TASK_TOMBSTONE) {
  1818. raw_spin_unlock_irq(&ctx->lock);
  1819. return;
  1820. }
  1821. update_context_time(ctx);
  1822. /*
  1823. * Update cgrp time only if current cgrp matches event->cgrp.
  1824. * Must be done before calling add_event_to_ctx().
  1825. */
  1826. update_cgrp_time_from_event(event);
  1827. add_event_to_ctx(event, ctx);
  1828. raw_spin_unlock_irq(&ctx->lock);
  1829. if (task)
  1830. task_function_call(task, __perf_install_in_context, ctx);
  1831. else
  1832. cpu_function_call(cpu, __perf_install_in_context, ctx);
  1833. }
  1834. /*
  1835. * Put a event into inactive state and update time fields.
  1836. * Enabling the leader of a group effectively enables all
  1837. * the group members that aren't explicitly disabled, so we
  1838. * have to update their ->tstamp_enabled also.
  1839. * Note: this works for group members as well as group leaders
  1840. * since the non-leader members' sibling_lists will be empty.
  1841. */
  1842. static void __perf_event_mark_enabled(struct perf_event *event)
  1843. {
  1844. struct perf_event *sub;
  1845. u64 tstamp = perf_event_time(event);
  1846. event->state = PERF_EVENT_STATE_INACTIVE;
  1847. event->tstamp_enabled = tstamp - event->total_time_enabled;
  1848. list_for_each_entry(sub, &event->sibling_list, group_entry) {
  1849. if (sub->state >= PERF_EVENT_STATE_INACTIVE)
  1850. sub->tstamp_enabled = tstamp - sub->total_time_enabled;
  1851. }
  1852. }
  1853. /*
  1854. * Cross CPU call to enable a performance event
  1855. */
  1856. static void __perf_event_enable(struct perf_event *event,
  1857. struct perf_cpu_context *cpuctx,
  1858. struct perf_event_context *ctx,
  1859. void *info)
  1860. {
  1861. struct perf_event *leader = event->group_leader;
  1862. struct perf_event_context *task_ctx;
  1863. if (event->state >= PERF_EVENT_STATE_INACTIVE ||
  1864. event->state <= PERF_EVENT_STATE_ERROR)
  1865. return;
  1866. update_context_time(ctx);
  1867. __perf_event_mark_enabled(event);
  1868. if (!ctx->is_active)
  1869. return;
  1870. if (!event_filter_match(event)) {
  1871. if (is_cgroup_event(event)) {
  1872. perf_cgroup_set_timestamp(current, ctx); // XXX ?
  1873. perf_cgroup_defer_enabled(event);
  1874. }
  1875. return;
  1876. }
  1877. /*
  1878. * If the event is in a group and isn't the group leader,
  1879. * then don't put it on unless the group is on.
  1880. */
  1881. if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
  1882. return;
  1883. task_ctx = cpuctx->task_ctx;
  1884. if (ctx->task)
  1885. WARN_ON_ONCE(task_ctx != ctx);
  1886. ctx_resched(cpuctx, task_ctx);
  1887. }
  1888. /*
  1889. * Enable a event.
  1890. *
  1891. * If event->ctx is a cloned context, callers must make sure that
  1892. * every task struct that event->ctx->task could possibly point to
  1893. * remains valid. This condition is satisfied when called through
  1894. * perf_event_for_each_child or perf_event_for_each as described
  1895. * for perf_event_disable.
  1896. */
  1897. static void _perf_event_enable(struct perf_event *event)
  1898. {
  1899. struct perf_event_context *ctx = event->ctx;
  1900. raw_spin_lock_irq(&ctx->lock);
  1901. if (event->state >= PERF_EVENT_STATE_INACTIVE ||
  1902. event->state < PERF_EVENT_STATE_ERROR) {
  1903. raw_spin_unlock_irq(&ctx->lock);
  1904. return;
  1905. }
  1906. /*
  1907. * If the event is in error state, clear that first.
  1908. *
  1909. * That way, if we see the event in error state below, we know that it
  1910. * has gone back into error state, as distinct from the task having
  1911. * been scheduled away before the cross-call arrived.
  1912. */
  1913. if (event->state == PERF_EVENT_STATE_ERROR)
  1914. event->state = PERF_EVENT_STATE_OFF;
  1915. raw_spin_unlock_irq(&ctx->lock);
  1916. event_function_call(event, __perf_event_enable, NULL);
  1917. }
  1918. /*
  1919. * See perf_event_disable();
  1920. */
  1921. void perf_event_enable(struct perf_event *event)
  1922. {
  1923. struct perf_event_context *ctx;
  1924. ctx = perf_event_ctx_lock(event);
  1925. _perf_event_enable(event);
  1926. perf_event_ctx_unlock(event, ctx);
  1927. }
  1928. EXPORT_SYMBOL_GPL(perf_event_enable);
  1929. static int _perf_event_refresh(struct perf_event *event, int refresh)
  1930. {
  1931. /*
  1932. * not supported on inherited events
  1933. */
  1934. if (event->attr.inherit || !is_sampling_event(event))
  1935. return -EINVAL;
  1936. atomic_add(refresh, &event->event_limit);
  1937. _perf_event_enable(event);
  1938. return 0;
  1939. }
  1940. /*
  1941. * See perf_event_disable()
  1942. */
  1943. int perf_event_refresh(struct perf_event *event, int refresh)
  1944. {
  1945. struct perf_event_context *ctx;
  1946. int ret;
  1947. ctx = perf_event_ctx_lock(event);
  1948. ret = _perf_event_refresh(event, refresh);
  1949. perf_event_ctx_unlock(event, ctx);
  1950. return ret;
  1951. }
  1952. EXPORT_SYMBOL_GPL(perf_event_refresh);
  1953. static void ctx_sched_out(struct perf_event_context *ctx,
  1954. struct perf_cpu_context *cpuctx,
  1955. enum event_type_t event_type)
  1956. {
  1957. int is_active = ctx->is_active;
  1958. struct perf_event *event;
  1959. lockdep_assert_held(&ctx->lock);
  1960. if (likely(!ctx->nr_events)) {
  1961. /*
  1962. * See __perf_remove_from_context().
  1963. */
  1964. WARN_ON_ONCE(ctx->is_active);
  1965. if (ctx->task)
  1966. WARN_ON_ONCE(cpuctx->task_ctx);
  1967. return;
  1968. }
  1969. ctx->is_active &= ~event_type;
  1970. if (ctx->task) {
  1971. WARN_ON_ONCE(cpuctx->task_ctx != ctx);
  1972. if (!ctx->is_active)
  1973. cpuctx->task_ctx = NULL;
  1974. }
  1975. update_context_time(ctx);
  1976. update_cgrp_time_from_cpuctx(cpuctx);
  1977. if (!ctx->nr_active)
  1978. return;
  1979. perf_pmu_disable(ctx->pmu);
  1980. if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
  1981. list_for_each_entry(event, &ctx->pinned_groups, group_entry)
  1982. group_sched_out(event, cpuctx, ctx);
  1983. }
  1984. if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
  1985. list_for_each_entry(event, &ctx->flexible_groups, group_entry)
  1986. group_sched_out(event, cpuctx, ctx);
  1987. }
  1988. perf_pmu_enable(ctx->pmu);
  1989. }
  1990. /*
  1991. * Test whether two contexts are equivalent, i.e. whether they have both been
  1992. * cloned from the same version of the same context.
  1993. *
  1994. * Equivalence is measured using a generation number in the context that is
  1995. * incremented on each modification to it; see unclone_ctx(), list_add_event()
  1996. * and list_del_event().
  1997. */
  1998. static int context_equiv(struct perf_event_context *ctx1,
  1999. struct perf_event_context *ctx2)
  2000. {
  2001. lockdep_assert_held(&ctx1->lock);
  2002. lockdep_assert_held(&ctx2->lock);
  2003. /* Pinning disables the swap optimization */
  2004. if (ctx1->pin_count || ctx2->pin_count)
  2005. return 0;
  2006. /* If ctx1 is the parent of ctx2 */
  2007. if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
  2008. return 1;
  2009. /* If ctx2 is the parent of ctx1 */
  2010. if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
  2011. return 1;
  2012. /*
  2013. * If ctx1 and ctx2 have the same parent; we flatten the parent
  2014. * hierarchy, see perf_event_init_context().
  2015. */
  2016. if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
  2017. ctx1->parent_gen == ctx2->parent_gen)
  2018. return 1;
  2019. /* Unmatched */
  2020. return 0;
  2021. }
  2022. static void __perf_event_sync_stat(struct perf_event *event,
  2023. struct perf_event *next_event)
  2024. {
  2025. u64 value;
  2026. if (!event->attr.inherit_stat)
  2027. return;
  2028. /*
  2029. * Update the event value, we cannot use perf_event_read()
  2030. * because we're in the middle of a context switch and have IRQs
  2031. * disabled, which upsets smp_call_function_single(), however
  2032. * we know the event must be on the current CPU, therefore we
  2033. * don't need to use it.
  2034. */
  2035. switch (event->state) {
  2036. case PERF_EVENT_STATE_ACTIVE:
  2037. event->pmu->read(event);
  2038. /* fall-through */
  2039. case PERF_EVENT_STATE_INACTIVE:
  2040. update_event_times(event);
  2041. break;
  2042. default:
  2043. break;
  2044. }
  2045. /*
  2046. * In order to keep per-task stats reliable we need to flip the event
  2047. * values when we flip the contexts.
  2048. */
  2049. value = local64_read(&next_event->count);
  2050. value = local64_xchg(&event->count, value);
  2051. local64_set(&next_event->count, value);
  2052. swap(event->total_time_enabled, next_event->total_time_enabled);
  2053. swap(event->total_time_running, next_event->total_time_running);
  2054. /*
  2055. * Since we swizzled the values, update the user visible data too.
  2056. */
  2057. perf_event_update_userpage(event);
  2058. perf_event_update_userpage(next_event);
  2059. }
  2060. static void perf_event_sync_stat(struct perf_event_context *ctx,
  2061. struct perf_event_context *next_ctx)
  2062. {
  2063. struct perf_event *event, *next_event;
  2064. if (!ctx->nr_stat)
  2065. return;
  2066. update_context_time(ctx);
  2067. event = list_first_entry(&ctx->event_list,
  2068. struct perf_event, event_entry);
  2069. next_event = list_first_entry(&next_ctx->event_list,
  2070. struct perf_event, event_entry);
  2071. while (&event->event_entry != &ctx->event_list &&
  2072. &next_event->event_entry != &next_ctx->event_list) {
  2073. __perf_event_sync_stat(event, next_event);
  2074. event = list_next_entry(event, event_entry);
  2075. next_event = list_next_entry(next_event, event_entry);
  2076. }
  2077. }
  2078. static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
  2079. struct task_struct *next)
  2080. {
  2081. struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
  2082. struct perf_event_context *next_ctx;
  2083. struct perf_event_context *parent, *next_parent;
  2084. struct perf_cpu_context *cpuctx;
  2085. int do_switch = 1;
  2086. if (likely(!ctx))
  2087. return;
  2088. cpuctx = __get_cpu_context(ctx);
  2089. if (!cpuctx->task_ctx)
  2090. return;
  2091. rcu_read_lock();
  2092. next_ctx = next->perf_event_ctxp[ctxn];
  2093. if (!next_ctx)
  2094. goto unlock;
  2095. parent = rcu_dereference(ctx->parent_ctx);
  2096. next_parent = rcu_dereference(next_ctx->parent_ctx);
  2097. /* If neither context have a parent context; they cannot be clones. */
  2098. if (!parent && !next_parent)
  2099. goto unlock;
  2100. if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
  2101. /*
  2102. * Looks like the two contexts are clones, so we might be
  2103. * able to optimize the context switch. We lock both
  2104. * contexts and check that they are clones under the
  2105. * lock (including re-checking that neither has been
  2106. * uncloned in the meantime). It doesn't matter which
  2107. * order we take the locks because no other cpu could
  2108. * be trying to lock both of these tasks.
  2109. */
  2110. raw_spin_lock(&ctx->lock);
  2111. raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
  2112. if (context_equiv(ctx, next_ctx)) {
  2113. WRITE_ONCE(ctx->task, next);
  2114. WRITE_ONCE(next_ctx->task, task);
  2115. swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
  2116. /*
  2117. * RCU_INIT_POINTER here is safe because we've not
  2118. * modified the ctx and the above modification of
  2119. * ctx->task and ctx->task_ctx_data are immaterial
  2120. * since those values are always verified under
  2121. * ctx->lock which we're now holding.
  2122. */
  2123. RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
  2124. RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
  2125. do_switch = 0;
  2126. perf_event_sync_stat(ctx, next_ctx);
  2127. }
  2128. raw_spin_unlock(&next_ctx->lock);
  2129. raw_spin_unlock(&ctx->lock);
  2130. }
  2131. unlock:
  2132. rcu_read_unlock();
  2133. if (do_switch) {
  2134. raw_spin_lock(&ctx->lock);
  2135. task_ctx_sched_out(cpuctx, ctx);
  2136. raw_spin_unlock(&ctx->lock);
  2137. }
  2138. }
  2139. void perf_sched_cb_dec(struct pmu *pmu)
  2140. {
  2141. this_cpu_dec(perf_sched_cb_usages);
  2142. }
  2143. void perf_sched_cb_inc(struct pmu *pmu)
  2144. {
  2145. this_cpu_inc(perf_sched_cb_usages);
  2146. }
  2147. /*
  2148. * This function provides the context switch callback to the lower code
  2149. * layer. It is invoked ONLY when the context switch callback is enabled.
  2150. */
  2151. static void perf_pmu_sched_task(struct task_struct *prev,
  2152. struct task_struct *next,
  2153. bool sched_in)
  2154. {
  2155. struct perf_cpu_context *cpuctx;
  2156. struct pmu *pmu;
  2157. unsigned long flags;
  2158. if (prev == next)
  2159. return;
  2160. local_irq_save(flags);
  2161. rcu_read_lock();
  2162. list_for_each_entry_rcu(pmu, &pmus, entry) {
  2163. if (pmu->sched_task) {
  2164. cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  2165. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  2166. perf_pmu_disable(pmu);
  2167. pmu->sched_task(cpuctx->task_ctx, sched_in);
  2168. perf_pmu_enable(pmu);
  2169. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  2170. }
  2171. }
  2172. rcu_read_unlock();
  2173. local_irq_restore(flags);
  2174. }
  2175. static void perf_event_switch(struct task_struct *task,
  2176. struct task_struct *next_prev, bool sched_in);
  2177. #define for_each_task_context_nr(ctxn) \
  2178. for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
  2179. /*
  2180. * Called from scheduler to remove the events of the current task,
  2181. * with interrupts disabled.
  2182. *
  2183. * We stop each event and update the event value in event->count.
  2184. *
  2185. * This does not protect us against NMI, but disable()
  2186. * sets the disabled bit in the control field of event _before_
  2187. * accessing the event control register. If a NMI hits, then it will
  2188. * not restart the event.
  2189. */
  2190. void __perf_event_task_sched_out(struct task_struct *task,
  2191. struct task_struct *next)
  2192. {
  2193. int ctxn;
  2194. if (__this_cpu_read(perf_sched_cb_usages))
  2195. perf_pmu_sched_task(task, next, false);
  2196. if (atomic_read(&nr_switch_events))
  2197. perf_event_switch(task, next, false);
  2198. for_each_task_context_nr(ctxn)
  2199. perf_event_context_sched_out(task, ctxn, next);
  2200. /*
  2201. * if cgroup events exist on this CPU, then we need
  2202. * to check if we have to switch out PMU state.
  2203. * cgroup event are system-wide mode only
  2204. */
  2205. if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
  2206. perf_cgroup_sched_out(task, next);
  2207. }
  2208. static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
  2209. struct perf_event_context *ctx)
  2210. {
  2211. if (!cpuctx->task_ctx)
  2212. return;
  2213. if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
  2214. return;
  2215. ctx_sched_out(ctx, cpuctx, EVENT_ALL);
  2216. }
  2217. /*
  2218. * Called with IRQs disabled
  2219. */
  2220. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  2221. enum event_type_t event_type)
  2222. {
  2223. ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
  2224. }
  2225. static void
  2226. ctx_pinned_sched_in(struct perf_event_context *ctx,
  2227. struct perf_cpu_context *cpuctx)
  2228. {
  2229. struct perf_event *event;
  2230. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  2231. if (event->state <= PERF_EVENT_STATE_OFF)
  2232. continue;
  2233. if (!event_filter_match(event))
  2234. continue;
  2235. /* may need to reset tstamp_enabled */
  2236. if (is_cgroup_event(event))
  2237. perf_cgroup_mark_enabled(event, ctx);
  2238. if (group_can_go_on(event, cpuctx, 1))
  2239. group_sched_in(event, cpuctx, ctx);
  2240. /*
  2241. * If this pinned group hasn't been scheduled,
  2242. * put it in error state.
  2243. */
  2244. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  2245. update_group_times(event);
  2246. event->state = PERF_EVENT_STATE_ERROR;
  2247. }
  2248. }
  2249. }
  2250. static void
  2251. ctx_flexible_sched_in(struct perf_event_context *ctx,
  2252. struct perf_cpu_context *cpuctx)
  2253. {
  2254. struct perf_event *event;
  2255. int can_add_hw = 1;
  2256. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  2257. /* Ignore events in OFF or ERROR state */
  2258. if (event->state <= PERF_EVENT_STATE_OFF)
  2259. continue;
  2260. /*
  2261. * Listen to the 'cpu' scheduling filter constraint
  2262. * of events:
  2263. */
  2264. if (!event_filter_match(event))
  2265. continue;
  2266. /* may need to reset tstamp_enabled */
  2267. if (is_cgroup_event(event))
  2268. perf_cgroup_mark_enabled(event, ctx);
  2269. if (group_can_go_on(event, cpuctx, can_add_hw)) {
  2270. if (group_sched_in(event, cpuctx, ctx))
  2271. can_add_hw = 0;
  2272. }
  2273. }
  2274. }
  2275. static void
  2276. ctx_sched_in(struct perf_event_context *ctx,
  2277. struct perf_cpu_context *cpuctx,
  2278. enum event_type_t event_type,
  2279. struct task_struct *task)
  2280. {
  2281. int is_active = ctx->is_active;
  2282. u64 now;
  2283. lockdep_assert_held(&ctx->lock);
  2284. if (likely(!ctx->nr_events))
  2285. return;
  2286. ctx->is_active |= event_type;
  2287. if (ctx->task) {
  2288. if (!is_active)
  2289. cpuctx->task_ctx = ctx;
  2290. else
  2291. WARN_ON_ONCE(cpuctx->task_ctx != ctx);
  2292. }
  2293. now = perf_clock();
  2294. ctx->timestamp = now;
  2295. perf_cgroup_set_timestamp(task, ctx);
  2296. /*
  2297. * First go through the list and put on any pinned groups
  2298. * in order to give them the best chance of going on.
  2299. */
  2300. if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
  2301. ctx_pinned_sched_in(ctx, cpuctx);
  2302. /* Then walk through the lower prio flexible groups */
  2303. if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
  2304. ctx_flexible_sched_in(ctx, cpuctx);
  2305. }
  2306. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  2307. enum event_type_t event_type,
  2308. struct task_struct *task)
  2309. {
  2310. struct perf_event_context *ctx = &cpuctx->ctx;
  2311. ctx_sched_in(ctx, cpuctx, event_type, task);
  2312. }
  2313. static void perf_event_context_sched_in(struct perf_event_context *ctx,
  2314. struct task_struct *task)
  2315. {
  2316. struct perf_cpu_context *cpuctx;
  2317. cpuctx = __get_cpu_context(ctx);
  2318. if (cpuctx->task_ctx == ctx)
  2319. return;
  2320. perf_ctx_lock(cpuctx, ctx);
  2321. perf_pmu_disable(ctx->pmu);
  2322. /*
  2323. * We want to keep the following priority order:
  2324. * cpu pinned (that don't need to move), task pinned,
  2325. * cpu flexible, task flexible.
  2326. */
  2327. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  2328. perf_event_sched_in(cpuctx, ctx, task);
  2329. perf_pmu_enable(ctx->pmu);
  2330. perf_ctx_unlock(cpuctx, ctx);
  2331. }
  2332. /*
  2333. * Called from scheduler to add the events of the current task
  2334. * with interrupts disabled.
  2335. *
  2336. * We restore the event value and then enable it.
  2337. *
  2338. * This does not protect us against NMI, but enable()
  2339. * sets the enabled bit in the control field of event _before_
  2340. * accessing the event control register. If a NMI hits, then it will
  2341. * keep the event running.
  2342. */
  2343. void __perf_event_task_sched_in(struct task_struct *prev,
  2344. struct task_struct *task)
  2345. {
  2346. struct perf_event_context *ctx;
  2347. int ctxn;
  2348. /*
  2349. * If cgroup events exist on this CPU, then we need to check if we have
  2350. * to switch in PMU state; cgroup event are system-wide mode only.
  2351. *
  2352. * Since cgroup events are CPU events, we must schedule these in before
  2353. * we schedule in the task events.
  2354. */
  2355. if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
  2356. perf_cgroup_sched_in(prev, task);
  2357. for_each_task_context_nr(ctxn) {
  2358. ctx = task->perf_event_ctxp[ctxn];
  2359. if (likely(!ctx))
  2360. continue;
  2361. perf_event_context_sched_in(ctx, task);
  2362. }
  2363. if (atomic_read(&nr_switch_events))
  2364. perf_event_switch(task, prev, true);
  2365. if (__this_cpu_read(perf_sched_cb_usages))
  2366. perf_pmu_sched_task(prev, task, true);
  2367. }
  2368. static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
  2369. {
  2370. u64 frequency = event->attr.sample_freq;
  2371. u64 sec = NSEC_PER_SEC;
  2372. u64 divisor, dividend;
  2373. int count_fls, nsec_fls, frequency_fls, sec_fls;
  2374. count_fls = fls64(count);
  2375. nsec_fls = fls64(nsec);
  2376. frequency_fls = fls64(frequency);
  2377. sec_fls = 30;
  2378. /*
  2379. * We got @count in @nsec, with a target of sample_freq HZ
  2380. * the target period becomes:
  2381. *
  2382. * @count * 10^9
  2383. * period = -------------------
  2384. * @nsec * sample_freq
  2385. *
  2386. */
  2387. /*
  2388. * Reduce accuracy by one bit such that @a and @b converge
  2389. * to a similar magnitude.
  2390. */
  2391. #define REDUCE_FLS(a, b) \
  2392. do { \
  2393. if (a##_fls > b##_fls) { \
  2394. a >>= 1; \
  2395. a##_fls--; \
  2396. } else { \
  2397. b >>= 1; \
  2398. b##_fls--; \
  2399. } \
  2400. } while (0)
  2401. /*
  2402. * Reduce accuracy until either term fits in a u64, then proceed with
  2403. * the other, so that finally we can do a u64/u64 division.
  2404. */
  2405. while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
  2406. REDUCE_FLS(nsec, frequency);
  2407. REDUCE_FLS(sec, count);
  2408. }
  2409. if (count_fls + sec_fls > 64) {
  2410. divisor = nsec * frequency;
  2411. while (count_fls + sec_fls > 64) {
  2412. REDUCE_FLS(count, sec);
  2413. divisor >>= 1;
  2414. }
  2415. dividend = count * sec;
  2416. } else {
  2417. dividend = count * sec;
  2418. while (nsec_fls + frequency_fls > 64) {
  2419. REDUCE_FLS(nsec, frequency);
  2420. dividend >>= 1;
  2421. }
  2422. divisor = nsec * frequency;
  2423. }
  2424. if (!divisor)
  2425. return dividend;
  2426. return div64_u64(dividend, divisor);
  2427. }
  2428. static DEFINE_PER_CPU(int, perf_throttled_count);
  2429. static DEFINE_PER_CPU(u64, perf_throttled_seq);
  2430. static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
  2431. {
  2432. struct hw_perf_event *hwc = &event->hw;
  2433. s64 period, sample_period;
  2434. s64 delta;
  2435. period = perf_calculate_period(event, nsec, count);
  2436. delta = (s64)(period - hwc->sample_period);
  2437. delta = (delta + 7) / 8; /* low pass filter */
  2438. sample_period = hwc->sample_period + delta;
  2439. if (!sample_period)
  2440. sample_period = 1;
  2441. hwc->sample_period = sample_period;
  2442. if (local64_read(&hwc->period_left) > 8*sample_period) {
  2443. if (disable)
  2444. event->pmu->stop(event, PERF_EF_UPDATE);
  2445. local64_set(&hwc->period_left, 0);
  2446. if (disable)
  2447. event->pmu->start(event, PERF_EF_RELOAD);
  2448. }
  2449. }
  2450. /*
  2451. * combine freq adjustment with unthrottling to avoid two passes over the
  2452. * events. At the same time, make sure, having freq events does not change
  2453. * the rate of unthrottling as that would introduce bias.
  2454. */
  2455. static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
  2456. int needs_unthr)
  2457. {
  2458. struct perf_event *event;
  2459. struct hw_perf_event *hwc;
  2460. u64 now, period = TICK_NSEC;
  2461. s64 delta;
  2462. /*
  2463. * only need to iterate over all events iff:
  2464. * - context have events in frequency mode (needs freq adjust)
  2465. * - there are events to unthrottle on this cpu
  2466. */
  2467. if (!(ctx->nr_freq || needs_unthr))
  2468. return;
  2469. raw_spin_lock(&ctx->lock);
  2470. perf_pmu_disable(ctx->pmu);
  2471. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  2472. if (event->state != PERF_EVENT_STATE_ACTIVE)
  2473. continue;
  2474. if (!event_filter_match(event))
  2475. continue;
  2476. perf_pmu_disable(event->pmu);
  2477. hwc = &event->hw;
  2478. if (hwc->interrupts == MAX_INTERRUPTS) {
  2479. hwc->interrupts = 0;
  2480. perf_log_throttle(event, 1);
  2481. event->pmu->start(event, 0);
  2482. }
  2483. if (!event->attr.freq || !event->attr.sample_freq)
  2484. goto next;
  2485. /*
  2486. * stop the event and update event->count
  2487. */
  2488. event->pmu->stop(event, PERF_EF_UPDATE);
  2489. now = local64_read(&event->count);
  2490. delta = now - hwc->freq_count_stamp;
  2491. hwc->freq_count_stamp = now;
  2492. /*
  2493. * restart the event
  2494. * reload only if value has changed
  2495. * we have stopped the event so tell that
  2496. * to perf_adjust_period() to avoid stopping it
  2497. * twice.
  2498. */
  2499. if (delta > 0)
  2500. perf_adjust_period(event, period, delta, false);
  2501. event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
  2502. next:
  2503. perf_pmu_enable(event->pmu);
  2504. }
  2505. perf_pmu_enable(ctx->pmu);
  2506. raw_spin_unlock(&ctx->lock);
  2507. }
  2508. /*
  2509. * Round-robin a context's events:
  2510. */
  2511. static void rotate_ctx(struct perf_event_context *ctx)
  2512. {
  2513. /*
  2514. * Rotate the first entry last of non-pinned groups. Rotation might be
  2515. * disabled by the inheritance code.
  2516. */
  2517. if (!ctx->rotate_disable)
  2518. list_rotate_left(&ctx->flexible_groups);
  2519. }
  2520. static int perf_rotate_context(struct perf_cpu_context *cpuctx)
  2521. {
  2522. struct perf_event_context *ctx = NULL;
  2523. int rotate = 0;
  2524. if (cpuctx->ctx.nr_events) {
  2525. if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
  2526. rotate = 1;
  2527. }
  2528. ctx = cpuctx->task_ctx;
  2529. if (ctx && ctx->nr_events) {
  2530. if (ctx->nr_events != ctx->nr_active)
  2531. rotate = 1;
  2532. }
  2533. if (!rotate)
  2534. goto done;
  2535. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  2536. perf_pmu_disable(cpuctx->ctx.pmu);
  2537. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  2538. if (ctx)
  2539. ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
  2540. rotate_ctx(&cpuctx->ctx);
  2541. if (ctx)
  2542. rotate_ctx(ctx);
  2543. perf_event_sched_in(cpuctx, ctx, current);
  2544. perf_pmu_enable(cpuctx->ctx.pmu);
  2545. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  2546. done:
  2547. return rotate;
  2548. }
  2549. #ifdef CONFIG_NO_HZ_FULL
  2550. bool perf_event_can_stop_tick(void)
  2551. {
  2552. if (atomic_read(&nr_freq_events) ||
  2553. __this_cpu_read(perf_throttled_count))
  2554. return false;
  2555. else
  2556. return true;
  2557. }
  2558. #endif
  2559. void perf_event_task_tick(void)
  2560. {
  2561. struct list_head *head = this_cpu_ptr(&active_ctx_list);
  2562. struct perf_event_context *ctx, *tmp;
  2563. int throttled;
  2564. WARN_ON(!irqs_disabled());
  2565. __this_cpu_inc(perf_throttled_seq);
  2566. throttled = __this_cpu_xchg(perf_throttled_count, 0);
  2567. list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
  2568. perf_adjust_freq_unthr_context(ctx, throttled);
  2569. }
  2570. static int event_enable_on_exec(struct perf_event *event,
  2571. struct perf_event_context *ctx)
  2572. {
  2573. if (!event->attr.enable_on_exec)
  2574. return 0;
  2575. event->attr.enable_on_exec = 0;
  2576. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  2577. return 0;
  2578. __perf_event_mark_enabled(event);
  2579. return 1;
  2580. }
  2581. /*
  2582. * Enable all of a task's events that have been marked enable-on-exec.
  2583. * This expects task == current.
  2584. */
  2585. static void perf_event_enable_on_exec(int ctxn)
  2586. {
  2587. struct perf_event_context *ctx, *clone_ctx = NULL;
  2588. struct perf_cpu_context *cpuctx;
  2589. struct perf_event *event;
  2590. unsigned long flags;
  2591. int enabled = 0;
  2592. local_irq_save(flags);
  2593. ctx = current->perf_event_ctxp[ctxn];
  2594. if (!ctx || !ctx->nr_events)
  2595. goto out;
  2596. cpuctx = __get_cpu_context(ctx);
  2597. perf_ctx_lock(cpuctx, ctx);
  2598. list_for_each_entry(event, &ctx->event_list, event_entry)
  2599. enabled |= event_enable_on_exec(event, ctx);
  2600. /*
  2601. * Unclone and reschedule this context if we enabled any event.
  2602. */
  2603. if (enabled) {
  2604. clone_ctx = unclone_ctx(ctx);
  2605. ctx_resched(cpuctx, ctx);
  2606. }
  2607. perf_ctx_unlock(cpuctx, ctx);
  2608. out:
  2609. local_irq_restore(flags);
  2610. if (clone_ctx)
  2611. put_ctx(clone_ctx);
  2612. }
  2613. void perf_event_exec(void)
  2614. {
  2615. int ctxn;
  2616. rcu_read_lock();
  2617. for_each_task_context_nr(ctxn)
  2618. perf_event_enable_on_exec(ctxn);
  2619. rcu_read_unlock();
  2620. }
  2621. struct perf_read_data {
  2622. struct perf_event *event;
  2623. bool group;
  2624. int ret;
  2625. };
  2626. /*
  2627. * Cross CPU call to read the hardware event
  2628. */
  2629. static void __perf_event_read(void *info)
  2630. {
  2631. struct perf_read_data *data = info;
  2632. struct perf_event *sub, *event = data->event;
  2633. struct perf_event_context *ctx = event->ctx;
  2634. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  2635. struct pmu *pmu = event->pmu;
  2636. /*
  2637. * If this is a task context, we need to check whether it is
  2638. * the current task context of this cpu. If not it has been
  2639. * scheduled out before the smp call arrived. In that case
  2640. * event->count would have been updated to a recent sample
  2641. * when the event was scheduled out.
  2642. */
  2643. if (ctx->task && cpuctx->task_ctx != ctx)
  2644. return;
  2645. raw_spin_lock(&ctx->lock);
  2646. if (ctx->is_active) {
  2647. update_context_time(ctx);
  2648. update_cgrp_time_from_event(event);
  2649. }
  2650. update_event_times(event);
  2651. if (event->state != PERF_EVENT_STATE_ACTIVE)
  2652. goto unlock;
  2653. if (!data->group) {
  2654. pmu->read(event);
  2655. data->ret = 0;
  2656. goto unlock;
  2657. }
  2658. pmu->start_txn(pmu, PERF_PMU_TXN_READ);
  2659. pmu->read(event);
  2660. list_for_each_entry(sub, &event->sibling_list, group_entry) {
  2661. update_event_times(sub);
  2662. if (sub->state == PERF_EVENT_STATE_ACTIVE) {
  2663. /*
  2664. * Use sibling's PMU rather than @event's since
  2665. * sibling could be on different (eg: software) PMU.
  2666. */
  2667. sub->pmu->read(sub);
  2668. }
  2669. }
  2670. data->ret = pmu->commit_txn(pmu);
  2671. unlock:
  2672. raw_spin_unlock(&ctx->lock);
  2673. }
  2674. static inline u64 perf_event_count(struct perf_event *event)
  2675. {
  2676. if (event->pmu->count)
  2677. return event->pmu->count(event);
  2678. return __perf_event_count(event);
  2679. }
  2680. /*
  2681. * NMI-safe method to read a local event, that is an event that
  2682. * is:
  2683. * - either for the current task, or for this CPU
  2684. * - does not have inherit set, for inherited task events
  2685. * will not be local and we cannot read them atomically
  2686. * - must not have a pmu::count method
  2687. */
  2688. u64 perf_event_read_local(struct perf_event *event)
  2689. {
  2690. unsigned long flags;
  2691. u64 val;
  2692. /*
  2693. * Disabling interrupts avoids all counter scheduling (context
  2694. * switches, timer based rotation and IPIs).
  2695. */
  2696. local_irq_save(flags);
  2697. /* If this is a per-task event, it must be for current */
  2698. WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
  2699. event->hw.target != current);
  2700. /* If this is a per-CPU event, it must be for this CPU */
  2701. WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
  2702. event->cpu != smp_processor_id());
  2703. /*
  2704. * It must not be an event with inherit set, we cannot read
  2705. * all child counters from atomic context.
  2706. */
  2707. WARN_ON_ONCE(event->attr.inherit);
  2708. /*
  2709. * It must not have a pmu::count method, those are not
  2710. * NMI safe.
  2711. */
  2712. WARN_ON_ONCE(event->pmu->count);
  2713. /*
  2714. * If the event is currently on this CPU, its either a per-task event,
  2715. * or local to this CPU. Furthermore it means its ACTIVE (otherwise
  2716. * oncpu == -1).
  2717. */
  2718. if (event->oncpu == smp_processor_id())
  2719. event->pmu->read(event);
  2720. val = local64_read(&event->count);
  2721. local_irq_restore(flags);
  2722. return val;
  2723. }
  2724. static int perf_event_read(struct perf_event *event, bool group)
  2725. {
  2726. int ret = 0;
  2727. /*
  2728. * If event is enabled and currently active on a CPU, update the
  2729. * value in the event structure:
  2730. */
  2731. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  2732. struct perf_read_data data = {
  2733. .event = event,
  2734. .group = group,
  2735. .ret = 0,
  2736. };
  2737. smp_call_function_single(event->oncpu,
  2738. __perf_event_read, &data, 1);
  2739. ret = data.ret;
  2740. } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
  2741. struct perf_event_context *ctx = event->ctx;
  2742. unsigned long flags;
  2743. raw_spin_lock_irqsave(&ctx->lock, flags);
  2744. /*
  2745. * may read while context is not active
  2746. * (e.g., thread is blocked), in that case
  2747. * we cannot update context time
  2748. */
  2749. if (ctx->is_active) {
  2750. update_context_time(ctx);
  2751. update_cgrp_time_from_event(event);
  2752. }
  2753. if (group)
  2754. update_group_times(event);
  2755. else
  2756. update_event_times(event);
  2757. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  2758. }
  2759. return ret;
  2760. }
  2761. /*
  2762. * Initialize the perf_event context in a task_struct:
  2763. */
  2764. static void __perf_event_init_context(struct perf_event_context *ctx)
  2765. {
  2766. raw_spin_lock_init(&ctx->lock);
  2767. mutex_init(&ctx->mutex);
  2768. INIT_LIST_HEAD(&ctx->active_ctx_list);
  2769. INIT_LIST_HEAD(&ctx->pinned_groups);
  2770. INIT_LIST_HEAD(&ctx->flexible_groups);
  2771. INIT_LIST_HEAD(&ctx->event_list);
  2772. atomic_set(&ctx->refcount, 1);
  2773. }
  2774. static struct perf_event_context *
  2775. alloc_perf_context(struct pmu *pmu, struct task_struct *task)
  2776. {
  2777. struct perf_event_context *ctx;
  2778. ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
  2779. if (!ctx)
  2780. return NULL;
  2781. __perf_event_init_context(ctx);
  2782. if (task) {
  2783. ctx->task = task;
  2784. get_task_struct(task);
  2785. }
  2786. ctx->pmu = pmu;
  2787. return ctx;
  2788. }
  2789. static struct task_struct *
  2790. find_lively_task_by_vpid(pid_t vpid)
  2791. {
  2792. struct task_struct *task;
  2793. int err;
  2794. rcu_read_lock();
  2795. if (!vpid)
  2796. task = current;
  2797. else
  2798. task = find_task_by_vpid(vpid);
  2799. if (task)
  2800. get_task_struct(task);
  2801. rcu_read_unlock();
  2802. if (!task)
  2803. return ERR_PTR(-ESRCH);
  2804. /* Reuse ptrace permission checks for now. */
  2805. err = -EACCES;
  2806. if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
  2807. goto errout;
  2808. return task;
  2809. errout:
  2810. put_task_struct(task);
  2811. return ERR_PTR(err);
  2812. }
  2813. /*
  2814. * Returns a matching context with refcount and pincount.
  2815. */
  2816. static struct perf_event_context *
  2817. find_get_context(struct pmu *pmu, struct task_struct *task,
  2818. struct perf_event *event)
  2819. {
  2820. struct perf_event_context *ctx, *clone_ctx = NULL;
  2821. struct perf_cpu_context *cpuctx;
  2822. void *task_ctx_data = NULL;
  2823. unsigned long flags;
  2824. int ctxn, err;
  2825. int cpu = event->cpu;
  2826. if (!task) {
  2827. /* Must be root to operate on a CPU event: */
  2828. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  2829. return ERR_PTR(-EACCES);
  2830. /*
  2831. * We could be clever and allow to attach a event to an
  2832. * offline CPU and activate it when the CPU comes up, but
  2833. * that's for later.
  2834. */
  2835. if (!cpu_online(cpu))
  2836. return ERR_PTR(-ENODEV);
  2837. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  2838. ctx = &cpuctx->ctx;
  2839. get_ctx(ctx);
  2840. ++ctx->pin_count;
  2841. return ctx;
  2842. }
  2843. err = -EINVAL;
  2844. ctxn = pmu->task_ctx_nr;
  2845. if (ctxn < 0)
  2846. goto errout;
  2847. if (event->attach_state & PERF_ATTACH_TASK_DATA) {
  2848. task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
  2849. if (!task_ctx_data) {
  2850. err = -ENOMEM;
  2851. goto errout;
  2852. }
  2853. }
  2854. retry:
  2855. ctx = perf_lock_task_context(task, ctxn, &flags);
  2856. if (ctx) {
  2857. clone_ctx = unclone_ctx(ctx);
  2858. ++ctx->pin_count;
  2859. if (task_ctx_data && !ctx->task_ctx_data) {
  2860. ctx->task_ctx_data = task_ctx_data;
  2861. task_ctx_data = NULL;
  2862. }
  2863. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  2864. if (clone_ctx)
  2865. put_ctx(clone_ctx);
  2866. } else {
  2867. ctx = alloc_perf_context(pmu, task);
  2868. err = -ENOMEM;
  2869. if (!ctx)
  2870. goto errout;
  2871. if (task_ctx_data) {
  2872. ctx->task_ctx_data = task_ctx_data;
  2873. task_ctx_data = NULL;
  2874. }
  2875. err = 0;
  2876. mutex_lock(&task->perf_event_mutex);
  2877. /*
  2878. * If it has already passed perf_event_exit_task().
  2879. * we must see PF_EXITING, it takes this mutex too.
  2880. */
  2881. if (task->flags & PF_EXITING)
  2882. err = -ESRCH;
  2883. else if (task->perf_event_ctxp[ctxn])
  2884. err = -EAGAIN;
  2885. else {
  2886. get_ctx(ctx);
  2887. ++ctx->pin_count;
  2888. rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
  2889. }
  2890. mutex_unlock(&task->perf_event_mutex);
  2891. if (unlikely(err)) {
  2892. put_ctx(ctx);
  2893. if (err == -EAGAIN)
  2894. goto retry;
  2895. goto errout;
  2896. }
  2897. }
  2898. kfree(task_ctx_data);
  2899. return ctx;
  2900. errout:
  2901. kfree(task_ctx_data);
  2902. return ERR_PTR(err);
  2903. }
  2904. static void perf_event_free_filter(struct perf_event *event);
  2905. static void perf_event_free_bpf_prog(struct perf_event *event);
  2906. static void free_event_rcu(struct rcu_head *head)
  2907. {
  2908. struct perf_event *event;
  2909. event = container_of(head, struct perf_event, rcu_head);
  2910. if (event->ns)
  2911. put_pid_ns(event->ns);
  2912. perf_event_free_filter(event);
  2913. kfree(event);
  2914. }
  2915. static void ring_buffer_attach(struct perf_event *event,
  2916. struct ring_buffer *rb);
  2917. static void unaccount_event_cpu(struct perf_event *event, int cpu)
  2918. {
  2919. if (event->parent)
  2920. return;
  2921. if (is_cgroup_event(event))
  2922. atomic_dec(&per_cpu(perf_cgroup_events, cpu));
  2923. }
  2924. static void unaccount_event(struct perf_event *event)
  2925. {
  2926. bool dec = false;
  2927. if (event->parent)
  2928. return;
  2929. if (event->attach_state & PERF_ATTACH_TASK)
  2930. dec = true;
  2931. if (event->attr.mmap || event->attr.mmap_data)
  2932. atomic_dec(&nr_mmap_events);
  2933. if (event->attr.comm)
  2934. atomic_dec(&nr_comm_events);
  2935. if (event->attr.task)
  2936. atomic_dec(&nr_task_events);
  2937. if (event->attr.freq)
  2938. atomic_dec(&nr_freq_events);
  2939. if (event->attr.context_switch) {
  2940. dec = true;
  2941. atomic_dec(&nr_switch_events);
  2942. }
  2943. if (is_cgroup_event(event))
  2944. dec = true;
  2945. if (has_branch_stack(event))
  2946. dec = true;
  2947. if (dec)
  2948. static_key_slow_dec_deferred(&perf_sched_events);
  2949. unaccount_event_cpu(event, event->cpu);
  2950. }
  2951. /*
  2952. * The following implement mutual exclusion of events on "exclusive" pmus
  2953. * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
  2954. * at a time, so we disallow creating events that might conflict, namely:
  2955. *
  2956. * 1) cpu-wide events in the presence of per-task events,
  2957. * 2) per-task events in the presence of cpu-wide events,
  2958. * 3) two matching events on the same context.
  2959. *
  2960. * The former two cases are handled in the allocation path (perf_event_alloc(),
  2961. * _free_event()), the latter -- before the first perf_install_in_context().
  2962. */
  2963. static int exclusive_event_init(struct perf_event *event)
  2964. {
  2965. struct pmu *pmu = event->pmu;
  2966. if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
  2967. return 0;
  2968. /*
  2969. * Prevent co-existence of per-task and cpu-wide events on the
  2970. * same exclusive pmu.
  2971. *
  2972. * Negative pmu::exclusive_cnt means there are cpu-wide
  2973. * events on this "exclusive" pmu, positive means there are
  2974. * per-task events.
  2975. *
  2976. * Since this is called in perf_event_alloc() path, event::ctx
  2977. * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
  2978. * to mean "per-task event", because unlike other attach states it
  2979. * never gets cleared.
  2980. */
  2981. if (event->attach_state & PERF_ATTACH_TASK) {
  2982. if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
  2983. return -EBUSY;
  2984. } else {
  2985. if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
  2986. return -EBUSY;
  2987. }
  2988. return 0;
  2989. }
  2990. static void exclusive_event_destroy(struct perf_event *event)
  2991. {
  2992. struct pmu *pmu = event->pmu;
  2993. if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
  2994. return;
  2995. /* see comment in exclusive_event_init() */
  2996. if (event->attach_state & PERF_ATTACH_TASK)
  2997. atomic_dec(&pmu->exclusive_cnt);
  2998. else
  2999. atomic_inc(&pmu->exclusive_cnt);
  3000. }
  3001. static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
  3002. {
  3003. if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) &&
  3004. (e1->cpu == e2->cpu ||
  3005. e1->cpu == -1 ||
  3006. e2->cpu == -1))
  3007. return true;
  3008. return false;
  3009. }
  3010. /* Called under the same ctx::mutex as perf_install_in_context() */
  3011. static bool exclusive_event_installable(struct perf_event *event,
  3012. struct perf_event_context *ctx)
  3013. {
  3014. struct perf_event *iter_event;
  3015. struct pmu *pmu = event->pmu;
  3016. if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
  3017. return true;
  3018. list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
  3019. if (exclusive_event_match(iter_event, event))
  3020. return false;
  3021. }
  3022. return true;
  3023. }
  3024. static void _free_event(struct perf_event *event)
  3025. {
  3026. irq_work_sync(&event->pending);
  3027. unaccount_event(event);
  3028. if (event->rb) {
  3029. /*
  3030. * Can happen when we close an event with re-directed output.
  3031. *
  3032. * Since we have a 0 refcount, perf_mmap_close() will skip
  3033. * over us; possibly making our ring_buffer_put() the last.
  3034. */
  3035. mutex_lock(&event->mmap_mutex);
  3036. ring_buffer_attach(event, NULL);
  3037. mutex_unlock(&event->mmap_mutex);
  3038. }
  3039. if (is_cgroup_event(event))
  3040. perf_detach_cgroup(event);
  3041. if (!event->parent) {
  3042. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
  3043. put_callchain_buffers();
  3044. }
  3045. perf_event_free_bpf_prog(event);
  3046. if (event->destroy)
  3047. event->destroy(event);
  3048. if (event->ctx)
  3049. put_ctx(event->ctx);
  3050. if (event->pmu) {
  3051. exclusive_event_destroy(event);
  3052. module_put(event->pmu->module);
  3053. }
  3054. call_rcu(&event->rcu_head, free_event_rcu);
  3055. }
  3056. /*
  3057. * Used to free events which have a known refcount of 1, such as in error paths
  3058. * where the event isn't exposed yet and inherited events.
  3059. */
  3060. static void free_event(struct perf_event *event)
  3061. {
  3062. if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
  3063. "unexpected event refcount: %ld; ptr=%p\n",
  3064. atomic_long_read(&event->refcount), event)) {
  3065. /* leak to avoid use-after-free */
  3066. return;
  3067. }
  3068. _free_event(event);
  3069. }
  3070. /*
  3071. * Remove user event from the owner task.
  3072. */
  3073. static void perf_remove_from_owner(struct perf_event *event)
  3074. {
  3075. struct task_struct *owner;
  3076. rcu_read_lock();
  3077. /*
  3078. * Matches the smp_store_release() in perf_event_exit_task(). If we
  3079. * observe !owner it means the list deletion is complete and we can
  3080. * indeed free this event, otherwise we need to serialize on
  3081. * owner->perf_event_mutex.
  3082. */
  3083. owner = lockless_dereference(event->owner);
  3084. if (owner) {
  3085. /*
  3086. * Since delayed_put_task_struct() also drops the last
  3087. * task reference we can safely take a new reference
  3088. * while holding the rcu_read_lock().
  3089. */
  3090. get_task_struct(owner);
  3091. }
  3092. rcu_read_unlock();
  3093. if (owner) {
  3094. /*
  3095. * If we're here through perf_event_exit_task() we're already
  3096. * holding ctx->mutex which would be an inversion wrt. the
  3097. * normal lock order.
  3098. *
  3099. * However we can safely take this lock because its the child
  3100. * ctx->mutex.
  3101. */
  3102. mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
  3103. /*
  3104. * We have to re-check the event->owner field, if it is cleared
  3105. * we raced with perf_event_exit_task(), acquiring the mutex
  3106. * ensured they're done, and we can proceed with freeing the
  3107. * event.
  3108. */
  3109. if (event->owner) {
  3110. list_del_init(&event->owner_entry);
  3111. smp_store_release(&event->owner, NULL);
  3112. }
  3113. mutex_unlock(&owner->perf_event_mutex);
  3114. put_task_struct(owner);
  3115. }
  3116. }
  3117. static void put_event(struct perf_event *event)
  3118. {
  3119. if (!atomic_long_dec_and_test(&event->refcount))
  3120. return;
  3121. _free_event(event);
  3122. }
  3123. /*
  3124. * Kill an event dead; while event:refcount will preserve the event
  3125. * object, it will not preserve its functionality. Once the last 'user'
  3126. * gives up the object, we'll destroy the thing.
  3127. */
  3128. int perf_event_release_kernel(struct perf_event *event)
  3129. {
  3130. struct perf_event_context *ctx;
  3131. struct perf_event *child, *tmp;
  3132. if (!is_kernel_event(event))
  3133. perf_remove_from_owner(event);
  3134. ctx = perf_event_ctx_lock(event);
  3135. WARN_ON_ONCE(ctx->parent_ctx);
  3136. perf_remove_from_context(event, DETACH_GROUP | DETACH_STATE);
  3137. perf_event_ctx_unlock(event, ctx);
  3138. /*
  3139. * At this point we must have event->state == PERF_EVENT_STATE_EXIT,
  3140. * either from the above perf_remove_from_context() or through
  3141. * perf_event_exit_event().
  3142. *
  3143. * Therefore, anybody acquiring event->child_mutex after the below
  3144. * loop _must_ also see this, most importantly inherit_event() which
  3145. * will avoid placing more children on the list.
  3146. *
  3147. * Thus this guarantees that we will in fact observe and kill _ALL_
  3148. * child events.
  3149. */
  3150. WARN_ON_ONCE(event->state != PERF_EVENT_STATE_EXIT);
  3151. again:
  3152. mutex_lock(&event->child_mutex);
  3153. list_for_each_entry(child, &event->child_list, child_list) {
  3154. /*
  3155. * Cannot change, child events are not migrated, see the
  3156. * comment with perf_event_ctx_lock_nested().
  3157. */
  3158. ctx = lockless_dereference(child->ctx);
  3159. /*
  3160. * Since child_mutex nests inside ctx::mutex, we must jump
  3161. * through hoops. We start by grabbing a reference on the ctx.
  3162. *
  3163. * Since the event cannot get freed while we hold the
  3164. * child_mutex, the context must also exist and have a !0
  3165. * reference count.
  3166. */
  3167. get_ctx(ctx);
  3168. /*
  3169. * Now that we have a ctx ref, we can drop child_mutex, and
  3170. * acquire ctx::mutex without fear of it going away. Then we
  3171. * can re-acquire child_mutex.
  3172. */
  3173. mutex_unlock(&event->child_mutex);
  3174. mutex_lock(&ctx->mutex);
  3175. mutex_lock(&event->child_mutex);
  3176. /*
  3177. * Now that we hold ctx::mutex and child_mutex, revalidate our
  3178. * state, if child is still the first entry, it didn't get freed
  3179. * and we can continue doing so.
  3180. */
  3181. tmp = list_first_entry_or_null(&event->child_list,
  3182. struct perf_event, child_list);
  3183. if (tmp == child) {
  3184. perf_remove_from_context(child, DETACH_GROUP);
  3185. list_del(&child->child_list);
  3186. free_event(child);
  3187. /*
  3188. * This matches the refcount bump in inherit_event();
  3189. * this can't be the last reference.
  3190. */
  3191. put_event(event);
  3192. }
  3193. mutex_unlock(&event->child_mutex);
  3194. mutex_unlock(&ctx->mutex);
  3195. put_ctx(ctx);
  3196. goto again;
  3197. }
  3198. mutex_unlock(&event->child_mutex);
  3199. /* Must be the last reference */
  3200. put_event(event);
  3201. return 0;
  3202. }
  3203. EXPORT_SYMBOL_GPL(perf_event_release_kernel);
  3204. /*
  3205. * Called when the last reference to the file is gone.
  3206. */
  3207. static int perf_release(struct inode *inode, struct file *file)
  3208. {
  3209. perf_event_release_kernel(file->private_data);
  3210. return 0;
  3211. }
  3212. u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
  3213. {
  3214. struct perf_event *child;
  3215. u64 total = 0;
  3216. *enabled = 0;
  3217. *running = 0;
  3218. mutex_lock(&event->child_mutex);
  3219. (void)perf_event_read(event, false);
  3220. total += perf_event_count(event);
  3221. *enabled += event->total_time_enabled +
  3222. atomic64_read(&event->child_total_time_enabled);
  3223. *running += event->total_time_running +
  3224. atomic64_read(&event->child_total_time_running);
  3225. list_for_each_entry(child, &event->child_list, child_list) {
  3226. (void)perf_event_read(child, false);
  3227. total += perf_event_count(child);
  3228. *enabled += child->total_time_enabled;
  3229. *running += child->total_time_running;
  3230. }
  3231. mutex_unlock(&event->child_mutex);
  3232. return total;
  3233. }
  3234. EXPORT_SYMBOL_GPL(perf_event_read_value);
  3235. static int __perf_read_group_add(struct perf_event *leader,
  3236. u64 read_format, u64 *values)
  3237. {
  3238. struct perf_event *sub;
  3239. int n = 1; /* skip @nr */
  3240. int ret;
  3241. ret = perf_event_read(leader, true);
  3242. if (ret)
  3243. return ret;
  3244. /*
  3245. * Since we co-schedule groups, {enabled,running} times of siblings
  3246. * will be identical to those of the leader, so we only publish one
  3247. * set.
  3248. */
  3249. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  3250. values[n++] += leader->total_time_enabled +
  3251. atomic64_read(&leader->child_total_time_enabled);
  3252. }
  3253. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  3254. values[n++] += leader->total_time_running +
  3255. atomic64_read(&leader->child_total_time_running);
  3256. }
  3257. /*
  3258. * Write {count,id} tuples for every sibling.
  3259. */
  3260. values[n++] += perf_event_count(leader);
  3261. if (read_format & PERF_FORMAT_ID)
  3262. values[n++] = primary_event_id(leader);
  3263. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  3264. values[n++] += perf_event_count(sub);
  3265. if (read_format & PERF_FORMAT_ID)
  3266. values[n++] = primary_event_id(sub);
  3267. }
  3268. return 0;
  3269. }
  3270. static int perf_read_group(struct perf_event *event,
  3271. u64 read_format, char __user *buf)
  3272. {
  3273. struct perf_event *leader = event->group_leader, *child;
  3274. struct perf_event_context *ctx = leader->ctx;
  3275. int ret;
  3276. u64 *values;
  3277. lockdep_assert_held(&ctx->mutex);
  3278. values = kzalloc(event->read_size, GFP_KERNEL);
  3279. if (!values)
  3280. return -ENOMEM;
  3281. values[0] = 1 + leader->nr_siblings;
  3282. /*
  3283. * By locking the child_mutex of the leader we effectively
  3284. * lock the child list of all siblings.. XXX explain how.
  3285. */
  3286. mutex_lock(&leader->child_mutex);
  3287. ret = __perf_read_group_add(leader, read_format, values);
  3288. if (ret)
  3289. goto unlock;
  3290. list_for_each_entry(child, &leader->child_list, child_list) {
  3291. ret = __perf_read_group_add(child, read_format, values);
  3292. if (ret)
  3293. goto unlock;
  3294. }
  3295. mutex_unlock(&leader->child_mutex);
  3296. ret = event->read_size;
  3297. if (copy_to_user(buf, values, event->read_size))
  3298. ret = -EFAULT;
  3299. goto out;
  3300. unlock:
  3301. mutex_unlock(&leader->child_mutex);
  3302. out:
  3303. kfree(values);
  3304. return ret;
  3305. }
  3306. static int perf_read_one(struct perf_event *event,
  3307. u64 read_format, char __user *buf)
  3308. {
  3309. u64 enabled, running;
  3310. u64 values[4];
  3311. int n = 0;
  3312. values[n++] = perf_event_read_value(event, &enabled, &running);
  3313. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  3314. values[n++] = enabled;
  3315. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  3316. values[n++] = running;
  3317. if (read_format & PERF_FORMAT_ID)
  3318. values[n++] = primary_event_id(event);
  3319. if (copy_to_user(buf, values, n * sizeof(u64)))
  3320. return -EFAULT;
  3321. return n * sizeof(u64);
  3322. }
  3323. static bool is_event_hup(struct perf_event *event)
  3324. {
  3325. bool no_children;
  3326. if (event->state != PERF_EVENT_STATE_EXIT)
  3327. return false;
  3328. mutex_lock(&event->child_mutex);
  3329. no_children = list_empty(&event->child_list);
  3330. mutex_unlock(&event->child_mutex);
  3331. return no_children;
  3332. }
  3333. /*
  3334. * Read the performance event - simple non blocking version for now
  3335. */
  3336. static ssize_t
  3337. __perf_read(struct perf_event *event, char __user *buf, size_t count)
  3338. {
  3339. u64 read_format = event->attr.read_format;
  3340. int ret;
  3341. /*
  3342. * Return end-of-file for a read on a event that is in
  3343. * error state (i.e. because it was pinned but it couldn't be
  3344. * scheduled on to the CPU at some point).
  3345. */
  3346. if (event->state == PERF_EVENT_STATE_ERROR)
  3347. return 0;
  3348. if (count < event->read_size)
  3349. return -ENOSPC;
  3350. WARN_ON_ONCE(event->ctx->parent_ctx);
  3351. if (read_format & PERF_FORMAT_GROUP)
  3352. ret = perf_read_group(event, read_format, buf);
  3353. else
  3354. ret = perf_read_one(event, read_format, buf);
  3355. return ret;
  3356. }
  3357. static ssize_t
  3358. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  3359. {
  3360. struct perf_event *event = file->private_data;
  3361. struct perf_event_context *ctx;
  3362. int ret;
  3363. ctx = perf_event_ctx_lock(event);
  3364. ret = __perf_read(event, buf, count);
  3365. perf_event_ctx_unlock(event, ctx);
  3366. return ret;
  3367. }
  3368. static unsigned int perf_poll(struct file *file, poll_table *wait)
  3369. {
  3370. struct perf_event *event = file->private_data;
  3371. struct ring_buffer *rb;
  3372. unsigned int events = POLLHUP;
  3373. poll_wait(file, &event->waitq, wait);
  3374. if (is_event_hup(event))
  3375. return events;
  3376. /*
  3377. * Pin the event->rb by taking event->mmap_mutex; otherwise
  3378. * perf_event_set_output() can swizzle our rb and make us miss wakeups.
  3379. */
  3380. mutex_lock(&event->mmap_mutex);
  3381. rb = event->rb;
  3382. if (rb)
  3383. events = atomic_xchg(&rb->poll, 0);
  3384. mutex_unlock(&event->mmap_mutex);
  3385. return events;
  3386. }
  3387. static void _perf_event_reset(struct perf_event *event)
  3388. {
  3389. (void)perf_event_read(event, false);
  3390. local64_set(&event->count, 0);
  3391. perf_event_update_userpage(event);
  3392. }
  3393. /*
  3394. * Holding the top-level event's child_mutex means that any
  3395. * descendant process that has inherited this event will block
  3396. * in perf_event_exit_event() if it goes to exit, thus satisfying the
  3397. * task existence requirements of perf_event_enable/disable.
  3398. */
  3399. static void perf_event_for_each_child(struct perf_event *event,
  3400. void (*func)(struct perf_event *))
  3401. {
  3402. struct perf_event *child;
  3403. WARN_ON_ONCE(event->ctx->parent_ctx);
  3404. mutex_lock(&event->child_mutex);
  3405. func(event);
  3406. list_for_each_entry(child, &event->child_list, child_list)
  3407. func(child);
  3408. mutex_unlock(&event->child_mutex);
  3409. }
  3410. static void perf_event_for_each(struct perf_event *event,
  3411. void (*func)(struct perf_event *))
  3412. {
  3413. struct perf_event_context *ctx = event->ctx;
  3414. struct perf_event *sibling;
  3415. lockdep_assert_held(&ctx->mutex);
  3416. event = event->group_leader;
  3417. perf_event_for_each_child(event, func);
  3418. list_for_each_entry(sibling, &event->sibling_list, group_entry)
  3419. perf_event_for_each_child(sibling, func);
  3420. }
  3421. static void __perf_event_period(struct perf_event *event,
  3422. struct perf_cpu_context *cpuctx,
  3423. struct perf_event_context *ctx,
  3424. void *info)
  3425. {
  3426. u64 value = *((u64 *)info);
  3427. bool active;
  3428. if (event->attr.freq) {
  3429. event->attr.sample_freq = value;
  3430. } else {
  3431. event->attr.sample_period = value;
  3432. event->hw.sample_period = value;
  3433. }
  3434. active = (event->state == PERF_EVENT_STATE_ACTIVE);
  3435. if (active) {
  3436. perf_pmu_disable(ctx->pmu);
  3437. event->pmu->stop(event, PERF_EF_UPDATE);
  3438. }
  3439. local64_set(&event->hw.period_left, 0);
  3440. if (active) {
  3441. event->pmu->start(event, PERF_EF_RELOAD);
  3442. perf_pmu_enable(ctx->pmu);
  3443. }
  3444. }
  3445. static int perf_event_period(struct perf_event *event, u64 __user *arg)
  3446. {
  3447. u64 value;
  3448. if (!is_sampling_event(event))
  3449. return -EINVAL;
  3450. if (copy_from_user(&value, arg, sizeof(value)))
  3451. return -EFAULT;
  3452. if (!value)
  3453. return -EINVAL;
  3454. if (event->attr.freq && value > sysctl_perf_event_sample_rate)
  3455. return -EINVAL;
  3456. event_function_call(event, __perf_event_period, &value);
  3457. return 0;
  3458. }
  3459. static const struct file_operations perf_fops;
  3460. static inline int perf_fget_light(int fd, struct fd *p)
  3461. {
  3462. struct fd f = fdget(fd);
  3463. if (!f.file)
  3464. return -EBADF;
  3465. if (f.file->f_op != &perf_fops) {
  3466. fdput(f);
  3467. return -EBADF;
  3468. }
  3469. *p = f;
  3470. return 0;
  3471. }
  3472. static int perf_event_set_output(struct perf_event *event,
  3473. struct perf_event *output_event);
  3474. static int perf_event_set_filter(struct perf_event *event, void __user *arg);
  3475. static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
  3476. static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
  3477. {
  3478. void (*func)(struct perf_event *);
  3479. u32 flags = arg;
  3480. switch (cmd) {
  3481. case PERF_EVENT_IOC_ENABLE:
  3482. func = _perf_event_enable;
  3483. break;
  3484. case PERF_EVENT_IOC_DISABLE:
  3485. func = _perf_event_disable;
  3486. break;
  3487. case PERF_EVENT_IOC_RESET:
  3488. func = _perf_event_reset;
  3489. break;
  3490. case PERF_EVENT_IOC_REFRESH:
  3491. return _perf_event_refresh(event, arg);
  3492. case PERF_EVENT_IOC_PERIOD:
  3493. return perf_event_period(event, (u64 __user *)arg);
  3494. case PERF_EVENT_IOC_ID:
  3495. {
  3496. u64 id = primary_event_id(event);
  3497. if (copy_to_user((void __user *)arg, &id, sizeof(id)))
  3498. return -EFAULT;
  3499. return 0;
  3500. }
  3501. case PERF_EVENT_IOC_SET_OUTPUT:
  3502. {
  3503. int ret;
  3504. if (arg != -1) {
  3505. struct perf_event *output_event;
  3506. struct fd output;
  3507. ret = perf_fget_light(arg, &output);
  3508. if (ret)
  3509. return ret;
  3510. output_event = output.file->private_data;
  3511. ret = perf_event_set_output(event, output_event);
  3512. fdput(output);
  3513. } else {
  3514. ret = perf_event_set_output(event, NULL);
  3515. }
  3516. return ret;
  3517. }
  3518. case PERF_EVENT_IOC_SET_FILTER:
  3519. return perf_event_set_filter(event, (void __user *)arg);
  3520. case PERF_EVENT_IOC_SET_BPF:
  3521. return perf_event_set_bpf_prog(event, arg);
  3522. default:
  3523. return -ENOTTY;
  3524. }
  3525. if (flags & PERF_IOC_FLAG_GROUP)
  3526. perf_event_for_each(event, func);
  3527. else
  3528. perf_event_for_each_child(event, func);
  3529. return 0;
  3530. }
  3531. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  3532. {
  3533. struct perf_event *event = file->private_data;
  3534. struct perf_event_context *ctx;
  3535. long ret;
  3536. ctx = perf_event_ctx_lock(event);
  3537. ret = _perf_ioctl(event, cmd, arg);
  3538. perf_event_ctx_unlock(event, ctx);
  3539. return ret;
  3540. }
  3541. #ifdef CONFIG_COMPAT
  3542. static long perf_compat_ioctl(struct file *file, unsigned int cmd,
  3543. unsigned long arg)
  3544. {
  3545. switch (_IOC_NR(cmd)) {
  3546. case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
  3547. case _IOC_NR(PERF_EVENT_IOC_ID):
  3548. /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
  3549. if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
  3550. cmd &= ~IOCSIZE_MASK;
  3551. cmd |= sizeof(void *) << IOCSIZE_SHIFT;
  3552. }
  3553. break;
  3554. }
  3555. return perf_ioctl(file, cmd, arg);
  3556. }
  3557. #else
  3558. # define perf_compat_ioctl NULL
  3559. #endif
  3560. int perf_event_task_enable(void)
  3561. {
  3562. struct perf_event_context *ctx;
  3563. struct perf_event *event;
  3564. mutex_lock(&current->perf_event_mutex);
  3565. list_for_each_entry(event, &current->perf_event_list, owner_entry) {
  3566. ctx = perf_event_ctx_lock(event);
  3567. perf_event_for_each_child(event, _perf_event_enable);
  3568. perf_event_ctx_unlock(event, ctx);
  3569. }
  3570. mutex_unlock(&current->perf_event_mutex);
  3571. return 0;
  3572. }
  3573. int perf_event_task_disable(void)
  3574. {
  3575. struct perf_event_context *ctx;
  3576. struct perf_event *event;
  3577. mutex_lock(&current->perf_event_mutex);
  3578. list_for_each_entry(event, &current->perf_event_list, owner_entry) {
  3579. ctx = perf_event_ctx_lock(event);
  3580. perf_event_for_each_child(event, _perf_event_disable);
  3581. perf_event_ctx_unlock(event, ctx);
  3582. }
  3583. mutex_unlock(&current->perf_event_mutex);
  3584. return 0;
  3585. }
  3586. static int perf_event_index(struct perf_event *event)
  3587. {
  3588. if (event->hw.state & PERF_HES_STOPPED)
  3589. return 0;
  3590. if (event->state != PERF_EVENT_STATE_ACTIVE)
  3591. return 0;
  3592. return event->pmu->event_idx(event);
  3593. }
  3594. static void calc_timer_values(struct perf_event *event,
  3595. u64 *now,
  3596. u64 *enabled,
  3597. u64 *running)
  3598. {
  3599. u64 ctx_time;
  3600. *now = perf_clock();
  3601. ctx_time = event->shadow_ctx_time + *now;
  3602. *enabled = ctx_time - event->tstamp_enabled;
  3603. *running = ctx_time - event->tstamp_running;
  3604. }
  3605. static void perf_event_init_userpage(struct perf_event *event)
  3606. {
  3607. struct perf_event_mmap_page *userpg;
  3608. struct ring_buffer *rb;
  3609. rcu_read_lock();
  3610. rb = rcu_dereference(event->rb);
  3611. if (!rb)
  3612. goto unlock;
  3613. userpg = rb->user_page;
  3614. /* Allow new userspace to detect that bit 0 is deprecated */
  3615. userpg->cap_bit0_is_deprecated = 1;
  3616. userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
  3617. userpg->data_offset = PAGE_SIZE;
  3618. userpg->data_size = perf_data_size(rb);
  3619. unlock:
  3620. rcu_read_unlock();
  3621. }
  3622. void __weak arch_perf_update_userpage(
  3623. struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
  3624. {
  3625. }
  3626. /*
  3627. * Callers need to ensure there can be no nesting of this function, otherwise
  3628. * the seqlock logic goes bad. We can not serialize this because the arch
  3629. * code calls this from NMI context.
  3630. */
  3631. void perf_event_update_userpage(struct perf_event *event)
  3632. {
  3633. struct perf_event_mmap_page *userpg;
  3634. struct ring_buffer *rb;
  3635. u64 enabled, running, now;
  3636. rcu_read_lock();
  3637. rb = rcu_dereference(event->rb);
  3638. if (!rb)
  3639. goto unlock;
  3640. /*
  3641. * compute total_time_enabled, total_time_running
  3642. * based on snapshot values taken when the event
  3643. * was last scheduled in.
  3644. *
  3645. * we cannot simply called update_context_time()
  3646. * because of locking issue as we can be called in
  3647. * NMI context
  3648. */
  3649. calc_timer_values(event, &now, &enabled, &running);
  3650. userpg = rb->user_page;
  3651. /*
  3652. * Disable preemption so as to not let the corresponding user-space
  3653. * spin too long if we get preempted.
  3654. */
  3655. preempt_disable();
  3656. ++userpg->lock;
  3657. barrier();
  3658. userpg->index = perf_event_index(event);
  3659. userpg->offset = perf_event_count(event);
  3660. if (userpg->index)
  3661. userpg->offset -= local64_read(&event->hw.prev_count);
  3662. userpg->time_enabled = enabled +
  3663. atomic64_read(&event->child_total_time_enabled);
  3664. userpg->time_running = running +
  3665. atomic64_read(&event->child_total_time_running);
  3666. arch_perf_update_userpage(event, userpg, now);
  3667. barrier();
  3668. ++userpg->lock;
  3669. preempt_enable();
  3670. unlock:
  3671. rcu_read_unlock();
  3672. }
  3673. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  3674. {
  3675. struct perf_event *event = vma->vm_file->private_data;
  3676. struct ring_buffer *rb;
  3677. int ret = VM_FAULT_SIGBUS;
  3678. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  3679. if (vmf->pgoff == 0)
  3680. ret = 0;
  3681. return ret;
  3682. }
  3683. rcu_read_lock();
  3684. rb = rcu_dereference(event->rb);
  3685. if (!rb)
  3686. goto unlock;
  3687. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  3688. goto unlock;
  3689. vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
  3690. if (!vmf->page)
  3691. goto unlock;
  3692. get_page(vmf->page);
  3693. vmf->page->mapping = vma->vm_file->f_mapping;
  3694. vmf->page->index = vmf->pgoff;
  3695. ret = 0;
  3696. unlock:
  3697. rcu_read_unlock();
  3698. return ret;
  3699. }
  3700. static void ring_buffer_attach(struct perf_event *event,
  3701. struct ring_buffer *rb)
  3702. {
  3703. struct ring_buffer *old_rb = NULL;
  3704. unsigned long flags;
  3705. if (event->rb) {
  3706. /*
  3707. * Should be impossible, we set this when removing
  3708. * event->rb_entry and wait/clear when adding event->rb_entry.
  3709. */
  3710. WARN_ON_ONCE(event->rcu_pending);
  3711. old_rb = event->rb;
  3712. spin_lock_irqsave(&old_rb->event_lock, flags);
  3713. list_del_rcu(&event->rb_entry);
  3714. spin_unlock_irqrestore(&old_rb->event_lock, flags);
  3715. event->rcu_batches = get_state_synchronize_rcu();
  3716. event->rcu_pending = 1;
  3717. }
  3718. if (rb) {
  3719. if (event->rcu_pending) {
  3720. cond_synchronize_rcu(event->rcu_batches);
  3721. event->rcu_pending = 0;
  3722. }
  3723. spin_lock_irqsave(&rb->event_lock, flags);
  3724. list_add_rcu(&event->rb_entry, &rb->event_list);
  3725. spin_unlock_irqrestore(&rb->event_lock, flags);
  3726. }
  3727. rcu_assign_pointer(event->rb, rb);
  3728. if (old_rb) {
  3729. ring_buffer_put(old_rb);
  3730. /*
  3731. * Since we detached before setting the new rb, so that we
  3732. * could attach the new rb, we could have missed a wakeup.
  3733. * Provide it now.
  3734. */
  3735. wake_up_all(&event->waitq);
  3736. }
  3737. }
  3738. static void ring_buffer_wakeup(struct perf_event *event)
  3739. {
  3740. struct ring_buffer *rb;
  3741. rcu_read_lock();
  3742. rb = rcu_dereference(event->rb);
  3743. if (rb) {
  3744. list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
  3745. wake_up_all(&event->waitq);
  3746. }
  3747. rcu_read_unlock();
  3748. }
  3749. struct ring_buffer *ring_buffer_get(struct perf_event *event)
  3750. {
  3751. struct ring_buffer *rb;
  3752. rcu_read_lock();
  3753. rb = rcu_dereference(event->rb);
  3754. if (rb) {
  3755. if (!atomic_inc_not_zero(&rb->refcount))
  3756. rb = NULL;
  3757. }
  3758. rcu_read_unlock();
  3759. return rb;
  3760. }
  3761. void ring_buffer_put(struct ring_buffer *rb)
  3762. {
  3763. if (!atomic_dec_and_test(&rb->refcount))
  3764. return;
  3765. WARN_ON_ONCE(!list_empty(&rb->event_list));
  3766. call_rcu(&rb->rcu_head, rb_free_rcu);
  3767. }
  3768. static void perf_mmap_open(struct vm_area_struct *vma)
  3769. {
  3770. struct perf_event *event = vma->vm_file->private_data;
  3771. atomic_inc(&event->mmap_count);
  3772. atomic_inc(&event->rb->mmap_count);
  3773. if (vma->vm_pgoff)
  3774. atomic_inc(&event->rb->aux_mmap_count);
  3775. if (event->pmu->event_mapped)
  3776. event->pmu->event_mapped(event);
  3777. }
  3778. /*
  3779. * A buffer can be mmap()ed multiple times; either directly through the same
  3780. * event, or through other events by use of perf_event_set_output().
  3781. *
  3782. * In order to undo the VM accounting done by perf_mmap() we need to destroy
  3783. * the buffer here, where we still have a VM context. This means we need
  3784. * to detach all events redirecting to us.
  3785. */
  3786. static void perf_mmap_close(struct vm_area_struct *vma)
  3787. {
  3788. struct perf_event *event = vma->vm_file->private_data;
  3789. struct ring_buffer *rb = ring_buffer_get(event);
  3790. struct user_struct *mmap_user = rb->mmap_user;
  3791. int mmap_locked = rb->mmap_locked;
  3792. unsigned long size = perf_data_size(rb);
  3793. if (event->pmu->event_unmapped)
  3794. event->pmu->event_unmapped(event);
  3795. /*
  3796. * rb->aux_mmap_count will always drop before rb->mmap_count and
  3797. * event->mmap_count, so it is ok to use event->mmap_mutex to
  3798. * serialize with perf_mmap here.
  3799. */
  3800. if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
  3801. atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
  3802. atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
  3803. vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
  3804. rb_free_aux(rb);
  3805. mutex_unlock(&event->mmap_mutex);
  3806. }
  3807. atomic_dec(&rb->mmap_count);
  3808. if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
  3809. goto out_put;
  3810. ring_buffer_attach(event, NULL);
  3811. mutex_unlock(&event->mmap_mutex);
  3812. /* If there's still other mmap()s of this buffer, we're done. */
  3813. if (atomic_read(&rb->mmap_count))
  3814. goto out_put;
  3815. /*
  3816. * No other mmap()s, detach from all other events that might redirect
  3817. * into the now unreachable buffer. Somewhat complicated by the
  3818. * fact that rb::event_lock otherwise nests inside mmap_mutex.
  3819. */
  3820. again:
  3821. rcu_read_lock();
  3822. list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
  3823. if (!atomic_long_inc_not_zero(&event->refcount)) {
  3824. /*
  3825. * This event is en-route to free_event() which will
  3826. * detach it and remove it from the list.
  3827. */
  3828. continue;
  3829. }
  3830. rcu_read_unlock();
  3831. mutex_lock(&event->mmap_mutex);
  3832. /*
  3833. * Check we didn't race with perf_event_set_output() which can
  3834. * swizzle the rb from under us while we were waiting to
  3835. * acquire mmap_mutex.
  3836. *
  3837. * If we find a different rb; ignore this event, a next
  3838. * iteration will no longer find it on the list. We have to
  3839. * still restart the iteration to make sure we're not now
  3840. * iterating the wrong list.
  3841. */
  3842. if (event->rb == rb)
  3843. ring_buffer_attach(event, NULL);
  3844. mutex_unlock(&event->mmap_mutex);
  3845. put_event(event);
  3846. /*
  3847. * Restart the iteration; either we're on the wrong list or
  3848. * destroyed its integrity by doing a deletion.
  3849. */
  3850. goto again;
  3851. }
  3852. rcu_read_unlock();
  3853. /*
  3854. * It could be there's still a few 0-ref events on the list; they'll
  3855. * get cleaned up by free_event() -- they'll also still have their
  3856. * ref on the rb and will free it whenever they are done with it.
  3857. *
  3858. * Aside from that, this buffer is 'fully' detached and unmapped,
  3859. * undo the VM accounting.
  3860. */
  3861. atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
  3862. vma->vm_mm->pinned_vm -= mmap_locked;
  3863. free_uid(mmap_user);
  3864. out_put:
  3865. ring_buffer_put(rb); /* could be last */
  3866. }
  3867. static const struct vm_operations_struct perf_mmap_vmops = {
  3868. .open = perf_mmap_open,
  3869. .close = perf_mmap_close, /* non mergable */
  3870. .fault = perf_mmap_fault,
  3871. .page_mkwrite = perf_mmap_fault,
  3872. };
  3873. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  3874. {
  3875. struct perf_event *event = file->private_data;
  3876. unsigned long user_locked, user_lock_limit;
  3877. struct user_struct *user = current_user();
  3878. unsigned long locked, lock_limit;
  3879. struct ring_buffer *rb = NULL;
  3880. unsigned long vma_size;
  3881. unsigned long nr_pages;
  3882. long user_extra = 0, extra = 0;
  3883. int ret = 0, flags = 0;
  3884. /*
  3885. * Don't allow mmap() of inherited per-task counters. This would
  3886. * create a performance issue due to all children writing to the
  3887. * same rb.
  3888. */
  3889. if (event->cpu == -1 && event->attr.inherit)
  3890. return -EINVAL;
  3891. if (!(vma->vm_flags & VM_SHARED))
  3892. return -EINVAL;
  3893. vma_size = vma->vm_end - vma->vm_start;
  3894. if (vma->vm_pgoff == 0) {
  3895. nr_pages = (vma_size / PAGE_SIZE) - 1;
  3896. } else {
  3897. /*
  3898. * AUX area mapping: if rb->aux_nr_pages != 0, it's already
  3899. * mapped, all subsequent mappings should have the same size
  3900. * and offset. Must be above the normal perf buffer.
  3901. */
  3902. u64 aux_offset, aux_size;
  3903. if (!event->rb)
  3904. return -EINVAL;
  3905. nr_pages = vma_size / PAGE_SIZE;
  3906. mutex_lock(&event->mmap_mutex);
  3907. ret = -EINVAL;
  3908. rb = event->rb;
  3909. if (!rb)
  3910. goto aux_unlock;
  3911. aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
  3912. aux_size = ACCESS_ONCE(rb->user_page->aux_size);
  3913. if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
  3914. goto aux_unlock;
  3915. if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
  3916. goto aux_unlock;
  3917. /* already mapped with a different offset */
  3918. if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
  3919. goto aux_unlock;
  3920. if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
  3921. goto aux_unlock;
  3922. /* already mapped with a different size */
  3923. if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
  3924. goto aux_unlock;
  3925. if (!is_power_of_2(nr_pages))
  3926. goto aux_unlock;
  3927. if (!atomic_inc_not_zero(&rb->mmap_count))
  3928. goto aux_unlock;
  3929. if (rb_has_aux(rb)) {
  3930. atomic_inc(&rb->aux_mmap_count);
  3931. ret = 0;
  3932. goto unlock;
  3933. }
  3934. atomic_set(&rb->aux_mmap_count, 1);
  3935. user_extra = nr_pages;
  3936. goto accounting;
  3937. }
  3938. /*
  3939. * If we have rb pages ensure they're a power-of-two number, so we
  3940. * can do bitmasks instead of modulo.
  3941. */
  3942. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  3943. return -EINVAL;
  3944. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  3945. return -EINVAL;
  3946. WARN_ON_ONCE(event->ctx->parent_ctx);
  3947. again:
  3948. mutex_lock(&event->mmap_mutex);
  3949. if (event->rb) {
  3950. if (event->rb->nr_pages != nr_pages) {
  3951. ret = -EINVAL;
  3952. goto unlock;
  3953. }
  3954. if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
  3955. /*
  3956. * Raced against perf_mmap_close() through
  3957. * perf_event_set_output(). Try again, hope for better
  3958. * luck.
  3959. */
  3960. mutex_unlock(&event->mmap_mutex);
  3961. goto again;
  3962. }
  3963. goto unlock;
  3964. }
  3965. user_extra = nr_pages + 1;
  3966. accounting:
  3967. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  3968. /*
  3969. * Increase the limit linearly with more CPUs:
  3970. */
  3971. user_lock_limit *= num_online_cpus();
  3972. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  3973. if (user_locked > user_lock_limit)
  3974. extra = user_locked - user_lock_limit;
  3975. lock_limit = rlimit(RLIMIT_MEMLOCK);
  3976. lock_limit >>= PAGE_SHIFT;
  3977. locked = vma->vm_mm->pinned_vm + extra;
  3978. if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  3979. !capable(CAP_IPC_LOCK)) {
  3980. ret = -EPERM;
  3981. goto unlock;
  3982. }
  3983. WARN_ON(!rb && event->rb);
  3984. if (vma->vm_flags & VM_WRITE)
  3985. flags |= RING_BUFFER_WRITABLE;
  3986. if (!rb) {
  3987. rb = rb_alloc(nr_pages,
  3988. event->attr.watermark ? event->attr.wakeup_watermark : 0,
  3989. event->cpu, flags);
  3990. if (!rb) {
  3991. ret = -ENOMEM;
  3992. goto unlock;
  3993. }
  3994. atomic_set(&rb->mmap_count, 1);
  3995. rb->mmap_user = get_current_user();
  3996. rb->mmap_locked = extra;
  3997. ring_buffer_attach(event, rb);
  3998. perf_event_init_userpage(event);
  3999. perf_event_update_userpage(event);
  4000. } else {
  4001. ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
  4002. event->attr.aux_watermark, flags);
  4003. if (!ret)
  4004. rb->aux_mmap_locked = extra;
  4005. }
  4006. unlock:
  4007. if (!ret) {
  4008. atomic_long_add(user_extra, &user->locked_vm);
  4009. vma->vm_mm->pinned_vm += extra;
  4010. atomic_inc(&event->mmap_count);
  4011. } else if (rb) {
  4012. atomic_dec(&rb->mmap_count);
  4013. }
  4014. aux_unlock:
  4015. mutex_unlock(&event->mmap_mutex);
  4016. /*
  4017. * Since pinned accounting is per vm we cannot allow fork() to copy our
  4018. * vma.
  4019. */
  4020. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
  4021. vma->vm_ops = &perf_mmap_vmops;
  4022. if (event->pmu->event_mapped)
  4023. event->pmu->event_mapped(event);
  4024. return ret;
  4025. }
  4026. static int perf_fasync(int fd, struct file *filp, int on)
  4027. {
  4028. struct inode *inode = file_inode(filp);
  4029. struct perf_event *event = filp->private_data;
  4030. int retval;
  4031. inode_lock(inode);
  4032. retval = fasync_helper(fd, filp, on, &event->fasync);
  4033. inode_unlock(inode);
  4034. if (retval < 0)
  4035. return retval;
  4036. return 0;
  4037. }
  4038. static const struct file_operations perf_fops = {
  4039. .llseek = no_llseek,
  4040. .release = perf_release,
  4041. .read = perf_read,
  4042. .poll = perf_poll,
  4043. .unlocked_ioctl = perf_ioctl,
  4044. .compat_ioctl = perf_compat_ioctl,
  4045. .mmap = perf_mmap,
  4046. .fasync = perf_fasync,
  4047. };
  4048. /*
  4049. * Perf event wakeup
  4050. *
  4051. * If there's data, ensure we set the poll() state and publish everything
  4052. * to user-space before waking everybody up.
  4053. */
  4054. static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
  4055. {
  4056. /* only the parent has fasync state */
  4057. if (event->parent)
  4058. event = event->parent;
  4059. return &event->fasync;
  4060. }
  4061. void perf_event_wakeup(struct perf_event *event)
  4062. {
  4063. ring_buffer_wakeup(event);
  4064. if (event->pending_kill) {
  4065. kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
  4066. event->pending_kill = 0;
  4067. }
  4068. }
  4069. static void perf_pending_event(struct irq_work *entry)
  4070. {
  4071. struct perf_event *event = container_of(entry,
  4072. struct perf_event, pending);
  4073. int rctx;
  4074. rctx = perf_swevent_get_recursion_context();
  4075. /*
  4076. * If we 'fail' here, that's OK, it means recursion is already disabled
  4077. * and we won't recurse 'further'.
  4078. */
  4079. if (event->pending_disable) {
  4080. event->pending_disable = 0;
  4081. perf_event_disable_local(event);
  4082. }
  4083. if (event->pending_wakeup) {
  4084. event->pending_wakeup = 0;
  4085. perf_event_wakeup(event);
  4086. }
  4087. if (rctx >= 0)
  4088. perf_swevent_put_recursion_context(rctx);
  4089. }
  4090. /*
  4091. * We assume there is only KVM supporting the callbacks.
  4092. * Later on, we might change it to a list if there is
  4093. * another virtualization implementation supporting the callbacks.
  4094. */
  4095. struct perf_guest_info_callbacks *perf_guest_cbs;
  4096. int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  4097. {
  4098. perf_guest_cbs = cbs;
  4099. return 0;
  4100. }
  4101. EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
  4102. int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  4103. {
  4104. perf_guest_cbs = NULL;
  4105. return 0;
  4106. }
  4107. EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
  4108. static void
  4109. perf_output_sample_regs(struct perf_output_handle *handle,
  4110. struct pt_regs *regs, u64 mask)
  4111. {
  4112. int bit;
  4113. for_each_set_bit(bit, (const unsigned long *) &mask,
  4114. sizeof(mask) * BITS_PER_BYTE) {
  4115. u64 val;
  4116. val = perf_reg_value(regs, bit);
  4117. perf_output_put(handle, val);
  4118. }
  4119. }
  4120. static void perf_sample_regs_user(struct perf_regs *regs_user,
  4121. struct pt_regs *regs,
  4122. struct pt_regs *regs_user_copy)
  4123. {
  4124. if (user_mode(regs)) {
  4125. regs_user->abi = perf_reg_abi(current);
  4126. regs_user->regs = regs;
  4127. } else if (current->mm) {
  4128. perf_get_regs_user(regs_user, regs, regs_user_copy);
  4129. } else {
  4130. regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
  4131. regs_user->regs = NULL;
  4132. }
  4133. }
  4134. static void perf_sample_regs_intr(struct perf_regs *regs_intr,
  4135. struct pt_regs *regs)
  4136. {
  4137. regs_intr->regs = regs;
  4138. regs_intr->abi = perf_reg_abi(current);
  4139. }
  4140. /*
  4141. * Get remaining task size from user stack pointer.
  4142. *
  4143. * It'd be better to take stack vma map and limit this more
  4144. * precisly, but there's no way to get it safely under interrupt,
  4145. * so using TASK_SIZE as limit.
  4146. */
  4147. static u64 perf_ustack_task_size(struct pt_regs *regs)
  4148. {
  4149. unsigned long addr = perf_user_stack_pointer(regs);
  4150. if (!addr || addr >= TASK_SIZE)
  4151. return 0;
  4152. return TASK_SIZE - addr;
  4153. }
  4154. static u16
  4155. perf_sample_ustack_size(u16 stack_size, u16 header_size,
  4156. struct pt_regs *regs)
  4157. {
  4158. u64 task_size;
  4159. /* No regs, no stack pointer, no dump. */
  4160. if (!regs)
  4161. return 0;
  4162. /*
  4163. * Check if we fit in with the requested stack size into the:
  4164. * - TASK_SIZE
  4165. * If we don't, we limit the size to the TASK_SIZE.
  4166. *
  4167. * - remaining sample size
  4168. * If we don't, we customize the stack size to
  4169. * fit in to the remaining sample size.
  4170. */
  4171. task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
  4172. stack_size = min(stack_size, (u16) task_size);
  4173. /* Current header size plus static size and dynamic size. */
  4174. header_size += 2 * sizeof(u64);
  4175. /* Do we fit in with the current stack dump size? */
  4176. if ((u16) (header_size + stack_size) < header_size) {
  4177. /*
  4178. * If we overflow the maximum size for the sample,
  4179. * we customize the stack dump size to fit in.
  4180. */
  4181. stack_size = USHRT_MAX - header_size - sizeof(u64);
  4182. stack_size = round_up(stack_size, sizeof(u64));
  4183. }
  4184. return stack_size;
  4185. }
  4186. static void
  4187. perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
  4188. struct pt_regs *regs)
  4189. {
  4190. /* Case of a kernel thread, nothing to dump */
  4191. if (!regs) {
  4192. u64 size = 0;
  4193. perf_output_put(handle, size);
  4194. } else {
  4195. unsigned long sp;
  4196. unsigned int rem;
  4197. u64 dyn_size;
  4198. /*
  4199. * We dump:
  4200. * static size
  4201. * - the size requested by user or the best one we can fit
  4202. * in to the sample max size
  4203. * data
  4204. * - user stack dump data
  4205. * dynamic size
  4206. * - the actual dumped size
  4207. */
  4208. /* Static size. */
  4209. perf_output_put(handle, dump_size);
  4210. /* Data. */
  4211. sp = perf_user_stack_pointer(regs);
  4212. rem = __output_copy_user(handle, (void *) sp, dump_size);
  4213. dyn_size = dump_size - rem;
  4214. perf_output_skip(handle, rem);
  4215. /* Dynamic size. */
  4216. perf_output_put(handle, dyn_size);
  4217. }
  4218. }
  4219. static void __perf_event_header__init_id(struct perf_event_header *header,
  4220. struct perf_sample_data *data,
  4221. struct perf_event *event)
  4222. {
  4223. u64 sample_type = event->attr.sample_type;
  4224. data->type = sample_type;
  4225. header->size += event->id_header_size;
  4226. if (sample_type & PERF_SAMPLE_TID) {
  4227. /* namespace issues */
  4228. data->tid_entry.pid = perf_event_pid(event, current);
  4229. data->tid_entry.tid = perf_event_tid(event, current);
  4230. }
  4231. if (sample_type & PERF_SAMPLE_TIME)
  4232. data->time = perf_event_clock(event);
  4233. if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
  4234. data->id = primary_event_id(event);
  4235. if (sample_type & PERF_SAMPLE_STREAM_ID)
  4236. data->stream_id = event->id;
  4237. if (sample_type & PERF_SAMPLE_CPU) {
  4238. data->cpu_entry.cpu = raw_smp_processor_id();
  4239. data->cpu_entry.reserved = 0;
  4240. }
  4241. }
  4242. void perf_event_header__init_id(struct perf_event_header *header,
  4243. struct perf_sample_data *data,
  4244. struct perf_event *event)
  4245. {
  4246. if (event->attr.sample_id_all)
  4247. __perf_event_header__init_id(header, data, event);
  4248. }
  4249. static void __perf_event__output_id_sample(struct perf_output_handle *handle,
  4250. struct perf_sample_data *data)
  4251. {
  4252. u64 sample_type = data->type;
  4253. if (sample_type & PERF_SAMPLE_TID)
  4254. perf_output_put(handle, data->tid_entry);
  4255. if (sample_type & PERF_SAMPLE_TIME)
  4256. perf_output_put(handle, data->time);
  4257. if (sample_type & PERF_SAMPLE_ID)
  4258. perf_output_put(handle, data->id);
  4259. if (sample_type & PERF_SAMPLE_STREAM_ID)
  4260. perf_output_put(handle, data->stream_id);
  4261. if (sample_type & PERF_SAMPLE_CPU)
  4262. perf_output_put(handle, data->cpu_entry);
  4263. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  4264. perf_output_put(handle, data->id);
  4265. }
  4266. void perf_event__output_id_sample(struct perf_event *event,
  4267. struct perf_output_handle *handle,
  4268. struct perf_sample_data *sample)
  4269. {
  4270. if (event->attr.sample_id_all)
  4271. __perf_event__output_id_sample(handle, sample);
  4272. }
  4273. static void perf_output_read_one(struct perf_output_handle *handle,
  4274. struct perf_event *event,
  4275. u64 enabled, u64 running)
  4276. {
  4277. u64 read_format = event->attr.read_format;
  4278. u64 values[4];
  4279. int n = 0;
  4280. values[n++] = perf_event_count(event);
  4281. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  4282. values[n++] = enabled +
  4283. atomic64_read(&event->child_total_time_enabled);
  4284. }
  4285. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  4286. values[n++] = running +
  4287. atomic64_read(&event->child_total_time_running);
  4288. }
  4289. if (read_format & PERF_FORMAT_ID)
  4290. values[n++] = primary_event_id(event);
  4291. __output_copy(handle, values, n * sizeof(u64));
  4292. }
  4293. /*
  4294. * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
  4295. */
  4296. static void perf_output_read_group(struct perf_output_handle *handle,
  4297. struct perf_event *event,
  4298. u64 enabled, u64 running)
  4299. {
  4300. struct perf_event *leader = event->group_leader, *sub;
  4301. u64 read_format = event->attr.read_format;
  4302. u64 values[5];
  4303. int n = 0;
  4304. values[n++] = 1 + leader->nr_siblings;
  4305. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  4306. values[n++] = enabled;
  4307. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  4308. values[n++] = running;
  4309. if (leader != event)
  4310. leader->pmu->read(leader);
  4311. values[n++] = perf_event_count(leader);
  4312. if (read_format & PERF_FORMAT_ID)
  4313. values[n++] = primary_event_id(leader);
  4314. __output_copy(handle, values, n * sizeof(u64));
  4315. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  4316. n = 0;
  4317. if ((sub != event) &&
  4318. (sub->state == PERF_EVENT_STATE_ACTIVE))
  4319. sub->pmu->read(sub);
  4320. values[n++] = perf_event_count(sub);
  4321. if (read_format & PERF_FORMAT_ID)
  4322. values[n++] = primary_event_id(sub);
  4323. __output_copy(handle, values, n * sizeof(u64));
  4324. }
  4325. }
  4326. #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
  4327. PERF_FORMAT_TOTAL_TIME_RUNNING)
  4328. static void perf_output_read(struct perf_output_handle *handle,
  4329. struct perf_event *event)
  4330. {
  4331. u64 enabled = 0, running = 0, now;
  4332. u64 read_format = event->attr.read_format;
  4333. /*
  4334. * compute total_time_enabled, total_time_running
  4335. * based on snapshot values taken when the event
  4336. * was last scheduled in.
  4337. *
  4338. * we cannot simply called update_context_time()
  4339. * because of locking issue as we are called in
  4340. * NMI context
  4341. */
  4342. if (read_format & PERF_FORMAT_TOTAL_TIMES)
  4343. calc_timer_values(event, &now, &enabled, &running);
  4344. if (event->attr.read_format & PERF_FORMAT_GROUP)
  4345. perf_output_read_group(handle, event, enabled, running);
  4346. else
  4347. perf_output_read_one(handle, event, enabled, running);
  4348. }
  4349. void perf_output_sample(struct perf_output_handle *handle,
  4350. struct perf_event_header *header,
  4351. struct perf_sample_data *data,
  4352. struct perf_event *event)
  4353. {
  4354. u64 sample_type = data->type;
  4355. perf_output_put(handle, *header);
  4356. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  4357. perf_output_put(handle, data->id);
  4358. if (sample_type & PERF_SAMPLE_IP)
  4359. perf_output_put(handle, data->ip);
  4360. if (sample_type & PERF_SAMPLE_TID)
  4361. perf_output_put(handle, data->tid_entry);
  4362. if (sample_type & PERF_SAMPLE_TIME)
  4363. perf_output_put(handle, data->time);
  4364. if (sample_type & PERF_SAMPLE_ADDR)
  4365. perf_output_put(handle, data->addr);
  4366. if (sample_type & PERF_SAMPLE_ID)
  4367. perf_output_put(handle, data->id);
  4368. if (sample_type & PERF_SAMPLE_STREAM_ID)
  4369. perf_output_put(handle, data->stream_id);
  4370. if (sample_type & PERF_SAMPLE_CPU)
  4371. perf_output_put(handle, data->cpu_entry);
  4372. if (sample_type & PERF_SAMPLE_PERIOD)
  4373. perf_output_put(handle, data->period);
  4374. if (sample_type & PERF_SAMPLE_READ)
  4375. perf_output_read(handle, event);
  4376. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  4377. if (data->callchain) {
  4378. int size = 1;
  4379. if (data->callchain)
  4380. size += data->callchain->nr;
  4381. size *= sizeof(u64);
  4382. __output_copy(handle, data->callchain, size);
  4383. } else {
  4384. u64 nr = 0;
  4385. perf_output_put(handle, nr);
  4386. }
  4387. }
  4388. if (sample_type & PERF_SAMPLE_RAW) {
  4389. if (data->raw) {
  4390. u32 raw_size = data->raw->size;
  4391. u32 real_size = round_up(raw_size + sizeof(u32),
  4392. sizeof(u64)) - sizeof(u32);
  4393. u64 zero = 0;
  4394. perf_output_put(handle, real_size);
  4395. __output_copy(handle, data->raw->data, raw_size);
  4396. if (real_size - raw_size)
  4397. __output_copy(handle, &zero, real_size - raw_size);
  4398. } else {
  4399. struct {
  4400. u32 size;
  4401. u32 data;
  4402. } raw = {
  4403. .size = sizeof(u32),
  4404. .data = 0,
  4405. };
  4406. perf_output_put(handle, raw);
  4407. }
  4408. }
  4409. if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
  4410. if (data->br_stack) {
  4411. size_t size;
  4412. size = data->br_stack->nr
  4413. * sizeof(struct perf_branch_entry);
  4414. perf_output_put(handle, data->br_stack->nr);
  4415. perf_output_copy(handle, data->br_stack->entries, size);
  4416. } else {
  4417. /*
  4418. * we always store at least the value of nr
  4419. */
  4420. u64 nr = 0;
  4421. perf_output_put(handle, nr);
  4422. }
  4423. }
  4424. if (sample_type & PERF_SAMPLE_REGS_USER) {
  4425. u64 abi = data->regs_user.abi;
  4426. /*
  4427. * If there are no regs to dump, notice it through
  4428. * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
  4429. */
  4430. perf_output_put(handle, abi);
  4431. if (abi) {
  4432. u64 mask = event->attr.sample_regs_user;
  4433. perf_output_sample_regs(handle,
  4434. data->regs_user.regs,
  4435. mask);
  4436. }
  4437. }
  4438. if (sample_type & PERF_SAMPLE_STACK_USER) {
  4439. perf_output_sample_ustack(handle,
  4440. data->stack_user_size,
  4441. data->regs_user.regs);
  4442. }
  4443. if (sample_type & PERF_SAMPLE_WEIGHT)
  4444. perf_output_put(handle, data->weight);
  4445. if (sample_type & PERF_SAMPLE_DATA_SRC)
  4446. perf_output_put(handle, data->data_src.val);
  4447. if (sample_type & PERF_SAMPLE_TRANSACTION)
  4448. perf_output_put(handle, data->txn);
  4449. if (sample_type & PERF_SAMPLE_REGS_INTR) {
  4450. u64 abi = data->regs_intr.abi;
  4451. /*
  4452. * If there are no regs to dump, notice it through
  4453. * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
  4454. */
  4455. perf_output_put(handle, abi);
  4456. if (abi) {
  4457. u64 mask = event->attr.sample_regs_intr;
  4458. perf_output_sample_regs(handle,
  4459. data->regs_intr.regs,
  4460. mask);
  4461. }
  4462. }
  4463. if (!event->attr.watermark) {
  4464. int wakeup_events = event->attr.wakeup_events;
  4465. if (wakeup_events) {
  4466. struct ring_buffer *rb = handle->rb;
  4467. int events = local_inc_return(&rb->events);
  4468. if (events >= wakeup_events) {
  4469. local_sub(wakeup_events, &rb->events);
  4470. local_inc(&rb->wakeup);
  4471. }
  4472. }
  4473. }
  4474. }
  4475. void perf_prepare_sample(struct perf_event_header *header,
  4476. struct perf_sample_data *data,
  4477. struct perf_event *event,
  4478. struct pt_regs *regs)
  4479. {
  4480. u64 sample_type = event->attr.sample_type;
  4481. header->type = PERF_RECORD_SAMPLE;
  4482. header->size = sizeof(*header) + event->header_size;
  4483. header->misc = 0;
  4484. header->misc |= perf_misc_flags(regs);
  4485. __perf_event_header__init_id(header, data, event);
  4486. if (sample_type & PERF_SAMPLE_IP)
  4487. data->ip = perf_instruction_pointer(regs);
  4488. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  4489. int size = 1;
  4490. data->callchain = perf_callchain(event, regs);
  4491. if (data->callchain)
  4492. size += data->callchain->nr;
  4493. header->size += size * sizeof(u64);
  4494. }
  4495. if (sample_type & PERF_SAMPLE_RAW) {
  4496. int size = sizeof(u32);
  4497. if (data->raw)
  4498. size += data->raw->size;
  4499. else
  4500. size += sizeof(u32);
  4501. header->size += round_up(size, sizeof(u64));
  4502. }
  4503. if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
  4504. int size = sizeof(u64); /* nr */
  4505. if (data->br_stack) {
  4506. size += data->br_stack->nr
  4507. * sizeof(struct perf_branch_entry);
  4508. }
  4509. header->size += size;
  4510. }
  4511. if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
  4512. perf_sample_regs_user(&data->regs_user, regs,
  4513. &data->regs_user_copy);
  4514. if (sample_type & PERF_SAMPLE_REGS_USER) {
  4515. /* regs dump ABI info */
  4516. int size = sizeof(u64);
  4517. if (data->regs_user.regs) {
  4518. u64 mask = event->attr.sample_regs_user;
  4519. size += hweight64(mask) * sizeof(u64);
  4520. }
  4521. header->size += size;
  4522. }
  4523. if (sample_type & PERF_SAMPLE_STACK_USER) {
  4524. /*
  4525. * Either we need PERF_SAMPLE_STACK_USER bit to be allways
  4526. * processed as the last one or have additional check added
  4527. * in case new sample type is added, because we could eat
  4528. * up the rest of the sample size.
  4529. */
  4530. u16 stack_size = event->attr.sample_stack_user;
  4531. u16 size = sizeof(u64);
  4532. stack_size = perf_sample_ustack_size(stack_size, header->size,
  4533. data->regs_user.regs);
  4534. /*
  4535. * If there is something to dump, add space for the dump
  4536. * itself and for the field that tells the dynamic size,
  4537. * which is how many have been actually dumped.
  4538. */
  4539. if (stack_size)
  4540. size += sizeof(u64) + stack_size;
  4541. data->stack_user_size = stack_size;
  4542. header->size += size;
  4543. }
  4544. if (sample_type & PERF_SAMPLE_REGS_INTR) {
  4545. /* regs dump ABI info */
  4546. int size = sizeof(u64);
  4547. perf_sample_regs_intr(&data->regs_intr, regs);
  4548. if (data->regs_intr.regs) {
  4549. u64 mask = event->attr.sample_regs_intr;
  4550. size += hweight64(mask) * sizeof(u64);
  4551. }
  4552. header->size += size;
  4553. }
  4554. }
  4555. void perf_event_output(struct perf_event *event,
  4556. struct perf_sample_data *data,
  4557. struct pt_regs *regs)
  4558. {
  4559. struct perf_output_handle handle;
  4560. struct perf_event_header header;
  4561. /* protect the callchain buffers */
  4562. rcu_read_lock();
  4563. perf_prepare_sample(&header, data, event, regs);
  4564. if (perf_output_begin(&handle, event, header.size))
  4565. goto exit;
  4566. perf_output_sample(&handle, &header, data, event);
  4567. perf_output_end(&handle);
  4568. exit:
  4569. rcu_read_unlock();
  4570. }
  4571. /*
  4572. * read event_id
  4573. */
  4574. struct perf_read_event {
  4575. struct perf_event_header header;
  4576. u32 pid;
  4577. u32 tid;
  4578. };
  4579. static void
  4580. perf_event_read_event(struct perf_event *event,
  4581. struct task_struct *task)
  4582. {
  4583. struct perf_output_handle handle;
  4584. struct perf_sample_data sample;
  4585. struct perf_read_event read_event = {
  4586. .header = {
  4587. .type = PERF_RECORD_READ,
  4588. .misc = 0,
  4589. .size = sizeof(read_event) + event->read_size,
  4590. },
  4591. .pid = perf_event_pid(event, task),
  4592. .tid = perf_event_tid(event, task),
  4593. };
  4594. int ret;
  4595. perf_event_header__init_id(&read_event.header, &sample, event);
  4596. ret = perf_output_begin(&handle, event, read_event.header.size);
  4597. if (ret)
  4598. return;
  4599. perf_output_put(&handle, read_event);
  4600. perf_output_read(&handle, event);
  4601. perf_event__output_id_sample(event, &handle, &sample);
  4602. perf_output_end(&handle);
  4603. }
  4604. typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
  4605. static void
  4606. perf_event_aux_ctx(struct perf_event_context *ctx,
  4607. perf_event_aux_output_cb output,
  4608. void *data)
  4609. {
  4610. struct perf_event *event;
  4611. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  4612. if (event->state < PERF_EVENT_STATE_INACTIVE)
  4613. continue;
  4614. if (!event_filter_match(event))
  4615. continue;
  4616. output(event, data);
  4617. }
  4618. }
  4619. static void
  4620. perf_event_aux_task_ctx(perf_event_aux_output_cb output, void *data,
  4621. struct perf_event_context *task_ctx)
  4622. {
  4623. rcu_read_lock();
  4624. preempt_disable();
  4625. perf_event_aux_ctx(task_ctx, output, data);
  4626. preempt_enable();
  4627. rcu_read_unlock();
  4628. }
  4629. static void
  4630. perf_event_aux(perf_event_aux_output_cb output, void *data,
  4631. struct perf_event_context *task_ctx)
  4632. {
  4633. struct perf_cpu_context *cpuctx;
  4634. struct perf_event_context *ctx;
  4635. struct pmu *pmu;
  4636. int ctxn;
  4637. /*
  4638. * If we have task_ctx != NULL we only notify
  4639. * the task context itself. The task_ctx is set
  4640. * only for EXIT events before releasing task
  4641. * context.
  4642. */
  4643. if (task_ctx) {
  4644. perf_event_aux_task_ctx(output, data, task_ctx);
  4645. return;
  4646. }
  4647. rcu_read_lock();
  4648. list_for_each_entry_rcu(pmu, &pmus, entry) {
  4649. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  4650. if (cpuctx->unique_pmu != pmu)
  4651. goto next;
  4652. perf_event_aux_ctx(&cpuctx->ctx, output, data);
  4653. ctxn = pmu->task_ctx_nr;
  4654. if (ctxn < 0)
  4655. goto next;
  4656. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  4657. if (ctx)
  4658. perf_event_aux_ctx(ctx, output, data);
  4659. next:
  4660. put_cpu_ptr(pmu->pmu_cpu_context);
  4661. }
  4662. rcu_read_unlock();
  4663. }
  4664. /*
  4665. * task tracking -- fork/exit
  4666. *
  4667. * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
  4668. */
  4669. struct perf_task_event {
  4670. struct task_struct *task;
  4671. struct perf_event_context *task_ctx;
  4672. struct {
  4673. struct perf_event_header header;
  4674. u32 pid;
  4675. u32 ppid;
  4676. u32 tid;
  4677. u32 ptid;
  4678. u64 time;
  4679. } event_id;
  4680. };
  4681. static int perf_event_task_match(struct perf_event *event)
  4682. {
  4683. return event->attr.comm || event->attr.mmap ||
  4684. event->attr.mmap2 || event->attr.mmap_data ||
  4685. event->attr.task;
  4686. }
  4687. static void perf_event_task_output(struct perf_event *event,
  4688. void *data)
  4689. {
  4690. struct perf_task_event *task_event = data;
  4691. struct perf_output_handle handle;
  4692. struct perf_sample_data sample;
  4693. struct task_struct *task = task_event->task;
  4694. int ret, size = task_event->event_id.header.size;
  4695. if (!perf_event_task_match(event))
  4696. return;
  4697. perf_event_header__init_id(&task_event->event_id.header, &sample, event);
  4698. ret = perf_output_begin(&handle, event,
  4699. task_event->event_id.header.size);
  4700. if (ret)
  4701. goto out;
  4702. task_event->event_id.pid = perf_event_pid(event, task);
  4703. task_event->event_id.ppid = perf_event_pid(event, current);
  4704. task_event->event_id.tid = perf_event_tid(event, task);
  4705. task_event->event_id.ptid = perf_event_tid(event, current);
  4706. task_event->event_id.time = perf_event_clock(event);
  4707. perf_output_put(&handle, task_event->event_id);
  4708. perf_event__output_id_sample(event, &handle, &sample);
  4709. perf_output_end(&handle);
  4710. out:
  4711. task_event->event_id.header.size = size;
  4712. }
  4713. static void perf_event_task(struct task_struct *task,
  4714. struct perf_event_context *task_ctx,
  4715. int new)
  4716. {
  4717. struct perf_task_event task_event;
  4718. if (!atomic_read(&nr_comm_events) &&
  4719. !atomic_read(&nr_mmap_events) &&
  4720. !atomic_read(&nr_task_events))
  4721. return;
  4722. task_event = (struct perf_task_event){
  4723. .task = task,
  4724. .task_ctx = task_ctx,
  4725. .event_id = {
  4726. .header = {
  4727. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  4728. .misc = 0,
  4729. .size = sizeof(task_event.event_id),
  4730. },
  4731. /* .pid */
  4732. /* .ppid */
  4733. /* .tid */
  4734. /* .ptid */
  4735. /* .time */
  4736. },
  4737. };
  4738. perf_event_aux(perf_event_task_output,
  4739. &task_event,
  4740. task_ctx);
  4741. }
  4742. void perf_event_fork(struct task_struct *task)
  4743. {
  4744. perf_event_task(task, NULL, 1);
  4745. }
  4746. /*
  4747. * comm tracking
  4748. */
  4749. struct perf_comm_event {
  4750. struct task_struct *task;
  4751. char *comm;
  4752. int comm_size;
  4753. struct {
  4754. struct perf_event_header header;
  4755. u32 pid;
  4756. u32 tid;
  4757. } event_id;
  4758. };
  4759. static int perf_event_comm_match(struct perf_event *event)
  4760. {
  4761. return event->attr.comm;
  4762. }
  4763. static void perf_event_comm_output(struct perf_event *event,
  4764. void *data)
  4765. {
  4766. struct perf_comm_event *comm_event = data;
  4767. struct perf_output_handle handle;
  4768. struct perf_sample_data sample;
  4769. int size = comm_event->event_id.header.size;
  4770. int ret;
  4771. if (!perf_event_comm_match(event))
  4772. return;
  4773. perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
  4774. ret = perf_output_begin(&handle, event,
  4775. comm_event->event_id.header.size);
  4776. if (ret)
  4777. goto out;
  4778. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  4779. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  4780. perf_output_put(&handle, comm_event->event_id);
  4781. __output_copy(&handle, comm_event->comm,
  4782. comm_event->comm_size);
  4783. perf_event__output_id_sample(event, &handle, &sample);
  4784. perf_output_end(&handle);
  4785. out:
  4786. comm_event->event_id.header.size = size;
  4787. }
  4788. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  4789. {
  4790. char comm[TASK_COMM_LEN];
  4791. unsigned int size;
  4792. memset(comm, 0, sizeof(comm));
  4793. strlcpy(comm, comm_event->task->comm, sizeof(comm));
  4794. size = ALIGN(strlen(comm)+1, sizeof(u64));
  4795. comm_event->comm = comm;
  4796. comm_event->comm_size = size;
  4797. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  4798. perf_event_aux(perf_event_comm_output,
  4799. comm_event,
  4800. NULL);
  4801. }
  4802. void perf_event_comm(struct task_struct *task, bool exec)
  4803. {
  4804. struct perf_comm_event comm_event;
  4805. if (!atomic_read(&nr_comm_events))
  4806. return;
  4807. comm_event = (struct perf_comm_event){
  4808. .task = task,
  4809. /* .comm */
  4810. /* .comm_size */
  4811. .event_id = {
  4812. .header = {
  4813. .type = PERF_RECORD_COMM,
  4814. .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
  4815. /* .size */
  4816. },
  4817. /* .pid */
  4818. /* .tid */
  4819. },
  4820. };
  4821. perf_event_comm_event(&comm_event);
  4822. }
  4823. /*
  4824. * mmap tracking
  4825. */
  4826. struct perf_mmap_event {
  4827. struct vm_area_struct *vma;
  4828. const char *file_name;
  4829. int file_size;
  4830. int maj, min;
  4831. u64 ino;
  4832. u64 ino_generation;
  4833. u32 prot, flags;
  4834. struct {
  4835. struct perf_event_header header;
  4836. u32 pid;
  4837. u32 tid;
  4838. u64 start;
  4839. u64 len;
  4840. u64 pgoff;
  4841. } event_id;
  4842. };
  4843. static int perf_event_mmap_match(struct perf_event *event,
  4844. void *data)
  4845. {
  4846. struct perf_mmap_event *mmap_event = data;
  4847. struct vm_area_struct *vma = mmap_event->vma;
  4848. int executable = vma->vm_flags & VM_EXEC;
  4849. return (!executable && event->attr.mmap_data) ||
  4850. (executable && (event->attr.mmap || event->attr.mmap2));
  4851. }
  4852. static void perf_event_mmap_output(struct perf_event *event,
  4853. void *data)
  4854. {
  4855. struct perf_mmap_event *mmap_event = data;
  4856. struct perf_output_handle handle;
  4857. struct perf_sample_data sample;
  4858. int size = mmap_event->event_id.header.size;
  4859. int ret;
  4860. if (!perf_event_mmap_match(event, data))
  4861. return;
  4862. if (event->attr.mmap2) {
  4863. mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
  4864. mmap_event->event_id.header.size += sizeof(mmap_event->maj);
  4865. mmap_event->event_id.header.size += sizeof(mmap_event->min);
  4866. mmap_event->event_id.header.size += sizeof(mmap_event->ino);
  4867. mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
  4868. mmap_event->event_id.header.size += sizeof(mmap_event->prot);
  4869. mmap_event->event_id.header.size += sizeof(mmap_event->flags);
  4870. }
  4871. perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
  4872. ret = perf_output_begin(&handle, event,
  4873. mmap_event->event_id.header.size);
  4874. if (ret)
  4875. goto out;
  4876. mmap_event->event_id.pid = perf_event_pid(event, current);
  4877. mmap_event->event_id.tid = perf_event_tid(event, current);
  4878. perf_output_put(&handle, mmap_event->event_id);
  4879. if (event->attr.mmap2) {
  4880. perf_output_put(&handle, mmap_event->maj);
  4881. perf_output_put(&handle, mmap_event->min);
  4882. perf_output_put(&handle, mmap_event->ino);
  4883. perf_output_put(&handle, mmap_event->ino_generation);
  4884. perf_output_put(&handle, mmap_event->prot);
  4885. perf_output_put(&handle, mmap_event->flags);
  4886. }
  4887. __output_copy(&handle, mmap_event->file_name,
  4888. mmap_event->file_size);
  4889. perf_event__output_id_sample(event, &handle, &sample);
  4890. perf_output_end(&handle);
  4891. out:
  4892. mmap_event->event_id.header.size = size;
  4893. }
  4894. static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
  4895. {
  4896. struct vm_area_struct *vma = mmap_event->vma;
  4897. struct file *file = vma->vm_file;
  4898. int maj = 0, min = 0;
  4899. u64 ino = 0, gen = 0;
  4900. u32 prot = 0, flags = 0;
  4901. unsigned int size;
  4902. char tmp[16];
  4903. char *buf = NULL;
  4904. char *name;
  4905. if (file) {
  4906. struct inode *inode;
  4907. dev_t dev;
  4908. buf = kmalloc(PATH_MAX, GFP_KERNEL);
  4909. if (!buf) {
  4910. name = "//enomem";
  4911. goto cpy_name;
  4912. }
  4913. /*
  4914. * d_path() works from the end of the rb backwards, so we
  4915. * need to add enough zero bytes after the string to handle
  4916. * the 64bit alignment we do later.
  4917. */
  4918. name = file_path(file, buf, PATH_MAX - sizeof(u64));
  4919. if (IS_ERR(name)) {
  4920. name = "//toolong";
  4921. goto cpy_name;
  4922. }
  4923. inode = file_inode(vma->vm_file);
  4924. dev = inode->i_sb->s_dev;
  4925. ino = inode->i_ino;
  4926. gen = inode->i_generation;
  4927. maj = MAJOR(dev);
  4928. min = MINOR(dev);
  4929. if (vma->vm_flags & VM_READ)
  4930. prot |= PROT_READ;
  4931. if (vma->vm_flags & VM_WRITE)
  4932. prot |= PROT_WRITE;
  4933. if (vma->vm_flags & VM_EXEC)
  4934. prot |= PROT_EXEC;
  4935. if (vma->vm_flags & VM_MAYSHARE)
  4936. flags = MAP_SHARED;
  4937. else
  4938. flags = MAP_PRIVATE;
  4939. if (vma->vm_flags & VM_DENYWRITE)
  4940. flags |= MAP_DENYWRITE;
  4941. if (vma->vm_flags & VM_MAYEXEC)
  4942. flags |= MAP_EXECUTABLE;
  4943. if (vma->vm_flags & VM_LOCKED)
  4944. flags |= MAP_LOCKED;
  4945. if (vma->vm_flags & VM_HUGETLB)
  4946. flags |= MAP_HUGETLB;
  4947. goto got_name;
  4948. } else {
  4949. if (vma->vm_ops && vma->vm_ops->name) {
  4950. name = (char *) vma->vm_ops->name(vma);
  4951. if (name)
  4952. goto cpy_name;
  4953. }
  4954. name = (char *)arch_vma_name(vma);
  4955. if (name)
  4956. goto cpy_name;
  4957. if (vma->vm_start <= vma->vm_mm->start_brk &&
  4958. vma->vm_end >= vma->vm_mm->brk) {
  4959. name = "[heap]";
  4960. goto cpy_name;
  4961. }
  4962. if (vma->vm_start <= vma->vm_mm->start_stack &&
  4963. vma->vm_end >= vma->vm_mm->start_stack) {
  4964. name = "[stack]";
  4965. goto cpy_name;
  4966. }
  4967. name = "//anon";
  4968. goto cpy_name;
  4969. }
  4970. cpy_name:
  4971. strlcpy(tmp, name, sizeof(tmp));
  4972. name = tmp;
  4973. got_name:
  4974. /*
  4975. * Since our buffer works in 8 byte units we need to align our string
  4976. * size to a multiple of 8. However, we must guarantee the tail end is
  4977. * zero'd out to avoid leaking random bits to userspace.
  4978. */
  4979. size = strlen(name)+1;
  4980. while (!IS_ALIGNED(size, sizeof(u64)))
  4981. name[size++] = '\0';
  4982. mmap_event->file_name = name;
  4983. mmap_event->file_size = size;
  4984. mmap_event->maj = maj;
  4985. mmap_event->min = min;
  4986. mmap_event->ino = ino;
  4987. mmap_event->ino_generation = gen;
  4988. mmap_event->prot = prot;
  4989. mmap_event->flags = flags;
  4990. if (!(vma->vm_flags & VM_EXEC))
  4991. mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
  4992. mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
  4993. perf_event_aux(perf_event_mmap_output,
  4994. mmap_event,
  4995. NULL);
  4996. kfree(buf);
  4997. }
  4998. void perf_event_mmap(struct vm_area_struct *vma)
  4999. {
  5000. struct perf_mmap_event mmap_event;
  5001. if (!atomic_read(&nr_mmap_events))
  5002. return;
  5003. mmap_event = (struct perf_mmap_event){
  5004. .vma = vma,
  5005. /* .file_name */
  5006. /* .file_size */
  5007. .event_id = {
  5008. .header = {
  5009. .type = PERF_RECORD_MMAP,
  5010. .misc = PERF_RECORD_MISC_USER,
  5011. /* .size */
  5012. },
  5013. /* .pid */
  5014. /* .tid */
  5015. .start = vma->vm_start,
  5016. .len = vma->vm_end - vma->vm_start,
  5017. .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
  5018. },
  5019. /* .maj (attr_mmap2 only) */
  5020. /* .min (attr_mmap2 only) */
  5021. /* .ino (attr_mmap2 only) */
  5022. /* .ino_generation (attr_mmap2 only) */
  5023. /* .prot (attr_mmap2 only) */
  5024. /* .flags (attr_mmap2 only) */
  5025. };
  5026. perf_event_mmap_event(&mmap_event);
  5027. }
  5028. void perf_event_aux_event(struct perf_event *event, unsigned long head,
  5029. unsigned long size, u64 flags)
  5030. {
  5031. struct perf_output_handle handle;
  5032. struct perf_sample_data sample;
  5033. struct perf_aux_event {
  5034. struct perf_event_header header;
  5035. u64 offset;
  5036. u64 size;
  5037. u64 flags;
  5038. } rec = {
  5039. .header = {
  5040. .type = PERF_RECORD_AUX,
  5041. .misc = 0,
  5042. .size = sizeof(rec),
  5043. },
  5044. .offset = head,
  5045. .size = size,
  5046. .flags = flags,
  5047. };
  5048. int ret;
  5049. perf_event_header__init_id(&rec.header, &sample, event);
  5050. ret = perf_output_begin(&handle, event, rec.header.size);
  5051. if (ret)
  5052. return;
  5053. perf_output_put(&handle, rec);
  5054. perf_event__output_id_sample(event, &handle, &sample);
  5055. perf_output_end(&handle);
  5056. }
  5057. /*
  5058. * Lost/dropped samples logging
  5059. */
  5060. void perf_log_lost_samples(struct perf_event *event, u64 lost)
  5061. {
  5062. struct perf_output_handle handle;
  5063. struct perf_sample_data sample;
  5064. int ret;
  5065. struct {
  5066. struct perf_event_header header;
  5067. u64 lost;
  5068. } lost_samples_event = {
  5069. .header = {
  5070. .type = PERF_RECORD_LOST_SAMPLES,
  5071. .misc = 0,
  5072. .size = sizeof(lost_samples_event),
  5073. },
  5074. .lost = lost,
  5075. };
  5076. perf_event_header__init_id(&lost_samples_event.header, &sample, event);
  5077. ret = perf_output_begin(&handle, event,
  5078. lost_samples_event.header.size);
  5079. if (ret)
  5080. return;
  5081. perf_output_put(&handle, lost_samples_event);
  5082. perf_event__output_id_sample(event, &handle, &sample);
  5083. perf_output_end(&handle);
  5084. }
  5085. /*
  5086. * context_switch tracking
  5087. */
  5088. struct perf_switch_event {
  5089. struct task_struct *task;
  5090. struct task_struct *next_prev;
  5091. struct {
  5092. struct perf_event_header header;
  5093. u32 next_prev_pid;
  5094. u32 next_prev_tid;
  5095. } event_id;
  5096. };
  5097. static int perf_event_switch_match(struct perf_event *event)
  5098. {
  5099. return event->attr.context_switch;
  5100. }
  5101. static void perf_event_switch_output(struct perf_event *event, void *data)
  5102. {
  5103. struct perf_switch_event *se = data;
  5104. struct perf_output_handle handle;
  5105. struct perf_sample_data sample;
  5106. int ret;
  5107. if (!perf_event_switch_match(event))
  5108. return;
  5109. /* Only CPU-wide events are allowed to see next/prev pid/tid */
  5110. if (event->ctx->task) {
  5111. se->event_id.header.type = PERF_RECORD_SWITCH;
  5112. se->event_id.header.size = sizeof(se->event_id.header);
  5113. } else {
  5114. se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
  5115. se->event_id.header.size = sizeof(se->event_id);
  5116. se->event_id.next_prev_pid =
  5117. perf_event_pid(event, se->next_prev);
  5118. se->event_id.next_prev_tid =
  5119. perf_event_tid(event, se->next_prev);
  5120. }
  5121. perf_event_header__init_id(&se->event_id.header, &sample, event);
  5122. ret = perf_output_begin(&handle, event, se->event_id.header.size);
  5123. if (ret)
  5124. return;
  5125. if (event->ctx->task)
  5126. perf_output_put(&handle, se->event_id.header);
  5127. else
  5128. perf_output_put(&handle, se->event_id);
  5129. perf_event__output_id_sample(event, &handle, &sample);
  5130. perf_output_end(&handle);
  5131. }
  5132. static void perf_event_switch(struct task_struct *task,
  5133. struct task_struct *next_prev, bool sched_in)
  5134. {
  5135. struct perf_switch_event switch_event;
  5136. /* N.B. caller checks nr_switch_events != 0 */
  5137. switch_event = (struct perf_switch_event){
  5138. .task = task,
  5139. .next_prev = next_prev,
  5140. .event_id = {
  5141. .header = {
  5142. /* .type */
  5143. .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
  5144. /* .size */
  5145. },
  5146. /* .next_prev_pid */
  5147. /* .next_prev_tid */
  5148. },
  5149. };
  5150. perf_event_aux(perf_event_switch_output,
  5151. &switch_event,
  5152. NULL);
  5153. }
  5154. /*
  5155. * IRQ throttle logging
  5156. */
  5157. static void perf_log_throttle(struct perf_event *event, int enable)
  5158. {
  5159. struct perf_output_handle handle;
  5160. struct perf_sample_data sample;
  5161. int ret;
  5162. struct {
  5163. struct perf_event_header header;
  5164. u64 time;
  5165. u64 id;
  5166. u64 stream_id;
  5167. } throttle_event = {
  5168. .header = {
  5169. .type = PERF_RECORD_THROTTLE,
  5170. .misc = 0,
  5171. .size = sizeof(throttle_event),
  5172. },
  5173. .time = perf_event_clock(event),
  5174. .id = primary_event_id(event),
  5175. .stream_id = event->id,
  5176. };
  5177. if (enable)
  5178. throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
  5179. perf_event_header__init_id(&throttle_event.header, &sample, event);
  5180. ret = perf_output_begin(&handle, event,
  5181. throttle_event.header.size);
  5182. if (ret)
  5183. return;
  5184. perf_output_put(&handle, throttle_event);
  5185. perf_event__output_id_sample(event, &handle, &sample);
  5186. perf_output_end(&handle);
  5187. }
  5188. static void perf_log_itrace_start(struct perf_event *event)
  5189. {
  5190. struct perf_output_handle handle;
  5191. struct perf_sample_data sample;
  5192. struct perf_aux_event {
  5193. struct perf_event_header header;
  5194. u32 pid;
  5195. u32 tid;
  5196. } rec;
  5197. int ret;
  5198. if (event->parent)
  5199. event = event->parent;
  5200. if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
  5201. event->hw.itrace_started)
  5202. return;
  5203. rec.header.type = PERF_RECORD_ITRACE_START;
  5204. rec.header.misc = 0;
  5205. rec.header.size = sizeof(rec);
  5206. rec.pid = perf_event_pid(event, current);
  5207. rec.tid = perf_event_tid(event, current);
  5208. perf_event_header__init_id(&rec.header, &sample, event);
  5209. ret = perf_output_begin(&handle, event, rec.header.size);
  5210. if (ret)
  5211. return;
  5212. perf_output_put(&handle, rec);
  5213. perf_event__output_id_sample(event, &handle, &sample);
  5214. perf_output_end(&handle);
  5215. }
  5216. /*
  5217. * Generic event overflow handling, sampling.
  5218. */
  5219. static int __perf_event_overflow(struct perf_event *event,
  5220. int throttle, struct perf_sample_data *data,
  5221. struct pt_regs *regs)
  5222. {
  5223. int events = atomic_read(&event->event_limit);
  5224. struct hw_perf_event *hwc = &event->hw;
  5225. u64 seq;
  5226. int ret = 0;
  5227. /*
  5228. * Non-sampling counters might still use the PMI to fold short
  5229. * hardware counters, ignore those.
  5230. */
  5231. if (unlikely(!is_sampling_event(event)))
  5232. return 0;
  5233. seq = __this_cpu_read(perf_throttled_seq);
  5234. if (seq != hwc->interrupts_seq) {
  5235. hwc->interrupts_seq = seq;
  5236. hwc->interrupts = 1;
  5237. } else {
  5238. hwc->interrupts++;
  5239. if (unlikely(throttle
  5240. && hwc->interrupts >= max_samples_per_tick)) {
  5241. __this_cpu_inc(perf_throttled_count);
  5242. hwc->interrupts = MAX_INTERRUPTS;
  5243. perf_log_throttle(event, 0);
  5244. tick_nohz_full_kick();
  5245. ret = 1;
  5246. }
  5247. }
  5248. if (event->attr.freq) {
  5249. u64 now = perf_clock();
  5250. s64 delta = now - hwc->freq_time_stamp;
  5251. hwc->freq_time_stamp = now;
  5252. if (delta > 0 && delta < 2*TICK_NSEC)
  5253. perf_adjust_period(event, delta, hwc->last_period, true);
  5254. }
  5255. /*
  5256. * XXX event_limit might not quite work as expected on inherited
  5257. * events
  5258. */
  5259. event->pending_kill = POLL_IN;
  5260. if (events && atomic_dec_and_test(&event->event_limit)) {
  5261. ret = 1;
  5262. event->pending_kill = POLL_HUP;
  5263. event->pending_disable = 1;
  5264. irq_work_queue(&event->pending);
  5265. }
  5266. if (event->overflow_handler)
  5267. event->overflow_handler(event, data, regs);
  5268. else
  5269. perf_event_output(event, data, regs);
  5270. if (*perf_event_fasync(event) && event->pending_kill) {
  5271. event->pending_wakeup = 1;
  5272. irq_work_queue(&event->pending);
  5273. }
  5274. return ret;
  5275. }
  5276. int perf_event_overflow(struct perf_event *event,
  5277. struct perf_sample_data *data,
  5278. struct pt_regs *regs)
  5279. {
  5280. return __perf_event_overflow(event, 1, data, regs);
  5281. }
  5282. /*
  5283. * Generic software event infrastructure
  5284. */
  5285. struct swevent_htable {
  5286. struct swevent_hlist *swevent_hlist;
  5287. struct mutex hlist_mutex;
  5288. int hlist_refcount;
  5289. /* Recursion avoidance in each contexts */
  5290. int recursion[PERF_NR_CONTEXTS];
  5291. };
  5292. static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
  5293. /*
  5294. * We directly increment event->count and keep a second value in
  5295. * event->hw.period_left to count intervals. This period event
  5296. * is kept in the range [-sample_period, 0] so that we can use the
  5297. * sign as trigger.
  5298. */
  5299. u64 perf_swevent_set_period(struct perf_event *event)
  5300. {
  5301. struct hw_perf_event *hwc = &event->hw;
  5302. u64 period = hwc->last_period;
  5303. u64 nr, offset;
  5304. s64 old, val;
  5305. hwc->last_period = hwc->sample_period;
  5306. again:
  5307. old = val = local64_read(&hwc->period_left);
  5308. if (val < 0)
  5309. return 0;
  5310. nr = div64_u64(period + val, period);
  5311. offset = nr * period;
  5312. val -= offset;
  5313. if (local64_cmpxchg(&hwc->period_left, old, val) != old)
  5314. goto again;
  5315. return nr;
  5316. }
  5317. static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
  5318. struct perf_sample_data *data,
  5319. struct pt_regs *regs)
  5320. {
  5321. struct hw_perf_event *hwc = &event->hw;
  5322. int throttle = 0;
  5323. if (!overflow)
  5324. overflow = perf_swevent_set_period(event);
  5325. if (hwc->interrupts == MAX_INTERRUPTS)
  5326. return;
  5327. for (; overflow; overflow--) {
  5328. if (__perf_event_overflow(event, throttle,
  5329. data, regs)) {
  5330. /*
  5331. * We inhibit the overflow from happening when
  5332. * hwc->interrupts == MAX_INTERRUPTS.
  5333. */
  5334. break;
  5335. }
  5336. throttle = 1;
  5337. }
  5338. }
  5339. static void perf_swevent_event(struct perf_event *event, u64 nr,
  5340. struct perf_sample_data *data,
  5341. struct pt_regs *regs)
  5342. {
  5343. struct hw_perf_event *hwc = &event->hw;
  5344. local64_add(nr, &event->count);
  5345. if (!regs)
  5346. return;
  5347. if (!is_sampling_event(event))
  5348. return;
  5349. if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
  5350. data->period = nr;
  5351. return perf_swevent_overflow(event, 1, data, regs);
  5352. } else
  5353. data->period = event->hw.last_period;
  5354. if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  5355. return perf_swevent_overflow(event, 1, data, regs);
  5356. if (local64_add_negative(nr, &hwc->period_left))
  5357. return;
  5358. perf_swevent_overflow(event, 0, data, regs);
  5359. }
  5360. static int perf_exclude_event(struct perf_event *event,
  5361. struct pt_regs *regs)
  5362. {
  5363. if (event->hw.state & PERF_HES_STOPPED)
  5364. return 1;
  5365. if (regs) {
  5366. if (event->attr.exclude_user && user_mode(regs))
  5367. return 1;
  5368. if (event->attr.exclude_kernel && !user_mode(regs))
  5369. return 1;
  5370. }
  5371. return 0;
  5372. }
  5373. static int perf_swevent_match(struct perf_event *event,
  5374. enum perf_type_id type,
  5375. u32 event_id,
  5376. struct perf_sample_data *data,
  5377. struct pt_regs *regs)
  5378. {
  5379. if (event->attr.type != type)
  5380. return 0;
  5381. if (event->attr.config != event_id)
  5382. return 0;
  5383. if (perf_exclude_event(event, regs))
  5384. return 0;
  5385. return 1;
  5386. }
  5387. static inline u64 swevent_hash(u64 type, u32 event_id)
  5388. {
  5389. u64 val = event_id | (type << 32);
  5390. return hash_64(val, SWEVENT_HLIST_BITS);
  5391. }
  5392. static inline struct hlist_head *
  5393. __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
  5394. {
  5395. u64 hash = swevent_hash(type, event_id);
  5396. return &hlist->heads[hash];
  5397. }
  5398. /* For the read side: events when they trigger */
  5399. static inline struct hlist_head *
  5400. find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
  5401. {
  5402. struct swevent_hlist *hlist;
  5403. hlist = rcu_dereference(swhash->swevent_hlist);
  5404. if (!hlist)
  5405. return NULL;
  5406. return __find_swevent_head(hlist, type, event_id);
  5407. }
  5408. /* For the event head insertion and removal in the hlist */
  5409. static inline struct hlist_head *
  5410. find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
  5411. {
  5412. struct swevent_hlist *hlist;
  5413. u32 event_id = event->attr.config;
  5414. u64 type = event->attr.type;
  5415. /*
  5416. * Event scheduling is always serialized against hlist allocation
  5417. * and release. Which makes the protected version suitable here.
  5418. * The context lock guarantees that.
  5419. */
  5420. hlist = rcu_dereference_protected(swhash->swevent_hlist,
  5421. lockdep_is_held(&event->ctx->lock));
  5422. if (!hlist)
  5423. return NULL;
  5424. return __find_swevent_head(hlist, type, event_id);
  5425. }
  5426. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  5427. u64 nr,
  5428. struct perf_sample_data *data,
  5429. struct pt_regs *regs)
  5430. {
  5431. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  5432. struct perf_event *event;
  5433. struct hlist_head *head;
  5434. rcu_read_lock();
  5435. head = find_swevent_head_rcu(swhash, type, event_id);
  5436. if (!head)
  5437. goto end;
  5438. hlist_for_each_entry_rcu(event, head, hlist_entry) {
  5439. if (perf_swevent_match(event, type, event_id, data, regs))
  5440. perf_swevent_event(event, nr, data, regs);
  5441. }
  5442. end:
  5443. rcu_read_unlock();
  5444. }
  5445. DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
  5446. int perf_swevent_get_recursion_context(void)
  5447. {
  5448. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  5449. return get_recursion_context(swhash->recursion);
  5450. }
  5451. EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
  5452. inline void perf_swevent_put_recursion_context(int rctx)
  5453. {
  5454. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  5455. put_recursion_context(swhash->recursion, rctx);
  5456. }
  5457. void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  5458. {
  5459. struct perf_sample_data data;
  5460. if (WARN_ON_ONCE(!regs))
  5461. return;
  5462. perf_sample_data_init(&data, addr, 0);
  5463. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
  5464. }
  5465. void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  5466. {
  5467. int rctx;
  5468. preempt_disable_notrace();
  5469. rctx = perf_swevent_get_recursion_context();
  5470. if (unlikely(rctx < 0))
  5471. goto fail;
  5472. ___perf_sw_event(event_id, nr, regs, addr);
  5473. perf_swevent_put_recursion_context(rctx);
  5474. fail:
  5475. preempt_enable_notrace();
  5476. }
  5477. static void perf_swevent_read(struct perf_event *event)
  5478. {
  5479. }
  5480. static int perf_swevent_add(struct perf_event *event, int flags)
  5481. {
  5482. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  5483. struct hw_perf_event *hwc = &event->hw;
  5484. struct hlist_head *head;
  5485. if (is_sampling_event(event)) {
  5486. hwc->last_period = hwc->sample_period;
  5487. perf_swevent_set_period(event);
  5488. }
  5489. hwc->state = !(flags & PERF_EF_START);
  5490. head = find_swevent_head(swhash, event);
  5491. if (WARN_ON_ONCE(!head))
  5492. return -EINVAL;
  5493. hlist_add_head_rcu(&event->hlist_entry, head);
  5494. perf_event_update_userpage(event);
  5495. return 0;
  5496. }
  5497. static void perf_swevent_del(struct perf_event *event, int flags)
  5498. {
  5499. hlist_del_rcu(&event->hlist_entry);
  5500. }
  5501. static void perf_swevent_start(struct perf_event *event, int flags)
  5502. {
  5503. event->hw.state = 0;
  5504. }
  5505. static void perf_swevent_stop(struct perf_event *event, int flags)
  5506. {
  5507. event->hw.state = PERF_HES_STOPPED;
  5508. }
  5509. /* Deref the hlist from the update side */
  5510. static inline struct swevent_hlist *
  5511. swevent_hlist_deref(struct swevent_htable *swhash)
  5512. {
  5513. return rcu_dereference_protected(swhash->swevent_hlist,
  5514. lockdep_is_held(&swhash->hlist_mutex));
  5515. }
  5516. static void swevent_hlist_release(struct swevent_htable *swhash)
  5517. {
  5518. struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
  5519. if (!hlist)
  5520. return;
  5521. RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
  5522. kfree_rcu(hlist, rcu_head);
  5523. }
  5524. static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
  5525. {
  5526. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5527. mutex_lock(&swhash->hlist_mutex);
  5528. if (!--swhash->hlist_refcount)
  5529. swevent_hlist_release(swhash);
  5530. mutex_unlock(&swhash->hlist_mutex);
  5531. }
  5532. static void swevent_hlist_put(struct perf_event *event)
  5533. {
  5534. int cpu;
  5535. for_each_possible_cpu(cpu)
  5536. swevent_hlist_put_cpu(event, cpu);
  5537. }
  5538. static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
  5539. {
  5540. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5541. int err = 0;
  5542. mutex_lock(&swhash->hlist_mutex);
  5543. if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
  5544. struct swevent_hlist *hlist;
  5545. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  5546. if (!hlist) {
  5547. err = -ENOMEM;
  5548. goto exit;
  5549. }
  5550. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  5551. }
  5552. swhash->hlist_refcount++;
  5553. exit:
  5554. mutex_unlock(&swhash->hlist_mutex);
  5555. return err;
  5556. }
  5557. static int swevent_hlist_get(struct perf_event *event)
  5558. {
  5559. int err;
  5560. int cpu, failed_cpu;
  5561. get_online_cpus();
  5562. for_each_possible_cpu(cpu) {
  5563. err = swevent_hlist_get_cpu(event, cpu);
  5564. if (err) {
  5565. failed_cpu = cpu;
  5566. goto fail;
  5567. }
  5568. }
  5569. put_online_cpus();
  5570. return 0;
  5571. fail:
  5572. for_each_possible_cpu(cpu) {
  5573. if (cpu == failed_cpu)
  5574. break;
  5575. swevent_hlist_put_cpu(event, cpu);
  5576. }
  5577. put_online_cpus();
  5578. return err;
  5579. }
  5580. struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
  5581. static void sw_perf_event_destroy(struct perf_event *event)
  5582. {
  5583. u64 event_id = event->attr.config;
  5584. WARN_ON(event->parent);
  5585. static_key_slow_dec(&perf_swevent_enabled[event_id]);
  5586. swevent_hlist_put(event);
  5587. }
  5588. static int perf_swevent_init(struct perf_event *event)
  5589. {
  5590. u64 event_id = event->attr.config;
  5591. if (event->attr.type != PERF_TYPE_SOFTWARE)
  5592. return -ENOENT;
  5593. /*
  5594. * no branch sampling for software events
  5595. */
  5596. if (has_branch_stack(event))
  5597. return -EOPNOTSUPP;
  5598. switch (event_id) {
  5599. case PERF_COUNT_SW_CPU_CLOCK:
  5600. case PERF_COUNT_SW_TASK_CLOCK:
  5601. return -ENOENT;
  5602. default:
  5603. break;
  5604. }
  5605. if (event_id >= PERF_COUNT_SW_MAX)
  5606. return -ENOENT;
  5607. if (!event->parent) {
  5608. int err;
  5609. err = swevent_hlist_get(event);
  5610. if (err)
  5611. return err;
  5612. static_key_slow_inc(&perf_swevent_enabled[event_id]);
  5613. event->destroy = sw_perf_event_destroy;
  5614. }
  5615. return 0;
  5616. }
  5617. static struct pmu perf_swevent = {
  5618. .task_ctx_nr = perf_sw_context,
  5619. .capabilities = PERF_PMU_CAP_NO_NMI,
  5620. .event_init = perf_swevent_init,
  5621. .add = perf_swevent_add,
  5622. .del = perf_swevent_del,
  5623. .start = perf_swevent_start,
  5624. .stop = perf_swevent_stop,
  5625. .read = perf_swevent_read,
  5626. };
  5627. #ifdef CONFIG_EVENT_TRACING
  5628. static int perf_tp_filter_match(struct perf_event *event,
  5629. struct perf_sample_data *data)
  5630. {
  5631. void *record = data->raw->data;
  5632. /* only top level events have filters set */
  5633. if (event->parent)
  5634. event = event->parent;
  5635. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  5636. return 1;
  5637. return 0;
  5638. }
  5639. static int perf_tp_event_match(struct perf_event *event,
  5640. struct perf_sample_data *data,
  5641. struct pt_regs *regs)
  5642. {
  5643. if (event->hw.state & PERF_HES_STOPPED)
  5644. return 0;
  5645. /*
  5646. * All tracepoints are from kernel-space.
  5647. */
  5648. if (event->attr.exclude_kernel)
  5649. return 0;
  5650. if (!perf_tp_filter_match(event, data))
  5651. return 0;
  5652. return 1;
  5653. }
  5654. void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
  5655. struct pt_regs *regs, struct hlist_head *head, int rctx,
  5656. struct task_struct *task)
  5657. {
  5658. struct perf_sample_data data;
  5659. struct perf_event *event;
  5660. struct perf_raw_record raw = {
  5661. .size = entry_size,
  5662. .data = record,
  5663. };
  5664. perf_sample_data_init(&data, addr, 0);
  5665. data.raw = &raw;
  5666. hlist_for_each_entry_rcu(event, head, hlist_entry) {
  5667. if (perf_tp_event_match(event, &data, regs))
  5668. perf_swevent_event(event, count, &data, regs);
  5669. }
  5670. /*
  5671. * If we got specified a target task, also iterate its context and
  5672. * deliver this event there too.
  5673. */
  5674. if (task && task != current) {
  5675. struct perf_event_context *ctx;
  5676. struct trace_entry *entry = record;
  5677. rcu_read_lock();
  5678. ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
  5679. if (!ctx)
  5680. goto unlock;
  5681. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  5682. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  5683. continue;
  5684. if (event->attr.config != entry->type)
  5685. continue;
  5686. if (perf_tp_event_match(event, &data, regs))
  5687. perf_swevent_event(event, count, &data, regs);
  5688. }
  5689. unlock:
  5690. rcu_read_unlock();
  5691. }
  5692. perf_swevent_put_recursion_context(rctx);
  5693. }
  5694. EXPORT_SYMBOL_GPL(perf_tp_event);
  5695. static void tp_perf_event_destroy(struct perf_event *event)
  5696. {
  5697. perf_trace_destroy(event);
  5698. }
  5699. static int perf_tp_event_init(struct perf_event *event)
  5700. {
  5701. int err;
  5702. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  5703. return -ENOENT;
  5704. /*
  5705. * no branch sampling for tracepoint events
  5706. */
  5707. if (has_branch_stack(event))
  5708. return -EOPNOTSUPP;
  5709. err = perf_trace_init(event);
  5710. if (err)
  5711. return err;
  5712. event->destroy = tp_perf_event_destroy;
  5713. return 0;
  5714. }
  5715. static struct pmu perf_tracepoint = {
  5716. .task_ctx_nr = perf_sw_context,
  5717. .event_init = perf_tp_event_init,
  5718. .add = perf_trace_add,
  5719. .del = perf_trace_del,
  5720. .start = perf_swevent_start,
  5721. .stop = perf_swevent_stop,
  5722. .read = perf_swevent_read,
  5723. };
  5724. static inline void perf_tp_register(void)
  5725. {
  5726. perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
  5727. }
  5728. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  5729. {
  5730. char *filter_str;
  5731. int ret;
  5732. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  5733. return -EINVAL;
  5734. filter_str = strndup_user(arg, PAGE_SIZE);
  5735. if (IS_ERR(filter_str))
  5736. return PTR_ERR(filter_str);
  5737. ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  5738. kfree(filter_str);
  5739. return ret;
  5740. }
  5741. static void perf_event_free_filter(struct perf_event *event)
  5742. {
  5743. ftrace_profile_free_filter(event);
  5744. }
  5745. static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
  5746. {
  5747. struct bpf_prog *prog;
  5748. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  5749. return -EINVAL;
  5750. if (event->tp_event->prog)
  5751. return -EEXIST;
  5752. if (!(event->tp_event->flags & TRACE_EVENT_FL_UKPROBE))
  5753. /* bpf programs can only be attached to u/kprobes */
  5754. return -EINVAL;
  5755. prog = bpf_prog_get(prog_fd);
  5756. if (IS_ERR(prog))
  5757. return PTR_ERR(prog);
  5758. if (prog->type != BPF_PROG_TYPE_KPROBE) {
  5759. /* valid fd, but invalid bpf program type */
  5760. bpf_prog_put(prog);
  5761. return -EINVAL;
  5762. }
  5763. event->tp_event->prog = prog;
  5764. return 0;
  5765. }
  5766. static void perf_event_free_bpf_prog(struct perf_event *event)
  5767. {
  5768. struct bpf_prog *prog;
  5769. if (!event->tp_event)
  5770. return;
  5771. prog = event->tp_event->prog;
  5772. if (prog) {
  5773. event->tp_event->prog = NULL;
  5774. bpf_prog_put(prog);
  5775. }
  5776. }
  5777. #else
  5778. static inline void perf_tp_register(void)
  5779. {
  5780. }
  5781. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  5782. {
  5783. return -ENOENT;
  5784. }
  5785. static void perf_event_free_filter(struct perf_event *event)
  5786. {
  5787. }
  5788. static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
  5789. {
  5790. return -ENOENT;
  5791. }
  5792. static void perf_event_free_bpf_prog(struct perf_event *event)
  5793. {
  5794. }
  5795. #endif /* CONFIG_EVENT_TRACING */
  5796. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  5797. void perf_bp_event(struct perf_event *bp, void *data)
  5798. {
  5799. struct perf_sample_data sample;
  5800. struct pt_regs *regs = data;
  5801. perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
  5802. if (!bp->hw.state && !perf_exclude_event(bp, regs))
  5803. perf_swevent_event(bp, 1, &sample, regs);
  5804. }
  5805. #endif
  5806. /*
  5807. * hrtimer based swevent callback
  5808. */
  5809. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  5810. {
  5811. enum hrtimer_restart ret = HRTIMER_RESTART;
  5812. struct perf_sample_data data;
  5813. struct pt_regs *regs;
  5814. struct perf_event *event;
  5815. u64 period;
  5816. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  5817. if (event->state != PERF_EVENT_STATE_ACTIVE)
  5818. return HRTIMER_NORESTART;
  5819. event->pmu->read(event);
  5820. perf_sample_data_init(&data, 0, event->hw.last_period);
  5821. regs = get_irq_regs();
  5822. if (regs && !perf_exclude_event(event, regs)) {
  5823. if (!(event->attr.exclude_idle && is_idle_task(current)))
  5824. if (__perf_event_overflow(event, 1, &data, regs))
  5825. ret = HRTIMER_NORESTART;
  5826. }
  5827. period = max_t(u64, 10000, event->hw.sample_period);
  5828. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  5829. return ret;
  5830. }
  5831. static void perf_swevent_start_hrtimer(struct perf_event *event)
  5832. {
  5833. struct hw_perf_event *hwc = &event->hw;
  5834. s64 period;
  5835. if (!is_sampling_event(event))
  5836. return;
  5837. period = local64_read(&hwc->period_left);
  5838. if (period) {
  5839. if (period < 0)
  5840. period = 10000;
  5841. local64_set(&hwc->period_left, 0);
  5842. } else {
  5843. period = max_t(u64, 10000, hwc->sample_period);
  5844. }
  5845. hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
  5846. HRTIMER_MODE_REL_PINNED);
  5847. }
  5848. static void perf_swevent_cancel_hrtimer(struct perf_event *event)
  5849. {
  5850. struct hw_perf_event *hwc = &event->hw;
  5851. if (is_sampling_event(event)) {
  5852. ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
  5853. local64_set(&hwc->period_left, ktime_to_ns(remaining));
  5854. hrtimer_cancel(&hwc->hrtimer);
  5855. }
  5856. }
  5857. static void perf_swevent_init_hrtimer(struct perf_event *event)
  5858. {
  5859. struct hw_perf_event *hwc = &event->hw;
  5860. if (!is_sampling_event(event))
  5861. return;
  5862. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  5863. hwc->hrtimer.function = perf_swevent_hrtimer;
  5864. /*
  5865. * Since hrtimers have a fixed rate, we can do a static freq->period
  5866. * mapping and avoid the whole period adjust feedback stuff.
  5867. */
  5868. if (event->attr.freq) {
  5869. long freq = event->attr.sample_freq;
  5870. event->attr.sample_period = NSEC_PER_SEC / freq;
  5871. hwc->sample_period = event->attr.sample_period;
  5872. local64_set(&hwc->period_left, hwc->sample_period);
  5873. hwc->last_period = hwc->sample_period;
  5874. event->attr.freq = 0;
  5875. }
  5876. }
  5877. /*
  5878. * Software event: cpu wall time clock
  5879. */
  5880. static void cpu_clock_event_update(struct perf_event *event)
  5881. {
  5882. s64 prev;
  5883. u64 now;
  5884. now = local_clock();
  5885. prev = local64_xchg(&event->hw.prev_count, now);
  5886. local64_add(now - prev, &event->count);
  5887. }
  5888. static void cpu_clock_event_start(struct perf_event *event, int flags)
  5889. {
  5890. local64_set(&event->hw.prev_count, local_clock());
  5891. perf_swevent_start_hrtimer(event);
  5892. }
  5893. static void cpu_clock_event_stop(struct perf_event *event, int flags)
  5894. {
  5895. perf_swevent_cancel_hrtimer(event);
  5896. cpu_clock_event_update(event);
  5897. }
  5898. static int cpu_clock_event_add(struct perf_event *event, int flags)
  5899. {
  5900. if (flags & PERF_EF_START)
  5901. cpu_clock_event_start(event, flags);
  5902. perf_event_update_userpage(event);
  5903. return 0;
  5904. }
  5905. static void cpu_clock_event_del(struct perf_event *event, int flags)
  5906. {
  5907. cpu_clock_event_stop(event, flags);
  5908. }
  5909. static void cpu_clock_event_read(struct perf_event *event)
  5910. {
  5911. cpu_clock_event_update(event);
  5912. }
  5913. static int cpu_clock_event_init(struct perf_event *event)
  5914. {
  5915. if (event->attr.type != PERF_TYPE_SOFTWARE)
  5916. return -ENOENT;
  5917. if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
  5918. return -ENOENT;
  5919. /*
  5920. * no branch sampling for software events
  5921. */
  5922. if (has_branch_stack(event))
  5923. return -EOPNOTSUPP;
  5924. perf_swevent_init_hrtimer(event);
  5925. return 0;
  5926. }
  5927. static struct pmu perf_cpu_clock = {
  5928. .task_ctx_nr = perf_sw_context,
  5929. .capabilities = PERF_PMU_CAP_NO_NMI,
  5930. .event_init = cpu_clock_event_init,
  5931. .add = cpu_clock_event_add,
  5932. .del = cpu_clock_event_del,
  5933. .start = cpu_clock_event_start,
  5934. .stop = cpu_clock_event_stop,
  5935. .read = cpu_clock_event_read,
  5936. };
  5937. /*
  5938. * Software event: task time clock
  5939. */
  5940. static void task_clock_event_update(struct perf_event *event, u64 now)
  5941. {
  5942. u64 prev;
  5943. s64 delta;
  5944. prev = local64_xchg(&event->hw.prev_count, now);
  5945. delta = now - prev;
  5946. local64_add(delta, &event->count);
  5947. }
  5948. static void task_clock_event_start(struct perf_event *event, int flags)
  5949. {
  5950. local64_set(&event->hw.prev_count, event->ctx->time);
  5951. perf_swevent_start_hrtimer(event);
  5952. }
  5953. static void task_clock_event_stop(struct perf_event *event, int flags)
  5954. {
  5955. perf_swevent_cancel_hrtimer(event);
  5956. task_clock_event_update(event, event->ctx->time);
  5957. }
  5958. static int task_clock_event_add(struct perf_event *event, int flags)
  5959. {
  5960. if (flags & PERF_EF_START)
  5961. task_clock_event_start(event, flags);
  5962. perf_event_update_userpage(event);
  5963. return 0;
  5964. }
  5965. static void task_clock_event_del(struct perf_event *event, int flags)
  5966. {
  5967. task_clock_event_stop(event, PERF_EF_UPDATE);
  5968. }
  5969. static void task_clock_event_read(struct perf_event *event)
  5970. {
  5971. u64 now = perf_clock();
  5972. u64 delta = now - event->ctx->timestamp;
  5973. u64 time = event->ctx->time + delta;
  5974. task_clock_event_update(event, time);
  5975. }
  5976. static int task_clock_event_init(struct perf_event *event)
  5977. {
  5978. if (event->attr.type != PERF_TYPE_SOFTWARE)
  5979. return -ENOENT;
  5980. if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
  5981. return -ENOENT;
  5982. /*
  5983. * no branch sampling for software events
  5984. */
  5985. if (has_branch_stack(event))
  5986. return -EOPNOTSUPP;
  5987. perf_swevent_init_hrtimer(event);
  5988. return 0;
  5989. }
  5990. static struct pmu perf_task_clock = {
  5991. .task_ctx_nr = perf_sw_context,
  5992. .capabilities = PERF_PMU_CAP_NO_NMI,
  5993. .event_init = task_clock_event_init,
  5994. .add = task_clock_event_add,
  5995. .del = task_clock_event_del,
  5996. .start = task_clock_event_start,
  5997. .stop = task_clock_event_stop,
  5998. .read = task_clock_event_read,
  5999. };
  6000. static void perf_pmu_nop_void(struct pmu *pmu)
  6001. {
  6002. }
  6003. static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
  6004. {
  6005. }
  6006. static int perf_pmu_nop_int(struct pmu *pmu)
  6007. {
  6008. return 0;
  6009. }
  6010. static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
  6011. static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
  6012. {
  6013. __this_cpu_write(nop_txn_flags, flags);
  6014. if (flags & ~PERF_PMU_TXN_ADD)
  6015. return;
  6016. perf_pmu_disable(pmu);
  6017. }
  6018. static int perf_pmu_commit_txn(struct pmu *pmu)
  6019. {
  6020. unsigned int flags = __this_cpu_read(nop_txn_flags);
  6021. __this_cpu_write(nop_txn_flags, 0);
  6022. if (flags & ~PERF_PMU_TXN_ADD)
  6023. return 0;
  6024. perf_pmu_enable(pmu);
  6025. return 0;
  6026. }
  6027. static void perf_pmu_cancel_txn(struct pmu *pmu)
  6028. {
  6029. unsigned int flags = __this_cpu_read(nop_txn_flags);
  6030. __this_cpu_write(nop_txn_flags, 0);
  6031. if (flags & ~PERF_PMU_TXN_ADD)
  6032. return;
  6033. perf_pmu_enable(pmu);
  6034. }
  6035. static int perf_event_idx_default(struct perf_event *event)
  6036. {
  6037. return 0;
  6038. }
  6039. /*
  6040. * Ensures all contexts with the same task_ctx_nr have the same
  6041. * pmu_cpu_context too.
  6042. */
  6043. static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
  6044. {
  6045. struct pmu *pmu;
  6046. if (ctxn < 0)
  6047. return NULL;
  6048. list_for_each_entry(pmu, &pmus, entry) {
  6049. if (pmu->task_ctx_nr == ctxn)
  6050. return pmu->pmu_cpu_context;
  6051. }
  6052. return NULL;
  6053. }
  6054. static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
  6055. {
  6056. int cpu;
  6057. for_each_possible_cpu(cpu) {
  6058. struct perf_cpu_context *cpuctx;
  6059. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  6060. if (cpuctx->unique_pmu == old_pmu)
  6061. cpuctx->unique_pmu = pmu;
  6062. }
  6063. }
  6064. static void free_pmu_context(struct pmu *pmu)
  6065. {
  6066. struct pmu *i;
  6067. mutex_lock(&pmus_lock);
  6068. /*
  6069. * Like a real lame refcount.
  6070. */
  6071. list_for_each_entry(i, &pmus, entry) {
  6072. if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
  6073. update_pmu_context(i, pmu);
  6074. goto out;
  6075. }
  6076. }
  6077. free_percpu(pmu->pmu_cpu_context);
  6078. out:
  6079. mutex_unlock(&pmus_lock);
  6080. }
  6081. static struct idr pmu_idr;
  6082. static ssize_t
  6083. type_show(struct device *dev, struct device_attribute *attr, char *page)
  6084. {
  6085. struct pmu *pmu = dev_get_drvdata(dev);
  6086. return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
  6087. }
  6088. static DEVICE_ATTR_RO(type);
  6089. static ssize_t
  6090. perf_event_mux_interval_ms_show(struct device *dev,
  6091. struct device_attribute *attr,
  6092. char *page)
  6093. {
  6094. struct pmu *pmu = dev_get_drvdata(dev);
  6095. return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
  6096. }
  6097. static DEFINE_MUTEX(mux_interval_mutex);
  6098. static ssize_t
  6099. perf_event_mux_interval_ms_store(struct device *dev,
  6100. struct device_attribute *attr,
  6101. const char *buf, size_t count)
  6102. {
  6103. struct pmu *pmu = dev_get_drvdata(dev);
  6104. int timer, cpu, ret;
  6105. ret = kstrtoint(buf, 0, &timer);
  6106. if (ret)
  6107. return ret;
  6108. if (timer < 1)
  6109. return -EINVAL;
  6110. /* same value, noting to do */
  6111. if (timer == pmu->hrtimer_interval_ms)
  6112. return count;
  6113. mutex_lock(&mux_interval_mutex);
  6114. pmu->hrtimer_interval_ms = timer;
  6115. /* update all cpuctx for this PMU */
  6116. get_online_cpus();
  6117. for_each_online_cpu(cpu) {
  6118. struct perf_cpu_context *cpuctx;
  6119. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  6120. cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
  6121. cpu_function_call(cpu,
  6122. (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
  6123. }
  6124. put_online_cpus();
  6125. mutex_unlock(&mux_interval_mutex);
  6126. return count;
  6127. }
  6128. static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
  6129. static struct attribute *pmu_dev_attrs[] = {
  6130. &dev_attr_type.attr,
  6131. &dev_attr_perf_event_mux_interval_ms.attr,
  6132. NULL,
  6133. };
  6134. ATTRIBUTE_GROUPS(pmu_dev);
  6135. static int pmu_bus_running;
  6136. static struct bus_type pmu_bus = {
  6137. .name = "event_source",
  6138. .dev_groups = pmu_dev_groups,
  6139. };
  6140. static void pmu_dev_release(struct device *dev)
  6141. {
  6142. kfree(dev);
  6143. }
  6144. static int pmu_dev_alloc(struct pmu *pmu)
  6145. {
  6146. int ret = -ENOMEM;
  6147. pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  6148. if (!pmu->dev)
  6149. goto out;
  6150. pmu->dev->groups = pmu->attr_groups;
  6151. device_initialize(pmu->dev);
  6152. ret = dev_set_name(pmu->dev, "%s", pmu->name);
  6153. if (ret)
  6154. goto free_dev;
  6155. dev_set_drvdata(pmu->dev, pmu);
  6156. pmu->dev->bus = &pmu_bus;
  6157. pmu->dev->release = pmu_dev_release;
  6158. ret = device_add(pmu->dev);
  6159. if (ret)
  6160. goto free_dev;
  6161. out:
  6162. return ret;
  6163. free_dev:
  6164. put_device(pmu->dev);
  6165. goto out;
  6166. }
  6167. static struct lock_class_key cpuctx_mutex;
  6168. static struct lock_class_key cpuctx_lock;
  6169. int perf_pmu_register(struct pmu *pmu, const char *name, int type)
  6170. {
  6171. int cpu, ret;
  6172. mutex_lock(&pmus_lock);
  6173. ret = -ENOMEM;
  6174. pmu->pmu_disable_count = alloc_percpu(int);
  6175. if (!pmu->pmu_disable_count)
  6176. goto unlock;
  6177. pmu->type = -1;
  6178. if (!name)
  6179. goto skip_type;
  6180. pmu->name = name;
  6181. if (type < 0) {
  6182. type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
  6183. if (type < 0) {
  6184. ret = type;
  6185. goto free_pdc;
  6186. }
  6187. }
  6188. pmu->type = type;
  6189. if (pmu_bus_running) {
  6190. ret = pmu_dev_alloc(pmu);
  6191. if (ret)
  6192. goto free_idr;
  6193. }
  6194. skip_type:
  6195. pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
  6196. if (pmu->pmu_cpu_context)
  6197. goto got_cpu_context;
  6198. ret = -ENOMEM;
  6199. pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
  6200. if (!pmu->pmu_cpu_context)
  6201. goto free_dev;
  6202. for_each_possible_cpu(cpu) {
  6203. struct perf_cpu_context *cpuctx;
  6204. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  6205. __perf_event_init_context(&cpuctx->ctx);
  6206. lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
  6207. lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
  6208. cpuctx->ctx.pmu = pmu;
  6209. __perf_mux_hrtimer_init(cpuctx, cpu);
  6210. cpuctx->unique_pmu = pmu;
  6211. }
  6212. got_cpu_context:
  6213. if (!pmu->start_txn) {
  6214. if (pmu->pmu_enable) {
  6215. /*
  6216. * If we have pmu_enable/pmu_disable calls, install
  6217. * transaction stubs that use that to try and batch
  6218. * hardware accesses.
  6219. */
  6220. pmu->start_txn = perf_pmu_start_txn;
  6221. pmu->commit_txn = perf_pmu_commit_txn;
  6222. pmu->cancel_txn = perf_pmu_cancel_txn;
  6223. } else {
  6224. pmu->start_txn = perf_pmu_nop_txn;
  6225. pmu->commit_txn = perf_pmu_nop_int;
  6226. pmu->cancel_txn = perf_pmu_nop_void;
  6227. }
  6228. }
  6229. if (!pmu->pmu_enable) {
  6230. pmu->pmu_enable = perf_pmu_nop_void;
  6231. pmu->pmu_disable = perf_pmu_nop_void;
  6232. }
  6233. if (!pmu->event_idx)
  6234. pmu->event_idx = perf_event_idx_default;
  6235. list_add_rcu(&pmu->entry, &pmus);
  6236. atomic_set(&pmu->exclusive_cnt, 0);
  6237. ret = 0;
  6238. unlock:
  6239. mutex_unlock(&pmus_lock);
  6240. return ret;
  6241. free_dev:
  6242. device_del(pmu->dev);
  6243. put_device(pmu->dev);
  6244. free_idr:
  6245. if (pmu->type >= PERF_TYPE_MAX)
  6246. idr_remove(&pmu_idr, pmu->type);
  6247. free_pdc:
  6248. free_percpu(pmu->pmu_disable_count);
  6249. goto unlock;
  6250. }
  6251. EXPORT_SYMBOL_GPL(perf_pmu_register);
  6252. void perf_pmu_unregister(struct pmu *pmu)
  6253. {
  6254. mutex_lock(&pmus_lock);
  6255. list_del_rcu(&pmu->entry);
  6256. mutex_unlock(&pmus_lock);
  6257. /*
  6258. * We dereference the pmu list under both SRCU and regular RCU, so
  6259. * synchronize against both of those.
  6260. */
  6261. synchronize_srcu(&pmus_srcu);
  6262. synchronize_rcu();
  6263. free_percpu(pmu->pmu_disable_count);
  6264. if (pmu->type >= PERF_TYPE_MAX)
  6265. idr_remove(&pmu_idr, pmu->type);
  6266. device_del(pmu->dev);
  6267. put_device(pmu->dev);
  6268. free_pmu_context(pmu);
  6269. }
  6270. EXPORT_SYMBOL_GPL(perf_pmu_unregister);
  6271. static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
  6272. {
  6273. struct perf_event_context *ctx = NULL;
  6274. int ret;
  6275. if (!try_module_get(pmu->module))
  6276. return -ENODEV;
  6277. if (event->group_leader != event) {
  6278. /*
  6279. * This ctx->mutex can nest when we're called through
  6280. * inheritance. See the perf_event_ctx_lock_nested() comment.
  6281. */
  6282. ctx = perf_event_ctx_lock_nested(event->group_leader,
  6283. SINGLE_DEPTH_NESTING);
  6284. BUG_ON(!ctx);
  6285. }
  6286. event->pmu = pmu;
  6287. ret = pmu->event_init(event);
  6288. if (ctx)
  6289. perf_event_ctx_unlock(event->group_leader, ctx);
  6290. if (ret)
  6291. module_put(pmu->module);
  6292. return ret;
  6293. }
  6294. static struct pmu *perf_init_event(struct perf_event *event)
  6295. {
  6296. struct pmu *pmu = NULL;
  6297. int idx;
  6298. int ret;
  6299. idx = srcu_read_lock(&pmus_srcu);
  6300. rcu_read_lock();
  6301. pmu = idr_find(&pmu_idr, event->attr.type);
  6302. rcu_read_unlock();
  6303. if (pmu) {
  6304. ret = perf_try_init_event(pmu, event);
  6305. if (ret)
  6306. pmu = ERR_PTR(ret);
  6307. goto unlock;
  6308. }
  6309. list_for_each_entry_rcu(pmu, &pmus, entry) {
  6310. ret = perf_try_init_event(pmu, event);
  6311. if (!ret)
  6312. goto unlock;
  6313. if (ret != -ENOENT) {
  6314. pmu = ERR_PTR(ret);
  6315. goto unlock;
  6316. }
  6317. }
  6318. pmu = ERR_PTR(-ENOENT);
  6319. unlock:
  6320. srcu_read_unlock(&pmus_srcu, idx);
  6321. return pmu;
  6322. }
  6323. static void account_event_cpu(struct perf_event *event, int cpu)
  6324. {
  6325. if (event->parent)
  6326. return;
  6327. if (is_cgroup_event(event))
  6328. atomic_inc(&per_cpu(perf_cgroup_events, cpu));
  6329. }
  6330. static void account_event(struct perf_event *event)
  6331. {
  6332. bool inc = false;
  6333. if (event->parent)
  6334. return;
  6335. if (event->attach_state & PERF_ATTACH_TASK)
  6336. inc = true;
  6337. if (event->attr.mmap || event->attr.mmap_data)
  6338. atomic_inc(&nr_mmap_events);
  6339. if (event->attr.comm)
  6340. atomic_inc(&nr_comm_events);
  6341. if (event->attr.task)
  6342. atomic_inc(&nr_task_events);
  6343. if (event->attr.freq) {
  6344. if (atomic_inc_return(&nr_freq_events) == 1)
  6345. tick_nohz_full_kick_all();
  6346. }
  6347. if (event->attr.context_switch) {
  6348. atomic_inc(&nr_switch_events);
  6349. inc = true;
  6350. }
  6351. if (has_branch_stack(event))
  6352. inc = true;
  6353. if (is_cgroup_event(event))
  6354. inc = true;
  6355. if (inc)
  6356. static_key_slow_inc(&perf_sched_events.key);
  6357. account_event_cpu(event, event->cpu);
  6358. }
  6359. /*
  6360. * Allocate and initialize a event structure
  6361. */
  6362. static struct perf_event *
  6363. perf_event_alloc(struct perf_event_attr *attr, int cpu,
  6364. struct task_struct *task,
  6365. struct perf_event *group_leader,
  6366. struct perf_event *parent_event,
  6367. perf_overflow_handler_t overflow_handler,
  6368. void *context, int cgroup_fd)
  6369. {
  6370. struct pmu *pmu;
  6371. struct perf_event *event;
  6372. struct hw_perf_event *hwc;
  6373. long err = -EINVAL;
  6374. if ((unsigned)cpu >= nr_cpu_ids) {
  6375. if (!task || cpu != -1)
  6376. return ERR_PTR(-EINVAL);
  6377. }
  6378. event = kzalloc(sizeof(*event), GFP_KERNEL);
  6379. if (!event)
  6380. return ERR_PTR(-ENOMEM);
  6381. /*
  6382. * Single events are their own group leaders, with an
  6383. * empty sibling list:
  6384. */
  6385. if (!group_leader)
  6386. group_leader = event;
  6387. mutex_init(&event->child_mutex);
  6388. INIT_LIST_HEAD(&event->child_list);
  6389. INIT_LIST_HEAD(&event->group_entry);
  6390. INIT_LIST_HEAD(&event->event_entry);
  6391. INIT_LIST_HEAD(&event->sibling_list);
  6392. INIT_LIST_HEAD(&event->rb_entry);
  6393. INIT_LIST_HEAD(&event->active_entry);
  6394. INIT_HLIST_NODE(&event->hlist_entry);
  6395. init_waitqueue_head(&event->waitq);
  6396. init_irq_work(&event->pending, perf_pending_event);
  6397. mutex_init(&event->mmap_mutex);
  6398. atomic_long_set(&event->refcount, 1);
  6399. event->cpu = cpu;
  6400. event->attr = *attr;
  6401. event->group_leader = group_leader;
  6402. event->pmu = NULL;
  6403. event->oncpu = -1;
  6404. event->parent = parent_event;
  6405. event->ns = get_pid_ns(task_active_pid_ns(current));
  6406. event->id = atomic64_inc_return(&perf_event_id);
  6407. event->state = PERF_EVENT_STATE_INACTIVE;
  6408. if (task) {
  6409. event->attach_state = PERF_ATTACH_TASK;
  6410. /*
  6411. * XXX pmu::event_init needs to know what task to account to
  6412. * and we cannot use the ctx information because we need the
  6413. * pmu before we get a ctx.
  6414. */
  6415. event->hw.target = task;
  6416. }
  6417. event->clock = &local_clock;
  6418. if (parent_event)
  6419. event->clock = parent_event->clock;
  6420. if (!overflow_handler && parent_event) {
  6421. overflow_handler = parent_event->overflow_handler;
  6422. context = parent_event->overflow_handler_context;
  6423. }
  6424. event->overflow_handler = overflow_handler;
  6425. event->overflow_handler_context = context;
  6426. perf_event__state_init(event);
  6427. pmu = NULL;
  6428. hwc = &event->hw;
  6429. hwc->sample_period = attr->sample_period;
  6430. if (attr->freq && attr->sample_freq)
  6431. hwc->sample_period = 1;
  6432. hwc->last_period = hwc->sample_period;
  6433. local64_set(&hwc->period_left, hwc->sample_period);
  6434. /*
  6435. * we currently do not support PERF_FORMAT_GROUP on inherited events
  6436. */
  6437. if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
  6438. goto err_ns;
  6439. if (!has_branch_stack(event))
  6440. event->attr.branch_sample_type = 0;
  6441. if (cgroup_fd != -1) {
  6442. err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
  6443. if (err)
  6444. goto err_ns;
  6445. }
  6446. pmu = perf_init_event(event);
  6447. if (!pmu)
  6448. goto err_ns;
  6449. else if (IS_ERR(pmu)) {
  6450. err = PTR_ERR(pmu);
  6451. goto err_ns;
  6452. }
  6453. err = exclusive_event_init(event);
  6454. if (err)
  6455. goto err_pmu;
  6456. if (!event->parent) {
  6457. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  6458. err = get_callchain_buffers();
  6459. if (err)
  6460. goto err_per_task;
  6461. }
  6462. }
  6463. return event;
  6464. err_per_task:
  6465. exclusive_event_destroy(event);
  6466. err_pmu:
  6467. if (event->destroy)
  6468. event->destroy(event);
  6469. module_put(pmu->module);
  6470. err_ns:
  6471. if (is_cgroup_event(event))
  6472. perf_detach_cgroup(event);
  6473. if (event->ns)
  6474. put_pid_ns(event->ns);
  6475. kfree(event);
  6476. return ERR_PTR(err);
  6477. }
  6478. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  6479. struct perf_event_attr *attr)
  6480. {
  6481. u32 size;
  6482. int ret;
  6483. if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  6484. return -EFAULT;
  6485. /*
  6486. * zero the full structure, so that a short copy will be nice.
  6487. */
  6488. memset(attr, 0, sizeof(*attr));
  6489. ret = get_user(size, &uattr->size);
  6490. if (ret)
  6491. return ret;
  6492. if (size > PAGE_SIZE) /* silly large */
  6493. goto err_size;
  6494. if (!size) /* abi compat */
  6495. size = PERF_ATTR_SIZE_VER0;
  6496. if (size < PERF_ATTR_SIZE_VER0)
  6497. goto err_size;
  6498. /*
  6499. * If we're handed a bigger struct than we know of,
  6500. * ensure all the unknown bits are 0 - i.e. new
  6501. * user-space does not rely on any kernel feature
  6502. * extensions we dont know about yet.
  6503. */
  6504. if (size > sizeof(*attr)) {
  6505. unsigned char __user *addr;
  6506. unsigned char __user *end;
  6507. unsigned char val;
  6508. addr = (void __user *)uattr + sizeof(*attr);
  6509. end = (void __user *)uattr + size;
  6510. for (; addr < end; addr++) {
  6511. ret = get_user(val, addr);
  6512. if (ret)
  6513. return ret;
  6514. if (val)
  6515. goto err_size;
  6516. }
  6517. size = sizeof(*attr);
  6518. }
  6519. ret = copy_from_user(attr, uattr, size);
  6520. if (ret)
  6521. return -EFAULT;
  6522. if (attr->__reserved_1)
  6523. return -EINVAL;
  6524. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  6525. return -EINVAL;
  6526. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  6527. return -EINVAL;
  6528. if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
  6529. u64 mask = attr->branch_sample_type;
  6530. /* only using defined bits */
  6531. if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
  6532. return -EINVAL;
  6533. /* at least one branch bit must be set */
  6534. if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
  6535. return -EINVAL;
  6536. /* propagate priv level, when not set for branch */
  6537. if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
  6538. /* exclude_kernel checked on syscall entry */
  6539. if (!attr->exclude_kernel)
  6540. mask |= PERF_SAMPLE_BRANCH_KERNEL;
  6541. if (!attr->exclude_user)
  6542. mask |= PERF_SAMPLE_BRANCH_USER;
  6543. if (!attr->exclude_hv)
  6544. mask |= PERF_SAMPLE_BRANCH_HV;
  6545. /*
  6546. * adjust user setting (for HW filter setup)
  6547. */
  6548. attr->branch_sample_type = mask;
  6549. }
  6550. /* privileged levels capture (kernel, hv): check permissions */
  6551. if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
  6552. && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  6553. return -EACCES;
  6554. }
  6555. if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
  6556. ret = perf_reg_validate(attr->sample_regs_user);
  6557. if (ret)
  6558. return ret;
  6559. }
  6560. if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
  6561. if (!arch_perf_have_user_stack_dump())
  6562. return -ENOSYS;
  6563. /*
  6564. * We have __u32 type for the size, but so far
  6565. * we can only use __u16 as maximum due to the
  6566. * __u16 sample size limit.
  6567. */
  6568. if (attr->sample_stack_user >= USHRT_MAX)
  6569. ret = -EINVAL;
  6570. else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
  6571. ret = -EINVAL;
  6572. }
  6573. if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
  6574. ret = perf_reg_validate(attr->sample_regs_intr);
  6575. out:
  6576. return ret;
  6577. err_size:
  6578. put_user(sizeof(*attr), &uattr->size);
  6579. ret = -E2BIG;
  6580. goto out;
  6581. }
  6582. static int
  6583. perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
  6584. {
  6585. struct ring_buffer *rb = NULL;
  6586. int ret = -EINVAL;
  6587. if (!output_event)
  6588. goto set;
  6589. /* don't allow circular references */
  6590. if (event == output_event)
  6591. goto out;
  6592. /*
  6593. * Don't allow cross-cpu buffers
  6594. */
  6595. if (output_event->cpu != event->cpu)
  6596. goto out;
  6597. /*
  6598. * If its not a per-cpu rb, it must be the same task.
  6599. */
  6600. if (output_event->cpu == -1 && output_event->ctx != event->ctx)
  6601. goto out;
  6602. /*
  6603. * Mixing clocks in the same buffer is trouble you don't need.
  6604. */
  6605. if (output_event->clock != event->clock)
  6606. goto out;
  6607. /*
  6608. * If both events generate aux data, they must be on the same PMU
  6609. */
  6610. if (has_aux(event) && has_aux(output_event) &&
  6611. event->pmu != output_event->pmu)
  6612. goto out;
  6613. set:
  6614. mutex_lock(&event->mmap_mutex);
  6615. /* Can't redirect output if we've got an active mmap() */
  6616. if (atomic_read(&event->mmap_count))
  6617. goto unlock;
  6618. if (output_event) {
  6619. /* get the rb we want to redirect to */
  6620. rb = ring_buffer_get(output_event);
  6621. if (!rb)
  6622. goto unlock;
  6623. }
  6624. ring_buffer_attach(event, rb);
  6625. ret = 0;
  6626. unlock:
  6627. mutex_unlock(&event->mmap_mutex);
  6628. out:
  6629. return ret;
  6630. }
  6631. static void mutex_lock_double(struct mutex *a, struct mutex *b)
  6632. {
  6633. if (b < a)
  6634. swap(a, b);
  6635. mutex_lock(a);
  6636. mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
  6637. }
  6638. static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
  6639. {
  6640. bool nmi_safe = false;
  6641. switch (clk_id) {
  6642. case CLOCK_MONOTONIC:
  6643. event->clock = &ktime_get_mono_fast_ns;
  6644. nmi_safe = true;
  6645. break;
  6646. case CLOCK_MONOTONIC_RAW:
  6647. event->clock = &ktime_get_raw_fast_ns;
  6648. nmi_safe = true;
  6649. break;
  6650. case CLOCK_REALTIME:
  6651. event->clock = &ktime_get_real_ns;
  6652. break;
  6653. case CLOCK_BOOTTIME:
  6654. event->clock = &ktime_get_boot_ns;
  6655. break;
  6656. case CLOCK_TAI:
  6657. event->clock = &ktime_get_tai_ns;
  6658. break;
  6659. default:
  6660. return -EINVAL;
  6661. }
  6662. if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
  6663. return -EINVAL;
  6664. return 0;
  6665. }
  6666. /**
  6667. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  6668. *
  6669. * @attr_uptr: event_id type attributes for monitoring/sampling
  6670. * @pid: target pid
  6671. * @cpu: target cpu
  6672. * @group_fd: group leader event fd
  6673. */
  6674. SYSCALL_DEFINE5(perf_event_open,
  6675. struct perf_event_attr __user *, attr_uptr,
  6676. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  6677. {
  6678. struct perf_event *group_leader = NULL, *output_event = NULL;
  6679. struct perf_event *event, *sibling;
  6680. struct perf_event_attr attr;
  6681. struct perf_event_context *ctx, *uninitialized_var(gctx);
  6682. struct file *event_file = NULL;
  6683. struct fd group = {NULL, 0};
  6684. struct task_struct *task = NULL;
  6685. struct pmu *pmu;
  6686. int event_fd;
  6687. int move_group = 0;
  6688. int err;
  6689. int f_flags = O_RDWR;
  6690. int cgroup_fd = -1;
  6691. /* for future expandability... */
  6692. if (flags & ~PERF_FLAG_ALL)
  6693. return -EINVAL;
  6694. err = perf_copy_attr(attr_uptr, &attr);
  6695. if (err)
  6696. return err;
  6697. if (!attr.exclude_kernel) {
  6698. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  6699. return -EACCES;
  6700. }
  6701. if (attr.freq) {
  6702. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  6703. return -EINVAL;
  6704. } else {
  6705. if (attr.sample_period & (1ULL << 63))
  6706. return -EINVAL;
  6707. }
  6708. /*
  6709. * In cgroup mode, the pid argument is used to pass the fd
  6710. * opened to the cgroup directory in cgroupfs. The cpu argument
  6711. * designates the cpu on which to monitor threads from that
  6712. * cgroup.
  6713. */
  6714. if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
  6715. return -EINVAL;
  6716. if (flags & PERF_FLAG_FD_CLOEXEC)
  6717. f_flags |= O_CLOEXEC;
  6718. event_fd = get_unused_fd_flags(f_flags);
  6719. if (event_fd < 0)
  6720. return event_fd;
  6721. if (group_fd != -1) {
  6722. err = perf_fget_light(group_fd, &group);
  6723. if (err)
  6724. goto err_fd;
  6725. group_leader = group.file->private_data;
  6726. if (flags & PERF_FLAG_FD_OUTPUT)
  6727. output_event = group_leader;
  6728. if (flags & PERF_FLAG_FD_NO_GROUP)
  6729. group_leader = NULL;
  6730. }
  6731. if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
  6732. task = find_lively_task_by_vpid(pid);
  6733. if (IS_ERR(task)) {
  6734. err = PTR_ERR(task);
  6735. goto err_group_fd;
  6736. }
  6737. }
  6738. if (task && group_leader &&
  6739. group_leader->attr.inherit != attr.inherit) {
  6740. err = -EINVAL;
  6741. goto err_task;
  6742. }
  6743. get_online_cpus();
  6744. if (flags & PERF_FLAG_PID_CGROUP)
  6745. cgroup_fd = pid;
  6746. event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
  6747. NULL, NULL, cgroup_fd);
  6748. if (IS_ERR(event)) {
  6749. err = PTR_ERR(event);
  6750. goto err_cpus;
  6751. }
  6752. if (is_sampling_event(event)) {
  6753. if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
  6754. err = -ENOTSUPP;
  6755. goto err_alloc;
  6756. }
  6757. }
  6758. account_event(event);
  6759. /*
  6760. * Special case software events and allow them to be part of
  6761. * any hardware group.
  6762. */
  6763. pmu = event->pmu;
  6764. if (attr.use_clockid) {
  6765. err = perf_event_set_clock(event, attr.clockid);
  6766. if (err)
  6767. goto err_alloc;
  6768. }
  6769. if (group_leader &&
  6770. (is_software_event(event) != is_software_event(group_leader))) {
  6771. if (is_software_event(event)) {
  6772. /*
  6773. * If event and group_leader are not both a software
  6774. * event, and event is, then group leader is not.
  6775. *
  6776. * Allow the addition of software events to !software
  6777. * groups, this is safe because software events never
  6778. * fail to schedule.
  6779. */
  6780. pmu = group_leader->pmu;
  6781. } else if (is_software_event(group_leader) &&
  6782. (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
  6783. /*
  6784. * In case the group is a pure software group, and we
  6785. * try to add a hardware event, move the whole group to
  6786. * the hardware context.
  6787. */
  6788. move_group = 1;
  6789. }
  6790. }
  6791. /*
  6792. * Get the target context (task or percpu):
  6793. */
  6794. ctx = find_get_context(pmu, task, event);
  6795. if (IS_ERR(ctx)) {
  6796. err = PTR_ERR(ctx);
  6797. goto err_alloc;
  6798. }
  6799. if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
  6800. err = -EBUSY;
  6801. goto err_context;
  6802. }
  6803. if (task) {
  6804. put_task_struct(task);
  6805. task = NULL;
  6806. }
  6807. /*
  6808. * Look up the group leader (we will attach this event to it):
  6809. */
  6810. if (group_leader) {
  6811. err = -EINVAL;
  6812. /*
  6813. * Do not allow a recursive hierarchy (this new sibling
  6814. * becoming part of another group-sibling):
  6815. */
  6816. if (group_leader->group_leader != group_leader)
  6817. goto err_context;
  6818. /* All events in a group should have the same clock */
  6819. if (group_leader->clock != event->clock)
  6820. goto err_context;
  6821. /*
  6822. * Do not allow to attach to a group in a different
  6823. * task or CPU context:
  6824. */
  6825. if (move_group) {
  6826. /*
  6827. * Make sure we're both on the same task, or both
  6828. * per-cpu events.
  6829. */
  6830. if (group_leader->ctx->task != ctx->task)
  6831. goto err_context;
  6832. /*
  6833. * Make sure we're both events for the same CPU;
  6834. * grouping events for different CPUs is broken; since
  6835. * you can never concurrently schedule them anyhow.
  6836. */
  6837. if (group_leader->cpu != event->cpu)
  6838. goto err_context;
  6839. } else {
  6840. if (group_leader->ctx != ctx)
  6841. goto err_context;
  6842. }
  6843. /*
  6844. * Only a group leader can be exclusive or pinned
  6845. */
  6846. if (attr.exclusive || attr.pinned)
  6847. goto err_context;
  6848. }
  6849. if (output_event) {
  6850. err = perf_event_set_output(event, output_event);
  6851. if (err)
  6852. goto err_context;
  6853. }
  6854. event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
  6855. f_flags);
  6856. if (IS_ERR(event_file)) {
  6857. err = PTR_ERR(event_file);
  6858. goto err_context;
  6859. }
  6860. if (move_group) {
  6861. gctx = group_leader->ctx;
  6862. mutex_lock_double(&gctx->mutex, &ctx->mutex);
  6863. } else {
  6864. mutex_lock(&ctx->mutex);
  6865. }
  6866. if (!perf_event_validate_size(event)) {
  6867. err = -E2BIG;
  6868. goto err_locked;
  6869. }
  6870. /*
  6871. * Must be under the same ctx::mutex as perf_install_in_context(),
  6872. * because we need to serialize with concurrent event creation.
  6873. */
  6874. if (!exclusive_event_installable(event, ctx)) {
  6875. /* exclusive and group stuff are assumed mutually exclusive */
  6876. WARN_ON_ONCE(move_group);
  6877. err = -EBUSY;
  6878. goto err_locked;
  6879. }
  6880. WARN_ON_ONCE(ctx->parent_ctx);
  6881. if (move_group) {
  6882. /*
  6883. * See perf_event_ctx_lock() for comments on the details
  6884. * of swizzling perf_event::ctx.
  6885. */
  6886. perf_remove_from_context(group_leader, 0);
  6887. list_for_each_entry(sibling, &group_leader->sibling_list,
  6888. group_entry) {
  6889. perf_remove_from_context(sibling, 0);
  6890. put_ctx(gctx);
  6891. }
  6892. /*
  6893. * Wait for everybody to stop referencing the events through
  6894. * the old lists, before installing it on new lists.
  6895. */
  6896. synchronize_rcu();
  6897. /*
  6898. * Install the group siblings before the group leader.
  6899. *
  6900. * Because a group leader will try and install the entire group
  6901. * (through the sibling list, which is still in-tact), we can
  6902. * end up with siblings installed in the wrong context.
  6903. *
  6904. * By installing siblings first we NO-OP because they're not
  6905. * reachable through the group lists.
  6906. */
  6907. list_for_each_entry(sibling, &group_leader->sibling_list,
  6908. group_entry) {
  6909. perf_event__state_init(sibling);
  6910. perf_install_in_context(ctx, sibling, sibling->cpu);
  6911. get_ctx(ctx);
  6912. }
  6913. /*
  6914. * Removing from the context ends up with disabled
  6915. * event. What we want here is event in the initial
  6916. * startup state, ready to be add into new context.
  6917. */
  6918. perf_event__state_init(group_leader);
  6919. perf_install_in_context(ctx, group_leader, group_leader->cpu);
  6920. get_ctx(ctx);
  6921. /*
  6922. * Now that all events are installed in @ctx, nothing
  6923. * references @gctx anymore, so drop the last reference we have
  6924. * on it.
  6925. */
  6926. put_ctx(gctx);
  6927. }
  6928. /*
  6929. * Precalculate sample_data sizes; do while holding ctx::mutex such
  6930. * that we're serialized against further additions and before
  6931. * perf_install_in_context() which is the point the event is active and
  6932. * can use these values.
  6933. */
  6934. perf_event__header_size(event);
  6935. perf_event__id_header_size(event);
  6936. event->owner = current;
  6937. perf_install_in_context(ctx, event, event->cpu);
  6938. perf_unpin_context(ctx);
  6939. if (move_group)
  6940. mutex_unlock(&gctx->mutex);
  6941. mutex_unlock(&ctx->mutex);
  6942. put_online_cpus();
  6943. mutex_lock(&current->perf_event_mutex);
  6944. list_add_tail(&event->owner_entry, &current->perf_event_list);
  6945. mutex_unlock(&current->perf_event_mutex);
  6946. /*
  6947. * Drop the reference on the group_event after placing the
  6948. * new event on the sibling_list. This ensures destruction
  6949. * of the group leader will find the pointer to itself in
  6950. * perf_group_detach().
  6951. */
  6952. fdput(group);
  6953. fd_install(event_fd, event_file);
  6954. return event_fd;
  6955. err_locked:
  6956. if (move_group)
  6957. mutex_unlock(&gctx->mutex);
  6958. mutex_unlock(&ctx->mutex);
  6959. /* err_file: */
  6960. fput(event_file);
  6961. err_context:
  6962. perf_unpin_context(ctx);
  6963. put_ctx(ctx);
  6964. err_alloc:
  6965. free_event(event);
  6966. err_cpus:
  6967. put_online_cpus();
  6968. err_task:
  6969. if (task)
  6970. put_task_struct(task);
  6971. err_group_fd:
  6972. fdput(group);
  6973. err_fd:
  6974. put_unused_fd(event_fd);
  6975. return err;
  6976. }
  6977. /**
  6978. * perf_event_create_kernel_counter
  6979. *
  6980. * @attr: attributes of the counter to create
  6981. * @cpu: cpu in which the counter is bound
  6982. * @task: task to profile (NULL for percpu)
  6983. */
  6984. struct perf_event *
  6985. perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  6986. struct task_struct *task,
  6987. perf_overflow_handler_t overflow_handler,
  6988. void *context)
  6989. {
  6990. struct perf_event_context *ctx;
  6991. struct perf_event *event;
  6992. int err;
  6993. /*
  6994. * Get the target context (task or percpu):
  6995. */
  6996. event = perf_event_alloc(attr, cpu, task, NULL, NULL,
  6997. overflow_handler, context, -1);
  6998. if (IS_ERR(event)) {
  6999. err = PTR_ERR(event);
  7000. goto err;
  7001. }
  7002. /* Mark owner so we could distinguish it from user events. */
  7003. event->owner = TASK_TOMBSTONE;
  7004. account_event(event);
  7005. ctx = find_get_context(event->pmu, task, event);
  7006. if (IS_ERR(ctx)) {
  7007. err = PTR_ERR(ctx);
  7008. goto err_free;
  7009. }
  7010. WARN_ON_ONCE(ctx->parent_ctx);
  7011. mutex_lock(&ctx->mutex);
  7012. if (!exclusive_event_installable(event, ctx)) {
  7013. mutex_unlock(&ctx->mutex);
  7014. perf_unpin_context(ctx);
  7015. put_ctx(ctx);
  7016. err = -EBUSY;
  7017. goto err_free;
  7018. }
  7019. perf_install_in_context(ctx, event, cpu);
  7020. perf_unpin_context(ctx);
  7021. mutex_unlock(&ctx->mutex);
  7022. return event;
  7023. err_free:
  7024. free_event(event);
  7025. err:
  7026. return ERR_PTR(err);
  7027. }
  7028. EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
  7029. void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
  7030. {
  7031. struct perf_event_context *src_ctx;
  7032. struct perf_event_context *dst_ctx;
  7033. struct perf_event *event, *tmp;
  7034. LIST_HEAD(events);
  7035. src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
  7036. dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
  7037. /*
  7038. * See perf_event_ctx_lock() for comments on the details
  7039. * of swizzling perf_event::ctx.
  7040. */
  7041. mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
  7042. list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
  7043. event_entry) {
  7044. perf_remove_from_context(event, 0);
  7045. unaccount_event_cpu(event, src_cpu);
  7046. put_ctx(src_ctx);
  7047. list_add(&event->migrate_entry, &events);
  7048. }
  7049. /*
  7050. * Wait for the events to quiesce before re-instating them.
  7051. */
  7052. synchronize_rcu();
  7053. /*
  7054. * Re-instate events in 2 passes.
  7055. *
  7056. * Skip over group leaders and only install siblings on this first
  7057. * pass, siblings will not get enabled without a leader, however a
  7058. * leader will enable its siblings, even if those are still on the old
  7059. * context.
  7060. */
  7061. list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
  7062. if (event->group_leader == event)
  7063. continue;
  7064. list_del(&event->migrate_entry);
  7065. if (event->state >= PERF_EVENT_STATE_OFF)
  7066. event->state = PERF_EVENT_STATE_INACTIVE;
  7067. account_event_cpu(event, dst_cpu);
  7068. perf_install_in_context(dst_ctx, event, dst_cpu);
  7069. get_ctx(dst_ctx);
  7070. }
  7071. /*
  7072. * Once all the siblings are setup properly, install the group leaders
  7073. * to make it go.
  7074. */
  7075. list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
  7076. list_del(&event->migrate_entry);
  7077. if (event->state >= PERF_EVENT_STATE_OFF)
  7078. event->state = PERF_EVENT_STATE_INACTIVE;
  7079. account_event_cpu(event, dst_cpu);
  7080. perf_install_in_context(dst_ctx, event, dst_cpu);
  7081. get_ctx(dst_ctx);
  7082. }
  7083. mutex_unlock(&dst_ctx->mutex);
  7084. mutex_unlock(&src_ctx->mutex);
  7085. }
  7086. EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
  7087. static void sync_child_event(struct perf_event *child_event,
  7088. struct task_struct *child)
  7089. {
  7090. struct perf_event *parent_event = child_event->parent;
  7091. u64 child_val;
  7092. if (child_event->attr.inherit_stat)
  7093. perf_event_read_event(child_event, child);
  7094. child_val = perf_event_count(child_event);
  7095. /*
  7096. * Add back the child's count to the parent's count:
  7097. */
  7098. atomic64_add(child_val, &parent_event->child_count);
  7099. atomic64_add(child_event->total_time_enabled,
  7100. &parent_event->child_total_time_enabled);
  7101. atomic64_add(child_event->total_time_running,
  7102. &parent_event->child_total_time_running);
  7103. }
  7104. static void
  7105. perf_event_exit_event(struct perf_event *child_event,
  7106. struct perf_event_context *child_ctx,
  7107. struct task_struct *child)
  7108. {
  7109. struct perf_event *parent_event = child_event->parent;
  7110. /*
  7111. * Do not destroy the 'original' grouping; because of the context
  7112. * switch optimization the original events could've ended up in a
  7113. * random child task.
  7114. *
  7115. * If we were to destroy the original group, all group related
  7116. * operations would cease to function properly after this random
  7117. * child dies.
  7118. *
  7119. * Do destroy all inherited groups, we don't care about those
  7120. * and being thorough is better.
  7121. */
  7122. raw_spin_lock_irq(&child_ctx->lock);
  7123. WARN_ON_ONCE(child_ctx->is_active);
  7124. if (parent_event)
  7125. perf_group_detach(child_event);
  7126. list_del_event(child_event, child_ctx);
  7127. child_event->state = PERF_EVENT_STATE_EXIT; /* see perf_event_release_kernel() */
  7128. raw_spin_unlock_irq(&child_ctx->lock);
  7129. /*
  7130. * Parent events are governed by their filedesc, retain them.
  7131. */
  7132. if (!parent_event) {
  7133. perf_event_wakeup(child_event);
  7134. return;
  7135. }
  7136. /*
  7137. * Child events can be cleaned up.
  7138. */
  7139. sync_child_event(child_event, child);
  7140. /*
  7141. * Remove this event from the parent's list
  7142. */
  7143. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  7144. mutex_lock(&parent_event->child_mutex);
  7145. list_del_init(&child_event->child_list);
  7146. mutex_unlock(&parent_event->child_mutex);
  7147. /*
  7148. * Kick perf_poll() for is_event_hup().
  7149. */
  7150. perf_event_wakeup(parent_event);
  7151. free_event(child_event);
  7152. put_event(parent_event);
  7153. }
  7154. static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
  7155. {
  7156. struct perf_event_context *child_ctx, *clone_ctx = NULL;
  7157. struct perf_event *child_event, *next;
  7158. WARN_ON_ONCE(child != current);
  7159. child_ctx = perf_pin_task_context(child, ctxn);
  7160. if (!child_ctx)
  7161. return;
  7162. /*
  7163. * In order to reduce the amount of tricky in ctx tear-down, we hold
  7164. * ctx::mutex over the entire thing. This serializes against almost
  7165. * everything that wants to access the ctx.
  7166. *
  7167. * The exception is sys_perf_event_open() /
  7168. * perf_event_create_kernel_count() which does find_get_context()
  7169. * without ctx::mutex (it cannot because of the move_group double mutex
  7170. * lock thing). See the comments in perf_install_in_context().
  7171. */
  7172. mutex_lock(&child_ctx->mutex);
  7173. /*
  7174. * In a single ctx::lock section, de-schedule the events and detach the
  7175. * context from the task such that we cannot ever get it scheduled back
  7176. * in.
  7177. */
  7178. raw_spin_lock_irq(&child_ctx->lock);
  7179. task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
  7180. /*
  7181. * Now that the context is inactive, destroy the task <-> ctx relation
  7182. * and mark the context dead.
  7183. */
  7184. RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
  7185. put_ctx(child_ctx); /* cannot be last */
  7186. WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
  7187. put_task_struct(current); /* cannot be last */
  7188. clone_ctx = unclone_ctx(child_ctx);
  7189. raw_spin_unlock_irq(&child_ctx->lock);
  7190. if (clone_ctx)
  7191. put_ctx(clone_ctx);
  7192. /*
  7193. * Report the task dead after unscheduling the events so that we
  7194. * won't get any samples after PERF_RECORD_EXIT. We can however still
  7195. * get a few PERF_RECORD_READ events.
  7196. */
  7197. perf_event_task(child, child_ctx, 0);
  7198. list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
  7199. perf_event_exit_event(child_event, child_ctx, child);
  7200. mutex_unlock(&child_ctx->mutex);
  7201. put_ctx(child_ctx);
  7202. }
  7203. /*
  7204. * When a child task exits, feed back event values to parent events.
  7205. */
  7206. void perf_event_exit_task(struct task_struct *child)
  7207. {
  7208. struct perf_event *event, *tmp;
  7209. int ctxn;
  7210. mutex_lock(&child->perf_event_mutex);
  7211. list_for_each_entry_safe(event, tmp, &child->perf_event_list,
  7212. owner_entry) {
  7213. list_del_init(&event->owner_entry);
  7214. /*
  7215. * Ensure the list deletion is visible before we clear
  7216. * the owner, closes a race against perf_release() where
  7217. * we need to serialize on the owner->perf_event_mutex.
  7218. */
  7219. smp_store_release(&event->owner, NULL);
  7220. }
  7221. mutex_unlock(&child->perf_event_mutex);
  7222. for_each_task_context_nr(ctxn)
  7223. perf_event_exit_task_context(child, ctxn);
  7224. /*
  7225. * The perf_event_exit_task_context calls perf_event_task
  7226. * with child's task_ctx, which generates EXIT events for
  7227. * child contexts and sets child->perf_event_ctxp[] to NULL.
  7228. * At this point we need to send EXIT events to cpu contexts.
  7229. */
  7230. perf_event_task(child, NULL, 0);
  7231. }
  7232. static void perf_free_event(struct perf_event *event,
  7233. struct perf_event_context *ctx)
  7234. {
  7235. struct perf_event *parent = event->parent;
  7236. if (WARN_ON_ONCE(!parent))
  7237. return;
  7238. mutex_lock(&parent->child_mutex);
  7239. list_del_init(&event->child_list);
  7240. mutex_unlock(&parent->child_mutex);
  7241. put_event(parent);
  7242. raw_spin_lock_irq(&ctx->lock);
  7243. perf_group_detach(event);
  7244. list_del_event(event, ctx);
  7245. raw_spin_unlock_irq(&ctx->lock);
  7246. free_event(event);
  7247. }
  7248. /*
  7249. * Free an unexposed, unused context as created by inheritance by
  7250. * perf_event_init_task below, used by fork() in case of fail.
  7251. *
  7252. * Not all locks are strictly required, but take them anyway to be nice and
  7253. * help out with the lockdep assertions.
  7254. */
  7255. void perf_event_free_task(struct task_struct *task)
  7256. {
  7257. struct perf_event_context *ctx;
  7258. struct perf_event *event, *tmp;
  7259. int ctxn;
  7260. for_each_task_context_nr(ctxn) {
  7261. ctx = task->perf_event_ctxp[ctxn];
  7262. if (!ctx)
  7263. continue;
  7264. mutex_lock(&ctx->mutex);
  7265. again:
  7266. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
  7267. group_entry)
  7268. perf_free_event(event, ctx);
  7269. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
  7270. group_entry)
  7271. perf_free_event(event, ctx);
  7272. if (!list_empty(&ctx->pinned_groups) ||
  7273. !list_empty(&ctx->flexible_groups))
  7274. goto again;
  7275. mutex_unlock(&ctx->mutex);
  7276. put_ctx(ctx);
  7277. }
  7278. }
  7279. void perf_event_delayed_put(struct task_struct *task)
  7280. {
  7281. int ctxn;
  7282. for_each_task_context_nr(ctxn)
  7283. WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
  7284. }
  7285. struct file *perf_event_get(unsigned int fd)
  7286. {
  7287. struct file *file;
  7288. file = fget_raw(fd);
  7289. if (!file)
  7290. return ERR_PTR(-EBADF);
  7291. if (file->f_op != &perf_fops) {
  7292. fput(file);
  7293. return ERR_PTR(-EBADF);
  7294. }
  7295. return file;
  7296. }
  7297. const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
  7298. {
  7299. if (!event)
  7300. return ERR_PTR(-EINVAL);
  7301. return &event->attr;
  7302. }
  7303. /*
  7304. * inherit a event from parent task to child task:
  7305. */
  7306. static struct perf_event *
  7307. inherit_event(struct perf_event *parent_event,
  7308. struct task_struct *parent,
  7309. struct perf_event_context *parent_ctx,
  7310. struct task_struct *child,
  7311. struct perf_event *group_leader,
  7312. struct perf_event_context *child_ctx)
  7313. {
  7314. enum perf_event_active_state parent_state = parent_event->state;
  7315. struct perf_event *child_event;
  7316. unsigned long flags;
  7317. /*
  7318. * Instead of creating recursive hierarchies of events,
  7319. * we link inherited events back to the original parent,
  7320. * which has a filp for sure, which we use as the reference
  7321. * count:
  7322. */
  7323. if (parent_event->parent)
  7324. parent_event = parent_event->parent;
  7325. child_event = perf_event_alloc(&parent_event->attr,
  7326. parent_event->cpu,
  7327. child,
  7328. group_leader, parent_event,
  7329. NULL, NULL, -1);
  7330. if (IS_ERR(child_event))
  7331. return child_event;
  7332. /*
  7333. * is_orphaned_event() and list_add_tail(&parent_event->child_list)
  7334. * must be under the same lock in order to serialize against
  7335. * perf_event_release_kernel(), such that either we must observe
  7336. * is_orphaned_event() or they will observe us on the child_list.
  7337. */
  7338. mutex_lock(&parent_event->child_mutex);
  7339. if (is_orphaned_event(parent_event) ||
  7340. !atomic_long_inc_not_zero(&parent_event->refcount)) {
  7341. mutex_unlock(&parent_event->child_mutex);
  7342. free_event(child_event);
  7343. return NULL;
  7344. }
  7345. get_ctx(child_ctx);
  7346. /*
  7347. * Make the child state follow the state of the parent event,
  7348. * not its attr.disabled bit. We hold the parent's mutex,
  7349. * so we won't race with perf_event_{en, dis}able_family.
  7350. */
  7351. if (parent_state >= PERF_EVENT_STATE_INACTIVE)
  7352. child_event->state = PERF_EVENT_STATE_INACTIVE;
  7353. else
  7354. child_event->state = PERF_EVENT_STATE_OFF;
  7355. if (parent_event->attr.freq) {
  7356. u64 sample_period = parent_event->hw.sample_period;
  7357. struct hw_perf_event *hwc = &child_event->hw;
  7358. hwc->sample_period = sample_period;
  7359. hwc->last_period = sample_period;
  7360. local64_set(&hwc->period_left, sample_period);
  7361. }
  7362. child_event->ctx = child_ctx;
  7363. child_event->overflow_handler = parent_event->overflow_handler;
  7364. child_event->overflow_handler_context
  7365. = parent_event->overflow_handler_context;
  7366. /*
  7367. * Precalculate sample_data sizes
  7368. */
  7369. perf_event__header_size(child_event);
  7370. perf_event__id_header_size(child_event);
  7371. /*
  7372. * Link it up in the child's context:
  7373. */
  7374. raw_spin_lock_irqsave(&child_ctx->lock, flags);
  7375. add_event_to_ctx(child_event, child_ctx);
  7376. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  7377. /*
  7378. * Link this into the parent event's child list
  7379. */
  7380. list_add_tail(&child_event->child_list, &parent_event->child_list);
  7381. mutex_unlock(&parent_event->child_mutex);
  7382. return child_event;
  7383. }
  7384. static int inherit_group(struct perf_event *parent_event,
  7385. struct task_struct *parent,
  7386. struct perf_event_context *parent_ctx,
  7387. struct task_struct *child,
  7388. struct perf_event_context *child_ctx)
  7389. {
  7390. struct perf_event *leader;
  7391. struct perf_event *sub;
  7392. struct perf_event *child_ctr;
  7393. leader = inherit_event(parent_event, parent, parent_ctx,
  7394. child, NULL, child_ctx);
  7395. if (IS_ERR(leader))
  7396. return PTR_ERR(leader);
  7397. list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  7398. child_ctr = inherit_event(sub, parent, parent_ctx,
  7399. child, leader, child_ctx);
  7400. if (IS_ERR(child_ctr))
  7401. return PTR_ERR(child_ctr);
  7402. }
  7403. return 0;
  7404. }
  7405. static int
  7406. inherit_task_group(struct perf_event *event, struct task_struct *parent,
  7407. struct perf_event_context *parent_ctx,
  7408. struct task_struct *child, int ctxn,
  7409. int *inherited_all)
  7410. {
  7411. int ret;
  7412. struct perf_event_context *child_ctx;
  7413. if (!event->attr.inherit) {
  7414. *inherited_all = 0;
  7415. return 0;
  7416. }
  7417. child_ctx = child->perf_event_ctxp[ctxn];
  7418. if (!child_ctx) {
  7419. /*
  7420. * This is executed from the parent task context, so
  7421. * inherit events that have been marked for cloning.
  7422. * First allocate and initialize a context for the
  7423. * child.
  7424. */
  7425. child_ctx = alloc_perf_context(parent_ctx->pmu, child);
  7426. if (!child_ctx)
  7427. return -ENOMEM;
  7428. child->perf_event_ctxp[ctxn] = child_ctx;
  7429. }
  7430. ret = inherit_group(event, parent, parent_ctx,
  7431. child, child_ctx);
  7432. if (ret)
  7433. *inherited_all = 0;
  7434. return ret;
  7435. }
  7436. /*
  7437. * Initialize the perf_event context in task_struct
  7438. */
  7439. static int perf_event_init_context(struct task_struct *child, int ctxn)
  7440. {
  7441. struct perf_event_context *child_ctx, *parent_ctx;
  7442. struct perf_event_context *cloned_ctx;
  7443. struct perf_event *event;
  7444. struct task_struct *parent = current;
  7445. int inherited_all = 1;
  7446. unsigned long flags;
  7447. int ret = 0;
  7448. if (likely(!parent->perf_event_ctxp[ctxn]))
  7449. return 0;
  7450. /*
  7451. * If the parent's context is a clone, pin it so it won't get
  7452. * swapped under us.
  7453. */
  7454. parent_ctx = perf_pin_task_context(parent, ctxn);
  7455. if (!parent_ctx)
  7456. return 0;
  7457. /*
  7458. * No need to check if parent_ctx != NULL here; since we saw
  7459. * it non-NULL earlier, the only reason for it to become NULL
  7460. * is if we exit, and since we're currently in the middle of
  7461. * a fork we can't be exiting at the same time.
  7462. */
  7463. /*
  7464. * Lock the parent list. No need to lock the child - not PID
  7465. * hashed yet and not running, so nobody can access it.
  7466. */
  7467. mutex_lock(&parent_ctx->mutex);
  7468. /*
  7469. * We dont have to disable NMIs - we are only looking at
  7470. * the list, not manipulating it:
  7471. */
  7472. list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
  7473. ret = inherit_task_group(event, parent, parent_ctx,
  7474. child, ctxn, &inherited_all);
  7475. if (ret)
  7476. break;
  7477. }
  7478. /*
  7479. * We can't hold ctx->lock when iterating the ->flexible_group list due
  7480. * to allocations, but we need to prevent rotation because
  7481. * rotate_ctx() will change the list from interrupt context.
  7482. */
  7483. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  7484. parent_ctx->rotate_disable = 1;
  7485. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  7486. list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
  7487. ret = inherit_task_group(event, parent, parent_ctx,
  7488. child, ctxn, &inherited_all);
  7489. if (ret)
  7490. break;
  7491. }
  7492. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  7493. parent_ctx->rotate_disable = 0;
  7494. child_ctx = child->perf_event_ctxp[ctxn];
  7495. if (child_ctx && inherited_all) {
  7496. /*
  7497. * Mark the child context as a clone of the parent
  7498. * context, or of whatever the parent is a clone of.
  7499. *
  7500. * Note that if the parent is a clone, the holding of
  7501. * parent_ctx->lock avoids it from being uncloned.
  7502. */
  7503. cloned_ctx = parent_ctx->parent_ctx;
  7504. if (cloned_ctx) {
  7505. child_ctx->parent_ctx = cloned_ctx;
  7506. child_ctx->parent_gen = parent_ctx->parent_gen;
  7507. } else {
  7508. child_ctx->parent_ctx = parent_ctx;
  7509. child_ctx->parent_gen = parent_ctx->generation;
  7510. }
  7511. get_ctx(child_ctx->parent_ctx);
  7512. }
  7513. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  7514. mutex_unlock(&parent_ctx->mutex);
  7515. perf_unpin_context(parent_ctx);
  7516. put_ctx(parent_ctx);
  7517. return ret;
  7518. }
  7519. /*
  7520. * Initialize the perf_event context in task_struct
  7521. */
  7522. int perf_event_init_task(struct task_struct *child)
  7523. {
  7524. int ctxn, ret;
  7525. memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
  7526. mutex_init(&child->perf_event_mutex);
  7527. INIT_LIST_HEAD(&child->perf_event_list);
  7528. for_each_task_context_nr(ctxn) {
  7529. ret = perf_event_init_context(child, ctxn);
  7530. if (ret) {
  7531. perf_event_free_task(child);
  7532. return ret;
  7533. }
  7534. }
  7535. return 0;
  7536. }
  7537. static void __init perf_event_init_all_cpus(void)
  7538. {
  7539. struct swevent_htable *swhash;
  7540. int cpu;
  7541. for_each_possible_cpu(cpu) {
  7542. swhash = &per_cpu(swevent_htable, cpu);
  7543. mutex_init(&swhash->hlist_mutex);
  7544. INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
  7545. }
  7546. }
  7547. static void perf_event_init_cpu(int cpu)
  7548. {
  7549. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  7550. mutex_lock(&swhash->hlist_mutex);
  7551. if (swhash->hlist_refcount > 0) {
  7552. struct swevent_hlist *hlist;
  7553. hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
  7554. WARN_ON(!hlist);
  7555. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  7556. }
  7557. mutex_unlock(&swhash->hlist_mutex);
  7558. }
  7559. #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
  7560. static void __perf_event_exit_context(void *__info)
  7561. {
  7562. struct perf_event_context *ctx = __info;
  7563. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  7564. struct perf_event *event;
  7565. raw_spin_lock(&ctx->lock);
  7566. list_for_each_entry(event, &ctx->event_list, event_entry)
  7567. __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
  7568. raw_spin_unlock(&ctx->lock);
  7569. }
  7570. static void perf_event_exit_cpu_context(int cpu)
  7571. {
  7572. struct perf_event_context *ctx;
  7573. struct pmu *pmu;
  7574. int idx;
  7575. idx = srcu_read_lock(&pmus_srcu);
  7576. list_for_each_entry_rcu(pmu, &pmus, entry) {
  7577. ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
  7578. mutex_lock(&ctx->mutex);
  7579. smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
  7580. mutex_unlock(&ctx->mutex);
  7581. }
  7582. srcu_read_unlock(&pmus_srcu, idx);
  7583. }
  7584. static void perf_event_exit_cpu(int cpu)
  7585. {
  7586. perf_event_exit_cpu_context(cpu);
  7587. }
  7588. #else
  7589. static inline void perf_event_exit_cpu(int cpu) { }
  7590. #endif
  7591. static int
  7592. perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
  7593. {
  7594. int cpu;
  7595. for_each_online_cpu(cpu)
  7596. perf_event_exit_cpu(cpu);
  7597. return NOTIFY_OK;
  7598. }
  7599. /*
  7600. * Run the perf reboot notifier at the very last possible moment so that
  7601. * the generic watchdog code runs as long as possible.
  7602. */
  7603. static struct notifier_block perf_reboot_notifier = {
  7604. .notifier_call = perf_reboot,
  7605. .priority = INT_MIN,
  7606. };
  7607. static int
  7608. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  7609. {
  7610. unsigned int cpu = (long)hcpu;
  7611. switch (action & ~CPU_TASKS_FROZEN) {
  7612. case CPU_UP_PREPARE:
  7613. case CPU_DOWN_FAILED:
  7614. perf_event_init_cpu(cpu);
  7615. break;
  7616. case CPU_UP_CANCELED:
  7617. case CPU_DOWN_PREPARE:
  7618. perf_event_exit_cpu(cpu);
  7619. break;
  7620. default:
  7621. break;
  7622. }
  7623. return NOTIFY_OK;
  7624. }
  7625. void __init perf_event_init(void)
  7626. {
  7627. int ret;
  7628. idr_init(&pmu_idr);
  7629. perf_event_init_all_cpus();
  7630. init_srcu_struct(&pmus_srcu);
  7631. perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
  7632. perf_pmu_register(&perf_cpu_clock, NULL, -1);
  7633. perf_pmu_register(&perf_task_clock, NULL, -1);
  7634. perf_tp_register();
  7635. perf_cpu_notifier(perf_cpu_notify);
  7636. register_reboot_notifier(&perf_reboot_notifier);
  7637. ret = init_hw_breakpoint();
  7638. WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
  7639. /* do not patch jump label more than once per second */
  7640. jump_label_rate_limit(&perf_sched_events, HZ);
  7641. /*
  7642. * Build time assertion that we keep the data_head at the intended
  7643. * location. IOW, validation we got the __reserved[] size right.
  7644. */
  7645. BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
  7646. != 1024);
  7647. }
  7648. ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
  7649. char *page)
  7650. {
  7651. struct perf_pmu_events_attr *pmu_attr =
  7652. container_of(attr, struct perf_pmu_events_attr, attr);
  7653. if (pmu_attr->event_str)
  7654. return sprintf(page, "%s\n", pmu_attr->event_str);
  7655. return 0;
  7656. }
  7657. static int __init perf_event_sysfs_init(void)
  7658. {
  7659. struct pmu *pmu;
  7660. int ret;
  7661. mutex_lock(&pmus_lock);
  7662. ret = bus_register(&pmu_bus);
  7663. if (ret)
  7664. goto unlock;
  7665. list_for_each_entry(pmu, &pmus, entry) {
  7666. if (!pmu->name || pmu->type < 0)
  7667. continue;
  7668. ret = pmu_dev_alloc(pmu);
  7669. WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
  7670. }
  7671. pmu_bus_running = 1;
  7672. ret = 0;
  7673. unlock:
  7674. mutex_unlock(&pmus_lock);
  7675. return ret;
  7676. }
  7677. device_initcall(perf_event_sysfs_init);
  7678. #ifdef CONFIG_CGROUP_PERF
  7679. static struct cgroup_subsys_state *
  7680. perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
  7681. {
  7682. struct perf_cgroup *jc;
  7683. jc = kzalloc(sizeof(*jc), GFP_KERNEL);
  7684. if (!jc)
  7685. return ERR_PTR(-ENOMEM);
  7686. jc->info = alloc_percpu(struct perf_cgroup_info);
  7687. if (!jc->info) {
  7688. kfree(jc);
  7689. return ERR_PTR(-ENOMEM);
  7690. }
  7691. return &jc->css;
  7692. }
  7693. static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
  7694. {
  7695. struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
  7696. free_percpu(jc->info);
  7697. kfree(jc);
  7698. }
  7699. static int __perf_cgroup_move(void *info)
  7700. {
  7701. struct task_struct *task = info;
  7702. rcu_read_lock();
  7703. perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
  7704. rcu_read_unlock();
  7705. return 0;
  7706. }
  7707. static void perf_cgroup_attach(struct cgroup_taskset *tset)
  7708. {
  7709. struct task_struct *task;
  7710. struct cgroup_subsys_state *css;
  7711. cgroup_taskset_for_each(task, css, tset)
  7712. task_function_call(task, __perf_cgroup_move, task);
  7713. }
  7714. struct cgroup_subsys perf_event_cgrp_subsys = {
  7715. .css_alloc = perf_cgroup_css_alloc,
  7716. .css_free = perf_cgroup_css_free,
  7717. .attach = perf_cgroup_attach,
  7718. };
  7719. #endif /* CONFIG_CGROUP_PERF */