core.c 160 KB

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