core.c 186 KB

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