core.c 173 KB

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