core.c 186 KB

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