perf_event.c 169 KB

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