core.c 168 KB

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