core.c 193 KB

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