core.c 183 KB

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