core.c 169 KB

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