core.c 187 KB

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