core.c 160 KB

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