perf_event.c 146 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429
  1. /*
  2. * Performance events core code:
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2008-2009 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/file.h>
  16. #include <linux/poll.h>
  17. #include <linux/slab.h>
  18. #include <linux/hash.h>
  19. #include <linux/sysfs.h>
  20. #include <linux/dcache.h>
  21. #include <linux/percpu.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/vmstat.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/hardirq.h>
  26. #include <linux/rculist.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/anon_inodes.h>
  30. #include <linux/kernel_stat.h>
  31. #include <linux/perf_event.h>
  32. #include <linux/ftrace_event.h>
  33. #include <linux/hw_breakpoint.h>
  34. #include <asm/irq_regs.h>
  35. atomic_t perf_task_events __read_mostly;
  36. static atomic_t nr_mmap_events __read_mostly;
  37. static atomic_t nr_comm_events __read_mostly;
  38. static atomic_t nr_task_events __read_mostly;
  39. static LIST_HEAD(pmus);
  40. static DEFINE_MUTEX(pmus_lock);
  41. static struct srcu_struct pmus_srcu;
  42. /*
  43. * perf event paranoia level:
  44. * -1 - not paranoid at all
  45. * 0 - disallow raw tracepoint access for unpriv
  46. * 1 - disallow cpu events for unpriv
  47. * 2 - disallow kernel profiling for unpriv
  48. */
  49. int sysctl_perf_event_paranoid __read_mostly = 1;
  50. int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
  51. /*
  52. * max perf event sample rate
  53. */
  54. int sysctl_perf_event_sample_rate __read_mostly = 100000;
  55. static atomic64_t perf_event_id;
  56. void __weak perf_event_print_debug(void) { }
  57. extern __weak const char *perf_pmu_name(void)
  58. {
  59. return "pmu";
  60. }
  61. void perf_pmu_disable(struct pmu *pmu)
  62. {
  63. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  64. if (!(*count)++)
  65. pmu->pmu_disable(pmu);
  66. }
  67. void perf_pmu_enable(struct pmu *pmu)
  68. {
  69. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  70. if (!--(*count))
  71. pmu->pmu_enable(pmu);
  72. }
  73. static DEFINE_PER_CPU(struct list_head, rotation_list);
  74. /*
  75. * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
  76. * because they're strictly cpu affine and rotate_start is called with IRQs
  77. * disabled, while rotate_context is called from IRQ context.
  78. */
  79. static void perf_pmu_rotate_start(struct pmu *pmu)
  80. {
  81. struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  82. struct list_head *head = &__get_cpu_var(rotation_list);
  83. WARN_ON(!irqs_disabled());
  84. if (list_empty(&cpuctx->rotation_list))
  85. list_add(&cpuctx->rotation_list, head);
  86. }
  87. static void get_ctx(struct perf_event_context *ctx)
  88. {
  89. WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
  90. }
  91. static void free_ctx(struct rcu_head *head)
  92. {
  93. struct perf_event_context *ctx;
  94. ctx = container_of(head, struct perf_event_context, rcu_head);
  95. kfree(ctx);
  96. }
  97. static void put_ctx(struct perf_event_context *ctx)
  98. {
  99. if (atomic_dec_and_test(&ctx->refcount)) {
  100. if (ctx->parent_ctx)
  101. put_ctx(ctx->parent_ctx);
  102. if (ctx->task)
  103. put_task_struct(ctx->task);
  104. call_rcu(&ctx->rcu_head, free_ctx);
  105. }
  106. }
  107. static void unclone_ctx(struct perf_event_context *ctx)
  108. {
  109. if (ctx->parent_ctx) {
  110. put_ctx(ctx->parent_ctx);
  111. ctx->parent_ctx = NULL;
  112. }
  113. }
  114. /*
  115. * If we inherit events we want to return the parent event id
  116. * to userspace.
  117. */
  118. static u64 primary_event_id(struct perf_event *event)
  119. {
  120. u64 id = event->id;
  121. if (event->parent)
  122. id = event->parent->id;
  123. return id;
  124. }
  125. /*
  126. * Get the perf_event_context for a task and lock it.
  127. * This has to cope with with the fact that until it is locked,
  128. * the context could get moved to another task.
  129. */
  130. static struct perf_event_context *
  131. perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
  132. {
  133. struct perf_event_context *ctx;
  134. rcu_read_lock();
  135. retry:
  136. ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
  137. if (ctx) {
  138. /*
  139. * If this context is a clone of another, it might
  140. * get swapped for another underneath us by
  141. * perf_event_task_sched_out, though the
  142. * rcu_read_lock() protects us from any context
  143. * getting freed. Lock the context and check if it
  144. * got swapped before we could get the lock, and retry
  145. * if so. If we locked the right context, then it
  146. * can't get swapped on us any more.
  147. */
  148. raw_spin_lock_irqsave(&ctx->lock, *flags);
  149. if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
  150. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  151. goto retry;
  152. }
  153. if (!atomic_inc_not_zero(&ctx->refcount)) {
  154. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  155. ctx = NULL;
  156. }
  157. }
  158. rcu_read_unlock();
  159. return ctx;
  160. }
  161. /*
  162. * Get the context for a task and increment its pin_count so it
  163. * can't get swapped to another task. This also increments its
  164. * reference count so that the context can't get freed.
  165. */
  166. static struct perf_event_context *
  167. perf_pin_task_context(struct task_struct *task, int ctxn)
  168. {
  169. struct perf_event_context *ctx;
  170. unsigned long flags;
  171. ctx = perf_lock_task_context(task, ctxn, &flags);
  172. if (ctx) {
  173. ++ctx->pin_count;
  174. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  175. }
  176. return ctx;
  177. }
  178. static void perf_unpin_context(struct perf_event_context *ctx)
  179. {
  180. unsigned long flags;
  181. raw_spin_lock_irqsave(&ctx->lock, flags);
  182. --ctx->pin_count;
  183. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  184. put_ctx(ctx);
  185. }
  186. static inline u64 perf_clock(void)
  187. {
  188. return local_clock();
  189. }
  190. /*
  191. * Update the record of the current time in a context.
  192. */
  193. static void update_context_time(struct perf_event_context *ctx)
  194. {
  195. u64 now = perf_clock();
  196. ctx->time += now - ctx->timestamp;
  197. ctx->timestamp = now;
  198. }
  199. /*
  200. * Update the total_time_enabled and total_time_running fields for a event.
  201. */
  202. static void update_event_times(struct perf_event *event)
  203. {
  204. struct perf_event_context *ctx = event->ctx;
  205. u64 run_end;
  206. if (event->state < PERF_EVENT_STATE_INACTIVE ||
  207. event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
  208. return;
  209. if (ctx->is_active)
  210. run_end = ctx->time;
  211. else
  212. run_end = event->tstamp_stopped;
  213. event->total_time_enabled = run_end - event->tstamp_enabled;
  214. if (event->state == PERF_EVENT_STATE_INACTIVE)
  215. run_end = event->tstamp_stopped;
  216. else
  217. run_end = ctx->time;
  218. event->total_time_running = run_end - event->tstamp_running;
  219. }
  220. /*
  221. * Update total_time_enabled and total_time_running for all events in a group.
  222. */
  223. static void update_group_times(struct perf_event *leader)
  224. {
  225. struct perf_event *event;
  226. update_event_times(leader);
  227. list_for_each_entry(event, &leader->sibling_list, group_entry)
  228. update_event_times(event);
  229. }
  230. static struct list_head *
  231. ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
  232. {
  233. if (event->attr.pinned)
  234. return &ctx->pinned_groups;
  235. else
  236. return &ctx->flexible_groups;
  237. }
  238. /*
  239. * Add a event from the lists for its context.
  240. * Must be called with ctx->mutex and ctx->lock held.
  241. */
  242. static void
  243. list_add_event(struct perf_event *event, struct perf_event_context *ctx)
  244. {
  245. WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
  246. event->attach_state |= PERF_ATTACH_CONTEXT;
  247. /*
  248. * If we're a stand alone event or group leader, we go to the context
  249. * list, group events are kept attached to the group so that
  250. * perf_group_detach can, at all times, locate all siblings.
  251. */
  252. if (event->group_leader == event) {
  253. struct list_head *list;
  254. if (is_software_event(event))
  255. event->group_flags |= PERF_GROUP_SOFTWARE;
  256. list = ctx_group_list(event, ctx);
  257. list_add_tail(&event->group_entry, list);
  258. }
  259. list_add_rcu(&event->event_entry, &ctx->event_list);
  260. if (!ctx->nr_events)
  261. perf_pmu_rotate_start(ctx->pmu);
  262. ctx->nr_events++;
  263. if (event->attr.inherit_stat)
  264. ctx->nr_stat++;
  265. }
  266. /*
  267. * Called at perf_event creation and when events are attached/detached from a
  268. * group.
  269. */
  270. static void perf_event__read_size(struct perf_event *event)
  271. {
  272. int entry = sizeof(u64); /* value */
  273. int size = 0;
  274. int nr = 1;
  275. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  276. size += sizeof(u64);
  277. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  278. size += sizeof(u64);
  279. if (event->attr.read_format & PERF_FORMAT_ID)
  280. entry += sizeof(u64);
  281. if (event->attr.read_format & PERF_FORMAT_GROUP) {
  282. nr += event->group_leader->nr_siblings;
  283. size += sizeof(u64);
  284. }
  285. size += entry * nr;
  286. event->read_size = size;
  287. }
  288. static void perf_event__header_size(struct perf_event *event)
  289. {
  290. struct perf_sample_data *data;
  291. u64 sample_type = event->attr.sample_type;
  292. u16 size = 0;
  293. perf_event__read_size(event);
  294. if (sample_type & PERF_SAMPLE_IP)
  295. size += sizeof(data->ip);
  296. if (sample_type & PERF_SAMPLE_TID)
  297. size += sizeof(data->tid_entry);
  298. if (sample_type & PERF_SAMPLE_TIME)
  299. size += sizeof(data->time);
  300. if (sample_type & PERF_SAMPLE_ADDR)
  301. size += sizeof(data->addr);
  302. if (sample_type & PERF_SAMPLE_ID)
  303. size += sizeof(data->id);
  304. if (sample_type & PERF_SAMPLE_STREAM_ID)
  305. size += sizeof(data->stream_id);
  306. if (sample_type & PERF_SAMPLE_CPU)
  307. size += sizeof(data->cpu_entry);
  308. if (sample_type & PERF_SAMPLE_PERIOD)
  309. size += sizeof(data->period);
  310. if (sample_type & PERF_SAMPLE_READ)
  311. size += event->read_size;
  312. event->header_size = size;
  313. }
  314. static void perf_group_attach(struct perf_event *event)
  315. {
  316. struct perf_event *group_leader = event->group_leader, *pos;
  317. /*
  318. * We can have double attach due to group movement in perf_event_open.
  319. */
  320. if (event->attach_state & PERF_ATTACH_GROUP)
  321. return;
  322. event->attach_state |= PERF_ATTACH_GROUP;
  323. if (group_leader == event)
  324. return;
  325. if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
  326. !is_software_event(event))
  327. group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
  328. list_add_tail(&event->group_entry, &group_leader->sibling_list);
  329. group_leader->nr_siblings++;
  330. perf_event__header_size(group_leader);
  331. list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
  332. perf_event__header_size(pos);
  333. }
  334. /*
  335. * Remove a event from the lists for its context.
  336. * Must be called with ctx->mutex and ctx->lock held.
  337. */
  338. static void
  339. list_del_event(struct perf_event *event, struct perf_event_context *ctx)
  340. {
  341. /*
  342. * We can have double detach due to exit/hot-unplug + close.
  343. */
  344. if (!(event->attach_state & PERF_ATTACH_CONTEXT))
  345. return;
  346. event->attach_state &= ~PERF_ATTACH_CONTEXT;
  347. ctx->nr_events--;
  348. if (event->attr.inherit_stat)
  349. ctx->nr_stat--;
  350. list_del_rcu(&event->event_entry);
  351. if (event->group_leader == event)
  352. list_del_init(&event->group_entry);
  353. update_group_times(event);
  354. /*
  355. * If event was in error state, then keep it
  356. * that way, otherwise bogus counts will be
  357. * returned on read(). The only way to get out
  358. * of error state is by explicit re-enabling
  359. * of the event
  360. */
  361. if (event->state > PERF_EVENT_STATE_OFF)
  362. event->state = PERF_EVENT_STATE_OFF;
  363. }
  364. static void perf_group_detach(struct perf_event *event)
  365. {
  366. struct perf_event *sibling, *tmp;
  367. struct list_head *list = NULL;
  368. /*
  369. * We can have double detach due to exit/hot-unplug + close.
  370. */
  371. if (!(event->attach_state & PERF_ATTACH_GROUP))
  372. return;
  373. event->attach_state &= ~PERF_ATTACH_GROUP;
  374. /*
  375. * If this is a sibling, remove it from its group.
  376. */
  377. if (event->group_leader != event) {
  378. list_del_init(&event->group_entry);
  379. event->group_leader->nr_siblings--;
  380. goto out;
  381. }
  382. if (!list_empty(&event->group_entry))
  383. list = &event->group_entry;
  384. /*
  385. * If this was a group event with sibling events then
  386. * upgrade the siblings to singleton events by adding them
  387. * to whatever list we are on.
  388. */
  389. list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
  390. if (list)
  391. list_move_tail(&sibling->group_entry, list);
  392. sibling->group_leader = sibling;
  393. /* Inherit group flags from the previous leader */
  394. sibling->group_flags = event->group_flags;
  395. }
  396. out:
  397. perf_event__header_size(event->group_leader);
  398. list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
  399. perf_event__header_size(tmp);
  400. }
  401. static inline int
  402. event_filter_match(struct perf_event *event)
  403. {
  404. return event->cpu == -1 || event->cpu == smp_processor_id();
  405. }
  406. static void
  407. event_sched_out(struct perf_event *event,
  408. struct perf_cpu_context *cpuctx,
  409. struct perf_event_context *ctx)
  410. {
  411. u64 delta;
  412. /*
  413. * An event which could not be activated because of
  414. * filter mismatch still needs to have its timings
  415. * maintained, otherwise bogus information is return
  416. * via read() for time_enabled, time_running:
  417. */
  418. if (event->state == PERF_EVENT_STATE_INACTIVE
  419. && !event_filter_match(event)) {
  420. delta = ctx->time - event->tstamp_stopped;
  421. event->tstamp_running += delta;
  422. event->tstamp_stopped = ctx->time;
  423. }
  424. if (event->state != PERF_EVENT_STATE_ACTIVE)
  425. return;
  426. event->state = PERF_EVENT_STATE_INACTIVE;
  427. if (event->pending_disable) {
  428. event->pending_disable = 0;
  429. event->state = PERF_EVENT_STATE_OFF;
  430. }
  431. event->tstamp_stopped = ctx->time;
  432. event->pmu->del(event, 0);
  433. event->oncpu = -1;
  434. if (!is_software_event(event))
  435. cpuctx->active_oncpu--;
  436. ctx->nr_active--;
  437. if (event->attr.exclusive || !cpuctx->active_oncpu)
  438. cpuctx->exclusive = 0;
  439. }
  440. static void
  441. group_sched_out(struct perf_event *group_event,
  442. struct perf_cpu_context *cpuctx,
  443. struct perf_event_context *ctx)
  444. {
  445. struct perf_event *event;
  446. int state = group_event->state;
  447. event_sched_out(group_event, cpuctx, ctx);
  448. /*
  449. * Schedule out siblings (if any):
  450. */
  451. list_for_each_entry(event, &group_event->sibling_list, group_entry)
  452. event_sched_out(event, cpuctx, ctx);
  453. if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
  454. cpuctx->exclusive = 0;
  455. }
  456. static inline struct perf_cpu_context *
  457. __get_cpu_context(struct perf_event_context *ctx)
  458. {
  459. return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
  460. }
  461. /*
  462. * Cross CPU call to remove a performance event
  463. *
  464. * We disable the event on the hardware level first. After that we
  465. * remove it from the context list.
  466. */
  467. static void __perf_event_remove_from_context(void *info)
  468. {
  469. struct perf_event *event = info;
  470. struct perf_event_context *ctx = event->ctx;
  471. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  472. /*
  473. * If this is a task context, we need to check whether it is
  474. * the current task context of this cpu. If not it has been
  475. * scheduled out before the smp call arrived.
  476. */
  477. if (ctx->task && cpuctx->task_ctx != ctx)
  478. return;
  479. raw_spin_lock(&ctx->lock);
  480. event_sched_out(event, cpuctx, ctx);
  481. list_del_event(event, ctx);
  482. raw_spin_unlock(&ctx->lock);
  483. }
  484. /*
  485. * Remove the event from a task's (or a CPU's) list of events.
  486. *
  487. * Must be called with ctx->mutex held.
  488. *
  489. * CPU events are removed with a smp call. For task events we only
  490. * call when the task is on a CPU.
  491. *
  492. * If event->ctx is a cloned context, callers must make sure that
  493. * every task struct that event->ctx->task could possibly point to
  494. * remains valid. This is OK when called from perf_release since
  495. * that only calls us on the top-level context, which can't be a clone.
  496. * When called from perf_event_exit_task, it's OK because the
  497. * context has been detached from its task.
  498. */
  499. static void perf_event_remove_from_context(struct perf_event *event)
  500. {
  501. struct perf_event_context *ctx = event->ctx;
  502. struct task_struct *task = ctx->task;
  503. if (!task) {
  504. /*
  505. * Per cpu events are removed via an smp call and
  506. * the removal is always successful.
  507. */
  508. smp_call_function_single(event->cpu,
  509. __perf_event_remove_from_context,
  510. event, 1);
  511. return;
  512. }
  513. retry:
  514. task_oncpu_function_call(task, __perf_event_remove_from_context,
  515. event);
  516. raw_spin_lock_irq(&ctx->lock);
  517. /*
  518. * If the context is active we need to retry the smp call.
  519. */
  520. if (ctx->nr_active && !list_empty(&event->group_entry)) {
  521. raw_spin_unlock_irq(&ctx->lock);
  522. goto retry;
  523. }
  524. /*
  525. * The lock prevents that this context is scheduled in so we
  526. * can remove the event safely, if the call above did not
  527. * succeed.
  528. */
  529. if (!list_empty(&event->group_entry))
  530. list_del_event(event, ctx);
  531. raw_spin_unlock_irq(&ctx->lock);
  532. }
  533. /*
  534. * Cross CPU call to disable a performance event
  535. */
  536. static void __perf_event_disable(void *info)
  537. {
  538. struct perf_event *event = info;
  539. struct perf_event_context *ctx = event->ctx;
  540. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  541. /*
  542. * If this is a per-task event, need to check whether this
  543. * event's task is the current task on this cpu.
  544. */
  545. if (ctx->task && cpuctx->task_ctx != ctx)
  546. return;
  547. raw_spin_lock(&ctx->lock);
  548. /*
  549. * If the event is on, turn it off.
  550. * If it is in error state, leave it in error state.
  551. */
  552. if (event->state >= PERF_EVENT_STATE_INACTIVE) {
  553. update_context_time(ctx);
  554. update_group_times(event);
  555. if (event == event->group_leader)
  556. group_sched_out(event, cpuctx, ctx);
  557. else
  558. event_sched_out(event, cpuctx, ctx);
  559. event->state = PERF_EVENT_STATE_OFF;
  560. }
  561. raw_spin_unlock(&ctx->lock);
  562. }
  563. /*
  564. * Disable a event.
  565. *
  566. * If event->ctx is a cloned context, callers must make sure that
  567. * every task struct that event->ctx->task could possibly point to
  568. * remains valid. This condition is satisifed when called through
  569. * perf_event_for_each_child or perf_event_for_each because they
  570. * hold the top-level event's child_mutex, so any descendant that
  571. * goes to exit will block in sync_child_event.
  572. * When called from perf_pending_event it's OK because event->ctx
  573. * is the current context on this CPU and preemption is disabled,
  574. * hence we can't get into perf_event_task_sched_out for this context.
  575. */
  576. void perf_event_disable(struct perf_event *event)
  577. {
  578. struct perf_event_context *ctx = event->ctx;
  579. struct task_struct *task = ctx->task;
  580. if (!task) {
  581. /*
  582. * Disable the event on the cpu that it's on
  583. */
  584. smp_call_function_single(event->cpu, __perf_event_disable,
  585. event, 1);
  586. return;
  587. }
  588. retry:
  589. task_oncpu_function_call(task, __perf_event_disable, event);
  590. raw_spin_lock_irq(&ctx->lock);
  591. /*
  592. * If the event is still active, we need to retry the cross-call.
  593. */
  594. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  595. raw_spin_unlock_irq(&ctx->lock);
  596. goto retry;
  597. }
  598. /*
  599. * Since we have the lock this context can't be scheduled
  600. * in, so we can change the state safely.
  601. */
  602. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  603. update_group_times(event);
  604. event->state = PERF_EVENT_STATE_OFF;
  605. }
  606. raw_spin_unlock_irq(&ctx->lock);
  607. }
  608. static int
  609. event_sched_in(struct perf_event *event,
  610. struct perf_cpu_context *cpuctx,
  611. struct perf_event_context *ctx)
  612. {
  613. if (event->state <= PERF_EVENT_STATE_OFF)
  614. return 0;
  615. event->state = PERF_EVENT_STATE_ACTIVE;
  616. event->oncpu = smp_processor_id();
  617. /*
  618. * The new state must be visible before we turn it on in the hardware:
  619. */
  620. smp_wmb();
  621. if (event->pmu->add(event, PERF_EF_START)) {
  622. event->state = PERF_EVENT_STATE_INACTIVE;
  623. event->oncpu = -1;
  624. return -EAGAIN;
  625. }
  626. event->tstamp_running += ctx->time - event->tstamp_stopped;
  627. event->shadow_ctx_time = ctx->time - ctx->timestamp;
  628. if (!is_software_event(event))
  629. cpuctx->active_oncpu++;
  630. ctx->nr_active++;
  631. if (event->attr.exclusive)
  632. cpuctx->exclusive = 1;
  633. return 0;
  634. }
  635. static int
  636. group_sched_in(struct perf_event *group_event,
  637. struct perf_cpu_context *cpuctx,
  638. struct perf_event_context *ctx)
  639. {
  640. struct perf_event *event, *partial_group = NULL;
  641. struct pmu *pmu = group_event->pmu;
  642. u64 now = ctx->time;
  643. bool simulate = false;
  644. if (group_event->state == PERF_EVENT_STATE_OFF)
  645. return 0;
  646. pmu->start_txn(pmu);
  647. if (event_sched_in(group_event, cpuctx, ctx)) {
  648. pmu->cancel_txn(pmu);
  649. return -EAGAIN;
  650. }
  651. /*
  652. * Schedule in siblings as one group (if any):
  653. */
  654. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  655. if (event_sched_in(event, cpuctx, ctx)) {
  656. partial_group = event;
  657. goto group_error;
  658. }
  659. }
  660. if (!pmu->commit_txn(pmu))
  661. return 0;
  662. group_error:
  663. /*
  664. * Groups can be scheduled in as one unit only, so undo any
  665. * partial group before returning:
  666. * The events up to the failed event are scheduled out normally,
  667. * tstamp_stopped will be updated.
  668. *
  669. * The failed events and the remaining siblings need to have
  670. * their timings updated as if they had gone thru event_sched_in()
  671. * and event_sched_out(). This is required to get consistent timings
  672. * across the group. This also takes care of the case where the group
  673. * could never be scheduled by ensuring tstamp_stopped is set to mark
  674. * the time the event was actually stopped, such that time delta
  675. * calculation in update_event_times() is correct.
  676. */
  677. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  678. if (event == partial_group)
  679. simulate = true;
  680. if (simulate) {
  681. event->tstamp_running += now - event->tstamp_stopped;
  682. event->tstamp_stopped = now;
  683. } else {
  684. event_sched_out(event, cpuctx, ctx);
  685. }
  686. }
  687. event_sched_out(group_event, cpuctx, ctx);
  688. pmu->cancel_txn(pmu);
  689. return -EAGAIN;
  690. }
  691. /*
  692. * Work out whether we can put this event group on the CPU now.
  693. */
  694. static int group_can_go_on(struct perf_event *event,
  695. struct perf_cpu_context *cpuctx,
  696. int can_add_hw)
  697. {
  698. /*
  699. * Groups consisting entirely of software events can always go on.
  700. */
  701. if (event->group_flags & PERF_GROUP_SOFTWARE)
  702. return 1;
  703. /*
  704. * If an exclusive group is already on, no other hardware
  705. * events can go on.
  706. */
  707. if (cpuctx->exclusive)
  708. return 0;
  709. /*
  710. * If this group is exclusive and there are already
  711. * events on the CPU, it can't go on.
  712. */
  713. if (event->attr.exclusive && cpuctx->active_oncpu)
  714. return 0;
  715. /*
  716. * Otherwise, try to add it if all previous groups were able
  717. * to go on.
  718. */
  719. return can_add_hw;
  720. }
  721. static void add_event_to_ctx(struct perf_event *event,
  722. struct perf_event_context *ctx)
  723. {
  724. list_add_event(event, ctx);
  725. perf_group_attach(event);
  726. event->tstamp_enabled = ctx->time;
  727. event->tstamp_running = ctx->time;
  728. event->tstamp_stopped = ctx->time;
  729. }
  730. /*
  731. * Cross CPU call to install and enable a performance event
  732. *
  733. * Must be called with ctx->mutex held
  734. */
  735. static void __perf_install_in_context(void *info)
  736. {
  737. struct perf_event *event = info;
  738. struct perf_event_context *ctx = event->ctx;
  739. struct perf_event *leader = event->group_leader;
  740. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  741. int err;
  742. /*
  743. * If this is a task context, we need to check whether it is
  744. * the current task context of this cpu. If not it has been
  745. * scheduled out before the smp call arrived.
  746. * Or possibly this is the right context but it isn't
  747. * on this cpu because it had no events.
  748. */
  749. if (ctx->task && cpuctx->task_ctx != ctx) {
  750. if (cpuctx->task_ctx || ctx->task != current)
  751. return;
  752. cpuctx->task_ctx = ctx;
  753. }
  754. raw_spin_lock(&ctx->lock);
  755. ctx->is_active = 1;
  756. update_context_time(ctx);
  757. add_event_to_ctx(event, ctx);
  758. if (event->cpu != -1 && event->cpu != smp_processor_id())
  759. goto unlock;
  760. /*
  761. * Don't put the event on if it is disabled or if
  762. * it is in a group and the group isn't on.
  763. */
  764. if (event->state != PERF_EVENT_STATE_INACTIVE ||
  765. (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
  766. goto unlock;
  767. /*
  768. * An exclusive event can't go on if there are already active
  769. * hardware events, and no hardware event can go on if there
  770. * is already an exclusive event on.
  771. */
  772. if (!group_can_go_on(event, cpuctx, 1))
  773. err = -EEXIST;
  774. else
  775. err = event_sched_in(event, cpuctx, ctx);
  776. if (err) {
  777. /*
  778. * This event couldn't go on. If it is in a group
  779. * then we have to pull the whole group off.
  780. * If the event group is pinned then put it in error state.
  781. */
  782. if (leader != event)
  783. group_sched_out(leader, cpuctx, ctx);
  784. if (leader->attr.pinned) {
  785. update_group_times(leader);
  786. leader->state = PERF_EVENT_STATE_ERROR;
  787. }
  788. }
  789. unlock:
  790. raw_spin_unlock(&ctx->lock);
  791. }
  792. /*
  793. * Attach a performance event to a context
  794. *
  795. * First we add the event to the list with the hardware enable bit
  796. * in event->hw_config cleared.
  797. *
  798. * If the event is attached to a task which is on a CPU we use a smp
  799. * call to enable it in the task context. The task might have been
  800. * scheduled away, but we check this in the smp call again.
  801. *
  802. * Must be called with ctx->mutex held.
  803. */
  804. static void
  805. perf_install_in_context(struct perf_event_context *ctx,
  806. struct perf_event *event,
  807. int cpu)
  808. {
  809. struct task_struct *task = ctx->task;
  810. event->ctx = ctx;
  811. if (!task) {
  812. /*
  813. * Per cpu events are installed via an smp call and
  814. * the install is always successful.
  815. */
  816. smp_call_function_single(cpu, __perf_install_in_context,
  817. event, 1);
  818. return;
  819. }
  820. retry:
  821. task_oncpu_function_call(task, __perf_install_in_context,
  822. event);
  823. raw_spin_lock_irq(&ctx->lock);
  824. /*
  825. * we need to retry the smp call.
  826. */
  827. if (ctx->is_active && list_empty(&event->group_entry)) {
  828. raw_spin_unlock_irq(&ctx->lock);
  829. goto retry;
  830. }
  831. /*
  832. * The lock prevents that this context is scheduled in so we
  833. * can add the event safely, if it the call above did not
  834. * succeed.
  835. */
  836. if (list_empty(&event->group_entry))
  837. add_event_to_ctx(event, ctx);
  838. raw_spin_unlock_irq(&ctx->lock);
  839. }
  840. /*
  841. * Put a event into inactive state and update time fields.
  842. * Enabling the leader of a group effectively enables all
  843. * the group members that aren't explicitly disabled, so we
  844. * have to update their ->tstamp_enabled also.
  845. * Note: this works for group members as well as group leaders
  846. * since the non-leader members' sibling_lists will be empty.
  847. */
  848. static void __perf_event_mark_enabled(struct perf_event *event,
  849. struct perf_event_context *ctx)
  850. {
  851. struct perf_event *sub;
  852. event->state = PERF_EVENT_STATE_INACTIVE;
  853. event->tstamp_enabled = ctx->time - event->total_time_enabled;
  854. list_for_each_entry(sub, &event->sibling_list, group_entry) {
  855. if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
  856. sub->tstamp_enabled =
  857. ctx->time - sub->total_time_enabled;
  858. }
  859. }
  860. }
  861. /*
  862. * Cross CPU call to enable a performance event
  863. */
  864. static void __perf_event_enable(void *info)
  865. {
  866. struct perf_event *event = info;
  867. struct perf_event_context *ctx = event->ctx;
  868. struct perf_event *leader = event->group_leader;
  869. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  870. int err;
  871. /*
  872. * If this is a per-task event, need to check whether this
  873. * event's task is the current task on this cpu.
  874. */
  875. if (ctx->task && cpuctx->task_ctx != ctx) {
  876. if (cpuctx->task_ctx || ctx->task != current)
  877. return;
  878. cpuctx->task_ctx = ctx;
  879. }
  880. raw_spin_lock(&ctx->lock);
  881. ctx->is_active = 1;
  882. update_context_time(ctx);
  883. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  884. goto unlock;
  885. __perf_event_mark_enabled(event, ctx);
  886. if (event->cpu != -1 && event->cpu != smp_processor_id())
  887. goto unlock;
  888. /*
  889. * If the event is in a group and isn't the group leader,
  890. * then don't put it on unless the group is on.
  891. */
  892. if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
  893. goto unlock;
  894. if (!group_can_go_on(event, cpuctx, 1)) {
  895. err = -EEXIST;
  896. } else {
  897. if (event == leader)
  898. err = group_sched_in(event, cpuctx, ctx);
  899. else
  900. err = event_sched_in(event, cpuctx, ctx);
  901. }
  902. if (err) {
  903. /*
  904. * If this event can't go on and it's part of a
  905. * group, then the whole group has to come off.
  906. */
  907. if (leader != event)
  908. group_sched_out(leader, cpuctx, ctx);
  909. if (leader->attr.pinned) {
  910. update_group_times(leader);
  911. leader->state = PERF_EVENT_STATE_ERROR;
  912. }
  913. }
  914. unlock:
  915. raw_spin_unlock(&ctx->lock);
  916. }
  917. /*
  918. * Enable a event.
  919. *
  920. * If event->ctx is a cloned context, callers must make sure that
  921. * every task struct that event->ctx->task could possibly point to
  922. * remains valid. This condition is satisfied when called through
  923. * perf_event_for_each_child or perf_event_for_each as described
  924. * for perf_event_disable.
  925. */
  926. void perf_event_enable(struct perf_event *event)
  927. {
  928. struct perf_event_context *ctx = event->ctx;
  929. struct task_struct *task = ctx->task;
  930. if (!task) {
  931. /*
  932. * Enable the event on the cpu that it's on
  933. */
  934. smp_call_function_single(event->cpu, __perf_event_enable,
  935. event, 1);
  936. return;
  937. }
  938. raw_spin_lock_irq(&ctx->lock);
  939. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  940. goto out;
  941. /*
  942. * If the event is in error state, clear that first.
  943. * That way, if we see the event in error state below, we
  944. * know that it has gone back into error state, as distinct
  945. * from the task having been scheduled away before the
  946. * cross-call arrived.
  947. */
  948. if (event->state == PERF_EVENT_STATE_ERROR)
  949. event->state = PERF_EVENT_STATE_OFF;
  950. retry:
  951. raw_spin_unlock_irq(&ctx->lock);
  952. task_oncpu_function_call(task, __perf_event_enable, event);
  953. raw_spin_lock_irq(&ctx->lock);
  954. /*
  955. * If the context is active and the event is still off,
  956. * we need to retry the cross-call.
  957. */
  958. if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
  959. goto retry;
  960. /*
  961. * Since we have the lock this context can't be scheduled
  962. * in, so we can change the state safely.
  963. */
  964. if (event->state == PERF_EVENT_STATE_OFF)
  965. __perf_event_mark_enabled(event, ctx);
  966. out:
  967. raw_spin_unlock_irq(&ctx->lock);
  968. }
  969. static int perf_event_refresh(struct perf_event *event, int refresh)
  970. {
  971. /*
  972. * not supported on inherited events
  973. */
  974. if (event->attr.inherit || !is_sampling_event(event))
  975. return -EINVAL;
  976. atomic_add(refresh, &event->event_limit);
  977. perf_event_enable(event);
  978. return 0;
  979. }
  980. enum event_type_t {
  981. EVENT_FLEXIBLE = 0x1,
  982. EVENT_PINNED = 0x2,
  983. EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
  984. };
  985. static void ctx_sched_out(struct perf_event_context *ctx,
  986. struct perf_cpu_context *cpuctx,
  987. enum event_type_t event_type)
  988. {
  989. struct perf_event *event;
  990. raw_spin_lock(&ctx->lock);
  991. perf_pmu_disable(ctx->pmu);
  992. ctx->is_active = 0;
  993. if (likely(!ctx->nr_events))
  994. goto out;
  995. update_context_time(ctx);
  996. if (!ctx->nr_active)
  997. goto out;
  998. if (event_type & EVENT_PINNED) {
  999. list_for_each_entry(event, &ctx->pinned_groups, group_entry)
  1000. group_sched_out(event, cpuctx, ctx);
  1001. }
  1002. if (event_type & EVENT_FLEXIBLE) {
  1003. list_for_each_entry(event, &ctx->flexible_groups, group_entry)
  1004. group_sched_out(event, cpuctx, ctx);
  1005. }
  1006. out:
  1007. perf_pmu_enable(ctx->pmu);
  1008. raw_spin_unlock(&ctx->lock);
  1009. }
  1010. /*
  1011. * Test whether two contexts are equivalent, i.e. whether they
  1012. * have both been cloned from the same version of the same context
  1013. * and they both have the same number of enabled events.
  1014. * If the number of enabled events is the same, then the set
  1015. * of enabled events should be the same, because these are both
  1016. * inherited contexts, therefore we can't access individual events
  1017. * in them directly with an fd; we can only enable/disable all
  1018. * events via prctl, or enable/disable all events in a family
  1019. * via ioctl, which will have the same effect on both contexts.
  1020. */
  1021. static int context_equiv(struct perf_event_context *ctx1,
  1022. struct perf_event_context *ctx2)
  1023. {
  1024. return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
  1025. && ctx1->parent_gen == ctx2->parent_gen
  1026. && !ctx1->pin_count && !ctx2->pin_count;
  1027. }
  1028. static void __perf_event_sync_stat(struct perf_event *event,
  1029. struct perf_event *next_event)
  1030. {
  1031. u64 value;
  1032. if (!event->attr.inherit_stat)
  1033. return;
  1034. /*
  1035. * Update the event value, we cannot use perf_event_read()
  1036. * because we're in the middle of a context switch and have IRQs
  1037. * disabled, which upsets smp_call_function_single(), however
  1038. * we know the event must be on the current CPU, therefore we
  1039. * don't need to use it.
  1040. */
  1041. switch (event->state) {
  1042. case PERF_EVENT_STATE_ACTIVE:
  1043. event->pmu->read(event);
  1044. /* fall-through */
  1045. case PERF_EVENT_STATE_INACTIVE:
  1046. update_event_times(event);
  1047. break;
  1048. default:
  1049. break;
  1050. }
  1051. /*
  1052. * In order to keep per-task stats reliable we need to flip the event
  1053. * values when we flip the contexts.
  1054. */
  1055. value = local64_read(&next_event->count);
  1056. value = local64_xchg(&event->count, value);
  1057. local64_set(&next_event->count, value);
  1058. swap(event->total_time_enabled, next_event->total_time_enabled);
  1059. swap(event->total_time_running, next_event->total_time_running);
  1060. /*
  1061. * Since we swizzled the values, update the user visible data too.
  1062. */
  1063. perf_event_update_userpage(event);
  1064. perf_event_update_userpage(next_event);
  1065. }
  1066. #define list_next_entry(pos, member) \
  1067. list_entry(pos->member.next, typeof(*pos), member)
  1068. static void perf_event_sync_stat(struct perf_event_context *ctx,
  1069. struct perf_event_context *next_ctx)
  1070. {
  1071. struct perf_event *event, *next_event;
  1072. if (!ctx->nr_stat)
  1073. return;
  1074. update_context_time(ctx);
  1075. event = list_first_entry(&ctx->event_list,
  1076. struct perf_event, event_entry);
  1077. next_event = list_first_entry(&next_ctx->event_list,
  1078. struct perf_event, event_entry);
  1079. while (&event->event_entry != &ctx->event_list &&
  1080. &next_event->event_entry != &next_ctx->event_list) {
  1081. __perf_event_sync_stat(event, next_event);
  1082. event = list_next_entry(event, event_entry);
  1083. next_event = list_next_entry(next_event, event_entry);
  1084. }
  1085. }
  1086. void perf_event_context_sched_out(struct task_struct *task, int ctxn,
  1087. struct task_struct *next)
  1088. {
  1089. struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
  1090. struct perf_event_context *next_ctx;
  1091. struct perf_event_context *parent;
  1092. struct perf_cpu_context *cpuctx;
  1093. int do_switch = 1;
  1094. if (likely(!ctx))
  1095. return;
  1096. cpuctx = __get_cpu_context(ctx);
  1097. if (!cpuctx->task_ctx)
  1098. return;
  1099. rcu_read_lock();
  1100. parent = rcu_dereference(ctx->parent_ctx);
  1101. next_ctx = next->perf_event_ctxp[ctxn];
  1102. if (parent && next_ctx &&
  1103. rcu_dereference(next_ctx->parent_ctx) == parent) {
  1104. /*
  1105. * Looks like the two contexts are clones, so we might be
  1106. * able to optimize the context switch. We lock both
  1107. * contexts and check that they are clones under the
  1108. * lock (including re-checking that neither has been
  1109. * uncloned in the meantime). It doesn't matter which
  1110. * order we take the locks because no other cpu could
  1111. * be trying to lock both of these tasks.
  1112. */
  1113. raw_spin_lock(&ctx->lock);
  1114. raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
  1115. if (context_equiv(ctx, next_ctx)) {
  1116. /*
  1117. * XXX do we need a memory barrier of sorts
  1118. * wrt to rcu_dereference() of perf_event_ctxp
  1119. */
  1120. task->perf_event_ctxp[ctxn] = next_ctx;
  1121. next->perf_event_ctxp[ctxn] = ctx;
  1122. ctx->task = next;
  1123. next_ctx->task = task;
  1124. do_switch = 0;
  1125. perf_event_sync_stat(ctx, next_ctx);
  1126. }
  1127. raw_spin_unlock(&next_ctx->lock);
  1128. raw_spin_unlock(&ctx->lock);
  1129. }
  1130. rcu_read_unlock();
  1131. if (do_switch) {
  1132. ctx_sched_out(ctx, cpuctx, EVENT_ALL);
  1133. cpuctx->task_ctx = NULL;
  1134. }
  1135. }
  1136. #define for_each_task_context_nr(ctxn) \
  1137. for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
  1138. /*
  1139. * Called from scheduler to remove the events of the current task,
  1140. * with interrupts disabled.
  1141. *
  1142. * We stop each event and update the event value in event->count.
  1143. *
  1144. * This does not protect us against NMI, but disable()
  1145. * sets the disabled bit in the control field of event _before_
  1146. * accessing the event control register. If a NMI hits, then it will
  1147. * not restart the event.
  1148. */
  1149. void __perf_event_task_sched_out(struct task_struct *task,
  1150. struct task_struct *next)
  1151. {
  1152. int ctxn;
  1153. for_each_task_context_nr(ctxn)
  1154. perf_event_context_sched_out(task, ctxn, next);
  1155. }
  1156. static void task_ctx_sched_out(struct perf_event_context *ctx,
  1157. enum event_type_t event_type)
  1158. {
  1159. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1160. if (!cpuctx->task_ctx)
  1161. return;
  1162. if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
  1163. return;
  1164. ctx_sched_out(ctx, cpuctx, event_type);
  1165. cpuctx->task_ctx = NULL;
  1166. }
  1167. /*
  1168. * Called with IRQs disabled
  1169. */
  1170. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  1171. enum event_type_t event_type)
  1172. {
  1173. ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
  1174. }
  1175. static void
  1176. ctx_pinned_sched_in(struct perf_event_context *ctx,
  1177. struct perf_cpu_context *cpuctx)
  1178. {
  1179. struct perf_event *event;
  1180. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  1181. if (event->state <= PERF_EVENT_STATE_OFF)
  1182. continue;
  1183. if (event->cpu != -1 && event->cpu != smp_processor_id())
  1184. continue;
  1185. if (group_can_go_on(event, cpuctx, 1))
  1186. group_sched_in(event, cpuctx, ctx);
  1187. /*
  1188. * If this pinned group hasn't been scheduled,
  1189. * put it in error state.
  1190. */
  1191. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1192. update_group_times(event);
  1193. event->state = PERF_EVENT_STATE_ERROR;
  1194. }
  1195. }
  1196. }
  1197. static void
  1198. ctx_flexible_sched_in(struct perf_event_context *ctx,
  1199. struct perf_cpu_context *cpuctx)
  1200. {
  1201. struct perf_event *event;
  1202. int can_add_hw = 1;
  1203. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  1204. /* Ignore events in OFF or ERROR state */
  1205. if (event->state <= PERF_EVENT_STATE_OFF)
  1206. continue;
  1207. /*
  1208. * Listen to the 'cpu' scheduling filter constraint
  1209. * of events:
  1210. */
  1211. if (event->cpu != -1 && event->cpu != smp_processor_id())
  1212. continue;
  1213. if (group_can_go_on(event, cpuctx, can_add_hw)) {
  1214. if (group_sched_in(event, cpuctx, ctx))
  1215. can_add_hw = 0;
  1216. }
  1217. }
  1218. }
  1219. static void
  1220. ctx_sched_in(struct perf_event_context *ctx,
  1221. struct perf_cpu_context *cpuctx,
  1222. enum event_type_t event_type)
  1223. {
  1224. raw_spin_lock(&ctx->lock);
  1225. ctx->is_active = 1;
  1226. if (likely(!ctx->nr_events))
  1227. goto out;
  1228. ctx->timestamp = perf_clock();
  1229. /*
  1230. * First go through the list and put on any pinned groups
  1231. * in order to give them the best chance of going on.
  1232. */
  1233. if (event_type & EVENT_PINNED)
  1234. ctx_pinned_sched_in(ctx, cpuctx);
  1235. /* Then walk through the lower prio flexible groups */
  1236. if (event_type & EVENT_FLEXIBLE)
  1237. ctx_flexible_sched_in(ctx, cpuctx);
  1238. out:
  1239. raw_spin_unlock(&ctx->lock);
  1240. }
  1241. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  1242. enum event_type_t event_type)
  1243. {
  1244. struct perf_event_context *ctx = &cpuctx->ctx;
  1245. ctx_sched_in(ctx, cpuctx, event_type);
  1246. }
  1247. static void task_ctx_sched_in(struct perf_event_context *ctx,
  1248. enum event_type_t event_type)
  1249. {
  1250. struct perf_cpu_context *cpuctx;
  1251. cpuctx = __get_cpu_context(ctx);
  1252. if (cpuctx->task_ctx == ctx)
  1253. return;
  1254. ctx_sched_in(ctx, cpuctx, event_type);
  1255. cpuctx->task_ctx = ctx;
  1256. }
  1257. void perf_event_context_sched_in(struct perf_event_context *ctx)
  1258. {
  1259. struct perf_cpu_context *cpuctx;
  1260. cpuctx = __get_cpu_context(ctx);
  1261. if (cpuctx->task_ctx == ctx)
  1262. return;
  1263. perf_pmu_disable(ctx->pmu);
  1264. /*
  1265. * We want to keep the following priority order:
  1266. * cpu pinned (that don't need to move), task pinned,
  1267. * cpu flexible, task flexible.
  1268. */
  1269. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  1270. ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
  1271. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
  1272. ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
  1273. cpuctx->task_ctx = ctx;
  1274. /*
  1275. * Since these rotations are per-cpu, we need to ensure the
  1276. * cpu-context we got scheduled on is actually rotating.
  1277. */
  1278. perf_pmu_rotate_start(ctx->pmu);
  1279. perf_pmu_enable(ctx->pmu);
  1280. }
  1281. /*
  1282. * Called from scheduler to add the events of the current task
  1283. * with interrupts disabled.
  1284. *
  1285. * We restore the event value and then enable it.
  1286. *
  1287. * This does not protect us against NMI, but enable()
  1288. * sets the enabled bit in the control field of event _before_
  1289. * accessing the event control register. If a NMI hits, then it will
  1290. * keep the event running.
  1291. */
  1292. void __perf_event_task_sched_in(struct task_struct *task)
  1293. {
  1294. struct perf_event_context *ctx;
  1295. int ctxn;
  1296. for_each_task_context_nr(ctxn) {
  1297. ctx = task->perf_event_ctxp[ctxn];
  1298. if (likely(!ctx))
  1299. continue;
  1300. perf_event_context_sched_in(ctx);
  1301. }
  1302. }
  1303. #define MAX_INTERRUPTS (~0ULL)
  1304. static void perf_log_throttle(struct perf_event *event, int enable);
  1305. static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
  1306. {
  1307. u64 frequency = event->attr.sample_freq;
  1308. u64 sec = NSEC_PER_SEC;
  1309. u64 divisor, dividend;
  1310. int count_fls, nsec_fls, frequency_fls, sec_fls;
  1311. count_fls = fls64(count);
  1312. nsec_fls = fls64(nsec);
  1313. frequency_fls = fls64(frequency);
  1314. sec_fls = 30;
  1315. /*
  1316. * We got @count in @nsec, with a target of sample_freq HZ
  1317. * the target period becomes:
  1318. *
  1319. * @count * 10^9
  1320. * period = -------------------
  1321. * @nsec * sample_freq
  1322. *
  1323. */
  1324. /*
  1325. * Reduce accuracy by one bit such that @a and @b converge
  1326. * to a similar magnitude.
  1327. */
  1328. #define REDUCE_FLS(a, b) \
  1329. do { \
  1330. if (a##_fls > b##_fls) { \
  1331. a >>= 1; \
  1332. a##_fls--; \
  1333. } else { \
  1334. b >>= 1; \
  1335. b##_fls--; \
  1336. } \
  1337. } while (0)
  1338. /*
  1339. * Reduce accuracy until either term fits in a u64, then proceed with
  1340. * the other, so that finally we can do a u64/u64 division.
  1341. */
  1342. while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
  1343. REDUCE_FLS(nsec, frequency);
  1344. REDUCE_FLS(sec, count);
  1345. }
  1346. if (count_fls + sec_fls > 64) {
  1347. divisor = nsec * frequency;
  1348. while (count_fls + sec_fls > 64) {
  1349. REDUCE_FLS(count, sec);
  1350. divisor >>= 1;
  1351. }
  1352. dividend = count * sec;
  1353. } else {
  1354. dividend = count * sec;
  1355. while (nsec_fls + frequency_fls > 64) {
  1356. REDUCE_FLS(nsec, frequency);
  1357. dividend >>= 1;
  1358. }
  1359. divisor = nsec * frequency;
  1360. }
  1361. if (!divisor)
  1362. return dividend;
  1363. return div64_u64(dividend, divisor);
  1364. }
  1365. static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
  1366. {
  1367. struct hw_perf_event *hwc = &event->hw;
  1368. s64 period, sample_period;
  1369. s64 delta;
  1370. period = perf_calculate_period(event, nsec, count);
  1371. delta = (s64)(period - hwc->sample_period);
  1372. delta = (delta + 7) / 8; /* low pass filter */
  1373. sample_period = hwc->sample_period + delta;
  1374. if (!sample_period)
  1375. sample_period = 1;
  1376. hwc->sample_period = sample_period;
  1377. if (local64_read(&hwc->period_left) > 8*sample_period) {
  1378. event->pmu->stop(event, PERF_EF_UPDATE);
  1379. local64_set(&hwc->period_left, 0);
  1380. event->pmu->start(event, PERF_EF_RELOAD);
  1381. }
  1382. }
  1383. static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
  1384. {
  1385. struct perf_event *event;
  1386. struct hw_perf_event *hwc;
  1387. u64 interrupts, now;
  1388. s64 delta;
  1389. raw_spin_lock(&ctx->lock);
  1390. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  1391. if (event->state != PERF_EVENT_STATE_ACTIVE)
  1392. continue;
  1393. if (event->cpu != -1 && event->cpu != smp_processor_id())
  1394. continue;
  1395. hwc = &event->hw;
  1396. interrupts = hwc->interrupts;
  1397. hwc->interrupts = 0;
  1398. /*
  1399. * unthrottle events on the tick
  1400. */
  1401. if (interrupts == MAX_INTERRUPTS) {
  1402. perf_log_throttle(event, 1);
  1403. event->pmu->start(event, 0);
  1404. }
  1405. if (!event->attr.freq || !event->attr.sample_freq)
  1406. continue;
  1407. event->pmu->read(event);
  1408. now = local64_read(&event->count);
  1409. delta = now - hwc->freq_count_stamp;
  1410. hwc->freq_count_stamp = now;
  1411. if (delta > 0)
  1412. perf_adjust_period(event, period, delta);
  1413. }
  1414. raw_spin_unlock(&ctx->lock);
  1415. }
  1416. /*
  1417. * Round-robin a context's events:
  1418. */
  1419. static void rotate_ctx(struct perf_event_context *ctx)
  1420. {
  1421. raw_spin_lock(&ctx->lock);
  1422. /*
  1423. * Rotate the first entry last of non-pinned groups. Rotation might be
  1424. * disabled by the inheritance code.
  1425. */
  1426. if (!ctx->rotate_disable)
  1427. list_rotate_left(&ctx->flexible_groups);
  1428. raw_spin_unlock(&ctx->lock);
  1429. }
  1430. /*
  1431. * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
  1432. * because they're strictly cpu affine and rotate_start is called with IRQs
  1433. * disabled, while rotate_context is called from IRQ context.
  1434. */
  1435. static void perf_rotate_context(struct perf_cpu_context *cpuctx)
  1436. {
  1437. u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
  1438. struct perf_event_context *ctx = NULL;
  1439. int rotate = 0, remove = 1;
  1440. if (cpuctx->ctx.nr_events) {
  1441. remove = 0;
  1442. if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
  1443. rotate = 1;
  1444. }
  1445. ctx = cpuctx->task_ctx;
  1446. if (ctx && ctx->nr_events) {
  1447. remove = 0;
  1448. if (ctx->nr_events != ctx->nr_active)
  1449. rotate = 1;
  1450. }
  1451. perf_pmu_disable(cpuctx->ctx.pmu);
  1452. perf_ctx_adjust_freq(&cpuctx->ctx, interval);
  1453. if (ctx)
  1454. perf_ctx_adjust_freq(ctx, interval);
  1455. if (!rotate)
  1456. goto done;
  1457. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  1458. if (ctx)
  1459. task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
  1460. rotate_ctx(&cpuctx->ctx);
  1461. if (ctx)
  1462. rotate_ctx(ctx);
  1463. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
  1464. if (ctx)
  1465. task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
  1466. done:
  1467. if (remove)
  1468. list_del_init(&cpuctx->rotation_list);
  1469. perf_pmu_enable(cpuctx->ctx.pmu);
  1470. }
  1471. void perf_event_task_tick(void)
  1472. {
  1473. struct list_head *head = &__get_cpu_var(rotation_list);
  1474. struct perf_cpu_context *cpuctx, *tmp;
  1475. WARN_ON(!irqs_disabled());
  1476. list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
  1477. if (cpuctx->jiffies_interval == 1 ||
  1478. !(jiffies % cpuctx->jiffies_interval))
  1479. perf_rotate_context(cpuctx);
  1480. }
  1481. }
  1482. static int event_enable_on_exec(struct perf_event *event,
  1483. struct perf_event_context *ctx)
  1484. {
  1485. if (!event->attr.enable_on_exec)
  1486. return 0;
  1487. event->attr.enable_on_exec = 0;
  1488. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  1489. return 0;
  1490. __perf_event_mark_enabled(event, ctx);
  1491. return 1;
  1492. }
  1493. /*
  1494. * Enable all of a task's events that have been marked enable-on-exec.
  1495. * This expects task == current.
  1496. */
  1497. static void perf_event_enable_on_exec(struct perf_event_context *ctx)
  1498. {
  1499. struct perf_event *event;
  1500. unsigned long flags;
  1501. int enabled = 0;
  1502. int ret;
  1503. local_irq_save(flags);
  1504. if (!ctx || !ctx->nr_events)
  1505. goto out;
  1506. task_ctx_sched_out(ctx, EVENT_ALL);
  1507. raw_spin_lock(&ctx->lock);
  1508. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  1509. ret = event_enable_on_exec(event, ctx);
  1510. if (ret)
  1511. enabled = 1;
  1512. }
  1513. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  1514. ret = event_enable_on_exec(event, ctx);
  1515. if (ret)
  1516. enabled = 1;
  1517. }
  1518. /*
  1519. * Unclone this context if we enabled any event.
  1520. */
  1521. if (enabled)
  1522. unclone_ctx(ctx);
  1523. raw_spin_unlock(&ctx->lock);
  1524. perf_event_context_sched_in(ctx);
  1525. out:
  1526. local_irq_restore(flags);
  1527. }
  1528. /*
  1529. * Cross CPU call to read the hardware event
  1530. */
  1531. static void __perf_event_read(void *info)
  1532. {
  1533. struct perf_event *event = info;
  1534. struct perf_event_context *ctx = event->ctx;
  1535. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1536. /*
  1537. * If this is a task context, we need to check whether it is
  1538. * the current task context of this cpu. If not it has been
  1539. * scheduled out before the smp call arrived. In that case
  1540. * event->count would have been updated to a recent sample
  1541. * when the event was scheduled out.
  1542. */
  1543. if (ctx->task && cpuctx->task_ctx != ctx)
  1544. return;
  1545. raw_spin_lock(&ctx->lock);
  1546. update_context_time(ctx);
  1547. update_event_times(event);
  1548. raw_spin_unlock(&ctx->lock);
  1549. event->pmu->read(event);
  1550. }
  1551. static inline u64 perf_event_count(struct perf_event *event)
  1552. {
  1553. return local64_read(&event->count) + atomic64_read(&event->child_count);
  1554. }
  1555. static u64 perf_event_read(struct perf_event *event)
  1556. {
  1557. /*
  1558. * If event is enabled and currently active on a CPU, update the
  1559. * value in the event structure:
  1560. */
  1561. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  1562. smp_call_function_single(event->oncpu,
  1563. __perf_event_read, event, 1);
  1564. } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1565. struct perf_event_context *ctx = event->ctx;
  1566. unsigned long flags;
  1567. raw_spin_lock_irqsave(&ctx->lock, flags);
  1568. /*
  1569. * may read while context is not active
  1570. * (e.g., thread is blocked), in that case
  1571. * we cannot update context time
  1572. */
  1573. if (ctx->is_active)
  1574. update_context_time(ctx);
  1575. update_event_times(event);
  1576. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1577. }
  1578. return perf_event_count(event);
  1579. }
  1580. /*
  1581. * Callchain support
  1582. */
  1583. struct callchain_cpus_entries {
  1584. struct rcu_head rcu_head;
  1585. struct perf_callchain_entry *cpu_entries[0];
  1586. };
  1587. static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
  1588. static atomic_t nr_callchain_events;
  1589. static DEFINE_MUTEX(callchain_mutex);
  1590. struct callchain_cpus_entries *callchain_cpus_entries;
  1591. __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
  1592. struct pt_regs *regs)
  1593. {
  1594. }
  1595. __weak void perf_callchain_user(struct perf_callchain_entry *entry,
  1596. struct pt_regs *regs)
  1597. {
  1598. }
  1599. static void release_callchain_buffers_rcu(struct rcu_head *head)
  1600. {
  1601. struct callchain_cpus_entries *entries;
  1602. int cpu;
  1603. entries = container_of(head, struct callchain_cpus_entries, rcu_head);
  1604. for_each_possible_cpu(cpu)
  1605. kfree(entries->cpu_entries[cpu]);
  1606. kfree(entries);
  1607. }
  1608. static void release_callchain_buffers(void)
  1609. {
  1610. struct callchain_cpus_entries *entries;
  1611. entries = callchain_cpus_entries;
  1612. rcu_assign_pointer(callchain_cpus_entries, NULL);
  1613. call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
  1614. }
  1615. static int alloc_callchain_buffers(void)
  1616. {
  1617. int cpu;
  1618. int size;
  1619. struct callchain_cpus_entries *entries;
  1620. /*
  1621. * We can't use the percpu allocation API for data that can be
  1622. * accessed from NMI. Use a temporary manual per cpu allocation
  1623. * until that gets sorted out.
  1624. */
  1625. size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
  1626. num_possible_cpus();
  1627. entries = kzalloc(size, GFP_KERNEL);
  1628. if (!entries)
  1629. return -ENOMEM;
  1630. size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
  1631. for_each_possible_cpu(cpu) {
  1632. entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
  1633. cpu_to_node(cpu));
  1634. if (!entries->cpu_entries[cpu])
  1635. goto fail;
  1636. }
  1637. rcu_assign_pointer(callchain_cpus_entries, entries);
  1638. return 0;
  1639. fail:
  1640. for_each_possible_cpu(cpu)
  1641. kfree(entries->cpu_entries[cpu]);
  1642. kfree(entries);
  1643. return -ENOMEM;
  1644. }
  1645. static int get_callchain_buffers(void)
  1646. {
  1647. int err = 0;
  1648. int count;
  1649. mutex_lock(&callchain_mutex);
  1650. count = atomic_inc_return(&nr_callchain_events);
  1651. if (WARN_ON_ONCE(count < 1)) {
  1652. err = -EINVAL;
  1653. goto exit;
  1654. }
  1655. if (count > 1) {
  1656. /* If the allocation failed, give up */
  1657. if (!callchain_cpus_entries)
  1658. err = -ENOMEM;
  1659. goto exit;
  1660. }
  1661. err = alloc_callchain_buffers();
  1662. if (err)
  1663. release_callchain_buffers();
  1664. exit:
  1665. mutex_unlock(&callchain_mutex);
  1666. return err;
  1667. }
  1668. static void put_callchain_buffers(void)
  1669. {
  1670. if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
  1671. release_callchain_buffers();
  1672. mutex_unlock(&callchain_mutex);
  1673. }
  1674. }
  1675. static int get_recursion_context(int *recursion)
  1676. {
  1677. int rctx;
  1678. if (in_nmi())
  1679. rctx = 3;
  1680. else if (in_irq())
  1681. rctx = 2;
  1682. else if (in_softirq())
  1683. rctx = 1;
  1684. else
  1685. rctx = 0;
  1686. if (recursion[rctx])
  1687. return -1;
  1688. recursion[rctx]++;
  1689. barrier();
  1690. return rctx;
  1691. }
  1692. static inline void put_recursion_context(int *recursion, int rctx)
  1693. {
  1694. barrier();
  1695. recursion[rctx]--;
  1696. }
  1697. static struct perf_callchain_entry *get_callchain_entry(int *rctx)
  1698. {
  1699. int cpu;
  1700. struct callchain_cpus_entries *entries;
  1701. *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
  1702. if (*rctx == -1)
  1703. return NULL;
  1704. entries = rcu_dereference(callchain_cpus_entries);
  1705. if (!entries)
  1706. return NULL;
  1707. cpu = smp_processor_id();
  1708. return &entries->cpu_entries[cpu][*rctx];
  1709. }
  1710. static void
  1711. put_callchain_entry(int rctx)
  1712. {
  1713. put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
  1714. }
  1715. static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  1716. {
  1717. int rctx;
  1718. struct perf_callchain_entry *entry;
  1719. entry = get_callchain_entry(&rctx);
  1720. if (rctx == -1)
  1721. return NULL;
  1722. if (!entry)
  1723. goto exit_put;
  1724. entry->nr = 0;
  1725. if (!user_mode(regs)) {
  1726. perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
  1727. perf_callchain_kernel(entry, regs);
  1728. if (current->mm)
  1729. regs = task_pt_regs(current);
  1730. else
  1731. regs = NULL;
  1732. }
  1733. if (regs) {
  1734. perf_callchain_store(entry, PERF_CONTEXT_USER);
  1735. perf_callchain_user(entry, regs);
  1736. }
  1737. exit_put:
  1738. put_callchain_entry(rctx);
  1739. return entry;
  1740. }
  1741. /*
  1742. * Initialize the perf_event context in a task_struct:
  1743. */
  1744. static void __perf_event_init_context(struct perf_event_context *ctx)
  1745. {
  1746. raw_spin_lock_init(&ctx->lock);
  1747. mutex_init(&ctx->mutex);
  1748. INIT_LIST_HEAD(&ctx->pinned_groups);
  1749. INIT_LIST_HEAD(&ctx->flexible_groups);
  1750. INIT_LIST_HEAD(&ctx->event_list);
  1751. atomic_set(&ctx->refcount, 1);
  1752. }
  1753. static struct perf_event_context *
  1754. alloc_perf_context(struct pmu *pmu, struct task_struct *task)
  1755. {
  1756. struct perf_event_context *ctx;
  1757. ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
  1758. if (!ctx)
  1759. return NULL;
  1760. __perf_event_init_context(ctx);
  1761. if (task) {
  1762. ctx->task = task;
  1763. get_task_struct(task);
  1764. }
  1765. ctx->pmu = pmu;
  1766. return ctx;
  1767. }
  1768. static struct task_struct *
  1769. find_lively_task_by_vpid(pid_t vpid)
  1770. {
  1771. struct task_struct *task;
  1772. int err;
  1773. rcu_read_lock();
  1774. if (!vpid)
  1775. task = current;
  1776. else
  1777. task = find_task_by_vpid(vpid);
  1778. if (task)
  1779. get_task_struct(task);
  1780. rcu_read_unlock();
  1781. if (!task)
  1782. return ERR_PTR(-ESRCH);
  1783. /*
  1784. * Can't attach events to a dying task.
  1785. */
  1786. err = -ESRCH;
  1787. if (task->flags & PF_EXITING)
  1788. goto errout;
  1789. /* Reuse ptrace permission checks for now. */
  1790. err = -EACCES;
  1791. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  1792. goto errout;
  1793. return task;
  1794. errout:
  1795. put_task_struct(task);
  1796. return ERR_PTR(err);
  1797. }
  1798. static struct perf_event_context *
  1799. find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
  1800. {
  1801. struct perf_event_context *ctx;
  1802. struct perf_cpu_context *cpuctx;
  1803. unsigned long flags;
  1804. int ctxn, err;
  1805. if (!task && cpu != -1) {
  1806. /* Must be root to operate on a CPU event: */
  1807. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  1808. return ERR_PTR(-EACCES);
  1809. if (cpu < 0 || cpu >= nr_cpumask_bits)
  1810. return ERR_PTR(-EINVAL);
  1811. /*
  1812. * We could be clever and allow to attach a event to an
  1813. * offline CPU and activate it when the CPU comes up, but
  1814. * that's for later.
  1815. */
  1816. if (!cpu_online(cpu))
  1817. return ERR_PTR(-ENODEV);
  1818. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  1819. ctx = &cpuctx->ctx;
  1820. get_ctx(ctx);
  1821. return ctx;
  1822. }
  1823. err = -EINVAL;
  1824. ctxn = pmu->task_ctx_nr;
  1825. if (ctxn < 0)
  1826. goto errout;
  1827. retry:
  1828. ctx = perf_lock_task_context(task, ctxn, &flags);
  1829. if (ctx) {
  1830. unclone_ctx(ctx);
  1831. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1832. }
  1833. if (!ctx) {
  1834. ctx = alloc_perf_context(pmu, task);
  1835. err = -ENOMEM;
  1836. if (!ctx)
  1837. goto errout;
  1838. get_ctx(ctx);
  1839. if (cmpxchg(&task->perf_event_ctxp[ctxn], NULL, ctx)) {
  1840. /*
  1841. * We raced with some other task; use
  1842. * the context they set.
  1843. */
  1844. put_task_struct(task);
  1845. kfree(ctx);
  1846. goto retry;
  1847. }
  1848. }
  1849. return ctx;
  1850. errout:
  1851. return ERR_PTR(err);
  1852. }
  1853. static void perf_event_free_filter(struct perf_event *event);
  1854. static void free_event_rcu(struct rcu_head *head)
  1855. {
  1856. struct perf_event *event;
  1857. event = container_of(head, struct perf_event, rcu_head);
  1858. if (event->ns)
  1859. put_pid_ns(event->ns);
  1860. perf_event_free_filter(event);
  1861. kfree(event);
  1862. }
  1863. static void perf_buffer_put(struct perf_buffer *buffer);
  1864. static void free_event(struct perf_event *event)
  1865. {
  1866. irq_work_sync(&event->pending);
  1867. if (!event->parent) {
  1868. if (event->attach_state & PERF_ATTACH_TASK)
  1869. jump_label_dec(&perf_task_events);
  1870. if (event->attr.mmap || event->attr.mmap_data)
  1871. atomic_dec(&nr_mmap_events);
  1872. if (event->attr.comm)
  1873. atomic_dec(&nr_comm_events);
  1874. if (event->attr.task)
  1875. atomic_dec(&nr_task_events);
  1876. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
  1877. put_callchain_buffers();
  1878. }
  1879. if (event->buffer) {
  1880. perf_buffer_put(event->buffer);
  1881. event->buffer = NULL;
  1882. }
  1883. if (event->destroy)
  1884. event->destroy(event);
  1885. if (event->ctx)
  1886. put_ctx(event->ctx);
  1887. call_rcu(&event->rcu_head, free_event_rcu);
  1888. }
  1889. int perf_event_release_kernel(struct perf_event *event)
  1890. {
  1891. struct perf_event_context *ctx = event->ctx;
  1892. /*
  1893. * Remove from the PMU, can't get re-enabled since we got
  1894. * here because the last ref went.
  1895. */
  1896. perf_event_disable(event);
  1897. WARN_ON_ONCE(ctx->parent_ctx);
  1898. /*
  1899. * There are two ways this annotation is useful:
  1900. *
  1901. * 1) there is a lock recursion from perf_event_exit_task
  1902. * see the comment there.
  1903. *
  1904. * 2) there is a lock-inversion with mmap_sem through
  1905. * perf_event_read_group(), which takes faults while
  1906. * holding ctx->mutex, however this is called after
  1907. * the last filedesc died, so there is no possibility
  1908. * to trigger the AB-BA case.
  1909. */
  1910. mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
  1911. raw_spin_lock_irq(&ctx->lock);
  1912. perf_group_detach(event);
  1913. list_del_event(event, ctx);
  1914. raw_spin_unlock_irq(&ctx->lock);
  1915. mutex_unlock(&ctx->mutex);
  1916. free_event(event);
  1917. return 0;
  1918. }
  1919. EXPORT_SYMBOL_GPL(perf_event_release_kernel);
  1920. /*
  1921. * Called when the last reference to the file is gone.
  1922. */
  1923. static int perf_release(struct inode *inode, struct file *file)
  1924. {
  1925. struct perf_event *event = file->private_data;
  1926. struct task_struct *owner;
  1927. file->private_data = NULL;
  1928. rcu_read_lock();
  1929. owner = ACCESS_ONCE(event->owner);
  1930. /*
  1931. * Matches the smp_wmb() in perf_event_exit_task(). If we observe
  1932. * !owner it means the list deletion is complete and we can indeed
  1933. * free this event, otherwise we need to serialize on
  1934. * owner->perf_event_mutex.
  1935. */
  1936. smp_read_barrier_depends();
  1937. if (owner) {
  1938. /*
  1939. * Since delayed_put_task_struct() also drops the last
  1940. * task reference we can safely take a new reference
  1941. * while holding the rcu_read_lock().
  1942. */
  1943. get_task_struct(owner);
  1944. }
  1945. rcu_read_unlock();
  1946. if (owner) {
  1947. mutex_lock(&owner->perf_event_mutex);
  1948. /*
  1949. * We have to re-check the event->owner field, if it is cleared
  1950. * we raced with perf_event_exit_task(), acquiring the mutex
  1951. * ensured they're done, and we can proceed with freeing the
  1952. * event.
  1953. */
  1954. if (event->owner)
  1955. list_del_init(&event->owner_entry);
  1956. mutex_unlock(&owner->perf_event_mutex);
  1957. put_task_struct(owner);
  1958. }
  1959. return perf_event_release_kernel(event);
  1960. }
  1961. u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
  1962. {
  1963. struct perf_event *child;
  1964. u64 total = 0;
  1965. *enabled = 0;
  1966. *running = 0;
  1967. mutex_lock(&event->child_mutex);
  1968. total += perf_event_read(event);
  1969. *enabled += event->total_time_enabled +
  1970. atomic64_read(&event->child_total_time_enabled);
  1971. *running += event->total_time_running +
  1972. atomic64_read(&event->child_total_time_running);
  1973. list_for_each_entry(child, &event->child_list, child_list) {
  1974. total += perf_event_read(child);
  1975. *enabled += child->total_time_enabled;
  1976. *running += child->total_time_running;
  1977. }
  1978. mutex_unlock(&event->child_mutex);
  1979. return total;
  1980. }
  1981. EXPORT_SYMBOL_GPL(perf_event_read_value);
  1982. static int perf_event_read_group(struct perf_event *event,
  1983. u64 read_format, char __user *buf)
  1984. {
  1985. struct perf_event *leader = event->group_leader, *sub;
  1986. int n = 0, size = 0, ret = -EFAULT;
  1987. struct perf_event_context *ctx = leader->ctx;
  1988. u64 values[5];
  1989. u64 count, enabled, running;
  1990. mutex_lock(&ctx->mutex);
  1991. count = perf_event_read_value(leader, &enabled, &running);
  1992. values[n++] = 1 + leader->nr_siblings;
  1993. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1994. values[n++] = enabled;
  1995. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1996. values[n++] = running;
  1997. values[n++] = count;
  1998. if (read_format & PERF_FORMAT_ID)
  1999. values[n++] = primary_event_id(leader);
  2000. size = n * sizeof(u64);
  2001. if (copy_to_user(buf, values, size))
  2002. goto unlock;
  2003. ret = size;
  2004. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  2005. n = 0;
  2006. values[n++] = perf_event_read_value(sub, &enabled, &running);
  2007. if (read_format & PERF_FORMAT_ID)
  2008. values[n++] = primary_event_id(sub);
  2009. size = n * sizeof(u64);
  2010. if (copy_to_user(buf + ret, values, size)) {
  2011. ret = -EFAULT;
  2012. goto unlock;
  2013. }
  2014. ret += size;
  2015. }
  2016. unlock:
  2017. mutex_unlock(&ctx->mutex);
  2018. return ret;
  2019. }
  2020. static int perf_event_read_one(struct perf_event *event,
  2021. u64 read_format, char __user *buf)
  2022. {
  2023. u64 enabled, running;
  2024. u64 values[4];
  2025. int n = 0;
  2026. values[n++] = perf_event_read_value(event, &enabled, &running);
  2027. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2028. values[n++] = enabled;
  2029. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2030. values[n++] = running;
  2031. if (read_format & PERF_FORMAT_ID)
  2032. values[n++] = primary_event_id(event);
  2033. if (copy_to_user(buf, values, n * sizeof(u64)))
  2034. return -EFAULT;
  2035. return n * sizeof(u64);
  2036. }
  2037. /*
  2038. * Read the performance event - simple non blocking version for now
  2039. */
  2040. static ssize_t
  2041. perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
  2042. {
  2043. u64 read_format = event->attr.read_format;
  2044. int ret;
  2045. /*
  2046. * Return end-of-file for a read on a event that is in
  2047. * error state (i.e. because it was pinned but it couldn't be
  2048. * scheduled on to the CPU at some point).
  2049. */
  2050. if (event->state == PERF_EVENT_STATE_ERROR)
  2051. return 0;
  2052. if (count < event->read_size)
  2053. return -ENOSPC;
  2054. WARN_ON_ONCE(event->ctx->parent_ctx);
  2055. if (read_format & PERF_FORMAT_GROUP)
  2056. ret = perf_event_read_group(event, read_format, buf);
  2057. else
  2058. ret = perf_event_read_one(event, read_format, buf);
  2059. return ret;
  2060. }
  2061. static ssize_t
  2062. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  2063. {
  2064. struct perf_event *event = file->private_data;
  2065. return perf_read_hw(event, buf, count);
  2066. }
  2067. static unsigned int perf_poll(struct file *file, poll_table *wait)
  2068. {
  2069. struct perf_event *event = file->private_data;
  2070. struct perf_buffer *buffer;
  2071. unsigned int events = POLL_HUP;
  2072. rcu_read_lock();
  2073. buffer = rcu_dereference(event->buffer);
  2074. if (buffer)
  2075. events = atomic_xchg(&buffer->poll, 0);
  2076. rcu_read_unlock();
  2077. poll_wait(file, &event->waitq, wait);
  2078. return events;
  2079. }
  2080. static void perf_event_reset(struct perf_event *event)
  2081. {
  2082. (void)perf_event_read(event);
  2083. local64_set(&event->count, 0);
  2084. perf_event_update_userpage(event);
  2085. }
  2086. /*
  2087. * Holding the top-level event's child_mutex means that any
  2088. * descendant process that has inherited this event will block
  2089. * in sync_child_event if it goes to exit, thus satisfying the
  2090. * task existence requirements of perf_event_enable/disable.
  2091. */
  2092. static void perf_event_for_each_child(struct perf_event *event,
  2093. void (*func)(struct perf_event *))
  2094. {
  2095. struct perf_event *child;
  2096. WARN_ON_ONCE(event->ctx->parent_ctx);
  2097. mutex_lock(&event->child_mutex);
  2098. func(event);
  2099. list_for_each_entry(child, &event->child_list, child_list)
  2100. func(child);
  2101. mutex_unlock(&event->child_mutex);
  2102. }
  2103. static void perf_event_for_each(struct perf_event *event,
  2104. void (*func)(struct perf_event *))
  2105. {
  2106. struct perf_event_context *ctx = event->ctx;
  2107. struct perf_event *sibling;
  2108. WARN_ON_ONCE(ctx->parent_ctx);
  2109. mutex_lock(&ctx->mutex);
  2110. event = event->group_leader;
  2111. perf_event_for_each_child(event, func);
  2112. func(event);
  2113. list_for_each_entry(sibling, &event->sibling_list, group_entry)
  2114. perf_event_for_each_child(event, func);
  2115. mutex_unlock(&ctx->mutex);
  2116. }
  2117. static int perf_event_period(struct perf_event *event, u64 __user *arg)
  2118. {
  2119. struct perf_event_context *ctx = event->ctx;
  2120. int ret = 0;
  2121. u64 value;
  2122. if (!is_sampling_event(event))
  2123. return -EINVAL;
  2124. if (copy_from_user(&value, arg, sizeof(value)))
  2125. return -EFAULT;
  2126. if (!value)
  2127. return -EINVAL;
  2128. raw_spin_lock_irq(&ctx->lock);
  2129. if (event->attr.freq) {
  2130. if (value > sysctl_perf_event_sample_rate) {
  2131. ret = -EINVAL;
  2132. goto unlock;
  2133. }
  2134. event->attr.sample_freq = value;
  2135. } else {
  2136. event->attr.sample_period = value;
  2137. event->hw.sample_period = value;
  2138. }
  2139. unlock:
  2140. raw_spin_unlock_irq(&ctx->lock);
  2141. return ret;
  2142. }
  2143. static const struct file_operations perf_fops;
  2144. static struct perf_event *perf_fget_light(int fd, int *fput_needed)
  2145. {
  2146. struct file *file;
  2147. file = fget_light(fd, fput_needed);
  2148. if (!file)
  2149. return ERR_PTR(-EBADF);
  2150. if (file->f_op != &perf_fops) {
  2151. fput_light(file, *fput_needed);
  2152. *fput_needed = 0;
  2153. return ERR_PTR(-EBADF);
  2154. }
  2155. return file->private_data;
  2156. }
  2157. static int perf_event_set_output(struct perf_event *event,
  2158. struct perf_event *output_event);
  2159. static int perf_event_set_filter(struct perf_event *event, void __user *arg);
  2160. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2161. {
  2162. struct perf_event *event = file->private_data;
  2163. void (*func)(struct perf_event *);
  2164. u32 flags = arg;
  2165. switch (cmd) {
  2166. case PERF_EVENT_IOC_ENABLE:
  2167. func = perf_event_enable;
  2168. break;
  2169. case PERF_EVENT_IOC_DISABLE:
  2170. func = perf_event_disable;
  2171. break;
  2172. case PERF_EVENT_IOC_RESET:
  2173. func = perf_event_reset;
  2174. break;
  2175. case PERF_EVENT_IOC_REFRESH:
  2176. return perf_event_refresh(event, arg);
  2177. case PERF_EVENT_IOC_PERIOD:
  2178. return perf_event_period(event, (u64 __user *)arg);
  2179. case PERF_EVENT_IOC_SET_OUTPUT:
  2180. {
  2181. struct perf_event *output_event = NULL;
  2182. int fput_needed = 0;
  2183. int ret;
  2184. if (arg != -1) {
  2185. output_event = perf_fget_light(arg, &fput_needed);
  2186. if (IS_ERR(output_event))
  2187. return PTR_ERR(output_event);
  2188. }
  2189. ret = perf_event_set_output(event, output_event);
  2190. if (output_event)
  2191. fput_light(output_event->filp, fput_needed);
  2192. return ret;
  2193. }
  2194. case PERF_EVENT_IOC_SET_FILTER:
  2195. return perf_event_set_filter(event, (void __user *)arg);
  2196. default:
  2197. return -ENOTTY;
  2198. }
  2199. if (flags & PERF_IOC_FLAG_GROUP)
  2200. perf_event_for_each(event, func);
  2201. else
  2202. perf_event_for_each_child(event, func);
  2203. return 0;
  2204. }
  2205. int perf_event_task_enable(void)
  2206. {
  2207. struct perf_event *event;
  2208. mutex_lock(&current->perf_event_mutex);
  2209. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  2210. perf_event_for_each_child(event, perf_event_enable);
  2211. mutex_unlock(&current->perf_event_mutex);
  2212. return 0;
  2213. }
  2214. int perf_event_task_disable(void)
  2215. {
  2216. struct perf_event *event;
  2217. mutex_lock(&current->perf_event_mutex);
  2218. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  2219. perf_event_for_each_child(event, perf_event_disable);
  2220. mutex_unlock(&current->perf_event_mutex);
  2221. return 0;
  2222. }
  2223. #ifndef PERF_EVENT_INDEX_OFFSET
  2224. # define PERF_EVENT_INDEX_OFFSET 0
  2225. #endif
  2226. static int perf_event_index(struct perf_event *event)
  2227. {
  2228. if (event->hw.state & PERF_HES_STOPPED)
  2229. return 0;
  2230. if (event->state != PERF_EVENT_STATE_ACTIVE)
  2231. return 0;
  2232. return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
  2233. }
  2234. /*
  2235. * Callers need to ensure there can be no nesting of this function, otherwise
  2236. * the seqlock logic goes bad. We can not serialize this because the arch
  2237. * code calls this from NMI context.
  2238. */
  2239. void perf_event_update_userpage(struct perf_event *event)
  2240. {
  2241. struct perf_event_mmap_page *userpg;
  2242. struct perf_buffer *buffer;
  2243. rcu_read_lock();
  2244. buffer = rcu_dereference(event->buffer);
  2245. if (!buffer)
  2246. goto unlock;
  2247. userpg = buffer->user_page;
  2248. /*
  2249. * Disable preemption so as to not let the corresponding user-space
  2250. * spin too long if we get preempted.
  2251. */
  2252. preempt_disable();
  2253. ++userpg->lock;
  2254. barrier();
  2255. userpg->index = perf_event_index(event);
  2256. userpg->offset = perf_event_count(event);
  2257. if (event->state == PERF_EVENT_STATE_ACTIVE)
  2258. userpg->offset -= local64_read(&event->hw.prev_count);
  2259. userpg->time_enabled = event->total_time_enabled +
  2260. atomic64_read(&event->child_total_time_enabled);
  2261. userpg->time_running = event->total_time_running +
  2262. atomic64_read(&event->child_total_time_running);
  2263. barrier();
  2264. ++userpg->lock;
  2265. preempt_enable();
  2266. unlock:
  2267. rcu_read_unlock();
  2268. }
  2269. static unsigned long perf_data_size(struct perf_buffer *buffer);
  2270. static void
  2271. perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
  2272. {
  2273. long max_size = perf_data_size(buffer);
  2274. if (watermark)
  2275. buffer->watermark = min(max_size, watermark);
  2276. if (!buffer->watermark)
  2277. buffer->watermark = max_size / 2;
  2278. if (flags & PERF_BUFFER_WRITABLE)
  2279. buffer->writable = 1;
  2280. atomic_set(&buffer->refcount, 1);
  2281. }
  2282. #ifndef CONFIG_PERF_USE_VMALLOC
  2283. /*
  2284. * Back perf_mmap() with regular GFP_KERNEL-0 pages.
  2285. */
  2286. static struct page *
  2287. perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
  2288. {
  2289. if (pgoff > buffer->nr_pages)
  2290. return NULL;
  2291. if (pgoff == 0)
  2292. return virt_to_page(buffer->user_page);
  2293. return virt_to_page(buffer->data_pages[pgoff - 1]);
  2294. }
  2295. static void *perf_mmap_alloc_page(int cpu)
  2296. {
  2297. struct page *page;
  2298. int node;
  2299. node = (cpu == -1) ? cpu : cpu_to_node(cpu);
  2300. page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  2301. if (!page)
  2302. return NULL;
  2303. return page_address(page);
  2304. }
  2305. static struct perf_buffer *
  2306. perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
  2307. {
  2308. struct perf_buffer *buffer;
  2309. unsigned long size;
  2310. int i;
  2311. size = sizeof(struct perf_buffer);
  2312. size += nr_pages * sizeof(void *);
  2313. buffer = kzalloc(size, GFP_KERNEL);
  2314. if (!buffer)
  2315. goto fail;
  2316. buffer->user_page = perf_mmap_alloc_page(cpu);
  2317. if (!buffer->user_page)
  2318. goto fail_user_page;
  2319. for (i = 0; i < nr_pages; i++) {
  2320. buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
  2321. if (!buffer->data_pages[i])
  2322. goto fail_data_pages;
  2323. }
  2324. buffer->nr_pages = nr_pages;
  2325. perf_buffer_init(buffer, watermark, flags);
  2326. return buffer;
  2327. fail_data_pages:
  2328. for (i--; i >= 0; i--)
  2329. free_page((unsigned long)buffer->data_pages[i]);
  2330. free_page((unsigned long)buffer->user_page);
  2331. fail_user_page:
  2332. kfree(buffer);
  2333. fail:
  2334. return NULL;
  2335. }
  2336. static void perf_mmap_free_page(unsigned long addr)
  2337. {
  2338. struct page *page = virt_to_page((void *)addr);
  2339. page->mapping = NULL;
  2340. __free_page(page);
  2341. }
  2342. static void perf_buffer_free(struct perf_buffer *buffer)
  2343. {
  2344. int i;
  2345. perf_mmap_free_page((unsigned long)buffer->user_page);
  2346. for (i = 0; i < buffer->nr_pages; i++)
  2347. perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
  2348. kfree(buffer);
  2349. }
  2350. static inline int page_order(struct perf_buffer *buffer)
  2351. {
  2352. return 0;
  2353. }
  2354. #else
  2355. /*
  2356. * Back perf_mmap() with vmalloc memory.
  2357. *
  2358. * Required for architectures that have d-cache aliasing issues.
  2359. */
  2360. static inline int page_order(struct perf_buffer *buffer)
  2361. {
  2362. return buffer->page_order;
  2363. }
  2364. static struct page *
  2365. perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
  2366. {
  2367. if (pgoff > (1UL << page_order(buffer)))
  2368. return NULL;
  2369. return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
  2370. }
  2371. static void perf_mmap_unmark_page(void *addr)
  2372. {
  2373. struct page *page = vmalloc_to_page(addr);
  2374. page->mapping = NULL;
  2375. }
  2376. static void perf_buffer_free_work(struct work_struct *work)
  2377. {
  2378. struct perf_buffer *buffer;
  2379. void *base;
  2380. int i, nr;
  2381. buffer = container_of(work, struct perf_buffer, work);
  2382. nr = 1 << page_order(buffer);
  2383. base = buffer->user_page;
  2384. for (i = 0; i < nr + 1; i++)
  2385. perf_mmap_unmark_page(base + (i * PAGE_SIZE));
  2386. vfree(base);
  2387. kfree(buffer);
  2388. }
  2389. static void perf_buffer_free(struct perf_buffer *buffer)
  2390. {
  2391. schedule_work(&buffer->work);
  2392. }
  2393. static struct perf_buffer *
  2394. perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
  2395. {
  2396. struct perf_buffer *buffer;
  2397. unsigned long size;
  2398. void *all_buf;
  2399. size = sizeof(struct perf_buffer);
  2400. size += sizeof(void *);
  2401. buffer = kzalloc(size, GFP_KERNEL);
  2402. if (!buffer)
  2403. goto fail;
  2404. INIT_WORK(&buffer->work, perf_buffer_free_work);
  2405. all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
  2406. if (!all_buf)
  2407. goto fail_all_buf;
  2408. buffer->user_page = all_buf;
  2409. buffer->data_pages[0] = all_buf + PAGE_SIZE;
  2410. buffer->page_order = ilog2(nr_pages);
  2411. buffer->nr_pages = 1;
  2412. perf_buffer_init(buffer, watermark, flags);
  2413. return buffer;
  2414. fail_all_buf:
  2415. kfree(buffer);
  2416. fail:
  2417. return NULL;
  2418. }
  2419. #endif
  2420. static unsigned long perf_data_size(struct perf_buffer *buffer)
  2421. {
  2422. return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
  2423. }
  2424. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  2425. {
  2426. struct perf_event *event = vma->vm_file->private_data;
  2427. struct perf_buffer *buffer;
  2428. int ret = VM_FAULT_SIGBUS;
  2429. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  2430. if (vmf->pgoff == 0)
  2431. ret = 0;
  2432. return ret;
  2433. }
  2434. rcu_read_lock();
  2435. buffer = rcu_dereference(event->buffer);
  2436. if (!buffer)
  2437. goto unlock;
  2438. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  2439. goto unlock;
  2440. vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
  2441. if (!vmf->page)
  2442. goto unlock;
  2443. get_page(vmf->page);
  2444. vmf->page->mapping = vma->vm_file->f_mapping;
  2445. vmf->page->index = vmf->pgoff;
  2446. ret = 0;
  2447. unlock:
  2448. rcu_read_unlock();
  2449. return ret;
  2450. }
  2451. static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
  2452. {
  2453. struct perf_buffer *buffer;
  2454. buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
  2455. perf_buffer_free(buffer);
  2456. }
  2457. static struct perf_buffer *perf_buffer_get(struct perf_event *event)
  2458. {
  2459. struct perf_buffer *buffer;
  2460. rcu_read_lock();
  2461. buffer = rcu_dereference(event->buffer);
  2462. if (buffer) {
  2463. if (!atomic_inc_not_zero(&buffer->refcount))
  2464. buffer = NULL;
  2465. }
  2466. rcu_read_unlock();
  2467. return buffer;
  2468. }
  2469. static void perf_buffer_put(struct perf_buffer *buffer)
  2470. {
  2471. if (!atomic_dec_and_test(&buffer->refcount))
  2472. return;
  2473. call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
  2474. }
  2475. static void perf_mmap_open(struct vm_area_struct *vma)
  2476. {
  2477. struct perf_event *event = vma->vm_file->private_data;
  2478. atomic_inc(&event->mmap_count);
  2479. }
  2480. static void perf_mmap_close(struct vm_area_struct *vma)
  2481. {
  2482. struct perf_event *event = vma->vm_file->private_data;
  2483. if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
  2484. unsigned long size = perf_data_size(event->buffer);
  2485. struct user_struct *user = event->mmap_user;
  2486. struct perf_buffer *buffer = event->buffer;
  2487. atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
  2488. vma->vm_mm->locked_vm -= event->mmap_locked;
  2489. rcu_assign_pointer(event->buffer, NULL);
  2490. mutex_unlock(&event->mmap_mutex);
  2491. perf_buffer_put(buffer);
  2492. free_uid(user);
  2493. }
  2494. }
  2495. static const struct vm_operations_struct perf_mmap_vmops = {
  2496. .open = perf_mmap_open,
  2497. .close = perf_mmap_close,
  2498. .fault = perf_mmap_fault,
  2499. .page_mkwrite = perf_mmap_fault,
  2500. };
  2501. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  2502. {
  2503. struct perf_event *event = file->private_data;
  2504. unsigned long user_locked, user_lock_limit;
  2505. struct user_struct *user = current_user();
  2506. unsigned long locked, lock_limit;
  2507. struct perf_buffer *buffer;
  2508. unsigned long vma_size;
  2509. unsigned long nr_pages;
  2510. long user_extra, extra;
  2511. int ret = 0, flags = 0;
  2512. /*
  2513. * Don't allow mmap() of inherited per-task counters. This would
  2514. * create a performance issue due to all children writing to the
  2515. * same buffer.
  2516. */
  2517. if (event->cpu == -1 && event->attr.inherit)
  2518. return -EINVAL;
  2519. if (!(vma->vm_flags & VM_SHARED))
  2520. return -EINVAL;
  2521. vma_size = vma->vm_end - vma->vm_start;
  2522. nr_pages = (vma_size / PAGE_SIZE) - 1;
  2523. /*
  2524. * If we have buffer pages ensure they're a power-of-two number, so we
  2525. * can do bitmasks instead of modulo.
  2526. */
  2527. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  2528. return -EINVAL;
  2529. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  2530. return -EINVAL;
  2531. if (vma->vm_pgoff != 0)
  2532. return -EINVAL;
  2533. WARN_ON_ONCE(event->ctx->parent_ctx);
  2534. mutex_lock(&event->mmap_mutex);
  2535. if (event->buffer) {
  2536. if (event->buffer->nr_pages == nr_pages)
  2537. atomic_inc(&event->buffer->refcount);
  2538. else
  2539. ret = -EINVAL;
  2540. goto unlock;
  2541. }
  2542. user_extra = nr_pages + 1;
  2543. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  2544. /*
  2545. * Increase the limit linearly with more CPUs:
  2546. */
  2547. user_lock_limit *= num_online_cpus();
  2548. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  2549. extra = 0;
  2550. if (user_locked > user_lock_limit)
  2551. extra = user_locked - user_lock_limit;
  2552. lock_limit = rlimit(RLIMIT_MEMLOCK);
  2553. lock_limit >>= PAGE_SHIFT;
  2554. locked = vma->vm_mm->locked_vm + extra;
  2555. if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  2556. !capable(CAP_IPC_LOCK)) {
  2557. ret = -EPERM;
  2558. goto unlock;
  2559. }
  2560. WARN_ON(event->buffer);
  2561. if (vma->vm_flags & VM_WRITE)
  2562. flags |= PERF_BUFFER_WRITABLE;
  2563. buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
  2564. event->cpu, flags);
  2565. if (!buffer) {
  2566. ret = -ENOMEM;
  2567. goto unlock;
  2568. }
  2569. rcu_assign_pointer(event->buffer, buffer);
  2570. atomic_long_add(user_extra, &user->locked_vm);
  2571. event->mmap_locked = extra;
  2572. event->mmap_user = get_current_user();
  2573. vma->vm_mm->locked_vm += event->mmap_locked;
  2574. unlock:
  2575. if (!ret)
  2576. atomic_inc(&event->mmap_count);
  2577. mutex_unlock(&event->mmap_mutex);
  2578. vma->vm_flags |= VM_RESERVED;
  2579. vma->vm_ops = &perf_mmap_vmops;
  2580. return ret;
  2581. }
  2582. static int perf_fasync(int fd, struct file *filp, int on)
  2583. {
  2584. struct inode *inode = filp->f_path.dentry->d_inode;
  2585. struct perf_event *event = filp->private_data;
  2586. int retval;
  2587. mutex_lock(&inode->i_mutex);
  2588. retval = fasync_helper(fd, filp, on, &event->fasync);
  2589. mutex_unlock(&inode->i_mutex);
  2590. if (retval < 0)
  2591. return retval;
  2592. return 0;
  2593. }
  2594. static const struct file_operations perf_fops = {
  2595. .llseek = no_llseek,
  2596. .release = perf_release,
  2597. .read = perf_read,
  2598. .poll = perf_poll,
  2599. .unlocked_ioctl = perf_ioctl,
  2600. .compat_ioctl = perf_ioctl,
  2601. .mmap = perf_mmap,
  2602. .fasync = perf_fasync,
  2603. };
  2604. /*
  2605. * Perf event wakeup
  2606. *
  2607. * If there's data, ensure we set the poll() state and publish everything
  2608. * to user-space before waking everybody up.
  2609. */
  2610. void perf_event_wakeup(struct perf_event *event)
  2611. {
  2612. wake_up_all(&event->waitq);
  2613. if (event->pending_kill) {
  2614. kill_fasync(&event->fasync, SIGIO, event->pending_kill);
  2615. event->pending_kill = 0;
  2616. }
  2617. }
  2618. static void perf_pending_event(struct irq_work *entry)
  2619. {
  2620. struct perf_event *event = container_of(entry,
  2621. struct perf_event, pending);
  2622. if (event->pending_disable) {
  2623. event->pending_disable = 0;
  2624. __perf_event_disable(event);
  2625. }
  2626. if (event->pending_wakeup) {
  2627. event->pending_wakeup = 0;
  2628. perf_event_wakeup(event);
  2629. }
  2630. }
  2631. /*
  2632. * We assume there is only KVM supporting the callbacks.
  2633. * Later on, we might change it to a list if there is
  2634. * another virtualization implementation supporting the callbacks.
  2635. */
  2636. struct perf_guest_info_callbacks *perf_guest_cbs;
  2637. int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  2638. {
  2639. perf_guest_cbs = cbs;
  2640. return 0;
  2641. }
  2642. EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
  2643. int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  2644. {
  2645. perf_guest_cbs = NULL;
  2646. return 0;
  2647. }
  2648. EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
  2649. /*
  2650. * Output
  2651. */
  2652. static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
  2653. unsigned long offset, unsigned long head)
  2654. {
  2655. unsigned long mask;
  2656. if (!buffer->writable)
  2657. return true;
  2658. mask = perf_data_size(buffer) - 1;
  2659. offset = (offset - tail) & mask;
  2660. head = (head - tail) & mask;
  2661. if ((int)(head - offset) < 0)
  2662. return false;
  2663. return true;
  2664. }
  2665. static void perf_output_wakeup(struct perf_output_handle *handle)
  2666. {
  2667. atomic_set(&handle->buffer->poll, POLL_IN);
  2668. if (handle->nmi) {
  2669. handle->event->pending_wakeup = 1;
  2670. irq_work_queue(&handle->event->pending);
  2671. } else
  2672. perf_event_wakeup(handle->event);
  2673. }
  2674. /*
  2675. * We need to ensure a later event_id doesn't publish a head when a former
  2676. * event isn't done writing. However since we need to deal with NMIs we
  2677. * cannot fully serialize things.
  2678. *
  2679. * We only publish the head (and generate a wakeup) when the outer-most
  2680. * event completes.
  2681. */
  2682. static void perf_output_get_handle(struct perf_output_handle *handle)
  2683. {
  2684. struct perf_buffer *buffer = handle->buffer;
  2685. preempt_disable();
  2686. local_inc(&buffer->nest);
  2687. handle->wakeup = local_read(&buffer->wakeup);
  2688. }
  2689. static void perf_output_put_handle(struct perf_output_handle *handle)
  2690. {
  2691. struct perf_buffer *buffer = handle->buffer;
  2692. unsigned long head;
  2693. again:
  2694. head = local_read(&buffer->head);
  2695. /*
  2696. * IRQ/NMI can happen here, which means we can miss a head update.
  2697. */
  2698. if (!local_dec_and_test(&buffer->nest))
  2699. goto out;
  2700. /*
  2701. * Publish the known good head. Rely on the full barrier implied
  2702. * by atomic_dec_and_test() order the buffer->head read and this
  2703. * write.
  2704. */
  2705. buffer->user_page->data_head = head;
  2706. /*
  2707. * Now check if we missed an update, rely on the (compiler)
  2708. * barrier in atomic_dec_and_test() to re-read buffer->head.
  2709. */
  2710. if (unlikely(head != local_read(&buffer->head))) {
  2711. local_inc(&buffer->nest);
  2712. goto again;
  2713. }
  2714. if (handle->wakeup != local_read(&buffer->wakeup))
  2715. perf_output_wakeup(handle);
  2716. out:
  2717. preempt_enable();
  2718. }
  2719. __always_inline void perf_output_copy(struct perf_output_handle *handle,
  2720. const void *buf, unsigned int len)
  2721. {
  2722. do {
  2723. unsigned long size = min_t(unsigned long, handle->size, len);
  2724. memcpy(handle->addr, buf, size);
  2725. len -= size;
  2726. handle->addr += size;
  2727. buf += size;
  2728. handle->size -= size;
  2729. if (!handle->size) {
  2730. struct perf_buffer *buffer = handle->buffer;
  2731. handle->page++;
  2732. handle->page &= buffer->nr_pages - 1;
  2733. handle->addr = buffer->data_pages[handle->page];
  2734. handle->size = PAGE_SIZE << page_order(buffer);
  2735. }
  2736. } while (len);
  2737. }
  2738. int perf_output_begin(struct perf_output_handle *handle,
  2739. struct perf_event *event, unsigned int size,
  2740. int nmi, int sample)
  2741. {
  2742. struct perf_buffer *buffer;
  2743. unsigned long tail, offset, head;
  2744. int have_lost;
  2745. struct {
  2746. struct perf_event_header header;
  2747. u64 id;
  2748. u64 lost;
  2749. } lost_event;
  2750. rcu_read_lock();
  2751. /*
  2752. * For inherited events we send all the output towards the parent.
  2753. */
  2754. if (event->parent)
  2755. event = event->parent;
  2756. buffer = rcu_dereference(event->buffer);
  2757. if (!buffer)
  2758. goto out;
  2759. handle->buffer = buffer;
  2760. handle->event = event;
  2761. handle->nmi = nmi;
  2762. handle->sample = sample;
  2763. if (!buffer->nr_pages)
  2764. goto out;
  2765. have_lost = local_read(&buffer->lost);
  2766. if (have_lost)
  2767. size += sizeof(lost_event);
  2768. perf_output_get_handle(handle);
  2769. do {
  2770. /*
  2771. * Userspace could choose to issue a mb() before updating the
  2772. * tail pointer. So that all reads will be completed before the
  2773. * write is issued.
  2774. */
  2775. tail = ACCESS_ONCE(buffer->user_page->data_tail);
  2776. smp_rmb();
  2777. offset = head = local_read(&buffer->head);
  2778. head += size;
  2779. if (unlikely(!perf_output_space(buffer, tail, offset, head)))
  2780. goto fail;
  2781. } while (local_cmpxchg(&buffer->head, offset, head) != offset);
  2782. if (head - local_read(&buffer->wakeup) > buffer->watermark)
  2783. local_add(buffer->watermark, &buffer->wakeup);
  2784. handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
  2785. handle->page &= buffer->nr_pages - 1;
  2786. handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
  2787. handle->addr = buffer->data_pages[handle->page];
  2788. handle->addr += handle->size;
  2789. handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
  2790. if (have_lost) {
  2791. lost_event.header.type = PERF_RECORD_LOST;
  2792. lost_event.header.misc = 0;
  2793. lost_event.header.size = sizeof(lost_event);
  2794. lost_event.id = event->id;
  2795. lost_event.lost = local_xchg(&buffer->lost, 0);
  2796. perf_output_put(handle, lost_event);
  2797. }
  2798. return 0;
  2799. fail:
  2800. local_inc(&buffer->lost);
  2801. perf_output_put_handle(handle);
  2802. out:
  2803. rcu_read_unlock();
  2804. return -ENOSPC;
  2805. }
  2806. void perf_output_end(struct perf_output_handle *handle)
  2807. {
  2808. struct perf_event *event = handle->event;
  2809. struct perf_buffer *buffer = handle->buffer;
  2810. int wakeup_events = event->attr.wakeup_events;
  2811. if (handle->sample && wakeup_events) {
  2812. int events = local_inc_return(&buffer->events);
  2813. if (events >= wakeup_events) {
  2814. local_sub(wakeup_events, &buffer->events);
  2815. local_inc(&buffer->wakeup);
  2816. }
  2817. }
  2818. perf_output_put_handle(handle);
  2819. rcu_read_unlock();
  2820. }
  2821. static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
  2822. {
  2823. /*
  2824. * only top level events have the pid namespace they were created in
  2825. */
  2826. if (event->parent)
  2827. event = event->parent;
  2828. return task_tgid_nr_ns(p, event->ns);
  2829. }
  2830. static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
  2831. {
  2832. /*
  2833. * only top level events have the pid namespace they were created in
  2834. */
  2835. if (event->parent)
  2836. event = event->parent;
  2837. return task_pid_nr_ns(p, event->ns);
  2838. }
  2839. static void perf_output_read_one(struct perf_output_handle *handle,
  2840. struct perf_event *event,
  2841. u64 enabled, u64 running)
  2842. {
  2843. u64 read_format = event->attr.read_format;
  2844. u64 values[4];
  2845. int n = 0;
  2846. values[n++] = perf_event_count(event);
  2847. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  2848. values[n++] = enabled +
  2849. atomic64_read(&event->child_total_time_enabled);
  2850. }
  2851. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  2852. values[n++] = running +
  2853. atomic64_read(&event->child_total_time_running);
  2854. }
  2855. if (read_format & PERF_FORMAT_ID)
  2856. values[n++] = primary_event_id(event);
  2857. perf_output_copy(handle, values, n * sizeof(u64));
  2858. }
  2859. /*
  2860. * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
  2861. */
  2862. static void perf_output_read_group(struct perf_output_handle *handle,
  2863. struct perf_event *event,
  2864. u64 enabled, u64 running)
  2865. {
  2866. struct perf_event *leader = event->group_leader, *sub;
  2867. u64 read_format = event->attr.read_format;
  2868. u64 values[5];
  2869. int n = 0;
  2870. values[n++] = 1 + leader->nr_siblings;
  2871. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2872. values[n++] = enabled;
  2873. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2874. values[n++] = running;
  2875. if (leader != event)
  2876. leader->pmu->read(leader);
  2877. values[n++] = perf_event_count(leader);
  2878. if (read_format & PERF_FORMAT_ID)
  2879. values[n++] = primary_event_id(leader);
  2880. perf_output_copy(handle, values, n * sizeof(u64));
  2881. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  2882. n = 0;
  2883. if (sub != event)
  2884. sub->pmu->read(sub);
  2885. values[n++] = perf_event_count(sub);
  2886. if (read_format & PERF_FORMAT_ID)
  2887. values[n++] = primary_event_id(sub);
  2888. perf_output_copy(handle, values, n * sizeof(u64));
  2889. }
  2890. }
  2891. #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
  2892. PERF_FORMAT_TOTAL_TIME_RUNNING)
  2893. static void perf_output_read(struct perf_output_handle *handle,
  2894. struct perf_event *event)
  2895. {
  2896. u64 enabled = 0, running = 0, now, ctx_time;
  2897. u64 read_format = event->attr.read_format;
  2898. /*
  2899. * compute total_time_enabled, total_time_running
  2900. * based on snapshot values taken when the event
  2901. * was last scheduled in.
  2902. *
  2903. * we cannot simply called update_context_time()
  2904. * because of locking issue as we are called in
  2905. * NMI context
  2906. */
  2907. if (read_format & PERF_FORMAT_TOTAL_TIMES) {
  2908. now = perf_clock();
  2909. ctx_time = event->shadow_ctx_time + now;
  2910. enabled = ctx_time - event->tstamp_enabled;
  2911. running = ctx_time - event->tstamp_running;
  2912. }
  2913. if (event->attr.read_format & PERF_FORMAT_GROUP)
  2914. perf_output_read_group(handle, event, enabled, running);
  2915. else
  2916. perf_output_read_one(handle, event, enabled, running);
  2917. }
  2918. void perf_output_sample(struct perf_output_handle *handle,
  2919. struct perf_event_header *header,
  2920. struct perf_sample_data *data,
  2921. struct perf_event *event)
  2922. {
  2923. u64 sample_type = data->type;
  2924. perf_output_put(handle, *header);
  2925. if (sample_type & PERF_SAMPLE_IP)
  2926. perf_output_put(handle, data->ip);
  2927. if (sample_type & PERF_SAMPLE_TID)
  2928. perf_output_put(handle, data->tid_entry);
  2929. if (sample_type & PERF_SAMPLE_TIME)
  2930. perf_output_put(handle, data->time);
  2931. if (sample_type & PERF_SAMPLE_ADDR)
  2932. perf_output_put(handle, data->addr);
  2933. if (sample_type & PERF_SAMPLE_ID)
  2934. perf_output_put(handle, data->id);
  2935. if (sample_type & PERF_SAMPLE_STREAM_ID)
  2936. perf_output_put(handle, data->stream_id);
  2937. if (sample_type & PERF_SAMPLE_CPU)
  2938. perf_output_put(handle, data->cpu_entry);
  2939. if (sample_type & PERF_SAMPLE_PERIOD)
  2940. perf_output_put(handle, data->period);
  2941. if (sample_type & PERF_SAMPLE_READ)
  2942. perf_output_read(handle, event);
  2943. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  2944. if (data->callchain) {
  2945. int size = 1;
  2946. if (data->callchain)
  2947. size += data->callchain->nr;
  2948. size *= sizeof(u64);
  2949. perf_output_copy(handle, data->callchain, size);
  2950. } else {
  2951. u64 nr = 0;
  2952. perf_output_put(handle, nr);
  2953. }
  2954. }
  2955. if (sample_type & PERF_SAMPLE_RAW) {
  2956. if (data->raw) {
  2957. perf_output_put(handle, data->raw->size);
  2958. perf_output_copy(handle, data->raw->data,
  2959. data->raw->size);
  2960. } else {
  2961. struct {
  2962. u32 size;
  2963. u32 data;
  2964. } raw = {
  2965. .size = sizeof(u32),
  2966. .data = 0,
  2967. };
  2968. perf_output_put(handle, raw);
  2969. }
  2970. }
  2971. }
  2972. void perf_prepare_sample(struct perf_event_header *header,
  2973. struct perf_sample_data *data,
  2974. struct perf_event *event,
  2975. struct pt_regs *regs)
  2976. {
  2977. u64 sample_type = event->attr.sample_type;
  2978. data->type = sample_type;
  2979. header->type = PERF_RECORD_SAMPLE;
  2980. header->size = sizeof(*header) + event->header_size;
  2981. header->misc = 0;
  2982. header->misc |= perf_misc_flags(regs);
  2983. if (sample_type & PERF_SAMPLE_IP)
  2984. data->ip = perf_instruction_pointer(regs);
  2985. if (sample_type & PERF_SAMPLE_TID) {
  2986. /* namespace issues */
  2987. data->tid_entry.pid = perf_event_pid(event, current);
  2988. data->tid_entry.tid = perf_event_tid(event, current);
  2989. }
  2990. if (sample_type & PERF_SAMPLE_TIME)
  2991. data->time = perf_clock();
  2992. if (sample_type & PERF_SAMPLE_ID)
  2993. data->id = primary_event_id(event);
  2994. if (sample_type & PERF_SAMPLE_STREAM_ID)
  2995. data->stream_id = event->id;
  2996. if (sample_type & PERF_SAMPLE_CPU) {
  2997. data->cpu_entry.cpu = raw_smp_processor_id();
  2998. data->cpu_entry.reserved = 0;
  2999. }
  3000. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  3001. int size = 1;
  3002. data->callchain = perf_callchain(regs);
  3003. if (data->callchain)
  3004. size += data->callchain->nr;
  3005. header->size += size * sizeof(u64);
  3006. }
  3007. if (sample_type & PERF_SAMPLE_RAW) {
  3008. int size = sizeof(u32);
  3009. if (data->raw)
  3010. size += data->raw->size;
  3011. else
  3012. size += sizeof(u32);
  3013. WARN_ON_ONCE(size & (sizeof(u64)-1));
  3014. header->size += size;
  3015. }
  3016. }
  3017. static void perf_event_output(struct perf_event *event, int nmi,
  3018. struct perf_sample_data *data,
  3019. struct pt_regs *regs)
  3020. {
  3021. struct perf_output_handle handle;
  3022. struct perf_event_header header;
  3023. /* protect the callchain buffers */
  3024. rcu_read_lock();
  3025. perf_prepare_sample(&header, data, event, regs);
  3026. if (perf_output_begin(&handle, event, header.size, nmi, 1))
  3027. goto exit;
  3028. perf_output_sample(&handle, &header, data, event);
  3029. perf_output_end(&handle);
  3030. exit:
  3031. rcu_read_unlock();
  3032. }
  3033. /*
  3034. * read event_id
  3035. */
  3036. struct perf_read_event {
  3037. struct perf_event_header header;
  3038. u32 pid;
  3039. u32 tid;
  3040. };
  3041. static void
  3042. perf_event_read_event(struct perf_event *event,
  3043. struct task_struct *task)
  3044. {
  3045. struct perf_output_handle handle;
  3046. struct perf_read_event read_event = {
  3047. .header = {
  3048. .type = PERF_RECORD_READ,
  3049. .misc = 0,
  3050. .size = sizeof(read_event) + event->read_size,
  3051. },
  3052. .pid = perf_event_pid(event, task),
  3053. .tid = perf_event_tid(event, task),
  3054. };
  3055. int ret;
  3056. ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
  3057. if (ret)
  3058. return;
  3059. perf_output_put(&handle, read_event);
  3060. perf_output_read(&handle, event);
  3061. perf_output_end(&handle);
  3062. }
  3063. /*
  3064. * task tracking -- fork/exit
  3065. *
  3066. * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
  3067. */
  3068. struct perf_task_event {
  3069. struct task_struct *task;
  3070. struct perf_event_context *task_ctx;
  3071. struct {
  3072. struct perf_event_header header;
  3073. u32 pid;
  3074. u32 ppid;
  3075. u32 tid;
  3076. u32 ptid;
  3077. u64 time;
  3078. } event_id;
  3079. };
  3080. static void perf_event_task_output(struct perf_event *event,
  3081. struct perf_task_event *task_event)
  3082. {
  3083. struct perf_output_handle handle;
  3084. struct task_struct *task = task_event->task;
  3085. int size, ret;
  3086. size = task_event->event_id.header.size;
  3087. ret = perf_output_begin(&handle, event, size, 0, 0);
  3088. if (ret)
  3089. return;
  3090. task_event->event_id.pid = perf_event_pid(event, task);
  3091. task_event->event_id.ppid = perf_event_pid(event, current);
  3092. task_event->event_id.tid = perf_event_tid(event, task);
  3093. task_event->event_id.ptid = perf_event_tid(event, current);
  3094. perf_output_put(&handle, task_event->event_id);
  3095. perf_output_end(&handle);
  3096. }
  3097. static int perf_event_task_match(struct perf_event *event)
  3098. {
  3099. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3100. return 0;
  3101. if (event->cpu != -1 && event->cpu != smp_processor_id())
  3102. return 0;
  3103. if (event->attr.comm || event->attr.mmap ||
  3104. event->attr.mmap_data || event->attr.task)
  3105. return 1;
  3106. return 0;
  3107. }
  3108. static void perf_event_task_ctx(struct perf_event_context *ctx,
  3109. struct perf_task_event *task_event)
  3110. {
  3111. struct perf_event *event;
  3112. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3113. if (perf_event_task_match(event))
  3114. perf_event_task_output(event, task_event);
  3115. }
  3116. }
  3117. static void perf_event_task_event(struct perf_task_event *task_event)
  3118. {
  3119. struct perf_cpu_context *cpuctx;
  3120. struct perf_event_context *ctx;
  3121. struct pmu *pmu;
  3122. int ctxn;
  3123. rcu_read_lock();
  3124. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3125. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3126. perf_event_task_ctx(&cpuctx->ctx, task_event);
  3127. ctx = task_event->task_ctx;
  3128. if (!ctx) {
  3129. ctxn = pmu->task_ctx_nr;
  3130. if (ctxn < 0)
  3131. goto next;
  3132. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3133. }
  3134. if (ctx)
  3135. perf_event_task_ctx(ctx, task_event);
  3136. next:
  3137. put_cpu_ptr(pmu->pmu_cpu_context);
  3138. }
  3139. rcu_read_unlock();
  3140. }
  3141. static void perf_event_task(struct task_struct *task,
  3142. struct perf_event_context *task_ctx,
  3143. int new)
  3144. {
  3145. struct perf_task_event task_event;
  3146. if (!atomic_read(&nr_comm_events) &&
  3147. !atomic_read(&nr_mmap_events) &&
  3148. !atomic_read(&nr_task_events))
  3149. return;
  3150. task_event = (struct perf_task_event){
  3151. .task = task,
  3152. .task_ctx = task_ctx,
  3153. .event_id = {
  3154. .header = {
  3155. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  3156. .misc = 0,
  3157. .size = sizeof(task_event.event_id),
  3158. },
  3159. /* .pid */
  3160. /* .ppid */
  3161. /* .tid */
  3162. /* .ptid */
  3163. .time = perf_clock(),
  3164. },
  3165. };
  3166. perf_event_task_event(&task_event);
  3167. }
  3168. void perf_event_fork(struct task_struct *task)
  3169. {
  3170. perf_event_task(task, NULL, 1);
  3171. }
  3172. /*
  3173. * comm tracking
  3174. */
  3175. struct perf_comm_event {
  3176. struct task_struct *task;
  3177. char *comm;
  3178. int comm_size;
  3179. struct {
  3180. struct perf_event_header header;
  3181. u32 pid;
  3182. u32 tid;
  3183. } event_id;
  3184. };
  3185. static void perf_event_comm_output(struct perf_event *event,
  3186. struct perf_comm_event *comm_event)
  3187. {
  3188. struct perf_output_handle handle;
  3189. int size = comm_event->event_id.header.size;
  3190. int ret = perf_output_begin(&handle, event, size, 0, 0);
  3191. if (ret)
  3192. return;
  3193. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  3194. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  3195. perf_output_put(&handle, comm_event->event_id);
  3196. perf_output_copy(&handle, comm_event->comm,
  3197. comm_event->comm_size);
  3198. perf_output_end(&handle);
  3199. }
  3200. static int perf_event_comm_match(struct perf_event *event)
  3201. {
  3202. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3203. return 0;
  3204. if (event->cpu != -1 && event->cpu != smp_processor_id())
  3205. return 0;
  3206. if (event->attr.comm)
  3207. return 1;
  3208. return 0;
  3209. }
  3210. static void perf_event_comm_ctx(struct perf_event_context *ctx,
  3211. struct perf_comm_event *comm_event)
  3212. {
  3213. struct perf_event *event;
  3214. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3215. if (perf_event_comm_match(event))
  3216. perf_event_comm_output(event, comm_event);
  3217. }
  3218. }
  3219. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  3220. {
  3221. struct perf_cpu_context *cpuctx;
  3222. struct perf_event_context *ctx;
  3223. char comm[TASK_COMM_LEN];
  3224. unsigned int size;
  3225. struct pmu *pmu;
  3226. int ctxn;
  3227. memset(comm, 0, sizeof(comm));
  3228. strlcpy(comm, comm_event->task->comm, sizeof(comm));
  3229. size = ALIGN(strlen(comm)+1, sizeof(u64));
  3230. comm_event->comm = comm;
  3231. comm_event->comm_size = size;
  3232. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  3233. rcu_read_lock();
  3234. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3235. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3236. perf_event_comm_ctx(&cpuctx->ctx, comm_event);
  3237. ctxn = pmu->task_ctx_nr;
  3238. if (ctxn < 0)
  3239. goto next;
  3240. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3241. if (ctx)
  3242. perf_event_comm_ctx(ctx, comm_event);
  3243. next:
  3244. put_cpu_ptr(pmu->pmu_cpu_context);
  3245. }
  3246. rcu_read_unlock();
  3247. }
  3248. void perf_event_comm(struct task_struct *task)
  3249. {
  3250. struct perf_comm_event comm_event;
  3251. struct perf_event_context *ctx;
  3252. int ctxn;
  3253. for_each_task_context_nr(ctxn) {
  3254. ctx = task->perf_event_ctxp[ctxn];
  3255. if (!ctx)
  3256. continue;
  3257. perf_event_enable_on_exec(ctx);
  3258. }
  3259. if (!atomic_read(&nr_comm_events))
  3260. return;
  3261. comm_event = (struct perf_comm_event){
  3262. .task = task,
  3263. /* .comm */
  3264. /* .comm_size */
  3265. .event_id = {
  3266. .header = {
  3267. .type = PERF_RECORD_COMM,
  3268. .misc = 0,
  3269. /* .size */
  3270. },
  3271. /* .pid */
  3272. /* .tid */
  3273. },
  3274. };
  3275. perf_event_comm_event(&comm_event);
  3276. }
  3277. /*
  3278. * mmap tracking
  3279. */
  3280. struct perf_mmap_event {
  3281. struct vm_area_struct *vma;
  3282. const char *file_name;
  3283. int file_size;
  3284. struct {
  3285. struct perf_event_header header;
  3286. u32 pid;
  3287. u32 tid;
  3288. u64 start;
  3289. u64 len;
  3290. u64 pgoff;
  3291. } event_id;
  3292. };
  3293. static void perf_event_mmap_output(struct perf_event *event,
  3294. struct perf_mmap_event *mmap_event)
  3295. {
  3296. struct perf_output_handle handle;
  3297. int size = mmap_event->event_id.header.size;
  3298. int ret = perf_output_begin(&handle, event, size, 0, 0);
  3299. if (ret)
  3300. return;
  3301. mmap_event->event_id.pid = perf_event_pid(event, current);
  3302. mmap_event->event_id.tid = perf_event_tid(event, current);
  3303. perf_output_put(&handle, mmap_event->event_id);
  3304. perf_output_copy(&handle, mmap_event->file_name,
  3305. mmap_event->file_size);
  3306. perf_output_end(&handle);
  3307. }
  3308. static int perf_event_mmap_match(struct perf_event *event,
  3309. struct perf_mmap_event *mmap_event,
  3310. int executable)
  3311. {
  3312. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3313. return 0;
  3314. if (event->cpu != -1 && event->cpu != smp_processor_id())
  3315. return 0;
  3316. if ((!executable && event->attr.mmap_data) ||
  3317. (executable && event->attr.mmap))
  3318. return 1;
  3319. return 0;
  3320. }
  3321. static void perf_event_mmap_ctx(struct perf_event_context *ctx,
  3322. struct perf_mmap_event *mmap_event,
  3323. int executable)
  3324. {
  3325. struct perf_event *event;
  3326. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3327. if (perf_event_mmap_match(event, mmap_event, executable))
  3328. perf_event_mmap_output(event, mmap_event);
  3329. }
  3330. }
  3331. static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
  3332. {
  3333. struct perf_cpu_context *cpuctx;
  3334. struct perf_event_context *ctx;
  3335. struct vm_area_struct *vma = mmap_event->vma;
  3336. struct file *file = vma->vm_file;
  3337. unsigned int size;
  3338. char tmp[16];
  3339. char *buf = NULL;
  3340. const char *name;
  3341. struct pmu *pmu;
  3342. int ctxn;
  3343. memset(tmp, 0, sizeof(tmp));
  3344. if (file) {
  3345. /*
  3346. * d_path works from the end of the buffer backwards, so we
  3347. * need to add enough zero bytes after the string to handle
  3348. * the 64bit alignment we do later.
  3349. */
  3350. buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
  3351. if (!buf) {
  3352. name = strncpy(tmp, "//enomem", sizeof(tmp));
  3353. goto got_name;
  3354. }
  3355. name = d_path(&file->f_path, buf, PATH_MAX);
  3356. if (IS_ERR(name)) {
  3357. name = strncpy(tmp, "//toolong", sizeof(tmp));
  3358. goto got_name;
  3359. }
  3360. } else {
  3361. if (arch_vma_name(mmap_event->vma)) {
  3362. name = strncpy(tmp, arch_vma_name(mmap_event->vma),
  3363. sizeof(tmp));
  3364. goto got_name;
  3365. }
  3366. if (!vma->vm_mm) {
  3367. name = strncpy(tmp, "[vdso]", sizeof(tmp));
  3368. goto got_name;
  3369. } else if (vma->vm_start <= vma->vm_mm->start_brk &&
  3370. vma->vm_end >= vma->vm_mm->brk) {
  3371. name = strncpy(tmp, "[heap]", sizeof(tmp));
  3372. goto got_name;
  3373. } else if (vma->vm_start <= vma->vm_mm->start_stack &&
  3374. vma->vm_end >= vma->vm_mm->start_stack) {
  3375. name = strncpy(tmp, "[stack]", sizeof(tmp));
  3376. goto got_name;
  3377. }
  3378. name = strncpy(tmp, "//anon", sizeof(tmp));
  3379. goto got_name;
  3380. }
  3381. got_name:
  3382. size = ALIGN(strlen(name)+1, sizeof(u64));
  3383. mmap_event->file_name = name;
  3384. mmap_event->file_size = size;
  3385. mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
  3386. rcu_read_lock();
  3387. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3388. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3389. perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
  3390. vma->vm_flags & VM_EXEC);
  3391. ctxn = pmu->task_ctx_nr;
  3392. if (ctxn < 0)
  3393. goto next;
  3394. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3395. if (ctx) {
  3396. perf_event_mmap_ctx(ctx, mmap_event,
  3397. vma->vm_flags & VM_EXEC);
  3398. }
  3399. next:
  3400. put_cpu_ptr(pmu->pmu_cpu_context);
  3401. }
  3402. rcu_read_unlock();
  3403. kfree(buf);
  3404. }
  3405. void perf_event_mmap(struct vm_area_struct *vma)
  3406. {
  3407. struct perf_mmap_event mmap_event;
  3408. if (!atomic_read(&nr_mmap_events))
  3409. return;
  3410. mmap_event = (struct perf_mmap_event){
  3411. .vma = vma,
  3412. /* .file_name */
  3413. /* .file_size */
  3414. .event_id = {
  3415. .header = {
  3416. .type = PERF_RECORD_MMAP,
  3417. .misc = PERF_RECORD_MISC_USER,
  3418. /* .size */
  3419. },
  3420. /* .pid */
  3421. /* .tid */
  3422. .start = vma->vm_start,
  3423. .len = vma->vm_end - vma->vm_start,
  3424. .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
  3425. },
  3426. };
  3427. perf_event_mmap_event(&mmap_event);
  3428. }
  3429. /*
  3430. * IRQ throttle logging
  3431. */
  3432. static void perf_log_throttle(struct perf_event *event, int enable)
  3433. {
  3434. struct perf_output_handle handle;
  3435. int ret;
  3436. struct {
  3437. struct perf_event_header header;
  3438. u64 time;
  3439. u64 id;
  3440. u64 stream_id;
  3441. } throttle_event = {
  3442. .header = {
  3443. .type = PERF_RECORD_THROTTLE,
  3444. .misc = 0,
  3445. .size = sizeof(throttle_event),
  3446. },
  3447. .time = perf_clock(),
  3448. .id = primary_event_id(event),
  3449. .stream_id = event->id,
  3450. };
  3451. if (enable)
  3452. throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
  3453. ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
  3454. if (ret)
  3455. return;
  3456. perf_output_put(&handle, throttle_event);
  3457. perf_output_end(&handle);
  3458. }
  3459. /*
  3460. * Generic event overflow handling, sampling.
  3461. */
  3462. static int __perf_event_overflow(struct perf_event *event, int nmi,
  3463. int throttle, struct perf_sample_data *data,
  3464. struct pt_regs *regs)
  3465. {
  3466. int events = atomic_read(&event->event_limit);
  3467. struct hw_perf_event *hwc = &event->hw;
  3468. int ret = 0;
  3469. /*
  3470. * Non-sampling counters might still use the PMI to fold short
  3471. * hardware counters, ignore those.
  3472. */
  3473. if (unlikely(!is_sampling_event(event)))
  3474. return 0;
  3475. if (!throttle) {
  3476. hwc->interrupts++;
  3477. } else {
  3478. if (hwc->interrupts != MAX_INTERRUPTS) {
  3479. hwc->interrupts++;
  3480. if (HZ * hwc->interrupts >
  3481. (u64)sysctl_perf_event_sample_rate) {
  3482. hwc->interrupts = MAX_INTERRUPTS;
  3483. perf_log_throttle(event, 0);
  3484. ret = 1;
  3485. }
  3486. } else {
  3487. /*
  3488. * Keep re-disabling events even though on the previous
  3489. * pass we disabled it - just in case we raced with a
  3490. * sched-in and the event got enabled again:
  3491. */
  3492. ret = 1;
  3493. }
  3494. }
  3495. if (event->attr.freq) {
  3496. u64 now = perf_clock();
  3497. s64 delta = now - hwc->freq_time_stamp;
  3498. hwc->freq_time_stamp = now;
  3499. if (delta > 0 && delta < 2*TICK_NSEC)
  3500. perf_adjust_period(event, delta, hwc->last_period);
  3501. }
  3502. /*
  3503. * XXX event_limit might not quite work as expected on inherited
  3504. * events
  3505. */
  3506. event->pending_kill = POLL_IN;
  3507. if (events && atomic_dec_and_test(&event->event_limit)) {
  3508. ret = 1;
  3509. event->pending_kill = POLL_HUP;
  3510. if (nmi) {
  3511. event->pending_disable = 1;
  3512. irq_work_queue(&event->pending);
  3513. } else
  3514. perf_event_disable(event);
  3515. }
  3516. if (event->overflow_handler)
  3517. event->overflow_handler(event, nmi, data, regs);
  3518. else
  3519. perf_event_output(event, nmi, data, regs);
  3520. return ret;
  3521. }
  3522. int perf_event_overflow(struct perf_event *event, int nmi,
  3523. struct perf_sample_data *data,
  3524. struct pt_regs *regs)
  3525. {
  3526. return __perf_event_overflow(event, nmi, 1, data, regs);
  3527. }
  3528. /*
  3529. * Generic software event infrastructure
  3530. */
  3531. struct swevent_htable {
  3532. struct swevent_hlist *swevent_hlist;
  3533. struct mutex hlist_mutex;
  3534. int hlist_refcount;
  3535. /* Recursion avoidance in each contexts */
  3536. int recursion[PERF_NR_CONTEXTS];
  3537. };
  3538. static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
  3539. /*
  3540. * We directly increment event->count and keep a second value in
  3541. * event->hw.period_left to count intervals. This period event
  3542. * is kept in the range [-sample_period, 0] so that we can use the
  3543. * sign as trigger.
  3544. */
  3545. static u64 perf_swevent_set_period(struct perf_event *event)
  3546. {
  3547. struct hw_perf_event *hwc = &event->hw;
  3548. u64 period = hwc->last_period;
  3549. u64 nr, offset;
  3550. s64 old, val;
  3551. hwc->last_period = hwc->sample_period;
  3552. again:
  3553. old = val = local64_read(&hwc->period_left);
  3554. if (val < 0)
  3555. return 0;
  3556. nr = div64_u64(period + val, period);
  3557. offset = nr * period;
  3558. val -= offset;
  3559. if (local64_cmpxchg(&hwc->period_left, old, val) != old)
  3560. goto again;
  3561. return nr;
  3562. }
  3563. static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
  3564. int nmi, struct perf_sample_data *data,
  3565. struct pt_regs *regs)
  3566. {
  3567. struct hw_perf_event *hwc = &event->hw;
  3568. int throttle = 0;
  3569. data->period = event->hw.last_period;
  3570. if (!overflow)
  3571. overflow = perf_swevent_set_period(event);
  3572. if (hwc->interrupts == MAX_INTERRUPTS)
  3573. return;
  3574. for (; overflow; overflow--) {
  3575. if (__perf_event_overflow(event, nmi, throttle,
  3576. data, regs)) {
  3577. /*
  3578. * We inhibit the overflow from happening when
  3579. * hwc->interrupts == MAX_INTERRUPTS.
  3580. */
  3581. break;
  3582. }
  3583. throttle = 1;
  3584. }
  3585. }
  3586. static void perf_swevent_event(struct perf_event *event, u64 nr,
  3587. int nmi, struct perf_sample_data *data,
  3588. struct pt_regs *regs)
  3589. {
  3590. struct hw_perf_event *hwc = &event->hw;
  3591. local64_add(nr, &event->count);
  3592. if (!regs)
  3593. return;
  3594. if (!is_sampling_event(event))
  3595. return;
  3596. if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  3597. return perf_swevent_overflow(event, 1, nmi, data, regs);
  3598. if (local64_add_negative(nr, &hwc->period_left))
  3599. return;
  3600. perf_swevent_overflow(event, 0, nmi, data, regs);
  3601. }
  3602. static int perf_exclude_event(struct perf_event *event,
  3603. struct pt_regs *regs)
  3604. {
  3605. if (event->hw.state & PERF_HES_STOPPED)
  3606. return 0;
  3607. if (regs) {
  3608. if (event->attr.exclude_user && user_mode(regs))
  3609. return 1;
  3610. if (event->attr.exclude_kernel && !user_mode(regs))
  3611. return 1;
  3612. }
  3613. return 0;
  3614. }
  3615. static int perf_swevent_match(struct perf_event *event,
  3616. enum perf_type_id type,
  3617. u32 event_id,
  3618. struct perf_sample_data *data,
  3619. struct pt_regs *regs)
  3620. {
  3621. if (event->attr.type != type)
  3622. return 0;
  3623. if (event->attr.config != event_id)
  3624. return 0;
  3625. if (perf_exclude_event(event, regs))
  3626. return 0;
  3627. return 1;
  3628. }
  3629. static inline u64 swevent_hash(u64 type, u32 event_id)
  3630. {
  3631. u64 val = event_id | (type << 32);
  3632. return hash_64(val, SWEVENT_HLIST_BITS);
  3633. }
  3634. static inline struct hlist_head *
  3635. __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
  3636. {
  3637. u64 hash = swevent_hash(type, event_id);
  3638. return &hlist->heads[hash];
  3639. }
  3640. /* For the read side: events when they trigger */
  3641. static inline struct hlist_head *
  3642. find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
  3643. {
  3644. struct swevent_hlist *hlist;
  3645. hlist = rcu_dereference(swhash->swevent_hlist);
  3646. if (!hlist)
  3647. return NULL;
  3648. return __find_swevent_head(hlist, type, event_id);
  3649. }
  3650. /* For the event head insertion and removal in the hlist */
  3651. static inline struct hlist_head *
  3652. find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
  3653. {
  3654. struct swevent_hlist *hlist;
  3655. u32 event_id = event->attr.config;
  3656. u64 type = event->attr.type;
  3657. /*
  3658. * Event scheduling is always serialized against hlist allocation
  3659. * and release. Which makes the protected version suitable here.
  3660. * The context lock guarantees that.
  3661. */
  3662. hlist = rcu_dereference_protected(swhash->swevent_hlist,
  3663. lockdep_is_held(&event->ctx->lock));
  3664. if (!hlist)
  3665. return NULL;
  3666. return __find_swevent_head(hlist, type, event_id);
  3667. }
  3668. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  3669. u64 nr, int nmi,
  3670. struct perf_sample_data *data,
  3671. struct pt_regs *regs)
  3672. {
  3673. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3674. struct perf_event *event;
  3675. struct hlist_node *node;
  3676. struct hlist_head *head;
  3677. rcu_read_lock();
  3678. head = find_swevent_head_rcu(swhash, type, event_id);
  3679. if (!head)
  3680. goto end;
  3681. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  3682. if (perf_swevent_match(event, type, event_id, data, regs))
  3683. perf_swevent_event(event, nr, nmi, data, regs);
  3684. }
  3685. end:
  3686. rcu_read_unlock();
  3687. }
  3688. int perf_swevent_get_recursion_context(void)
  3689. {
  3690. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3691. return get_recursion_context(swhash->recursion);
  3692. }
  3693. EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
  3694. void inline perf_swevent_put_recursion_context(int rctx)
  3695. {
  3696. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3697. put_recursion_context(swhash->recursion, rctx);
  3698. }
  3699. void __perf_sw_event(u32 event_id, u64 nr, int nmi,
  3700. struct pt_regs *regs, u64 addr)
  3701. {
  3702. struct perf_sample_data data;
  3703. int rctx;
  3704. preempt_disable_notrace();
  3705. rctx = perf_swevent_get_recursion_context();
  3706. if (rctx < 0)
  3707. return;
  3708. perf_sample_data_init(&data, addr);
  3709. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
  3710. perf_swevent_put_recursion_context(rctx);
  3711. preempt_enable_notrace();
  3712. }
  3713. static void perf_swevent_read(struct perf_event *event)
  3714. {
  3715. }
  3716. static int perf_swevent_add(struct perf_event *event, int flags)
  3717. {
  3718. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3719. struct hw_perf_event *hwc = &event->hw;
  3720. struct hlist_head *head;
  3721. if (is_sampling_event(event)) {
  3722. hwc->last_period = hwc->sample_period;
  3723. perf_swevent_set_period(event);
  3724. }
  3725. hwc->state = !(flags & PERF_EF_START);
  3726. head = find_swevent_head(swhash, event);
  3727. if (WARN_ON_ONCE(!head))
  3728. return -EINVAL;
  3729. hlist_add_head_rcu(&event->hlist_entry, head);
  3730. return 0;
  3731. }
  3732. static void perf_swevent_del(struct perf_event *event, int flags)
  3733. {
  3734. hlist_del_rcu(&event->hlist_entry);
  3735. }
  3736. static void perf_swevent_start(struct perf_event *event, int flags)
  3737. {
  3738. event->hw.state = 0;
  3739. }
  3740. static void perf_swevent_stop(struct perf_event *event, int flags)
  3741. {
  3742. event->hw.state = PERF_HES_STOPPED;
  3743. }
  3744. /* Deref the hlist from the update side */
  3745. static inline struct swevent_hlist *
  3746. swevent_hlist_deref(struct swevent_htable *swhash)
  3747. {
  3748. return rcu_dereference_protected(swhash->swevent_hlist,
  3749. lockdep_is_held(&swhash->hlist_mutex));
  3750. }
  3751. static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
  3752. {
  3753. struct swevent_hlist *hlist;
  3754. hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
  3755. kfree(hlist);
  3756. }
  3757. static void swevent_hlist_release(struct swevent_htable *swhash)
  3758. {
  3759. struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
  3760. if (!hlist)
  3761. return;
  3762. rcu_assign_pointer(swhash->swevent_hlist, NULL);
  3763. call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
  3764. }
  3765. static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
  3766. {
  3767. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  3768. mutex_lock(&swhash->hlist_mutex);
  3769. if (!--swhash->hlist_refcount)
  3770. swevent_hlist_release(swhash);
  3771. mutex_unlock(&swhash->hlist_mutex);
  3772. }
  3773. static void swevent_hlist_put(struct perf_event *event)
  3774. {
  3775. int cpu;
  3776. if (event->cpu != -1) {
  3777. swevent_hlist_put_cpu(event, event->cpu);
  3778. return;
  3779. }
  3780. for_each_possible_cpu(cpu)
  3781. swevent_hlist_put_cpu(event, cpu);
  3782. }
  3783. static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
  3784. {
  3785. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  3786. int err = 0;
  3787. mutex_lock(&swhash->hlist_mutex);
  3788. if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
  3789. struct swevent_hlist *hlist;
  3790. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  3791. if (!hlist) {
  3792. err = -ENOMEM;
  3793. goto exit;
  3794. }
  3795. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  3796. }
  3797. swhash->hlist_refcount++;
  3798. exit:
  3799. mutex_unlock(&swhash->hlist_mutex);
  3800. return err;
  3801. }
  3802. static int swevent_hlist_get(struct perf_event *event)
  3803. {
  3804. int err;
  3805. int cpu, failed_cpu;
  3806. if (event->cpu != -1)
  3807. return swevent_hlist_get_cpu(event, event->cpu);
  3808. get_online_cpus();
  3809. for_each_possible_cpu(cpu) {
  3810. err = swevent_hlist_get_cpu(event, cpu);
  3811. if (err) {
  3812. failed_cpu = cpu;
  3813. goto fail;
  3814. }
  3815. }
  3816. put_online_cpus();
  3817. return 0;
  3818. fail:
  3819. for_each_possible_cpu(cpu) {
  3820. if (cpu == failed_cpu)
  3821. break;
  3822. swevent_hlist_put_cpu(event, cpu);
  3823. }
  3824. put_online_cpus();
  3825. return err;
  3826. }
  3827. atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
  3828. static void sw_perf_event_destroy(struct perf_event *event)
  3829. {
  3830. u64 event_id = event->attr.config;
  3831. WARN_ON(event->parent);
  3832. jump_label_dec(&perf_swevent_enabled[event_id]);
  3833. swevent_hlist_put(event);
  3834. }
  3835. static int perf_swevent_init(struct perf_event *event)
  3836. {
  3837. int event_id = event->attr.config;
  3838. if (event->attr.type != PERF_TYPE_SOFTWARE)
  3839. return -ENOENT;
  3840. switch (event_id) {
  3841. case PERF_COUNT_SW_CPU_CLOCK:
  3842. case PERF_COUNT_SW_TASK_CLOCK:
  3843. return -ENOENT;
  3844. default:
  3845. break;
  3846. }
  3847. if (event_id > PERF_COUNT_SW_MAX)
  3848. return -ENOENT;
  3849. if (!event->parent) {
  3850. int err;
  3851. err = swevent_hlist_get(event);
  3852. if (err)
  3853. return err;
  3854. jump_label_inc(&perf_swevent_enabled[event_id]);
  3855. event->destroy = sw_perf_event_destroy;
  3856. }
  3857. return 0;
  3858. }
  3859. static struct pmu perf_swevent = {
  3860. .task_ctx_nr = perf_sw_context,
  3861. .event_init = perf_swevent_init,
  3862. .add = perf_swevent_add,
  3863. .del = perf_swevent_del,
  3864. .start = perf_swevent_start,
  3865. .stop = perf_swevent_stop,
  3866. .read = perf_swevent_read,
  3867. };
  3868. #ifdef CONFIG_EVENT_TRACING
  3869. static int perf_tp_filter_match(struct perf_event *event,
  3870. struct perf_sample_data *data)
  3871. {
  3872. void *record = data->raw->data;
  3873. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  3874. return 1;
  3875. return 0;
  3876. }
  3877. static int perf_tp_event_match(struct perf_event *event,
  3878. struct perf_sample_data *data,
  3879. struct pt_regs *regs)
  3880. {
  3881. /*
  3882. * All tracepoints are from kernel-space.
  3883. */
  3884. if (event->attr.exclude_kernel)
  3885. return 0;
  3886. if (!perf_tp_filter_match(event, data))
  3887. return 0;
  3888. return 1;
  3889. }
  3890. void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
  3891. struct pt_regs *regs, struct hlist_head *head, int rctx)
  3892. {
  3893. struct perf_sample_data data;
  3894. struct perf_event *event;
  3895. struct hlist_node *node;
  3896. struct perf_raw_record raw = {
  3897. .size = entry_size,
  3898. .data = record,
  3899. };
  3900. perf_sample_data_init(&data, addr);
  3901. data.raw = &raw;
  3902. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  3903. if (perf_tp_event_match(event, &data, regs))
  3904. perf_swevent_event(event, count, 1, &data, regs);
  3905. }
  3906. perf_swevent_put_recursion_context(rctx);
  3907. }
  3908. EXPORT_SYMBOL_GPL(perf_tp_event);
  3909. static void tp_perf_event_destroy(struct perf_event *event)
  3910. {
  3911. perf_trace_destroy(event);
  3912. }
  3913. static int perf_tp_event_init(struct perf_event *event)
  3914. {
  3915. int err;
  3916. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  3917. return -ENOENT;
  3918. err = perf_trace_init(event);
  3919. if (err)
  3920. return err;
  3921. event->destroy = tp_perf_event_destroy;
  3922. return 0;
  3923. }
  3924. static struct pmu perf_tracepoint = {
  3925. .task_ctx_nr = perf_sw_context,
  3926. .event_init = perf_tp_event_init,
  3927. .add = perf_trace_add,
  3928. .del = perf_trace_del,
  3929. .start = perf_swevent_start,
  3930. .stop = perf_swevent_stop,
  3931. .read = perf_swevent_read,
  3932. };
  3933. static inline void perf_tp_register(void)
  3934. {
  3935. perf_pmu_register(&perf_tracepoint);
  3936. }
  3937. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  3938. {
  3939. char *filter_str;
  3940. int ret;
  3941. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  3942. return -EINVAL;
  3943. filter_str = strndup_user(arg, PAGE_SIZE);
  3944. if (IS_ERR(filter_str))
  3945. return PTR_ERR(filter_str);
  3946. ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  3947. kfree(filter_str);
  3948. return ret;
  3949. }
  3950. static void perf_event_free_filter(struct perf_event *event)
  3951. {
  3952. ftrace_profile_free_filter(event);
  3953. }
  3954. #else
  3955. static inline void perf_tp_register(void)
  3956. {
  3957. }
  3958. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  3959. {
  3960. return -ENOENT;
  3961. }
  3962. static void perf_event_free_filter(struct perf_event *event)
  3963. {
  3964. }
  3965. #endif /* CONFIG_EVENT_TRACING */
  3966. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  3967. void perf_bp_event(struct perf_event *bp, void *data)
  3968. {
  3969. struct perf_sample_data sample;
  3970. struct pt_regs *regs = data;
  3971. perf_sample_data_init(&sample, bp->attr.bp_addr);
  3972. if (!bp->hw.state && !perf_exclude_event(bp, regs))
  3973. perf_swevent_event(bp, 1, 1, &sample, regs);
  3974. }
  3975. #endif
  3976. /*
  3977. * hrtimer based swevent callback
  3978. */
  3979. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  3980. {
  3981. enum hrtimer_restart ret = HRTIMER_RESTART;
  3982. struct perf_sample_data data;
  3983. struct pt_regs *regs;
  3984. struct perf_event *event;
  3985. u64 period;
  3986. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  3987. event->pmu->read(event);
  3988. perf_sample_data_init(&data, 0);
  3989. data.period = event->hw.last_period;
  3990. regs = get_irq_regs();
  3991. if (regs && !perf_exclude_event(event, regs)) {
  3992. if (!(event->attr.exclude_idle && current->pid == 0))
  3993. if (perf_event_overflow(event, 0, &data, regs))
  3994. ret = HRTIMER_NORESTART;
  3995. }
  3996. period = max_t(u64, 10000, event->hw.sample_period);
  3997. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  3998. return ret;
  3999. }
  4000. static void perf_swevent_start_hrtimer(struct perf_event *event)
  4001. {
  4002. struct hw_perf_event *hwc = &event->hw;
  4003. s64 period;
  4004. if (!is_sampling_event(event))
  4005. return;
  4006. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  4007. hwc->hrtimer.function = perf_swevent_hrtimer;
  4008. period = local64_read(&hwc->period_left);
  4009. if (period) {
  4010. if (period < 0)
  4011. period = 10000;
  4012. local64_set(&hwc->period_left, 0);
  4013. } else {
  4014. period = max_t(u64, 10000, hwc->sample_period);
  4015. }
  4016. __hrtimer_start_range_ns(&hwc->hrtimer,
  4017. ns_to_ktime(period), 0,
  4018. HRTIMER_MODE_REL_PINNED, 0);
  4019. }
  4020. static void perf_swevent_cancel_hrtimer(struct perf_event *event)
  4021. {
  4022. struct hw_perf_event *hwc = &event->hw;
  4023. if (is_sampling_event(event)) {
  4024. ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
  4025. local64_set(&hwc->period_left, ktime_to_ns(remaining));
  4026. hrtimer_cancel(&hwc->hrtimer);
  4027. }
  4028. }
  4029. /*
  4030. * Software event: cpu wall time clock
  4031. */
  4032. static void cpu_clock_event_update(struct perf_event *event)
  4033. {
  4034. s64 prev;
  4035. u64 now;
  4036. now = local_clock();
  4037. prev = local64_xchg(&event->hw.prev_count, now);
  4038. local64_add(now - prev, &event->count);
  4039. }
  4040. static void cpu_clock_event_start(struct perf_event *event, int flags)
  4041. {
  4042. local64_set(&event->hw.prev_count, local_clock());
  4043. perf_swevent_start_hrtimer(event);
  4044. }
  4045. static void cpu_clock_event_stop(struct perf_event *event, int flags)
  4046. {
  4047. perf_swevent_cancel_hrtimer(event);
  4048. cpu_clock_event_update(event);
  4049. }
  4050. static int cpu_clock_event_add(struct perf_event *event, int flags)
  4051. {
  4052. if (flags & PERF_EF_START)
  4053. cpu_clock_event_start(event, flags);
  4054. return 0;
  4055. }
  4056. static void cpu_clock_event_del(struct perf_event *event, int flags)
  4057. {
  4058. cpu_clock_event_stop(event, flags);
  4059. }
  4060. static void cpu_clock_event_read(struct perf_event *event)
  4061. {
  4062. cpu_clock_event_update(event);
  4063. }
  4064. static int cpu_clock_event_init(struct perf_event *event)
  4065. {
  4066. if (event->attr.type != PERF_TYPE_SOFTWARE)
  4067. return -ENOENT;
  4068. if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
  4069. return -ENOENT;
  4070. return 0;
  4071. }
  4072. static struct pmu perf_cpu_clock = {
  4073. .task_ctx_nr = perf_sw_context,
  4074. .event_init = cpu_clock_event_init,
  4075. .add = cpu_clock_event_add,
  4076. .del = cpu_clock_event_del,
  4077. .start = cpu_clock_event_start,
  4078. .stop = cpu_clock_event_stop,
  4079. .read = cpu_clock_event_read,
  4080. };
  4081. /*
  4082. * Software event: task time clock
  4083. */
  4084. static void task_clock_event_update(struct perf_event *event, u64 now)
  4085. {
  4086. u64 prev;
  4087. s64 delta;
  4088. prev = local64_xchg(&event->hw.prev_count, now);
  4089. delta = now - prev;
  4090. local64_add(delta, &event->count);
  4091. }
  4092. static void task_clock_event_start(struct perf_event *event, int flags)
  4093. {
  4094. local64_set(&event->hw.prev_count, event->ctx->time);
  4095. perf_swevent_start_hrtimer(event);
  4096. }
  4097. static void task_clock_event_stop(struct perf_event *event, int flags)
  4098. {
  4099. perf_swevent_cancel_hrtimer(event);
  4100. task_clock_event_update(event, event->ctx->time);
  4101. }
  4102. static int task_clock_event_add(struct perf_event *event, int flags)
  4103. {
  4104. if (flags & PERF_EF_START)
  4105. task_clock_event_start(event, flags);
  4106. return 0;
  4107. }
  4108. static void task_clock_event_del(struct perf_event *event, int flags)
  4109. {
  4110. task_clock_event_stop(event, PERF_EF_UPDATE);
  4111. }
  4112. static void task_clock_event_read(struct perf_event *event)
  4113. {
  4114. u64 time;
  4115. if (!in_nmi()) {
  4116. update_context_time(event->ctx);
  4117. time = event->ctx->time;
  4118. } else {
  4119. u64 now = perf_clock();
  4120. u64 delta = now - event->ctx->timestamp;
  4121. time = event->ctx->time + delta;
  4122. }
  4123. task_clock_event_update(event, time);
  4124. }
  4125. static int task_clock_event_init(struct perf_event *event)
  4126. {
  4127. if (event->attr.type != PERF_TYPE_SOFTWARE)
  4128. return -ENOENT;
  4129. if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
  4130. return -ENOENT;
  4131. return 0;
  4132. }
  4133. static struct pmu perf_task_clock = {
  4134. .task_ctx_nr = perf_sw_context,
  4135. .event_init = task_clock_event_init,
  4136. .add = task_clock_event_add,
  4137. .del = task_clock_event_del,
  4138. .start = task_clock_event_start,
  4139. .stop = task_clock_event_stop,
  4140. .read = task_clock_event_read,
  4141. };
  4142. static void perf_pmu_nop_void(struct pmu *pmu)
  4143. {
  4144. }
  4145. static int perf_pmu_nop_int(struct pmu *pmu)
  4146. {
  4147. return 0;
  4148. }
  4149. static void perf_pmu_start_txn(struct pmu *pmu)
  4150. {
  4151. perf_pmu_disable(pmu);
  4152. }
  4153. static int perf_pmu_commit_txn(struct pmu *pmu)
  4154. {
  4155. perf_pmu_enable(pmu);
  4156. return 0;
  4157. }
  4158. static void perf_pmu_cancel_txn(struct pmu *pmu)
  4159. {
  4160. perf_pmu_enable(pmu);
  4161. }
  4162. /*
  4163. * Ensures all contexts with the same task_ctx_nr have the same
  4164. * pmu_cpu_context too.
  4165. */
  4166. static void *find_pmu_context(int ctxn)
  4167. {
  4168. struct pmu *pmu;
  4169. if (ctxn < 0)
  4170. return NULL;
  4171. list_for_each_entry(pmu, &pmus, entry) {
  4172. if (pmu->task_ctx_nr == ctxn)
  4173. return pmu->pmu_cpu_context;
  4174. }
  4175. return NULL;
  4176. }
  4177. static void free_pmu_context(void * __percpu cpu_context)
  4178. {
  4179. struct pmu *pmu;
  4180. mutex_lock(&pmus_lock);
  4181. /*
  4182. * Like a real lame refcount.
  4183. */
  4184. list_for_each_entry(pmu, &pmus, entry) {
  4185. if (pmu->pmu_cpu_context == cpu_context)
  4186. goto out;
  4187. }
  4188. free_percpu(cpu_context);
  4189. out:
  4190. mutex_unlock(&pmus_lock);
  4191. }
  4192. int perf_pmu_register(struct pmu *pmu)
  4193. {
  4194. int cpu, ret;
  4195. mutex_lock(&pmus_lock);
  4196. ret = -ENOMEM;
  4197. pmu->pmu_disable_count = alloc_percpu(int);
  4198. if (!pmu->pmu_disable_count)
  4199. goto unlock;
  4200. pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
  4201. if (pmu->pmu_cpu_context)
  4202. goto got_cpu_context;
  4203. pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
  4204. if (!pmu->pmu_cpu_context)
  4205. goto free_pdc;
  4206. for_each_possible_cpu(cpu) {
  4207. struct perf_cpu_context *cpuctx;
  4208. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  4209. __perf_event_init_context(&cpuctx->ctx);
  4210. cpuctx->ctx.type = cpu_context;
  4211. cpuctx->ctx.pmu = pmu;
  4212. cpuctx->jiffies_interval = 1;
  4213. INIT_LIST_HEAD(&cpuctx->rotation_list);
  4214. }
  4215. got_cpu_context:
  4216. if (!pmu->start_txn) {
  4217. if (pmu->pmu_enable) {
  4218. /*
  4219. * If we have pmu_enable/pmu_disable calls, install
  4220. * transaction stubs that use that to try and batch
  4221. * hardware accesses.
  4222. */
  4223. pmu->start_txn = perf_pmu_start_txn;
  4224. pmu->commit_txn = perf_pmu_commit_txn;
  4225. pmu->cancel_txn = perf_pmu_cancel_txn;
  4226. } else {
  4227. pmu->start_txn = perf_pmu_nop_void;
  4228. pmu->commit_txn = perf_pmu_nop_int;
  4229. pmu->cancel_txn = perf_pmu_nop_void;
  4230. }
  4231. }
  4232. if (!pmu->pmu_enable) {
  4233. pmu->pmu_enable = perf_pmu_nop_void;
  4234. pmu->pmu_disable = perf_pmu_nop_void;
  4235. }
  4236. list_add_rcu(&pmu->entry, &pmus);
  4237. ret = 0;
  4238. unlock:
  4239. mutex_unlock(&pmus_lock);
  4240. return ret;
  4241. free_pdc:
  4242. free_percpu(pmu->pmu_disable_count);
  4243. goto unlock;
  4244. }
  4245. void perf_pmu_unregister(struct pmu *pmu)
  4246. {
  4247. mutex_lock(&pmus_lock);
  4248. list_del_rcu(&pmu->entry);
  4249. mutex_unlock(&pmus_lock);
  4250. /*
  4251. * We dereference the pmu list under both SRCU and regular RCU, so
  4252. * synchronize against both of those.
  4253. */
  4254. synchronize_srcu(&pmus_srcu);
  4255. synchronize_rcu();
  4256. free_percpu(pmu->pmu_disable_count);
  4257. free_pmu_context(pmu->pmu_cpu_context);
  4258. }
  4259. struct pmu *perf_init_event(struct perf_event *event)
  4260. {
  4261. struct pmu *pmu = NULL;
  4262. int idx;
  4263. idx = srcu_read_lock(&pmus_srcu);
  4264. list_for_each_entry_rcu(pmu, &pmus, entry) {
  4265. int ret = pmu->event_init(event);
  4266. if (!ret)
  4267. goto unlock;
  4268. if (ret != -ENOENT) {
  4269. pmu = ERR_PTR(ret);
  4270. goto unlock;
  4271. }
  4272. }
  4273. pmu = ERR_PTR(-ENOENT);
  4274. unlock:
  4275. srcu_read_unlock(&pmus_srcu, idx);
  4276. return pmu;
  4277. }
  4278. /*
  4279. * Allocate and initialize a event structure
  4280. */
  4281. static struct perf_event *
  4282. perf_event_alloc(struct perf_event_attr *attr, int cpu,
  4283. struct task_struct *task,
  4284. struct perf_event *group_leader,
  4285. struct perf_event *parent_event,
  4286. perf_overflow_handler_t overflow_handler)
  4287. {
  4288. struct pmu *pmu;
  4289. struct perf_event *event;
  4290. struct hw_perf_event *hwc;
  4291. long err;
  4292. event = kzalloc(sizeof(*event), GFP_KERNEL);
  4293. if (!event)
  4294. return ERR_PTR(-ENOMEM);
  4295. /*
  4296. * Single events are their own group leaders, with an
  4297. * empty sibling list:
  4298. */
  4299. if (!group_leader)
  4300. group_leader = event;
  4301. mutex_init(&event->child_mutex);
  4302. INIT_LIST_HEAD(&event->child_list);
  4303. INIT_LIST_HEAD(&event->group_entry);
  4304. INIT_LIST_HEAD(&event->event_entry);
  4305. INIT_LIST_HEAD(&event->sibling_list);
  4306. init_waitqueue_head(&event->waitq);
  4307. init_irq_work(&event->pending, perf_pending_event);
  4308. mutex_init(&event->mmap_mutex);
  4309. event->cpu = cpu;
  4310. event->attr = *attr;
  4311. event->group_leader = group_leader;
  4312. event->pmu = NULL;
  4313. event->oncpu = -1;
  4314. event->parent = parent_event;
  4315. event->ns = get_pid_ns(current->nsproxy->pid_ns);
  4316. event->id = atomic64_inc_return(&perf_event_id);
  4317. event->state = PERF_EVENT_STATE_INACTIVE;
  4318. if (task) {
  4319. event->attach_state = PERF_ATTACH_TASK;
  4320. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  4321. /*
  4322. * hw_breakpoint is a bit difficult here..
  4323. */
  4324. if (attr->type == PERF_TYPE_BREAKPOINT)
  4325. event->hw.bp_target = task;
  4326. #endif
  4327. }
  4328. if (!overflow_handler && parent_event)
  4329. overflow_handler = parent_event->overflow_handler;
  4330. event->overflow_handler = overflow_handler;
  4331. if (attr->disabled)
  4332. event->state = PERF_EVENT_STATE_OFF;
  4333. pmu = NULL;
  4334. hwc = &event->hw;
  4335. hwc->sample_period = attr->sample_period;
  4336. if (attr->freq && attr->sample_freq)
  4337. hwc->sample_period = 1;
  4338. hwc->last_period = hwc->sample_period;
  4339. local64_set(&hwc->period_left, hwc->sample_period);
  4340. /*
  4341. * we currently do not support PERF_FORMAT_GROUP on inherited events
  4342. */
  4343. if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
  4344. goto done;
  4345. pmu = perf_init_event(event);
  4346. done:
  4347. err = 0;
  4348. if (!pmu)
  4349. err = -EINVAL;
  4350. else if (IS_ERR(pmu))
  4351. err = PTR_ERR(pmu);
  4352. if (err) {
  4353. if (event->ns)
  4354. put_pid_ns(event->ns);
  4355. kfree(event);
  4356. return ERR_PTR(err);
  4357. }
  4358. event->pmu = pmu;
  4359. if (!event->parent) {
  4360. if (event->attach_state & PERF_ATTACH_TASK)
  4361. jump_label_inc(&perf_task_events);
  4362. if (event->attr.mmap || event->attr.mmap_data)
  4363. atomic_inc(&nr_mmap_events);
  4364. if (event->attr.comm)
  4365. atomic_inc(&nr_comm_events);
  4366. if (event->attr.task)
  4367. atomic_inc(&nr_task_events);
  4368. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  4369. err = get_callchain_buffers();
  4370. if (err) {
  4371. free_event(event);
  4372. return ERR_PTR(err);
  4373. }
  4374. }
  4375. }
  4376. return event;
  4377. }
  4378. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  4379. struct perf_event_attr *attr)
  4380. {
  4381. u32 size;
  4382. int ret;
  4383. if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  4384. return -EFAULT;
  4385. /*
  4386. * zero the full structure, so that a short copy will be nice.
  4387. */
  4388. memset(attr, 0, sizeof(*attr));
  4389. ret = get_user(size, &uattr->size);
  4390. if (ret)
  4391. return ret;
  4392. if (size > PAGE_SIZE) /* silly large */
  4393. goto err_size;
  4394. if (!size) /* abi compat */
  4395. size = PERF_ATTR_SIZE_VER0;
  4396. if (size < PERF_ATTR_SIZE_VER0)
  4397. goto err_size;
  4398. /*
  4399. * If we're handed a bigger struct than we know of,
  4400. * ensure all the unknown bits are 0 - i.e. new
  4401. * user-space does not rely on any kernel feature
  4402. * extensions we dont know about yet.
  4403. */
  4404. if (size > sizeof(*attr)) {
  4405. unsigned char __user *addr;
  4406. unsigned char __user *end;
  4407. unsigned char val;
  4408. addr = (void __user *)uattr + sizeof(*attr);
  4409. end = (void __user *)uattr + size;
  4410. for (; addr < end; addr++) {
  4411. ret = get_user(val, addr);
  4412. if (ret)
  4413. return ret;
  4414. if (val)
  4415. goto err_size;
  4416. }
  4417. size = sizeof(*attr);
  4418. }
  4419. ret = copy_from_user(attr, uattr, size);
  4420. if (ret)
  4421. return -EFAULT;
  4422. /*
  4423. * If the type exists, the corresponding creation will verify
  4424. * the attr->config.
  4425. */
  4426. if (attr->type >= PERF_TYPE_MAX)
  4427. return -EINVAL;
  4428. if (attr->__reserved_1)
  4429. return -EINVAL;
  4430. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  4431. return -EINVAL;
  4432. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  4433. return -EINVAL;
  4434. out:
  4435. return ret;
  4436. err_size:
  4437. put_user(sizeof(*attr), &uattr->size);
  4438. ret = -E2BIG;
  4439. goto out;
  4440. }
  4441. static int
  4442. perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
  4443. {
  4444. struct perf_buffer *buffer = NULL, *old_buffer = NULL;
  4445. int ret = -EINVAL;
  4446. if (!output_event)
  4447. goto set;
  4448. /* don't allow circular references */
  4449. if (event == output_event)
  4450. goto out;
  4451. /*
  4452. * Don't allow cross-cpu buffers
  4453. */
  4454. if (output_event->cpu != event->cpu)
  4455. goto out;
  4456. /*
  4457. * If its not a per-cpu buffer, it must be the same task.
  4458. */
  4459. if (output_event->cpu == -1 && output_event->ctx != event->ctx)
  4460. goto out;
  4461. set:
  4462. mutex_lock(&event->mmap_mutex);
  4463. /* Can't redirect output if we've got an active mmap() */
  4464. if (atomic_read(&event->mmap_count))
  4465. goto unlock;
  4466. if (output_event) {
  4467. /* get the buffer we want to redirect to */
  4468. buffer = perf_buffer_get(output_event);
  4469. if (!buffer)
  4470. goto unlock;
  4471. }
  4472. old_buffer = event->buffer;
  4473. rcu_assign_pointer(event->buffer, buffer);
  4474. ret = 0;
  4475. unlock:
  4476. mutex_unlock(&event->mmap_mutex);
  4477. if (old_buffer)
  4478. perf_buffer_put(old_buffer);
  4479. out:
  4480. return ret;
  4481. }
  4482. /**
  4483. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  4484. *
  4485. * @attr_uptr: event_id type attributes for monitoring/sampling
  4486. * @pid: target pid
  4487. * @cpu: target cpu
  4488. * @group_fd: group leader event fd
  4489. */
  4490. SYSCALL_DEFINE5(perf_event_open,
  4491. struct perf_event_attr __user *, attr_uptr,
  4492. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  4493. {
  4494. struct perf_event *group_leader = NULL, *output_event = NULL;
  4495. struct perf_event *event, *sibling;
  4496. struct perf_event_attr attr;
  4497. struct perf_event_context *ctx;
  4498. struct file *event_file = NULL;
  4499. struct file *group_file = NULL;
  4500. struct task_struct *task = NULL;
  4501. struct pmu *pmu;
  4502. int event_fd;
  4503. int move_group = 0;
  4504. int fput_needed = 0;
  4505. int err;
  4506. /* for future expandability... */
  4507. if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
  4508. return -EINVAL;
  4509. err = perf_copy_attr(attr_uptr, &attr);
  4510. if (err)
  4511. return err;
  4512. if (!attr.exclude_kernel) {
  4513. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  4514. return -EACCES;
  4515. }
  4516. if (attr.freq) {
  4517. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  4518. return -EINVAL;
  4519. }
  4520. event_fd = get_unused_fd_flags(O_RDWR);
  4521. if (event_fd < 0)
  4522. return event_fd;
  4523. if (group_fd != -1) {
  4524. group_leader = perf_fget_light(group_fd, &fput_needed);
  4525. if (IS_ERR(group_leader)) {
  4526. err = PTR_ERR(group_leader);
  4527. goto err_fd;
  4528. }
  4529. group_file = group_leader->filp;
  4530. if (flags & PERF_FLAG_FD_OUTPUT)
  4531. output_event = group_leader;
  4532. if (flags & PERF_FLAG_FD_NO_GROUP)
  4533. group_leader = NULL;
  4534. }
  4535. if (pid != -1) {
  4536. task = find_lively_task_by_vpid(pid);
  4537. if (IS_ERR(task)) {
  4538. err = PTR_ERR(task);
  4539. goto err_group_fd;
  4540. }
  4541. }
  4542. event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
  4543. if (IS_ERR(event)) {
  4544. err = PTR_ERR(event);
  4545. goto err_task;
  4546. }
  4547. /*
  4548. * Special case software events and allow them to be part of
  4549. * any hardware group.
  4550. */
  4551. pmu = event->pmu;
  4552. if (group_leader &&
  4553. (is_software_event(event) != is_software_event(group_leader))) {
  4554. if (is_software_event(event)) {
  4555. /*
  4556. * If event and group_leader are not both a software
  4557. * event, and event is, then group leader is not.
  4558. *
  4559. * Allow the addition of software events to !software
  4560. * groups, this is safe because software events never
  4561. * fail to schedule.
  4562. */
  4563. pmu = group_leader->pmu;
  4564. } else if (is_software_event(group_leader) &&
  4565. (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
  4566. /*
  4567. * In case the group is a pure software group, and we
  4568. * try to add a hardware event, move the whole group to
  4569. * the hardware context.
  4570. */
  4571. move_group = 1;
  4572. }
  4573. }
  4574. /*
  4575. * Get the target context (task or percpu):
  4576. */
  4577. ctx = find_get_context(pmu, task, cpu);
  4578. if (IS_ERR(ctx)) {
  4579. err = PTR_ERR(ctx);
  4580. goto err_alloc;
  4581. }
  4582. /*
  4583. * Look up the group leader (we will attach this event to it):
  4584. */
  4585. if (group_leader) {
  4586. err = -EINVAL;
  4587. /*
  4588. * Do not allow a recursive hierarchy (this new sibling
  4589. * becoming part of another group-sibling):
  4590. */
  4591. if (group_leader->group_leader != group_leader)
  4592. goto err_context;
  4593. /*
  4594. * Do not allow to attach to a group in a different
  4595. * task or CPU context:
  4596. */
  4597. if (move_group) {
  4598. if (group_leader->ctx->type != ctx->type)
  4599. goto err_context;
  4600. } else {
  4601. if (group_leader->ctx != ctx)
  4602. goto err_context;
  4603. }
  4604. /*
  4605. * Only a group leader can be exclusive or pinned
  4606. */
  4607. if (attr.exclusive || attr.pinned)
  4608. goto err_context;
  4609. }
  4610. if (output_event) {
  4611. err = perf_event_set_output(event, output_event);
  4612. if (err)
  4613. goto err_context;
  4614. }
  4615. event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
  4616. if (IS_ERR(event_file)) {
  4617. err = PTR_ERR(event_file);
  4618. goto err_context;
  4619. }
  4620. if (move_group) {
  4621. struct perf_event_context *gctx = group_leader->ctx;
  4622. mutex_lock(&gctx->mutex);
  4623. perf_event_remove_from_context(group_leader);
  4624. list_for_each_entry(sibling, &group_leader->sibling_list,
  4625. group_entry) {
  4626. perf_event_remove_from_context(sibling);
  4627. put_ctx(gctx);
  4628. }
  4629. mutex_unlock(&gctx->mutex);
  4630. put_ctx(gctx);
  4631. }
  4632. event->filp = event_file;
  4633. WARN_ON_ONCE(ctx->parent_ctx);
  4634. mutex_lock(&ctx->mutex);
  4635. if (move_group) {
  4636. perf_install_in_context(ctx, group_leader, cpu);
  4637. get_ctx(ctx);
  4638. list_for_each_entry(sibling, &group_leader->sibling_list,
  4639. group_entry) {
  4640. perf_install_in_context(ctx, sibling, cpu);
  4641. get_ctx(ctx);
  4642. }
  4643. }
  4644. perf_install_in_context(ctx, event, cpu);
  4645. ++ctx->generation;
  4646. mutex_unlock(&ctx->mutex);
  4647. event->owner = current;
  4648. mutex_lock(&current->perf_event_mutex);
  4649. list_add_tail(&event->owner_entry, &current->perf_event_list);
  4650. mutex_unlock(&current->perf_event_mutex);
  4651. /*
  4652. * Precalculate sample_data sizes
  4653. */
  4654. perf_event__header_size(event);
  4655. /*
  4656. * Drop the reference on the group_event after placing the
  4657. * new event on the sibling_list. This ensures destruction
  4658. * of the group leader will find the pointer to itself in
  4659. * perf_group_detach().
  4660. */
  4661. fput_light(group_file, fput_needed);
  4662. fd_install(event_fd, event_file);
  4663. return event_fd;
  4664. err_context:
  4665. put_ctx(ctx);
  4666. err_alloc:
  4667. free_event(event);
  4668. err_task:
  4669. if (task)
  4670. put_task_struct(task);
  4671. err_group_fd:
  4672. fput_light(group_file, fput_needed);
  4673. err_fd:
  4674. put_unused_fd(event_fd);
  4675. return err;
  4676. }
  4677. /**
  4678. * perf_event_create_kernel_counter
  4679. *
  4680. * @attr: attributes of the counter to create
  4681. * @cpu: cpu in which the counter is bound
  4682. * @task: task to profile (NULL for percpu)
  4683. */
  4684. struct perf_event *
  4685. perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  4686. struct task_struct *task,
  4687. perf_overflow_handler_t overflow_handler)
  4688. {
  4689. struct perf_event_context *ctx;
  4690. struct perf_event *event;
  4691. int err;
  4692. /*
  4693. * Get the target context (task or percpu):
  4694. */
  4695. event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
  4696. if (IS_ERR(event)) {
  4697. err = PTR_ERR(event);
  4698. goto err;
  4699. }
  4700. ctx = find_get_context(event->pmu, task, cpu);
  4701. if (IS_ERR(ctx)) {
  4702. err = PTR_ERR(ctx);
  4703. goto err_free;
  4704. }
  4705. event->filp = NULL;
  4706. WARN_ON_ONCE(ctx->parent_ctx);
  4707. mutex_lock(&ctx->mutex);
  4708. perf_install_in_context(ctx, event, cpu);
  4709. ++ctx->generation;
  4710. mutex_unlock(&ctx->mutex);
  4711. return event;
  4712. err_free:
  4713. free_event(event);
  4714. err:
  4715. return ERR_PTR(err);
  4716. }
  4717. EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
  4718. static void sync_child_event(struct perf_event *child_event,
  4719. struct task_struct *child)
  4720. {
  4721. struct perf_event *parent_event = child_event->parent;
  4722. u64 child_val;
  4723. if (child_event->attr.inherit_stat)
  4724. perf_event_read_event(child_event, child);
  4725. child_val = perf_event_count(child_event);
  4726. /*
  4727. * Add back the child's count to the parent's count:
  4728. */
  4729. atomic64_add(child_val, &parent_event->child_count);
  4730. atomic64_add(child_event->total_time_enabled,
  4731. &parent_event->child_total_time_enabled);
  4732. atomic64_add(child_event->total_time_running,
  4733. &parent_event->child_total_time_running);
  4734. /*
  4735. * Remove this event from the parent's list
  4736. */
  4737. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  4738. mutex_lock(&parent_event->child_mutex);
  4739. list_del_init(&child_event->child_list);
  4740. mutex_unlock(&parent_event->child_mutex);
  4741. /*
  4742. * Release the parent event, if this was the last
  4743. * reference to it.
  4744. */
  4745. fput(parent_event->filp);
  4746. }
  4747. static void
  4748. __perf_event_exit_task(struct perf_event *child_event,
  4749. struct perf_event_context *child_ctx,
  4750. struct task_struct *child)
  4751. {
  4752. struct perf_event *parent_event;
  4753. perf_event_remove_from_context(child_event);
  4754. parent_event = child_event->parent;
  4755. /*
  4756. * It can happen that parent exits first, and has events
  4757. * that are still around due to the child reference. These
  4758. * events need to be zapped - but otherwise linger.
  4759. */
  4760. if (parent_event) {
  4761. sync_child_event(child_event, child);
  4762. free_event(child_event);
  4763. }
  4764. }
  4765. static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
  4766. {
  4767. struct perf_event *child_event, *tmp;
  4768. struct perf_event_context *child_ctx;
  4769. unsigned long flags;
  4770. if (likely(!child->perf_event_ctxp[ctxn])) {
  4771. perf_event_task(child, NULL, 0);
  4772. return;
  4773. }
  4774. local_irq_save(flags);
  4775. /*
  4776. * We can't reschedule here because interrupts are disabled,
  4777. * and either child is current or it is a task that can't be
  4778. * scheduled, so we are now safe from rescheduling changing
  4779. * our context.
  4780. */
  4781. child_ctx = child->perf_event_ctxp[ctxn];
  4782. task_ctx_sched_out(child_ctx, EVENT_ALL);
  4783. /*
  4784. * Take the context lock here so that if find_get_context is
  4785. * reading child->perf_event_ctxp, we wait until it has
  4786. * incremented the context's refcount before we do put_ctx below.
  4787. */
  4788. raw_spin_lock(&child_ctx->lock);
  4789. child->perf_event_ctxp[ctxn] = NULL;
  4790. /*
  4791. * If this context is a clone; unclone it so it can't get
  4792. * swapped to another process while we're removing all
  4793. * the events from it.
  4794. */
  4795. unclone_ctx(child_ctx);
  4796. update_context_time(child_ctx);
  4797. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  4798. /*
  4799. * Report the task dead after unscheduling the events so that we
  4800. * won't get any samples after PERF_RECORD_EXIT. We can however still
  4801. * get a few PERF_RECORD_READ events.
  4802. */
  4803. perf_event_task(child, child_ctx, 0);
  4804. /*
  4805. * We can recurse on the same lock type through:
  4806. *
  4807. * __perf_event_exit_task()
  4808. * sync_child_event()
  4809. * fput(parent_event->filp)
  4810. * perf_release()
  4811. * mutex_lock(&ctx->mutex)
  4812. *
  4813. * But since its the parent context it won't be the same instance.
  4814. */
  4815. mutex_lock(&child_ctx->mutex);
  4816. again:
  4817. list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
  4818. group_entry)
  4819. __perf_event_exit_task(child_event, child_ctx, child);
  4820. list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
  4821. group_entry)
  4822. __perf_event_exit_task(child_event, child_ctx, child);
  4823. /*
  4824. * If the last event was a group event, it will have appended all
  4825. * its siblings to the list, but we obtained 'tmp' before that which
  4826. * will still point to the list head terminating the iteration.
  4827. */
  4828. if (!list_empty(&child_ctx->pinned_groups) ||
  4829. !list_empty(&child_ctx->flexible_groups))
  4830. goto again;
  4831. mutex_unlock(&child_ctx->mutex);
  4832. put_ctx(child_ctx);
  4833. }
  4834. /*
  4835. * When a child task exits, feed back event values to parent events.
  4836. */
  4837. void perf_event_exit_task(struct task_struct *child)
  4838. {
  4839. struct perf_event *event, *tmp;
  4840. int ctxn;
  4841. mutex_lock(&child->perf_event_mutex);
  4842. list_for_each_entry_safe(event, tmp, &child->perf_event_list,
  4843. owner_entry) {
  4844. list_del_init(&event->owner_entry);
  4845. /*
  4846. * Ensure the list deletion is visible before we clear
  4847. * the owner, closes a race against perf_release() where
  4848. * we need to serialize on the owner->perf_event_mutex.
  4849. */
  4850. smp_wmb();
  4851. event->owner = NULL;
  4852. }
  4853. mutex_unlock(&child->perf_event_mutex);
  4854. for_each_task_context_nr(ctxn)
  4855. perf_event_exit_task_context(child, ctxn);
  4856. }
  4857. static void perf_free_event(struct perf_event *event,
  4858. struct perf_event_context *ctx)
  4859. {
  4860. struct perf_event *parent = event->parent;
  4861. if (WARN_ON_ONCE(!parent))
  4862. return;
  4863. mutex_lock(&parent->child_mutex);
  4864. list_del_init(&event->child_list);
  4865. mutex_unlock(&parent->child_mutex);
  4866. fput(parent->filp);
  4867. perf_group_detach(event);
  4868. list_del_event(event, ctx);
  4869. free_event(event);
  4870. }
  4871. /*
  4872. * free an unexposed, unused context as created by inheritance by
  4873. * perf_event_init_task below, used by fork() in case of fail.
  4874. */
  4875. void perf_event_free_task(struct task_struct *task)
  4876. {
  4877. struct perf_event_context *ctx;
  4878. struct perf_event *event, *tmp;
  4879. int ctxn;
  4880. for_each_task_context_nr(ctxn) {
  4881. ctx = task->perf_event_ctxp[ctxn];
  4882. if (!ctx)
  4883. continue;
  4884. mutex_lock(&ctx->mutex);
  4885. again:
  4886. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
  4887. group_entry)
  4888. perf_free_event(event, ctx);
  4889. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
  4890. group_entry)
  4891. perf_free_event(event, ctx);
  4892. if (!list_empty(&ctx->pinned_groups) ||
  4893. !list_empty(&ctx->flexible_groups))
  4894. goto again;
  4895. mutex_unlock(&ctx->mutex);
  4896. put_ctx(ctx);
  4897. }
  4898. }
  4899. void perf_event_delayed_put(struct task_struct *task)
  4900. {
  4901. int ctxn;
  4902. for_each_task_context_nr(ctxn)
  4903. WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
  4904. }
  4905. /*
  4906. * inherit a event from parent task to child task:
  4907. */
  4908. static struct perf_event *
  4909. inherit_event(struct perf_event *parent_event,
  4910. struct task_struct *parent,
  4911. struct perf_event_context *parent_ctx,
  4912. struct task_struct *child,
  4913. struct perf_event *group_leader,
  4914. struct perf_event_context *child_ctx)
  4915. {
  4916. struct perf_event *child_event;
  4917. unsigned long flags;
  4918. /*
  4919. * Instead of creating recursive hierarchies of events,
  4920. * we link inherited events back to the original parent,
  4921. * which has a filp for sure, which we use as the reference
  4922. * count:
  4923. */
  4924. if (parent_event->parent)
  4925. parent_event = parent_event->parent;
  4926. child_event = perf_event_alloc(&parent_event->attr,
  4927. parent_event->cpu,
  4928. child,
  4929. group_leader, parent_event,
  4930. NULL);
  4931. if (IS_ERR(child_event))
  4932. return child_event;
  4933. get_ctx(child_ctx);
  4934. /*
  4935. * Make the child state follow the state of the parent event,
  4936. * not its attr.disabled bit. We hold the parent's mutex,
  4937. * so we won't race with perf_event_{en, dis}able_family.
  4938. */
  4939. if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
  4940. child_event->state = PERF_EVENT_STATE_INACTIVE;
  4941. else
  4942. child_event->state = PERF_EVENT_STATE_OFF;
  4943. if (parent_event->attr.freq) {
  4944. u64 sample_period = parent_event->hw.sample_period;
  4945. struct hw_perf_event *hwc = &child_event->hw;
  4946. hwc->sample_period = sample_period;
  4947. hwc->last_period = sample_period;
  4948. local64_set(&hwc->period_left, sample_period);
  4949. }
  4950. child_event->ctx = child_ctx;
  4951. child_event->overflow_handler = parent_event->overflow_handler;
  4952. /*
  4953. * Precalculate sample_data sizes
  4954. */
  4955. perf_event__header_size(child_event);
  4956. /*
  4957. * Link it up in the child's context:
  4958. */
  4959. raw_spin_lock_irqsave(&child_ctx->lock, flags);
  4960. add_event_to_ctx(child_event, child_ctx);
  4961. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  4962. /*
  4963. * Get a reference to the parent filp - we will fput it
  4964. * when the child event exits. This is safe to do because
  4965. * we are in the parent and we know that the filp still
  4966. * exists and has a nonzero count:
  4967. */
  4968. atomic_long_inc(&parent_event->filp->f_count);
  4969. /*
  4970. * Link this into the parent event's child list
  4971. */
  4972. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  4973. mutex_lock(&parent_event->child_mutex);
  4974. list_add_tail(&child_event->child_list, &parent_event->child_list);
  4975. mutex_unlock(&parent_event->child_mutex);
  4976. return child_event;
  4977. }
  4978. static int inherit_group(struct perf_event *parent_event,
  4979. struct task_struct *parent,
  4980. struct perf_event_context *parent_ctx,
  4981. struct task_struct *child,
  4982. struct perf_event_context *child_ctx)
  4983. {
  4984. struct perf_event *leader;
  4985. struct perf_event *sub;
  4986. struct perf_event *child_ctr;
  4987. leader = inherit_event(parent_event, parent, parent_ctx,
  4988. child, NULL, child_ctx);
  4989. if (IS_ERR(leader))
  4990. return PTR_ERR(leader);
  4991. list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  4992. child_ctr = inherit_event(sub, parent, parent_ctx,
  4993. child, leader, child_ctx);
  4994. if (IS_ERR(child_ctr))
  4995. return PTR_ERR(child_ctr);
  4996. }
  4997. return 0;
  4998. }
  4999. static int
  5000. inherit_task_group(struct perf_event *event, struct task_struct *parent,
  5001. struct perf_event_context *parent_ctx,
  5002. struct task_struct *child, int ctxn,
  5003. int *inherited_all)
  5004. {
  5005. int ret;
  5006. struct perf_event_context *child_ctx;
  5007. if (!event->attr.inherit) {
  5008. *inherited_all = 0;
  5009. return 0;
  5010. }
  5011. child_ctx = child->perf_event_ctxp[ctxn];
  5012. if (!child_ctx) {
  5013. /*
  5014. * This is executed from the parent task context, so
  5015. * inherit events that have been marked for cloning.
  5016. * First allocate and initialize a context for the
  5017. * child.
  5018. */
  5019. child_ctx = alloc_perf_context(event->pmu, child);
  5020. if (!child_ctx)
  5021. return -ENOMEM;
  5022. child->perf_event_ctxp[ctxn] = child_ctx;
  5023. }
  5024. ret = inherit_group(event, parent, parent_ctx,
  5025. child, child_ctx);
  5026. if (ret)
  5027. *inherited_all = 0;
  5028. return ret;
  5029. }
  5030. /*
  5031. * Initialize the perf_event context in task_struct
  5032. */
  5033. int perf_event_init_context(struct task_struct *child, int ctxn)
  5034. {
  5035. struct perf_event_context *child_ctx, *parent_ctx;
  5036. struct perf_event_context *cloned_ctx;
  5037. struct perf_event *event;
  5038. struct task_struct *parent = current;
  5039. int inherited_all = 1;
  5040. unsigned long flags;
  5041. int ret = 0;
  5042. child->perf_event_ctxp[ctxn] = NULL;
  5043. mutex_init(&child->perf_event_mutex);
  5044. INIT_LIST_HEAD(&child->perf_event_list);
  5045. if (likely(!parent->perf_event_ctxp[ctxn]))
  5046. return 0;
  5047. /*
  5048. * If the parent's context is a clone, pin it so it won't get
  5049. * swapped under us.
  5050. */
  5051. parent_ctx = perf_pin_task_context(parent, ctxn);
  5052. /*
  5053. * No need to check if parent_ctx != NULL here; since we saw
  5054. * it non-NULL earlier, the only reason for it to become NULL
  5055. * is if we exit, and since we're currently in the middle of
  5056. * a fork we can't be exiting at the same time.
  5057. */
  5058. /*
  5059. * Lock the parent list. No need to lock the child - not PID
  5060. * hashed yet and not running, so nobody can access it.
  5061. */
  5062. mutex_lock(&parent_ctx->mutex);
  5063. /*
  5064. * We dont have to disable NMIs - we are only looking at
  5065. * the list, not manipulating it:
  5066. */
  5067. list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
  5068. ret = inherit_task_group(event, parent, parent_ctx,
  5069. child, ctxn, &inherited_all);
  5070. if (ret)
  5071. break;
  5072. }
  5073. /*
  5074. * We can't hold ctx->lock when iterating the ->flexible_group list due
  5075. * to allocations, but we need to prevent rotation because
  5076. * rotate_ctx() will change the list from interrupt context.
  5077. */
  5078. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  5079. parent_ctx->rotate_disable = 1;
  5080. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  5081. list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
  5082. ret = inherit_task_group(event, parent, parent_ctx,
  5083. child, ctxn, &inherited_all);
  5084. if (ret)
  5085. break;
  5086. }
  5087. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  5088. parent_ctx->rotate_disable = 0;
  5089. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  5090. child_ctx = child->perf_event_ctxp[ctxn];
  5091. if (child_ctx && inherited_all) {
  5092. /*
  5093. * Mark the child context as a clone of the parent
  5094. * context, or of whatever the parent is a clone of.
  5095. * Note that if the parent is a clone, it could get
  5096. * uncloned at any point, but that doesn't matter
  5097. * because the list of events and the generation
  5098. * count can't have changed since we took the mutex.
  5099. */
  5100. cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
  5101. if (cloned_ctx) {
  5102. child_ctx->parent_ctx = cloned_ctx;
  5103. child_ctx->parent_gen = parent_ctx->parent_gen;
  5104. } else {
  5105. child_ctx->parent_ctx = parent_ctx;
  5106. child_ctx->parent_gen = parent_ctx->generation;
  5107. }
  5108. get_ctx(child_ctx->parent_ctx);
  5109. }
  5110. mutex_unlock(&parent_ctx->mutex);
  5111. perf_unpin_context(parent_ctx);
  5112. return ret;
  5113. }
  5114. /*
  5115. * Initialize the perf_event context in task_struct
  5116. */
  5117. int perf_event_init_task(struct task_struct *child)
  5118. {
  5119. int ctxn, ret;
  5120. for_each_task_context_nr(ctxn) {
  5121. ret = perf_event_init_context(child, ctxn);
  5122. if (ret)
  5123. return ret;
  5124. }
  5125. return 0;
  5126. }
  5127. static void __init perf_event_init_all_cpus(void)
  5128. {
  5129. struct swevent_htable *swhash;
  5130. int cpu;
  5131. for_each_possible_cpu(cpu) {
  5132. swhash = &per_cpu(swevent_htable, cpu);
  5133. mutex_init(&swhash->hlist_mutex);
  5134. INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
  5135. }
  5136. }
  5137. static void __cpuinit perf_event_init_cpu(int cpu)
  5138. {
  5139. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5140. mutex_lock(&swhash->hlist_mutex);
  5141. if (swhash->hlist_refcount > 0) {
  5142. struct swevent_hlist *hlist;
  5143. hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
  5144. WARN_ON(!hlist);
  5145. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  5146. }
  5147. mutex_unlock(&swhash->hlist_mutex);
  5148. }
  5149. #ifdef CONFIG_HOTPLUG_CPU
  5150. static void perf_pmu_rotate_stop(struct pmu *pmu)
  5151. {
  5152. struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  5153. WARN_ON(!irqs_disabled());
  5154. list_del_init(&cpuctx->rotation_list);
  5155. }
  5156. static void __perf_event_exit_context(void *__info)
  5157. {
  5158. struct perf_event_context *ctx = __info;
  5159. struct perf_event *event, *tmp;
  5160. perf_pmu_rotate_stop(ctx->pmu);
  5161. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
  5162. __perf_event_remove_from_context(event);
  5163. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
  5164. __perf_event_remove_from_context(event);
  5165. }
  5166. static void perf_event_exit_cpu_context(int cpu)
  5167. {
  5168. struct perf_event_context *ctx;
  5169. struct pmu *pmu;
  5170. int idx;
  5171. idx = srcu_read_lock(&pmus_srcu);
  5172. list_for_each_entry_rcu(pmu, &pmus, entry) {
  5173. ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
  5174. mutex_lock(&ctx->mutex);
  5175. smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
  5176. mutex_unlock(&ctx->mutex);
  5177. }
  5178. srcu_read_unlock(&pmus_srcu, idx);
  5179. }
  5180. static void perf_event_exit_cpu(int cpu)
  5181. {
  5182. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5183. mutex_lock(&swhash->hlist_mutex);
  5184. swevent_hlist_release(swhash);
  5185. mutex_unlock(&swhash->hlist_mutex);
  5186. perf_event_exit_cpu_context(cpu);
  5187. }
  5188. #else
  5189. static inline void perf_event_exit_cpu(int cpu) { }
  5190. #endif
  5191. static int __cpuinit
  5192. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  5193. {
  5194. unsigned int cpu = (long)hcpu;
  5195. switch (action & ~CPU_TASKS_FROZEN) {
  5196. case CPU_UP_PREPARE:
  5197. case CPU_DOWN_FAILED:
  5198. perf_event_init_cpu(cpu);
  5199. break;
  5200. case CPU_UP_CANCELED:
  5201. case CPU_DOWN_PREPARE:
  5202. perf_event_exit_cpu(cpu);
  5203. break;
  5204. default:
  5205. break;
  5206. }
  5207. return NOTIFY_OK;
  5208. }
  5209. void __init perf_event_init(void)
  5210. {
  5211. int ret;
  5212. perf_event_init_all_cpus();
  5213. init_srcu_struct(&pmus_srcu);
  5214. perf_pmu_register(&perf_swevent);
  5215. perf_pmu_register(&perf_cpu_clock);
  5216. perf_pmu_register(&perf_task_clock);
  5217. perf_tp_register();
  5218. perf_cpu_notifier(perf_cpu_notify);
  5219. ret = init_hw_breakpoint();
  5220. WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
  5221. }