perf_event.c 152 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723
  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. /* Reuse ptrace permission checks for now. */
  1823. err = -EACCES;
  1824. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  1825. goto errout;
  1826. return task;
  1827. errout:
  1828. put_task_struct(task);
  1829. return ERR_PTR(err);
  1830. }
  1831. static struct perf_event_context *
  1832. find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
  1833. {
  1834. struct perf_event_context *ctx;
  1835. struct perf_cpu_context *cpuctx;
  1836. unsigned long flags;
  1837. int ctxn, err;
  1838. if (!task) {
  1839. /* Must be root to operate on a CPU event: */
  1840. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  1841. return ERR_PTR(-EACCES);
  1842. /*
  1843. * We could be clever and allow to attach a event to an
  1844. * offline CPU and activate it when the CPU comes up, but
  1845. * that's for later.
  1846. */
  1847. if (!cpu_online(cpu))
  1848. return ERR_PTR(-ENODEV);
  1849. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  1850. ctx = &cpuctx->ctx;
  1851. get_ctx(ctx);
  1852. return ctx;
  1853. }
  1854. err = -EINVAL;
  1855. ctxn = pmu->task_ctx_nr;
  1856. if (ctxn < 0)
  1857. goto errout;
  1858. retry:
  1859. ctx = perf_lock_task_context(task, ctxn, &flags);
  1860. if (ctx) {
  1861. unclone_ctx(ctx);
  1862. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1863. }
  1864. if (!ctx) {
  1865. ctx = alloc_perf_context(pmu, task);
  1866. err = -ENOMEM;
  1867. if (!ctx)
  1868. goto errout;
  1869. get_ctx(ctx);
  1870. err = 0;
  1871. mutex_lock(&task->perf_event_mutex);
  1872. /*
  1873. * If it has already passed perf_event_exit_task().
  1874. * we must see PF_EXITING, it takes this mutex too.
  1875. */
  1876. if (task->flags & PF_EXITING)
  1877. err = -ESRCH;
  1878. else if (task->perf_event_ctxp[ctxn])
  1879. err = -EAGAIN;
  1880. else
  1881. rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
  1882. mutex_unlock(&task->perf_event_mutex);
  1883. if (unlikely(err)) {
  1884. put_task_struct(task);
  1885. kfree(ctx);
  1886. if (err == -EAGAIN)
  1887. goto retry;
  1888. goto errout;
  1889. }
  1890. }
  1891. return ctx;
  1892. errout:
  1893. return ERR_PTR(err);
  1894. }
  1895. static void perf_event_free_filter(struct perf_event *event);
  1896. static void free_event_rcu(struct rcu_head *head)
  1897. {
  1898. struct perf_event *event;
  1899. event = container_of(head, struct perf_event, rcu_head);
  1900. if (event->ns)
  1901. put_pid_ns(event->ns);
  1902. perf_event_free_filter(event);
  1903. kfree(event);
  1904. }
  1905. static void perf_buffer_put(struct perf_buffer *buffer);
  1906. static void free_event(struct perf_event *event)
  1907. {
  1908. irq_work_sync(&event->pending);
  1909. if (!event->parent) {
  1910. if (event->attach_state & PERF_ATTACH_TASK)
  1911. jump_label_dec(&perf_task_events);
  1912. if (event->attr.mmap || event->attr.mmap_data)
  1913. atomic_dec(&nr_mmap_events);
  1914. if (event->attr.comm)
  1915. atomic_dec(&nr_comm_events);
  1916. if (event->attr.task)
  1917. atomic_dec(&nr_task_events);
  1918. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
  1919. put_callchain_buffers();
  1920. }
  1921. if (event->buffer) {
  1922. perf_buffer_put(event->buffer);
  1923. event->buffer = NULL;
  1924. }
  1925. if (event->destroy)
  1926. event->destroy(event);
  1927. if (event->ctx)
  1928. put_ctx(event->ctx);
  1929. call_rcu(&event->rcu_head, free_event_rcu);
  1930. }
  1931. int perf_event_release_kernel(struct perf_event *event)
  1932. {
  1933. struct perf_event_context *ctx = event->ctx;
  1934. /*
  1935. * Remove from the PMU, can't get re-enabled since we got
  1936. * here because the last ref went.
  1937. */
  1938. perf_event_disable(event);
  1939. WARN_ON_ONCE(ctx->parent_ctx);
  1940. /*
  1941. * There are two ways this annotation is useful:
  1942. *
  1943. * 1) there is a lock recursion from perf_event_exit_task
  1944. * see the comment there.
  1945. *
  1946. * 2) there is a lock-inversion with mmap_sem through
  1947. * perf_event_read_group(), which takes faults while
  1948. * holding ctx->mutex, however this is called after
  1949. * the last filedesc died, so there is no possibility
  1950. * to trigger the AB-BA case.
  1951. */
  1952. mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
  1953. raw_spin_lock_irq(&ctx->lock);
  1954. perf_group_detach(event);
  1955. list_del_event(event, ctx);
  1956. raw_spin_unlock_irq(&ctx->lock);
  1957. mutex_unlock(&ctx->mutex);
  1958. free_event(event);
  1959. return 0;
  1960. }
  1961. EXPORT_SYMBOL_GPL(perf_event_release_kernel);
  1962. /*
  1963. * Called when the last reference to the file is gone.
  1964. */
  1965. static int perf_release(struct inode *inode, struct file *file)
  1966. {
  1967. struct perf_event *event = file->private_data;
  1968. struct task_struct *owner;
  1969. file->private_data = NULL;
  1970. rcu_read_lock();
  1971. owner = ACCESS_ONCE(event->owner);
  1972. /*
  1973. * Matches the smp_wmb() in perf_event_exit_task(). If we observe
  1974. * !owner it means the list deletion is complete and we can indeed
  1975. * free this event, otherwise we need to serialize on
  1976. * owner->perf_event_mutex.
  1977. */
  1978. smp_read_barrier_depends();
  1979. if (owner) {
  1980. /*
  1981. * Since delayed_put_task_struct() also drops the last
  1982. * task reference we can safely take a new reference
  1983. * while holding the rcu_read_lock().
  1984. */
  1985. get_task_struct(owner);
  1986. }
  1987. rcu_read_unlock();
  1988. if (owner) {
  1989. mutex_lock(&owner->perf_event_mutex);
  1990. /*
  1991. * We have to re-check the event->owner field, if it is cleared
  1992. * we raced with perf_event_exit_task(), acquiring the mutex
  1993. * ensured they're done, and we can proceed with freeing the
  1994. * event.
  1995. */
  1996. if (event->owner)
  1997. list_del_init(&event->owner_entry);
  1998. mutex_unlock(&owner->perf_event_mutex);
  1999. put_task_struct(owner);
  2000. }
  2001. return perf_event_release_kernel(event);
  2002. }
  2003. u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
  2004. {
  2005. struct perf_event *child;
  2006. u64 total = 0;
  2007. *enabled = 0;
  2008. *running = 0;
  2009. mutex_lock(&event->child_mutex);
  2010. total += perf_event_read(event);
  2011. *enabled += event->total_time_enabled +
  2012. atomic64_read(&event->child_total_time_enabled);
  2013. *running += event->total_time_running +
  2014. atomic64_read(&event->child_total_time_running);
  2015. list_for_each_entry(child, &event->child_list, child_list) {
  2016. total += perf_event_read(child);
  2017. *enabled += child->total_time_enabled;
  2018. *running += child->total_time_running;
  2019. }
  2020. mutex_unlock(&event->child_mutex);
  2021. return total;
  2022. }
  2023. EXPORT_SYMBOL_GPL(perf_event_read_value);
  2024. static int perf_event_read_group(struct perf_event *event,
  2025. u64 read_format, char __user *buf)
  2026. {
  2027. struct perf_event *leader = event->group_leader, *sub;
  2028. int n = 0, size = 0, ret = -EFAULT;
  2029. struct perf_event_context *ctx = leader->ctx;
  2030. u64 values[5];
  2031. u64 count, enabled, running;
  2032. mutex_lock(&ctx->mutex);
  2033. count = perf_event_read_value(leader, &enabled, &running);
  2034. values[n++] = 1 + leader->nr_siblings;
  2035. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2036. values[n++] = enabled;
  2037. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2038. values[n++] = running;
  2039. values[n++] = count;
  2040. if (read_format & PERF_FORMAT_ID)
  2041. values[n++] = primary_event_id(leader);
  2042. size = n * sizeof(u64);
  2043. if (copy_to_user(buf, values, size))
  2044. goto unlock;
  2045. ret = size;
  2046. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  2047. n = 0;
  2048. values[n++] = perf_event_read_value(sub, &enabled, &running);
  2049. if (read_format & PERF_FORMAT_ID)
  2050. values[n++] = primary_event_id(sub);
  2051. size = n * sizeof(u64);
  2052. if (copy_to_user(buf + ret, values, size)) {
  2053. ret = -EFAULT;
  2054. goto unlock;
  2055. }
  2056. ret += size;
  2057. }
  2058. unlock:
  2059. mutex_unlock(&ctx->mutex);
  2060. return ret;
  2061. }
  2062. static int perf_event_read_one(struct perf_event *event,
  2063. u64 read_format, char __user *buf)
  2064. {
  2065. u64 enabled, running;
  2066. u64 values[4];
  2067. int n = 0;
  2068. values[n++] = perf_event_read_value(event, &enabled, &running);
  2069. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2070. values[n++] = enabled;
  2071. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2072. values[n++] = running;
  2073. if (read_format & PERF_FORMAT_ID)
  2074. values[n++] = primary_event_id(event);
  2075. if (copy_to_user(buf, values, n * sizeof(u64)))
  2076. return -EFAULT;
  2077. return n * sizeof(u64);
  2078. }
  2079. /*
  2080. * Read the performance event - simple non blocking version for now
  2081. */
  2082. static ssize_t
  2083. perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
  2084. {
  2085. u64 read_format = event->attr.read_format;
  2086. int ret;
  2087. /*
  2088. * Return end-of-file for a read on a event that is in
  2089. * error state (i.e. because it was pinned but it couldn't be
  2090. * scheduled on to the CPU at some point).
  2091. */
  2092. if (event->state == PERF_EVENT_STATE_ERROR)
  2093. return 0;
  2094. if (count < event->read_size)
  2095. return -ENOSPC;
  2096. WARN_ON_ONCE(event->ctx->parent_ctx);
  2097. if (read_format & PERF_FORMAT_GROUP)
  2098. ret = perf_event_read_group(event, read_format, buf);
  2099. else
  2100. ret = perf_event_read_one(event, read_format, buf);
  2101. return ret;
  2102. }
  2103. static ssize_t
  2104. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  2105. {
  2106. struct perf_event *event = file->private_data;
  2107. return perf_read_hw(event, buf, count);
  2108. }
  2109. static unsigned int perf_poll(struct file *file, poll_table *wait)
  2110. {
  2111. struct perf_event *event = file->private_data;
  2112. struct perf_buffer *buffer;
  2113. unsigned int events = POLL_HUP;
  2114. rcu_read_lock();
  2115. buffer = rcu_dereference(event->buffer);
  2116. if (buffer)
  2117. events = atomic_xchg(&buffer->poll, 0);
  2118. rcu_read_unlock();
  2119. poll_wait(file, &event->waitq, wait);
  2120. return events;
  2121. }
  2122. static void perf_event_reset(struct perf_event *event)
  2123. {
  2124. (void)perf_event_read(event);
  2125. local64_set(&event->count, 0);
  2126. perf_event_update_userpage(event);
  2127. }
  2128. /*
  2129. * Holding the top-level event's child_mutex means that any
  2130. * descendant process that has inherited this event will block
  2131. * in sync_child_event if it goes to exit, thus satisfying the
  2132. * task existence requirements of perf_event_enable/disable.
  2133. */
  2134. static void perf_event_for_each_child(struct perf_event *event,
  2135. void (*func)(struct perf_event *))
  2136. {
  2137. struct perf_event *child;
  2138. WARN_ON_ONCE(event->ctx->parent_ctx);
  2139. mutex_lock(&event->child_mutex);
  2140. func(event);
  2141. list_for_each_entry(child, &event->child_list, child_list)
  2142. func(child);
  2143. mutex_unlock(&event->child_mutex);
  2144. }
  2145. static void perf_event_for_each(struct perf_event *event,
  2146. void (*func)(struct perf_event *))
  2147. {
  2148. struct perf_event_context *ctx = event->ctx;
  2149. struct perf_event *sibling;
  2150. WARN_ON_ONCE(ctx->parent_ctx);
  2151. mutex_lock(&ctx->mutex);
  2152. event = event->group_leader;
  2153. perf_event_for_each_child(event, func);
  2154. func(event);
  2155. list_for_each_entry(sibling, &event->sibling_list, group_entry)
  2156. perf_event_for_each_child(event, func);
  2157. mutex_unlock(&ctx->mutex);
  2158. }
  2159. static int perf_event_period(struct perf_event *event, u64 __user *arg)
  2160. {
  2161. struct perf_event_context *ctx = event->ctx;
  2162. int ret = 0;
  2163. u64 value;
  2164. if (!is_sampling_event(event))
  2165. return -EINVAL;
  2166. if (copy_from_user(&value, arg, sizeof(value)))
  2167. return -EFAULT;
  2168. if (!value)
  2169. return -EINVAL;
  2170. raw_spin_lock_irq(&ctx->lock);
  2171. if (event->attr.freq) {
  2172. if (value > sysctl_perf_event_sample_rate) {
  2173. ret = -EINVAL;
  2174. goto unlock;
  2175. }
  2176. event->attr.sample_freq = value;
  2177. } else {
  2178. event->attr.sample_period = value;
  2179. event->hw.sample_period = value;
  2180. }
  2181. unlock:
  2182. raw_spin_unlock_irq(&ctx->lock);
  2183. return ret;
  2184. }
  2185. static const struct file_operations perf_fops;
  2186. static struct perf_event *perf_fget_light(int fd, int *fput_needed)
  2187. {
  2188. struct file *file;
  2189. file = fget_light(fd, fput_needed);
  2190. if (!file)
  2191. return ERR_PTR(-EBADF);
  2192. if (file->f_op != &perf_fops) {
  2193. fput_light(file, *fput_needed);
  2194. *fput_needed = 0;
  2195. return ERR_PTR(-EBADF);
  2196. }
  2197. return file->private_data;
  2198. }
  2199. static int perf_event_set_output(struct perf_event *event,
  2200. struct perf_event *output_event);
  2201. static int perf_event_set_filter(struct perf_event *event, void __user *arg);
  2202. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2203. {
  2204. struct perf_event *event = file->private_data;
  2205. void (*func)(struct perf_event *);
  2206. u32 flags = arg;
  2207. switch (cmd) {
  2208. case PERF_EVENT_IOC_ENABLE:
  2209. func = perf_event_enable;
  2210. break;
  2211. case PERF_EVENT_IOC_DISABLE:
  2212. func = perf_event_disable;
  2213. break;
  2214. case PERF_EVENT_IOC_RESET:
  2215. func = perf_event_reset;
  2216. break;
  2217. case PERF_EVENT_IOC_REFRESH:
  2218. return perf_event_refresh(event, arg);
  2219. case PERF_EVENT_IOC_PERIOD:
  2220. return perf_event_period(event, (u64 __user *)arg);
  2221. case PERF_EVENT_IOC_SET_OUTPUT:
  2222. {
  2223. struct perf_event *output_event = NULL;
  2224. int fput_needed = 0;
  2225. int ret;
  2226. if (arg != -1) {
  2227. output_event = perf_fget_light(arg, &fput_needed);
  2228. if (IS_ERR(output_event))
  2229. return PTR_ERR(output_event);
  2230. }
  2231. ret = perf_event_set_output(event, output_event);
  2232. if (output_event)
  2233. fput_light(output_event->filp, fput_needed);
  2234. return ret;
  2235. }
  2236. case PERF_EVENT_IOC_SET_FILTER:
  2237. return perf_event_set_filter(event, (void __user *)arg);
  2238. default:
  2239. return -ENOTTY;
  2240. }
  2241. if (flags & PERF_IOC_FLAG_GROUP)
  2242. perf_event_for_each(event, func);
  2243. else
  2244. perf_event_for_each_child(event, func);
  2245. return 0;
  2246. }
  2247. int perf_event_task_enable(void)
  2248. {
  2249. struct perf_event *event;
  2250. mutex_lock(&current->perf_event_mutex);
  2251. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  2252. perf_event_for_each_child(event, perf_event_enable);
  2253. mutex_unlock(&current->perf_event_mutex);
  2254. return 0;
  2255. }
  2256. int perf_event_task_disable(void)
  2257. {
  2258. struct perf_event *event;
  2259. mutex_lock(&current->perf_event_mutex);
  2260. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  2261. perf_event_for_each_child(event, perf_event_disable);
  2262. mutex_unlock(&current->perf_event_mutex);
  2263. return 0;
  2264. }
  2265. #ifndef PERF_EVENT_INDEX_OFFSET
  2266. # define PERF_EVENT_INDEX_OFFSET 0
  2267. #endif
  2268. static int perf_event_index(struct perf_event *event)
  2269. {
  2270. if (event->hw.state & PERF_HES_STOPPED)
  2271. return 0;
  2272. if (event->state != PERF_EVENT_STATE_ACTIVE)
  2273. return 0;
  2274. return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
  2275. }
  2276. /*
  2277. * Callers need to ensure there can be no nesting of this function, otherwise
  2278. * the seqlock logic goes bad. We can not serialize this because the arch
  2279. * code calls this from NMI context.
  2280. */
  2281. void perf_event_update_userpage(struct perf_event *event)
  2282. {
  2283. struct perf_event_mmap_page *userpg;
  2284. struct perf_buffer *buffer;
  2285. rcu_read_lock();
  2286. buffer = rcu_dereference(event->buffer);
  2287. if (!buffer)
  2288. goto unlock;
  2289. userpg = buffer->user_page;
  2290. /*
  2291. * Disable preemption so as to not let the corresponding user-space
  2292. * spin too long if we get preempted.
  2293. */
  2294. preempt_disable();
  2295. ++userpg->lock;
  2296. barrier();
  2297. userpg->index = perf_event_index(event);
  2298. userpg->offset = perf_event_count(event);
  2299. if (event->state == PERF_EVENT_STATE_ACTIVE)
  2300. userpg->offset -= local64_read(&event->hw.prev_count);
  2301. userpg->time_enabled = event->total_time_enabled +
  2302. atomic64_read(&event->child_total_time_enabled);
  2303. userpg->time_running = event->total_time_running +
  2304. atomic64_read(&event->child_total_time_running);
  2305. barrier();
  2306. ++userpg->lock;
  2307. preempt_enable();
  2308. unlock:
  2309. rcu_read_unlock();
  2310. }
  2311. static unsigned long perf_data_size(struct perf_buffer *buffer);
  2312. static void
  2313. perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
  2314. {
  2315. long max_size = perf_data_size(buffer);
  2316. if (watermark)
  2317. buffer->watermark = min(max_size, watermark);
  2318. if (!buffer->watermark)
  2319. buffer->watermark = max_size / 2;
  2320. if (flags & PERF_BUFFER_WRITABLE)
  2321. buffer->writable = 1;
  2322. atomic_set(&buffer->refcount, 1);
  2323. }
  2324. #ifndef CONFIG_PERF_USE_VMALLOC
  2325. /*
  2326. * Back perf_mmap() with regular GFP_KERNEL-0 pages.
  2327. */
  2328. static struct page *
  2329. perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
  2330. {
  2331. if (pgoff > buffer->nr_pages)
  2332. return NULL;
  2333. if (pgoff == 0)
  2334. return virt_to_page(buffer->user_page);
  2335. return virt_to_page(buffer->data_pages[pgoff - 1]);
  2336. }
  2337. static void *perf_mmap_alloc_page(int cpu)
  2338. {
  2339. struct page *page;
  2340. int node;
  2341. node = (cpu == -1) ? cpu : cpu_to_node(cpu);
  2342. page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  2343. if (!page)
  2344. return NULL;
  2345. return page_address(page);
  2346. }
  2347. static struct perf_buffer *
  2348. perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
  2349. {
  2350. struct perf_buffer *buffer;
  2351. unsigned long size;
  2352. int i;
  2353. size = sizeof(struct perf_buffer);
  2354. size += nr_pages * sizeof(void *);
  2355. buffer = kzalloc(size, GFP_KERNEL);
  2356. if (!buffer)
  2357. goto fail;
  2358. buffer->user_page = perf_mmap_alloc_page(cpu);
  2359. if (!buffer->user_page)
  2360. goto fail_user_page;
  2361. for (i = 0; i < nr_pages; i++) {
  2362. buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
  2363. if (!buffer->data_pages[i])
  2364. goto fail_data_pages;
  2365. }
  2366. buffer->nr_pages = nr_pages;
  2367. perf_buffer_init(buffer, watermark, flags);
  2368. return buffer;
  2369. fail_data_pages:
  2370. for (i--; i >= 0; i--)
  2371. free_page((unsigned long)buffer->data_pages[i]);
  2372. free_page((unsigned long)buffer->user_page);
  2373. fail_user_page:
  2374. kfree(buffer);
  2375. fail:
  2376. return NULL;
  2377. }
  2378. static void perf_mmap_free_page(unsigned long addr)
  2379. {
  2380. struct page *page = virt_to_page((void *)addr);
  2381. page->mapping = NULL;
  2382. __free_page(page);
  2383. }
  2384. static void perf_buffer_free(struct perf_buffer *buffer)
  2385. {
  2386. int i;
  2387. perf_mmap_free_page((unsigned long)buffer->user_page);
  2388. for (i = 0; i < buffer->nr_pages; i++)
  2389. perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
  2390. kfree(buffer);
  2391. }
  2392. static inline int page_order(struct perf_buffer *buffer)
  2393. {
  2394. return 0;
  2395. }
  2396. #else
  2397. /*
  2398. * Back perf_mmap() with vmalloc memory.
  2399. *
  2400. * Required for architectures that have d-cache aliasing issues.
  2401. */
  2402. static inline int page_order(struct perf_buffer *buffer)
  2403. {
  2404. return buffer->page_order;
  2405. }
  2406. static struct page *
  2407. perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
  2408. {
  2409. if (pgoff > (1UL << page_order(buffer)))
  2410. return NULL;
  2411. return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
  2412. }
  2413. static void perf_mmap_unmark_page(void *addr)
  2414. {
  2415. struct page *page = vmalloc_to_page(addr);
  2416. page->mapping = NULL;
  2417. }
  2418. static void perf_buffer_free_work(struct work_struct *work)
  2419. {
  2420. struct perf_buffer *buffer;
  2421. void *base;
  2422. int i, nr;
  2423. buffer = container_of(work, struct perf_buffer, work);
  2424. nr = 1 << page_order(buffer);
  2425. base = buffer->user_page;
  2426. for (i = 0; i < nr + 1; i++)
  2427. perf_mmap_unmark_page(base + (i * PAGE_SIZE));
  2428. vfree(base);
  2429. kfree(buffer);
  2430. }
  2431. static void perf_buffer_free(struct perf_buffer *buffer)
  2432. {
  2433. schedule_work(&buffer->work);
  2434. }
  2435. static struct perf_buffer *
  2436. perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
  2437. {
  2438. struct perf_buffer *buffer;
  2439. unsigned long size;
  2440. void *all_buf;
  2441. size = sizeof(struct perf_buffer);
  2442. size += sizeof(void *);
  2443. buffer = kzalloc(size, GFP_KERNEL);
  2444. if (!buffer)
  2445. goto fail;
  2446. INIT_WORK(&buffer->work, perf_buffer_free_work);
  2447. all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
  2448. if (!all_buf)
  2449. goto fail_all_buf;
  2450. buffer->user_page = all_buf;
  2451. buffer->data_pages[0] = all_buf + PAGE_SIZE;
  2452. buffer->page_order = ilog2(nr_pages);
  2453. buffer->nr_pages = 1;
  2454. perf_buffer_init(buffer, watermark, flags);
  2455. return buffer;
  2456. fail_all_buf:
  2457. kfree(buffer);
  2458. fail:
  2459. return NULL;
  2460. }
  2461. #endif
  2462. static unsigned long perf_data_size(struct perf_buffer *buffer)
  2463. {
  2464. return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
  2465. }
  2466. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  2467. {
  2468. struct perf_event *event = vma->vm_file->private_data;
  2469. struct perf_buffer *buffer;
  2470. int ret = VM_FAULT_SIGBUS;
  2471. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  2472. if (vmf->pgoff == 0)
  2473. ret = 0;
  2474. return ret;
  2475. }
  2476. rcu_read_lock();
  2477. buffer = rcu_dereference(event->buffer);
  2478. if (!buffer)
  2479. goto unlock;
  2480. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  2481. goto unlock;
  2482. vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
  2483. if (!vmf->page)
  2484. goto unlock;
  2485. get_page(vmf->page);
  2486. vmf->page->mapping = vma->vm_file->f_mapping;
  2487. vmf->page->index = vmf->pgoff;
  2488. ret = 0;
  2489. unlock:
  2490. rcu_read_unlock();
  2491. return ret;
  2492. }
  2493. static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
  2494. {
  2495. struct perf_buffer *buffer;
  2496. buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
  2497. perf_buffer_free(buffer);
  2498. }
  2499. static struct perf_buffer *perf_buffer_get(struct perf_event *event)
  2500. {
  2501. struct perf_buffer *buffer;
  2502. rcu_read_lock();
  2503. buffer = rcu_dereference(event->buffer);
  2504. if (buffer) {
  2505. if (!atomic_inc_not_zero(&buffer->refcount))
  2506. buffer = NULL;
  2507. }
  2508. rcu_read_unlock();
  2509. return buffer;
  2510. }
  2511. static void perf_buffer_put(struct perf_buffer *buffer)
  2512. {
  2513. if (!atomic_dec_and_test(&buffer->refcount))
  2514. return;
  2515. call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
  2516. }
  2517. static void perf_mmap_open(struct vm_area_struct *vma)
  2518. {
  2519. struct perf_event *event = vma->vm_file->private_data;
  2520. atomic_inc(&event->mmap_count);
  2521. }
  2522. static void perf_mmap_close(struct vm_area_struct *vma)
  2523. {
  2524. struct perf_event *event = vma->vm_file->private_data;
  2525. if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
  2526. unsigned long size = perf_data_size(event->buffer);
  2527. struct user_struct *user = event->mmap_user;
  2528. struct perf_buffer *buffer = event->buffer;
  2529. atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
  2530. vma->vm_mm->locked_vm -= event->mmap_locked;
  2531. rcu_assign_pointer(event->buffer, NULL);
  2532. mutex_unlock(&event->mmap_mutex);
  2533. perf_buffer_put(buffer);
  2534. free_uid(user);
  2535. }
  2536. }
  2537. static const struct vm_operations_struct perf_mmap_vmops = {
  2538. .open = perf_mmap_open,
  2539. .close = perf_mmap_close,
  2540. .fault = perf_mmap_fault,
  2541. .page_mkwrite = perf_mmap_fault,
  2542. };
  2543. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  2544. {
  2545. struct perf_event *event = file->private_data;
  2546. unsigned long user_locked, user_lock_limit;
  2547. struct user_struct *user = current_user();
  2548. unsigned long locked, lock_limit;
  2549. struct perf_buffer *buffer;
  2550. unsigned long vma_size;
  2551. unsigned long nr_pages;
  2552. long user_extra, extra;
  2553. int ret = 0, flags = 0;
  2554. /*
  2555. * Don't allow mmap() of inherited per-task counters. This would
  2556. * create a performance issue due to all children writing to the
  2557. * same buffer.
  2558. */
  2559. if (event->cpu == -1 && event->attr.inherit)
  2560. return -EINVAL;
  2561. if (!(vma->vm_flags & VM_SHARED))
  2562. return -EINVAL;
  2563. vma_size = vma->vm_end - vma->vm_start;
  2564. nr_pages = (vma_size / PAGE_SIZE) - 1;
  2565. /*
  2566. * If we have buffer pages ensure they're a power-of-two number, so we
  2567. * can do bitmasks instead of modulo.
  2568. */
  2569. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  2570. return -EINVAL;
  2571. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  2572. return -EINVAL;
  2573. if (vma->vm_pgoff != 0)
  2574. return -EINVAL;
  2575. WARN_ON_ONCE(event->ctx->parent_ctx);
  2576. mutex_lock(&event->mmap_mutex);
  2577. if (event->buffer) {
  2578. if (event->buffer->nr_pages == nr_pages)
  2579. atomic_inc(&event->buffer->refcount);
  2580. else
  2581. ret = -EINVAL;
  2582. goto unlock;
  2583. }
  2584. user_extra = nr_pages + 1;
  2585. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  2586. /*
  2587. * Increase the limit linearly with more CPUs:
  2588. */
  2589. user_lock_limit *= num_online_cpus();
  2590. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  2591. extra = 0;
  2592. if (user_locked > user_lock_limit)
  2593. extra = user_locked - user_lock_limit;
  2594. lock_limit = rlimit(RLIMIT_MEMLOCK);
  2595. lock_limit >>= PAGE_SHIFT;
  2596. locked = vma->vm_mm->locked_vm + extra;
  2597. if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  2598. !capable(CAP_IPC_LOCK)) {
  2599. ret = -EPERM;
  2600. goto unlock;
  2601. }
  2602. WARN_ON(event->buffer);
  2603. if (vma->vm_flags & VM_WRITE)
  2604. flags |= PERF_BUFFER_WRITABLE;
  2605. buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
  2606. event->cpu, flags);
  2607. if (!buffer) {
  2608. ret = -ENOMEM;
  2609. goto unlock;
  2610. }
  2611. rcu_assign_pointer(event->buffer, buffer);
  2612. atomic_long_add(user_extra, &user->locked_vm);
  2613. event->mmap_locked = extra;
  2614. event->mmap_user = get_current_user();
  2615. vma->vm_mm->locked_vm += event->mmap_locked;
  2616. unlock:
  2617. if (!ret)
  2618. atomic_inc(&event->mmap_count);
  2619. mutex_unlock(&event->mmap_mutex);
  2620. vma->vm_flags |= VM_RESERVED;
  2621. vma->vm_ops = &perf_mmap_vmops;
  2622. return ret;
  2623. }
  2624. static int perf_fasync(int fd, struct file *filp, int on)
  2625. {
  2626. struct inode *inode = filp->f_path.dentry->d_inode;
  2627. struct perf_event *event = filp->private_data;
  2628. int retval;
  2629. mutex_lock(&inode->i_mutex);
  2630. retval = fasync_helper(fd, filp, on, &event->fasync);
  2631. mutex_unlock(&inode->i_mutex);
  2632. if (retval < 0)
  2633. return retval;
  2634. return 0;
  2635. }
  2636. static const struct file_operations perf_fops = {
  2637. .llseek = no_llseek,
  2638. .release = perf_release,
  2639. .read = perf_read,
  2640. .poll = perf_poll,
  2641. .unlocked_ioctl = perf_ioctl,
  2642. .compat_ioctl = perf_ioctl,
  2643. .mmap = perf_mmap,
  2644. .fasync = perf_fasync,
  2645. };
  2646. /*
  2647. * Perf event wakeup
  2648. *
  2649. * If there's data, ensure we set the poll() state and publish everything
  2650. * to user-space before waking everybody up.
  2651. */
  2652. void perf_event_wakeup(struct perf_event *event)
  2653. {
  2654. wake_up_all(&event->waitq);
  2655. if (event->pending_kill) {
  2656. kill_fasync(&event->fasync, SIGIO, event->pending_kill);
  2657. event->pending_kill = 0;
  2658. }
  2659. }
  2660. static void perf_pending_event(struct irq_work *entry)
  2661. {
  2662. struct perf_event *event = container_of(entry,
  2663. struct perf_event, pending);
  2664. if (event->pending_disable) {
  2665. event->pending_disable = 0;
  2666. __perf_event_disable(event);
  2667. }
  2668. if (event->pending_wakeup) {
  2669. event->pending_wakeup = 0;
  2670. perf_event_wakeup(event);
  2671. }
  2672. }
  2673. /*
  2674. * We assume there is only KVM supporting the callbacks.
  2675. * Later on, we might change it to a list if there is
  2676. * another virtualization implementation supporting the callbacks.
  2677. */
  2678. struct perf_guest_info_callbacks *perf_guest_cbs;
  2679. int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  2680. {
  2681. perf_guest_cbs = cbs;
  2682. return 0;
  2683. }
  2684. EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
  2685. int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  2686. {
  2687. perf_guest_cbs = NULL;
  2688. return 0;
  2689. }
  2690. EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
  2691. /*
  2692. * Output
  2693. */
  2694. static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
  2695. unsigned long offset, unsigned long head)
  2696. {
  2697. unsigned long mask;
  2698. if (!buffer->writable)
  2699. return true;
  2700. mask = perf_data_size(buffer) - 1;
  2701. offset = (offset - tail) & mask;
  2702. head = (head - tail) & mask;
  2703. if ((int)(head - offset) < 0)
  2704. return false;
  2705. return true;
  2706. }
  2707. static void perf_output_wakeup(struct perf_output_handle *handle)
  2708. {
  2709. atomic_set(&handle->buffer->poll, POLL_IN);
  2710. if (handle->nmi) {
  2711. handle->event->pending_wakeup = 1;
  2712. irq_work_queue(&handle->event->pending);
  2713. } else
  2714. perf_event_wakeup(handle->event);
  2715. }
  2716. /*
  2717. * We need to ensure a later event_id doesn't publish a head when a former
  2718. * event isn't done writing. However since we need to deal with NMIs we
  2719. * cannot fully serialize things.
  2720. *
  2721. * We only publish the head (and generate a wakeup) when the outer-most
  2722. * event completes.
  2723. */
  2724. static void perf_output_get_handle(struct perf_output_handle *handle)
  2725. {
  2726. struct perf_buffer *buffer = handle->buffer;
  2727. preempt_disable();
  2728. local_inc(&buffer->nest);
  2729. handle->wakeup = local_read(&buffer->wakeup);
  2730. }
  2731. static void perf_output_put_handle(struct perf_output_handle *handle)
  2732. {
  2733. struct perf_buffer *buffer = handle->buffer;
  2734. unsigned long head;
  2735. again:
  2736. head = local_read(&buffer->head);
  2737. /*
  2738. * IRQ/NMI can happen here, which means we can miss a head update.
  2739. */
  2740. if (!local_dec_and_test(&buffer->nest))
  2741. goto out;
  2742. /*
  2743. * Publish the known good head. Rely on the full barrier implied
  2744. * by atomic_dec_and_test() order the buffer->head read and this
  2745. * write.
  2746. */
  2747. buffer->user_page->data_head = head;
  2748. /*
  2749. * Now check if we missed an update, rely on the (compiler)
  2750. * barrier in atomic_dec_and_test() to re-read buffer->head.
  2751. */
  2752. if (unlikely(head != local_read(&buffer->head))) {
  2753. local_inc(&buffer->nest);
  2754. goto again;
  2755. }
  2756. if (handle->wakeup != local_read(&buffer->wakeup))
  2757. perf_output_wakeup(handle);
  2758. out:
  2759. preempt_enable();
  2760. }
  2761. __always_inline void perf_output_copy(struct perf_output_handle *handle,
  2762. const void *buf, unsigned int len)
  2763. {
  2764. do {
  2765. unsigned long size = min_t(unsigned long, handle->size, len);
  2766. memcpy(handle->addr, buf, size);
  2767. len -= size;
  2768. handle->addr += size;
  2769. buf += size;
  2770. handle->size -= size;
  2771. if (!handle->size) {
  2772. struct perf_buffer *buffer = handle->buffer;
  2773. handle->page++;
  2774. handle->page &= buffer->nr_pages - 1;
  2775. handle->addr = buffer->data_pages[handle->page];
  2776. handle->size = PAGE_SIZE << page_order(buffer);
  2777. }
  2778. } while (len);
  2779. }
  2780. static void __perf_event_header__init_id(struct perf_event_header *header,
  2781. struct perf_sample_data *data,
  2782. struct perf_event *event)
  2783. {
  2784. u64 sample_type = event->attr.sample_type;
  2785. data->type = sample_type;
  2786. header->size += event->id_header_size;
  2787. if (sample_type & PERF_SAMPLE_TID) {
  2788. /* namespace issues */
  2789. data->tid_entry.pid = perf_event_pid(event, current);
  2790. data->tid_entry.tid = perf_event_tid(event, current);
  2791. }
  2792. if (sample_type & PERF_SAMPLE_TIME)
  2793. data->time = perf_clock();
  2794. if (sample_type & PERF_SAMPLE_ID)
  2795. data->id = primary_event_id(event);
  2796. if (sample_type & PERF_SAMPLE_STREAM_ID)
  2797. data->stream_id = event->id;
  2798. if (sample_type & PERF_SAMPLE_CPU) {
  2799. data->cpu_entry.cpu = raw_smp_processor_id();
  2800. data->cpu_entry.reserved = 0;
  2801. }
  2802. }
  2803. static void perf_event_header__init_id(struct perf_event_header *header,
  2804. struct perf_sample_data *data,
  2805. struct perf_event *event)
  2806. {
  2807. if (event->attr.sample_id_all)
  2808. __perf_event_header__init_id(header, data, event);
  2809. }
  2810. static void __perf_event__output_id_sample(struct perf_output_handle *handle,
  2811. struct perf_sample_data *data)
  2812. {
  2813. u64 sample_type = data->type;
  2814. if (sample_type & PERF_SAMPLE_TID)
  2815. perf_output_put(handle, data->tid_entry);
  2816. if (sample_type & PERF_SAMPLE_TIME)
  2817. perf_output_put(handle, data->time);
  2818. if (sample_type & PERF_SAMPLE_ID)
  2819. perf_output_put(handle, data->id);
  2820. if (sample_type & PERF_SAMPLE_STREAM_ID)
  2821. perf_output_put(handle, data->stream_id);
  2822. if (sample_type & PERF_SAMPLE_CPU)
  2823. perf_output_put(handle, data->cpu_entry);
  2824. }
  2825. static void perf_event__output_id_sample(struct perf_event *event,
  2826. struct perf_output_handle *handle,
  2827. struct perf_sample_data *sample)
  2828. {
  2829. if (event->attr.sample_id_all)
  2830. __perf_event__output_id_sample(handle, sample);
  2831. }
  2832. int perf_output_begin(struct perf_output_handle *handle,
  2833. struct perf_event *event, unsigned int size,
  2834. int nmi, int sample)
  2835. {
  2836. struct perf_buffer *buffer;
  2837. unsigned long tail, offset, head;
  2838. int have_lost;
  2839. struct perf_sample_data sample_data;
  2840. struct {
  2841. struct perf_event_header header;
  2842. u64 id;
  2843. u64 lost;
  2844. } lost_event;
  2845. rcu_read_lock();
  2846. /*
  2847. * For inherited events we send all the output towards the parent.
  2848. */
  2849. if (event->parent)
  2850. event = event->parent;
  2851. buffer = rcu_dereference(event->buffer);
  2852. if (!buffer)
  2853. goto out;
  2854. handle->buffer = buffer;
  2855. handle->event = event;
  2856. handle->nmi = nmi;
  2857. handle->sample = sample;
  2858. if (!buffer->nr_pages)
  2859. goto out;
  2860. have_lost = local_read(&buffer->lost);
  2861. if (have_lost) {
  2862. lost_event.header.size = sizeof(lost_event);
  2863. perf_event_header__init_id(&lost_event.header, &sample_data,
  2864. event);
  2865. size += lost_event.header.size;
  2866. }
  2867. perf_output_get_handle(handle);
  2868. do {
  2869. /*
  2870. * Userspace could choose to issue a mb() before updating the
  2871. * tail pointer. So that all reads will be completed before the
  2872. * write is issued.
  2873. */
  2874. tail = ACCESS_ONCE(buffer->user_page->data_tail);
  2875. smp_rmb();
  2876. offset = head = local_read(&buffer->head);
  2877. head += size;
  2878. if (unlikely(!perf_output_space(buffer, tail, offset, head)))
  2879. goto fail;
  2880. } while (local_cmpxchg(&buffer->head, offset, head) != offset);
  2881. if (head - local_read(&buffer->wakeup) > buffer->watermark)
  2882. local_add(buffer->watermark, &buffer->wakeup);
  2883. handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
  2884. handle->page &= buffer->nr_pages - 1;
  2885. handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
  2886. handle->addr = buffer->data_pages[handle->page];
  2887. handle->addr += handle->size;
  2888. handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
  2889. if (have_lost) {
  2890. lost_event.header.type = PERF_RECORD_LOST;
  2891. lost_event.header.misc = 0;
  2892. lost_event.id = event->id;
  2893. lost_event.lost = local_xchg(&buffer->lost, 0);
  2894. perf_output_put(handle, lost_event);
  2895. perf_event__output_id_sample(event, handle, &sample_data);
  2896. }
  2897. return 0;
  2898. fail:
  2899. local_inc(&buffer->lost);
  2900. perf_output_put_handle(handle);
  2901. out:
  2902. rcu_read_unlock();
  2903. return -ENOSPC;
  2904. }
  2905. void perf_output_end(struct perf_output_handle *handle)
  2906. {
  2907. struct perf_event *event = handle->event;
  2908. struct perf_buffer *buffer = handle->buffer;
  2909. int wakeup_events = event->attr.wakeup_events;
  2910. if (handle->sample && wakeup_events) {
  2911. int events = local_inc_return(&buffer->events);
  2912. if (events >= wakeup_events) {
  2913. local_sub(wakeup_events, &buffer->events);
  2914. local_inc(&buffer->wakeup);
  2915. }
  2916. }
  2917. perf_output_put_handle(handle);
  2918. rcu_read_unlock();
  2919. }
  2920. static void perf_output_read_one(struct perf_output_handle *handle,
  2921. struct perf_event *event,
  2922. u64 enabled, u64 running)
  2923. {
  2924. u64 read_format = event->attr.read_format;
  2925. u64 values[4];
  2926. int n = 0;
  2927. values[n++] = perf_event_count(event);
  2928. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  2929. values[n++] = enabled +
  2930. atomic64_read(&event->child_total_time_enabled);
  2931. }
  2932. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  2933. values[n++] = running +
  2934. atomic64_read(&event->child_total_time_running);
  2935. }
  2936. if (read_format & PERF_FORMAT_ID)
  2937. values[n++] = primary_event_id(event);
  2938. perf_output_copy(handle, values, n * sizeof(u64));
  2939. }
  2940. /*
  2941. * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
  2942. */
  2943. static void perf_output_read_group(struct perf_output_handle *handle,
  2944. struct perf_event *event,
  2945. u64 enabled, u64 running)
  2946. {
  2947. struct perf_event *leader = event->group_leader, *sub;
  2948. u64 read_format = event->attr.read_format;
  2949. u64 values[5];
  2950. int n = 0;
  2951. values[n++] = 1 + leader->nr_siblings;
  2952. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2953. values[n++] = enabled;
  2954. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2955. values[n++] = running;
  2956. if (leader != event)
  2957. leader->pmu->read(leader);
  2958. values[n++] = perf_event_count(leader);
  2959. if (read_format & PERF_FORMAT_ID)
  2960. values[n++] = primary_event_id(leader);
  2961. perf_output_copy(handle, values, n * sizeof(u64));
  2962. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  2963. n = 0;
  2964. if (sub != event)
  2965. sub->pmu->read(sub);
  2966. values[n++] = perf_event_count(sub);
  2967. if (read_format & PERF_FORMAT_ID)
  2968. values[n++] = primary_event_id(sub);
  2969. perf_output_copy(handle, values, n * sizeof(u64));
  2970. }
  2971. }
  2972. #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
  2973. PERF_FORMAT_TOTAL_TIME_RUNNING)
  2974. static void perf_output_read(struct perf_output_handle *handle,
  2975. struct perf_event *event)
  2976. {
  2977. u64 enabled = 0, running = 0, now, ctx_time;
  2978. u64 read_format = event->attr.read_format;
  2979. /*
  2980. * compute total_time_enabled, total_time_running
  2981. * based on snapshot values taken when the event
  2982. * was last scheduled in.
  2983. *
  2984. * we cannot simply called update_context_time()
  2985. * because of locking issue as we are called in
  2986. * NMI context
  2987. */
  2988. if (read_format & PERF_FORMAT_TOTAL_TIMES) {
  2989. now = perf_clock();
  2990. ctx_time = event->shadow_ctx_time + now;
  2991. enabled = ctx_time - event->tstamp_enabled;
  2992. running = ctx_time - event->tstamp_running;
  2993. }
  2994. if (event->attr.read_format & PERF_FORMAT_GROUP)
  2995. perf_output_read_group(handle, event, enabled, running);
  2996. else
  2997. perf_output_read_one(handle, event, enabled, running);
  2998. }
  2999. void perf_output_sample(struct perf_output_handle *handle,
  3000. struct perf_event_header *header,
  3001. struct perf_sample_data *data,
  3002. struct perf_event *event)
  3003. {
  3004. u64 sample_type = data->type;
  3005. perf_output_put(handle, *header);
  3006. if (sample_type & PERF_SAMPLE_IP)
  3007. perf_output_put(handle, data->ip);
  3008. if (sample_type & PERF_SAMPLE_TID)
  3009. perf_output_put(handle, data->tid_entry);
  3010. if (sample_type & PERF_SAMPLE_TIME)
  3011. perf_output_put(handle, data->time);
  3012. if (sample_type & PERF_SAMPLE_ADDR)
  3013. perf_output_put(handle, data->addr);
  3014. if (sample_type & PERF_SAMPLE_ID)
  3015. perf_output_put(handle, data->id);
  3016. if (sample_type & PERF_SAMPLE_STREAM_ID)
  3017. perf_output_put(handle, data->stream_id);
  3018. if (sample_type & PERF_SAMPLE_CPU)
  3019. perf_output_put(handle, data->cpu_entry);
  3020. if (sample_type & PERF_SAMPLE_PERIOD)
  3021. perf_output_put(handle, data->period);
  3022. if (sample_type & PERF_SAMPLE_READ)
  3023. perf_output_read(handle, event);
  3024. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  3025. if (data->callchain) {
  3026. int size = 1;
  3027. if (data->callchain)
  3028. size += data->callchain->nr;
  3029. size *= sizeof(u64);
  3030. perf_output_copy(handle, data->callchain, size);
  3031. } else {
  3032. u64 nr = 0;
  3033. perf_output_put(handle, nr);
  3034. }
  3035. }
  3036. if (sample_type & PERF_SAMPLE_RAW) {
  3037. if (data->raw) {
  3038. perf_output_put(handle, data->raw->size);
  3039. perf_output_copy(handle, data->raw->data,
  3040. data->raw->size);
  3041. } else {
  3042. struct {
  3043. u32 size;
  3044. u32 data;
  3045. } raw = {
  3046. .size = sizeof(u32),
  3047. .data = 0,
  3048. };
  3049. perf_output_put(handle, raw);
  3050. }
  3051. }
  3052. }
  3053. void perf_prepare_sample(struct perf_event_header *header,
  3054. struct perf_sample_data *data,
  3055. struct perf_event *event,
  3056. struct pt_regs *regs)
  3057. {
  3058. u64 sample_type = event->attr.sample_type;
  3059. header->type = PERF_RECORD_SAMPLE;
  3060. header->size = sizeof(*header) + event->header_size;
  3061. header->misc = 0;
  3062. header->misc |= perf_misc_flags(regs);
  3063. __perf_event_header__init_id(header, data, event);
  3064. if (sample_type & PERF_SAMPLE_IP)
  3065. data->ip = perf_instruction_pointer(regs);
  3066. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  3067. int size = 1;
  3068. data->callchain = perf_callchain(regs);
  3069. if (data->callchain)
  3070. size += data->callchain->nr;
  3071. header->size += size * sizeof(u64);
  3072. }
  3073. if (sample_type & PERF_SAMPLE_RAW) {
  3074. int size = sizeof(u32);
  3075. if (data->raw)
  3076. size += data->raw->size;
  3077. else
  3078. size += sizeof(u32);
  3079. WARN_ON_ONCE(size & (sizeof(u64)-1));
  3080. header->size += size;
  3081. }
  3082. }
  3083. static void perf_event_output(struct perf_event *event, int nmi,
  3084. struct perf_sample_data *data,
  3085. struct pt_regs *regs)
  3086. {
  3087. struct perf_output_handle handle;
  3088. struct perf_event_header header;
  3089. /* protect the callchain buffers */
  3090. rcu_read_lock();
  3091. perf_prepare_sample(&header, data, event, regs);
  3092. if (perf_output_begin(&handle, event, header.size, nmi, 1))
  3093. goto exit;
  3094. perf_output_sample(&handle, &header, data, event);
  3095. perf_output_end(&handle);
  3096. exit:
  3097. rcu_read_unlock();
  3098. }
  3099. /*
  3100. * read event_id
  3101. */
  3102. struct perf_read_event {
  3103. struct perf_event_header header;
  3104. u32 pid;
  3105. u32 tid;
  3106. };
  3107. static void
  3108. perf_event_read_event(struct perf_event *event,
  3109. struct task_struct *task)
  3110. {
  3111. struct perf_output_handle handle;
  3112. struct perf_sample_data sample;
  3113. struct perf_read_event read_event = {
  3114. .header = {
  3115. .type = PERF_RECORD_READ,
  3116. .misc = 0,
  3117. .size = sizeof(read_event) + event->read_size,
  3118. },
  3119. .pid = perf_event_pid(event, task),
  3120. .tid = perf_event_tid(event, task),
  3121. };
  3122. int ret;
  3123. perf_event_header__init_id(&read_event.header, &sample, event);
  3124. ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
  3125. if (ret)
  3126. return;
  3127. perf_output_put(&handle, read_event);
  3128. perf_output_read(&handle, event);
  3129. perf_event__output_id_sample(event, &handle, &sample);
  3130. perf_output_end(&handle);
  3131. }
  3132. /*
  3133. * task tracking -- fork/exit
  3134. *
  3135. * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
  3136. */
  3137. struct perf_task_event {
  3138. struct task_struct *task;
  3139. struct perf_event_context *task_ctx;
  3140. struct {
  3141. struct perf_event_header header;
  3142. u32 pid;
  3143. u32 ppid;
  3144. u32 tid;
  3145. u32 ptid;
  3146. u64 time;
  3147. } event_id;
  3148. };
  3149. static void perf_event_task_output(struct perf_event *event,
  3150. struct perf_task_event *task_event)
  3151. {
  3152. struct perf_output_handle handle;
  3153. struct perf_sample_data sample;
  3154. struct task_struct *task = task_event->task;
  3155. int ret, size = task_event->event_id.header.size;
  3156. perf_event_header__init_id(&task_event->event_id.header, &sample, event);
  3157. ret = perf_output_begin(&handle, event,
  3158. task_event->event_id.header.size, 0, 0);
  3159. if (ret)
  3160. goto out;
  3161. task_event->event_id.pid = perf_event_pid(event, task);
  3162. task_event->event_id.ppid = perf_event_pid(event, current);
  3163. task_event->event_id.tid = perf_event_tid(event, task);
  3164. task_event->event_id.ptid = perf_event_tid(event, current);
  3165. perf_output_put(&handle, task_event->event_id);
  3166. perf_event__output_id_sample(event, &handle, &sample);
  3167. perf_output_end(&handle);
  3168. out:
  3169. task_event->event_id.header.size = size;
  3170. }
  3171. static int perf_event_task_match(struct perf_event *event)
  3172. {
  3173. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3174. return 0;
  3175. if (!event_filter_match(event))
  3176. return 0;
  3177. if (event->attr.comm || event->attr.mmap ||
  3178. event->attr.mmap_data || event->attr.task)
  3179. return 1;
  3180. return 0;
  3181. }
  3182. static void perf_event_task_ctx(struct perf_event_context *ctx,
  3183. struct perf_task_event *task_event)
  3184. {
  3185. struct perf_event *event;
  3186. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3187. if (perf_event_task_match(event))
  3188. perf_event_task_output(event, task_event);
  3189. }
  3190. }
  3191. static void perf_event_task_event(struct perf_task_event *task_event)
  3192. {
  3193. struct perf_cpu_context *cpuctx;
  3194. struct perf_event_context *ctx;
  3195. struct pmu *pmu;
  3196. int ctxn;
  3197. rcu_read_lock();
  3198. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3199. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3200. if (cpuctx->active_pmu != pmu)
  3201. goto next;
  3202. perf_event_task_ctx(&cpuctx->ctx, task_event);
  3203. ctx = task_event->task_ctx;
  3204. if (!ctx) {
  3205. ctxn = pmu->task_ctx_nr;
  3206. if (ctxn < 0)
  3207. goto next;
  3208. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3209. }
  3210. if (ctx)
  3211. perf_event_task_ctx(ctx, task_event);
  3212. next:
  3213. put_cpu_ptr(pmu->pmu_cpu_context);
  3214. }
  3215. rcu_read_unlock();
  3216. }
  3217. static void perf_event_task(struct task_struct *task,
  3218. struct perf_event_context *task_ctx,
  3219. int new)
  3220. {
  3221. struct perf_task_event task_event;
  3222. if (!atomic_read(&nr_comm_events) &&
  3223. !atomic_read(&nr_mmap_events) &&
  3224. !atomic_read(&nr_task_events))
  3225. return;
  3226. task_event = (struct perf_task_event){
  3227. .task = task,
  3228. .task_ctx = task_ctx,
  3229. .event_id = {
  3230. .header = {
  3231. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  3232. .misc = 0,
  3233. .size = sizeof(task_event.event_id),
  3234. },
  3235. /* .pid */
  3236. /* .ppid */
  3237. /* .tid */
  3238. /* .ptid */
  3239. .time = perf_clock(),
  3240. },
  3241. };
  3242. perf_event_task_event(&task_event);
  3243. }
  3244. void perf_event_fork(struct task_struct *task)
  3245. {
  3246. perf_event_task(task, NULL, 1);
  3247. }
  3248. /*
  3249. * comm tracking
  3250. */
  3251. struct perf_comm_event {
  3252. struct task_struct *task;
  3253. char *comm;
  3254. int comm_size;
  3255. struct {
  3256. struct perf_event_header header;
  3257. u32 pid;
  3258. u32 tid;
  3259. } event_id;
  3260. };
  3261. static void perf_event_comm_output(struct perf_event *event,
  3262. struct perf_comm_event *comm_event)
  3263. {
  3264. struct perf_output_handle handle;
  3265. struct perf_sample_data sample;
  3266. int size = comm_event->event_id.header.size;
  3267. int ret;
  3268. perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
  3269. ret = perf_output_begin(&handle, event,
  3270. comm_event->event_id.header.size, 0, 0);
  3271. if (ret)
  3272. goto out;
  3273. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  3274. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  3275. perf_output_put(&handle, comm_event->event_id);
  3276. perf_output_copy(&handle, comm_event->comm,
  3277. comm_event->comm_size);
  3278. perf_event__output_id_sample(event, &handle, &sample);
  3279. perf_output_end(&handle);
  3280. out:
  3281. comm_event->event_id.header.size = size;
  3282. }
  3283. static int perf_event_comm_match(struct perf_event *event)
  3284. {
  3285. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3286. return 0;
  3287. if (!event_filter_match(event))
  3288. return 0;
  3289. if (event->attr.comm)
  3290. return 1;
  3291. return 0;
  3292. }
  3293. static void perf_event_comm_ctx(struct perf_event_context *ctx,
  3294. struct perf_comm_event *comm_event)
  3295. {
  3296. struct perf_event *event;
  3297. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3298. if (perf_event_comm_match(event))
  3299. perf_event_comm_output(event, comm_event);
  3300. }
  3301. }
  3302. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  3303. {
  3304. struct perf_cpu_context *cpuctx;
  3305. struct perf_event_context *ctx;
  3306. char comm[TASK_COMM_LEN];
  3307. unsigned int size;
  3308. struct pmu *pmu;
  3309. int ctxn;
  3310. memset(comm, 0, sizeof(comm));
  3311. strlcpy(comm, comm_event->task->comm, sizeof(comm));
  3312. size = ALIGN(strlen(comm)+1, sizeof(u64));
  3313. comm_event->comm = comm;
  3314. comm_event->comm_size = size;
  3315. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  3316. rcu_read_lock();
  3317. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3318. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3319. if (cpuctx->active_pmu != pmu)
  3320. goto next;
  3321. perf_event_comm_ctx(&cpuctx->ctx, comm_event);
  3322. ctxn = pmu->task_ctx_nr;
  3323. if (ctxn < 0)
  3324. goto next;
  3325. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3326. if (ctx)
  3327. perf_event_comm_ctx(ctx, comm_event);
  3328. next:
  3329. put_cpu_ptr(pmu->pmu_cpu_context);
  3330. }
  3331. rcu_read_unlock();
  3332. }
  3333. void perf_event_comm(struct task_struct *task)
  3334. {
  3335. struct perf_comm_event comm_event;
  3336. struct perf_event_context *ctx;
  3337. int ctxn;
  3338. for_each_task_context_nr(ctxn) {
  3339. ctx = task->perf_event_ctxp[ctxn];
  3340. if (!ctx)
  3341. continue;
  3342. perf_event_enable_on_exec(ctx);
  3343. }
  3344. if (!atomic_read(&nr_comm_events))
  3345. return;
  3346. comm_event = (struct perf_comm_event){
  3347. .task = task,
  3348. /* .comm */
  3349. /* .comm_size */
  3350. .event_id = {
  3351. .header = {
  3352. .type = PERF_RECORD_COMM,
  3353. .misc = 0,
  3354. /* .size */
  3355. },
  3356. /* .pid */
  3357. /* .tid */
  3358. },
  3359. };
  3360. perf_event_comm_event(&comm_event);
  3361. }
  3362. /*
  3363. * mmap tracking
  3364. */
  3365. struct perf_mmap_event {
  3366. struct vm_area_struct *vma;
  3367. const char *file_name;
  3368. int file_size;
  3369. struct {
  3370. struct perf_event_header header;
  3371. u32 pid;
  3372. u32 tid;
  3373. u64 start;
  3374. u64 len;
  3375. u64 pgoff;
  3376. } event_id;
  3377. };
  3378. static void perf_event_mmap_output(struct perf_event *event,
  3379. struct perf_mmap_event *mmap_event)
  3380. {
  3381. struct perf_output_handle handle;
  3382. struct perf_sample_data sample;
  3383. int size = mmap_event->event_id.header.size;
  3384. int ret;
  3385. perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
  3386. ret = perf_output_begin(&handle, event,
  3387. mmap_event->event_id.header.size, 0, 0);
  3388. if (ret)
  3389. goto out;
  3390. mmap_event->event_id.pid = perf_event_pid(event, current);
  3391. mmap_event->event_id.tid = perf_event_tid(event, current);
  3392. perf_output_put(&handle, mmap_event->event_id);
  3393. perf_output_copy(&handle, mmap_event->file_name,
  3394. mmap_event->file_size);
  3395. perf_event__output_id_sample(event, &handle, &sample);
  3396. perf_output_end(&handle);
  3397. out:
  3398. mmap_event->event_id.header.size = size;
  3399. }
  3400. static int perf_event_mmap_match(struct perf_event *event,
  3401. struct perf_mmap_event *mmap_event,
  3402. int executable)
  3403. {
  3404. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3405. return 0;
  3406. if (!event_filter_match(event))
  3407. return 0;
  3408. if ((!executable && event->attr.mmap_data) ||
  3409. (executable && event->attr.mmap))
  3410. return 1;
  3411. return 0;
  3412. }
  3413. static void perf_event_mmap_ctx(struct perf_event_context *ctx,
  3414. struct perf_mmap_event *mmap_event,
  3415. int executable)
  3416. {
  3417. struct perf_event *event;
  3418. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3419. if (perf_event_mmap_match(event, mmap_event, executable))
  3420. perf_event_mmap_output(event, mmap_event);
  3421. }
  3422. }
  3423. static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
  3424. {
  3425. struct perf_cpu_context *cpuctx;
  3426. struct perf_event_context *ctx;
  3427. struct vm_area_struct *vma = mmap_event->vma;
  3428. struct file *file = vma->vm_file;
  3429. unsigned int size;
  3430. char tmp[16];
  3431. char *buf = NULL;
  3432. const char *name;
  3433. struct pmu *pmu;
  3434. int ctxn;
  3435. memset(tmp, 0, sizeof(tmp));
  3436. if (file) {
  3437. /*
  3438. * d_path works from the end of the buffer backwards, so we
  3439. * need to add enough zero bytes after the string to handle
  3440. * the 64bit alignment we do later.
  3441. */
  3442. buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
  3443. if (!buf) {
  3444. name = strncpy(tmp, "//enomem", sizeof(tmp));
  3445. goto got_name;
  3446. }
  3447. name = d_path(&file->f_path, buf, PATH_MAX);
  3448. if (IS_ERR(name)) {
  3449. name = strncpy(tmp, "//toolong", sizeof(tmp));
  3450. goto got_name;
  3451. }
  3452. } else {
  3453. if (arch_vma_name(mmap_event->vma)) {
  3454. name = strncpy(tmp, arch_vma_name(mmap_event->vma),
  3455. sizeof(tmp));
  3456. goto got_name;
  3457. }
  3458. if (!vma->vm_mm) {
  3459. name = strncpy(tmp, "[vdso]", sizeof(tmp));
  3460. goto got_name;
  3461. } else if (vma->vm_start <= vma->vm_mm->start_brk &&
  3462. vma->vm_end >= vma->vm_mm->brk) {
  3463. name = strncpy(tmp, "[heap]", sizeof(tmp));
  3464. goto got_name;
  3465. } else if (vma->vm_start <= vma->vm_mm->start_stack &&
  3466. vma->vm_end >= vma->vm_mm->start_stack) {
  3467. name = strncpy(tmp, "[stack]", sizeof(tmp));
  3468. goto got_name;
  3469. }
  3470. name = strncpy(tmp, "//anon", sizeof(tmp));
  3471. goto got_name;
  3472. }
  3473. got_name:
  3474. size = ALIGN(strlen(name)+1, sizeof(u64));
  3475. mmap_event->file_name = name;
  3476. mmap_event->file_size = size;
  3477. mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
  3478. rcu_read_lock();
  3479. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3480. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3481. if (cpuctx->active_pmu != pmu)
  3482. goto next;
  3483. perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
  3484. vma->vm_flags & VM_EXEC);
  3485. ctxn = pmu->task_ctx_nr;
  3486. if (ctxn < 0)
  3487. goto next;
  3488. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3489. if (ctx) {
  3490. perf_event_mmap_ctx(ctx, mmap_event,
  3491. vma->vm_flags & VM_EXEC);
  3492. }
  3493. next:
  3494. put_cpu_ptr(pmu->pmu_cpu_context);
  3495. }
  3496. rcu_read_unlock();
  3497. kfree(buf);
  3498. }
  3499. void perf_event_mmap(struct vm_area_struct *vma)
  3500. {
  3501. struct perf_mmap_event mmap_event;
  3502. if (!atomic_read(&nr_mmap_events))
  3503. return;
  3504. mmap_event = (struct perf_mmap_event){
  3505. .vma = vma,
  3506. /* .file_name */
  3507. /* .file_size */
  3508. .event_id = {
  3509. .header = {
  3510. .type = PERF_RECORD_MMAP,
  3511. .misc = PERF_RECORD_MISC_USER,
  3512. /* .size */
  3513. },
  3514. /* .pid */
  3515. /* .tid */
  3516. .start = vma->vm_start,
  3517. .len = vma->vm_end - vma->vm_start,
  3518. .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
  3519. },
  3520. };
  3521. perf_event_mmap_event(&mmap_event);
  3522. }
  3523. /*
  3524. * IRQ throttle logging
  3525. */
  3526. static void perf_log_throttle(struct perf_event *event, int enable)
  3527. {
  3528. struct perf_output_handle handle;
  3529. struct perf_sample_data sample;
  3530. int ret;
  3531. struct {
  3532. struct perf_event_header header;
  3533. u64 time;
  3534. u64 id;
  3535. u64 stream_id;
  3536. } throttle_event = {
  3537. .header = {
  3538. .type = PERF_RECORD_THROTTLE,
  3539. .misc = 0,
  3540. .size = sizeof(throttle_event),
  3541. },
  3542. .time = perf_clock(),
  3543. .id = primary_event_id(event),
  3544. .stream_id = event->id,
  3545. };
  3546. if (enable)
  3547. throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
  3548. perf_event_header__init_id(&throttle_event.header, &sample, event);
  3549. ret = perf_output_begin(&handle, event,
  3550. throttle_event.header.size, 1, 0);
  3551. if (ret)
  3552. return;
  3553. perf_output_put(&handle, throttle_event);
  3554. perf_event__output_id_sample(event, &handle, &sample);
  3555. perf_output_end(&handle);
  3556. }
  3557. /*
  3558. * Generic event overflow handling, sampling.
  3559. */
  3560. static int __perf_event_overflow(struct perf_event *event, int nmi,
  3561. int throttle, struct perf_sample_data *data,
  3562. struct pt_regs *regs)
  3563. {
  3564. int events = atomic_read(&event->event_limit);
  3565. struct hw_perf_event *hwc = &event->hw;
  3566. int ret = 0;
  3567. /*
  3568. * Non-sampling counters might still use the PMI to fold short
  3569. * hardware counters, ignore those.
  3570. */
  3571. if (unlikely(!is_sampling_event(event)))
  3572. return 0;
  3573. if (!throttle) {
  3574. hwc->interrupts++;
  3575. } else {
  3576. if (hwc->interrupts != MAX_INTERRUPTS) {
  3577. hwc->interrupts++;
  3578. if (HZ * hwc->interrupts >
  3579. (u64)sysctl_perf_event_sample_rate) {
  3580. hwc->interrupts = MAX_INTERRUPTS;
  3581. perf_log_throttle(event, 0);
  3582. ret = 1;
  3583. }
  3584. } else {
  3585. /*
  3586. * Keep re-disabling events even though on the previous
  3587. * pass we disabled it - just in case we raced with a
  3588. * sched-in and the event got enabled again:
  3589. */
  3590. ret = 1;
  3591. }
  3592. }
  3593. if (event->attr.freq) {
  3594. u64 now = perf_clock();
  3595. s64 delta = now - hwc->freq_time_stamp;
  3596. hwc->freq_time_stamp = now;
  3597. if (delta > 0 && delta < 2*TICK_NSEC)
  3598. perf_adjust_period(event, delta, hwc->last_period);
  3599. }
  3600. /*
  3601. * XXX event_limit might not quite work as expected on inherited
  3602. * events
  3603. */
  3604. event->pending_kill = POLL_IN;
  3605. if (events && atomic_dec_and_test(&event->event_limit)) {
  3606. ret = 1;
  3607. event->pending_kill = POLL_HUP;
  3608. if (nmi) {
  3609. event->pending_disable = 1;
  3610. irq_work_queue(&event->pending);
  3611. } else
  3612. perf_event_disable(event);
  3613. }
  3614. if (event->overflow_handler)
  3615. event->overflow_handler(event, nmi, data, regs);
  3616. else
  3617. perf_event_output(event, nmi, data, regs);
  3618. return ret;
  3619. }
  3620. int perf_event_overflow(struct perf_event *event, int nmi,
  3621. struct perf_sample_data *data,
  3622. struct pt_regs *regs)
  3623. {
  3624. return __perf_event_overflow(event, nmi, 1, data, regs);
  3625. }
  3626. /*
  3627. * Generic software event infrastructure
  3628. */
  3629. struct swevent_htable {
  3630. struct swevent_hlist *swevent_hlist;
  3631. struct mutex hlist_mutex;
  3632. int hlist_refcount;
  3633. /* Recursion avoidance in each contexts */
  3634. int recursion[PERF_NR_CONTEXTS];
  3635. };
  3636. static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
  3637. /*
  3638. * We directly increment event->count and keep a second value in
  3639. * event->hw.period_left to count intervals. This period event
  3640. * is kept in the range [-sample_period, 0] so that we can use the
  3641. * sign as trigger.
  3642. */
  3643. static u64 perf_swevent_set_period(struct perf_event *event)
  3644. {
  3645. struct hw_perf_event *hwc = &event->hw;
  3646. u64 period = hwc->last_period;
  3647. u64 nr, offset;
  3648. s64 old, val;
  3649. hwc->last_period = hwc->sample_period;
  3650. again:
  3651. old = val = local64_read(&hwc->period_left);
  3652. if (val < 0)
  3653. return 0;
  3654. nr = div64_u64(period + val, period);
  3655. offset = nr * period;
  3656. val -= offset;
  3657. if (local64_cmpxchg(&hwc->period_left, old, val) != old)
  3658. goto again;
  3659. return nr;
  3660. }
  3661. static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
  3662. int nmi, struct perf_sample_data *data,
  3663. struct pt_regs *regs)
  3664. {
  3665. struct hw_perf_event *hwc = &event->hw;
  3666. int throttle = 0;
  3667. data->period = event->hw.last_period;
  3668. if (!overflow)
  3669. overflow = perf_swevent_set_period(event);
  3670. if (hwc->interrupts == MAX_INTERRUPTS)
  3671. return;
  3672. for (; overflow; overflow--) {
  3673. if (__perf_event_overflow(event, nmi, throttle,
  3674. data, regs)) {
  3675. /*
  3676. * We inhibit the overflow from happening when
  3677. * hwc->interrupts == MAX_INTERRUPTS.
  3678. */
  3679. break;
  3680. }
  3681. throttle = 1;
  3682. }
  3683. }
  3684. static void perf_swevent_event(struct perf_event *event, u64 nr,
  3685. int nmi, struct perf_sample_data *data,
  3686. struct pt_regs *regs)
  3687. {
  3688. struct hw_perf_event *hwc = &event->hw;
  3689. local64_add(nr, &event->count);
  3690. if (!regs)
  3691. return;
  3692. if (!is_sampling_event(event))
  3693. return;
  3694. if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  3695. return perf_swevent_overflow(event, 1, nmi, data, regs);
  3696. if (local64_add_negative(nr, &hwc->period_left))
  3697. return;
  3698. perf_swevent_overflow(event, 0, nmi, data, regs);
  3699. }
  3700. static int perf_exclude_event(struct perf_event *event,
  3701. struct pt_regs *regs)
  3702. {
  3703. if (event->hw.state & PERF_HES_STOPPED)
  3704. return 0;
  3705. if (regs) {
  3706. if (event->attr.exclude_user && user_mode(regs))
  3707. return 1;
  3708. if (event->attr.exclude_kernel && !user_mode(regs))
  3709. return 1;
  3710. }
  3711. return 0;
  3712. }
  3713. static int perf_swevent_match(struct perf_event *event,
  3714. enum perf_type_id type,
  3715. u32 event_id,
  3716. struct perf_sample_data *data,
  3717. struct pt_regs *regs)
  3718. {
  3719. if (event->attr.type != type)
  3720. return 0;
  3721. if (event->attr.config != event_id)
  3722. return 0;
  3723. if (perf_exclude_event(event, regs))
  3724. return 0;
  3725. return 1;
  3726. }
  3727. static inline u64 swevent_hash(u64 type, u32 event_id)
  3728. {
  3729. u64 val = event_id | (type << 32);
  3730. return hash_64(val, SWEVENT_HLIST_BITS);
  3731. }
  3732. static inline struct hlist_head *
  3733. __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
  3734. {
  3735. u64 hash = swevent_hash(type, event_id);
  3736. return &hlist->heads[hash];
  3737. }
  3738. /* For the read side: events when they trigger */
  3739. static inline struct hlist_head *
  3740. find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
  3741. {
  3742. struct swevent_hlist *hlist;
  3743. hlist = rcu_dereference(swhash->swevent_hlist);
  3744. if (!hlist)
  3745. return NULL;
  3746. return __find_swevent_head(hlist, type, event_id);
  3747. }
  3748. /* For the event head insertion and removal in the hlist */
  3749. static inline struct hlist_head *
  3750. find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
  3751. {
  3752. struct swevent_hlist *hlist;
  3753. u32 event_id = event->attr.config;
  3754. u64 type = event->attr.type;
  3755. /*
  3756. * Event scheduling is always serialized against hlist allocation
  3757. * and release. Which makes the protected version suitable here.
  3758. * The context lock guarantees that.
  3759. */
  3760. hlist = rcu_dereference_protected(swhash->swevent_hlist,
  3761. lockdep_is_held(&event->ctx->lock));
  3762. if (!hlist)
  3763. return NULL;
  3764. return __find_swevent_head(hlist, type, event_id);
  3765. }
  3766. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  3767. u64 nr, int nmi,
  3768. struct perf_sample_data *data,
  3769. struct pt_regs *regs)
  3770. {
  3771. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3772. struct perf_event *event;
  3773. struct hlist_node *node;
  3774. struct hlist_head *head;
  3775. rcu_read_lock();
  3776. head = find_swevent_head_rcu(swhash, type, event_id);
  3777. if (!head)
  3778. goto end;
  3779. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  3780. if (perf_swevent_match(event, type, event_id, data, regs))
  3781. perf_swevent_event(event, nr, nmi, data, regs);
  3782. }
  3783. end:
  3784. rcu_read_unlock();
  3785. }
  3786. int perf_swevent_get_recursion_context(void)
  3787. {
  3788. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3789. return get_recursion_context(swhash->recursion);
  3790. }
  3791. EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
  3792. inline void perf_swevent_put_recursion_context(int rctx)
  3793. {
  3794. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3795. put_recursion_context(swhash->recursion, rctx);
  3796. }
  3797. void __perf_sw_event(u32 event_id, u64 nr, int nmi,
  3798. struct pt_regs *regs, u64 addr)
  3799. {
  3800. struct perf_sample_data data;
  3801. int rctx;
  3802. preempt_disable_notrace();
  3803. rctx = perf_swevent_get_recursion_context();
  3804. if (rctx < 0)
  3805. return;
  3806. perf_sample_data_init(&data, addr);
  3807. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
  3808. perf_swevent_put_recursion_context(rctx);
  3809. preempt_enable_notrace();
  3810. }
  3811. static void perf_swevent_read(struct perf_event *event)
  3812. {
  3813. }
  3814. static int perf_swevent_add(struct perf_event *event, int flags)
  3815. {
  3816. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3817. struct hw_perf_event *hwc = &event->hw;
  3818. struct hlist_head *head;
  3819. if (is_sampling_event(event)) {
  3820. hwc->last_period = hwc->sample_period;
  3821. perf_swevent_set_period(event);
  3822. }
  3823. hwc->state = !(flags & PERF_EF_START);
  3824. head = find_swevent_head(swhash, event);
  3825. if (WARN_ON_ONCE(!head))
  3826. return -EINVAL;
  3827. hlist_add_head_rcu(&event->hlist_entry, head);
  3828. return 0;
  3829. }
  3830. static void perf_swevent_del(struct perf_event *event, int flags)
  3831. {
  3832. hlist_del_rcu(&event->hlist_entry);
  3833. }
  3834. static void perf_swevent_start(struct perf_event *event, int flags)
  3835. {
  3836. event->hw.state = 0;
  3837. }
  3838. static void perf_swevent_stop(struct perf_event *event, int flags)
  3839. {
  3840. event->hw.state = PERF_HES_STOPPED;
  3841. }
  3842. /* Deref the hlist from the update side */
  3843. static inline struct swevent_hlist *
  3844. swevent_hlist_deref(struct swevent_htable *swhash)
  3845. {
  3846. return rcu_dereference_protected(swhash->swevent_hlist,
  3847. lockdep_is_held(&swhash->hlist_mutex));
  3848. }
  3849. static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
  3850. {
  3851. struct swevent_hlist *hlist;
  3852. hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
  3853. kfree(hlist);
  3854. }
  3855. static void swevent_hlist_release(struct swevent_htable *swhash)
  3856. {
  3857. struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
  3858. if (!hlist)
  3859. return;
  3860. rcu_assign_pointer(swhash->swevent_hlist, NULL);
  3861. call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
  3862. }
  3863. static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
  3864. {
  3865. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  3866. mutex_lock(&swhash->hlist_mutex);
  3867. if (!--swhash->hlist_refcount)
  3868. swevent_hlist_release(swhash);
  3869. mutex_unlock(&swhash->hlist_mutex);
  3870. }
  3871. static void swevent_hlist_put(struct perf_event *event)
  3872. {
  3873. int cpu;
  3874. if (event->cpu != -1) {
  3875. swevent_hlist_put_cpu(event, event->cpu);
  3876. return;
  3877. }
  3878. for_each_possible_cpu(cpu)
  3879. swevent_hlist_put_cpu(event, cpu);
  3880. }
  3881. static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
  3882. {
  3883. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  3884. int err = 0;
  3885. mutex_lock(&swhash->hlist_mutex);
  3886. if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
  3887. struct swevent_hlist *hlist;
  3888. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  3889. if (!hlist) {
  3890. err = -ENOMEM;
  3891. goto exit;
  3892. }
  3893. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  3894. }
  3895. swhash->hlist_refcount++;
  3896. exit:
  3897. mutex_unlock(&swhash->hlist_mutex);
  3898. return err;
  3899. }
  3900. static int swevent_hlist_get(struct perf_event *event)
  3901. {
  3902. int err;
  3903. int cpu, failed_cpu;
  3904. if (event->cpu != -1)
  3905. return swevent_hlist_get_cpu(event, event->cpu);
  3906. get_online_cpus();
  3907. for_each_possible_cpu(cpu) {
  3908. err = swevent_hlist_get_cpu(event, cpu);
  3909. if (err) {
  3910. failed_cpu = cpu;
  3911. goto fail;
  3912. }
  3913. }
  3914. put_online_cpus();
  3915. return 0;
  3916. fail:
  3917. for_each_possible_cpu(cpu) {
  3918. if (cpu == failed_cpu)
  3919. break;
  3920. swevent_hlist_put_cpu(event, cpu);
  3921. }
  3922. put_online_cpus();
  3923. return err;
  3924. }
  3925. atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
  3926. static void sw_perf_event_destroy(struct perf_event *event)
  3927. {
  3928. u64 event_id = event->attr.config;
  3929. WARN_ON(event->parent);
  3930. jump_label_dec(&perf_swevent_enabled[event_id]);
  3931. swevent_hlist_put(event);
  3932. }
  3933. static int perf_swevent_init(struct perf_event *event)
  3934. {
  3935. int event_id = event->attr.config;
  3936. if (event->attr.type != PERF_TYPE_SOFTWARE)
  3937. return -ENOENT;
  3938. switch (event_id) {
  3939. case PERF_COUNT_SW_CPU_CLOCK:
  3940. case PERF_COUNT_SW_TASK_CLOCK:
  3941. return -ENOENT;
  3942. default:
  3943. break;
  3944. }
  3945. if (event_id >= PERF_COUNT_SW_MAX)
  3946. return -ENOENT;
  3947. if (!event->parent) {
  3948. int err;
  3949. err = swevent_hlist_get(event);
  3950. if (err)
  3951. return err;
  3952. jump_label_inc(&perf_swevent_enabled[event_id]);
  3953. event->destroy = sw_perf_event_destroy;
  3954. }
  3955. return 0;
  3956. }
  3957. static struct pmu perf_swevent = {
  3958. .task_ctx_nr = perf_sw_context,
  3959. .event_init = perf_swevent_init,
  3960. .add = perf_swevent_add,
  3961. .del = perf_swevent_del,
  3962. .start = perf_swevent_start,
  3963. .stop = perf_swevent_stop,
  3964. .read = perf_swevent_read,
  3965. };
  3966. #ifdef CONFIG_EVENT_TRACING
  3967. static int perf_tp_filter_match(struct perf_event *event,
  3968. struct perf_sample_data *data)
  3969. {
  3970. void *record = data->raw->data;
  3971. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  3972. return 1;
  3973. return 0;
  3974. }
  3975. static int perf_tp_event_match(struct perf_event *event,
  3976. struct perf_sample_data *data,
  3977. struct pt_regs *regs)
  3978. {
  3979. /*
  3980. * All tracepoints are from kernel-space.
  3981. */
  3982. if (event->attr.exclude_kernel)
  3983. return 0;
  3984. if (!perf_tp_filter_match(event, data))
  3985. return 0;
  3986. return 1;
  3987. }
  3988. void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
  3989. struct pt_regs *regs, struct hlist_head *head, int rctx)
  3990. {
  3991. struct perf_sample_data data;
  3992. struct perf_event *event;
  3993. struct hlist_node *node;
  3994. struct perf_raw_record raw = {
  3995. .size = entry_size,
  3996. .data = record,
  3997. };
  3998. perf_sample_data_init(&data, addr);
  3999. data.raw = &raw;
  4000. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  4001. if (perf_tp_event_match(event, &data, regs))
  4002. perf_swevent_event(event, count, 1, &data, regs);
  4003. }
  4004. perf_swevent_put_recursion_context(rctx);
  4005. }
  4006. EXPORT_SYMBOL_GPL(perf_tp_event);
  4007. static void tp_perf_event_destroy(struct perf_event *event)
  4008. {
  4009. perf_trace_destroy(event);
  4010. }
  4011. static int perf_tp_event_init(struct perf_event *event)
  4012. {
  4013. int err;
  4014. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  4015. return -ENOENT;
  4016. err = perf_trace_init(event);
  4017. if (err)
  4018. return err;
  4019. event->destroy = tp_perf_event_destroy;
  4020. return 0;
  4021. }
  4022. static struct pmu perf_tracepoint = {
  4023. .task_ctx_nr = perf_sw_context,
  4024. .event_init = perf_tp_event_init,
  4025. .add = perf_trace_add,
  4026. .del = perf_trace_del,
  4027. .start = perf_swevent_start,
  4028. .stop = perf_swevent_stop,
  4029. .read = perf_swevent_read,
  4030. };
  4031. static inline void perf_tp_register(void)
  4032. {
  4033. perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
  4034. }
  4035. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  4036. {
  4037. char *filter_str;
  4038. int ret;
  4039. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  4040. return -EINVAL;
  4041. filter_str = strndup_user(arg, PAGE_SIZE);
  4042. if (IS_ERR(filter_str))
  4043. return PTR_ERR(filter_str);
  4044. ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  4045. kfree(filter_str);
  4046. return ret;
  4047. }
  4048. static void perf_event_free_filter(struct perf_event *event)
  4049. {
  4050. ftrace_profile_free_filter(event);
  4051. }
  4052. #else
  4053. static inline void perf_tp_register(void)
  4054. {
  4055. }
  4056. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  4057. {
  4058. return -ENOENT;
  4059. }
  4060. static void perf_event_free_filter(struct perf_event *event)
  4061. {
  4062. }
  4063. #endif /* CONFIG_EVENT_TRACING */
  4064. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  4065. void perf_bp_event(struct perf_event *bp, void *data)
  4066. {
  4067. struct perf_sample_data sample;
  4068. struct pt_regs *regs = data;
  4069. perf_sample_data_init(&sample, bp->attr.bp_addr);
  4070. if (!bp->hw.state && !perf_exclude_event(bp, regs))
  4071. perf_swevent_event(bp, 1, 1, &sample, regs);
  4072. }
  4073. #endif
  4074. /*
  4075. * hrtimer based swevent callback
  4076. */
  4077. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  4078. {
  4079. enum hrtimer_restart ret = HRTIMER_RESTART;
  4080. struct perf_sample_data data;
  4081. struct pt_regs *regs;
  4082. struct perf_event *event;
  4083. u64 period;
  4084. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  4085. event->pmu->read(event);
  4086. perf_sample_data_init(&data, 0);
  4087. data.period = event->hw.last_period;
  4088. regs = get_irq_regs();
  4089. if (regs && !perf_exclude_event(event, regs)) {
  4090. if (!(event->attr.exclude_idle && current->pid == 0))
  4091. if (perf_event_overflow(event, 0, &data, regs))
  4092. ret = HRTIMER_NORESTART;
  4093. }
  4094. period = max_t(u64, 10000, event->hw.sample_period);
  4095. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  4096. return ret;
  4097. }
  4098. static void perf_swevent_start_hrtimer(struct perf_event *event)
  4099. {
  4100. struct hw_perf_event *hwc = &event->hw;
  4101. s64 period;
  4102. if (!is_sampling_event(event))
  4103. return;
  4104. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  4105. hwc->hrtimer.function = perf_swevent_hrtimer;
  4106. period = local64_read(&hwc->period_left);
  4107. if (period) {
  4108. if (period < 0)
  4109. period = 10000;
  4110. local64_set(&hwc->period_left, 0);
  4111. } else {
  4112. period = max_t(u64, 10000, hwc->sample_period);
  4113. }
  4114. __hrtimer_start_range_ns(&hwc->hrtimer,
  4115. ns_to_ktime(period), 0,
  4116. HRTIMER_MODE_REL_PINNED, 0);
  4117. }
  4118. static void perf_swevent_cancel_hrtimer(struct perf_event *event)
  4119. {
  4120. struct hw_perf_event *hwc = &event->hw;
  4121. if (is_sampling_event(event)) {
  4122. ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
  4123. local64_set(&hwc->period_left, ktime_to_ns(remaining));
  4124. hrtimer_cancel(&hwc->hrtimer);
  4125. }
  4126. }
  4127. /*
  4128. * Software event: cpu wall time clock
  4129. */
  4130. static void cpu_clock_event_update(struct perf_event *event)
  4131. {
  4132. s64 prev;
  4133. u64 now;
  4134. now = local_clock();
  4135. prev = local64_xchg(&event->hw.prev_count, now);
  4136. local64_add(now - prev, &event->count);
  4137. }
  4138. static void cpu_clock_event_start(struct perf_event *event, int flags)
  4139. {
  4140. local64_set(&event->hw.prev_count, local_clock());
  4141. perf_swevent_start_hrtimer(event);
  4142. }
  4143. static void cpu_clock_event_stop(struct perf_event *event, int flags)
  4144. {
  4145. perf_swevent_cancel_hrtimer(event);
  4146. cpu_clock_event_update(event);
  4147. }
  4148. static int cpu_clock_event_add(struct perf_event *event, int flags)
  4149. {
  4150. if (flags & PERF_EF_START)
  4151. cpu_clock_event_start(event, flags);
  4152. return 0;
  4153. }
  4154. static void cpu_clock_event_del(struct perf_event *event, int flags)
  4155. {
  4156. cpu_clock_event_stop(event, flags);
  4157. }
  4158. static void cpu_clock_event_read(struct perf_event *event)
  4159. {
  4160. cpu_clock_event_update(event);
  4161. }
  4162. static int cpu_clock_event_init(struct perf_event *event)
  4163. {
  4164. if (event->attr.type != PERF_TYPE_SOFTWARE)
  4165. return -ENOENT;
  4166. if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
  4167. return -ENOENT;
  4168. return 0;
  4169. }
  4170. static struct pmu perf_cpu_clock = {
  4171. .task_ctx_nr = perf_sw_context,
  4172. .event_init = cpu_clock_event_init,
  4173. .add = cpu_clock_event_add,
  4174. .del = cpu_clock_event_del,
  4175. .start = cpu_clock_event_start,
  4176. .stop = cpu_clock_event_stop,
  4177. .read = cpu_clock_event_read,
  4178. };
  4179. /*
  4180. * Software event: task time clock
  4181. */
  4182. static void task_clock_event_update(struct perf_event *event, u64 now)
  4183. {
  4184. u64 prev;
  4185. s64 delta;
  4186. prev = local64_xchg(&event->hw.prev_count, now);
  4187. delta = now - prev;
  4188. local64_add(delta, &event->count);
  4189. }
  4190. static void task_clock_event_start(struct perf_event *event, int flags)
  4191. {
  4192. local64_set(&event->hw.prev_count, event->ctx->time);
  4193. perf_swevent_start_hrtimer(event);
  4194. }
  4195. static void task_clock_event_stop(struct perf_event *event, int flags)
  4196. {
  4197. perf_swevent_cancel_hrtimer(event);
  4198. task_clock_event_update(event, event->ctx->time);
  4199. }
  4200. static int task_clock_event_add(struct perf_event *event, int flags)
  4201. {
  4202. if (flags & PERF_EF_START)
  4203. task_clock_event_start(event, flags);
  4204. return 0;
  4205. }
  4206. static void task_clock_event_del(struct perf_event *event, int flags)
  4207. {
  4208. task_clock_event_stop(event, PERF_EF_UPDATE);
  4209. }
  4210. static void task_clock_event_read(struct perf_event *event)
  4211. {
  4212. u64 time;
  4213. if (!in_nmi()) {
  4214. update_context_time(event->ctx);
  4215. time = event->ctx->time;
  4216. } else {
  4217. u64 now = perf_clock();
  4218. u64 delta = now - event->ctx->timestamp;
  4219. time = event->ctx->time + delta;
  4220. }
  4221. task_clock_event_update(event, time);
  4222. }
  4223. static int task_clock_event_init(struct perf_event *event)
  4224. {
  4225. if (event->attr.type != PERF_TYPE_SOFTWARE)
  4226. return -ENOENT;
  4227. if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
  4228. return -ENOENT;
  4229. return 0;
  4230. }
  4231. static struct pmu perf_task_clock = {
  4232. .task_ctx_nr = perf_sw_context,
  4233. .event_init = task_clock_event_init,
  4234. .add = task_clock_event_add,
  4235. .del = task_clock_event_del,
  4236. .start = task_clock_event_start,
  4237. .stop = task_clock_event_stop,
  4238. .read = task_clock_event_read,
  4239. };
  4240. static void perf_pmu_nop_void(struct pmu *pmu)
  4241. {
  4242. }
  4243. static int perf_pmu_nop_int(struct pmu *pmu)
  4244. {
  4245. return 0;
  4246. }
  4247. static void perf_pmu_start_txn(struct pmu *pmu)
  4248. {
  4249. perf_pmu_disable(pmu);
  4250. }
  4251. static int perf_pmu_commit_txn(struct pmu *pmu)
  4252. {
  4253. perf_pmu_enable(pmu);
  4254. return 0;
  4255. }
  4256. static void perf_pmu_cancel_txn(struct pmu *pmu)
  4257. {
  4258. perf_pmu_enable(pmu);
  4259. }
  4260. /*
  4261. * Ensures all contexts with the same task_ctx_nr have the same
  4262. * pmu_cpu_context too.
  4263. */
  4264. static void *find_pmu_context(int ctxn)
  4265. {
  4266. struct pmu *pmu;
  4267. if (ctxn < 0)
  4268. return NULL;
  4269. list_for_each_entry(pmu, &pmus, entry) {
  4270. if (pmu->task_ctx_nr == ctxn)
  4271. return pmu->pmu_cpu_context;
  4272. }
  4273. return NULL;
  4274. }
  4275. static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
  4276. {
  4277. int cpu;
  4278. for_each_possible_cpu(cpu) {
  4279. struct perf_cpu_context *cpuctx;
  4280. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  4281. if (cpuctx->active_pmu == old_pmu)
  4282. cpuctx->active_pmu = pmu;
  4283. }
  4284. }
  4285. static void free_pmu_context(struct pmu *pmu)
  4286. {
  4287. struct pmu *i;
  4288. mutex_lock(&pmus_lock);
  4289. /*
  4290. * Like a real lame refcount.
  4291. */
  4292. list_for_each_entry(i, &pmus, entry) {
  4293. if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
  4294. update_pmu_context(i, pmu);
  4295. goto out;
  4296. }
  4297. }
  4298. free_percpu(pmu->pmu_cpu_context);
  4299. out:
  4300. mutex_unlock(&pmus_lock);
  4301. }
  4302. static struct idr pmu_idr;
  4303. static ssize_t
  4304. type_show(struct device *dev, struct device_attribute *attr, char *page)
  4305. {
  4306. struct pmu *pmu = dev_get_drvdata(dev);
  4307. return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
  4308. }
  4309. static struct device_attribute pmu_dev_attrs[] = {
  4310. __ATTR_RO(type),
  4311. __ATTR_NULL,
  4312. };
  4313. static int pmu_bus_running;
  4314. static struct bus_type pmu_bus = {
  4315. .name = "event_source",
  4316. .dev_attrs = pmu_dev_attrs,
  4317. };
  4318. static void pmu_dev_release(struct device *dev)
  4319. {
  4320. kfree(dev);
  4321. }
  4322. static int pmu_dev_alloc(struct pmu *pmu)
  4323. {
  4324. int ret = -ENOMEM;
  4325. pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  4326. if (!pmu->dev)
  4327. goto out;
  4328. device_initialize(pmu->dev);
  4329. ret = dev_set_name(pmu->dev, "%s", pmu->name);
  4330. if (ret)
  4331. goto free_dev;
  4332. dev_set_drvdata(pmu->dev, pmu);
  4333. pmu->dev->bus = &pmu_bus;
  4334. pmu->dev->release = pmu_dev_release;
  4335. ret = device_add(pmu->dev);
  4336. if (ret)
  4337. goto free_dev;
  4338. out:
  4339. return ret;
  4340. free_dev:
  4341. put_device(pmu->dev);
  4342. goto out;
  4343. }
  4344. static struct lock_class_key cpuctx_mutex;
  4345. int perf_pmu_register(struct pmu *pmu, char *name, int type)
  4346. {
  4347. int cpu, ret;
  4348. mutex_lock(&pmus_lock);
  4349. ret = -ENOMEM;
  4350. pmu->pmu_disable_count = alloc_percpu(int);
  4351. if (!pmu->pmu_disable_count)
  4352. goto unlock;
  4353. pmu->type = -1;
  4354. if (!name)
  4355. goto skip_type;
  4356. pmu->name = name;
  4357. if (type < 0) {
  4358. int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
  4359. if (!err)
  4360. goto free_pdc;
  4361. err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
  4362. if (err) {
  4363. ret = err;
  4364. goto free_pdc;
  4365. }
  4366. }
  4367. pmu->type = type;
  4368. if (pmu_bus_running) {
  4369. ret = pmu_dev_alloc(pmu);
  4370. if (ret)
  4371. goto free_idr;
  4372. }
  4373. skip_type:
  4374. pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
  4375. if (pmu->pmu_cpu_context)
  4376. goto got_cpu_context;
  4377. pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
  4378. if (!pmu->pmu_cpu_context)
  4379. goto free_dev;
  4380. for_each_possible_cpu(cpu) {
  4381. struct perf_cpu_context *cpuctx;
  4382. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  4383. __perf_event_init_context(&cpuctx->ctx);
  4384. lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
  4385. cpuctx->ctx.type = cpu_context;
  4386. cpuctx->ctx.pmu = pmu;
  4387. cpuctx->jiffies_interval = 1;
  4388. INIT_LIST_HEAD(&cpuctx->rotation_list);
  4389. cpuctx->active_pmu = pmu;
  4390. }
  4391. got_cpu_context:
  4392. if (!pmu->start_txn) {
  4393. if (pmu->pmu_enable) {
  4394. /*
  4395. * If we have pmu_enable/pmu_disable calls, install
  4396. * transaction stubs that use that to try and batch
  4397. * hardware accesses.
  4398. */
  4399. pmu->start_txn = perf_pmu_start_txn;
  4400. pmu->commit_txn = perf_pmu_commit_txn;
  4401. pmu->cancel_txn = perf_pmu_cancel_txn;
  4402. } else {
  4403. pmu->start_txn = perf_pmu_nop_void;
  4404. pmu->commit_txn = perf_pmu_nop_int;
  4405. pmu->cancel_txn = perf_pmu_nop_void;
  4406. }
  4407. }
  4408. if (!pmu->pmu_enable) {
  4409. pmu->pmu_enable = perf_pmu_nop_void;
  4410. pmu->pmu_disable = perf_pmu_nop_void;
  4411. }
  4412. list_add_rcu(&pmu->entry, &pmus);
  4413. ret = 0;
  4414. unlock:
  4415. mutex_unlock(&pmus_lock);
  4416. return ret;
  4417. free_dev:
  4418. device_del(pmu->dev);
  4419. put_device(pmu->dev);
  4420. free_idr:
  4421. if (pmu->type >= PERF_TYPE_MAX)
  4422. idr_remove(&pmu_idr, pmu->type);
  4423. free_pdc:
  4424. free_percpu(pmu->pmu_disable_count);
  4425. goto unlock;
  4426. }
  4427. void perf_pmu_unregister(struct pmu *pmu)
  4428. {
  4429. mutex_lock(&pmus_lock);
  4430. list_del_rcu(&pmu->entry);
  4431. mutex_unlock(&pmus_lock);
  4432. /*
  4433. * We dereference the pmu list under both SRCU and regular RCU, so
  4434. * synchronize against both of those.
  4435. */
  4436. synchronize_srcu(&pmus_srcu);
  4437. synchronize_rcu();
  4438. free_percpu(pmu->pmu_disable_count);
  4439. if (pmu->type >= PERF_TYPE_MAX)
  4440. idr_remove(&pmu_idr, pmu->type);
  4441. device_del(pmu->dev);
  4442. put_device(pmu->dev);
  4443. free_pmu_context(pmu);
  4444. }
  4445. struct pmu *perf_init_event(struct perf_event *event)
  4446. {
  4447. struct pmu *pmu = NULL;
  4448. int idx;
  4449. idx = srcu_read_lock(&pmus_srcu);
  4450. rcu_read_lock();
  4451. pmu = idr_find(&pmu_idr, event->attr.type);
  4452. rcu_read_unlock();
  4453. if (pmu)
  4454. goto unlock;
  4455. list_for_each_entry_rcu(pmu, &pmus, entry) {
  4456. int ret = pmu->event_init(event);
  4457. if (!ret)
  4458. goto unlock;
  4459. if (ret != -ENOENT) {
  4460. pmu = ERR_PTR(ret);
  4461. goto unlock;
  4462. }
  4463. }
  4464. pmu = ERR_PTR(-ENOENT);
  4465. unlock:
  4466. srcu_read_unlock(&pmus_srcu, idx);
  4467. return pmu;
  4468. }
  4469. /*
  4470. * Allocate and initialize a event structure
  4471. */
  4472. static struct perf_event *
  4473. perf_event_alloc(struct perf_event_attr *attr, int cpu,
  4474. struct task_struct *task,
  4475. struct perf_event *group_leader,
  4476. struct perf_event *parent_event,
  4477. perf_overflow_handler_t overflow_handler)
  4478. {
  4479. struct pmu *pmu;
  4480. struct perf_event *event;
  4481. struct hw_perf_event *hwc;
  4482. long err;
  4483. if ((unsigned)cpu >= nr_cpu_ids) {
  4484. if (!task || cpu != -1)
  4485. return ERR_PTR(-EINVAL);
  4486. }
  4487. event = kzalloc(sizeof(*event), GFP_KERNEL);
  4488. if (!event)
  4489. return ERR_PTR(-ENOMEM);
  4490. /*
  4491. * Single events are their own group leaders, with an
  4492. * empty sibling list:
  4493. */
  4494. if (!group_leader)
  4495. group_leader = event;
  4496. mutex_init(&event->child_mutex);
  4497. INIT_LIST_HEAD(&event->child_list);
  4498. INIT_LIST_HEAD(&event->group_entry);
  4499. INIT_LIST_HEAD(&event->event_entry);
  4500. INIT_LIST_HEAD(&event->sibling_list);
  4501. init_waitqueue_head(&event->waitq);
  4502. init_irq_work(&event->pending, perf_pending_event);
  4503. mutex_init(&event->mmap_mutex);
  4504. event->cpu = cpu;
  4505. event->attr = *attr;
  4506. event->group_leader = group_leader;
  4507. event->pmu = NULL;
  4508. event->oncpu = -1;
  4509. event->parent = parent_event;
  4510. event->ns = get_pid_ns(current->nsproxy->pid_ns);
  4511. event->id = atomic64_inc_return(&perf_event_id);
  4512. event->state = PERF_EVENT_STATE_INACTIVE;
  4513. if (task) {
  4514. event->attach_state = PERF_ATTACH_TASK;
  4515. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  4516. /*
  4517. * hw_breakpoint is a bit difficult here..
  4518. */
  4519. if (attr->type == PERF_TYPE_BREAKPOINT)
  4520. event->hw.bp_target = task;
  4521. #endif
  4522. }
  4523. if (!overflow_handler && parent_event)
  4524. overflow_handler = parent_event->overflow_handler;
  4525. event->overflow_handler = overflow_handler;
  4526. if (attr->disabled)
  4527. event->state = PERF_EVENT_STATE_OFF;
  4528. pmu = NULL;
  4529. hwc = &event->hw;
  4530. hwc->sample_period = attr->sample_period;
  4531. if (attr->freq && attr->sample_freq)
  4532. hwc->sample_period = 1;
  4533. hwc->last_period = hwc->sample_period;
  4534. local64_set(&hwc->period_left, hwc->sample_period);
  4535. /*
  4536. * we currently do not support PERF_FORMAT_GROUP on inherited events
  4537. */
  4538. if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
  4539. goto done;
  4540. pmu = perf_init_event(event);
  4541. done:
  4542. err = 0;
  4543. if (!pmu)
  4544. err = -EINVAL;
  4545. else if (IS_ERR(pmu))
  4546. err = PTR_ERR(pmu);
  4547. if (err) {
  4548. if (event->ns)
  4549. put_pid_ns(event->ns);
  4550. kfree(event);
  4551. return ERR_PTR(err);
  4552. }
  4553. event->pmu = pmu;
  4554. if (!event->parent) {
  4555. if (event->attach_state & PERF_ATTACH_TASK)
  4556. jump_label_inc(&perf_task_events);
  4557. if (event->attr.mmap || event->attr.mmap_data)
  4558. atomic_inc(&nr_mmap_events);
  4559. if (event->attr.comm)
  4560. atomic_inc(&nr_comm_events);
  4561. if (event->attr.task)
  4562. atomic_inc(&nr_task_events);
  4563. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  4564. err = get_callchain_buffers();
  4565. if (err) {
  4566. free_event(event);
  4567. return ERR_PTR(err);
  4568. }
  4569. }
  4570. }
  4571. return event;
  4572. }
  4573. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  4574. struct perf_event_attr *attr)
  4575. {
  4576. u32 size;
  4577. int ret;
  4578. if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  4579. return -EFAULT;
  4580. /*
  4581. * zero the full structure, so that a short copy will be nice.
  4582. */
  4583. memset(attr, 0, sizeof(*attr));
  4584. ret = get_user(size, &uattr->size);
  4585. if (ret)
  4586. return ret;
  4587. if (size > PAGE_SIZE) /* silly large */
  4588. goto err_size;
  4589. if (!size) /* abi compat */
  4590. size = PERF_ATTR_SIZE_VER0;
  4591. if (size < PERF_ATTR_SIZE_VER0)
  4592. goto err_size;
  4593. /*
  4594. * If we're handed a bigger struct than we know of,
  4595. * ensure all the unknown bits are 0 - i.e. new
  4596. * user-space does not rely on any kernel feature
  4597. * extensions we dont know about yet.
  4598. */
  4599. if (size > sizeof(*attr)) {
  4600. unsigned char __user *addr;
  4601. unsigned char __user *end;
  4602. unsigned char val;
  4603. addr = (void __user *)uattr + sizeof(*attr);
  4604. end = (void __user *)uattr + size;
  4605. for (; addr < end; addr++) {
  4606. ret = get_user(val, addr);
  4607. if (ret)
  4608. return ret;
  4609. if (val)
  4610. goto err_size;
  4611. }
  4612. size = sizeof(*attr);
  4613. }
  4614. ret = copy_from_user(attr, uattr, size);
  4615. if (ret)
  4616. return -EFAULT;
  4617. /*
  4618. * If the type exists, the corresponding creation will verify
  4619. * the attr->config.
  4620. */
  4621. if (attr->type >= PERF_TYPE_MAX)
  4622. return -EINVAL;
  4623. if (attr->__reserved_1)
  4624. return -EINVAL;
  4625. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  4626. return -EINVAL;
  4627. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  4628. return -EINVAL;
  4629. out:
  4630. return ret;
  4631. err_size:
  4632. put_user(sizeof(*attr), &uattr->size);
  4633. ret = -E2BIG;
  4634. goto out;
  4635. }
  4636. static int
  4637. perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
  4638. {
  4639. struct perf_buffer *buffer = NULL, *old_buffer = NULL;
  4640. int ret = -EINVAL;
  4641. if (!output_event)
  4642. goto set;
  4643. /* don't allow circular references */
  4644. if (event == output_event)
  4645. goto out;
  4646. /*
  4647. * Don't allow cross-cpu buffers
  4648. */
  4649. if (output_event->cpu != event->cpu)
  4650. goto out;
  4651. /*
  4652. * If its not a per-cpu buffer, it must be the same task.
  4653. */
  4654. if (output_event->cpu == -1 && output_event->ctx != event->ctx)
  4655. goto out;
  4656. set:
  4657. mutex_lock(&event->mmap_mutex);
  4658. /* Can't redirect output if we've got an active mmap() */
  4659. if (atomic_read(&event->mmap_count))
  4660. goto unlock;
  4661. if (output_event) {
  4662. /* get the buffer we want to redirect to */
  4663. buffer = perf_buffer_get(output_event);
  4664. if (!buffer)
  4665. goto unlock;
  4666. }
  4667. old_buffer = event->buffer;
  4668. rcu_assign_pointer(event->buffer, buffer);
  4669. ret = 0;
  4670. unlock:
  4671. mutex_unlock(&event->mmap_mutex);
  4672. if (old_buffer)
  4673. perf_buffer_put(old_buffer);
  4674. out:
  4675. return ret;
  4676. }
  4677. /**
  4678. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  4679. *
  4680. * @attr_uptr: event_id type attributes for monitoring/sampling
  4681. * @pid: target pid
  4682. * @cpu: target cpu
  4683. * @group_fd: group leader event fd
  4684. */
  4685. SYSCALL_DEFINE5(perf_event_open,
  4686. struct perf_event_attr __user *, attr_uptr,
  4687. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  4688. {
  4689. struct perf_event *group_leader = NULL, *output_event = NULL;
  4690. struct perf_event *event, *sibling;
  4691. struct perf_event_attr attr;
  4692. struct perf_event_context *ctx;
  4693. struct file *event_file = NULL;
  4694. struct file *group_file = NULL;
  4695. struct task_struct *task = NULL;
  4696. struct pmu *pmu;
  4697. int event_fd;
  4698. int move_group = 0;
  4699. int fput_needed = 0;
  4700. int err;
  4701. /* for future expandability... */
  4702. if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
  4703. return -EINVAL;
  4704. err = perf_copy_attr(attr_uptr, &attr);
  4705. if (err)
  4706. return err;
  4707. if (!attr.exclude_kernel) {
  4708. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  4709. return -EACCES;
  4710. }
  4711. if (attr.freq) {
  4712. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  4713. return -EINVAL;
  4714. }
  4715. event_fd = get_unused_fd_flags(O_RDWR);
  4716. if (event_fd < 0)
  4717. return event_fd;
  4718. if (group_fd != -1) {
  4719. group_leader = perf_fget_light(group_fd, &fput_needed);
  4720. if (IS_ERR(group_leader)) {
  4721. err = PTR_ERR(group_leader);
  4722. goto err_fd;
  4723. }
  4724. group_file = group_leader->filp;
  4725. if (flags & PERF_FLAG_FD_OUTPUT)
  4726. output_event = group_leader;
  4727. if (flags & PERF_FLAG_FD_NO_GROUP)
  4728. group_leader = NULL;
  4729. }
  4730. if (pid != -1) {
  4731. task = find_lively_task_by_vpid(pid);
  4732. if (IS_ERR(task)) {
  4733. err = PTR_ERR(task);
  4734. goto err_group_fd;
  4735. }
  4736. }
  4737. event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
  4738. if (IS_ERR(event)) {
  4739. err = PTR_ERR(event);
  4740. goto err_task;
  4741. }
  4742. /*
  4743. * Special case software events and allow them to be part of
  4744. * any hardware group.
  4745. */
  4746. pmu = event->pmu;
  4747. if (group_leader &&
  4748. (is_software_event(event) != is_software_event(group_leader))) {
  4749. if (is_software_event(event)) {
  4750. /*
  4751. * If event and group_leader are not both a software
  4752. * event, and event is, then group leader is not.
  4753. *
  4754. * Allow the addition of software events to !software
  4755. * groups, this is safe because software events never
  4756. * fail to schedule.
  4757. */
  4758. pmu = group_leader->pmu;
  4759. } else if (is_software_event(group_leader) &&
  4760. (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
  4761. /*
  4762. * In case the group is a pure software group, and we
  4763. * try to add a hardware event, move the whole group to
  4764. * the hardware context.
  4765. */
  4766. move_group = 1;
  4767. }
  4768. }
  4769. /*
  4770. * Get the target context (task or percpu):
  4771. */
  4772. ctx = find_get_context(pmu, task, cpu);
  4773. if (IS_ERR(ctx)) {
  4774. err = PTR_ERR(ctx);
  4775. goto err_alloc;
  4776. }
  4777. /*
  4778. * Look up the group leader (we will attach this event to it):
  4779. */
  4780. if (group_leader) {
  4781. err = -EINVAL;
  4782. /*
  4783. * Do not allow a recursive hierarchy (this new sibling
  4784. * becoming part of another group-sibling):
  4785. */
  4786. if (group_leader->group_leader != group_leader)
  4787. goto err_context;
  4788. /*
  4789. * Do not allow to attach to a group in a different
  4790. * task or CPU context:
  4791. */
  4792. if (move_group) {
  4793. if (group_leader->ctx->type != ctx->type)
  4794. goto err_context;
  4795. } else {
  4796. if (group_leader->ctx != ctx)
  4797. goto err_context;
  4798. }
  4799. /*
  4800. * Only a group leader can be exclusive or pinned
  4801. */
  4802. if (attr.exclusive || attr.pinned)
  4803. goto err_context;
  4804. }
  4805. if (output_event) {
  4806. err = perf_event_set_output(event, output_event);
  4807. if (err)
  4808. goto err_context;
  4809. }
  4810. event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
  4811. if (IS_ERR(event_file)) {
  4812. err = PTR_ERR(event_file);
  4813. goto err_context;
  4814. }
  4815. if (move_group) {
  4816. struct perf_event_context *gctx = group_leader->ctx;
  4817. mutex_lock(&gctx->mutex);
  4818. perf_event_remove_from_context(group_leader);
  4819. list_for_each_entry(sibling, &group_leader->sibling_list,
  4820. group_entry) {
  4821. perf_event_remove_from_context(sibling);
  4822. put_ctx(gctx);
  4823. }
  4824. mutex_unlock(&gctx->mutex);
  4825. put_ctx(gctx);
  4826. }
  4827. event->filp = event_file;
  4828. WARN_ON_ONCE(ctx->parent_ctx);
  4829. mutex_lock(&ctx->mutex);
  4830. if (move_group) {
  4831. perf_install_in_context(ctx, group_leader, cpu);
  4832. get_ctx(ctx);
  4833. list_for_each_entry(sibling, &group_leader->sibling_list,
  4834. group_entry) {
  4835. perf_install_in_context(ctx, sibling, cpu);
  4836. get_ctx(ctx);
  4837. }
  4838. }
  4839. perf_install_in_context(ctx, event, cpu);
  4840. ++ctx->generation;
  4841. mutex_unlock(&ctx->mutex);
  4842. event->owner = current;
  4843. mutex_lock(&current->perf_event_mutex);
  4844. list_add_tail(&event->owner_entry, &current->perf_event_list);
  4845. mutex_unlock(&current->perf_event_mutex);
  4846. /*
  4847. * Precalculate sample_data sizes
  4848. */
  4849. perf_event__header_size(event);
  4850. perf_event__id_header_size(event);
  4851. /*
  4852. * Drop the reference on the group_event after placing the
  4853. * new event on the sibling_list. This ensures destruction
  4854. * of the group leader will find the pointer to itself in
  4855. * perf_group_detach().
  4856. */
  4857. fput_light(group_file, fput_needed);
  4858. fd_install(event_fd, event_file);
  4859. return event_fd;
  4860. err_context:
  4861. put_ctx(ctx);
  4862. err_alloc:
  4863. free_event(event);
  4864. err_task:
  4865. if (task)
  4866. put_task_struct(task);
  4867. err_group_fd:
  4868. fput_light(group_file, fput_needed);
  4869. err_fd:
  4870. put_unused_fd(event_fd);
  4871. return err;
  4872. }
  4873. /**
  4874. * perf_event_create_kernel_counter
  4875. *
  4876. * @attr: attributes of the counter to create
  4877. * @cpu: cpu in which the counter is bound
  4878. * @task: task to profile (NULL for percpu)
  4879. */
  4880. struct perf_event *
  4881. perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  4882. struct task_struct *task,
  4883. perf_overflow_handler_t overflow_handler)
  4884. {
  4885. struct perf_event_context *ctx;
  4886. struct perf_event *event;
  4887. int err;
  4888. /*
  4889. * Get the target context (task or percpu):
  4890. */
  4891. event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
  4892. if (IS_ERR(event)) {
  4893. err = PTR_ERR(event);
  4894. goto err;
  4895. }
  4896. ctx = find_get_context(event->pmu, task, cpu);
  4897. if (IS_ERR(ctx)) {
  4898. err = PTR_ERR(ctx);
  4899. goto err_free;
  4900. }
  4901. event->filp = NULL;
  4902. WARN_ON_ONCE(ctx->parent_ctx);
  4903. mutex_lock(&ctx->mutex);
  4904. perf_install_in_context(ctx, event, cpu);
  4905. ++ctx->generation;
  4906. mutex_unlock(&ctx->mutex);
  4907. return event;
  4908. err_free:
  4909. free_event(event);
  4910. err:
  4911. return ERR_PTR(err);
  4912. }
  4913. EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
  4914. static void sync_child_event(struct perf_event *child_event,
  4915. struct task_struct *child)
  4916. {
  4917. struct perf_event *parent_event = child_event->parent;
  4918. u64 child_val;
  4919. if (child_event->attr.inherit_stat)
  4920. perf_event_read_event(child_event, child);
  4921. child_val = perf_event_count(child_event);
  4922. /*
  4923. * Add back the child's count to the parent's count:
  4924. */
  4925. atomic64_add(child_val, &parent_event->child_count);
  4926. atomic64_add(child_event->total_time_enabled,
  4927. &parent_event->child_total_time_enabled);
  4928. atomic64_add(child_event->total_time_running,
  4929. &parent_event->child_total_time_running);
  4930. /*
  4931. * Remove this event from the parent's list
  4932. */
  4933. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  4934. mutex_lock(&parent_event->child_mutex);
  4935. list_del_init(&child_event->child_list);
  4936. mutex_unlock(&parent_event->child_mutex);
  4937. /*
  4938. * Release the parent event, if this was the last
  4939. * reference to it.
  4940. */
  4941. fput(parent_event->filp);
  4942. }
  4943. static void
  4944. __perf_event_exit_task(struct perf_event *child_event,
  4945. struct perf_event_context *child_ctx,
  4946. struct task_struct *child)
  4947. {
  4948. struct perf_event *parent_event;
  4949. perf_event_remove_from_context(child_event);
  4950. parent_event = child_event->parent;
  4951. /*
  4952. * It can happen that parent exits first, and has events
  4953. * that are still around due to the child reference. These
  4954. * events need to be zapped - but otherwise linger.
  4955. */
  4956. if (parent_event) {
  4957. sync_child_event(child_event, child);
  4958. free_event(child_event);
  4959. }
  4960. }
  4961. static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
  4962. {
  4963. struct perf_event *child_event, *tmp;
  4964. struct perf_event_context *child_ctx;
  4965. unsigned long flags;
  4966. if (likely(!child->perf_event_ctxp[ctxn])) {
  4967. perf_event_task(child, NULL, 0);
  4968. return;
  4969. }
  4970. local_irq_save(flags);
  4971. /*
  4972. * We can't reschedule here because interrupts are disabled,
  4973. * and either child is current or it is a task that can't be
  4974. * scheduled, so we are now safe from rescheduling changing
  4975. * our context.
  4976. */
  4977. child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
  4978. task_ctx_sched_out(child_ctx, EVENT_ALL);
  4979. /*
  4980. * Take the context lock here so that if find_get_context is
  4981. * reading child->perf_event_ctxp, we wait until it has
  4982. * incremented the context's refcount before we do put_ctx below.
  4983. */
  4984. raw_spin_lock(&child_ctx->lock);
  4985. child->perf_event_ctxp[ctxn] = NULL;
  4986. /*
  4987. * If this context is a clone; unclone it so it can't get
  4988. * swapped to another process while we're removing all
  4989. * the events from it.
  4990. */
  4991. unclone_ctx(child_ctx);
  4992. update_context_time(child_ctx);
  4993. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  4994. /*
  4995. * Report the task dead after unscheduling the events so that we
  4996. * won't get any samples after PERF_RECORD_EXIT. We can however still
  4997. * get a few PERF_RECORD_READ events.
  4998. */
  4999. perf_event_task(child, child_ctx, 0);
  5000. /*
  5001. * We can recurse on the same lock type through:
  5002. *
  5003. * __perf_event_exit_task()
  5004. * sync_child_event()
  5005. * fput(parent_event->filp)
  5006. * perf_release()
  5007. * mutex_lock(&ctx->mutex)
  5008. *
  5009. * But since its the parent context it won't be the same instance.
  5010. */
  5011. mutex_lock(&child_ctx->mutex);
  5012. again:
  5013. list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
  5014. group_entry)
  5015. __perf_event_exit_task(child_event, child_ctx, child);
  5016. list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
  5017. group_entry)
  5018. __perf_event_exit_task(child_event, child_ctx, child);
  5019. /*
  5020. * If the last event was a group event, it will have appended all
  5021. * its siblings to the list, but we obtained 'tmp' before that which
  5022. * will still point to the list head terminating the iteration.
  5023. */
  5024. if (!list_empty(&child_ctx->pinned_groups) ||
  5025. !list_empty(&child_ctx->flexible_groups))
  5026. goto again;
  5027. mutex_unlock(&child_ctx->mutex);
  5028. put_ctx(child_ctx);
  5029. }
  5030. /*
  5031. * When a child task exits, feed back event values to parent events.
  5032. */
  5033. void perf_event_exit_task(struct task_struct *child)
  5034. {
  5035. struct perf_event *event, *tmp;
  5036. int ctxn;
  5037. mutex_lock(&child->perf_event_mutex);
  5038. list_for_each_entry_safe(event, tmp, &child->perf_event_list,
  5039. owner_entry) {
  5040. list_del_init(&event->owner_entry);
  5041. /*
  5042. * Ensure the list deletion is visible before we clear
  5043. * the owner, closes a race against perf_release() where
  5044. * we need to serialize on the owner->perf_event_mutex.
  5045. */
  5046. smp_wmb();
  5047. event->owner = NULL;
  5048. }
  5049. mutex_unlock(&child->perf_event_mutex);
  5050. for_each_task_context_nr(ctxn)
  5051. perf_event_exit_task_context(child, ctxn);
  5052. }
  5053. static void perf_free_event(struct perf_event *event,
  5054. struct perf_event_context *ctx)
  5055. {
  5056. struct perf_event *parent = event->parent;
  5057. if (WARN_ON_ONCE(!parent))
  5058. return;
  5059. mutex_lock(&parent->child_mutex);
  5060. list_del_init(&event->child_list);
  5061. mutex_unlock(&parent->child_mutex);
  5062. fput(parent->filp);
  5063. perf_group_detach(event);
  5064. list_del_event(event, ctx);
  5065. free_event(event);
  5066. }
  5067. /*
  5068. * free an unexposed, unused context as created by inheritance by
  5069. * perf_event_init_task below, used by fork() in case of fail.
  5070. */
  5071. void perf_event_free_task(struct task_struct *task)
  5072. {
  5073. struct perf_event_context *ctx;
  5074. struct perf_event *event, *tmp;
  5075. int ctxn;
  5076. for_each_task_context_nr(ctxn) {
  5077. ctx = task->perf_event_ctxp[ctxn];
  5078. if (!ctx)
  5079. continue;
  5080. mutex_lock(&ctx->mutex);
  5081. again:
  5082. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
  5083. group_entry)
  5084. perf_free_event(event, ctx);
  5085. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
  5086. group_entry)
  5087. perf_free_event(event, ctx);
  5088. if (!list_empty(&ctx->pinned_groups) ||
  5089. !list_empty(&ctx->flexible_groups))
  5090. goto again;
  5091. mutex_unlock(&ctx->mutex);
  5092. put_ctx(ctx);
  5093. }
  5094. }
  5095. void perf_event_delayed_put(struct task_struct *task)
  5096. {
  5097. int ctxn;
  5098. for_each_task_context_nr(ctxn)
  5099. WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
  5100. }
  5101. /*
  5102. * inherit a event from parent task to child task:
  5103. */
  5104. static struct perf_event *
  5105. inherit_event(struct perf_event *parent_event,
  5106. struct task_struct *parent,
  5107. struct perf_event_context *parent_ctx,
  5108. struct task_struct *child,
  5109. struct perf_event *group_leader,
  5110. struct perf_event_context *child_ctx)
  5111. {
  5112. struct perf_event *child_event;
  5113. unsigned long flags;
  5114. /*
  5115. * Instead of creating recursive hierarchies of events,
  5116. * we link inherited events back to the original parent,
  5117. * which has a filp for sure, which we use as the reference
  5118. * count:
  5119. */
  5120. if (parent_event->parent)
  5121. parent_event = parent_event->parent;
  5122. child_event = perf_event_alloc(&parent_event->attr,
  5123. parent_event->cpu,
  5124. child,
  5125. group_leader, parent_event,
  5126. NULL);
  5127. if (IS_ERR(child_event))
  5128. return child_event;
  5129. get_ctx(child_ctx);
  5130. /*
  5131. * Make the child state follow the state of the parent event,
  5132. * not its attr.disabled bit. We hold the parent's mutex,
  5133. * so we won't race with perf_event_{en, dis}able_family.
  5134. */
  5135. if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
  5136. child_event->state = PERF_EVENT_STATE_INACTIVE;
  5137. else
  5138. child_event->state = PERF_EVENT_STATE_OFF;
  5139. if (parent_event->attr.freq) {
  5140. u64 sample_period = parent_event->hw.sample_period;
  5141. struct hw_perf_event *hwc = &child_event->hw;
  5142. hwc->sample_period = sample_period;
  5143. hwc->last_period = sample_period;
  5144. local64_set(&hwc->period_left, sample_period);
  5145. }
  5146. child_event->ctx = child_ctx;
  5147. child_event->overflow_handler = parent_event->overflow_handler;
  5148. /*
  5149. * Precalculate sample_data sizes
  5150. */
  5151. perf_event__header_size(child_event);
  5152. perf_event__id_header_size(child_event);
  5153. /*
  5154. * Link it up in the child's context:
  5155. */
  5156. raw_spin_lock_irqsave(&child_ctx->lock, flags);
  5157. add_event_to_ctx(child_event, child_ctx);
  5158. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  5159. /*
  5160. * Get a reference to the parent filp - we will fput it
  5161. * when the child event exits. This is safe to do because
  5162. * we are in the parent and we know that the filp still
  5163. * exists and has a nonzero count:
  5164. */
  5165. atomic_long_inc(&parent_event->filp->f_count);
  5166. /*
  5167. * Link this into the parent event's child list
  5168. */
  5169. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  5170. mutex_lock(&parent_event->child_mutex);
  5171. list_add_tail(&child_event->child_list, &parent_event->child_list);
  5172. mutex_unlock(&parent_event->child_mutex);
  5173. return child_event;
  5174. }
  5175. static int inherit_group(struct perf_event *parent_event,
  5176. struct task_struct *parent,
  5177. struct perf_event_context *parent_ctx,
  5178. struct task_struct *child,
  5179. struct perf_event_context *child_ctx)
  5180. {
  5181. struct perf_event *leader;
  5182. struct perf_event *sub;
  5183. struct perf_event *child_ctr;
  5184. leader = inherit_event(parent_event, parent, parent_ctx,
  5185. child, NULL, child_ctx);
  5186. if (IS_ERR(leader))
  5187. return PTR_ERR(leader);
  5188. list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  5189. child_ctr = inherit_event(sub, parent, parent_ctx,
  5190. child, leader, child_ctx);
  5191. if (IS_ERR(child_ctr))
  5192. return PTR_ERR(child_ctr);
  5193. }
  5194. return 0;
  5195. }
  5196. static int
  5197. inherit_task_group(struct perf_event *event, struct task_struct *parent,
  5198. struct perf_event_context *parent_ctx,
  5199. struct task_struct *child, int ctxn,
  5200. int *inherited_all)
  5201. {
  5202. int ret;
  5203. struct perf_event_context *child_ctx;
  5204. if (!event->attr.inherit) {
  5205. *inherited_all = 0;
  5206. return 0;
  5207. }
  5208. child_ctx = child->perf_event_ctxp[ctxn];
  5209. if (!child_ctx) {
  5210. /*
  5211. * This is executed from the parent task context, so
  5212. * inherit events that have been marked for cloning.
  5213. * First allocate and initialize a context for the
  5214. * child.
  5215. */
  5216. child_ctx = alloc_perf_context(event->pmu, child);
  5217. if (!child_ctx)
  5218. return -ENOMEM;
  5219. child->perf_event_ctxp[ctxn] = child_ctx;
  5220. }
  5221. ret = inherit_group(event, parent, parent_ctx,
  5222. child, child_ctx);
  5223. if (ret)
  5224. *inherited_all = 0;
  5225. return ret;
  5226. }
  5227. /*
  5228. * Initialize the perf_event context in task_struct
  5229. */
  5230. int perf_event_init_context(struct task_struct *child, int ctxn)
  5231. {
  5232. struct perf_event_context *child_ctx, *parent_ctx;
  5233. struct perf_event_context *cloned_ctx;
  5234. struct perf_event *event;
  5235. struct task_struct *parent = current;
  5236. int inherited_all = 1;
  5237. unsigned long flags;
  5238. int ret = 0;
  5239. if (likely(!parent->perf_event_ctxp[ctxn]))
  5240. return 0;
  5241. /*
  5242. * If the parent's context is a clone, pin it so it won't get
  5243. * swapped under us.
  5244. */
  5245. parent_ctx = perf_pin_task_context(parent, ctxn);
  5246. /*
  5247. * No need to check if parent_ctx != NULL here; since we saw
  5248. * it non-NULL earlier, the only reason for it to become NULL
  5249. * is if we exit, and since we're currently in the middle of
  5250. * a fork we can't be exiting at the same time.
  5251. */
  5252. /*
  5253. * Lock the parent list. No need to lock the child - not PID
  5254. * hashed yet and not running, so nobody can access it.
  5255. */
  5256. mutex_lock(&parent_ctx->mutex);
  5257. /*
  5258. * We dont have to disable NMIs - we are only looking at
  5259. * the list, not manipulating it:
  5260. */
  5261. list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
  5262. ret = inherit_task_group(event, parent, parent_ctx,
  5263. child, ctxn, &inherited_all);
  5264. if (ret)
  5265. break;
  5266. }
  5267. /*
  5268. * We can't hold ctx->lock when iterating the ->flexible_group list due
  5269. * to allocations, but we need to prevent rotation because
  5270. * rotate_ctx() will change the list from interrupt context.
  5271. */
  5272. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  5273. parent_ctx->rotate_disable = 1;
  5274. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  5275. list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
  5276. ret = inherit_task_group(event, parent, parent_ctx,
  5277. child, ctxn, &inherited_all);
  5278. if (ret)
  5279. break;
  5280. }
  5281. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  5282. parent_ctx->rotate_disable = 0;
  5283. child_ctx = child->perf_event_ctxp[ctxn];
  5284. if (child_ctx && inherited_all) {
  5285. /*
  5286. * Mark the child context as a clone of the parent
  5287. * context, or of whatever the parent is a clone of.
  5288. *
  5289. * Note that if the parent is a clone, the holding of
  5290. * parent_ctx->lock avoids it from being uncloned.
  5291. */
  5292. cloned_ctx = parent_ctx->parent_ctx;
  5293. if (cloned_ctx) {
  5294. child_ctx->parent_ctx = cloned_ctx;
  5295. child_ctx->parent_gen = parent_ctx->parent_gen;
  5296. } else {
  5297. child_ctx->parent_ctx = parent_ctx;
  5298. child_ctx->parent_gen = parent_ctx->generation;
  5299. }
  5300. get_ctx(child_ctx->parent_ctx);
  5301. }
  5302. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  5303. mutex_unlock(&parent_ctx->mutex);
  5304. perf_unpin_context(parent_ctx);
  5305. return ret;
  5306. }
  5307. /*
  5308. * Initialize the perf_event context in task_struct
  5309. */
  5310. int perf_event_init_task(struct task_struct *child)
  5311. {
  5312. int ctxn, ret;
  5313. memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
  5314. mutex_init(&child->perf_event_mutex);
  5315. INIT_LIST_HEAD(&child->perf_event_list);
  5316. for_each_task_context_nr(ctxn) {
  5317. ret = perf_event_init_context(child, ctxn);
  5318. if (ret)
  5319. return ret;
  5320. }
  5321. return 0;
  5322. }
  5323. static void __init perf_event_init_all_cpus(void)
  5324. {
  5325. struct swevent_htable *swhash;
  5326. int cpu;
  5327. for_each_possible_cpu(cpu) {
  5328. swhash = &per_cpu(swevent_htable, cpu);
  5329. mutex_init(&swhash->hlist_mutex);
  5330. INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
  5331. }
  5332. }
  5333. static void __cpuinit perf_event_init_cpu(int cpu)
  5334. {
  5335. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5336. mutex_lock(&swhash->hlist_mutex);
  5337. if (swhash->hlist_refcount > 0) {
  5338. struct swevent_hlist *hlist;
  5339. hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
  5340. WARN_ON(!hlist);
  5341. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  5342. }
  5343. mutex_unlock(&swhash->hlist_mutex);
  5344. }
  5345. #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
  5346. static void perf_pmu_rotate_stop(struct pmu *pmu)
  5347. {
  5348. struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  5349. WARN_ON(!irqs_disabled());
  5350. list_del_init(&cpuctx->rotation_list);
  5351. }
  5352. static void __perf_event_exit_context(void *__info)
  5353. {
  5354. struct perf_event_context *ctx = __info;
  5355. struct perf_event *event, *tmp;
  5356. perf_pmu_rotate_stop(ctx->pmu);
  5357. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
  5358. __perf_event_remove_from_context(event);
  5359. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
  5360. __perf_event_remove_from_context(event);
  5361. }
  5362. static void perf_event_exit_cpu_context(int cpu)
  5363. {
  5364. struct perf_event_context *ctx;
  5365. struct pmu *pmu;
  5366. int idx;
  5367. idx = srcu_read_lock(&pmus_srcu);
  5368. list_for_each_entry_rcu(pmu, &pmus, entry) {
  5369. ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
  5370. mutex_lock(&ctx->mutex);
  5371. smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
  5372. mutex_unlock(&ctx->mutex);
  5373. }
  5374. srcu_read_unlock(&pmus_srcu, idx);
  5375. }
  5376. static void perf_event_exit_cpu(int cpu)
  5377. {
  5378. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5379. mutex_lock(&swhash->hlist_mutex);
  5380. swevent_hlist_release(swhash);
  5381. mutex_unlock(&swhash->hlist_mutex);
  5382. perf_event_exit_cpu_context(cpu);
  5383. }
  5384. #else
  5385. static inline void perf_event_exit_cpu(int cpu) { }
  5386. #endif
  5387. static int
  5388. perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
  5389. {
  5390. int cpu;
  5391. for_each_online_cpu(cpu)
  5392. perf_event_exit_cpu(cpu);
  5393. return NOTIFY_OK;
  5394. }
  5395. /*
  5396. * Run the perf reboot notifier at the very last possible moment so that
  5397. * the generic watchdog code runs as long as possible.
  5398. */
  5399. static struct notifier_block perf_reboot_notifier = {
  5400. .notifier_call = perf_reboot,
  5401. .priority = INT_MIN,
  5402. };
  5403. static int __cpuinit
  5404. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  5405. {
  5406. unsigned int cpu = (long)hcpu;
  5407. switch (action & ~CPU_TASKS_FROZEN) {
  5408. case CPU_UP_PREPARE:
  5409. case CPU_DOWN_FAILED:
  5410. perf_event_init_cpu(cpu);
  5411. break;
  5412. case CPU_UP_CANCELED:
  5413. case CPU_DOWN_PREPARE:
  5414. perf_event_exit_cpu(cpu);
  5415. break;
  5416. default:
  5417. break;
  5418. }
  5419. return NOTIFY_OK;
  5420. }
  5421. void __init perf_event_init(void)
  5422. {
  5423. int ret;
  5424. idr_init(&pmu_idr);
  5425. perf_event_init_all_cpus();
  5426. init_srcu_struct(&pmus_srcu);
  5427. perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
  5428. perf_pmu_register(&perf_cpu_clock, NULL, -1);
  5429. perf_pmu_register(&perf_task_clock, NULL, -1);
  5430. perf_tp_register();
  5431. perf_cpu_notifier(perf_cpu_notify);
  5432. register_reboot_notifier(&perf_reboot_notifier);
  5433. ret = init_hw_breakpoint();
  5434. WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
  5435. }
  5436. static int __init perf_event_sysfs_init(void)
  5437. {
  5438. struct pmu *pmu;
  5439. int ret;
  5440. mutex_lock(&pmus_lock);
  5441. ret = bus_register(&pmu_bus);
  5442. if (ret)
  5443. goto unlock;
  5444. list_for_each_entry(pmu, &pmus, entry) {
  5445. if (!pmu->name || pmu->type < 0)
  5446. continue;
  5447. ret = pmu_dev_alloc(pmu);
  5448. WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
  5449. }
  5450. pmu_bus_running = 1;
  5451. ret = 0;
  5452. unlock:
  5453. mutex_unlock(&pmus_lock);
  5454. return ret;
  5455. }
  5456. device_initcall(perf_event_sysfs_init);