core.c 160 KB

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