core.c 185 KB

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