perf_event.c 149 KB

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