core.c 164 KB

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