core.c 169 KB

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