core.c 160 KB

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