perf_event.c 143 KB

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