core.c 164 KB

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