core.c 160 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002
  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. /*
  2776. * Callers need to ensure there can be no nesting of this function, otherwise
  2777. * the seqlock logic goes bad. We can not serialize this because the arch
  2778. * code calls this from NMI context.
  2779. */
  2780. void perf_event_update_userpage(struct perf_event *event)
  2781. {
  2782. struct perf_event_mmap_page *userpg;
  2783. struct ring_buffer *rb;
  2784. rcu_read_lock();
  2785. rb = rcu_dereference(event->rb);
  2786. if (!rb)
  2787. goto unlock;
  2788. userpg = rb->user_page;
  2789. /*
  2790. * Disable preemption so as to not let the corresponding user-space
  2791. * spin too long if we get preempted.
  2792. */
  2793. preempt_disable();
  2794. ++userpg->lock;
  2795. barrier();
  2796. userpg->index = perf_event_index(event);
  2797. userpg->offset = perf_event_count(event);
  2798. if (event->state == PERF_EVENT_STATE_ACTIVE)
  2799. userpg->offset -= local64_read(&event->hw.prev_count);
  2800. userpg->time_enabled = event->total_time_enabled +
  2801. atomic64_read(&event->child_total_time_enabled);
  2802. userpg->time_running = event->total_time_running +
  2803. atomic64_read(&event->child_total_time_running);
  2804. barrier();
  2805. ++userpg->lock;
  2806. preempt_enable();
  2807. unlock:
  2808. rcu_read_unlock();
  2809. }
  2810. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  2811. {
  2812. struct perf_event *event = vma->vm_file->private_data;
  2813. struct ring_buffer *rb;
  2814. int ret = VM_FAULT_SIGBUS;
  2815. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  2816. if (vmf->pgoff == 0)
  2817. ret = 0;
  2818. return ret;
  2819. }
  2820. rcu_read_lock();
  2821. rb = rcu_dereference(event->rb);
  2822. if (!rb)
  2823. goto unlock;
  2824. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  2825. goto unlock;
  2826. vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
  2827. if (!vmf->page)
  2828. goto unlock;
  2829. get_page(vmf->page);
  2830. vmf->page->mapping = vma->vm_file->f_mapping;
  2831. vmf->page->index = vmf->pgoff;
  2832. ret = 0;
  2833. unlock:
  2834. rcu_read_unlock();
  2835. return ret;
  2836. }
  2837. static void rb_free_rcu(struct rcu_head *rcu_head)
  2838. {
  2839. struct ring_buffer *rb;
  2840. rb = container_of(rcu_head, struct ring_buffer, rcu_head);
  2841. rb_free(rb);
  2842. }
  2843. static struct ring_buffer *ring_buffer_get(struct perf_event *event)
  2844. {
  2845. struct ring_buffer *rb;
  2846. rcu_read_lock();
  2847. rb = rcu_dereference(event->rb);
  2848. if (rb) {
  2849. if (!atomic_inc_not_zero(&rb->refcount))
  2850. rb = NULL;
  2851. }
  2852. rcu_read_unlock();
  2853. return rb;
  2854. }
  2855. static void ring_buffer_put(struct ring_buffer *rb)
  2856. {
  2857. if (!atomic_dec_and_test(&rb->refcount))
  2858. return;
  2859. call_rcu(&rb->rcu_head, rb_free_rcu);
  2860. }
  2861. static void perf_mmap_open(struct vm_area_struct *vma)
  2862. {
  2863. struct perf_event *event = vma->vm_file->private_data;
  2864. atomic_inc(&event->mmap_count);
  2865. }
  2866. static void perf_mmap_close(struct vm_area_struct *vma)
  2867. {
  2868. struct perf_event *event = vma->vm_file->private_data;
  2869. if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
  2870. unsigned long size = perf_data_size(event->rb);
  2871. struct user_struct *user = event->mmap_user;
  2872. struct ring_buffer *rb = event->rb;
  2873. atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
  2874. vma->vm_mm->locked_vm -= event->mmap_locked;
  2875. rcu_assign_pointer(event->rb, NULL);
  2876. mutex_unlock(&event->mmap_mutex);
  2877. ring_buffer_put(rb);
  2878. free_uid(user);
  2879. }
  2880. }
  2881. static const struct vm_operations_struct perf_mmap_vmops = {
  2882. .open = perf_mmap_open,
  2883. .close = perf_mmap_close,
  2884. .fault = perf_mmap_fault,
  2885. .page_mkwrite = perf_mmap_fault,
  2886. };
  2887. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  2888. {
  2889. struct perf_event *event = file->private_data;
  2890. unsigned long user_locked, user_lock_limit;
  2891. struct user_struct *user = current_user();
  2892. unsigned long locked, lock_limit;
  2893. struct ring_buffer *rb;
  2894. unsigned long vma_size;
  2895. unsigned long nr_pages;
  2896. long user_extra, extra;
  2897. int ret = 0, flags = 0;
  2898. /*
  2899. * Don't allow mmap() of inherited per-task counters. This would
  2900. * create a performance issue due to all children writing to the
  2901. * same rb.
  2902. */
  2903. if (event->cpu == -1 && event->attr.inherit)
  2904. return -EINVAL;
  2905. if (!(vma->vm_flags & VM_SHARED))
  2906. return -EINVAL;
  2907. vma_size = vma->vm_end - vma->vm_start;
  2908. nr_pages = (vma_size / PAGE_SIZE) - 1;
  2909. /*
  2910. * If we have rb pages ensure they're a power-of-two number, so we
  2911. * can do bitmasks instead of modulo.
  2912. */
  2913. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  2914. return -EINVAL;
  2915. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  2916. return -EINVAL;
  2917. if (vma->vm_pgoff != 0)
  2918. return -EINVAL;
  2919. WARN_ON_ONCE(event->ctx->parent_ctx);
  2920. mutex_lock(&event->mmap_mutex);
  2921. if (event->rb) {
  2922. if (event->rb->nr_pages == nr_pages)
  2923. atomic_inc(&event->rb->refcount);
  2924. else
  2925. ret = -EINVAL;
  2926. goto unlock;
  2927. }
  2928. user_extra = nr_pages + 1;
  2929. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  2930. /*
  2931. * Increase the limit linearly with more CPUs:
  2932. */
  2933. user_lock_limit *= num_online_cpus();
  2934. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  2935. extra = 0;
  2936. if (user_locked > user_lock_limit)
  2937. extra = user_locked - user_lock_limit;
  2938. lock_limit = rlimit(RLIMIT_MEMLOCK);
  2939. lock_limit >>= PAGE_SHIFT;
  2940. locked = vma->vm_mm->locked_vm + extra;
  2941. if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  2942. !capable(CAP_IPC_LOCK)) {
  2943. ret = -EPERM;
  2944. goto unlock;
  2945. }
  2946. WARN_ON(event->rb);
  2947. if (vma->vm_flags & VM_WRITE)
  2948. flags |= RING_BUFFER_WRITABLE;
  2949. rb = rb_alloc(nr_pages,
  2950. event->attr.watermark ? event->attr.wakeup_watermark : 0,
  2951. event->cpu, flags);
  2952. if (!rb) {
  2953. ret = -ENOMEM;
  2954. goto unlock;
  2955. }
  2956. rcu_assign_pointer(event->rb, rb);
  2957. atomic_long_add(user_extra, &user->locked_vm);
  2958. event->mmap_locked = extra;
  2959. event->mmap_user = get_current_user();
  2960. vma->vm_mm->locked_vm += event->mmap_locked;
  2961. unlock:
  2962. if (!ret)
  2963. atomic_inc(&event->mmap_count);
  2964. mutex_unlock(&event->mmap_mutex);
  2965. vma->vm_flags |= VM_RESERVED;
  2966. vma->vm_ops = &perf_mmap_vmops;
  2967. return ret;
  2968. }
  2969. static int perf_fasync(int fd, struct file *filp, int on)
  2970. {
  2971. struct inode *inode = filp->f_path.dentry->d_inode;
  2972. struct perf_event *event = filp->private_data;
  2973. int retval;
  2974. mutex_lock(&inode->i_mutex);
  2975. retval = fasync_helper(fd, filp, on, &event->fasync);
  2976. mutex_unlock(&inode->i_mutex);
  2977. if (retval < 0)
  2978. return retval;
  2979. return 0;
  2980. }
  2981. static const struct file_operations perf_fops = {
  2982. .llseek = no_llseek,
  2983. .release = perf_release,
  2984. .read = perf_read,
  2985. .poll = perf_poll,
  2986. .unlocked_ioctl = perf_ioctl,
  2987. .compat_ioctl = perf_ioctl,
  2988. .mmap = perf_mmap,
  2989. .fasync = perf_fasync,
  2990. };
  2991. /*
  2992. * Perf event wakeup
  2993. *
  2994. * If there's data, ensure we set the poll() state and publish everything
  2995. * to user-space before waking everybody up.
  2996. */
  2997. void perf_event_wakeup(struct perf_event *event)
  2998. {
  2999. wake_up_all(&event->waitq);
  3000. if (event->pending_kill) {
  3001. kill_fasync(&event->fasync, SIGIO, event->pending_kill);
  3002. event->pending_kill = 0;
  3003. }
  3004. }
  3005. static void perf_pending_event(struct irq_work *entry)
  3006. {
  3007. struct perf_event *event = container_of(entry,
  3008. struct perf_event, pending);
  3009. if (event->pending_disable) {
  3010. event->pending_disable = 0;
  3011. __perf_event_disable(event);
  3012. }
  3013. if (event->pending_wakeup) {
  3014. event->pending_wakeup = 0;
  3015. perf_event_wakeup(event);
  3016. }
  3017. }
  3018. /*
  3019. * We assume there is only KVM supporting the callbacks.
  3020. * Later on, we might change it to a list if there is
  3021. * another virtualization implementation supporting the callbacks.
  3022. */
  3023. struct perf_guest_info_callbacks *perf_guest_cbs;
  3024. int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  3025. {
  3026. perf_guest_cbs = cbs;
  3027. return 0;
  3028. }
  3029. EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
  3030. int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  3031. {
  3032. perf_guest_cbs = NULL;
  3033. return 0;
  3034. }
  3035. EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
  3036. static void __perf_event_header__init_id(struct perf_event_header *header,
  3037. struct perf_sample_data *data,
  3038. struct perf_event *event)
  3039. {
  3040. u64 sample_type = event->attr.sample_type;
  3041. data->type = sample_type;
  3042. header->size += event->id_header_size;
  3043. if (sample_type & PERF_SAMPLE_TID) {
  3044. /* namespace issues */
  3045. data->tid_entry.pid = perf_event_pid(event, current);
  3046. data->tid_entry.tid = perf_event_tid(event, current);
  3047. }
  3048. if (sample_type & PERF_SAMPLE_TIME)
  3049. data->time = perf_clock();
  3050. if (sample_type & PERF_SAMPLE_ID)
  3051. data->id = primary_event_id(event);
  3052. if (sample_type & PERF_SAMPLE_STREAM_ID)
  3053. data->stream_id = event->id;
  3054. if (sample_type & PERF_SAMPLE_CPU) {
  3055. data->cpu_entry.cpu = raw_smp_processor_id();
  3056. data->cpu_entry.reserved = 0;
  3057. }
  3058. }
  3059. void perf_event_header__init_id(struct perf_event_header *header,
  3060. struct perf_sample_data *data,
  3061. struct perf_event *event)
  3062. {
  3063. if (event->attr.sample_id_all)
  3064. __perf_event_header__init_id(header, data, event);
  3065. }
  3066. static void __perf_event__output_id_sample(struct perf_output_handle *handle,
  3067. struct perf_sample_data *data)
  3068. {
  3069. u64 sample_type = data->type;
  3070. if (sample_type & PERF_SAMPLE_TID)
  3071. perf_output_put(handle, data->tid_entry);
  3072. if (sample_type & PERF_SAMPLE_TIME)
  3073. perf_output_put(handle, data->time);
  3074. if (sample_type & PERF_SAMPLE_ID)
  3075. perf_output_put(handle, data->id);
  3076. if (sample_type & PERF_SAMPLE_STREAM_ID)
  3077. perf_output_put(handle, data->stream_id);
  3078. if (sample_type & PERF_SAMPLE_CPU)
  3079. perf_output_put(handle, data->cpu_entry);
  3080. }
  3081. void perf_event__output_id_sample(struct perf_event *event,
  3082. struct perf_output_handle *handle,
  3083. struct perf_sample_data *sample)
  3084. {
  3085. if (event->attr.sample_id_all)
  3086. __perf_event__output_id_sample(handle, sample);
  3087. }
  3088. static void perf_output_read_one(struct perf_output_handle *handle,
  3089. struct perf_event *event,
  3090. u64 enabled, u64 running)
  3091. {
  3092. u64 read_format = event->attr.read_format;
  3093. u64 values[4];
  3094. int n = 0;
  3095. values[n++] = perf_event_count(event);
  3096. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  3097. values[n++] = enabled +
  3098. atomic64_read(&event->child_total_time_enabled);
  3099. }
  3100. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  3101. values[n++] = running +
  3102. atomic64_read(&event->child_total_time_running);
  3103. }
  3104. if (read_format & PERF_FORMAT_ID)
  3105. values[n++] = primary_event_id(event);
  3106. __output_copy(handle, values, n * sizeof(u64));
  3107. }
  3108. /*
  3109. * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
  3110. */
  3111. static void perf_output_read_group(struct perf_output_handle *handle,
  3112. struct perf_event *event,
  3113. u64 enabled, u64 running)
  3114. {
  3115. struct perf_event *leader = event->group_leader, *sub;
  3116. u64 read_format = event->attr.read_format;
  3117. u64 values[5];
  3118. int n = 0;
  3119. values[n++] = 1 + leader->nr_siblings;
  3120. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  3121. values[n++] = enabled;
  3122. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  3123. values[n++] = running;
  3124. if (leader != event)
  3125. leader->pmu->read(leader);
  3126. values[n++] = perf_event_count(leader);
  3127. if (read_format & PERF_FORMAT_ID)
  3128. values[n++] = primary_event_id(leader);
  3129. __output_copy(handle, values, n * sizeof(u64));
  3130. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  3131. n = 0;
  3132. if (sub != event)
  3133. sub->pmu->read(sub);
  3134. values[n++] = perf_event_count(sub);
  3135. if (read_format & PERF_FORMAT_ID)
  3136. values[n++] = primary_event_id(sub);
  3137. __output_copy(handle, values, n * sizeof(u64));
  3138. }
  3139. }
  3140. #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
  3141. PERF_FORMAT_TOTAL_TIME_RUNNING)
  3142. static void perf_output_read(struct perf_output_handle *handle,
  3143. struct perf_event *event)
  3144. {
  3145. u64 enabled = 0, running = 0, now, ctx_time;
  3146. u64 read_format = event->attr.read_format;
  3147. /*
  3148. * compute total_time_enabled, total_time_running
  3149. * based on snapshot values taken when the event
  3150. * was last scheduled in.
  3151. *
  3152. * we cannot simply called update_context_time()
  3153. * because of locking issue as we are called in
  3154. * NMI context
  3155. */
  3156. if (read_format & PERF_FORMAT_TOTAL_TIMES) {
  3157. now = perf_clock();
  3158. ctx_time = event->shadow_ctx_time + now;
  3159. enabled = ctx_time - event->tstamp_enabled;
  3160. running = ctx_time - event->tstamp_running;
  3161. }
  3162. if (event->attr.read_format & PERF_FORMAT_GROUP)
  3163. perf_output_read_group(handle, event, enabled, running);
  3164. else
  3165. perf_output_read_one(handle, event, enabled, running);
  3166. }
  3167. void perf_output_sample(struct perf_output_handle *handle,
  3168. struct perf_event_header *header,
  3169. struct perf_sample_data *data,
  3170. struct perf_event *event)
  3171. {
  3172. u64 sample_type = data->type;
  3173. perf_output_put(handle, *header);
  3174. if (sample_type & PERF_SAMPLE_IP)
  3175. perf_output_put(handle, data->ip);
  3176. if (sample_type & PERF_SAMPLE_TID)
  3177. perf_output_put(handle, data->tid_entry);
  3178. if (sample_type & PERF_SAMPLE_TIME)
  3179. perf_output_put(handle, data->time);
  3180. if (sample_type & PERF_SAMPLE_ADDR)
  3181. perf_output_put(handle, data->addr);
  3182. if (sample_type & PERF_SAMPLE_ID)
  3183. perf_output_put(handle, data->id);
  3184. if (sample_type & PERF_SAMPLE_STREAM_ID)
  3185. perf_output_put(handle, data->stream_id);
  3186. if (sample_type & PERF_SAMPLE_CPU)
  3187. perf_output_put(handle, data->cpu_entry);
  3188. if (sample_type & PERF_SAMPLE_PERIOD)
  3189. perf_output_put(handle, data->period);
  3190. if (sample_type & PERF_SAMPLE_READ)
  3191. perf_output_read(handle, event);
  3192. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  3193. if (data->callchain) {
  3194. int size = 1;
  3195. if (data->callchain)
  3196. size += data->callchain->nr;
  3197. size *= sizeof(u64);
  3198. __output_copy(handle, data->callchain, size);
  3199. } else {
  3200. u64 nr = 0;
  3201. perf_output_put(handle, nr);
  3202. }
  3203. }
  3204. if (sample_type & PERF_SAMPLE_RAW) {
  3205. if (data->raw) {
  3206. perf_output_put(handle, data->raw->size);
  3207. __output_copy(handle, data->raw->data,
  3208. data->raw->size);
  3209. } else {
  3210. struct {
  3211. u32 size;
  3212. u32 data;
  3213. } raw = {
  3214. .size = sizeof(u32),
  3215. .data = 0,
  3216. };
  3217. perf_output_put(handle, raw);
  3218. }
  3219. }
  3220. }
  3221. void perf_prepare_sample(struct perf_event_header *header,
  3222. struct perf_sample_data *data,
  3223. struct perf_event *event,
  3224. struct pt_regs *regs)
  3225. {
  3226. u64 sample_type = event->attr.sample_type;
  3227. header->type = PERF_RECORD_SAMPLE;
  3228. header->size = sizeof(*header) + event->header_size;
  3229. header->misc = 0;
  3230. header->misc |= perf_misc_flags(regs);
  3231. __perf_event_header__init_id(header, data, event);
  3232. if (sample_type & PERF_SAMPLE_IP)
  3233. data->ip = perf_instruction_pointer(regs);
  3234. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  3235. int size = 1;
  3236. data->callchain = perf_callchain(regs);
  3237. if (data->callchain)
  3238. size += data->callchain->nr;
  3239. header->size += size * sizeof(u64);
  3240. }
  3241. if (sample_type & PERF_SAMPLE_RAW) {
  3242. int size = sizeof(u32);
  3243. if (data->raw)
  3244. size += data->raw->size;
  3245. else
  3246. size += sizeof(u32);
  3247. WARN_ON_ONCE(size & (sizeof(u64)-1));
  3248. header->size += size;
  3249. }
  3250. }
  3251. static void perf_event_output(struct perf_event *event, int nmi,
  3252. struct perf_sample_data *data,
  3253. struct pt_regs *regs)
  3254. {
  3255. struct perf_output_handle handle;
  3256. struct perf_event_header header;
  3257. /* protect the callchain buffers */
  3258. rcu_read_lock();
  3259. perf_prepare_sample(&header, data, event, regs);
  3260. if (perf_output_begin(&handle, event, header.size, nmi, 1))
  3261. goto exit;
  3262. perf_output_sample(&handle, &header, data, event);
  3263. perf_output_end(&handle);
  3264. exit:
  3265. rcu_read_unlock();
  3266. }
  3267. /*
  3268. * read event_id
  3269. */
  3270. struct perf_read_event {
  3271. struct perf_event_header header;
  3272. u32 pid;
  3273. u32 tid;
  3274. };
  3275. static void
  3276. perf_event_read_event(struct perf_event *event,
  3277. struct task_struct *task)
  3278. {
  3279. struct perf_output_handle handle;
  3280. struct perf_sample_data sample;
  3281. struct perf_read_event read_event = {
  3282. .header = {
  3283. .type = PERF_RECORD_READ,
  3284. .misc = 0,
  3285. .size = sizeof(read_event) + event->read_size,
  3286. },
  3287. .pid = perf_event_pid(event, task),
  3288. .tid = perf_event_tid(event, task),
  3289. };
  3290. int ret;
  3291. perf_event_header__init_id(&read_event.header, &sample, event);
  3292. ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
  3293. if (ret)
  3294. return;
  3295. perf_output_put(&handle, read_event);
  3296. perf_output_read(&handle, event);
  3297. perf_event__output_id_sample(event, &handle, &sample);
  3298. perf_output_end(&handle);
  3299. }
  3300. /*
  3301. * task tracking -- fork/exit
  3302. *
  3303. * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
  3304. */
  3305. struct perf_task_event {
  3306. struct task_struct *task;
  3307. struct perf_event_context *task_ctx;
  3308. struct {
  3309. struct perf_event_header header;
  3310. u32 pid;
  3311. u32 ppid;
  3312. u32 tid;
  3313. u32 ptid;
  3314. u64 time;
  3315. } event_id;
  3316. };
  3317. static void perf_event_task_output(struct perf_event *event,
  3318. struct perf_task_event *task_event)
  3319. {
  3320. struct perf_output_handle handle;
  3321. struct perf_sample_data sample;
  3322. struct task_struct *task = task_event->task;
  3323. int ret, size = task_event->event_id.header.size;
  3324. perf_event_header__init_id(&task_event->event_id.header, &sample, event);
  3325. ret = perf_output_begin(&handle, event,
  3326. task_event->event_id.header.size, 0, 0);
  3327. if (ret)
  3328. goto out;
  3329. task_event->event_id.pid = perf_event_pid(event, task);
  3330. task_event->event_id.ppid = perf_event_pid(event, current);
  3331. task_event->event_id.tid = perf_event_tid(event, task);
  3332. task_event->event_id.ptid = perf_event_tid(event, current);
  3333. perf_output_put(&handle, task_event->event_id);
  3334. perf_event__output_id_sample(event, &handle, &sample);
  3335. perf_output_end(&handle);
  3336. out:
  3337. task_event->event_id.header.size = size;
  3338. }
  3339. static int perf_event_task_match(struct perf_event *event)
  3340. {
  3341. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3342. return 0;
  3343. if (!event_filter_match(event))
  3344. return 0;
  3345. if (event->attr.comm || event->attr.mmap ||
  3346. event->attr.mmap_data || event->attr.task)
  3347. return 1;
  3348. return 0;
  3349. }
  3350. static void perf_event_task_ctx(struct perf_event_context *ctx,
  3351. struct perf_task_event *task_event)
  3352. {
  3353. struct perf_event *event;
  3354. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3355. if (perf_event_task_match(event))
  3356. perf_event_task_output(event, task_event);
  3357. }
  3358. }
  3359. static void perf_event_task_event(struct perf_task_event *task_event)
  3360. {
  3361. struct perf_cpu_context *cpuctx;
  3362. struct perf_event_context *ctx;
  3363. struct pmu *pmu;
  3364. int ctxn;
  3365. rcu_read_lock();
  3366. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3367. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3368. if (cpuctx->active_pmu != pmu)
  3369. goto next;
  3370. perf_event_task_ctx(&cpuctx->ctx, task_event);
  3371. ctx = task_event->task_ctx;
  3372. if (!ctx) {
  3373. ctxn = pmu->task_ctx_nr;
  3374. if (ctxn < 0)
  3375. goto next;
  3376. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3377. }
  3378. if (ctx)
  3379. perf_event_task_ctx(ctx, task_event);
  3380. next:
  3381. put_cpu_ptr(pmu->pmu_cpu_context);
  3382. }
  3383. rcu_read_unlock();
  3384. }
  3385. static void perf_event_task(struct task_struct *task,
  3386. struct perf_event_context *task_ctx,
  3387. int new)
  3388. {
  3389. struct perf_task_event task_event;
  3390. if (!atomic_read(&nr_comm_events) &&
  3391. !atomic_read(&nr_mmap_events) &&
  3392. !atomic_read(&nr_task_events))
  3393. return;
  3394. task_event = (struct perf_task_event){
  3395. .task = task,
  3396. .task_ctx = task_ctx,
  3397. .event_id = {
  3398. .header = {
  3399. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  3400. .misc = 0,
  3401. .size = sizeof(task_event.event_id),
  3402. },
  3403. /* .pid */
  3404. /* .ppid */
  3405. /* .tid */
  3406. /* .ptid */
  3407. .time = perf_clock(),
  3408. },
  3409. };
  3410. perf_event_task_event(&task_event);
  3411. }
  3412. void perf_event_fork(struct task_struct *task)
  3413. {
  3414. perf_event_task(task, NULL, 1);
  3415. }
  3416. /*
  3417. * comm tracking
  3418. */
  3419. struct perf_comm_event {
  3420. struct task_struct *task;
  3421. char *comm;
  3422. int comm_size;
  3423. struct {
  3424. struct perf_event_header header;
  3425. u32 pid;
  3426. u32 tid;
  3427. } event_id;
  3428. };
  3429. static void perf_event_comm_output(struct perf_event *event,
  3430. struct perf_comm_event *comm_event)
  3431. {
  3432. struct perf_output_handle handle;
  3433. struct perf_sample_data sample;
  3434. int size = comm_event->event_id.header.size;
  3435. int ret;
  3436. perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
  3437. ret = perf_output_begin(&handle, event,
  3438. comm_event->event_id.header.size, 0, 0);
  3439. if (ret)
  3440. goto out;
  3441. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  3442. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  3443. perf_output_put(&handle, comm_event->event_id);
  3444. __output_copy(&handle, comm_event->comm,
  3445. comm_event->comm_size);
  3446. perf_event__output_id_sample(event, &handle, &sample);
  3447. perf_output_end(&handle);
  3448. out:
  3449. comm_event->event_id.header.size = size;
  3450. }
  3451. static int perf_event_comm_match(struct perf_event *event)
  3452. {
  3453. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3454. return 0;
  3455. if (!event_filter_match(event))
  3456. return 0;
  3457. if (event->attr.comm)
  3458. return 1;
  3459. return 0;
  3460. }
  3461. static void perf_event_comm_ctx(struct perf_event_context *ctx,
  3462. struct perf_comm_event *comm_event)
  3463. {
  3464. struct perf_event *event;
  3465. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3466. if (perf_event_comm_match(event))
  3467. perf_event_comm_output(event, comm_event);
  3468. }
  3469. }
  3470. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  3471. {
  3472. struct perf_cpu_context *cpuctx;
  3473. struct perf_event_context *ctx;
  3474. char comm[TASK_COMM_LEN];
  3475. unsigned int size;
  3476. struct pmu *pmu;
  3477. int ctxn;
  3478. memset(comm, 0, sizeof(comm));
  3479. strlcpy(comm, comm_event->task->comm, sizeof(comm));
  3480. size = ALIGN(strlen(comm)+1, sizeof(u64));
  3481. comm_event->comm = comm;
  3482. comm_event->comm_size = size;
  3483. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  3484. rcu_read_lock();
  3485. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3486. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3487. if (cpuctx->active_pmu != pmu)
  3488. goto next;
  3489. perf_event_comm_ctx(&cpuctx->ctx, comm_event);
  3490. ctxn = pmu->task_ctx_nr;
  3491. if (ctxn < 0)
  3492. goto next;
  3493. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3494. if (ctx)
  3495. perf_event_comm_ctx(ctx, comm_event);
  3496. next:
  3497. put_cpu_ptr(pmu->pmu_cpu_context);
  3498. }
  3499. rcu_read_unlock();
  3500. }
  3501. void perf_event_comm(struct task_struct *task)
  3502. {
  3503. struct perf_comm_event comm_event;
  3504. struct perf_event_context *ctx;
  3505. int ctxn;
  3506. for_each_task_context_nr(ctxn) {
  3507. ctx = task->perf_event_ctxp[ctxn];
  3508. if (!ctx)
  3509. continue;
  3510. perf_event_enable_on_exec(ctx);
  3511. }
  3512. if (!atomic_read(&nr_comm_events))
  3513. return;
  3514. comm_event = (struct perf_comm_event){
  3515. .task = task,
  3516. /* .comm */
  3517. /* .comm_size */
  3518. .event_id = {
  3519. .header = {
  3520. .type = PERF_RECORD_COMM,
  3521. .misc = 0,
  3522. /* .size */
  3523. },
  3524. /* .pid */
  3525. /* .tid */
  3526. },
  3527. };
  3528. perf_event_comm_event(&comm_event);
  3529. }
  3530. /*
  3531. * mmap tracking
  3532. */
  3533. struct perf_mmap_event {
  3534. struct vm_area_struct *vma;
  3535. const char *file_name;
  3536. int file_size;
  3537. struct {
  3538. struct perf_event_header header;
  3539. u32 pid;
  3540. u32 tid;
  3541. u64 start;
  3542. u64 len;
  3543. u64 pgoff;
  3544. } event_id;
  3545. };
  3546. static void perf_event_mmap_output(struct perf_event *event,
  3547. struct perf_mmap_event *mmap_event)
  3548. {
  3549. struct perf_output_handle handle;
  3550. struct perf_sample_data sample;
  3551. int size = mmap_event->event_id.header.size;
  3552. int ret;
  3553. perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
  3554. ret = perf_output_begin(&handle, event,
  3555. mmap_event->event_id.header.size, 0, 0);
  3556. if (ret)
  3557. goto out;
  3558. mmap_event->event_id.pid = perf_event_pid(event, current);
  3559. mmap_event->event_id.tid = perf_event_tid(event, current);
  3560. perf_output_put(&handle, mmap_event->event_id);
  3561. __output_copy(&handle, mmap_event->file_name,
  3562. mmap_event->file_size);
  3563. perf_event__output_id_sample(event, &handle, &sample);
  3564. perf_output_end(&handle);
  3565. out:
  3566. mmap_event->event_id.header.size = size;
  3567. }
  3568. static int perf_event_mmap_match(struct perf_event *event,
  3569. struct perf_mmap_event *mmap_event,
  3570. int executable)
  3571. {
  3572. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3573. return 0;
  3574. if (!event_filter_match(event))
  3575. return 0;
  3576. if ((!executable && event->attr.mmap_data) ||
  3577. (executable && event->attr.mmap))
  3578. return 1;
  3579. return 0;
  3580. }
  3581. static void perf_event_mmap_ctx(struct perf_event_context *ctx,
  3582. struct perf_mmap_event *mmap_event,
  3583. int executable)
  3584. {
  3585. struct perf_event *event;
  3586. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3587. if (perf_event_mmap_match(event, mmap_event, executable))
  3588. perf_event_mmap_output(event, mmap_event);
  3589. }
  3590. }
  3591. static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
  3592. {
  3593. struct perf_cpu_context *cpuctx;
  3594. struct perf_event_context *ctx;
  3595. struct vm_area_struct *vma = mmap_event->vma;
  3596. struct file *file = vma->vm_file;
  3597. unsigned int size;
  3598. char tmp[16];
  3599. char *buf = NULL;
  3600. const char *name;
  3601. struct pmu *pmu;
  3602. int ctxn;
  3603. memset(tmp, 0, sizeof(tmp));
  3604. if (file) {
  3605. /*
  3606. * d_path works from the end of the rb backwards, so we
  3607. * need to add enough zero bytes after the string to handle
  3608. * the 64bit alignment we do later.
  3609. */
  3610. buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
  3611. if (!buf) {
  3612. name = strncpy(tmp, "//enomem", sizeof(tmp));
  3613. goto got_name;
  3614. }
  3615. name = d_path(&file->f_path, buf, PATH_MAX);
  3616. if (IS_ERR(name)) {
  3617. name = strncpy(tmp, "//toolong", sizeof(tmp));
  3618. goto got_name;
  3619. }
  3620. } else {
  3621. if (arch_vma_name(mmap_event->vma)) {
  3622. name = strncpy(tmp, arch_vma_name(mmap_event->vma),
  3623. sizeof(tmp));
  3624. goto got_name;
  3625. }
  3626. if (!vma->vm_mm) {
  3627. name = strncpy(tmp, "[vdso]", sizeof(tmp));
  3628. goto got_name;
  3629. } else if (vma->vm_start <= vma->vm_mm->start_brk &&
  3630. vma->vm_end >= vma->vm_mm->brk) {
  3631. name = strncpy(tmp, "[heap]", sizeof(tmp));
  3632. goto got_name;
  3633. } else if (vma->vm_start <= vma->vm_mm->start_stack &&
  3634. vma->vm_end >= vma->vm_mm->start_stack) {
  3635. name = strncpy(tmp, "[stack]", sizeof(tmp));
  3636. goto got_name;
  3637. }
  3638. name = strncpy(tmp, "//anon", sizeof(tmp));
  3639. goto got_name;
  3640. }
  3641. got_name:
  3642. size = ALIGN(strlen(name)+1, sizeof(u64));
  3643. mmap_event->file_name = name;
  3644. mmap_event->file_size = size;
  3645. mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
  3646. rcu_read_lock();
  3647. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3648. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3649. if (cpuctx->active_pmu != pmu)
  3650. goto next;
  3651. perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
  3652. vma->vm_flags & VM_EXEC);
  3653. ctxn = pmu->task_ctx_nr;
  3654. if (ctxn < 0)
  3655. goto next;
  3656. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3657. if (ctx) {
  3658. perf_event_mmap_ctx(ctx, mmap_event,
  3659. vma->vm_flags & VM_EXEC);
  3660. }
  3661. next:
  3662. put_cpu_ptr(pmu->pmu_cpu_context);
  3663. }
  3664. rcu_read_unlock();
  3665. kfree(buf);
  3666. }
  3667. void perf_event_mmap(struct vm_area_struct *vma)
  3668. {
  3669. struct perf_mmap_event mmap_event;
  3670. if (!atomic_read(&nr_mmap_events))
  3671. return;
  3672. mmap_event = (struct perf_mmap_event){
  3673. .vma = vma,
  3674. /* .file_name */
  3675. /* .file_size */
  3676. .event_id = {
  3677. .header = {
  3678. .type = PERF_RECORD_MMAP,
  3679. .misc = PERF_RECORD_MISC_USER,
  3680. /* .size */
  3681. },
  3682. /* .pid */
  3683. /* .tid */
  3684. .start = vma->vm_start,
  3685. .len = vma->vm_end - vma->vm_start,
  3686. .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
  3687. },
  3688. };
  3689. perf_event_mmap_event(&mmap_event);
  3690. }
  3691. /*
  3692. * IRQ throttle logging
  3693. */
  3694. static void perf_log_throttle(struct perf_event *event, int enable)
  3695. {
  3696. struct perf_output_handle handle;
  3697. struct perf_sample_data sample;
  3698. int ret;
  3699. struct {
  3700. struct perf_event_header header;
  3701. u64 time;
  3702. u64 id;
  3703. u64 stream_id;
  3704. } throttle_event = {
  3705. .header = {
  3706. .type = PERF_RECORD_THROTTLE,
  3707. .misc = 0,
  3708. .size = sizeof(throttle_event),
  3709. },
  3710. .time = perf_clock(),
  3711. .id = primary_event_id(event),
  3712. .stream_id = event->id,
  3713. };
  3714. if (enable)
  3715. throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
  3716. perf_event_header__init_id(&throttle_event.header, &sample, event);
  3717. ret = perf_output_begin(&handle, event,
  3718. throttle_event.header.size, 1, 0);
  3719. if (ret)
  3720. return;
  3721. perf_output_put(&handle, throttle_event);
  3722. perf_event__output_id_sample(event, &handle, &sample);
  3723. perf_output_end(&handle);
  3724. }
  3725. /*
  3726. * Generic event overflow handling, sampling.
  3727. */
  3728. static int __perf_event_overflow(struct perf_event *event, int nmi,
  3729. int throttle, struct perf_sample_data *data,
  3730. struct pt_regs *regs)
  3731. {
  3732. int events = atomic_read(&event->event_limit);
  3733. struct hw_perf_event *hwc = &event->hw;
  3734. int ret = 0;
  3735. /*
  3736. * Non-sampling counters might still use the PMI to fold short
  3737. * hardware counters, ignore those.
  3738. */
  3739. if (unlikely(!is_sampling_event(event)))
  3740. return 0;
  3741. if (unlikely(hwc->interrupts >= max_samples_per_tick)) {
  3742. if (throttle) {
  3743. hwc->interrupts = MAX_INTERRUPTS;
  3744. perf_log_throttle(event, 0);
  3745. ret = 1;
  3746. }
  3747. } else
  3748. hwc->interrupts++;
  3749. if (event->attr.freq) {
  3750. u64 now = perf_clock();
  3751. s64 delta = now - hwc->freq_time_stamp;
  3752. hwc->freq_time_stamp = now;
  3753. if (delta > 0 && delta < 2*TICK_NSEC)
  3754. perf_adjust_period(event, delta, hwc->last_period);
  3755. }
  3756. /*
  3757. * XXX event_limit might not quite work as expected on inherited
  3758. * events
  3759. */
  3760. event->pending_kill = POLL_IN;
  3761. if (events && atomic_dec_and_test(&event->event_limit)) {
  3762. ret = 1;
  3763. event->pending_kill = POLL_HUP;
  3764. if (nmi) {
  3765. event->pending_disable = 1;
  3766. irq_work_queue(&event->pending);
  3767. } else
  3768. perf_event_disable(event);
  3769. }
  3770. if (event->overflow_handler)
  3771. event->overflow_handler(event, nmi, data, regs);
  3772. else
  3773. perf_event_output(event, nmi, data, regs);
  3774. if (event->fasync && event->pending_kill) {
  3775. if (nmi) {
  3776. event->pending_wakeup = 1;
  3777. irq_work_queue(&event->pending);
  3778. } else
  3779. perf_event_wakeup(event);
  3780. }
  3781. return ret;
  3782. }
  3783. int perf_event_overflow(struct perf_event *event, int nmi,
  3784. struct perf_sample_data *data,
  3785. struct pt_regs *regs)
  3786. {
  3787. return __perf_event_overflow(event, nmi, 1, data, regs);
  3788. }
  3789. /*
  3790. * Generic software event infrastructure
  3791. */
  3792. struct swevent_htable {
  3793. struct swevent_hlist *swevent_hlist;
  3794. struct mutex hlist_mutex;
  3795. int hlist_refcount;
  3796. /* Recursion avoidance in each contexts */
  3797. int recursion[PERF_NR_CONTEXTS];
  3798. };
  3799. static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
  3800. /*
  3801. * We directly increment event->count and keep a second value in
  3802. * event->hw.period_left to count intervals. This period event
  3803. * is kept in the range [-sample_period, 0] so that we can use the
  3804. * sign as trigger.
  3805. */
  3806. static u64 perf_swevent_set_period(struct perf_event *event)
  3807. {
  3808. struct hw_perf_event *hwc = &event->hw;
  3809. u64 period = hwc->last_period;
  3810. u64 nr, offset;
  3811. s64 old, val;
  3812. hwc->last_period = hwc->sample_period;
  3813. again:
  3814. old = val = local64_read(&hwc->period_left);
  3815. if (val < 0)
  3816. return 0;
  3817. nr = div64_u64(period + val, period);
  3818. offset = nr * period;
  3819. val -= offset;
  3820. if (local64_cmpxchg(&hwc->period_left, old, val) != old)
  3821. goto again;
  3822. return nr;
  3823. }
  3824. static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
  3825. int nmi, struct perf_sample_data *data,
  3826. struct pt_regs *regs)
  3827. {
  3828. struct hw_perf_event *hwc = &event->hw;
  3829. int throttle = 0;
  3830. data->period = event->hw.last_period;
  3831. if (!overflow)
  3832. overflow = perf_swevent_set_period(event);
  3833. if (hwc->interrupts == MAX_INTERRUPTS)
  3834. return;
  3835. for (; overflow; overflow--) {
  3836. if (__perf_event_overflow(event, nmi, throttle,
  3837. data, regs)) {
  3838. /*
  3839. * We inhibit the overflow from happening when
  3840. * hwc->interrupts == MAX_INTERRUPTS.
  3841. */
  3842. break;
  3843. }
  3844. throttle = 1;
  3845. }
  3846. }
  3847. static void perf_swevent_event(struct perf_event *event, u64 nr,
  3848. int nmi, struct perf_sample_data *data,
  3849. struct pt_regs *regs)
  3850. {
  3851. struct hw_perf_event *hwc = &event->hw;
  3852. local64_add(nr, &event->count);
  3853. if (!regs)
  3854. return;
  3855. if (!is_sampling_event(event))
  3856. return;
  3857. if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  3858. return perf_swevent_overflow(event, 1, nmi, data, regs);
  3859. if (local64_add_negative(nr, &hwc->period_left))
  3860. return;
  3861. perf_swevent_overflow(event, 0, nmi, data, regs);
  3862. }
  3863. static int perf_exclude_event(struct perf_event *event,
  3864. struct pt_regs *regs)
  3865. {
  3866. if (event->hw.state & PERF_HES_STOPPED)
  3867. return 1;
  3868. if (regs) {
  3869. if (event->attr.exclude_user && user_mode(regs))
  3870. return 1;
  3871. if (event->attr.exclude_kernel && !user_mode(regs))
  3872. return 1;
  3873. }
  3874. return 0;
  3875. }
  3876. static int perf_swevent_match(struct perf_event *event,
  3877. enum perf_type_id type,
  3878. u32 event_id,
  3879. struct perf_sample_data *data,
  3880. struct pt_regs *regs)
  3881. {
  3882. if (event->attr.type != type)
  3883. return 0;
  3884. if (event->attr.config != event_id)
  3885. return 0;
  3886. if (perf_exclude_event(event, regs))
  3887. return 0;
  3888. return 1;
  3889. }
  3890. static inline u64 swevent_hash(u64 type, u32 event_id)
  3891. {
  3892. u64 val = event_id | (type << 32);
  3893. return hash_64(val, SWEVENT_HLIST_BITS);
  3894. }
  3895. static inline struct hlist_head *
  3896. __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
  3897. {
  3898. u64 hash = swevent_hash(type, event_id);
  3899. return &hlist->heads[hash];
  3900. }
  3901. /* For the read side: events when they trigger */
  3902. static inline struct hlist_head *
  3903. find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
  3904. {
  3905. struct swevent_hlist *hlist;
  3906. hlist = rcu_dereference(swhash->swevent_hlist);
  3907. if (!hlist)
  3908. return NULL;
  3909. return __find_swevent_head(hlist, type, event_id);
  3910. }
  3911. /* For the event head insertion and removal in the hlist */
  3912. static inline struct hlist_head *
  3913. find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
  3914. {
  3915. struct swevent_hlist *hlist;
  3916. u32 event_id = event->attr.config;
  3917. u64 type = event->attr.type;
  3918. /*
  3919. * Event scheduling is always serialized against hlist allocation
  3920. * and release. Which makes the protected version suitable here.
  3921. * The context lock guarantees that.
  3922. */
  3923. hlist = rcu_dereference_protected(swhash->swevent_hlist,
  3924. lockdep_is_held(&event->ctx->lock));
  3925. if (!hlist)
  3926. return NULL;
  3927. return __find_swevent_head(hlist, type, event_id);
  3928. }
  3929. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  3930. u64 nr, int nmi,
  3931. struct perf_sample_data *data,
  3932. struct pt_regs *regs)
  3933. {
  3934. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3935. struct perf_event *event;
  3936. struct hlist_node *node;
  3937. struct hlist_head *head;
  3938. rcu_read_lock();
  3939. head = find_swevent_head_rcu(swhash, type, event_id);
  3940. if (!head)
  3941. goto end;
  3942. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  3943. if (perf_swevent_match(event, type, event_id, data, regs))
  3944. perf_swevent_event(event, nr, nmi, data, regs);
  3945. }
  3946. end:
  3947. rcu_read_unlock();
  3948. }
  3949. int perf_swevent_get_recursion_context(void)
  3950. {
  3951. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3952. return get_recursion_context(swhash->recursion);
  3953. }
  3954. EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
  3955. inline void perf_swevent_put_recursion_context(int rctx)
  3956. {
  3957. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3958. put_recursion_context(swhash->recursion, rctx);
  3959. }
  3960. void __perf_sw_event(u32 event_id, u64 nr, int nmi,
  3961. struct pt_regs *regs, u64 addr)
  3962. {
  3963. struct perf_sample_data data;
  3964. int rctx;
  3965. preempt_disable_notrace();
  3966. rctx = perf_swevent_get_recursion_context();
  3967. if (rctx < 0)
  3968. return;
  3969. perf_sample_data_init(&data, addr);
  3970. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
  3971. perf_swevent_put_recursion_context(rctx);
  3972. preempt_enable_notrace();
  3973. }
  3974. static void perf_swevent_read(struct perf_event *event)
  3975. {
  3976. }
  3977. static int perf_swevent_add(struct perf_event *event, int flags)
  3978. {
  3979. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3980. struct hw_perf_event *hwc = &event->hw;
  3981. struct hlist_head *head;
  3982. if (is_sampling_event(event)) {
  3983. hwc->last_period = hwc->sample_period;
  3984. perf_swevent_set_period(event);
  3985. }
  3986. hwc->state = !(flags & PERF_EF_START);
  3987. head = find_swevent_head(swhash, event);
  3988. if (WARN_ON_ONCE(!head))
  3989. return -EINVAL;
  3990. hlist_add_head_rcu(&event->hlist_entry, head);
  3991. return 0;
  3992. }
  3993. static void perf_swevent_del(struct perf_event *event, int flags)
  3994. {
  3995. hlist_del_rcu(&event->hlist_entry);
  3996. }
  3997. static void perf_swevent_start(struct perf_event *event, int flags)
  3998. {
  3999. event->hw.state = 0;
  4000. }
  4001. static void perf_swevent_stop(struct perf_event *event, int flags)
  4002. {
  4003. event->hw.state = PERF_HES_STOPPED;
  4004. }
  4005. /* Deref the hlist from the update side */
  4006. static inline struct swevent_hlist *
  4007. swevent_hlist_deref(struct swevent_htable *swhash)
  4008. {
  4009. return rcu_dereference_protected(swhash->swevent_hlist,
  4010. lockdep_is_held(&swhash->hlist_mutex));
  4011. }
  4012. static void swevent_hlist_release(struct swevent_htable *swhash)
  4013. {
  4014. struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
  4015. if (!hlist)
  4016. return;
  4017. rcu_assign_pointer(swhash->swevent_hlist, NULL);
  4018. kfree_rcu(hlist, rcu_head);
  4019. }
  4020. static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
  4021. {
  4022. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  4023. mutex_lock(&swhash->hlist_mutex);
  4024. if (!--swhash->hlist_refcount)
  4025. swevent_hlist_release(swhash);
  4026. mutex_unlock(&swhash->hlist_mutex);
  4027. }
  4028. static void swevent_hlist_put(struct perf_event *event)
  4029. {
  4030. int cpu;
  4031. if (event->cpu != -1) {
  4032. swevent_hlist_put_cpu(event, event->cpu);
  4033. return;
  4034. }
  4035. for_each_possible_cpu(cpu)
  4036. swevent_hlist_put_cpu(event, cpu);
  4037. }
  4038. static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
  4039. {
  4040. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  4041. int err = 0;
  4042. mutex_lock(&swhash->hlist_mutex);
  4043. if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
  4044. struct swevent_hlist *hlist;
  4045. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  4046. if (!hlist) {
  4047. err = -ENOMEM;
  4048. goto exit;
  4049. }
  4050. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  4051. }
  4052. swhash->hlist_refcount++;
  4053. exit:
  4054. mutex_unlock(&swhash->hlist_mutex);
  4055. return err;
  4056. }
  4057. static int swevent_hlist_get(struct perf_event *event)
  4058. {
  4059. int err;
  4060. int cpu, failed_cpu;
  4061. if (event->cpu != -1)
  4062. return swevent_hlist_get_cpu(event, event->cpu);
  4063. get_online_cpus();
  4064. for_each_possible_cpu(cpu) {
  4065. err = swevent_hlist_get_cpu(event, cpu);
  4066. if (err) {
  4067. failed_cpu = cpu;
  4068. goto fail;
  4069. }
  4070. }
  4071. put_online_cpus();
  4072. return 0;
  4073. fail:
  4074. for_each_possible_cpu(cpu) {
  4075. if (cpu == failed_cpu)
  4076. break;
  4077. swevent_hlist_put_cpu(event, cpu);
  4078. }
  4079. put_online_cpus();
  4080. return err;
  4081. }
  4082. struct jump_label_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
  4083. static void sw_perf_event_destroy(struct perf_event *event)
  4084. {
  4085. u64 event_id = event->attr.config;
  4086. WARN_ON(event->parent);
  4087. jump_label_dec(&perf_swevent_enabled[event_id]);
  4088. swevent_hlist_put(event);
  4089. }
  4090. static int perf_swevent_init(struct perf_event *event)
  4091. {
  4092. int event_id = event->attr.config;
  4093. if (event->attr.type != PERF_TYPE_SOFTWARE)
  4094. return -ENOENT;
  4095. switch (event_id) {
  4096. case PERF_COUNT_SW_CPU_CLOCK:
  4097. case PERF_COUNT_SW_TASK_CLOCK:
  4098. return -ENOENT;
  4099. default:
  4100. break;
  4101. }
  4102. if (event_id >= PERF_COUNT_SW_MAX)
  4103. return -ENOENT;
  4104. if (!event->parent) {
  4105. int err;
  4106. err = swevent_hlist_get(event);
  4107. if (err)
  4108. return err;
  4109. jump_label_inc(&perf_swevent_enabled[event_id]);
  4110. event->destroy = sw_perf_event_destroy;
  4111. }
  4112. return 0;
  4113. }
  4114. static struct pmu perf_swevent = {
  4115. .task_ctx_nr = perf_sw_context,
  4116. .event_init = perf_swevent_init,
  4117. .add = perf_swevent_add,
  4118. .del = perf_swevent_del,
  4119. .start = perf_swevent_start,
  4120. .stop = perf_swevent_stop,
  4121. .read = perf_swevent_read,
  4122. };
  4123. #ifdef CONFIG_EVENT_TRACING
  4124. static int perf_tp_filter_match(struct perf_event *event,
  4125. struct perf_sample_data *data)
  4126. {
  4127. void *record = data->raw->data;
  4128. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  4129. return 1;
  4130. return 0;
  4131. }
  4132. static int perf_tp_event_match(struct perf_event *event,
  4133. struct perf_sample_data *data,
  4134. struct pt_regs *regs)
  4135. {
  4136. if (event->hw.state & PERF_HES_STOPPED)
  4137. return 0;
  4138. /*
  4139. * All tracepoints are from kernel-space.
  4140. */
  4141. if (event->attr.exclude_kernel)
  4142. return 0;
  4143. if (!perf_tp_filter_match(event, data))
  4144. return 0;
  4145. return 1;
  4146. }
  4147. void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
  4148. struct pt_regs *regs, struct hlist_head *head, int rctx)
  4149. {
  4150. struct perf_sample_data data;
  4151. struct perf_event *event;
  4152. struct hlist_node *node;
  4153. struct perf_raw_record raw = {
  4154. .size = entry_size,
  4155. .data = record,
  4156. };
  4157. perf_sample_data_init(&data, addr);
  4158. data.raw = &raw;
  4159. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  4160. if (perf_tp_event_match(event, &data, regs))
  4161. perf_swevent_event(event, count, 1, &data, regs);
  4162. }
  4163. perf_swevent_put_recursion_context(rctx);
  4164. }
  4165. EXPORT_SYMBOL_GPL(perf_tp_event);
  4166. static void tp_perf_event_destroy(struct perf_event *event)
  4167. {
  4168. perf_trace_destroy(event);
  4169. }
  4170. static int perf_tp_event_init(struct perf_event *event)
  4171. {
  4172. int err;
  4173. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  4174. return -ENOENT;
  4175. err = perf_trace_init(event);
  4176. if (err)
  4177. return err;
  4178. event->destroy = tp_perf_event_destroy;
  4179. return 0;
  4180. }
  4181. static struct pmu perf_tracepoint = {
  4182. .task_ctx_nr = perf_sw_context,
  4183. .event_init = perf_tp_event_init,
  4184. .add = perf_trace_add,
  4185. .del = perf_trace_del,
  4186. .start = perf_swevent_start,
  4187. .stop = perf_swevent_stop,
  4188. .read = perf_swevent_read,
  4189. };
  4190. static inline void perf_tp_register(void)
  4191. {
  4192. perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
  4193. }
  4194. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  4195. {
  4196. char *filter_str;
  4197. int ret;
  4198. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  4199. return -EINVAL;
  4200. filter_str = strndup_user(arg, PAGE_SIZE);
  4201. if (IS_ERR(filter_str))
  4202. return PTR_ERR(filter_str);
  4203. ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  4204. kfree(filter_str);
  4205. return ret;
  4206. }
  4207. static void perf_event_free_filter(struct perf_event *event)
  4208. {
  4209. ftrace_profile_free_filter(event);
  4210. }
  4211. #else
  4212. static inline void perf_tp_register(void)
  4213. {
  4214. }
  4215. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  4216. {
  4217. return -ENOENT;
  4218. }
  4219. static void perf_event_free_filter(struct perf_event *event)
  4220. {
  4221. }
  4222. #endif /* CONFIG_EVENT_TRACING */
  4223. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  4224. void perf_bp_event(struct perf_event *bp, void *data)
  4225. {
  4226. struct perf_sample_data sample;
  4227. struct pt_regs *regs = data;
  4228. perf_sample_data_init(&sample, bp->attr.bp_addr);
  4229. if (!bp->hw.state && !perf_exclude_event(bp, regs))
  4230. perf_swevent_event(bp, 1, 1, &sample, regs);
  4231. }
  4232. #endif
  4233. /*
  4234. * hrtimer based swevent callback
  4235. */
  4236. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  4237. {
  4238. enum hrtimer_restart ret = HRTIMER_RESTART;
  4239. struct perf_sample_data data;
  4240. struct pt_regs *regs;
  4241. struct perf_event *event;
  4242. u64 period;
  4243. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  4244. if (event->state != PERF_EVENT_STATE_ACTIVE)
  4245. return HRTIMER_NORESTART;
  4246. event->pmu->read(event);
  4247. perf_sample_data_init(&data, 0);
  4248. data.period = event->hw.last_period;
  4249. regs = get_irq_regs();
  4250. if (regs && !perf_exclude_event(event, regs)) {
  4251. if (!(event->attr.exclude_idle && current->pid == 0))
  4252. if (perf_event_overflow(event, 0, &data, regs))
  4253. ret = HRTIMER_NORESTART;
  4254. }
  4255. period = max_t(u64, 10000, event->hw.sample_period);
  4256. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  4257. return ret;
  4258. }
  4259. static void perf_swevent_start_hrtimer(struct perf_event *event)
  4260. {
  4261. struct hw_perf_event *hwc = &event->hw;
  4262. s64 period;
  4263. if (!is_sampling_event(event))
  4264. return;
  4265. period = local64_read(&hwc->period_left);
  4266. if (period) {
  4267. if (period < 0)
  4268. period = 10000;
  4269. local64_set(&hwc->period_left, 0);
  4270. } else {
  4271. period = max_t(u64, 10000, hwc->sample_period);
  4272. }
  4273. __hrtimer_start_range_ns(&hwc->hrtimer,
  4274. ns_to_ktime(period), 0,
  4275. HRTIMER_MODE_REL_PINNED, 0);
  4276. }
  4277. static void perf_swevent_cancel_hrtimer(struct perf_event *event)
  4278. {
  4279. struct hw_perf_event *hwc = &event->hw;
  4280. if (is_sampling_event(event)) {
  4281. ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
  4282. local64_set(&hwc->period_left, ktime_to_ns(remaining));
  4283. hrtimer_cancel(&hwc->hrtimer);
  4284. }
  4285. }
  4286. static void perf_swevent_init_hrtimer(struct perf_event *event)
  4287. {
  4288. struct hw_perf_event *hwc = &event->hw;
  4289. if (!is_sampling_event(event))
  4290. return;
  4291. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  4292. hwc->hrtimer.function = perf_swevent_hrtimer;
  4293. /*
  4294. * Since hrtimers have a fixed rate, we can do a static freq->period
  4295. * mapping and avoid the whole period adjust feedback stuff.
  4296. */
  4297. if (event->attr.freq) {
  4298. long freq = event->attr.sample_freq;
  4299. event->attr.sample_period = NSEC_PER_SEC / freq;
  4300. hwc->sample_period = event->attr.sample_period;
  4301. local64_set(&hwc->period_left, hwc->sample_period);
  4302. event->attr.freq = 0;
  4303. }
  4304. }
  4305. /*
  4306. * Software event: cpu wall time clock
  4307. */
  4308. static void cpu_clock_event_update(struct perf_event *event)
  4309. {
  4310. s64 prev;
  4311. u64 now;
  4312. now = local_clock();
  4313. prev = local64_xchg(&event->hw.prev_count, now);
  4314. local64_add(now - prev, &event->count);
  4315. }
  4316. static void cpu_clock_event_start(struct perf_event *event, int flags)
  4317. {
  4318. local64_set(&event->hw.prev_count, local_clock());
  4319. perf_swevent_start_hrtimer(event);
  4320. }
  4321. static void cpu_clock_event_stop(struct perf_event *event, int flags)
  4322. {
  4323. perf_swevent_cancel_hrtimer(event);
  4324. cpu_clock_event_update(event);
  4325. }
  4326. static int cpu_clock_event_add(struct perf_event *event, int flags)
  4327. {
  4328. if (flags & PERF_EF_START)
  4329. cpu_clock_event_start(event, flags);
  4330. return 0;
  4331. }
  4332. static void cpu_clock_event_del(struct perf_event *event, int flags)
  4333. {
  4334. cpu_clock_event_stop(event, flags);
  4335. }
  4336. static void cpu_clock_event_read(struct perf_event *event)
  4337. {
  4338. cpu_clock_event_update(event);
  4339. }
  4340. static int cpu_clock_event_init(struct perf_event *event)
  4341. {
  4342. if (event->attr.type != PERF_TYPE_SOFTWARE)
  4343. return -ENOENT;
  4344. if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
  4345. return -ENOENT;
  4346. perf_swevent_init_hrtimer(event);
  4347. return 0;
  4348. }
  4349. static struct pmu perf_cpu_clock = {
  4350. .task_ctx_nr = perf_sw_context,
  4351. .event_init = cpu_clock_event_init,
  4352. .add = cpu_clock_event_add,
  4353. .del = cpu_clock_event_del,
  4354. .start = cpu_clock_event_start,
  4355. .stop = cpu_clock_event_stop,
  4356. .read = cpu_clock_event_read,
  4357. };
  4358. /*
  4359. * Software event: task time clock
  4360. */
  4361. static void task_clock_event_update(struct perf_event *event, u64 now)
  4362. {
  4363. u64 prev;
  4364. s64 delta;
  4365. prev = local64_xchg(&event->hw.prev_count, now);
  4366. delta = now - prev;
  4367. local64_add(delta, &event->count);
  4368. }
  4369. static void task_clock_event_start(struct perf_event *event, int flags)
  4370. {
  4371. local64_set(&event->hw.prev_count, event->ctx->time);
  4372. perf_swevent_start_hrtimer(event);
  4373. }
  4374. static void task_clock_event_stop(struct perf_event *event, int flags)
  4375. {
  4376. perf_swevent_cancel_hrtimer(event);
  4377. task_clock_event_update(event, event->ctx->time);
  4378. }
  4379. static int task_clock_event_add(struct perf_event *event, int flags)
  4380. {
  4381. if (flags & PERF_EF_START)
  4382. task_clock_event_start(event, flags);
  4383. return 0;
  4384. }
  4385. static void task_clock_event_del(struct perf_event *event, int flags)
  4386. {
  4387. task_clock_event_stop(event, PERF_EF_UPDATE);
  4388. }
  4389. static void task_clock_event_read(struct perf_event *event)
  4390. {
  4391. u64 now = perf_clock();
  4392. u64 delta = now - event->ctx->timestamp;
  4393. u64 time = event->ctx->time + delta;
  4394. task_clock_event_update(event, time);
  4395. }
  4396. static int task_clock_event_init(struct perf_event *event)
  4397. {
  4398. if (event->attr.type != PERF_TYPE_SOFTWARE)
  4399. return -ENOENT;
  4400. if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
  4401. return -ENOENT;
  4402. perf_swevent_init_hrtimer(event);
  4403. return 0;
  4404. }
  4405. static struct pmu perf_task_clock = {
  4406. .task_ctx_nr = perf_sw_context,
  4407. .event_init = task_clock_event_init,
  4408. .add = task_clock_event_add,
  4409. .del = task_clock_event_del,
  4410. .start = task_clock_event_start,
  4411. .stop = task_clock_event_stop,
  4412. .read = task_clock_event_read,
  4413. };
  4414. static void perf_pmu_nop_void(struct pmu *pmu)
  4415. {
  4416. }
  4417. static int perf_pmu_nop_int(struct pmu *pmu)
  4418. {
  4419. return 0;
  4420. }
  4421. static void perf_pmu_start_txn(struct pmu *pmu)
  4422. {
  4423. perf_pmu_disable(pmu);
  4424. }
  4425. static int perf_pmu_commit_txn(struct pmu *pmu)
  4426. {
  4427. perf_pmu_enable(pmu);
  4428. return 0;
  4429. }
  4430. static void perf_pmu_cancel_txn(struct pmu *pmu)
  4431. {
  4432. perf_pmu_enable(pmu);
  4433. }
  4434. /*
  4435. * Ensures all contexts with the same task_ctx_nr have the same
  4436. * pmu_cpu_context too.
  4437. */
  4438. static void *find_pmu_context(int ctxn)
  4439. {
  4440. struct pmu *pmu;
  4441. if (ctxn < 0)
  4442. return NULL;
  4443. list_for_each_entry(pmu, &pmus, entry) {
  4444. if (pmu->task_ctx_nr == ctxn)
  4445. return pmu->pmu_cpu_context;
  4446. }
  4447. return NULL;
  4448. }
  4449. static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
  4450. {
  4451. int cpu;
  4452. for_each_possible_cpu(cpu) {
  4453. struct perf_cpu_context *cpuctx;
  4454. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  4455. if (cpuctx->active_pmu == old_pmu)
  4456. cpuctx->active_pmu = pmu;
  4457. }
  4458. }
  4459. static void free_pmu_context(struct pmu *pmu)
  4460. {
  4461. struct pmu *i;
  4462. mutex_lock(&pmus_lock);
  4463. /*
  4464. * Like a real lame refcount.
  4465. */
  4466. list_for_each_entry(i, &pmus, entry) {
  4467. if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
  4468. update_pmu_context(i, pmu);
  4469. goto out;
  4470. }
  4471. }
  4472. free_percpu(pmu->pmu_cpu_context);
  4473. out:
  4474. mutex_unlock(&pmus_lock);
  4475. }
  4476. static struct idr pmu_idr;
  4477. static ssize_t
  4478. type_show(struct device *dev, struct device_attribute *attr, char *page)
  4479. {
  4480. struct pmu *pmu = dev_get_drvdata(dev);
  4481. return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
  4482. }
  4483. static struct device_attribute pmu_dev_attrs[] = {
  4484. __ATTR_RO(type),
  4485. __ATTR_NULL,
  4486. };
  4487. static int pmu_bus_running;
  4488. static struct bus_type pmu_bus = {
  4489. .name = "event_source",
  4490. .dev_attrs = pmu_dev_attrs,
  4491. };
  4492. static void pmu_dev_release(struct device *dev)
  4493. {
  4494. kfree(dev);
  4495. }
  4496. static int pmu_dev_alloc(struct pmu *pmu)
  4497. {
  4498. int ret = -ENOMEM;
  4499. pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  4500. if (!pmu->dev)
  4501. goto out;
  4502. device_initialize(pmu->dev);
  4503. ret = dev_set_name(pmu->dev, "%s", pmu->name);
  4504. if (ret)
  4505. goto free_dev;
  4506. dev_set_drvdata(pmu->dev, pmu);
  4507. pmu->dev->bus = &pmu_bus;
  4508. pmu->dev->release = pmu_dev_release;
  4509. ret = device_add(pmu->dev);
  4510. if (ret)
  4511. goto free_dev;
  4512. out:
  4513. return ret;
  4514. free_dev:
  4515. put_device(pmu->dev);
  4516. goto out;
  4517. }
  4518. static struct lock_class_key cpuctx_mutex;
  4519. static struct lock_class_key cpuctx_lock;
  4520. int perf_pmu_register(struct pmu *pmu, char *name, int type)
  4521. {
  4522. int cpu, ret;
  4523. mutex_lock(&pmus_lock);
  4524. ret = -ENOMEM;
  4525. pmu->pmu_disable_count = alloc_percpu(int);
  4526. if (!pmu->pmu_disable_count)
  4527. goto unlock;
  4528. pmu->type = -1;
  4529. if (!name)
  4530. goto skip_type;
  4531. pmu->name = name;
  4532. if (type < 0) {
  4533. int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
  4534. if (!err)
  4535. goto free_pdc;
  4536. err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
  4537. if (err) {
  4538. ret = err;
  4539. goto free_pdc;
  4540. }
  4541. }
  4542. pmu->type = type;
  4543. if (pmu_bus_running) {
  4544. ret = pmu_dev_alloc(pmu);
  4545. if (ret)
  4546. goto free_idr;
  4547. }
  4548. skip_type:
  4549. pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
  4550. if (pmu->pmu_cpu_context)
  4551. goto got_cpu_context;
  4552. pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
  4553. if (!pmu->pmu_cpu_context)
  4554. goto free_dev;
  4555. for_each_possible_cpu(cpu) {
  4556. struct perf_cpu_context *cpuctx;
  4557. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  4558. __perf_event_init_context(&cpuctx->ctx);
  4559. lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
  4560. lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
  4561. cpuctx->ctx.type = cpu_context;
  4562. cpuctx->ctx.pmu = pmu;
  4563. cpuctx->jiffies_interval = 1;
  4564. INIT_LIST_HEAD(&cpuctx->rotation_list);
  4565. cpuctx->active_pmu = pmu;
  4566. }
  4567. got_cpu_context:
  4568. if (!pmu->start_txn) {
  4569. if (pmu->pmu_enable) {
  4570. /*
  4571. * If we have pmu_enable/pmu_disable calls, install
  4572. * transaction stubs that use that to try and batch
  4573. * hardware accesses.
  4574. */
  4575. pmu->start_txn = perf_pmu_start_txn;
  4576. pmu->commit_txn = perf_pmu_commit_txn;
  4577. pmu->cancel_txn = perf_pmu_cancel_txn;
  4578. } else {
  4579. pmu->start_txn = perf_pmu_nop_void;
  4580. pmu->commit_txn = perf_pmu_nop_int;
  4581. pmu->cancel_txn = perf_pmu_nop_void;
  4582. }
  4583. }
  4584. if (!pmu->pmu_enable) {
  4585. pmu->pmu_enable = perf_pmu_nop_void;
  4586. pmu->pmu_disable = perf_pmu_nop_void;
  4587. }
  4588. list_add_rcu(&pmu->entry, &pmus);
  4589. ret = 0;
  4590. unlock:
  4591. mutex_unlock(&pmus_lock);
  4592. return ret;
  4593. free_dev:
  4594. device_del(pmu->dev);
  4595. put_device(pmu->dev);
  4596. free_idr:
  4597. if (pmu->type >= PERF_TYPE_MAX)
  4598. idr_remove(&pmu_idr, pmu->type);
  4599. free_pdc:
  4600. free_percpu(pmu->pmu_disable_count);
  4601. goto unlock;
  4602. }
  4603. void perf_pmu_unregister(struct pmu *pmu)
  4604. {
  4605. mutex_lock(&pmus_lock);
  4606. list_del_rcu(&pmu->entry);
  4607. mutex_unlock(&pmus_lock);
  4608. /*
  4609. * We dereference the pmu list under both SRCU and regular RCU, so
  4610. * synchronize against both of those.
  4611. */
  4612. synchronize_srcu(&pmus_srcu);
  4613. synchronize_rcu();
  4614. free_percpu(pmu->pmu_disable_count);
  4615. if (pmu->type >= PERF_TYPE_MAX)
  4616. idr_remove(&pmu_idr, pmu->type);
  4617. device_del(pmu->dev);
  4618. put_device(pmu->dev);
  4619. free_pmu_context(pmu);
  4620. }
  4621. struct pmu *perf_init_event(struct perf_event *event)
  4622. {
  4623. struct pmu *pmu = NULL;
  4624. int idx;
  4625. int ret;
  4626. idx = srcu_read_lock(&pmus_srcu);
  4627. rcu_read_lock();
  4628. pmu = idr_find(&pmu_idr, event->attr.type);
  4629. rcu_read_unlock();
  4630. if (pmu) {
  4631. ret = pmu->event_init(event);
  4632. if (ret)
  4633. pmu = ERR_PTR(ret);
  4634. goto unlock;
  4635. }
  4636. list_for_each_entry_rcu(pmu, &pmus, entry) {
  4637. ret = pmu->event_init(event);
  4638. if (!ret)
  4639. goto unlock;
  4640. if (ret != -ENOENT) {
  4641. pmu = ERR_PTR(ret);
  4642. goto unlock;
  4643. }
  4644. }
  4645. pmu = ERR_PTR(-ENOENT);
  4646. unlock:
  4647. srcu_read_unlock(&pmus_srcu, idx);
  4648. return pmu;
  4649. }
  4650. /*
  4651. * Allocate and initialize a event structure
  4652. */
  4653. static struct perf_event *
  4654. perf_event_alloc(struct perf_event_attr *attr, int cpu,
  4655. struct task_struct *task,
  4656. struct perf_event *group_leader,
  4657. struct perf_event *parent_event,
  4658. perf_overflow_handler_t overflow_handler)
  4659. {
  4660. struct pmu *pmu;
  4661. struct perf_event *event;
  4662. struct hw_perf_event *hwc;
  4663. long err;
  4664. if ((unsigned)cpu >= nr_cpu_ids) {
  4665. if (!task || cpu != -1)
  4666. return ERR_PTR(-EINVAL);
  4667. }
  4668. event = kzalloc(sizeof(*event), GFP_KERNEL);
  4669. if (!event)
  4670. return ERR_PTR(-ENOMEM);
  4671. /*
  4672. * Single events are their own group leaders, with an
  4673. * empty sibling list:
  4674. */
  4675. if (!group_leader)
  4676. group_leader = event;
  4677. mutex_init(&event->child_mutex);
  4678. INIT_LIST_HEAD(&event->child_list);
  4679. INIT_LIST_HEAD(&event->group_entry);
  4680. INIT_LIST_HEAD(&event->event_entry);
  4681. INIT_LIST_HEAD(&event->sibling_list);
  4682. init_waitqueue_head(&event->waitq);
  4683. init_irq_work(&event->pending, perf_pending_event);
  4684. mutex_init(&event->mmap_mutex);
  4685. event->cpu = cpu;
  4686. event->attr = *attr;
  4687. event->group_leader = group_leader;
  4688. event->pmu = NULL;
  4689. event->oncpu = -1;
  4690. event->parent = parent_event;
  4691. event->ns = get_pid_ns(current->nsproxy->pid_ns);
  4692. event->id = atomic64_inc_return(&perf_event_id);
  4693. event->state = PERF_EVENT_STATE_INACTIVE;
  4694. if (task) {
  4695. event->attach_state = PERF_ATTACH_TASK;
  4696. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  4697. /*
  4698. * hw_breakpoint is a bit difficult here..
  4699. */
  4700. if (attr->type == PERF_TYPE_BREAKPOINT)
  4701. event->hw.bp_target = task;
  4702. #endif
  4703. }
  4704. if (!overflow_handler && parent_event)
  4705. overflow_handler = parent_event->overflow_handler;
  4706. event->overflow_handler = overflow_handler;
  4707. if (attr->disabled)
  4708. event->state = PERF_EVENT_STATE_OFF;
  4709. pmu = NULL;
  4710. hwc = &event->hw;
  4711. hwc->sample_period = attr->sample_period;
  4712. if (attr->freq && attr->sample_freq)
  4713. hwc->sample_period = 1;
  4714. hwc->last_period = hwc->sample_period;
  4715. local64_set(&hwc->period_left, hwc->sample_period);
  4716. /*
  4717. * we currently do not support PERF_FORMAT_GROUP on inherited events
  4718. */
  4719. if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
  4720. goto done;
  4721. pmu = perf_init_event(event);
  4722. done:
  4723. err = 0;
  4724. if (!pmu)
  4725. err = -EINVAL;
  4726. else if (IS_ERR(pmu))
  4727. err = PTR_ERR(pmu);
  4728. if (err) {
  4729. if (event->ns)
  4730. put_pid_ns(event->ns);
  4731. kfree(event);
  4732. return ERR_PTR(err);
  4733. }
  4734. event->pmu = pmu;
  4735. if (!event->parent) {
  4736. if (event->attach_state & PERF_ATTACH_TASK)
  4737. jump_label_inc(&perf_sched_events);
  4738. if (event->attr.mmap || event->attr.mmap_data)
  4739. atomic_inc(&nr_mmap_events);
  4740. if (event->attr.comm)
  4741. atomic_inc(&nr_comm_events);
  4742. if (event->attr.task)
  4743. atomic_inc(&nr_task_events);
  4744. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  4745. err = get_callchain_buffers();
  4746. if (err) {
  4747. free_event(event);
  4748. return ERR_PTR(err);
  4749. }
  4750. }
  4751. }
  4752. return event;
  4753. }
  4754. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  4755. struct perf_event_attr *attr)
  4756. {
  4757. u32 size;
  4758. int ret;
  4759. if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  4760. return -EFAULT;
  4761. /*
  4762. * zero the full structure, so that a short copy will be nice.
  4763. */
  4764. memset(attr, 0, sizeof(*attr));
  4765. ret = get_user(size, &uattr->size);
  4766. if (ret)
  4767. return ret;
  4768. if (size > PAGE_SIZE) /* silly large */
  4769. goto err_size;
  4770. if (!size) /* abi compat */
  4771. size = PERF_ATTR_SIZE_VER0;
  4772. if (size < PERF_ATTR_SIZE_VER0)
  4773. goto err_size;
  4774. /*
  4775. * If we're handed a bigger struct than we know of,
  4776. * ensure all the unknown bits are 0 - i.e. new
  4777. * user-space does not rely on any kernel feature
  4778. * extensions we dont know about yet.
  4779. */
  4780. if (size > sizeof(*attr)) {
  4781. unsigned char __user *addr;
  4782. unsigned char __user *end;
  4783. unsigned char val;
  4784. addr = (void __user *)uattr + sizeof(*attr);
  4785. end = (void __user *)uattr + size;
  4786. for (; addr < end; addr++) {
  4787. ret = get_user(val, addr);
  4788. if (ret)
  4789. return ret;
  4790. if (val)
  4791. goto err_size;
  4792. }
  4793. size = sizeof(*attr);
  4794. }
  4795. ret = copy_from_user(attr, uattr, size);
  4796. if (ret)
  4797. return -EFAULT;
  4798. /*
  4799. * If the type exists, the corresponding creation will verify
  4800. * the attr->config.
  4801. */
  4802. if (attr->type >= PERF_TYPE_MAX)
  4803. return -EINVAL;
  4804. if (attr->__reserved_1)
  4805. return -EINVAL;
  4806. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  4807. return -EINVAL;
  4808. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  4809. return -EINVAL;
  4810. out:
  4811. return ret;
  4812. err_size:
  4813. put_user(sizeof(*attr), &uattr->size);
  4814. ret = -E2BIG;
  4815. goto out;
  4816. }
  4817. static int
  4818. perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
  4819. {
  4820. struct ring_buffer *rb = NULL, *old_rb = NULL;
  4821. int ret = -EINVAL;
  4822. if (!output_event)
  4823. goto set;
  4824. /* don't allow circular references */
  4825. if (event == output_event)
  4826. goto out;
  4827. /*
  4828. * Don't allow cross-cpu buffers
  4829. */
  4830. if (output_event->cpu != event->cpu)
  4831. goto out;
  4832. /*
  4833. * If its not a per-cpu rb, it must be the same task.
  4834. */
  4835. if (output_event->cpu == -1 && output_event->ctx != event->ctx)
  4836. goto out;
  4837. set:
  4838. mutex_lock(&event->mmap_mutex);
  4839. /* Can't redirect output if we've got an active mmap() */
  4840. if (atomic_read(&event->mmap_count))
  4841. goto unlock;
  4842. if (output_event) {
  4843. /* get the rb we want to redirect to */
  4844. rb = ring_buffer_get(output_event);
  4845. if (!rb)
  4846. goto unlock;
  4847. }
  4848. old_rb = event->rb;
  4849. rcu_assign_pointer(event->rb, rb);
  4850. ret = 0;
  4851. unlock:
  4852. mutex_unlock(&event->mmap_mutex);
  4853. if (old_rb)
  4854. ring_buffer_put(old_rb);
  4855. out:
  4856. return ret;
  4857. }
  4858. /**
  4859. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  4860. *
  4861. * @attr_uptr: event_id type attributes for monitoring/sampling
  4862. * @pid: target pid
  4863. * @cpu: target cpu
  4864. * @group_fd: group leader event fd
  4865. */
  4866. SYSCALL_DEFINE5(perf_event_open,
  4867. struct perf_event_attr __user *, attr_uptr,
  4868. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  4869. {
  4870. struct perf_event *group_leader = NULL, *output_event = NULL;
  4871. struct perf_event *event, *sibling;
  4872. struct perf_event_attr attr;
  4873. struct perf_event_context *ctx;
  4874. struct file *event_file = NULL;
  4875. struct file *group_file = NULL;
  4876. struct task_struct *task = NULL;
  4877. struct pmu *pmu;
  4878. int event_fd;
  4879. int move_group = 0;
  4880. int fput_needed = 0;
  4881. int err;
  4882. /* for future expandability... */
  4883. if (flags & ~PERF_FLAG_ALL)
  4884. return -EINVAL;
  4885. err = perf_copy_attr(attr_uptr, &attr);
  4886. if (err)
  4887. return err;
  4888. if (!attr.exclude_kernel) {
  4889. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  4890. return -EACCES;
  4891. }
  4892. if (attr.freq) {
  4893. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  4894. return -EINVAL;
  4895. }
  4896. /*
  4897. * In cgroup mode, the pid argument is used to pass the fd
  4898. * opened to the cgroup directory in cgroupfs. The cpu argument
  4899. * designates the cpu on which to monitor threads from that
  4900. * cgroup.
  4901. */
  4902. if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
  4903. return -EINVAL;
  4904. event_fd = get_unused_fd_flags(O_RDWR);
  4905. if (event_fd < 0)
  4906. return event_fd;
  4907. if (group_fd != -1) {
  4908. group_leader = perf_fget_light(group_fd, &fput_needed);
  4909. if (IS_ERR(group_leader)) {
  4910. err = PTR_ERR(group_leader);
  4911. goto err_fd;
  4912. }
  4913. group_file = group_leader->filp;
  4914. if (flags & PERF_FLAG_FD_OUTPUT)
  4915. output_event = group_leader;
  4916. if (flags & PERF_FLAG_FD_NO_GROUP)
  4917. group_leader = NULL;
  4918. }
  4919. if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
  4920. task = find_lively_task_by_vpid(pid);
  4921. if (IS_ERR(task)) {
  4922. err = PTR_ERR(task);
  4923. goto err_group_fd;
  4924. }
  4925. }
  4926. event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
  4927. if (IS_ERR(event)) {
  4928. err = PTR_ERR(event);
  4929. goto err_task;
  4930. }
  4931. if (flags & PERF_FLAG_PID_CGROUP) {
  4932. err = perf_cgroup_connect(pid, event, &attr, group_leader);
  4933. if (err)
  4934. goto err_alloc;
  4935. /*
  4936. * one more event:
  4937. * - that has cgroup constraint on event->cpu
  4938. * - that may need work on context switch
  4939. */
  4940. atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
  4941. jump_label_inc(&perf_sched_events);
  4942. }
  4943. /*
  4944. * Special case software events and allow them to be part of
  4945. * any hardware group.
  4946. */
  4947. pmu = event->pmu;
  4948. if (group_leader &&
  4949. (is_software_event(event) != is_software_event(group_leader))) {
  4950. if (is_software_event(event)) {
  4951. /*
  4952. * If event and group_leader are not both a software
  4953. * event, and event is, then group leader is not.
  4954. *
  4955. * Allow the addition of software events to !software
  4956. * groups, this is safe because software events never
  4957. * fail to schedule.
  4958. */
  4959. pmu = group_leader->pmu;
  4960. } else if (is_software_event(group_leader) &&
  4961. (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
  4962. /*
  4963. * In case the group is a pure software group, and we
  4964. * try to add a hardware event, move the whole group to
  4965. * the hardware context.
  4966. */
  4967. move_group = 1;
  4968. }
  4969. }
  4970. /*
  4971. * Get the target context (task or percpu):
  4972. */
  4973. ctx = find_get_context(pmu, task, cpu);
  4974. if (IS_ERR(ctx)) {
  4975. err = PTR_ERR(ctx);
  4976. goto err_alloc;
  4977. }
  4978. if (task) {
  4979. put_task_struct(task);
  4980. task = NULL;
  4981. }
  4982. /*
  4983. * Look up the group leader (we will attach this event to it):
  4984. */
  4985. if (group_leader) {
  4986. err = -EINVAL;
  4987. /*
  4988. * Do not allow a recursive hierarchy (this new sibling
  4989. * becoming part of another group-sibling):
  4990. */
  4991. if (group_leader->group_leader != group_leader)
  4992. goto err_context;
  4993. /*
  4994. * Do not allow to attach to a group in a different
  4995. * task or CPU context:
  4996. */
  4997. if (move_group) {
  4998. if (group_leader->ctx->type != ctx->type)
  4999. goto err_context;
  5000. } else {
  5001. if (group_leader->ctx != ctx)
  5002. goto err_context;
  5003. }
  5004. /*
  5005. * Only a group leader can be exclusive or pinned
  5006. */
  5007. if (attr.exclusive || attr.pinned)
  5008. goto err_context;
  5009. }
  5010. if (output_event) {
  5011. err = perf_event_set_output(event, output_event);
  5012. if (err)
  5013. goto err_context;
  5014. }
  5015. event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
  5016. if (IS_ERR(event_file)) {
  5017. err = PTR_ERR(event_file);
  5018. goto err_context;
  5019. }
  5020. if (move_group) {
  5021. struct perf_event_context *gctx = group_leader->ctx;
  5022. mutex_lock(&gctx->mutex);
  5023. perf_remove_from_context(group_leader);
  5024. list_for_each_entry(sibling, &group_leader->sibling_list,
  5025. group_entry) {
  5026. perf_remove_from_context(sibling);
  5027. put_ctx(gctx);
  5028. }
  5029. mutex_unlock(&gctx->mutex);
  5030. put_ctx(gctx);
  5031. }
  5032. event->filp = event_file;
  5033. WARN_ON_ONCE(ctx->parent_ctx);
  5034. mutex_lock(&ctx->mutex);
  5035. if (move_group) {
  5036. perf_install_in_context(ctx, group_leader, cpu);
  5037. get_ctx(ctx);
  5038. list_for_each_entry(sibling, &group_leader->sibling_list,
  5039. group_entry) {
  5040. perf_install_in_context(ctx, sibling, cpu);
  5041. get_ctx(ctx);
  5042. }
  5043. }
  5044. perf_install_in_context(ctx, event, cpu);
  5045. ++ctx->generation;
  5046. perf_unpin_context(ctx);
  5047. mutex_unlock(&ctx->mutex);
  5048. event->owner = current;
  5049. mutex_lock(&current->perf_event_mutex);
  5050. list_add_tail(&event->owner_entry, &current->perf_event_list);
  5051. mutex_unlock(&current->perf_event_mutex);
  5052. /*
  5053. * Precalculate sample_data sizes
  5054. */
  5055. perf_event__header_size(event);
  5056. perf_event__id_header_size(event);
  5057. /*
  5058. * Drop the reference on the group_event after placing the
  5059. * new event on the sibling_list. This ensures destruction
  5060. * of the group leader will find the pointer to itself in
  5061. * perf_group_detach().
  5062. */
  5063. fput_light(group_file, fput_needed);
  5064. fd_install(event_fd, event_file);
  5065. return event_fd;
  5066. err_context:
  5067. perf_unpin_context(ctx);
  5068. put_ctx(ctx);
  5069. err_alloc:
  5070. free_event(event);
  5071. err_task:
  5072. if (task)
  5073. put_task_struct(task);
  5074. err_group_fd:
  5075. fput_light(group_file, fput_needed);
  5076. err_fd:
  5077. put_unused_fd(event_fd);
  5078. return err;
  5079. }
  5080. /**
  5081. * perf_event_create_kernel_counter
  5082. *
  5083. * @attr: attributes of the counter to create
  5084. * @cpu: cpu in which the counter is bound
  5085. * @task: task to profile (NULL for percpu)
  5086. */
  5087. struct perf_event *
  5088. perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  5089. struct task_struct *task,
  5090. perf_overflow_handler_t overflow_handler)
  5091. {
  5092. struct perf_event_context *ctx;
  5093. struct perf_event *event;
  5094. int err;
  5095. /*
  5096. * Get the target context (task or percpu):
  5097. */
  5098. event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
  5099. if (IS_ERR(event)) {
  5100. err = PTR_ERR(event);
  5101. goto err;
  5102. }
  5103. ctx = find_get_context(event->pmu, task, cpu);
  5104. if (IS_ERR(ctx)) {
  5105. err = PTR_ERR(ctx);
  5106. goto err_free;
  5107. }
  5108. event->filp = NULL;
  5109. WARN_ON_ONCE(ctx->parent_ctx);
  5110. mutex_lock(&ctx->mutex);
  5111. perf_install_in_context(ctx, event, cpu);
  5112. ++ctx->generation;
  5113. perf_unpin_context(ctx);
  5114. mutex_unlock(&ctx->mutex);
  5115. return event;
  5116. err_free:
  5117. free_event(event);
  5118. err:
  5119. return ERR_PTR(err);
  5120. }
  5121. EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
  5122. static void sync_child_event(struct perf_event *child_event,
  5123. struct task_struct *child)
  5124. {
  5125. struct perf_event *parent_event = child_event->parent;
  5126. u64 child_val;
  5127. if (child_event->attr.inherit_stat)
  5128. perf_event_read_event(child_event, child);
  5129. child_val = perf_event_count(child_event);
  5130. /*
  5131. * Add back the child's count to the parent's count:
  5132. */
  5133. atomic64_add(child_val, &parent_event->child_count);
  5134. atomic64_add(child_event->total_time_enabled,
  5135. &parent_event->child_total_time_enabled);
  5136. atomic64_add(child_event->total_time_running,
  5137. &parent_event->child_total_time_running);
  5138. /*
  5139. * Remove this event from the parent's list
  5140. */
  5141. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  5142. mutex_lock(&parent_event->child_mutex);
  5143. list_del_init(&child_event->child_list);
  5144. mutex_unlock(&parent_event->child_mutex);
  5145. /*
  5146. * Release the parent event, if this was the last
  5147. * reference to it.
  5148. */
  5149. fput(parent_event->filp);
  5150. }
  5151. static void
  5152. __perf_event_exit_task(struct perf_event *child_event,
  5153. struct perf_event_context *child_ctx,
  5154. struct task_struct *child)
  5155. {
  5156. if (child_event->parent) {
  5157. raw_spin_lock_irq(&child_ctx->lock);
  5158. perf_group_detach(child_event);
  5159. raw_spin_unlock_irq(&child_ctx->lock);
  5160. }
  5161. perf_remove_from_context(child_event);
  5162. /*
  5163. * It can happen that the parent exits first, and has events
  5164. * that are still around due to the child reference. These
  5165. * events need to be zapped.
  5166. */
  5167. if (child_event->parent) {
  5168. sync_child_event(child_event, child);
  5169. free_event(child_event);
  5170. }
  5171. }
  5172. static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
  5173. {
  5174. struct perf_event *child_event, *tmp;
  5175. struct perf_event_context *child_ctx;
  5176. unsigned long flags;
  5177. if (likely(!child->perf_event_ctxp[ctxn])) {
  5178. perf_event_task(child, NULL, 0);
  5179. return;
  5180. }
  5181. local_irq_save(flags);
  5182. /*
  5183. * We can't reschedule here because interrupts are disabled,
  5184. * and either child is current or it is a task that can't be
  5185. * scheduled, so we are now safe from rescheduling changing
  5186. * our context.
  5187. */
  5188. child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
  5189. /*
  5190. * Take the context lock here so that if find_get_context is
  5191. * reading child->perf_event_ctxp, we wait until it has
  5192. * incremented the context's refcount before we do put_ctx below.
  5193. */
  5194. raw_spin_lock(&child_ctx->lock);
  5195. task_ctx_sched_out(child_ctx);
  5196. child->perf_event_ctxp[ctxn] = NULL;
  5197. /*
  5198. * If this context is a clone; unclone it so it can't get
  5199. * swapped to another process while we're removing all
  5200. * the events from it.
  5201. */
  5202. unclone_ctx(child_ctx);
  5203. update_context_time(child_ctx);
  5204. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  5205. /*
  5206. * Report the task dead after unscheduling the events so that we
  5207. * won't get any samples after PERF_RECORD_EXIT. We can however still
  5208. * get a few PERF_RECORD_READ events.
  5209. */
  5210. perf_event_task(child, child_ctx, 0);
  5211. /*
  5212. * We can recurse on the same lock type through:
  5213. *
  5214. * __perf_event_exit_task()
  5215. * sync_child_event()
  5216. * fput(parent_event->filp)
  5217. * perf_release()
  5218. * mutex_lock(&ctx->mutex)
  5219. *
  5220. * But since its the parent context it won't be the same instance.
  5221. */
  5222. mutex_lock(&child_ctx->mutex);
  5223. again:
  5224. list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
  5225. group_entry)
  5226. __perf_event_exit_task(child_event, child_ctx, child);
  5227. list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
  5228. group_entry)
  5229. __perf_event_exit_task(child_event, child_ctx, child);
  5230. /*
  5231. * If the last event was a group event, it will have appended all
  5232. * its siblings to the list, but we obtained 'tmp' before that which
  5233. * will still point to the list head terminating the iteration.
  5234. */
  5235. if (!list_empty(&child_ctx->pinned_groups) ||
  5236. !list_empty(&child_ctx->flexible_groups))
  5237. goto again;
  5238. mutex_unlock(&child_ctx->mutex);
  5239. put_ctx(child_ctx);
  5240. }
  5241. /*
  5242. * When a child task exits, feed back event values to parent events.
  5243. */
  5244. void perf_event_exit_task(struct task_struct *child)
  5245. {
  5246. struct perf_event *event, *tmp;
  5247. int ctxn;
  5248. mutex_lock(&child->perf_event_mutex);
  5249. list_for_each_entry_safe(event, tmp, &child->perf_event_list,
  5250. owner_entry) {
  5251. list_del_init(&event->owner_entry);
  5252. /*
  5253. * Ensure the list deletion is visible before we clear
  5254. * the owner, closes a race against perf_release() where
  5255. * we need to serialize on the owner->perf_event_mutex.
  5256. */
  5257. smp_wmb();
  5258. event->owner = NULL;
  5259. }
  5260. mutex_unlock(&child->perf_event_mutex);
  5261. for_each_task_context_nr(ctxn)
  5262. perf_event_exit_task_context(child, ctxn);
  5263. }
  5264. static void perf_free_event(struct perf_event *event,
  5265. struct perf_event_context *ctx)
  5266. {
  5267. struct perf_event *parent = event->parent;
  5268. if (WARN_ON_ONCE(!parent))
  5269. return;
  5270. mutex_lock(&parent->child_mutex);
  5271. list_del_init(&event->child_list);
  5272. mutex_unlock(&parent->child_mutex);
  5273. fput(parent->filp);
  5274. perf_group_detach(event);
  5275. list_del_event(event, ctx);
  5276. free_event(event);
  5277. }
  5278. /*
  5279. * free an unexposed, unused context as created by inheritance by
  5280. * perf_event_init_task below, used by fork() in case of fail.
  5281. */
  5282. void perf_event_free_task(struct task_struct *task)
  5283. {
  5284. struct perf_event_context *ctx;
  5285. struct perf_event *event, *tmp;
  5286. int ctxn;
  5287. for_each_task_context_nr(ctxn) {
  5288. ctx = task->perf_event_ctxp[ctxn];
  5289. if (!ctx)
  5290. continue;
  5291. mutex_lock(&ctx->mutex);
  5292. again:
  5293. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
  5294. group_entry)
  5295. perf_free_event(event, ctx);
  5296. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
  5297. group_entry)
  5298. perf_free_event(event, ctx);
  5299. if (!list_empty(&ctx->pinned_groups) ||
  5300. !list_empty(&ctx->flexible_groups))
  5301. goto again;
  5302. mutex_unlock(&ctx->mutex);
  5303. put_ctx(ctx);
  5304. }
  5305. }
  5306. void perf_event_delayed_put(struct task_struct *task)
  5307. {
  5308. int ctxn;
  5309. for_each_task_context_nr(ctxn)
  5310. WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
  5311. }
  5312. /*
  5313. * inherit a event from parent task to child task:
  5314. */
  5315. static struct perf_event *
  5316. inherit_event(struct perf_event *parent_event,
  5317. struct task_struct *parent,
  5318. struct perf_event_context *parent_ctx,
  5319. struct task_struct *child,
  5320. struct perf_event *group_leader,
  5321. struct perf_event_context *child_ctx)
  5322. {
  5323. struct perf_event *child_event;
  5324. unsigned long flags;
  5325. /*
  5326. * Instead of creating recursive hierarchies of events,
  5327. * we link inherited events back to the original parent,
  5328. * which has a filp for sure, which we use as the reference
  5329. * count:
  5330. */
  5331. if (parent_event->parent)
  5332. parent_event = parent_event->parent;
  5333. child_event = perf_event_alloc(&parent_event->attr,
  5334. parent_event->cpu,
  5335. child,
  5336. group_leader, parent_event,
  5337. NULL);
  5338. if (IS_ERR(child_event))
  5339. return child_event;
  5340. get_ctx(child_ctx);
  5341. /*
  5342. * Make the child state follow the state of the parent event,
  5343. * not its attr.disabled bit. We hold the parent's mutex,
  5344. * so we won't race with perf_event_{en, dis}able_family.
  5345. */
  5346. if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
  5347. child_event->state = PERF_EVENT_STATE_INACTIVE;
  5348. else
  5349. child_event->state = PERF_EVENT_STATE_OFF;
  5350. if (parent_event->attr.freq) {
  5351. u64 sample_period = parent_event->hw.sample_period;
  5352. struct hw_perf_event *hwc = &child_event->hw;
  5353. hwc->sample_period = sample_period;
  5354. hwc->last_period = sample_period;
  5355. local64_set(&hwc->period_left, sample_period);
  5356. }
  5357. child_event->ctx = child_ctx;
  5358. child_event->overflow_handler = parent_event->overflow_handler;
  5359. /*
  5360. * Precalculate sample_data sizes
  5361. */
  5362. perf_event__header_size(child_event);
  5363. perf_event__id_header_size(child_event);
  5364. /*
  5365. * Link it up in the child's context:
  5366. */
  5367. raw_spin_lock_irqsave(&child_ctx->lock, flags);
  5368. add_event_to_ctx(child_event, child_ctx);
  5369. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  5370. /*
  5371. * Get a reference to the parent filp - we will fput it
  5372. * when the child event exits. This is safe to do because
  5373. * we are in the parent and we know that the filp still
  5374. * exists and has a nonzero count:
  5375. */
  5376. atomic_long_inc(&parent_event->filp->f_count);
  5377. /*
  5378. * Link this into the parent event's child list
  5379. */
  5380. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  5381. mutex_lock(&parent_event->child_mutex);
  5382. list_add_tail(&child_event->child_list, &parent_event->child_list);
  5383. mutex_unlock(&parent_event->child_mutex);
  5384. return child_event;
  5385. }
  5386. static int inherit_group(struct perf_event *parent_event,
  5387. struct task_struct *parent,
  5388. struct perf_event_context *parent_ctx,
  5389. struct task_struct *child,
  5390. struct perf_event_context *child_ctx)
  5391. {
  5392. struct perf_event *leader;
  5393. struct perf_event *sub;
  5394. struct perf_event *child_ctr;
  5395. leader = inherit_event(parent_event, parent, parent_ctx,
  5396. child, NULL, child_ctx);
  5397. if (IS_ERR(leader))
  5398. return PTR_ERR(leader);
  5399. list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  5400. child_ctr = inherit_event(sub, parent, parent_ctx,
  5401. child, leader, child_ctx);
  5402. if (IS_ERR(child_ctr))
  5403. return PTR_ERR(child_ctr);
  5404. }
  5405. return 0;
  5406. }
  5407. static int
  5408. inherit_task_group(struct perf_event *event, struct task_struct *parent,
  5409. struct perf_event_context *parent_ctx,
  5410. struct task_struct *child, int ctxn,
  5411. int *inherited_all)
  5412. {
  5413. int ret;
  5414. struct perf_event_context *child_ctx;
  5415. if (!event->attr.inherit) {
  5416. *inherited_all = 0;
  5417. return 0;
  5418. }
  5419. child_ctx = child->perf_event_ctxp[ctxn];
  5420. if (!child_ctx) {
  5421. /*
  5422. * This is executed from the parent task context, so
  5423. * inherit events that have been marked for cloning.
  5424. * First allocate and initialize a context for the
  5425. * child.
  5426. */
  5427. child_ctx = alloc_perf_context(event->pmu, child);
  5428. if (!child_ctx)
  5429. return -ENOMEM;
  5430. child->perf_event_ctxp[ctxn] = child_ctx;
  5431. }
  5432. ret = inherit_group(event, parent, parent_ctx,
  5433. child, child_ctx);
  5434. if (ret)
  5435. *inherited_all = 0;
  5436. return ret;
  5437. }
  5438. /*
  5439. * Initialize the perf_event context in task_struct
  5440. */
  5441. int perf_event_init_context(struct task_struct *child, int ctxn)
  5442. {
  5443. struct perf_event_context *child_ctx, *parent_ctx;
  5444. struct perf_event_context *cloned_ctx;
  5445. struct perf_event *event;
  5446. struct task_struct *parent = current;
  5447. int inherited_all = 1;
  5448. unsigned long flags;
  5449. int ret = 0;
  5450. if (likely(!parent->perf_event_ctxp[ctxn]))
  5451. return 0;
  5452. /*
  5453. * If the parent's context is a clone, pin it so it won't get
  5454. * swapped under us.
  5455. */
  5456. parent_ctx = perf_pin_task_context(parent, ctxn);
  5457. /*
  5458. * No need to check if parent_ctx != NULL here; since we saw
  5459. * it non-NULL earlier, the only reason for it to become NULL
  5460. * is if we exit, and since we're currently in the middle of
  5461. * a fork we can't be exiting at the same time.
  5462. */
  5463. /*
  5464. * Lock the parent list. No need to lock the child - not PID
  5465. * hashed yet and not running, so nobody can access it.
  5466. */
  5467. mutex_lock(&parent_ctx->mutex);
  5468. /*
  5469. * We dont have to disable NMIs - we are only looking at
  5470. * the list, not manipulating it:
  5471. */
  5472. list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
  5473. ret = inherit_task_group(event, parent, parent_ctx,
  5474. child, ctxn, &inherited_all);
  5475. if (ret)
  5476. break;
  5477. }
  5478. /*
  5479. * We can't hold ctx->lock when iterating the ->flexible_group list due
  5480. * to allocations, but we need to prevent rotation because
  5481. * rotate_ctx() will change the list from interrupt context.
  5482. */
  5483. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  5484. parent_ctx->rotate_disable = 1;
  5485. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  5486. list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
  5487. ret = inherit_task_group(event, parent, parent_ctx,
  5488. child, ctxn, &inherited_all);
  5489. if (ret)
  5490. break;
  5491. }
  5492. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  5493. parent_ctx->rotate_disable = 0;
  5494. child_ctx = child->perf_event_ctxp[ctxn];
  5495. if (child_ctx && inherited_all) {
  5496. /*
  5497. * Mark the child context as a clone of the parent
  5498. * context, or of whatever the parent is a clone of.
  5499. *
  5500. * Note that if the parent is a clone, the holding of
  5501. * parent_ctx->lock avoids it from being uncloned.
  5502. */
  5503. cloned_ctx = parent_ctx->parent_ctx;
  5504. if (cloned_ctx) {
  5505. child_ctx->parent_ctx = cloned_ctx;
  5506. child_ctx->parent_gen = parent_ctx->parent_gen;
  5507. } else {
  5508. child_ctx->parent_ctx = parent_ctx;
  5509. child_ctx->parent_gen = parent_ctx->generation;
  5510. }
  5511. get_ctx(child_ctx->parent_ctx);
  5512. }
  5513. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  5514. mutex_unlock(&parent_ctx->mutex);
  5515. perf_unpin_context(parent_ctx);
  5516. put_ctx(parent_ctx);
  5517. return ret;
  5518. }
  5519. /*
  5520. * Initialize the perf_event context in task_struct
  5521. */
  5522. int perf_event_init_task(struct task_struct *child)
  5523. {
  5524. int ctxn, ret;
  5525. memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
  5526. mutex_init(&child->perf_event_mutex);
  5527. INIT_LIST_HEAD(&child->perf_event_list);
  5528. for_each_task_context_nr(ctxn) {
  5529. ret = perf_event_init_context(child, ctxn);
  5530. if (ret)
  5531. return ret;
  5532. }
  5533. return 0;
  5534. }
  5535. static void __init perf_event_init_all_cpus(void)
  5536. {
  5537. struct swevent_htable *swhash;
  5538. int cpu;
  5539. for_each_possible_cpu(cpu) {
  5540. swhash = &per_cpu(swevent_htable, cpu);
  5541. mutex_init(&swhash->hlist_mutex);
  5542. INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
  5543. }
  5544. }
  5545. static void __cpuinit perf_event_init_cpu(int cpu)
  5546. {
  5547. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5548. mutex_lock(&swhash->hlist_mutex);
  5549. if (swhash->hlist_refcount > 0) {
  5550. struct swevent_hlist *hlist;
  5551. hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
  5552. WARN_ON(!hlist);
  5553. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  5554. }
  5555. mutex_unlock(&swhash->hlist_mutex);
  5556. }
  5557. #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
  5558. static void perf_pmu_rotate_stop(struct pmu *pmu)
  5559. {
  5560. struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  5561. WARN_ON(!irqs_disabled());
  5562. list_del_init(&cpuctx->rotation_list);
  5563. }
  5564. static void __perf_event_exit_context(void *__info)
  5565. {
  5566. struct perf_event_context *ctx = __info;
  5567. struct perf_event *event, *tmp;
  5568. perf_pmu_rotate_stop(ctx->pmu);
  5569. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
  5570. __perf_remove_from_context(event);
  5571. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
  5572. __perf_remove_from_context(event);
  5573. }
  5574. static void perf_event_exit_cpu_context(int cpu)
  5575. {
  5576. struct perf_event_context *ctx;
  5577. struct pmu *pmu;
  5578. int idx;
  5579. idx = srcu_read_lock(&pmus_srcu);
  5580. list_for_each_entry_rcu(pmu, &pmus, entry) {
  5581. ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
  5582. mutex_lock(&ctx->mutex);
  5583. smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
  5584. mutex_unlock(&ctx->mutex);
  5585. }
  5586. srcu_read_unlock(&pmus_srcu, idx);
  5587. }
  5588. static void perf_event_exit_cpu(int cpu)
  5589. {
  5590. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5591. mutex_lock(&swhash->hlist_mutex);
  5592. swevent_hlist_release(swhash);
  5593. mutex_unlock(&swhash->hlist_mutex);
  5594. perf_event_exit_cpu_context(cpu);
  5595. }
  5596. #else
  5597. static inline void perf_event_exit_cpu(int cpu) { }
  5598. #endif
  5599. static int
  5600. perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
  5601. {
  5602. int cpu;
  5603. for_each_online_cpu(cpu)
  5604. perf_event_exit_cpu(cpu);
  5605. return NOTIFY_OK;
  5606. }
  5607. /*
  5608. * Run the perf reboot notifier at the very last possible moment so that
  5609. * the generic watchdog code runs as long as possible.
  5610. */
  5611. static struct notifier_block perf_reboot_notifier = {
  5612. .notifier_call = perf_reboot,
  5613. .priority = INT_MIN,
  5614. };
  5615. static int __cpuinit
  5616. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  5617. {
  5618. unsigned int cpu = (long)hcpu;
  5619. switch (action & ~CPU_TASKS_FROZEN) {
  5620. case CPU_UP_PREPARE:
  5621. case CPU_DOWN_FAILED:
  5622. perf_event_init_cpu(cpu);
  5623. break;
  5624. case CPU_UP_CANCELED:
  5625. case CPU_DOWN_PREPARE:
  5626. perf_event_exit_cpu(cpu);
  5627. break;
  5628. default:
  5629. break;
  5630. }
  5631. return NOTIFY_OK;
  5632. }
  5633. void __init perf_event_init(void)
  5634. {
  5635. int ret;
  5636. idr_init(&pmu_idr);
  5637. perf_event_init_all_cpus();
  5638. init_srcu_struct(&pmus_srcu);
  5639. perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
  5640. perf_pmu_register(&perf_cpu_clock, NULL, -1);
  5641. perf_pmu_register(&perf_task_clock, NULL, -1);
  5642. perf_tp_register();
  5643. perf_cpu_notifier(perf_cpu_notify);
  5644. register_reboot_notifier(&perf_reboot_notifier);
  5645. ret = init_hw_breakpoint();
  5646. WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
  5647. }
  5648. static int __init perf_event_sysfs_init(void)
  5649. {
  5650. struct pmu *pmu;
  5651. int ret;
  5652. mutex_lock(&pmus_lock);
  5653. ret = bus_register(&pmu_bus);
  5654. if (ret)
  5655. goto unlock;
  5656. list_for_each_entry(pmu, &pmus, entry) {
  5657. if (!pmu->name || pmu->type < 0)
  5658. continue;
  5659. ret = pmu_dev_alloc(pmu);
  5660. WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
  5661. }
  5662. pmu_bus_running = 1;
  5663. ret = 0;
  5664. unlock:
  5665. mutex_unlock(&pmus_lock);
  5666. return ret;
  5667. }
  5668. device_initcall(perf_event_sysfs_init);
  5669. #ifdef CONFIG_CGROUP_PERF
  5670. static struct cgroup_subsys_state *perf_cgroup_create(
  5671. struct cgroup_subsys *ss, struct cgroup *cont)
  5672. {
  5673. struct perf_cgroup *jc;
  5674. jc = kzalloc(sizeof(*jc), GFP_KERNEL);
  5675. if (!jc)
  5676. return ERR_PTR(-ENOMEM);
  5677. jc->info = alloc_percpu(struct perf_cgroup_info);
  5678. if (!jc->info) {
  5679. kfree(jc);
  5680. return ERR_PTR(-ENOMEM);
  5681. }
  5682. return &jc->css;
  5683. }
  5684. static void perf_cgroup_destroy(struct cgroup_subsys *ss,
  5685. struct cgroup *cont)
  5686. {
  5687. struct perf_cgroup *jc;
  5688. jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
  5689. struct perf_cgroup, css);
  5690. free_percpu(jc->info);
  5691. kfree(jc);
  5692. }
  5693. static int __perf_cgroup_move(void *info)
  5694. {
  5695. struct task_struct *task = info;
  5696. perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
  5697. return 0;
  5698. }
  5699. static void
  5700. perf_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *task)
  5701. {
  5702. task_function_call(task, __perf_cgroup_move, task);
  5703. }
  5704. static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
  5705. struct cgroup *old_cgrp, struct task_struct *task)
  5706. {
  5707. /*
  5708. * cgroup_exit() is called in the copy_process() failure path.
  5709. * Ignore this case since the task hasn't ran yet, this avoids
  5710. * trying to poke a half freed task state from generic code.
  5711. */
  5712. if (!(task->flags & PF_EXITING))
  5713. return;
  5714. perf_cgroup_attach_task(cgrp, task);
  5715. }
  5716. struct cgroup_subsys perf_subsys = {
  5717. .name = "perf_event",
  5718. .subsys_id = perf_subsys_id,
  5719. .create = perf_cgroup_create,
  5720. .destroy = perf_cgroup_destroy,
  5721. .exit = perf_cgroup_exit,
  5722. .attach_task = perf_cgroup_attach_task,
  5723. };
  5724. #endif /* CONFIG_CGROUP_PERF */