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