core.c 161 KB

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