perf_event.c 152 KB

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